From bcf8aa2c8772efb65d672e75e1b5ab16137971ad Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Thu, 13 Feb 2025 14:44:33 -0500 Subject: [PATCH 001/111] Allocate lexer inside `alloc_parser()` --- ext/rbs_extension/parser.c | 9 +++------ ext/rbs_extension/parserstate.c | 3 ++- ext/rbs_extension/parserstate.h | 6 +++--- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index fed9a0e83..471356489 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -2882,8 +2882,7 @@ rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, V { VALUE string = rb_funcall(buffer, rb_intern("content"), 0); StringValue(string); - lexstate *lexer = alloc_lexer(string, FIX2INT(start_pos), FIX2INT(end_pos)); - parserstate *parser = alloc_parser(buffer, lexer, FIX2INT(start_pos), FIX2INT(end_pos), variables); + parserstate *parser = alloc_parser(buffer, string, FIX2INT(start_pos), FIX2INT(end_pos), variables); struct parse_type_arg arg = { parser, require_eof @@ -2913,8 +2912,7 @@ rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end { VALUE string = rb_funcall(buffer, rb_intern("content"), 0); StringValue(string); - lexstate *lexer = alloc_lexer(string, FIX2INT(start_pos), FIX2INT(end_pos)); - parserstate *parser = alloc_parser(buffer, lexer, FIX2INT(start_pos), FIX2INT(end_pos), variables); + parserstate *parser = alloc_parser(buffer, string, FIX2INT(start_pos), FIX2INT(end_pos), variables); struct parse_type_arg arg = { parser, require_eof @@ -2933,8 +2931,7 @@ rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_p { VALUE string = rb_funcall(buffer, rb_intern("content"), 0); StringValue(string); - lexstate *lexer = alloc_lexer(string, FIX2INT(start_pos), FIX2INT(end_pos)); - parserstate *parser = alloc_parser(buffer, lexer, FIX2INT(start_pos), FIX2INT(end_pos), Qnil); + parserstate *parser = alloc_parser(buffer, string, FIX2INT(start_pos), FIX2INT(end_pos), Qnil); return rb_ensure(parse_signature_try, (VALUE)parser, ensure_free_parser, (VALUE)parser); } diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index ceb3d3cc5..5670ddcf0 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -319,7 +319,8 @@ lexstate *alloc_lexer(VALUE string, int start_pos, int end_pos) { return lexer; } -parserstate *alloc_parser(VALUE buffer, lexstate *lexer, int start_pos, int end_pos, VALUE variables) { +parserstate *alloc_parser(VALUE buffer, VALUE string, int start_pos, int end_pos, VALUE variables) { + lexstate *lexer = alloc_lexer(string, start_pos, end_pos); parserstate *parser = malloc(sizeof(parserstate)); *parser = (parserstate) { diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index d4c5d17a5..9d315586b 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -109,11 +109,11 @@ lexstate *alloc_lexer(VALUE string, int start_pos, int end_pos); * Allocate new parserstate object. * * ``` - * alloc_parser(buffer, lexer, 0, 1, variables) // New parserstate with variables - * alloc_parser(buffer, lexer, 3, 5, Qnil) // New parserstate without variables + * alloc_parser(buffer, VALUE string, 0, 1, variables) // New parserstate with variables + * alloc_parser(buffer, VALUE string, 3, 5, Qnil) // New parserstate without variables * ``` * */ -parserstate *alloc_parser(VALUE buffer, lexstate *lexer, int start_pos, int end_pos, VALUE variables); +parserstate *alloc_parser(VALUE buffer, VALUE string, int start_pos, int end_pos, VALUE variables); void free_parser(parserstate *parser); /** * Advance one token. From 5f8e833b44353a1c50e33f30ea7ff747b65df2e9 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Thu, 13 Feb 2025 13:48:43 -0500 Subject: [PATCH 002/111] Add new arena allocator Initial template for C structs Use allocator in node constructors --- Rakefile | 9 +- config.yml | 3 + ext/rbs_extension/ast_translation.c | 455 ++++++ ext/rbs_extension/ast_translation.h | 16 + ext/rbs_extension/main.c | 3 +- ext/rbs_extension/parser.c | 118 +- ext/rbs_extension/parserstate.c | 65 +- ext/rbs_extension/parserstate.h | 10 +- include/rbs.h | 1 + include/rbs/ast.h | 635 ++++++++ include/rbs/util/rbs_allocator.h | 21 + src/ast.c | 1287 +++++++++++++++++ src/util/rbs_allocator.c | 184 +++ sync_from_prism.rb | 37 + .../ext/rbs_extension/ast_translation.c.erb | 44 + .../ext/rbs_extension/ast_translation.h.erb | 9 + templates/include/rbs/ast.h.erb | 35 + templates/include/rbs/constants.h.erb | 2 +- templates/include/rbs/ruby_objs.h.erb | 2 +- templates/src/ast.c.erb | 26 + templates/src/constants.c.erb | 4 +- templates/src/ruby_objs.c.erb | 2 +- templates/template.rb | 85 +- 23 files changed, 2958 insertions(+), 95 deletions(-) create mode 100644 ext/rbs_extension/ast_translation.c create mode 100644 ext/rbs_extension/ast_translation.h create mode 100644 include/rbs/ast.h create mode 100644 include/rbs/util/rbs_allocator.h create mode 100644 src/ast.c create mode 100644 src/util/rbs_allocator.c create mode 100644 sync_from_prism.rb create mode 100644 templates/ext/rbs_extension/ast_translation.c.erb create mode 100644 templates/ext/rbs_extension/ast_translation.h.erb create mode 100644 templates/include/rbs/ast.h.erb create mode 100644 templates/src/ast.c.erb diff --git a/Rakefile b/Rakefile index e75433ca5..a244dd63d 100644 --- a/Rakefile +++ b/Rakefile @@ -56,9 +56,16 @@ task :confirm_annotation do end task :templates do + sh "#{ruby} templates/template.rb ext/rbs_extension/ast_translation.h" + sh "#{ruby} templates/template.rb ext/rbs_extension/ast_translation.c" + + sh "#{ruby} templates/template.rb include/rbs/ast.h" + sh "#{ruby} templates/template.rb src/ast.c" + sh "#{ruby} templates/template.rb include/rbs/constants.h" - sh "#{ruby} templates/template.rb include/rbs/ruby_objs.h" sh "#{ruby} templates/template.rb src/constants.c" + + sh "#{ruby} templates/template.rb include/rbs/ruby_objs.h" sh "#{ruby} templates/template.rb src/ruby_objs.c" end diff --git a/config.yml b/config.yml index 8a79c19e9..963348c60 100644 --- a/config.yml +++ b/config.yml @@ -311,3 +311,6 @@ nodes: fields: - name: name - name: location + - name: RBS::Types::ZzzTmpNotImplemented + expose_to_ruby: false + fields: [] diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c new file mode 100644 index 000000000..a965ed0d8 --- /dev/null +++ b/ext/rbs_extension/ast_translation.c @@ -0,0 +1,455 @@ +/*----------------------------------------------------------------------------*/ +/* This file is generated by the templates/template.rb script and should not */ +/* be modified manually. */ +/* To change the template see */ +/* templates/ext/rbs_extension/ast_translation.c.erb */ +/*----------------------------------------------------------------------------*/ + +#include "ast_translation.h" + + +const char* get_class_name(VALUE o) { + VALUE klass = rb_class_of(o); // Get the class of the object + VALUE klass_name = rb_class_name(klass); // Get the name of the class + const char* name = StringValueCStr(klass_name); // Convert to C string + return name; +} + +VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { + if (instance == NULL) { + fprintf(stderr, "Tried to call rbs_struct_to_ruby_value(NULL)\n"); + exit(1); + } + + if (instance->type == RBS_TYPES_ZZZTMPNOTIMPLEMENTED) { + // Special case: skip assertions/translation below. + return instance->cached_ruby_value; + } + + VALUE ruby_value = instance->cached_ruby_value; + + if (ruby_value == Qnil || ruby_value == Qundef) { + fprintf(stderr, "cached_ruby_value is NULL\n"); + exit(1); + } + + const char *class_name = get_class_name(ruby_value); + + switch (instance->type) { + case RBS_AST_ANNOTATION: { + if (strcmp(class_name, "RBS::AST::Annotation") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Annotation, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_COMMENT: { + if (strcmp(class_name, "RBS::AST::Comment") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Comment, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_DECLARATIONS_CLASS: { + if (strcmp(class_name, "RBS::AST::Declarations::Class") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Declarations::Class, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_DECLARATIONS_CLASS_SUPER: { + if (strcmp(class_name, "RBS::AST::Declarations::Class::Super") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Declarations::Class::Super, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_DECLARATIONS_CLASSALIAS: { + if (strcmp(class_name, "RBS::AST::Declarations::ClassAlias") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Declarations::ClassAlias, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_DECLARATIONS_CONSTANT: { + if (strcmp(class_name, "RBS::AST::Declarations::Constant") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Declarations::Constant, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_DECLARATIONS_GLOBAL: { + if (strcmp(class_name, "RBS::AST::Declarations::Global") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Declarations::Global, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_DECLARATIONS_INTERFACE: { + if (strcmp(class_name, "RBS::AST::Declarations::Interface") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Declarations::Interface, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_DECLARATIONS_MODULE: { + if (strcmp(class_name, "RBS::AST::Declarations::Module") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Declarations::Module, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_DECLARATIONS_MODULE_SELF: { + if (strcmp(class_name, "RBS::AST::Declarations::Module::Self") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Declarations::Module::Self, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_DECLARATIONS_MODULEALIAS: { + if (strcmp(class_name, "RBS::AST::Declarations::ModuleAlias") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Declarations::ModuleAlias, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_DECLARATIONS_TYPEALIAS: { + if (strcmp(class_name, "RBS::AST::Declarations::TypeAlias") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Declarations::TypeAlias, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_DIRECTIVES_USE: { + if (strcmp(class_name, "RBS::AST::Directives::Use") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Directives::Use, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_DIRECTIVES_USE_SINGLECLAUSE: { + if (strcmp(class_name, "RBS::AST::Directives::Use::SingleClause") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Directives::Use::SingleClause, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE: { + if (strcmp(class_name, "RBS::AST::Directives::Use::WildcardClause") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Directives::Use::WildcardClause, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_MEMBERS_ALIAS: { + if (strcmp(class_name, "RBS::AST::Members::Alias") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Members::Alias, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_MEMBERS_ATTRACCESSOR: { + if (strcmp(class_name, "RBS::AST::Members::AttrAccessor") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Members::AttrAccessor, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_MEMBERS_ATTRREADER: { + if (strcmp(class_name, "RBS::AST::Members::AttrReader") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Members::AttrReader, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_MEMBERS_ATTRWRITER: { + if (strcmp(class_name, "RBS::AST::Members::AttrWriter") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Members::AttrWriter, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE: { + if (strcmp(class_name, "RBS::AST::Members::ClassInstanceVariable") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Members::ClassInstanceVariable, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_MEMBERS_CLASSVARIABLE: { + if (strcmp(class_name, "RBS::AST::Members::ClassVariable") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Members::ClassVariable, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_MEMBERS_EXTEND: { + if (strcmp(class_name, "RBS::AST::Members::Extend") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Members::Extend, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_MEMBERS_INCLUDE: { + if (strcmp(class_name, "RBS::AST::Members::Include") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Members::Include, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_MEMBERS_INSTANCEVARIABLE: { + if (strcmp(class_name, "RBS::AST::Members::InstanceVariable") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Members::InstanceVariable, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_MEMBERS_METHODDEFINITION: { + if (strcmp(class_name, "RBS::AST::Members::MethodDefinition") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Members::MethodDefinition, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD: { + if (strcmp(class_name, "RBS::AST::Members::MethodDefinition::Overload") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Members::MethodDefinition::Overload, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_MEMBERS_PREPEND: { + if (strcmp(class_name, "RBS::AST::Members::Prepend") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Members::Prepend, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_MEMBERS_PRIVATE: { + if (strcmp(class_name, "RBS::AST::Members::Private") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Members::Private, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_MEMBERS_PUBLIC: { + if (strcmp(class_name, "RBS::AST::Members::Public") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Members::Public, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_AST_TYPEPARAM: { + if (strcmp(class_name, "RBS::AST::TypeParam") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::TypeParam, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_METHODTYPE: { + if (strcmp(class_name, "RBS::MethodType") != 0) { + fprintf(stderr, "Expected class name: RBS::MethodType, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_NAMESPACE: { + if (strcmp(class_name, "RBS::Namespace") != 0) { + fprintf(stderr, "Expected class name: RBS::Namespace, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPENAME: { + if (strcmp(class_name, "RBS::TypeName") != 0) { + fprintf(stderr, "Expected class name: RBS::TypeName, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_ALIAS: { + if (strcmp(class_name, "RBS::Types::Alias") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Alias, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_BASES_ANY: { + if (strcmp(class_name, "RBS::Types::Bases::Any") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Bases::Any, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_BASES_BOOL: { + if (strcmp(class_name, "RBS::Types::Bases::Bool") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Bases::Bool, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_BASES_BOTTOM: { + if (strcmp(class_name, "RBS::Types::Bases::Bottom") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Bases::Bottom, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_BASES_CLASS: { + if (strcmp(class_name, "RBS::Types::Bases::Class") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Bases::Class, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_BASES_INSTANCE: { + if (strcmp(class_name, "RBS::Types::Bases::Instance") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Bases::Instance, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_BASES_NIL: { + if (strcmp(class_name, "RBS::Types::Bases::Nil") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Bases::Nil, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_BASES_SELF: { + if (strcmp(class_name, "RBS::Types::Bases::Self") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Bases::Self, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_BASES_TOP: { + if (strcmp(class_name, "RBS::Types::Bases::Top") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Bases::Top, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_BASES_VOID: { + if (strcmp(class_name, "RBS::Types::Bases::Void") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Bases::Void, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_BLOCK: { + if (strcmp(class_name, "RBS::Types::Block") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Block, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_CLASSINSTANCE: { + if (strcmp(class_name, "RBS::Types::ClassInstance") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::ClassInstance, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_CLASSSINGLETON: { + if (strcmp(class_name, "RBS::Types::ClassSingleton") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::ClassSingleton, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_FUNCTION: { + if (strcmp(class_name, "RBS::Types::Function") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Function, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_FUNCTION_PARAM: { + if (strcmp(class_name, "RBS::Types::Function::Param") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Function::Param, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_INTERFACE: { + if (strcmp(class_name, "RBS::Types::Interface") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Interface, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_INTERSECTION: { + if (strcmp(class_name, "RBS::Types::Intersection") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Intersection, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_LITERAL: { + if (strcmp(class_name, "RBS::Types::Literal") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Literal, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_OPTIONAL: { + if (strcmp(class_name, "RBS::Types::Optional") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Optional, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_PROC: { + if (strcmp(class_name, "RBS::Types::Proc") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Proc, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_RECORD: { + if (strcmp(class_name, "RBS::Types::Record") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Record, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_TUPLE: { + if (strcmp(class_name, "RBS::Types::Tuple") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Tuple, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_UNION: { + if (strcmp(class_name, "RBS::Types::Union") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Union, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_UNTYPEDFUNCTION: { + if (strcmp(class_name, "RBS::Types::UntypedFunction") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::UntypedFunction, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_VARIABLE: { + if (strcmp(class_name, "RBS::Types::Variable") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::Variable, got %s\n", class_name); + exit(1); + } + break; + } + case RBS_TYPES_ZZZTMPNOTIMPLEMENTED: { + if (strcmp(class_name, "RBS::Types::ZzzTmpNotImplemented") != 0) { + fprintf(stderr, "Expected class name: RBS::Types::ZzzTmpNotImplemented, got %s\n", class_name); + exit(1); + } + break; + } + } + + return instance->cached_ruby_value; +} diff --git a/ext/rbs_extension/ast_translation.h b/ext/rbs_extension/ast_translation.h new file mode 100644 index 000000000..e2b333ee9 --- /dev/null +++ b/ext/rbs_extension/ast_translation.h @@ -0,0 +1,16 @@ +/*----------------------------------------------------------------------------*/ +/* This file is generated by the templates/template.rb script and should not */ +/* be modified manually. */ +/* To change the template see */ +/* templates/ext/rbs_extension/ast_translation.h.erb */ +/*----------------------------------------------------------------------------*/ + +#ifndef RBS_EXTENSION_AST_TRANSLATION_H +#define RBS_EXTENSION_AST_TRANSLATION_H + +#include "ruby.h" +#include "rbs/ast.h" + +VALUE rbs_struct_to_ruby_value(rbs_node_t *instance); + +#endif diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 227ea3b5b..a59a3f2e9 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -1,6 +1,6 @@ #include "rbs_extension.h" +#include "rbs/util/rbs_allocator.h" #include "rbs/util/rbs_constant_pool.h" - #include "ruby/vm.h" static @@ -14,6 +14,7 @@ Init_rbs_extension(void) #ifdef HAVE_RB_EXT_RACTOR_SAFE rb_ext_ractor_safe(true); #endif + rbs__init_arena_allocator(); rbs__init_constants(); rbs__init_location(); rbs__init_parser(); diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index 471356489..719cb26e8 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -1,5 +1,7 @@ #include "rbs_extension.h" #include "rbs/util/rbs_constant_pool.h" +#include "rbs/ast.h" +#include "ast_translation.h" #define INTERN(str) \ rbs_constant_pool_insert_constant( \ @@ -87,7 +89,7 @@ static VALUE rbs_location_current_token(parserstate *state) { } static VALUE parse_optional(parserstate *state); -static VALUE parse_simple(parserstate *state); +static rbs_node_t *parse_simple(parserstate *state); static VALUE string_of_loc(parserstate *state, position start, position end) { return rb_enc_str_new( @@ -594,8 +596,7 @@ static void parse_params(parserstate *state, method_params *params) { static VALUE parse_optional(parserstate *state) { range rg; rg.start = state->next_token.range.start; - - VALUE type = parse_simple(state); + VALUE type = rbs_struct_to_ruby_value(parse_simple(state)); if (state->next_token.type == pQUESTION) { parser_advance(state); @@ -797,7 +798,7 @@ static VALUE parse_record_attributes(parserstate *state) { case tINTEGER: case kTRUE: case kFALSE: { - key = rb_funcall(parse_simple(state), rb_intern("literal"), 0); + key = rb_funcall(rbs_struct_to_ruby_value(parse_simple(state)), rb_intern("literal"), 0); break; } default: @@ -959,74 +960,101 @@ static VALUE parse_singleton_type(parserstate *state) { | {} `{` record_attributes <`}`> | {} `^` */ -static VALUE parse_simple(parserstate *state) { +static rbs_node_t *parse_simple(parserstate *state) { parser_advance(state); switch (state->current_token.type) { case pLPAREN: { VALUE type = parse_type(state); parser_advance_assert(state, pRPAREN); - return type; + + return (rbs_node_t *) rbs_types_zzztmpnotimplemented_new(&state->allocator, type); } case kBOOL: { - return rbs_bases_bool(rbs_location_current_token(state)); + VALUE loc = rbs_location_current_token(state); + VALUE value = rbs_bases_bool(loc); + return (rbs_node_t *) rbs_types_bases_bool_new(&state->allocator, value, loc); } case kBOT: { - return rbs_bases_bottom(rbs_location_current_token(state)); + VALUE loc = rbs_location_current_token(state); + VALUE value = rbs_bases_bottom(loc); + return (rbs_node_t *) rbs_types_bases_bottom_new(&state->allocator, value, loc); } case kCLASS: { - return rbs_bases_class(rbs_location_current_token(state)); + VALUE loc = rbs_location_current_token(state); + VALUE value = rbs_bases_class(loc); + return (rbs_node_t *) rbs_types_bases_class_new(&state->allocator, value, loc); } case kINSTANCE: { - return rbs_bases_instance(rbs_location_current_token(state)); + VALUE loc = rbs_location_current_token(state); + VALUE value = rbs_bases_instance(loc); + return (rbs_node_t *) rbs_types_bases_instance_new(&state->allocator, value, loc); } case kNIL: { - return rbs_bases_nil(rbs_location_current_token(state)); + VALUE loc = rbs_location_current_token(state); + VALUE value = rbs_bases_nil(loc); + return (rbs_node_t *) rbs_types_bases_nil_new(&state->allocator, value, loc); } case kSELF: { - return rbs_bases_self(rbs_location_current_token(state)); + VALUE loc = rbs_location_current_token(state); + VALUE value = rbs_bases_self(loc); + return (rbs_node_t *) rbs_types_bases_self_new(&state->allocator, value, loc); } case kTOP: { - return rbs_bases_top(rbs_location_current_token(state)); + VALUE loc = rbs_location_current_token(state); + VALUE value = rbs_bases_top(loc); + return (rbs_node_t *) rbs_types_bases_top_new(&state->allocator, value, loc); } case kVOID: { - return rbs_bases_void(rbs_location_current_token(state)); + VALUE loc = rbs_location_current_token(state); + VALUE value = rbs_bases_void(loc); + return (rbs_node_t *) rbs_types_bases_void_new(&state->allocator, value, loc); } case kUNTYPED: { - return rbs_bases_any(false, rbs_location_current_token(state)); + VALUE loc = rbs_location_current_token(state); + VALUE value = rbs_bases_any(Qfalse, loc); + return (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, value, Qfalse, loc); } case k__TODO__: { - return rbs_bases_any(true, rbs_location_current_token(state)); + VALUE loc = rbs_location_current_token(state); + VALUE value = rbs_bases_any(Qtrue, loc); + return (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, value, Qtrue, loc); } case tINTEGER: { + VALUE loc = rbs_location_current_token(state); VALUE literal = rb_funcall( string_of_loc(state, state->current_token.range.start, state->current_token.range.end), rb_intern("to_i"), 0 ); - return rbs_literal( - literal, - rbs_location_current_token(state) - ); + VALUE value = rbs_literal(literal, loc); + return (rbs_node_t *) rbs_types_literal_new(&state->allocator, value, literal, loc); } case kTRUE: { - return rbs_literal(Qtrue, rbs_location_current_token(state)); + VALUE loc = rbs_location_current_token(state); + VALUE literal = Qtrue; + VALUE value = rbs_literal(literal, loc); + return (rbs_node_t *) rbs_types_literal_new(&state->allocator, value, literal, loc); } case kFALSE: { - return rbs_literal(Qfalse, rbs_location_current_token(state)); + VALUE loc = rbs_location_current_token(state); + VALUE literal = Qfalse; + VALUE value = rbs_literal(literal, loc); + return (rbs_node_t *) rbs_types_literal_new(&state->allocator, value, literal, loc); } case tSQSTRING: case tDQSTRING: { + VALUE loc = rbs_location_current_token(state); VALUE literal = rbs_unquote_string(state, state->current_token.range, 0); - return rbs_literal( - literal, - rbs_location_current_token(state) - ); + VALUE value = rbs_literal(literal, loc); + return (rbs_node_t *) rbs_types_literal_new(&state->allocator, value, literal, loc); } case tSYMBOL: case tSQSYMBOL: case tDQSYMBOL: { - return parse_symbol(state); + VALUE loc = rbs_location_current_token(state); + VALUE value = parse_symbol(state); + return (rbs_node_t *) rbs_types_literal_new(&state->allocator, value, value, loc); } case tUIDENT: { const char *name_str = peek_token(state->lexstate, state->current_token); @@ -1036,17 +1064,22 @@ static VALUE parse_simple(parserstate *state) { if (parser_typevar_member(state, name)) { ID name = rb_intern3(name_str, name_len, rb_enc_get(state->lexstate->string)); - return rbs_variable(ID2SYM(name), rbs_location_current_token(state)); + VALUE loc = rbs_location_current_token(state); + VALUE value = rbs_variable(ID2SYM(name), loc); + return (rbs_node_t *) rbs_types_variable_new(&state->allocator, value, ID2SYM(name), loc); } // fallthrough for type name } case tULIDENT: // fallthrough case tLIDENT: // fallthrough case pCOLON2: { - return parse_instance_type(state, true); + VALUE value = parse_instance_type(state, true); + return (rbs_node_t *) rbs_types_zzztmpnotimplemented_new(&state->allocator, value); } case kSINGLETON: { - return parse_singleton_type(state); + VALUE value = parse_singleton_type(state); + VALUE loc = rb_funcall(value, rb_intern("location"), 0); + return (rbs_node_t *) rbs_types_classsingleton_new(&state->allocator, value, value, loc); } case pLBRACKET: { range rg; @@ -1058,21 +1091,27 @@ static VALUE parse_simple(parserstate *state) { parser_advance_assert(state, pRBRACKET); rg.end = state->current_token.range.end; - return rbs_tuple(types, rbs_new_location(state->buffer, rg)); + VALUE location = rbs_new_location(state->buffer, rg); + VALUE value = rbs_tuple(types, location); + return (rbs_node_t *) rbs_types_tuple_new(&state->allocator, value, types, location); } case pAREF_OPR: { - return rbs_tuple(EMPTY_ARRAY, rbs_new_location(state->buffer, state->current_token.range)); + VALUE loc = rbs_location_current_token(state); + VALUE value = rbs_tuple(EMPTY_ARRAY, loc); + return (rbs_node_t *) rbs_types_tuple_new(&state->allocator, value, EMPTY_ARRAY, loc); } case pLBRACE: { position start = state->current_token.range.start; VALUE fields = parse_record_attributes(state); parser_advance_assert(state, pRBRACE); position end = state->current_token.range.end; - VALUE location = rbs_location_pp(state->buffer, &start, &end); - return rbs_record(fields, location); + VALUE loc = rbs_location_pp(state->buffer, &start, &end); + VALUE value = rbs_record(fields, loc); + return (rbs_node_t *) rbs_types_record_new(&state->allocator, value, fields, loc); } case pHAT: { - return parse_proc_type(state); + VALUE value = parse_proc_type(state); + return (rbs_node_t *) rbs_types_zzztmpnotimplemented_new(&state->allocator, value); } default: raise_syntax_error( @@ -2939,9 +2978,12 @@ static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) { VALUE string = rb_funcall(buffer, rb_intern("content"), 0); StringValue(string); - lexstate *lexer = alloc_lexer(string, 0, FIX2INT(end_pos)); - VALUE results = rb_ary_new(); + rbs_allocator_t allocator; + rbs_allocator_init(&allocator); + lexstate *lexer = alloc_lexer(&allocator, string, 0, FIX2INT(end_pos)); + + VALUE results = rb_ary_new(); token token = NullToken; while (token.type != pEOF) { token = rbsparser_next_token(lexer); @@ -2951,7 +2993,7 @@ rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) { rb_ary_push(results, pair); } - free(lexer); + rbs_allocator_free(&allocator); return results; } diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index 5670ddcf0..ca04f41d9 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -1,23 +1,22 @@ #include "rbs_extension.h" -#include "rbs/util/rbs_constant_pool.h" #define RESET_TABLE_P(table) (table->size == 0) -id_table *alloc_empty_table(void) { - id_table *table = malloc(sizeof(id_table)); +id_table *alloc_empty_table(rbs_allocator_t *allocator) { + id_table *table = rbs_allocator_alloc(allocator, id_table); *table = (id_table) { .size = 10, .count = 0, - .ids = calloc(10, sizeof(rbs_constant_id_t)), + .ids = rbs_allocator_calloc(allocator, 10, rbs_constant_id_t), .next = NULL, }; return table; } -id_table *alloc_reset_table(void) { - id_table *table = malloc(sizeof(id_table)); +id_table *alloc_reset_table(rbs_allocator_t *allocator) { + id_table *table = rbs_allocator_alloc(allocator, id_table); *table = (id_table) { .size = 0, @@ -31,12 +30,12 @@ id_table *alloc_reset_table(void) { id_table *parser_push_typevar_table(parserstate *state, bool reset) { if (reset) { - id_table *table = alloc_reset_table(); + id_table *table = alloc_reset_table(&state->allocator); table->next = state->vars; state->vars = table; } - id_table *table = alloc_empty_table(); + id_table *table = alloc_empty_table(&state->allocator); table->next = state->vars; state->vars = table; @@ -49,8 +48,6 @@ void parser_pop_typevar_table(parserstate *state) { if (state->vars) { table = state->vars; state->vars = table->next; - free(table->ids); - free(table); } else { rb_raise(rb_eRuntimeError, "Cannot pop empty table"); } @@ -58,7 +55,6 @@ void parser_pop_typevar_table(parserstate *state) { if (state->vars && RESET_TABLE_P(state->vars)) { table = state->vars; state->vars = table->next; - free(table); } } @@ -73,9 +69,8 @@ void parser_insert_typevar(parserstate *state, rbs_constant_id_t id) { // expand rbs_constant_id_t *ptr = table->ids; table->size += 10; - table->ids = calloc(table->size, sizeof(rbs_constant_id_t)); + table->ids = rbs_allocator_calloc(&state->allocator, table->size, rbs_constant_id_t); memcpy(table->ids, ptr, sizeof(rbs_constant_id_t) * table->count); - free(ptr); } table->ids[table->count++] = id; @@ -174,9 +169,9 @@ void insert_comment_line(parserstate *state, token tok) { comment *com = comment_get_comment(state->last_comment, prev_line); if (com) { - comment_insert_new_line(com, tok); + comment_insert_new_line(&state->allocator, com, tok); } else { - state->last_comment = alloc_comment(tok, state->last_comment); + state->last_comment = alloc_comment(&state->allocator, tok, state->last_comment); } } @@ -192,8 +187,8 @@ VALUE get_comment(parserstate *state, int subject_line) { } } -comment *alloc_comment(token comment_token, comment *last_comment) { - comment *new_comment = malloc(sizeof(comment)); +comment *alloc_comment(rbs_allocator_t *allocator, token comment_token, comment *last_comment) { + comment *new_comment = rbs_allocator_alloc(allocator, comment); *new_comment = (comment) { .start = comment_token.range.start, @@ -206,21 +201,12 @@ comment *alloc_comment(token comment_token, comment *last_comment) { .next_comment = last_comment, }; - comment_insert_new_line(new_comment, comment_token); + comment_insert_new_line(allocator, new_comment, comment_token); return new_comment; } -void free_comment(comment *com) { - if (com->next_comment) { - free_comment(com->next_comment); - } - - free(com->tokens); - free(com); -} - -void comment_insert_new_line(comment *com, token comment_token) { +void comment_insert_new_line(rbs_allocator_t *allocator, comment *com, token comment_token) { if (com->line_count == 0) { com->start = comment_token.range.start; } @@ -230,11 +216,10 @@ void comment_insert_new_line(comment *com, token comment_token) { if (com->tokens) { token *p = com->tokens; - com->tokens = calloc(com->line_size, sizeof(token)); + com->tokens = rbs_allocator_calloc(allocator, com->line_size, token); memcpy(com->tokens, p, sizeof(token) * com->line_count); - free(p); } else { - com->tokens = calloc(com->line_size, sizeof(token)); + com->tokens = rbs_allocator_calloc(allocator, com->line_size, token); } } @@ -288,12 +273,12 @@ VALUE comment_to_ruby(comment *com, VALUE buffer) { ); } -lexstate *alloc_lexer(VALUE string, int start_pos, int end_pos) { +lexstate *alloc_lexer(rbs_allocator_t *allocator, VALUE string, int start_pos, int end_pos) { if (start_pos < 0 || end_pos < 0) { rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos); } - lexstate *lexer = malloc(sizeof(lexstate)); + lexstate *lexer = rbs_allocator_alloc(allocator, lexstate); position start_position = (position) { .byte_pos = 0, @@ -320,8 +305,11 @@ lexstate *alloc_lexer(VALUE string, int start_pos, int end_pos) { } parserstate *alloc_parser(VALUE buffer, VALUE string, int start_pos, int end_pos, VALUE variables) { - lexstate *lexer = alloc_lexer(string, start_pos, end_pos); - parserstate *parser = malloc(sizeof(parserstate)); + rbs_allocator_t allocator; + rbs_allocator_init(&allocator); + + lexstate *lexer = alloc_lexer(&allocator, string, start_pos, end_pos); + parserstate *parser = rbs_allocator_alloc(&allocator, parserstate); *parser = (parserstate) { .lexstate = lexer, @@ -336,6 +324,7 @@ parserstate *alloc_parser(VALUE buffer, VALUE string, int start_pos, int end_pos .last_comment = NULL, .constant_pool = {}, + .allocator = allocator, }; // The parser's constant pool is mainly used for storing the names of type variables, which usually aren't many. @@ -389,10 +378,6 @@ parserstate *alloc_parser(VALUE buffer, VALUE string, int start_pos, int end_pos } void free_parser(parserstate *parser) { - free(parser->lexstate); - if (parser->last_comment) { - free_comment(parser->last_comment); - } rbs_constant_pool_free(&parser->constant_pool); - free(parser); + rbs_allocator_free(&parser->allocator); } diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index 9d315586b..ebfb0a498 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -3,6 +3,8 @@ #include +#include "rbs/util/rbs_allocator.h" +#include "rbs/util/rbs_constant_pool.h" #include "lexer.h" #include "location.h" @@ -57,11 +59,11 @@ typedef struct { comment *last_comment; /* Last read comment */ rbs_constant_pool_t constant_pool; + rbs_allocator_t allocator; } parserstate; -comment *alloc_comment(token comment_token, comment *last_comment); -void free_comment(comment *com); -void comment_insert_new_line(comment *com, token comment_token); +comment *alloc_comment(rbs_allocator_t *, token comment_token, comment *last_comment); +void comment_insert_new_line(rbs_allocator_t *, comment *com, token comment_token); comment *comment_get_comment(comment *com, int line); VALUE comment_to_ruby(comment *com, VALUE buffer); @@ -103,7 +105,7 @@ bool parser_typevar_member(parserstate *state, rbs_constant_id_t id); * alloc_lexer(string, 0, 31) // New lexstate with buffer content * ``` * */ -lexstate *alloc_lexer(VALUE string, int start_pos, int end_pos); +lexstate *alloc_lexer(rbs_allocator_t *, VALUE string, int start_pos, int end_pos); /** * Allocate new parserstate object. diff --git a/include/rbs.h b/include/rbs.h index da76e3535..0f105eae0 100644 --- a/include/rbs.h +++ b/include/rbs.h @@ -1,6 +1,7 @@ #ifndef RBS_H #define RBS_H +#include "rbs/ast.h" #include "rbs/constants.h" #include "rbs/ruby_objs.h" diff --git a/include/rbs/ast.h b/include/rbs/ast.h new file mode 100644 index 000000000..39060b570 --- /dev/null +++ b/include/rbs/ast.h @@ -0,0 +1,635 @@ +/*----------------------------------------------------------------------------*/ +/* This file is generated by the templates/template.rb script and should not */ +/* be modified manually. */ +/* To change the template see */ +/* templates/include/rbs/ast.h.erb */ +/*----------------------------------------------------------------------------*/ + +#ifndef RBS__AST_H +#define RBS__AST_H + +#include "ruby.h" +#include "rbs/util/rbs_allocator.h" + +enum rbs_node_type { + RBS_AST_ANNOTATION = 1, + RBS_AST_COMMENT = 2, + RBS_AST_DECLARATIONS_CLASS = 3, + RBS_AST_DECLARATIONS_CLASS_SUPER = 4, + RBS_AST_DECLARATIONS_CLASSALIAS = 5, + RBS_AST_DECLARATIONS_CONSTANT = 6, + RBS_AST_DECLARATIONS_GLOBAL = 7, + RBS_AST_DECLARATIONS_INTERFACE = 8, + RBS_AST_DECLARATIONS_MODULE = 9, + RBS_AST_DECLARATIONS_MODULE_SELF = 10, + RBS_AST_DECLARATIONS_MODULEALIAS = 11, + RBS_AST_DECLARATIONS_TYPEALIAS = 12, + RBS_AST_DIRECTIVES_USE = 13, + RBS_AST_DIRECTIVES_USE_SINGLECLAUSE = 14, + RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE = 15, + RBS_AST_MEMBERS_ALIAS = 16, + RBS_AST_MEMBERS_ATTRACCESSOR = 17, + RBS_AST_MEMBERS_ATTRREADER = 18, + RBS_AST_MEMBERS_ATTRWRITER = 19, + RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE = 20, + RBS_AST_MEMBERS_CLASSVARIABLE = 21, + RBS_AST_MEMBERS_EXTEND = 22, + RBS_AST_MEMBERS_INCLUDE = 23, + RBS_AST_MEMBERS_INSTANCEVARIABLE = 24, + RBS_AST_MEMBERS_METHODDEFINITION = 25, + RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD = 26, + RBS_AST_MEMBERS_PREPEND = 27, + RBS_AST_MEMBERS_PRIVATE = 28, + RBS_AST_MEMBERS_PUBLIC = 29, + RBS_AST_TYPEPARAM = 30, + RBS_METHODTYPE = 31, + RBS_NAMESPACE = 32, + RBS_TYPENAME = 33, + RBS_TYPES_ALIAS = 34, + RBS_TYPES_BASES_ANY = 35, + RBS_TYPES_BASES_BOOL = 36, + RBS_TYPES_BASES_BOTTOM = 37, + RBS_TYPES_BASES_CLASS = 38, + RBS_TYPES_BASES_INSTANCE = 39, + RBS_TYPES_BASES_NIL = 40, + RBS_TYPES_BASES_SELF = 41, + RBS_TYPES_BASES_TOP = 42, + RBS_TYPES_BASES_VOID = 43, + RBS_TYPES_BLOCK = 44, + RBS_TYPES_CLASSINSTANCE = 45, + RBS_TYPES_CLASSSINGLETON = 46, + RBS_TYPES_FUNCTION = 47, + RBS_TYPES_FUNCTION_PARAM = 48, + RBS_TYPES_INTERFACE = 49, + RBS_TYPES_INTERSECTION = 50, + RBS_TYPES_LITERAL = 51, + RBS_TYPES_OPTIONAL = 52, + RBS_TYPES_PROC = 53, + RBS_TYPES_RECORD = 54, + RBS_TYPES_TUPLE = 55, + RBS_TYPES_UNION = 56, + RBS_TYPES_UNTYPEDFUNCTION = 57, + RBS_TYPES_VARIABLE = 58, + RBS_TYPES_ZZZTMPNOTIMPLEMENTED = 59, +}; + +typedef struct rbs_node { + VALUE cached_ruby_value; + enum rbs_node_type type; +} rbs_node_t; + +typedef struct { + rbs_node_t base; + + VALUE string; + VALUE location; +} rbs_ast_annotation_t; + +typedef struct { + rbs_node_t base; + + VALUE string; + VALUE location; +} rbs_ast_comment_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE type_params; + VALUE super_class; + VALUE members; + VALUE annotations; + VALUE location; + VALUE comment; +} rbs_ast_declarations_class_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE args; + VALUE location; +} rbs_ast_declarations_class_super_t; + +typedef struct { + rbs_node_t base; + + VALUE new_name; + VALUE old_name; + VALUE location; + VALUE comment; +} rbs_ast_declarations_classalias_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE type; + VALUE location; + VALUE comment; +} rbs_ast_declarations_constant_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE type; + VALUE location; + VALUE comment; +} rbs_ast_declarations_global_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE type_params; + VALUE members; + VALUE annotations; + VALUE location; + VALUE comment; +} rbs_ast_declarations_interface_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE type_params; + VALUE self_types; + VALUE members; + VALUE annotations; + VALUE location; + VALUE comment; +} rbs_ast_declarations_module_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE args; + VALUE location; +} rbs_ast_declarations_module_self_t; + +typedef struct { + rbs_node_t base; + + VALUE new_name; + VALUE old_name; + VALUE location; + VALUE comment; +} rbs_ast_declarations_modulealias_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE type_params; + VALUE type; + VALUE annotations; + VALUE location; + VALUE comment; +} rbs_ast_declarations_typealias_t; + +typedef struct { + rbs_node_t base; + + VALUE clauses; + VALUE location; +} rbs_ast_directives_use_t; + +typedef struct { + rbs_node_t base; + + VALUE type_name; + VALUE new_name; + VALUE location; +} rbs_ast_directives_use_singleclause_t; + +typedef struct { + rbs_node_t base; + + VALUE namespace; + VALUE location; +} rbs_ast_directives_use_wildcardclause_t; + +typedef struct { + rbs_node_t base; + + VALUE new_name; + VALUE old_name; + VALUE kind; + VALUE annotations; + VALUE location; + VALUE comment; +} rbs_ast_members_alias_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE type; + VALUE ivar_name; + VALUE kind; + VALUE annotations; + VALUE location; + VALUE comment; + VALUE visibility; +} rbs_ast_members_attraccessor_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE type; + VALUE ivar_name; + VALUE kind; + VALUE annotations; + VALUE location; + VALUE comment; + VALUE visibility; +} rbs_ast_members_attrreader_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE type; + VALUE ivar_name; + VALUE kind; + VALUE annotations; + VALUE location; + VALUE comment; + VALUE visibility; +} rbs_ast_members_attrwriter_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE type; + VALUE location; + VALUE comment; +} rbs_ast_members_classinstancevariable_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE type; + VALUE location; + VALUE comment; +} rbs_ast_members_classvariable_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE args; + VALUE annotations; + VALUE location; + VALUE comment; +} rbs_ast_members_extend_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE args; + VALUE annotations; + VALUE location; + VALUE comment; +} rbs_ast_members_include_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE type; + VALUE location; + VALUE comment; +} rbs_ast_members_instancevariable_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE kind; + VALUE overloads; + VALUE annotations; + VALUE location; + VALUE comment; + VALUE overloading; + VALUE visibility; +} rbs_ast_members_methoddefinition_t; + +typedef struct { + rbs_node_t base; + + VALUE annotations; + VALUE method_type; +} rbs_ast_members_methoddefinition_overload_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE args; + VALUE annotations; + VALUE location; + VALUE comment; +} rbs_ast_members_prepend_t; + +typedef struct { + rbs_node_t base; + + VALUE location; +} rbs_ast_members_private_t; + +typedef struct { + rbs_node_t base; + + VALUE location; +} rbs_ast_members_public_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE variance; + VALUE upper_bound; + VALUE default_type; + VALUE unchecked; + VALUE location; +} rbs_ast_typeparam_t; + +typedef struct { + rbs_node_t base; + + VALUE type_params; + VALUE type; + VALUE block; + VALUE location; +} rbs_methodtype_t; + +typedef struct { + rbs_node_t base; + + VALUE path; + VALUE absolute; +} rbs_namespace_t; + +typedef struct { + rbs_node_t base; + + VALUE namespace; + VALUE name; +} rbs_typename_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE args; + VALUE location; +} rbs_types_alias_t; + +typedef struct { + rbs_node_t base; + + VALUE todo; + VALUE location; +} rbs_types_bases_any_t; + +typedef struct { + rbs_node_t base; + + VALUE location; +} rbs_types_bases_bool_t; + +typedef struct { + rbs_node_t base; + + VALUE location; +} rbs_types_bases_bottom_t; + +typedef struct { + rbs_node_t base; + + VALUE location; +} rbs_types_bases_class_t; + +typedef struct { + rbs_node_t base; + + VALUE location; +} rbs_types_bases_instance_t; + +typedef struct { + rbs_node_t base; + + VALUE location; +} rbs_types_bases_nil_t; + +typedef struct { + rbs_node_t base; + + VALUE location; +} rbs_types_bases_self_t; + +typedef struct { + rbs_node_t base; + + VALUE location; +} rbs_types_bases_top_t; + +typedef struct { + rbs_node_t base; + + VALUE location; +} rbs_types_bases_void_t; + +typedef struct { + rbs_node_t base; + + VALUE type; + VALUE required; + VALUE self_type; +} rbs_types_block_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE args; + VALUE location; +} rbs_types_classinstance_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE location; +} rbs_types_classsingleton_t; + +typedef struct { + rbs_node_t base; + + VALUE required_positionals; + VALUE optional_positionals; + VALUE rest_positionals; + VALUE trailing_positionals; + VALUE required_keywords; + VALUE optional_keywords; + VALUE rest_keywords; + VALUE return_type; +} rbs_types_function_t; + +typedef struct { + rbs_node_t base; + + VALUE type; + VALUE name; + VALUE location; +} rbs_types_function_param_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE args; + VALUE location; +} rbs_types_interface_t; + +typedef struct { + rbs_node_t base; + + VALUE types; + VALUE location; +} rbs_types_intersection_t; + +typedef struct { + rbs_node_t base; + + VALUE literal; + VALUE location; +} rbs_types_literal_t; + +typedef struct { + rbs_node_t base; + + VALUE type; + VALUE location; +} rbs_types_optional_t; + +typedef struct { + rbs_node_t base; + + VALUE type; + VALUE block; + VALUE location; + VALUE self_type; +} rbs_types_proc_t; + +typedef struct { + rbs_node_t base; + + VALUE all_fields; + VALUE location; +} rbs_types_record_t; + +typedef struct { + rbs_node_t base; + + VALUE types; + VALUE location; +} rbs_types_tuple_t; + +typedef struct { + rbs_node_t base; + + VALUE types; + VALUE location; +} rbs_types_union_t; + +typedef struct { + rbs_node_t base; + + VALUE return_type; +} rbs_types_untypedfunction_t; + +typedef struct { + rbs_node_t base; + + VALUE name; + VALUE location; +} rbs_types_variable_t; + +typedef struct { + rbs_node_t base; + +} rbs_types_zzztmpnotimplemented_t; + + +rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE string, VALUE location); +rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE string, VALUE location); +rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE super_class, VALUE members, VALUE annotations, VALUE location, VALUE comment); +rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location); +rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE new_name, VALUE old_name, VALUE location, VALUE comment); +rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment); +rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment); +rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE members, VALUE annotations, VALUE location, VALUE comment); +rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE self_types, VALUE members, VALUE annotations, VALUE location, VALUE comment); +rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location); +rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE new_name, VALUE old_name, VALUE location, VALUE comment); +rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE type, VALUE annotations, VALUE location, VALUE comment); +rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE clauses, VALUE location); +rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type_name, VALUE new_name, VALUE location); +rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE namespace, VALUE location); +rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE new_name, VALUE old_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment); +rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility); +rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility); +rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility); +rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment); +rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment); +rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment); +rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment); +rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment); +rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE kind, VALUE overloads, VALUE annotations, VALUE location, VALUE comment, VALUE overloading, VALUE visibility); +rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE annotations, VALUE method_type); +rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment); +rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); +rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); +rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE variance, VALUE upper_bound, VALUE default_type, VALUE unchecked, VALUE location); +rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type_params, VALUE type, VALUE block, VALUE location); +rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE path, VALUE absolute); +rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE namespace, VALUE name); +rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location); +rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE todo, VALUE location); +rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); +rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); +rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); +rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); +rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); +rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); +rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); +rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); +rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE required, VALUE self_type); +rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location); +rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE location); +rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE required_positionals, VALUE optional_positionals, VALUE rest_positionals, VALUE trailing_positionals, VALUE required_keywords, VALUE optional_keywords, VALUE rest_keywords, VALUE return_type); +rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE name, VALUE location); +rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location); +rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE types, VALUE location); +rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE literal, VALUE location); +rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE location); +rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE block, VALUE location, VALUE self_type); +rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE all_fields, VALUE location); +rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE types, VALUE location); +rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE types, VALUE location); +rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE return_type); +rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE location); +rbs_types_zzztmpnotimplemented_t *rbs_types_zzztmpnotimplemented_new(rbs_allocator_t *allocator, VALUE ruby_value); + +VALUE rbs_struct_to_ruby_value(rbs_node_t *instance); + +#endif diff --git a/include/rbs/util/rbs_allocator.h b/include/rbs/util/rbs_allocator.h new file mode 100644 index 000000000..e0b45eb33 --- /dev/null +++ b/include/rbs/util/rbs_allocator.h @@ -0,0 +1,21 @@ +#ifndef RBS_ALLOCATOR_H +#define RBS_ALLOCATOR_H + +#include + +typedef struct rbs_allocator { + // The head of a linked list of pages, starting with the most recently allocated page. + struct rbs_allocator_page *page; +} rbs_allocator_t; + +void rbs_allocator_init(rbs_allocator_t *); +void rbs_allocator_free(rbs_allocator_t *); +void *rbs_allocator_malloc_impl(rbs_allocator_t *, /* 1 */ size_t size, size_t alignment); +void *rbs_allocator_calloc_impl(rbs_allocator_t *, size_t count, size_t size, size_t alignment); + +#define rbs_allocator_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), alignof(type))) +#define rbs_allocator_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), alignof(type))) + +void rbs__init_arena_allocator(void); + +#endif diff --git a/src/ast.c b/src/ast.c new file mode 100644 index 000000000..9efc9f5b3 --- /dev/null +++ b/src/ast.c @@ -0,0 +1,1287 @@ +/*----------------------------------------------------------------------------*/ +/* This file is generated by the templates/template.rb script and should not */ +/* be modified manually. */ +/* To change the template see */ +/* templates/src/ast.c.erb */ +/*----------------------------------------------------------------------------*/ + +#include "rbs/ast.h" +#include + +rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE string, VALUE location) { + rbs_ast_annotation_t *instance = rbs_allocator_alloc(allocator, rbs_ast_annotation_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(string); + rb_gc_register_mark_object(location); + + *instance = (rbs_ast_annotation_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_ANNOTATION + }, + .string = string, + .location = location, + }; + + return instance; +} + +rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE string, VALUE location) { + rbs_ast_comment_t *instance = rbs_allocator_alloc(allocator, rbs_ast_comment_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(string); + rb_gc_register_mark_object(location); + + *instance = (rbs_ast_comment_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_COMMENT + }, + .string = string, + .location = location, + }; + + return instance; +} + +rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE super_class, VALUE members, VALUE annotations, VALUE location, VALUE comment) { + rbs_ast_declarations_class_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(type_params); + rb_gc_register_mark_object(super_class); + rb_gc_register_mark_object(members); + rb_gc_register_mark_object(annotations); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + + *instance = (rbs_ast_declarations_class_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_DECLARATIONS_CLASS + }, + .name = name, + .type_params = type_params, + .super_class = super_class, + .members = members, + .annotations = annotations, + .location = location, + .comment = comment, + }; + + return instance; +} + +rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location) { + rbs_ast_declarations_class_super_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_super_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(args); + rb_gc_register_mark_object(location); + + *instance = (rbs_ast_declarations_class_super_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_DECLARATIONS_CLASS_SUPER + }, + .name = name, + .args = args, + .location = location, + }; + + return instance; +} + +rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE new_name, VALUE old_name, VALUE location, VALUE comment) { + rbs_ast_declarations_classalias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_classalias_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(new_name); + rb_gc_register_mark_object(old_name); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + + *instance = (rbs_ast_declarations_classalias_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_DECLARATIONS_CLASSALIAS + }, + .new_name = new_name, + .old_name = old_name, + .location = location, + .comment = comment, + }; + + return instance; +} + +rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment) { + rbs_ast_declarations_constant_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_constant_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(type); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + + *instance = (rbs_ast_declarations_constant_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_DECLARATIONS_CONSTANT + }, + .name = name, + .type = type, + .location = location, + .comment = comment, + }; + + return instance; +} + +rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment) { + rbs_ast_declarations_global_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_global_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(type); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + + *instance = (rbs_ast_declarations_global_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_DECLARATIONS_GLOBAL + }, + .name = name, + .type = type, + .location = location, + .comment = comment, + }; + + return instance; +} + +rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE members, VALUE annotations, VALUE location, VALUE comment) { + rbs_ast_declarations_interface_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_interface_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(type_params); + rb_gc_register_mark_object(members); + rb_gc_register_mark_object(annotations); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + + *instance = (rbs_ast_declarations_interface_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_DECLARATIONS_INTERFACE + }, + .name = name, + .type_params = type_params, + .members = members, + .annotations = annotations, + .location = location, + .comment = comment, + }; + + return instance; +} + +rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE self_types, VALUE members, VALUE annotations, VALUE location, VALUE comment) { + rbs_ast_declarations_module_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(type_params); + rb_gc_register_mark_object(self_types); + rb_gc_register_mark_object(members); + rb_gc_register_mark_object(annotations); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + + *instance = (rbs_ast_declarations_module_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_DECLARATIONS_MODULE + }, + .name = name, + .type_params = type_params, + .self_types = self_types, + .members = members, + .annotations = annotations, + .location = location, + .comment = comment, + }; + + return instance; +} + +rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location) { + rbs_ast_declarations_module_self_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_self_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(args); + rb_gc_register_mark_object(location); + + *instance = (rbs_ast_declarations_module_self_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_DECLARATIONS_MODULE_SELF + }, + .name = name, + .args = args, + .location = location, + }; + + return instance; +} + +rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE new_name, VALUE old_name, VALUE location, VALUE comment) { + rbs_ast_declarations_modulealias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_modulealias_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(new_name); + rb_gc_register_mark_object(old_name); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + + *instance = (rbs_ast_declarations_modulealias_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_DECLARATIONS_MODULEALIAS + }, + .new_name = new_name, + .old_name = old_name, + .location = location, + .comment = comment, + }; + + return instance; +} + +rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE type, VALUE annotations, VALUE location, VALUE comment) { + rbs_ast_declarations_typealias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_typealias_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(type_params); + rb_gc_register_mark_object(type); + rb_gc_register_mark_object(annotations); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + + *instance = (rbs_ast_declarations_typealias_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_DECLARATIONS_TYPEALIAS + }, + .name = name, + .type_params = type_params, + .type = type, + .annotations = annotations, + .location = location, + .comment = comment, + }; + + return instance; +} + +rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE clauses, VALUE location) { + rbs_ast_directives_use_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(clauses); + rb_gc_register_mark_object(location); + + *instance = (rbs_ast_directives_use_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_DIRECTIVES_USE + }, + .clauses = clauses, + .location = location, + }; + + return instance; +} + +rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type_name, VALUE new_name, VALUE location) { + rbs_ast_directives_use_singleclause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_singleclause_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(type_name); + rb_gc_register_mark_object(new_name); + rb_gc_register_mark_object(location); + + *instance = (rbs_ast_directives_use_singleclause_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_DIRECTIVES_USE_SINGLECLAUSE + }, + .type_name = type_name, + .new_name = new_name, + .location = location, + }; + + return instance; +} + +rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE namespace, VALUE location) { + rbs_ast_directives_use_wildcardclause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_wildcardclause_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(namespace); + rb_gc_register_mark_object(location); + + *instance = (rbs_ast_directives_use_wildcardclause_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE + }, + .namespace = namespace, + .location = location, + }; + + return instance; +} + +rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE new_name, VALUE old_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment) { + rbs_ast_members_alias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_alias_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(new_name); + rb_gc_register_mark_object(old_name); + rb_gc_register_mark_object(kind); + rb_gc_register_mark_object(annotations); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + + *instance = (rbs_ast_members_alias_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_MEMBERS_ALIAS + }, + .new_name = new_name, + .old_name = old_name, + .kind = kind, + .annotations = annotations, + .location = location, + .comment = comment, + }; + + return instance; +} + +rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility) { + rbs_ast_members_attraccessor_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attraccessor_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(type); + rb_gc_register_mark_object(ivar_name); + rb_gc_register_mark_object(kind); + rb_gc_register_mark_object(annotations); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + rb_gc_register_mark_object(visibility); + + *instance = (rbs_ast_members_attraccessor_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_MEMBERS_ATTRACCESSOR + }, + .name = name, + .type = type, + .ivar_name = ivar_name, + .kind = kind, + .annotations = annotations, + .location = location, + .comment = comment, + .visibility = visibility, + }; + + return instance; +} + +rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility) { + rbs_ast_members_attrreader_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attrreader_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(type); + rb_gc_register_mark_object(ivar_name); + rb_gc_register_mark_object(kind); + rb_gc_register_mark_object(annotations); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + rb_gc_register_mark_object(visibility); + + *instance = (rbs_ast_members_attrreader_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_MEMBERS_ATTRREADER + }, + .name = name, + .type = type, + .ivar_name = ivar_name, + .kind = kind, + .annotations = annotations, + .location = location, + .comment = comment, + .visibility = visibility, + }; + + return instance; +} + +rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility) { + rbs_ast_members_attrwriter_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attrwriter_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(type); + rb_gc_register_mark_object(ivar_name); + rb_gc_register_mark_object(kind); + rb_gc_register_mark_object(annotations); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + rb_gc_register_mark_object(visibility); + + *instance = (rbs_ast_members_attrwriter_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_MEMBERS_ATTRWRITER + }, + .name = name, + .type = type, + .ivar_name = ivar_name, + .kind = kind, + .annotations = annotations, + .location = location, + .comment = comment, + .visibility = visibility, + }; + + return instance; +} + +rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment) { + rbs_ast_members_classinstancevariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_classinstancevariable_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(type); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + + *instance = (rbs_ast_members_classinstancevariable_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE + }, + .name = name, + .type = type, + .location = location, + .comment = comment, + }; + + return instance; +} + +rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment) { + rbs_ast_members_classvariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_classvariable_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(type); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + + *instance = (rbs_ast_members_classvariable_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_MEMBERS_CLASSVARIABLE + }, + .name = name, + .type = type, + .location = location, + .comment = comment, + }; + + return instance; +} + +rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment) { + rbs_ast_members_extend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_extend_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(args); + rb_gc_register_mark_object(annotations); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + + *instance = (rbs_ast_members_extend_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_MEMBERS_EXTEND + }, + .name = name, + .args = args, + .annotations = annotations, + .location = location, + .comment = comment, + }; + + return instance; +} + +rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment) { + rbs_ast_members_include_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_include_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(args); + rb_gc_register_mark_object(annotations); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + + *instance = (rbs_ast_members_include_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_MEMBERS_INCLUDE + }, + .name = name, + .args = args, + .annotations = annotations, + .location = location, + .comment = comment, + }; + + return instance; +} + +rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment) { + rbs_ast_members_instancevariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_instancevariable_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(type); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + + *instance = (rbs_ast_members_instancevariable_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_MEMBERS_INSTANCEVARIABLE + }, + .name = name, + .type = type, + .location = location, + .comment = comment, + }; + + return instance; +} + +rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE kind, VALUE overloads, VALUE annotations, VALUE location, VALUE comment, VALUE overloading, VALUE visibility) { + rbs_ast_members_methoddefinition_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_methoddefinition_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(kind); + rb_gc_register_mark_object(overloads); + rb_gc_register_mark_object(annotations); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + rb_gc_register_mark_object(overloading); + rb_gc_register_mark_object(visibility); + + *instance = (rbs_ast_members_methoddefinition_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_MEMBERS_METHODDEFINITION + }, + .name = name, + .kind = kind, + .overloads = overloads, + .annotations = annotations, + .location = location, + .comment = comment, + .overloading = overloading, + .visibility = visibility, + }; + + return instance; +} + +rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE annotations, VALUE method_type) { + rbs_ast_members_methoddefinition_overload_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_methoddefinition_overload_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(annotations); + rb_gc_register_mark_object(method_type); + + *instance = (rbs_ast_members_methoddefinition_overload_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD + }, + .annotations = annotations, + .method_type = method_type, + }; + + return instance; +} + +rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment) { + rbs_ast_members_prepend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_prepend_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(args); + rb_gc_register_mark_object(annotations); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(comment); + + *instance = (rbs_ast_members_prepend_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_MEMBERS_PREPEND + }, + .name = name, + .args = args, + .annotations = annotations, + .location = location, + .comment = comment, + }; + + return instance; +} + +rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { + rbs_ast_members_private_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_private_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(location); + + *instance = (rbs_ast_members_private_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_MEMBERS_PRIVATE + }, + .location = location, + }; + + return instance; +} + +rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { + rbs_ast_members_public_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_public_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(location); + + *instance = (rbs_ast_members_public_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_MEMBERS_PUBLIC + }, + .location = location, + }; + + return instance; +} + +rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE variance, VALUE upper_bound, VALUE default_type, VALUE unchecked, VALUE location) { + rbs_ast_typeparam_t *instance = rbs_allocator_alloc(allocator, rbs_ast_typeparam_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(variance); + rb_gc_register_mark_object(upper_bound); + rb_gc_register_mark_object(default_type); + rb_gc_register_mark_object(unchecked); + rb_gc_register_mark_object(location); + + *instance = (rbs_ast_typeparam_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_TYPEPARAM + }, + .name = name, + .variance = variance, + .upper_bound = upper_bound, + .default_type = default_type, + .unchecked = unchecked, + .location = location, + }; + + return instance; +} + +rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type_params, VALUE type, VALUE block, VALUE location) { + rbs_methodtype_t *instance = rbs_allocator_alloc(allocator, rbs_methodtype_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(type_params); + rb_gc_register_mark_object(type); + rb_gc_register_mark_object(block); + rb_gc_register_mark_object(location); + + *instance = (rbs_methodtype_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_METHODTYPE + }, + .type_params = type_params, + .type = type, + .block = block, + .location = location, + }; + + return instance; +} + +rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE path, VALUE absolute) { + rbs_namespace_t *instance = rbs_allocator_alloc(allocator, rbs_namespace_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(path); + rb_gc_register_mark_object(absolute); + + *instance = (rbs_namespace_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_NAMESPACE + }, + .path = path, + .absolute = absolute, + }; + + return instance; +} + +rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE namespace, VALUE name) { + rbs_typename_t *instance = rbs_allocator_alloc(allocator, rbs_typename_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(namespace); + rb_gc_register_mark_object(name); + + *instance = (rbs_typename_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPENAME + }, + .namespace = namespace, + .name = name, + }; + + return instance; +} + +rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location) { + rbs_types_alias_t *instance = rbs_allocator_alloc(allocator, rbs_types_alias_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(args); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_alias_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_ALIAS + }, + .name = name, + .args = args, + .location = location, + }; + + return instance; +} + +rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE todo, VALUE location) { + rbs_types_bases_any_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_any_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(todo); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_bases_any_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_BASES_ANY + }, + .todo = todo, + .location = location, + }; + + return instance; +} + +rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { + rbs_types_bases_bool_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_bool_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_bases_bool_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_BASES_BOOL + }, + .location = location, + }; + + return instance; +} + +rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { + rbs_types_bases_bottom_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_bottom_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_bases_bottom_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_BASES_BOTTOM + }, + .location = location, + }; + + return instance; +} + +rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { + rbs_types_bases_class_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_class_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_bases_class_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_BASES_CLASS + }, + .location = location, + }; + + return instance; +} + +rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { + rbs_types_bases_instance_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_instance_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_bases_instance_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_BASES_INSTANCE + }, + .location = location, + }; + + return instance; +} + +rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { + rbs_types_bases_nil_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_nil_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_bases_nil_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_BASES_NIL + }, + .location = location, + }; + + return instance; +} + +rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { + rbs_types_bases_self_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_self_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_bases_self_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_BASES_SELF + }, + .location = location, + }; + + return instance; +} + +rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { + rbs_types_bases_top_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_top_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_bases_top_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_BASES_TOP + }, + .location = location, + }; + + return instance; +} + +rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { + rbs_types_bases_void_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_void_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_bases_void_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_BASES_VOID + }, + .location = location, + }; + + return instance; +} + +rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE required, VALUE self_type) { + rbs_types_block_t *instance = rbs_allocator_alloc(allocator, rbs_types_block_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(type); + rb_gc_register_mark_object(required); + rb_gc_register_mark_object(self_type); + + *instance = (rbs_types_block_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_BLOCK + }, + .type = type, + .required = required, + .self_type = self_type, + }; + + return instance; +} + +rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location) { + rbs_types_classinstance_t *instance = rbs_allocator_alloc(allocator, rbs_types_classinstance_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(args); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_classinstance_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_CLASSINSTANCE + }, + .name = name, + .args = args, + .location = location, + }; + + return instance; +} + +rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE location) { + rbs_types_classsingleton_t *instance = rbs_allocator_alloc(allocator, rbs_types_classsingleton_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_classsingleton_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_CLASSSINGLETON + }, + .name = name, + .location = location, + }; + + return instance; +} + +rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE required_positionals, VALUE optional_positionals, VALUE rest_positionals, VALUE trailing_positionals, VALUE required_keywords, VALUE optional_keywords, VALUE rest_keywords, VALUE return_type) { + rbs_types_function_t *instance = rbs_allocator_alloc(allocator, rbs_types_function_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(required_positionals); + rb_gc_register_mark_object(optional_positionals); + rb_gc_register_mark_object(rest_positionals); + rb_gc_register_mark_object(trailing_positionals); + rb_gc_register_mark_object(required_keywords); + rb_gc_register_mark_object(optional_keywords); + rb_gc_register_mark_object(rest_keywords); + rb_gc_register_mark_object(return_type); + + *instance = (rbs_types_function_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_FUNCTION + }, + .required_positionals = required_positionals, + .optional_positionals = optional_positionals, + .rest_positionals = rest_positionals, + .trailing_positionals = trailing_positionals, + .required_keywords = required_keywords, + .optional_keywords = optional_keywords, + .rest_keywords = rest_keywords, + .return_type = return_type, + }; + + return instance; +} + +rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE name, VALUE location) { + rbs_types_function_param_t *instance = rbs_allocator_alloc(allocator, rbs_types_function_param_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(type); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_function_param_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_FUNCTION_PARAM + }, + .type = type, + .name = name, + .location = location, + }; + + return instance; +} + +rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location) { + rbs_types_interface_t *instance = rbs_allocator_alloc(allocator, rbs_types_interface_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(args); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_interface_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_INTERFACE + }, + .name = name, + .args = args, + .location = location, + }; + + return instance; +} + +rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE types, VALUE location) { + rbs_types_intersection_t *instance = rbs_allocator_alloc(allocator, rbs_types_intersection_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(types); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_intersection_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_INTERSECTION + }, + .types = types, + .location = location, + }; + + return instance; +} + +rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE literal, VALUE location) { + rbs_types_literal_t *instance = rbs_allocator_alloc(allocator, rbs_types_literal_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(literal); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_literal_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_LITERAL + }, + .literal = literal, + .location = location, + }; + + return instance; +} + +rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE location) { + rbs_types_optional_t *instance = rbs_allocator_alloc(allocator, rbs_types_optional_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(type); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_optional_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_OPTIONAL + }, + .type = type, + .location = location, + }; + + return instance; +} + +rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE block, VALUE location, VALUE self_type) { + rbs_types_proc_t *instance = rbs_allocator_alloc(allocator, rbs_types_proc_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(type); + rb_gc_register_mark_object(block); + rb_gc_register_mark_object(location); + rb_gc_register_mark_object(self_type); + + *instance = (rbs_types_proc_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_PROC + }, + .type = type, + .block = block, + .location = location, + .self_type = self_type, + }; + + return instance; +} + +rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE all_fields, VALUE location) { + rbs_types_record_t *instance = rbs_allocator_alloc(allocator, rbs_types_record_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(all_fields); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_record_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_RECORD + }, + .all_fields = all_fields, + .location = location, + }; + + return instance; +} + +rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE types, VALUE location) { + rbs_types_tuple_t *instance = rbs_allocator_alloc(allocator, rbs_types_tuple_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(types); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_tuple_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_TUPLE + }, + .types = types, + .location = location, + }; + + return instance; +} + +rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE types, VALUE location) { + rbs_types_union_t *instance = rbs_allocator_alloc(allocator, rbs_types_union_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(types); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_union_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_UNION + }, + .types = types, + .location = location, + }; + + return instance; +} + +rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE return_type) { + rbs_types_untypedfunction_t *instance = rbs_allocator_alloc(allocator, rbs_types_untypedfunction_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(return_type); + + *instance = (rbs_types_untypedfunction_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_UNTYPEDFUNCTION + }, + .return_type = return_type, + }; + + return instance; +} + +rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE location) { + rbs_types_variable_t *instance = rbs_allocator_alloc(allocator, rbs_types_variable_t); + + rb_gc_register_mark_object(ruby_value); + rb_gc_register_mark_object(name); + rb_gc_register_mark_object(location); + + *instance = (rbs_types_variable_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_VARIABLE + }, + .name = name, + .location = location, + }; + + return instance; +} + +rbs_types_zzztmpnotimplemented_t *rbs_types_zzztmpnotimplemented_new(rbs_allocator_t *allocator, VALUE ruby_value) { + rbs_types_zzztmpnotimplemented_t *instance = rbs_allocator_alloc(allocator, rbs_types_zzztmpnotimplemented_t); + + rb_gc_register_mark_object(ruby_value); + + *instance = (rbs_types_zzztmpnotimplemented_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_TYPES_ZZZTMPNOTIMPLEMENTED + }, + }; + + return instance; +} + diff --git a/src/util/rbs_allocator.c b/src/util/rbs_allocator.c new file mode 100644 index 000000000..122f89f3a --- /dev/null +++ b/src/util/rbs_allocator.c @@ -0,0 +1,184 @@ +/** + * @file rbs_allocator.c + * + * A simple arena allocator that can be freed all at once. + * + * This allocator maintains a linked list of pages, which come in two flavours: + * 1. Small allocation pages, which are the same size as the system page size. + * 2. Large allocation pages, which are the exact size requested, for sizes greater than the small page size. + * + * Small allocations always fit into the unused space at the end of the "head" page. If there isn't enough room, a new + * page is allocated, and the small allocation is placed at its start. This approach wastes that unused slack at the + * end of the previous page, but it means that allocations are instant and never scan the linked list to find a gap. + * + * This allocator doesn't support freeing individual allocations. Only the whole arena can be freed at once at the end. + */ + +#include "rbs/util/rbs_allocator.h" + +#include +#include +#include // for memset() + +#ifdef _WIN32 + #include +#else + #include + #include +#endif + +typedef struct rbs_allocator_page { + uint32_t payload_size; + + // The offset of the next available byte. + uint32_t offset; + + // The previously allocated page, or NULL if this is the first page. + struct rbs_allocator_page *next; + + // The variably-sized payload of the page. + char payload[]; +} rbs_allocator_page_t; + +// This allocator's normal pages have the same size as the system memory pages, consisting of a fixed-size header +// (sizeof(rbs_allocator_page_t)) followed `default_page_payload_size` bytes of payload. +// TODO: When we have real-world usage data, we can tune this to use a smaller number of larger pages. +static size_t default_page_payload_size = 0; +static uint32_t large_page_flag = UINT32_MAX; + +static size_t get_system_page_size() { +#ifdef _WIN32 + SYSTEM_INFO si; + GetSystemInfo(&si); + return si.dwPageSize; +#else + long sz = sysconf(_SC_PAGESIZE); + if (sz == -1) return 4096; // Fallback to the common 4KB page size + return (size_t) sz; +#endif +} + +void rbs__init_arena_allocator(void) { + const size_t system_page_size = get_system_page_size(); + + // The size of a struct that ends with a flexible array member is the size of the struct without the + // flexible array member. https://en.wikipedia.org/wiki/Flexible_array_member#Effect_on_struct_size_and_padding + const size_t page_header_size = sizeof(rbs_allocator_page_t); + default_page_payload_size = system_page_size - page_header_size; +} + +// Returns the number of bytes needed to pad the given pointer up to the nearest multiple of the given `alignment`. +static size_t needed_padding(void *ptr, size_t alignment) { + const uintptr_t addr = (uintptr_t) ptr; + const uintptr_t aligned_addr = (addr + (alignment - 1)) & -alignment; + return (aligned_addr - addr); +} + +static rbs_allocator_page_t *rbs_allocator_page_new(size_t payload_size) { + const size_t page_header_size = sizeof(rbs_allocator_page_t); + + rbs_allocator_page_t *page = calloc(1, page_header_size + payload_size); + *page = (rbs_allocator_page_t) { + .payload_size = (uint32_t) payload_size, + .offset = page_header_size, + .next = NULL, + }; + return page; +} + +static rbs_allocator_page_t *rbs_allocator_page_new_default(void) { + return rbs_allocator_page_new(default_page_payload_size); +} + +static rbs_allocator_page_t *rbs_allocator_page_new_large(size_t payload_size) { + rbs_allocator_page_t *page = rbs_allocator_page_new(payload_size); + + page->offset = large_page_flag; + + return page; +} + +// Attempts to allocate `size` bytes from `page`, returning NULL if there is insufficient space. +static void *rbs_allocator_page_attempt_alloc(rbs_allocator_page_t *page, size_t size, size_t alignment) { + const size_t alignment_padding = needed_padding(page->payload + page->offset, alignment); + + const size_t remaining_size = page->payload_size - page->offset; + const size_t needed_size = alignment_padding + size; + if (remaining_size < needed_size) return NULL; // Not enough space in this page. + + void *ptr = page->payload + page->offset + alignment_padding; + page->offset += needed_size; + return ptr; +} + +void rbs_allocator_init(rbs_allocator_t *allocator) { + *allocator = (rbs_allocator_t) { + .page = rbs_allocator_page_new_default(), + }; +} + +void rbs_allocator_free(rbs_allocator_t *allocator) { + rbs_allocator_page_t *page = allocator->page; + while (page) { + rbs_allocator_page_t *next = page->next; + free(page); + page = next; + } + + *allocator = (rbs_allocator_t) { + .page = NULL, + }; +} + +// Allocates `size` bytes from `allocator`, aligned to an `alignment`-byte boundary. +void *rbs_allocator_malloc_impl(rbs_allocator_t *allocator, size_t size, size_t alignment) { + assert(size % alignment == 0 && "size must be a multiple of the alignment"); + + if (default_page_payload_size < size) { // Big allocation, give it its own page. + // How much we need to pad the new page's payload in order to get an aligned pointer + // hack? + const size_t alignment_padding = needed_padding((void *) (sizeof(rbs_allocator_page_t) + size), alignment); + + rbs_allocator_page_t *new_page = rbs_allocator_page_new_large(alignment_padding + size); + + // This simple allocator can only put small allocations into the head page. + // Naively prepending this large allocation page to the head of the allocator before the previous head page + // would waste the remaining space in the head page. + // So instead, we'll splice in the large page *after* the head page. + // + // +-------+ +-----------+ +-----------+ + // | arena | | head page | | new_page | + // |-------| |-----------+ |-----------+ + // | *page |--->| size | +--->| size | +---> ... previous tail + // +-------+ | offset | | | offset | | + // | *next ----+---+ | *next ----+---+ + // | ... | | ... | + // +-----------+ +-----------+ + // + new_page->next = allocator->page->next; + allocator->page->next = new_page; + + return new_page->payload + alignment_padding; + } + + void *p = rbs_allocator_page_attempt_alloc(allocator->page, size, alignment); + if (p != NULL) return p; + + // Not enough space. Allocate a new page and prepend it to the allocator's linked list. + rbs_allocator_page_t *new_page = rbs_allocator_page_new_default(); + new_page->next = allocator->page; + allocator->page = new_page; + + p = rbs_allocator_page_attempt_alloc(new_page, size, alignment); + assert(p != NULL && "Failed to allocate a new allocator page"); + return p; +} + +// Note: This will eagerly fill with zeroes, unlike `calloc()` which can map a page in a page to be zeroed lazily. +// It's assumed that callers to this function will immediately write to the allocated memory, anyway. +void *rbs_allocator_calloc_impl(rbs_allocator_t *allocator, size_t count, size_t size, size_t alignment) { + void *p = rbs_allocator_malloc_impl(allocator, count * size, alignment); + memset(p, 0, count * size); + return p; +} + diff --git a/sync_from_prism.rb b/sync_from_prism.rb new file mode 100644 index 000000000..a4b23ef9e --- /dev/null +++ b/sync_from_prism.rb @@ -0,0 +1,37 @@ +require "Pathname" + +# This script assumes you have this layout: +# ├── Ruby +# │ └── prism +# └── Shopify +# └── rbs <---- current working directory + +import_from_prism("../../ruby/prism/include/prism/defines.h") +import_from_prism("../../ruby/prism/include/prism/util/pm_constant_pool.h") +import_from_prism("../../ruby/prism/src/util/pm_constant_pool.c") + +BEGIN { + def import_from_prism(prism_file_path_str) + prism_file_path = Pathname.new(prism_file_path_str).realpath + puts prism_file_path + + dest_file_path_str = "./" + prism_file_path_str.delete_prefix("../../ruby/prism/") + dest_file_path_str.gsub!("include/prism/", "include/rbs/") + dest_file_path_str.gsub!("pm_", "rbs_") + + dest_file_path = Pathname.new(dest_file_path_str).expand_path + puts dest_file_path.dirname.mkpath + + puts "Importing \"#{prism_file_path.relative_path_from(Pathname.pwd)}\" to \"#{dest_file_path.relative_path_from(Pathname.pwd)}\"" + + contents = File.read(prism_file_path) + + contents.gsub!("PRISM", "RBS") + contents.gsub!("Prism", "RBS") + contents.gsub!("prism", "rbs") + contents.gsub!("pm_", "rbs_") + contents.gsub!("PM_", "RBS_") + + File.write(dest_file_path, contents) + end +} diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb new file mode 100644 index 000000000..ccab436d7 --- /dev/null +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -0,0 +1,44 @@ +#include "ast_translation.h" + + +const char* get_class_name(VALUE o) { + VALUE klass = rb_class_of(o); // Get the class of the object + VALUE klass_name = rb_class_name(klass); // Get the name of the class + const char* name = StringValueCStr(klass_name); // Convert to C string + return name; +} + +VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { + if (instance == NULL) { + fprintf(stderr, "Tried to call rbs_struct_to_ruby_value(NULL)\n"); + exit(1); + } + + if (instance->type == RBS_TYPES_ZZZTMPNOTIMPLEMENTED) { + // Special case: skip assertions/translation below. + return instance->cached_ruby_value; + } + + VALUE ruby_value = instance->cached_ruby_value; + + if (ruby_value == Qnil || ruby_value == Qundef) { + fprintf(stderr, "cached_ruby_value is NULL\n"); + exit(1); + } + + const char *class_name = get_class_name(ruby_value); + + switch (instance->type) { + <%- nodes.each do |node| -%> + case <%= node.c_type_enum_name %>: { + if (strcmp(class_name, "<%= node.ruby_full_name %>") != 0) { + fprintf(stderr, "Expected class name: <%= node.ruby_full_name %>, got %s\n", class_name); + exit(1); + } + break; + } + <%- end -%> + } + + return instance->cached_ruby_value; +} diff --git a/templates/ext/rbs_extension/ast_translation.h.erb b/templates/ext/rbs_extension/ast_translation.h.erb new file mode 100644 index 000000000..e759d9396 --- /dev/null +++ b/templates/ext/rbs_extension/ast_translation.h.erb @@ -0,0 +1,9 @@ +#ifndef RBS_EXTENSION_AST_TRANSLATION_H +#define RBS_EXTENSION_AST_TRANSLATION_H + +#include "ruby.h" +#include "rbs/ast.h" + +VALUE rbs_struct_to_ruby_value(rbs_node_t *instance); + +#endif diff --git a/templates/include/rbs/ast.h.erb b/templates/include/rbs/ast.h.erb new file mode 100644 index 000000000..6cf7e054e --- /dev/null +++ b/templates/include/rbs/ast.h.erb @@ -0,0 +1,35 @@ +#ifndef RBS__AST_H +#define RBS__AST_H + +#include "ruby.h" +#include "rbs/util/rbs_allocator.h" + +enum rbs_node_type { +<%- nodes.each_with_index do |node, index| -%> + <%= node.c_type_enum_name %> = <%= index + 1 %>, +<%- end -%> +}; + +typedef struct rbs_node { + VALUE cached_ruby_value; + enum rbs_node_type type; +} rbs_node_t; + +<%- nodes.each do |node| -%> +typedef struct { + rbs_node_t base; + + <%- node.fields.each do |field| -%> + VALUE <%= field.name %>; + <%- end -%> +} <%= node.c_type_name %>; + +<%- end -%> + +<%- nodes.each do |node| -%> +<%= node.c_type_name %> *<%= node.c_constructor_function_name %>(<%= node.constructor_params.map(&:parameter_decl).join(", ") %>); +<%- end -%> + +VALUE rbs_struct_to_ruby_value(rbs_node_t *instance); + +#endif diff --git a/templates/include/rbs/constants.h.erb b/templates/include/rbs/constants.h.erb index 7be88cdfa..bc8f44f87 100644 --- a/templates/include/rbs/constants.h.erb +++ b/templates/include/rbs/constants.h.erb @@ -11,7 +11,7 @@ extern VALUE RBS_Types; extern VALUE RBS_Types_Bases; extern VALUE RBS_ParsingError; -<%- nodes.each do |node| -%> +<%- nodes.filter(&:expose_to_ruby?).each do |node| -%> extern VALUE <%= node.c_constant_name %>; <%- end -%> diff --git a/templates/include/rbs/ruby_objs.h.erb b/templates/include/rbs/ruby_objs.h.erb index b5376a301..88fd6fa55 100644 --- a/templates/include/rbs/ruby_objs.h.erb +++ b/templates/include/rbs/ruby_objs.h.erb @@ -3,7 +3,7 @@ #include "ruby.h" -<%- nodes.each do |node| -%> +<%- nodes.filter(&:expose_to_ruby?).each do |node| -%> VALUE <%= node.c_function_name %>(<%= node.fields.map { |field| "#{field.c_type} #{field.name}" }.join(", ") %>); <%- end -%> diff --git a/templates/src/ast.c.erb b/templates/src/ast.c.erb new file mode 100644 index 000000000..365243c66 --- /dev/null +++ b/templates/src/ast.c.erb @@ -0,0 +1,26 @@ +#include "rbs/ast.h" +#include + +<%- nodes.each do |node| -%> +<%= node.c_type_name %> *<%= node.c_constructor_function_name %>(<%= node.constructor_params.map(&:parameter_decl).join(", ") %>) { + <%= node.c_type_name %> *instance = rbs_allocator_alloc(allocator, <%= node.c_type_name %>); + + rb_gc_register_mark_object(ruby_value); + <%- node.fields.each do |field| -%> + rb_gc_register_mark_object(<%= field.name %>); + <%- end -%> + + *instance = (<%= node.c_type_name %>) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = <%= node.c_type_enum_name %> + }, + <%- node.fields.each do |field| -%> + .<%= field.name %> = <%= field.name %>, + <%- end -%> + }; + + return instance; +} + +<%- end -%> diff --git a/templates/src/constants.c.erb b/templates/src/constants.c.erb index 1a6ff9400..e4f9e9b1c 100644 --- a/templates/src/constants.c.erb +++ b/templates/src/constants.c.erb @@ -11,7 +11,7 @@ VALUE RBS_Parser; VALUE RBS_Types; VALUE RBS_Types_Bases; -<%- nodes.each do |node| -%> +<%- nodes.filter(&:expose_to_ruby?).each do |node| -%> VALUE <%= node.c_constant_name %>; <%- end -%> @@ -30,7 +30,7 @@ void rbs__init_constants(void) { IMPORT_CONSTANT(RBS_Types, RBS, "Types"); IMPORT_CONSTANT(RBS_Types_Bases, RBS_Types, "Bases"); - <%- nodes.each do |node| -%> + <%- nodes.filter(&:expose_to_ruby?).each do |node| -%> IMPORT_CONSTANT(<%= node.c_constant_name %>, <%= node.c_parent_constant_name %>, "<%= node.ruby_class_name %>"); <%- end -%> } diff --git a/templates/src/ruby_objs.c.erb b/templates/src/ruby_objs.c.erb index 24cbe4ffb..dbbb716b2 100644 --- a/templates/src/ruby_objs.c.erb +++ b/templates/src/ruby_objs.c.erb @@ -10,7 +10,7 @@ rb_class_new_instance(argc, argv, receiver) #endif -<%- nodes.each do |node| -%> +<%- nodes.filter(&:expose_to_ruby?).each do |node| -%> VALUE <%= node.c_function_name %>(<%= node.fields.map { |field| "#{field.c_type} #{field.name}" }.join(", ") %>) { VALUE _init_kwargs = rb_hash_new(); <%- node.fields.each do |field| -%> diff --git a/templates/template.rb b/templates/template.rb index 270b0f799..937c0e29b 100644 --- a/templates/template.rb +++ b/templates/template.rb @@ -7,12 +7,55 @@ module RBS class Template class Field - attr_reader :name - attr_reader :c_type + attr_reader :name, :c_type, :c_name #: String - def initialize(yaml) - @name = yaml["name"] - @c_type = "VALUE" + def initialize(name:, c_type:, c_name: nil) + @name = name + @c_type = c_type + @c_name = c_name || name + end + + def self.from_hash(hash) + new(name: hash["name"], c_type: hash.fetch("c_type", "VALUE"), c_name: hash["c_name"]) + end + + def parameter_decl + case @c_type + when "VALUE", "bool" + "#{@c_type} #{c_name}" + when "rbs_string" + "rbs_string_t #{c_name}" + when ->(c_type) { c_type.include?("_t") } + "#{c_type}#{c_name}" + else + "#{@c_type}_t *#{c_name}" + end + end + + def stored_field_decl + case @c_type + when "VALUE" + "VALUE #{c_name}" + when "bool" + "bool #{c_name}" + when "rbs_string" + "rbs_string_t #{c_name}" + else + "struct #{@c_type} *#{c_name}" + end + end + + def ast_node? + @c_type == "rbs_node" || + @c_type == "rbs_typename" || + @c_type == "rbs_namespace" || + @c_type.include?("_ast_") || + @c_type.include?("_decl_") || + @c_type.include?("_types_") + end + + def needs_to_be_freed? + !["VALUE", "bool"].include?(@c_type) end end @@ -41,6 +84,10 @@ class Type # e.g. `RBS_AST_Declarations` attr_reader :c_parent_constant_name #: String + attr_reader :c_struct_name #: String + attr_reader :c_type_enum_name #: String + + attr_reader :constructor_params #: Array[RBS::Template::Field] attr_reader :fields #: Array[RBS::Template::Field] def initialize(yaml) @@ -52,8 +99,34 @@ def initialize(yaml) @c_function_name.gsub!(/^rbs_ast_declarations_/, 'rbs_ast_decl_') @c_constant_name = @ruby_full_name.gsub("::", "_") @c_parent_constant_name = @ruby_full_name.split("::")[0..-2].join("::").gsub("::", "_") + @c_base_name = @c_constant_name.downcase + @c_type_name = @c_base_name + "_t" + + @c_struct_name = "#{@c_base_name}_t" + @c_type_enum_name = @c_base_name.upcase + + @expose_to_ruby = yaml.fetch("expose_to_ruby", true) + + @fields = yaml.fetch("fields", []).map { |field| Field.from_hash(field) }.freeze + + @constructor_params = [ + Field.new(name: "allocator", c_type: "rbs_allocator_t *"), + Field.new(name: "ruby_value", c_type: "VALUE"), + ] + @constructor_params.concat @fields + @constructor_params.freeze + end + + # The name of the C function which constructs new instances of this C structure. + # e.g. `rbs_ast_declarations_typealias_new` + def c_constructor_function_name #: String + "#{@c_base_name}_new" + end - @fields = yaml.fetch("fields", []).map { |field| Field.new(field) }.freeze + # Every templated type will have a C struct created for it. + # If this is true, then we will also create a Ruby class for it, otherwise we'll skip that. + def expose_to_ruby? + @expose_to_ruby end end From a51d08ca3f93029934d540e0d3ceee2c398ab073 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Thu, 28 Nov 2024 11:10:47 -0500 Subject: [PATCH 003/111] Enable unused-result warnings Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/extconf.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/rbs_extension/extconf.rb b/ext/rbs_extension/extconf.rb index 9ea9b0006..092f25b41 100644 --- a/ext/rbs_extension/extconf.rb +++ b/ext/rbs_extension/extconf.rb @@ -11,5 +11,5 @@ $srcs = Dir.glob("#{root_dir}/src/**/*.c") + Dir.glob("#{root_dir}/ext/rbs_extension/*.c") -append_cflags ['-std=gnu99'] +append_cflags ['-std=gnu99', '-Wunused-result'] create_makefile 'rbs_extension' From c2da2597b5521ab2211fc0b617199edefc187abd Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Wed, 30 Oct 2024 16:16:37 -0400 Subject: [PATCH 004/111] Move away from using VALUE and other Ruby data structs Signed-off-by: Alexandre Terrasa Add linked list implementation Signed-off-by: Alexandre Terrasa Type `Class#super_class` field Signed-off-by: Alexandre Terrasa Type fields of `RBS::Types::Block` Signed-off-by: Alexandre Terrasa Type `block` fields Signed-off-by: Alexandre Terrasa Type `RBS::Types::Proc#self_type` field Signed-off-by: Alexandre Terrasa Refactor `parse_function` Signed-off-by: Alexandre Terrasa Copy value in `rbs_struct_to_ruby_value` Remove usages of `rbs_loc` from `parser.c` Extract `rbs_location.h` Migrate `RBS::Types::Function::Param` fields Signed-off-by: Alexandre Terrasa Type `RBS::Types::UntypedFunction` fields Signed-off-by: Alexandre Terrasa Type fields of `RBS::AST::TypeParam` Signed-off-by: Alexandre Terrasa Type some more fields of `RBS::AST::Members::Attr` Signed-off-by: Alexandre Terrasa Type fields in `RBS::AST::Members::MethodDefinition` Signed-off-by: Alexandre Terrasa Type `RBS::AST::Directives::Use::SingleClause#new_name` Signed-off-by: Alexandre Terrasa Type `RBS::Namespace#absolute` Signed-off-by: Alexandre Terrasa Temporary handle nil types Signed-off-by: Alexandre Terrasa Handle `bool` type Signed-off-by: Alexandre Terrasa Type all fields of `RBS::Types::Variable` Signed-off-by: Alexandre Terrasa Migrate `RBS::TypeName` Signed-off-by: Alexandre Terrasa Migrate `parse_use_clauses` Signed-off-by: Alexandre Terrasa Migrate `class_instance_name` Signed-off-by: Alexandre Terrasa Handle overloads as a rbs_node_list Signed-off-by: Alexandre Terrasa Remove more `builds_ruby_object_internally` flags Signed-off-by: Alexandre Terrasa Invert `builds_ruby_object_internally` default value Signed-off-by: Alexandre Terrasa Introduce `rbs_location_t` Signed-off-by: Alexandre Terrasa Store C structs instead of Ruby `VALUE`s Introduce +rbs_ast_symbol_t and migrate to it Signed-off-by: Alexandre Terrasa Remove ZzzTmpNotImplemented node Signed-off-by: Alexandre Terrasa Remove one more instance of EMPTY_ARRAY Signed-off-by: Alexandre Terrasa Migrate from VALUE array to rbs_node_list_t Signed-off-by: Alexandre Terrasa Migrate `method_params` from taking a VALUE arrays Signed-off-by: Alexandre Terrasa Migrate `parse_type_list` from taking a VALUE array Signed-off-by: Alexandre Terrasa Forward all C-typed params as-is Get types on constructor params Handle mix of C types and Ruby VALUE Move Ruby object construction into `new` functions Conditionally construct `ruby_value` internally Type Attr* field `ivar_name` Signed-off-by: Alexandre Terrasa Add `AST::Bool` Signed-off-by: Alexandre Terrasa Use two less VALUE values Signed-off-by: Alexandre Terrasa Use more instance of `bool` Signed-off-by: Alexandre Terrasa Add Hash implementation Signed-off-by: Alexandre Terrasa --- config.yml | 187 ++- ext/rbs_extension/ast_translation.c | 907 ++++++++++++-- ext/rbs_extension/ast_translation.h | 3 + ext/rbs_extension/location.c | 35 +- ext/rbs_extension/location.h | 19 +- ext/rbs_extension/parser.c | 1059 ++++++++--------- ext/rbs_extension/parser.h | 6 +- include/rbs/ast.h | 762 ++++++------ include/rbs/constants.h | 1 + include/rbs/rbs_location.h | 28 + include/rbs/ruby_objs.h | 115 +- src/ast.c | 991 +++++++++++---- src/constants.c | 2 + src/rbs_location.c | 27 + src/ruby_objs.c | 452 +++---- .../ext/rbs_extension/ast_translation.c.erb | 83 +- .../ext/rbs_extension/ast_translation.h.erb | 3 + templates/include/rbs/ast.h.erb | 52 +- templates/include/rbs/ruby_objs.h.erb | 2 +- templates/src/ast.c.erb | 129 +- templates/src/ruby_objs.c.erb | 4 +- templates/template.rb | 46 +- test/rbs/type_parsing_test.rb | 7 +- 23 files changed, 3328 insertions(+), 1592 deletions(-) create mode 100644 include/rbs/rbs_location.h create mode 100644 src/rbs_location.c diff --git a/config.yml b/config.yml index 963348c60..af4357477 100644 --- a/config.yml +++ b/config.yml @@ -3,257 +3,418 @@ nodes: fields: - name: string - name: location + c_type: rbs_location + - name: RBS::AST::Bool + expose_to_ruby: false + fields: + - name: value + c_type: bool - name: RBS::AST::Comment + builds_ruby_object_internally: false fields: - name: string - name: location + c_type: rbs_location - name: RBS::AST::Declarations::Class fields: - name: name + c_type: rbs_typename - name: type_params + c_type: rbs_node_list - name: super_class + c_type: rbs_ast_declarations_class_super - name: members + c_type: rbs_node_list - name: annotations + c_type: rbs_node_list - name: location + c_type: rbs_location - name: comment - name: RBS::AST::Declarations::Class::Super fields: - name: name + c_type: rbs_typename - name: args + c_type: rbs_node_list - name: location + c_type: rbs_location - name: RBS::AST::Declarations::ClassAlias fields: - name: new_name + c_type: rbs_typename - name: old_name + c_type: rbs_typename - name: location + c_type: rbs_location - name: comment - name: RBS::AST::Declarations::Constant fields: - name: name + c_type: rbs_typename - name: type + c_type: rbs_node - name: location + c_type: rbs_location - name: comment - name: RBS::AST::Declarations::Global fields: - name: name + c_type: rbs_ast_symbol - name: type + c_type: rbs_node - name: location + c_type: rbs_location - name: comment - name: RBS::AST::Declarations::Interface fields: - name: name + c_type: rbs_typename - name: type_params + c_type: rbs_node_list - name: members + c_type: rbs_node_list - name: annotations + c_type: rbs_node_list - name: location + c_type: rbs_location - name: comment - name: RBS::AST::Declarations::Module fields: - name: name + c_type: rbs_typename - name: type_params + c_type: rbs_node_list - name: self_types + c_type: rbs_node_list - name: members + c_type: rbs_node_list - name: annotations + c_type: rbs_node_list - name: location + c_type: rbs_location - name: comment - name: RBS::AST::Declarations::Module::Self fields: - name: name + c_type: rbs_typename - name: args + c_type: rbs_node_list - name: location + c_type: rbs_location - name: RBS::AST::Declarations::ModuleAlias fields: - name: new_name + c_type: rbs_typename - name: old_name + c_type: rbs_typename - name: location + c_type: rbs_location - name: comment + - name: RBS::AST::Declarations::Nodes + builds_ruby_object_internally: false + expose_to_ruby: false + fields: + - name: declarations + c_type: rbs_node_list - name: RBS::AST::Declarations::TypeAlias fields: - name: name + c_type: rbs_typename - name: type_params + c_type: rbs_node_list - name: type + c_type: rbs_node - name: annotations + c_type: rbs_node_list - name: location + c_type: rbs_location - name: comment + - name: RBS::AST::Directives::Nodes + builds_ruby_object_internally: false + expose_to_ruby: false + fields: + - name: directives + c_type: rbs_node_list - name: RBS::AST::Directives::Use fields: - name: clauses + c_type: rbs_node_list - name: location + c_type: rbs_location - name: RBS::AST::Directives::Use::SingleClause fields: - name: type_name + c_type: rbs_typename - name: new_name + c_type: rbs_ast_symbol - name: location + c_type: rbs_location - name: RBS::AST::Directives::Use::WildcardClause fields: - name: namespace + c_type: rbs_namespace - name: location + c_type: rbs_location - name: RBS::AST::Members::Alias fields: - name: new_name + c_type: rbs_ast_symbol - name: old_name + c_type: rbs_ast_symbol - name: kind + c_type: rbs_ast_symbol - name: annotations + c_type: rbs_node_list - name: location + c_type: rbs_location - name: comment - name: RBS::AST::Members::AttrAccessor fields: - name: name + c_type: rbs_ast_symbol - name: type + c_type: rbs_node - name: ivar_name + c_type: rbs_node # rbs_ast_symbol_t, NULL or rbs_ast_bool_new(false) - name: kind + c_type: rbs_ast_symbol - name: annotations + c_type: rbs_node_list - name: location + c_type: rbs_location - name: comment - name: visibility + c_type: rbs_ast_symbol - name: RBS::AST::Members::AttrReader fields: - name: name + c_type: rbs_ast_symbol - name: type + c_type: rbs_node - name: ivar_name + c_type: rbs_node # rbs_ast_symbol_t, NULL or rbs_ast_bool_new(false) - name: kind + c_type: rbs_ast_symbol - name: annotations + c_type: rbs_node_list - name: location + c_type: rbs_location - name: comment - name: visibility + c_type: rbs_ast_symbol - name: RBS::AST::Members::AttrWriter fields: - name: name + c_type: rbs_ast_symbol - name: type + c_type: rbs_node - name: ivar_name + c_type: rbs_node # rbs_ast_symbol_t, NULL or rbs_ast_bool_new(false) - name: kind + c_type: rbs_ast_symbol - name: annotations + c_type: rbs_node_list - name: location + c_type: rbs_location - name: comment - name: visibility + c_type: rbs_ast_symbol - name: RBS::AST::Members::ClassInstanceVariable fields: - name: name + c_type: rbs_ast_symbol - name: type + c_type: rbs_node - name: location + c_type: rbs_location - name: comment - name: RBS::AST::Members::ClassVariable fields: - name: name + c_type: rbs_ast_symbol - name: type + c_type: rbs_node - name: location + c_type: rbs_location - name: comment - name: RBS::AST::Members::Extend fields: - name: name + c_type: rbs_typename - name: args + c_type: rbs_node_list - name: annotations + c_type: rbs_node_list - name: location + c_type: rbs_location - name: comment - name: RBS::AST::Members::Include fields: - name: name + c_type: rbs_typename - name: args + c_type: rbs_node_list - name: annotations + c_type: rbs_node_list - name: location + c_type: rbs_location - name: comment - name: RBS::AST::Members::InstanceVariable fields: - name: name + c_type: rbs_ast_symbol - name: type + c_type: rbs_node - name: location + c_type: rbs_location - name: comment - name: RBS::AST::Members::MethodDefinition fields: - name: name + c_type: rbs_ast_symbol - name: kind + c_type: rbs_ast_symbol - name: overloads + c_type: rbs_node_list - name: annotations + c_type: rbs_node_list - name: location + c_type: rbs_location - name: comment - name: overloading + c_type: bool - name: visibility + c_type: rbs_ast_symbol - name: RBS::AST::Members::MethodDefinition::Overload fields: - name: annotations + c_type: rbs_node_list - name: method_type + c_type: rbs_node - name: RBS::AST::Members::Prepend fields: - name: name + c_type: rbs_typename - name: args + c_type: rbs_node_list - name: annotations + c_type: rbs_node_list - name: location + c_type: rbs_location - name: comment - name: RBS::AST::Members::Private fields: - name: location + c_type: rbs_location - name: RBS::AST::Members::Public fields: - name: location + c_type: rbs_location + - name: RBS::AST::Symbol + builds_ruby_object_internally: false - name: RBS::AST::TypeParam fields: - name: name + c_type: rbs_ast_symbol - name: variance + c_type: rbs_ast_symbol - name: upper_bound + c_type: rbs_node - name: default_type + c_type: rbs_node - name: unchecked + c_type: bool - name: location + c_type: rbs_location - name: RBS::MethodType fields: - name: type_params + c_type: rbs_node_list - name: type + c_type: rbs_node - name: block + c_type: rbs_types_block - name: location + c_type: rbs_location - name: RBS::Namespace fields: - name: path + c_type: rbs_node_list - name: absolute + c_type: bool - name: RBS::TypeName fields: - name: namespace + c_type: rbs_namespace - name: name + c_type: rbs_ast_symbol - name: RBS::Types::Alias fields: - name: name + c_type: rbs_typename - name: args + c_type: rbs_node_list - name: location + c_type: rbs_location - name: RBS::Types::Bases::Any fields: - name: todo - name: location + c_type: rbs_location - name: RBS::Types::Bases::Bool fields: - name: location + c_type: rbs_location - name: RBS::Types::Bases::Bottom fields: - name: location + c_type: rbs_location - name: RBS::Types::Bases::Class fields: - name: location + c_type: rbs_location - name: RBS::Types::Bases::Instance fields: - name: location + c_type: rbs_location - name: RBS::Types::Bases::Nil fields: - name: location + c_type: rbs_location - name: RBS::Types::Bases::Self fields: - name: location + c_type: rbs_location - name: RBS::Types::Bases::Top fields: - name: location + c_type: rbs_location - name: RBS::Types::Bases::Void fields: - name: location + c_type: rbs_location - name: RBS::Types::Block fields: - name: type + c_type: rbs_node - name: required + c_type: bool - name: self_type + c_type: rbs_node - name: RBS::Types::ClassInstance fields: - name: name + c_type: rbs_typename - name: args + c_type: rbs_node_list - name: location + c_type: rbs_location - name: RBS::Types::ClassSingleton fields: - name: name + c_type: rbs_typename - name: location + c_type: rbs_location - name: RBS::Types::Function fields: - name: required_positionals @@ -267,50 +428,70 @@ nodes: - name: RBS::Types::Function::Param fields: - name: type + c_type: rbs_node - name: name + c_type: rbs_ast_symbol - name: location + c_type: rbs_location - name: RBS::Types::Interface fields: - name: name + c_type: rbs_typename - name: args + c_type: rbs_node_list - name: location + c_type: rbs_location - name: RBS::Types::Intersection fields: - name: types + c_type: rbs_node_list - name: location + c_type: rbs_location - name: RBS::Types::Literal fields: - name: literal - name: location + c_type: rbs_location - name: RBS::Types::Optional fields: - name: type + c_type: rbs_node - name: location + c_type: rbs_location - name: RBS::Types::Proc fields: - name: type + c_type: rbs_node - name: block + c_type: rbs_types_block - name: location + c_type: rbs_location - name: self_type + c_type: rbs_node - name: RBS::Types::Record fields: - name: all_fields - name: location + c_type: rbs_location - name: RBS::Types::Tuple fields: - name: types + c_type: rbs_node_list - name: location + c_type: rbs_location - name: RBS::Types::Union fields: - name: types + c_type: rbs_node_list - name: location + c_type: rbs_location - name: RBS::Types::UntypedFunction fields: - name: return_type + c_type: rbs_node - name: RBS::Types::Variable fields: - name: name + c_type: rbs_ast_symbol - name: location - - name: RBS::Types::ZzzTmpNotImplemented - expose_to_ruby: false - fields: [] + c_type: rbs_location \ No newline at end of file diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index a965ed0d8..ad4d6dbb8 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -7,6 +7,32 @@ #include "ast_translation.h" +#include "rbs/constants.h" + +#include + + +VALUE rbs_node_list_to_ruby_array(rbs_node_list_t *list) { + return list->cached_ruby_value; +} + +VALUE rbs_hash_to_ruby_hash(rbs_hash_t *hash) { + return hash->cached_ruby_value; +} + +VALUE rbs_loc_to_ruby_location(rbs_location_t *loc) { + return loc->cached_ruby_value; +} + +#ifdef RB_PASS_KEYWORDS + // Ruby 2.7 or later + #define CLASS_NEW_INSTANCE(klass, argc, argv)\ + rb_class_new_instance_kw(argc, argv, klass, RB_PASS_KEYWORDS) +#else + // Ruby 2.6 + #define CLASS_NEW_INSTANCE(receiver, argc, argv)\ + rb_class_new_instance(argc, argv, receiver) +#endif const char* get_class_name(VALUE o) { VALUE klass = rb_class_of(o); // Get the class of the object @@ -16,15 +42,7 @@ const char* get_class_name(VALUE o) { } VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { - if (instance == NULL) { - fprintf(stderr, "Tried to call rbs_struct_to_ruby_value(NULL)\n"); - exit(1); - } - - if (instance->type == RBS_TYPES_ZZZTMPNOTIMPLEMENTED) { - // Special case: skip assertions/translation below. - return instance->cached_ruby_value; - } + if (instance == NULL) return Qnil; VALUE ruby_value = instance->cached_ruby_value; @@ -41,415 +59,1152 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { fprintf(stderr, "Expected class name: RBS::AST::Annotation, got %s\n", class_name); exit(1); } - break; + + rbs_ast_annotation_t *node = (rbs_ast_annotation_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("string")), node->string); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Annotation, + 1, + &h + ); + } + case RBS_AST_BOOL: { + if (strcmp(class_name, "RBS::AST::Bool") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Bool, got %s\n", class_name); + exit(1); + } + return instance->cached_ruby_value; } case RBS_AST_COMMENT: { if (strcmp(class_name, "RBS::AST::Comment") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Comment, got %s\n", class_name); exit(1); } - break; + + rbs_ast_comment_t *node = (rbs_ast_comment_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("string")), node->string); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Comment, + 1, + &h + ); } case RBS_AST_DECLARATIONS_CLASS: { if (strcmp(class_name, "RBS::AST::Declarations::Class") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Declarations::Class, got %s\n", class_name); exit(1); } - break; + + rbs_ast_declarations_class_t *node = (rbs_ast_declarations_class_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(node->type_params)); + rb_hash_aset(h, ID2SYM(rb_intern("super_class")), rbs_struct_to_ruby_value((rbs_node_t *) node->super_class)); // rbs_ast_declarations_class_super + rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(node->members)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_Class, + 1, + &h + ); } case RBS_AST_DECLARATIONS_CLASS_SUPER: { if (strcmp(class_name, "RBS::AST::Declarations::Class::Super") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Declarations::Class::Super, got %s\n", class_name); exit(1); } - break; + + rbs_ast_declarations_class_super_t *node = (rbs_ast_declarations_class_super_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_Class_Super, + 1, + &h + ); } case RBS_AST_DECLARATIONS_CLASSALIAS: { if (strcmp(class_name, "RBS::AST::Declarations::ClassAlias") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Declarations::ClassAlias, got %s\n", class_name); exit(1); } - break; + + rbs_ast_declarations_classalias_t *node = (rbs_ast_declarations_classalias_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->new_name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->old_name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_ClassAlias, + 1, + &h + ); } case RBS_AST_DECLARATIONS_CONSTANT: { if (strcmp(class_name, "RBS::AST::Declarations::Constant") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Declarations::Constant, got %s\n", class_name); exit(1); } - break; + + rbs_ast_declarations_constant_t *node = (rbs_ast_declarations_constant_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_Constant, + 1, + &h + ); } case RBS_AST_DECLARATIONS_GLOBAL: { if (strcmp(class_name, "RBS::AST::Declarations::Global") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Declarations::Global, got %s\n", class_name); exit(1); } - break; + + rbs_ast_declarations_global_t *node = (rbs_ast_declarations_global_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_Global, + 1, + &h + ); } case RBS_AST_DECLARATIONS_INTERFACE: { if (strcmp(class_name, "RBS::AST::Declarations::Interface") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Declarations::Interface, got %s\n", class_name); exit(1); } - break; + + rbs_ast_declarations_interface_t *node = (rbs_ast_declarations_interface_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(node->type_params)); + rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(node->members)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_Interface, + 1, + &h + ); } case RBS_AST_DECLARATIONS_MODULE: { if (strcmp(class_name, "RBS::AST::Declarations::Module") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Declarations::Module, got %s\n", class_name); exit(1); } - break; + + rbs_ast_declarations_module_t *node = (rbs_ast_declarations_module_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(node->type_params)); + rb_hash_aset(h, ID2SYM(rb_intern("self_types")), rbs_node_list_to_ruby_array(node->self_types)); + rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(node->members)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_Module, + 1, + &h + ); } case RBS_AST_DECLARATIONS_MODULE_SELF: { if (strcmp(class_name, "RBS::AST::Declarations::Module::Self") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Declarations::Module::Self, got %s\n", class_name); exit(1); } - break; + + rbs_ast_declarations_module_self_t *node = (rbs_ast_declarations_module_self_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_Module_Self, + 1, + &h + ); } case RBS_AST_DECLARATIONS_MODULEALIAS: { if (strcmp(class_name, "RBS::AST::Declarations::ModuleAlias") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Declarations::ModuleAlias, got %s\n", class_name); exit(1); } - break; + + rbs_ast_declarations_modulealias_t *node = (rbs_ast_declarations_modulealias_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->new_name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->old_name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_ModuleAlias, + 1, + &h + ); + } + case RBS_AST_DECLARATIONS_NODES: { + if (strcmp(class_name, "RBS::AST::Declarations::Nodes") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Declarations::Nodes, got %s\n", class_name); + exit(1); + } + return instance->cached_ruby_value; } case RBS_AST_DECLARATIONS_TYPEALIAS: { if (strcmp(class_name, "RBS::AST::Declarations::TypeAlias") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Declarations::TypeAlias, got %s\n", class_name); exit(1); } - break; + + rbs_ast_declarations_typealias_t *node = (rbs_ast_declarations_typealias_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(node->type_params)); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + + return CLASS_NEW_INSTANCE( + RBS_AST_Declarations_TypeAlias, + 1, + &h + ); + } + case RBS_AST_DIRECTIVES_NODES: { + if (strcmp(class_name, "RBS::AST::Directives::Nodes") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Directives::Nodes, got %s\n", class_name); + exit(1); + } + return instance->cached_ruby_value; } case RBS_AST_DIRECTIVES_USE: { if (strcmp(class_name, "RBS::AST::Directives::Use") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Directives::Use, got %s\n", class_name); exit(1); } - break; + + rbs_ast_directives_use_t *node = (rbs_ast_directives_use_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("clauses")), rbs_node_list_to_ruby_array(node->clauses)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Directives_Use, + 1, + &h + ); } case RBS_AST_DIRECTIVES_USE_SINGLECLAUSE: { if (strcmp(class_name, "RBS::AST::Directives::Use::SingleClause") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Directives::Use::SingleClause, got %s\n", class_name); exit(1); } - break; + + rbs_ast_directives_use_singleclause_t *node = (rbs_ast_directives_use_singleclause_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("type_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->type_name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->new_name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Directives_Use_SingleClause, + 1, + &h + ); } case RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE: { if (strcmp(class_name, "RBS::AST::Directives::Use::WildcardClause") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Directives::Use::WildcardClause, got %s\n", class_name); exit(1); } - break; + + rbs_ast_directives_use_wildcardclause_t *node = (rbs_ast_directives_use_wildcardclause_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value((rbs_node_t *) node->namespace)); // rbs_namespace + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Directives_Use_WildcardClause, + 1, + &h + ); } case RBS_AST_MEMBERS_ALIAS: { if (strcmp(class_name, "RBS::AST::Members::Alias") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Members::Alias, got %s\n", class_name); exit(1); } - break; + + rbs_ast_members_alias_t *node = (rbs_ast_members_alias_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->new_name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->old_name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value((rbs_node_t *) node->kind)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_Alias, + 1, + &h + ); } case RBS_AST_MEMBERS_ATTRACCESSOR: { if (strcmp(class_name, "RBS::AST::Members::AttrAccessor") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Members::AttrAccessor, got %s\n", class_name); exit(1); } - break; + + rbs_ast_members_attraccessor_t *node = (rbs_ast_members_attraccessor_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->ivar_name)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value((rbs_node_t *) node->kind)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value((rbs_node_t *) node->visibility)); // rbs_ast_symbol + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_AttrAccessor, + 1, + &h + ); } case RBS_AST_MEMBERS_ATTRREADER: { if (strcmp(class_name, "RBS::AST::Members::AttrReader") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Members::AttrReader, got %s\n", class_name); exit(1); } - break; + + rbs_ast_members_attrreader_t *node = (rbs_ast_members_attrreader_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->ivar_name)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value((rbs_node_t *) node->kind)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value((rbs_node_t *) node->visibility)); // rbs_ast_symbol + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_AttrReader, + 1, + &h + ); } case RBS_AST_MEMBERS_ATTRWRITER: { if (strcmp(class_name, "RBS::AST::Members::AttrWriter") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Members::AttrWriter, got %s\n", class_name); exit(1); } - break; + + rbs_ast_members_attrwriter_t *node = (rbs_ast_members_attrwriter_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->ivar_name)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value((rbs_node_t *) node->kind)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value((rbs_node_t *) node->visibility)); // rbs_ast_symbol + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_AttrWriter, + 1, + &h + ); } case RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE: { if (strcmp(class_name, "RBS::AST::Members::ClassInstanceVariable") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Members::ClassInstanceVariable, got %s\n", class_name); exit(1); } - break; + + rbs_ast_members_classinstancevariable_t *node = (rbs_ast_members_classinstancevariable_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_ClassInstanceVariable, + 1, + &h + ); } case RBS_AST_MEMBERS_CLASSVARIABLE: { if (strcmp(class_name, "RBS::AST::Members::ClassVariable") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Members::ClassVariable, got %s\n", class_name); exit(1); } - break; + + rbs_ast_members_classvariable_t *node = (rbs_ast_members_classvariable_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_ClassVariable, + 1, + &h + ); } case RBS_AST_MEMBERS_EXTEND: { if (strcmp(class_name, "RBS::AST::Members::Extend") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Members::Extend, got %s\n", class_name); exit(1); } - break; + + rbs_ast_members_extend_t *node = (rbs_ast_members_extend_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_Extend, + 1, + &h + ); } case RBS_AST_MEMBERS_INCLUDE: { if (strcmp(class_name, "RBS::AST::Members::Include") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Members::Include, got %s\n", class_name); exit(1); } - break; + + rbs_ast_members_include_t *node = (rbs_ast_members_include_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_Include, + 1, + &h + ); } case RBS_AST_MEMBERS_INSTANCEVARIABLE: { if (strcmp(class_name, "RBS::AST::Members::InstanceVariable") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Members::InstanceVariable, got %s\n", class_name); exit(1); } - break; + + rbs_ast_members_instancevariable_t *node = (rbs_ast_members_instancevariable_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_InstanceVariable, + 1, + &h + ); } case RBS_AST_MEMBERS_METHODDEFINITION: { if (strcmp(class_name, "RBS::AST::Members::MethodDefinition") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Members::MethodDefinition, got %s\n", class_name); exit(1); } - break; + + rbs_ast_members_methoddefinition_t *node = (rbs_ast_members_methoddefinition_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value((rbs_node_t *) node->kind)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("overloads")), rbs_node_list_to_ruby_array(node->overloads)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("overloading")), node->overloading ? Qtrue : Qfalse); + rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value((rbs_node_t *) node->visibility)); // rbs_ast_symbol + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_MethodDefinition, + 1, + &h + ); } case RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD: { if (strcmp(class_name, "RBS::AST::Members::MethodDefinition::Overload") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Members::MethodDefinition::Overload, got %s\n", class_name); exit(1); } - break; + + rbs_ast_members_methoddefinition_overload_t *node = (rbs_ast_members_methoddefinition_overload_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("method_type")), rbs_struct_to_ruby_value((rbs_node_t *) node->method_type)); // rbs_node + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_MethodDefinition_Overload, + 1, + &h + ); } case RBS_AST_MEMBERS_PREPEND: { if (strcmp(class_name, "RBS::AST::Members::Prepend") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Members::Prepend, got %s\n", class_name); exit(1); } - break; + + rbs_ast_members_prepend_t *node = (rbs_ast_members_prepend_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_Prepend, + 1, + &h + ); } case RBS_AST_MEMBERS_PRIVATE: { if (strcmp(class_name, "RBS::AST::Members::Private") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Members::Private, got %s\n", class_name); exit(1); } - break; + + rbs_ast_members_private_t *node = (rbs_ast_members_private_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_Private, + 1, + &h + ); } case RBS_AST_MEMBERS_PUBLIC: { if (strcmp(class_name, "RBS::AST::Members::Public") != 0) { fprintf(stderr, "Expected class name: RBS::AST::Members::Public, got %s\n", class_name); exit(1); } - break; + + rbs_ast_members_public_t *node = (rbs_ast_members_public_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_Members_Public, + 1, + &h + ); + } + case RBS_AST_SYMBOL: { + if (strcmp(class_name, "RBS::AST::Symbol") != 0) { + fprintf(stderr, "Expected class name: RBS::AST::Symbol, got %s\n", class_name); + exit(1); + } + return instance->cached_ruby_value; } case RBS_AST_TYPEPARAM: { if (strcmp(class_name, "RBS::AST::TypeParam") != 0) { fprintf(stderr, "Expected class name: RBS::AST::TypeParam, got %s\n", class_name); exit(1); } - break; + + rbs_ast_typeparam_t *node = (rbs_ast_typeparam_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("variance")), rbs_struct_to_ruby_value((rbs_node_t *) node->variance)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("upper_bound")), rbs_struct_to_ruby_value((rbs_node_t *) node->upper_bound)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("default_type")), rbs_struct_to_ruby_value((rbs_node_t *) node->default_type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("unchecked")), node->unchecked ? Qtrue : Qfalse); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_AST_TypeParam, + 1, + &h + ); } case RBS_METHODTYPE: { if (strcmp(class_name, "RBS::MethodType") != 0) { fprintf(stderr, "Expected class name: RBS::MethodType, got %s\n", class_name); exit(1); } - break; + + rbs_methodtype_t *node = (rbs_methodtype_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(node->type_params)); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("block")), rbs_struct_to_ruby_value((rbs_node_t *) node->block)); // rbs_types_block + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_MethodType, + 1, + &h + ); } case RBS_NAMESPACE: { if (strcmp(class_name, "RBS::Namespace") != 0) { fprintf(stderr, "Expected class name: RBS::Namespace, got %s\n", class_name); exit(1); } - break; + + rbs_namespace_t *node = (rbs_namespace_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("path")), rbs_node_list_to_ruby_array(node->path)); + rb_hash_aset(h, ID2SYM(rb_intern("absolute")), node->absolute ? Qtrue : Qfalse); + + return CLASS_NEW_INSTANCE( + RBS_Namespace, + 1, + &h + ); } case RBS_TYPENAME: { if (strcmp(class_name, "RBS::TypeName") != 0) { fprintf(stderr, "Expected class name: RBS::TypeName, got %s\n", class_name); exit(1); } - break; + + rbs_typename_t *node = (rbs_typename_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value((rbs_node_t *) node->namespace)); // rbs_namespace + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol + + return CLASS_NEW_INSTANCE( + RBS_TypeName, + 1, + &h + ); } case RBS_TYPES_ALIAS: { if (strcmp(class_name, "RBS::Types::Alias") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Alias, got %s\n", class_name); exit(1); } - break; + + rbs_types_alias_t *node = (rbs_types_alias_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Alias, + 1, + &h + ); } case RBS_TYPES_BASES_ANY: { if (strcmp(class_name, "RBS::Types::Bases::Any") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Bases::Any, got %s\n", class_name); exit(1); } - break; + + rbs_types_bases_any_t *node = (rbs_types_bases_any_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("todo")), node->todo); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Any, + 1, + &h + ); } case RBS_TYPES_BASES_BOOL: { if (strcmp(class_name, "RBS::Types::Bases::Bool") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Bases::Bool, got %s\n", class_name); exit(1); } - break; + + rbs_types_bases_bool_t *node = (rbs_types_bases_bool_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Bool, + 1, + &h + ); } case RBS_TYPES_BASES_BOTTOM: { if (strcmp(class_name, "RBS::Types::Bases::Bottom") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Bases::Bottom, got %s\n", class_name); exit(1); } - break; + + rbs_types_bases_bottom_t *node = (rbs_types_bases_bottom_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Bottom, + 1, + &h + ); } case RBS_TYPES_BASES_CLASS: { if (strcmp(class_name, "RBS::Types::Bases::Class") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Bases::Class, got %s\n", class_name); exit(1); } - break; + + rbs_types_bases_class_t *node = (rbs_types_bases_class_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Class, + 1, + &h + ); } case RBS_TYPES_BASES_INSTANCE: { if (strcmp(class_name, "RBS::Types::Bases::Instance") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Bases::Instance, got %s\n", class_name); exit(1); } - break; + + rbs_types_bases_instance_t *node = (rbs_types_bases_instance_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Instance, + 1, + &h + ); } case RBS_TYPES_BASES_NIL: { if (strcmp(class_name, "RBS::Types::Bases::Nil") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Bases::Nil, got %s\n", class_name); exit(1); } - break; + + rbs_types_bases_nil_t *node = (rbs_types_bases_nil_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Nil, + 1, + &h + ); } case RBS_TYPES_BASES_SELF: { if (strcmp(class_name, "RBS::Types::Bases::Self") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Bases::Self, got %s\n", class_name); exit(1); } - break; + + rbs_types_bases_self_t *node = (rbs_types_bases_self_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Self, + 1, + &h + ); } case RBS_TYPES_BASES_TOP: { if (strcmp(class_name, "RBS::Types::Bases::Top") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Bases::Top, got %s\n", class_name); exit(1); } - break; + + rbs_types_bases_top_t *node = (rbs_types_bases_top_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Top, + 1, + &h + ); } case RBS_TYPES_BASES_VOID: { if (strcmp(class_name, "RBS::Types::Bases::Void") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Bases::Void, got %s\n", class_name); exit(1); } - break; + + rbs_types_bases_void_t *node = (rbs_types_bases_void_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Bases_Void, + 1, + &h + ); } case RBS_TYPES_BLOCK: { if (strcmp(class_name, "RBS::Types::Block") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Block, got %s\n", class_name); exit(1); } - break; + + rbs_types_block_t *node = (rbs_types_block_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("required")), node->required ? Qtrue : Qfalse); + rb_hash_aset(h, ID2SYM(rb_intern("self_type")), rbs_struct_to_ruby_value((rbs_node_t *) node->self_type)); // rbs_node + + return CLASS_NEW_INSTANCE( + RBS_Types_Block, + 1, + &h + ); } case RBS_TYPES_CLASSINSTANCE: { if (strcmp(class_name, "RBS::Types::ClassInstance") != 0) { fprintf(stderr, "Expected class name: RBS::Types::ClassInstance, got %s\n", class_name); exit(1); } - break; + + rbs_types_classinstance_t *node = (rbs_types_classinstance_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_ClassInstance, + 1, + &h + ); } case RBS_TYPES_CLASSSINGLETON: { if (strcmp(class_name, "RBS::Types::ClassSingleton") != 0) { fprintf(stderr, "Expected class name: RBS::Types::ClassSingleton, got %s\n", class_name); exit(1); } - break; + + rbs_types_classsingleton_t *node = (rbs_types_classsingleton_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_ClassSingleton, + 1, + &h + ); } case RBS_TYPES_FUNCTION: { if (strcmp(class_name, "RBS::Types::Function") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Function, got %s\n", class_name); exit(1); } - break; + + rbs_types_function_t *node = (rbs_types_function_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("required_positionals")), node->required_positionals); + rb_hash_aset(h, ID2SYM(rb_intern("optional_positionals")), node->optional_positionals); + rb_hash_aset(h, ID2SYM(rb_intern("rest_positionals")), node->rest_positionals); + rb_hash_aset(h, ID2SYM(rb_intern("trailing_positionals")), node->trailing_positionals); + rb_hash_aset(h, ID2SYM(rb_intern("required_keywords")), node->required_keywords); + rb_hash_aset(h, ID2SYM(rb_intern("optional_keywords")), node->optional_keywords); + rb_hash_aset(h, ID2SYM(rb_intern("rest_keywords")), node->rest_keywords); + rb_hash_aset(h, ID2SYM(rb_intern("return_type")), node->return_type); + + return CLASS_NEW_INSTANCE( + RBS_Types_Function, + 1, + &h + ); } case RBS_TYPES_FUNCTION_PARAM: { if (strcmp(class_name, "RBS::Types::Function::Param") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Function::Param, got %s\n", class_name); exit(1); } - break; + + rbs_types_function_param_t *node = (rbs_types_function_param_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Function_Param, + 1, + &h + ); } case RBS_TYPES_INTERFACE: { if (strcmp(class_name, "RBS::Types::Interface") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Interface, got %s\n", class_name); exit(1); } - break; + + rbs_types_interface_t *node = (rbs_types_interface_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Interface, + 1, + &h + ); } case RBS_TYPES_INTERSECTION: { if (strcmp(class_name, "RBS::Types::Intersection") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Intersection, got %s\n", class_name); exit(1); } - break; + + rbs_types_intersection_t *node = (rbs_types_intersection_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(node->types)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Intersection, + 1, + &h + ); } case RBS_TYPES_LITERAL: { if (strcmp(class_name, "RBS::Types::Literal") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Literal, got %s\n", class_name); exit(1); } - break; + + rbs_types_literal_t *node = (rbs_types_literal_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("literal")), node->literal); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Literal, + 1, + &h + ); } case RBS_TYPES_OPTIONAL: { if (strcmp(class_name, "RBS::Types::Optional") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Optional, got %s\n", class_name); exit(1); } - break; + + rbs_types_optional_t *node = (rbs_types_optional_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Optional, + 1, + &h + ); } case RBS_TYPES_PROC: { if (strcmp(class_name, "RBS::Types::Proc") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Proc, got %s\n", class_name); exit(1); } - break; + + rbs_types_proc_t *node = (rbs_types_proc_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("block")), rbs_struct_to_ruby_value((rbs_node_t *) node->block)); // rbs_types_block + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("self_type")), rbs_struct_to_ruby_value((rbs_node_t *) node->self_type)); // rbs_node + + return CLASS_NEW_INSTANCE( + RBS_Types_Proc, + 1, + &h + ); } case RBS_TYPES_RECORD: { if (strcmp(class_name, "RBS::Types::Record") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Record, got %s\n", class_name); exit(1); } - break; + + rbs_types_record_t *node = (rbs_types_record_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("all_fields")), node->all_fields); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Record, + 1, + &h + ); } case RBS_TYPES_TUPLE: { if (strcmp(class_name, "RBS::Types::Tuple") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Tuple, got %s\n", class_name); exit(1); } - break; + + rbs_types_tuple_t *node = (rbs_types_tuple_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(node->types)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Tuple, + 1, + &h + ); } case RBS_TYPES_UNION: { if (strcmp(class_name, "RBS::Types::Union") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Union, got %s\n", class_name); exit(1); } - break; + + rbs_types_union_t *node = (rbs_types_union_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(node->types)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Union, + 1, + &h + ); } case RBS_TYPES_UNTYPEDFUNCTION: { if (strcmp(class_name, "RBS::Types::UntypedFunction") != 0) { fprintf(stderr, "Expected class name: RBS::Types::UntypedFunction, got %s\n", class_name); exit(1); } - break; + + rbs_types_untypedfunction_t *node = (rbs_types_untypedfunction_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("return_type")), rbs_struct_to_ruby_value((rbs_node_t *) node->return_type)); // rbs_node + + return CLASS_NEW_INSTANCE( + RBS_Types_UntypedFunction, + 1, + &h + ); } case RBS_TYPES_VARIABLE: { if (strcmp(class_name, "RBS::Types::Variable") != 0) { fprintf(stderr, "Expected class name: RBS::Types::Variable, got %s\n", class_name); exit(1); } - break; - } - case RBS_TYPES_ZZZTMPNOTIMPLEMENTED: { - if (strcmp(class_name, "RBS::Types::ZzzTmpNotImplemented") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::ZzzTmpNotImplemented, got %s\n", class_name); - exit(1); - } - break; + + rbs_types_variable_t *node = (rbs_types_variable_t *)instance; + + VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + + return CLASS_NEW_INSTANCE( + RBS_Types_Variable, + 1, + &h + ); } } - - return instance->cached_ruby_value; } diff --git a/ext/rbs_extension/ast_translation.h b/ext/rbs_extension/ast_translation.h index e2b333ee9..0a05e0440 100644 --- a/ext/rbs_extension/ast_translation.h +++ b/ext/rbs_extension/ast_translation.h @@ -11,6 +11,9 @@ #include "ruby.h" #include "rbs/ast.h" +VALUE rbs_node_list_to_ruby_array(rbs_node_list_t *list); +VALUE rbs_hash_to_ruby_hash(rbs_hash_t *hash); +VALUE rbs_loc_to_ruby_location(rbs_location_t *loc); VALUE rbs_struct_to_ruby_value(rbs_node_t *instance); #endif diff --git a/ext/rbs_extension/location.c b/ext/rbs_extension/location.c index 5b251bbc6..a6637cb85 100644 --- a/ext/rbs_extension/location.c +++ b/ext/rbs_extension/location.c @@ -8,6 +8,15 @@ rbs_loc_range RBS_LOC_NULL_RANGE = { -1, -1 }; VALUE RBS_Location; +rbs_location_t *rbs_location_new(VALUE buffer, range rg) { + rbs_location_t *location = (rbs_location_t *)malloc(sizeof(rbs_location_t)); + location->cached_ruby_value = rbs_new_location(buffer, rg); + rb_gc_register_mark_object(location->cached_ruby_value); + location->buffer = buffer; + location->rg = rg; + return location; +} + position rbs_loc_position(int char_pos) { position pos = { 0, char_pos, -1, -1 }; return pos; @@ -30,7 +39,7 @@ static void check_children_max(unsigned short n) { } } -void rbs_loc_alloc_children(rbs_loc *loc, unsigned short cap) { +void rbs_loc_legacy_alloc_children(rbs_loc *loc, unsigned short cap) { check_children_max(cap); size_t s = RBS_LOC_CHILDREN_SIZE(cap); @@ -46,7 +55,7 @@ void rbs_loc_alloc_children(rbs_loc *loc, unsigned short cap) { static void check_children_cap(rbs_loc *loc) { if (loc->children == NULL) { - rbs_loc_alloc_children(loc, 1); + rbs_loc_legacy_alloc_children(loc, 1); } else { if (loc->children->len == loc->children->cap) { check_children_max(loc->children->cap + 1); @@ -56,14 +65,14 @@ static void check_children_cap(rbs_loc *loc) { } } -void rbs_loc_add_required_child(rbs_loc *loc, rbs_constant_id_t name, range r) { - rbs_loc_add_optional_child(loc, name, r); +void rbs_loc_legacy_add_required_child(rbs_loc *loc, rbs_constant_id_t name, range r) { + rbs_loc_legacy_add_optional_child(loc, name, r); unsigned short last_index = loc->children->len - 1; loc->children->required_p |= 1 << last_index; } -void rbs_loc_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, range r) { +void rbs_loc_legacy_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, range r) { check_children_cap(loc); unsigned short i = loc->children->len++; @@ -146,7 +155,7 @@ static VALUE location_initialize_copy(VALUE self, VALUE other) { }; if (other_loc->children != NULL) { - rbs_loc_alloc_children(self_loc, other_loc->children->cap); + rbs_loc_legacy_alloc_children(self_loc, other_loc->children->cap); memcpy(self_loc->children, other_loc->children, RBS_LOC_CHILDREN_SIZE(other_loc->children->cap)); } @@ -184,7 +193,7 @@ static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VA rg.start = rbs_loc_position(FIX2INT(start)); rg.end = rbs_loc_position(FIX2INT(end)); - rbs_loc_add_required_child(loc, rbs_constant_pool_insert_ruby_symbol(name), rg); + rbs_loc_legacy_add_required_child(loc, rbs_constant_pool_insert_ruby_symbol(name), rg); return Qnil; } @@ -196,7 +205,7 @@ static VALUE location_add_optional_child(VALUE self, VALUE name, VALUE start, VA rg.start = rbs_loc_position(FIX2INT(start)); rg.end = rbs_loc_position(FIX2INT(end)); - rbs_loc_add_optional_child(loc, rbs_constant_pool_insert_ruby_symbol(name), rg); + rbs_loc_legacy_add_optional_child(loc, rbs_constant_pool_insert_ruby_symbol(name), rg); return Qnil; } @@ -204,7 +213,7 @@ static VALUE location_add_optional_child(VALUE self, VALUE name, VALUE start, VA static VALUE location_add_optional_no_child(VALUE self, VALUE name) { rbs_loc *loc = rbs_check_location(self); - rbs_loc_add_optional_child(loc, rbs_constant_pool_insert_ruby_symbol(name), NULL_RANGE); + rbs_loc_legacy_add_optional_child(loc, rbs_constant_pool_insert_ruby_symbol(name), NULL_RANGE); return Qnil; } @@ -300,14 +309,6 @@ static VALUE location_required_keys(VALUE self) { return keys; } -VALUE rbs_location_pp(VALUE buffer, const position *start_pos, const position *end_pos) { - range rg = { *start_pos, *end_pos }; - rg.start = *start_pos; - rg.end = *end_pos; - - return rbs_new_location(buffer, rg); -} - void rbs__init_location(void) { RBS_Location = rb_define_class_under(RBS, "Location", rb_cObject); rb_define_alloc_func(RBS_Location, location_s_allocate); diff --git a/ext/rbs_extension/location.h b/ext/rbs_extension/location.h index 8a6c64b0b..63b071bc8 100644 --- a/ext/rbs_extension/location.h +++ b/ext/rbs_extension/location.h @@ -52,30 +52,21 @@ rbs_loc *rbs_check_location(VALUE location); * * Do not call twice for the same location. * */ -void rbs_loc_alloc_children(rbs_loc *loc, unsigned short cap); +void rbs_loc_legacy_alloc_children(rbs_loc *loc, unsigned short cap); /** * Add a required child range with given name. * - * Allocate memory for children with rbs_loc_alloc_children before calling this function. + * Allocate memory for children with rbs_loc_legacy_alloc_children before calling this function. * */ -void rbs_loc_add_required_child(rbs_loc *loc, rbs_constant_id_t name, range r); +void rbs_loc_legacy_add_required_child(rbs_loc *loc, rbs_constant_id_t name, range r); /** * Add an optional child range with given name. * - * Allocate memory for children with rbs_loc_alloc_children before calling this function. + * Allocate memory for children with rbs_loc_legacy_alloc_children before calling this function. * */ -void rbs_loc_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, range r); - -/** - * Returns RBS::Location object with start/end positions. - * - * @param start_pos - * @param end_pos - * @return New RSS::Location object. - * */ -VALUE rbs_location_pp(VALUE buffer, const position *start_pos, const position *end_pos); +void rbs_loc_legacy_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, range r); /** * Define RBS::Location class. diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index 719cb26e8..1d52d98fa 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -1,3 +1,4 @@ +#include "location.h" #include "rbs_extension.h" #include "rbs/util/rbs_constant_pool.h" #include "rbs/ast.h" @@ -53,25 +54,17 @@ /* nop */ typedef struct { - VALUE required_positionals; - VALUE optional_positionals; - VALUE rest_positionals; - VALUE trailing_positionals; + rbs_node_list_t *required_positionals; + rbs_node_list_t *optional_positionals; + rbs_node_t *rest_positionals; + rbs_node_list_t *trailing_positionals; VALUE required_keywords; VALUE optional_keywords; - VALUE rest_keywords; + rbs_node_t *rest_keywords; } method_params; -static VALUE EMPTY_ARRAY; - -static inline void melt_array(VALUE *array) { - if (*array == EMPTY_ARRAY) { - *array = rb_ary_new(); - } -} - static bool rbs_is_untyped_params(method_params *params) { - return NIL_P(params->required_positionals); + return params->required_positionals == NULL; } // /** @@ -80,7 +73,7 @@ static bool rbs_is_untyped_params(method_params *params) { // * @param state // * @return New RBS::Location object. // * */ -static VALUE rbs_location_current_token(parserstate *state) { +static rbs_location_t *rbs_location_current_token(parserstate *state) { return rbs_location_pp( state->buffer, &state->current_token.range.start, @@ -88,7 +81,7 @@ static VALUE rbs_location_current_token(parserstate *state) { ); } -static VALUE parse_optional(parserstate *state); +static rbs_node_t *parse_optional(parserstate *state); static rbs_node_t *parse_simple(parserstate *state); static VALUE string_of_loc(parserstate *state, position start, position end) { @@ -153,33 +146,35 @@ void parser_advance_no_gap(parserstate *state) { | {(tUIDENT `::`)*} | {} */ -static VALUE parse_type_name(parserstate *state, TypeNameKind kind, range *rg) { - VALUE absolute = Qfalse; - VALUE path = EMPTY_ARRAY; +static rbs_typename_t *parse_type_name(parserstate *state, TypeNameKind kind, range *rg) { + bool absolute = false; if (rg) { rg->start = state->current_token.range.start; } if (state->current_token.type == pCOLON2) { - absolute = Qtrue; + absolute = true; parser_advance_no_gap(state); } + rbs_node_list_t *path = rbs_node_list_new(&state->allocator); + while ( state->current_token.type == tUIDENT && state->next_token.type == pCOLON2 && state->current_token.range.end.byte_pos == state->next_token.range.start.byte_pos && state->next_token.range.end.byte_pos == state->next_token2.range.start.byte_pos ) { - melt_array(&path); - rb_ary_push(path, ID2SYM(INTERN_TOKEN(state, state->current_token))); + VALUE symbol_value = ID2SYM(INTERN_TOKEN(state, state->current_token)); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbol_value); + rbs_node_list_append(path, (rbs_node_t *)symbol); parser_advance(state); parser_advance(state); } - VALUE namespace = rbs_namespace(path, absolute); + rbs_namespace_t *namespace = rbs_namespace_new(&state->allocator, path, absolute); switch (state->current_token.type) { case tLIDENT: @@ -200,7 +195,9 @@ static VALUE parse_type_name(parserstate *state, TypeNameKind kind, range *rg) { rg->end = state->current_token.range.end; } - return rbs_type_name(namespace, ID2SYM(INTERN_TOKEN(state, state->current_token))); + VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token)); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, name); + return rbs_typename_new(&state->allocator, namespace, symbol); } error: { @@ -230,10 +227,10 @@ static VALUE parse_type_name(parserstate *state, TypeNameKind kind, range *rg) { type_list ::= {} type `,` ... <`,`> eol | {} type `,` ... `,` eol */ -static void parse_type_list(parserstate *state, enum TokenType eol, VALUE *types) { +static void parse_type_list(parserstate *state, enum TokenType eol, rbs_node_list_t *types) { while (true) { - melt_array(types); - rb_ary_push(*types, parse_type(state)); + rbs_node_t *type = parse_type(state); + rbs_node_list_append(types, type); if (state->next_token.type == pCOMMA) { parser_advance(state); @@ -275,21 +272,20 @@ static bool is_keyword_token(enum TokenType type) { function_param ::= {} | {} type */ -static VALUE parse_function_param(parserstate *state) { +static rbs_types_function_param_t *parse_function_param(parserstate *state) { range type_range; type_range.start = state->next_token.range.start; - VALUE type = parse_type(state); + rbs_node_t *type = parse_type(state); type_range.end = state->current_token.range.end; if (state->next_token.type == pCOMMA || state->next_token.type == pRPAREN) { range param_range = type_range; - VALUE location = rbs_new_location(state->buffer, param_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, param_range); rbs_loc_alloc_children(loc, 1); rbs_loc_add_optional_child(loc, INTERN("name"), NULL_RANGE); - return rbs_function_param(type, Qnil, location); + return rbs_types_function_param_new(&state->allocator, type, NULL, loc); } else { range name_range = state->next_token.range; @@ -308,13 +304,12 @@ static VALUE parse_function_param(parserstate *state) { ); } - VALUE name = rb_to_symbol(rbs_unquote_string(state, state->current_token.range, 0)); - VALUE location = rbs_new_location(state->buffer, param_range); - rbs_loc *loc = rbs_check_location(location); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, rb_to_symbol(rbs_unquote_string(state, state->current_token.range, 0))); + rbs_location_t *loc = rbs_location_new(state->buffer, param_range); rbs_loc_alloc_children(loc, 1); rbs_loc_add_optional_child(loc, INTERN("name"), name_range); - return rbs_function_param(type, name, location); + return rbs_types_function_param_new(&state->allocator, type, name, loc); } } @@ -330,15 +325,14 @@ static ID intern_token_start_end(parserstate *state, token start_token, token en keyword_key ::= {} `:` | {} keyword <`?`> `:` */ -static VALUE parse_keyword_key(parserstate *state) { +static rbs_ast_symbol_t *parse_keyword_key(parserstate *state) { parser_advance(state); - if (state->next_token.type == pQUESTION) { - VALUE key = ID2SYM(intern_token_start_end(state, state->current_token, state->next_token)); + rbs_ast_symbol_t *key = rbs_ast_symbol_new(&state->allocator, ID2SYM(intern_token_start_end(state, state->current_token, state->next_token))); parser_advance(state); return key; } else { - return ID2SYM(INTERN_TOKEN(state, state->current_token)); + return rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); } } @@ -346,22 +340,22 @@ static VALUE parse_keyword_key(parserstate *state) { keyword ::= {} keyword `:` */ static void parse_keyword(parserstate *state, VALUE keywords, VALUE memo) { - VALUE key = parse_keyword_key(state); + rbs_ast_symbol_t *key = parse_keyword_key(state); - if (!NIL_P(rb_hash_aref(memo, key))) { + if (!NIL_P(rb_hash_aref(memo, ((rbs_node_t *)key)->cached_ruby_value))) { raise_syntax_error( state, state->current_token, "duplicated keyword argument" ); } else { - rb_hash_aset(memo, key, Qtrue); + rb_hash_aset(memo, ((rbs_node_t *)key)->cached_ruby_value, Qtrue); } parser_advance_assert(state, pCOLON); - VALUE param = parse_function_param(state); + rbs_types_function_param_t *param = parse_function_param(state); - rb_hash_aset(keywords, key, param); + rb_hash_aset(keywords, ((rbs_node_t *)key)->cached_ruby_value, ((rbs_node_t *)param)->cached_ruby_value); return; } @@ -419,7 +413,7 @@ static bool is_keyword(parserstate *state) { */ static void parse_params(parserstate *state, method_params *params) { if (state->next_token.type == pQUESTION && state->next_token2.type == pRPAREN) { - params->required_positionals = Qnil; + params->required_positionals = NULL; parser_advance(state); return; } @@ -430,8 +424,6 @@ static void parse_params(parserstate *state, method_params *params) { VALUE memo = rb_hash_new(); while (true) { - VALUE param; - switch (state->next_token.type) { case pQUESTION: goto PARSE_OPTIONAL_PARAMS; @@ -447,9 +439,8 @@ static void parse_params(parserstate *state, method_params *params) { goto PARSE_KEYWORDS; } - param = parse_function_param(state); - melt_array(¶ms->required_positionals); - rb_ary_push(params->required_positionals, param); + rbs_types_function_param_t *param = parse_function_param(state); + rbs_node_list_append(params->required_positionals, (rbs_node_t *)param); break; } @@ -461,8 +452,6 @@ static void parse_params(parserstate *state, method_params *params) { PARSE_OPTIONAL_PARAMS: while (true) { - VALUE param; - switch (state->next_token.type) { case pQUESTION: parser_advance(state); @@ -473,9 +462,8 @@ static void parse_params(parserstate *state, method_params *params) { goto PARSE_KEYWORDS; } - param = parse_function_param(state); - melt_array(¶ms->optional_positionals); - rb_ary_push(params->optional_positionals, param); + rbs_types_function_param_t *param = parse_function_param(state); + rbs_node_list_append(params->optional_positionals, (rbs_node_t *)param); break; default: @@ -490,7 +478,8 @@ static void parse_params(parserstate *state, method_params *params) { PARSE_REST_PARAM: if (state->next_token.type == pSTAR) { parser_advance(state); - params->rest_positionals = parse_function_param(state); + rbs_types_function_param_t *param = parse_function_param(state); + params->rest_positionals = (rbs_node_t *) param; if (!parser_advance_if(state, pCOMMA)) { goto EOP; @@ -500,8 +489,6 @@ static void parse_params(parserstate *state, method_params *params) { PARSE_TRAILING_PARAMS: while (true) { - VALUE param; - switch (state->next_token.type) { case pQUESTION: goto PARSE_KEYWORDS; @@ -517,9 +504,8 @@ static void parse_params(parserstate *state, method_params *params) { goto PARSE_KEYWORDS; } - param = parse_function_param(state); - melt_array(¶ms->trailing_positionals); - rb_ary_push(params->trailing_positionals, param); + rbs_types_function_param_t *param = parse_function_param(state); + rbs_node_list_append(params->trailing_positionals, (rbs_node_t *)param); break; } @@ -547,7 +533,8 @@ static void parse_params(parserstate *state, method_params *params) { case pSTAR2: parser_advance(state); - params->rest_keywords = parse_function_param(state); + rbs_types_function_param_t *param = parse_function_param(state); + params->rest_keywords = (rbs_node_t *) param; break; case tUIDENT: @@ -593,30 +580,30 @@ static void parse_params(parserstate *state, method_params *params) { optional ::= {} | {} simple_type <`?`> */ -static VALUE parse_optional(parserstate *state) { +static rbs_node_t *parse_optional(parserstate *state) { range rg; rg.start = state->next_token.range.start; - VALUE type = rbs_struct_to_ruby_value(parse_simple(state)); + rbs_node_t *type = parse_simple(state); if (state->next_token.type == pQUESTION) { parser_advance(state); rg.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, rg); - return rbs_optional(type, location); + rbs_location_t *location = rbs_location_new(state->buffer, rg); + return (rbs_node_t *) rbs_types_optional_new(&state->allocator, type, location); } else { return type; } } -static void initialize_method_params(method_params *params){ +static void initialize_method_params(method_params *params, rbs_allocator_t *allocator) { *params = (method_params) { - .required_positionals = EMPTY_ARRAY, - .optional_positionals = EMPTY_ARRAY, - .rest_positionals = Qnil, - .trailing_positionals = EMPTY_ARRAY, + .required_positionals = rbs_node_list_new(allocator), + .optional_positionals = rbs_node_list_new(allocator), + .rest_positionals = NULL, + .trailing_positionals = rbs_node_list_new(allocator), .required_keywords = rb_hash_new(), .optional_keywords = rb_hash_new(), - .rest_keywords = Qnil, + .rest_keywords = NULL, }; } @@ -624,19 +611,25 @@ static void initialize_method_params(method_params *params){ self_type_binding ::= {} <> | {} `[` `self` `:` type <`]`> */ -static VALUE parse_self_type_binding(parserstate *state) { +static rbs_node_t *parse_self_type_binding(parserstate *state) { if (state->next_token.type == pLBRACKET) { parser_advance(state); parser_advance_assert(state, kSELF); parser_advance_assert(state, pCOLON); - VALUE type = parse_type(state); + rbs_node_t *type = parse_type(state); parser_advance_assert(state, pRBRACKET); return type; } else { - return Qnil; + return NULL; } } +typedef struct { + rbs_node_t *function; + rbs_types_block_t *block; + rbs_node_t *function_self_type; +} parse_function_result; + /* function ::= {} `(` params `)` self_type_binding? `{` `(` params `)` self_type_binding? `->` optional `}` `->` | {} `(` params `)` self_type_binding? `->` @@ -644,9 +637,12 @@ static VALUE parse_self_type_binding(parserstate *state) { | {} self_type_binding? `{` self_type_binding `->` optional `}` `->` | {} self_type_binding? `->` */ -static void parse_function(parserstate *state, VALUE *function, VALUE *block, VALUE *function_self_type) { +static parse_function_result parse_function(parserstate *state, bool accept_type_binding) { + rbs_node_t *function = NULL; + rbs_types_block_t *block = NULL; + rbs_node_t *function_self_type = NULL; method_params params; - initialize_method_params(¶ms); + initialize_method_params(¶ms, &state->allocator); if (state->next_token.type == pLPAREN) { parser_advance(state); @@ -662,21 +658,21 @@ static void parse_function(parserstate *state, VALUE *function, VALUE *block, VA } // Passing NULL to function_self_type means the function itself doesn't accept self type binding. (== method type) - if (function_self_type) { - *function_self_type = parse_self_type_binding(state); + if (accept_type_binding) { + function_self_type = parse_self_type_binding(state); } - VALUE required = Qtrue; + bool required = true; if (state->next_token.type == pQUESTION && state->next_token2.type == pLBRACE) { // Optional block - required = Qfalse; + required = false; parser_advance(state); } if (state->next_token.type == pLBRACE) { parser_advance(state); method_params block_params; - initialize_method_params(&block_params); + initialize_method_params(&block_params, &state->allocator); if (state->next_token.type == pLPAREN) { parser_advance(state); @@ -684,68 +680,100 @@ static void parse_function(parserstate *state, VALUE *function, VALUE *block, VA parser_advance_assert(state, pRPAREN); } - VALUE block_self_type = parse_self_type_binding(state); + rbs_node_t *self_type = parse_self_type_binding(state); parser_advance_assert(state, pARROW); - VALUE block_return_type = parse_optional(state); + rbs_node_t *block_return_type = parse_optional(state); - VALUE block_function = Qnil; + rbs_node_t *block_function = NULL; if (rbs_is_untyped_params(&block_params)) { - block_function = rbs_untyped_function(block_return_type); + block_function = (rbs_node_t *) rbs_types_untypedfunction_new(&state->allocator, block_return_type); } else { - block_function = rbs_function( - block_params.required_positionals, - block_params.optional_positionals, - block_params.rest_positionals, - block_params.trailing_positionals, + VALUE rest_positionals; + if (block_params.rest_positionals == NULL) { + rest_positionals = Qnil; + } else { + rest_positionals = block_params.rest_positionals->cached_ruby_value; + } + + VALUE rest_keywords; + if (block_params.rest_keywords == NULL) { + rest_keywords = Qnil; + } else { + rest_keywords = block_params.rest_keywords->cached_ruby_value; + } + + block_function = (rbs_node_t *) rbs_types_function_new( + &state->allocator, + block_params.required_positionals->cached_ruby_value, + block_params.optional_positionals->cached_ruby_value, + rest_positionals, + block_params.trailing_positionals->cached_ruby_value, block_params.required_keywords, block_params.optional_keywords, - block_params.rest_keywords, - block_return_type + rest_keywords, + block_return_type->cached_ruby_value ); } - *block = rbs_block(block_function, required, block_self_type); + block = rbs_types_block_new(&state->allocator, block_function, required, self_type); parser_advance_assert(state, pRBRACE); } parser_advance_assert(state, pARROW); - VALUE type = parse_optional(state); + rbs_node_t *type = parse_optional(state); if (rbs_is_untyped_params(¶ms)) { - *function = rbs_untyped_function(type); + function = (rbs_node_t *) rbs_types_untypedfunction_new(&state->allocator, type); } else { - *function = rbs_function( - params.required_positionals, - params.optional_positionals, - params.rest_positionals, - params.trailing_positionals, + VALUE rest_positionals; + if (params.rest_positionals == NULL) { + rest_positionals = Qnil; + } else { + rest_positionals = params.rest_positionals->cached_ruby_value; + } + + VALUE rest_keywords; + if (params.rest_keywords == NULL) { + rest_keywords = Qnil; + } else { + rest_keywords = params.rest_keywords->cached_ruby_value; + } + + function = (rbs_node_t *) rbs_types_function_new( + &state->allocator, + params.required_positionals->cached_ruby_value, + params.optional_positionals->cached_ruby_value, + rest_positionals, + params.trailing_positionals->cached_ruby_value, params.required_keywords, params.optional_keywords, - params.rest_keywords, - type + rest_keywords, + type->cached_ruby_value ); } + + parse_function_result result; + result.function = function; + result.block = block; + result.function_self_type = function_self_type; + return result; } /* proc_type ::= {`^`} */ -static VALUE parse_proc_type(parserstate *state) { +static rbs_types_proc_t *parse_proc_type(parserstate *state) { position start = state->current_token.range.start; - VALUE function = Qnil; - VALUE block = Qnil; - VALUE proc_self = Qnil; - parse_function(state, &function, &block, &proc_self); + parse_function_result result = parse_function(state, true); position end = state->current_token.range.end; - VALUE loc = rbs_location_pp(state->buffer, &start, &end); - - return rbs_proc(function, block, loc, proc_self); + rbs_location_t *loc = rbs_location_pp(state->buffer, &start, &end); + return rbs_types_proc_new(&state->allocator, result.function, result.block, loc, result.function_self_type); } -static void check_key_duplication(parserstate *state, VALUE fields, VALUE key) { - if (!NIL_P(rb_hash_aref(fields, key))) { +static void check_key_duplication(parserstate *state, VALUE fields, rbs_ast_symbol_t *key) { + if (!NIL_P(rb_hash_aref(fields, ((rbs_node_t *)key)->cached_ruby_value))) { raise_syntax_error( state, state->current_token, @@ -772,13 +800,13 @@ static VALUE parse_record_attributes(parserstate *state) { } while (true) { - VALUE key, type, - value = rb_ary_new(), - required = Qtrue; + rbs_ast_symbol_t *key; + VALUE value = rb_ary_new(); + bool required = true; if (state->next_token.type == pQUESTION) { // { ?foo: type } syntax - required = Qfalse; + required = false; parser_advance(state); } @@ -798,7 +826,7 @@ static VALUE parse_record_attributes(parserstate *state) { case tINTEGER: case kTRUE: case kFALSE: { - key = rb_funcall(rbs_struct_to_ruby_value(parse_simple(state)), rb_intern("literal"), 0); + key = rbs_ast_symbol_new(&state->allocator, rb_funcall(rbs_struct_to_ruby_value(parse_simple(state)), rb_intern("literal"), 0)); break; } default: @@ -811,10 +839,10 @@ static VALUE parse_record_attributes(parserstate *state) { check_key_duplication(state, fields, key); parser_advance_assert(state, pFATARROW); } - type = parse_type(state); - rb_ary_push(value, type); - rb_ary_push(value, required); - rb_hash_aset(fields, key, value); + rbs_node_t *type = parse_type(state); + rb_ary_push(value, type->cached_ruby_value); + rb_ary_push(value, rbs_ast_bool_new(&state->allocator, required)->base.cached_ruby_value); + rb_hash_aset(fields, ((rbs_node_t *)key)->cached_ruby_value, value); if (parser_advance_if(state, pCOMMA)) { if (state->next_token.type == pRBRACE) { @@ -830,28 +858,31 @@ static VALUE parse_record_attributes(parserstate *state) { /* symbol ::= {} */ -static VALUE parse_symbol(parserstate *state) { +static rbs_types_literal_t *parse_symbol(parserstate *state, rbs_location_t *location) { VALUE string = state->lexstate->string; rb_encoding *enc = rb_enc_get(string); int offset_bytes = rb_enc_codelen(':', enc); int bytes = token_bytes(state->current_token) - offset_bytes; - VALUE literal; + rbs_ast_symbol_t *literal; switch (state->current_token.type) { case tSYMBOL: { char *buffer = peek_token(state->lexstate, state->current_token); - literal = ID2SYM(rb_intern3(buffer+offset_bytes, bytes, enc)); + literal = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern3(buffer+offset_bytes, bytes, enc))); break; } case tDQSYMBOL: case tSQSYMBOL: { - literal = rb_funcall( - rbs_unquote_string(state, state->current_token.range, offset_bytes), - rb_intern("to_sym"), - 0 + literal = rbs_ast_symbol_new( + &state->allocator, + rb_funcall( + rbs_unquote_string(state, state->current_token.range, offset_bytes), + rb_intern("to_sym"), + 0 + ) ); break; } @@ -859,10 +890,7 @@ static VALUE parse_symbol(parserstate *state) { rbs_abort(); } - return rbs_literal( - literal, - rbs_location_current_token(state) - ); + return rbs_types_literal_new(&state->allocator, ((rbs_node_t *)literal)->cached_ruby_value, location); } /* @@ -871,15 +899,15 @@ static VALUE parse_symbol(parserstate *state) { type_args ::= {} <> /empty/ | {} `[` type_list <`]`> */ -static VALUE parse_instance_type(parserstate *state, bool parse_alias) { +static rbs_node_t *parse_instance_type(parserstate *state, bool parse_alias) { TypeNameKind expected_kind = INTERFACE_NAME | CLASS_NAME; if (parse_alias) { expected_kind |= ALIAS_NAME; } range name_range; - VALUE typename = parse_type_name(state, expected_kind, &name_range); - VALUE types = EMPTY_ARRAY; + rbs_typename_t *typename = parse_type_name(state, expected_kind, &name_range); + rbs_node_list_t *types = rbs_node_list_new(&state->allocator); TypeNameKind kind; if (state->current_token.type == tUIDENT) { @@ -896,7 +924,7 @@ static VALUE parse_instance_type(parserstate *state, bool parse_alias) { if (state->next_token.type == pLBRACKET) { parser_advance(state); args_range.start = state->current_token.range.start; - parse_type_list(state, pRBRACKET, &types); + parse_type_list(state, pRBRACKET, types); parser_advance_assert(state, pRBRACKET); args_range.end = state->current_token.range.end; } else { @@ -908,27 +936,26 @@ static VALUE parse_instance_type(parserstate *state, bool parse_alias) { .end = nonnull_pos_or(args_range.end, name_range.end), }; - VALUE location = rbs_new_location(state->buffer, type_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, type_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); if (kind == CLASS_NAME) { - return rbs_class_instance(typename, types, location); + return (rbs_node_t *) rbs_types_classinstance_new(&state->allocator, typename, types, loc); } else if (kind == INTERFACE_NAME) { - return rbs_interface(typename, types, location); + return (rbs_node_t *) rbs_types_interface_new(&state->allocator, typename, types, loc); } else if (kind == ALIAS_NAME) { - return rbs_alias(typename, types, location); + return (rbs_node_t *) rbs_types_alias_new(&state->allocator, typename, types, loc); } else { - return Qnil; + return NULL; } } /* singleton_type ::= {`singleton`} `(` type_name <`)`> */ -static VALUE parse_singleton_type(parserstate *state) { +static rbs_types_classsingleton_t *parse_singleton_type(parserstate *state) { parser_assert(state, kSINGLETON); range type_range; @@ -937,17 +964,16 @@ static VALUE parse_singleton_type(parserstate *state) { parser_advance(state); range name_range; - VALUE typename = parse_type_name(state, CLASS_NAME, &name_range); + rbs_typename_t *typename = parse_type_name(state, CLASS_NAME, &name_range); parser_advance_assert(state, pRPAREN); type_range.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, type_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, type_range); rbs_loc_alloc_children(loc, 1); rbs_loc_add_required_child(loc, INTERN("name"), name_range); - return rbs_class_singleton(typename, location); + return rbs_types_classsingleton_new(&state->allocator, typename, loc); } /* @@ -965,96 +991,78 @@ static rbs_node_t *parse_simple(parserstate *state) { switch (state->current_token.type) { case pLPAREN: { - VALUE type = parse_type(state); + rbs_node_t *type = parse_type(state); parser_advance_assert(state, pRPAREN); - - return (rbs_node_t *) rbs_types_zzztmpnotimplemented_new(&state->allocator, type); + return type; } case kBOOL: { - VALUE loc = rbs_location_current_token(state); - VALUE value = rbs_bases_bool(loc); - return (rbs_node_t *) rbs_types_bases_bool_new(&state->allocator, value, loc); + rbs_location_t *loc = rbs_location_current_token(state); + return (rbs_node_t *) rbs_types_bases_bool_new(&state->allocator, loc); } case kBOT: { - VALUE loc = rbs_location_current_token(state); - VALUE value = rbs_bases_bottom(loc); - return (rbs_node_t *) rbs_types_bases_bottom_new(&state->allocator, value, loc); + rbs_location_t *loc = rbs_location_current_token(state); + return (rbs_node_t *) rbs_types_bases_bottom_new(&state->allocator, loc); } case kCLASS: { - VALUE loc = rbs_location_current_token(state); - VALUE value = rbs_bases_class(loc); - return (rbs_node_t *) rbs_types_bases_class_new(&state->allocator, value, loc); + rbs_location_t *loc = rbs_location_current_token(state); + return (rbs_node_t *) rbs_types_bases_class_new(&state->allocator, loc); } case kINSTANCE: { - VALUE loc = rbs_location_current_token(state); - VALUE value = rbs_bases_instance(loc); - return (rbs_node_t *) rbs_types_bases_instance_new(&state->allocator, value, loc); + rbs_location_t *loc = rbs_location_current_token(state); + return (rbs_node_t *) rbs_types_bases_instance_new(&state->allocator, loc); } case kNIL: { - VALUE loc = rbs_location_current_token(state); - VALUE value = rbs_bases_nil(loc); - return (rbs_node_t *) rbs_types_bases_nil_new(&state->allocator, value, loc); + rbs_location_t *loc = rbs_location_current_token(state); + return (rbs_node_t *) rbs_types_bases_nil_new(&state->allocator, loc); } case kSELF: { - VALUE loc = rbs_location_current_token(state); - VALUE value = rbs_bases_self(loc); - return (rbs_node_t *) rbs_types_bases_self_new(&state->allocator, value, loc); + rbs_location_t *loc = rbs_location_current_token(state); + return (rbs_node_t *) rbs_types_bases_self_new(&state->allocator, loc); } case kTOP: { - VALUE loc = rbs_location_current_token(state); - VALUE value = rbs_bases_top(loc); - return (rbs_node_t *) rbs_types_bases_top_new(&state->allocator, value, loc); + rbs_location_t *loc = rbs_location_current_token(state); + return (rbs_node_t *) rbs_types_bases_top_new(&state->allocator, loc); } case kVOID: { - VALUE loc = rbs_location_current_token(state); - VALUE value = rbs_bases_void(loc); - return (rbs_node_t *) rbs_types_bases_void_new(&state->allocator, value, loc); + rbs_location_t *loc = rbs_location_current_token(state); + return (rbs_node_t *) rbs_types_bases_void_new(&state->allocator, loc); } case kUNTYPED: { - VALUE loc = rbs_location_current_token(state); - VALUE value = rbs_bases_any(Qfalse, loc); - return (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, value, Qfalse, loc); + rbs_location_t *loc = rbs_location_current_token(state); + return (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, Qfalse, loc); } case k__TODO__: { - VALUE loc = rbs_location_current_token(state); - VALUE value = rbs_bases_any(Qtrue, loc); - return (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, value, Qtrue, loc); + rbs_location_t *loc = rbs_location_current_token(state); + return (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, Qtrue, loc); } case tINTEGER: { - VALUE loc = rbs_location_current_token(state); + rbs_location_t *loc = rbs_location_current_token(state); VALUE literal = rb_funcall( string_of_loc(state, state->current_token.range.start, state->current_token.range.end), rb_intern("to_i"), 0 ); - VALUE value = rbs_literal(literal, loc); - return (rbs_node_t *) rbs_types_literal_new(&state->allocator, value, literal, loc); + return (rbs_node_t *) rbs_types_literal_new(&state->allocator, literal, loc); } case kTRUE: { - VALUE loc = rbs_location_current_token(state); - VALUE literal = Qtrue; - VALUE value = rbs_literal(literal, loc); - return (rbs_node_t *) rbs_types_literal_new(&state->allocator, value, literal, loc); + rbs_location_t *loc = rbs_location_current_token(state); + return (rbs_node_t *) rbs_types_literal_new(&state->allocator, Qtrue, loc); } case kFALSE: { - VALUE loc = rbs_location_current_token(state); - VALUE literal = Qfalse; - VALUE value = rbs_literal(literal, loc); - return (rbs_node_t *) rbs_types_literal_new(&state->allocator, value, literal, loc); + rbs_location_t *loc = rbs_location_current_token(state); + return (rbs_node_t *) rbs_types_literal_new(&state->allocator, Qfalse, loc); } case tSQSTRING: case tDQSTRING: { - VALUE loc = rbs_location_current_token(state); + rbs_location_t *loc = rbs_location_current_token(state); VALUE literal = rbs_unquote_string(state, state->current_token.range, 0); - VALUE value = rbs_literal(literal, loc); - return (rbs_node_t *) rbs_types_literal_new(&state->allocator, value, literal, loc); + return (rbs_node_t *) rbs_types_literal_new(&state->allocator, literal, loc); } case tSYMBOL: case tSQSYMBOL: case tDQSYMBOL: { - VALUE loc = rbs_location_current_token(state); - VALUE value = parse_symbol(state); - return (rbs_node_t *) rbs_types_literal_new(&state->allocator, value, value, loc); + rbs_location_t *loc = rbs_location_current_token(state); + return (rbs_node_t *) parse_symbol(state, loc); } case tUIDENT: { const char *name_str = peek_token(state->lexstate, state->current_token); @@ -1064,54 +1072,49 @@ static rbs_node_t *parse_simple(parserstate *state) { if (parser_typevar_member(state, name)) { ID name = rb_intern3(name_str, name_len, rb_enc_get(state->lexstate->string)); - VALUE loc = rbs_location_current_token(state); - VALUE value = rbs_variable(ID2SYM(name), loc); - return (rbs_node_t *) rbs_types_variable_new(&state->allocator, value, ID2SYM(name), loc); + rbs_location_t *loc = rbs_location_current_token(state); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, ID2SYM(name)); + return (rbs_node_t *) rbs_types_variable_new(&state->allocator, symbol, loc); } // fallthrough for type name } case tULIDENT: // fallthrough case tLIDENT: // fallthrough case pCOLON2: { - VALUE value = parse_instance_type(state, true); - return (rbs_node_t *) rbs_types_zzztmpnotimplemented_new(&state->allocator, value); + return parse_instance_type(state, true); } case kSINGLETON: { - VALUE value = parse_singleton_type(state); - VALUE loc = rb_funcall(value, rb_intern("location"), 0); - return (rbs_node_t *) rbs_types_classsingleton_new(&state->allocator, value, value, loc); + return (rbs_node_t *) parse_singleton_type(state); } case pLBRACKET: { range rg; rg.start = state->current_token.range.start; - VALUE types = EMPTY_ARRAY; + rbs_node_list_t *types = rbs_node_list_new(&state->allocator); if (state->next_token.type != pRBRACKET) { - parse_type_list(state, pRBRACKET, &types); + parse_type_list(state, pRBRACKET, types); } parser_advance_assert(state, pRBRACKET); rg.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, rg); - VALUE value = rbs_tuple(types, location); - return (rbs_node_t *) rbs_types_tuple_new(&state->allocator, value, types, location); + rbs_location_t *loc = rbs_location_new(state->buffer, rg); + return (rbs_node_t *) rbs_types_tuple_new(&state->allocator, types, loc); } case pAREF_OPR: { - VALUE loc = rbs_location_current_token(state); - VALUE value = rbs_tuple(EMPTY_ARRAY, loc); - return (rbs_node_t *) rbs_types_tuple_new(&state->allocator, value, EMPTY_ARRAY, loc); + rbs_location_t *loc = rbs_location_current_token(state); + rbs_node_list_t *types = rbs_node_list_new(&state->allocator); + return (rbs_node_t *) rbs_types_tuple_new(&state->allocator, types, loc); } case pLBRACE: { position start = state->current_token.range.start; VALUE fields = parse_record_attributes(state); parser_advance_assert(state, pRBRACE); position end = state->current_token.range.end; - VALUE loc = rbs_location_pp(state->buffer, &start, &end); - VALUE value = rbs_record(fields, loc); - return (rbs_node_t *) rbs_types_record_new(&state->allocator, value, fields, loc); + rbs_location_t *loc = rbs_location_pp(state->buffer, &start, &end); + return (rbs_node_t *) rbs_types_record_new(&state->allocator, fields, loc); } case pHAT: { - VALUE value = parse_proc_type(state); - return (rbs_node_t *) rbs_types_zzztmpnotimplemented_new(&state->allocator, value); + rbs_types_proc_t *value = parse_proc_type(state); + return (rbs_node_t *) value; } default: raise_syntax_error( @@ -1126,24 +1129,23 @@ static rbs_node_t *parse_simple(parserstate *state) { intersection ::= {} optional `&` ... '&' | {} */ -static VALUE parse_intersection(parserstate *state) { +static rbs_node_t *parse_intersection(parserstate *state) { range rg; rg.start = state->next_token.range.start; + rbs_node_t *type = parse_optional(state); + rbs_node_list_t *intersection_types = rbs_node_list_new(&state->allocator); - VALUE type = parse_optional(state); - VALUE intersection_types = rb_ary_new(); - - rb_ary_push(intersection_types, type); + rbs_node_list_append(intersection_types, type); while (state->next_token.type == pAMP) { parser_advance(state); - rb_ary_push(intersection_types, parse_optional(state)); + rbs_node_list_append(intersection_types, parse_optional(state)); } rg.end = state->current_token.range.end; - if (rb_array_len(intersection_types) > 1) { - VALUE location = rbs_new_location(state->buffer, rg); - type = rbs_intersection(intersection_types, location); + if (intersection_types->length > 1) { + rbs_location_t *location = rbs_location_new(state->buffer, rg); + type = (rbs_node_t *) rbs_types_intersection_new(&state->allocator, intersection_types, location); } return type; @@ -1153,24 +1155,24 @@ static VALUE parse_intersection(parserstate *state) { union ::= {} intersection '|' ... '|' | {} */ -VALUE parse_type(parserstate *state) { +rbs_node_t *parse_type(parserstate *state) { range rg; rg.start = state->next_token.range.start; - VALUE type = parse_intersection(state); - VALUE union_types = rb_ary_new(); + rbs_node_t *type = parse_intersection(state); + rbs_node_list_t *union_types = rbs_node_list_new(&state->allocator); - rb_ary_push(union_types, type); + rbs_node_list_append(union_types, type); while (state->next_token.type == pBAR) { parser_advance(state); - rb_ary_push(union_types, parse_intersection(state)); + rbs_node_list_append(union_types, parse_intersection(state)); } rg.end = state->current_token.range.end; - if (rb_array_len(union_types) > 1) { - VALUE location = rbs_new_location(state->buffer, rg); - type = rbs_union(union_types, location); + if (union_types->length > 1) { + rbs_location_t *location = rbs_location_new(state->buffer, rg); + type = (rbs_node_t *) rbs_types_union_new(&state->allocator, union_types, location); } return type; @@ -1184,8 +1186,8 @@ VALUE parse_type(parserstate *state) { type_param ::= tUIDENT upper_bound? default_type? (module_type_params == false) */ -static VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params) { - VALUE params = EMPTY_ARRAY; +static rbs_node_list_t *parse_type_params(parserstate *state, range *rg, bool module_type_params) { + rbs_node_list_t *params = rbs_node_list_new(&state->allocator); bool required_param_allowed = true; @@ -1195,16 +1197,16 @@ static VALUE parse_type_params(parserstate *state, range *rg, bool module_type_p rg->start = state->current_token.range.start; while (true) { - VALUE unchecked = Qfalse; - VALUE variance = ID2SYM(rb_intern("invariant")); - VALUE upper_bound = Qnil; - VALUE default_type = Qnil; + bool unchecked = false; + rbs_ast_symbol_t *variance = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("invariant"))); + rbs_node_t *upper_bound = NULL; + rbs_node_t *default_type = NULL; range param_range; param_range.start = state->next_token.range.start; - range variance_range = NULL_RANGE; range unchecked_range = NULL_RANGE; + range variance_range = NULL_RANGE; if (module_type_params) { if (state->next_token.type == kUNCHECKED) { unchecked = Qtrue; @@ -1215,10 +1217,10 @@ static VALUE parse_type_params(parserstate *state, range *rg, bool module_type_p if (state->next_token.type == kIN || state->next_token.type == kOUT) { switch (state->next_token.type) { case kIN: - variance = ID2SYM(rb_intern("contravariant")); + variance = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("contravariant"))); break; case kOUT: - variance = ID2SYM(rb_intern("covariant")); + variance = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("covariant"))); break; default: rbs_abort(); @@ -1238,7 +1240,8 @@ static VALUE parse_type_params(parserstate *state, range *rg, bool module_type_p token_bytes(state->current_token) ); - VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token)); + VALUE name_sym = ID2SYM(INTERN_TOKEN(state, state->current_token)); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, name_sym); parser_insert_typevar(state, id); @@ -1273,8 +1276,7 @@ static VALUE parse_type_params(parserstate *state, range *rg, bool module_type_p param_range.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, param_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, param_range); rbs_loc_alloc_children(loc, 5); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("variance"), variance_range); @@ -1282,10 +1284,9 @@ static VALUE parse_type_params(parserstate *state, range *rg, bool module_type_p rbs_loc_add_optional_child(loc, INTERN("upper_bound"), upper_bound_range); rbs_loc_add_optional_child(loc, INTERN("default"), default_type_range); - VALUE param = rbs_ast_type_param(name, variance, upper_bound, default_type, unchecked, location); + rbs_ast_typeparam_t *param = rbs_ast_typeparam_new(&state->allocator, name, variance, upper_bound, default_type, unchecked, loc); - melt_array(¶ms); - rb_ary_push(params, param); + rbs_node_list_append(params, (rbs_node_t *) param); if (state->next_token.type == pCOMMA) { parser_advance(state); @@ -1306,7 +1307,7 @@ static VALUE parse_type_params(parserstate *state, range *rg, bool module_type_p RBS_AST_TypeParam, rb_intern("resolve_variables"), 1, - params + params->cached_ruby_value ); return params; @@ -1315,98 +1316,88 @@ static VALUE parse_type_params(parserstate *state, range *rg, bool module_type_p /* method_type ::= {} type_params */ -VALUE parse_method_type(parserstate *state) { +rbs_methodtype_t *parse_method_type(parserstate *state) { parser_push_typevar_table(state, false); range rg; rg.start = state->next_token.range.start; range params_range = NULL_RANGE; - VALUE type_params = parse_type_params(state, ¶ms_range, false); + rbs_node_list_t *type_params = parse_type_params(state, ¶ms_range, false); range type_range; type_range.start = state->next_token.range.start; - VALUE function = Qnil; - VALUE block = Qnil; - parse_function(state, &function, &block, NULL); + parse_function_result result = parse_function(state, false); rg.end = state->current_token.range.end; type_range.end = rg.end; parser_pop_typevar_table(state); - VALUE location = rbs_new_location(state->buffer, rg); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, rg); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("type"), type_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range); - return rbs_method_type( - type_params, - function, - block, - location - ); + return rbs_methodtype_new(&state->allocator, type_params, result.function, result.block, loc); } /* global_decl ::= {tGIDENT} `:` */ -static VALUE parse_global_decl(parserstate *state) { +static rbs_ast_declarations_global_t *parse_global_decl(parserstate *state) { range decl_range; decl_range.start = state->current_token.range.start; VALUE comment = get_comment(state, decl_range.start.line); range name_range = state->current_token.range; - VALUE typename = ID2SYM(INTERN_TOKEN(state, state->current_token)); + rbs_ast_symbol_t *typename = rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); parser_advance_assert(state, pCOLON); range colon_range = state->current_token.range; - VALUE type = parse_type(state); + rbs_node_t *type = parse_type(state); decl_range.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, decl_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - return rbs_ast_decl_global(typename, type, location, comment); + return rbs_ast_declarations_global_new(&state->allocator, typename, type, loc, comment); } /* const_decl ::= {const_name} `:` */ -static VALUE parse_const_decl(parserstate *state) { +static rbs_ast_declarations_constant_t *parse_const_decl(parserstate *state) { range decl_range; decl_range.start = state->current_token.range.start; VALUE comment = get_comment(state, decl_range.start.line); range name_range; - VALUE typename = parse_type_name(state, CLASS_NAME, &name_range); + rbs_typename_t *typename = parse_type_name(state, CLASS_NAME, &name_range); parser_advance_assert(state, pCOLON); range colon_range = state->current_token.range; - VALUE type = parse_type(state); + rbs_node_t *type = parse_type(state); decl_range.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, decl_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - return rbs_ast_decl_constant(typename, type, location, comment); + return rbs_ast_declarations_constant_new(&state->allocator, typename, type, loc, comment); } /* type_decl ::= {kTYPE} alias_name `=` */ -static VALUE parse_type_decl(parserstate *state, position comment_pos, VALUE annotations) { +static rbs_ast_declarations_typealias_t *parse_type_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations) { parser_push_typevar_table(state, true); range decl_range; @@ -1418,19 +1409,18 @@ static VALUE parse_type_decl(parserstate *state, position comment_pos, VALUE ann parser_advance(state); range name_range; - VALUE typename = parse_type_name(state, ALIAS_NAME, &name_range); + rbs_typename_t *typename = parse_type_name(state, ALIAS_NAME, &name_range); range params_range; - VALUE type_params = parse_type_params(state, ¶ms_range, true); + rbs_node_list_t *type_params = parse_type_params(state, ¶ms_range, true); parser_advance_assert(state, pEQ); range eq_range = state->current_token.range; - VALUE type = parse_type(state); + rbs_node_t *type = parse_type(state); decl_range.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, decl_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); rbs_loc_alloc_children(loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -1439,20 +1429,14 @@ static VALUE parse_type_decl(parserstate *state, position comment_pos, VALUE ann parser_pop_typevar_table(state); - return rbs_ast_decl_type_alias( - typename, - type_params, - type, - annotations, - location, - get_comment(state, comment_pos.line) - ); + VALUE comment = get_comment(state, comment_pos.line); + return rbs_ast_declarations_typealias_new(&state->allocator, typename, type_params, type, annotations, loc, comment); } /* annotation ::= {} */ -static VALUE parse_annotation(parserstate *state) { +static rbs_ast_annotation_t *parse_annotation(parserstate *state) { VALUE content = rb_funcall(state->buffer, rb_intern("content"), 0); rb_encoding *enc = rb_enc_get(content); @@ -1499,16 +1483,15 @@ static VALUE parse_annotation(parserstate *state) { ); rb_funcall(string, rb_intern("strip!"), 0); - VALUE location = rbs_location_current_token(state); - - return rbs_ast_annotation(string, location); + rbs_location_t *loc = rbs_location_new(state->buffer, rg); + return rbs_ast_annotation_new(&state->allocator, string, loc); } /* annotations ::= {} annotation ... | {<>} */ -static void parse_annotations(parserstate *state, VALUE *annotations, position *annot_pos) { +static void parse_annotations(parserstate *state, rbs_node_list_t *annotations, position *annot_pos) { *annot_pos = NullPosition; while (true) { @@ -1519,8 +1502,7 @@ static void parse_annotations(parserstate *state, VALUE *annotations, position * *annot_pos = state->current_token.range.start; } - melt_array(annotations); - rb_ary_push(*annotations, parse_annotation(state)); + rbs_node_list_append(annotations, (rbs_node_t *)parse_annotation(state)); } else { break; } @@ -1531,7 +1513,7 @@ static void parse_annotations(parserstate *state, VALUE *annotations, position * method_name ::= {} | {} (IDENT | keyword)~<`?`> */ -static VALUE parse_method_name(parserstate *state, range *range) { +static rbs_ast_symbol_t *parse_method_name(parserstate *state, range *range) { parser_advance(state); switch (state->current_token.type) @@ -1552,21 +1534,20 @@ static VALUE parse_method_name(parserstate *state, range *range) { rb_enc_get(state->lexstate->string) ); - return ID2SYM(id); + return rbs_ast_symbol_new(&state->allocator, ID2SYM(id)); } else { *range = state->current_token.range; - return ID2SYM(INTERN_TOKEN(state, state->current_token)); + return rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); } case tBANGIDENT: - case tEQIDENT: + case tEQIDENT: { *range = state->current_token.range; - return ID2SYM(INTERN_TOKEN(state, state->current_token)); - + return rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); + } case tQIDENT: { - return rb_to_symbol(rbs_unquote_string(state, state->current_token.range, 0)); + return rbs_ast_symbol_new(&state->allocator, rb_to_symbol(rbs_unquote_string(state, state->current_token.range, 0))); } - case pBAR: case pHAT: case pAMP: @@ -1576,7 +1557,7 @@ static VALUE parse_method_name(parserstate *state, range *range) { case pAREF_OPR: case tOPERATOR: *range = state->current_token.range; - return ID2SYM(INTERN_TOKEN(state, state->current_token)); + return rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); default: raise_syntax_error( @@ -1644,7 +1625,7 @@ static InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, b * @param instance_only `true` to reject singleton method definition. * @param accept_overload `true` to accept overloading (...) definition. * */ -static VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overload, position comment_pos, VALUE annotations) { +static rbs_ast_members_methoddefinition_t *parse_member_def(parserstate *state, bool instance_only, bool accept_overload, position comment_pos, rbs_node_list_t *annotations) { range member_range; member_range.start = state->current_token.range.start; comment_pos = nonnull_pos_or(comment_pos, member_range.start); @@ -1652,26 +1633,26 @@ static VALUE parse_member_def(parserstate *state, bool instance_only, bool accep VALUE comment = get_comment(state, comment_pos.line); range visibility_range; - VALUE visibility; + rbs_ast_symbol_t *visibility; switch (state->current_token.type) { case kPRIVATE: { visibility_range = state->current_token.range; - visibility = ID2SYM(rb_intern("private")); + visibility = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("private"))); member_range.start = visibility_range.start; parser_advance(state); break; } case kPUBLIC: { visibility_range = state->current_token.range; - visibility = ID2SYM(rb_intern("public")); + visibility = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("public"))); member_range.start = visibility_range.start; parser_advance(state); break; } default: visibility_range = NULL_RANGE; - visibility = Qnil; + visibility = NULL; break; } @@ -1683,15 +1664,13 @@ static VALUE parse_member_def(parserstate *state, bool instance_only, bool accep kind_range = NULL_RANGE; kind = INSTANCE_KIND; } else { - kind = parse_instance_singleton_kind(state, NIL_P(visibility), &kind_range); + kind = parse_instance_singleton_kind(state, visibility == NULL, &kind_range); } range name_range; - VALUE name = parse_method_name(state, &name_range); - VALUE overloads = rb_ary_new(); - VALUE overloading = Qfalse; + rbs_ast_symbol_t *name = parse_method_name(state, &name_range); - if (state->next_token.type == pDOT && RB_SYM2ID(name) == rb_intern("self?")) { + if (state->next_token.type == pDOT && RB_SYM2ID(((rbs_node_t *)name)->cached_ruby_value) == rb_intern("self?")) { raise_syntax_error( state, state->next_token, @@ -1703,14 +1682,16 @@ static VALUE parse_member_def(parserstate *state, bool instance_only, bool accep parser_push_typevar_table(state, kind != INSTANCE_KIND); + rbs_node_list_t *overloads = rbs_node_list_new(&state->allocator); + bool overloading = false; range overloading_range = NULL_RANGE; bool loop = true; while (loop) { - VALUE annotations = EMPTY_ARRAY; + rbs_node_list_t *annotations = rbs_node_list_new(&state->allocator); position overload_annot_pos = NullPosition; if (state->next_token.type == tANNOTATION) { - parse_annotations(state, &annotations, &overload_annot_pos); + parse_annotations(state, annotations, &overload_annot_pos); } switch (state->next_token.type) { @@ -1720,15 +1701,16 @@ static VALUE parse_member_def(parserstate *state, bool instance_only, bool accep case pLBRACKET: case pQUESTION: { - VALUE method_type = parse_method_type(state); - rb_ary_push(overloads, rbs_ast_members_method_definition_overload(annotations, method_type)); + rbs_node_t *method_type = (rbs_node_t *) parse_method_type(state); + rbs_node_t *overload = (rbs_node_t *) rbs_ast_members_methoddefinition_overload_new(&state->allocator, annotations, method_type); + rbs_node_list_append(overloads, overload); member_range.end = state->current_token.range.end; break; } case pDOT3: if (accept_overload) { - overloading = Qtrue; + overloading = true; parser_advance(state); loop = false; overloading_range = state->current_token.range; @@ -1759,23 +1741,22 @@ static VALUE parse_member_def(parserstate *state, bool instance_only, bool accep parser_pop_typevar_table(state); - VALUE k; + rbs_ast_symbol_t *k; switch (kind) { case INSTANCE_KIND: - k = ID2SYM(rb_intern("instance")); + k = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("instance"))); break; case SINGLETON_KIND: - k = ID2SYM(rb_intern("singleton")); + k = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("singleton"))); break; case INSTANCE_SINGLETON_KIND: - k = ID2SYM(rb_intern("singleton_instance")); + k = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("singleton_instance"))); break; default: rbs_abort(); } - VALUE location = rbs_new_location(state->buffer, member_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, member_range); rbs_loc_alloc_children(loc, 5); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -1783,16 +1764,7 @@ static VALUE parse_member_def(parserstate *state, bool instance_only, bool accep rbs_loc_add_optional_child(loc, INTERN("overloading"), overloading_range); rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range); - return rbs_ast_members_method_definition( - name, - k, - overloads, - annotations, - location, - comment, - overloading, - visibility - ); + return rbs_ast_members_methoddefinition_new(&state->allocator, name, k, overloads, annotations, loc, comment, overloading, visibility); } /** @@ -1801,10 +1773,10 @@ static VALUE parse_member_def(parserstate *state, bool instance_only, bool accep * * @param kind * */ -void class_instance_name(parserstate *state, TypeNameKind kind, VALUE *name, VALUE *args, range *name_range, range *args_range) { +rbs_typename_t *class_instance_name(parserstate *state, TypeNameKind kind, rbs_node_list_t *args, range *name_range, range *args_range) { parser_advance(state); - *name = parse_type_name(state, kind, name_range); + rbs_typename_t *name = parse_type_name(state, kind, name_range); if (state->next_token.type == pLBRACKET) { parser_advance(state); @@ -1815,6 +1787,8 @@ void class_instance_name(parserstate *state, TypeNameKind kind, VALUE *name, VAL } else { *args_range = NULL_RANGE; } + + return name; } /** @@ -1824,7 +1798,7 @@ void class_instance_name(parserstate *state, TypeNameKind kind, VALUE *name, VAL * * @param from_interface `true` when the member is in an interface. * */ -static VALUE parse_mixin_member(parserstate *state, bool from_interface, position comment_pos, VALUE annotations) { +static rbs_node_t *parse_mixin_member(parserstate *state, bool from_interface, position comment_pos, rbs_node_list_t *annotations) { range member_range; member_range.start = state->current_token.range.start; comment_pos = nonnull_pos_or(comment_pos, member_range.start); @@ -1860,22 +1834,20 @@ static VALUE parse_mixin_member(parserstate *state, bool from_interface, positio parser_push_typevar_table(state, reset_typevar_scope); - VALUE name; - VALUE args = EMPTY_ARRAY; + rbs_node_list_t *args = rbs_node_list_new(&state->allocator); range name_range; range args_range = NULL_RANGE; - class_instance_name( + rbs_typename_t *name = class_instance_name( state, from_interface ? INTERFACE_NAME : (INTERFACE_NAME | CLASS_NAME), - &name, &args, &name_range, &args_range + args, &name_range, &args_range ); parser_pop_typevar_table(state); member_range.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, member_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, member_range); rbs_loc_alloc_children(loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); @@ -1885,11 +1857,11 @@ static VALUE parse_mixin_member(parserstate *state, bool from_interface, positio switch (type) { case kINCLUDE: - return rbs_ast_members_include(name, args, annotations, location, comment); + return (rbs_node_t *) rbs_ast_members_include_new(&state->allocator, name, args, annotations, loc, comment); case kEXTEND: - return rbs_ast_members_extend(name, args, annotations, location, comment); + return (rbs_node_t *) rbs_ast_members_extend_new(&state->allocator, name, args, annotations, loc, comment); case kPREPEND: - return rbs_ast_members_prepend(name, args, annotations, location, comment); + return (rbs_node_t *) rbs_ast_members_prepend_new(&state->allocator, name, args, annotations, loc, comment); default: rbs_abort(); } @@ -1903,7 +1875,7 @@ static VALUE parse_mixin_member(parserstate *state, bool from_interface, positio * * @param[in] instance_only `true` to reject `self.` alias. * */ -static VALUE parse_alias_member(parserstate *state, bool instance_only, position comment_pos, VALUE annotations) { +static rbs_ast_members_alias_t *parse_alias_member(parserstate *state, bool instance_only, position comment_pos, rbs_node_list_t *annotations) { range member_range; member_range.start = state->current_token.range.start; range keyword_range = state->current_token.range; @@ -1911,10 +1883,11 @@ static VALUE parse_alias_member(parserstate *state, bool instance_only, position comment_pos = nonnull_pos_or(comment_pos, member_range.start); VALUE comment = get_comment(state, comment_pos.line); - VALUE kind, new_name, old_name; + rbs_ast_symbol_t *kind, *new_name, *old_name; range new_kind_range, old_kind_range, new_name_range, old_name_range; + if (!instance_only && state->next_token.type == kSELF) { - kind = ID2SYM(rb_intern("singleton")); + kind = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("singleton"))); new_kind_range.start = state->next_token.range.start; new_kind_range.end = state->next_token2.range.end; @@ -1928,7 +1901,7 @@ static VALUE parse_alias_member(parserstate *state, bool instance_only, position parser_advance_assert(state, pDOT); old_name = parse_method_name(state, &old_name_range); } else { - kind = ID2SYM(rb_intern("instance")); + kind = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("instance"))); new_name = parse_method_name(state, &new_name_range); old_name = parse_method_name(state, &old_name_range); @@ -1937,8 +1910,7 @@ static VALUE parse_alias_member(parserstate *state, bool instance_only, position } member_range.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, member_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, member_range); rbs_loc_alloc_children(loc, 5); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("new_name"), new_name_range); @@ -1946,14 +1918,7 @@ static VALUE parse_alias_member(parserstate *state, bool instance_only, position rbs_loc_add_optional_child(loc, INTERN("new_kind"), new_kind_range); rbs_loc_add_optional_child(loc, INTERN("old_kind"), old_kind_range); - return rbs_ast_members_alias( - new_name, - old_name, - kind, - annotations, - location, - comment - ); + return rbs_ast_members_alias_new(&state->allocator, new_name, old_name, kind, annotations, loc, comment); } /* @@ -1961,8 +1926,8 @@ static VALUE parse_alias_member(parserstate *state, bool instance_only, position | {kSELF} `.` tAIDENT `:` | {tA2IDENT} `:` */ -static VALUE parse_variable_member(parserstate *state, position comment_pos, VALUE annotations) { - if (rb_array_len(annotations) > 0) { +static rbs_node_t *parse_variable_member(parserstate *state, position comment_pos, rbs_node_list_t *annotations) { + if (annotations->length > 0) { raise_syntax_error( state, state->current_token, @@ -1979,43 +1944,41 @@ static VALUE parse_variable_member(parserstate *state, position comment_pos, VAL { case tAIDENT: { range name_range = state->current_token.range; - VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token)); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); parser_advance_assert(state, pCOLON); range colon_range = state->current_token.range; - VALUE type = parse_type(state); + rbs_node_t *type = parse_type(state); member_range.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, member_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, member_range); rbs_loc_alloc_children(loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); - return rbs_ast_members_instance_variable(name, type, location, comment); + return (rbs_node_t *)rbs_ast_members_instancevariable_new(&state->allocator, name, type, loc, comment); } case tA2IDENT: { range name_range = state->current_token.range; - VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token)); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); parser_advance_assert(state, pCOLON); range colon_range = state->current_token.range; parser_push_typevar_table(state, true); - VALUE type = parse_type(state); + rbs_node_t *type = parse_type(state); parser_pop_typevar_table(state); member_range.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, member_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, member_range); rbs_loc_alloc_children(loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); - return rbs_ast_members_class_variable(name, type, location, comment); + return (rbs_node_t *) rbs_ast_members_classvariable_new(&state->allocator, name, type, loc, comment); } case kSELF: { range kind_range = { @@ -2027,24 +1990,23 @@ static VALUE parse_variable_member(parserstate *state, position comment_pos, VAL parser_advance_assert(state, tAIDENT); range name_range = state->current_token.range; - VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token)); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); parser_advance_assert(state, pCOLON); range colon_range = state->current_token.range; parser_push_typevar_table(state, true); - VALUE type = parse_type(state); + rbs_node_t *type = parse_type(state); parser_pop_typevar_table(state); member_range.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, member_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, member_range); rbs_loc_alloc_children(loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range); - return rbs_ast_members_class_instance_variable(name, type, location, comment); + return (rbs_node_t *)rbs_ast_members_classinstancevariable_new(&state->allocator, name, type, loc, comment); } default: rbs_abort(); @@ -2055,8 +2017,8 @@ static VALUE parse_variable_member(parserstate *state, position comment_pos, VAL visibility_member ::= {<`public`>} | {<`private`>} */ -static VALUE parse_visibility_member(parserstate *state, VALUE annotations) { - if (rb_array_len(annotations) > 0) { +static rbs_node_t *parse_visibility_member(parserstate *state, rbs_node_list_t *annotations) { + if (annotations->length > 0) { raise_syntax_error( state, state->current_token, @@ -2064,14 +2026,14 @@ static VALUE parse_visibility_member(parserstate *state, VALUE annotations) { ); } - VALUE location = rbs_new_location(state->buffer, state->current_token.range); + rbs_location_t *location = rbs_location_new(state->buffer, state->current_token.range); switch (state->current_token.type) { case kPUBLIC: - return rbs_ast_members_public(location); + return (rbs_node_t *) rbs_ast_members_public_new(&state->allocator, location); case kPRIVATE: - return rbs_ast_members_private(location); + return (rbs_node_t *) rbs_ast_members_private_new(&state->allocator, location); default: rbs_abort(); } @@ -2091,28 +2053,29 @@ static VALUE parse_visibility_member(parserstate *state, VALUE annotations) { | `(` tAIDENT `)` # Ivar name | `(` `)` # No variable */ -static VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE annotations) { +static rbs_node_t *parse_attribute_member(parserstate *state, position comment_pos, rbs_node_list_t *annotations) { range member_range; + member_range.start = state->current_token.range.start; comment_pos = nonnull_pos_or(comment_pos, member_range.start); VALUE comment = get_comment(state, comment_pos.line); - VALUE visibility; + rbs_ast_symbol_t *visibility; range visibility_range; switch (state->current_token.type) { case kPRIVATE: - visibility = ID2SYM(rb_intern("private")); + visibility = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("private"))); visibility_range = state->current_token.range; parser_advance(state); break; case kPUBLIC: - visibility = ID2SYM(rb_intern("public")); + visibility = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("public"))); visibility_range = state->current_token.range; parser_advance(state); break; default: - visibility = Qnil; + visibility = NULL; visibility_range = NULL_RANGE; break; } @@ -2122,22 +2085,22 @@ static VALUE parse_attribute_member(parserstate *state, position comment_pos, VA range kind_range; InstanceSingletonKind is_kind = parse_instance_singleton_kind(state, false, &kind_range); - VALUE kind = ID2SYM(rb_intern((is_kind == INSTANCE_KIND) ? "instance" : "singleton")); + rbs_ast_symbol_t *kind = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern((is_kind == INSTANCE_KIND) ? "instance" : "singleton"))); range name_range; - VALUE attr_name = parse_method_name(state, &name_range); + rbs_ast_symbol_t *attr_name = parse_method_name(state, &name_range); - VALUE ivar_name; + rbs_node_t *ivar_name; // rbs_ast_symbol_t, NULL or rbs_ast_bool_new(false) range ivar_range, ivar_name_range; if (state->next_token.type == pLPAREN) { parser_advance_assert(state, pLPAREN); ivar_range.start = state->current_token.range.start; if (parser_advance_if(state, tAIDENT)) { - ivar_name = ID2SYM(INTERN_TOKEN(state, state->current_token)); + ivar_name = (rbs_node_t *) rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); ivar_name_range = state->current_token.range; } else { - ivar_name = Qfalse; + ivar_name = (rbs_node_t *) rbs_ast_bool_new(&state->allocator, false); ivar_name_range = NULL_RANGE; } @@ -2145,7 +2108,7 @@ static VALUE parse_attribute_member(parserstate *state, position comment_pos, VA ivar_range.end = state->current_token.range.end; } else { ivar_range = NULL_RANGE; - ivar_name = Qnil; + ivar_name = NULL; ivar_name_range = NULL_RANGE; } @@ -2153,12 +2116,11 @@ static VALUE parse_attribute_member(parserstate *state, position comment_pos, VA range colon_range = state->current_token.range; parser_push_typevar_table(state, is_kind == SINGLETON_KIND); - VALUE type = parse_type(state); + rbs_node_t *type = parse_type(state); parser_pop_typevar_table(state); member_range.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, member_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, member_range); rbs_loc_alloc_children(loc, 7); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -2171,11 +2133,11 @@ static VALUE parse_attribute_member(parserstate *state, position comment_pos, VA switch (attr_type) { case kATTRREADER: - return rbs_ast_members_attr_reader(attr_name, type, ivar_name, kind, annotations, location, comment, visibility); + return (rbs_node_t *) rbs_ast_members_attrreader_new(&state->allocator, attr_name, type, ivar_name, kind, annotations, loc, comment, visibility); case kATTRWRITER: - return rbs_ast_members_attr_writer(attr_name, type, ivar_name, kind, annotations, location, comment, visibility); + return (rbs_node_t *) rbs_ast_members_attrwriter_new(&state->allocator, attr_name, type, ivar_name, kind, annotations, loc, comment, visibility); case kATTRACCESSOR: - return rbs_ast_members_attr_accessor(attr_name, type, ivar_name, kind, annotations, location, comment, visibility); + return (rbs_node_t *) rbs_ast_members_attraccessor_new(&state->allocator, attr_name, type, ivar_name, kind, annotations, loc, comment, visibility); default: rbs_abort(); } @@ -2188,33 +2150,33 @@ static VALUE parse_attribute_member(parserstate *state, position comment_pos, VA | mixin_member (interface only) | alias_member (instance only) */ -static VALUE parse_interface_members(parserstate *state) { - VALUE members = EMPTY_ARRAY; +static rbs_node_list_t *parse_interface_members(parserstate *state) { + rbs_node_list_t *members = rbs_node_list_new(&state->allocator); while (state->next_token.type != kEND) { - VALUE annotations = EMPTY_ARRAY; + rbs_node_list_t *annotations = rbs_node_list_new(&state->allocator); position annot_pos = NullPosition; - parse_annotations(state, &annotations, &annot_pos); + parse_annotations(state, annotations, &annot_pos); parser_advance(state); - VALUE member; + rbs_node_t *member; switch (state->current_token.type) { case kDEF: { - member = parse_member_def(state, true, true, annot_pos, annotations); + member = (rbs_node_t *) parse_member_def(state, true, true, annot_pos, annotations); break; } case kINCLUDE: case kEXTEND: case kPREPEND: { - member = parse_mixin_member(state, true, annot_pos, annotations); + member = (rbs_node_t *) parse_mixin_member(state, true, annot_pos, annotations); break; } case kALIAS: { - member = parse_alias_member(state, true, annot_pos, annotations); + member = (rbs_node_t *) parse_alias_member(state, true, annot_pos, annotations); break; } @@ -2226,8 +2188,7 @@ static VALUE parse_interface_members(parserstate *state) { ); } - melt_array(&members); - rb_ary_push(members, member); + rbs_node_list_append(members, member); } return members; @@ -2236,7 +2197,7 @@ static VALUE parse_interface_members(parserstate *state) { /* interface_decl ::= {`interface`} interface_name module_type_params interface_members */ -static VALUE parse_interface_decl(parserstate *state, position comment_pos, VALUE annotations) { +static rbs_ast_declarations_interface_t *parse_interface_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations) { parser_push_typevar_table(state, true); range member_range; @@ -2248,10 +2209,12 @@ static VALUE parse_interface_decl(parserstate *state, position comment_pos, VALU parser_advance(state); range name_range; - VALUE name = parse_type_name(state, INTERFACE_NAME, &name_range); + rbs_typename_t *name = parse_type_name(state, INTERFACE_NAME, &name_range); + range type_params_range; - VALUE params = parse_type_params(state, &type_params_range, true); - VALUE members = parse_interface_members(state); + rbs_node_list_t *type_params = parse_type_params(state, &type_params_range, true); + + rbs_node_list_t *members = parse_interface_members(state); parser_advance_assert(state, kEND); range end_range = state->current_token.range; @@ -2259,22 +2222,16 @@ static VALUE parse_interface_decl(parserstate *state, position comment_pos, VALU parser_pop_typevar_table(state); - VALUE location = rbs_new_location(state->buffer, member_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, member_range); rbs_loc_alloc_children(loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("end"), end_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range); - return rbs_ast_decl_interface( - name, - params, - members, - annotations, - location, - get_comment(state, comment_pos.line) - ); + VALUE comment = get_comment(state, comment_pos.line); + + return rbs_ast_declarations_interface_new(&state->allocator, name, type_params, members, annotations, loc, comment); } /* @@ -2283,35 +2240,33 @@ static VALUE parse_interface_decl(parserstate *state, position comment_pos, VALU module_self_type ::= | module_name `[` type_list <`]`> */ -static void parse_module_self_types(parserstate *state, VALUE *array) { +static void parse_module_self_types(parserstate *state, rbs_node_list_t *array) { while (true) { parser_advance(state); range self_range; self_range.start = state->current_token.range.start; range name_range; - VALUE module_name = parse_type_name(state, CLASS_NAME | INTERFACE_NAME, &name_range); + rbs_typename_t *module_name = parse_type_name(state, CLASS_NAME | INTERFACE_NAME, &name_range); self_range.end = name_range.end; - VALUE args = EMPTY_ARRAY; + rbs_node_list_t *args = rbs_node_list_new(&state->allocator); range args_range = NULL_RANGE; if (state->next_token.type == pLBRACKET) { parser_advance(state); args_range.start = state->current_token.range.start; - parse_type_list(state, pRBRACKET, &args); + parse_type_list(state, pRBRACKET, args); parser_advance(state); self_range.end = args_range.end = state->current_token.range.end; } - VALUE location = rbs_new_location(state->buffer, self_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, self_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); - VALUE self_type = rbs_ast_decl_module_self(module_name, args, location); - melt_array(array); - rb_ary_push(*array, self_type); + rbs_ast_declarations_module_self_t *self_type = rbs_ast_declarations_module_self_new(&state->allocator, module_name, args, loc); + rbs_node_list_append(array, (rbs_node_t *)self_type); if (state->next_token.type == pCOMMA) { parser_advance(state); @@ -2321,7 +2276,7 @@ static void parse_module_self_types(parserstate *state, VALUE *array) { } } -static VALUE parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, VALUE annotations); +static rbs_node_t *parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, rbs_node_list_t *annotations); /* module_members ::= {} ... kEND @@ -2334,33 +2289,34 @@ static VALUE parse_nested_decl(parserstate *state, const char *nested_in, positi | `public` | `private` */ -static VALUE parse_module_members(parserstate *state) { - VALUE members = EMPTY_ARRAY; +static rbs_node_list_t *parse_module_members(parserstate *state) { + rbs_node_list_t *members = rbs_node_list_new(&state->allocator); while (state->next_token.type != kEND) { - VALUE annotations = EMPTY_ARRAY; + rbs_node_list_t *annotations = rbs_node_list_new(&state->allocator); + position annot_pos = NullPosition; - parse_annotations(state, &annotations, &annot_pos); + parse_annotations(state, annotations, &annot_pos); parser_advance(state); - VALUE member; + rbs_node_t *member; switch (state->current_token.type) { case kDEF: { - member = parse_member_def(state, false, true, annot_pos, annotations); + member = (rbs_node_t *) parse_member_def(state, false, true, annot_pos, annotations); break; } case kINCLUDE: case kEXTEND: case kPREPEND: { - member = parse_mixin_member(state, false, annot_pos, annotations); + member = (rbs_node_t *) parse_mixin_member(state, false, annot_pos, annotations); break; } case kALIAS: { - member = parse_alias_member(state, false, annot_pos, annotations); + member = (rbs_node_t *) parse_alias_member(state, false, annot_pos, annotations); break; } @@ -2384,7 +2340,7 @@ static VALUE parse_module_members(parserstate *state) { switch (state->next_token.type) { case kDEF: { - member = parse_member_def(state, false, true, annot_pos, annotations); + member = (rbs_node_t *) parse_member_def(state, false, true, annot_pos, annotations); break; } case kATTRREADER: @@ -2406,8 +2362,7 @@ static VALUE parse_module_members(parserstate *state) { break; } - melt_array(&members); - rb_ary_push(members, member); + rbs_node_list_append(members, member); } return members; @@ -2417,36 +2372,36 @@ static VALUE parse_module_members(parserstate *state) { module_decl ::= {module_name} module_type_params module_members | {module_name} module_name module_type_params `:` module_self_types module_members */ -static VALUE parse_module_decl0(parserstate *state, range keyword_range, VALUE module_name, range name_range, VALUE comment, VALUE annotations) { +static rbs_ast_declarations_module_t *parse_module_decl0(parserstate *state, range keyword_range, rbs_typename_t *module_name, range name_range, VALUE comment, rbs_node_list_t *annotations) { parser_push_typevar_table(state, true); range decl_range; decl_range.start = keyword_range.start; + range type_params_range; - VALUE type_params = parse_type_params(state, &type_params_range, true); + rbs_node_list_t *type_params = parse_type_params(state, &type_params_range, true); - VALUE self_types = EMPTY_ARRAY; + rbs_node_list_t *self_types = rbs_node_list_new(&state->allocator); range colon_range; range self_types_range; if (state->next_token.type == pCOLON) { parser_advance(state); colon_range = state->current_token.range; self_types_range.start = state->next_token.range.start; - parse_module_self_types(state, &self_types); + parse_module_self_types(state, self_types); self_types_range.end = state->current_token.range.end; } else { colon_range = NULL_RANGE; self_types_range = NULL_RANGE; } - VALUE members = parse_module_members(state); + rbs_node_list_t *members = parse_module_members(state); parser_advance_assert(state, kEND); range end_range = state->current_token.range; decl_range.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, decl_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); rbs_loc_alloc_children(loc, 6); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -2457,15 +2412,7 @@ static VALUE parse_module_decl0(parserstate *state, range keyword_range, VALUE m parser_pop_typevar_table(state); - return rbs_ast_decl_module( - module_name, - type_params, - self_types, - members, - annotations, - location, - comment - ); + return rbs_ast_declarations_module_new(&state->allocator, module_name, type_params, self_types, members, annotations, loc, comment); } /* @@ -2473,7 +2420,7 @@ static VALUE parse_module_decl0(parserstate *state, range keyword_range, VALUE m | {`module`} module_name module_decl0 */ -static VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE annotations) { +static rbs_node_t *parse_module_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations) { range keyword_range = state->current_token.range; comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start); @@ -2481,7 +2428,7 @@ static VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE a parser_advance(state); range module_name_range; - VALUE module_name = parse_type_name(state, CLASS_NAME, &module_name_range); + rbs_typename_t *module_name = parse_type_name(state, CLASS_NAME, &module_name_range); if (state->next_token.type == pEQ) { range eq_range = state->next_token.range; @@ -2489,24 +2436,23 @@ static VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE a parser_advance(state); range old_name_range; - VALUE old_name = parse_type_name(state, CLASS_NAME, &old_name_range); + rbs_typename_t *old_name = parse_type_name(state, CLASS_NAME, &old_name_range); range decl_range = { .start = keyword_range.start, .end = old_name_range.end }; - VALUE location = rbs_new_location(state->buffer, decl_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); rbs_loc_alloc_children(loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("new_name"), module_name_range); rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); - return rbs_ast_decl_module_alias(module_name, old_name, location, comment); + return (rbs_node_t *) rbs_ast_declarations_modulealias_new(&state->allocator, module_name, old_name, loc, comment); } else { - return parse_module_decl0(state, keyword_range, module_name, module_name_range, comment, annotations); + return (rbs_node_t *) parse_module_decl0(state, keyword_range, module_name, module_name_range, comment, annotations); } } @@ -2514,50 +2460,48 @@ static VALUE parse_module_decl(parserstate *state, position comment_pos, VALUE a class_decl_super ::= {} `<` | {<>} */ -static VALUE parse_class_decl_super(parserstate *state, range *lt_range) { +static rbs_ast_declarations_class_super_t *parse_class_decl_super(parserstate *state, range *lt_range) { if (parser_advance_if(state, pLT)) { *lt_range = state->current_token.range; range super_range; super_range.start = state->next_token.range.start; - VALUE name; - VALUE args = EMPTY_ARRAY; + rbs_node_list_t *args = rbs_node_list_new(&state->allocator); range name_range, args_range; - class_instance_name(state, CLASS_NAME, &name, &args, &name_range, &args_range); + rbs_typename_t *name = class_instance_name(state, CLASS_NAME, args, &name_range, &args_range); super_range.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, super_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, super_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); - return rbs_ast_decl_class_super(name, args, location); + + return rbs_ast_declarations_class_super_new(&state->allocator, name, args, loc); } else { *lt_range = NULL_RANGE; - return Qnil; + return NULL; } } /* class_decl ::= {class_name} type_params class_decl_super class_members <`end`> */ -static VALUE parse_class_decl0(parserstate *state, range keyword_range, VALUE name, range name_range, VALUE comment, VALUE annotations) { +static rbs_ast_declarations_class_t *parse_class_decl0(parserstate *state, range keyword_range, rbs_typename_t *name, range name_range, VALUE comment, rbs_node_list_t *annotations) { parser_push_typevar_table(state, true); range decl_range; decl_range.start = keyword_range.start; range type_params_range; - VALUE type_params = parse_type_params(state, &type_params_range, true); + rbs_node_list_t *type_params = parse_type_params(state, &type_params_range, true); range lt_range; - VALUE super = parse_class_decl_super(state, <_range); - - VALUE members = parse_module_members(state); + rbs_ast_declarations_class_super_t *super = parse_class_decl_super(state, <_range); + struct rbs_node_list *members = parse_module_members(state); parser_advance_assert(state, kEND); range end_range = state->current_token.range; @@ -2566,8 +2510,7 @@ static VALUE parse_class_decl0(parserstate *state, range keyword_range, VALUE na parser_pop_typevar_table(state); - VALUE location = rbs_new_location(state->buffer, decl_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); rbs_loc_alloc_children(loc, 5); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -2575,22 +2518,14 @@ static VALUE parse_class_decl0(parserstate *state, range keyword_range, VALUE na rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range); rbs_loc_add_optional_child(loc, INTERN("lt"), lt_range); - return rbs_ast_decl_class( - name, - type_params, - super, - members, - annotations, - location, - comment - ); + return rbs_ast_declarations_class_new(&state->allocator, name, type_params, super, members, annotations, loc, comment); } /* class_decl ::= {`class`} class_name `=` | {`class`} class_name */ -static VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE annotations) { +static rbs_node_t *parse_class_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations) { range keyword_range = state->current_token.range; comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start); @@ -2598,7 +2533,7 @@ static VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE an parser_advance(state); range class_name_range; - VALUE class_name = parse_type_name(state, CLASS_NAME, &class_name_range); + rbs_typename_t *class_name = parse_type_name(state, CLASS_NAME, &class_name_range); if (state->next_token.type == pEQ) { range eq_range = state->next_token.range; @@ -2606,24 +2541,24 @@ static VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE an parser_advance(state); range old_name_range; - VALUE old_name = parse_type_name(state, CLASS_NAME, &old_name_range); + rbs_typename_t *old_name = parse_type_name(state, CLASS_NAME, &old_name_range); range decl_range = { .start = keyword_range.start, .end = old_name_range.end, }; - VALUE location = rbs_new_location(state->buffer, decl_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); rbs_loc_alloc_children(loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("new_name"), class_name_range); rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); - return rbs_ast_decl_class_alias(class_name, old_name, location, comment); + + return (rbs_node_t *) rbs_ast_declarations_classalias_new(&state->allocator, class_name, old_name, loc, comment); } else { - return parse_class_decl0(state, keyword_range, class_name, class_name_range, comment, annotations); + return (rbs_node_t *) parse_class_decl0(state, keyword_range, class_name, class_name_range, comment, annotations); } } @@ -2634,34 +2569,34 @@ static VALUE parse_class_decl(parserstate *state, position comment_pos, VALUE an | {} | {} */ -static VALUE parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, VALUE annotations) { +static rbs_node_t *parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, rbs_node_list_t *annotations) { parser_push_typevar_table(state, true); - VALUE decl; + rbs_node_t *decl; switch (state->current_token.type) { case tUIDENT: case pCOLON2: { - decl = parse_const_decl(state); + decl = (rbs_node_t *) parse_const_decl(state); break; } case tGIDENT: { - decl = parse_global_decl(state); + decl = (rbs_node_t *) parse_global_decl(state); break; } case kTYPE: { - decl = parse_type_decl(state, annot_pos, annotations); + decl = (rbs_node_t *) parse_type_decl(state, annot_pos, annotations); break; } case kINTERFACE: { - decl = parse_interface_decl(state, annot_pos, annotations); + decl = (rbs_node_t *) parse_interface_decl(state, annot_pos, annotations); break; } case kMODULE: { - decl = parse_module_decl(state, annot_pos, annotations); + decl = (rbs_node_t *) parse_module_decl(state, annot_pos, annotations); break; } case kCLASS: { - decl = parse_class_decl(state, annot_pos, annotations); + decl = (rbs_node_t *) parse_class_decl(state, annot_pos, annotations); break; } default: @@ -2677,32 +2612,32 @@ static VALUE parse_nested_decl(parserstate *state, const char *nested_in, positi return decl; } -static VALUE parse_decl(parserstate *state) { - VALUE annotations = EMPTY_ARRAY; +static rbs_node_t *parse_decl(parserstate *state) { + rbs_node_list_t *annotations = rbs_node_list_new(&state->allocator); position annot_pos = NullPosition; - parse_annotations(state, &annotations, &annot_pos); + parse_annotations(state, annotations, &annot_pos); parser_advance(state); switch (state->current_token.type) { case tUIDENT: case pCOLON2: { - return parse_const_decl(state); + return (rbs_node_t *) parse_const_decl(state); } case tGIDENT: { - return parse_global_decl(state); + return (rbs_node_t *) parse_global_decl(state); } case kTYPE: { - return parse_type_decl(state, annot_pos, annotations); + return (rbs_node_t *) parse_type_decl(state, annot_pos, annotations); } case kINTERFACE: { - return parse_interface_decl(state, annot_pos, annotations); + return (rbs_node_t *) parse_interface_decl(state, annot_pos, annotations); } case kMODULE: { - return parse_module_decl(state, annot_pos, annotations); + return (rbs_node_t *) parse_module_decl(state, annot_pos, annotations); } case kCLASS: { - return parse_class_decl(state, annot_pos, annotations); + return (rbs_node_t *) parse_class_decl(state, annot_pos, annotations); } default: raise_syntax_error( @@ -2717,7 +2652,7 @@ static VALUE parse_decl(parserstate *state) { namespace ::= {} (`::`)? (`tUIDENT` `::`)* `tUIDENT` <`::`> | {} <> (empty -- returns empty namespace) */ -static VALUE parse_namespace(parserstate *state, range *rg) { +static rbs_namespace_t *parse_namespace(parserstate *state, range *rg) { bool is_absolute = false; if (state->next_token.type == pCOLON2) { @@ -2730,12 +2665,13 @@ static VALUE parse_namespace(parserstate *state, range *rg) { parser_advance(state); } - VALUE path = EMPTY_ARRAY; + rbs_node_list_t *path = rbs_node_list_new(&state->allocator); while (true) { if (state->next_token.type == tUIDENT && state->next_token2.type == pCOLON2) { - melt_array(&path); - rb_ary_push(path, ID2SYM(INTERN_TOKEN(state, state->next_token))); + VALUE symbol_value = ID2SYM(INTERN_TOKEN(state, state->next_token)); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbol_value); + rbs_node_list_append(path, (rbs_node_t *)symbol); if (null_position_p(rg->start)) { rg->start = state->next_token.range.start; } @@ -2747,7 +2683,7 @@ static VALUE parse_namespace(parserstate *state, range *rg) { } } - return rbs_namespace(path, is_absolute ? Qtrue : Qfalse); + return rbs_namespace_new(&state->allocator, path, is_absolute); } /* @@ -2757,10 +2693,10 @@ static VALUE parse_namespace(parserstate *state, range *rg) { | {} namespace tUIDENT `as` | {} namespace */ -static void parse_use_clauses(parserstate *state, VALUE clauses) { +static void parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { while (true) { range namespace_range = NULL_RANGE; - VALUE namespace = parse_namespace(state, &namespace_range); + rbs_namespace_t *namespace = parse_namespace(state, &namespace_range); switch (state->next_token.type) { @@ -2775,12 +2711,12 @@ static void parse_use_clauses(parserstate *state, VALUE clauses) { ? state->current_token.range : (range) { .start = namespace_range.start, .end = state->current_token.range.end }; - VALUE type_name = rbs_type_name(namespace, ID2SYM(INTERN_TOKEN(state, state->current_token))); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); + rbs_typename_t *type_name = rbs_typename_new(&state->allocator, namespace, symbol); range keyword_range = NULL_RANGE; range new_name_range = NULL_RANGE; - - VALUE new_name = Qnil; + rbs_ast_symbol_t *new_name = NULL; range clause_range = type_name_range; if (state->next_token.type == kAS) { parser_advance(state); @@ -2790,19 +2726,19 @@ static void parse_use_clauses(parserstate *state, VALUE clauses) { if (ident_type == tLIDENT) parser_advance_assert(state, tLIDENT); if (ident_type == tULIDENT) parser_advance_assert(state, tULIDENT); - new_name = ID2SYM(INTERN_TOKEN(state, state->current_token)); + new_name = rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); new_name_range = state->current_token.range; clause_range.end = new_name_range.end; } - VALUE location = rbs_new_location(state->buffer, clause_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, clause_range); rbs_loc_alloc_children(loc, 3); rbs_loc_add_required_child(loc, INTERN("type_name"), type_name_range); rbs_loc_add_optional_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_optional_child(loc, INTERN("new_name"), new_name_range); - rb_ary_push(clauses, rbs_ast_directives_use_single_clause(type_name, new_name, location)); + rbs_ast_directives_use_singleclause_t *clause = rbs_ast_directives_use_singleclause_new(&state->allocator, type_name, new_name, loc); + rbs_node_list_append(clauses, (rbs_node_t *)clause); break; } @@ -2814,13 +2750,13 @@ static void parse_use_clauses(parserstate *state, VALUE clauses) { range star_range = state->current_token.range; clause_range.end = star_range.end; - VALUE location = rbs_new_location(state->buffer, clause_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, clause_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("namespace"), namespace_range); rbs_loc_add_required_child(loc, INTERN("star"), star_range); - rb_ary_push(clauses, rbs_ast_directives_use_wildcard_clause(namespace, location)); + rbs_ast_directives_use_wildcardclause_t *clause = rbs_ast_directives_use_wildcardclause_new(&state->allocator, namespace, loc); + rbs_node_list_append(clauses, (rbs_node_t *)clause); break; } @@ -2845,46 +2781,52 @@ static void parse_use_clauses(parserstate *state, VALUE clauses) { /* use_directive ::= {} `use` */ -static VALUE parse_use_directive(parserstate *state) { +static rbs_ast_directives_use_t *parse_use_directive(parserstate *state) { if (state->next_token.type == kUSE) { parser_advance(state); range keyword_range = state->current_token.range; - VALUE clauses = rb_ary_new(); + rbs_node_list_t *clauses = rbs_node_list_new(&state->allocator); parse_use_clauses(state, clauses); range directive_range = keyword_range; directive_range.end = state->current_token.range.end; - VALUE location = rbs_new_location(state->buffer, directive_range); - rbs_loc *loc = rbs_check_location(location); + rbs_location_t *loc = rbs_location_new(state->buffer, directive_range); rbs_loc_alloc_children(loc, 1); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); - return rbs_ast_directives_use(clauses, location); + return rbs_ast_directives_use_new(&state->allocator, clauses, loc); } else { - return Qnil; + return NULL; } } -VALUE parse_signature(parserstate *state) { - VALUE dirs = EMPTY_ARRAY; - VALUE decls = EMPTY_ARRAY; +rbs_node_list_t *parse_signature(parserstate *state) { + rbs_node_list_t *dir_nodes = rbs_node_list_new(&state->allocator); + rbs_ast_directives_nodes_t *dirs = rbs_ast_directives_nodes_new(&state->allocator, dir_nodes->cached_ruby_value, dir_nodes); + + rbs_node_list_t *decl_nodes = rbs_node_list_new(&state->allocator); + rbs_ast_declarations_nodes_t *decls = rbs_ast_declarations_nodes_new(&state->allocator, decl_nodes->cached_ruby_value, decl_nodes); while (state->next_token.type == kUSE) { - melt_array(&dirs); - rb_ary_push(dirs, parse_use_directive(state)); + rbs_ast_directives_use_t *use_node = parse_use_directive(state); + if (use_node == NULL) { + rbs_node_list_append(dir_nodes, NULL); + } else { + rbs_node_list_append(dir_nodes, (rbs_node_t *)use_node); + } } while (state->next_token.type != pEOF) { - melt_array(&decls); - rb_ary_push(decls, parse_decl(state)); + rbs_node_t *decl = parse_decl(state); + rbs_node_list_append(decl_nodes, decl); } - VALUE ret = rb_ary_new(); - rb_ary_push(ret, dirs); - rb_ary_push(ret, decls); + rbs_node_list_t *ret = rbs_node_list_new(&state->allocator); + rbs_node_list_append(ret, (rbs_node_t *)dirs); + rbs_node_list_append(ret, (rbs_node_t *)decls); return ret; } @@ -2907,13 +2849,13 @@ parse_type_try(VALUE a) { return Qnil; } - VALUE type = parse_type(arg->parser); + rbs_node_t *type = parse_type(arg->parser); if (RB_TEST(arg->require_eof)) { parser_advance_assert(arg->parser, pEOF); } - return type; + return type->cached_ruby_value; } static VALUE @@ -2937,13 +2879,13 @@ parse_method_type_try(VALUE a) { return Qnil; } - VALUE method_type = parse_method_type(arg->parser); + rbs_methodtype_t *method_type = parse_method_type(arg->parser); if (RB_TEST(arg->require_eof)) { parser_advance_assert(arg->parser, pEOF); } - return method_type; + return rbs_struct_to_ruby_value((rbs_node_t *)method_type); } static VALUE @@ -2962,7 +2904,7 @@ rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end static VALUE parse_signature_try(VALUE a) { parserstate *parser = (parserstate *)a; - return parse_signature(parser); + return parse_signature(parser)->cached_ruby_value; } static VALUE @@ -3003,7 +2945,6 @@ void rbs__init_parser(void) { rb_gc_register_mark_object(RBS_Parser); VALUE empty_array = rb_obj_freeze(rb_ary_new()); rb_gc_register_mark_object(empty_array); - EMPTY_ARRAY = empty_array; rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 5); rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5); diff --git a/ext/rbs_extension/parser.h b/ext/rbs_extension/parser.h index ea496d7ae..4d8b3de6e 100644 --- a/ext/rbs_extension/parser.h +++ b/ext/rbs_extension/parser.h @@ -9,9 +9,9 @@ * */ extern VALUE RBS_Parser; -VALUE parse_type(parserstate *state); -VALUE parse_method_type(parserstate *state); -VALUE parse_signature(parserstate *state); +rbs_node_t *parse_type(parserstate *state); +rbs_methodtype_t *parse_method_type(parserstate *state); +rbs_node_list_t *parse_signature(parserstate *state); void rbs__init_parser(); diff --git a/include/rbs/ast.h b/include/rbs/ast.h index 39060b570..e5936b590 100644 --- a/include/rbs/ast.h +++ b/include/rbs/ast.h @@ -10,67 +10,71 @@ #include "ruby.h" #include "rbs/util/rbs_allocator.h" +#include "rbs_location.h" enum rbs_node_type { RBS_AST_ANNOTATION = 1, - RBS_AST_COMMENT = 2, - RBS_AST_DECLARATIONS_CLASS = 3, - RBS_AST_DECLARATIONS_CLASS_SUPER = 4, - RBS_AST_DECLARATIONS_CLASSALIAS = 5, - RBS_AST_DECLARATIONS_CONSTANT = 6, - RBS_AST_DECLARATIONS_GLOBAL = 7, - RBS_AST_DECLARATIONS_INTERFACE = 8, - RBS_AST_DECLARATIONS_MODULE = 9, - RBS_AST_DECLARATIONS_MODULE_SELF = 10, - RBS_AST_DECLARATIONS_MODULEALIAS = 11, - RBS_AST_DECLARATIONS_TYPEALIAS = 12, - RBS_AST_DIRECTIVES_USE = 13, - RBS_AST_DIRECTIVES_USE_SINGLECLAUSE = 14, - RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE = 15, - RBS_AST_MEMBERS_ALIAS = 16, - RBS_AST_MEMBERS_ATTRACCESSOR = 17, - RBS_AST_MEMBERS_ATTRREADER = 18, - RBS_AST_MEMBERS_ATTRWRITER = 19, - RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE = 20, - RBS_AST_MEMBERS_CLASSVARIABLE = 21, - RBS_AST_MEMBERS_EXTEND = 22, - RBS_AST_MEMBERS_INCLUDE = 23, - RBS_AST_MEMBERS_INSTANCEVARIABLE = 24, - RBS_AST_MEMBERS_METHODDEFINITION = 25, - RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD = 26, - RBS_AST_MEMBERS_PREPEND = 27, - RBS_AST_MEMBERS_PRIVATE = 28, - RBS_AST_MEMBERS_PUBLIC = 29, - RBS_AST_TYPEPARAM = 30, - RBS_METHODTYPE = 31, - RBS_NAMESPACE = 32, - RBS_TYPENAME = 33, - RBS_TYPES_ALIAS = 34, - RBS_TYPES_BASES_ANY = 35, - RBS_TYPES_BASES_BOOL = 36, - RBS_TYPES_BASES_BOTTOM = 37, - RBS_TYPES_BASES_CLASS = 38, - RBS_TYPES_BASES_INSTANCE = 39, - RBS_TYPES_BASES_NIL = 40, - RBS_TYPES_BASES_SELF = 41, - RBS_TYPES_BASES_TOP = 42, - RBS_TYPES_BASES_VOID = 43, - RBS_TYPES_BLOCK = 44, - RBS_TYPES_CLASSINSTANCE = 45, - RBS_TYPES_CLASSSINGLETON = 46, - RBS_TYPES_FUNCTION = 47, - RBS_TYPES_FUNCTION_PARAM = 48, - RBS_TYPES_INTERFACE = 49, - RBS_TYPES_INTERSECTION = 50, - RBS_TYPES_LITERAL = 51, - RBS_TYPES_OPTIONAL = 52, - RBS_TYPES_PROC = 53, - RBS_TYPES_RECORD = 54, - RBS_TYPES_TUPLE = 55, - RBS_TYPES_UNION = 56, - RBS_TYPES_UNTYPEDFUNCTION = 57, - RBS_TYPES_VARIABLE = 58, - RBS_TYPES_ZZZTMPNOTIMPLEMENTED = 59, + RBS_AST_BOOL = 2, + RBS_AST_COMMENT = 3, + RBS_AST_DECLARATIONS_CLASS = 4, + RBS_AST_DECLARATIONS_CLASS_SUPER = 5, + RBS_AST_DECLARATIONS_CLASSALIAS = 6, + RBS_AST_DECLARATIONS_CONSTANT = 7, + RBS_AST_DECLARATIONS_GLOBAL = 8, + RBS_AST_DECLARATIONS_INTERFACE = 9, + RBS_AST_DECLARATIONS_MODULE = 10, + RBS_AST_DECLARATIONS_MODULE_SELF = 11, + RBS_AST_DECLARATIONS_MODULEALIAS = 12, + RBS_AST_DECLARATIONS_NODES = 13, + RBS_AST_DECLARATIONS_TYPEALIAS = 14, + RBS_AST_DIRECTIVES_NODES = 15, + RBS_AST_DIRECTIVES_USE = 16, + RBS_AST_DIRECTIVES_USE_SINGLECLAUSE = 17, + RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE = 18, + RBS_AST_MEMBERS_ALIAS = 19, + RBS_AST_MEMBERS_ATTRACCESSOR = 20, + RBS_AST_MEMBERS_ATTRREADER = 21, + RBS_AST_MEMBERS_ATTRWRITER = 22, + RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE = 23, + RBS_AST_MEMBERS_CLASSVARIABLE = 24, + RBS_AST_MEMBERS_EXTEND = 25, + RBS_AST_MEMBERS_INCLUDE = 26, + RBS_AST_MEMBERS_INSTANCEVARIABLE = 27, + RBS_AST_MEMBERS_METHODDEFINITION = 28, + RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD = 29, + RBS_AST_MEMBERS_PREPEND = 30, + RBS_AST_MEMBERS_PRIVATE = 31, + RBS_AST_MEMBERS_PUBLIC = 32, + RBS_AST_SYMBOL = 33, + RBS_AST_TYPEPARAM = 34, + RBS_METHODTYPE = 35, + RBS_NAMESPACE = 36, + RBS_TYPENAME = 37, + RBS_TYPES_ALIAS = 38, + RBS_TYPES_BASES_ANY = 39, + RBS_TYPES_BASES_BOOL = 40, + RBS_TYPES_BASES_BOTTOM = 41, + RBS_TYPES_BASES_CLASS = 42, + RBS_TYPES_BASES_INSTANCE = 43, + RBS_TYPES_BASES_NIL = 44, + RBS_TYPES_BASES_SELF = 45, + RBS_TYPES_BASES_TOP = 46, + RBS_TYPES_BASES_VOID = 47, + RBS_TYPES_BLOCK = 48, + RBS_TYPES_CLASSINSTANCE = 49, + RBS_TYPES_CLASSSINGLETON = 50, + RBS_TYPES_FUNCTION = 51, + RBS_TYPES_FUNCTION_PARAM = 52, + RBS_TYPES_INTERFACE = 53, + RBS_TYPES_INTERSECTION = 54, + RBS_TYPES_LITERAL = 55, + RBS_TYPES_OPTIONAL = 56, + RBS_TYPES_PROC = 57, + RBS_TYPES_RECORD = 58, + RBS_TYPES_TUPLE = 59, + RBS_TYPES_UNION = 60, + RBS_TYPES_UNTYPEDFUNCTION = 61, + RBS_TYPES_VARIABLE = 62, }; typedef struct rbs_node { @@ -78,400 +82,468 @@ typedef struct rbs_node { enum rbs_node_type type; } rbs_node_t; -typedef struct { +/* rbs_node_list_node */ + +typedef struct rbs_node_list_node { + rbs_node_t *node; + struct rbs_node_list_node *next; +} rbs_node_list_node_t; + +typedef struct rbs_node_list { + rbs_allocator_t *allocator; + rbs_node_list_node_t *head; + rbs_node_list_node_t *tail; + size_t length; + VALUE cached_ruby_value; +} rbs_node_list_t; + +rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *); + +void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node); + +/* rbs_hash */ + +typedef struct rbs_hash_node { + rbs_node_t *key; + rbs_node_t *value; + struct rbs_hash_node *next; +} rbs_hash_node_t; + +typedef struct rbs_hash { + rbs_allocator_t *allocator; + rbs_hash_node_t *head; + rbs_hash_node_t *tail; + size_t length; + VALUE cached_ruby_value; +} rbs_hash_t; + +rbs_hash_t* rbs_hash_new(rbs_allocator_t *); + +void rbs_hash_set(rbs_hash_t *hash, rbs_node_t *key, rbs_node_t *value); + +rbs_hash_node_t* rbs_hash_find(rbs_hash_t *hash, rbs_node_t *key); + +rbs_node_t* rbs_hash_get(rbs_hash_t *hash, rbs_node_t *key); + +/* rbs_ast_node */ + +typedef struct rbs_ast_annotation { rbs_node_t base; VALUE string; - VALUE location; + struct rbs_location *location; } rbs_ast_annotation_t; -typedef struct { +typedef struct rbs_ast_bool { + rbs_node_t base; + + bool value; +} rbs_ast_bool_t; + +typedef struct rbs_ast_comment { rbs_node_t base; VALUE string; - VALUE location; + struct rbs_location *location; } rbs_ast_comment_t; -typedef struct { +typedef struct rbs_ast_declarations_class { rbs_node_t base; - VALUE name; - VALUE type_params; - VALUE super_class; - VALUE members; - VALUE annotations; - VALUE location; + struct rbs_typename *name; + struct rbs_node_list *type_params; + struct rbs_ast_declarations_class_super *super_class; + struct rbs_node_list *members; + struct rbs_node_list *annotations; + struct rbs_location *location; VALUE comment; } rbs_ast_declarations_class_t; -typedef struct { +typedef struct rbs_ast_declarations_class_super { rbs_node_t base; - VALUE name; - VALUE args; - VALUE location; + struct rbs_typename *name; + struct rbs_node_list *args; + struct rbs_location *location; } rbs_ast_declarations_class_super_t; -typedef struct { +typedef struct rbs_ast_declarations_classalias { rbs_node_t base; - VALUE new_name; - VALUE old_name; - VALUE location; + struct rbs_typename *new_name; + struct rbs_typename *old_name; + struct rbs_location *location; VALUE comment; } rbs_ast_declarations_classalias_t; -typedef struct { +typedef struct rbs_ast_declarations_constant { rbs_node_t base; - VALUE name; - VALUE type; - VALUE location; + struct rbs_typename *name; + struct rbs_node *type; + struct rbs_location *location; VALUE comment; } rbs_ast_declarations_constant_t; -typedef struct { +typedef struct rbs_ast_declarations_global { rbs_node_t base; - VALUE name; - VALUE type; - VALUE location; + struct rbs_ast_symbol *name; + struct rbs_node *type; + struct rbs_location *location; VALUE comment; } rbs_ast_declarations_global_t; -typedef struct { +typedef struct rbs_ast_declarations_interface { rbs_node_t base; - VALUE name; - VALUE type_params; - VALUE members; - VALUE annotations; - VALUE location; + struct rbs_typename *name; + struct rbs_node_list *type_params; + struct rbs_node_list *members; + struct rbs_node_list *annotations; + struct rbs_location *location; VALUE comment; } rbs_ast_declarations_interface_t; -typedef struct { +typedef struct rbs_ast_declarations_module { rbs_node_t base; - VALUE name; - VALUE type_params; - VALUE self_types; - VALUE members; - VALUE annotations; - VALUE location; + struct rbs_typename *name; + struct rbs_node_list *type_params; + struct rbs_node_list *self_types; + struct rbs_node_list *members; + struct rbs_node_list *annotations; + struct rbs_location *location; VALUE comment; } rbs_ast_declarations_module_t; -typedef struct { +typedef struct rbs_ast_declarations_module_self { rbs_node_t base; - VALUE name; - VALUE args; - VALUE location; + struct rbs_typename *name; + struct rbs_node_list *args; + struct rbs_location *location; } rbs_ast_declarations_module_self_t; -typedef struct { +typedef struct rbs_ast_declarations_modulealias { rbs_node_t base; - VALUE new_name; - VALUE old_name; - VALUE location; + struct rbs_typename *new_name; + struct rbs_typename *old_name; + struct rbs_location *location; VALUE comment; } rbs_ast_declarations_modulealias_t; -typedef struct { +typedef struct rbs_ast_declarations_nodes { rbs_node_t base; - VALUE name; - VALUE type_params; - VALUE type; - VALUE annotations; - VALUE location; + struct rbs_node_list *declarations; +} rbs_ast_declarations_nodes_t; + +typedef struct rbs_ast_declarations_typealias { + rbs_node_t base; + + struct rbs_typename *name; + struct rbs_node_list *type_params; + struct rbs_node *type; + struct rbs_node_list *annotations; + struct rbs_location *location; VALUE comment; } rbs_ast_declarations_typealias_t; -typedef struct { +typedef struct rbs_ast_directives_nodes { + rbs_node_t base; + + struct rbs_node_list *directives; +} rbs_ast_directives_nodes_t; + +typedef struct rbs_ast_directives_use { rbs_node_t base; - VALUE clauses; - VALUE location; + struct rbs_node_list *clauses; + struct rbs_location *location; } rbs_ast_directives_use_t; -typedef struct { +typedef struct rbs_ast_directives_use_singleclause { rbs_node_t base; - VALUE type_name; - VALUE new_name; - VALUE location; + struct rbs_typename *type_name; + struct rbs_ast_symbol *new_name; + struct rbs_location *location; } rbs_ast_directives_use_singleclause_t; -typedef struct { +typedef struct rbs_ast_directives_use_wildcardclause { rbs_node_t base; - VALUE namespace; - VALUE location; + struct rbs_namespace *namespace; + struct rbs_location *location; } rbs_ast_directives_use_wildcardclause_t; -typedef struct { +typedef struct rbs_ast_members_alias { rbs_node_t base; - VALUE new_name; - VALUE old_name; - VALUE kind; - VALUE annotations; - VALUE location; + struct rbs_ast_symbol *new_name; + struct rbs_ast_symbol *old_name; + struct rbs_ast_symbol *kind; + struct rbs_node_list *annotations; + struct rbs_location *location; VALUE comment; } rbs_ast_members_alias_t; -typedef struct { +typedef struct rbs_ast_members_attraccessor { rbs_node_t base; - VALUE name; - VALUE type; - VALUE ivar_name; - VALUE kind; - VALUE annotations; - VALUE location; + struct rbs_ast_symbol *name; + struct rbs_node *type; + struct rbs_node *ivar_name; + struct rbs_ast_symbol *kind; + struct rbs_node_list *annotations; + struct rbs_location *location; VALUE comment; - VALUE visibility; + struct rbs_ast_symbol *visibility; } rbs_ast_members_attraccessor_t; -typedef struct { +typedef struct rbs_ast_members_attrreader { rbs_node_t base; - VALUE name; - VALUE type; - VALUE ivar_name; - VALUE kind; - VALUE annotations; - VALUE location; + struct rbs_ast_symbol *name; + struct rbs_node *type; + struct rbs_node *ivar_name; + struct rbs_ast_symbol *kind; + struct rbs_node_list *annotations; + struct rbs_location *location; VALUE comment; - VALUE visibility; + struct rbs_ast_symbol *visibility; } rbs_ast_members_attrreader_t; -typedef struct { +typedef struct rbs_ast_members_attrwriter { rbs_node_t base; - VALUE name; - VALUE type; - VALUE ivar_name; - VALUE kind; - VALUE annotations; - VALUE location; + struct rbs_ast_symbol *name; + struct rbs_node *type; + struct rbs_node *ivar_name; + struct rbs_ast_symbol *kind; + struct rbs_node_list *annotations; + struct rbs_location *location; VALUE comment; - VALUE visibility; + struct rbs_ast_symbol *visibility; } rbs_ast_members_attrwriter_t; -typedef struct { +typedef struct rbs_ast_members_classinstancevariable { rbs_node_t base; - VALUE name; - VALUE type; - VALUE location; + struct rbs_ast_symbol *name; + struct rbs_node *type; + struct rbs_location *location; VALUE comment; } rbs_ast_members_classinstancevariable_t; -typedef struct { +typedef struct rbs_ast_members_classvariable { rbs_node_t base; - VALUE name; - VALUE type; - VALUE location; + struct rbs_ast_symbol *name; + struct rbs_node *type; + struct rbs_location *location; VALUE comment; } rbs_ast_members_classvariable_t; -typedef struct { +typedef struct rbs_ast_members_extend { rbs_node_t base; - VALUE name; - VALUE args; - VALUE annotations; - VALUE location; + struct rbs_typename *name; + struct rbs_node_list *args; + struct rbs_node_list *annotations; + struct rbs_location *location; VALUE comment; } rbs_ast_members_extend_t; -typedef struct { +typedef struct rbs_ast_members_include { rbs_node_t base; - VALUE name; - VALUE args; - VALUE annotations; - VALUE location; + struct rbs_typename *name; + struct rbs_node_list *args; + struct rbs_node_list *annotations; + struct rbs_location *location; VALUE comment; } rbs_ast_members_include_t; -typedef struct { +typedef struct rbs_ast_members_instancevariable { rbs_node_t base; - VALUE name; - VALUE type; - VALUE location; + struct rbs_ast_symbol *name; + struct rbs_node *type; + struct rbs_location *location; VALUE comment; } rbs_ast_members_instancevariable_t; -typedef struct { +typedef struct rbs_ast_members_methoddefinition { rbs_node_t base; - VALUE name; - VALUE kind; - VALUE overloads; - VALUE annotations; - VALUE location; + struct rbs_ast_symbol *name; + struct rbs_ast_symbol *kind; + struct rbs_node_list *overloads; + struct rbs_node_list *annotations; + struct rbs_location *location; VALUE comment; - VALUE overloading; - VALUE visibility; + bool overloading; + struct rbs_ast_symbol *visibility; } rbs_ast_members_methoddefinition_t; -typedef struct { +typedef struct rbs_ast_members_methoddefinition_overload { rbs_node_t base; - VALUE annotations; - VALUE method_type; + struct rbs_node_list *annotations; + struct rbs_node *method_type; } rbs_ast_members_methoddefinition_overload_t; -typedef struct { +typedef struct rbs_ast_members_prepend { rbs_node_t base; - VALUE name; - VALUE args; - VALUE annotations; - VALUE location; + struct rbs_typename *name; + struct rbs_node_list *args; + struct rbs_node_list *annotations; + struct rbs_location *location; VALUE comment; } rbs_ast_members_prepend_t; -typedef struct { +typedef struct rbs_ast_members_private { rbs_node_t base; - VALUE location; + struct rbs_location *location; } rbs_ast_members_private_t; -typedef struct { +typedef struct rbs_ast_members_public { rbs_node_t base; - VALUE location; + struct rbs_location *location; } rbs_ast_members_public_t; -typedef struct { +typedef struct rbs_ast_symbol { rbs_node_t base; - VALUE name; - VALUE variance; - VALUE upper_bound; - VALUE default_type; - VALUE unchecked; - VALUE location; +} rbs_ast_symbol_t; + +typedef struct rbs_ast_typeparam { + rbs_node_t base; + + struct rbs_ast_symbol *name; + struct rbs_ast_symbol *variance; + struct rbs_node *upper_bound; + struct rbs_node *default_type; + bool unchecked; + struct rbs_location *location; } rbs_ast_typeparam_t; -typedef struct { +typedef struct rbs_methodtype { rbs_node_t base; - VALUE type_params; - VALUE type; - VALUE block; - VALUE location; + struct rbs_node_list *type_params; + struct rbs_node *type; + struct rbs_types_block *block; + struct rbs_location *location; } rbs_methodtype_t; -typedef struct { +typedef struct rbs_namespace { rbs_node_t base; - VALUE path; - VALUE absolute; + struct rbs_node_list *path; + bool absolute; } rbs_namespace_t; -typedef struct { +typedef struct rbs_typename { rbs_node_t base; - VALUE namespace; - VALUE name; + struct rbs_namespace *namespace; + struct rbs_ast_symbol *name; } rbs_typename_t; -typedef struct { +typedef struct rbs_types_alias { rbs_node_t base; - VALUE name; - VALUE args; - VALUE location; + struct rbs_typename *name; + struct rbs_node_list *args; + struct rbs_location *location; } rbs_types_alias_t; -typedef struct { +typedef struct rbs_types_bases_any { rbs_node_t base; VALUE todo; - VALUE location; + struct rbs_location *location; } rbs_types_bases_any_t; -typedef struct { +typedef struct rbs_types_bases_bool { rbs_node_t base; - VALUE location; + struct rbs_location *location; } rbs_types_bases_bool_t; -typedef struct { +typedef struct rbs_types_bases_bottom { rbs_node_t base; - VALUE location; + struct rbs_location *location; } rbs_types_bases_bottom_t; -typedef struct { +typedef struct rbs_types_bases_class { rbs_node_t base; - VALUE location; + struct rbs_location *location; } rbs_types_bases_class_t; -typedef struct { +typedef struct rbs_types_bases_instance { rbs_node_t base; - VALUE location; + struct rbs_location *location; } rbs_types_bases_instance_t; -typedef struct { +typedef struct rbs_types_bases_nil { rbs_node_t base; - VALUE location; + struct rbs_location *location; } rbs_types_bases_nil_t; -typedef struct { +typedef struct rbs_types_bases_self { rbs_node_t base; - VALUE location; + struct rbs_location *location; } rbs_types_bases_self_t; -typedef struct { +typedef struct rbs_types_bases_top { rbs_node_t base; - VALUE location; + struct rbs_location *location; } rbs_types_bases_top_t; -typedef struct { +typedef struct rbs_types_bases_void { rbs_node_t base; - VALUE location; + struct rbs_location *location; } rbs_types_bases_void_t; -typedef struct { +typedef struct rbs_types_block { rbs_node_t base; - VALUE type; - VALUE required; - VALUE self_type; + struct rbs_node *type; + bool required; + struct rbs_node *self_type; } rbs_types_block_t; -typedef struct { +typedef struct rbs_types_classinstance { rbs_node_t base; - VALUE name; - VALUE args; - VALUE location; + struct rbs_typename *name; + struct rbs_node_list *args; + struct rbs_location *location; } rbs_types_classinstance_t; -typedef struct { +typedef struct rbs_types_classsingleton { rbs_node_t base; - VALUE name; - VALUE location; + struct rbs_typename *name; + struct rbs_location *location; } rbs_types_classsingleton_t; -typedef struct { +typedef struct rbs_types_function { rbs_node_t base; VALUE required_positionals; @@ -484,152 +556,148 @@ typedef struct { VALUE return_type; } rbs_types_function_t; -typedef struct { +typedef struct rbs_types_function_param { rbs_node_t base; - VALUE type; - VALUE name; - VALUE location; + struct rbs_node *type; + struct rbs_ast_symbol *name; + struct rbs_location *location; } rbs_types_function_param_t; -typedef struct { +typedef struct rbs_types_interface { rbs_node_t base; - VALUE name; - VALUE args; - VALUE location; + struct rbs_typename *name; + struct rbs_node_list *args; + struct rbs_location *location; } rbs_types_interface_t; -typedef struct { +typedef struct rbs_types_intersection { rbs_node_t base; - VALUE types; - VALUE location; + struct rbs_node_list *types; + struct rbs_location *location; } rbs_types_intersection_t; -typedef struct { +typedef struct rbs_types_literal { rbs_node_t base; VALUE literal; - VALUE location; + struct rbs_location *location; } rbs_types_literal_t; -typedef struct { +typedef struct rbs_types_optional { rbs_node_t base; - VALUE type; - VALUE location; + struct rbs_node *type; + struct rbs_location *location; } rbs_types_optional_t; -typedef struct { +typedef struct rbs_types_proc { rbs_node_t base; - VALUE type; - VALUE block; - VALUE location; - VALUE self_type; + struct rbs_node *type; + struct rbs_types_block *block; + struct rbs_location *location; + struct rbs_node *self_type; } rbs_types_proc_t; -typedef struct { +typedef struct rbs_types_record { rbs_node_t base; VALUE all_fields; - VALUE location; + struct rbs_location *location; } rbs_types_record_t; -typedef struct { +typedef struct rbs_types_tuple { rbs_node_t base; - VALUE types; - VALUE location; + struct rbs_node_list *types; + struct rbs_location *location; } rbs_types_tuple_t; -typedef struct { +typedef struct rbs_types_union { rbs_node_t base; - VALUE types; - VALUE location; + struct rbs_node_list *types; + struct rbs_location *location; } rbs_types_union_t; -typedef struct { +typedef struct rbs_types_untypedfunction { rbs_node_t base; - VALUE return_type; + struct rbs_node *return_type; } rbs_types_untypedfunction_t; -typedef struct { +typedef struct rbs_types_variable { rbs_node_t base; - VALUE name; - VALUE location; + struct rbs_ast_symbol *name; + struct rbs_location *location; } rbs_types_variable_t; -typedef struct { - rbs_node_t base; - -} rbs_types_zzztmpnotimplemented_t; - - -rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE string, VALUE location); -rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE string, VALUE location); -rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE super_class, VALUE members, VALUE annotations, VALUE location, VALUE comment); -rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location); -rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE new_name, VALUE old_name, VALUE location, VALUE comment); -rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment); -rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment); -rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE members, VALUE annotations, VALUE location, VALUE comment); -rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE self_types, VALUE members, VALUE annotations, VALUE location, VALUE comment); -rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location); -rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE new_name, VALUE old_name, VALUE location, VALUE comment); -rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE type, VALUE annotations, VALUE location, VALUE comment); -rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE clauses, VALUE location); -rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type_name, VALUE new_name, VALUE location); -rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE namespace, VALUE location); -rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE new_name, VALUE old_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment); -rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility); -rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility); -rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility); -rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment); -rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment); -rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment); -rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment); -rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment); -rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE kind, VALUE overloads, VALUE annotations, VALUE location, VALUE comment, VALUE overloading, VALUE visibility); -rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE annotations, VALUE method_type); -rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment); -rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); -rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); -rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE variance, VALUE upper_bound, VALUE default_type, VALUE unchecked, VALUE location); -rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type_params, VALUE type, VALUE block, VALUE location); -rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE path, VALUE absolute); -rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE namespace, VALUE name); -rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location); -rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE todo, VALUE location); -rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); -rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); -rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); -rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); -rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); -rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); -rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); -rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location); -rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE required, VALUE self_type); -rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location); -rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE location); -rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE required_positionals, VALUE optional_positionals, VALUE rest_positionals, VALUE trailing_positionals, VALUE required_keywords, VALUE optional_keywords, VALUE rest_keywords, VALUE return_type); -rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE name, VALUE location); -rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location); -rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE types, VALUE location); -rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE literal, VALUE location); -rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE location); -rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE block, VALUE location, VALUE self_type); -rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE all_fields, VALUE location); -rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE types, VALUE location); -rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE types, VALUE location); -rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE return_type); -rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE location); -rbs_types_zzztmpnotimplemented_t *rbs_types_zzztmpnotimplemented_new(rbs_allocator_t *allocator, VALUE ruby_value); - -VALUE rbs_struct_to_ruby_value(rbs_node_t *instance); + +rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, VALUE string, rbs_location_t *location); +rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, bool value); +rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE string, rbs_location_t *location); +rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); +rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment); +rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); +rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); +rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); +rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment); +rbs_ast_declarations_nodes_t *rbs_ast_declarations_nodes_new(rbs_allocator_t *allocator, VALUE ruby_value, rbs_node_list_t *declarations); +rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +rbs_ast_directives_nodes_t *rbs_ast_directives_nodes_new(rbs_allocator_t *allocator, VALUE ruby_value, rbs_node_list_t *directives); +rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, rbs_node_list_t *clauses, rbs_location_t *location); +rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, rbs_typename_t *type_name, rbs_ast_symbol_t *new_name, rbs_location_t *location); +rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_namespace_t *namespace, rbs_location_t *location); +rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility); +rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility); +rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility); +rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); +rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); +rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); +rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_ast_symbol_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, bool overloading, rbs_ast_symbol_t *visibility); +rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, rbs_node_list_t *annotations, rbs_node_t *method_type); +rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocator, rbs_location_t *location); +rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, rbs_location_t *location); +rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, VALUE ruby_value); +rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_ast_symbol_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked, rbs_location_t *location); +rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location); +rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_node_list_t *path, bool absolute); +rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_namespace_t *namespace, rbs_ast_symbol_t *name); +rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); +rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, VALUE todo, rbs_location_t *location); +rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, rbs_location_t *location); +rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, rbs_location_t *location); +rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, rbs_location_t *location); +rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *allocator, rbs_location_t *location); +rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, rbs_location_t *location); +rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, rbs_location_t *location); +rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, rbs_location_t *location); +rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, rbs_location_t *location); +rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_node_t *type, bool required, rbs_node_t *self_type); +rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); +rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_location_t *location); +rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, VALUE required_positionals, VALUE optional_positionals, VALUE rest_positionals, VALUE trailing_positionals, VALUE required_keywords, VALUE optional_keywords, VALUE rest_keywords, VALUE return_type); +rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_ast_symbol_t *name, rbs_location_t *location); +rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); +rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location); +rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, VALUE literal, rbs_location_t *location); +rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_location_t *location); +rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location, rbs_node_t *self_type); +rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, VALUE all_fields, rbs_location_t *location); +rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location); +rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location); +rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, rbs_node_t *return_type); +rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_location_t *location); #endif diff --git a/include/rbs/constants.h b/include/rbs/constants.h index 995f5f923..c67d7c15e 100644 --- a/include/rbs/constants.h +++ b/include/rbs/constants.h @@ -47,6 +47,7 @@ extern VALUE RBS_AST_Members_MethodDefinition_Overload; extern VALUE RBS_AST_Members_Prepend; extern VALUE RBS_AST_Members_Private; extern VALUE RBS_AST_Members_Public; +extern VALUE RBS_AST_Symbol; extern VALUE RBS_AST_TypeParam; extern VALUE RBS_MethodType; extern VALUE RBS_Namespace; diff --git a/include/rbs/rbs_location.h b/include/rbs/rbs_location.h new file mode 100644 index 000000000..edd0cd6f8 --- /dev/null +++ b/include/rbs/rbs_location.h @@ -0,0 +1,28 @@ +#ifndef RBS__RBS_LOCATION_H +#define RBS__RBS_LOCATION_H + +#include "ruby.h" +#include "lexer.h" +#include "rbs/util/rbs_constant_pool.h" + +typedef struct rbs_location { + VALUE cached_ruby_value; + VALUE buffer; + range rg; +} rbs_location_t; + +rbs_location_t *rbs_location_new(VALUE buffer, range rg); +void rbs_loc_alloc_children(rbs_location_t *loc, int size); +void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, range r); +void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r); + +/** + * Returns rbs_location_t struct with start/end positions. + * + * @param start_pos + * @param end_pos + * @return New rbs_location_t struct. + * */ +rbs_location_t *rbs_location_pp(VALUE buffer, const position *start_pos, const position *end_pos); + +#endif diff --git a/include/rbs/ruby_objs.h b/include/rbs/ruby_objs.h index 04b2d2466..4809bde6f 100644 --- a/include/rbs/ruby_objs.h +++ b/include/rbs/ruby_objs.h @@ -10,63 +10,64 @@ #include "ruby.h" -VALUE rbs_ast_annotation(VALUE string, VALUE location); -VALUE rbs_ast_comment(VALUE string, VALUE location); -VALUE rbs_ast_decl_class(VALUE name, VALUE type_params, VALUE super_class, VALUE members, VALUE annotations, VALUE location, VALUE comment); -VALUE rbs_ast_decl_class_super(VALUE name, VALUE args, VALUE location); -VALUE rbs_ast_decl_class_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment); -VALUE rbs_ast_decl_constant(VALUE name, VALUE type, VALUE location, VALUE comment); -VALUE rbs_ast_decl_global(VALUE name, VALUE type, VALUE location, VALUE comment); -VALUE rbs_ast_decl_interface(VALUE name, VALUE type_params, VALUE members, VALUE annotations, VALUE location, VALUE comment); -VALUE rbs_ast_decl_module(VALUE name, VALUE type_params, VALUE self_types, VALUE members, VALUE annotations, VALUE location, VALUE comment); -VALUE rbs_ast_decl_module_self(VALUE name, VALUE args, VALUE location); -VALUE rbs_ast_decl_module_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment); -VALUE rbs_ast_decl_type_alias(VALUE name, VALUE type_params, VALUE type, VALUE annotations, VALUE location, VALUE comment); -VALUE rbs_ast_directives_use(VALUE clauses, VALUE location); -VALUE rbs_ast_directives_use_single_clause(VALUE type_name, VALUE new_name, VALUE location); -VALUE rbs_ast_directives_use_wildcard_clause(VALUE namespace, VALUE location); -VALUE rbs_ast_members_alias(VALUE new_name, VALUE old_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment); -VALUE rbs_ast_members_attr_accessor(VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility); -VALUE rbs_ast_members_attr_reader(VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility); -VALUE rbs_ast_members_attr_writer(VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility); -VALUE rbs_ast_members_class_instance_variable(VALUE name, VALUE type, VALUE location, VALUE comment); -VALUE rbs_ast_members_class_variable(VALUE name, VALUE type, VALUE location, VALUE comment); -VALUE rbs_ast_members_extend(VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment); -VALUE rbs_ast_members_include(VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment); -VALUE rbs_ast_members_instance_variable(VALUE name, VALUE type, VALUE location, VALUE comment); -VALUE rbs_ast_members_method_definition(VALUE name, VALUE kind, VALUE overloads, VALUE annotations, VALUE location, VALUE comment, VALUE overloading, VALUE visibility); -VALUE rbs_ast_members_method_definition_overload(VALUE annotations, VALUE method_type); -VALUE rbs_ast_members_prepend(VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment); -VALUE rbs_ast_members_private(VALUE location); -VALUE rbs_ast_members_public(VALUE location); -VALUE rbs_ast_type_param(VALUE name, VALUE variance, VALUE upper_bound, VALUE default_type, VALUE unchecked, VALUE location); -VALUE rbs_method_type(VALUE type_params, VALUE type, VALUE block, VALUE location); -VALUE rbs_namespace(VALUE path, VALUE absolute); -VALUE rbs_type_name(VALUE namespace, VALUE name); -VALUE rbs_alias(VALUE name, VALUE args, VALUE location); -VALUE rbs_bases_any(VALUE todo, VALUE location); -VALUE rbs_bases_bool(VALUE location); -VALUE rbs_bases_bottom(VALUE location); -VALUE rbs_bases_class(VALUE location); -VALUE rbs_bases_instance(VALUE location); -VALUE rbs_bases_nil(VALUE location); -VALUE rbs_bases_self(VALUE location); -VALUE rbs_bases_top(VALUE location); -VALUE rbs_bases_void(VALUE location); -VALUE rbs_block(VALUE type, VALUE required, VALUE self_type); -VALUE rbs_class_instance(VALUE name, VALUE args, VALUE location); -VALUE rbs_class_singleton(VALUE name, VALUE location); +VALUE rbs_ast_annotation(VALUE string, rbs_location_t *location); +VALUE rbs_ast_comment(VALUE string, rbs_location_t *location); +VALUE rbs_ast_decl_class(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +VALUE rbs_ast_decl_class_super(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); +VALUE rbs_ast_decl_class_alias(rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment); +VALUE rbs_ast_decl_constant(rbs_typename_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); +VALUE rbs_ast_decl_global(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); +VALUE rbs_ast_decl_interface(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +VALUE rbs_ast_decl_module(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +VALUE rbs_ast_decl_module_self(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); +VALUE rbs_ast_decl_module_alias(rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment); +VALUE rbs_ast_decl_type_alias(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +VALUE rbs_ast_directives_use(rbs_node_list_t *clauses, rbs_location_t *location); +VALUE rbs_ast_directives_use_single_clause(rbs_typename_t *type_name, rbs_ast_symbol_t *new_name, rbs_location_t *location); +VALUE rbs_ast_directives_use_wildcard_clause(rbs_namespace_t *namespace, rbs_location_t *location); +VALUE rbs_ast_members_alias(rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +VALUE rbs_ast_members_attr_accessor(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility); +VALUE rbs_ast_members_attr_reader(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility); +VALUE rbs_ast_members_attr_writer(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility); +VALUE rbs_ast_members_class_instance_variable(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); +VALUE rbs_ast_members_class_variable(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); +VALUE rbs_ast_members_extend(rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +VALUE rbs_ast_members_include(rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +VALUE rbs_ast_members_instance_variable(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); +VALUE rbs_ast_members_method_definition(rbs_ast_symbol_t *name, rbs_ast_symbol_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, bool overloading, rbs_ast_symbol_t *visibility); +VALUE rbs_ast_members_method_definition_overload(rbs_node_list_t *annotations, rbs_node_t *method_type); +VALUE rbs_ast_members_prepend(rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +VALUE rbs_ast_members_private(rbs_location_t *location); +VALUE rbs_ast_members_public(rbs_location_t *location); +VALUE rbs_ast_symbol(); +VALUE rbs_ast_type_param(rbs_ast_symbol_t *name, rbs_ast_symbol_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked, rbs_location_t *location); +VALUE rbs_method_type(rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location); +VALUE rbs_namespace(rbs_node_list_t *path, bool absolute); +VALUE rbs_type_name(rbs_namespace_t *namespace, rbs_ast_symbol_t *name); +VALUE rbs_alias(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); +VALUE rbs_bases_any(VALUE todo, rbs_location_t *location); +VALUE rbs_bases_bool(rbs_location_t *location); +VALUE rbs_bases_bottom(rbs_location_t *location); +VALUE rbs_bases_class(rbs_location_t *location); +VALUE rbs_bases_instance(rbs_location_t *location); +VALUE rbs_bases_nil(rbs_location_t *location); +VALUE rbs_bases_self(rbs_location_t *location); +VALUE rbs_bases_top(rbs_location_t *location); +VALUE rbs_bases_void(rbs_location_t *location); +VALUE rbs_block(rbs_node_t *type, bool required, rbs_node_t *self_type); +VALUE rbs_class_instance(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); +VALUE rbs_class_singleton(rbs_typename_t *name, rbs_location_t *location); VALUE rbs_function(VALUE required_positionals, VALUE optional_positionals, VALUE rest_positionals, VALUE trailing_positionals, VALUE required_keywords, VALUE optional_keywords, VALUE rest_keywords, VALUE return_type); -VALUE rbs_function_param(VALUE type, VALUE name, VALUE location); -VALUE rbs_interface(VALUE name, VALUE args, VALUE location); -VALUE rbs_intersection(VALUE types, VALUE location); -VALUE rbs_literal(VALUE literal, VALUE location); -VALUE rbs_optional(VALUE type, VALUE location); -VALUE rbs_proc(VALUE type, VALUE block, VALUE location, VALUE self_type); -VALUE rbs_record(VALUE all_fields, VALUE location); -VALUE rbs_tuple(VALUE types, VALUE location); -VALUE rbs_union(VALUE types, VALUE location); -VALUE rbs_untyped_function(VALUE return_type); -VALUE rbs_variable(VALUE name, VALUE location); +VALUE rbs_function_param(rbs_node_t *type, rbs_ast_symbol_t *name, rbs_location_t *location); +VALUE rbs_interface(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); +VALUE rbs_intersection(rbs_node_list_t *types, rbs_location_t *location); +VALUE rbs_literal(VALUE literal, rbs_location_t *location); +VALUE rbs_optional(rbs_node_t *type, rbs_location_t *location); +VALUE rbs_proc(rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location, rbs_node_t *self_type); +VALUE rbs_record(VALUE all_fields, rbs_location_t *location); +VALUE rbs_tuple(rbs_node_list_t *types, rbs_location_t *location); +VALUE rbs_union(rbs_node_list_t *types, rbs_location_t *location); +VALUE rbs_untyped_function(rbs_node_t *return_type); +VALUE rbs_variable(rbs_ast_symbol_t *name, rbs_location_t *location); #endif diff --git a/src/ast.c b/src/ast.c index 9efc9f5b3..4b91bdae3 100644 --- a/src/ast.c +++ b/src/ast.c @@ -6,14 +6,131 @@ /*----------------------------------------------------------------------------*/ #include "rbs/ast.h" -#include -rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE string, VALUE location) { +#include "rbs/ruby_objs.h" + +/* rbs_node_list */ + +rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *allocator) { + rbs_node_list_t *list = rbs_allocator_alloc(allocator, rbs_node_list_t); + *list = (rbs_node_list_t) { + .allocator = allocator, + .head = NULL, + .tail = NULL, + .length = 0, + .cached_ruby_value = rb_ary_new(), + }; + + rb_gc_register_mark_object(list->cached_ruby_value); + + return list; +} + +void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node) { + rb_gc_register_mark_object(node->cached_ruby_value); + + rbs_node_list_node_t *new_node = rbs_allocator_alloc(list->allocator, rbs_node_list_node_t); + *new_node = (rbs_node_list_node_t) { + .node = node, + .next = NULL, + }; + + if (list->tail == NULL) { + list->head = new_node; + list->tail = new_node; + } else { + list->tail->next = new_node; + list->tail = new_node; + } + list->length++; + + rb_ary_push(list->cached_ruby_value, node->cached_ruby_value); +} + +/* rbs_hash */ + +rbs_hash_t* rbs_hash_new(rbs_allocator_t *allocator) { + rbs_hash_t *hash = rbs_allocator_alloc(allocator, rbs_hash_t); + *hash = (rbs_hash_t) { + .allocator = allocator, + .head = NULL, + .tail = NULL, + .length = 0, + .cached_ruby_value = rb_hash_new(), + }; + + rb_gc_register_mark_object(hash->cached_ruby_value); + + return hash; +} + +bool rbs_node_equal(rbs_node_t *lhs, rbs_node_t *rhs) { + if (lhs == rhs) return true; + if (lhs->type != rhs->type) return false; + + switch (lhs->type) { + case RBS_AST_BOOL: + return ((rbs_ast_bool_t *)lhs)->value == ((rbs_ast_bool_t *) rhs)->value; + default: + return rb_equal(lhs->cached_ruby_value, rhs->cached_ruby_value); + } +} + +rbs_hash_node_t* rbs_hash_find(rbs_hash_t *hash, rbs_node_t *key) { + rbs_hash_node_t *current = hash->head; + + while (current != NULL) { + if (rbs_node_equal(key, current->key)) { + return current; + } + current = current->next; + } + + return NULL; +} + +void rbs_hash_set(rbs_hash_t *hash, rbs_node_t *key, rbs_node_t *value) { + rb_gc_register_mark_object(key->cached_ruby_value); + rb_gc_register_mark_object(value->cached_ruby_value); + + rbs_hash_node_t *existing_node = rbs_hash_find(hash, key); + if (existing_node != NULL) { + existing_node->value = value; + return; + } + + rb_hash_aset(hash->cached_ruby_value, key->cached_ruby_value, value->cached_ruby_value); + + rbs_hash_node_t *new_node = rbs_allocator_alloc(hash->allocator, rbs_hash_node_t); + new_node->key = key; + new_node->value = value; + new_node->next = NULL; + + if (hash->tail == NULL) { + hash->head = new_node; + hash->tail = new_node; + } else { + hash->tail->next = new_node; + hash->tail = new_node; + } +} + +rbs_node_t* rbs_hash_get(rbs_hash_t *hash, rbs_node_t *key) { + rbs_hash_node_t *node = rbs_hash_find(hash, key); + return node ? node->value : NULL; +} + +rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, VALUE string, rbs_location_t *location) { rbs_ast_annotation_t *instance = rbs_allocator_alloc(allocator, rbs_ast_annotation_t); - rb_gc_register_mark_object(ruby_value); + // Disable GC for all these Ruby objects. rb_gc_register_mark_object(string); - rb_gc_register_mark_object(location); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_annotation(string, location); + + rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_annotation_t) { .base = (rbs_node_t) { @@ -27,12 +144,37 @@ rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, VALUE r return instance; } -rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE string, VALUE location) { - rbs_ast_comment_t *instance = rbs_allocator_alloc(allocator, rbs_ast_comment_t); +rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, bool value) { + rbs_ast_bool_t *instance = rbs_allocator_alloc(allocator, rbs_ast_bool_t); + + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(value ? Qtrue : Qfalse); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = value ? Qtrue : Qfalse; rb_gc_register_mark_object(ruby_value); + + *instance = (rbs_ast_bool_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_BOOL + }, + .value = value, + }; + + return instance; +} + +rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE string, rbs_location_t *location) { + rbs_ast_comment_t *instance = rbs_allocator_alloc(allocator, rbs_ast_comment_t); + + // Disable GC for all these Ruby objects. rb_gc_register_mark_object(string); - rb_gc_register_mark_object(location); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + + rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_comment_t) { .base = (rbs_node_t) { @@ -46,18 +188,23 @@ rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, VALUE ruby_va return instance; } -rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE super_class, VALUE members, VALUE annotations, VALUE location, VALUE comment) { +rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { rbs_ast_declarations_class_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(type_params); - rb_gc_register_mark_object(super_class); - rb_gc_register_mark_object(members); - rb_gc_register_mark_object(annotations); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(type_params == NULL ? Qnil : type_params->cached_ruby_value); + rb_gc_register_mark_object(super_class == NULL ? Qnil : super_class->base.cached_ruby_value); + rb_gc_register_mark_object(members == NULL ? Qnil : members->cached_ruby_value); + rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_decl_class(name, type_params, super_class, members, annotations, location, comment); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_ast_declarations_class_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -75,13 +222,18 @@ rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *al return instance; } -rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location) { +rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { rbs_ast_declarations_class_super_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_super_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_decl_class_super(name, args, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(args); - rb_gc_register_mark_object(location); *instance = (rbs_ast_declarations_class_super_t) { .base = (rbs_node_t) { @@ -96,15 +248,20 @@ rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_all return instance; } -rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE new_name, VALUE old_name, VALUE location, VALUE comment) { +rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment) { rbs_ast_declarations_classalias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_classalias_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(new_name); - rb_gc_register_mark_object(old_name); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(new_name == NULL ? Qnil : new_name->base.cached_ruby_value); + rb_gc_register_mark_object(old_name == NULL ? Qnil : old_name->base.cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_decl_class_alias(new_name, old_name, location, comment); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_ast_declarations_classalias_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -119,15 +276,20 @@ rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_alloc return instance; } -rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment) { +rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { rbs_ast_declarations_constant_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_constant_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(type); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_decl_constant(name, type, location, comment); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_ast_declarations_constant_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -142,15 +304,20 @@ rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator return instance; } -rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment) { +rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { rbs_ast_declarations_global_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_global_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(type); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_decl_global(name, type, location, comment); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_ast_declarations_global_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -165,17 +332,22 @@ rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t * return instance; } -rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE members, VALUE annotations, VALUE location, VALUE comment) { +rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { rbs_ast_declarations_interface_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_interface_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(type_params); - rb_gc_register_mark_object(members); - rb_gc_register_mark_object(annotations); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(type_params == NULL ? Qnil : type_params->cached_ruby_value); + rb_gc_register_mark_object(members == NULL ? Qnil : members->cached_ruby_value); + rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_decl_interface(name, type_params, members, annotations, location, comment); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_ast_declarations_interface_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -192,18 +364,23 @@ rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocat return instance; } -rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE self_types, VALUE members, VALUE annotations, VALUE location, VALUE comment) { +rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { rbs_ast_declarations_module_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(type_params); - rb_gc_register_mark_object(self_types); - rb_gc_register_mark_object(members); - rb_gc_register_mark_object(annotations); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(type_params == NULL ? Qnil : type_params->cached_ruby_value); + rb_gc_register_mark_object(self_types == NULL ? Qnil : self_types->cached_ruby_value); + rb_gc_register_mark_object(members == NULL ? Qnil : members->cached_ruby_value); + rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_decl_module(name, type_params, self_types, members, annotations, location, comment); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_ast_declarations_module_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -221,13 +398,18 @@ rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t * return instance; } -rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location) { +rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { rbs_ast_declarations_module_self_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_self_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_decl_module_self(name, args, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(args); - rb_gc_register_mark_object(location); *instance = (rbs_ast_declarations_module_self_t) { .base = (rbs_node_t) { @@ -242,15 +424,20 @@ rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_all return instance; } -rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE new_name, VALUE old_name, VALUE location, VALUE comment) { +rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment) { rbs_ast_declarations_modulealias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_modulealias_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(new_name); - rb_gc_register_mark_object(old_name); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(new_name == NULL ? Qnil : new_name->base.cached_ruby_value); + rb_gc_register_mark_object(old_name == NULL ? Qnil : old_name->base.cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_decl_module_alias(new_name, old_name, location, comment); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_ast_declarations_modulealias_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -265,17 +452,42 @@ rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_all return instance; } -rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type_params, VALUE type, VALUE annotations, VALUE location, VALUE comment) { - rbs_ast_declarations_typealias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_typealias_t); +rbs_ast_declarations_nodes_t *rbs_ast_declarations_nodes_new(rbs_allocator_t *allocator, VALUE ruby_value, rbs_node_list_t *declarations) { + rbs_ast_declarations_nodes_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_nodes_t); + + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(declarations == NULL ? Qnil : declarations->cached_ruby_value); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(type_params); - rb_gc_register_mark_object(type); - rb_gc_register_mark_object(annotations); - rb_gc_register_mark_object(location); + + *instance = (rbs_ast_declarations_nodes_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_DECLARATIONS_NODES + }, + .declarations = declarations, + }; + + return instance; +} + +rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { + rbs_ast_declarations_typealias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_typealias_t); + + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(type_params == NULL ? Qnil : type_params->cached_ruby_value); + rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); + rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_decl_type_alias(name, type_params, type, annotations, location, comment); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_ast_declarations_typealias_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -292,12 +504,37 @@ rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocat return instance; } -rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE clauses, VALUE location) { +rbs_ast_directives_nodes_t *rbs_ast_directives_nodes_new(rbs_allocator_t *allocator, VALUE ruby_value, rbs_node_list_t *directives) { + rbs_ast_directives_nodes_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_nodes_t); + + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(directives == NULL ? Qnil : directives->cached_ruby_value); + + + rb_gc_register_mark_object(ruby_value); + + *instance = (rbs_ast_directives_nodes_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_DIRECTIVES_NODES + }, + .directives = directives, + }; + + return instance; +} + +rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, rbs_node_list_t *clauses, rbs_location_t *location) { rbs_ast_directives_use_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(clauses == NULL ? Qnil : clauses->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_directives_use(clauses, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(clauses); - rb_gc_register_mark_object(location); *instance = (rbs_ast_directives_use_t) { .base = (rbs_node_t) { @@ -311,13 +548,18 @@ rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, return instance; } -rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type_name, VALUE new_name, VALUE location) { +rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, rbs_typename_t *type_name, rbs_ast_symbol_t *new_name, rbs_location_t *location) { rbs_ast_directives_use_singleclause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_singleclause_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(type_name == NULL ? Qnil : type_name->base.cached_ruby_value); + rb_gc_register_mark_object(new_name == NULL ? Qnil : new_name->base.cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_directives_use_single_clause(type_name, new_name, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(type_name); - rb_gc_register_mark_object(new_name); - rb_gc_register_mark_object(location); *instance = (rbs_ast_directives_use_singleclause_t) { .base = (rbs_node_t) { @@ -332,12 +574,17 @@ rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(r return instance; } -rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE namespace, VALUE location) { +rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_namespace_t *namespace, rbs_location_t *location) { rbs_ast_directives_use_wildcardclause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_wildcardclause_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(namespace == NULL ? Qnil : namespace->base.cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_directives_use_wildcard_clause(namespace, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(namespace); - rb_gc_register_mark_object(location); *instance = (rbs_ast_directives_use_wildcardclause_t) { .base = (rbs_node_t) { @@ -351,17 +598,22 @@ rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_n return instance; } -rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE new_name, VALUE old_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment) { +rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { rbs_ast_members_alias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_alias_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(new_name); - rb_gc_register_mark_object(old_name); - rb_gc_register_mark_object(kind); - rb_gc_register_mark_object(annotations); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(new_name == NULL ? Qnil : new_name->base.cached_ruby_value); + rb_gc_register_mark_object(old_name == NULL ? Qnil : old_name->base.cached_ruby_value); + rb_gc_register_mark_object(kind == NULL ? Qnil : kind->base.cached_ruby_value); + rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_members_alias(new_name, old_name, kind, annotations, location, comment); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_ast_members_alias_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -378,18 +630,23 @@ rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, V return instance; } -rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility) { +rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility) { rbs_ast_members_attraccessor_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attraccessor_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(type); - rb_gc_register_mark_object(ivar_name); - rb_gc_register_mark_object(kind); - rb_gc_register_mark_object(annotations); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); + rb_gc_register_mark_object(ivar_name == NULL ? Qnil : ivar_name->cached_ruby_value); + rb_gc_register_mark_object(kind == NULL ? Qnil : kind->base.cached_ruby_value); + rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); - rb_gc_register_mark_object(visibility); + rb_gc_register_mark_object(visibility == NULL ? Qnil : visibility->base.cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_members_attr_accessor(name, type, ivar_name, kind, annotations, location, comment, visibility); + + rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_attraccessor_t) { .base = (rbs_node_t) { @@ -409,18 +666,23 @@ rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t return instance; } -rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility) { +rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility) { rbs_ast_members_attrreader_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attrreader_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(type); - rb_gc_register_mark_object(ivar_name); - rb_gc_register_mark_object(kind); - rb_gc_register_mark_object(annotations); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); + rb_gc_register_mark_object(ivar_name == NULL ? Qnil : ivar_name->cached_ruby_value); + rb_gc_register_mark_object(kind == NULL ? Qnil : kind->base.cached_ruby_value); + rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); - rb_gc_register_mark_object(visibility); + rb_gc_register_mark_object(visibility == NULL ? Qnil : visibility->base.cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_members_attr_reader(name, type, ivar_name, kind, annotations, location, comment, visibility); + + rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_attrreader_t) { .base = (rbs_node_t) { @@ -440,18 +702,23 @@ rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *al return instance; } -rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility) { +rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility) { rbs_ast_members_attrwriter_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attrwriter_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(type); - rb_gc_register_mark_object(ivar_name); - rb_gc_register_mark_object(kind); - rb_gc_register_mark_object(annotations); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); + rb_gc_register_mark_object(ivar_name == NULL ? Qnil : ivar_name->cached_ruby_value); + rb_gc_register_mark_object(kind == NULL ? Qnil : kind->base.cached_ruby_value); + rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); - rb_gc_register_mark_object(visibility); + rb_gc_register_mark_object(visibility == NULL ? Qnil : visibility->base.cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_members_attr_writer(name, type, ivar_name, kind, annotations, location, comment, visibility); + + rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_attrwriter_t) { .base = (rbs_node_t) { @@ -471,15 +738,20 @@ rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *al return instance; } -rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment) { +rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { rbs_ast_members_classinstancevariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_classinstancevariable_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(type); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_members_class_instance_variable(name, type, location, comment); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_ast_members_classinstancevariable_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -494,15 +766,20 @@ rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_n return instance; } -rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment) { +rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { rbs_ast_members_classvariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_classvariable_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(type); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_members_class_variable(name, type, location, comment); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_ast_members_classvariable_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -517,16 +794,21 @@ rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator return instance; } -rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment) { +rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { rbs_ast_members_extend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_extend_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(args); - rb_gc_register_mark_object(annotations); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); + rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_members_extend(name, args, annotations, location, comment); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_ast_members_extend_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -542,16 +824,21 @@ rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, return instance; } -rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment) { +rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { rbs_ast_members_include_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_include_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(args); - rb_gc_register_mark_object(annotations); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); + rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_members_include(name, args, annotations, location, comment); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_ast_members_include_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -567,15 +854,20 @@ rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocato return instance; } -rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE type, VALUE location, VALUE comment) { +rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { rbs_ast_members_instancevariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_instancevariable_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(type); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_members_instance_variable(name, type, location, comment); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_ast_members_instancevariable_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -590,18 +882,23 @@ rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_all return instance; } -rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE kind, VALUE overloads, VALUE annotations, VALUE location, VALUE comment, VALUE overloading, VALUE visibility) { +rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_ast_symbol_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, bool overloading, rbs_ast_symbol_t *visibility) { rbs_ast_members_methoddefinition_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_methoddefinition_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(kind); - rb_gc_register_mark_object(overloads); - rb_gc_register_mark_object(annotations); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(kind == NULL ? Qnil : kind->base.cached_ruby_value); + rb_gc_register_mark_object(overloads == NULL ? Qnil : overloads->cached_ruby_value); + rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); - rb_gc_register_mark_object(overloading); - rb_gc_register_mark_object(visibility); + rb_gc_register_mark_object(overloading ? Qtrue : Qfalse); + rb_gc_register_mark_object(visibility == NULL ? Qnil : visibility->base.cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_members_method_definition(name, kind, overloads, annotations, location, comment, overloading, visibility); + + rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_methoddefinition_t) { .base = (rbs_node_t) { @@ -621,12 +918,17 @@ rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_all return instance; } -rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE annotations, VALUE method_type) { +rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, rbs_node_list_t *annotations, rbs_node_t *method_type) { rbs_ast_members_methoddefinition_overload_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_methoddefinition_overload_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_gc_register_mark_object(method_type == NULL ? Qnil : method_type->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_members_method_definition_overload(annotations, method_type); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(annotations); - rb_gc_register_mark_object(method_type); *instance = (rbs_ast_members_methoddefinition_overload_t) { .base = (rbs_node_t) { @@ -640,16 +942,21 @@ rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_ov return instance; } -rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment) { +rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { rbs_ast_members_prepend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_prepend_t); - rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(args); - rb_gc_register_mark_object(annotations); - rb_gc_register_mark_object(location); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); + rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); rb_gc_register_mark_object(comment); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_members_prepend(name, args, annotations, location, comment); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_ast_members_prepend_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -665,11 +972,16 @@ rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocato return instance; } -rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { +rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_ast_members_private_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_private_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_members_private(location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(location); *instance = (rbs_ast_members_private_t) { .base = (rbs_node_t) { @@ -682,11 +994,16 @@ rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocato return instance; } -rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { +rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_ast_members_public_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_public_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_members_public(location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(location); *instance = (rbs_ast_members_public_t) { .base = (rbs_node_t) { @@ -699,16 +1016,39 @@ rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, return instance; } -rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE variance, VALUE upper_bound, VALUE default_type, VALUE unchecked, VALUE location) { +rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, VALUE ruby_value) { + rbs_ast_symbol_t *instance = rbs_allocator_alloc(allocator, rbs_ast_symbol_t); + + // Disable GC for all these Ruby objects. + + + rb_gc_register_mark_object(ruby_value); + + *instance = (rbs_ast_symbol_t) { + .base = (rbs_node_t) { + .cached_ruby_value = ruby_value, + .type = RBS_AST_SYMBOL + }, + }; + + return instance; +} + +rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_ast_symbol_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked, rbs_location_t *location) { rbs_ast_typeparam_t *instance = rbs_allocator_alloc(allocator, rbs_ast_typeparam_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(variance == NULL ? Qnil : variance->base.cached_ruby_value); + rb_gc_register_mark_object(upper_bound == NULL ? Qnil : upper_bound->cached_ruby_value); + rb_gc_register_mark_object(default_type == NULL ? Qnil : default_type->cached_ruby_value); + rb_gc_register_mark_object(unchecked ? Qtrue : Qfalse); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_ast_type_param(name, variance, upper_bound, default_type, unchecked, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(variance); - rb_gc_register_mark_object(upper_bound); - rb_gc_register_mark_object(default_type); - rb_gc_register_mark_object(unchecked); - rb_gc_register_mark_object(location); *instance = (rbs_ast_typeparam_t) { .base = (rbs_node_t) { @@ -726,14 +1066,19 @@ rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, VALUE rub return instance; } -rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type_params, VALUE type, VALUE block, VALUE location) { +rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location) { rbs_methodtype_t *instance = rbs_allocator_alloc(allocator, rbs_methodtype_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(type_params == NULL ? Qnil : type_params->cached_ruby_value); + rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); + rb_gc_register_mark_object(block == NULL ? Qnil : block->base.cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_method_type(type_params, type, block, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(type_params); - rb_gc_register_mark_object(type); - rb_gc_register_mark_object(block); - rb_gc_register_mark_object(location); *instance = (rbs_methodtype_t) { .base = (rbs_node_t) { @@ -749,12 +1094,17 @@ rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, VALUE ruby_valu return instance; } -rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE path, VALUE absolute) { +rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_node_list_t *path, bool absolute) { rbs_namespace_t *instance = rbs_allocator_alloc(allocator, rbs_namespace_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(path == NULL ? Qnil : path->cached_ruby_value); + rb_gc_register_mark_object(absolute ? Qtrue : Qfalse); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_namespace(path, absolute); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(path); - rb_gc_register_mark_object(absolute); *instance = (rbs_namespace_t) { .base = (rbs_node_t) { @@ -768,12 +1118,17 @@ rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, VALUE ruby_value, return instance; } -rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE namespace, VALUE name) { +rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_namespace_t *namespace, rbs_ast_symbol_t *name) { rbs_typename_t *instance = rbs_allocator_alloc(allocator, rbs_typename_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(namespace == NULL ? Qnil : namespace->base.cached_ruby_value); + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_type_name(namespace, name); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(namespace); - rb_gc_register_mark_object(name); *instance = (rbs_typename_t) { .base = (rbs_node_t) { @@ -787,13 +1142,18 @@ rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, VALUE ruby_value, V return instance; } -rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location) { +rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { rbs_types_alias_t *instance = rbs_allocator_alloc(allocator, rbs_types_alias_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_alias(name, args, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(args); - rb_gc_register_mark_object(location); *instance = (rbs_types_alias_t) { .base = (rbs_node_t) { @@ -808,12 +1168,17 @@ rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, VALUE ruby_va return instance; } -rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE todo, VALUE location) { +rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, VALUE todo, rbs_location_t *location) { rbs_types_bases_any_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_any_t); - rb_gc_register_mark_object(ruby_value); + // Disable GC for all these Ruby objects. rb_gc_register_mark_object(todo); - rb_gc_register_mark_object(location); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_bases_any(todo, location); + + rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_bases_any_t) { .base = (rbs_node_t) { @@ -827,11 +1192,16 @@ rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, VALUE return instance; } -rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { +rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_bool_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_bool_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_bases_bool(location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(location); *instance = (rbs_types_bases_bool_t) { .base = (rbs_node_t) { @@ -844,11 +1214,16 @@ rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, VAL return instance; } -rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { +rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_bottom_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_bottom_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_bases_bottom(location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(location); *instance = (rbs_types_bases_bottom_t) { .base = (rbs_node_t) { @@ -861,11 +1236,16 @@ rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, return instance; } -rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { +rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_class_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_class_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_bases_class(location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(location); *instance = (rbs_types_bases_class_t) { .base = (rbs_node_t) { @@ -878,11 +1258,16 @@ rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, V return instance; } -rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { +rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_instance_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_instance_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_bases_instance(location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(location); *instance = (rbs_types_bases_instance_t) { .base = (rbs_node_t) { @@ -895,11 +1280,16 @@ rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *alloca return instance; } -rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { +rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_nil_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_nil_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_bases_nil(location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(location); *instance = (rbs_types_bases_nil_t) { .base = (rbs_node_t) { @@ -912,11 +1302,16 @@ rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, VALUE return instance; } -rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { +rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_self_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_self_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_bases_self(location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(location); *instance = (rbs_types_bases_self_t) { .base = (rbs_node_t) { @@ -929,11 +1324,16 @@ rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, VAL return instance; } -rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { +rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_top_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_top_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_bases_top(location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(location); *instance = (rbs_types_bases_top_t) { .base = (rbs_node_t) { @@ -946,11 +1346,16 @@ rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, VALUE return instance; } -rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE location) { +rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_void_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_void_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_bases_void(location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(location); *instance = (rbs_types_bases_void_t) { .base = (rbs_node_t) { @@ -963,13 +1368,18 @@ rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, VAL return instance; } -rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE required, VALUE self_type) { +rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_node_t *type, bool required, rbs_node_t *self_type) { rbs_types_block_t *instance = rbs_allocator_alloc(allocator, rbs_types_block_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); + rb_gc_register_mark_object(required ? Qtrue : Qfalse); + rb_gc_register_mark_object(self_type == NULL ? Qnil : self_type->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_block(type, required, self_type); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(type); - rb_gc_register_mark_object(required); - rb_gc_register_mark_object(self_type); *instance = (rbs_types_block_t) { .base = (rbs_node_t) { @@ -984,13 +1394,18 @@ rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, VALUE ruby_va return instance; } -rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location) { +rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { rbs_types_classinstance_t *instance = rbs_allocator_alloc(allocator, rbs_types_classinstance_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_class_instance(name, args, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(args); - rb_gc_register_mark_object(location); *instance = (rbs_types_classinstance_t) { .base = (rbs_node_t) { @@ -1005,12 +1420,17 @@ rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocato return instance; } -rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE location) { +rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_location_t *location) { rbs_types_classsingleton_t *instance = rbs_allocator_alloc(allocator, rbs_types_classsingleton_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_class_singleton(name, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(location); *instance = (rbs_types_classsingleton_t) { .base = (rbs_node_t) { @@ -1024,10 +1444,10 @@ rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *alloca return instance; } -rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE required_positionals, VALUE optional_positionals, VALUE rest_positionals, VALUE trailing_positionals, VALUE required_keywords, VALUE optional_keywords, VALUE rest_keywords, VALUE return_type) { +rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, VALUE required_positionals, VALUE optional_positionals, VALUE rest_positionals, VALUE trailing_positionals, VALUE required_keywords, VALUE optional_keywords, VALUE rest_keywords, VALUE return_type) { rbs_types_function_t *instance = rbs_allocator_alloc(allocator, rbs_types_function_t); - rb_gc_register_mark_object(ruby_value); + // Disable GC for all these Ruby objects. rb_gc_register_mark_object(required_positionals); rb_gc_register_mark_object(optional_positionals); rb_gc_register_mark_object(rest_positionals); @@ -1037,6 +1457,11 @@ rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, VALUE r rb_gc_register_mark_object(rest_keywords); rb_gc_register_mark_object(return_type); + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_function(required_positionals, optional_positionals, rest_positionals, trailing_positionals, required_keywords, optional_keywords, rest_keywords, return_type); + + rb_gc_register_mark_object(ruby_value); + *instance = (rbs_types_function_t) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, @@ -1055,13 +1480,18 @@ rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, VALUE r return instance; } -rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE name, VALUE location) { +rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_ast_symbol_t *name, rbs_location_t *location) { rbs_types_function_param_t *instance = rbs_allocator_alloc(allocator, rbs_types_function_param_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_function_param(type, name, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(type); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(location); *instance = (rbs_types_function_param_t) { .base = (rbs_node_t) { @@ -1076,13 +1506,18 @@ rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *alloca return instance; } -rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE args, VALUE location) { +rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { rbs_types_interface_t *instance = rbs_allocator_alloc(allocator, rbs_types_interface_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_interface(name, args, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(args); - rb_gc_register_mark_object(location); *instance = (rbs_types_interface_t) { .base = (rbs_node_t) { @@ -1097,12 +1532,17 @@ rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, VALUE return instance; } -rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE types, VALUE location) { +rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location) { rbs_types_intersection_t *instance = rbs_allocator_alloc(allocator, rbs_types_intersection_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(types == NULL ? Qnil : types->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_intersection(types, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(types); - rb_gc_register_mark_object(location); *instance = (rbs_types_intersection_t) { .base = (rbs_node_t) { @@ -1116,12 +1556,17 @@ rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, return instance; } -rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE literal, VALUE location) { +rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, VALUE literal, rbs_location_t *location) { rbs_types_literal_t *instance = rbs_allocator_alloc(allocator, rbs_types_literal_t); - rb_gc_register_mark_object(ruby_value); + // Disable GC for all these Ruby objects. rb_gc_register_mark_object(literal); - rb_gc_register_mark_object(location); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_literal(literal, location); + + rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_literal_t) { .base = (rbs_node_t) { @@ -1135,12 +1580,17 @@ rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, VALUE rub return instance; } -rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE location) { +rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_location_t *location) { rbs_types_optional_t *instance = rbs_allocator_alloc(allocator, rbs_types_optional_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_optional(type, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(type); - rb_gc_register_mark_object(location); *instance = (rbs_types_optional_t) { .base = (rbs_node_t) { @@ -1154,14 +1604,19 @@ rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, VALUE r return instance; } -rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE type, VALUE block, VALUE location, VALUE self_type) { +rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location, rbs_node_t *self_type) { rbs_types_proc_t *instance = rbs_allocator_alloc(allocator, rbs_types_proc_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); + rb_gc_register_mark_object(block == NULL ? Qnil : block->base.cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + rb_gc_register_mark_object(self_type == NULL ? Qnil : self_type->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_proc(type, block, location, self_type); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(type); - rb_gc_register_mark_object(block); - rb_gc_register_mark_object(location); - rb_gc_register_mark_object(self_type); *instance = (rbs_types_proc_t) { .base = (rbs_node_t) { @@ -1177,12 +1632,17 @@ rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, VALUE ruby_valu return instance; } -rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE all_fields, VALUE location) { +rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, VALUE all_fields, rbs_location_t *location) { rbs_types_record_t *instance = rbs_allocator_alloc(allocator, rbs_types_record_t); - rb_gc_register_mark_object(ruby_value); + // Disable GC for all these Ruby objects. rb_gc_register_mark_object(all_fields); - rb_gc_register_mark_object(location); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_record(all_fields, location); + + rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_record_t) { .base = (rbs_node_t) { @@ -1196,12 +1656,17 @@ rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, VALUE ruby_ return instance; } -rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE types, VALUE location) { +rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location) { rbs_types_tuple_t *instance = rbs_allocator_alloc(allocator, rbs_types_tuple_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(types == NULL ? Qnil : types->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_tuple(types, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(types); - rb_gc_register_mark_object(location); *instance = (rbs_types_tuple_t) { .base = (rbs_node_t) { @@ -1215,12 +1680,17 @@ rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, VALUE ruby_va return instance; } -rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE types, VALUE location) { +rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location) { rbs_types_union_t *instance = rbs_allocator_alloc(allocator, rbs_types_union_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(types == NULL ? Qnil : types->cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_union(types, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(types); - rb_gc_register_mark_object(location); *instance = (rbs_types_union_t) { .base = (rbs_node_t) { @@ -1234,11 +1704,16 @@ rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, VALUE ruby_va return instance; } -rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE return_type) { +rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, rbs_node_t *return_type) { rbs_types_untypedfunction_t *instance = rbs_allocator_alloc(allocator, rbs_types_untypedfunction_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(return_type == NULL ? Qnil : return_type->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_untyped_function(return_type); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(return_type); *instance = (rbs_types_untypedfunction_t) { .base = (rbs_node_t) { @@ -1251,12 +1726,17 @@ rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allo return instance; } -rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE name, VALUE location) { +rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_location_t *location) { rbs_types_variable_t *instance = rbs_allocator_alloc(allocator, rbs_types_variable_t); + // Disable GC for all these Ruby objects. + rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); + rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + VALUE ruby_value = rbs_variable(name, location); + rb_gc_register_mark_object(ruby_value); - rb_gc_register_mark_object(name); - rb_gc_register_mark_object(location); *instance = (rbs_types_variable_t) { .base = (rbs_node_t) { @@ -1270,18 +1750,3 @@ rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, VALUE r return instance; } -rbs_types_zzztmpnotimplemented_t *rbs_types_zzztmpnotimplemented_new(rbs_allocator_t *allocator, VALUE ruby_value) { - rbs_types_zzztmpnotimplemented_t *instance = rbs_allocator_alloc(allocator, rbs_types_zzztmpnotimplemented_t); - - rb_gc_register_mark_object(ruby_value); - - *instance = (rbs_types_zzztmpnotimplemented_t) { - .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, - .type = RBS_TYPES_ZZZTMPNOTIMPLEMENTED - }, - }; - - return instance; -} - diff --git a/src/constants.c b/src/constants.c index dd1e2ee5c..19e12e0a9 100644 --- a/src/constants.c +++ b/src/constants.c @@ -47,6 +47,7 @@ VALUE RBS_AST_Members_MethodDefinition_Overload; VALUE RBS_AST_Members_Prepend; VALUE RBS_AST_Members_Private; VALUE RBS_AST_Members_Public; +VALUE RBS_AST_Symbol; VALUE RBS_AST_TypeParam; VALUE RBS_MethodType; VALUE RBS_Namespace; @@ -121,6 +122,7 @@ void rbs__init_constants(void) { IMPORT_CONSTANT(RBS_AST_Members_Prepend, RBS_AST_Members, "Prepend"); IMPORT_CONSTANT(RBS_AST_Members_Private, RBS_AST_Members, "Private"); IMPORT_CONSTANT(RBS_AST_Members_Public, RBS_AST_Members, "Public"); + IMPORT_CONSTANT(RBS_AST_Symbol, RBS_AST, "Symbol"); IMPORT_CONSTANT(RBS_AST_TypeParam, RBS_AST, "TypeParam"); IMPORT_CONSTANT(RBS_MethodType, RBS, "MethodType"); IMPORT_CONSTANT(RBS_Namespace, RBS, "Namespace"); diff --git a/src/rbs_location.c b/src/rbs_location.c new file mode 100644 index 000000000..0376fe4d1 --- /dev/null +++ b/src/rbs_location.c @@ -0,0 +1,27 @@ +#include "rbs/rbs_location.h" +#include "location.h" + +// A helper for getting the "old style" `rbs_loc` from the new `rbs_location_t` struct. +static rbs_loc *get_rbs_location(const rbs_location_t *loc) { + return rbs_check_location(loc->cached_ruby_value); +} + +rbs_location_t *rbs_location_pp(VALUE buffer, const position *start_pos, const position *end_pos) { + range rg = { *start_pos, *end_pos }; + rg.start = *start_pos; + rg.end = *end_pos; + + return rbs_location_new(buffer, rg); +} + +void rbs_loc_alloc_children(rbs_location_t *loc, int size) { + rbs_loc_legacy_alloc_children(get_rbs_location(loc), size); +} + +void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, range r) { + rbs_loc_legacy_add_required_child(get_rbs_location(loc), name, r); +} + +void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r) { + rbs_loc_legacy_add_optional_child(get_rbs_location(loc), name, r); +} diff --git a/src/ruby_objs.c b/src/ruby_objs.c index fd9df5429..870723be1 100644 --- a/src/ruby_objs.c +++ b/src/ruby_objs.c @@ -17,10 +17,10 @@ rb_class_new_instance(argc, argv, receiver) #endif -VALUE rbs_ast_annotation(VALUE string, VALUE location) { +VALUE rbs_ast_annotation(VALUE string, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("string")), string); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_AST_Annotation, @@ -29,10 +29,10 @@ VALUE rbs_ast_annotation(VALUE string, VALUE location) { ); } -VALUE rbs_ast_comment(VALUE string, VALUE location) { +VALUE rbs_ast_comment(VALUE string, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("string")), string); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_AST_Comment, @@ -41,14 +41,14 @@ VALUE rbs_ast_comment(VALUE string, VALUE location) { ); } -VALUE rbs_ast_decl_class(VALUE name, VALUE type_params, VALUE super_class, VALUE members, VALUE annotations, VALUE location, VALUE comment) { +VALUE rbs_ast_decl_class(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_params")), type_params); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("super_class")), super_class); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("members")), members); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_params")), type_params == NULL ? Qnil : type_params->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("super_class")), super_class == NULL ? Qnil : super_class->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("members")), members == NULL ? Qnil : members->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); return CLASS_NEW_INSTANCE( @@ -58,11 +58,11 @@ VALUE rbs_ast_decl_class(VALUE name, VALUE type_params, VALUE super_class, VALUE ); } -VALUE rbs_ast_decl_class_super(VALUE name, VALUE args, VALUE location) { +VALUE rbs_ast_decl_class_super(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_AST_Declarations_Class_Super, @@ -71,11 +71,11 @@ VALUE rbs_ast_decl_class_super(VALUE name, VALUE args, VALUE location) { ); } -VALUE rbs_ast_decl_class_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment) { +VALUE rbs_ast_decl_class_alias(rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("new_name")), new_name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("old_name")), old_name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("new_name")), new_name == NULL ? Qnil : new_name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("old_name")), old_name == NULL ? Qnil : old_name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); return CLASS_NEW_INSTANCE( @@ -85,11 +85,11 @@ VALUE rbs_ast_decl_class_alias(VALUE new_name, VALUE old_name, VALUE location, V ); } -VALUE rbs_ast_decl_constant(VALUE name, VALUE type, VALUE location, VALUE comment) { +VALUE rbs_ast_decl_constant(rbs_typename_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); return CLASS_NEW_INSTANCE( @@ -99,11 +99,11 @@ VALUE rbs_ast_decl_constant(VALUE name, VALUE type, VALUE location, VALUE commen ); } -VALUE rbs_ast_decl_global(VALUE name, VALUE type, VALUE location, VALUE comment) { +VALUE rbs_ast_decl_global(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); return CLASS_NEW_INSTANCE( @@ -113,13 +113,13 @@ VALUE rbs_ast_decl_global(VALUE name, VALUE type, VALUE location, VALUE comment) ); } -VALUE rbs_ast_decl_interface(VALUE name, VALUE type_params, VALUE members, VALUE annotations, VALUE location, VALUE comment) { +VALUE rbs_ast_decl_interface(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_params")), type_params); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("members")), members); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_params")), type_params == NULL ? Qnil : type_params->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("members")), members == NULL ? Qnil : members->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); return CLASS_NEW_INSTANCE( @@ -129,14 +129,14 @@ VALUE rbs_ast_decl_interface(VALUE name, VALUE type_params, VALUE members, VALUE ); } -VALUE rbs_ast_decl_module(VALUE name, VALUE type_params, VALUE self_types, VALUE members, VALUE annotations, VALUE location, VALUE comment) { +VALUE rbs_ast_decl_module(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_params")), type_params); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("self_types")), self_types); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("members")), members); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_params")), type_params == NULL ? Qnil : type_params->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("self_types")), self_types == NULL ? Qnil : self_types->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("members")), members == NULL ? Qnil : members->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); return CLASS_NEW_INSTANCE( @@ -146,11 +146,11 @@ VALUE rbs_ast_decl_module(VALUE name, VALUE type_params, VALUE self_types, VALUE ); } -VALUE rbs_ast_decl_module_self(VALUE name, VALUE args, VALUE location) { +VALUE rbs_ast_decl_module_self(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_AST_Declarations_Module_Self, @@ -159,11 +159,11 @@ VALUE rbs_ast_decl_module_self(VALUE name, VALUE args, VALUE location) { ); } -VALUE rbs_ast_decl_module_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment) { +VALUE rbs_ast_decl_module_alias(rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("new_name")), new_name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("old_name")), old_name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("new_name")), new_name == NULL ? Qnil : new_name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("old_name")), old_name == NULL ? Qnil : old_name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); return CLASS_NEW_INSTANCE( @@ -173,13 +173,13 @@ VALUE rbs_ast_decl_module_alias(VALUE new_name, VALUE old_name, VALUE location, ); } -VALUE rbs_ast_decl_type_alias(VALUE name, VALUE type_params, VALUE type, VALUE annotations, VALUE location, VALUE comment) { +VALUE rbs_ast_decl_type_alias(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_params")), type_params); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_params")), type_params == NULL ? Qnil : type_params->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); return CLASS_NEW_INSTANCE( @@ -189,10 +189,10 @@ VALUE rbs_ast_decl_type_alias(VALUE name, VALUE type_params, VALUE type, VALUE a ); } -VALUE rbs_ast_directives_use(VALUE clauses, VALUE location) { +VALUE rbs_ast_directives_use(rbs_node_list_t *clauses, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("clauses")), clauses); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("clauses")), clauses == NULL ? Qnil : clauses->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_AST_Directives_Use, @@ -201,11 +201,11 @@ VALUE rbs_ast_directives_use(VALUE clauses, VALUE location) { ); } -VALUE rbs_ast_directives_use_single_clause(VALUE type_name, VALUE new_name, VALUE location) { +VALUE rbs_ast_directives_use_single_clause(rbs_typename_t *type_name, rbs_ast_symbol_t *new_name, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_name")), type_name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("new_name")), new_name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_name")), type_name == NULL ? Qnil : type_name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("new_name")), new_name == NULL ? Qnil : new_name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_AST_Directives_Use_SingleClause, @@ -214,10 +214,10 @@ VALUE rbs_ast_directives_use_single_clause(VALUE type_name, VALUE new_name, VALU ); } -VALUE rbs_ast_directives_use_wildcard_clause(VALUE namespace, VALUE location) { +VALUE rbs_ast_directives_use_wildcard_clause(rbs_namespace_t *namespace, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("namespace")), namespace); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("namespace")), namespace == NULL ? Qnil : namespace->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_AST_Directives_Use_WildcardClause, @@ -226,13 +226,13 @@ VALUE rbs_ast_directives_use_wildcard_clause(VALUE namespace, VALUE location) { ); } -VALUE rbs_ast_members_alias(VALUE new_name, VALUE old_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment) { +VALUE rbs_ast_members_alias(rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("new_name")), new_name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("old_name")), old_name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("kind")), kind); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("new_name")), new_name == NULL ? Qnil : new_name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("old_name")), old_name == NULL ? Qnil : old_name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("kind")), kind == NULL ? Qnil : kind->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); return CLASS_NEW_INSTANCE( @@ -242,16 +242,16 @@ VALUE rbs_ast_members_alias(VALUE new_name, VALUE old_name, VALUE kind, VALUE an ); } -VALUE rbs_ast_members_attr_accessor(VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility) { +VALUE rbs_ast_members_attr_accessor(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("ivar_name")), ivar_name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("kind")), kind); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("ivar_name")), ivar_name == NULL ? Qnil : ivar_name->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("kind")), kind == NULL ? Qnil : kind->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("visibility")), visibility); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("visibility")), visibility == NULL ? Qnil : visibility->base.cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_AST_Members_AttrAccessor, @@ -260,16 +260,16 @@ VALUE rbs_ast_members_attr_accessor(VALUE name, VALUE type, VALUE ivar_name, VAL ); } -VALUE rbs_ast_members_attr_reader(VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility) { +VALUE rbs_ast_members_attr_reader(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("ivar_name")), ivar_name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("kind")), kind); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("ivar_name")), ivar_name == NULL ? Qnil : ivar_name->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("kind")), kind == NULL ? Qnil : kind->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("visibility")), visibility); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("visibility")), visibility == NULL ? Qnil : visibility->base.cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_AST_Members_AttrReader, @@ -278,16 +278,16 @@ VALUE rbs_ast_members_attr_reader(VALUE name, VALUE type, VALUE ivar_name, VALUE ); } -VALUE rbs_ast_members_attr_writer(VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility) { +VALUE rbs_ast_members_attr_writer(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("ivar_name")), ivar_name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("kind")), kind); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("ivar_name")), ivar_name == NULL ? Qnil : ivar_name->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("kind")), kind == NULL ? Qnil : kind->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("visibility")), visibility); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("visibility")), visibility == NULL ? Qnil : visibility->base.cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_AST_Members_AttrWriter, @@ -296,11 +296,11 @@ VALUE rbs_ast_members_attr_writer(VALUE name, VALUE type, VALUE ivar_name, VALUE ); } -VALUE rbs_ast_members_class_instance_variable(VALUE name, VALUE type, VALUE location, VALUE comment) { +VALUE rbs_ast_members_class_instance_variable(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); return CLASS_NEW_INSTANCE( @@ -310,11 +310,11 @@ VALUE rbs_ast_members_class_instance_variable(VALUE name, VALUE type, VALUE loca ); } -VALUE rbs_ast_members_class_variable(VALUE name, VALUE type, VALUE location, VALUE comment) { +VALUE rbs_ast_members_class_variable(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); return CLASS_NEW_INSTANCE( @@ -324,12 +324,12 @@ VALUE rbs_ast_members_class_variable(VALUE name, VALUE type, VALUE location, VAL ); } -VALUE rbs_ast_members_extend(VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment) { +VALUE rbs_ast_members_extend(rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); return CLASS_NEW_INSTANCE( @@ -339,12 +339,12 @@ VALUE rbs_ast_members_extend(VALUE name, VALUE args, VALUE annotations, VALUE lo ); } -VALUE rbs_ast_members_include(VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment) { +VALUE rbs_ast_members_include(rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); return CLASS_NEW_INSTANCE( @@ -354,11 +354,11 @@ VALUE rbs_ast_members_include(VALUE name, VALUE args, VALUE annotations, VALUE l ); } -VALUE rbs_ast_members_instance_variable(VALUE name, VALUE type, VALUE location, VALUE comment) { +VALUE rbs_ast_members_instance_variable(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); return CLASS_NEW_INSTANCE( @@ -368,16 +368,16 @@ VALUE rbs_ast_members_instance_variable(VALUE name, VALUE type, VALUE location, ); } -VALUE rbs_ast_members_method_definition(VALUE name, VALUE kind, VALUE overloads, VALUE annotations, VALUE location, VALUE comment, VALUE overloading, VALUE visibility) { +VALUE rbs_ast_members_method_definition(rbs_ast_symbol_t *name, rbs_ast_symbol_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, bool overloading, rbs_ast_symbol_t *visibility) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("kind")), kind); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("overloads")), overloads); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("kind")), kind == NULL ? Qnil : kind->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("overloads")), overloads == NULL ? Qnil : overloads->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("overloading")), overloading); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("visibility")), visibility); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("overloading")), overloading ? Qtrue : Qfalse); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("visibility")), visibility == NULL ? Qnil : visibility->base.cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_AST_Members_MethodDefinition, @@ -386,10 +386,10 @@ VALUE rbs_ast_members_method_definition(VALUE name, VALUE kind, VALUE overloads, ); } -VALUE rbs_ast_members_method_definition_overload(VALUE annotations, VALUE method_type) { +VALUE rbs_ast_members_method_definition_overload(rbs_node_list_t *annotations, rbs_node_t *method_type) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("method_type")), method_type); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("method_type")), method_type == NULL ? Qnil : method_type->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_AST_Members_MethodDefinition_Overload, @@ -398,12 +398,12 @@ VALUE rbs_ast_members_method_definition_overload(VALUE annotations, VALUE method ); } -VALUE rbs_ast_members_prepend(VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment) { +VALUE rbs_ast_members_prepend(rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); return CLASS_NEW_INSTANCE( @@ -413,9 +413,9 @@ VALUE rbs_ast_members_prepend(VALUE name, VALUE args, VALUE annotations, VALUE l ); } -VALUE rbs_ast_members_private(VALUE location) { +VALUE rbs_ast_members_private(rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_AST_Members_Private, @@ -424,9 +424,9 @@ VALUE rbs_ast_members_private(VALUE location) { ); } -VALUE rbs_ast_members_public(VALUE location) { +VALUE rbs_ast_members_public(rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_AST_Members_Public, @@ -435,14 +435,24 @@ VALUE rbs_ast_members_public(VALUE location) { ); } -VALUE rbs_ast_type_param(VALUE name, VALUE variance, VALUE upper_bound, VALUE default_type, VALUE unchecked, VALUE location) { +VALUE rbs_ast_symbol() { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("variance")), variance); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("upper_bound")), upper_bound); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("default_type")), default_type); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("unchecked")), unchecked); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + + return CLASS_NEW_INSTANCE( + RBS_AST_Symbol, + 1, + &_init_kwargs + ); +} + +VALUE rbs_ast_type_param(rbs_ast_symbol_t *name, rbs_ast_symbol_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked, rbs_location_t *location) { + VALUE _init_kwargs = rb_hash_new(); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("variance")), variance == NULL ? Qnil : variance->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("upper_bound")), upper_bound == NULL ? Qnil : upper_bound->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("default_type")), default_type == NULL ? Qnil : default_type->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("unchecked")), unchecked ? Qtrue : Qfalse); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_AST_TypeParam, @@ -451,12 +461,12 @@ VALUE rbs_ast_type_param(VALUE name, VALUE variance, VALUE upper_bound, VALUE de ); } -VALUE rbs_method_type(VALUE type_params, VALUE type, VALUE block, VALUE location) { +VALUE rbs_method_type(rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_params")), type_params); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("block")), block); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_params")), type_params == NULL ? Qnil : type_params->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("block")), block == NULL ? Qnil : block->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_MethodType, @@ -465,10 +475,10 @@ VALUE rbs_method_type(VALUE type_params, VALUE type, VALUE block, VALUE location ); } -VALUE rbs_namespace(VALUE path, VALUE absolute) { +VALUE rbs_namespace(rbs_node_list_t *path, bool absolute) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("path")), path); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("absolute")), absolute); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("path")), path == NULL ? Qnil : path->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("absolute")), absolute ? Qtrue : Qfalse); return CLASS_NEW_INSTANCE( RBS_Namespace, @@ -477,10 +487,10 @@ VALUE rbs_namespace(VALUE path, VALUE absolute) { ); } -VALUE rbs_type_name(VALUE namespace, VALUE name) { +VALUE rbs_type_name(rbs_namespace_t *namespace, rbs_ast_symbol_t *name) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("namespace")), namespace); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("namespace")), namespace == NULL ? Qnil : namespace->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_TypeName, @@ -489,11 +499,11 @@ VALUE rbs_type_name(VALUE namespace, VALUE name) { ); } -VALUE rbs_alias(VALUE name, VALUE args, VALUE location) { +VALUE rbs_alias(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Alias, @@ -502,10 +512,10 @@ VALUE rbs_alias(VALUE name, VALUE args, VALUE location) { ); } -VALUE rbs_bases_any(VALUE todo, VALUE location) { +VALUE rbs_bases_any(VALUE todo, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("todo")), todo); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Bases_Any, @@ -514,9 +524,9 @@ VALUE rbs_bases_any(VALUE todo, VALUE location) { ); } -VALUE rbs_bases_bool(VALUE location) { +VALUE rbs_bases_bool(rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Bases_Bool, @@ -525,9 +535,9 @@ VALUE rbs_bases_bool(VALUE location) { ); } -VALUE rbs_bases_bottom(VALUE location) { +VALUE rbs_bases_bottom(rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Bases_Bottom, @@ -536,9 +546,9 @@ VALUE rbs_bases_bottom(VALUE location) { ); } -VALUE rbs_bases_class(VALUE location) { +VALUE rbs_bases_class(rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Bases_Class, @@ -547,9 +557,9 @@ VALUE rbs_bases_class(VALUE location) { ); } -VALUE rbs_bases_instance(VALUE location) { +VALUE rbs_bases_instance(rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Bases_Instance, @@ -558,9 +568,9 @@ VALUE rbs_bases_instance(VALUE location) { ); } -VALUE rbs_bases_nil(VALUE location) { +VALUE rbs_bases_nil(rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Bases_Nil, @@ -569,9 +579,9 @@ VALUE rbs_bases_nil(VALUE location) { ); } -VALUE rbs_bases_self(VALUE location) { +VALUE rbs_bases_self(rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Bases_Self, @@ -580,9 +590,9 @@ VALUE rbs_bases_self(VALUE location) { ); } -VALUE rbs_bases_top(VALUE location) { +VALUE rbs_bases_top(rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Bases_Top, @@ -591,9 +601,9 @@ VALUE rbs_bases_top(VALUE location) { ); } -VALUE rbs_bases_void(VALUE location) { +VALUE rbs_bases_void(rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Bases_Void, @@ -602,11 +612,11 @@ VALUE rbs_bases_void(VALUE location) { ); } -VALUE rbs_block(VALUE type, VALUE required, VALUE self_type) { +VALUE rbs_block(rbs_node_t *type, bool required, rbs_node_t *self_type) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("required")), required); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("self_type")), self_type); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("required")), required ? Qtrue : Qfalse); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("self_type")), self_type == NULL ? Qnil : self_type->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Block, @@ -615,11 +625,11 @@ VALUE rbs_block(VALUE type, VALUE required, VALUE self_type) { ); } -VALUE rbs_class_instance(VALUE name, VALUE args, VALUE location) { +VALUE rbs_class_instance(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_ClassInstance, @@ -628,10 +638,10 @@ VALUE rbs_class_instance(VALUE name, VALUE args, VALUE location) { ); } -VALUE rbs_class_singleton(VALUE name, VALUE location) { +VALUE rbs_class_singleton(rbs_typename_t *name, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_ClassSingleton, @@ -658,11 +668,11 @@ VALUE rbs_function(VALUE required_positionals, VALUE optional_positionals, VALUE ); } -VALUE rbs_function_param(VALUE type, VALUE name, VALUE location) { +VALUE rbs_function_param(rbs_node_t *type, rbs_ast_symbol_t *name, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Function_Param, @@ -671,11 +681,11 @@ VALUE rbs_function_param(VALUE type, VALUE name, VALUE location) { ); } -VALUE rbs_interface(VALUE name, VALUE args, VALUE location) { +VALUE rbs_interface(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Interface, @@ -684,10 +694,10 @@ VALUE rbs_interface(VALUE name, VALUE args, VALUE location) { ); } -VALUE rbs_intersection(VALUE types, VALUE location) { +VALUE rbs_intersection(rbs_node_list_t *types, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("types")), types); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("types")), types == NULL ? Qnil : types->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Intersection, @@ -696,10 +706,10 @@ VALUE rbs_intersection(VALUE types, VALUE location) { ); } -VALUE rbs_literal(VALUE literal, VALUE location) { +VALUE rbs_literal(VALUE literal, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("literal")), literal); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Literal, @@ -708,10 +718,10 @@ VALUE rbs_literal(VALUE literal, VALUE location) { ); } -VALUE rbs_optional(VALUE type, VALUE location) { +VALUE rbs_optional(rbs_node_t *type, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Optional, @@ -720,12 +730,12 @@ VALUE rbs_optional(VALUE type, VALUE location) { ); } -VALUE rbs_proc(VALUE type, VALUE block, VALUE location, VALUE self_type) { +VALUE rbs_proc(rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location, rbs_node_t *self_type) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("block")), block); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("self_type")), self_type); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("block")), block == NULL ? Qnil : block->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("self_type")), self_type == NULL ? Qnil : self_type->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Proc, @@ -734,10 +744,10 @@ VALUE rbs_proc(VALUE type, VALUE block, VALUE location, VALUE self_type) { ); } -VALUE rbs_record(VALUE all_fields, VALUE location) { +VALUE rbs_record(VALUE all_fields, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("all_fields")), all_fields); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Record, @@ -746,10 +756,10 @@ VALUE rbs_record(VALUE all_fields, VALUE location) { ); } -VALUE rbs_tuple(VALUE types, VALUE location) { +VALUE rbs_tuple(rbs_node_list_t *types, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("types")), types); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("types")), types == NULL ? Qnil : types->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Tuple, @@ -758,10 +768,10 @@ VALUE rbs_tuple(VALUE types, VALUE location) { ); } -VALUE rbs_union(VALUE types, VALUE location) { +VALUE rbs_union(rbs_node_list_t *types, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("types")), types); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("types")), types == NULL ? Qnil : types->cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Union, @@ -770,9 +780,9 @@ VALUE rbs_union(VALUE types, VALUE location) { ); } -VALUE rbs_untyped_function(VALUE return_type) { +VALUE rbs_untyped_function(rbs_node_t *return_type) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("return_type")), return_type); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("return_type")), return_type == NULL ? Qnil : return_type->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_UntypedFunction, @@ -781,10 +791,10 @@ VALUE rbs_untyped_function(VALUE return_type) { ); } -VALUE rbs_variable(VALUE name, VALUE location) { +VALUE rbs_variable(rbs_ast_symbol_t *name, rbs_location_t *location) { VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); return CLASS_NEW_INSTANCE( RBS_Types_Variable, diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index ccab436d7..047f4feeb 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -1,5 +1,31 @@ #include "ast_translation.h" +#include "rbs/constants.h" + +#include + + +VALUE rbs_node_list_to_ruby_array(rbs_node_list_t *list) { + return list->cached_ruby_value; +} + +VALUE rbs_hash_to_ruby_hash(rbs_hash_t *hash) { + return hash->cached_ruby_value; +} + +VALUE rbs_loc_to_ruby_location(rbs_location_t *loc) { + return loc->cached_ruby_value; +} + +#ifdef RB_PASS_KEYWORDS + // Ruby 2.7 or later + #define CLASS_NEW_INSTANCE(klass, argc, argv)\ + rb_class_new_instance_kw(argc, argv, klass, RB_PASS_KEYWORDS) +#else + // Ruby 2.6 + #define CLASS_NEW_INSTANCE(receiver, argc, argv)\ + rb_class_new_instance(argc, argv, receiver) +#endif const char* get_class_name(VALUE o) { VALUE klass = rb_class_of(o); // Get the class of the object @@ -9,15 +35,7 @@ const char* get_class_name(VALUE o) { } VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { - if (instance == NULL) { - fprintf(stderr, "Tried to call rbs_struct_to_ruby_value(NULL)\n"); - exit(1); - } - - if (instance->type == RBS_TYPES_ZZZTMPNOTIMPLEMENTED) { - // Special case: skip assertions/translation below. - return instance->cached_ruby_value; - } + if (instance == NULL) return Qnil; VALUE ruby_value = instance->cached_ruby_value; @@ -35,10 +53,51 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { fprintf(stderr, "Expected class name: <%= node.ruby_full_name %>, got %s\n", class_name); exit(1); } - break; + <%- + rbs_struct_to_ruby_value_shitlist = [ + "RBS::AST::Bool", + "RBS::AST::Symbol", + "RBS::AST::Declarations::Nodes", + "RBS::AST::Directives::Nodes", + ] + + if rbs_struct_to_ruby_value_shitlist.include?(node.ruby_full_name) -%> + return instance->cached_ruby_value; + <%- else -%> + <%- if node.fields.any? -%> <%# This prevents "warning: unused variable 'node'" %> + <%= node.c_type_name %> *node = (<%= node.c_type_name %> *)instance; + <%- end -%> + + VALUE h = rb_hash_new(); + <%- node.fields.each do |field| -%> + <%- case field.c_type -%> + <%- when "VALUE" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), node-><%= field.name %>); + <%- when "rbs_node_list" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_node_list_to_ruby_array(node-><%= field.name %>)); + <%- when "rbs_hash" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_hash_to_ruby_hash(node-><%= field.name %>)); + <%- when "rbs_location" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_loc_to_ruby_location(node-><%= field.name %>)); + <%- when "comment" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), node-><%= field.name %>->string); + <%- when "bool" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), node-><%= field.name %> ? Qtrue : Qfalse); + <%- else -%> + <%- unless field.ast_node? -%> + #warning unexpected type <%= field.c_type -%> + <%- end -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value((rbs_node_t *) node-><%= field.name %>)); // <%= field.c_type %> + <%- end -%> + <%- end -%> + + return CLASS_NEW_INSTANCE( + <%= node.c_constant_name %>, + 1, + &h + ); + <%- end -%> } <%- end -%> } - - return instance->cached_ruby_value; } diff --git a/templates/ext/rbs_extension/ast_translation.h.erb b/templates/ext/rbs_extension/ast_translation.h.erb index e759d9396..a9a92921a 100644 --- a/templates/ext/rbs_extension/ast_translation.h.erb +++ b/templates/ext/rbs_extension/ast_translation.h.erb @@ -4,6 +4,9 @@ #include "ruby.h" #include "rbs/ast.h" +VALUE rbs_node_list_to_ruby_array(rbs_node_list_t *list); +VALUE rbs_hash_to_ruby_hash(rbs_hash_t *hash); +VALUE rbs_loc_to_ruby_location(rbs_location_t *loc); VALUE rbs_struct_to_ruby_value(rbs_node_t *instance); #endif diff --git a/templates/include/rbs/ast.h.erb b/templates/include/rbs/ast.h.erb index 6cf7e054e..86abbb92c 100644 --- a/templates/include/rbs/ast.h.erb +++ b/templates/include/rbs/ast.h.erb @@ -3,6 +3,7 @@ #include "ruby.h" #include "rbs/util/rbs_allocator.h" +#include "rbs_location.h" enum rbs_node_type { <%- nodes.each_with_index do |node, index| -%> @@ -15,12 +16,57 @@ typedef struct rbs_node { enum rbs_node_type type; } rbs_node_t; +/* rbs_node_list_node */ + +typedef struct rbs_node_list_node { + rbs_node_t *node; + struct rbs_node_list_node *next; +} rbs_node_list_node_t; + +typedef struct rbs_node_list { + rbs_allocator_t *allocator; + rbs_node_list_node_t *head; + rbs_node_list_node_t *tail; + size_t length; + VALUE cached_ruby_value; +} rbs_node_list_t; + +rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *); + +void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node); + +/* rbs_hash */ + +typedef struct rbs_hash_node { + rbs_node_t *key; + rbs_node_t *value; + struct rbs_hash_node *next; +} rbs_hash_node_t; + +typedef struct rbs_hash { + rbs_allocator_t *allocator; + rbs_hash_node_t *head; + rbs_hash_node_t *tail; + size_t length; + VALUE cached_ruby_value; +} rbs_hash_t; + +rbs_hash_t* rbs_hash_new(rbs_allocator_t *); + +void rbs_hash_set(rbs_hash_t *hash, rbs_node_t *key, rbs_node_t *value); + +rbs_hash_node_t* rbs_hash_find(rbs_hash_t *hash, rbs_node_t *key); + +rbs_node_t* rbs_hash_get(rbs_hash_t *hash, rbs_node_t *key); + +/* rbs_ast_node */ + <%- nodes.each do |node| -%> -typedef struct { +typedef struct <%= node.c_base_name %> { rbs_node_t base; <%- node.fields.each do |field| -%> - VALUE <%= field.name %>; + <%= field.stored_field_decl %>; <%- end -%> } <%= node.c_type_name %>; @@ -30,6 +76,4 @@ typedef struct { <%= node.c_type_name %> *<%= node.c_constructor_function_name %>(<%= node.constructor_params.map(&:parameter_decl).join(", ") %>); <%- end -%> -VALUE rbs_struct_to_ruby_value(rbs_node_t *instance); - #endif diff --git a/templates/include/rbs/ruby_objs.h.erb b/templates/include/rbs/ruby_objs.h.erb index 88fd6fa55..499ba4db8 100644 --- a/templates/include/rbs/ruby_objs.h.erb +++ b/templates/include/rbs/ruby_objs.h.erb @@ -4,7 +4,7 @@ #include "ruby.h" <%- nodes.filter(&:expose_to_ruby?).each do |node| -%> -VALUE <%= node.c_function_name %>(<%= node.fields.map { |field| "#{field.c_type} #{field.name}" }.join(", ") %>); +VALUE <%= node.c_function_name %>(<%= node.fields.map(&:parameter_decl).join(", ") %>); <%- end -%> #endif diff --git a/templates/src/ast.c.erb b/templates/src/ast.c.erb index 365243c66..3caebdbc1 100644 --- a/templates/src/ast.c.erb +++ b/templates/src/ast.c.erb @@ -1,15 +1,138 @@ #include "rbs/ast.h" -#include + +#include "rbs/ruby_objs.h" + +/* rbs_node_list */ + +rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *allocator) { + rbs_node_list_t *list = rbs_allocator_alloc(allocator, rbs_node_list_t); + *list = (rbs_node_list_t) { + .allocator = allocator, + .head = NULL, + .tail = NULL, + .length = 0, + .cached_ruby_value = rb_ary_new(), + }; + + rb_gc_register_mark_object(list->cached_ruby_value); + + return list; +} + +void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node) { + rb_gc_register_mark_object(node->cached_ruby_value); + + rbs_node_list_node_t *new_node = rbs_allocator_alloc(list->allocator, rbs_node_list_node_t); + *new_node = (rbs_node_list_node_t) { + .node = node, + .next = NULL, + }; + + if (list->tail == NULL) { + list->head = new_node; + list->tail = new_node; + } else { + list->tail->next = new_node; + list->tail = new_node; + } + list->length++; + + rb_ary_push(list->cached_ruby_value, node->cached_ruby_value); +} + +/* rbs_hash */ + +rbs_hash_t* rbs_hash_new(rbs_allocator_t *allocator) { + rbs_hash_t *hash = rbs_allocator_alloc(allocator, rbs_hash_t); + *hash = (rbs_hash_t) { + .allocator = allocator, + .head = NULL, + .tail = NULL, + .length = 0, + .cached_ruby_value = rb_hash_new(), + }; + + rb_gc_register_mark_object(hash->cached_ruby_value); + + return hash; +} + +bool rbs_node_equal(rbs_node_t *lhs, rbs_node_t *rhs) { + if (lhs == rhs) return true; + if (lhs->type != rhs->type) return false; + + switch (lhs->type) { + case RBS_AST_BOOL: + return ((rbs_ast_bool_t *)lhs)->value == ((rbs_ast_bool_t *) rhs)->value; + default: + return rb_equal(lhs->cached_ruby_value, rhs->cached_ruby_value); + } +} + +rbs_hash_node_t* rbs_hash_find(rbs_hash_t *hash, rbs_node_t *key) { + rbs_hash_node_t *current = hash->head; + + while (current != NULL) { + if (rbs_node_equal(key, current->key)) { + return current; + } + current = current->next; + } + + return NULL; +} + +void rbs_hash_set(rbs_hash_t *hash, rbs_node_t *key, rbs_node_t *value) { + rb_gc_register_mark_object(key->cached_ruby_value); + rb_gc_register_mark_object(value->cached_ruby_value); + + rbs_hash_node_t *existing_node = rbs_hash_find(hash, key); + if (existing_node != NULL) { + existing_node->value = value; + return; + } + + rb_hash_aset(hash->cached_ruby_value, key->cached_ruby_value, value->cached_ruby_value); + + rbs_hash_node_t *new_node = rbs_allocator_alloc(hash->allocator, rbs_hash_node_t); + new_node->key = key; + new_node->value = value; + new_node->next = NULL; + + if (hash->tail == NULL) { + hash->head = new_node; + hash->tail = new_node; + } else { + hash->tail->next = new_node; + hash->tail = new_node; + } +} + +rbs_node_t* rbs_hash_get(rbs_hash_t *hash, rbs_node_t *key) { + rbs_hash_node_t *node = rbs_hash_find(hash, key); + return node ? node->value : NULL; +} <%- nodes.each do |node| -%> <%= node.c_type_name %> *<%= node.c_constructor_function_name %>(<%= node.constructor_params.map(&:parameter_decl).join(", ") %>) { <%= node.c_type_name %> *instance = rbs_allocator_alloc(allocator, <%= node.c_type_name %>); - rb_gc_register_mark_object(ruby_value); + // Disable GC for all these Ruby objects. <%- node.fields.each do |field| -%> - rb_gc_register_mark_object(<%= field.name %>); + rb_gc_register_mark_object(<%= field.cached_ruby_value_expr %>); <%- end -%> + <%- if node.builds_ruby_object_internally? -%> + // Generate our own Ruby VALUE here, rather than accepting it from a parameter. + <%- if node.c_type_name == "rbs_ast_bool_t" -%> + VALUE ruby_value = value ? Qtrue : Qfalse; + <%- else -%> + VALUE ruby_value = <%= node.c_function_name %>(<%= node.fields.map(&:name).join(", ") %>); + <%- end -%> + <%- end -%> + + rb_gc_register_mark_object(ruby_value); + *instance = (<%= node.c_type_name %>) { .base = (rbs_node_t) { .cached_ruby_value = ruby_value, diff --git a/templates/src/ruby_objs.c.erb b/templates/src/ruby_objs.c.erb index dbbb716b2..a6e5f4fc4 100644 --- a/templates/src/ruby_objs.c.erb +++ b/templates/src/ruby_objs.c.erb @@ -11,10 +11,10 @@ #endif <%- nodes.filter(&:expose_to_ruby?).each do |node| -%> -VALUE <%= node.c_function_name %>(<%= node.fields.map { |field| "#{field.c_type} #{field.name}" }.join(", ") %>) { +VALUE <%= node.c_function_name %>(<%= node.fields.map(&:parameter_decl).join(", ") %>) { VALUE _init_kwargs = rb_hash_new(); <%- node.fields.each do |field| -%> - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("<%= field.name %>")), <%= field.name %>); + rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("<%= field.name %>")), <%= field.cached_ruby_value_expr %>); <%- end -%> return CLASS_NEW_INSTANCE( diff --git a/templates/template.rb b/templates/template.rb index 937c0e29b..c31c1b36d 100644 --- a/templates/template.rb +++ b/templates/template.rb @@ -25,8 +25,8 @@ def parameter_decl "#{@c_type} #{c_name}" when "rbs_string" "rbs_string_t #{c_name}" - when ->(c_type) { c_type.include?("_t") } - "#{c_type}#{c_name}" + when ->(c_type) { c_type.end_with?("_t *") } + "#{@c_type}#{c_name}" else "#{@c_type}_t *#{c_name}" end @@ -54,9 +54,32 @@ def ast_node? @c_type.include?("_types_") end + # Returns a C expression that evaluates to the Ruby VALUE object for this field. + def cached_ruby_value_expr + case @c_type + when "VALUE" + @name + when "bool" + "#{@name} ? Qtrue : Qfalse" + when "rbs_node", "rbs_node_list", "rbs_location", "rbs_hash" + "#{@name} == NULL ? Qnil : #{@name}->cached_ruby_value" + else + "#{@name} == NULL ? Qnil : #{@name}->base.cached_ruby_value" + end + end + def needs_to_be_freed? !["VALUE", "bool"].include?(@c_type) end + + def ast_node? + @c_type == "rbs_node" || + @c_type == "rbs_typename" || + @c_type == "rbs_namespace" || + @c_type.include?("_ast_") || + @c_type.include?("_decl_") || + @c_type.include?("_types_") + end end class Type @@ -68,7 +91,11 @@ class Type # e.g. `TypeAlias` attr_reader :ruby_class_name #: String - # The name of the auto-generated C struct for this type, + # The base name of the auto-generated C struct for this type. + # e.g. `rbs_ast_declarations_typealias` + attr_reader :c_base_name #: String + + # The name of the typedef of the auto-generated C struct for this type, # e.g. `rbs_ast_declarations_typealias_t` attr_reader :c_type_name #: String @@ -106,13 +133,12 @@ def initialize(yaml) @c_type_enum_name = @c_base_name.upcase @expose_to_ruby = yaml.fetch("expose_to_ruby", true) + @builds_ruby_object_internally = yaml.fetch("builds_ruby_object_internally", true) @fields = yaml.fetch("fields", []).map { |field| Field.from_hash(field) }.freeze - @constructor_params = [ - Field.new(name: "allocator", c_type: "rbs_allocator_t *"), - Field.new(name: "ruby_value", c_type: "VALUE"), - ] + @constructor_params = [Field.new(name: "allocator", c_type: "rbs_allocator_t *")] + @constructor_params << Field.new(name: "ruby_value", c_type: "VALUE") unless builds_ruby_object_internally? @constructor_params.concat @fields @constructor_params.freeze end @@ -128,6 +154,12 @@ def c_constructor_function_name #: String def expose_to_ruby? @expose_to_ruby end + + # When true, this object is expected to build its own Ruby VALUE object inside its `*_new()` function. + # When false, the `*_new()` function will take a Ruby VALUE as its first argument. + def builds_ruby_object_internally? + @builds_ruby_object_internally + end end class << self diff --git a/test/rbs/type_parsing_test.rb b/test/rbs/type_parsing_test.rb index 7a60203e7..b234b73cd 100644 --- a/test/rbs/type_parsing_test.rb +++ b/test/rbs/type_parsing_test.rb @@ -638,13 +638,14 @@ def test_string_literal_union end def test_record - Parser.parse_type("{ foo: untyped, 3 => 'hoge' }").yield_self do |type| + Parser.parse_type("{ foo: untyped, 3 => 'hoge', 4 => 'huge' }").yield_self do |type| assert_instance_of Types::Record, type assert_equal({ foo: Types::Bases::Any.new(location: nil), - 3 => Types::Literal.new(literal: "hoge", location: nil) + 3 => Types::Literal.new(literal: "hoge", location: nil), + 4 => Types::Literal.new(literal: "huge", location: nil), }, type.fields) - assert_equal "{ foo: untyped, 3 => 'hoge' }", type.location.source + assert_equal "{ foo: untyped, 3 => 'hoge', 4 => 'huge' }", type.location.source end Parser.parse_type("{}").yield_self do |type| From 6fcb89ebac214968803101b27144f7c01cbec2ca Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Fri, 8 Nov 2024 12:38:37 -0500 Subject: [PATCH 005/111] Do not test inner type for shitlist Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/ast_translation.c | 16 ---------------- .../ext/rbs_extension/ast_translation.c.erb | 8 ++++---- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index ad4d6dbb8..9cc0d431d 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -73,10 +73,6 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_BOOL: { - if (strcmp(class_name, "RBS::AST::Bool") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Bool, got %s\n", class_name); - exit(1); - } return instance->cached_ruby_value; } case RBS_AST_COMMENT: { @@ -284,10 +280,6 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_DECLARATIONS_NODES: { - if (strcmp(class_name, "RBS::AST::Declarations::Nodes") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Declarations::Nodes, got %s\n", class_name); - exit(1); - } return instance->cached_ruby_value; } case RBS_AST_DECLARATIONS_TYPEALIAS: { @@ -313,10 +305,6 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_DIRECTIVES_NODES: { - if (strcmp(class_name, "RBS::AST::Directives::Nodes") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Directives::Nodes, got %s\n", class_name); - exit(1); - } return instance->cached_ruby_value; } case RBS_AST_DIRECTIVES_USE: { @@ -668,10 +656,6 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_SYMBOL: { - if (strcmp(class_name, "RBS::AST::Symbol") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Symbol, got %s\n", class_name); - exit(1); - } return instance->cached_ruby_value; } case RBS_AST_TYPEPARAM: { diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index 047f4feeb..c6ee3c70e 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -49,10 +49,6 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { switch (instance->type) { <%- nodes.each do |node| -%> case <%= node.c_type_enum_name %>: { - if (strcmp(class_name, "<%= node.ruby_full_name %>") != 0) { - fprintf(stderr, "Expected class name: <%= node.ruby_full_name %>, got %s\n", class_name); - exit(1); - } <%- rbs_struct_to_ruby_value_shitlist = [ "RBS::AST::Bool", @@ -64,6 +60,10 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { if rbs_struct_to_ruby_value_shitlist.include?(node.ruby_full_name) -%> return instance->cached_ruby_value; <%- else -%> + if (strcmp(class_name, "<%= node.ruby_full_name %>") != 0) { + fprintf(stderr, "Expected class name: <%= node.ruby_full_name %>, got %s\n", class_name); + exit(1); + } <%- if node.fields.any? -%> <%# This prevents "warning: unused variable 'node'" %> <%= node.c_type_name %> *node = (<%= node.c_type_name %> *)instance; <%- end -%> From cd901ba3958e320b6566c28b336a31fda29db42e Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Fri, 8 Nov 2024 13:33:32 -0500 Subject: [PATCH 006/111] Move away from Ruby data types Signed-off-by: Alexandre Terrasa Use C hash for `check_key_duplication` Signed-off-by: Alexandre Terrasa Use C hash to represent Record fields Signed-off-by: Alexandre Terrasa Migrate `memo` to using a C hash Signed-off-by: Alexandre Terrasa Uses C hashes for keyword parameters Signed-off-by: Alexandre Terrasa Remove parser call to `todo!` Signed-off-by: Alexandre Terrasa Remove calls to `rbs_struct_to_ruby_value` Signed-off-by: Alexandre Terrasa TMP symbol Signed-off-by: Alexandre Terrasa Replace 2 fake nodes by one Signed-off-by: Alexandre Terrasa Set fields for `Record::FieldType` Signed-off-by: Alexandre Terrasa Make comment use a `rbs_ast_comment_t` instead of a `VALUE` Signed-off-by: Alexandre Terrasa Add `rbs_ast_string_t` Add `rbs_ast_integer_t` Migrate `literal` to store C nodes Remove `cached_ruby_string` Remove useless templating stuff Signed-off-by: Alexandre Terrasa Remove `cached_ruby_value` from `rbs_node_list` Signed-off-by: Alexandre Terrasa Remove `cached_ruby_value` from `rbs_hash` Signed-off-by: Alexandre Terrasa Add `rbs_string`, and use it for annotations Add `rbs_ast_symbol_t` to model symbols in the AST Co-Authored-By: Alexander Momchilov --- Rakefile | 5 - config.yml | 92 +- demo.rb | 23 + ext/rbs_extension/ast_translation.c | 704 +++++++-------- ext/rbs_extension/ast_translation.h | 7 +- ext/rbs_extension/lexer.h | 2 + ext/rbs_extension/parser.c | 410 ++++----- ext/rbs_extension/parser.h | 2 +- ext/rbs_extension/parserstate.c | 11 +- ext/rbs_extension/parserstate.h | 5 +- ext/rbs_extension/rbs_string_bridging.c | 17 + ext/rbs_extension/rbs_string_bridging.h | 17 + ext/rbs_extension/unescape.c | 3 +- include/rbs.h | 1 - include/rbs/ast.h | 274 +++--- include/rbs/constants.h | 1 - include/rbs/encoding.h | 9 + include/rbs/rbs_string.h | 85 ++ include/rbs/rbs_unescape.h | 20 + include/rbs/ruby_objs.h | 73 -- include/rbs/util/rbs_constant_pool.h | 1 + src/ast.c | 823 +++--------------- src/constants.c | 2 - src/encoding.c | 57 ++ src/rbs_string.c | 112 +++ src/rbs_unescape.c | 126 +++ src/ruby_objs.c | 805 ----------------- src/util/rbs_constant_pool.c | 5 + .../ext/rbs_extension/ast_translation.c.erb | 134 ++- .../ext/rbs_extension/ast_translation.h.erb | 7 +- templates/include/rbs/ast.h.erb | 39 +- templates/include/rbs/ruby_objs.h.erb | 10 - templates/src/ast.c.erb | 89 +- templates/src/ruby_objs.c.erb | 27 - templates/template.rb | 32 - 35 files changed, 1539 insertions(+), 2491 deletions(-) create mode 100644 demo.rb create mode 100644 ext/rbs_extension/rbs_string_bridging.c create mode 100644 ext/rbs_extension/rbs_string_bridging.h create mode 100644 include/rbs/encoding.h create mode 100644 include/rbs/rbs_string.h create mode 100644 include/rbs/rbs_unescape.h delete mode 100644 include/rbs/ruby_objs.h create mode 100644 src/encoding.c create mode 100644 src/rbs_string.c create mode 100644 src/rbs_unescape.c delete mode 100644 src/ruby_objs.c delete mode 100644 templates/include/rbs/ruby_objs.h.erb delete mode 100644 templates/src/ruby_objs.c.erb diff --git a/Rakefile b/Rakefile index a244dd63d..19e83a520 100644 --- a/Rakefile +++ b/Rakefile @@ -64,16 +64,11 @@ task :templates do sh "#{ruby} templates/template.rb include/rbs/constants.h" sh "#{ruby} templates/template.rb src/constants.c" - - sh "#{ruby} templates/template.rb include/rbs/ruby_objs.h" - sh "#{ruby} templates/template.rb src/ruby_objs.c" end task :compile => "ext/rbs_extension/lexer.c" task :compile => "include/rbs/constants.h" -task :compile => "include/rbs/ruby_objs.h" task :compile => "src/constants.c" -task :compile => "src/ruby_objs.c" task :test_doc do files = Dir.chdir(File.expand_path('..', __FILE__)) do diff --git a/config.yml b/config.yml index af4357477..b3007f4e8 100644 --- a/config.yml +++ b/config.yml @@ -2,6 +2,7 @@ nodes: - name: RBS::AST::Annotation fields: - name: string + c_type: rbs_string - name: location c_type: rbs_location - name: RBS::AST::Bool @@ -10,7 +11,6 @@ nodes: - name: value c_type: bool - name: RBS::AST::Comment - builds_ruby_object_internally: false fields: - name: string - name: location @@ -30,6 +30,7 @@ nodes: - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: RBS::AST::Declarations::Class::Super fields: - name: name @@ -47,6 +48,7 @@ nodes: - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: RBS::AST::Declarations::Constant fields: - name: name @@ -56,6 +58,7 @@ nodes: - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: RBS::AST::Declarations::Global fields: - name: name @@ -65,6 +68,7 @@ nodes: - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: RBS::AST::Declarations::Interface fields: - name: name @@ -78,6 +82,7 @@ nodes: - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: RBS::AST::Declarations::Module fields: - name: name @@ -93,6 +98,7 @@ nodes: - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: RBS::AST::Declarations::Module::Self fields: - name: name @@ -110,12 +116,7 @@ nodes: - name: location c_type: rbs_location - name: comment - - name: RBS::AST::Declarations::Nodes - builds_ruby_object_internally: false - expose_to_ruby: false - fields: - - name: declarations - c_type: rbs_node_list + c_type: rbs_ast_comment - name: RBS::AST::Declarations::TypeAlias fields: - name: name @@ -129,12 +130,7 @@ nodes: - name: location c_type: rbs_location - name: comment - - name: RBS::AST::Directives::Nodes - builds_ruby_object_internally: false - expose_to_ruby: false - fields: - - name: directives - c_type: rbs_node_list + c_type: rbs_ast_comment - name: RBS::AST::Directives::Use fields: - name: clauses @@ -162,12 +158,13 @@ nodes: - name: old_name c_type: rbs_ast_symbol - name: kind - c_type: rbs_ast_symbol + c_type: rbs_keyword - name: annotations c_type: rbs_node_list - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: RBS::AST::Members::AttrAccessor fields: - name: name @@ -177,14 +174,15 @@ nodes: - name: ivar_name c_type: rbs_node # rbs_ast_symbol_t, NULL or rbs_ast_bool_new(false) - name: kind - c_type: rbs_ast_symbol + c_type: rbs_keyword - name: annotations c_type: rbs_node_list - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: visibility - c_type: rbs_ast_symbol + c_type: rbs_keyword - name: RBS::AST::Members::AttrReader fields: - name: name @@ -194,14 +192,15 @@ nodes: - name: ivar_name c_type: rbs_node # rbs_ast_symbol_t, NULL or rbs_ast_bool_new(false) - name: kind - c_type: rbs_ast_symbol + c_type: rbs_keyword - name: annotations c_type: rbs_node_list - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: visibility - c_type: rbs_ast_symbol + c_type: rbs_keyword - name: RBS::AST::Members::AttrWriter fields: - name: name @@ -211,14 +210,15 @@ nodes: - name: ivar_name c_type: rbs_node # rbs_ast_symbol_t, NULL or rbs_ast_bool_new(false) - name: kind - c_type: rbs_ast_symbol + c_type: rbs_keyword - name: annotations c_type: rbs_node_list - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: visibility - c_type: rbs_ast_symbol + c_type: rbs_keyword - name: RBS::AST::Members::ClassInstanceVariable fields: - name: name @@ -228,6 +228,7 @@ nodes: - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: RBS::AST::Members::ClassVariable fields: - name: name @@ -237,6 +238,7 @@ nodes: - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: RBS::AST::Members::Extend fields: - name: name @@ -248,6 +250,7 @@ nodes: - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: RBS::AST::Members::Include fields: - name: name @@ -259,6 +262,7 @@ nodes: - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: RBS::AST::Members::InstanceVariable fields: - name: name @@ -268,12 +272,13 @@ nodes: - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: RBS::AST::Members::MethodDefinition fields: - name: name c_type: rbs_ast_symbol - name: kind - c_type: rbs_ast_symbol + c_type: rbs_keyword - name: overloads c_type: rbs_node_list - name: annotations @@ -281,10 +286,11 @@ nodes: - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: overloading c_type: bool - name: visibility - c_type: rbs_ast_symbol + c_type: rbs_keyword - name: RBS::AST::Members::MethodDefinition::Overload fields: - name: annotations @@ -302,6 +308,7 @@ nodes: - name: location c_type: rbs_location - name: comment + c_type: rbs_ast_comment - name: RBS::AST::Members::Private fields: - name: location @@ -310,14 +317,12 @@ nodes: fields: - name: location c_type: rbs_location - - name: RBS::AST::Symbol - builds_ruby_object_internally: false - name: RBS::AST::TypeParam fields: - name: name c_type: rbs_ast_symbol - name: variance - c_type: rbs_ast_symbol + c_type: rbs_keyword - name: upper_bound c_type: rbs_node - name: default_type @@ -326,6 +331,16 @@ nodes: c_type: bool - name: location c_type: rbs_location + - name: RBS::AST::Integer + expose_to_ruby: false + fields: + - name: string_representation + c_type: rbs_string + - name: RBS::AST::String + expose_to_ruby: false + fields: + - name: string + c_type: rbs_string - name: RBS::MethodType fields: - name: type_params @@ -342,6 +357,13 @@ nodes: c_type: rbs_node_list - name: absolute c_type: bool + - name: RBS::Signature + expose_to_ruby: false + fields: + - name: directives + c_type: rbs_node_list + - name: declarations + c_type: rbs_node_list - name: RBS::TypeName fields: - name: namespace @@ -359,6 +381,7 @@ nodes: - name: RBS::Types::Bases::Any fields: - name: todo + c_type: bool - name: location c_type: rbs_location - name: RBS::Types::Bases::Bool @@ -418,13 +441,21 @@ nodes: - name: RBS::Types::Function fields: - name: required_positionals + c_type: rbs_node_list - name: optional_positionals + c_type: rbs_node_list - name: rest_positionals + c_type: rbs_node - name: trailing_positionals + c_type: rbs_node_list - name: required_keywords + c_type: rbs_hash - name: optional_keywords + c_type: rbs_hash - name: rest_keywords + c_type: rbs_node - name: return_type + c_type: rbs_node - name: RBS::Types::Function::Param fields: - name: type @@ -450,6 +481,7 @@ nodes: - name: RBS::Types::Literal fields: - name: literal + c_type: rbs_node - name: location c_type: rbs_location - name: RBS::Types::Optional @@ -471,8 +503,16 @@ nodes: - name: RBS::Types::Record fields: - name: all_fields + c_type: rbs_hash - name: location c_type: rbs_location + - name: RBS::Types::Record::FieldType + expose_to_ruby: false + fields: + - name: type + c_type: rbs_node + - name: required + c_type: bool - name: RBS::Types::Tuple fields: - name: types @@ -494,4 +534,4 @@ nodes: - name: name c_type: rbs_ast_symbol - name: location - c_type: rbs_location \ No newline at end of file + c_type: rbs_location diff --git a/demo.rb b/demo.rb new file mode 100644 index 000000000..997901935 --- /dev/null +++ b/demo.rb @@ -0,0 +1,23 @@ +require 'rubyvm/instruction_sequence' + +# Example Ruby code +code = <<~RUBY + class C + def x + [1, 2, 3] + end + + def hash + [1, 2, 3].hash + end + end +RUBY + +# Compile the Ruby code into instruction sequences +iseq = RubyVM::InstructionSequence.compile(code) + +# Disassemble the instruction sequences +disassembled_code = iseq.disasm + +# Print disassembled code +puts disassembled_code diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index 9cc0d431d..99adc490f 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -7,17 +7,32 @@ #include "ast_translation.h" +#include "ruby.h" +#include "ruby/encoding.h" + #include "rbs/constants.h" +#include "rbs_string_bridging.h" -#include +VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t *list) { + VALUE ruby_array = rb_ary_new(); + for (rbs_node_list_node_t *n = list->head; n != NULL; n = n->next) { + rb_ary_push(ruby_array, rbs_struct_to_ruby_value(ctx, n->node)); + } -VALUE rbs_node_list_to_ruby_array(rbs_node_list_t *list) { - return list->cached_ruby_value; + return ruby_array; } -VALUE rbs_hash_to_ruby_hash(rbs_hash_t *hash) { - return hash->cached_ruby_value; +VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t ctx, rbs_hash_t *rbs_hash) { + VALUE ruby_hash = rb_hash_new(); + + for (rbs_hash_node_t *n = rbs_hash->head; n != NULL; n = n->next) { + VALUE key = rbs_struct_to_ruby_value(ctx, n->key); + VALUE value = rbs_struct_to_ruby_value(ctx, n->value); + rb_hash_aset(ruby_hash, key, value); + } + + return ruby_hash; } VALUE rbs_loc_to_ruby_location(rbs_location_t *loc) { @@ -34,38 +49,19 @@ VALUE rbs_loc_to_ruby_location(rbs_location_t *loc) { rb_class_new_instance(argc, argv, receiver) #endif -const char* get_class_name(VALUE o) { - VALUE klass = rb_class_of(o); // Get the class of the object - VALUE klass_name = rb_class_name(klass); // Get the name of the class - const char* name = StringValueCStr(klass_name); // Convert to C string - return name; -} - -VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { +VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instance) { if (instance == NULL) return Qnil; - VALUE ruby_value = instance->cached_ruby_value; - - if (ruby_value == Qnil || ruby_value == Qundef) { - fprintf(stderr, "cached_ruby_value is NULL\n"); - exit(1); - } - - const char *class_name = get_class_name(ruby_value); - switch (instance->type) { case RBS_AST_ANNOTATION: { - if (strcmp(class_name, "RBS::AST::Annotation") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Annotation, got %s\n", class_name); - exit(1); - } rbs_ast_annotation_t *node = (rbs_ast_annotation_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("string")), node->string); + rb_hash_aset(h, ID2SYM(rb_intern("string")), rbs_string_to_ruby_string(&node->string)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_AST_Annotation, 1, @@ -73,13 +69,10 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_BOOL: { - return instance->cached_ruby_value; + return ((rbs_ast_bool_t *) instance)->value ? Qtrue : Qfalse; + } case RBS_AST_COMMENT: { - if (strcmp(class_name, "RBS::AST::Comment") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Comment, got %s\n", class_name); - exit(1); - } rbs_ast_comment_t *node = (rbs_ast_comment_t *)instance; @@ -87,6 +80,7 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { rb_hash_aset(h, ID2SYM(rb_intern("string")), node->string); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_AST_Comment, 1, @@ -94,21 +88,24 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_DECLARATIONS_CLASS: { - if (strcmp(class_name, "RBS::AST::Declarations::Class") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Declarations::Class, got %s\n", class_name); - exit(1); - } rbs_ast_declarations_class_t *node = (rbs_ast_declarations_class_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(node->type_params)); - rb_hash_aset(h, ID2SYM(rb_intern("super_class")), rbs_struct_to_ruby_value((rbs_node_t *) node->super_class)); // rbs_ast_declarations_class_super - rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(node->members)); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); + rb_hash_aset(h, ID2SYM(rb_intern("super_class")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->super_class)); // rbs_ast_declarations_class_super + rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + + rb_funcall( + RBS_AST_TypeParam, + rb_intern("resolve_variables"), + 1, + rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) + ); return CLASS_NEW_INSTANCE( RBS_AST_Declarations_Class, @@ -117,18 +114,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_DECLARATIONS_CLASS_SUPER: { - if (strcmp(class_name, "RBS::AST::Declarations::Class::Super") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Declarations::Class::Super, got %s\n", class_name); - exit(1); - } rbs_ast_declarations_class_super_t *node = (rbs_ast_declarations_class_super_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_AST_Declarations_Class_Super, 1, @@ -136,18 +130,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_DECLARATIONS_CLASSALIAS: { - if (strcmp(class_name, "RBS::AST::Declarations::ClassAlias") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Declarations::ClassAlias, got %s\n", class_name); - exit(1); - } rbs_ast_declarations_classalias_t *node = (rbs_ast_declarations_classalias_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->new_name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->old_name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + return CLASS_NEW_INSTANCE( RBS_AST_Declarations_ClassAlias, @@ -156,18 +147,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_DECLARATIONS_CONSTANT: { - if (strcmp(class_name, "RBS::AST::Declarations::Constant") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Declarations::Constant, got %s\n", class_name); - exit(1); - } rbs_ast_declarations_constant_t *node = (rbs_ast_declarations_constant_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + return CLASS_NEW_INSTANCE( RBS_AST_Declarations_Constant, @@ -176,18 +164,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_DECLARATIONS_GLOBAL: { - if (strcmp(class_name, "RBS::AST::Declarations::Global") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Declarations::Global, got %s\n", class_name); - exit(1); - } rbs_ast_declarations_global_t *node = (rbs_ast_declarations_global_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + return CLASS_NEW_INSTANCE( RBS_AST_Declarations_Global, @@ -196,20 +181,23 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_DECLARATIONS_INTERFACE: { - if (strcmp(class_name, "RBS::AST::Declarations::Interface") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Declarations::Interface, got %s\n", class_name); - exit(1); - } rbs_ast_declarations_interface_t *node = (rbs_ast_declarations_interface_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(node->type_params)); - rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(node->members)); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); + rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + + rb_funcall( + RBS_AST_TypeParam, + rb_intern("resolve_variables"), + 1, + rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) + ); return CLASS_NEW_INSTANCE( RBS_AST_Declarations_Interface, @@ -218,21 +206,24 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_DECLARATIONS_MODULE: { - if (strcmp(class_name, "RBS::AST::Declarations::Module") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Declarations::Module, got %s\n", class_name); - exit(1); - } rbs_ast_declarations_module_t *node = (rbs_ast_declarations_module_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(node->type_params)); - rb_hash_aset(h, ID2SYM(rb_intern("self_types")), rbs_node_list_to_ruby_array(node->self_types)); - rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(node->members)); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); + rb_hash_aset(h, ID2SYM(rb_intern("self_types")), rbs_node_list_to_ruby_array(ctx, node->self_types)); + rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + + rb_funcall( + RBS_AST_TypeParam, + rb_intern("resolve_variables"), + 1, + rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) + ); return CLASS_NEW_INSTANCE( RBS_AST_Declarations_Module, @@ -241,18 +232,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_DECLARATIONS_MODULE_SELF: { - if (strcmp(class_name, "RBS::AST::Declarations::Module::Self") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Declarations::Module::Self, got %s\n", class_name); - exit(1); - } rbs_ast_declarations_module_self_t *node = (rbs_ast_declarations_module_self_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_AST_Declarations_Module_Self, 1, @@ -260,18 +248,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_DECLARATIONS_MODULEALIAS: { - if (strcmp(class_name, "RBS::AST::Declarations::ModuleAlias") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Declarations::ModuleAlias, got %s\n", class_name); - exit(1); - } rbs_ast_declarations_modulealias_t *node = (rbs_ast_declarations_modulealias_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->new_name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->old_name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + return CLASS_NEW_INSTANCE( RBS_AST_Declarations_ModuleAlias, @@ -279,24 +264,24 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { &h ); } - case RBS_AST_DECLARATIONS_NODES: { - return instance->cached_ruby_value; - } case RBS_AST_DECLARATIONS_TYPEALIAS: { - if (strcmp(class_name, "RBS::AST::Declarations::TypeAlias") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Declarations::TypeAlias, got %s\n", class_name); - exit(1); - } rbs_ast_declarations_typealias_t *node = (rbs_ast_declarations_typealias_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(node->type_params)); - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + + rb_funcall( + RBS_AST_TypeParam, + rb_intern("resolve_variables"), + 1, + rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) + ); return CLASS_NEW_INSTANCE( RBS_AST_Declarations_TypeAlias, @@ -304,21 +289,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { &h ); } - case RBS_AST_DIRECTIVES_NODES: { - return instance->cached_ruby_value; - } case RBS_AST_DIRECTIVES_USE: { - if (strcmp(class_name, "RBS::AST::Directives::Use") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Directives::Use, got %s\n", class_name); - exit(1); - } rbs_ast_directives_use_t *node = (rbs_ast_directives_use_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("clauses")), rbs_node_list_to_ruby_array(node->clauses)); + rb_hash_aset(h, ID2SYM(rb_intern("clauses")), rbs_node_list_to_ruby_array(ctx, node->clauses)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_AST_Directives_Use, 1, @@ -326,18 +305,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_DIRECTIVES_USE_SINGLECLAUSE: { - if (strcmp(class_name, "RBS::AST::Directives::Use::SingleClause") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Directives::Use::SingleClause, got %s\n", class_name); - exit(1); - } rbs_ast_directives_use_singleclause_t *node = (rbs_ast_directives_use_singleclause_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("type_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->type_name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->new_name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type_name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_AST_Directives_Use_SingleClause, 1, @@ -345,38 +321,41 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE: { - if (strcmp(class_name, "RBS::AST::Directives::Use::WildcardClause") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Directives::Use::WildcardClause, got %s\n", class_name); - exit(1); - } rbs_ast_directives_use_wildcardclause_t *node = (rbs_ast_directives_use_wildcardclause_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value((rbs_node_t *) node->namespace)); // rbs_namespace + rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->namespace)); // rbs_namespace rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_AST_Directives_Use_WildcardClause, 1, &h ); } + case RBS_AST_INTEGER: { + rbs_ast_integer_t *integer_node = (rbs_ast_integer_t *) instance; + rbs_string_t string_repr = integer_node->string_representation; + + VALUE str = rb_enc_str_new(string_repr.start, rbs_string_len(string_repr), rb_utf8_encoding()); + + return rb_funcall(str, rb_intern("to_i"), 0); + + } case RBS_AST_MEMBERS_ALIAS: { - if (strcmp(class_name, "RBS::AST::Members::Alias") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Members::Alias, got %s\n", class_name); - exit(1); - } rbs_ast_members_alias_t *node = (rbs_ast_members_alias_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->new_name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->old_name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value((rbs_node_t *) node->kind)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + return CLASS_NEW_INSTANCE( RBS_AST_Members_Alias, @@ -385,22 +364,19 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_MEMBERS_ATTRACCESSOR: { - if (strcmp(class_name, "RBS::AST::Members::AttrAccessor") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Members::AttrAccessor, got %s\n", class_name); - exit(1); - } rbs_ast_members_attraccessor_t *node = (rbs_ast_members_attraccessor_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->ivar_name)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value((rbs_node_t *) node->kind)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); - rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value((rbs_node_t *) node->visibility)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword + return CLASS_NEW_INSTANCE( RBS_AST_Members_AttrAccessor, @@ -409,22 +385,19 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_MEMBERS_ATTRREADER: { - if (strcmp(class_name, "RBS::AST::Members::AttrReader") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Members::AttrReader, got %s\n", class_name); - exit(1); - } rbs_ast_members_attrreader_t *node = (rbs_ast_members_attrreader_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->ivar_name)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value((rbs_node_t *) node->kind)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); - rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value((rbs_node_t *) node->visibility)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword + return CLASS_NEW_INSTANCE( RBS_AST_Members_AttrReader, @@ -433,22 +406,19 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_MEMBERS_ATTRWRITER: { - if (strcmp(class_name, "RBS::AST::Members::AttrWriter") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Members::AttrWriter, got %s\n", class_name); - exit(1); - } rbs_ast_members_attrwriter_t *node = (rbs_ast_members_attrwriter_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value((rbs_node_t *) node->ivar_name)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value((rbs_node_t *) node->kind)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); - rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value((rbs_node_t *) node->visibility)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword + return CLASS_NEW_INSTANCE( RBS_AST_Members_AttrWriter, @@ -457,18 +427,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE: { - if (strcmp(class_name, "RBS::AST::Members::ClassInstanceVariable") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Members::ClassInstanceVariable, got %s\n", class_name); - exit(1); - } rbs_ast_members_classinstancevariable_t *node = (rbs_ast_members_classinstancevariable_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + return CLASS_NEW_INSTANCE( RBS_AST_Members_ClassInstanceVariable, @@ -477,18 +444,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_MEMBERS_CLASSVARIABLE: { - if (strcmp(class_name, "RBS::AST::Members::ClassVariable") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Members::ClassVariable, got %s\n", class_name); - exit(1); - } rbs_ast_members_classvariable_t *node = (rbs_ast_members_classvariable_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + return CLASS_NEW_INSTANCE( RBS_AST_Members_ClassVariable, @@ -497,19 +461,16 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_MEMBERS_EXTEND: { - if (strcmp(class_name, "RBS::AST::Members::Extend") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Members::Extend, got %s\n", class_name); - exit(1); - } rbs_ast_members_extend_t *node = (rbs_ast_members_extend_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + return CLASS_NEW_INSTANCE( RBS_AST_Members_Extend, @@ -518,19 +479,16 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_MEMBERS_INCLUDE: { - if (strcmp(class_name, "RBS::AST::Members::Include") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Members::Include, got %s\n", class_name); - exit(1); - } rbs_ast_members_include_t *node = (rbs_ast_members_include_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + return CLASS_NEW_INSTANCE( RBS_AST_Members_Include, @@ -539,18 +497,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_MEMBERS_INSTANCEVARIABLE: { - if (strcmp(class_name, "RBS::AST::Members::InstanceVariable") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Members::InstanceVariable, got %s\n", class_name); - exit(1); - } rbs_ast_members_instancevariable_t *node = (rbs_ast_members_instancevariable_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + return CLASS_NEW_INSTANCE( RBS_AST_Members_InstanceVariable, @@ -559,22 +514,19 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_MEMBERS_METHODDEFINITION: { - if (strcmp(class_name, "RBS::AST::Members::MethodDefinition") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Members::MethodDefinition, got %s\n", class_name); - exit(1); - } rbs_ast_members_methoddefinition_t *node = (rbs_ast_members_methoddefinition_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value((rbs_node_t *) node->kind)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("overloads")), rbs_node_list_to_ruby_array(node->overloads)); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword + rb_hash_aset(h, ID2SYM(rb_intern("overloads")), rbs_node_list_to_ruby_array(ctx, node->overloads)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_hash_aset(h, ID2SYM(rb_intern("overloading")), node->overloading ? Qtrue : Qfalse); - rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value((rbs_node_t *) node->visibility)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword + return CLASS_NEW_INSTANCE( RBS_AST_Members_MethodDefinition, @@ -583,16 +535,13 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD: { - if (strcmp(class_name, "RBS::AST::Members::MethodDefinition::Overload") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Members::MethodDefinition::Overload, got %s\n", class_name); - exit(1); - } rbs_ast_members_methoddefinition_overload_t *node = (rbs_ast_members_methoddefinition_overload_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("method_type")), rbs_struct_to_ruby_value((rbs_node_t *) node->method_type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("method_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->method_type)); // rbs_node + return CLASS_NEW_INSTANCE( RBS_AST_Members_MethodDefinition_Overload, @@ -601,19 +550,16 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_MEMBERS_PREPEND: { - if (strcmp(class_name, "RBS::AST::Members::Prepend") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Members::Prepend, got %s\n", class_name); - exit(1); - } rbs_ast_members_prepend_t *node = (rbs_ast_members_prepend_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(node->annotations)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("comment")), node->comment); + rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment + return CLASS_NEW_INSTANCE( RBS_AST_Members_Prepend, @@ -622,16 +568,13 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_MEMBERS_PRIVATE: { - if (strcmp(class_name, "RBS::AST::Members::Private") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Members::Private, got %s\n", class_name); - exit(1); - } rbs_ast_members_private_t *node = (rbs_ast_members_private_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_AST_Members_Private, 1, @@ -639,41 +582,39 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_AST_MEMBERS_PUBLIC: { - if (strcmp(class_name, "RBS::AST::Members::Public") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::Members::Public, got %s\n", class_name); - exit(1); - } rbs_ast_members_public_t *node = (rbs_ast_members_public_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_AST_Members_Public, 1, &h ); } - case RBS_AST_SYMBOL: { - return instance->cached_ruby_value; + case RBS_AST_STRING: { + rbs_ast_string_t *string_node = (rbs_ast_string_t *) instance; + rbs_string_t s = string_node->string; + + return rb_enc_str_new(s.start, rbs_string_len(s), rb_utf8_encoding()); + } case RBS_AST_TYPEPARAM: { - if (strcmp(class_name, "RBS::AST::TypeParam") != 0) { - fprintf(stderr, "Expected class name: RBS::AST::TypeParam, got %s\n", class_name); - exit(1); - } rbs_ast_typeparam_t *node = (rbs_ast_typeparam_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("variance")), rbs_struct_to_ruby_value((rbs_node_t *) node->variance)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("upper_bound")), rbs_struct_to_ruby_value((rbs_node_t *) node->upper_bound)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("default_type")), rbs_struct_to_ruby_value((rbs_node_t *) node->default_type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("variance")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->variance)); // rbs_keyword + rb_hash_aset(h, ID2SYM(rb_intern("upper_bound")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->upper_bound)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("default_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->default_type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("unchecked")), node->unchecked ? Qtrue : Qfalse); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_AST_TypeParam, 1, @@ -681,19 +622,22 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_METHODTYPE: { - if (strcmp(class_name, "RBS::MethodType") != 0) { - fprintf(stderr, "Expected class name: RBS::MethodType, got %s\n", class_name); - exit(1); - } rbs_methodtype_t *node = (rbs_methodtype_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(node->type_params)); - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("block")), rbs_struct_to_ruby_value((rbs_node_t *) node->block)); // rbs_types_block + rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("block")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->block)); // rbs_types_block rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_funcall( + RBS_AST_TypeParam, + rb_intern("resolve_variables"), + 1, + rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) + ); + return CLASS_NEW_INSTANCE( RBS_MethodType, 1, @@ -701,34 +645,36 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_NAMESPACE: { - if (strcmp(class_name, "RBS::Namespace") != 0) { - fprintf(stderr, "Expected class name: RBS::Namespace, got %s\n", class_name); - exit(1); - } rbs_namespace_t *node = (rbs_namespace_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("path")), rbs_node_list_to_ruby_array(node->path)); + rb_hash_aset(h, ID2SYM(rb_intern("path")), rbs_node_list_to_ruby_array(ctx, node->path)); rb_hash_aset(h, ID2SYM(rb_intern("absolute")), node->absolute ? Qtrue : Qfalse); + return CLASS_NEW_INSTANCE( RBS_Namespace, 1, &h ); } + case RBS_SIGNATURE: { + rbs_signature_t *signature = (rbs_signature_t *) instance; + + VALUE array = rb_ary_new(); + rb_ary_push(array, rbs_node_list_to_ruby_array(ctx, signature->directives)); + rb_ary_push(array, rbs_node_list_to_ruby_array(ctx, signature->declarations)); + return array; + } case RBS_TYPENAME: { - if (strcmp(class_name, "RBS::TypeName") != 0) { - fprintf(stderr, "Expected class name: RBS::TypeName, got %s\n", class_name); - exit(1); - } rbs_typename_t *node = (rbs_typename_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value((rbs_node_t *) node->namespace)); // rbs_namespace - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->namespace)); // rbs_namespace + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol + return CLASS_NEW_INSTANCE( RBS_TypeName, @@ -737,18 +683,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_ALIAS: { - if (strcmp(class_name, "RBS::Types::Alias") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Alias, got %s\n", class_name); - exit(1); - } rbs_types_alias_t *node = (rbs_types_alias_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Alias, 1, @@ -756,17 +699,14 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_BASES_ANY: { - if (strcmp(class_name, "RBS::Types::Bases::Any") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Bases::Any, got %s\n", class_name); - exit(1); - } rbs_types_bases_any_t *node = (rbs_types_bases_any_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("todo")), node->todo); + rb_hash_aset(h, ID2SYM(rb_intern("todo")), node->todo ? Qtrue : Qfalse); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Bases_Any, 1, @@ -774,16 +714,13 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_BASES_BOOL: { - if (strcmp(class_name, "RBS::Types::Bases::Bool") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Bases::Bool, got %s\n", class_name); - exit(1); - } rbs_types_bases_bool_t *node = (rbs_types_bases_bool_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Bases_Bool, 1, @@ -791,16 +728,13 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_BASES_BOTTOM: { - if (strcmp(class_name, "RBS::Types::Bases::Bottom") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Bases::Bottom, got %s\n", class_name); - exit(1); - } rbs_types_bases_bottom_t *node = (rbs_types_bases_bottom_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Bases_Bottom, 1, @@ -808,16 +742,13 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_BASES_CLASS: { - if (strcmp(class_name, "RBS::Types::Bases::Class") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Bases::Class, got %s\n", class_name); - exit(1); - } rbs_types_bases_class_t *node = (rbs_types_bases_class_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Bases_Class, 1, @@ -825,16 +756,13 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_BASES_INSTANCE: { - if (strcmp(class_name, "RBS::Types::Bases::Instance") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Bases::Instance, got %s\n", class_name); - exit(1); - } rbs_types_bases_instance_t *node = (rbs_types_bases_instance_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Bases_Instance, 1, @@ -842,16 +770,13 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_BASES_NIL: { - if (strcmp(class_name, "RBS::Types::Bases::Nil") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Bases::Nil, got %s\n", class_name); - exit(1); - } rbs_types_bases_nil_t *node = (rbs_types_bases_nil_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Bases_Nil, 1, @@ -859,16 +784,13 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_BASES_SELF: { - if (strcmp(class_name, "RBS::Types::Bases::Self") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Bases::Self, got %s\n", class_name); - exit(1); - } rbs_types_bases_self_t *node = (rbs_types_bases_self_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Bases_Self, 1, @@ -876,16 +798,13 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_BASES_TOP: { - if (strcmp(class_name, "RBS::Types::Bases::Top") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Bases::Top, got %s\n", class_name); - exit(1); - } rbs_types_bases_top_t *node = (rbs_types_bases_top_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Bases_Top, 1, @@ -893,16 +812,13 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_BASES_VOID: { - if (strcmp(class_name, "RBS::Types::Bases::Void") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Bases::Void, got %s\n", class_name); - exit(1); - } rbs_types_bases_void_t *node = (rbs_types_bases_void_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Bases_Void, 1, @@ -910,17 +826,14 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_BLOCK: { - if (strcmp(class_name, "RBS::Types::Block") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Block, got %s\n", class_name); - exit(1); - } rbs_types_block_t *node = (rbs_types_block_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("required")), node->required ? Qtrue : Qfalse); - rb_hash_aset(h, ID2SYM(rb_intern("self_type")), rbs_struct_to_ruby_value((rbs_node_t *) node->self_type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("self_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->self_type)); // rbs_node + return CLASS_NEW_INSTANCE( RBS_Types_Block, @@ -929,18 +842,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_CLASSINSTANCE: { - if (strcmp(class_name, "RBS::Types::ClassInstance") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::ClassInstance, got %s\n", class_name); - exit(1); - } rbs_types_classinstance_t *node = (rbs_types_classinstance_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_ClassInstance, 1, @@ -948,17 +858,14 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_CLASSSINGLETON: { - if (strcmp(class_name, "RBS::Types::ClassSingleton") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::ClassSingleton, got %s\n", class_name); - exit(1); - } rbs_types_classsingleton_t *node = (rbs_types_classsingleton_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_ClassSingleton, 1, @@ -966,22 +873,19 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_FUNCTION: { - if (strcmp(class_name, "RBS::Types::Function") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Function, got %s\n", class_name); - exit(1); - } rbs_types_function_t *node = (rbs_types_function_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("required_positionals")), node->required_positionals); - rb_hash_aset(h, ID2SYM(rb_intern("optional_positionals")), node->optional_positionals); - rb_hash_aset(h, ID2SYM(rb_intern("rest_positionals")), node->rest_positionals); - rb_hash_aset(h, ID2SYM(rb_intern("trailing_positionals")), node->trailing_positionals); - rb_hash_aset(h, ID2SYM(rb_intern("required_keywords")), node->required_keywords); - rb_hash_aset(h, ID2SYM(rb_intern("optional_keywords")), node->optional_keywords); - rb_hash_aset(h, ID2SYM(rb_intern("rest_keywords")), node->rest_keywords); - rb_hash_aset(h, ID2SYM(rb_intern("return_type")), node->return_type); + rb_hash_aset(h, ID2SYM(rb_intern("required_positionals")), rbs_node_list_to_ruby_array(ctx, node->required_positionals)); + rb_hash_aset(h, ID2SYM(rb_intern("optional_positionals")), rbs_node_list_to_ruby_array(ctx, node->optional_positionals)); + rb_hash_aset(h, ID2SYM(rb_intern("rest_positionals")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->rest_positionals)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("trailing_positionals")), rbs_node_list_to_ruby_array(ctx, node->trailing_positionals)); + rb_hash_aset(h, ID2SYM(rb_intern("required_keywords")), rbs_hash_to_ruby_hash(ctx, node->required_keywords)); + rb_hash_aset(h, ID2SYM(rb_intern("optional_keywords")), rbs_hash_to_ruby_hash(ctx, node->optional_keywords)); + rb_hash_aset(h, ID2SYM(rb_intern("rest_keywords")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->rest_keywords)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("return_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->return_type)); // rbs_node + return CLASS_NEW_INSTANCE( RBS_Types_Function, @@ -990,18 +894,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_FUNCTION_PARAM: { - if (strcmp(class_name, "RBS::Types::Function::Param") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Function::Param, got %s\n", class_name); - exit(1); - } rbs_types_function_param_t *node = (rbs_types_function_param_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Function_Param, 1, @@ -1009,18 +910,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_INTERFACE: { - if (strcmp(class_name, "RBS::Types::Interface") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Interface, got %s\n", class_name); - exit(1); - } rbs_types_interface_t *node = (rbs_types_interface_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(node->args)); + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Interface, 1, @@ -1028,17 +926,14 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_INTERSECTION: { - if (strcmp(class_name, "RBS::Types::Intersection") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Intersection, got %s\n", class_name); - exit(1); - } rbs_types_intersection_t *node = (rbs_types_intersection_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(node->types)); + rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(ctx, node->types)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Intersection, 1, @@ -1046,17 +941,14 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_LITERAL: { - if (strcmp(class_name, "RBS::Types::Literal") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Literal, got %s\n", class_name); - exit(1); - } rbs_types_literal_t *node = (rbs_types_literal_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("literal")), node->literal); + rb_hash_aset(h, ID2SYM(rb_intern("literal")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->literal)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Literal, 1, @@ -1064,17 +956,14 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_OPTIONAL: { - if (strcmp(class_name, "RBS::Types::Optional") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Optional, got %s\n", class_name); - exit(1); - } rbs_types_optional_t *node = (rbs_types_optional_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Optional, 1, @@ -1082,18 +971,15 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_PROC: { - if (strcmp(class_name, "RBS::Types::Proc") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Proc, got %s\n", class_name); - exit(1); - } rbs_types_proc_t *node = (rbs_types_proc_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value((rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("block")), rbs_struct_to_ruby_value((rbs_node_t *) node->block)); // rbs_types_block + rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("block")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->block)); // rbs_types_block rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); - rb_hash_aset(h, ID2SYM(rb_intern("self_type")), rbs_struct_to_ruby_value((rbs_node_t *) node->self_type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("self_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->self_type)); // rbs_node + return CLASS_NEW_INSTANCE( RBS_Types_Proc, @@ -1102,35 +988,38 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_RECORD: { - if (strcmp(class_name, "RBS::Types::Record") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Record, got %s\n", class_name); - exit(1); - } rbs_types_record_t *node = (rbs_types_record_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("all_fields")), node->all_fields); + rb_hash_aset(h, ID2SYM(rb_intern("all_fields")), rbs_hash_to_ruby_hash(ctx, node->all_fields)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Record, 1, &h ); } + case RBS_TYPES_RECORD_FIELDTYPE: { + rbs_types_record_fieldtype_t *record_fieldtype = (rbs_types_record_fieldtype_t *) instance; + + VALUE array = rb_ary_new(); + rb_ary_push(array, rbs_struct_to_ruby_value(ctx, record_fieldtype->type)); + rb_ary_push(array, record_fieldtype->required ? Qtrue : Qfalse); + return array; + + } case RBS_TYPES_TUPLE: { - if (strcmp(class_name, "RBS::Types::Tuple") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Tuple, got %s\n", class_name); - exit(1); - } rbs_types_tuple_t *node = (rbs_types_tuple_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(node->types)); + rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(ctx, node->types)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Tuple, 1, @@ -1138,17 +1027,14 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_UNION: { - if (strcmp(class_name, "RBS::Types::Union") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Union, got %s\n", class_name); - exit(1); - } rbs_types_union_t *node = (rbs_types_union_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(node->types)); + rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(ctx, node->types)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Union, 1, @@ -1156,15 +1042,12 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_UNTYPEDFUNCTION: { - if (strcmp(class_name, "RBS::Types::UntypedFunction") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::UntypedFunction, got %s\n", class_name); - exit(1); - } rbs_types_untypedfunction_t *node = (rbs_types_untypedfunction_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("return_type")), rbs_struct_to_ruby_value((rbs_node_t *) node->return_type)); // rbs_node + rb_hash_aset(h, ID2SYM(rb_intern("return_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->return_type)); // rbs_node + return CLASS_NEW_INSTANCE( RBS_Types_UntypedFunction, @@ -1173,22 +1056,39 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { ); } case RBS_TYPES_VARIABLE: { - if (strcmp(class_name, "RBS::Types::Variable") != 0) { - fprintf(stderr, "Expected class name: RBS::Types::Variable, got %s\n", class_name); - exit(1); - } rbs_types_variable_t *node = (rbs_types_variable_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value((rbs_node_t *) node->name)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + return CLASS_NEW_INSTANCE( RBS_Types_Variable, 1, &h ); } + case RBS_KEYWORD: { + rbs_constant_t *constant = rbs_constant_pool_id_to_constant(RBS_GLOBAL_CONSTANT_POOL, ((rbs_keyword_t *) instance)->constant_id); + assert(constant != NULL && "constant is NULL"); + assert(constant->start != NULL && "constant->start is NULL"); + + return ID2SYM(rb_intern2((const char *) constant->start, constant->length)); + } + case RBS_AST_SYMBOL: { + rbs_constant_t *constant = rbs_constant_pool_id_to_constant(ctx.constant_pool, ((rbs_keyword_t *) instance)->constant_id); + assert(constant != NULL && "constant is NULL"); + assert(constant->start != NULL && "constant->start is NULL"); + + // FIXME: Add `rb_encoding` to the `ctx` and use it here. + rb_encoding *encoding = rb_usascii_encoding(); + + return ID2SYM(rb_intern3((const char *) constant->start, constant->length, encoding)); + } + case RBS_OTHER_RUBY_VALUE: { + return ((rbs_other_ruby_value_t *) instance)->ruby_value; + } } } diff --git a/ext/rbs_extension/ast_translation.h b/ext/rbs_extension/ast_translation.h index 0a05e0440..30681148b 100644 --- a/ext/rbs_extension/ast_translation.h +++ b/ext/rbs_extension/ast_translation.h @@ -11,9 +11,8 @@ #include "ruby.h" #include "rbs/ast.h" -VALUE rbs_node_list_to_ruby_array(rbs_node_list_t *list); -VALUE rbs_hash_to_ruby_hash(rbs_hash_t *hash); -VALUE rbs_loc_to_ruby_location(rbs_location_t *loc); -VALUE rbs_struct_to_ruby_value(rbs_node_t *instance); +VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *list); +VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t, rbs_hash_t *hash); +VALUE rbs_struct_to_ruby_value(rbs_translation_context_t, rbs_node_t *instance); #endif diff --git a/ext/rbs_extension/lexer.h b/ext/rbs_extension/lexer.h index 55a8a3994..6b294eab4 100644 --- a/ext/rbs_extension/lexer.h +++ b/ext/rbs_extension/lexer.h @@ -1,6 +1,8 @@ #ifndef RBS__LEXER_H #define RBS__LEXER_H +#include "ruby.h" + enum TokenType { NullType, /* (Nothing) */ pEOF, /* EOF */ diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index 1d52d98fa..27034e5e7 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -2,7 +2,10 @@ #include "rbs_extension.h" #include "rbs/util/rbs_constant_pool.h" #include "rbs/ast.h" +#include "rbs/rbs_string.h" #include "ast_translation.h" +#include "rbs/rbs_unescape.h" +#include "rbs_string_bridging.h" #define INTERN(str) \ rbs_constant_pool_insert_constant( \ @@ -11,11 +14,12 @@ strlen(str) \ ) -#define INTERN_TOKEN(parserstate, tok) \ - rb_intern3(\ - peek_token(parserstate->lexstate, tok),\ - token_bytes(tok),\ - rb_enc_get(parserstate->lexstate->string)\ +#define INTERN_TOKEN(parserstate, tok) \ + rbs_constant_pool_insert_shared_with_encoding( \ + &parserstate->constant_pool, \ + (const uint8_t *) peek_token(parserstate->lexstate, tok),\ + token_bytes(tok), \ + (void *) rb_enc_get(parserstate->lexstate->string) \ ) #define KEYWORD_CASES \ @@ -58,8 +62,8 @@ typedef struct { rbs_node_list_t *optional_positionals; rbs_node_t *rest_positionals; rbs_node_list_t *trailing_positionals; - VALUE required_keywords; - VALUE optional_keywords; + rbs_hash_t *required_keywords; + rbs_hash_t *optional_keywords; rbs_node_t *rest_keywords; } method_params; @@ -84,14 +88,6 @@ static rbs_location_t *rbs_location_current_token(parserstate *state) { static rbs_node_t *parse_optional(parserstate *state); static rbs_node_t *parse_simple(parserstate *state); -static VALUE string_of_loc(parserstate *state, position start, position end) { - return rb_enc_str_new( - RSTRING_PTR(state->lexstate->string) + start.byte_pos, - end.byte_pos - start.byte_pos, - rb_enc_get(state->lexstate->string) - ); -} - /** * Raises RuntimeError with "Unexpected error " message. * */ @@ -166,8 +162,8 @@ static rbs_typename_t *parse_type_name(parserstate *state, TypeNameKind kind, ra && state->current_token.range.end.byte_pos == state->next_token.range.start.byte_pos && state->next_token.range.end.byte_pos == state->next_token2.range.start.byte_pos ) { - VALUE symbol_value = ID2SYM(INTERN_TOKEN(state, state->current_token)); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbol_value); + rbs_constant_id_t symbol_value = INTERN_TOKEN(state, state->current_token); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, symbol_value); rbs_node_list_append(path, (rbs_node_t *)symbol); parser_advance(state); @@ -195,8 +191,8 @@ static rbs_typename_t *parse_type_name(parserstate *state, TypeNameKind kind, ra rg->end = state->current_token.range.end; } - VALUE name = ID2SYM(INTERN_TOKEN(state, state->current_token)); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, name); + rbs_constant_id_t name = INTERN_TOKEN(state, state->current_token); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, name); return rbs_typename_new(&state->allocator, namespace, symbol); } @@ -304,7 +300,14 @@ static rbs_types_function_param_t *parse_function_param(parserstate *state) { ); } - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, rb_to_symbol(rbs_unquote_string(state, state->current_token.range, 0))); + VALUE name_str = rbs_unquote_string(state, state->current_token.range, 0); + rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared( + &state->constant_pool, + (const uint8_t *) RSTRING_PTR(name_str), + RSTRING_LEN(name_str) + ); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); + rbs_location_t *loc = rbs_location_new(state->buffer, param_range); rbs_loc_alloc_children(loc, 1); rbs_loc_add_optional_child(loc, INTERN("name"), name_range); @@ -313,11 +316,12 @@ static rbs_types_function_param_t *parse_function_param(parserstate *state) { } } -static ID intern_token_start_end(parserstate *state, token start_token, token end_token) { - return rb_intern3( - peek_token(state->lexstate, start_token), +static rbs_constant_id_t intern_token_start_end(parserstate *state, token start_token, token end_token) { + return rbs_constant_pool_insert_shared_with_encoding( + &state->constant_pool, + (const uint8_t *) peek_token(state->lexstate, start_token), end_token.range.end.byte_pos - start_token.range.start.byte_pos, - rb_enc_get(state->lexstate->string) + (void *) rb_enc_get(state->lexstate->string) ); } @@ -328,34 +332,38 @@ static ID intern_token_start_end(parserstate *state, token start_token, token en static rbs_ast_symbol_t *parse_keyword_key(parserstate *state) { parser_advance(state); if (state->next_token.type == pQUESTION) { - rbs_ast_symbol_t *key = rbs_ast_symbol_new(&state->allocator, ID2SYM(intern_token_start_end(state, state->current_token, state->next_token))); + rbs_ast_symbol_t *key = rbs_ast_symbol_new( + &state->allocator, + &state->constant_pool, + intern_token_start_end(state, state->current_token, state->next_token) + ); parser_advance(state); return key; } else { - return rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); + return rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); } } /* keyword ::= {} keyword `:` */ -static void parse_keyword(parserstate *state, VALUE keywords, VALUE memo) { +static void parse_keyword(parserstate *state, rbs_hash_t *keywords, rbs_hash_t *memo) { rbs_ast_symbol_t *key = parse_keyword_key(state); - if (!NIL_P(rb_hash_aref(memo, ((rbs_node_t *)key)->cached_ruby_value))) { + if (rbs_hash_find(memo, (rbs_node_t *) key)) { raise_syntax_error( state, state->current_token, "duplicated keyword argument" ); } else { - rb_hash_aset(memo, ((rbs_node_t *)key)->cached_ruby_value, Qtrue); + rbs_hash_set(memo, (rbs_node_t *) key, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, true)); } parser_advance_assert(state, pCOLON); rbs_types_function_param_t *param = parse_function_param(state); - rb_hash_aset(keywords, ((rbs_node_t *)key)->cached_ruby_value, ((rbs_node_t *)param)->cached_ruby_value); + rbs_hash_set(keywords, (rbs_node_t *) key, (rbs_node_t *) param); return; } @@ -421,7 +429,7 @@ static void parse_params(parserstate *state, method_params *params) { return; } - VALUE memo = rb_hash_new(); + rbs_hash_t *memo = rbs_hash_new(&state->allocator); while (true) { switch (state->next_token.type) { @@ -601,8 +609,8 @@ static void initialize_method_params(method_params *params, rbs_allocator_t *all .optional_positionals = rbs_node_list_new(allocator), .rest_positionals = NULL, .trailing_positionals = rbs_node_list_new(allocator), - .required_keywords = rb_hash_new(), - .optional_keywords = rb_hash_new(), + .required_keywords = rbs_hash_new(allocator), + .optional_keywords = rbs_hash_new(allocator), .rest_keywords = NULL, }; } @@ -689,30 +697,16 @@ static parse_function_result parse_function(parserstate *state, bool accept_type if (rbs_is_untyped_params(&block_params)) { block_function = (rbs_node_t *) rbs_types_untypedfunction_new(&state->allocator, block_return_type); } else { - VALUE rest_positionals; - if (block_params.rest_positionals == NULL) { - rest_positionals = Qnil; - } else { - rest_positionals = block_params.rest_positionals->cached_ruby_value; - } - - VALUE rest_keywords; - if (block_params.rest_keywords == NULL) { - rest_keywords = Qnil; - } else { - rest_keywords = block_params.rest_keywords->cached_ruby_value; - } - block_function = (rbs_node_t *) rbs_types_function_new( &state->allocator, - block_params.required_positionals->cached_ruby_value, - block_params.optional_positionals->cached_ruby_value, - rest_positionals, - block_params.trailing_positionals->cached_ruby_value, + block_params.required_positionals, + block_params.optional_positionals, + block_params.rest_positionals, + block_params.trailing_positionals, block_params.required_keywords, block_params.optional_keywords, - rest_keywords, - block_return_type->cached_ruby_value + block_params.rest_keywords, + block_return_type ); } @@ -727,30 +721,16 @@ static parse_function_result parse_function(parserstate *state, bool accept_type if (rbs_is_untyped_params(¶ms)) { function = (rbs_node_t *) rbs_types_untypedfunction_new(&state->allocator, type); } else { - VALUE rest_positionals; - if (params.rest_positionals == NULL) { - rest_positionals = Qnil; - } else { - rest_positionals = params.rest_positionals->cached_ruby_value; - } - - VALUE rest_keywords; - if (params.rest_keywords == NULL) { - rest_keywords = Qnil; - } else { - rest_keywords = params.rest_keywords->cached_ruby_value; - } - function = (rbs_node_t *) rbs_types_function_new( &state->allocator, - params.required_positionals->cached_ruby_value, - params.optional_positionals->cached_ruby_value, - rest_positionals, - params.trailing_positionals->cached_ruby_value, + params.required_positionals, + params.optional_positionals, + params.rest_positionals, + params.trailing_positionals, params.required_keywords, params.optional_keywords, - rest_keywords, - type->cached_ruby_value + params.rest_keywords, + type ); } @@ -772,8 +752,8 @@ static rbs_types_proc_t *parse_proc_type(parserstate *state) { return rbs_types_proc_new(&state->allocator, result.function, result.block, loc, result.function_self_type); } -static void check_key_duplication(parserstate *state, VALUE fields, rbs_ast_symbol_t *key) { - if (!NIL_P(rb_hash_aref(fields, ((rbs_node_t *)key)->cached_ruby_value))) { +static void check_key_duplication(parserstate *state, rbs_hash_t *fields, rbs_node_t *key) { + if (rbs_hash_find(fields, ((rbs_node_t *) key))) { raise_syntax_error( state, state->current_token, @@ -792,16 +772,15 @@ static void check_key_duplication(parserstate *state, VALUE fields, rbs_ast_symb record_attribute ::= {} keyword_token `:` | {} literal_type `=>` */ -static VALUE parse_record_attributes(parserstate *state) { - VALUE fields = rb_hash_new(); +static rbs_hash_t *parse_record_attributes(parserstate *state) { + rbs_hash_t *fields = rbs_hash_new(&state->allocator); if (state->next_token.type == pRBRACE) { return fields; } while (true) { - rbs_ast_symbol_t *key; - VALUE value = rb_ary_new(); + rbs_node_t *key; bool required = true; if (state->next_token.type == pQUESTION) { @@ -812,7 +791,7 @@ static VALUE parse_record_attributes(parserstate *state) { if (is_keyword(state)) { // { foo: type } syntax - key = parse_keyword_key(state); + key = (rbs_node_t *) parse_keyword_key(state); check_key_duplication(state, fields, key); parser_advance_assert(state, pCOLON); } else { @@ -826,7 +805,7 @@ static VALUE parse_record_attributes(parserstate *state) { case tINTEGER: case kTRUE: case kFALSE: { - key = rbs_ast_symbol_new(&state->allocator, rb_funcall(rbs_struct_to_ruby_value(parse_simple(state)), rb_intern("literal"), 0)); + key = (rbs_node_t *) ((rbs_types_literal_t *) parse_simple(state))->literal; break; } default: @@ -840,9 +819,7 @@ static VALUE parse_record_attributes(parserstate *state) { parser_advance_assert(state, pFATARROW); } rbs_node_t *type = parse_type(state); - rb_ary_push(value, type->cached_ruby_value); - rb_ary_push(value, rbs_ast_bool_new(&state->allocator, required)->base.cached_ruby_value); - rb_hash_aset(fields, ((rbs_node_t *)key)->cached_ruby_value, value); + rbs_hash_set(fields, (rbs_node_t *) key, (rbs_node_t *) rbs_types_record_fieldtype_new(&state->allocator, type, required)); if (parser_advance_if(state, pCOMMA)) { if (state->next_token.type == pRBRACE) { @@ -871,26 +848,30 @@ static rbs_types_literal_t *parse_symbol(parserstate *state, rbs_location_t *loc { case tSYMBOL: { char *buffer = peek_token(state->lexstate, state->current_token); - literal = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern3(buffer+offset_bytes, bytes, enc))); + rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared( + &state->constant_pool, + (const uint8_t *) buffer+offset_bytes, + bytes + ); + literal = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); break; } case tDQSYMBOL: case tSQSYMBOL: { - literal = rbs_ast_symbol_new( - &state->allocator, - rb_funcall( - rbs_unquote_string(state, state->current_token.range, offset_bytes), - rb_intern("to_sym"), - 0 - ) + VALUE ruby_str = rbs_unquote_string(state, state->current_token.range, offset_bytes); + rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared( + &state->constant_pool, + (const uint8_t *) RSTRING_PTR(ruby_str), + RSTRING_LEN(ruby_str) ); + literal = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); break; } default: rbs_abort(); } - return rbs_types_literal_new(&state->allocator, ((rbs_node_t *)literal)->cached_ruby_value, location); + return rbs_types_literal_new(&state->allocator, (rbs_node_t *) literal, location); } /* @@ -1029,33 +1010,45 @@ static rbs_node_t *parse_simple(parserstate *state) { } case kUNTYPED: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, Qfalse, loc); + return (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, false, loc); } case k__TODO__: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, Qtrue, loc); + return (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, true, loc); } case tINTEGER: { rbs_location_t *loc = rbs_location_current_token(state); - VALUE literal = rb_funcall( - string_of_loc(state, state->current_token.range.start, state->current_token.range.end), - rb_intern("to_i"), - 0 - ); + + rbs_string_t string = rbs_string_from_ruby_string(state->lexstate->string); + rbs_string_drop_first(&string, state->current_token.range.start.byte_pos); + rbs_string_limit_length(&string, state->current_token.range.end.byte_pos - state->current_token.range.start.byte_pos); + rbs_string_strip_whitespace(&string); + rbs_string_ensure_owned(&string); + + rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(&state->allocator, string); return (rbs_node_t *) rbs_types_literal_new(&state->allocator, literal, loc); } case kTRUE: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_literal_new(&state->allocator, Qtrue, loc); + return (rbs_node_t *) rbs_types_literal_new(&state->allocator, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, true), loc); } case kFALSE: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_literal_new(&state->allocator, Qfalse, loc); + return (rbs_node_t *) rbs_types_literal_new(&state->allocator, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, false), loc); } case tSQSTRING: case tDQSTRING: { rbs_location_t *loc = rbs_location_current_token(state); - VALUE literal = rbs_unquote_string(state, state->current_token.range, 0); + + rbs_string_t string = rbs_string_from_ruby_string(state->lexstate->string); + rbs_string_drop_first(&string, state->current_token.range.start.byte_pos); + rbs_string_limit_length(&string, state->current_token.range.end.byte_pos - state->current_token.range.start.byte_pos); + rbs_string_ensure_owned(&string); + + rbs_string_t unquoted_str = rbs_unquote_string2(string); + rbs_string_ensure_owned(&unquoted_str); + + rbs_node_t *literal = (rbs_node_t *) rbs_ast_string_new(&state->allocator, unquoted_str); return (rbs_node_t *) rbs_types_literal_new(&state->allocator, literal, loc); } case tSYMBOL: @@ -1071,9 +1064,8 @@ static rbs_node_t *parse_simple(parserstate *state) { rbs_constant_id_t name = rbs_constant_pool_find(&state->constant_pool, (const uint8_t *) name_str, name_len); if (parser_typevar_member(state, name)) { - ID name = rb_intern3(name_str, name_len, rb_enc_get(state->lexstate->string)); rbs_location_t *loc = rbs_location_current_token(state); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, ID2SYM(name)); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, name); return (rbs_node_t *) rbs_types_variable_new(&state->allocator, symbol, loc); } // fallthrough for type name @@ -1106,7 +1098,7 @@ static rbs_node_t *parse_simple(parserstate *state) { } case pLBRACE: { position start = state->current_token.range.start; - VALUE fields = parse_record_attributes(state); + rbs_hash_t *fields = parse_record_attributes(state); parser_advance_assert(state, pRBRACE); position end = state->current_token.range.end; rbs_location_t *loc = rbs_location_pp(state->buffer, &start, &end); @@ -1198,7 +1190,7 @@ static rbs_node_list_t *parse_type_params(parserstate *state, range *rg, bool mo while (true) { bool unchecked = false; - rbs_ast_symbol_t *variance = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("invariant"))); + rbs_keyword_t *variance = rbs_keyword_new(&state->allocator, INTERN("invariant")); rbs_node_t *upper_bound = NULL; rbs_node_t *default_type = NULL; @@ -1217,10 +1209,10 @@ static rbs_node_list_t *parse_type_params(parserstate *state, range *rg, bool mo if (state->next_token.type == kIN || state->next_token.type == kOUT) { switch (state->next_token.type) { case kIN: - variance = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("contravariant"))); + variance = rbs_keyword_new(&state->allocator, INTERN("contravariant")); break; case kOUT: - variance = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("covariant"))); + variance = rbs_keyword_new(&state->allocator, INTERN("covariant")); break; default: rbs_abort(); @@ -1240,8 +1232,7 @@ static rbs_node_list_t *parse_type_params(parserstate *state, range *rg, bool mo token_bytes(state->current_token) ); - VALUE name_sym = ID2SYM(INTERN_TOKEN(state, state->current_token)); - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, name_sym); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, id); parser_insert_typevar(state, id); @@ -1303,13 +1294,6 @@ static rbs_node_list_t *parse_type_params(parserstate *state, range *rg, bool mo *rg = NULL_RANGE; } - rb_funcall( - RBS_AST_TypeParam, - rb_intern("resolve_variables"), - 1, - params->cached_ruby_value - ); - return params; } @@ -1350,9 +1334,10 @@ static rbs_ast_declarations_global_t *parse_global_decl(parserstate *state) { range decl_range; decl_range.start = state->current_token.range.start; - VALUE comment = get_comment(state, decl_range.start.line); + rbs_ast_comment_t *comment = get_comment(state, decl_range.start.line); + range name_range = state->current_token.range; - rbs_ast_symbol_t *typename = rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); + rbs_ast_symbol_t *typename = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); parser_advance_assert(state, pCOLON); range colon_range = state->current_token.range; @@ -1375,7 +1360,7 @@ static rbs_ast_declarations_constant_t *parse_const_decl(parserstate *state) { range decl_range; decl_range.start = state->current_token.range.start; - VALUE comment = get_comment(state, decl_range.start.line); + rbs_ast_comment_t *comment = get_comment(state, decl_range.start.line); range name_range; rbs_typename_t *typename = parse_type_name(state, CLASS_NAME, &name_range); @@ -1429,7 +1414,7 @@ static rbs_ast_declarations_typealias_t *parse_type_decl(parserstate *state, pos parser_pop_typevar_table(state); - VALUE comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); return rbs_ast_declarations_typealias_new(&state->allocator, typename, type_params, type, annotations, loc, comment); } @@ -1475,13 +1460,11 @@ static rbs_ast_annotation_t *parse_annotation(parserstate *state) { int open_bytes = rb_enc_codelen(open_char, enc); int close_bytes = rb_enc_codelen(close_char, enc); - char *buffer = RSTRING_PTR(state->lexstate->string) + rg.start.byte_pos + offset_bytes + open_bytes; - VALUE string = rb_enc_str_new( - buffer, - rg.end.byte_pos - rg.start.byte_pos - offset_bytes - open_bytes - close_bytes, - enc - ); - rb_funcall(string, rb_intern("strip!"), 0); + rbs_string_t string = rbs_string_from_ruby_string(state->lexstate->string); + rbs_string_drop_first(&string, rg.start.byte_pos + offset_bytes + open_bytes); + rbs_string_limit_length(&string, rg.end.byte_pos - rg.start.byte_pos - offset_bytes - open_bytes - close_bytes); + rbs_string_strip_whitespace(&string); + rbs_string_ensure_owned(&string); rbs_location_t *loc = rbs_location_new(state->buffer, rg); return rbs_ast_annotation_new(&state->allocator, string, loc); @@ -1528,26 +1511,34 @@ static rbs_ast_symbol_t *parse_method_name(parserstate *state, range *range) { range->end = state->next_token.range.end; parser_advance(state); - ID id = rb_intern3( - RSTRING_PTR(state->lexstate->string) + range->start.byte_pos, - range->end.byte_pos - range->start.byte_pos, - rb_enc_get(state->lexstate->string) + rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared( + &state->constant_pool, + (const uint8_t *) RSTRING_PTR(state->lexstate->string) + range->start.byte_pos, + range->end.byte_pos - range->start.byte_pos + // rb_enc_get(state->lexstate->string) // FIXME: Add encoding back ); - return rbs_ast_symbol_new(&state->allocator, ID2SYM(id)); + return rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); } else { *range = state->current_token.range; - return rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); + return rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); } case tBANGIDENT: case tEQIDENT: { *range = state->current_token.range; - return rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); + return rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); } case tQIDENT: { - return rbs_ast_symbol_new(&state->allocator, rb_to_symbol(rbs_unquote_string(state, state->current_token.range, 0))); + VALUE ruby_str = rbs_unquote_string(state, state->current_token.range, 0); + rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared( + &state->constant_pool, + (const uint8_t *) RSTRING_PTR(ruby_str), + RSTRING_LEN(ruby_str) + ); + return rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); } + case pBAR: case pHAT: case pAMP: @@ -1555,9 +1546,10 @@ static rbs_ast_symbol_t *parse_method_name(parserstate *state, range *range) { case pSTAR2: case pLT: case pAREF_OPR: - case tOPERATOR: + case tOPERATOR: { *range = state->current_token.range; - return rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); + return rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + } default: raise_syntax_error( @@ -1630,22 +1622,22 @@ static rbs_ast_members_methoddefinition_t *parse_member_def(parserstate *state, member_range.start = state->current_token.range.start; comment_pos = nonnull_pos_or(comment_pos, member_range.start); - VALUE comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); range visibility_range; - rbs_ast_symbol_t *visibility; + rbs_keyword_t *visibility; switch (state->current_token.type) { case kPRIVATE: { visibility_range = state->current_token.range; - visibility = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("private"))); + visibility = rbs_keyword_new(&state->allocator, INTERN("private")); member_range.start = visibility_range.start; parser_advance(state); break; } case kPUBLIC: { visibility_range = state->current_token.range; - visibility = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("public"))); + visibility = rbs_keyword_new(&state->allocator, INTERN("public")); member_range.start = visibility_range.start; parser_advance(state); break; @@ -1670,7 +1662,9 @@ static rbs_ast_members_methoddefinition_t *parse_member_def(parserstate *state, range name_range; rbs_ast_symbol_t *name = parse_method_name(state, &name_range); - if (state->next_token.type == pDOT && RB_SYM2ID(((rbs_node_t *)name)->cached_ruby_value) == rb_intern("self?")) { + #define SELF_ID rbs_constant_pool_insert_constant(&state->constant_pool, (const unsigned char *) "self?", strlen("self?")) + + if (state->next_token.type == pDOT && name->constant_id == SELF_ID) { raise_syntax_error( state, state->next_token, @@ -1741,17 +1735,20 @@ static rbs_ast_members_methoddefinition_t *parse_member_def(parserstate *state, parser_pop_typevar_table(state); - rbs_ast_symbol_t *k; + rbs_keyword_t *k; switch (kind) { - case INSTANCE_KIND: - k = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("instance"))); + case INSTANCE_KIND: { + k = rbs_keyword_new(&state->allocator, INTERN("instance")); break; - case SINGLETON_KIND: - k = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("singleton"))); + } + case SINGLETON_KIND: { + k = rbs_keyword_new(&state->allocator, INTERN("singleton")); break; - case INSTANCE_SINGLETON_KIND: - k = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("singleton_instance"))); + } + case INSTANCE_SINGLETON_KIND: { + k = rbs_keyword_new(&state->allocator, INTERN("singleton_instance")); break; + } default: rbs_abort(); } @@ -1853,7 +1850,7 @@ static rbs_node_t *parse_mixin_member(parserstate *state, bool from_interface, p rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); - VALUE comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); switch (type) { case kINCLUDE: @@ -1881,13 +1878,14 @@ static rbs_ast_members_alias_t *parse_alias_member(parserstate *state, bool inst range keyword_range = state->current_token.range; comment_pos = nonnull_pos_or(comment_pos, member_range.start); - VALUE comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); - rbs_ast_symbol_t *kind, *new_name, *old_name; + rbs_keyword_t *kind; + rbs_ast_symbol_t *new_name, *old_name; range new_kind_range, old_kind_range, new_name_range, old_name_range; if (!instance_only && state->next_token.type == kSELF) { - kind = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("singleton"))); + kind = rbs_keyword_new(&state->allocator, INTERN("singleton")); new_kind_range.start = state->next_token.range.start; new_kind_range.end = state->next_token2.range.end; @@ -1901,7 +1899,7 @@ static rbs_ast_members_alias_t *parse_alias_member(parserstate *state, bool inst parser_advance_assert(state, pDOT); old_name = parse_method_name(state, &old_name_range); } else { - kind = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("instance"))); + kind = rbs_keyword_new(&state->allocator, INTERN("instance")); new_name = parse_method_name(state, &new_name_range); old_name = parse_method_name(state, &old_name_range); @@ -1938,13 +1936,13 @@ static rbs_node_t *parse_variable_member(parserstate *state, position comment_po range member_range; member_range.start = state->current_token.range.start; comment_pos = nonnull_pos_or(comment_pos, member_range.start); - VALUE comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); switch (state->current_token.type) { case tAIDENT: { range name_range = state->current_token.range; - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); parser_advance_assert(state, pCOLON); range colon_range = state->current_token.range; @@ -1962,7 +1960,7 @@ static rbs_node_t *parse_variable_member(parserstate *state, position comment_po } case tA2IDENT: { range name_range = state->current_token.range; - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); parser_advance_assert(state, pCOLON); range colon_range = state->current_token.range; @@ -1990,7 +1988,7 @@ static rbs_node_t *parse_variable_member(parserstate *state, position comment_po parser_advance_assert(state, tAIDENT); range name_range = state->current_token.range; - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); parser_advance_assert(state, pCOLON); range colon_range = state->current_token.range; @@ -2058,22 +2056,24 @@ static rbs_node_t *parse_attribute_member(parserstate *state, position comment_p member_range.start = state->current_token.range.start; comment_pos = nonnull_pos_or(comment_pos, member_range.start); - VALUE comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); - rbs_ast_symbol_t *visibility; + rbs_keyword_t *visibility; range visibility_range; switch (state->current_token.type) { - case kPRIVATE: - visibility = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("private"))); + case kPRIVATE: { + visibility = rbs_keyword_new(&state->allocator, INTERN("private")); visibility_range = state->current_token.range; parser_advance(state); break; - case kPUBLIC: - visibility = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern("public"))); + } + case kPUBLIC: { + visibility = rbs_keyword_new(&state->allocator, INTERN("public")); visibility_range = state->current_token.range; parser_advance(state); break; + } default: visibility = NULL; visibility_range = NULL_RANGE; @@ -2085,19 +2085,19 @@ static rbs_node_t *parse_attribute_member(parserstate *state, position comment_p range kind_range; InstanceSingletonKind is_kind = parse_instance_singleton_kind(state, false, &kind_range); - rbs_ast_symbol_t *kind = rbs_ast_symbol_new(&state->allocator, ID2SYM(rb_intern((is_kind == INSTANCE_KIND) ? "instance" : "singleton"))); + rbs_keyword_t *kind = rbs_keyword_new(&state->allocator, INTERN(((is_kind == INSTANCE_KIND) ? "instance" : "singleton"))); range name_range; rbs_ast_symbol_t *attr_name = parse_method_name(state, &name_range); - rbs_node_t *ivar_name; // rbs_ast_symbol_t, NULL or rbs_ast_bool_new(false) + rbs_node_t *ivar_name; // rbs_ast_symbol_t, NULL or rbs_ast_bool_new(&state->allocator, false) range ivar_range, ivar_name_range; if (state->next_token.type == pLPAREN) { parser_advance_assert(state, pLPAREN); ivar_range.start = state->current_token.range.start; if (parser_advance_if(state, tAIDENT)) { - ivar_name = (rbs_node_t *) rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); + ivar_name = (rbs_node_t *) rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); ivar_name_range = state->current_token.range; } else { ivar_name = (rbs_node_t *) rbs_ast_bool_new(&state->allocator, false); @@ -2229,7 +2229,7 @@ static rbs_ast_declarations_interface_t *parse_interface_decl(parserstate *state rbs_loc_add_required_child(loc, INTERN("end"), end_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range); - VALUE comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); return rbs_ast_declarations_interface_new(&state->allocator, name, type_params, members, annotations, loc, comment); } @@ -2372,7 +2372,7 @@ static rbs_node_list_t *parse_module_members(parserstate *state) { module_decl ::= {module_name} module_type_params module_members | {module_name} module_name module_type_params `:` module_self_types module_members */ -static rbs_ast_declarations_module_t *parse_module_decl0(parserstate *state, range keyword_range, rbs_typename_t *module_name, range name_range, VALUE comment, rbs_node_list_t *annotations) { +static rbs_ast_declarations_module_t *parse_module_decl0(parserstate *state, range keyword_range, rbs_typename_t *module_name, range name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations) { parser_push_typevar_table(state, true); range decl_range; @@ -2424,7 +2424,7 @@ static rbs_node_t *parse_module_decl(parserstate *state, position comment_pos, r range keyword_range = state->current_token.range; comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start); - VALUE comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); parser_advance(state); range module_name_range; @@ -2489,7 +2489,7 @@ static rbs_ast_declarations_class_super_t *parse_class_decl_super(parserstate *s /* class_decl ::= {class_name} type_params class_decl_super class_members <`end`> */ -static rbs_ast_declarations_class_t *parse_class_decl0(parserstate *state, range keyword_range, rbs_typename_t *name, range name_range, VALUE comment, rbs_node_list_t *annotations) { +static rbs_ast_declarations_class_t *parse_class_decl0(parserstate *state, range keyword_range, rbs_typename_t *name, range name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations) { parser_push_typevar_table(state, true); range decl_range; @@ -2529,7 +2529,7 @@ static rbs_node_t *parse_class_decl(parserstate *state, position comment_pos, rb range keyword_range = state->current_token.range; comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start); - VALUE comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); parser_advance(state); range class_name_range; @@ -2669,8 +2669,7 @@ static rbs_namespace_t *parse_namespace(parserstate *state, range *rg) { while (true) { if (state->next_token.type == tUIDENT && state->next_token2.type == pCOLON2) { - VALUE symbol_value = ID2SYM(INTERN_TOKEN(state, state->next_token)); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbol_value); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->next_token)); rbs_node_list_append(path, (rbs_node_t *)symbol); if (null_position_p(rg->start)) { rg->start = state->next_token.range.start; @@ -2711,7 +2710,7 @@ static void parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { ? state->current_token.range : (range) { .start = namespace_range.start, .end = state->current_token.range.end }; - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); rbs_typename_t *type_name = rbs_typename_new(&state->allocator, namespace, symbol); range keyword_range = NULL_RANGE; @@ -2726,7 +2725,7 @@ static void parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { if (ident_type == tLIDENT) parser_advance_assert(state, tLIDENT); if (ident_type == tULIDENT) parser_advance_assert(state, tULIDENT); - new_name = rbs_ast_symbol_new(&state->allocator, ID2SYM(INTERN_TOKEN(state, state->current_token))); + new_name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); new_name_range = state->current_token.range; clause_range.end = new_name_range.end; } @@ -2803,31 +2802,25 @@ static rbs_ast_directives_use_t *parse_use_directive(parserstate *state) { } } -rbs_node_list_t *parse_signature(parserstate *state) { - rbs_node_list_t *dir_nodes = rbs_node_list_new(&state->allocator); - rbs_ast_directives_nodes_t *dirs = rbs_ast_directives_nodes_new(&state->allocator, dir_nodes->cached_ruby_value, dir_nodes); - - rbs_node_list_t *decl_nodes = rbs_node_list_new(&state->allocator); - rbs_ast_declarations_nodes_t *decls = rbs_ast_declarations_nodes_new(&state->allocator, decl_nodes->cached_ruby_value, decl_nodes); +rbs_signature_t *parse_signature(parserstate *state) { + rbs_node_list_t *dirs = rbs_node_list_new(&state->allocator); + rbs_node_list_t *decls = rbs_node_list_new(&state->allocator); while (state->next_token.type == kUSE) { rbs_ast_directives_use_t *use_node = parse_use_directive(state); if (use_node == NULL) { - rbs_node_list_append(dir_nodes, NULL); + rbs_node_list_append(dirs, NULL); } else { - rbs_node_list_append(dir_nodes, (rbs_node_t *)use_node); + rbs_node_list_append(dirs, (rbs_node_t *)use_node); } } while (state->next_token.type != pEOF) { rbs_node_t *decl = parse_decl(state); - rbs_node_list_append(decl_nodes, decl); + rbs_node_list_append(decls, decl); } - rbs_node_list_t *ret = rbs_node_list_new(&state->allocator); - rbs_node_list_append(ret, (rbs_node_t *)dirs); - rbs_node_list_append(ret, (rbs_node_t *)decls); - return ret; + return rbs_signature_new(&state->allocator, dirs, decls); } struct parse_type_arg { @@ -2844,18 +2837,23 @@ ensure_free_parser(VALUE parser) { static VALUE parse_type_try(VALUE a) { struct parse_type_arg *arg = (struct parse_type_arg *)a; + parserstate *parser = arg->parser; - if (arg->parser->next_token.type == pEOF) { + if (parser->next_token.type == pEOF) { return Qnil; } - rbs_node_t *type = parse_type(arg->parser); + rbs_node_t *type = parse_type(parser); if (RB_TEST(arg->require_eof)) { - parser_advance_assert(arg->parser, pEOF); + parser_advance_assert(parser, pEOF); } - return type->cached_ruby_value; + rbs_translation_context_t ctx = { + .constant_pool = &parser->constant_pool, + }; + + return rbs_struct_to_ruby_value(ctx, type); } static VALUE @@ -2874,18 +2872,23 @@ rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, V static VALUE parse_method_type_try(VALUE a) { struct parse_type_arg *arg = (struct parse_type_arg *)a; + parserstate *parser = arg->parser; - if (arg->parser->next_token.type == pEOF) { + if (parser->next_token.type == pEOF) { return Qnil; } - rbs_methodtype_t *method_type = parse_method_type(arg->parser); + rbs_methodtype_t *method_type = parse_method_type(parser); if (RB_TEST(arg->require_eof)) { - parser_advance_assert(arg->parser, pEOF); + parser_advance_assert(parser, pEOF); } - return rbs_struct_to_ruby_value((rbs_node_t *)method_type); + rbs_translation_context_t ctx = { + .constant_pool = &parser->constant_pool, + }; + + return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type); } static VALUE @@ -2904,7 +2907,14 @@ rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end static VALUE parse_signature_try(VALUE a) { parserstate *parser = (parserstate *)a; - return parse_signature(parser)->cached_ruby_value; + + rbs_signature_t *signature = parse_signature(parser); + + rbs_translation_context_t ctx = { + .constant_pool = &parser->constant_pool, + }; + + return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature); } static VALUE diff --git a/ext/rbs_extension/parser.h b/ext/rbs_extension/parser.h index 4d8b3de6e..16b84987c 100644 --- a/ext/rbs_extension/parser.h +++ b/ext/rbs_extension/parser.h @@ -11,7 +11,7 @@ extern VALUE RBS_Parser; rbs_node_t *parse_type(parserstate *state); rbs_methodtype_t *parse_method_type(parserstate *state); -rbs_node_list_t *parse_signature(parserstate *state); +rbs_signature_t *parse_signature(parserstate *state); void rbs__init_parser(); diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index ca04f41d9..d0ce6ab97 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -175,15 +175,15 @@ void insert_comment_line(parserstate *state, token tok) { } } -VALUE get_comment(parserstate *state, int subject_line) { +rbs_ast_comment_t *get_comment(parserstate *state, int subject_line) { int comment_line = subject_line - 1; comment *com = comment_get_comment(state->last_comment, comment_line); if (com) { - return comment_to_ruby(com, state->buffer); + return comment_to_ruby(&state->allocator, com, state->buffer); } else { - return Qnil; + return NULL; } } @@ -243,7 +243,7 @@ comment *comment_get_comment(comment *com, int line) { return comment_get_comment(com->next_comment, line); } -VALUE comment_to_ruby(comment *com, VALUE buffer) { +rbs_ast_comment_t *comment_to_ruby(rbs_allocator_t *allocator, comment *com, VALUE buffer) { VALUE content = rb_funcall(buffer, rb_intern("content"), 0); rb_encoding *enc = rb_enc_get(content); VALUE string = rb_enc_str_new_cstr("", enc); @@ -267,7 +267,8 @@ VALUE comment_to_ruby(comment *com, VALUE buffer) { rb_str_cat_cstr(string, "\n"); } - return rbs_ast_comment( + return rbs_ast_comment_new( + allocator, string, rbs_location_pp(buffer, &com->start, &com->end) ); diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index ebfb0a498..968f346ad 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -7,6 +7,7 @@ #include "rbs/util/rbs_constant_pool.h" #include "lexer.h" #include "location.h" +#include "rbs/ast.h" /** * id_table represents a set of RBS constant IDs. @@ -65,7 +66,7 @@ typedef struct { comment *alloc_comment(rbs_allocator_t *, token comment_token, comment *last_comment); void comment_insert_new_line(rbs_allocator_t *, comment *com, token comment_token); comment *comment_get_comment(comment *com, int line); -VALUE comment_to_ruby(comment *com, VALUE buffer); +rbs_ast_comment_t *comment_to_ruby(rbs_allocator_t *, comment *com, VALUE buffer); /** * Insert new table entry. @@ -160,6 +161,6 @@ void insert_comment_line(parserstate *state, token token); * end * ``` * */ -VALUE get_comment(parserstate *state, int subject_line); +rbs_ast_comment_t *get_comment(parserstate *state, int subject_line); #endif diff --git a/ext/rbs_extension/rbs_string_bridging.c b/ext/rbs_extension/rbs_string_bridging.c new file mode 100644 index 000000000..6d9704104 --- /dev/null +++ b/ext/rbs_extension/rbs_string_bridging.c @@ -0,0 +1,17 @@ +#include "rbs_string_bridging.h" + +rbs_string_t rbs_string_from_ruby_string(VALUE ruby_string) { + rb_gc_register_mark_object(ruby_string); + + rbs_string_t s = rbs_string_shared_new(StringValueCStr(ruby_string), RSTRING_END(ruby_string)); + + rbs_string_ensure_owned(&s); // Copy out the string so we don't need the Ruby object to stay alive. + + return s; +} + +VALUE rbs_string_to_ruby_string(rbs_string_t *self) { + VALUE str = rb_str_new_static(self->start, rbs_string_len(*self)); + // rb_enc_associate(str, self->encoding); + return str; +} diff --git a/ext/rbs_extension/rbs_string_bridging.h b/ext/rbs_extension/rbs_string_bridging.h new file mode 100644 index 000000000..b8e11c508 --- /dev/null +++ b/ext/rbs_extension/rbs_string_bridging.h @@ -0,0 +1,17 @@ +#ifndef RBS__RBS_STRING_BRIDGING_H +#define RBS__RBS_STRING_BRIDGING_H + +#include "ruby.h" +#include "rbs/rbs_string.h" + +/** + * Returns a new shared rbs_string_t from the given Ruby string. + */ +rbs_string_t rbs_string_from_ruby_string(VALUE ruby_string); + +/** + * Returns a new Ruby string from the given rbs_string_t. + */ +VALUE rbs_string_to_ruby_string(rbs_string_t *self); + +#endif diff --git a/ext/rbs_extension/unescape.c b/ext/rbs_extension/unescape.c index f78661e4d..0efdf8ad3 100644 --- a/ext/rbs_extension/unescape.c +++ b/ext/rbs_extension/unescape.c @@ -18,7 +18,7 @@ VALUE rbs_unquote_string(parserstate *state, range rg, int offset_bytes) { byte_length -= 2 * bs; } - char *buffer = RSTRING_PTR(state->lexstate->string) + rg.start.byte_pos + offset_bytes; + char *buffer = RSTRING_PTR(string) + rg.start.byte_pos + offset_bytes; VALUE str = rb_enc_str_new(buffer, byte_length, enc); return rb_funcall( @@ -29,4 +29,3 @@ VALUE rbs_unquote_string(parserstate *state, range rg, int offset_bytes) { first_char == '\"' ? Qtrue : Qfalse ); } - diff --git a/include/rbs.h b/include/rbs.h index 0f105eae0..3660481a5 100644 --- a/include/rbs.h +++ b/include/rbs.h @@ -3,6 +3,5 @@ #include "rbs/ast.h" #include "rbs/constants.h" -#include "rbs/ruby_objs.h" #endif diff --git a/include/rbs/ast.h b/include/rbs/ast.h index e5936b590..1f38764c9 100644 --- a/include/rbs/ast.h +++ b/include/rbs/ast.h @@ -10,9 +10,12 @@ #include "ruby.h" #include "rbs/util/rbs_allocator.h" +#include "rbs/util/rbs_constant_pool.h" +#include "rbs_string.h" #include "rbs_location.h" enum rbs_node_type { + RBS_OTHER_RUBY_VALUE = 0, RBS_AST_ANNOTATION = 1, RBS_AST_BOOL = 2, RBS_AST_COMMENT = 3, @@ -25,30 +28,30 @@ enum rbs_node_type { RBS_AST_DECLARATIONS_MODULE = 10, RBS_AST_DECLARATIONS_MODULE_SELF = 11, RBS_AST_DECLARATIONS_MODULEALIAS = 12, - RBS_AST_DECLARATIONS_NODES = 13, - RBS_AST_DECLARATIONS_TYPEALIAS = 14, - RBS_AST_DIRECTIVES_NODES = 15, - RBS_AST_DIRECTIVES_USE = 16, - RBS_AST_DIRECTIVES_USE_SINGLECLAUSE = 17, - RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE = 18, - RBS_AST_MEMBERS_ALIAS = 19, - RBS_AST_MEMBERS_ATTRACCESSOR = 20, - RBS_AST_MEMBERS_ATTRREADER = 21, - RBS_AST_MEMBERS_ATTRWRITER = 22, - RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE = 23, - RBS_AST_MEMBERS_CLASSVARIABLE = 24, - RBS_AST_MEMBERS_EXTEND = 25, - RBS_AST_MEMBERS_INCLUDE = 26, - RBS_AST_MEMBERS_INSTANCEVARIABLE = 27, - RBS_AST_MEMBERS_METHODDEFINITION = 28, - RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD = 29, - RBS_AST_MEMBERS_PREPEND = 30, - RBS_AST_MEMBERS_PRIVATE = 31, - RBS_AST_MEMBERS_PUBLIC = 32, - RBS_AST_SYMBOL = 33, - RBS_AST_TYPEPARAM = 34, - RBS_METHODTYPE = 35, - RBS_NAMESPACE = 36, + RBS_AST_DECLARATIONS_TYPEALIAS = 13, + RBS_AST_DIRECTIVES_USE = 14, + RBS_AST_DIRECTIVES_USE_SINGLECLAUSE = 15, + RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE = 16, + RBS_AST_INTEGER = 17, + RBS_AST_MEMBERS_ALIAS = 18, + RBS_AST_MEMBERS_ATTRACCESSOR = 19, + RBS_AST_MEMBERS_ATTRREADER = 20, + RBS_AST_MEMBERS_ATTRWRITER = 21, + RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE = 22, + RBS_AST_MEMBERS_CLASSVARIABLE = 23, + RBS_AST_MEMBERS_EXTEND = 24, + RBS_AST_MEMBERS_INCLUDE = 25, + RBS_AST_MEMBERS_INSTANCEVARIABLE = 26, + RBS_AST_MEMBERS_METHODDEFINITION = 27, + RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD = 28, + RBS_AST_MEMBERS_PREPEND = 29, + RBS_AST_MEMBERS_PRIVATE = 30, + RBS_AST_MEMBERS_PUBLIC = 31, + RBS_AST_STRING = 32, + RBS_AST_TYPEPARAM = 33, + RBS_METHODTYPE = 34, + RBS_NAMESPACE = 35, + RBS_SIGNATURE = 36, RBS_TYPENAME = 37, RBS_TYPES_ALIAS = 38, RBS_TYPES_BASES_ANY = 39, @@ -71,17 +74,24 @@ enum rbs_node_type { RBS_TYPES_OPTIONAL = 56, RBS_TYPES_PROC = 57, RBS_TYPES_RECORD = 58, - RBS_TYPES_TUPLE = 59, - RBS_TYPES_UNION = 60, - RBS_TYPES_UNTYPEDFUNCTION = 61, - RBS_TYPES_VARIABLE = 62, + RBS_TYPES_RECORD_FIELDTYPE = 59, + RBS_TYPES_TUPLE = 60, + RBS_TYPES_UNION = 61, + RBS_TYPES_UNTYPEDFUNCTION = 62, + RBS_TYPES_VARIABLE = 63, + RBS_KEYWORD, + RBS_AST_SYMBOL, }; typedef struct rbs_node { - VALUE cached_ruby_value; enum rbs_node_type type; } rbs_node_t; +/// A bag of values needed when copying RBS C structs into Ruby objects. +typedef struct rbs_translation_context { + rbs_constant_pool_t *constant_pool; +} rbs_translation_context_t; + /* rbs_node_list_node */ typedef struct rbs_node_list_node { @@ -94,7 +104,6 @@ typedef struct rbs_node_list { rbs_node_list_node_t *head; rbs_node_list_node_t *tail; size_t length; - VALUE cached_ruby_value; } rbs_node_list_t; rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *); @@ -114,7 +123,6 @@ typedef struct rbs_hash { rbs_hash_node_t *head; rbs_hash_node_t *tail; size_t length; - VALUE cached_ruby_value; } rbs_hash_t; rbs_hash_t* rbs_hash_new(rbs_allocator_t *); @@ -130,7 +138,7 @@ rbs_node_t* rbs_hash_get(rbs_hash_t *hash, rbs_node_t *key); typedef struct rbs_ast_annotation { rbs_node_t base; - VALUE string; + rbs_string_t string; struct rbs_location *location; } rbs_ast_annotation_t; @@ -156,7 +164,7 @@ typedef struct rbs_ast_declarations_class { struct rbs_node_list *members; struct rbs_node_list *annotations; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; } rbs_ast_declarations_class_t; typedef struct rbs_ast_declarations_class_super { @@ -173,7 +181,7 @@ typedef struct rbs_ast_declarations_classalias { struct rbs_typename *new_name; struct rbs_typename *old_name; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; } rbs_ast_declarations_classalias_t; typedef struct rbs_ast_declarations_constant { @@ -182,7 +190,7 @@ typedef struct rbs_ast_declarations_constant { struct rbs_typename *name; struct rbs_node *type; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; } rbs_ast_declarations_constant_t; typedef struct rbs_ast_declarations_global { @@ -191,7 +199,7 @@ typedef struct rbs_ast_declarations_global { struct rbs_ast_symbol *name; struct rbs_node *type; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; } rbs_ast_declarations_global_t; typedef struct rbs_ast_declarations_interface { @@ -202,7 +210,7 @@ typedef struct rbs_ast_declarations_interface { struct rbs_node_list *members; struct rbs_node_list *annotations; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; } rbs_ast_declarations_interface_t; typedef struct rbs_ast_declarations_module { @@ -214,7 +222,7 @@ typedef struct rbs_ast_declarations_module { struct rbs_node_list *members; struct rbs_node_list *annotations; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; } rbs_ast_declarations_module_t; typedef struct rbs_ast_declarations_module_self { @@ -231,15 +239,9 @@ typedef struct rbs_ast_declarations_modulealias { struct rbs_typename *new_name; struct rbs_typename *old_name; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; } rbs_ast_declarations_modulealias_t; -typedef struct rbs_ast_declarations_nodes { - rbs_node_t base; - - struct rbs_node_list *declarations; -} rbs_ast_declarations_nodes_t; - typedef struct rbs_ast_declarations_typealias { rbs_node_t base; @@ -248,15 +250,9 @@ typedef struct rbs_ast_declarations_typealias { struct rbs_node *type; struct rbs_node_list *annotations; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; } rbs_ast_declarations_typealias_t; -typedef struct rbs_ast_directives_nodes { - rbs_node_t base; - - struct rbs_node_list *directives; -} rbs_ast_directives_nodes_t; - typedef struct rbs_ast_directives_use { rbs_node_t base; @@ -279,15 +275,21 @@ typedef struct rbs_ast_directives_use_wildcardclause { struct rbs_location *location; } rbs_ast_directives_use_wildcardclause_t; +typedef struct rbs_ast_integer { + rbs_node_t base; + + rbs_string_t string_representation; +} rbs_ast_integer_t; + typedef struct rbs_ast_members_alias { rbs_node_t base; struct rbs_ast_symbol *new_name; struct rbs_ast_symbol *old_name; - struct rbs_ast_symbol *kind; + struct rbs_keyword *kind; struct rbs_node_list *annotations; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; } rbs_ast_members_alias_t; typedef struct rbs_ast_members_attraccessor { @@ -296,11 +298,11 @@ typedef struct rbs_ast_members_attraccessor { struct rbs_ast_symbol *name; struct rbs_node *type; struct rbs_node *ivar_name; - struct rbs_ast_symbol *kind; + struct rbs_keyword *kind; struct rbs_node_list *annotations; struct rbs_location *location; - VALUE comment; - struct rbs_ast_symbol *visibility; + struct rbs_ast_comment *comment; + struct rbs_keyword *visibility; } rbs_ast_members_attraccessor_t; typedef struct rbs_ast_members_attrreader { @@ -309,11 +311,11 @@ typedef struct rbs_ast_members_attrreader { struct rbs_ast_symbol *name; struct rbs_node *type; struct rbs_node *ivar_name; - struct rbs_ast_symbol *kind; + struct rbs_keyword *kind; struct rbs_node_list *annotations; struct rbs_location *location; - VALUE comment; - struct rbs_ast_symbol *visibility; + struct rbs_ast_comment *comment; + struct rbs_keyword *visibility; } rbs_ast_members_attrreader_t; typedef struct rbs_ast_members_attrwriter { @@ -322,11 +324,11 @@ typedef struct rbs_ast_members_attrwriter { struct rbs_ast_symbol *name; struct rbs_node *type; struct rbs_node *ivar_name; - struct rbs_ast_symbol *kind; + struct rbs_keyword *kind; struct rbs_node_list *annotations; struct rbs_location *location; - VALUE comment; - struct rbs_ast_symbol *visibility; + struct rbs_ast_comment *comment; + struct rbs_keyword *visibility; } rbs_ast_members_attrwriter_t; typedef struct rbs_ast_members_classinstancevariable { @@ -335,7 +337,7 @@ typedef struct rbs_ast_members_classinstancevariable { struct rbs_ast_symbol *name; struct rbs_node *type; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; } rbs_ast_members_classinstancevariable_t; typedef struct rbs_ast_members_classvariable { @@ -344,7 +346,7 @@ typedef struct rbs_ast_members_classvariable { struct rbs_ast_symbol *name; struct rbs_node *type; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; } rbs_ast_members_classvariable_t; typedef struct rbs_ast_members_extend { @@ -354,7 +356,7 @@ typedef struct rbs_ast_members_extend { struct rbs_node_list *args; struct rbs_node_list *annotations; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; } rbs_ast_members_extend_t; typedef struct rbs_ast_members_include { @@ -364,7 +366,7 @@ typedef struct rbs_ast_members_include { struct rbs_node_list *args; struct rbs_node_list *annotations; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; } rbs_ast_members_include_t; typedef struct rbs_ast_members_instancevariable { @@ -373,20 +375,20 @@ typedef struct rbs_ast_members_instancevariable { struct rbs_ast_symbol *name; struct rbs_node *type; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; } rbs_ast_members_instancevariable_t; typedef struct rbs_ast_members_methoddefinition { rbs_node_t base; struct rbs_ast_symbol *name; - struct rbs_ast_symbol *kind; + struct rbs_keyword *kind; struct rbs_node_list *overloads; struct rbs_node_list *annotations; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; bool overloading; - struct rbs_ast_symbol *visibility; + struct rbs_keyword *visibility; } rbs_ast_members_methoddefinition_t; typedef struct rbs_ast_members_methoddefinition_overload { @@ -403,7 +405,7 @@ typedef struct rbs_ast_members_prepend { struct rbs_node_list *args; struct rbs_node_list *annotations; struct rbs_location *location; - VALUE comment; + struct rbs_ast_comment *comment; } rbs_ast_members_prepend_t; typedef struct rbs_ast_members_private { @@ -418,16 +420,17 @@ typedef struct rbs_ast_members_public { struct rbs_location *location; } rbs_ast_members_public_t; -typedef struct rbs_ast_symbol { +typedef struct rbs_ast_string { rbs_node_t base; -} rbs_ast_symbol_t; + rbs_string_t string; +} rbs_ast_string_t; typedef struct rbs_ast_typeparam { rbs_node_t base; struct rbs_ast_symbol *name; - struct rbs_ast_symbol *variance; + struct rbs_keyword *variance; struct rbs_node *upper_bound; struct rbs_node *default_type; bool unchecked; @@ -450,6 +453,13 @@ typedef struct rbs_namespace { bool absolute; } rbs_namespace_t; +typedef struct rbs_signature { + rbs_node_t base; + + struct rbs_node_list *directives; + struct rbs_node_list *declarations; +} rbs_signature_t; + typedef struct rbs_typename { rbs_node_t base; @@ -468,7 +478,7 @@ typedef struct rbs_types_alias { typedef struct rbs_types_bases_any { rbs_node_t base; - VALUE todo; + bool todo; struct rbs_location *location; } rbs_types_bases_any_t; @@ -546,14 +556,14 @@ typedef struct rbs_types_classsingleton { typedef struct rbs_types_function { rbs_node_t base; - VALUE required_positionals; - VALUE optional_positionals; - VALUE rest_positionals; - VALUE trailing_positionals; - VALUE required_keywords; - VALUE optional_keywords; - VALUE rest_keywords; - VALUE return_type; + struct rbs_node_list *required_positionals; + struct rbs_node_list *optional_positionals; + struct rbs_node *rest_positionals; + struct rbs_node_list *trailing_positionals; + struct rbs_hash *required_keywords; + struct rbs_hash *optional_keywords; + struct rbs_node *rest_keywords; + struct rbs_node *return_type; } rbs_types_function_t; typedef struct rbs_types_function_param { @@ -582,7 +592,7 @@ typedef struct rbs_types_intersection { typedef struct rbs_types_literal { rbs_node_t base; - VALUE literal; + struct rbs_node *literal; struct rbs_location *location; } rbs_types_literal_t; @@ -605,10 +615,17 @@ typedef struct rbs_types_proc { typedef struct rbs_types_record { rbs_node_t base; - VALUE all_fields; + struct rbs_hash *all_fields; struct rbs_location *location; } rbs_types_record_t; +typedef struct rbs_types_record_fieldtype { + rbs_node_t base; + + struct rbs_node *type; + bool required; +} rbs_types_record_fieldtype_t; + typedef struct rbs_types_tuple { rbs_node_t base; @@ -637,45 +654,71 @@ typedef struct rbs_types_variable { } rbs_types_variable_t; -rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, VALUE string, rbs_location_t *location); +/// `rbs_keyword_t` models RBS keywords like "private", "instance", "covariant", etc. +/// These are stored in the global constant pool, and get surfaced to Ruby as `Symbol`s, +/// just like `rbs_ast_symbol_t`s. +typedef struct rbs_keyword { + rbs_node_t base; + rbs_constant_id_t constant_id; +} rbs_keyword_t; + +rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *, rbs_constant_id_t); + +/// `rbs_ast_symbol_t` models user-defined identifiers like class names, method names, etc. +/// These get stored in the parser's own constant pool, and get surfaced to Ruby as `Symbol`s. +typedef struct rbs_ast_symbol { + rbs_node_t base; + rbs_constant_id_t constant_id; +} rbs_ast_symbol_t; + +rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *, rbs_constant_pool_t *, rbs_constant_id_t); + +typedef struct rbs_other_ruby_value { + rbs_node_t base; + VALUE ruby_value; +} rbs_other_ruby_value_t; + +rbs_other_ruby_value_t *rbs_other_ruby_value_new(VALUE ruby_value); + +rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_string_t string, rbs_location_t *location); rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, bool value); -rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE string, rbs_location_t *location); -rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, VALUE string, rbs_location_t *location); +rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); -rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment); -rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); -rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); -rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); -rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, rbs_ast_comment_t *comment); +rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment); +rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment); +rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); +rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); -rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment); -rbs_ast_declarations_nodes_t *rbs_ast_declarations_nodes_new(rbs_allocator_t *allocator, VALUE ruby_value, rbs_node_list_t *declarations); -rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); -rbs_ast_directives_nodes_t *rbs_ast_directives_nodes_new(rbs_allocator_t *allocator, VALUE ruby_value, rbs_node_list_t *directives); +rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, rbs_ast_comment_t *comment); +rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, rbs_node_list_t *clauses, rbs_location_t *location); rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, rbs_typename_t *type_name, rbs_ast_symbol_t *new_name, rbs_location_t *location); rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_namespace_t *namespace, rbs_location_t *location); -rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); -rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility); -rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility); -rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility); -rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); -rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); -rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); -rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); -rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); -rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_ast_symbol_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, bool overloading, rbs_ast_symbol_t *visibility); +rbs_ast_integer_t *rbs_ast_integer_new(rbs_allocator_t *allocator, rbs_string_t string_representation); +rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); +rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); +rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); +rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); +rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment); +rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment); +rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); +rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); +rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment); +rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_keyword_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, bool overloading, rbs_keyword_t *visibility); rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, rbs_node_list_t *annotations, rbs_node_t *method_type); -rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); +rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocator, rbs_location_t *location); rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, rbs_location_t *location); -rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, VALUE ruby_value); -rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_ast_symbol_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked, rbs_location_t *location); +rbs_ast_string_t *rbs_ast_string_new(rbs_allocator_t *allocator, rbs_string_t string); +rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_keyword_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked, rbs_location_t *location); rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location); rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_node_list_t *path, bool absolute); +rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_node_list_t *directives, rbs_node_list_t *declarations); rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_namespace_t *namespace, rbs_ast_symbol_t *name); rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); -rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, VALUE todo, rbs_location_t *location); +rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, bool todo, rbs_location_t *location); rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, rbs_location_t *location); rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, rbs_location_t *location); rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, rbs_location_t *location); @@ -687,14 +730,15 @@ rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, rbs rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_node_t *type, bool required, rbs_node_t *self_type); rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_location_t *location); -rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, VALUE required_positionals, VALUE optional_positionals, VALUE rest_positionals, VALUE trailing_positionals, VALUE required_keywords, VALUE optional_keywords, VALUE rest_keywords, VALUE return_type); +rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, rbs_node_list_t *required_positionals, rbs_node_list_t *optional_positionals, rbs_node_t *rest_positionals, rbs_node_list_t *trailing_positionals, rbs_hash_t *required_keywords, rbs_hash_t *optional_keywords, rbs_node_t *rest_keywords, rbs_node_t *return_type); rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_ast_symbol_t *name, rbs_location_t *location); rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location); -rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, VALUE literal, rbs_location_t *location); +rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, rbs_node_t *literal, rbs_location_t *location); rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_location_t *location); rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location, rbs_node_t *self_type); -rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, VALUE all_fields, rbs_location_t *location); +rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, rbs_hash_t *all_fields, rbs_location_t *location); +rbs_types_record_fieldtype_t *rbs_types_record_fieldtype_new(rbs_allocator_t *allocator, rbs_node_t *type, bool required); rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location); rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location); rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, rbs_node_t *return_type); diff --git a/include/rbs/constants.h b/include/rbs/constants.h index c67d7c15e..995f5f923 100644 --- a/include/rbs/constants.h +++ b/include/rbs/constants.h @@ -47,7 +47,6 @@ extern VALUE RBS_AST_Members_MethodDefinition_Overload; extern VALUE RBS_AST_Members_Prepend; extern VALUE RBS_AST_Members_Private; extern VALUE RBS_AST_Members_Public; -extern VALUE RBS_AST_Symbol; extern VALUE RBS_AST_TypeParam; extern VALUE RBS_MethodType; extern VALUE RBS_Namespace; diff --git a/include/rbs/encoding.h b/include/rbs/encoding.h new file mode 100644 index 000000000..3afc102bb --- /dev/null +++ b/include/rbs/encoding.h @@ -0,0 +1,9 @@ +#ifndef RBS_ENCODING_H +#define RBS_ENCODING_H + +#include "rbs_string.h" + +unsigned int utf8_to_codepoint(const rbs_string_t string); +int utf8_codelen(unsigned int c); + +#endif // RBS_ENCODING_H diff --git a/include/rbs/rbs_string.h b/include/rbs/rbs_string.h new file mode 100644 index 000000000..c5d4b96fb --- /dev/null +++ b/include/rbs/rbs_string.h @@ -0,0 +1,85 @@ +#ifndef RBS__RBS_STRING_H +#define RBS__RBS_STRING_H + +#include +#include + +typedef struct { + const char *start; + const char *end; + + enum rbs_string_type { + /** This string is a constant string, and should not be freed. */ + RBS_STRING_CONSTANT, + /** This is a slice of another string, and should not be freed. */ + RBS_STRING_SHARED, + /** This string owns its memory, and should be freed using `rbs_string_free`. */ + RBS_STRING_OWNED, + } type; +} rbs_string_t; + +#define RBS_STRING_NULL ((rbs_string_t) { \ + .start = NULL, \ + .end = NULL, \ + .type = RBS_STRING_CONSTANT, \ + }) + +/** + * Returns a new `rbs_string_t` struct that points to the given C string without owning it. + */ +rbs_string_t rbs_string_shared_new(const char *start, const char *end); + +/** + * Returns a new `rbs_string_t` struct that owns its memory. + */ +rbs_string_t rbs_string_owned_new(const char *start, const char *end); + +/** + * Ensures that the given string is owned, so that it manages its own memory, uncoupled from its original source. + */ +void rbs_string_ensure_owned(rbs_string_t *self); + +/** + * Returns a new `rbs_string_t` with its start shifted forward by the given amount. + * This returns a shared string which points to the same memory as the original string. + */ +rbs_string_t rbs_string_offset(const rbs_string_t self, size_t offset); + +/** + * Modifies the given string to drop its first `n` characters. + */ +void rbs_string_drop_first(rbs_string_t *self, size_t n); + +/** + * Modifies the given string to drop its last `n` characters. + */ +void rbs_string_drop_last(rbs_string_t *self, size_t n); + +/** + * Modifies the given string to limit its length to the given number of characters. + */ +void rbs_string_limit_length(rbs_string_t *self, size_t new_length); + +/** + * Copies a portion of the input string into a new owned string. + * @param start_inset Number of characters to exclude from the start + * @param length Number of characters to include + */ +rbs_string_t rbs_string_slice(const rbs_string_t self, size_t start_inset, size_t length); + +/** + * Drops the leading and trailing whitespace from the given string, in-place. + */ +void rbs_string_strip_whitespace(rbs_string_t *self); + +/** + * Returns the length of the string. + */ +size_t rbs_string_len(const rbs_string_t self); + +/** + * Compares two strings for equality. + */ +bool rbs_string_equal(const rbs_string_t lhs, const rbs_string_t rhs); + +#endif diff --git a/include/rbs/rbs_unescape.h b/include/rbs/rbs_unescape.h new file mode 100644 index 000000000..2b382d698 --- /dev/null +++ b/include/rbs/rbs_unescape.h @@ -0,0 +1,20 @@ +#ifndef RBS_RBS_UNESCAPE_H +#define RBS_RBS_UNESCAPE_H + +#include +#include "parserstate.h" + +/** + * Receives `parserstate` and `range`, which represents a string token or symbol token, and returns a string VALUE. + * + * Input token | Output string + * ------------+------------- + * "foo\\n" | foo\n + * 'foo' | foo + * `bar` | bar + * :"baz\\t" | baz\t + * :'baz' | baz + * */ +rbs_string_t rbs_unquote_string2(rbs_string_t input); + +#endif // RBS_RBS_UNESCAPE_H diff --git a/include/rbs/ruby_objs.h b/include/rbs/ruby_objs.h deleted file mode 100644 index 4809bde6f..000000000 --- a/include/rbs/ruby_objs.h +++ /dev/null @@ -1,73 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* This file is generated by the templates/template.rb script and should not */ -/* be modified manually. */ -/* To change the template see */ -/* templates/include/rbs/ruby_objs.h.erb */ -/*----------------------------------------------------------------------------*/ - -#ifndef RBS__RUBY_OBJS_H -#define RBS__RUBY_OBJS_H - -#include "ruby.h" - -VALUE rbs_ast_annotation(VALUE string, rbs_location_t *location); -VALUE rbs_ast_comment(VALUE string, rbs_location_t *location); -VALUE rbs_ast_decl_class(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); -VALUE rbs_ast_decl_class_super(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); -VALUE rbs_ast_decl_class_alias(rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment); -VALUE rbs_ast_decl_constant(rbs_typename_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); -VALUE rbs_ast_decl_global(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); -VALUE rbs_ast_decl_interface(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); -VALUE rbs_ast_decl_module(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); -VALUE rbs_ast_decl_module_self(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); -VALUE rbs_ast_decl_module_alias(rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment); -VALUE rbs_ast_decl_type_alias(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); -VALUE rbs_ast_directives_use(rbs_node_list_t *clauses, rbs_location_t *location); -VALUE rbs_ast_directives_use_single_clause(rbs_typename_t *type_name, rbs_ast_symbol_t *new_name, rbs_location_t *location); -VALUE rbs_ast_directives_use_wildcard_clause(rbs_namespace_t *namespace, rbs_location_t *location); -VALUE rbs_ast_members_alias(rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); -VALUE rbs_ast_members_attr_accessor(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility); -VALUE rbs_ast_members_attr_reader(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility); -VALUE rbs_ast_members_attr_writer(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility); -VALUE rbs_ast_members_class_instance_variable(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); -VALUE rbs_ast_members_class_variable(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); -VALUE rbs_ast_members_extend(rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); -VALUE rbs_ast_members_include(rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); -VALUE rbs_ast_members_instance_variable(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment); -VALUE rbs_ast_members_method_definition(rbs_ast_symbol_t *name, rbs_ast_symbol_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, bool overloading, rbs_ast_symbol_t *visibility); -VALUE rbs_ast_members_method_definition_overload(rbs_node_list_t *annotations, rbs_node_t *method_type); -VALUE rbs_ast_members_prepend(rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment); -VALUE rbs_ast_members_private(rbs_location_t *location); -VALUE rbs_ast_members_public(rbs_location_t *location); -VALUE rbs_ast_symbol(); -VALUE rbs_ast_type_param(rbs_ast_symbol_t *name, rbs_ast_symbol_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked, rbs_location_t *location); -VALUE rbs_method_type(rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location); -VALUE rbs_namespace(rbs_node_list_t *path, bool absolute); -VALUE rbs_type_name(rbs_namespace_t *namespace, rbs_ast_symbol_t *name); -VALUE rbs_alias(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); -VALUE rbs_bases_any(VALUE todo, rbs_location_t *location); -VALUE rbs_bases_bool(rbs_location_t *location); -VALUE rbs_bases_bottom(rbs_location_t *location); -VALUE rbs_bases_class(rbs_location_t *location); -VALUE rbs_bases_instance(rbs_location_t *location); -VALUE rbs_bases_nil(rbs_location_t *location); -VALUE rbs_bases_self(rbs_location_t *location); -VALUE rbs_bases_top(rbs_location_t *location); -VALUE rbs_bases_void(rbs_location_t *location); -VALUE rbs_block(rbs_node_t *type, bool required, rbs_node_t *self_type); -VALUE rbs_class_instance(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); -VALUE rbs_class_singleton(rbs_typename_t *name, rbs_location_t *location); -VALUE rbs_function(VALUE required_positionals, VALUE optional_positionals, VALUE rest_positionals, VALUE trailing_positionals, VALUE required_keywords, VALUE optional_keywords, VALUE rest_keywords, VALUE return_type); -VALUE rbs_function_param(rbs_node_t *type, rbs_ast_symbol_t *name, rbs_location_t *location); -VALUE rbs_interface(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); -VALUE rbs_intersection(rbs_node_list_t *types, rbs_location_t *location); -VALUE rbs_literal(VALUE literal, rbs_location_t *location); -VALUE rbs_optional(rbs_node_t *type, rbs_location_t *location); -VALUE rbs_proc(rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location, rbs_node_t *self_type); -VALUE rbs_record(VALUE all_fields, rbs_location_t *location); -VALUE rbs_tuple(rbs_node_list_t *types, rbs_location_t *location); -VALUE rbs_union(rbs_node_list_t *types, rbs_location_t *location); -VALUE rbs_untyped_function(rbs_node_t *return_type); -VALUE rbs_variable(rbs_ast_symbol_t *name, rbs_location_t *location); - -#endif diff --git a/include/rbs/util/rbs_constant_pool.h b/include/rbs/util/rbs_constant_pool.h index c63b1dea9..63f15627b 100644 --- a/include/rbs/util/rbs_constant_pool.h +++ b/include/rbs/util/rbs_constant_pool.h @@ -185,6 +185,7 @@ rbs_constant_id_t rbs_constant_pool_find(const rbs_constant_pool_t *pool, const * @return The id of the constant. */ rbs_constant_id_t rbs_constant_pool_insert_shared(rbs_constant_pool_t *pool, const uint8_t *start, size_t length); +rbs_constant_id_t rbs_constant_pool_insert_shared_with_encoding(rbs_constant_pool_t *pool, const uint8_t *start, size_t length, void *encoding); /** * Insert a constant into a constant pool from memory that is now owned by the diff --git a/src/ast.c b/src/ast.c index 4b91bdae3..0fe0775be 100644 --- a/src/ast.c +++ b/src/ast.c @@ -7,8 +7,6 @@ #include "rbs/ast.h" -#include "rbs/ruby_objs.h" - /* rbs_node_list */ rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *allocator) { @@ -18,17 +16,12 @@ rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *allocator) { .head = NULL, .tail = NULL, .length = 0, - .cached_ruby_value = rb_ary_new(), }; - rb_gc_register_mark_object(list->cached_ruby_value); - return list; } void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node) { - rb_gc_register_mark_object(node->cached_ruby_value); - rbs_node_list_node_t *new_node = rbs_allocator_alloc(list->allocator, rbs_node_list_node_t); *new_node = (rbs_node_list_node_t) { .node = node, @@ -42,9 +35,8 @@ void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node) { list->tail->next = new_node; list->tail = new_node; } - list->length++; - rb_ary_push(list->cached_ruby_value, node->cached_ruby_value); + list->length++; } /* rbs_hash */ @@ -56,11 +48,8 @@ rbs_hash_t* rbs_hash_new(rbs_allocator_t *allocator) { .head = NULL, .tail = NULL, .length = 0, - .cached_ruby_value = rb_hash_new(), }; - rb_gc_register_mark_object(hash->cached_ruby_value); - return hash; } @@ -69,10 +58,21 @@ bool rbs_node_equal(rbs_node_t *lhs, rbs_node_t *rhs) { if (lhs->type != rhs->type) return false; switch (lhs->type) { + case RBS_AST_SYMBOL: + return ((rbs_ast_symbol_t *)lhs)->constant_id == ((rbs_ast_symbol_t *) rhs)->constant_id; + case RBS_KEYWORD: + return ((rbs_keyword_t *)lhs)->constant_id == ((rbs_keyword_t *) rhs)->constant_id; case RBS_AST_BOOL: return ((rbs_ast_bool_t *)lhs)->value == ((rbs_ast_bool_t *) rhs)->value; + case RBS_AST_INTEGER: + return rbs_string_equal(((rbs_ast_integer_t *) lhs)->string_representation, ((rbs_ast_integer_t *) rhs)->string_representation); + case RBS_AST_STRING: + return rbs_string_equal(((rbs_ast_string_t *) lhs)->string, ((rbs_ast_string_t *) rhs)->string); + case RBS_OTHER_RUBY_VALUE: + return rb_equal(((rbs_other_ruby_value_t *) lhs)->ruby_value, ((rbs_other_ruby_value_t *) rhs)->ruby_value); default: - return rb_equal(lhs->cached_ruby_value, rhs->cached_ruby_value); + printf("Unhandled node type: %d\n", lhs->type); + return false; } } @@ -90,17 +90,12 @@ rbs_hash_node_t* rbs_hash_find(rbs_hash_t *hash, rbs_node_t *key) { } void rbs_hash_set(rbs_hash_t *hash, rbs_node_t *key, rbs_node_t *value) { - rb_gc_register_mark_object(key->cached_ruby_value); - rb_gc_register_mark_object(value->cached_ruby_value); - rbs_hash_node_t *existing_node = rbs_hash_find(hash, key); if (existing_node != NULL) { existing_node->value = value; return; } - rb_hash_aset(hash->cached_ruby_value, key->cached_ruby_value, value->cached_ruby_value); - rbs_hash_node_t *new_node = rbs_allocator_alloc(hash->allocator, rbs_hash_node_t); new_node->key = key; new_node->value = value; @@ -120,21 +115,53 @@ rbs_node_t* rbs_hash_get(rbs_hash_t *hash, rbs_node_t *key) { return node ? node->value : NULL; } -rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, VALUE string, rbs_location_t *location) { - rbs_ast_annotation_t *instance = rbs_allocator_alloc(allocator, rbs_ast_annotation_t); +rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *allocator, rbs_constant_id_t constant_id) { + rbs_keyword_t *instance = rbs_allocator_alloc(allocator, rbs_keyword_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(string); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); + *instance = (rbs_keyword_t) { + .base = (rbs_node_t) { + .type = RBS_KEYWORD, + }, + .constant_id = constant_id, + }; + + return instance; +} + +rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_constant_pool_t *constant_pool, rbs_constant_id_t constant_id) { + rbs_ast_symbol_t *instance = rbs_allocator_alloc(allocator, rbs_ast_symbol_t); - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_annotation(string, location); + *instance = (rbs_ast_symbol_t) { + .base = (rbs_node_t) { + .type = RBS_AST_SYMBOL, + }, + .constant_id = constant_id, + }; + return instance; +} + +rbs_other_ruby_value_t *rbs_other_ruby_value_new(VALUE ruby_value) { rb_gc_register_mark_object(ruby_value); + rbs_other_ruby_value_t *instance = malloc(sizeof(rbs_other_ruby_value_t)); + + *instance = (rbs_other_ruby_value_t) { + .base = (rbs_node_t) { + .type = RBS_OTHER_RUBY_VALUE + }, + .ruby_value = ruby_value, + }; + + return instance; +} + +rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_string_t string, rbs_location_t *location) { + rbs_ast_annotation_t *instance = rbs_allocator_alloc(allocator, rbs_ast_annotation_t); + + *instance = (rbs_ast_annotation_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_ANNOTATION }, .string = string, @@ -147,17 +174,9 @@ rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, VALUE s rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, bool value) { rbs_ast_bool_t *instance = rbs_allocator_alloc(allocator, rbs_ast_bool_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(value ? Qtrue : Qfalse); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = value ? Qtrue : Qfalse; - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_bool_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_BOOL }, .value = value, @@ -166,19 +185,13 @@ rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, bool value) { return instance; } -rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, VALUE ruby_value, VALUE string, rbs_location_t *location) { +rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, VALUE string, rbs_location_t *location) { rbs_ast_comment_t *instance = rbs_allocator_alloc(allocator, rbs_ast_comment_t); - // Disable GC for all these Ruby objects. rb_gc_register_mark_object(string); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_comment_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_COMMENT }, .string = string, @@ -188,26 +201,12 @@ rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, VALUE ruby_va return instance; } -rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { +rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { rbs_ast_declarations_class_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(type_params == NULL ? Qnil : type_params->cached_ruby_value); - rb_gc_register_mark_object(super_class == NULL ? Qnil : super_class->base.cached_ruby_value); - rb_gc_register_mark_object(members == NULL ? Qnil : members->cached_ruby_value); - rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_decl_class(name, type_params, super_class, members, annotations, location, comment); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_declarations_class_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_DECLARATIONS_CLASS }, .name = name, @@ -225,19 +224,9 @@ rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *al rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { rbs_ast_declarations_class_super_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_super_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_decl_class_super(name, args, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_declarations_class_super_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_DECLARATIONS_CLASS_SUPER }, .name = name, @@ -248,23 +237,12 @@ rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_all return instance; } -rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment) { +rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, rbs_ast_comment_t *comment) { rbs_ast_declarations_classalias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_classalias_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(new_name == NULL ? Qnil : new_name->base.cached_ruby_value); - rb_gc_register_mark_object(old_name == NULL ? Qnil : old_name->base.cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_decl_class_alias(new_name, old_name, location, comment); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_declarations_classalias_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_DECLARATIONS_CLASSALIAS }, .new_name = new_name, @@ -276,23 +254,12 @@ rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_alloc return instance; } -rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { +rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment) { rbs_ast_declarations_constant_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_constant_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_decl_constant(name, type, location, comment); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_declarations_constant_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_DECLARATIONS_CONSTANT }, .name = name, @@ -304,23 +271,12 @@ rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator return instance; } -rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { +rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment) { rbs_ast_declarations_global_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_global_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_decl_global(name, type, location, comment); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_declarations_global_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_DECLARATIONS_GLOBAL }, .name = name, @@ -332,25 +288,12 @@ rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t * return instance; } -rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { +rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { rbs_ast_declarations_interface_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_interface_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(type_params == NULL ? Qnil : type_params->cached_ruby_value); - rb_gc_register_mark_object(members == NULL ? Qnil : members->cached_ruby_value); - rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_decl_interface(name, type_params, members, annotations, location, comment); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_declarations_interface_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_DECLARATIONS_INTERFACE }, .name = name, @@ -364,26 +307,12 @@ rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocat return instance; } -rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { +rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { rbs_ast_declarations_module_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(type_params == NULL ? Qnil : type_params->cached_ruby_value); - rb_gc_register_mark_object(self_types == NULL ? Qnil : self_types->cached_ruby_value); - rb_gc_register_mark_object(members == NULL ? Qnil : members->cached_ruby_value); - rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_decl_module(name, type_params, self_types, members, annotations, location, comment); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_declarations_module_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_DECLARATIONS_MODULE }, .name = name, @@ -401,19 +330,9 @@ rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t * rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { rbs_ast_declarations_module_self_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_self_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_decl_module_self(name, args, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_declarations_module_self_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_DECLARATIONS_MODULE_SELF }, .name = name, @@ -424,23 +343,12 @@ rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_all return instance; } -rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment) { +rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, rbs_ast_comment_t *comment) { rbs_ast_declarations_modulealias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_modulealias_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(new_name == NULL ? Qnil : new_name->base.cached_ruby_value); - rb_gc_register_mark_object(old_name == NULL ? Qnil : old_name->base.cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_decl_module_alias(new_name, old_name, location, comment); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_declarations_modulealias_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_DECLARATIONS_MODULEALIAS }, .new_name = new_name, @@ -452,45 +360,12 @@ rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_all return instance; } -rbs_ast_declarations_nodes_t *rbs_ast_declarations_nodes_new(rbs_allocator_t *allocator, VALUE ruby_value, rbs_node_list_t *declarations) { - rbs_ast_declarations_nodes_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_nodes_t); - - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(declarations == NULL ? Qnil : declarations->cached_ruby_value); - - - rb_gc_register_mark_object(ruby_value); - - *instance = (rbs_ast_declarations_nodes_t) { - .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, - .type = RBS_AST_DECLARATIONS_NODES - }, - .declarations = declarations, - }; - - return instance; -} - -rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { +rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { rbs_ast_declarations_typealias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_typealias_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(type_params == NULL ? Qnil : type_params->cached_ruby_value); - rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); - rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_decl_type_alias(name, type_params, type, annotations, location, comment); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_declarations_typealias_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_DECLARATIONS_TYPEALIAS }, .name = name, @@ -504,41 +379,12 @@ rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocat return instance; } -rbs_ast_directives_nodes_t *rbs_ast_directives_nodes_new(rbs_allocator_t *allocator, VALUE ruby_value, rbs_node_list_t *directives) { - rbs_ast_directives_nodes_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_nodes_t); - - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(directives == NULL ? Qnil : directives->cached_ruby_value); - - - rb_gc_register_mark_object(ruby_value); - - *instance = (rbs_ast_directives_nodes_t) { - .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, - .type = RBS_AST_DIRECTIVES_NODES - }, - .directives = directives, - }; - - return instance; -} - rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, rbs_node_list_t *clauses, rbs_location_t *location) { rbs_ast_directives_use_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(clauses == NULL ? Qnil : clauses->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_directives_use(clauses, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_directives_use_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_DIRECTIVES_USE }, .clauses = clauses, @@ -551,19 +397,9 @@ rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, rbs_typename_t *type_name, rbs_ast_symbol_t *new_name, rbs_location_t *location) { rbs_ast_directives_use_singleclause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_singleclause_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(type_name == NULL ? Qnil : type_name->base.cached_ruby_value); - rb_gc_register_mark_object(new_name == NULL ? Qnil : new_name->base.cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_directives_use_single_clause(type_name, new_name, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_directives_use_singleclause_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_DIRECTIVES_USE_SINGLECLAUSE }, .type_name = type_name, @@ -577,18 +413,9 @@ rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(r rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_namespace_t *namespace, rbs_location_t *location) { rbs_ast_directives_use_wildcardclause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_wildcardclause_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(namespace == NULL ? Qnil : namespace->base.cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_directives_use_wildcard_clause(namespace, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_directives_use_wildcardclause_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE }, .namespace = namespace, @@ -598,25 +425,26 @@ rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_n return instance; } -rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { - rbs_ast_members_alias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_alias_t); +rbs_ast_integer_t *rbs_ast_integer_new(rbs_allocator_t *allocator, rbs_string_t string_representation) { + rbs_ast_integer_t *instance = rbs_allocator_alloc(allocator, rbs_ast_integer_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(new_name == NULL ? Qnil : new_name->base.cached_ruby_value); - rb_gc_register_mark_object(old_name == NULL ? Qnil : old_name->base.cached_ruby_value); - rb_gc_register_mark_object(kind == NULL ? Qnil : kind->base.cached_ruby_value); - rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_members_alias(new_name, old_name, kind, annotations, location, comment); + *instance = (rbs_ast_integer_t) { + .base = (rbs_node_t) { + .type = RBS_AST_INTEGER + }, + .string_representation = string_representation, + }; + + return instance; +} + +rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { + rbs_ast_members_alias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_alias_t); - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_alias_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_MEMBERS_ALIAS }, .new_name = new_name, @@ -630,27 +458,12 @@ rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, r return instance; } -rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility) { +rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { rbs_ast_members_attraccessor_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attraccessor_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); - rb_gc_register_mark_object(ivar_name == NULL ? Qnil : ivar_name->cached_ruby_value); - rb_gc_register_mark_object(kind == NULL ? Qnil : kind->base.cached_ruby_value); - rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - rb_gc_register_mark_object(visibility == NULL ? Qnil : visibility->base.cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_members_attr_accessor(name, type, ivar_name, kind, annotations, location, comment, visibility); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_attraccessor_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_MEMBERS_ATTRACCESSOR }, .name = name, @@ -666,27 +479,12 @@ rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t return instance; } -rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility) { +rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { rbs_ast_members_attrreader_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attrreader_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); - rb_gc_register_mark_object(ivar_name == NULL ? Qnil : ivar_name->cached_ruby_value); - rb_gc_register_mark_object(kind == NULL ? Qnil : kind->base.cached_ruby_value); - rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - rb_gc_register_mark_object(visibility == NULL ? Qnil : visibility->base.cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_members_attr_reader(name, type, ivar_name, kind, annotations, location, comment, visibility); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_attrreader_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_MEMBERS_ATTRREADER }, .name = name, @@ -702,27 +500,12 @@ rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *al return instance; } -rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility) { +rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { rbs_ast_members_attrwriter_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attrwriter_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); - rb_gc_register_mark_object(ivar_name == NULL ? Qnil : ivar_name->cached_ruby_value); - rb_gc_register_mark_object(kind == NULL ? Qnil : kind->base.cached_ruby_value); - rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - rb_gc_register_mark_object(visibility == NULL ? Qnil : visibility->base.cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_members_attr_writer(name, type, ivar_name, kind, annotations, location, comment, visibility); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_attrwriter_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_MEMBERS_ATTRWRITER }, .name = name, @@ -738,23 +521,12 @@ rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *al return instance; } -rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { +rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment) { rbs_ast_members_classinstancevariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_classinstancevariable_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_members_class_instance_variable(name, type, location, comment); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_classinstancevariable_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE }, .name = name, @@ -766,23 +538,12 @@ rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_n return instance; } -rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { +rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment) { rbs_ast_members_classvariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_classvariable_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_members_class_variable(name, type, location, comment); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_classvariable_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_MEMBERS_CLASSVARIABLE }, .name = name, @@ -794,24 +555,12 @@ rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator return instance; } -rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { +rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { rbs_ast_members_extend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_extend_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); - rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_members_extend(name, args, annotations, location, comment); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_extend_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_MEMBERS_EXTEND }, .name = name, @@ -824,24 +573,12 @@ rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, return instance; } -rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { +rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { rbs_ast_members_include_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_include_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); - rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_members_include(name, args, annotations, location, comment); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_include_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_MEMBERS_INCLUDE }, .name = name, @@ -854,23 +591,12 @@ rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocato return instance; } -rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { +rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment) { rbs_ast_members_instancevariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_instancevariable_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_members_instance_variable(name, type, location, comment); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_instancevariable_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_MEMBERS_INSTANCEVARIABLE }, .name = name, @@ -882,27 +608,12 @@ rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_all return instance; } -rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_ast_symbol_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, bool overloading, rbs_ast_symbol_t *visibility) { +rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_keyword_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, bool overloading, rbs_keyword_t *visibility) { rbs_ast_members_methoddefinition_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_methoddefinition_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(kind == NULL ? Qnil : kind->base.cached_ruby_value); - rb_gc_register_mark_object(overloads == NULL ? Qnil : overloads->cached_ruby_value); - rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - rb_gc_register_mark_object(overloading ? Qtrue : Qfalse); - rb_gc_register_mark_object(visibility == NULL ? Qnil : visibility->base.cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_members_method_definition(name, kind, overloads, annotations, location, comment, overloading, visibility); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_methoddefinition_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_MEMBERS_METHODDEFINITION }, .name = name, @@ -921,18 +632,9 @@ rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_all rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, rbs_node_list_t *annotations, rbs_node_t *method_type) { rbs_ast_members_methoddefinition_overload_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_methoddefinition_overload_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_gc_register_mark_object(method_type == NULL ? Qnil : method_type->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_members_method_definition_overload(annotations, method_type); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_methoddefinition_overload_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD }, .annotations = annotations, @@ -942,24 +644,12 @@ rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_ov return instance; } -rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { +rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { rbs_ast_members_prepend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_prepend_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); - rb_gc_register_mark_object(annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(comment); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_members_prepend(name, args, annotations, location, comment); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_prepend_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_MEMBERS_PREPEND }, .name = name, @@ -975,17 +665,9 @@ rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocato rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_ast_members_private_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_private_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_members_private(location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_private_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_MEMBERS_PRIVATE }, .location = location, @@ -997,17 +679,9 @@ rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocato rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_ast_members_public_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_public_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_members_public(location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_members_public_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_MEMBERS_PUBLIC }, .location = location, @@ -1016,43 +690,26 @@ rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, return instance; } -rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, VALUE ruby_value) { - rbs_ast_symbol_t *instance = rbs_allocator_alloc(allocator, rbs_ast_symbol_t); - - // Disable GC for all these Ruby objects. +rbs_ast_string_t *rbs_ast_string_new(rbs_allocator_t *allocator, rbs_string_t string) { + rbs_ast_string_t *instance = rbs_allocator_alloc(allocator, rbs_ast_string_t); - rb_gc_register_mark_object(ruby_value); - - *instance = (rbs_ast_symbol_t) { + *instance = (rbs_ast_string_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, - .type = RBS_AST_SYMBOL + .type = RBS_AST_STRING }, + .string = string, }; return instance; } -rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_ast_symbol_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked, rbs_location_t *location) { +rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_keyword_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked, rbs_location_t *location) { rbs_ast_typeparam_t *instance = rbs_allocator_alloc(allocator, rbs_ast_typeparam_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(variance == NULL ? Qnil : variance->base.cached_ruby_value); - rb_gc_register_mark_object(upper_bound == NULL ? Qnil : upper_bound->cached_ruby_value); - rb_gc_register_mark_object(default_type == NULL ? Qnil : default_type->cached_ruby_value); - rb_gc_register_mark_object(unchecked ? Qtrue : Qfalse); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_ast_type_param(name, variance, upper_bound, default_type, unchecked, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_ast_typeparam_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_AST_TYPEPARAM }, .name = name, @@ -1069,20 +726,9 @@ rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_ast_s rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location) { rbs_methodtype_t *instance = rbs_allocator_alloc(allocator, rbs_methodtype_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(type_params == NULL ? Qnil : type_params->cached_ruby_value); - rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); - rb_gc_register_mark_object(block == NULL ? Qnil : block->base.cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_method_type(type_params, type, block, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_methodtype_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_METHODTYPE }, .type_params = type_params, @@ -1097,18 +743,9 @@ rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_node_list_t rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_node_list_t *path, bool absolute) { rbs_namespace_t *instance = rbs_allocator_alloc(allocator, rbs_namespace_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(path == NULL ? Qnil : path->cached_ruby_value); - rb_gc_register_mark_object(absolute ? Qtrue : Qfalse); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_namespace(path, absolute); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_namespace_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_NAMESPACE }, .path = path, @@ -1118,21 +755,27 @@ rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_node_list_t * return instance; } -rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_namespace_t *namespace, rbs_ast_symbol_t *name) { - rbs_typename_t *instance = rbs_allocator_alloc(allocator, rbs_typename_t); +rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_node_list_t *directives, rbs_node_list_t *declarations) { + rbs_signature_t *instance = rbs_allocator_alloc(allocator, rbs_signature_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(namespace == NULL ? Qnil : namespace->base.cached_ruby_value); - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_type_name(namespace, name); + *instance = (rbs_signature_t) { + .base = (rbs_node_t) { + .type = RBS_SIGNATURE + }, + .directives = directives, + .declarations = declarations, + }; + + return instance; +} + +rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_namespace_t *namespace, rbs_ast_symbol_t *name) { + rbs_typename_t *instance = rbs_allocator_alloc(allocator, rbs_typename_t); - rb_gc_register_mark_object(ruby_value); *instance = (rbs_typename_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPENAME }, .namespace = namespace, @@ -1145,19 +788,9 @@ rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_namespace_t *na rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { rbs_types_alias_t *instance = rbs_allocator_alloc(allocator, rbs_types_alias_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_alias(name, args, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_alias_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_ALIAS }, .name = name, @@ -1168,21 +801,12 @@ rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_typename_ return instance; } -rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, VALUE todo, rbs_location_t *location) { +rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, bool todo, rbs_location_t *location) { rbs_types_bases_any_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_any_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(todo); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_bases_any(todo, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_bases_any_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_BASES_ANY }, .todo = todo, @@ -1195,17 +819,9 @@ rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, VALUE rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_bool_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_bool_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_bases_bool(location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_bases_bool_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_BASES_BOOL }, .location = location, @@ -1217,17 +833,9 @@ rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, rbs rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_bottom_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_bottom_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_bases_bottom(location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_bases_bottom_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_BASES_BOTTOM }, .location = location, @@ -1239,17 +847,9 @@ rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_class_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_class_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_bases_class(location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_bases_class_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_BASES_CLASS }, .location = location, @@ -1261,17 +861,9 @@ rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, r rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_instance_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_instance_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_bases_instance(location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_bases_instance_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_BASES_INSTANCE }, .location = location, @@ -1283,17 +875,9 @@ rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *alloca rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_nil_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_nil_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_bases_nil(location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_bases_nil_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_BASES_NIL }, .location = location, @@ -1305,17 +889,9 @@ rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, rbs_l rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_self_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_self_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_bases_self(location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_bases_self_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_BASES_SELF }, .location = location, @@ -1327,17 +903,9 @@ rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, rbs rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_top_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_top_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_bases_top(location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_bases_top_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_BASES_TOP }, .location = location, @@ -1349,17 +917,9 @@ rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, rbs_l rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_void_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_void_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_bases_void(location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_bases_void_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_BASES_VOID }, .location = location, @@ -1371,19 +931,9 @@ rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, rbs rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_node_t *type, bool required, rbs_node_t *self_type) { rbs_types_block_t *instance = rbs_allocator_alloc(allocator, rbs_types_block_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); - rb_gc_register_mark_object(required ? Qtrue : Qfalse); - rb_gc_register_mark_object(self_type == NULL ? Qnil : self_type->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_block(type, required, self_type); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_block_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_BLOCK }, .type = type, @@ -1397,19 +947,9 @@ rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_node_t *t rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { rbs_types_classinstance_t *instance = rbs_allocator_alloc(allocator, rbs_types_classinstance_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_class_instance(name, args, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_classinstance_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_CLASSINSTANCE }, .name = name, @@ -1423,18 +963,9 @@ rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocato rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_location_t *location) { rbs_types_classsingleton_t *instance = rbs_allocator_alloc(allocator, rbs_types_classsingleton_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_class_singleton(name, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_classsingleton_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_CLASSSINGLETON }, .name = name, @@ -1444,27 +975,12 @@ rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *alloca return instance; } -rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, VALUE required_positionals, VALUE optional_positionals, VALUE rest_positionals, VALUE trailing_positionals, VALUE required_keywords, VALUE optional_keywords, VALUE rest_keywords, VALUE return_type) { +rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, rbs_node_list_t *required_positionals, rbs_node_list_t *optional_positionals, rbs_node_t *rest_positionals, rbs_node_list_t *trailing_positionals, rbs_hash_t *required_keywords, rbs_hash_t *optional_keywords, rbs_node_t *rest_keywords, rbs_node_t *return_type) { rbs_types_function_t *instance = rbs_allocator_alloc(allocator, rbs_types_function_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(required_positionals); - rb_gc_register_mark_object(optional_positionals); - rb_gc_register_mark_object(rest_positionals); - rb_gc_register_mark_object(trailing_positionals); - rb_gc_register_mark_object(required_keywords); - rb_gc_register_mark_object(optional_keywords); - rb_gc_register_mark_object(rest_keywords); - rb_gc_register_mark_object(return_type); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_function(required_positionals, optional_positionals, rest_positionals, trailing_positionals, required_keywords, optional_keywords, rest_keywords, return_type); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_function_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_FUNCTION }, .required_positionals = required_positionals, @@ -1483,19 +999,9 @@ rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, VALUE r rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_ast_symbol_t *name, rbs_location_t *location) { rbs_types_function_param_t *instance = rbs_allocator_alloc(allocator, rbs_types_function_param_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_function_param(type, name, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_function_param_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_FUNCTION_PARAM }, .type = type, @@ -1509,19 +1015,9 @@ rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *alloca rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { rbs_types_interface_t *instance = rbs_allocator_alloc(allocator, rbs_types_interface_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(args == NULL ? Qnil : args->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_interface(name, args, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_interface_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_INTERFACE }, .name = name, @@ -1535,18 +1031,9 @@ rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_t rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location) { rbs_types_intersection_t *instance = rbs_allocator_alloc(allocator, rbs_types_intersection_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(types == NULL ? Qnil : types->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_intersection(types, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_intersection_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_INTERSECTION }, .types = types, @@ -1556,21 +1043,12 @@ rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, return instance; } -rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, VALUE literal, rbs_location_t *location) { +rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, rbs_node_t *literal, rbs_location_t *location) { rbs_types_literal_t *instance = rbs_allocator_alloc(allocator, rbs_types_literal_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(literal); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_literal(literal, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_literal_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_LITERAL }, .literal = literal, @@ -1583,18 +1061,9 @@ rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, VALUE lit rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_location_t *location) { rbs_types_optional_t *instance = rbs_allocator_alloc(allocator, rbs_types_optional_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_optional(type, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_optional_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_OPTIONAL }, .type = type, @@ -1607,20 +1076,9 @@ rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_nod rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location, rbs_node_t *self_type) { rbs_types_proc_t *instance = rbs_allocator_alloc(allocator, rbs_types_proc_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(type == NULL ? Qnil : type->cached_ruby_value); - rb_gc_register_mark_object(block == NULL ? Qnil : block->base.cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - rb_gc_register_mark_object(self_type == NULL ? Qnil : self_type->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_proc(type, block, location, self_type); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_proc_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_PROC }, .type = type, @@ -1632,21 +1090,12 @@ rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_node_t *typ return instance; } -rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, VALUE all_fields, rbs_location_t *location) { +rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, rbs_hash_t *all_fields, rbs_location_t *location) { rbs_types_record_t *instance = rbs_allocator_alloc(allocator, rbs_types_record_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(all_fields); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_record(all_fields, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_record_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_RECORD }, .all_fields = all_fields, @@ -1656,21 +1105,27 @@ rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, VALUE all_f return instance; } -rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location) { - rbs_types_tuple_t *instance = rbs_allocator_alloc(allocator, rbs_types_tuple_t); +rbs_types_record_fieldtype_t *rbs_types_record_fieldtype_new(rbs_allocator_t *allocator, rbs_node_t *type, bool required) { + rbs_types_record_fieldtype_t *instance = rbs_allocator_alloc(allocator, rbs_types_record_fieldtype_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(types == NULL ? Qnil : types->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_tuple(types, location); + *instance = (rbs_types_record_fieldtype_t) { + .base = (rbs_node_t) { + .type = RBS_TYPES_RECORD_FIELDTYPE + }, + .type = type, + .required = required, + }; + + return instance; +} + +rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location) { + rbs_types_tuple_t *instance = rbs_allocator_alloc(allocator, rbs_types_tuple_t); - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_tuple_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_TUPLE }, .types = types, @@ -1683,18 +1138,9 @@ rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_node_list rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location) { rbs_types_union_t *instance = rbs_allocator_alloc(allocator, rbs_types_union_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(types == NULL ? Qnil : types->cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_union(types, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_union_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_UNION }, .types = types, @@ -1707,17 +1153,9 @@ rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_node_list rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, rbs_node_t *return_type) { rbs_types_untypedfunction_t *instance = rbs_allocator_alloc(allocator, rbs_types_untypedfunction_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(return_type == NULL ? Qnil : return_type->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_untyped_function(return_type); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_untypedfunction_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_UNTYPEDFUNCTION }, .return_type = return_type, @@ -1729,18 +1167,9 @@ rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allo rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_location_t *location) { rbs_types_variable_t *instance = rbs_allocator_alloc(allocator, rbs_types_variable_t); - // Disable GC for all these Ruby objects. - rb_gc_register_mark_object(name == NULL ? Qnil : name->base.cached_ruby_value); - rb_gc_register_mark_object(location == NULL ? Qnil : location->cached_ruby_value); - - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - VALUE ruby_value = rbs_variable(name, location); - - rb_gc_register_mark_object(ruby_value); *instance = (rbs_types_variable_t) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = RBS_TYPES_VARIABLE }, .name = name, diff --git a/src/constants.c b/src/constants.c index 19e12e0a9..dd1e2ee5c 100644 --- a/src/constants.c +++ b/src/constants.c @@ -47,7 +47,6 @@ VALUE RBS_AST_Members_MethodDefinition_Overload; VALUE RBS_AST_Members_Prepend; VALUE RBS_AST_Members_Private; VALUE RBS_AST_Members_Public; -VALUE RBS_AST_Symbol; VALUE RBS_AST_TypeParam; VALUE RBS_MethodType; VALUE RBS_Namespace; @@ -122,7 +121,6 @@ void rbs__init_constants(void) { IMPORT_CONSTANT(RBS_AST_Members_Prepend, RBS_AST_Members, "Prepend"); IMPORT_CONSTANT(RBS_AST_Members_Private, RBS_AST_Members, "Private"); IMPORT_CONSTANT(RBS_AST_Members_Public, RBS_AST_Members, "Public"); - IMPORT_CONSTANT(RBS_AST_Symbol, RBS_AST, "Symbol"); IMPORT_CONSTANT(RBS_AST_TypeParam, RBS_AST, "TypeParam"); IMPORT_CONSTANT(RBS_MethodType, RBS, "MethodType"); IMPORT_CONSTANT(RBS_Namespace, RBS, "Namespace"); diff --git a/src/encoding.c b/src/encoding.c new file mode 100644 index 000000000..fdac17bbe --- /dev/null +++ b/src/encoding.c @@ -0,0 +1,57 @@ +#include "rbs/encoding.h" + +unsigned int utf8_to_codepoint(const rbs_string_t string) { + unsigned int codepoint = 0; + int remaining_bytes = 0; + + const char *s = string.start; + const char *end = string.end; + + if (s >= end) return 0; // End of string + + if ((*s & 0x80) == 0) { + // Single byte character (0xxxxxxx) + return *s; + } else if ((*s & 0xE0) == 0xC0) { + // Two byte character (110xxxxx 10xxxxxx) + codepoint = *s & 0x1F; + remaining_bytes = 1; + } else if ((*s & 0xF0) == 0xE0) { + // Three byte character (1110xxxx 10xxxxxx 10xxxxxx) + codepoint = *s & 0x0F; + remaining_bytes = 2; + } else if ((*s & 0xF8) == 0xF0) { + // Four byte character (11110xxx 10xxxxxx 10xxxxxx 10xxxxxx) + codepoint = *s & 0x07; + remaining_bytes = 3; + } else { + // Invalid UTF-8 sequence + return 0xFFFD; // Unicode replacement character + } + + s++; + while (remaining_bytes > 0 && s < end) { + if ((*s & 0xC0) != 0x80) { + // Invalid continuation byte + return 0xFFFD; + } + codepoint = (codepoint << 6) | (*s & 0x3F); + s++; + remaining_bytes--; + } + + if (remaining_bytes > 0) { + // Incomplete sequence + return 0xFFFD; + } + + return codepoint; +} + +int utf8_codelen(unsigned int c) { + if (c <= 0x7F) return 1; + if (c <= 0x7FF) return 2; + if (c <= 0xFFFF) return 3; + if (c <= 0x10FFFF) return 4; + return 1; // Invalid Unicode codepoint, treat as 1 byte +} diff --git a/src/rbs_string.c b/src/rbs_string.c new file mode 100644 index 000000000..a0b13c747 --- /dev/null +++ b/src/rbs_string.c @@ -0,0 +1,112 @@ +#include "rbs/rbs_string.h" + +#include +#include +#include +#include + +rbs_string_t rbs_string_shared_new(const char *start, const char *end) { + return (rbs_string_t) { + .start = start, + .end = end, + .type = RBS_STRING_SHARED, + }; +} + +rbs_string_t rbs_string_owned_new(const char *start, const char *end) { + return (rbs_string_t) { + .start = start, + .end = end, + .type = RBS_STRING_OWNED, + }; +} + +void rbs_string_ensure_owned(rbs_string_t *self) { + if (self->type == RBS_STRING_OWNED) return; + + char *buffer = (char *)malloc(self->end - self->start + 1); + size_t length = self->end - self->start; + + strncpy(buffer, self->start, length); + buffer[length] = '\0'; + + *self = rbs_string_owned_new(buffer, buffer + length); +} + +rbs_string_t rbs_string_offset(const rbs_string_t self, const size_t offset) { + return rbs_string_shared_new(self.start + offset, self.end); +} + +// // Ensure the given string is shared, so that we can slice it without needing to free the old string. +// static void ensure_shared(rbs_string_t *self) { +// if (self->type != RBS_STRING_SHARED) { +// fprintf(stderr, "Calling this function requires a shared string.\n"); +// exit(EXIT_FAILURE); +// } +// } + +void rbs_string_drop_first(rbs_string_t *self, size_t n) { + // ensure_shared(self); + + self->start += n; +} + +void rbs_string_drop_last(rbs_string_t *self, size_t n) { + // ensure_shared(self); + + self->end -= n; +} + +void rbs_string_limit_length(rbs_string_t *self, size_t new_length) { + // ensure_shared(self); + + self->end = self->start + new_length; +} + +rbs_string_t rbs_string_slice(const rbs_string_t self, size_t start_inset, size_t length) { + if (length > rbs_string_len(self)) { + fprintf(stderr, "rbs_string_slice tried to slice more characters than exist in the string.\n"); + exit(EXIT_FAILURE); + } + + if (self.start + start_inset + length >= self.end) { + fprintf(stderr, "rbs_string_slice tried to slice past the end of the string.\n"); + exit(EXIT_FAILURE); + } + + const char *new_start = self.start + start_inset; + + return rbs_string_shared_new(new_start, new_start + length); +} + +void rbs_string_strip_whitespace(rbs_string_t *self) { + // ensure_shared(self); + + const char *new_start = self->start; + while (isspace(*new_start) && new_start < self->end) { + new_start++; + } + + if (new_start == self->end) { // Handle empty string case + self->start = new_start; + return; + } + + const char *new_end = self->end - 1; + while (isspace(*new_end) && new_start < new_end) { + new_end--; + } + + self->start = new_start; + self->end = new_end + 1; +} + +size_t rbs_string_len(const rbs_string_t self) { + return self.end - self.start; +} + +bool rbs_string_equal(const rbs_string_t lhs, const rbs_string_t rhs) { + if (lhs.start == rhs.start && lhs.end == rhs.end) return true; + if (rbs_string_len(lhs) != rbs_string_len(rhs)) return false; + return strncmp(lhs.start, rhs.start, rbs_string_len(lhs)) == 0; +} diff --git a/src/rbs_unescape.c b/src/rbs_unescape.c new file mode 100644 index 000000000..d538b5282 --- /dev/null +++ b/src/rbs_unescape.c @@ -0,0 +1,126 @@ +#include "rbs/encoding.h" +#include "rbs/rbs_unescape.h" +#include "rbs/rbs_string.h" +#include +#include +#include + +// Define the escape character mappings +// TODO: use a switch instead +static const struct { + const char* from; + const char* to; +} TABLE[] = { + {"\\a", "\a"}, + {"\\b", "\b"}, + {"\\e", "\033"}, + {"\\f", "\f"}, + {"\\n", "\n"}, + {"\\r", "\r"}, + {"\\s", " "}, + {"\\t", "\t"}, + {"\\v", "\v"}, + {"\\\"", "\""}, + {"\\'", "'"}, + {"\\\\", "\\"}, + {"\\", ""} +}; + +// Helper function to convert hex string to integer +static int hex_to_int(const char* hex, int length) { + int result = 0; + for (int i = 0; i < length; i++) { + result = result * 16 + (isdigit(hex[i]) ? hex[i] - '0' : tolower(hex[i]) - 'a' + 10); + } + return result; +} + +// Helper function to convert octal string to integer +static int octal_to_int(const char* octal, int length) { + int result = 0; + for (int i = 0; i < length; i++) { + result = result * 8 + (octal[i] - '0'); + } + return result; +} + +rbs_string_t unescape_string(const rbs_string_t string, bool is_double_quote) { + if (!string.start) return RBS_STRING_NULL; + + size_t len = string.end - string.start; + const char* input = string.start; + + char* output = malloc(len + 1); + if (!output) return RBS_STRING_NULL; + + size_t i = 0, j = 0; + while (i < len) { + if (input[i] == '\\' && i + 1 < len) { + if (is_double_quote) { + if (isdigit(input[i+1])) { + // Octal escape + int octal_len = 1; + while (octal_len < 3 && i + 1 + octal_len < len && isdigit(input[i + 1 + octal_len])) octal_len++; + int value = octal_to_int(input + i + 1, octal_len); + output[j++] = (char)value; + i += octal_len + 1; + } else if (input[i+1] == 'x' && i + 3 < len) { + // Hex escape + int hex_len = isxdigit(input[i+3]) ? 2 : 1; + int value = hex_to_int(input + i + 2, hex_len); + output[j++] = (char)value; + i += hex_len + 2; + } else if (input[i+1] == 'u' && i + 5 < len) { + // Unicode escape + int value = hex_to_int(input + i + 2, 4); + output[j++] = (char)value; + i += 6; + } else { + // Other escapes + int found = 0; + for (size_t k = 0; k < sizeof(TABLE) / sizeof(TABLE[0]); k++) { + if (strncmp(input + i, TABLE[k].from, strlen(TABLE[k].from)) == 0) { + output[j++] = TABLE[k].to[0]; + i += strlen(TABLE[k].from); + found = 1; + break; + } + } + if (!found) { + output[j++] = input[i++]; + } + } + } else { + /* Single quote: only escape ' and \ */ + if (input[i+1] == '\'' || input[i+1] == '\\') { + output[j++] = input[i+1]; + i += 2; + } else { + output[j++] = input[i++]; + } + } + } else { + output[j++] = input[i++]; + } + } + output[j] = '\0'; + rbs_string_t str = rbs_string_shared_new(output, output + j); + rbs_string_ensure_owned(&str); + return str; +} + +rbs_string_t rbs_unquote_string2(rbs_string_t input) { + unsigned int first_char = utf8_to_codepoint(input); + size_t byte_length = rbs_string_len(input); + + ptrdiff_t start_offset = 0; + if (first_char == '"' || first_char == '\'' || first_char == '`') { + int bs = utf8_codelen(first_char); + start_offset += bs; + byte_length -= 2 * bs; + } + + rbs_string_t string = rbs_string_offset(input, start_offset); + string.end = string.start + byte_length; + return unescape_string(string, first_char == '"'); +} diff --git a/src/ruby_objs.c b/src/ruby_objs.c deleted file mode 100644 index 870723be1..000000000 --- a/src/ruby_objs.c +++ /dev/null @@ -1,805 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* This file is generated by the templates/template.rb script and should not */ -/* be modified manually. */ -/* To change the template see */ -/* templates/src/ruby_objs.c.erb */ -/*----------------------------------------------------------------------------*/ - -#include "rbs_extension.h" - -#ifdef RB_PASS_KEYWORDS - // Ruby 2.7 or later - #define CLASS_NEW_INSTANCE(klass, argc, argv)\ - rb_class_new_instance_kw(argc, argv, klass, RB_PASS_KEYWORDS) -#else - // Ruby 2.6 - #define CLASS_NEW_INSTANCE(receiver, argc, argv)\ - rb_class_new_instance(argc, argv, receiver) -#endif - -VALUE rbs_ast_annotation(VALUE string, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("string")), string); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_AST_Annotation, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_comment(VALUE string, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("string")), string); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_AST_Comment, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_decl_class(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_params")), type_params == NULL ? Qnil : type_params->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("super_class")), super_class == NULL ? Qnil : super_class->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("members")), members == NULL ? Qnil : members->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_Class, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_decl_class_super(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_Class_Super, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_decl_class_alias(rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("new_name")), new_name == NULL ? Qnil : new_name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("old_name")), old_name == NULL ? Qnil : old_name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_ClassAlias, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_decl_constant(rbs_typename_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_Constant, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_decl_global(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_Global, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_decl_interface(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_params")), type_params == NULL ? Qnil : type_params->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("members")), members == NULL ? Qnil : members->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_Interface, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_decl_module(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_params")), type_params == NULL ? Qnil : type_params->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("self_types")), self_types == NULL ? Qnil : self_types->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("members")), members == NULL ? Qnil : members->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_Module, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_decl_module_self(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_Module_Self, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_decl_module_alias(rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, VALUE comment) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("new_name")), new_name == NULL ? Qnil : new_name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("old_name")), old_name == NULL ? Qnil : old_name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_ModuleAlias, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_decl_type_alias(rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_params")), type_params == NULL ? Qnil : type_params->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - - return CLASS_NEW_INSTANCE( - RBS_AST_Declarations_TypeAlias, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_directives_use(rbs_node_list_t *clauses, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("clauses")), clauses == NULL ? Qnil : clauses->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_AST_Directives_Use, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_directives_use_single_clause(rbs_typename_t *type_name, rbs_ast_symbol_t *new_name, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_name")), type_name == NULL ? Qnil : type_name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("new_name")), new_name == NULL ? Qnil : new_name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_AST_Directives_Use_SingleClause, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_directives_use_wildcard_clause(rbs_namespace_t *namespace, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("namespace")), namespace == NULL ? Qnil : namespace->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_AST_Directives_Use_WildcardClause, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_members_alias(rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("new_name")), new_name == NULL ? Qnil : new_name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("old_name")), old_name == NULL ? Qnil : old_name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("kind")), kind == NULL ? Qnil : kind->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_Alias, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_members_attr_accessor(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("ivar_name")), ivar_name == NULL ? Qnil : ivar_name->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("kind")), kind == NULL ? Qnil : kind->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("visibility")), visibility == NULL ? Qnil : visibility->base.cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_AttrAccessor, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_members_attr_reader(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("ivar_name")), ivar_name == NULL ? Qnil : ivar_name->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("kind")), kind == NULL ? Qnil : kind->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("visibility")), visibility == NULL ? Qnil : visibility->base.cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_AttrReader, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_members_attr_writer(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_ast_symbol_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, rbs_ast_symbol_t *visibility) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("ivar_name")), ivar_name == NULL ? Qnil : ivar_name->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("kind")), kind == NULL ? Qnil : kind->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("visibility")), visibility == NULL ? Qnil : visibility->base.cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_AttrWriter, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_members_class_instance_variable(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_ClassInstanceVariable, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_members_class_variable(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_ClassVariable, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_members_extend(rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_Extend, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_members_include(rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_Include, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_members_instance_variable(rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, VALUE comment) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_InstanceVariable, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_members_method_definition(rbs_ast_symbol_t *name, rbs_ast_symbol_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment, bool overloading, rbs_ast_symbol_t *visibility) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("kind")), kind == NULL ? Qnil : kind->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("overloads")), overloads == NULL ? Qnil : overloads->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("overloading")), overloading ? Qtrue : Qfalse); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("visibility")), visibility == NULL ? Qnil : visibility->base.cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_MethodDefinition, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_members_method_definition_overload(rbs_node_list_t *annotations, rbs_node_t *method_type) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("method_type")), method_type == NULL ? Qnil : method_type->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_MethodDefinition_Overload, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_members_prepend(rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, VALUE comment) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("annotations")), annotations == NULL ? Qnil : annotations->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("comment")), comment); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_Prepend, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_members_private(rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_Private, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_members_public(rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_AST_Members_Public, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_symbol() { - VALUE _init_kwargs = rb_hash_new(); - - return CLASS_NEW_INSTANCE( - RBS_AST_Symbol, - 1, - &_init_kwargs - ); -} - -VALUE rbs_ast_type_param(rbs_ast_symbol_t *name, rbs_ast_symbol_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("variance")), variance == NULL ? Qnil : variance->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("upper_bound")), upper_bound == NULL ? Qnil : upper_bound->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("default_type")), default_type == NULL ? Qnil : default_type->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("unchecked")), unchecked ? Qtrue : Qfalse); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_AST_TypeParam, - 1, - &_init_kwargs - ); -} - -VALUE rbs_method_type(rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type_params")), type_params == NULL ? Qnil : type_params->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("block")), block == NULL ? Qnil : block->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_MethodType, - 1, - &_init_kwargs - ); -} - -VALUE rbs_namespace(rbs_node_list_t *path, bool absolute) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("path")), path == NULL ? Qnil : path->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("absolute")), absolute ? Qtrue : Qfalse); - - return CLASS_NEW_INSTANCE( - RBS_Namespace, - 1, - &_init_kwargs - ); -} - -VALUE rbs_type_name(rbs_namespace_t *namespace, rbs_ast_symbol_t *name) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("namespace")), namespace == NULL ? Qnil : namespace->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_TypeName, - 1, - &_init_kwargs - ); -} - -VALUE rbs_alias(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Alias, - 1, - &_init_kwargs - ); -} - -VALUE rbs_bases_any(VALUE todo, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("todo")), todo); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Any, - 1, - &_init_kwargs - ); -} - -VALUE rbs_bases_bool(rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Bool, - 1, - &_init_kwargs - ); -} - -VALUE rbs_bases_bottom(rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Bottom, - 1, - &_init_kwargs - ); -} - -VALUE rbs_bases_class(rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Class, - 1, - &_init_kwargs - ); -} - -VALUE rbs_bases_instance(rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Instance, - 1, - &_init_kwargs - ); -} - -VALUE rbs_bases_nil(rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Nil, - 1, - &_init_kwargs - ); -} - -VALUE rbs_bases_self(rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Self, - 1, - &_init_kwargs - ); -} - -VALUE rbs_bases_top(rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Top, - 1, - &_init_kwargs - ); -} - -VALUE rbs_bases_void(rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Bases_Void, - 1, - &_init_kwargs - ); -} - -VALUE rbs_block(rbs_node_t *type, bool required, rbs_node_t *self_type) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("required")), required ? Qtrue : Qfalse); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("self_type")), self_type == NULL ? Qnil : self_type->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Block, - 1, - &_init_kwargs - ); -} - -VALUE rbs_class_instance(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_ClassInstance, - 1, - &_init_kwargs - ); -} - -VALUE rbs_class_singleton(rbs_typename_t *name, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_ClassSingleton, - 1, - &_init_kwargs - ); -} - -VALUE rbs_function(VALUE required_positionals, VALUE optional_positionals, VALUE rest_positionals, VALUE trailing_positionals, VALUE required_keywords, VALUE optional_keywords, VALUE rest_keywords, VALUE return_type) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("required_positionals")), required_positionals); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("optional_positionals")), optional_positionals); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("rest_positionals")), rest_positionals); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("trailing_positionals")), trailing_positionals); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("required_keywords")), required_keywords); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("optional_keywords")), optional_keywords); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("rest_keywords")), rest_keywords); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("return_type")), return_type); - - return CLASS_NEW_INSTANCE( - RBS_Types_Function, - 1, - &_init_kwargs - ); -} - -VALUE rbs_function_param(rbs_node_t *type, rbs_ast_symbol_t *name, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Function_Param, - 1, - &_init_kwargs - ); -} - -VALUE rbs_interface(rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("args")), args == NULL ? Qnil : args->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Interface, - 1, - &_init_kwargs - ); -} - -VALUE rbs_intersection(rbs_node_list_t *types, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("types")), types == NULL ? Qnil : types->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Intersection, - 1, - &_init_kwargs - ); -} - -VALUE rbs_literal(VALUE literal, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("literal")), literal); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Literal, - 1, - &_init_kwargs - ); -} - -VALUE rbs_optional(rbs_node_t *type, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Optional, - 1, - &_init_kwargs - ); -} - -VALUE rbs_proc(rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location, rbs_node_t *self_type) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("type")), type == NULL ? Qnil : type->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("block")), block == NULL ? Qnil : block->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("self_type")), self_type == NULL ? Qnil : self_type->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Proc, - 1, - &_init_kwargs - ); -} - -VALUE rbs_record(VALUE all_fields, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("all_fields")), all_fields); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Record, - 1, - &_init_kwargs - ); -} - -VALUE rbs_tuple(rbs_node_list_t *types, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("types")), types == NULL ? Qnil : types->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Tuple, - 1, - &_init_kwargs - ); -} - -VALUE rbs_union(rbs_node_list_t *types, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("types")), types == NULL ? Qnil : types->cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Union, - 1, - &_init_kwargs - ); -} - -VALUE rbs_untyped_function(rbs_node_t *return_type) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("return_type")), return_type == NULL ? Qnil : return_type->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_UntypedFunction, - 1, - &_init_kwargs - ); -} - -VALUE rbs_variable(rbs_ast_symbol_t *name, rbs_location_t *location) { - VALUE _init_kwargs = rb_hash_new(); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("name")), name == NULL ? Qnil : name->base.cached_ruby_value); - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("location")), location == NULL ? Qnil : location->cached_ruby_value); - - return CLASS_NEW_INSTANCE( - RBS_Types_Variable, - 1, - &_init_kwargs - ); -} - diff --git a/src/util/rbs_constant_pool.c b/src/util/rbs_constant_pool.c index 406c36464..343902b51 100644 --- a/src/util/rbs_constant_pool.c +++ b/src/util/rbs_constant_pool.c @@ -301,6 +301,11 @@ rbs_constant_pool_insert_shared(rbs_constant_pool_t *pool, const uint8_t *start, return rbs_constant_pool_insert(pool, start, length, RBS_CONSTANT_POOL_BUCKET_DEFAULT); } +rbs_constant_id_t +rbs_constant_pool_insert_shared_with_encoding(rbs_constant_pool_t *pool, const uint8_t *start, size_t length, void *encoding) { + return rbs_constant_pool_insert_shared(pool, start, length); +} + /** * Insert a constant into a constant pool from memory that is now owned by the * constant pool. Returns the id of the constant, or RBS_CONSTANT_ID_UNSET if any diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index c6ee3c70e..3fb69392d 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -1,16 +1,31 @@ #include "ast_translation.h" +#include "ruby.h" +#include "ruby/encoding.h" + #include "rbs/constants.h" +#include "rbs_string_bridging.h" -#include +VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t *list) { + VALUE ruby_array = rb_ary_new(); + for (rbs_node_list_node_t *n = list->head; n != NULL; n = n->next) { + rb_ary_push(ruby_array, rbs_struct_to_ruby_value(ctx, n->node)); + } -VALUE rbs_node_list_to_ruby_array(rbs_node_list_t *list) { - return list->cached_ruby_value; + return ruby_array; } -VALUE rbs_hash_to_ruby_hash(rbs_hash_t *hash) { - return hash->cached_ruby_value; +VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t ctx, rbs_hash_t *rbs_hash) { + VALUE ruby_hash = rb_hash_new(); + + for (rbs_hash_node_t *n = rbs_hash->head; n != NULL; n = n->next) { + VALUE key = rbs_struct_to_ruby_value(ctx, n->key); + VALUE value = rbs_struct_to_ruby_value(ctx, n->value); + rb_hash_aset(ruby_hash, key, value); + } + + return ruby_hash; } VALUE rbs_loc_to_ruby_location(rbs_location_t *loc) { @@ -27,43 +42,46 @@ VALUE rbs_loc_to_ruby_location(rbs_location_t *loc) { rb_class_new_instance(argc, argv, receiver) #endif -const char* get_class_name(VALUE o) { - VALUE klass = rb_class_of(o); // Get the class of the object - VALUE klass_name = rb_class_name(klass); // Get the name of the class - const char* name = StringValueCStr(klass_name); // Convert to C string - return name; -} - -VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { +VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instance) { if (instance == NULL) return Qnil; - VALUE ruby_value = instance->cached_ruby_value; - - if (ruby_value == Qnil || ruby_value == Qundef) { - fprintf(stderr, "cached_ruby_value is NULL\n"); - exit(1); - } - - const char *class_name = get_class_name(ruby_value); - switch (instance->type) { <%- nodes.each do |node| -%> case <%= node.c_type_enum_name %>: { - <%- - rbs_struct_to_ruby_value_shitlist = [ - "RBS::AST::Bool", - "RBS::AST::Symbol", - "RBS::AST::Declarations::Nodes", - "RBS::AST::Directives::Nodes", - ] - - if rbs_struct_to_ruby_value_shitlist.include?(node.ruby_full_name) -%> - return instance->cached_ruby_value; + <%- case node.ruby_full_name -%> + <%- when "RBS::AST::Bool" -%> + return ((rbs_ast_bool_t *) instance)->value ? Qtrue : Qfalse; + + <%- when "RBS::AST::Integer" -%> + rbs_ast_integer_t *integer_node = (rbs_ast_integer_t *) instance; + rbs_string_t string_repr = integer_node->string_representation; + + VALUE str = rb_enc_str_new(string_repr.start, rbs_string_len(string_repr), rb_utf8_encoding()); + + return rb_funcall(str, rb_intern("to_i"), 0); + + <%- when "RBS::AST::String" -%> + rbs_ast_string_t *string_node = (rbs_ast_string_t *) instance; + rbs_string_t s = string_node->string; + + return rb_enc_str_new(s.start, rbs_string_len(s), rb_utf8_encoding()); + + <%- when "RBS::Types::Record::FieldType" -%> + rbs_types_record_fieldtype_t *record_fieldtype = (rbs_types_record_fieldtype_t *) instance; + + VALUE array = rb_ary_new(); + rb_ary_push(array, rbs_struct_to_ruby_value(ctx, record_fieldtype->type)); + rb_ary_push(array, record_fieldtype->required ? Qtrue : Qfalse); + return array; + + <%- when "RBS::Signature" -%> + rbs_signature_t *signature = (rbs_signature_t *) instance; + + VALUE array = rb_ary_new(); + rb_ary_push(array, rbs_node_list_to_ruby_array(ctx, signature->directives)); + rb_ary_push(array, rbs_node_list_to_ruby_array(ctx, signature->declarations)); + return array; <%- else -%> - if (strcmp(class_name, "<%= node.ruby_full_name %>") != 0) { - fprintf(stderr, "Expected class name: <%= node.ruby_full_name %>, got %s\n", class_name); - exit(1); - } <%- if node.fields.any? -%> <%# This prevents "warning: unused variable 'node'" %> <%= node.c_type_name %> *node = (<%= node.c_type_name %> *)instance; <%- end -%> @@ -74,21 +92,35 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { <%- when "VALUE" -%> rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), node-><%= field.name %>); <%- when "rbs_node_list" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_node_list_to_ruby_array(node-><%= field.name %>)); + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_node_list_to_ruby_array(ctx, node-><%= field.name %>)); <%- when "rbs_hash" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_hash_to_ruby_hash(node-><%= field.name %>)); + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_hash_to_ruby_hash(ctx, node-><%= field.name %>)); <%- when "rbs_location" -%> rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_loc_to_ruby_location(node-><%= field.name %>)); - <%- when "comment" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), node-><%= field.name %>->string); + <%- when "rbs_ast_symbol" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.name %>)); // rbs_ast_symbol + <%- when "rbs_keyword" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.name %>)); // rbs_keyword + <%- when "rbs_string" -%> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_string_to_ruby_string(&node-><%= field.name %>)); <%- when "bool" -%> rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), node-><%= field.name %> ? Qtrue : Qfalse); <%- else -%> <%- unless field.ast_node? -%> #warning unexpected type <%= field.c_type -%> <%- end -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value((rbs_node_t *) node-><%= field.name %>)); // <%= field.c_type %> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.name %>)); // <%= field.c_type %> + <%- end -%> <%- end -%> + + <%- case node.ruby_full_name -%> + <%- when "RBS::AST::Declarations::Class", "RBS::AST::Declarations::Module", "RBS::AST::Declarations::Interface", "RBS::AST::Declarations::TypeAlias", "RBS::MethodType" -%> + rb_funcall( + RBS_AST_TypeParam, + rb_intern("resolve_variables"), + 1, + rb_hash_lookup(h, ID2SYM(rb_intern("type_params"))) + ); <%- end -%> return CLASS_NEW_INSTANCE( @@ -99,5 +131,25 @@ VALUE rbs_struct_to_ruby_value(rbs_node_t *instance) { <%- end -%> } <%- end -%> + case RBS_KEYWORD: { + rbs_constant_t *constant = rbs_constant_pool_id_to_constant(RBS_GLOBAL_CONSTANT_POOL, ((rbs_keyword_t *) instance)->constant_id); + assert(constant != NULL && "constant is NULL"); + assert(constant->start != NULL && "constant->start is NULL"); + + return ID2SYM(rb_intern2((const char *) constant->start, constant->length)); + } + case RBS_AST_SYMBOL: { + rbs_constant_t *constant = rbs_constant_pool_id_to_constant(ctx.constant_pool, ((rbs_keyword_t *) instance)->constant_id); + assert(constant != NULL && "constant is NULL"); + assert(constant->start != NULL && "constant->start is NULL"); + + // FIXME: Add `rb_encoding` to the `ctx` and use it here. + rb_encoding *encoding = rb_usascii_encoding(); + + return ID2SYM(rb_intern3((const char *) constant->start, constant->length, encoding)); + } + case RBS_OTHER_RUBY_VALUE: { + return ((rbs_other_ruby_value_t *) instance)->ruby_value; + } } } diff --git a/templates/ext/rbs_extension/ast_translation.h.erb b/templates/ext/rbs_extension/ast_translation.h.erb index a9a92921a..fcc26f904 100644 --- a/templates/ext/rbs_extension/ast_translation.h.erb +++ b/templates/ext/rbs_extension/ast_translation.h.erb @@ -4,9 +4,8 @@ #include "ruby.h" #include "rbs/ast.h" -VALUE rbs_node_list_to_ruby_array(rbs_node_list_t *list); -VALUE rbs_hash_to_ruby_hash(rbs_hash_t *hash); -VALUE rbs_loc_to_ruby_location(rbs_location_t *loc); -VALUE rbs_struct_to_ruby_value(rbs_node_t *instance); +VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *list); +VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t, rbs_hash_t *hash); +VALUE rbs_struct_to_ruby_value(rbs_translation_context_t, rbs_node_t *instance); #endif diff --git a/templates/include/rbs/ast.h.erb b/templates/include/rbs/ast.h.erb index 86abbb92c..33e55c036 100644 --- a/templates/include/rbs/ast.h.erb +++ b/templates/include/rbs/ast.h.erb @@ -3,19 +3,28 @@ #include "ruby.h" #include "rbs/util/rbs_allocator.h" +#include "rbs/util/rbs_constant_pool.h" +#include "rbs_string.h" #include "rbs_location.h" enum rbs_node_type { + RBS_OTHER_RUBY_VALUE = 0, <%- nodes.each_with_index do |node, index| -%> <%= node.c_type_enum_name %> = <%= index + 1 %>, <%- end -%> + RBS_KEYWORD, + RBS_AST_SYMBOL, }; typedef struct rbs_node { - VALUE cached_ruby_value; enum rbs_node_type type; } rbs_node_t; +/// A bag of values needed when copying RBS C structs into Ruby objects. +typedef struct rbs_translation_context { + rbs_constant_pool_t *constant_pool; +} rbs_translation_context_t; + /* rbs_node_list_node */ typedef struct rbs_node_list_node { @@ -28,7 +37,6 @@ typedef struct rbs_node_list { rbs_node_list_node_t *head; rbs_node_list_node_t *tail; size_t length; - VALUE cached_ruby_value; } rbs_node_list_t; rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *); @@ -48,7 +56,6 @@ typedef struct rbs_hash { rbs_hash_node_t *head; rbs_hash_node_t *tail; size_t length; - VALUE cached_ruby_value; } rbs_hash_t; rbs_hash_t* rbs_hash_new(rbs_allocator_t *); @@ -72,6 +79,32 @@ typedef struct <%= node.c_base_name %> { <%- end -%> +/// `rbs_keyword_t` models RBS keywords like "private", "instance", "covariant", etc. +/// These are stored in the global constant pool, and get surfaced to Ruby as `Symbol`s, +/// just like `rbs_ast_symbol_t`s. +typedef struct rbs_keyword { + rbs_node_t base; + rbs_constant_id_t constant_id; +} rbs_keyword_t; + +rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *, rbs_constant_id_t); + +/// `rbs_ast_symbol_t` models user-defined identifiers like class names, method names, etc. +/// These get stored in the parser's own constant pool, and get surfaced to Ruby as `Symbol`s. +typedef struct rbs_ast_symbol { + rbs_node_t base; + rbs_constant_id_t constant_id; +} rbs_ast_symbol_t; + +rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *, rbs_constant_pool_t *, rbs_constant_id_t); + +typedef struct rbs_other_ruby_value { + rbs_node_t base; + VALUE ruby_value; +} rbs_other_ruby_value_t; + +rbs_other_ruby_value_t *rbs_other_ruby_value_new(VALUE ruby_value); + <%- nodes.each do |node| -%> <%= node.c_type_name %> *<%= node.c_constructor_function_name %>(<%= node.constructor_params.map(&:parameter_decl).join(", ") %>); <%- end -%> diff --git a/templates/include/rbs/ruby_objs.h.erb b/templates/include/rbs/ruby_objs.h.erb deleted file mode 100644 index 499ba4db8..000000000 --- a/templates/include/rbs/ruby_objs.h.erb +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef RBS__RUBY_OBJS_H -#define RBS__RUBY_OBJS_H - -#include "ruby.h" - -<%- nodes.filter(&:expose_to_ruby?).each do |node| -%> -VALUE <%= node.c_function_name %>(<%= node.fields.map(&:parameter_decl).join(", ") %>); -<%- end -%> - -#endif diff --git a/templates/src/ast.c.erb b/templates/src/ast.c.erb index 3caebdbc1..bd68d7cf8 100644 --- a/templates/src/ast.c.erb +++ b/templates/src/ast.c.erb @@ -1,7 +1,5 @@ #include "rbs/ast.h" -#include "rbs/ruby_objs.h" - /* rbs_node_list */ rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *allocator) { @@ -11,17 +9,12 @@ rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *allocator) { .head = NULL, .tail = NULL, .length = 0, - .cached_ruby_value = rb_ary_new(), }; - rb_gc_register_mark_object(list->cached_ruby_value); - return list; } void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node) { - rb_gc_register_mark_object(node->cached_ruby_value); - rbs_node_list_node_t *new_node = rbs_allocator_alloc(list->allocator, rbs_node_list_node_t); *new_node = (rbs_node_list_node_t) { .node = node, @@ -35,9 +28,8 @@ void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node) { list->tail->next = new_node; list->tail = new_node; } - list->length++; - rb_ary_push(list->cached_ruby_value, node->cached_ruby_value); + list->length++; } /* rbs_hash */ @@ -49,11 +41,8 @@ rbs_hash_t* rbs_hash_new(rbs_allocator_t *allocator) { .head = NULL, .tail = NULL, .length = 0, - .cached_ruby_value = rb_hash_new(), }; - rb_gc_register_mark_object(hash->cached_ruby_value); - return hash; } @@ -62,10 +51,21 @@ bool rbs_node_equal(rbs_node_t *lhs, rbs_node_t *rhs) { if (lhs->type != rhs->type) return false; switch (lhs->type) { + case RBS_AST_SYMBOL: + return ((rbs_ast_symbol_t *)lhs)->constant_id == ((rbs_ast_symbol_t *) rhs)->constant_id; + case RBS_KEYWORD: + return ((rbs_keyword_t *)lhs)->constant_id == ((rbs_keyword_t *) rhs)->constant_id; case RBS_AST_BOOL: return ((rbs_ast_bool_t *)lhs)->value == ((rbs_ast_bool_t *) rhs)->value; + case RBS_AST_INTEGER: + return rbs_string_equal(((rbs_ast_integer_t *) lhs)->string_representation, ((rbs_ast_integer_t *) rhs)->string_representation); + case RBS_AST_STRING: + return rbs_string_equal(((rbs_ast_string_t *) lhs)->string, ((rbs_ast_string_t *) rhs)->string); + case RBS_OTHER_RUBY_VALUE: + return rb_equal(((rbs_other_ruby_value_t *) lhs)->ruby_value, ((rbs_other_ruby_value_t *) rhs)->ruby_value); default: - return rb_equal(lhs->cached_ruby_value, rhs->cached_ruby_value); + printf("Unhandled node type: %d\n", lhs->type); + return false; } } @@ -83,17 +83,12 @@ rbs_hash_node_t* rbs_hash_find(rbs_hash_t *hash, rbs_node_t *key) { } void rbs_hash_set(rbs_hash_t *hash, rbs_node_t *key, rbs_node_t *value) { - rb_gc_register_mark_object(key->cached_ruby_value); - rb_gc_register_mark_object(value->cached_ruby_value); - rbs_hash_node_t *existing_node = rbs_hash_find(hash, key); if (existing_node != NULL) { existing_node->value = value; return; } - rb_hash_aset(hash->cached_ruby_value, key->cached_ruby_value, value->cached_ruby_value); - rbs_hash_node_t *new_node = rbs_allocator_alloc(hash->allocator, rbs_hash_node_t); new_node->key = key; new_node->value = value; @@ -113,29 +108,57 @@ rbs_node_t* rbs_hash_get(rbs_hash_t *hash, rbs_node_t *key) { return node ? node->value : NULL; } +rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *allocator, rbs_constant_id_t constant_id) { + rbs_keyword_t *instance = rbs_allocator_alloc(allocator, rbs_keyword_t); + + *instance = (rbs_keyword_t) { + .base = (rbs_node_t) { + .type = RBS_KEYWORD, + }, + .constant_id = constant_id, + }; + + return instance; +} + +rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_constant_pool_t *constant_pool, rbs_constant_id_t constant_id) { + rbs_ast_symbol_t *instance = rbs_allocator_alloc(allocator, rbs_ast_symbol_t); + + *instance = (rbs_ast_symbol_t) { + .base = (rbs_node_t) { + .type = RBS_AST_SYMBOL, + }, + .constant_id = constant_id, + }; + + return instance; +} + +rbs_other_ruby_value_t *rbs_other_ruby_value_new(VALUE ruby_value) { + rb_gc_register_mark_object(ruby_value); + + rbs_other_ruby_value_t *instance = malloc(sizeof(rbs_other_ruby_value_t)); + + *instance = (rbs_other_ruby_value_t) { + .base = (rbs_node_t) { + .type = RBS_OTHER_RUBY_VALUE + }, + .ruby_value = ruby_value, + }; + + return instance; +} + <%- nodes.each do |node| -%> <%= node.c_type_name %> *<%= node.c_constructor_function_name %>(<%= node.constructor_params.map(&:parameter_decl).join(", ") %>) { <%= node.c_type_name %> *instance = rbs_allocator_alloc(allocator, <%= node.c_type_name %>); - // Disable GC for all these Ruby objects. - <%- node.fields.each do |field| -%> - rb_gc_register_mark_object(<%= field.cached_ruby_value_expr %>); + <%- node.fields.filter { |f| f.c_type == "VALUE" }.each do |f| -%> + rb_gc_register_mark_object(<%= f.name %>); <%- end -%> - <%- if node.builds_ruby_object_internally? -%> - // Generate our own Ruby VALUE here, rather than accepting it from a parameter. - <%- if node.c_type_name == "rbs_ast_bool_t" -%> - VALUE ruby_value = value ? Qtrue : Qfalse; - <%- else -%> - VALUE ruby_value = <%= node.c_function_name %>(<%= node.fields.map(&:name).join(", ") %>); - <%- end -%> - <%- end -%> - - rb_gc_register_mark_object(ruby_value); - *instance = (<%= node.c_type_name %>) { .base = (rbs_node_t) { - .cached_ruby_value = ruby_value, .type = <%= node.c_type_enum_name %> }, <%- node.fields.each do |field| -%> diff --git a/templates/src/ruby_objs.c.erb b/templates/src/ruby_objs.c.erb deleted file mode 100644 index a6e5f4fc4..000000000 --- a/templates/src/ruby_objs.c.erb +++ /dev/null @@ -1,27 +0,0 @@ -#include "rbs_extension.h" - -#ifdef RB_PASS_KEYWORDS - // Ruby 2.7 or later - #define CLASS_NEW_INSTANCE(klass, argc, argv)\ - rb_class_new_instance_kw(argc, argv, klass, RB_PASS_KEYWORDS) -#else - // Ruby 2.6 - #define CLASS_NEW_INSTANCE(receiver, argc, argv)\ - rb_class_new_instance(argc, argv, receiver) -#endif - -<%- nodes.filter(&:expose_to_ruby?).each do |node| -%> -VALUE <%= node.c_function_name %>(<%= node.fields.map(&:parameter_decl).join(", ") %>) { - VALUE _init_kwargs = rb_hash_new(); - <%- node.fields.each do |field| -%> - rb_hash_aset(_init_kwargs, ID2SYM(rb_intern("<%= field.name %>")), <%= field.cached_ruby_value_expr %>); - <%- end -%> - - return CLASS_NEW_INSTANCE( - <%= node.c_constant_name %>, - 1, - &_init_kwargs - ); -} - -<%- end -%> diff --git a/templates/template.rb b/templates/template.rb index c31c1b36d..692461813 100644 --- a/templates/template.rb +++ b/templates/template.rb @@ -54,20 +54,6 @@ def ast_node? @c_type.include?("_types_") end - # Returns a C expression that evaluates to the Ruby VALUE object for this field. - def cached_ruby_value_expr - case @c_type - when "VALUE" - @name - when "bool" - "#{@name} ? Qtrue : Qfalse" - when "rbs_node", "rbs_node_list", "rbs_location", "rbs_hash" - "#{@name} == NULL ? Qnil : #{@name}->cached_ruby_value" - else - "#{@name} == NULL ? Qnil : #{@name}->base.cached_ruby_value" - end - end - def needs_to_be_freed? !["VALUE", "bool"].include?(@c_type) end @@ -99,10 +85,6 @@ class Type # e.g. `rbs_ast_declarations_typealias_t` attr_reader :c_type_name #: String - # The name of the pre-existing C function which constructs new Ruby objects of this type. - # e.g. `rbs_ast_declarations_typealias_new` - attr_reader :c_function_name #: String - # The name of the C constant which stores the Ruby VALUE pointing to the generated class. # e.g. `RBS_AST_Declarations_TypeAlias` attr_reader :c_constant_name #: String @@ -111,7 +93,6 @@ class Type # e.g. `RBS_AST_Declarations` attr_reader :c_parent_constant_name #: String - attr_reader :c_struct_name #: String attr_reader :c_type_enum_name #: String attr_reader :constructor_params #: Array[RBS::Template::Field] @@ -120,25 +101,18 @@ class Type def initialize(yaml) @ruby_full_name = yaml["name"] @ruby_class_name = @ruby_full_name[/[^:]+\z/] # demodulize-like - name = @ruby_full_name.gsub("::", "_") - @c_function_name = name.gsub(/(^)?(_)?([A-Z](?:[A-Z]*(?=[A-Z_])|[a-z0-9]*))/) { ($1 || $2 || "_") + $3.downcase } # underscore-like - @c_function_name.gsub!(/^rbs_types_/, 'rbs_') - @c_function_name.gsub!(/^rbs_ast_declarations_/, 'rbs_ast_decl_') @c_constant_name = @ruby_full_name.gsub("::", "_") @c_parent_constant_name = @ruby_full_name.split("::")[0..-2].join("::").gsub("::", "_") @c_base_name = @c_constant_name.downcase @c_type_name = @c_base_name + "_t" - @c_struct_name = "#{@c_base_name}_t" @c_type_enum_name = @c_base_name.upcase @expose_to_ruby = yaml.fetch("expose_to_ruby", true) - @builds_ruby_object_internally = yaml.fetch("builds_ruby_object_internally", true) @fields = yaml.fetch("fields", []).map { |field| Field.from_hash(field) }.freeze @constructor_params = [Field.new(name: "allocator", c_type: "rbs_allocator_t *")] - @constructor_params << Field.new(name: "ruby_value", c_type: "VALUE") unless builds_ruby_object_internally? @constructor_params.concat @fields @constructor_params.freeze end @@ -154,12 +128,6 @@ def c_constructor_function_name #: String def expose_to_ruby? @expose_to_ruby end - - # When true, this object is expected to build its own Ruby VALUE object inside its `*_new()` function. - # When false, the `*_new()` function will take a Ruby VALUE as its first argument. - def builds_ruby_object_internally? - @builds_ruby_object_internally - end end class << self From f6753f70cd6b83966b48a77c0147dd6202bcf740 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Thu, 28 Nov 2024 15:50:49 -0500 Subject: [PATCH 007/111] Move `constants.c`/`.h` to `rbs_extension/` And rename it to `class_constants` to disambiguate it from `rbs_constant_id`, `rbs_constant_pool`, etc. --- Rakefile | 10 +++++----- ext/rbs_extension/ast_translation.c | 2 +- src/constants.c => ext/rbs_extension/class_constants.c | 2 +- .../constants.h => ext/rbs_extension/class_constants.h | 2 +- ext/rbs_extension/rbs_extension.h | 1 + include/rbs.h | 1 - templates/ext/rbs_extension/ast_translation.c.erb | 2 +- .../rbs_extension/class_constants.c.erb} | 0 .../rbs_extension/class_constants.h.erb} | 0 9 files changed, 10 insertions(+), 10 deletions(-) rename src/constants.c => ext/rbs_extension/class_constants.c (99%) rename include/rbs/constants.h => ext/rbs_extension/class_constants.h (97%) rename templates/{src/constants.c.erb => ext/rbs_extension/class_constants.c.erb} (100%) rename templates/{include/rbs/constants.h.erb => ext/rbs_extension/class_constants.h.erb} (100%) diff --git a/Rakefile b/Rakefile index 19e83a520..9f70123f7 100644 --- a/Rakefile +++ b/Rakefile @@ -59,16 +59,16 @@ task :templates do sh "#{ruby} templates/template.rb ext/rbs_extension/ast_translation.h" sh "#{ruby} templates/template.rb ext/rbs_extension/ast_translation.c" + sh "#{ruby} templates/template.rb ext/rbs_extension/class_constants.h" + sh "#{ruby} templates/template.rb ext/rbs_extension/class_constants.c" + sh "#{ruby} templates/template.rb include/rbs/ast.h" sh "#{ruby} templates/template.rb src/ast.c" - - sh "#{ruby} templates/template.rb include/rbs/constants.h" - sh "#{ruby} templates/template.rb src/constants.c" end task :compile => "ext/rbs_extension/lexer.c" -task :compile => "include/rbs/constants.h" -task :compile => "src/constants.c" +task :compile => "ext/rbs_extension/class_constants.h" +task :compile => "ext/rbs_extension/class_constants.c" task :test_doc do files = Dir.chdir(File.expand_path('..', __FILE__)) do diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index 99adc490f..6bad63482 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -10,7 +10,7 @@ #include "ruby.h" #include "ruby/encoding.h" -#include "rbs/constants.h" +#include "class_constants.h" #include "rbs_string_bridging.h" VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t *list) { diff --git a/src/constants.c b/ext/rbs_extension/class_constants.c similarity index 99% rename from src/constants.c rename to ext/rbs_extension/class_constants.c index dd1e2ee5c..1ff669da2 100644 --- a/src/constants.c +++ b/ext/rbs_extension/class_constants.c @@ -2,7 +2,7 @@ /* This file is generated by the templates/template.rb script and should not */ /* be modified manually. */ /* To change the template see */ -/* templates/src/constants.c.erb */ +/* templates/ext/rbs_extension/class_constants.c.erb */ /*----------------------------------------------------------------------------*/ #include "rbs_extension.h" diff --git a/include/rbs/constants.h b/ext/rbs_extension/class_constants.h similarity index 97% rename from include/rbs/constants.h rename to ext/rbs_extension/class_constants.h index 995f5f923..8700b1b16 100644 --- a/include/rbs/constants.h +++ b/ext/rbs_extension/class_constants.h @@ -2,7 +2,7 @@ /* This file is generated by the templates/template.rb script and should not */ /* be modified manually. */ /* To change the template see */ -/* templates/include/rbs/constants.h.erb */ +/* templates/ext/rbs_extension/class_constants.h.erb */ /*----------------------------------------------------------------------------*/ #ifndef RBS__CONSTANTS_H diff --git a/ext/rbs_extension/rbs_extension.h b/ext/rbs_extension/rbs_extension.h index aeebf3f58..f8a11f67d 100644 --- a/ext/rbs_extension/rbs_extension.h +++ b/ext/rbs_extension/rbs_extension.h @@ -4,6 +4,7 @@ #include "ruby/re.h" #include "ruby/encoding.h" +#include "class_constants.h" #include "rbs.h" #include "lexer.h" #include "parser.h" diff --git a/include/rbs.h b/include/rbs.h index 3660481a5..6a0c4f8c6 100644 --- a/include/rbs.h +++ b/include/rbs.h @@ -2,6 +2,5 @@ #define RBS_H #include "rbs/ast.h" -#include "rbs/constants.h" #endif diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index 3fb69392d..f9eb4fea4 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -3,7 +3,7 @@ #include "ruby.h" #include "ruby/encoding.h" -#include "rbs/constants.h" +#include "class_constants.h" #include "rbs_string_bridging.h" VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t *list) { diff --git a/templates/src/constants.c.erb b/templates/ext/rbs_extension/class_constants.c.erb similarity index 100% rename from templates/src/constants.c.erb rename to templates/ext/rbs_extension/class_constants.c.erb diff --git a/templates/include/rbs/constants.h.erb b/templates/ext/rbs_extension/class_constants.h.erb similarity index 100% rename from templates/include/rbs/constants.h.erb rename to templates/ext/rbs_extension/class_constants.h.erb From 7110ba459fc527eb7aabafb021a918a35bf58241 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Fri, 3 Jan 2025 15:18:18 -0500 Subject: [PATCH 008/111] Replace `rbs_abort` Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/parser.c | 52 ++++++++++++++++++++------------- ext/rbs_extension/parserstate.c | 1 + ext/rbs_extension/parserstate.h | 1 + 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index 27034e5e7..0892a393a 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -88,16 +88,6 @@ static rbs_location_t *rbs_location_current_token(parserstate *state) { static rbs_node_t *parse_optional(parserstate *state); static rbs_node_t *parse_simple(parserstate *state); -/** - * Raises RuntimeError with "Unexpected error " message. - * */ -static NORETURN(void) rbs_abort(void) { - rb_raise( - rb_eRuntimeError, - "Unexpected error" - ); -} - NORETURN(void) raise_syntax_error(parserstate *state, token tok, const char *fmt, ...) { va_list args; va_start(args, fmt); @@ -868,7 +858,8 @@ static rbs_types_literal_t *parse_symbol(parserstate *state, rbs_location_t *loc break; } default: - rbs_abort(); + state->aborted = true; + return NULL; } return rbs_types_literal_new(&state->allocator, (rbs_node_t *) literal, location); @@ -898,7 +889,8 @@ static rbs_node_t *parse_instance_type(parserstate *state, bool parse_alias) { } else if (state->current_token.type == tLIDENT) { kind = ALIAS_NAME; } else { - rbs_abort(); + state->aborted = true; + return NULL; } range args_range; @@ -1215,7 +1207,8 @@ static rbs_node_list_t *parse_type_params(parserstate *state, range *rg, bool mo variance = rbs_keyword_new(&state->allocator, INTERN("covariant")); break; default: - rbs_abort(); + state->aborted = true; + return NULL; } parser_advance(state); @@ -1454,7 +1447,8 @@ static rbs_ast_annotation_t *parse_annotation(parserstate *state) { close_char = '|'; break; default: - rbs_abort(); + state->aborted = true; + return NULL; } int open_bytes = rb_enc_codelen(open_char, enc); @@ -1750,7 +1744,8 @@ static rbs_ast_members_methoddefinition_t *parse_member_def(parserstate *state, break; } default: - rbs_abort(); + state->aborted = true; + return NULL; } rbs_location_t *loc = rbs_location_new(state->buffer, member_range); @@ -1816,7 +1811,8 @@ static rbs_node_t *parse_mixin_member(parserstate *state, bool from_interface, p reset_typevar_scope = false; break; default: - rbs_abort(); + state->aborted = true; + return NULL; } if (from_interface) { @@ -1860,7 +1856,8 @@ static rbs_node_t *parse_mixin_member(parserstate *state, bool from_interface, p case kPREPEND: return (rbs_node_t *) rbs_ast_members_prepend_new(&state->allocator, name, args, annotations, loc, comment); default: - rbs_abort(); + state->aborted = true; + return NULL; } } @@ -2007,7 +2004,8 @@ static rbs_node_t *parse_variable_member(parserstate *state, position comment_po return (rbs_node_t *)rbs_ast_members_classinstancevariable_new(&state->allocator, name, type, loc, comment); } default: - rbs_abort(); + state->aborted = true; + return NULL; } } @@ -2033,7 +2031,8 @@ static rbs_node_t *parse_visibility_member(parserstate *state, rbs_node_list_t * case kPRIVATE: return (rbs_node_t *) rbs_ast_members_private_new(&state->allocator, location); default: - rbs_abort(); + state->aborted = true; + return NULL; } } @@ -2139,7 +2138,8 @@ static rbs_node_t *parse_attribute_member(parserstate *state, position comment_p case kATTRACCESSOR: return (rbs_node_t *) rbs_ast_members_attraccessor_new(&state->allocator, attr_name, type, ivar_name, kind, annotations, loc, comment, visibility); default: - rbs_abort(); + state->aborted = true; + return NULL; } } @@ -2845,6 +2845,10 @@ parse_type_try(VALUE a) { rbs_node_t *type = parse_type(parser); + if (parser->aborted) { + rb_raise(rb_eRuntimeError, "Unexpected error"); + } + if (RB_TEST(arg->require_eof)) { parser_advance_assert(parser, pEOF); } @@ -2880,6 +2884,10 @@ parse_method_type_try(VALUE a) { rbs_methodtype_t *method_type = parse_method_type(parser); + if (parser->aborted) { + rb_raise(rb_eRuntimeError, "Unexpected error"); + } + if (RB_TEST(arg->require_eof)) { parser_advance_assert(parser, pEOF); } @@ -2910,6 +2918,10 @@ parse_signature_try(VALUE a) { rbs_signature_t *signature = parse_signature(parser); + if (parser->aborted) { + rb_raise(rb_eRuntimeError, "Unexpected error"); + } + rbs_translation_context_t ctx = { .constant_pool = &parser->constant_pool, }; diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index d0ce6ab97..b376bed80 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -326,6 +326,7 @@ parserstate *alloc_parser(VALUE buffer, VALUE string, int start_pos, int end_pos .constant_pool = {}, .allocator = allocator, + .aborted = false, }; // The parser's constant pool is mainly used for storing the names of type variables, which usually aren't many. diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index 968f346ad..f61d32c74 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -61,6 +61,7 @@ typedef struct { rbs_constant_pool_t constant_pool; rbs_allocator_t allocator; + bool aborted; } parserstate; comment *alloc_comment(rbs_allocator_t *, token comment_token, comment *last_comment); From b396faf554e5ebddf6ad3e1e1d908afdd794de7c Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 6 Dec 2024 12:50:49 -0500 Subject: [PATCH 009/111] Do not raise for syntax errors Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/parser.c | 1064 +++++++++++++++++------------ ext/rbs_extension/parser.h | 6 +- ext/rbs_extension/parserstate.c | 3 +- ext/rbs_extension/parserstate.h | 6 + ext/rbs_extension/rbs_extension.h | 4 +- 5 files changed, 660 insertions(+), 423 deletions(-) diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index 0892a393a..00ccb6562 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -57,6 +57,14 @@ case k__TODO__: \ /* nop */ +#define NODISCARD \ + __attribute__ ((warn_unused_result)) + +#define CHECK_PARSE(call) \ + if (!call) { \ + return false; \ + } + typedef struct { rbs_node_list_t *required_positionals; rbs_node_list_t *optional_positionals; @@ -85,28 +93,44 @@ static rbs_location_t *rbs_location_current_token(parserstate *state) { ); } -static rbs_node_t *parse_optional(parserstate *state); -static rbs_node_t *parse_simple(parserstate *state); +static bool parse_optional(parserstate *state, rbs_node_t **optional); +static bool parse_simple(parserstate *state, rbs_node_t **type); + +bool has_error(parserstate *state) { + return state->error != NULL || state->aborted; +} + +void syntax_error(parserstate *state, token tok, const char *fmt, ...) { + if (state->error) { + return; + } -NORETURN(void) raise_syntax_error(parserstate *state, token tok, const char *fmt, ...) { va_list args; + char *message; + va_start(args, fmt); - VALUE message = rb_vsprintf(fmt, args); + vasprintf(&message, fmt, args); va_end(args); - VALUE location = rbs_new_location(state->buffer, tok.range); - VALUE type = rb_str_new_cstr(token_type_str(tok.type)); + state->error = (error *)malloc(sizeof(error)); + state->error->token = tok; + state->error->message = message; +} + +NORETURN(void) raise_syntax_error(parserstate *state, error *error) { + VALUE location = rbs_new_location(state->buffer, error->token.range); + VALUE type = rb_str_new_cstr(token_type_str(error->token.type)); - VALUE error = rb_funcall( + VALUE rb_error = rb_funcall( RBS_ParsingError, rb_intern("new"), 3, location, - message, + rb_str_new_cstr(error->message), type ); - rb_exc_raise(error); + rb_exc_raise(rb_error); } typedef enum { @@ -119,11 +143,7 @@ void parser_advance_no_gap(parserstate *state) { if (state->current_token.range.end.byte_pos == state->next_token.range.start.byte_pos) { parser_advance(state); } else { - raise_syntax_error( - state, - state->next_token, - "unexpected token" - ); + syntax_error(state, state->next_token, "unexpected token"); } } @@ -132,7 +152,8 @@ void parser_advance_no_gap(parserstate *state) { | {(tUIDENT `::`)*} | {} */ -static rbs_typename_t *parse_type_name(parserstate *state, TypeNameKind kind, range *rg) { +NODISCARD +static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rbs_typename_t **typename) { bool absolute = false; if (rg) { @@ -183,7 +204,8 @@ static rbs_typename_t *parse_type_name(parserstate *state, TypeNameKind kind, ra rbs_constant_id_t name = INTERN_TOKEN(state, state->current_token); rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, name); - return rbs_typename_new(&state->allocator, namespace, symbol); + *typename = rbs_typename_new(&state->allocator, namespace, symbol); + return true; } error: { @@ -200,12 +222,8 @@ static rbs_typename_t *parse_type_name(parserstate *state, TypeNameKind kind, ra VALUE string = rb_funcall(ids, rb_intern("join"), 1, rb_str_new_cstr(", ")); - raise_syntax_error( - state, - state->current_token, - "expected one of %"PRIsVALUE, - string - ); + syntax_error(state, state->current_token, "expected one of %s", StringValuePtr(string)); + return false; } } @@ -213,9 +231,11 @@ static rbs_typename_t *parse_type_name(parserstate *state, TypeNameKind kind, ra type_list ::= {} type `,` ... <`,`> eol | {} type `,` ... `,` eol */ -static void parse_type_list(parserstate *state, enum TokenType eol, rbs_node_list_t *types) { +NODISCARD +static bool parse_type_list(parserstate *state, enum TokenType eol, rbs_node_list_t *types) { while (true) { - rbs_node_t *type = parse_type(state); + rbs_node_t *type; + CHECK_PARSE(parse_type(state, &type)); rbs_node_list_append(types, type); if (state->next_token.type == pCOMMA) { @@ -228,14 +248,13 @@ static void parse_type_list(parserstate *state, enum TokenType eol, rbs_node_lis if (state->next_token.type == eol) { break; } else { - raise_syntax_error( - state, - state->next_token, - "comma delimited type list is expected" - ); + syntax_error(state, state->next_token, "comma delimited type list is expected"); + return false; } } } + + return true; } static bool is_keyword_token(enum TokenType type) { @@ -258,10 +277,12 @@ static bool is_keyword_token(enum TokenType type) { function_param ::= {} | {} type */ -static rbs_types_function_param_t *parse_function_param(parserstate *state) { +NODISCARD +static bool parse_function_param(parserstate *state, rbs_types_function_param_t **function_param) { range type_range; type_range.start = state->next_token.range.start; - rbs_node_t *type = parse_type(state); + rbs_node_t *type; + CHECK_PARSE(parse_type(state, &type)); type_range.end = state->current_token.range.end; if (state->next_token.type == pCOMMA || state->next_token.type == pRPAREN) { @@ -271,7 +292,8 @@ static rbs_types_function_param_t *parse_function_param(parserstate *state) { rbs_loc_alloc_children(loc, 1); rbs_loc_add_optional_child(loc, INTERN("name"), NULL_RANGE); - return rbs_types_function_param_new(&state->allocator, type, NULL, loc); + *function_param = rbs_types_function_param_new(&state->allocator, type, NULL, loc); + return true; } else { range name_range = state->next_token.range; @@ -283,11 +305,8 @@ static rbs_types_function_param_t *parse_function_param(parserstate *state) { }; if (!is_keyword_token(state->current_token.type)) { - raise_syntax_error( - state, - state->current_token, - "unexpected token for function parameter name" - ); + syntax_error(state, state->current_token, "unexpected token for function parameter name"); + return false; } VALUE name_str = rbs_unquote_string(state, state->current_token.range, 0); @@ -302,7 +321,8 @@ static rbs_types_function_param_t *parse_function_param(parserstate *state) { rbs_loc_alloc_children(loc, 1); rbs_loc_add_optional_child(loc, INTERN("name"), name_range); - return rbs_types_function_param_new(&state->allocator, type, name, loc); + *function_param = rbs_types_function_param_new(&state->allocator, type, name, loc); + return true; } } @@ -319,43 +339,45 @@ static rbs_constant_id_t intern_token_start_end(parserstate *state, token start_ keyword_key ::= {} `:` | {} keyword <`?`> `:` */ -static rbs_ast_symbol_t *parse_keyword_key(parserstate *state) { +NODISCARD +static bool parse_keyword_key(parserstate *state, rbs_ast_symbol_t **key) { parser_advance(state); if (state->next_token.type == pQUESTION) { - rbs_ast_symbol_t *key = rbs_ast_symbol_new( + *key = rbs_ast_symbol_new( &state->allocator, &state->constant_pool, intern_token_start_end(state, state->current_token, state->next_token) ); parser_advance(state); - return key; } else { - return rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + *key = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); } + + return true; } /* keyword ::= {} keyword `:` */ -static void parse_keyword(parserstate *state, rbs_hash_t *keywords, rbs_hash_t *memo) { - rbs_ast_symbol_t *key = parse_keyword_key(state); +NODISCARD +static bool parse_keyword(parserstate *state, rbs_hash_t *keywords, rbs_hash_t *memo) { + rbs_ast_symbol_t *key = NULL; + CHECK_PARSE(parse_keyword_key(state, &key)); if (rbs_hash_find(memo, (rbs_node_t *) key)) { - raise_syntax_error( - state, - state->current_token, - "duplicated keyword argument" - ); + syntax_error(state, state->current_token, "duplicated keyword argument"); + return false; } else { rbs_hash_set(memo, (rbs_node_t *) key, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, true)); } parser_advance_assert(state, pCOLON); - rbs_types_function_param_t *param = parse_function_param(state); + rbs_types_function_param_t *param = NULL; + CHECK_PARSE(parse_function_param(state, ¶m)); rbs_hash_set(keywords, (rbs_node_t *) key, (rbs_node_t *) param); - return; + return true; } /* @@ -409,14 +431,15 @@ static bool is_keyword(parserstate *state) { | {} `?` | {} `**` */ -static void parse_params(parserstate *state, method_params *params) { +NODISCARD +static bool parse_params(parserstate *state, method_params *params) { if (state->next_token.type == pQUESTION && state->next_token2.type == pRPAREN) { params->required_positionals = NULL; parser_advance(state); - return; + return true; } if (state->next_token.type == pRPAREN) { - return; + return true; } rbs_hash_t *memo = rbs_hash_new(&state->allocator); @@ -437,7 +460,8 @@ static void parse_params(parserstate *state, method_params *params) { goto PARSE_KEYWORDS; } - rbs_types_function_param_t *param = parse_function_param(state); + rbs_types_function_param_t *param = NULL; + CHECK_PARSE(parse_function_param(state, ¶m)); rbs_node_list_append(params->required_positionals, (rbs_node_t *)param); break; @@ -455,12 +479,13 @@ static void parse_params(parserstate *state, method_params *params) { parser_advance(state); if (is_keyword(state)) { - parse_keyword(state, params->optional_keywords, memo); + CHECK_PARSE(parse_keyword(state, params->optional_keywords, memo)); parser_advance_if(state, pCOMMA); goto PARSE_KEYWORDS; } - rbs_types_function_param_t *param = parse_function_param(state); + rbs_types_function_param_t *param = NULL; + CHECK_PARSE(parse_function_param(state, ¶m)); rbs_node_list_append(params->optional_positionals, (rbs_node_t *)param); break; @@ -476,7 +501,8 @@ static void parse_params(parserstate *state, method_params *params) { PARSE_REST_PARAM: if (state->next_token.type == pSTAR) { parser_advance(state); - rbs_types_function_param_t *param = parse_function_param(state); + rbs_types_function_param_t *param = NULL; + CHECK_PARSE(parse_function_param(state, ¶m)); params->rest_positionals = (rbs_node_t *) param; if (!parser_advance_if(state, pCOMMA)) { @@ -502,7 +528,8 @@ static void parse_params(parserstate *state, method_params *params) { goto PARSE_KEYWORDS; } - rbs_types_function_param_t *param = parse_function_param(state); + rbs_types_function_param_t *param = NULL; + CHECK_PARSE(parse_function_param(state, ¶m)); rbs_node_list_append(params->trailing_positionals, (rbs_node_t *)param); break; @@ -519,19 +546,17 @@ static void parse_params(parserstate *state, method_params *params) { case pQUESTION: parser_advance(state); if (is_keyword(state)) { - parse_keyword(state, params->optional_keywords, memo); + CHECK_PARSE(parse_keyword(state, params->optional_keywords, memo)); } else { - raise_syntax_error( - state, - state->next_token, - "optional keyword argument type is expected" - ); + syntax_error(state, state->next_token, "optional keyword argument type is expected"); + return false; } break; case pSTAR2: parser_advance(state); - rbs_types_function_param_t *param = parse_function_param(state); + rbs_types_function_param_t *param = NULL; + CHECK_PARSE(parse_function_param(state, ¶m)); params->rest_keywords = (rbs_node_t *) param; break; @@ -543,13 +568,10 @@ static void parse_params(parserstate *state, method_params *params) { case tBANGIDENT: KEYWORD_CASES if (is_keyword(state)) { - parse_keyword(state, params->required_keywords, memo); + CHECK_PARSE(parse_keyword(state, params->required_keywords, memo)); } else { - raise_syntax_error( - state, - state->next_token, - "required keyword argument type is expected" - ); + syntax_error(state, state->next_token, "required keyword argument type is expected"); + return false; } break; @@ -564,33 +586,35 @@ static void parse_params(parserstate *state, method_params *params) { EOP: if (state->next_token.type != pRPAREN) { - raise_syntax_error( - state, - state->next_token, - "unexpected token for method type parameters" - ); + syntax_error(state, state->next_token, "unexpected token for method type parameters"); + return false; } - return; + return true; } /* optional ::= {} | {} simple_type <`?`> */ -static rbs_node_t *parse_optional(parserstate *state) { +NODISCARD +static bool parse_optional(parserstate *state, rbs_node_t **optional) { range rg; rg.start = state->next_token.range.start; - rbs_node_t *type = parse_simple(state); + + rbs_node_t *type = NULL; + CHECK_PARSE(parse_simple(state, &type)); if (state->next_token.type == pQUESTION) { parser_advance(state); rg.end = state->current_token.range.end; rbs_location_t *location = rbs_location_new(state->buffer, rg); - return (rbs_node_t *) rbs_types_optional_new(&state->allocator, type, location); + *optional = (rbs_node_t *) rbs_types_optional_new(&state->allocator, type, location); } else { - return type; + *optional = type; } + + return true; } static void initialize_method_params(method_params *params, rbs_allocator_t *allocator) { @@ -609,17 +633,19 @@ static void initialize_method_params(method_params *params, rbs_allocator_t *all self_type_binding ::= {} <> | {} `[` `self` `:` type <`]`> */ -static rbs_node_t *parse_self_type_binding(parserstate *state) { +NODISCARD +static bool parse_self_type_binding(parserstate *state, rbs_node_t **self_type) { if (state->next_token.type == pLBRACKET) { parser_advance(state); parser_advance_assert(state, kSELF); parser_advance_assert(state, pCOLON); - rbs_node_t *type = parse_type(state); + rbs_node_t *type; + CHECK_PARSE(parse_type(state, &type)); parser_advance_assert(state, pRBRACKET); - return type; - } else { - return NULL; + *self_type = type; } + + return true; } typedef struct { @@ -635,7 +661,8 @@ typedef struct { | {} self_type_binding? `{` self_type_binding `->` optional `}` `->` | {} self_type_binding? `->` */ -static parse_function_result parse_function(parserstate *state, bool accept_type_binding) { +NODISCARD +static bool parse_function(parserstate *state, bool accept_type_binding, parse_function_result **result) { rbs_node_t *function = NULL; rbs_types_block_t *block = NULL; rbs_node_t *function_self_type = NULL; @@ -644,20 +671,21 @@ static parse_function_result parse_function(parserstate *state, bool accept_type if (state->next_token.type == pLPAREN) { parser_advance(state); - parse_params(state, ¶ms); + CHECK_PARSE(parse_params(state, ¶ms)); parser_advance_assert(state, pRPAREN); } // Untyped method parameter means it cannot have block if (rbs_is_untyped_params(¶ms)) { if (state->next_token.type != pARROW) { - raise_syntax_error(state, state->next_token2, "A method type with untyped method parameter cannot have block"); + syntax_error(state, state->next_token2, "A method type with untyped method parameter cannot have block"); + return false; } } // Passing NULL to function_self_type means the function itself doesn't accept self type binding. (== method type) if (accept_type_binding) { - function_self_type = parse_self_type_binding(state); + CHECK_PARSE(parse_self_type_binding(state, &function_self_type)); } bool required = true; @@ -674,14 +702,16 @@ static parse_function_result parse_function(parserstate *state, bool accept_type if (state->next_token.type == pLPAREN) { parser_advance(state); - parse_params(state, &block_params); + CHECK_PARSE(parse_params(state, &block_params)); parser_advance_assert(state, pRPAREN); } - rbs_node_t *self_type = parse_self_type_binding(state); + rbs_node_t *self_type = NULL; + CHECK_PARSE(parse_self_type_binding(state, &self_type)); parser_advance_assert(state, pARROW); - rbs_node_t *block_return_type = parse_optional(state); + rbs_node_t *block_return_type = NULL; + CHECK_PARSE(parse_optional(state, &block_return_type)); rbs_node_t *block_function = NULL; if (rbs_is_untyped_params(&block_params)) { @@ -706,7 +736,8 @@ static parse_function_result parse_function(parserstate *state, bool accept_type } parser_advance_assert(state, pARROW); - rbs_node_t *type = parse_optional(state); + rbs_node_t *type = NULL; + CHECK_PARSE(parse_optional(state, &type)); if (rbs_is_untyped_params(¶ms)) { function = (rbs_node_t *) rbs_types_untypedfunction_new(&state->allocator, type); @@ -724,31 +755,37 @@ static parse_function_result parse_function(parserstate *state, bool accept_type ); } - parse_function_result result; - result.function = function; - result.block = block; - result.function_self_type = function_self_type; - return result; + *result = malloc(sizeof(parse_function_result)); + // *result = (parse_function_result) { + // .function = function, + // .block = block, + // .function_self_type = function_self_type, + // }; + + (*result)->function = function; + (*result)->block = block; + (*result)->function_self_type = function_self_type; + return true; } /* proc_type ::= {`^`} */ -static rbs_types_proc_t *parse_proc_type(parserstate *state) { +NODISCARD +static bool parse_proc_type(parserstate *state, rbs_types_proc_t **proc) { position start = state->current_token.range.start; - parse_function_result result = parse_function(state, true); + parse_function_result *result = NULL; + CHECK_PARSE(parse_function(state, true, &result)); + position end = state->current_token.range.end; rbs_location_t *loc = rbs_location_pp(state->buffer, &start, &end); - return rbs_types_proc_new(&state->allocator, result.function, result.block, loc, result.function_self_type); + *proc = rbs_types_proc_new(&state->allocator, result->function, result->block, loc, result->function_self_type); + return true; } static void check_key_duplication(parserstate *state, rbs_hash_t *fields, rbs_node_t *key) { if (rbs_hash_find(fields, ((rbs_node_t *) key))) { - raise_syntax_error( - state, - state->current_token, - "duplicated record key" - ); + syntax_error(state, state->current_token, "duplicated record key"); } } @@ -762,15 +799,14 @@ static void check_key_duplication(parserstate *state, rbs_hash_t *fields, rbs_no record_attribute ::= {} keyword_token `:` | {} literal_type `=>` */ -static rbs_hash_t *parse_record_attributes(parserstate *state) { - rbs_hash_t *fields = rbs_hash_new(&state->allocator); +NODISCARD +static bool parse_record_attributes(parserstate *state, rbs_hash_t **fields) { + *fields = rbs_hash_new(&state->allocator); - if (state->next_token.type == pRBRACE) { - return fields; - } + if (state->next_token.type == pRBRACE) return true; while (true) { - rbs_node_t *key; + rbs_ast_symbol_t *key = NULL; bool required = true; if (state->next_token.type == pQUESTION) { @@ -781,8 +817,9 @@ static rbs_hash_t *parse_record_attributes(parserstate *state) { if (is_keyword(state)) { // { foo: type } syntax - key = (rbs_node_t *) parse_keyword_key(state); - check_key_duplication(state, fields, key); + CHECK_PARSE(parse_keyword_key(state, &key)); + + check_key_duplication(state, *fields, (rbs_node_t *) key); parser_advance_assert(state, pCOLON); } else { // { key => type } syntax @@ -795,21 +832,24 @@ static rbs_hash_t *parse_record_attributes(parserstate *state) { case tINTEGER: case kTRUE: case kFALSE: { - key = (rbs_node_t *) ((rbs_types_literal_t *) parse_simple(state))->literal; + rbs_node_t *type = NULL; + CHECK_PARSE(parse_simple(state, &type)); + + key = (rbs_ast_symbol_t *) ((rbs_types_literal_t *) type)->literal; break; } default: - raise_syntax_error( - state, - state->next_token, - "unexpected record key token" - ); + syntax_error(state, state->next_token, "unexpected record key token"); + return false; } - check_key_duplication(state, fields, key); + check_key_duplication(state, *fields, (rbs_node_t *) key); parser_advance_assert(state, pFATARROW); } - rbs_node_t *type = parse_type(state); - rbs_hash_set(fields, (rbs_node_t *) key, (rbs_node_t *) rbs_types_record_fieldtype_new(&state->allocator, type, required)); + + rbs_node_t *type; + CHECK_PARSE(parse_type(state, &type)); + + rbs_hash_set(*fields, (rbs_node_t *) key, (rbs_node_t *) rbs_types_record_fieldtype_new(&state->allocator, type, required)); if (parser_advance_if(state, pCOMMA)) { if (state->next_token.type == pRBRACE) { @@ -819,13 +859,14 @@ static rbs_hash_t *parse_record_attributes(parserstate *state) { break; } } - return fields; + return true; } /* symbol ::= {} */ -static rbs_types_literal_t *parse_symbol(parserstate *state, rbs_location_t *location) { +NODISCARD +static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types_literal_t **symbol) { VALUE string = state->lexstate->string; rb_encoding *enc = rb_enc_get(string); @@ -859,10 +900,11 @@ static rbs_types_literal_t *parse_symbol(parserstate *state, rbs_location_t *loc } default: state->aborted = true; - return NULL; + return false; } - return rbs_types_literal_new(&state->allocator, (rbs_node_t *) literal, location); + *symbol = rbs_types_literal_new(&state->allocator, (rbs_node_t *) literal, location); + return true; } /* @@ -871,14 +913,17 @@ static rbs_types_literal_t *parse_symbol(parserstate *state, rbs_location_t *loc type_args ::= {} <> /empty/ | {} `[` type_list <`]`> */ -static rbs_node_t *parse_instance_type(parserstate *state, bool parse_alias) { +NODISCARD +static bool parse_instance_type(parserstate *state, bool parse_alias, rbs_node_t **type) { TypeNameKind expected_kind = INTERFACE_NAME | CLASS_NAME; if (parse_alias) { expected_kind |= ALIAS_NAME; } range name_range; - rbs_typename_t *typename = parse_type_name(state, expected_kind, &name_range); + rbs_typename_t *typename = NULL; + CHECK_PARSE(parse_type_name(state, expected_kind, &name_range, &typename)); + rbs_node_list_t *types = rbs_node_list_new(&state->allocator); TypeNameKind kind; @@ -890,14 +935,14 @@ static rbs_node_t *parse_instance_type(parserstate *state, bool parse_alias) { kind = ALIAS_NAME; } else { state->aborted = true; - return NULL; + return false; } range args_range; if (state->next_token.type == pLBRACKET) { parser_advance(state); args_range.start = state->current_token.range.start; - parse_type_list(state, pRBRACKET, types); + CHECK_PARSE(parse_type_list(state, pRBRACKET, types)); parser_advance_assert(state, pRBRACKET); args_range.end = state->current_token.range.end; } else { @@ -915,20 +960,21 @@ static rbs_node_t *parse_instance_type(parserstate *state, bool parse_alias) { rbs_loc_add_optional_child(loc, INTERN("args"), args_range); if (kind == CLASS_NAME) { - return (rbs_node_t *) rbs_types_classinstance_new(&state->allocator, typename, types, loc); + *type = (rbs_node_t *) rbs_types_classinstance_new(&state->allocator, typename, types, loc); } else if (kind == INTERFACE_NAME) { - return (rbs_node_t *) rbs_types_interface_new(&state->allocator, typename, types, loc); + *type = (rbs_node_t *) rbs_types_interface_new(&state->allocator, typename, types, loc); } else if (kind == ALIAS_NAME) { - return (rbs_node_t *) rbs_types_alias_new(&state->allocator, typename, types, loc); - } else { - return NULL; + *type = (rbs_node_t *) rbs_types_alias_new(&state->allocator, typename, types, loc); } + + return true; } /* singleton_type ::= {`singleton`} `(` type_name <`)`> */ -static rbs_types_classsingleton_t *parse_singleton_type(parserstate *state) { +NODISCARD +static bool parse_singleton_type(parserstate *state, rbs_types_classsingleton_t **singleton) { parser_assert(state, kSINGLETON); range type_range; @@ -937,7 +983,8 @@ static rbs_types_classsingleton_t *parse_singleton_type(parserstate *state) { parser_advance(state); range name_range; - rbs_typename_t *typename = parse_type_name(state, CLASS_NAME, &name_range); + rbs_typename_t *typename = NULL; + CHECK_PARSE(parse_type_name(state, CLASS_NAME, &name_range, &typename)); parser_advance_assert(state, pRPAREN); type_range.end = state->current_token.range.end; @@ -946,7 +993,8 @@ static rbs_types_classsingleton_t *parse_singleton_type(parserstate *state) { rbs_loc_alloc_children(loc, 1); rbs_loc_add_required_child(loc, INTERN("name"), name_range); - return rbs_types_classsingleton_new(&state->allocator, typename, loc); + *singleton = rbs_types_classsingleton_new(&state->allocator, typename, loc); + return true; } /* @@ -959,54 +1007,67 @@ static rbs_types_classsingleton_t *parse_singleton_type(parserstate *state) { | {} `{` record_attributes <`}`> | {} `^` */ -static rbs_node_t *parse_simple(parserstate *state) { +NODISCARD +static bool parse_simple(parserstate *state, rbs_node_t **type) { parser_advance(state); switch (state->current_token.type) { case pLPAREN: { - rbs_node_t *type = parse_type(state); + rbs_node_t *lparen_type; + CHECK_PARSE(parse_type(state, &lparen_type)); parser_advance_assert(state, pRPAREN); - return type; + *type = lparen_type; + return true; } case kBOOL: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_bases_bool_new(&state->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_bool_new(&state->allocator, loc); + return true; } case kBOT: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_bases_bottom_new(&state->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_bottom_new(&state->allocator, loc); + return true; } case kCLASS: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_bases_class_new(&state->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_class_new(&state->allocator, loc); + return true; } case kINSTANCE: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_bases_instance_new(&state->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_instance_new(&state->allocator, loc); + return true; } case kNIL: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_bases_nil_new(&state->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_nil_new(&state->allocator, loc); + return true; } case kSELF: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_bases_self_new(&state->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_self_new(&state->allocator, loc); + return true; } case kTOP: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_bases_top_new(&state->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_top_new(&state->allocator, loc); + return true; } case kVOID: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_bases_void_new(&state->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_void_new(&state->allocator, loc); + return true; } case kUNTYPED: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, false, loc); + *type = (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, false, loc); + return true; } case k__TODO__: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, true, loc); + *type = (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, true, loc); + return true; } case tINTEGER: { rbs_location_t *loc = rbs_location_current_token(state); @@ -1018,15 +1079,18 @@ static rbs_node_t *parse_simple(parserstate *state) { rbs_string_ensure_owned(&string); rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(&state->allocator, string); - return (rbs_node_t *) rbs_types_literal_new(&state->allocator, literal, loc); + *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, literal, loc); + return true; } case kTRUE: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_literal_new(&state->allocator, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, true), loc); + *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, true), loc); + return true; } case kFALSE: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) rbs_types_literal_new(&state->allocator, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, false), loc); + *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, false), loc); + return true; } case tSQSTRING: case tDQSTRING: { @@ -1041,13 +1105,17 @@ static rbs_node_t *parse_simple(parserstate *state) { rbs_string_ensure_owned(&unquoted_str); rbs_node_t *literal = (rbs_node_t *) rbs_ast_string_new(&state->allocator, unquoted_str); - return (rbs_node_t *) rbs_types_literal_new(&state->allocator, literal, loc); + *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, literal, loc); + return true; } case tSYMBOL: case tSQSYMBOL: case tDQSYMBOL: { rbs_location_t *loc = rbs_location_current_token(state); - return (rbs_node_t *) parse_symbol(state, loc); + rbs_types_literal_t *literal = NULL; + CHECK_PARSE(parse_symbol(state, loc, &literal)); + *type = (rbs_node_t *) literal; + return true; } case tUIDENT: { const char *name_str = peek_token(state->lexstate, state->current_token); @@ -1058,54 +1126,64 @@ static rbs_node_t *parse_simple(parserstate *state) { if (parser_typevar_member(state, name)) { rbs_location_t *loc = rbs_location_current_token(state); rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, name); - return (rbs_node_t *) rbs_types_variable_new(&state->allocator, symbol, loc); + *type = (rbs_node_t *) rbs_types_variable_new(&state->allocator, symbol, loc); + return true; } // fallthrough for type name } case tULIDENT: // fallthrough case tLIDENT: // fallthrough case pCOLON2: { - return parse_instance_type(state, true); + rbs_node_t *instance_type = NULL; + CHECK_PARSE(parse_instance_type(state, true, &instance_type)); + *type = instance_type; + return true; } case kSINGLETON: { - return (rbs_node_t *) parse_singleton_type(state); + rbs_types_classsingleton_t *singleton = NULL; + CHECK_PARSE(parse_singleton_type(state, &singleton)); + *type = (rbs_node_t *) singleton; + return true; } case pLBRACKET: { range rg; rg.start = state->current_token.range.start; rbs_node_list_t *types = rbs_node_list_new(&state->allocator); if (state->next_token.type != pRBRACKET) { - parse_type_list(state, pRBRACKET, types); + CHECK_PARSE(parse_type_list(state, pRBRACKET, types)); } parser_advance_assert(state, pRBRACKET); rg.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(state->buffer, rg); - return (rbs_node_t *) rbs_types_tuple_new(&state->allocator, types, loc); + *type = (rbs_node_t *) rbs_types_tuple_new(&state->allocator, types, loc); + return true; } case pAREF_OPR: { rbs_location_t *loc = rbs_location_current_token(state); rbs_node_list_t *types = rbs_node_list_new(&state->allocator); - return (rbs_node_t *) rbs_types_tuple_new(&state->allocator, types, loc); + *type = (rbs_node_t *) rbs_types_tuple_new(&state->allocator, types, loc); + return true; } case pLBRACE: { position start = state->current_token.range.start; - rbs_hash_t *fields = parse_record_attributes(state); + rbs_hash_t *fields = NULL; + CHECK_PARSE(parse_record_attributes(state, &fields)); parser_advance_assert(state, pRBRACE); position end = state->current_token.range.end; rbs_location_t *loc = rbs_location_pp(state->buffer, &start, &end); - return (rbs_node_t *) rbs_types_record_new(&state->allocator, fields, loc); + *type = (rbs_node_t *) rbs_types_record_new(&state->allocator, fields, loc); + return true; } case pHAT: { - rbs_types_proc_t *value = parse_proc_type(state); - return (rbs_node_t *) value; + rbs_types_proc_t *value = NULL; + CHECK_PARSE(parse_proc_type(state, &value)); + *type = (rbs_node_t *) value; + return true; } default: - raise_syntax_error( - state, - state->current_token, - "unexpected token for simple type" - ); + syntax_error(state, state->current_token, "unexpected token for simple type"); + return false; } } @@ -1113,53 +1191,63 @@ static rbs_node_t *parse_simple(parserstate *state) { intersection ::= {} optional `&` ... '&' | {} */ -static rbs_node_t *parse_intersection(parserstate *state) { +NODISCARD +static bool parse_intersection(parserstate *state, rbs_node_t **type) { range rg; rg.start = state->next_token.range.start; - rbs_node_t *type = parse_optional(state); + + rbs_node_t *optional = NULL; + CHECK_PARSE(parse_optional(state, &optional)); + *type = optional; + rbs_node_list_t *intersection_types = rbs_node_list_new(&state->allocator); - rbs_node_list_append(intersection_types, type); + rbs_node_list_append(intersection_types, optional); while (state->next_token.type == pAMP) { parser_advance(state); - rbs_node_list_append(intersection_types, parse_optional(state)); + rbs_node_t *type = NULL; + CHECK_PARSE(parse_optional(state, &type)); + rbs_node_list_append(intersection_types, type); } rg.end = state->current_token.range.end; if (intersection_types->length > 1) { rbs_location_t *location = rbs_location_new(state->buffer, rg); - type = (rbs_node_t *) rbs_types_intersection_new(&state->allocator, intersection_types, location); + *type = (rbs_node_t *) rbs_types_intersection_new(&state->allocator, intersection_types, location); } - return type; + return true; } /* union ::= {} intersection '|' ... '|' | {} */ -rbs_node_t *parse_type(parserstate *state) { +bool parse_type(parserstate *state, rbs_node_t **type) { range rg; rg.start = state->next_token.range.start; - - rbs_node_t *type = parse_intersection(state); rbs_node_list_t *union_types = rbs_node_list_new(&state->allocator); - rbs_node_list_append(union_types, type); + CHECK_PARSE(parse_intersection(state, type)); + + rbs_node_list_append(union_types, *type); + while (state->next_token.type == pBAR) { parser_advance(state); - rbs_node_list_append(union_types, parse_intersection(state)); + rbs_node_t *intersection = NULL; + CHECK_PARSE(parse_intersection(state, &intersection)); + rbs_node_list_append(union_types, intersection); } rg.end = state->current_token.range.end; if (union_types->length > 1) { rbs_location_t *location = rbs_location_new(state->buffer, rg); - type = (rbs_node_t *) rbs_types_union_new(&state->allocator, union_types, location); + *type = (rbs_node_t *) rbs_types_union_new(&state->allocator, union_types, location); } - return type; + return true; } /* @@ -1233,7 +1321,7 @@ static rbs_node_list_t *parse_type_params(parserstate *state, range *rg, bool mo if (state->next_token.type == pLT) { parser_advance(state); upper_bound_range.start = state->current_token.range.start; - upper_bound = parse_type(state); + CHECK_PARSE(parse_type(state, &upper_bound)); upper_bound_range.end = state->current_token.range.end; } @@ -1243,17 +1331,14 @@ static rbs_node_list_t *parse_type_params(parserstate *state, range *rg, bool mo parser_advance(state); default_type_range.start = state->current_token.range.start; - default_type = parse_type(state); + CHECK_PARSE(parse_type(state, &default_type)); default_type_range.end = state->current_token.range.end; required_param_allowed = false; } else { if (!required_param_allowed) { - raise_syntax_error( - state, - state->current_token, - "required type parameter is not allowed after optional type parameter" - ); + syntax_error(state, state->current_token, "required type parameter is not allowed after optional type parameter"); + return NULL; } } } @@ -1293,19 +1378,22 @@ static rbs_node_list_t *parse_type_params(parserstate *state, range *rg, bool mo /* method_type ::= {} type_params */ -rbs_methodtype_t *parse_method_type(parserstate *state) { +// TODO: Should this be NODISCARD? +bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type) { parser_push_typevar_table(state, false); range rg; rg.start = state->next_token.range.start; range params_range = NULL_RANGE; - rbs_node_list_t *type_params = parse_type_params(state, ¶ms_range, false); + rbs_node_list_t *type_params = parse_type_params(state, ¶ms_range, false); // TODO + if (has_error(state)) return false; range type_range; type_range.start = state->next_token.range.start; - parse_function_result result = parse_function(state, false); + parse_function_result *result; + CHECK_PARSE(parse_function(state, false, &result)); rg.end = state->current_token.range.end; type_range.end = rg.end; @@ -1317,13 +1405,15 @@ rbs_methodtype_t *parse_method_type(parserstate *state) { rbs_loc_add_required_child(loc, INTERN("type"), type_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range); - return rbs_methodtype_new(&state->allocator, type_params, result.function, result.block, loc); + *method_type = rbs_methodtype_new(&state->allocator, type_params, result->function, result->block, loc); + return true; } /* global_decl ::= {tGIDENT} `:` */ -static rbs_ast_declarations_global_t *parse_global_decl(parserstate *state) { +NODISCARD +static bool parse_global_decl(parserstate *state, rbs_ast_declarations_global_t **global) { range decl_range; decl_range.start = state->current_token.range.start; @@ -1335,7 +1425,8 @@ static rbs_ast_declarations_global_t *parse_global_decl(parserstate *state) { parser_advance_assert(state, pCOLON); range colon_range = state->current_token.range; - rbs_node_t *type = parse_type(state); + rbs_node_t *type; + CHECK_PARSE(parse_type(state, &type)); decl_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); @@ -1343,25 +1434,30 @@ static rbs_ast_declarations_global_t *parse_global_decl(parserstate *state) { rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - return rbs_ast_declarations_global_new(&state->allocator, typename, type, loc, comment); + *global = rbs_ast_declarations_global_new(&state->allocator, typename, type, loc, comment); + return true; } /* const_decl ::= {const_name} `:` */ -static rbs_ast_declarations_constant_t *parse_const_decl(parserstate *state) { +NODISCARD +static bool parse_const_decl(parserstate *state, rbs_ast_declarations_constant_t **constant) { range decl_range; decl_range.start = state->current_token.range.start; rbs_ast_comment_t *comment = get_comment(state, decl_range.start.line); range name_range; - rbs_typename_t *typename = parse_type_name(state, CLASS_NAME, &name_range); + rbs_typename_t *typename = NULL; + CHECK_PARSE(parse_type_name(state, CLASS_NAME, &name_range, &typename)); parser_advance_assert(state, pCOLON); range colon_range = state->current_token.range; - rbs_node_t *type = parse_type(state); + rbs_node_t *type; + CHECK_PARSE(parse_type(state, &type)); + decl_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); @@ -1369,13 +1465,15 @@ static rbs_ast_declarations_constant_t *parse_const_decl(parserstate *state) { rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - return rbs_ast_declarations_constant_new(&state->allocator, typename, type, loc, comment); + *constant = rbs_ast_declarations_constant_new(&state->allocator, typename, type, loc, comment); + return true; } /* type_decl ::= {kTYPE} alias_name `=` */ -static rbs_ast_declarations_typealias_t *parse_type_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations) { +NODISCARD +static bool parse_type_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_typealias_t **typealias) { parser_push_typevar_table(state, true); range decl_range; @@ -1387,15 +1485,19 @@ static rbs_ast_declarations_typealias_t *parse_type_decl(parserstate *state, pos parser_advance(state); range name_range; - rbs_typename_t *typename = parse_type_name(state, ALIAS_NAME, &name_range); + rbs_typename_t *typename = NULL; + CHECK_PARSE(parse_type_name(state, ALIAS_NAME, &name_range, &typename)); range params_range; rbs_node_list_t *type_params = parse_type_params(state, ¶ms_range, true); + if (has_error(state)) return false; parser_advance_assert(state, pEQ); range eq_range = state->current_token.range; - rbs_node_t *type = parse_type(state); + rbs_node_t *type; + CHECK_PARSE(parse_type(state, &type)); + decl_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); @@ -1408,13 +1510,16 @@ static rbs_ast_declarations_typealias_t *parse_type_decl(parserstate *state, pos parser_pop_typevar_table(state); rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); - return rbs_ast_declarations_typealias_new(&state->allocator, typename, type_params, type, annotations, loc, comment); + + *typealias = rbs_ast_declarations_typealias_new(&state->allocator, typename, type_params, type, annotations, loc, comment); + return true; } /* annotation ::= {} */ -static rbs_ast_annotation_t *parse_annotation(parserstate *state) { +NODISCARD +static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotation) { VALUE content = rb_funcall(state->buffer, rb_intern("content"), 0); rb_encoding *enc = rb_enc_get(content); @@ -1448,7 +1553,7 @@ static rbs_ast_annotation_t *parse_annotation(parserstate *state) { break; default: state->aborted = true; - return NULL; + return false; } int open_bytes = rb_enc_codelen(open_char, enc); @@ -1461,14 +1566,17 @@ static rbs_ast_annotation_t *parse_annotation(parserstate *state) { rbs_string_ensure_owned(&string); rbs_location_t *loc = rbs_location_new(state->buffer, rg); - return rbs_ast_annotation_new(&state->allocator, string, loc); + + *annotation = rbs_ast_annotation_new(&state->allocator, string, loc); + return true; } /* annotations ::= {} annotation ... | {<>} */ -static void parse_annotations(parserstate *state, rbs_node_list_t *annotations, position *annot_pos) { +NODISCARD +static bool parse_annotations(parserstate *state, rbs_node_list_t *annotations, position *annot_pos) { *annot_pos = NullPosition; while (true) { @@ -1479,18 +1587,23 @@ static void parse_annotations(parserstate *state, rbs_node_list_t *annotations, *annot_pos = state->current_token.range.start; } - rbs_node_list_append(annotations, (rbs_node_t *)parse_annotation(state)); + rbs_ast_annotation_t *annotation = NULL; + CHECK_PARSE(parse_annotation(state, &annotation)); + rbs_node_list_append(annotations, (rbs_node_t *) annotation); } else { break; } } + + return true; } /* method_name ::= {} | {} (IDENT | keyword)~<`?`> */ -static rbs_ast_symbol_t *parse_method_name(parserstate *state, range *range) { +NODISCARD +static bool parse_method_name(parserstate *state, range *range, rbs_ast_symbol_t **symbol) { parser_advance(state); switch (state->current_token.type) @@ -1512,16 +1625,18 @@ static rbs_ast_symbol_t *parse_method_name(parserstate *state, range *range) { // rb_enc_get(state->lexstate->string) // FIXME: Add encoding back ); - return rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); + *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); } else { *range = state->current_token.range; - return rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); } + return true; case tBANGIDENT: case tEQIDENT: { *range = state->current_token.range; - return rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + return true; } case tQIDENT: { VALUE ruby_str = rbs_unquote_string(state, state->current_token.range, 0); @@ -1530,7 +1645,8 @@ static rbs_ast_symbol_t *parse_method_name(parserstate *state, range *range) { (const uint8_t *) RSTRING_PTR(ruby_str), RSTRING_LEN(ruby_str) ); - return rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); + *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); + return true; } case pBAR: @@ -1542,15 +1658,13 @@ static rbs_ast_symbol_t *parse_method_name(parserstate *state, range *range) { case pAREF_OPR: case tOPERATOR: { *range = state->current_token.range; - return rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + return true; } default: - raise_syntax_error( - state, - state->current_token, - "unexpected token for method name" - ); + syntax_error(state, state->current_token, "unexpected token for method name"); + return false; } } @@ -1611,7 +1725,8 @@ static InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, b * @param instance_only `true` to reject singleton method definition. * @param accept_overload `true` to accept overloading (...) definition. * */ -static rbs_ast_members_methoddefinition_t *parse_member_def(parserstate *state, bool instance_only, bool accept_overload, position comment_pos, rbs_node_list_t *annotations) { +NODISCARD +static bool parse_member_def(parserstate *state, bool instance_only, bool accept_overload, position comment_pos, rbs_node_list_t *annotations, rbs_ast_members_methoddefinition_t **method_definition) { range member_range; member_range.start = state->current_token.range.start; comment_pos = nonnull_pos_or(comment_pos, member_range.start); @@ -1651,19 +1766,18 @@ static rbs_ast_members_methoddefinition_t *parse_member_def(parserstate *state, kind = INSTANCE_KIND; } else { kind = parse_instance_singleton_kind(state, visibility == NULL, &kind_range); + if (has_error(state)) return false; } range name_range; - rbs_ast_symbol_t *name = parse_method_name(state, &name_range); + rbs_ast_symbol_t *name = NULL; + CHECK_PARSE(parse_method_name(state, &name_range, &name)); #define SELF_ID rbs_constant_pool_insert_constant(&state->constant_pool, (const unsigned char *) "self?", strlen("self?")) if (state->next_token.type == pDOT && name->constant_id == SELF_ID) { - raise_syntax_error( - state, - state->next_token, - "`self?` method cannot have visibility" - ); + syntax_error(state, state->next_token, "`self?` method cannot have visibility"); + return false; } else { parser_advance_assert(state, pCOLON); } @@ -1679,7 +1793,7 @@ static rbs_ast_members_methoddefinition_t *parse_member_def(parserstate *state, position overload_annot_pos = NullPosition; if (state->next_token.type == tANNOTATION) { - parse_annotations(state, annotations, &overload_annot_pos); + CHECK_PARSE(parse_annotations(state, annotations, &overload_annot_pos)); } switch (state->next_token.type) { @@ -1689,8 +1803,10 @@ static rbs_ast_members_methoddefinition_t *parse_member_def(parserstate *state, case pLBRACKET: case pQUESTION: { - rbs_node_t *method_type = (rbs_node_t *) parse_method_type(state); - rbs_node_t *overload = (rbs_node_t *) rbs_ast_members_methoddefinition_overload_new(&state->allocator, annotations, method_type); + rbs_methodtype_t *method_type = NULL; + CHECK_PARSE(parse_method_type(state, &method_type)); + + rbs_node_t *overload = (rbs_node_t *) rbs_ast_members_methoddefinition_overload_new(&state->allocator, annotations, (rbs_node_t *) method_type); rbs_node_list_append(overloads, overload); member_range.end = state->current_token.range.end; break; @@ -1705,19 +1821,13 @@ static rbs_ast_members_methoddefinition_t *parse_member_def(parserstate *state, member_range.end = overloading_range.end; break; } else { - raise_syntax_error( - state, - state->next_token, - "unexpected overloading method definition" - ); + syntax_error(state, state->next_token, "unexpected overloading method definition"); + return false; } default: - raise_syntax_error( - state, - state->next_token, - "unexpected token for method type" - ); + syntax_error(state, state->next_token, "unexpected token for method type"); + return false; } if (state->next_token.type == pBAR) { @@ -1745,7 +1855,7 @@ static rbs_ast_members_methoddefinition_t *parse_member_def(parserstate *state, } default: state->aborted = true; - return NULL; + return false; } rbs_location_t *loc = rbs_location_new(state->buffer, member_range); @@ -1756,7 +1866,8 @@ static rbs_ast_members_methoddefinition_t *parse_member_def(parserstate *state, rbs_loc_add_optional_child(loc, INTERN("overloading"), overloading_range); rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range); - return rbs_ast_members_methoddefinition_new(&state->allocator, name, k, overloads, annotations, loc, comment, overloading, visibility); + *method_definition = rbs_ast_members_methoddefinition_new(&state->allocator, name, k, overloads, annotations, loc, comment, overloading, visibility); + return true; } /** @@ -1765,22 +1876,25 @@ static rbs_ast_members_methoddefinition_t *parse_member_def(parserstate *state, * * @param kind * */ -rbs_typename_t *class_instance_name(parserstate *state, TypeNameKind kind, rbs_node_list_t *args, range *name_range, range *args_range) { +NODISCARD +static bool class_instance_name(parserstate *state, TypeNameKind kind, rbs_node_list_t *args, range *name_range, range *args_range, rbs_typename_t **name) { parser_advance(state); - rbs_typename_t *name = parse_type_name(state, kind, name_range); + rbs_typename_t *typename = NULL; + CHECK_PARSE(parse_type_name(state, kind, name_range, &typename)); + *name = typename; if (state->next_token.type == pLBRACKET) { parser_advance(state); args_range->start = state->current_token.range.start; - parse_type_list(state, pRBRACKET, args); + CHECK_PARSE(parse_type_list(state, pRBRACKET, args)); parser_advance_assert(state, pRBRACKET); args_range->end = state->current_token.range.end; } else { *args_range = NULL_RANGE; } - return name; + return true; } /** @@ -1790,7 +1904,8 @@ rbs_typename_t *class_instance_name(parserstate *state, TypeNameKind kind, rbs_n * * @param from_interface `true` when the member is in an interface. * */ -static rbs_node_t *parse_mixin_member(parserstate *state, bool from_interface, position comment_pos, rbs_node_list_t *annotations) { +NODISCARD +static bool parse_mixin_member(parserstate *state, bool from_interface, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **mixin_member) { range member_range; member_range.start = state->current_token.range.start; comment_pos = nonnull_pos_or(comment_pos, member_range.start); @@ -1812,16 +1927,13 @@ static rbs_node_t *parse_mixin_member(parserstate *state, bool from_interface, p break; default: state->aborted = true; - return NULL; + return false; } if (from_interface) { if (state->current_token.type != kINCLUDE) { - raise_syntax_error( - state, - state->current_token, - "unexpected mixin in interface declaration" - ); + syntax_error(state, state->current_token, "unexpected mixin in interface declaration"); + return false; } } @@ -1830,11 +1942,12 @@ static rbs_node_t *parse_mixin_member(parserstate *state, bool from_interface, p rbs_node_list_t *args = rbs_node_list_new(&state->allocator); range name_range; range args_range = NULL_RANGE; - rbs_typename_t *name = class_instance_name( + rbs_typename_t *name = NULL; + CHECK_PARSE(class_instance_name( state, from_interface ? INTERFACE_NAME : (INTERFACE_NAME | CLASS_NAME), - args, &name_range, &args_range - ); + args, &name_range, &args_range, &name + )); parser_pop_typevar_table(state); @@ -1850,14 +1963,17 @@ static rbs_node_t *parse_mixin_member(parserstate *state, bool from_interface, p switch (type) { case kINCLUDE: - return (rbs_node_t *) rbs_ast_members_include_new(&state->allocator, name, args, annotations, loc, comment); + *mixin_member = (rbs_node_t *) rbs_ast_members_include_new(&state->allocator, name, args, annotations, loc, comment); + return true; case kEXTEND: - return (rbs_node_t *) rbs_ast_members_extend_new(&state->allocator, name, args, annotations, loc, comment); + *mixin_member = (rbs_node_t *) rbs_ast_members_extend_new(&state->allocator, name, args, annotations, loc, comment); + return true; case kPREPEND: - return (rbs_node_t *) rbs_ast_members_prepend_new(&state->allocator, name, args, annotations, loc, comment); + *mixin_member = (rbs_node_t *) rbs_ast_members_prepend_new(&state->allocator, name, args, annotations, loc, comment); + return true; default: state->aborted = true; - return NULL; + return false; } } @@ -1869,7 +1985,8 @@ static rbs_node_t *parse_mixin_member(parserstate *state, bool from_interface, p * * @param[in] instance_only `true` to reject `self.` alias. * */ -static rbs_ast_members_alias_t *parse_alias_member(parserstate *state, bool instance_only, position comment_pos, rbs_node_list_t *annotations) { +NODISCARD +static bool parse_alias_member(parserstate *state, bool instance_only, position comment_pos, rbs_node_list_t *annotations, rbs_ast_members_alias_t **alias_member) { range member_range; member_range.start = state->current_token.range.start; range keyword_range = state->current_token.range; @@ -1888,18 +2005,17 @@ static rbs_ast_members_alias_t *parse_alias_member(parserstate *state, bool inst new_kind_range.end = state->next_token2.range.end; parser_advance_assert(state, kSELF); parser_advance_assert(state, pDOT); - new_name = parse_method_name(state, &new_name_range); + CHECK_PARSE(parse_method_name(state, &new_name_range, &new_name)); old_kind_range.start = state->next_token.range.start; old_kind_range.end = state->next_token2.range.end; parser_advance_assert(state, kSELF); parser_advance_assert(state, pDOT); - old_name = parse_method_name(state, &old_name_range); + CHECK_PARSE(parse_method_name(state, &old_name_range, &old_name)); } else { kind = rbs_keyword_new(&state->allocator, INTERN("instance")); - new_name = parse_method_name(state, &new_name_range); - old_name = parse_method_name(state, &old_name_range); - + CHECK_PARSE(parse_method_name(state, &new_name_range, &new_name)); + CHECK_PARSE(parse_method_name(state, &old_name_range, &old_name)); new_kind_range = NULL_RANGE; old_kind_range = NULL_RANGE; } @@ -1913,7 +2029,8 @@ static rbs_ast_members_alias_t *parse_alias_member(parserstate *state, bool inst rbs_loc_add_optional_child(loc, INTERN("new_kind"), new_kind_range); rbs_loc_add_optional_child(loc, INTERN("old_kind"), old_kind_range); - return rbs_ast_members_alias_new(&state->allocator, new_name, old_name, kind, annotations, loc, comment); + *alias_member = rbs_ast_members_alias_new(&state->allocator, new_name, old_name, kind, annotations, loc, comment); + return true; } /* @@ -1921,13 +2038,11 @@ static rbs_ast_members_alias_t *parse_alias_member(parserstate *state, bool inst | {kSELF} `.` tAIDENT `:` | {tA2IDENT} `:` */ -static rbs_node_t *parse_variable_member(parserstate *state, position comment_pos, rbs_node_list_t *annotations) { +NODISCARD +static bool parse_variable_member(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **variable_member) { if (annotations->length > 0) { - raise_syntax_error( - state, - state->current_token, - "annotation cannot be given to variable members" - ); + syntax_error(state, state->current_token, "annotation cannot be given to variable members"); + return false; } range member_range; @@ -1944,7 +2059,8 @@ static rbs_node_t *parse_variable_member(parserstate *state, position comment_po parser_advance_assert(state, pCOLON); range colon_range = state->current_token.range; - rbs_node_t *type = parse_type(state); + rbs_node_t *type; + CHECK_PARSE(parse_type(state, &type)); member_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(state->buffer, member_range); @@ -1953,7 +2069,8 @@ static rbs_node_t *parse_variable_member(parserstate *state, position comment_po rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); - return (rbs_node_t *)rbs_ast_members_instancevariable_new(&state->allocator, name, type, loc, comment); + *variable_member = (rbs_node_t *)rbs_ast_members_instancevariable_new(&state->allocator, name, type, loc, comment); + return true; } case tA2IDENT: { range name_range = state->current_token.range; @@ -1963,7 +2080,10 @@ static rbs_node_t *parse_variable_member(parserstate *state, position comment_po range colon_range = state->current_token.range; parser_push_typevar_table(state, true); - rbs_node_t *type = parse_type(state); + + rbs_node_t *type; + CHECK_PARSE(parse_type(state, &type)); + parser_pop_typevar_table(state); member_range.end = state->current_token.range.end; @@ -1973,7 +2093,8 @@ static rbs_node_t *parse_variable_member(parserstate *state, position comment_po rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); - return (rbs_node_t *) rbs_ast_members_classvariable_new(&state->allocator, name, type, loc, comment); + *variable_member = (rbs_node_t *) rbs_ast_members_classvariable_new(&state->allocator, name, type, loc, comment); + return true; } case kSELF: { range kind_range = { @@ -1991,7 +2112,10 @@ static rbs_node_t *parse_variable_member(parserstate *state, position comment_po range colon_range = state->current_token.range; parser_push_typevar_table(state, true); - rbs_node_t *type = parse_type(state); + + rbs_node_t *type; + CHECK_PARSE(parse_type(state, &type)); + parser_pop_typevar_table(state); member_range.end = state->current_token.range.end; @@ -2001,11 +2125,12 @@ static rbs_node_t *parse_variable_member(parserstate *state, position comment_po rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range); - return (rbs_node_t *)rbs_ast_members_classinstancevariable_new(&state->allocator, name, type, loc, comment); + *variable_member = (rbs_node_t *)rbs_ast_members_classinstancevariable_new(&state->allocator, name, type, loc, comment); + return true; } default: state->aborted = true; - return NULL; + return false; } } @@ -2013,26 +2138,29 @@ static rbs_node_t *parse_variable_member(parserstate *state, position comment_po visibility_member ::= {<`public`>} | {<`private`>} */ -static rbs_node_t *parse_visibility_member(parserstate *state, rbs_node_list_t *annotations) { +NODISCARD +static bool parse_visibility_member(parserstate *state, rbs_node_list_t *annotations, rbs_node_t **visibility_member) { if (annotations->length > 0) { - raise_syntax_error( - state, - state->current_token, - "annotation cannot be given to visibility members" - ); + syntax_error(state, state->current_token, "annotation cannot be given to visibility members"); + return false; } rbs_location_t *location = rbs_location_new(state->buffer, state->current_token.range); switch (state->current_token.type) { - case kPUBLIC: - return (rbs_node_t *) rbs_ast_members_public_new(&state->allocator, location); - case kPRIVATE: - return (rbs_node_t *) rbs_ast_members_private_new(&state->allocator, location); - default: + case kPUBLIC: { + *visibility_member = (rbs_node_t *) rbs_ast_members_public_new(&state->allocator, location); + return true; + } + case kPRIVATE: { + *visibility_member = (rbs_node_t *) rbs_ast_members_private_new(&state->allocator, location); + return true; + } + default: { state->aborted = true; - return NULL; + return false; + } } } @@ -2050,7 +2178,8 @@ static rbs_node_t *parse_visibility_member(parserstate *state, rbs_node_list_t * | `(` tAIDENT `)` # Ivar name | `(` `)` # No variable */ -static rbs_node_t *parse_attribute_member(parserstate *state, position comment_pos, rbs_node_list_t *annotations) { +NODISCARD +static bool parse_attribute_member(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **attribute_member) { range member_range; member_range.start = state->current_token.range.start; @@ -2084,10 +2213,13 @@ static rbs_node_t *parse_attribute_member(parserstate *state, position comment_p range kind_range; InstanceSingletonKind is_kind = parse_instance_singleton_kind(state, false, &kind_range); + if (has_error(state)) return false; + rbs_keyword_t *kind = rbs_keyword_new(&state->allocator, INTERN(((is_kind == INSTANCE_KIND) ? "instance" : "singleton"))); range name_range; - rbs_ast_symbol_t *attr_name = parse_method_name(state, &name_range); + rbs_ast_symbol_t *attr_name; + CHECK_PARSE(parse_method_name(state, &name_range, &attr_name)); rbs_node_t *ivar_name; // rbs_ast_symbol_t, NULL or rbs_ast_bool_new(&state->allocator, false) range ivar_range, ivar_name_range; @@ -2115,7 +2247,10 @@ static rbs_node_t *parse_attribute_member(parserstate *state, position comment_p range colon_range = state->current_token.range; parser_push_typevar_table(state, is_kind == SINGLETON_KIND); - rbs_node_t *type = parse_type(state); + + rbs_node_t *type; + CHECK_PARSE(parse_type(state, &type)); + parser_pop_typevar_table(state); member_range.end = state->current_token.range.end; @@ -2132,14 +2267,17 @@ static rbs_node_t *parse_attribute_member(parserstate *state, position comment_p switch (attr_type) { case kATTRREADER: - return (rbs_node_t *) rbs_ast_members_attrreader_new(&state->allocator, attr_name, type, ivar_name, kind, annotations, loc, comment, visibility); + *attribute_member = (rbs_node_t *) rbs_ast_members_attrreader_new(&state->allocator, attr_name, type, ivar_name, kind, annotations, loc, comment, visibility); + return true; case kATTRWRITER: - return (rbs_node_t *) rbs_ast_members_attrwriter_new(&state->allocator, attr_name, type, ivar_name, kind, annotations, loc, comment, visibility); + *attribute_member = (rbs_node_t *) rbs_ast_members_attrwriter_new(&state->allocator, attr_name, type, ivar_name, kind, annotations, loc, comment, visibility); + return true; case kATTRACCESSOR: - return (rbs_node_t *) rbs_ast_members_attraccessor_new(&state->allocator, attr_name, type, ivar_name, kind, annotations, loc, comment, visibility); + *attribute_member = (rbs_node_t *) rbs_ast_members_attraccessor_new(&state->allocator, attr_name, type, ivar_name, kind, annotations, loc, comment, visibility); + return true; default: state->aborted = true; - return NULL; + return false; } } @@ -2150,54 +2288,56 @@ static rbs_node_t *parse_attribute_member(parserstate *state, position comment_p | mixin_member (interface only) | alias_member (instance only) */ -static rbs_node_list_t *parse_interface_members(parserstate *state) { - rbs_node_list_t *members = rbs_node_list_new(&state->allocator); +NODISCARD +static bool parse_interface_members(parserstate *state, rbs_node_list_t **members) { + *members = rbs_node_list_new(&state->allocator); while (state->next_token.type != kEND) { rbs_node_list_t *annotations = rbs_node_list_new(&state->allocator); position annot_pos = NullPosition; - parse_annotations(state, annotations, &annot_pos); - + CHECK_PARSE(parse_annotations(state, annotations, &annot_pos)); parser_advance(state); rbs_node_t *member; switch (state->current_token.type) { case kDEF: { - member = (rbs_node_t *) parse_member_def(state, true, true, annot_pos, annotations); + rbs_ast_members_methoddefinition_t *method_definition = NULL; + CHECK_PARSE(parse_member_def(state, true, true, annot_pos, annotations, &method_definition)); + member = (rbs_node_t *) method_definition; break; } case kINCLUDE: case kEXTEND: case kPREPEND: { - member = (rbs_node_t *) parse_mixin_member(state, true, annot_pos, annotations); + CHECK_PARSE(parse_mixin_member(state, true, annot_pos, annotations, &member)); break; } case kALIAS: { - member = (rbs_node_t *) parse_alias_member(state, true, annot_pos, annotations); + rbs_ast_members_alias_t *alias_member = NULL; + CHECK_PARSE(parse_alias_member(state, true, annot_pos, annotations, &alias_member)); + member = (rbs_node_t *) alias_member; break; } default: - raise_syntax_error( - state, - state->current_token, - "unexpected token for interface declaration member" - ); + syntax_error(state, state->current_token, "unexpected token for interface declaration member"); + return false; } - rbs_node_list_append(members, member); + rbs_node_list_append(*members, member); } - return members; + return true; } /* interface_decl ::= {`interface`} interface_name module_type_params interface_members */ -static rbs_ast_declarations_interface_t *parse_interface_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations) { +NODISCARD +static bool parse_interface_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_interface_t **interface_decl) { parser_push_typevar_table(state, true); range member_range; @@ -2209,12 +2349,15 @@ static rbs_ast_declarations_interface_t *parse_interface_decl(parserstate *state parser_advance(state); range name_range; - rbs_typename_t *name = parse_type_name(state, INTERFACE_NAME, &name_range); + rbs_typename_t *name = NULL; + CHECK_PARSE(parse_type_name(state, INTERFACE_NAME, &name_range, &name)); range type_params_range; rbs_node_list_t *type_params = parse_type_params(state, &type_params_range, true); + if (has_error(state)) return false; - rbs_node_list_t *members = parse_interface_members(state); + rbs_node_list_t *members = NULL; + CHECK_PARSE(parse_interface_members(state, &members)); parser_advance_assert(state, kEND); range end_range = state->current_token.range; @@ -2231,7 +2374,8 @@ static rbs_ast_declarations_interface_t *parse_interface_decl(parserstate *state rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); - return rbs_ast_declarations_interface_new(&state->allocator, name, type_params, members, annotations, loc, comment); + *interface_decl = rbs_ast_declarations_interface_new(&state->allocator, name, type_params, members, annotations, loc, comment); + return true; } /* @@ -2240,14 +2384,17 @@ static rbs_ast_declarations_interface_t *parse_interface_decl(parserstate *state module_self_type ::= | module_name `[` type_list <`]`> */ -static void parse_module_self_types(parserstate *state, rbs_node_list_t *array) { +NODISCARD +static bool parse_module_self_types(parserstate *state, rbs_node_list_t *array) { while (true) { parser_advance(state); range self_range; self_range.start = state->current_token.range.start; + range name_range; - rbs_typename_t *module_name = parse_type_name(state, CLASS_NAME | INTERFACE_NAME, &name_range); + rbs_typename_t *module_name = NULL; + CHECK_PARSE(parse_type_name(state, CLASS_NAME | INTERFACE_NAME, &name_range, &module_name)); self_range.end = name_range.end; rbs_node_list_t *args = rbs_node_list_new(&state->allocator); @@ -2255,7 +2402,7 @@ static void parse_module_self_types(parserstate *state, rbs_node_list_t *array) if (state->next_token.type == pLBRACKET) { parser_advance(state); args_range.start = state->current_token.range.start; - parse_type_list(state, pRBRACKET, args); + CHECK_PARSE(parse_type_list(state, pRBRACKET, args)); parser_advance(state); self_range.end = args_range.end = state->current_token.range.end; } @@ -2274,9 +2421,12 @@ static void parse_module_self_types(parserstate *state, rbs_node_list_t *array) break; } } + + return true; } -static rbs_node_t *parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, rbs_node_list_t *annotations); +NODISCARD +static bool parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl); /* module_members ::= {} ... kEND @@ -2289,14 +2439,14 @@ static rbs_node_t *parse_nested_decl(parserstate *state, const char *nested_in, | `public` | `private` */ -static rbs_node_list_t *parse_module_members(parserstate *state) { - rbs_node_list_t *members = rbs_node_list_new(&state->allocator); +NODISCARD +static bool parse_module_members(parserstate *state, rbs_node_list_t **members) { + *members = rbs_node_list_new(&state->allocator); while (state->next_token.type != kEND) { rbs_node_list_t *annotations = rbs_node_list_new(&state->allocator); - - position annot_pos = NullPosition; - parse_annotations(state, annotations, &annot_pos); + position annot_pos; + CHECK_PARSE(parse_annotations(state, annotations, &annot_pos)); parser_advance(state); @@ -2304,33 +2454,35 @@ static rbs_node_list_t *parse_module_members(parserstate *state) { switch (state->current_token.type) { case kDEF: { - member = (rbs_node_t *) parse_member_def(state, false, true, annot_pos, annotations); + rbs_ast_members_methoddefinition_t *method_definition; + CHECK_PARSE(parse_member_def(state, false, true, annot_pos, annotations, &method_definition)); + member = (rbs_node_t *) method_definition; break; } case kINCLUDE: case kEXTEND: case kPREPEND: { - member = (rbs_node_t *) parse_mixin_member(state, false, annot_pos, annotations); + CHECK_PARSE(parse_mixin_member(state, false, annot_pos, annotations, &member)); break; } - case kALIAS: { - member = (rbs_node_t *) parse_alias_member(state, false, annot_pos, annotations); + rbs_ast_members_alias_t *alias_member = NULL; + CHECK_PARSE(parse_alias_member(state, false, annot_pos, annotations, &alias_member)); + member = (rbs_node_t *) alias_member; break; } - case tAIDENT: case tA2IDENT: case kSELF: { - member = parse_variable_member(state, annot_pos, annotations); + CHECK_PARSE(parse_variable_member(state, annot_pos, annotations, &member)); break; } case kATTRREADER: case kATTRWRITER: case kATTRACCESSOR: { - member = parse_attribute_member(state, annot_pos, annotations); + CHECK_PARSE(parse_attribute_member(state, annot_pos, annotations, &member)); break; } @@ -2340,39 +2492,43 @@ static rbs_node_list_t *parse_module_members(parserstate *state) { switch (state->next_token.type) { case kDEF: { - member = (rbs_node_t *) parse_member_def(state, false, true, annot_pos, annotations); + rbs_ast_members_methoddefinition_t *method_definition = NULL; + CHECK_PARSE(parse_member_def(state, false, true, annot_pos, annotations, &method_definition)); + member = (rbs_node_t *) method_definition; break; } case kATTRREADER: case kATTRWRITER: case kATTRACCESSOR: { - member = parse_attribute_member(state, annot_pos, annotations); + CHECK_PARSE(parse_attribute_member(state, annot_pos, annotations, &member)); break; } default: - raise_syntax_error(state, state->next_token, "method or attribute definition is expected after visibility modifier"); + syntax_error(state, state->next_token, "method or attribute definition is expected after visibility modifier"); + return false; } } else { - member = parse_visibility_member(state, annotations); + CHECK_PARSE(parse_visibility_member(state, annotations, &member)); } break; default: - member = parse_nested_decl(state, "module", annot_pos, annotations); + CHECK_PARSE(parse_nested_decl(state, "module", annot_pos, annotations, &member)); break; } - rbs_node_list_append(members, member); + rbs_node_list_append(*members, member); } - return members; + return true; } /* module_decl ::= {module_name} module_type_params module_members | {module_name} module_name module_type_params `:` module_self_types module_members */ -static rbs_ast_declarations_module_t *parse_module_decl0(parserstate *state, range keyword_range, rbs_typename_t *module_name, range name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations) { +NODISCARD +static bool parse_module_decl0(parserstate *state, range keyword_range, rbs_typename_t *module_name, range name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_module_t **module_decl) { parser_push_typevar_table(state, true); range decl_range; @@ -2380,6 +2536,7 @@ static rbs_ast_declarations_module_t *parse_module_decl0(parserstate *state, ran range type_params_range; rbs_node_list_t *type_params = parse_type_params(state, &type_params_range, true); + if (has_error(state)) return false; rbs_node_list_t *self_types = rbs_node_list_new(&state->allocator); range colon_range; @@ -2388,14 +2545,15 @@ static rbs_ast_declarations_module_t *parse_module_decl0(parserstate *state, ran parser_advance(state); colon_range = state->current_token.range; self_types_range.start = state->next_token.range.start; - parse_module_self_types(state, self_types); + CHECK_PARSE(parse_module_self_types(state, self_types)); self_types_range.end = state->current_token.range.end; } else { colon_range = NULL_RANGE; self_types_range = NULL_RANGE; } - rbs_node_list_t *members = parse_module_members(state); + rbs_node_list_t *members = NULL; + CHECK_PARSE(parse_module_members(state, &members)); parser_advance_assert(state, kEND); range end_range = state->current_token.range; @@ -2412,7 +2570,8 @@ static rbs_ast_declarations_module_t *parse_module_decl0(parserstate *state, ran parser_pop_typevar_table(state); - return rbs_ast_declarations_module_new(&state->allocator, module_name, type_params, self_types, members, annotations, loc, comment); + *module_decl = rbs_ast_declarations_module_new(&state->allocator, module_name, type_params, self_types, members, annotations, loc, comment); + return true; } /* @@ -2420,15 +2579,18 @@ static rbs_ast_declarations_module_t *parse_module_decl0(parserstate *state, ran | {`module`} module_name module_decl0 */ -static rbs_node_t *parse_module_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations) { +NODISCARD +static bool parse_module_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **module_decl) { range keyword_range = state->current_token.range; comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start); rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); parser_advance(state); + range module_name_range; - rbs_typename_t *module_name = parse_type_name(state, CLASS_NAME, &module_name_range); + rbs_typename_t *module_name; + CHECK_PARSE(parse_type_name(state, CLASS_NAME, &module_name_range, &module_name)); if (state->next_token.type == pEQ) { range eq_range = state->next_token.range; @@ -2436,7 +2598,8 @@ static rbs_node_t *parse_module_decl(parserstate *state, position comment_pos, r parser_advance(state); range old_name_range; - rbs_typename_t *old_name = parse_type_name(state, CLASS_NAME, &old_name_range); + rbs_typename_t *old_name = NULL; + CHECK_PARSE(parse_type_name(state, CLASS_NAME, &old_name_range, &old_name)); range decl_range = { .start = keyword_range.start, @@ -2450,17 +2613,22 @@ static rbs_node_t *parse_module_decl(parserstate *state, position comment_pos, r rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); - return (rbs_node_t *) rbs_ast_declarations_modulealias_new(&state->allocator, module_name, old_name, loc, comment); + *module_decl = (rbs_node_t *) rbs_ast_declarations_modulealias_new(&state->allocator, module_name, old_name, loc, comment); } else { - return (rbs_node_t *) parse_module_decl0(state, keyword_range, module_name, module_name_range, comment, annotations); + rbs_ast_declarations_module_t *module_decl0 = NULL; + CHECK_PARSE(parse_module_decl0(state, keyword_range, module_name, module_name_range, comment, annotations, &module_decl0)); + *module_decl = (rbs_node_t *) module_decl0; } + + return true; } /* class_decl_super ::= {} `<` | {<>} */ -static rbs_ast_declarations_class_super_t *parse_class_decl_super(parserstate *state, range *lt_range) { +NODISCARD +static bool parse_class_decl_super(parserstate *state, range *lt_range, rbs_ast_declarations_class_super_t **super) { if (parser_advance_if(state, pLT)) { *lt_range = state->current_token.range; @@ -2468,8 +2636,9 @@ static rbs_ast_declarations_class_super_t *parse_class_decl_super(parserstate *s super_range.start = state->next_token.range.start; rbs_node_list_t *args = rbs_node_list_new(&state->allocator); + rbs_typename_t *name = NULL; range name_range, args_range; - rbs_typename_t *name = class_instance_name(state, CLASS_NAME, args, &name_range, &args_range); + CHECK_PARSE(class_instance_name(state, CLASS_NAME, args, &name_range, &args_range, &name)); super_range.end = state->current_token.range.end; @@ -2479,17 +2648,19 @@ static rbs_ast_declarations_class_super_t *parse_class_decl_super(parserstate *s rbs_loc_add_optional_child(loc, INTERN("args"), args_range); - return rbs_ast_declarations_class_super_new(&state->allocator, name, args, loc); + *super = rbs_ast_declarations_class_super_new(&state->allocator, name, args, loc); } else { *lt_range = NULL_RANGE; - return NULL; } + + return true; } /* class_decl ::= {class_name} type_params class_decl_super class_members <`end`> */ -static rbs_ast_declarations_class_t *parse_class_decl0(parserstate *state, range keyword_range, rbs_typename_t *name, range name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations) { +NODISCARD +static bool parse_class_decl0(parserstate *state, range keyword_range, rbs_typename_t *name, range name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_class_t **class_decl) { parser_push_typevar_table(state, true); range decl_range; @@ -2497,11 +2668,15 @@ static rbs_ast_declarations_class_t *parse_class_decl0(parserstate *state, range range type_params_range; rbs_node_list_t *type_params = parse_type_params(state, &type_params_range, true); + if (has_error(state)) return false; range lt_range; - rbs_ast_declarations_class_super_t *super = parse_class_decl_super(state, <_range); + rbs_ast_declarations_class_super_t *super = NULL; + CHECK_PARSE(parse_class_decl_super(state, <_range, &super)); + + rbs_node_list_t *members = NULL; + CHECK_PARSE(parse_module_members(state, &members)); - struct rbs_node_list *members = parse_module_members(state); parser_advance_assert(state, kEND); range end_range = state->current_token.range; @@ -2518,14 +2693,16 @@ static rbs_ast_declarations_class_t *parse_class_decl0(parserstate *state, range rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range); rbs_loc_add_optional_child(loc, INTERN("lt"), lt_range); - return rbs_ast_declarations_class_new(&state->allocator, name, type_params, super, members, annotations, loc, comment); + *class_decl = rbs_ast_declarations_class_new(&state->allocator, name, type_params, super, members, annotations, loc, comment); + return true; } /* class_decl ::= {`class`} class_name `=` | {`class`} class_name */ -static rbs_node_t *parse_class_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations) { +NODISCARD +static bool parse_class_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **class_decl) { range keyword_range = state->current_token.range; comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start); @@ -2533,7 +2710,8 @@ static rbs_node_t *parse_class_decl(parserstate *state, position comment_pos, rb parser_advance(state); range class_name_range; - rbs_typename_t *class_name = parse_type_name(state, CLASS_NAME, &class_name_range); + rbs_typename_t *class_name = NULL; + CHECK_PARSE(parse_type_name(state, CLASS_NAME, &class_name_range, &class_name)); if (state->next_token.type == pEQ) { range eq_range = state->next_token.range; @@ -2541,7 +2719,8 @@ static rbs_node_t *parse_class_decl(parserstate *state, position comment_pos, rb parser_advance(state); range old_name_range; - rbs_typename_t *old_name = parse_type_name(state, CLASS_NAME, &old_name_range); + rbs_typename_t *old_name = NULL; + CHECK_PARSE(parse_type_name(state, CLASS_NAME, &old_name_range, &old_name)); range decl_range = { .start = keyword_range.start, @@ -2555,11 +2734,14 @@ static rbs_node_t *parse_class_decl(parserstate *state, position comment_pos, rb rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); - - return (rbs_node_t *) rbs_ast_declarations_classalias_new(&state->allocator, class_name, old_name, loc, comment); + *class_decl = (rbs_node_t *) rbs_ast_declarations_classalias_new(&state->allocator, class_name, old_name, loc, comment); } else { - return (rbs_node_t *) parse_class_decl0(state, keyword_range, class_name, class_name_range, comment, annotations); + rbs_ast_declarations_class_t *class_decl0 = NULL; + CHECK_PARSE(parse_class_decl0(state, keyword_range, class_name, class_name_range, comment, annotations, &class_decl0)); + *class_decl = (rbs_node_t *) class_decl0; } + + return true; } /* @@ -2569,82 +2751,107 @@ static rbs_node_t *parse_class_decl(parserstate *state, position comment_pos, rb | {} | {} */ -static rbs_node_t *parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, rbs_node_list_t *annotations) { +NODISCARD +static bool parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl) { parser_push_typevar_table(state, true); - rbs_node_t *decl; switch (state->current_token.type) { case tUIDENT: case pCOLON2: { - decl = (rbs_node_t *) parse_const_decl(state); + rbs_ast_declarations_constant_t *constant = NULL; + CHECK_PARSE(parse_const_decl(state, &constant)); + *decl = (rbs_node_t *) constant; break; } case tGIDENT: { - decl = (rbs_node_t *) parse_global_decl(state); + rbs_ast_declarations_global_t *global = NULL; + CHECK_PARSE(parse_global_decl(state, &global)); + *decl = (rbs_node_t *) global; break; } case kTYPE: { - decl = (rbs_node_t *) parse_type_decl(state, annot_pos, annotations); + rbs_ast_declarations_typealias_t *typealias = NULL; + CHECK_PARSE(parse_type_decl(state, annot_pos, annotations, &typealias)); + *decl = (rbs_node_t *) typealias; break; } case kINTERFACE: { - decl = (rbs_node_t *) parse_interface_decl(state, annot_pos, annotations); + rbs_ast_declarations_interface_t *interface_decl = NULL; + CHECK_PARSE(parse_interface_decl(state, annot_pos, annotations, &interface_decl)); + *decl = (rbs_node_t *) interface_decl; break; } case kMODULE: { - decl = (rbs_node_t *) parse_module_decl(state, annot_pos, annotations); + rbs_node_t *module_decl = NULL; + CHECK_PARSE(parse_module_decl(state, annot_pos, annotations, &module_decl)); + *decl = module_decl; break; } case kCLASS: { - decl = (rbs_node_t *) parse_class_decl(state, annot_pos, annotations); + rbs_node_t *class_decl = NULL; + CHECK_PARSE(parse_class_decl(state, annot_pos, annotations, &class_decl)); + *decl = class_decl; break; } default: - raise_syntax_error( - state, - state->current_token, - "unexpected token for class/module declaration member" - ); + syntax_error(state, state->current_token, "unexpected token for class/module declaration member"); + return false; } parser_pop_typevar_table(state); - return decl; + return true; } -static rbs_node_t *parse_decl(parserstate *state) { +NODISCARD +static bool parse_decl(parserstate *state, rbs_node_t **decl) { rbs_node_list_t *annotations = rbs_node_list_new(&state->allocator); position annot_pos = NullPosition; - parse_annotations(state, annotations, &annot_pos); - + CHECK_PARSE(parse_annotations(state, annotations, &annot_pos)); parser_advance(state); + switch (state->current_token.type) { case tUIDENT: case pCOLON2: { - return (rbs_node_t *) parse_const_decl(state); + rbs_ast_declarations_constant_t *constant = NULL; + CHECK_PARSE(parse_const_decl(state, &constant)); + *decl = (rbs_node_t *) constant; + return true; } case tGIDENT: { - return (rbs_node_t *) parse_global_decl(state); + rbs_ast_declarations_global_t *global = NULL; + CHECK_PARSE(parse_global_decl(state, &global)); + *decl = (rbs_node_t *) global; + return true; } case kTYPE: { - return (rbs_node_t *) parse_type_decl(state, annot_pos, annotations); + rbs_ast_declarations_typealias_t *typealias = NULL; + CHECK_PARSE(parse_type_decl(state, annot_pos, annotations, &typealias)); + *decl = (rbs_node_t *) typealias; + return true; } case kINTERFACE: { - return (rbs_node_t *) parse_interface_decl(state, annot_pos, annotations); + rbs_ast_declarations_interface_t *interface_decl = NULL; + CHECK_PARSE(parse_interface_decl(state, annot_pos, annotations, &interface_decl)); + *decl = (rbs_node_t *) interface_decl; + return true; } case kMODULE: { - return (rbs_node_t *) parse_module_decl(state, annot_pos, annotations); + rbs_node_t *module_decl = NULL; + CHECK_PARSE(parse_module_decl(state, annot_pos, annotations, &module_decl)); + *decl = module_decl; + return true; } case kCLASS: { - return (rbs_node_t *) parse_class_decl(state, annot_pos, annotations); + rbs_node_t *class_decl = NULL; + CHECK_PARSE(parse_class_decl(state, annot_pos, annotations, &class_decl)); + *decl = class_decl; + return true; } default: - raise_syntax_error( - state, - state->current_token, - "cannot start a declaration" - ); + syntax_error(state, state->current_token, "cannot start a declaration"); + return false; } } @@ -2652,7 +2859,8 @@ static rbs_node_t *parse_decl(parserstate *state) { namespace ::= {} (`::`)? (`tUIDENT` `::`)* `tUIDENT` <`::`> | {} <> (empty -- returns empty namespace) */ -static rbs_namespace_t *parse_namespace(parserstate *state, range *rg) { +NODISCARD +static bool parse_namespace(parserstate *state, range *rg, rbs_namespace_t **namespace) { bool is_absolute = false; if (state->next_token.type == pCOLON2) { @@ -2682,7 +2890,8 @@ static rbs_namespace_t *parse_namespace(parserstate *state, range *rg) { } } - return rbs_namespace_new(&state->allocator, path, is_absolute); + *namespace = rbs_namespace_new(&state->allocator, path, is_absolute); + return true; } /* @@ -2692,10 +2901,12 @@ static rbs_namespace_t *parse_namespace(parserstate *state, range *rg) { | {} namespace tUIDENT `as` | {} namespace */ -static void parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { +NODISCARD +static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { while (true) { range namespace_range = NULL_RANGE; - rbs_namespace_t *namespace = parse_namespace(state, &namespace_range); + rbs_namespace_t *namespace = NULL; + CHECK_PARSE(parse_namespace(state, &namespace_range, &namespace)); switch (state->next_token.type) { @@ -2760,11 +2971,8 @@ static void parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { break; } default: - raise_syntax_error( - state, - state->next_token, - "use clause is expected" - ); + syntax_error(state, state->next_token, "use clause is expected"); + return false; } if (state->next_token.type == pCOMMA) { @@ -2774,20 +2982,21 @@ static void parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { } } - return; + return true; } /* use_directive ::= {} `use` */ -static rbs_ast_directives_use_t *parse_use_directive(parserstate *state) { +NODISCARD +static bool parse_use_directive(parserstate *state, rbs_ast_directives_use_t **use_directive) { if (state->next_token.type == kUSE) { parser_advance(state); range keyword_range = state->current_token.range; rbs_node_list_t *clauses = rbs_node_list_new(&state->allocator); - parse_use_clauses(state, clauses); + CHECK_PARSE(parse_use_clauses(state, clauses)); range directive_range = keyword_range; directive_range.end = state->current_token.range.end; @@ -2796,18 +3005,20 @@ static rbs_ast_directives_use_t *parse_use_directive(parserstate *state) { rbs_loc_alloc_children(loc, 1); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); - return rbs_ast_directives_use_new(&state->allocator, clauses, loc); - } else { - return NULL; + *use_directive = rbs_ast_directives_use_new(&state->allocator, clauses, loc); } + + return true; } -rbs_signature_t *parse_signature(parserstate *state) { +bool parse_signature(parserstate *state, rbs_signature_t **signature) { rbs_node_list_t *dirs = rbs_node_list_new(&state->allocator); rbs_node_list_t *decls = rbs_node_list_new(&state->allocator); while (state->next_token.type == kUSE) { - rbs_ast_directives_use_t *use_node = parse_use_directive(state); + rbs_ast_directives_use_t *use_node; + CHECK_PARSE(parse_use_directive(state, &use_node)); + if (use_node == NULL) { rbs_node_list_append(dirs, NULL); } else { @@ -2816,11 +3027,13 @@ rbs_signature_t *parse_signature(parserstate *state) { } while (state->next_token.type != pEOF) { - rbs_node_t *decl = parse_decl(state); + rbs_node_t *decl = NULL; + CHECK_PARSE(parse_decl(state, &decl)); rbs_node_list_append(decls, decl); } - return rbs_signature_new(&state->allocator, dirs, decls); + *signature = rbs_signature_new(&state->allocator, dirs, decls); + return true; } struct parse_type_arg { @@ -2843,12 +3056,17 @@ parse_type_try(VALUE a) { return Qnil; } - rbs_node_t *type = parse_type(parser); + rbs_node_t *type; + parse_type(parser, &type); if (parser->aborted) { rb_raise(rb_eRuntimeError, "Unexpected error"); } + if (parser->error) { + raise_syntax_error(parser, parser->error); + } + if (RB_TEST(arg->require_eof)) { parser_advance_assert(parser, pEOF); } @@ -2882,12 +3100,17 @@ parse_method_type_try(VALUE a) { return Qnil; } - rbs_methodtype_t *method_type = parse_method_type(parser); + rbs_methodtype_t *method_type = NULL; + parse_method_type(parser, &method_type); if (parser->aborted) { rb_raise(rb_eRuntimeError, "Unexpected error"); } + if (parser->error) { + raise_syntax_error(parser, parser->error); + } + if (RB_TEST(arg->require_eof)) { parser_advance_assert(parser, pEOF); } @@ -2916,12 +3139,17 @@ static VALUE parse_signature_try(VALUE a) { parserstate *parser = (parserstate *)a; - rbs_signature_t *signature = parse_signature(parser); + rbs_signature_t *signature = NULL; + parse_signature(parser, &signature); if (parser->aborted) { rb_raise(rb_eRuntimeError, "Unexpected error"); } + if (parser->error) { + raise_syntax_error(parser, parser->error); + } + rbs_translation_context_t ctx = { .constant_pool = &parser->constant_pool, }; diff --git a/ext/rbs_extension/parser.h b/ext/rbs_extension/parser.h index 16b84987c..d8ab8bc73 100644 --- a/ext/rbs_extension/parser.h +++ b/ext/rbs_extension/parser.h @@ -9,9 +9,9 @@ * */ extern VALUE RBS_Parser; -rbs_node_t *parse_type(parserstate *state); -rbs_methodtype_t *parse_method_type(parserstate *state); -rbs_signature_t *parse_signature(parserstate *state); +bool parse_type(parserstate *state, rbs_node_t **type); +bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type); +bool parse_signature(parserstate *state, rbs_signature_t **signature); void rbs__init_parser(); diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index b376bed80..d6db856c2 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -140,12 +140,13 @@ bool parser_advance_if(parserstate *state, enum TokenType type) { void parser_assert(parserstate *state, enum TokenType type) { if (state->current_token.type != type) { - raise_syntax_error( + syntax_error( state, state->current_token, "expected a token `%s`", token_type_str(type) ); + raise_syntax_error(state, state->error); } } diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index f61d32c74..f5fbf7bce 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -44,6 +44,11 @@ typedef struct comment { struct comment *next_comment; } comment; +typedef struct error { + char *message; + token token; +} error; + /** * An RBS parser is a LL(3) parser. * */ @@ -62,6 +67,7 @@ typedef struct { rbs_constant_pool_t constant_pool; rbs_allocator_t allocator; bool aborted; + error *error; } parserstate; comment *alloc_comment(rbs_allocator_t *, token comment_token, comment *last_comment); diff --git a/ext/rbs_extension/rbs_extension.h b/ext/rbs_extension/rbs_extension.h index f8a11f67d..c3d4c5fbc 100644 --- a/ext/rbs_extension/rbs_extension.h +++ b/ext/rbs_extension/rbs_extension.h @@ -22,6 +22,8 @@ * */ VALUE rbs_unquote_string(parserstate *state, range rg, int offset_bytes); +PRINTF_ARGS(void syntax_error(parserstate *state, token tok, const char *fmt, ...), 3, 4); + /** * Raises RBS::ParsingError on `tok` with message constructed with given `fmt`. * @@ -29,4 +31,4 @@ VALUE rbs_unquote_string(parserstate *state, range rg, int offset_bytes); * foo.rbs:11:21...11:25: Syntax error: {message}, token=`{tok source}` ({tok type}) * ``` * */ -PRINTF_ARGS(NORETURN(void) raise_syntax_error(parserstate *state, token tok, const char *fmt, ...), 3, 4); +NORETURN(void) raise_syntax_error(parserstate *state, error *error); From 1ab4c329961d8ea825563c4f6396842f453336fb Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Thu, 28 Nov 2024 18:31:37 -0500 Subject: [PATCH 010/111] Make `parse_type_params` return a NODISCARD bool Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/parser.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index 00ccb6562..ad34570a3 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -1258,8 +1258,9 @@ bool parse_type(parserstate *state, rbs_node_t **type) { type_param ::= tUIDENT upper_bound? default_type? (module_type_params == false) */ -static rbs_node_list_t *parse_type_params(parserstate *state, range *rg, bool module_type_params) { - rbs_node_list_t *params = rbs_node_list_new(&state->allocator); +NODISCARD +static bool parse_type_params(parserstate *state, range *rg, bool module_type_params, rbs_node_list_t **params) { + *params = rbs_node_list_new(&state->allocator); bool required_param_allowed = true; @@ -1296,7 +1297,7 @@ static rbs_node_list_t *parse_type_params(parserstate *state, range *rg, bool mo break; default: state->aborted = true; - return NULL; + return false; } parser_advance(state); @@ -1338,7 +1339,7 @@ static rbs_node_list_t *parse_type_params(parserstate *state, range *rg, bool mo } else { if (!required_param_allowed) { syntax_error(state, state->current_token, "required type parameter is not allowed after optional type parameter"); - return NULL; + return false; } } } @@ -1355,7 +1356,7 @@ static rbs_node_list_t *parse_type_params(parserstate *state, range *rg, bool mo rbs_ast_typeparam_t *param = rbs_ast_typeparam_new(&state->allocator, name, variance, upper_bound, default_type, unchecked, loc); - rbs_node_list_append(params, (rbs_node_t *) param); + rbs_node_list_append(*params, (rbs_node_t *) param); if (state->next_token.type == pCOMMA) { parser_advance(state); @@ -1372,7 +1373,7 @@ static rbs_node_list_t *parse_type_params(parserstate *state, range *rg, bool mo *rg = NULL_RANGE; } - return params; + return true; } /* @@ -1386,8 +1387,8 @@ bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type) { rg.start = state->next_token.range.start; range params_range = NULL_RANGE; - rbs_node_list_t *type_params = parse_type_params(state, ¶ms_range, false); // TODO - if (has_error(state)) return false; + rbs_node_list_t *type_params; + CHECK_PARSE(parse_type_params(state, ¶ms_range, false, &type_params)); range type_range; type_range.start = state->next_token.range.start; @@ -1489,8 +1490,8 @@ static bool parse_type_decl(parserstate *state, position comment_pos, rbs_node_l CHECK_PARSE(parse_type_name(state, ALIAS_NAME, &name_range, &typename)); range params_range; - rbs_node_list_t *type_params = parse_type_params(state, ¶ms_range, true); - if (has_error(state)) return false; + rbs_node_list_t *type_params; + CHECK_PARSE(parse_type_params(state, ¶ms_range, true, &type_params)); parser_advance_assert(state, pEQ); range eq_range = state->current_token.range; @@ -2353,8 +2354,8 @@ static bool parse_interface_decl(parserstate *state, position comment_pos, rbs_n CHECK_PARSE(parse_type_name(state, INTERFACE_NAME, &name_range, &name)); range type_params_range; - rbs_node_list_t *type_params = parse_type_params(state, &type_params_range, true); - if (has_error(state)) return false; + rbs_node_list_t *type_params; + CHECK_PARSE(parse_type_params(state, &type_params_range, true, &type_params)); rbs_node_list_t *members = NULL; CHECK_PARSE(parse_interface_members(state, &members)); @@ -2535,8 +2536,8 @@ static bool parse_module_decl0(parserstate *state, range keyword_range, rbs_type decl_range.start = keyword_range.start; range type_params_range; - rbs_node_list_t *type_params = parse_type_params(state, &type_params_range, true); - if (has_error(state)) return false; + rbs_node_list_t *type_params; + CHECK_PARSE(parse_type_params(state, &type_params_range, true, &type_params)); rbs_node_list_t *self_types = rbs_node_list_new(&state->allocator); range colon_range; @@ -2667,8 +2668,8 @@ static bool parse_class_decl0(parserstate *state, range keyword_range, rbs_typen decl_range.start = keyword_range.start; range type_params_range; - rbs_node_list_t *type_params = parse_type_params(state, &type_params_range, true); - if (has_error(state)) return false; + rbs_node_list_t *type_params; + CHECK_PARSE(parse_type_params(state, &type_params_range, true, &type_params)); range lt_range; rbs_ast_declarations_class_super_t *super = NULL; From 62e7e79e2f18c6f5cd0af6a599efc42fc6843a25 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Thu, 28 Nov 2024 18:41:16 -0500 Subject: [PATCH 011/111] Rename `syntax_error` to `set_syntax_error` Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/parser.c | 58 ++++++++++++++----------------- ext/rbs_extension/parserstate.c | 2 +- ext/rbs_extension/rbs_extension.h | 2 +- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index ad34570a3..c1f52abea 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -96,11 +96,7 @@ static rbs_location_t *rbs_location_current_token(parserstate *state) { static bool parse_optional(parserstate *state, rbs_node_t **optional); static bool parse_simple(parserstate *state, rbs_node_t **type); -bool has_error(parserstate *state) { - return state->error != NULL || state->aborted; -} - -void syntax_error(parserstate *state, token tok, const char *fmt, ...) { +void set_syntax_error(parserstate *state, token tok, const char *fmt, ...) { if (state->error) { return; } @@ -143,7 +139,7 @@ void parser_advance_no_gap(parserstate *state) { if (state->current_token.range.end.byte_pos == state->next_token.range.start.byte_pos) { parser_advance(state); } else { - syntax_error(state, state->next_token, "unexpected token"); + set_syntax_error(state, state->next_token, "unexpected token"); } } @@ -222,7 +218,7 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb VALUE string = rb_funcall(ids, rb_intern("join"), 1, rb_str_new_cstr(", ")); - syntax_error(state, state->current_token, "expected one of %s", StringValuePtr(string)); + set_syntax_error(state, state->current_token, "expected one of %s", StringValuePtr(string)); return false; } } @@ -248,7 +244,7 @@ static bool parse_type_list(parserstate *state, enum TokenType eol, rbs_node_lis if (state->next_token.type == eol) { break; } else { - syntax_error(state, state->next_token, "comma delimited type list is expected"); + set_syntax_error(state, state->next_token, "comma delimited type list is expected"); return false; } } @@ -305,7 +301,7 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t }; if (!is_keyword_token(state->current_token.type)) { - syntax_error(state, state->current_token, "unexpected token for function parameter name"); + set_syntax_error(state, state->current_token, "unexpected token for function parameter name"); return false; } @@ -365,7 +361,7 @@ static bool parse_keyword(parserstate *state, rbs_hash_t *keywords, rbs_hash_t * CHECK_PARSE(parse_keyword_key(state, &key)); if (rbs_hash_find(memo, (rbs_node_t *) key)) { - syntax_error(state, state->current_token, "duplicated keyword argument"); + set_syntax_error(state, state->current_token, "duplicated keyword argument"); return false; } else { rbs_hash_set(memo, (rbs_node_t *) key, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, true)); @@ -548,7 +544,7 @@ static bool parse_params(parserstate *state, method_params *params) { if (is_keyword(state)) { CHECK_PARSE(parse_keyword(state, params->optional_keywords, memo)); } else { - syntax_error(state, state->next_token, "optional keyword argument type is expected"); + set_syntax_error(state, state->next_token, "optional keyword argument type is expected"); return false; } break; @@ -570,7 +566,7 @@ static bool parse_params(parserstate *state, method_params *params) { if (is_keyword(state)) { CHECK_PARSE(parse_keyword(state, params->required_keywords, memo)); } else { - syntax_error(state, state->next_token, "required keyword argument type is expected"); + set_syntax_error(state, state->next_token, "required keyword argument type is expected"); return false; } break; @@ -586,7 +582,7 @@ static bool parse_params(parserstate *state, method_params *params) { EOP: if (state->next_token.type != pRPAREN) { - syntax_error(state, state->next_token, "unexpected token for method type parameters"); + set_syntax_error(state, state->next_token, "unexpected token for method type parameters"); return false; } @@ -678,7 +674,7 @@ static bool parse_function(parserstate *state, bool accept_type_binding, parse_f // Untyped method parameter means it cannot have block if (rbs_is_untyped_params(¶ms)) { if (state->next_token.type != pARROW) { - syntax_error(state, state->next_token2, "A method type with untyped method parameter cannot have block"); + set_syntax_error(state, state->next_token2, "A method type with untyped method parameter cannot have block"); return false; } } @@ -785,7 +781,7 @@ static bool parse_proc_type(parserstate *state, rbs_types_proc_t **proc) { static void check_key_duplication(parserstate *state, rbs_hash_t *fields, rbs_node_t *key) { if (rbs_hash_find(fields, ((rbs_node_t *) key))) { - syntax_error(state, state->current_token, "duplicated record key"); + set_syntax_error(state, state->current_token, "duplicated record key"); } } @@ -839,7 +835,7 @@ static bool parse_record_attributes(parserstate *state, rbs_hash_t **fields) { break; } default: - syntax_error(state, state->next_token, "unexpected record key token"); + set_syntax_error(state, state->next_token, "unexpected record key token"); return false; } check_key_duplication(state, *fields, (rbs_node_t *) key); @@ -1182,7 +1178,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { return true; } default: - syntax_error(state, state->current_token, "unexpected token for simple type"); + set_syntax_error(state, state->current_token, "unexpected token for simple type"); return false; } } @@ -1338,7 +1334,7 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa required_param_allowed = false; } else { if (!required_param_allowed) { - syntax_error(state, state->current_token, "required type parameter is not allowed after optional type parameter"); + set_syntax_error(state, state->current_token, "required type parameter is not allowed after optional type parameter"); return false; } } @@ -1664,7 +1660,7 @@ static bool parse_method_name(parserstate *state, range *range, rbs_ast_symbol_t } default: - syntax_error(state, state->current_token, "unexpected token for method name"); + set_syntax_error(state, state->current_token, "unexpected token for method name"); return false; } } @@ -1767,7 +1763,6 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept kind = INSTANCE_KIND; } else { kind = parse_instance_singleton_kind(state, visibility == NULL, &kind_range); - if (has_error(state)) return false; } range name_range; @@ -1777,7 +1772,7 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept #define SELF_ID rbs_constant_pool_insert_constant(&state->constant_pool, (const unsigned char *) "self?", strlen("self?")) if (state->next_token.type == pDOT && name->constant_id == SELF_ID) { - syntax_error(state, state->next_token, "`self?` method cannot have visibility"); + set_syntax_error(state, state->next_token, "`self?` method cannot have visibility"); return false; } else { parser_advance_assert(state, pCOLON); @@ -1822,12 +1817,12 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept member_range.end = overloading_range.end; break; } else { - syntax_error(state, state->next_token, "unexpected overloading method definition"); + set_syntax_error(state, state->next_token, "unexpected overloading method definition"); return false; } default: - syntax_error(state, state->next_token, "unexpected token for method type"); + set_syntax_error(state, state->next_token, "unexpected token for method type"); return false; } @@ -1933,7 +1928,7 @@ static bool parse_mixin_member(parserstate *state, bool from_interface, position if (from_interface) { if (state->current_token.type != kINCLUDE) { - syntax_error(state, state->current_token, "unexpected mixin in interface declaration"); + set_syntax_error(state, state->current_token, "unexpected mixin in interface declaration"); return false; } } @@ -2042,7 +2037,7 @@ static bool parse_alias_member(parserstate *state, bool instance_only, position NODISCARD static bool parse_variable_member(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **variable_member) { if (annotations->length > 0) { - syntax_error(state, state->current_token, "annotation cannot be given to variable members"); + set_syntax_error(state, state->current_token, "annotation cannot be given to variable members"); return false; } @@ -2142,7 +2137,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ NODISCARD static bool parse_visibility_member(parserstate *state, rbs_node_list_t *annotations, rbs_node_t **visibility_member) { if (annotations->length > 0) { - syntax_error(state, state->current_token, "annotation cannot be given to visibility members"); + set_syntax_error(state, state->current_token, "annotation cannot be given to visibility members"); return false; } @@ -2214,7 +2209,6 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs range kind_range; InstanceSingletonKind is_kind = parse_instance_singleton_kind(state, false, &kind_range); - if (has_error(state)) return false; rbs_keyword_t *kind = rbs_keyword_new(&state->allocator, INTERN(((is_kind == INSTANCE_KIND) ? "instance" : "singleton"))); @@ -2324,7 +2318,7 @@ static bool parse_interface_members(parserstate *state, rbs_node_list_t **member } default: - syntax_error(state, state->current_token, "unexpected token for interface declaration member"); + set_syntax_error(state, state->current_token, "unexpected token for interface declaration member"); return false; } @@ -2505,7 +2499,7 @@ static bool parse_module_members(parserstate *state, rbs_node_list_t **members) break; } default: - syntax_error(state, state->next_token, "method or attribute definition is expected after visibility modifier"); + set_syntax_error(state, state->next_token, "method or attribute definition is expected after visibility modifier"); return false; } } else { @@ -2795,7 +2789,7 @@ static bool parse_nested_decl(parserstate *state, const char *nested_in, positio break; } default: - syntax_error(state, state->current_token, "unexpected token for class/module declaration member"); + set_syntax_error(state, state->current_token, "unexpected token for class/module declaration member"); return false; } @@ -2851,7 +2845,7 @@ static bool parse_decl(parserstate *state, rbs_node_t **decl) { return true; } default: - syntax_error(state, state->current_token, "cannot start a declaration"); + set_syntax_error(state, state->current_token, "cannot start a declaration"); return false; } } @@ -2972,7 +2966,7 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { break; } default: - syntax_error(state, state->next_token, "use clause is expected"); + set_syntax_error(state, state->next_token, "use clause is expected"); return false; } diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index d6db856c2..a63267bf5 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -140,7 +140,7 @@ bool parser_advance_if(parserstate *state, enum TokenType type) { void parser_assert(parserstate *state, enum TokenType type) { if (state->current_token.type != type) { - syntax_error( + set_syntax_error( state, state->current_token, "expected a token `%s`", diff --git a/ext/rbs_extension/rbs_extension.h b/ext/rbs_extension/rbs_extension.h index c3d4c5fbc..335f981f1 100644 --- a/ext/rbs_extension/rbs_extension.h +++ b/ext/rbs_extension/rbs_extension.h @@ -22,7 +22,7 @@ * */ VALUE rbs_unquote_string(parserstate *state, range rg, int offset_bytes); -PRINTF_ARGS(void syntax_error(parserstate *state, token tok, const char *fmt, ...), 3, 4); +PRINTF_ARGS(void set_syntax_error(parserstate *state, token tok, const char *fmt, ...), 3, 4); /** * Raises RBS::ParsingError on `tok` with message constructed with given `fmt`. From 57f9a5e3a5013a92f4fe2d574d0bad7c5d588a34 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Thu, 28 Nov 2024 19:05:01 -0500 Subject: [PATCH 012/111] Merge `aborted` and `syntax_error` Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/parser.c | 102 ++++++++++++++---------------- ext/rbs_extension/parserstate.c | 7 +- ext/rbs_extension/parserstate.h | 2 +- ext/rbs_extension/rbs_extension.h | 4 +- 4 files changed, 54 insertions(+), 61 deletions(-) diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index c1f52abea..bec1d0214 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -96,14 +96,14 @@ static rbs_location_t *rbs_location_current_token(parserstate *state) { static bool parse_optional(parserstate *state, rbs_node_t **optional); static bool parse_simple(parserstate *state, rbs_node_t **type); -void set_syntax_error(parserstate *state, token tok, const char *fmt, ...) { +void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt, ...) { if (state->error) { return; } - va_list args; char *message; + va_list args; va_start(args, fmt); vasprintf(&message, fmt, args); va_end(args); @@ -111,9 +111,14 @@ void set_syntax_error(parserstate *state, token tok, const char *fmt, ...) { state->error = (error *)malloc(sizeof(error)); state->error->token = tok; state->error->message = message; + state->error->syntax_error = syntax_error; } -NORETURN(void) raise_syntax_error(parserstate *state, error *error) { +NORETURN(void) raise_error(parserstate *state, error *error) { + if (!error->syntax_error) { + rb_raise(rb_eRuntimeError, "Unexpected error"); + } + VALUE location = rbs_new_location(state->buffer, error->token.range); VALUE type = rb_str_new_cstr(token_type_str(error->token.type)); @@ -139,7 +144,7 @@ void parser_advance_no_gap(parserstate *state) { if (state->current_token.range.end.byte_pos == state->next_token.range.start.byte_pos) { parser_advance(state); } else { - set_syntax_error(state, state->next_token, "unexpected token"); + set_error(state, state->next_token, true, "unexpected token"); } } @@ -218,7 +223,7 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb VALUE string = rb_funcall(ids, rb_intern("join"), 1, rb_str_new_cstr(", ")); - set_syntax_error(state, state->current_token, "expected one of %s", StringValuePtr(string)); + set_error(state, state->current_token, true, "expected one of %s", StringValuePtr(string)); return false; } } @@ -244,7 +249,7 @@ static bool parse_type_list(parserstate *state, enum TokenType eol, rbs_node_lis if (state->next_token.type == eol) { break; } else { - set_syntax_error(state, state->next_token, "comma delimited type list is expected"); + set_error(state, state->next_token, true, "comma delimited type list is expected"); return false; } } @@ -301,7 +306,7 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t }; if (!is_keyword_token(state->current_token.type)) { - set_syntax_error(state, state->current_token, "unexpected token for function parameter name"); + set_error(state, state->current_token, true, "unexpected token for function parameter name"); return false; } @@ -361,7 +366,7 @@ static bool parse_keyword(parserstate *state, rbs_hash_t *keywords, rbs_hash_t * CHECK_PARSE(parse_keyword_key(state, &key)); if (rbs_hash_find(memo, (rbs_node_t *) key)) { - set_syntax_error(state, state->current_token, "duplicated keyword argument"); + set_error(state, state->current_token, true, "duplicated keyword argument"); return false; } else { rbs_hash_set(memo, (rbs_node_t *) key, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, true)); @@ -544,7 +549,7 @@ static bool parse_params(parserstate *state, method_params *params) { if (is_keyword(state)) { CHECK_PARSE(parse_keyword(state, params->optional_keywords, memo)); } else { - set_syntax_error(state, state->next_token, "optional keyword argument type is expected"); + set_error(state, state->next_token, true, "optional keyword argument type is expected"); return false; } break; @@ -566,7 +571,7 @@ static bool parse_params(parserstate *state, method_params *params) { if (is_keyword(state)) { CHECK_PARSE(parse_keyword(state, params->required_keywords, memo)); } else { - set_syntax_error(state, state->next_token, "required keyword argument type is expected"); + set_error(state, state->next_token, true, "required keyword argument type is expected"); return false; } break; @@ -582,7 +587,7 @@ static bool parse_params(parserstate *state, method_params *params) { EOP: if (state->next_token.type != pRPAREN) { - set_syntax_error(state, state->next_token, "unexpected token for method type parameters"); + set_error(state, state->next_token, true, "unexpected token for method type parameters"); return false; } @@ -674,7 +679,7 @@ static bool parse_function(parserstate *state, bool accept_type_binding, parse_f // Untyped method parameter means it cannot have block if (rbs_is_untyped_params(¶ms)) { if (state->next_token.type != pARROW) { - set_syntax_error(state, state->next_token2, "A method type with untyped method parameter cannot have block"); + set_error(state, state->next_token2, true, "A method type with untyped method parameter cannot have block"); return false; } } @@ -781,7 +786,7 @@ static bool parse_proc_type(parserstate *state, rbs_types_proc_t **proc) { static void check_key_duplication(parserstate *state, rbs_hash_t *fields, rbs_node_t *key) { if (rbs_hash_find(fields, ((rbs_node_t *) key))) { - set_syntax_error(state, state->current_token, "duplicated record key"); + set_error(state, state->current_token, true, "duplicated record key"); } } @@ -835,7 +840,7 @@ static bool parse_record_attributes(parserstate *state, rbs_hash_t **fields) { break; } default: - set_syntax_error(state, state->next_token, "unexpected record key token"); + set_error(state, state->next_token, true, "unexpected record key token"); return false; } check_key_duplication(state, *fields, (rbs_node_t *) key); @@ -895,7 +900,7 @@ static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types break; } default: - state->aborted = true; + set_error(state, state->current_token, false, "Unexpected error"); return false; } @@ -930,7 +935,7 @@ static bool parse_instance_type(parserstate *state, bool parse_alias, rbs_node_t } else if (state->current_token.type == tLIDENT) { kind = ALIAS_NAME; } else { - state->aborted = true; + set_error(state, state->current_token, false, "Unexpected error"); return false; } @@ -1178,7 +1183,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { return true; } default: - set_syntax_error(state, state->current_token, "unexpected token for simple type"); + set_error(state, state->current_token, true, "unexpected token for simple type"); return false; } } @@ -1292,7 +1297,7 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa variance = rbs_keyword_new(&state->allocator, INTERN("covariant")); break; default: - state->aborted = true; + set_error(state, state->current_token, false, "Unexpected error"); return false; } @@ -1334,7 +1339,7 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa required_param_allowed = false; } else { if (!required_param_allowed) { - set_syntax_error(state, state->current_token, "required type parameter is not allowed after optional type parameter"); + set_error(state, state->current_token, true, "required type parameter is not allowed after optional type parameter"); return false; } } @@ -1549,7 +1554,7 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati close_char = '|'; break; default: - state->aborted = true; + set_error(state, state->current_token, false, "Unexpected error"); return false; } @@ -1660,7 +1665,7 @@ static bool parse_method_name(parserstate *state, range *range, rbs_ast_symbol_t } default: - set_syntax_error(state, state->current_token, "unexpected token for method name"); + set_error(state, state->current_token, true, "unexpected token for method name"); return false; } } @@ -1772,7 +1777,7 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept #define SELF_ID rbs_constant_pool_insert_constant(&state->constant_pool, (const unsigned char *) "self?", strlen("self?")) if (state->next_token.type == pDOT && name->constant_id == SELF_ID) { - set_syntax_error(state, state->next_token, "`self?` method cannot have visibility"); + set_error(state, state->next_token, true, "`self?` method cannot have visibility"); return false; } else { parser_advance_assert(state, pCOLON); @@ -1817,12 +1822,12 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept member_range.end = overloading_range.end; break; } else { - set_syntax_error(state, state->next_token, "unexpected overloading method definition"); + set_error(state, state->next_token, true, "unexpected overloading method definition"); return false; } default: - set_syntax_error(state, state->next_token, "unexpected token for method type"); + set_error(state, state->next_token, true, "unexpected token for method type"); return false; } @@ -1850,7 +1855,7 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept break; } default: - state->aborted = true; + set_error(state, state->current_token, false, "Unexpected error"); return false; } @@ -1922,13 +1927,13 @@ static bool parse_mixin_member(parserstate *state, bool from_interface, position reset_typevar_scope = false; break; default: - state->aborted = true; + set_error(state, state->current_token, false, "Unexpected error"); return false; } if (from_interface) { if (state->current_token.type != kINCLUDE) { - set_syntax_error(state, state->current_token, "unexpected mixin in interface declaration"); + set_error(state, state->current_token, true, "unexpected mixin in interface declaration"); return false; } } @@ -1968,7 +1973,7 @@ static bool parse_mixin_member(parserstate *state, bool from_interface, position *mixin_member = (rbs_node_t *) rbs_ast_members_prepend_new(&state->allocator, name, args, annotations, loc, comment); return true; default: - state->aborted = true; + set_error(state, state->current_token, false, "Unexpected error"); return false; } } @@ -2037,7 +2042,7 @@ static bool parse_alias_member(parserstate *state, bool instance_only, position NODISCARD static bool parse_variable_member(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **variable_member) { if (annotations->length > 0) { - set_syntax_error(state, state->current_token, "annotation cannot be given to variable members"); + set_error(state, state->current_token, true, "annotation cannot be given to variable members"); return false; } @@ -2125,7 +2130,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ return true; } default: - state->aborted = true; + set_error(state, state->current_token, false, "Unexpected error"); return false; } } @@ -2137,7 +2142,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ NODISCARD static bool parse_visibility_member(parserstate *state, rbs_node_list_t *annotations, rbs_node_t **visibility_member) { if (annotations->length > 0) { - set_syntax_error(state, state->current_token, "annotation cannot be given to visibility members"); + set_error(state, state->current_token, true, "annotation cannot be given to visibility members"); return false; } @@ -2153,11 +2158,10 @@ static bool parse_visibility_member(parserstate *state, rbs_node_list_t *annotat *visibility_member = (rbs_node_t *) rbs_ast_members_private_new(&state->allocator, location); return true; } - default: { - state->aborted = true; + default: + set_error(state, state->current_token, false, "Unexpected error"); return false; } - } } /* @@ -2271,7 +2275,7 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs *attribute_member = (rbs_node_t *) rbs_ast_members_attraccessor_new(&state->allocator, attr_name, type, ivar_name, kind, annotations, loc, comment, visibility); return true; default: - state->aborted = true; + set_error(state, state->current_token, false, "Unexpected error"); return false; } } @@ -2318,7 +2322,7 @@ static bool parse_interface_members(parserstate *state, rbs_node_list_t **member } default: - set_syntax_error(state, state->current_token, "unexpected token for interface declaration member"); + set_error(state, state->current_token, true, "unexpected token for interface declaration member"); return false; } @@ -2499,7 +2503,7 @@ static bool parse_module_members(parserstate *state, rbs_node_list_t **members) break; } default: - set_syntax_error(state, state->next_token, "method or attribute definition is expected after visibility modifier"); + set_error(state, state->next_token, true, "method or attribute definition is expected after visibility modifier"); return false; } } else { @@ -2789,7 +2793,7 @@ static bool parse_nested_decl(parserstate *state, const char *nested_in, positio break; } default: - set_syntax_error(state, state->current_token, "unexpected token for class/module declaration member"); + set_error(state, state->current_token, true, "unexpected token for class/module declaration member"); return false; } @@ -2845,7 +2849,7 @@ static bool parse_decl(parserstate *state, rbs_node_t **decl) { return true; } default: - set_syntax_error(state, state->current_token, "cannot start a declaration"); + set_error(state, state->current_token, true, "cannot start a declaration"); return false; } } @@ -2966,7 +2970,7 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { break; } default: - set_syntax_error(state, state->next_token, "use clause is expected"); + set_error(state, state->next_token, true, "use clause is expected"); return false; } @@ -3054,12 +3058,8 @@ parse_type_try(VALUE a) { rbs_node_t *type; parse_type(parser, &type); - if (parser->aborted) { - rb_raise(rb_eRuntimeError, "Unexpected error"); - } - if (parser->error) { - raise_syntax_error(parser, parser->error); + raise_error(parser, parser->error); } if (RB_TEST(arg->require_eof)) { @@ -3098,12 +3098,8 @@ parse_method_type_try(VALUE a) { rbs_methodtype_t *method_type = NULL; parse_method_type(parser, &method_type); - if (parser->aborted) { - rb_raise(rb_eRuntimeError, "Unexpected error"); - } - if (parser->error) { - raise_syntax_error(parser, parser->error); + raise_error(parser, parser->error); } if (RB_TEST(arg->require_eof)) { @@ -3137,12 +3133,8 @@ parse_signature_try(VALUE a) { rbs_signature_t *signature = NULL; parse_signature(parser, &signature); - if (parser->aborted) { - rb_raise(rb_eRuntimeError, "Unexpected error"); - } - if (parser->error) { - raise_syntax_error(parser, parser->error); + raise_error(parser, parser->error); } rbs_translation_context_t ctx = { diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index a63267bf5..75128a776 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -140,13 +140,14 @@ bool parser_advance_if(parserstate *state, enum TokenType type) { void parser_assert(parserstate *state, enum TokenType type) { if (state->current_token.type != type) { - set_syntax_error( + set_error( state, state->current_token, + true, "expected a token `%s`", token_type_str(type) ); - raise_syntax_error(state, state->error); + raise_error(state, state->error); } } @@ -327,7 +328,7 @@ parserstate *alloc_parser(VALUE buffer, VALUE string, int start_pos, int end_pos .constant_pool = {}, .allocator = allocator, - .aborted = false, + .error = NULL, }; // The parser's constant pool is mainly used for storing the names of type variables, which usually aren't many. diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index f5fbf7bce..7f7677704 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -47,6 +47,7 @@ typedef struct comment { typedef struct error { char *message; token token; + bool syntax_error; } error; /** @@ -66,7 +67,6 @@ typedef struct { rbs_constant_pool_t constant_pool; rbs_allocator_t allocator; - bool aborted; error *error; } parserstate; diff --git a/ext/rbs_extension/rbs_extension.h b/ext/rbs_extension/rbs_extension.h index 335f981f1..1279317ba 100644 --- a/ext/rbs_extension/rbs_extension.h +++ b/ext/rbs_extension/rbs_extension.h @@ -22,7 +22,7 @@ * */ VALUE rbs_unquote_string(parserstate *state, range rg, int offset_bytes); -PRINTF_ARGS(void set_syntax_error(parserstate *state, token tok, const char *fmt, ...), 3, 4); +PRINTF_ARGS(void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt, ...), 4, 5); /** * Raises RBS::ParsingError on `tok` with message constructed with given `fmt`. @@ -31,4 +31,4 @@ PRINTF_ARGS(void set_syntax_error(parserstate *state, token tok, const char *fmt * foo.rbs:11:21...11:25: Syntax error: {message}, token=`{tok source}` ({tok type}) * ``` * */ -NORETURN(void) raise_syntax_error(parserstate *state, error *error); +NORETURN(void) raise_error(parserstate *state, error *error); From 0fab17e2dc28a42cb4141a4d76f8aa6d7fe0cd26 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Fri, 29 Nov 2024 13:20:36 -0500 Subject: [PATCH 013/111] Introduce `rbs_buffer` Signed-off-by: Alexandre Terrasa Do not create comments using a VALUE Use a rbs_string instead Signed-off-by: Alexandre Terrasa --- config.yml | 1 + ext/rbs_extension/ast_translation.c | 2 +- ext/rbs_extension/parserstate.c | 69 +++++++++++----------- ext/rbs_extension/parserstate.h | 1 - include/rbs/ast.h | 4 +- include/rbs/rbs_buffer.h | 89 +++++++++++++++++++++++++++++ src/ast.c | 3 +- src/rbs_buffer.c | 64 +++++++++++++++++++++ 8 files changed, 195 insertions(+), 38 deletions(-) create mode 100644 include/rbs/rbs_buffer.h create mode 100644 src/rbs_buffer.c diff --git a/config.yml b/config.yml index b3007f4e8..49c1d3419 100644 --- a/config.yml +++ b/config.yml @@ -13,6 +13,7 @@ nodes: - name: RBS::AST::Comment fields: - name: string + c_type: rbs_string - name: location c_type: rbs_location - name: RBS::AST::Declarations::Class diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index 6bad63482..f795b9a19 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -77,7 +77,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rbs_ast_comment_t *node = (rbs_ast_comment_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("string")), node->string); + rb_hash_aset(h, ID2SYM(rb_intern("string")), rbs_string_to_ruby_string(&node->string)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index 75128a776..63461391a 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -1,5 +1,8 @@ #include "rbs_extension.h" +#include "rbs_string_bridging.h" +#include "rbs/rbs_buffer.h" + #define RESET_TABLE_P(table) (table->size == 0) id_table *alloc_empty_table(rbs_allocator_t *allocator) { @@ -177,13 +180,46 @@ void insert_comment_line(parserstate *state, token tok) { } } +static rbs_ast_comment_t *parse_comment_lines(rbs_allocator_t *allocator, comment *com, VALUE buffer) { + VALUE content = rb_funcall(buffer, rb_intern("content"), 0); + rb_encoding *enc = rb_enc_get(content); + + int hash_bytes = rb_enc_codelen('#', enc); + int space_bytes = rb_enc_codelen(' ', enc); + + rbs_buffer_t rbs_buffer; + rbs_buffer_init(&rbs_buffer); + + for (size_t i = 0; i < com->line_count; i++) { + token tok = com->tokens[i]; + + char *comment_start = RSTRING_PTR(content) + tok.range.start.byte_pos + hash_bytes; + int comment_bytes = RANGE_BYTES(tok.range) - hash_bytes; + unsigned char c = rb_enc_mbc_to_codepoint(comment_start, RSTRING_END(content), enc); + + if (c == ' ') { + comment_start += space_bytes; + comment_bytes -= space_bytes; + } + + rbs_buffer_append_string(&rbs_buffer, comment_start, comment_bytes); + rbs_buffer_append_cstr(&rbs_buffer, "\n"); + } + + return rbs_ast_comment_new( + allocator, + rbs_buffer_to_string(&rbs_buffer), + rbs_location_pp(buffer, &com->start, &com->end) + ); +} + rbs_ast_comment_t *get_comment(parserstate *state, int subject_line) { int comment_line = subject_line - 1; comment *com = comment_get_comment(state->last_comment, comment_line); if (com) { - return comment_to_ruby(&state->allocator, com, state->buffer); + return parse_comment_lines(&state->allocator, com, state->buffer); } else { return NULL; } @@ -245,37 +281,6 @@ comment *comment_get_comment(comment *com, int line) { return comment_get_comment(com->next_comment, line); } -rbs_ast_comment_t *comment_to_ruby(rbs_allocator_t *allocator, comment *com, VALUE buffer) { - VALUE content = rb_funcall(buffer, rb_intern("content"), 0); - rb_encoding *enc = rb_enc_get(content); - VALUE string = rb_enc_str_new_cstr("", enc); - - int hash_bytes = rb_enc_codelen('#', enc); - int space_bytes = rb_enc_codelen(' ', enc); - - for (size_t i = 0; i < com->line_count; i++) { - token tok = com->tokens[i]; - - char *comment_start = RSTRING_PTR(content) + tok.range.start.byte_pos + hash_bytes; - int comment_bytes = RANGE_BYTES(tok.range) - hash_bytes; - unsigned char c = rb_enc_mbc_to_codepoint(comment_start, RSTRING_END(content), enc); - - if (c == ' ') { - comment_start += space_bytes; - comment_bytes -= space_bytes; - } - - rb_str_cat(string, comment_start, comment_bytes); - rb_str_cat_cstr(string, "\n"); - } - - return rbs_ast_comment_new( - allocator, - string, - rbs_location_pp(buffer, &com->start, &com->end) - ); -} - lexstate *alloc_lexer(rbs_allocator_t *allocator, VALUE string, int start_pos, int end_pos) { if (start_pos < 0 || end_pos < 0) { rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos); diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index 7f7677704..b27ba64ca 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -73,7 +73,6 @@ typedef struct { comment *alloc_comment(rbs_allocator_t *, token comment_token, comment *last_comment); void comment_insert_new_line(rbs_allocator_t *, comment *com, token comment_token); comment *comment_get_comment(comment *com, int line); -rbs_ast_comment_t *comment_to_ruby(rbs_allocator_t *, comment *com, VALUE buffer); /** * Insert new table entry. diff --git a/include/rbs/ast.h b/include/rbs/ast.h index 1f38764c9..6b36c7d29 100644 --- a/include/rbs/ast.h +++ b/include/rbs/ast.h @@ -151,7 +151,7 @@ typedef struct rbs_ast_bool { typedef struct rbs_ast_comment { rbs_node_t base; - VALUE string; + rbs_string_t string; struct rbs_location *location; } rbs_ast_comment_t; @@ -682,7 +682,7 @@ rbs_other_ruby_value_t *rbs_other_ruby_value_new(VALUE ruby_value); rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_string_t string, rbs_location_t *location); rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, bool value); -rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, VALUE string, rbs_location_t *location); +rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, rbs_string_t string, rbs_location_t *location); rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, rbs_ast_comment_t *comment); diff --git a/include/rbs/rbs_buffer.h b/include/rbs/rbs_buffer.h new file mode 100644 index 000000000..ba85fba4b --- /dev/null +++ b/include/rbs/rbs_buffer.h @@ -0,0 +1,89 @@ +#ifndef RBS__RBS_BUFFER_H +#define RBS__RBS_BUFFER_H + +#include +#include +#include + +#include "rbs/rbs_string.h" + +/** + * A rbs_buffer_t is a simple memory buffer that stores data in a contiguous block of memory. + */ +typedef struct { + /** The length of the buffer in bytes. */ + size_t length; + + /** The capacity of the buffer in bytes that has been allocated. */ + size_t capacity; + + /** A pointer to the start of the buffer. */ + char *value; +} rbs_buffer_t; + +/** + * Initialize a rbs_buffer_t with the given capacity. + * + * @param buffer The buffer to initialize. + * @param capacity The capacity of the buffer. + * @returns True if the buffer was initialized successfully, false otherwise. + */ +bool rbs_buffer_init_capacity(rbs_buffer_t *buffer, size_t capacity); + +/** + * Initialize a rbs_buffer_t with its default values. + * + * @param buffer The buffer to initialize. + * @returns True if the buffer was initialized successfully, false otherwise. + */ +bool rbs_buffer_init(rbs_buffer_t *buffer); + +/** + * Return the value of the buffer. + * + * @param buffer The buffer to get the value of. + * @returns The value of the buffer. + */ +char *rbs_buffer_value(const rbs_buffer_t *buffer); + +/** + * Return the length of the buffer. + * + * @param buffer The buffer to get the length of. + * @returns The length of the buffer. + */ +size_t rbs_buffer_length(const rbs_buffer_t *buffer); + +/** + * Append a C string to the buffer. + * + * @param buffer The buffer to append to. + * @param value The C string to append. + */ +void rbs_buffer_append_cstr(rbs_buffer_t *buffer, const char *value); + +/** + * Append a string to the buffer. + * + * @param buffer The buffer to append to. + * @param value The string to append. + * @param length The length of the string to append. + */ +void rbs_buffer_append_string(rbs_buffer_t *buffer, const char *value, size_t length); + +/** + * Convert the buffer to a rbs_string_t. + * + * @param buffer The buffer to convert. + * @returns The converted rbs_string_t. + */ +rbs_string_t rbs_buffer_to_string(rbs_buffer_t *buffer); + +/** + * Free the memory associated with the buffer. + * + * @param buffer The buffer to free. + */ +void pm_buffer_free(rbs_buffer_t *buffer); + +#endif diff --git a/src/ast.c b/src/ast.c index 0fe0775be..dffe41fb5 100644 --- a/src/ast.c +++ b/src/ast.c @@ -185,10 +185,9 @@ rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, bool value) { return instance; } -rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, VALUE string, rbs_location_t *location) { +rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, rbs_string_t string, rbs_location_t *location) { rbs_ast_comment_t *instance = rbs_allocator_alloc(allocator, rbs_ast_comment_t); - rb_gc_register_mark_object(string); *instance = (rbs_ast_comment_t) { .base = (rbs_node_t) { diff --git a/src/rbs_buffer.c b/src/rbs_buffer.c new file mode 100644 index 000000000..43a894a74 --- /dev/null +++ b/src/rbs_buffer.c @@ -0,0 +1,64 @@ +#include "rbs/rbs_buffer.h" + +bool rbs_buffer_init_capacity(rbs_buffer_t *buffer, size_t capacity) { + buffer->length = 0; + buffer->capacity = capacity; + + buffer->value = (char *) malloc(capacity); + return buffer->value != NULL; +} + +bool rbs_buffer_init(rbs_buffer_t *buffer) { + return rbs_buffer_init_capacity(buffer, 1024); +} + +char *rbs_buffer_value(const rbs_buffer_t *buffer) { + return buffer->value; +} + +size_t rbs_buffer_length(const rbs_buffer_t *buffer) { + return buffer->length; +} + +static inline bool rbs_buffer_append_length(rbs_buffer_t *buffer, size_t length) { + size_t next_length = buffer->length + length; + + if (next_length > buffer->capacity) { + if (buffer->capacity == 0) { + buffer->capacity = 1; + } + + while (next_length > buffer->capacity) { + buffer->capacity *= 2; + } + + buffer->value = realloc(buffer->value, buffer->capacity); + if (buffer->value == NULL) return false; + } + + buffer->length = next_length; + return true; +} + +static inline void rbs_buffer_append(rbs_buffer_t *buffer, const void *source, size_t length) { + size_t cursor = buffer->length; + if (rbs_buffer_append_length(buffer, length)) { + memcpy(buffer->value + cursor, source, length); + } +} + +void rbs_buffer_append_cstr(rbs_buffer_t *buffer, const char *value) { + rbs_buffer_append(buffer, value, strlen(value)); +} + +void rbs_buffer_append_string(rbs_buffer_t *buffer, const char *value, size_t length) { + rbs_buffer_append(buffer, value, length); +} + +rbs_string_t rbs_buffer_to_string(rbs_buffer_t *buffer) { + return rbs_string_owned_new(buffer->value, buffer->value + buffer->length); +} + +void rbs_buffer_free(rbs_buffer_t *buffer) { + free(buffer->value); +} From 547bb0bc71901eb41516a56f9644fb8f3b0be74c Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Fri, 29 Nov 2024 13:49:39 -0500 Subject: [PATCH 014/111] Move extension parsing functions out of `parser.c` Signed-off-by: Alexandre Terrasa --- .idea/.gitignore | 8 + .idea/misc.xml | 4 + .idea/modules.xml | 8 + .idea/rbs.iml | 342 ++++++++++++++++++++++++++++++ .idea/vcs.xml | 6 + ext/rbs_extension/main.c | 166 +++++++++++++++ ext/rbs_extension/parser.c | 174 --------------- ext/rbs_extension/parser.h | 5 +- ext/rbs_extension/rbs_extension.h | 7 +- 9 files changed, 540 insertions(+), 180 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/rbs.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 000000000..13566b81b --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..378540077 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000..40be3710b --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/rbs.iml b/.idea/rbs.iml new file mode 100644 index 000000000..e15d86f08 --- /dev/null +++ b/.idea/rbs.iml @@ -0,0 +1,342 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..35eb1ddfb --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index a59a3f2e9..c1e5479a3 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -1,8 +1,174 @@ #include "rbs_extension.h" #include "rbs/util/rbs_allocator.h" #include "rbs/util/rbs_constant_pool.h" +#include "ast_translation.h" + #include "ruby/vm.h" +NORETURN(void) raise_error(parserstate *state, error *error) { + if (!error->syntax_error) { + rb_raise(rb_eRuntimeError, "Unexpected error"); + } + + VALUE location = rbs_new_location(state->buffer, error->token.range); + VALUE type = rb_str_new_cstr(token_type_str(error->token.type)); + + VALUE rb_error = rb_funcall( + RBS_ParsingError, + rb_intern("new"), + 3, + location, + rb_str_new_cstr(error->message), + type + ); + + rb_exc_raise(rb_error); +} + +struct parse_type_arg { + parserstate *parser; + VALUE require_eof; +}; + +static VALUE ensure_free_parser(VALUE parser) { + free_parser((parserstate *)parser); + return Qnil; +} + +static VALUE parse_type_try(VALUE a) { + struct parse_type_arg *arg = (struct parse_type_arg *)a; + parserstate *parser = arg->parser; + + if (parser->next_token.type == pEOF) { + return Qnil; + } + + rbs_node_t *type; + parse_type(parser, &type); + + if (parser->error) { + raise_error(parser, parser->error); + } + + if (RB_TEST(arg->require_eof)) { + parser_advance_assert(parser, pEOF); + } + + rbs_translation_context_t ctx = { + .constant_pool = &parser->constant_pool, + }; + + return rbs_struct_to_ruby_value(ctx, type); +} + +static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) { + VALUE string = rb_funcall(buffer, rb_intern("content"), 0); + StringValue(string); + parserstate *parser = alloc_parser(buffer, string, FIX2INT(start_pos), FIX2INT(end_pos), variables); + struct parse_type_arg arg = { + parser, + require_eof + }; + return rb_ensure(parse_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser); +} + +static VALUE parse_method_type_try(VALUE a) { + struct parse_type_arg *arg = (struct parse_type_arg *)a; + parserstate *parser = arg->parser; + + if (parser->next_token.type == pEOF) { + return Qnil; + } + + rbs_methodtype_t *method_type = NULL; + parse_method_type(parser, &method_type); + + if (parser->error) { + raise_error(parser, parser->error); + } + + if (RB_TEST(arg->require_eof)) { + parser_advance_assert(parser, pEOF); + } + + rbs_translation_context_t ctx = { + .constant_pool = &parser->constant_pool, + }; + + return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type); +} + +static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) { + VALUE string = rb_funcall(buffer, rb_intern("content"), 0); + StringValue(string); + parserstate *parser = alloc_parser(buffer, string, FIX2INT(start_pos), FIX2INT(end_pos), variables); + struct parse_type_arg arg = { + parser, + require_eof + }; + return rb_ensure(parse_method_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser); +} + +static VALUE parse_signature_try(VALUE a) { + parserstate *parser = (parserstate *)a; + + rbs_signature_t *signature = NULL; + parse_signature(parser, &signature); + + if (parser->error) { + raise_error(parser, parser->error); + } + + rbs_translation_context_t ctx = { + .constant_pool = &parser->constant_pool, + }; + + return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature); +} + +static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) { + VALUE string = rb_funcall(buffer, rb_intern("content"), 0); + StringValue(string); + parserstate *parser = alloc_parser(buffer, string, FIX2INT(start_pos), FIX2INT(end_pos), Qnil); + return rb_ensure(parse_signature_try, (VALUE)parser, ensure_free_parser, (VALUE)parser); +} + +static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) { + VALUE string = rb_funcall(buffer, rb_intern("content"), 0); + StringValue(string); + + + rbs_allocator_t allocator; + rbs_allocator_init(&allocator); + lexstate *lexer = alloc_lexer(&allocator, string, 0, FIX2INT(end_pos)); + + VALUE results = rb_ary_new(); + token token = NullToken; + while (token.type != pEOF) { + token = rbsparser_next_token(lexer); + VALUE type = ID2SYM(rb_intern(token_type_str(token.type))); + VALUE location = rbs_new_location(buffer, token.range); + VALUE pair = rb_ary_new3(2, type, location); + rb_ary_push(results, pair); + } + + rbs_allocator_free(&allocator); + + return results; +} + +void rbs__init_parser(void) { + RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject); + rb_gc_register_mark_object(RBS_Parser); + VALUE empty_array = rb_obj_freeze(rb_ary_new()); + rb_gc_register_mark_object(empty_array); + + rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 5); + rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5); + rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 3); + rb_define_singleton_method(RBS_Parser, "_lex", rbsparser_lex, 2); +} + static void Deinit_rbs_extension(ruby_vm_t *_) { rbs_constant_pool_free(RBS_GLOBAL_CONSTANT_POOL); diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index bec1d0214..106b4efa4 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -114,26 +114,6 @@ void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt state->error->syntax_error = syntax_error; } -NORETURN(void) raise_error(parserstate *state, error *error) { - if (!error->syntax_error) { - rb_raise(rb_eRuntimeError, "Unexpected error"); - } - - VALUE location = rbs_new_location(state->buffer, error->token.range); - VALUE type = rb_str_new_cstr(token_type_str(error->token.type)); - - VALUE rb_error = rb_funcall( - RBS_ParsingError, - rb_intern("new"), - 3, - location, - rb_str_new_cstr(error->message), - type - ); - - rb_exc_raise(rb_error); -} - typedef enum { CLASS_NAME = 1, INTERFACE_NAME = 2, @@ -3034,157 +3014,3 @@ bool parse_signature(parserstate *state, rbs_signature_t **signature) { *signature = rbs_signature_new(&state->allocator, dirs, decls); return true; } - -struct parse_type_arg { - parserstate *parser; - VALUE require_eof; -}; - -static VALUE -ensure_free_parser(VALUE parser) { - free_parser((parserstate *)parser); - return Qnil; -} - -static VALUE -parse_type_try(VALUE a) { - struct parse_type_arg *arg = (struct parse_type_arg *)a; - parserstate *parser = arg->parser; - - if (parser->next_token.type == pEOF) { - return Qnil; - } - - rbs_node_t *type; - parse_type(parser, &type); - - if (parser->error) { - raise_error(parser, parser->error); - } - - if (RB_TEST(arg->require_eof)) { - parser_advance_assert(parser, pEOF); - } - - rbs_translation_context_t ctx = { - .constant_pool = &parser->constant_pool, - }; - - return rbs_struct_to_ruby_value(ctx, type); -} - -static VALUE -rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) -{ - VALUE string = rb_funcall(buffer, rb_intern("content"), 0); - StringValue(string); - parserstate *parser = alloc_parser(buffer, string, FIX2INT(start_pos), FIX2INT(end_pos), variables); - struct parse_type_arg arg = { - parser, - require_eof - }; - return rb_ensure(parse_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser); -} - -static VALUE -parse_method_type_try(VALUE a) { - struct parse_type_arg *arg = (struct parse_type_arg *)a; - parserstate *parser = arg->parser; - - if (parser->next_token.type == pEOF) { - return Qnil; - } - - rbs_methodtype_t *method_type = NULL; - parse_method_type(parser, &method_type); - - if (parser->error) { - raise_error(parser, parser->error); - } - - if (RB_TEST(arg->require_eof)) { - parser_advance_assert(parser, pEOF); - } - - rbs_translation_context_t ctx = { - .constant_pool = &parser->constant_pool, - }; - - return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type); -} - -static VALUE -rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) -{ - VALUE string = rb_funcall(buffer, rb_intern("content"), 0); - StringValue(string); - parserstate *parser = alloc_parser(buffer, string, FIX2INT(start_pos), FIX2INT(end_pos), variables); - struct parse_type_arg arg = { - parser, - require_eof - }; - return rb_ensure(parse_method_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser); -} - -static VALUE -parse_signature_try(VALUE a) { - parserstate *parser = (parserstate *)a; - - rbs_signature_t *signature = NULL; - parse_signature(parser, &signature); - - if (parser->error) { - raise_error(parser, parser->error); - } - - rbs_translation_context_t ctx = { - .constant_pool = &parser->constant_pool, - }; - - return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature); -} - -static VALUE -rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) -{ - VALUE string = rb_funcall(buffer, rb_intern("content"), 0); - StringValue(string); - parserstate *parser = alloc_parser(buffer, string, FIX2INT(start_pos), FIX2INT(end_pos), Qnil); - return rb_ensure(parse_signature_try, (VALUE)parser, ensure_free_parser, (VALUE)parser); -} - -static VALUE -rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) { - VALUE string = rb_funcall(buffer, rb_intern("content"), 0); - StringValue(string); - - rbs_allocator_t allocator; - rbs_allocator_init(&allocator); - lexstate *lexer = alloc_lexer(&allocator, string, 0, FIX2INT(end_pos)); - - VALUE results = rb_ary_new(); - token token = NullToken; - while (token.type != pEOF) { - token = rbsparser_next_token(lexer); - VALUE type = ID2SYM(rb_intern(token_type_str(token.type))); - VALUE location = rbs_new_location(buffer, token.range); - VALUE pair = rb_ary_new3(2, type, location); - rb_ary_push(results, pair); - } - - rbs_allocator_free(&allocator); - - return results; -} - -void rbs__init_parser(void) { - RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject); - rb_gc_register_mark_object(RBS_Parser); - VALUE empty_array = rb_obj_freeze(rb_ary_new()); - rb_gc_register_mark_object(empty_array); - - rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 5); - rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5); - rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 3); - rb_define_singleton_method(RBS_Parser, "_lex", rbsparser_lex, 2); -} diff --git a/ext/rbs_extension/parser.h b/ext/rbs_extension/parser.h index d8ab8bc73..396786b0a 100644 --- a/ext/rbs_extension/parser.h +++ b/ext/rbs_extension/parser.h @@ -4,10 +4,7 @@ #include "ruby.h" #include "parserstate.h" -/** - * RBS::Parser class - * */ -extern VALUE RBS_Parser; +PRINTF_ARGS(void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt, ...), 4, 5); bool parse_type(parserstate *state, rbs_node_t **type); bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type); diff --git a/ext/rbs_extension/rbs_extension.h b/ext/rbs_extension/rbs_extension.h index 1279317ba..70b6d1dbd 100644 --- a/ext/rbs_extension/rbs_extension.h +++ b/ext/rbs_extension/rbs_extension.h @@ -9,6 +9,11 @@ #include "lexer.h" #include "parser.h" +/** + * RBS::Parser class + * */ +extern VALUE RBS_Parser; + /** * Receives `parserstate` and `range`, which represents a string token or symbol token, and returns a string VALUE. * @@ -22,8 +27,6 @@ * */ VALUE rbs_unquote_string(parserstate *state, range rg, int offset_bytes); -PRINTF_ARGS(void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt, ...), 4, 5); - /** * Raises RBS::ParsingError on `tok` with message constructed with given `fmt`. * From eecf8242419c5abe467d9a51f5fcc876051d2458 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Fri, 29 Nov 2024 13:56:21 -0500 Subject: [PATCH 015/111] Remove a few more `ruby.h` Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/parser.h | 1 - include/rbs/ast.h | 1 - templates/include/rbs/ast.h.erb | 1 - 3 files changed, 3 deletions(-) diff --git a/ext/rbs_extension/parser.h b/ext/rbs_extension/parser.h index 396786b0a..2bf48f347 100644 --- a/ext/rbs_extension/parser.h +++ b/ext/rbs_extension/parser.h @@ -1,7 +1,6 @@ #ifndef RBS__PARSER_H #define RBS__PARSER_H -#include "ruby.h" #include "parserstate.h" PRINTF_ARGS(void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt, ...), 4, 5); diff --git a/include/rbs/ast.h b/include/rbs/ast.h index 6b36c7d29..7ee363993 100644 --- a/include/rbs/ast.h +++ b/include/rbs/ast.h @@ -8,7 +8,6 @@ #ifndef RBS__AST_H #define RBS__AST_H -#include "ruby.h" #include "rbs/util/rbs_allocator.h" #include "rbs/util/rbs_constant_pool.h" #include "rbs_string.h" diff --git a/templates/include/rbs/ast.h.erb b/templates/include/rbs/ast.h.erb index 33e55c036..34825edb6 100644 --- a/templates/include/rbs/ast.h.erb +++ b/templates/include/rbs/ast.h.erb @@ -1,7 +1,6 @@ #ifndef RBS__AST_H #define RBS__AST_H -#include "ruby.h" #include "rbs/util/rbs_allocator.h" #include "rbs/util/rbs_constant_pool.h" #include "rbs_string.h" From ae31edf697b0377ab49fdd08c7ac1f2c72302411 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Fri, 29 Nov 2024 15:38:45 -0500 Subject: [PATCH 016/111] Use a bare `char *` for type_name error Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/parser.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index 106b4efa4..4dc419528 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -190,20 +190,18 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb } error: { - VALUE ids = rb_ary_new(); + const char *ids; if (kind & ALIAS_NAME) { - rb_ary_push(ids, rb_str_new_literal("alias name")); + ids = "alias name"; } if (kind & INTERFACE_NAME) { - rb_ary_push(ids, rb_str_new_literal("interface name")); + ids = "interface name"; } if (kind & CLASS_NAME) { - rb_ary_push(ids, rb_str_new_literal("class/module/constant name")); + ids = "class/module/constant name"; } - VALUE string = rb_funcall(ids, rb_intern("join"), 1, rb_str_new_cstr(", ")); - - set_error(state, state->current_token, true, "expected one of %s", StringValuePtr(string)); + set_error(state, state->current_token, true, "expected one of %s", ids); return false; } } From 52f201e0f18a52240ab03d2b12c14bdcf7748034 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 29 Nov 2024 13:42:02 -0500 Subject: [PATCH 017/111] Add `rbs_translation_context_t` to `rbs_loc_to_ruby_location` --- ext/rbs_extension/ast_translation.c | 106 +++++++++--------- .../ext/rbs_extension/ast_translation.c.erb | 4 +- 2 files changed, 55 insertions(+), 55 deletions(-) diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index f795b9a19..9999b4129 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -35,7 +35,7 @@ VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t ctx, rbs_hash_t *rbs_hash) return ruby_hash; } -VALUE rbs_loc_to_ruby_location(rbs_location_t *loc) { +VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *loc) { return loc->cached_ruby_value; } @@ -59,7 +59,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("string")), rbs_string_to_ruby_string(&node->string)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -78,7 +78,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("string")), rbs_string_to_ruby_string(&node->string)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -97,7 +97,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_hash_aset(h, ID2SYM(rb_intern("super_class")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->super_class)); // rbs_ast_declarations_class_super rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_funcall( @@ -120,7 +120,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -136,7 +136,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -153,7 +153,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -170,7 +170,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -189,7 +189,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_funcall( @@ -215,7 +215,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_hash_aset(h, ID2SYM(rb_intern("self_types")), rbs_node_list_to_ruby_array(ctx, node->self_types)); rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_funcall( @@ -238,7 +238,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -254,7 +254,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -273,7 +273,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_funcall( @@ -295,7 +295,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("clauses")), rbs_node_list_to_ruby_array(ctx, node->clauses)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -311,7 +311,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("type_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type_name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -326,7 +326,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->namespace)); // rbs_namespace - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -353,7 +353,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -373,7 +373,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword @@ -394,7 +394,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword @@ -415,7 +415,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword @@ -433,7 +433,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -450,7 +450,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -468,7 +468,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -486,7 +486,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -503,7 +503,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -522,7 +522,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword rb_hash_aset(h, ID2SYM(rb_intern("overloads")), rbs_node_list_to_ruby_array(ctx, node->overloads)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_hash_aset(h, ID2SYM(rb_intern("overloading")), node->overloading ? Qtrue : Qfalse); rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword @@ -557,7 +557,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -572,7 +572,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rbs_ast_members_private_t *node = (rbs_ast_members_private_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -586,7 +586,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rbs_ast_members_public_t *node = (rbs_ast_members_public_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -612,7 +612,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_hash_aset(h, ID2SYM(rb_intern("upper_bound")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->upper_bound)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("default_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->default_type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("unchecked")), node->unchecked ? Qtrue : Qfalse); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -629,7 +629,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("block")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->block)); // rbs_types_block - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_funcall( RBS_AST_TypeParam, @@ -689,7 +689,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -704,7 +704,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("todo")), node->todo ? Qtrue : Qfalse); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -718,7 +718,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rbs_types_bases_bool_t *node = (rbs_types_bases_bool_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -732,7 +732,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rbs_types_bases_bottom_t *node = (rbs_types_bases_bottom_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -746,7 +746,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rbs_types_bases_class_t *node = (rbs_types_bases_class_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -760,7 +760,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rbs_types_bases_instance_t *node = (rbs_types_bases_instance_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -774,7 +774,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rbs_types_bases_nil_t *node = (rbs_types_bases_nil_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -788,7 +788,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rbs_types_bases_self_t *node = (rbs_types_bases_self_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -802,7 +802,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rbs_types_bases_top_t *node = (rbs_types_bases_top_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -816,7 +816,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rbs_types_bases_void_t *node = (rbs_types_bases_void_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -848,7 +848,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -863,7 +863,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -900,7 +900,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -916,7 +916,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -931,7 +931,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(ctx, node->types)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -946,7 +946,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("literal")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->literal)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -961,7 +961,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -977,7 +977,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("block")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->block)); // rbs_types_block - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("self_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->self_type)); // rbs_node @@ -993,7 +993,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("all_fields")), rbs_hash_to_ruby_hash(ctx, node->all_fields)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -1017,7 +1017,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(ctx, node->types)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -1032,7 +1032,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(ctx, node->types)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -1061,7 +1061,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index f9eb4fea4..37b6a669b 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -28,7 +28,7 @@ VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t ctx, rbs_hash_t *rbs_hash) return ruby_hash; } -VALUE rbs_loc_to_ruby_location(rbs_location_t *loc) { +VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *loc) { return loc->cached_ruby_value; } @@ -96,7 +96,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan <%- when "rbs_hash" -%> rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_hash_to_ruby_hash(ctx, node-><%= field.name %>)); <%- when "rbs_location" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_loc_to_ruby_location(node-><%= field.name %>)); + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_loc_to_ruby_location(ctx, node-><%= field.name %>)); <%- when "rbs_ast_symbol" -%> rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.name %>)); // rbs_ast_symbol <%- when "rbs_keyword" -%> From 2a67c9663c42508794b31f61e17264d3625cdf2b Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 29 Nov 2024 14:19:25 -0500 Subject: [PATCH 018/111] =?UTF-8?q?Set=20locations=E2=80=99=20`buffer`=20i?= =?UTF-8?q?n=20translation=20layer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ext/rbs_extension/ast_translation.c | 5 +- ext/rbs_extension/location.c | 9 --- ext/rbs_extension/main.c | 3 + ext/rbs_extension/parser.c | 69 +++++++++---------- ext/rbs_extension/parserstate.c | 2 +- include/rbs/ast.h | 1 + include/rbs/rbs_location.h | 5 +- src/rbs_location.c | 16 ++++- .../ext/rbs_extension/ast_translation.c.erb | 5 +- templates/include/rbs/ast.h.erb | 1 + 10 files changed, 64 insertions(+), 52 deletions(-) diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index 9999b4129..c8a1fed46 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -12,6 +12,7 @@ #include "class_constants.h" #include "rbs_string_bridging.h" +#include "location.h" VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t *list) { VALUE ruby_array = rb_ary_new(); @@ -36,7 +37,9 @@ VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t ctx, rbs_hash_t *rbs_hash) } VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *loc) { - return loc->cached_ruby_value; + VALUE ruby_loc = loc->cached_ruby_value; + rbs_check_location(ruby_loc)->buffer = ctx.buffer; + return ruby_loc; } #ifdef RB_PASS_KEYWORDS diff --git a/ext/rbs_extension/location.c b/ext/rbs_extension/location.c index a6637cb85..07f1fcf18 100644 --- a/ext/rbs_extension/location.c +++ b/ext/rbs_extension/location.c @@ -8,15 +8,6 @@ rbs_loc_range RBS_LOC_NULL_RANGE = { -1, -1 }; VALUE RBS_Location; -rbs_location_t *rbs_location_new(VALUE buffer, range rg) { - rbs_location_t *location = (rbs_location_t *)malloc(sizeof(rbs_location_t)); - location->cached_ruby_value = rbs_new_location(buffer, rg); - rb_gc_register_mark_object(location->cached_ruby_value); - location->buffer = buffer; - location->rg = rg; - return location; -} - position rbs_loc_position(int char_pos) { position pos = { 0, char_pos, -1, -1 }; return pos; diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index c1e5479a3..bd2247313 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -56,6 +56,7 @@ static VALUE parse_type_try(VALUE a) { rbs_translation_context_t ctx = { .constant_pool = &parser->constant_pool, + .buffer = parser->buffer, }; return rbs_struct_to_ruby_value(ctx, type); @@ -93,6 +94,7 @@ static VALUE parse_method_type_try(VALUE a) { rbs_translation_context_t ctx = { .constant_pool = &parser->constant_pool, + .buffer = parser->buffer, }; return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type); @@ -121,6 +123,7 @@ static VALUE parse_signature_try(VALUE a) { rbs_translation_context_t ctx = { .constant_pool = &parser->constant_pool, + .buffer = parser->buffer, }; return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature); diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index 4dc419528..caf73cd7b 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -87,7 +87,6 @@ static bool rbs_is_untyped_params(method_params *params) { // * */ static rbs_location_t *rbs_location_current_token(parserstate *state) { return rbs_location_pp( - state->buffer, &state->current_token.range.start, &state->current_token.range.end ); @@ -267,7 +266,7 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t if (state->next_token.type == pCOMMA || state->next_token.type == pRPAREN) { range param_range = type_range; - rbs_location_t *loc = rbs_location_new(state->buffer, param_range); + rbs_location_t *loc = rbs_location_new(param_range); rbs_loc_alloc_children(loc, 1); rbs_loc_add_optional_child(loc, INTERN("name"), NULL_RANGE); @@ -296,7 +295,7 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t ); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); - rbs_location_t *loc = rbs_location_new(state->buffer, param_range); + rbs_location_t *loc = rbs_location_new(param_range); rbs_loc_alloc_children(loc, 1); rbs_loc_add_optional_child(loc, INTERN("name"), name_range); @@ -587,7 +586,7 @@ static bool parse_optional(parserstate *state, rbs_node_t **optional) { if (state->next_token.type == pQUESTION) { parser_advance(state); rg.end = state->current_token.range.end; - rbs_location_t *location = rbs_location_new(state->buffer, rg); + rbs_location_t *location = rbs_location_new(rg); *optional = (rbs_node_t *) rbs_types_optional_new(&state->allocator, type, location); } else { *optional = type; @@ -757,7 +756,7 @@ static bool parse_proc_type(parserstate *state, rbs_types_proc_t **proc) { CHECK_PARSE(parse_function(state, true, &result)); position end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_pp(state->buffer, &start, &end); + rbs_location_t *loc = rbs_location_pp(&start, &end); *proc = rbs_types_proc_new(&state->allocator, result->function, result->block, loc, result->function_self_type); return true; } @@ -933,7 +932,7 @@ static bool parse_instance_type(parserstate *state, bool parse_alias, rbs_node_t .end = nonnull_pos_or(args_range.end, name_range.end), }; - rbs_location_t *loc = rbs_location_new(state->buffer, type_range); + rbs_location_t *loc = rbs_location_new(type_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); @@ -968,7 +967,7 @@ static bool parse_singleton_type(parserstate *state, rbs_types_classsingleton_t parser_advance_assert(state, pRPAREN); type_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, type_range); + rbs_location_t *loc = rbs_location_new(type_range); rbs_loc_alloc_children(loc, 1); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -1134,7 +1133,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { parser_advance_assert(state, pRBRACKET); rg.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, rg); + rbs_location_t *loc = rbs_location_new(rg); *type = (rbs_node_t *) rbs_types_tuple_new(&state->allocator, types, loc); return true; } @@ -1150,7 +1149,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { CHECK_PARSE(parse_record_attributes(state, &fields)); parser_advance_assert(state, pRBRACE); position end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_pp(state->buffer, &start, &end); + rbs_location_t *loc = rbs_location_pp(&start, &end); *type = (rbs_node_t *) rbs_types_record_new(&state->allocator, fields, loc); return true; } @@ -1192,7 +1191,7 @@ static bool parse_intersection(parserstate *state, rbs_node_t **type) { rg.end = state->current_token.range.end; if (intersection_types->length > 1) { - rbs_location_t *location = rbs_location_new(state->buffer, rg); + rbs_location_t *location = rbs_location_new(rg); *type = (rbs_node_t *) rbs_types_intersection_new(&state->allocator, intersection_types, location); } @@ -1222,7 +1221,7 @@ bool parse_type(parserstate *state, rbs_node_t **type) { rg.end = state->current_token.range.end; if (union_types->length > 1) { - rbs_location_t *location = rbs_location_new(state->buffer, rg); + rbs_location_t *location = rbs_location_new(rg); *type = (rbs_node_t *) rbs_types_union_new(&state->allocator, union_types, location); } @@ -1325,7 +1324,7 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa param_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, param_range); + rbs_location_t *loc = rbs_location_new(param_range); rbs_loc_alloc_children(loc, 5); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("variance"), variance_range); @@ -1380,7 +1379,7 @@ bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type) { parser_pop_typevar_table(state); - rbs_location_t *loc = rbs_location_new(state->buffer, rg); + rbs_location_t *loc = rbs_location_new(rg); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("type"), type_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range); @@ -1409,7 +1408,7 @@ static bool parse_global_decl(parserstate *state, rbs_ast_declarations_global_t CHECK_PARSE(parse_type(state, &type)); decl_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); + rbs_location_t *loc = rbs_location_new(decl_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); @@ -1440,7 +1439,7 @@ static bool parse_const_decl(parserstate *state, rbs_ast_declarations_constant_t decl_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); + rbs_location_t *loc = rbs_location_new(decl_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); @@ -1480,7 +1479,7 @@ static bool parse_type_decl(parserstate *state, position comment_pos, rbs_node_l decl_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); + rbs_location_t *loc = rbs_location_new(decl_range); rbs_loc_alloc_children(loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -1545,7 +1544,7 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati rbs_string_strip_whitespace(&string); rbs_string_ensure_owned(&string); - rbs_location_t *loc = rbs_location_new(state->buffer, rg); + rbs_location_t *loc = rbs_location_new(rg); *annotation = rbs_ast_annotation_new(&state->allocator, string, loc); return true; @@ -1837,7 +1836,7 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept return false; } - rbs_location_t *loc = rbs_location_new(state->buffer, member_range); + rbs_location_t *loc = rbs_location_new(member_range); rbs_loc_alloc_children(loc, 5); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -1932,7 +1931,7 @@ static bool parse_mixin_member(parserstate *state, bool from_interface, position member_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, member_range); + rbs_location_t *loc = rbs_location_new(member_range); rbs_loc_alloc_children(loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); @@ -2000,7 +1999,7 @@ static bool parse_alias_member(parserstate *state, bool instance_only, position } member_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, member_range); + rbs_location_t *loc = rbs_location_new(member_range); rbs_loc_alloc_children(loc, 5); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("new_name"), new_name_range); @@ -2042,7 +2041,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ CHECK_PARSE(parse_type(state, &type)); member_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, member_range); + rbs_location_t *loc = rbs_location_new(member_range); rbs_loc_alloc_children(loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); @@ -2066,7 +2065,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ parser_pop_typevar_table(state); member_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, member_range); + rbs_location_t *loc = rbs_location_new(member_range); rbs_loc_alloc_children(loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); @@ -2098,7 +2097,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ parser_pop_typevar_table(state); member_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, member_range); + rbs_location_t *loc = rbs_location_new(member_range); rbs_loc_alloc_children(loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); @@ -2124,7 +2123,7 @@ static bool parse_visibility_member(parserstate *state, rbs_node_list_t *annotat return false; } - rbs_location_t *location = rbs_location_new(state->buffer, state->current_token.range); + rbs_location_t *location = rbs_location_new(state->current_token.range); switch (state->current_token.type) { @@ -2231,7 +2230,7 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs parser_pop_typevar_table(state); member_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, member_range); + rbs_location_t *loc = rbs_location_new(member_range); rbs_loc_alloc_children(loc, 7); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -2342,7 +2341,7 @@ static bool parse_interface_decl(parserstate *state, position comment_pos, rbs_n parser_pop_typevar_table(state); - rbs_location_t *loc = rbs_location_new(state->buffer, member_range); + rbs_location_t *loc = rbs_location_new(member_range); rbs_loc_alloc_children(loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -2384,7 +2383,7 @@ static bool parse_module_self_types(parserstate *state, rbs_node_list_t *array) self_range.end = args_range.end = state->current_token.range.end; } - rbs_location_t *loc = rbs_location_new(state->buffer, self_range); + rbs_location_t *loc = rbs_location_new(self_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); @@ -2536,7 +2535,7 @@ static bool parse_module_decl0(parserstate *state, range keyword_range, rbs_type range end_range = state->current_token.range; decl_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); + rbs_location_t *loc = rbs_location_new(decl_range); rbs_loc_alloc_children(loc, 6); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -2583,7 +2582,7 @@ static bool parse_module_decl(parserstate *state, position comment_pos, rbs_node .end = old_name_range.end }; - rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); + rbs_location_t *loc = rbs_location_new(decl_range); rbs_loc_alloc_children(loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("new_name"), module_name_range); @@ -2619,7 +2618,7 @@ static bool parse_class_decl_super(parserstate *state, range *lt_range, rbs_ast_ super_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, super_range); + rbs_location_t *loc = rbs_location_new(super_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); @@ -2662,7 +2661,7 @@ static bool parse_class_decl0(parserstate *state, range keyword_range, rbs_typen parser_pop_typevar_table(state); - rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); + rbs_location_t *loc = rbs_location_new(decl_range); rbs_loc_alloc_children(loc, 5); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -2704,7 +2703,7 @@ static bool parse_class_decl(parserstate *state, position comment_pos, rbs_node_ .end = old_name_range.end, }; - rbs_location_t *loc = rbs_location_new(state->buffer, decl_range); + rbs_location_t *loc = rbs_location_new(decl_range); rbs_loc_alloc_children(loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("new_name"), class_name_range); @@ -2918,7 +2917,7 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { clause_range.end = new_name_range.end; } - rbs_location_t *loc = rbs_location_new(state->buffer, clause_range); + rbs_location_t *loc = rbs_location_new(clause_range); rbs_loc_alloc_children(loc, 3); rbs_loc_add_required_child(loc, INTERN("type_name"), type_name_range); rbs_loc_add_optional_child(loc, INTERN("keyword"), keyword_range); @@ -2937,7 +2936,7 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { range star_range = state->current_token.range; clause_range.end = star_range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, clause_range); + rbs_location_t *loc = rbs_location_new(clause_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("namespace"), namespace_range); rbs_loc_add_required_child(loc, INTERN("star"), star_range); @@ -2978,7 +2977,7 @@ static bool parse_use_directive(parserstate *state, rbs_ast_directives_use_t **u range directive_range = keyword_range; directive_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(state->buffer, directive_range); + rbs_location_t *loc = rbs_location_new(directive_range); rbs_loc_alloc_children(loc, 1); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index 63461391a..6b8616bc4 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -209,7 +209,7 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_allocator_t *allocator, commen return rbs_ast_comment_new( allocator, rbs_buffer_to_string(&rbs_buffer), - rbs_location_pp(buffer, &com->start, &com->end) + rbs_location_pp(&com->start, &com->end) ); } diff --git a/include/rbs/ast.h b/include/rbs/ast.h index 7ee363993..a156e0407 100644 --- a/include/rbs/ast.h +++ b/include/rbs/ast.h @@ -89,6 +89,7 @@ typedef struct rbs_node { /// A bag of values needed when copying RBS C structs into Ruby objects. typedef struct rbs_translation_context { rbs_constant_pool_t *constant_pool; + VALUE buffer; } rbs_translation_context_t; /* rbs_node_list_node */ diff --git a/include/rbs/rbs_location.h b/include/rbs/rbs_location.h index edd0cd6f8..933c5419a 100644 --- a/include/rbs/rbs_location.h +++ b/include/rbs/rbs_location.h @@ -7,11 +7,10 @@ typedef struct rbs_location { VALUE cached_ruby_value; - VALUE buffer; range rg; } rbs_location_t; -rbs_location_t *rbs_location_new(VALUE buffer, range rg); +rbs_location_t *rbs_location_new(range rg); void rbs_loc_alloc_children(rbs_location_t *loc, int size); void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, range r); void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r); @@ -23,6 +22,6 @@ void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, ran * @param end_pos * @return New rbs_location_t struct. * */ -rbs_location_t *rbs_location_pp(VALUE buffer, const position *start_pos, const position *end_pos); +rbs_location_t *rbs_location_pp(const position *start_pos, const position *end_pos); #endif diff --git a/src/rbs_location.c b/src/rbs_location.c index 0376fe4d1..88a260a6f 100644 --- a/src/rbs_location.c +++ b/src/rbs_location.c @@ -6,12 +6,24 @@ static rbs_loc *get_rbs_location(const rbs_location_t *loc) { return rbs_check_location(loc->cached_ruby_value); } -rbs_location_t *rbs_location_pp(VALUE buffer, const position *start_pos, const position *end_pos) { +rbs_location_t *rbs_location_pp(const position *start_pos, const position *end_pos) { range rg = { *start_pos, *end_pos }; rg.start = *start_pos; rg.end = *end_pos; - return rbs_location_new(buffer, rg); + return rbs_location_new(rg); +} + +rbs_location_t *rbs_location_new(range rg) { + rbs_location_t *location = (rbs_location_t *)malloc(sizeof(rbs_location_t)); + *location = (rbs_location_t) { + .cached_ruby_value = rbs_new_location(Qundef, rg), + .rg = rg, + }; + + rb_gc_register_mark_object(location->cached_ruby_value); + + return location; } void rbs_loc_alloc_children(rbs_location_t *loc, int size) { diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index 37b6a669b..db852d51c 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -5,6 +5,7 @@ #include "class_constants.h" #include "rbs_string_bridging.h" +#include "location.h" VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t *list) { VALUE ruby_array = rb_ary_new(); @@ -29,7 +30,9 @@ VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t ctx, rbs_hash_t *rbs_hash) } VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *loc) { - return loc->cached_ruby_value; + VALUE ruby_loc = loc->cached_ruby_value; + rbs_check_location(ruby_loc)->buffer = ctx.buffer; + return ruby_loc; } #ifdef RB_PASS_KEYWORDS diff --git a/templates/include/rbs/ast.h.erb b/templates/include/rbs/ast.h.erb index 34825edb6..e8468ec53 100644 --- a/templates/include/rbs/ast.h.erb +++ b/templates/include/rbs/ast.h.erb @@ -22,6 +22,7 @@ typedef struct rbs_node { /// A bag of values needed when copying RBS C structs into Ruby objects. typedef struct rbs_translation_context { rbs_constant_pool_t *constant_pool; + VALUE buffer; } rbs_translation_context_t; /* rbs_node_list_node */ From b239f86107b9d1da72555f113f8523d384adff6f Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 29 Nov 2024 14:11:24 -0500 Subject: [PATCH 019/111] Remove `cached_ruby_object` from `rbs_location_t` --- ext/rbs_extension/ast_translation.c | 16 ++++-- ext/rbs_extension/location.c | 2 +- ext/rbs_extension/location.h | 22 +------- include/rbs/rbs_location.h | 3 +- include/rbs/rbs_location_internals.h | 27 +++++++++ src/rbs_location.c | 56 +++++++++++++++---- .../ext/rbs_extension/ast_translation.c.erb | 16 ++++-- 7 files changed, 101 insertions(+), 41 deletions(-) create mode 100644 include/rbs/rbs_location_internals.h diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index c8a1fed46..67951f004 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -14,6 +14,8 @@ #include "rbs_string_bridging.h" #include "location.h" +#define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1)) + VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t *list) { VALUE ruby_array = rb_ary_new(); @@ -36,10 +38,16 @@ VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t ctx, rbs_hash_t *rbs_hash) return ruby_hash; } -VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *loc) { - VALUE ruby_loc = loc->cached_ruby_value; - rbs_check_location(ruby_loc)->buffer = ctx.buffer; - return ruby_loc; +VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *source_loc) { + VALUE new_loc = rbs_new_location(ctx.buffer, source_loc->rg); + rbs_loc *new_loc_struct = rbs_check_location(new_loc); + + if (source_loc->children != NULL) { + rbs_loc_legacy_alloc_children(new_loc_struct, source_loc->children->cap); + memcpy(new_loc_struct->children, source_loc->children, RBS_LOC_CHILDREN_SIZE(source_loc->children->cap)); + } + + return new_loc; } #ifdef RB_PASS_KEYWORDS diff --git a/ext/rbs_extension/location.c b/ext/rbs_extension/location.c index 07f1fcf18..a08c3ca95 100644 --- a/ext/rbs_extension/location.c +++ b/ext/rbs_extension/location.c @@ -18,7 +18,7 @@ position rbs_loc_position3(int char_pos, int line, int column) { return pos; } -rbs_loc_range rbs_new_loc_range(range rg) { +static rbs_loc_range rbs_new_loc_range(range rg) { rbs_loc_range r = { rg.start.char_pos, rg.end.char_pos }; return r; } diff --git a/ext/rbs_extension/location.h b/ext/rbs_extension/location.h index 63b071bc8..19a15b1da 100644 --- a/ext/rbs_extension/location.h +++ b/ext/rbs_extension/location.h @@ -3,6 +3,7 @@ #include "ruby.h" #include "lexer.h" +#include "rbs/rbs_location_internals.h" #include "rbs/util/rbs_constant_pool.h" /** @@ -10,27 +11,6 @@ * */ extern VALUE RBS_Location; -typedef struct { - int start; - int end; -} rbs_loc_range; - -typedef struct { - rbs_constant_id_t name; - rbs_loc_range rg; -} rbs_loc_entry; - -typedef unsigned int rbs_loc_entry_bitmap; - -// The flexible array always allocates, but it's okay. -// This struct is not allocated when the `rbs_loc` doesn't have children. -typedef struct { - unsigned short len; - unsigned short cap; - rbs_loc_entry_bitmap required_p; - rbs_loc_entry entries[1]; -} rbs_loc_children; - typedef struct { VALUE buffer; rbs_loc_range rg; diff --git a/include/rbs/rbs_location.h b/include/rbs/rbs_location.h index 933c5419a..212f932b8 100644 --- a/include/rbs/rbs_location.h +++ b/include/rbs/rbs_location.h @@ -4,10 +4,11 @@ #include "ruby.h" #include "lexer.h" #include "rbs/util/rbs_constant_pool.h" +#include "rbs/rbs_location_internals.h" typedef struct rbs_location { - VALUE cached_ruby_value; range rg; + rbs_loc_children *children; } rbs_location_t; rbs_location_t *rbs_location_new(range rg); diff --git a/include/rbs/rbs_location_internals.h b/include/rbs/rbs_location_internals.h new file mode 100644 index 000000000..43fa9282c --- /dev/null +++ b/include/rbs/rbs_location_internals.h @@ -0,0 +1,27 @@ +#ifndef RBS__RBS_LOCATION_INTERNALS_H +#define RBS__RBS_LOCATION_INTERNALS_H + +#include "rbs/util/rbs_constant_pool.h" + +typedef struct { + int start; + int end; +} rbs_loc_range; + +typedef struct { + rbs_constant_id_t name; + rbs_loc_range rg; +} rbs_loc_entry; + +typedef unsigned int rbs_loc_entry_bitmap; + +// The flexible array always allocates, but it's okay. +// This struct is not allocated when the `rbs_loc` doesn't have children. +typedef struct { + unsigned short len; + unsigned short cap; + rbs_loc_entry_bitmap required_p; + rbs_loc_entry entries[1]; +} rbs_loc_children; + +#endif diff --git a/src/rbs_location.c b/src/rbs_location.c index 88a260a6f..5240fbb72 100644 --- a/src/rbs_location.c +++ b/src/rbs_location.c @@ -1,9 +1,30 @@ #include "rbs/rbs_location.h" #include "location.h" -// A helper for getting the "old style" `rbs_loc` from the new `rbs_location_t` struct. -static rbs_loc *get_rbs_location(const rbs_location_t *loc) { - return rbs_check_location(loc->cached_ruby_value); +#define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1)) + +static void check_children_max(unsigned short n) { + size_t max = sizeof(rbs_loc_entry_bitmap) * 8; + if (n > max) { + rb_raise(rb_eRuntimeError, "Too many children added to location: %d", n); + } +} + +static void check_children_cap(rbs_location_t *loc) { + if (loc->children == NULL) { + rbs_loc_alloc_children(loc, 1); + } else { + if (loc->children->len == loc->children->cap) { + check_children_max(loc->children->cap + 1); + size_t s = RBS_LOC_CHILDREN_SIZE(++loc->children->cap); + loc->children = realloc(loc->children, s); + } + } +} + +static rbs_loc_range rbs_new_loc_range(range rg) { + rbs_loc_range r = { rg.start.char_pos, rg.end.char_pos }; + return r; } rbs_location_t *rbs_location_pp(const position *start_pos, const position *end_pos) { @@ -17,23 +38,38 @@ rbs_location_t *rbs_location_pp(const position *start_pos, const position *end_p rbs_location_t *rbs_location_new(range rg) { rbs_location_t *location = (rbs_location_t *)malloc(sizeof(rbs_location_t)); *location = (rbs_location_t) { - .cached_ruby_value = rbs_new_location(Qundef, rg), .rg = rg, + .children = NULL, }; - rb_gc_register_mark_object(location->cached_ruby_value); - return location; } -void rbs_loc_alloc_children(rbs_location_t *loc, int size) { - rbs_loc_legacy_alloc_children(get_rbs_location(loc), size); +void rbs_loc_alloc_children(rbs_location_t *loc, int capacity) { + check_children_max(capacity); + + size_t s = RBS_LOC_CHILDREN_SIZE(capacity); + loc->children = malloc(s); + + loc->children->len = 0; + loc->children->required_p = 0; + loc->children->cap = capacity; } void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, range r) { - rbs_loc_legacy_add_required_child(get_rbs_location(loc), name, r); + check_children_cap(loc); + + unsigned short i = loc->children->len++; + loc->children->entries[i].name = name; + loc->children->entries[i].rg = rbs_new_loc_range(r); + + loc->children->required_p |= 1 << i; } void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r) { - rbs_loc_legacy_add_optional_child(get_rbs_location(loc), name, r); + check_children_cap(loc); + + unsigned short i = loc->children->len++; + loc->children->entries[i].name = name; + loc->children->entries[i].rg = rbs_new_loc_range(r); } diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index db852d51c..3b405d775 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -7,6 +7,8 @@ #include "rbs_string_bridging.h" #include "location.h" +#define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1)) + VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t *list) { VALUE ruby_array = rb_ary_new(); @@ -29,10 +31,16 @@ VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t ctx, rbs_hash_t *rbs_hash) return ruby_hash; } -VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *loc) { - VALUE ruby_loc = loc->cached_ruby_value; - rbs_check_location(ruby_loc)->buffer = ctx.buffer; - return ruby_loc; +VALUE rbs_loc_to_ruby_location(rbs_translation_context_t ctx, rbs_location_t *source_loc) { + VALUE new_loc = rbs_new_location(ctx.buffer, source_loc->rg); + rbs_loc *new_loc_struct = rbs_check_location(new_loc); + + if (source_loc->children != NULL) { + rbs_loc_legacy_alloc_children(new_loc_struct, source_loc->children->cap); + memcpy(new_loc_struct->children, source_loc->children, RBS_LOC_CHILDREN_SIZE(source_loc->children->cap)); + } + + return new_loc; } #ifdef RB_PASS_KEYWORDS From 3ca4117d6d468605245e2c9d29af8921fab6e6bf Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 29 Nov 2024 14:22:54 -0500 Subject: [PATCH 020/111] Dedupe `rbs_loc_add_required_child` body --- src/rbs_location.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/rbs_location.c b/src/rbs_location.c index 5240fbb72..1b5a6fcdb 100644 --- a/src/rbs_location.c +++ b/src/rbs_location.c @@ -57,13 +57,10 @@ void rbs_loc_alloc_children(rbs_location_t *loc, int capacity) { } void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, range r) { - check_children_cap(loc); - - unsigned short i = loc->children->len++; - loc->children->entries[i].name = name; - loc->children->entries[i].rg = rbs_new_loc_range(r); + rbs_loc_add_optional_child(loc, name, r); - loc->children->required_p |= 1 << i; + unsigned short last_index = loc->children->len - 1; + loc->children->required_p |= 1 << last_index; } void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r) { From 2f136c52f3e87abf71482b65a34985f41b57655c Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 29 Nov 2024 14:46:35 -0500 Subject: [PATCH 021/111] misc cleanup --- ext/rbs_extension/location.c | 14 +++++++------- ext/rbs_extension/location.h | 14 -------------- ext/rbs_extension/parser.c | 1 + include/rbs/rbs_location.h | 1 + 4 files changed, 9 insertions(+), 21 deletions(-) diff --git a/ext/rbs_extension/location.c b/ext/rbs_extension/location.c index a08c3ca95..905be13e2 100644 --- a/ext/rbs_extension/location.c +++ b/ext/rbs_extension/location.c @@ -56,13 +56,6 @@ static void check_children_cap(rbs_loc *loc) { } } -void rbs_loc_legacy_add_required_child(rbs_loc *loc, rbs_constant_id_t name, range r) { - rbs_loc_legacy_add_optional_child(loc, name, r); - - unsigned short last_index = loc->children->len - 1; - loc->children->required_p |= 1 << last_index; -} - void rbs_loc_legacy_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, range r) { check_children_cap(loc); @@ -73,6 +66,13 @@ void rbs_loc_legacy_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, ran }; } +void rbs_loc_legacy_add_required_child(rbs_loc *loc, rbs_constant_id_t name, range r) { + rbs_loc_legacy_add_optional_child(loc, name, r); + + unsigned short last_index = loc->children->len - 1; + loc->children->required_p |= 1 << last_index; +} + void rbs_loc_init(rbs_loc *loc, VALUE buffer, rbs_loc_range rg) { *loc = (rbs_loc) { .buffer = buffer, diff --git a/ext/rbs_extension/location.h b/ext/rbs_extension/location.h index 19a15b1da..9ee501770 100644 --- a/ext/rbs_extension/location.h +++ b/ext/rbs_extension/location.h @@ -34,20 +34,6 @@ rbs_loc *rbs_check_location(VALUE location); * */ void rbs_loc_legacy_alloc_children(rbs_loc *loc, unsigned short cap); -/** - * Add a required child range with given name. - * - * Allocate memory for children with rbs_loc_legacy_alloc_children before calling this function. - * */ -void rbs_loc_legacy_add_required_child(rbs_loc *loc, rbs_constant_id_t name, range r); - -/** - * Add an optional child range with given name. - * - * Allocate memory for children with rbs_loc_legacy_alloc_children before calling this function. - * */ -void rbs_loc_legacy_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, range r); - /** * Define RBS::Location class. * */ diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index caf73cd7b..aa39368d3 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -1,3 +1,4 @@ +#include "lexer.h" #include "location.h" #include "rbs_extension.h" #include "rbs/util/rbs_constant_pool.h" diff --git a/include/rbs/rbs_location.h b/include/rbs/rbs_location.h index 212f932b8..83d1e4c60 100644 --- a/include/rbs/rbs_location.h +++ b/include/rbs/rbs_location.h @@ -3,6 +3,7 @@ #include "ruby.h" #include "lexer.h" + #include "rbs/util/rbs_constant_pool.h" #include "rbs/rbs_location_internals.h" From aad46c1781ad11de14e29086aaedbdd3fbaa0068 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 29 Nov 2024 17:59:32 -0500 Subject: [PATCH 022/111] Migrate remaining uses of `rbs_unquote_string` --- ext/rbs_extension/parser.c | 53 +++++++++++++++---------------- ext/rbs_extension/rbs_extension.h | 13 -------- ext/rbs_extension/unescape.c | 31 ------------------ include/rbs/rbs_unescape.h | 2 +- src/rbs_unescape.c | 2 +- 5 files changed, 28 insertions(+), 73 deletions(-) delete mode 100644 ext/rbs_extension/unescape.c diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index aa39368d3..26c5421fc 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -96,6 +96,21 @@ static rbs_location_t *rbs_location_current_token(parserstate *state) { static bool parse_optional(parserstate *state, rbs_node_t **optional); static bool parse_simple(parserstate *state, rbs_node_t **type); +static rbs_string_t rbs_parser_get_current_token(parserstate *state) { + range rg = state->current_token.range; + + rbs_string_t string = rbs_string_from_ruby_string(state->lexstate->string); + rbs_string_drop_first(&string, rg.start.byte_pos); + rbs_string_limit_length(&string, rg.end.byte_pos - rg.start.byte_pos); + rbs_string_ensure_owned(&string); + + return string; +} + +static rbs_constant_id_t rbs_constant_pool_insert_string(rbs_constant_pool_t *self, rbs_string_t string) { + return rbs_constant_pool_insert_shared(self, (const uint8_t *) string.start, rbs_string_len(string)); +} + void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt, ...) { if (state->error) { return; @@ -288,12 +303,8 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t return false; } - VALUE name_str = rbs_unquote_string(state, state->current_token.range, 0); - rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared( - &state->constant_pool, - (const uint8_t *) RSTRING_PTR(name_str), - RSTRING_LEN(name_str) - ); + rbs_string_t unquoted_str = rbs_unquote_string(rbs_parser_get_current_token(state)); + rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); rbs_location_t *loc = rbs_location_new(param_range); @@ -868,13 +879,11 @@ static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types } case tDQSYMBOL: case tSQSYMBOL: { - VALUE ruby_str = rbs_unquote_string(state, state->current_token.range, offset_bytes); - rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared( - &state->constant_pool, - (const uint8_t *) RSTRING_PTR(ruby_str), - RSTRING_LEN(ruby_str) - ); - literal = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); + rbs_string_t string = rbs_parser_get_current_token(state); + rbs_string_drop_first(&string, offset_bytes); + + rbs_string_t unquoted_str = rbs_unquote_string(string); + literal = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str)); break; } default: @@ -1075,14 +1084,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { case tDQSTRING: { rbs_location_t *loc = rbs_location_current_token(state); - rbs_string_t string = rbs_string_from_ruby_string(state->lexstate->string); - rbs_string_drop_first(&string, state->current_token.range.start.byte_pos); - rbs_string_limit_length(&string, state->current_token.range.end.byte_pos - state->current_token.range.start.byte_pos); - rbs_string_ensure_owned(&string); - - rbs_string_t unquoted_str = rbs_unquote_string2(string); - rbs_string_ensure_owned(&unquoted_str); - + rbs_string_t unquoted_str = rbs_unquote_string(rbs_parser_get_current_token(state)); rbs_node_t *literal = (rbs_node_t *) rbs_ast_string_new(&state->allocator, unquoted_str); *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, literal, loc); return true; @@ -1619,12 +1621,9 @@ static bool parse_method_name(parserstate *state, range *range, rbs_ast_symbol_t return true; } case tQIDENT: { - VALUE ruby_str = rbs_unquote_string(state, state->current_token.range, 0); - rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared( - &state->constant_pool, - (const uint8_t *) RSTRING_PTR(ruby_str), - RSTRING_LEN(ruby_str) - ); + rbs_string_t string = rbs_parser_get_current_token(state); + rbs_string_t unquoted_str = rbs_unquote_string(string); + rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str); *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); return true; } diff --git a/ext/rbs_extension/rbs_extension.h b/ext/rbs_extension/rbs_extension.h index 70b6d1dbd..c22ac2a89 100644 --- a/ext/rbs_extension/rbs_extension.h +++ b/ext/rbs_extension/rbs_extension.h @@ -14,19 +14,6 @@ * */ extern VALUE RBS_Parser; -/** - * Receives `parserstate` and `range`, which represents a string token or symbol token, and returns a string VALUE. - * - * Input token | Output string - * ------------+------------- - * "foo\\n" | foo\n - * 'foo' | foo - * `bar` | bar - * :"baz\\t" | baz\t - * :'baz' | baz - * */ -VALUE rbs_unquote_string(parserstate *state, range rg, int offset_bytes); - /** * Raises RBS::ParsingError on `tok` with message constructed with given `fmt`. * diff --git a/ext/rbs_extension/unescape.c b/ext/rbs_extension/unescape.c deleted file mode 100644 index 0efdf8ad3..000000000 --- a/ext/rbs_extension/unescape.c +++ /dev/null @@ -1,31 +0,0 @@ -#include "rbs_extension.h" - -VALUE rbs_unquote_string(parserstate *state, range rg, int offset_bytes) { - VALUE string = state->lexstate->string; - rb_encoding *enc = rb_enc_get(string); - - unsigned int first_char = rb_enc_mbc_to_codepoint( - RSTRING_PTR(string) + rg.start.byte_pos + offset_bytes, - RSTRING_END(string), - enc - ); - - int byte_length = rg.end.byte_pos - rg.start.byte_pos - offset_bytes; - - if (first_char == '"' || first_char == '\'' || first_char == '`') { - int bs = rb_enc_codelen(first_char, enc); - offset_bytes += bs; - byte_length -= 2 * bs; - } - - char *buffer = RSTRING_PTR(string) + rg.start.byte_pos + offset_bytes; - VALUE str = rb_enc_str_new(buffer, byte_length, enc); - - return rb_funcall( - RBS_Types_Literal, - rb_intern("unescape_string"), - 2, - str, - first_char == '\"' ? Qtrue : Qfalse - ); -} diff --git a/include/rbs/rbs_unescape.h b/include/rbs/rbs_unescape.h index 2b382d698..cd803f83d 100644 --- a/include/rbs/rbs_unescape.h +++ b/include/rbs/rbs_unescape.h @@ -15,6 +15,6 @@ * :"baz\\t" | baz\t * :'baz' | baz * */ -rbs_string_t rbs_unquote_string2(rbs_string_t input); +rbs_string_t rbs_unquote_string(rbs_string_t input); #endif // RBS_RBS_UNESCAPE_H diff --git a/src/rbs_unescape.c b/src/rbs_unescape.c index d538b5282..808053dfa 100644 --- a/src/rbs_unescape.c +++ b/src/rbs_unescape.c @@ -109,7 +109,7 @@ rbs_string_t unescape_string(const rbs_string_t string, bool is_double_quote) { return str; } -rbs_string_t rbs_unquote_string2(rbs_string_t input) { +rbs_string_t rbs_unquote_string(rbs_string_t input) { unsigned int first_char = utf8_to_codepoint(input); size_t byte_length = rbs_string_len(input); From c998ecfc9f5986f8740597b46f9966d8cbac3cf4 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 29 Nov 2024 18:00:20 -0500 Subject: [PATCH 023/111] Apply `rbs_parser_get_current_token()` --- ext/rbs_extension/parser.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index 26c5421fc..46fcfbb2d 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -1060,9 +1060,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { case tINTEGER: { rbs_location_t *loc = rbs_location_current_token(state); - rbs_string_t string = rbs_string_from_ruby_string(state->lexstate->string); - rbs_string_drop_first(&string, state->current_token.range.start.byte_pos); - rbs_string_limit_length(&string, state->current_token.range.end.byte_pos - state->current_token.range.start.byte_pos); + rbs_string_t string = rbs_parser_get_current_token(state); rbs_string_strip_whitespace(&string); rbs_string_ensure_owned(&string); @@ -1289,12 +1287,8 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa parser_advance_assert(state, tUIDENT); range name_range = state->current_token.range; - rbs_constant_id_t id = rbs_constant_pool_insert_shared( - &state->constant_pool, - (const uint8_t *) peek_token(state->lexstate, state->current_token), - token_bytes(state->current_token) - ); - + rbs_string_t string = rbs_parser_get_current_token(state); + rbs_constant_id_t id = rbs_constant_pool_insert_string(&state->constant_pool, string); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, id); parser_insert_typevar(state, id); @@ -1541,9 +1535,9 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati int open_bytes = rb_enc_codelen(open_char, enc); int close_bytes = rb_enc_codelen(close_char, enc); - rbs_string_t string = rbs_string_from_ruby_string(state->lexstate->string); - rbs_string_drop_first(&string, rg.start.byte_pos + offset_bytes + open_bytes); - rbs_string_limit_length(&string, rg.end.byte_pos - rg.start.byte_pos - offset_bytes - open_bytes - close_bytes); + rbs_string_t string = rbs_parser_get_current_token(state); + rbs_string_drop_first(&string, offset_bytes + open_bytes); + rbs_string_limit_length(&string, rbs_string_len(string) - close_bytes); rbs_string_strip_whitespace(&string); rbs_string_ensure_owned(&string); From 7c7257088383f671c382f78f0c87c21321e6242a Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Mon, 2 Dec 2024 12:11:04 -0500 Subject: [PATCH 024/111] Delete `rbs_other_ruby_value_t` --- ext/rbs_extension/ast_translation.c | 3 --- ext/rbs_extension/ast_translation.h | 1 - include/rbs/ast.h | 8 -------- src/ast.c | 17 ----------------- .../ext/rbs_extension/ast_translation.c.erb | 3 --- .../ext/rbs_extension/ast_translation.h.erb | 1 - templates/include/rbs/ast.h.erb | 8 -------- templates/src/ast.c.erb | 17 ----------------- 8 files changed, 58 deletions(-) diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index 67951f004..4b7326b26 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -1098,8 +1098,5 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan return ID2SYM(rb_intern3((const char *) constant->start, constant->length, encoding)); } - case RBS_OTHER_RUBY_VALUE: { - return ((rbs_other_ruby_value_t *) instance)->ruby_value; - } } } diff --git a/ext/rbs_extension/ast_translation.h b/ext/rbs_extension/ast_translation.h index 30681148b..442e370c6 100644 --- a/ext/rbs_extension/ast_translation.h +++ b/ext/rbs_extension/ast_translation.h @@ -8,7 +8,6 @@ #ifndef RBS_EXTENSION_AST_TRANSLATION_H #define RBS_EXTENSION_AST_TRANSLATION_H -#include "ruby.h" #include "rbs/ast.h" VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *list); diff --git a/include/rbs/ast.h b/include/rbs/ast.h index a156e0407..332bc4b6d 100644 --- a/include/rbs/ast.h +++ b/include/rbs/ast.h @@ -14,7 +14,6 @@ #include "rbs_location.h" enum rbs_node_type { - RBS_OTHER_RUBY_VALUE = 0, RBS_AST_ANNOTATION = 1, RBS_AST_BOOL = 2, RBS_AST_COMMENT = 3, @@ -673,13 +672,6 @@ typedef struct rbs_ast_symbol { rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *, rbs_constant_pool_t *, rbs_constant_id_t); -typedef struct rbs_other_ruby_value { - rbs_node_t base; - VALUE ruby_value; -} rbs_other_ruby_value_t; - -rbs_other_ruby_value_t *rbs_other_ruby_value_new(VALUE ruby_value); - rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_string_t string, rbs_location_t *location); rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, bool value); rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, rbs_string_t string, rbs_location_t *location); diff --git a/src/ast.c b/src/ast.c index dffe41fb5..3acd2b7cc 100644 --- a/src/ast.c +++ b/src/ast.c @@ -68,8 +68,6 @@ bool rbs_node_equal(rbs_node_t *lhs, rbs_node_t *rhs) { return rbs_string_equal(((rbs_ast_integer_t *) lhs)->string_representation, ((rbs_ast_integer_t *) rhs)->string_representation); case RBS_AST_STRING: return rbs_string_equal(((rbs_ast_string_t *) lhs)->string, ((rbs_ast_string_t *) rhs)->string); - case RBS_OTHER_RUBY_VALUE: - return rb_equal(((rbs_other_ruby_value_t *) lhs)->ruby_value, ((rbs_other_ruby_value_t *) rhs)->ruby_value); default: printf("Unhandled node type: %d\n", lhs->type); return false; @@ -141,21 +139,6 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_constant_po return instance; } -rbs_other_ruby_value_t *rbs_other_ruby_value_new(VALUE ruby_value) { - rb_gc_register_mark_object(ruby_value); - - rbs_other_ruby_value_t *instance = malloc(sizeof(rbs_other_ruby_value_t)); - - *instance = (rbs_other_ruby_value_t) { - .base = (rbs_node_t) { - .type = RBS_OTHER_RUBY_VALUE - }, - .ruby_value = ruby_value, - }; - - return instance; -} - rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_string_t string, rbs_location_t *location) { rbs_ast_annotation_t *instance = rbs_allocator_alloc(allocator, rbs_ast_annotation_t); diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index 3b405d775..5d99caf64 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -159,8 +159,5 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan return ID2SYM(rb_intern3((const char *) constant->start, constant->length, encoding)); } - case RBS_OTHER_RUBY_VALUE: { - return ((rbs_other_ruby_value_t *) instance)->ruby_value; - } } } diff --git a/templates/ext/rbs_extension/ast_translation.h.erb b/templates/ext/rbs_extension/ast_translation.h.erb index fcc26f904..f23f5a573 100644 --- a/templates/ext/rbs_extension/ast_translation.h.erb +++ b/templates/ext/rbs_extension/ast_translation.h.erb @@ -1,7 +1,6 @@ #ifndef RBS_EXTENSION_AST_TRANSLATION_H #define RBS_EXTENSION_AST_TRANSLATION_H -#include "ruby.h" #include "rbs/ast.h" VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *list); diff --git a/templates/include/rbs/ast.h.erb b/templates/include/rbs/ast.h.erb index e8468ec53..0eb54f69a 100644 --- a/templates/include/rbs/ast.h.erb +++ b/templates/include/rbs/ast.h.erb @@ -7,7 +7,6 @@ #include "rbs_location.h" enum rbs_node_type { - RBS_OTHER_RUBY_VALUE = 0, <%- nodes.each_with_index do |node, index| -%> <%= node.c_type_enum_name %> = <%= index + 1 %>, <%- end -%> @@ -98,13 +97,6 @@ typedef struct rbs_ast_symbol { rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *, rbs_constant_pool_t *, rbs_constant_id_t); -typedef struct rbs_other_ruby_value { - rbs_node_t base; - VALUE ruby_value; -} rbs_other_ruby_value_t; - -rbs_other_ruby_value_t *rbs_other_ruby_value_new(VALUE ruby_value); - <%- nodes.each do |node| -%> <%= node.c_type_name %> *<%= node.c_constructor_function_name %>(<%= node.constructor_params.map(&:parameter_decl).join(", ") %>); <%- end -%> diff --git a/templates/src/ast.c.erb b/templates/src/ast.c.erb index bd68d7cf8..286df16f6 100644 --- a/templates/src/ast.c.erb +++ b/templates/src/ast.c.erb @@ -61,8 +61,6 @@ bool rbs_node_equal(rbs_node_t *lhs, rbs_node_t *rhs) { return rbs_string_equal(((rbs_ast_integer_t *) lhs)->string_representation, ((rbs_ast_integer_t *) rhs)->string_representation); case RBS_AST_STRING: return rbs_string_equal(((rbs_ast_string_t *) lhs)->string, ((rbs_ast_string_t *) rhs)->string); - case RBS_OTHER_RUBY_VALUE: - return rb_equal(((rbs_other_ruby_value_t *) lhs)->ruby_value, ((rbs_other_ruby_value_t *) rhs)->ruby_value); default: printf("Unhandled node type: %d\n", lhs->type); return false; @@ -134,21 +132,6 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_constant_po return instance; } -rbs_other_ruby_value_t *rbs_other_ruby_value_new(VALUE ruby_value) { - rb_gc_register_mark_object(ruby_value); - - rbs_other_ruby_value_t *instance = malloc(sizeof(rbs_other_ruby_value_t)); - - *instance = (rbs_other_ruby_value_t) { - .base = (rbs_node_t) { - .type = RBS_OTHER_RUBY_VALUE - }, - .ruby_value = ruby_value, - }; - - return instance; -} - <%- nodes.each do |node| -%> <%= node.c_type_name %> *<%= node.c_constructor_function_name %>(<%= node.constructor_params.map(&:parameter_decl).join(", ") %>) { <%= node.c_type_name %> *instance = rbs_allocator_alloc(allocator, <%= node.c_type_name %>); From 869a1a203eb2c96fa530c146d4675e8218049037 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Fri, 29 Nov 2024 17:26:30 -0500 Subject: [PATCH 025/111] Import encoding handling from Prism Signed-off-by: Alexandre Terrasa --- include/rbs/rbs_encoding.h | 282 ++ include/rbs/rbs_strncasecmp.h | 30 + src/rbs_encoding.c | 5241 +++++++++++++++++++++++++++++++++ src/rbs_strncasecmp.c | 24 + 4 files changed, 5577 insertions(+) create mode 100644 include/rbs/rbs_encoding.h create mode 100644 include/rbs/rbs_strncasecmp.h create mode 100644 src/rbs_encoding.c create mode 100644 src/rbs_strncasecmp.c diff --git a/include/rbs/rbs_encoding.h b/include/rbs/rbs_encoding.h new file mode 100644 index 000000000..e3c941c15 --- /dev/null +++ b/include/rbs/rbs_encoding.h @@ -0,0 +1,282 @@ +/** + * @file encoding.h + * + * The encoding interface and implementations used by the parser. + */ +#ifndef RBS_RBS_ENCODING_H +#define RBS_RBS_ENCODING_H + +#include "rbs/rbs_strncasecmp.h" + +#include +#include +#include +#include + +/** + * This struct defines the functions necessary to implement the encoding + * interface so we can determine how many bytes the subsequent character takes. + * Each callback should return the number of bytes, or 0 if the next bytes are + * invalid for the encoding and type. + */ +typedef struct { + /** + * Return the number of bytes that the next character takes if it is valid + * in the encoding. Does not read more than n bytes. It is assumed that n is + * at least 1. + */ + size_t (*char_width)(const uint8_t *b, ptrdiff_t n); + + /** + * Return the number of bytes that the next character takes if it is valid + * in the encoding and is alphabetical. Does not read more than n bytes. It + * is assumed that n is at least 1. + */ + size_t (*alpha_char)(const uint8_t *b, ptrdiff_t n); + + /** + * Return the number of bytes that the next character takes if it is valid + * in the encoding and is alphanumeric. Does not read more than n bytes. It + * is assumed that n is at least 1. + */ + size_t (*alnum_char)(const uint8_t *b, ptrdiff_t n); + + /** + * Return true if the next character is valid in the encoding and is an + * uppercase character. Does not read more than n bytes. It is assumed that + * n is at least 1. + */ + bool (*isupper_char)(const uint8_t *b, ptrdiff_t n); + + /** + * The name of the encoding. This should correspond to a value that can be + * passed to Encoding.find in Ruby. + */ + const char *name; + + /** + * Return true if the encoding is a multibyte encoding. + */ + bool multibyte; +} rbs_encoding_t; + +/** + * All of the lookup tables use the first bit of each embedded byte to indicate + * whether the codepoint is alphabetical. + */ +#define RBS_ENCODING_ALPHABETIC_BIT 1 << 0 + +/** + * All of the lookup tables use the second bit of each embedded byte to indicate + * whether the codepoint is alphanumeric. + */ +#define RBS_ENCODING_ALPHANUMERIC_BIT 1 << 1 + +/** + * All of the lookup tables use the third bit of each embedded byte to indicate + * whether the codepoint is uppercase. + */ +#define RBS_ENCODING_UPPERCASE_BIT 1 << 2 + +/** + * Return the size of the next character in the UTF-8 encoding. + * + * @param b The bytes to read. + * @param n The number of bytes that can be read. + * @returns The number of bytes that the next character takes if it is valid in + * the encoding, or 0 if it is not. + */ +size_t rbs_encoding_utf_8_char_width(const uint8_t *b, ptrdiff_t n); + +/** + * Return the size of the next character in the UTF-8 encoding if it is an + * alphabetical character. + * + * @param b The bytes to read. + * @param n The number of bytes that can be read. + * @returns The number of bytes that the next character takes if it is valid in + * the encoding, or 0 if it is not. + */ +size_t rbs_encoding_utf_8_alpha_char(const uint8_t *b, ptrdiff_t n); + +/** + * Return the size of the next character in the UTF-8 encoding if it is an + * alphanumeric character. + * + * @param b The bytes to read. + * @param n The number of bytes that can be read. + * @returns The number of bytes that the next character takes if it is valid in + * the encoding, or 0 if it is not. + */ +size_t rbs_encoding_utf_8_alnum_char(const uint8_t *b, ptrdiff_t n); + +/** + * Return true if the next character in the UTF-8 encoding if it is an uppercase + * character. + * + * @param b The bytes to read. + * @param n The number of bytes that can be read. + * @returns True if the next character is valid in the encoding and is an + * uppercase character, or false if it is not. + */ +bool rbs_encoding_utf_8_isupper_char(const uint8_t *b, ptrdiff_t n); + +/** + * This lookup table is referenced in both the UTF-8 encoding file and the + * parser directly in order to speed up the default encoding processing. It is + * used to indicate whether a character is alphabetical, alphanumeric, or + * uppercase in unicode mappings. + */ +extern const uint8_t rbs_encoding_unicode_table[256]; + +/** + * These are all of the encodings that prism supports. + */ +typedef enum { + RBS_ENCODING_UTF_8 = 0, + RBS_ENCODING_US_ASCII, + RBS_ENCODING_ASCII_8BIT, + RBS_ENCODING_EUC_JP, + RBS_ENCODING_WINDOWS_31J, + +// We optionally support excluding the full set of encodings to only support the +// minimum necessary to process Ruby code without encoding comments. +#ifndef RBS_ENCODING_EXCLUDE_FULL + RBS_ENCODING_BIG5, + RBS_ENCODING_BIG5_HKSCS, + RBS_ENCODING_BIG5_UAO, + RBS_ENCODING_CESU_8, + RBS_ENCODING_CP51932, + RBS_ENCODING_CP850, + RBS_ENCODING_CP852, + RBS_ENCODING_CP855, + RBS_ENCODING_CP949, + RBS_ENCODING_CP950, + RBS_ENCODING_CP951, + RBS_ENCODING_EMACS_MULE, + RBS_ENCODING_EUC_JP_MS, + RBS_ENCODING_EUC_JIS_2004, + RBS_ENCODING_EUC_KR, + RBS_ENCODING_EUC_TW, + RBS_ENCODING_GB12345, + RBS_ENCODING_GB18030, + RBS_ENCODING_GB1988, + RBS_ENCODING_GB2312, + RBS_ENCODING_GBK, + RBS_ENCODING_IBM437, + RBS_ENCODING_IBM720, + RBS_ENCODING_IBM737, + RBS_ENCODING_IBM775, + RBS_ENCODING_IBM852, + RBS_ENCODING_IBM855, + RBS_ENCODING_IBM857, + RBS_ENCODING_IBM860, + RBS_ENCODING_IBM861, + RBS_ENCODING_IBM862, + RBS_ENCODING_IBM863, + RBS_ENCODING_IBM864, + RBS_ENCODING_IBM865, + RBS_ENCODING_IBM866, + RBS_ENCODING_IBM869, + RBS_ENCODING_ISO_8859_1, + RBS_ENCODING_ISO_8859_2, + RBS_ENCODING_ISO_8859_3, + RBS_ENCODING_ISO_8859_4, + RBS_ENCODING_ISO_8859_5, + RBS_ENCODING_ISO_8859_6, + RBS_ENCODING_ISO_8859_7, + RBS_ENCODING_ISO_8859_8, + RBS_ENCODING_ISO_8859_9, + RBS_ENCODING_ISO_8859_10, + RBS_ENCODING_ISO_8859_11, + RBS_ENCODING_ISO_8859_13, + RBS_ENCODING_ISO_8859_14, + RBS_ENCODING_ISO_8859_15, + RBS_ENCODING_ISO_8859_16, + RBS_ENCODING_KOI8_R, + RBS_ENCODING_KOI8_U, + RBS_ENCODING_MAC_CENT_EURO, + RBS_ENCODING_MAC_CROATIAN, + RBS_ENCODING_MAC_CYRILLIC, + RBS_ENCODING_MAC_GREEK, + RBS_ENCODING_MAC_ICELAND, + RBS_ENCODING_MAC_JAPANESE, + RBS_ENCODING_MAC_ROMAN, + RBS_ENCODING_MAC_ROMANIA, + RBS_ENCODING_MAC_THAI, + RBS_ENCODING_MAC_TURKISH, + RBS_ENCODING_MAC_UKRAINE, + RBS_ENCODING_SHIFT_JIS, + RBS_ENCODING_SJIS_DOCOMO, + RBS_ENCODING_SJIS_KDDI, + RBS_ENCODING_SJIS_SOFTBANK, + RBS_ENCODING_STATELESS_ISO_2022_JP, + RBS_ENCODING_STATELESS_ISO_2022_JP_KDDI, + RBS_ENCODING_TIS_620, + RBS_ENCODING_UTF8_MAC, + RBS_ENCODING_UTF8_DOCOMO, + RBS_ENCODING_UTF8_KDDI, + RBS_ENCODING_UTF8_SOFTBANK, + RBS_ENCODING_WINDOWS_1250, + RBS_ENCODING_WINDOWS_1251, + RBS_ENCODING_WINDOWS_1252, + RBS_ENCODING_WINDOWS_1253, + RBS_ENCODING_WINDOWS_1254, + RBS_ENCODING_WINDOWS_1255, + RBS_ENCODING_WINDOWS_1256, + RBS_ENCODING_WINDOWS_1257, + RBS_ENCODING_WINDOWS_1258, + RBS_ENCODING_WINDOWS_874, +#endif + + RBS_ENCODING_MAXIMUM +} rbs_encoding_type_t; + +/** + * This is the table of all of the encodings that prism supports. + */ +extern const rbs_encoding_t rbs_encodings[RBS_ENCODING_MAXIMUM]; + +/** + * This is the default UTF-8 encoding. We need a reference to it to quickly + * create parsers. + */ +#define RBS_ENCODING_UTF_8_ENTRY (&rbs_encodings[RBS_ENCODING_UTF_8]) + +/** + * This is the US-ASCII encoding. We need a reference to it to be able to + * compare against it when a string is being created because it could possibly + * need to fall back to ASCII-8BIT. + */ +#define RBS_ENCODING_US_ASCII_ENTRY (&rbs_encodings[RBS_ENCODING_US_ASCII]) + +/** + * This is the ASCII-8BIT encoding. We need a reference to it so that rbs_strpbrk + * can compare against it because invalid multibyte characters are not a thing + * in this encoding. It is also needed for handling Regexp encoding flags. + */ +#define RBS_ENCODING_ASCII_8BIT_ENTRY (&rbs_encodings[RBS_ENCODING_ASCII_8BIT]) + +/** + * This is the EUC-JP encoding. We need a reference to it to quickly process + * regular expression modifiers. + */ +#define RBS_ENCODING_EUC_JP_ENTRY (&rbs_encodings[RBS_ENCODING_EUC_JP]) + +/** + * This is the Windows-31J encoding. We need a reference to it to quickly + * process regular expression modifiers. + */ +#define RBS_ENCODING_WINDOWS_31J_ENTRY (&rbs_encodings[RBS_ENCODING_WINDOWS_31J]) + +/** + * Parse the given name of an encoding and return a pointer to the corresponding + * encoding struct if one can be found, otherwise return NULL. + * + * @param start A pointer to the first byte of the name. + * @param end A pointer to the last byte of the name. + * @returns A pointer to the encoding struct if one is found, otherwise NULL. + */ +const rbs_encoding_t * rbs_encoding_find(const uint8_t *start, const uint8_t *end); + +#endif diff --git a/include/rbs/rbs_strncasecmp.h b/include/rbs/rbs_strncasecmp.h new file mode 100644 index 000000000..87d99703f --- /dev/null +++ b/include/rbs/rbs_strncasecmp.h @@ -0,0 +1,30 @@ +/** + * @file rbs_strncasecmp.h + * + * A custom strncasecmp implementation. + */ +#ifndef RBS_STRNCASECMP_H +#define RBS_STRNCASECMP_H + +#include +#include +#include + +/** + * Compare two strings, ignoring case, up to the given length. Returns 0 if the + * strings are equal, a negative number if string1 is less than string2, or a + * positive number if string1 is greater than string2. + * + * Note that this is effectively our own implementation of strncasecmp, but it's + * not available on all of the platforms we want to support so we're rolling it + * here. + * + * @param string1 The first string to compare. + * @param string2 The second string to compare + * @param length The maximum number of characters to compare. + * @return 0 if the strings are equal, a negative number if string1 is less than + * string2, or a positive number if string1 is greater than string2. + */ +int rbs_strncasecmp(const uint8_t *string1, const uint8_t *string2, size_t length); + +#endif diff --git a/src/rbs_encoding.c b/src/rbs_encoding.c new file mode 100644 index 000000000..c5d8bf794 --- /dev/null +++ b/src/rbs_encoding.c @@ -0,0 +1,5241 @@ +#include "rbs/rbs_encoding.h" + +#if defined(__GNUC__) +# define RBS_ATTRIBUTE_UNUSED __attribute__((unused)) +#else +# define RBS_ATTRIBUTE_UNUSED +#endif + +typedef uint32_t rbs_unicode_codepoint_t; + +#define UNICODE_ALPHA_CODEPOINTS_LENGTH 1450 +static const rbs_unicode_codepoint_t unicode_alpha_codepoints[UNICODE_ALPHA_CODEPOINTS_LENGTH] = { + 0x100, 0x2C1, + 0x2C6, 0x2D1, + 0x2E0, 0x2E4, + 0x2EC, 0x2EC, + 0x2EE, 0x2EE, + 0x345, 0x345, + 0x370, 0x374, + 0x376, 0x377, + 0x37A, 0x37D, + 0x37F, 0x37F, + 0x386, 0x386, + 0x388, 0x38A, + 0x38C, 0x38C, + 0x38E, 0x3A1, + 0x3A3, 0x3F5, + 0x3F7, 0x481, + 0x48A, 0x52F, + 0x531, 0x556, + 0x559, 0x559, + 0x560, 0x588, + 0x5B0, 0x5BD, + 0x5BF, 0x5BF, + 0x5C1, 0x5C2, + 0x5C4, 0x5C5, + 0x5C7, 0x5C7, + 0x5D0, 0x5EA, + 0x5EF, 0x5F2, + 0x610, 0x61A, + 0x620, 0x657, + 0x659, 0x65F, + 0x66E, 0x6D3, + 0x6D5, 0x6DC, + 0x6E1, 0x6E8, + 0x6ED, 0x6EF, + 0x6FA, 0x6FC, + 0x6FF, 0x6FF, + 0x710, 0x73F, + 0x74D, 0x7B1, + 0x7CA, 0x7EA, + 0x7F4, 0x7F5, + 0x7FA, 0x7FA, + 0x800, 0x817, + 0x81A, 0x82C, + 0x840, 0x858, + 0x860, 0x86A, + 0x870, 0x887, + 0x889, 0x88E, + 0x8A0, 0x8C9, + 0x8D4, 0x8DF, + 0x8E3, 0x8E9, + 0x8F0, 0x93B, + 0x93D, 0x94C, + 0x94E, 0x950, + 0x955, 0x963, + 0x971, 0x983, + 0x985, 0x98C, + 0x98F, 0x990, + 0x993, 0x9A8, + 0x9AA, 0x9B0, + 0x9B2, 0x9B2, + 0x9B6, 0x9B9, + 0x9BD, 0x9C4, + 0x9C7, 0x9C8, + 0x9CB, 0x9CC, + 0x9CE, 0x9CE, + 0x9D7, 0x9D7, + 0x9DC, 0x9DD, + 0x9DF, 0x9E3, + 0x9F0, 0x9F1, + 0x9FC, 0x9FC, + 0xA01, 0xA03, + 0xA05, 0xA0A, + 0xA0F, 0xA10, + 0xA13, 0xA28, + 0xA2A, 0xA30, + 0xA32, 0xA33, + 0xA35, 0xA36, + 0xA38, 0xA39, + 0xA3E, 0xA42, + 0xA47, 0xA48, + 0xA4B, 0xA4C, + 0xA51, 0xA51, + 0xA59, 0xA5C, + 0xA5E, 0xA5E, + 0xA70, 0xA75, + 0xA81, 0xA83, + 0xA85, 0xA8D, + 0xA8F, 0xA91, + 0xA93, 0xAA8, + 0xAAA, 0xAB0, + 0xAB2, 0xAB3, + 0xAB5, 0xAB9, + 0xABD, 0xAC5, + 0xAC7, 0xAC9, + 0xACB, 0xACC, + 0xAD0, 0xAD0, + 0xAE0, 0xAE3, + 0xAF9, 0xAFC, + 0xB01, 0xB03, + 0xB05, 0xB0C, + 0xB0F, 0xB10, + 0xB13, 0xB28, + 0xB2A, 0xB30, + 0xB32, 0xB33, + 0xB35, 0xB39, + 0xB3D, 0xB44, + 0xB47, 0xB48, + 0xB4B, 0xB4C, + 0xB56, 0xB57, + 0xB5C, 0xB5D, + 0xB5F, 0xB63, + 0xB71, 0xB71, + 0xB82, 0xB83, + 0xB85, 0xB8A, + 0xB8E, 0xB90, + 0xB92, 0xB95, + 0xB99, 0xB9A, + 0xB9C, 0xB9C, + 0xB9E, 0xB9F, + 0xBA3, 0xBA4, + 0xBA8, 0xBAA, + 0xBAE, 0xBB9, + 0xBBE, 0xBC2, + 0xBC6, 0xBC8, + 0xBCA, 0xBCC, + 0xBD0, 0xBD0, + 0xBD7, 0xBD7, + 0xC00, 0xC0C, + 0xC0E, 0xC10, + 0xC12, 0xC28, + 0xC2A, 0xC39, + 0xC3D, 0xC44, + 0xC46, 0xC48, + 0xC4A, 0xC4C, + 0xC55, 0xC56, + 0xC58, 0xC5A, + 0xC5D, 0xC5D, + 0xC60, 0xC63, + 0xC80, 0xC83, + 0xC85, 0xC8C, + 0xC8E, 0xC90, + 0xC92, 0xCA8, + 0xCAA, 0xCB3, + 0xCB5, 0xCB9, + 0xCBD, 0xCC4, + 0xCC6, 0xCC8, + 0xCCA, 0xCCC, + 0xCD5, 0xCD6, + 0xCDD, 0xCDE, + 0xCE0, 0xCE3, + 0xCF1, 0xCF3, + 0xD00, 0xD0C, + 0xD0E, 0xD10, + 0xD12, 0xD3A, + 0xD3D, 0xD44, + 0xD46, 0xD48, + 0xD4A, 0xD4C, + 0xD4E, 0xD4E, + 0xD54, 0xD57, + 0xD5F, 0xD63, + 0xD7A, 0xD7F, + 0xD81, 0xD83, + 0xD85, 0xD96, + 0xD9A, 0xDB1, + 0xDB3, 0xDBB, + 0xDBD, 0xDBD, + 0xDC0, 0xDC6, + 0xDCF, 0xDD4, + 0xDD6, 0xDD6, + 0xDD8, 0xDDF, + 0xDF2, 0xDF3, + 0xE01, 0xE3A, + 0xE40, 0xE46, + 0xE4D, 0xE4D, + 0xE81, 0xE82, + 0xE84, 0xE84, + 0xE86, 0xE8A, + 0xE8C, 0xEA3, + 0xEA5, 0xEA5, + 0xEA7, 0xEB9, + 0xEBB, 0xEBD, + 0xEC0, 0xEC4, + 0xEC6, 0xEC6, + 0xECD, 0xECD, + 0xEDC, 0xEDF, + 0xF00, 0xF00, + 0xF40, 0xF47, + 0xF49, 0xF6C, + 0xF71, 0xF83, + 0xF88, 0xF97, + 0xF99, 0xFBC, + 0x1000, 0x1036, + 0x1038, 0x1038, + 0x103B, 0x103F, + 0x1050, 0x108F, + 0x109A, 0x109D, + 0x10A0, 0x10C5, + 0x10C7, 0x10C7, + 0x10CD, 0x10CD, + 0x10D0, 0x10FA, + 0x10FC, 0x1248, + 0x124A, 0x124D, + 0x1250, 0x1256, + 0x1258, 0x1258, + 0x125A, 0x125D, + 0x1260, 0x1288, + 0x128A, 0x128D, + 0x1290, 0x12B0, + 0x12B2, 0x12B5, + 0x12B8, 0x12BE, + 0x12C0, 0x12C0, + 0x12C2, 0x12C5, + 0x12C8, 0x12D6, + 0x12D8, 0x1310, + 0x1312, 0x1315, + 0x1318, 0x135A, + 0x1380, 0x138F, + 0x13A0, 0x13F5, + 0x13F8, 0x13FD, + 0x1401, 0x166C, + 0x166F, 0x167F, + 0x1681, 0x169A, + 0x16A0, 0x16EA, + 0x16EE, 0x16F8, + 0x1700, 0x1713, + 0x171F, 0x1733, + 0x1740, 0x1753, + 0x1760, 0x176C, + 0x176E, 0x1770, + 0x1772, 0x1773, + 0x1780, 0x17B3, + 0x17B6, 0x17C8, + 0x17D7, 0x17D7, + 0x17DC, 0x17DC, + 0x1820, 0x1878, + 0x1880, 0x18AA, + 0x18B0, 0x18F5, + 0x1900, 0x191E, + 0x1920, 0x192B, + 0x1930, 0x1938, + 0x1950, 0x196D, + 0x1970, 0x1974, + 0x1980, 0x19AB, + 0x19B0, 0x19C9, + 0x1A00, 0x1A1B, + 0x1A20, 0x1A5E, + 0x1A61, 0x1A74, + 0x1AA7, 0x1AA7, + 0x1ABF, 0x1AC0, + 0x1ACC, 0x1ACE, + 0x1B00, 0x1B33, + 0x1B35, 0x1B43, + 0x1B45, 0x1B4C, + 0x1B80, 0x1BA9, + 0x1BAC, 0x1BAF, + 0x1BBA, 0x1BE5, + 0x1BE7, 0x1BF1, + 0x1C00, 0x1C36, + 0x1C4D, 0x1C4F, + 0x1C5A, 0x1C7D, + 0x1C80, 0x1C88, + 0x1C90, 0x1CBA, + 0x1CBD, 0x1CBF, + 0x1CE9, 0x1CEC, + 0x1CEE, 0x1CF3, + 0x1CF5, 0x1CF6, + 0x1CFA, 0x1CFA, + 0x1D00, 0x1DBF, + 0x1DE7, 0x1DF4, + 0x1E00, 0x1F15, + 0x1F18, 0x1F1D, + 0x1F20, 0x1F45, + 0x1F48, 0x1F4D, + 0x1F50, 0x1F57, + 0x1F59, 0x1F59, + 0x1F5B, 0x1F5B, + 0x1F5D, 0x1F5D, + 0x1F5F, 0x1F7D, + 0x1F80, 0x1FB4, + 0x1FB6, 0x1FBC, + 0x1FBE, 0x1FBE, + 0x1FC2, 0x1FC4, + 0x1FC6, 0x1FCC, + 0x1FD0, 0x1FD3, + 0x1FD6, 0x1FDB, + 0x1FE0, 0x1FEC, + 0x1FF2, 0x1FF4, + 0x1FF6, 0x1FFC, + 0x2071, 0x2071, + 0x207F, 0x207F, + 0x2090, 0x209C, + 0x2102, 0x2102, + 0x2107, 0x2107, + 0x210A, 0x2113, + 0x2115, 0x2115, + 0x2119, 0x211D, + 0x2124, 0x2124, + 0x2126, 0x2126, + 0x2128, 0x2128, + 0x212A, 0x212D, + 0x212F, 0x2139, + 0x213C, 0x213F, + 0x2145, 0x2149, + 0x214E, 0x214E, + 0x2160, 0x2188, + 0x24B6, 0x24E9, + 0x2C00, 0x2CE4, + 0x2CEB, 0x2CEE, + 0x2CF2, 0x2CF3, + 0x2D00, 0x2D25, + 0x2D27, 0x2D27, + 0x2D2D, 0x2D2D, + 0x2D30, 0x2D67, + 0x2D6F, 0x2D6F, + 0x2D80, 0x2D96, + 0x2DA0, 0x2DA6, + 0x2DA8, 0x2DAE, + 0x2DB0, 0x2DB6, + 0x2DB8, 0x2DBE, + 0x2DC0, 0x2DC6, + 0x2DC8, 0x2DCE, + 0x2DD0, 0x2DD6, + 0x2DD8, 0x2DDE, + 0x2DE0, 0x2DFF, + 0x2E2F, 0x2E2F, + 0x3005, 0x3007, + 0x3021, 0x3029, + 0x3031, 0x3035, + 0x3038, 0x303C, + 0x3041, 0x3096, + 0x309D, 0x309F, + 0x30A1, 0x30FA, + 0x30FC, 0x30FF, + 0x3105, 0x312F, + 0x3131, 0x318E, + 0x31A0, 0x31BF, + 0x31F0, 0x31FF, + 0x3400, 0x4DBF, + 0x4E00, 0xA48C, + 0xA4D0, 0xA4FD, + 0xA500, 0xA60C, + 0xA610, 0xA61F, + 0xA62A, 0xA62B, + 0xA640, 0xA66E, + 0xA674, 0xA67B, + 0xA67F, 0xA6EF, + 0xA717, 0xA71F, + 0xA722, 0xA788, + 0xA78B, 0xA7CA, + 0xA7D0, 0xA7D1, + 0xA7D3, 0xA7D3, + 0xA7D5, 0xA7D9, + 0xA7F2, 0xA805, + 0xA807, 0xA827, + 0xA840, 0xA873, + 0xA880, 0xA8C3, + 0xA8C5, 0xA8C5, + 0xA8F2, 0xA8F7, + 0xA8FB, 0xA8FB, + 0xA8FD, 0xA8FF, + 0xA90A, 0xA92A, + 0xA930, 0xA952, + 0xA960, 0xA97C, + 0xA980, 0xA9B2, + 0xA9B4, 0xA9BF, + 0xA9CF, 0xA9CF, + 0xA9E0, 0xA9EF, + 0xA9FA, 0xA9FE, + 0xAA00, 0xAA36, + 0xAA40, 0xAA4D, + 0xAA60, 0xAA76, + 0xAA7A, 0xAABE, + 0xAAC0, 0xAAC0, + 0xAAC2, 0xAAC2, + 0xAADB, 0xAADD, + 0xAAE0, 0xAAEF, + 0xAAF2, 0xAAF5, + 0xAB01, 0xAB06, + 0xAB09, 0xAB0E, + 0xAB11, 0xAB16, + 0xAB20, 0xAB26, + 0xAB28, 0xAB2E, + 0xAB30, 0xAB5A, + 0xAB5C, 0xAB69, + 0xAB70, 0xABEA, + 0xAC00, 0xD7A3, + 0xD7B0, 0xD7C6, + 0xD7CB, 0xD7FB, + 0xF900, 0xFA6D, + 0xFA70, 0xFAD9, + 0xFB00, 0xFB06, + 0xFB13, 0xFB17, + 0xFB1D, 0xFB28, + 0xFB2A, 0xFB36, + 0xFB38, 0xFB3C, + 0xFB3E, 0xFB3E, + 0xFB40, 0xFB41, + 0xFB43, 0xFB44, + 0xFB46, 0xFBB1, + 0xFBD3, 0xFD3D, + 0xFD50, 0xFD8F, + 0xFD92, 0xFDC7, + 0xFDF0, 0xFDFB, + 0xFE70, 0xFE74, + 0xFE76, 0xFEFC, + 0xFF21, 0xFF3A, + 0xFF41, 0xFF5A, + 0xFF66, 0xFFBE, + 0xFFC2, 0xFFC7, + 0xFFCA, 0xFFCF, + 0xFFD2, 0xFFD7, + 0xFFDA, 0xFFDC, + 0x10000, 0x1000B, + 0x1000D, 0x10026, + 0x10028, 0x1003A, + 0x1003C, 0x1003D, + 0x1003F, 0x1004D, + 0x10050, 0x1005D, + 0x10080, 0x100FA, + 0x10140, 0x10174, + 0x10280, 0x1029C, + 0x102A0, 0x102D0, + 0x10300, 0x1031F, + 0x1032D, 0x1034A, + 0x10350, 0x1037A, + 0x10380, 0x1039D, + 0x103A0, 0x103C3, + 0x103C8, 0x103CF, + 0x103D1, 0x103D5, + 0x10400, 0x1049D, + 0x104B0, 0x104D3, + 0x104D8, 0x104FB, + 0x10500, 0x10527, + 0x10530, 0x10563, + 0x10570, 0x1057A, + 0x1057C, 0x1058A, + 0x1058C, 0x10592, + 0x10594, 0x10595, + 0x10597, 0x105A1, + 0x105A3, 0x105B1, + 0x105B3, 0x105B9, + 0x105BB, 0x105BC, + 0x10600, 0x10736, + 0x10740, 0x10755, + 0x10760, 0x10767, + 0x10780, 0x10785, + 0x10787, 0x107B0, + 0x107B2, 0x107BA, + 0x10800, 0x10805, + 0x10808, 0x10808, + 0x1080A, 0x10835, + 0x10837, 0x10838, + 0x1083C, 0x1083C, + 0x1083F, 0x10855, + 0x10860, 0x10876, + 0x10880, 0x1089E, + 0x108E0, 0x108F2, + 0x108F4, 0x108F5, + 0x10900, 0x10915, + 0x10920, 0x10939, + 0x10980, 0x109B7, + 0x109BE, 0x109BF, + 0x10A00, 0x10A03, + 0x10A05, 0x10A06, + 0x10A0C, 0x10A13, + 0x10A15, 0x10A17, + 0x10A19, 0x10A35, + 0x10A60, 0x10A7C, + 0x10A80, 0x10A9C, + 0x10AC0, 0x10AC7, + 0x10AC9, 0x10AE4, + 0x10B00, 0x10B35, + 0x10B40, 0x10B55, + 0x10B60, 0x10B72, + 0x10B80, 0x10B91, + 0x10C00, 0x10C48, + 0x10C80, 0x10CB2, + 0x10CC0, 0x10CF2, + 0x10D00, 0x10D27, + 0x10E80, 0x10EA9, + 0x10EAB, 0x10EAC, + 0x10EB0, 0x10EB1, + 0x10F00, 0x10F1C, + 0x10F27, 0x10F27, + 0x10F30, 0x10F45, + 0x10F70, 0x10F81, + 0x10FB0, 0x10FC4, + 0x10FE0, 0x10FF6, + 0x11000, 0x11045, + 0x11071, 0x11075, + 0x11080, 0x110B8, + 0x110C2, 0x110C2, + 0x110D0, 0x110E8, + 0x11100, 0x11132, + 0x11144, 0x11147, + 0x11150, 0x11172, + 0x11176, 0x11176, + 0x11180, 0x111BF, + 0x111C1, 0x111C4, + 0x111CE, 0x111CF, + 0x111DA, 0x111DA, + 0x111DC, 0x111DC, + 0x11200, 0x11211, + 0x11213, 0x11234, + 0x11237, 0x11237, + 0x1123E, 0x11241, + 0x11280, 0x11286, + 0x11288, 0x11288, + 0x1128A, 0x1128D, + 0x1128F, 0x1129D, + 0x1129F, 0x112A8, + 0x112B0, 0x112E8, + 0x11300, 0x11303, + 0x11305, 0x1130C, + 0x1130F, 0x11310, + 0x11313, 0x11328, + 0x1132A, 0x11330, + 0x11332, 0x11333, + 0x11335, 0x11339, + 0x1133D, 0x11344, + 0x11347, 0x11348, + 0x1134B, 0x1134C, + 0x11350, 0x11350, + 0x11357, 0x11357, + 0x1135D, 0x11363, + 0x11400, 0x11441, + 0x11443, 0x11445, + 0x11447, 0x1144A, + 0x1145F, 0x11461, + 0x11480, 0x114C1, + 0x114C4, 0x114C5, + 0x114C7, 0x114C7, + 0x11580, 0x115B5, + 0x115B8, 0x115BE, + 0x115D8, 0x115DD, + 0x11600, 0x1163E, + 0x11640, 0x11640, + 0x11644, 0x11644, + 0x11680, 0x116B5, + 0x116B8, 0x116B8, + 0x11700, 0x1171A, + 0x1171D, 0x1172A, + 0x11740, 0x11746, + 0x11800, 0x11838, + 0x118A0, 0x118DF, + 0x118FF, 0x11906, + 0x11909, 0x11909, + 0x1190C, 0x11913, + 0x11915, 0x11916, + 0x11918, 0x11935, + 0x11937, 0x11938, + 0x1193B, 0x1193C, + 0x1193F, 0x11942, + 0x119A0, 0x119A7, + 0x119AA, 0x119D7, + 0x119DA, 0x119DF, + 0x119E1, 0x119E1, + 0x119E3, 0x119E4, + 0x11A00, 0x11A32, + 0x11A35, 0x11A3E, + 0x11A50, 0x11A97, + 0x11A9D, 0x11A9D, + 0x11AB0, 0x11AF8, + 0x11C00, 0x11C08, + 0x11C0A, 0x11C36, + 0x11C38, 0x11C3E, + 0x11C40, 0x11C40, + 0x11C72, 0x11C8F, + 0x11C92, 0x11CA7, + 0x11CA9, 0x11CB6, + 0x11D00, 0x11D06, + 0x11D08, 0x11D09, + 0x11D0B, 0x11D36, + 0x11D3A, 0x11D3A, + 0x11D3C, 0x11D3D, + 0x11D3F, 0x11D41, + 0x11D43, 0x11D43, + 0x11D46, 0x11D47, + 0x11D60, 0x11D65, + 0x11D67, 0x11D68, + 0x11D6A, 0x11D8E, + 0x11D90, 0x11D91, + 0x11D93, 0x11D96, + 0x11D98, 0x11D98, + 0x11EE0, 0x11EF6, + 0x11F00, 0x11F10, + 0x11F12, 0x11F3A, + 0x11F3E, 0x11F40, + 0x11FB0, 0x11FB0, + 0x12000, 0x12399, + 0x12400, 0x1246E, + 0x12480, 0x12543, + 0x12F90, 0x12FF0, + 0x13000, 0x1342F, + 0x13441, 0x13446, + 0x14400, 0x14646, + 0x16800, 0x16A38, + 0x16A40, 0x16A5E, + 0x16A70, 0x16ABE, + 0x16AD0, 0x16AED, + 0x16B00, 0x16B2F, + 0x16B40, 0x16B43, + 0x16B63, 0x16B77, + 0x16B7D, 0x16B8F, + 0x16E40, 0x16E7F, + 0x16F00, 0x16F4A, + 0x16F4F, 0x16F87, + 0x16F8F, 0x16F9F, + 0x16FE0, 0x16FE1, + 0x16FE3, 0x16FE3, + 0x16FF0, 0x16FF1, + 0x17000, 0x187F7, + 0x18800, 0x18CD5, + 0x18D00, 0x18D08, + 0x1AFF0, 0x1AFF3, + 0x1AFF5, 0x1AFFB, + 0x1AFFD, 0x1AFFE, + 0x1B000, 0x1B122, + 0x1B132, 0x1B132, + 0x1B150, 0x1B152, + 0x1B155, 0x1B155, + 0x1B164, 0x1B167, + 0x1B170, 0x1B2FB, + 0x1BC00, 0x1BC6A, + 0x1BC70, 0x1BC7C, + 0x1BC80, 0x1BC88, + 0x1BC90, 0x1BC99, + 0x1BC9E, 0x1BC9E, + 0x1D400, 0x1D454, + 0x1D456, 0x1D49C, + 0x1D49E, 0x1D49F, + 0x1D4A2, 0x1D4A2, + 0x1D4A5, 0x1D4A6, + 0x1D4A9, 0x1D4AC, + 0x1D4AE, 0x1D4B9, + 0x1D4BB, 0x1D4BB, + 0x1D4BD, 0x1D4C3, + 0x1D4C5, 0x1D505, + 0x1D507, 0x1D50A, + 0x1D50D, 0x1D514, + 0x1D516, 0x1D51C, + 0x1D51E, 0x1D539, + 0x1D53B, 0x1D53E, + 0x1D540, 0x1D544, + 0x1D546, 0x1D546, + 0x1D54A, 0x1D550, + 0x1D552, 0x1D6A5, + 0x1D6A8, 0x1D6C0, + 0x1D6C2, 0x1D6DA, + 0x1D6DC, 0x1D6FA, + 0x1D6FC, 0x1D714, + 0x1D716, 0x1D734, + 0x1D736, 0x1D74E, + 0x1D750, 0x1D76E, + 0x1D770, 0x1D788, + 0x1D78A, 0x1D7A8, + 0x1D7AA, 0x1D7C2, + 0x1D7C4, 0x1D7CB, + 0x1DF00, 0x1DF1E, + 0x1DF25, 0x1DF2A, + 0x1E000, 0x1E006, + 0x1E008, 0x1E018, + 0x1E01B, 0x1E021, + 0x1E023, 0x1E024, + 0x1E026, 0x1E02A, + 0x1E030, 0x1E06D, + 0x1E08F, 0x1E08F, + 0x1E100, 0x1E12C, + 0x1E137, 0x1E13D, + 0x1E14E, 0x1E14E, + 0x1E290, 0x1E2AD, + 0x1E2C0, 0x1E2EB, + 0x1E4D0, 0x1E4EB, + 0x1E7E0, 0x1E7E6, + 0x1E7E8, 0x1E7EB, + 0x1E7ED, 0x1E7EE, + 0x1E7F0, 0x1E7FE, + 0x1E800, 0x1E8C4, + 0x1E900, 0x1E943, + 0x1E947, 0x1E947, + 0x1E94B, 0x1E94B, + 0x1EE00, 0x1EE03, + 0x1EE05, 0x1EE1F, + 0x1EE21, 0x1EE22, + 0x1EE24, 0x1EE24, + 0x1EE27, 0x1EE27, + 0x1EE29, 0x1EE32, + 0x1EE34, 0x1EE37, + 0x1EE39, 0x1EE39, + 0x1EE3B, 0x1EE3B, + 0x1EE42, 0x1EE42, + 0x1EE47, 0x1EE47, + 0x1EE49, 0x1EE49, + 0x1EE4B, 0x1EE4B, + 0x1EE4D, 0x1EE4F, + 0x1EE51, 0x1EE52, + 0x1EE54, 0x1EE54, + 0x1EE57, 0x1EE57, + 0x1EE59, 0x1EE59, + 0x1EE5B, 0x1EE5B, + 0x1EE5D, 0x1EE5D, + 0x1EE5F, 0x1EE5F, + 0x1EE61, 0x1EE62, + 0x1EE64, 0x1EE64, + 0x1EE67, 0x1EE6A, + 0x1EE6C, 0x1EE72, + 0x1EE74, 0x1EE77, + 0x1EE79, 0x1EE7C, + 0x1EE7E, 0x1EE7E, + 0x1EE80, 0x1EE89, + 0x1EE8B, 0x1EE9B, + 0x1EEA1, 0x1EEA3, + 0x1EEA5, 0x1EEA9, + 0x1EEAB, 0x1EEBB, + 0x1F130, 0x1F149, + 0x1F150, 0x1F169, + 0x1F170, 0x1F189, + 0x20000, 0x2A6DF, + 0x2A700, 0x2B739, + 0x2B740, 0x2B81D, + 0x2B820, 0x2CEA1, + 0x2CEB0, 0x2EBE0, + 0x2F800, 0x2FA1D, + 0x30000, 0x3134A, + 0x31350, 0x323AF, +}; + +#define UNICODE_ALNUM_CODEPOINTS_LENGTH 1528 +static const rbs_unicode_codepoint_t unicode_alnum_codepoints[UNICODE_ALNUM_CODEPOINTS_LENGTH] = { + 0x100, 0x2C1, + 0x2C6, 0x2D1, + 0x2E0, 0x2E4, + 0x2EC, 0x2EC, + 0x2EE, 0x2EE, + 0x345, 0x345, + 0x370, 0x374, + 0x376, 0x377, + 0x37A, 0x37D, + 0x37F, 0x37F, + 0x386, 0x386, + 0x388, 0x38A, + 0x38C, 0x38C, + 0x38E, 0x3A1, + 0x3A3, 0x3F5, + 0x3F7, 0x481, + 0x48A, 0x52F, + 0x531, 0x556, + 0x559, 0x559, + 0x560, 0x588, + 0x5B0, 0x5BD, + 0x5BF, 0x5BF, + 0x5C1, 0x5C2, + 0x5C4, 0x5C5, + 0x5C7, 0x5C7, + 0x5D0, 0x5EA, + 0x5EF, 0x5F2, + 0x610, 0x61A, + 0x620, 0x657, + 0x659, 0x669, + 0x66E, 0x6D3, + 0x6D5, 0x6DC, + 0x6E1, 0x6E8, + 0x6ED, 0x6FC, + 0x6FF, 0x6FF, + 0x710, 0x73F, + 0x74D, 0x7B1, + 0x7C0, 0x7EA, + 0x7F4, 0x7F5, + 0x7FA, 0x7FA, + 0x800, 0x817, + 0x81A, 0x82C, + 0x840, 0x858, + 0x860, 0x86A, + 0x870, 0x887, + 0x889, 0x88E, + 0x8A0, 0x8C9, + 0x8D4, 0x8DF, + 0x8E3, 0x8E9, + 0x8F0, 0x93B, + 0x93D, 0x94C, + 0x94E, 0x950, + 0x955, 0x963, + 0x966, 0x96F, + 0x971, 0x983, + 0x985, 0x98C, + 0x98F, 0x990, + 0x993, 0x9A8, + 0x9AA, 0x9B0, + 0x9B2, 0x9B2, + 0x9B6, 0x9B9, + 0x9BD, 0x9C4, + 0x9C7, 0x9C8, + 0x9CB, 0x9CC, + 0x9CE, 0x9CE, + 0x9D7, 0x9D7, + 0x9DC, 0x9DD, + 0x9DF, 0x9E3, + 0x9E6, 0x9F1, + 0x9FC, 0x9FC, + 0xA01, 0xA03, + 0xA05, 0xA0A, + 0xA0F, 0xA10, + 0xA13, 0xA28, + 0xA2A, 0xA30, + 0xA32, 0xA33, + 0xA35, 0xA36, + 0xA38, 0xA39, + 0xA3E, 0xA42, + 0xA47, 0xA48, + 0xA4B, 0xA4C, + 0xA51, 0xA51, + 0xA59, 0xA5C, + 0xA5E, 0xA5E, + 0xA66, 0xA75, + 0xA81, 0xA83, + 0xA85, 0xA8D, + 0xA8F, 0xA91, + 0xA93, 0xAA8, + 0xAAA, 0xAB0, + 0xAB2, 0xAB3, + 0xAB5, 0xAB9, + 0xABD, 0xAC5, + 0xAC7, 0xAC9, + 0xACB, 0xACC, + 0xAD0, 0xAD0, + 0xAE0, 0xAE3, + 0xAE6, 0xAEF, + 0xAF9, 0xAFC, + 0xB01, 0xB03, + 0xB05, 0xB0C, + 0xB0F, 0xB10, + 0xB13, 0xB28, + 0xB2A, 0xB30, + 0xB32, 0xB33, + 0xB35, 0xB39, + 0xB3D, 0xB44, + 0xB47, 0xB48, + 0xB4B, 0xB4C, + 0xB56, 0xB57, + 0xB5C, 0xB5D, + 0xB5F, 0xB63, + 0xB66, 0xB6F, + 0xB71, 0xB71, + 0xB82, 0xB83, + 0xB85, 0xB8A, + 0xB8E, 0xB90, + 0xB92, 0xB95, + 0xB99, 0xB9A, + 0xB9C, 0xB9C, + 0xB9E, 0xB9F, + 0xBA3, 0xBA4, + 0xBA8, 0xBAA, + 0xBAE, 0xBB9, + 0xBBE, 0xBC2, + 0xBC6, 0xBC8, + 0xBCA, 0xBCC, + 0xBD0, 0xBD0, + 0xBD7, 0xBD7, + 0xBE6, 0xBEF, + 0xC00, 0xC0C, + 0xC0E, 0xC10, + 0xC12, 0xC28, + 0xC2A, 0xC39, + 0xC3D, 0xC44, + 0xC46, 0xC48, + 0xC4A, 0xC4C, + 0xC55, 0xC56, + 0xC58, 0xC5A, + 0xC5D, 0xC5D, + 0xC60, 0xC63, + 0xC66, 0xC6F, + 0xC80, 0xC83, + 0xC85, 0xC8C, + 0xC8E, 0xC90, + 0xC92, 0xCA8, + 0xCAA, 0xCB3, + 0xCB5, 0xCB9, + 0xCBD, 0xCC4, + 0xCC6, 0xCC8, + 0xCCA, 0xCCC, + 0xCD5, 0xCD6, + 0xCDD, 0xCDE, + 0xCE0, 0xCE3, + 0xCE6, 0xCEF, + 0xCF1, 0xCF3, + 0xD00, 0xD0C, + 0xD0E, 0xD10, + 0xD12, 0xD3A, + 0xD3D, 0xD44, + 0xD46, 0xD48, + 0xD4A, 0xD4C, + 0xD4E, 0xD4E, + 0xD54, 0xD57, + 0xD5F, 0xD63, + 0xD66, 0xD6F, + 0xD7A, 0xD7F, + 0xD81, 0xD83, + 0xD85, 0xD96, + 0xD9A, 0xDB1, + 0xDB3, 0xDBB, + 0xDBD, 0xDBD, + 0xDC0, 0xDC6, + 0xDCF, 0xDD4, + 0xDD6, 0xDD6, + 0xDD8, 0xDDF, + 0xDE6, 0xDEF, + 0xDF2, 0xDF3, + 0xE01, 0xE3A, + 0xE40, 0xE46, + 0xE4D, 0xE4D, + 0xE50, 0xE59, + 0xE81, 0xE82, + 0xE84, 0xE84, + 0xE86, 0xE8A, + 0xE8C, 0xEA3, + 0xEA5, 0xEA5, + 0xEA7, 0xEB9, + 0xEBB, 0xEBD, + 0xEC0, 0xEC4, + 0xEC6, 0xEC6, + 0xECD, 0xECD, + 0xED0, 0xED9, + 0xEDC, 0xEDF, + 0xF00, 0xF00, + 0xF20, 0xF29, + 0xF40, 0xF47, + 0xF49, 0xF6C, + 0xF71, 0xF83, + 0xF88, 0xF97, + 0xF99, 0xFBC, + 0x1000, 0x1036, + 0x1038, 0x1038, + 0x103B, 0x1049, + 0x1050, 0x109D, + 0x10A0, 0x10C5, + 0x10C7, 0x10C7, + 0x10CD, 0x10CD, + 0x10D0, 0x10FA, + 0x10FC, 0x1248, + 0x124A, 0x124D, + 0x1250, 0x1256, + 0x1258, 0x1258, + 0x125A, 0x125D, + 0x1260, 0x1288, + 0x128A, 0x128D, + 0x1290, 0x12B0, + 0x12B2, 0x12B5, + 0x12B8, 0x12BE, + 0x12C0, 0x12C0, + 0x12C2, 0x12C5, + 0x12C8, 0x12D6, + 0x12D8, 0x1310, + 0x1312, 0x1315, + 0x1318, 0x135A, + 0x1380, 0x138F, + 0x13A0, 0x13F5, + 0x13F8, 0x13FD, + 0x1401, 0x166C, + 0x166F, 0x167F, + 0x1681, 0x169A, + 0x16A0, 0x16EA, + 0x16EE, 0x16F8, + 0x1700, 0x1713, + 0x171F, 0x1733, + 0x1740, 0x1753, + 0x1760, 0x176C, + 0x176E, 0x1770, + 0x1772, 0x1773, + 0x1780, 0x17B3, + 0x17B6, 0x17C8, + 0x17D7, 0x17D7, + 0x17DC, 0x17DC, + 0x17E0, 0x17E9, + 0x1810, 0x1819, + 0x1820, 0x1878, + 0x1880, 0x18AA, + 0x18B0, 0x18F5, + 0x1900, 0x191E, + 0x1920, 0x192B, + 0x1930, 0x1938, + 0x1946, 0x196D, + 0x1970, 0x1974, + 0x1980, 0x19AB, + 0x19B0, 0x19C9, + 0x19D0, 0x19D9, + 0x1A00, 0x1A1B, + 0x1A20, 0x1A5E, + 0x1A61, 0x1A74, + 0x1A80, 0x1A89, + 0x1A90, 0x1A99, + 0x1AA7, 0x1AA7, + 0x1ABF, 0x1AC0, + 0x1ACC, 0x1ACE, + 0x1B00, 0x1B33, + 0x1B35, 0x1B43, + 0x1B45, 0x1B4C, + 0x1B50, 0x1B59, + 0x1B80, 0x1BA9, + 0x1BAC, 0x1BE5, + 0x1BE7, 0x1BF1, + 0x1C00, 0x1C36, + 0x1C40, 0x1C49, + 0x1C4D, 0x1C7D, + 0x1C80, 0x1C88, + 0x1C90, 0x1CBA, + 0x1CBD, 0x1CBF, + 0x1CE9, 0x1CEC, + 0x1CEE, 0x1CF3, + 0x1CF5, 0x1CF6, + 0x1CFA, 0x1CFA, + 0x1D00, 0x1DBF, + 0x1DE7, 0x1DF4, + 0x1E00, 0x1F15, + 0x1F18, 0x1F1D, + 0x1F20, 0x1F45, + 0x1F48, 0x1F4D, + 0x1F50, 0x1F57, + 0x1F59, 0x1F59, + 0x1F5B, 0x1F5B, + 0x1F5D, 0x1F5D, + 0x1F5F, 0x1F7D, + 0x1F80, 0x1FB4, + 0x1FB6, 0x1FBC, + 0x1FBE, 0x1FBE, + 0x1FC2, 0x1FC4, + 0x1FC6, 0x1FCC, + 0x1FD0, 0x1FD3, + 0x1FD6, 0x1FDB, + 0x1FE0, 0x1FEC, + 0x1FF2, 0x1FF4, + 0x1FF6, 0x1FFC, + 0x2071, 0x2071, + 0x207F, 0x207F, + 0x2090, 0x209C, + 0x2102, 0x2102, + 0x2107, 0x2107, + 0x210A, 0x2113, + 0x2115, 0x2115, + 0x2119, 0x211D, + 0x2124, 0x2124, + 0x2126, 0x2126, + 0x2128, 0x2128, + 0x212A, 0x212D, + 0x212F, 0x2139, + 0x213C, 0x213F, + 0x2145, 0x2149, + 0x214E, 0x214E, + 0x2160, 0x2188, + 0x24B6, 0x24E9, + 0x2C00, 0x2CE4, + 0x2CEB, 0x2CEE, + 0x2CF2, 0x2CF3, + 0x2D00, 0x2D25, + 0x2D27, 0x2D27, + 0x2D2D, 0x2D2D, + 0x2D30, 0x2D67, + 0x2D6F, 0x2D6F, + 0x2D80, 0x2D96, + 0x2DA0, 0x2DA6, + 0x2DA8, 0x2DAE, + 0x2DB0, 0x2DB6, + 0x2DB8, 0x2DBE, + 0x2DC0, 0x2DC6, + 0x2DC8, 0x2DCE, + 0x2DD0, 0x2DD6, + 0x2DD8, 0x2DDE, + 0x2DE0, 0x2DFF, + 0x2E2F, 0x2E2F, + 0x3005, 0x3007, + 0x3021, 0x3029, + 0x3031, 0x3035, + 0x3038, 0x303C, + 0x3041, 0x3096, + 0x309D, 0x309F, + 0x30A1, 0x30FA, + 0x30FC, 0x30FF, + 0x3105, 0x312F, + 0x3131, 0x318E, + 0x31A0, 0x31BF, + 0x31F0, 0x31FF, + 0x3400, 0x4DBF, + 0x4E00, 0xA48C, + 0xA4D0, 0xA4FD, + 0xA500, 0xA60C, + 0xA610, 0xA62B, + 0xA640, 0xA66E, + 0xA674, 0xA67B, + 0xA67F, 0xA6EF, + 0xA717, 0xA71F, + 0xA722, 0xA788, + 0xA78B, 0xA7CA, + 0xA7D0, 0xA7D1, + 0xA7D3, 0xA7D3, + 0xA7D5, 0xA7D9, + 0xA7F2, 0xA805, + 0xA807, 0xA827, + 0xA840, 0xA873, + 0xA880, 0xA8C3, + 0xA8C5, 0xA8C5, + 0xA8D0, 0xA8D9, + 0xA8F2, 0xA8F7, + 0xA8FB, 0xA8FB, + 0xA8FD, 0xA92A, + 0xA930, 0xA952, + 0xA960, 0xA97C, + 0xA980, 0xA9B2, + 0xA9B4, 0xA9BF, + 0xA9CF, 0xA9D9, + 0xA9E0, 0xA9FE, + 0xAA00, 0xAA36, + 0xAA40, 0xAA4D, + 0xAA50, 0xAA59, + 0xAA60, 0xAA76, + 0xAA7A, 0xAABE, + 0xAAC0, 0xAAC0, + 0xAAC2, 0xAAC2, + 0xAADB, 0xAADD, + 0xAAE0, 0xAAEF, + 0xAAF2, 0xAAF5, + 0xAB01, 0xAB06, + 0xAB09, 0xAB0E, + 0xAB11, 0xAB16, + 0xAB20, 0xAB26, + 0xAB28, 0xAB2E, + 0xAB30, 0xAB5A, + 0xAB5C, 0xAB69, + 0xAB70, 0xABEA, + 0xABF0, 0xABF9, + 0xAC00, 0xD7A3, + 0xD7B0, 0xD7C6, + 0xD7CB, 0xD7FB, + 0xF900, 0xFA6D, + 0xFA70, 0xFAD9, + 0xFB00, 0xFB06, + 0xFB13, 0xFB17, + 0xFB1D, 0xFB28, + 0xFB2A, 0xFB36, + 0xFB38, 0xFB3C, + 0xFB3E, 0xFB3E, + 0xFB40, 0xFB41, + 0xFB43, 0xFB44, + 0xFB46, 0xFBB1, + 0xFBD3, 0xFD3D, + 0xFD50, 0xFD8F, + 0xFD92, 0xFDC7, + 0xFDF0, 0xFDFB, + 0xFE70, 0xFE74, + 0xFE76, 0xFEFC, + 0xFF10, 0xFF19, + 0xFF21, 0xFF3A, + 0xFF41, 0xFF5A, + 0xFF66, 0xFFBE, + 0xFFC2, 0xFFC7, + 0xFFCA, 0xFFCF, + 0xFFD2, 0xFFD7, + 0xFFDA, 0xFFDC, + 0x10000, 0x1000B, + 0x1000D, 0x10026, + 0x10028, 0x1003A, + 0x1003C, 0x1003D, + 0x1003F, 0x1004D, + 0x10050, 0x1005D, + 0x10080, 0x100FA, + 0x10140, 0x10174, + 0x10280, 0x1029C, + 0x102A0, 0x102D0, + 0x10300, 0x1031F, + 0x1032D, 0x1034A, + 0x10350, 0x1037A, + 0x10380, 0x1039D, + 0x103A0, 0x103C3, + 0x103C8, 0x103CF, + 0x103D1, 0x103D5, + 0x10400, 0x1049D, + 0x104A0, 0x104A9, + 0x104B0, 0x104D3, + 0x104D8, 0x104FB, + 0x10500, 0x10527, + 0x10530, 0x10563, + 0x10570, 0x1057A, + 0x1057C, 0x1058A, + 0x1058C, 0x10592, + 0x10594, 0x10595, + 0x10597, 0x105A1, + 0x105A3, 0x105B1, + 0x105B3, 0x105B9, + 0x105BB, 0x105BC, + 0x10600, 0x10736, + 0x10740, 0x10755, + 0x10760, 0x10767, + 0x10780, 0x10785, + 0x10787, 0x107B0, + 0x107B2, 0x107BA, + 0x10800, 0x10805, + 0x10808, 0x10808, + 0x1080A, 0x10835, + 0x10837, 0x10838, + 0x1083C, 0x1083C, + 0x1083F, 0x10855, + 0x10860, 0x10876, + 0x10880, 0x1089E, + 0x108E0, 0x108F2, + 0x108F4, 0x108F5, + 0x10900, 0x10915, + 0x10920, 0x10939, + 0x10980, 0x109B7, + 0x109BE, 0x109BF, + 0x10A00, 0x10A03, + 0x10A05, 0x10A06, + 0x10A0C, 0x10A13, + 0x10A15, 0x10A17, + 0x10A19, 0x10A35, + 0x10A60, 0x10A7C, + 0x10A80, 0x10A9C, + 0x10AC0, 0x10AC7, + 0x10AC9, 0x10AE4, + 0x10B00, 0x10B35, + 0x10B40, 0x10B55, + 0x10B60, 0x10B72, + 0x10B80, 0x10B91, + 0x10C00, 0x10C48, + 0x10C80, 0x10CB2, + 0x10CC0, 0x10CF2, + 0x10D00, 0x10D27, + 0x10D30, 0x10D39, + 0x10E80, 0x10EA9, + 0x10EAB, 0x10EAC, + 0x10EB0, 0x10EB1, + 0x10F00, 0x10F1C, + 0x10F27, 0x10F27, + 0x10F30, 0x10F45, + 0x10F70, 0x10F81, + 0x10FB0, 0x10FC4, + 0x10FE0, 0x10FF6, + 0x11000, 0x11045, + 0x11066, 0x1106F, + 0x11071, 0x11075, + 0x11080, 0x110B8, + 0x110C2, 0x110C2, + 0x110D0, 0x110E8, + 0x110F0, 0x110F9, + 0x11100, 0x11132, + 0x11136, 0x1113F, + 0x11144, 0x11147, + 0x11150, 0x11172, + 0x11176, 0x11176, + 0x11180, 0x111BF, + 0x111C1, 0x111C4, + 0x111CE, 0x111DA, + 0x111DC, 0x111DC, + 0x11200, 0x11211, + 0x11213, 0x11234, + 0x11237, 0x11237, + 0x1123E, 0x11241, + 0x11280, 0x11286, + 0x11288, 0x11288, + 0x1128A, 0x1128D, + 0x1128F, 0x1129D, + 0x1129F, 0x112A8, + 0x112B0, 0x112E8, + 0x112F0, 0x112F9, + 0x11300, 0x11303, + 0x11305, 0x1130C, + 0x1130F, 0x11310, + 0x11313, 0x11328, + 0x1132A, 0x11330, + 0x11332, 0x11333, + 0x11335, 0x11339, + 0x1133D, 0x11344, + 0x11347, 0x11348, + 0x1134B, 0x1134C, + 0x11350, 0x11350, + 0x11357, 0x11357, + 0x1135D, 0x11363, + 0x11400, 0x11441, + 0x11443, 0x11445, + 0x11447, 0x1144A, + 0x11450, 0x11459, + 0x1145F, 0x11461, + 0x11480, 0x114C1, + 0x114C4, 0x114C5, + 0x114C7, 0x114C7, + 0x114D0, 0x114D9, + 0x11580, 0x115B5, + 0x115B8, 0x115BE, + 0x115D8, 0x115DD, + 0x11600, 0x1163E, + 0x11640, 0x11640, + 0x11644, 0x11644, + 0x11650, 0x11659, + 0x11680, 0x116B5, + 0x116B8, 0x116B8, + 0x116C0, 0x116C9, + 0x11700, 0x1171A, + 0x1171D, 0x1172A, + 0x11730, 0x11739, + 0x11740, 0x11746, + 0x11800, 0x11838, + 0x118A0, 0x118E9, + 0x118FF, 0x11906, + 0x11909, 0x11909, + 0x1190C, 0x11913, + 0x11915, 0x11916, + 0x11918, 0x11935, + 0x11937, 0x11938, + 0x1193B, 0x1193C, + 0x1193F, 0x11942, + 0x11950, 0x11959, + 0x119A0, 0x119A7, + 0x119AA, 0x119D7, + 0x119DA, 0x119DF, + 0x119E1, 0x119E1, + 0x119E3, 0x119E4, + 0x11A00, 0x11A32, + 0x11A35, 0x11A3E, + 0x11A50, 0x11A97, + 0x11A9D, 0x11A9D, + 0x11AB0, 0x11AF8, + 0x11C00, 0x11C08, + 0x11C0A, 0x11C36, + 0x11C38, 0x11C3E, + 0x11C40, 0x11C40, + 0x11C50, 0x11C59, + 0x11C72, 0x11C8F, + 0x11C92, 0x11CA7, + 0x11CA9, 0x11CB6, + 0x11D00, 0x11D06, + 0x11D08, 0x11D09, + 0x11D0B, 0x11D36, + 0x11D3A, 0x11D3A, + 0x11D3C, 0x11D3D, + 0x11D3F, 0x11D41, + 0x11D43, 0x11D43, + 0x11D46, 0x11D47, + 0x11D50, 0x11D59, + 0x11D60, 0x11D65, + 0x11D67, 0x11D68, + 0x11D6A, 0x11D8E, + 0x11D90, 0x11D91, + 0x11D93, 0x11D96, + 0x11D98, 0x11D98, + 0x11DA0, 0x11DA9, + 0x11EE0, 0x11EF6, + 0x11F00, 0x11F10, + 0x11F12, 0x11F3A, + 0x11F3E, 0x11F40, + 0x11F50, 0x11F59, + 0x11FB0, 0x11FB0, + 0x12000, 0x12399, + 0x12400, 0x1246E, + 0x12480, 0x12543, + 0x12F90, 0x12FF0, + 0x13000, 0x1342F, + 0x13441, 0x13446, + 0x14400, 0x14646, + 0x16800, 0x16A38, + 0x16A40, 0x16A5E, + 0x16A60, 0x16A69, + 0x16A70, 0x16ABE, + 0x16AC0, 0x16AC9, + 0x16AD0, 0x16AED, + 0x16B00, 0x16B2F, + 0x16B40, 0x16B43, + 0x16B50, 0x16B59, + 0x16B63, 0x16B77, + 0x16B7D, 0x16B8F, + 0x16E40, 0x16E7F, + 0x16F00, 0x16F4A, + 0x16F4F, 0x16F87, + 0x16F8F, 0x16F9F, + 0x16FE0, 0x16FE1, + 0x16FE3, 0x16FE3, + 0x16FF0, 0x16FF1, + 0x17000, 0x187F7, + 0x18800, 0x18CD5, + 0x18D00, 0x18D08, + 0x1AFF0, 0x1AFF3, + 0x1AFF5, 0x1AFFB, + 0x1AFFD, 0x1AFFE, + 0x1B000, 0x1B122, + 0x1B132, 0x1B132, + 0x1B150, 0x1B152, + 0x1B155, 0x1B155, + 0x1B164, 0x1B167, + 0x1B170, 0x1B2FB, + 0x1BC00, 0x1BC6A, + 0x1BC70, 0x1BC7C, + 0x1BC80, 0x1BC88, + 0x1BC90, 0x1BC99, + 0x1BC9E, 0x1BC9E, + 0x1D400, 0x1D454, + 0x1D456, 0x1D49C, + 0x1D49E, 0x1D49F, + 0x1D4A2, 0x1D4A2, + 0x1D4A5, 0x1D4A6, + 0x1D4A9, 0x1D4AC, + 0x1D4AE, 0x1D4B9, + 0x1D4BB, 0x1D4BB, + 0x1D4BD, 0x1D4C3, + 0x1D4C5, 0x1D505, + 0x1D507, 0x1D50A, + 0x1D50D, 0x1D514, + 0x1D516, 0x1D51C, + 0x1D51E, 0x1D539, + 0x1D53B, 0x1D53E, + 0x1D540, 0x1D544, + 0x1D546, 0x1D546, + 0x1D54A, 0x1D550, + 0x1D552, 0x1D6A5, + 0x1D6A8, 0x1D6C0, + 0x1D6C2, 0x1D6DA, + 0x1D6DC, 0x1D6FA, + 0x1D6FC, 0x1D714, + 0x1D716, 0x1D734, + 0x1D736, 0x1D74E, + 0x1D750, 0x1D76E, + 0x1D770, 0x1D788, + 0x1D78A, 0x1D7A8, + 0x1D7AA, 0x1D7C2, + 0x1D7C4, 0x1D7CB, + 0x1D7CE, 0x1D7FF, + 0x1DF00, 0x1DF1E, + 0x1DF25, 0x1DF2A, + 0x1E000, 0x1E006, + 0x1E008, 0x1E018, + 0x1E01B, 0x1E021, + 0x1E023, 0x1E024, + 0x1E026, 0x1E02A, + 0x1E030, 0x1E06D, + 0x1E08F, 0x1E08F, + 0x1E100, 0x1E12C, + 0x1E137, 0x1E13D, + 0x1E140, 0x1E149, + 0x1E14E, 0x1E14E, + 0x1E290, 0x1E2AD, + 0x1E2C0, 0x1E2EB, + 0x1E2F0, 0x1E2F9, + 0x1E4D0, 0x1E4EB, + 0x1E4F0, 0x1E4F9, + 0x1E7E0, 0x1E7E6, + 0x1E7E8, 0x1E7EB, + 0x1E7ED, 0x1E7EE, + 0x1E7F0, 0x1E7FE, + 0x1E800, 0x1E8C4, + 0x1E900, 0x1E943, + 0x1E947, 0x1E947, + 0x1E94B, 0x1E94B, + 0x1E950, 0x1E959, + 0x1EE00, 0x1EE03, + 0x1EE05, 0x1EE1F, + 0x1EE21, 0x1EE22, + 0x1EE24, 0x1EE24, + 0x1EE27, 0x1EE27, + 0x1EE29, 0x1EE32, + 0x1EE34, 0x1EE37, + 0x1EE39, 0x1EE39, + 0x1EE3B, 0x1EE3B, + 0x1EE42, 0x1EE42, + 0x1EE47, 0x1EE47, + 0x1EE49, 0x1EE49, + 0x1EE4B, 0x1EE4B, + 0x1EE4D, 0x1EE4F, + 0x1EE51, 0x1EE52, + 0x1EE54, 0x1EE54, + 0x1EE57, 0x1EE57, + 0x1EE59, 0x1EE59, + 0x1EE5B, 0x1EE5B, + 0x1EE5D, 0x1EE5D, + 0x1EE5F, 0x1EE5F, + 0x1EE61, 0x1EE62, + 0x1EE64, 0x1EE64, + 0x1EE67, 0x1EE6A, + 0x1EE6C, 0x1EE72, + 0x1EE74, 0x1EE77, + 0x1EE79, 0x1EE7C, + 0x1EE7E, 0x1EE7E, + 0x1EE80, 0x1EE89, + 0x1EE8B, 0x1EE9B, + 0x1EEA1, 0x1EEA3, + 0x1EEA5, 0x1EEA9, + 0x1EEAB, 0x1EEBB, + 0x1F130, 0x1F149, + 0x1F150, 0x1F169, + 0x1F170, 0x1F189, + 0x1FBF0, 0x1FBF9, + 0x20000, 0x2A6DF, + 0x2A700, 0x2B739, + 0x2B740, 0x2B81D, + 0x2B820, 0x2CEA1, + 0x2CEB0, 0x2EBE0, + 0x2F800, 0x2FA1D, + 0x30000, 0x3134A, + 0x31350, 0x323AF, +}; + +#define UNICODE_ISUPPER_CODEPOINTS_LENGTH 1302 +static const rbs_unicode_codepoint_t unicode_isupper_codepoints[UNICODE_ISUPPER_CODEPOINTS_LENGTH] = { + 0x100, 0x100, + 0x102, 0x102, + 0x104, 0x104, + 0x106, 0x106, + 0x108, 0x108, + 0x10A, 0x10A, + 0x10C, 0x10C, + 0x10E, 0x10E, + 0x110, 0x110, + 0x112, 0x112, + 0x114, 0x114, + 0x116, 0x116, + 0x118, 0x118, + 0x11A, 0x11A, + 0x11C, 0x11C, + 0x11E, 0x11E, + 0x120, 0x120, + 0x122, 0x122, + 0x124, 0x124, + 0x126, 0x126, + 0x128, 0x128, + 0x12A, 0x12A, + 0x12C, 0x12C, + 0x12E, 0x12E, + 0x130, 0x130, + 0x132, 0x132, + 0x134, 0x134, + 0x136, 0x136, + 0x139, 0x139, + 0x13B, 0x13B, + 0x13D, 0x13D, + 0x13F, 0x13F, + 0x141, 0x141, + 0x143, 0x143, + 0x145, 0x145, + 0x147, 0x147, + 0x14A, 0x14A, + 0x14C, 0x14C, + 0x14E, 0x14E, + 0x150, 0x150, + 0x152, 0x152, + 0x154, 0x154, + 0x156, 0x156, + 0x158, 0x158, + 0x15A, 0x15A, + 0x15C, 0x15C, + 0x15E, 0x15E, + 0x160, 0x160, + 0x162, 0x162, + 0x164, 0x164, + 0x166, 0x166, + 0x168, 0x168, + 0x16A, 0x16A, + 0x16C, 0x16C, + 0x16E, 0x16E, + 0x170, 0x170, + 0x172, 0x172, + 0x174, 0x174, + 0x176, 0x176, + 0x178, 0x179, + 0x17B, 0x17B, + 0x17D, 0x17D, + 0x181, 0x182, + 0x184, 0x184, + 0x186, 0x187, + 0x189, 0x18B, + 0x18E, 0x191, + 0x193, 0x194, + 0x196, 0x198, + 0x19C, 0x19D, + 0x19F, 0x1A0, + 0x1A2, 0x1A2, + 0x1A4, 0x1A4, + 0x1A6, 0x1A7, + 0x1A9, 0x1A9, + 0x1AC, 0x1AC, + 0x1AE, 0x1AF, + 0x1B1, 0x1B3, + 0x1B5, 0x1B5, + 0x1B7, 0x1B8, + 0x1BC, 0x1BC, + 0x1C4, 0x1C5, + 0x1C7, 0x1C8, + 0x1CA, 0x1CB, + 0x1CD, 0x1CD, + 0x1CF, 0x1CF, + 0x1D1, 0x1D1, + 0x1D3, 0x1D3, + 0x1D5, 0x1D5, + 0x1D7, 0x1D7, + 0x1D9, 0x1D9, + 0x1DB, 0x1DB, + 0x1DE, 0x1DE, + 0x1E0, 0x1E0, + 0x1E2, 0x1E2, + 0x1E4, 0x1E4, + 0x1E6, 0x1E6, + 0x1E8, 0x1E8, + 0x1EA, 0x1EA, + 0x1EC, 0x1EC, + 0x1EE, 0x1EE, + 0x1F1, 0x1F2, + 0x1F4, 0x1F4, + 0x1F6, 0x1F8, + 0x1FA, 0x1FA, + 0x1FC, 0x1FC, + 0x1FE, 0x1FE, + 0x200, 0x200, + 0x202, 0x202, + 0x204, 0x204, + 0x206, 0x206, + 0x208, 0x208, + 0x20A, 0x20A, + 0x20C, 0x20C, + 0x20E, 0x20E, + 0x210, 0x210, + 0x212, 0x212, + 0x214, 0x214, + 0x216, 0x216, + 0x218, 0x218, + 0x21A, 0x21A, + 0x21C, 0x21C, + 0x21E, 0x21E, + 0x220, 0x220, + 0x222, 0x222, + 0x224, 0x224, + 0x226, 0x226, + 0x228, 0x228, + 0x22A, 0x22A, + 0x22C, 0x22C, + 0x22E, 0x22E, + 0x230, 0x230, + 0x232, 0x232, + 0x23A, 0x23B, + 0x23D, 0x23E, + 0x241, 0x241, + 0x243, 0x246, + 0x248, 0x248, + 0x24A, 0x24A, + 0x24C, 0x24C, + 0x24E, 0x24E, + 0x370, 0x370, + 0x372, 0x372, + 0x376, 0x376, + 0x37F, 0x37F, + 0x386, 0x386, + 0x388, 0x38A, + 0x38C, 0x38C, + 0x38E, 0x38F, + 0x391, 0x3A1, + 0x3A3, 0x3AB, + 0x3CF, 0x3CF, + 0x3D2, 0x3D4, + 0x3D8, 0x3D8, + 0x3DA, 0x3DA, + 0x3DC, 0x3DC, + 0x3DE, 0x3DE, + 0x3E0, 0x3E0, + 0x3E2, 0x3E2, + 0x3E4, 0x3E4, + 0x3E6, 0x3E6, + 0x3E8, 0x3E8, + 0x3EA, 0x3EA, + 0x3EC, 0x3EC, + 0x3EE, 0x3EE, + 0x3F4, 0x3F4, + 0x3F7, 0x3F7, + 0x3F9, 0x3FA, + 0x3FD, 0x42F, + 0x460, 0x460, + 0x462, 0x462, + 0x464, 0x464, + 0x466, 0x466, + 0x468, 0x468, + 0x46A, 0x46A, + 0x46C, 0x46C, + 0x46E, 0x46E, + 0x470, 0x470, + 0x472, 0x472, + 0x474, 0x474, + 0x476, 0x476, + 0x478, 0x478, + 0x47A, 0x47A, + 0x47C, 0x47C, + 0x47E, 0x47E, + 0x480, 0x480, + 0x48A, 0x48A, + 0x48C, 0x48C, + 0x48E, 0x48E, + 0x490, 0x490, + 0x492, 0x492, + 0x494, 0x494, + 0x496, 0x496, + 0x498, 0x498, + 0x49A, 0x49A, + 0x49C, 0x49C, + 0x49E, 0x49E, + 0x4A0, 0x4A0, + 0x4A2, 0x4A2, + 0x4A4, 0x4A4, + 0x4A6, 0x4A6, + 0x4A8, 0x4A8, + 0x4AA, 0x4AA, + 0x4AC, 0x4AC, + 0x4AE, 0x4AE, + 0x4B0, 0x4B0, + 0x4B2, 0x4B2, + 0x4B4, 0x4B4, + 0x4B6, 0x4B6, + 0x4B8, 0x4B8, + 0x4BA, 0x4BA, + 0x4BC, 0x4BC, + 0x4BE, 0x4BE, + 0x4C0, 0x4C1, + 0x4C3, 0x4C3, + 0x4C5, 0x4C5, + 0x4C7, 0x4C7, + 0x4C9, 0x4C9, + 0x4CB, 0x4CB, + 0x4CD, 0x4CD, + 0x4D0, 0x4D0, + 0x4D2, 0x4D2, + 0x4D4, 0x4D4, + 0x4D6, 0x4D6, + 0x4D8, 0x4D8, + 0x4DA, 0x4DA, + 0x4DC, 0x4DC, + 0x4DE, 0x4DE, + 0x4E0, 0x4E0, + 0x4E2, 0x4E2, + 0x4E4, 0x4E4, + 0x4E6, 0x4E6, + 0x4E8, 0x4E8, + 0x4EA, 0x4EA, + 0x4EC, 0x4EC, + 0x4EE, 0x4EE, + 0x4F0, 0x4F0, + 0x4F2, 0x4F2, + 0x4F4, 0x4F4, + 0x4F6, 0x4F6, + 0x4F8, 0x4F8, + 0x4FA, 0x4FA, + 0x4FC, 0x4FC, + 0x4FE, 0x4FE, + 0x500, 0x500, + 0x502, 0x502, + 0x504, 0x504, + 0x506, 0x506, + 0x508, 0x508, + 0x50A, 0x50A, + 0x50C, 0x50C, + 0x50E, 0x50E, + 0x510, 0x510, + 0x512, 0x512, + 0x514, 0x514, + 0x516, 0x516, + 0x518, 0x518, + 0x51A, 0x51A, + 0x51C, 0x51C, + 0x51E, 0x51E, + 0x520, 0x520, + 0x522, 0x522, + 0x524, 0x524, + 0x526, 0x526, + 0x528, 0x528, + 0x52A, 0x52A, + 0x52C, 0x52C, + 0x52E, 0x52E, + 0x531, 0x556, + 0x10A0, 0x10C5, + 0x10C7, 0x10C7, + 0x10CD, 0x10CD, + 0x13A0, 0x13F5, + 0x1C90, 0x1CBA, + 0x1CBD, 0x1CBF, + 0x1E00, 0x1E00, + 0x1E02, 0x1E02, + 0x1E04, 0x1E04, + 0x1E06, 0x1E06, + 0x1E08, 0x1E08, + 0x1E0A, 0x1E0A, + 0x1E0C, 0x1E0C, + 0x1E0E, 0x1E0E, + 0x1E10, 0x1E10, + 0x1E12, 0x1E12, + 0x1E14, 0x1E14, + 0x1E16, 0x1E16, + 0x1E18, 0x1E18, + 0x1E1A, 0x1E1A, + 0x1E1C, 0x1E1C, + 0x1E1E, 0x1E1E, + 0x1E20, 0x1E20, + 0x1E22, 0x1E22, + 0x1E24, 0x1E24, + 0x1E26, 0x1E26, + 0x1E28, 0x1E28, + 0x1E2A, 0x1E2A, + 0x1E2C, 0x1E2C, + 0x1E2E, 0x1E2E, + 0x1E30, 0x1E30, + 0x1E32, 0x1E32, + 0x1E34, 0x1E34, + 0x1E36, 0x1E36, + 0x1E38, 0x1E38, + 0x1E3A, 0x1E3A, + 0x1E3C, 0x1E3C, + 0x1E3E, 0x1E3E, + 0x1E40, 0x1E40, + 0x1E42, 0x1E42, + 0x1E44, 0x1E44, + 0x1E46, 0x1E46, + 0x1E48, 0x1E48, + 0x1E4A, 0x1E4A, + 0x1E4C, 0x1E4C, + 0x1E4E, 0x1E4E, + 0x1E50, 0x1E50, + 0x1E52, 0x1E52, + 0x1E54, 0x1E54, + 0x1E56, 0x1E56, + 0x1E58, 0x1E58, + 0x1E5A, 0x1E5A, + 0x1E5C, 0x1E5C, + 0x1E5E, 0x1E5E, + 0x1E60, 0x1E60, + 0x1E62, 0x1E62, + 0x1E64, 0x1E64, + 0x1E66, 0x1E66, + 0x1E68, 0x1E68, + 0x1E6A, 0x1E6A, + 0x1E6C, 0x1E6C, + 0x1E6E, 0x1E6E, + 0x1E70, 0x1E70, + 0x1E72, 0x1E72, + 0x1E74, 0x1E74, + 0x1E76, 0x1E76, + 0x1E78, 0x1E78, + 0x1E7A, 0x1E7A, + 0x1E7C, 0x1E7C, + 0x1E7E, 0x1E7E, + 0x1E80, 0x1E80, + 0x1E82, 0x1E82, + 0x1E84, 0x1E84, + 0x1E86, 0x1E86, + 0x1E88, 0x1E88, + 0x1E8A, 0x1E8A, + 0x1E8C, 0x1E8C, + 0x1E8E, 0x1E8E, + 0x1E90, 0x1E90, + 0x1E92, 0x1E92, + 0x1E94, 0x1E94, + 0x1E9E, 0x1E9E, + 0x1EA0, 0x1EA0, + 0x1EA2, 0x1EA2, + 0x1EA4, 0x1EA4, + 0x1EA6, 0x1EA6, + 0x1EA8, 0x1EA8, + 0x1EAA, 0x1EAA, + 0x1EAC, 0x1EAC, + 0x1EAE, 0x1EAE, + 0x1EB0, 0x1EB0, + 0x1EB2, 0x1EB2, + 0x1EB4, 0x1EB4, + 0x1EB6, 0x1EB6, + 0x1EB8, 0x1EB8, + 0x1EBA, 0x1EBA, + 0x1EBC, 0x1EBC, + 0x1EBE, 0x1EBE, + 0x1EC0, 0x1EC0, + 0x1EC2, 0x1EC2, + 0x1EC4, 0x1EC4, + 0x1EC6, 0x1EC6, + 0x1EC8, 0x1EC8, + 0x1ECA, 0x1ECA, + 0x1ECC, 0x1ECC, + 0x1ECE, 0x1ECE, + 0x1ED0, 0x1ED0, + 0x1ED2, 0x1ED2, + 0x1ED4, 0x1ED4, + 0x1ED6, 0x1ED6, + 0x1ED8, 0x1ED8, + 0x1EDA, 0x1EDA, + 0x1EDC, 0x1EDC, + 0x1EDE, 0x1EDE, + 0x1EE0, 0x1EE0, + 0x1EE2, 0x1EE2, + 0x1EE4, 0x1EE4, + 0x1EE6, 0x1EE6, + 0x1EE8, 0x1EE8, + 0x1EEA, 0x1EEA, + 0x1EEC, 0x1EEC, + 0x1EEE, 0x1EEE, + 0x1EF0, 0x1EF0, + 0x1EF2, 0x1EF2, + 0x1EF4, 0x1EF4, + 0x1EF6, 0x1EF6, + 0x1EF8, 0x1EF8, + 0x1EFA, 0x1EFA, + 0x1EFC, 0x1EFC, + 0x1EFE, 0x1EFE, + 0x1F08, 0x1F0F, + 0x1F18, 0x1F1D, + 0x1F28, 0x1F2F, + 0x1F38, 0x1F3F, + 0x1F48, 0x1F4D, + 0x1F59, 0x1F59, + 0x1F5B, 0x1F5B, + 0x1F5D, 0x1F5D, + 0x1F5F, 0x1F5F, + 0x1F68, 0x1F6F, + 0x1F88, 0x1F8F, + 0x1F98, 0x1F9F, + 0x1FA8, 0x1FAF, + 0x1FB8, 0x1FBC, + 0x1FC8, 0x1FCC, + 0x1FD8, 0x1FDB, + 0x1FE8, 0x1FEC, + 0x1FF8, 0x1FFC, + 0x2102, 0x2102, + 0x2107, 0x2107, + 0x210B, 0x210D, + 0x2110, 0x2112, + 0x2115, 0x2115, + 0x2119, 0x211D, + 0x2124, 0x2124, + 0x2126, 0x2126, + 0x2128, 0x2128, + 0x212A, 0x212D, + 0x2130, 0x2133, + 0x213E, 0x213F, + 0x2145, 0x2145, + 0x2160, 0x216F, + 0x2183, 0x2183, + 0x24B6, 0x24CF, + 0x2C00, 0x2C2F, + 0x2C60, 0x2C60, + 0x2C62, 0x2C64, + 0x2C67, 0x2C67, + 0x2C69, 0x2C69, + 0x2C6B, 0x2C6B, + 0x2C6D, 0x2C70, + 0x2C72, 0x2C72, + 0x2C75, 0x2C75, + 0x2C7E, 0x2C80, + 0x2C82, 0x2C82, + 0x2C84, 0x2C84, + 0x2C86, 0x2C86, + 0x2C88, 0x2C88, + 0x2C8A, 0x2C8A, + 0x2C8C, 0x2C8C, + 0x2C8E, 0x2C8E, + 0x2C90, 0x2C90, + 0x2C92, 0x2C92, + 0x2C94, 0x2C94, + 0x2C96, 0x2C96, + 0x2C98, 0x2C98, + 0x2C9A, 0x2C9A, + 0x2C9C, 0x2C9C, + 0x2C9E, 0x2C9E, + 0x2CA0, 0x2CA0, + 0x2CA2, 0x2CA2, + 0x2CA4, 0x2CA4, + 0x2CA6, 0x2CA6, + 0x2CA8, 0x2CA8, + 0x2CAA, 0x2CAA, + 0x2CAC, 0x2CAC, + 0x2CAE, 0x2CAE, + 0x2CB0, 0x2CB0, + 0x2CB2, 0x2CB2, + 0x2CB4, 0x2CB4, + 0x2CB6, 0x2CB6, + 0x2CB8, 0x2CB8, + 0x2CBA, 0x2CBA, + 0x2CBC, 0x2CBC, + 0x2CBE, 0x2CBE, + 0x2CC0, 0x2CC0, + 0x2CC2, 0x2CC2, + 0x2CC4, 0x2CC4, + 0x2CC6, 0x2CC6, + 0x2CC8, 0x2CC8, + 0x2CCA, 0x2CCA, + 0x2CCC, 0x2CCC, + 0x2CCE, 0x2CCE, + 0x2CD0, 0x2CD0, + 0x2CD2, 0x2CD2, + 0x2CD4, 0x2CD4, + 0x2CD6, 0x2CD6, + 0x2CD8, 0x2CD8, + 0x2CDA, 0x2CDA, + 0x2CDC, 0x2CDC, + 0x2CDE, 0x2CDE, + 0x2CE0, 0x2CE0, + 0x2CE2, 0x2CE2, + 0x2CEB, 0x2CEB, + 0x2CED, 0x2CED, + 0x2CF2, 0x2CF2, + 0xA640, 0xA640, + 0xA642, 0xA642, + 0xA644, 0xA644, + 0xA646, 0xA646, + 0xA648, 0xA648, + 0xA64A, 0xA64A, + 0xA64C, 0xA64C, + 0xA64E, 0xA64E, + 0xA650, 0xA650, + 0xA652, 0xA652, + 0xA654, 0xA654, + 0xA656, 0xA656, + 0xA658, 0xA658, + 0xA65A, 0xA65A, + 0xA65C, 0xA65C, + 0xA65E, 0xA65E, + 0xA660, 0xA660, + 0xA662, 0xA662, + 0xA664, 0xA664, + 0xA666, 0xA666, + 0xA668, 0xA668, + 0xA66A, 0xA66A, + 0xA66C, 0xA66C, + 0xA680, 0xA680, + 0xA682, 0xA682, + 0xA684, 0xA684, + 0xA686, 0xA686, + 0xA688, 0xA688, + 0xA68A, 0xA68A, + 0xA68C, 0xA68C, + 0xA68E, 0xA68E, + 0xA690, 0xA690, + 0xA692, 0xA692, + 0xA694, 0xA694, + 0xA696, 0xA696, + 0xA698, 0xA698, + 0xA69A, 0xA69A, + 0xA722, 0xA722, + 0xA724, 0xA724, + 0xA726, 0xA726, + 0xA728, 0xA728, + 0xA72A, 0xA72A, + 0xA72C, 0xA72C, + 0xA72E, 0xA72E, + 0xA732, 0xA732, + 0xA734, 0xA734, + 0xA736, 0xA736, + 0xA738, 0xA738, + 0xA73A, 0xA73A, + 0xA73C, 0xA73C, + 0xA73E, 0xA73E, + 0xA740, 0xA740, + 0xA742, 0xA742, + 0xA744, 0xA744, + 0xA746, 0xA746, + 0xA748, 0xA748, + 0xA74A, 0xA74A, + 0xA74C, 0xA74C, + 0xA74E, 0xA74E, + 0xA750, 0xA750, + 0xA752, 0xA752, + 0xA754, 0xA754, + 0xA756, 0xA756, + 0xA758, 0xA758, + 0xA75A, 0xA75A, + 0xA75C, 0xA75C, + 0xA75E, 0xA75E, + 0xA760, 0xA760, + 0xA762, 0xA762, + 0xA764, 0xA764, + 0xA766, 0xA766, + 0xA768, 0xA768, + 0xA76A, 0xA76A, + 0xA76C, 0xA76C, + 0xA76E, 0xA76E, + 0xA779, 0xA779, + 0xA77B, 0xA77B, + 0xA77D, 0xA77E, + 0xA780, 0xA780, + 0xA782, 0xA782, + 0xA784, 0xA784, + 0xA786, 0xA786, + 0xA78B, 0xA78B, + 0xA78D, 0xA78D, + 0xA790, 0xA790, + 0xA792, 0xA792, + 0xA796, 0xA796, + 0xA798, 0xA798, + 0xA79A, 0xA79A, + 0xA79C, 0xA79C, + 0xA79E, 0xA79E, + 0xA7A0, 0xA7A0, + 0xA7A2, 0xA7A2, + 0xA7A4, 0xA7A4, + 0xA7A6, 0xA7A6, + 0xA7A8, 0xA7A8, + 0xA7AA, 0xA7AE, + 0xA7B0, 0xA7B4, + 0xA7B6, 0xA7B6, + 0xA7B8, 0xA7B8, + 0xA7BA, 0xA7BA, + 0xA7BC, 0xA7BC, + 0xA7BE, 0xA7BE, + 0xA7C0, 0xA7C0, + 0xA7C2, 0xA7C2, + 0xA7C4, 0xA7C7, + 0xA7C9, 0xA7C9, + 0xA7D0, 0xA7D0, + 0xA7D6, 0xA7D6, + 0xA7D8, 0xA7D8, + 0xA7F5, 0xA7F5, + 0xFF21, 0xFF3A, + 0x10400, 0x10427, + 0x104B0, 0x104D3, + 0x10570, 0x1057A, + 0x1057C, 0x1058A, + 0x1058C, 0x10592, + 0x10594, 0x10595, + 0x10C80, 0x10CB2, + 0x118A0, 0x118BF, + 0x16E40, 0x16E5F, + 0x1D400, 0x1D419, + 0x1D434, 0x1D44D, + 0x1D468, 0x1D481, + 0x1D49C, 0x1D49C, + 0x1D49E, 0x1D49F, + 0x1D4A2, 0x1D4A2, + 0x1D4A5, 0x1D4A6, + 0x1D4A9, 0x1D4AC, + 0x1D4AE, 0x1D4B5, + 0x1D4D0, 0x1D4E9, + 0x1D504, 0x1D505, + 0x1D507, 0x1D50A, + 0x1D50D, 0x1D514, + 0x1D516, 0x1D51C, + 0x1D538, 0x1D539, + 0x1D53B, 0x1D53E, + 0x1D540, 0x1D544, + 0x1D546, 0x1D546, + 0x1D54A, 0x1D550, + 0x1D56C, 0x1D585, + 0x1D5A0, 0x1D5B9, + 0x1D5D4, 0x1D5ED, + 0x1D608, 0x1D621, + 0x1D63C, 0x1D655, + 0x1D670, 0x1D689, + 0x1D6A8, 0x1D6C0, + 0x1D6E2, 0x1D6FA, + 0x1D71C, 0x1D734, + 0x1D756, 0x1D76E, + 0x1D790, 0x1D7A8, + 0x1D7CA, 0x1D7CA, + 0x1E900, 0x1E921, + 0x1F130, 0x1F149, + 0x1F150, 0x1F169, + 0x1F170, 0x1F189, +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding unicode codepoint. Note that + * this table is different from other encodings where we used a lookup table + * because the indices of those tables are the byte representations, not the + * codepoints themselves. + */ +const uint8_t rbs_encoding_unicode_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 7, 7, 7, 7, 7, 0, 7, 7, 7, 7, 7, 7, 7, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3, 3, // Fx +}; + +/** + * Binary search through the given list of codepoints to see if the given + * codepoint is in the list. + */ +static bool +rbs_unicode_codepoint_match(rbs_unicode_codepoint_t codepoint, const rbs_unicode_codepoint_t *codepoints, size_t size) { + size_t start = 0; + size_t end = size; + + while (start < end) { + size_t middle = start + (end - start) / 2; + if ((middle % 2) != 0) middle--; + + if (codepoint >= codepoints[middle] && codepoint <= codepoints[middle + 1]) { + return true; + } + + if (codepoint < codepoints[middle]) { + end = middle; + } else { + start = middle + 2; + } + } + + return false; +} + +/** + * A state transition table for decoding UTF-8. + * + * Copyright (c) 2008-2009 Bjoern Hoehrmann + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +static const uint8_t rbs_utf_8_dfa[] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 00..1f + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 20..3f + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 40..5f + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 60..7f + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, // 80..9f + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, // a0..bf + 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // c0..df + 0xa,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x3,0x4,0x3,0x3, // e0..ef + 0xb,0x6,0x6,0x6,0x5,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8,0x8, // f0..ff + 0x0,0x1,0x2,0x3,0x5,0x8,0x7,0x1,0x1,0x1,0x4,0x6,0x1,0x1,0x1,0x1, // s0..s0 + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,1, // s1..s2 + 1,2,1,1,1,1,1,2,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1, // s3..s4 + 1,2,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,3,1,1,1,1,1,1, // s5..s6 + 1,3,1,1,1,1,1,3,1,3,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // s7..s8 +}; + +/** + * Given a pointer to a string and the number of bytes remaining in the string, + * decode the next UTF-8 codepoint and return it. The number of bytes consumed + * is returned in the width out parameter. + */ +static rbs_unicode_codepoint_t +rbs_utf_8_codepoint(const uint8_t *b, ptrdiff_t n, size_t *width) { + assert(n >= 0); + + size_t maximum = (n > 4) ? 4 : ((size_t) n); + uint32_t codepoint; + uint32_t state = 0; + + for (size_t index = 0; index < maximum; index++) { + uint32_t byte = b[index]; + uint32_t type = rbs_utf_8_dfa[byte]; + + codepoint = (state != 0) ? + (byte & 0x3fu) | (codepoint << 6) : + (0xffu >> type) & (byte); + + state = rbs_utf_8_dfa[256 + (state * 16) + type]; + if (state == 0) { + *width = index + 1; + return (rbs_unicode_codepoint_t) codepoint; + } + } + + *width = 0; + return 0; +} + +/** + * Return the size of the next character in the UTF-8 encoding. + */ +size_t +rbs_encoding_utf_8_char_width(const uint8_t *b, ptrdiff_t n) { + assert(n >= 0); + + size_t maximum = (n > 4) ? 4 : ((size_t) n); + uint32_t state = 0; + + for (size_t index = 0; index < maximum; index++) { + state = rbs_utf_8_dfa[256 + (state * 16) + rbs_utf_8_dfa[b[index]]]; + if (state == 0) return index + 1; + } + + return 0; +} + +/** + * Return the size of the next character in the UTF-8 encoding if it is an + * alphabetical character. + */ +size_t +rbs_encoding_utf_8_alpha_char(const uint8_t *b, ptrdiff_t n) { + if (*b < 0x80) { + return (rbs_encoding_unicode_table[*b] & RBS_ENCODING_ALPHABETIC_BIT) ? 1 : 0; + } + + size_t width; + rbs_unicode_codepoint_t codepoint = rbs_utf_8_codepoint(b, n, &width); + + if (codepoint <= 0xFF) { + return (rbs_encoding_unicode_table[(uint8_t) codepoint] & RBS_ENCODING_ALPHABETIC_BIT) ? width : 0; + } else { + return rbs_unicode_codepoint_match(codepoint, unicode_alpha_codepoints, UNICODE_ALPHA_CODEPOINTS_LENGTH) ? width : 0; + } +} + +/** + * Return the size of the next character in the UTF-8 encoding if it is an + * alphanumeric character. + */ +size_t +rbs_encoding_utf_8_alnum_char(const uint8_t *b, ptrdiff_t n) { + if (*b < 0x80) { + return (rbs_encoding_unicode_table[*b] & (RBS_ENCODING_ALPHANUMERIC_BIT)) ? 1 : 0; + } + + size_t width; + rbs_unicode_codepoint_t codepoint = rbs_utf_8_codepoint(b, n, &width); + + if (codepoint <= 0xFF) { + return (rbs_encoding_unicode_table[(uint8_t) codepoint] & (RBS_ENCODING_ALPHANUMERIC_BIT)) ? width : 0; + } else { + return rbs_unicode_codepoint_match(codepoint, unicode_alnum_codepoints, UNICODE_ALNUM_CODEPOINTS_LENGTH) ? width : 0; + } +} + +/** + * Return true if the next character in the UTF-8 encoding if it is an uppercase + * character. + */ +bool +rbs_encoding_utf_8_isupper_char(const uint8_t *b, ptrdiff_t n) { + if (*b < 0x80) { + return (rbs_encoding_unicode_table[*b] & RBS_ENCODING_UPPERCASE_BIT) ? true : false; + } + + size_t width; + rbs_unicode_codepoint_t codepoint = rbs_utf_8_codepoint(b, n, &width); + + if (codepoint <= 0xFF) { + return (rbs_encoding_unicode_table[(uint8_t) codepoint] & RBS_ENCODING_UPPERCASE_BIT) ? true : false; + } else { + return rbs_unicode_codepoint_match(codepoint, unicode_isupper_codepoints, UNICODE_ISUPPER_CODEPOINTS_LENGTH) ? true : false; + } +} + +#ifndef RBS_ENCODING_EXCLUDE_FULL + +static rbs_unicode_codepoint_t +rbs_cesu_8_codepoint(const uint8_t *b, ptrdiff_t n, size_t *width) { + if (b[0] < 0x80) { + *width = 1; + return (rbs_unicode_codepoint_t) b[0]; + } + + if (n > 1 && b[0] >= 0xC2 && b[0] <= 0xDF && b[1] >= 0x80 && b[1] <= 0xBF) { + *width = 2; + + // 110xxxxx 10xxxxxx + return (rbs_unicode_codepoint_t) (((b[0] & 0x1F) << 6) | (b[1] & 0x3F)); + } + + if (n > 5 && b[0] == 0xED && b[1] >= 0xA0 && b[1] <= 0xAF && b[2] >= 0x80 && b[2] <= 0xBF && b[3] == 0xED && b[4] >= 0xB0 && b[4] <= 0xBF && b[5] >= 0x80 && b[5] <= 0xBF) { + *width = 6; + + // 11101101 1010xxxx 10xxxxxx 11101101 1011xxxx 10xxxxxx + return (rbs_unicode_codepoint_t) (0x10000 + (((b[1] & 0xF) << 16) | ((b[2] & 0x3F) << 10) | ((b[4] & 0xF) << 6) | (b[5] & 0x3F))); + } + + if (n > 2 && b[0] == 0xED && b[1] >= 0xA0 && b[1] <= 0xBF) { + *width = 3; + + // 11101101 1010xxxx 10xxxxx + return (rbs_unicode_codepoint_t) (0x10000 + (((b[0] & 0x03) << 16) | ((b[1] & 0x3F) << 10) | (b[2] & 0x3F))); + } + + if (n > 2 && ((b[0] == 0xE0 && b[1] >= 0xA0) || (b[0] >= 0xE1 && b[0] <= 0xEF && b[1] >= 0x80)) && b[1] <= 0xBF && b[2] >= 0x80 && b[2] <= 0xBF) { + *width = 3; + + // 1110xxxx 10xxxxxx 10xxxxx + return (rbs_unicode_codepoint_t) (((b[0] & 0xF) << 12) | ((b[1] & 0x3F) << 6) | (b[2] & 0x3F)); + } + + *width = 0; + return 0; +} + +static size_t +rbs_encoding_cesu_8_char_width(const uint8_t *b, ptrdiff_t n) { + size_t width; + rbs_cesu_8_codepoint(b, n, &width); + return width; +} + +static size_t +rbs_encoding_cesu_8_alpha_char(const uint8_t *b, ptrdiff_t n) { + if (*b < 0x80) { + return (rbs_encoding_unicode_table[*b] & RBS_ENCODING_ALPHABETIC_BIT) ? 1 : 0; + } + + size_t width; + rbs_unicode_codepoint_t codepoint = rbs_cesu_8_codepoint(b, n, &width); + + if (codepoint <= 0xFF) { + return (rbs_encoding_unicode_table[(uint8_t) codepoint] & RBS_ENCODING_ALPHABETIC_BIT) ? width : 0; + } else { + return rbs_unicode_codepoint_match(codepoint, unicode_alpha_codepoints, UNICODE_ALPHA_CODEPOINTS_LENGTH) ? width : 0; + } +} + +static size_t +rbs_encoding_cesu_8_alnum_char(const uint8_t *b, ptrdiff_t n) { + if (*b < 0x80) { + return (rbs_encoding_unicode_table[*b] & (RBS_ENCODING_ALPHANUMERIC_BIT)) ? 1 : 0; + } + + size_t width; + rbs_unicode_codepoint_t codepoint = rbs_cesu_8_codepoint(b, n, &width); + + if (codepoint <= 0xFF) { + return (rbs_encoding_unicode_table[(uint8_t) codepoint] & (RBS_ENCODING_ALPHANUMERIC_BIT)) ? width : 0; + } else { + return rbs_unicode_codepoint_match(codepoint, unicode_alnum_codepoints, UNICODE_ALNUM_CODEPOINTS_LENGTH) ? width : 0; + } +} + +static bool +rbs_encoding_cesu_8_isupper_char(const uint8_t *b, ptrdiff_t n) { + if (*b < 0x80) { + return (rbs_encoding_unicode_table[*b] & RBS_ENCODING_UPPERCASE_BIT) ? true : false; + } + + size_t width; + rbs_unicode_codepoint_t codepoint = rbs_cesu_8_codepoint(b, n, &width); + + if (codepoint <= 0xFF) { + return (rbs_encoding_unicode_table[(uint8_t) codepoint] & RBS_ENCODING_UPPERCASE_BIT) ? true : false; + } else { + return rbs_unicode_codepoint_match(codepoint, unicode_isupper_codepoints, UNICODE_ISUPPER_CODEPOINTS_LENGTH) ? true : false; + } +} + +#endif + +#undef UNICODE_ALPHA_CODEPOINTS_LENGTH +#undef UNICODE_ALNUM_CODEPOINTS_LENGTH +#undef UNICODE_ISUPPER_CODEPOINTS_LENGTH + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding US-ASCII character. + */ +static const uint8_t rbs_encoding_ascii_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +#ifndef RBS_ENCODING_EXCLUDE_FULL + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding CP850 character. + */ +static const uint8_t rbs_encoding_cp850_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding CP852 character. + */ +static const uint8_t rbs_encoding_cp852_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding CP855 character. + */ +static const uint8_t rbs_encoding_cp855_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding GB1988 character. + */ +static const uint8_t rbs_encoding_gb1988_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding IBM437 character. + */ +static const uint8_t rbs_encoding_ibm437_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding IBM720 character. + */ +static const uint8_t rbs_encoding_ibm720_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding IBM737 character. + */ +static const uint8_t rbs_encoding_ibm737_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding IBM775 character. + */ +static const uint8_t rbs_encoding_ibm775_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding IBM852 character. + */ +static const uint8_t rbs_encoding_ibm852_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding IBM855 character. + */ +static const uint8_t rbs_encoding_ibm855_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding IBM857 character. + */ +static const uint8_t rbs_encoding_ibm857_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding IBM860 character. + */ +static const uint8_t rbs_encoding_ibm860_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding IBM861 character. + */ +static const uint8_t rbs_encoding_ibm861_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding IBM862 character. + */ +static const uint8_t rbs_encoding_ibm862_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding IBM863 character. + */ +static const uint8_t rbs_encoding_ibm863_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding IBM864 character. + */ +static const uint8_t rbs_encoding_ibm864_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding IBM865 character. + */ +static const uint8_t rbs_encoding_ibm865_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding IBM866 character. + */ +static const uint8_t rbs_encoding_ibm866_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding IBM869 character. + */ +static const uint8_t rbs_encoding_ibm869_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding ISO-8859-1 character. + */ +static const uint8_t rbs_encoding_iso_8859_1_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 7, 7, 7, 7, 7, 0, 7, 7, 7, 7, 7, 7, 7, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3, 3, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding ISO-8859-2 character. + */ +static const uint8_t rbs_encoding_iso_8859_2_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 7, 0, 7, 0, 7, 7, 0, 0, 7, 7, 7, 7, 0, 7, 7, // Ax + 0, 3, 0, 3, 0, 3, 3, 0, 0, 3, 3, 3, 3, 0, 3, 3, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 7, 7, 7, 7, 7, 0, 7, 7, 7, 7, 7, 7, 7, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding ISO-8859-3 character. + */ +static const uint8_t rbs_encoding_iso_8859_3_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 7, 0, 0, 0, 0, 7, 0, 0, 7, 7, 7, 7, 0, 0, 7, // Ax + 0, 3, 0, 0, 0, 3, 3, 0, 0, 3, 3, 3, 3, 0, 0, 3, // Bx + 7, 7, 7, 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 0, 7, 7, 7, 7, 7, 7, 0, 7, 7, 7, 7, 7, 7, 7, 3, // Dx + 3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 0, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding ISO-8859-4 character. + */ +static const uint8_t rbs_encoding_iso_8859_4_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 7, 3, 7, 0, 7, 7, 0, 0, 7, 7, 7, 7, 0, 7, 0, // Ax + 0, 3, 0, 3, 0, 3, 3, 0, 0, 3, 3, 3, 3, 7, 3, 3, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 7, 7, 7, 7, 7, 0, 7, 7, 7, 7, 7, 7, 7, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding ISO-8859-5 character. + */ +static const uint8_t rbs_encoding_iso_8859_5_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 7, 7, // Ax + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 3, 3, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding ISO-8859-6 character. + */ +static const uint8_t rbs_encoding_iso_8859_6_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Cx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding ISO-8859-7 character. + */ +static const uint8_t rbs_encoding_iso_8859_7_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 7, 0, 7, 7, 7, 0, 7, 0, 7, 7, // Bx + 3, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 3, 3, 3, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding ISO-8859-8 character. + */ +static const uint8_t rbs_encoding_iso_8859_8_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding ISO-8859-9 character. + */ +static const uint8_t rbs_encoding_iso_8859_9_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 7, 7, 7, 7, 7, 0, 7, 7, 7, 7, 7, 7, 7, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3, 3, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding ISO-8859-10 character. + */ +static const uint8_t rbs_encoding_iso_8859_10_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 7, 7, 7, 7, 7, 7, 0, 7, 7, 7, 7, 7, 0, 7, 7, // Ax + 0, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 0, 3, 3, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding ISO-8859-11 character. + */ +static const uint8_t rbs_encoding_iso_8859_11_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ax + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Bx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Cx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding ISO-8859-13 character. + */ +static const uint8_t rbs_encoding_iso_8859_13_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 7, 0, 0, 0, 0, 7, // Ax + 0, 0, 0, 0, 0, 3, 0, 0, 3, 0, 3, 0, 0, 0, 0, 3, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 7, 7, 7, 7, 7, 0, 7, 7, 7, 7, 7, 7, 7, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding ISO-8859-14 character. + */ +static const uint8_t rbs_encoding_iso_8859_14_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 7, 3, 0, 7, 3, 7, 0, 7, 0, 7, 3, 7, 0, 0, 7, // Ax + 7, 3, 7, 3, 7, 3, 0, 7, 3, 3, 3, 7, 3, 7, 3, 3, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding ISO-8859-15 character. + */ +static const uint8_t rbs_encoding_iso_8859_15_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 7, 0, 3, 0, 3, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 7, 3, 0, 0, 3, 0, 3, 0, 7, 3, 7, 0, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 7, 7, 7, 7, 7, 0, 7, 7, 7, 7, 7, 7, 7, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3, 3, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding ISO-8859-16 character. + */ +static const uint8_t rbs_encoding_iso_8859_16_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 7, 3, 7, 0, 0, 7, 0, 3, 0, 7, 0, 7, 0, 3, 7, // Ax + 0, 0, 7, 3, 7, 0, 0, 0, 3, 3, 3, 0, 7, 3, 7, 3, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding KOI8-R character. + */ +static const uint8_t rbs_encoding_koi8_r_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Cx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Dx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Ex + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding KOI8-U character. + */ +static const uint8_t rbs_encoding_koi8_u_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 3, 3, 0, 3, 3, 0, 0, 0, 0, 0, 3, 0, 0, // Ax + 0, 0, 0, 7, 7, 0, 7, 7, 0, 0, 0, 0, 0, 7, 0, 0, // Bx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Cx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Dx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Ex + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding macCentEuro character. + */ +static const uint8_t rbs_encoding_mac_cent_euro_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding macCroatian character. + */ +static const uint8_t rbs_encoding_mac_croatian_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + + /** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding macCyrillic character. + */ +static const uint8_t rbs_encoding_mac_cyrillic_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding macGreek character. + */ +static const uint8_t rbs_encoding_mac_greek_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding macIceland character. + */ +static const uint8_t rbs_encoding_mac_iceland_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding macRoman character. + */ +static const uint8_t rbs_encoding_mac_roman_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding macRomania character. + */ +static const uint8_t rbs_encoding_mac_romania_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding macThai character. + */ +static const uint8_t rbs_encoding_mac_thai_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding TIS-620 character. + */ +static const uint8_t rbs_encoding_tis_620_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ax + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Bx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Cx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding macTurkish character. + */ +static const uint8_t rbs_encoding_mac_turkish_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding macUkraine character. + */ +static const uint8_t rbs_encoding_mac_ukraine_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding windows-1250 character. + */ +static const uint8_t rbs_encoding_windows_1250_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 7, 7, 7, 7, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 3, 3, 3, // 9x + 0, 0, 0, 7, 0, 7, 0, 0, 0, 0, 7, 0, 0, 0, 0, 7, // Ax + 0, 0, 0, 3, 0, 3, 0, 0, 0, 3, 3, 0, 7, 0, 3, 3, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding windows-1251 character. + */ +static const uint8_t rbs_encoding_windows_1251_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 7, 7, 0, 3, 0, 0, 0, 0, 0, 0, 7, 0, 7, 7, 7, 7, // 8x + 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 3, 3, 3, // 9x + 0, 7, 3, 7, 0, 7, 0, 0, 7, 0, 7, 0, 0, 0, 0, 7, // Ax + 0, 0, 7, 3, 3, 3, 0, 0, 3, 0, 3, 0, 3, 7, 3, 3, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding windows-1252 character. + */ +static const uint8_t rbs_encoding_windows_1252_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 7, 0, 7, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0, 3, 7, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3, 3, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding windows-1253 character. + */ +static const uint8_t rbs_encoding_windows_1253_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 3, 7, 0, 7, 7, 7, 0, 7, 0, 7, 7, // Bx + 3, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 3, 3, 3, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding windows-1254 character. + */ +static const uint8_t rbs_encoding_windows_1254_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 7, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0, 0, 7, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 7, 7, 7, 7, 7, 0, 7, 7, 7, 7, 7, 7, 7, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3, 3, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding windows-1255 character. + */ +static const uint8_t rbs_encoding_windows_1255_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding windows-1256 character. + */ +static const uint8_t rbs_encoding_windows_1256_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Cx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding windows-1257 character. + */ +static const uint8_t rbs_encoding_windows_1257_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 7, 0, 0, 0, 0, 7, // Ax + 0, 0, 0, 0, 0, 3, 0, 0, 3, 0, 3, 0, 0, 0, 0, 3, // Bx + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // Cx + 7, 7, 7, 7, 7, 7, 7, 0, 7, 7, 7, 7, 7, 7, 7, 3, // Dx + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // Ex + 3, 3, 3, 3, 3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding windows-1258 character. + */ +static const uint8_t rbs_encoding_windows_1258_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +/** + * Each element of the following table contains a bitfield that indicates a + * piece of information about the corresponding windows-874 character. + */ +static const uint8_t rbs_encoding_windows_874_table[256] = { +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 2x + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, // 3x + 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 4x + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, // 5x + 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 6x + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, // 7x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ax + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Bx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Cx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Dx + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Ex + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Fx +}; + +#define RBS_ENCODING_TABLE(name) \ + static size_t rbs_encoding_ ##name ## _alpha_char(const uint8_t *b, RBS_ATTRIBUTE_UNUSED ptrdiff_t n) { \ + return (rbs_encoding_ ##name ## _table[*b] & RBS_ENCODING_ALPHABETIC_BIT); \ + } \ + static size_t rbs_encoding_ ##name ## _alnum_char(const uint8_t *b, RBS_ATTRIBUTE_UNUSED ptrdiff_t n) { \ + return (rbs_encoding_ ##name ## _table[*b] & RBS_ENCODING_ALPHANUMERIC_BIT) ? 1 : 0; \ + } \ + static bool rbs_encoding_ ##name ## _isupper_char(const uint8_t *b, RBS_ATTRIBUTE_UNUSED ptrdiff_t n) { \ + return (rbs_encoding_ ##name ## _table[*b] & RBS_ENCODING_UPPERCASE_BIT); \ + } + +RBS_ENCODING_TABLE(cp850) +RBS_ENCODING_TABLE(cp852) +RBS_ENCODING_TABLE(cp855) +RBS_ENCODING_TABLE(gb1988) +RBS_ENCODING_TABLE(ibm437) +RBS_ENCODING_TABLE(ibm720) +RBS_ENCODING_TABLE(ibm737) +RBS_ENCODING_TABLE(ibm775) +RBS_ENCODING_TABLE(ibm852) +RBS_ENCODING_TABLE(ibm855) +RBS_ENCODING_TABLE(ibm857) +RBS_ENCODING_TABLE(ibm860) +RBS_ENCODING_TABLE(ibm861) +RBS_ENCODING_TABLE(ibm862) +RBS_ENCODING_TABLE(ibm863) +RBS_ENCODING_TABLE(ibm864) +RBS_ENCODING_TABLE(ibm865) +RBS_ENCODING_TABLE(ibm866) +RBS_ENCODING_TABLE(ibm869) +RBS_ENCODING_TABLE(iso_8859_1) +RBS_ENCODING_TABLE(iso_8859_2) +RBS_ENCODING_TABLE(iso_8859_3) +RBS_ENCODING_TABLE(iso_8859_4) +RBS_ENCODING_TABLE(iso_8859_5) +RBS_ENCODING_TABLE(iso_8859_6) +RBS_ENCODING_TABLE(iso_8859_7) +RBS_ENCODING_TABLE(iso_8859_8) +RBS_ENCODING_TABLE(iso_8859_9) +RBS_ENCODING_TABLE(iso_8859_10) +RBS_ENCODING_TABLE(iso_8859_11) +RBS_ENCODING_TABLE(iso_8859_13) +RBS_ENCODING_TABLE(iso_8859_14) +RBS_ENCODING_TABLE(iso_8859_15) +RBS_ENCODING_TABLE(iso_8859_16) +RBS_ENCODING_TABLE(koi8_r) +RBS_ENCODING_TABLE(koi8_u) +RBS_ENCODING_TABLE(mac_cent_euro) +RBS_ENCODING_TABLE(mac_croatian) +RBS_ENCODING_TABLE(mac_cyrillic) +RBS_ENCODING_TABLE(mac_greek) +RBS_ENCODING_TABLE(mac_iceland) +RBS_ENCODING_TABLE(mac_roman) +RBS_ENCODING_TABLE(mac_romania) +RBS_ENCODING_TABLE(mac_thai) +RBS_ENCODING_TABLE(mac_turkish) +RBS_ENCODING_TABLE(mac_ukraine) +RBS_ENCODING_TABLE(tis_620) +RBS_ENCODING_TABLE(windows_1250) +RBS_ENCODING_TABLE(windows_1251) +RBS_ENCODING_TABLE(windows_1252) +RBS_ENCODING_TABLE(windows_1253) +RBS_ENCODING_TABLE(windows_1254) +RBS_ENCODING_TABLE(windows_1255) +RBS_ENCODING_TABLE(windows_1256) +RBS_ENCODING_TABLE(windows_1257) +RBS_ENCODING_TABLE(windows_1258) +RBS_ENCODING_TABLE(windows_874) + +#undef RBS_ENCODING_TABLE +#endif + +/** + * Returns the size of the next character in the ASCII encoding. This basically + * means that if the top bit is not set, the character is 1 byte long. + */ +static size_t +rbs_encoding_ascii_char_width(const uint8_t *b, RBS_ATTRIBUTE_UNUSED ptrdiff_t n) { + return *b < 0x80 ? 1 : 0; +} + +/** + * Return the size of the next character in the ASCII encoding if it is an + * alphabetical character. + */ +static size_t +rbs_encoding_ascii_alpha_char(const uint8_t *b, RBS_ATTRIBUTE_UNUSED ptrdiff_t n) { + return (rbs_encoding_ascii_table[*b] & RBS_ENCODING_ALPHABETIC_BIT); +} + +/** + * Certain encodings are equivalent to ASCII below 0x80, so it works for our + * purposes to have a function here that first checks the bounds and then falls + * back to checking the ASCII lookup table. + */ +static size_t +rbs_encoding_ascii_alpha_char_7bit(const uint8_t *b, ptrdiff_t n) { + return (*b < 0x80) ? rbs_encoding_ascii_alpha_char(b, n) : 0; +} + +/** + * Return the size of the next character in the ASCII encoding if it is an + * alphanumeric character. + */ +static size_t +rbs_encoding_ascii_alnum_char(const uint8_t *b, RBS_ATTRIBUTE_UNUSED ptrdiff_t n) { + return (rbs_encoding_ascii_table[*b] & RBS_ENCODING_ALPHANUMERIC_BIT) ? 1 : 0; +} + +/** + * Certain encodings are equivalent to ASCII below 0x80, so it works for our + * purposes to have a function here that first checks the bounds and then falls + * back to checking the ASCII lookup table. + */ +static size_t +rbs_encoding_ascii_alnum_char_7bit(const uint8_t *b, ptrdiff_t n) { + return (*b < 0x80) ? rbs_encoding_ascii_alnum_char(b, n) : 0; +} + +/** + * Return true if the next character in the ASCII encoding if it is an uppercase + * character. + */ +static bool +rbs_encoding_ascii_isupper_char(const uint8_t *b, RBS_ATTRIBUTE_UNUSED ptrdiff_t n) { + return (rbs_encoding_ascii_table[*b] & RBS_ENCODING_UPPERCASE_BIT); +} + +/** + * For a lot of encodings the default is that they are a single byte long no + * matter what the codepoint, so this function is shared between them. + */ +static size_t +rbs_encoding_single_char_width(RBS_ATTRIBUTE_UNUSED const uint8_t *b, RBS_ATTRIBUTE_UNUSED ptrdiff_t n) { + return 1; +} + +/** + * Returns the size of the next character in the EUC-JP encoding, or 0 if a + * character cannot be decoded from the given bytes. + */ +static size_t +rbs_encoding_euc_jp_char_width(const uint8_t *b, ptrdiff_t n) { + // These are the single byte characters. + if (*b < 0x80) { + return 1; + } + + // These are the double byte characters. + if ((n > 1) && ((b[0] == 0x8E) || (b[0] >= 0xA1 && b[0] <= 0xFE)) && (b[1] >= 0xA1 && b[1] <= 0xFE)) { + return 2; + } + + // These are the triple byte characters. + if ((n > 2) && (b[0] == 0x8F) && (b[1] >= 0xA1 && b[2] <= 0xFE) && (b[2] >= 0xA1 && b[2] <= 0xFE)) { + return 3; + } + + return 0; +} + +/** + * Returns the size of the next character in the EUC-JP encoding if it is an + * uppercase character. + */ +static bool +rbs_encoding_euc_jp_isupper_char(const uint8_t *b, ptrdiff_t n) { + size_t width = rbs_encoding_euc_jp_char_width(b, n); + + if (width == 1) { + return rbs_encoding_ascii_isupper_char(b, n); + } else if (width == 2) { + return ( + (b[0] == 0xA3 && b[1] >= 0xC1 && b[1] <= 0xDA) || + (b[0] == 0xA6 && b[1] >= 0xA1 && b[1] <= 0xB8) || + (b[0] == 0xA7 && b[1] >= 0xA1 && b[1] <= 0xC1) + ); + } else { + return false; + } +} + +/** + * Returns the size of the next character in the Shift_JIS encoding, or 0 if a + * character cannot be decoded from the given bytes. + */ +static size_t +rbs_encoding_shift_jis_char_width(const uint8_t *b, ptrdiff_t n) { + // These are the single byte characters. + if (b[0] < 0x80 || (b[0] >= 0xA1 && b[0] <= 0xDF)) { + return 1; + } + + // These are the double byte characters. + if ((n > 1) && ((b[0] >= 0x81 && b[0] <= 0x9F) || (b[0] >= 0xE0 && b[0] <= 0xFC)) && (b[1] >= 0x40 && b[1] <= 0xFC && b[1] != 0x7F)) { + return 2; + } + + return 0; +} + +/** + * Returns the size of the next character in the Shift_JIS encoding if it is an + * alphanumeric character. + */ +static size_t +rbs_encoding_shift_jis_alnum_char(const uint8_t *b, ptrdiff_t n) { + size_t width = rbs_encoding_shift_jis_char_width(b, n); + return width == 1 ? ((b[0] >= 0x80) || rbs_encoding_ascii_alnum_char(b, n)) : width; +} + +/** + * Returns the size of the next character in the Shift_JIS encoding if it is an + * alphabetical character. + */ +static size_t +rbs_encoding_shift_jis_alpha_char(const uint8_t *b, ptrdiff_t n) { + size_t width = rbs_encoding_shift_jis_char_width(b, n); + return width == 1 ? ((b[0] >= 0x80) || rbs_encoding_ascii_alpha_char(b, n)) : width; +} + +/** + * Returns the size of the next character in the Shift_JIS encoding if it is an + * uppercase character. + */ +static bool +rbs_encoding_shift_jis_isupper_char(const uint8_t *b, ptrdiff_t n) { + size_t width = rbs_encoding_shift_jis_char_width(b, n); + + if (width == 1) { + return rbs_encoding_ascii_isupper_char(b, n); + } else if (width == 2) { + return ( + ((b[0] == 0x82) && (b[1] >= 0x60 && b[1] <= 0x79)) || + ((b[0] == 0x83) && (b[1] >= 0x9F && b[1] <= 0xB6)) || + ((b[0] == 0x84) && (b[1] >= 0x40 && b[1] <= 0x60)) + ); + } else { + return width; + } +} + +#ifndef RBS_ENCODING_EXCLUDE_FULL + +/** + * Certain encodings are equivalent to ASCII below 0x80, so it works for our + * purposes to have a function here that first checks the bounds and then falls + * back to checking the ASCII lookup table. + */ +static bool +rbs_encoding_ascii_isupper_char_7bit(const uint8_t *b, ptrdiff_t n) { + return (*b < 0x80) && rbs_encoding_ascii_isupper_char(b, n); +} + +/** + * Returns the size of the next character in the Big5 encoding, or 0 if a + * character cannot be decoded from the given bytes. + */ +static size_t +rbs_encoding_big5_char_width(const uint8_t *b, ptrdiff_t n) { + // These are the single byte characters. + if (*b < 0x80) { + return 1; + } + + // These are the double byte characters. + if ((n > 1) && (b[0] >= 0xA1 && b[0] <= 0xFE) && ((b[1] >= 0x40 && b[1] <= 0x7E) || (b[1] >= 0xA1 && b[1] <= 0xFE))) { + return 2; + } + + return 0; +} + +/** + * Returns the size of the next character in the CP949 encoding, or 0 if a + * character cannot be decoded from the given bytes. + */ +static size_t +rbs_encoding_cp949_char_width(const uint8_t *b, ptrdiff_t n) { + // These are the single byte characters + if (*b <= 0x80) { + return 1; + } + + // These are the double byte characters + if ((n > 1) && (b[0] >= 0x81 && b[0] <= 0xFE) && ((b[1] >= 0x41 && b[1] <= 0x5A) || (b[1] >= 0x61 && b[1] <= 0x7A) || (b[1] >= 0x81 && b[1] <= 0xFE))) { + return 2; + } + + return 0; +} + +/** + * Returns the size of the next character in the Emacs MULE encoding, or 0 if a + * character cannot be decoded from the given bytes. + */ +static size_t +rbs_encoding_emacs_mule_char_width(const uint8_t *b, ptrdiff_t n) { + // These are the 1 byte characters. + if (*b < 0x80) { + return 1; + } + + // These are the 2 byte characters. + if ((n > 1) && (b[0] >= 0x81 && b[0] <= 0x8F) && (b[1] >= 0xA0)) { + return 2; + } + + // These are the 3 byte characters. + if ( + (n > 2) && + ( + ((b[0] >= 0x90 && b[0] <= 0x99) && (b[1] >= 0xA0)) || + ((b[0] == 0x9A || b[0] == 0x9B) && (b[1] >= 0xE0 && b[1] <= 0xEF)) + ) && + (b[2] >= 0xA0) + ) { + return 3; + } + + // These are the 4 byte characters. + if ( + (n > 3) && + ( + ((b[0] == 0x9C) && (b[1] >= 0xF0) && (b[1] <= 0xF4)) || + ((b[0] == 0x9D) && (b[1] >= 0xF5) && (b[1] <= 0xFE)) + ) && + (b[2] >= 0xA0) && (b[3] >= 0xA0) + ) { + return 4; + } + + return 0; +} + +/** + * Returns the size of the next character in the EUC-KR encoding, or 0 if a + * character cannot be decoded from the given bytes. + */ +static size_t +rbs_encoding_euc_kr_char_width(const uint8_t *b, ptrdiff_t n) { + // These are the single byte characters. + if (*b < 0x80) { + return 1; + } + + // These are the double byte characters. + if ((n > 1) && (b[0] >= 0xA1 && b[0] <= 0xFE) && (b[1] >= 0xA1 && b[1] <= 0xFE)) { + return 2; + } + + return 0; +} + +/** + * Returns the size of the next character in the EUC-TW encoding, or 0 if a + * character cannot be decoded from the given bytes. + */ +static size_t +rbs_encoding_euc_tw_char_width(const uint8_t *b, ptrdiff_t n) { + // These are the single byte characters. + if (*b < 0x80) { + return 1; + } + + // These are the double byte characters. + if ((n > 1) && (b[0] >= 0xA1) && (b[0] <= 0xFE) && (b[1] >= 0xA1) && (b[1] <= 0xFE)) { + return 2; + } + + // These are the quadruple byte characters. + if ((n > 3) && (b[0] == 0x8E) && (b[1] >= 0xA1) && (b[1] <= 0xB0) && (b[2] >= 0xA1) && (b[2] <= 0xFE) && (b[3] >= 0xA1) && (b[3] <= 0xFE)) { + return 4; + } + + return 0; +} + +/** + * Returns the size of the next character in the GB18030 encoding, or 0 if a + * character cannot be decoded from the given bytes. + */ +static size_t +rbs_encoding_gb18030_char_width(const uint8_t *b, ptrdiff_t n) { + // These are the 1 byte characters. + if (*b < 0x80) { + return 1; + } + + // These are the 2 byte characters. + if ((n > 1) && (b[0] >= 0x81 && b[0] <= 0xFE) && (b[1] >= 0x40 && b[1] <= 0xFE && b[1] != 0x7F)) { + return 2; + } + + // These are the 4 byte characters. + if ((n > 3) && ((b[0] >= 0x81 && b[0] <= 0xFE) && (b[1] >= 0x30 && b[1] <= 0x39) && (b[2] >= 0x81 && b[2] <= 0xFE) && (b[3] >= 0x30 && b[3] <= 0x39))) { + return 4; + } + + return 0; +} + +/** + * Returns the size of the next character in the GBK encoding, or 0 if a + * character cannot be decoded from the given bytes. + */ +static size_t +rbs_encoding_gbk_char_width(const uint8_t *b, ptrdiff_t n) { + // These are the single byte characters. + if (*b <= 0x80) { + return 1; + } + + // These are the double byte characters. + if ( + (n > 1) && + ( + ((b[0] >= 0xA1 && b[0] <= 0xA9) && (b[1] >= 0xA1 && b[1] <= 0xFE)) || // GBK/1 + ((b[0] >= 0xB0 && b[0] <= 0xF7) && (b[1] >= 0xA1 && b[1] <= 0xFE)) || // GBK/2 + ((b[0] >= 0x81 && b[0] <= 0xA0) && (b[1] >= 0x40 && b[1] <= 0xFE) && (b[1] != 0x7F)) || // GBK/3 + ((b[0] >= 0xAA && b[0] <= 0xFE) && (b[1] >= 0x40 && b[1] <= 0xA0) && (b[1] != 0x7F)) || // GBK/4 + ((b[0] >= 0xA8 && b[0] <= 0xA9) && (b[1] >= 0x40 && b[1] <= 0xA0) && (b[1] != 0x7F)) || // GBK/5 + ((b[0] >= 0xAA && b[0] <= 0xAF) && (b[1] >= 0xA1 && b[1] <= 0xFE)) || // user-defined 1 + ((b[0] >= 0xF8 && b[0] <= 0xFE) && (b[1] >= 0xA1 && b[1] <= 0xFE)) || // user-defined 2 + ((b[0] >= 0xA1 && b[0] <= 0xA7) && (b[1] >= 0x40 && b[1] <= 0xA0) && (b[1] != 0x7F)) // user-defined 3 + ) + ) { + return 2; + } + + return 0; +} + +#endif + +/** + * This is the table of all of the encodings that prism supports. + */ +const rbs_encoding_t rbs_encodings[] = { + [RBS_ENCODING_UTF_8] = { + .name = "UTF-8", + .char_width = rbs_encoding_utf_8_char_width, + .alnum_char = rbs_encoding_utf_8_alnum_char, + .alpha_char = rbs_encoding_utf_8_alpha_char, + .isupper_char = rbs_encoding_utf_8_isupper_char, + .multibyte = true + }, + [RBS_ENCODING_US_ASCII] = { + .name = "US-ASCII", + .char_width = rbs_encoding_ascii_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char, + .alpha_char = rbs_encoding_ascii_alpha_char, + .isupper_char = rbs_encoding_ascii_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ASCII_8BIT] = { + .name = "ASCII-8BIT", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char, + .alpha_char = rbs_encoding_ascii_alpha_char, + .isupper_char = rbs_encoding_ascii_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_EUC_JP] = { + .name = "EUC-JP", + .char_width = rbs_encoding_euc_jp_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_euc_jp_isupper_char, + .multibyte = true + }, + [RBS_ENCODING_WINDOWS_31J] = { + .name = "Windows-31J", + .char_width = rbs_encoding_shift_jis_char_width, + .alnum_char = rbs_encoding_shift_jis_alnum_char, + .alpha_char = rbs_encoding_shift_jis_alpha_char, + .isupper_char = rbs_encoding_shift_jis_isupper_char, + .multibyte = true + }, + +#ifndef RBS_ENCODING_EXCLUDE_FULL + [RBS_ENCODING_BIG5] = { + .name = "Big5", + .char_width = rbs_encoding_big5_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_ascii_isupper_char_7bit, + .multibyte = true + }, + [RBS_ENCODING_BIG5_HKSCS] = { + .name = "Big5-HKSCS", + .char_width = rbs_encoding_big5_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_ascii_isupper_char_7bit, + .multibyte = true + }, + [RBS_ENCODING_BIG5_UAO] = { + .name = "Big5-UAO", + .char_width = rbs_encoding_big5_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_ascii_isupper_char_7bit, + .multibyte = true + }, + [RBS_ENCODING_CESU_8] = { + .name = "CESU-8", + .char_width = rbs_encoding_cesu_8_char_width, + .alnum_char = rbs_encoding_cesu_8_alnum_char, + .alpha_char = rbs_encoding_cesu_8_alpha_char, + .isupper_char = rbs_encoding_cesu_8_isupper_char, + .multibyte = true + }, + [RBS_ENCODING_CP51932] = { + .name = "CP51932", + .char_width = rbs_encoding_euc_jp_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_euc_jp_isupper_char, + .multibyte = true + }, + [RBS_ENCODING_CP850] = { + .name = "CP850", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_cp850_alnum_char, + .alpha_char = rbs_encoding_cp850_alpha_char, + .isupper_char = rbs_encoding_cp850_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_CP852] = { + .name = "CP852", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_cp852_alnum_char, + .alpha_char = rbs_encoding_cp852_alpha_char, + .isupper_char = rbs_encoding_cp852_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_CP855] = { + .name = "CP855", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_cp855_alnum_char, + .alpha_char = rbs_encoding_cp855_alpha_char, + .isupper_char = rbs_encoding_cp855_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_CP949] = { + .name = "CP949", + .char_width = rbs_encoding_cp949_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_ascii_isupper_char_7bit, + .multibyte = true + }, + [RBS_ENCODING_CP950] = { + .name = "CP950", + .char_width = rbs_encoding_big5_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_ascii_isupper_char_7bit, + .multibyte = true + }, + [RBS_ENCODING_CP951] = { + .name = "CP951", + .char_width = rbs_encoding_big5_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_ascii_isupper_char_7bit, + .multibyte = true + }, + [RBS_ENCODING_EMACS_MULE] = { + .name = "Emacs-Mule", + .char_width = rbs_encoding_emacs_mule_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_ascii_isupper_char_7bit, + .multibyte = true + }, + [RBS_ENCODING_EUC_JP_MS] = { + .name = "eucJP-ms", + .char_width = rbs_encoding_euc_jp_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_euc_jp_isupper_char, + .multibyte = true + }, + [RBS_ENCODING_EUC_JIS_2004] = { + .name = "EUC-JIS-2004", + .char_width = rbs_encoding_euc_jp_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_euc_jp_isupper_char, + .multibyte = true + }, + [RBS_ENCODING_EUC_KR] = { + .name = "EUC-KR", + .char_width = rbs_encoding_euc_kr_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_ascii_isupper_char_7bit, + .multibyte = true + }, + [RBS_ENCODING_EUC_TW] = { + .name = "EUC-TW", + .char_width = rbs_encoding_euc_tw_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_ascii_isupper_char_7bit, + .multibyte = true + }, + [RBS_ENCODING_GB12345] = { + .name = "GB12345", + .char_width = rbs_encoding_euc_kr_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_ascii_isupper_char_7bit, + .multibyte = true + }, + [RBS_ENCODING_GB18030] = { + .name = "GB18030", + .char_width = rbs_encoding_gb18030_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_ascii_isupper_char_7bit, + .multibyte = true + }, + [RBS_ENCODING_GB1988] = { + .name = "GB1988", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_gb1988_alnum_char, + .alpha_char = rbs_encoding_gb1988_alpha_char, + .isupper_char = rbs_encoding_gb1988_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_GB2312] = { + .name = "GB2312", + .char_width = rbs_encoding_euc_kr_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_ascii_isupper_char_7bit, + .multibyte = true + }, + [RBS_ENCODING_GBK] = { + .name = "GBK", + .char_width = rbs_encoding_gbk_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_ascii_isupper_char_7bit, + .multibyte = true + }, + [RBS_ENCODING_IBM437] = { + .name = "IBM437", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ibm437_alnum_char, + .alpha_char = rbs_encoding_ibm437_alpha_char, + .isupper_char = rbs_encoding_ibm437_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_IBM720] = { + .name = "IBM720", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ibm720_alnum_char, + .alpha_char = rbs_encoding_ibm720_alpha_char, + .isupper_char = rbs_encoding_ibm720_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_IBM737] = { + .name = "IBM737", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ibm737_alnum_char, + .alpha_char = rbs_encoding_ibm737_alpha_char, + .isupper_char = rbs_encoding_ibm737_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_IBM775] = { + .name = "IBM775", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ibm775_alnum_char, + .alpha_char = rbs_encoding_ibm775_alpha_char, + .isupper_char = rbs_encoding_ibm775_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_IBM852] = { + .name = "IBM852", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ibm852_alnum_char, + .alpha_char = rbs_encoding_ibm852_alpha_char, + .isupper_char = rbs_encoding_ibm852_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_IBM855] = { + .name = "IBM855", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ibm855_alnum_char, + .alpha_char = rbs_encoding_ibm855_alpha_char, + .isupper_char = rbs_encoding_ibm855_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_IBM857] = { + .name = "IBM857", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ibm857_alnum_char, + .alpha_char = rbs_encoding_ibm857_alpha_char, + .isupper_char = rbs_encoding_ibm857_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_IBM860] = { + .name = "IBM860", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ibm860_alnum_char, + .alpha_char = rbs_encoding_ibm860_alpha_char, + .isupper_char = rbs_encoding_ibm860_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_IBM861] = { + .name = "IBM861", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ibm861_alnum_char, + .alpha_char = rbs_encoding_ibm861_alpha_char, + .isupper_char = rbs_encoding_ibm861_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_IBM862] = { + .name = "IBM862", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ibm862_alnum_char, + .alpha_char = rbs_encoding_ibm862_alpha_char, + .isupper_char = rbs_encoding_ibm862_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_IBM863] = { + .name = "IBM863", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ibm863_alnum_char, + .alpha_char = rbs_encoding_ibm863_alpha_char, + .isupper_char = rbs_encoding_ibm863_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_IBM864] = { + .name = "IBM864", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ibm864_alnum_char, + .alpha_char = rbs_encoding_ibm864_alpha_char, + .isupper_char = rbs_encoding_ibm864_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_IBM865] = { + .name = "IBM865", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ibm865_alnum_char, + .alpha_char = rbs_encoding_ibm865_alpha_char, + .isupper_char = rbs_encoding_ibm865_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_IBM866] = { + .name = "IBM866", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ibm866_alnum_char, + .alpha_char = rbs_encoding_ibm866_alpha_char, + .isupper_char = rbs_encoding_ibm866_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_IBM869] = { + .name = "IBM869", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_ibm869_alnum_char, + .alpha_char = rbs_encoding_ibm869_alpha_char, + .isupper_char = rbs_encoding_ibm869_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ISO_8859_1] = { + .name = "ISO-8859-1", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_iso_8859_1_alnum_char, + .alpha_char = rbs_encoding_iso_8859_1_alpha_char, + .isupper_char = rbs_encoding_iso_8859_1_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ISO_8859_2] = { + .name = "ISO-8859-2", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_iso_8859_2_alnum_char, + .alpha_char = rbs_encoding_iso_8859_2_alpha_char, + .isupper_char = rbs_encoding_iso_8859_2_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ISO_8859_3] = { + .name = "ISO-8859-3", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_iso_8859_3_alnum_char, + .alpha_char = rbs_encoding_iso_8859_3_alpha_char, + .isupper_char = rbs_encoding_iso_8859_3_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ISO_8859_4] = { + .name = "ISO-8859-4", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_iso_8859_4_alnum_char, + .alpha_char = rbs_encoding_iso_8859_4_alpha_char, + .isupper_char = rbs_encoding_iso_8859_4_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ISO_8859_5] = { + .name = "ISO-8859-5", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_iso_8859_5_alnum_char, + .alpha_char = rbs_encoding_iso_8859_5_alpha_char, + .isupper_char = rbs_encoding_iso_8859_5_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ISO_8859_6] = { + .name = "ISO-8859-6", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_iso_8859_6_alnum_char, + .alpha_char = rbs_encoding_iso_8859_6_alpha_char, + .isupper_char = rbs_encoding_iso_8859_6_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ISO_8859_7] = { + .name = "ISO-8859-7", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_iso_8859_7_alnum_char, + .alpha_char = rbs_encoding_iso_8859_7_alpha_char, + .isupper_char = rbs_encoding_iso_8859_7_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ISO_8859_8] = { + .name = "ISO-8859-8", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_iso_8859_8_alnum_char, + .alpha_char = rbs_encoding_iso_8859_8_alpha_char, + .isupper_char = rbs_encoding_iso_8859_8_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ISO_8859_9] = { + .name = "ISO-8859-9", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_iso_8859_9_alnum_char, + .alpha_char = rbs_encoding_iso_8859_9_alpha_char, + .isupper_char = rbs_encoding_iso_8859_9_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ISO_8859_10] = { + .name = "ISO-8859-10", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_iso_8859_10_alnum_char, + .alpha_char = rbs_encoding_iso_8859_10_alpha_char, + .isupper_char = rbs_encoding_iso_8859_10_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ISO_8859_11] = { + .name = "ISO-8859-11", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_iso_8859_11_alnum_char, + .alpha_char = rbs_encoding_iso_8859_11_alpha_char, + .isupper_char = rbs_encoding_iso_8859_11_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ISO_8859_13] = { + .name = "ISO-8859-13", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_iso_8859_13_alnum_char, + .alpha_char = rbs_encoding_iso_8859_13_alpha_char, + .isupper_char = rbs_encoding_iso_8859_13_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ISO_8859_14] = { + .name = "ISO-8859-14", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_iso_8859_14_alnum_char, + .alpha_char = rbs_encoding_iso_8859_14_alpha_char, + .isupper_char = rbs_encoding_iso_8859_14_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ISO_8859_15] = { + .name = "ISO-8859-15", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_iso_8859_15_alnum_char, + .alpha_char = rbs_encoding_iso_8859_15_alpha_char, + .isupper_char = rbs_encoding_iso_8859_15_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_ISO_8859_16] = { + .name = "ISO-8859-16", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_iso_8859_16_alnum_char, + .alpha_char = rbs_encoding_iso_8859_16_alpha_char, + .isupper_char = rbs_encoding_iso_8859_16_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_KOI8_R] = { + .name = "KOI8-R", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_koi8_r_alnum_char, + .alpha_char = rbs_encoding_koi8_r_alpha_char, + .isupper_char = rbs_encoding_koi8_r_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_KOI8_U] = { + .name = "KOI8-U", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_koi8_u_alnum_char, + .alpha_char = rbs_encoding_koi8_u_alpha_char, + .isupper_char = rbs_encoding_koi8_u_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_MAC_CENT_EURO] = { + .name = "macCentEuro", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_mac_cent_euro_alnum_char, + .alpha_char = rbs_encoding_mac_cent_euro_alpha_char, + .isupper_char = rbs_encoding_mac_cent_euro_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_MAC_CROATIAN] = { + .name = "macCroatian", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_mac_croatian_alnum_char, + .alpha_char = rbs_encoding_mac_croatian_alpha_char, + .isupper_char = rbs_encoding_mac_croatian_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_MAC_CYRILLIC] = { + .name = "macCyrillic", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_mac_cyrillic_alnum_char, + .alpha_char = rbs_encoding_mac_cyrillic_alpha_char, + .isupper_char = rbs_encoding_mac_cyrillic_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_MAC_GREEK] = { + .name = "macGreek", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_mac_greek_alnum_char, + .alpha_char = rbs_encoding_mac_greek_alpha_char, + .isupper_char = rbs_encoding_mac_greek_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_MAC_ICELAND] = { + .name = "macIceland", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_mac_iceland_alnum_char, + .alpha_char = rbs_encoding_mac_iceland_alpha_char, + .isupper_char = rbs_encoding_mac_iceland_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_MAC_JAPANESE] = { + .name = "MacJapanese", + .char_width = rbs_encoding_shift_jis_char_width, + .alnum_char = rbs_encoding_shift_jis_alnum_char, + .alpha_char = rbs_encoding_shift_jis_alpha_char, + .isupper_char = rbs_encoding_shift_jis_isupper_char, + .multibyte = true + }, + [RBS_ENCODING_MAC_ROMAN] = { + .name = "macRoman", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_mac_roman_alnum_char, + .alpha_char = rbs_encoding_mac_roman_alpha_char, + .isupper_char = rbs_encoding_mac_roman_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_MAC_ROMANIA] = { + .name = "macRomania", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_mac_romania_alnum_char, + .alpha_char = rbs_encoding_mac_romania_alpha_char, + .isupper_char = rbs_encoding_mac_romania_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_MAC_THAI] = { + .name = "macThai", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_mac_thai_alnum_char, + .alpha_char = rbs_encoding_mac_thai_alpha_char, + .isupper_char = rbs_encoding_mac_thai_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_MAC_TURKISH] = { + .name = "macTurkish", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_mac_turkish_alnum_char, + .alpha_char = rbs_encoding_mac_turkish_alpha_char, + .isupper_char = rbs_encoding_mac_turkish_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_MAC_UKRAINE] = { + .name = "macUkraine", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_mac_ukraine_alnum_char, + .alpha_char = rbs_encoding_mac_ukraine_alpha_char, + .isupper_char = rbs_encoding_mac_ukraine_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_SHIFT_JIS] = { + .name = "Shift_JIS", + .char_width = rbs_encoding_shift_jis_char_width, + .alnum_char = rbs_encoding_shift_jis_alnum_char, + .alpha_char = rbs_encoding_shift_jis_alpha_char, + .isupper_char = rbs_encoding_shift_jis_isupper_char, + .multibyte = true + }, + [RBS_ENCODING_SJIS_DOCOMO] = { + .name = "SJIS-DoCoMo", + .char_width = rbs_encoding_shift_jis_char_width, + .alnum_char = rbs_encoding_shift_jis_alnum_char, + .alpha_char = rbs_encoding_shift_jis_alpha_char, + .isupper_char = rbs_encoding_shift_jis_isupper_char, + .multibyte = true + }, + [RBS_ENCODING_SJIS_KDDI] = { + .name = "SJIS-KDDI", + .char_width = rbs_encoding_shift_jis_char_width, + .alnum_char = rbs_encoding_shift_jis_alnum_char, + .alpha_char = rbs_encoding_shift_jis_alpha_char, + .isupper_char = rbs_encoding_shift_jis_isupper_char, + .multibyte = true + }, + [RBS_ENCODING_SJIS_SOFTBANK] = { + .name = "SJIS-SoftBank", + .char_width = rbs_encoding_shift_jis_char_width, + .alnum_char = rbs_encoding_shift_jis_alnum_char, + .alpha_char = rbs_encoding_shift_jis_alpha_char, + .isupper_char = rbs_encoding_shift_jis_isupper_char, + .multibyte = true + }, + [RBS_ENCODING_STATELESS_ISO_2022_JP] = { + .name = "stateless-ISO-2022-JP", + .char_width = rbs_encoding_emacs_mule_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_ascii_isupper_char_7bit, + .multibyte = true + }, + [RBS_ENCODING_STATELESS_ISO_2022_JP_KDDI] = { + .name = "stateless-ISO-2022-JP-KDDI", + .char_width = rbs_encoding_emacs_mule_char_width, + .alnum_char = rbs_encoding_ascii_alnum_char_7bit, + .alpha_char = rbs_encoding_ascii_alpha_char_7bit, + .isupper_char = rbs_encoding_ascii_isupper_char_7bit, + .multibyte = true + }, + [RBS_ENCODING_TIS_620] = { + .name = "TIS-620", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_tis_620_alnum_char, + .alpha_char = rbs_encoding_tis_620_alpha_char, + .isupper_char = rbs_encoding_tis_620_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_UTF8_MAC] = { + .name = "UTF8-MAC", + .char_width = rbs_encoding_utf_8_char_width, + .alnum_char = rbs_encoding_utf_8_alnum_char, + .alpha_char = rbs_encoding_utf_8_alpha_char, + .isupper_char = rbs_encoding_utf_8_isupper_char, + .multibyte = true + }, + [RBS_ENCODING_UTF8_DOCOMO] = { + .name = "UTF8-DoCoMo", + .char_width = rbs_encoding_utf_8_char_width, + .alnum_char = rbs_encoding_utf_8_alnum_char, + .alpha_char = rbs_encoding_utf_8_alpha_char, + .isupper_char = rbs_encoding_utf_8_isupper_char, + .multibyte = true + }, + [RBS_ENCODING_UTF8_KDDI] = { + .name = "UTF8-KDDI", + .char_width = rbs_encoding_utf_8_char_width, + .alnum_char = rbs_encoding_utf_8_alnum_char, + .alpha_char = rbs_encoding_utf_8_alpha_char, + .isupper_char = rbs_encoding_utf_8_isupper_char, + .multibyte = true + }, + [RBS_ENCODING_UTF8_SOFTBANK] = { + .name = "UTF8-SoftBank", + .char_width = rbs_encoding_utf_8_char_width, + .alnum_char = rbs_encoding_utf_8_alnum_char, + .alpha_char = rbs_encoding_utf_8_alpha_char, + .isupper_char = rbs_encoding_utf_8_isupper_char, + .multibyte = true + }, + [RBS_ENCODING_WINDOWS_1250] = { + .name = "Windows-1250", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_windows_1250_alnum_char, + .alpha_char = rbs_encoding_windows_1250_alpha_char, + .isupper_char = rbs_encoding_windows_1250_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_WINDOWS_1251] = { + .name = "Windows-1251", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_windows_1251_alnum_char, + .alpha_char = rbs_encoding_windows_1251_alpha_char, + .isupper_char = rbs_encoding_windows_1251_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_WINDOWS_1252] = { + .name = "Windows-1252", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_windows_1252_alnum_char, + .alpha_char = rbs_encoding_windows_1252_alpha_char, + .isupper_char = rbs_encoding_windows_1252_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_WINDOWS_1253] = { + .name = "Windows-1253", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_windows_1253_alnum_char, + .alpha_char = rbs_encoding_windows_1253_alpha_char, + .isupper_char = rbs_encoding_windows_1253_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_WINDOWS_1254] = { + .name = "Windows-1254", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_windows_1254_alnum_char, + .alpha_char = rbs_encoding_windows_1254_alpha_char, + .isupper_char = rbs_encoding_windows_1254_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_WINDOWS_1255] = { + .name = "Windows-1255", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_windows_1255_alnum_char, + .alpha_char = rbs_encoding_windows_1255_alpha_char, + .isupper_char = rbs_encoding_windows_1255_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_WINDOWS_1256] = { + .name = "Windows-1256", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_windows_1256_alnum_char, + .alpha_char = rbs_encoding_windows_1256_alpha_char, + .isupper_char = rbs_encoding_windows_1256_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_WINDOWS_1257] = { + .name = "Windows-1257", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_windows_1257_alnum_char, + .alpha_char = rbs_encoding_windows_1257_alpha_char, + .isupper_char = rbs_encoding_windows_1257_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_WINDOWS_1258] = { + .name = "Windows-1258", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_windows_1258_alnum_char, + .alpha_char = rbs_encoding_windows_1258_alpha_char, + .isupper_char = rbs_encoding_windows_1258_isupper_char, + .multibyte = false + }, + [RBS_ENCODING_WINDOWS_874] = { + .name = "Windows-874", + .char_width = rbs_encoding_single_char_width, + .alnum_char = rbs_encoding_windows_874_alnum_char, + .alpha_char = rbs_encoding_windows_874_alpha_char, + .isupper_char = rbs_encoding_windows_874_isupper_char, + .multibyte = false + } +#endif +}; + +/** + * Parse the given name of an encoding and return a pointer to the corresponding + * encoding struct if one can be found, otherwise return NULL. + */ +const rbs_encoding_t * +rbs_encoding_find(const uint8_t *start, const uint8_t *end) { + size_t width = (size_t) (end - start); + + // First, we're going to check for UTF-8. This is the most common encoding. + // UTF-8 can contain extra information at the end about the platform it is + // encoded on, such as UTF-8-MAC or UTF-8-UNIX. We'll ignore those suffixes. + if ((start + 5 <= end) && (rbs_strncasecmp(start, (const uint8_t *) "UTF-8", 5) == 0)) { +#ifndef RBS_ENCODING_EXCLUDE_FULL + // We need to explicitly handle UTF-8-HFS, as that one needs to switch + // over to being UTF8-MAC. + if (width == 9 && (rbs_strncasecmp(start + 5, (const uint8_t *) "-HFS", 4) == 0)) { + return &rbs_encodings[RBS_ENCODING_UTF8_MAC]; + } +#endif + + // Otherwise we'll return the default UTF-8 encoding. + return RBS_ENCODING_UTF_8_ENTRY; + } + + // Next, we're going to loop through each of the encodings that we handle + // explicitly. If we found one that we understand, we'll use that value. +#define ENCODING1(name, encoding) if (width == sizeof(name) - 1 && rbs_strncasecmp(start, (const uint8_t *) name, width) == 0) return &rbs_encodings[encoding]; +#define ENCODING2(name1, name2, encoding) ENCODING1(name1, encoding) ENCODING1(name2, encoding) + + if (width >= 3) { + switch (*start) { + case 'A': case 'a': + ENCODING1("ASCII", RBS_ENCODING_US_ASCII); + ENCODING1("ASCII-8BIT", RBS_ENCODING_ASCII_8BIT); + ENCODING1("ANSI_X3.4-1968", RBS_ENCODING_US_ASCII); + break; + case 'B': case 'b': + ENCODING1("BINARY", RBS_ENCODING_ASCII_8BIT); +#ifndef RBS_ENCODING_EXCLUDE_FULL + ENCODING1("Big5", RBS_ENCODING_BIG5); + ENCODING2("Big5-HKSCS", "Big5-HKSCS:2008", RBS_ENCODING_BIG5_HKSCS); + ENCODING1("Big5-UAO", RBS_ENCODING_BIG5_UAO); +#endif + break; + case 'C': case 'c': + ENCODING1("CP65001", RBS_ENCODING_UTF_8); + ENCODING2("CP932", "csWindows31J", RBS_ENCODING_WINDOWS_31J); +#ifndef RBS_ENCODING_EXCLUDE_FULL + ENCODING1("CESU-8", RBS_ENCODING_CESU_8); + ENCODING1("CP437", RBS_ENCODING_IBM437); + ENCODING1("CP720", RBS_ENCODING_IBM720); + ENCODING1("CP737", RBS_ENCODING_IBM737); + ENCODING1("CP775", RBS_ENCODING_IBM775); + ENCODING1("CP850", RBS_ENCODING_CP850); + ENCODING1("CP852", RBS_ENCODING_CP852); + ENCODING1("CP855", RBS_ENCODING_CP855); + ENCODING1("CP857", RBS_ENCODING_IBM857); + ENCODING1("CP860", RBS_ENCODING_IBM860); + ENCODING1("CP861", RBS_ENCODING_IBM861); + ENCODING1("CP862", RBS_ENCODING_IBM862); + ENCODING1("CP864", RBS_ENCODING_IBM864); + ENCODING1("CP865", RBS_ENCODING_IBM865); + ENCODING1("CP866", RBS_ENCODING_IBM866); + ENCODING1("CP869", RBS_ENCODING_IBM869); + ENCODING1("CP874", RBS_ENCODING_WINDOWS_874); + ENCODING1("CP878", RBS_ENCODING_KOI8_R); + ENCODING1("CP863", RBS_ENCODING_IBM863); + ENCODING1("CP936", RBS_ENCODING_GBK); + ENCODING1("CP949", RBS_ENCODING_CP949); + ENCODING1("CP950", RBS_ENCODING_CP950); + ENCODING1("CP951", RBS_ENCODING_CP951); + ENCODING1("CP1250", RBS_ENCODING_WINDOWS_1250); + ENCODING1("CP1251", RBS_ENCODING_WINDOWS_1251); + ENCODING1("CP1252", RBS_ENCODING_WINDOWS_1252); + ENCODING1("CP1253", RBS_ENCODING_WINDOWS_1253); + ENCODING1("CP1254", RBS_ENCODING_WINDOWS_1254); + ENCODING1("CP1255", RBS_ENCODING_WINDOWS_1255); + ENCODING1("CP1256", RBS_ENCODING_WINDOWS_1256); + ENCODING1("CP1257", RBS_ENCODING_WINDOWS_1257); + ENCODING1("CP1258", RBS_ENCODING_WINDOWS_1258); + ENCODING1("CP51932", RBS_ENCODING_CP51932); +#endif + break; + case 'E': case 'e': + ENCODING2("EUC-JP", "eucJP", RBS_ENCODING_EUC_JP); +#ifndef RBS_ENCODING_EXCLUDE_FULL + ENCODING2("eucJP-ms", "euc-jp-ms", RBS_ENCODING_EUC_JP_MS); + ENCODING2("EUC-JIS-2004", "EUC-JISX0213", RBS_ENCODING_EUC_JIS_2004); + ENCODING2("EUC-KR", "eucKR", RBS_ENCODING_EUC_KR); + ENCODING2("EUC-CN", "eucCN", RBS_ENCODING_GB2312); + ENCODING2("EUC-TW", "eucTW", RBS_ENCODING_EUC_TW); + ENCODING1("Emacs-Mule", RBS_ENCODING_EMACS_MULE); +#endif + break; + case 'G': case 'g': +#ifndef RBS_ENCODING_EXCLUDE_FULL + ENCODING1("GBK", RBS_ENCODING_GBK); + ENCODING1("GB12345", RBS_ENCODING_GB12345); + ENCODING1("GB18030", RBS_ENCODING_GB18030); + ENCODING1("GB1988", RBS_ENCODING_GB1988); + ENCODING1("GB2312", RBS_ENCODING_GB2312); +#endif + break; + case 'I': case 'i': +#ifndef RBS_ENCODING_EXCLUDE_FULL + ENCODING1("IBM437", RBS_ENCODING_IBM437); + ENCODING1("IBM720", RBS_ENCODING_IBM720); + ENCODING1("IBM737", RBS_ENCODING_IBM737); + ENCODING1("IBM775", RBS_ENCODING_IBM775); + ENCODING1("IBM850", RBS_ENCODING_CP850); + ENCODING1("IBM852", RBS_ENCODING_IBM852); + ENCODING1("IBM855", RBS_ENCODING_IBM855); + ENCODING1("IBM857", RBS_ENCODING_IBM857); + ENCODING1("IBM860", RBS_ENCODING_IBM860); + ENCODING1("IBM861", RBS_ENCODING_IBM861); + ENCODING1("IBM862", RBS_ENCODING_IBM862); + ENCODING1("IBM863", RBS_ENCODING_IBM863); + ENCODING1("IBM864", RBS_ENCODING_IBM864); + ENCODING1("IBM865", RBS_ENCODING_IBM865); + ENCODING1("IBM866", RBS_ENCODING_IBM866); + ENCODING1("IBM869", RBS_ENCODING_IBM869); + ENCODING2("ISO-8859-1", "ISO8859-1", RBS_ENCODING_ISO_8859_1); + ENCODING2("ISO-8859-2", "ISO8859-2", RBS_ENCODING_ISO_8859_2); + ENCODING2("ISO-8859-3", "ISO8859-3", RBS_ENCODING_ISO_8859_3); + ENCODING2("ISO-8859-4", "ISO8859-4", RBS_ENCODING_ISO_8859_4); + ENCODING2("ISO-8859-5", "ISO8859-5", RBS_ENCODING_ISO_8859_5); + ENCODING2("ISO-8859-6", "ISO8859-6", RBS_ENCODING_ISO_8859_6); + ENCODING2("ISO-8859-7", "ISO8859-7", RBS_ENCODING_ISO_8859_7); + ENCODING2("ISO-8859-8", "ISO8859-8", RBS_ENCODING_ISO_8859_8); + ENCODING2("ISO-8859-9", "ISO8859-9", RBS_ENCODING_ISO_8859_9); + ENCODING2("ISO-8859-10", "ISO8859-10", RBS_ENCODING_ISO_8859_10); + ENCODING2("ISO-8859-11", "ISO8859-11", RBS_ENCODING_ISO_8859_11); + ENCODING2("ISO-8859-13", "ISO8859-13", RBS_ENCODING_ISO_8859_13); + ENCODING2("ISO-8859-14", "ISO8859-14", RBS_ENCODING_ISO_8859_14); + ENCODING2("ISO-8859-15", "ISO8859-15", RBS_ENCODING_ISO_8859_15); + ENCODING2("ISO-8859-16", "ISO8859-16", RBS_ENCODING_ISO_8859_16); +#endif + break; + case 'K': case 'k': +#ifndef RBS_ENCODING_EXCLUDE_FULL + ENCODING1("KOI8-R", RBS_ENCODING_KOI8_R); + ENCODING1("KOI8-U", RBS_ENCODING_KOI8_U); +#endif + break; + case 'M': case 'm': +#ifndef RBS_ENCODING_EXCLUDE_FULL + ENCODING1("macCentEuro", RBS_ENCODING_MAC_CENT_EURO); + ENCODING1("macCroatian", RBS_ENCODING_MAC_CROATIAN); + ENCODING1("macCyrillic", RBS_ENCODING_MAC_CYRILLIC); + ENCODING1("macGreek", RBS_ENCODING_MAC_GREEK); + ENCODING1("macIceland", RBS_ENCODING_MAC_ICELAND); + ENCODING1("MacJapanese", RBS_ENCODING_MAC_JAPANESE); + ENCODING1("MacJapan", RBS_ENCODING_MAC_JAPANESE); + ENCODING1("macRoman", RBS_ENCODING_MAC_ROMAN); + ENCODING1("macRomania", RBS_ENCODING_MAC_ROMANIA); + ENCODING1("macThai", RBS_ENCODING_MAC_THAI); + ENCODING1("macTurkish", RBS_ENCODING_MAC_TURKISH); + ENCODING1("macUkraine", RBS_ENCODING_MAC_UKRAINE); +#endif + break; + case 'P': case 'p': + ENCODING1("PCK", RBS_ENCODING_WINDOWS_31J); + break; + case 'S': case 's': + ENCODING1("SJIS", RBS_ENCODING_WINDOWS_31J); +#ifndef RBS_ENCODING_EXCLUDE_FULL + ENCODING1("Shift_JIS", RBS_ENCODING_SHIFT_JIS); + ENCODING1("SJIS-DoCoMo", RBS_ENCODING_SJIS_DOCOMO); + ENCODING1("SJIS-KDDI", RBS_ENCODING_SJIS_KDDI); + ENCODING1("SJIS-SoftBank", RBS_ENCODING_SJIS_SOFTBANK); + ENCODING1("stateless-ISO-2022-JP", RBS_ENCODING_STATELESS_ISO_2022_JP); + ENCODING1("stateless-ISO-2022-JP-KDDI", RBS_ENCODING_STATELESS_ISO_2022_JP_KDDI); +#endif + break; + case 'T': case 't': +#ifndef RBS_ENCODING_EXCLUDE_FULL + ENCODING1("TIS-620", RBS_ENCODING_TIS_620); +#endif + break; + case 'U': case 'u': + ENCODING1("US-ASCII", RBS_ENCODING_US_ASCII); +#ifndef RBS_ENCODING_EXCLUDE_FULL + ENCODING2("UTF8-MAC", "UTF-8-HFS", RBS_ENCODING_UTF8_MAC); + ENCODING1("UTF8-DoCoMo", RBS_ENCODING_UTF8_DOCOMO); + ENCODING1("UTF8-KDDI", RBS_ENCODING_UTF8_KDDI); + ENCODING1("UTF8-SoftBank", RBS_ENCODING_UTF8_SOFTBANK); +#endif + break; + case 'W': case 'w': + ENCODING1("Windows-31J", RBS_ENCODING_WINDOWS_31J); +#ifndef RBS_ENCODING_EXCLUDE_FULL + ENCODING1("Windows-874", RBS_ENCODING_WINDOWS_874); + ENCODING1("Windows-1250", RBS_ENCODING_WINDOWS_1250); + ENCODING1("Windows-1251", RBS_ENCODING_WINDOWS_1251); + ENCODING1("Windows-1252", RBS_ENCODING_WINDOWS_1252); + ENCODING1("Windows-1253", RBS_ENCODING_WINDOWS_1253); + ENCODING1("Windows-1254", RBS_ENCODING_WINDOWS_1254); + ENCODING1("Windows-1255", RBS_ENCODING_WINDOWS_1255); + ENCODING1("Windows-1256", RBS_ENCODING_WINDOWS_1256); + ENCODING1("Windows-1257", RBS_ENCODING_WINDOWS_1257); + ENCODING1("Windows-1258", RBS_ENCODING_WINDOWS_1258); +#endif + break; + case '6': + ENCODING1("646", RBS_ENCODING_US_ASCII); + break; + } + } + +#undef ENCODING2 +#undef ENCODING1 + + // If we didn't match any encodings, return NULL. + return NULL; +} diff --git a/src/rbs_strncasecmp.c b/src/rbs_strncasecmp.c new file mode 100644 index 000000000..e46a7c43a --- /dev/null +++ b/src/rbs_strncasecmp.c @@ -0,0 +1,24 @@ +#include "rbs/rbs_strncasecmp.h" + +/** + * Compare two strings, ignoring case, up to the given length. Returns 0 if the + * strings are equal, a negative number if string1 is less than string2, or a + * positive number if string1 is greater than string2. + * + * Note that this is effectively our own implementation of strncasecmp, but it's + * not available on all of the platforms we want to support so we're rolling it + * here. + */ +int +rbs_strncasecmp(const uint8_t *string1, const uint8_t *string2, size_t length) { + size_t offset = 0; + int difference = 0; + + while (offset < length && string1[offset] != '\0') { + if (string2[offset] == '\0') return string1[offset]; + if ((difference = tolower(string1[offset]) - tolower(string2[offset])) != 0) return difference; + offset++; + } + + return difference; +} From 0984a4a4bf2210f356d3da27e2c31e96090fdf51 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Fri, 3 Jan 2025 14:13:03 -0500 Subject: [PATCH 026/111] Store encoding in the lexer state Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/lexer.h | 2 ++ ext/rbs_extension/parserstate.c | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/ext/rbs_extension/lexer.h b/ext/rbs_extension/lexer.h index 6b294eab4..2bb2fc4af 100644 --- a/ext/rbs_extension/lexer.h +++ b/ext/rbs_extension/lexer.h @@ -2,6 +2,7 @@ #define RBS__LEXER_H #include "ruby.h" +#include "rbs/rbs_encoding.h" enum TokenType { NullType, /* (Nothing) */ @@ -132,6 +133,7 @@ typedef struct { position start; /* The start position of the current token */ bool first_token_of_line; /* This flag is used for tLINECOMMENT */ unsigned int last_char; /* Last peeked character */ + const rbs_encoding_t *encoding; } lexstate; extern token NullToken; diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index 6b8616bc4..ce91e58b1 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -286,6 +286,11 @@ lexstate *alloc_lexer(rbs_allocator_t *allocator, VALUE string, int start_pos, i rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos); } + rb_encoding *ruby_encoding = rb_enc_get(string); + const char *encoding_name = rb_enc_name(ruby_encoding); + const char *encoding_name_end = encoding_name + strlen(encoding_name); + const rbs_encoding_t *encoding = rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) encoding_name_end); + lexstate *lexer = rbs_allocator_alloc(allocator, lexstate); position start_position = (position) { @@ -303,6 +308,7 @@ lexstate *alloc_lexer(rbs_allocator_t *allocator, VALUE string, int start_pos, i .start = { 0 }, .first_token_of_line = false, .last_char = 0, + .encoding = encoding, }; skipn(lexer, start_pos); From 08b4a7aaf7dba20221a4fc36324d5b44dad06290 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 10 Jan 2025 14:56:06 -0500 Subject: [PATCH 027/111] Migrate `lexerstate` string to `rbs_string` --- ext/rbs_extension/ast_translation.c | 9 +++---- ext/rbs_extension/ast_translation.h | 12 +++++++++ ext/rbs_extension/lexer.h | 4 +-- ext/rbs_extension/lexstate.c | 24 ++++++++++++++--- ext/rbs_extension/main.c | 3 +++ ext/rbs_extension/parser.c | 27 +++++++++---------- ext/rbs_extension/parserstate.c | 3 ++- ext/rbs_extension/parserstate.h | 3 +++ ext/rbs_extension/rbs_string_bridging.c | 4 +-- ext/rbs_extension/rbs_string_bridging.h | 4 ++- include/rbs/ast.h | 6 ----- include/rbs/util/rbs_constant_pool.h | 4 ++- src/util/rbs_constant_pool.c | 2 +- .../ext/rbs_extension/ast_translation.c.erb | 7 ++--- .../ext/rbs_extension/ast_translation.h.erb | 12 +++++++++ templates/include/rbs/ast.h.erb | 6 ----- 16 files changed, 80 insertions(+), 50 deletions(-) diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index 4b7326b26..ca15fa903 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -69,7 +69,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rbs_ast_annotation_t *node = (rbs_ast_annotation_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("string")), rbs_string_to_ruby_string(&node->string)); + rb_hash_aset(h, ID2SYM(rb_intern("string")), rbs_string_to_ruby_string(&node->string, ctx.encoding)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); @@ -88,7 +88,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rbs_ast_comment_t *node = (rbs_ast_comment_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("string")), rbs_string_to_ruby_string(&node->string)); + rb_hash_aset(h, ID2SYM(rb_intern("string")), rbs_string_to_ruby_string(&node->string, ctx.encoding)); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); @@ -1093,10 +1093,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan assert(constant != NULL && "constant is NULL"); assert(constant->start != NULL && "constant->start is NULL"); - // FIXME: Add `rb_encoding` to the `ctx` and use it here. - rb_encoding *encoding = rb_usascii_encoding(); - - return ID2SYM(rb_intern3((const char *) constant->start, constant->length, encoding)); + return ID2SYM(rb_intern3((const char *) constant->start, constant->length, ctx.encoding)); } } } diff --git a/ext/rbs_extension/ast_translation.h b/ext/rbs_extension/ast_translation.h index 442e370c6..3e07bed5d 100644 --- a/ext/rbs_extension/ast_translation.h +++ b/ext/rbs_extension/ast_translation.h @@ -8,7 +8,19 @@ #ifndef RBS_EXTENSION_AST_TRANSLATION_H #define RBS_EXTENSION_AST_TRANSLATION_H +#include "ruby.h" +#include "ruby/encoding.h" + #include "rbs/ast.h" +#include "rbs/rbs_encoding.h" +#include "rbs/rbs_location.h" + +/// A bag of values needed when copying RBS C structs into Ruby objects. +typedef struct rbs_translation_context { + rbs_constant_pool_t *constant_pool; + VALUE buffer; + rb_encoding *encoding; +} rbs_translation_context_t; VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *list); VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t, rbs_hash_t *hash); diff --git a/ext/rbs_extension/lexer.h b/ext/rbs_extension/lexer.h index 2bb2fc4af..599a768ac 100644 --- a/ext/rbs_extension/lexer.h +++ b/ext/rbs_extension/lexer.h @@ -1,7 +1,7 @@ #ifndef RBS__LEXER_H #define RBS__LEXER_H -#include "ruby.h" +#include "rbs/rbs_string.h" #include "rbs/rbs_encoding.h" enum TokenType { @@ -126,7 +126,7 @@ typedef struct { * ``` * */ typedef struct { - VALUE string; + rbs_string_t string; int start_pos; /* The character position that defines the start of the input */ int end_pos; /* The character position that defines the end of the input */ position current; /* The current position */ diff --git a/ext/rbs_extension/lexstate.c b/ext/rbs_extension/lexstate.c index ed32fd06a..c39fab4ee 100644 --- a/ext/rbs_extension/lexstate.c +++ b/ext/rbs_extension/lexstate.c @@ -1,4 +1,7 @@ #include "rbs_extension.h" +#include "ruby.h" +#include "rbs_string_bridging.h" +#include "rbs/encoding.h" static const char *RBS_TOKENTYPE_NAMES[] = { "NullType", @@ -109,7 +112,12 @@ unsigned int peek(lexstate *state) { state->last_char = '\0'; return 0; } else { - unsigned int c = rb_enc_mbc_to_codepoint(RSTRING_PTR(state->string) + state->current.byte_pos, RSTRING_END(state->string), rb_enc_get(state->string)); + rbs_string_t str = { + .start = state->string.start + state->current.byte_pos, + .end = state->string.end, + .type = RBS_STRING_SHARED, + }; + unsigned int c = utf8_to_codepoint(str); state->last_char = c; return c; } @@ -130,7 +138,7 @@ token next_token(lexstate *state, enum TokenType type) { } token next_eof_token(lexstate *state) { - if (state->current.byte_pos == RSTRING_LEN(state->string)+1) { + if ((size_t) state->current.byte_pos == rbs_string_len(state->string) + 1) { // End of String token t; t.type = pEOF; @@ -149,7 +157,15 @@ void rbs_skip(lexstate *state) { if (!state->last_char) { peek(state); } - int byte_len = rb_enc_codelen(state->last_char, rb_enc_get(state->string)); + + size_t byte_len; + + if (state->last_char == '\0') { + byte_len = 1; + } else { + const char *start = state->string.start + state->current.byte_pos; + byte_len = state->encoding->char_width((const uint8_t *) start, (ptrdiff_t) (state->string.end - start)); + } state->current.char_pos += 1; state->current.byte_pos += byte_len; @@ -171,5 +187,5 @@ void skipn(lexstate *state, size_t size) { } char *peek_token(lexstate *state, token tok) { - return RSTRING_PTR(state->string) + tok.range.start.byte_pos; + return (char *) state->string.start + tok.range.start.byte_pos; } diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index bd2247313..5aac8746b 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -57,6 +57,7 @@ static VALUE parse_type_try(VALUE a) { rbs_translation_context_t ctx = { .constant_pool = &parser->constant_pool, .buffer = parser->buffer, + .encoding = parser->encoding, }; return rbs_struct_to_ruby_value(ctx, type); @@ -95,6 +96,7 @@ static VALUE parse_method_type_try(VALUE a) { rbs_translation_context_t ctx = { .constant_pool = &parser->constant_pool, .buffer = parser->buffer, + .encoding = parser->encoding, }; return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type); @@ -124,6 +126,7 @@ static VALUE parse_signature_try(VALUE a) { rbs_translation_context_t ctx = { .constant_pool = &parser->constant_pool, .buffer = parser->buffer, + .encoding = parser->encoding, }; return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature); diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index 46fcfbb2d..2bf1250fc 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -3,10 +3,10 @@ #include "rbs_extension.h" #include "rbs/util/rbs_constant_pool.h" #include "rbs/ast.h" +#include "rbs/encoding.h" #include "rbs/rbs_string.h" #include "ast_translation.h" #include "rbs/rbs_unescape.h" -#include "rbs_string_bridging.h" #define INTERN(str) \ rbs_constant_pool_insert_constant( \ @@ -20,7 +20,7 @@ &parserstate->constant_pool, \ (const uint8_t *) peek_token(parserstate->lexstate, tok),\ token_bytes(tok), \ - (void *) rb_enc_get(parserstate->lexstate->string) \ + (void *) parserstate->lexstate->encoding \ ) #define KEYWORD_CASES \ @@ -99,7 +99,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type); static rbs_string_t rbs_parser_get_current_token(parserstate *state) { range rg = state->current_token.range; - rbs_string_t string = rbs_string_from_ruby_string(state->lexstate->string); + rbs_string_t string = state->lexstate->string; rbs_string_drop_first(&string, rg.start.byte_pos); rbs_string_limit_length(&string, rg.end.byte_pos - rg.start.byte_pos); rbs_string_ensure_owned(&string); @@ -321,7 +321,7 @@ static rbs_constant_id_t intern_token_start_end(parserstate *state, token start_ &state->constant_pool, (const uint8_t *) peek_token(state->lexstate, start_token), end_token.range.end.byte_pos - start_token.range.start.byte_pos, - (void *) rb_enc_get(state->lexstate->string) + state->lexstate->encoding ); } @@ -857,11 +857,8 @@ static bool parse_record_attributes(parserstate *state, rbs_hash_t **fields) { */ NODISCARD static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types_literal_t **symbol) { - VALUE string = state->lexstate->string; - rb_encoding *enc = rb_enc_get(string); - - int offset_bytes = rb_enc_codelen(':', enc); - int bytes = token_bytes(state->current_token) - offset_bytes; + size_t offset_bytes = state->lexstate->encoding->char_width((const uint8_t *) ":", (size_t) 1); + size_t bytes = token_bytes(state->current_token) - offset_bytes; rbs_ast_symbol_t *literal; @@ -1504,8 +1501,8 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati int offset_bytes = rb_enc_codelen('%', enc) + rb_enc_codelen('a', enc); unsigned int open_char = rb_enc_mbc_to_codepoint( - RSTRING_PTR(state->lexstate->string) + rg.start.byte_pos + offset_bytes, - RSTRING_END(state->lexstate->string), + state->lexstate->string.start + rg.start.byte_pos + offset_bytes, + state->lexstate->string.end, enc ); @@ -1594,11 +1591,11 @@ static bool parse_method_name(parserstate *state, range *range, rbs_ast_symbol_t range->end = state->next_token.range.end; parser_advance(state); - rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared( + rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared_with_encoding( &state->constant_pool, - (const uint8_t *) RSTRING_PTR(state->lexstate->string) + range->start.byte_pos, - range->end.byte_pos - range->start.byte_pos - // rb_enc_get(state->lexstate->string) // FIXME: Add encoding back + (const uint8_t *) state->lexstate->string.start + range->start.byte_pos, + range->end.byte_pos - range->start.byte_pos, + state->lexstate->encoding ); *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index ce91e58b1..e959fcf43 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -301,7 +301,7 @@ lexstate *alloc_lexer(rbs_allocator_t *allocator, VALUE string, int start_pos, i }; *lexer = (lexstate) { - .string = string, + .string = rbs_string_from_ruby_string(string), .start_pos = start_pos, .end_pos = end_pos, .current = start_position, @@ -333,6 +333,7 @@ parserstate *alloc_parser(VALUE buffer, VALUE string, int start_pos, int end_pos .next_token2 = NullToken, .next_token3 = NullToken, .buffer = buffer, + .encoding = rb_enc_get(buffer), .vars = NULL, .last_comment = NULL, diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index b27ba64ca..56def767e 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -5,6 +5,8 @@ #include "rbs/util/rbs_allocator.h" #include "rbs/util/rbs_constant_pool.h" +#include "ruby/encoding.h" // TODO: remove this + #include "lexer.h" #include "location.h" #include "rbs/ast.h" @@ -61,6 +63,7 @@ typedef struct { token next_token2; /* The second lookahead token */ token next_token3; /* The third lookahead token */ VALUE buffer; + rb_encoding *encoding; // TODO: Remove this id_table *vars; /* Known type variables */ comment *last_comment; /* Last read comment */ diff --git a/ext/rbs_extension/rbs_string_bridging.c b/ext/rbs_extension/rbs_string_bridging.c index 6d9704104..48282dea8 100644 --- a/ext/rbs_extension/rbs_string_bridging.c +++ b/ext/rbs_extension/rbs_string_bridging.c @@ -10,8 +10,8 @@ rbs_string_t rbs_string_from_ruby_string(VALUE ruby_string) { return s; } -VALUE rbs_string_to_ruby_string(rbs_string_t *self) { +VALUE rbs_string_to_ruby_string(rbs_string_t *self, rb_encoding *encoding) { VALUE str = rb_str_new_static(self->start, rbs_string_len(*self)); - // rb_enc_associate(str, self->encoding); + rb_enc_associate(str, encoding); return str; } diff --git a/ext/rbs_extension/rbs_string_bridging.h b/ext/rbs_extension/rbs_string_bridging.h index b8e11c508..3acc1bc26 100644 --- a/ext/rbs_extension/rbs_string_bridging.h +++ b/ext/rbs_extension/rbs_string_bridging.h @@ -2,6 +2,8 @@ #define RBS__RBS_STRING_BRIDGING_H #include "ruby.h" +#include "ruby/encoding.h" + #include "rbs/rbs_string.h" /** @@ -12,6 +14,6 @@ rbs_string_t rbs_string_from_ruby_string(VALUE ruby_string); /** * Returns a new Ruby string from the given rbs_string_t. */ -VALUE rbs_string_to_ruby_string(rbs_string_t *self); +VALUE rbs_string_to_ruby_string(rbs_string_t *self, rb_encoding *encoding); #endif diff --git a/include/rbs/ast.h b/include/rbs/ast.h index 332bc4b6d..96d5be12a 100644 --- a/include/rbs/ast.h +++ b/include/rbs/ast.h @@ -85,12 +85,6 @@ typedef struct rbs_node { enum rbs_node_type type; } rbs_node_t; -/// A bag of values needed when copying RBS C structs into Ruby objects. -typedef struct rbs_translation_context { - rbs_constant_pool_t *constant_pool; - VALUE buffer; -} rbs_translation_context_t; - /* rbs_node_list_node */ typedef struct rbs_node_list_node { diff --git a/include/rbs/util/rbs_constant_pool.h b/include/rbs/util/rbs_constant_pool.h index 63f15627b..4be1b7ce1 100644 --- a/include/rbs/util/rbs_constant_pool.h +++ b/include/rbs/util/rbs_constant_pool.h @@ -10,6 +10,8 @@ #ifndef RBS_CONSTANT_POOL_H #define RBS_CONSTANT_POOL_H +#include "rbs/rbs_encoding.h" + #include #include #include @@ -185,7 +187,7 @@ rbs_constant_id_t rbs_constant_pool_find(const rbs_constant_pool_t *pool, const * @return The id of the constant. */ rbs_constant_id_t rbs_constant_pool_insert_shared(rbs_constant_pool_t *pool, const uint8_t *start, size_t length); -rbs_constant_id_t rbs_constant_pool_insert_shared_with_encoding(rbs_constant_pool_t *pool, const uint8_t *start, size_t length, void *encoding); +rbs_constant_id_t rbs_constant_pool_insert_shared_with_encoding(rbs_constant_pool_t *pool, const uint8_t *start, size_t length, const rbs_encoding_t *encoding); /** * Insert a constant into a constant pool from memory that is now owned by the diff --git a/src/util/rbs_constant_pool.c b/src/util/rbs_constant_pool.c index 343902b51..d260e841a 100644 --- a/src/util/rbs_constant_pool.c +++ b/src/util/rbs_constant_pool.c @@ -302,7 +302,7 @@ rbs_constant_pool_insert_shared(rbs_constant_pool_t *pool, const uint8_t *start, } rbs_constant_id_t -rbs_constant_pool_insert_shared_with_encoding(rbs_constant_pool_t *pool, const uint8_t *start, size_t length, void *encoding) { +rbs_constant_pool_insert_shared_with_encoding(rbs_constant_pool_t *pool, const uint8_t *start, size_t length, const rbs_encoding_t *encoding) { return rbs_constant_pool_insert_shared(pool, start, length); } diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index 5d99caf64..7c23c882f 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -113,7 +113,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan <%- when "rbs_keyword" -%> rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.name %>)); // rbs_keyword <%- when "rbs_string" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_string_to_ruby_string(&node-><%= field.name %>)); + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_string_to_ruby_string(&node-><%= field.name %>, ctx.encoding)); <%- when "bool" -%> rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), node-><%= field.name %> ? Qtrue : Qfalse); <%- else -%> @@ -154,10 +154,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan assert(constant != NULL && "constant is NULL"); assert(constant->start != NULL && "constant->start is NULL"); - // FIXME: Add `rb_encoding` to the `ctx` and use it here. - rb_encoding *encoding = rb_usascii_encoding(); - - return ID2SYM(rb_intern3((const char *) constant->start, constant->length, encoding)); + return ID2SYM(rb_intern3((const char *) constant->start, constant->length, ctx.encoding)); } } } diff --git a/templates/ext/rbs_extension/ast_translation.h.erb b/templates/ext/rbs_extension/ast_translation.h.erb index f23f5a573..d83a1e42b 100644 --- a/templates/ext/rbs_extension/ast_translation.h.erb +++ b/templates/ext/rbs_extension/ast_translation.h.erb @@ -1,7 +1,19 @@ #ifndef RBS_EXTENSION_AST_TRANSLATION_H #define RBS_EXTENSION_AST_TRANSLATION_H +#include "ruby.h" +#include "ruby/encoding.h" + #include "rbs/ast.h" +#include "rbs/rbs_encoding.h" +#include "rbs/rbs_location.h" + +/// A bag of values needed when copying RBS C structs into Ruby objects. +typedef struct rbs_translation_context { + rbs_constant_pool_t *constant_pool; + VALUE buffer; + rb_encoding *encoding; +} rbs_translation_context_t; VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *list); VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t, rbs_hash_t *hash); diff --git a/templates/include/rbs/ast.h.erb b/templates/include/rbs/ast.h.erb index 0eb54f69a..bd0e7db4f 100644 --- a/templates/include/rbs/ast.h.erb +++ b/templates/include/rbs/ast.h.erb @@ -18,12 +18,6 @@ typedef struct rbs_node { enum rbs_node_type type; } rbs_node_t; -/// A bag of values needed when copying RBS C structs into Ruby objects. -typedef struct rbs_translation_context { - rbs_constant_pool_t *constant_pool; - VALUE buffer; -} rbs_translation_context_t; - /* rbs_node_list_node */ typedef struct rbs_node_list_node { From efa66464635f294bd87ebb9649cc47ce886701da Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Mon, 2 Dec 2024 15:14:34 -0500 Subject: [PATCH 028/111] Remove useless includes Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/ast_translation.c | 3 --- ext/rbs_extension/class_constants.h | 2 ++ ext/rbs_extension/lexer.c | 2 +- ext/rbs_extension/lexer.re | 2 +- ext/rbs_extension/lexstate.c | 4 +--- ext/rbs_extension/location.c | 1 + ext/rbs_extension/location.h | 1 + ext/rbs_extension/main.c | 1 + ext/rbs_extension/parser.c | 7 +------ ext/rbs_extension/parserstate.h | 1 - include/rbs/rbs_location.h | 1 - src/ast.c | 2 ++ templates/ext/rbs_extension/ast_translation.c.erb | 3 --- templates/ext/rbs_extension/class_constants.h.erb | 2 ++ templates/src/ast.c.erb | 2 ++ 15 files changed, 15 insertions(+), 19 deletions(-) diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index ca15fa903..ab980a172 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -7,9 +7,6 @@ #include "ast_translation.h" -#include "ruby.h" -#include "ruby/encoding.h" - #include "class_constants.h" #include "rbs_string_bridging.h" #include "location.h" diff --git a/ext/rbs_extension/class_constants.h b/ext/rbs_extension/class_constants.h index 8700b1b16..de571b4ca 100644 --- a/ext/rbs_extension/class_constants.h +++ b/ext/rbs_extension/class_constants.h @@ -8,6 +8,8 @@ #ifndef RBS__CONSTANTS_H #define RBS__CONSTANTS_H +#include "ruby.h" + extern VALUE RBS; extern VALUE RBS_AST; diff --git a/ext/rbs_extension/lexer.c b/ext/rbs_extension/lexer.c index 73984cf84..74df4acf2 100644 --- a/ext/rbs_extension/lexer.c +++ b/ext/rbs_extension/lexer.c @@ -1,6 +1,6 @@ /* Generated by re2c 3.1 */ #line 1 "ext/rbs_extension/lexer.re" -#include "rbs_extension.h" +#include "lexer.h" token rbsparser_next_token(lexstate *state) { lexstate backup; diff --git a/ext/rbs_extension/lexer.re b/ext/rbs_extension/lexer.re index aa1b94746..76204647d 100644 --- a/ext/rbs_extension/lexer.re +++ b/ext/rbs_extension/lexer.re @@ -1,4 +1,4 @@ -#include "rbs_extension.h" +#include "lexer.h" token rbsparser_next_token(lexstate *state) { lexstate backup; diff --git a/ext/rbs_extension/lexstate.c b/ext/rbs_extension/lexstate.c index c39fab4ee..c074d14b3 100644 --- a/ext/rbs_extension/lexstate.c +++ b/ext/rbs_extension/lexstate.c @@ -1,6 +1,4 @@ -#include "rbs_extension.h" -#include "ruby.h" -#include "rbs_string_bridging.h" +#include "lexer.h" #include "rbs/encoding.h" static const char *RBS_TOKENTYPE_NAMES[] = { diff --git a/ext/rbs_extension/location.c b/ext/rbs_extension/location.c index 905be13e2..dd58373d3 100644 --- a/ext/rbs_extension/location.c +++ b/ext/rbs_extension/location.c @@ -1,3 +1,4 @@ +#include "location.h" #include "rbs_extension.h" #define RBS_LOC_REQUIRED_P(loc, i) ((loc)->children->required_p & (1 << (i))) diff --git a/ext/rbs_extension/location.h b/ext/rbs_extension/location.h index 9ee501770..96b32e5e6 100644 --- a/ext/rbs_extension/location.h +++ b/ext/rbs_extension/location.h @@ -3,6 +3,7 @@ #include "ruby.h" #include "lexer.h" +#include "rbs/rbs_location.h" #include "rbs/rbs_location_internals.h" #include "rbs/util/rbs_constant_pool.h" diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 5aac8746b..791ee6050 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -2,6 +2,7 @@ #include "rbs/util/rbs_allocator.h" #include "rbs/util/rbs_constant_pool.h" #include "ast_translation.h" +#include "location.h" #include "ruby/vm.h" diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index 2bf1250fc..a3dd7f0c4 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -1,9 +1,4 @@ -#include "lexer.h" -#include "location.h" -#include "rbs_extension.h" -#include "rbs/util/rbs_constant_pool.h" -#include "rbs/ast.h" -#include "rbs/encoding.h" +#include "parser.h" #include "rbs/rbs_string.h" #include "ast_translation.h" #include "rbs/rbs_unescape.h" diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index 56def767e..8b619637c 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -8,7 +8,6 @@ #include "ruby/encoding.h" // TODO: remove this #include "lexer.h" -#include "location.h" #include "rbs/ast.h" /** diff --git a/include/rbs/rbs_location.h b/include/rbs/rbs_location.h index 83d1e4c60..797e5ecd3 100644 --- a/include/rbs/rbs_location.h +++ b/include/rbs/rbs_location.h @@ -1,7 +1,6 @@ #ifndef RBS__RBS_LOCATION_H #define RBS__RBS_LOCATION_H -#include "ruby.h" #include "lexer.h" #include "rbs/util/rbs_constant_pool.h" diff --git a/src/ast.c b/src/ast.c index 3acd2b7cc..41f4b7c5d 100644 --- a/src/ast.c +++ b/src/ast.c @@ -7,6 +7,8 @@ #include "rbs/ast.h" +#include + /* rbs_node_list */ rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *allocator) { diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index 7c23c882f..16995b696 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -1,8 +1,5 @@ #include "ast_translation.h" -#include "ruby.h" -#include "ruby/encoding.h" - #include "class_constants.h" #include "rbs_string_bridging.h" #include "location.h" diff --git a/templates/ext/rbs_extension/class_constants.h.erb b/templates/ext/rbs_extension/class_constants.h.erb index bc8f44f87..fadb2ed9a 100644 --- a/templates/ext/rbs_extension/class_constants.h.erb +++ b/templates/ext/rbs_extension/class_constants.h.erb @@ -1,6 +1,8 @@ #ifndef RBS__CONSTANTS_H #define RBS__CONSTANTS_H +#include "ruby.h" + extern VALUE RBS; extern VALUE RBS_AST; diff --git a/templates/src/ast.c.erb b/templates/src/ast.c.erb index 286df16f6..a965e2e9b 100644 --- a/templates/src/ast.c.erb +++ b/templates/src/ast.c.erb @@ -1,5 +1,7 @@ #include "rbs/ast.h" +#include + /* rbs_node_list */ rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *allocator) { From 89b4e4ba40140a4abcfa4b2209341a1d2b0b1f94 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Mon, 2 Dec 2024 16:30:38 -0500 Subject: [PATCH 029/111] Remove last `ruby/encoding` dependencies from parser.h Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/parser.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index a3dd7f0c4..b2511728f 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -1,4 +1,5 @@ #include "parser.h" +#include "rbs/encoding.h" #include "rbs/rbs_string.h" #include "ast_translation.h" #include "rbs/rbs_unescape.h" @@ -1488,18 +1489,18 @@ static bool parse_type_decl(parserstate *state, position comment_pos, rbs_node_l */ NODISCARD static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotation) { - VALUE content = rb_funcall(state->buffer, rb_intern("content"), 0); - rb_encoding *enc = rb_enc_get(content); - range rg = state->current_token.range; - int offset_bytes = rb_enc_codelen('%', enc) + rb_enc_codelen('a', enc); + size_t offset_bytes = + state->lexstate->encoding->char_width((const uint8_t *) "%", (size_t) 1) + + state->lexstate->encoding->char_width((const uint8_t *) "a", (size_t) 1); - unsigned int open_char = rb_enc_mbc_to_codepoint( - state->lexstate->string.start + rg.start.byte_pos + offset_bytes, - state->lexstate->string.end, - enc - ); + rbs_string_t str = { + .start = state->lexstate->string.start + rg.start.byte_pos + offset_bytes, + .end = state->lexstate->string.end, + .type = RBS_STRING_SHARED, + }; + unsigned int open_char = utf8_to_codepoint(str); unsigned int close_char; @@ -1524,8 +1525,8 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati return false; } - int open_bytes = rb_enc_codelen(open_char, enc); - int close_bytes = rb_enc_codelen(close_char, enc); + size_t open_bytes = state->lexstate->encoding->char_width((const uint8_t *) &open_char, (size_t) 1); + size_t close_bytes = state->lexstate->encoding->char_width((const uint8_t *) &close_char, (size_t) 1); rbs_string_t string = rbs_parser_get_current_token(state); rbs_string_drop_first(&string, offset_bytes + open_bytes); From c87a4366d7fdcdadf247f414ae948102d85a267e Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 3 Dec 2024 10:32:04 -0500 Subject: [PATCH 030/111] Move lexer out of the extension directory Signed-off-by: Alexandre Terrasa --- Rakefile | 6 +- ext/rbs_extension/location.h | 2 +- ext/rbs_extension/parserstate.h | 2 +- ext/rbs_extension/rbs_extension.h | 2 +- {ext/rbs_extension => include/rbs}/lexer.h | 0 {ext/rbs_extension => src}/lexer.c | 352 ++++++++++----------- {ext/rbs_extension => src}/lexer.re | 2 +- {ext/rbs_extension => src}/lexstate.c | 2 +- 8 files changed, 184 insertions(+), 184 deletions(-) rename {ext/rbs_extension => include/rbs}/lexer.h (100%) rename {ext/rbs_extension => src}/lexer.c (87%) rename {ext/rbs_extension => src}/lexer.re (99%) rename {ext/rbs_extension => src}/lexstate.c (99%) diff --git a/Rakefile b/Rakefile index 9f70123f7..1ad3bac77 100644 --- a/Rakefile +++ b/Rakefile @@ -22,12 +22,12 @@ end multitask :default => [:test, :stdlib_test, :typecheck_test, :rubocop, :validate, :test_doc] task :lexer do - sh "re2c -W --no-generation-date -o ext/rbs_extension/lexer.c ext/rbs_extension/lexer.re" + sh "re2c -W --no-generation-date -o src/lexer.c src/lexer.re" end task :confirm_lexer => :lexer do puts "Testing if lexer.c is updated with respect to lexer.re" - sh "git diff --exit-code ext/rbs_extension/lexer.c" + sh "git diff --exit-code src/lexer.c" end task :confirm_templates => :templates do @@ -66,9 +66,9 @@ task :templates do sh "#{ruby} templates/template.rb src/ast.c" end -task :compile => "ext/rbs_extension/lexer.c" task :compile => "ext/rbs_extension/class_constants.h" task :compile => "ext/rbs_extension/class_constants.c" +task :compile => "src/lexer.c" task :test_doc do files = Dir.chdir(File.expand_path('..', __FILE__)) do diff --git a/ext/rbs_extension/location.h b/ext/rbs_extension/location.h index 96b32e5e6..c3e030f81 100644 --- a/ext/rbs_extension/location.h +++ b/ext/rbs_extension/location.h @@ -2,7 +2,7 @@ #define RBS_LOCATION_H #include "ruby.h" -#include "lexer.h" +#include "rbs/lexer.h" #include "rbs/rbs_location.h" #include "rbs/rbs_location_internals.h" #include "rbs/util/rbs_constant_pool.h" diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index 8b619637c..d350fc8da 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -7,7 +7,7 @@ #include "rbs/util/rbs_constant_pool.h" #include "ruby/encoding.h" // TODO: remove this -#include "lexer.h" +#include "rbs/lexer.h" #include "rbs/ast.h" /** diff --git a/ext/rbs_extension/rbs_extension.h b/ext/rbs_extension/rbs_extension.h index c22ac2a89..67704b412 100644 --- a/ext/rbs_extension/rbs_extension.h +++ b/ext/rbs_extension/rbs_extension.h @@ -6,7 +6,7 @@ #include "class_constants.h" #include "rbs.h" -#include "lexer.h" +#include "rbs/lexer.h" #include "parser.h" /** diff --git a/ext/rbs_extension/lexer.h b/include/rbs/lexer.h similarity index 100% rename from ext/rbs_extension/lexer.h rename to include/rbs/lexer.h diff --git a/ext/rbs_extension/lexer.c b/src/lexer.c similarity index 87% rename from ext/rbs_extension/lexer.c rename to src/lexer.c index 74df4acf2..3d697809a 100644 --- a/ext/rbs_extension/lexer.c +++ b/src/lexer.c @@ -1,6 +1,6 @@ /* Generated by re2c 3.1 */ -#line 1 "ext/rbs_extension/lexer.re" -#include "lexer.h" +#line 1 "src/lexer.re" +#include "rbs/lexer.h" token rbsparser_next_token(lexstate *state) { lexstate backup; @@ -8,7 +8,7 @@ token rbsparser_next_token(lexstate *state) { backup = *state; -#line 12 "ext/rbs_extension/lexer.c" +#line 12 "src/lexer.c" { unsigned int yych; unsigned int yyaccept = 0; @@ -115,24 +115,24 @@ token rbsparser_next_token(lexstate *state) { } yy1: rbs_skip(state); -#line 144 "ext/rbs_extension/lexer.re" +#line 144 "src/lexer.re" { return next_eof_token(state); } -#line 121 "ext/rbs_extension/lexer.c" +#line 121 "src/lexer.c" yy2: rbs_skip(state); yy3: -#line 145 "ext/rbs_extension/lexer.re" +#line 145 "src/lexer.re" { return next_token(state, ErrorToken); } -#line 127 "ext/rbs_extension/lexer.c" +#line 127 "src/lexer.c" yy4: rbs_skip(state); yych = peek(state); if (yych == '\t') goto yy4; if (yych == ' ') goto yy4; yy5: -#line 143 "ext/rbs_extension/lexer.re" +#line 143 "src/lexer.re" { return next_token(state, tTRIVIA); } -#line 136 "ext/rbs_extension/lexer.c" +#line 136 "src/lexer.c" yy6: rbs_skip(state); goto yy5; @@ -142,9 +142,9 @@ token rbsparser_next_token(lexstate *state) { if (yych == '=') goto yy24; if (yych == '~') goto yy24; yy8: -#line 48 "ext/rbs_extension/lexer.re" +#line 48 "src/lexer.re" { return next_token(state, tOPERATOR); } -#line 148 "ext/rbs_extension/lexer.c" +#line 148 "src/lexer.c" yy9: yyaccept = 0; rbs_skip(state); @@ -158,14 +158,14 @@ token rbsparser_next_token(lexstate *state) { if (yych <= 0x00000000) goto yy11; if (yych != '\n') goto yy10; yy11: -#line 59 "ext/rbs_extension/lexer.re" +#line 59 "src/lexer.re" { return next_token( state, state->first_token_of_line ? tLINECOMMENT : tCOMMENT ); } -#line 169 "ext/rbs_extension/lexer.c" +#line 169 "src/lexer.c" yy12: rbs_skip(state); yych = peek(state); @@ -221,9 +221,9 @@ token rbsparser_next_token(lexstate *state) { goto yy8; yy14: rbs_skip(state); -#line 33 "ext/rbs_extension/lexer.re" +#line 33 "src/lexer.re" { return next_token(state, pAMP); } -#line 227 "ext/rbs_extension/lexer.c" +#line 227 "src/lexer.c" yy15: yyaccept = 0; rbs_skip(state); @@ -233,21 +233,21 @@ token rbsparser_next_token(lexstate *state) { goto yy76; yy16: rbs_skip(state); -#line 24 "ext/rbs_extension/lexer.re" +#line 24 "src/lexer.re" { return next_token(state, pLPAREN); } -#line 239 "ext/rbs_extension/lexer.c" +#line 239 "src/lexer.c" yy17: rbs_skip(state); -#line 25 "ext/rbs_extension/lexer.re" +#line 25 "src/lexer.re" { return next_token(state, pRPAREN); } -#line 244 "ext/rbs_extension/lexer.c" +#line 244 "src/lexer.c" yy18: rbs_skip(state); yych = peek(state); if (yych == '*') goto yy80; -#line 35 "ext/rbs_extension/lexer.re" +#line 35 "src/lexer.re" { return next_token(state, pSTAR); } -#line 251 "ext/rbs_extension/lexer.c" +#line 251 "src/lexer.c" yy19: rbs_skip(state); yych = peek(state); @@ -257,9 +257,9 @@ token rbsparser_next_token(lexstate *state) { goto yy8; yy20: rbs_skip(state); -#line 30 "ext/rbs_extension/lexer.re" +#line 30 "src/lexer.re" { return next_token(state, pCOMMA); } -#line 263 "ext/rbs_extension/lexer.c" +#line 263 "src/lexer.c" yy21: rbs_skip(state); yych = peek(state); @@ -279,9 +279,9 @@ token rbsparser_next_token(lexstate *state) { yych = peek(state); if (yych == '.') goto yy82; yy23: -#line 37 "ext/rbs_extension/lexer.re" +#line 37 "src/lexer.re" { return next_token(state, pDOT); } -#line 285 "ext/rbs_extension/lexer.c" +#line 285 "src/lexer.c" yy24: rbs_skip(state); goto yy8; @@ -292,9 +292,9 @@ token rbsparser_next_token(lexstate *state) { if (yych <= '9') goto yy25; if (yych == '_') goto yy25; yy26: -#line 51 "ext/rbs_extension/lexer.re" +#line 51 "src/lexer.re" { return next_token(state, tINTEGER); } -#line 298 "ext/rbs_extension/lexer.c" +#line 298 "src/lexer.c" yy27: yyaccept = 3; rbs_skip(state); @@ -377,9 +377,9 @@ token rbsparser_next_token(lexstate *state) { default: goto yy28; } yy28: -#line 44 "ext/rbs_extension/lexer.re" +#line 44 "src/lexer.re" { return next_token(state, pCOLON); } -#line 383 "ext/rbs_extension/lexer.c" +#line 383 "src/lexer.c" yy29: rbs_skip(state); yych = peek(state); @@ -387,9 +387,9 @@ token rbsparser_next_token(lexstate *state) { if (yych <= '<') goto yy24; if (yych <= '=') goto yy99; yy30: -#line 46 "ext/rbs_extension/lexer.re" +#line 46 "src/lexer.re" { return next_token(state, pLT); } -#line 393 "ext/rbs_extension/lexer.c" +#line 393 "src/lexer.c" yy31: rbs_skip(state); yych = peek(state); @@ -401,9 +401,9 @@ token rbsparser_next_token(lexstate *state) { if (yych == '~') goto yy24; } yy32: -#line 43 "ext/rbs_extension/lexer.re" +#line 43 "src/lexer.re" { return next_token(state, pEQ); } -#line 407 "ext/rbs_extension/lexer.c" +#line 407 "src/lexer.c" yy33: rbs_skip(state); yych = peek(state); @@ -412,9 +412,9 @@ token rbsparser_next_token(lexstate *state) { goto yy8; yy34: rbs_skip(state); -#line 34 "ext/rbs_extension/lexer.re" +#line 34 "src/lexer.re" { return next_token(state, pQUESTION); } -#line 418 "ext/rbs_extension/lexer.c" +#line 418 "src/lexer.c" yy35: yyaccept = 0; rbs_skip(state); @@ -450,26 +450,26 @@ token rbsparser_next_token(lexstate *state) { } } yy37: -#line 129 "ext/rbs_extension/lexer.re" +#line 129 "src/lexer.re" { return next_token(state, tUIDENT); } -#line 456 "ext/rbs_extension/lexer.c" +#line 456 "src/lexer.c" yy38: rbs_skip(state); yych = peek(state); if (yych == ']') goto yy107; -#line 26 "ext/rbs_extension/lexer.re" +#line 26 "src/lexer.re" { return next_token(state, pLBRACKET); } -#line 463 "ext/rbs_extension/lexer.c" +#line 463 "src/lexer.c" yy39: rbs_skip(state); -#line 27 "ext/rbs_extension/lexer.re" +#line 27 "src/lexer.re" { return next_token(state, pRBRACKET); } -#line 468 "ext/rbs_extension/lexer.c" +#line 468 "src/lexer.c" yy40: rbs_skip(state); -#line 32 "ext/rbs_extension/lexer.re" +#line 32 "src/lexer.re" { return next_token(state, pHAT); } -#line 473 "ext/rbs_extension/lexer.c" +#line 473 "src/lexer.c" yy41: rbs_skip(state); yych = peek(state); @@ -491,9 +491,9 @@ token rbsparser_next_token(lexstate *state) { } } yy42: -#line 132 "ext/rbs_extension/lexer.re" +#line 132 "src/lexer.re" { return next_token(state, tULLIDENT); } -#line 497 "ext/rbs_extension/lexer.c" +#line 497 "src/lexer.c" yy43: yyaccept = 4; rbs_skip(state); @@ -506,9 +506,9 @@ token rbsparser_next_token(lexstate *state) { if (yych != ':') goto yy114; } yy44: -#line 39 "ext/rbs_extension/lexer.re" +#line 39 "src/lexer.re" { return next_token(state, tOPERATOR); } -#line 512 "ext/rbs_extension/lexer.c" +#line 512 "src/lexer.c" yy45: rbs_skip(state); yych = peek(state); @@ -521,9 +521,9 @@ token rbsparser_next_token(lexstate *state) { goto yy53; } yy46: -#line 128 "ext/rbs_extension/lexer.re" +#line 128 "src/lexer.re" { return next_token(state, tLIDENT); } -#line 527 "ext/rbs_extension/lexer.c" +#line 527 "src/lexer.c" yy47: rbs_skip(state); yych = peek(state); @@ -630,19 +630,19 @@ token rbsparser_next_token(lexstate *state) { goto yy53; yy63: rbs_skip(state); -#line 28 "ext/rbs_extension/lexer.re" +#line 28 "src/lexer.re" { return next_token(state, pLBRACE); } -#line 636 "ext/rbs_extension/lexer.c" +#line 636 "src/lexer.c" yy64: rbs_skip(state); -#line 31 "ext/rbs_extension/lexer.re" +#line 31 "src/lexer.re" { return next_token(state, pBAR); } -#line 641 "ext/rbs_extension/lexer.c" +#line 641 "src/lexer.c" yy65: rbs_skip(state); -#line 29 "ext/rbs_extension/lexer.re" +#line 29 "src/lexer.re" { return next_token(state, pRBRACE); } -#line 646 "ext/rbs_extension/lexer.c" +#line 646 "src/lexer.c" yy66: rbs_skip(state); yych = peek(state); @@ -684,9 +684,9 @@ token rbsparser_next_token(lexstate *state) { } yy69: rbs_skip(state); -#line 106 "ext/rbs_extension/lexer.re" +#line 106 "src/lexer.re" { return next_token(state, tDQSTRING); } -#line 690 "ext/rbs_extension/lexer.c" +#line 690 "src/lexer.c" yy70: rbs_skip(state); yych = peek(state); @@ -723,9 +723,9 @@ token rbsparser_next_token(lexstate *state) { } } yy72: -#line 139 "ext/rbs_extension/lexer.re" +#line 139 "src/lexer.re" { return next_token(state, tGIDENT); } -#line 729 "ext/rbs_extension/lexer.c" +#line 729 "src/lexer.c" yy73: rbs_skip(state); goto yy72; @@ -764,9 +764,9 @@ token rbsparser_next_token(lexstate *state) { yy77: rbs_skip(state); yy78: -#line 107 "ext/rbs_extension/lexer.re" +#line 107 "src/lexer.re" { return next_token(state, tSQSTRING); } -#line 770 "ext/rbs_extension/lexer.c" +#line 770 "src/lexer.c" yy79: rbs_skip(state); yych = peek(state); @@ -780,14 +780,14 @@ token rbsparser_next_token(lexstate *state) { } yy80: rbs_skip(state); -#line 36 "ext/rbs_extension/lexer.re" +#line 36 "src/lexer.re" { return next_token(state, pSTAR2); } -#line 786 "ext/rbs_extension/lexer.c" +#line 786 "src/lexer.c" yy81: rbs_skip(state); -#line 41 "ext/rbs_extension/lexer.re" +#line 41 "src/lexer.re" { return next_token(state, pARROW); } -#line 791 "ext/rbs_extension/lexer.c" +#line 791 "src/lexer.c" yy82: rbs_skip(state); yych = peek(state); @@ -799,9 +799,9 @@ token rbsparser_next_token(lexstate *state) { if (yych == '=') goto yy87; if (yych == '~') goto yy87; yy84: -#line 126 "ext/rbs_extension/lexer.re" +#line 126 "src/lexer.re" { return next_token(state, tSYMBOL); } -#line 805 "ext/rbs_extension/lexer.c" +#line 805 "src/lexer.c" yy85: rbs_skip(state); yych = peek(state); @@ -885,9 +885,9 @@ token rbsparser_next_token(lexstate *state) { goto yy84; yy91: rbs_skip(state); -#line 45 "ext/rbs_extension/lexer.re" +#line 45 "src/lexer.re" { return next_token(state, pCOLON2); } -#line 891 "ext/rbs_extension/lexer.c" +#line 891 "src/lexer.c" yy92: rbs_skip(state); yych = peek(state); @@ -941,9 +941,9 @@ token rbsparser_next_token(lexstate *state) { } } yy97: -#line 122 "ext/rbs_extension/lexer.re" +#line 122 "src/lexer.re" { return next_token(state, tSYMBOL); } -#line 947 "ext/rbs_extension/lexer.c" +#line 947 "src/lexer.c" yy98: rbs_skip(state); yych = peek(state); @@ -961,9 +961,9 @@ token rbsparser_next_token(lexstate *state) { goto yy8; yy101: rbs_skip(state); -#line 42 "ext/rbs_extension/lexer.re" +#line 42 "src/lexer.re" { return next_token(state, pFATARROW); } -#line 967 "ext/rbs_extension/lexer.c" +#line 967 "src/lexer.c" yy102: rbs_skip(state); yych = peek(state); @@ -992,26 +992,26 @@ token rbsparser_next_token(lexstate *state) { } } yy104: -#line 136 "ext/rbs_extension/lexer.re" +#line 136 "src/lexer.re" { return next_token(state, tAIDENT); } -#line 998 "ext/rbs_extension/lexer.c" +#line 998 "src/lexer.c" yy105: rbs_skip(state); -#line 133 "ext/rbs_extension/lexer.re" +#line 133 "src/lexer.re" { return next_token(state, tBANGIDENT); } -#line 1003 "ext/rbs_extension/lexer.c" +#line 1003 "src/lexer.c" yy106: rbs_skip(state); -#line 134 "ext/rbs_extension/lexer.re" +#line 134 "src/lexer.re" { return next_token(state, tEQIDENT); } -#line 1008 "ext/rbs_extension/lexer.c" +#line 1008 "src/lexer.c" yy107: rbs_skip(state); yych = peek(state); if (yych == '=') goto yy24; -#line 47 "ext/rbs_extension/lexer.re" +#line 47 "src/lexer.re" { return next_token(state, pAREF_OPR); } -#line 1015 "ext/rbs_extension/lexer.c" +#line 1015 "src/lexer.c" yy108: rbs_skip(state); yych = peek(state); @@ -1033,9 +1033,9 @@ token rbsparser_next_token(lexstate *state) { } } yy110: -#line 130 "ext/rbs_extension/lexer.re" +#line 130 "src/lexer.re" { return next_token(state, tULLIDENT); } -#line 1039 "ext/rbs_extension/lexer.c" +#line 1039 "src/lexer.c" yy111: rbs_skip(state); yych = peek(state); @@ -1056,9 +1056,9 @@ token rbsparser_next_token(lexstate *state) { } } yy112: -#line 131 "ext/rbs_extension/lexer.re" +#line 131 "src/lexer.re" { return next_token(state, tULIDENT); } -#line 1062 "ext/rbs_extension/lexer.c" +#line 1062 "src/lexer.c" yy113: rbs_skip(state); yych = peek(state); @@ -1095,9 +1095,9 @@ token rbsparser_next_token(lexstate *state) { } } yy117: -#line 96 "ext/rbs_extension/lexer.re" +#line 96 "src/lexer.re" { return next_token(state, kAS); } -#line 1101 "ext/rbs_extension/lexer.c" +#line 1101 "src/lexer.c" yy118: rbs_skip(state); yych = peek(state); @@ -1165,9 +1165,9 @@ token rbsparser_next_token(lexstate *state) { } } yy126: -#line 77 "ext/rbs_extension/lexer.re" +#line 77 "src/lexer.re" { return next_token(state, kIN); } -#line 1171 "ext/rbs_extension/lexer.c" +#line 1171 "src/lexer.c" yy127: rbs_skip(state); yych = peek(state); @@ -1301,14 +1301,14 @@ token rbsparser_next_token(lexstate *state) { } yy148: rbs_skip(state); -#line 38 "ext/rbs_extension/lexer.re" +#line 38 "src/lexer.re" { return next_token(state, pDOT3); } -#line 1307 "ext/rbs_extension/lexer.c" +#line 1307 "src/lexer.c" yy149: rbs_skip(state); -#line 108 "ext/rbs_extension/lexer.re" +#line 108 "src/lexer.re" { return next_token(state, tDQSYMBOL); } -#line 1312 "ext/rbs_extension/lexer.c" +#line 1312 "src/lexer.c" yy150: rbs_skip(state); yych = peek(state); @@ -1345,18 +1345,18 @@ token rbsparser_next_token(lexstate *state) { } } yy152: -#line 125 "ext/rbs_extension/lexer.re" +#line 125 "src/lexer.re" { return next_token(state, tSYMBOL); } -#line 1351 "ext/rbs_extension/lexer.c" +#line 1351 "src/lexer.c" yy153: rbs_skip(state); goto yy152; yy154: rbs_skip(state); yy155: -#line 109 "ext/rbs_extension/lexer.re" +#line 109 "src/lexer.re" { return next_token(state, tSQSYMBOL); } -#line 1360 "ext/rbs_extension/lexer.c" +#line 1360 "src/lexer.c" yy156: rbs_skip(state); yych = peek(state); @@ -1411,9 +1411,9 @@ token rbsparser_next_token(lexstate *state) { } } yy161: -#line 123 "ext/rbs_extension/lexer.re" +#line 123 "src/lexer.re" { return next_token(state, tSYMBOL); } -#line 1417 "ext/rbs_extension/lexer.c" +#line 1417 "src/lexer.c" yy162: rbs_skip(state); goto yy97; @@ -1433,9 +1433,9 @@ token rbsparser_next_token(lexstate *state) { } } yy164: -#line 137 "ext/rbs_extension/lexer.re" +#line 137 "src/lexer.re" { return next_token(state, tA2IDENT); } -#line 1439 "ext/rbs_extension/lexer.c" +#line 1439 "src/lexer.c" yy165: rbs_skip(state); yych = peek(state); @@ -1443,9 +1443,9 @@ token rbsparser_next_token(lexstate *state) { goto yy109; yy166: rbs_skip(state); -#line 40 "ext/rbs_extension/lexer.re" +#line 40 "src/lexer.re" { return next_token(state, tQIDENT); } -#line 1449 "ext/rbs_extension/lexer.c" +#line 1449 "src/lexer.c" yy167: rbs_skip(state); yych = peek(state); @@ -1481,9 +1481,9 @@ token rbsparser_next_token(lexstate *state) { } } yy171: -#line 71 "ext/rbs_extension/lexer.re" +#line 71 "src/lexer.re" { return next_token(state, kBOT); } -#line 1487 "ext/rbs_extension/lexer.c" +#line 1487 "src/lexer.c" yy172: rbs_skip(state); yych = peek(state); @@ -1509,9 +1509,9 @@ token rbsparser_next_token(lexstate *state) { } } yy174: -#line 73 "ext/rbs_extension/lexer.re" +#line 73 "src/lexer.re" { return next_token(state, kDEF); } -#line 1515 "ext/rbs_extension/lexer.c" +#line 1515 "src/lexer.c" yy175: rbs_skip(state); yych = peek(state); @@ -1532,9 +1532,9 @@ token rbsparser_next_token(lexstate *state) { } } yy176: -#line 74 "ext/rbs_extension/lexer.re" +#line 74 "src/lexer.re" { return next_token(state, kEND); } -#line 1538 "ext/rbs_extension/lexer.c" +#line 1538 "src/lexer.c" yy177: rbs_skip(state); yych = peek(state); @@ -1585,9 +1585,9 @@ token rbsparser_next_token(lexstate *state) { } } yy184: -#line 82 "ext/rbs_extension/lexer.re" +#line 82 "src/lexer.re" { return next_token(state, kNIL); } -#line 1591 "ext/rbs_extension/lexer.c" +#line 1591 "src/lexer.c" yy185: rbs_skip(state); yych = peek(state); @@ -1608,9 +1608,9 @@ token rbsparser_next_token(lexstate *state) { } } yy186: -#line 83 "ext/rbs_extension/lexer.re" +#line 83 "src/lexer.re" { return next_token(state, kOUT); } -#line 1614 "ext/rbs_extension/lexer.c" +#line 1614 "src/lexer.c" yy187: rbs_skip(state); yych = peek(state); @@ -1656,9 +1656,9 @@ token rbsparser_next_token(lexstate *state) { } } yy193: -#line 89 "ext/rbs_extension/lexer.re" +#line 89 "src/lexer.re" { return next_token(state, kTOP); } -#line 1662 "ext/rbs_extension/lexer.c" +#line 1662 "src/lexer.c" yy194: rbs_skip(state); yych = peek(state); @@ -1699,9 +1699,9 @@ token rbsparser_next_token(lexstate *state) { } } yy199: -#line 95 "ext/rbs_extension/lexer.re" +#line 95 "src/lexer.re" { return next_token(state, kUSE); } -#line 1705 "ext/rbs_extension/lexer.c" +#line 1705 "src/lexer.c" yy200: rbs_skip(state); yych = peek(state); @@ -1722,29 +1722,29 @@ token rbsparser_next_token(lexstate *state) { } yy202: rbs_skip(state); -#line 54 "ext/rbs_extension/lexer.re" +#line 54 "src/lexer.re" { return next_token(state, tANNOTATION); } -#line 1728 "ext/rbs_extension/lexer.c" +#line 1728 "src/lexer.c" yy203: rbs_skip(state); -#line 57 "ext/rbs_extension/lexer.re" +#line 57 "src/lexer.re" { return next_token(state, tANNOTATION); } -#line 1733 "ext/rbs_extension/lexer.c" +#line 1733 "src/lexer.c" yy204: rbs_skip(state); -#line 55 "ext/rbs_extension/lexer.re" +#line 55 "src/lexer.re" { return next_token(state, tANNOTATION); } -#line 1738 "ext/rbs_extension/lexer.c" +#line 1738 "src/lexer.c" yy205: rbs_skip(state); -#line 53 "ext/rbs_extension/lexer.re" +#line 53 "src/lexer.re" { return next_token(state, tANNOTATION); } -#line 1743 "ext/rbs_extension/lexer.c" +#line 1743 "src/lexer.c" yy206: rbs_skip(state); -#line 56 "ext/rbs_extension/lexer.re" +#line 56 "src/lexer.re" { return next_token(state, tANNOTATION); } -#line 1748 "ext/rbs_extension/lexer.c" +#line 1748 "src/lexer.c" yy207: rbs_skip(state); yych = peek(state); @@ -1800,9 +1800,9 @@ token rbsparser_next_token(lexstate *state) { } } yy211: -#line 124 "ext/rbs_extension/lexer.re" +#line 124 "src/lexer.re" { return next_token(state, tSYMBOL); } -#line 1806 "ext/rbs_extension/lexer.c" +#line 1806 "src/lexer.c" yy212: rbs_skip(state); goto yy161; @@ -1841,9 +1841,9 @@ token rbsparser_next_token(lexstate *state) { } } yy217: -#line 70 "ext/rbs_extension/lexer.re" +#line 70 "src/lexer.re" { return next_token(state, kBOOL); } -#line 1847 "ext/rbs_extension/lexer.c" +#line 1847 "src/lexer.c" yy218: rbs_skip(state); yych = peek(state); @@ -1914,9 +1914,9 @@ token rbsparser_next_token(lexstate *state) { } } yy229: -#line 87 "ext/rbs_extension/lexer.re" +#line 87 "src/lexer.re" { return next_token(state, kSELF); } -#line 1920 "ext/rbs_extension/lexer.c" +#line 1920 "src/lexer.c" yy230: rbs_skip(state); yych = peek(state); @@ -1942,9 +1942,9 @@ token rbsparser_next_token(lexstate *state) { } } yy232: -#line 90 "ext/rbs_extension/lexer.re" +#line 90 "src/lexer.re" { return next_token(state, kTRUE); } -#line 1948 "ext/rbs_extension/lexer.c" +#line 1948 "src/lexer.c" yy233: rbs_skip(state); yych = peek(state); @@ -1965,9 +1965,9 @@ token rbsparser_next_token(lexstate *state) { } } yy234: -#line 91 "ext/rbs_extension/lexer.re" +#line 91 "src/lexer.re" { return next_token(state, kTYPE); } -#line 1971 "ext/rbs_extension/lexer.c" +#line 1971 "src/lexer.c" yy235: rbs_skip(state); yych = peek(state); @@ -1998,9 +1998,9 @@ token rbsparser_next_token(lexstate *state) { } } yy238: -#line 94 "ext/rbs_extension/lexer.re" +#line 94 "src/lexer.re" { return next_token(state, kVOID); } -#line 2004 "ext/rbs_extension/lexer.c" +#line 2004 "src/lexer.c" yy239: rbs_skip(state); yych = peek(state); @@ -2055,9 +2055,9 @@ token rbsparser_next_token(lexstate *state) { } } yy244: -#line 66 "ext/rbs_extension/lexer.re" +#line 66 "src/lexer.re" { return next_token(state, kALIAS); } -#line 2061 "ext/rbs_extension/lexer.c" +#line 2061 "src/lexer.c" yy245: rbs_skip(state); yych = peek(state); @@ -2089,9 +2089,9 @@ token rbsparser_next_token(lexstate *state) { } } yy247: -#line 72 "ext/rbs_extension/lexer.re" +#line 72 "src/lexer.re" { return next_token(state, kCLASS); } -#line 2095 "ext/rbs_extension/lexer.c" +#line 2095 "src/lexer.c" yy248: rbs_skip(state); yych = peek(state); @@ -2117,9 +2117,9 @@ token rbsparser_next_token(lexstate *state) { } } yy250: -#line 76 "ext/rbs_extension/lexer.re" +#line 76 "src/lexer.re" { return next_token(state, kFALSE); } -#line 2123 "ext/rbs_extension/lexer.c" +#line 2123 "src/lexer.c" yy251: rbs_skip(state); yych = peek(state); @@ -2236,9 +2236,9 @@ token rbsparser_next_token(lexstate *state) { } } yy268: -#line 75 "ext/rbs_extension/lexer.re" +#line 75 "src/lexer.re" { return next_token(state, kEXTEND); } -#line 2242 "ext/rbs_extension/lexer.c" +#line 2242 "src/lexer.c" yy269: rbs_skip(state); yych = peek(state); @@ -2274,9 +2274,9 @@ token rbsparser_next_token(lexstate *state) { } } yy273: -#line 81 "ext/rbs_extension/lexer.re" +#line 81 "src/lexer.re" { return next_token(state, kMODULE); } -#line 2280 "ext/rbs_extension/lexer.c" +#line 2280 "src/lexer.c" yy274: rbs_skip(state); yych = peek(state); @@ -2307,9 +2307,9 @@ token rbsparser_next_token(lexstate *state) { } } yy277: -#line 86 "ext/rbs_extension/lexer.re" +#line 86 "src/lexer.re" { return next_token(state, kPUBLIC); } -#line 2313 "ext/rbs_extension/lexer.c" +#line 2313 "src/lexer.c" yy278: rbs_skip(state); yych = peek(state); @@ -2378,9 +2378,9 @@ token rbsparser_next_token(lexstate *state) { } } yy287: -#line 78 "ext/rbs_extension/lexer.re" +#line 78 "src/lexer.re" { return next_token(state, kINCLUDE); } -#line 2384 "ext/rbs_extension/lexer.c" +#line 2384 "src/lexer.c" yy288: rbs_skip(state); yych = peek(state); @@ -2411,9 +2411,9 @@ token rbsparser_next_token(lexstate *state) { } } yy291: -#line 84 "ext/rbs_extension/lexer.re" +#line 84 "src/lexer.re" { return next_token(state, kPREPEND); } -#line 2417 "ext/rbs_extension/lexer.c" +#line 2417 "src/lexer.c" yy292: rbs_skip(state); yych = peek(state); @@ -2434,9 +2434,9 @@ token rbsparser_next_token(lexstate *state) { } } yy293: -#line 85 "ext/rbs_extension/lexer.re" +#line 85 "src/lexer.re" { return next_token(state, kPRIVATE); } -#line 2440 "ext/rbs_extension/lexer.c" +#line 2440 "src/lexer.c" yy294: rbs_skip(state); yych = peek(state); @@ -2467,9 +2467,9 @@ token rbsparser_next_token(lexstate *state) { } } yy297: -#line 93 "ext/rbs_extension/lexer.re" +#line 93 "src/lexer.re" { return next_token(state, kUNTYPED); } -#line 2473 "ext/rbs_extension/lexer.c" +#line 2473 "src/lexer.c" yy298: rbs_skip(state); yych = peek(state); @@ -2490,9 +2490,9 @@ token rbsparser_next_token(lexstate *state) { } } yy299: -#line 97 "ext/rbs_extension/lexer.re" +#line 97 "src/lexer.re" { return next_token(state, k__TODO__); } -#line 2496 "ext/rbs_extension/lexer.c" +#line 2496 "src/lexer.c" yy300: rbs_skip(state); yych = peek(state); @@ -2528,9 +2528,9 @@ token rbsparser_next_token(lexstate *state) { } } yy304: -#line 79 "ext/rbs_extension/lexer.re" +#line 79 "src/lexer.re" { return next_token(state, kINSTANCE); } -#line 2534 "ext/rbs_extension/lexer.c" +#line 2534 "src/lexer.c" yy305: rbs_skip(state); yych = peek(state); @@ -2581,9 +2581,9 @@ token rbsparser_next_token(lexstate *state) { } } yy312: -#line 80 "ext/rbs_extension/lexer.re" +#line 80 "src/lexer.re" { return next_token(state, kINTERFACE); } -#line 2587 "ext/rbs_extension/lexer.c" +#line 2587 "src/lexer.c" yy313: rbs_skip(state); yych = peek(state); @@ -2604,9 +2604,9 @@ token rbsparser_next_token(lexstate *state) { } } yy314: -#line 88 "ext/rbs_extension/lexer.re" +#line 88 "src/lexer.re" { return next_token(state, kSINGLETON); } -#line 2610 "ext/rbs_extension/lexer.c" +#line 2610 "src/lexer.c" yy315: rbs_skip(state); yych = peek(state); @@ -2627,9 +2627,9 @@ token rbsparser_next_token(lexstate *state) { } } yy316: -#line 92 "ext/rbs_extension/lexer.re" +#line 92 "src/lexer.re" { return next_token(state, kUNCHECKED); } -#line 2633 "ext/rbs_extension/lexer.c" +#line 2633 "src/lexer.c" yy317: rbs_skip(state); yych = peek(state); @@ -2670,9 +2670,9 @@ token rbsparser_next_token(lexstate *state) { } } yy322: -#line 68 "ext/rbs_extension/lexer.re" +#line 68 "src/lexer.re" { return next_token(state, kATTRREADER); } -#line 2676 "ext/rbs_extension/lexer.c" +#line 2676 "src/lexer.c" yy323: rbs_skip(state); yych = peek(state); @@ -2693,9 +2693,9 @@ token rbsparser_next_token(lexstate *state) { } } yy324: -#line 69 "ext/rbs_extension/lexer.re" +#line 69 "src/lexer.re" { return next_token(state, kATTRWRITER); } -#line 2699 "ext/rbs_extension/lexer.c" +#line 2699 "src/lexer.c" yy325: rbs_skip(state); yych = peek(state); @@ -2719,10 +2719,10 @@ token rbsparser_next_token(lexstate *state) { } } yy326: -#line 67 "ext/rbs_extension/lexer.re" +#line 67 "src/lexer.re" { return next_token(state, kATTRACCESSOR); } -#line 2725 "ext/rbs_extension/lexer.c" +#line 2725 "src/lexer.c" } -#line 146 "ext/rbs_extension/lexer.re" +#line 146 "src/lexer.re" } diff --git a/ext/rbs_extension/lexer.re b/src/lexer.re similarity index 99% rename from ext/rbs_extension/lexer.re rename to src/lexer.re index 76204647d..df9487b2c 100644 --- a/ext/rbs_extension/lexer.re +++ b/src/lexer.re @@ -1,4 +1,4 @@ -#include "lexer.h" +#include "rbs/lexer.h" token rbsparser_next_token(lexstate *state) { lexstate backup; diff --git a/ext/rbs_extension/lexstate.c b/src/lexstate.c similarity index 99% rename from ext/rbs_extension/lexstate.c rename to src/lexstate.c index c074d14b3..b5f187826 100644 --- a/ext/rbs_extension/lexstate.c +++ b/src/lexstate.c @@ -1,4 +1,4 @@ -#include "lexer.h" +#include "rbs/lexer.h" #include "rbs/encoding.h" static const char *RBS_TOKENTYPE_NAMES[] = { From 4b7621469c874e6677fa5f738269074e8830e59d Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 3 Dec 2024 10:56:32 -0500 Subject: [PATCH 031/111] Extract `alloc_lexer_from_buffer` Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/main.c | 13 +++---------- ext/rbs_extension/parserstate.c | 5 ++++- ext/rbs_extension/parserstate.h | 6 +++--- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 791ee6050..784f1ca95 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -65,9 +65,7 @@ static VALUE parse_type_try(VALUE a) { } static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) { - VALUE string = rb_funcall(buffer, rb_intern("content"), 0); - StringValue(string); - parserstate *parser = alloc_parser(buffer, string, FIX2INT(start_pos), FIX2INT(end_pos), variables); + parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables); struct parse_type_arg arg = { parser, require_eof @@ -104,9 +102,7 @@ static VALUE parse_method_type_try(VALUE a) { } static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) { - VALUE string = rb_funcall(buffer, rb_intern("content"), 0); - StringValue(string); - parserstate *parser = alloc_parser(buffer, string, FIX2INT(start_pos), FIX2INT(end_pos), variables); + parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables); struct parse_type_arg arg = { parser, require_eof @@ -134,9 +130,7 @@ static VALUE parse_signature_try(VALUE a) { } static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) { - VALUE string = rb_funcall(buffer, rb_intern("content"), 0); - StringValue(string); - parserstate *parser = alloc_parser(buffer, string, FIX2INT(start_pos), FIX2INT(end_pos), Qnil); + parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), Qnil); return rb_ensure(parse_signature_try, (VALUE)parser, ensure_free_parser, (VALUE)parser); } @@ -144,7 +138,6 @@ static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) { VALUE string = rb_funcall(buffer, rb_intern("content"), 0); StringValue(string); - rbs_allocator_t allocator; rbs_allocator_init(&allocator); lexstate *lexer = alloc_lexer(&allocator, string, 0, FIX2INT(end_pos)); diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index e959fcf43..9e61b9375 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -318,7 +318,10 @@ lexstate *alloc_lexer(rbs_allocator_t *allocator, VALUE string, int start_pos, i return lexer; } -parserstate *alloc_parser(VALUE buffer, VALUE string, int start_pos, int end_pos, VALUE variables) { +parserstate *alloc_parser(VALUE buffer, int start_pos, int end_pos, VALUE variables) { + VALUE string = rb_funcall(buffer, rb_intern("content"), 0); + StringValue(string); + rbs_allocator_t allocator; rbs_allocator_init(&allocator); diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index d350fc8da..af61c1b33 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -120,11 +120,11 @@ lexstate *alloc_lexer(rbs_allocator_t *, VALUE string, int start_pos, int end_po * Allocate new parserstate object. * * ``` - * alloc_parser(buffer, VALUE string, 0, 1, variables) // New parserstate with variables - * alloc_parser(buffer, VALUE string, 3, 5, Qnil) // New parserstate without variables + * alloc_parser(buffer, 0, 1, variables) // New parserstate with variables + * alloc_parser(buffer, 3, 5, Qnil) // New parserstate without variables * ``` * */ -parserstate *alloc_parser(VALUE buffer, VALUE string, int start_pos, int end_pos, VALUE variables); +parserstate *alloc_parser(VALUE buffer, int start_pos, int end_pos, VALUE variables); void free_parser(parserstate *parser); /** * Advance one token. From 5dab633991cfb4fda351acd240e9f90793d3b046 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 3 Dec 2024 11:02:28 -0500 Subject: [PATCH 032/111] Move the Ruby out of `alloc_lexer` Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/main.c | 54 ++++++++++++++++++++++++++++----- ext/rbs_extension/parserstate.c | 20 +++--------- ext/rbs_extension/parserstate.h | 8 ++--- 3 files changed, 55 insertions(+), 27 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 784f1ca95..09cde7234 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -3,6 +3,7 @@ #include "rbs/util/rbs_constant_pool.h" #include "ast_translation.h" #include "location.h" +#include "rbs_string_bridging.h" #include "ruby/vm.h" @@ -64,8 +65,50 @@ static VALUE parse_type_try(VALUE a) { return rbs_struct_to_ruby_value(ctx, type); } +static lexstate *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE buffer, int start_pos, int end_pos) { + if (start_pos < 0 || end_pos < 0) { + rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos); + } + + VALUE string = rb_funcall(buffer, rb_intern("content"), 0); + StringValue(string); + + rb_encoding *encoding = rb_enc_get(string); + const char *encoding_name = rb_enc_name(encoding); + + return alloc_lexer( + allocator, + rbs_string_from_ruby_string(string), + rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))), + start_pos, + end_pos + ); +} + +static parserstate *alloc_parser_from_buffer(VALUE buffer, int start_pos, int end_pos, VALUE variables) { + if (start_pos < 0 || end_pos < 0) { + rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos); + } + + VALUE string = rb_funcall(buffer, rb_intern("content"), 0); + StringValue(string); + + rb_encoding *encoding = rb_enc_get(string); + const char *encoding_name = rb_enc_name(encoding); + + return alloc_parser( + buffer, + rbs_string_from_ruby_string(string), + rbs_encoding_find((const uint8_t *) encoding_name, + (const uint8_t *) (encoding_name + strlen(encoding_name))), + start_pos, + end_pos, + variables + ); +} + static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) { - parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables); + parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables); struct parse_type_arg arg = { parser, require_eof @@ -102,7 +145,7 @@ static VALUE parse_method_type_try(VALUE a) { } static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) { - parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables); + parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables); struct parse_type_arg arg = { parser, require_eof @@ -130,17 +173,14 @@ static VALUE parse_signature_try(VALUE a) { } static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) { - parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), Qnil); + parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos), Qnil); return rb_ensure(parse_signature_try, (VALUE)parser, ensure_free_parser, (VALUE)parser); } static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) { - VALUE string = rb_funcall(buffer, rb_intern("content"), 0); - StringValue(string); - rbs_allocator_t allocator; rbs_allocator_init(&allocator); - lexstate *lexer = alloc_lexer(&allocator, string, 0, FIX2INT(end_pos)); + lexstate *lexer = alloc_lexer_from_buffer(&allocator, buffer, 0, FIX2INT(end_pos)); VALUE results = rb_ary_new(); token token = NullToken; diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index 9e61b9375..7f6090d3c 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -281,16 +281,7 @@ comment *comment_get_comment(comment *com, int line) { return comment_get_comment(com->next_comment, line); } -lexstate *alloc_lexer(rbs_allocator_t *allocator, VALUE string, int start_pos, int end_pos) { - if (start_pos < 0 || end_pos < 0) { - rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos); - } - - rb_encoding *ruby_encoding = rb_enc_get(string); - const char *encoding_name = rb_enc_name(ruby_encoding); - const char *encoding_name_end = encoding_name + strlen(encoding_name); - const rbs_encoding_t *encoding = rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) encoding_name_end); - +lexstate *alloc_lexer(rbs_allocator_t *allocator, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { lexstate *lexer = rbs_allocator_alloc(allocator, lexstate); position start_position = (position) { @@ -301,7 +292,7 @@ lexstate *alloc_lexer(rbs_allocator_t *allocator, VALUE string, int start_pos, i }; *lexer = (lexstate) { - .string = rbs_string_from_ruby_string(string), + .string = string, .start_pos = start_pos, .end_pos = end_pos, .current = start_position, @@ -318,14 +309,11 @@ lexstate *alloc_lexer(rbs_allocator_t *allocator, VALUE string, int start_pos, i return lexer; } -parserstate *alloc_parser(VALUE buffer, int start_pos, int end_pos, VALUE variables) { - VALUE string = rb_funcall(buffer, rb_intern("content"), 0); - StringValue(string); - +parserstate *alloc_parser(VALUE buffer, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos, VALUE variables) { rbs_allocator_t allocator; rbs_allocator_init(&allocator); - lexstate *lexer = alloc_lexer(&allocator, string, start_pos, end_pos); + lexstate *lexer = alloc_lexer(&allocator, string, encoding, start_pos, end_pos); parserstate *parser = rbs_allocator_alloc(&allocator, parserstate); *parser = (parserstate) { diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index af61c1b33..94883c9b9 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -114,17 +114,17 @@ bool parser_typevar_member(parserstate *state, rbs_constant_id_t id); * alloc_lexer(string, 0, 31) // New lexstate with buffer content * ``` * */ -lexstate *alloc_lexer(rbs_allocator_t *, VALUE string, int start_pos, int end_pos); +lexstate *alloc_lexer(rbs_allocator_t *, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); /** * Allocate new parserstate object. * * ``` - * alloc_parser(buffer, 0, 1, variables) // New parserstate with variables - * alloc_parser(buffer, 3, 5, Qnil) // New parserstate without variables + * alloc_parser(buffer, string, encoding, 0, 1, variables) // New parserstate with variables + * alloc_parser(buffer, string, encoding, 3, 5, Qnil) // New parserstate without variables * ``` * */ -parserstate *alloc_parser(VALUE buffer, int start_pos, int end_pos, VALUE variables); +parserstate *alloc_parser(VALUE buffer, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos, VALUE variables); void free_parser(parserstate *parser); /** * Advance one token. From 87cbbf53b8247128c3cb459a33dfed0605383c27 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 3 Dec 2024 11:14:07 -0500 Subject: [PATCH 033/111] Does not pass `parserstate` around for node translation Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/ast_translation.c | 14 +++++++++ ext/rbs_extension/ast_translation.h | 2 ++ ext/rbs_extension/main.c | 31 ++++++++++--------- ext/rbs_extension/parserstate.c | 1 - ext/rbs_extension/parserstate.h | 3 +- .../ext/rbs_extension/ast_translation.c.erb | 14 +++++++++ .../ext/rbs_extension/ast_translation.h.erb | 2 ++ 7 files changed, 49 insertions(+), 18 deletions(-) diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index ab980a172..3d47a1d73 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -13,6 +13,20 @@ #define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1)) +static rb_encoding *rbs_encoding_to_ruby_new(const rbs_encoding_t *encoding) { + rb_encoding *ruby_encoding = rb_enc_find(encoding->name); + assert(ruby_encoding != NULL); + return ruby_encoding; +} + +rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *constant_pool, VALUE buffer, const rbs_encoding_t *encoding) { + return (rbs_translation_context_t) { + .constant_pool = constant_pool, + .buffer = buffer, + .encoding = rbs_encoding_to_ruby_new(encoding), + }; +} + VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t *list) { VALUE ruby_array = rb_ary_new(); diff --git a/ext/rbs_extension/ast_translation.h b/ext/rbs_extension/ast_translation.h index 3e07bed5d..2fc0b001b 100644 --- a/ext/rbs_extension/ast_translation.h +++ b/ext/rbs_extension/ast_translation.h @@ -22,6 +22,8 @@ typedef struct rbs_translation_context { rb_encoding *encoding; } rbs_translation_context_t; +rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *, VALUE buffer_string, const rbs_encoding_t *); + VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *list); VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t, rbs_hash_t *hash); VALUE rbs_struct_to_ruby_value(rbs_translation_context_t, rbs_node_t *instance); diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 09cde7234..ace729db9 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -56,11 +56,12 @@ static VALUE parse_type_try(VALUE a) { parser_advance_assert(parser, pEOF); } - rbs_translation_context_t ctx = { - .constant_pool = &parser->constant_pool, - .buffer = parser->buffer, - .encoding = parser->encoding, - }; + rbs_translation_context_t ctx = rbs_translation_context_create( + &parser->constant_pool, + parser->buffer, + parser->lexstate->encoding + ); + return rbs_struct_to_ruby_value(ctx, type); } @@ -135,11 +136,11 @@ static VALUE parse_method_type_try(VALUE a) { parser_advance_assert(parser, pEOF); } - rbs_translation_context_t ctx = { - .constant_pool = &parser->constant_pool, - .buffer = parser->buffer, - .encoding = parser->encoding, - }; + rbs_translation_context_t ctx = rbs_translation_context_create( + &parser->constant_pool, + parser->buffer, + parser->lexstate->encoding + ); return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type); } @@ -163,11 +164,11 @@ static VALUE parse_signature_try(VALUE a) { raise_error(parser, parser->error); } - rbs_translation_context_t ctx = { - .constant_pool = &parser->constant_pool, - .buffer = parser->buffer, - .encoding = parser->encoding, - }; + rbs_translation_context_t ctx = rbs_translation_context_create( + &parser->constant_pool, + parser->buffer, + parser->lexstate->encoding + ); return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature); } diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index 7f6090d3c..26c7fb888 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -324,7 +324,6 @@ parserstate *alloc_parser(VALUE buffer, rbs_string_t string, const rbs_encoding_ .next_token2 = NullToken, .next_token3 = NullToken, .buffer = buffer, - .encoding = rb_enc_get(buffer), .vars = NULL, .last_comment = NULL, diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index 94883c9b9..50a421b98 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -5,7 +5,7 @@ #include "rbs/util/rbs_allocator.h" #include "rbs/util/rbs_constant_pool.h" -#include "ruby/encoding.h" // TODO: remove this +#include "ruby.h" // TODO: remove this #include "rbs/lexer.h" #include "rbs/ast.h" @@ -62,7 +62,6 @@ typedef struct { token next_token2; /* The second lookahead token */ token next_token3; /* The third lookahead token */ VALUE buffer; - rb_encoding *encoding; // TODO: Remove this id_table *vars; /* Known type variables */ comment *last_comment; /* Last read comment */ diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index 16995b696..415327237 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -6,6 +6,20 @@ #define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1)) +static rb_encoding *rbs_encoding_to_ruby_new(const rbs_encoding_t *encoding) { + rb_encoding *ruby_encoding = rb_enc_find(encoding->name); + assert(ruby_encoding != NULL); + return ruby_encoding; +} + +rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *constant_pool, VALUE buffer, const rbs_encoding_t *encoding) { + return (rbs_translation_context_t) { + .constant_pool = constant_pool, + .buffer = buffer, + .encoding = rbs_encoding_to_ruby_new(encoding), + }; +} + VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t ctx, rbs_node_list_t *list) { VALUE ruby_array = rb_ary_new(); diff --git a/templates/ext/rbs_extension/ast_translation.h.erb b/templates/ext/rbs_extension/ast_translation.h.erb index d83a1e42b..71338d670 100644 --- a/templates/ext/rbs_extension/ast_translation.h.erb +++ b/templates/ext/rbs_extension/ast_translation.h.erb @@ -15,6 +15,8 @@ typedef struct rbs_translation_context { rb_encoding *encoding; } rbs_translation_context_t; +rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *, VALUE buffer_string, const rbs_encoding_t *); + VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *list); VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t, rbs_hash_t *hash); VALUE rbs_struct_to_ruby_value(rbs_translation_context_t, rbs_node_t *instance); From 1ec4c47dc805995736d5679cb7b0c7379a2f2009 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 3 Dec 2024 11:20:32 -0500 Subject: [PATCH 034/111] Do not store `rb_encoding` in `parserstate` Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/ast_translation.c | 10 +-- ext/rbs_extension/ast_translation.h | 2 +- ext/rbs_extension/main.c | 61 +++++++++++++------ .../ext/rbs_extension/ast_translation.c.erb | 10 +-- .../ext/rbs_extension/ast_translation.h.erb | 2 +- 5 files changed, 49 insertions(+), 36 deletions(-) diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index 3d47a1d73..143a85356 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -13,17 +13,11 @@ #define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1)) -static rb_encoding *rbs_encoding_to_ruby_new(const rbs_encoding_t *encoding) { - rb_encoding *ruby_encoding = rb_enc_find(encoding->name); - assert(ruby_encoding != NULL); - return ruby_encoding; -} - -rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *constant_pool, VALUE buffer, const rbs_encoding_t *encoding) { +rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *constant_pool, VALUE buffer, rb_encoding *ruby_encoding) { return (rbs_translation_context_t) { .constant_pool = constant_pool, .buffer = buffer, - .encoding = rbs_encoding_to_ruby_new(encoding), + .encoding = ruby_encoding, }; } diff --git a/ext/rbs_extension/ast_translation.h b/ext/rbs_extension/ast_translation.h index 2fc0b001b..c016b8994 100644 --- a/ext/rbs_extension/ast_translation.h +++ b/ext/rbs_extension/ast_translation.h @@ -22,7 +22,7 @@ typedef struct rbs_translation_context { rb_encoding *encoding; } rbs_translation_context_t; -rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *, VALUE buffer_string, const rbs_encoding_t *); +rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *, VALUE buffer_string, rb_encoding *ruby_encoding); VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *list); VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t, rbs_hash_t *hash); diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index ace729db9..aa517bf6d 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -28,6 +28,8 @@ NORETURN(void) raise_error(parserstate *state, error *error) { } struct parse_type_arg { + VALUE buffer; + rb_encoding *encoding; parserstate *parser; VALUE require_eof; }; @@ -58,23 +60,19 @@ static VALUE parse_type_try(VALUE a) { rbs_translation_context_t ctx = rbs_translation_context_create( &parser->constant_pool, - parser->buffer, - parser->lexstate->encoding + arg->buffer, + arg->encoding ); return rbs_struct_to_ruby_value(ctx, type); } -static lexstate *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE buffer, int start_pos, int end_pos) { +static lexstate *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE string, rb_encoding *encoding, int start_pos, int end_pos) { if (start_pos < 0 || end_pos < 0) { rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos); } - VALUE string = rb_funcall(buffer, rb_intern("content"), 0); - StringValue(string); - - rb_encoding *encoding = rb_enc_get(string); const char *encoding_name = rb_enc_name(encoding); return alloc_lexer( @@ -109,10 +107,16 @@ static parserstate *alloc_parser_from_buffer(VALUE buffer, int start_pos, int en } static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) { + VALUE string = rb_funcall(buffer, rb_intern("content"), 0); + StringValue(string); + rb_encoding *encoding = rb_enc_get(string); + parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables); struct parse_type_arg arg = { - parser, - require_eof + .buffer = buffer, + .encoding = encoding, + .parser = parser, + .require_eof = require_eof }; return rb_ensure(parse_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser); } @@ -138,24 +142,31 @@ static VALUE parse_method_type_try(VALUE a) { rbs_translation_context_t ctx = rbs_translation_context_create( &parser->constant_pool, - parser->buffer, - parser->lexstate->encoding + arg->buffer, + arg->encoding ); return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type); } static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) { + VALUE string = rb_funcall(buffer, rb_intern("content"), 0); + StringValue(string); + rb_encoding *encoding = rb_enc_get(string); + parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables); struct parse_type_arg arg = { - parser, - require_eof + .buffer = buffer, + .encoding = encoding, + .parser = parser, + .require_eof = require_eof }; return rb_ensure(parse_method_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser); } static VALUE parse_signature_try(VALUE a) { - parserstate *parser = (parserstate *)a; + struct parse_type_arg *arg = (struct parse_type_arg *)a; + parserstate *parser = arg->parser; rbs_signature_t *signature = NULL; parse_signature(parser, &signature); @@ -166,22 +177,36 @@ static VALUE parse_signature_try(VALUE a) { rbs_translation_context_t ctx = rbs_translation_context_create( &parser->constant_pool, - parser->buffer, - parser->lexstate->encoding + arg->buffer, + arg->encoding ); return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature); } static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) { + VALUE string = rb_funcall(buffer, rb_intern("content"), 0); + StringValue(string); + rb_encoding *encoding = rb_enc_get(string); + parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos), Qnil); - return rb_ensure(parse_signature_try, (VALUE)parser, ensure_free_parser, (VALUE)parser); + struct parse_type_arg arg = { + .buffer = buffer, + .encoding = encoding, + .parser = parser, + .require_eof = false + }; + return rb_ensure(parse_signature_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser); } static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) { + VALUE string = rb_funcall(buffer, rb_intern("content"), 0); + StringValue(string); + rb_encoding *encoding = rb_enc_get(string); + rbs_allocator_t allocator; rbs_allocator_init(&allocator); - lexstate *lexer = alloc_lexer_from_buffer(&allocator, buffer, 0, FIX2INT(end_pos)); + lexstate *lexer = alloc_lexer_from_buffer(&allocator, string, encoding, 0, FIX2INT(end_pos)); VALUE results = rb_ary_new(); token token = NullToken; diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index 415327237..c7334915c 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -6,17 +6,11 @@ #define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1)) -static rb_encoding *rbs_encoding_to_ruby_new(const rbs_encoding_t *encoding) { - rb_encoding *ruby_encoding = rb_enc_find(encoding->name); - assert(ruby_encoding != NULL); - return ruby_encoding; -} - -rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *constant_pool, VALUE buffer, const rbs_encoding_t *encoding) { +rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *constant_pool, VALUE buffer, rb_encoding *ruby_encoding) { return (rbs_translation_context_t) { .constant_pool = constant_pool, .buffer = buffer, - .encoding = rbs_encoding_to_ruby_new(encoding), + .encoding = ruby_encoding, }; } diff --git a/templates/ext/rbs_extension/ast_translation.h.erb b/templates/ext/rbs_extension/ast_translation.h.erb index 71338d670..42812ab58 100644 --- a/templates/ext/rbs_extension/ast_translation.h.erb +++ b/templates/ext/rbs_extension/ast_translation.h.erb @@ -15,7 +15,7 @@ typedef struct rbs_translation_context { rb_encoding *encoding; } rbs_translation_context_t; -rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *, VALUE buffer_string, const rbs_encoding_t *); +rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *, VALUE buffer_string, rb_encoding *ruby_encoding); VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *list); VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t, rbs_hash_t *hash); From f266000c964f0f074ee770251bdcd576e7b61613 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 3 Dec 2024 12:02:04 -0500 Subject: [PATCH 035/111] Delete `parser_advance_assert` Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/main.c | 12 +++- ext/rbs_extension/parser.c | 97 ++++++++++++++++++--------------- ext/rbs_extension/parserstate.c | 18 ------ ext/rbs_extension/parserstate.h | 14 ----- 4 files changed, 64 insertions(+), 77 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index aa517bf6d..bc9baac3d 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -55,7 +55,11 @@ static VALUE parse_type_try(VALUE a) { } if (RB_TEST(arg->require_eof)) { - parser_advance_assert(parser, pEOF); + parser_advance(parser); + if (parser->current_token.type != pEOF) { + set_error(parser, parser->current_token, true, "expected a token `%s`", token_type_str(pEOF)); + raise_error(parser, parser->error); + } } rbs_translation_context_t ctx = rbs_translation_context_create( @@ -137,7 +141,11 @@ static VALUE parse_method_type_try(VALUE a) { } if (RB_TEST(arg->require_eof)) { - parser_advance_assert(parser, pEOF); + parser_advance(parser); + if (parser->current_token.type != pEOF) { + set_error(parser, parser->current_token, true, "expected a token `%s`", token_type_str(pEOF)); + raise_error(parser, parser->error); + } } rbs_translation_context_t ctx = rbs_translation_context_create( diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index b2511728f..b9a03cf36 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -62,6 +62,17 @@ return false; \ } +#define ASSERT_TOKEN(state, expected_type) \ + if (state->current_token.type != expected_type) { \ + set_error(state, state->current_token, true, "expected a token `%s`", token_type_str(expected_type)); \ + return false; \ + } + +#define ADVANCE_ASSERT(state, expected_type) do {\ + parser_advance(state); \ + ASSERT_TOKEN(state, expected_type) \ + } while(0); + typedef struct { rbs_node_list_t *required_positionals; rbs_node_list_t *optional_positionals; @@ -357,7 +368,7 @@ static bool parse_keyword(parserstate *state, rbs_hash_t *keywords, rbs_hash_t * rbs_hash_set(memo, (rbs_node_t *) key, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, true)); } - parser_advance_assert(state, pCOLON); + ADVANCE_ASSERT(state, pCOLON); rbs_types_function_param_t *param = NULL; CHECK_PARSE(parse_function_param(state, ¶m)); @@ -623,11 +634,11 @@ NODISCARD static bool parse_self_type_binding(parserstate *state, rbs_node_t **self_type) { if (state->next_token.type == pLBRACKET) { parser_advance(state); - parser_advance_assert(state, kSELF); - parser_advance_assert(state, pCOLON); + ADVANCE_ASSERT(state, kSELF); + ADVANCE_ASSERT(state, pCOLON); rbs_node_t *type; CHECK_PARSE(parse_type(state, &type)); - parser_advance_assert(state, pRBRACKET); + ADVANCE_ASSERT(state, pRBRACKET); *self_type = type; } @@ -658,7 +669,7 @@ static bool parse_function(parserstate *state, bool accept_type_binding, parse_f if (state->next_token.type == pLPAREN) { parser_advance(state); CHECK_PARSE(parse_params(state, ¶ms)); - parser_advance_assert(state, pRPAREN); + ADVANCE_ASSERT(state, pRPAREN); } // Untyped method parameter means it cannot have block @@ -689,13 +700,13 @@ static bool parse_function(parserstate *state, bool accept_type_binding, parse_f if (state->next_token.type == pLPAREN) { parser_advance(state); CHECK_PARSE(parse_params(state, &block_params)); - parser_advance_assert(state, pRPAREN); + ADVANCE_ASSERT(state, pRPAREN); } rbs_node_t *self_type = NULL; CHECK_PARSE(parse_self_type_binding(state, &self_type)); - parser_advance_assert(state, pARROW); + ADVANCE_ASSERT(state, pARROW); rbs_node_t *block_return_type = NULL; CHECK_PARSE(parse_optional(state, &block_return_type)); @@ -718,10 +729,10 @@ static bool parse_function(parserstate *state, bool accept_type_binding, parse_f block = rbs_types_block_new(&state->allocator, block_function, required, self_type); - parser_advance_assert(state, pRBRACE); + ADVANCE_ASSERT(state, pRBRACE); } - parser_advance_assert(state, pARROW); + ADVANCE_ASSERT(state, pARROW); rbs_node_t *type = NULL; CHECK_PARSE(parse_optional(state, &type)); @@ -806,7 +817,7 @@ static bool parse_record_attributes(parserstate *state, rbs_hash_t **fields) { CHECK_PARSE(parse_keyword_key(state, &key)); check_key_duplication(state, *fields, (rbs_node_t *) key); - parser_advance_assert(state, pCOLON); + ADVANCE_ASSERT(state, pCOLON); } else { // { key => type } syntax switch (state->next_token.type) { @@ -829,7 +840,7 @@ static bool parse_record_attributes(parserstate *state, rbs_hash_t **fields) { return false; } check_key_duplication(state, *fields, (rbs_node_t *) key); - parser_advance_assert(state, pFATARROW); + ADVANCE_ASSERT(state, pFATARROW); } rbs_node_t *type; @@ -924,7 +935,7 @@ static bool parse_instance_type(parserstate *state, bool parse_alias, rbs_node_t parser_advance(state); args_range.start = state->current_token.range.start; CHECK_PARSE(parse_type_list(state, pRBRACKET, types)); - parser_advance_assert(state, pRBRACKET); + ADVANCE_ASSERT(state, pRBRACKET); args_range.end = state->current_token.range.end; } else { args_range = NULL_RANGE; @@ -956,18 +967,18 @@ static bool parse_instance_type(parserstate *state, bool parse_alias, rbs_node_t */ NODISCARD static bool parse_singleton_type(parserstate *state, rbs_types_classsingleton_t **singleton) { - parser_assert(state, kSINGLETON); + ASSERT_TOKEN(state, kSINGLETON); range type_range; type_range.start = state->current_token.range.start; - parser_advance_assert(state, pLPAREN); + ADVANCE_ASSERT(state, pLPAREN); parser_advance(state); range name_range; rbs_typename_t *typename = NULL; CHECK_PARSE(parse_type_name(state, CLASS_NAME, &name_range, &typename)); - parser_advance_assert(state, pRPAREN); + ADVANCE_ASSERT(state, pRPAREN); type_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(type_range); @@ -996,7 +1007,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { case pLPAREN: { rbs_node_t *lparen_type; CHECK_PARSE(parse_type(state, &lparen_type)); - parser_advance_assert(state, pRPAREN); + ADVANCE_ASSERT(state, pRPAREN); *type = lparen_type; return true; } @@ -1124,7 +1135,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { if (state->next_token.type != pRBRACKET) { CHECK_PARSE(parse_type_list(state, pRBRACKET, types)); } - parser_advance_assert(state, pRBRACKET); + ADVANCE_ASSERT(state, pRBRACKET); rg.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(rg); @@ -1141,7 +1152,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { position start = state->current_token.range.start; rbs_hash_t *fields = NULL; CHECK_PARSE(parse_record_attributes(state, &fields)); - parser_advance_assert(state, pRBRACE); + ADVANCE_ASSERT(state, pRBRACE); position end = state->current_token.range.end; rbs_location_t *loc = rbs_location_pp(&start, &end); *type = (rbs_node_t *) rbs_types_record_new(&state->allocator, fields, loc); @@ -1277,7 +1288,7 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa } } - parser_advance_assert(state, tUIDENT); + ADVANCE_ASSERT(state, tUIDENT); range name_range = state->current_token.range; rbs_string_t string = rbs_parser_get_current_token(state); @@ -1335,7 +1346,7 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa } } - parser_advance_assert(state, pRBRACKET); + ADVANCE_ASSERT(state, pRBRACKET); rg->end = state->current_token.range.end; } else { *rg = NULL_RANGE; @@ -1391,7 +1402,7 @@ static bool parse_global_decl(parserstate *state, rbs_ast_declarations_global_t range name_range = state->current_token.range; rbs_ast_symbol_t *typename = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); - parser_advance_assert(state, pCOLON); + ADVANCE_ASSERT(state, pCOLON); range colon_range = state->current_token.range; rbs_node_t *type; @@ -1421,7 +1432,7 @@ static bool parse_const_decl(parserstate *state, rbs_ast_declarations_constant_t rbs_typename_t *typename = NULL; CHECK_PARSE(parse_type_name(state, CLASS_NAME, &name_range, &typename)); - parser_advance_assert(state, pCOLON); + ADVANCE_ASSERT(state, pCOLON); range colon_range = state->current_token.range; rbs_node_t *type; @@ -1461,7 +1472,7 @@ static bool parse_type_decl(parserstate *state, position comment_pos, rbs_node_l rbs_node_list_t *type_params; CHECK_PARSE(parse_type_params(state, ¶ms_range, true, &type_params)); - parser_advance_assert(state, pEQ); + ADVANCE_ASSERT(state, pEQ); range eq_range = state->current_token.range; rbs_node_t *type; @@ -1744,7 +1755,7 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept set_error(state, state->next_token, true, "`self?` method cannot have visibility"); return false; } else { - parser_advance_assert(state, pCOLON); + ADVANCE_ASSERT(state, pCOLON); } parser_push_typevar_table(state, kind != INSTANCE_KIND); @@ -1853,7 +1864,7 @@ static bool class_instance_name(parserstate *state, TypeNameKind kind, rbs_node_ parser_advance(state); args_range->start = state->current_token.range.start; CHECK_PARSE(parse_type_list(state, pRBRACKET, args)); - parser_advance_assert(state, pRBRACKET); + ADVANCE_ASSERT(state, pRBRACKET); args_range->end = state->current_token.range.end; } else { *args_range = NULL_RANGE; @@ -1968,14 +1979,14 @@ static bool parse_alias_member(parserstate *state, bool instance_only, position new_kind_range.start = state->next_token.range.start; new_kind_range.end = state->next_token2.range.end; - parser_advance_assert(state, kSELF); - parser_advance_assert(state, pDOT); + ADVANCE_ASSERT(state, kSELF); + ADVANCE_ASSERT(state, pDOT); CHECK_PARSE(parse_method_name(state, &new_name_range, &new_name)); old_kind_range.start = state->next_token.range.start; old_kind_range.end = state->next_token2.range.end; - parser_advance_assert(state, kSELF); - parser_advance_assert(state, pDOT); + ADVANCE_ASSERT(state, kSELF); + ADVANCE_ASSERT(state, pDOT); CHECK_PARSE(parse_method_name(state, &old_name_range, &old_name)); } else { kind = rbs_keyword_new(&state->allocator, INTERN("instance")); @@ -2021,7 +2032,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ range name_range = state->current_token.range; rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); - parser_advance_assert(state, pCOLON); + ADVANCE_ASSERT(state, pCOLON); range colon_range = state->current_token.range; rbs_node_t *type; @@ -2041,7 +2052,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ range name_range = state->current_token.range; rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); - parser_advance_assert(state, pCOLON); + ADVANCE_ASSERT(state, pCOLON); range colon_range = state->current_token.range; parser_push_typevar_table(state, true); @@ -2067,13 +2078,13 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ .end = state->next_token.range.end }; - parser_advance_assert(state, pDOT); - parser_advance_assert(state, tAIDENT); + ADVANCE_ASSERT(state, pDOT); + ADVANCE_ASSERT(state, tAIDENT); range name_range = state->current_token.range; rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); - parser_advance_assert(state, pCOLON); + ADVANCE_ASSERT(state, pCOLON); range colon_range = state->current_token.range; parser_push_typevar_table(state, true); @@ -2187,7 +2198,7 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs rbs_node_t *ivar_name; // rbs_ast_symbol_t, NULL or rbs_ast_bool_new(&state->allocator, false) range ivar_range, ivar_name_range; if (state->next_token.type == pLPAREN) { - parser_advance_assert(state, pLPAREN); + ADVANCE_ASSERT(state, pLPAREN); ivar_range.start = state->current_token.range.start; if (parser_advance_if(state, tAIDENT)) { @@ -2198,7 +2209,7 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs ivar_name_range = NULL_RANGE; } - parser_advance_assert(state, pRPAREN); + ADVANCE_ASSERT(state, pRPAREN); ivar_range.end = state->current_token.range.end; } else { ivar_range = NULL_RANGE; @@ -2206,7 +2217,7 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs ivar_name_range = NULL_RANGE; } - parser_advance_assert(state, pCOLON); + ADVANCE_ASSERT(state, pCOLON); range colon_range = state->current_token.range; parser_push_typevar_table(state, is_kind == SINGLETON_KIND); @@ -2322,7 +2333,7 @@ static bool parse_interface_decl(parserstate *state, position comment_pos, rbs_n rbs_node_list_t *members = NULL; CHECK_PARSE(parse_interface_members(state, &members)); - parser_advance_assert(state, kEND); + ADVANCE_ASSERT(state, kEND); range end_range = state->current_token.range; member_range.end = end_range.end; @@ -2518,7 +2529,7 @@ static bool parse_module_decl0(parserstate *state, range keyword_range, rbs_type rbs_node_list_t *members = NULL; CHECK_PARSE(parse_module_members(state, &members)); - parser_advance_assert(state, kEND); + ADVANCE_ASSERT(state, kEND); range end_range = state->current_token.range; decl_range.end = state->current_token.range.end; @@ -2640,7 +2651,7 @@ static bool parse_class_decl0(parserstate *state, range keyword_range, rbs_typen rbs_node_list_t *members = NULL; CHECK_PARSE(parse_module_members(state, &members)); - parser_advance_assert(state, kEND); + ADVANCE_ASSERT(state, kEND); range end_range = state->current_token.range; @@ -2895,9 +2906,9 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { parser_advance(state); keyword_range = state->current_token.range; - if (ident_type == tUIDENT) parser_advance_assert(state, tUIDENT); - if (ident_type == tLIDENT) parser_advance_assert(state, tLIDENT); - if (ident_type == tULIDENT) parser_advance_assert(state, tULIDENT); + if (ident_type == tUIDENT) ADVANCE_ASSERT(state, tUIDENT); + if (ident_type == tLIDENT) ADVANCE_ASSERT(state, tLIDENT); + if (ident_type == tULIDENT) ADVANCE_ASSERT(state, tULIDENT); new_name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); new_name_range = state->current_token.range; diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index 26c7fb888..3cec8ad7f 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -141,24 +141,6 @@ bool parser_advance_if(parserstate *state, enum TokenType type) { } } -void parser_assert(parserstate *state, enum TokenType type) { - if (state->current_token.type != type) { - set_error( - state, - state->current_token, - true, - "expected a token `%s`", - token_type_str(type) - ); - raise_error(state, state->error); - } -} - -void parser_advance_assert(parserstate *state, enum TokenType type) { - parser_advance(state); - parser_assert(state, type); -} - void print_token(token tok) { printf( "%s char=%d...%d\n", diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index 50a421b98..a73f6e634 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -130,20 +130,6 @@ void free_parser(parserstate *parser); * */ void parser_advance(parserstate *state); -/** - * @brief Raises an exception if `current_token->type != type`. - * - * @param state - * @param type - */ -void parser_assert(parserstate *state, enum TokenType type); - -/** - * Advance one token, and assert the current token type. - * Raises an exception if `current_token->type != type`. - * */ -void parser_advance_assert(parserstate *state, enum TokenType type); - /** * Advance one token if the next_token is a token of the type. * */ From e9eaed67944dd92cc7cd47287cc33fc23bc49a63 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 10 Jan 2025 14:02:25 -0500 Subject: [PATCH 036/111] Raise errors without using parser state Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/main.c | 21 ++++++++++++++------- ext/rbs_extension/rbs_extension.h | 9 --------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index bc9baac3d..a1679590c 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -7,12 +7,19 @@ #include "ruby/vm.h" -NORETURN(void) raise_error(parserstate *state, error *error) { +/** + * Raises RBS::ParsingError on `tok` with message constructed with given `fmt`. + * + * ``` + * foo.rbs:11:21...11:25: Syntax error: {message}, token=`{tok source}` ({tok type}) + * ``` + * */ +static NORETURN(void) raise_error(error *error, VALUE buffer) { if (!error->syntax_error) { rb_raise(rb_eRuntimeError, "Unexpected error"); } - VALUE location = rbs_new_location(state->buffer, error->token.range); + VALUE location = rbs_new_location(buffer, error->token.range); VALUE type = rb_str_new_cstr(token_type_str(error->token.type)); VALUE rb_error = rb_funcall( @@ -51,14 +58,14 @@ static VALUE parse_type_try(VALUE a) { parse_type(parser, &type); if (parser->error) { - raise_error(parser, parser->error); + raise_error(parser->error, arg->buffer); } if (RB_TEST(arg->require_eof)) { parser_advance(parser); if (parser->current_token.type != pEOF) { set_error(parser, parser->current_token, true, "expected a token `%s`", token_type_str(pEOF)); - raise_error(parser, parser->error); + raise_error(parser->error, arg->buffer); } } @@ -137,14 +144,14 @@ static VALUE parse_method_type_try(VALUE a) { parse_method_type(parser, &method_type); if (parser->error) { - raise_error(parser, parser->error); + raise_error(parser->error, arg->buffer); } if (RB_TEST(arg->require_eof)) { parser_advance(parser); if (parser->current_token.type != pEOF) { set_error(parser, parser->current_token, true, "expected a token `%s`", token_type_str(pEOF)); - raise_error(parser, parser->error); + raise_error(parser->error, arg->buffer); } } @@ -180,7 +187,7 @@ static VALUE parse_signature_try(VALUE a) { parse_signature(parser, &signature); if (parser->error) { - raise_error(parser, parser->error); + raise_error(parser->error, arg->buffer); } rbs_translation_context_t ctx = rbs_translation_context_create( diff --git a/ext/rbs_extension/rbs_extension.h b/ext/rbs_extension/rbs_extension.h index 67704b412..f9ebf25bf 100644 --- a/ext/rbs_extension/rbs_extension.h +++ b/ext/rbs_extension/rbs_extension.h @@ -13,12 +13,3 @@ * RBS::Parser class * */ extern VALUE RBS_Parser; - -/** - * Raises RBS::ParsingError on `tok` with message constructed with given `fmt`. - * - * ``` - * foo.rbs:11:21...11:25: Syntax error: {message}, token=`{tok source}` ({tok type}) - * ``` - * */ -NORETURN(void) raise_error(parserstate *state, error *error); From 11dd363e5b5d50bae7857e78daed89d8237d2b9d Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 3 Dec 2024 14:48:25 -0500 Subject: [PATCH 037/111] Do not use parserstate's buffer to parse comments Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/parserstate.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index 3cec8ad7f..85d22cfa6 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -1,6 +1,6 @@ #include "rbs_extension.h" -#include "rbs_string_bridging.h" +#include "rbs/encoding.h" #include "rbs/rbs_buffer.h" #define RESET_TABLE_P(table) (table->size == 0) @@ -162,12 +162,9 @@ void insert_comment_line(parserstate *state, token tok) { } } -static rbs_ast_comment_t *parse_comment_lines(rbs_allocator_t *allocator, comment *com, VALUE buffer) { - VALUE content = rb_funcall(buffer, rb_intern("content"), 0); - rb_encoding *enc = rb_enc_get(content); - - int hash_bytes = rb_enc_codelen('#', enc); - int space_bytes = rb_enc_codelen(' ', enc); +static rbs_ast_comment_t *parse_comment_lines(parserstate *state, comment *com) { + size_t hash_bytes = state->lexstate->encoding->char_width((const uint8_t *) "#", (size_t) 1); + size_t space_bytes = state->lexstate->encoding->char_width((const uint8_t *) " ", (size_t) 1); rbs_buffer_t rbs_buffer; rbs_buffer_init(&rbs_buffer); @@ -175,9 +172,15 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_allocator_t *allocator, commen for (size_t i = 0; i < com->line_count; i++) { token tok = com->tokens[i]; - char *comment_start = RSTRING_PTR(content) + tok.range.start.byte_pos + hash_bytes; - int comment_bytes = RANGE_BYTES(tok.range) - hash_bytes; - unsigned char c = rb_enc_mbc_to_codepoint(comment_start, RSTRING_END(content), enc); + const char *comment_start = state->lexstate->string.start + tok.range.start.byte_pos + hash_bytes; + size_t comment_bytes = RANGE_BYTES(tok.range) - hash_bytes; + + rbs_string_t str = { + .start = comment_start, + .end = state->lexstate->string.end, + .type = RBS_STRING_SHARED, + }; + unsigned char c = utf8_to_codepoint(str); if (c == ' ') { comment_start += space_bytes; @@ -189,7 +192,7 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_allocator_t *allocator, commen } return rbs_ast_comment_new( - allocator, + &state->allocator, rbs_buffer_to_string(&rbs_buffer), rbs_location_pp(&com->start, &com->end) ); @@ -201,7 +204,7 @@ rbs_ast_comment_t *get_comment(parserstate *state, int subject_line) { comment *com = comment_get_comment(state->last_comment, comment_line); if (com) { - return parse_comment_lines(&state->allocator, com, state->buffer); + return parse_comment_lines(state, com); } else { return NULL; } From c7faf00b9e3f67d1d8539180534c073f271391ff Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 3 Dec 2024 14:53:27 -0500 Subject: [PATCH 038/111] Remove buffer from parserstate Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/main.c | 1 - ext/rbs_extension/parserstate.c | 3 +-- ext/rbs_extension/parserstate.h | 3 +-- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index a1679590c..0b0e3609d 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -107,7 +107,6 @@ static parserstate *alloc_parser_from_buffer(VALUE buffer, int start_pos, int en const char *encoding_name = rb_enc_name(encoding); return alloc_parser( - buffer, rbs_string_from_ruby_string(string), rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))), diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index 85d22cfa6..496363c8f 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -294,7 +294,7 @@ lexstate *alloc_lexer(rbs_allocator_t *allocator, rbs_string_t string, const rbs return lexer; } -parserstate *alloc_parser(VALUE buffer, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos, VALUE variables) { +parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos, VALUE variables) { rbs_allocator_t allocator; rbs_allocator_init(&allocator); @@ -308,7 +308,6 @@ parserstate *alloc_parser(VALUE buffer, rbs_string_t string, const rbs_encoding_ .next_token = NullToken, .next_token2 = NullToken, .next_token3 = NullToken, - .buffer = buffer, .vars = NULL, .last_comment = NULL, diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index a73f6e634..e11403b88 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -61,7 +61,6 @@ typedef struct { token next_token; /* The first lookahead token */ token next_token2; /* The second lookahead token */ token next_token3; /* The third lookahead token */ - VALUE buffer; id_table *vars; /* Known type variables */ comment *last_comment; /* Last read comment */ @@ -123,7 +122,7 @@ lexstate *alloc_lexer(rbs_allocator_t *, rbs_string_t string, const rbs_encoding * alloc_parser(buffer, string, encoding, 3, 5, Qnil) // New parserstate without variables * ``` * */ -parserstate *alloc_parser(VALUE buffer, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos, VALUE variables); +parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos, VALUE variables); void free_parser(parserstate *parser); /** * Advance one token. From 1ff65c6fc5137a8b2842342c5f3ed6c4afa0d933 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 3 Dec 2024 15:00:05 -0500 Subject: [PATCH 039/111] Rework some includes Signed-off-by: Alexandre Terrasa --- include/rbs/rbs_unescape.h | 3 ++- src/ast.c | 1 + templates/src/ast.c.erb | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/rbs/rbs_unescape.h b/include/rbs/rbs_unescape.h index cd803f83d..3a4881f7a 100644 --- a/include/rbs/rbs_unescape.h +++ b/include/rbs/rbs_unescape.h @@ -2,7 +2,8 @@ #define RBS_RBS_UNESCAPE_H #include -#include "parserstate.h" + +#include "rbs/rbs_string.h" /** * Receives `parserstate` and `range`, which represents a string token or symbol token, and returns a string VALUE. diff --git a/src/ast.c b/src/ast.c index 41f4b7c5d..da7dcd66f 100644 --- a/src/ast.c +++ b/src/ast.c @@ -8,6 +8,7 @@ #include "rbs/ast.h" #include +#include /* rbs_node_list */ diff --git a/templates/src/ast.c.erb b/templates/src/ast.c.erb index a965e2e9b..bb314f70c 100644 --- a/templates/src/ast.c.erb +++ b/templates/src/ast.c.erb @@ -1,6 +1,7 @@ #include "rbs/ast.h" #include +#include /* rbs_node_list */ From 3b7aaa4756a50647a21fd60f3a58739d901784be Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Mon, 2 Dec 2024 12:12:08 -0500 Subject: [PATCH 040/111] Un-nest and use early return --- ext/rbs_extension/parserstate.c | 34 ++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index 496363c8f..1bc288080 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -341,27 +341,27 @@ parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, i parser_advance(parser); parser_advance(parser); - if (!NIL_P(variables)) { - if (!RB_TYPE_P(variables, T_ARRAY)) { - rb_raise(rb_eTypeError, - "wrong argument type %"PRIsVALUE" (must be array or nil)", - rb_obj_class(variables)); - } + if (NIL_P(variables)) return parser; + + if (!RB_TYPE_P(variables, T_ARRAY)) { + rb_raise(rb_eTypeError, + "wrong argument type %"PRIsVALUE" (must be array or nil)", + rb_obj_class(variables)); + } - parser_push_typevar_table(parser, true); + parser_push_typevar_table(parser, true); - for (long i = 0; i < rb_array_len(variables); i++) { - VALUE symbol = rb_ary_entry(variables, i); - VALUE name = rb_sym2str(symbol); + for (long i = 0; i < rb_array_len(variables); i++) { + VALUE symbol = rb_ary_entry(variables, i); + VALUE name = rb_sym2str(symbol); - rbs_constant_id_t id = rbs_constant_pool_insert_shared( - &parser->constant_pool, - (const uint8_t *) RSTRING_PTR(name), - RSTRING_LEN(name) - ); + rbs_constant_id_t id = rbs_constant_pool_insert_shared( + &parser->constant_pool, + (const uint8_t *) RSTRING_PTR(name), + RSTRING_LEN(name) + ); - parser_insert_typevar(parser, id); - } + parser_insert_typevar(parser, id); } return parser; From 9ff49b711834340979980ee955bc86d492c8778f Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Mon, 2 Dec 2024 12:30:56 -0500 Subject: [PATCH 041/111] Add typecheck to prevent segfault Raises TypeError to match the behaviour of the `SYM2ID()` call that used to exist here. --- ext/rbs_extension/parserstate.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index 1bc288080..cb1583eed 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -353,12 +353,19 @@ parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, i for (long i = 0; i < rb_array_len(variables); i++) { VALUE symbol = rb_ary_entry(variables, i); - VALUE name = rb_sym2str(symbol); + + if (!RB_TYPE_P(symbol, T_SYMBOL)) { + rb_raise(rb_eTypeError, + "Type variables Array contains invalid value %"PRIsVALUE" of type %"PRIsVALUE" (must be an Array of Symbols or nil)", + rb_inspect(symbol), rb_obj_class(symbol)); + } + + VALUE name_str = rb_sym2str(symbol); rbs_constant_id_t id = rbs_constant_pool_insert_shared( &parser->constant_pool, - (const uint8_t *) RSTRING_PTR(name), - RSTRING_LEN(name) + (const uint8_t *) RSTRING_PTR(name_str), + RSTRING_LEN(name_str) ); parser_insert_typevar(parser, id); From 902773888c93da94cb12c90f95802277c4380f08 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 3 Dec 2024 10:45:57 -0500 Subject: [PATCH 042/111] Extract function to populate type var table --- ext/rbs_extension/main.c | 13 +++++++------ ext/rbs_extension/parserstate.c | 29 +++++++++++++++++++++++------ ext/rbs_extension/parserstate.h | 18 +++++++++++++++--- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 0b0e3609d..61b12da4f 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -95,7 +95,7 @@ static lexstate *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE strin ); } -static parserstate *alloc_parser_from_buffer(VALUE buffer, int start_pos, int end_pos, VALUE variables) { +static parserstate *alloc_parser_from_buffer(VALUE buffer, int start_pos, int end_pos) { if (start_pos < 0 || end_pos < 0) { rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos); } @@ -111,8 +111,7 @@ static parserstate *alloc_parser_from_buffer(VALUE buffer, int start_pos, int en rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))), start_pos, - end_pos, - variables + end_pos ); } @@ -121,7 +120,8 @@ static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VAL StringValue(string); rb_encoding *encoding = rb_enc_get(string); - parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables); + parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); + rbs_parser_declare_type_variables_from_ruby_array(parser, variables); struct parse_type_arg arg = { .buffer = buffer, .encoding = encoding, @@ -168,7 +168,8 @@ static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_p StringValue(string); rb_encoding *encoding = rb_enc_get(string); - parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables); + parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); + rbs_parser_declare_type_variables_from_ruby_array(parser, variables); struct parse_type_arg arg = { .buffer = buffer, .encoding = encoding, @@ -203,7 +204,7 @@ static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos StringValue(string); rb_encoding *encoding = rb_enc_get(string); - parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos), Qnil); + parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); struct parse_type_arg arg = { .buffer = buffer, .encoding = encoding, diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index cb1583eed..26b82f54f 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -294,7 +294,7 @@ lexstate *alloc_lexer(rbs_allocator_t *allocator, rbs_string_t string, const rbs return lexer; } -parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos, VALUE variables) { +parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { rbs_allocator_t allocator; rbs_allocator_init(&allocator); @@ -341,12 +341,31 @@ parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, i parser_advance(parser); parser_advance(parser); - if (NIL_P(variables)) return parser; + return parser; +} + +void rbs_parser_declare_type_variables(parserstate *parser, size_t count, const char *variables[count]) { + if (variables == NULL) return; // Nothing to do. + + parser_push_typevar_table(parser, true); + + for (size_t i = 0; i < count; i++) { + rbs_constant_id_t name = rbs_constant_pool_insert_shared( + &parser->constant_pool, + (const uint8_t *) variables[i], + strlen(variables[i]) + ); + parser_insert_typevar(parser, name); + } +} + +void rbs_parser_declare_type_variables_from_ruby_array(parserstate *parser, VALUE variables) { + if (NIL_P(variables)) return; // Nothing to do. if (!RB_TYPE_P(variables, T_ARRAY)) { rb_raise(rb_eTypeError, - "wrong argument type %"PRIsVALUE" (must be array or nil)", - rb_obj_class(variables)); + "wrong argument type %"PRIsVALUE" (must be an Array of Symbols or nil)", + rb_obj_class(variables)); } parser_push_typevar_table(parser, true); @@ -370,8 +389,6 @@ parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, i parser_insert_typevar(parser, id); } - - return parser; } void free_parser(parserstate *parser) { diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index e11403b88..8008f52a9 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -116,14 +116,26 @@ lexstate *alloc_lexer(rbs_allocator_t *, rbs_string_t string, const rbs_encoding /** * Allocate new parserstate object. + * Optionally call `rbs_parser_declare_type_variables_from_ruby_array()` after, to populate the type variable table. + * + * Once allocated, optionally call `rbs_parser_declare_type_variables_from_ruby_array` if you'd like to declare + * any type variables to be used during the parsing. * * ``` - * alloc_parser(buffer, string, encoding, 0, 1, variables) // New parserstate with variables - * alloc_parser(buffer, string, encoding, 3, 5, Qnil) // New parserstate without variables + * alloc_parser(buffer, string, encoding, 0, 1); + * rbs_parser_declare_type_variables_from_ruby_array(variables); * ``` * */ -parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos, VALUE variables); +parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); void free_parser(parserstate *parser); + +/** + * Inserts the given array of type variables names into the parser's type variable table. + * @param parser + * @param variables A Ruby Array of Symbols, or nil. + */ +void rbs_parser_declare_type_variables_from_ruby_array(parserstate *parser, VALUE variables); + /** * Advance one token. * */ From ac9db92acaf6a8aa16cb3d797122fe40f47472c8 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 3 Dec 2024 18:06:29 -0500 Subject: [PATCH 043/111] Use `c_name` to rename C++ keywords Signed-off-by: Alexandre Terrasa --- config.yml | 2 ++ ext/rbs_extension/ast_translation.c | 4 ++-- include/rbs/ast.h | 8 ++++---- include/rbs/util/rbs_allocator.h | 14 ++++++++++++++ src/ast.c | 8 ++++---- .../ext/rbs_extension/ast_translation.c.erb | 18 +++++++++--------- templates/src/ast.c.erb | 4 ++-- 7 files changed, 37 insertions(+), 21 deletions(-) diff --git a/config.yml b/config.yml index 49c1d3419..d6bb4ba7f 100644 --- a/config.yml +++ b/config.yml @@ -150,6 +150,7 @@ nodes: fields: - name: namespace c_type: rbs_namespace + c_name: rbs_namespace - name: location c_type: rbs_location - name: RBS::AST::Members::Alias @@ -369,6 +370,7 @@ nodes: fields: - name: namespace c_type: rbs_namespace + c_name: rbs_namespace - name: name c_type: rbs_ast_symbol - name: RBS::Types::Alias diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index 143a85356..a423cfb79 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -341,7 +341,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rbs_ast_directives_use_wildcardclause_t *node = (rbs_ast_directives_use_wildcardclause_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->namespace)); // rbs_namespace + rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->rbs_namespace)); // rbs_namespace rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); @@ -688,7 +688,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rbs_typename_t *node = (rbs_typename_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->namespace)); // rbs_namespace + rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->rbs_namespace)); // rbs_namespace rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol diff --git a/include/rbs/ast.h b/include/rbs/ast.h index 96d5be12a..e11d88939 100644 --- a/include/rbs/ast.h +++ b/include/rbs/ast.h @@ -264,7 +264,7 @@ typedef struct rbs_ast_directives_use_singleclause { typedef struct rbs_ast_directives_use_wildcardclause { rbs_node_t base; - struct rbs_namespace *namespace; + struct rbs_namespace *rbs_namespace; struct rbs_location *location; } rbs_ast_directives_use_wildcardclause_t; @@ -456,7 +456,7 @@ typedef struct rbs_signature { typedef struct rbs_typename { rbs_node_t base; - struct rbs_namespace *namespace; + struct rbs_namespace *rbs_namespace; struct rbs_ast_symbol *name; } rbs_typename_t; @@ -681,7 +681,7 @@ rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_all rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, rbs_node_list_t *clauses, rbs_location_t *location); rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, rbs_typename_t *type_name, rbs_ast_symbol_t *new_name, rbs_location_t *location); -rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_namespace_t *namespace, rbs_location_t *location); +rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_namespace_t *rbs_namespace, rbs_location_t *location); rbs_ast_integer_t *rbs_ast_integer_new(rbs_allocator_t *allocator, rbs_string_t string_representation); rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); @@ -702,7 +702,7 @@ rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_ast_s rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location); rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_node_list_t *path, bool absolute); rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_node_list_t *directives, rbs_node_list_t *declarations); -rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_namespace_t *namespace, rbs_ast_symbol_t *name); +rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_namespace_t *rbs_namespace, rbs_ast_symbol_t *name); rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, bool todo, rbs_location_t *location); rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, rbs_location_t *location); diff --git a/include/rbs/util/rbs_allocator.h b/include/rbs/util/rbs_allocator.h index e0b45eb33..a5d4cbdc0 100644 --- a/include/rbs/util/rbs_allocator.h +++ b/include/rbs/util/rbs_allocator.h @@ -3,6 +3,20 @@ #include +#ifndef alignof + #if defined(__GNUC__) || defined(__clang__) + /* GCC or Clang */ + #define alignof(type) __alignof__(type) + #elif defined(_MSC_VER) + /* Microsoft Visual C++ */ + #define alignof(type) __alignof(type) + #else + /* Fallback using offset trick */ + #include + #define alignof(type) offsetof(struct { char c; type member; }, member) + #endif +#endif + typedef struct rbs_allocator { // The head of a linked list of pages, starting with the most recently allocated page. struct rbs_allocator_page *page; diff --git a/src/ast.c b/src/ast.c index da7dcd66f..56159590f 100644 --- a/src/ast.c +++ b/src/ast.c @@ -395,7 +395,7 @@ rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(r return instance; } -rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_namespace_t *namespace, rbs_location_t *location) { +rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_namespace_t *rbs_namespace, rbs_location_t *location) { rbs_ast_directives_use_wildcardclause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_wildcardclause_t); @@ -403,7 +403,7 @@ rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_n .base = (rbs_node_t) { .type = RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE }, - .namespace = namespace, + .rbs_namespace = rbs_namespace, .location = location, }; @@ -755,7 +755,7 @@ rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_node_list_t * return instance; } -rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_namespace_t *namespace, rbs_ast_symbol_t *name) { +rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_namespace_t *rbs_namespace, rbs_ast_symbol_t *name) { rbs_typename_t *instance = rbs_allocator_alloc(allocator, rbs_typename_t); @@ -763,7 +763,7 @@ rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_namespace_t *na .base = (rbs_node_t) { .type = RBS_TYPENAME }, - .namespace = namespace, + .rbs_namespace = rbs_namespace, .name = name, }; diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index c7334915c..eefb025d7 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -106,26 +106,26 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan <%- node.fields.each do |field| -%> <%- case field.c_type -%> <%- when "VALUE" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), node-><%= field.name %>); + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), node-><%= field.c_name %>); <%- when "rbs_node_list" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_node_list_to_ruby_array(ctx, node-><%= field.name %>)); + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_node_list_to_ruby_array(ctx, node-><%= field.c_name %>)); <%- when "rbs_hash" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_hash_to_ruby_hash(ctx, node-><%= field.name %>)); + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_hash_to_ruby_hash(ctx, node-><%= field.c_name %>)); <%- when "rbs_location" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_loc_to_ruby_location(ctx, node-><%= field.name %>)); + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_loc_to_ruby_location(ctx, node-><%= field.c_name %>)); <%- when "rbs_ast_symbol" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.name %>)); // rbs_ast_symbol + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.c_name %>)); // rbs_ast_symbol <%- when "rbs_keyword" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.name %>)); // rbs_keyword + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.c_name %>)); // rbs_keyword <%- when "rbs_string" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_string_to_ruby_string(&node-><%= field.name %>, ctx.encoding)); + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_string_to_ruby_string(&node-><%= field.c_name %>, ctx.encoding)); <%- when "bool" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), node-><%= field.name %> ? Qtrue : Qfalse); + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), node-><%= field.c_name %> ? Qtrue : Qfalse); <%- else -%> <%- unless field.ast_node? -%> #warning unexpected type <%= field.c_type -%> <%- end -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.name %>)); // <%= field.c_type %> + rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.c_name %>)); // <%= field.c_type %> <%- end -%> <%- end -%> diff --git a/templates/src/ast.c.erb b/templates/src/ast.c.erb index bb314f70c..7339562f3 100644 --- a/templates/src/ast.c.erb +++ b/templates/src/ast.c.erb @@ -140,7 +140,7 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_constant_po <%= node.c_type_name %> *instance = rbs_allocator_alloc(allocator, <%= node.c_type_name %>); <%- node.fields.filter { |f| f.c_type == "VALUE" }.each do |f| -%> - rb_gc_register_mark_object(<%= f.name %>); + rb_gc_register_mark_object(<%= f.c_name %>); <%- end -%> *instance = (<%= node.c_type_name %>) { @@ -148,7 +148,7 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_constant_po .type = <%= node.c_type_enum_name %> }, <%- node.fields.each do |field| -%> - .<%= field.name %> = <%= field.name %>, + .<%= field.c_name %> = <%= field.c_name %>, <%- end -%> }; From b33b6f58951308f58097d01b2bef66943b9e0958 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 3 Dec 2024 19:14:55 -0500 Subject: [PATCH 044/111] Move `declare_type_variables` to `main.c` --- ext/rbs_extension/main.c | 36 +++++++++++++++++++++++++++++++-- ext/rbs_extension/parserstate.c | 32 ----------------------------- ext/rbs_extension/parserstate.h | 9 +-------- 3 files changed, 35 insertions(+), 42 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 61b12da4f..0d899ccc0 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -34,6 +34,38 @@ static NORETURN(void) raise_error(error *error, VALUE buffer) { rb_exc_raise(rb_error); } +static void declare_type_variables(parserstate *parser, VALUE variables) { + if (NIL_P(variables)) return; // Nothing to do. + + if (!RB_TYPE_P(variables, T_ARRAY)) { + rb_raise(rb_eTypeError, + "wrong argument type %"PRIsVALUE" (must be an Array of Symbols or nil)", + rb_obj_class(variables)); + } + + parser_push_typevar_table(parser, true); + + for (long i = 0; i < rb_array_len(variables); i++) { + VALUE symbol = rb_ary_entry(variables, i); + + if (!RB_TYPE_P(symbol, T_SYMBOL)) { + rb_raise(rb_eTypeError, + "Type variables Array contains invalid value %"PRIsVALUE" of type %"PRIsVALUE" (must be an Array of Symbols or nil)", + rb_inspect(symbol), rb_obj_class(symbol)); + } + + VALUE name_str = rb_sym2str(symbol); + + rbs_constant_id_t id = rbs_constant_pool_insert_shared( + &parser->constant_pool, + (const uint8_t *) RSTRING_PTR(name_str), + RSTRING_LEN(name_str) + ); + + parser_insert_typevar(parser, id); + } +} + struct parse_type_arg { VALUE buffer; rb_encoding *encoding; @@ -121,7 +153,7 @@ static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VAL rb_encoding *encoding = rb_enc_get(string); parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); - rbs_parser_declare_type_variables_from_ruby_array(parser, variables); + declare_type_variables(parser, variables); struct parse_type_arg arg = { .buffer = buffer, .encoding = encoding, @@ -169,7 +201,7 @@ static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_p rb_encoding *encoding = rb_enc_get(string); parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); - rbs_parser_declare_type_variables_from_ruby_array(parser, variables); + declare_type_variables(parser, variables); struct parse_type_arg arg = { .buffer = buffer, .encoding = encoding, diff --git a/ext/rbs_extension/parserstate.c b/ext/rbs_extension/parserstate.c index 26b82f54f..42bf3a434 100644 --- a/ext/rbs_extension/parserstate.c +++ b/ext/rbs_extension/parserstate.c @@ -359,38 +359,6 @@ void rbs_parser_declare_type_variables(parserstate *parser, size_t count, const } } -void rbs_parser_declare_type_variables_from_ruby_array(parserstate *parser, VALUE variables) { - if (NIL_P(variables)) return; // Nothing to do. - - if (!RB_TYPE_P(variables, T_ARRAY)) { - rb_raise(rb_eTypeError, - "wrong argument type %"PRIsVALUE" (must be an Array of Symbols or nil)", - rb_obj_class(variables)); - } - - parser_push_typevar_table(parser, true); - - for (long i = 0; i < rb_array_len(variables); i++) { - VALUE symbol = rb_ary_entry(variables, i); - - if (!RB_TYPE_P(symbol, T_SYMBOL)) { - rb_raise(rb_eTypeError, - "Type variables Array contains invalid value %"PRIsVALUE" of type %"PRIsVALUE" (must be an Array of Symbols or nil)", - rb_inspect(symbol), rb_obj_class(symbol)); - } - - VALUE name_str = rb_sym2str(symbol); - - rbs_constant_id_t id = rbs_constant_pool_insert_shared( - &parser->constant_pool, - (const uint8_t *) RSTRING_PTR(name_str), - RSTRING_LEN(name_str) - ); - - parser_insert_typevar(parser, id); - } -} - void free_parser(parserstate *parser) { rbs_constant_pool_free(&parser->constant_pool); rbs_allocator_free(&parser->allocator); diff --git a/ext/rbs_extension/parserstate.h b/ext/rbs_extension/parserstate.h index 8008f52a9..a9c6ae60d 100644 --- a/ext/rbs_extension/parserstate.h +++ b/ext/rbs_extension/parserstate.h @@ -118,7 +118,7 @@ lexstate *alloc_lexer(rbs_allocator_t *, rbs_string_t string, const rbs_encoding * Allocate new parserstate object. * Optionally call `rbs_parser_declare_type_variables_from_ruby_array()` after, to populate the type variable table. * - * Once allocated, optionally call `rbs_parser_declare_type_variables_from_ruby_array` if you'd like to declare + * Once allocated, optionally call `rbs_parser_declare_type_variables` if you'd like to declare * any type variables to be used during the parsing. * * ``` @@ -129,13 +129,6 @@ lexstate *alloc_lexer(rbs_allocator_t *, rbs_string_t string, const rbs_encoding parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); void free_parser(parserstate *parser); -/** - * Inserts the given array of type variables names into the parser's type variable table. - * @param parser - * @param variables A Ruby Array of Symbols, or nil. - */ -void rbs_parser_declare_type_variables_from_ruby_array(parserstate *parser, VALUE variables); - /** * Advance one token. * */ From 9a28ef6cc900022108c0dd21c039b44253288091 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 3 Dec 2024 19:15:32 -0500 Subject: [PATCH 045/111] =?UTF-8?q?Replace=20Ruby=E2=80=99s=20`PRINTF=5FAR?= =?UTF-8?q?GS`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ext/rbs_extension/parser.h | 3 ++- include/rbs/defines.h | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 include/rbs/defines.h diff --git a/ext/rbs_extension/parser.h b/ext/rbs_extension/parser.h index 2bf48f347..6da3aef2d 100644 --- a/ext/rbs_extension/parser.h +++ b/ext/rbs_extension/parser.h @@ -1,9 +1,10 @@ #ifndef RBS__PARSER_H #define RBS__PARSER_H +#include "rbs/defines.h" #include "parserstate.h" -PRINTF_ARGS(void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt, ...), 4, 5); +void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5); bool parse_type(parserstate *state, rbs_node_t **type); bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type); diff --git a/include/rbs/defines.h b/include/rbs/defines.h new file mode 100644 index 000000000..ad24fd6c3 --- /dev/null +++ b/include/rbs/defines.h @@ -0,0 +1,24 @@ +// This file contains a modified subset of Prism's `include/prism/defines.h` + +#ifndef RBS_DEFINES_H +#define RBS_DEFINES_H + +/** + * Certain compilers support specifying that a function accepts variadic + * parameters that look like printf format strings to provide a better developer + * experience when someone is using the function. This macro does that in a + * compiler-agnostic way. + */ +#if defined(__GNUC__) +# if defined(__MINGW_PRINTF_FORMAT) +# define RBS_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(__MINGW_PRINTF_FORMAT, string_index, argument_index))) +# else +# define RBS_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(printf, string_index, argument_index))) +# endif +#elif defined(__clang__) +# define RBS_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((__format__(__printf__, string_index, argument_index))) +#else +# define RBS_ATTRIBUTE_FORMAT(string_index, argument_index) +#endif + +#endif From 6a51147ecdd52dbac48dc34c1fefe960e95ffc1d Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 3 Dec 2024 19:32:06 -0500 Subject: [PATCH 046/111] Remove `ruby.h` from `parserstate.h` --- ext/rbs_extension/parser.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ext/rbs_extension/parser.c b/ext/rbs_extension/parser.c index b9a03cf36..fa49f4ac0 100644 --- a/ext/rbs_extension/parser.c +++ b/ext/rbs_extension/parser.c @@ -1,4 +1,11 @@ #include "parser.h" + +#include +#include +#include +#include +#include + #include "rbs/encoding.h" #include "rbs/rbs_string.h" #include "ast_translation.h" From 6826520fc79b40a40679129b19f2b048024c2c2b Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Wed, 4 Dec 2024 10:22:14 -0500 Subject: [PATCH 047/111] Move parser and parserstate out of `rbs_extension/` Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/rbs_extension.h | 2 +- {ext/rbs_extension => include/rbs}/parser.h | 0 {ext/rbs_extension => include/rbs}/parserstate.h | 0 {ext/rbs_extension => src}/parser.c | 2 +- {ext/rbs_extension => src}/parserstate.c | 2 ++ 5 files changed, 4 insertions(+), 2 deletions(-) rename {ext/rbs_extension => include/rbs}/parser.h (100%) rename {ext/rbs_extension => include/rbs}/parserstate.h (100%) rename {ext/rbs_extension => src}/parser.c (99%) rename {ext/rbs_extension => src}/parserstate.c (99%) diff --git a/ext/rbs_extension/rbs_extension.h b/ext/rbs_extension/rbs_extension.h index f9ebf25bf..afedf85a1 100644 --- a/ext/rbs_extension/rbs_extension.h +++ b/ext/rbs_extension/rbs_extension.h @@ -7,7 +7,7 @@ #include "class_constants.h" #include "rbs.h" #include "rbs/lexer.h" -#include "parser.h" +#include "rbs/parser.h" /** * RBS::Parser class diff --git a/ext/rbs_extension/parser.h b/include/rbs/parser.h similarity index 100% rename from ext/rbs_extension/parser.h rename to include/rbs/parser.h diff --git a/ext/rbs_extension/parserstate.h b/include/rbs/parserstate.h similarity index 100% rename from ext/rbs_extension/parserstate.h rename to include/rbs/parserstate.h diff --git a/ext/rbs_extension/parser.c b/src/parser.c similarity index 99% rename from ext/rbs_extension/parser.c rename to src/parser.c index fa49f4ac0..a2ef49f4d 100644 --- a/ext/rbs_extension/parser.c +++ b/src/parser.c @@ -1,4 +1,4 @@ -#include "parser.h" +#include "rbs/parser.h" #include #include diff --git a/ext/rbs_extension/parserstate.c b/src/parserstate.c similarity index 99% rename from ext/rbs_extension/parserstate.c rename to src/parserstate.c index 42bf3a434..088bb22d6 100644 --- a/ext/rbs_extension/parserstate.c +++ b/src/parserstate.c @@ -1,3 +1,5 @@ +#include "rbs/parserstate.h" + #include "rbs_extension.h" #include "rbs/encoding.h" From 9308358b1e8c2783bf7db1dd69c1be9f111b105c Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Wed, 4 Dec 2024 11:42:53 -0500 Subject: [PATCH 048/111] Move `NODISCARD` macro out of `parser.c` --- include/rbs/defines.h | 13 +++++++++++-- src/parser.c | 3 --- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/rbs/defines.h b/include/rbs/defines.h index ad24fd6c3..8bf1cedd1 100644 --- a/include/rbs/defines.h +++ b/include/rbs/defines.h @@ -1,8 +1,11 @@ -// This file contains a modified subset of Prism's `include/prism/defines.h` - #ifndef RBS_DEFINES_H #define RBS_DEFINES_H + +/*********************************************************************************************************************** + * Copied+modified subset of Prism's `include/prism/defines.h` * + **********************************************************************************************************************/ + /** * Certain compilers support specifying that a function accepts variadic * parameters that look like printf format strings to provide a better developer @@ -21,4 +24,10 @@ # define RBS_ATTRIBUTE_FORMAT(string_index, argument_index) #endif +/*********************************************************************************************************************** + * Custom defines for RBS * + **********************************************************************************************************************/ + +#define NODISCARD __attribute__((warn_unused_result)) + #endif diff --git a/src/parser.c b/src/parser.c index a2ef49f4d..a8063d7f1 100644 --- a/src/parser.c +++ b/src/parser.c @@ -61,9 +61,6 @@ case k__TODO__: \ /* nop */ -#define NODISCARD \ - __attribute__ ((warn_unused_result)) - #define CHECK_PARSE(call) \ if (!call) { \ return false; \ From 0f59bf65b5e590c1e197d041e5d8f7cf9deded3d Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Wed, 4 Dec 2024 11:43:37 -0500 Subject: [PATCH 049/111] Remove `rb_raise` from typevar table functions --- ext/rbs_extension/main.c | 16 ++++++++++++---- include/rbs/parserstate.h | 7 ++++--- src/parser.c | 27 +++++++++++++++------------ src/parserstate.c | 22 +++++++++++++++++----- 4 files changed, 48 insertions(+), 24 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 0d899ccc0..a5ed8dc69 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -34,7 +34,12 @@ static NORETURN(void) raise_error(error *error, VALUE buffer) { rb_exc_raise(rb_error); } -static void declare_type_variables(parserstate *parser, VALUE variables) { +/** + * Inserts the given array of type variables names into the parser's type variable table. + * @param parser + * @param variables A Ruby Array of Symbols, or nil. + */ +static void declare_type_variables(parserstate *parser, VALUE variables, VALUE buffer) { if (NIL_P(variables)) return; // Nothing to do. if (!RB_TYPE_P(variables, T_ARRAY)) { @@ -62,7 +67,10 @@ static void declare_type_variables(parserstate *parser, VALUE variables) { RSTRING_LEN(name_str) ); - parser_insert_typevar(parser, id); + if (!parser_insert_typevar(parser, id)) { + assert(parser->error != NULL); + raise_error(parser->error, buffer); + } } } @@ -153,7 +161,7 @@ static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VAL rb_encoding *encoding = rb_enc_get(string); parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); - declare_type_variables(parser, variables); + declare_type_variables(parser, variables, buffer); struct parse_type_arg arg = { .buffer = buffer, .encoding = encoding, @@ -201,7 +209,7 @@ static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_p rb_encoding *encoding = rb_enc_get(string); parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); - declare_type_variables(parser, variables); + declare_type_variables(parser, variables, buffer); struct parse_type_arg arg = { .buffer = buffer, .encoding = encoding, diff --git a/include/rbs/parserstate.h b/include/rbs/parserstate.h index a9c6ae60d..8cc183ea9 100644 --- a/include/rbs/parserstate.h +++ b/include/rbs/parserstate.h @@ -3,9 +3,9 @@ #include +#include "rbs/defines.h" #include "rbs/util/rbs_allocator.h" #include "rbs/util/rbs_constant_pool.h" -#include "ruby.h" // TODO: remove this #include "rbs/lexer.h" #include "rbs/ast.h" @@ -91,11 +91,12 @@ comment *comment_get_comment(comment *com, int line); * ``` * */ id_table *parser_push_typevar_table(parserstate *state, bool reset); -void parser_pop_typevar_table(parserstate *state); +NODISCARD bool parser_pop_typevar_table(parserstate *state); + /** * Insert new type variable into the latest table. * */ -void parser_insert_typevar(parserstate *state, rbs_constant_id_t id); +NODISCARD bool parser_insert_typevar(parserstate *state, rbs_constant_id_t id); /** * Returns true if given type variable is recorded in the table. diff --git a/src/parser.c b/src/parser.c index a8063d7f1..e9f05c16a 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1299,7 +1299,7 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa rbs_constant_id_t id = rbs_constant_pool_insert_string(&state->constant_pool, string); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, id); - parser_insert_typevar(state, id); + CHECK_PARSE(parser_insert_typevar(state, id)); range upper_bound_range = NULL_RANGE; if (state->next_token.type == pLT) { @@ -1382,7 +1382,7 @@ bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type) { rg.end = state->current_token.range.end; type_range.end = rg.end; - parser_pop_typevar_table(state); + CHECK_PARSE(parser_pop_typevar_table(state)); rbs_location_t *loc = rbs_location_new(rg); rbs_loc_alloc_children(loc, 2); @@ -1491,7 +1491,7 @@ static bool parse_type_decl(parserstate *state, position comment_pos, rbs_node_l rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range); rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); - parser_pop_typevar_table(state); + CHECK_PARSE(parser_pop_typevar_table(state)); rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); @@ -1817,7 +1817,7 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept } } - parser_pop_typevar_table(state); + CHECK_PARSE(parser_pop_typevar_table(state)); rbs_keyword_t *k; switch (kind) { @@ -1929,7 +1929,7 @@ static bool parse_mixin_member(parserstate *state, bool from_interface, position args, &name_range, &args_range, &name )); - parser_pop_typevar_table(state); + CHECK_PARSE(parser_pop_typevar_table(state)); member_range.end = state->current_token.range.end; @@ -2064,7 +2064,8 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ rbs_node_t *type; CHECK_PARSE(parse_type(state, &type)); - parser_pop_typevar_table(state); + CHECK_PARSE(parser_pop_typevar_table(state)); + member_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(member_range); @@ -2096,7 +2097,8 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ rbs_node_t *type; CHECK_PARSE(parse_type(state, &type)); - parser_pop_typevar_table(state); + CHECK_PARSE(parser_pop_typevar_table(state)); + member_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(member_range); @@ -2229,7 +2231,8 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs rbs_node_t *type; CHECK_PARSE(parse_type(state, &type)); - parser_pop_typevar_table(state); + CHECK_PARSE(parser_pop_typevar_table(state)); + member_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(member_range); @@ -2341,7 +2344,7 @@ static bool parse_interface_decl(parserstate *state, position comment_pos, rbs_n range end_range = state->current_token.range; member_range.end = end_range.end; - parser_pop_typevar_table(state); + CHECK_PARSE(parser_pop_typevar_table(state)); rbs_location_t *loc = rbs_location_new(member_range); rbs_loc_alloc_children(loc, 4); @@ -2546,7 +2549,7 @@ static bool parse_module_decl0(parserstate *state, range keyword_range, rbs_type rbs_loc_add_optional_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("self_types"), self_types_range); - parser_pop_typevar_table(state); + CHECK_PARSE(parser_pop_typevar_table(state)); *module_decl = rbs_ast_declarations_module_new(&state->allocator, module_name, type_params, self_types, members, annotations, loc, comment); return true; @@ -2661,7 +2664,7 @@ static bool parse_class_decl0(parserstate *state, range keyword_range, rbs_typen decl_range.end = end_range.end; - parser_pop_typevar_table(state); + CHECK_PARSE(parser_pop_typevar_table(state)); rbs_location_t *loc = rbs_location_new(decl_range); rbs_loc_alloc_children(loc, 5); @@ -2776,7 +2779,7 @@ static bool parse_nested_decl(parserstate *state, const char *nested_in, positio return false; } - parser_pop_typevar_table(state); + CHECK_PARSE(parser_pop_typevar_table(state)); return true; } diff --git a/src/parserstate.c b/src/parserstate.c index 088bb22d6..704272043 100644 --- a/src/parserstate.c +++ b/src/parserstate.c @@ -47,27 +47,33 @@ id_table *parser_push_typevar_table(parserstate *state, bool reset) { return table; } -void parser_pop_typevar_table(parserstate *state) { +NODISCARD +bool parser_pop_typevar_table(parserstate *state) { id_table *table; if (state->vars) { table = state->vars; state->vars = table->next; } else { - rb_raise(rb_eRuntimeError, "Cannot pop empty table"); + set_error(state, state->current_token, false, "Cannot pop empty table"); + return false; } if (state->vars && RESET_TABLE_P(state->vars)) { table = state->vars; state->vars = table->next; } + + return true; } -void parser_insert_typevar(parserstate *state, rbs_constant_id_t id) { +NODISCARD +bool parser_insert_typevar(parserstate *state, rbs_constant_id_t id) { id_table *table = state->vars; if (RESET_TABLE_P(table)) { - rb_raise(rb_eRuntimeError, "Cannot insert to reset table"); + set_error(state, state->current_token, false, "Cannot insert to reset table"); + return false; } if (table->size == table->count) { @@ -79,6 +85,8 @@ void parser_insert_typevar(parserstate *state, rbs_constant_id_t id) { } table->ids[table->count++] = id; + + return true; } bool parser_typevar_member(parserstate *state, rbs_constant_id_t id) { @@ -357,7 +365,11 @@ void rbs_parser_declare_type_variables(parserstate *parser, size_t count, const (const uint8_t *) variables[i], strlen(variables[i]) ); - parser_insert_typevar(parser, name); + + if (!parser_insert_typevar(parser, name)) { + fprintf(stderr, "RuntimeError: %s\n", parser->error->message); + exit(1); + } } } From c6830cf5e12e1fe6799ab8a520a196f185926089 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Wed, 4 Dec 2024 14:22:10 -0500 Subject: [PATCH 050/111] Clean some `.h` includes Signed-off-by: Alexandre Terrasa --- ext/rbs_extension/location.h | 5 +---- ext/rbs_extension/rbs_extension.h | 2 -- include/rbs.h | 2 +- include/rbs/lexer.h | 4 ++-- include/rbs/parser.h | 1 - include/rbs/parserstate.h | 6 +++--- include/rbs/rbs_buffer.h | 4 ++-- include/rbs/rbs_encoding.h | 2 +- include/rbs/rbs_unescape.h | 2 -- include/rbs/util/rbs_allocator.h | 5 +---- src/parserstate.c | 5 +++-- 11 files changed, 14 insertions(+), 24 deletions(-) diff --git a/ext/rbs_extension/location.h b/ext/rbs_extension/location.h index c3e030f81..2c1446f63 100644 --- a/ext/rbs_extension/location.h +++ b/ext/rbs_extension/location.h @@ -2,10 +2,7 @@ #define RBS_LOCATION_H #include "ruby.h" -#include "rbs/lexer.h" -#include "rbs/rbs_location.h" -#include "rbs/rbs_location_internals.h" -#include "rbs/util/rbs_constant_pool.h" +#include "rbs.h" /** * RBS::Location class diff --git a/ext/rbs_extension/rbs_extension.h b/ext/rbs_extension/rbs_extension.h index afedf85a1..ddc092850 100644 --- a/ext/rbs_extension/rbs_extension.h +++ b/ext/rbs_extension/rbs_extension.h @@ -6,8 +6,6 @@ #include "class_constants.h" #include "rbs.h" -#include "rbs/lexer.h" -#include "rbs/parser.h" /** * RBS::Parser class diff --git a/include/rbs.h b/include/rbs.h index 6a0c4f8c6..8de185498 100644 --- a/include/rbs.h +++ b/include/rbs.h @@ -1,6 +1,6 @@ #ifndef RBS_H #define RBS_H -#include "rbs/ast.h" +#include "rbs/parser.h" #endif diff --git a/include/rbs/lexer.h b/include/rbs/lexer.h index 599a768ac..0e2130503 100644 --- a/include/rbs/lexer.h +++ b/include/rbs/lexer.h @@ -1,8 +1,8 @@ #ifndef RBS__LEXER_H #define RBS__LEXER_H -#include "rbs/rbs_string.h" -#include "rbs/rbs_encoding.h" +#include "rbs_string.h" +#include "rbs_encoding.h" enum TokenType { NullType, /* (Nothing) */ diff --git a/include/rbs/parser.h b/include/rbs/parser.h index 6da3aef2d..a647d8f62 100644 --- a/include/rbs/parser.h +++ b/include/rbs/parser.h @@ -1,7 +1,6 @@ #ifndef RBS__PARSER_H #define RBS__PARSER_H -#include "rbs/defines.h" #include "parserstate.h" void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5); diff --git a/include/rbs/parserstate.h b/include/rbs/parserstate.h index 8cc183ea9..c6d2ce528 100644 --- a/include/rbs/parserstate.h +++ b/include/rbs/parserstate.h @@ -1,15 +1,15 @@ #ifndef RBS__PARSERSTATE_H #define RBS__PARSERSTATE_H -#include - #include "rbs/defines.h" #include "rbs/util/rbs_allocator.h" #include "rbs/util/rbs_constant_pool.h" - #include "rbs/lexer.h" #include "rbs/ast.h" +#include +#include + /** * id_table represents a set of RBS constant IDs. * This is used to manage the set of bound variables. diff --git a/include/rbs/rbs_buffer.h b/include/rbs/rbs_buffer.h index ba85fba4b..a8e70c1b6 100644 --- a/include/rbs/rbs_buffer.h +++ b/include/rbs/rbs_buffer.h @@ -1,12 +1,12 @@ #ifndef RBS__RBS_BUFFER_H #define RBS__RBS_BUFFER_H +#include "rbs_string.h" + #include #include #include -#include "rbs/rbs_string.h" - /** * A rbs_buffer_t is a simple memory buffer that stores data in a contiguous block of memory. */ diff --git a/include/rbs/rbs_encoding.h b/include/rbs/rbs_encoding.h index e3c941c15..b0c294c3d 100644 --- a/include/rbs/rbs_encoding.h +++ b/include/rbs/rbs_encoding.h @@ -6,7 +6,7 @@ #ifndef RBS_RBS_ENCODING_H #define RBS_RBS_ENCODING_H -#include "rbs/rbs_strncasecmp.h" +#include "rbs_strncasecmp.h" #include #include diff --git a/include/rbs/rbs_unescape.h b/include/rbs/rbs_unescape.h index 3a4881f7a..a74cef74b 100644 --- a/include/rbs/rbs_unescape.h +++ b/include/rbs/rbs_unescape.h @@ -3,8 +3,6 @@ #include -#include "rbs/rbs_string.h" - /** * Receives `parserstate` and `range`, which represents a string token or symbol token, and returns a string VALUE. * diff --git a/include/rbs/util/rbs_allocator.h b/include/rbs/util/rbs_allocator.h index a5d4cbdc0..0a53cbe89 100644 --- a/include/rbs/util/rbs_allocator.h +++ b/include/rbs/util/rbs_allocator.h @@ -5,14 +5,11 @@ #ifndef alignof #if defined(__GNUC__) || defined(__clang__) - /* GCC or Clang */ #define alignof(type) __alignof__(type) #elif defined(_MSC_VER) - /* Microsoft Visual C++ */ #define alignof(type) __alignof(type) #else - /* Fallback using offset trick */ - #include + // Fallback using offset trick #define alignof(type) offsetof(struct { char c; type member; }, member) #endif #endif diff --git a/src/parserstate.c b/src/parserstate.c index 704272043..717da4373 100644 --- a/src/parserstate.c +++ b/src/parserstate.c @@ -1,10 +1,11 @@ #include "rbs/parserstate.h" -#include "rbs_extension.h" - +#include "rbs/parser.h" #include "rbs/encoding.h" #include "rbs/rbs_buffer.h" +#include + #define RESET_TABLE_P(table) (table->size == 0) id_table *alloc_empty_table(rbs_allocator_t *allocator) { From 3a1c658141fdf6cd66265d6b0a41a09e3fe61697 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 7 Jan 2025 11:59:05 -0500 Subject: [PATCH 051/111] Enable implicit fall-through errors --- ext/rbs_extension/extconf.rb | 2 +- include/rbs/defines.h | 23 +++++++++++++++++++++++ src/parser.c | 8 +++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/ext/rbs_extension/extconf.rb b/ext/rbs_extension/extconf.rb index 092f25b41..a63182c4e 100644 --- a/ext/rbs_extension/extconf.rb +++ b/ext/rbs_extension/extconf.rb @@ -11,5 +11,5 @@ $srcs = Dir.glob("#{root_dir}/src/**/*.c") + Dir.glob("#{root_dir}/ext/rbs_extension/*.c") -append_cflags ['-std=gnu99', '-Wunused-result'] +append_cflags ['-std=gnu99', '-Wimplicit-fallthrough', '-Wunused-result'] create_makefile 'rbs_extension' diff --git a/include/rbs/defines.h b/include/rbs/defines.h index 8bf1cedd1..adac6dc5a 100644 --- a/include/rbs/defines.h +++ b/include/rbs/defines.h @@ -1,3 +1,12 @@ +/** + * @file defines.h + * + * Macro definitions used throughout the rbs library. + * + * This file should be included first by any *.h or *.c in rbs for consistency + * and to ensure that the macros are defined before they are used. + */ + #ifndef RBS_DEFINES_H #define RBS_DEFINES_H @@ -24,6 +33,20 @@ # define RBS_ATTRIBUTE_FORMAT(string_index, argument_index) #endif +/** + * We use -Wimplicit-fallthrough to guard potentially unintended fall-through between cases of a switch. + * Use RBS_FALLTHROUGH to explicitly annotate cases where the fallthrough is intentional. + */ +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L // C23 or later + #define RBS_FALLTHROUGH [[fallthrough]]; +#elif defined(__GNUC__) || defined(__clang__) + #define RBS_FALLTHROUGH __attribute__((fallthrough)); +#elif defined(_MSC_VER) + #define RBS_FALLTHROUGH __fallthrough; +#else + #define RBS_FALLTHROUGH +#endif + /*********************************************************************************************************************** * Custom defines for RBS * **********************************************************************************************************************/ diff --git a/src/parser.c b/src/parser.c index e9f05c16a..ed52e0bd6 100644 --- a/src/parser.c +++ b/src/parser.c @@ -6,6 +6,7 @@ #include #include +#include "rbs/defines.h" #include "rbs/encoding.h" #include "rbs/rbs_string.h" #include "ast_translation.h" @@ -1116,10 +1117,11 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { *type = (rbs_node_t *) rbs_types_variable_new(&state->allocator, symbol, loc); return true; } - // fallthrough for type name + + RBS_FALLTHROUGH // for type name } - case tULIDENT: // fallthrough - case tLIDENT: // fallthrough + case tULIDENT: + case tLIDENT: case pCOLON2: { rbs_node_t *instance_type = NULL; CHECK_PARSE(parse_instance_type(state, true, &instance_type)); From 31823201541d6dcb206bc29487c88589e648593a Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Wed, 4 Dec 2024 18:15:52 -0500 Subject: [PATCH 052/111] Extract `raise_error_if_any()` --- ext/rbs_extension/main.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index a5ed8dc69..bd4fe778b 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -8,13 +8,15 @@ #include "ruby/vm.h" /** - * Raises RBS::ParsingError on `tok` with message constructed with given `fmt`. + * Raises `RBS::ParsingError` or `RuntimeError` on `tok` with message constructed with given `fmt`. * * ``` * foo.rbs:11:21...11:25: Syntax error: {message}, token=`{tok source}` ({tok type}) * ``` * */ static NORETURN(void) raise_error(error *error, VALUE buffer) { + assert(error != NULL && "raise_error() called with NULL error"); + if (!error->syntax_error) { rb_raise(rb_eRuntimeError, "Unexpected error"); } @@ -34,6 +36,12 @@ static NORETURN(void) raise_error(error *error, VALUE buffer) { rb_exc_raise(rb_error); } +void raise_error_if_any(parserstate *parser, VALUE buffer) { + if (parser->error != NULL) { + raise_error(parser->error, buffer); + } +} + /** * Inserts the given array of type variables names into the parser's type variable table. * @param parser @@ -68,7 +76,6 @@ static void declare_type_variables(parserstate *parser, VALUE variables, VALUE b ); if (!parser_insert_typevar(parser, id)) { - assert(parser->error != NULL); raise_error(parser->error, buffer); } } @@ -97,9 +104,7 @@ static VALUE parse_type_try(VALUE a) { rbs_node_t *type; parse_type(parser, &type); - if (parser->error) { - raise_error(parser->error, arg->buffer); - } + raise_error_if_any(parser, arg->buffer); if (RB_TEST(arg->require_eof)) { parser_advance(parser); @@ -182,9 +187,7 @@ static VALUE parse_method_type_try(VALUE a) { rbs_methodtype_t *method_type = NULL; parse_method_type(parser, &method_type); - if (parser->error) { - raise_error(parser->error, arg->buffer); - } + raise_error_if_any(parser, arg->buffer); if (RB_TEST(arg->require_eof)) { parser_advance(parser); @@ -226,9 +229,7 @@ static VALUE parse_signature_try(VALUE a) { rbs_signature_t *signature = NULL; parse_signature(parser, &signature); - if (parser->error) { - raise_error(parser->error, arg->buffer); - } + raise_error_if_any(parser, arg->buffer); rbs_translation_context_t ctx = rbs_translation_context_create( &parser->constant_pool, From 12ee0a7323f5c4af06293398507d2a273ce18f1d Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Thu, 5 Dec 2024 10:23:22 -0500 Subject: [PATCH 053/111] Add reverse lookup for nodes Signed-off-by: Alexandre Terrasa --- include/rbs/ast.h | 2 + src/ast.c | 70 +++++++++++++++++++++++++++++++++ templates/include/rbs/ast.h.erb | 2 + templates/src/ast.c.erb | 10 +++++ 4 files changed, 84 insertions(+) diff --git a/include/rbs/ast.h b/include/rbs/ast.h index e11d88939..4dccf9bd4 100644 --- a/include/rbs/ast.h +++ b/include/rbs/ast.h @@ -85,6 +85,8 @@ typedef struct rbs_node { enum rbs_node_type type; } rbs_node_t; +const char* rbs_node_type_name(rbs_node_t *node); + /* rbs_node_list_node */ typedef struct rbs_node_list_node { diff --git a/src/ast.c b/src/ast.c index 56159590f..2245fa881 100644 --- a/src/ast.c +++ b/src/ast.c @@ -10,6 +10,76 @@ #include #include +const char* rbs_node_type_name(rbs_node_t *node) { + switch (node->type) { + case RBS_AST_ANNOTATION: return "RBS::AST::Annotation"; + case RBS_AST_BOOL: return "RBS::AST::Bool"; + case RBS_AST_COMMENT: return "RBS::AST::Comment"; + case RBS_AST_DECLARATIONS_CLASS: return "RBS::AST::Declarations::Class"; + case RBS_AST_DECLARATIONS_CLASS_SUPER: return "RBS::AST::Declarations::Class::Super"; + case RBS_AST_DECLARATIONS_CLASSALIAS: return "RBS::AST::Declarations::ClassAlias"; + case RBS_AST_DECLARATIONS_CONSTANT: return "RBS::AST::Declarations::Constant"; + case RBS_AST_DECLARATIONS_GLOBAL: return "RBS::AST::Declarations::Global"; + case RBS_AST_DECLARATIONS_INTERFACE: return "RBS::AST::Declarations::Interface"; + case RBS_AST_DECLARATIONS_MODULE: return "RBS::AST::Declarations::Module"; + case RBS_AST_DECLARATIONS_MODULE_SELF: return "RBS::AST::Declarations::Module::Self"; + case RBS_AST_DECLARATIONS_MODULEALIAS: return "RBS::AST::Declarations::ModuleAlias"; + case RBS_AST_DECLARATIONS_TYPEALIAS: return "RBS::AST::Declarations::TypeAlias"; + case RBS_AST_DIRECTIVES_USE: return "RBS::AST::Directives::Use"; + case RBS_AST_DIRECTIVES_USE_SINGLECLAUSE: return "RBS::AST::Directives::Use::SingleClause"; + case RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE: return "RBS::AST::Directives::Use::WildcardClause"; + case RBS_AST_INTEGER: return "RBS::AST::Integer"; + case RBS_AST_MEMBERS_ALIAS: return "RBS::AST::Members::Alias"; + case RBS_AST_MEMBERS_ATTRACCESSOR: return "RBS::AST::Members::AttrAccessor"; + case RBS_AST_MEMBERS_ATTRREADER: return "RBS::AST::Members::AttrReader"; + case RBS_AST_MEMBERS_ATTRWRITER: return "RBS::AST::Members::AttrWriter"; + case RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE: return "RBS::AST::Members::ClassInstanceVariable"; + case RBS_AST_MEMBERS_CLASSVARIABLE: return "RBS::AST::Members::ClassVariable"; + case RBS_AST_MEMBERS_EXTEND: return "RBS::AST::Members::Extend"; + case RBS_AST_MEMBERS_INCLUDE: return "RBS::AST::Members::Include"; + case RBS_AST_MEMBERS_INSTANCEVARIABLE: return "RBS::AST::Members::InstanceVariable"; + case RBS_AST_MEMBERS_METHODDEFINITION: return "RBS::AST::Members::MethodDefinition"; + case RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD: return "RBS::AST::Members::MethodDefinition::Overload"; + case RBS_AST_MEMBERS_PREPEND: return "RBS::AST::Members::Prepend"; + case RBS_AST_MEMBERS_PRIVATE: return "RBS::AST::Members::Private"; + case RBS_AST_MEMBERS_PUBLIC: return "RBS::AST::Members::Public"; + case RBS_AST_STRING: return "RBS::AST::String"; + case RBS_AST_TYPEPARAM: return "RBS::AST::TypeParam"; + case RBS_METHODTYPE: return "RBS::MethodType"; + case RBS_NAMESPACE: return "RBS::Namespace"; + case RBS_SIGNATURE: return "RBS::Signature"; + case RBS_TYPENAME: return "RBS::TypeName"; + case RBS_TYPES_ALIAS: return "RBS::Types::Alias"; + case RBS_TYPES_BASES_ANY: return "RBS::Types::Bases::Any"; + case RBS_TYPES_BASES_BOOL: return "RBS::Types::Bases::Bool"; + case RBS_TYPES_BASES_BOTTOM: return "RBS::Types::Bases::Bottom"; + case RBS_TYPES_BASES_CLASS: return "RBS::Types::Bases::Class"; + case RBS_TYPES_BASES_INSTANCE: return "RBS::Types::Bases::Instance"; + case RBS_TYPES_BASES_NIL: return "RBS::Types::Bases::Nil"; + case RBS_TYPES_BASES_SELF: return "RBS::Types::Bases::Self"; + case RBS_TYPES_BASES_TOP: return "RBS::Types::Bases::Top"; + case RBS_TYPES_BASES_VOID: return "RBS::Types::Bases::Void"; + case RBS_TYPES_BLOCK: return "RBS::Types::Block"; + case RBS_TYPES_CLASSINSTANCE: return "RBS::Types::ClassInstance"; + case RBS_TYPES_CLASSSINGLETON: return "RBS::Types::ClassSingleton"; + case RBS_TYPES_FUNCTION: return "RBS::Types::Function"; + case RBS_TYPES_FUNCTION_PARAM: return "RBS::Types::Function::Param"; + case RBS_TYPES_INTERFACE: return "RBS::Types::Interface"; + case RBS_TYPES_INTERSECTION: return "RBS::Types::Intersection"; + case RBS_TYPES_LITERAL: return "RBS::Types::Literal"; + case RBS_TYPES_OPTIONAL: return "RBS::Types::Optional"; + case RBS_TYPES_PROC: return "RBS::Types::Proc"; + case RBS_TYPES_RECORD: return "RBS::Types::Record"; + case RBS_TYPES_RECORD_FIELDTYPE: return "RBS::Types::Record::FieldType"; + case RBS_TYPES_TUPLE: return "RBS::Types::Tuple"; + case RBS_TYPES_UNION: return "RBS::Types::Union"; + case RBS_TYPES_UNTYPEDFUNCTION: return "RBS::Types::UntypedFunction"; + case RBS_TYPES_VARIABLE: return "RBS::Types::Variable"; + case RBS_AST_SYMBOL: return "Symbol"; + default: return "Unknown"; + } +} + /* rbs_node_list */ rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *allocator) { diff --git a/templates/include/rbs/ast.h.erb b/templates/include/rbs/ast.h.erb index bd0e7db4f..a3c8a81aa 100644 --- a/templates/include/rbs/ast.h.erb +++ b/templates/include/rbs/ast.h.erb @@ -18,6 +18,8 @@ typedef struct rbs_node { enum rbs_node_type type; } rbs_node_t; +const char* rbs_node_type_name(rbs_node_t *node); + /* rbs_node_list_node */ typedef struct rbs_node_list_node { diff --git a/templates/src/ast.c.erb b/templates/src/ast.c.erb index 7339562f3..922d9d236 100644 --- a/templates/src/ast.c.erb +++ b/templates/src/ast.c.erb @@ -3,6 +3,16 @@ #include #include +const char* rbs_node_type_name(rbs_node_t *node) { + switch (node->type) { + <%- nodes.each do |node| -%> + case <%= node.c_type_enum_name %>: return "<%= node.ruby_full_name %>"; + <%- end -%> + case RBS_AST_SYMBOL: return "Symbol"; + default: return "Unknown"; + } +} + /* rbs_node_list */ rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *allocator) { From bf51108171896b2945987a455b919f3951fb4c5b Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Thu, 5 Dec 2024 10:26:26 -0500 Subject: [PATCH 054/111] Silence warning on missing range initializer Signed-off-by: Alexandre Terrasa --- src/lexstate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lexstate.c b/src/lexstate.c index b5f187826..5e108076e 100644 --- a/src/lexstate.c +++ b/src/lexstate.c @@ -89,7 +89,7 @@ static const char *RBS_TOKENTYPE_NAMES[] = { "tANNOTATION", /* Annotation */ }; -token NullToken = { NullType }; +token NullToken = { .type = NullType, .range = {} }; position NullPosition = { -1, -1, -1, -1 }; range NULL_RANGE = { { -1, -1, -1, -1 }, { -1, -1, -1, -1 } }; From 1753f87457e3de824666b88826cb8de65515a5c2 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Fri, 3 Jan 2025 14:00:02 -0500 Subject: [PATCH 055/111] Give all nodes a location field Signed-off-by: Alexandre Terrasa --- config.yml | 125 +------ ext/rbs_extension/ast_translation.c | 162 +++----- include/rbs/ast.h | 165 +++----- src/ast.c | 353 +++++++++--------- src/parser.c | 263 ++++++++----- src/parserstate.c | 4 +- .../ext/rbs_extension/ast_translation.c.erb | 7 +- templates/include/rbs/ast.h.erb | 5 +- templates/src/ast.c.erb | 9 +- templates/template.rb | 10 +- 10 files changed, 492 insertions(+), 611 deletions(-) diff --git a/config.yml b/config.yml index d6bb4ba7f..f7a8e7b01 100644 --- a/config.yml +++ b/config.yml @@ -3,10 +3,9 @@ nodes: fields: - name: string c_type: rbs_string - - name: location - c_type: rbs_location - name: RBS::AST::Bool expose_to_ruby: false + expose_location: false fields: - name: value c_type: bool @@ -14,8 +13,6 @@ nodes: fields: - name: string c_type: rbs_string - - name: location - c_type: rbs_location - name: RBS::AST::Declarations::Class fields: - name: name @@ -28,8 +25,6 @@ nodes: c_type: rbs_node_list - name: annotations c_type: rbs_node_list - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: RBS::AST::Declarations::Class::Super @@ -38,16 +33,12 @@ nodes: c_type: rbs_typename - name: args c_type: rbs_node_list - - name: location - c_type: rbs_location - name: RBS::AST::Declarations::ClassAlias fields: - name: new_name c_type: rbs_typename - name: old_name c_type: rbs_typename - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: RBS::AST::Declarations::Constant @@ -56,8 +47,6 @@ nodes: c_type: rbs_typename - name: type c_type: rbs_node - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: RBS::AST::Declarations::Global @@ -66,8 +55,6 @@ nodes: c_type: rbs_ast_symbol - name: type c_type: rbs_node - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: RBS::AST::Declarations::Interface @@ -80,8 +67,6 @@ nodes: c_type: rbs_node_list - name: annotations c_type: rbs_node_list - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: RBS::AST::Declarations::Module @@ -96,8 +81,6 @@ nodes: c_type: rbs_node_list - name: annotations c_type: rbs_node_list - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: RBS::AST::Declarations::Module::Self @@ -106,16 +89,12 @@ nodes: c_type: rbs_typename - name: args c_type: rbs_node_list - - name: location - c_type: rbs_location - name: RBS::AST::Declarations::ModuleAlias fields: - name: new_name c_type: rbs_typename - name: old_name c_type: rbs_typename - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: RBS::AST::Declarations::TypeAlias @@ -128,31 +107,23 @@ nodes: c_type: rbs_node - name: annotations c_type: rbs_node_list - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: RBS::AST::Directives::Use fields: - name: clauses c_type: rbs_node_list - - name: location - c_type: rbs_location - name: RBS::AST::Directives::Use::SingleClause fields: - name: type_name c_type: rbs_typename - name: new_name c_type: rbs_ast_symbol - - name: location - c_type: rbs_location - name: RBS::AST::Directives::Use::WildcardClause fields: - name: namespace c_type: rbs_namespace c_name: rbs_namespace - - name: location - c_type: rbs_location - name: RBS::AST::Members::Alias fields: - name: new_name @@ -163,8 +134,6 @@ nodes: c_type: rbs_keyword - name: annotations c_type: rbs_node_list - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: RBS::AST::Members::AttrAccessor @@ -179,8 +148,6 @@ nodes: c_type: rbs_keyword - name: annotations c_type: rbs_node_list - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: visibility @@ -197,8 +164,6 @@ nodes: c_type: rbs_keyword - name: annotations c_type: rbs_node_list - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: visibility @@ -215,8 +180,6 @@ nodes: c_type: rbs_keyword - name: annotations c_type: rbs_node_list - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: visibility @@ -227,8 +190,6 @@ nodes: c_type: rbs_ast_symbol - name: type c_type: rbs_node - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: RBS::AST::Members::ClassVariable @@ -237,8 +198,6 @@ nodes: c_type: rbs_ast_symbol - name: type c_type: rbs_node - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: RBS::AST::Members::Extend @@ -249,8 +208,6 @@ nodes: c_type: rbs_node_list - name: annotations c_type: rbs_node_list - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: RBS::AST::Members::Include @@ -261,8 +218,6 @@ nodes: c_type: rbs_node_list - name: annotations c_type: rbs_node_list - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: RBS::AST::Members::InstanceVariable @@ -271,8 +226,6 @@ nodes: c_type: rbs_ast_symbol - name: type c_type: rbs_node - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: RBS::AST::Members::MethodDefinition @@ -285,8 +238,6 @@ nodes: c_type: rbs_node_list - name: annotations c_type: rbs_node_list - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: overloading @@ -294,6 +245,7 @@ nodes: - name: visibility c_type: rbs_keyword - name: RBS::AST::Members::MethodDefinition::Overload + expose_location: false fields: - name: annotations c_type: rbs_node_list @@ -307,18 +259,10 @@ nodes: c_type: rbs_node_list - name: annotations c_type: rbs_node_list - - name: location - c_type: rbs_location - name: comment c_type: rbs_ast_comment - name: RBS::AST::Members::Private - fields: - - name: location - c_type: rbs_location - name: RBS::AST::Members::Public - fields: - - name: location - c_type: rbs_location - name: RBS::AST::TypeParam fields: - name: name @@ -331,15 +275,15 @@ nodes: c_type: rbs_node - name: unchecked c_type: bool - - name: location - c_type: rbs_location - name: RBS::AST::Integer expose_to_ruby: false + expose_location: false fields: - name: string_representation c_type: rbs_string - name: RBS::AST::String expose_to_ruby: false + expose_location: false fields: - name: string c_type: rbs_string @@ -351,9 +295,8 @@ nodes: c_type: rbs_node - name: block c_type: rbs_types_block - - name: location - c_type: rbs_location - name: RBS::Namespace + expose_location: false fields: - name: path c_type: rbs_node_list @@ -361,12 +304,14 @@ nodes: c_type: bool - name: RBS::Signature expose_to_ruby: false + expose_location: false fields: - name: directives c_type: rbs_node_list - name: declarations c_type: rbs_node_list - name: RBS::TypeName + expose_location: false fields: - name: namespace c_type: rbs_namespace @@ -379,47 +324,20 @@ nodes: c_type: rbs_typename - name: args c_type: rbs_node_list - - name: location - c_type: rbs_location - name: RBS::Types::Bases::Any fields: - name: todo c_type: bool - - name: location - c_type: rbs_location - name: RBS::Types::Bases::Bool - fields: - - name: location - c_type: rbs_location - name: RBS::Types::Bases::Bottom - fields: - - name: location - c_type: rbs_location - name: RBS::Types::Bases::Class - fields: - - name: location - c_type: rbs_location - name: RBS::Types::Bases::Instance - fields: - - name: location - c_type: rbs_location - name: RBS::Types::Bases::Nil - fields: - - name: location - c_type: rbs_location - name: RBS::Types::Bases::Self - fields: - - name: location - c_type: rbs_location - name: RBS::Types::Bases::Top - fields: - - name: location - c_type: rbs_location - name: RBS::Types::Bases::Void - fields: - - name: location - c_type: rbs_location - name: RBS::Types::Block + expose_location: false fields: - name: type c_type: rbs_node @@ -433,15 +351,12 @@ nodes: c_type: rbs_typename - name: args c_type: rbs_node_list - - name: location - c_type: rbs_location - name: RBS::Types::ClassSingleton fields: - name: name c_type: rbs_typename - - name: location - c_type: rbs_location - name: RBS::Types::Function + expose_location: false fields: - name: required_positionals c_type: rbs_node_list @@ -465,52 +380,39 @@ nodes: c_type: rbs_node - name: name c_type: rbs_ast_symbol - - name: location - c_type: rbs_location - name: RBS::Types::Interface fields: - name: name c_type: rbs_typename - name: args c_type: rbs_node_list - - name: location - c_type: rbs_location - name: RBS::Types::Intersection fields: - name: types c_type: rbs_node_list - - name: location - c_type: rbs_location - name: RBS::Types::Literal fields: - name: literal c_type: rbs_node - - name: location - c_type: rbs_location - name: RBS::Types::Optional fields: - name: type c_type: rbs_node - - name: location - c_type: rbs_location - name: RBS::Types::Proc fields: - name: type c_type: rbs_node - name: block c_type: rbs_types_block - - name: location - c_type: rbs_location - name: self_type c_type: rbs_node - name: RBS::Types::Record fields: - name: all_fields c_type: rbs_hash - - name: location - c_type: rbs_location - name: RBS::Types::Record::FieldType expose_to_ruby: false + expose_location: false fields: - name: type c_type: rbs_node @@ -520,15 +422,12 @@ nodes: fields: - name: types c_type: rbs_node_list - - name: location - c_type: rbs_location - name: RBS::Types::Union fields: - name: types c_type: rbs_node_list - - name: location - c_type: rbs_location - name: RBS::Types::UntypedFunction + expose_location: false fields: - name: return_type c_type: rbs_node @@ -536,5 +435,3 @@ nodes: fields: - name: name c_type: rbs_ast_symbol - - name: location - c_type: rbs_location diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index a423cfb79..04ab3d5cf 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -70,12 +70,11 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan switch (instance->type) { case RBS_AST_ANNOTATION: { - rbs_ast_annotation_t *node = (rbs_ast_annotation_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("string")), rbs_string_to_ruby_string(&node->string, ctx.encoding)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -89,12 +88,11 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan } case RBS_AST_COMMENT: { - rbs_ast_comment_t *node = (rbs_ast_comment_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("string")), rbs_string_to_ruby_string(&node->string, ctx.encoding)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -104,16 +102,15 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_DECLARATIONS_CLASS: { - rbs_ast_declarations_class_t *node = (rbs_ast_declarations_class_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); rb_hash_aset(h, ID2SYM(rb_intern("super_class")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->super_class)); // rbs_ast_declarations_class_super rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_funcall( @@ -130,13 +127,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_DECLARATIONS_CLASS_SUPER: { - rbs_ast_declarations_class_super_t *node = (rbs_ast_declarations_class_super_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -146,13 +142,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_DECLARATIONS_CLASSALIAS: { - rbs_ast_declarations_classalias_t *node = (rbs_ast_declarations_classalias_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -163,13 +158,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_DECLARATIONS_CONSTANT: { - rbs_ast_declarations_constant_t *node = (rbs_ast_declarations_constant_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -180,13 +174,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_DECLARATIONS_GLOBAL: { - rbs_ast_declarations_global_t *node = (rbs_ast_declarations_global_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -197,15 +190,14 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_DECLARATIONS_INTERFACE: { - rbs_ast_declarations_interface_t *node = (rbs_ast_declarations_interface_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_funcall( @@ -222,16 +214,15 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_DECLARATIONS_MODULE: { - rbs_ast_declarations_module_t *node = (rbs_ast_declarations_module_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); rb_hash_aset(h, ID2SYM(rb_intern("self_types")), rbs_node_list_to_ruby_array(ctx, node->self_types)); rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_funcall( @@ -248,13 +239,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_DECLARATIONS_MODULE_SELF: { - rbs_ast_declarations_module_self_t *node = (rbs_ast_declarations_module_self_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -264,13 +254,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_DECLARATIONS_MODULEALIAS: { - rbs_ast_declarations_modulealias_t *node = (rbs_ast_declarations_modulealias_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -281,15 +270,14 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_DECLARATIONS_TYPEALIAS: { - rbs_ast_declarations_typealias_t *node = (rbs_ast_declarations_typealias_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_funcall( @@ -306,12 +294,11 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_DIRECTIVES_USE: { - rbs_ast_directives_use_t *node = (rbs_ast_directives_use_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("clauses")), rbs_node_list_to_ruby_array(ctx, node->clauses)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -321,13 +308,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_DIRECTIVES_USE_SINGLECLAUSE: { - rbs_ast_directives_use_singleclause_t *node = (rbs_ast_directives_use_singleclause_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("type_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type_name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -337,12 +323,11 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE: { - rbs_ast_directives_use_wildcardclause_t *node = (rbs_ast_directives_use_wildcardclause_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->rbs_namespace)); // rbs_namespace - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -361,15 +346,14 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan } case RBS_AST_MEMBERS_ALIAS: { - rbs_ast_members_alias_t *node = (rbs_ast_members_alias_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -380,16 +364,15 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_MEMBERS_ATTRACCESSOR: { - rbs_ast_members_attraccessor_t *node = (rbs_ast_members_attraccessor_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword @@ -401,16 +384,15 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_MEMBERS_ATTRREADER: { - rbs_ast_members_attrreader_t *node = (rbs_ast_members_attrreader_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword @@ -422,16 +404,15 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_MEMBERS_ATTRWRITER: { - rbs_ast_members_attrwriter_t *node = (rbs_ast_members_attrwriter_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("ivar_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->ivar_name)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword @@ -443,13 +424,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE: { - rbs_ast_members_classinstancevariable_t *node = (rbs_ast_members_classinstancevariable_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -460,13 +440,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_MEMBERS_CLASSVARIABLE: { - rbs_ast_members_classvariable_t *node = (rbs_ast_members_classvariable_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -477,14 +456,13 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_MEMBERS_EXTEND: { - rbs_ast_members_extend_t *node = (rbs_ast_members_extend_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -495,14 +473,13 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_MEMBERS_INCLUDE: { - rbs_ast_members_include_t *node = (rbs_ast_members_include_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -513,13 +490,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_MEMBERS_INSTANCEVARIABLE: { - rbs_ast_members_instancevariable_t *node = (rbs_ast_members_instancevariable_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -530,15 +506,14 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_MEMBERS_METHODDEFINITION: { - rbs_ast_members_methoddefinition_t *node = (rbs_ast_members_methoddefinition_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("kind")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->kind)); // rbs_keyword rb_hash_aset(h, ID2SYM(rb_intern("overloads")), rbs_node_list_to_ruby_array(ctx, node->overloads)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_hash_aset(h, ID2SYM(rb_intern("overloading")), node->overloading ? Qtrue : Qfalse); rb_hash_aset(h, ID2SYM(rb_intern("visibility")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->visibility)); // rbs_keyword @@ -551,7 +526,6 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD: { - rbs_ast_members_methoddefinition_overload_t *node = (rbs_ast_members_methoddefinition_overload_t *)instance; VALUE h = rb_hash_new(); @@ -566,14 +540,13 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_MEMBERS_PREPEND: { - rbs_ast_members_prepend_t *node = (rbs_ast_members_prepend_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -584,11 +557,10 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_MEMBERS_PRIVATE: { - rbs_ast_members_private_t *node = (rbs_ast_members_private_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); return CLASS_NEW_INSTANCE( @@ -598,11 +570,10 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_AST_MEMBERS_PUBLIC: { - rbs_ast_members_public_t *node = (rbs_ast_members_public_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); return CLASS_NEW_INSTANCE( @@ -619,16 +590,15 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan } case RBS_AST_TYPEPARAM: { - rbs_ast_typeparam_t *node = (rbs_ast_typeparam_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol rb_hash_aset(h, ID2SYM(rb_intern("variance")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->variance)); // rbs_keyword rb_hash_aset(h, ID2SYM(rb_intern("upper_bound")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->upper_bound)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("default_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->default_type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("unchecked")), node->unchecked ? Qtrue : Qfalse); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -638,14 +608,13 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_METHODTYPE: { - rbs_methodtype_t *node = (rbs_methodtype_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("block")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->block)); // rbs_types_block - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_funcall( RBS_AST_TypeParam, @@ -661,7 +630,6 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_NAMESPACE: { - rbs_namespace_t *node = (rbs_namespace_t *)instance; VALUE h = rb_hash_new(); @@ -684,7 +652,6 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan return array; } case RBS_TYPENAME: { - rbs_typename_t *node = (rbs_typename_t *)instance; VALUE h = rb_hash_new(); @@ -699,13 +666,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_ALIAS: { - rbs_types_alias_t *node = (rbs_types_alias_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -715,12 +681,11 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_BASES_ANY: { - rbs_types_bases_any_t *node = (rbs_types_bases_any_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("todo")), node->todo ? Qtrue : Qfalse); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -730,11 +695,10 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_BASES_BOOL: { - rbs_types_bases_bool_t *node = (rbs_types_bases_bool_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); return CLASS_NEW_INSTANCE( @@ -744,11 +708,10 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_BASES_BOTTOM: { - rbs_types_bases_bottom_t *node = (rbs_types_bases_bottom_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); return CLASS_NEW_INSTANCE( @@ -758,11 +721,10 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_BASES_CLASS: { - rbs_types_bases_class_t *node = (rbs_types_bases_class_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); return CLASS_NEW_INSTANCE( @@ -772,11 +734,10 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_BASES_INSTANCE: { - rbs_types_bases_instance_t *node = (rbs_types_bases_instance_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); return CLASS_NEW_INSTANCE( @@ -786,11 +747,10 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_BASES_NIL: { - rbs_types_bases_nil_t *node = (rbs_types_bases_nil_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); return CLASS_NEW_INSTANCE( @@ -800,11 +760,10 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_BASES_SELF: { - rbs_types_bases_self_t *node = (rbs_types_bases_self_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); return CLASS_NEW_INSTANCE( @@ -814,11 +773,10 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_BASES_TOP: { - rbs_types_bases_top_t *node = (rbs_types_bases_top_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); return CLASS_NEW_INSTANCE( @@ -828,11 +786,10 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_BASES_VOID: { - rbs_types_bases_void_t *node = (rbs_types_bases_void_t *)instance; VALUE h = rb_hash_new(); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); return CLASS_NEW_INSTANCE( @@ -842,7 +799,6 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_BLOCK: { - rbs_types_block_t *node = (rbs_types_block_t *)instance; VALUE h = rb_hash_new(); @@ -858,13 +814,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_CLASSINSTANCE: { - rbs_types_classinstance_t *node = (rbs_types_classinstance_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -874,12 +829,11 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_CLASSSINGLETON: { - rbs_types_classsingleton_t *node = (rbs_types_classsingleton_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -889,7 +843,6 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_FUNCTION: { - rbs_types_function_t *node = (rbs_types_function_t *)instance; VALUE h = rb_hash_new(); @@ -910,13 +863,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_FUNCTION_PARAM: { - rbs_types_function_param_t *node = (rbs_types_function_param_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -926,13 +878,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_INTERFACE: { - rbs_types_interface_t *node = (rbs_types_interface_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -942,12 +893,11 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_INTERSECTION: { - rbs_types_intersection_t *node = (rbs_types_intersection_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(ctx, node->types)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -957,12 +907,11 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_LITERAL: { - rbs_types_literal_t *node = (rbs_types_literal_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("literal")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->literal)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -972,12 +921,11 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_OPTIONAL: { - rbs_types_optional_t *node = (rbs_types_optional_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -987,13 +935,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_PROC: { - rbs_types_proc_t *node = (rbs_types_proc_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("block")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->block)); // rbs_types_block - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); rb_hash_aset(h, ID2SYM(rb_intern("self_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->self_type)); // rbs_node @@ -1004,12 +951,11 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_RECORD: { - rbs_types_record_t *node = (rbs_types_record_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("all_fields")), rbs_hash_to_ruby_hash(ctx, node->all_fields)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -1028,12 +974,11 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan } case RBS_TYPES_TUPLE: { - rbs_types_tuple_t *node = (rbs_types_tuple_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(ctx, node->types)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -1043,12 +988,11 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_UNION: { - rbs_types_union_t *node = (rbs_types_union_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("types")), rbs_node_list_to_ruby_array(ctx, node->types)); - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( @@ -1058,7 +1002,6 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_UNTYPEDFUNCTION: { - rbs_types_untypedfunction_t *node = (rbs_types_untypedfunction_t *)instance; VALUE h = rb_hash_new(); @@ -1072,12 +1015,11 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan ); } case RBS_TYPES_VARIABLE: { - rbs_types_variable_t *node = (rbs_types_variable_t *)instance; VALUE h = rb_hash_new(); + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_ast_symbol - rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->location)); return CLASS_NEW_INSTANCE( diff --git a/include/rbs/ast.h b/include/rbs/ast.h index 4dccf9bd4..2d94df560 100644 --- a/include/rbs/ast.h +++ b/include/rbs/ast.h @@ -83,6 +83,7 @@ enum rbs_node_type { typedef struct rbs_node { enum rbs_node_type type; + rbs_location_t *location; } rbs_node_t; const char* rbs_node_type_name(rbs_node_t *node); @@ -134,7 +135,6 @@ typedef struct rbs_ast_annotation { rbs_node_t base; rbs_string_t string; - struct rbs_location *location; } rbs_ast_annotation_t; typedef struct rbs_ast_bool { @@ -147,7 +147,6 @@ typedef struct rbs_ast_comment { rbs_node_t base; rbs_string_t string; - struct rbs_location *location; } rbs_ast_comment_t; typedef struct rbs_ast_declarations_class { @@ -158,7 +157,6 @@ typedef struct rbs_ast_declarations_class { struct rbs_ast_declarations_class_super *super_class; struct rbs_node_list *members; struct rbs_node_list *annotations; - struct rbs_location *location; struct rbs_ast_comment *comment; } rbs_ast_declarations_class_t; @@ -167,7 +165,6 @@ typedef struct rbs_ast_declarations_class_super { struct rbs_typename *name; struct rbs_node_list *args; - struct rbs_location *location; } rbs_ast_declarations_class_super_t; typedef struct rbs_ast_declarations_classalias { @@ -175,7 +172,6 @@ typedef struct rbs_ast_declarations_classalias { struct rbs_typename *new_name; struct rbs_typename *old_name; - struct rbs_location *location; struct rbs_ast_comment *comment; } rbs_ast_declarations_classalias_t; @@ -184,7 +180,6 @@ typedef struct rbs_ast_declarations_constant { struct rbs_typename *name; struct rbs_node *type; - struct rbs_location *location; struct rbs_ast_comment *comment; } rbs_ast_declarations_constant_t; @@ -193,7 +188,6 @@ typedef struct rbs_ast_declarations_global { struct rbs_ast_symbol *name; struct rbs_node *type; - struct rbs_location *location; struct rbs_ast_comment *comment; } rbs_ast_declarations_global_t; @@ -204,7 +198,6 @@ typedef struct rbs_ast_declarations_interface { struct rbs_node_list *type_params; struct rbs_node_list *members; struct rbs_node_list *annotations; - struct rbs_location *location; struct rbs_ast_comment *comment; } rbs_ast_declarations_interface_t; @@ -216,7 +209,6 @@ typedef struct rbs_ast_declarations_module { struct rbs_node_list *self_types; struct rbs_node_list *members; struct rbs_node_list *annotations; - struct rbs_location *location; struct rbs_ast_comment *comment; } rbs_ast_declarations_module_t; @@ -225,7 +217,6 @@ typedef struct rbs_ast_declarations_module_self { struct rbs_typename *name; struct rbs_node_list *args; - struct rbs_location *location; } rbs_ast_declarations_module_self_t; typedef struct rbs_ast_declarations_modulealias { @@ -233,7 +224,6 @@ typedef struct rbs_ast_declarations_modulealias { struct rbs_typename *new_name; struct rbs_typename *old_name; - struct rbs_location *location; struct rbs_ast_comment *comment; } rbs_ast_declarations_modulealias_t; @@ -244,7 +234,6 @@ typedef struct rbs_ast_declarations_typealias { struct rbs_node_list *type_params; struct rbs_node *type; struct rbs_node_list *annotations; - struct rbs_location *location; struct rbs_ast_comment *comment; } rbs_ast_declarations_typealias_t; @@ -252,7 +241,6 @@ typedef struct rbs_ast_directives_use { rbs_node_t base; struct rbs_node_list *clauses; - struct rbs_location *location; } rbs_ast_directives_use_t; typedef struct rbs_ast_directives_use_singleclause { @@ -260,14 +248,12 @@ typedef struct rbs_ast_directives_use_singleclause { struct rbs_typename *type_name; struct rbs_ast_symbol *new_name; - struct rbs_location *location; } rbs_ast_directives_use_singleclause_t; typedef struct rbs_ast_directives_use_wildcardclause { rbs_node_t base; struct rbs_namespace *rbs_namespace; - struct rbs_location *location; } rbs_ast_directives_use_wildcardclause_t; typedef struct rbs_ast_integer { @@ -283,7 +269,6 @@ typedef struct rbs_ast_members_alias { struct rbs_ast_symbol *old_name; struct rbs_keyword *kind; struct rbs_node_list *annotations; - struct rbs_location *location; struct rbs_ast_comment *comment; } rbs_ast_members_alias_t; @@ -295,7 +280,6 @@ typedef struct rbs_ast_members_attraccessor { struct rbs_node *ivar_name; struct rbs_keyword *kind; struct rbs_node_list *annotations; - struct rbs_location *location; struct rbs_ast_comment *comment; struct rbs_keyword *visibility; } rbs_ast_members_attraccessor_t; @@ -308,7 +292,6 @@ typedef struct rbs_ast_members_attrreader { struct rbs_node *ivar_name; struct rbs_keyword *kind; struct rbs_node_list *annotations; - struct rbs_location *location; struct rbs_ast_comment *comment; struct rbs_keyword *visibility; } rbs_ast_members_attrreader_t; @@ -321,7 +304,6 @@ typedef struct rbs_ast_members_attrwriter { struct rbs_node *ivar_name; struct rbs_keyword *kind; struct rbs_node_list *annotations; - struct rbs_location *location; struct rbs_ast_comment *comment; struct rbs_keyword *visibility; } rbs_ast_members_attrwriter_t; @@ -331,7 +313,6 @@ typedef struct rbs_ast_members_classinstancevariable { struct rbs_ast_symbol *name; struct rbs_node *type; - struct rbs_location *location; struct rbs_ast_comment *comment; } rbs_ast_members_classinstancevariable_t; @@ -340,7 +321,6 @@ typedef struct rbs_ast_members_classvariable { struct rbs_ast_symbol *name; struct rbs_node *type; - struct rbs_location *location; struct rbs_ast_comment *comment; } rbs_ast_members_classvariable_t; @@ -350,7 +330,6 @@ typedef struct rbs_ast_members_extend { struct rbs_typename *name; struct rbs_node_list *args; struct rbs_node_list *annotations; - struct rbs_location *location; struct rbs_ast_comment *comment; } rbs_ast_members_extend_t; @@ -360,7 +339,6 @@ typedef struct rbs_ast_members_include { struct rbs_typename *name; struct rbs_node_list *args; struct rbs_node_list *annotations; - struct rbs_location *location; struct rbs_ast_comment *comment; } rbs_ast_members_include_t; @@ -369,7 +347,6 @@ typedef struct rbs_ast_members_instancevariable { struct rbs_ast_symbol *name; struct rbs_node *type; - struct rbs_location *location; struct rbs_ast_comment *comment; } rbs_ast_members_instancevariable_t; @@ -380,7 +357,6 @@ typedef struct rbs_ast_members_methoddefinition { struct rbs_keyword *kind; struct rbs_node_list *overloads; struct rbs_node_list *annotations; - struct rbs_location *location; struct rbs_ast_comment *comment; bool overloading; struct rbs_keyword *visibility; @@ -399,20 +375,17 @@ typedef struct rbs_ast_members_prepend { struct rbs_typename *name; struct rbs_node_list *args; struct rbs_node_list *annotations; - struct rbs_location *location; struct rbs_ast_comment *comment; } rbs_ast_members_prepend_t; typedef struct rbs_ast_members_private { rbs_node_t base; - struct rbs_location *location; } rbs_ast_members_private_t; typedef struct rbs_ast_members_public { rbs_node_t base; - struct rbs_location *location; } rbs_ast_members_public_t; typedef struct rbs_ast_string { @@ -429,7 +402,6 @@ typedef struct rbs_ast_typeparam { struct rbs_node *upper_bound; struct rbs_node *default_type; bool unchecked; - struct rbs_location *location; } rbs_ast_typeparam_t; typedef struct rbs_methodtype { @@ -438,7 +410,6 @@ typedef struct rbs_methodtype { struct rbs_node_list *type_params; struct rbs_node *type; struct rbs_types_block *block; - struct rbs_location *location; } rbs_methodtype_t; typedef struct rbs_namespace { @@ -467,62 +438,52 @@ typedef struct rbs_types_alias { struct rbs_typename *name; struct rbs_node_list *args; - struct rbs_location *location; } rbs_types_alias_t; typedef struct rbs_types_bases_any { rbs_node_t base; bool todo; - struct rbs_location *location; } rbs_types_bases_any_t; typedef struct rbs_types_bases_bool { rbs_node_t base; - struct rbs_location *location; } rbs_types_bases_bool_t; typedef struct rbs_types_bases_bottom { rbs_node_t base; - struct rbs_location *location; } rbs_types_bases_bottom_t; typedef struct rbs_types_bases_class { rbs_node_t base; - struct rbs_location *location; } rbs_types_bases_class_t; typedef struct rbs_types_bases_instance { rbs_node_t base; - struct rbs_location *location; } rbs_types_bases_instance_t; typedef struct rbs_types_bases_nil { rbs_node_t base; - struct rbs_location *location; } rbs_types_bases_nil_t; typedef struct rbs_types_bases_self { rbs_node_t base; - struct rbs_location *location; } rbs_types_bases_self_t; typedef struct rbs_types_bases_top { rbs_node_t base; - struct rbs_location *location; } rbs_types_bases_top_t; typedef struct rbs_types_bases_void { rbs_node_t base; - struct rbs_location *location; } rbs_types_bases_void_t; typedef struct rbs_types_block { @@ -538,14 +499,12 @@ typedef struct rbs_types_classinstance { struct rbs_typename *name; struct rbs_node_list *args; - struct rbs_location *location; } rbs_types_classinstance_t; typedef struct rbs_types_classsingleton { rbs_node_t base; struct rbs_typename *name; - struct rbs_location *location; } rbs_types_classsingleton_t; typedef struct rbs_types_function { @@ -566,7 +525,6 @@ typedef struct rbs_types_function_param { struct rbs_node *type; struct rbs_ast_symbol *name; - struct rbs_location *location; } rbs_types_function_param_t; typedef struct rbs_types_interface { @@ -574,28 +532,24 @@ typedef struct rbs_types_interface { struct rbs_typename *name; struct rbs_node_list *args; - struct rbs_location *location; } rbs_types_interface_t; typedef struct rbs_types_intersection { rbs_node_t base; struct rbs_node_list *types; - struct rbs_location *location; } rbs_types_intersection_t; typedef struct rbs_types_literal { rbs_node_t base; struct rbs_node *literal; - struct rbs_location *location; } rbs_types_literal_t; typedef struct rbs_types_optional { rbs_node_t base; struct rbs_node *type; - struct rbs_location *location; } rbs_types_optional_t; typedef struct rbs_types_proc { @@ -603,7 +557,6 @@ typedef struct rbs_types_proc { struct rbs_node *type; struct rbs_types_block *block; - struct rbs_location *location; struct rbs_node *self_type; } rbs_types_proc_t; @@ -611,7 +564,6 @@ typedef struct rbs_types_record { rbs_node_t base; struct rbs_hash *all_fields; - struct rbs_location *location; } rbs_types_record_t; typedef struct rbs_types_record_fieldtype { @@ -625,14 +577,12 @@ typedef struct rbs_types_tuple { rbs_node_t base; struct rbs_node_list *types; - struct rbs_location *location; } rbs_types_tuple_t; typedef struct rbs_types_union { rbs_node_t base; struct rbs_node_list *types; - struct rbs_location *location; } rbs_types_union_t; typedef struct rbs_types_untypedfunction { @@ -645,7 +595,6 @@ typedef struct rbs_types_variable { rbs_node_t base; struct rbs_ast_symbol *name; - struct rbs_location *location; } rbs_types_variable_t; @@ -657,7 +606,7 @@ typedef struct rbs_keyword { rbs_constant_id_t constant_id; } rbs_keyword_t; -rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *, rbs_constant_id_t); +rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *, rbs_location_t *, rbs_constant_id_t); /// `rbs_ast_symbol_t` models user-defined identifiers like class names, method names, etc. /// These get stored in the parser's own constant pool, and get surfaced to Ruby as `Symbol`s. @@ -666,47 +615,47 @@ typedef struct rbs_ast_symbol { rbs_constant_id_t constant_id; } rbs_ast_symbol_t; -rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *, rbs_constant_pool_t *, rbs_constant_id_t); - -rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_string_t string, rbs_location_t *location); -rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, bool value); -rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, rbs_string_t string, rbs_location_t *location); -rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); -rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); -rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, rbs_ast_comment_t *comment); -rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment); -rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment); -rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); -rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); -rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); -rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, rbs_ast_comment_t *comment); -rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); -rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, rbs_node_list_t *clauses, rbs_location_t *location); -rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, rbs_typename_t *type_name, rbs_ast_symbol_t *new_name, rbs_location_t *location); -rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_namespace_t *rbs_namespace, rbs_location_t *location); -rbs_ast_integer_t *rbs_ast_integer_new(rbs_allocator_t *allocator, rbs_string_t string_representation); -rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); -rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); -rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); -rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); -rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment); -rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment); -rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); -rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); -rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment); -rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_keyword_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, bool overloading, rbs_keyword_t *visibility); -rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, rbs_node_list_t *annotations, rbs_node_t *method_type); -rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment); +rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *, rbs_location_t *, rbs_constant_pool_t *, rbs_constant_id_t); + +rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string); +rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, rbs_location_t *location, bool value); +rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string); +rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); +rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args); +rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_ast_comment_t *comment); +rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_t *type, rbs_ast_comment_t *comment); +rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment); +rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); +rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); +rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args); +rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_ast_comment_t *comment); +rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); +rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *clauses); +rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *type_name, rbs_ast_symbol_t *new_name); +rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace); +rbs_ast_integer_t *rbs_ast_integer_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string_representation); +rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); +rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); +rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); +rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); +rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment); +rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment); +rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); +rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); +rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment); +rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, bool overloading, rbs_keyword_t *visibility); +rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *annotations, rbs_node_t *method_type); +rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocator, rbs_location_t *location); rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, rbs_location_t *location); -rbs_ast_string_t *rbs_ast_string_new(rbs_allocator_t *allocator, rbs_string_t string); -rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_keyword_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked, rbs_location_t *location); -rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location); -rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_node_list_t *path, bool absolute); -rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_node_list_t *directives, rbs_node_list_t *declarations); -rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_namespace_t *rbs_namespace, rbs_ast_symbol_t *name); -rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); -rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, bool todo, rbs_location_t *location); +rbs_ast_string_t *rbs_ast_string_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string); +rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked); +rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block); +rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *path, bool absolute); +rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *directives, rbs_node_list_t *declarations); +rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace, rbs_ast_symbol_t *name); +rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args); +rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, rbs_location_t *location, bool todo); rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, rbs_location_t *location); rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, rbs_location_t *location); rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, rbs_location_t *location); @@ -715,21 +664,21 @@ rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, rbs_l rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, rbs_location_t *location); rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, rbs_location_t *location); rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, rbs_location_t *location); -rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_node_t *type, bool required, rbs_node_t *self_type); -rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); -rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_location_t *location); -rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, rbs_node_list_t *required_positionals, rbs_node_list_t *optional_positionals, rbs_node_t *rest_positionals, rbs_node_list_t *trailing_positionals, rbs_hash_t *required_keywords, rbs_hash_t *optional_keywords, rbs_node_t *rest_keywords, rbs_node_t *return_type); -rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_ast_symbol_t *name, rbs_location_t *location); -rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location); -rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location); -rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, rbs_node_t *literal, rbs_location_t *location); -rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_location_t *location); -rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location, rbs_node_t *self_type); -rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, rbs_hash_t *all_fields, rbs_location_t *location); -rbs_types_record_fieldtype_t *rbs_types_record_fieldtype_new(rbs_allocator_t *allocator, rbs_node_t *type, bool required); -rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location); -rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location); -rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, rbs_node_t *return_type); -rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_location_t *location); +rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, bool required, rbs_node_t *self_type); +rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args); +rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name); +rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *required_positionals, rbs_node_list_t *optional_positionals, rbs_node_t *rest_positionals, rbs_node_list_t *trailing_positionals, rbs_hash_t *required_keywords, rbs_hash_t *optional_keywords, rbs_node_t *rest_keywords, rbs_node_t *return_type); +rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, rbs_ast_symbol_t *name); +rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args); +rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types); +rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *literal); +rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type); +rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, rbs_types_block_t *block, rbs_node_t *self_type); +rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_hash_t *all_fields); +rbs_types_record_fieldtype_t *rbs_types_record_fieldtype_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, bool required); +rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types); +rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types); +rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *return_type); +rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name); #endif diff --git a/src/ast.c b/src/ast.c index 2245fa881..91447fc73 100644 --- a/src/ast.c +++ b/src/ast.c @@ -186,12 +186,13 @@ rbs_node_t* rbs_hash_get(rbs_hash_t *hash, rbs_node_t *key) { return node ? node->value : NULL; } -rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *allocator, rbs_constant_id_t constant_id) { +rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_constant_id_t constant_id) { rbs_keyword_t *instance = rbs_allocator_alloc(allocator, rbs_keyword_t); *instance = (rbs_keyword_t) { .base = (rbs_node_t) { .type = RBS_KEYWORD, + .location = location, }, .constant_id = constant_id, }; @@ -199,12 +200,13 @@ rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *allocator, rbs_constant_id_t con return instance; } -rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_constant_pool_t *constant_pool, rbs_constant_id_t constant_id) { +rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_constant_pool_t *constant_pool, rbs_constant_id_t constant_id) { rbs_ast_symbol_t *instance = rbs_allocator_alloc(allocator, rbs_ast_symbol_t); *instance = (rbs_ast_symbol_t) { .base = (rbs_node_t) { .type = RBS_AST_SYMBOL, + .location = location, }, .constant_id = constant_id, }; @@ -212,28 +214,29 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_constant_po return instance; } -rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_string_t string, rbs_location_t *location) { +rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string) { rbs_ast_annotation_t *instance = rbs_allocator_alloc(allocator, rbs_ast_annotation_t); *instance = (rbs_ast_annotation_t) { .base = (rbs_node_t) { - .type = RBS_AST_ANNOTATION + .type = RBS_AST_ANNOTATION, + .location = location, }, .string = string, - .location = location, }; return instance; } -rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, bool value) { +rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, rbs_location_t *location, bool value) { rbs_ast_bool_t *instance = rbs_allocator_alloc(allocator, rbs_ast_bool_t); *instance = (rbs_ast_bool_t) { .base = (rbs_node_t) { - .type = RBS_AST_BOOL + .type = RBS_AST_BOOL, + .location = location, }, .value = value, }; @@ -241,252 +244,253 @@ rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, bool value) { return instance; } -rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, rbs_string_t string, rbs_location_t *location) { +rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string) { rbs_ast_comment_t *instance = rbs_allocator_alloc(allocator, rbs_ast_comment_t); *instance = (rbs_ast_comment_t) { .base = (rbs_node_t) { - .type = RBS_AST_COMMENT + .type = RBS_AST_COMMENT, + .location = location, }, .string = string, - .location = location, }; return instance; } -rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { +rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_class_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_t); *instance = (rbs_ast_declarations_class_t) { .base = (rbs_node_t) { - .type = RBS_AST_DECLARATIONS_CLASS + .type = RBS_AST_DECLARATIONS_CLASS, + .location = location, }, .name = name, .type_params = type_params, .super_class = super_class, .members = members, .annotations = annotations, - .location = location, .comment = comment, }; return instance; } -rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { +rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { rbs_ast_declarations_class_super_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_super_t); *instance = (rbs_ast_declarations_class_super_t) { .base = (rbs_node_t) { - .type = RBS_AST_DECLARATIONS_CLASS_SUPER + .type = RBS_AST_DECLARATIONS_CLASS_SUPER, + .location = location, }, .name = name, .args = args, - .location = location, }; return instance; } -rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, rbs_ast_comment_t *comment) { +rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_ast_comment_t *comment) { rbs_ast_declarations_classalias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_classalias_t); *instance = (rbs_ast_declarations_classalias_t) { .base = (rbs_node_t) { - .type = RBS_AST_DECLARATIONS_CLASSALIAS + .type = RBS_AST_DECLARATIONS_CLASSALIAS, + .location = location, }, .new_name = new_name, .old_name = old_name, - .location = location, .comment = comment, }; return instance; } -rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment) { +rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_declarations_constant_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_constant_t); *instance = (rbs_ast_declarations_constant_t) { .base = (rbs_node_t) { - .type = RBS_AST_DECLARATIONS_CONSTANT + .type = RBS_AST_DECLARATIONS_CONSTANT, + .location = location, }, .name = name, .type = type, - .location = location, .comment = comment, }; return instance; } -rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment) { +rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_declarations_global_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_global_t); *instance = (rbs_ast_declarations_global_t) { .base = (rbs_node_t) { - .type = RBS_AST_DECLARATIONS_GLOBAL + .type = RBS_AST_DECLARATIONS_GLOBAL, + .location = location, }, .name = name, .type = type, - .location = location, .comment = comment, }; return instance; } -rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { +rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_interface_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_interface_t); *instance = (rbs_ast_declarations_interface_t) { .base = (rbs_node_t) { - .type = RBS_AST_DECLARATIONS_INTERFACE + .type = RBS_AST_DECLARATIONS_INTERFACE, + .location = location, }, .name = name, .type_params = type_params, .members = members, .annotations = annotations, - .location = location, .comment = comment, }; return instance; } -rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { +rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_module_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_t); *instance = (rbs_ast_declarations_module_t) { .base = (rbs_node_t) { - .type = RBS_AST_DECLARATIONS_MODULE + .type = RBS_AST_DECLARATIONS_MODULE, + .location = location, }, .name = name, .type_params = type_params, .self_types = self_types, .members = members, .annotations = annotations, - .location = location, .comment = comment, }; return instance; } -rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { +rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { rbs_ast_declarations_module_self_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_self_t); *instance = (rbs_ast_declarations_module_self_t) { .base = (rbs_node_t) { - .type = RBS_AST_DECLARATIONS_MODULE_SELF + .type = RBS_AST_DECLARATIONS_MODULE_SELF, + .location = location, }, .name = name, .args = args, - .location = location, }; return instance; } -rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_location_t *location, rbs_ast_comment_t *comment) { +rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_ast_comment_t *comment) { rbs_ast_declarations_modulealias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_modulealias_t); *instance = (rbs_ast_declarations_modulealias_t) { .base = (rbs_node_t) { - .type = RBS_AST_DECLARATIONS_MODULEALIAS + .type = RBS_AST_DECLARATIONS_MODULEALIAS, + .location = location, }, .new_name = new_name, .old_name = old_name, - .location = location, .comment = comment, }; return instance; } -rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { +rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_typealias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_typealias_t); *instance = (rbs_ast_declarations_typealias_t) { .base = (rbs_node_t) { - .type = RBS_AST_DECLARATIONS_TYPEALIAS + .type = RBS_AST_DECLARATIONS_TYPEALIAS, + .location = location, }, .name = name, .type_params = type_params, .type = type, .annotations = annotations, - .location = location, .comment = comment, }; return instance; } -rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, rbs_node_list_t *clauses, rbs_location_t *location) { +rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *clauses) { rbs_ast_directives_use_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_t); *instance = (rbs_ast_directives_use_t) { .base = (rbs_node_t) { - .type = RBS_AST_DIRECTIVES_USE + .type = RBS_AST_DIRECTIVES_USE, + .location = location, }, .clauses = clauses, - .location = location, }; return instance; } -rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, rbs_typename_t *type_name, rbs_ast_symbol_t *new_name, rbs_location_t *location) { +rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *type_name, rbs_ast_symbol_t *new_name) { rbs_ast_directives_use_singleclause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_singleclause_t); *instance = (rbs_ast_directives_use_singleclause_t) { .base = (rbs_node_t) { - .type = RBS_AST_DIRECTIVES_USE_SINGLECLAUSE + .type = RBS_AST_DIRECTIVES_USE_SINGLECLAUSE, + .location = location, }, .type_name = type_name, .new_name = new_name, - .location = location, }; return instance; } -rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_namespace_t *rbs_namespace, rbs_location_t *location) { +rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace) { rbs_ast_directives_use_wildcardclause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_wildcardclause_t); *instance = (rbs_ast_directives_use_wildcardclause_t) { .base = (rbs_node_t) { - .type = RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE + .type = RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE, + .location = location, }, .rbs_namespace = rbs_namespace, - .location = location, }; return instance; } -rbs_ast_integer_t *rbs_ast_integer_new(rbs_allocator_t *allocator, rbs_string_t string_representation) { +rbs_ast_integer_t *rbs_ast_integer_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string_representation) { rbs_ast_integer_t *instance = rbs_allocator_alloc(allocator, rbs_ast_integer_t); *instance = (rbs_ast_integer_t) { .base = (rbs_node_t) { - .type = RBS_AST_INTEGER + .type = RBS_AST_INTEGER, + .location = location, }, .string_representation = string_representation, }; @@ -494,39 +498,39 @@ rbs_ast_integer_t *rbs_ast_integer_new(rbs_allocator_t *allocator, rbs_string_t return instance; } -rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { +rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_alias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_alias_t); *instance = (rbs_ast_members_alias_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_ALIAS + .type = RBS_AST_MEMBERS_ALIAS, + .location = location, }, .new_name = new_name, .old_name = old_name, .kind = kind, .annotations = annotations, - .location = location, .comment = comment, }; return instance; } -rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { +rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { rbs_ast_members_attraccessor_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attraccessor_t); *instance = (rbs_ast_members_attraccessor_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_ATTRACCESSOR + .type = RBS_AST_MEMBERS_ATTRACCESSOR, + .location = location, }, .name = name, .type = type, .ivar_name = ivar_name, .kind = kind, .annotations = annotations, - .location = location, .comment = comment, .visibility = visibility, }; @@ -534,20 +538,20 @@ rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t return instance; } -rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { +rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { rbs_ast_members_attrreader_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attrreader_t); *instance = (rbs_ast_members_attrreader_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_ATTRREADER + .type = RBS_AST_MEMBERS_ATTRREADER, + .location = location, }, .name = name, .type = type, .ivar_name = ivar_name, .kind = kind, .annotations = annotations, - .location = location, .comment = comment, .visibility = visibility, }; @@ -555,20 +559,20 @@ rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *al return instance; } -rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { +rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { rbs_ast_members_attrwriter_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attrwriter_t); *instance = (rbs_ast_members_attrwriter_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_ATTRWRITER + .type = RBS_AST_MEMBERS_ATTRWRITER, + .location = location, }, .name = name, .type = type, .ivar_name = ivar_name, .kind = kind, .annotations = annotations, - .location = location, .comment = comment, .visibility = visibility, }; @@ -576,106 +580,106 @@ rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *al return instance; } -rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment) { +rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_members_classinstancevariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_classinstancevariable_t); *instance = (rbs_ast_members_classinstancevariable_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE + .type = RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE, + .location = location, }, .name = name, .type = type, - .location = location, .comment = comment, }; return instance; } -rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment) { +rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_members_classvariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_classvariable_t); *instance = (rbs_ast_members_classvariable_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_CLASSVARIABLE + .type = RBS_AST_MEMBERS_CLASSVARIABLE, + .location = location, }, .name = name, .type = type, - .location = location, .comment = comment, }; return instance; } -rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { +rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_extend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_extend_t); *instance = (rbs_ast_members_extend_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_EXTEND + .type = RBS_AST_MEMBERS_EXTEND, + .location = location, }, .name = name, .args = args, .annotations = annotations, - .location = location, .comment = comment, }; return instance; } -rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { +rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_include_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_include_t); *instance = (rbs_ast_members_include_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_INCLUDE + .type = RBS_AST_MEMBERS_INCLUDE, + .location = location, }, .name = name, .args = args, .annotations = annotations, - .location = location, .comment = comment, }; return instance; } -rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_location_t *location, rbs_ast_comment_t *comment) { +rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_members_instancevariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_instancevariable_t); *instance = (rbs_ast_members_instancevariable_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_INSTANCEVARIABLE + .type = RBS_AST_MEMBERS_INSTANCEVARIABLE, + .location = location, }, .name = name, .type = type, - .location = location, .comment = comment, }; return instance; } -rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_keyword_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment, bool overloading, rbs_keyword_t *visibility) { +rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, bool overloading, rbs_keyword_t *visibility) { rbs_ast_members_methoddefinition_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_methoddefinition_t); *instance = (rbs_ast_members_methoddefinition_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_METHODDEFINITION + .type = RBS_AST_MEMBERS_METHODDEFINITION, + .location = location, }, .name = name, .kind = kind, .overloads = overloads, .annotations = annotations, - .location = location, .comment = comment, .overloading = overloading, .visibility = visibility, @@ -684,13 +688,14 @@ rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_all return instance; } -rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, rbs_node_list_t *annotations, rbs_node_t *method_type) { +rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *annotations, rbs_node_t *method_type) { rbs_ast_members_methoddefinition_overload_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_methoddefinition_overload_t); *instance = (rbs_ast_members_methoddefinition_overload_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD + .type = RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD, + .location = location, }, .annotations = annotations, .method_type = method_type, @@ -699,18 +704,18 @@ rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_ov return instance; } -rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_location_t *location, rbs_ast_comment_t *comment) { +rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_prepend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_prepend_t); *instance = (rbs_ast_members_prepend_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_PREPEND + .type = RBS_AST_MEMBERS_PREPEND, + .location = location, }, .name = name, .args = args, .annotations = annotations, - .location = location, .comment = comment, }; @@ -723,9 +728,9 @@ rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocato *instance = (rbs_ast_members_private_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_PRIVATE + .type = RBS_AST_MEMBERS_PRIVATE, + .location = location, }, - .location = location, }; return instance; @@ -737,21 +742,22 @@ rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, *instance = (rbs_ast_members_public_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_PUBLIC + .type = RBS_AST_MEMBERS_PUBLIC, + .location = location, }, - .location = location, }; return instance; } -rbs_ast_string_t *rbs_ast_string_new(rbs_allocator_t *allocator, rbs_string_t string) { +rbs_ast_string_t *rbs_ast_string_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string) { rbs_ast_string_t *instance = rbs_allocator_alloc(allocator, rbs_ast_string_t); *instance = (rbs_ast_string_t) { .base = (rbs_node_t) { - .type = RBS_AST_STRING + .type = RBS_AST_STRING, + .location = location, }, .string = string, }; @@ -759,49 +765,50 @@ rbs_ast_string_t *rbs_ast_string_new(rbs_allocator_t *allocator, rbs_string_t st return instance; } -rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_keyword_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked, rbs_location_t *location) { +rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked) { rbs_ast_typeparam_t *instance = rbs_allocator_alloc(allocator, rbs_ast_typeparam_t); *instance = (rbs_ast_typeparam_t) { .base = (rbs_node_t) { - .type = RBS_AST_TYPEPARAM + .type = RBS_AST_TYPEPARAM, + .location = location, }, .name = name, .variance = variance, .upper_bound = upper_bound, .default_type = default_type, .unchecked = unchecked, - .location = location, }; return instance; } -rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location) { +rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block) { rbs_methodtype_t *instance = rbs_allocator_alloc(allocator, rbs_methodtype_t); *instance = (rbs_methodtype_t) { .base = (rbs_node_t) { - .type = RBS_METHODTYPE + .type = RBS_METHODTYPE, + .location = location, }, .type_params = type_params, .type = type, .block = block, - .location = location, }; return instance; } -rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_node_list_t *path, bool absolute) { +rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *path, bool absolute) { rbs_namespace_t *instance = rbs_allocator_alloc(allocator, rbs_namespace_t); *instance = (rbs_namespace_t) { .base = (rbs_node_t) { - .type = RBS_NAMESPACE + .type = RBS_NAMESPACE, + .location = location, }, .path = path, .absolute = absolute, @@ -810,13 +817,14 @@ rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_node_list_t * return instance; } -rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_node_list_t *directives, rbs_node_list_t *declarations) { +rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *directives, rbs_node_list_t *declarations) { rbs_signature_t *instance = rbs_allocator_alloc(allocator, rbs_signature_t); *instance = (rbs_signature_t) { .base = (rbs_node_t) { - .type = RBS_SIGNATURE + .type = RBS_SIGNATURE, + .location = location, }, .directives = directives, .declarations = declarations, @@ -825,13 +833,14 @@ rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_node_list_t * return instance; } -rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_namespace_t *rbs_namespace, rbs_ast_symbol_t *name) { +rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace, rbs_ast_symbol_t *name) { rbs_typename_t *instance = rbs_allocator_alloc(allocator, rbs_typename_t); *instance = (rbs_typename_t) { .base = (rbs_node_t) { - .type = RBS_TYPENAME + .type = RBS_TYPENAME, + .location = location, }, .rbs_namespace = rbs_namespace, .name = name, @@ -840,32 +849,32 @@ rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_namespace_t *rb return instance; } -rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { +rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { rbs_types_alias_t *instance = rbs_allocator_alloc(allocator, rbs_types_alias_t); *instance = (rbs_types_alias_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_ALIAS + .type = RBS_TYPES_ALIAS, + .location = location, }, .name = name, .args = args, - .location = location, }; return instance; } -rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, bool todo, rbs_location_t *location) { +rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, rbs_location_t *location, bool todo) { rbs_types_bases_any_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_any_t); *instance = (rbs_types_bases_any_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_BASES_ANY + .type = RBS_TYPES_BASES_ANY, + .location = location, }, .todo = todo, - .location = location, }; return instance; @@ -877,9 +886,9 @@ rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, rbs *instance = (rbs_types_bases_bool_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_BASES_BOOL + .type = RBS_TYPES_BASES_BOOL, + .location = location, }, - .location = location, }; return instance; @@ -891,9 +900,9 @@ rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, *instance = (rbs_types_bases_bottom_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_BASES_BOTTOM + .type = RBS_TYPES_BASES_BOTTOM, + .location = location, }, - .location = location, }; return instance; @@ -905,9 +914,9 @@ rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, r *instance = (rbs_types_bases_class_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_BASES_CLASS + .type = RBS_TYPES_BASES_CLASS, + .location = location, }, - .location = location, }; return instance; @@ -919,9 +928,9 @@ rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *alloca *instance = (rbs_types_bases_instance_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_BASES_INSTANCE + .type = RBS_TYPES_BASES_INSTANCE, + .location = location, }, - .location = location, }; return instance; @@ -933,9 +942,9 @@ rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, rbs_l *instance = (rbs_types_bases_nil_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_BASES_NIL + .type = RBS_TYPES_BASES_NIL, + .location = location, }, - .location = location, }; return instance; @@ -947,9 +956,9 @@ rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, rbs *instance = (rbs_types_bases_self_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_BASES_SELF + .type = RBS_TYPES_BASES_SELF, + .location = location, }, - .location = location, }; return instance; @@ -961,9 +970,9 @@ rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, rbs_l *instance = (rbs_types_bases_top_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_BASES_TOP + .type = RBS_TYPES_BASES_TOP, + .location = location, }, - .location = location, }; return instance; @@ -975,21 +984,22 @@ rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, rbs *instance = (rbs_types_bases_void_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_BASES_VOID + .type = RBS_TYPES_BASES_VOID, + .location = location, }, - .location = location, }; return instance; } -rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_node_t *type, bool required, rbs_node_t *self_type) { +rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, bool required, rbs_node_t *self_type) { rbs_types_block_t *instance = rbs_allocator_alloc(allocator, rbs_types_block_t); *instance = (rbs_types_block_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_BLOCK + .type = RBS_TYPES_BLOCK, + .location = location, }, .type = type, .required = required, @@ -999,44 +1009,45 @@ rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_node_t *t return instance; } -rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { +rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { rbs_types_classinstance_t *instance = rbs_allocator_alloc(allocator, rbs_types_classinstance_t); *instance = (rbs_types_classinstance_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_CLASSINSTANCE + .type = RBS_TYPES_CLASSINSTANCE, + .location = location, }, .name = name, .args = args, - .location = location, }; return instance; } -rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_location_t *location) { +rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name) { rbs_types_classsingleton_t *instance = rbs_allocator_alloc(allocator, rbs_types_classsingleton_t); *instance = (rbs_types_classsingleton_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_CLASSSINGLETON + .type = RBS_TYPES_CLASSSINGLETON, + .location = location, }, .name = name, - .location = location, }; return instance; } -rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, rbs_node_list_t *required_positionals, rbs_node_list_t *optional_positionals, rbs_node_t *rest_positionals, rbs_node_list_t *trailing_positionals, rbs_hash_t *required_keywords, rbs_hash_t *optional_keywords, rbs_node_t *rest_keywords, rbs_node_t *return_type) { +rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *required_positionals, rbs_node_list_t *optional_positionals, rbs_node_t *rest_positionals, rbs_node_list_t *trailing_positionals, rbs_hash_t *required_keywords, rbs_hash_t *optional_keywords, rbs_node_t *rest_keywords, rbs_node_t *return_type) { rbs_types_function_t *instance = rbs_allocator_alloc(allocator, rbs_types_function_t); *instance = (rbs_types_function_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_FUNCTION + .type = RBS_TYPES_FUNCTION, + .location = location, }, .required_positionals = required_positionals, .optional_positionals = optional_positionals, @@ -1051,122 +1062,123 @@ rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, rbs_nod return instance; } -rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_ast_symbol_t *name, rbs_location_t *location) { +rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, rbs_ast_symbol_t *name) { rbs_types_function_param_t *instance = rbs_allocator_alloc(allocator, rbs_types_function_param_t); *instance = (rbs_types_function_param_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_FUNCTION_PARAM + .type = RBS_TYPES_FUNCTION_PARAM, + .location = location, }, .type = type, .name = name, - .location = location, }; return instance; } -rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_typename_t *name, rbs_node_list_t *args, rbs_location_t *location) { +rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { rbs_types_interface_t *instance = rbs_allocator_alloc(allocator, rbs_types_interface_t); *instance = (rbs_types_interface_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_INTERFACE + .type = RBS_TYPES_INTERFACE, + .location = location, }, .name = name, .args = args, - .location = location, }; return instance; } -rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location) { +rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types) { rbs_types_intersection_t *instance = rbs_allocator_alloc(allocator, rbs_types_intersection_t); *instance = (rbs_types_intersection_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_INTERSECTION + .type = RBS_TYPES_INTERSECTION, + .location = location, }, .types = types, - .location = location, }; return instance; } -rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, rbs_node_t *literal, rbs_location_t *location) { +rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *literal) { rbs_types_literal_t *instance = rbs_allocator_alloc(allocator, rbs_types_literal_t); *instance = (rbs_types_literal_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_LITERAL + .type = RBS_TYPES_LITERAL, + .location = location, }, .literal = literal, - .location = location, }; return instance; } -rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_location_t *location) { +rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type) { rbs_types_optional_t *instance = rbs_allocator_alloc(allocator, rbs_types_optional_t); *instance = (rbs_types_optional_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_OPTIONAL + .type = RBS_TYPES_OPTIONAL, + .location = location, }, .type = type, - .location = location, }; return instance; } -rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_node_t *type, rbs_types_block_t *block, rbs_location_t *location, rbs_node_t *self_type) { +rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, rbs_types_block_t *block, rbs_node_t *self_type) { rbs_types_proc_t *instance = rbs_allocator_alloc(allocator, rbs_types_proc_t); *instance = (rbs_types_proc_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_PROC + .type = RBS_TYPES_PROC, + .location = location, }, .type = type, .block = block, - .location = location, .self_type = self_type, }; return instance; } -rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, rbs_hash_t *all_fields, rbs_location_t *location) { +rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_hash_t *all_fields) { rbs_types_record_t *instance = rbs_allocator_alloc(allocator, rbs_types_record_t); *instance = (rbs_types_record_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_RECORD + .type = RBS_TYPES_RECORD, + .location = location, }, .all_fields = all_fields, - .location = location, }; return instance; } -rbs_types_record_fieldtype_t *rbs_types_record_fieldtype_new(rbs_allocator_t *allocator, rbs_node_t *type, bool required) { +rbs_types_record_fieldtype_t *rbs_types_record_fieldtype_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, bool required) { rbs_types_record_fieldtype_t *instance = rbs_allocator_alloc(allocator, rbs_types_record_fieldtype_t); *instance = (rbs_types_record_fieldtype_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_RECORD_FIELDTYPE + .type = RBS_TYPES_RECORD_FIELDTYPE, + .location = location, }, .type = type, .required = required, @@ -1175,43 +1187,44 @@ rbs_types_record_fieldtype_t *rbs_types_record_fieldtype_new(rbs_allocator_t *al return instance; } -rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location) { +rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types) { rbs_types_tuple_t *instance = rbs_allocator_alloc(allocator, rbs_types_tuple_t); *instance = (rbs_types_tuple_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_TUPLE + .type = RBS_TYPES_TUPLE, + .location = location, }, .types = types, - .location = location, }; return instance; } -rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_node_list_t *types, rbs_location_t *location) { +rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types) { rbs_types_union_t *instance = rbs_allocator_alloc(allocator, rbs_types_union_t); *instance = (rbs_types_union_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_UNION + .type = RBS_TYPES_UNION, + .location = location, }, .types = types, - .location = location, }; return instance; } -rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, rbs_node_t *return_type) { +rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *return_type) { rbs_types_untypedfunction_t *instance = rbs_allocator_alloc(allocator, rbs_types_untypedfunction_t); *instance = (rbs_types_untypedfunction_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_UNTYPEDFUNCTION + .type = RBS_TYPES_UNTYPEDFUNCTION, + .location = location, }, .return_type = return_type, }; @@ -1219,16 +1232,16 @@ rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allo return instance; } -rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_ast_symbol_t *name, rbs_location_t *location) { +rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name) { rbs_types_variable_t *instance = rbs_allocator_alloc(allocator, rbs_types_variable_t); *instance = (rbs_types_variable_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_VARIABLE + .type = RBS_TYPES_VARIABLE, + .location = location, }, .name = name, - .location = location, }; return instance; diff --git a/src/parser.c b/src/parser.c index ed52e0bd6..de768f906 100644 --- a/src/parser.c +++ b/src/parser.c @@ -99,6 +99,7 @@ static bool rbs_is_untyped_params(method_params *params) { // * @return New RBS::Location object. // * */ static rbs_location_t *rbs_location_current_token(parserstate *state) { + // TODO: Can this just be simplified to `return rbs_location_new(state->current_token.range);`? return rbs_location_pp( &state->current_token.range.start, &state->current_token.range.end @@ -182,14 +183,20 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb && state->next_token.range.end.byte_pos == state->next_token2.range.start.byte_pos ) { rbs_constant_id_t symbol_value = INTERN_TOKEN(state, state->current_token); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, symbol_value); + rbs_location_t *symbolLoc = rbs_location_new(state->next_token.range); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, symbol_value); rbs_node_list_append(path, (rbs_node_t *)symbol); parser_advance(state); parser_advance(state); } - rbs_namespace_t *namespace = rbs_namespace_new(&state->allocator, path, absolute); + range namespace_range = { + .start = rg->start, + .end = state->current_token.range.end + }; + rbs_location_t *loc = rbs_location_new(namespace_range); + rbs_namespace_t *namespace = rbs_namespace_new(&state->allocator, loc, path, absolute); switch (state->current_token.type) { case tLIDENT: @@ -210,9 +217,10 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb rg->end = state->current_token.range.end; } + rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); rbs_constant_id_t name = INTERN_TOKEN(state, state->current_token); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, name); - *typename = rbs_typename_new(&state->allocator, namespace, symbol); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, name); + *typename = rbs_typename_new(&state->allocator, rbs_location_new(*rg), namespace, symbol); return true; } @@ -298,7 +306,7 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t rbs_loc_alloc_children(loc, 1); rbs_loc_add_optional_child(loc, INTERN("name"), NULL_RANGE); - *function_param = rbs_types_function_param_new(&state->allocator, type, NULL, loc); + *function_param = rbs_types_function_param_new(&state->allocator, loc, type, NULL); return true; } else { range name_range = state->next_token.range; @@ -316,14 +324,15 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t } rbs_string_t unquoted_str = rbs_unquote_string(rbs_parser_get_current_token(state)); + rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str); - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); rbs_location_t *loc = rbs_location_new(param_range); rbs_loc_alloc_children(loc, 1); rbs_loc_add_optional_child(loc, INTERN("name"), name_range); - *function_param = rbs_types_function_param_new(&state->allocator, type, name, loc); + *function_param = rbs_types_function_param_new(&state->allocator, loc, type, name); return true; } } @@ -344,15 +353,19 @@ static rbs_constant_id_t intern_token_start_end(parserstate *state, token start_ NODISCARD static bool parse_keyword_key(parserstate *state, rbs_ast_symbol_t **key) { parser_advance(state); + + rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); + if (state->next_token.type == pQUESTION) { *key = rbs_ast_symbol_new( &state->allocator, + symbolLoc, &state->constant_pool, intern_token_start_end(state, state->current_token, state->next_token) ); parser_advance(state); } else { - *key = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + *key = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); } return true; @@ -370,7 +383,8 @@ static bool parse_keyword(parserstate *state, rbs_hash_t *keywords, rbs_hash_t * set_error(state, state->current_token, true, "duplicated keyword argument"); return false; } else { - rbs_hash_set(memo, (rbs_node_t *) key, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, true)); + rbs_location_t *loc = rbs_location_new(state->current_token.range); + rbs_hash_set(memo, (rbs_node_t *) key, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, loc, true)); } ADVANCE_ASSERT(state, pCOLON); @@ -611,7 +625,7 @@ static bool parse_optional(parserstate *state, rbs_node_t **optional) { parser_advance(state); rg.end = state->current_token.range.end; rbs_location_t *location = rbs_location_new(rg); - *optional = (rbs_node_t *) rbs_types_optional_new(&state->allocator, type, location); + *optional = (rbs_node_t *) rbs_types_optional_new(&state->allocator, location, type); } else { *optional = type; } @@ -668,6 +682,9 @@ static bool parse_function(parserstate *state, bool accept_type_binding, parse_f rbs_node_t *function = NULL; rbs_types_block_t *block = NULL; rbs_node_t *function_self_type = NULL; + range function_range; + function_range.start = state->current_token.range.start; + method_params params; initialize_method_params(¶ms, &state->allocator); @@ -716,11 +733,14 @@ static bool parse_function(parserstate *state, bool accept_type_binding, parse_f CHECK_PARSE(parse_optional(state, &block_return_type)); rbs_node_t *block_function = NULL; + function_range.end = state->current_token.range.end; + rbs_location_t *loc = rbs_location_new(function_range); if (rbs_is_untyped_params(&block_params)) { - block_function = (rbs_node_t *) rbs_types_untypedfunction_new(&state->allocator, block_return_type); + block_function = (rbs_node_t *) rbs_types_untypedfunction_new(&state->allocator, loc, block_return_type); } else { block_function = (rbs_node_t *) rbs_types_function_new( &state->allocator, + loc, block_params.required_positionals, block_params.optional_positionals, block_params.rest_positionals, @@ -732,7 +752,7 @@ static bool parse_function(parserstate *state, bool accept_type_binding, parse_f ); } - block = rbs_types_block_new(&state->allocator, block_function, required, self_type); + block = rbs_types_block_new(&state->allocator, loc, block_function, required, self_type); ADVANCE_ASSERT(state, pRBRACE); } @@ -741,11 +761,14 @@ static bool parse_function(parserstate *state, bool accept_type_binding, parse_f rbs_node_t *type = NULL; CHECK_PARSE(parse_optional(state, &type)); + function_range.end = state->current_token.range.end; + rbs_location_t *loc = rbs_location_new(function_range); if (rbs_is_untyped_params(¶ms)) { - function = (rbs_node_t *) rbs_types_untypedfunction_new(&state->allocator, type); + function = (rbs_node_t *) rbs_types_untypedfunction_new(&state->allocator, loc, type); } else { function = (rbs_node_t *) rbs_types_function_new( &state->allocator, + loc, params.required_positionals, params.optional_positionals, params.rest_positionals, @@ -781,7 +804,7 @@ static bool parse_proc_type(parserstate *state, rbs_types_proc_t **proc) { position end = state->current_token.range.end; rbs_location_t *loc = rbs_location_pp(&start, &end); - *proc = rbs_types_proc_new(&state->allocator, result->function, result->block, loc, result->function_self_type); + *proc = rbs_types_proc_new(&state->allocator, loc, result->function, result->block, result->function_self_type); return true; } @@ -848,10 +871,15 @@ static bool parse_record_attributes(parserstate *state, rbs_hash_t **fields) { ADVANCE_ASSERT(state, pFATARROW); } + range field_range; + field_range.start = state->current_token.range.end; + rbs_node_t *type; CHECK_PARSE(parse_type(state, &type)); - rbs_hash_set(*fields, (rbs_node_t *) key, (rbs_node_t *) rbs_types_record_fieldtype_new(&state->allocator, type, required)); + field_range.end = state->current_token.range.end; + rbs_location_t *loc = rbs_location_new(field_range); + rbs_hash_set(*fields, (rbs_node_t *) key, (rbs_node_t *) rbs_types_record_fieldtype_new(&state->allocator, loc, type, required)); if (parser_advance_if(state, pCOMMA)) { if (state->next_token.type == pRBRACE) { @@ -877,22 +905,31 @@ static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types switch (state->current_token.type) { case tSYMBOL: { + rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); + char *buffer = peek_token(state->lexstate, state->current_token); rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared( &state->constant_pool, (const uint8_t *) buffer+offset_bytes, bytes ); - literal = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); + literal = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); break; } case tDQSYMBOL: case tSQSYMBOL: { + rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); rbs_string_t string = rbs_parser_get_current_token(state); rbs_string_drop_first(&string, offset_bytes); rbs_string_t unquoted_str = rbs_unquote_string(string); - literal = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str)); + + literal = rbs_ast_symbol_new( + &state->allocator, + symbolLoc, + &state->constant_pool, + rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str) + ); break; } default: @@ -900,7 +937,7 @@ static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types return false; } - *symbol = rbs_types_literal_new(&state->allocator, (rbs_node_t *) literal, location); + *symbol = rbs_types_literal_new(&state->allocator, location, (rbs_node_t *) literal); return true; } @@ -957,11 +994,11 @@ static bool parse_instance_type(parserstate *state, bool parse_alias, rbs_node_t rbs_loc_add_optional_child(loc, INTERN("args"), args_range); if (kind == CLASS_NAME) { - *type = (rbs_node_t *) rbs_types_classinstance_new(&state->allocator, typename, types, loc); + *type = (rbs_node_t *) rbs_types_classinstance_new(&state->allocator, loc, typename, types); } else if (kind == INTERFACE_NAME) { - *type = (rbs_node_t *) rbs_types_interface_new(&state->allocator, typename, types, loc); + *type = (rbs_node_t *) rbs_types_interface_new(&state->allocator, loc, typename, types); } else if (kind == ALIAS_NAME) { - *type = (rbs_node_t *) rbs_types_alias_new(&state->allocator, typename, types, loc); + *type = (rbs_node_t *) rbs_types_alias_new(&state->allocator, loc, typename, types); } return true; @@ -990,7 +1027,7 @@ static bool parse_singleton_type(parserstate *state, rbs_types_classsingleton_t rbs_loc_alloc_children(loc, 1); rbs_loc_add_required_child(loc, INTERN("name"), name_range); - *singleton = rbs_types_classsingleton_new(&state->allocator, typename, loc); + *singleton = rbs_types_classsingleton_new(&state->allocator, loc, typename); return true; } @@ -1058,12 +1095,12 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { } case kUNTYPED: { rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, false, loc); + *type = (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, loc, false); return true; } case k__TODO__: { rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, true, loc); + *type = (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, loc, true); return true; } case tINTEGER: { @@ -1073,18 +1110,18 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { rbs_string_strip_whitespace(&string); rbs_string_ensure_owned(&string); - rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(&state->allocator, string); - *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, literal, loc); + rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(&state->allocator, loc, string); + *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, loc, literal); return true; } case kTRUE: { rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, true), loc); + *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, loc, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, loc, true)); return true; } case kFALSE: { rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, false), loc); + *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, loc, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, loc, false)); return true; } case tSQSTRING: @@ -1092,8 +1129,8 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { rbs_location_t *loc = rbs_location_current_token(state); rbs_string_t unquoted_str = rbs_unquote_string(rbs_parser_get_current_token(state)); - rbs_node_t *literal = (rbs_node_t *) rbs_ast_string_new(&state->allocator, unquoted_str); - *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, literal, loc); + rbs_node_t *literal = (rbs_node_t *) rbs_ast_string_new(&state->allocator, loc, unquoted_str); + *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, loc, literal); return true; } case tSYMBOL: @@ -1113,8 +1150,8 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { if (parser_typevar_member(state, name)) { rbs_location_t *loc = rbs_location_current_token(state); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, name); - *type = (rbs_node_t *) rbs_types_variable_new(&state->allocator, symbol, loc); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, loc, &state->constant_pool, name); + *type = (rbs_node_t *) rbs_types_variable_new(&state->allocator, loc, symbol); return true; } @@ -1145,13 +1182,13 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { rg.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(rg); - *type = (rbs_node_t *) rbs_types_tuple_new(&state->allocator, types, loc); + *type = (rbs_node_t *) rbs_types_tuple_new(&state->allocator, loc, types); return true; } case pAREF_OPR: { rbs_location_t *loc = rbs_location_current_token(state); rbs_node_list_t *types = rbs_node_list_new(&state->allocator); - *type = (rbs_node_t *) rbs_types_tuple_new(&state->allocator, types, loc); + *type = (rbs_node_t *) rbs_types_tuple_new(&state->allocator, loc, types); return true; } case pLBRACE: { @@ -1161,7 +1198,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { ADVANCE_ASSERT(state, pRBRACE); position end = state->current_token.range.end; rbs_location_t *loc = rbs_location_pp(&start, &end); - *type = (rbs_node_t *) rbs_types_record_new(&state->allocator, fields, loc); + *type = (rbs_node_t *) rbs_types_record_new(&state->allocator, loc, fields); return true; } case pHAT: { @@ -1203,7 +1240,7 @@ static bool parse_intersection(parserstate *state, rbs_node_t **type) { if (intersection_types->length > 1) { rbs_location_t *location = rbs_location_new(rg); - *type = (rbs_node_t *) rbs_types_intersection_new(&state->allocator, intersection_types, location); + *type = (rbs_node_t *) rbs_types_intersection_new(&state->allocator, location, intersection_types); } return true; @@ -1233,7 +1270,7 @@ bool parse_type(parserstate *state, rbs_node_t **type) { if (union_types->length > 1) { rbs_location_t *location = rbs_location_new(rg); - *type = (rbs_node_t *) rbs_types_union_new(&state->allocator, union_types, location); + *type = (rbs_node_t *) rbs_types_union_new(&state->allocator, location, union_types); } return true; @@ -1260,7 +1297,7 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa while (true) { bool unchecked = false; - rbs_keyword_t *variance = rbs_keyword_new(&state->allocator, INTERN("invariant")); + rbs_keyword_t *variance = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("invariant")); rbs_node_t *upper_bound = NULL; rbs_node_t *default_type = NULL; @@ -1279,10 +1316,10 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa if (state->next_token.type == kIN || state->next_token.type == kOUT) { switch (state->next_token.type) { case kIN: - variance = rbs_keyword_new(&state->allocator, INTERN("contravariant")); + variance = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("contravariant")); break; case kOUT: - variance = rbs_keyword_new(&state->allocator, INTERN("covariant")); + variance = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("covariant")); break; default: set_error(state, state->current_token, false, "Unexpected error"); @@ -1298,8 +1335,9 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa range name_range = state->current_token.range; rbs_string_t string = rbs_parser_get_current_token(state); + rbs_location_t *nameSymbolLoc = rbs_location_new(state->current_token.range); rbs_constant_id_t id = rbs_constant_pool_insert_string(&state->constant_pool, string); - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, id); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, nameSymbolLoc, &state->constant_pool, id); CHECK_PARSE(parser_insert_typevar(state, id)); @@ -1339,7 +1377,7 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa rbs_loc_add_optional_child(loc, INTERN("upper_bound"), upper_bound_range); rbs_loc_add_optional_child(loc, INTERN("default"), default_type_range); - rbs_ast_typeparam_t *param = rbs_ast_typeparam_new(&state->allocator, name, variance, upper_bound, default_type, unchecked, loc); + rbs_ast_typeparam_t *param = rbs_ast_typeparam_new(&state->allocator, loc, name, variance, upper_bound, default_type, unchecked); rbs_node_list_append(*params, (rbs_node_t *) param); @@ -1391,7 +1429,7 @@ bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type) { rbs_loc_add_required_child(loc, INTERN("type"), type_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range); - *method_type = rbs_methodtype_new(&state->allocator, type_params, result->function, result->block, loc); + *method_type = rbs_methodtype_new(&state->allocator, loc, type_params, result->function, result->block); return true; } @@ -1406,7 +1444,9 @@ static bool parse_global_decl(parserstate *state, rbs_ast_declarations_global_t rbs_ast_comment_t *comment = get_comment(state, decl_range.start.line); range name_range = state->current_token.range; - rbs_ast_symbol_t *typename = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(name_range); + + rbs_ast_symbol_t *typename = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); ADVANCE_ASSERT(state, pCOLON); range colon_range = state->current_token.range; @@ -1420,7 +1460,7 @@ static bool parse_global_decl(parserstate *state, rbs_ast_declarations_global_t rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - *global = rbs_ast_declarations_global_new(&state->allocator, typename, type, loc, comment); + *global = rbs_ast_declarations_global_new(&state->allocator, loc, typename, type, comment); return true; } @@ -1451,7 +1491,7 @@ static bool parse_const_decl(parserstate *state, rbs_ast_declarations_constant_t rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - *constant = rbs_ast_declarations_constant_new(&state->allocator, typename, type, loc, comment); + *constant = rbs_ast_declarations_constant_new(&state->allocator, loc, typename, type, comment); return true; } @@ -1497,7 +1537,7 @@ static bool parse_type_decl(parserstate *state, position comment_pos, rbs_node_l rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); - *typealias = rbs_ast_declarations_typealias_new(&state->allocator, typename, type_params, type, annotations, loc, comment); + *typealias = rbs_ast_declarations_typealias_new(&state->allocator, loc, typename, type_params, type, annotations, comment); return true; } @@ -1553,7 +1593,7 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati rbs_location_t *loc = rbs_location_new(rg); - *annotation = rbs_ast_annotation_new(&state->allocator, string, loc); + *annotation = rbs_ast_annotation_new(&state->allocator, loc, string); return true; } @@ -1611,24 +1651,28 @@ static bool parse_method_name(parserstate *state, range *range, rbs_ast_symbol_t state->lexstate->encoding ); - *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); + rbs_location_t *symbolLoc = rbs_location_new(*range); + *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); } else { *range = state->current_token.range; - *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(*range); + *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); } return true; case tBANGIDENT: case tEQIDENT: { *range = state->current_token.range; - *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(*range); + *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); return true; } case tQIDENT: { rbs_string_t string = rbs_parser_get_current_token(state); rbs_string_t unquoted_str = rbs_unquote_string(string); rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str); - *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, constant_id); + rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); + *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); return true; } @@ -1641,7 +1685,8 @@ static bool parse_method_name(parserstate *state, range *range, rbs_ast_symbol_t case pAREF_OPR: case tOPERATOR: { *range = state->current_token.range; - *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(*range); + *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); return true; } @@ -1722,14 +1767,14 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept { case kPRIVATE: { visibility_range = state->current_token.range; - visibility = rbs_keyword_new(&state->allocator, INTERN("private")); + visibility = rbs_keyword_new(&state->allocator, rbs_location_new(visibility_range), INTERN("private")); member_range.start = visibility_range.start; parser_advance(state); break; } case kPUBLIC: { visibility_range = state->current_token.range; - visibility = rbs_keyword_new(&state->allocator, INTERN("public")); + visibility = rbs_keyword_new(&state->allocator, rbs_location_new(visibility_range), INTERN("public")); member_range.start = visibility_range.start; parser_advance(state); break; @@ -1774,6 +1819,9 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept rbs_node_list_t *annotations = rbs_node_list_new(&state->allocator); position overload_annot_pos = NullPosition; + range overload_range; + overload_range.start = state->current_token.range.start; + if (state->next_token.type == tANNOTATION) { CHECK_PARSE(parse_annotations(state, annotations, &overload_annot_pos)); } @@ -1788,7 +1836,9 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept rbs_methodtype_t *method_type = NULL; CHECK_PARSE(parse_method_type(state, &method_type)); - rbs_node_t *overload = (rbs_node_t *) rbs_ast_members_methoddefinition_overload_new(&state->allocator, annotations, (rbs_node_t *) method_type); + overload_range.end = state->current_token.range.end; + rbs_location_t *loc = rbs_location_new(overload_range); + rbs_node_t *overload = (rbs_node_t *) rbs_ast_members_methoddefinition_overload_new(&state->allocator, loc, annotations, (rbs_node_t *) method_type); rbs_node_list_append(overloads, overload); member_range.end = state->current_token.range.end; break; @@ -1824,15 +1874,15 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept rbs_keyword_t *k; switch (kind) { case INSTANCE_KIND: { - k = rbs_keyword_new(&state->allocator, INTERN("instance")); + k = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("instance")); break; } case SINGLETON_KIND: { - k = rbs_keyword_new(&state->allocator, INTERN("singleton")); + k = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("singleton")); break; } case INSTANCE_SINGLETON_KIND: { - k = rbs_keyword_new(&state->allocator, INTERN("singleton_instance")); + k = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("singleton_instance")); break; } default: @@ -1848,7 +1898,7 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept rbs_loc_add_optional_child(loc, INTERN("overloading"), overloading_range); rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range); - *method_definition = rbs_ast_members_methoddefinition_new(&state->allocator, name, k, overloads, annotations, loc, comment, overloading, visibility); + *method_definition = rbs_ast_members_methoddefinition_new(&state->allocator, loc, name, k, overloads, annotations, comment, overloading, visibility); return true; } @@ -1945,13 +1995,13 @@ static bool parse_mixin_member(parserstate *state, bool from_interface, position switch (type) { case kINCLUDE: - *mixin_member = (rbs_node_t *) rbs_ast_members_include_new(&state->allocator, name, args, annotations, loc, comment); + *mixin_member = (rbs_node_t *) rbs_ast_members_include_new(&state->allocator, loc, name, args, annotations, comment); return true; case kEXTEND: - *mixin_member = (rbs_node_t *) rbs_ast_members_extend_new(&state->allocator, name, args, annotations, loc, comment); + *mixin_member = (rbs_node_t *) rbs_ast_members_extend_new(&state->allocator, loc, name, args, annotations, comment); return true; case kPREPEND: - *mixin_member = (rbs_node_t *) rbs_ast_members_prepend_new(&state->allocator, name, args, annotations, loc, comment); + *mixin_member = (rbs_node_t *) rbs_ast_members_prepend_new(&state->allocator, loc, name, args, annotations, comment); return true; default: set_error(state, state->current_token, false, "Unexpected error"); @@ -1981,7 +2031,7 @@ static bool parse_alias_member(parserstate *state, bool instance_only, position range new_kind_range, old_kind_range, new_name_range, old_name_range; if (!instance_only && state->next_token.type == kSELF) { - kind = rbs_keyword_new(&state->allocator, INTERN("singleton")); + kind = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("singleton")); new_kind_range.start = state->next_token.range.start; new_kind_range.end = state->next_token2.range.end; @@ -1995,7 +2045,7 @@ static bool parse_alias_member(parserstate *state, bool instance_only, position ADVANCE_ASSERT(state, pDOT); CHECK_PARSE(parse_method_name(state, &old_name_range, &old_name)); } else { - kind = rbs_keyword_new(&state->allocator, INTERN("instance")); + kind = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("instance")); CHECK_PARSE(parse_method_name(state, &new_name_range, &new_name)); CHECK_PARSE(parse_method_name(state, &old_name_range, &old_name)); new_kind_range = NULL_RANGE; @@ -2011,7 +2061,7 @@ static bool parse_alias_member(parserstate *state, bool instance_only, position rbs_loc_add_optional_child(loc, INTERN("new_kind"), new_kind_range); rbs_loc_add_optional_child(loc, INTERN("old_kind"), old_kind_range); - *alias_member = rbs_ast_members_alias_new(&state->allocator, new_name, old_name, kind, annotations, loc, comment); + *alias_member = rbs_ast_members_alias_new(&state->allocator, loc, new_name, old_name, kind, annotations, comment); return true; } @@ -2036,7 +2086,8 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ { case tAIDENT: { range name_range = state->current_token.range; - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(name_range); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); ADVANCE_ASSERT(state, pCOLON); range colon_range = state->current_token.range; @@ -2051,12 +2102,13 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); - *variable_member = (rbs_node_t *)rbs_ast_members_instancevariable_new(&state->allocator, name, type, loc, comment); + *variable_member = (rbs_node_t *)rbs_ast_members_instancevariable_new(&state->allocator, loc, name, type, comment); return true; } case tA2IDENT: { range name_range = state->current_token.range; - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(name_range); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); ADVANCE_ASSERT(state, pCOLON); range colon_range = state->current_token.range; @@ -2076,7 +2128,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); - *variable_member = (rbs_node_t *) rbs_ast_members_classvariable_new(&state->allocator, name, type, loc, comment); + *variable_member = (rbs_node_t *) rbs_ast_members_classvariable_new(&state->allocator, loc, name, type, comment); return true; } case kSELF: { @@ -2089,7 +2141,8 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ ADVANCE_ASSERT(state, tAIDENT); range name_range = state->current_token.range; - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(name_range); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); ADVANCE_ASSERT(state, pCOLON); range colon_range = state->current_token.range; @@ -2109,7 +2162,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range); - *variable_member = (rbs_node_t *)rbs_ast_members_classinstancevariable_new(&state->allocator, name, type, loc, comment); + *variable_member = (rbs_node_t *)rbs_ast_members_classinstancevariable_new(&state->allocator, loc, name, type, comment); return true; } default: @@ -2169,19 +2222,19 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs comment_pos = nonnull_pos_or(comment_pos, member_range.start); rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); - rbs_keyword_t *visibility; range visibility_range; + rbs_keyword_t *visibility; switch (state->current_token.type) { case kPRIVATE: { - visibility = rbs_keyword_new(&state->allocator, INTERN("private")); visibility_range = state->current_token.range; + visibility = rbs_keyword_new(&state->allocator, rbs_location_new(visibility_range), INTERN("private")); parser_advance(state); break; } case kPUBLIC: { - visibility = rbs_keyword_new(&state->allocator, INTERN("public")); visibility_range = state->current_token.range; + visibility = rbs_keyword_new(&state->allocator, rbs_location_new(visibility_range), INTERN("public")); parser_advance(state); break; } @@ -2197,7 +2250,11 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs range kind_range; InstanceSingletonKind is_kind = parse_instance_singleton_kind(state, false, &kind_range); - rbs_keyword_t *kind = rbs_keyword_new(&state->allocator, INTERN(((is_kind == INSTANCE_KIND) ? "instance" : "singleton"))); + rbs_keyword_t *kind = rbs_keyword_new( + &state->allocator, + rbs_location_new(keyword_range), + INTERN(((is_kind == INSTANCE_KIND) ? "instance" : "singleton")) + ); range name_range; rbs_ast_symbol_t *attr_name; @@ -2210,10 +2267,15 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs ivar_range.start = state->current_token.range.start; if (parser_advance_if(state, tAIDENT)) { - ivar_name = (rbs_node_t *) rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); + ivar_name = (rbs_node_t *) rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); ivar_name_range = state->current_token.range; } else { - ivar_name = (rbs_node_t *) rbs_ast_bool_new(&state->allocator, false); + range false_range = { + .start = state->current_token.range.start, + .end = state->current_token.range.end + }; + ivar_name = (rbs_node_t *) rbs_ast_bool_new(&state->allocator, rbs_location_new(false_range), false); ivar_name_range = NULL_RANGE; } @@ -2250,13 +2312,13 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs switch (attr_type) { case kATTRREADER: - *attribute_member = (rbs_node_t *) rbs_ast_members_attrreader_new(&state->allocator, attr_name, type, ivar_name, kind, annotations, loc, comment, visibility); + *attribute_member = (rbs_node_t *) rbs_ast_members_attrreader_new(&state->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); return true; case kATTRWRITER: - *attribute_member = (rbs_node_t *) rbs_ast_members_attrwriter_new(&state->allocator, attr_name, type, ivar_name, kind, annotations, loc, comment, visibility); + *attribute_member = (rbs_node_t *) rbs_ast_members_attrwriter_new(&state->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); return true; case kATTRACCESSOR: - *attribute_member = (rbs_node_t *) rbs_ast_members_attraccessor_new(&state->allocator, attr_name, type, ivar_name, kind, annotations, loc, comment, visibility); + *attribute_member = (rbs_node_t *) rbs_ast_members_attraccessor_new(&state->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); return true; default: set_error(state, state->current_token, false, "Unexpected error"); @@ -2357,7 +2419,7 @@ static bool parse_interface_decl(parserstate *state, position comment_pos, rbs_n rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); - *interface_decl = rbs_ast_declarations_interface_new(&state->allocator, name, type_params, members, annotations, loc, comment); + *interface_decl = rbs_ast_declarations_interface_new(&state->allocator, loc, name, type_params, members, annotations, comment); return true; } @@ -2395,7 +2457,7 @@ static bool parse_module_self_types(parserstate *state, rbs_node_list_t *array) rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); - rbs_ast_declarations_module_self_t *self_type = rbs_ast_declarations_module_self_new(&state->allocator, module_name, args, loc); + rbs_ast_declarations_module_self_t *self_type = rbs_ast_declarations_module_self_new(&state->allocator, loc, module_name, args); rbs_node_list_append(array, (rbs_node_t *)self_type); if (state->next_token.type == pCOMMA) { @@ -2553,7 +2615,7 @@ static bool parse_module_decl0(parserstate *state, range keyword_range, rbs_type CHECK_PARSE(parser_pop_typevar_table(state)); - *module_decl = rbs_ast_declarations_module_new(&state->allocator, module_name, type_params, self_types, members, annotations, loc, comment); + *module_decl = rbs_ast_declarations_module_new(&state->allocator, loc, module_name, type_params, self_types, members, annotations, comment); return true; } @@ -2596,7 +2658,7 @@ static bool parse_module_decl(parserstate *state, position comment_pos, rbs_node rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); - *module_decl = (rbs_node_t *) rbs_ast_declarations_modulealias_new(&state->allocator, module_name, old_name, loc, comment); + *module_decl = (rbs_node_t *) rbs_ast_declarations_modulealias_new(&state->allocator, loc, module_name, old_name, comment); } else { rbs_ast_declarations_module_t *module_decl0 = NULL; CHECK_PARSE(parse_module_decl0(state, keyword_range, module_name, module_name_range, comment, annotations, &module_decl0)); @@ -2631,7 +2693,7 @@ static bool parse_class_decl_super(parserstate *state, range *lt_range, rbs_ast_ rbs_loc_add_optional_child(loc, INTERN("args"), args_range); - *super = rbs_ast_declarations_class_super_new(&state->allocator, name, args, loc); + *super = rbs_ast_declarations_class_super_new(&state->allocator, loc, name, args); } else { *lt_range = NULL_RANGE; } @@ -2676,7 +2738,7 @@ static bool parse_class_decl0(parserstate *state, range keyword_range, rbs_typen rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range); rbs_loc_add_optional_child(loc, INTERN("lt"), lt_range); - *class_decl = rbs_ast_declarations_class_new(&state->allocator, name, type_params, super, members, annotations, loc, comment); + *class_decl = rbs_ast_declarations_class_new(&state->allocator, loc, name, type_params, super, members, annotations, comment); return true; } @@ -2717,7 +2779,7 @@ static bool parse_class_decl(parserstate *state, position comment_pos, rbs_node_ rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); - *class_decl = (rbs_node_t *) rbs_ast_declarations_classalias_new(&state->allocator, class_name, old_name, loc, comment); + *class_decl = (rbs_node_t *) rbs_ast_declarations_classalias_new(&state->allocator, loc, class_name, old_name, comment); } else { rbs_ast_declarations_class_t *class_decl0 = NULL; CHECK_PARSE(parse_class_decl0(state, keyword_range, class_name, class_name_range, comment, annotations, &class_decl0)); @@ -2860,7 +2922,8 @@ static bool parse_namespace(parserstate *state, range *rg, rbs_namespace_t **nam while (true) { if (state->next_token.type == tUIDENT && state->next_token2.type == pCOLON2) { - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->next_token)); + rbs_location_t *symbolLoc = rbs_location_new(state->next_token.range); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->next_token)); rbs_node_list_append(path, (rbs_node_t *)symbol); if (null_position_p(rg->start)) { rg->start = state->next_token.range.start; @@ -2873,7 +2936,7 @@ static bool parse_namespace(parserstate *state, range *rg, rbs_namespace_t **nam } } - *namespace = rbs_namespace_new(&state->allocator, path, is_absolute); + *namespace = rbs_namespace_new(&state->allocator, rbs_location_new(*rg), path, is_absolute); return true; } @@ -2904,8 +2967,9 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { ? state->current_token.range : (range) { .start = namespace_range.start, .end = state->current_token.range.end }; - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); - rbs_typename_t *type_name = rbs_typename_new(&state->allocator, namespace, symbol); + rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + rbs_typename_t *type_name = rbs_typename_new(&state->allocator, rbs_location_new(type_name_range),namespace, symbol); range keyword_range = NULL_RANGE; range new_name_range = NULL_RANGE; @@ -2919,7 +2983,8 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { if (ident_type == tLIDENT) ADVANCE_ASSERT(state, tLIDENT); if (ident_type == tULIDENT) ADVANCE_ASSERT(state, tULIDENT); - new_name = rbs_ast_symbol_new(&state->allocator, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(new_name_range); + new_name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); new_name_range = state->current_token.range; clause_range.end = new_name_range.end; } @@ -2930,7 +2995,7 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { rbs_loc_add_optional_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_optional_child(loc, INTERN("new_name"), new_name_range); - rbs_ast_directives_use_singleclause_t *clause = rbs_ast_directives_use_singleclause_new(&state->allocator, type_name, new_name, loc); + rbs_ast_directives_use_singleclause_t *clause = rbs_ast_directives_use_singleclause_new(&state->allocator, loc, type_name, new_name); rbs_node_list_append(clauses, (rbs_node_t *)clause); break; @@ -2948,7 +3013,7 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { rbs_loc_add_required_child(loc, INTERN("namespace"), namespace_range); rbs_loc_add_required_child(loc, INTERN("star"), star_range); - rbs_ast_directives_use_wildcardclause_t *clause = rbs_ast_directives_use_wildcardclause_new(&state->allocator, namespace, loc); + rbs_ast_directives_use_wildcardclause_t *clause = rbs_ast_directives_use_wildcardclause_new(&state->allocator, loc, namespace); rbs_node_list_append(clauses, (rbs_node_t *)clause); break; @@ -2988,13 +3053,16 @@ static bool parse_use_directive(parserstate *state, rbs_ast_directives_use_t **u rbs_loc_alloc_children(loc, 1); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); - *use_directive = rbs_ast_directives_use_new(&state->allocator, clauses, loc); + *use_directive = rbs_ast_directives_use_new(&state->allocator, loc, clauses); } return true; } bool parse_signature(parserstate *state, rbs_signature_t **signature) { + range signature_range; + signature_range.start = state->current_token.range.start; + rbs_node_list_t *dirs = rbs_node_list_new(&state->allocator); rbs_node_list_t *decls = rbs_node_list_new(&state->allocator); @@ -3015,6 +3083,7 @@ bool parse_signature(parserstate *state, rbs_signature_t **signature) { rbs_node_list_append(decls, decl); } - *signature = rbs_signature_new(&state->allocator, dirs, decls); + signature_range.end = state->current_token.range.end; + *signature = rbs_signature_new(&state->allocator, rbs_location_new(signature_range), dirs, decls); return true; } diff --git a/src/parserstate.c b/src/parserstate.c index 717da4373..a50c9482d 100644 --- a/src/parserstate.c +++ b/src/parserstate.c @@ -204,8 +204,8 @@ static rbs_ast_comment_t *parse_comment_lines(parserstate *state, comment *com) return rbs_ast_comment_new( &state->allocator, - rbs_buffer_to_string(&rbs_buffer), - rbs_location_pp(&com->start, &com->end) + rbs_location_pp(&com->start, &com->end), + rbs_buffer_to_string(&rbs_buffer) ); } diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index eefb025d7..6bfad7f34 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -98,11 +98,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_ary_push(array, rbs_node_list_to_ruby_array(ctx, signature->declarations)); return array; <%- else -%> - <%- if node.fields.any? -%> <%# This prevents "warning: unused variable 'node'" %> <%= node.c_type_name %> *node = (<%= node.c_type_name %> *)instance; - <%- end -%> VALUE h = rb_hash_new(); + <%- if node.expose_location? -%> + rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); + <%- end -%> <%- node.fields.each do |field| -%> <%- case field.c_type -%> <%- when "VALUE" -%> @@ -111,8 +112,6 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_node_list_to_ruby_array(ctx, node-><%= field.c_name %>)); <%- when "rbs_hash" -%> rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_hash_to_ruby_hash(ctx, node-><%= field.c_name %>)); - <%- when "rbs_location" -%> - rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_loc_to_ruby_location(ctx, node-><%= field.c_name %>)); <%- when "rbs_ast_symbol" -%> rb_hash_aset(h, ID2SYM(rb_intern("<%= field.name %>")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node-><%= field.c_name %>)); // rbs_ast_symbol <%- when "rbs_keyword" -%> diff --git a/templates/include/rbs/ast.h.erb b/templates/include/rbs/ast.h.erb index a3c8a81aa..f719aaf12 100644 --- a/templates/include/rbs/ast.h.erb +++ b/templates/include/rbs/ast.h.erb @@ -16,6 +16,7 @@ enum rbs_node_type { typedef struct rbs_node { enum rbs_node_type type; + rbs_location_t *location; } rbs_node_t; const char* rbs_node_type_name(rbs_node_t *node); @@ -82,7 +83,7 @@ typedef struct rbs_keyword { rbs_constant_id_t constant_id; } rbs_keyword_t; -rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *, rbs_constant_id_t); +rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *, rbs_location_t *, rbs_constant_id_t); /// `rbs_ast_symbol_t` models user-defined identifiers like class names, method names, etc. /// These get stored in the parser's own constant pool, and get surfaced to Ruby as `Symbol`s. @@ -91,7 +92,7 @@ typedef struct rbs_ast_symbol { rbs_constant_id_t constant_id; } rbs_ast_symbol_t; -rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *, rbs_constant_pool_t *, rbs_constant_id_t); +rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *, rbs_location_t *, rbs_constant_pool_t *, rbs_constant_id_t); <%- nodes.each do |node| -%> <%= node.c_type_name %> *<%= node.c_constructor_function_name %>(<%= node.constructor_params.map(&:parameter_decl).join(", ") %>); diff --git a/templates/src/ast.c.erb b/templates/src/ast.c.erb index 922d9d236..9d69e49cb 100644 --- a/templates/src/ast.c.erb +++ b/templates/src/ast.c.erb @@ -119,12 +119,13 @@ rbs_node_t* rbs_hash_get(rbs_hash_t *hash, rbs_node_t *key) { return node ? node->value : NULL; } -rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *allocator, rbs_constant_id_t constant_id) { +rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_constant_id_t constant_id) { rbs_keyword_t *instance = rbs_allocator_alloc(allocator, rbs_keyword_t); *instance = (rbs_keyword_t) { .base = (rbs_node_t) { .type = RBS_KEYWORD, + .location = location, }, .constant_id = constant_id, }; @@ -132,12 +133,13 @@ rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *allocator, rbs_constant_id_t con return instance; } -rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_constant_pool_t *constant_pool, rbs_constant_id_t constant_id) { +rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_constant_pool_t *constant_pool, rbs_constant_id_t constant_id) { rbs_ast_symbol_t *instance = rbs_allocator_alloc(allocator, rbs_ast_symbol_t); *instance = (rbs_ast_symbol_t) { .base = (rbs_node_t) { .type = RBS_AST_SYMBOL, + .location = location, }, .constant_id = constant_id, }; @@ -155,7 +157,8 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_constant_po *instance = (<%= node.c_type_name %>) { .base = (rbs_node_t) { - .type = <%= node.c_type_enum_name %> + .type = <%= node.c_type_enum_name %>, + .location = location, }, <%- node.fields.each do |field| -%> .<%= field.c_name %> = <%= field.c_name %>, diff --git a/templates/template.rb b/templates/template.rb index 692461813..8e02a956e 100644 --- a/templates/template.rb +++ b/templates/template.rb @@ -109,10 +109,14 @@ def initialize(yaml) @c_type_enum_name = @c_base_name.upcase @expose_to_ruby = yaml.fetch("expose_to_ruby", true) + @expose_location = yaml.fetch("expose_location", true) @fields = yaml.fetch("fields", []).map { |field| Field.from_hash(field) }.freeze - @constructor_params = [Field.new(name: "allocator", c_type: "rbs_allocator_t *")] + @constructor_params = [ + Field.new(name: "allocator", c_type: "rbs_allocator_t *"), + Field.new(name: "location", c_type: "rbs_location_t *" ), + ] @constructor_params.concat @fields @constructor_params.freeze end @@ -128,6 +132,10 @@ def c_constructor_function_name #: String def expose_to_ruby? @expose_to_ruby end + + def expose_location? + @expose_location + end end class << self From 1a2294b88914678cb87b67f3533890f1abd60067 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 17 Dec 2024 13:42:06 -0500 Subject: [PATCH 056/111] =?UTF-8?q?Don=E2=80=99t=20copy=20Ruby=20strings?= =?UTF-8?q?=20into=20extension?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ext/rbs_extension/main.c | 22 +++++++++++++++++++--- ext/rbs_extension/rbs_string_bridging.c | 8 +------- ext/rbs_extension/rbs_string_bridging.h | 3 ++- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index bd4fe778b..9800636cb 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -173,7 +173,12 @@ static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VAL .parser = parser, .require_eof = require_eof }; - return rb_ensure(parse_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser); + + VALUE result = rb_ensure(parse_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser); + + RB_GC_GUARD(string); + + return result; } static VALUE parse_method_type_try(VALUE a) { @@ -219,7 +224,12 @@ static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_p .parser = parser, .require_eof = require_eof }; - return rb_ensure(parse_method_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser); + + VALUE result = rb_ensure(parse_method_type_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser); + + RB_GC_GUARD(string); + + return result; } static VALUE parse_signature_try(VALUE a) { @@ -252,7 +262,12 @@ static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos .parser = parser, .require_eof = false }; - return rb_ensure(parse_signature_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser); + + VALUE result = rb_ensure(parse_signature_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser); + + RB_GC_GUARD(string); + + return result; } static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) { @@ -275,6 +290,7 @@ static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) { } rbs_allocator_free(&allocator); + RB_GC_GUARD(string); return results; } diff --git a/ext/rbs_extension/rbs_string_bridging.c b/ext/rbs_extension/rbs_string_bridging.c index 48282dea8..c75b5abfb 100644 --- a/ext/rbs_extension/rbs_string_bridging.c +++ b/ext/rbs_extension/rbs_string_bridging.c @@ -1,13 +1,7 @@ #include "rbs_string_bridging.h" rbs_string_t rbs_string_from_ruby_string(VALUE ruby_string) { - rb_gc_register_mark_object(ruby_string); - - rbs_string_t s = rbs_string_shared_new(StringValueCStr(ruby_string), RSTRING_END(ruby_string)); - - rbs_string_ensure_owned(&s); // Copy out the string so we don't need the Ruby object to stay alive. - - return s; + return rbs_string_shared_new(StringValueCStr(ruby_string), RSTRING_END(ruby_string)); } VALUE rbs_string_to_ruby_string(rbs_string_t *self, rb_encoding *encoding) { diff --git a/ext/rbs_extension/rbs_string_bridging.h b/ext/rbs_extension/rbs_string_bridging.h index 3acc1bc26..54afe563b 100644 --- a/ext/rbs_extension/rbs_string_bridging.h +++ b/ext/rbs_extension/rbs_string_bridging.h @@ -7,7 +7,8 @@ #include "rbs/rbs_string.h" /** - * Returns a new shared rbs_string_t from the given Ruby string. + * @returns A new shared rbs_string_t from the given Ruby string, which points into the given Ruby String's memory, + * and does not need to be `free()`ed. However, the Ruby String needs to be kept alive for the duration of the rbs_string_t. */ rbs_string_t rbs_string_from_ruby_string(VALUE ruby_string); From 7704c78f47076a425d7ab6513a5eee907bde5ae9 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 17 Dec 2024 14:19:01 -0500 Subject: [PATCH 057/111] Add `rbs_string_copy_slice()` --- include/rbs/rbs_string.h | 8 ++++++++ src/rbs_string.c | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/include/rbs/rbs_string.h b/include/rbs/rbs_string.h index c5d4b96fb..682b4ee0f 100644 --- a/include/rbs/rbs_string.h +++ b/include/rbs/rbs_string.h @@ -34,6 +34,14 @@ rbs_string_t rbs_string_shared_new(const char *start, const char *end); */ rbs_string_t rbs_string_owned_new(const char *start, const char *end); +/** + * Copies a portion of the input string into a new owned string. + * @param start_inset Number of characters to exclude from the start + * @param length Number of characters to include + * @return A new owned string that needs to be freed using `rbs_string_free()`. + */ +rbs_string_t rbs_string_copy_slice(rbs_string_t *self, size_t start_inset, size_t length); + /** * Ensures that the given string is owned, so that it manages its own memory, uncoupled from its original source. */ diff --git a/src/rbs_string.c b/src/rbs_string.c index a0b13c747..7a7096f27 100644 --- a/src/rbs_string.c +++ b/src/rbs_string.c @@ -21,6 +21,14 @@ rbs_string_t rbs_string_owned_new(const char *start, const char *end) { }; } +rbs_string_t rbs_string_copy_slice(rbs_string_t *self, size_t start_inset, size_t length) { + char *buffer = (char *) malloc(length + 1); + strncpy(buffer, self->start + start_inset, length); + buffer[length] = '\0'; + + return rbs_string_owned_new(buffer, buffer + length); +} + void rbs_string_ensure_owned(rbs_string_t *self) { if (self->type == RBS_STRING_OWNED) return; From 9c7d8da8b69419e99a74651b71c2b6005dec1154 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 3 Jan 2025 12:13:01 -0500 Subject: [PATCH 058/111] Fix string handling in preperation for `free()` --- include/rbs/rbs_string.h | 33 --------------------------------- include/rbs/rbs_unescape.h | 2 ++ src/parser.c | 35 ++++++++++++++++++----------------- src/rbs_buffer.c | 2 +- src/rbs_string.c | 16 ---------------- src/rbs_unescape.c | 7 ++----- 6 files changed, 23 insertions(+), 72 deletions(-) diff --git a/include/rbs/rbs_string.h b/include/rbs/rbs_string.h index 682b4ee0f..4b17c40ac 100644 --- a/include/rbs/rbs_string.h +++ b/include/rbs/rbs_string.h @@ -42,39 +42,6 @@ rbs_string_t rbs_string_owned_new(const char *start, const char *end); */ rbs_string_t rbs_string_copy_slice(rbs_string_t *self, size_t start_inset, size_t length); -/** - * Ensures that the given string is owned, so that it manages its own memory, uncoupled from its original source. - */ -void rbs_string_ensure_owned(rbs_string_t *self); - -/** - * Returns a new `rbs_string_t` with its start shifted forward by the given amount. - * This returns a shared string which points to the same memory as the original string. - */ -rbs_string_t rbs_string_offset(const rbs_string_t self, size_t offset); - -/** - * Modifies the given string to drop its first `n` characters. - */ -void rbs_string_drop_first(rbs_string_t *self, size_t n); - -/** - * Modifies the given string to drop its last `n` characters. - */ -void rbs_string_drop_last(rbs_string_t *self, size_t n); - -/** - * Modifies the given string to limit its length to the given number of characters. - */ -void rbs_string_limit_length(rbs_string_t *self, size_t new_length); - -/** - * Copies a portion of the input string into a new owned string. - * @param start_inset Number of characters to exclude from the start - * @param length Number of characters to include - */ -rbs_string_t rbs_string_slice(const rbs_string_t self, size_t start_inset, size_t length); - /** * Drops the leading and trailing whitespace from the given string, in-place. */ diff --git a/include/rbs/rbs_unescape.h b/include/rbs/rbs_unescape.h index a74cef74b..22f3938fa 100644 --- a/include/rbs/rbs_unescape.h +++ b/include/rbs/rbs_unescape.h @@ -13,6 +13,8 @@ * `bar` | bar * :"baz\\t" | baz\t * :'baz' | baz + * + * @returns A new owned string that needs to be freed with `rbs_string_free()` * */ rbs_string_t rbs_unquote_string(rbs_string_t input); diff --git a/src/parser.c b/src/parser.c index de768f906..d7af16c7c 100644 --- a/src/parser.c +++ b/src/parser.c @@ -112,12 +112,11 @@ static bool parse_simple(parserstate *state, rbs_node_t **type); static rbs_string_t rbs_parser_get_current_token(parserstate *state) { range rg = state->current_token.range; - rbs_string_t string = state->lexstate->string; - rbs_string_drop_first(&string, rg.start.byte_pos); - rbs_string_limit_length(&string, rg.end.byte_pos - rg.start.byte_pos); - rbs_string_ensure_owned(&string); + size_t start_inset = rg.start.byte_pos; + size_t length = rg.end.byte_pos - rg.start.byte_pos; - return string; + // TODO: return a borrowed view into the file, without copying. + return rbs_string_copy_slice(&state->lexstate->string, start_inset, length); } static rbs_constant_id_t rbs_constant_pool_insert_string(rbs_constant_pool_t *self, rbs_string_t string) { @@ -919,16 +918,17 @@ static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types case tDQSYMBOL: case tSQSYMBOL: { rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); - rbs_string_t string = rbs_parser_get_current_token(state); - rbs_string_drop_first(&string, offset_bytes); + rbs_string_t current_token = rbs_parser_get_current_token(state); - rbs_string_t unquoted_str = rbs_unquote_string(string); + rbs_string_t symbol = rbs_string_copy_slice(¤t_token, offset_bytes, rbs_string_len(current_token) - offset_bytes); + + rbs_string_t unquoted_symbol = rbs_unquote_string(symbol); literal = rbs_ast_symbol_new( &state->allocator, symbolLoc, &state->constant_pool, - rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str) + rbs_constant_pool_insert_string(&state->constant_pool, unquoted_symbol) ); break; } @@ -1108,7 +1108,6 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { rbs_string_t string = rbs_parser_get_current_token(state); rbs_string_strip_whitespace(&string); - rbs_string_ensure_owned(&string); rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(&state->allocator, loc, string); *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, loc, literal); @@ -1585,15 +1584,17 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati size_t open_bytes = state->lexstate->encoding->char_width((const uint8_t *) &open_char, (size_t) 1); size_t close_bytes = state->lexstate->encoding->char_width((const uint8_t *) &close_char, (size_t) 1); - rbs_string_t string = rbs_parser_get_current_token(state); - rbs_string_drop_first(&string, offset_bytes + open_bytes); - rbs_string_limit_length(&string, rbs_string_len(string) - close_bytes); - rbs_string_strip_whitespace(&string); - rbs_string_ensure_owned(&string); + rbs_string_t current_token = rbs_parser_get_current_token(state); + size_t total_offset = offset_bytes + open_bytes; - rbs_location_t *loc = rbs_location_new(rg); + rbs_string_t annotation_str = rbs_string_copy_slice( + ¤t_token, + total_offset, + rbs_string_len(current_token) - total_offset - close_bytes + ); + rbs_string_strip_whitespace(&annotation_str); - *annotation = rbs_ast_annotation_new(&state->allocator, loc, string); + *annotation = rbs_ast_annotation_new(&state->allocator, rbs_location_new(rg), annotation_str); return true; } diff --git a/src/rbs_buffer.c b/src/rbs_buffer.c index 43a894a74..164dd9091 100644 --- a/src/rbs_buffer.c +++ b/src/rbs_buffer.c @@ -56,7 +56,7 @@ void rbs_buffer_append_string(rbs_buffer_t *buffer, const char *value, size_t le } rbs_string_t rbs_buffer_to_string(rbs_buffer_t *buffer) { - return rbs_string_owned_new(buffer->value, buffer->value + buffer->length); + return rbs_string_shared_new(buffer->value, buffer->value + buffer->length); } void rbs_buffer_free(rbs_buffer_t *buffer) { diff --git a/src/rbs_string.c b/src/rbs_string.c index 7a7096f27..f8cf66e66 100644 --- a/src/rbs_string.c +++ b/src/rbs_string.c @@ -29,22 +29,6 @@ rbs_string_t rbs_string_copy_slice(rbs_string_t *self, size_t start_inset, size_ return rbs_string_owned_new(buffer, buffer + length); } -void rbs_string_ensure_owned(rbs_string_t *self) { - if (self->type == RBS_STRING_OWNED) return; - - char *buffer = (char *)malloc(self->end - self->start + 1); - size_t length = self->end - self->start; - - strncpy(buffer, self->start, length); - buffer[length] = '\0'; - - *self = rbs_string_owned_new(buffer, buffer + length); -} - -rbs_string_t rbs_string_offset(const rbs_string_t self, const size_t offset) { - return rbs_string_shared_new(self.start + offset, self.end); -} - // // Ensure the given string is shared, so that we can slice it without needing to free the old string. // static void ensure_shared(rbs_string_t *self) { // if (self->type != RBS_STRING_SHARED) { diff --git a/src/rbs_unescape.c b/src/rbs_unescape.c index 808053dfa..de5125854 100644 --- a/src/rbs_unescape.c +++ b/src/rbs_unescape.c @@ -104,9 +104,7 @@ rbs_string_t unescape_string(const rbs_string_t string, bool is_double_quote) { } } output[j] = '\0'; - rbs_string_t str = rbs_string_shared_new(output, output + j); - rbs_string_ensure_owned(&str); - return str; + return rbs_string_owned_new(output, output + j); } rbs_string_t rbs_unquote_string(rbs_string_t input) { @@ -120,7 +118,6 @@ rbs_string_t rbs_unquote_string(rbs_string_t input) { byte_length -= 2 * bs; } - rbs_string_t string = rbs_string_offset(input, start_offset); - string.end = string.start + byte_length; + rbs_string_t string = rbs_string_copy_slice(&input, start_offset, byte_length); return unescape_string(string, first_char == '"'); } From 91e40ff78cf886900c8864fa48db7597adfdc002 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 17 Dec 2024 14:18:22 -0500 Subject: [PATCH 059/111] Delete `rbs_string_offset()` --- src/rbs_string.c | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/rbs_string.c b/src/rbs_string.c index f8cf66e66..b86e097a8 100644 --- a/src/rbs_string.c +++ b/src/rbs_string.c @@ -55,22 +55,6 @@ void rbs_string_limit_length(rbs_string_t *self, size_t new_length) { self->end = self->start + new_length; } -rbs_string_t rbs_string_slice(const rbs_string_t self, size_t start_inset, size_t length) { - if (length > rbs_string_len(self)) { - fprintf(stderr, "rbs_string_slice tried to slice more characters than exist in the string.\n"); - exit(EXIT_FAILURE); - } - - if (self.start + start_inset + length >= self.end) { - fprintf(stderr, "rbs_string_slice tried to slice past the end of the string.\n"); - exit(EXIT_FAILURE); - } - - const char *new_start = self.start + start_inset; - - return rbs_string_shared_new(new_start, new_start + length); -} - void rbs_string_strip_whitespace(rbs_string_t *self) { // ensure_shared(self); From 8d5aeb4a348a530a819c4f57b38082e0fc89738e Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 17 Dec 2024 15:30:12 -0500 Subject: [PATCH 060/111] `rbs_string_strip_whitespace()` without copying --- include/rbs/rbs_string.h | 3 ++- src/parser.c | 9 +++++---- src/rbs_string.c | 8 +++----- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/rbs/rbs_string.h b/include/rbs/rbs_string.h index 4b17c40ac..2b0bb65ce 100644 --- a/include/rbs/rbs_string.h +++ b/include/rbs/rbs_string.h @@ -44,8 +44,9 @@ rbs_string_t rbs_string_copy_slice(rbs_string_t *self, size_t start_inset, size_ /** * Drops the leading and trailing whitespace from the given string, in-place. + * @returns A new owned string that needs to be freed with `rbs_string_free()` */ -void rbs_string_strip_whitespace(rbs_string_t *self); +rbs_string_t rbs_string_strip_whitespace(rbs_string_t *self); /** * Returns the length of the string. diff --git a/src/parser.c b/src/parser.c index d7af16c7c..a343cbd1c 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1107,9 +1107,9 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { rbs_location_t *loc = rbs_location_current_token(state); rbs_string_t string = rbs_parser_get_current_token(state); - rbs_string_strip_whitespace(&string); + rbs_string_t stripped_string = rbs_string_strip_whitespace(&string); - rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(&state->allocator, loc, string); + rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(&state->allocator, loc, stripped_string); *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, loc, literal); return true; } @@ -1592,9 +1592,10 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati total_offset, rbs_string_len(current_token) - total_offset - close_bytes ); - rbs_string_strip_whitespace(&annotation_str); - *annotation = rbs_ast_annotation_new(&state->allocator, rbs_location_new(rg), annotation_str); + rbs_string_t stripped_annotation_str = rbs_string_strip_whitespace(&annotation_str); + + *annotation = rbs_ast_annotation_new(&state->allocator, rbs_location_new(rg), stripped_annotation_str); return true; } diff --git a/src/rbs_string.c b/src/rbs_string.c index b86e097a8..f3228db28 100644 --- a/src/rbs_string.c +++ b/src/rbs_string.c @@ -55,7 +55,7 @@ void rbs_string_limit_length(rbs_string_t *self, size_t new_length) { self->end = self->start + new_length; } -void rbs_string_strip_whitespace(rbs_string_t *self) { +rbs_string_t rbs_string_strip_whitespace(rbs_string_t *self) { // ensure_shared(self); const char *new_start = self->start; @@ -64,8 +64,7 @@ void rbs_string_strip_whitespace(rbs_string_t *self) { } if (new_start == self->end) { // Handle empty string case - self->start = new_start; - return; + return rbs_string_shared_new(new_start, new_start); } const char *new_end = self->end - 1; @@ -73,8 +72,7 @@ void rbs_string_strip_whitespace(rbs_string_t *self) { new_end--; } - self->start = new_start; - self->end = new_end + 1; + return rbs_string_copy_slice(self, new_start - self->start, new_end - new_start + 1); } size_t rbs_string_len(const rbs_string_t self) { From 1607efdea539c37be53d0c8327f3dad01b7b5edd Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 17 Dec 2024 11:12:46 -0500 Subject: [PATCH 061/111] `rbs_parser_peek_current_token()` without copying --- src/parser.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/parser.c b/src/parser.c index a343cbd1c..04a0bab80 100644 --- a/src/parser.c +++ b/src/parser.c @@ -109,14 +109,16 @@ static rbs_location_t *rbs_location_current_token(parserstate *state) { static bool parse_optional(parserstate *state, rbs_node_t **optional); static bool parse_simple(parserstate *state, rbs_node_t **type); -static rbs_string_t rbs_parser_get_current_token(parserstate *state) { +/** + * @returns A borrowed copy of the current token, which does *not* need to be freed. + */ +static rbs_string_t rbs_parser_peek_current_token(parserstate *state) { range rg = state->current_token.range; - size_t start_inset = rg.start.byte_pos; + const char *start = state->lexstate->string.start + rg.start.byte_pos; size_t length = rg.end.byte_pos - rg.start.byte_pos; - // TODO: return a borrowed view into the file, without copying. - return rbs_string_copy_slice(&state->lexstate->string, start_inset, length); + return rbs_string_shared_new(start, start + length); } static rbs_constant_id_t rbs_constant_pool_insert_string(rbs_constant_pool_t *self, rbs_string_t string) { @@ -322,7 +324,7 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t return false; } - rbs_string_t unquoted_str = rbs_unquote_string(rbs_parser_get_current_token(state)); + rbs_string_t unquoted_str = rbs_unquote_string(rbs_parser_peek_current_token(state)); rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); @@ -918,7 +920,7 @@ static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types case tDQSYMBOL: case tSQSYMBOL: { rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); - rbs_string_t current_token = rbs_parser_get_current_token(state); + rbs_string_t current_token = rbs_parser_peek_current_token(state); rbs_string_t symbol = rbs_string_copy_slice(¤t_token, offset_bytes, rbs_string_len(current_token) - offset_bytes); @@ -1106,7 +1108,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { case tINTEGER: { rbs_location_t *loc = rbs_location_current_token(state); - rbs_string_t string = rbs_parser_get_current_token(state); + rbs_string_t string = rbs_parser_peek_current_token(state); rbs_string_t stripped_string = rbs_string_strip_whitespace(&string); rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(&state->allocator, loc, stripped_string); @@ -1127,7 +1129,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { case tDQSTRING: { rbs_location_t *loc = rbs_location_current_token(state); - rbs_string_t unquoted_str = rbs_unquote_string(rbs_parser_get_current_token(state)); + rbs_string_t unquoted_str = rbs_unquote_string(rbs_parser_peek_current_token(state)); rbs_node_t *literal = (rbs_node_t *) rbs_ast_string_new(&state->allocator, loc, unquoted_str); *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, loc, literal); return true; @@ -1333,7 +1335,7 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa ADVANCE_ASSERT(state, tUIDENT); range name_range = state->current_token.range; - rbs_string_t string = rbs_parser_get_current_token(state); + rbs_string_t string = rbs_parser_peek_current_token(state); rbs_location_t *nameSymbolLoc = rbs_location_new(state->current_token.range); rbs_constant_id_t id = rbs_constant_pool_insert_string(&state->constant_pool, string); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, nameSymbolLoc, &state->constant_pool, id); @@ -1584,7 +1586,7 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati size_t open_bytes = state->lexstate->encoding->char_width((const uint8_t *) &open_char, (size_t) 1); size_t close_bytes = state->lexstate->encoding->char_width((const uint8_t *) &close_char, (size_t) 1); - rbs_string_t current_token = rbs_parser_get_current_token(state); + rbs_string_t current_token = rbs_parser_peek_current_token(state); size_t total_offset = offset_bytes + open_bytes; rbs_string_t annotation_str = rbs_string_copy_slice( @@ -1670,7 +1672,7 @@ static bool parse_method_name(parserstate *state, range *range, rbs_ast_symbol_t return true; } case tQIDENT: { - rbs_string_t string = rbs_parser_get_current_token(state); + rbs_string_t string = rbs_parser_peek_current_token(state); rbs_string_t unquoted_str = rbs_unquote_string(string); rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str); rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); From e5cc95a99fbb09236b2479a1a7ffe246258ebf1d Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 17 Dec 2024 12:19:50 -0500 Subject: [PATCH 062/111] Copy RBS Strings into Ruby Instead of using `rb_str_new_static` to reference immortal RBS strings. --- ext/rbs_extension/rbs_string_bridging.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ext/rbs_extension/rbs_string_bridging.c b/ext/rbs_extension/rbs_string_bridging.c index c75b5abfb..b7315091b 100644 --- a/ext/rbs_extension/rbs_string_bridging.c +++ b/ext/rbs_extension/rbs_string_bridging.c @@ -5,7 +5,5 @@ rbs_string_t rbs_string_from_ruby_string(VALUE ruby_string) { } VALUE rbs_string_to_ruby_string(rbs_string_t *self, rb_encoding *encoding) { - VALUE str = rb_str_new_static(self->start, rbs_string_len(*self)); - rb_enc_associate(str, encoding); - return str; + return rb_enc_str_new(self->start, rbs_string_len(*self), encoding); } From 0af6a57d4abcdaa053297586a68367908993ed7f Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 17 Dec 2024 14:33:58 -0500 Subject: [PATCH 063/111] Delete `rbs_string_offset()` --- include/rbs/rbs_string.h | 16 ++++++++++++++++ src/rbs_string.c | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/rbs/rbs_string.h b/include/rbs/rbs_string.h index 2b0bb65ce..3f5bf1882 100644 --- a/include/rbs/rbs_string.h +++ b/include/rbs/rbs_string.h @@ -42,6 +42,22 @@ rbs_string_t rbs_string_owned_new(const char *start, const char *end); */ rbs_string_t rbs_string_copy_slice(rbs_string_t *self, size_t start_inset, size_t length); +/** + * Free the associated memory of the given string if it is owned, otherwise does nothing. + * + * @param string The string to free. + * \public \memberof rbs_string_t + */ +void rbs_string_free_if_needed(rbs_string_t *self); + +/** + * Free the associated memory of the given string if it is owned, otherwise fails (exits the program). + * + * @param string The string to free. + * \public \memberof rbs_string_t + */ +void rbs_string_free(rbs_string_t *self); + /** * Drops the leading and trailing whitespace from the given string, in-place. * @returns A new owned string that needs to be freed with `rbs_string_free()` diff --git a/src/rbs_string.c b/src/rbs_string.c index f3228db28..116959193 100644 --- a/src/rbs_string.c +++ b/src/rbs_string.c @@ -29,6 +29,21 @@ rbs_string_t rbs_string_copy_slice(rbs_string_t *self, size_t start_inset, size_ return rbs_string_owned_new(buffer, buffer + length); } +void rbs_string_free_if_needed(rbs_string_t *self) { + if (self->type == RBS_STRING_OWNED) { + rbs_string_free(self); + } +} + +void rbs_string_free(rbs_string_t *self) { + if (self->type != RBS_STRING_OWNED) { + fprintf(stderr, "rbs_string_free(%p): not owned\n", self->start); + exit(EXIT_FAILURE); + } + + free((void *) self->start); +} + // // Ensure the given string is shared, so that we can slice it without needing to free the old string. // static void ensure_shared(rbs_string_t *self) { // if (self->type != RBS_STRING_SHARED) { From 1b021fe180d6c28ba1610758006adcf96a5d888d Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 17 Dec 2024 16:17:07 -0500 Subject: [PATCH 064/111] Free intermediate strings in `parser.c` --- src/parser.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/parser.c b/src/parser.c index 04a0bab80..5399c579f 100644 --- a/src/parser.c +++ b/src/parser.c @@ -925,13 +925,12 @@ static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types rbs_string_t symbol = rbs_string_copy_slice(¤t_token, offset_bytes, rbs_string_len(current_token) - offset_bytes); rbs_string_t unquoted_symbol = rbs_unquote_string(symbol); + rbs_string_free(&symbol); - literal = rbs_ast_symbol_new( - &state->allocator, - symbolLoc, - &state->constant_pool, - rbs_constant_pool_insert_string(&state->constant_pool, unquoted_symbol) - ); + rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&state->constant_pool, unquoted_symbol); + // rbs_string_free(&unquoted_symbol); + + literal = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); break; } default: @@ -1596,6 +1595,7 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati ); rbs_string_t stripped_annotation_str = rbs_string_strip_whitespace(&annotation_str); + rbs_string_free(&annotation_str); *annotation = rbs_ast_annotation_new(&state->allocator, rbs_location_new(rg), stripped_annotation_str); return true; From 7cc7a7d7985d819d1b3825bf96dd5bcb03b66a8d Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 14 Feb 2025 18:45:38 -0500 Subject: [PATCH 065/111] Add `rbs_node_destroy()` --- ext/rbs_extension/main.c | 12 +- include/rbs/ast.h | 2 + src/ast.c | 833 +++++++++++++++++++++++++++++--- templates/include/rbs/ast.h.erb | 2 + templates/src/ast.c.erb | 42 +- templates/template.rb | 13 +- 6 files changed, 829 insertions(+), 75 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 9800636cb..1bba1d7b3 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -121,7 +121,9 @@ static VALUE parse_type_try(VALUE a) { ); - return rbs_struct_to_ruby_value(ctx, type); + VALUE ruby_ast = rbs_struct_to_ruby_value(ctx, type); + rbs_node_destroy((rbs_node_t *) type); + return ruby_ast; } static lexstate *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE string, rb_encoding *encoding, int start_pos, int end_pos) { @@ -208,7 +210,9 @@ static VALUE parse_method_type_try(VALUE a) { arg->encoding ); - return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type); + VALUE ruby_ast = rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type); + rbs_node_destroy((rbs_node_t *) method_type); + return ruby_ast; } static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) { @@ -247,7 +251,9 @@ static VALUE parse_signature_try(VALUE a) { arg->encoding ); - return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature); + VALUE ruby_ast = rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature); + rbs_node_destroy((rbs_node_t *) signature); + return ruby_ast; } static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) { diff --git a/include/rbs/ast.h b/include/rbs/ast.h index 2d94df560..bc961c365 100644 --- a/include/rbs/ast.h +++ b/include/rbs/ast.h @@ -681,4 +681,6 @@ rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_location_ rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *return_type); rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name); +void rbs_node_destroy(rbs_node_t *any_node); + #endif diff --git a/src/ast.c b/src/ast.c index 91447fc73..883ae1e45 100644 --- a/src/ast.c +++ b/src/ast.c @@ -5,6 +5,7 @@ /* templates/src/ast.c.erb */ /*----------------------------------------------------------------------------*/ +#line 2 "prism/templates/src/ast.c.erb" #include "rbs/ast.h" #include @@ -214,6 +215,7 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_location_t return instance; } +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string) { rbs_ast_annotation_t *instance = rbs_allocator_alloc(allocator, rbs_ast_annotation_t); @@ -228,7 +230,7 @@ rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_loc return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, rbs_location_t *location, bool value) { rbs_ast_bool_t *instance = rbs_allocator_alloc(allocator, rbs_ast_bool_t); @@ -243,7 +245,7 @@ rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, rbs_location_t *loc return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string) { rbs_ast_comment_t *instance = rbs_allocator_alloc(allocator, rbs_ast_comment_t); @@ -258,7 +260,7 @@ rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, rbs_location_ return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_class_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_t); @@ -278,7 +280,7 @@ rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *al return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { rbs_ast_declarations_class_super_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_super_t); @@ -294,7 +296,7 @@ rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_all return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_ast_comment_t *comment) { rbs_ast_declarations_classalias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_classalias_t); @@ -311,7 +313,7 @@ rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_alloc return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_declarations_constant_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_constant_t); @@ -328,7 +330,7 @@ rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_declarations_global_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_global_t); @@ -345,7 +347,7 @@ rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t * return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_interface_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_interface_t); @@ -364,7 +366,7 @@ rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocat return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_module_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_t); @@ -384,7 +386,7 @@ rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t * return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { rbs_ast_declarations_module_self_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_self_t); @@ -400,7 +402,7 @@ rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_all return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_ast_comment_t *comment) { rbs_ast_declarations_modulealias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_modulealias_t); @@ -417,7 +419,7 @@ rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_all return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_typealias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_typealias_t); @@ -436,7 +438,7 @@ rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocat return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *clauses) { rbs_ast_directives_use_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_t); @@ -451,7 +453,7 @@ rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *type_name, rbs_ast_symbol_t *new_name) { rbs_ast_directives_use_singleclause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_singleclause_t); @@ -467,7 +469,7 @@ rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(r return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace) { rbs_ast_directives_use_wildcardclause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_wildcardclause_t); @@ -482,7 +484,7 @@ rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_n return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_integer_t *rbs_ast_integer_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string_representation) { rbs_ast_integer_t *instance = rbs_allocator_alloc(allocator, rbs_ast_integer_t); @@ -497,7 +499,7 @@ rbs_ast_integer_t *rbs_ast_integer_new(rbs_allocator_t *allocator, rbs_location_ return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_alias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_alias_t); @@ -516,7 +518,7 @@ rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, r return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { rbs_ast_members_attraccessor_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attraccessor_t); @@ -537,7 +539,7 @@ rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { rbs_ast_members_attrreader_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attrreader_t); @@ -558,7 +560,7 @@ rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *al return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { rbs_ast_members_attrwriter_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attrwriter_t); @@ -579,7 +581,7 @@ rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *al return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_members_classinstancevariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_classinstancevariable_t); @@ -596,7 +598,7 @@ rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_n return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_members_classvariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_classvariable_t); @@ -613,7 +615,7 @@ rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_extend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_extend_t); @@ -631,7 +633,7 @@ rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_include_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_include_t); @@ -649,7 +651,7 @@ rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocato return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_members_instancevariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_instancevariable_t); @@ -666,7 +668,7 @@ rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_all return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, bool overloading, rbs_keyword_t *visibility) { rbs_ast_members_methoddefinition_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_methoddefinition_t); @@ -687,7 +689,7 @@ rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_all return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *annotations, rbs_node_t *method_type) { rbs_ast_members_methoddefinition_overload_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_methoddefinition_overload_t); @@ -703,7 +705,7 @@ rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_ov return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_prepend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_prepend_t); @@ -721,7 +723,7 @@ rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocato return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_ast_members_private_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_private_t); @@ -735,7 +737,7 @@ rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocato return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_ast_members_public_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_public_t); @@ -749,7 +751,7 @@ rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_string_t *rbs_ast_string_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string) { rbs_ast_string_t *instance = rbs_allocator_alloc(allocator, rbs_ast_string_t); @@ -764,7 +766,7 @@ rbs_ast_string_t *rbs_ast_string_new(rbs_allocator_t *allocator, rbs_location_t return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked) { rbs_ast_typeparam_t *instance = rbs_allocator_alloc(allocator, rbs_ast_typeparam_t); @@ -783,7 +785,7 @@ rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_locat return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block) { rbs_methodtype_t *instance = rbs_allocator_alloc(allocator, rbs_methodtype_t); @@ -800,7 +802,7 @@ rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_location_t return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *path, bool absolute) { rbs_namespace_t *instance = rbs_allocator_alloc(allocator, rbs_namespace_t); @@ -816,7 +818,7 @@ rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_location_t *l return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *directives, rbs_node_list_t *declarations) { rbs_signature_t *instance = rbs_allocator_alloc(allocator, rbs_signature_t); @@ -832,7 +834,7 @@ rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_location_t *l return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace, rbs_ast_symbol_t *name) { rbs_typename_t *instance = rbs_allocator_alloc(allocator, rbs_typename_t); @@ -848,7 +850,7 @@ rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_location_t *loc return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { rbs_types_alias_t *instance = rbs_allocator_alloc(allocator, rbs_types_alias_t); @@ -864,7 +866,7 @@ rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_location_ return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, rbs_location_t *location, bool todo) { rbs_types_bases_any_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_any_t); @@ -879,7 +881,7 @@ rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, rbs_l return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_bool_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_bool_t); @@ -893,7 +895,7 @@ rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, rbs return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_bottom_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_bottom_t); @@ -907,7 +909,7 @@ rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_class_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_class_t); @@ -921,7 +923,7 @@ rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, r return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_instance_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_instance_t); @@ -935,7 +937,7 @@ rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *alloca return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_nil_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_nil_t); @@ -949,7 +951,7 @@ rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, rbs_l return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_self_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_self_t); @@ -963,7 +965,7 @@ rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, rbs return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_top_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_top_t); @@ -977,7 +979,7 @@ rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, rbs_l return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_void_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_void_t); @@ -991,7 +993,7 @@ rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, rbs return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, bool required, rbs_node_t *self_type) { rbs_types_block_t *instance = rbs_allocator_alloc(allocator, rbs_types_block_t); @@ -1008,7 +1010,7 @@ rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_location_ return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { rbs_types_classinstance_t *instance = rbs_allocator_alloc(allocator, rbs_types_classinstance_t); @@ -1024,7 +1026,7 @@ rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocato return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name) { rbs_types_classsingleton_t *instance = rbs_allocator_alloc(allocator, rbs_types_classsingleton_t); @@ -1039,7 +1041,7 @@ rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *alloca return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *required_positionals, rbs_node_list_t *optional_positionals, rbs_node_t *rest_positionals, rbs_node_list_t *trailing_positionals, rbs_hash_t *required_keywords, rbs_hash_t *optional_keywords, rbs_node_t *rest_keywords, rbs_node_t *return_type) { rbs_types_function_t *instance = rbs_allocator_alloc(allocator, rbs_types_function_t); @@ -1061,7 +1063,7 @@ rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, rbs_loc return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, rbs_ast_symbol_t *name) { rbs_types_function_param_t *instance = rbs_allocator_alloc(allocator, rbs_types_function_param_t); @@ -1077,7 +1079,7 @@ rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *alloca return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { rbs_types_interface_t *instance = rbs_allocator_alloc(allocator, rbs_types_interface_t); @@ -1093,7 +1095,7 @@ rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_l return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types) { rbs_types_intersection_t *instance = rbs_allocator_alloc(allocator, rbs_types_intersection_t); @@ -1108,7 +1110,7 @@ rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *literal) { rbs_types_literal_t *instance = rbs_allocator_alloc(allocator, rbs_types_literal_t); @@ -1123,7 +1125,7 @@ rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, rbs_locat return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type) { rbs_types_optional_t *instance = rbs_allocator_alloc(allocator, rbs_types_optional_t); @@ -1138,7 +1140,7 @@ rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_loc return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, rbs_types_block_t *block, rbs_node_t *self_type) { rbs_types_proc_t *instance = rbs_allocator_alloc(allocator, rbs_types_proc_t); @@ -1155,7 +1157,7 @@ rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_location_t return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_hash_t *all_fields) { rbs_types_record_t *instance = rbs_allocator_alloc(allocator, rbs_types_record_t); @@ -1170,7 +1172,7 @@ rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, rbs_locatio return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_record_fieldtype_t *rbs_types_record_fieldtype_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, bool required) { rbs_types_record_fieldtype_t *instance = rbs_allocator_alloc(allocator, rbs_types_record_fieldtype_t); @@ -1186,7 +1188,7 @@ rbs_types_record_fieldtype_t *rbs_types_record_fieldtype_new(rbs_allocator_t *al return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types) { rbs_types_tuple_t *instance = rbs_allocator_alloc(allocator, rbs_types_tuple_t); @@ -1201,7 +1203,7 @@ rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_location_ return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types) { rbs_types_union_t *instance = rbs_allocator_alloc(allocator, rbs_types_union_t); @@ -1216,7 +1218,7 @@ rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_location_ return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *return_type) { rbs_types_untypedfunction_t *instance = rbs_allocator_alloc(allocator, rbs_types_untypedfunction_t); @@ -1231,7 +1233,7 @@ rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allo return instance; } - +#line 165 "prism/templates/src/ast.c.erb" rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name) { rbs_types_variable_t *instance = rbs_allocator_alloc(allocator, rbs_types_variable_t); @@ -1247,3 +1249,710 @@ rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_loc return instance; } +#line 187 "prism/templates/src/ast.c.erb" +void rbs_node_destroy(rbs_node_t *any_node) { + switch (any_node->type) { +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_ANNOTATION: { + rbs_ast_annotation_t *node = (rbs_ast_annotation_t *)any_node; + + rbs_string_free_if_needed(&node->string); + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_BOOL: { + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_COMMENT: { + rbs_ast_comment_t *node = (rbs_ast_comment_t *)any_node; + + rbs_string_free_if_needed(&node->string); + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_DECLARATIONS_CLASS: { + rbs_ast_declarations_class_t *node = (rbs_ast_declarations_class_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + rbs_node_list_free(node->type_params); + if (node->super_class != NULL) { + rbs_node_destroy((rbs_node_t *) node->super_class); + } + rbs_node_list_free(node->members); + rbs_node_list_free(node->annotations); + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_DECLARATIONS_CLASS_SUPER: { + rbs_ast_declarations_class_super_t *node = (rbs_ast_declarations_class_super_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + rbs_node_list_free(node->args); + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_DECLARATIONS_CLASSALIAS: { + rbs_ast_declarations_classalias_t *node = (rbs_ast_declarations_classalias_t *)any_node; + + if (node->new_name != NULL) { + rbs_node_destroy((rbs_node_t *) node->new_name); + } + if (node->old_name != NULL) { + rbs_node_destroy((rbs_node_t *) node->old_name); + } + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_DECLARATIONS_CONSTANT: { + rbs_ast_declarations_constant_t *node = (rbs_ast_declarations_constant_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + if (node->type != NULL) { + rbs_node_destroy((rbs_node_t *) node->type); + } + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_DECLARATIONS_GLOBAL: { + rbs_ast_declarations_global_t *node = (rbs_ast_declarations_global_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + if (node->type != NULL) { + rbs_node_destroy((rbs_node_t *) node->type); + } + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_DECLARATIONS_INTERFACE: { + rbs_ast_declarations_interface_t *node = (rbs_ast_declarations_interface_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + rbs_node_list_free(node->type_params); + rbs_node_list_free(node->members); + rbs_node_list_free(node->annotations); + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_DECLARATIONS_MODULE: { + rbs_ast_declarations_module_t *node = (rbs_ast_declarations_module_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + rbs_node_list_free(node->type_params); + rbs_node_list_free(node->self_types); + rbs_node_list_free(node->members); + rbs_node_list_free(node->annotations); + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_DECLARATIONS_MODULE_SELF: { + rbs_ast_declarations_module_self_t *node = (rbs_ast_declarations_module_self_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + rbs_node_list_free(node->args); + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_DECLARATIONS_MODULEALIAS: { + rbs_ast_declarations_modulealias_t *node = (rbs_ast_declarations_modulealias_t *)any_node; + + if (node->new_name != NULL) { + rbs_node_destroy((rbs_node_t *) node->new_name); + } + if (node->old_name != NULL) { + rbs_node_destroy((rbs_node_t *) node->old_name); + } + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_DECLARATIONS_TYPEALIAS: { + rbs_ast_declarations_typealias_t *node = (rbs_ast_declarations_typealias_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + rbs_node_list_free(node->type_params); + if (node->type != NULL) { + rbs_node_destroy((rbs_node_t *) node->type); + } + rbs_node_list_free(node->annotations); + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_DIRECTIVES_USE: { + rbs_ast_directives_use_t *node = (rbs_ast_directives_use_t *)any_node; + + rbs_node_list_free(node->clauses); + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_DIRECTIVES_USE_SINGLECLAUSE: { + rbs_ast_directives_use_singleclause_t *node = (rbs_ast_directives_use_singleclause_t *)any_node; + + if (node->type_name != NULL) { + rbs_node_destroy((rbs_node_t *) node->type_name); + } + if (node->new_name != NULL) { + rbs_node_destroy((rbs_node_t *) node->new_name); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE: { + rbs_ast_directives_use_wildcardclause_t *node = (rbs_ast_directives_use_wildcardclause_t *)any_node; + + if (node->rbs_namespace != NULL) { + rbs_node_destroy((rbs_node_t *) node->rbs_namespace); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_INTEGER: { + rbs_ast_integer_t *node = (rbs_ast_integer_t *)any_node; + + rbs_string_free_if_needed(&node->string_representation); + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_MEMBERS_ALIAS: { + rbs_ast_members_alias_t *node = (rbs_ast_members_alias_t *)any_node; + + if (node->new_name != NULL) { + rbs_node_destroy((rbs_node_t *) node->new_name); + } + if (node->old_name != NULL) { + rbs_node_destroy((rbs_node_t *) node->old_name); + } + if (node->kind != NULL) { + rbs_node_destroy((rbs_node_t *) node->kind); + } + rbs_node_list_free(node->annotations); + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_MEMBERS_ATTRACCESSOR: { + rbs_ast_members_attraccessor_t *node = (rbs_ast_members_attraccessor_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + if (node->type != NULL) { + rbs_node_destroy((rbs_node_t *) node->type); + } + if (node->ivar_name != NULL) { + rbs_node_destroy((rbs_node_t *) node->ivar_name); + } + if (node->kind != NULL) { + rbs_node_destroy((rbs_node_t *) node->kind); + } + rbs_node_list_free(node->annotations); + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + if (node->visibility != NULL) { + rbs_node_destroy((rbs_node_t *) node->visibility); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_MEMBERS_ATTRREADER: { + rbs_ast_members_attrreader_t *node = (rbs_ast_members_attrreader_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + if (node->type != NULL) { + rbs_node_destroy((rbs_node_t *) node->type); + } + if (node->ivar_name != NULL) { + rbs_node_destroy((rbs_node_t *) node->ivar_name); + } + if (node->kind != NULL) { + rbs_node_destroy((rbs_node_t *) node->kind); + } + rbs_node_list_free(node->annotations); + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + if (node->visibility != NULL) { + rbs_node_destroy((rbs_node_t *) node->visibility); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_MEMBERS_ATTRWRITER: { + rbs_ast_members_attrwriter_t *node = (rbs_ast_members_attrwriter_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + if (node->type != NULL) { + rbs_node_destroy((rbs_node_t *) node->type); + } + if (node->ivar_name != NULL) { + rbs_node_destroy((rbs_node_t *) node->ivar_name); + } + if (node->kind != NULL) { + rbs_node_destroy((rbs_node_t *) node->kind); + } + rbs_node_list_free(node->annotations); + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + if (node->visibility != NULL) { + rbs_node_destroy((rbs_node_t *) node->visibility); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE: { + rbs_ast_members_classinstancevariable_t *node = (rbs_ast_members_classinstancevariable_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + if (node->type != NULL) { + rbs_node_destroy((rbs_node_t *) node->type); + } + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_MEMBERS_CLASSVARIABLE: { + rbs_ast_members_classvariable_t *node = (rbs_ast_members_classvariable_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + if (node->type != NULL) { + rbs_node_destroy((rbs_node_t *) node->type); + } + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_MEMBERS_EXTEND: { + rbs_ast_members_extend_t *node = (rbs_ast_members_extend_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + rbs_node_list_free(node->args); + rbs_node_list_free(node->annotations); + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_MEMBERS_INCLUDE: { + rbs_ast_members_include_t *node = (rbs_ast_members_include_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + rbs_node_list_free(node->args); + rbs_node_list_free(node->annotations); + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_MEMBERS_INSTANCEVARIABLE: { + rbs_ast_members_instancevariable_t *node = (rbs_ast_members_instancevariable_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + if (node->type != NULL) { + rbs_node_destroy((rbs_node_t *) node->type); + } + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_MEMBERS_METHODDEFINITION: { + rbs_ast_members_methoddefinition_t *node = (rbs_ast_members_methoddefinition_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + if (node->kind != NULL) { + rbs_node_destroy((rbs_node_t *) node->kind); + } + rbs_node_list_free(node->overloads); + rbs_node_list_free(node->annotations); + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + // overloading is a bool, so we don't need to free it. + if (node->visibility != NULL) { + rbs_node_destroy((rbs_node_t *) node->visibility); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD: { + rbs_ast_members_methoddefinition_overload_t *node = (rbs_ast_members_methoddefinition_overload_t *)any_node; + + rbs_node_list_free(node->annotations); + if (node->method_type != NULL) { + rbs_node_destroy((rbs_node_t *) node->method_type); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_MEMBERS_PREPEND: { + rbs_ast_members_prepend_t *node = (rbs_ast_members_prepend_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + rbs_node_list_free(node->args); + rbs_node_list_free(node->annotations); + if (node->comment != NULL) { + rbs_node_destroy((rbs_node_t *) node->comment); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_MEMBERS_PRIVATE: { + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_MEMBERS_PUBLIC: { + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_STRING: { + rbs_ast_string_t *node = (rbs_ast_string_t *)any_node; + + rbs_string_free_if_needed(&node->string); + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_AST_TYPEPARAM: { + rbs_ast_typeparam_t *node = (rbs_ast_typeparam_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + if (node->variance != NULL) { + rbs_node_destroy((rbs_node_t *) node->variance); + } + if (node->upper_bound != NULL) { + rbs_node_destroy((rbs_node_t *) node->upper_bound); + } + if (node->default_type != NULL) { + rbs_node_destroy((rbs_node_t *) node->default_type); + } + // unchecked is a bool, so we don't need to free it. + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_METHODTYPE: { + rbs_methodtype_t *node = (rbs_methodtype_t *)any_node; + + rbs_node_list_free(node->type_params); + if (node->type != NULL) { + rbs_node_destroy((rbs_node_t *) node->type); + } + if (node->block != NULL) { + rbs_node_destroy((rbs_node_t *) node->block); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_NAMESPACE: { + rbs_namespace_t *node = (rbs_namespace_t *)any_node; + + rbs_node_list_free(node->path); + // absolute is a bool, so we don't need to free it. + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_SIGNATURE: { + rbs_signature_t *node = (rbs_signature_t *)any_node; + + rbs_node_list_free(node->directives); + rbs_node_list_free(node->declarations); + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPENAME: { + rbs_typename_t *node = (rbs_typename_t *)any_node; + + if (node->rbs_namespace != NULL) { + rbs_node_destroy((rbs_node_t *) node->rbs_namespace); + } + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_ALIAS: { + rbs_types_alias_t *node = (rbs_types_alias_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + rbs_node_list_free(node->args); + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_BASES_ANY: { + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_BASES_BOOL: { + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_BASES_BOTTOM: { + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_BASES_CLASS: { + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_BASES_INSTANCE: { + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_BASES_NIL: { + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_BASES_SELF: { + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_BASES_TOP: { + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_BASES_VOID: { + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_BLOCK: { + rbs_types_block_t *node = (rbs_types_block_t *)any_node; + + if (node->type != NULL) { + rbs_node_destroy((rbs_node_t *) node->type); + } + // required is a bool, so we don't need to free it. + if (node->self_type != NULL) { + rbs_node_destroy((rbs_node_t *) node->self_type); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_CLASSINSTANCE: { + rbs_types_classinstance_t *node = (rbs_types_classinstance_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + rbs_node_list_free(node->args); + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_CLASSSINGLETON: { + rbs_types_classsingleton_t *node = (rbs_types_classsingleton_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_FUNCTION: { + rbs_types_function_t *node = (rbs_types_function_t *)any_node; + + rbs_node_list_free(node->required_positionals); + rbs_node_list_free(node->optional_positionals); + if (node->rest_positionals != NULL) { + rbs_node_destroy((rbs_node_t *) node->rest_positionals); + } + rbs_node_list_free(node->trailing_positionals); + rbs_hash_free(node->required_keywords); + rbs_hash_free(node->optional_keywords); + if (node->rest_keywords != NULL) { + rbs_node_destroy((rbs_node_t *) node->rest_keywords); + } + if (node->return_type != NULL) { + rbs_node_destroy((rbs_node_t *) node->return_type); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_FUNCTION_PARAM: { + rbs_types_function_param_t *node = (rbs_types_function_param_t *)any_node; + + if (node->type != NULL) { + rbs_node_destroy((rbs_node_t *) node->type); + } + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_INTERFACE: { + rbs_types_interface_t *node = (rbs_types_interface_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + rbs_node_list_free(node->args); + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_INTERSECTION: { + rbs_types_intersection_t *node = (rbs_types_intersection_t *)any_node; + + rbs_node_list_free(node->types); + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_LITERAL: { + rbs_types_literal_t *node = (rbs_types_literal_t *)any_node; + + if (node->literal != NULL) { + rbs_node_destroy((rbs_node_t *) node->literal); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_OPTIONAL: { + rbs_types_optional_t *node = (rbs_types_optional_t *)any_node; + + if (node->type != NULL) { + rbs_node_destroy((rbs_node_t *) node->type); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_PROC: { + rbs_types_proc_t *node = (rbs_types_proc_t *)any_node; + + if (node->type != NULL) { + rbs_node_destroy((rbs_node_t *) node->type); + } + if (node->block != NULL) { + rbs_node_destroy((rbs_node_t *) node->block); + } + if (node->self_type != NULL) { + rbs_node_destroy((rbs_node_t *) node->self_type); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_RECORD: { + rbs_types_record_t *node = (rbs_types_record_t *)any_node; + + rbs_hash_free(node->all_fields); + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_RECORD_FIELDTYPE: { + rbs_types_record_fieldtype_t *node = (rbs_types_record_fieldtype_t *)any_node; + + if (node->type != NULL) { + rbs_node_destroy((rbs_node_t *) node->type); + } + // required is a bool, so we don't need to free it. + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_TUPLE: { + rbs_types_tuple_t *node = (rbs_types_tuple_t *)any_node; + + rbs_node_list_free(node->types); + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_UNION: { + rbs_types_union_t *node = (rbs_types_union_t *)any_node; + + rbs_node_list_free(node->types); + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_UNTYPEDFUNCTION: { + rbs_types_untypedfunction_t *node = (rbs_types_untypedfunction_t *)any_node; + + if (node->return_type != NULL) { + rbs_node_destroy((rbs_node_t *) node->return_type); + } + break; + } +#line 191 "prism/templates/src/ast.c.erb" + case RBS_TYPES_VARIABLE: { + rbs_types_variable_t *node = (rbs_types_variable_t *)any_node; + + if (node->name != NULL) { + rbs_node_destroy((rbs_node_t *) node->name); + } + break; + } + case RBS_KEYWORD: + case RBS_AST_SYMBOL: { + // TODO: Delete symbol from constant pool? Or just free the whole constant pool? + break; + } + } + + // free(any_node); +} diff --git a/templates/include/rbs/ast.h.erb b/templates/include/rbs/ast.h.erb index f719aaf12..e901d760d 100644 --- a/templates/include/rbs/ast.h.erb +++ b/templates/include/rbs/ast.h.erb @@ -98,4 +98,6 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *, rbs_location_t *, rbs_co <%= node.c_type_name %> *<%= node.c_constructor_function_name %>(<%= node.constructor_params.map(&:parameter_decl).join(", ") %>); <%- end -%> +void rbs_node_destroy(rbs_node_t *any_node); + #endif diff --git a/templates/src/ast.c.erb b/templates/src/ast.c.erb index 9d69e49cb..64647cb28 100644 --- a/templates/src/ast.c.erb +++ b/templates/src/ast.c.erb @@ -1,3 +1,4 @@ +#line <%= __LINE__ + 1 %> "prism/templates/src/<%= File.basename(__FILE__) %>" #include "rbs/ast.h" #include @@ -148,6 +149,7 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_location_t } <%- nodes.each do |node| -%> +#line <%= __LINE__ + 1 %> "prism/templates/src/<%= File.basename(__FILE__) %>" <%= node.c_type_name %> *<%= node.c_constructor_function_name %>(<%= node.constructor_params.map(&:parameter_decl).join(", ") %>) { <%= node.c_type_name %> *instance = rbs_allocator_alloc(allocator, <%= node.c_type_name %>); @@ -167,5 +169,43 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_location_t return instance; } - <%- end -%> + +#line <%= __LINE__ + 1 %> "prism/templates/src/<%= File.basename(__FILE__) %>" +void rbs_node_destroy(rbs_node_t *any_node) { + switch (any_node->type) { + <%- nodes.each do |node| -%> +#line <%= __LINE__ + 1 %> "prism/templates/src/<%= File.basename(__FILE__) %>" + case <%= node.c_type_enum_name %>: { + <%- if node.has_children_to_free? -%> + <%= node.c_type_name %> *node = (<%= node.c_type_name %> *)any_node; + + <%- node.fields.each do |field| -%> + <%- case field.c_type -%> + <%- when "rbs_string" -%> + rbs_string_free_if_needed(&node-><%= field.c_name %>); + <%- when "rbs_node_list" -%> + rbs_node_list_free(node-><%= field.c_name %>); + <%- when "rbs_hash" -%> + rbs_hash_free(node-><%= field.c_name %>); + <%- when "bool" -%> + // <%= field.c_name %> is a bool, so we don't need to free it. + <%- else -%> + if (node-><%= field.c_name %> != NULL) { + rbs_node_destroy((rbs_node_t *) node-><%= field.c_name %>); + } + <%- end -%> + <%- end -%> + <%- end -%> + break; + } + <%- end -%> + case RBS_KEYWORD: + case RBS_AST_SYMBOL: { + // TODO: Delete symbol from constant pool? Or just free the whole constant pool? + break; + } + } + + // `any_node` will be freed automatically when the arena is freed. +} diff --git a/templates/template.rb b/templates/template.rb index 8e02a956e..9843b5eee 100644 --- a/templates/template.rb +++ b/templates/template.rb @@ -57,15 +57,6 @@ def ast_node? def needs_to_be_freed? !["VALUE", "bool"].include?(@c_type) end - - def ast_node? - @c_type == "rbs_node" || - @c_type == "rbs_typename" || - @c_type == "rbs_namespace" || - @c_type.include?("_ast_") || - @c_type.include?("_decl_") || - @c_type.include?("_types_") - end end class Type @@ -136,6 +127,10 @@ def expose_to_ruby? def expose_location? @expose_location end + + def has_children_to_free? + @fields.any?(&:needs_to_be_freed?) + end end class << self From 590c1002fe16f40a4f1973789e1cdce4a2b51ee1 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Tue, 17 Dec 2024 14:42:24 -0500 Subject: [PATCH 066/111] Delete `rbs_string_offset()` --- src/rbs_string.c | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/src/rbs_string.c b/src/rbs_string.c index 116959193..950da768e 100644 --- a/src/rbs_string.c +++ b/src/rbs_string.c @@ -44,35 +44,7 @@ void rbs_string_free(rbs_string_t *self) { free((void *) self->start); } -// // Ensure the given string is shared, so that we can slice it without needing to free the old string. -// static void ensure_shared(rbs_string_t *self) { -// if (self->type != RBS_STRING_SHARED) { -// fprintf(stderr, "Calling this function requires a shared string.\n"); -// exit(EXIT_FAILURE); -// } -// } - -void rbs_string_drop_first(rbs_string_t *self, size_t n) { - // ensure_shared(self); - - self->start += n; -} - -void rbs_string_drop_last(rbs_string_t *self, size_t n) { - // ensure_shared(self); - - self->end -= n; -} - -void rbs_string_limit_length(rbs_string_t *self, size_t new_length) { - // ensure_shared(self); - - self->end = self->start + new_length; -} - rbs_string_t rbs_string_strip_whitespace(rbs_string_t *self) { - // ensure_shared(self); - const char *new_start = self->start; while (isspace(*new_start) && new_start < self->end) { new_start++; From b2de7f17a9317db3ac853f4787c598504757c28b Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 14 Feb 2025 18:46:18 -0500 Subject: [PATCH 067/111] Call `rbs_node_destroy()` on list and hash members --- src/ast.c | 279 ++++++++++++++++++++++------------------ templates/src/ast.c.erb | 23 ++++ 2 files changed, 174 insertions(+), 128 deletions(-) diff --git a/src/ast.c b/src/ast.c index 883ae1e45..62a3c11a7 100644 --- a/src/ast.c +++ b/src/ast.c @@ -95,6 +95,17 @@ rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *allocator) { return list; } +void rbs_node_list_free(rbs_node_list_t *list) { + rbs_node_list_node_t *current = list->head; + while (current != NULL) { + rbs_node_list_node_t *next = current->next; + rbs_node_destroy(current->node); + // `current` is owned by the arena, so it will be freed automatically when the arena is freed. + current = next; + } + // `list` is owned by the arena, so it will be freed automatically when the arena is freed. +} + void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node) { rbs_node_list_node_t *new_node = rbs_allocator_alloc(list->allocator, rbs_node_list_node_t); *new_node = (rbs_node_list_node_t) { @@ -127,6 +138,18 @@ rbs_hash_t* rbs_hash_new(rbs_allocator_t *allocator) { return hash; } +void rbs_hash_free(rbs_hash_t *hash) { + rbs_hash_node_t *current = hash->head; + while (current != NULL) { + rbs_hash_node_t *next = current->next; + rbs_node_destroy(current->key); + rbs_node_destroy(current->value); + // `current` is owned by the arena, so it will be freed automatically when the arena is freed. + current = next; + } + // `hash` is owned by the arena, so it will be freed automatically when the arena is freed. +} + bool rbs_node_equal(rbs_node_t *lhs, rbs_node_t *rhs) { if (lhs == rhs) return true; if (lhs->type != rhs->type) return false; @@ -215,7 +238,7 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_location_t return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string) { rbs_ast_annotation_t *instance = rbs_allocator_alloc(allocator, rbs_ast_annotation_t); @@ -230,7 +253,7 @@ rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_loc return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, rbs_location_t *location, bool value) { rbs_ast_bool_t *instance = rbs_allocator_alloc(allocator, rbs_ast_bool_t); @@ -245,7 +268,7 @@ rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, rbs_location_t *loc return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string) { rbs_ast_comment_t *instance = rbs_allocator_alloc(allocator, rbs_ast_comment_t); @@ -260,7 +283,7 @@ rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, rbs_location_ return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_class_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_t); @@ -280,7 +303,7 @@ rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *al return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { rbs_ast_declarations_class_super_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_super_t); @@ -296,7 +319,7 @@ rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_all return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_ast_comment_t *comment) { rbs_ast_declarations_classalias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_classalias_t); @@ -313,7 +336,7 @@ rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_alloc return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_declarations_constant_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_constant_t); @@ -330,7 +353,7 @@ rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_declarations_global_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_global_t); @@ -347,7 +370,7 @@ rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t * return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_interface_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_interface_t); @@ -366,7 +389,7 @@ rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocat return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_module_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_t); @@ -386,7 +409,7 @@ rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t * return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { rbs_ast_declarations_module_self_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_self_t); @@ -402,7 +425,7 @@ rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_all return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_ast_comment_t *comment) { rbs_ast_declarations_modulealias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_modulealias_t); @@ -419,7 +442,7 @@ rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_all return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_typealias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_typealias_t); @@ -438,7 +461,7 @@ rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocat return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *clauses) { rbs_ast_directives_use_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_t); @@ -453,7 +476,7 @@ rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *type_name, rbs_ast_symbol_t *new_name) { rbs_ast_directives_use_singleclause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_singleclause_t); @@ -469,7 +492,7 @@ rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(r return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace) { rbs_ast_directives_use_wildcardclause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_wildcardclause_t); @@ -484,7 +507,7 @@ rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_n return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_integer_t *rbs_ast_integer_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string_representation) { rbs_ast_integer_t *instance = rbs_allocator_alloc(allocator, rbs_ast_integer_t); @@ -499,7 +522,7 @@ rbs_ast_integer_t *rbs_ast_integer_new(rbs_allocator_t *allocator, rbs_location_ return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_alias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_alias_t); @@ -518,7 +541,7 @@ rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, r return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { rbs_ast_members_attraccessor_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attraccessor_t); @@ -539,7 +562,7 @@ rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { rbs_ast_members_attrreader_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attrreader_t); @@ -560,7 +583,7 @@ rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *al return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { rbs_ast_members_attrwriter_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attrwriter_t); @@ -581,7 +604,7 @@ rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *al return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_members_classinstancevariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_classinstancevariable_t); @@ -598,7 +621,7 @@ rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_n return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_members_classvariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_classvariable_t); @@ -615,7 +638,7 @@ rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_extend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_extend_t); @@ -633,7 +656,7 @@ rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_include_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_include_t); @@ -651,7 +674,7 @@ rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocato return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_members_instancevariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_instancevariable_t); @@ -668,7 +691,7 @@ rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_all return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, bool overloading, rbs_keyword_t *visibility) { rbs_ast_members_methoddefinition_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_methoddefinition_t); @@ -689,7 +712,7 @@ rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_all return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *annotations, rbs_node_t *method_type) { rbs_ast_members_methoddefinition_overload_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_methoddefinition_overload_t); @@ -705,7 +728,7 @@ rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_ov return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_prepend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_prepend_t); @@ -723,7 +746,7 @@ rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocato return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_ast_members_private_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_private_t); @@ -737,7 +760,7 @@ rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocato return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_ast_members_public_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_public_t); @@ -751,7 +774,7 @@ rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_string_t *rbs_ast_string_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string) { rbs_ast_string_t *instance = rbs_allocator_alloc(allocator, rbs_ast_string_t); @@ -766,7 +789,7 @@ rbs_ast_string_t *rbs_ast_string_new(rbs_allocator_t *allocator, rbs_location_t return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked) { rbs_ast_typeparam_t *instance = rbs_allocator_alloc(allocator, rbs_ast_typeparam_t); @@ -785,7 +808,7 @@ rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_locat return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block) { rbs_methodtype_t *instance = rbs_allocator_alloc(allocator, rbs_methodtype_t); @@ -802,7 +825,7 @@ rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_location_t return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *path, bool absolute) { rbs_namespace_t *instance = rbs_allocator_alloc(allocator, rbs_namespace_t); @@ -818,7 +841,7 @@ rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_location_t *l return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *directives, rbs_node_list_t *declarations) { rbs_signature_t *instance = rbs_allocator_alloc(allocator, rbs_signature_t); @@ -834,7 +857,7 @@ rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_location_t *l return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace, rbs_ast_symbol_t *name) { rbs_typename_t *instance = rbs_allocator_alloc(allocator, rbs_typename_t); @@ -850,7 +873,7 @@ rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_location_t *loc return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { rbs_types_alias_t *instance = rbs_allocator_alloc(allocator, rbs_types_alias_t); @@ -866,7 +889,7 @@ rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_location_ return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, rbs_location_t *location, bool todo) { rbs_types_bases_any_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_any_t); @@ -881,7 +904,7 @@ rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, rbs_l return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_bool_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_bool_t); @@ -895,7 +918,7 @@ rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, rbs return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_bottom_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_bottom_t); @@ -909,7 +932,7 @@ rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_class_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_class_t); @@ -923,7 +946,7 @@ rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, r return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_instance_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_instance_t); @@ -937,7 +960,7 @@ rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *alloca return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_nil_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_nil_t); @@ -951,7 +974,7 @@ rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, rbs_l return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_self_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_self_t); @@ -965,7 +988,7 @@ rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, rbs return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_top_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_top_t); @@ -979,7 +1002,7 @@ rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, rbs_l return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_void_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_void_t); @@ -993,7 +1016,7 @@ rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, rbs return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, bool required, rbs_node_t *self_type) { rbs_types_block_t *instance = rbs_allocator_alloc(allocator, rbs_types_block_t); @@ -1010,7 +1033,7 @@ rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_location_ return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { rbs_types_classinstance_t *instance = rbs_allocator_alloc(allocator, rbs_types_classinstance_t); @@ -1026,7 +1049,7 @@ rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocato return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name) { rbs_types_classsingleton_t *instance = rbs_allocator_alloc(allocator, rbs_types_classsingleton_t); @@ -1041,7 +1064,7 @@ rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *alloca return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *required_positionals, rbs_node_list_t *optional_positionals, rbs_node_t *rest_positionals, rbs_node_list_t *trailing_positionals, rbs_hash_t *required_keywords, rbs_hash_t *optional_keywords, rbs_node_t *rest_keywords, rbs_node_t *return_type) { rbs_types_function_t *instance = rbs_allocator_alloc(allocator, rbs_types_function_t); @@ -1063,7 +1086,7 @@ rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, rbs_loc return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, rbs_ast_symbol_t *name) { rbs_types_function_param_t *instance = rbs_allocator_alloc(allocator, rbs_types_function_param_t); @@ -1079,7 +1102,7 @@ rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *alloca return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { rbs_types_interface_t *instance = rbs_allocator_alloc(allocator, rbs_types_interface_t); @@ -1095,7 +1118,7 @@ rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_l return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types) { rbs_types_intersection_t *instance = rbs_allocator_alloc(allocator, rbs_types_intersection_t); @@ -1110,7 +1133,7 @@ rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *literal) { rbs_types_literal_t *instance = rbs_allocator_alloc(allocator, rbs_types_literal_t); @@ -1125,7 +1148,7 @@ rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, rbs_locat return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type) { rbs_types_optional_t *instance = rbs_allocator_alloc(allocator, rbs_types_optional_t); @@ -1140,7 +1163,7 @@ rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_loc return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, rbs_types_block_t *block, rbs_node_t *self_type) { rbs_types_proc_t *instance = rbs_allocator_alloc(allocator, rbs_types_proc_t); @@ -1157,7 +1180,7 @@ rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_location_t return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_hash_t *all_fields) { rbs_types_record_t *instance = rbs_allocator_alloc(allocator, rbs_types_record_t); @@ -1172,7 +1195,7 @@ rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, rbs_locatio return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_record_fieldtype_t *rbs_types_record_fieldtype_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, bool required) { rbs_types_record_fieldtype_t *instance = rbs_allocator_alloc(allocator, rbs_types_record_fieldtype_t); @@ -1188,7 +1211,7 @@ rbs_types_record_fieldtype_t *rbs_types_record_fieldtype_new(rbs_allocator_t *al return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types) { rbs_types_tuple_t *instance = rbs_allocator_alloc(allocator, rbs_types_tuple_t); @@ -1203,7 +1226,7 @@ rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_location_ return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types) { rbs_types_union_t *instance = rbs_allocator_alloc(allocator, rbs_types_union_t); @@ -1218,7 +1241,7 @@ rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_location_ return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *return_type) { rbs_types_untypedfunction_t *instance = rbs_allocator_alloc(allocator, rbs_types_untypedfunction_t); @@ -1233,7 +1256,7 @@ rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allo return instance; } -#line 165 "prism/templates/src/ast.c.erb" +#line 176 "prism/templates/src/ast.c.erb" rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name) { rbs_types_variable_t *instance = rbs_allocator_alloc(allocator, rbs_types_variable_t); @@ -1249,28 +1272,28 @@ rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_loc return instance; } -#line 187 "prism/templates/src/ast.c.erb" +#line 198 "prism/templates/src/ast.c.erb" void rbs_node_destroy(rbs_node_t *any_node) { switch (any_node->type) { -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_ANNOTATION: { rbs_ast_annotation_t *node = (rbs_ast_annotation_t *)any_node; rbs_string_free_if_needed(&node->string); break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_BOOL: { break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_COMMENT: { rbs_ast_comment_t *node = (rbs_ast_comment_t *)any_node; rbs_string_free_if_needed(&node->string); break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_DECLARATIONS_CLASS: { rbs_ast_declarations_class_t *node = (rbs_ast_declarations_class_t *)any_node; @@ -1288,7 +1311,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_DECLARATIONS_CLASS_SUPER: { rbs_ast_declarations_class_super_t *node = (rbs_ast_declarations_class_super_t *)any_node; @@ -1298,7 +1321,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { rbs_node_list_free(node->args); break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_DECLARATIONS_CLASSALIAS: { rbs_ast_declarations_classalias_t *node = (rbs_ast_declarations_classalias_t *)any_node; @@ -1313,7 +1336,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_DECLARATIONS_CONSTANT: { rbs_ast_declarations_constant_t *node = (rbs_ast_declarations_constant_t *)any_node; @@ -1328,7 +1351,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_DECLARATIONS_GLOBAL: { rbs_ast_declarations_global_t *node = (rbs_ast_declarations_global_t *)any_node; @@ -1343,7 +1366,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_DECLARATIONS_INTERFACE: { rbs_ast_declarations_interface_t *node = (rbs_ast_declarations_interface_t *)any_node; @@ -1358,7 +1381,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_DECLARATIONS_MODULE: { rbs_ast_declarations_module_t *node = (rbs_ast_declarations_module_t *)any_node; @@ -1374,7 +1397,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_DECLARATIONS_MODULE_SELF: { rbs_ast_declarations_module_self_t *node = (rbs_ast_declarations_module_self_t *)any_node; @@ -1384,7 +1407,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { rbs_node_list_free(node->args); break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_DECLARATIONS_MODULEALIAS: { rbs_ast_declarations_modulealias_t *node = (rbs_ast_declarations_modulealias_t *)any_node; @@ -1399,7 +1422,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_DECLARATIONS_TYPEALIAS: { rbs_ast_declarations_typealias_t *node = (rbs_ast_declarations_typealias_t *)any_node; @@ -1416,14 +1439,14 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_DIRECTIVES_USE: { rbs_ast_directives_use_t *node = (rbs_ast_directives_use_t *)any_node; rbs_node_list_free(node->clauses); break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_DIRECTIVES_USE_SINGLECLAUSE: { rbs_ast_directives_use_singleclause_t *node = (rbs_ast_directives_use_singleclause_t *)any_node; @@ -1435,7 +1458,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE: { rbs_ast_directives_use_wildcardclause_t *node = (rbs_ast_directives_use_wildcardclause_t *)any_node; @@ -1444,14 +1467,14 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_INTEGER: { rbs_ast_integer_t *node = (rbs_ast_integer_t *)any_node; rbs_string_free_if_needed(&node->string_representation); break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_MEMBERS_ALIAS: { rbs_ast_members_alias_t *node = (rbs_ast_members_alias_t *)any_node; @@ -1470,7 +1493,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_MEMBERS_ATTRACCESSOR: { rbs_ast_members_attraccessor_t *node = (rbs_ast_members_attraccessor_t *)any_node; @@ -1495,7 +1518,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_MEMBERS_ATTRREADER: { rbs_ast_members_attrreader_t *node = (rbs_ast_members_attrreader_t *)any_node; @@ -1520,7 +1543,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_MEMBERS_ATTRWRITER: { rbs_ast_members_attrwriter_t *node = (rbs_ast_members_attrwriter_t *)any_node; @@ -1545,7 +1568,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE: { rbs_ast_members_classinstancevariable_t *node = (rbs_ast_members_classinstancevariable_t *)any_node; @@ -1560,7 +1583,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_MEMBERS_CLASSVARIABLE: { rbs_ast_members_classvariable_t *node = (rbs_ast_members_classvariable_t *)any_node; @@ -1575,7 +1598,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_MEMBERS_EXTEND: { rbs_ast_members_extend_t *node = (rbs_ast_members_extend_t *)any_node; @@ -1589,7 +1612,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_MEMBERS_INCLUDE: { rbs_ast_members_include_t *node = (rbs_ast_members_include_t *)any_node; @@ -1603,7 +1626,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_MEMBERS_INSTANCEVARIABLE: { rbs_ast_members_instancevariable_t *node = (rbs_ast_members_instancevariable_t *)any_node; @@ -1618,7 +1641,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_MEMBERS_METHODDEFINITION: { rbs_ast_members_methoddefinition_t *node = (rbs_ast_members_methoddefinition_t *)any_node; @@ -1639,7 +1662,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD: { rbs_ast_members_methoddefinition_overload_t *node = (rbs_ast_members_methoddefinition_overload_t *)any_node; @@ -1649,7 +1672,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_MEMBERS_PREPEND: { rbs_ast_members_prepend_t *node = (rbs_ast_members_prepend_t *)any_node; @@ -1663,22 +1686,22 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_MEMBERS_PRIVATE: { break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_MEMBERS_PUBLIC: { break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_STRING: { rbs_ast_string_t *node = (rbs_ast_string_t *)any_node; rbs_string_free_if_needed(&node->string); break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_AST_TYPEPARAM: { rbs_ast_typeparam_t *node = (rbs_ast_typeparam_t *)any_node; @@ -1697,7 +1720,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { // unchecked is a bool, so we don't need to free it. break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_METHODTYPE: { rbs_methodtype_t *node = (rbs_methodtype_t *)any_node; @@ -1710,7 +1733,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_NAMESPACE: { rbs_namespace_t *node = (rbs_namespace_t *)any_node; @@ -1718,7 +1741,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { // absolute is a bool, so we don't need to free it. break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_SIGNATURE: { rbs_signature_t *node = (rbs_signature_t *)any_node; @@ -1726,7 +1749,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { rbs_node_list_free(node->declarations); break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPENAME: { rbs_typename_t *node = (rbs_typename_t *)any_node; @@ -1738,7 +1761,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_ALIAS: { rbs_types_alias_t *node = (rbs_types_alias_t *)any_node; @@ -1748,43 +1771,43 @@ void rbs_node_destroy(rbs_node_t *any_node) { rbs_node_list_free(node->args); break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_BASES_ANY: { break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_BASES_BOOL: { break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_BASES_BOTTOM: { break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_BASES_CLASS: { break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_BASES_INSTANCE: { break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_BASES_NIL: { break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_BASES_SELF: { break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_BASES_TOP: { break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_BASES_VOID: { break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_BLOCK: { rbs_types_block_t *node = (rbs_types_block_t *)any_node; @@ -1797,7 +1820,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_CLASSINSTANCE: { rbs_types_classinstance_t *node = (rbs_types_classinstance_t *)any_node; @@ -1807,7 +1830,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { rbs_node_list_free(node->args); break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_CLASSSINGLETON: { rbs_types_classsingleton_t *node = (rbs_types_classsingleton_t *)any_node; @@ -1816,7 +1839,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_FUNCTION: { rbs_types_function_t *node = (rbs_types_function_t *)any_node; @@ -1836,7 +1859,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_FUNCTION_PARAM: { rbs_types_function_param_t *node = (rbs_types_function_param_t *)any_node; @@ -1848,7 +1871,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_INTERFACE: { rbs_types_interface_t *node = (rbs_types_interface_t *)any_node; @@ -1858,14 +1881,14 @@ void rbs_node_destroy(rbs_node_t *any_node) { rbs_node_list_free(node->args); break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_INTERSECTION: { rbs_types_intersection_t *node = (rbs_types_intersection_t *)any_node; rbs_node_list_free(node->types); break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_LITERAL: { rbs_types_literal_t *node = (rbs_types_literal_t *)any_node; @@ -1874,7 +1897,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_OPTIONAL: { rbs_types_optional_t *node = (rbs_types_optional_t *)any_node; @@ -1883,7 +1906,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_PROC: { rbs_types_proc_t *node = (rbs_types_proc_t *)any_node; @@ -1898,14 +1921,14 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_RECORD: { rbs_types_record_t *node = (rbs_types_record_t *)any_node; rbs_hash_free(node->all_fields); break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_RECORD_FIELDTYPE: { rbs_types_record_fieldtype_t *node = (rbs_types_record_fieldtype_t *)any_node; @@ -1915,21 +1938,21 @@ void rbs_node_destroy(rbs_node_t *any_node) { // required is a bool, so we don't need to free it. break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_TUPLE: { rbs_types_tuple_t *node = (rbs_types_tuple_t *)any_node; rbs_node_list_free(node->types); break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_UNION: { rbs_types_union_t *node = (rbs_types_union_t *)any_node; rbs_node_list_free(node->types); break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_UNTYPEDFUNCTION: { rbs_types_untypedfunction_t *node = (rbs_types_untypedfunction_t *)any_node; @@ -1938,7 +1961,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { } break; } -#line 191 "prism/templates/src/ast.c.erb" +#line 202 "prism/templates/src/ast.c.erb" case RBS_TYPES_VARIABLE: { rbs_types_variable_t *node = (rbs_types_variable_t *)any_node; @@ -1954,5 +1977,5 @@ void rbs_node_destroy(rbs_node_t *any_node) { } } - // free(any_node); + // `any_node` will be freed automatically when the arena is freed. } diff --git a/templates/src/ast.c.erb b/templates/src/ast.c.erb index 64647cb28..18154f838 100644 --- a/templates/src/ast.c.erb +++ b/templates/src/ast.c.erb @@ -28,6 +28,17 @@ rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *allocator) { return list; } +void rbs_node_list_free(rbs_node_list_t *list) { + rbs_node_list_node_t *current = list->head; + while (current != NULL) { + rbs_node_list_node_t *next = current->next; + rbs_node_destroy(current->node); + // `current` is owned by the arena, so it will be freed automatically when the arena is freed. + current = next; + } + // `list` is owned by the arena, so it will be freed automatically when the arena is freed. +} + void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node) { rbs_node_list_node_t *new_node = rbs_allocator_alloc(list->allocator, rbs_node_list_node_t); *new_node = (rbs_node_list_node_t) { @@ -60,6 +71,18 @@ rbs_hash_t* rbs_hash_new(rbs_allocator_t *allocator) { return hash; } +void rbs_hash_free(rbs_hash_t *hash) { + rbs_hash_node_t *current = hash->head; + while (current != NULL) { + rbs_hash_node_t *next = current->next; + rbs_node_destroy(current->key); + rbs_node_destroy(current->value); + // `current` is owned by the arena, so it will be freed automatically when the arena is freed. + current = next; + } + // `hash` is owned by the arena, so it will be freed automatically when the arena is freed. +} + bool rbs_node_equal(rbs_node_t *lhs, rbs_node_t *rhs) { if (lhs == rhs) return true; if (lhs->type != rhs->type) return false; From edadf9fc438857e3edfba4c73373b4ed9d54a453 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Mon, 13 Jan 2025 14:52:27 -0500 Subject: [PATCH 068/111] Add VSCode debugger configuration --- .vscode/launch.json | 118 +++++++++++++++++++++++++++++++++++ ext/rbs_extension/extconf.rb | 2 + 2 files changed, 120 insertions(+) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..4e7f7906c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,118 @@ +// Running these tasks does not trigger a recompile, so you'll need to do that yourself. +// +// Make sure you're using a debug build when debugging. +// Add `append_cflags ['-O0', '-g']` to extconf.rb to enable this. +// +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug all tests in LLDB", + "program": "/opt/rubies/3.3.4/bin/ruby", + "args": [ + "-w", + "-Ilib", + "-Itest", + "/Users/alex/.gem/ruby/3.3.0/gems/rake-13.2.1/lib/rake/rake_test_loader.rb", + "test/rbs/ancestor_builder_test.rb", + "test/rbs/ancestor_graph_test.rb", + "test/rbs/annotate/annotations_test.rb", + "test/rbs/annotate/rdoc_annotator_test.rb", + "test/rbs/annotate/rdoc_source_test.rb", + "test/rbs/ast/type_param_test.rb", + "test/rbs/ast/visitor_test.rb", + "test/rbs/buffer_test.rb", + "test/rbs/cli_test.rb", + "test/rbs/collection/cleaner_test.rb", + "test/rbs/collection/config_test.rb", + "test/rbs/collection/installer_test.rb", + "test/rbs/collection/sources/git_test.rb", + "test/rbs/collection/sources/local_test.rb", + "test/rbs/collection/sources/stdlib_test.rb", + "test/rbs/definition_builder_test.rb", + "test/rbs/diff_test.rb", + "test/rbs/environment_loader_test.rb", + "test/rbs/environment_test.rb", + "test/rbs/environment_walker_test.rb", + "test/rbs/errors_test.rb", + "test/rbs/factory_test.rb", + "test/rbs/file_finder_test.rb", + "test/rbs/location_test.rb", + "test/rbs/locator_test.rb", + "test/rbs/method_builder_test.rb", + "test/rbs/method_type_parsing_test.rb", + "test/rbs/node_usage_test.rb", + "test/rbs/parser_test.rb", + "test/rbs/rb_prototype_test.rb", + "test/rbs/rbi_prototype_test.rb", + "test/rbs/rdoc/rbs_parser_test.rb", + "test/rbs/repository_test.rb", + "test/rbs/resolver/constant_resolver_test.rb", + "test/rbs/resolver/type_name_resolver_test.rb", + "test/rbs/runtime_prototype_test.rb", + "test/rbs/schema_test.rb", + "test/rbs/signature_parsing_test.rb", + "test/rbs/sorter_test.rb", + "test/rbs/subtractor_test.rb", + "test/rbs/test/hook_test.rb", + "test/rbs/test/runtime_test_test.rb", + "test/rbs/test/setup_helper_test.rb", + "test/rbs/test/tester_test.rb", + "test/rbs/test/type_check_test.rb", + "test/rbs/type_alias_dependency_test.rb", + "test/rbs/type_alias_regulartiry_test.rb", + "test/rbs/type_parsing_test.rb", + ], + "cwd": "${workspaceFolder}", + "stopOnEntry": false, + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug test in LLDB", + "program": "/opt/rubies/3.3.4/bin/ruby", + "args": [ + "-Ilib", + "-Itest", + "${workspaceFolder}/bin/test_runner.rb", + "${workspaceFolder}/test/rbs/location_test.rb" + ], + "cwd": "${workspaceFolder}", + "stopOnEntry": false, + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug test/rbs/location_test.rb", + "program": "/opt/rubies/3.3.4/bin/ruby", + "args": [ + "-Ilib", + "-Itest", + "${workspaceFolder}/bin/test_runner.rb", + "${workspaceFolder}/test/rbs/location_test.rb" + ], + "cwd": "${workspaceFolder}", + "stopOnEntry": false, + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug test/rbs/ancestor_builder_test.rb", + "program": "/opt/rubies/3.3.4/bin/ruby", + "args": [ + "-Ilib", + "-Itest", + "${workspaceFolder}/bin/test_runner.rb", + "${workspaceFolder}/test/rbs/ancestor_builder_test.rb" + ], + "cwd": "${workspaceFolder}", + "stopOnEntry": false, + }, + + ] +} diff --git a/ext/rbs_extension/extconf.rb b/ext/rbs_extension/extconf.rb index a63182c4e..c095b1623 100644 --- a/ext/rbs_extension/extconf.rb +++ b/ext/rbs_extension/extconf.rb @@ -12,4 +12,6 @@ Dir.glob("#{root_dir}/ext/rbs_extension/*.c") append_cflags ['-std=gnu99', '-Wimplicit-fallthrough', '-Wunused-result'] +append_cflags ['-O0', '-g'] if ENV['DEBUG'] + create_makefile 'rbs_extension' From 71eca450bba635dd51152d9a2e2fda85b731f081 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Mon, 13 Jan 2025 15:08:47 -0500 Subject: [PATCH 069/111] Add scripts for profiling in Instruments --- make_signed_ruby.rb | 192 ++++++++++++++++++++++++++++++++++++++++++++ trace_rbs.sh | 99 +++++++++++++++++++++++ 2 files changed, 291 insertions(+) create mode 100644 make_signed_ruby.rb create mode 100755 trace_rbs.sh diff --git a/make_signed_ruby.rb b/make_signed_ruby.rb new file mode 100644 index 000000000..74be88294 --- /dev/null +++ b/make_signed_ruby.rb @@ -0,0 +1,192 @@ +# This script copies and signs the current `ruby` executable, +# so that it can be run in a debugger or profiler, like Xcode's +# Instruments. +# +# The output folder is configured by `destination_folder` below, +# but it's just the current dir by default. +# This script will create a new `Ruby.app` bundle in that dest. +# +# Before running this script: +# +# 1. Create a signing certificate for yourself. +# +# Follow the instructions under the heading "To obtain a self-signed certificate using Certificate Assistant" +# https://developer.apple.com/library/archive/documentation/Security/Conceptual/CodeSigningGuide/Procedures/Procedures.html +# +# The defaults worked for me, so I didn't need to check "Let me override defaults". +# +# Once done, it should show up when you run `security find-identity` +# +# 2. Set `$signing_cert_name` to the name of your new certificate +# +# 3. Run `sudo DevToolsSecurity -enable` (Optional) +# This will make it so you don't get prompted for a password every time you run a debugger/profiler. +# +# After running this script, you can either use the `dest/Ruby.app/Contents/macOS/ruby` binary from there, +# or copy it to another place of your choosing. + +require "rbconfig" +require "pathname" +require "tempfile" + +# Inputs +$signing_cert_name = "'Alexander Momchilov (Shopify)'" +destination_folder = Pathname.pwd + +app_bundle = create_bundle_skeleton(at: destination_folder) + +copy_ruby_executable(into: app_bundle) + +sign_bundle(app_bundle) + +if verify_entitlements(app_bundle) + puts "Successfully created a signed Ruby at: #{app_bundle}" + exit(true) +else + puts "Something went wrong." + exit(false) +end + + + + + +BEGIN { # Helper methods + def create_bundle_skeleton(at:) + destination = at + + app_bundle = (destination / "Ruby.app").expand_path + contents_folder = app_bundle / "Contents" + + contents_folder.mkpath + + info_plist_content = <<~INFO_PLIST + + + + + CFBundleInfoDictionaryVersion + 6.0 + CFBundleExecutable + ruby + CFBundleIdentifier + com.shopify.amomchilov.Ruby + CFBundleName + Ruby + CFBundleDisplayName + Ruby + CFBundleShortVersionString + #{RUBY_VERSION} + CFBundleVersion + #{RUBY_VERSION}p#{RUBY_PATCHLEVEL} + NSHumanReadableCopyright + #{RUBY_COPYRIGHT.delete_prefix("ruby - ")} + + + INFO_PLIST + + (contents_folder / "Info.plist").write(info_plist_content) + + app_bundle + end + + def copy_ruby_executable(into:) + app_bundle = into + destination = (app_bundle / "Contents/MacOS") + + begin + destination.mkpath + rescue Errno::EEXIST + # Folder already exists. No problem. + end + + original_ruby = Pathname.new(RbConfig.ruby) + puts "Copying Ruby #{RUBY_VERSION} from #{original_ruby}" + FileUtils.cp(original_ruby, destination) + destination + end + + def sign_bundle(bundle_path) + entitlements_plist = <<~PLIST + + + + + com.apple.security.get-task-allow + + + + com.apple.security.cs.disable-library-validation + + + + PLIST + + Tempfile.create("entitlements.plist") do |entitlements_file| + entitlements_path = entitlements_file.path + + args = [ + "/usr/bin/codesign", + "--force", # Replace the existing signiture, if any + "--sign", $signing_cert_name, + "-o", "runtime", + "--entitlements", entitlements_file.path, + "--timestamp\\=none", + "--generate-entitlement-der", # necessary? + "#{bundle_path}" + ] + + entitlements_file.puts(entitlements_plist) + entitlements_file.flush + + puts "\nSigning #{bundle_path}." + puts "----- codesign output:" + system(args.join(" "), exception: true) + puts "-----\n\n" + end + + nil + end + + def verify_entitlements(bundle_path) + puts "Verifying the code signature..." + entitlements_xml = `codesign --display --entitlements - --xml #{bundle_path} 2>/dev/null` + + disable_lib_validation = "com.apple.security.cs.disable-library-validation" + allow_debugging = "com.apple.security.get-task-allow" + + issues = [] + + # Doing dumb string matching, so we don't need to pull in an XML/Plist parser. + if entitlements_xml.include?(disable_lib_validation) + if entitlements_xml.include?("#{disable_lib_validation}") + puts "\t- ✅ Entitlement #{disable_lib_validation.inspect} was set correctly" + else + issues << "\t- ❌ #{disable_lib_validation.inspect} was not `true` in the bundle's entitlements." + end + else + issues << "\t- ❌ #{disable_lib_validation.inspect} was missing from the bundle's entitlements." + end + + if entitlements_xml.include?(allow_debugging) + if entitlements_xml.include?("#{allow_debugging}") + puts "\t- ✅ Entitlement #{allow_debugging.inspect} was set correctly" + else + issues << "\t- ❌ #{allow_debugging.inspect} was not `true` in the bundle's entitlements." + end + else + issues << "\t- ❌ #{allow_debugging.inspect} was missing from the bundle's entitlements." + end + + if issues.any? + puts "There were issues with the code-signing:" + puts issues + puts + return false + end + + puts + + true + end +} diff --git a/trace_rbs.sh b/trace_rbs.sh new file mode 100755 index 000000000..a88300336 --- /dev/null +++ b/trace_rbs.sh @@ -0,0 +1,99 @@ +# Profile the RBS tests in Instruments. +# +# Based on https://www.jviotti.com/2024/01/29/using-xcode-instruments-for-cpp-cpu-profiling.html +# +# The usual Instruments templates are: +# --template 'Allocations' +# --template 'CPU Profiler' +# +# ...but I made myself a custom template called "Ruby" with these instruments: +# - Allocations +# - Points of Integer +# - os_signpost +# - stdout/stderr +# - Time Profiler +# - Sampler +# +# The "Leaks" instrument doesn't work, and IDK why... some issue with libmalloc. + +cd /Users/alex/src/github.com/Shopify/rbs + +source /opt/dev/sh/chruby/chruby.sh +chruby 3.3.4 + +TRACE_FILE="$(mktemp -t trace_XXXXXX).trace" + +echo "Will save trace file to $TRACE_FILE" + +xcrun xctrace record \ + --template 'Ruby' \ + --no-prompt \ + --output "$TRACE_FILE" \ + --target-stdout - \ + --launch \ + -- \ + /Users/alex/Downloads/ruby \ + -w -Ilib -Itest \ + /Users/alex/.gem/ruby/3.3.0/gems/rake-13.2.1/lib/rake/rake_test_loader.rb \ + "test/rbs/signature_parsing_test.rb" \ + +# "test/rbs/ancestor_builder_test.rb" \ +# "test/rbs/ancestor_graph_test.rb" \ +# "test/rbs/annotate/annotations_test.rb" \ +# "test/rbs/annotate/rdoc_annotator_test.rb" \ +# "test/rbs/annotate/rdoc_source_test.rb" \ +# "test/rbs/ast/type_param_test.rb" \ +# "test/rbs/ast/visitor_test.rb" \ +# "test/rbs/buffer_test.rb" \ +# "test/rbs/cli_test.rb" \ +# "test/rbs/collection/cleaner_test.rb" \ +# "test/rbs/collection/config_test.rb" \ +# "test/rbs/collection/installer_test.rb" \ +# "test/rbs/collection/sources/git_test.rb" \ +# "test/rbs/collection/sources/local_test.rb" \ +# "test/rbs/collection/sources/stdlib_test.rb" \ +# "test/rbs/definition_builder_test.rb" \ +# "test/rbs/diff_test.rb" \ +# "test/rbs/environment_loader_test.rb" \ +# "test/rbs/environment_test.rb" \ +# "test/rbs/environment_walker_test.rb" \ +# "test/rbs/errors_test.rb" \ +# "test/rbs/factory_test.rb" \ +# "test/rbs/file_finder_test.rb" \ +# "test/rbs/location_test.rb" \ +# "test/rbs/locator_test.rb" \ +# "test/rbs/method_builder_test.rb" \ +# "test/rbs/method_type_parsing_test.rb" \ +# "test/rbs/node_usage_test.rb" \ +# "test/rbs/parser_test.rb" \ +# "test/rbs/rb_prototype_test.rb" \ +# "test/rbs/rbi_prototype_test.rb" \ +# "test/rbs/rdoc/rbs_parser_test.rb" \ +# "test/rbs/repository_test.rb" \ +# "test/rbs/resolver/constant_resolver_test.rb" \ +# "test/rbs/resolver/type_name_resolver_test.rb" \ +# "test/rbs/runtime_prototype_test.rb" \ +# "test/rbs/schema_test.rb" \ +# "test/rbs/sorter_test.rb" \ +# "test/rbs/subtractor_test.rb" \ +# "test/rbs/test/hook_test.rb" \ +# "test/rbs/test/runtime_test_test.rb" \ +# "test/rbs/test/setup_helper_test.rb" \ +# "test/rbs/test/tester_test.rb" \ +# "test/rbs/test/type_check_test.rb" \ +# "test/rbs/type_alias_dependency_test.rb" \ +# "test/rbs/type_alias_regulartiry_test.rb" \ +# "test/rbs/type_parsing_test.rb" \ +# "test/rbs/types_test.rb" \ +# "test/rbs/use_map_test.rb" \ +# "test/rbs/variance_calculator_test.rb" \ +# "test/rbs/vendorer_test.rb" \ +# "test/rbs/writer_test.rb" \ +# "test/validator_test.rb" + +echo "xtrace exited with code $?" +# 2 => fail +# 0 => success +# 54 => also success? + +open "$TRACE_FILE" From 2221f5f66253f6c331c120b6bac6e84b5c5a3fe6 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Mon, 24 Feb 2025 18:46:20 +0000 Subject: [PATCH 070/111] Add missing stdint import --- src/util/rbs_allocator.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/util/rbs_allocator.c b/src/util/rbs_allocator.c index 122f89f3a..5dfd07ec2 100644 --- a/src/util/rbs_allocator.c +++ b/src/util/rbs_allocator.c @@ -19,6 +19,7 @@ #include #include #include // for memset() +#include #ifdef _WIN32 #include From c5f04597d230089b947d8185a33af3e71fa9631a Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Mon, 24 Feb 2025 19:36:32 +0000 Subject: [PATCH 071/111] Remove unused local to please RuboCop --- make_signed_ruby.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/make_signed_ruby.rb b/make_signed_ruby.rb index 74be88294..11ded48e1 100644 --- a/make_signed_ruby.rb +++ b/make_signed_ruby.rb @@ -123,8 +123,6 @@ def sign_bundle(bundle_path) PLIST Tempfile.create("entitlements.plist") do |entitlements_file| - entitlements_path = entitlements_file.path - args = [ "/usr/bin/codesign", "--force", # Replace the existing signiture, if any From f785525aa7fc107503d44122dab4d7c5c557c07a Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Mon, 24 Feb 2025 20:54:37 +0000 Subject: [PATCH 072/111] Avoid using vasprintf vasprintf is not available on all platforms (e.g. Windows). So we should switch to vsnprintf even tho it'd complicate the code a bit. --- src/parser.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/parser.c b/src/parser.c index 5399c579f..fccbfb97d 100644 --- a/src/parser.c +++ b/src/parser.c @@ -130,11 +130,16 @@ void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt return; } - char *message; - va_list args; + + va_start(args, fmt); + int length = vsnprintf(NULL, 0, fmt, args); + va_end(args); + + char *message = (char *) malloc(length + 1); + va_start(args, fmt); - vasprintf(&message, fmt, args); + vsnprintf(message, length + 1, fmt, args); va_end(args); state->error = (error *)malloc(sizeof(error)); From 503705488f3e12168ebbfe0896a9545359f36c00 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Tue, 25 Feb 2025 15:20:39 +0000 Subject: [PATCH 073/111] Fix MSVC compatibility issues This commit addresses several compatibility issues with MSVC compiler: 1. Updated NODISCARD macro in defines.h to use MSVC-specific _Check_return_ attribute instead of GCC/Clang's __attribute__((warn_unused_result)). 2. Fixed variable-length array (VLA) parameter in rbs_parser_declare_type_variables function. Changed from `const char *variables[count]` to `const char **variables` since VLAs are not supported in MSVC. 3. Added the declaration of rbs_parser_declare_type_variables to parserstate.h to ensure proper function prototyping. 4. Rename `error` label to `error_handling` to avoid syntax error on mswin. These changes maintain the same functionality while ensuring compatibility with MSVC, allowing the RBS extension to build successfully on Windows. --- include/rbs/defines.h | 4 ++++ include/rbs/parserstate.h | 5 +++++ src/parser.c | 10 +++++----- src/parserstate.c | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/rbs/defines.h b/include/rbs/defines.h index adac6dc5a..3a4ffb893 100644 --- a/include/rbs/defines.h +++ b/include/rbs/defines.h @@ -51,6 +51,10 @@ * Custom defines for RBS * **********************************************************************************************************************/ +#if defined(_MSC_VER) +#define NODISCARD _Check_return_ +#else #define NODISCARD __attribute__((warn_unused_result)) +#endif #endif diff --git a/include/rbs/parserstate.h b/include/rbs/parserstate.h index c6d2ce528..cef37aaf6 100644 --- a/include/rbs/parserstate.h +++ b/include/rbs/parserstate.h @@ -130,6 +130,11 @@ lexstate *alloc_lexer(rbs_allocator_t *, rbs_string_t string, const rbs_encoding parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); void free_parser(parserstate *parser); +/** + * Declare type variables to be used during parsing. + * */ +void rbs_parser_declare_type_variables(parserstate *parser, size_t count, const char **variables); + /** * Advance one token. * */ diff --git a/src/parser.c b/src/parser.c index fccbfb97d..8167c9967 100644 --- a/src/parser.c +++ b/src/parser.c @@ -207,15 +207,15 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb switch (state->current_token.type) { case tLIDENT: if (kind & ALIAS_NAME) goto success; - goto error; + goto error_handling; case tULIDENT: if (kind & INTERFACE_NAME) goto success; - goto error; + goto error_handling; case tUIDENT: if (kind & CLASS_NAME) goto success; - goto error; + goto error_handling; default: - goto error; + goto error_handling; } success: { @@ -230,7 +230,7 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb return true; } - error: { + error_handling: { const char *ids; if (kind & ALIAS_NAME) { ids = "alias name"; diff --git a/src/parserstate.c b/src/parserstate.c index a50c9482d..aeb801a08 100644 --- a/src/parserstate.c +++ b/src/parserstate.c @@ -355,7 +355,7 @@ parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, i return parser; } -void rbs_parser_declare_type_variables(parserstate *parser, size_t count, const char *variables[count]) { +void rbs_parser_declare_type_variables(parserstate *parser, size_t count, const char **variables) { if (variables == NULL) return; // Nothing to do. parser_push_typevar_table(parser, true); From 98d0ebcdaf036d10a617b68386a7eb07c33e7d6a Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Tue, 25 Feb 2025 17:12:16 +0000 Subject: [PATCH 074/111] Assert ids always gets assigned --- src/parser.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/parser.c b/src/parser.c index 8167c9967..daf4e5eae 100644 --- a/src/parser.c +++ b/src/parser.c @@ -231,7 +231,7 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb } error_handling: { - const char *ids; + const char *ids = NULL; if (kind & ALIAS_NAME) { ids = "alias name"; } @@ -242,6 +242,8 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb ids = "class/module/constant name"; } + assert(ids != NULL && "Unknown kind of type"); + set_error(state, state->current_token, true, "expected one of %s", ids); return false; } From 8f8b05a945e510bd15e5b4d492610a93290abd6e Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Mon, 24 Feb 2025 18:03:22 +0000 Subject: [PATCH 075/111] Avoid using rb_raise in parser --- src/rbs_location.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/rbs_location.c b/src/rbs_location.c index 1b5a6fcdb..d571b1c73 100644 --- a/src/rbs_location.c +++ b/src/rbs_location.c @@ -1,12 +1,14 @@ #include "rbs/rbs_location.h" -#include "location.h" + +#include #define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1)) static void check_children_max(unsigned short n) { size_t max = sizeof(rbs_loc_entry_bitmap) * 8; if (n > max) { - rb_raise(rb_eRuntimeError, "Too many children added to location: %d", n); + fprintf(stderr, "Too many children added to location: %d", n); + exit(EXIT_FAILURE); } } From 823c4d15f16091544e01767bfb5f0e2c3a0025f5 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Mon, 24 Feb 2025 18:13:07 +0000 Subject: [PATCH 076/111] Fix incorrect Qtrue usage and remove unnecessary import --- src/parser.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/parser.c b/src/parser.c index daf4e5eae..77f5852e2 100644 --- a/src/parser.c +++ b/src/parser.c @@ -9,7 +9,6 @@ #include "rbs/defines.h" #include "rbs/encoding.h" #include "rbs/rbs_string.h" -#include "ast_translation.h" #include "rbs/rbs_unescape.h" #define INTERN(str) \ @@ -1315,7 +1314,7 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa range variance_range = NULL_RANGE; if (module_type_params) { if (state->next_token.type == kUNCHECKED) { - unchecked = Qtrue; + unchecked = true; parser_advance(state); unchecked_range = state->current_token.range; } From ead4c201c8d1ef949312531f20b6c3527d3775e0 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Thu, 6 Mar 2025 19:29:47 +0000 Subject: [PATCH 077/111] Remove debugging assignment This code was added for easier debugging but it's not needed anymore and would fail memory leak checks. --- src/util/rbs_allocator.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/util/rbs_allocator.c b/src/util/rbs_allocator.c index 5dfd07ec2..045ed34ad 100644 --- a/src/util/rbs_allocator.c +++ b/src/util/rbs_allocator.c @@ -125,10 +125,6 @@ void rbs_allocator_free(rbs_allocator_t *allocator) { free(page); page = next; } - - *allocator = (rbs_allocator_t) { - .page = NULL, - }; } // Allocates `size` bytes from `allocator`, aligned to an `alignment`-byte boundary. From 6f475a760bdc42761b02662a0f40481bd4dca7a9 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Thu, 6 Mar 2025 23:40:00 +0000 Subject: [PATCH 078/111] Simplify rbs_location_current_token --- src/parser.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/parser.c b/src/parser.c index 77f5852e2..d79efddfd 100644 --- a/src/parser.c +++ b/src/parser.c @@ -98,11 +98,7 @@ static bool rbs_is_untyped_params(method_params *params) { // * @return New RBS::Location object. // * */ static rbs_location_t *rbs_location_current_token(parserstate *state) { - // TODO: Can this just be simplified to `return rbs_location_new(state->current_token.range);`? - return rbs_location_pp( - &state->current_token.range.start, - &state->current_token.range.end - ); + return rbs_location_new(state->current_token.range); } static bool parse_optional(parserstate *state, rbs_node_t **optional); From 4ecb007f2a3882aaf77805e678a1a2e386d71edc Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Thu, 6 Mar 2025 23:50:51 +0000 Subject: [PATCH 079/111] Allocate location with allocator Rename alloc_location to rbs_location_new Use rbs_location_current_token when possible --- include/rbs/rbs_location.h | 10 +-- src/parser.c | 158 ++++++++++++++++++------------------- src/parserstate.c | 2 +- src/rbs_location.c | 29 +++---- 4 files changed, 94 insertions(+), 105 deletions(-) diff --git a/include/rbs/rbs_location.h b/include/rbs/rbs_location.h index 797e5ecd3..bc1d240a4 100644 --- a/include/rbs/rbs_location.h +++ b/include/rbs/rbs_location.h @@ -4,6 +4,7 @@ #include "lexer.h" #include "rbs/util/rbs_constant_pool.h" +#include "rbs/util/rbs_allocator.h" #include "rbs/rbs_location_internals.h" typedef struct rbs_location { @@ -11,18 +12,13 @@ typedef struct rbs_location { rbs_loc_children *children; } rbs_location_t; -rbs_location_t *rbs_location_new(range rg); void rbs_loc_alloc_children(rbs_location_t *loc, int size); void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, range r); void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r); /** - * Returns rbs_location_t struct with start/end positions. - * - * @param start_pos - * @param end_pos - * @return New rbs_location_t struct. + * Allocate new rbs_location_t object through the given allocator. * */ -rbs_location_t *rbs_location_pp(const position *start_pos, const position *end_pos); +rbs_location_t *rbs_location_new(rbs_allocator_t *allocator, range rg); #endif diff --git a/src/parser.c b/src/parser.c index d79efddfd..8badac5cf 100644 --- a/src/parser.c +++ b/src/parser.c @@ -98,7 +98,7 @@ static bool rbs_is_untyped_params(method_params *params) { // * @return New RBS::Location object. // * */ static rbs_location_t *rbs_location_current_token(parserstate *state) { - return rbs_location_new(state->current_token.range); + return rbs_location_new(&state->allocator, state->current_token.range); } static bool parse_optional(parserstate *state, rbs_node_t **optional); @@ -184,7 +184,7 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb && state->next_token.range.end.byte_pos == state->next_token2.range.start.byte_pos ) { rbs_constant_id_t symbol_value = INTERN_TOKEN(state, state->current_token); - rbs_location_t *symbolLoc = rbs_location_new(state->next_token.range); + rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, state->next_token.range); rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, symbol_value); rbs_node_list_append(path, (rbs_node_t *)symbol); @@ -196,7 +196,7 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb .start = rg->start, .end = state->current_token.range.end }; - rbs_location_t *loc = rbs_location_new(namespace_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, namespace_range); rbs_namespace_t *namespace = rbs_namespace_new(&state->allocator, loc, path, absolute); switch (state->current_token.type) { @@ -218,10 +218,10 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb rg->end = state->current_token.range.end; } - rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); + rbs_location_t *symbolLoc = rbs_location_current_token(state); rbs_constant_id_t name = INTERN_TOKEN(state, state->current_token); rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, name); - *typename = rbs_typename_new(&state->allocator, rbs_location_new(*rg), namespace, symbol); + *typename = rbs_typename_new(&state->allocator, rbs_location_new(&state->allocator, *rg), namespace, symbol); return true; } @@ -305,7 +305,7 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t if (state->next_token.type == pCOMMA || state->next_token.type == pRPAREN) { range param_range = type_range; - rbs_location_t *loc = rbs_location_new(param_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, param_range); rbs_loc_alloc_children(loc, 1); rbs_loc_add_optional_child(loc, INTERN("name"), NULL_RANGE); @@ -327,11 +327,11 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t } rbs_string_t unquoted_str = rbs_unquote_string(rbs_parser_peek_current_token(state)); - rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); + rbs_location_t *symbolLoc = rbs_location_current_token(state); rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); - rbs_location_t *loc = rbs_location_new(param_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, param_range); rbs_loc_alloc_children(loc, 1); rbs_loc_add_optional_child(loc, INTERN("name"), name_range); @@ -357,7 +357,7 @@ NODISCARD static bool parse_keyword_key(parserstate *state, rbs_ast_symbol_t **key) { parser_advance(state); - rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); + rbs_location_t *symbolLoc = rbs_location_current_token(state); if (state->next_token.type == pQUESTION) { *key = rbs_ast_symbol_new( @@ -386,7 +386,7 @@ static bool parse_keyword(parserstate *state, rbs_hash_t *keywords, rbs_hash_t * set_error(state, state->current_token, true, "duplicated keyword argument"); return false; } else { - rbs_location_t *loc = rbs_location_new(state->current_token.range); + rbs_location_t *loc = rbs_location_current_token(state); rbs_hash_set(memo, (rbs_node_t *) key, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, loc, true)); } @@ -627,7 +627,7 @@ static bool parse_optional(parserstate *state, rbs_node_t **optional) { if (state->next_token.type == pQUESTION) { parser_advance(state); rg.end = state->current_token.range.end; - rbs_location_t *location = rbs_location_new(rg); + rbs_location_t *location = rbs_location_new(&state->allocator, rg); *optional = (rbs_node_t *) rbs_types_optional_new(&state->allocator, location, type); } else { *optional = type; @@ -737,7 +737,7 @@ static bool parse_function(parserstate *state, bool accept_type_binding, parse_f rbs_node_t *block_function = NULL; function_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(function_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, function_range); if (rbs_is_untyped_params(&block_params)) { block_function = (rbs_node_t *) rbs_types_untypedfunction_new(&state->allocator, loc, block_return_type); } else { @@ -765,7 +765,7 @@ static bool parse_function(parserstate *state, bool accept_type_binding, parse_f CHECK_PARSE(parse_optional(state, &type)); function_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(function_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, function_range); if (rbs_is_untyped_params(¶ms)) { function = (rbs_node_t *) rbs_types_untypedfunction_new(&state->allocator, loc, type); } else { @@ -806,7 +806,7 @@ static bool parse_proc_type(parserstate *state, rbs_types_proc_t **proc) { CHECK_PARSE(parse_function(state, true, &result)); position end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_pp(&start, &end); + rbs_location_t *loc = rbs_location_new(&state->allocator, (range) { .start = start, .end = end }); *proc = rbs_types_proc_new(&state->allocator, loc, result->function, result->block, result->function_self_type); return true; } @@ -881,7 +881,7 @@ static bool parse_record_attributes(parserstate *state, rbs_hash_t **fields) { CHECK_PARSE(parse_type(state, &type)); field_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(field_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, field_range); rbs_hash_set(*fields, (rbs_node_t *) key, (rbs_node_t *) rbs_types_record_fieldtype_new(&state->allocator, loc, type, required)); if (parser_advance_if(state, pCOMMA)) { @@ -908,7 +908,7 @@ static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types switch (state->current_token.type) { case tSYMBOL: { - rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); + rbs_location_t *symbolLoc = rbs_location_current_token(state); char *buffer = peek_token(state->lexstate, state->current_token); rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared( @@ -921,7 +921,7 @@ static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types } case tDQSYMBOL: case tSQSYMBOL: { - rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); + rbs_location_t *symbolLoc = rbs_location_current_token(state); rbs_string_t current_token = rbs_parser_peek_current_token(state); rbs_string_t symbol = rbs_string_copy_slice(¤t_token, offset_bytes, rbs_string_len(current_token) - offset_bytes); @@ -991,7 +991,7 @@ static bool parse_instance_type(parserstate *state, bool parse_alias, rbs_node_t .end = nonnull_pos_or(args_range.end, name_range.end), }; - rbs_location_t *loc = rbs_location_new(type_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, type_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); @@ -1026,7 +1026,7 @@ static bool parse_singleton_type(parserstate *state, rbs_types_classsingleton_t ADVANCE_ASSERT(state, pRPAREN); type_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(type_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, type_range); rbs_loc_alloc_children(loc, 1); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -1183,7 +1183,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { ADVANCE_ASSERT(state, pRBRACKET); rg.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(rg); + rbs_location_t *loc = rbs_location_new(&state->allocator, rg); *type = (rbs_node_t *) rbs_types_tuple_new(&state->allocator, loc, types); return true; } @@ -1199,7 +1199,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { CHECK_PARSE(parse_record_attributes(state, &fields)); ADVANCE_ASSERT(state, pRBRACE); position end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_pp(&start, &end); + rbs_location_t *loc = rbs_location_new(&state->allocator, (range) { .start = start, .end = end }); *type = (rbs_node_t *) rbs_types_record_new(&state->allocator, loc, fields); return true; } @@ -1241,7 +1241,7 @@ static bool parse_intersection(parserstate *state, rbs_node_t **type) { rg.end = state->current_token.range.end; if (intersection_types->length > 1) { - rbs_location_t *location = rbs_location_new(rg); + rbs_location_t *location = rbs_location_new(&state->allocator, rg); *type = (rbs_node_t *) rbs_types_intersection_new(&state->allocator, location, intersection_types); } @@ -1271,7 +1271,7 @@ bool parse_type(parserstate *state, rbs_node_t **type) { rg.end = state->current_token.range.end; if (union_types->length > 1) { - rbs_location_t *location = rbs_location_new(rg); + rbs_location_t *location = rbs_location_new(&state->allocator, rg); *type = (rbs_node_t *) rbs_types_union_new(&state->allocator, location, union_types); } @@ -1299,7 +1299,7 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa while (true) { bool unchecked = false; - rbs_keyword_t *variance = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("invariant")); + rbs_keyword_t *variance = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("invariant")); rbs_node_t *upper_bound = NULL; rbs_node_t *default_type = NULL; @@ -1318,10 +1318,10 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa if (state->next_token.type == kIN || state->next_token.type == kOUT) { switch (state->next_token.type) { case kIN: - variance = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("contravariant")); + variance = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("contravariant")); break; case kOUT: - variance = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("covariant")); + variance = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("covariant")); break; default: set_error(state, state->current_token, false, "Unexpected error"); @@ -1337,7 +1337,7 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa range name_range = state->current_token.range; rbs_string_t string = rbs_parser_peek_current_token(state); - rbs_location_t *nameSymbolLoc = rbs_location_new(state->current_token.range); + rbs_location_t *nameSymbolLoc = rbs_location_current_token(state); rbs_constant_id_t id = rbs_constant_pool_insert_string(&state->constant_pool, string); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, nameSymbolLoc, &state->constant_pool, id); @@ -1371,7 +1371,7 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa param_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(param_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, param_range); rbs_loc_alloc_children(loc, 5); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("variance"), variance_range); @@ -1426,7 +1426,7 @@ bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type) { CHECK_PARSE(parser_pop_typevar_table(state)); - rbs_location_t *loc = rbs_location_new(rg); + rbs_location_t *loc = rbs_location_new(&state->allocator, rg); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("type"), type_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range); @@ -1446,7 +1446,7 @@ static bool parse_global_decl(parserstate *state, rbs_ast_declarations_global_t rbs_ast_comment_t *comment = get_comment(state, decl_range.start.line); range name_range = state->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(name_range); + rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, name_range); rbs_ast_symbol_t *typename = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); @@ -1457,7 +1457,7 @@ static bool parse_global_decl(parserstate *state, rbs_ast_declarations_global_t CHECK_PARSE(parse_type(state, &type)); decl_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(decl_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); @@ -1488,7 +1488,7 @@ static bool parse_const_decl(parserstate *state, rbs_ast_declarations_constant_t decl_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(decl_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); @@ -1528,7 +1528,7 @@ static bool parse_type_decl(parserstate *state, position comment_pos, rbs_node_l decl_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(decl_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); rbs_loc_alloc_children(loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -1599,7 +1599,7 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati rbs_string_t stripped_annotation_str = rbs_string_strip_whitespace(&annotation_str); rbs_string_free(&annotation_str); - *annotation = rbs_ast_annotation_new(&state->allocator, rbs_location_new(rg), stripped_annotation_str); + *annotation = rbs_ast_annotation_new(&state->allocator, rbs_location_new(&state->allocator, rg), stripped_annotation_str); return true; } @@ -1657,11 +1657,11 @@ static bool parse_method_name(parserstate *state, range *range, rbs_ast_symbol_t state->lexstate->encoding ); - rbs_location_t *symbolLoc = rbs_location_new(*range); + rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, *range); *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); } else { *range = state->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(*range); + rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, *range); *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); } return true; @@ -1669,7 +1669,7 @@ static bool parse_method_name(parserstate *state, range *range, rbs_ast_symbol_t case tBANGIDENT: case tEQIDENT: { *range = state->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(*range); + rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, *range); *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); return true; } @@ -1677,7 +1677,7 @@ static bool parse_method_name(parserstate *state, range *range, rbs_ast_symbol_t rbs_string_t string = rbs_parser_peek_current_token(state); rbs_string_t unquoted_str = rbs_unquote_string(string); rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str); - rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); + rbs_location_t *symbolLoc = rbs_location_current_token(state); *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); return true; } @@ -1691,7 +1691,7 @@ static bool parse_method_name(parserstate *state, range *range, rbs_ast_symbol_t case pAREF_OPR: case tOPERATOR: { *range = state->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(*range); + rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, *range); *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); return true; } @@ -1773,14 +1773,14 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept { case kPRIVATE: { visibility_range = state->current_token.range; - visibility = rbs_keyword_new(&state->allocator, rbs_location_new(visibility_range), INTERN("private")); + visibility = rbs_keyword_new(&state->allocator, rbs_location_new(&state->allocator, visibility_range), INTERN("private")); member_range.start = visibility_range.start; parser_advance(state); break; } case kPUBLIC: { visibility_range = state->current_token.range; - visibility = rbs_keyword_new(&state->allocator, rbs_location_new(visibility_range), INTERN("public")); + visibility = rbs_keyword_new(&state->allocator, rbs_location_new(&state->allocator, visibility_range), INTERN("public")); member_range.start = visibility_range.start; parser_advance(state); break; @@ -1843,7 +1843,7 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept CHECK_PARSE(parse_method_type(state, &method_type)); overload_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(overload_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, overload_range); rbs_node_t *overload = (rbs_node_t *) rbs_ast_members_methoddefinition_overload_new(&state->allocator, loc, annotations, (rbs_node_t *) method_type); rbs_node_list_append(overloads, overload); member_range.end = state->current_token.range.end; @@ -1880,15 +1880,15 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept rbs_keyword_t *k; switch (kind) { case INSTANCE_KIND: { - k = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("instance")); + k = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("instance")); break; } case SINGLETON_KIND: { - k = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("singleton")); + k = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("singleton")); break; } case INSTANCE_SINGLETON_KIND: { - k = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("singleton_instance")); + k = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("singleton_instance")); break; } default: @@ -1896,7 +1896,7 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept return false; } - rbs_location_t *loc = rbs_location_new(member_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(loc, 5); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -1991,7 +1991,7 @@ static bool parse_mixin_member(parserstate *state, bool from_interface, position member_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(member_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); @@ -2037,7 +2037,7 @@ static bool parse_alias_member(parserstate *state, bool instance_only, position range new_kind_range, old_kind_range, new_name_range, old_name_range; if (!instance_only && state->next_token.type == kSELF) { - kind = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("singleton")); + kind = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("singleton")); new_kind_range.start = state->next_token.range.start; new_kind_range.end = state->next_token2.range.end; @@ -2051,7 +2051,7 @@ static bool parse_alias_member(parserstate *state, bool instance_only, position ADVANCE_ASSERT(state, pDOT); CHECK_PARSE(parse_method_name(state, &old_name_range, &old_name)); } else { - kind = rbs_keyword_new(&state->allocator, rbs_location_new(state->current_token.range), INTERN("instance")); + kind = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("instance")); CHECK_PARSE(parse_method_name(state, &new_name_range, &new_name)); CHECK_PARSE(parse_method_name(state, &old_name_range, &old_name)); new_kind_range = NULL_RANGE; @@ -2059,7 +2059,7 @@ static bool parse_alias_member(parserstate *state, bool instance_only, position } member_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(member_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(loc, 5); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("new_name"), new_name_range); @@ -2092,7 +2092,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ { case tAIDENT: { range name_range = state->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(name_range); + rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, name_range); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); ADVANCE_ASSERT(state, pCOLON); @@ -2102,7 +2102,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ CHECK_PARSE(parse_type(state, &type)); member_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(member_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); @@ -2113,7 +2113,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ } case tA2IDENT: { range name_range = state->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(name_range); + rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, name_range); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); ADVANCE_ASSERT(state, pCOLON); @@ -2128,7 +2128,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ member_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(member_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); @@ -2147,7 +2147,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ ADVANCE_ASSERT(state, tAIDENT); range name_range = state->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(name_range); + rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, name_range); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); ADVANCE_ASSERT(state, pCOLON); @@ -2162,7 +2162,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ member_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(member_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); @@ -2188,7 +2188,7 @@ static bool parse_visibility_member(parserstate *state, rbs_node_list_t *annotat return false; } - rbs_location_t *location = rbs_location_new(state->current_token.range); + rbs_location_t *location = rbs_location_current_token(state); switch (state->current_token.type) { @@ -2234,13 +2234,13 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs { case kPRIVATE: { visibility_range = state->current_token.range; - visibility = rbs_keyword_new(&state->allocator, rbs_location_new(visibility_range), INTERN("private")); + visibility = rbs_keyword_new(&state->allocator, rbs_location_new(&state->allocator, visibility_range), INTERN("private")); parser_advance(state); break; } case kPUBLIC: { visibility_range = state->current_token.range; - visibility = rbs_keyword_new(&state->allocator, rbs_location_new(visibility_range), INTERN("public")); + visibility = rbs_keyword_new(&state->allocator, rbs_location_new(&state->allocator, visibility_range), INTERN("public")); parser_advance(state); break; } @@ -2258,7 +2258,7 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs rbs_keyword_t *kind = rbs_keyword_new( &state->allocator, - rbs_location_new(keyword_range), + rbs_location_new(&state->allocator, keyword_range), INTERN(((is_kind == INSTANCE_KIND) ? "instance" : "singleton")) ); @@ -2273,7 +2273,7 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs ivar_range.start = state->current_token.range.start; if (parser_advance_if(state, tAIDENT)) { - rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); + rbs_location_t *symbolLoc = rbs_location_current_token(state); ivar_name = (rbs_node_t *) rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); ivar_name_range = state->current_token.range; } else { @@ -2281,7 +2281,7 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs .start = state->current_token.range.start, .end = state->current_token.range.end }; - ivar_name = (rbs_node_t *) rbs_ast_bool_new(&state->allocator, rbs_location_new(false_range), false); + ivar_name = (rbs_node_t *) rbs_ast_bool_new(&state->allocator, rbs_location_new(&state->allocator, false_range), false); ivar_name_range = NULL_RANGE; } @@ -2305,7 +2305,7 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs member_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(member_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(loc, 7); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -2416,7 +2416,7 @@ static bool parse_interface_decl(parserstate *state, position comment_pos, rbs_n CHECK_PARSE(parser_pop_typevar_table(state)); - rbs_location_t *loc = rbs_location_new(member_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -2458,7 +2458,7 @@ static bool parse_module_self_types(parserstate *state, rbs_node_list_t *array) self_range.end = args_range.end = state->current_token.range.end; } - rbs_location_t *loc = rbs_location_new(self_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, self_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); @@ -2610,7 +2610,7 @@ static bool parse_module_decl0(parserstate *state, range keyword_range, rbs_type range end_range = state->current_token.range; decl_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(decl_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); rbs_loc_alloc_children(loc, 6); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -2657,7 +2657,7 @@ static bool parse_module_decl(parserstate *state, position comment_pos, rbs_node .end = old_name_range.end }; - rbs_location_t *loc = rbs_location_new(decl_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); rbs_loc_alloc_children(loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("new_name"), module_name_range); @@ -2693,7 +2693,7 @@ static bool parse_class_decl_super(parserstate *state, range *lt_range, rbs_ast_ super_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(super_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, super_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); @@ -2736,7 +2736,7 @@ static bool parse_class_decl0(parserstate *state, range keyword_range, rbs_typen CHECK_PARSE(parser_pop_typevar_table(state)); - rbs_location_t *loc = rbs_location_new(decl_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); rbs_loc_alloc_children(loc, 5); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); @@ -2778,7 +2778,7 @@ static bool parse_class_decl(parserstate *state, position comment_pos, rbs_node_ .end = old_name_range.end, }; - rbs_location_t *loc = rbs_location_new(decl_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); rbs_loc_alloc_children(loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("new_name"), class_name_range); @@ -2928,7 +2928,7 @@ static bool parse_namespace(parserstate *state, range *rg, rbs_namespace_t **nam while (true) { if (state->next_token.type == tUIDENT && state->next_token2.type == pCOLON2) { - rbs_location_t *symbolLoc = rbs_location_new(state->next_token.range); + rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, state->next_token.range); rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->next_token)); rbs_node_list_append(path, (rbs_node_t *)symbol); if (null_position_p(rg->start)) { @@ -2942,7 +2942,7 @@ static bool parse_namespace(parserstate *state, range *rg, rbs_namespace_t **nam } } - *namespace = rbs_namespace_new(&state->allocator, rbs_location_new(*rg), path, is_absolute); + *namespace = rbs_namespace_new(&state->allocator, rbs_location_new(&state->allocator, *rg), path, is_absolute); return true; } @@ -2973,9 +2973,9 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { ? state->current_token.range : (range) { .start = namespace_range.start, .end = state->current_token.range.end }; - rbs_location_t *symbolLoc = rbs_location_new(state->current_token.range); + rbs_location_t *symbolLoc = rbs_location_current_token(state); rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); - rbs_typename_t *type_name = rbs_typename_new(&state->allocator, rbs_location_new(type_name_range),namespace, symbol); + rbs_typename_t *type_name = rbs_typename_new(&state->allocator, rbs_location_new(&state->allocator, type_name_range),namespace, symbol); range keyword_range = NULL_RANGE; range new_name_range = NULL_RANGE; @@ -2989,13 +2989,13 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { if (ident_type == tLIDENT) ADVANCE_ASSERT(state, tLIDENT); if (ident_type == tULIDENT) ADVANCE_ASSERT(state, tULIDENT); - rbs_location_t *symbolLoc = rbs_location_new(new_name_range); + rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, new_name_range); new_name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); new_name_range = state->current_token.range; clause_range.end = new_name_range.end; } - rbs_location_t *loc = rbs_location_new(clause_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, clause_range); rbs_loc_alloc_children(loc, 3); rbs_loc_add_required_child(loc, INTERN("type_name"), type_name_range); rbs_loc_add_optional_child(loc, INTERN("keyword"), keyword_range); @@ -3014,7 +3014,7 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { range star_range = state->current_token.range; clause_range.end = star_range.end; - rbs_location_t *loc = rbs_location_new(clause_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, clause_range); rbs_loc_alloc_children(loc, 2); rbs_loc_add_required_child(loc, INTERN("namespace"), namespace_range); rbs_loc_add_required_child(loc, INTERN("star"), star_range); @@ -3055,7 +3055,7 @@ static bool parse_use_directive(parserstate *state, rbs_ast_directives_use_t **u range directive_range = keyword_range; directive_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(directive_range); + rbs_location_t *loc = rbs_location_new(&state->allocator, directive_range); rbs_loc_alloc_children(loc, 1); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); @@ -3090,6 +3090,6 @@ bool parse_signature(parserstate *state, rbs_signature_t **signature) { } signature_range.end = state->current_token.range.end; - *signature = rbs_signature_new(&state->allocator, rbs_location_new(signature_range), dirs, decls); + *signature = rbs_signature_new(&state->allocator, rbs_location_new(&state->allocator, signature_range), dirs, decls); return true; } diff --git a/src/parserstate.c b/src/parserstate.c index aeb801a08..d528f8f2e 100644 --- a/src/parserstate.c +++ b/src/parserstate.c @@ -204,7 +204,7 @@ static rbs_ast_comment_t *parse_comment_lines(parserstate *state, comment *com) return rbs_ast_comment_new( &state->allocator, - rbs_location_pp(&com->start, &com->end), + rbs_location_new(&state->allocator, (range) { .start = com->start, .end = com->end }), rbs_buffer_to_string(&rbs_buffer) ); } diff --git a/src/rbs_location.c b/src/rbs_location.c index d571b1c73..d9eff195a 100644 --- a/src/rbs_location.c +++ b/src/rbs_location.c @@ -29,24 +29,6 @@ static rbs_loc_range rbs_new_loc_range(range rg) { return r; } -rbs_location_t *rbs_location_pp(const position *start_pos, const position *end_pos) { - range rg = { *start_pos, *end_pos }; - rg.start = *start_pos; - rg.end = *end_pos; - - return rbs_location_new(rg); -} - -rbs_location_t *rbs_location_new(range rg) { - rbs_location_t *location = (rbs_location_t *)malloc(sizeof(rbs_location_t)); - *location = (rbs_location_t) { - .rg = rg, - .children = NULL, - }; - - return location; -} - void rbs_loc_alloc_children(rbs_location_t *loc, int capacity) { check_children_max(capacity); @@ -72,3 +54,14 @@ void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, ran loc->children->entries[i].name = name; loc->children->entries[i].rg = rbs_new_loc_range(r); } + +rbs_location_t *rbs_location_new(rbs_allocator_t *allocator, range rg) { + rbs_location_t *location = rbs_allocator_alloc(allocator, rbs_location_t); + *location = (rbs_location_t) { + .rg = rg, + .children = NULL, + }; + + return location; +} + From 5320431114d563e453699cd067ec2f41af743491 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Fri, 7 Mar 2025 20:22:21 +0000 Subject: [PATCH 080/111] Fix comment --- src/parser.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/parser.c b/src/parser.c index 8badac5cf..afe8da6ed 100644 --- a/src/parser.c +++ b/src/parser.c @@ -91,12 +91,12 @@ static bool rbs_is_untyped_params(method_params *params) { return params->required_positionals == NULL; } -// /** -// * Returns RBS::Location object of `current_token` of a parser state. -// * -// * @param state -// * @return New RBS::Location object. -// * */ +/** + * Returns RBS::Location object of `current_token` of a parser state. + * + * @param state + * @return New RBS::Location object. + * */ static rbs_location_t *rbs_location_current_token(parserstate *state) { return rbs_location_new(&state->allocator, state->current_token.range); } From ae4d316d9404a3e00605a9ccc4dce8c19a87ce98 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Tue, 11 Mar 2025 23:57:29 +0800 Subject: [PATCH 081/111] Fix location child memory leak (#10) * Make rbs_loca_add* methods take allocator * Remove unnecessary rbs_new_loc_range helper function * Make rbs_loc_alloc_children allocate through allocator * Rename check_children_cap to ensure_children_capacity * Implement rbs_allocator_realloc function * Reorganize location children functions * Update src/rbs_location.c Co-authored-by: Alexandre Terrasa <583144+Morriar@users.noreply.github.com> --------- Co-authored-by: Alexandre Terrasa <583144+Morriar@users.noreply.github.com> --- include/rbs/rbs_location.h | 8 +- include/rbs/util/rbs_allocator.h | 2 + src/parser.c | 216 +++++++++++++++---------------- src/rbs_location.c | 62 ++++----- src/util/rbs_allocator.c | 9 ++ 5 files changed, 148 insertions(+), 149 deletions(-) diff --git a/include/rbs/rbs_location.h b/include/rbs/rbs_location.h index bc1d240a4..c4a78f96e 100644 --- a/include/rbs/rbs_location.h +++ b/include/rbs/rbs_location.h @@ -12,13 +12,13 @@ typedef struct rbs_location { rbs_loc_children *children; } rbs_location_t; -void rbs_loc_alloc_children(rbs_location_t *loc, int size); -void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, range r); -void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r); +void rbs_loc_alloc_children(rbs_allocator_t *, rbs_location_t *loc, int size); +void rbs_loc_add_required_child(rbs_allocator_t *, rbs_location_t *loc, rbs_constant_id_t name, range r); +void rbs_loc_add_optional_child(rbs_allocator_t *, rbs_location_t *loc, rbs_constant_id_t name, range r); /** * Allocate new rbs_location_t object through the given allocator. * */ -rbs_location_t *rbs_location_new(rbs_allocator_t *allocator, range rg); +rbs_location_t *rbs_location_new(rbs_allocator_t *, range rg); #endif diff --git a/include/rbs/util/rbs_allocator.h b/include/rbs/util/rbs_allocator.h index 0a53cbe89..28b9360e9 100644 --- a/include/rbs/util/rbs_allocator.h +++ b/include/rbs/util/rbs_allocator.h @@ -23,9 +23,11 @@ void rbs_allocator_init(rbs_allocator_t *); void rbs_allocator_free(rbs_allocator_t *); void *rbs_allocator_malloc_impl(rbs_allocator_t *, /* 1 */ size_t size, size_t alignment); void *rbs_allocator_calloc_impl(rbs_allocator_t *, size_t count, size_t size, size_t alignment); +void *rbs_allocator_realloc_impl(rbs_allocator_t *, void *ptr, size_t old_size, size_t new_size, size_t alignment); #define rbs_allocator_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), alignof(type))) #define rbs_allocator_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), alignof(type))) +#define rbs_allocator_realloc(allocator, ptr, old_size, new_size, type) ((type *) rbs_allocator_realloc_impl((allocator), (ptr), (old_size), (new_size), alignof(type))) void rbs__init_arena_allocator(void); diff --git a/src/parser.c b/src/parser.c index afe8da6ed..d838fe2cc 100644 --- a/src/parser.c +++ b/src/parser.c @@ -306,8 +306,8 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t range param_range = type_range; rbs_location_t *loc = rbs_location_new(&state->allocator, param_range); - rbs_loc_alloc_children(loc, 1); - rbs_loc_add_optional_child(loc, INTERN("name"), NULL_RANGE); + rbs_loc_alloc_children(&state->allocator, loc, 1); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("name"), NULL_RANGE); *function_param = rbs_types_function_param_new(&state->allocator, loc, type, NULL); return true; @@ -332,8 +332,8 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); rbs_location_t *loc = rbs_location_new(&state->allocator, param_range); - rbs_loc_alloc_children(loc, 1); - rbs_loc_add_optional_child(loc, INTERN("name"), name_range); + rbs_loc_alloc_children(&state->allocator, loc, 1); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("name"), name_range); *function_param = rbs_types_function_param_new(&state->allocator, loc, type, name); return true; @@ -992,9 +992,9 @@ static bool parse_instance_type(parserstate *state, bool parse_alias, rbs_node_t }; rbs_location_t *loc = rbs_location_new(&state->allocator, type_range); - rbs_loc_alloc_children(loc, 2); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_optional_child(loc, INTERN("args"), args_range); + rbs_loc_alloc_children(&state->allocator, loc, 2); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("args"), args_range); if (kind == CLASS_NAME) { *type = (rbs_node_t *) rbs_types_classinstance_new(&state->allocator, loc, typename, types); @@ -1027,8 +1027,8 @@ static bool parse_singleton_type(parserstate *state, rbs_types_classsingleton_t type_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, type_range); - rbs_loc_alloc_children(loc, 1); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_alloc_children(&state->allocator, loc, 1); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); *singleton = rbs_types_classsingleton_new(&state->allocator, loc, typename); return true; @@ -1372,12 +1372,12 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa param_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, param_range); - rbs_loc_alloc_children(loc, 5); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_optional_child(loc, INTERN("variance"), variance_range); - rbs_loc_add_optional_child(loc, INTERN("unchecked"), unchecked_range); - rbs_loc_add_optional_child(loc, INTERN("upper_bound"), upper_bound_range); - rbs_loc_add_optional_child(loc, INTERN("default"), default_type_range); + rbs_loc_alloc_children(&state->allocator, loc, 5); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("variance"), variance_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("unchecked"), unchecked_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("upper_bound"), upper_bound_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("default"), default_type_range); rbs_ast_typeparam_t *param = rbs_ast_typeparam_new(&state->allocator, loc, name, variance, upper_bound, default_type, unchecked); @@ -1427,9 +1427,9 @@ bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type) { CHECK_PARSE(parser_pop_typevar_table(state)); rbs_location_t *loc = rbs_location_new(&state->allocator, rg); - rbs_loc_alloc_children(loc, 2); - rbs_loc_add_required_child(loc, INTERN("type"), type_range); - rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range); + rbs_loc_alloc_children(&state->allocator, loc, 2); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("type"), type_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("type_params"), params_range); *method_type = rbs_methodtype_new(&state->allocator, loc, type_params, result->function, result->block); return true; @@ -1458,9 +1458,9 @@ static bool parse_global_decl(parserstate *state, rbs_ast_declarations_global_t decl_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); - rbs_loc_alloc_children(loc, 2); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); + rbs_loc_alloc_children(&state->allocator, loc, 2); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("colon"), colon_range); *global = rbs_ast_declarations_global_new(&state->allocator, loc, typename, type, comment); return true; @@ -1489,9 +1489,9 @@ static bool parse_const_decl(parserstate *state, rbs_ast_declarations_constant_t decl_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); - rbs_loc_alloc_children(loc, 2); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); + rbs_loc_alloc_children(&state->allocator, loc, 2); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("colon"), colon_range); *constant = rbs_ast_declarations_constant_new(&state->allocator, loc, typename, type, comment); return true; @@ -1529,11 +1529,11 @@ static bool parse_type_decl(parserstate *state, position comment_pos, rbs_node_l decl_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); - rbs_loc_alloc_children(loc, 4); - rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range); - rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); + rbs_loc_alloc_children(&state->allocator, loc, 4); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("type_params"), params_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("eq"), eq_range); CHECK_PARSE(parser_pop_typevar_table(state)); @@ -1897,12 +1897,12 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept } rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(loc, 5); - rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range); - rbs_loc_add_optional_child(loc, INTERN("overloading"), overloading_range); - rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range); + rbs_loc_alloc_children(&state->allocator, loc, 5); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("kind"), kind_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("overloading"), overloading_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("visibility"), visibility_range); *method_definition = rbs_ast_members_methoddefinition_new(&state->allocator, loc, name, k, overloads, annotations, comment, overloading, visibility); return true; @@ -1992,10 +1992,10 @@ static bool parse_mixin_member(parserstate *state, bool from_interface, position member_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(loc, 3); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); - rbs_loc_add_optional_child(loc, INTERN("args"), args_range); + rbs_loc_alloc_children(&state->allocator, loc, 3); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("args"), args_range); rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); switch (type) @@ -2060,12 +2060,12 @@ static bool parse_alias_member(parserstate *state, bool instance_only, position member_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(loc, 5); - rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(loc, INTERN("new_name"), new_name_range); - rbs_loc_add_required_child(loc, INTERN("old_name"), old_name_range); - rbs_loc_add_optional_child(loc, INTERN("new_kind"), new_kind_range); - rbs_loc_add_optional_child(loc, INTERN("old_kind"), old_kind_range); + rbs_loc_alloc_children(&state->allocator, loc, 5); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("new_name"), new_name_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("old_name"), old_name_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("new_kind"), new_kind_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("old_kind"), old_kind_range); *alias_member = rbs_ast_members_alias_new(&state->allocator, loc, new_name, old_name, kind, annotations, comment); return true; @@ -2103,10 +2103,10 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ member_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(loc, 3); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); + rbs_loc_alloc_children(&state->allocator, loc, 3); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("colon"), colon_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("kind"), NULL_RANGE); *variable_member = (rbs_node_t *)rbs_ast_members_instancevariable_new(&state->allocator, loc, name, type, comment); return true; @@ -2129,10 +2129,10 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ member_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(loc, 3); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); + rbs_loc_alloc_children(&state->allocator, loc, 3); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("colon"), colon_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("kind"), NULL_RANGE); *variable_member = (rbs_node_t *) rbs_ast_members_classvariable_new(&state->allocator, loc, name, type, comment); return true; @@ -2163,10 +2163,10 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ member_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(loc, 3); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range); + rbs_loc_alloc_children(&state->allocator, loc, 3); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("colon"), colon_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("kind"), kind_range); *variable_member = (rbs_node_t *)rbs_ast_members_classinstancevariable_new(&state->allocator, loc, name, type, comment); return true; @@ -2306,14 +2306,14 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs member_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(loc, 7); - rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range); - rbs_loc_add_optional_child(loc, INTERN("ivar"), ivar_range); - rbs_loc_add_optional_child(loc, INTERN("ivar_name"), ivar_name_range); - rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range); + rbs_loc_alloc_children(&state->allocator, loc, 7); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("colon"), colon_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("kind"), kind_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("ivar"), ivar_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("ivar_name"), ivar_name_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("visibility"), visibility_range); switch (attr_type) { @@ -2417,11 +2417,11 @@ static bool parse_interface_decl(parserstate *state, position comment_pos, rbs_n CHECK_PARSE(parser_pop_typevar_table(state)); rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(loc, 4); - rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_required_child(loc, INTERN("end"), end_range); - rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range); + rbs_loc_alloc_children(&state->allocator, loc, 4); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("end"), end_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("type_params"), type_params_range); rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); @@ -2459,9 +2459,9 @@ static bool parse_module_self_types(parserstate *state, rbs_node_list_t *array) } rbs_location_t *loc = rbs_location_new(&state->allocator, self_range); - rbs_loc_alloc_children(loc, 2); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_optional_child(loc, INTERN("args"), args_range); + rbs_loc_alloc_children(&state->allocator, loc, 2); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("args"), args_range); rbs_ast_declarations_module_self_t *self_type = rbs_ast_declarations_module_self_new(&state->allocator, loc, module_name, args); rbs_node_list_append(array, (rbs_node_t *)self_type); @@ -2611,13 +2611,13 @@ static bool parse_module_decl0(parserstate *state, range keyword_range, rbs_type decl_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); - rbs_loc_alloc_children(loc, 6); - rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_required_child(loc, INTERN("end"), end_range); - rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range); - rbs_loc_add_optional_child(loc, INTERN("colon"), colon_range); - rbs_loc_add_optional_child(loc, INTERN("self_types"), self_types_range); + rbs_loc_alloc_children(&state->allocator, loc, 6); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("end"), end_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("type_params"), type_params_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("colon"), colon_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("self_types"), self_types_range); CHECK_PARSE(parser_pop_typevar_table(state)); @@ -2658,11 +2658,11 @@ static bool parse_module_decl(parserstate *state, position comment_pos, rbs_node }; rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); - rbs_loc_alloc_children(loc, 4); - rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(loc, INTERN("new_name"), module_name_range); - rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); - rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); + rbs_loc_alloc_children(&state->allocator, loc, 4); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("new_name"), module_name_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("eq"), eq_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("old_name"), old_name_range); *module_decl = (rbs_node_t *) rbs_ast_declarations_modulealias_new(&state->allocator, loc, module_name, old_name, comment); } else { @@ -2694,9 +2694,9 @@ static bool parse_class_decl_super(parserstate *state, range *lt_range, rbs_ast_ super_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, super_range); - rbs_loc_alloc_children(loc, 2); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_optional_child(loc, INTERN("args"), args_range); + rbs_loc_alloc_children(&state->allocator, loc, 2); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("args"), args_range); *super = rbs_ast_declarations_class_super_new(&state->allocator, loc, name, args); @@ -2737,12 +2737,12 @@ static bool parse_class_decl0(parserstate *state, range keyword_range, rbs_typen CHECK_PARSE(parser_pop_typevar_table(state)); rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); - rbs_loc_alloc_children(loc, 5); - rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(loc, INTERN("name"), name_range); - rbs_loc_add_required_child(loc, INTERN("end"), end_range); - rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range); - rbs_loc_add_optional_child(loc, INTERN("lt"), lt_range); + rbs_loc_alloc_children(&state->allocator, loc, 5); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("end"), end_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("type_params"), type_params_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("lt"), lt_range); *class_decl = rbs_ast_declarations_class_new(&state->allocator, loc, name, type_params, super, members, annotations, comment); return true; @@ -2779,11 +2779,11 @@ static bool parse_class_decl(parserstate *state, position comment_pos, rbs_node_ }; rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); - rbs_loc_alloc_children(loc, 4); - rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(loc, INTERN("new_name"), class_name_range); - rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); - rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); + rbs_loc_alloc_children(&state->allocator, loc, 4); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("new_name"), class_name_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("eq"), eq_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("old_name"), old_name_range); *class_decl = (rbs_node_t *) rbs_ast_declarations_classalias_new(&state->allocator, loc, class_name, old_name, comment); } else { @@ -2996,10 +2996,10 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { } rbs_location_t *loc = rbs_location_new(&state->allocator, clause_range); - rbs_loc_alloc_children(loc, 3); - rbs_loc_add_required_child(loc, INTERN("type_name"), type_name_range); - rbs_loc_add_optional_child(loc, INTERN("keyword"), keyword_range); - rbs_loc_add_optional_child(loc, INTERN("new_name"), new_name_range); + rbs_loc_alloc_children(&state->allocator, loc, 3); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("type_name"), type_name_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("keyword"), keyword_range); + rbs_loc_add_optional_child(&state->allocator, loc, INTERN("new_name"), new_name_range); rbs_ast_directives_use_singleclause_t *clause = rbs_ast_directives_use_singleclause_new(&state->allocator, loc, type_name, new_name); rbs_node_list_append(clauses, (rbs_node_t *)clause); @@ -3015,9 +3015,9 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { clause_range.end = star_range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, clause_range); - rbs_loc_alloc_children(loc, 2); - rbs_loc_add_required_child(loc, INTERN("namespace"), namespace_range); - rbs_loc_add_required_child(loc, INTERN("star"), star_range); + rbs_loc_alloc_children(&state->allocator, loc, 2); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("namespace"), namespace_range); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("star"), star_range); rbs_ast_directives_use_wildcardclause_t *clause = rbs_ast_directives_use_wildcardclause_new(&state->allocator, loc, namespace); rbs_node_list_append(clauses, (rbs_node_t *)clause); @@ -3056,8 +3056,8 @@ static bool parse_use_directive(parserstate *state, rbs_ast_directives_use_t **u directive_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, directive_range); - rbs_loc_alloc_children(loc, 1); - rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); + rbs_loc_alloc_children(&state->allocator, loc, 1); + rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); *use_directive = rbs_ast_directives_use_new(&state->allocator, loc, clauses); } diff --git a/src/rbs_location.c b/src/rbs_location.c index d9eff195a..ab99e9b7f 100644 --- a/src/rbs_location.c +++ b/src/rbs_location.c @@ -4,55 +4,43 @@ #define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1)) -static void check_children_max(unsigned short n) { +void rbs_loc_alloc_children(rbs_allocator_t *allocator, rbs_location_t *loc, int capacity) { size_t max = sizeof(rbs_loc_entry_bitmap) * 8; - if (n > max) { - fprintf(stderr, "Too many children added to location: %d", n); - exit(EXIT_FAILURE); - } -} - -static void check_children_cap(rbs_location_t *loc) { - if (loc->children == NULL) { - rbs_loc_alloc_children(loc, 1); - } else { - if (loc->children->len == loc->children->cap) { - check_children_max(loc->children->cap + 1); - size_t s = RBS_LOC_CHILDREN_SIZE(++loc->children->cap); - loc->children = realloc(loc->children, s); - } - } -} - -static rbs_loc_range rbs_new_loc_range(range rg) { - rbs_loc_range r = { rg.start.char_pos, rg.end.char_pos }; - return r; -} + assert(capacity <= max && "Capacity is too large"); -void rbs_loc_alloc_children(rbs_location_t *loc, int capacity) { - check_children_max(capacity); - - size_t s = RBS_LOC_CHILDREN_SIZE(capacity); - loc->children = malloc(s); + loc->children = rbs_allocator_malloc_impl(allocator, RBS_LOC_CHILDREN_SIZE(capacity), alignof(rbs_loc_children)); loc->children->len = 0; loc->children->required_p = 0; loc->children->cap = capacity; } -void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, range r) { - rbs_loc_add_optional_child(loc, name, r); +void rbs_loc_add_optional_child(rbs_allocator_t *allocator, rbs_location_t *loc, rbs_constant_id_t name, range r) { + // If the location has no children, allocate memory for one child. + if (loc->children == NULL) { + rbs_loc_alloc_children(allocator, loc, 1); + } else if (loc->children->len == loc->children->cap) { + // If the location has no more capacity, double the capacity. + size_t old_size = RBS_LOC_CHILDREN_SIZE(loc->children->cap); + size_t new_size = old_size * 2; + + // Reallocate the children array to the new size. + rbs_loc_children *new_children = rbs_allocator_realloc(allocator, loc->children, old_size, new_size, rbs_loc_children); + new_children->cap = new_size; + loc->children = new_children; + } - unsigned short last_index = loc->children->len - 1; - loc->children->required_p |= 1 << last_index; + // Add a new child to the location. + unsigned short i = loc->children->len++; + loc->children->entries[i].name = name; + loc->children->entries[i].rg = (rbs_loc_range) { r.start.char_pos, r.end.char_pos }; } -void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r) { - check_children_cap(loc); +void rbs_loc_add_required_child(rbs_allocator_t *allocator, rbs_location_t *loc, rbs_constant_id_t name, range r) { + rbs_loc_add_optional_child(allocator, loc, name, r); - unsigned short i = loc->children->len++; - loc->children->entries[i].name = name; - loc->children->entries[i].rg = rbs_new_loc_range(r); + unsigned short last_index = loc->children->len - 1; + loc->children->required_p |= 1 << last_index; } rbs_location_t *rbs_location_new(rbs_allocator_t *allocator, range rg) { diff --git a/src/util/rbs_allocator.c b/src/util/rbs_allocator.c index 045ed34ad..c61c9ff8e 100644 --- a/src/util/rbs_allocator.c +++ b/src/util/rbs_allocator.c @@ -127,6 +127,15 @@ void rbs_allocator_free(rbs_allocator_t *allocator) { } } +// Allocates `new_size` bytes from `allocator`, aligned to an `alignment`-byte boundary. +// Copies `old_size` bytes from `ptr` to the new allocation. +// It always reallocates the memory in new space and thus wastes the old space. +void *rbs_allocator_realloc_impl(rbs_allocator_t *allocator, void *ptr, size_t old_size, size_t new_size, size_t alignment) { + void *p = rbs_allocator_malloc_impl(allocator, new_size, alignment); + memcpy(p, ptr, old_size); + return p; +} + // Allocates `size` bytes from `allocator`, aligned to an `alignment`-byte boundary. void *rbs_allocator_malloc_impl(rbs_allocator_t *allocator, size_t size, size_t alignment) { assert(size % alignment == 0 && "size must be a multiple of the alignment"); From 10cd0750862588148e64966c3be3f3f20294dc45 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 12 Mar 2025 04:00:12 +0800 Subject: [PATCH 082/111] Remove unused rbs_constant_id_list and related functions (#12) --- include/rbs/util/rbs_constant_pool.h | 64 ------------------------- src/util/rbs_constant_pool.c | 71 ---------------------------- 2 files changed, 135 deletions(-) diff --git a/include/rbs/util/rbs_constant_pool.h b/include/rbs/util/rbs_constant_pool.h index 4be1b7ce1..7d02f2b21 100644 --- a/include/rbs/util/rbs_constant_pool.h +++ b/include/rbs/util/rbs_constant_pool.h @@ -30,70 +30,6 @@ */ typedef uint32_t rbs_constant_id_t; -/** - * A list of constant IDs. Usually used to represent a set of locals. - */ -typedef struct { - /** The number of constant ids in the list. */ - size_t size; - - /** The number of constant ids that have been allocated in the list. */ - size_t capacity; - - /** The constant ids in the list. */ - rbs_constant_id_t *ids; -} rbs_constant_id_list_t; - -/** - * Initialize a list of constant ids. - * - * @param list The list to initialize. - */ -void rbs_constant_id_list_init(rbs_constant_id_list_t *list); - -/** - * Initialize a list of constant ids with a given capacity. - * - * @param list The list to initialize. - * @param capacity The initial capacity of the list. - */ -void rbs_constant_id_list_init_capacity(rbs_constant_id_list_t *list, size_t capacity); - -/** - * Append a constant id to a list of constant ids. Returns false if any - * potential reallocations fail. - * - * @param list The list to append to. - * @param id The id to append. - * @return Whether the append succeeded. - */ -bool rbs_constant_id_list_append(rbs_constant_id_list_t *list, rbs_constant_id_t id); - -/** - * Insert a constant id into a list of constant ids at the specified index. - * - * @param list The list to insert into. - * @param index The index at which to insert. - * @param id The id to insert. - */ -void rbs_constant_id_list_insert(rbs_constant_id_list_t *list, size_t index, rbs_constant_id_t id); - -/** - * Checks if the current constant id list includes the given constant id. - * - * @param list The list to check. - * @param id The id to check for. - * @return Whether the list includes the given id. - */ -bool rbs_constant_id_list_includes(rbs_constant_id_list_t *list, rbs_constant_id_t id); - -/** - * Free the memory associated with a list of constant ids. - * - * @param list The list to free. - */ -void rbs_constant_id_list_free(rbs_constant_id_list_t *list); - /** * The type of bucket in the constant pool hash map. This determines how the * bucket should be freed. diff --git a/src/util/rbs_constant_pool.c b/src/util/rbs_constant_pool.c index d260e841a..44776c98d 100644 --- a/src/util/rbs_constant_pool.c +++ b/src/util/rbs_constant_pool.c @@ -1,76 +1,5 @@ #include "rbs/util/rbs_constant_pool.h" -/** - * Initialize a list of constant ids. - */ -void -rbs_constant_id_list_init(rbs_constant_id_list_t *list) { - list->ids = NULL; - list->size = 0; - list->capacity = 0; -} - -/** - * Initialize a list of constant ids with a given capacity. - */ -void -rbs_constant_id_list_init_capacity(rbs_constant_id_list_t *list, size_t capacity) { - list->ids = calloc(capacity, sizeof(rbs_constant_id_t)); - if (list->ids == NULL) abort(); - - list->size = 0; - list->capacity = capacity; -} - -/** - * Append a constant id to a list of constant ids. Returns false if any - * potential reallocations fail. - */ -bool -rbs_constant_id_list_append(rbs_constant_id_list_t *list, rbs_constant_id_t id) { - if (list->size >= list->capacity) { - list->capacity = list->capacity == 0 ? 8 : list->capacity * 2; - list->ids = (rbs_constant_id_t *) realloc(list->ids, sizeof(rbs_constant_id_t) * list->capacity); - if (list->ids == NULL) return false; - } - - list->ids[list->size++] = id; - return true; -} - -/** - * Insert a constant id into a list of constant ids at the specified index. - */ -void -rbs_constant_id_list_insert(rbs_constant_id_list_t *list, size_t index, rbs_constant_id_t id) { - assert(index < list->capacity); - assert(list->ids[index] == RBS_CONSTANT_ID_UNSET); - - list->ids[index] = id; - list->size++; -} - -/** - * Checks if the current constant id list includes the given constant id. - */ -bool -rbs_constant_id_list_includes(rbs_constant_id_list_t *list, rbs_constant_id_t id) { - for (size_t index = 0; index < list->size; index++) { - if (list->ids[index] == id) return true; - } - return false; -} - -/** - * Free the memory associated with a list of constant ids. - */ -void -rbs_constant_id_list_free(rbs_constant_id_list_t *list) { - if (list->ids != NULL) { - free(list->ids); - } -} - /** * A relatively simple hash function (djb2) that is used to hash strings. We are * optimizing here for simplicity and speed. From 0fb84942d3bcf9e1dc3baa4f8fbbe249c6a85943 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 12 Mar 2025 04:00:23 +0800 Subject: [PATCH 083/111] Remove unnecessary re-allocations from child-adding functions (#11) * Remove unnecessary re-allocations from child-adding functions We always pre-allocate enough space for a location's children with `rbs_loc_alloc_children` in `parser.c`. So we should never hit the case where we need to re-allocate the children array. This change removes the unnecessary re-allocations and makes the code simpler. * Update src/rbs_location.c Co-authored-by: Alexander Momchilov --------- Co-authored-by: Alexander Momchilov --- include/rbs/rbs_location.h | 4 +- src/parser.c | 164 ++++++++++++++++++------------------- src/rbs_location.c | 25 ++---- 3 files changed, 90 insertions(+), 103 deletions(-) diff --git a/include/rbs/rbs_location.h b/include/rbs/rbs_location.h index c4a78f96e..c00b40111 100644 --- a/include/rbs/rbs_location.h +++ b/include/rbs/rbs_location.h @@ -13,8 +13,8 @@ typedef struct rbs_location { } rbs_location_t; void rbs_loc_alloc_children(rbs_allocator_t *, rbs_location_t *loc, int size); -void rbs_loc_add_required_child(rbs_allocator_t *, rbs_location_t *loc, rbs_constant_id_t name, range r); -void rbs_loc_add_optional_child(rbs_allocator_t *, rbs_location_t *loc, rbs_constant_id_t name, range r); +void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, range r); +void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r); /** * Allocate new rbs_location_t object through the given allocator. diff --git a/src/parser.c b/src/parser.c index d838fe2cc..e426a9a43 100644 --- a/src/parser.c +++ b/src/parser.c @@ -307,7 +307,7 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t rbs_location_t *loc = rbs_location_new(&state->allocator, param_range); rbs_loc_alloc_children(&state->allocator, loc, 1); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("name"), NULL_RANGE); + rbs_loc_add_optional_child(loc, INTERN("name"), NULL_RANGE); *function_param = rbs_types_function_param_new(&state->allocator, loc, type, NULL); return true; @@ -333,7 +333,7 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t rbs_location_t *loc = rbs_location_new(&state->allocator, param_range); rbs_loc_alloc_children(&state->allocator, loc, 1); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_optional_child(loc, INTERN("name"), name_range); *function_param = rbs_types_function_param_new(&state->allocator, loc, type, name); return true; @@ -993,8 +993,8 @@ static bool parse_instance_type(parserstate *state, bool parse_alias, rbs_node_t rbs_location_t *loc = rbs_location_new(&state->allocator, type_range); rbs_loc_alloc_children(&state->allocator, loc, 2); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("args"), args_range); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_optional_child(loc, INTERN("args"), args_range); if (kind == CLASS_NAME) { *type = (rbs_node_t *) rbs_types_classinstance_new(&state->allocator, loc, typename, types); @@ -1028,7 +1028,7 @@ static bool parse_singleton_type(parserstate *state, rbs_types_classsingleton_t rbs_location_t *loc = rbs_location_new(&state->allocator, type_range); rbs_loc_alloc_children(&state->allocator, loc, 1); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); *singleton = rbs_types_classsingleton_new(&state->allocator, loc, typename); return true; @@ -1373,11 +1373,11 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa rbs_location_t *loc = rbs_location_new(&state->allocator, param_range); rbs_loc_alloc_children(&state->allocator, loc, 5); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("variance"), variance_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("unchecked"), unchecked_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("upper_bound"), upper_bound_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("default"), default_type_range); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_optional_child(loc, INTERN("variance"), variance_range); + rbs_loc_add_optional_child(loc, INTERN("unchecked"), unchecked_range); + rbs_loc_add_optional_child(loc, INTERN("upper_bound"), upper_bound_range); + rbs_loc_add_optional_child(loc, INTERN("default"), default_type_range); rbs_ast_typeparam_t *param = rbs_ast_typeparam_new(&state->allocator, loc, name, variance, upper_bound, default_type, unchecked); @@ -1428,8 +1428,8 @@ bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type) { rbs_location_t *loc = rbs_location_new(&state->allocator, rg); rbs_loc_alloc_children(&state->allocator, loc, 2); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("type"), type_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("type_params"), params_range); + rbs_loc_add_required_child(loc, INTERN("type"), type_range); + rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range); *method_type = rbs_methodtype_new(&state->allocator, loc, type_params, result->function, result->block); return true; @@ -1459,8 +1459,8 @@ static bool parse_global_decl(parserstate *state, rbs_ast_declarations_global_t rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); rbs_loc_alloc_children(&state->allocator, loc, 2); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("colon"), colon_range); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); *global = rbs_ast_declarations_global_new(&state->allocator, loc, typename, type, comment); return true; @@ -1490,8 +1490,8 @@ static bool parse_const_decl(parserstate *state, rbs_ast_declarations_constant_t rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); rbs_loc_alloc_children(&state->allocator, loc, 2); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("colon"), colon_range); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); *constant = rbs_ast_declarations_constant_new(&state->allocator, loc, typename, type, comment); return true; @@ -1530,10 +1530,10 @@ static bool parse_type_decl(parserstate *state, position comment_pos, rbs_node_l rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); rbs_loc_alloc_children(&state->allocator, loc, 4); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("type_params"), params_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("eq"), eq_range); + rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range); + rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); CHECK_PARSE(parser_pop_typevar_table(state)); @@ -1898,11 +1898,11 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(&state->allocator, loc, 5); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("kind"), kind_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("overloading"), overloading_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("visibility"), visibility_range); + rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range); + rbs_loc_add_optional_child(loc, INTERN("overloading"), overloading_range); + rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range); *method_definition = rbs_ast_members_methoddefinition_new(&state->allocator, loc, name, k, overloads, annotations, comment, overloading, visibility); return true; @@ -1993,9 +1993,9 @@ static bool parse_mixin_member(parserstate *state, bool from_interface, position rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(&state->allocator, loc, 3); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("args"), args_range); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); + rbs_loc_add_optional_child(loc, INTERN("args"), args_range); rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); switch (type) @@ -2061,11 +2061,11 @@ static bool parse_alias_member(parserstate *state, bool instance_only, position member_range.end = state->current_token.range.end; rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(&state->allocator, loc, 5); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("new_name"), new_name_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("old_name"), old_name_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("new_kind"), new_kind_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("old_kind"), old_kind_range); + rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(loc, INTERN("new_name"), new_name_range); + rbs_loc_add_required_child(loc, INTERN("old_name"), old_name_range); + rbs_loc_add_optional_child(loc, INTERN("new_kind"), new_kind_range); + rbs_loc_add_optional_child(loc, INTERN("old_kind"), old_kind_range); *alias_member = rbs_ast_members_alias_new(&state->allocator, loc, new_name, old_name, kind, annotations, comment); return true; @@ -2104,9 +2104,9 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(&state->allocator, loc, 3); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("colon"), colon_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("kind"), NULL_RANGE); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); + rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); *variable_member = (rbs_node_t *)rbs_ast_members_instancevariable_new(&state->allocator, loc, name, type, comment); return true; @@ -2130,9 +2130,9 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(&state->allocator, loc, 3); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("colon"), colon_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("kind"), NULL_RANGE); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); + rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); *variable_member = (rbs_node_t *) rbs_ast_members_classvariable_new(&state->allocator, loc, name, type, comment); return true; @@ -2164,9 +2164,9 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(&state->allocator, loc, 3); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("colon"), colon_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("kind"), kind_range); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); + rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range); *variable_member = (rbs_node_t *)rbs_ast_members_classinstancevariable_new(&state->allocator, loc, name, type, comment); return true; @@ -2307,13 +2307,13 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(&state->allocator, loc, 7); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("colon"), colon_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("kind"), kind_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("ivar"), ivar_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("ivar_name"), ivar_name_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("visibility"), visibility_range); + rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); + rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range); + rbs_loc_add_optional_child(loc, INTERN("ivar"), ivar_range); + rbs_loc_add_optional_child(loc, INTERN("ivar_name"), ivar_name_range); + rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range); switch (attr_type) { @@ -2418,10 +2418,10 @@ static bool parse_interface_decl(parserstate *state, position comment_pos, rbs_n rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); rbs_loc_alloc_children(&state->allocator, loc, 4); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("end"), end_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("type_params"), type_params_range); + rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_required_child(loc, INTERN("end"), end_range); + rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range); rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); @@ -2460,8 +2460,8 @@ static bool parse_module_self_types(parserstate *state, rbs_node_list_t *array) rbs_location_t *loc = rbs_location_new(&state->allocator, self_range); rbs_loc_alloc_children(&state->allocator, loc, 2); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("args"), args_range); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_optional_child(loc, INTERN("args"), args_range); rbs_ast_declarations_module_self_t *self_type = rbs_ast_declarations_module_self_new(&state->allocator, loc, module_name, args); rbs_node_list_append(array, (rbs_node_t *)self_type); @@ -2612,12 +2612,12 @@ static bool parse_module_decl0(parserstate *state, range keyword_range, rbs_type rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); rbs_loc_alloc_children(&state->allocator, loc, 6); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("end"), end_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("type_params"), type_params_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("colon"), colon_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("self_types"), self_types_range); + rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_required_child(loc, INTERN("end"), end_range); + rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range); + rbs_loc_add_optional_child(loc, INTERN("colon"), colon_range); + rbs_loc_add_optional_child(loc, INTERN("self_types"), self_types_range); CHECK_PARSE(parser_pop_typevar_table(state)); @@ -2659,10 +2659,10 @@ static bool parse_module_decl(parserstate *state, position comment_pos, rbs_node rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); rbs_loc_alloc_children(&state->allocator, loc, 4); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("new_name"), module_name_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("eq"), eq_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("old_name"), old_name_range); + rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(loc, INTERN("new_name"), module_name_range); + rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); + rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); *module_decl = (rbs_node_t *) rbs_ast_declarations_modulealias_new(&state->allocator, loc, module_name, old_name, comment); } else { @@ -2695,8 +2695,8 @@ static bool parse_class_decl_super(parserstate *state, range *lt_range, rbs_ast_ rbs_location_t *loc = rbs_location_new(&state->allocator, super_range); rbs_loc_alloc_children(&state->allocator, loc, 2); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("args"), args_range); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_optional_child(loc, INTERN("args"), args_range); *super = rbs_ast_declarations_class_super_new(&state->allocator, loc, name, args); @@ -2738,11 +2738,11 @@ static bool parse_class_decl0(parserstate *state, range keyword_range, rbs_typen rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); rbs_loc_alloc_children(&state->allocator, loc, 5); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("name"), name_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("end"), end_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("type_params"), type_params_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("lt"), lt_range); + rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(loc, INTERN("name"), name_range); + rbs_loc_add_required_child(loc, INTERN("end"), end_range); + rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range); + rbs_loc_add_optional_child(loc, INTERN("lt"), lt_range); *class_decl = rbs_ast_declarations_class_new(&state->allocator, loc, name, type_params, super, members, annotations, comment); return true; @@ -2780,10 +2780,10 @@ static bool parse_class_decl(parserstate *state, position comment_pos, rbs_node_ rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); rbs_loc_alloc_children(&state->allocator, loc, 4); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("new_name"), class_name_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("eq"), eq_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("old_name"), old_name_range); + rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(loc, INTERN("new_name"), class_name_range); + rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); + rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); *class_decl = (rbs_node_t *) rbs_ast_declarations_classalias_new(&state->allocator, loc, class_name, old_name, comment); } else { @@ -2997,9 +2997,9 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { rbs_location_t *loc = rbs_location_new(&state->allocator, clause_range); rbs_loc_alloc_children(&state->allocator, loc, 3); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("type_name"), type_name_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("keyword"), keyword_range); - rbs_loc_add_optional_child(&state->allocator, loc, INTERN("new_name"), new_name_range); + rbs_loc_add_required_child(loc, INTERN("type_name"), type_name_range); + rbs_loc_add_optional_child(loc, INTERN("keyword"), keyword_range); + rbs_loc_add_optional_child(loc, INTERN("new_name"), new_name_range); rbs_ast_directives_use_singleclause_t *clause = rbs_ast_directives_use_singleclause_new(&state->allocator, loc, type_name, new_name); rbs_node_list_append(clauses, (rbs_node_t *)clause); @@ -3016,8 +3016,8 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { rbs_location_t *loc = rbs_location_new(&state->allocator, clause_range); rbs_loc_alloc_children(&state->allocator, loc, 2); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("namespace"), namespace_range); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("star"), star_range); + rbs_loc_add_required_child(loc, INTERN("namespace"), namespace_range); + rbs_loc_add_required_child(loc, INTERN("star"), star_range); rbs_ast_directives_use_wildcardclause_t *clause = rbs_ast_directives_use_wildcardclause_new(&state->allocator, loc, namespace); rbs_node_list_append(clauses, (rbs_node_t *)clause); @@ -3057,7 +3057,7 @@ static bool parse_use_directive(parserstate *state, rbs_ast_directives_use_t **u rbs_location_t *loc = rbs_location_new(&state->allocator, directive_range); rbs_loc_alloc_children(&state->allocator, loc, 1); - rbs_loc_add_required_child(&state->allocator, loc, INTERN("keyword"), keyword_range); + rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); *use_directive = rbs_ast_directives_use_new(&state->allocator, loc, clauses); } diff --git a/src/rbs_location.c b/src/rbs_location.c index ab99e9b7f..5f9d3225b 100644 --- a/src/rbs_location.c +++ b/src/rbs_location.c @@ -15,30 +15,17 @@ void rbs_loc_alloc_children(rbs_allocator_t *allocator, rbs_location_t *loc, int loc->children->cap = capacity; } -void rbs_loc_add_optional_child(rbs_allocator_t *allocator, rbs_location_t *loc, rbs_constant_id_t name, range r) { - // If the location has no children, allocate memory for one child. - if (loc->children == NULL) { - rbs_loc_alloc_children(allocator, loc, 1); - } else if (loc->children->len == loc->children->cap) { - // If the location has no more capacity, double the capacity. - size_t old_size = RBS_LOC_CHILDREN_SIZE(loc->children->cap); - size_t new_size = old_size * 2; - - // Reallocate the children array to the new size. - rbs_loc_children *new_children = rbs_allocator_realloc(allocator, loc->children, old_size, new_size, rbs_loc_children); - new_children->cap = new_size; - loc->children = new_children; - } - - // Add a new child to the location. +void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r) { + assert(loc->children != NULL && "All children should have been pre-allocated with rbs_loc_alloc_children()"); + assert((loc->children->len + 1 <= loc->children->cap) && "Not enough space was pre-allocated for the children."); + unsigned short i = loc->children->len++; loc->children->entries[i].name = name; loc->children->entries[i].rg = (rbs_loc_range) { r.start.char_pos, r.end.char_pos }; } -void rbs_loc_add_required_child(rbs_allocator_t *allocator, rbs_location_t *loc, rbs_constant_id_t name, range r) { - rbs_loc_add_optional_child(allocator, loc, name, r); - +void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, range r) { + rbs_loc_add_optional_child(loc, name, r); unsigned short last_index = loc->children->len - 1; loc->children->required_p |= 1 << last_index; } From 63c0730400651e53e3e162b6068e89d9c17107f2 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 12 Mar 2025 21:26:22 +0800 Subject: [PATCH 084/111] Allocate parser error and result with allocator (#15) * Allocate parser error with allocator * Allocate parse result with allocator --- src/parser.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/parser.c b/src/parser.c index e426a9a43..810acfd76 100644 --- a/src/parser.c +++ b/src/parser.c @@ -131,13 +131,13 @@ void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt int length = vsnprintf(NULL, 0, fmt, args); va_end(args); - char *message = (char *) malloc(length + 1); + char *message = rbs_allocator_calloc(&state->allocator, length + 1, char); va_start(args, fmt); vsnprintf(message, length + 1, fmt, args); va_end(args); - state->error = (error *)malloc(sizeof(error)); + state->error = rbs_allocator_alloc(&state->allocator, error); state->error->token = tok; state->error->message = message; state->error->syntax_error = syntax_error; @@ -783,13 +783,6 @@ static bool parse_function(parserstate *state, bool accept_type_binding, parse_f ); } - *result = malloc(sizeof(parse_function_result)); - // *result = (parse_function_result) { - // .function = function, - // .block = block, - // .function_self_type = function_self_type, - // }; - (*result)->function = function; (*result)->block = block; (*result)->function_self_type = function_self_type; @@ -802,7 +795,7 @@ static bool parse_function(parserstate *state, bool accept_type_binding, parse_f NODISCARD static bool parse_proc_type(parserstate *state, rbs_types_proc_t **proc) { position start = state->current_token.range.start; - parse_function_result *result = NULL; + parse_function_result *result = rbs_allocator_alloc(&state->allocator, parse_function_result); CHECK_PARSE(parse_function(state, true, &result)); position end = state->current_token.range.end; @@ -1418,7 +1411,7 @@ bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type) { range type_range; type_range.start = state->next_token.range.start; - parse_function_result *result; + parse_function_result *result = rbs_allocator_alloc(&state->allocator, parse_function_result); CHECK_PARSE(parse_function(state, false, &result)); rg.end = state->current_token.range.end; From 01cdcb2866040c95ba0a53d89e4ba3b12db81850 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Tue, 11 Mar 2025 21:26:06 +0000 Subject: [PATCH 085/111] Allocate owned rbs_string_t using allocator --- include/rbs/rbs_string.h | 27 ++++++--------------------- include/rbs/rbs_unescape.h | 6 ++++-- src/ast.c | 8 ++++---- src/parser.c | 18 ++++++++---------- src/rbs_string.c | 23 ++++------------------- src/rbs_unescape.c | 11 +++++------ templates/src/ast.c.erb | 2 +- 7 files changed, 32 insertions(+), 63 deletions(-) diff --git a/include/rbs/rbs_string.h b/include/rbs/rbs_string.h index 3f5bf1882..829d7f82b 100644 --- a/include/rbs/rbs_string.h +++ b/include/rbs/rbs_string.h @@ -3,6 +3,7 @@ #include #include +#include "rbs/util/rbs_allocator.h" typedef struct { const char *start; @@ -13,7 +14,7 @@ typedef struct { RBS_STRING_CONSTANT, /** This is a slice of another string, and should not be freed. */ RBS_STRING_SHARED, - /** This string owns its memory, and should be freed using `rbs_string_free`. */ + /** This string owns its memory, and will be freed when the allocator is freed. */ RBS_STRING_OWNED, } type; } rbs_string_t; @@ -38,31 +39,15 @@ rbs_string_t rbs_string_owned_new(const char *start, const char *end); * Copies a portion of the input string into a new owned string. * @param start_inset Number of characters to exclude from the start * @param length Number of characters to include - * @return A new owned string that needs to be freed using `rbs_string_free()`. + * @return A new owned string that will be freed when the allocator is freed. */ -rbs_string_t rbs_string_copy_slice(rbs_string_t *self, size_t start_inset, size_t length); - -/** - * Free the associated memory of the given string if it is owned, otherwise does nothing. - * - * @param string The string to free. - * \public \memberof rbs_string_t - */ -void rbs_string_free_if_needed(rbs_string_t *self); - -/** - * Free the associated memory of the given string if it is owned, otherwise fails (exits the program). - * - * @param string The string to free. - * \public \memberof rbs_string_t - */ -void rbs_string_free(rbs_string_t *self); +rbs_string_t rbs_string_copy_slice(rbs_allocator_t *, rbs_string_t *self, size_t start_inset, size_t length); /** * Drops the leading and trailing whitespace from the given string, in-place. - * @returns A new owned string that needs to be freed with `rbs_string_free()` + * @returns A new owned string that will be freed when the allocator is freed. */ -rbs_string_t rbs_string_strip_whitespace(rbs_string_t *self); +rbs_string_t rbs_string_strip_whitespace(rbs_allocator_t *, rbs_string_t *self); /** * Returns the length of the string. diff --git a/include/rbs/rbs_unescape.h b/include/rbs/rbs_unescape.h index 22f3938fa..0b4182acc 100644 --- a/include/rbs/rbs_unescape.h +++ b/include/rbs/rbs_unescape.h @@ -2,6 +2,8 @@ #define RBS_RBS_UNESCAPE_H #include +#include "rbs/util/rbs_allocator.h" +#include "rbs/rbs_string.h" /** * Receives `parserstate` and `range`, which represents a string token or symbol token, and returns a string VALUE. @@ -14,8 +16,8 @@ * :"baz\\t" | baz\t * :'baz' | baz * - * @returns A new owned string that needs to be freed with `rbs_string_free()` + * @returns A new owned string that will be freed when the allocator is freed. * */ -rbs_string_t rbs_unquote_string(rbs_string_t input); +rbs_string_t rbs_unquote_string(rbs_allocator_t *, rbs_string_t input); #endif // RBS_RBS_UNESCAPE_H diff --git a/src/ast.c b/src/ast.c index 62a3c11a7..d08b94695 100644 --- a/src/ast.c +++ b/src/ast.c @@ -1279,7 +1279,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { case RBS_AST_ANNOTATION: { rbs_ast_annotation_t *node = (rbs_ast_annotation_t *)any_node; - rbs_string_free_if_needed(&node->string); + // string is a string, so it will be freed when the allocator is freed. break; } #line 202 "prism/templates/src/ast.c.erb" @@ -1290,7 +1290,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { case RBS_AST_COMMENT: { rbs_ast_comment_t *node = (rbs_ast_comment_t *)any_node; - rbs_string_free_if_needed(&node->string); + // string is a string, so it will be freed when the allocator is freed. break; } #line 202 "prism/templates/src/ast.c.erb" @@ -1471,7 +1471,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { case RBS_AST_INTEGER: { rbs_ast_integer_t *node = (rbs_ast_integer_t *)any_node; - rbs_string_free_if_needed(&node->string_representation); + // string_representation is a string, so it will be freed when the allocator is freed. break; } #line 202 "prism/templates/src/ast.c.erb" @@ -1698,7 +1698,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { case RBS_AST_STRING: { rbs_ast_string_t *node = (rbs_ast_string_t *)any_node; - rbs_string_free_if_needed(&node->string); + // string is a string, so it will be freed when the allocator is freed. break; } #line 202 "prism/templates/src/ast.c.erb" diff --git a/src/parser.c b/src/parser.c index 810acfd76..4aa50a983 100644 --- a/src/parser.c +++ b/src/parser.c @@ -326,7 +326,7 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t return false; } - rbs_string_t unquoted_str = rbs_unquote_string(rbs_parser_peek_current_token(state)); + rbs_string_t unquoted_str = rbs_unquote_string(&state->allocator, rbs_parser_peek_current_token(state)); rbs_location_t *symbolLoc = rbs_location_current_token(state); rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); @@ -917,13 +917,11 @@ static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types rbs_location_t *symbolLoc = rbs_location_current_token(state); rbs_string_t current_token = rbs_parser_peek_current_token(state); - rbs_string_t symbol = rbs_string_copy_slice(¤t_token, offset_bytes, rbs_string_len(current_token) - offset_bytes); + rbs_string_t symbol = rbs_string_copy_slice(&state->allocator, ¤t_token, offset_bytes, rbs_string_len(current_token) - offset_bytes); - rbs_string_t unquoted_symbol = rbs_unquote_string(symbol); - rbs_string_free(&symbol); + rbs_string_t unquoted_symbol = rbs_unquote_string(&state->allocator, symbol); rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&state->constant_pool, unquoted_symbol); - // rbs_string_free(&unquoted_symbol); literal = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); break; @@ -1103,7 +1101,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { rbs_location_t *loc = rbs_location_current_token(state); rbs_string_t string = rbs_parser_peek_current_token(state); - rbs_string_t stripped_string = rbs_string_strip_whitespace(&string); + rbs_string_t stripped_string = rbs_string_strip_whitespace(&state->allocator, &string); rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(&state->allocator, loc, stripped_string); *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, loc, literal); @@ -1123,7 +1121,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { case tDQSTRING: { rbs_location_t *loc = rbs_location_current_token(state); - rbs_string_t unquoted_str = rbs_unquote_string(rbs_parser_peek_current_token(state)); + rbs_string_t unquoted_str = rbs_unquote_string(&state->allocator, rbs_parser_peek_current_token(state)); rbs_node_t *literal = (rbs_node_t *) rbs_ast_string_new(&state->allocator, loc, unquoted_str); *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, loc, literal); return true; @@ -1584,13 +1582,13 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati size_t total_offset = offset_bytes + open_bytes; rbs_string_t annotation_str = rbs_string_copy_slice( + &state->allocator, ¤t_token, total_offset, rbs_string_len(current_token) - total_offset - close_bytes ); - rbs_string_t stripped_annotation_str = rbs_string_strip_whitespace(&annotation_str); - rbs_string_free(&annotation_str); + rbs_string_t stripped_annotation_str = rbs_string_strip_whitespace(&state->allocator, &annotation_str); *annotation = rbs_ast_annotation_new(&state->allocator, rbs_location_new(&state->allocator, rg), stripped_annotation_str); return true; @@ -1668,7 +1666,7 @@ static bool parse_method_name(parserstate *state, range *range, rbs_ast_symbol_t } case tQIDENT: { rbs_string_t string = rbs_parser_peek_current_token(state); - rbs_string_t unquoted_str = rbs_unquote_string(string); + rbs_string_t unquoted_str = rbs_unquote_string(&state->allocator, string); rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str); rbs_location_t *symbolLoc = rbs_location_current_token(state); *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); diff --git a/src/rbs_string.c b/src/rbs_string.c index 950da768e..b7ac5c4cc 100644 --- a/src/rbs_string.c +++ b/src/rbs_string.c @@ -21,30 +21,15 @@ rbs_string_t rbs_string_owned_new(const char *start, const char *end) { }; } -rbs_string_t rbs_string_copy_slice(rbs_string_t *self, size_t start_inset, size_t length) { - char *buffer = (char *) malloc(length + 1); +rbs_string_t rbs_string_copy_slice(rbs_allocator_t *allocator, rbs_string_t *self, size_t start_inset, size_t length) { + char *buffer = rbs_allocator_calloc(allocator, length + 1, char); strncpy(buffer, self->start + start_inset, length); buffer[length] = '\0'; return rbs_string_owned_new(buffer, buffer + length); } -void rbs_string_free_if_needed(rbs_string_t *self) { - if (self->type == RBS_STRING_OWNED) { - rbs_string_free(self); - } -} - -void rbs_string_free(rbs_string_t *self) { - if (self->type != RBS_STRING_OWNED) { - fprintf(stderr, "rbs_string_free(%p): not owned\n", self->start); - exit(EXIT_FAILURE); - } - - free((void *) self->start); -} - -rbs_string_t rbs_string_strip_whitespace(rbs_string_t *self) { +rbs_string_t rbs_string_strip_whitespace(rbs_allocator_t *allocator, rbs_string_t *self) { const char *new_start = self->start; while (isspace(*new_start) && new_start < self->end) { new_start++; @@ -59,7 +44,7 @@ rbs_string_t rbs_string_strip_whitespace(rbs_string_t *self) { new_end--; } - return rbs_string_copy_slice(self, new_start - self->start, new_end - new_start + 1); + return rbs_string_copy_slice(allocator, self, new_start - self->start, new_end - new_start + 1); } size_t rbs_string_len(const rbs_string_t self) { diff --git a/src/rbs_unescape.c b/src/rbs_unescape.c index de5125854..c1ae97f5c 100644 --- a/src/rbs_unescape.c +++ b/src/rbs_unescape.c @@ -1,6 +1,5 @@ #include "rbs/encoding.h" #include "rbs/rbs_unescape.h" -#include "rbs/rbs_string.h" #include #include #include @@ -44,13 +43,13 @@ static int octal_to_int(const char* octal, int length) { return result; } -rbs_string_t unescape_string(const rbs_string_t string, bool is_double_quote) { +rbs_string_t unescape_string(rbs_allocator_t *allocator, rbs_string_t string, bool is_double_quote) { if (!string.start) return RBS_STRING_NULL; size_t len = string.end - string.start; const char* input = string.start; - char* output = malloc(len + 1); + char* output = rbs_allocator_calloc(allocator, len + 1, char); if (!output) return RBS_STRING_NULL; size_t i = 0, j = 0; @@ -107,7 +106,7 @@ rbs_string_t unescape_string(const rbs_string_t string, bool is_double_quote) { return rbs_string_owned_new(output, output + j); } -rbs_string_t rbs_unquote_string(rbs_string_t input) { +rbs_string_t rbs_unquote_string(rbs_allocator_t *allocator, rbs_string_t input) { unsigned int first_char = utf8_to_codepoint(input); size_t byte_length = rbs_string_len(input); @@ -118,6 +117,6 @@ rbs_string_t rbs_unquote_string(rbs_string_t input) { byte_length -= 2 * bs; } - rbs_string_t string = rbs_string_copy_slice(&input, start_offset, byte_length); - return unescape_string(string, first_char == '"'); + rbs_string_t string = rbs_string_copy_slice(allocator, &input, start_offset, byte_length); + return unescape_string(allocator, string, first_char == '"'); } diff --git a/templates/src/ast.c.erb b/templates/src/ast.c.erb index 18154f838..74eb338b5 100644 --- a/templates/src/ast.c.erb +++ b/templates/src/ast.c.erb @@ -206,7 +206,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { <%- node.fields.each do |field| -%> <%- case field.c_type -%> <%- when "rbs_string" -%> - rbs_string_free_if_needed(&node-><%= field.c_name %>); + // <%= field.c_name %> is a string, so it will be freed when the allocator is freed. <%- when "rbs_node_list" -%> rbs_node_list_free(node-><%= field.c_name %>); <%- when "rbs_hash" -%> From fcb65f64003406799a955a2006cddc4a5a7584e7 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Tue, 11 Mar 2025 21:34:40 +0000 Subject: [PATCH 086/111] Remove string type enum Since we don't manually allocate/free strings anymore, we don't need the string type enum and all the complexity that comes with it. --- ext/rbs_extension/rbs_string_bridging.c | 2 +- include/rbs/rbs_string.h | 19 ++----------------- src/ast.c | 8 ++++---- src/lexstate.c | 9 ++++----- src/parser.c | 11 +++++------ src/parserstate.c | 9 ++++----- src/rbs_buffer.c | 2 +- src/rbs_string.c | 15 +++------------ src/rbs_unescape.c | 2 +- templates/src/ast.c.erb | 2 +- 10 files changed, 26 insertions(+), 53 deletions(-) diff --git a/ext/rbs_extension/rbs_string_bridging.c b/ext/rbs_extension/rbs_string_bridging.c index b7315091b..7770f7907 100644 --- a/ext/rbs_extension/rbs_string_bridging.c +++ b/ext/rbs_extension/rbs_string_bridging.c @@ -1,7 +1,7 @@ #include "rbs_string_bridging.h" rbs_string_t rbs_string_from_ruby_string(VALUE ruby_string) { - return rbs_string_shared_new(StringValueCStr(ruby_string), RSTRING_END(ruby_string)); + return rbs_string_new(StringValueCStr(ruby_string), RSTRING_END(ruby_string)); } VALUE rbs_string_to_ruby_string(rbs_string_t *self, rb_encoding *encoding) { diff --git a/include/rbs/rbs_string.h b/include/rbs/rbs_string.h index 829d7f82b..22669e64d 100644 --- a/include/rbs/rbs_string.h +++ b/include/rbs/rbs_string.h @@ -8,32 +8,17 @@ typedef struct { const char *start; const char *end; - - enum rbs_string_type { - /** This string is a constant string, and should not be freed. */ - RBS_STRING_CONSTANT, - /** This is a slice of another string, and should not be freed. */ - RBS_STRING_SHARED, - /** This string owns its memory, and will be freed when the allocator is freed. */ - RBS_STRING_OWNED, - } type; } rbs_string_t; #define RBS_STRING_NULL ((rbs_string_t) { \ .start = NULL, \ .end = NULL, \ - .type = RBS_STRING_CONSTANT, \ }) /** - * Returns a new `rbs_string_t` struct that points to the given C string without owning it. - */ -rbs_string_t rbs_string_shared_new(const char *start, const char *end); - -/** - * Returns a new `rbs_string_t` struct that owns its memory. + * Returns a new `rbs_string_t` struct */ -rbs_string_t rbs_string_owned_new(const char *start, const char *end); +rbs_string_t rbs_string_new(const char *start, const char *end); /** * Copies a portion of the input string into a new owned string. diff --git a/src/ast.c b/src/ast.c index d08b94695..3f7bd1c57 100644 --- a/src/ast.c +++ b/src/ast.c @@ -1279,7 +1279,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { case RBS_AST_ANNOTATION: { rbs_ast_annotation_t *node = (rbs_ast_annotation_t *)any_node; - // string is a string, so it will be freed when the allocator is freed. + // node->string is a `rbs_string_t` that will be freed by the arena allocator. break; } #line 202 "prism/templates/src/ast.c.erb" @@ -1290,7 +1290,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { case RBS_AST_COMMENT: { rbs_ast_comment_t *node = (rbs_ast_comment_t *)any_node; - // string is a string, so it will be freed when the allocator is freed. + // node->string is a `rbs_string_t` that will be freed by the arena allocator. break; } #line 202 "prism/templates/src/ast.c.erb" @@ -1471,7 +1471,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { case RBS_AST_INTEGER: { rbs_ast_integer_t *node = (rbs_ast_integer_t *)any_node; - // string_representation is a string, so it will be freed when the allocator is freed. + // node->string_representation is a `rbs_string_t` that will be freed by the arena allocator. break; } #line 202 "prism/templates/src/ast.c.erb" @@ -1698,7 +1698,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { case RBS_AST_STRING: { rbs_ast_string_t *node = (rbs_ast_string_t *)any_node; - // string is a string, so it will be freed when the allocator is freed. + // node->string is a `rbs_string_t` that will be freed by the arena allocator. break; } #line 202 "prism/templates/src/ast.c.erb" diff --git a/src/lexstate.c b/src/lexstate.c index 5e108076e..ef65c0e84 100644 --- a/src/lexstate.c +++ b/src/lexstate.c @@ -110,11 +110,10 @@ unsigned int peek(lexstate *state) { state->last_char = '\0'; return 0; } else { - rbs_string_t str = { - .start = state->string.start + state->current.byte_pos, - .end = state->string.end, - .type = RBS_STRING_SHARED, - }; + rbs_string_t str = rbs_string_new( + state->string.start + state->current.byte_pos, + state->string.end + ); unsigned int c = utf8_to_codepoint(str); state->last_char = c; return c; diff --git a/src/parser.c b/src/parser.c index 4aa50a983..a4642942f 100644 --- a/src/parser.c +++ b/src/parser.c @@ -113,7 +113,7 @@ static rbs_string_t rbs_parser_peek_current_token(parserstate *state) { const char *start = state->lexstate->string.start + rg.start.byte_pos; size_t length = rg.end.byte_pos - rg.start.byte_pos; - return rbs_string_shared_new(start, start + length); + return rbs_string_new(start, start + length); } static rbs_constant_id_t rbs_constant_pool_insert_string(rbs_constant_pool_t *self, rbs_string_t string) { @@ -1545,11 +1545,10 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati state->lexstate->encoding->char_width((const uint8_t *) "%", (size_t) 1) + state->lexstate->encoding->char_width((const uint8_t *) "a", (size_t) 1); - rbs_string_t str = { - .start = state->lexstate->string.start + rg.start.byte_pos + offset_bytes, - .end = state->lexstate->string.end, - .type = RBS_STRING_SHARED, - }; + rbs_string_t str = rbs_string_new( + state->lexstate->string.start + rg.start.byte_pos + offset_bytes, + state->lexstate->string.end + ); unsigned int open_char = utf8_to_codepoint(str); unsigned int close_char; diff --git a/src/parserstate.c b/src/parserstate.c index d528f8f2e..c612cf45b 100644 --- a/src/parserstate.c +++ b/src/parserstate.c @@ -186,11 +186,10 @@ static rbs_ast_comment_t *parse_comment_lines(parserstate *state, comment *com) const char *comment_start = state->lexstate->string.start + tok.range.start.byte_pos + hash_bytes; size_t comment_bytes = RANGE_BYTES(tok.range) - hash_bytes; - rbs_string_t str = { - .start = comment_start, - .end = state->lexstate->string.end, - .type = RBS_STRING_SHARED, - }; + rbs_string_t str = rbs_string_new( + comment_start, + state->lexstate->string.end + ); unsigned char c = utf8_to_codepoint(str); if (c == ' ') { diff --git a/src/rbs_buffer.c b/src/rbs_buffer.c index 164dd9091..d143f4f86 100644 --- a/src/rbs_buffer.c +++ b/src/rbs_buffer.c @@ -56,7 +56,7 @@ void rbs_buffer_append_string(rbs_buffer_t *buffer, const char *value, size_t le } rbs_string_t rbs_buffer_to_string(rbs_buffer_t *buffer) { - return rbs_string_shared_new(buffer->value, buffer->value + buffer->length); + return rbs_string_new(buffer->value, buffer->value + buffer->length); } void rbs_buffer_free(rbs_buffer_t *buffer) { diff --git a/src/rbs_string.c b/src/rbs_string.c index b7ac5c4cc..e9fd16796 100644 --- a/src/rbs_string.c +++ b/src/rbs_string.c @@ -5,19 +5,10 @@ #include #include -rbs_string_t rbs_string_shared_new(const char *start, const char *end) { +rbs_string_t rbs_string_new(const char *start, const char *end) { return (rbs_string_t) { .start = start, .end = end, - .type = RBS_STRING_SHARED, - }; -} - -rbs_string_t rbs_string_owned_new(const char *start, const char *end) { - return (rbs_string_t) { - .start = start, - .end = end, - .type = RBS_STRING_OWNED, }; } @@ -26,7 +17,7 @@ rbs_string_t rbs_string_copy_slice(rbs_allocator_t *allocator, rbs_string_t *sel strncpy(buffer, self->start + start_inset, length); buffer[length] = '\0'; - return rbs_string_owned_new(buffer, buffer + length); + return rbs_string_new(buffer, buffer + length); } rbs_string_t rbs_string_strip_whitespace(rbs_allocator_t *allocator, rbs_string_t *self) { @@ -36,7 +27,7 @@ rbs_string_t rbs_string_strip_whitespace(rbs_allocator_t *allocator, rbs_string_ } if (new_start == self->end) { // Handle empty string case - return rbs_string_shared_new(new_start, new_start); + return rbs_string_new(new_start, new_start); } const char *new_end = self->end - 1; diff --git a/src/rbs_unescape.c b/src/rbs_unescape.c index c1ae97f5c..be96b8c76 100644 --- a/src/rbs_unescape.c +++ b/src/rbs_unescape.c @@ -103,7 +103,7 @@ rbs_string_t unescape_string(rbs_allocator_t *allocator, rbs_string_t string, bo } } output[j] = '\0'; - return rbs_string_owned_new(output, output + j); + return rbs_string_new(output, output + j); } rbs_string_t rbs_unquote_string(rbs_allocator_t *allocator, rbs_string_t input) { diff --git a/templates/src/ast.c.erb b/templates/src/ast.c.erb index 74eb338b5..0e3b24fb4 100644 --- a/templates/src/ast.c.erb +++ b/templates/src/ast.c.erb @@ -206,7 +206,7 @@ void rbs_node_destroy(rbs_node_t *any_node) { <%- node.fields.each do |field| -%> <%- case field.c_type -%> <%- when "rbs_string" -%> - // <%= field.c_name %> is a string, so it will be freed when the allocator is freed. + // node-><%= field.c_name %> is a `rbs_string_t` that will be freed by the arena allocator. <%- when "rbs_node_list" -%> rbs_node_list_free(node-><%= field.c_name %>); <%- when "rbs_hash" -%> From 40fb219fbd0d63db0664b6244b381f0dc6219182 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 12 Mar 2025 22:28:08 +0800 Subject: [PATCH 087/111] Allocate `rbs_buffer_t` with allocator (#13) * Remove unnecessary rbs_buffer_init_with_capacity function * Manage buffer allocation with allocator * Set default capacity for buffer to 128 Most comments are less than 128 bytes, so this should help reduce memory waste. See https://github.com/Shopify/rbs/pull/13#discussion_r1990055172 --- include/rbs/rbs_buffer.h | 34 +++++++++++-------------- src/parserstate.c | 6 ++--- src/rbs_buffer.c | 55 +++++++++++++++++----------------------- 3 files changed, 41 insertions(+), 54 deletions(-) diff --git a/include/rbs/rbs_buffer.h b/include/rbs/rbs_buffer.h index a8e70c1b6..bcbcb1986 100644 --- a/include/rbs/rbs_buffer.h +++ b/include/rbs/rbs_buffer.h @@ -2,11 +2,20 @@ #define RBS__RBS_BUFFER_H #include "rbs_string.h" +#include "rbs/util/rbs_allocator.h" #include #include #include + + +/** + * The default capacity of a rbs_buffer_t. + * If the buffer needs to grow beyond this capacity, it will be doubled. + */ +#define RBS_BUFFER_DEFAULT_CAPACITY 128 + /** * A rbs_buffer_t is a simple memory buffer that stores data in a contiguous block of memory. */ @@ -21,22 +30,14 @@ typedef struct { char *value; } rbs_buffer_t; -/** - * Initialize a rbs_buffer_t with the given capacity. - * - * @param buffer The buffer to initialize. - * @param capacity The capacity of the buffer. - * @returns True if the buffer was initialized successfully, false otherwise. - */ -bool rbs_buffer_init_capacity(rbs_buffer_t *buffer, size_t capacity); - /** * Initialize a rbs_buffer_t with its default values. * + * @param allocator The allocator to use. * @param buffer The buffer to initialize. * @returns True if the buffer was initialized successfully, false otherwise. */ -bool rbs_buffer_init(rbs_buffer_t *buffer); +bool rbs_buffer_init(rbs_allocator_t *, rbs_buffer_t *buffer); /** * Return the value of the buffer. @@ -57,19 +58,21 @@ size_t rbs_buffer_length(const rbs_buffer_t *buffer); /** * Append a C string to the buffer. * + * @param allocator The allocator to use. * @param buffer The buffer to append to. * @param value The C string to append. */ -void rbs_buffer_append_cstr(rbs_buffer_t *buffer, const char *value); +void rbs_buffer_append_cstr(rbs_allocator_t *, rbs_buffer_t *buffer, const char *value); /** * Append a string to the buffer. * + * @param allocator The allocator to use. * @param buffer The buffer to append to. * @param value The string to append. * @param length The length of the string to append. */ -void rbs_buffer_append_string(rbs_buffer_t *buffer, const char *value, size_t length); +void rbs_buffer_append_string(rbs_allocator_t *, rbs_buffer_t *buffer, const char *value, size_t length); /** * Convert the buffer to a rbs_string_t. @@ -79,11 +82,4 @@ void rbs_buffer_append_string(rbs_buffer_t *buffer, const char *value, size_t le */ rbs_string_t rbs_buffer_to_string(rbs_buffer_t *buffer); -/** - * Free the memory associated with the buffer. - * - * @param buffer The buffer to free. - */ -void pm_buffer_free(rbs_buffer_t *buffer); - #endif diff --git a/src/parserstate.c b/src/parserstate.c index c612cf45b..26cacce30 100644 --- a/src/parserstate.c +++ b/src/parserstate.c @@ -178,7 +178,7 @@ static rbs_ast_comment_t *parse_comment_lines(parserstate *state, comment *com) size_t space_bytes = state->lexstate->encoding->char_width((const uint8_t *) " ", (size_t) 1); rbs_buffer_t rbs_buffer; - rbs_buffer_init(&rbs_buffer); + rbs_buffer_init(&state->allocator, &rbs_buffer); for (size_t i = 0; i < com->line_count; i++) { token tok = com->tokens[i]; @@ -197,8 +197,8 @@ static rbs_ast_comment_t *parse_comment_lines(parserstate *state, comment *com) comment_bytes -= space_bytes; } - rbs_buffer_append_string(&rbs_buffer, comment_start, comment_bytes); - rbs_buffer_append_cstr(&rbs_buffer, "\n"); + rbs_buffer_append_string(&state->allocator, &rbs_buffer, comment_start, comment_bytes); + rbs_buffer_append_cstr(&state->allocator, &rbs_buffer, "\n"); } return rbs_ast_comment_new( diff --git a/src/rbs_buffer.c b/src/rbs_buffer.c index d143f4f86..71e48713f 100644 --- a/src/rbs_buffer.c +++ b/src/rbs_buffer.c @@ -1,17 +1,17 @@ #include "rbs/rbs_buffer.h" -bool rbs_buffer_init_capacity(rbs_buffer_t *buffer, size_t capacity) { +#include + +bool rbs_buffer_init(rbs_allocator_t *allocator, rbs_buffer_t *buffer) { + size_t capacity = RBS_BUFFER_DEFAULT_CAPACITY; + buffer->length = 0; buffer->capacity = capacity; - buffer->value = (char *) malloc(capacity); + buffer->value = rbs_allocator_calloc(allocator, capacity, char); return buffer->value != NULL; } -bool rbs_buffer_init(rbs_buffer_t *buffer) { - return rbs_buffer_init_capacity(buffer, 1024); -} - char *rbs_buffer_value(const rbs_buffer_t *buffer) { return buffer->value; } @@ -20,45 +20,36 @@ size_t rbs_buffer_length(const rbs_buffer_t *buffer) { return buffer->length; } -static inline bool rbs_buffer_append_length(rbs_buffer_t *buffer, size_t length) { +void rbs_buffer_append_string(rbs_allocator_t *allocator, rbs_buffer_t *buffer, const char *source, size_t length) { size_t next_length = buffer->length + length; if (next_length > buffer->capacity) { - if (buffer->capacity == 0) { - buffer->capacity = 1; - } + size_t old_capacity = buffer->capacity; - while (next_length > buffer->capacity) { - buffer->capacity *= 2; - } + assert(old_capacity != 0 && "Precondition: capacity must be at least 1."); - buffer->value = realloc(buffer->value, buffer->capacity); - if (buffer->value == NULL) return false; - } + size_t new_capacity = buffer->capacity * 2; - buffer->length = next_length; - return true; -} + while (next_length > new_capacity) { + new_capacity *= 2; + } -static inline void rbs_buffer_append(rbs_buffer_t *buffer, const void *source, size_t length) { - size_t cursor = buffer->length; - if (rbs_buffer_append_length(buffer, length)) { - memcpy(buffer->value + cursor, source, length); + char *new_value = rbs_allocator_realloc(allocator, buffer->value, old_capacity, new_capacity, char); + assert(new_value != NULL && "Failed to append to buffer"); + + buffer->value = new_value; + buffer->capacity = new_capacity; } -} -void rbs_buffer_append_cstr(rbs_buffer_t *buffer, const char *value) { - rbs_buffer_append(buffer, value, strlen(value)); + size_t cursor = buffer->length; + buffer->length = next_length; + memcpy(buffer->value + cursor, source, length); } -void rbs_buffer_append_string(rbs_buffer_t *buffer, const char *value, size_t length) { - rbs_buffer_append(buffer, value, length); +void rbs_buffer_append_cstr(rbs_allocator_t *allocator, rbs_buffer_t *buffer, const char *value) { + rbs_buffer_append_string(allocator, buffer, value, strlen(value)); } rbs_string_t rbs_buffer_to_string(rbs_buffer_t *buffer) { return rbs_string_new(buffer->value, buffer->value + buffer->length); } - -void rbs_buffer_free(rbs_buffer_t *buffer) { - free(buffer->value); -} From b5ebb6229455aa63164a687d3bd7e21a9ef8c170 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 12 Mar 2025 11:31:33 +0000 Subject: [PATCH 088/111] Free parser before raising Ruby exceptions --- ext/rbs_extension/main.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 1bba1d7b3..d486604c3 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -51,6 +51,7 @@ static void declare_type_variables(parserstate *parser, VALUE variables, VALUE b if (NIL_P(variables)) return; // Nothing to do. if (!RB_TYPE_P(variables, T_ARRAY)) { + free_parser(parser); rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (must be an Array of Symbols or nil)", rb_obj_class(variables)); @@ -62,6 +63,7 @@ static void declare_type_variables(parserstate *parser, VALUE variables, VALUE b VALUE symbol = rb_ary_entry(variables, i); if (!RB_TYPE_P(symbol, T_SYMBOL)) { + free_parser(parser); rb_raise(rb_eTypeError, "Type variables Array contains invalid value %"PRIsVALUE" of type %"PRIsVALUE" (must be an Array of Symbols or nil)", rb_inspect(symbol), rb_obj_class(symbol)); From b28e38b497a382c1a3016f94c3aae1869a4dae3a Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 12 Mar 2025 23:46:10 +0800 Subject: [PATCH 089/111] Merge pull request #17 from Shopify/add-allocate-many-function Add `rbs_allocator_alloc_many` to avoid unnecessary 0 initialization on memory --- include/rbs/util/rbs_allocator.h | 18 +++++++++++++----- src/parser.c | 2 +- src/rbs_string.c | 2 +- src/rbs_unescape.c | 2 +- src/util/rbs_allocator.c | 6 +++++- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/include/rbs/util/rbs_allocator.h b/include/rbs/util/rbs_allocator.h index 28b9360e9..a50a5eef4 100644 --- a/include/rbs/util/rbs_allocator.h +++ b/include/rbs/util/rbs_allocator.h @@ -21,12 +21,20 @@ typedef struct rbs_allocator { void rbs_allocator_init(rbs_allocator_t *); void rbs_allocator_free(rbs_allocator_t *); -void *rbs_allocator_malloc_impl(rbs_allocator_t *, /* 1 */ size_t size, size_t alignment); -void *rbs_allocator_calloc_impl(rbs_allocator_t *, size_t count, size_t size, size_t alignment); -void *rbs_allocator_realloc_impl(rbs_allocator_t *, void *ptr, size_t old_size, size_t new_size, size_t alignment); +void *rbs_allocator_malloc_impl (rbs_allocator_t *, /* 1 */ size_t size, size_t alignment); +void *rbs_allocator_malloc_many_impl (rbs_allocator_t *, size_t count, size_t size, size_t alignment); +void *rbs_allocator_calloc_impl (rbs_allocator_t *, size_t count, size_t size, size_t alignment); -#define rbs_allocator_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), alignof(type))) -#define rbs_allocator_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), alignof(type))) +void *rbs_allocator_realloc_impl (rbs_allocator_t *, void *ptr, size_t old_size, size_t new_size, size_t alignment); + +// Use this when allocating memory for a single instance of a type. +#define rbs_allocator_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), alignof(type))) +// Use this when allocating memory that will be immediately written to in full. +// Such as allocating strings +#define rbs_allocator_alloc_many(allocator, count, type) ((type *) rbs_allocator_malloc_many_impl((allocator), (count), sizeof(type), alignof(type))) +// Use this when allocating memory that will NOT be immediately written to in full. +// Such as allocating buffers +#define rbs_allocator_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), alignof(type))) #define rbs_allocator_realloc(allocator, ptr, old_size, new_size, type) ((type *) rbs_allocator_realloc_impl((allocator), (ptr), (old_size), (new_size), alignof(type))) void rbs__init_arena_allocator(void); diff --git a/src/parser.c b/src/parser.c index a4642942f..9de43b560 100644 --- a/src/parser.c +++ b/src/parser.c @@ -131,7 +131,7 @@ void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt int length = vsnprintf(NULL, 0, fmt, args); va_end(args); - char *message = rbs_allocator_calloc(&state->allocator, length + 1, char); + char *message = rbs_allocator_alloc_many(&state->allocator, length + 1, char); va_start(args, fmt); vsnprintf(message, length + 1, fmt, args); diff --git a/src/rbs_string.c b/src/rbs_string.c index e9fd16796..8d2976559 100644 --- a/src/rbs_string.c +++ b/src/rbs_string.c @@ -13,7 +13,7 @@ rbs_string_t rbs_string_new(const char *start, const char *end) { } rbs_string_t rbs_string_copy_slice(rbs_allocator_t *allocator, rbs_string_t *self, size_t start_inset, size_t length) { - char *buffer = rbs_allocator_calloc(allocator, length + 1, char); + char *buffer = rbs_allocator_alloc_many(allocator, length + 1, char); strncpy(buffer, self->start + start_inset, length); buffer[length] = '\0'; diff --git a/src/rbs_unescape.c b/src/rbs_unescape.c index be96b8c76..66fc102e7 100644 --- a/src/rbs_unescape.c +++ b/src/rbs_unescape.c @@ -49,7 +49,7 @@ rbs_string_t unescape_string(rbs_allocator_t *allocator, rbs_string_t string, bo size_t len = string.end - string.start; const char* input = string.start; - char* output = rbs_allocator_calloc(allocator, len + 1, char); + char* output = rbs_allocator_alloc_many(allocator, len + 1, char); if (!output) return RBS_STRING_NULL; size_t i = 0, j = 0; diff --git a/src/util/rbs_allocator.c b/src/util/rbs_allocator.c index c61c9ff8e..18772a175 100644 --- a/src/util/rbs_allocator.c +++ b/src/util/rbs_allocator.c @@ -183,8 +183,12 @@ void *rbs_allocator_malloc_impl(rbs_allocator_t *allocator, size_t size, size_t // Note: This will eagerly fill with zeroes, unlike `calloc()` which can map a page in a page to be zeroed lazily. // It's assumed that callers to this function will immediately write to the allocated memory, anyway. void *rbs_allocator_calloc_impl(rbs_allocator_t *allocator, size_t count, size_t size, size_t alignment) { - void *p = rbs_allocator_malloc_impl(allocator, count * size, alignment); + void *p = rbs_allocator_malloc_many_impl(allocator, count, size, alignment); memset(p, 0, count * size); return p; } +// Similar to `rbs_allocator_malloc_impl()`, but allocates `count` instances of `size` bytes, aligned to an `alignment`-byte boundary. +void *rbs_allocator_malloc_many_impl(rbs_allocator_t *allocator, size_t count, size_t size, size_t alignment) { + return rbs_allocator_malloc_impl(allocator, count * size, alignment); +} From 5afb8798ba8c789d6d6b11ed36a5aeedf623ed3e Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Thu, 13 Mar 2025 00:47:42 +0800 Subject: [PATCH 090/111] Fix code that triggers warnings when compiled with Sorbet (#19) ### Avoid declaring unused `node` variables in `rbs_node_destroy`'s template Such declarations will fail Sorbet's compiler: ``` prism/templates/src/ast.c.erb:203:31: error: unused variable 'node' [-Werror,-Wunused-variable] rbs_ast_annotation_t *node = (rbs_ast_annotation_t *)any_node; ^ prism/templates/src/ast.c.erb:203:28: error: unused variable 'node' [-Werror,-Wunused-variable] rbs_ast_comment_t *node = (rbs_ast_comment_t *)any_node; ^ prism/templates/src/ast.c.erb:203:28: error: unused variable 'node' [-Werror,-Wunused-variable] rbs_ast_integer_t *node = (rbs_ast_integer_t *)any_node; ^ prism/templates/src/ast.c.erb:203:27: error: unused variable 'node' [-Werror,-Wunused-variable] rbs_ast_string_t *node = (rbs_ast_string_t *)any_node; ^ ``` ### Use `size_t` instead of `int` for `capacity` in `rbs_loc_alloc_children` This fixes a warning about comparing `int` and `size_t` in `rbs_loc_alloc_children`'s assertion. ### Avoid declaring the `max` variable that's just used in assertions When the `assert` is stripped out, the variable becomes unused and triggers a warning. ``` external/rbs_parser/src/rbs_location.c:8:10: warning: unused variable 'max' [-Wunused-variable] size_t max = sizeof(rbs_loc_entry_bitmap) * 8; ^ ``` --- include/rbs/rbs_location.h | 2 +- src/ast.c | 12 ------------ src/rbs_location.c | 7 +++---- templates/src/ast.c.erb | 2 -- templates/template.rb | 2 +- 5 files changed, 5 insertions(+), 20 deletions(-) diff --git a/include/rbs/rbs_location.h b/include/rbs/rbs_location.h index c00b40111..87c016a68 100644 --- a/include/rbs/rbs_location.h +++ b/include/rbs/rbs_location.h @@ -12,7 +12,7 @@ typedef struct rbs_location { rbs_loc_children *children; } rbs_location_t; -void rbs_loc_alloc_children(rbs_allocator_t *, rbs_location_t *loc, int size); +void rbs_loc_alloc_children(rbs_allocator_t *, rbs_location_t *loc, size_t capacity); void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, range r); void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r); diff --git a/src/ast.c b/src/ast.c index 3f7bd1c57..8d60c7454 100644 --- a/src/ast.c +++ b/src/ast.c @@ -1277,9 +1277,6 @@ void rbs_node_destroy(rbs_node_t *any_node) { switch (any_node->type) { #line 202 "prism/templates/src/ast.c.erb" case RBS_AST_ANNOTATION: { - rbs_ast_annotation_t *node = (rbs_ast_annotation_t *)any_node; - - // node->string is a `rbs_string_t` that will be freed by the arena allocator. break; } #line 202 "prism/templates/src/ast.c.erb" @@ -1288,9 +1285,6 @@ void rbs_node_destroy(rbs_node_t *any_node) { } #line 202 "prism/templates/src/ast.c.erb" case RBS_AST_COMMENT: { - rbs_ast_comment_t *node = (rbs_ast_comment_t *)any_node; - - // node->string is a `rbs_string_t` that will be freed by the arena allocator. break; } #line 202 "prism/templates/src/ast.c.erb" @@ -1469,9 +1463,6 @@ void rbs_node_destroy(rbs_node_t *any_node) { } #line 202 "prism/templates/src/ast.c.erb" case RBS_AST_INTEGER: { - rbs_ast_integer_t *node = (rbs_ast_integer_t *)any_node; - - // node->string_representation is a `rbs_string_t` that will be freed by the arena allocator. break; } #line 202 "prism/templates/src/ast.c.erb" @@ -1696,9 +1687,6 @@ void rbs_node_destroy(rbs_node_t *any_node) { } #line 202 "prism/templates/src/ast.c.erb" case RBS_AST_STRING: { - rbs_ast_string_t *node = (rbs_ast_string_t *)any_node; - - // node->string is a `rbs_string_t` that will be freed by the arena allocator. break; } #line 202 "prism/templates/src/ast.c.erb" diff --git a/src/rbs_location.c b/src/rbs_location.c index 5f9d3225b..7ead354b6 100644 --- a/src/rbs_location.c +++ b/src/rbs_location.c @@ -4,9 +4,8 @@ #define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1)) -void rbs_loc_alloc_children(rbs_allocator_t *allocator, rbs_location_t *loc, int capacity) { - size_t max = sizeof(rbs_loc_entry_bitmap) * 8; - assert(capacity <= max && "Capacity is too large"); +void rbs_loc_alloc_children(rbs_allocator_t *allocator, rbs_location_t *loc, size_t capacity) { + assert(capacity <= sizeof(rbs_loc_entry_bitmap) * 8 && "Capacity is too large"); loc->children = rbs_allocator_malloc_impl(allocator, RBS_LOC_CHILDREN_SIZE(capacity), alignof(rbs_loc_children)); @@ -18,7 +17,7 @@ void rbs_loc_alloc_children(rbs_allocator_t *allocator, rbs_location_t *loc, int void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r) { assert(loc->children != NULL && "All children should have been pre-allocated with rbs_loc_alloc_children()"); assert((loc->children->len + 1 <= loc->children->cap) && "Not enough space was pre-allocated for the children."); - + unsigned short i = loc->children->len++; loc->children->entries[i].name = name; loc->children->entries[i].rg = (rbs_loc_range) { r.start.char_pos, r.end.char_pos }; diff --git a/templates/src/ast.c.erb b/templates/src/ast.c.erb index 0e3b24fb4..b92e74b24 100644 --- a/templates/src/ast.c.erb +++ b/templates/src/ast.c.erb @@ -205,8 +205,6 @@ void rbs_node_destroy(rbs_node_t *any_node) { <%- node.fields.each do |field| -%> <%- case field.c_type -%> - <%- when "rbs_string" -%> - // node-><%= field.c_name %> is a `rbs_string_t` that will be freed by the arena allocator. <%- when "rbs_node_list" -%> rbs_node_list_free(node-><%= field.c_name %>); <%- when "rbs_hash" -%> diff --git a/templates/template.rb b/templates/template.rb index 9843b5eee..35425a531 100644 --- a/templates/template.rb +++ b/templates/template.rb @@ -55,7 +55,7 @@ def ast_node? end def needs_to_be_freed? - !["VALUE", "bool"].include?(@c_type) + !["VALUE", "bool", "rbs_string"].include?(@c_type) end end From 2c74261173c91c538c233571ac5e8214a9919587 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Tue, 18 Mar 2025 01:21:30 +0800 Subject: [PATCH 091/111] Replace `assert` with `rbs_assert` for better assertion message (#23) Closes https://github.com/Shopify/team-ruby-dx/issues/1422 --- ext/rbs_extension/main.c | 3 ++- include/rbs/util/rbs_assert.h | 9 +++++++++ src/parser.c | 3 ++- src/parserstate.c | 4 ++-- src/rbs_buffer.c | 7 +++---- src/rbs_encoding.c | 5 +++-- src/rbs_location.c | 7 ++++--- src/util/rbs_allocator.c | 6 +++--- src/util/rbs_assert.c | 19 +++++++++++++++++++ src/util/rbs_constant_pool.c | 11 ++++++----- 10 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 include/rbs/util/rbs_assert.h create mode 100644 src/util/rbs_assert.c diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index d486604c3..26fa170df 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -1,4 +1,5 @@ #include "rbs_extension.h" +#include "rbs/util/rbs_assert.h" #include "rbs/util/rbs_allocator.h" #include "rbs/util/rbs_constant_pool.h" #include "ast_translation.h" @@ -15,7 +16,7 @@ * ``` * */ static NORETURN(void) raise_error(error *error, VALUE buffer) { - assert(error != NULL && "raise_error() called with NULL error"); + rbs_assert(error != NULL, "raise_error() called with NULL error"); if (!error->syntax_error) { rb_raise(rb_eRuntimeError, "Unexpected error"); diff --git a/include/rbs/util/rbs_assert.h b/include/rbs/util/rbs_assert.h new file mode 100644 index 000000000..6a6201f92 --- /dev/null +++ b/include/rbs/util/rbs_assert.h @@ -0,0 +1,9 @@ +#ifndef RBS_ASSERT_H +#define RBS_ASSERT_H + +#include "rbs/defines.h" +#include + +void rbs_assert(bool condition, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(2, 3); + +#endif diff --git a/src/parser.c b/src/parser.c index 9de43b560..af03e70e6 100644 --- a/src/parser.c +++ b/src/parser.c @@ -10,6 +10,7 @@ #include "rbs/encoding.h" #include "rbs/rbs_string.h" #include "rbs/rbs_unescape.h" +#include "rbs/util/rbs_assert.h" #define INTERN(str) \ rbs_constant_pool_insert_constant( \ @@ -237,7 +238,7 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb ids = "class/module/constant name"; } - assert(ids != NULL && "Unknown kind of type"); + rbs_assert(ids != NULL, "Unknown kind of type: %i", kind); set_error(state, state->current_token, true, "expected one of %s", ids); return false; diff --git a/src/parserstate.c b/src/parserstate.c index 26cacce30..cc8c185de 100644 --- a/src/parserstate.c +++ b/src/parserstate.c @@ -3,6 +3,7 @@ #include "rbs/parser.h" #include "rbs/encoding.h" #include "rbs/rbs_buffer.h" +#include "rbs/util/rbs_assert.h" #include @@ -367,8 +368,7 @@ void rbs_parser_declare_type_variables(parserstate *parser, size_t count, const ); if (!parser_insert_typevar(parser, name)) { - fprintf(stderr, "RuntimeError: %s\n", parser->error->message); - exit(1); + rbs_assert(false, "Failed to insert type variable. Message: %s", parser->error->message); } } } diff --git a/src/rbs_buffer.c b/src/rbs_buffer.c index 71e48713f..7803cafb2 100644 --- a/src/rbs_buffer.c +++ b/src/rbs_buffer.c @@ -1,6 +1,5 @@ #include "rbs/rbs_buffer.h" - -#include +#include "rbs/util/rbs_assert.h" bool rbs_buffer_init(rbs_allocator_t *allocator, rbs_buffer_t *buffer) { size_t capacity = RBS_BUFFER_DEFAULT_CAPACITY; @@ -26,7 +25,7 @@ void rbs_buffer_append_string(rbs_allocator_t *allocator, rbs_buffer_t *buffer, if (next_length > buffer->capacity) { size_t old_capacity = buffer->capacity; - assert(old_capacity != 0 && "Precondition: capacity must be at least 1."); + rbs_assert(old_capacity != 0, "Precondition: capacity must be at least 1. Got %zu", old_capacity); size_t new_capacity = buffer->capacity * 2; @@ -35,7 +34,7 @@ void rbs_buffer_append_string(rbs_allocator_t *allocator, rbs_buffer_t *buffer, } char *new_value = rbs_allocator_realloc(allocator, buffer->value, old_capacity, new_capacity, char); - assert(new_value != NULL && "Failed to append to buffer"); + rbs_assert(new_value != NULL, "Failed to append to buffer. Old capacity: %zu, new capacity: %zu", old_capacity, new_capacity); buffer->value = new_value; buffer->capacity = new_capacity; diff --git a/src/rbs_encoding.c b/src/rbs_encoding.c index c5d8bf794..181d5897c 100644 --- a/src/rbs_encoding.c +++ b/src/rbs_encoding.c @@ -1,4 +1,5 @@ #include "rbs/rbs_encoding.h" +#include "rbs/util/rbs_assert.h" #if defined(__GNUC__) # define RBS_ATTRIBUTE_UNUSED __attribute__((unused)) @@ -2261,7 +2262,7 @@ static const uint8_t rbs_utf_8_dfa[] = { */ static rbs_unicode_codepoint_t rbs_utf_8_codepoint(const uint8_t *b, ptrdiff_t n, size_t *width) { - assert(n >= 0); + rbs_assert(n >= 0, "n must be greater than or equal to 0. Got %ti", n); size_t maximum = (n > 4) ? 4 : ((size_t) n); uint32_t codepoint; @@ -2291,7 +2292,7 @@ rbs_utf_8_codepoint(const uint8_t *b, ptrdiff_t n, size_t *width) { */ size_t rbs_encoding_utf_8_char_width(const uint8_t *b, ptrdiff_t n) { - assert(n >= 0); + rbs_assert(n >= 0, "n must be greater than or equal to 0. Got %ti", n); size_t maximum = (n > 4) ? 4 : ((size_t) n); uint32_t state = 0; diff --git a/src/rbs_location.c b/src/rbs_location.c index 7ead354b6..cc79591b7 100644 --- a/src/rbs_location.c +++ b/src/rbs_location.c @@ -1,11 +1,12 @@ #include "rbs/rbs_location.h" +#include "rbs/util/rbs_assert.h" #include #define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1)) void rbs_loc_alloc_children(rbs_allocator_t *allocator, rbs_location_t *loc, size_t capacity) { - assert(capacity <= sizeof(rbs_loc_entry_bitmap) * 8 && "Capacity is too large"); + rbs_assert(capacity <= sizeof(rbs_loc_entry_bitmap) * 8, "Capacity %zu is too large. Max is %zu", capacity, sizeof(rbs_loc_entry_bitmap) * 8); loc->children = rbs_allocator_malloc_impl(allocator, RBS_LOC_CHILDREN_SIZE(capacity), alignof(rbs_loc_children)); @@ -15,8 +16,8 @@ void rbs_loc_alloc_children(rbs_allocator_t *allocator, rbs_location_t *loc, siz } void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r) { - assert(loc->children != NULL && "All children should have been pre-allocated with rbs_loc_alloc_children()"); - assert((loc->children->len + 1 <= loc->children->cap) && "Not enough space was pre-allocated for the children."); + rbs_assert(loc->children != NULL, "All children should have been pre-allocated with rbs_loc_alloc_children()"); + rbs_assert((loc->children->len + 1 <= loc->children->cap), "Not enough space was pre-allocated for the children. Children: %hu, Capacity: %hu", loc->children->len, loc->children->cap); unsigned short i = loc->children->len++; loc->children->entries[i].name = name; diff --git a/src/util/rbs_allocator.c b/src/util/rbs_allocator.c index 18772a175..56e422d05 100644 --- a/src/util/rbs_allocator.c +++ b/src/util/rbs_allocator.c @@ -15,9 +15,9 @@ */ #include "rbs/util/rbs_allocator.h" +#include "rbs/util/rbs_assert.h" #include -#include #include // for memset() #include @@ -138,7 +138,7 @@ void *rbs_allocator_realloc_impl(rbs_allocator_t *allocator, void *ptr, size_t o // Allocates `size` bytes from `allocator`, aligned to an `alignment`-byte boundary. void *rbs_allocator_malloc_impl(rbs_allocator_t *allocator, size_t size, size_t alignment) { - assert(size % alignment == 0 && "size must be a multiple of the alignment"); + rbs_assert(size % alignment == 0, "size must be a multiple of the alignment. size: %zu, alignment: %zu", size, alignment); if (default_page_payload_size < size) { // Big allocation, give it its own page. // How much we need to pad the new page's payload in order to get an aligned pointer @@ -176,7 +176,7 @@ void *rbs_allocator_malloc_impl(rbs_allocator_t *allocator, size_t size, size_t allocator->page = new_page; p = rbs_allocator_page_attempt_alloc(new_page, size, alignment); - assert(p != NULL && "Failed to allocate a new allocator page"); + rbs_assert(p != NULL, "Failed to allocate a new allocator page"); return p; } diff --git a/src/util/rbs_assert.c b/src/util/rbs_assert.c new file mode 100644 index 000000000..c28a59ebf --- /dev/null +++ b/src/util/rbs_assert.c @@ -0,0 +1,19 @@ +#include "rbs/util/rbs_assert.h" + +#include +#include +#include +#include + +void rbs_assert(bool condition, const char *fmt, ...) { + if (condition) { + return; + } + + va_list args; + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + fprintf(stderr, "\n"); + exit(EXIT_FAILURE); +} diff --git a/src/util/rbs_constant_pool.c b/src/util/rbs_constant_pool.c index 44776c98d..8df73c687 100644 --- a/src/util/rbs_constant_pool.c +++ b/src/util/rbs_constant_pool.c @@ -1,4 +1,5 @@ #include "rbs/util/rbs_constant_pool.h" +#include "rbs/util/rbs_assert.h" /** * A relatively simple hash function (djb2) that is used to hash strings. We are @@ -48,7 +49,7 @@ is_power_of_two(uint32_t size) { */ static inline bool rbs_constant_pool_resize(rbs_constant_pool_t *pool) { - assert(is_power_of_two(pool->capacity)); + rbs_assert(is_power_of_two(pool->capacity), "pool->capacity is not a power of two. Got %i", pool->capacity); uint32_t next_capacity = pool->capacity * 2; if (next_capacity < pool->capacity) return false; @@ -126,7 +127,7 @@ rbs_constant_pool_init(rbs_constant_pool_t *pool, uint32_t capacity) { */ rbs_constant_t * rbs_constant_pool_id_to_constant(const rbs_constant_pool_t *pool, rbs_constant_id_t constant_id) { - assert(constant_id != RBS_CONSTANT_ID_UNSET && constant_id <= pool->size); + rbs_assert(constant_id != RBS_CONSTANT_ID_UNSET && constant_id <= pool->size, "constant_id is not valid. Got %i, pool->size: %i", constant_id, pool->size); return &pool->constants[constant_id - 1]; } @@ -136,7 +137,7 @@ rbs_constant_pool_id_to_constant(const rbs_constant_pool_t *pool, rbs_constant_i */ rbs_constant_id_t rbs_constant_pool_find(const rbs_constant_pool_t *pool, const uint8_t *start, size_t length) { - assert(is_power_of_two(pool->capacity)); + rbs_assert(is_power_of_two(pool->capacity), "pool->capacity is not a power of two. Got %i", pool->capacity); const uint32_t mask = pool->capacity - 1; uint32_t hash = rbs_constant_pool_hash(start, length); @@ -164,7 +165,7 @@ rbs_constant_pool_insert(rbs_constant_pool_t *pool, const uint8_t *start, size_t if (!rbs_constant_pool_resize(pool)) return RBS_CONSTANT_ID_UNSET; } - assert(is_power_of_two(pool->capacity)); + rbs_assert(is_power_of_two(pool->capacity), "pool->capacity is not a power of two. Got %i", pool->capacity); const uint32_t mask = pool->capacity - 1; uint32_t hash = rbs_constant_pool_hash(start, length); @@ -205,7 +206,7 @@ rbs_constant_pool_insert(rbs_constant_pool_t *pool, const uint8_t *start, size_t // IDs are allocated starting at 1, since the value 0 denotes a non-existent // constant. uint32_t id = ++pool->size; - assert(pool->size < ((uint32_t) (1 << 30))); + rbs_assert(pool->size < ((uint32_t) (1 << 30)), "pool->size is too large. Got %i", pool->size); *bucket = (rbs_constant_pool_bucket_t) { .id = (unsigned int) (id & 0x3fffffff), From 1ca7576d8bb43412a0465788bb81ab125cfcef1f Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Sun, 23 Mar 2025 21:52:11 -0400 Subject: [PATCH 092/111] Strip whitespace without allocation --- include/rbs/rbs_string.h | 4 ++-- src/parser.c | 4 ++-- src/rbs_string.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/rbs/rbs_string.h b/include/rbs/rbs_string.h index 22669e64d..8db0f96b1 100644 --- a/include/rbs/rbs_string.h +++ b/include/rbs/rbs_string.h @@ -30,9 +30,9 @@ rbs_string_t rbs_string_copy_slice(rbs_allocator_t *, rbs_string_t *self, size_t /** * Drops the leading and trailing whitespace from the given string, in-place. - * @returns A new owned string that will be freed when the allocator is freed. + * @returns A new string that provides a view into the original string `self`. */ -rbs_string_t rbs_string_strip_whitespace(rbs_allocator_t *, rbs_string_t *self); +rbs_string_t rbs_string_strip_whitespace(rbs_string_t *self); /** * Returns the length of the string. diff --git a/src/parser.c b/src/parser.c index af03e70e6..9cd9fcec4 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1102,7 +1102,7 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { rbs_location_t *loc = rbs_location_current_token(state); rbs_string_t string = rbs_parser_peek_current_token(state); - rbs_string_t stripped_string = rbs_string_strip_whitespace(&state->allocator, &string); + rbs_string_t stripped_string = rbs_string_strip_whitespace( &string); rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(&state->allocator, loc, stripped_string); *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, loc, literal); @@ -1588,7 +1588,7 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati rbs_string_len(current_token) - total_offset - close_bytes ); - rbs_string_t stripped_annotation_str = rbs_string_strip_whitespace(&state->allocator, &annotation_str); + rbs_string_t stripped_annotation_str = rbs_string_strip_whitespace(&annotation_str); *annotation = rbs_ast_annotation_new(&state->allocator, rbs_location_new(&state->allocator, rg), stripped_annotation_str); return true; diff --git a/src/rbs_string.c b/src/rbs_string.c index 8d2976559..5c74b6f5c 100644 --- a/src/rbs_string.c +++ b/src/rbs_string.c @@ -20,7 +20,7 @@ rbs_string_t rbs_string_copy_slice(rbs_allocator_t *allocator, rbs_string_t *sel return rbs_string_new(buffer, buffer + length); } -rbs_string_t rbs_string_strip_whitespace(rbs_allocator_t *allocator, rbs_string_t *self) { +rbs_string_t rbs_string_strip_whitespace(rbs_string_t *self) { const char *new_start = self->start; while (isspace(*new_start) && new_start < self->end) { new_start++; @@ -35,7 +35,7 @@ rbs_string_t rbs_string_strip_whitespace(rbs_allocator_t *allocator, rbs_string_ new_end--; } - return rbs_string_copy_slice(allocator, self, new_start - self->start, new_end - new_start + 1); + return rbs_string_new(new_start, new_end + 1); } size_t rbs_string_len(const rbs_string_t self) { From 10075ee6e74c50203cd96c17b05da22a9550fe6a Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Sun, 23 Mar 2025 22:13:09 -0400 Subject: [PATCH 093/111] Remove all string slice copying --- include/rbs/rbs_unescape.h | 2 +- src/parser.c | 10 ++++------ src/rbs_string.c | 8 -------- src/rbs_unescape.c | 5 +++-- 4 files changed, 8 insertions(+), 17 deletions(-) diff --git a/include/rbs/rbs_unescape.h b/include/rbs/rbs_unescape.h index 0b4182acc..fef05a5e6 100644 --- a/include/rbs/rbs_unescape.h +++ b/include/rbs/rbs_unescape.h @@ -18,6 +18,6 @@ * * @returns A new owned string that will be freed when the allocator is freed. * */ -rbs_string_t rbs_unquote_string(rbs_allocator_t *, rbs_string_t input); +rbs_string_t rbs_unquote_string(rbs_allocator_t *, const rbs_string_t input); #endif // RBS_RBS_UNESCAPE_H diff --git a/src/parser.c b/src/parser.c index 9cd9fcec4..871add3f9 100644 --- a/src/parser.c +++ b/src/parser.c @@ -918,7 +918,7 @@ static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types rbs_location_t *symbolLoc = rbs_location_current_token(state); rbs_string_t current_token = rbs_parser_peek_current_token(state); - rbs_string_t symbol = rbs_string_copy_slice(&state->allocator, ¤t_token, offset_bytes, rbs_string_len(current_token) - offset_bytes); + rbs_string_t symbol = rbs_string_new(current_token.start + offset_bytes, current_token.end); rbs_string_t unquoted_symbol = rbs_unquote_string(&state->allocator, symbol); @@ -1581,11 +1581,9 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati rbs_string_t current_token = rbs_parser_peek_current_token(state); size_t total_offset = offset_bytes + open_bytes; - rbs_string_t annotation_str = rbs_string_copy_slice( - &state->allocator, - ¤t_token, - total_offset, - rbs_string_len(current_token) - total_offset - close_bytes + rbs_string_t annotation_str = rbs_string_new( + current_token.start + total_offset, + current_token.end - close_bytes ); rbs_string_t stripped_annotation_str = rbs_string_strip_whitespace(&annotation_str); diff --git a/src/rbs_string.c b/src/rbs_string.c index 5c74b6f5c..178a581e3 100644 --- a/src/rbs_string.c +++ b/src/rbs_string.c @@ -12,14 +12,6 @@ rbs_string_t rbs_string_new(const char *start, const char *end) { }; } -rbs_string_t rbs_string_copy_slice(rbs_allocator_t *allocator, rbs_string_t *self, size_t start_inset, size_t length) { - char *buffer = rbs_allocator_alloc_many(allocator, length + 1, char); - strncpy(buffer, self->start + start_inset, length); - buffer[length] = '\0'; - - return rbs_string_new(buffer, buffer + length); -} - rbs_string_t rbs_string_strip_whitespace(rbs_string_t *self) { const char *new_start = self->start; while (isspace(*new_start) && new_start < self->end) { diff --git a/src/rbs_unescape.c b/src/rbs_unescape.c index 66fc102e7..ed53c3017 100644 --- a/src/rbs_unescape.c +++ b/src/rbs_unescape.c @@ -43,7 +43,7 @@ static int octal_to_int(const char* octal, int length) { return result; } -rbs_string_t unescape_string(rbs_allocator_t *allocator, rbs_string_t string, bool is_double_quote) { +rbs_string_t unescape_string(rbs_allocator_t *allocator, const rbs_string_t string, bool is_double_quote) { if (!string.start) return RBS_STRING_NULL; size_t len = string.end - string.start; @@ -117,6 +117,7 @@ rbs_string_t rbs_unquote_string(rbs_allocator_t *allocator, rbs_string_t input) byte_length -= 2 * bs; } - rbs_string_t string = rbs_string_copy_slice(allocator, &input, start_offset, byte_length); + const char *new_start = input.start + start_offset; + rbs_string_t string = rbs_string_new(new_start, new_start + byte_length); return unescape_string(allocator, string, first_char == '"'); } From a8cdc87b092b247742ef5ab5d896392b9af65407 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Thu, 27 Mar 2025 03:53:37 +0800 Subject: [PATCH 094/111] Prefix internal lexer and encoding functions and types (#25) Part of https://github.com/Shopify/team-ruby-dx/issues/1436 --- include/rbs/encoding.h | 2 +- include/rbs/lexer.h | 30 +- include/rbs/parserstate.h | 2 +- src/encoding.c | 2 +- src/lexer.c | 630 +++++++++++++++++++------------------- src/lexer.re | 174 +++++------ src/lexstate.c | 26 +- src/parser.c | 52 ++-- src/parserstate.c | 10 +- src/rbs_unescape.c | 2 +- 10 files changed, 465 insertions(+), 465 deletions(-) diff --git a/include/rbs/encoding.h b/include/rbs/encoding.h index 3afc102bb..2717aa194 100644 --- a/include/rbs/encoding.h +++ b/include/rbs/encoding.h @@ -3,7 +3,7 @@ #include "rbs_string.h" -unsigned int utf8_to_codepoint(const rbs_string_t string); +unsigned int rbs_utf8_to_codepoint(const rbs_string_t string); int utf8_codelen(unsigned int c); #endif // RBS_ENCODING_H diff --git a/include/rbs/lexer.h b/include/rbs/lexer.h index 0e2130503..4d5e8e143 100644 --- a/include/rbs/lexer.h +++ b/include/rbs/lexer.h @@ -4,7 +4,7 @@ #include "rbs_string.h" #include "rbs_encoding.h" -enum TokenType { +enum RBSTokenType { NullType, /* (Nothing) */ pEOF, /* EOF */ ErrorToken, /* Error */ @@ -111,7 +111,7 @@ typedef struct { } range; typedef struct { - enum TokenType type; + enum RBSTokenType type; range range; } token; @@ -140,21 +140,21 @@ extern token NullToken; extern position NullPosition; extern range NULL_RANGE; -char *peek_token(lexstate *state, token tok); -int token_chars(token tok); -int token_bytes(token tok); +char *rbs_peek_token(lexstate *state, token tok); +int rbs_token_chars(token tok); +int rbs_token_bytes(token tok); -#define null_position_p(pos) (pos.byte_pos == -1) -#define null_range_p(range) (range.start.byte_pos == -1) -#define nonnull_pos_or(pos1, pos2) (null_position_p(pos1) ? pos2 : pos1) -#define RANGE_BYTES(range) (range.end.byte_pos - range.start.byte_pos) +#define rbs_null_position_p(pos) (pos.byte_pos == -1) +#define rbs_null_range_p(range) (range.start.byte_pos == -1) +#define rbs_nonnull_pos_or(pos1, pos2) (rbs_null_position_p(pos1) ? pos2 : pos1) +#define RBS_RANGE_BYTES(range) (range.end.byte_pos - range.start.byte_pos) -const char *token_type_str(enum TokenType type); +const char *token_type_str(enum RBSTokenType type); /** * Read next character. * */ -unsigned int peek(lexstate *state); +unsigned int rbs_peek(lexstate *state); /** * Skip one character. @@ -164,20 +164,20 @@ void rbs_skip(lexstate *state); /** * Skip n characters. * */ -void skipn(lexstate *state, size_t size); +void rbs_skipn(lexstate *state, size_t size); /** * Return new token with given type. * */ -token next_token(lexstate *state, enum TokenType type); +token rbs_next_token(lexstate *state, enum RBSTokenType type); /** * Return new token with EOF type. * */ -token next_eof_token(lexstate *state); +token rbs_next_eof_token(lexstate *state); token rbsparser_next_token(lexstate *state); -void print_token(token tok); +void rbs_print_token(token tok); #endif diff --git a/include/rbs/parserstate.h b/include/rbs/parserstate.h index cef37aaf6..548b89769 100644 --- a/include/rbs/parserstate.h +++ b/include/rbs/parserstate.h @@ -143,7 +143,7 @@ void parser_advance(parserstate *state); /** * Advance one token if the next_token is a token of the type. * */ -bool parser_advance_if(parserstate *state, enum TokenType type); +bool parser_advance_if(parserstate *state, enum RBSTokenType type); void print_parser(parserstate *state); /** diff --git a/src/encoding.c b/src/encoding.c index fdac17bbe..22461ab14 100644 --- a/src/encoding.c +++ b/src/encoding.c @@ -1,6 +1,6 @@ #include "rbs/encoding.h" -unsigned int utf8_to_codepoint(const rbs_string_t string) { +unsigned int rbs_utf8_to_codepoint(const rbs_string_t string) { unsigned int codepoint = 0; int remaining_bytes = 0; diff --git a/src/lexer.c b/src/lexer.c index 3d697809a..bd76768fa 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -12,7 +12,7 @@ token rbsparser_next_token(lexstate *state) { { unsigned int yych; unsigned int yyaccept = 0; - yych = peek(state); + yych = rbs_peek(state); switch (yych) { case 0x00000000: goto yy1; case '\t': @@ -116,51 +116,51 @@ token rbsparser_next_token(lexstate *state) { yy1: rbs_skip(state); #line 144 "src/lexer.re" - { return next_eof_token(state); } + { return rbs_next_eof_token(state); } #line 121 "src/lexer.c" yy2: rbs_skip(state); yy3: #line 145 "src/lexer.re" - { return next_token(state, ErrorToken); } + { return rbs_next_token(state, ErrorToken); } #line 127 "src/lexer.c" yy4: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '\t') goto yy4; if (yych == ' ') goto yy4; yy5: #line 143 "src/lexer.re" - { return next_token(state, tTRIVIA); } + { return rbs_next_token(state, tTRIVIA); } #line 136 "src/lexer.c" yy6: rbs_skip(state); goto yy5; yy7: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '=') goto yy24; if (yych == '~') goto yy24; yy8: #line 48 "src/lexer.re" - { return next_token(state, tOPERATOR); } + { return rbs_next_token(state, tOPERATOR); } #line 148 "src/lexer.c" yy9: yyaccept = 0; rbs_skip(state); backup = *state; - yych = peek(state); + yych = rbs_peek(state); if (yych <= 0x00000000) goto yy3; goto yy67; yy10: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= 0x00000000) goto yy11; if (yych != '\n') goto yy10; yy11: #line 59 "src/lexer.re" { - return next_token( + return rbs_next_token( state, state->first_token_of_line ? tLINECOMMENT : tCOMMENT ); @@ -168,7 +168,7 @@ token rbsparser_next_token(lexstate *state) { #line 169 "src/lexer.c" yy12: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= ')') { if (yych <= 0x0000001F) { if (yych <= '\n') { @@ -216,41 +216,41 @@ token rbsparser_next_token(lexstate *state) { yyaccept = 1; rbs_skip(state); backup = *state; - yych = peek(state); + yych = rbs_peek(state); if (yych == 'a') goto yy74; goto yy8; yy14: rbs_skip(state); #line 33 "src/lexer.re" - { return next_token(state, pAMP); } + { return rbs_next_token(state, pAMP); } #line 227 "src/lexer.c" yy15: yyaccept = 0; rbs_skip(state); backup = *state; - yych = peek(state); + yych = rbs_peek(state); if (yych <= 0x00000000) goto yy3; goto yy76; yy16: rbs_skip(state); #line 24 "src/lexer.re" - { return next_token(state, pLPAREN); } + { return rbs_next_token(state, pLPAREN); } #line 239 "src/lexer.c" yy17: rbs_skip(state); #line 25 "src/lexer.re" - { return next_token(state, pRPAREN); } + { return rbs_next_token(state, pRPAREN); } #line 244 "src/lexer.c" yy18: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '*') goto yy80; #line 35 "src/lexer.re" - { return next_token(state, pSTAR); } + { return rbs_next_token(state, pSTAR); } #line 251 "src/lexer.c" yy19: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '/') goto yy8; if (yych <= '9') goto yy25; if (yych == '@') goto yy24; @@ -258,11 +258,11 @@ token rbsparser_next_token(lexstate *state) { yy20: rbs_skip(state); #line 30 "src/lexer.re" - { return next_token(state, pCOMMA); } + { return rbs_next_token(state, pCOMMA); } #line 263 "src/lexer.c" yy21: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') goto yy8; if (yych <= '9') goto yy25; @@ -276,30 +276,30 @@ token rbsparser_next_token(lexstate *state) { yyaccept = 2; rbs_skip(state); backup = *state; - yych = peek(state); + yych = rbs_peek(state); if (yych == '.') goto yy82; yy23: #line 37 "src/lexer.re" - { return next_token(state, pDOT); } + { return rbs_next_token(state, pDOT); } #line 285 "src/lexer.c" yy24: rbs_skip(state); goto yy8; yy25: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '/') goto yy26; if (yych <= '9') goto yy25; if (yych == '_') goto yy25; yy26: #line 51 "src/lexer.re" - { return next_token(state, tINTEGER); } + { return rbs_next_token(state, tINTEGER); } #line 298 "src/lexer.c" yy27: yyaccept = 3; rbs_skip(state); backup = *state; - yych = peek(state); + yych = rbs_peek(state); switch (yych) { case '!': goto yy83; case '"': goto yy85; @@ -378,21 +378,21 @@ token rbsparser_next_token(lexstate *state) { } yy28: #line 44 "src/lexer.re" - { return next_token(state, pCOLON); } + { return rbs_next_token(state, pCOLON); } #line 383 "src/lexer.c" yy29: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= ';') goto yy30; if (yych <= '<') goto yy24; if (yych <= '=') goto yy99; yy30: #line 46 "src/lexer.re" - { return next_token(state, pLT); } + { return rbs_next_token(state, pLT); } #line 393 "src/lexer.c" yy31: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '>') { if (yych <= '<') goto yy32; if (yych <= '=') goto yy100; @@ -402,24 +402,24 @@ token rbsparser_next_token(lexstate *state) { } yy32: #line 43 "src/lexer.re" - { return next_token(state, pEQ); } + { return rbs_next_token(state, pEQ); } #line 407 "src/lexer.c" yy33: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '<') goto yy8; if (yych <= '>') goto yy24; goto yy8; yy34: rbs_skip(state); #line 34 "src/lexer.re" - { return next_token(state, pQUESTION); } + { return rbs_next_token(state, pQUESTION); } #line 418 "src/lexer.c" yy35: yyaccept = 0; rbs_skip(state); backup = *state; - yych = peek(state); + yych = rbs_peek(state); if (yych <= '^') { if (yych <= '?') goto yy3; if (yych <= '@') goto yy102; @@ -432,7 +432,7 @@ token rbsparser_next_token(lexstate *state) { } yy36: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -451,28 +451,28 @@ token rbsparser_next_token(lexstate *state) { } yy37: #line 129 "src/lexer.re" - { return next_token(state, tUIDENT); } + { return rbs_next_token(state, tUIDENT); } #line 456 "src/lexer.c" yy38: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == ']') goto yy107; #line 26 "src/lexer.re" - { return next_token(state, pLBRACKET); } + { return rbs_next_token(state, pLBRACKET); } #line 463 "src/lexer.c" yy39: rbs_skip(state); #line 27 "src/lexer.re" - { return next_token(state, pRBRACKET); } + { return rbs_next_token(state, pRBRACKET); } #line 468 "src/lexer.c" yy40: rbs_skip(state); #line 32 "src/lexer.re" - { return next_token(state, pHAT); } + { return rbs_next_token(state, pHAT); } #line 473 "src/lexer.c" yy41: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -492,13 +492,13 @@ token rbsparser_next_token(lexstate *state) { } yy42: #line 132 "src/lexer.re" - { return next_token(state, tULLIDENT); } + { return rbs_next_token(state, tULLIDENT); } #line 497 "src/lexer.c" yy43: yyaccept = 4; rbs_skip(state); backup = *state; - yych = peek(state); + yych = rbs_peek(state); if (yych <= ' ') { if (yych <= 0x00000000) goto yy44; if (yych <= 0x0000001F) goto yy114; @@ -507,11 +507,11 @@ token rbsparser_next_token(lexstate *state) { } yy44: #line 39 "src/lexer.re" - { return next_token(state, tOPERATOR); } + { return rbs_next_token(state, tOPERATOR); } #line 512 "src/lexer.c" yy45: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= 'r') { if (yych == 'l') goto yy115; goto yy53; @@ -522,37 +522,37 @@ token rbsparser_next_token(lexstate *state) { } yy46: #line 128 "src/lexer.re" - { return next_token(state, tLIDENT); } + { return rbs_next_token(state, tLIDENT); } #line 527 "src/lexer.c" yy47: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'o') goto yy119; goto yy53; yy48: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'l') goto yy120; goto yy53; yy49: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy121; goto yy53; yy50: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'n') goto yy122; if (yych == 'x') goto yy123; goto yy53; yy51: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'a') goto yy124; goto yy53; yy52: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); yy53: if (yych <= '=') { if (yych <= '/') { @@ -576,39 +576,39 @@ token rbsparser_next_token(lexstate *state) { } yy54: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'n') goto yy125; goto yy53; yy55: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'o') goto yy127; goto yy53; yy56: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'i') goto yy128; goto yy53; yy57: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'u') goto yy129; goto yy53; yy58: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'r') goto yy130; if (yych == 'u') goto yy131; goto yy53; yy59: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy132; if (yych == 'i') goto yy133; goto yy53; yy60: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= 'q') { if (yych == 'o') goto yy134; goto yy53; @@ -619,33 +619,33 @@ token rbsparser_next_token(lexstate *state) { } yy61: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'n') goto yy137; if (yych == 's') goto yy138; goto yy53; yy62: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'o') goto yy139; goto yy53; yy63: rbs_skip(state); #line 28 "src/lexer.re" - { return next_token(state, pLBRACE); } + { return rbs_next_token(state, pLBRACE); } #line 636 "src/lexer.c" yy64: rbs_skip(state); #line 31 "src/lexer.re" - { return next_token(state, pBAR); } + { return rbs_next_token(state, pBAR); } #line 641 "src/lexer.c" yy65: rbs_skip(state); #line 29 "src/lexer.re" - { return next_token(state, pRBRACE); } + { return rbs_next_token(state, pRBRACE); } #line 646 "src/lexer.c" yy66: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); yy67: if (yych <= '"') { if (yych <= 0x00000000) goto yy68; @@ -685,17 +685,17 @@ token rbsparser_next_token(lexstate *state) { yy69: rbs_skip(state); #line 106 "src/lexer.re" - { return next_token(state, tDQSTRING); } + { return rbs_next_token(state, tDQSTRING); } #line 690 "src/lexer.c" yy70: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'u') goto yy140; if (yych == 'x') goto yy141; goto yy66; yy71: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= ',') { if (yych <= '\f') { if (yych <= 0x00000000) goto yy72; @@ -724,14 +724,14 @@ token rbsparser_next_token(lexstate *state) { } yy72: #line 139 "src/lexer.re" - { return next_token(state, tGIDENT); } + { return rbs_next_token(state, tGIDENT); } #line 729 "src/lexer.c" yy73: rbs_skip(state); goto yy72; yy74: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= 'Z') { if (yych <= '(') { if (yych <= '\'') goto yy68; @@ -752,7 +752,7 @@ token rbsparser_next_token(lexstate *state) { } yy75: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); yy76: if (yych <= '\'') { if (yych <= 0x00000000) goto yy68; @@ -765,11 +765,11 @@ token rbsparser_next_token(lexstate *state) { rbs_skip(state); yy78: #line 107 "src/lexer.re" - { return next_token(state, tSQSTRING); } + { return rbs_next_token(state, tSQSTRING); } #line 770 "src/lexer.c" yy79: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '\'') { if (yych <= 0x00000000) goto yy68; if (yych <= '&') goto yy75; @@ -781,30 +781,30 @@ token rbsparser_next_token(lexstate *state) { yy80: rbs_skip(state); #line 36 "src/lexer.re" - { return next_token(state, pSTAR2); } + { return rbs_next_token(state, pSTAR2); } #line 786 "src/lexer.c" yy81: rbs_skip(state); #line 41 "src/lexer.re" - { return next_token(state, pARROW); } + { return rbs_next_token(state, pARROW); } #line 791 "src/lexer.c" yy82: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '.') goto yy148; goto yy68; yy83: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '=') goto yy87; if (yych == '~') goto yy87; yy84: #line 126 "src/lexer.re" - { return next_token(state, tSYMBOL); } + { return rbs_next_token(state, tSYMBOL); } #line 805 "src/lexer.c" yy85: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '"') { if (yych <= 0x00000000) goto yy68; if (yych <= '!') goto yy85; @@ -815,7 +815,7 @@ token rbsparser_next_token(lexstate *state) { } yy86: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= ')') { if (yych <= 0x0000001F) { if (yych <= '\n') { @@ -864,7 +864,7 @@ token rbsparser_next_token(lexstate *state) { goto yy84; yy88: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '\'') { if (yych <= 0x00000000) goto yy68; if (yych <= '&') goto yy88; @@ -875,41 +875,41 @@ token rbsparser_next_token(lexstate *state) { } yy89: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '*') goto yy87; goto yy84; yy90: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '@') goto yy87; goto yy84; yy91: rbs_skip(state); #line 45 "src/lexer.re" - { return next_token(state, pCOLON2); } + { return rbs_next_token(state, pCOLON2); } #line 891 "src/lexer.c" yy92: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= ';') goto yy84; if (yych <= '<') goto yy87; if (yych <= '=') goto yy157; goto yy84; yy93: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '=') goto yy158; if (yych == '~') goto yy87; goto yy68; yy94: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '<') goto yy84; if (yych <= '>') goto yy87; goto yy84; yy95: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '^') { if (yych <= '?') goto yy68; if (yych <= '@') goto yy159; @@ -922,7 +922,7 @@ token rbsparser_next_token(lexstate *state) { } yy96: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '>') { if (yych <= '/') { if (yych == '!') goto yy162; @@ -942,31 +942,31 @@ token rbsparser_next_token(lexstate *state) { } yy97: #line 122 "src/lexer.re" - { return next_token(state, tSYMBOL); } + { return rbs_next_token(state, tSYMBOL); } #line 947 "src/lexer.c" yy98: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == ']') goto yy158; goto yy68; yy99: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '>') goto yy24; goto yy8; yy100: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '=') goto yy24; goto yy8; yy101: rbs_skip(state); #line 42 "src/lexer.re" - { return next_token(state, pFATARROW); } + { return rbs_next_token(state, pFATARROW); } #line 967 "src/lexer.c" yy102: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '^') { if (yych <= '@') goto yy68; if (yych <= 'Z') goto yy163; @@ -978,7 +978,7 @@ token rbsparser_next_token(lexstate *state) { } yy103: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= 'Z') { if (yych <= '/') goto yy104; if (yych <= '9') goto yy103; @@ -993,28 +993,28 @@ token rbsparser_next_token(lexstate *state) { } yy104: #line 136 "src/lexer.re" - { return next_token(state, tAIDENT); } + { return rbs_next_token(state, tAIDENT); } #line 998 "src/lexer.c" yy105: rbs_skip(state); #line 133 "src/lexer.re" - { return next_token(state, tBANGIDENT); } + { return rbs_next_token(state, tBANGIDENT); } #line 1003 "src/lexer.c" yy106: rbs_skip(state); #line 134 "src/lexer.re" - { return next_token(state, tEQIDENT); } + { return rbs_next_token(state, tEQIDENT); } #line 1008 "src/lexer.c" yy107: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '=') goto yy24; #line 47 "src/lexer.re" - { return next_token(state, pAREF_OPR); } + { return rbs_next_token(state, pAREF_OPR); } #line 1015 "src/lexer.c" yy108: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); yy109: if (yych <= '=') { if (yych <= '/') { @@ -1034,11 +1034,11 @@ token rbsparser_next_token(lexstate *state) { } yy110: #line 130 "src/lexer.re" - { return next_token(state, tULLIDENT); } + { return rbs_next_token(state, tULLIDENT); } #line 1039 "src/lexer.c" yy111: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1057,27 +1057,27 @@ token rbsparser_next_token(lexstate *state) { } yy112: #line 131 "src/lexer.re" - { return next_token(state, tULIDENT); } + { return rbs_next_token(state, tULIDENT); } #line 1062 "src/lexer.c" yy113: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 't') goto yy165; goto yy109; yy114: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= 0x00000000) goto yy68; if (yych == '`') goto yy166; goto yy114; yy115: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'i') goto yy167; goto yy53; yy116: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1096,47 +1096,47 @@ token rbsparser_next_token(lexstate *state) { } yy117: #line 96 "src/lexer.re" - { return next_token(state, kAS); } + { return rbs_next_token(state, kAS); } #line 1101 "src/lexer.c" yy118: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 't') goto yy168; goto yy53; yy119: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'o') goto yy169; if (yych == 't') goto yy170; goto yy53; yy120: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'a') goto yy172; goto yy53; yy121: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'f') goto yy173; goto yy53; yy122: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'd') goto yy175; goto yy53; yy123: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 't') goto yy177; goto yy53; yy124: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'l') goto yy178; goto yy53; yy125: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '^') { if (yych <= '9') { if (yych == '!') goto yy105; @@ -1166,78 +1166,78 @@ token rbsparser_next_token(lexstate *state) { } yy126: #line 77 "src/lexer.re" - { return next_token(state, kIN); } + { return rbs_next_token(state, kIN); } #line 1171 "src/lexer.c" yy127: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'd') goto yy182; goto yy53; yy128: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'l') goto yy183; goto yy53; yy129: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 't') goto yy185; goto yy53; yy130: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy187; if (yych == 'i') goto yy188; goto yy53; yy131: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'b') goto yy189; goto yy53; yy132: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'l') goto yy190; goto yy53; yy133: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'n') goto yy191; goto yy53; yy134: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'p') goto yy192; goto yy53; yy135: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'u') goto yy194; goto yy53; yy136: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'p') goto yy195; goto yy53; yy137: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'c') goto yy196; if (yych == 't') goto yy197; goto yy53; yy138: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy198; goto yy53; yy139: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'i') goto yy200; goto yy53; yy140: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy201; @@ -1250,7 +1250,7 @@ token rbsparser_next_token(lexstate *state) { } yy141: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '/') goto yy68; if (yych <= '9') goto yy66; if (yych <= '`') goto yy68; @@ -1258,31 +1258,31 @@ token rbsparser_next_token(lexstate *state) { goto yy68; yy142: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= 0x00000000) goto yy68; if (yych == ')') goto yy202; goto yy142; yy143: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= 0x00000000) goto yy68; if (yych == '>') goto yy203; goto yy143; yy144: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= 0x00000000) goto yy68; if (yych == ']') goto yy204; goto yy144; yy145: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= 0x00000000) goto yy68; if (yych == '}') goto yy205; goto yy145; yy146: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= 0x00000000) goto yy68; if (yych == '|') goto yy206; goto yy146; @@ -1290,7 +1290,7 @@ token rbsparser_next_token(lexstate *state) { yyaccept = 5; rbs_skip(state); backup = *state; - yych = peek(state); + yych = rbs_peek(state); if (yych <= '\'') { if (yych <= 0x00000000) goto yy78; if (yych <= '&') goto yy75; @@ -1302,22 +1302,22 @@ token rbsparser_next_token(lexstate *state) { yy148: rbs_skip(state); #line 38 "src/lexer.re" - { return next_token(state, pDOT3); } + { return rbs_next_token(state, pDOT3); } #line 1307 "src/lexer.c" yy149: rbs_skip(state); #line 108 "src/lexer.re" - { return next_token(state, tDQSYMBOL); } + { return rbs_next_token(state, tDQSYMBOL); } #line 1312 "src/lexer.c" yy150: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'u') goto yy207; if (yych == 'x') goto yy208; goto yy85; yy151: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= ',') { if (yych <= '\f') { if (yych <= 0x00000000) goto yy152; @@ -1346,7 +1346,7 @@ token rbsparser_next_token(lexstate *state) { } yy152: #line 125 "src/lexer.re" - { return next_token(state, tSYMBOL); } + { return rbs_next_token(state, tSYMBOL); } #line 1351 "src/lexer.c" yy153: rbs_skip(state); @@ -1355,11 +1355,11 @@ token rbsparser_next_token(lexstate *state) { rbs_skip(state); yy155: #line 109 "src/lexer.re" - { return next_token(state, tSQSYMBOL); } + { return rbs_next_token(state, tSQSYMBOL); } #line 1360 "src/lexer.c" yy156: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '\'') { if (yych <= 0x00000000) goto yy68; if (yych <= '&') goto yy88; @@ -1370,17 +1370,17 @@ token rbsparser_next_token(lexstate *state) { } yy157: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '>') goto yy87; goto yy84; yy158: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '=') goto yy87; goto yy84; yy159: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '^') { if (yych <= '@') goto yy68; if (yych <= 'Z') goto yy210; @@ -1392,7 +1392,7 @@ token rbsparser_next_token(lexstate *state) { } yy160: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '>') { if (yych <= '/') { if (yych == '!') goto yy212; @@ -1412,14 +1412,14 @@ token rbsparser_next_token(lexstate *state) { } yy161: #line 123 "src/lexer.re" - { return next_token(state, tSYMBOL); } + { return rbs_next_token(state, tSYMBOL); } #line 1417 "src/lexer.c" yy162: rbs_skip(state); goto yy97; yy163: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= 'Z') { if (yych <= '/') goto yy164; if (yych <= '9') goto yy163; @@ -1434,36 +1434,36 @@ token rbsparser_next_token(lexstate *state) { } yy164: #line 137 "src/lexer.re" - { return next_token(state, tA2IDENT); } + { return rbs_next_token(state, tA2IDENT); } #line 1439 "src/lexer.c" yy165: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'o') goto yy213; goto yy109; yy166: rbs_skip(state); #line 40 "src/lexer.re" - { return next_token(state, tQIDENT); } + { return rbs_next_token(state, tQIDENT); } #line 1449 "src/lexer.c" yy167: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'a') goto yy214; goto yy53; yy168: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'r') goto yy215; goto yy53; yy169: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'l') goto yy216; goto yy53; yy170: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1482,16 +1482,16 @@ token rbsparser_next_token(lexstate *state) { } yy171: #line 71 "src/lexer.re" - { return next_token(state, kBOT); } + { return rbs_next_token(state, kBOT); } #line 1487 "src/lexer.c" yy172: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 's') goto yy218; goto yy53; yy173: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1510,11 +1510,11 @@ token rbsparser_next_token(lexstate *state) { } yy174: #line 73 "src/lexer.re" - { return next_token(state, kDEF); } + { return rbs_next_token(state, kDEF); } #line 1515 "src/lexer.c" yy175: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1533,41 +1533,41 @@ token rbsparser_next_token(lexstate *state) { } yy176: #line 74 "src/lexer.re" - { return next_token(state, kEND); } + { return rbs_next_token(state, kEND); } #line 1538 "src/lexer.c" yy177: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy219; goto yy53; yy178: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 's') goto yy220; goto yy53; yy179: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'l') goto yy221; goto yy53; yy180: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 't') goto yy222; goto yy53; yy181: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy223; goto yy53; yy182: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'u') goto yy224; goto yy53; yy183: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1586,11 +1586,11 @@ token rbsparser_next_token(lexstate *state) { } yy184: #line 82 "src/lexer.re" - { return next_token(state, kNIL); } + { return rbs_next_token(state, kNIL); } #line 1591 "src/lexer.c" yy185: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1609,36 +1609,36 @@ token rbsparser_next_token(lexstate *state) { } yy186: #line 83 "src/lexer.re" - { return next_token(state, kOUT); } + { return rbs_next_token(state, kOUT); } #line 1614 "src/lexer.c" yy187: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'p') goto yy225; goto yy53; yy188: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'v') goto yy226; goto yy53; yy189: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'l') goto yy227; goto yy53; yy190: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'f') goto yy228; goto yy53; yy191: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'g') goto yy230; goto yy53; yy192: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1657,31 +1657,31 @@ token rbsparser_next_token(lexstate *state) { } yy193: #line 89 "src/lexer.re" - { return next_token(state, kTOP); } + { return rbs_next_token(state, kTOP); } #line 1662 "src/lexer.c" yy194: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy231; goto yy53; yy195: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy233; goto yy53; yy196: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'h') goto yy235; goto yy53; yy197: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'y') goto yy236; goto yy53; yy198: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1700,16 +1700,16 @@ token rbsparser_next_token(lexstate *state) { } yy199: #line 95 "src/lexer.re" - { return next_token(state, kUSE); } + { return rbs_next_token(state, kUSE); } #line 1705 "src/lexer.c" yy200: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'd') goto yy237; goto yy53; yy201: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy239; @@ -1723,31 +1723,31 @@ token rbsparser_next_token(lexstate *state) { yy202: rbs_skip(state); #line 54 "src/lexer.re" - { return next_token(state, tANNOTATION); } + { return rbs_next_token(state, tANNOTATION); } #line 1728 "src/lexer.c" yy203: rbs_skip(state); #line 57 "src/lexer.re" - { return next_token(state, tANNOTATION); } + { return rbs_next_token(state, tANNOTATION); } #line 1733 "src/lexer.c" yy204: rbs_skip(state); #line 55 "src/lexer.re" - { return next_token(state, tANNOTATION); } + { return rbs_next_token(state, tANNOTATION); } #line 1738 "src/lexer.c" yy205: rbs_skip(state); #line 53 "src/lexer.re" - { return next_token(state, tANNOTATION); } + { return rbs_next_token(state, tANNOTATION); } #line 1743 "src/lexer.c" yy206: rbs_skip(state); #line 56 "src/lexer.re" - { return next_token(state, tANNOTATION); } + { return rbs_next_token(state, tANNOTATION); } #line 1748 "src/lexer.c" yy207: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy240; @@ -1760,7 +1760,7 @@ token rbsparser_next_token(lexstate *state) { } yy208: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '/') goto yy68; if (yych <= '9') goto yy85; if (yych <= '`') goto yy68; @@ -1770,7 +1770,7 @@ token rbsparser_next_token(lexstate *state) { yyaccept = 6; rbs_skip(state); backup = *state; - yych = peek(state); + yych = rbs_peek(state); if (yych <= '\'') { if (yych <= 0x00000000) goto yy155; if (yych <= '&') goto yy88; @@ -1781,7 +1781,7 @@ token rbsparser_next_token(lexstate *state) { } yy210: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '>') { if (yych <= '/') { if (yych == '!') goto yy241; @@ -1801,29 +1801,29 @@ token rbsparser_next_token(lexstate *state) { } yy211: #line 124 "src/lexer.re" - { return next_token(state, tSYMBOL); } + { return rbs_next_token(state, tSYMBOL); } #line 1806 "src/lexer.c" yy212: rbs_skip(state); goto yy161; yy213: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'd') goto yy242; goto yy109; yy214: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 's') goto yy243; goto yy53; yy215: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '_') goto yy245; goto yy53; yy216: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1842,61 +1842,61 @@ token rbsparser_next_token(lexstate *state) { } yy217: #line 70 "src/lexer.re" - { return next_token(state, kBOOL); } + { return rbs_next_token(state, kBOOL); } #line 1847 "src/lexer.c" yy218: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 's') goto yy246; goto yy53; yy219: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'n') goto yy248; goto yy53; yy220: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy249; goto yy53; yy221: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'u') goto yy251; goto yy53; yy222: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'a') goto yy252; goto yy53; yy223: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'r') goto yy253; goto yy53; yy224: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'l') goto yy254; goto yy53; yy225: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy255; goto yy53; yy226: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'a') goto yy256; goto yy53; yy227: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'i') goto yy257; goto yy53; yy228: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1915,16 +1915,16 @@ token rbsparser_next_token(lexstate *state) { } yy229: #line 87 "src/lexer.re" - { return next_token(state, kSELF); } + { return rbs_next_token(state, kSELF); } #line 1920 "src/lexer.c" yy230: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'l') goto yy258; goto yy53; yy231: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1943,11 +1943,11 @@ token rbsparser_next_token(lexstate *state) { } yy232: #line 90 "src/lexer.re" - { return next_token(state, kTRUE); } + { return rbs_next_token(state, kTRUE); } #line 1948 "src/lexer.c" yy233: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1966,21 +1966,21 @@ token rbsparser_next_token(lexstate *state) { } yy234: #line 91 "src/lexer.re" - { return next_token(state, kTYPE); } + { return rbs_next_token(state, kTYPE); } #line 1971 "src/lexer.c" yy235: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy259; goto yy53; yy236: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'p') goto yy260; goto yy53; yy237: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1999,11 +1999,11 @@ token rbsparser_next_token(lexstate *state) { } yy238: #line 94 "src/lexer.re" - { return next_token(state, kVOID); } + { return rbs_next_token(state, kVOID); } #line 2004 "src/lexer.c" yy239: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy261; @@ -2016,7 +2016,7 @@ token rbsparser_next_token(lexstate *state) { } yy240: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy262; @@ -2032,12 +2032,12 @@ token rbsparser_next_token(lexstate *state) { goto yy211; yy242: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'o') goto yy263; goto yy109; yy243: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2056,11 +2056,11 @@ token rbsparser_next_token(lexstate *state) { } yy244: #line 66 "src/lexer.re" - { return next_token(state, kALIAS); } + { return rbs_next_token(state, kALIAS); } #line 2061 "src/lexer.c" yy245: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= 'q') { if (yych == 'a') goto yy264; goto yy53; @@ -2071,7 +2071,7 @@ token rbsparser_next_token(lexstate *state) { } yy246: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2090,16 +2090,16 @@ token rbsparser_next_token(lexstate *state) { } yy247: #line 72 "src/lexer.re" - { return next_token(state, kCLASS); } + { return rbs_next_token(state, kCLASS); } #line 2095 "src/lexer.c" yy248: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'd') goto yy267; goto yy53; yy249: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2118,61 +2118,61 @@ token rbsparser_next_token(lexstate *state) { } yy250: #line 76 "src/lexer.re" - { return next_token(state, kFALSE); } + { return rbs_next_token(state, kFALSE); } #line 2123 "src/lexer.c" yy251: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'd') goto yy269; goto yy53; yy252: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'n') goto yy270; goto yy53; yy253: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'f') goto yy271; goto yy53; yy254: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy272; goto yy53; yy255: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'n') goto yy274; goto yy53; yy256: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 't') goto yy275; goto yy53; yy257: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'c') goto yy276; goto yy53; yy258: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy278; goto yy53; yy259: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'c') goto yy279; goto yy53; yy260: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy280; goto yy53; yy261: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy66; @@ -2185,7 +2185,7 @@ token rbsparser_next_token(lexstate *state) { } yy262: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy281; @@ -2198,27 +2198,27 @@ token rbsparser_next_token(lexstate *state) { } yy263: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '_') goto yy282; goto yy109; yy264: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'c') goto yy283; goto yy53; yy265: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy284; goto yy53; yy266: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'r') goto yy285; goto yy53; yy267: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2237,26 +2237,26 @@ token rbsparser_next_token(lexstate *state) { } yy268: #line 75 "src/lexer.re" - { return next_token(state, kEXTEND); } + { return rbs_next_token(state, kEXTEND); } #line 2242 "src/lexer.c" yy269: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy286; goto yy53; yy270: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'c') goto yy288; goto yy53; yy271: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'a') goto yy289; goto yy53; yy272: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2275,21 +2275,21 @@ token rbsparser_next_token(lexstate *state) { } yy273: #line 81 "src/lexer.re" - { return next_token(state, kMODULE); } + { return rbs_next_token(state, kMODULE); } #line 2280 "src/lexer.c" yy274: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'd') goto yy290; goto yy53; yy275: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy292; goto yy53; yy276: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2308,26 +2308,26 @@ token rbsparser_next_token(lexstate *state) { } yy277: #line 86 "src/lexer.re" - { return next_token(state, kPUBLIC); } + { return rbs_next_token(state, kPUBLIC); } #line 2313 "src/lexer.c" yy278: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 't') goto yy294; goto yy53; yy279: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'k') goto yy295; goto yy53; yy280: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'd') goto yy296; goto yy53; yy281: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy85; @@ -2340,27 +2340,27 @@ token rbsparser_next_token(lexstate *state) { } yy282: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == '_') goto yy298; goto yy109; yy283: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'c') goto yy300; goto yy53; yy284: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'a') goto yy301; goto yy53; yy285: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'i') goto yy302; goto yy53; yy286: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2379,21 +2379,21 @@ token rbsparser_next_token(lexstate *state) { } yy287: #line 78 "src/lexer.re" - { return next_token(state, kINCLUDE); } + { return rbs_next_token(state, kINCLUDE); } #line 2384 "src/lexer.c" yy288: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy303; goto yy53; yy289: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'c') goto yy305; goto yy53; yy290: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2412,11 +2412,11 @@ token rbsparser_next_token(lexstate *state) { } yy291: #line 84 "src/lexer.re" - { return next_token(state, kPREPEND); } + { return rbs_next_token(state, kPREPEND); } #line 2417 "src/lexer.c" yy292: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2435,21 +2435,21 @@ token rbsparser_next_token(lexstate *state) { } yy293: #line 85 "src/lexer.re" - { return next_token(state, kPRIVATE); } + { return rbs_next_token(state, kPRIVATE); } #line 2440 "src/lexer.c" yy294: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'o') goto yy306; goto yy53; yy295: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy307; goto yy53; yy296: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2468,11 +2468,11 @@ token rbsparser_next_token(lexstate *state) { } yy297: #line 93 "src/lexer.re" - { return next_token(state, kUNTYPED); } + { return rbs_next_token(state, kUNTYPED); } #line 2473 "src/lexer.c" yy298: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2491,26 +2491,26 @@ token rbsparser_next_token(lexstate *state) { } yy299: #line 97 "src/lexer.re" - { return next_token(state, k__TODO__); } + { return rbs_next_token(state, k__TODO__); } #line 2496 "src/lexer.c" yy300: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy308; goto yy53; yy301: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'd') goto yy309; goto yy53; yy302: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 't') goto yy310; goto yy53; yy303: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2529,41 +2529,41 @@ token rbsparser_next_token(lexstate *state) { } yy304: #line 79 "src/lexer.re" - { return next_token(state, kINSTANCE); } + { return rbs_next_token(state, kINSTANCE); } #line 2534 "src/lexer.c" yy305: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy311; goto yy53; yy306: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'n') goto yy313; goto yy53; yy307: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'd') goto yy315; goto yy53; yy308: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 's') goto yy317; goto yy53; yy309: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy318; goto yy53; yy310: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'e') goto yy319; goto yy53; yy311: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2582,11 +2582,11 @@ token rbsparser_next_token(lexstate *state) { } yy312: #line 80 "src/lexer.re" - { return next_token(state, kINTERFACE); } + { return rbs_next_token(state, kINTERFACE); } #line 2587 "src/lexer.c" yy313: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2605,11 +2605,11 @@ token rbsparser_next_token(lexstate *state) { } yy314: #line 88 "src/lexer.re" - { return next_token(state, kSINGLETON); } + { return rbs_next_token(state, kSINGLETON); } #line 2610 "src/lexer.c" yy315: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2628,31 +2628,31 @@ token rbsparser_next_token(lexstate *state) { } yy316: #line 92 "src/lexer.re" - { return next_token(state, kUNCHECKED); } + { return rbs_next_token(state, kUNCHECKED); } #line 2633 "src/lexer.c" yy317: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 's') goto yy320; goto yy53; yy318: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'r') goto yy321; goto yy53; yy319: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'r') goto yy323; goto yy53; yy320: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych == 'o') goto yy325; goto yy53; yy321: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2671,11 +2671,11 @@ token rbsparser_next_token(lexstate *state) { } yy322: #line 68 "src/lexer.re" - { return next_token(state, kATTRREADER); } + { return rbs_next_token(state, kATTRREADER); } #line 2676 "src/lexer.c" yy323: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2694,14 +2694,14 @@ token rbsparser_next_token(lexstate *state) { } yy324: #line 69 "src/lexer.re" - { return next_token(state, kATTRWRITER); } + { return rbs_next_token(state, kATTRWRITER); } #line 2699 "src/lexer.c" yy325: rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych != 'r') goto yy53; rbs_skip(state); - yych = peek(state); + yych = rbs_peek(state); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2720,7 +2720,7 @@ token rbsparser_next_token(lexstate *state) { } yy326: #line 67 "src/lexer.re" - { return next_token(state, kATTRACCESSOR); } + { return rbs_next_token(state, kATTRACCESSOR); } #line 2725 "src/lexer.c" } #line 146 "src/lexer.re" diff --git a/src/lexer.re b/src/lexer.re index df9487b2c..7de2a7101 100644 --- a/src/lexer.re +++ b/src/lexer.re @@ -10,7 +10,7 @@ token rbsparser_next_token(lexstate *state) { re2c:api:style = free-form; re2c:flags:input = custom; re2c:define:YYCTYPE = "unsigned int"; - re2c:define:YYPEEK = "peek(state)"; + re2c:define:YYPEEK = "rbs_peek(state)"; re2c:define:YYSKIP = "rbs_skip(state);"; re2c:define:YYBACKUP = "backup = *state;"; re2c:define:YYRESTORE = "*state = backup;"; @@ -21,80 +21,80 @@ token rbsparser_next_token(lexstate *state) { operator = "/" | "~" | "[]=" | "!" | "!=" | "!~" | "-" | "-@" | "+" | "+@" | "==" | "===" | "=~" | "<<" | "<=" | "<=>" | ">" | ">=" | ">>" | "%"; - "(" { return next_token(state, pLPAREN); } - ")" { return next_token(state, pRPAREN); } - "[" { return next_token(state, pLBRACKET); } - "]" { return next_token(state, pRBRACKET); } - "{" { return next_token(state, pLBRACE); } - "}" { return next_token(state, pRBRACE); } - "," { return next_token(state, pCOMMA); } - "|" { return next_token(state, pBAR); } - "^" { return next_token(state, pHAT); } - "&" { return next_token(state, pAMP); } - "?" { return next_token(state, pQUESTION); } - "*" { return next_token(state, pSTAR); } - "**" { return next_token(state, pSTAR2); } - "." { return next_token(state, pDOT); } - "..." { return next_token(state, pDOT3); } - "`" { return next_token(state, tOPERATOR); } - "`" [^ :\x00] [^`\x00]* "`" { return next_token(state, tQIDENT); } - "->" { return next_token(state, pARROW); } - "=>" { return next_token(state, pFATARROW); } - "=" { return next_token(state, pEQ); } - ":" { return next_token(state, pCOLON); } - "::" { return next_token(state, pCOLON2); } - "<" { return next_token(state, pLT); } - "[]" { return next_token(state, pAREF_OPR); } - operator { return next_token(state, tOPERATOR); } + "(" { return rbs_next_token(state, pLPAREN); } + ")" { return rbs_next_token(state, pRPAREN); } + "[" { return rbs_next_token(state, pLBRACKET); } + "]" { return rbs_next_token(state, pRBRACKET); } + "{" { return rbs_next_token(state, pLBRACE); } + "}" { return rbs_next_token(state, pRBRACE); } + "," { return rbs_next_token(state, pCOMMA); } + "|" { return rbs_next_token(state, pBAR); } + "^" { return rbs_next_token(state, pHAT); } + "&" { return rbs_next_token(state, pAMP); } + "?" { return rbs_next_token(state, pQUESTION); } + "*" { return rbs_next_token(state, pSTAR); } + "**" { return rbs_next_token(state, pSTAR2); } + "." { return rbs_next_token(state, pDOT); } + "..." { return rbs_next_token(state, pDOT3); } + "`" { return rbs_next_token(state, tOPERATOR); } + "`" [^ :\x00] [^`\x00]* "`" { return rbs_next_token(state, tQIDENT); } + "->" { return rbs_next_token(state, pARROW); } + "=>" { return rbs_next_token(state, pFATARROW); } + "=" { return rbs_next_token(state, pEQ); } + ":" { return rbs_next_token(state, pCOLON); } + "::" { return rbs_next_token(state, pCOLON2); } + "<" { return rbs_next_token(state, pLT); } + "[]" { return rbs_next_token(state, pAREF_OPR); } + operator { return rbs_next_token(state, tOPERATOR); } number = [0-9] [0-9_]*; - ("-"|"+")? number { return next_token(state, tINTEGER); } + ("-"|"+")? number { return rbs_next_token(state, tINTEGER); } - "%a{" [^}\x00]* "}" { return next_token(state, tANNOTATION); } - "%a(" [^)\x00]* ")" { return next_token(state, tANNOTATION); } - "%a[" [^\]\x00]* "]" { return next_token(state, tANNOTATION); } - "%a|" [^|\x00]* "|" { return next_token(state, tANNOTATION); } - "%a<" [^>\x00]* ">" { return next_token(state, tANNOTATION); } + "%a{" [^}\x00]* "}" { return rbs_next_token(state, tANNOTATION); } + "%a(" [^)\x00]* ")" { return rbs_next_token(state, tANNOTATION); } + "%a[" [^\]\x00]* "]" { return rbs_next_token(state, tANNOTATION); } + "%a|" [^|\x00]* "|" { return rbs_next_token(state, tANNOTATION); } + "%a<" [^>\x00]* ">" { return rbs_next_token(state, tANNOTATION); } "#" (. \ [\x00])* { - return next_token( + return rbs_next_token( state, state->first_token_of_line ? tLINECOMMENT : tCOMMENT ); } - "alias" { return next_token(state, kALIAS); } - "attr_accessor" { return next_token(state, kATTRACCESSOR); } - "attr_reader" { return next_token(state, kATTRREADER); } - "attr_writer" { return next_token(state, kATTRWRITER); } - "bool" { return next_token(state, kBOOL); } - "bot" { return next_token(state, kBOT); } - "class" { return next_token(state, kCLASS); } - "def" { return next_token(state, kDEF); } - "end" { return next_token(state, kEND); } - "extend" { return next_token(state, kEXTEND); } - "false" { return next_token(state, kFALSE); } - "in" { return next_token(state, kIN); } - "include" { return next_token(state, kINCLUDE); } - "instance" { return next_token(state, kINSTANCE); } - "interface" { return next_token(state, kINTERFACE); } - "module" { return next_token(state, kMODULE); } - "nil" { return next_token(state, kNIL); } - "out" { return next_token(state, kOUT); } - "prepend" { return next_token(state, kPREPEND); } - "private" { return next_token(state, kPRIVATE); } - "public" { return next_token(state, kPUBLIC); } - "self" { return next_token(state, kSELF); } - "singleton" { return next_token(state, kSINGLETON); } - "top" { return next_token(state, kTOP); } - "true" { return next_token(state, kTRUE); } - "type" { return next_token(state, kTYPE); } - "unchecked" { return next_token(state, kUNCHECKED); } - "untyped" { return next_token(state, kUNTYPED); } - "void" { return next_token(state, kVOID); } - "use" { return next_token(state, kUSE); } - "as" { return next_token(state, kAS); } - "__todo__" { return next_token(state, k__TODO__); } + "alias" { return rbs_next_token(state, kALIAS); } + "attr_accessor" { return rbs_next_token(state, kATTRACCESSOR); } + "attr_reader" { return rbs_next_token(state, kATTRREADER); } + "attr_writer" { return rbs_next_token(state, kATTRWRITER); } + "bool" { return rbs_next_token(state, kBOOL); } + "bot" { return rbs_next_token(state, kBOT); } + "class" { return rbs_next_token(state, kCLASS); } + "def" { return rbs_next_token(state, kDEF); } + "end" { return rbs_next_token(state, kEND); } + "extend" { return rbs_next_token(state, kEXTEND); } + "false" { return rbs_next_token(state, kFALSE); } + "in" { return rbs_next_token(state, kIN); } + "include" { return rbs_next_token(state, kINCLUDE); } + "instance" { return rbs_next_token(state, kINSTANCE); } + "interface" { return rbs_next_token(state, kINTERFACE); } + "module" { return rbs_next_token(state, kMODULE); } + "nil" { return rbs_next_token(state, kNIL); } + "out" { return rbs_next_token(state, kOUT); } + "prepend" { return rbs_next_token(state, kPREPEND); } + "private" { return rbs_next_token(state, kPRIVATE); } + "public" { return rbs_next_token(state, kPUBLIC); } + "self" { return rbs_next_token(state, kSELF); } + "singleton" { return rbs_next_token(state, kSINGLETON); } + "top" { return rbs_next_token(state, kTOP); } + "true" { return rbs_next_token(state, kTRUE); } + "type" { return rbs_next_token(state, kTYPE); } + "unchecked" { return rbs_next_token(state, kUNCHECKED); } + "untyped" { return rbs_next_token(state, kUNTYPED); } + "void" { return rbs_next_token(state, kVOID); } + "use" { return rbs_next_token(state, kUSE); } + "as" { return rbs_next_token(state, kAS); } + "__todo__" { return rbs_next_token(state, k__TODO__); } unicode_char = "\\u" [0-9a-fA-F]{4}; oct_char = "\\x" [0-9a-f]{1,2}; @@ -103,10 +103,10 @@ token rbsparser_next_token(lexstate *state) { dqstring = ["] (unicode_char | oct_char | hex_char | "\\" [^xu] | [^\\"\x00])* ["]; sqstring = ['] ("\\"['\\] | [^'\x00])* [']; - dqstring { return next_token(state, tDQSTRING); } - sqstring { return next_token(state, tSQSTRING); } - ":" dqstring { return next_token(state, tDQSYMBOL); } - ":" sqstring { return next_token(state, tSQSYMBOL); } + dqstring { return rbs_next_token(state, tDQSTRING); } + sqstring { return rbs_next_token(state, tSQSTRING); } + ":" dqstring { return rbs_next_token(state, tDQSYMBOL); } + ":" sqstring { return rbs_next_token(state, tSQSYMBOL); } identifier = [a-zA-Z_] word* [!?=]?; symbol_opr = ":|" | ":&" | ":/" | ":%" | ":~" | ":`" | ":^" @@ -119,29 +119,29 @@ token rbsparser_next_token(lexstate *state) { | [~*$?!@\\/;,.=:<>"&'`+] | [^ \t\r\n:;=.,!"$%&()-+~|\\'[\]{}*/<>^\x00]+; - ":" identifier { return next_token(state, tSYMBOL); } - ":@" identifier { return next_token(state, tSYMBOL); } - ":@@" identifier { return next_token(state, tSYMBOL); } - ":$" global_ident { return next_token(state, tSYMBOL); } - symbol_opr { return next_token(state, tSYMBOL); } + ":" identifier { return rbs_next_token(state, tSYMBOL); } + ":@" identifier { return rbs_next_token(state, tSYMBOL); } + ":@@" identifier { return rbs_next_token(state, tSYMBOL); } + ":$" global_ident { return rbs_next_token(state, tSYMBOL); } + symbol_opr { return rbs_next_token(state, tSYMBOL); } - [a-z] word* { return next_token(state, tLIDENT); } - [A-Z] word* { return next_token(state, tUIDENT); } - "_" [a-z0-9_] word* { return next_token(state, tULLIDENT); } - "_" [A-Z] word* { return next_token(state, tULIDENT); } - "_" { return next_token(state, tULLIDENT); } - [a-zA-Z_] word* "!" { return next_token(state, tBANGIDENT); } - [a-zA-Z_] word* "=" { return next_token(state, tEQIDENT); } + [a-z] word* { return rbs_next_token(state, tLIDENT); } + [A-Z] word* { return rbs_next_token(state, tUIDENT); } + "_" [a-z0-9_] word* { return rbs_next_token(state, tULLIDENT); } + "_" [A-Z] word* { return rbs_next_token(state, tULIDENT); } + "_" { return rbs_next_token(state, tULLIDENT); } + [a-zA-Z_] word* "!" { return rbs_next_token(state, tBANGIDENT); } + [a-zA-Z_] word* "=" { return rbs_next_token(state, tEQIDENT); } - "@" [a-zA-Z_] word* { return next_token(state, tAIDENT); } - "@@" [a-zA-Z_] word* { return next_token(state, tA2IDENT); } + "@" [a-zA-Z_] word* { return rbs_next_token(state, tAIDENT); } + "@@" [a-zA-Z_] word* { return rbs_next_token(state, tA2IDENT); } - "$" global_ident { return next_token(state, tGIDENT); } + "$" global_ident { return rbs_next_token(state, tGIDENT); } skip = ([ \t]+|[\r\n]); - skip { return next_token(state, tTRIVIA); } - "\x00" { return next_eof_token(state); } - * { return next_token(state, ErrorToken); } + skip { return rbs_next_token(state, tTRIVIA); } + "\x00" { return rbs_next_eof_token(state); } + * { return rbs_next_token(state, ErrorToken); } */ } diff --git a/src/lexstate.c b/src/lexstate.c index ef65c0e84..7bdbd32ed 100644 --- a/src/lexstate.c +++ b/src/lexstate.c @@ -93,19 +93,19 @@ token NullToken = { .type = NullType, .range = {} }; position NullPosition = { -1, -1, -1, -1 }; range NULL_RANGE = { { -1, -1, -1, -1 }, { -1, -1, -1, -1 } }; -const char *token_type_str(enum TokenType type) { +const char *token_type_str(enum RBSTokenType type) { return RBS_TOKENTYPE_NAMES[type]; } -int token_chars(token tok) { +int rbs_token_chars(token tok) { return tok.range.end.char_pos - tok.range.start.char_pos; } -int token_bytes(token tok) { - return RANGE_BYTES(tok.range); +int rbs_token_bytes(token tok) { + return RBS_RANGE_BYTES(tok.range); } -unsigned int peek(lexstate *state) { +unsigned int rbs_peek(lexstate *state) { if (state->current.char_pos == state->end_pos) { state->last_char = '\0'; return 0; @@ -114,13 +114,13 @@ unsigned int peek(lexstate *state) { state->string.start + state->current.byte_pos, state->string.end ); - unsigned int c = utf8_to_codepoint(str); + unsigned int c = rbs_utf8_to_codepoint(str); state->last_char = c; return c; } } -token next_token(lexstate *state, enum TokenType type) { +token rbs_next_token(lexstate *state, enum RBSTokenType type) { token t; t.type = type; @@ -134,7 +134,7 @@ token next_token(lexstate *state, enum TokenType type) { return t; } -token next_eof_token(lexstate *state) { +token rbs_next_eof_token(lexstate *state) { if ((size_t) state->current.byte_pos == rbs_string_len(state->string) + 1) { // End of String token t; @@ -146,13 +146,13 @@ token next_eof_token(lexstate *state) { return t; } else { // NULL byte in the middle of the string - return next_token(state, pEOF); + return rbs_next_token(state, pEOF); } } void rbs_skip(lexstate *state) { if (!state->last_char) { - peek(state); + rbs_peek(state); } size_t byte_len; @@ -176,13 +176,13 @@ void rbs_skip(lexstate *state) { } } -void skipn(lexstate *state, size_t size) { +void rbs_skipn(lexstate *state, size_t size) { for (size_t i = 0; i < size; i ++) { - peek(state); + rbs_peek(state); rbs_skip(state); } } -char *peek_token(lexstate *state, token tok) { +char *rbs_peek_token(lexstate *state, token tok) { return (char *) state->string.start + tok.range.start.byte_pos; } diff --git a/src/parser.c b/src/parser.c index 079f32ac3..c973634a5 100644 --- a/src/parser.c +++ b/src/parser.c @@ -22,8 +22,8 @@ #define INTERN_TOKEN(parserstate, tok) \ rbs_constant_pool_insert_shared_with_encoding( \ &parserstate->constant_pool, \ - (const uint8_t *) peek_token(parserstate->lexstate, tok),\ - token_bytes(tok), \ + (const uint8_t *) rbs_peek_token(parserstate->lexstate, tok),\ + rbs_token_bytes(tok), \ (void *) parserstate->lexstate->encoding \ ) @@ -250,7 +250,7 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb | {} type `,` ... `,` eol */ NODISCARD -static bool parse_type_list(parserstate *state, enum TokenType eol, rbs_node_list_t *types) { +static bool parse_type_list(parserstate *state, enum RBSTokenType eol, rbs_node_list_t *types) { while (true) { rbs_node_t *type; CHECK_PARSE(parse_type(state, &type)); @@ -275,7 +275,7 @@ static bool parse_type_list(parserstate *state, enum TokenType eol, rbs_node_lis return true; } -static bool is_keyword_token(enum TokenType type) { +static bool is_keyword_token(enum RBSTokenType type) { switch (type) { case tLIDENT: @@ -344,7 +344,7 @@ static bool parse_function_param(parserstate *state, rbs_types_function_param_t static rbs_constant_id_t intern_token_start_end(parserstate *state, token start_token, token end_token) { return rbs_constant_pool_insert_shared_with_encoding( &state->constant_pool, - (const uint8_t *) peek_token(state->lexstate, start_token), + (const uint8_t *) rbs_peek_token(state->lexstate, start_token), end_token.range.end.byte_pos - start_token.range.start.byte_pos, state->lexstate->encoding ); @@ -894,7 +894,7 @@ static bool parse_record_attributes(parserstate *state, rbs_hash_t **fields) { NODISCARD static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types_literal_t **symbol) { size_t offset_bytes = state->lexstate->encoding->char_width((const uint8_t *) ":", (size_t) 1); - size_t bytes = token_bytes(state->current_token) - offset_bytes; + size_t bytes = rbs_token_bytes(state->current_token) - offset_bytes; rbs_ast_symbol_t *literal; @@ -903,7 +903,7 @@ static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types case tSYMBOL: { rbs_location_t *symbolLoc = rbs_location_current_token(state); - char *buffer = peek_token(state->lexstate, state->current_token); + char *buffer = rbs_peek_token(state->lexstate, state->current_token); rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared( &state->constant_pool, (const uint8_t *) buffer+offset_bytes, @@ -979,7 +979,7 @@ static bool parse_instance_type(parserstate *state, bool parse_alias, rbs_node_t range type_range = { .start = name_range.start, - .end = nonnull_pos_or(args_range.end, name_range.end), + .end = rbs_nonnull_pos_or(args_range.end, name_range.end), }; rbs_location_t *loc = rbs_location_new(&state->allocator, type_range); @@ -1136,8 +1136,8 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { return true; } case tUIDENT: { - const char *name_str = peek_token(state->lexstate, state->current_token); - size_t name_len = token_bytes(state->current_token); + const char *name_str = rbs_peek_token(state->lexstate, state->current_token); + size_t name_len = rbs_token_bytes(state->current_token); rbs_constant_id_t name = rbs_constant_pool_find(&state->constant_pool, (const uint8_t *) name_str, name_len); @@ -1497,7 +1497,7 @@ static bool parse_type_decl(parserstate *state, position comment_pos, rbs_node_l range decl_range; decl_range.start = state->current_token.range.start; - comment_pos = nonnull_pos_or(comment_pos, decl_range.start); + comment_pos = rbs_nonnull_pos_or(comment_pos, decl_range.start); range keyword_range = state->current_token.range; @@ -1549,7 +1549,7 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati state->lexstate->string.start + rg.start.byte_pos + offset_bytes, state->lexstate->string.end ); - unsigned int open_char = utf8_to_codepoint(str); + unsigned int open_char = rbs_utf8_to_codepoint(str); unsigned int close_char; @@ -1603,7 +1603,7 @@ static bool parse_annotations(parserstate *state, rbs_node_list_t *annotations, if (state->next_token.type == tANNOTATION) { parser_advance(state); - if (null_position_p((*annot_pos))) { + if (rbs_null_position_p((*annot_pos))) { *annot_pos = state->current_token.range.start; } @@ -1751,7 +1751,7 @@ NODISCARD static bool parse_member_def(parserstate *state, bool instance_only, bool accept_overload, position comment_pos, rbs_node_list_t *annotations, rbs_ast_members_methoddefinition_t **method_definition) { range member_range; member_range.start = state->current_token.range.start; - comment_pos = nonnull_pos_or(comment_pos, member_range.start); + comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); @@ -1934,9 +1934,9 @@ NODISCARD static bool parse_mixin_member(parserstate *state, bool from_interface, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **mixin_member) { range member_range; member_range.start = state->current_token.range.start; - comment_pos = nonnull_pos_or(comment_pos, member_range.start); + comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); - enum TokenType type = state->current_token.type; + enum RBSTokenType type = state->current_token.type; range keyword_range = state->current_token.range; bool reset_typevar_scope; @@ -2017,7 +2017,7 @@ static bool parse_alias_member(parserstate *state, bool instance_only, position member_range.start = state->current_token.range.start; range keyword_range = state->current_token.range; - comment_pos = nonnull_pos_or(comment_pos, member_range.start); + comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); rbs_keyword_t *kind; @@ -2073,7 +2073,7 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ range member_range; member_range.start = state->current_token.range.start; - comment_pos = nonnull_pos_or(comment_pos, member_range.start); + comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); switch (state->current_token.type) @@ -2213,7 +2213,7 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs range member_range; member_range.start = state->current_token.range.start; - comment_pos = nonnull_pos_or(comment_pos, member_range.start); + comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); range visibility_range; @@ -2238,7 +2238,7 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs break; } - enum TokenType attr_type = state->current_token.type; + enum RBSTokenType attr_type = state->current_token.type; range keyword_range = state->current_token.range; range kind_range; @@ -2381,7 +2381,7 @@ static bool parse_interface_decl(parserstate *state, position comment_pos, rbs_n range member_range; member_range.start = state->current_token.range.start; - comment_pos = nonnull_pos_or(comment_pos, member_range.start); + comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); range keyword_range = state->current_token.range; @@ -2622,7 +2622,7 @@ NODISCARD static bool parse_module_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **module_decl) { range keyword_range = state->current_token.range; - comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start); + comment_pos = rbs_nonnull_pos_or(comment_pos, state->current_token.range.start); rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); parser_advance(state); @@ -2744,7 +2744,7 @@ NODISCARD static bool parse_class_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **class_decl) { range keyword_range = state->current_token.range; - comment_pos = nonnull_pos_or(comment_pos, state->current_token.range.start); + comment_pos = rbs_nonnull_pos_or(comment_pos, state->current_token.range.start); rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); parser_advance(state); @@ -2919,7 +2919,7 @@ static bool parse_namespace(parserstate *state, range *rg, rbs_namespace_t **nam rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, state->next_token.range); rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->next_token)); rbs_node_list_append(path, (rbs_node_t *)symbol); - if (null_position_p(rg->start)) { + if (rbs_null_position_p(rg->start)) { rg->start = state->next_token.range.start; } rg->end = state->next_token2.range.end; @@ -2955,9 +2955,9 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { case tUIDENT: { parser_advance(state); - enum TokenType ident_type = state->current_token.type; + enum RBSTokenType ident_type = state->current_token.type; - range type_name_range = null_range_p(namespace_range) + range type_name_range = rbs_null_range_p(namespace_range) ? state->current_token.range : (range) { .start = namespace_range.start, .end = state->current_token.range.end }; diff --git a/src/parserstate.c b/src/parserstate.c index cc8c185de..4599cc16c 100644 --- a/src/parserstate.c +++ b/src/parserstate.c @@ -144,7 +144,7 @@ void parser_advance(parserstate *state) { * * @returns true if token advances, false otherwise. **/ -bool parser_advance_if(parserstate *state, enum TokenType type) { +bool parser_advance_if(parserstate *state, enum RBSTokenType type) { if (state->next_token.type == type) { parser_advance(state); return true; @@ -153,7 +153,7 @@ bool parser_advance_if(parserstate *state, enum TokenType type) { } } -void print_token(token tok) { +void rbs_print_token(token tok) { printf( "%s char=%d...%d\n", token_type_str(tok.type), @@ -185,13 +185,13 @@ static rbs_ast_comment_t *parse_comment_lines(parserstate *state, comment *com) token tok = com->tokens[i]; const char *comment_start = state->lexstate->string.start + tok.range.start.byte_pos + hash_bytes; - size_t comment_bytes = RANGE_BYTES(tok.range) - hash_bytes; + size_t comment_bytes = RBS_RANGE_BYTES(tok.range) - hash_bytes; rbs_string_t str = rbs_string_new( comment_start, state->lexstate->string.end ); - unsigned char c = utf8_to_codepoint(str); + unsigned char c = rbs_utf8_to_codepoint(str); if (c == ' ') { comment_start += space_bytes; @@ -298,7 +298,7 @@ lexstate *alloc_lexer(rbs_allocator_t *allocator, rbs_string_t string, const rbs .encoding = encoding, }; - skipn(lexer, start_pos); + rbs_skipn(lexer, start_pos); lexer->start = lexer->current; lexer->first_token_of_line = lexer->current.column == 0; diff --git a/src/rbs_unescape.c b/src/rbs_unescape.c index ed53c3017..589aa4fa5 100644 --- a/src/rbs_unescape.c +++ b/src/rbs_unescape.c @@ -107,7 +107,7 @@ rbs_string_t unescape_string(rbs_allocator_t *allocator, const rbs_string_t stri } rbs_string_t rbs_unquote_string(rbs_allocator_t *allocator, rbs_string_t input) { - unsigned int first_char = utf8_to_codepoint(input); + unsigned int first_char = rbs_utf8_to_codepoint(input); size_t byte_length = rbs_string_len(input); ptrdiff_t start_offset = 0; From 334c2778addaf6c6d113fc2c446097eca9050b78 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Fri, 28 Mar 2025 06:05:19 +0800 Subject: [PATCH 095/111] Merge `parserstate.c` and `.h` files into respective `parser` files + some cleanups (#26) Part of https://github.com/Shopify/team-ruby-dx/issues/1436 In addition to merging the files, I also did some cleanups like: - Remove unused function declaration - Make some helper functions that aren't referenced outside of C library AND doesn't look like public API as static - A leftover in #25 I recommend reviewing by commits. --- include/rbs/encoding.h | 2 +- include/rbs/parser.h | 136 ++++++++++++- include/rbs/parserstate.h | 169 ---------------- src/encoding.c | 2 +- src/parser.c | 410 +++++++++++++++++++++++++++++++++++--- src/parserstate.c | 379 ----------------------------------- src/rbs_unescape.c | 2 +- 7 files changed, 522 insertions(+), 578 deletions(-) delete mode 100644 include/rbs/parserstate.h delete mode 100644 src/parserstate.c diff --git a/include/rbs/encoding.h b/include/rbs/encoding.h index 2717aa194..c53e4ed62 100644 --- a/include/rbs/encoding.h +++ b/include/rbs/encoding.h @@ -4,6 +4,6 @@ #include "rbs_string.h" unsigned int rbs_utf8_to_codepoint(const rbs_string_t string); -int utf8_codelen(unsigned int c); +int rbs_utf8_codelen(unsigned int c); #endif // RBS_ENCODING_H diff --git a/include/rbs/parser.h b/include/rbs/parser.h index a647d8f62..5a52de4ef 100644 --- a/include/rbs/parser.h +++ b/include/rbs/parser.h @@ -1,7 +1,139 @@ #ifndef RBS__PARSER_H #define RBS__PARSER_H -#include "parserstate.h" +#include "rbs/defines.h" +#include "rbs/util/rbs_allocator.h" +#include "rbs/util/rbs_constant_pool.h" +#include "rbs/lexer.h" +#include "rbs/ast.h" + +#include +#include + +/** + * id_table represents a set of RBS constant IDs. + * This is used to manage the set of bound variables. + * */ +typedef struct id_table { + size_t size; + size_t count; + rbs_constant_id_t *ids; + struct id_table *next; +} id_table; + +/** + * comment represents a sequence of comment lines. + * + * # Comment for the method. + * # + * # ```rb + * # object.foo() # Do something + * # ``` + * # + * def foo: () -> void + * + * A comment object represents the six lines of comments. + * */ +typedef struct comment { + position start; + position end; + + size_t line_size; + size_t line_count; + token *tokens; + + struct comment *next_comment; +} comment; + +typedef struct error { + char *message; + token token; + bool syntax_error; +} error; + +/** + * An RBS parser is a LL(3) parser. + * */ +typedef struct { + lexstate *lexstate; + + token current_token; + token next_token; /* The first lookahead token */ + token next_token2; /* The second lookahead token */ + token next_token3; /* The third lookahead token */ + + id_table *vars; /* Known type variables */ + comment *last_comment; /* Last read comment */ + + rbs_constant_pool_t constant_pool; + rbs_allocator_t allocator; + error *error; +} parserstate; + +/** + * Insert new table entry. + * Setting `reset` inserts a _reset_ entry, which stops searching. + * + * ``` + * class Foo[A] + * ^^^ <= push new table with reset + * def foo: [B] () -> [A, B] + * ^^^ <= push new table without reset + * + * class Baz[C] + * ^^^ <= push new table with reset + * end + * end + * ``` + * */ +id_table *parser_push_typevar_table(parserstate *state, bool reset); + +/** + * Insert new type variable into the latest table. + * */ +NODISCARD bool parser_insert_typevar(parserstate *state, rbs_constant_id_t id); + +/** + * Allocate new lexstate object. + * + * ``` + * VALUE string = rb_funcall(buffer, rb_intern("content"), 0); + * alloc_lexer(string, 0, 31) // New lexstate with buffer content + * ``` + * */ +lexstate *alloc_lexer(rbs_allocator_t *, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); + +/** + * Allocate new parserstate object. + * + * ``` + * alloc_parser(buffer, string, encoding, 0, 1); + * ``` + * */ +parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); +void free_parser(parserstate *parser); + +/** + * Advance one token. + * */ +void parser_advance(parserstate *state); + +void print_parser(parserstate *state); + +/** + * Returns a RBS::Comment object associated with an subject at `subject_line`. + * + * ```rbs + * # Comment1 + * class Foo # This is the subject line for Comment1 + * + * # Comment2 + * %a{annotation} # This is the subject line for Comment2 + * def foo: () -> void + * end + * ``` + * */ +rbs_ast_comment_t *get_comment(parserstate *state, int subject_line); void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5); @@ -9,6 +141,4 @@ bool parse_type(parserstate *state, rbs_node_t **type); bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type); bool parse_signature(parserstate *state, rbs_signature_t **signature); -void rbs__init_parser(); - #endif diff --git a/include/rbs/parserstate.h b/include/rbs/parserstate.h deleted file mode 100644 index 548b89769..000000000 --- a/include/rbs/parserstate.h +++ /dev/null @@ -1,169 +0,0 @@ -#ifndef RBS__PARSERSTATE_H -#define RBS__PARSERSTATE_H - -#include "rbs/defines.h" -#include "rbs/util/rbs_allocator.h" -#include "rbs/util/rbs_constant_pool.h" -#include "rbs/lexer.h" -#include "rbs/ast.h" - -#include -#include - -/** - * id_table represents a set of RBS constant IDs. - * This is used to manage the set of bound variables. - * */ -typedef struct id_table { - size_t size; - size_t count; - rbs_constant_id_t *ids; - struct id_table *next; -} id_table; - -/** - * comment represents a sequence of comment lines. - * - * # Comment for the method. - * # - * # ```rb - * # object.foo() # Do something - * # ``` - * # - * def foo: () -> void - * - * A comment object represents the six lines of comments. - * */ -typedef struct comment { - position start; - position end; - - size_t line_size; - size_t line_count; - token *tokens; - - struct comment *next_comment; -} comment; - -typedef struct error { - char *message; - token token; - bool syntax_error; -} error; - -/** - * An RBS parser is a LL(3) parser. - * */ -typedef struct { - lexstate *lexstate; - - token current_token; - token next_token; /* The first lookahead token */ - token next_token2; /* The second lookahead token */ - token next_token3; /* The third lookahead token */ - - id_table *vars; /* Known type variables */ - comment *last_comment; /* Last read comment */ - - rbs_constant_pool_t constant_pool; - rbs_allocator_t allocator; - error *error; -} parserstate; - -comment *alloc_comment(rbs_allocator_t *, token comment_token, comment *last_comment); -void comment_insert_new_line(rbs_allocator_t *, comment *com, token comment_token); -comment *comment_get_comment(comment *com, int line); - -/** - * Insert new table entry. - * Setting `reset` inserts a _reset_ entry, which stops searching. - * - * ``` - * class Foo[A] - * ^^^ <= push new table with reset - * def foo: [B] () -> [A, B] - * ^^^ <= push new table without reset - * - * class Baz[C] - * ^^^ <= push new table with reset - * end - * end - * ``` - * */ -id_table *parser_push_typevar_table(parserstate *state, bool reset); -NODISCARD bool parser_pop_typevar_table(parserstate *state); - -/** - * Insert new type variable into the latest table. - * */ -NODISCARD bool parser_insert_typevar(parserstate *state, rbs_constant_id_t id); - -/** - * Returns true if given type variable is recorded in the table. - * If not found, it goes one table up, if it's not a reset table. - * Or returns false, if it's a reset table. - * */ -bool parser_typevar_member(parserstate *state, rbs_constant_id_t id); - -/** - * Allocate new lexstate object. - * - * ``` - * VALUE string = rb_funcall(buffer, rb_intern("content"), 0); - * alloc_lexer(string, 0, 31) // New lexstate with buffer content - * ``` - * */ -lexstate *alloc_lexer(rbs_allocator_t *, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); - -/** - * Allocate new parserstate object. - * Optionally call `rbs_parser_declare_type_variables_from_ruby_array()` after, to populate the type variable table. - * - * Once allocated, optionally call `rbs_parser_declare_type_variables` if you'd like to declare - * any type variables to be used during the parsing. - * - * ``` - * alloc_parser(buffer, string, encoding, 0, 1); - * rbs_parser_declare_type_variables_from_ruby_array(variables); - * ``` - * */ -parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); -void free_parser(parserstate *parser); - -/** - * Declare type variables to be used during parsing. - * */ -void rbs_parser_declare_type_variables(parserstate *parser, size_t count, const char **variables); - -/** - * Advance one token. - * */ -void parser_advance(parserstate *state); - -/** - * Advance one token if the next_token is a token of the type. - * */ -bool parser_advance_if(parserstate *state, enum RBSTokenType type); -void print_parser(parserstate *state); - -/** - * Insert new comment line token. - * */ -void insert_comment_line(parserstate *state, token token); - -/** - * Returns a RBS::Comment object associated with an subject at `subject_line`. - * - * ```rbs - * # Comment1 - * class Foo # This is the subject line for Comment1 - * - * # Comment2 - * %a{annotation} # This is the subject line for Comment2 - * def foo: () -> void - * end - * ``` - * */ -rbs_ast_comment_t *get_comment(parserstate *state, int subject_line); - -#endif diff --git a/src/encoding.c b/src/encoding.c index 22461ab14..6d47ce2f7 100644 --- a/src/encoding.c +++ b/src/encoding.c @@ -48,7 +48,7 @@ unsigned int rbs_utf8_to_codepoint(const rbs_string_t string) { return codepoint; } -int utf8_codelen(unsigned int c) { +int rbs_utf8_codelen(unsigned int c) { if (c <= 0x7F) return 1; if (c <= 0x7FF) return 2; if (c <= 0xFFFF) return 3; diff --git a/src/parser.c b/src/parser.c index c973634a5..a12ef29f8 100644 --- a/src/parser.c +++ b/src/parser.c @@ -9,6 +9,7 @@ #include "rbs/defines.h" #include "rbs/encoding.h" #include "rbs/rbs_string.h" +#include "rbs/rbs_buffer.h" #include "rbs/rbs_unescape.h" #include "rbs/util/rbs_assert.h" @@ -78,6 +79,8 @@ ASSERT_TOKEN(state, expected_type) \ } while(0); +#define RESET_TABLE_P(table) (table->size == 0) + typedef struct { rbs_node_list_t *required_positionals; rbs_node_list_t *optional_positionals; @@ -121,36 +124,13 @@ static rbs_constant_id_t rbs_constant_pool_insert_string(rbs_constant_pool_t *se return rbs_constant_pool_insert_shared(self, (const uint8_t *) string.start, rbs_string_len(string)); } -void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt, ...) { - if (state->error) { - return; - } - - va_list args; - - va_start(args, fmt); - int length = vsnprintf(NULL, 0, fmt, args); - va_end(args); - - char *message = rbs_allocator_alloc_many(&state->allocator, length + 1, char); - - va_start(args, fmt); - vsnprintf(message, length + 1, fmt, args); - va_end(args); - - state->error = rbs_allocator_alloc(&state->allocator, error); - state->error->token = tok; - state->error->message = message; - state->error->syntax_error = syntax_error; -} - typedef enum { CLASS_NAME = 1, INTERFACE_NAME = 2, ALIAS_NAME = 4 } TypeNameKind; -void parser_advance_no_gap(parserstate *state) { +static void parser_advance_no_gap(parserstate *state) { if (state->current_token.range.end.byte_pos == state->next_token.range.start.byte_pos) { parser_advance(state); } else { @@ -422,6 +402,21 @@ static bool is_keyword(parserstate *state) { return false; } +/** + * Advance token if _next_ token is `type`. + * Ensures one token advance and `state->current_token.type == type`, or current token not changed. + * + * @returns true if token advances, false otherwise. + **/ +static bool parser_advance_if(parserstate *state, enum RBSTokenType type) { + if (state->next_token.type == type) { + parser_advance(state); + return true; + } else { + return false; + } +} + /* params ::= {} `)` | {} `?` `)` -- Untyped function params (assign params.required = nil) @@ -1025,6 +1020,27 @@ static bool parse_singleton_type(parserstate *state, rbs_types_classsingleton_t return true; } +/** + * Returns true if given type variable is recorded in the table. + * If not found, it goes one table up, if it's not a reset table. + * Or returns false, if it's a reset table. + * */ +static bool parser_typevar_member(parserstate *state, rbs_constant_id_t id) { + id_table *table = state->vars; + + while (table && !RESET_TABLE_P(table)) { + for (size_t i = 0; i < table->count; i++) { + if (table->ids[i] == id) { + return true; + } + } + + table = table->next; + } + + return false; +} + /* simple ::= {} `(` type <`)`> | {} @@ -1392,6 +1408,26 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa return true; } +NODISCARD +static bool parser_pop_typevar_table(parserstate *state) { + id_table *table; + + if (state->vars) { + table = state->vars; + state->vars = table->next; + } else { + set_error(state, state->current_token, false, "Cannot pop empty table"); + return false; + } + + if (state->vars && RESET_TABLE_P(state->vars)) { + table = state->vars; + state->vars = table->next; + } + + return true; +} + /* method_type ::= {} type_params */ @@ -3053,6 +3089,112 @@ static bool parse_use_directive(parserstate *state, rbs_ast_directives_use_t **u return true; } +static rbs_ast_comment_t *parse_comment_lines(parserstate *state, comment *com) { + size_t hash_bytes = state->lexstate->encoding->char_width((const uint8_t *) "#", (size_t) 1); + size_t space_bytes = state->lexstate->encoding->char_width((const uint8_t *) " ", (size_t) 1); + + rbs_buffer_t rbs_buffer; + rbs_buffer_init(&state->allocator, &rbs_buffer); + + for (size_t i = 0; i < com->line_count; i++) { + token tok = com->tokens[i]; + + const char *comment_start = state->lexstate->string.start + tok.range.start.byte_pos + hash_bytes; + size_t comment_bytes = RBS_RANGE_BYTES(tok.range) - hash_bytes; + + rbs_string_t str = rbs_string_new( + comment_start, + state->lexstate->string.end + ); + unsigned char c = rbs_utf8_to_codepoint(str); + + if (c == ' ') { + comment_start += space_bytes; + comment_bytes -= space_bytes; + } + + rbs_buffer_append_string(&state->allocator, &rbs_buffer, comment_start, comment_bytes); + rbs_buffer_append_cstr(&state->allocator, &rbs_buffer, "\n"); + } + + return rbs_ast_comment_new( + &state->allocator, + rbs_location_new(&state->allocator, (range) { .start = com->start, .end = com->end }), + rbs_buffer_to_string(&rbs_buffer) + ); +} + +static comment *comment_get_comment(comment *com, int line) { + if (com == NULL) { + return NULL; + } + + if (com->end.line < line) { + return NULL; + } + + if (com->end.line == line) { + return com; + } + + return comment_get_comment(com->next_comment, line); +} + +static void comment_insert_new_line(rbs_allocator_t *allocator, comment *com, token comment_token) { + if (com->line_count == 0) { + com->start = comment_token.range.start; + } + + if (com->line_count == com->line_size) { + com->line_size += 10; + + if (com->tokens) { + token *p = com->tokens; + com->tokens = rbs_allocator_calloc(allocator, com->line_size, token); + memcpy(com->tokens, p, sizeof(token) * com->line_count); + } else { + com->tokens = rbs_allocator_calloc(allocator, com->line_size, token); + } + } + + com->tokens[com->line_count++] = comment_token; + com->end = comment_token.range.end; +} + +static comment *alloc_comment(rbs_allocator_t *allocator, token comment_token, comment *last_comment) { + comment *new_comment = rbs_allocator_alloc(allocator, comment); + + *new_comment = (comment) { + .start = comment_token.range.start, + .end = comment_token.range.end, + + .line_size = 0, + .line_count = 0, + .tokens = NULL, + + .next_comment = last_comment, + }; + + comment_insert_new_line(allocator, new_comment, comment_token); + + return new_comment; +} + +/** + * Insert new comment line token. + * */ +static void insert_comment_line(parserstate *state, token tok) { + int prev_line = tok.range.start.line - 1; + + comment *com = comment_get_comment(state->last_comment, prev_line); + + if (com) { + comment_insert_new_line(&state->allocator, com, tok); + } else { + state->last_comment = alloc_comment(&state->allocator, tok, state->last_comment); + } +} + bool parse_signature(parserstate *state, rbs_signature_t **signature) { range signature_range; signature_range.start = state->current_token.range.start; @@ -3081,3 +3223,223 @@ bool parse_signature(parserstate *state, rbs_signature_t **signature) { *signature = rbs_signature_new(&state->allocator, rbs_location_new(&state->allocator, signature_range), dirs, decls); return true; } + +id_table *alloc_empty_table(rbs_allocator_t *allocator) { + id_table *table = rbs_allocator_alloc(allocator, id_table); + + *table = (id_table) { + .size = 10, + .count = 0, + .ids = rbs_allocator_calloc(allocator, 10, rbs_constant_id_t), + .next = NULL, + }; + + return table; +} + +id_table *alloc_reset_table(rbs_allocator_t *allocator) { + id_table *table = rbs_allocator_alloc(allocator, id_table); + + *table = (id_table) { + .size = 0, + .count = 0, + .ids = NULL, + .next = NULL, + }; + + return table; +} + +id_table *parser_push_typevar_table(parserstate *state, bool reset) { + if (reset) { + id_table *table = alloc_reset_table(&state->allocator); + table->next = state->vars; + state->vars = table; + } + + id_table *table = alloc_empty_table(&state->allocator); + table->next = state->vars; + state->vars = table; + + return table; +} + +NODISCARD +bool parser_insert_typevar(parserstate *state, rbs_constant_id_t id) { + id_table *table = state->vars; + + if (RESET_TABLE_P(table)) { + set_error(state, state->current_token, false, "Cannot insert to reset table"); + return false; + } + + if (table->size == table->count) { + // expand + rbs_constant_id_t *ptr = table->ids; + table->size += 10; + table->ids = rbs_allocator_calloc(&state->allocator, table->size, rbs_constant_id_t); + memcpy(table->ids, ptr, sizeof(rbs_constant_id_t) * table->count); + } + + table->ids[table->count++] = id; + + return true; +} + +void print_parser(parserstate *state) { + printf(" current_token = %s (%d...%d)\n", token_type_str(state->current_token.type), state->current_token.range.start.char_pos, state->current_token.range.end.char_pos); + printf(" next_token = %s (%d...%d)\n", token_type_str(state->next_token.type), state->next_token.range.start.char_pos, state->next_token.range.end.char_pos); + printf(" next_token2 = %s (%d...%d)\n", token_type_str(state->next_token2.type), state->next_token2.range.start.char_pos, state->next_token2.range.end.char_pos); + printf(" next_token3 = %s (%d...%d)\n", token_type_str(state->next_token3.type), state->next_token3.range.start.char_pos, state->next_token3.range.end.char_pos); +} + +void parser_advance(parserstate *state) { + state->current_token = state->next_token; + state->next_token = state->next_token2; + state->next_token2 = state->next_token3; + + while (true) { + if (state->next_token3.type == pEOF) { + break; + } + + state->next_token3 = rbsparser_next_token(state->lexstate); + + if (state->next_token3.type == tCOMMENT) { + // skip + } else if (state->next_token3.type == tLINECOMMENT) { + insert_comment_line(state, state->next_token3); + } else if (state->next_token3.type == tTRIVIA) { + //skip + } else { + break; + } + } +} + +void rbs_print_token(token tok) { + printf( + "%s char=%d...%d\n", + token_type_str(tok.type), + tok.range.start.char_pos, + tok.range.end.char_pos + ); +} + +rbs_ast_comment_t *get_comment(parserstate *state, int subject_line) { + int comment_line = subject_line - 1; + + comment *com = comment_get_comment(state->last_comment, comment_line); + + if (com) { + return parse_comment_lines(state, com); + } else { + return NULL; + } +} + +lexstate *alloc_lexer(rbs_allocator_t *allocator, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { + lexstate *lexer = rbs_allocator_alloc(allocator, lexstate); + + position start_position = (position) { + .byte_pos = 0, + .char_pos = 0, + .line = 1, + .column = 0, + }; + + *lexer = (lexstate) { + .string = string, + .start_pos = start_pos, + .end_pos = end_pos, + .current = start_position, + .start = { 0 }, + .first_token_of_line = false, + .last_char = 0, + .encoding = encoding, + }; + + rbs_skipn(lexer, start_pos); + lexer->start = lexer->current; + lexer->first_token_of_line = lexer->current.column == 0; + + return lexer; +} + +parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { + rbs_allocator_t allocator; + rbs_allocator_init(&allocator); + + lexstate *lexer = alloc_lexer(&allocator, string, encoding, start_pos, end_pos); + parserstate *parser = rbs_allocator_alloc(&allocator, parserstate); + + *parser = (parserstate) { + .lexstate = lexer, + + .current_token = NullToken, + .next_token = NullToken, + .next_token2 = NullToken, + .next_token3 = NullToken, + + .vars = NULL, + .last_comment = NULL, + + .constant_pool = {}, + .allocator = allocator, + .error = NULL, + }; + + // The parser's constant pool is mainly used for storing the names of type variables, which usually aren't many. + // Below are some statistics gathered from the current test suite. We can see that 56% of parsers never add to their + // constant pool at all. The initial capacity needs to be a power of 2. Picking 2 means that we won't need to realloc + // in 85% of cases. + // + // TODO: recalculate these statistics based on a real world codebase, rather than the test suite. + // + // | Size | Count | Cumulative | % Coverage | + // |------|-------|------------|------------| + // | 0 | 7,862 | 7,862 | 56% | + // | 1 | 3,196 | 11,058 | 79% | + // | 2 | 778 | 12,719 | 85% | + // | 3 | 883 | 11,941 | 91% | + // | 4 | 478 | 13,197 | 95% | + // | 5 | 316 | 13,513 | 97% | + // | 6 | 288 | 13,801 | 99% | + // | 7 | 144 | 13,945 | 100% | + const size_t initial_pool_capacity = 2; + rbs_constant_pool_init(&parser->constant_pool, initial_pool_capacity); + + parser_advance(parser); + parser_advance(parser); + parser_advance(parser); + + return parser; +} + +void free_parser(parserstate *parser) { + rbs_constant_pool_free(&parser->constant_pool); + rbs_allocator_free(&parser->allocator); +} + +void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt, ...) { + if (state->error) { + return; + } + + va_list args; + + va_start(args, fmt); + int length = vsnprintf(NULL, 0, fmt, args); + va_end(args); + + char *message = rbs_allocator_alloc_many(&state->allocator, length + 1, char); + + va_start(args, fmt); + vsnprintf(message, length + 1, fmt, args); + va_end(args); + + state->error = rbs_allocator_alloc(&state->allocator, error); + state->error->token = tok; + state->error->message = message; + state->error->syntax_error = syntax_error; +} diff --git a/src/parserstate.c b/src/parserstate.c deleted file mode 100644 index 4599cc16c..000000000 --- a/src/parserstate.c +++ /dev/null @@ -1,379 +0,0 @@ -#include "rbs/parserstate.h" - -#include "rbs/parser.h" -#include "rbs/encoding.h" -#include "rbs/rbs_buffer.h" -#include "rbs/util/rbs_assert.h" - -#include - -#define RESET_TABLE_P(table) (table->size == 0) - -id_table *alloc_empty_table(rbs_allocator_t *allocator) { - id_table *table = rbs_allocator_alloc(allocator, id_table); - - *table = (id_table) { - .size = 10, - .count = 0, - .ids = rbs_allocator_calloc(allocator, 10, rbs_constant_id_t), - .next = NULL, - }; - - return table; -} - -id_table *alloc_reset_table(rbs_allocator_t *allocator) { - id_table *table = rbs_allocator_alloc(allocator, id_table); - - *table = (id_table) { - .size = 0, - .count = 0, - .ids = NULL, - .next = NULL, - }; - - return table; -} - -id_table *parser_push_typevar_table(parserstate *state, bool reset) { - if (reset) { - id_table *table = alloc_reset_table(&state->allocator); - table->next = state->vars; - state->vars = table; - } - - id_table *table = alloc_empty_table(&state->allocator); - table->next = state->vars; - state->vars = table; - - return table; -} - -NODISCARD -bool parser_pop_typevar_table(parserstate *state) { - id_table *table; - - if (state->vars) { - table = state->vars; - state->vars = table->next; - } else { - set_error(state, state->current_token, false, "Cannot pop empty table"); - return false; - } - - if (state->vars && RESET_TABLE_P(state->vars)) { - table = state->vars; - state->vars = table->next; - } - - return true; -} - -NODISCARD -bool parser_insert_typevar(parserstate *state, rbs_constant_id_t id) { - id_table *table = state->vars; - - if (RESET_TABLE_P(table)) { - set_error(state, state->current_token, false, "Cannot insert to reset table"); - return false; - } - - if (table->size == table->count) { - // expand - rbs_constant_id_t *ptr = table->ids; - table->size += 10; - table->ids = rbs_allocator_calloc(&state->allocator, table->size, rbs_constant_id_t); - memcpy(table->ids, ptr, sizeof(rbs_constant_id_t) * table->count); - } - - table->ids[table->count++] = id; - - return true; -} - -bool parser_typevar_member(parserstate *state, rbs_constant_id_t id) { - id_table *table = state->vars; - - while (table && !RESET_TABLE_P(table)) { - for (size_t i = 0; i < table->count; i++) { - if (table->ids[i] == id) { - return true; - } - } - - table = table->next; - } - - return false; -} - -void print_parser(parserstate *state) { - printf(" current_token = %s (%d...%d)\n", token_type_str(state->current_token.type), state->current_token.range.start.char_pos, state->current_token.range.end.char_pos); - printf(" next_token = %s (%d...%d)\n", token_type_str(state->next_token.type), state->next_token.range.start.char_pos, state->next_token.range.end.char_pos); - printf(" next_token2 = %s (%d...%d)\n", token_type_str(state->next_token2.type), state->next_token2.range.start.char_pos, state->next_token2.range.end.char_pos); - printf(" next_token3 = %s (%d...%d)\n", token_type_str(state->next_token3.type), state->next_token3.range.start.char_pos, state->next_token3.range.end.char_pos); -} - -void parser_advance(parserstate *state) { - state->current_token = state->next_token; - state->next_token = state->next_token2; - state->next_token2 = state->next_token3; - - while (true) { - if (state->next_token3.type == pEOF) { - break; - } - - state->next_token3 = rbsparser_next_token(state->lexstate); - - if (state->next_token3.type == tCOMMENT) { - // skip - } else if (state->next_token3.type == tLINECOMMENT) { - insert_comment_line(state, state->next_token3); - } else if (state->next_token3.type == tTRIVIA) { - //skip - } else { - break; - } - } -} - -/** - * Advance token if _next_ token is `type`. - * Ensures one token advance and `state->current_token.type == type`, or current token not changed. - * - * @returns true if token advances, false otherwise. - **/ -bool parser_advance_if(parserstate *state, enum RBSTokenType type) { - if (state->next_token.type == type) { - parser_advance(state); - return true; - } else { - return false; - } -} - -void rbs_print_token(token tok) { - printf( - "%s char=%d...%d\n", - token_type_str(tok.type), - tok.range.start.char_pos, - tok.range.end.char_pos - ); -} - -void insert_comment_line(parserstate *state, token tok) { - int prev_line = tok.range.start.line - 1; - - comment *com = comment_get_comment(state->last_comment, prev_line); - - if (com) { - comment_insert_new_line(&state->allocator, com, tok); - } else { - state->last_comment = alloc_comment(&state->allocator, tok, state->last_comment); - } -} - -static rbs_ast_comment_t *parse_comment_lines(parserstate *state, comment *com) { - size_t hash_bytes = state->lexstate->encoding->char_width((const uint8_t *) "#", (size_t) 1); - size_t space_bytes = state->lexstate->encoding->char_width((const uint8_t *) " ", (size_t) 1); - - rbs_buffer_t rbs_buffer; - rbs_buffer_init(&state->allocator, &rbs_buffer); - - for (size_t i = 0; i < com->line_count; i++) { - token tok = com->tokens[i]; - - const char *comment_start = state->lexstate->string.start + tok.range.start.byte_pos + hash_bytes; - size_t comment_bytes = RBS_RANGE_BYTES(tok.range) - hash_bytes; - - rbs_string_t str = rbs_string_new( - comment_start, - state->lexstate->string.end - ); - unsigned char c = rbs_utf8_to_codepoint(str); - - if (c == ' ') { - comment_start += space_bytes; - comment_bytes -= space_bytes; - } - - rbs_buffer_append_string(&state->allocator, &rbs_buffer, comment_start, comment_bytes); - rbs_buffer_append_cstr(&state->allocator, &rbs_buffer, "\n"); - } - - return rbs_ast_comment_new( - &state->allocator, - rbs_location_new(&state->allocator, (range) { .start = com->start, .end = com->end }), - rbs_buffer_to_string(&rbs_buffer) - ); -} - -rbs_ast_comment_t *get_comment(parserstate *state, int subject_line) { - int comment_line = subject_line - 1; - - comment *com = comment_get_comment(state->last_comment, comment_line); - - if (com) { - return parse_comment_lines(state, com); - } else { - return NULL; - } -} - -comment *alloc_comment(rbs_allocator_t *allocator, token comment_token, comment *last_comment) { - comment *new_comment = rbs_allocator_alloc(allocator, comment); - - *new_comment = (comment) { - .start = comment_token.range.start, - .end = comment_token.range.end, - - .line_size = 0, - .line_count = 0, - .tokens = NULL, - - .next_comment = last_comment, - }; - - comment_insert_new_line(allocator, new_comment, comment_token); - - return new_comment; -} - -void comment_insert_new_line(rbs_allocator_t *allocator, comment *com, token comment_token) { - if (com->line_count == 0) { - com->start = comment_token.range.start; - } - - if (com->line_count == com->line_size) { - com->line_size += 10; - - if (com->tokens) { - token *p = com->tokens; - com->tokens = rbs_allocator_calloc(allocator, com->line_size, token); - memcpy(com->tokens, p, sizeof(token) * com->line_count); - } else { - com->tokens = rbs_allocator_calloc(allocator, com->line_size, token); - } - } - - com->tokens[com->line_count++] = comment_token; - com->end = comment_token.range.end; -} - -comment *comment_get_comment(comment *com, int line) { - if (com == NULL) { - return NULL; - } - - if (com->end.line < line) { - return NULL; - } - - if (com->end.line == line) { - return com; - } - - return comment_get_comment(com->next_comment, line); -} - -lexstate *alloc_lexer(rbs_allocator_t *allocator, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { - lexstate *lexer = rbs_allocator_alloc(allocator, lexstate); - - position start_position = (position) { - .byte_pos = 0, - .char_pos = 0, - .line = 1, - .column = 0, - }; - - *lexer = (lexstate) { - .string = string, - .start_pos = start_pos, - .end_pos = end_pos, - .current = start_position, - .start = { 0 }, - .first_token_of_line = false, - .last_char = 0, - .encoding = encoding, - }; - - rbs_skipn(lexer, start_pos); - lexer->start = lexer->current; - lexer->first_token_of_line = lexer->current.column == 0; - - return lexer; -} - -parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { - rbs_allocator_t allocator; - rbs_allocator_init(&allocator); - - lexstate *lexer = alloc_lexer(&allocator, string, encoding, start_pos, end_pos); - parserstate *parser = rbs_allocator_alloc(&allocator, parserstate); - - *parser = (parserstate) { - .lexstate = lexer, - - .current_token = NullToken, - .next_token = NullToken, - .next_token2 = NullToken, - .next_token3 = NullToken, - - .vars = NULL, - .last_comment = NULL, - - .constant_pool = {}, - .allocator = allocator, - .error = NULL, - }; - - // The parser's constant pool is mainly used for storing the names of type variables, which usually aren't many. - // Below are some statistics gathered from the current test suite. We can see that 56% of parsers never add to their - // constant pool at all. The initial capacity needs to be a power of 2. Picking 2 means that we won't need to realloc - // in 85% of cases. - // - // TODO: recalculate these statistics based on a real world codebase, rather than the test suite. - // - // | Size | Count | Cumulative | % Coverage | - // |------|-------|------------|------------| - // | 0 | 7,862 | 7,862 | 56% | - // | 1 | 3,196 | 11,058 | 79% | - // | 2 | 778 | 12,719 | 85% | - // | 3 | 883 | 11,941 | 91% | - // | 4 | 478 | 13,197 | 95% | - // | 5 | 316 | 13,513 | 97% | - // | 6 | 288 | 13,801 | 99% | - // | 7 | 144 | 13,945 | 100% | - const size_t initial_pool_capacity = 2; - rbs_constant_pool_init(&parser->constant_pool, initial_pool_capacity); - - parser_advance(parser); - parser_advance(parser); - parser_advance(parser); - - return parser; -} - -void rbs_parser_declare_type_variables(parserstate *parser, size_t count, const char **variables) { - if (variables == NULL) return; // Nothing to do. - - parser_push_typevar_table(parser, true); - - for (size_t i = 0; i < count; i++) { - rbs_constant_id_t name = rbs_constant_pool_insert_shared( - &parser->constant_pool, - (const uint8_t *) variables[i], - strlen(variables[i]) - ); - - if (!parser_insert_typevar(parser, name)) { - rbs_assert(false, "Failed to insert type variable. Message: %s", parser->error->message); - } - } -} - -void free_parser(parserstate *parser) { - rbs_constant_pool_free(&parser->constant_pool); - rbs_allocator_free(&parser->allocator); -} diff --git a/src/rbs_unescape.c b/src/rbs_unescape.c index 589aa4fa5..59a4ef798 100644 --- a/src/rbs_unescape.c +++ b/src/rbs_unescape.c @@ -112,7 +112,7 @@ rbs_string_t rbs_unquote_string(rbs_allocator_t *allocator, rbs_string_t input) ptrdiff_t start_offset = 0; if (first_char == '"' || first_char == '\'' || first_char == '`') { - int bs = utf8_codelen(first_char); + int bs = rbs_utf8_codelen(first_char); start_offset += bs; byte_length -= 2 * bs; } From bb5e560ad3f48f838df24a215ae58ee7966d088f Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Fri, 28 Mar 2025 23:30:15 +0800 Subject: [PATCH 096/111] Hide `id_table` struct (#27) --- include/rbs/parser.h | 15 ++------------- src/parser.c | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/include/rbs/parser.h b/include/rbs/parser.h index 5a52de4ef..2c7a602df 100644 --- a/include/rbs/parser.h +++ b/include/rbs/parser.h @@ -10,17 +10,6 @@ #include #include -/** - * id_table represents a set of RBS constant IDs. - * This is used to manage the set of bound variables. - * */ -typedef struct id_table { - size_t size; - size_t count; - rbs_constant_id_t *ids; - struct id_table *next; -} id_table; - /** * comment represents a sequence of comment lines. * @@ -62,7 +51,7 @@ typedef struct { token next_token2; /* The second lookahead token */ token next_token3; /* The third lookahead token */ - id_table *vars; /* Known type variables */ + struct id_table *vars; /* Known type variables */ comment *last_comment; /* Last read comment */ rbs_constant_pool_t constant_pool; @@ -86,7 +75,7 @@ typedef struct { * end * ``` * */ -id_table *parser_push_typevar_table(parserstate *state, bool reset); +void parser_push_typevar_table(parserstate *state, bool reset); /** * Insert new type variable into the latest table. diff --git a/src/parser.c b/src/parser.c index a12ef29f8..a495ff33e 100644 --- a/src/parser.c +++ b/src/parser.c @@ -91,6 +91,17 @@ typedef struct { rbs_node_t *rest_keywords; } method_params; +/** + * id_table represents a set of RBS constant IDs. + * This is used to manage the set of bound variables. + * */ +typedef struct id_table { + size_t size; + size_t count; + rbs_constant_id_t *ids; + struct id_table *next; +} id_table; + static bool rbs_is_untyped_params(method_params *params) { return params->required_positionals == NULL; } @@ -3250,7 +3261,7 @@ id_table *alloc_reset_table(rbs_allocator_t *allocator) { return table; } -id_table *parser_push_typevar_table(parserstate *state, bool reset) { +void parser_push_typevar_table(parserstate *state, bool reset) { if (reset) { id_table *table = alloc_reset_table(&state->allocator); table->next = state->vars; @@ -3260,8 +3271,6 @@ id_table *parser_push_typevar_table(parserstate *state, bool reset) { id_table *table = alloc_empty_table(&state->allocator); table->next = state->vars; state->vars = table; - - return table; } NODISCARD From 61b3e4a3bb76b3879eec49a97e3821c34be555e0 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Sat, 29 Mar 2025 02:10:20 +0800 Subject: [PATCH 097/111] Rename parser related structs (#28) - `parserstate` -> `rbs_parser_t` - Variable names `state` -> `parser` - `comment` -> `rbs_comment_t` - `error` -> `rbs_error_t` --- ext/rbs_extension/main.c | 24 +- include/rbs/parser.h | 40 +- include/rbs/rbs_unescape.h | 2 +- src/parser.c | 1956 ++++++++++++++++++------------------ 4 files changed, 1011 insertions(+), 1011 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 26fa170df..b2c2f0860 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -15,7 +15,7 @@ * foo.rbs:11:21...11:25: Syntax error: {message}, token=`{tok source}` ({tok type}) * ``` * */ -static NORETURN(void) raise_error(error *error, VALUE buffer) { +static NORETURN(void) raise_error(rbs_error_t *error, VALUE buffer) { rbs_assert(error != NULL, "raise_error() called with NULL error"); if (!error->syntax_error) { @@ -37,7 +37,7 @@ static NORETURN(void) raise_error(error *error, VALUE buffer) { rb_exc_raise(rb_error); } -void raise_error_if_any(parserstate *parser, VALUE buffer) { +void raise_error_if_any(rbs_parser_t *parser, VALUE buffer) { if (parser->error != NULL) { raise_error(parser->error, buffer); } @@ -48,7 +48,7 @@ void raise_error_if_any(parserstate *parser, VALUE buffer) { * @param parser * @param variables A Ruby Array of Symbols, or nil. */ -static void declare_type_variables(parserstate *parser, VALUE variables, VALUE buffer) { +static void declare_type_variables(rbs_parser_t *parser, VALUE variables, VALUE buffer) { if (NIL_P(variables)) return; // Nothing to do. if (!RB_TYPE_P(variables, T_ARRAY)) { @@ -87,18 +87,18 @@ static void declare_type_variables(parserstate *parser, VALUE variables, VALUE b struct parse_type_arg { VALUE buffer; rb_encoding *encoding; - parserstate *parser; + rbs_parser_t *parser; VALUE require_eof; }; static VALUE ensure_free_parser(VALUE parser) { - free_parser((parserstate *)parser); + free_parser((rbs_parser_t *)parser); return Qnil; } static VALUE parse_type_try(VALUE a) { struct parse_type_arg *arg = (struct parse_type_arg *)a; - parserstate *parser = arg->parser; + rbs_parser_t *parser = arg->parser; if (parser->next_token.type == pEOF) { return Qnil; @@ -145,7 +145,7 @@ static lexstate *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE strin ); } -static parserstate *alloc_parser_from_buffer(VALUE buffer, int start_pos, int end_pos) { +static rbs_parser_t *alloc_parser_from_buffer(VALUE buffer, int start_pos, int end_pos) { if (start_pos < 0 || end_pos < 0) { rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos); } @@ -170,7 +170,7 @@ static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VAL StringValue(string); rb_encoding *encoding = rb_enc_get(string); - parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); + rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); declare_type_variables(parser, variables, buffer); struct parse_type_arg arg = { .buffer = buffer, @@ -188,7 +188,7 @@ static VALUE rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VAL static VALUE parse_method_type_try(VALUE a) { struct parse_type_arg *arg = (struct parse_type_arg *)a; - parserstate *parser = arg->parser; + rbs_parser_t *parser = arg->parser; if (parser->next_token.type == pEOF) { return Qnil; @@ -223,7 +223,7 @@ static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_p StringValue(string); rb_encoding *encoding = rb_enc_get(string); - parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); + rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); declare_type_variables(parser, variables, buffer); struct parse_type_arg arg = { .buffer = buffer, @@ -241,7 +241,7 @@ static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_p static VALUE parse_signature_try(VALUE a) { struct parse_type_arg *arg = (struct parse_type_arg *)a; - parserstate *parser = arg->parser; + rbs_parser_t *parser = arg->parser; rbs_signature_t *signature = NULL; parse_signature(parser, &signature); @@ -264,7 +264,7 @@ static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos StringValue(string); rb_encoding *encoding = rb_enc_get(string); - parserstate *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); + rbs_parser_t *parser = alloc_parser_from_buffer(buffer, FIX2INT(start_pos), FIX2INT(end_pos)); struct parse_type_arg arg = { .buffer = buffer, .encoding = encoding, diff --git a/include/rbs/parser.h b/include/rbs/parser.h index 2c7a602df..800dce100 100644 --- a/include/rbs/parser.h +++ b/include/rbs/parser.h @@ -23,7 +23,7 @@ * * A comment object represents the six lines of comments. * */ -typedef struct comment { +typedef struct rbs_comment_t { position start; position end; @@ -31,14 +31,14 @@ typedef struct comment { size_t line_count; token *tokens; - struct comment *next_comment; -} comment; + struct rbs_comment_t *next_comment; +} rbs_comment_t; -typedef struct error { +typedef struct rbs_error_t { char *message; token token; bool syntax_error; -} error; +} rbs_error_t; /** * An RBS parser is a LL(3) parser. @@ -52,12 +52,12 @@ typedef struct { token next_token3; /* The third lookahead token */ struct id_table *vars; /* Known type variables */ - comment *last_comment; /* Last read comment */ + rbs_comment_t *last_comment; /* Last read comment */ rbs_constant_pool_t constant_pool; rbs_allocator_t allocator; - error *error; -} parserstate; + rbs_error_t *error; +} rbs_parser_t; /** * Insert new table entry. @@ -75,12 +75,12 @@ typedef struct { * end * ``` * */ -void parser_push_typevar_table(parserstate *state, bool reset); +void parser_push_typevar_table(rbs_parser_t *parser, bool reset); /** * Insert new type variable into the latest table. * */ -NODISCARD bool parser_insert_typevar(parserstate *state, rbs_constant_id_t id); +NODISCARD bool parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id); /** * Allocate new lexstate object. @@ -93,21 +93,21 @@ NODISCARD bool parser_insert_typevar(parserstate *state, rbs_constant_id_t id); lexstate *alloc_lexer(rbs_allocator_t *, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); /** - * Allocate new parserstate object. + * Allocate new rbs_parser_t object. * * ``` * alloc_parser(buffer, string, encoding, 0, 1); * ``` * */ -parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); -void free_parser(parserstate *parser); +rbs_parser_t *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); +void free_parser(rbs_parser_t *parser); /** * Advance one token. * */ -void parser_advance(parserstate *state); +void parser_advance(rbs_parser_t *parser); -void print_parser(parserstate *state); +void print_parser(rbs_parser_t *parser); /** * Returns a RBS::Comment object associated with an subject at `subject_line`. @@ -122,12 +122,12 @@ void print_parser(parserstate *state); * end * ``` * */ -rbs_ast_comment_t *get_comment(parserstate *state, int subject_line); +rbs_ast_comment_t *get_comment(rbs_parser_t *parser, int subject_line); -void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5); +void set_error(rbs_parser_t *parser, token tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5); -bool parse_type(parserstate *state, rbs_node_t **type); -bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type); -bool parse_signature(parserstate *state, rbs_signature_t **signature); +bool parse_type(rbs_parser_t *parser, rbs_node_t **type); +bool parse_method_type(rbs_parser_t *parser, rbs_methodtype_t **method_type); +bool parse_signature(rbs_parser_t *parser, rbs_signature_t **signature); #endif diff --git a/include/rbs/rbs_unescape.h b/include/rbs/rbs_unescape.h index fef05a5e6..90c9f90a6 100644 --- a/include/rbs/rbs_unescape.h +++ b/include/rbs/rbs_unescape.h @@ -6,7 +6,7 @@ #include "rbs/rbs_string.h" /** - * Receives `parserstate` and `range`, which represents a string token or symbol token, and returns a string VALUE. + * Receives `rbs_parser_t` and `range`, which represents a string token or symbol token, and returns a string VALUE. * * Input token | Output string * ------------+------------- diff --git a/src/parser.c b/src/parser.c index a495ff33e..acb5ab0e5 100644 --- a/src/parser.c +++ b/src/parser.c @@ -20,12 +20,12 @@ strlen(str) \ ) -#define INTERN_TOKEN(parserstate, tok) \ +#define INTERN_TOKEN(parser, tok) \ rbs_constant_pool_insert_shared_with_encoding( \ - &parserstate->constant_pool, \ - (const uint8_t *) rbs_peek_token(parserstate->lexstate, tok),\ + &parser->constant_pool, \ + (const uint8_t *) rbs_peek_token(parser->lexstate, tok),\ rbs_token_bytes(tok), \ - (void *) parserstate->lexstate->encoding \ + (void *) parser->lexstate->encoding \ ) #define KEYWORD_CASES \ @@ -68,15 +68,15 @@ return false; \ } -#define ASSERT_TOKEN(state, expected_type) \ - if (state->current_token.type != expected_type) { \ - set_error(state, state->current_token, true, "expected a token `%s`", token_type_str(expected_type)); \ +#define ASSERT_TOKEN(parser, expected_type) \ + if (parser->current_token.type != expected_type) { \ + set_error(parser, parser->current_token, true, "expected a token `%s`", token_type_str(expected_type)); \ return false; \ } -#define ADVANCE_ASSERT(state, expected_type) do {\ - parser_advance(state); \ - ASSERT_TOKEN(state, expected_type) \ +#define ADVANCE_ASSERT(parser, expected_type) do {\ + parser_advance(parser); \ + ASSERT_TOKEN(parser, expected_type) \ } while(0); #define RESET_TABLE_P(table) (table->size == 0) @@ -107,25 +107,25 @@ static bool rbs_is_untyped_params(method_params *params) { } /** - * Returns RBS::Location object of `current_token` of a parser state. + * Returns RBS::Location object of `current_token` of a parser parser. * - * @param state + * @param parser * @return New RBS::Location object. * */ -static rbs_location_t *rbs_location_current_token(parserstate *state) { - return rbs_location_new(&state->allocator, state->current_token.range); +static rbs_location_t *rbs_location_current_token(rbs_parser_t *parser) { + return rbs_location_new(&parser->allocator, parser->current_token.range); } -static bool parse_optional(parserstate *state, rbs_node_t **optional); -static bool parse_simple(parserstate *state, rbs_node_t **type); +static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional); +static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type); /** * @returns A borrowed copy of the current token, which does *not* need to be freed. */ -static rbs_string_t rbs_parser_peek_current_token(parserstate *state) { - range rg = state->current_token.range; +static rbs_string_t rbs_parser_peek_current_token(rbs_parser_t *parser) { + range rg = parser->current_token.range; - const char *start = state->lexstate->string.start + rg.start.byte_pos; + const char *start = parser->lexstate->string.start + rg.start.byte_pos; size_t length = rg.end.byte_pos - rg.start.byte_pos; return rbs_string_new(start, start + length); @@ -141,11 +141,11 @@ typedef enum { ALIAS_NAME = 4 } TypeNameKind; -static void parser_advance_no_gap(parserstate *state) { - if (state->current_token.range.end.byte_pos == state->next_token.range.start.byte_pos) { - parser_advance(state); +static void parser_advance_no_gap(rbs_parser_t *parser) { + if (parser->current_token.range.end.byte_pos == parser->next_token.range.start.byte_pos) { + parser_advance(parser); } else { - set_error(state, state->next_token, true, "unexpected token"); + set_error(parser, parser->next_token, true, "unexpected token"); } } @@ -155,43 +155,43 @@ static void parser_advance_no_gap(parserstate *state) { | {} */ NODISCARD -static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rbs_typename_t **typename) { +static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, range *rg, rbs_typename_t **typename) { bool absolute = false; if (rg) { - rg->start = state->current_token.range.start; + rg->start = parser->current_token.range.start; } - if (state->current_token.type == pCOLON2) { + if (parser->current_token.type == pCOLON2) { absolute = true; - parser_advance_no_gap(state); + parser_advance_no_gap(parser); } - rbs_node_list_t *path = rbs_node_list_new(&state->allocator); + rbs_node_list_t *path = rbs_node_list_new(&parser->allocator); while ( - state->current_token.type == tUIDENT - && state->next_token.type == pCOLON2 - && state->current_token.range.end.byte_pos == state->next_token.range.start.byte_pos - && state->next_token.range.end.byte_pos == state->next_token2.range.start.byte_pos + parser->current_token.type == tUIDENT + && parser->next_token.type == pCOLON2 + && parser->current_token.range.end.byte_pos == parser->next_token.range.start.byte_pos + && parser->next_token.range.end.byte_pos == parser->next_token2.range.start.byte_pos ) { - rbs_constant_id_t symbol_value = INTERN_TOKEN(state, state->current_token); - rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, state->next_token.range); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, symbol_value); + rbs_constant_id_t symbol_value = INTERN_TOKEN(parser, parser->current_token); + rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, parser->next_token.range); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, symbol_value); rbs_node_list_append(path, (rbs_node_t *)symbol); - parser_advance(state); - parser_advance(state); + parser_advance(parser); + parser_advance(parser); } range namespace_range = { .start = rg->start, - .end = state->current_token.range.end + .end = parser->current_token.range.end }; - rbs_location_t *loc = rbs_location_new(&state->allocator, namespace_range); - rbs_namespace_t *namespace = rbs_namespace_new(&state->allocator, loc, path, absolute); + rbs_location_t *loc = rbs_location_new(&parser->allocator, namespace_range); + rbs_namespace_t *namespace = rbs_namespace_new(&parser->allocator, loc, path, absolute); - switch (state->current_token.type) { + switch (parser->current_token.type) { case tLIDENT: if (kind & ALIAS_NAME) goto success; goto error_handling; @@ -207,13 +207,13 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb success: { if (rg) { - rg->end = state->current_token.range.end; + rg->end = parser->current_token.range.end; } - rbs_location_t *symbolLoc = rbs_location_current_token(state); - rbs_constant_id_t name = INTERN_TOKEN(state, state->current_token); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, name); - *typename = rbs_typename_new(&state->allocator, rbs_location_new(&state->allocator, *rg), namespace, symbol); + rbs_location_t *symbolLoc = rbs_location_current_token(parser); + rbs_constant_id_t name = INTERN_TOKEN(parser, parser->current_token); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, name); + *typename = rbs_typename_new(&parser->allocator, rbs_location_new(&parser->allocator, *rg), namespace, symbol); return true; } @@ -231,7 +231,7 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb rbs_assert(ids != NULL, "Unknown kind of type: %i", kind); - set_error(state, state->current_token, true, "expected one of %s", ids); + set_error(parser, parser->current_token, true, "expected one of %s", ids); return false; } } @@ -241,23 +241,23 @@ static bool parse_type_name(parserstate *state, TypeNameKind kind, range *rg, rb | {} type `,` ... `,` eol */ NODISCARD -static bool parse_type_list(parserstate *state, enum RBSTokenType eol, rbs_node_list_t *types) { +static bool parse_type_list(rbs_parser_t *parser, enum RBSTokenType eol, rbs_node_list_t *types) { while (true) { rbs_node_t *type; - CHECK_PARSE(parse_type(state, &type)); + CHECK_PARSE(parse_type(parser, &type)); rbs_node_list_append(types, type); - if (state->next_token.type == pCOMMA) { - parser_advance(state); + if (parser->next_token.type == pCOMMA) { + parser_advance(parser); - if (state->next_token.type == eol) { + if (parser->next_token.type == eol) { break; } } else { - if (state->next_token.type == eol) { + if (parser->next_token.type == eol) { break; } else { - set_error(state, state->next_token, true, "comma delimited type list is expected"); + set_error(parser, parser->next_token, true, "comma delimited type list is expected"); return false; } } @@ -287,57 +287,57 @@ static bool is_keyword_token(enum RBSTokenType type) { | {} type */ NODISCARD -static bool parse_function_param(parserstate *state, rbs_types_function_param_t **function_param) { +static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_t **function_param) { range type_range; - type_range.start = state->next_token.range.start; + type_range.start = parser->next_token.range.start; rbs_node_t *type; - CHECK_PARSE(parse_type(state, &type)); - type_range.end = state->current_token.range.end; + CHECK_PARSE(parse_type(parser, &type)); + type_range.end = parser->current_token.range.end; - if (state->next_token.type == pCOMMA || state->next_token.type == pRPAREN) { + if (parser->next_token.type == pCOMMA || parser->next_token.type == pRPAREN) { range param_range = type_range; - rbs_location_t *loc = rbs_location_new(&state->allocator, param_range); - rbs_loc_alloc_children(&state->allocator, loc, 1); + rbs_location_t *loc = rbs_location_new(&parser->allocator, param_range); + rbs_loc_alloc_children(&parser->allocator, loc, 1); rbs_loc_add_optional_child(loc, INTERN("name"), NULL_RANGE); - *function_param = rbs_types_function_param_new(&state->allocator, loc, type, NULL); + *function_param = rbs_types_function_param_new(&parser->allocator, loc, type, NULL); return true; } else { - range name_range = state->next_token.range; + range name_range = parser->next_token.range; - parser_advance(state); + parser_advance(parser); range param_range = { .start = type_range.start, .end = name_range.end, }; - if (!is_keyword_token(state->current_token.type)) { - set_error(state, state->current_token, true, "unexpected token for function parameter name"); + if (!is_keyword_token(parser->current_token.type)) { + set_error(parser, parser->current_token, true, "unexpected token for function parameter name"); return false; } - rbs_string_t unquoted_str = rbs_unquote_string(&state->allocator, rbs_parser_peek_current_token(state)); - rbs_location_t *symbolLoc = rbs_location_current_token(state); - rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str); - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); + rbs_string_t unquoted_str = rbs_unquote_string(&parser->allocator, rbs_parser_peek_current_token(parser)); + rbs_location_t *symbolLoc = rbs_location_current_token(parser); + rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_str); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, constant_id); - rbs_location_t *loc = rbs_location_new(&state->allocator, param_range); - rbs_loc_alloc_children(&state->allocator, loc, 1); + rbs_location_t *loc = rbs_location_new(&parser->allocator, param_range); + rbs_loc_alloc_children(&parser->allocator, loc, 1); rbs_loc_add_optional_child(loc, INTERN("name"), name_range); - *function_param = rbs_types_function_param_new(&state->allocator, loc, type, name); + *function_param = rbs_types_function_param_new(&parser->allocator, loc, type, name); return true; } } -static rbs_constant_id_t intern_token_start_end(parserstate *state, token start_token, token end_token) { +static rbs_constant_id_t intern_token_start_end(rbs_parser_t *parser, token start_token, token end_token) { return rbs_constant_pool_insert_shared_with_encoding( - &state->constant_pool, - (const uint8_t *) rbs_peek_token(state->lexstate, start_token), + &parser->constant_pool, + (const uint8_t *) rbs_peek_token(parser->lexstate, start_token), end_token.range.end.byte_pos - start_token.range.start.byte_pos, - state->lexstate->encoding + parser->lexstate->encoding ); } @@ -346,21 +346,21 @@ static rbs_constant_id_t intern_token_start_end(parserstate *state, token start_ | {} keyword <`?`> `:` */ NODISCARD -static bool parse_keyword_key(parserstate *state, rbs_ast_symbol_t **key) { - parser_advance(state); +static bool parse_keyword_key(rbs_parser_t *parser, rbs_ast_symbol_t **key) { + parser_advance(parser); - rbs_location_t *symbolLoc = rbs_location_current_token(state); + rbs_location_t *symbolLoc = rbs_location_current_token(parser); - if (state->next_token.type == pQUESTION) { + if (parser->next_token.type == pQUESTION) { *key = rbs_ast_symbol_new( - &state->allocator, + &parser->allocator, symbolLoc, - &state->constant_pool, - intern_token_start_end(state, state->current_token, state->next_token) + &parser->constant_pool, + intern_token_start_end(parser, parser->current_token, parser->next_token) ); - parser_advance(state); + parser_advance(parser); } else { - *key = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + *key = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); } return true; @@ -370,21 +370,21 @@ static bool parse_keyword_key(parserstate *state, rbs_ast_symbol_t **key) { keyword ::= {} keyword `:` */ NODISCARD -static bool parse_keyword(parserstate *state, rbs_hash_t *keywords, rbs_hash_t *memo) { +static bool parse_keyword(rbs_parser_t *parser, rbs_hash_t *keywords, rbs_hash_t *memo) { rbs_ast_symbol_t *key = NULL; - CHECK_PARSE(parse_keyword_key(state, &key)); + CHECK_PARSE(parse_keyword_key(parser, &key)); if (rbs_hash_find(memo, (rbs_node_t *) key)) { - set_error(state, state->current_token, true, "duplicated keyword argument"); + set_error(parser, parser->current_token, true, "duplicated keyword argument"); return false; } else { - rbs_location_t *loc = rbs_location_current_token(state); - rbs_hash_set(memo, (rbs_node_t *) key, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, loc, true)); + rbs_location_t *loc = rbs_location_current_token(parser); + rbs_hash_set(memo, (rbs_node_t *) key, (rbs_node_t *) rbs_ast_bool_new(&parser->allocator, loc, true)); } - ADVANCE_ASSERT(state, pCOLON); + ADVANCE_ASSERT(parser, pCOLON); rbs_types_function_param_t *param = NULL; - CHECK_PARSE(parse_function_param(state, ¶m)); + CHECK_PARSE(parse_function_param(parser, ¶m)); rbs_hash_set(keywords, (rbs_node_t *) key, (rbs_node_t *) param); @@ -396,16 +396,16 @@ Returns true if keyword is given. is_keyword === {} KEYWORD `:` */ -static bool is_keyword(parserstate *state) { - if (is_keyword_token(state->next_token.type)) { - if (state->next_token2.type == pCOLON && state->next_token.range.end.byte_pos == state->next_token2.range.start.byte_pos) { +static bool is_keyword(rbs_parser_t *parser) { + if (is_keyword_token(parser->next_token.type)) { + if (parser->next_token2.type == pCOLON && parser->next_token.range.end.byte_pos == parser->next_token2.range.start.byte_pos) { return true; } - if (state->next_token2.type == pQUESTION - && state->next_token3.type == pCOLON - && state->next_token.range.end.byte_pos == state->next_token2.range.start.byte_pos - && state->next_token2.range.end.byte_pos == state->next_token3.range.start.byte_pos) { + if (parser->next_token2.type == pQUESTION + && parser->next_token3.type == pCOLON + && parser->next_token.range.end.byte_pos == parser->next_token2.range.start.byte_pos + && parser->next_token2.range.end.byte_pos == parser->next_token3.range.start.byte_pos) { return true; } } @@ -415,13 +415,13 @@ static bool is_keyword(parserstate *state) { /** * Advance token if _next_ token is `type`. - * Ensures one token advance and `state->current_token.type == type`, or current token not changed. + * Ensures one token advance and `parser->current_token.type == type`, or current token not changed. * * @returns true if token advances, false otherwise. **/ -static bool parser_advance_if(parserstate *state, enum RBSTokenType type) { - if (state->next_token.type == type) { - parser_advance(state); +static bool parser_advance_if(rbs_parser_t *parser, enum RBSTokenType type) { + if (parser->next_token.type == type) { + parser_advance(parser); return true; } else { return false; @@ -458,20 +458,20 @@ static bool parser_advance_if(parserstate *state, enum RBSTokenType type) { | {} `**` */ NODISCARD -static bool parse_params(parserstate *state, method_params *params) { - if (state->next_token.type == pQUESTION && state->next_token2.type == pRPAREN) { +static bool parse_params(rbs_parser_t *parser, method_params *params) { + if (parser->next_token.type == pQUESTION && parser->next_token2.type == pRPAREN) { params->required_positionals = NULL; - parser_advance(state); + parser_advance(parser); return true; } - if (state->next_token.type == pRPAREN) { + if (parser->next_token.type == pRPAREN) { return true; } - rbs_hash_t *memo = rbs_hash_new(&state->allocator); + rbs_hash_t *memo = rbs_hash_new(&parser->allocator); while (true) { - switch (state->next_token.type) { + switch (parser->next_token.type) { case pQUESTION: goto PARSE_OPTIONAL_PARAMS; case pSTAR: @@ -482,36 +482,36 @@ static bool parse_params(parserstate *state, method_params *params) { goto EOP; default: - if (is_keyword(state)) { + if (is_keyword(parser)) { goto PARSE_KEYWORDS; } rbs_types_function_param_t *param = NULL; - CHECK_PARSE(parse_function_param(state, ¶m)); + CHECK_PARSE(parse_function_param(parser, ¶m)); rbs_node_list_append(params->required_positionals, (rbs_node_t *)param); break; } - if (!parser_advance_if(state, pCOMMA)) { + if (!parser_advance_if(parser, pCOMMA)) { goto EOP; } } PARSE_OPTIONAL_PARAMS: while (true) { - switch (state->next_token.type) { + switch (parser->next_token.type) { case pQUESTION: - parser_advance(state); + parser_advance(parser); - if (is_keyword(state)) { - CHECK_PARSE(parse_keyword(state, params->optional_keywords, memo)); - parser_advance_if(state, pCOMMA); + if (is_keyword(parser)) { + CHECK_PARSE(parse_keyword(parser, params->optional_keywords, memo)); + parser_advance_if(parser, pCOMMA); goto PARSE_KEYWORDS; } rbs_types_function_param_t *param = NULL; - CHECK_PARSE(parse_function_param(state, ¶m)); + CHECK_PARSE(parse_function_param(parser, ¶m)); rbs_node_list_append(params->optional_positionals, (rbs_node_t *)param); break; @@ -519,19 +519,19 @@ static bool parse_params(parserstate *state, method_params *params) { goto PARSE_REST_PARAM; } - if (!parser_advance_if(state, pCOMMA)) { + if (!parser_advance_if(parser, pCOMMA)) { goto EOP; } } PARSE_REST_PARAM: - if (state->next_token.type == pSTAR) { - parser_advance(state); + if (parser->next_token.type == pSTAR) { + parser_advance(parser); rbs_types_function_param_t *param = NULL; - CHECK_PARSE(parse_function_param(state, ¶m)); + CHECK_PARSE(parse_function_param(parser, ¶m)); params->rest_positionals = (rbs_node_t *) param; - if (!parser_advance_if(state, pCOMMA)) { + if (!parser_advance_if(parser, pCOMMA)) { goto EOP; } } @@ -539,7 +539,7 @@ static bool parse_params(parserstate *state, method_params *params) { PARSE_TRAILING_PARAMS: while (true) { - switch (state->next_token.type) { + switch (parser->next_token.type) { case pQUESTION: goto PARSE_KEYWORDS; case pSTAR: @@ -550,39 +550,39 @@ static bool parse_params(parserstate *state, method_params *params) { goto EOP; default: - if (is_keyword(state)) { + if (is_keyword(parser)) { goto PARSE_KEYWORDS; } rbs_types_function_param_t *param = NULL; - CHECK_PARSE(parse_function_param(state, ¶m)); + CHECK_PARSE(parse_function_param(parser, ¶m)); rbs_node_list_append(params->trailing_positionals, (rbs_node_t *)param); break; } - if (!parser_advance_if(state, pCOMMA)) { + if (!parser_advance_if(parser, pCOMMA)) { goto EOP; } } PARSE_KEYWORDS: while (true) { - switch (state->next_token.type) { + switch (parser->next_token.type) { case pQUESTION: - parser_advance(state); - if (is_keyword(state)) { - CHECK_PARSE(parse_keyword(state, params->optional_keywords, memo)); + parser_advance(parser); + if (is_keyword(parser)) { + CHECK_PARSE(parse_keyword(parser, params->optional_keywords, memo)); } else { - set_error(state, state->next_token, true, "optional keyword argument type is expected"); + set_error(parser, parser->next_token, true, "optional keyword argument type is expected"); return false; } break; case pSTAR2: - parser_advance(state); + parser_advance(parser); rbs_types_function_param_t *param = NULL; - CHECK_PARSE(parse_function_param(state, ¶m)); + CHECK_PARSE(parse_function_param(parser, ¶m)); params->rest_keywords = (rbs_node_t *) param; break; @@ -593,10 +593,10 @@ static bool parse_params(parserstate *state, method_params *params) { case tULLIDENT: case tBANGIDENT: KEYWORD_CASES - if (is_keyword(state)) { - CHECK_PARSE(parse_keyword(state, params->required_keywords, memo)); + if (is_keyword(parser)) { + CHECK_PARSE(parse_keyword(parser, params->required_keywords, memo)); } else { - set_error(state, state->next_token, true, "required keyword argument type is expected"); + set_error(parser, parser->next_token, true, "required keyword argument type is expected"); return false; } break; @@ -605,14 +605,14 @@ static bool parse_params(parserstate *state, method_params *params) { goto EOP; } - if (!parser_advance_if(state, pCOMMA)) { + if (!parser_advance_if(parser, pCOMMA)) { goto EOP; } } EOP: - if (state->next_token.type != pRPAREN) { - set_error(state, state->next_token, true, "unexpected token for method type parameters"); + if (parser->next_token.type != pRPAREN) { + set_error(parser, parser->next_token, true, "unexpected token for method type parameters"); return false; } @@ -624,18 +624,18 @@ static bool parse_params(parserstate *state, method_params *params) { | {} simple_type <`?`> */ NODISCARD -static bool parse_optional(parserstate *state, rbs_node_t **optional) { +static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional) { range rg; - rg.start = state->next_token.range.start; + rg.start = parser->next_token.range.start; rbs_node_t *type = NULL; - CHECK_PARSE(parse_simple(state, &type)); + CHECK_PARSE(parse_simple(parser, &type)); - if (state->next_token.type == pQUESTION) { - parser_advance(state); - rg.end = state->current_token.range.end; - rbs_location_t *location = rbs_location_new(&state->allocator, rg); - *optional = (rbs_node_t *) rbs_types_optional_new(&state->allocator, location, type); + if (parser->next_token.type == pQUESTION) { + parser_advance(parser); + rg.end = parser->current_token.range.end; + rbs_location_t *location = rbs_location_new(&parser->allocator, rg); + *optional = (rbs_node_t *) rbs_types_optional_new(&parser->allocator, location, type); } else { *optional = type; } @@ -660,14 +660,14 @@ static void initialize_method_params(method_params *params, rbs_allocator_t *all | {} `[` `self` `:` type <`]`> */ NODISCARD -static bool parse_self_type_binding(parserstate *state, rbs_node_t **self_type) { - if (state->next_token.type == pLBRACKET) { - parser_advance(state); - ADVANCE_ASSERT(state, kSELF); - ADVANCE_ASSERT(state, pCOLON); +static bool parse_self_type_binding(rbs_parser_t *parser, rbs_node_t **self_type) { + if (parser->next_token.type == pLBRACKET) { + parser_advance(parser); + ADVANCE_ASSERT(parser, kSELF); + ADVANCE_ASSERT(parser, pCOLON); rbs_node_t *type; - CHECK_PARSE(parse_type(state, &type)); - ADVANCE_ASSERT(state, pRBRACKET); + CHECK_PARSE(parse_type(parser, &type)); + ADVANCE_ASSERT(parser, pRBRACKET); *self_type = type; } @@ -688,67 +688,67 @@ typedef struct { | {} self_type_binding? `->` */ NODISCARD -static bool parse_function(parserstate *state, bool accept_type_binding, parse_function_result **result) { +static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse_function_result **result) { rbs_node_t *function = NULL; rbs_types_block_t *block = NULL; rbs_node_t *function_self_type = NULL; range function_range; - function_range.start = state->current_token.range.start; + function_range.start = parser->current_token.range.start; method_params params; - initialize_method_params(¶ms, &state->allocator); + initialize_method_params(¶ms, &parser->allocator); - if (state->next_token.type == pLPAREN) { - parser_advance(state); - CHECK_PARSE(parse_params(state, ¶ms)); - ADVANCE_ASSERT(state, pRPAREN); + if (parser->next_token.type == pLPAREN) { + parser_advance(parser); + CHECK_PARSE(parse_params(parser, ¶ms)); + ADVANCE_ASSERT(parser, pRPAREN); } // Passing NULL to function_self_type means the function itself doesn't accept self type binding. (== method type) if (accept_type_binding) { - CHECK_PARSE(parse_self_type_binding(state, &function_self_type)); + CHECK_PARSE(parse_self_type_binding(parser, &function_self_type)); } else { if (rbs_is_untyped_params(¶ms)) { - if (state->next_token.type != pARROW) { - set_error(state, state->next_token2, true, "A method type with untyped method parameter cannot have block"); + if (parser->next_token.type != pARROW) { + set_error(parser, parser->next_token2, true, "A method type with untyped method parameter cannot have block"); return false; } } } bool required = true; - if (state->next_token.type == pQUESTION && state->next_token2.type == pLBRACE) { + if (parser->next_token.type == pQUESTION && parser->next_token2.type == pLBRACE) { // Optional block required = false; - parser_advance(state); + parser_advance(parser); } - if (state->next_token.type == pLBRACE) { - parser_advance(state); + if (parser->next_token.type == pLBRACE) { + parser_advance(parser); method_params block_params; - initialize_method_params(&block_params, &state->allocator); + initialize_method_params(&block_params, &parser->allocator); - if (state->next_token.type == pLPAREN) { - parser_advance(state); - CHECK_PARSE(parse_params(state, &block_params)); - ADVANCE_ASSERT(state, pRPAREN); + if (parser->next_token.type == pLPAREN) { + parser_advance(parser); + CHECK_PARSE(parse_params(parser, &block_params)); + ADVANCE_ASSERT(parser, pRPAREN); } rbs_node_t *self_type = NULL; - CHECK_PARSE(parse_self_type_binding(state, &self_type)); + CHECK_PARSE(parse_self_type_binding(parser, &self_type)); - ADVANCE_ASSERT(state, pARROW); + ADVANCE_ASSERT(parser, pARROW); rbs_node_t *block_return_type = NULL; - CHECK_PARSE(parse_optional(state, &block_return_type)); + CHECK_PARSE(parse_optional(parser, &block_return_type)); rbs_node_t *block_function = NULL; - function_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, function_range); + function_range.end = parser->current_token.range.end; + rbs_location_t *loc = rbs_location_new(&parser->allocator, function_range); if (rbs_is_untyped_params(&block_params)) { - block_function = (rbs_node_t *) rbs_types_untypedfunction_new(&state->allocator, loc, block_return_type); + block_function = (rbs_node_t *) rbs_types_untypedfunction_new(&parser->allocator, loc, block_return_type); } else { block_function = (rbs_node_t *) rbs_types_function_new( - &state->allocator, + &parser->allocator, loc, block_params.required_positionals, block_params.optional_positionals, @@ -761,22 +761,22 @@ static bool parse_function(parserstate *state, bool accept_type_binding, parse_f ); } - block = rbs_types_block_new(&state->allocator, loc, block_function, required, self_type); + block = rbs_types_block_new(&parser->allocator, loc, block_function, required, self_type); - ADVANCE_ASSERT(state, pRBRACE); + ADVANCE_ASSERT(parser, pRBRACE); } - ADVANCE_ASSERT(state, pARROW); + ADVANCE_ASSERT(parser, pARROW); rbs_node_t *type = NULL; - CHECK_PARSE(parse_optional(state, &type)); + CHECK_PARSE(parse_optional(parser, &type)); - function_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, function_range); + function_range.end = parser->current_token.range.end; + rbs_location_t *loc = rbs_location_new(&parser->allocator, function_range); if (rbs_is_untyped_params(¶ms)) { - function = (rbs_node_t *) rbs_types_untypedfunction_new(&state->allocator, loc, type); + function = (rbs_node_t *) rbs_types_untypedfunction_new(&parser->allocator, loc, type); } else { function = (rbs_node_t *) rbs_types_function_new( - &state->allocator, + &parser->allocator, loc, params.required_positionals, params.optional_positionals, @@ -799,20 +799,20 @@ static bool parse_function(parserstate *state, bool accept_type_binding, parse_f proc_type ::= {`^`} */ NODISCARD -static bool parse_proc_type(parserstate *state, rbs_types_proc_t **proc) { - position start = state->current_token.range.start; - parse_function_result *result = rbs_allocator_alloc(&state->allocator, parse_function_result); - CHECK_PARSE(parse_function(state, true, &result)); - - position end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, (range) { .start = start, .end = end }); - *proc = rbs_types_proc_new(&state->allocator, loc, result->function, result->block, result->function_self_type); +static bool parse_proc_type(rbs_parser_t *parser, rbs_types_proc_t **proc) { + position start = parser->current_token.range.start; + parse_function_result *result = rbs_allocator_alloc(&parser->allocator, parse_function_result); + CHECK_PARSE(parse_function(parser, true, &result)); + + position end = parser->current_token.range.end; + rbs_location_t *loc = rbs_location_new(&parser->allocator, (range) { .start = start, .end = end }); + *proc = rbs_types_proc_new(&parser->allocator, loc, result->function, result->block, result->function_self_type); return true; } -static void check_key_duplication(parserstate *state, rbs_hash_t *fields, rbs_node_t *key) { +static void check_key_duplication(rbs_parser_t *parser, rbs_hash_t *fields, rbs_node_t *key) { if (rbs_hash_find(fields, ((rbs_node_t *) key))) { - set_error(state, state->current_token, true, "duplicated record key"); + set_error(parser, parser->current_token, true, "duplicated record key"); } } @@ -827,30 +827,30 @@ static void check_key_duplication(parserstate *state, rbs_hash_t *fields, rbs_no | {} literal_type `=>` */ NODISCARD -static bool parse_record_attributes(parserstate *state, rbs_hash_t **fields) { - *fields = rbs_hash_new(&state->allocator); +static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields) { + *fields = rbs_hash_new(&parser->allocator); - if (state->next_token.type == pRBRACE) return true; + if (parser->next_token.type == pRBRACE) return true; while (true) { rbs_ast_symbol_t *key = NULL; bool required = true; - if (state->next_token.type == pQUESTION) { + if (parser->next_token.type == pQUESTION) { // { ?foo: type } syntax required = false; - parser_advance(state); + parser_advance(parser); } - if (is_keyword(state)) { + if (is_keyword(parser)) { // { foo: type } syntax - CHECK_PARSE(parse_keyword_key(state, &key)); + CHECK_PARSE(parse_keyword_key(parser, &key)); - check_key_duplication(state, *fields, (rbs_node_t *) key); - ADVANCE_ASSERT(state, pCOLON); + check_key_duplication(parser, *fields, (rbs_node_t *) key); + ADVANCE_ASSERT(parser, pCOLON); } else { // { key => type } syntax - switch (state->next_token.type) { + switch (parser->next_token.type) { case tSYMBOL: case tSQSYMBOL: case tDQSYMBOL: @@ -860,31 +860,31 @@ static bool parse_record_attributes(parserstate *state, rbs_hash_t **fields) { case kTRUE: case kFALSE: { rbs_node_t *type = NULL; - CHECK_PARSE(parse_simple(state, &type)); + CHECK_PARSE(parse_simple(parser, &type)); key = (rbs_ast_symbol_t *) ((rbs_types_literal_t *) type)->literal; break; } default: - set_error(state, state->next_token, true, "unexpected record key token"); + set_error(parser, parser->next_token, true, "unexpected record key token"); return false; } - check_key_duplication(state, *fields, (rbs_node_t *) key); - ADVANCE_ASSERT(state, pFATARROW); + check_key_duplication(parser, *fields, (rbs_node_t *) key); + ADVANCE_ASSERT(parser, pFATARROW); } range field_range; - field_range.start = state->current_token.range.end; + field_range.start = parser->current_token.range.end; rbs_node_t *type; - CHECK_PARSE(parse_type(state, &type)); + CHECK_PARSE(parse_type(parser, &type)); - field_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, field_range); - rbs_hash_set(*fields, (rbs_node_t *) key, (rbs_node_t *) rbs_types_record_fieldtype_new(&state->allocator, loc, type, required)); + field_range.end = parser->current_token.range.end; + rbs_location_t *loc = rbs_location_new(&parser->allocator, field_range); + rbs_hash_set(*fields, (rbs_node_t *) key, (rbs_node_t *) rbs_types_record_fieldtype_new(&parser->allocator, loc, type, required)); - if (parser_advance_if(state, pCOMMA)) { - if (state->next_token.type == pRBRACE) { + if (parser_advance_if(parser, pCOMMA)) { + if (parser->next_token.type == pRBRACE) { break; } } else { @@ -898,46 +898,46 @@ static bool parse_record_attributes(parserstate *state, rbs_hash_t **fields) { symbol ::= {} */ NODISCARD -static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types_literal_t **symbol) { - size_t offset_bytes = state->lexstate->encoding->char_width((const uint8_t *) ":", (size_t) 1); - size_t bytes = rbs_token_bytes(state->current_token) - offset_bytes; +static bool parse_symbol(rbs_parser_t *parser, rbs_location_t *location, rbs_types_literal_t **symbol) { + size_t offset_bytes = parser->lexstate->encoding->char_width((const uint8_t *) ":", (size_t) 1); + size_t bytes = rbs_token_bytes(parser->current_token) - offset_bytes; rbs_ast_symbol_t *literal; - switch (state->current_token.type) + switch (parser->current_token.type) { case tSYMBOL: { - rbs_location_t *symbolLoc = rbs_location_current_token(state); + rbs_location_t *symbolLoc = rbs_location_current_token(parser); - char *buffer = rbs_peek_token(state->lexstate, state->current_token); + char *buffer = rbs_peek_token(parser->lexstate, parser->current_token); rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared( - &state->constant_pool, + &parser->constant_pool, (const uint8_t *) buffer+offset_bytes, bytes ); - literal = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); + literal = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, constant_id); break; } case tDQSYMBOL: case tSQSYMBOL: { - rbs_location_t *symbolLoc = rbs_location_current_token(state); - rbs_string_t current_token = rbs_parser_peek_current_token(state); + rbs_location_t *symbolLoc = rbs_location_current_token(parser); + rbs_string_t current_token = rbs_parser_peek_current_token(parser); rbs_string_t symbol = rbs_string_new(current_token.start + offset_bytes, current_token.end); - rbs_string_t unquoted_symbol = rbs_unquote_string(&state->allocator, symbol); + rbs_string_t unquoted_symbol = rbs_unquote_string(&parser->allocator, symbol); - rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&state->constant_pool, unquoted_symbol); + rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_symbol); - literal = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); + literal = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, constant_id); break; } default: - set_error(state, state->current_token, false, "Unexpected error"); + set_error(parser, parser->current_token, false, "Unexpected error"); return false; } - *symbol = rbs_types_literal_new(&state->allocator, location, (rbs_node_t *) literal); + *symbol = rbs_types_literal_new(&parser->allocator, location, (rbs_node_t *) literal); return true; } @@ -948,7 +948,7 @@ static bool parse_symbol(parserstate *state, rbs_location_t *location, rbs_types | {} `[` type_list <`]`> */ NODISCARD -static bool parse_instance_type(parserstate *state, bool parse_alias, rbs_node_t **type) { +static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node_t **type) { TypeNameKind expected_kind = INTERFACE_NAME | CLASS_NAME; if (parse_alias) { expected_kind |= ALIAS_NAME; @@ -956,29 +956,29 @@ static bool parse_instance_type(parserstate *state, bool parse_alias, rbs_node_t range name_range; rbs_typename_t *typename = NULL; - CHECK_PARSE(parse_type_name(state, expected_kind, &name_range, &typename)); + CHECK_PARSE(parse_type_name(parser, expected_kind, &name_range, &typename)); - rbs_node_list_t *types = rbs_node_list_new(&state->allocator); + rbs_node_list_t *types = rbs_node_list_new(&parser->allocator); TypeNameKind kind; - if (state->current_token.type == tUIDENT) { + if (parser->current_token.type == tUIDENT) { kind = CLASS_NAME; - } else if (state->current_token.type == tULIDENT) { + } else if (parser->current_token.type == tULIDENT) { kind = INTERFACE_NAME; - } else if (state->current_token.type == tLIDENT) { + } else if (parser->current_token.type == tLIDENT) { kind = ALIAS_NAME; } else { - set_error(state, state->current_token, false, "Unexpected error"); + set_error(parser, parser->current_token, false, "Unexpected error"); return false; } range args_range; - if (state->next_token.type == pLBRACKET) { - parser_advance(state); - args_range.start = state->current_token.range.start; - CHECK_PARSE(parse_type_list(state, pRBRACKET, types)); - ADVANCE_ASSERT(state, pRBRACKET); - args_range.end = state->current_token.range.end; + if (parser->next_token.type == pLBRACKET) { + parser_advance(parser); + args_range.start = parser->current_token.range.start; + CHECK_PARSE(parse_type_list(parser, pRBRACKET, types)); + ADVANCE_ASSERT(parser, pRBRACKET); + args_range.end = parser->current_token.range.end; } else { args_range = NULL_RANGE; } @@ -988,17 +988,17 @@ static bool parse_instance_type(parserstate *state, bool parse_alias, rbs_node_t .end = rbs_nonnull_pos_or(args_range.end, name_range.end), }; - rbs_location_t *loc = rbs_location_new(&state->allocator, type_range); - rbs_loc_alloc_children(&state->allocator, loc, 2); + rbs_location_t *loc = rbs_location_new(&parser->allocator, type_range); + rbs_loc_alloc_children(&parser->allocator, loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); if (kind == CLASS_NAME) { - *type = (rbs_node_t *) rbs_types_classinstance_new(&state->allocator, loc, typename, types); + *type = (rbs_node_t *) rbs_types_classinstance_new(&parser->allocator, loc, typename, types); } else if (kind == INTERFACE_NAME) { - *type = (rbs_node_t *) rbs_types_interface_new(&state->allocator, loc, typename, types); + *type = (rbs_node_t *) rbs_types_interface_new(&parser->allocator, loc, typename, types); } else if (kind == ALIAS_NAME) { - *type = (rbs_node_t *) rbs_types_alias_new(&state->allocator, loc, typename, types); + *type = (rbs_node_t *) rbs_types_alias_new(&parser->allocator, loc, typename, types); } return true; @@ -1008,26 +1008,26 @@ static bool parse_instance_type(parserstate *state, bool parse_alias, rbs_node_t singleton_type ::= {`singleton`} `(` type_name <`)`> */ NODISCARD -static bool parse_singleton_type(parserstate *state, rbs_types_classsingleton_t **singleton) { - ASSERT_TOKEN(state, kSINGLETON); +static bool parse_singleton_type(rbs_parser_t *parser, rbs_types_classsingleton_t **singleton) { + ASSERT_TOKEN(parser, kSINGLETON); range type_range; - type_range.start = state->current_token.range.start; - ADVANCE_ASSERT(state, pLPAREN); - parser_advance(state); + type_range.start = parser->current_token.range.start; + ADVANCE_ASSERT(parser, pLPAREN); + parser_advance(parser); range name_range; rbs_typename_t *typename = NULL; - CHECK_PARSE(parse_type_name(state, CLASS_NAME, &name_range, &typename)); + CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &name_range, &typename)); - ADVANCE_ASSERT(state, pRPAREN); - type_range.end = state->current_token.range.end; + ADVANCE_ASSERT(parser, pRPAREN); + type_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, type_range); - rbs_loc_alloc_children(&state->allocator, loc, 1); + rbs_location_t *loc = rbs_location_new(&parser->allocator, type_range); + rbs_loc_alloc_children(&parser->allocator, loc, 1); rbs_loc_add_required_child(loc, INTERN("name"), name_range); - *singleton = rbs_types_classsingleton_new(&state->allocator, loc, typename); + *singleton = rbs_types_classsingleton_new(&parser->allocator, loc, typename); return true; } @@ -1036,8 +1036,8 @@ static bool parse_singleton_type(parserstate *state, rbs_types_classsingleton_t * If not found, it goes one table up, if it's not a reset table. * Or returns false, if it's a reset table. * */ -static bool parser_typevar_member(parserstate *state, rbs_constant_id_t id) { - id_table *table = state->vars; +static bool parser_typevar_member(rbs_parser_t *parser, rbs_constant_id_t id) { + id_table *table = parser->vars; while (table && !RESET_TABLE_P(table)) { for (size_t i = 0; i < table->count; i++) { @@ -1063,115 +1063,115 @@ static bool parser_typevar_member(parserstate *state, rbs_constant_id_t id) { | {} `^` */ NODISCARD -static bool parse_simple(parserstate *state, rbs_node_t **type) { - parser_advance(state); +static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) { + parser_advance(parser); - switch (state->current_token.type) { + switch (parser->current_token.type) { case pLPAREN: { rbs_node_t *lparen_type; - CHECK_PARSE(parse_type(state, &lparen_type)); - ADVANCE_ASSERT(state, pRPAREN); + CHECK_PARSE(parse_type(parser, &lparen_type)); + ADVANCE_ASSERT(parser, pRPAREN); *type = lparen_type; return true; } case kBOOL: { - rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_bases_bool_new(&state->allocator, loc); + rbs_location_t *loc = rbs_location_current_token(parser); + *type = (rbs_node_t *) rbs_types_bases_bool_new(&parser->allocator, loc); return true; } case kBOT: { - rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_bases_bottom_new(&state->allocator, loc); + rbs_location_t *loc = rbs_location_current_token(parser); + *type = (rbs_node_t *) rbs_types_bases_bottom_new(&parser->allocator, loc); return true; } case kCLASS: { - rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_bases_class_new(&state->allocator, loc); + rbs_location_t *loc = rbs_location_current_token(parser); + *type = (rbs_node_t *) rbs_types_bases_class_new(&parser->allocator, loc); return true; } case kINSTANCE: { - rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_bases_instance_new(&state->allocator, loc); + rbs_location_t *loc = rbs_location_current_token(parser); + *type = (rbs_node_t *) rbs_types_bases_instance_new(&parser->allocator, loc); return true; } case kNIL: { - rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_bases_nil_new(&state->allocator, loc); + rbs_location_t *loc = rbs_location_current_token(parser); + *type = (rbs_node_t *) rbs_types_bases_nil_new(&parser->allocator, loc); return true; } case kSELF: { - rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_bases_self_new(&state->allocator, loc); + rbs_location_t *loc = rbs_location_current_token(parser); + *type = (rbs_node_t *) rbs_types_bases_self_new(&parser->allocator, loc); return true; } case kTOP: { - rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_bases_top_new(&state->allocator, loc); + rbs_location_t *loc = rbs_location_current_token(parser); + *type = (rbs_node_t *) rbs_types_bases_top_new(&parser->allocator, loc); return true; } case kVOID: { - rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_bases_void_new(&state->allocator, loc); + rbs_location_t *loc = rbs_location_current_token(parser); + *type = (rbs_node_t *) rbs_types_bases_void_new(&parser->allocator, loc); return true; } case kUNTYPED: { - rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, loc, false); + rbs_location_t *loc = rbs_location_current_token(parser); + *type = (rbs_node_t *) rbs_types_bases_any_new(&parser->allocator, loc, false); return true; } case k__TODO__: { - rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_bases_any_new(&state->allocator, loc, true); + rbs_location_t *loc = rbs_location_current_token(parser); + *type = (rbs_node_t *) rbs_types_bases_any_new(&parser->allocator, loc, true); return true; } case tINTEGER: { - rbs_location_t *loc = rbs_location_current_token(state); + rbs_location_t *loc = rbs_location_current_token(parser); - rbs_string_t string = rbs_parser_peek_current_token(state); + rbs_string_t string = rbs_parser_peek_current_token(parser); rbs_string_t stripped_string = rbs_string_strip_whitespace( &string); - rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(&state->allocator, loc, stripped_string); - *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, loc, literal); + rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(&parser->allocator, loc, stripped_string); + *type = (rbs_node_t *) rbs_types_literal_new(&parser->allocator, loc, literal); return true; } case kTRUE: { - rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, loc, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, loc, true)); + rbs_location_t *loc = rbs_location_current_token(parser); + *type = (rbs_node_t *) rbs_types_literal_new(&parser->allocator, loc, (rbs_node_t *) rbs_ast_bool_new(&parser->allocator, loc, true)); return true; } case kFALSE: { - rbs_location_t *loc = rbs_location_current_token(state); - *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, loc, (rbs_node_t *) rbs_ast_bool_new(&state->allocator, loc, false)); + rbs_location_t *loc = rbs_location_current_token(parser); + *type = (rbs_node_t *) rbs_types_literal_new(&parser->allocator, loc, (rbs_node_t *) rbs_ast_bool_new(&parser->allocator, loc, false)); return true; } case tSQSTRING: case tDQSTRING: { - rbs_location_t *loc = rbs_location_current_token(state); + rbs_location_t *loc = rbs_location_current_token(parser); - rbs_string_t unquoted_str = rbs_unquote_string(&state->allocator, rbs_parser_peek_current_token(state)); - rbs_node_t *literal = (rbs_node_t *) rbs_ast_string_new(&state->allocator, loc, unquoted_str); - *type = (rbs_node_t *) rbs_types_literal_new(&state->allocator, loc, literal); + rbs_string_t unquoted_str = rbs_unquote_string(&parser->allocator, rbs_parser_peek_current_token(parser)); + rbs_node_t *literal = (rbs_node_t *) rbs_ast_string_new(&parser->allocator, loc, unquoted_str); + *type = (rbs_node_t *) rbs_types_literal_new(&parser->allocator, loc, literal); return true; } case tSYMBOL: case tSQSYMBOL: case tDQSYMBOL: { - rbs_location_t *loc = rbs_location_current_token(state); + rbs_location_t *loc = rbs_location_current_token(parser); rbs_types_literal_t *literal = NULL; - CHECK_PARSE(parse_symbol(state, loc, &literal)); + CHECK_PARSE(parse_symbol(parser, loc, &literal)); *type = (rbs_node_t *) literal; return true; } case tUIDENT: { - const char *name_str = rbs_peek_token(state->lexstate, state->current_token); - size_t name_len = rbs_token_bytes(state->current_token); + const char *name_str = rbs_peek_token(parser->lexstate, parser->current_token); + size_t name_len = rbs_token_bytes(parser->current_token); - rbs_constant_id_t name = rbs_constant_pool_find(&state->constant_pool, (const uint8_t *) name_str, name_len); + rbs_constant_id_t name = rbs_constant_pool_find(&parser->constant_pool, (const uint8_t *) name_str, name_len); - if (parser_typevar_member(state, name)) { - rbs_location_t *loc = rbs_location_current_token(state); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, loc, &state->constant_pool, name); - *type = (rbs_node_t *) rbs_types_variable_new(&state->allocator, loc, symbol); + if (parser_typevar_member(parser, name)) { + rbs_location_t *loc = rbs_location_current_token(parser); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&parser->allocator, loc, &parser->constant_pool, name); + *type = (rbs_node_t *) rbs_types_variable_new(&parser->allocator, loc, symbol); return true; } @@ -1181,54 +1181,54 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { case tLIDENT: case pCOLON2: { rbs_node_t *instance_type = NULL; - CHECK_PARSE(parse_instance_type(state, true, &instance_type)); + CHECK_PARSE(parse_instance_type(parser, true, &instance_type)); *type = instance_type; return true; } case kSINGLETON: { rbs_types_classsingleton_t *singleton = NULL; - CHECK_PARSE(parse_singleton_type(state, &singleton)); + CHECK_PARSE(parse_singleton_type(parser, &singleton)); *type = (rbs_node_t *) singleton; return true; } case pLBRACKET: { range rg; - rg.start = state->current_token.range.start; - rbs_node_list_t *types = rbs_node_list_new(&state->allocator); - if (state->next_token.type != pRBRACKET) { - CHECK_PARSE(parse_type_list(state, pRBRACKET, types)); + rg.start = parser->current_token.range.start; + rbs_node_list_t *types = rbs_node_list_new(&parser->allocator); + if (parser->next_token.type != pRBRACKET) { + CHECK_PARSE(parse_type_list(parser, pRBRACKET, types)); } - ADVANCE_ASSERT(state, pRBRACKET); - rg.end = state->current_token.range.end; + ADVANCE_ASSERT(parser, pRBRACKET); + rg.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, rg); - *type = (rbs_node_t *) rbs_types_tuple_new(&state->allocator, loc, types); + rbs_location_t *loc = rbs_location_new(&parser->allocator, rg); + *type = (rbs_node_t *) rbs_types_tuple_new(&parser->allocator, loc, types); return true; } case pAREF_OPR: { - rbs_location_t *loc = rbs_location_current_token(state); - rbs_node_list_t *types = rbs_node_list_new(&state->allocator); - *type = (rbs_node_t *) rbs_types_tuple_new(&state->allocator, loc, types); + rbs_location_t *loc = rbs_location_current_token(parser); + rbs_node_list_t *types = rbs_node_list_new(&parser->allocator); + *type = (rbs_node_t *) rbs_types_tuple_new(&parser->allocator, loc, types); return true; } case pLBRACE: { - position start = state->current_token.range.start; + position start = parser->current_token.range.start; rbs_hash_t *fields = NULL; - CHECK_PARSE(parse_record_attributes(state, &fields)); - ADVANCE_ASSERT(state, pRBRACE); - position end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, (range) { .start = start, .end = end }); - *type = (rbs_node_t *) rbs_types_record_new(&state->allocator, loc, fields); + CHECK_PARSE(parse_record_attributes(parser, &fields)); + ADVANCE_ASSERT(parser, pRBRACE); + position end = parser->current_token.range.end; + rbs_location_t *loc = rbs_location_new(&parser->allocator, (range) { .start = start, .end = end }); + *type = (rbs_node_t *) rbs_types_record_new(&parser->allocator, loc, fields); return true; } case pHAT: { rbs_types_proc_t *value = NULL; - CHECK_PARSE(parse_proc_type(state, &value)); + CHECK_PARSE(parse_proc_type(parser, &value)); *type = (rbs_node_t *) value; return true; } default: - set_error(state, state->current_token, true, "unexpected token for simple type"); + set_error(parser, parser->current_token, true, "unexpected token for simple type"); return false; } } @@ -1238,29 +1238,29 @@ static bool parse_simple(parserstate *state, rbs_node_t **type) { | {} */ NODISCARD -static bool parse_intersection(parserstate *state, rbs_node_t **type) { +static bool parse_intersection(rbs_parser_t *parser, rbs_node_t **type) { range rg; - rg.start = state->next_token.range.start; + rg.start = parser->next_token.range.start; rbs_node_t *optional = NULL; - CHECK_PARSE(parse_optional(state, &optional)); + CHECK_PARSE(parse_optional(parser, &optional)); *type = optional; - rbs_node_list_t *intersection_types = rbs_node_list_new(&state->allocator); + rbs_node_list_t *intersection_types = rbs_node_list_new(&parser->allocator); rbs_node_list_append(intersection_types, optional); - while (state->next_token.type == pAMP) { - parser_advance(state); + while (parser->next_token.type == pAMP) { + parser_advance(parser); rbs_node_t *type = NULL; - CHECK_PARSE(parse_optional(state, &type)); + CHECK_PARSE(parse_optional(parser, &type)); rbs_node_list_append(intersection_types, type); } - rg.end = state->current_token.range.end; + rg.end = parser->current_token.range.end; if (intersection_types->length > 1) { - rbs_location_t *location = rbs_location_new(&state->allocator, rg); - *type = (rbs_node_t *) rbs_types_intersection_new(&state->allocator, location, intersection_types); + rbs_location_t *location = rbs_location_new(&parser->allocator, rg); + *type = (rbs_node_t *) rbs_types_intersection_new(&parser->allocator, location, intersection_types); } return true; @@ -1270,27 +1270,27 @@ static bool parse_intersection(parserstate *state, rbs_node_t **type) { union ::= {} intersection '|' ... '|' | {} */ -bool parse_type(parserstate *state, rbs_node_t **type) { +bool parse_type(rbs_parser_t *parser, rbs_node_t **type) { range rg; - rg.start = state->next_token.range.start; - rbs_node_list_t *union_types = rbs_node_list_new(&state->allocator); + rg.start = parser->next_token.range.start; + rbs_node_list_t *union_types = rbs_node_list_new(&parser->allocator); - CHECK_PARSE(parse_intersection(state, type)); + CHECK_PARSE(parse_intersection(parser, type)); rbs_node_list_append(union_types, *type); - while (state->next_token.type == pBAR) { - parser_advance(state); + while (parser->next_token.type == pBAR) { + parser_advance(parser); rbs_node_t *intersection = NULL; - CHECK_PARSE(parse_intersection(state, &intersection)); + CHECK_PARSE(parse_intersection(parser, &intersection)); rbs_node_list_append(union_types, intersection); } - rg.end = state->current_token.range.end; + rg.end = parser->current_token.range.end; if (union_types->length > 1) { - rbs_location_t *location = rbs_location_new(&state->allocator, rg); - *type = (rbs_node_t *) rbs_types_union_new(&state->allocator, location, union_types); + rbs_location_t *location = rbs_location_new(&parser->allocator, rg); + *type = (rbs_node_t *) rbs_types_union_new(&parser->allocator, location, union_types); } return true; @@ -1305,113 +1305,113 @@ bool parse_type(parserstate *state, rbs_node_t **type) { type_param ::= tUIDENT upper_bound? default_type? (module_type_params == false) */ NODISCARD -static bool parse_type_params(parserstate *state, range *rg, bool module_type_params, rbs_node_list_t **params) { - *params = rbs_node_list_new(&state->allocator); +static bool parse_type_params(rbs_parser_t *parser, range *rg, bool module_type_params, rbs_node_list_t **params) { + *params = rbs_node_list_new(&parser->allocator); bool required_param_allowed = true; - if (state->next_token.type == pLBRACKET) { - parser_advance(state); + if (parser->next_token.type == pLBRACKET) { + parser_advance(parser); - rg->start = state->current_token.range.start; + rg->start = parser->current_token.range.start; while (true) { bool unchecked = false; - rbs_keyword_t *variance = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("invariant")); + rbs_keyword_t *variance = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("invariant")); rbs_node_t *upper_bound = NULL; rbs_node_t *default_type = NULL; range param_range; - param_range.start = state->next_token.range.start; + param_range.start = parser->next_token.range.start; range unchecked_range = NULL_RANGE; range variance_range = NULL_RANGE; if (module_type_params) { - if (state->next_token.type == kUNCHECKED) { + if (parser->next_token.type == kUNCHECKED) { unchecked = true; - parser_advance(state); - unchecked_range = state->current_token.range; + parser_advance(parser); + unchecked_range = parser->current_token.range; } - if (state->next_token.type == kIN || state->next_token.type == kOUT) { - switch (state->next_token.type) { + if (parser->next_token.type == kIN || parser->next_token.type == kOUT) { + switch (parser->next_token.type) { case kIN: - variance = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("contravariant")); + variance = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("contravariant")); break; case kOUT: - variance = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("covariant")); + variance = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("covariant")); break; default: - set_error(state, state->current_token, false, "Unexpected error"); + set_error(parser, parser->current_token, false, "Unexpected error"); return false; } - parser_advance(state); - variance_range = state->current_token.range; + parser_advance(parser); + variance_range = parser->current_token.range; } } - ADVANCE_ASSERT(state, tUIDENT); - range name_range = state->current_token.range; + ADVANCE_ASSERT(parser, tUIDENT); + range name_range = parser->current_token.range; - rbs_string_t string = rbs_parser_peek_current_token(state); - rbs_location_t *nameSymbolLoc = rbs_location_current_token(state); - rbs_constant_id_t id = rbs_constant_pool_insert_string(&state->constant_pool, string); - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, nameSymbolLoc, &state->constant_pool, id); + rbs_string_t string = rbs_parser_peek_current_token(parser); + rbs_location_t *nameSymbolLoc = rbs_location_current_token(parser); + rbs_constant_id_t id = rbs_constant_pool_insert_string(&parser->constant_pool, string); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&parser->allocator, nameSymbolLoc, &parser->constant_pool, id); - CHECK_PARSE(parser_insert_typevar(state, id)); + CHECK_PARSE(parser_insert_typevar(parser, id)); range upper_bound_range = NULL_RANGE; - if (state->next_token.type == pLT) { - parser_advance(state); - upper_bound_range.start = state->current_token.range.start; - CHECK_PARSE(parse_type(state, &upper_bound)); - upper_bound_range.end = state->current_token.range.end; + if (parser->next_token.type == pLT) { + parser_advance(parser); + upper_bound_range.start = parser->current_token.range.start; + CHECK_PARSE(parse_type(parser, &upper_bound)); + upper_bound_range.end = parser->current_token.range.end; } range default_type_range = NULL_RANGE; if (module_type_params) { - if (state->next_token.type == pEQ) { - parser_advance(state); + if (parser->next_token.type == pEQ) { + parser_advance(parser); - default_type_range.start = state->current_token.range.start; - CHECK_PARSE(parse_type(state, &default_type)); - default_type_range.end = state->current_token.range.end; + default_type_range.start = parser->current_token.range.start; + CHECK_PARSE(parse_type(parser, &default_type)); + default_type_range.end = parser->current_token.range.end; required_param_allowed = false; } else { if (!required_param_allowed) { - set_error(state, state->current_token, true, "required type parameter is not allowed after optional type parameter"); + set_error(parser, parser->current_token, true, "required type parameter is not allowed after optional type parameter"); return false; } } } - param_range.end = state->current_token.range.end; + param_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, param_range); - rbs_loc_alloc_children(&state->allocator, loc, 5); + rbs_location_t *loc = rbs_location_new(&parser->allocator, param_range); + rbs_loc_alloc_children(&parser->allocator, loc, 5); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("variance"), variance_range); rbs_loc_add_optional_child(loc, INTERN("unchecked"), unchecked_range); rbs_loc_add_optional_child(loc, INTERN("upper_bound"), upper_bound_range); rbs_loc_add_optional_child(loc, INTERN("default"), default_type_range); - rbs_ast_typeparam_t *param = rbs_ast_typeparam_new(&state->allocator, loc, name, variance, upper_bound, default_type, unchecked); + rbs_ast_typeparam_t *param = rbs_ast_typeparam_new(&parser->allocator, loc, name, variance, upper_bound, default_type, unchecked); rbs_node_list_append(*params, (rbs_node_t *) param); - if (state->next_token.type == pCOMMA) { - parser_advance(state); + if (parser->next_token.type == pCOMMA) { + parser_advance(parser); } - if (state->next_token.type == pRBRACKET) { + if (parser->next_token.type == pRBRACKET) { break; } } - ADVANCE_ASSERT(state, pRBRACKET); - rg->end = state->current_token.range.end; + ADVANCE_ASSERT(parser, pRBRACKET); + rg->end = parser->current_token.range.end; } else { *rg = NULL_RANGE; } @@ -1420,20 +1420,20 @@ static bool parse_type_params(parserstate *state, range *rg, bool module_type_pa } NODISCARD -static bool parser_pop_typevar_table(parserstate *state) { +static bool parser_pop_typevar_table(rbs_parser_t *parser) { id_table *table; - if (state->vars) { - table = state->vars; - state->vars = table->next; + if (parser->vars) { + table = parser->vars; + parser->vars = table->next; } else { - set_error(state, state->current_token, false, "Cannot pop empty table"); + set_error(parser, parser->current_token, false, "Cannot pop empty table"); return false; } - if (state->vars && RESET_TABLE_P(state->vars)) { - table = state->vars; - state->vars = table->next; + if (parser->vars && RESET_TABLE_P(parser->vars)) { + table = parser->vars; + parser->vars = table->next; } return true; @@ -1443,33 +1443,33 @@ static bool parser_pop_typevar_table(parserstate *state) { method_type ::= {} type_params */ // TODO: Should this be NODISCARD? -bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type) { - parser_push_typevar_table(state, false); +bool parse_method_type(rbs_parser_t *parser, rbs_methodtype_t **method_type) { + parser_push_typevar_table(parser, false); range rg; - rg.start = state->next_token.range.start; + rg.start = parser->next_token.range.start; range params_range = NULL_RANGE; rbs_node_list_t *type_params; - CHECK_PARSE(parse_type_params(state, ¶ms_range, false, &type_params)); + CHECK_PARSE(parse_type_params(parser, ¶ms_range, false, &type_params)); range type_range; - type_range.start = state->next_token.range.start; + type_range.start = parser->next_token.range.start; - parse_function_result *result = rbs_allocator_alloc(&state->allocator, parse_function_result); - CHECK_PARSE(parse_function(state, false, &result)); + parse_function_result *result = rbs_allocator_alloc(&parser->allocator, parse_function_result); + CHECK_PARSE(parse_function(parser, false, &result)); - rg.end = state->current_token.range.end; + rg.end = parser->current_token.range.end; type_range.end = rg.end; - CHECK_PARSE(parser_pop_typevar_table(state)); + CHECK_PARSE(parser_pop_typevar_table(parser)); - rbs_location_t *loc = rbs_location_new(&state->allocator, rg); - rbs_loc_alloc_children(&state->allocator, loc, 2); + rbs_location_t *loc = rbs_location_new(&parser->allocator, rg); + rbs_loc_alloc_children(&parser->allocator, loc, 2); rbs_loc_add_required_child(loc, INTERN("type"), type_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range); - *method_type = rbs_methodtype_new(&state->allocator, loc, type_params, result->function, result->block); + *method_type = rbs_methodtype_new(&parser->allocator, loc, type_params, result->function, result->block); return true; } @@ -1477,30 +1477,30 @@ bool parse_method_type(parserstate *state, rbs_methodtype_t **method_type) { global_decl ::= {tGIDENT} `:` */ NODISCARD -static bool parse_global_decl(parserstate *state, rbs_node_list_t *annotations, rbs_ast_declarations_global_t **global) { +static bool parse_global_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_ast_declarations_global_t **global) { range decl_range; - decl_range.start = state->current_token.range.start; + decl_range.start = parser->current_token.range.start; - rbs_ast_comment_t *comment = get_comment(state, decl_range.start.line); + rbs_ast_comment_t *comment = get_comment(parser, decl_range.start.line); - range name_range = state->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, name_range); + range name_range = parser->current_token.range; + rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, name_range); - rbs_ast_symbol_t *typename = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + rbs_ast_symbol_t *typename = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); - ADVANCE_ASSERT(state, pCOLON); - range colon_range = state->current_token.range; + ADVANCE_ASSERT(parser, pCOLON); + range colon_range = parser->current_token.range; rbs_node_t *type; - CHECK_PARSE(parse_type(state, &type)); - decl_range.end = state->current_token.range.end; + CHECK_PARSE(parse_type(parser, &type)); + decl_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); - rbs_loc_alloc_children(&state->allocator, loc, 2); + rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); + rbs_loc_alloc_children(&parser->allocator, loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - *global = rbs_ast_declarations_global_new(&state->allocator, loc, typename, type, comment, annotations); + *global = rbs_ast_declarations_global_new(&parser->allocator, loc, typename, type, comment, annotations); return true; } @@ -1508,30 +1508,30 @@ static bool parse_global_decl(parserstate *state, rbs_node_list_t *annotations, const_decl ::= {const_name} `:` */ NODISCARD -static bool parse_const_decl(parserstate *state, rbs_node_list_t *annotations, rbs_ast_declarations_constant_t **constant) { +static bool parse_const_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_ast_declarations_constant_t **constant) { range decl_range; - decl_range.start = state->current_token.range.start; - rbs_ast_comment_t *comment = get_comment(state, decl_range.start.line); + decl_range.start = parser->current_token.range.start; + rbs_ast_comment_t *comment = get_comment(parser, decl_range.start.line); range name_range; rbs_typename_t *typename = NULL; - CHECK_PARSE(parse_type_name(state, CLASS_NAME, &name_range, &typename)); + CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &name_range, &typename)); - ADVANCE_ASSERT(state, pCOLON); - range colon_range = state->current_token.range; + ADVANCE_ASSERT(parser, pCOLON); + range colon_range = parser->current_token.range; rbs_node_t *type; - CHECK_PARSE(parse_type(state, &type)); + CHECK_PARSE(parse_type(parser, &type)); - decl_range.end = state->current_token.range.end; + decl_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); - rbs_loc_alloc_children(&state->allocator, loc, 2); + rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); + rbs_loc_alloc_children(&parser->allocator, loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - *constant = rbs_ast_declarations_constant_new(&state->allocator, loc, typename, type, comment, annotations); + *constant = rbs_ast_declarations_constant_new(&parser->allocator, loc, typename, type, comment, annotations); return true; } @@ -1539,45 +1539,45 @@ static bool parse_const_decl(parserstate *state, rbs_node_list_t *annotations, r type_decl ::= {kTYPE} alias_name `=` */ NODISCARD -static bool parse_type_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_typealias_t **typealias) { - parser_push_typevar_table(state, true); +static bool parse_type_decl(rbs_parser_t *parser, position comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_typealias_t **typealias) { + parser_push_typevar_table(parser, true); range decl_range; - decl_range.start = state->current_token.range.start; + decl_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, decl_range.start); - range keyword_range = state->current_token.range; + range keyword_range = parser->current_token.range; - parser_advance(state); + parser_advance(parser); range name_range; rbs_typename_t *typename = NULL; - CHECK_PARSE(parse_type_name(state, ALIAS_NAME, &name_range, &typename)); + CHECK_PARSE(parse_type_name(parser, ALIAS_NAME, &name_range, &typename)); range params_range; rbs_node_list_t *type_params; - CHECK_PARSE(parse_type_params(state, ¶ms_range, true, &type_params)); + CHECK_PARSE(parse_type_params(parser, ¶ms_range, true, &type_params)); - ADVANCE_ASSERT(state, pEQ); - range eq_range = state->current_token.range; + ADVANCE_ASSERT(parser, pEQ); + range eq_range = parser->current_token.range; rbs_node_t *type; - CHECK_PARSE(parse_type(state, &type)); + CHECK_PARSE(parse_type(parser, &type)); - decl_range.end = state->current_token.range.end; + decl_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); - rbs_loc_alloc_children(&state->allocator, loc, 4); + rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); + rbs_loc_alloc_children(&parser->allocator, loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range); rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); - CHECK_PARSE(parser_pop_typevar_table(state)); + CHECK_PARSE(parser_pop_typevar_table(parser)); - rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); - *typealias = rbs_ast_declarations_typealias_new(&state->allocator, loc, typename, type_params, type, annotations, comment); + *typealias = rbs_ast_declarations_typealias_new(&parser->allocator, loc, typename, type_params, type, annotations, comment); return true; } @@ -1585,16 +1585,16 @@ static bool parse_type_decl(parserstate *state, position comment_pos, rbs_node_l annotation ::= {} */ NODISCARD -static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotation) { - range rg = state->current_token.range; +static bool parse_annotation(rbs_parser_t *parser, rbs_ast_annotation_t **annotation) { + range rg = parser->current_token.range; size_t offset_bytes = - state->lexstate->encoding->char_width((const uint8_t *) "%", (size_t) 1) + - state->lexstate->encoding->char_width((const uint8_t *) "a", (size_t) 1); + parser->lexstate->encoding->char_width((const uint8_t *) "%", (size_t) 1) + + parser->lexstate->encoding->char_width((const uint8_t *) "a", (size_t) 1); rbs_string_t str = rbs_string_new( - state->lexstate->string.start + rg.start.byte_pos + offset_bytes, - state->lexstate->string.end + parser->lexstate->string.start + rg.start.byte_pos + offset_bytes, + parser->lexstate->string.end ); unsigned int open_char = rbs_utf8_to_codepoint(str); @@ -1617,14 +1617,14 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati close_char = '|'; break; default: - set_error(state, state->current_token, false, "Unexpected error"); + set_error(parser, parser->current_token, false, "Unexpected error"); return false; } - size_t open_bytes = state->lexstate->encoding->char_width((const uint8_t *) &open_char, (size_t) 1); - size_t close_bytes = state->lexstate->encoding->char_width((const uint8_t *) &close_char, (size_t) 1); + size_t open_bytes = parser->lexstate->encoding->char_width((const uint8_t *) &open_char, (size_t) 1); + size_t close_bytes = parser->lexstate->encoding->char_width((const uint8_t *) &close_char, (size_t) 1); - rbs_string_t current_token = rbs_parser_peek_current_token(state); + rbs_string_t current_token = rbs_parser_peek_current_token(parser); size_t total_offset = offset_bytes + open_bytes; rbs_string_t annotation_str = rbs_string_new( @@ -1634,7 +1634,7 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati rbs_string_t stripped_annotation_str = rbs_string_strip_whitespace(&annotation_str); - *annotation = rbs_ast_annotation_new(&state->allocator, rbs_location_new(&state->allocator, rg), stripped_annotation_str); + *annotation = rbs_ast_annotation_new(&parser->allocator, rbs_location_new(&parser->allocator, rg), stripped_annotation_str); return true; } @@ -1643,19 +1643,19 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati | {<>} */ NODISCARD -static bool parse_annotations(parserstate *state, rbs_node_list_t *annotations, position *annot_pos) { +static bool parse_annotations(rbs_parser_t *parser, rbs_node_list_t *annotations, position *annot_pos) { *annot_pos = NullPosition; while (true) { - if (state->next_token.type == tANNOTATION) { - parser_advance(state); + if (parser->next_token.type == tANNOTATION) { + parser_advance(parser); if (rbs_null_position_p((*annot_pos))) { - *annot_pos = state->current_token.range.start; + *annot_pos = parser->current_token.range.start; } rbs_ast_annotation_t *annotation = NULL; - CHECK_PARSE(parse_annotation(state, &annotation)); + CHECK_PARSE(parse_annotation(parser, &annotation)); rbs_node_list_append(annotations, (rbs_node_t *) annotation); } else { break; @@ -1670,50 +1670,50 @@ static bool parse_annotations(parserstate *state, rbs_node_list_t *annotations, | {} (IDENT | keyword)~<`?`> */ NODISCARD -static bool parse_method_name(parserstate *state, range *range, rbs_ast_symbol_t **symbol) { - parser_advance(state); +static bool parse_method_name(rbs_parser_t *parser, range *range, rbs_ast_symbol_t **symbol) { + parser_advance(parser); - switch (state->current_token.type) + switch (parser->current_token.type) { case tUIDENT: case tLIDENT: case tULIDENT: case tULLIDENT: KEYWORD_CASES - if (state->next_token.type == pQUESTION && state->current_token.range.end.byte_pos == state->next_token.range.start.byte_pos) { - range->start = state->current_token.range.start; - range->end = state->next_token.range.end; - parser_advance(state); + if (parser->next_token.type == pQUESTION && parser->current_token.range.end.byte_pos == parser->next_token.range.start.byte_pos) { + range->start = parser->current_token.range.start; + range->end = parser->next_token.range.end; + parser_advance(parser); rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared_with_encoding( - &state->constant_pool, - (const uint8_t *) state->lexstate->string.start + range->start.byte_pos, + &parser->constant_pool, + (const uint8_t *) parser->lexstate->string.start + range->start.byte_pos, range->end.byte_pos - range->start.byte_pos, - state->lexstate->encoding + parser->lexstate->encoding ); - rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, *range); - *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); + rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, *range); + *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, constant_id); } else { - *range = state->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, *range); - *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + *range = parser->current_token.range; + rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, *range); + *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); } return true; case tBANGIDENT: case tEQIDENT: { - *range = state->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, *range); - *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + *range = parser->current_token.range; + rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, *range); + *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); return true; } case tQIDENT: { - rbs_string_t string = rbs_parser_peek_current_token(state); - rbs_string_t unquoted_str = rbs_unquote_string(&state->allocator, string); - rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&state->constant_pool, unquoted_str); - rbs_location_t *symbolLoc = rbs_location_current_token(state); - *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, constant_id); + rbs_string_t string = rbs_parser_peek_current_token(parser); + rbs_string_t unquoted_str = rbs_unquote_string(&parser->allocator, string); + rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_str); + rbs_location_t *symbolLoc = rbs_location_current_token(parser); + *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, constant_id); return true; } @@ -1725,14 +1725,14 @@ static bool parse_method_name(parserstate *state, range *range, rbs_ast_symbol_t case pLT: case pAREF_OPR: case tOPERATOR: { - *range = state->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, *range); - *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + *range = parser->current_token.range; + rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, *range); + *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); return true; } default: - set_error(state, state->current_token, true, "unexpected token for method name"); + set_error(parser, parser->current_token, true, "unexpected token for method name"); return false; } } @@ -1750,30 +1750,30 @@ typedef enum { @param allow_selfq `true` to accept `self?` kind. */ -static InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, bool allow_selfq, range *rg) { +static InstanceSingletonKind parse_instance_singleton_kind(rbs_parser_t *parser, bool allow_selfq, range *rg) { InstanceSingletonKind kind = INSTANCE_KIND; - if (state->next_token.type == kSELF) { - range self_range = state->next_token.range; + if (parser->next_token.type == kSELF) { + range self_range = parser->next_token.range; - if (state->next_token2.type == pDOT) { - parser_advance(state); - parser_advance(state); + if (parser->next_token2.type == pDOT) { + parser_advance(parser); + parser_advance(parser); kind = SINGLETON_KIND; } else if ( - state->next_token2.type == pQUESTION - && state->next_token.range.end.char_pos == state->next_token2.range.start.char_pos - && state->next_token3.type == pDOT + parser->next_token2.type == pQUESTION + && parser->next_token.range.end.char_pos == parser->next_token2.range.start.char_pos + && parser->next_token3.type == pDOT && allow_selfq) { - parser_advance(state); - parser_advance(state); - parser_advance(state); + parser_advance(parser); + parser_advance(parser); + parser_advance(parser); kind = INSTANCE_SINGLETON_KIND; } *rg = (range) { .start = self_range.start, - .end = state->current_token.range.end, + .end = parser->current_token.range.end, }; } else { *rg = NULL_RANGE; @@ -1795,29 +1795,29 @@ static InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, b * @param accept_overload `true` to accept overloading (...) definition. * */ NODISCARD -static bool parse_member_def(parserstate *state, bool instance_only, bool accept_overload, position comment_pos, rbs_node_list_t *annotations, rbs_ast_members_methoddefinition_t **method_definition) { +static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool accept_overload, position comment_pos, rbs_node_list_t *annotations, rbs_ast_members_methoddefinition_t **method_definition) { range member_range; - member_range.start = state->current_token.range.start; + member_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); - rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); range visibility_range; rbs_keyword_t *visibility; - switch (state->current_token.type) + switch (parser->current_token.type) { case kPRIVATE: { - visibility_range = state->current_token.range; - visibility = rbs_keyword_new(&state->allocator, rbs_location_new(&state->allocator, visibility_range), INTERN("private")); + visibility_range = parser->current_token.range; + visibility = rbs_keyword_new(&parser->allocator, rbs_location_new(&parser->allocator, visibility_range), INTERN("private")); member_range.start = visibility_range.start; - parser_advance(state); + parser_advance(parser); break; } case kPUBLIC: { - visibility_range = state->current_token.range; - visibility = rbs_keyword_new(&state->allocator, rbs_location_new(&state->allocator, visibility_range), INTERN("public")); + visibility_range = parser->current_token.range; + visibility = rbs_keyword_new(&parser->allocator, rbs_location_new(&parser->allocator, visibility_range), INTERN("public")); member_range.start = visibility_range.start; - parser_advance(state); + parser_advance(parser); break; } default: @@ -1826,7 +1826,7 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept break; } - range keyword_range = state->current_token.range; + range keyword_range = parser->current_token.range; range kind_range; InstanceSingletonKind kind; @@ -1834,40 +1834,40 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept kind_range = NULL_RANGE; kind = INSTANCE_KIND; } else { - kind = parse_instance_singleton_kind(state, visibility == NULL, &kind_range); + kind = parse_instance_singleton_kind(parser, visibility == NULL, &kind_range); } range name_range; rbs_ast_symbol_t *name = NULL; - CHECK_PARSE(parse_method_name(state, &name_range, &name)); + CHECK_PARSE(parse_method_name(parser, &name_range, &name)); - #define SELF_ID rbs_constant_pool_insert_constant(&state->constant_pool, (const unsigned char *) "self?", strlen("self?")) + #define SELF_ID rbs_constant_pool_insert_constant(&parser->constant_pool, (const unsigned char *) "self?", strlen("self?")) - if (state->next_token.type == pDOT && name->constant_id == SELF_ID) { - set_error(state, state->next_token, true, "`self?` method cannot have visibility"); + if (parser->next_token.type == pDOT && name->constant_id == SELF_ID) { + set_error(parser, parser->next_token, true, "`self?` method cannot have visibility"); return false; } else { - ADVANCE_ASSERT(state, pCOLON); + ADVANCE_ASSERT(parser, pCOLON); } - parser_push_typevar_table(state, kind != INSTANCE_KIND); + parser_push_typevar_table(parser, kind != INSTANCE_KIND); - rbs_node_list_t *overloads = rbs_node_list_new(&state->allocator); + rbs_node_list_t *overloads = rbs_node_list_new(&parser->allocator); bool overloading = false; range overloading_range = NULL_RANGE; bool loop = true; while (loop) { - rbs_node_list_t *annotations = rbs_node_list_new(&state->allocator); + rbs_node_list_t *annotations = rbs_node_list_new(&parser->allocator); position overload_annot_pos = NullPosition; range overload_range; - overload_range.start = state->current_token.range.start; + overload_range.start = parser->current_token.range.start; - if (state->next_token.type == tANNOTATION) { - CHECK_PARSE(parse_annotations(state, annotations, &overload_annot_pos)); + if (parser->next_token.type == tANNOTATION) { + CHECK_PARSE(parse_annotations(parser, annotations, &overload_annot_pos)); } - switch (state->next_token.type) { + switch (parser->next_token.type) { case pLPAREN: case pARROW: case pLBRACE: @@ -1875,71 +1875,71 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept case pQUESTION: { rbs_methodtype_t *method_type = NULL; - CHECK_PARSE(parse_method_type(state, &method_type)); + CHECK_PARSE(parse_method_type(parser, &method_type)); - overload_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, overload_range); - rbs_node_t *overload = (rbs_node_t *) rbs_ast_members_methoddefinition_overload_new(&state->allocator, loc, annotations, (rbs_node_t *) method_type); + overload_range.end = parser->current_token.range.end; + rbs_location_t *loc = rbs_location_new(&parser->allocator, overload_range); + rbs_node_t *overload = (rbs_node_t *) rbs_ast_members_methoddefinition_overload_new(&parser->allocator, loc, annotations, (rbs_node_t *) method_type); rbs_node_list_append(overloads, overload); - member_range.end = state->current_token.range.end; + member_range.end = parser->current_token.range.end; break; } case pDOT3: if (accept_overload) { overloading = true; - parser_advance(state); + parser_advance(parser); loop = false; - overloading_range = state->current_token.range; + overloading_range = parser->current_token.range; member_range.end = overloading_range.end; break; } else { - set_error(state, state->next_token, true, "unexpected overloading method definition"); + set_error(parser, parser->next_token, true, "unexpected overloading method definition"); return false; } default: - set_error(state, state->next_token, true, "unexpected token for method type"); + set_error(parser, parser->next_token, true, "unexpected token for method type"); return false; } - if (state->next_token.type == pBAR) { - parser_advance(state); + if (parser->next_token.type == pBAR) { + parser_advance(parser); } else { loop = false; } } - CHECK_PARSE(parser_pop_typevar_table(state)); + CHECK_PARSE(parser_pop_typevar_table(parser)); rbs_keyword_t *k; switch (kind) { case INSTANCE_KIND: { - k = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("instance")); + k = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("instance")); break; } case SINGLETON_KIND: { - k = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("singleton")); + k = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("singleton")); break; } case INSTANCE_SINGLETON_KIND: { - k = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("singleton_instance")); + k = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("singleton_instance")); break; } default: - set_error(state, state->current_token, false, "Unexpected error"); + set_error(parser, parser->current_token, false, "Unexpected error"); return false; } - rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(&state->allocator, loc, 5); + rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); + rbs_loc_alloc_children(&parser->allocator, loc, 5); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range); rbs_loc_add_optional_child(loc, INTERN("overloading"), overloading_range); rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range); - *method_definition = rbs_ast_members_methoddefinition_new(&state->allocator, loc, name, k, overloads, annotations, comment, overloading, visibility); + *method_definition = rbs_ast_members_methoddefinition_new(&parser->allocator, loc, name, k, overloads, annotations, comment, overloading, visibility); return true; } @@ -1950,19 +1950,19 @@ static bool parse_member_def(parserstate *state, bool instance_only, bool accept * @param kind * */ NODISCARD -static bool class_instance_name(parserstate *state, TypeNameKind kind, rbs_node_list_t *args, range *name_range, range *args_range, rbs_typename_t **name) { - parser_advance(state); +static bool class_instance_name(rbs_parser_t *parser, TypeNameKind kind, rbs_node_list_t *args, range *name_range, range *args_range, rbs_typename_t **name) { + parser_advance(parser); rbs_typename_t *typename = NULL; - CHECK_PARSE(parse_type_name(state, kind, name_range, &typename)); + CHECK_PARSE(parse_type_name(parser, kind, name_range, &typename)); *name = typename; - if (state->next_token.type == pLBRACKET) { - parser_advance(state); - args_range->start = state->current_token.range.start; - CHECK_PARSE(parse_type_list(state, pRBRACKET, args)); - ADVANCE_ASSERT(state, pRBRACKET); - args_range->end = state->current_token.range.end; + if (parser->next_token.type == pLBRACKET) { + parser_advance(parser); + args_range->start = parser->current_token.range.start; + CHECK_PARSE(parse_type_list(parser, pRBRACKET, args)); + ADVANCE_ASSERT(parser, pRBRACKET); + args_range->end = parser->current_token.range.end; } else { *args_range = NULL_RANGE; } @@ -1978,13 +1978,13 @@ static bool class_instance_name(parserstate *state, TypeNameKind kind, rbs_node_ * @param from_interface `true` when the member is in an interface. * */ NODISCARD -static bool parse_mixin_member(parserstate *state, bool from_interface, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **mixin_member) { +static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **mixin_member) { range member_range; - member_range.start = state->current_token.range.start; + member_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); - enum RBSTokenType type = state->current_token.type; - range keyword_range = state->current_token.range; + enum RBSTokenType type = parser->current_token.type; + range keyword_range = parser->current_token.range; bool reset_typevar_scope; switch (type) @@ -1999,53 +1999,53 @@ static bool parse_mixin_member(parserstate *state, bool from_interface, position reset_typevar_scope = false; break; default: - set_error(state, state->current_token, false, "Unexpected error"); + set_error(parser, parser->current_token, false, "Unexpected error"); return false; } if (from_interface) { - if (state->current_token.type != kINCLUDE) { - set_error(state, state->current_token, true, "unexpected mixin in interface declaration"); + if (parser->current_token.type != kINCLUDE) { + set_error(parser, parser->current_token, true, "unexpected mixin in interface declaration"); return false; } } - parser_push_typevar_table(state, reset_typevar_scope); + parser_push_typevar_table(parser, reset_typevar_scope); - rbs_node_list_t *args = rbs_node_list_new(&state->allocator); + rbs_node_list_t *args = rbs_node_list_new(&parser->allocator); range name_range; range args_range = NULL_RANGE; rbs_typename_t *name = NULL; CHECK_PARSE(class_instance_name( - state, + parser, from_interface ? INTERFACE_NAME : (INTERFACE_NAME | CLASS_NAME), args, &name_range, &args_range, &name )); - CHECK_PARSE(parser_pop_typevar_table(state)); + CHECK_PARSE(parser_pop_typevar_table(parser)); - member_range.end = state->current_token.range.end; + member_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(&state->allocator, loc, 3); + rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); + rbs_loc_alloc_children(&parser->allocator, loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); - rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); switch (type) { case kINCLUDE: - *mixin_member = (rbs_node_t *) rbs_ast_members_include_new(&state->allocator, loc, name, args, annotations, comment); + *mixin_member = (rbs_node_t *) rbs_ast_members_include_new(&parser->allocator, loc, name, args, annotations, comment); return true; case kEXTEND: - *mixin_member = (rbs_node_t *) rbs_ast_members_extend_new(&state->allocator, loc, name, args, annotations, comment); + *mixin_member = (rbs_node_t *) rbs_ast_members_extend_new(&parser->allocator, loc, name, args, annotations, comment); return true; case kPREPEND: - *mixin_member = (rbs_node_t *) rbs_ast_members_prepend_new(&state->allocator, loc, name, args, annotations, comment); + *mixin_member = (rbs_node_t *) rbs_ast_members_prepend_new(&parser->allocator, loc, name, args, annotations, comment); return true; default: - set_error(state, state->current_token, false, "Unexpected error"); + set_error(parser, parser->current_token, false, "Unexpected error"); return false; } } @@ -2059,50 +2059,50 @@ static bool parse_mixin_member(parserstate *state, bool from_interface, position * @param[in] instance_only `true` to reject `self.` alias. * */ NODISCARD -static bool parse_alias_member(parserstate *state, bool instance_only, position comment_pos, rbs_node_list_t *annotations, rbs_ast_members_alias_t **alias_member) { +static bool parse_alias_member(rbs_parser_t *parser, bool instance_only, position comment_pos, rbs_node_list_t *annotations, rbs_ast_members_alias_t **alias_member) { range member_range; - member_range.start = state->current_token.range.start; - range keyword_range = state->current_token.range; + member_range.start = parser->current_token.range.start; + range keyword_range = parser->current_token.range; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); - rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); rbs_keyword_t *kind; rbs_ast_symbol_t *new_name, *old_name; range new_kind_range, old_kind_range, new_name_range, old_name_range; - if (!instance_only && state->next_token.type == kSELF) { - kind = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("singleton")); + if (!instance_only && parser->next_token.type == kSELF) { + kind = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("singleton")); - new_kind_range.start = state->next_token.range.start; - new_kind_range.end = state->next_token2.range.end; - ADVANCE_ASSERT(state, kSELF); - ADVANCE_ASSERT(state, pDOT); - CHECK_PARSE(parse_method_name(state, &new_name_range, &new_name)); + new_kind_range.start = parser->next_token.range.start; + new_kind_range.end = parser->next_token2.range.end; + ADVANCE_ASSERT(parser, kSELF); + ADVANCE_ASSERT(parser, pDOT); + CHECK_PARSE(parse_method_name(parser, &new_name_range, &new_name)); - old_kind_range.start = state->next_token.range.start; - old_kind_range.end = state->next_token2.range.end; - ADVANCE_ASSERT(state, kSELF); - ADVANCE_ASSERT(state, pDOT); - CHECK_PARSE(parse_method_name(state, &old_name_range, &old_name)); + old_kind_range.start = parser->next_token.range.start; + old_kind_range.end = parser->next_token2.range.end; + ADVANCE_ASSERT(parser, kSELF); + ADVANCE_ASSERT(parser, pDOT); + CHECK_PARSE(parse_method_name(parser, &old_name_range, &old_name)); } else { - kind = rbs_keyword_new(&state->allocator, rbs_location_current_token(state), INTERN("instance")); - CHECK_PARSE(parse_method_name(state, &new_name_range, &new_name)); - CHECK_PARSE(parse_method_name(state, &old_name_range, &old_name)); + kind = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("instance")); + CHECK_PARSE(parse_method_name(parser, &new_name_range, &new_name)); + CHECK_PARSE(parse_method_name(parser, &old_name_range, &old_name)); new_kind_range = NULL_RANGE; old_kind_range = NULL_RANGE; } - member_range.end = state->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(&state->allocator, loc, 5); + member_range.end = parser->current_token.range.end; + rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); + rbs_loc_alloc_children(&parser->allocator, loc, 5); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("new_name"), new_name_range); rbs_loc_add_required_child(loc, INTERN("old_name"), old_name_range); rbs_loc_add_optional_child(loc, INTERN("new_kind"), new_kind_range); rbs_loc_add_optional_child(loc, INTERN("old_kind"), old_kind_range); - *alias_member = rbs_ast_members_alias_new(&state->allocator, loc, new_name, old_name, kind, annotations, comment); + *alias_member = rbs_ast_members_alias_new(&parser->allocator, loc, new_name, old_name, kind, annotations, comment); return true; } @@ -2112,102 +2112,102 @@ static bool parse_alias_member(parserstate *state, bool instance_only, position | {tA2IDENT} `:` */ NODISCARD -static bool parse_variable_member(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **variable_member) { +static bool parse_variable_member(rbs_parser_t *parser, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **variable_member) { if (annotations->length > 0) { - set_error(state, state->current_token, true, "annotation cannot be given to variable members"); + set_error(parser, parser->current_token, true, "annotation cannot be given to variable members"); return false; } range member_range; - member_range.start = state->current_token.range.start; + member_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); - rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); - switch (state->current_token.type) + switch (parser->current_token.type) { case tAIDENT: { - range name_range = state->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, name_range); - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + range name_range = parser->current_token.range; + rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, name_range); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); - ADVANCE_ASSERT(state, pCOLON); - range colon_range = state->current_token.range; + ADVANCE_ASSERT(parser, pCOLON); + range colon_range = parser->current_token.range; rbs_node_t *type; - CHECK_PARSE(parse_type(state, &type)); - member_range.end = state->current_token.range.end; + CHECK_PARSE(parse_type(parser, &type)); + member_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(&state->allocator, loc, 3); + rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); + rbs_loc_alloc_children(&parser->allocator, loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); - *variable_member = (rbs_node_t *)rbs_ast_members_instancevariable_new(&state->allocator, loc, name, type, comment); + *variable_member = (rbs_node_t *)rbs_ast_members_instancevariable_new(&parser->allocator, loc, name, type, comment); return true; } case tA2IDENT: { - range name_range = state->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, name_range); - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + range name_range = parser->current_token.range; + rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, name_range); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); - ADVANCE_ASSERT(state, pCOLON); - range colon_range = state->current_token.range; + ADVANCE_ASSERT(parser, pCOLON); + range colon_range = parser->current_token.range; - parser_push_typevar_table(state, true); + parser_push_typevar_table(parser, true); rbs_node_t *type; - CHECK_PARSE(parse_type(state, &type)); + CHECK_PARSE(parse_type(parser, &type)); - CHECK_PARSE(parser_pop_typevar_table(state)); + CHECK_PARSE(parser_pop_typevar_table(parser)); - member_range.end = state->current_token.range.end; + member_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(&state->allocator, loc, 3); + rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); + rbs_loc_alloc_children(&parser->allocator, loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); - *variable_member = (rbs_node_t *) rbs_ast_members_classvariable_new(&state->allocator, loc, name, type, comment); + *variable_member = (rbs_node_t *) rbs_ast_members_classvariable_new(&parser->allocator, loc, name, type, comment); return true; } case kSELF: { range kind_range = { - .start = state->current_token.range.start, - .end = state->next_token.range.end + .start = parser->current_token.range.start, + .end = parser->next_token.range.end }; - ADVANCE_ASSERT(state, pDOT); - ADVANCE_ASSERT(state, tAIDENT); + ADVANCE_ASSERT(parser, pDOT); + ADVANCE_ASSERT(parser, tAIDENT); - range name_range = state->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, name_range); - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); + range name_range = parser->current_token.range; + rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, name_range); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); - ADVANCE_ASSERT(state, pCOLON); - range colon_range = state->current_token.range; + ADVANCE_ASSERT(parser, pCOLON); + range colon_range = parser->current_token.range; - parser_push_typevar_table(state, true); + parser_push_typevar_table(parser, true); rbs_node_t *type; - CHECK_PARSE(parse_type(state, &type)); + CHECK_PARSE(parse_type(parser, &type)); - CHECK_PARSE(parser_pop_typevar_table(state)); + CHECK_PARSE(parser_pop_typevar_table(parser)); - member_range.end = state->current_token.range.end; + member_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(&state->allocator, loc, 3); + rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); + rbs_loc_alloc_children(&parser->allocator, loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range); - *variable_member = (rbs_node_t *)rbs_ast_members_classinstancevariable_new(&state->allocator, loc, name, type, comment); + *variable_member = (rbs_node_t *)rbs_ast_members_classinstancevariable_new(&parser->allocator, loc, name, type, comment); return true; } default: - set_error(state, state->current_token, false, "Unexpected error"); + set_error(parser, parser->current_token, false, "Unexpected error"); return false; } } @@ -2217,26 +2217,26 @@ static bool parse_variable_member(parserstate *state, position comment_pos, rbs_ | {<`private`>} */ NODISCARD -static bool parse_visibility_member(parserstate *state, rbs_node_list_t *annotations, rbs_node_t **visibility_member) { +static bool parse_visibility_member(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_node_t **visibility_member) { if (annotations->length > 0) { - set_error(state, state->current_token, true, "annotation cannot be given to visibility members"); + set_error(parser, parser->current_token, true, "annotation cannot be given to visibility members"); return false; } - rbs_location_t *location = rbs_location_current_token(state); + rbs_location_t *location = rbs_location_current_token(parser); - switch (state->current_token.type) + switch (parser->current_token.type) { case kPUBLIC: { - *visibility_member = (rbs_node_t *) rbs_ast_members_public_new(&state->allocator, location); + *visibility_member = (rbs_node_t *) rbs_ast_members_public_new(&parser->allocator, location); return true; } case kPRIVATE: { - *visibility_member = (rbs_node_t *) rbs_ast_members_private_new(&state->allocator, location); + *visibility_member = (rbs_node_t *) rbs_ast_members_private_new(&parser->allocator, location); return true; } default: - set_error(state, state->current_token, false, "Unexpected error"); + set_error(parser, parser->current_token, false, "Unexpected error"); return false; } } @@ -2256,27 +2256,27 @@ static bool parse_visibility_member(parserstate *state, rbs_node_list_t *annotat | `(` `)` # No variable */ NODISCARD -static bool parse_attribute_member(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **attribute_member) { +static bool parse_attribute_member(rbs_parser_t *parser, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **attribute_member) { range member_range; - member_range.start = state->current_token.range.start; + member_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); - rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); range visibility_range; rbs_keyword_t *visibility; - switch (state->current_token.type) + switch (parser->current_token.type) { case kPRIVATE: { - visibility_range = state->current_token.range; - visibility = rbs_keyword_new(&state->allocator, rbs_location_new(&state->allocator, visibility_range), INTERN("private")); - parser_advance(state); + visibility_range = parser->current_token.range; + visibility = rbs_keyword_new(&parser->allocator, rbs_location_new(&parser->allocator, visibility_range), INTERN("private")); + parser_advance(parser); break; } case kPUBLIC: { - visibility_range = state->current_token.range; - visibility = rbs_keyword_new(&state->allocator, rbs_location_new(&state->allocator, visibility_range), INTERN("public")); - parser_advance(state); + visibility_range = parser->current_token.range; + visibility = rbs_keyword_new(&parser->allocator, rbs_location_new(&parser->allocator, visibility_range), INTERN("public")); + parser_advance(parser); break; } default: @@ -2285,63 +2285,63 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs break; } - enum RBSTokenType attr_type = state->current_token.type; - range keyword_range = state->current_token.range; + enum RBSTokenType attr_type = parser->current_token.type; + range keyword_range = parser->current_token.range; range kind_range; - InstanceSingletonKind is_kind = parse_instance_singleton_kind(state, false, &kind_range); + InstanceSingletonKind is_kind = parse_instance_singleton_kind(parser, false, &kind_range); rbs_keyword_t *kind = rbs_keyword_new( - &state->allocator, - rbs_location_new(&state->allocator, keyword_range), + &parser->allocator, + rbs_location_new(&parser->allocator, keyword_range), INTERN(((is_kind == INSTANCE_KIND) ? "instance" : "singleton")) ); range name_range; rbs_ast_symbol_t *attr_name; - CHECK_PARSE(parse_method_name(state, &name_range, &attr_name)); + CHECK_PARSE(parse_method_name(parser, &name_range, &attr_name)); - rbs_node_t *ivar_name; // rbs_ast_symbol_t, NULL or rbs_ast_bool_new(&state->allocator, false) + rbs_node_t *ivar_name; // rbs_ast_symbol_t, NULL or rbs_ast_bool_new(&parser->allocator, false) range ivar_range, ivar_name_range; - if (state->next_token.type == pLPAREN) { - ADVANCE_ASSERT(state, pLPAREN); - ivar_range.start = state->current_token.range.start; - - if (parser_advance_if(state, tAIDENT)) { - rbs_location_t *symbolLoc = rbs_location_current_token(state); - ivar_name = (rbs_node_t *) rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); - ivar_name_range = state->current_token.range; + if (parser->next_token.type == pLPAREN) { + ADVANCE_ASSERT(parser, pLPAREN); + ivar_range.start = parser->current_token.range.start; + + if (parser_advance_if(parser, tAIDENT)) { + rbs_location_t *symbolLoc = rbs_location_current_token(parser); + ivar_name = (rbs_node_t *) rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); + ivar_name_range = parser->current_token.range; } else { range false_range = { - .start = state->current_token.range.start, - .end = state->current_token.range.end + .start = parser->current_token.range.start, + .end = parser->current_token.range.end }; - ivar_name = (rbs_node_t *) rbs_ast_bool_new(&state->allocator, rbs_location_new(&state->allocator, false_range), false); + ivar_name = (rbs_node_t *) rbs_ast_bool_new(&parser->allocator, rbs_location_new(&parser->allocator, false_range), false); ivar_name_range = NULL_RANGE; } - ADVANCE_ASSERT(state, pRPAREN); - ivar_range.end = state->current_token.range.end; + ADVANCE_ASSERT(parser, pRPAREN); + ivar_range.end = parser->current_token.range.end; } else { ivar_range = NULL_RANGE; ivar_name = NULL; ivar_name_range = NULL_RANGE; } - ADVANCE_ASSERT(state, pCOLON); - range colon_range = state->current_token.range; + ADVANCE_ASSERT(parser, pCOLON); + range colon_range = parser->current_token.range; - parser_push_typevar_table(state, is_kind == SINGLETON_KIND); + parser_push_typevar_table(parser, is_kind == SINGLETON_KIND); rbs_node_t *type; - CHECK_PARSE(parse_type(state, &type)); + CHECK_PARSE(parse_type(parser, &type)); - CHECK_PARSE(parser_pop_typevar_table(state)); + CHECK_PARSE(parser_pop_typevar_table(parser)); - member_range.end = state->current_token.range.end; + member_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(&state->allocator, loc, 7); + rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); + rbs_loc_alloc_children(&parser->allocator, loc, 7); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); @@ -2353,16 +2353,16 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs switch (attr_type) { case kATTRREADER: - *attribute_member = (rbs_node_t *) rbs_ast_members_attrreader_new(&state->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); + *attribute_member = (rbs_node_t *) rbs_ast_members_attrreader_new(&parser->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); return true; case kATTRWRITER: - *attribute_member = (rbs_node_t *) rbs_ast_members_attrwriter_new(&state->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); + *attribute_member = (rbs_node_t *) rbs_ast_members_attrwriter_new(&parser->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); return true; case kATTRACCESSOR: - *attribute_member = (rbs_node_t *) rbs_ast_members_attraccessor_new(&state->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); + *attribute_member = (rbs_node_t *) rbs_ast_members_attraccessor_new(&parser->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); return true; default: - set_error(state, state->current_token, false, "Unexpected error"); + set_error(parser, parser->current_token, false, "Unexpected error"); return false; } } @@ -2375,21 +2375,21 @@ static bool parse_attribute_member(parserstate *state, position comment_pos, rbs | alias_member (instance only) */ NODISCARD -static bool parse_interface_members(parserstate *state, rbs_node_list_t **members) { - *members = rbs_node_list_new(&state->allocator); +static bool parse_interface_members(rbs_parser_t *parser, rbs_node_list_t **members) { + *members = rbs_node_list_new(&parser->allocator); - while (state->next_token.type != kEND) { - rbs_node_list_t *annotations = rbs_node_list_new(&state->allocator); + while (parser->next_token.type != kEND) { + rbs_node_list_t *annotations = rbs_node_list_new(&parser->allocator); position annot_pos = NullPosition; - CHECK_PARSE(parse_annotations(state, annotations, &annot_pos)); - parser_advance(state); + CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos)); + parser_advance(parser); rbs_node_t *member; - switch (state->current_token.type) { + switch (parser->current_token.type) { case kDEF: { rbs_ast_members_methoddefinition_t *method_definition = NULL; - CHECK_PARSE(parse_member_def(state, true, true, annot_pos, annotations, &method_definition)); + CHECK_PARSE(parse_member_def(parser, true, true, annot_pos, annotations, &method_definition)); member = (rbs_node_t *) method_definition; break; } @@ -2397,19 +2397,19 @@ static bool parse_interface_members(parserstate *state, rbs_node_list_t **member case kINCLUDE: case kEXTEND: case kPREPEND: { - CHECK_PARSE(parse_mixin_member(state, true, annot_pos, annotations, &member)); + CHECK_PARSE(parse_mixin_member(parser, true, annot_pos, annotations, &member)); break; } case kALIAS: { rbs_ast_members_alias_t *alias_member = NULL; - CHECK_PARSE(parse_alias_member(state, true, annot_pos, annotations, &alias_member)); + CHECK_PARSE(parse_alias_member(parser, true, annot_pos, annotations, &alias_member)); member = (rbs_node_t *) alias_member; break; } default: - set_error(state, state->current_token, true, "unexpected token for interface declaration member"); + set_error(parser, parser->current_token, true, "unexpected token for interface declaration member"); return false; } @@ -2423,44 +2423,44 @@ static bool parse_interface_members(parserstate *state, rbs_node_list_t **member interface_decl ::= {`interface`} interface_name module_type_params interface_members */ NODISCARD -static bool parse_interface_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_interface_t **interface_decl) { - parser_push_typevar_table(state, true); +static bool parse_interface_decl(rbs_parser_t *parser, position comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_interface_t **interface_decl) { + parser_push_typevar_table(parser, true); range member_range; - member_range.start = state->current_token.range.start; + member_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); - range keyword_range = state->current_token.range; + range keyword_range = parser->current_token.range; - parser_advance(state); + parser_advance(parser); range name_range; rbs_typename_t *name = NULL; - CHECK_PARSE(parse_type_name(state, INTERFACE_NAME, &name_range, &name)); + CHECK_PARSE(parse_type_name(parser, INTERFACE_NAME, &name_range, &name)); range type_params_range; rbs_node_list_t *type_params; - CHECK_PARSE(parse_type_params(state, &type_params_range, true, &type_params)); + CHECK_PARSE(parse_type_params(parser, &type_params_range, true, &type_params)); rbs_node_list_t *members = NULL; - CHECK_PARSE(parse_interface_members(state, &members)); + CHECK_PARSE(parse_interface_members(parser, &members)); - ADVANCE_ASSERT(state, kEND); - range end_range = state->current_token.range; + ADVANCE_ASSERT(parser, kEND); + range end_range = parser->current_token.range; member_range.end = end_range.end; - CHECK_PARSE(parser_pop_typevar_table(state)); + CHECK_PARSE(parser_pop_typevar_table(parser)); - rbs_location_t *loc = rbs_location_new(&state->allocator, member_range); - rbs_loc_alloc_children(&state->allocator, loc, 4); + rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); + rbs_loc_alloc_children(&parser->allocator, loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("end"), end_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range); - rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); + rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); - *interface_decl = rbs_ast_declarations_interface_new(&state->allocator, loc, name, type_params, members, annotations, comment); + *interface_decl = rbs_ast_declarations_interface_new(&parser->allocator, loc, name, type_params, members, annotations, comment); return true; } @@ -2471,38 +2471,38 @@ static bool parse_interface_decl(parserstate *state, position comment_pos, rbs_n | module_name `[` type_list <`]`> */ NODISCARD -static bool parse_module_self_types(parserstate *state, rbs_node_list_t *array) { +static bool parse_module_self_types(rbs_parser_t *parser, rbs_node_list_t *array) { while (true) { - parser_advance(state); + parser_advance(parser); range self_range; - self_range.start = state->current_token.range.start; + self_range.start = parser->current_token.range.start; range name_range; rbs_typename_t *module_name = NULL; - CHECK_PARSE(parse_type_name(state, CLASS_NAME | INTERFACE_NAME, &name_range, &module_name)); + CHECK_PARSE(parse_type_name(parser, CLASS_NAME | INTERFACE_NAME, &name_range, &module_name)); self_range.end = name_range.end; - rbs_node_list_t *args = rbs_node_list_new(&state->allocator); + rbs_node_list_t *args = rbs_node_list_new(&parser->allocator); range args_range = NULL_RANGE; - if (state->next_token.type == pLBRACKET) { - parser_advance(state); - args_range.start = state->current_token.range.start; - CHECK_PARSE(parse_type_list(state, pRBRACKET, args)); - parser_advance(state); - self_range.end = args_range.end = state->current_token.range.end; + if (parser->next_token.type == pLBRACKET) { + parser_advance(parser); + args_range.start = parser->current_token.range.start; + CHECK_PARSE(parse_type_list(parser, pRBRACKET, args)); + parser_advance(parser); + self_range.end = args_range.end = parser->current_token.range.end; } - rbs_location_t *loc = rbs_location_new(&state->allocator, self_range); - rbs_loc_alloc_children(&state->allocator, loc, 2); + rbs_location_t *loc = rbs_location_new(&parser->allocator, self_range); + rbs_loc_alloc_children(&parser->allocator, loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); - rbs_ast_declarations_module_self_t *self_type = rbs_ast_declarations_module_self_new(&state->allocator, loc, module_name, args); + rbs_ast_declarations_module_self_t *self_type = rbs_ast_declarations_module_self_new(&parser->allocator, loc, module_name, args); rbs_node_list_append(array, (rbs_node_t *)self_type); - if (state->next_token.type == pCOMMA) { - parser_advance(state); + if (parser->next_token.type == pCOMMA) { + parser_advance(parser); } else { break; } @@ -2512,7 +2512,7 @@ static bool parse_module_self_types(parserstate *state, rbs_node_list_t *array) } NODISCARD -static bool parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl); +static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, position annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl); /* module_members ::= {} ... kEND @@ -2526,22 +2526,22 @@ static bool parse_nested_decl(parserstate *state, const char *nested_in, positio | `private` */ NODISCARD -static bool parse_module_members(parserstate *state, rbs_node_list_t **members) { - *members = rbs_node_list_new(&state->allocator); +static bool parse_module_members(rbs_parser_t *parser, rbs_node_list_t **members) { + *members = rbs_node_list_new(&parser->allocator); - while (state->next_token.type != kEND) { - rbs_node_list_t *annotations = rbs_node_list_new(&state->allocator); + while (parser->next_token.type != kEND) { + rbs_node_list_t *annotations = rbs_node_list_new(&parser->allocator); position annot_pos; - CHECK_PARSE(parse_annotations(state, annotations, &annot_pos)); + CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos)); - parser_advance(state); + parser_advance(parser); rbs_node_t *member; - switch (state->current_token.type) + switch (parser->current_token.type) { case kDEF: { rbs_ast_members_methoddefinition_t *method_definition; - CHECK_PARSE(parse_member_def(state, false, true, annot_pos, annotations, &method_definition)); + CHECK_PARSE(parse_member_def(parser, false, true, annot_pos, annotations, &method_definition)); member = (rbs_node_t *) method_definition; break; } @@ -2549,57 +2549,57 @@ static bool parse_module_members(parserstate *state, rbs_node_list_t **members) case kINCLUDE: case kEXTEND: case kPREPEND: { - CHECK_PARSE(parse_mixin_member(state, false, annot_pos, annotations, &member)); + CHECK_PARSE(parse_mixin_member(parser, false, annot_pos, annotations, &member)); break; } case kALIAS: { rbs_ast_members_alias_t *alias_member = NULL; - CHECK_PARSE(parse_alias_member(state, false, annot_pos, annotations, &alias_member)); + CHECK_PARSE(parse_alias_member(parser, false, annot_pos, annotations, &alias_member)); member = (rbs_node_t *) alias_member; break; } case tAIDENT: case tA2IDENT: case kSELF: { - CHECK_PARSE(parse_variable_member(state, annot_pos, annotations, &member)); + CHECK_PARSE(parse_variable_member(parser, annot_pos, annotations, &member)); break; } case kATTRREADER: case kATTRWRITER: case kATTRACCESSOR: { - CHECK_PARSE(parse_attribute_member(state, annot_pos, annotations, &member)); + CHECK_PARSE(parse_attribute_member(parser, annot_pos, annotations, &member)); break; } case kPUBLIC: case kPRIVATE: - if (state->next_token.range.start.line == state->current_token.range.start.line) { - switch (state->next_token.type) + if (parser->next_token.range.start.line == parser->current_token.range.start.line) { + switch (parser->next_token.type) { case kDEF: { rbs_ast_members_methoddefinition_t *method_definition = NULL; - CHECK_PARSE(parse_member_def(state, false, true, annot_pos, annotations, &method_definition)); + CHECK_PARSE(parse_member_def(parser, false, true, annot_pos, annotations, &method_definition)); member = (rbs_node_t *) method_definition; break; } case kATTRREADER: case kATTRWRITER: case kATTRACCESSOR: { - CHECK_PARSE(parse_attribute_member(state, annot_pos, annotations, &member)); + CHECK_PARSE(parse_attribute_member(parser, annot_pos, annotations, &member)); break; } default: - set_error(state, state->next_token, true, "method or attribute definition is expected after visibility modifier"); + set_error(parser, parser->next_token, true, "method or attribute definition is expected after visibility modifier"); return false; } } else { - CHECK_PARSE(parse_visibility_member(state, annotations, &member)); + CHECK_PARSE(parse_visibility_member(parser, annotations, &member)); } break; default: - CHECK_PARSE(parse_nested_decl(state, "module", annot_pos, annotations, &member)); + CHECK_PARSE(parse_nested_decl(parser, "module", annot_pos, annotations, &member)); break; } @@ -2614,39 +2614,39 @@ static bool parse_module_members(parserstate *state, rbs_node_list_t **members) | {module_name} module_name module_type_params `:` module_self_types module_members */ NODISCARD -static bool parse_module_decl0(parserstate *state, range keyword_range, rbs_typename_t *module_name, range name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_module_t **module_decl) { - parser_push_typevar_table(state, true); +static bool parse_module_decl0(rbs_parser_t *parser, range keyword_range, rbs_typename_t *module_name, range name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_module_t **module_decl) { + parser_push_typevar_table(parser, true); range decl_range; decl_range.start = keyword_range.start; range type_params_range; rbs_node_list_t *type_params; - CHECK_PARSE(parse_type_params(state, &type_params_range, true, &type_params)); + CHECK_PARSE(parse_type_params(parser, &type_params_range, true, &type_params)); - rbs_node_list_t *self_types = rbs_node_list_new(&state->allocator); + rbs_node_list_t *self_types = rbs_node_list_new(&parser->allocator); range colon_range; range self_types_range; - if (state->next_token.type == pCOLON) { - parser_advance(state); - colon_range = state->current_token.range; - self_types_range.start = state->next_token.range.start; - CHECK_PARSE(parse_module_self_types(state, self_types)); - self_types_range.end = state->current_token.range.end; + if (parser->next_token.type == pCOLON) { + parser_advance(parser); + colon_range = parser->current_token.range; + self_types_range.start = parser->next_token.range.start; + CHECK_PARSE(parse_module_self_types(parser, self_types)); + self_types_range.end = parser->current_token.range.end; } else { colon_range = NULL_RANGE; self_types_range = NULL_RANGE; } rbs_node_list_t *members = NULL; - CHECK_PARSE(parse_module_members(state, &members)); + CHECK_PARSE(parse_module_members(parser, &members)); - ADVANCE_ASSERT(state, kEND); - range end_range = state->current_token.range; - decl_range.end = state->current_token.range.end; + ADVANCE_ASSERT(parser, kEND); + range end_range = parser->current_token.range; + decl_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); - rbs_loc_alloc_children(&state->allocator, loc, 6); + rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); + rbs_loc_alloc_children(&parser->allocator, loc, 6); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("end"), end_range); @@ -2654,9 +2654,9 @@ static bool parse_module_decl0(parserstate *state, range keyword_range, rbs_type rbs_loc_add_optional_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("self_types"), self_types_range); - CHECK_PARSE(parser_pop_typevar_table(state)); + CHECK_PARSE(parser_pop_typevar_table(parser)); - *module_decl = rbs_ast_declarations_module_new(&state->allocator, loc, module_name, type_params, self_types, members, annotations, comment); + *module_decl = rbs_ast_declarations_module_new(&parser->allocator, loc, module_name, type_params, self_types, members, annotations, comment); return true; } @@ -2666,43 +2666,43 @@ static bool parse_module_decl0(parserstate *state, range keyword_range, rbs_type */ NODISCARD -static bool parse_module_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **module_decl) { - range keyword_range = state->current_token.range; +static bool parse_module_decl(rbs_parser_t *parser, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **module_decl) { + range keyword_range = parser->current_token.range; - comment_pos = rbs_nonnull_pos_or(comment_pos, state->current_token.range.start); - rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); + comment_pos = rbs_nonnull_pos_or(comment_pos, parser->current_token.range.start); + rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); - parser_advance(state); + parser_advance(parser); range module_name_range; rbs_typename_t *module_name; - CHECK_PARSE(parse_type_name(state, CLASS_NAME, &module_name_range, &module_name)); + CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &module_name_range, &module_name)); - if (state->next_token.type == pEQ) { - range eq_range = state->next_token.range; - parser_advance(state); - parser_advance(state); + if (parser->next_token.type == pEQ) { + range eq_range = parser->next_token.range; + parser_advance(parser); + parser_advance(parser); range old_name_range; rbs_typename_t *old_name = NULL; - CHECK_PARSE(parse_type_name(state, CLASS_NAME, &old_name_range, &old_name)); + CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &old_name_range, &old_name)); range decl_range = { .start = keyword_range.start, .end = old_name_range.end }; - rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); - rbs_loc_alloc_children(&state->allocator, loc, 4); + rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); + rbs_loc_alloc_children(&parser->allocator, loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("new_name"), module_name_range); rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); - *module_decl = (rbs_node_t *) rbs_ast_declarations_modulealias_new(&state->allocator, loc, module_name, old_name, comment, annotations); + *module_decl = (rbs_node_t *) rbs_ast_declarations_modulealias_new(&parser->allocator, loc, module_name, old_name, comment, annotations); } else { rbs_ast_declarations_module_t *module_decl0 = NULL; - CHECK_PARSE(parse_module_decl0(state, keyword_range, module_name, module_name_range, comment, annotations, &module_decl0)); + CHECK_PARSE(parse_module_decl0(parser, keyword_range, module_name, module_name_range, comment, annotations, &module_decl0)); *module_decl = (rbs_node_t *) module_decl0; } @@ -2714,27 +2714,27 @@ static bool parse_module_decl(parserstate *state, position comment_pos, rbs_node | {<>} */ NODISCARD -static bool parse_class_decl_super(parserstate *state, range *lt_range, rbs_ast_declarations_class_super_t **super) { - if (parser_advance_if(state, pLT)) { - *lt_range = state->current_token.range; +static bool parse_class_decl_super(rbs_parser_t *parser, range *lt_range, rbs_ast_declarations_class_super_t **super) { + if (parser_advance_if(parser, pLT)) { + *lt_range = parser->current_token.range; range super_range; - super_range.start = state->next_token.range.start; + super_range.start = parser->next_token.range.start; - rbs_node_list_t *args = rbs_node_list_new(&state->allocator); + rbs_node_list_t *args = rbs_node_list_new(&parser->allocator); rbs_typename_t *name = NULL; range name_range, args_range; - CHECK_PARSE(class_instance_name(state, CLASS_NAME, args, &name_range, &args_range, &name)); + CHECK_PARSE(class_instance_name(parser, CLASS_NAME, args, &name_range, &args_range, &name)); - super_range.end = state->current_token.range.end; + super_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, super_range); - rbs_loc_alloc_children(&state->allocator, loc, 2); + rbs_location_t *loc = rbs_location_new(&parser->allocator, super_range); + rbs_loc_alloc_children(&parser->allocator, loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); - *super = rbs_ast_declarations_class_super_new(&state->allocator, loc, name, args); + *super = rbs_ast_declarations_class_super_new(&parser->allocator, loc, name, args); } else { *lt_range = NULL_RANGE; } @@ -2746,40 +2746,40 @@ static bool parse_class_decl_super(parserstate *state, range *lt_range, rbs_ast_ class_decl ::= {class_name} type_params class_decl_super class_members <`end`> */ NODISCARD -static bool parse_class_decl0(parserstate *state, range keyword_range, rbs_typename_t *name, range name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_class_t **class_decl) { - parser_push_typevar_table(state, true); +static bool parse_class_decl0(rbs_parser_t *parser, range keyword_range, rbs_typename_t *name, range name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_class_t **class_decl) { + parser_push_typevar_table(parser, true); range decl_range; decl_range.start = keyword_range.start; range type_params_range; rbs_node_list_t *type_params; - CHECK_PARSE(parse_type_params(state, &type_params_range, true, &type_params)); + CHECK_PARSE(parse_type_params(parser, &type_params_range, true, &type_params)); range lt_range; rbs_ast_declarations_class_super_t *super = NULL; - CHECK_PARSE(parse_class_decl_super(state, <_range, &super)); + CHECK_PARSE(parse_class_decl_super(parser, <_range, &super)); rbs_node_list_t *members = NULL; - CHECK_PARSE(parse_module_members(state, &members)); + CHECK_PARSE(parse_module_members(parser, &members)); - ADVANCE_ASSERT(state, kEND); + ADVANCE_ASSERT(parser, kEND); - range end_range = state->current_token.range; + range end_range = parser->current_token.range; decl_range.end = end_range.end; - CHECK_PARSE(parser_pop_typevar_table(state)); + CHECK_PARSE(parser_pop_typevar_table(parser)); - rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); - rbs_loc_alloc_children(&state->allocator, loc, 5); + rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); + rbs_loc_alloc_children(&parser->allocator, loc, 5); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("end"), end_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range); rbs_loc_add_optional_child(loc, INTERN("lt"), lt_range); - *class_decl = rbs_ast_declarations_class_new(&state->allocator, loc, name, type_params, super, members, annotations, comment); + *class_decl = rbs_ast_declarations_class_new(&parser->allocator, loc, name, type_params, super, members, annotations, comment); return true; } @@ -2788,42 +2788,42 @@ static bool parse_class_decl0(parserstate *state, range keyword_range, rbs_typen | {`class`} class_name */ NODISCARD -static bool parse_class_decl(parserstate *state, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **class_decl) { - range keyword_range = state->current_token.range; +static bool parse_class_decl(rbs_parser_t *parser, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **class_decl) { + range keyword_range = parser->current_token.range; - comment_pos = rbs_nonnull_pos_or(comment_pos, state->current_token.range.start); - rbs_ast_comment_t *comment = get_comment(state, comment_pos.line); + comment_pos = rbs_nonnull_pos_or(comment_pos, parser->current_token.range.start); + rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); - parser_advance(state); + parser_advance(parser); range class_name_range; rbs_typename_t *class_name = NULL; - CHECK_PARSE(parse_type_name(state, CLASS_NAME, &class_name_range, &class_name)); + CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &class_name_range, &class_name)); - if (state->next_token.type == pEQ) { - range eq_range = state->next_token.range; - parser_advance(state); - parser_advance(state); + if (parser->next_token.type == pEQ) { + range eq_range = parser->next_token.range; + parser_advance(parser); + parser_advance(parser); range old_name_range; rbs_typename_t *old_name = NULL; - CHECK_PARSE(parse_type_name(state, CLASS_NAME, &old_name_range, &old_name)); + CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &old_name_range, &old_name)); range decl_range = { .start = keyword_range.start, .end = old_name_range.end, }; - rbs_location_t *loc = rbs_location_new(&state->allocator, decl_range); - rbs_loc_alloc_children(&state->allocator, loc, 4); + rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); + rbs_loc_alloc_children(&parser->allocator, loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("new_name"), class_name_range); rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); - *class_decl = (rbs_node_t *) rbs_ast_declarations_classalias_new(&state->allocator, loc, class_name, old_name, comment, annotations); + *class_decl = (rbs_node_t *) rbs_ast_declarations_classalias_new(&parser->allocator, loc, class_name, old_name, comment, annotations); } else { rbs_ast_declarations_class_t *class_decl0 = NULL; - CHECK_PARSE(parse_class_decl0(state, keyword_range, class_name, class_name_range, comment, annotations, &class_decl0)); + CHECK_PARSE(parse_class_decl0(parser, keyword_range, class_name, class_name_range, comment, annotations, &class_decl0)); *class_decl = (rbs_node_t *) class_decl0; } @@ -2838,105 +2838,105 @@ static bool parse_class_decl(parserstate *state, position comment_pos, rbs_node_ | {} */ NODISCARD -static bool parse_nested_decl(parserstate *state, const char *nested_in, position annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl) { - parser_push_typevar_table(state, true); +static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, position annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl) { + parser_push_typevar_table(parser, true); - switch (state->current_token.type) { + switch (parser->current_token.type) { case tUIDENT: case pCOLON2: { rbs_ast_declarations_constant_t *constant = NULL; - CHECK_PARSE(parse_const_decl(state, annotations, &constant)); + CHECK_PARSE(parse_const_decl(parser, annotations, &constant)); *decl = (rbs_node_t *) constant; break; } case tGIDENT: { rbs_ast_declarations_global_t *global = NULL; - CHECK_PARSE(parse_global_decl(state, annotations, &global)); + CHECK_PARSE(parse_global_decl(parser, annotations, &global)); *decl = (rbs_node_t *) global; break; } case kTYPE: { rbs_ast_declarations_typealias_t *typealias = NULL; - CHECK_PARSE(parse_type_decl(state, annot_pos, annotations, &typealias)); + CHECK_PARSE(parse_type_decl(parser, annot_pos, annotations, &typealias)); *decl = (rbs_node_t *) typealias; break; } case kINTERFACE: { rbs_ast_declarations_interface_t *interface_decl = NULL; - CHECK_PARSE(parse_interface_decl(state, annot_pos, annotations, &interface_decl)); + CHECK_PARSE(parse_interface_decl(parser, annot_pos, annotations, &interface_decl)); *decl = (rbs_node_t *) interface_decl; break; } case kMODULE: { rbs_node_t *module_decl = NULL; - CHECK_PARSE(parse_module_decl(state, annot_pos, annotations, &module_decl)); + CHECK_PARSE(parse_module_decl(parser, annot_pos, annotations, &module_decl)); *decl = module_decl; break; } case kCLASS: { rbs_node_t *class_decl = NULL; - CHECK_PARSE(parse_class_decl(state, annot_pos, annotations, &class_decl)); + CHECK_PARSE(parse_class_decl(parser, annot_pos, annotations, &class_decl)); *decl = class_decl; break; } default: - set_error(state, state->current_token, true, "unexpected token for class/module declaration member"); + set_error(parser, parser->current_token, true, "unexpected token for class/module declaration member"); return false; } - CHECK_PARSE(parser_pop_typevar_table(state)); + CHECK_PARSE(parser_pop_typevar_table(parser)); return true; } NODISCARD -static bool parse_decl(parserstate *state, rbs_node_t **decl) { - rbs_node_list_t *annotations = rbs_node_list_new(&state->allocator); +static bool parse_decl(rbs_parser_t *parser, rbs_node_t **decl) { + rbs_node_list_t *annotations = rbs_node_list_new(&parser->allocator); position annot_pos = NullPosition; - CHECK_PARSE(parse_annotations(state, annotations, &annot_pos)); - parser_advance(state); + CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos)); + parser_advance(parser); - switch (state->current_token.type) { + switch (parser->current_token.type) { case tUIDENT: case pCOLON2: { rbs_ast_declarations_constant_t *constant = NULL; - CHECK_PARSE(parse_const_decl(state, annotations, &constant)); + CHECK_PARSE(parse_const_decl(parser, annotations, &constant)); *decl = (rbs_node_t *) constant; return true; } case tGIDENT: { rbs_ast_declarations_global_t *global = NULL; - CHECK_PARSE(parse_global_decl(state, annotations, &global)); + CHECK_PARSE(parse_global_decl(parser, annotations, &global)); *decl = (rbs_node_t *) global; return true; } case kTYPE: { rbs_ast_declarations_typealias_t *typealias = NULL; - CHECK_PARSE(parse_type_decl(state, annot_pos, annotations, &typealias)); + CHECK_PARSE(parse_type_decl(parser, annot_pos, annotations, &typealias)); *decl = (rbs_node_t *) typealias; return true; } case kINTERFACE: { rbs_ast_declarations_interface_t *interface_decl = NULL; - CHECK_PARSE(parse_interface_decl(state, annot_pos, annotations, &interface_decl)); + CHECK_PARSE(parse_interface_decl(parser, annot_pos, annotations, &interface_decl)); *decl = (rbs_node_t *) interface_decl; return true; } case kMODULE: { rbs_node_t *module_decl = NULL; - CHECK_PARSE(parse_module_decl(state, annot_pos, annotations, &module_decl)); + CHECK_PARSE(parse_module_decl(parser, annot_pos, annotations, &module_decl)); *decl = module_decl; return true; } case kCLASS: { rbs_node_t *class_decl = NULL; - CHECK_PARSE(parse_class_decl(state, annot_pos, annotations, &class_decl)); + CHECK_PARSE(parse_class_decl(parser, annot_pos, annotations, &class_decl)); *decl = class_decl; return true; } default: - set_error(state, state->current_token, true, "cannot start a declaration"); + set_error(parser, parser->current_token, true, "cannot start a declaration"); return false; } } @@ -2946,38 +2946,38 @@ static bool parse_decl(parserstate *state, rbs_node_t **decl) { | {} <> (empty -- returns empty namespace) */ NODISCARD -static bool parse_namespace(parserstate *state, range *rg, rbs_namespace_t **namespace) { +static bool parse_namespace(rbs_parser_t *parser, range *rg, rbs_namespace_t **namespace) { bool is_absolute = false; - if (state->next_token.type == pCOLON2) { + if (parser->next_token.type == pCOLON2) { *rg = (range) { - .start = state->next_token.range.start, - .end = state->next_token.range.end, + .start = parser->next_token.range.start, + .end = parser->next_token.range.end, }; is_absolute = true; - parser_advance(state); + parser_advance(parser); } - rbs_node_list_t *path = rbs_node_list_new(&state->allocator); + rbs_node_list_t *path = rbs_node_list_new(&parser->allocator); while (true) { - if (state->next_token.type == tUIDENT && state->next_token2.type == pCOLON2) { - rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, state->next_token.range); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->next_token)); + if (parser->next_token.type == tUIDENT && parser->next_token2.type == pCOLON2) { + rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, parser->next_token.range); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->next_token)); rbs_node_list_append(path, (rbs_node_t *)symbol); if (rbs_null_position_p(rg->start)) { - rg->start = state->next_token.range.start; + rg->start = parser->next_token.range.start; } - rg->end = state->next_token2.range.end; - parser_advance(state); - parser_advance(state); + rg->end = parser->next_token2.range.end; + parser_advance(parser); + parser_advance(parser); } else { break; } } - *namespace = rbs_namespace_new(&state->allocator, rbs_location_new(&state->allocator, *rg), path, is_absolute); + *namespace = rbs_namespace_new(&parser->allocator, rbs_location_new(&parser->allocator, *rg), path, is_absolute); return true; } @@ -2989,54 +2989,54 @@ static bool parse_namespace(parserstate *state, range *rg, rbs_namespace_t **nam | {} namespace */ NODISCARD -static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { +static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) { while (true) { range namespace_range = NULL_RANGE; rbs_namespace_t *namespace = NULL; - CHECK_PARSE(parse_namespace(state, &namespace_range, &namespace)); + CHECK_PARSE(parse_namespace(parser, &namespace_range, &namespace)); - switch (state->next_token.type) + switch (parser->next_token.type) { case tLIDENT: case tULIDENT: case tUIDENT: { - parser_advance(state); + parser_advance(parser); - enum RBSTokenType ident_type = state->current_token.type; + enum RBSTokenType ident_type = parser->current_token.type; range type_name_range = rbs_null_range_p(namespace_range) - ? state->current_token.range - : (range) { .start = namespace_range.start, .end = state->current_token.range.end }; + ? parser->current_token.range + : (range) { .start = namespace_range.start, .end = parser->current_token.range.end }; - rbs_location_t *symbolLoc = rbs_location_current_token(state); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); - rbs_typename_t *type_name = rbs_typename_new(&state->allocator, rbs_location_new(&state->allocator, type_name_range),namespace, symbol); + rbs_location_t *symbolLoc = rbs_location_current_token(parser); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); + rbs_typename_t *type_name = rbs_typename_new(&parser->allocator, rbs_location_new(&parser->allocator, type_name_range),namespace, symbol); range keyword_range = NULL_RANGE; range new_name_range = NULL_RANGE; rbs_ast_symbol_t *new_name = NULL; range clause_range = type_name_range; - if (state->next_token.type == kAS) { - parser_advance(state); - keyword_range = state->current_token.range; + if (parser->next_token.type == kAS) { + parser_advance(parser); + keyword_range = parser->current_token.range; - if (ident_type == tUIDENT) ADVANCE_ASSERT(state, tUIDENT); - if (ident_type == tLIDENT) ADVANCE_ASSERT(state, tLIDENT); - if (ident_type == tULIDENT) ADVANCE_ASSERT(state, tULIDENT); + if (ident_type == tUIDENT) ADVANCE_ASSERT(parser, tUIDENT); + if (ident_type == tLIDENT) ADVANCE_ASSERT(parser, tLIDENT); + if (ident_type == tULIDENT) ADVANCE_ASSERT(parser, tULIDENT); - rbs_location_t *symbolLoc = rbs_location_new(&state->allocator, new_name_range); - new_name = rbs_ast_symbol_new(&state->allocator, symbolLoc, &state->constant_pool, INTERN_TOKEN(state, state->current_token)); - new_name_range = state->current_token.range; + rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, new_name_range); + new_name = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); + new_name_range = parser->current_token.range; clause_range.end = new_name_range.end; } - rbs_location_t *loc = rbs_location_new(&state->allocator, clause_range); - rbs_loc_alloc_children(&state->allocator, loc, 3); + rbs_location_t *loc = rbs_location_new(&parser->allocator, clause_range); + rbs_loc_alloc_children(&parser->allocator, loc, 3); rbs_loc_add_required_child(loc, INTERN("type_name"), type_name_range); rbs_loc_add_optional_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_optional_child(loc, INTERN("new_name"), new_name_range); - rbs_ast_directives_use_singleclause_t *clause = rbs_ast_directives_use_singleclause_new(&state->allocator, loc, type_name, new_name); + rbs_ast_directives_use_singleclause_t *clause = rbs_ast_directives_use_singleclause_new(&parser->allocator, loc, type_name, new_name); rbs_node_list_append(clauses, (rbs_node_t *)clause); break; @@ -3044,28 +3044,28 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { case pSTAR: { range clause_range = namespace_range; - parser_advance(state); + parser_advance(parser); - range star_range = state->current_token.range; + range star_range = parser->current_token.range; clause_range.end = star_range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, clause_range); - rbs_loc_alloc_children(&state->allocator, loc, 2); + rbs_location_t *loc = rbs_location_new(&parser->allocator, clause_range); + rbs_loc_alloc_children(&parser->allocator, loc, 2); rbs_loc_add_required_child(loc, INTERN("namespace"), namespace_range); rbs_loc_add_required_child(loc, INTERN("star"), star_range); - rbs_ast_directives_use_wildcardclause_t *clause = rbs_ast_directives_use_wildcardclause_new(&state->allocator, loc, namespace); + rbs_ast_directives_use_wildcardclause_t *clause = rbs_ast_directives_use_wildcardclause_new(&parser->allocator, loc, namespace); rbs_node_list_append(clauses, (rbs_node_t *)clause); break; } default: - set_error(state, state->next_token, true, "use clause is expected"); + set_error(parser, parser->next_token, true, "use clause is expected"); return false; } - if (state->next_token.type == pCOMMA) { - parser_advance(state); + if (parser->next_token.type == pCOMMA) { + parser_advance(parser); } else { break; } @@ -3078,44 +3078,44 @@ static bool parse_use_clauses(parserstate *state, rbs_node_list_t *clauses) { use_directive ::= {} `use` */ NODISCARD -static bool parse_use_directive(parserstate *state, rbs_ast_directives_use_t **use_directive) { - if (state->next_token.type == kUSE) { - parser_advance(state); +static bool parse_use_directive(rbs_parser_t *parser, rbs_ast_directives_use_t **use_directive) { + if (parser->next_token.type == kUSE) { + parser_advance(parser); - range keyword_range = state->current_token.range; + range keyword_range = parser->current_token.range; - rbs_node_list_t *clauses = rbs_node_list_new(&state->allocator); - CHECK_PARSE(parse_use_clauses(state, clauses)); + rbs_node_list_t *clauses = rbs_node_list_new(&parser->allocator); + CHECK_PARSE(parse_use_clauses(parser, clauses)); range directive_range = keyword_range; - directive_range.end = state->current_token.range.end; + directive_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&state->allocator, directive_range); - rbs_loc_alloc_children(&state->allocator, loc, 1); + rbs_location_t *loc = rbs_location_new(&parser->allocator, directive_range); + rbs_loc_alloc_children(&parser->allocator, loc, 1); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); - *use_directive = rbs_ast_directives_use_new(&state->allocator, loc, clauses); + *use_directive = rbs_ast_directives_use_new(&parser->allocator, loc, clauses); } return true; } -static rbs_ast_comment_t *parse_comment_lines(parserstate *state, comment *com) { - size_t hash_bytes = state->lexstate->encoding->char_width((const uint8_t *) "#", (size_t) 1); - size_t space_bytes = state->lexstate->encoding->char_width((const uint8_t *) " ", (size_t) 1); +static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_t *com) { + size_t hash_bytes = parser->lexstate->encoding->char_width((const uint8_t *) "#", (size_t) 1); + size_t space_bytes = parser->lexstate->encoding->char_width((const uint8_t *) " ", (size_t) 1); rbs_buffer_t rbs_buffer; - rbs_buffer_init(&state->allocator, &rbs_buffer); + rbs_buffer_init(&parser->allocator, &rbs_buffer); for (size_t i = 0; i < com->line_count; i++) { token tok = com->tokens[i]; - const char *comment_start = state->lexstate->string.start + tok.range.start.byte_pos + hash_bytes; + const char *comment_start = parser->lexstate->string.start + tok.range.start.byte_pos + hash_bytes; size_t comment_bytes = RBS_RANGE_BYTES(tok.range) - hash_bytes; rbs_string_t str = rbs_string_new( comment_start, - state->lexstate->string.end + parser->lexstate->string.end ); unsigned char c = rbs_utf8_to_codepoint(str); @@ -3124,18 +3124,18 @@ static rbs_ast_comment_t *parse_comment_lines(parserstate *state, comment *com) comment_bytes -= space_bytes; } - rbs_buffer_append_string(&state->allocator, &rbs_buffer, comment_start, comment_bytes); - rbs_buffer_append_cstr(&state->allocator, &rbs_buffer, "\n"); + rbs_buffer_append_string(&parser->allocator, &rbs_buffer, comment_start, comment_bytes); + rbs_buffer_append_cstr(&parser->allocator, &rbs_buffer, "\n"); } return rbs_ast_comment_new( - &state->allocator, - rbs_location_new(&state->allocator, (range) { .start = com->start, .end = com->end }), + &parser->allocator, + rbs_location_new(&parser->allocator, (range) { .start = com->start, .end = com->end }), rbs_buffer_to_string(&rbs_buffer) ); } -static comment *comment_get_comment(comment *com, int line) { +static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) { if (com == NULL) { return NULL; } @@ -3151,7 +3151,7 @@ static comment *comment_get_comment(comment *com, int line) { return comment_get_comment(com->next_comment, line); } -static void comment_insert_new_line(rbs_allocator_t *allocator, comment *com, token comment_token) { +static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, token comment_token) { if (com->line_count == 0) { com->start = comment_token.range.start; } @@ -3172,10 +3172,10 @@ static void comment_insert_new_line(rbs_allocator_t *allocator, comment *com, to com->end = comment_token.range.end; } -static comment *alloc_comment(rbs_allocator_t *allocator, token comment_token, comment *last_comment) { - comment *new_comment = rbs_allocator_alloc(allocator, comment); +static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, token comment_token, rbs_comment_t *last_comment) { + rbs_comment_t *new_comment = rbs_allocator_alloc(allocator, rbs_comment_t); - *new_comment = (comment) { + *new_comment = (rbs_comment_t) { .start = comment_token.range.start, .end = comment_token.range.end, @@ -3194,28 +3194,28 @@ static comment *alloc_comment(rbs_allocator_t *allocator, token comment_token, c /** * Insert new comment line token. * */ -static void insert_comment_line(parserstate *state, token tok) { +static void insert_comment_line(rbs_parser_t *parser, token tok) { int prev_line = tok.range.start.line - 1; - comment *com = comment_get_comment(state->last_comment, prev_line); + rbs_comment_t *com = comment_get_comment(parser->last_comment, prev_line); if (com) { - comment_insert_new_line(&state->allocator, com, tok); + comment_insert_new_line(&parser->allocator, com, tok); } else { - state->last_comment = alloc_comment(&state->allocator, tok, state->last_comment); + parser->last_comment = alloc_comment(&parser->allocator, tok, parser->last_comment); } } -bool parse_signature(parserstate *state, rbs_signature_t **signature) { +bool parse_signature(rbs_parser_t *parser, rbs_signature_t **signature) { range signature_range; - signature_range.start = state->current_token.range.start; + signature_range.start = parser->current_token.range.start; - rbs_node_list_t *dirs = rbs_node_list_new(&state->allocator); - rbs_node_list_t *decls = rbs_node_list_new(&state->allocator); + rbs_node_list_t *dirs = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *decls = rbs_node_list_new(&parser->allocator); - while (state->next_token.type == kUSE) { + while (parser->next_token.type == kUSE) { rbs_ast_directives_use_t *use_node; - CHECK_PARSE(parse_use_directive(state, &use_node)); + CHECK_PARSE(parse_use_directive(parser, &use_node)); if (use_node == NULL) { rbs_node_list_append(dirs, NULL); @@ -3224,14 +3224,14 @@ bool parse_signature(parserstate *state, rbs_signature_t **signature) { } } - while (state->next_token.type != pEOF) { + while (parser->next_token.type != pEOF) { rbs_node_t *decl = NULL; - CHECK_PARSE(parse_decl(state, &decl)); + CHECK_PARSE(parse_decl(parser, &decl)); rbs_node_list_append(decls, decl); } - signature_range.end = state->current_token.range.end; - *signature = rbs_signature_new(&state->allocator, rbs_location_new(&state->allocator, signature_range), dirs, decls); + signature_range.end = parser->current_token.range.end; + *signature = rbs_signature_new(&parser->allocator, rbs_location_new(&parser->allocator, signature_range), dirs, decls); return true; } @@ -3261,24 +3261,24 @@ id_table *alloc_reset_table(rbs_allocator_t *allocator) { return table; } -void parser_push_typevar_table(parserstate *state, bool reset) { +void parser_push_typevar_table(rbs_parser_t *parser, bool reset) { if (reset) { - id_table *table = alloc_reset_table(&state->allocator); - table->next = state->vars; - state->vars = table; + id_table *table = alloc_reset_table(&parser->allocator); + table->next = parser->vars; + parser->vars = table; } - id_table *table = alloc_empty_table(&state->allocator); - table->next = state->vars; - state->vars = table; + id_table *table = alloc_empty_table(&parser->allocator); + table->next = parser->vars; + parser->vars = table; } NODISCARD -bool parser_insert_typevar(parserstate *state, rbs_constant_id_t id) { - id_table *table = state->vars; +bool parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id) { + id_table *table = parser->vars; if (RESET_TABLE_P(table)) { - set_error(state, state->current_token, false, "Cannot insert to reset table"); + set_error(parser, parser->current_token, false, "Cannot insert to reset table"); return false; } @@ -3286,7 +3286,7 @@ bool parser_insert_typevar(parserstate *state, rbs_constant_id_t id) { // expand rbs_constant_id_t *ptr = table->ids; table->size += 10; - table->ids = rbs_allocator_calloc(&state->allocator, table->size, rbs_constant_id_t); + table->ids = rbs_allocator_calloc(&parser->allocator, table->size, rbs_constant_id_t); memcpy(table->ids, ptr, sizeof(rbs_constant_id_t) * table->count); } @@ -3295,30 +3295,30 @@ bool parser_insert_typevar(parserstate *state, rbs_constant_id_t id) { return true; } -void print_parser(parserstate *state) { - printf(" current_token = %s (%d...%d)\n", token_type_str(state->current_token.type), state->current_token.range.start.char_pos, state->current_token.range.end.char_pos); - printf(" next_token = %s (%d...%d)\n", token_type_str(state->next_token.type), state->next_token.range.start.char_pos, state->next_token.range.end.char_pos); - printf(" next_token2 = %s (%d...%d)\n", token_type_str(state->next_token2.type), state->next_token2.range.start.char_pos, state->next_token2.range.end.char_pos); - printf(" next_token3 = %s (%d...%d)\n", token_type_str(state->next_token3.type), state->next_token3.range.start.char_pos, state->next_token3.range.end.char_pos); +void print_parser(rbs_parser_t *parser) { + printf(" current_token = %s (%d...%d)\n", token_type_str(parser->current_token.type), parser->current_token.range.start.char_pos, parser->current_token.range.end.char_pos); + printf(" next_token = %s (%d...%d)\n", token_type_str(parser->next_token.type), parser->next_token.range.start.char_pos, parser->next_token.range.end.char_pos); + printf(" next_token2 = %s (%d...%d)\n", token_type_str(parser->next_token2.type), parser->next_token2.range.start.char_pos, parser->next_token2.range.end.char_pos); + printf(" next_token3 = %s (%d...%d)\n", token_type_str(parser->next_token3.type), parser->next_token3.range.start.char_pos, parser->next_token3.range.end.char_pos); } -void parser_advance(parserstate *state) { - state->current_token = state->next_token; - state->next_token = state->next_token2; - state->next_token2 = state->next_token3; +void parser_advance(rbs_parser_t *parser) { + parser->current_token = parser->next_token; + parser->next_token = parser->next_token2; + parser->next_token2 = parser->next_token3; while (true) { - if (state->next_token3.type == pEOF) { + if (parser->next_token3.type == pEOF) { break; } - state->next_token3 = rbsparser_next_token(state->lexstate); + parser->next_token3 = rbsparser_next_token(parser->lexstate); - if (state->next_token3.type == tCOMMENT) { + if (parser->next_token3.type == tCOMMENT) { // skip - } else if (state->next_token3.type == tLINECOMMENT) { - insert_comment_line(state, state->next_token3); - } else if (state->next_token3.type == tTRIVIA) { + } else if (parser->next_token3.type == tLINECOMMENT) { + insert_comment_line(parser, parser->next_token3); + } else if (parser->next_token3.type == tTRIVIA) { //skip } else { break; @@ -3335,13 +3335,13 @@ void rbs_print_token(token tok) { ); } -rbs_ast_comment_t *get_comment(parserstate *state, int subject_line) { +rbs_ast_comment_t *get_comment(rbs_parser_t *parser, int subject_line) { int comment_line = subject_line - 1; - comment *com = comment_get_comment(state->last_comment, comment_line); + rbs_comment_t *com = comment_get_comment(parser->last_comment, comment_line); if (com) { - return parse_comment_lines(state, com); + return parse_comment_lines(parser, com); } else { return NULL; } @@ -3375,14 +3375,14 @@ lexstate *alloc_lexer(rbs_allocator_t *allocator, rbs_string_t string, const rbs return lexer; } -parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { +rbs_parser_t *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { rbs_allocator_t allocator; rbs_allocator_init(&allocator); lexstate *lexer = alloc_lexer(&allocator, string, encoding, start_pos, end_pos); - parserstate *parser = rbs_allocator_alloc(&allocator, parserstate); + rbs_parser_t *parser = rbs_allocator_alloc(&allocator, rbs_parser_t); - *parser = (parserstate) { + *parser = (rbs_parser_t) { .lexstate = lexer, .current_token = NullToken, @@ -3425,13 +3425,13 @@ parserstate *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, i return parser; } -void free_parser(parserstate *parser) { +void free_parser(rbs_parser_t *parser) { rbs_constant_pool_free(&parser->constant_pool); rbs_allocator_free(&parser->allocator); } -void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt, ...) { - if (state->error) { +void set_error(rbs_parser_t *parser, token tok, bool syntax_error, const char *fmt, ...) { + if (parser->error) { return; } @@ -3441,14 +3441,14 @@ void set_error(parserstate *state, token tok, bool syntax_error, const char *fmt int length = vsnprintf(NULL, 0, fmt, args); va_end(args); - char *message = rbs_allocator_alloc_many(&state->allocator, length + 1, char); + char *message = rbs_allocator_alloc_many(&parser->allocator, length + 1, char); va_start(args, fmt); vsnprintf(message, length + 1, fmt, args); va_end(args); - state->error = rbs_allocator_alloc(&state->allocator, error); - state->error->token = tok; - state->error->message = message; - state->error->syntax_error = syntax_error; + parser->error = rbs_allocator_alloc(&parser->allocator, rbs_error_t); + parser->error->token = tok; + parser->error->message = message; + parser->error->syntax_error = syntax_error; } From 67b2fe1bc7368de503c979bcce2ffe466fb2b46d Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Sat, 29 Mar 2025 04:54:40 +0800 Subject: [PATCH 098/111] Rename `position`, `token`, and `range`types (#30) These names are too generic and can easily conflict in projects. New names are: - `rbs_position_t` - `rbs_range_t` - `rbs_token_t` --- ext/rbs_extension/location.c | 22 ++- ext/rbs_extension/location.h | 2 +- ext/rbs_extension/main.c | 10 +- include/rbs/lexer.h | 42 ++--- include/rbs/parser.h | 18 +- include/rbs/rbs_location.h | 8 +- src/lexer.c | 2 +- src/lexer.re | 2 +- src/lexstate.c | 22 +-- src/parser.c | 322 +++++++++++++++++------------------ src/rbs_location.c | 6 +- 11 files changed, 227 insertions(+), 229 deletions(-) diff --git a/ext/rbs_extension/location.c b/ext/rbs_extension/location.c index dd58373d3..62dd1ee12 100644 --- a/ext/rbs_extension/location.c +++ b/ext/rbs_extension/location.c @@ -9,17 +9,15 @@ rbs_loc_range RBS_LOC_NULL_RANGE = { -1, -1 }; VALUE RBS_Location; -position rbs_loc_position(int char_pos) { - position pos = { 0, char_pos, -1, -1 }; - return pos; +rbs_position_t rbs_loc_position(int char_pos) { + return (rbs_position_t) { 0, char_pos, -1, -1 }; } -position rbs_loc_position3(int char_pos, int line, int column) { - position pos = { 0, char_pos, line, column }; - return pos; +rbs_position_t rbs_loc_position3(int char_pos, int line, int column) { + return (rbs_position_t) { 0, char_pos, line, column }; } -static rbs_loc_range rbs_new_loc_range(range rg) { +static rbs_loc_range rbs_new_loc_range(rbs_range_t rg) { rbs_loc_range r = { rg.start.char_pos, rg.end.char_pos }; return r; } @@ -57,7 +55,7 @@ static void check_children_cap(rbs_loc *loc) { } } -void rbs_loc_legacy_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, range r) { +void rbs_loc_legacy_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, rbs_range_t r) { check_children_cap(loc); unsigned short i = loc->children->len++; @@ -67,7 +65,7 @@ void rbs_loc_legacy_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, ran }; } -void rbs_loc_legacy_add_required_child(rbs_loc *loc, rbs_constant_id_t name, range r) { +void rbs_loc_legacy_add_required_child(rbs_loc *loc, rbs_constant_id_t name, rbs_range_t r) { rbs_loc_legacy_add_optional_child(loc, name, r); unsigned short last_index = loc->children->len - 1; @@ -181,7 +179,7 @@ static rbs_constant_id_t rbs_constant_pool_insert_ruby_symbol(VALUE symbol) { static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VALUE end) { rbs_loc *loc = rbs_check_location(self); - range rg; + rbs_range_t rg; rg.start = rbs_loc_position(FIX2INT(start)); rg.end = rbs_loc_position(FIX2INT(end)); @@ -193,7 +191,7 @@ static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VA static VALUE location_add_optional_child(VALUE self, VALUE name, VALUE start, VALUE end) { rbs_loc *loc = rbs_check_location(self); - range rg; + rbs_range_t rg; rg.start = rbs_loc_position(FIX2INT(start)); rg.end = rbs_loc_position(FIX2INT(end)); @@ -210,7 +208,7 @@ static VALUE location_add_optional_no_child(VALUE self, VALUE name) { return Qnil; } -VALUE rbs_new_location(VALUE buffer, range rg) { +VALUE rbs_new_location(VALUE buffer, rbs_range_t rg) { rbs_loc *loc; VALUE obj = TypedData_Make_Struct(RBS_Location, rbs_loc, &location_type, loc); diff --git a/ext/rbs_extension/location.h b/ext/rbs_extension/location.h index 2c1446f63..1663296c4 100644 --- a/ext/rbs_extension/location.h +++ b/ext/rbs_extension/location.h @@ -18,7 +18,7 @@ typedef struct { /** * Returns new RBS::Location object, with given buffer and range. * */ -VALUE rbs_new_location(VALUE buffer, range rg); +VALUE rbs_new_location(VALUE buffer, rbs_range_t rg); /** * Return rbs_loc associated with the RBS::Location object. diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index b2c2f0860..e7e0838ef 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -23,7 +23,7 @@ static NORETURN(void) raise_error(rbs_error_t *error, VALUE buffer) { } VALUE location = rbs_new_location(buffer, error->token.range); - VALUE type = rb_str_new_cstr(token_type_str(error->token.type)); + VALUE type = rb_str_new_cstr(rbs_token_type_str(error->token.type)); VALUE rb_error = rb_funcall( RBS_ParsingError, @@ -112,7 +112,7 @@ static VALUE parse_type_try(VALUE a) { if (RB_TEST(arg->require_eof)) { parser_advance(parser); if (parser->current_token.type != pEOF) { - set_error(parser, parser->current_token, true, "expected a token `%s`", token_type_str(pEOF)); + set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF)); raise_error(parser->error, arg->buffer); } } @@ -202,7 +202,7 @@ static VALUE parse_method_type_try(VALUE a) { if (RB_TEST(arg->require_eof)) { parser_advance(parser); if (parser->current_token.type != pEOF) { - set_error(parser, parser->current_token, true, "expected a token `%s`", token_type_str(pEOF)); + set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF)); raise_error(parser->error, arg->buffer); } } @@ -289,10 +289,10 @@ static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) { lexstate *lexer = alloc_lexer_from_buffer(&allocator, string, encoding, 0, FIX2INT(end_pos)); VALUE results = rb_ary_new(); - token token = NullToken; + rbs_token_t token = NullToken; while (token.type != pEOF) { token = rbsparser_next_token(lexer); - VALUE type = ID2SYM(rb_intern(token_type_str(token.type))); + VALUE type = ID2SYM(rb_intern(rbs_token_type_str(token.type))); VALUE location = rbs_new_location(buffer, token.range); VALUE pair = rb_ary_new3(2, type, location); rb_ary_push(results, pair); diff --git a/include/rbs/lexer.h b/include/rbs/lexer.h index 4d5e8e143..d1c9b9f0c 100644 --- a/include/rbs/lexer.h +++ b/include/rbs/lexer.h @@ -103,17 +103,17 @@ typedef struct { int char_pos; int line; int column; -} position; +} rbs_position_t; typedef struct { - position start; - position end; -} range; + rbs_position_t start; + rbs_position_t end; +} rbs_range_t; typedef struct { enum RBSTokenType type; - range range; -} token; + rbs_range_t range; +} rbs_token_t; /** * The lexer state is the curren token. @@ -129,27 +129,27 @@ typedef struct { rbs_string_t string; int start_pos; /* The character position that defines the start of the input */ int end_pos; /* The character position that defines the end of the input */ - position current; /* The current position */ - position start; /* The start position of the current token */ + rbs_position_t current; /* The current position */ + rbs_position_t start; /* The start position of the current token */ bool first_token_of_line; /* This flag is used for tLINECOMMENT */ unsigned int last_char; /* Last peeked character */ const rbs_encoding_t *encoding; } lexstate; -extern token NullToken; -extern position NullPosition; -extern range NULL_RANGE; +extern rbs_token_t NullToken; +extern rbs_position_t NullPosition; +extern rbs_range_t NULL_RANGE; -char *rbs_peek_token(lexstate *state, token tok); -int rbs_token_chars(token tok); -int rbs_token_bytes(token tok); +char *rbs_peek_token(lexstate *state, rbs_token_t tok); +int rbs_token_chars(rbs_token_t tok); +int rbs_token_bytes(rbs_token_t tok); #define rbs_null_position_p(pos) (pos.byte_pos == -1) #define rbs_null_range_p(range) (range.start.byte_pos == -1) #define rbs_nonnull_pos_or(pos1, pos2) (rbs_null_position_p(pos1) ? pos2 : pos1) #define RBS_RANGE_BYTES(range) (range.end.byte_pos - range.start.byte_pos) -const char *token_type_str(enum RBSTokenType type); +const char *rbs_token_type_str(enum RBSTokenType type); /** * Read next character. @@ -167,17 +167,17 @@ void rbs_skip(lexstate *state); void rbs_skipn(lexstate *state, size_t size); /** - * Return new token with given type. + * Return new rbs_token_t with given type. * */ -token rbs_next_token(lexstate *state, enum RBSTokenType type); +rbs_token_t rbs_next_token(lexstate *state, enum RBSTokenType type); /** - * Return new token with EOF type. + * Return new rbs_token_t with EOF type. * */ -token rbs_next_eof_token(lexstate *state); +rbs_token_t rbs_next_eof_token(lexstate *state); -token rbsparser_next_token(lexstate *state); +rbs_token_t rbsparser_next_token(lexstate *state); -void rbs_print_token(token tok); +void rbs_print_token(rbs_token_t tok); #endif diff --git a/include/rbs/parser.h b/include/rbs/parser.h index 800dce100..756b859aa 100644 --- a/include/rbs/parser.h +++ b/include/rbs/parser.h @@ -24,19 +24,19 @@ * A comment object represents the six lines of comments. * */ typedef struct rbs_comment_t { - position start; - position end; + rbs_position_t start; + rbs_position_t end; size_t line_size; size_t line_count; - token *tokens; + rbs_token_t *tokens; struct rbs_comment_t *next_comment; } rbs_comment_t; typedef struct rbs_error_t { char *message; - token token; + rbs_token_t token; bool syntax_error; } rbs_error_t; @@ -46,10 +46,10 @@ typedef struct rbs_error_t { typedef struct { lexstate *lexstate; - token current_token; - token next_token; /* The first lookahead token */ - token next_token2; /* The second lookahead token */ - token next_token3; /* The third lookahead token */ + rbs_token_t current_token; + rbs_token_t next_token; /* The first lookahead token */ + rbs_token_t next_token2; /* The second lookahead token */ + rbs_token_t next_token3; /* The third lookahead token */ struct id_table *vars; /* Known type variables */ rbs_comment_t *last_comment; /* Last read comment */ @@ -124,7 +124,7 @@ void print_parser(rbs_parser_t *parser); * */ rbs_ast_comment_t *get_comment(rbs_parser_t *parser, int subject_line); -void set_error(rbs_parser_t *parser, token tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5); +void set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5); bool parse_type(rbs_parser_t *parser, rbs_node_t **type); bool parse_method_type(rbs_parser_t *parser, rbs_methodtype_t **method_type); diff --git a/include/rbs/rbs_location.h b/include/rbs/rbs_location.h index 87c016a68..cd925abb8 100644 --- a/include/rbs/rbs_location.h +++ b/include/rbs/rbs_location.h @@ -8,17 +8,17 @@ #include "rbs/rbs_location_internals.h" typedef struct rbs_location { - range rg; + rbs_range_t rg; rbs_loc_children *children; } rbs_location_t; void rbs_loc_alloc_children(rbs_allocator_t *, rbs_location_t *loc, size_t capacity); -void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, range r); -void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r); +void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, rbs_range_t r); +void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, rbs_range_t r); /** * Allocate new rbs_location_t object through the given allocator. * */ -rbs_location_t *rbs_location_new(rbs_allocator_t *, range rg); +rbs_location_t *rbs_location_new(rbs_allocator_t *, rbs_range_t rg); #endif diff --git a/src/lexer.c b/src/lexer.c index bd76768fa..df214a939 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -2,7 +2,7 @@ #line 1 "src/lexer.re" #include "rbs/lexer.h" -token rbsparser_next_token(lexstate *state) { +rbs_token_t rbsparser_next_token(lexstate *state) { lexstate backup; backup = *state; diff --git a/src/lexer.re b/src/lexer.re index 7de2a7101..df5a148c1 100644 --- a/src/lexer.re +++ b/src/lexer.re @@ -1,6 +1,6 @@ #include "rbs/lexer.h" -token rbsparser_next_token(lexstate *state) { +rbs_token_t rbsparser_next_token(lexstate *state) { lexstate backup; backup = *state; diff --git a/src/lexstate.c b/src/lexstate.c index 7bdbd32ed..18be9c6be 100644 --- a/src/lexstate.c +++ b/src/lexstate.c @@ -89,19 +89,19 @@ static const char *RBS_TOKENTYPE_NAMES[] = { "tANNOTATION", /* Annotation */ }; -token NullToken = { .type = NullType, .range = {} }; -position NullPosition = { -1, -1, -1, -1 }; -range NULL_RANGE = { { -1, -1, -1, -1 }, { -1, -1, -1, -1 } }; +rbs_token_t NullToken = { .type = NullType, .range = {} }; +rbs_position_t NullPosition = { -1, -1, -1, -1 }; +rbs_range_t NULL_RANGE = { { -1, -1, -1, -1 }, { -1, -1, -1, -1 } }; -const char *token_type_str(enum RBSTokenType type) { +const char *rbs_token_type_str(enum RBSTokenType type) { return RBS_TOKENTYPE_NAMES[type]; } -int rbs_token_chars(token tok) { +int rbs_token_chars(rbs_token_t tok) { return tok.range.end.char_pos - tok.range.start.char_pos; } -int rbs_token_bytes(token tok) { +int rbs_token_bytes(rbs_token_t tok) { return RBS_RANGE_BYTES(tok.range); } @@ -120,8 +120,8 @@ unsigned int rbs_peek(lexstate *state) { } } -token rbs_next_token(lexstate *state, enum RBSTokenType type) { - token t; +rbs_token_t rbs_next_token(lexstate *state, enum RBSTokenType type) { + rbs_token_t t; t.type = type; t.range.start = state->start; @@ -134,10 +134,10 @@ token rbs_next_token(lexstate *state, enum RBSTokenType type) { return t; } -token rbs_next_eof_token(lexstate *state) { +rbs_token_t rbs_next_eof_token(lexstate *state) { if ((size_t) state->current.byte_pos == rbs_string_len(state->string) + 1) { // End of String - token t; + rbs_token_t t; t.type = pEOF; t.range.start = state->start; t.range.end = state->start; @@ -183,6 +183,6 @@ void rbs_skipn(lexstate *state, size_t size) { } } -char *rbs_peek_token(lexstate *state, token tok) { +char *rbs_peek_token(lexstate *state, rbs_token_t tok) { return (char *) state->string.start + tok.range.start.byte_pos; } diff --git a/src/parser.c b/src/parser.c index acb5ab0e5..a7df38541 100644 --- a/src/parser.c +++ b/src/parser.c @@ -70,7 +70,7 @@ #define ASSERT_TOKEN(parser, expected_type) \ if (parser->current_token.type != expected_type) { \ - set_error(parser, parser->current_token, true, "expected a token `%s`", token_type_str(expected_type)); \ + set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(expected_type)); \ return false; \ } @@ -123,7 +123,7 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type); * @returns A borrowed copy of the current token, which does *not* need to be freed. */ static rbs_string_t rbs_parser_peek_current_token(rbs_parser_t *parser) { - range rg = parser->current_token.range; + rbs_range_t rg = parser->current_token.range; const char *start = parser->lexstate->string.start + rg.start.byte_pos; size_t length = rg.end.byte_pos - rg.start.byte_pos; @@ -155,7 +155,7 @@ static void parser_advance_no_gap(rbs_parser_t *parser) { | {} */ NODISCARD -static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, range *rg, rbs_typename_t **typename) { +static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, rbs_range_t *rg, rbs_typename_t **typename) { bool absolute = false; if (rg) { @@ -184,7 +184,7 @@ static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, range *rg, parser_advance(parser); } - range namespace_range = { + rbs_range_t namespace_range = { .start = rg->start, .end = parser->current_token.range.end }; @@ -288,14 +288,14 @@ static bool is_keyword_token(enum RBSTokenType type) { */ NODISCARD static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_t **function_param) { - range type_range; + rbs_range_t type_range; type_range.start = parser->next_token.range.start; rbs_node_t *type; CHECK_PARSE(parse_type(parser, &type)); type_range.end = parser->current_token.range.end; if (parser->next_token.type == pCOMMA || parser->next_token.type == pRPAREN) { - range param_range = type_range; + rbs_range_t param_range = type_range; rbs_location_t *loc = rbs_location_new(&parser->allocator, param_range); rbs_loc_alloc_children(&parser->allocator, loc, 1); @@ -304,11 +304,11 @@ static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_ *function_param = rbs_types_function_param_new(&parser->allocator, loc, type, NULL); return true; } else { - range name_range = parser->next_token.range; + rbs_range_t name_range = parser->next_token.range; parser_advance(parser); - range param_range = { + rbs_range_t param_range = { .start = type_range.start, .end = name_range.end, }; @@ -332,7 +332,7 @@ static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_ } } -static rbs_constant_id_t intern_token_start_end(rbs_parser_t *parser, token start_token, token end_token) { +static rbs_constant_id_t intern_token_start_end(rbs_parser_t *parser, rbs_token_t start_token, rbs_token_t end_token) { return rbs_constant_pool_insert_shared_with_encoding( &parser->constant_pool, (const uint8_t *) rbs_peek_token(parser->lexstate, start_token), @@ -625,7 +625,7 @@ static bool parse_params(rbs_parser_t *parser, method_params *params) { */ NODISCARD static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional) { - range rg; + rbs_range_t rg; rg.start = parser->next_token.range.start; rbs_node_t *type = NULL; @@ -692,7 +692,7 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse rbs_node_t *function = NULL; rbs_types_block_t *block = NULL; rbs_node_t *function_self_type = NULL; - range function_range; + rbs_range_t function_range; function_range.start = parser->current_token.range.start; method_params params; @@ -800,12 +800,12 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse */ NODISCARD static bool parse_proc_type(rbs_parser_t *parser, rbs_types_proc_t **proc) { - position start = parser->current_token.range.start; + rbs_position_t start = parser->current_token.range.start; parse_function_result *result = rbs_allocator_alloc(&parser->allocator, parse_function_result); CHECK_PARSE(parse_function(parser, true, &result)); - position end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, (range) { .start = start, .end = end }); + rbs_position_t end = parser->current_token.range.end; + rbs_location_t *loc = rbs_location_new(&parser->allocator, (rbs_range_t) { .start = start, .end = end }); *proc = rbs_types_proc_new(&parser->allocator, loc, result->function, result->block, result->function_self_type); return true; } @@ -873,7 +873,7 @@ static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields) { ADVANCE_ASSERT(parser, pFATARROW); } - range field_range; + rbs_range_t field_range; field_range.start = parser->current_token.range.end; rbs_node_t *type; @@ -954,7 +954,7 @@ static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node expected_kind |= ALIAS_NAME; } - range name_range; + rbs_range_t name_range; rbs_typename_t *typename = NULL; CHECK_PARSE(parse_type_name(parser, expected_kind, &name_range, &typename)); @@ -972,7 +972,7 @@ static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node return false; } - range args_range; + rbs_range_t args_range; if (parser->next_token.type == pLBRACKET) { parser_advance(parser); args_range.start = parser->current_token.range.start; @@ -983,7 +983,7 @@ static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node args_range = NULL_RANGE; } - range type_range = { + rbs_range_t type_range = { .start = name_range.start, .end = rbs_nonnull_pos_or(args_range.end, name_range.end), }; @@ -1011,12 +1011,12 @@ NODISCARD static bool parse_singleton_type(rbs_parser_t *parser, rbs_types_classsingleton_t **singleton) { ASSERT_TOKEN(parser, kSINGLETON); - range type_range; + rbs_range_t type_range; type_range.start = parser->current_token.range.start; ADVANCE_ASSERT(parser, pLPAREN); parser_advance(parser); - range name_range; + rbs_range_t name_range; rbs_typename_t *typename = NULL; CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &name_range, &typename)); @@ -1192,7 +1192,7 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) { return true; } case pLBRACKET: { - range rg; + rbs_range_t rg; rg.start = parser->current_token.range.start; rbs_node_list_t *types = rbs_node_list_new(&parser->allocator); if (parser->next_token.type != pRBRACKET) { @@ -1212,12 +1212,12 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) { return true; } case pLBRACE: { - position start = parser->current_token.range.start; + rbs_position_t start = parser->current_token.range.start; rbs_hash_t *fields = NULL; CHECK_PARSE(parse_record_attributes(parser, &fields)); ADVANCE_ASSERT(parser, pRBRACE); - position end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, (range) { .start = start, .end = end }); + rbs_position_t end = parser->current_token.range.end; + rbs_location_t *loc = rbs_location_new(&parser->allocator, (rbs_range_t) { .start = start, .end = end }); *type = (rbs_node_t *) rbs_types_record_new(&parser->allocator, loc, fields); return true; } @@ -1239,7 +1239,7 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) { */ NODISCARD static bool parse_intersection(rbs_parser_t *parser, rbs_node_t **type) { - range rg; + rbs_range_t rg; rg.start = parser->next_token.range.start; rbs_node_t *optional = NULL; @@ -1271,7 +1271,7 @@ static bool parse_intersection(rbs_parser_t *parser, rbs_node_t **type) { | {} */ bool parse_type(rbs_parser_t *parser, rbs_node_t **type) { - range rg; + rbs_range_t rg; rg.start = parser->next_token.range.start; rbs_node_list_t *union_types = rbs_node_list_new(&parser->allocator); @@ -1305,7 +1305,7 @@ bool parse_type(rbs_parser_t *parser, rbs_node_t **type) { type_param ::= tUIDENT upper_bound? default_type? (module_type_params == false) */ NODISCARD -static bool parse_type_params(rbs_parser_t *parser, range *rg, bool module_type_params, rbs_node_list_t **params) { +static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module_type_params, rbs_node_list_t **params) { *params = rbs_node_list_new(&parser->allocator); bool required_param_allowed = true; @@ -1321,11 +1321,11 @@ static bool parse_type_params(rbs_parser_t *parser, range *rg, bool module_type_ rbs_node_t *upper_bound = NULL; rbs_node_t *default_type = NULL; - range param_range; + rbs_range_t param_range; param_range.start = parser->next_token.range.start; - range unchecked_range = NULL_RANGE; - range variance_range = NULL_RANGE; + rbs_range_t unchecked_range = NULL_RANGE; + rbs_range_t variance_range = NULL_RANGE; if (module_type_params) { if (parser->next_token.type == kUNCHECKED) { unchecked = true; @@ -1352,7 +1352,7 @@ static bool parse_type_params(rbs_parser_t *parser, range *rg, bool module_type_ } ADVANCE_ASSERT(parser, tUIDENT); - range name_range = parser->current_token.range; + rbs_range_t name_range = parser->current_token.range; rbs_string_t string = rbs_parser_peek_current_token(parser); rbs_location_t *nameSymbolLoc = rbs_location_current_token(parser); @@ -1361,7 +1361,7 @@ static bool parse_type_params(rbs_parser_t *parser, range *rg, bool module_type_ CHECK_PARSE(parser_insert_typevar(parser, id)); - range upper_bound_range = NULL_RANGE; + rbs_range_t upper_bound_range = NULL_RANGE; if (parser->next_token.type == pLT) { parser_advance(parser); upper_bound_range.start = parser->current_token.range.start; @@ -1369,7 +1369,7 @@ static bool parse_type_params(rbs_parser_t *parser, range *rg, bool module_type_ upper_bound_range.end = parser->current_token.range.end; } - range default_type_range = NULL_RANGE; + rbs_range_t default_type_range = NULL_RANGE; if (module_type_params) { if (parser->next_token.type == pEQ) { parser_advance(parser); @@ -1446,14 +1446,14 @@ static bool parser_pop_typevar_table(rbs_parser_t *parser) { bool parse_method_type(rbs_parser_t *parser, rbs_methodtype_t **method_type) { parser_push_typevar_table(parser, false); - range rg; + rbs_range_t rg; rg.start = parser->next_token.range.start; - range params_range = NULL_RANGE; + rbs_range_t params_range = NULL_RANGE; rbs_node_list_t *type_params; CHECK_PARSE(parse_type_params(parser, ¶ms_range, false, &type_params)); - range type_range; + rbs_range_t type_range; type_range.start = parser->next_token.range.start; parse_function_result *result = rbs_allocator_alloc(&parser->allocator, parse_function_result); @@ -1478,18 +1478,18 @@ bool parse_method_type(rbs_parser_t *parser, rbs_methodtype_t **method_type) { */ NODISCARD static bool parse_global_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_ast_declarations_global_t **global) { - range decl_range; + rbs_range_t decl_range; decl_range.start = parser->current_token.range.start; rbs_ast_comment_t *comment = get_comment(parser, decl_range.start.line); - range name_range = parser->current_token.range; + rbs_range_t name_range = parser->current_token.range; rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, name_range); rbs_ast_symbol_t *typename = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); ADVANCE_ASSERT(parser, pCOLON); - range colon_range = parser->current_token.range; + rbs_range_t colon_range = parser->current_token.range; rbs_node_t *type; CHECK_PARSE(parse_type(parser, &type)); @@ -1509,17 +1509,17 @@ static bool parse_global_decl(rbs_parser_t *parser, rbs_node_list_t *annotations */ NODISCARD static bool parse_const_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_ast_declarations_constant_t **constant) { - range decl_range; + rbs_range_t decl_range; decl_range.start = parser->current_token.range.start; rbs_ast_comment_t *comment = get_comment(parser, decl_range.start.line); - range name_range; + rbs_range_t name_range; rbs_typename_t *typename = NULL; CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &name_range, &typename)); ADVANCE_ASSERT(parser, pCOLON); - range colon_range = parser->current_token.range; + rbs_range_t colon_range = parser->current_token.range; rbs_node_t *type; CHECK_PARSE(parse_type(parser, &type)); @@ -1539,27 +1539,27 @@ static bool parse_const_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, type_decl ::= {kTYPE} alias_name `=` */ NODISCARD -static bool parse_type_decl(rbs_parser_t *parser, position comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_typealias_t **typealias) { +static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_typealias_t **typealias) { parser_push_typevar_table(parser, true); - range decl_range; + rbs_range_t decl_range; decl_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, decl_range.start); - range keyword_range = parser->current_token.range; + rbs_range_t keyword_range = parser->current_token.range; parser_advance(parser); - range name_range; + rbs_range_t name_range; rbs_typename_t *typename = NULL; CHECK_PARSE(parse_type_name(parser, ALIAS_NAME, &name_range, &typename)); - range params_range; + rbs_range_t params_range; rbs_node_list_t *type_params; CHECK_PARSE(parse_type_params(parser, ¶ms_range, true, &type_params)); ADVANCE_ASSERT(parser, pEQ); - range eq_range = parser->current_token.range; + rbs_range_t eq_range = parser->current_token.range; rbs_node_t *type; CHECK_PARSE(parse_type(parser, &type)); @@ -1586,7 +1586,7 @@ static bool parse_type_decl(rbs_parser_t *parser, position comment_pos, rbs_node */ NODISCARD static bool parse_annotation(rbs_parser_t *parser, rbs_ast_annotation_t **annotation) { - range rg = parser->current_token.range; + rbs_range_t rg = parser->current_token.range; size_t offset_bytes = parser->lexstate->encoding->char_width((const uint8_t *) "%", (size_t) 1) + @@ -1643,7 +1643,7 @@ static bool parse_annotation(rbs_parser_t *parser, rbs_ast_annotation_t **annota | {<>} */ NODISCARD -static bool parse_annotations(rbs_parser_t *parser, rbs_node_list_t *annotations, position *annot_pos) { +static bool parse_annotations(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_position_t *annot_pos) { *annot_pos = NullPosition; while (true) { @@ -1670,7 +1670,7 @@ static bool parse_annotations(rbs_parser_t *parser, rbs_node_list_t *annotations | {} (IDENT | keyword)~<`?`> */ NODISCARD -static bool parse_method_name(rbs_parser_t *parser, range *range, rbs_ast_symbol_t **symbol) { +static bool parse_method_name(rbs_parser_t *parser, rbs_range_t *range, rbs_ast_symbol_t **symbol) { parser_advance(parser); switch (parser->current_token.type) @@ -1750,11 +1750,11 @@ typedef enum { @param allow_selfq `true` to accept `self?` kind. */ -static InstanceSingletonKind parse_instance_singleton_kind(rbs_parser_t *parser, bool allow_selfq, range *rg) { +static InstanceSingletonKind parse_instance_singleton_kind(rbs_parser_t *parser, bool allow_selfq, rbs_range_t *rg) { InstanceSingletonKind kind = INSTANCE_KIND; if (parser->next_token.type == kSELF) { - range self_range = parser->next_token.range; + rbs_range_t self_range = parser->next_token.range; if (parser->next_token2.type == pDOT) { parser_advance(parser); @@ -1771,7 +1771,7 @@ static InstanceSingletonKind parse_instance_singleton_kind(rbs_parser_t *parser, kind = INSTANCE_SINGLETON_KIND; } - *rg = (range) { + *rg = (rbs_range_t) { .start = self_range.start, .end = parser->current_token.range.end, }; @@ -1795,14 +1795,14 @@ static InstanceSingletonKind parse_instance_singleton_kind(rbs_parser_t *parser, * @param accept_overload `true` to accept overloading (...) definition. * */ NODISCARD -static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool accept_overload, position comment_pos, rbs_node_list_t *annotations, rbs_ast_members_methoddefinition_t **method_definition) { - range member_range; +static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool accept_overload, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_members_methoddefinition_t **method_definition) { + rbs_range_t member_range; member_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); - range visibility_range; + rbs_range_t visibility_range; rbs_keyword_t *visibility; switch (parser->current_token.type) { @@ -1826,9 +1826,9 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce break; } - range keyword_range = parser->current_token.range; + rbs_range_t keyword_range = parser->current_token.range; - range kind_range; + rbs_range_t kind_range; InstanceSingletonKind kind; if (instance_only) { kind_range = NULL_RANGE; @@ -1837,7 +1837,7 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce kind = parse_instance_singleton_kind(parser, visibility == NULL, &kind_range); } - range name_range; + rbs_range_t name_range; rbs_ast_symbol_t *name = NULL; CHECK_PARSE(parse_method_name(parser, &name_range, &name)); @@ -1854,13 +1854,13 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce rbs_node_list_t *overloads = rbs_node_list_new(&parser->allocator); bool overloading = false; - range overloading_range = NULL_RANGE; + rbs_range_t overloading_range = NULL_RANGE; bool loop = true; while (loop) { rbs_node_list_t *annotations = rbs_node_list_new(&parser->allocator); - position overload_annot_pos = NullPosition; + rbs_position_t overload_annot_pos = NullPosition; - range overload_range; + rbs_range_t overload_range; overload_range.start = parser->current_token.range.start; if (parser->next_token.type == tANNOTATION) { @@ -1950,7 +1950,7 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce * @param kind * */ NODISCARD -static bool class_instance_name(rbs_parser_t *parser, TypeNameKind kind, rbs_node_list_t *args, range *name_range, range *args_range, rbs_typename_t **name) { +static bool class_instance_name(rbs_parser_t *parser, TypeNameKind kind, rbs_node_list_t *args, rbs_range_t *name_range, rbs_range_t *args_range, rbs_typename_t **name) { parser_advance(parser); rbs_typename_t *typename = NULL; @@ -1978,13 +1978,13 @@ static bool class_instance_name(rbs_parser_t *parser, TypeNameKind kind, rbs_nod * @param from_interface `true` when the member is in an interface. * */ NODISCARD -static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **mixin_member) { - range member_range; +static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **mixin_member) { + rbs_range_t member_range; member_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); enum RBSTokenType type = parser->current_token.type; - range keyword_range = parser->current_token.range; + rbs_range_t keyword_range = parser->current_token.range; bool reset_typevar_scope; switch (type) @@ -2013,8 +2013,8 @@ static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, positi parser_push_typevar_table(parser, reset_typevar_scope); rbs_node_list_t *args = rbs_node_list_new(&parser->allocator); - range name_range; - range args_range = NULL_RANGE; + rbs_range_t name_range; + rbs_range_t args_range = NULL_RANGE; rbs_typename_t *name = NULL; CHECK_PARSE(class_instance_name( parser, @@ -2059,17 +2059,17 @@ static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, positi * @param[in] instance_only `true` to reject `self.` alias. * */ NODISCARD -static bool parse_alias_member(rbs_parser_t *parser, bool instance_only, position comment_pos, rbs_node_list_t *annotations, rbs_ast_members_alias_t **alias_member) { - range member_range; +static bool parse_alias_member(rbs_parser_t *parser, bool instance_only, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_members_alias_t **alias_member) { + rbs_range_t member_range; member_range.start = parser->current_token.range.start; - range keyword_range = parser->current_token.range; + rbs_range_t keyword_range = parser->current_token.range; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); rbs_keyword_t *kind; rbs_ast_symbol_t *new_name, *old_name; - range new_kind_range, old_kind_range, new_name_range, old_name_range; + rbs_range_t new_kind_range, old_kind_range, new_name_range, old_name_range; if (!instance_only && parser->next_token.type == kSELF) { kind = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("singleton")); @@ -2112,13 +2112,13 @@ static bool parse_alias_member(rbs_parser_t *parser, bool instance_only, positio | {tA2IDENT} `:` */ NODISCARD -static bool parse_variable_member(rbs_parser_t *parser, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **variable_member) { +static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **variable_member) { if (annotations->length > 0) { set_error(parser, parser->current_token, true, "annotation cannot be given to variable members"); return false; } - range member_range; + rbs_range_t member_range; member_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); @@ -2126,12 +2126,12 @@ static bool parse_variable_member(rbs_parser_t *parser, position comment_pos, rb switch (parser->current_token.type) { case tAIDENT: { - range name_range = parser->current_token.range; + rbs_range_t name_range = parser->current_token.range; rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, name_range); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); ADVANCE_ASSERT(parser, pCOLON); - range colon_range = parser->current_token.range; + rbs_range_t colon_range = parser->current_token.range; rbs_node_t *type; CHECK_PARSE(parse_type(parser, &type)); @@ -2147,12 +2147,12 @@ static bool parse_variable_member(rbs_parser_t *parser, position comment_pos, rb return true; } case tA2IDENT: { - range name_range = parser->current_token.range; + rbs_range_t name_range = parser->current_token.range; rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, name_range); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); ADVANCE_ASSERT(parser, pCOLON); - range colon_range = parser->current_token.range; + rbs_range_t colon_range = parser->current_token.range; parser_push_typevar_table(parser, true); @@ -2173,7 +2173,7 @@ static bool parse_variable_member(rbs_parser_t *parser, position comment_pos, rb return true; } case kSELF: { - range kind_range = { + rbs_range_t kind_range = { .start = parser->current_token.range.start, .end = parser->next_token.range.end }; @@ -2181,12 +2181,12 @@ static bool parse_variable_member(rbs_parser_t *parser, position comment_pos, rb ADVANCE_ASSERT(parser, pDOT); ADVANCE_ASSERT(parser, tAIDENT); - range name_range = parser->current_token.range; + rbs_range_t name_range = parser->current_token.range; rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, name_range); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); ADVANCE_ASSERT(parser, pCOLON); - range colon_range = parser->current_token.range; + rbs_range_t colon_range = parser->current_token.range; parser_push_typevar_table(parser, true); @@ -2256,14 +2256,14 @@ static bool parse_visibility_member(rbs_parser_t *parser, rbs_node_list_t *annot | `(` `)` # No variable */ NODISCARD -static bool parse_attribute_member(rbs_parser_t *parser, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **attribute_member) { - range member_range; +static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **attribute_member) { + rbs_range_t member_range; member_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); - range visibility_range; + rbs_range_t visibility_range; rbs_keyword_t *visibility; switch (parser->current_token.type) { @@ -2286,9 +2286,9 @@ static bool parse_attribute_member(rbs_parser_t *parser, position comment_pos, r } enum RBSTokenType attr_type = parser->current_token.type; - range keyword_range = parser->current_token.range; + rbs_range_t keyword_range = parser->current_token.range; - range kind_range; + rbs_range_t kind_range; InstanceSingletonKind is_kind = parse_instance_singleton_kind(parser, false, &kind_range); rbs_keyword_t *kind = rbs_keyword_new( @@ -2297,12 +2297,12 @@ static bool parse_attribute_member(rbs_parser_t *parser, position comment_pos, r INTERN(((is_kind == INSTANCE_KIND) ? "instance" : "singleton")) ); - range name_range; + rbs_range_t name_range; rbs_ast_symbol_t *attr_name; CHECK_PARSE(parse_method_name(parser, &name_range, &attr_name)); rbs_node_t *ivar_name; // rbs_ast_symbol_t, NULL or rbs_ast_bool_new(&parser->allocator, false) - range ivar_range, ivar_name_range; + rbs_range_t ivar_range, ivar_name_range; if (parser->next_token.type == pLPAREN) { ADVANCE_ASSERT(parser, pLPAREN); ivar_range.start = parser->current_token.range.start; @@ -2312,7 +2312,7 @@ static bool parse_attribute_member(rbs_parser_t *parser, position comment_pos, r ivar_name = (rbs_node_t *) rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); ivar_name_range = parser->current_token.range; } else { - range false_range = { + rbs_range_t false_range = { .start = parser->current_token.range.start, .end = parser->current_token.range.end }; @@ -2329,7 +2329,7 @@ static bool parse_attribute_member(rbs_parser_t *parser, position comment_pos, r } ADVANCE_ASSERT(parser, pCOLON); - range colon_range = parser->current_token.range; + rbs_range_t colon_range = parser->current_token.range; parser_push_typevar_table(parser, is_kind == SINGLETON_KIND); @@ -2380,7 +2380,7 @@ static bool parse_interface_members(rbs_parser_t *parser, rbs_node_list_t **memb while (parser->next_token.type != kEND) { rbs_node_list_t *annotations = rbs_node_list_new(&parser->allocator); - position annot_pos = NullPosition; + rbs_position_t annot_pos = NullPosition; CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos)); parser_advance(parser); @@ -2423,22 +2423,22 @@ static bool parse_interface_members(rbs_parser_t *parser, rbs_node_list_t **memb interface_decl ::= {`interface`} interface_name module_type_params interface_members */ NODISCARD -static bool parse_interface_decl(rbs_parser_t *parser, position comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_interface_t **interface_decl) { +static bool parse_interface_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_interface_t **interface_decl) { parser_push_typevar_table(parser, true); - range member_range; + rbs_range_t member_range; member_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); - range keyword_range = parser->current_token.range; + rbs_range_t keyword_range = parser->current_token.range; parser_advance(parser); - range name_range; + rbs_range_t name_range; rbs_typename_t *name = NULL; CHECK_PARSE(parse_type_name(parser, INTERFACE_NAME, &name_range, &name)); - range type_params_range; + rbs_range_t type_params_range; rbs_node_list_t *type_params; CHECK_PARSE(parse_type_params(parser, &type_params_range, true, &type_params)); @@ -2446,7 +2446,7 @@ static bool parse_interface_decl(rbs_parser_t *parser, position comment_pos, rbs CHECK_PARSE(parse_interface_members(parser, &members)); ADVANCE_ASSERT(parser, kEND); - range end_range = parser->current_token.range; + rbs_range_t end_range = parser->current_token.range; member_range.end = end_range.end; CHECK_PARSE(parser_pop_typevar_table(parser)); @@ -2475,16 +2475,16 @@ static bool parse_module_self_types(rbs_parser_t *parser, rbs_node_list_t *array while (true) { parser_advance(parser); - range self_range; + rbs_range_t self_range; self_range.start = parser->current_token.range.start; - range name_range; + rbs_range_t name_range; rbs_typename_t *module_name = NULL; CHECK_PARSE(parse_type_name(parser, CLASS_NAME | INTERFACE_NAME, &name_range, &module_name)); self_range.end = name_range.end; rbs_node_list_t *args = rbs_node_list_new(&parser->allocator); - range args_range = NULL_RANGE; + rbs_range_t args_range = NULL_RANGE; if (parser->next_token.type == pLBRACKET) { parser_advance(parser); args_range.start = parser->current_token.range.start; @@ -2512,7 +2512,7 @@ static bool parse_module_self_types(rbs_parser_t *parser, rbs_node_list_t *array } NODISCARD -static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, position annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl); +static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, rbs_position_t annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl); /* module_members ::= {} ... kEND @@ -2531,7 +2531,7 @@ static bool parse_module_members(rbs_parser_t *parser, rbs_node_list_t **members while (parser->next_token.type != kEND) { rbs_node_list_t *annotations = rbs_node_list_new(&parser->allocator); - position annot_pos; + rbs_position_t annot_pos; CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos)); parser_advance(parser); @@ -2614,19 +2614,19 @@ static bool parse_module_members(rbs_parser_t *parser, rbs_node_list_t **members | {module_name} module_name module_type_params `:` module_self_types module_members */ NODISCARD -static bool parse_module_decl0(rbs_parser_t *parser, range keyword_range, rbs_typename_t *module_name, range name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_module_t **module_decl) { +static bool parse_module_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_typename_t *module_name, rbs_range_t name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_module_t **module_decl) { parser_push_typevar_table(parser, true); - range decl_range; + rbs_range_t decl_range; decl_range.start = keyword_range.start; - range type_params_range; + rbs_range_t type_params_range; rbs_node_list_t *type_params; CHECK_PARSE(parse_type_params(parser, &type_params_range, true, &type_params)); rbs_node_list_t *self_types = rbs_node_list_new(&parser->allocator); - range colon_range; - range self_types_range; + rbs_range_t colon_range; + rbs_range_t self_types_range; if (parser->next_token.type == pCOLON) { parser_advance(parser); colon_range = parser->current_token.range; @@ -2642,7 +2642,7 @@ static bool parse_module_decl0(rbs_parser_t *parser, range keyword_range, rbs_ty CHECK_PARSE(parse_module_members(parser, &members)); ADVANCE_ASSERT(parser, kEND); - range end_range = parser->current_token.range; + rbs_range_t end_range = parser->current_token.range; decl_range.end = parser->current_token.range.end; rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); @@ -2666,28 +2666,28 @@ static bool parse_module_decl0(rbs_parser_t *parser, range keyword_range, rbs_ty */ NODISCARD -static bool parse_module_decl(rbs_parser_t *parser, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **module_decl) { - range keyword_range = parser->current_token.range; +static bool parse_module_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **module_decl) { + rbs_range_t keyword_range = parser->current_token.range; comment_pos = rbs_nonnull_pos_or(comment_pos, parser->current_token.range.start); rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); parser_advance(parser); - range module_name_range; + rbs_range_t module_name_range; rbs_typename_t *module_name; CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &module_name_range, &module_name)); if (parser->next_token.type == pEQ) { - range eq_range = parser->next_token.range; + rbs_range_t eq_range = parser->next_token.range; parser_advance(parser); parser_advance(parser); - range old_name_range; + rbs_range_t old_name_range; rbs_typename_t *old_name = NULL; CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &old_name_range, &old_name)); - range decl_range = { + rbs_range_t decl_range = { .start = keyword_range.start, .end = old_name_range.end }; @@ -2714,16 +2714,16 @@ static bool parse_module_decl(rbs_parser_t *parser, position comment_pos, rbs_no | {<>} */ NODISCARD -static bool parse_class_decl_super(rbs_parser_t *parser, range *lt_range, rbs_ast_declarations_class_super_t **super) { +static bool parse_class_decl_super(rbs_parser_t *parser, rbs_range_t *lt_range, rbs_ast_declarations_class_super_t **super) { if (parser_advance_if(parser, pLT)) { *lt_range = parser->current_token.range; - range super_range; + rbs_range_t super_range; super_range.start = parser->next_token.range.start; rbs_node_list_t *args = rbs_node_list_new(&parser->allocator); rbs_typename_t *name = NULL; - range name_range, args_range; + rbs_range_t name_range, args_range; CHECK_PARSE(class_instance_name(parser, CLASS_NAME, args, &name_range, &args_range, &name)); super_range.end = parser->current_token.range.end; @@ -2746,17 +2746,17 @@ static bool parse_class_decl_super(rbs_parser_t *parser, range *lt_range, rbs_as class_decl ::= {class_name} type_params class_decl_super class_members <`end`> */ NODISCARD -static bool parse_class_decl0(rbs_parser_t *parser, range keyword_range, rbs_typename_t *name, range name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_class_t **class_decl) { +static bool parse_class_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_typename_t *name, rbs_range_t name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_class_t **class_decl) { parser_push_typevar_table(parser, true); - range decl_range; + rbs_range_t decl_range; decl_range.start = keyword_range.start; - range type_params_range; + rbs_range_t type_params_range; rbs_node_list_t *type_params; CHECK_PARSE(parse_type_params(parser, &type_params_range, true, &type_params)); - range lt_range; + rbs_range_t lt_range; rbs_ast_declarations_class_super_t *super = NULL; CHECK_PARSE(parse_class_decl_super(parser, <_range, &super)); @@ -2765,7 +2765,7 @@ static bool parse_class_decl0(rbs_parser_t *parser, range keyword_range, rbs_typ ADVANCE_ASSERT(parser, kEND); - range end_range = parser->current_token.range; + rbs_range_t end_range = parser->current_token.range; decl_range.end = end_range.end; @@ -2788,27 +2788,27 @@ static bool parse_class_decl0(rbs_parser_t *parser, range keyword_range, rbs_typ | {`class`} class_name */ NODISCARD -static bool parse_class_decl(rbs_parser_t *parser, position comment_pos, rbs_node_list_t *annotations, rbs_node_t **class_decl) { - range keyword_range = parser->current_token.range; +static bool parse_class_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **class_decl) { + rbs_range_t keyword_range = parser->current_token.range; comment_pos = rbs_nonnull_pos_or(comment_pos, parser->current_token.range.start); rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); parser_advance(parser); - range class_name_range; + rbs_range_t class_name_range; rbs_typename_t *class_name = NULL; CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &class_name_range, &class_name)); if (parser->next_token.type == pEQ) { - range eq_range = parser->next_token.range; + rbs_range_t eq_range = parser->next_token.range; parser_advance(parser); parser_advance(parser); - range old_name_range; + rbs_range_t old_name_range; rbs_typename_t *old_name = NULL; CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &old_name_range, &old_name)); - range decl_range = { + rbs_range_t decl_range = { .start = keyword_range.start, .end = old_name_range.end, }; @@ -2838,7 +2838,7 @@ static bool parse_class_decl(rbs_parser_t *parser, position comment_pos, rbs_nod | {} */ NODISCARD -static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, position annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl) { +static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, rbs_position_t annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl) { parser_push_typevar_table(parser, true); switch (parser->current_token.type) { @@ -2892,7 +2892,7 @@ static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, posit NODISCARD static bool parse_decl(rbs_parser_t *parser, rbs_node_t **decl) { rbs_node_list_t *annotations = rbs_node_list_new(&parser->allocator); - position annot_pos = NullPosition; + rbs_position_t annot_pos = NullPosition; CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos)); parser_advance(parser); @@ -2946,11 +2946,11 @@ static bool parse_decl(rbs_parser_t *parser, rbs_node_t **decl) { | {} <> (empty -- returns empty namespace) */ NODISCARD -static bool parse_namespace(rbs_parser_t *parser, range *rg, rbs_namespace_t **namespace) { +static bool parse_namespace(rbs_parser_t *parser, rbs_range_t *rg, rbs_namespace_t **namespace) { bool is_absolute = false; if (parser->next_token.type == pCOLON2) { - *rg = (range) { + *rg = (rbs_range_t) { .start = parser->next_token.range.start, .end = parser->next_token.range.end, }; @@ -2991,7 +2991,7 @@ static bool parse_namespace(rbs_parser_t *parser, range *rg, rbs_namespace_t **n NODISCARD static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) { while (true) { - range namespace_range = NULL_RANGE; + rbs_range_t namespace_range = NULL_RANGE; rbs_namespace_t *namespace = NULL; CHECK_PARSE(parse_namespace(parser, &namespace_range, &namespace)); @@ -3004,18 +3004,18 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) { enum RBSTokenType ident_type = parser->current_token.type; - range type_name_range = rbs_null_range_p(namespace_range) + rbs_range_t type_name_range = rbs_null_range_p(namespace_range) ? parser->current_token.range - : (range) { .start = namespace_range.start, .end = parser->current_token.range.end }; + : (rbs_range_t) { .start = namespace_range.start, .end = parser->current_token.range.end }; rbs_location_t *symbolLoc = rbs_location_current_token(parser); rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); rbs_typename_t *type_name = rbs_typename_new(&parser->allocator, rbs_location_new(&parser->allocator, type_name_range),namespace, symbol); - range keyword_range = NULL_RANGE; - range new_name_range = NULL_RANGE; + rbs_range_t keyword_range = NULL_RANGE; + rbs_range_t new_name_range = NULL_RANGE; rbs_ast_symbol_t *new_name = NULL; - range clause_range = type_name_range; + rbs_range_t clause_range = type_name_range; if (parser->next_token.type == kAS) { parser_advance(parser); keyword_range = parser->current_token.range; @@ -3043,10 +3043,10 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) { } case pSTAR: { - range clause_range = namespace_range; + rbs_range_t clause_range = namespace_range; parser_advance(parser); - range star_range = parser->current_token.range; + rbs_range_t star_range = parser->current_token.range; clause_range.end = star_range.end; rbs_location_t *loc = rbs_location_new(&parser->allocator, clause_range); @@ -3082,12 +3082,12 @@ static bool parse_use_directive(rbs_parser_t *parser, rbs_ast_directives_use_t * if (parser->next_token.type == kUSE) { parser_advance(parser); - range keyword_range = parser->current_token.range; + rbs_range_t keyword_range = parser->current_token.range; rbs_node_list_t *clauses = rbs_node_list_new(&parser->allocator); CHECK_PARSE(parse_use_clauses(parser, clauses)); - range directive_range = keyword_range; + rbs_range_t directive_range = keyword_range; directive_range.end = parser->current_token.range.end; rbs_location_t *loc = rbs_location_new(&parser->allocator, directive_range); @@ -3108,7 +3108,7 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_ rbs_buffer_init(&parser->allocator, &rbs_buffer); for (size_t i = 0; i < com->line_count; i++) { - token tok = com->tokens[i]; + rbs_token_t tok = com->tokens[i]; const char *comment_start = parser->lexstate->string.start + tok.range.start.byte_pos + hash_bytes; size_t comment_bytes = RBS_RANGE_BYTES(tok.range) - hash_bytes; @@ -3130,7 +3130,7 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_ return rbs_ast_comment_new( &parser->allocator, - rbs_location_new(&parser->allocator, (range) { .start = com->start, .end = com->end }), + rbs_location_new(&parser->allocator, (rbs_range_t) { .start = com->start, .end = com->end }), rbs_buffer_to_string(&rbs_buffer) ); } @@ -3151,7 +3151,7 @@ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) { return comment_get_comment(com->next_comment, line); } -static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, token comment_token) { +static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, rbs_token_t comment_token) { if (com->line_count == 0) { com->start = comment_token.range.start; } @@ -3160,11 +3160,11 @@ static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *c com->line_size += 10; if (com->tokens) { - token *p = com->tokens; - com->tokens = rbs_allocator_calloc(allocator, com->line_size, token); - memcpy(com->tokens, p, sizeof(token) * com->line_count); + rbs_token_t *p = com->tokens; + com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t); + memcpy(com->tokens, p, sizeof(rbs_token_t) * com->line_count); } else { - com->tokens = rbs_allocator_calloc(allocator, com->line_size, token); + com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t); } } @@ -3172,7 +3172,7 @@ static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *c com->end = comment_token.range.end; } -static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, token comment_token, rbs_comment_t *last_comment) { +static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, rbs_token_t comment_token, rbs_comment_t *last_comment) { rbs_comment_t *new_comment = rbs_allocator_alloc(allocator, rbs_comment_t); *new_comment = (rbs_comment_t) { @@ -3194,7 +3194,7 @@ static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, token comment_to /** * Insert new comment line token. * */ -static void insert_comment_line(rbs_parser_t *parser, token tok) { +static void insert_comment_line(rbs_parser_t *parser, rbs_token_t tok) { int prev_line = tok.range.start.line - 1; rbs_comment_t *com = comment_get_comment(parser->last_comment, prev_line); @@ -3207,7 +3207,7 @@ static void insert_comment_line(rbs_parser_t *parser, token tok) { } bool parse_signature(rbs_parser_t *parser, rbs_signature_t **signature) { - range signature_range; + rbs_range_t signature_range; signature_range.start = parser->current_token.range.start; rbs_node_list_t *dirs = rbs_node_list_new(&parser->allocator); @@ -3296,10 +3296,10 @@ bool parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id) { } void print_parser(rbs_parser_t *parser) { - printf(" current_token = %s (%d...%d)\n", token_type_str(parser->current_token.type), parser->current_token.range.start.char_pos, parser->current_token.range.end.char_pos); - printf(" next_token = %s (%d...%d)\n", token_type_str(parser->next_token.type), parser->next_token.range.start.char_pos, parser->next_token.range.end.char_pos); - printf(" next_token2 = %s (%d...%d)\n", token_type_str(parser->next_token2.type), parser->next_token2.range.start.char_pos, parser->next_token2.range.end.char_pos); - printf(" next_token3 = %s (%d...%d)\n", token_type_str(parser->next_token3.type), parser->next_token3.range.start.char_pos, parser->next_token3.range.end.char_pos); + printf(" current_token = %s (%d...%d)\n", rbs_token_type_str(parser->current_token.type), parser->current_token.range.start.char_pos, parser->current_token.range.end.char_pos); + printf(" next_token = %s (%d...%d)\n", rbs_token_type_str(parser->next_token.type), parser->next_token.range.start.char_pos, parser->next_token.range.end.char_pos); + printf(" next_token2 = %s (%d...%d)\n", rbs_token_type_str(parser->next_token2.type), parser->next_token2.range.start.char_pos, parser->next_token2.range.end.char_pos); + printf(" next_token3 = %s (%d...%d)\n", rbs_token_type_str(parser->next_token3.type), parser->next_token3.range.start.char_pos, parser->next_token3.range.end.char_pos); } void parser_advance(rbs_parser_t *parser) { @@ -3326,10 +3326,10 @@ void parser_advance(rbs_parser_t *parser) { } } -void rbs_print_token(token tok) { +void rbs_print_token(rbs_token_t tok) { printf( "%s char=%d...%d\n", - token_type_str(tok.type), + rbs_token_type_str(tok.type), tok.range.start.char_pos, tok.range.end.char_pos ); @@ -3350,7 +3350,7 @@ rbs_ast_comment_t *get_comment(rbs_parser_t *parser, int subject_line) { lexstate *alloc_lexer(rbs_allocator_t *allocator, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { lexstate *lexer = rbs_allocator_alloc(allocator, lexstate); - position start_position = (position) { + rbs_position_t start_position = (rbs_position_t) { .byte_pos = 0, .char_pos = 0, .line = 1, @@ -3430,7 +3430,7 @@ void free_parser(rbs_parser_t *parser) { rbs_allocator_free(&parser->allocator); } -void set_error(rbs_parser_t *parser, token tok, bool syntax_error, const char *fmt, ...) { +void set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) { if (parser->error) { return; } diff --git a/src/rbs_location.c b/src/rbs_location.c index cc79591b7..06094b699 100644 --- a/src/rbs_location.c +++ b/src/rbs_location.c @@ -15,7 +15,7 @@ void rbs_loc_alloc_children(rbs_allocator_t *allocator, rbs_location_t *loc, siz loc->children->cap = capacity; } -void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, range r) { +void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, rbs_range_t r) { rbs_assert(loc->children != NULL, "All children should have been pre-allocated with rbs_loc_alloc_children()"); rbs_assert((loc->children->len + 1 <= loc->children->cap), "Not enough space was pre-allocated for the children. Children: %hu, Capacity: %hu", loc->children->len, loc->children->cap); @@ -24,13 +24,13 @@ void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, ran loc->children->entries[i].rg = (rbs_loc_range) { r.start.char_pos, r.end.char_pos }; } -void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, range r) { +void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, rbs_range_t r) { rbs_loc_add_optional_child(loc, name, r); unsigned short last_index = loc->children->len - 1; loc->children->required_p |= 1 << last_index; } -rbs_location_t *rbs_location_new(rbs_allocator_t *allocator, range rg) { +rbs_location_t *rbs_location_new(rbs_allocator_t *allocator, rbs_range_t rg) { rbs_location_t *location = rbs_allocator_alloc(allocator, rbs_location_t); *location = (rbs_location_t) { .rg = rg, From e2fb38bfe73dec2a9c58f20c198c2a0b89551f86 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Tue, 1 Apr 2025 05:05:43 +0800 Subject: [PATCH 099/111] Rename parser functions with `rbs_` prefix (#29) --- ext/rbs_extension/main.c | 28 ++-- include/rbs/parser.h | 28 ++-- src/parser.c | 328 +++++++++++++++++++-------------------- 3 files changed, 192 insertions(+), 192 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index e7e0838ef..edac1134b 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -52,19 +52,19 @@ static void declare_type_variables(rbs_parser_t *parser, VALUE variables, VALUE if (NIL_P(variables)) return; // Nothing to do. if (!RB_TYPE_P(variables, T_ARRAY)) { - free_parser(parser); + rbs_parser_free(parser); rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (must be an Array of Symbols or nil)", rb_obj_class(variables)); } - parser_push_typevar_table(parser, true); + rbs_parser_push_typevar_table(parser, true); for (long i = 0; i < rb_array_len(variables); i++) { VALUE symbol = rb_ary_entry(variables, i); if (!RB_TYPE_P(symbol, T_SYMBOL)) { - free_parser(parser); + rbs_parser_free(parser); rb_raise(rb_eTypeError, "Type variables Array contains invalid value %"PRIsVALUE" of type %"PRIsVALUE" (must be an Array of Symbols or nil)", rb_inspect(symbol), rb_obj_class(symbol)); @@ -78,7 +78,7 @@ static void declare_type_variables(rbs_parser_t *parser, VALUE variables, VALUE RSTRING_LEN(name_str) ); - if (!parser_insert_typevar(parser, id)) { + if (!rbs_parser_insert_typevar(parser, id)) { raise_error(parser->error, buffer); } } @@ -92,7 +92,7 @@ struct parse_type_arg { }; static VALUE ensure_free_parser(VALUE parser) { - free_parser((rbs_parser_t *)parser); + rbs_parser_free((rbs_parser_t *)parser); return Qnil; } @@ -105,14 +105,14 @@ static VALUE parse_type_try(VALUE a) { } rbs_node_t *type; - parse_type(parser, &type); + rbs_parse_type(parser, &type); raise_error_if_any(parser, arg->buffer); if (RB_TEST(arg->require_eof)) { - parser_advance(parser); + rbs_parser_advance(parser); if (parser->current_token.type != pEOF) { - set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF)); + rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF)); raise_error(parser->error, arg->buffer); } } @@ -136,7 +136,7 @@ static lexstate *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE strin const char *encoding_name = rb_enc_name(encoding); - return alloc_lexer( + return rbs_lexer_new( allocator, rbs_string_from_ruby_string(string), rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))), @@ -156,7 +156,7 @@ static rbs_parser_t *alloc_parser_from_buffer(VALUE buffer, int start_pos, int e rb_encoding *encoding = rb_enc_get(string); const char *encoding_name = rb_enc_name(encoding); - return alloc_parser( + return rbs_parser_new( rbs_string_from_ruby_string(string), rbs_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))), @@ -195,14 +195,14 @@ static VALUE parse_method_type_try(VALUE a) { } rbs_methodtype_t *method_type = NULL; - parse_method_type(parser, &method_type); + rbs_parse_method_type(parser, &method_type); raise_error_if_any(parser, arg->buffer); if (RB_TEST(arg->require_eof)) { - parser_advance(parser); + rbs_parser_advance(parser); if (parser->current_token.type != pEOF) { - set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF)); + rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF)); raise_error(parser->error, arg->buffer); } } @@ -244,7 +244,7 @@ static VALUE parse_signature_try(VALUE a) { rbs_parser_t *parser = arg->parser; rbs_signature_t *signature = NULL; - parse_signature(parser, &signature); + rbs_parse_signature(parser, &signature); raise_error_if_any(parser, arg->buffer); diff --git a/include/rbs/parser.h b/include/rbs/parser.h index 756b859aa..48a9a16f2 100644 --- a/include/rbs/parser.h +++ b/include/rbs/parser.h @@ -75,39 +75,39 @@ typedef struct { * end * ``` * */ -void parser_push_typevar_table(rbs_parser_t *parser, bool reset); +void rbs_parser_push_typevar_table(rbs_parser_t *parser, bool reset); /** * Insert new type variable into the latest table. * */ -NODISCARD bool parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id); +NODISCARD bool rbs_parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id); /** * Allocate new lexstate object. * * ``` * VALUE string = rb_funcall(buffer, rb_intern("content"), 0); - * alloc_lexer(string, 0, 31) // New lexstate with buffer content + * rbs_lexer_new(string, 0, 31) // New lexstate with buffer content * ``` * */ -lexstate *alloc_lexer(rbs_allocator_t *, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); +lexstate *rbs_lexer_new(rbs_allocator_t *, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); /** * Allocate new rbs_parser_t object. * * ``` - * alloc_parser(buffer, string, encoding, 0, 1); + * rbs_parser_new(buffer, string, encoding, 0, 1); * ``` * */ -rbs_parser_t *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); -void free_parser(rbs_parser_t *parser); +rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); +void rbs_parser_free(rbs_parser_t *parser); /** * Advance one token. * */ -void parser_advance(rbs_parser_t *parser); +void rbs_parser_advance(rbs_parser_t *parser); -void print_parser(rbs_parser_t *parser); +void rbs_parser_print(rbs_parser_t *parser); /** * Returns a RBS::Comment object associated with an subject at `subject_line`. @@ -122,12 +122,12 @@ void print_parser(rbs_parser_t *parser); * end * ``` * */ -rbs_ast_comment_t *get_comment(rbs_parser_t *parser, int subject_line); +rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line); -void set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5); +void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5); -bool parse_type(rbs_parser_t *parser, rbs_node_t **type); -bool parse_method_type(rbs_parser_t *parser, rbs_methodtype_t **method_type); -bool parse_signature(rbs_parser_t *parser, rbs_signature_t **signature); +bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type); +bool rbs_parse_method_type(rbs_parser_t *parser, rbs_methodtype_t **method_type); +bool rbs_parse_signature(rbs_parser_t *parser, rbs_signature_t **signature); #endif diff --git a/src/parser.c b/src/parser.c index a7df38541..0f2c904b6 100644 --- a/src/parser.c +++ b/src/parser.c @@ -70,12 +70,12 @@ #define ASSERT_TOKEN(parser, expected_type) \ if (parser->current_token.type != expected_type) { \ - set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(expected_type)); \ + rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(expected_type)); \ return false; \ } #define ADVANCE_ASSERT(parser, expected_type) do {\ - parser_advance(parser); \ + rbs_parser_advance(parser); \ ASSERT_TOKEN(parser, expected_type) \ } while(0); @@ -143,9 +143,9 @@ typedef enum { static void parser_advance_no_gap(rbs_parser_t *parser) { if (parser->current_token.range.end.byte_pos == parser->next_token.range.start.byte_pos) { - parser_advance(parser); + rbs_parser_advance(parser); } else { - set_error(parser, parser->next_token, true, "unexpected token"); + rbs_parser_set_error(parser, parser->next_token, true, "unexpected token"); } } @@ -180,8 +180,8 @@ static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, rbs_range_t rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, symbol_value); rbs_node_list_append(path, (rbs_node_t *)symbol); - parser_advance(parser); - parser_advance(parser); + rbs_parser_advance(parser); + rbs_parser_advance(parser); } rbs_range_t namespace_range = { @@ -231,7 +231,7 @@ static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, rbs_range_t rbs_assert(ids != NULL, "Unknown kind of type: %i", kind); - set_error(parser, parser->current_token, true, "expected one of %s", ids); + rbs_parser_set_error(parser, parser->current_token, true, "expected one of %s", ids); return false; } } @@ -244,11 +244,11 @@ NODISCARD static bool parse_type_list(rbs_parser_t *parser, enum RBSTokenType eol, rbs_node_list_t *types) { while (true) { rbs_node_t *type; - CHECK_PARSE(parse_type(parser, &type)); + CHECK_PARSE(rbs_parse_type(parser, &type)); rbs_node_list_append(types, type); if (parser->next_token.type == pCOMMA) { - parser_advance(parser); + rbs_parser_advance(parser); if (parser->next_token.type == eol) { break; @@ -257,7 +257,7 @@ static bool parse_type_list(rbs_parser_t *parser, enum RBSTokenType eol, rbs_nod if (parser->next_token.type == eol) { break; } else { - set_error(parser, parser->next_token, true, "comma delimited type list is expected"); + rbs_parser_set_error(parser, parser->next_token, true, "comma delimited type list is expected"); return false; } } @@ -291,7 +291,7 @@ static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_ rbs_range_t type_range; type_range.start = parser->next_token.range.start; rbs_node_t *type; - CHECK_PARSE(parse_type(parser, &type)); + CHECK_PARSE(rbs_parse_type(parser, &type)); type_range.end = parser->current_token.range.end; if (parser->next_token.type == pCOMMA || parser->next_token.type == pRPAREN) { @@ -306,7 +306,7 @@ static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_ } else { rbs_range_t name_range = parser->next_token.range; - parser_advance(parser); + rbs_parser_advance(parser); rbs_range_t param_range = { .start = type_range.start, @@ -314,7 +314,7 @@ static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_ }; if (!is_keyword_token(parser->current_token.type)) { - set_error(parser, parser->current_token, true, "unexpected token for function parameter name"); + rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for function parameter name"); return false; } @@ -347,7 +347,7 @@ static rbs_constant_id_t intern_token_start_end(rbs_parser_t *parser, rbs_token_ */ NODISCARD static bool parse_keyword_key(rbs_parser_t *parser, rbs_ast_symbol_t **key) { - parser_advance(parser); + rbs_parser_advance(parser); rbs_location_t *symbolLoc = rbs_location_current_token(parser); @@ -358,7 +358,7 @@ static bool parse_keyword_key(rbs_parser_t *parser, rbs_ast_symbol_t **key) { &parser->constant_pool, intern_token_start_end(parser, parser->current_token, parser->next_token) ); - parser_advance(parser); + rbs_parser_advance(parser); } else { *key = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); } @@ -375,7 +375,7 @@ static bool parse_keyword(rbs_parser_t *parser, rbs_hash_t *keywords, rbs_hash_t CHECK_PARSE(parse_keyword_key(parser, &key)); if (rbs_hash_find(memo, (rbs_node_t *) key)) { - set_error(parser, parser->current_token, true, "duplicated keyword argument"); + rbs_parser_set_error(parser, parser->current_token, true, "duplicated keyword argument"); return false; } else { rbs_location_t *loc = rbs_location_current_token(parser); @@ -421,7 +421,7 @@ static bool is_keyword(rbs_parser_t *parser) { **/ static bool parser_advance_if(rbs_parser_t *parser, enum RBSTokenType type) { if (parser->next_token.type == type) { - parser_advance(parser); + rbs_parser_advance(parser); return true; } else { return false; @@ -461,7 +461,7 @@ NODISCARD static bool parse_params(rbs_parser_t *parser, method_params *params) { if (parser->next_token.type == pQUESTION && parser->next_token2.type == pRPAREN) { params->required_positionals = NULL; - parser_advance(parser); + rbs_parser_advance(parser); return true; } if (parser->next_token.type == pRPAREN) { @@ -502,7 +502,7 @@ static bool parse_params(rbs_parser_t *parser, method_params *params) { while (true) { switch (parser->next_token.type) { case pQUESTION: - parser_advance(parser); + rbs_parser_advance(parser); if (is_keyword(parser)) { CHECK_PARSE(parse_keyword(parser, params->optional_keywords, memo)); @@ -526,7 +526,7 @@ static bool parse_params(rbs_parser_t *parser, method_params *params) { PARSE_REST_PARAM: if (parser->next_token.type == pSTAR) { - parser_advance(parser); + rbs_parser_advance(parser); rbs_types_function_param_t *param = NULL; CHECK_PARSE(parse_function_param(parser, ¶m)); params->rest_positionals = (rbs_node_t *) param; @@ -570,17 +570,17 @@ static bool parse_params(rbs_parser_t *parser, method_params *params) { while (true) { switch (parser->next_token.type) { case pQUESTION: - parser_advance(parser); + rbs_parser_advance(parser); if (is_keyword(parser)) { CHECK_PARSE(parse_keyword(parser, params->optional_keywords, memo)); } else { - set_error(parser, parser->next_token, true, "optional keyword argument type is expected"); + rbs_parser_set_error(parser, parser->next_token, true, "optional keyword argument type is expected"); return false; } break; case pSTAR2: - parser_advance(parser); + rbs_parser_advance(parser); rbs_types_function_param_t *param = NULL; CHECK_PARSE(parse_function_param(parser, ¶m)); params->rest_keywords = (rbs_node_t *) param; @@ -596,7 +596,7 @@ static bool parse_params(rbs_parser_t *parser, method_params *params) { if (is_keyword(parser)) { CHECK_PARSE(parse_keyword(parser, params->required_keywords, memo)); } else { - set_error(parser, parser->next_token, true, "required keyword argument type is expected"); + rbs_parser_set_error(parser, parser->next_token, true, "required keyword argument type is expected"); return false; } break; @@ -612,7 +612,7 @@ static bool parse_params(rbs_parser_t *parser, method_params *params) { EOP: if (parser->next_token.type != pRPAREN) { - set_error(parser, parser->next_token, true, "unexpected token for method type parameters"); + rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for method type parameters"); return false; } @@ -632,7 +632,7 @@ static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional) { CHECK_PARSE(parse_simple(parser, &type)); if (parser->next_token.type == pQUESTION) { - parser_advance(parser); + rbs_parser_advance(parser); rg.end = parser->current_token.range.end; rbs_location_t *location = rbs_location_new(&parser->allocator, rg); *optional = (rbs_node_t *) rbs_types_optional_new(&parser->allocator, location, type); @@ -662,11 +662,11 @@ static void initialize_method_params(method_params *params, rbs_allocator_t *all NODISCARD static bool parse_self_type_binding(rbs_parser_t *parser, rbs_node_t **self_type) { if (parser->next_token.type == pLBRACKET) { - parser_advance(parser); + rbs_parser_advance(parser); ADVANCE_ASSERT(parser, kSELF); ADVANCE_ASSERT(parser, pCOLON); rbs_node_t *type; - CHECK_PARSE(parse_type(parser, &type)); + CHECK_PARSE(rbs_parse_type(parser, &type)); ADVANCE_ASSERT(parser, pRBRACKET); *self_type = type; } @@ -699,7 +699,7 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse initialize_method_params(¶ms, &parser->allocator); if (parser->next_token.type == pLPAREN) { - parser_advance(parser); + rbs_parser_advance(parser); CHECK_PARSE(parse_params(parser, ¶ms)); ADVANCE_ASSERT(parser, pRPAREN); } @@ -710,7 +710,7 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse } else { if (rbs_is_untyped_params(¶ms)) { if (parser->next_token.type != pARROW) { - set_error(parser, parser->next_token2, true, "A method type with untyped method parameter cannot have block"); + rbs_parser_set_error(parser, parser->next_token2, true, "A method type with untyped method parameter cannot have block"); return false; } } @@ -720,16 +720,16 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse if (parser->next_token.type == pQUESTION && parser->next_token2.type == pLBRACE) { // Optional block required = false; - parser_advance(parser); + rbs_parser_advance(parser); } if (parser->next_token.type == pLBRACE) { - parser_advance(parser); + rbs_parser_advance(parser); method_params block_params; initialize_method_params(&block_params, &parser->allocator); if (parser->next_token.type == pLPAREN) { - parser_advance(parser); + rbs_parser_advance(parser); CHECK_PARSE(parse_params(parser, &block_params)); ADVANCE_ASSERT(parser, pRPAREN); } @@ -812,7 +812,7 @@ static bool parse_proc_type(rbs_parser_t *parser, rbs_types_proc_t **proc) { static void check_key_duplication(rbs_parser_t *parser, rbs_hash_t *fields, rbs_node_t *key) { if (rbs_hash_find(fields, ((rbs_node_t *) key))) { - set_error(parser, parser->current_token, true, "duplicated record key"); + rbs_parser_set_error(parser, parser->current_token, true, "duplicated record key"); } } @@ -839,7 +839,7 @@ static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields) { if (parser->next_token.type == pQUESTION) { // { ?foo: type } syntax required = false; - parser_advance(parser); + rbs_parser_advance(parser); } if (is_keyword(parser)) { @@ -866,7 +866,7 @@ static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields) { break; } default: - set_error(parser, parser->next_token, true, "unexpected record key token"); + rbs_parser_set_error(parser, parser->next_token, true, "unexpected record key token"); return false; } check_key_duplication(parser, *fields, (rbs_node_t *) key); @@ -877,7 +877,7 @@ static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields) { field_range.start = parser->current_token.range.end; rbs_node_t *type; - CHECK_PARSE(parse_type(parser, &type)); + CHECK_PARSE(rbs_parse_type(parser, &type)); field_range.end = parser->current_token.range.end; rbs_location_t *loc = rbs_location_new(&parser->allocator, field_range); @@ -933,7 +933,7 @@ static bool parse_symbol(rbs_parser_t *parser, rbs_location_t *location, rbs_typ break; } default: - set_error(parser, parser->current_token, false, "Unexpected error"); + rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error"); return false; } @@ -968,13 +968,13 @@ static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node } else if (parser->current_token.type == tLIDENT) { kind = ALIAS_NAME; } else { - set_error(parser, parser->current_token, false, "Unexpected error"); + rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error"); return false; } rbs_range_t args_range; if (parser->next_token.type == pLBRACKET) { - parser_advance(parser); + rbs_parser_advance(parser); args_range.start = parser->current_token.range.start; CHECK_PARSE(parse_type_list(parser, pRBRACKET, types)); ADVANCE_ASSERT(parser, pRBRACKET); @@ -1014,7 +1014,7 @@ static bool parse_singleton_type(rbs_parser_t *parser, rbs_types_classsingleton_ rbs_range_t type_range; type_range.start = parser->current_token.range.start; ADVANCE_ASSERT(parser, pLPAREN); - parser_advance(parser); + rbs_parser_advance(parser); rbs_range_t name_range; rbs_typename_t *typename = NULL; @@ -1064,12 +1064,12 @@ static bool parser_typevar_member(rbs_parser_t *parser, rbs_constant_id_t id) { */ NODISCARD static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) { - parser_advance(parser); + rbs_parser_advance(parser); switch (parser->current_token.type) { case pLPAREN: { rbs_node_t *lparen_type; - CHECK_PARSE(parse_type(parser, &lparen_type)); + CHECK_PARSE(rbs_parse_type(parser, &lparen_type)); ADVANCE_ASSERT(parser, pRPAREN); *type = lparen_type; return true; @@ -1228,7 +1228,7 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) { return true; } default: - set_error(parser, parser->current_token, true, "unexpected token for simple type"); + rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for simple type"); return false; } } @@ -1250,7 +1250,7 @@ static bool parse_intersection(rbs_parser_t *parser, rbs_node_t **type) { rbs_node_list_append(intersection_types, optional); while (parser->next_token.type == pAMP) { - parser_advance(parser); + rbs_parser_advance(parser); rbs_node_t *type = NULL; CHECK_PARSE(parse_optional(parser, &type)); rbs_node_list_append(intersection_types, type); @@ -1270,7 +1270,7 @@ static bool parse_intersection(rbs_parser_t *parser, rbs_node_t **type) { union ::= {} intersection '|' ... '|' | {} */ -bool parse_type(rbs_parser_t *parser, rbs_node_t **type) { +bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type) { rbs_range_t rg; rg.start = parser->next_token.range.start; rbs_node_list_t *union_types = rbs_node_list_new(&parser->allocator); @@ -1280,7 +1280,7 @@ bool parse_type(rbs_parser_t *parser, rbs_node_t **type) { rbs_node_list_append(union_types, *type); while (parser->next_token.type == pBAR) { - parser_advance(parser); + rbs_parser_advance(parser); rbs_node_t *intersection = NULL; CHECK_PARSE(parse_intersection(parser, &intersection)); rbs_node_list_append(union_types, intersection); @@ -1311,7 +1311,7 @@ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module bool required_param_allowed = true; if (parser->next_token.type == pLBRACKET) { - parser_advance(parser); + rbs_parser_advance(parser); rg->start = parser->current_token.range.start; @@ -1329,7 +1329,7 @@ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module if (module_type_params) { if (parser->next_token.type == kUNCHECKED) { unchecked = true; - parser_advance(parser); + rbs_parser_advance(parser); unchecked_range = parser->current_token.range; } @@ -1342,11 +1342,11 @@ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module variance = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("covariant")); break; default: - set_error(parser, parser->current_token, false, "Unexpected error"); + rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error"); return false; } - parser_advance(parser); + rbs_parser_advance(parser); variance_range = parser->current_token.range; } } @@ -1359,29 +1359,29 @@ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module rbs_constant_id_t id = rbs_constant_pool_insert_string(&parser->constant_pool, string); rbs_ast_symbol_t *name = rbs_ast_symbol_new(&parser->allocator, nameSymbolLoc, &parser->constant_pool, id); - CHECK_PARSE(parser_insert_typevar(parser, id)); + CHECK_PARSE(rbs_parser_insert_typevar(parser, id)); rbs_range_t upper_bound_range = NULL_RANGE; if (parser->next_token.type == pLT) { - parser_advance(parser); + rbs_parser_advance(parser); upper_bound_range.start = parser->current_token.range.start; - CHECK_PARSE(parse_type(parser, &upper_bound)); + CHECK_PARSE(rbs_parse_type(parser, &upper_bound)); upper_bound_range.end = parser->current_token.range.end; } rbs_range_t default_type_range = NULL_RANGE; if (module_type_params) { if (parser->next_token.type == pEQ) { - parser_advance(parser); + rbs_parser_advance(parser); default_type_range.start = parser->current_token.range.start; - CHECK_PARSE(parse_type(parser, &default_type)); + CHECK_PARSE(rbs_parse_type(parser, &default_type)); default_type_range.end = parser->current_token.range.end; required_param_allowed = false; } else { if (!required_param_allowed) { - set_error(parser, parser->current_token, true, "required type parameter is not allowed after optional type parameter"); + rbs_parser_set_error(parser, parser->current_token, true, "required type parameter is not allowed after optional type parameter"); return false; } } @@ -1402,7 +1402,7 @@ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module rbs_node_list_append(*params, (rbs_node_t *) param); if (parser->next_token.type == pCOMMA) { - parser_advance(parser); + rbs_parser_advance(parser); } if (parser->next_token.type == pRBRACKET) { @@ -1427,7 +1427,7 @@ static bool parser_pop_typevar_table(rbs_parser_t *parser) { table = parser->vars; parser->vars = table->next; } else { - set_error(parser, parser->current_token, false, "Cannot pop empty table"); + rbs_parser_set_error(parser, parser->current_token, false, "Cannot pop empty table"); return false; } @@ -1443,8 +1443,8 @@ static bool parser_pop_typevar_table(rbs_parser_t *parser) { method_type ::= {} type_params */ // TODO: Should this be NODISCARD? -bool parse_method_type(rbs_parser_t *parser, rbs_methodtype_t **method_type) { - parser_push_typevar_table(parser, false); +bool rbs_parse_method_type(rbs_parser_t *parser, rbs_methodtype_t **method_type) { + rbs_parser_push_typevar_table(parser, false); rbs_range_t rg; rg.start = parser->next_token.range.start; @@ -1481,7 +1481,7 @@ static bool parse_global_decl(rbs_parser_t *parser, rbs_node_list_t *annotations rbs_range_t decl_range; decl_range.start = parser->current_token.range.start; - rbs_ast_comment_t *comment = get_comment(parser, decl_range.start.line); + rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, decl_range.start.line); rbs_range_t name_range = parser->current_token.range; rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, name_range); @@ -1492,7 +1492,7 @@ static bool parse_global_decl(rbs_parser_t *parser, rbs_node_list_t *annotations rbs_range_t colon_range = parser->current_token.range; rbs_node_t *type; - CHECK_PARSE(parse_type(parser, &type)); + CHECK_PARSE(rbs_parse_type(parser, &type)); decl_range.end = parser->current_token.range.end; rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); @@ -1512,7 +1512,7 @@ static bool parse_const_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_range_t decl_range; decl_range.start = parser->current_token.range.start; - rbs_ast_comment_t *comment = get_comment(parser, decl_range.start.line); + rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, decl_range.start.line); rbs_range_t name_range; rbs_typename_t *typename = NULL; @@ -1522,7 +1522,7 @@ static bool parse_const_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_range_t colon_range = parser->current_token.range; rbs_node_t *type; - CHECK_PARSE(parse_type(parser, &type)); + CHECK_PARSE(rbs_parse_type(parser, &type)); decl_range.end = parser->current_token.range.end; @@ -1540,7 +1540,7 @@ static bool parse_const_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, */ NODISCARD static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_typealias_t **typealias) { - parser_push_typevar_table(parser, true); + rbs_parser_push_typevar_table(parser, true); rbs_range_t decl_range; decl_range.start = parser->current_token.range.start; @@ -1548,7 +1548,7 @@ static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rb rbs_range_t keyword_range = parser->current_token.range; - parser_advance(parser); + rbs_parser_advance(parser); rbs_range_t name_range; rbs_typename_t *typename = NULL; @@ -1562,7 +1562,7 @@ static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rb rbs_range_t eq_range = parser->current_token.range; rbs_node_t *type; - CHECK_PARSE(parse_type(parser, &type)); + CHECK_PARSE(rbs_parse_type(parser, &type)); decl_range.end = parser->current_token.range.end; @@ -1575,7 +1575,7 @@ static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rb CHECK_PARSE(parser_pop_typevar_table(parser)); - rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); + rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line); *typealias = rbs_ast_declarations_typealias_new(&parser->allocator, loc, typename, type_params, type, annotations, comment); return true; @@ -1617,7 +1617,7 @@ static bool parse_annotation(rbs_parser_t *parser, rbs_ast_annotation_t **annota close_char = '|'; break; default: - set_error(parser, parser->current_token, false, "Unexpected error"); + rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error"); return false; } @@ -1648,7 +1648,7 @@ static bool parse_annotations(rbs_parser_t *parser, rbs_node_list_t *annotations while (true) { if (parser->next_token.type == tANNOTATION) { - parser_advance(parser); + rbs_parser_advance(parser); if (rbs_null_position_p((*annot_pos))) { *annot_pos = parser->current_token.range.start; @@ -1671,7 +1671,7 @@ static bool parse_annotations(rbs_parser_t *parser, rbs_node_list_t *annotations */ NODISCARD static bool parse_method_name(rbs_parser_t *parser, rbs_range_t *range, rbs_ast_symbol_t **symbol) { - parser_advance(parser); + rbs_parser_advance(parser); switch (parser->current_token.type) { @@ -1683,7 +1683,7 @@ static bool parse_method_name(rbs_parser_t *parser, rbs_range_t *range, rbs_ast_ if (parser->next_token.type == pQUESTION && parser->current_token.range.end.byte_pos == parser->next_token.range.start.byte_pos) { range->start = parser->current_token.range.start; range->end = parser->next_token.range.end; - parser_advance(parser); + rbs_parser_advance(parser); rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared_with_encoding( &parser->constant_pool, @@ -1732,7 +1732,7 @@ static bool parse_method_name(rbs_parser_t *parser, rbs_range_t *range, rbs_ast_ } default: - set_error(parser, parser->current_token, true, "unexpected token for method name"); + rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for method name"); return false; } } @@ -1757,17 +1757,17 @@ static InstanceSingletonKind parse_instance_singleton_kind(rbs_parser_t *parser, rbs_range_t self_range = parser->next_token.range; if (parser->next_token2.type == pDOT) { - parser_advance(parser); - parser_advance(parser); + rbs_parser_advance(parser); + rbs_parser_advance(parser); kind = SINGLETON_KIND; } else if ( parser->next_token2.type == pQUESTION && parser->next_token.range.end.char_pos == parser->next_token2.range.start.char_pos && parser->next_token3.type == pDOT && allow_selfq) { - parser_advance(parser); - parser_advance(parser); - parser_advance(parser); + rbs_parser_advance(parser); + rbs_parser_advance(parser); + rbs_parser_advance(parser); kind = INSTANCE_SINGLETON_KIND; } @@ -1800,7 +1800,7 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce member_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); - rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); + rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line); rbs_range_t visibility_range; rbs_keyword_t *visibility; @@ -1810,14 +1810,14 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce visibility_range = parser->current_token.range; visibility = rbs_keyword_new(&parser->allocator, rbs_location_new(&parser->allocator, visibility_range), INTERN("private")); member_range.start = visibility_range.start; - parser_advance(parser); + rbs_parser_advance(parser); break; } case kPUBLIC: { visibility_range = parser->current_token.range; visibility = rbs_keyword_new(&parser->allocator, rbs_location_new(&parser->allocator, visibility_range), INTERN("public")); member_range.start = visibility_range.start; - parser_advance(parser); + rbs_parser_advance(parser); break; } default: @@ -1844,13 +1844,13 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce #define SELF_ID rbs_constant_pool_insert_constant(&parser->constant_pool, (const unsigned char *) "self?", strlen("self?")) if (parser->next_token.type == pDOT && name->constant_id == SELF_ID) { - set_error(parser, parser->next_token, true, "`self?` method cannot have visibility"); + rbs_parser_set_error(parser, parser->next_token, true, "`self?` method cannot have visibility"); return false; } else { ADVANCE_ASSERT(parser, pCOLON); } - parser_push_typevar_table(parser, kind != INSTANCE_KIND); + rbs_parser_push_typevar_table(parser, kind != INSTANCE_KIND); rbs_node_list_t *overloads = rbs_node_list_new(&parser->allocator); bool overloading = false; @@ -1875,7 +1875,7 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce case pQUESTION: { rbs_methodtype_t *method_type = NULL; - CHECK_PARSE(parse_method_type(parser, &method_type)); + CHECK_PARSE(rbs_parse_method_type(parser, &method_type)); overload_range.end = parser->current_token.range.end; rbs_location_t *loc = rbs_location_new(&parser->allocator, overload_range); @@ -1888,23 +1888,23 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce case pDOT3: if (accept_overload) { overloading = true; - parser_advance(parser); + rbs_parser_advance(parser); loop = false; overloading_range = parser->current_token.range; member_range.end = overloading_range.end; break; } else { - set_error(parser, parser->next_token, true, "unexpected overloading method definition"); + rbs_parser_set_error(parser, parser->next_token, true, "unexpected overloading method definition"); return false; } default: - set_error(parser, parser->next_token, true, "unexpected token for method type"); + rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for method type"); return false; } if (parser->next_token.type == pBAR) { - parser_advance(parser); + rbs_parser_advance(parser); } else { loop = false; } @@ -1927,7 +1927,7 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce break; } default: - set_error(parser, parser->current_token, false, "Unexpected error"); + rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error"); return false; } @@ -1951,14 +1951,14 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce * */ NODISCARD static bool class_instance_name(rbs_parser_t *parser, TypeNameKind kind, rbs_node_list_t *args, rbs_range_t *name_range, rbs_range_t *args_range, rbs_typename_t **name) { - parser_advance(parser); + rbs_parser_advance(parser); rbs_typename_t *typename = NULL; CHECK_PARSE(parse_type_name(parser, kind, name_range, &typename)); *name = typename; if (parser->next_token.type == pLBRACKET) { - parser_advance(parser); + rbs_parser_advance(parser); args_range->start = parser->current_token.range.start; CHECK_PARSE(parse_type_list(parser, pRBRACKET, args)); ADVANCE_ASSERT(parser, pRBRACKET); @@ -1999,18 +1999,18 @@ static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, rbs_po reset_typevar_scope = false; break; default: - set_error(parser, parser->current_token, false, "Unexpected error"); + rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error"); return false; } if (from_interface) { if (parser->current_token.type != kINCLUDE) { - set_error(parser, parser->current_token, true, "unexpected mixin in interface declaration"); + rbs_parser_set_error(parser, parser->current_token, true, "unexpected mixin in interface declaration"); return false; } } - parser_push_typevar_table(parser, reset_typevar_scope); + rbs_parser_push_typevar_table(parser, reset_typevar_scope); rbs_node_list_t *args = rbs_node_list_new(&parser->allocator); rbs_range_t name_range; @@ -2032,7 +2032,7 @@ static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, rbs_po rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); - rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); + rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line); switch (type) { case kINCLUDE: @@ -2045,7 +2045,7 @@ static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, rbs_po *mixin_member = (rbs_node_t *) rbs_ast_members_prepend_new(&parser->allocator, loc, name, args, annotations, comment); return true; default: - set_error(parser, parser->current_token, false, "Unexpected error"); + rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error"); return false; } } @@ -2065,7 +2065,7 @@ static bool parse_alias_member(rbs_parser_t *parser, bool instance_only, rbs_pos rbs_range_t keyword_range = parser->current_token.range; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); - rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); + rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line); rbs_keyword_t *kind; rbs_ast_symbol_t *new_name, *old_name; @@ -2114,14 +2114,14 @@ static bool parse_alias_member(rbs_parser_t *parser, bool instance_only, rbs_pos NODISCARD static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_node_t **variable_member) { if (annotations->length > 0) { - set_error(parser, parser->current_token, true, "annotation cannot be given to variable members"); + rbs_parser_set_error(parser, parser->current_token, true, "annotation cannot be given to variable members"); return false; } rbs_range_t member_range; member_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); - rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); + rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line); switch (parser->current_token.type) { @@ -2134,7 +2134,7 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p rbs_range_t colon_range = parser->current_token.range; rbs_node_t *type; - CHECK_PARSE(parse_type(parser, &type)); + CHECK_PARSE(rbs_parse_type(parser, &type)); member_range.end = parser->current_token.range.end; rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); @@ -2154,10 +2154,10 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p ADVANCE_ASSERT(parser, pCOLON); rbs_range_t colon_range = parser->current_token.range; - parser_push_typevar_table(parser, true); + rbs_parser_push_typevar_table(parser, true); rbs_node_t *type; - CHECK_PARSE(parse_type(parser, &type)); + CHECK_PARSE(rbs_parse_type(parser, &type)); CHECK_PARSE(parser_pop_typevar_table(parser)); @@ -2188,10 +2188,10 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p ADVANCE_ASSERT(parser, pCOLON); rbs_range_t colon_range = parser->current_token.range; - parser_push_typevar_table(parser, true); + rbs_parser_push_typevar_table(parser, true); rbs_node_t *type; - CHECK_PARSE(parse_type(parser, &type)); + CHECK_PARSE(rbs_parse_type(parser, &type)); CHECK_PARSE(parser_pop_typevar_table(parser)); @@ -2207,7 +2207,7 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p return true; } default: - set_error(parser, parser->current_token, false, "Unexpected error"); + rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error"); return false; } } @@ -2219,7 +2219,7 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p NODISCARD static bool parse_visibility_member(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_node_t **visibility_member) { if (annotations->length > 0) { - set_error(parser, parser->current_token, true, "annotation cannot be given to visibility members"); + rbs_parser_set_error(parser, parser->current_token, true, "annotation cannot be given to visibility members"); return false; } @@ -2236,7 +2236,7 @@ static bool parse_visibility_member(rbs_parser_t *parser, rbs_node_list_t *annot return true; } default: - set_error(parser, parser->current_token, false, "Unexpected error"); + rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error"); return false; } } @@ -2261,7 +2261,7 @@ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_ member_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); - rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); + rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line); rbs_range_t visibility_range; rbs_keyword_t *visibility; @@ -2270,13 +2270,13 @@ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_ case kPRIVATE: { visibility_range = parser->current_token.range; visibility = rbs_keyword_new(&parser->allocator, rbs_location_new(&parser->allocator, visibility_range), INTERN("private")); - parser_advance(parser); + rbs_parser_advance(parser); break; } case kPUBLIC: { visibility_range = parser->current_token.range; visibility = rbs_keyword_new(&parser->allocator, rbs_location_new(&parser->allocator, visibility_range), INTERN("public")); - parser_advance(parser); + rbs_parser_advance(parser); break; } default: @@ -2331,10 +2331,10 @@ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_ ADVANCE_ASSERT(parser, pCOLON); rbs_range_t colon_range = parser->current_token.range; - parser_push_typevar_table(parser, is_kind == SINGLETON_KIND); + rbs_parser_push_typevar_table(parser, is_kind == SINGLETON_KIND); rbs_node_t *type; - CHECK_PARSE(parse_type(parser, &type)); + CHECK_PARSE(rbs_parse_type(parser, &type)); CHECK_PARSE(parser_pop_typevar_table(parser)); @@ -2362,7 +2362,7 @@ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_ *attribute_member = (rbs_node_t *) rbs_ast_members_attraccessor_new(&parser->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); return true; default: - set_error(parser, parser->current_token, false, "Unexpected error"); + rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error"); return false; } } @@ -2383,7 +2383,7 @@ static bool parse_interface_members(rbs_parser_t *parser, rbs_node_list_t **memb rbs_position_t annot_pos = NullPosition; CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos)); - parser_advance(parser); + rbs_parser_advance(parser); rbs_node_t *member; switch (parser->current_token.type) { @@ -2409,7 +2409,7 @@ static bool parse_interface_members(rbs_parser_t *parser, rbs_node_list_t **memb } default: - set_error(parser, parser->current_token, true, "unexpected token for interface declaration member"); + rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for interface declaration member"); return false; } @@ -2424,7 +2424,7 @@ static bool parse_interface_members(rbs_parser_t *parser, rbs_node_list_t **memb */ NODISCARD static bool parse_interface_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_interface_t **interface_decl) { - parser_push_typevar_table(parser, true); + rbs_parser_push_typevar_table(parser, true); rbs_range_t member_range; member_range.start = parser->current_token.range.start; @@ -2432,7 +2432,7 @@ static bool parse_interface_decl(rbs_parser_t *parser, rbs_position_t comment_po rbs_range_t keyword_range = parser->current_token.range; - parser_advance(parser); + rbs_parser_advance(parser); rbs_range_t name_range; rbs_typename_t *name = NULL; @@ -2458,7 +2458,7 @@ static bool parse_interface_decl(rbs_parser_t *parser, rbs_position_t comment_po rbs_loc_add_required_child(loc, INTERN("end"), end_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range); - rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); + rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line); *interface_decl = rbs_ast_declarations_interface_new(&parser->allocator, loc, name, type_params, members, annotations, comment); return true; @@ -2473,7 +2473,7 @@ static bool parse_interface_decl(rbs_parser_t *parser, rbs_position_t comment_po NODISCARD static bool parse_module_self_types(rbs_parser_t *parser, rbs_node_list_t *array) { while (true) { - parser_advance(parser); + rbs_parser_advance(parser); rbs_range_t self_range; self_range.start = parser->current_token.range.start; @@ -2486,10 +2486,10 @@ static bool parse_module_self_types(rbs_parser_t *parser, rbs_node_list_t *array rbs_node_list_t *args = rbs_node_list_new(&parser->allocator); rbs_range_t args_range = NULL_RANGE; if (parser->next_token.type == pLBRACKET) { - parser_advance(parser); + rbs_parser_advance(parser); args_range.start = parser->current_token.range.start; CHECK_PARSE(parse_type_list(parser, pRBRACKET, args)); - parser_advance(parser); + rbs_parser_advance(parser); self_range.end = args_range.end = parser->current_token.range.end; } @@ -2502,7 +2502,7 @@ static bool parse_module_self_types(rbs_parser_t *parser, rbs_node_list_t *array rbs_node_list_append(array, (rbs_node_t *)self_type); if (parser->next_token.type == pCOMMA) { - parser_advance(parser); + rbs_parser_advance(parser); } else { break; } @@ -2534,7 +2534,7 @@ static bool parse_module_members(rbs_parser_t *parser, rbs_node_list_t **members rbs_position_t annot_pos; CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos)); - parser_advance(parser); + rbs_parser_advance(parser); rbs_node_t *member; switch (parser->current_token.type) @@ -2590,7 +2590,7 @@ static bool parse_module_members(rbs_parser_t *parser, rbs_node_list_t **members break; } default: - set_error(parser, parser->next_token, true, "method or attribute definition is expected after visibility modifier"); + rbs_parser_set_error(parser, parser->next_token, true, "method or attribute definition is expected after visibility modifier"); return false; } } else { @@ -2615,7 +2615,7 @@ static bool parse_module_members(rbs_parser_t *parser, rbs_node_list_t **members */ NODISCARD static bool parse_module_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_typename_t *module_name, rbs_range_t name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_module_t **module_decl) { - parser_push_typevar_table(parser, true); + rbs_parser_push_typevar_table(parser, true); rbs_range_t decl_range; decl_range.start = keyword_range.start; @@ -2628,7 +2628,7 @@ static bool parse_module_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_range_t colon_range; rbs_range_t self_types_range; if (parser->next_token.type == pCOLON) { - parser_advance(parser); + rbs_parser_advance(parser); colon_range = parser->current_token.range; self_types_range.start = parser->next_token.range.start; CHECK_PARSE(parse_module_self_types(parser, self_types)); @@ -2670,9 +2670,9 @@ static bool parse_module_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_range_t keyword_range = parser->current_token.range; comment_pos = rbs_nonnull_pos_or(comment_pos, parser->current_token.range.start); - rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); + rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line); - parser_advance(parser); + rbs_parser_advance(parser); rbs_range_t module_name_range; rbs_typename_t *module_name; @@ -2680,8 +2680,8 @@ static bool parse_module_decl(rbs_parser_t *parser, rbs_position_t comment_pos, if (parser->next_token.type == pEQ) { rbs_range_t eq_range = parser->next_token.range; - parser_advance(parser); - parser_advance(parser); + rbs_parser_advance(parser); + rbs_parser_advance(parser); rbs_range_t old_name_range; rbs_typename_t *old_name = NULL; @@ -2747,7 +2747,7 @@ static bool parse_class_decl_super(rbs_parser_t *parser, rbs_range_t *lt_range, */ NODISCARD static bool parse_class_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_typename_t *name, rbs_range_t name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_class_t **class_decl) { - parser_push_typevar_table(parser, true); + rbs_parser_push_typevar_table(parser, true); rbs_range_t decl_range; decl_range.start = keyword_range.start; @@ -2792,17 +2792,17 @@ static bool parse_class_decl(rbs_parser_t *parser, rbs_position_t comment_pos, r rbs_range_t keyword_range = parser->current_token.range; comment_pos = rbs_nonnull_pos_or(comment_pos, parser->current_token.range.start); - rbs_ast_comment_t *comment = get_comment(parser, comment_pos.line); + rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line); - parser_advance(parser); + rbs_parser_advance(parser); rbs_range_t class_name_range; rbs_typename_t *class_name = NULL; CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &class_name_range, &class_name)); if (parser->next_token.type == pEQ) { rbs_range_t eq_range = parser->next_token.range; - parser_advance(parser); - parser_advance(parser); + rbs_parser_advance(parser); + rbs_parser_advance(parser); rbs_range_t old_name_range; rbs_typename_t *old_name = NULL; @@ -2839,7 +2839,7 @@ static bool parse_class_decl(rbs_parser_t *parser, rbs_position_t comment_pos, r */ NODISCARD static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, rbs_position_t annot_pos, rbs_node_list_t *annotations, rbs_node_t **decl) { - parser_push_typevar_table(parser, true); + rbs_parser_push_typevar_table(parser, true); switch (parser->current_token.type) { case tUIDENT: @@ -2880,7 +2880,7 @@ static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, rbs_p break; } default: - set_error(parser, parser->current_token, true, "unexpected token for class/module declaration member"); + rbs_parser_set_error(parser, parser->current_token, true, "unexpected token for class/module declaration member"); return false; } @@ -2895,7 +2895,7 @@ static bool parse_decl(rbs_parser_t *parser, rbs_node_t **decl) { rbs_position_t annot_pos = NullPosition; CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos)); - parser_advance(parser); + rbs_parser_advance(parser); switch (parser->current_token.type) { case tUIDENT: @@ -2936,7 +2936,7 @@ static bool parse_decl(rbs_parser_t *parser, rbs_node_t **decl) { return true; } default: - set_error(parser, parser->current_token, true, "cannot start a declaration"); + rbs_parser_set_error(parser, parser->current_token, true, "cannot start a declaration"); return false; } } @@ -2956,7 +2956,7 @@ static bool parse_namespace(rbs_parser_t *parser, rbs_range_t *rg, rbs_namespace }; is_absolute = true; - parser_advance(parser); + rbs_parser_advance(parser); } rbs_node_list_t *path = rbs_node_list_new(&parser->allocator); @@ -2970,8 +2970,8 @@ static bool parse_namespace(rbs_parser_t *parser, rbs_range_t *rg, rbs_namespace rg->start = parser->next_token.range.start; } rg->end = parser->next_token2.range.end; - parser_advance(parser); - parser_advance(parser); + rbs_parser_advance(parser); + rbs_parser_advance(parser); } else { break; } @@ -3000,7 +3000,7 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) { case tLIDENT: case tULIDENT: case tUIDENT: { - parser_advance(parser); + rbs_parser_advance(parser); enum RBSTokenType ident_type = parser->current_token.type; @@ -3017,7 +3017,7 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) { rbs_ast_symbol_t *new_name = NULL; rbs_range_t clause_range = type_name_range; if (parser->next_token.type == kAS) { - parser_advance(parser); + rbs_parser_advance(parser); keyword_range = parser->current_token.range; if (ident_type == tUIDENT) ADVANCE_ASSERT(parser, tUIDENT); @@ -3044,7 +3044,7 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) { case pSTAR: { rbs_range_t clause_range = namespace_range; - parser_advance(parser); + rbs_parser_advance(parser); rbs_range_t star_range = parser->current_token.range; clause_range.end = star_range.end; @@ -3060,12 +3060,12 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) { break; } default: - set_error(parser, parser->next_token, true, "use clause is expected"); + rbs_parser_set_error(parser, parser->next_token, true, "use clause is expected"); return false; } if (parser->next_token.type == pCOMMA) { - parser_advance(parser); + rbs_parser_advance(parser); } else { break; } @@ -3080,7 +3080,7 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) { NODISCARD static bool parse_use_directive(rbs_parser_t *parser, rbs_ast_directives_use_t **use_directive) { if (parser->next_token.type == kUSE) { - parser_advance(parser); + rbs_parser_advance(parser); rbs_range_t keyword_range = parser->current_token.range; @@ -3206,7 +3206,7 @@ static void insert_comment_line(rbs_parser_t *parser, rbs_token_t tok) { } } -bool parse_signature(rbs_parser_t *parser, rbs_signature_t **signature) { +bool rbs_parse_signature(rbs_parser_t *parser, rbs_signature_t **signature) { rbs_range_t signature_range; signature_range.start = parser->current_token.range.start; @@ -3261,7 +3261,7 @@ id_table *alloc_reset_table(rbs_allocator_t *allocator) { return table; } -void parser_push_typevar_table(rbs_parser_t *parser, bool reset) { +void rbs_parser_push_typevar_table(rbs_parser_t *parser, bool reset) { if (reset) { id_table *table = alloc_reset_table(&parser->allocator); table->next = parser->vars; @@ -3274,11 +3274,11 @@ void parser_push_typevar_table(rbs_parser_t *parser, bool reset) { } NODISCARD -bool parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id) { +bool rbs_parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id) { id_table *table = parser->vars; if (RESET_TABLE_P(table)) { - set_error(parser, parser->current_token, false, "Cannot insert to reset table"); + rbs_parser_set_error(parser, parser->current_token, false, "Cannot insert to reset table"); return false; } @@ -3295,14 +3295,14 @@ bool parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id) { return true; } -void print_parser(rbs_parser_t *parser) { +void rbs_parser_print(rbs_parser_t *parser) { printf(" current_token = %s (%d...%d)\n", rbs_token_type_str(parser->current_token.type), parser->current_token.range.start.char_pos, parser->current_token.range.end.char_pos); printf(" next_token = %s (%d...%d)\n", rbs_token_type_str(parser->next_token.type), parser->next_token.range.start.char_pos, parser->next_token.range.end.char_pos); printf(" next_token2 = %s (%d...%d)\n", rbs_token_type_str(parser->next_token2.type), parser->next_token2.range.start.char_pos, parser->next_token2.range.end.char_pos); printf(" next_token3 = %s (%d...%d)\n", rbs_token_type_str(parser->next_token3.type), parser->next_token3.range.start.char_pos, parser->next_token3.range.end.char_pos); } -void parser_advance(rbs_parser_t *parser) { +void rbs_parser_advance(rbs_parser_t *parser) { parser->current_token = parser->next_token; parser->next_token = parser->next_token2; parser->next_token2 = parser->next_token3; @@ -3335,7 +3335,7 @@ void rbs_print_token(rbs_token_t tok) { ); } -rbs_ast_comment_t *get_comment(rbs_parser_t *parser, int subject_line) { +rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line) { int comment_line = subject_line - 1; rbs_comment_t *com = comment_get_comment(parser->last_comment, comment_line); @@ -3347,7 +3347,7 @@ rbs_ast_comment_t *get_comment(rbs_parser_t *parser, int subject_line) { } } -lexstate *alloc_lexer(rbs_allocator_t *allocator, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { +lexstate *rbs_lexer_new(rbs_allocator_t *allocator, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { lexstate *lexer = rbs_allocator_alloc(allocator, lexstate); rbs_position_t start_position = (rbs_position_t) { @@ -3375,11 +3375,11 @@ lexstate *alloc_lexer(rbs_allocator_t *allocator, rbs_string_t string, const rbs return lexer; } -rbs_parser_t *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { +rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { rbs_allocator_t allocator; rbs_allocator_init(&allocator); - lexstate *lexer = alloc_lexer(&allocator, string, encoding, start_pos, end_pos); + lexstate *lexer = rbs_lexer_new(&allocator, string, encoding, start_pos, end_pos); rbs_parser_t *parser = rbs_allocator_alloc(&allocator, rbs_parser_t); *parser = (rbs_parser_t) { @@ -3418,19 +3418,19 @@ rbs_parser_t *alloc_parser(rbs_string_t string, const rbs_encoding_t *encoding, const size_t initial_pool_capacity = 2; rbs_constant_pool_init(&parser->constant_pool, initial_pool_capacity); - parser_advance(parser); - parser_advance(parser); - parser_advance(parser); + rbs_parser_advance(parser); + rbs_parser_advance(parser); + rbs_parser_advance(parser); return parser; } -void free_parser(rbs_parser_t *parser) { +void rbs_parser_free(rbs_parser_t *parser) { rbs_constant_pool_free(&parser->constant_pool); rbs_allocator_free(&parser->allocator); } -void set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) { +void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) { if (parser->error) { return; } From cc33f9b0f248ac1e20f0a77dac78a7f8259d4ab5 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Tue, 1 Apr 2025 18:05:09 +0800 Subject: [PATCH 100/111] Correctly convert camel case to snake case when generating names in C files (#32) Currently, camel case names in `config.yml`, like `MethodDefinition`, will be generated as `methoddefinition`, which is not ideal. So this PR updates `template.rb` to make sure they're generated as `method_definition` instead. I also changed `rbs_typename` to `rbs_type_name` as I think it's also not ideal. NO Ruby files are touched so it shouldn't be breaking for end users. --- config.yml | 38 +-- ext/rbs_extension/ast_translation.c | 118 +++---- ext/rbs_extension/main.c | 2 +- include/rbs/ast.h | 220 ++++++------- include/rbs/parser.h | 2 +- src/ast.c | 302 +++++++++--------- src/parser.c | 122 +++---- .../ext/rbs_extension/ast_translation.c.erb | 2 +- templates/template.rb | 22 +- 9 files changed, 419 insertions(+), 409 deletions(-) diff --git a/config.yml b/config.yml index a67771010..682ae299e 100644 --- a/config.yml +++ b/config.yml @@ -16,7 +16,7 @@ nodes: - name: RBS::AST::Declarations::Class fields: - name: name - c_type: rbs_typename + c_type: rbs_type_name - name: type_params c_type: rbs_node_list - name: super_class @@ -30,15 +30,15 @@ nodes: - name: RBS::AST::Declarations::Class::Super fields: - name: name - c_type: rbs_typename + c_type: rbs_type_name - name: args c_type: rbs_node_list - name: RBS::AST::Declarations::ClassAlias fields: - name: new_name - c_type: rbs_typename + c_type: rbs_type_name - name: old_name - c_type: rbs_typename + c_type: rbs_type_name - name: comment c_type: rbs_ast_comment - name: annotations @@ -46,7 +46,7 @@ nodes: - name: RBS::AST::Declarations::Constant fields: - name: name - c_type: rbs_typename + c_type: rbs_type_name - name: type c_type: rbs_node - name: comment @@ -66,7 +66,7 @@ nodes: - name: RBS::AST::Declarations::Interface fields: - name: name - c_type: rbs_typename + c_type: rbs_type_name - name: type_params c_type: rbs_node_list - name: members @@ -78,7 +78,7 @@ nodes: - name: RBS::AST::Declarations::Module fields: - name: name - c_type: rbs_typename + c_type: rbs_type_name - name: type_params c_type: rbs_node_list - name: self_types @@ -92,15 +92,15 @@ nodes: - name: RBS::AST::Declarations::Module::Self fields: - name: name - c_type: rbs_typename + c_type: rbs_type_name - name: args c_type: rbs_node_list - name: RBS::AST::Declarations::ModuleAlias fields: - name: new_name - c_type: rbs_typename + c_type: rbs_type_name - name: old_name - c_type: rbs_typename + c_type: rbs_type_name - name: comment c_type: rbs_ast_comment - name: annotations @@ -108,7 +108,7 @@ nodes: - name: RBS::AST::Declarations::TypeAlias fields: - name: name - c_type: rbs_typename + c_type: rbs_type_name - name: type_params c_type: rbs_node_list - name: type @@ -124,7 +124,7 @@ nodes: - name: RBS::AST::Directives::Use::SingleClause fields: - name: type_name - c_type: rbs_typename + c_type: rbs_type_name - name: new_name c_type: rbs_ast_symbol - name: RBS::AST::Directives::Use::WildcardClause @@ -211,7 +211,7 @@ nodes: - name: RBS::AST::Members::Extend fields: - name: name - c_type: rbs_typename + c_type: rbs_type_name - name: args c_type: rbs_node_list - name: annotations @@ -221,7 +221,7 @@ nodes: - name: RBS::AST::Members::Include fields: - name: name - c_type: rbs_typename + c_type: rbs_type_name - name: args c_type: rbs_node_list - name: annotations @@ -262,7 +262,7 @@ nodes: - name: RBS::AST::Members::Prepend fields: - name: name - c_type: rbs_typename + c_type: rbs_type_name - name: args c_type: rbs_node_list - name: annotations @@ -329,7 +329,7 @@ nodes: - name: RBS::Types::Alias fields: - name: name - c_type: rbs_typename + c_type: rbs_type_name - name: args c_type: rbs_node_list - name: RBS::Types::Bases::Any @@ -356,13 +356,13 @@ nodes: - name: RBS::Types::ClassInstance fields: - name: name - c_type: rbs_typename + c_type: rbs_type_name - name: args c_type: rbs_node_list - name: RBS::Types::ClassSingleton fields: - name: name - c_type: rbs_typename + c_type: rbs_type_name - name: RBS::Types::Function expose_location: false fields: @@ -391,7 +391,7 @@ nodes: - name: RBS::Types::Interface fields: - name: name - c_type: rbs_typename + c_type: rbs_type_name - name: args c_type: rbs_node_list - name: RBS::Types::Intersection diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index fdffb4ff6..fc25cb187 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -106,7 +106,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); rb_hash_aset(h, ID2SYM(rb_intern("super_class")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->super_class)); // rbs_ast_declarations_class_super rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); @@ -131,7 +131,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); @@ -141,13 +141,13 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_AST_DECLARATIONS_CLASSALIAS: { - rbs_ast_declarations_classalias_t *node = (rbs_ast_declarations_classalias_t *)instance; + case RBS_AST_DECLARATIONS_CLASS_ALIAS: { + rbs_ast_declarations_class_alias_t *node = (rbs_ast_declarations_class_alias_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); @@ -163,7 +163,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); @@ -197,7 +197,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); @@ -221,7 +221,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); rb_hash_aset(h, ID2SYM(rb_intern("self_types")), rbs_node_list_to_ruby_array(ctx, node->self_types)); rb_hash_aset(h, ID2SYM(rb_intern("members")), rbs_node_list_to_ruby_array(ctx, node->members)); @@ -246,7 +246,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); @@ -256,13 +256,13 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_AST_DECLARATIONS_MODULEALIAS: { - rbs_ast_declarations_modulealias_t *node = (rbs_ast_declarations_modulealias_t *)instance; + case RBS_AST_DECLARATIONS_MODULE_ALIAS: { + rbs_ast_declarations_module_alias_t *node = (rbs_ast_declarations_module_alias_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_typename - rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_type_name + rb_hash_aset(h, ID2SYM(rb_intern("old_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->old_name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); @@ -273,12 +273,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_AST_DECLARATIONS_TYPEALIAS: { - rbs_ast_declarations_typealias_t *node = (rbs_ast_declarations_typealias_t *)instance; + case RBS_AST_DECLARATIONS_TYPE_ALIAS: { + rbs_ast_declarations_type_alias_t *node = (rbs_ast_declarations_type_alias_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("type_params")), rbs_node_list_to_ruby_array(ctx, node->type_params)); rb_hash_aset(h, ID2SYM(rb_intern("type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type)); // rbs_node rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); @@ -311,12 +311,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_AST_DIRECTIVES_USE_SINGLECLAUSE: { - rbs_ast_directives_use_singleclause_t *node = (rbs_ast_directives_use_singleclause_t *)instance; + case RBS_AST_DIRECTIVES_USE_SINGLE_CLAUSE: { + rbs_ast_directives_use_single_clause_t *node = (rbs_ast_directives_use_single_clause_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("type_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type_name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("type_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->type_name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("new_name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->new_name)); // rbs_ast_symbol @@ -326,8 +326,8 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE: { - rbs_ast_directives_use_wildcardclause_t *node = (rbs_ast_directives_use_wildcardclause_t *)instance; + case RBS_AST_DIRECTIVES_USE_WILDCARD_CLAUSE: { + rbs_ast_directives_use_wildcard_clause_t *node = (rbs_ast_directives_use_wildcard_clause_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); @@ -367,8 +367,8 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_AST_MEMBERS_ATTRACCESSOR: { - rbs_ast_members_attraccessor_t *node = (rbs_ast_members_attraccessor_t *)instance; + case RBS_AST_MEMBERS_ATTR_ACCESSOR: { + rbs_ast_members_attr_accessor_t *node = (rbs_ast_members_attr_accessor_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); @@ -387,8 +387,8 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_AST_MEMBERS_ATTRREADER: { - rbs_ast_members_attrreader_t *node = (rbs_ast_members_attrreader_t *)instance; + case RBS_AST_MEMBERS_ATTR_READER: { + rbs_ast_members_attr_reader_t *node = (rbs_ast_members_attr_reader_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); @@ -407,8 +407,8 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_AST_MEMBERS_ATTRWRITER: { - rbs_ast_members_attrwriter_t *node = (rbs_ast_members_attrwriter_t *)instance; + case RBS_AST_MEMBERS_ATTR_WRITER: { + rbs_ast_members_attr_writer_t *node = (rbs_ast_members_attr_writer_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); @@ -427,8 +427,8 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE: { - rbs_ast_members_classinstancevariable_t *node = (rbs_ast_members_classinstancevariable_t *)instance; + case RBS_AST_MEMBERS_CLASS_INSTANCE_VARIABLE: { + rbs_ast_members_class_instance_variable_t *node = (rbs_ast_members_class_instance_variable_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); @@ -443,8 +443,8 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_AST_MEMBERS_CLASSVARIABLE: { - rbs_ast_members_classvariable_t *node = (rbs_ast_members_classvariable_t *)instance; + case RBS_AST_MEMBERS_CLASS_VARIABLE: { + rbs_ast_members_class_variable_t *node = (rbs_ast_members_class_variable_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); @@ -464,7 +464,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -481,7 +481,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -493,8 +493,8 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_AST_MEMBERS_INSTANCEVARIABLE: { - rbs_ast_members_instancevariable_t *node = (rbs_ast_members_instancevariable_t *)instance; + case RBS_AST_MEMBERS_INSTANCE_VARIABLE: { + rbs_ast_members_instance_variable_t *node = (rbs_ast_members_instance_variable_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); @@ -509,8 +509,8 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_AST_MEMBERS_METHODDEFINITION: { - rbs_ast_members_methoddefinition_t *node = (rbs_ast_members_methoddefinition_t *)instance; + case RBS_AST_MEMBERS_METHOD_DEFINITION: { + rbs_ast_members_method_definition_t *node = (rbs_ast_members_method_definition_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); @@ -529,8 +529,8 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD: { - rbs_ast_members_methoddefinition_overload_t *node = (rbs_ast_members_methoddefinition_overload_t *)instance; + case RBS_AST_MEMBERS_METHOD_DEFINITION_OVERLOAD: { + rbs_ast_members_method_definition_overload_t *node = (rbs_ast_members_method_definition_overload_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); @@ -548,7 +548,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); rb_hash_aset(h, ID2SYM(rb_intern("annotations")), rbs_node_list_to_ruby_array(ctx, node->annotations)); rb_hash_aset(h, ID2SYM(rb_intern("comment")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->comment)); // rbs_ast_comment @@ -593,8 +593,8 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan return rb_enc_str_new(s.start, rbs_string_len(s), rb_utf8_encoding()); } - case RBS_AST_TYPEPARAM: { - rbs_ast_typeparam_t *node = (rbs_ast_typeparam_t *)instance; + case RBS_AST_TYPE_PARAM: { + rbs_ast_type_param_t *node = (rbs_ast_type_param_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); @@ -611,8 +611,8 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_METHODTYPE: { - rbs_methodtype_t *node = (rbs_methodtype_t *)instance; + case RBS_METHOD_TYPE: { + rbs_method_type_t *node = (rbs_method_type_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); @@ -655,8 +655,8 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan rb_ary_push(array, rbs_node_list_to_ruby_array(ctx, signature->declarations)); return array; } - case RBS_TYPENAME: { - rbs_typename_t *node = (rbs_typename_t *)instance; + case RBS_TYPE_NAME: { + rbs_type_name_t *node = (rbs_type_name_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("namespace")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->rbs_namespace)); // rbs_namespace @@ -674,7 +674,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); @@ -817,12 +817,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_TYPES_CLASSINSTANCE: { - rbs_types_classinstance_t *node = (rbs_types_classinstance_t *)instance; + case RBS_TYPES_CLASS_INSTANCE: { + rbs_types_class_instance_t *node = (rbs_types_class_instance_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); @@ -832,12 +832,12 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_TYPES_CLASSSINGLETON: { - rbs_types_classsingleton_t *node = (rbs_types_classsingleton_t *)instance; + case RBS_TYPES_CLASS_SINGLETON: { + rbs_types_class_singleton_t *node = (rbs_types_class_singleton_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name return CLASS_NEW_INSTANCE( @@ -886,7 +886,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("location")), rbs_loc_to_ruby_location(ctx, node->base.location)); - rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_typename + rb_hash_aset(h, ID2SYM(rb_intern("name")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->name)); // rbs_type_name rb_hash_aset(h, ID2SYM(rb_intern("args")), rbs_node_list_to_ruby_array(ctx, node->args)); @@ -968,8 +968,8 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_TYPES_RECORD_FIELDTYPE: { - rbs_types_record_fieldtype_t *record_fieldtype = (rbs_types_record_fieldtype_t *) instance; + case RBS_TYPES_RECORD_FIELD_TYPE: { + rbs_types_record_field_type_t *record_fieldtype = (rbs_types_record_field_type_t *) instance; VALUE array = rb_ary_new(); rb_ary_push(array, rbs_struct_to_ruby_value(ctx, record_fieldtype->type)); @@ -1005,8 +1005,8 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan &h ); } - case RBS_TYPES_UNTYPEDFUNCTION: { - rbs_types_untypedfunction_t *node = (rbs_types_untypedfunction_t *)instance; + case RBS_TYPES_UNTYPED_FUNCTION: { + rbs_types_untyped_function_t *node = (rbs_types_untyped_function_t *)instance; VALUE h = rb_hash_new(); rb_hash_aset(h, ID2SYM(rb_intern("return_type")), rbs_struct_to_ruby_value(ctx, (rbs_node_t *) node->return_type)); // rbs_node diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index edac1134b..2258b69c7 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -194,7 +194,7 @@ static VALUE parse_method_type_try(VALUE a) { return Qnil; } - rbs_methodtype_t *method_type = NULL; + rbs_method_type_t *method_type = NULL; rbs_parse_method_type(parser, &method_type); raise_error_if_any(parser, arg->buffer); diff --git a/include/rbs/ast.h b/include/rbs/ast.h index 503bbe007..581eeece8 100644 --- a/include/rbs/ast.h +++ b/include/rbs/ast.h @@ -19,38 +19,38 @@ enum rbs_node_type { RBS_AST_COMMENT = 3, RBS_AST_DECLARATIONS_CLASS = 4, RBS_AST_DECLARATIONS_CLASS_SUPER = 5, - RBS_AST_DECLARATIONS_CLASSALIAS = 6, + RBS_AST_DECLARATIONS_CLASS_ALIAS = 6, RBS_AST_DECLARATIONS_CONSTANT = 7, RBS_AST_DECLARATIONS_GLOBAL = 8, RBS_AST_DECLARATIONS_INTERFACE = 9, RBS_AST_DECLARATIONS_MODULE = 10, RBS_AST_DECLARATIONS_MODULE_SELF = 11, - RBS_AST_DECLARATIONS_MODULEALIAS = 12, - RBS_AST_DECLARATIONS_TYPEALIAS = 13, + RBS_AST_DECLARATIONS_MODULE_ALIAS = 12, + RBS_AST_DECLARATIONS_TYPE_ALIAS = 13, RBS_AST_DIRECTIVES_USE = 14, - RBS_AST_DIRECTIVES_USE_SINGLECLAUSE = 15, - RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE = 16, + RBS_AST_DIRECTIVES_USE_SINGLE_CLAUSE = 15, + RBS_AST_DIRECTIVES_USE_WILDCARD_CLAUSE = 16, RBS_AST_INTEGER = 17, RBS_AST_MEMBERS_ALIAS = 18, - RBS_AST_MEMBERS_ATTRACCESSOR = 19, - RBS_AST_MEMBERS_ATTRREADER = 20, - RBS_AST_MEMBERS_ATTRWRITER = 21, - RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE = 22, - RBS_AST_MEMBERS_CLASSVARIABLE = 23, + RBS_AST_MEMBERS_ATTR_ACCESSOR = 19, + RBS_AST_MEMBERS_ATTR_READER = 20, + RBS_AST_MEMBERS_ATTR_WRITER = 21, + RBS_AST_MEMBERS_CLASS_INSTANCE_VARIABLE = 22, + RBS_AST_MEMBERS_CLASS_VARIABLE = 23, RBS_AST_MEMBERS_EXTEND = 24, RBS_AST_MEMBERS_INCLUDE = 25, - RBS_AST_MEMBERS_INSTANCEVARIABLE = 26, - RBS_AST_MEMBERS_METHODDEFINITION = 27, - RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD = 28, + RBS_AST_MEMBERS_INSTANCE_VARIABLE = 26, + RBS_AST_MEMBERS_METHOD_DEFINITION = 27, + RBS_AST_MEMBERS_METHOD_DEFINITION_OVERLOAD = 28, RBS_AST_MEMBERS_PREPEND = 29, RBS_AST_MEMBERS_PRIVATE = 30, RBS_AST_MEMBERS_PUBLIC = 31, RBS_AST_STRING = 32, - RBS_AST_TYPEPARAM = 33, - RBS_METHODTYPE = 34, + RBS_AST_TYPE_PARAM = 33, + RBS_METHOD_TYPE = 34, RBS_NAMESPACE = 35, RBS_SIGNATURE = 36, - RBS_TYPENAME = 37, + RBS_TYPE_NAME = 37, RBS_TYPES_ALIAS = 38, RBS_TYPES_BASES_ANY = 39, RBS_TYPES_BASES_BOOL = 40, @@ -62,8 +62,8 @@ enum rbs_node_type { RBS_TYPES_BASES_TOP = 46, RBS_TYPES_BASES_VOID = 47, RBS_TYPES_BLOCK = 48, - RBS_TYPES_CLASSINSTANCE = 49, - RBS_TYPES_CLASSSINGLETON = 50, + RBS_TYPES_CLASS_INSTANCE = 49, + RBS_TYPES_CLASS_SINGLETON = 50, RBS_TYPES_FUNCTION = 51, RBS_TYPES_FUNCTION_PARAM = 52, RBS_TYPES_INTERFACE = 53, @@ -72,10 +72,10 @@ enum rbs_node_type { RBS_TYPES_OPTIONAL = 56, RBS_TYPES_PROC = 57, RBS_TYPES_RECORD = 58, - RBS_TYPES_RECORD_FIELDTYPE = 59, + RBS_TYPES_RECORD_FIELD_TYPE = 59, RBS_TYPES_TUPLE = 60, RBS_TYPES_UNION = 61, - RBS_TYPES_UNTYPEDFUNCTION = 62, + RBS_TYPES_UNTYPED_FUNCTION = 62, RBS_TYPES_VARIABLE = 63, RBS_KEYWORD, RBS_AST_SYMBOL, @@ -152,7 +152,7 @@ typedef struct rbs_ast_comment { typedef struct rbs_ast_declarations_class { rbs_node_t base; - struct rbs_typename *name; + struct rbs_type_name *name; struct rbs_node_list *type_params; struct rbs_ast_declarations_class_super *super_class; struct rbs_node_list *members; @@ -163,23 +163,23 @@ typedef struct rbs_ast_declarations_class { typedef struct rbs_ast_declarations_class_super { rbs_node_t base; - struct rbs_typename *name; + struct rbs_type_name *name; struct rbs_node_list *args; } rbs_ast_declarations_class_super_t; -typedef struct rbs_ast_declarations_classalias { +typedef struct rbs_ast_declarations_class_alias { rbs_node_t base; - struct rbs_typename *new_name; - struct rbs_typename *old_name; + struct rbs_type_name *new_name; + struct rbs_type_name *old_name; struct rbs_ast_comment *comment; struct rbs_node_list *annotations; -} rbs_ast_declarations_classalias_t; +} rbs_ast_declarations_class_alias_t; typedef struct rbs_ast_declarations_constant { rbs_node_t base; - struct rbs_typename *name; + struct rbs_type_name *name; struct rbs_node *type; struct rbs_ast_comment *comment; struct rbs_node_list *annotations; @@ -197,7 +197,7 @@ typedef struct rbs_ast_declarations_global { typedef struct rbs_ast_declarations_interface { rbs_node_t base; - struct rbs_typename *name; + struct rbs_type_name *name; struct rbs_node_list *type_params; struct rbs_node_list *members; struct rbs_node_list *annotations; @@ -207,7 +207,7 @@ typedef struct rbs_ast_declarations_interface { typedef struct rbs_ast_declarations_module { rbs_node_t base; - struct rbs_typename *name; + struct rbs_type_name *name; struct rbs_node_list *type_params; struct rbs_node_list *self_types; struct rbs_node_list *members; @@ -218,28 +218,28 @@ typedef struct rbs_ast_declarations_module { typedef struct rbs_ast_declarations_module_self { rbs_node_t base; - struct rbs_typename *name; + struct rbs_type_name *name; struct rbs_node_list *args; } rbs_ast_declarations_module_self_t; -typedef struct rbs_ast_declarations_modulealias { +typedef struct rbs_ast_declarations_module_alias { rbs_node_t base; - struct rbs_typename *new_name; - struct rbs_typename *old_name; + struct rbs_type_name *new_name; + struct rbs_type_name *old_name; struct rbs_ast_comment *comment; struct rbs_node_list *annotations; -} rbs_ast_declarations_modulealias_t; +} rbs_ast_declarations_module_alias_t; -typedef struct rbs_ast_declarations_typealias { +typedef struct rbs_ast_declarations_type_alias { rbs_node_t base; - struct rbs_typename *name; + struct rbs_type_name *name; struct rbs_node_list *type_params; struct rbs_node *type; struct rbs_node_list *annotations; struct rbs_ast_comment *comment; -} rbs_ast_declarations_typealias_t; +} rbs_ast_declarations_type_alias_t; typedef struct rbs_ast_directives_use { rbs_node_t base; @@ -247,18 +247,18 @@ typedef struct rbs_ast_directives_use { struct rbs_node_list *clauses; } rbs_ast_directives_use_t; -typedef struct rbs_ast_directives_use_singleclause { +typedef struct rbs_ast_directives_use_single_clause { rbs_node_t base; - struct rbs_typename *type_name; + struct rbs_type_name *type_name; struct rbs_ast_symbol *new_name; -} rbs_ast_directives_use_singleclause_t; +} rbs_ast_directives_use_single_clause_t; -typedef struct rbs_ast_directives_use_wildcardclause { +typedef struct rbs_ast_directives_use_wildcard_clause { rbs_node_t base; struct rbs_namespace *rbs_namespace; -} rbs_ast_directives_use_wildcardclause_t; +} rbs_ast_directives_use_wildcard_clause_t; typedef struct rbs_ast_integer { rbs_node_t base; @@ -276,7 +276,7 @@ typedef struct rbs_ast_members_alias { struct rbs_ast_comment *comment; } rbs_ast_members_alias_t; -typedef struct rbs_ast_members_attraccessor { +typedef struct rbs_ast_members_attr_accessor { rbs_node_t base; struct rbs_ast_symbol *name; @@ -286,9 +286,9 @@ typedef struct rbs_ast_members_attraccessor { struct rbs_node_list *annotations; struct rbs_ast_comment *comment; struct rbs_keyword *visibility; -} rbs_ast_members_attraccessor_t; +} rbs_ast_members_attr_accessor_t; -typedef struct rbs_ast_members_attrreader { +typedef struct rbs_ast_members_attr_reader { rbs_node_t base; struct rbs_ast_symbol *name; @@ -298,9 +298,9 @@ typedef struct rbs_ast_members_attrreader { struct rbs_node_list *annotations; struct rbs_ast_comment *comment; struct rbs_keyword *visibility; -} rbs_ast_members_attrreader_t; +} rbs_ast_members_attr_reader_t; -typedef struct rbs_ast_members_attrwriter { +typedef struct rbs_ast_members_attr_writer { rbs_node_t base; struct rbs_ast_symbol *name; @@ -310,28 +310,28 @@ typedef struct rbs_ast_members_attrwriter { struct rbs_node_list *annotations; struct rbs_ast_comment *comment; struct rbs_keyword *visibility; -} rbs_ast_members_attrwriter_t; +} rbs_ast_members_attr_writer_t; -typedef struct rbs_ast_members_classinstancevariable { +typedef struct rbs_ast_members_class_instance_variable { rbs_node_t base; struct rbs_ast_symbol *name; struct rbs_node *type; struct rbs_ast_comment *comment; -} rbs_ast_members_classinstancevariable_t; +} rbs_ast_members_class_instance_variable_t; -typedef struct rbs_ast_members_classvariable { +typedef struct rbs_ast_members_class_variable { rbs_node_t base; struct rbs_ast_symbol *name; struct rbs_node *type; struct rbs_ast_comment *comment; -} rbs_ast_members_classvariable_t; +} rbs_ast_members_class_variable_t; typedef struct rbs_ast_members_extend { rbs_node_t base; - struct rbs_typename *name; + struct rbs_type_name *name; struct rbs_node_list *args; struct rbs_node_list *annotations; struct rbs_ast_comment *comment; @@ -340,21 +340,21 @@ typedef struct rbs_ast_members_extend { typedef struct rbs_ast_members_include { rbs_node_t base; - struct rbs_typename *name; + struct rbs_type_name *name; struct rbs_node_list *args; struct rbs_node_list *annotations; struct rbs_ast_comment *comment; } rbs_ast_members_include_t; -typedef struct rbs_ast_members_instancevariable { +typedef struct rbs_ast_members_instance_variable { rbs_node_t base; struct rbs_ast_symbol *name; struct rbs_node *type; struct rbs_ast_comment *comment; -} rbs_ast_members_instancevariable_t; +} rbs_ast_members_instance_variable_t; -typedef struct rbs_ast_members_methoddefinition { +typedef struct rbs_ast_members_method_definition { rbs_node_t base; struct rbs_ast_symbol *name; @@ -364,19 +364,19 @@ typedef struct rbs_ast_members_methoddefinition { struct rbs_ast_comment *comment; bool overloading; struct rbs_keyword *visibility; -} rbs_ast_members_methoddefinition_t; +} rbs_ast_members_method_definition_t; -typedef struct rbs_ast_members_methoddefinition_overload { +typedef struct rbs_ast_members_method_definition_overload { rbs_node_t base; struct rbs_node_list *annotations; struct rbs_node *method_type; -} rbs_ast_members_methoddefinition_overload_t; +} rbs_ast_members_method_definition_overload_t; typedef struct rbs_ast_members_prepend { rbs_node_t base; - struct rbs_typename *name; + struct rbs_type_name *name; struct rbs_node_list *args; struct rbs_node_list *annotations; struct rbs_ast_comment *comment; @@ -398,7 +398,7 @@ typedef struct rbs_ast_string { rbs_string_t string; } rbs_ast_string_t; -typedef struct rbs_ast_typeparam { +typedef struct rbs_ast_type_param { rbs_node_t base; struct rbs_ast_symbol *name; @@ -406,15 +406,15 @@ typedef struct rbs_ast_typeparam { struct rbs_node *upper_bound; struct rbs_node *default_type; bool unchecked; -} rbs_ast_typeparam_t; +} rbs_ast_type_param_t; -typedef struct rbs_methodtype { +typedef struct rbs_method_type { rbs_node_t base; struct rbs_node_list *type_params; struct rbs_node *type; struct rbs_types_block *block; -} rbs_methodtype_t; +} rbs_method_type_t; typedef struct rbs_namespace { rbs_node_t base; @@ -430,17 +430,17 @@ typedef struct rbs_signature { struct rbs_node_list *declarations; } rbs_signature_t; -typedef struct rbs_typename { +typedef struct rbs_type_name { rbs_node_t base; struct rbs_namespace *rbs_namespace; struct rbs_ast_symbol *name; -} rbs_typename_t; +} rbs_type_name_t; typedef struct rbs_types_alias { rbs_node_t base; - struct rbs_typename *name; + struct rbs_type_name *name; struct rbs_node_list *args; } rbs_types_alias_t; @@ -498,18 +498,18 @@ typedef struct rbs_types_block { struct rbs_node *self_type; } rbs_types_block_t; -typedef struct rbs_types_classinstance { +typedef struct rbs_types_class_instance { rbs_node_t base; - struct rbs_typename *name; + struct rbs_type_name *name; struct rbs_node_list *args; -} rbs_types_classinstance_t; +} rbs_types_class_instance_t; -typedef struct rbs_types_classsingleton { +typedef struct rbs_types_class_singleton { rbs_node_t base; - struct rbs_typename *name; -} rbs_types_classsingleton_t; + struct rbs_type_name *name; +} rbs_types_class_singleton_t; typedef struct rbs_types_function { rbs_node_t base; @@ -534,7 +534,7 @@ typedef struct rbs_types_function_param { typedef struct rbs_types_interface { rbs_node_t base; - struct rbs_typename *name; + struct rbs_type_name *name; struct rbs_node_list *args; } rbs_types_interface_t; @@ -570,12 +570,12 @@ typedef struct rbs_types_record { struct rbs_hash *all_fields; } rbs_types_record_t; -typedef struct rbs_types_record_fieldtype { +typedef struct rbs_types_record_field_type { rbs_node_t base; struct rbs_node *type; bool required; -} rbs_types_record_fieldtype_t; +} rbs_types_record_field_type_t; typedef struct rbs_types_tuple { rbs_node_t base; @@ -589,11 +589,11 @@ typedef struct rbs_types_union { struct rbs_node_list *types; } rbs_types_union_t; -typedef struct rbs_types_untypedfunction { +typedef struct rbs_types_untyped_function { rbs_node_t base; struct rbs_node *return_type; -} rbs_types_untypedfunction_t; +} rbs_types_untyped_function_t; typedef struct rbs_types_variable { rbs_node_t base; @@ -624,41 +624,41 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *, rbs_location_t *, rbs_co rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string); rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, rbs_location_t *location, bool value); rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string); -rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); -rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args); -rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_ast_comment_t *comment, rbs_node_list_t *annotations); -rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_t *type, rbs_ast_comment_t *comment, rbs_node_list_t *annotations); +rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); +rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args); +rbs_ast_declarations_class_alias_t *rbs_ast_declarations_class_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *new_name, rbs_type_name_t *old_name, rbs_ast_comment_t *comment, rbs_node_list_t *annotations); +rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_t *type, rbs_ast_comment_t *comment, rbs_node_list_t *annotations); rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment, rbs_node_list_t *annotations); -rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); -rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); -rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args); -rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_ast_comment_t *comment, rbs_node_list_t *annotations); -rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); +rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); +rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); +rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args); +rbs_ast_declarations_module_alias_t *rbs_ast_declarations_module_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *new_name, rbs_type_name_t *old_name, rbs_ast_comment_t *comment, rbs_node_list_t *annotations); +rbs_ast_declarations_type_alias_t *rbs_ast_declarations_type_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *clauses); -rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *type_name, rbs_ast_symbol_t *new_name); -rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace); +rbs_ast_directives_use_single_clause_t *rbs_ast_directives_use_single_clause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *type_name, rbs_ast_symbol_t *new_name); +rbs_ast_directives_use_wildcard_clause_t *rbs_ast_directives_use_wildcard_clause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace); rbs_ast_integer_t *rbs_ast_integer_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string_representation); rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); -rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); -rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); -rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); -rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment); -rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment); -rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); -rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); -rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment); -rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, bool overloading, rbs_keyword_t *visibility); -rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *annotations, rbs_node_t *method_type); -rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); +rbs_ast_members_attr_accessor_t *rbs_ast_members_attr_accessor_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); +rbs_ast_members_attr_reader_t *rbs_ast_members_attr_reader_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); +rbs_ast_members_attr_writer_t *rbs_ast_members_attr_writer_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility); +rbs_ast_members_class_instance_variable_t *rbs_ast_members_class_instance_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment); +rbs_ast_members_class_variable_t *rbs_ast_members_class_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment); +rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); +rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); +rbs_ast_members_instance_variable_t *rbs_ast_members_instance_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment); +rbs_ast_members_method_definition_t *rbs_ast_members_method_definition_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, bool overloading, rbs_keyword_t *visibility); +rbs_ast_members_method_definition_overload_t *rbs_ast_members_method_definition_overload_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *annotations, rbs_node_t *method_type); +rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment); rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocator, rbs_location_t *location); rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, rbs_location_t *location); rbs_ast_string_t *rbs_ast_string_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string); -rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked); -rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block); +rbs_ast_type_param_t *rbs_ast_type_param_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked); +rbs_method_type_t *rbs_method_type_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block); rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *path, bool absolute); rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *directives, rbs_node_list_t *declarations); -rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace, rbs_ast_symbol_t *name); -rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args); +rbs_type_name_t *rbs_type_name_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace, rbs_ast_symbol_t *name); +rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args); rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, rbs_location_t *location, bool todo); rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, rbs_location_t *location); rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, rbs_location_t *location); @@ -669,20 +669,20 @@ rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, rbs rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, rbs_location_t *location); rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, rbs_location_t *location); rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, bool required, rbs_node_t *self_type); -rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args); -rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name); +rbs_types_class_instance_t *rbs_types_class_instance_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args); +rbs_types_class_singleton_t *rbs_types_class_singleton_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name); rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *required_positionals, rbs_node_list_t *optional_positionals, rbs_node_t *rest_positionals, rbs_node_list_t *trailing_positionals, rbs_hash_t *required_keywords, rbs_hash_t *optional_keywords, rbs_node_t *rest_keywords, rbs_node_t *return_type); rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, rbs_ast_symbol_t *name); -rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args); +rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args); rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types); rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *literal); rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type); rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, rbs_types_block_t *block, rbs_node_t *self_type); rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_hash_t *all_fields); -rbs_types_record_fieldtype_t *rbs_types_record_fieldtype_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, bool required); +rbs_types_record_field_type_t *rbs_types_record_field_type_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, bool required); rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types); rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types); -rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *return_type); +rbs_types_untyped_function_t *rbs_types_untyped_function_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *return_type); rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name); void rbs_node_destroy(rbs_node_t *any_node); diff --git a/include/rbs/parser.h b/include/rbs/parser.h index 48a9a16f2..1399b908a 100644 --- a/include/rbs/parser.h +++ b/include/rbs/parser.h @@ -127,7 +127,7 @@ rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5); bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type); -bool rbs_parse_method_type(rbs_parser_t *parser, rbs_methodtype_t **method_type); +bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type); bool rbs_parse_signature(rbs_parser_t *parser, rbs_signature_t **signature); #endif diff --git a/src/ast.c b/src/ast.c index 5056d75d9..18f9d7f81 100644 --- a/src/ast.c +++ b/src/ast.c @@ -18,38 +18,38 @@ const char* rbs_node_type_name(rbs_node_t *node) { case RBS_AST_COMMENT: return "RBS::AST::Comment"; case RBS_AST_DECLARATIONS_CLASS: return "RBS::AST::Declarations::Class"; case RBS_AST_DECLARATIONS_CLASS_SUPER: return "RBS::AST::Declarations::Class::Super"; - case RBS_AST_DECLARATIONS_CLASSALIAS: return "RBS::AST::Declarations::ClassAlias"; + case RBS_AST_DECLARATIONS_CLASS_ALIAS: return "RBS::AST::Declarations::ClassAlias"; case RBS_AST_DECLARATIONS_CONSTANT: return "RBS::AST::Declarations::Constant"; case RBS_AST_DECLARATIONS_GLOBAL: return "RBS::AST::Declarations::Global"; case RBS_AST_DECLARATIONS_INTERFACE: return "RBS::AST::Declarations::Interface"; case RBS_AST_DECLARATIONS_MODULE: return "RBS::AST::Declarations::Module"; case RBS_AST_DECLARATIONS_MODULE_SELF: return "RBS::AST::Declarations::Module::Self"; - case RBS_AST_DECLARATIONS_MODULEALIAS: return "RBS::AST::Declarations::ModuleAlias"; - case RBS_AST_DECLARATIONS_TYPEALIAS: return "RBS::AST::Declarations::TypeAlias"; + case RBS_AST_DECLARATIONS_MODULE_ALIAS: return "RBS::AST::Declarations::ModuleAlias"; + case RBS_AST_DECLARATIONS_TYPE_ALIAS: return "RBS::AST::Declarations::TypeAlias"; case RBS_AST_DIRECTIVES_USE: return "RBS::AST::Directives::Use"; - case RBS_AST_DIRECTIVES_USE_SINGLECLAUSE: return "RBS::AST::Directives::Use::SingleClause"; - case RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE: return "RBS::AST::Directives::Use::WildcardClause"; + case RBS_AST_DIRECTIVES_USE_SINGLE_CLAUSE: return "RBS::AST::Directives::Use::SingleClause"; + case RBS_AST_DIRECTIVES_USE_WILDCARD_CLAUSE: return "RBS::AST::Directives::Use::WildcardClause"; case RBS_AST_INTEGER: return "RBS::AST::Integer"; case RBS_AST_MEMBERS_ALIAS: return "RBS::AST::Members::Alias"; - case RBS_AST_MEMBERS_ATTRACCESSOR: return "RBS::AST::Members::AttrAccessor"; - case RBS_AST_MEMBERS_ATTRREADER: return "RBS::AST::Members::AttrReader"; - case RBS_AST_MEMBERS_ATTRWRITER: return "RBS::AST::Members::AttrWriter"; - case RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE: return "RBS::AST::Members::ClassInstanceVariable"; - case RBS_AST_MEMBERS_CLASSVARIABLE: return "RBS::AST::Members::ClassVariable"; + case RBS_AST_MEMBERS_ATTR_ACCESSOR: return "RBS::AST::Members::AttrAccessor"; + case RBS_AST_MEMBERS_ATTR_READER: return "RBS::AST::Members::AttrReader"; + case RBS_AST_MEMBERS_ATTR_WRITER: return "RBS::AST::Members::AttrWriter"; + case RBS_AST_MEMBERS_CLASS_INSTANCE_VARIABLE: return "RBS::AST::Members::ClassInstanceVariable"; + case RBS_AST_MEMBERS_CLASS_VARIABLE: return "RBS::AST::Members::ClassVariable"; case RBS_AST_MEMBERS_EXTEND: return "RBS::AST::Members::Extend"; case RBS_AST_MEMBERS_INCLUDE: return "RBS::AST::Members::Include"; - case RBS_AST_MEMBERS_INSTANCEVARIABLE: return "RBS::AST::Members::InstanceVariable"; - case RBS_AST_MEMBERS_METHODDEFINITION: return "RBS::AST::Members::MethodDefinition"; - case RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD: return "RBS::AST::Members::MethodDefinition::Overload"; + case RBS_AST_MEMBERS_INSTANCE_VARIABLE: return "RBS::AST::Members::InstanceVariable"; + case RBS_AST_MEMBERS_METHOD_DEFINITION: return "RBS::AST::Members::MethodDefinition"; + case RBS_AST_MEMBERS_METHOD_DEFINITION_OVERLOAD: return "RBS::AST::Members::MethodDefinition::Overload"; case RBS_AST_MEMBERS_PREPEND: return "RBS::AST::Members::Prepend"; case RBS_AST_MEMBERS_PRIVATE: return "RBS::AST::Members::Private"; case RBS_AST_MEMBERS_PUBLIC: return "RBS::AST::Members::Public"; case RBS_AST_STRING: return "RBS::AST::String"; - case RBS_AST_TYPEPARAM: return "RBS::AST::TypeParam"; - case RBS_METHODTYPE: return "RBS::MethodType"; + case RBS_AST_TYPE_PARAM: return "RBS::AST::TypeParam"; + case RBS_METHOD_TYPE: return "RBS::MethodType"; case RBS_NAMESPACE: return "RBS::Namespace"; case RBS_SIGNATURE: return "RBS::Signature"; - case RBS_TYPENAME: return "RBS::TypeName"; + case RBS_TYPE_NAME: return "RBS::TypeName"; case RBS_TYPES_ALIAS: return "RBS::Types::Alias"; case RBS_TYPES_BASES_ANY: return "RBS::Types::Bases::Any"; case RBS_TYPES_BASES_BOOL: return "RBS::Types::Bases::Bool"; @@ -61,8 +61,8 @@ const char* rbs_node_type_name(rbs_node_t *node) { case RBS_TYPES_BASES_TOP: return "RBS::Types::Bases::Top"; case RBS_TYPES_BASES_VOID: return "RBS::Types::Bases::Void"; case RBS_TYPES_BLOCK: return "RBS::Types::Block"; - case RBS_TYPES_CLASSINSTANCE: return "RBS::Types::ClassInstance"; - case RBS_TYPES_CLASSSINGLETON: return "RBS::Types::ClassSingleton"; + case RBS_TYPES_CLASS_INSTANCE: return "RBS::Types::ClassInstance"; + case RBS_TYPES_CLASS_SINGLETON: return "RBS::Types::ClassSingleton"; case RBS_TYPES_FUNCTION: return "RBS::Types::Function"; case RBS_TYPES_FUNCTION_PARAM: return "RBS::Types::Function::Param"; case RBS_TYPES_INTERFACE: return "RBS::Types::Interface"; @@ -71,10 +71,10 @@ const char* rbs_node_type_name(rbs_node_t *node) { case RBS_TYPES_OPTIONAL: return "RBS::Types::Optional"; case RBS_TYPES_PROC: return "RBS::Types::Proc"; case RBS_TYPES_RECORD: return "RBS::Types::Record"; - case RBS_TYPES_RECORD_FIELDTYPE: return "RBS::Types::Record::FieldType"; + case RBS_TYPES_RECORD_FIELD_TYPE: return "RBS::Types::Record::FieldType"; case RBS_TYPES_TUPLE: return "RBS::Types::Tuple"; case RBS_TYPES_UNION: return "RBS::Types::Union"; - case RBS_TYPES_UNTYPEDFUNCTION: return "RBS::Types::UntypedFunction"; + case RBS_TYPES_UNTYPED_FUNCTION: return "RBS::Types::UntypedFunction"; case RBS_TYPES_VARIABLE: return "RBS::Types::Variable"; case RBS_AST_SYMBOL: return "Symbol"; default: return "Unknown"; @@ -284,7 +284,7 @@ rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, rbs_location_ return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { +rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_class_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_t); @@ -304,7 +304,7 @@ rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *al return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { +rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args) { rbs_ast_declarations_class_super_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_super_t); @@ -320,13 +320,13 @@ rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_all return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_ast_comment_t *comment, rbs_node_list_t *annotations) { - rbs_ast_declarations_classalias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_classalias_t); +rbs_ast_declarations_class_alias_t *rbs_ast_declarations_class_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *new_name, rbs_type_name_t *old_name, rbs_ast_comment_t *comment, rbs_node_list_t *annotations) { + rbs_ast_declarations_class_alias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_alias_t); - *instance = (rbs_ast_declarations_classalias_t) { + *instance = (rbs_ast_declarations_class_alias_t) { .base = (rbs_node_t) { - .type = RBS_AST_DECLARATIONS_CLASSALIAS, + .type = RBS_AST_DECLARATIONS_CLASS_ALIAS, .location = location, }, .new_name = new_name, @@ -338,7 +338,7 @@ rbs_ast_declarations_classalias_t *rbs_ast_declarations_classalias_new(rbs_alloc return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_t *type, rbs_ast_comment_t *comment, rbs_node_list_t *annotations) { +rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_t *type, rbs_ast_comment_t *comment, rbs_node_list_t *annotations) { rbs_ast_declarations_constant_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_constant_t); @@ -374,7 +374,7 @@ rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t * return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { +rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_interface_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_interface_t); @@ -393,7 +393,7 @@ rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocat return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { +rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_module_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_t); @@ -413,7 +413,7 @@ rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t * return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { +rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args) { rbs_ast_declarations_module_self_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_self_t); @@ -429,13 +429,13 @@ rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_all return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *new_name, rbs_typename_t *old_name, rbs_ast_comment_t *comment, rbs_node_list_t *annotations) { - rbs_ast_declarations_modulealias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_modulealias_t); +rbs_ast_declarations_module_alias_t *rbs_ast_declarations_module_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *new_name, rbs_type_name_t *old_name, rbs_ast_comment_t *comment, rbs_node_list_t *annotations) { + rbs_ast_declarations_module_alias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_alias_t); - *instance = (rbs_ast_declarations_modulealias_t) { + *instance = (rbs_ast_declarations_module_alias_t) { .base = (rbs_node_t) { - .type = RBS_AST_DECLARATIONS_MODULEALIAS, + .type = RBS_AST_DECLARATIONS_MODULE_ALIAS, .location = location, }, .new_name = new_name, @@ -447,13 +447,13 @@ rbs_ast_declarations_modulealias_t *rbs_ast_declarations_modulealias_new(rbs_all return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_declarations_typealias_t *rbs_ast_declarations_typealias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { - rbs_ast_declarations_typealias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_typealias_t); +rbs_ast_declarations_type_alias_t *rbs_ast_declarations_type_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { + rbs_ast_declarations_type_alias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_type_alias_t); - *instance = (rbs_ast_declarations_typealias_t) { + *instance = (rbs_ast_declarations_type_alias_t) { .base = (rbs_node_t) { - .type = RBS_AST_DECLARATIONS_TYPEALIAS, + .type = RBS_AST_DECLARATIONS_TYPE_ALIAS, .location = location, }, .name = name, @@ -481,13 +481,13 @@ rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *type_name, rbs_ast_symbol_t *new_name) { - rbs_ast_directives_use_singleclause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_singleclause_t); +rbs_ast_directives_use_single_clause_t *rbs_ast_directives_use_single_clause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *type_name, rbs_ast_symbol_t *new_name) { + rbs_ast_directives_use_single_clause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_single_clause_t); - *instance = (rbs_ast_directives_use_singleclause_t) { + *instance = (rbs_ast_directives_use_single_clause_t) { .base = (rbs_node_t) { - .type = RBS_AST_DIRECTIVES_USE_SINGLECLAUSE, + .type = RBS_AST_DIRECTIVES_USE_SINGLE_CLAUSE, .location = location, }, .type_name = type_name, @@ -497,13 +497,13 @@ rbs_ast_directives_use_singleclause_t *rbs_ast_directives_use_singleclause_new(r return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_directives_use_wildcardclause_t *rbs_ast_directives_use_wildcardclause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace) { - rbs_ast_directives_use_wildcardclause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_wildcardclause_t); +rbs_ast_directives_use_wildcard_clause_t *rbs_ast_directives_use_wildcard_clause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace) { + rbs_ast_directives_use_wildcard_clause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_wildcard_clause_t); - *instance = (rbs_ast_directives_use_wildcardclause_t) { + *instance = (rbs_ast_directives_use_wildcard_clause_t) { .base = (rbs_node_t) { - .type = RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE, + .type = RBS_AST_DIRECTIVES_USE_WILDCARD_CLAUSE, .location = location, }, .rbs_namespace = rbs_namespace, @@ -546,13 +546,13 @@ rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, r return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { - rbs_ast_members_attraccessor_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attraccessor_t); +rbs_ast_members_attr_accessor_t *rbs_ast_members_attr_accessor_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { + rbs_ast_members_attr_accessor_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attr_accessor_t); - *instance = (rbs_ast_members_attraccessor_t) { + *instance = (rbs_ast_members_attr_accessor_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_ATTRACCESSOR, + .type = RBS_AST_MEMBERS_ATTR_ACCESSOR, .location = location, }, .name = name, @@ -567,13 +567,13 @@ rbs_ast_members_attraccessor_t *rbs_ast_members_attraccessor_new(rbs_allocator_t return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { - rbs_ast_members_attrreader_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attrreader_t); +rbs_ast_members_attr_reader_t *rbs_ast_members_attr_reader_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { + rbs_ast_members_attr_reader_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attr_reader_t); - *instance = (rbs_ast_members_attrreader_t) { + *instance = (rbs_ast_members_attr_reader_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_ATTRREADER, + .type = RBS_AST_MEMBERS_ATTR_READER, .location = location, }, .name = name, @@ -588,13 +588,13 @@ rbs_ast_members_attrreader_t *rbs_ast_members_attrreader_new(rbs_allocator_t *al return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { - rbs_ast_members_attrwriter_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attrwriter_t); +rbs_ast_members_attr_writer_t *rbs_ast_members_attr_writer_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { + rbs_ast_members_attr_writer_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attr_writer_t); - *instance = (rbs_ast_members_attrwriter_t) { + *instance = (rbs_ast_members_attr_writer_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_ATTRWRITER, + .type = RBS_AST_MEMBERS_ATTR_WRITER, .location = location, }, .name = name, @@ -609,13 +609,13 @@ rbs_ast_members_attrwriter_t *rbs_ast_members_attrwriter_new(rbs_allocator_t *al return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { - rbs_ast_members_classinstancevariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_classinstancevariable_t); +rbs_ast_members_class_instance_variable_t *rbs_ast_members_class_instance_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { + rbs_ast_members_class_instance_variable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_class_instance_variable_t); - *instance = (rbs_ast_members_classinstancevariable_t) { + *instance = (rbs_ast_members_class_instance_variable_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE, + .type = RBS_AST_MEMBERS_CLASS_INSTANCE_VARIABLE, .location = location, }, .name = name, @@ -626,13 +626,13 @@ rbs_ast_members_classinstancevariable_t *rbs_ast_members_classinstancevariable_n return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { - rbs_ast_members_classvariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_classvariable_t); +rbs_ast_members_class_variable_t *rbs_ast_members_class_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { + rbs_ast_members_class_variable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_class_variable_t); - *instance = (rbs_ast_members_classvariable_t) { + *instance = (rbs_ast_members_class_variable_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_CLASSVARIABLE, + .type = RBS_AST_MEMBERS_CLASS_VARIABLE, .location = location, }, .name = name, @@ -643,7 +643,7 @@ rbs_ast_members_classvariable_t *rbs_ast_members_classvariable_new(rbs_allocator return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { +rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_extend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_extend_t); @@ -661,7 +661,7 @@ rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { +rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_include_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_include_t); @@ -679,13 +679,13 @@ rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocato return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { - rbs_ast_members_instancevariable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_instancevariable_t); +rbs_ast_members_instance_variable_t *rbs_ast_members_instance_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { + rbs_ast_members_instance_variable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_instance_variable_t); - *instance = (rbs_ast_members_instancevariable_t) { + *instance = (rbs_ast_members_instance_variable_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_INSTANCEVARIABLE, + .type = RBS_AST_MEMBERS_INSTANCE_VARIABLE, .location = location, }, .name = name, @@ -696,13 +696,13 @@ rbs_ast_members_instancevariable_t *rbs_ast_members_instancevariable_new(rbs_all return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, bool overloading, rbs_keyword_t *visibility) { - rbs_ast_members_methoddefinition_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_methoddefinition_t); +rbs_ast_members_method_definition_t *rbs_ast_members_method_definition_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, bool overloading, rbs_keyword_t *visibility) { + rbs_ast_members_method_definition_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_method_definition_t); - *instance = (rbs_ast_members_methoddefinition_t) { + *instance = (rbs_ast_members_method_definition_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_METHODDEFINITION, + .type = RBS_AST_MEMBERS_METHOD_DEFINITION, .location = location, }, .name = name, @@ -717,13 +717,13 @@ rbs_ast_members_methoddefinition_t *rbs_ast_members_methoddefinition_new(rbs_all return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_overload_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *annotations, rbs_node_t *method_type) { - rbs_ast_members_methoddefinition_overload_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_methoddefinition_overload_t); +rbs_ast_members_method_definition_overload_t *rbs_ast_members_method_definition_overload_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *annotations, rbs_node_t *method_type) { + rbs_ast_members_method_definition_overload_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_method_definition_overload_t); - *instance = (rbs_ast_members_methoddefinition_overload_t) { + *instance = (rbs_ast_members_method_definition_overload_t) { .base = (rbs_node_t) { - .type = RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD, + .type = RBS_AST_MEMBERS_METHOD_DEFINITION_OVERLOAD, .location = location, }, .annotations = annotations, @@ -733,7 +733,7 @@ rbs_ast_members_methoddefinition_overload_t *rbs_ast_members_methoddefinition_ov return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { +rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_prepend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_prepend_t); @@ -794,13 +794,13 @@ rbs_ast_string_t *rbs_ast_string_new(rbs_allocator_t *allocator, rbs_location_t return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked) { - rbs_ast_typeparam_t *instance = rbs_allocator_alloc(allocator, rbs_ast_typeparam_t); +rbs_ast_type_param_t *rbs_ast_type_param_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked) { + rbs_ast_type_param_t *instance = rbs_allocator_alloc(allocator, rbs_ast_type_param_t); - *instance = (rbs_ast_typeparam_t) { + *instance = (rbs_ast_type_param_t) { .base = (rbs_node_t) { - .type = RBS_AST_TYPEPARAM, + .type = RBS_AST_TYPE_PARAM, .location = location, }, .name = name, @@ -813,13 +813,13 @@ rbs_ast_typeparam_t *rbs_ast_typeparam_new(rbs_allocator_t *allocator, rbs_locat return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_methodtype_t *rbs_methodtype_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block) { - rbs_methodtype_t *instance = rbs_allocator_alloc(allocator, rbs_methodtype_t); +rbs_method_type_t *rbs_method_type_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block) { + rbs_method_type_t *instance = rbs_allocator_alloc(allocator, rbs_method_type_t); - *instance = (rbs_methodtype_t) { + *instance = (rbs_method_type_t) { .base = (rbs_node_t) { - .type = RBS_METHODTYPE, + .type = RBS_METHOD_TYPE, .location = location, }, .type_params = type_params, @@ -862,13 +862,13 @@ rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_location_t *l return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace, rbs_ast_symbol_t *name) { - rbs_typename_t *instance = rbs_allocator_alloc(allocator, rbs_typename_t); +rbs_type_name_t *rbs_type_name_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace, rbs_ast_symbol_t *name) { + rbs_type_name_t *instance = rbs_allocator_alloc(allocator, rbs_type_name_t); - *instance = (rbs_typename_t) { + *instance = (rbs_type_name_t) { .base = (rbs_node_t) { - .type = RBS_TYPENAME, + .type = RBS_TYPE_NAME, .location = location, }, .rbs_namespace = rbs_namespace, @@ -878,7 +878,7 @@ rbs_typename_t *rbs_typename_new(rbs_allocator_t *allocator, rbs_location_t *loc return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { +rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args) { rbs_types_alias_t *instance = rbs_allocator_alloc(allocator, rbs_types_alias_t); @@ -1038,13 +1038,13 @@ rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_location_ return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { - rbs_types_classinstance_t *instance = rbs_allocator_alloc(allocator, rbs_types_classinstance_t); +rbs_types_class_instance_t *rbs_types_class_instance_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args) { + rbs_types_class_instance_t *instance = rbs_allocator_alloc(allocator, rbs_types_class_instance_t); - *instance = (rbs_types_classinstance_t) { + *instance = (rbs_types_class_instance_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_CLASSINSTANCE, + .type = RBS_TYPES_CLASS_INSTANCE, .location = location, }, .name = name, @@ -1054,13 +1054,13 @@ rbs_types_classinstance_t *rbs_types_classinstance_new(rbs_allocator_t *allocato return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_types_classsingleton_t *rbs_types_classsingleton_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name) { - rbs_types_classsingleton_t *instance = rbs_allocator_alloc(allocator, rbs_types_classsingleton_t); +rbs_types_class_singleton_t *rbs_types_class_singleton_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name) { + rbs_types_class_singleton_t *instance = rbs_allocator_alloc(allocator, rbs_types_class_singleton_t); - *instance = (rbs_types_classsingleton_t) { + *instance = (rbs_types_class_singleton_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_CLASSSINGLETON, + .type = RBS_TYPES_CLASS_SINGLETON, .location = location, }, .name = name, @@ -1107,7 +1107,7 @@ rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *alloca return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_typename_t *name, rbs_node_list_t *args) { +rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args) { rbs_types_interface_t *instance = rbs_allocator_alloc(allocator, rbs_types_interface_t); @@ -1200,13 +1200,13 @@ rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, rbs_locatio return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_types_record_fieldtype_t *rbs_types_record_fieldtype_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, bool required) { - rbs_types_record_fieldtype_t *instance = rbs_allocator_alloc(allocator, rbs_types_record_fieldtype_t); +rbs_types_record_field_type_t *rbs_types_record_field_type_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, bool required) { + rbs_types_record_field_type_t *instance = rbs_allocator_alloc(allocator, rbs_types_record_field_type_t); - *instance = (rbs_types_record_fieldtype_t) { + *instance = (rbs_types_record_field_type_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_RECORD_FIELDTYPE, + .type = RBS_TYPES_RECORD_FIELD_TYPE, .location = location, }, .type = type, @@ -1246,13 +1246,13 @@ rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_location_ return instance; } #line 176 "prism/templates/src/ast.c.erb" -rbs_types_untypedfunction_t *rbs_types_untypedfunction_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *return_type) { - rbs_types_untypedfunction_t *instance = rbs_allocator_alloc(allocator, rbs_types_untypedfunction_t); +rbs_types_untyped_function_t *rbs_types_untyped_function_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *return_type) { + rbs_types_untyped_function_t *instance = rbs_allocator_alloc(allocator, rbs_types_untyped_function_t); - *instance = (rbs_types_untypedfunction_t) { + *instance = (rbs_types_untyped_function_t) { .base = (rbs_node_t) { - .type = RBS_TYPES_UNTYPEDFUNCTION, + .type = RBS_TYPES_UNTYPED_FUNCTION, .location = location, }, .return_type = return_type, @@ -1320,8 +1320,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DECLARATIONS_CLASSALIAS: { - rbs_ast_declarations_classalias_t *node = (rbs_ast_declarations_classalias_t *)any_node; + case RBS_AST_DECLARATIONS_CLASS_ALIAS: { + rbs_ast_declarations_class_alias_t *node = (rbs_ast_declarations_class_alias_t *)any_node; if (node->new_name != NULL) { rbs_node_destroy((rbs_node_t *) node->new_name); @@ -1409,8 +1409,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DECLARATIONS_MODULEALIAS: { - rbs_ast_declarations_modulealias_t *node = (rbs_ast_declarations_modulealias_t *)any_node; + case RBS_AST_DECLARATIONS_MODULE_ALIAS: { + rbs_ast_declarations_module_alias_t *node = (rbs_ast_declarations_module_alias_t *)any_node; if (node->new_name != NULL) { rbs_node_destroy((rbs_node_t *) node->new_name); @@ -1425,8 +1425,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DECLARATIONS_TYPEALIAS: { - rbs_ast_declarations_typealias_t *node = (rbs_ast_declarations_typealias_t *)any_node; + case RBS_AST_DECLARATIONS_TYPE_ALIAS: { + rbs_ast_declarations_type_alias_t *node = (rbs_ast_declarations_type_alias_t *)any_node; if (node->name != NULL) { rbs_node_destroy((rbs_node_t *) node->name); @@ -1449,8 +1449,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DIRECTIVES_USE_SINGLECLAUSE: { - rbs_ast_directives_use_singleclause_t *node = (rbs_ast_directives_use_singleclause_t *)any_node; + case RBS_AST_DIRECTIVES_USE_SINGLE_CLAUSE: { + rbs_ast_directives_use_single_clause_t *node = (rbs_ast_directives_use_single_clause_t *)any_node; if (node->type_name != NULL) { rbs_node_destroy((rbs_node_t *) node->type_name); @@ -1461,8 +1461,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DIRECTIVES_USE_WILDCARDCLAUSE: { - rbs_ast_directives_use_wildcardclause_t *node = (rbs_ast_directives_use_wildcardclause_t *)any_node; + case RBS_AST_DIRECTIVES_USE_WILDCARD_CLAUSE: { + rbs_ast_directives_use_wildcard_clause_t *node = (rbs_ast_directives_use_wildcard_clause_t *)any_node; if (node->rbs_namespace != NULL) { rbs_node_destroy((rbs_node_t *) node->rbs_namespace); @@ -1493,8 +1493,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_ATTRACCESSOR: { - rbs_ast_members_attraccessor_t *node = (rbs_ast_members_attraccessor_t *)any_node; + case RBS_AST_MEMBERS_ATTR_ACCESSOR: { + rbs_ast_members_attr_accessor_t *node = (rbs_ast_members_attr_accessor_t *)any_node; if (node->name != NULL) { rbs_node_destroy((rbs_node_t *) node->name); @@ -1518,8 +1518,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_ATTRREADER: { - rbs_ast_members_attrreader_t *node = (rbs_ast_members_attrreader_t *)any_node; + case RBS_AST_MEMBERS_ATTR_READER: { + rbs_ast_members_attr_reader_t *node = (rbs_ast_members_attr_reader_t *)any_node; if (node->name != NULL) { rbs_node_destroy((rbs_node_t *) node->name); @@ -1543,8 +1543,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_ATTRWRITER: { - rbs_ast_members_attrwriter_t *node = (rbs_ast_members_attrwriter_t *)any_node; + case RBS_AST_MEMBERS_ATTR_WRITER: { + rbs_ast_members_attr_writer_t *node = (rbs_ast_members_attr_writer_t *)any_node; if (node->name != NULL) { rbs_node_destroy((rbs_node_t *) node->name); @@ -1568,8 +1568,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_CLASSINSTANCEVARIABLE: { - rbs_ast_members_classinstancevariable_t *node = (rbs_ast_members_classinstancevariable_t *)any_node; + case RBS_AST_MEMBERS_CLASS_INSTANCE_VARIABLE: { + rbs_ast_members_class_instance_variable_t *node = (rbs_ast_members_class_instance_variable_t *)any_node; if (node->name != NULL) { rbs_node_destroy((rbs_node_t *) node->name); @@ -1583,8 +1583,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_CLASSVARIABLE: { - rbs_ast_members_classvariable_t *node = (rbs_ast_members_classvariable_t *)any_node; + case RBS_AST_MEMBERS_CLASS_VARIABLE: { + rbs_ast_members_class_variable_t *node = (rbs_ast_members_class_variable_t *)any_node; if (node->name != NULL) { rbs_node_destroy((rbs_node_t *) node->name); @@ -1626,8 +1626,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_INSTANCEVARIABLE: { - rbs_ast_members_instancevariable_t *node = (rbs_ast_members_instancevariable_t *)any_node; + case RBS_AST_MEMBERS_INSTANCE_VARIABLE: { + rbs_ast_members_instance_variable_t *node = (rbs_ast_members_instance_variable_t *)any_node; if (node->name != NULL) { rbs_node_destroy((rbs_node_t *) node->name); @@ -1641,8 +1641,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_METHODDEFINITION: { - rbs_ast_members_methoddefinition_t *node = (rbs_ast_members_methoddefinition_t *)any_node; + case RBS_AST_MEMBERS_METHOD_DEFINITION: { + rbs_ast_members_method_definition_t *node = (rbs_ast_members_method_definition_t *)any_node; if (node->name != NULL) { rbs_node_destroy((rbs_node_t *) node->name); @@ -1662,8 +1662,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_METHODDEFINITION_OVERLOAD: { - rbs_ast_members_methoddefinition_overload_t *node = (rbs_ast_members_methoddefinition_overload_t *)any_node; + case RBS_AST_MEMBERS_METHOD_DEFINITION_OVERLOAD: { + rbs_ast_members_method_definition_overload_t *node = (rbs_ast_members_method_definition_overload_t *)any_node; rbs_node_list_free(node->annotations); if (node->method_type != NULL) { @@ -1698,8 +1698,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_TYPEPARAM: { - rbs_ast_typeparam_t *node = (rbs_ast_typeparam_t *)any_node; + case RBS_AST_TYPE_PARAM: { + rbs_ast_type_param_t *node = (rbs_ast_type_param_t *)any_node; if (node->name != NULL) { rbs_node_destroy((rbs_node_t *) node->name); @@ -1717,8 +1717,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_METHODTYPE: { - rbs_methodtype_t *node = (rbs_methodtype_t *)any_node; + case RBS_METHOD_TYPE: { + rbs_method_type_t *node = (rbs_method_type_t *)any_node; rbs_node_list_free(node->type_params); if (node->type != NULL) { @@ -1746,8 +1746,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPENAME: { - rbs_typename_t *node = (rbs_typename_t *)any_node; + case RBS_TYPE_NAME: { + rbs_type_name_t *node = (rbs_type_name_t *)any_node; if (node->rbs_namespace != NULL) { rbs_node_destroy((rbs_node_t *) node->rbs_namespace); @@ -1817,8 +1817,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_CLASSINSTANCE: { - rbs_types_classinstance_t *node = (rbs_types_classinstance_t *)any_node; + case RBS_TYPES_CLASS_INSTANCE: { + rbs_types_class_instance_t *node = (rbs_types_class_instance_t *)any_node; if (node->name != NULL) { rbs_node_destroy((rbs_node_t *) node->name); @@ -1827,8 +1827,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_CLASSSINGLETON: { - rbs_types_classsingleton_t *node = (rbs_types_classsingleton_t *)any_node; + case RBS_TYPES_CLASS_SINGLETON: { + rbs_types_class_singleton_t *node = (rbs_types_class_singleton_t *)any_node; if (node->name != NULL) { rbs_node_destroy((rbs_node_t *) node->name); @@ -1925,8 +1925,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_RECORD_FIELDTYPE: { - rbs_types_record_fieldtype_t *node = (rbs_types_record_fieldtype_t *)any_node; + case RBS_TYPES_RECORD_FIELD_TYPE: { + rbs_types_record_field_type_t *node = (rbs_types_record_field_type_t *)any_node; if (node->type != NULL) { rbs_node_destroy((rbs_node_t *) node->type); @@ -1949,8 +1949,8 @@ void rbs_node_destroy(rbs_node_t *any_node) { break; } #line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_UNTYPEDFUNCTION: { - rbs_types_untypedfunction_t *node = (rbs_types_untypedfunction_t *)any_node; + case RBS_TYPES_UNTYPED_FUNCTION: { + rbs_types_untyped_function_t *node = (rbs_types_untyped_function_t *)any_node; if (node->return_type != NULL) { rbs_node_destroy((rbs_node_t *) node->return_type); diff --git a/src/parser.c b/src/parser.c index 0f2c904b6..13db85627 100644 --- a/src/parser.c +++ b/src/parser.c @@ -155,7 +155,7 @@ static void parser_advance_no_gap(rbs_parser_t *parser) { | {} */ NODISCARD -static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, rbs_range_t *rg, rbs_typename_t **typename) { +static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, rbs_range_t *rg, rbs_type_name_t **type_name) { bool absolute = false; if (rg) { @@ -213,7 +213,7 @@ static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, rbs_range_t rbs_location_t *symbolLoc = rbs_location_current_token(parser); rbs_constant_id_t name = INTERN_TOKEN(parser, parser->current_token); rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, name); - *typename = rbs_typename_new(&parser->allocator, rbs_location_new(&parser->allocator, *rg), namespace, symbol); + *type_name = rbs_type_name_new(&parser->allocator, rbs_location_new(&parser->allocator, *rg), namespace, symbol); return true; } @@ -745,7 +745,7 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse function_range.end = parser->current_token.range.end; rbs_location_t *loc = rbs_location_new(&parser->allocator, function_range); if (rbs_is_untyped_params(&block_params)) { - block_function = (rbs_node_t *) rbs_types_untypedfunction_new(&parser->allocator, loc, block_return_type); + block_function = (rbs_node_t *) rbs_types_untyped_function_new(&parser->allocator, loc, block_return_type); } else { block_function = (rbs_node_t *) rbs_types_function_new( &parser->allocator, @@ -773,7 +773,7 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse function_range.end = parser->current_token.range.end; rbs_location_t *loc = rbs_location_new(&parser->allocator, function_range); if (rbs_is_untyped_params(¶ms)) { - function = (rbs_node_t *) rbs_types_untypedfunction_new(&parser->allocator, loc, type); + function = (rbs_node_t *) rbs_types_untyped_function_new(&parser->allocator, loc, type); } else { function = (rbs_node_t *) rbs_types_function_new( &parser->allocator, @@ -881,7 +881,7 @@ static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields) { field_range.end = parser->current_token.range.end; rbs_location_t *loc = rbs_location_new(&parser->allocator, field_range); - rbs_hash_set(*fields, (rbs_node_t *) key, (rbs_node_t *) rbs_types_record_fieldtype_new(&parser->allocator, loc, type, required)); + rbs_hash_set(*fields, (rbs_node_t *) key, (rbs_node_t *) rbs_types_record_field_type_new(&parser->allocator, loc, type, required)); if (parser_advance_if(parser, pCOMMA)) { if (parser->next_token.type == pRBRACE) { @@ -955,8 +955,8 @@ static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node } rbs_range_t name_range; - rbs_typename_t *typename = NULL; - CHECK_PARSE(parse_type_name(parser, expected_kind, &name_range, &typename)); + rbs_type_name_t *type_name = NULL; + CHECK_PARSE(parse_type_name(parser, expected_kind, &name_range, &type_name)); rbs_node_list_t *types = rbs_node_list_new(&parser->allocator); @@ -994,11 +994,11 @@ static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node rbs_loc_add_optional_child(loc, INTERN("args"), args_range); if (kind == CLASS_NAME) { - *type = (rbs_node_t *) rbs_types_classinstance_new(&parser->allocator, loc, typename, types); + *type = (rbs_node_t *) rbs_types_class_instance_new(&parser->allocator, loc, type_name, types); } else if (kind == INTERFACE_NAME) { - *type = (rbs_node_t *) rbs_types_interface_new(&parser->allocator, loc, typename, types); + *type = (rbs_node_t *) rbs_types_interface_new(&parser->allocator, loc, type_name, types); } else if (kind == ALIAS_NAME) { - *type = (rbs_node_t *) rbs_types_alias_new(&parser->allocator, loc, typename, types); + *type = (rbs_node_t *) rbs_types_alias_new(&parser->allocator, loc, type_name, types); } return true; @@ -1008,7 +1008,7 @@ static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node singleton_type ::= {`singleton`} `(` type_name <`)`> */ NODISCARD -static bool parse_singleton_type(rbs_parser_t *parser, rbs_types_classsingleton_t **singleton) { +static bool parse_singleton_type(rbs_parser_t *parser, rbs_types_class_singleton_t **singleton) { ASSERT_TOKEN(parser, kSINGLETON); rbs_range_t type_range; @@ -1017,8 +1017,8 @@ static bool parse_singleton_type(rbs_parser_t *parser, rbs_types_classsingleton_ rbs_parser_advance(parser); rbs_range_t name_range; - rbs_typename_t *typename = NULL; - CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &name_range, &typename)); + rbs_type_name_t *type_name = NULL; + CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &name_range, &type_name)); ADVANCE_ASSERT(parser, pRPAREN); type_range.end = parser->current_token.range.end; @@ -1027,7 +1027,7 @@ static bool parse_singleton_type(rbs_parser_t *parser, rbs_types_classsingleton_ rbs_loc_alloc_children(&parser->allocator, loc, 1); rbs_loc_add_required_child(loc, INTERN("name"), name_range); - *singleton = rbs_types_classsingleton_new(&parser->allocator, loc, typename); + *singleton = rbs_types_class_singleton_new(&parser->allocator, loc, type_name); return true; } @@ -1186,7 +1186,7 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) { return true; } case kSINGLETON: { - rbs_types_classsingleton_t *singleton = NULL; + rbs_types_class_singleton_t *singleton = NULL; CHECK_PARSE(parse_singleton_type(parser, &singleton)); *type = (rbs_node_t *) singleton; return true; @@ -1397,7 +1397,7 @@ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module rbs_loc_add_optional_child(loc, INTERN("upper_bound"), upper_bound_range); rbs_loc_add_optional_child(loc, INTERN("default"), default_type_range); - rbs_ast_typeparam_t *param = rbs_ast_typeparam_new(&parser->allocator, loc, name, variance, upper_bound, default_type, unchecked); + rbs_ast_type_param_t *param = rbs_ast_type_param_new(&parser->allocator, loc, name, variance, upper_bound, default_type, unchecked); rbs_node_list_append(*params, (rbs_node_t *) param); @@ -1443,7 +1443,7 @@ static bool parser_pop_typevar_table(rbs_parser_t *parser) { method_type ::= {} type_params */ // TODO: Should this be NODISCARD? -bool rbs_parse_method_type(rbs_parser_t *parser, rbs_methodtype_t **method_type) { +bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type) { rbs_parser_push_typevar_table(parser, false); rbs_range_t rg; @@ -1469,7 +1469,7 @@ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_methodtype_t **method_type) rbs_loc_add_required_child(loc, INTERN("type"), type_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range); - *method_type = rbs_methodtype_new(&parser->allocator, loc, type_params, result->function, result->block); + *method_type = rbs_method_type_new(&parser->allocator, loc, type_params, result->function, result->block); return true; } @@ -1486,7 +1486,7 @@ static bool parse_global_decl(rbs_parser_t *parser, rbs_node_list_t *annotations rbs_range_t name_range = parser->current_token.range; rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, name_range); - rbs_ast_symbol_t *typename = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); + rbs_ast_symbol_t *type_name = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); ADVANCE_ASSERT(parser, pCOLON); rbs_range_t colon_range = parser->current_token.range; @@ -1500,7 +1500,7 @@ static bool parse_global_decl(rbs_parser_t *parser, rbs_node_list_t *annotations rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - *global = rbs_ast_declarations_global_new(&parser->allocator, loc, typename, type, comment, annotations); + *global = rbs_ast_declarations_global_new(&parser->allocator, loc, type_name, type, comment, annotations); return true; } @@ -1515,8 +1515,8 @@ static bool parse_const_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, decl_range.start.line); rbs_range_t name_range; - rbs_typename_t *typename = NULL; - CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &name_range, &typename)); + rbs_type_name_t *type_name = NULL; + CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &name_range, &type_name)); ADVANCE_ASSERT(parser, pCOLON); rbs_range_t colon_range = parser->current_token.range; @@ -1531,7 +1531,7 @@ static bool parse_const_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - *constant = rbs_ast_declarations_constant_new(&parser->allocator, loc, typename, type, comment, annotations); + *constant = rbs_ast_declarations_constant_new(&parser->allocator, loc, type_name, type, comment, annotations); return true; } @@ -1539,7 +1539,7 @@ static bool parse_const_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, type_decl ::= {kTYPE} alias_name `=` */ NODISCARD -static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_typealias_t **typealias) { +static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_declarations_type_alias_t **typealias) { rbs_parser_push_typevar_table(parser, true); rbs_range_t decl_range; @@ -1551,8 +1551,8 @@ static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rb rbs_parser_advance(parser); rbs_range_t name_range; - rbs_typename_t *typename = NULL; - CHECK_PARSE(parse_type_name(parser, ALIAS_NAME, &name_range, &typename)); + rbs_type_name_t *type_name = NULL; + CHECK_PARSE(parse_type_name(parser, ALIAS_NAME, &name_range, &type_name)); rbs_range_t params_range; rbs_node_list_t *type_params; @@ -1577,7 +1577,7 @@ static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rb rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line); - *typealias = rbs_ast_declarations_typealias_new(&parser->allocator, loc, typename, type_params, type, annotations, comment); + *typealias = rbs_ast_declarations_type_alias_new(&parser->allocator, loc, type_name, type_params, type, annotations, comment); return true; } @@ -1795,7 +1795,7 @@ static InstanceSingletonKind parse_instance_singleton_kind(rbs_parser_t *parser, * @param accept_overload `true` to accept overloading (...) definition. * */ NODISCARD -static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool accept_overload, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_members_methoddefinition_t **method_definition) { +static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool accept_overload, rbs_position_t comment_pos, rbs_node_list_t *annotations, rbs_ast_members_method_definition_t **method_definition) { rbs_range_t member_range; member_range.start = parser->current_token.range.start; comment_pos = rbs_nonnull_pos_or(comment_pos, member_range.start); @@ -1874,12 +1874,12 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce case pLBRACKET: case pQUESTION: { - rbs_methodtype_t *method_type = NULL; + rbs_method_type_t *method_type = NULL; CHECK_PARSE(rbs_parse_method_type(parser, &method_type)); overload_range.end = parser->current_token.range.end; rbs_location_t *loc = rbs_location_new(&parser->allocator, overload_range); - rbs_node_t *overload = (rbs_node_t *) rbs_ast_members_methoddefinition_overload_new(&parser->allocator, loc, annotations, (rbs_node_t *) method_type); + rbs_node_t *overload = (rbs_node_t *) rbs_ast_members_method_definition_overload_new(&parser->allocator, loc, annotations, (rbs_node_t *) method_type); rbs_node_list_append(overloads, overload); member_range.end = parser->current_token.range.end; break; @@ -1939,7 +1939,7 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce rbs_loc_add_optional_child(loc, INTERN("overloading"), overloading_range); rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range); - *method_definition = rbs_ast_members_methoddefinition_new(&parser->allocator, loc, name, k, overloads, annotations, comment, overloading, visibility); + *method_definition = rbs_ast_members_method_definition_new(&parser->allocator, loc, name, k, overloads, annotations, comment, overloading, visibility); return true; } @@ -1950,12 +1950,12 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce * @param kind * */ NODISCARD -static bool class_instance_name(rbs_parser_t *parser, TypeNameKind kind, rbs_node_list_t *args, rbs_range_t *name_range, rbs_range_t *args_range, rbs_typename_t **name) { +static bool class_instance_name(rbs_parser_t *parser, TypeNameKind kind, rbs_node_list_t *args, rbs_range_t *name_range, rbs_range_t *args_range, rbs_type_name_t **name) { rbs_parser_advance(parser); - rbs_typename_t *typename = NULL; - CHECK_PARSE(parse_type_name(parser, kind, name_range, &typename)); - *name = typename; + rbs_type_name_t *type_name = NULL; + CHECK_PARSE(parse_type_name(parser, kind, name_range, &type_name)); + *name = type_name; if (parser->next_token.type == pLBRACKET) { rbs_parser_advance(parser); @@ -2015,7 +2015,7 @@ static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, rbs_po rbs_node_list_t *args = rbs_node_list_new(&parser->allocator); rbs_range_t name_range; rbs_range_t args_range = NULL_RANGE; - rbs_typename_t *name = NULL; + rbs_type_name_t *name = NULL; CHECK_PARSE(class_instance_name( parser, from_interface ? INTERFACE_NAME : (INTERFACE_NAME | CLASS_NAME), @@ -2143,7 +2143,7 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); - *variable_member = (rbs_node_t *)rbs_ast_members_instancevariable_new(&parser->allocator, loc, name, type, comment); + *variable_member = (rbs_node_t *)rbs_ast_members_instance_variable_new(&parser->allocator, loc, name, type, comment); return true; } case tA2IDENT: { @@ -2169,7 +2169,7 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); - *variable_member = (rbs_node_t *) rbs_ast_members_classvariable_new(&parser->allocator, loc, name, type, comment); + *variable_member = (rbs_node_t *) rbs_ast_members_class_variable_new(&parser->allocator, loc, name, type, comment); return true; } case kSELF: { @@ -2203,7 +2203,7 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range); - *variable_member = (rbs_node_t *)rbs_ast_members_classinstancevariable_new(&parser->allocator, loc, name, type, comment); + *variable_member = (rbs_node_t *)rbs_ast_members_class_instance_variable_new(&parser->allocator, loc, name, type, comment); return true; } default: @@ -2353,13 +2353,13 @@ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_ switch (attr_type) { case kATTRREADER: - *attribute_member = (rbs_node_t *) rbs_ast_members_attrreader_new(&parser->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); + *attribute_member = (rbs_node_t *) rbs_ast_members_attr_reader_new(&parser->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); return true; case kATTRWRITER: - *attribute_member = (rbs_node_t *) rbs_ast_members_attrwriter_new(&parser->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); + *attribute_member = (rbs_node_t *) rbs_ast_members_attr_writer_new(&parser->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); return true; case kATTRACCESSOR: - *attribute_member = (rbs_node_t *) rbs_ast_members_attraccessor_new(&parser->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); + *attribute_member = (rbs_node_t *) rbs_ast_members_attr_accessor_new(&parser->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); return true; default: rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error"); @@ -2388,7 +2388,7 @@ static bool parse_interface_members(rbs_parser_t *parser, rbs_node_list_t **memb rbs_node_t *member; switch (parser->current_token.type) { case kDEF: { - rbs_ast_members_methoddefinition_t *method_definition = NULL; + rbs_ast_members_method_definition_t *method_definition = NULL; CHECK_PARSE(parse_member_def(parser, true, true, annot_pos, annotations, &method_definition)); member = (rbs_node_t *) method_definition; break; @@ -2435,7 +2435,7 @@ static bool parse_interface_decl(rbs_parser_t *parser, rbs_position_t comment_po rbs_parser_advance(parser); rbs_range_t name_range; - rbs_typename_t *name = NULL; + rbs_type_name_t *name = NULL; CHECK_PARSE(parse_type_name(parser, INTERFACE_NAME, &name_range, &name)); rbs_range_t type_params_range; @@ -2479,7 +2479,7 @@ static bool parse_module_self_types(rbs_parser_t *parser, rbs_node_list_t *array self_range.start = parser->current_token.range.start; rbs_range_t name_range; - rbs_typename_t *module_name = NULL; + rbs_type_name_t *module_name = NULL; CHECK_PARSE(parse_type_name(parser, CLASS_NAME | INTERFACE_NAME, &name_range, &module_name)); self_range.end = name_range.end; @@ -2540,7 +2540,7 @@ static bool parse_module_members(rbs_parser_t *parser, rbs_node_list_t **members switch (parser->current_token.type) { case kDEF: { - rbs_ast_members_methoddefinition_t *method_definition; + rbs_ast_members_method_definition_t *method_definition; CHECK_PARSE(parse_member_def(parser, false, true, annot_pos, annotations, &method_definition)); member = (rbs_node_t *) method_definition; break; @@ -2578,7 +2578,7 @@ static bool parse_module_members(rbs_parser_t *parser, rbs_node_list_t **members switch (parser->next_token.type) { case kDEF: { - rbs_ast_members_methoddefinition_t *method_definition = NULL; + rbs_ast_members_method_definition_t *method_definition = NULL; CHECK_PARSE(parse_member_def(parser, false, true, annot_pos, annotations, &method_definition)); member = (rbs_node_t *) method_definition; break; @@ -2614,7 +2614,7 @@ static bool parse_module_members(rbs_parser_t *parser, rbs_node_list_t **members | {module_name} module_name module_type_params `:` module_self_types module_members */ NODISCARD -static bool parse_module_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_typename_t *module_name, rbs_range_t name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_module_t **module_decl) { +static bool parse_module_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_type_name_t *module_name, rbs_range_t name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_module_t **module_decl) { rbs_parser_push_typevar_table(parser, true); rbs_range_t decl_range; @@ -2675,7 +2675,7 @@ static bool parse_module_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_parser_advance(parser); rbs_range_t module_name_range; - rbs_typename_t *module_name; + rbs_type_name_t *module_name; CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &module_name_range, &module_name)); if (parser->next_token.type == pEQ) { @@ -2684,7 +2684,7 @@ static bool parse_module_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_parser_advance(parser); rbs_range_t old_name_range; - rbs_typename_t *old_name = NULL; + rbs_type_name_t *old_name = NULL; CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &old_name_range, &old_name)); rbs_range_t decl_range = { @@ -2699,7 +2699,7 @@ static bool parse_module_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); - *module_decl = (rbs_node_t *) rbs_ast_declarations_modulealias_new(&parser->allocator, loc, module_name, old_name, comment, annotations); + *module_decl = (rbs_node_t *) rbs_ast_declarations_module_alias_new(&parser->allocator, loc, module_name, old_name, comment, annotations); } else { rbs_ast_declarations_module_t *module_decl0 = NULL; CHECK_PARSE(parse_module_decl0(parser, keyword_range, module_name, module_name_range, comment, annotations, &module_decl0)); @@ -2722,7 +2722,7 @@ static bool parse_class_decl_super(rbs_parser_t *parser, rbs_range_t *lt_range, super_range.start = parser->next_token.range.start; rbs_node_list_t *args = rbs_node_list_new(&parser->allocator); - rbs_typename_t *name = NULL; + rbs_type_name_t *name = NULL; rbs_range_t name_range, args_range; CHECK_PARSE(class_instance_name(parser, CLASS_NAME, args, &name_range, &args_range, &name)); @@ -2746,7 +2746,7 @@ static bool parse_class_decl_super(rbs_parser_t *parser, rbs_range_t *lt_range, class_decl ::= {class_name} type_params class_decl_super class_members <`end`> */ NODISCARD -static bool parse_class_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_typename_t *name, rbs_range_t name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_class_t **class_decl) { +static bool parse_class_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_type_name_t *name, rbs_range_t name_range, rbs_ast_comment_t *comment, rbs_node_list_t *annotations, rbs_ast_declarations_class_t **class_decl) { rbs_parser_push_typevar_table(parser, true); rbs_range_t decl_range; @@ -2796,7 +2796,7 @@ static bool parse_class_decl(rbs_parser_t *parser, rbs_position_t comment_pos, r rbs_parser_advance(parser); rbs_range_t class_name_range; - rbs_typename_t *class_name = NULL; + rbs_type_name_t *class_name = NULL; CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &class_name_range, &class_name)); if (parser->next_token.type == pEQ) { @@ -2805,7 +2805,7 @@ static bool parse_class_decl(rbs_parser_t *parser, rbs_position_t comment_pos, r rbs_parser_advance(parser); rbs_range_t old_name_range; - rbs_typename_t *old_name = NULL; + rbs_type_name_t *old_name = NULL; CHECK_PARSE(parse_type_name(parser, CLASS_NAME, &old_name_range, &old_name)); rbs_range_t decl_range = { @@ -2820,7 +2820,7 @@ static bool parse_class_decl(rbs_parser_t *parser, rbs_position_t comment_pos, r rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); - *class_decl = (rbs_node_t *) rbs_ast_declarations_classalias_new(&parser->allocator, loc, class_name, old_name, comment, annotations); + *class_decl = (rbs_node_t *) rbs_ast_declarations_class_alias_new(&parser->allocator, loc, class_name, old_name, comment, annotations); } else { rbs_ast_declarations_class_t *class_decl0 = NULL; CHECK_PARSE(parse_class_decl0(parser, keyword_range, class_name, class_name_range, comment, annotations, &class_decl0)); @@ -2856,7 +2856,7 @@ static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, rbs_p break; } case kTYPE: { - rbs_ast_declarations_typealias_t *typealias = NULL; + rbs_ast_declarations_type_alias_t *typealias = NULL; CHECK_PARSE(parse_type_decl(parser, annot_pos, annotations, &typealias)); *decl = (rbs_node_t *) typealias; break; @@ -2912,7 +2912,7 @@ static bool parse_decl(rbs_parser_t *parser, rbs_node_t **decl) { return true; } case kTYPE: { - rbs_ast_declarations_typealias_t *typealias = NULL; + rbs_ast_declarations_type_alias_t *typealias = NULL; CHECK_PARSE(parse_type_decl(parser, annot_pos, annotations, &typealias)); *decl = (rbs_node_t *) typealias; return true; @@ -3010,7 +3010,7 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) { rbs_location_t *symbolLoc = rbs_location_current_token(parser); rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); - rbs_typename_t *type_name = rbs_typename_new(&parser->allocator, rbs_location_new(&parser->allocator, type_name_range),namespace, symbol); + rbs_type_name_t *type_name = rbs_type_name_new(&parser->allocator, rbs_location_new(&parser->allocator, type_name_range),namespace, symbol); rbs_range_t keyword_range = NULL_RANGE; rbs_range_t new_name_range = NULL_RANGE; @@ -3036,7 +3036,7 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) { rbs_loc_add_optional_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_optional_child(loc, INTERN("new_name"), new_name_range); - rbs_ast_directives_use_singleclause_t *clause = rbs_ast_directives_use_singleclause_new(&parser->allocator, loc, type_name, new_name); + rbs_ast_directives_use_single_clause_t *clause = rbs_ast_directives_use_single_clause_new(&parser->allocator, loc, type_name, new_name); rbs_node_list_append(clauses, (rbs_node_t *)clause); break; @@ -3054,7 +3054,7 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) { rbs_loc_add_required_child(loc, INTERN("namespace"), namespace_range); rbs_loc_add_required_child(loc, INTERN("star"), star_range); - rbs_ast_directives_use_wildcardclause_t *clause = rbs_ast_directives_use_wildcardclause_new(&parser->allocator, loc, namespace); + rbs_ast_directives_use_wildcard_clause_t *clause = rbs_ast_directives_use_wildcard_clause_new(&parser->allocator, loc, namespace); rbs_node_list_append(clauses, (rbs_node_t *)clause); break; diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index 6bfad7f34..94aba5513 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -83,7 +83,7 @@ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t ctx, rbs_node_t *instan return rb_enc_str_new(s.start, rbs_string_len(s), rb_utf8_encoding()); <%- when "RBS::Types::Record::FieldType" -%> - rbs_types_record_fieldtype_t *record_fieldtype = (rbs_types_record_fieldtype_t *) instance; + rbs_types_record_field_type_t *record_fieldtype = (rbs_types_record_field_type_t *) instance; VALUE array = rb_ary_new(); rb_ary_push(array, rbs_struct_to_ruby_value(ctx, record_fieldtype->type)); diff --git a/templates/template.rb b/templates/template.rb index 35425a531..0b1b06215 100644 --- a/templates/template.rb +++ b/templates/template.rb @@ -47,7 +47,7 @@ def stored_field_decl def ast_node? @c_type == "rbs_node" || - @c_type == "rbs_typename" || + @c_type == "rbs_type_name" || @c_type == "rbs_namespace" || @c_type.include?("_ast_") || @c_type.include?("_decl_") || @@ -69,11 +69,11 @@ class Type attr_reader :ruby_class_name #: String # The base name of the auto-generated C struct for this type. - # e.g. `rbs_ast_declarations_typealias` + # e.g. `rbs_ast_declarations_type_alias` attr_reader :c_base_name #: String # The name of the typedef of the auto-generated C struct for this type, - # e.g. `rbs_ast_declarations_typealias_t` + # e.g. `rbs_ast_declarations_type_alias_t` attr_reader :c_type_name #: String # The name of the C constant which stores the Ruby VALUE pointing to the generated class. @@ -92,10 +92,13 @@ class Type def initialize(yaml) @ruby_full_name = yaml["name"] @ruby_class_name = @ruby_full_name[/[^:]+\z/] # demodulize-like + + @c_base_name = @ruby_full_name.split("::").map { |part| camel_to_snake(part) }.join("_") + @c_type_name = @c_base_name + "_t" + + # For compatibility with existing code, use the original approach for constant naming @c_constant_name = @ruby_full_name.gsub("::", "_") @c_parent_constant_name = @ruby_full_name.split("::")[0..-2].join("::").gsub("::", "_") - @c_base_name = @c_constant_name.downcase - @c_type_name = @c_base_name + "_t" @c_type_enum_name = @c_base_name.upcase @@ -113,7 +116,7 @@ def initialize(yaml) end # The name of the C function which constructs new instances of this C structure. - # e.g. `rbs_ast_declarations_typealias_new` + # e.g. `rbs_ast_declarations_type_alias_new` def c_constructor_function_name #: String "#{@c_base_name}_new" end @@ -131,6 +134,13 @@ def expose_location? def has_children_to_free? @fields.any?(&:needs_to_be_freed?) end + + # Convert CamelCase to snake_case + # e.g. "FooBarBaz" -> "foo_bar_baz" + def camel_to_snake(str) + str.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase + end + end class << self From 5c7885e11737cec71f3dfc86c7c7e2bbed6094f6 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 2 Apr 2025 00:32:25 +0800 Subject: [PATCH 101/111] Move more internal components into `util` folder (#33) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Assuming the top-level `include/rbs` holds public components (e.g. `parser.h`), and `include/rbs/util` holds internal components, more stuff should be moved under `util`. This follows the same convention `prism` uses. ### Before ``` include ├── rbs │ ├── ast.h │ ├── defines.h │ ├── encoding.h │ ├── lexer.h │ ├── parser.h │ ├── rbs_buffer.h │ ├── rbs_encoding.h │ ├── rbs_location.h │ ├── rbs_location_internals.h │ ├── rbs_string.h │ ├── rbs_strncasecmp.h │ ├── rbs_unescape.h │ └── util │ ├── rbs_allocator.h │ ├── rbs_assert.h │ └── rbs_constant_pool.h └── rbs.h ``` ### After ``` include ├── rbs │ ├── ast.h │ ├── defines.h │ ├── lexer.h │ ├── parser.h │ ├── rbs_location.h │ ├── rbs_string.h │ └── util │ ├── rbs_allocator.h │ ├── rbs_assert.h │ ├── rbs_buffer.h │ ├── rbs_constant_pool.h │ ├── rbs_encoding.h │ └── rbs_unescape.h └── rbs.h ``` --- ext/rbs_extension/ast_translation.h | 1 - include/rbs/encoding.h | 9 --- include/rbs/lexer.h | 2 +- include/rbs/rbs_location.h | 22 ++++++- include/rbs/rbs_location_internals.h | 27 --------- include/rbs/rbs_string.h | 2 + include/rbs/rbs_strncasecmp.h | 30 ---------- include/rbs/{ => util}/rbs_buffer.h | 4 +- include/rbs/util/rbs_constant_pool.h | 2 +- include/rbs/{ => util}/rbs_encoding.h | 2 - include/rbs/{ => util}/rbs_unescape.h | 0 src/encoding.c | 57 ------------------- src/lexstate.c | 3 +- src/parser.c | 9 ++- src/rbs_string.c | 48 ++++++++++++++++ src/rbs_strncasecmp.c | 24 -------- src/{ => util}/rbs_buffer.c | 2 +- src/{ => util}/rbs_encoding.c | 33 ++++++++++- src/{ => util}/rbs_unescape.c | 13 ++++- .../ext/rbs_extension/ast_translation.h.erb | 1 - 20 files changed, 122 insertions(+), 169 deletions(-) delete mode 100644 include/rbs/encoding.h delete mode 100644 include/rbs/rbs_location_internals.h delete mode 100644 include/rbs/rbs_strncasecmp.h rename include/rbs/{ => util}/rbs_buffer.h (98%) rename include/rbs/{ => util}/rbs_encoding.h (99%) rename include/rbs/{ => util}/rbs_unescape.h (100%) delete mode 100644 src/encoding.c delete mode 100644 src/rbs_strncasecmp.c rename src/{ => util}/rbs_buffer.c (98%) rename src/{ => util}/rbs_encoding.c (99%) rename src/{ => util}/rbs_unescape.c (92%) diff --git a/ext/rbs_extension/ast_translation.h b/ext/rbs_extension/ast_translation.h index c016b8994..80622887c 100644 --- a/ext/rbs_extension/ast_translation.h +++ b/ext/rbs_extension/ast_translation.h @@ -12,7 +12,6 @@ #include "ruby/encoding.h" #include "rbs/ast.h" -#include "rbs/rbs_encoding.h" #include "rbs/rbs_location.h" /// A bag of values needed when copying RBS C structs into Ruby objects. diff --git a/include/rbs/encoding.h b/include/rbs/encoding.h deleted file mode 100644 index c53e4ed62..000000000 --- a/include/rbs/encoding.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef RBS_ENCODING_H -#define RBS_ENCODING_H - -#include "rbs_string.h" - -unsigned int rbs_utf8_to_codepoint(const rbs_string_t string); -int rbs_utf8_codelen(unsigned int c); - -#endif // RBS_ENCODING_H diff --git a/include/rbs/lexer.h b/include/rbs/lexer.h index d1c9b9f0c..53cea5150 100644 --- a/include/rbs/lexer.h +++ b/include/rbs/lexer.h @@ -2,7 +2,7 @@ #define RBS__LEXER_H #include "rbs_string.h" -#include "rbs_encoding.h" +#include "util/rbs_encoding.h" enum RBSTokenType { NullType, /* (Nothing) */ diff --git a/include/rbs/rbs_location.h b/include/rbs/rbs_location.h index cd925abb8..c2d825e8a 100644 --- a/include/rbs/rbs_location.h +++ b/include/rbs/rbs_location.h @@ -5,7 +5,27 @@ #include "rbs/util/rbs_constant_pool.h" #include "rbs/util/rbs_allocator.h" -#include "rbs/rbs_location_internals.h" + +typedef struct { + int start; + int end; +} rbs_loc_range; + +typedef struct { + rbs_constant_id_t name; + rbs_loc_range rg; +} rbs_loc_entry; + +typedef unsigned int rbs_loc_entry_bitmap; + +// The flexible array always allocates, but it's okay. +// This struct is not allocated when the `rbs_loc` doesn't have children. +typedef struct { + unsigned short len; + unsigned short cap; + rbs_loc_entry_bitmap required_p; + rbs_loc_entry entries[1]; +} rbs_loc_children; typedef struct rbs_location { rbs_range_t rg; diff --git a/include/rbs/rbs_location_internals.h b/include/rbs/rbs_location_internals.h deleted file mode 100644 index 43fa9282c..000000000 --- a/include/rbs/rbs_location_internals.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef RBS__RBS_LOCATION_INTERNALS_H -#define RBS__RBS_LOCATION_INTERNALS_H - -#include "rbs/util/rbs_constant_pool.h" - -typedef struct { - int start; - int end; -} rbs_loc_range; - -typedef struct { - rbs_constant_id_t name; - rbs_loc_range rg; -} rbs_loc_entry; - -typedef unsigned int rbs_loc_entry_bitmap; - -// The flexible array always allocates, but it's okay. -// This struct is not allocated when the `rbs_loc` doesn't have children. -typedef struct { - unsigned short len; - unsigned short cap; - rbs_loc_entry_bitmap required_p; - rbs_loc_entry entries[1]; -} rbs_loc_children; - -#endif diff --git a/include/rbs/rbs_string.h b/include/rbs/rbs_string.h index 8db0f96b1..98996a918 100644 --- a/include/rbs/rbs_string.h +++ b/include/rbs/rbs_string.h @@ -44,4 +44,6 @@ size_t rbs_string_len(const rbs_string_t self); */ bool rbs_string_equal(const rbs_string_t lhs, const rbs_string_t rhs); +unsigned int rbs_utf8_string_to_codepoint(const rbs_string_t string); + #endif diff --git a/include/rbs/rbs_strncasecmp.h b/include/rbs/rbs_strncasecmp.h deleted file mode 100644 index 87d99703f..000000000 --- a/include/rbs/rbs_strncasecmp.h +++ /dev/null @@ -1,30 +0,0 @@ -/** - * @file rbs_strncasecmp.h - * - * A custom strncasecmp implementation. - */ -#ifndef RBS_STRNCASECMP_H -#define RBS_STRNCASECMP_H - -#include -#include -#include - -/** - * Compare two strings, ignoring case, up to the given length. Returns 0 if the - * strings are equal, a negative number if string1 is less than string2, or a - * positive number if string1 is greater than string2. - * - * Note that this is effectively our own implementation of strncasecmp, but it's - * not available on all of the platforms we want to support so we're rolling it - * here. - * - * @param string1 The first string to compare. - * @param string2 The second string to compare - * @param length The maximum number of characters to compare. - * @return 0 if the strings are equal, a negative number if string1 is less than - * string2, or a positive number if string1 is greater than string2. - */ -int rbs_strncasecmp(const uint8_t *string1, const uint8_t *string2, size_t length); - -#endif diff --git a/include/rbs/rbs_buffer.h b/include/rbs/util/rbs_buffer.h similarity index 98% rename from include/rbs/rbs_buffer.h rename to include/rbs/util/rbs_buffer.h index bcbcb1986..3e8d9d7a4 100644 --- a/include/rbs/rbs_buffer.h +++ b/include/rbs/util/rbs_buffer.h @@ -1,15 +1,13 @@ #ifndef RBS__RBS_BUFFER_H #define RBS__RBS_BUFFER_H -#include "rbs_string.h" #include "rbs/util/rbs_allocator.h" +#include "rbs/rbs_string.h" #include #include #include - - /** * The default capacity of a rbs_buffer_t. * If the buffer needs to grow beyond this capacity, it will be doubled. diff --git a/include/rbs/util/rbs_constant_pool.h b/include/rbs/util/rbs_constant_pool.h index 7d02f2b21..7fce00223 100644 --- a/include/rbs/util/rbs_constant_pool.h +++ b/include/rbs/util/rbs_constant_pool.h @@ -10,7 +10,7 @@ #ifndef RBS_CONSTANT_POOL_H #define RBS_CONSTANT_POOL_H -#include "rbs/rbs_encoding.h" +#include "rbs/util/rbs_encoding.h" #include #include diff --git a/include/rbs/rbs_encoding.h b/include/rbs/util/rbs_encoding.h similarity index 99% rename from include/rbs/rbs_encoding.h rename to include/rbs/util/rbs_encoding.h index b0c294c3d..0c2a171bb 100644 --- a/include/rbs/rbs_encoding.h +++ b/include/rbs/util/rbs_encoding.h @@ -6,8 +6,6 @@ #ifndef RBS_RBS_ENCODING_H #define RBS_RBS_ENCODING_H -#include "rbs_strncasecmp.h" - #include #include #include diff --git a/include/rbs/rbs_unescape.h b/include/rbs/util/rbs_unescape.h similarity index 100% rename from include/rbs/rbs_unescape.h rename to include/rbs/util/rbs_unescape.h diff --git a/src/encoding.c b/src/encoding.c deleted file mode 100644 index 6d47ce2f7..000000000 --- a/src/encoding.c +++ /dev/null @@ -1,57 +0,0 @@ -#include "rbs/encoding.h" - -unsigned int rbs_utf8_to_codepoint(const rbs_string_t string) { - unsigned int codepoint = 0; - int remaining_bytes = 0; - - const char *s = string.start; - const char *end = string.end; - - if (s >= end) return 0; // End of string - - if ((*s & 0x80) == 0) { - // Single byte character (0xxxxxxx) - return *s; - } else if ((*s & 0xE0) == 0xC0) { - // Two byte character (110xxxxx 10xxxxxx) - codepoint = *s & 0x1F; - remaining_bytes = 1; - } else if ((*s & 0xF0) == 0xE0) { - // Three byte character (1110xxxx 10xxxxxx 10xxxxxx) - codepoint = *s & 0x0F; - remaining_bytes = 2; - } else if ((*s & 0xF8) == 0xF0) { - // Four byte character (11110xxx 10xxxxxx 10xxxxxx 10xxxxxx) - codepoint = *s & 0x07; - remaining_bytes = 3; - } else { - // Invalid UTF-8 sequence - return 0xFFFD; // Unicode replacement character - } - - s++; - while (remaining_bytes > 0 && s < end) { - if ((*s & 0xC0) != 0x80) { - // Invalid continuation byte - return 0xFFFD; - } - codepoint = (codepoint << 6) | (*s & 0x3F); - s++; - remaining_bytes--; - } - - if (remaining_bytes > 0) { - // Incomplete sequence - return 0xFFFD; - } - - return codepoint; -} - -int rbs_utf8_codelen(unsigned int c) { - if (c <= 0x7F) return 1; - if (c <= 0x7FF) return 2; - if (c <= 0xFFFF) return 3; - if (c <= 0x10FFFF) return 4; - return 1; // Invalid Unicode codepoint, treat as 1 byte -} diff --git a/src/lexstate.c b/src/lexstate.c index 18be9c6be..5827dab71 100644 --- a/src/lexstate.c +++ b/src/lexstate.c @@ -1,5 +1,4 @@ #include "rbs/lexer.h" -#include "rbs/encoding.h" static const char *RBS_TOKENTYPE_NAMES[] = { "NullType", @@ -114,7 +113,7 @@ unsigned int rbs_peek(lexstate *state) { state->string.start + state->current.byte_pos, state->string.end ); - unsigned int c = rbs_utf8_to_codepoint(str); + unsigned int c = rbs_utf8_string_to_codepoint(str); state->last_char = c; return c; } diff --git a/src/parser.c b/src/parser.c index 13db85627..e8fad0c5a 100644 --- a/src/parser.c +++ b/src/parser.c @@ -7,10 +7,9 @@ #include #include "rbs/defines.h" -#include "rbs/encoding.h" #include "rbs/rbs_string.h" -#include "rbs/rbs_buffer.h" -#include "rbs/rbs_unescape.h" +#include "rbs/util/rbs_unescape.h" +#include "rbs/util/rbs_buffer.h" #include "rbs/util/rbs_assert.h" #define INTERN(str) \ @@ -1596,7 +1595,7 @@ static bool parse_annotation(rbs_parser_t *parser, rbs_ast_annotation_t **annota parser->lexstate->string.start + rg.start.byte_pos + offset_bytes, parser->lexstate->string.end ); - unsigned int open_char = rbs_utf8_to_codepoint(str); + unsigned int open_char = rbs_utf8_string_to_codepoint(str); unsigned int close_char; @@ -3117,7 +3116,7 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_ comment_start, parser->lexstate->string.end ); - unsigned char c = rbs_utf8_to_codepoint(str); + unsigned char c = rbs_utf8_string_to_codepoint(str); if (c == ' ') { comment_start += space_bytes; diff --git a/src/rbs_string.c b/src/rbs_string.c index 178a581e3..6f731e8b7 100644 --- a/src/rbs_string.c +++ b/src/rbs_string.c @@ -5,6 +5,54 @@ #include #include +unsigned int rbs_utf8_string_to_codepoint(const rbs_string_t string) { + unsigned int codepoint = 0; + int remaining_bytes = 0; + + const char *s = string.start; + const char *end = string.end; + + if (s >= end) return 0; // End of string + + if ((*s & 0x80) == 0) { + // Single byte character (0xxxxxxx) + return *s; + } else if ((*s & 0xE0) == 0xC0) { + // Two byte character (110xxxxx 10xxxxxx) + codepoint = *s & 0x1F; + remaining_bytes = 1; + } else if ((*s & 0xF0) == 0xE0) { + // Three byte character (1110xxxx 10xxxxxx 10xxxxxx) + codepoint = *s & 0x0F; + remaining_bytes = 2; + } else if ((*s & 0xF8) == 0xF0) { + // Four byte character (11110xxx 10xxxxxx 10xxxxxx 10xxxxxx) + codepoint = *s & 0x07; + remaining_bytes = 3; + } else { + // Invalid UTF-8 sequence + return 0xFFFD; // Unicode replacement character + } + + s++; + while (remaining_bytes > 0 && s < end) { + if ((*s & 0xC0) != 0x80) { + // Invalid continuation byte + return 0xFFFD; + } + codepoint = (codepoint << 6) | (*s & 0x3F); + s++; + remaining_bytes--; + } + + if (remaining_bytes > 0) { + // Incomplete sequence + return 0xFFFD; + } + + return codepoint; +} + rbs_string_t rbs_string_new(const char *start, const char *end) { return (rbs_string_t) { .start = start, diff --git a/src/rbs_strncasecmp.c b/src/rbs_strncasecmp.c deleted file mode 100644 index e46a7c43a..000000000 --- a/src/rbs_strncasecmp.c +++ /dev/null @@ -1,24 +0,0 @@ -#include "rbs/rbs_strncasecmp.h" - -/** - * Compare two strings, ignoring case, up to the given length. Returns 0 if the - * strings are equal, a negative number if string1 is less than string2, or a - * positive number if string1 is greater than string2. - * - * Note that this is effectively our own implementation of strncasecmp, but it's - * not available on all of the platforms we want to support so we're rolling it - * here. - */ -int -rbs_strncasecmp(const uint8_t *string1, const uint8_t *string2, size_t length) { - size_t offset = 0; - int difference = 0; - - while (offset < length && string1[offset] != '\0') { - if (string2[offset] == '\0') return string1[offset]; - if ((difference = tolower(string1[offset]) - tolower(string2[offset])) != 0) return difference; - offset++; - } - - return difference; -} diff --git a/src/rbs_buffer.c b/src/util/rbs_buffer.c similarity index 98% rename from src/rbs_buffer.c rename to src/util/rbs_buffer.c index 7803cafb2..71421b975 100644 --- a/src/rbs_buffer.c +++ b/src/util/rbs_buffer.c @@ -1,4 +1,4 @@ -#include "rbs/rbs_buffer.h" +#include "rbs/util/rbs_buffer.h" #include "rbs/util/rbs_assert.h" bool rbs_buffer_init(rbs_allocator_t *allocator, rbs_buffer_t *buffer) { diff --git a/src/rbs_encoding.c b/src/util/rbs_encoding.c similarity index 99% rename from src/rbs_encoding.c rename to src/util/rbs_encoding.c index 181d5897c..27f1a97b4 100644 --- a/src/rbs_encoding.c +++ b/src/util/rbs_encoding.c @@ -1,6 +1,8 @@ -#include "rbs/rbs_encoding.h" +#include "rbs/util/rbs_encoding.h" #include "rbs/util/rbs_assert.h" +#include + #if defined(__GNUC__) # define RBS_ATTRIBUTE_UNUSED __attribute__((unused)) #else @@ -5025,6 +5027,35 @@ const rbs_encoding_t rbs_encodings[] = { #endif }; +/** + * Compare two strings, ignoring case, up to the given length. Returns 0 if the + * strings are equal, a negative number if string1 is less than string2, or a + * positive number if string1 is greater than string2. + * + * Note that this is effectively our own implementation of strncasecmp, but it's + * not available on all of the platforms we want to support so we're rolling it + * here. + * + * @param string1 The first string to compare. + * @param string2 The second string to compare + * @param length The maximum number of characters to compare. + * @return 0 if the strings are equal, a negative number if string1 is less than + * string2, or a positive number if string1 is greater than string2. + */ +static int +rbs_strncasecmp(const uint8_t *string1, const uint8_t *string2, size_t length) { + size_t offset = 0; + int difference = 0; + + while (offset < length && string1[offset] != '\0') { + if (string2[offset] == '\0') return string1[offset]; + if ((difference = tolower(string1[offset]) - tolower(string2[offset])) != 0) return difference; + offset++; + } + + return difference; +} + /** * Parse the given name of an encoding and return a pointer to the corresponding * encoding struct if one can be found, otherwise return NULL. diff --git a/src/rbs_unescape.c b/src/util/rbs_unescape.c similarity index 92% rename from src/rbs_unescape.c rename to src/util/rbs_unescape.c index 59a4ef798..ae2cdedb2 100644 --- a/src/rbs_unescape.c +++ b/src/util/rbs_unescape.c @@ -1,5 +1,4 @@ -#include "rbs/encoding.h" -#include "rbs/rbs_unescape.h" +#include "rbs/util/rbs_unescape.h" #include #include #include @@ -43,6 +42,14 @@ static int octal_to_int(const char* octal, int length) { return result; } +int rbs_utf8_codelen(unsigned int c) { + if (c <= 0x7F) return 1; + if (c <= 0x7FF) return 2; + if (c <= 0xFFFF) return 3; + if (c <= 0x10FFFF) return 4; + return 1; // Invalid Unicode codepoint, treat as 1 byte +} + rbs_string_t unescape_string(rbs_allocator_t *allocator, const rbs_string_t string, bool is_double_quote) { if (!string.start) return RBS_STRING_NULL; @@ -107,7 +114,7 @@ rbs_string_t unescape_string(rbs_allocator_t *allocator, const rbs_string_t stri } rbs_string_t rbs_unquote_string(rbs_allocator_t *allocator, rbs_string_t input) { - unsigned int first_char = rbs_utf8_to_codepoint(input); + unsigned int first_char = rbs_utf8_string_to_codepoint(input); size_t byte_length = rbs_string_len(input); ptrdiff_t start_offset = 0; diff --git a/templates/ext/rbs_extension/ast_translation.h.erb b/templates/ext/rbs_extension/ast_translation.h.erb index 42812ab58..125d6b0b6 100644 --- a/templates/ext/rbs_extension/ast_translation.h.erb +++ b/templates/ext/rbs_extension/ast_translation.h.erb @@ -5,7 +5,6 @@ #include "ruby/encoding.h" #include "rbs/ast.h" -#include "rbs/rbs_encoding.h" #include "rbs/rbs_location.h" /// A bag of values needed when copying RBS C structs into Ruby objects. From 496bfefc7ce371acba63fa0fa44ca3509cc3567b Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 2 Apr 2025 02:48:38 +0800 Subject: [PATCH 102/111] Rename `rbs_location.*` and `rbs_string.*` to `location.*` and `string.*` (#34) This aligns with the naming convention of Prism: - Private headers are prefixed with `rbs_` and placed under `include/rbs/util/`. - Public headers are placed under `include/rbs/` without the `rbs_` prefix. --- ext/rbs_extension/ast_translation.c | 2 +- ext/rbs_extension/ast_translation.h | 2 +- ext/rbs_extension/{location.c => legacy_location.c} | 2 +- ext/rbs_extension/{location.h => legacy_location.h} | 0 ext/rbs_extension/main.c | 2 +- ext/rbs_extension/rbs_string_bridging.h | 2 +- include/rbs/ast.h | 4 ++-- include/rbs/lexer.h | 2 +- include/rbs/{rbs_location.h => location.h} | 0 include/rbs/{rbs_string.h => string.h} | 0 include/rbs/util/rbs_buffer.h | 2 +- include/rbs/util/rbs_unescape.h | 2 +- src/{rbs_location.c => location.c} | 2 +- src/parser.c | 2 +- src/{rbs_string.c => string.c} | 2 +- templates/ext/rbs_extension/ast_translation.c.erb | 2 +- templates/ext/rbs_extension/ast_translation.h.erb | 2 +- templates/include/rbs/ast.h.erb | 4 ++-- 18 files changed, 17 insertions(+), 17 deletions(-) rename ext/rbs_extension/{location.c => legacy_location.c} (99%) rename ext/rbs_extension/{location.h => legacy_location.h} (100%) rename include/rbs/{rbs_location.h => location.h} (100%) rename include/rbs/{rbs_string.h => string.h} (100%) rename src/{rbs_location.c => location.c} (98%) rename src/{rbs_string.c => string.c} (98%) diff --git a/ext/rbs_extension/ast_translation.c b/ext/rbs_extension/ast_translation.c index fc25cb187..1cd1e68e2 100644 --- a/ext/rbs_extension/ast_translation.c +++ b/ext/rbs_extension/ast_translation.c @@ -9,7 +9,7 @@ #include "class_constants.h" #include "rbs_string_bridging.h" -#include "location.h" +#include "legacy_location.h" #define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1)) diff --git a/ext/rbs_extension/ast_translation.h b/ext/rbs_extension/ast_translation.h index 80622887c..8b682da7c 100644 --- a/ext/rbs_extension/ast_translation.h +++ b/ext/rbs_extension/ast_translation.h @@ -12,7 +12,7 @@ #include "ruby/encoding.h" #include "rbs/ast.h" -#include "rbs/rbs_location.h" +#include "rbs/location.h" /// A bag of values needed when copying RBS C structs into Ruby objects. typedef struct rbs_translation_context { diff --git a/ext/rbs_extension/location.c b/ext/rbs_extension/legacy_location.c similarity index 99% rename from ext/rbs_extension/location.c rename to ext/rbs_extension/legacy_location.c index 62dd1ee12..187c4fca5 100644 --- a/ext/rbs_extension/location.c +++ b/ext/rbs_extension/legacy_location.c @@ -1,4 +1,4 @@ -#include "location.h" +#include "legacy_location.h" #include "rbs_extension.h" #define RBS_LOC_REQUIRED_P(loc, i) ((loc)->children->required_p & (1 << (i))) diff --git a/ext/rbs_extension/location.h b/ext/rbs_extension/legacy_location.h similarity index 100% rename from ext/rbs_extension/location.h rename to ext/rbs_extension/legacy_location.h diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 2258b69c7..39d780f1c 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -3,7 +3,7 @@ #include "rbs/util/rbs_allocator.h" #include "rbs/util/rbs_constant_pool.h" #include "ast_translation.h" -#include "location.h" +#include "legacy_location.h" #include "rbs_string_bridging.h" #include "ruby/vm.h" diff --git a/ext/rbs_extension/rbs_string_bridging.h b/ext/rbs_extension/rbs_string_bridging.h index 54afe563b..ee4b8867b 100644 --- a/ext/rbs_extension/rbs_string_bridging.h +++ b/ext/rbs_extension/rbs_string_bridging.h @@ -4,7 +4,7 @@ #include "ruby.h" #include "ruby/encoding.h" -#include "rbs/rbs_string.h" +#include "rbs/string.h" /** * @returns A new shared rbs_string_t from the given Ruby string, which points into the given Ruby String's memory, diff --git a/include/rbs/ast.h b/include/rbs/ast.h index 581eeece8..db41a5e6f 100644 --- a/include/rbs/ast.h +++ b/include/rbs/ast.h @@ -10,8 +10,8 @@ #include "rbs/util/rbs_allocator.h" #include "rbs/util/rbs_constant_pool.h" -#include "rbs_string.h" -#include "rbs_location.h" +#include "string.h" +#include "location.h" enum rbs_node_type { RBS_AST_ANNOTATION = 1, diff --git a/include/rbs/lexer.h b/include/rbs/lexer.h index 53cea5150..ae671189c 100644 --- a/include/rbs/lexer.h +++ b/include/rbs/lexer.h @@ -1,7 +1,7 @@ #ifndef RBS__LEXER_H #define RBS__LEXER_H -#include "rbs_string.h" +#include "string.h" #include "util/rbs_encoding.h" enum RBSTokenType { diff --git a/include/rbs/rbs_location.h b/include/rbs/location.h similarity index 100% rename from include/rbs/rbs_location.h rename to include/rbs/location.h diff --git a/include/rbs/rbs_string.h b/include/rbs/string.h similarity index 100% rename from include/rbs/rbs_string.h rename to include/rbs/string.h diff --git a/include/rbs/util/rbs_buffer.h b/include/rbs/util/rbs_buffer.h index 3e8d9d7a4..7cc001ecc 100644 --- a/include/rbs/util/rbs_buffer.h +++ b/include/rbs/util/rbs_buffer.h @@ -2,7 +2,7 @@ #define RBS__RBS_BUFFER_H #include "rbs/util/rbs_allocator.h" -#include "rbs/rbs_string.h" +#include "rbs/string.h" #include #include diff --git a/include/rbs/util/rbs_unescape.h b/include/rbs/util/rbs_unescape.h index 90c9f90a6..cc551cbdd 100644 --- a/include/rbs/util/rbs_unescape.h +++ b/include/rbs/util/rbs_unescape.h @@ -3,7 +3,7 @@ #include #include "rbs/util/rbs_allocator.h" -#include "rbs/rbs_string.h" +#include "rbs/string.h" /** * Receives `rbs_parser_t` and `range`, which represents a string token or symbol token, and returns a string VALUE. diff --git a/src/rbs_location.c b/src/location.c similarity index 98% rename from src/rbs_location.c rename to src/location.c index 06094b699..45c80b4fb 100644 --- a/src/rbs_location.c +++ b/src/location.c @@ -1,4 +1,4 @@ -#include "rbs/rbs_location.h" +#include "rbs/location.h" #include "rbs/util/rbs_assert.h" #include diff --git a/src/parser.c b/src/parser.c index e8fad0c5a..9338073c0 100644 --- a/src/parser.c +++ b/src/parser.c @@ -7,7 +7,7 @@ #include #include "rbs/defines.h" -#include "rbs/rbs_string.h" +#include "rbs/string.h" #include "rbs/util/rbs_unescape.h" #include "rbs/util/rbs_buffer.h" #include "rbs/util/rbs_assert.h" diff --git a/src/rbs_string.c b/src/string.c similarity index 98% rename from src/rbs_string.c rename to src/string.c index 6f731e8b7..7b4ca658f 100644 --- a/src/rbs_string.c +++ b/src/string.c @@ -1,4 +1,4 @@ -#include "rbs/rbs_string.h" +#include "rbs/string.h" #include #include diff --git a/templates/ext/rbs_extension/ast_translation.c.erb b/templates/ext/rbs_extension/ast_translation.c.erb index 94aba5513..49ff27b4a 100644 --- a/templates/ext/rbs_extension/ast_translation.c.erb +++ b/templates/ext/rbs_extension/ast_translation.c.erb @@ -2,7 +2,7 @@ #include "class_constants.h" #include "rbs_string_bridging.h" -#include "location.h" +#include "legacy_location.h" #define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1)) diff --git a/templates/ext/rbs_extension/ast_translation.h.erb b/templates/ext/rbs_extension/ast_translation.h.erb index 125d6b0b6..4d4e2cff7 100644 --- a/templates/ext/rbs_extension/ast_translation.h.erb +++ b/templates/ext/rbs_extension/ast_translation.h.erb @@ -5,7 +5,7 @@ #include "ruby/encoding.h" #include "rbs/ast.h" -#include "rbs/rbs_location.h" +#include "rbs/location.h" /// A bag of values needed when copying RBS C structs into Ruby objects. typedef struct rbs_translation_context { diff --git a/templates/include/rbs/ast.h.erb b/templates/include/rbs/ast.h.erb index e901d760d..72b4cfe73 100644 --- a/templates/include/rbs/ast.h.erb +++ b/templates/include/rbs/ast.h.erb @@ -3,8 +3,8 @@ #include "rbs/util/rbs_allocator.h" #include "rbs/util/rbs_constant_pool.h" -#include "rbs_string.h" -#include "rbs_location.h" +#include "string.h" +#include "location.h" enum rbs_node_type { <%- nodes.each_with_index do |node, index| -%> From e6e7900792796c82fdc00f9df570db7df8754074 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 2 Apr 2025 03:59:29 +0800 Subject: [PATCH 103/111] Rename lexer struct, param, and functions (#35) 1. `lexerstate` should be `rbs_lexer_t` instead 2. Params representing `rbs_lexer_t` should be named `lexer` instead of `state` 3. `rbsparser_next_token` should be called `rbs_lexer_next_token` After this PR, all structs & functions we want to expose should have `rbs_` prefix. --- ext/rbs_extension/main.c | 6 +- include/rbs/lexer.h | 16 +- include/rbs/parser.h | 8 +- src/lexer.c | 1188 +++++++++++++++++++------------------- src/lexer.re | 188 +++--- src/lexstate.c | 70 +-- src/parser.c | 52 +- 7 files changed, 764 insertions(+), 764 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 39d780f1c..75739719e 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -129,7 +129,7 @@ static VALUE parse_type_try(VALUE a) { return ruby_ast; } -static lexstate *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE string, rb_encoding *encoding, int start_pos, int end_pos) { +static rbs_lexer_t *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE string, rb_encoding *encoding, int start_pos, int end_pos) { if (start_pos < 0 || end_pos < 0) { rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos); } @@ -286,12 +286,12 @@ static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) { rbs_allocator_t allocator; rbs_allocator_init(&allocator); - lexstate *lexer = alloc_lexer_from_buffer(&allocator, string, encoding, 0, FIX2INT(end_pos)); + rbs_lexer_t *lexer = alloc_lexer_from_buffer(&allocator, string, encoding, 0, FIX2INT(end_pos)); VALUE results = rb_ary_new(); rbs_token_t token = NullToken; while (token.type != pEOF) { - token = rbsparser_next_token(lexer); + token = rbs_lexer_next_token(lexer); VALUE type = ID2SYM(rb_intern(rbs_token_type_str(token.type))); VALUE location = rbs_new_location(buffer, token.range); VALUE pair = rb_ary_new3(2, type, location); diff --git a/include/rbs/lexer.h b/include/rbs/lexer.h index ae671189c..8c6b92c27 100644 --- a/include/rbs/lexer.h +++ b/include/rbs/lexer.h @@ -134,13 +134,13 @@ typedef struct { bool first_token_of_line; /* This flag is used for tLINECOMMENT */ unsigned int last_char; /* Last peeked character */ const rbs_encoding_t *encoding; -} lexstate; +} rbs_lexer_t; extern rbs_token_t NullToken; extern rbs_position_t NullPosition; extern rbs_range_t NULL_RANGE; -char *rbs_peek_token(lexstate *state, rbs_token_t tok); +char *rbs_peek_token(rbs_lexer_t *lexer, rbs_token_t tok); int rbs_token_chars(rbs_token_t tok); int rbs_token_bytes(rbs_token_t tok); @@ -154,29 +154,29 @@ const char *rbs_token_type_str(enum RBSTokenType type); /** * Read next character. * */ -unsigned int rbs_peek(lexstate *state); +unsigned int rbs_peek(rbs_lexer_t *lexer); /** * Skip one character. * */ -void rbs_skip(lexstate *state); +void rbs_skip(rbs_lexer_t *lexer); /** * Skip n characters. * */ -void rbs_skipn(lexstate *state, size_t size); +void rbs_skipn(rbs_lexer_t *lexer, size_t size); /** * Return new rbs_token_t with given type. * */ -rbs_token_t rbs_next_token(lexstate *state, enum RBSTokenType type); +rbs_token_t rbs_next_token(rbs_lexer_t *lexer, enum RBSTokenType type); /** * Return new rbs_token_t with EOF type. * */ -rbs_token_t rbs_next_eof_token(lexstate *state); +rbs_token_t rbs_next_eof_token(rbs_lexer_t *lexer); -rbs_token_t rbsparser_next_token(lexstate *state); +rbs_token_t rbs_lexer_next_token(rbs_lexer_t *lexer); void rbs_print_token(rbs_token_t tok); diff --git a/include/rbs/parser.h b/include/rbs/parser.h index 1399b908a..e1a35de83 100644 --- a/include/rbs/parser.h +++ b/include/rbs/parser.h @@ -44,7 +44,7 @@ typedef struct rbs_error_t { * An RBS parser is a LL(3) parser. * */ typedef struct { - lexstate *lexstate; + rbs_lexer_t *rbs_lexer_t; rbs_token_t current_token; rbs_token_t next_token; /* The first lookahead token */ @@ -83,14 +83,14 @@ void rbs_parser_push_typevar_table(rbs_parser_t *parser, bool reset); NODISCARD bool rbs_parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id); /** - * Allocate new lexstate object. + * Allocate new rbs_lexer_t object. * * ``` * VALUE string = rb_funcall(buffer, rb_intern("content"), 0); - * rbs_lexer_new(string, 0, 31) // New lexstate with buffer content + * rbs_lexer_new(string, 0, 31) // New rbs_lexer_t with buffer content * ``` * */ -lexstate *rbs_lexer_new(rbs_allocator_t *, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); +rbs_lexer_t *rbs_lexer_new(rbs_allocator_t *, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos); /** * Allocate new rbs_parser_t object. diff --git a/src/lexer.c b/src/lexer.c index df214a939..432966fdf 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -2,17 +2,17 @@ #line 1 "src/lexer.re" #include "rbs/lexer.h" -rbs_token_t rbsparser_next_token(lexstate *state) { - lexstate backup; +rbs_token_t rbs_lexer_next_token(rbs_lexer_t *lexer) { + rbs_lexer_t backup; - backup = *state; + backup = *lexer; #line 12 "src/lexer.c" { unsigned int yych; unsigned int yyaccept = 0; - yych = rbs_peek(state); + yych = rbs_peek(lexer); switch (yych) { case 0x00000000: goto yy1; case '\t': @@ -114,61 +114,61 @@ rbs_token_t rbsparser_next_token(lexstate *state) { default: goto yy2; } yy1: - rbs_skip(state); + rbs_skip(lexer); #line 144 "src/lexer.re" - { return rbs_next_eof_token(state); } + { return rbs_next_eof_token(lexer); } #line 121 "src/lexer.c" yy2: - rbs_skip(state); + rbs_skip(lexer); yy3: #line 145 "src/lexer.re" - { return rbs_next_token(state, ErrorToken); } + { return rbs_next_token(lexer, ErrorToken); } #line 127 "src/lexer.c" yy4: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '\t') goto yy4; if (yych == ' ') goto yy4; yy5: #line 143 "src/lexer.re" - { return rbs_next_token(state, tTRIVIA); } + { return rbs_next_token(lexer, tTRIVIA); } #line 136 "src/lexer.c" yy6: - rbs_skip(state); + rbs_skip(lexer); goto yy5; yy7: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '=') goto yy24; if (yych == '~') goto yy24; yy8: #line 48 "src/lexer.re" - { return rbs_next_token(state, tOPERATOR); } + { return rbs_next_token(lexer, tOPERATOR); } #line 148 "src/lexer.c" yy9: yyaccept = 0; - rbs_skip(state); - backup = *state; - yych = rbs_peek(state); + rbs_skip(lexer); + backup = *lexer; + yych = rbs_peek(lexer); if (yych <= 0x00000000) goto yy3; goto yy67; yy10: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= 0x00000000) goto yy11; if (yych != '\n') goto yy10; yy11: #line 59 "src/lexer.re" { return rbs_next_token( - state, - state->first_token_of_line ? tLINECOMMENT : tCOMMENT + lexer, + lexer->first_token_of_line ? tLINECOMMENT : tCOMMENT ); } #line 169 "src/lexer.c" yy12: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= ')') { if (yych <= 0x0000001F) { if (yych <= '\n') { @@ -214,55 +214,55 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy13: yyaccept = 1; - rbs_skip(state); - backup = *state; - yych = rbs_peek(state); + rbs_skip(lexer); + backup = *lexer; + yych = rbs_peek(lexer); if (yych == 'a') goto yy74; goto yy8; yy14: - rbs_skip(state); + rbs_skip(lexer); #line 33 "src/lexer.re" - { return rbs_next_token(state, pAMP); } + { return rbs_next_token(lexer, pAMP); } #line 227 "src/lexer.c" yy15: yyaccept = 0; - rbs_skip(state); - backup = *state; - yych = rbs_peek(state); + rbs_skip(lexer); + backup = *lexer; + yych = rbs_peek(lexer); if (yych <= 0x00000000) goto yy3; goto yy76; yy16: - rbs_skip(state); + rbs_skip(lexer); #line 24 "src/lexer.re" - { return rbs_next_token(state, pLPAREN); } + { return rbs_next_token(lexer, pLPAREN); } #line 239 "src/lexer.c" yy17: - rbs_skip(state); + rbs_skip(lexer); #line 25 "src/lexer.re" - { return rbs_next_token(state, pRPAREN); } + { return rbs_next_token(lexer, pRPAREN); } #line 244 "src/lexer.c" yy18: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '*') goto yy80; #line 35 "src/lexer.re" - { return rbs_next_token(state, pSTAR); } + { return rbs_next_token(lexer, pSTAR); } #line 251 "src/lexer.c" yy19: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '/') goto yy8; if (yych <= '9') goto yy25; if (yych == '@') goto yy24; goto yy8; yy20: - rbs_skip(state); + rbs_skip(lexer); #line 30 "src/lexer.re" - { return rbs_next_token(state, pCOMMA); } + { return rbs_next_token(lexer, pCOMMA); } #line 263 "src/lexer.c" yy21: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') goto yy8; if (yych <= '9') goto yy25; @@ -274,32 +274,32 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy22: yyaccept = 2; - rbs_skip(state); - backup = *state; - yych = rbs_peek(state); + rbs_skip(lexer); + backup = *lexer; + yych = rbs_peek(lexer); if (yych == '.') goto yy82; yy23: #line 37 "src/lexer.re" - { return rbs_next_token(state, pDOT); } + { return rbs_next_token(lexer, pDOT); } #line 285 "src/lexer.c" yy24: - rbs_skip(state); + rbs_skip(lexer); goto yy8; yy25: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '/') goto yy26; if (yych <= '9') goto yy25; if (yych == '_') goto yy25; yy26: #line 51 "src/lexer.re" - { return rbs_next_token(state, tINTEGER); } + { return rbs_next_token(lexer, tINTEGER); } #line 298 "src/lexer.c" yy27: yyaccept = 3; - rbs_skip(state); - backup = *state; - yych = rbs_peek(state); + rbs_skip(lexer); + backup = *lexer; + yych = rbs_peek(lexer); switch (yych) { case '!': goto yy83; case '"': goto yy85; @@ -378,21 +378,21 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy28: #line 44 "src/lexer.re" - { return rbs_next_token(state, pCOLON); } + { return rbs_next_token(lexer, pCOLON); } #line 383 "src/lexer.c" yy29: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= ';') goto yy30; if (yych <= '<') goto yy24; if (yych <= '=') goto yy99; yy30: #line 46 "src/lexer.re" - { return rbs_next_token(state, pLT); } + { return rbs_next_token(lexer, pLT); } #line 393 "src/lexer.c" yy31: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '>') { if (yych <= '<') goto yy32; if (yych <= '=') goto yy100; @@ -402,24 +402,24 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy32: #line 43 "src/lexer.re" - { return rbs_next_token(state, pEQ); } + { return rbs_next_token(lexer, pEQ); } #line 407 "src/lexer.c" yy33: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '<') goto yy8; if (yych <= '>') goto yy24; goto yy8; yy34: - rbs_skip(state); + rbs_skip(lexer); #line 34 "src/lexer.re" - { return rbs_next_token(state, pQUESTION); } + { return rbs_next_token(lexer, pQUESTION); } #line 418 "src/lexer.c" yy35: yyaccept = 0; - rbs_skip(state); - backup = *state; - yych = rbs_peek(state); + rbs_skip(lexer); + backup = *lexer; + yych = rbs_peek(lexer); if (yych <= '^') { if (yych <= '?') goto yy3; if (yych <= '@') goto yy102; @@ -431,8 +431,8 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy3; } yy36: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -451,28 +451,28 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy37: #line 129 "src/lexer.re" - { return rbs_next_token(state, tUIDENT); } + { return rbs_next_token(lexer, tUIDENT); } #line 456 "src/lexer.c" yy38: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == ']') goto yy107; #line 26 "src/lexer.re" - { return rbs_next_token(state, pLBRACKET); } + { return rbs_next_token(lexer, pLBRACKET); } #line 463 "src/lexer.c" yy39: - rbs_skip(state); + rbs_skip(lexer); #line 27 "src/lexer.re" - { return rbs_next_token(state, pRBRACKET); } + { return rbs_next_token(lexer, pRBRACKET); } #line 468 "src/lexer.c" yy40: - rbs_skip(state); + rbs_skip(lexer); #line 32 "src/lexer.re" - { return rbs_next_token(state, pHAT); } + { return rbs_next_token(lexer, pHAT); } #line 473 "src/lexer.c" yy41: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -492,13 +492,13 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy42: #line 132 "src/lexer.re" - { return rbs_next_token(state, tULLIDENT); } + { return rbs_next_token(lexer, tULLIDENT); } #line 497 "src/lexer.c" yy43: yyaccept = 4; - rbs_skip(state); - backup = *state; - yych = rbs_peek(state); + rbs_skip(lexer); + backup = *lexer; + yych = rbs_peek(lexer); if (yych <= ' ') { if (yych <= 0x00000000) goto yy44; if (yych <= 0x0000001F) goto yy114; @@ -507,11 +507,11 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy44: #line 39 "src/lexer.re" - { return rbs_next_token(state, tOPERATOR); } + { return rbs_next_token(lexer, tOPERATOR); } #line 512 "src/lexer.c" yy45: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= 'r') { if (yych == 'l') goto yy115; goto yy53; @@ -522,37 +522,37 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy46: #line 128 "src/lexer.re" - { return rbs_next_token(state, tLIDENT); } + { return rbs_next_token(lexer, tLIDENT); } #line 527 "src/lexer.c" yy47: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'o') goto yy119; goto yy53; yy48: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'l') goto yy120; goto yy53; yy49: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy121; goto yy53; yy50: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'n') goto yy122; if (yych == 'x') goto yy123; goto yy53; yy51: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'a') goto yy124; goto yy53; yy52: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); yy53: if (yych <= '=') { if (yych <= '/') { @@ -575,40 +575,40 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } } yy54: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'n') goto yy125; goto yy53; yy55: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'o') goto yy127; goto yy53; yy56: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'i') goto yy128; goto yy53; yy57: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'u') goto yy129; goto yy53; yy58: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'r') goto yy130; if (yych == 'u') goto yy131; goto yy53; yy59: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy132; if (yych == 'i') goto yy133; goto yy53; yy60: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= 'q') { if (yych == 'o') goto yy134; goto yy53; @@ -618,34 +618,34 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy53; } yy61: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'n') goto yy137; if (yych == 's') goto yy138; goto yy53; yy62: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'o') goto yy139; goto yy53; yy63: - rbs_skip(state); + rbs_skip(lexer); #line 28 "src/lexer.re" - { return rbs_next_token(state, pLBRACE); } + { return rbs_next_token(lexer, pLBRACE); } #line 636 "src/lexer.c" yy64: - rbs_skip(state); + rbs_skip(lexer); #line 31 "src/lexer.re" - { return rbs_next_token(state, pBAR); } + { return rbs_next_token(lexer, pBAR); } #line 641 "src/lexer.c" yy65: - rbs_skip(state); + rbs_skip(lexer); #line 29 "src/lexer.re" - { return rbs_next_token(state, pRBRACE); } + { return rbs_next_token(lexer, pRBRACE); } #line 646 "src/lexer.c" yy66: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); yy67: if (yych <= '"') { if (yych <= 0x00000000) goto yy68; @@ -656,7 +656,7 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy66; } yy68: - *state = backup; + *lexer = backup; if (yyaccept <= 3) { if (yyaccept <= 1) { if (yyaccept == 0) { @@ -683,19 +683,19 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } } yy69: - rbs_skip(state); + rbs_skip(lexer); #line 106 "src/lexer.re" - { return rbs_next_token(state, tDQSTRING); } + { return rbs_next_token(lexer, tDQSTRING); } #line 690 "src/lexer.c" yy70: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'u') goto yy140; if (yych == 'x') goto yy141; goto yy66; yy71: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= ',') { if (yych <= '\f') { if (yych <= 0x00000000) goto yy72; @@ -724,14 +724,14 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy72: #line 139 "src/lexer.re" - { return rbs_next_token(state, tGIDENT); } + { return rbs_next_token(lexer, tGIDENT); } #line 729 "src/lexer.c" yy73: - rbs_skip(state); + rbs_skip(lexer); goto yy72; yy74: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= 'Z') { if (yych <= '(') { if (yych <= '\'') goto yy68; @@ -751,8 +751,8 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } } yy75: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); yy76: if (yych <= '\'') { if (yych <= 0x00000000) goto yy68; @@ -762,14 +762,14 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy75; } yy77: - rbs_skip(state); + rbs_skip(lexer); yy78: #line 107 "src/lexer.re" - { return rbs_next_token(state, tSQSTRING); } + { return rbs_next_token(lexer, tSQSTRING); } #line 770 "src/lexer.c" yy79: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '\'') { if (yych <= 0x00000000) goto yy68; if (yych <= '&') goto yy75; @@ -779,32 +779,32 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy75; } yy80: - rbs_skip(state); + rbs_skip(lexer); #line 36 "src/lexer.re" - { return rbs_next_token(state, pSTAR2); } + { return rbs_next_token(lexer, pSTAR2); } #line 786 "src/lexer.c" yy81: - rbs_skip(state); + rbs_skip(lexer); #line 41 "src/lexer.re" - { return rbs_next_token(state, pARROW); } + { return rbs_next_token(lexer, pARROW); } #line 791 "src/lexer.c" yy82: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '.') goto yy148; goto yy68; yy83: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '=') goto yy87; if (yych == '~') goto yy87; yy84: #line 126 "src/lexer.re" - { return rbs_next_token(state, tSYMBOL); } + { return rbs_next_token(lexer, tSYMBOL); } #line 805 "src/lexer.c" yy85: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '"') { if (yych <= 0x00000000) goto yy68; if (yych <= '!') goto yy85; @@ -814,8 +814,8 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy85; } yy86: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= ')') { if (yych <= 0x0000001F) { if (yych <= '\n') { @@ -860,11 +860,11 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } } yy87: - rbs_skip(state); + rbs_skip(lexer); goto yy84; yy88: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '\'') { if (yych <= 0x00000000) goto yy68; if (yych <= '&') goto yy88; @@ -874,42 +874,42 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy88; } yy89: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '*') goto yy87; goto yy84; yy90: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '@') goto yy87; goto yy84; yy91: - rbs_skip(state); + rbs_skip(lexer); #line 45 "src/lexer.re" - { return rbs_next_token(state, pCOLON2); } + { return rbs_next_token(lexer, pCOLON2); } #line 891 "src/lexer.c" yy92: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= ';') goto yy84; if (yych <= '<') goto yy87; if (yych <= '=') goto yy157; goto yy84; yy93: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '=') goto yy158; if (yych == '~') goto yy87; goto yy68; yy94: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '<') goto yy84; if (yych <= '>') goto yy87; goto yy84; yy95: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '^') { if (yych <= '?') goto yy68; if (yych <= '@') goto yy159; @@ -921,8 +921,8 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy68; } yy96: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '>') { if (yych <= '/') { if (yych == '!') goto yy162; @@ -942,31 +942,31 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy97: #line 122 "src/lexer.re" - { return rbs_next_token(state, tSYMBOL); } + { return rbs_next_token(lexer, tSYMBOL); } #line 947 "src/lexer.c" yy98: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == ']') goto yy158; goto yy68; yy99: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '>') goto yy24; goto yy8; yy100: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '=') goto yy24; goto yy8; yy101: - rbs_skip(state); + rbs_skip(lexer); #line 42 "src/lexer.re" - { return rbs_next_token(state, pFATARROW); } + { return rbs_next_token(lexer, pFATARROW); } #line 967 "src/lexer.c" yy102: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '^') { if (yych <= '@') goto yy68; if (yych <= 'Z') goto yy163; @@ -977,8 +977,8 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy68; } yy103: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= 'Z') { if (yych <= '/') goto yy104; if (yych <= '9') goto yy103; @@ -993,28 +993,28 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy104: #line 136 "src/lexer.re" - { return rbs_next_token(state, tAIDENT); } + { return rbs_next_token(lexer, tAIDENT); } #line 998 "src/lexer.c" yy105: - rbs_skip(state); + rbs_skip(lexer); #line 133 "src/lexer.re" - { return rbs_next_token(state, tBANGIDENT); } + { return rbs_next_token(lexer, tBANGIDENT); } #line 1003 "src/lexer.c" yy106: - rbs_skip(state); + rbs_skip(lexer); #line 134 "src/lexer.re" - { return rbs_next_token(state, tEQIDENT); } + { return rbs_next_token(lexer, tEQIDENT); } #line 1008 "src/lexer.c" yy107: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '=') goto yy24; #line 47 "src/lexer.re" - { return rbs_next_token(state, pAREF_OPR); } + { return rbs_next_token(lexer, pAREF_OPR); } #line 1015 "src/lexer.c" yy108: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); yy109: if (yych <= '=') { if (yych <= '/') { @@ -1034,11 +1034,11 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy110: #line 130 "src/lexer.re" - { return rbs_next_token(state, tULLIDENT); } + { return rbs_next_token(lexer, tULLIDENT); } #line 1039 "src/lexer.c" yy111: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1057,27 +1057,27 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy112: #line 131 "src/lexer.re" - { return rbs_next_token(state, tULIDENT); } + { return rbs_next_token(lexer, tULIDENT); } #line 1062 "src/lexer.c" yy113: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 't') goto yy165; goto yy109; yy114: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= 0x00000000) goto yy68; if (yych == '`') goto yy166; goto yy114; yy115: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'i') goto yy167; goto yy53; yy116: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1096,47 +1096,47 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy117: #line 96 "src/lexer.re" - { return rbs_next_token(state, kAS); } + { return rbs_next_token(lexer, kAS); } #line 1101 "src/lexer.c" yy118: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 't') goto yy168; goto yy53; yy119: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'o') goto yy169; if (yych == 't') goto yy170; goto yy53; yy120: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'a') goto yy172; goto yy53; yy121: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'f') goto yy173; goto yy53; yy122: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'd') goto yy175; goto yy53; yy123: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 't') goto yy177; goto yy53; yy124: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'l') goto yy178; goto yy53; yy125: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '^') { if (yych <= '9') { if (yych == '!') goto yy105; @@ -1166,78 +1166,78 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy126: #line 77 "src/lexer.re" - { return rbs_next_token(state, kIN); } + { return rbs_next_token(lexer, kIN); } #line 1171 "src/lexer.c" yy127: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'd') goto yy182; goto yy53; yy128: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'l') goto yy183; goto yy53; yy129: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 't') goto yy185; goto yy53; yy130: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy187; if (yych == 'i') goto yy188; goto yy53; yy131: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'b') goto yy189; goto yy53; yy132: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'l') goto yy190; goto yy53; yy133: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'n') goto yy191; goto yy53; yy134: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'p') goto yy192; goto yy53; yy135: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'u') goto yy194; goto yy53; yy136: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'p') goto yy195; goto yy53; yy137: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'c') goto yy196; if (yych == 't') goto yy197; goto yy53; yy138: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy198; goto yy53; yy139: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'i') goto yy200; goto yy53; yy140: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy201; @@ -1249,48 +1249,48 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy68; } yy141: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '/') goto yy68; if (yych <= '9') goto yy66; if (yych <= '`') goto yy68; if (yych <= 'f') goto yy66; goto yy68; yy142: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= 0x00000000) goto yy68; if (yych == ')') goto yy202; goto yy142; yy143: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= 0x00000000) goto yy68; if (yych == '>') goto yy203; goto yy143; yy144: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= 0x00000000) goto yy68; if (yych == ']') goto yy204; goto yy144; yy145: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= 0x00000000) goto yy68; if (yych == '}') goto yy205; goto yy145; yy146: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= 0x00000000) goto yy68; if (yych == '|') goto yy206; goto yy146; yy147: yyaccept = 5; - rbs_skip(state); - backup = *state; - yych = rbs_peek(state); + rbs_skip(lexer); + backup = *lexer; + yych = rbs_peek(lexer); if (yych <= '\'') { if (yych <= 0x00000000) goto yy78; if (yych <= '&') goto yy75; @@ -1300,24 +1300,24 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy75; } yy148: - rbs_skip(state); + rbs_skip(lexer); #line 38 "src/lexer.re" - { return rbs_next_token(state, pDOT3); } + { return rbs_next_token(lexer, pDOT3); } #line 1307 "src/lexer.c" yy149: - rbs_skip(state); + rbs_skip(lexer); #line 108 "src/lexer.re" - { return rbs_next_token(state, tDQSYMBOL); } + { return rbs_next_token(lexer, tDQSYMBOL); } #line 1312 "src/lexer.c" yy150: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'u') goto yy207; if (yych == 'x') goto yy208; goto yy85; yy151: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= ',') { if (yych <= '\f') { if (yych <= 0x00000000) goto yy152; @@ -1346,20 +1346,20 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy152: #line 125 "src/lexer.re" - { return rbs_next_token(state, tSYMBOL); } + { return rbs_next_token(lexer, tSYMBOL); } #line 1351 "src/lexer.c" yy153: - rbs_skip(state); + rbs_skip(lexer); goto yy152; yy154: - rbs_skip(state); + rbs_skip(lexer); yy155: #line 109 "src/lexer.re" - { return rbs_next_token(state, tSQSYMBOL); } + { return rbs_next_token(lexer, tSQSYMBOL); } #line 1360 "src/lexer.c" yy156: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '\'') { if (yych <= 0x00000000) goto yy68; if (yych <= '&') goto yy88; @@ -1369,18 +1369,18 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy88; } yy157: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '>') goto yy87; goto yy84; yy158: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '=') goto yy87; goto yy84; yy159: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '^') { if (yych <= '@') goto yy68; if (yych <= 'Z') goto yy210; @@ -1391,8 +1391,8 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy68; } yy160: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '>') { if (yych <= '/') { if (yych == '!') goto yy212; @@ -1412,14 +1412,14 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy161: #line 123 "src/lexer.re" - { return rbs_next_token(state, tSYMBOL); } + { return rbs_next_token(lexer, tSYMBOL); } #line 1417 "src/lexer.c" yy162: - rbs_skip(state); + rbs_skip(lexer); goto yy97; yy163: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= 'Z') { if (yych <= '/') goto yy164; if (yych <= '9') goto yy163; @@ -1434,36 +1434,36 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy164: #line 137 "src/lexer.re" - { return rbs_next_token(state, tA2IDENT); } + { return rbs_next_token(lexer, tA2IDENT); } #line 1439 "src/lexer.c" yy165: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'o') goto yy213; goto yy109; yy166: - rbs_skip(state); + rbs_skip(lexer); #line 40 "src/lexer.re" - { return rbs_next_token(state, tQIDENT); } + { return rbs_next_token(lexer, tQIDENT); } #line 1449 "src/lexer.c" yy167: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'a') goto yy214; goto yy53; yy168: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'r') goto yy215; goto yy53; yy169: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'l') goto yy216; goto yy53; yy170: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1482,16 +1482,16 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy171: #line 71 "src/lexer.re" - { return rbs_next_token(state, kBOT); } + { return rbs_next_token(lexer, kBOT); } #line 1487 "src/lexer.c" yy172: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 's') goto yy218; goto yy53; yy173: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1510,11 +1510,11 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy174: #line 73 "src/lexer.re" - { return rbs_next_token(state, kDEF); } + { return rbs_next_token(lexer, kDEF); } #line 1515 "src/lexer.c" yy175: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1533,41 +1533,41 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy176: #line 74 "src/lexer.re" - { return rbs_next_token(state, kEND); } + { return rbs_next_token(lexer, kEND); } #line 1538 "src/lexer.c" yy177: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy219; goto yy53; yy178: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 's') goto yy220; goto yy53; yy179: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'l') goto yy221; goto yy53; yy180: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 't') goto yy222; goto yy53; yy181: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy223; goto yy53; yy182: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'u') goto yy224; goto yy53; yy183: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1586,11 +1586,11 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy184: #line 82 "src/lexer.re" - { return rbs_next_token(state, kNIL); } + { return rbs_next_token(lexer, kNIL); } #line 1591 "src/lexer.c" yy185: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1609,36 +1609,36 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy186: #line 83 "src/lexer.re" - { return rbs_next_token(state, kOUT); } + { return rbs_next_token(lexer, kOUT); } #line 1614 "src/lexer.c" yy187: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'p') goto yy225; goto yy53; yy188: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'v') goto yy226; goto yy53; yy189: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'l') goto yy227; goto yy53; yy190: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'f') goto yy228; goto yy53; yy191: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'g') goto yy230; goto yy53; yy192: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1657,31 +1657,31 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy193: #line 89 "src/lexer.re" - { return rbs_next_token(state, kTOP); } + { return rbs_next_token(lexer, kTOP); } #line 1662 "src/lexer.c" yy194: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy231; goto yy53; yy195: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy233; goto yy53; yy196: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'h') goto yy235; goto yy53; yy197: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'y') goto yy236; goto yy53; yy198: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1700,16 +1700,16 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy199: #line 95 "src/lexer.re" - { return rbs_next_token(state, kUSE); } + { return rbs_next_token(lexer, kUSE); } #line 1705 "src/lexer.c" yy200: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'd') goto yy237; goto yy53; yy201: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy239; @@ -1721,33 +1721,33 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy68; } yy202: - rbs_skip(state); + rbs_skip(lexer); #line 54 "src/lexer.re" - { return rbs_next_token(state, tANNOTATION); } + { return rbs_next_token(lexer, tANNOTATION); } #line 1728 "src/lexer.c" yy203: - rbs_skip(state); + rbs_skip(lexer); #line 57 "src/lexer.re" - { return rbs_next_token(state, tANNOTATION); } + { return rbs_next_token(lexer, tANNOTATION); } #line 1733 "src/lexer.c" yy204: - rbs_skip(state); + rbs_skip(lexer); #line 55 "src/lexer.re" - { return rbs_next_token(state, tANNOTATION); } + { return rbs_next_token(lexer, tANNOTATION); } #line 1738 "src/lexer.c" yy205: - rbs_skip(state); + rbs_skip(lexer); #line 53 "src/lexer.re" - { return rbs_next_token(state, tANNOTATION); } + { return rbs_next_token(lexer, tANNOTATION); } #line 1743 "src/lexer.c" yy206: - rbs_skip(state); + rbs_skip(lexer); #line 56 "src/lexer.re" - { return rbs_next_token(state, tANNOTATION); } + { return rbs_next_token(lexer, tANNOTATION); } #line 1748 "src/lexer.c" yy207: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy240; @@ -1759,8 +1759,8 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy68; } yy208: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '/') goto yy68; if (yych <= '9') goto yy85; if (yych <= '`') goto yy68; @@ -1768,9 +1768,9 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy68; yy209: yyaccept = 6; - rbs_skip(state); - backup = *state; - yych = rbs_peek(state); + rbs_skip(lexer); + backup = *lexer; + yych = rbs_peek(lexer); if (yych <= '\'') { if (yych <= 0x00000000) goto yy155; if (yych <= '&') goto yy88; @@ -1780,8 +1780,8 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy88; } yy210: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '>') { if (yych <= '/') { if (yych == '!') goto yy241; @@ -1801,29 +1801,29 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy211: #line 124 "src/lexer.re" - { return rbs_next_token(state, tSYMBOL); } + { return rbs_next_token(lexer, tSYMBOL); } #line 1806 "src/lexer.c" yy212: - rbs_skip(state); + rbs_skip(lexer); goto yy161; yy213: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'd') goto yy242; goto yy109; yy214: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 's') goto yy243; goto yy53; yy215: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '_') goto yy245; goto yy53; yy216: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1842,61 +1842,61 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy217: #line 70 "src/lexer.re" - { return rbs_next_token(state, kBOOL); } + { return rbs_next_token(lexer, kBOOL); } #line 1847 "src/lexer.c" yy218: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 's') goto yy246; goto yy53; yy219: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'n') goto yy248; goto yy53; yy220: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy249; goto yy53; yy221: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'u') goto yy251; goto yy53; yy222: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'a') goto yy252; goto yy53; yy223: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'r') goto yy253; goto yy53; yy224: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'l') goto yy254; goto yy53; yy225: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy255; goto yy53; yy226: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'a') goto yy256; goto yy53; yy227: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'i') goto yy257; goto yy53; yy228: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1915,16 +1915,16 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy229: #line 87 "src/lexer.re" - { return rbs_next_token(state, kSELF); } + { return rbs_next_token(lexer, kSELF); } #line 1920 "src/lexer.c" yy230: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'l') goto yy258; goto yy53; yy231: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1943,11 +1943,11 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy232: #line 90 "src/lexer.re" - { return rbs_next_token(state, kTRUE); } + { return rbs_next_token(lexer, kTRUE); } #line 1948 "src/lexer.c" yy233: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1966,21 +1966,21 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy234: #line 91 "src/lexer.re" - { return rbs_next_token(state, kTYPE); } + { return rbs_next_token(lexer, kTYPE); } #line 1971 "src/lexer.c" yy235: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy259; goto yy53; yy236: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'p') goto yy260; goto yy53; yy237: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -1999,11 +1999,11 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy238: #line 94 "src/lexer.re" - { return rbs_next_token(state, kVOID); } + { return rbs_next_token(lexer, kVOID); } #line 2004 "src/lexer.c" yy239: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy261; @@ -2015,8 +2015,8 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy68; } yy240: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy262; @@ -2028,16 +2028,16 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy68; } yy241: - rbs_skip(state); + rbs_skip(lexer); goto yy211; yy242: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'o') goto yy263; goto yy109; yy243: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2056,11 +2056,11 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy244: #line 66 "src/lexer.re" - { return rbs_next_token(state, kALIAS); } + { return rbs_next_token(lexer, kALIAS); } #line 2061 "src/lexer.c" yy245: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= 'q') { if (yych == 'a') goto yy264; goto yy53; @@ -2070,8 +2070,8 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy53; } yy246: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2090,16 +2090,16 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy247: #line 72 "src/lexer.re" - { return rbs_next_token(state, kCLASS); } + { return rbs_next_token(lexer, kCLASS); } #line 2095 "src/lexer.c" yy248: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'd') goto yy267; goto yy53; yy249: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2118,61 +2118,61 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy250: #line 76 "src/lexer.re" - { return rbs_next_token(state, kFALSE); } + { return rbs_next_token(lexer, kFALSE); } #line 2123 "src/lexer.c" yy251: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'd') goto yy269; goto yy53; yy252: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'n') goto yy270; goto yy53; yy253: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'f') goto yy271; goto yy53; yy254: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy272; goto yy53; yy255: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'n') goto yy274; goto yy53; yy256: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 't') goto yy275; goto yy53; yy257: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'c') goto yy276; goto yy53; yy258: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy278; goto yy53; yy259: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'c') goto yy279; goto yy53; yy260: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy280; goto yy53; yy261: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy66; @@ -2184,8 +2184,8 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy68; } yy262: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy281; @@ -2197,28 +2197,28 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy68; } yy263: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '_') goto yy282; goto yy109; yy264: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'c') goto yy283; goto yy53; yy265: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy284; goto yy53; yy266: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'r') goto yy285; goto yy53; yy267: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2237,26 +2237,26 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy268: #line 75 "src/lexer.re" - { return rbs_next_token(state, kEXTEND); } + { return rbs_next_token(lexer, kEXTEND); } #line 2242 "src/lexer.c" yy269: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy286; goto yy53; yy270: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'c') goto yy288; goto yy53; yy271: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'a') goto yy289; goto yy53; yy272: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2275,21 +2275,21 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy273: #line 81 "src/lexer.re" - { return rbs_next_token(state, kMODULE); } + { return rbs_next_token(lexer, kMODULE); } #line 2280 "src/lexer.c" yy274: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'd') goto yy290; goto yy53; yy275: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy292; goto yy53; yy276: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2308,26 +2308,26 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy277: #line 86 "src/lexer.re" - { return rbs_next_token(state, kPUBLIC); } + { return rbs_next_token(lexer, kPUBLIC); } #line 2313 "src/lexer.c" yy278: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 't') goto yy294; goto yy53; yy279: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'k') goto yy295; goto yy53; yy280: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'd') goto yy296; goto yy53; yy281: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '@') { if (yych <= '/') goto yy68; if (yych <= '9') goto yy85; @@ -2339,28 +2339,28 @@ rbs_token_t rbsparser_next_token(lexstate *state) { goto yy68; } yy282: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == '_') goto yy298; goto yy109; yy283: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'c') goto yy300; goto yy53; yy284: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'a') goto yy301; goto yy53; yy285: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'i') goto yy302; goto yy53; yy286: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2379,21 +2379,21 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy287: #line 78 "src/lexer.re" - { return rbs_next_token(state, kINCLUDE); } + { return rbs_next_token(lexer, kINCLUDE); } #line 2384 "src/lexer.c" yy288: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy303; goto yy53; yy289: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'c') goto yy305; goto yy53; yy290: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2412,11 +2412,11 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy291: #line 84 "src/lexer.re" - { return rbs_next_token(state, kPREPEND); } + { return rbs_next_token(lexer, kPREPEND); } #line 2417 "src/lexer.c" yy292: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2435,21 +2435,21 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy293: #line 85 "src/lexer.re" - { return rbs_next_token(state, kPRIVATE); } + { return rbs_next_token(lexer, kPRIVATE); } #line 2440 "src/lexer.c" yy294: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'o') goto yy306; goto yy53; yy295: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy307; goto yy53; yy296: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2468,11 +2468,11 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy297: #line 93 "src/lexer.re" - { return rbs_next_token(state, kUNTYPED); } + { return rbs_next_token(lexer, kUNTYPED); } #line 2473 "src/lexer.c" yy298: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2491,26 +2491,26 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy299: #line 97 "src/lexer.re" - { return rbs_next_token(state, k__TODO__); } + { return rbs_next_token(lexer, k__TODO__); } #line 2496 "src/lexer.c" yy300: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy308; goto yy53; yy301: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'd') goto yy309; goto yy53; yy302: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 't') goto yy310; goto yy53; yy303: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2529,41 +2529,41 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy304: #line 79 "src/lexer.re" - { return rbs_next_token(state, kINSTANCE); } + { return rbs_next_token(lexer, kINSTANCE); } #line 2534 "src/lexer.c" yy305: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy311; goto yy53; yy306: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'n') goto yy313; goto yy53; yy307: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'd') goto yy315; goto yy53; yy308: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 's') goto yy317; goto yy53; yy309: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy318; goto yy53; yy310: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'e') goto yy319; goto yy53; yy311: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2582,11 +2582,11 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy312: #line 80 "src/lexer.re" - { return rbs_next_token(state, kINTERFACE); } + { return rbs_next_token(lexer, kINTERFACE); } #line 2587 "src/lexer.c" yy313: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2605,11 +2605,11 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy314: #line 88 "src/lexer.re" - { return rbs_next_token(state, kSINGLETON); } + { return rbs_next_token(lexer, kSINGLETON); } #line 2610 "src/lexer.c" yy315: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2628,31 +2628,31 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy316: #line 92 "src/lexer.re" - { return rbs_next_token(state, kUNCHECKED); } + { return rbs_next_token(lexer, kUNCHECKED); } #line 2633 "src/lexer.c" yy317: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 's') goto yy320; goto yy53; yy318: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'r') goto yy321; goto yy53; yy319: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'r') goto yy323; goto yy53; yy320: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych == 'o') goto yy325; goto yy53; yy321: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2671,11 +2671,11 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy322: #line 68 "src/lexer.re" - { return rbs_next_token(state, kATTRREADER); } + { return rbs_next_token(lexer, kATTRREADER); } #line 2676 "src/lexer.c" yy323: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2694,14 +2694,14 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy324: #line 69 "src/lexer.re" - { return rbs_next_token(state, kATTRWRITER); } + { return rbs_next_token(lexer, kATTRWRITER); } #line 2699 "src/lexer.c" yy325: - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych != 'r') goto yy53; - rbs_skip(state); - yych = rbs_peek(state); + rbs_skip(lexer); + yych = rbs_peek(lexer); if (yych <= '=') { if (yych <= '/') { if (yych == '!') goto yy105; @@ -2720,7 +2720,7 @@ rbs_token_t rbsparser_next_token(lexstate *state) { } yy326: #line 67 "src/lexer.re" - { return rbs_next_token(state, kATTRACCESSOR); } + { return rbs_next_token(lexer, kATTRACCESSOR); } #line 2725 "src/lexer.c" } #line 146 "src/lexer.re" diff --git a/src/lexer.re b/src/lexer.re index df5a148c1..575a0c301 100644 --- a/src/lexer.re +++ b/src/lexer.re @@ -1,19 +1,19 @@ #include "rbs/lexer.h" -rbs_token_t rbsparser_next_token(lexstate *state) { - lexstate backup; +rbs_token_t rbs_lexer_next_token(rbs_lexer_t *lexer) { + rbs_lexer_t backup; - backup = *state; + backup = *lexer; /*!re2c re2c:flags:u = 1; re2c:api:style = free-form; re2c:flags:input = custom; re2c:define:YYCTYPE = "unsigned int"; - re2c:define:YYPEEK = "rbs_peek(state)"; - re2c:define:YYSKIP = "rbs_skip(state);"; - re2c:define:YYBACKUP = "backup = *state;"; - re2c:define:YYRESTORE = "*state = backup;"; + re2c:define:YYPEEK = "rbs_peek(lexer)"; + re2c:define:YYSKIP = "rbs_skip(lexer);"; + re2c:define:YYBACKUP = "backup = *lexer;"; + re2c:define:YYRESTORE = "*lexer = backup;"; re2c:yyfill:enable = 0; word = [a-zA-Z0-9_]; @@ -21,80 +21,80 @@ rbs_token_t rbsparser_next_token(lexstate *state) { operator = "/" | "~" | "[]=" | "!" | "!=" | "!~" | "-" | "-@" | "+" | "+@" | "==" | "===" | "=~" | "<<" | "<=" | "<=>" | ">" | ">=" | ">>" | "%"; - "(" { return rbs_next_token(state, pLPAREN); } - ")" { return rbs_next_token(state, pRPAREN); } - "[" { return rbs_next_token(state, pLBRACKET); } - "]" { return rbs_next_token(state, pRBRACKET); } - "{" { return rbs_next_token(state, pLBRACE); } - "}" { return rbs_next_token(state, pRBRACE); } - "," { return rbs_next_token(state, pCOMMA); } - "|" { return rbs_next_token(state, pBAR); } - "^" { return rbs_next_token(state, pHAT); } - "&" { return rbs_next_token(state, pAMP); } - "?" { return rbs_next_token(state, pQUESTION); } - "*" { return rbs_next_token(state, pSTAR); } - "**" { return rbs_next_token(state, pSTAR2); } - "." { return rbs_next_token(state, pDOT); } - "..." { return rbs_next_token(state, pDOT3); } - "`" { return rbs_next_token(state, tOPERATOR); } - "`" [^ :\x00] [^`\x00]* "`" { return rbs_next_token(state, tQIDENT); } - "->" { return rbs_next_token(state, pARROW); } - "=>" { return rbs_next_token(state, pFATARROW); } - "=" { return rbs_next_token(state, pEQ); } - ":" { return rbs_next_token(state, pCOLON); } - "::" { return rbs_next_token(state, pCOLON2); } - "<" { return rbs_next_token(state, pLT); } - "[]" { return rbs_next_token(state, pAREF_OPR); } - operator { return rbs_next_token(state, tOPERATOR); } + "(" { return rbs_next_token(lexer, pLPAREN); } + ")" { return rbs_next_token(lexer, pRPAREN); } + "[" { return rbs_next_token(lexer, pLBRACKET); } + "]" { return rbs_next_token(lexer, pRBRACKET); } + "{" { return rbs_next_token(lexer, pLBRACE); } + "}" { return rbs_next_token(lexer, pRBRACE); } + "," { return rbs_next_token(lexer, pCOMMA); } + "|" { return rbs_next_token(lexer, pBAR); } + "^" { return rbs_next_token(lexer, pHAT); } + "&" { return rbs_next_token(lexer, pAMP); } + "?" { return rbs_next_token(lexer, pQUESTION); } + "*" { return rbs_next_token(lexer, pSTAR); } + "**" { return rbs_next_token(lexer, pSTAR2); } + "." { return rbs_next_token(lexer, pDOT); } + "..." { return rbs_next_token(lexer, pDOT3); } + "`" { return rbs_next_token(lexer, tOPERATOR); } + "`" [^ :\x00] [^`\x00]* "`" { return rbs_next_token(lexer, tQIDENT); } + "->" { return rbs_next_token(lexer, pARROW); } + "=>" { return rbs_next_token(lexer, pFATARROW); } + "=" { return rbs_next_token(lexer, pEQ); } + ":" { return rbs_next_token(lexer, pCOLON); } + "::" { return rbs_next_token(lexer, pCOLON2); } + "<" { return rbs_next_token(lexer, pLT); } + "[]" { return rbs_next_token(lexer, pAREF_OPR); } + operator { return rbs_next_token(lexer, tOPERATOR); } number = [0-9] [0-9_]*; - ("-"|"+")? number { return rbs_next_token(state, tINTEGER); } + ("-"|"+")? number { return rbs_next_token(lexer, tINTEGER); } - "%a{" [^}\x00]* "}" { return rbs_next_token(state, tANNOTATION); } - "%a(" [^)\x00]* ")" { return rbs_next_token(state, tANNOTATION); } - "%a[" [^\]\x00]* "]" { return rbs_next_token(state, tANNOTATION); } - "%a|" [^|\x00]* "|" { return rbs_next_token(state, tANNOTATION); } - "%a<" [^>\x00]* ">" { return rbs_next_token(state, tANNOTATION); } + "%a{" [^}\x00]* "}" { return rbs_next_token(lexer, tANNOTATION); } + "%a(" [^)\x00]* ")" { return rbs_next_token(lexer, tANNOTATION); } + "%a[" [^\]\x00]* "]" { return rbs_next_token(lexer, tANNOTATION); } + "%a|" [^|\x00]* "|" { return rbs_next_token(lexer, tANNOTATION); } + "%a<" [^>\x00]* ">" { return rbs_next_token(lexer, tANNOTATION); } "#" (. \ [\x00])* { return rbs_next_token( - state, - state->first_token_of_line ? tLINECOMMENT : tCOMMENT + lexer, + lexer->first_token_of_line ? tLINECOMMENT : tCOMMENT ); } - "alias" { return rbs_next_token(state, kALIAS); } - "attr_accessor" { return rbs_next_token(state, kATTRACCESSOR); } - "attr_reader" { return rbs_next_token(state, kATTRREADER); } - "attr_writer" { return rbs_next_token(state, kATTRWRITER); } - "bool" { return rbs_next_token(state, kBOOL); } - "bot" { return rbs_next_token(state, kBOT); } - "class" { return rbs_next_token(state, kCLASS); } - "def" { return rbs_next_token(state, kDEF); } - "end" { return rbs_next_token(state, kEND); } - "extend" { return rbs_next_token(state, kEXTEND); } - "false" { return rbs_next_token(state, kFALSE); } - "in" { return rbs_next_token(state, kIN); } - "include" { return rbs_next_token(state, kINCLUDE); } - "instance" { return rbs_next_token(state, kINSTANCE); } - "interface" { return rbs_next_token(state, kINTERFACE); } - "module" { return rbs_next_token(state, kMODULE); } - "nil" { return rbs_next_token(state, kNIL); } - "out" { return rbs_next_token(state, kOUT); } - "prepend" { return rbs_next_token(state, kPREPEND); } - "private" { return rbs_next_token(state, kPRIVATE); } - "public" { return rbs_next_token(state, kPUBLIC); } - "self" { return rbs_next_token(state, kSELF); } - "singleton" { return rbs_next_token(state, kSINGLETON); } - "top" { return rbs_next_token(state, kTOP); } - "true" { return rbs_next_token(state, kTRUE); } - "type" { return rbs_next_token(state, kTYPE); } - "unchecked" { return rbs_next_token(state, kUNCHECKED); } - "untyped" { return rbs_next_token(state, kUNTYPED); } - "void" { return rbs_next_token(state, kVOID); } - "use" { return rbs_next_token(state, kUSE); } - "as" { return rbs_next_token(state, kAS); } - "__todo__" { return rbs_next_token(state, k__TODO__); } + "alias" { return rbs_next_token(lexer, kALIAS); } + "attr_accessor" { return rbs_next_token(lexer, kATTRACCESSOR); } + "attr_reader" { return rbs_next_token(lexer, kATTRREADER); } + "attr_writer" { return rbs_next_token(lexer, kATTRWRITER); } + "bool" { return rbs_next_token(lexer, kBOOL); } + "bot" { return rbs_next_token(lexer, kBOT); } + "class" { return rbs_next_token(lexer, kCLASS); } + "def" { return rbs_next_token(lexer, kDEF); } + "end" { return rbs_next_token(lexer, kEND); } + "extend" { return rbs_next_token(lexer, kEXTEND); } + "false" { return rbs_next_token(lexer, kFALSE); } + "in" { return rbs_next_token(lexer, kIN); } + "include" { return rbs_next_token(lexer, kINCLUDE); } + "instance" { return rbs_next_token(lexer, kINSTANCE); } + "interface" { return rbs_next_token(lexer, kINTERFACE); } + "module" { return rbs_next_token(lexer, kMODULE); } + "nil" { return rbs_next_token(lexer, kNIL); } + "out" { return rbs_next_token(lexer, kOUT); } + "prepend" { return rbs_next_token(lexer, kPREPEND); } + "private" { return rbs_next_token(lexer, kPRIVATE); } + "public" { return rbs_next_token(lexer, kPUBLIC); } + "self" { return rbs_next_token(lexer, kSELF); } + "singleton" { return rbs_next_token(lexer, kSINGLETON); } + "top" { return rbs_next_token(lexer, kTOP); } + "true" { return rbs_next_token(lexer, kTRUE); } + "type" { return rbs_next_token(lexer, kTYPE); } + "unchecked" { return rbs_next_token(lexer, kUNCHECKED); } + "untyped" { return rbs_next_token(lexer, kUNTYPED); } + "void" { return rbs_next_token(lexer, kVOID); } + "use" { return rbs_next_token(lexer, kUSE); } + "as" { return rbs_next_token(lexer, kAS); } + "__todo__" { return rbs_next_token(lexer, k__TODO__); } unicode_char = "\\u" [0-9a-fA-F]{4}; oct_char = "\\x" [0-9a-f]{1,2}; @@ -103,10 +103,10 @@ rbs_token_t rbsparser_next_token(lexstate *state) { dqstring = ["] (unicode_char | oct_char | hex_char | "\\" [^xu] | [^\\"\x00])* ["]; sqstring = ['] ("\\"['\\] | [^'\x00])* [']; - dqstring { return rbs_next_token(state, tDQSTRING); } - sqstring { return rbs_next_token(state, tSQSTRING); } - ":" dqstring { return rbs_next_token(state, tDQSYMBOL); } - ":" sqstring { return rbs_next_token(state, tSQSYMBOL); } + dqstring { return rbs_next_token(lexer, tDQSTRING); } + sqstring { return rbs_next_token(lexer, tSQSTRING); } + ":" dqstring { return rbs_next_token(lexer, tDQSYMBOL); } + ":" sqstring { return rbs_next_token(lexer, tSQSYMBOL); } identifier = [a-zA-Z_] word* [!?=]?; symbol_opr = ":|" | ":&" | ":/" | ":%" | ":~" | ":`" | ":^" @@ -119,29 +119,29 @@ rbs_token_t rbsparser_next_token(lexstate *state) { | [~*$?!@\\/;,.=:<>"&'`+] | [^ \t\r\n:;=.,!"$%&()-+~|\\'[\]{}*/<>^\x00]+; - ":" identifier { return rbs_next_token(state, tSYMBOL); } - ":@" identifier { return rbs_next_token(state, tSYMBOL); } - ":@@" identifier { return rbs_next_token(state, tSYMBOL); } - ":$" global_ident { return rbs_next_token(state, tSYMBOL); } - symbol_opr { return rbs_next_token(state, tSYMBOL); } + ":" identifier { return rbs_next_token(lexer, tSYMBOL); } + ":@" identifier { return rbs_next_token(lexer, tSYMBOL); } + ":@@" identifier { return rbs_next_token(lexer, tSYMBOL); } + ":$" global_ident { return rbs_next_token(lexer, tSYMBOL); } + symbol_opr { return rbs_next_token(lexer, tSYMBOL); } - [a-z] word* { return rbs_next_token(state, tLIDENT); } - [A-Z] word* { return rbs_next_token(state, tUIDENT); } - "_" [a-z0-9_] word* { return rbs_next_token(state, tULLIDENT); } - "_" [A-Z] word* { return rbs_next_token(state, tULIDENT); } - "_" { return rbs_next_token(state, tULLIDENT); } - [a-zA-Z_] word* "!" { return rbs_next_token(state, tBANGIDENT); } - [a-zA-Z_] word* "=" { return rbs_next_token(state, tEQIDENT); } + [a-z] word* { return rbs_next_token(lexer, tLIDENT); } + [A-Z] word* { return rbs_next_token(lexer, tUIDENT); } + "_" [a-z0-9_] word* { return rbs_next_token(lexer, tULLIDENT); } + "_" [A-Z] word* { return rbs_next_token(lexer, tULIDENT); } + "_" { return rbs_next_token(lexer, tULLIDENT); } + [a-zA-Z_] word* "!" { return rbs_next_token(lexer, tBANGIDENT); } + [a-zA-Z_] word* "=" { return rbs_next_token(lexer, tEQIDENT); } - "@" [a-zA-Z_] word* { return rbs_next_token(state, tAIDENT); } - "@@" [a-zA-Z_] word* { return rbs_next_token(state, tA2IDENT); } + "@" [a-zA-Z_] word* { return rbs_next_token(lexer, tAIDENT); } + "@@" [a-zA-Z_] word* { return rbs_next_token(lexer, tA2IDENT); } - "$" global_ident { return rbs_next_token(state, tGIDENT); } + "$" global_ident { return rbs_next_token(lexer, tGIDENT); } skip = ([ \t]+|[\r\n]); - skip { return rbs_next_token(state, tTRIVIA); } - "\x00" { return rbs_next_eof_token(state); } - * { return rbs_next_token(state, ErrorToken); } + skip { return rbs_next_token(lexer, tTRIVIA); } + "\x00" { return rbs_next_eof_token(lexer); } + * { return rbs_next_token(lexer, ErrorToken); } */ } diff --git a/src/lexstate.c b/src/lexstate.c index 5827dab71..02f654d5f 100644 --- a/src/lexstate.c +++ b/src/lexstate.c @@ -104,84 +104,84 @@ int rbs_token_bytes(rbs_token_t tok) { return RBS_RANGE_BYTES(tok.range); } -unsigned int rbs_peek(lexstate *state) { - if (state->current.char_pos == state->end_pos) { - state->last_char = '\0'; +unsigned int rbs_peek(rbs_lexer_t *lexer) { + if (lexer->current.char_pos == lexer->end_pos) { + lexer->last_char = '\0'; return 0; } else { rbs_string_t str = rbs_string_new( - state->string.start + state->current.byte_pos, - state->string.end + lexer->string.start + lexer->current.byte_pos, + lexer->string.end ); unsigned int c = rbs_utf8_string_to_codepoint(str); - state->last_char = c; + lexer->last_char = c; return c; } } -rbs_token_t rbs_next_token(lexstate *state, enum RBSTokenType type) { +rbs_token_t rbs_next_token(rbs_lexer_t *lexer, enum RBSTokenType type) { rbs_token_t t; t.type = type; - t.range.start = state->start; - t.range.end = state->current; - state->start = state->current; + t.range.start = lexer->start; + t.range.end = lexer->current; + lexer->start = lexer->current; if (type != tTRIVIA) { - state->first_token_of_line = false; + lexer->first_token_of_line = false; } return t; } -rbs_token_t rbs_next_eof_token(lexstate *state) { - if ((size_t) state->current.byte_pos == rbs_string_len(state->string) + 1) { +rbs_token_t rbs_next_eof_token(rbs_lexer_t *lexer) { + if ((size_t) lexer->current.byte_pos == rbs_string_len(lexer->string) + 1) { // End of String rbs_token_t t; t.type = pEOF; - t.range.start = state->start; - t.range.end = state->start; - state->start = state->current; + t.range.start = lexer->start; + t.range.end = lexer->start; + lexer->start = lexer->current; return t; } else { // NULL byte in the middle of the string - return rbs_next_token(state, pEOF); + return rbs_next_token(lexer, pEOF); } } -void rbs_skip(lexstate *state) { - if (!state->last_char) { - rbs_peek(state); +void rbs_skip(rbs_lexer_t *lexer) { + if (!lexer->last_char) { + rbs_peek(lexer); } size_t byte_len; - if (state->last_char == '\0') { + if (lexer->last_char == '\0') { byte_len = 1; } else { - const char *start = state->string.start + state->current.byte_pos; - byte_len = state->encoding->char_width((const uint8_t *) start, (ptrdiff_t) (state->string.end - start)); + const char *start = lexer->string.start + lexer->current.byte_pos; + byte_len = lexer->encoding->char_width((const uint8_t *) start, (ptrdiff_t) (lexer->string.end - start)); } - state->current.char_pos += 1; - state->current.byte_pos += byte_len; + lexer->current.char_pos += 1; + lexer->current.byte_pos += byte_len; - if (state->last_char == '\n') { - state->current.line += 1; - state->current.column = 0; - state->first_token_of_line = true; + if (lexer->last_char == '\n') { + lexer->current.line += 1; + lexer->current.column = 0; + lexer->first_token_of_line = true; } else { - state->current.column += 1; + lexer->current.column += 1; } } -void rbs_skipn(lexstate *state, size_t size) { +void rbs_skipn(rbs_lexer_t *lexer, size_t size) { for (size_t i = 0; i < size; i ++) { - rbs_peek(state); - rbs_skip(state); + rbs_peek(lexer); + rbs_skip(lexer); } } -char *rbs_peek_token(lexstate *state, rbs_token_t tok) { - return (char *) state->string.start + tok.range.start.byte_pos; +char *rbs_peek_token(rbs_lexer_t *lexer, rbs_token_t tok) { + return (char *) lexer->string.start + tok.range.start.byte_pos; } diff --git a/src/parser.c b/src/parser.c index 9338073c0..06073cac0 100644 --- a/src/parser.c +++ b/src/parser.c @@ -22,9 +22,9 @@ #define INTERN_TOKEN(parser, tok) \ rbs_constant_pool_insert_shared_with_encoding( \ &parser->constant_pool, \ - (const uint8_t *) rbs_peek_token(parser->lexstate, tok),\ + (const uint8_t *) rbs_peek_token(parser->rbs_lexer_t, tok),\ rbs_token_bytes(tok), \ - (void *) parser->lexstate->encoding \ + (void *) parser->rbs_lexer_t->encoding \ ) #define KEYWORD_CASES \ @@ -124,7 +124,7 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type); static rbs_string_t rbs_parser_peek_current_token(rbs_parser_t *parser) { rbs_range_t rg = parser->current_token.range; - const char *start = parser->lexstate->string.start + rg.start.byte_pos; + const char *start = parser->rbs_lexer_t->string.start + rg.start.byte_pos; size_t length = rg.end.byte_pos - rg.start.byte_pos; return rbs_string_new(start, start + length); @@ -334,9 +334,9 @@ static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_ static rbs_constant_id_t intern_token_start_end(rbs_parser_t *parser, rbs_token_t start_token, rbs_token_t end_token) { return rbs_constant_pool_insert_shared_with_encoding( &parser->constant_pool, - (const uint8_t *) rbs_peek_token(parser->lexstate, start_token), + (const uint8_t *) rbs_peek_token(parser->rbs_lexer_t, start_token), end_token.range.end.byte_pos - start_token.range.start.byte_pos, - parser->lexstate->encoding + parser->rbs_lexer_t->encoding ); } @@ -898,7 +898,7 @@ static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields) { */ NODISCARD static bool parse_symbol(rbs_parser_t *parser, rbs_location_t *location, rbs_types_literal_t **symbol) { - size_t offset_bytes = parser->lexstate->encoding->char_width((const uint8_t *) ":", (size_t) 1); + size_t offset_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) ":", (size_t) 1); size_t bytes = rbs_token_bytes(parser->current_token) - offset_bytes; rbs_ast_symbol_t *literal; @@ -908,7 +908,7 @@ static bool parse_symbol(rbs_parser_t *parser, rbs_location_t *location, rbs_typ case tSYMBOL: { rbs_location_t *symbolLoc = rbs_location_current_token(parser); - char *buffer = rbs_peek_token(parser->lexstate, parser->current_token); + char *buffer = rbs_peek_token(parser->rbs_lexer_t, parser->current_token); rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared( &parser->constant_pool, (const uint8_t *) buffer+offset_bytes, @@ -1162,7 +1162,7 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) { return true; } case tUIDENT: { - const char *name_str = rbs_peek_token(parser->lexstate, parser->current_token); + const char *name_str = rbs_peek_token(parser->rbs_lexer_t, parser->current_token); size_t name_len = rbs_token_bytes(parser->current_token); rbs_constant_id_t name = rbs_constant_pool_find(&parser->constant_pool, (const uint8_t *) name_str, name_len); @@ -1588,12 +1588,12 @@ static bool parse_annotation(rbs_parser_t *parser, rbs_ast_annotation_t **annota rbs_range_t rg = parser->current_token.range; size_t offset_bytes = - parser->lexstate->encoding->char_width((const uint8_t *) "%", (size_t) 1) + - parser->lexstate->encoding->char_width((const uint8_t *) "a", (size_t) 1); + parser->rbs_lexer_t->encoding->char_width((const uint8_t *) "%", (size_t) 1) + + parser->rbs_lexer_t->encoding->char_width((const uint8_t *) "a", (size_t) 1); rbs_string_t str = rbs_string_new( - parser->lexstate->string.start + rg.start.byte_pos + offset_bytes, - parser->lexstate->string.end + parser->rbs_lexer_t->string.start + rg.start.byte_pos + offset_bytes, + parser->rbs_lexer_t->string.end ); unsigned int open_char = rbs_utf8_string_to_codepoint(str); @@ -1620,8 +1620,8 @@ static bool parse_annotation(rbs_parser_t *parser, rbs_ast_annotation_t **annota return false; } - size_t open_bytes = parser->lexstate->encoding->char_width((const uint8_t *) &open_char, (size_t) 1); - size_t close_bytes = parser->lexstate->encoding->char_width((const uint8_t *) &close_char, (size_t) 1); + size_t open_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) &open_char, (size_t) 1); + size_t close_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) &close_char, (size_t) 1); rbs_string_t current_token = rbs_parser_peek_current_token(parser); size_t total_offset = offset_bytes + open_bytes; @@ -1686,9 +1686,9 @@ static bool parse_method_name(rbs_parser_t *parser, rbs_range_t *range, rbs_ast_ rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared_with_encoding( &parser->constant_pool, - (const uint8_t *) parser->lexstate->string.start + range->start.byte_pos, + (const uint8_t *) parser->rbs_lexer_t->string.start + range->start.byte_pos, range->end.byte_pos - range->start.byte_pos, - parser->lexstate->encoding + parser->rbs_lexer_t->encoding ); rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, *range); @@ -3100,8 +3100,8 @@ static bool parse_use_directive(rbs_parser_t *parser, rbs_ast_directives_use_t * } static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_t *com) { - size_t hash_bytes = parser->lexstate->encoding->char_width((const uint8_t *) "#", (size_t) 1); - size_t space_bytes = parser->lexstate->encoding->char_width((const uint8_t *) " ", (size_t) 1); + size_t hash_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) "#", (size_t) 1); + size_t space_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) " ", (size_t) 1); rbs_buffer_t rbs_buffer; rbs_buffer_init(&parser->allocator, &rbs_buffer); @@ -3109,12 +3109,12 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_ for (size_t i = 0; i < com->line_count; i++) { rbs_token_t tok = com->tokens[i]; - const char *comment_start = parser->lexstate->string.start + tok.range.start.byte_pos + hash_bytes; + const char *comment_start = parser->rbs_lexer_t->string.start + tok.range.start.byte_pos + hash_bytes; size_t comment_bytes = RBS_RANGE_BYTES(tok.range) - hash_bytes; rbs_string_t str = rbs_string_new( comment_start, - parser->lexstate->string.end + parser->rbs_lexer_t->string.end ); unsigned char c = rbs_utf8_string_to_codepoint(str); @@ -3311,7 +3311,7 @@ void rbs_parser_advance(rbs_parser_t *parser) { break; } - parser->next_token3 = rbsparser_next_token(parser->lexstate); + parser->next_token3 = rbs_lexer_next_token(parser->rbs_lexer_t); if (parser->next_token3.type == tCOMMENT) { // skip @@ -3346,8 +3346,8 @@ rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line } } -lexstate *rbs_lexer_new(rbs_allocator_t *allocator, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { - lexstate *lexer = rbs_allocator_alloc(allocator, lexstate); +rbs_lexer_t *rbs_lexer_new(rbs_allocator_t *allocator, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { + rbs_lexer_t *lexer = rbs_allocator_alloc(allocator, rbs_lexer_t); rbs_position_t start_position = (rbs_position_t) { .byte_pos = 0, @@ -3356,7 +3356,7 @@ lexstate *rbs_lexer_new(rbs_allocator_t *allocator, rbs_string_t string, const r .column = 0, }; - *lexer = (lexstate) { + *lexer = (rbs_lexer_t) { .string = string, .start_pos = start_pos, .end_pos = end_pos, @@ -3378,11 +3378,11 @@ rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding rbs_allocator_t allocator; rbs_allocator_init(&allocator); - lexstate *lexer = rbs_lexer_new(&allocator, string, encoding, start_pos, end_pos); + rbs_lexer_t *lexer = rbs_lexer_new(&allocator, string, encoding, start_pos, end_pos); rbs_parser_t *parser = rbs_allocator_alloc(&allocator, rbs_parser_t); *parser = (rbs_parser_t) { - .lexstate = lexer, + .rbs_lexer_t = lexer, .current_token = NullToken, .next_token = NullToken, From 91dfbbda3cc43cdc4f47048abb9de597a15c1ada Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 2 Apr 2025 22:30:36 +0800 Subject: [PATCH 104/111] Always define the `is_power_of_two` function (#36) It was only used in `assert` macros, so it only needs to be defined in debug mode. But since `rbs_assert` is a function, `is_power_of_two` always needs to be defined. Without this change, Sorbet fails to build with `c-api`. --- src/util/rbs_constant_pool.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/util/rbs_constant_pool.c b/src/util/rbs_constant_pool.c index 8df73c687..e740e8dea 100644 --- a/src/util/rbs_constant_pool.c +++ b/src/util/rbs_constant_pool.c @@ -37,12 +37,9 @@ next_power_of_two(uint32_t v) { return v; } -#ifndef NDEBUG -static bool -is_power_of_two(uint32_t size) { +static bool is_power_of_two(uint32_t size) { return (size & (size - 1)) == 0; } -#endif /** * Resize a constant pool to a given capacity. From ed97906ea99c9b7696b03de990eb2920726b2ae3 Mon Sep 17 00:00:00 2001 From: Max Bernstein Date: Thu, 3 Apr 2025 12:42:19 -0400 Subject: [PATCH 105/111] Make it easier to switch between allocator implementations Now we can change the macro (back) to &parser->allocator if need be. --- src/parser.c | 464 ++++++++++++++++++++++++++------------------------- 1 file changed, 233 insertions(+), 231 deletions(-) diff --git a/src/parser.c b/src/parser.c index 06073cac0..09ef1fc28 100644 --- a/src/parser.c +++ b/src/parser.c @@ -80,6 +80,8 @@ #define RESET_TABLE_P(table) (table->size == 0) +#define ALLOCATOR() &parser->allocator + typedef struct { rbs_node_list_t *required_positionals; rbs_node_list_t *optional_positionals; @@ -112,7 +114,7 @@ static bool rbs_is_untyped_params(method_params *params) { * @return New RBS::Location object. * */ static rbs_location_t *rbs_location_current_token(rbs_parser_t *parser) { - return rbs_location_new(&parser->allocator, parser->current_token.range); + return rbs_location_new(ALLOCATOR(), parser->current_token.range); } static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional); @@ -166,7 +168,7 @@ static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, rbs_range_t parser_advance_no_gap(parser); } - rbs_node_list_t *path = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *path = rbs_node_list_new(ALLOCATOR()); while ( parser->current_token.type == tUIDENT @@ -175,8 +177,8 @@ static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, rbs_range_t && parser->next_token.range.end.byte_pos == parser->next_token2.range.start.byte_pos ) { rbs_constant_id_t symbol_value = INTERN_TOKEN(parser, parser->current_token); - rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, parser->next_token.range); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, symbol_value); + rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), parser->next_token.range); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, symbol_value); rbs_node_list_append(path, (rbs_node_t *)symbol); rbs_parser_advance(parser); @@ -187,8 +189,8 @@ static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, rbs_range_t .start = rg->start, .end = parser->current_token.range.end }; - rbs_location_t *loc = rbs_location_new(&parser->allocator, namespace_range); - rbs_namespace_t *namespace = rbs_namespace_new(&parser->allocator, loc, path, absolute); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), namespace_range); + rbs_namespace_t *namespace = rbs_namespace_new(ALLOCATOR(), loc, path, absolute); switch (parser->current_token.type) { case tLIDENT: @@ -211,8 +213,8 @@ static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, rbs_range_t rbs_location_t *symbolLoc = rbs_location_current_token(parser); rbs_constant_id_t name = INTERN_TOKEN(parser, parser->current_token); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, name); - *type_name = rbs_type_name_new(&parser->allocator, rbs_location_new(&parser->allocator, *rg), namespace, symbol); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, name); + *type_name = rbs_type_name_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), *rg), namespace, symbol); return true; } @@ -296,11 +298,11 @@ static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_ if (parser->next_token.type == pCOMMA || parser->next_token.type == pRPAREN) { rbs_range_t param_range = type_range; - rbs_location_t *loc = rbs_location_new(&parser->allocator, param_range); - rbs_loc_alloc_children(&parser->allocator, loc, 1); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), param_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 1); rbs_loc_add_optional_child(loc, INTERN("name"), NULL_RANGE); - *function_param = rbs_types_function_param_new(&parser->allocator, loc, type, NULL); + *function_param = rbs_types_function_param_new(ALLOCATOR(), loc, type, NULL); return true; } else { rbs_range_t name_range = parser->next_token.range; @@ -317,16 +319,16 @@ static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_ return false; } - rbs_string_t unquoted_str = rbs_unquote_string(&parser->allocator, rbs_parser_peek_current_token(parser)); + rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), rbs_parser_peek_current_token(parser)); rbs_location_t *symbolLoc = rbs_location_current_token(parser); rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_str); - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, constant_id); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, constant_id); - rbs_location_t *loc = rbs_location_new(&parser->allocator, param_range); - rbs_loc_alloc_children(&parser->allocator, loc, 1); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), param_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 1); rbs_loc_add_optional_child(loc, INTERN("name"), name_range); - *function_param = rbs_types_function_param_new(&parser->allocator, loc, type, name); + *function_param = rbs_types_function_param_new(ALLOCATOR(), loc, type, name); return true; } } @@ -352,14 +354,14 @@ static bool parse_keyword_key(rbs_parser_t *parser, rbs_ast_symbol_t **key) { if (parser->next_token.type == pQUESTION) { *key = rbs_ast_symbol_new( - &parser->allocator, + ALLOCATOR(), symbolLoc, &parser->constant_pool, intern_token_start_end(parser, parser->current_token, parser->next_token) ); rbs_parser_advance(parser); } else { - *key = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); + *key = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); } return true; @@ -378,7 +380,7 @@ static bool parse_keyword(rbs_parser_t *parser, rbs_hash_t *keywords, rbs_hash_t return false; } else { rbs_location_t *loc = rbs_location_current_token(parser); - rbs_hash_set(memo, (rbs_node_t *) key, (rbs_node_t *) rbs_ast_bool_new(&parser->allocator, loc, true)); + rbs_hash_set(memo, (rbs_node_t *) key, (rbs_node_t *) rbs_ast_bool_new(ALLOCATOR(), loc, true)); } ADVANCE_ASSERT(parser, pCOLON); @@ -467,7 +469,7 @@ static bool parse_params(rbs_parser_t *parser, method_params *params) { return true; } - rbs_hash_t *memo = rbs_hash_new(&parser->allocator); + rbs_hash_t *memo = rbs_hash_new(ALLOCATOR()); while (true) { switch (parser->next_token.type) { @@ -633,8 +635,8 @@ static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional) { if (parser->next_token.type == pQUESTION) { rbs_parser_advance(parser); rg.end = parser->current_token.range.end; - rbs_location_t *location = rbs_location_new(&parser->allocator, rg); - *optional = (rbs_node_t *) rbs_types_optional_new(&parser->allocator, location, type); + rbs_location_t *location = rbs_location_new(ALLOCATOR(), rg); + *optional = (rbs_node_t *) rbs_types_optional_new(ALLOCATOR(), location, type); } else { *optional = type; } @@ -695,7 +697,7 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse function_range.start = parser->current_token.range.start; method_params params; - initialize_method_params(¶ms, &parser->allocator); + initialize_method_params(¶ms, ALLOCATOR()); if (parser->next_token.type == pLPAREN) { rbs_parser_advance(parser); @@ -725,7 +727,7 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse rbs_parser_advance(parser); method_params block_params; - initialize_method_params(&block_params, &parser->allocator); + initialize_method_params(&block_params, ALLOCATOR()); if (parser->next_token.type == pLPAREN) { rbs_parser_advance(parser); @@ -742,12 +744,12 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse rbs_node_t *block_function = NULL; function_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, function_range); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), function_range); if (rbs_is_untyped_params(&block_params)) { - block_function = (rbs_node_t *) rbs_types_untyped_function_new(&parser->allocator, loc, block_return_type); + block_function = (rbs_node_t *) rbs_types_untyped_function_new(ALLOCATOR(), loc, block_return_type); } else { block_function = (rbs_node_t *) rbs_types_function_new( - &parser->allocator, + ALLOCATOR(), loc, block_params.required_positionals, block_params.optional_positionals, @@ -760,7 +762,7 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse ); } - block = rbs_types_block_new(&parser->allocator, loc, block_function, required, self_type); + block = rbs_types_block_new(ALLOCATOR(), loc, block_function, required, self_type); ADVANCE_ASSERT(parser, pRBRACE); } @@ -770,12 +772,12 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse CHECK_PARSE(parse_optional(parser, &type)); function_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, function_range); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), function_range); if (rbs_is_untyped_params(¶ms)) { - function = (rbs_node_t *) rbs_types_untyped_function_new(&parser->allocator, loc, type); + function = (rbs_node_t *) rbs_types_untyped_function_new(ALLOCATOR(), loc, type); } else { function = (rbs_node_t *) rbs_types_function_new( - &parser->allocator, + ALLOCATOR(), loc, params.required_positionals, params.optional_positionals, @@ -800,12 +802,12 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse NODISCARD static bool parse_proc_type(rbs_parser_t *parser, rbs_types_proc_t **proc) { rbs_position_t start = parser->current_token.range.start; - parse_function_result *result = rbs_allocator_alloc(&parser->allocator, parse_function_result); + parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result); CHECK_PARSE(parse_function(parser, true, &result)); rbs_position_t end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, (rbs_range_t) { .start = start, .end = end }); - *proc = rbs_types_proc_new(&parser->allocator, loc, result->function, result->block, result->function_self_type); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), (rbs_range_t) { .start = start, .end = end }); + *proc = rbs_types_proc_new(ALLOCATOR(), loc, result->function, result->block, result->function_self_type); return true; } @@ -827,7 +829,7 @@ static void check_key_duplication(rbs_parser_t *parser, rbs_hash_t *fields, rbs_ */ NODISCARD static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields) { - *fields = rbs_hash_new(&parser->allocator); + *fields = rbs_hash_new(ALLOCATOR()); if (parser->next_token.type == pRBRACE) return true; @@ -879,8 +881,8 @@ static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields) { CHECK_PARSE(rbs_parse_type(parser, &type)); field_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, field_range); - rbs_hash_set(*fields, (rbs_node_t *) key, (rbs_node_t *) rbs_types_record_field_type_new(&parser->allocator, loc, type, required)); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), field_range); + rbs_hash_set(*fields, (rbs_node_t *) key, (rbs_node_t *) rbs_types_record_field_type_new(ALLOCATOR(), loc, type, required)); if (parser_advance_if(parser, pCOMMA)) { if (parser->next_token.type == pRBRACE) { @@ -914,7 +916,7 @@ static bool parse_symbol(rbs_parser_t *parser, rbs_location_t *location, rbs_typ (const uint8_t *) buffer+offset_bytes, bytes ); - literal = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, constant_id); + literal = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, constant_id); break; } case tDQSYMBOL: @@ -924,11 +926,11 @@ static bool parse_symbol(rbs_parser_t *parser, rbs_location_t *location, rbs_typ rbs_string_t symbol = rbs_string_new(current_token.start + offset_bytes, current_token.end); - rbs_string_t unquoted_symbol = rbs_unquote_string(&parser->allocator, symbol); + rbs_string_t unquoted_symbol = rbs_unquote_string(ALLOCATOR(), symbol); rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_symbol); - literal = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, constant_id); + literal = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, constant_id); break; } default: @@ -936,7 +938,7 @@ static bool parse_symbol(rbs_parser_t *parser, rbs_location_t *location, rbs_typ return false; } - *symbol = rbs_types_literal_new(&parser->allocator, location, (rbs_node_t *) literal); + *symbol = rbs_types_literal_new(ALLOCATOR(), location, (rbs_node_t *) literal); return true; } @@ -957,7 +959,7 @@ static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node rbs_type_name_t *type_name = NULL; CHECK_PARSE(parse_type_name(parser, expected_kind, &name_range, &type_name)); - rbs_node_list_t *types = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *types = rbs_node_list_new(ALLOCATOR()); TypeNameKind kind; if (parser->current_token.type == tUIDENT) { @@ -987,17 +989,17 @@ static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node .end = rbs_nonnull_pos_or(args_range.end, name_range.end), }; - rbs_location_t *loc = rbs_location_new(&parser->allocator, type_range); - rbs_loc_alloc_children(&parser->allocator, loc, 2); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), type_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); if (kind == CLASS_NAME) { - *type = (rbs_node_t *) rbs_types_class_instance_new(&parser->allocator, loc, type_name, types); + *type = (rbs_node_t *) rbs_types_class_instance_new(ALLOCATOR(), loc, type_name, types); } else if (kind == INTERFACE_NAME) { - *type = (rbs_node_t *) rbs_types_interface_new(&parser->allocator, loc, type_name, types); + *type = (rbs_node_t *) rbs_types_interface_new(ALLOCATOR(), loc, type_name, types); } else if (kind == ALIAS_NAME) { - *type = (rbs_node_t *) rbs_types_alias_new(&parser->allocator, loc, type_name, types); + *type = (rbs_node_t *) rbs_types_alias_new(ALLOCATOR(), loc, type_name, types); } return true; @@ -1022,11 +1024,11 @@ static bool parse_singleton_type(rbs_parser_t *parser, rbs_types_class_singleton ADVANCE_ASSERT(parser, pRPAREN); type_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, type_range); - rbs_loc_alloc_children(&parser->allocator, loc, 1); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), type_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 1); rbs_loc_add_required_child(loc, INTERN("name"), name_range); - *singleton = rbs_types_class_singleton_new(&parser->allocator, loc, type_name); + *singleton = rbs_types_class_singleton_new(ALLOCATOR(), loc, type_name); return true; } @@ -1075,52 +1077,52 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) { } case kBOOL: { rbs_location_t *loc = rbs_location_current_token(parser); - *type = (rbs_node_t *) rbs_types_bases_bool_new(&parser->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_bool_new(ALLOCATOR(), loc); return true; } case kBOT: { rbs_location_t *loc = rbs_location_current_token(parser); - *type = (rbs_node_t *) rbs_types_bases_bottom_new(&parser->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_bottom_new(ALLOCATOR(), loc); return true; } case kCLASS: { rbs_location_t *loc = rbs_location_current_token(parser); - *type = (rbs_node_t *) rbs_types_bases_class_new(&parser->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_class_new(ALLOCATOR(), loc); return true; } case kINSTANCE: { rbs_location_t *loc = rbs_location_current_token(parser); - *type = (rbs_node_t *) rbs_types_bases_instance_new(&parser->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_instance_new(ALLOCATOR(), loc); return true; } case kNIL: { rbs_location_t *loc = rbs_location_current_token(parser); - *type = (rbs_node_t *) rbs_types_bases_nil_new(&parser->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_nil_new(ALLOCATOR(), loc); return true; } case kSELF: { rbs_location_t *loc = rbs_location_current_token(parser); - *type = (rbs_node_t *) rbs_types_bases_self_new(&parser->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_self_new(ALLOCATOR(), loc); return true; } case kTOP: { rbs_location_t *loc = rbs_location_current_token(parser); - *type = (rbs_node_t *) rbs_types_bases_top_new(&parser->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_top_new(ALLOCATOR(), loc); return true; } case kVOID: { rbs_location_t *loc = rbs_location_current_token(parser); - *type = (rbs_node_t *) rbs_types_bases_void_new(&parser->allocator, loc); + *type = (rbs_node_t *) rbs_types_bases_void_new(ALLOCATOR(), loc); return true; } case kUNTYPED: { rbs_location_t *loc = rbs_location_current_token(parser); - *type = (rbs_node_t *) rbs_types_bases_any_new(&parser->allocator, loc, false); + *type = (rbs_node_t *) rbs_types_bases_any_new(ALLOCATOR(), loc, false); return true; } case k__TODO__: { rbs_location_t *loc = rbs_location_current_token(parser); - *type = (rbs_node_t *) rbs_types_bases_any_new(&parser->allocator, loc, true); + *type = (rbs_node_t *) rbs_types_bases_any_new(ALLOCATOR(), loc, true); return true; } case tINTEGER: { @@ -1129,27 +1131,27 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) { rbs_string_t string = rbs_parser_peek_current_token(parser); rbs_string_t stripped_string = rbs_string_strip_whitespace( &string); - rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(&parser->allocator, loc, stripped_string); - *type = (rbs_node_t *) rbs_types_literal_new(&parser->allocator, loc, literal); + rbs_node_t *literal = (rbs_node_t *) rbs_ast_integer_new(ALLOCATOR(), loc, stripped_string); + *type = (rbs_node_t *) rbs_types_literal_new(ALLOCATOR(), loc, literal); return true; } case kTRUE: { rbs_location_t *loc = rbs_location_current_token(parser); - *type = (rbs_node_t *) rbs_types_literal_new(&parser->allocator, loc, (rbs_node_t *) rbs_ast_bool_new(&parser->allocator, loc, true)); + *type = (rbs_node_t *) rbs_types_literal_new(ALLOCATOR(), loc, (rbs_node_t *) rbs_ast_bool_new(ALLOCATOR(), loc, true)); return true; } case kFALSE: { rbs_location_t *loc = rbs_location_current_token(parser); - *type = (rbs_node_t *) rbs_types_literal_new(&parser->allocator, loc, (rbs_node_t *) rbs_ast_bool_new(&parser->allocator, loc, false)); + *type = (rbs_node_t *) rbs_types_literal_new(ALLOCATOR(), loc, (rbs_node_t *) rbs_ast_bool_new(ALLOCATOR(), loc, false)); return true; } case tSQSTRING: case tDQSTRING: { rbs_location_t *loc = rbs_location_current_token(parser); - rbs_string_t unquoted_str = rbs_unquote_string(&parser->allocator, rbs_parser_peek_current_token(parser)); - rbs_node_t *literal = (rbs_node_t *) rbs_ast_string_new(&parser->allocator, loc, unquoted_str); - *type = (rbs_node_t *) rbs_types_literal_new(&parser->allocator, loc, literal); + rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), rbs_parser_peek_current_token(parser)); + rbs_node_t *literal = (rbs_node_t *) rbs_ast_string_new(ALLOCATOR(), loc, unquoted_str); + *type = (rbs_node_t *) rbs_types_literal_new(ALLOCATOR(), loc, literal); return true; } case tSYMBOL: @@ -1169,8 +1171,8 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) { if (parser_typevar_member(parser, name)) { rbs_location_t *loc = rbs_location_current_token(parser); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&parser->allocator, loc, &parser->constant_pool, name); - *type = (rbs_node_t *) rbs_types_variable_new(&parser->allocator, loc, symbol); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), loc, &parser->constant_pool, name); + *type = (rbs_node_t *) rbs_types_variable_new(ALLOCATOR(), loc, symbol); return true; } @@ -1193,21 +1195,21 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) { case pLBRACKET: { rbs_range_t rg; rg.start = parser->current_token.range.start; - rbs_node_list_t *types = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *types = rbs_node_list_new(ALLOCATOR()); if (parser->next_token.type != pRBRACKET) { CHECK_PARSE(parse_type_list(parser, pRBRACKET, types)); } ADVANCE_ASSERT(parser, pRBRACKET); rg.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, rg); - *type = (rbs_node_t *) rbs_types_tuple_new(&parser->allocator, loc, types); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), rg); + *type = (rbs_node_t *) rbs_types_tuple_new(ALLOCATOR(), loc, types); return true; } case pAREF_OPR: { rbs_location_t *loc = rbs_location_current_token(parser); - rbs_node_list_t *types = rbs_node_list_new(&parser->allocator); - *type = (rbs_node_t *) rbs_types_tuple_new(&parser->allocator, loc, types); + rbs_node_list_t *types = rbs_node_list_new(ALLOCATOR()); + *type = (rbs_node_t *) rbs_types_tuple_new(ALLOCATOR(), loc, types); return true; } case pLBRACE: { @@ -1216,8 +1218,8 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) { CHECK_PARSE(parse_record_attributes(parser, &fields)); ADVANCE_ASSERT(parser, pRBRACE); rbs_position_t end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, (rbs_range_t) { .start = start, .end = end }); - *type = (rbs_node_t *) rbs_types_record_new(&parser->allocator, loc, fields); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), (rbs_range_t) { .start = start, .end = end }); + *type = (rbs_node_t *) rbs_types_record_new(ALLOCATOR(), loc, fields); return true; } case pHAT: { @@ -1245,7 +1247,7 @@ static bool parse_intersection(rbs_parser_t *parser, rbs_node_t **type) { CHECK_PARSE(parse_optional(parser, &optional)); *type = optional; - rbs_node_list_t *intersection_types = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *intersection_types = rbs_node_list_new(ALLOCATOR()); rbs_node_list_append(intersection_types, optional); while (parser->next_token.type == pAMP) { @@ -1258,8 +1260,8 @@ static bool parse_intersection(rbs_parser_t *parser, rbs_node_t **type) { rg.end = parser->current_token.range.end; if (intersection_types->length > 1) { - rbs_location_t *location = rbs_location_new(&parser->allocator, rg); - *type = (rbs_node_t *) rbs_types_intersection_new(&parser->allocator, location, intersection_types); + rbs_location_t *location = rbs_location_new(ALLOCATOR(), rg); + *type = (rbs_node_t *) rbs_types_intersection_new(ALLOCATOR(), location, intersection_types); } return true; @@ -1272,7 +1274,7 @@ static bool parse_intersection(rbs_parser_t *parser, rbs_node_t **type) { bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type) { rbs_range_t rg; rg.start = parser->next_token.range.start; - rbs_node_list_t *union_types = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *union_types = rbs_node_list_new(ALLOCATOR()); CHECK_PARSE(parse_intersection(parser, type)); @@ -1288,8 +1290,8 @@ bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type) { rg.end = parser->current_token.range.end; if (union_types->length > 1) { - rbs_location_t *location = rbs_location_new(&parser->allocator, rg); - *type = (rbs_node_t *) rbs_types_union_new(&parser->allocator, location, union_types); + rbs_location_t *location = rbs_location_new(ALLOCATOR(), rg); + *type = (rbs_node_t *) rbs_types_union_new(ALLOCATOR(), location, union_types); } return true; @@ -1305,7 +1307,7 @@ bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type) { */ NODISCARD static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module_type_params, rbs_node_list_t **params) { - *params = rbs_node_list_new(&parser->allocator); + *params = rbs_node_list_new(ALLOCATOR()); bool required_param_allowed = true; @@ -1316,7 +1318,7 @@ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module while (true) { bool unchecked = false; - rbs_keyword_t *variance = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("invariant")); + rbs_keyword_t *variance = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("invariant")); rbs_node_t *upper_bound = NULL; rbs_node_t *default_type = NULL; @@ -1335,10 +1337,10 @@ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module if (parser->next_token.type == kIN || parser->next_token.type == kOUT) { switch (parser->next_token.type) { case kIN: - variance = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("contravariant")); + variance = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("contravariant")); break; case kOUT: - variance = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("covariant")); + variance = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("covariant")); break; default: rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error"); @@ -1356,7 +1358,7 @@ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module rbs_string_t string = rbs_parser_peek_current_token(parser); rbs_location_t *nameSymbolLoc = rbs_location_current_token(parser); rbs_constant_id_t id = rbs_constant_pool_insert_string(&parser->constant_pool, string); - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&parser->allocator, nameSymbolLoc, &parser->constant_pool, id); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), nameSymbolLoc, &parser->constant_pool, id); CHECK_PARSE(rbs_parser_insert_typevar(parser, id)); @@ -1388,15 +1390,15 @@ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module param_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, param_range); - rbs_loc_alloc_children(&parser->allocator, loc, 5); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), param_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 5); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("variance"), variance_range); rbs_loc_add_optional_child(loc, INTERN("unchecked"), unchecked_range); rbs_loc_add_optional_child(loc, INTERN("upper_bound"), upper_bound_range); rbs_loc_add_optional_child(loc, INTERN("default"), default_type_range); - rbs_ast_type_param_t *param = rbs_ast_type_param_new(&parser->allocator, loc, name, variance, upper_bound, default_type, unchecked); + rbs_ast_type_param_t *param = rbs_ast_type_param_new(ALLOCATOR(), loc, name, variance, upper_bound, default_type, unchecked); rbs_node_list_append(*params, (rbs_node_t *) param); @@ -1455,7 +1457,7 @@ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type rbs_range_t type_range; type_range.start = parser->next_token.range.start; - parse_function_result *result = rbs_allocator_alloc(&parser->allocator, parse_function_result); + parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result); CHECK_PARSE(parse_function(parser, false, &result)); rg.end = parser->current_token.range.end; @@ -1463,12 +1465,12 @@ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type CHECK_PARSE(parser_pop_typevar_table(parser)); - rbs_location_t *loc = rbs_location_new(&parser->allocator, rg); - rbs_loc_alloc_children(&parser->allocator, loc, 2); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), rg); + rbs_loc_alloc_children(ALLOCATOR(), loc, 2); rbs_loc_add_required_child(loc, INTERN("type"), type_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range); - *method_type = rbs_method_type_new(&parser->allocator, loc, type_params, result->function, result->block); + *method_type = rbs_method_type_new(ALLOCATOR(), loc, type_params, result->function, result->block); return true; } @@ -1483,9 +1485,9 @@ static bool parse_global_decl(rbs_parser_t *parser, rbs_node_list_t *annotations rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, decl_range.start.line); rbs_range_t name_range = parser->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, name_range); + rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), name_range); - rbs_ast_symbol_t *type_name = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); + rbs_ast_symbol_t *type_name = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); ADVANCE_ASSERT(parser, pCOLON); rbs_range_t colon_range = parser->current_token.range; @@ -1494,12 +1496,12 @@ static bool parse_global_decl(rbs_parser_t *parser, rbs_node_list_t *annotations CHECK_PARSE(rbs_parse_type(parser, &type)); decl_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); - rbs_loc_alloc_children(&parser->allocator, loc, 2); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), decl_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - *global = rbs_ast_declarations_global_new(&parser->allocator, loc, type_name, type, comment, annotations); + *global = rbs_ast_declarations_global_new(ALLOCATOR(), loc, type_name, type, comment, annotations); return true; } @@ -1525,12 +1527,12 @@ static bool parse_const_decl(rbs_parser_t *parser, rbs_node_list_t *annotations, decl_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); - rbs_loc_alloc_children(&parser->allocator, loc, 2); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), decl_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); - *constant = rbs_ast_declarations_constant_new(&parser->allocator, loc, type_name, type, comment, annotations); + *constant = rbs_ast_declarations_constant_new(ALLOCATOR(), loc, type_name, type, comment, annotations); return true; } @@ -1565,8 +1567,8 @@ static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rb decl_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); - rbs_loc_alloc_children(&parser->allocator, loc, 4); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), decl_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), params_range); @@ -1576,7 +1578,7 @@ static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rb rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line); - *typealias = rbs_ast_declarations_type_alias_new(&parser->allocator, loc, type_name, type_params, type, annotations, comment); + *typealias = rbs_ast_declarations_type_alias_new(ALLOCATOR(), loc, type_name, type_params, type, annotations, comment); return true; } @@ -1633,7 +1635,7 @@ static bool parse_annotation(rbs_parser_t *parser, rbs_ast_annotation_t **annota rbs_string_t stripped_annotation_str = rbs_string_strip_whitespace(&annotation_str); - *annotation = rbs_ast_annotation_new(&parser->allocator, rbs_location_new(&parser->allocator, rg), stripped_annotation_str); + *annotation = rbs_ast_annotation_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), rg), stripped_annotation_str); return true; } @@ -1691,28 +1693,28 @@ static bool parse_method_name(rbs_parser_t *parser, rbs_range_t *range, rbs_ast_ parser->rbs_lexer_t->encoding ); - rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, *range); - *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, constant_id); + rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), *range); + *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, constant_id); } else { *range = parser->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, *range); - *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), *range); + *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); } return true; case tBANGIDENT: case tEQIDENT: { *range = parser->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, *range); - *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), *range); + *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); return true; } case tQIDENT: { rbs_string_t string = rbs_parser_peek_current_token(parser); - rbs_string_t unquoted_str = rbs_unquote_string(&parser->allocator, string); + rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), string); rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_str); rbs_location_t *symbolLoc = rbs_location_current_token(parser); - *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, constant_id); + *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, constant_id); return true; } @@ -1725,8 +1727,8 @@ static bool parse_method_name(rbs_parser_t *parser, rbs_range_t *range, rbs_ast_ case pAREF_OPR: case tOPERATOR: { *range = parser->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, *range); - *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), *range); + *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); return true; } @@ -1807,14 +1809,14 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce { case kPRIVATE: { visibility_range = parser->current_token.range; - visibility = rbs_keyword_new(&parser->allocator, rbs_location_new(&parser->allocator, visibility_range), INTERN("private")); + visibility = rbs_keyword_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), visibility_range), INTERN("private")); member_range.start = visibility_range.start; rbs_parser_advance(parser); break; } case kPUBLIC: { visibility_range = parser->current_token.range; - visibility = rbs_keyword_new(&parser->allocator, rbs_location_new(&parser->allocator, visibility_range), INTERN("public")); + visibility = rbs_keyword_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), visibility_range), INTERN("public")); member_range.start = visibility_range.start; rbs_parser_advance(parser); break; @@ -1851,12 +1853,12 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce rbs_parser_push_typevar_table(parser, kind != INSTANCE_KIND); - rbs_node_list_t *overloads = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *overloads = rbs_node_list_new(ALLOCATOR()); bool overloading = false; rbs_range_t overloading_range = NULL_RANGE; bool loop = true; while (loop) { - rbs_node_list_t *annotations = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR()); rbs_position_t overload_annot_pos = NullPosition; rbs_range_t overload_range; @@ -1877,8 +1879,8 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce CHECK_PARSE(rbs_parse_method_type(parser, &method_type)); overload_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, overload_range); - rbs_node_t *overload = (rbs_node_t *) rbs_ast_members_method_definition_overload_new(&parser->allocator, loc, annotations, (rbs_node_t *) method_type); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), overload_range); + rbs_node_t *overload = (rbs_node_t *) rbs_ast_members_method_definition_overload_new(ALLOCATOR(), loc, annotations, (rbs_node_t *) method_type); rbs_node_list_append(overloads, overload); member_range.end = parser->current_token.range.end; break; @@ -1914,15 +1916,15 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce rbs_keyword_t *k; switch (kind) { case INSTANCE_KIND: { - k = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("instance")); + k = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("instance")); break; } case SINGLETON_KIND: { - k = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("singleton")); + k = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("singleton")); break; } case INSTANCE_SINGLETON_KIND: { - k = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("singleton_instance")); + k = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("singleton_instance")); break; } default: @@ -1930,15 +1932,15 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce return false; } - rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); - rbs_loc_alloc_children(&parser->allocator, loc, 5); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 5); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range); rbs_loc_add_optional_child(loc, INTERN("overloading"), overloading_range); rbs_loc_add_optional_child(loc, INTERN("visibility"), visibility_range); - *method_definition = rbs_ast_members_method_definition_new(&parser->allocator, loc, name, k, overloads, annotations, comment, overloading, visibility); + *method_definition = rbs_ast_members_method_definition_new(ALLOCATOR(), loc, name, k, overloads, annotations, comment, overloading, visibility); return true; } @@ -2011,7 +2013,7 @@ static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, rbs_po rbs_parser_push_typevar_table(parser, reset_typevar_scope); - rbs_node_list_t *args = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *args = rbs_node_list_new(ALLOCATOR()); rbs_range_t name_range; rbs_range_t args_range = NULL_RANGE; rbs_type_name_t *name = NULL; @@ -2025,8 +2027,8 @@ static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, rbs_po member_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); - rbs_loc_alloc_children(&parser->allocator, loc, 3); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); @@ -2035,13 +2037,13 @@ static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, rbs_po switch (type) { case kINCLUDE: - *mixin_member = (rbs_node_t *) rbs_ast_members_include_new(&parser->allocator, loc, name, args, annotations, comment); + *mixin_member = (rbs_node_t *) rbs_ast_members_include_new(ALLOCATOR(), loc, name, args, annotations, comment); return true; case kEXTEND: - *mixin_member = (rbs_node_t *) rbs_ast_members_extend_new(&parser->allocator, loc, name, args, annotations, comment); + *mixin_member = (rbs_node_t *) rbs_ast_members_extend_new(ALLOCATOR(), loc, name, args, annotations, comment); return true; case kPREPEND: - *mixin_member = (rbs_node_t *) rbs_ast_members_prepend_new(&parser->allocator, loc, name, args, annotations, comment); + *mixin_member = (rbs_node_t *) rbs_ast_members_prepend_new(ALLOCATOR(), loc, name, args, annotations, comment); return true; default: rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error"); @@ -2071,7 +2073,7 @@ static bool parse_alias_member(rbs_parser_t *parser, bool instance_only, rbs_pos rbs_range_t new_kind_range, old_kind_range, new_name_range, old_name_range; if (!instance_only && parser->next_token.type == kSELF) { - kind = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("singleton")); + kind = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("singleton")); new_kind_range.start = parser->next_token.range.start; new_kind_range.end = parser->next_token2.range.end; @@ -2085,7 +2087,7 @@ static bool parse_alias_member(rbs_parser_t *parser, bool instance_only, rbs_pos ADVANCE_ASSERT(parser, pDOT); CHECK_PARSE(parse_method_name(parser, &old_name_range, &old_name)); } else { - kind = rbs_keyword_new(&parser->allocator, rbs_location_current_token(parser), INTERN("instance")); + kind = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("instance")); CHECK_PARSE(parse_method_name(parser, &new_name_range, &new_name)); CHECK_PARSE(parse_method_name(parser, &old_name_range, &old_name)); new_kind_range = NULL_RANGE; @@ -2093,15 +2095,15 @@ static bool parse_alias_member(rbs_parser_t *parser, bool instance_only, rbs_pos } member_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); - rbs_loc_alloc_children(&parser->allocator, loc, 5); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 5); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("new_name"), new_name_range); rbs_loc_add_required_child(loc, INTERN("old_name"), old_name_range); rbs_loc_add_optional_child(loc, INTERN("new_kind"), new_kind_range); rbs_loc_add_optional_child(loc, INTERN("old_kind"), old_kind_range); - *alias_member = rbs_ast_members_alias_new(&parser->allocator, loc, new_name, old_name, kind, annotations, comment); + *alias_member = rbs_ast_members_alias_new(ALLOCATOR(), loc, new_name, old_name, kind, annotations, comment); return true; } @@ -2126,8 +2128,8 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p { case tAIDENT: { rbs_range_t name_range = parser->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, name_range); - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), name_range); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); ADVANCE_ASSERT(parser, pCOLON); rbs_range_t colon_range = parser->current_token.range; @@ -2136,19 +2138,19 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p CHECK_PARSE(rbs_parse_type(parser, &type)); member_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); - rbs_loc_alloc_children(&parser->allocator, loc, 3); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); - *variable_member = (rbs_node_t *)rbs_ast_members_instance_variable_new(&parser->allocator, loc, name, type, comment); + *variable_member = (rbs_node_t *)rbs_ast_members_instance_variable_new(ALLOCATOR(), loc, name, type, comment); return true; } case tA2IDENT: { rbs_range_t name_range = parser->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, name_range); - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), name_range); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); ADVANCE_ASSERT(parser, pCOLON); rbs_range_t colon_range = parser->current_token.range; @@ -2162,13 +2164,13 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p member_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); - rbs_loc_alloc_children(&parser->allocator, loc, 3); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), NULL_RANGE); - *variable_member = (rbs_node_t *) rbs_ast_members_class_variable_new(&parser->allocator, loc, name, type, comment); + *variable_member = (rbs_node_t *) rbs_ast_members_class_variable_new(ALLOCATOR(), loc, name, type, comment); return true; } case kSELF: { @@ -2181,8 +2183,8 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p ADVANCE_ASSERT(parser, tAIDENT); rbs_range_t name_range = parser->current_token.range; - rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, name_range); - rbs_ast_symbol_t *name = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), name_range); + rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); ADVANCE_ASSERT(parser, pCOLON); rbs_range_t colon_range = parser->current_token.range; @@ -2196,13 +2198,13 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p member_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); - rbs_loc_alloc_children(&parser->allocator, loc, 3); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 3); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); rbs_loc_add_optional_child(loc, INTERN("kind"), kind_range); - *variable_member = (rbs_node_t *)rbs_ast_members_class_instance_variable_new(&parser->allocator, loc, name, type, comment); + *variable_member = (rbs_node_t *)rbs_ast_members_class_instance_variable_new(ALLOCATOR(), loc, name, type, comment); return true; } default: @@ -2227,11 +2229,11 @@ static bool parse_visibility_member(rbs_parser_t *parser, rbs_node_list_t *annot switch (parser->current_token.type) { case kPUBLIC: { - *visibility_member = (rbs_node_t *) rbs_ast_members_public_new(&parser->allocator, location); + *visibility_member = (rbs_node_t *) rbs_ast_members_public_new(ALLOCATOR(), location); return true; } case kPRIVATE: { - *visibility_member = (rbs_node_t *) rbs_ast_members_private_new(&parser->allocator, location); + *visibility_member = (rbs_node_t *) rbs_ast_members_private_new(ALLOCATOR(), location); return true; } default: @@ -2268,13 +2270,13 @@ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_ { case kPRIVATE: { visibility_range = parser->current_token.range; - visibility = rbs_keyword_new(&parser->allocator, rbs_location_new(&parser->allocator, visibility_range), INTERN("private")); + visibility = rbs_keyword_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), visibility_range), INTERN("private")); rbs_parser_advance(parser); break; } case kPUBLIC: { visibility_range = parser->current_token.range; - visibility = rbs_keyword_new(&parser->allocator, rbs_location_new(&parser->allocator, visibility_range), INTERN("public")); + visibility = rbs_keyword_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), visibility_range), INTERN("public")); rbs_parser_advance(parser); break; } @@ -2291,8 +2293,8 @@ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_ InstanceSingletonKind is_kind = parse_instance_singleton_kind(parser, false, &kind_range); rbs_keyword_t *kind = rbs_keyword_new( - &parser->allocator, - rbs_location_new(&parser->allocator, keyword_range), + ALLOCATOR(), + rbs_location_new(ALLOCATOR(), keyword_range), INTERN(((is_kind == INSTANCE_KIND) ? "instance" : "singleton")) ); @@ -2300,7 +2302,7 @@ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_ rbs_ast_symbol_t *attr_name; CHECK_PARSE(parse_method_name(parser, &name_range, &attr_name)); - rbs_node_t *ivar_name; // rbs_ast_symbol_t, NULL or rbs_ast_bool_new(&parser->allocator, false) + rbs_node_t *ivar_name; // rbs_ast_symbol_t, NULL or rbs_ast_bool_new(ALLOCATOR(), false) rbs_range_t ivar_range, ivar_name_range; if (parser->next_token.type == pLPAREN) { ADVANCE_ASSERT(parser, pLPAREN); @@ -2308,14 +2310,14 @@ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_ if (parser_advance_if(parser, tAIDENT)) { rbs_location_t *symbolLoc = rbs_location_current_token(parser); - ivar_name = (rbs_node_t *) rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); + ivar_name = (rbs_node_t *) rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); ivar_name_range = parser->current_token.range; } else { rbs_range_t false_range = { .start = parser->current_token.range.start, .end = parser->current_token.range.end }; - ivar_name = (rbs_node_t *) rbs_ast_bool_new(&parser->allocator, rbs_location_new(&parser->allocator, false_range), false); + ivar_name = (rbs_node_t *) rbs_ast_bool_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), false_range), false); ivar_name_range = NULL_RANGE; } @@ -2339,8 +2341,8 @@ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_ member_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); - rbs_loc_alloc_children(&parser->allocator, loc, 7); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 7); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("colon"), colon_range); @@ -2352,13 +2354,13 @@ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_ switch (attr_type) { case kATTRREADER: - *attribute_member = (rbs_node_t *) rbs_ast_members_attr_reader_new(&parser->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); + *attribute_member = (rbs_node_t *) rbs_ast_members_attr_reader_new(ALLOCATOR(), loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); return true; case kATTRWRITER: - *attribute_member = (rbs_node_t *) rbs_ast_members_attr_writer_new(&parser->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); + *attribute_member = (rbs_node_t *) rbs_ast_members_attr_writer_new(ALLOCATOR(), loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); return true; case kATTRACCESSOR: - *attribute_member = (rbs_node_t *) rbs_ast_members_attr_accessor_new(&parser->allocator, loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); + *attribute_member = (rbs_node_t *) rbs_ast_members_attr_accessor_new(ALLOCATOR(), loc, attr_name, type, ivar_name, kind, annotations, comment, visibility); return true; default: rbs_parser_set_error(parser, parser->current_token, false, "Unexpected error"); @@ -2375,10 +2377,10 @@ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_ */ NODISCARD static bool parse_interface_members(rbs_parser_t *parser, rbs_node_list_t **members) { - *members = rbs_node_list_new(&parser->allocator); + *members = rbs_node_list_new(ALLOCATOR()); while (parser->next_token.type != kEND) { - rbs_node_list_t *annotations = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR()); rbs_position_t annot_pos = NullPosition; CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos)); @@ -2450,8 +2452,8 @@ static bool parse_interface_decl(rbs_parser_t *parser, rbs_position_t comment_po CHECK_PARSE(parser_pop_typevar_table(parser)); - rbs_location_t *loc = rbs_location_new(&parser->allocator, member_range); - rbs_loc_alloc_children(&parser->allocator, loc, 4); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("end"), end_range); @@ -2459,7 +2461,7 @@ static bool parse_interface_decl(rbs_parser_t *parser, rbs_position_t comment_po rbs_ast_comment_t *comment = rbs_parser_get_comment(parser, comment_pos.line); - *interface_decl = rbs_ast_declarations_interface_new(&parser->allocator, loc, name, type_params, members, annotations, comment); + *interface_decl = rbs_ast_declarations_interface_new(ALLOCATOR(), loc, name, type_params, members, annotations, comment); return true; } @@ -2482,7 +2484,7 @@ static bool parse_module_self_types(rbs_parser_t *parser, rbs_node_list_t *array CHECK_PARSE(parse_type_name(parser, CLASS_NAME | INTERFACE_NAME, &name_range, &module_name)); self_range.end = name_range.end; - rbs_node_list_t *args = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *args = rbs_node_list_new(ALLOCATOR()); rbs_range_t args_range = NULL_RANGE; if (parser->next_token.type == pLBRACKET) { rbs_parser_advance(parser); @@ -2492,12 +2494,12 @@ static bool parse_module_self_types(rbs_parser_t *parser, rbs_node_list_t *array self_range.end = args_range.end = parser->current_token.range.end; } - rbs_location_t *loc = rbs_location_new(&parser->allocator, self_range); - rbs_loc_alloc_children(&parser->allocator, loc, 2); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), self_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); - rbs_ast_declarations_module_self_t *self_type = rbs_ast_declarations_module_self_new(&parser->allocator, loc, module_name, args); + rbs_ast_declarations_module_self_t *self_type = rbs_ast_declarations_module_self_new(ALLOCATOR(), loc, module_name, args); rbs_node_list_append(array, (rbs_node_t *)self_type); if (parser->next_token.type == pCOMMA) { @@ -2526,10 +2528,10 @@ static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, rbs_p */ NODISCARD static bool parse_module_members(rbs_parser_t *parser, rbs_node_list_t **members) { - *members = rbs_node_list_new(&parser->allocator); + *members = rbs_node_list_new(ALLOCATOR()); while (parser->next_token.type != kEND) { - rbs_node_list_t *annotations = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR()); rbs_position_t annot_pos; CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos)); @@ -2623,7 +2625,7 @@ static bool parse_module_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_node_list_t *type_params; CHECK_PARSE(parse_type_params(parser, &type_params_range, true, &type_params)); - rbs_node_list_t *self_types = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *self_types = rbs_node_list_new(ALLOCATOR()); rbs_range_t colon_range; rbs_range_t self_types_range; if (parser->next_token.type == pCOLON) { @@ -2644,8 +2646,8 @@ static bool parse_module_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, rbs_range_t end_range = parser->current_token.range; decl_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); - rbs_loc_alloc_children(&parser->allocator, loc, 6); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), decl_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 6); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("end"), end_range); @@ -2655,7 +2657,7 @@ static bool parse_module_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, CHECK_PARSE(parser_pop_typevar_table(parser)); - *module_decl = rbs_ast_declarations_module_new(&parser->allocator, loc, module_name, type_params, self_types, members, annotations, comment); + *module_decl = rbs_ast_declarations_module_new(ALLOCATOR(), loc, module_name, type_params, self_types, members, annotations, comment); return true; } @@ -2691,14 +2693,14 @@ static bool parse_module_decl(rbs_parser_t *parser, rbs_position_t comment_pos, .end = old_name_range.end }; - rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); - rbs_loc_alloc_children(&parser->allocator, loc, 4); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), decl_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("new_name"), module_name_range); rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); - *module_decl = (rbs_node_t *) rbs_ast_declarations_module_alias_new(&parser->allocator, loc, module_name, old_name, comment, annotations); + *module_decl = (rbs_node_t *) rbs_ast_declarations_module_alias_new(ALLOCATOR(), loc, module_name, old_name, comment, annotations); } else { rbs_ast_declarations_module_t *module_decl0 = NULL; CHECK_PARSE(parse_module_decl0(parser, keyword_range, module_name, module_name_range, comment, annotations, &module_decl0)); @@ -2720,20 +2722,20 @@ static bool parse_class_decl_super(rbs_parser_t *parser, rbs_range_t *lt_range, rbs_range_t super_range; super_range.start = parser->next_token.range.start; - rbs_node_list_t *args = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *args = rbs_node_list_new(ALLOCATOR()); rbs_type_name_t *name = NULL; rbs_range_t name_range, args_range; CHECK_PARSE(class_instance_name(parser, CLASS_NAME, args, &name_range, &args_range, &name)); super_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, super_range); - rbs_loc_alloc_children(&parser->allocator, loc, 2); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), super_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 2); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_optional_child(loc, INTERN("args"), args_range); - *super = rbs_ast_declarations_class_super_new(&parser->allocator, loc, name, args); + *super = rbs_ast_declarations_class_super_new(ALLOCATOR(), loc, name, args); } else { *lt_range = NULL_RANGE; } @@ -2770,15 +2772,15 @@ static bool parse_class_decl0(rbs_parser_t *parser, rbs_range_t keyword_range, r CHECK_PARSE(parser_pop_typevar_table(parser)); - rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); - rbs_loc_alloc_children(&parser->allocator, loc, 5); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), decl_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 5); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("name"), name_range); rbs_loc_add_required_child(loc, INTERN("end"), end_range); rbs_loc_add_optional_child(loc, INTERN("type_params"), type_params_range); rbs_loc_add_optional_child(loc, INTERN("lt"), lt_range); - *class_decl = rbs_ast_declarations_class_new(&parser->allocator, loc, name, type_params, super, members, annotations, comment); + *class_decl = rbs_ast_declarations_class_new(ALLOCATOR(), loc, name, type_params, super, members, annotations, comment); return true; } @@ -2812,14 +2814,14 @@ static bool parse_class_decl(rbs_parser_t *parser, rbs_position_t comment_pos, r .end = old_name_range.end, }; - rbs_location_t *loc = rbs_location_new(&parser->allocator, decl_range); - rbs_loc_alloc_children(&parser->allocator, loc, 4); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), decl_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 4); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_required_child(loc, INTERN("new_name"), class_name_range); rbs_loc_add_required_child(loc, INTERN("eq"), eq_range); rbs_loc_add_optional_child(loc, INTERN("old_name"), old_name_range); - *class_decl = (rbs_node_t *) rbs_ast_declarations_class_alias_new(&parser->allocator, loc, class_name, old_name, comment, annotations); + *class_decl = (rbs_node_t *) rbs_ast_declarations_class_alias_new(ALLOCATOR(), loc, class_name, old_name, comment, annotations); } else { rbs_ast_declarations_class_t *class_decl0 = NULL; CHECK_PARSE(parse_class_decl0(parser, keyword_range, class_name, class_name_range, comment, annotations, &class_decl0)); @@ -2890,7 +2892,7 @@ static bool parse_nested_decl(rbs_parser_t *parser, const char *nested_in, rbs_p NODISCARD static bool parse_decl(rbs_parser_t *parser, rbs_node_t **decl) { - rbs_node_list_t *annotations = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *annotations = rbs_node_list_new(ALLOCATOR()); rbs_position_t annot_pos = NullPosition; CHECK_PARSE(parse_annotations(parser, annotations, &annot_pos)); @@ -2958,12 +2960,12 @@ static bool parse_namespace(rbs_parser_t *parser, rbs_range_t *rg, rbs_namespace rbs_parser_advance(parser); } - rbs_node_list_t *path = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *path = rbs_node_list_new(ALLOCATOR()); while (true) { if (parser->next_token.type == tUIDENT && parser->next_token2.type == pCOLON2) { - rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, parser->next_token.range); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->next_token)); + rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), parser->next_token.range); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->next_token)); rbs_node_list_append(path, (rbs_node_t *)symbol); if (rbs_null_position_p(rg->start)) { rg->start = parser->next_token.range.start; @@ -2976,7 +2978,7 @@ static bool parse_namespace(rbs_parser_t *parser, rbs_range_t *rg, rbs_namespace } } - *namespace = rbs_namespace_new(&parser->allocator, rbs_location_new(&parser->allocator, *rg), path, is_absolute); + *namespace = rbs_namespace_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), *rg), path, is_absolute); return true; } @@ -3008,8 +3010,8 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) { : (rbs_range_t) { .start = namespace_range.start, .end = parser->current_token.range.end }; rbs_location_t *symbolLoc = rbs_location_current_token(parser); - rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); - rbs_type_name_t *type_name = rbs_type_name_new(&parser->allocator, rbs_location_new(&parser->allocator, type_name_range),namespace, symbol); + rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); + rbs_type_name_t *type_name = rbs_type_name_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), type_name_range),namespace, symbol); rbs_range_t keyword_range = NULL_RANGE; rbs_range_t new_name_range = NULL_RANGE; @@ -3023,19 +3025,19 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) { if (ident_type == tLIDENT) ADVANCE_ASSERT(parser, tLIDENT); if (ident_type == tULIDENT) ADVANCE_ASSERT(parser, tULIDENT); - rbs_location_t *symbolLoc = rbs_location_new(&parser->allocator, new_name_range); - new_name = rbs_ast_symbol_new(&parser->allocator, symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); + rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), new_name_range); + new_name = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token)); new_name_range = parser->current_token.range; clause_range.end = new_name_range.end; } - rbs_location_t *loc = rbs_location_new(&parser->allocator, clause_range); - rbs_loc_alloc_children(&parser->allocator, loc, 3); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), clause_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 3); rbs_loc_add_required_child(loc, INTERN("type_name"), type_name_range); rbs_loc_add_optional_child(loc, INTERN("keyword"), keyword_range); rbs_loc_add_optional_child(loc, INTERN("new_name"), new_name_range); - rbs_ast_directives_use_single_clause_t *clause = rbs_ast_directives_use_single_clause_new(&parser->allocator, loc, type_name, new_name); + rbs_ast_directives_use_single_clause_t *clause = rbs_ast_directives_use_single_clause_new(ALLOCATOR(), loc, type_name, new_name); rbs_node_list_append(clauses, (rbs_node_t *)clause); break; @@ -3048,12 +3050,12 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) { rbs_range_t star_range = parser->current_token.range; clause_range.end = star_range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, clause_range); - rbs_loc_alloc_children(&parser->allocator, loc, 2); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), clause_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 2); rbs_loc_add_required_child(loc, INTERN("namespace"), namespace_range); rbs_loc_add_required_child(loc, INTERN("star"), star_range); - rbs_ast_directives_use_wildcard_clause_t *clause = rbs_ast_directives_use_wildcard_clause_new(&parser->allocator, loc, namespace); + rbs_ast_directives_use_wildcard_clause_t *clause = rbs_ast_directives_use_wildcard_clause_new(ALLOCATOR(), loc, namespace); rbs_node_list_append(clauses, (rbs_node_t *)clause); break; @@ -3083,17 +3085,17 @@ static bool parse_use_directive(rbs_parser_t *parser, rbs_ast_directives_use_t * rbs_range_t keyword_range = parser->current_token.range; - rbs_node_list_t *clauses = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *clauses = rbs_node_list_new(ALLOCATOR()); CHECK_PARSE(parse_use_clauses(parser, clauses)); rbs_range_t directive_range = keyword_range; directive_range.end = parser->current_token.range.end; - rbs_location_t *loc = rbs_location_new(&parser->allocator, directive_range); - rbs_loc_alloc_children(&parser->allocator, loc, 1); + rbs_location_t *loc = rbs_location_new(ALLOCATOR(), directive_range); + rbs_loc_alloc_children(ALLOCATOR(), loc, 1); rbs_loc_add_required_child(loc, INTERN("keyword"), keyword_range); - *use_directive = rbs_ast_directives_use_new(&parser->allocator, loc, clauses); + *use_directive = rbs_ast_directives_use_new(ALLOCATOR(), loc, clauses); } return true; @@ -3104,7 +3106,7 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_ size_t space_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) " ", (size_t) 1); rbs_buffer_t rbs_buffer; - rbs_buffer_init(&parser->allocator, &rbs_buffer); + rbs_buffer_init(ALLOCATOR(), &rbs_buffer); for (size_t i = 0; i < com->line_count; i++) { rbs_token_t tok = com->tokens[i]; @@ -3123,13 +3125,13 @@ static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_ comment_bytes -= space_bytes; } - rbs_buffer_append_string(&parser->allocator, &rbs_buffer, comment_start, comment_bytes); - rbs_buffer_append_cstr(&parser->allocator, &rbs_buffer, "\n"); + rbs_buffer_append_string(ALLOCATOR(), &rbs_buffer, comment_start, comment_bytes); + rbs_buffer_append_cstr(ALLOCATOR(), &rbs_buffer, "\n"); } return rbs_ast_comment_new( - &parser->allocator, - rbs_location_new(&parser->allocator, (rbs_range_t) { .start = com->start, .end = com->end }), + ALLOCATOR(), + rbs_location_new(ALLOCATOR(), (rbs_range_t) { .start = com->start, .end = com->end }), rbs_buffer_to_string(&rbs_buffer) ); } @@ -3199,9 +3201,9 @@ static void insert_comment_line(rbs_parser_t *parser, rbs_token_t tok) { rbs_comment_t *com = comment_get_comment(parser->last_comment, prev_line); if (com) { - comment_insert_new_line(&parser->allocator, com, tok); + comment_insert_new_line(ALLOCATOR(), com, tok); } else { - parser->last_comment = alloc_comment(&parser->allocator, tok, parser->last_comment); + parser->last_comment = alloc_comment(ALLOCATOR(), tok, parser->last_comment); } } @@ -3209,8 +3211,8 @@ bool rbs_parse_signature(rbs_parser_t *parser, rbs_signature_t **signature) { rbs_range_t signature_range; signature_range.start = parser->current_token.range.start; - rbs_node_list_t *dirs = rbs_node_list_new(&parser->allocator); - rbs_node_list_t *decls = rbs_node_list_new(&parser->allocator); + rbs_node_list_t *dirs = rbs_node_list_new(ALLOCATOR()); + rbs_node_list_t *decls = rbs_node_list_new(ALLOCATOR()); while (parser->next_token.type == kUSE) { rbs_ast_directives_use_t *use_node; @@ -3230,7 +3232,7 @@ bool rbs_parse_signature(rbs_parser_t *parser, rbs_signature_t **signature) { } signature_range.end = parser->current_token.range.end; - *signature = rbs_signature_new(&parser->allocator, rbs_location_new(&parser->allocator, signature_range), dirs, decls); + *signature = rbs_signature_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), signature_range), dirs, decls); return true; } @@ -3262,12 +3264,12 @@ id_table *alloc_reset_table(rbs_allocator_t *allocator) { void rbs_parser_push_typevar_table(rbs_parser_t *parser, bool reset) { if (reset) { - id_table *table = alloc_reset_table(&parser->allocator); + id_table *table = alloc_reset_table(ALLOCATOR()); table->next = parser->vars; parser->vars = table; } - id_table *table = alloc_empty_table(&parser->allocator); + id_table *table = alloc_empty_table(ALLOCATOR()); table->next = parser->vars; parser->vars = table; } @@ -3285,7 +3287,7 @@ bool rbs_parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id) { // expand rbs_constant_id_t *ptr = table->ids; table->size += 10; - table->ids = rbs_allocator_calloc(&parser->allocator, table->size, rbs_constant_id_t); + table->ids = rbs_allocator_calloc(ALLOCATOR(), table->size, rbs_constant_id_t); memcpy(table->ids, ptr, sizeof(rbs_constant_id_t) * table->count); } @@ -3426,7 +3428,7 @@ rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding void rbs_parser_free(rbs_parser_t *parser) { rbs_constant_pool_free(&parser->constant_pool); - rbs_allocator_free(&parser->allocator); + rbs_allocator_free(ALLOCATOR()); } void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) { @@ -3440,13 +3442,13 @@ void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_err int length = vsnprintf(NULL, 0, fmt, args); va_end(args); - char *message = rbs_allocator_alloc_many(&parser->allocator, length + 1, char); + char *message = rbs_allocator_alloc_many(ALLOCATOR(), length + 1, char); va_start(args, fmt); vsnprintf(message, length + 1, fmt, args); va_end(args); - parser->error = rbs_allocator_alloc(&parser->allocator, rbs_error_t); + parser->error = rbs_allocator_alloc(ALLOCATOR(), rbs_error_t); parser->error->token = tok; parser->error->message = message; parser->error->syntax_error = syntax_error; From 477ad58b825a8254507a6c8196bbbff8eb2c068a Mon Sep 17 00:00:00 2001 From: Max Bernstein Date: Thu, 3 Apr 2025 11:15:43 -0400 Subject: [PATCH 106/111] Use one massive slice of virtual memory per arena This has the handy effect of making allocations nearly free while unfortunately having the side effect of crashing your process if you write more than the arena size. However, if you are allocating more than 4 GiB, you likely have other problems. --- ext/rbs_extension/main.c | 8 +- include/rbs/parser.h | 2 +- include/rbs/util/rbs_allocator.h | 10 +- src/parser.c | 9 +- src/util/rbs_allocator.c | 183 ++++++++++++------------------- 5 files changed, 80 insertions(+), 132 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 75739719e..432e272d3 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -284,9 +284,8 @@ static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) { StringValue(string); rb_encoding *encoding = rb_enc_get(string); - rbs_allocator_t allocator; - rbs_allocator_init(&allocator); - rbs_lexer_t *lexer = alloc_lexer_from_buffer(&allocator, string, encoding, 0, FIX2INT(end_pos)); + rbs_allocator_t *allocator = rbs_allocator_init(); + rbs_lexer_t *lexer = alloc_lexer_from_buffer(allocator, string, encoding, 0, FIX2INT(end_pos)); VALUE results = rb_ary_new(); rbs_token_t token = NullToken; @@ -298,7 +297,7 @@ static VALUE rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) { rb_ary_push(results, pair); } - rbs_allocator_free(&allocator); + rbs_allocator_free(allocator); RB_GC_GUARD(string); return results; @@ -327,7 +326,6 @@ Init_rbs_extension(void) #ifdef HAVE_RB_EXT_RACTOR_SAFE rb_ext_ractor_safe(true); #endif - rbs__init_arena_allocator(); rbs__init_constants(); rbs__init_location(); rbs__init_parser(); diff --git a/include/rbs/parser.h b/include/rbs/parser.h index e1a35de83..38326cf93 100644 --- a/include/rbs/parser.h +++ b/include/rbs/parser.h @@ -55,7 +55,7 @@ typedef struct { rbs_comment_t *last_comment; /* Last read comment */ rbs_constant_pool_t constant_pool; - rbs_allocator_t allocator; + rbs_allocator_t *allocator; rbs_error_t *error; } rbs_parser_t; diff --git a/include/rbs/util/rbs_allocator.h b/include/rbs/util/rbs_allocator.h index a50a5eef4..40c53aeed 100644 --- a/include/rbs/util/rbs_allocator.h +++ b/include/rbs/util/rbs_allocator.h @@ -14,12 +14,10 @@ #endif #endif -typedef struct rbs_allocator { - // The head of a linked list of pages, starting with the most recently allocated page. - struct rbs_allocator_page *page; -} rbs_allocator_t; +struct rbs_allocator; +typedef struct rbs_allocator rbs_allocator_t; -void rbs_allocator_init(rbs_allocator_t *); +rbs_allocator_t *rbs_allocator_init(void); void rbs_allocator_free(rbs_allocator_t *); void *rbs_allocator_malloc_impl (rbs_allocator_t *, /* 1 */ size_t size, size_t alignment); void *rbs_allocator_malloc_many_impl (rbs_allocator_t *, size_t count, size_t size, size_t alignment); @@ -37,6 +35,4 @@ void *rbs_allocator_realloc_impl (rbs_allocator_t *, void *ptr, size_t old_s #define rbs_allocator_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), alignof(type))) #define rbs_allocator_realloc(allocator, ptr, old_size, new_size, type) ((type *) rbs_allocator_realloc_impl((allocator), (ptr), (old_size), (new_size), alignof(type))) -void rbs__init_arena_allocator(void); - #endif diff --git a/src/parser.c b/src/parser.c index 09ef1fc28..dce12189c 100644 --- a/src/parser.c +++ b/src/parser.c @@ -80,7 +80,7 @@ #define RESET_TABLE_P(table) (table->size == 0) -#define ALLOCATOR() &parser->allocator +#define ALLOCATOR() parser->allocator typedef struct { rbs_node_list_t *required_positionals; @@ -3377,11 +3377,10 @@ rbs_lexer_t *rbs_lexer_new(rbs_allocator_t *allocator, rbs_string_t string, cons } rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) { - rbs_allocator_t allocator; - rbs_allocator_init(&allocator); + rbs_allocator_t *allocator = rbs_allocator_init(); - rbs_lexer_t *lexer = rbs_lexer_new(&allocator, string, encoding, start_pos, end_pos); - rbs_parser_t *parser = rbs_allocator_alloc(&allocator, rbs_parser_t); + rbs_lexer_t *lexer = rbs_lexer_new(allocator, string, encoding, start_pos, end_pos); + rbs_parser_t *parser = rbs_allocator_alloc(allocator, rbs_parser_t); *parser = (rbs_parser_t) { .rbs_lexer_t = lexer, diff --git a/src/util/rbs_allocator.c b/src/util/rbs_allocator.c index 56e422d05..db198311b 100644 --- a/src/util/rbs_allocator.c +++ b/src/util/rbs_allocator.c @@ -3,14 +3,6 @@ * * A simple arena allocator that can be freed all at once. * - * This allocator maintains a linked list of pages, which come in two flavours: - * 1. Small allocation pages, which are the same size as the system page size. - * 2. Large allocation pages, which are the exact size requested, for sizes greater than the small page size. - * - * Small allocations always fit into the unused space at the end of the "head" page. If there isn't enough room, a new - * page is allocated, and the small allocation is placed at its start. This approach wastes that unused slack at the - * end of the previous page, but it means that allocations are instant and never scan the linked list to find a gap. - * * This allocator doesn't support freeing individual allocations. Only the whole arena can be freed at once at the end. */ @@ -20,34 +12,27 @@ #include #include // for memset() #include +#include #ifdef _WIN32 #include #else #include #include + #include #endif -typedef struct rbs_allocator_page { - uint32_t payload_size; - - // The offset of the next available byte. - uint32_t offset; - - // The previously allocated page, or NULL if this is the first page. - struct rbs_allocator_page *next; +#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__sun) +#define MAP_ANONYMOUS MAP_ANON +#endif - // The variably-sized payload of the page. - char payload[]; -} rbs_allocator_page_t; -// This allocator's normal pages have the same size as the system memory pages, consisting of a fixed-size header -// (sizeof(rbs_allocator_page_t)) followed `default_page_payload_size` bytes of payload. -// TODO: When we have real-world usage data, we can tune this to use a smaller number of larger pages. -static size_t default_page_payload_size = 0; -static uint32_t large_page_flag = UINT32_MAX; +struct rbs_allocator { + uintptr_t heap_ptr; + uintptr_t size; +}; -static size_t get_system_page_size() { +static size_t get_system_page_size(void) { #ifdef _WIN32 SYSTEM_INFO si; GetSystemInfo(&si); @@ -59,72 +44,74 @@ static size_t get_system_page_size() { #endif } -void rbs__init_arena_allocator(void) { - const size_t system_page_size = get_system_page_size(); - - // The size of a struct that ends with a flexible array member is the size of the struct without the - // flexible array member. https://en.wikipedia.org/wiki/Flexible_array_member#Effect_on_struct_size_and_padding - const size_t page_header_size = sizeof(rbs_allocator_page_t); - default_page_payload_size = system_page_size - page_header_size; +static void *map_memory(size_t size) { +#ifdef _WIN32 + LPVOID result = VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + rbs_assert(result != NULL, "VirtualAlloc failed"); +#else + void *result = mmap(NULL, size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + rbs_assert(result != MAP_FAILED, "mmap failed"); +#endif + return result; } -// Returns the number of bytes needed to pad the given pointer up to the nearest multiple of the given `alignment`. -static size_t needed_padding(void *ptr, size_t alignment) { - const uintptr_t addr = (uintptr_t) ptr; - const uintptr_t aligned_addr = (addr + (alignment - 1)) & -alignment; - return (aligned_addr - addr); +static void destroy_memory(void *memory, size_t size) { +#ifdef _WIN32 + VirtualFree(memory, 0, MEM_RELEASE); +#else + munmap(memory, size); +#endif } -static rbs_allocator_page_t *rbs_allocator_page_new(size_t payload_size) { - const size_t page_header_size = sizeof(rbs_allocator_page_t); - - rbs_allocator_page_t *page = calloc(1, page_header_size + payload_size); - *page = (rbs_allocator_page_t) { - .payload_size = (uint32_t) payload_size, - .offset = page_header_size, - .next = NULL, - }; - return page; +static void guard_page(void *memory, size_t page_size) { +#ifdef _WIN32 + DWORD old_protect_; + BOOL result = VirtualProtect(memory, page_size, PAGE_NOACCESS, &old_protect_); + rbs_assert(result != 0, "VirtualProtect failed"); +#else + int result = mprotect(memory, page_size, PROT_NONE); + rbs_assert(result == 0, "mprotect failed"); +#endif } -static rbs_allocator_page_t *rbs_allocator_page_new_default(void) { - return rbs_allocator_page_new(default_page_payload_size); +static size_t rbs_allocator_default_mem(void) { + size_t kib = 1024; + size_t mib = kib * 1024; + size_t gib = mib * 1024; + return 4 * gib; } -static rbs_allocator_page_t *rbs_allocator_page_new_large(size_t payload_size) { - rbs_allocator_page_t *page = rbs_allocator_page_new(payload_size); - - page->offset = large_page_flag; - - return page; +static inline bool is_power_of_two(uintptr_t value) { + return value > 0 && (value & (value - 1)) == 0; } -// Attempts to allocate `size` bytes from `page`, returning NULL if there is insufficient space. -static void *rbs_allocator_page_attempt_alloc(rbs_allocator_page_t *page, size_t size, size_t alignment) { - const size_t alignment_padding = needed_padding(page->payload + page->offset, alignment); - - const size_t remaining_size = page->payload_size - page->offset; - const size_t needed_size = alignment_padding + size; - if (remaining_size < needed_size) return NULL; // Not enough space in this page. - - void *ptr = page->payload + page->offset + alignment_padding; - page->offset += needed_size; - return ptr; +// Align `val' to nearest multiple of `alignment'. +static uintptr_t align(uintptr_t size, uintptr_t alignment) { + rbs_assert(is_power_of_two(alignment), "alignment is not a power of two"); + return (size + alignment - 1) & ~(alignment - 1); } -void rbs_allocator_init(rbs_allocator_t *allocator) { - *allocator = (rbs_allocator_t) { - .page = rbs_allocator_page_new_default(), +rbs_allocator_t *rbs_allocator_init(void) { + size_t size = rbs_allocator_default_mem(); + size_t page_size = get_system_page_size(); + size = align(size, page_size); + void *mem = map_memory(size + page_size); + // Guard page; remove range checks in alloc fast path and hard fail if we + // consume all memory + void *last_page = (char *) mem + size; + guard_page(last_page, page_size); + uintptr_t start = (uintptr_t) mem; + rbs_allocator_t header = (rbs_allocator_t) { + .heap_ptr = start + sizeof header, + .size = size + page_size, }; + memcpy(mem, &header, sizeof header); + return (rbs_allocator_t *) mem; } void rbs_allocator_free(rbs_allocator_t *allocator) { - rbs_allocator_page_t *page = allocator->page; - while (page) { - rbs_allocator_page_t *next = page->next; - free(page); - page = next; - } + destroy_memory((void *) allocator, allocator->size); } // Allocates `new_size` bytes from `allocator`, aligned to an `alignment`-byte boundary. @@ -139,52 +126,20 @@ void *rbs_allocator_realloc_impl(rbs_allocator_t *allocator, void *ptr, size_t o // Allocates `size` bytes from `allocator`, aligned to an `alignment`-byte boundary. void *rbs_allocator_malloc_impl(rbs_allocator_t *allocator, size_t size, size_t alignment) { rbs_assert(size % alignment == 0, "size must be a multiple of the alignment. size: %zu, alignment: %zu", size, alignment); - - if (default_page_payload_size < size) { // Big allocation, give it its own page. - // How much we need to pad the new page's payload in order to get an aligned pointer - // hack? - const size_t alignment_padding = needed_padding((void *) (sizeof(rbs_allocator_page_t) + size), alignment); - - rbs_allocator_page_t *new_page = rbs_allocator_page_new_large(alignment_padding + size); - - // This simple allocator can only put small allocations into the head page. - // Naively prepending this large allocation page to the head of the allocator before the previous head page - // would waste the remaining space in the head page. - // So instead, we'll splice in the large page *after* the head page. - // - // +-------+ +-----------+ +-----------+ - // | arena | | head page | | new_page | - // |-------| |-----------+ |-----------+ - // | *page |--->| size | +--->| size | +---> ... previous tail - // +-------+ | offset | | | offset | | - // | *next ----+---+ | *next ----+---+ - // | ... | | ... | - // +-----------+ +-----------+ - // - new_page->next = allocator->page->next; - allocator->page->next = new_page; - - return new_page->payload + alignment_padding; - } - - void *p = rbs_allocator_page_attempt_alloc(allocator->page, size, alignment); - if (p != NULL) return p; - - // Not enough space. Allocate a new page and prepend it to the allocator's linked list. - rbs_allocator_page_t *new_page = rbs_allocator_page_new_default(); - new_page->next = allocator->page; - allocator->page = new_page; - - p = rbs_allocator_page_attempt_alloc(new_page, size, alignment); - rbs_assert(p != NULL, "Failed to allocate a new allocator page"); - return p; + uintptr_t aligned = align(allocator->heap_ptr, alignment); + allocator->heap_ptr = aligned + size; + return (void *) aligned; } // Note: This will eagerly fill with zeroes, unlike `calloc()` which can map a page in a page to be zeroed lazily. // It's assumed that callers to this function will immediately write to the allocated memory, anyway. void *rbs_allocator_calloc_impl(rbs_allocator_t *allocator, size_t count, size_t size, size_t alignment) { void *p = rbs_allocator_malloc_many_impl(allocator, count, size, alignment); +#if defined(__linux__) + // mmap with MAP_ANONYMOUS gives zero-filled pages. +#else memset(p, 0, count * size); +#endif return p; } From 73060a66ab4fad3a7be4458e5dfc387b1714c087 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Fri, 25 Apr 2025 21:08:11 +0800 Subject: [PATCH 107/111] Remove unnecessary freeing functions (#40) `rbs_node_destroy`, `rbs_hash_free`, `rbs_node_list_free` are only calling each other recursively without any real freeing logic. This is the result of previous efforts to allocate all nodes on the arena. So we don't need these functions anymore. Discovered while working on #41 --- ext/rbs_extension/main.c | 13 +- include/rbs/ast.h | 2 - src/ast.c | 849 +++----------------------------- templates/include/rbs/ast.h.erb | 2 - templates/src/ast.c.erb | 60 --- templates/template.rb | 8 - 6 files changed, 66 insertions(+), 868 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 415d7886b..5360e69f2 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -123,10 +123,7 @@ static VALUE parse_type_try(VALUE a) { arg->encoding ); - - VALUE ruby_ast = rbs_struct_to_ruby_value(ctx, type); - rbs_node_destroy((rbs_node_t *) type); - return ruby_ast; + return rbs_struct_to_ruby_value(ctx, type); } static rbs_lexer_t *alloc_lexer_from_buffer(rbs_allocator_t *allocator, VALUE string, rb_encoding *encoding, int start_pos, int end_pos) { @@ -213,9 +210,7 @@ static VALUE parse_method_type_try(VALUE a) { arg->encoding ); - VALUE ruby_ast = rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type); - rbs_node_destroy((rbs_node_t *) method_type); - return ruby_ast; + return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) method_type); } static VALUE rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof) { @@ -254,9 +249,7 @@ static VALUE parse_signature_try(VALUE a) { arg->encoding ); - VALUE ruby_ast = rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature); - rbs_node_destroy((rbs_node_t *) signature); - return ruby_ast; + return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) signature); } static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) { diff --git a/include/rbs/ast.h b/include/rbs/ast.h index db41a5e6f..4f337c7ba 100644 --- a/include/rbs/ast.h +++ b/include/rbs/ast.h @@ -685,6 +685,4 @@ rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_location_ rbs_types_untyped_function_t *rbs_types_untyped_function_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *return_type); rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name); -void rbs_node_destroy(rbs_node_t *any_node); - #endif diff --git a/src/ast.c b/src/ast.c index 18f9d7f81..738e06b08 100644 --- a/src/ast.c +++ b/src/ast.c @@ -95,17 +95,6 @@ rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *allocator) { return list; } -void rbs_node_list_free(rbs_node_list_t *list) { - rbs_node_list_node_t *current = list->head; - while (current != NULL) { - rbs_node_list_node_t *next = current->next; - rbs_node_destroy(current->node); - // `current` is owned by the arena, so it will be freed automatically when the arena is freed. - current = next; - } - // `list` is owned by the arena, so it will be freed automatically when the arena is freed. -} - void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node) { rbs_node_list_node_t *new_node = rbs_allocator_alloc(list->allocator, rbs_node_list_node_t); *new_node = (rbs_node_list_node_t) { @@ -138,18 +127,6 @@ rbs_hash_t* rbs_hash_new(rbs_allocator_t *allocator) { return hash; } -void rbs_hash_free(rbs_hash_t *hash) { - rbs_hash_node_t *current = hash->head; - while (current != NULL) { - rbs_hash_node_t *next = current->next; - rbs_node_destroy(current->key); - rbs_node_destroy(current->value); - // `current` is owned by the arena, so it will be freed automatically when the arena is freed. - current = next; - } - // `hash` is owned by the arena, so it will be freed automatically when the arena is freed. -} - bool rbs_node_equal(rbs_node_t *lhs, rbs_node_t *rhs) { if (lhs == rhs) return true; if (lhs->type != rhs->type) return false; @@ -238,7 +215,7 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_location_t return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string) { rbs_ast_annotation_t *instance = rbs_allocator_alloc(allocator, rbs_ast_annotation_t); @@ -253,7 +230,7 @@ rbs_ast_annotation_t *rbs_ast_annotation_new(rbs_allocator_t *allocator, rbs_loc return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, rbs_location_t *location, bool value) { rbs_ast_bool_t *instance = rbs_allocator_alloc(allocator, rbs_ast_bool_t); @@ -268,7 +245,7 @@ rbs_ast_bool_t *rbs_ast_bool_new(rbs_allocator_t *allocator, rbs_location_t *loc return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string) { rbs_ast_comment_t *instance = rbs_allocator_alloc(allocator, rbs_ast_comment_t); @@ -283,7 +260,7 @@ rbs_ast_comment_t *rbs_ast_comment_new(rbs_allocator_t *allocator, rbs_location_ return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *type_params, rbs_ast_declarations_class_super_t *super_class, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_class_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_t); @@ -303,7 +280,7 @@ rbs_ast_declarations_class_t *rbs_ast_declarations_class_new(rbs_allocator_t *al return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args) { rbs_ast_declarations_class_super_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_super_t); @@ -319,7 +296,7 @@ rbs_ast_declarations_class_super_t *rbs_ast_declarations_class_super_new(rbs_all return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_declarations_class_alias_t *rbs_ast_declarations_class_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *new_name, rbs_type_name_t *old_name, rbs_ast_comment_t *comment, rbs_node_list_t *annotations) { rbs_ast_declarations_class_alias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_class_alias_t); @@ -337,7 +314,7 @@ rbs_ast_declarations_class_alias_t *rbs_ast_declarations_class_alias_new(rbs_all return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_t *type, rbs_ast_comment_t *comment, rbs_node_list_t *annotations) { rbs_ast_declarations_constant_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_constant_t); @@ -355,7 +332,7 @@ rbs_ast_declarations_constant_t *rbs_ast_declarations_constant_new(rbs_allocator return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment, rbs_node_list_t *annotations) { rbs_ast_declarations_global_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_global_t); @@ -373,7 +350,7 @@ rbs_ast_declarations_global_t *rbs_ast_declarations_global_new(rbs_allocator_t * return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *type_params, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_interface_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_interface_t); @@ -392,7 +369,7 @@ rbs_ast_declarations_interface_t *rbs_ast_declarations_interface_new(rbs_allocat return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *type_params, rbs_node_list_t *self_types, rbs_node_list_t *members, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_module_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_t); @@ -412,7 +389,7 @@ rbs_ast_declarations_module_t *rbs_ast_declarations_module_new(rbs_allocator_t * return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args) { rbs_ast_declarations_module_self_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_self_t); @@ -428,7 +405,7 @@ rbs_ast_declarations_module_self_t *rbs_ast_declarations_module_self_new(rbs_all return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_declarations_module_alias_t *rbs_ast_declarations_module_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *new_name, rbs_type_name_t *old_name, rbs_ast_comment_t *comment, rbs_node_list_t *annotations) { rbs_ast_declarations_module_alias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_module_alias_t); @@ -446,7 +423,7 @@ rbs_ast_declarations_module_alias_t *rbs_ast_declarations_module_alias_new(rbs_a return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_declarations_type_alias_t *rbs_ast_declarations_type_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *type_params, rbs_node_t *type, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_declarations_type_alias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_declarations_type_alias_t); @@ -465,7 +442,7 @@ rbs_ast_declarations_type_alias_t *rbs_ast_declarations_type_alias_new(rbs_alloc return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *clauses) { rbs_ast_directives_use_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_t); @@ -480,7 +457,7 @@ rbs_ast_directives_use_t *rbs_ast_directives_use_new(rbs_allocator_t *allocator, return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_directives_use_single_clause_t *rbs_ast_directives_use_single_clause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *type_name, rbs_ast_symbol_t *new_name) { rbs_ast_directives_use_single_clause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_single_clause_t); @@ -496,7 +473,7 @@ rbs_ast_directives_use_single_clause_t *rbs_ast_directives_use_single_clause_new return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_directives_use_wildcard_clause_t *rbs_ast_directives_use_wildcard_clause_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace) { rbs_ast_directives_use_wildcard_clause_t *instance = rbs_allocator_alloc(allocator, rbs_ast_directives_use_wildcard_clause_t); @@ -511,7 +488,7 @@ rbs_ast_directives_use_wildcard_clause_t *rbs_ast_directives_use_wildcard_clause return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_integer_t *rbs_ast_integer_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string_representation) { rbs_ast_integer_t *instance = rbs_allocator_alloc(allocator, rbs_ast_integer_t); @@ -526,7 +503,7 @@ rbs_ast_integer_t *rbs_ast_integer_new(rbs_allocator_t *allocator, rbs_location_ return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *new_name, rbs_ast_symbol_t *old_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_alias_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_alias_t); @@ -545,7 +522,7 @@ rbs_ast_members_alias_t *rbs_ast_members_alias_new(rbs_allocator_t *allocator, r return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_members_attr_accessor_t *rbs_ast_members_attr_accessor_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { rbs_ast_members_attr_accessor_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attr_accessor_t); @@ -566,7 +543,7 @@ rbs_ast_members_attr_accessor_t *rbs_ast_members_attr_accessor_new(rbs_allocator return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_members_attr_reader_t *rbs_ast_members_attr_reader_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { rbs_ast_members_attr_reader_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attr_reader_t); @@ -587,7 +564,7 @@ rbs_ast_members_attr_reader_t *rbs_ast_members_attr_reader_new(rbs_allocator_t * return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_members_attr_writer_t *rbs_ast_members_attr_writer_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_node_t *ivar_name, rbs_keyword_t *kind, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, rbs_keyword_t *visibility) { rbs_ast_members_attr_writer_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_attr_writer_t); @@ -608,7 +585,7 @@ rbs_ast_members_attr_writer_t *rbs_ast_members_attr_writer_new(rbs_allocator_t * return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_members_class_instance_variable_t *rbs_ast_members_class_instance_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_members_class_instance_variable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_class_instance_variable_t); @@ -625,7 +602,7 @@ rbs_ast_members_class_instance_variable_t *rbs_ast_members_class_instance_variab return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_members_class_variable_t *rbs_ast_members_class_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_members_class_variable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_class_variable_t); @@ -642,7 +619,7 @@ rbs_ast_members_class_variable_t *rbs_ast_members_class_variable_new(rbs_allocat return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_extend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_extend_t); @@ -660,7 +637,7 @@ rbs_ast_members_extend_t *rbs_ast_members_extend_new(rbs_allocator_t *allocator, return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_include_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_include_t); @@ -678,7 +655,7 @@ rbs_ast_members_include_t *rbs_ast_members_include_new(rbs_allocator_t *allocato return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_members_instance_variable_t *rbs_ast_members_instance_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_node_t *type, rbs_ast_comment_t *comment) { rbs_ast_members_instance_variable_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_instance_variable_t); @@ -695,7 +672,7 @@ rbs_ast_members_instance_variable_t *rbs_ast_members_instance_variable_new(rbs_a return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_members_method_definition_t *rbs_ast_members_method_definition_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *kind, rbs_node_list_t *overloads, rbs_node_list_t *annotations, rbs_ast_comment_t *comment, bool overloading, rbs_keyword_t *visibility) { rbs_ast_members_method_definition_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_method_definition_t); @@ -716,7 +693,7 @@ rbs_ast_members_method_definition_t *rbs_ast_members_method_definition_new(rbs_a return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_members_method_definition_overload_t *rbs_ast_members_method_definition_overload_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *annotations, rbs_node_t *method_type) { rbs_ast_members_method_definition_overload_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_method_definition_overload_t); @@ -732,7 +709,7 @@ rbs_ast_members_method_definition_overload_t *rbs_ast_members_method_definition_ return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args, rbs_node_list_t *annotations, rbs_ast_comment_t *comment) { rbs_ast_members_prepend_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_prepend_t); @@ -750,7 +727,7 @@ rbs_ast_members_prepend_t *rbs_ast_members_prepend_new(rbs_allocator_t *allocato return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_ast_members_private_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_private_t); @@ -764,7 +741,7 @@ rbs_ast_members_private_t *rbs_ast_members_private_new(rbs_allocator_t *allocato return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_ast_members_public_t *instance = rbs_allocator_alloc(allocator, rbs_ast_members_public_t); @@ -778,7 +755,7 @@ rbs_ast_members_public_t *rbs_ast_members_public_new(rbs_allocator_t *allocator, return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_string_t *rbs_ast_string_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_string_t string) { rbs_ast_string_t *instance = rbs_allocator_alloc(allocator, rbs_ast_string_t); @@ -793,7 +770,7 @@ rbs_ast_string_t *rbs_ast_string_new(rbs_allocator_t *allocator, rbs_location_t return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_ast_type_param_t *rbs_ast_type_param_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name, rbs_keyword_t *variance, rbs_node_t *upper_bound, rbs_node_t *default_type, bool unchecked) { rbs_ast_type_param_t *instance = rbs_allocator_alloc(allocator, rbs_ast_type_param_t); @@ -812,7 +789,7 @@ rbs_ast_type_param_t *rbs_ast_type_param_new(rbs_allocator_t *allocator, rbs_loc return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_method_type_t *rbs_method_type_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *type_params, rbs_node_t *type, rbs_types_block_t *block) { rbs_method_type_t *instance = rbs_allocator_alloc(allocator, rbs_method_type_t); @@ -829,7 +806,7 @@ rbs_method_type_t *rbs_method_type_new(rbs_allocator_t *allocator, rbs_location_ return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *path, bool absolute) { rbs_namespace_t *instance = rbs_allocator_alloc(allocator, rbs_namespace_t); @@ -845,7 +822,7 @@ rbs_namespace_t *rbs_namespace_new(rbs_allocator_t *allocator, rbs_location_t *l return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *directives, rbs_node_list_t *declarations) { rbs_signature_t *instance = rbs_allocator_alloc(allocator, rbs_signature_t); @@ -861,7 +838,7 @@ rbs_signature_t *rbs_signature_new(rbs_allocator_t *allocator, rbs_location_t *l return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_type_name_t *rbs_type_name_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_namespace_t *rbs_namespace, rbs_ast_symbol_t *name) { rbs_type_name_t *instance = rbs_allocator_alloc(allocator, rbs_type_name_t); @@ -877,7 +854,7 @@ rbs_type_name_t *rbs_type_name_new(rbs_allocator_t *allocator, rbs_location_t *l return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args) { rbs_types_alias_t *instance = rbs_allocator_alloc(allocator, rbs_types_alias_t); @@ -893,7 +870,7 @@ rbs_types_alias_t *rbs_types_alias_new(rbs_allocator_t *allocator, rbs_location_ return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, rbs_location_t *location, bool todo) { rbs_types_bases_any_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_any_t); @@ -908,7 +885,7 @@ rbs_types_bases_any_t *rbs_types_bases_any_new(rbs_allocator_t *allocator, rbs_l return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_bool_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_bool_t); @@ -922,7 +899,7 @@ rbs_types_bases_bool_t *rbs_types_bases_bool_new(rbs_allocator_t *allocator, rbs return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_bottom_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_bottom_t); @@ -936,7 +913,7 @@ rbs_types_bases_bottom_t *rbs_types_bases_bottom_new(rbs_allocator_t *allocator, return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_class_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_class_t); @@ -950,7 +927,7 @@ rbs_types_bases_class_t *rbs_types_bases_class_new(rbs_allocator_t *allocator, r return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_instance_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_instance_t); @@ -964,7 +941,7 @@ rbs_types_bases_instance_t *rbs_types_bases_instance_new(rbs_allocator_t *alloca return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_nil_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_nil_t); @@ -978,7 +955,7 @@ rbs_types_bases_nil_t *rbs_types_bases_nil_new(rbs_allocator_t *allocator, rbs_l return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_self_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_self_t); @@ -992,7 +969,7 @@ rbs_types_bases_self_t *rbs_types_bases_self_new(rbs_allocator_t *allocator, rbs return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_top_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_top_t); @@ -1006,7 +983,7 @@ rbs_types_bases_top_t *rbs_types_bases_top_new(rbs_allocator_t *allocator, rbs_l return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, rbs_location_t *location) { rbs_types_bases_void_t *instance = rbs_allocator_alloc(allocator, rbs_types_bases_void_t); @@ -1020,7 +997,7 @@ rbs_types_bases_void_t *rbs_types_bases_void_new(rbs_allocator_t *allocator, rbs return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, bool required, rbs_node_t *self_type) { rbs_types_block_t *instance = rbs_allocator_alloc(allocator, rbs_types_block_t); @@ -1037,7 +1014,7 @@ rbs_types_block_t *rbs_types_block_new(rbs_allocator_t *allocator, rbs_location_ return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_class_instance_t *rbs_types_class_instance_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args) { rbs_types_class_instance_t *instance = rbs_allocator_alloc(allocator, rbs_types_class_instance_t); @@ -1053,7 +1030,7 @@ rbs_types_class_instance_t *rbs_types_class_instance_new(rbs_allocator_t *alloca return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_class_singleton_t *rbs_types_class_singleton_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name) { rbs_types_class_singleton_t *instance = rbs_allocator_alloc(allocator, rbs_types_class_singleton_t); @@ -1068,7 +1045,7 @@ rbs_types_class_singleton_t *rbs_types_class_singleton_new(rbs_allocator_t *allo return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *required_positionals, rbs_node_list_t *optional_positionals, rbs_node_t *rest_positionals, rbs_node_list_t *trailing_positionals, rbs_hash_t *required_keywords, rbs_hash_t *optional_keywords, rbs_node_t *rest_keywords, rbs_node_t *return_type) { rbs_types_function_t *instance = rbs_allocator_alloc(allocator, rbs_types_function_t); @@ -1090,7 +1067,7 @@ rbs_types_function_t *rbs_types_function_new(rbs_allocator_t *allocator, rbs_loc return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, rbs_ast_symbol_t *name) { rbs_types_function_param_t *instance = rbs_allocator_alloc(allocator, rbs_types_function_param_t); @@ -1106,7 +1083,7 @@ rbs_types_function_param_t *rbs_types_function_param_new(rbs_allocator_t *alloca return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_type_name_t *name, rbs_node_list_t *args) { rbs_types_interface_t *instance = rbs_allocator_alloc(allocator, rbs_types_interface_t); @@ -1122,7 +1099,7 @@ rbs_types_interface_t *rbs_types_interface_new(rbs_allocator_t *allocator, rbs_l return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types) { rbs_types_intersection_t *instance = rbs_allocator_alloc(allocator, rbs_types_intersection_t); @@ -1137,7 +1114,7 @@ rbs_types_intersection_t *rbs_types_intersection_new(rbs_allocator_t *allocator, return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *literal) { rbs_types_literal_t *instance = rbs_allocator_alloc(allocator, rbs_types_literal_t); @@ -1152,7 +1129,7 @@ rbs_types_literal_t *rbs_types_literal_new(rbs_allocator_t *allocator, rbs_locat return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type) { rbs_types_optional_t *instance = rbs_allocator_alloc(allocator, rbs_types_optional_t); @@ -1167,7 +1144,7 @@ rbs_types_optional_t *rbs_types_optional_new(rbs_allocator_t *allocator, rbs_loc return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, rbs_types_block_t *block, rbs_node_t *self_type) { rbs_types_proc_t *instance = rbs_allocator_alloc(allocator, rbs_types_proc_t); @@ -1184,7 +1161,7 @@ rbs_types_proc_t *rbs_types_proc_new(rbs_allocator_t *allocator, rbs_location_t return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_hash_t *all_fields) { rbs_types_record_t *instance = rbs_allocator_alloc(allocator, rbs_types_record_t); @@ -1199,7 +1176,7 @@ rbs_types_record_t *rbs_types_record_new(rbs_allocator_t *allocator, rbs_locatio return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_record_field_type_t *rbs_types_record_field_type_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *type, bool required) { rbs_types_record_field_type_t *instance = rbs_allocator_alloc(allocator, rbs_types_record_field_type_t); @@ -1215,7 +1192,7 @@ rbs_types_record_field_type_t *rbs_types_record_field_type_new(rbs_allocator_t * return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types) { rbs_types_tuple_t *instance = rbs_allocator_alloc(allocator, rbs_types_tuple_t); @@ -1230,7 +1207,7 @@ rbs_types_tuple_t *rbs_types_tuple_new(rbs_allocator_t *allocator, rbs_location_ return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_list_t *types) { rbs_types_union_t *instance = rbs_allocator_alloc(allocator, rbs_types_union_t); @@ -1245,7 +1222,7 @@ rbs_types_union_t *rbs_types_union_new(rbs_allocator_t *allocator, rbs_location_ return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_untyped_function_t *rbs_types_untyped_function_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_node_t *return_type) { rbs_types_untyped_function_t *instance = rbs_allocator_alloc(allocator, rbs_types_untyped_function_t); @@ -1260,7 +1237,7 @@ rbs_types_untyped_function_t *rbs_types_untyped_function_new(rbs_allocator_t *al return instance; } -#line 176 "prism/templates/src/ast.c.erb" +#line 153 "prism/templates/src/ast.c.erb" rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_ast_symbol_t *name) { rbs_types_variable_t *instance = rbs_allocator_alloc(allocator, rbs_types_variable_t); @@ -1275,703 +1252,3 @@ rbs_types_variable_t *rbs_types_variable_new(rbs_allocator_t *allocator, rbs_loc return instance; } - -#line 198 "prism/templates/src/ast.c.erb" -void rbs_node_destroy(rbs_node_t *any_node) { - switch (any_node->type) { -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_ANNOTATION: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_BOOL: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_COMMENT: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DECLARATIONS_CLASS: { - rbs_ast_declarations_class_t *node = (rbs_ast_declarations_class_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - rbs_node_list_free(node->type_params); - if (node->super_class != NULL) { - rbs_node_destroy((rbs_node_t *) node->super_class); - } - rbs_node_list_free(node->members); - rbs_node_list_free(node->annotations); - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DECLARATIONS_CLASS_SUPER: { - rbs_ast_declarations_class_super_t *node = (rbs_ast_declarations_class_super_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - rbs_node_list_free(node->args); - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DECLARATIONS_CLASS_ALIAS: { - rbs_ast_declarations_class_alias_t *node = (rbs_ast_declarations_class_alias_t *)any_node; - - if (node->new_name != NULL) { - rbs_node_destroy((rbs_node_t *) node->new_name); - } - if (node->old_name != NULL) { - rbs_node_destroy((rbs_node_t *) node->old_name); - } - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - rbs_node_list_free(node->annotations); - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DECLARATIONS_CONSTANT: { - rbs_ast_declarations_constant_t *node = (rbs_ast_declarations_constant_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - if (node->type != NULL) { - rbs_node_destroy((rbs_node_t *) node->type); - } - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - rbs_node_list_free(node->annotations); - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DECLARATIONS_GLOBAL: { - rbs_ast_declarations_global_t *node = (rbs_ast_declarations_global_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - if (node->type != NULL) { - rbs_node_destroy((rbs_node_t *) node->type); - } - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - rbs_node_list_free(node->annotations); - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DECLARATIONS_INTERFACE: { - rbs_ast_declarations_interface_t *node = (rbs_ast_declarations_interface_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - rbs_node_list_free(node->type_params); - rbs_node_list_free(node->members); - rbs_node_list_free(node->annotations); - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DECLARATIONS_MODULE: { - rbs_ast_declarations_module_t *node = (rbs_ast_declarations_module_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - rbs_node_list_free(node->type_params); - rbs_node_list_free(node->self_types); - rbs_node_list_free(node->members); - rbs_node_list_free(node->annotations); - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DECLARATIONS_MODULE_SELF: { - rbs_ast_declarations_module_self_t *node = (rbs_ast_declarations_module_self_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - rbs_node_list_free(node->args); - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DECLARATIONS_MODULE_ALIAS: { - rbs_ast_declarations_module_alias_t *node = (rbs_ast_declarations_module_alias_t *)any_node; - - if (node->new_name != NULL) { - rbs_node_destroy((rbs_node_t *) node->new_name); - } - if (node->old_name != NULL) { - rbs_node_destroy((rbs_node_t *) node->old_name); - } - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - rbs_node_list_free(node->annotations); - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DECLARATIONS_TYPE_ALIAS: { - rbs_ast_declarations_type_alias_t *node = (rbs_ast_declarations_type_alias_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - rbs_node_list_free(node->type_params); - if (node->type != NULL) { - rbs_node_destroy((rbs_node_t *) node->type); - } - rbs_node_list_free(node->annotations); - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DIRECTIVES_USE: { - rbs_ast_directives_use_t *node = (rbs_ast_directives_use_t *)any_node; - - rbs_node_list_free(node->clauses); - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DIRECTIVES_USE_SINGLE_CLAUSE: { - rbs_ast_directives_use_single_clause_t *node = (rbs_ast_directives_use_single_clause_t *)any_node; - - if (node->type_name != NULL) { - rbs_node_destroy((rbs_node_t *) node->type_name); - } - if (node->new_name != NULL) { - rbs_node_destroy((rbs_node_t *) node->new_name); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_DIRECTIVES_USE_WILDCARD_CLAUSE: { - rbs_ast_directives_use_wildcard_clause_t *node = (rbs_ast_directives_use_wildcard_clause_t *)any_node; - - if (node->rbs_namespace != NULL) { - rbs_node_destroy((rbs_node_t *) node->rbs_namespace); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_INTEGER: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_ALIAS: { - rbs_ast_members_alias_t *node = (rbs_ast_members_alias_t *)any_node; - - if (node->new_name != NULL) { - rbs_node_destroy((rbs_node_t *) node->new_name); - } - if (node->old_name != NULL) { - rbs_node_destroy((rbs_node_t *) node->old_name); - } - if (node->kind != NULL) { - rbs_node_destroy((rbs_node_t *) node->kind); - } - rbs_node_list_free(node->annotations); - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_ATTR_ACCESSOR: { - rbs_ast_members_attr_accessor_t *node = (rbs_ast_members_attr_accessor_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - if (node->type != NULL) { - rbs_node_destroy((rbs_node_t *) node->type); - } - if (node->ivar_name != NULL) { - rbs_node_destroy((rbs_node_t *) node->ivar_name); - } - if (node->kind != NULL) { - rbs_node_destroy((rbs_node_t *) node->kind); - } - rbs_node_list_free(node->annotations); - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - if (node->visibility != NULL) { - rbs_node_destroy((rbs_node_t *) node->visibility); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_ATTR_READER: { - rbs_ast_members_attr_reader_t *node = (rbs_ast_members_attr_reader_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - if (node->type != NULL) { - rbs_node_destroy((rbs_node_t *) node->type); - } - if (node->ivar_name != NULL) { - rbs_node_destroy((rbs_node_t *) node->ivar_name); - } - if (node->kind != NULL) { - rbs_node_destroy((rbs_node_t *) node->kind); - } - rbs_node_list_free(node->annotations); - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - if (node->visibility != NULL) { - rbs_node_destroy((rbs_node_t *) node->visibility); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_ATTR_WRITER: { - rbs_ast_members_attr_writer_t *node = (rbs_ast_members_attr_writer_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - if (node->type != NULL) { - rbs_node_destroy((rbs_node_t *) node->type); - } - if (node->ivar_name != NULL) { - rbs_node_destroy((rbs_node_t *) node->ivar_name); - } - if (node->kind != NULL) { - rbs_node_destroy((rbs_node_t *) node->kind); - } - rbs_node_list_free(node->annotations); - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - if (node->visibility != NULL) { - rbs_node_destroy((rbs_node_t *) node->visibility); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_CLASS_INSTANCE_VARIABLE: { - rbs_ast_members_class_instance_variable_t *node = (rbs_ast_members_class_instance_variable_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - if (node->type != NULL) { - rbs_node_destroy((rbs_node_t *) node->type); - } - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_CLASS_VARIABLE: { - rbs_ast_members_class_variable_t *node = (rbs_ast_members_class_variable_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - if (node->type != NULL) { - rbs_node_destroy((rbs_node_t *) node->type); - } - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_EXTEND: { - rbs_ast_members_extend_t *node = (rbs_ast_members_extend_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - rbs_node_list_free(node->args); - rbs_node_list_free(node->annotations); - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_INCLUDE: { - rbs_ast_members_include_t *node = (rbs_ast_members_include_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - rbs_node_list_free(node->args); - rbs_node_list_free(node->annotations); - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_INSTANCE_VARIABLE: { - rbs_ast_members_instance_variable_t *node = (rbs_ast_members_instance_variable_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - if (node->type != NULL) { - rbs_node_destroy((rbs_node_t *) node->type); - } - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_METHOD_DEFINITION: { - rbs_ast_members_method_definition_t *node = (rbs_ast_members_method_definition_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - if (node->kind != NULL) { - rbs_node_destroy((rbs_node_t *) node->kind); - } - rbs_node_list_free(node->overloads); - rbs_node_list_free(node->annotations); - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - // overloading is a bool, so we don't need to free it. - if (node->visibility != NULL) { - rbs_node_destroy((rbs_node_t *) node->visibility); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_METHOD_DEFINITION_OVERLOAD: { - rbs_ast_members_method_definition_overload_t *node = (rbs_ast_members_method_definition_overload_t *)any_node; - - rbs_node_list_free(node->annotations); - if (node->method_type != NULL) { - rbs_node_destroy((rbs_node_t *) node->method_type); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_PREPEND: { - rbs_ast_members_prepend_t *node = (rbs_ast_members_prepend_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - rbs_node_list_free(node->args); - rbs_node_list_free(node->annotations); - if (node->comment != NULL) { - rbs_node_destroy((rbs_node_t *) node->comment); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_PRIVATE: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_MEMBERS_PUBLIC: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_STRING: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_AST_TYPE_PARAM: { - rbs_ast_type_param_t *node = (rbs_ast_type_param_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - if (node->variance != NULL) { - rbs_node_destroy((rbs_node_t *) node->variance); - } - if (node->upper_bound != NULL) { - rbs_node_destroy((rbs_node_t *) node->upper_bound); - } - if (node->default_type != NULL) { - rbs_node_destroy((rbs_node_t *) node->default_type); - } - // unchecked is a bool, so we don't need to free it. - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_METHOD_TYPE: { - rbs_method_type_t *node = (rbs_method_type_t *)any_node; - - rbs_node_list_free(node->type_params); - if (node->type != NULL) { - rbs_node_destroy((rbs_node_t *) node->type); - } - if (node->block != NULL) { - rbs_node_destroy((rbs_node_t *) node->block); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_NAMESPACE: { - rbs_namespace_t *node = (rbs_namespace_t *)any_node; - - rbs_node_list_free(node->path); - // absolute is a bool, so we don't need to free it. - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_SIGNATURE: { - rbs_signature_t *node = (rbs_signature_t *)any_node; - - rbs_node_list_free(node->directives); - rbs_node_list_free(node->declarations); - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPE_NAME: { - rbs_type_name_t *node = (rbs_type_name_t *)any_node; - - if (node->rbs_namespace != NULL) { - rbs_node_destroy((rbs_node_t *) node->rbs_namespace); - } - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_ALIAS: { - rbs_types_alias_t *node = (rbs_types_alias_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - rbs_node_list_free(node->args); - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_BASES_ANY: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_BASES_BOOL: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_BASES_BOTTOM: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_BASES_CLASS: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_BASES_INSTANCE: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_BASES_NIL: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_BASES_SELF: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_BASES_TOP: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_BASES_VOID: { - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_BLOCK: { - rbs_types_block_t *node = (rbs_types_block_t *)any_node; - - if (node->type != NULL) { - rbs_node_destroy((rbs_node_t *) node->type); - } - // required is a bool, so we don't need to free it. - if (node->self_type != NULL) { - rbs_node_destroy((rbs_node_t *) node->self_type); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_CLASS_INSTANCE: { - rbs_types_class_instance_t *node = (rbs_types_class_instance_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - rbs_node_list_free(node->args); - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_CLASS_SINGLETON: { - rbs_types_class_singleton_t *node = (rbs_types_class_singleton_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_FUNCTION: { - rbs_types_function_t *node = (rbs_types_function_t *)any_node; - - rbs_node_list_free(node->required_positionals); - rbs_node_list_free(node->optional_positionals); - if (node->rest_positionals != NULL) { - rbs_node_destroy((rbs_node_t *) node->rest_positionals); - } - rbs_node_list_free(node->trailing_positionals); - rbs_hash_free(node->required_keywords); - rbs_hash_free(node->optional_keywords); - if (node->rest_keywords != NULL) { - rbs_node_destroy((rbs_node_t *) node->rest_keywords); - } - if (node->return_type != NULL) { - rbs_node_destroy((rbs_node_t *) node->return_type); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_FUNCTION_PARAM: { - rbs_types_function_param_t *node = (rbs_types_function_param_t *)any_node; - - if (node->type != NULL) { - rbs_node_destroy((rbs_node_t *) node->type); - } - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_INTERFACE: { - rbs_types_interface_t *node = (rbs_types_interface_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - rbs_node_list_free(node->args); - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_INTERSECTION: { - rbs_types_intersection_t *node = (rbs_types_intersection_t *)any_node; - - rbs_node_list_free(node->types); - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_LITERAL: { - rbs_types_literal_t *node = (rbs_types_literal_t *)any_node; - - if (node->literal != NULL) { - rbs_node_destroy((rbs_node_t *) node->literal); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_OPTIONAL: { - rbs_types_optional_t *node = (rbs_types_optional_t *)any_node; - - if (node->type != NULL) { - rbs_node_destroy((rbs_node_t *) node->type); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_PROC: { - rbs_types_proc_t *node = (rbs_types_proc_t *)any_node; - - if (node->type != NULL) { - rbs_node_destroy((rbs_node_t *) node->type); - } - if (node->block != NULL) { - rbs_node_destroy((rbs_node_t *) node->block); - } - if (node->self_type != NULL) { - rbs_node_destroy((rbs_node_t *) node->self_type); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_RECORD: { - rbs_types_record_t *node = (rbs_types_record_t *)any_node; - - rbs_hash_free(node->all_fields); - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_RECORD_FIELD_TYPE: { - rbs_types_record_field_type_t *node = (rbs_types_record_field_type_t *)any_node; - - if (node->type != NULL) { - rbs_node_destroy((rbs_node_t *) node->type); - } - // required is a bool, so we don't need to free it. - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_TUPLE: { - rbs_types_tuple_t *node = (rbs_types_tuple_t *)any_node; - - rbs_node_list_free(node->types); - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_UNION: { - rbs_types_union_t *node = (rbs_types_union_t *)any_node; - - rbs_node_list_free(node->types); - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_UNTYPED_FUNCTION: { - rbs_types_untyped_function_t *node = (rbs_types_untyped_function_t *)any_node; - - if (node->return_type != NULL) { - rbs_node_destroy((rbs_node_t *) node->return_type); - } - break; - } -#line 202 "prism/templates/src/ast.c.erb" - case RBS_TYPES_VARIABLE: { - rbs_types_variable_t *node = (rbs_types_variable_t *)any_node; - - if (node->name != NULL) { - rbs_node_destroy((rbs_node_t *) node->name); - } - break; - } - case RBS_KEYWORD: - case RBS_AST_SYMBOL: { - // TODO: Delete symbol from constant pool? Or just free the whole constant pool? - break; - } - } - - // `any_node` will be freed automatically when the arena is freed. -} diff --git a/templates/include/rbs/ast.h.erb b/templates/include/rbs/ast.h.erb index 72b4cfe73..c45df362c 100644 --- a/templates/include/rbs/ast.h.erb +++ b/templates/include/rbs/ast.h.erb @@ -98,6 +98,4 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *, rbs_location_t *, rbs_co <%= node.c_type_name %> *<%= node.c_constructor_function_name %>(<%= node.constructor_params.map(&:parameter_decl).join(", ") %>); <%- end -%> -void rbs_node_destroy(rbs_node_t *any_node); - #endif diff --git a/templates/src/ast.c.erb b/templates/src/ast.c.erb index b92e74b24..543756782 100644 --- a/templates/src/ast.c.erb +++ b/templates/src/ast.c.erb @@ -28,17 +28,6 @@ rbs_node_list_t* rbs_node_list_new(rbs_allocator_t *allocator) { return list; } -void rbs_node_list_free(rbs_node_list_t *list) { - rbs_node_list_node_t *current = list->head; - while (current != NULL) { - rbs_node_list_node_t *next = current->next; - rbs_node_destroy(current->node); - // `current` is owned by the arena, so it will be freed automatically when the arena is freed. - current = next; - } - // `list` is owned by the arena, so it will be freed automatically when the arena is freed. -} - void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node) { rbs_node_list_node_t *new_node = rbs_allocator_alloc(list->allocator, rbs_node_list_node_t); *new_node = (rbs_node_list_node_t) { @@ -71,18 +60,6 @@ rbs_hash_t* rbs_hash_new(rbs_allocator_t *allocator) { return hash; } -void rbs_hash_free(rbs_hash_t *hash) { - rbs_hash_node_t *current = hash->head; - while (current != NULL) { - rbs_hash_node_t *next = current->next; - rbs_node_destroy(current->key); - rbs_node_destroy(current->value); - // `current` is owned by the arena, so it will be freed automatically when the arena is freed. - current = next; - } - // `hash` is owned by the arena, so it will be freed automatically when the arena is freed. -} - bool rbs_node_equal(rbs_node_t *lhs, rbs_node_t *rhs) { if (lhs == rhs) return true; if (lhs->type != rhs->type) return false; @@ -193,40 +170,3 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_location_t return instance; } <%- end -%> - -#line <%= __LINE__ + 1 %> "prism/templates/src/<%= File.basename(__FILE__) %>" -void rbs_node_destroy(rbs_node_t *any_node) { - switch (any_node->type) { - <%- nodes.each do |node| -%> -#line <%= __LINE__ + 1 %> "prism/templates/src/<%= File.basename(__FILE__) %>" - case <%= node.c_type_enum_name %>: { - <%- if node.has_children_to_free? -%> - <%= node.c_type_name %> *node = (<%= node.c_type_name %> *)any_node; - - <%- node.fields.each do |field| -%> - <%- case field.c_type -%> - <%- when "rbs_node_list" -%> - rbs_node_list_free(node-><%= field.c_name %>); - <%- when "rbs_hash" -%> - rbs_hash_free(node-><%= field.c_name %>); - <%- when "bool" -%> - // <%= field.c_name %> is a bool, so we don't need to free it. - <%- else -%> - if (node-><%= field.c_name %> != NULL) { - rbs_node_destroy((rbs_node_t *) node-><%= field.c_name %>); - } - <%- end -%> - <%- end -%> - <%- end -%> - break; - } - <%- end -%> - case RBS_KEYWORD: - case RBS_AST_SYMBOL: { - // TODO: Delete symbol from constant pool? Or just free the whole constant pool? - break; - } - } - - // `any_node` will be freed automatically when the arena is freed. -} diff --git a/templates/template.rb b/templates/template.rb index 0b1b06215..9e5114e3c 100644 --- a/templates/template.rb +++ b/templates/template.rb @@ -53,10 +53,6 @@ def ast_node? @c_type.include?("_decl_") || @c_type.include?("_types_") end - - def needs_to_be_freed? - !["VALUE", "bool", "rbs_string"].include?(@c_type) - end end class Type @@ -131,10 +127,6 @@ def expose_location? @expose_location end - def has_children_to_free? - @fields.any?(&:needs_to_be_freed?) - end - # Convert CamelCase to snake_case # e.g. "FooBarBaz" -> "foo_bar_baz" def camel_to_snake(str) From 563e081485049b7eeb8ad7360dba8c9f79ec1422 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Tue, 29 Apr 2025 22:44:23 +0800 Subject: [PATCH 108/111] Remove files that won't be upstreamed (#42) --- .idea/.gitignore | 8 -- .idea/misc.xml | 4 - .idea/modules.xml | 8 -- .idea/rbs.iml | 342 -------------------------------------------- .idea/vcs.xml | 6 - .vscode/launch.json | 118 --------------- demo.rb | 23 --- make_signed_ruby.rb | 190 ------------------------ sync_from_prism.rb | 37 ----- trace_rbs.sh | 99 ------------- 10 files changed, 835 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/rbs.iml delete mode 100644 .idea/vcs.xml delete mode 100644 .vscode/launch.json delete mode 100644 demo.rb delete mode 100644 make_signed_ruby.rb delete mode 100644 sync_from_prism.rb delete mode 100755 trace_rbs.sh diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b81b..000000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 378540077..000000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 40be3710b..000000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/rbs.iml b/.idea/rbs.iml deleted file mode 100644 index e15d86f08..000000000 --- a/.idea/rbs.iml +++ /dev/null @@ -1,342 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1ddfb..000000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 4e7f7906c..000000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,118 +0,0 @@ -// Running these tasks does not trigger a recompile, so you'll need to do that yourself. -// -// Make sure you're using a debug build when debugging. -// Add `append_cflags ['-O0', '-g']` to extconf.rb to enable this. -// -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "lldb", - "request": "launch", - "name": "Debug all tests in LLDB", - "program": "/opt/rubies/3.3.4/bin/ruby", - "args": [ - "-w", - "-Ilib", - "-Itest", - "/Users/alex/.gem/ruby/3.3.0/gems/rake-13.2.1/lib/rake/rake_test_loader.rb", - "test/rbs/ancestor_builder_test.rb", - "test/rbs/ancestor_graph_test.rb", - "test/rbs/annotate/annotations_test.rb", - "test/rbs/annotate/rdoc_annotator_test.rb", - "test/rbs/annotate/rdoc_source_test.rb", - "test/rbs/ast/type_param_test.rb", - "test/rbs/ast/visitor_test.rb", - "test/rbs/buffer_test.rb", - "test/rbs/cli_test.rb", - "test/rbs/collection/cleaner_test.rb", - "test/rbs/collection/config_test.rb", - "test/rbs/collection/installer_test.rb", - "test/rbs/collection/sources/git_test.rb", - "test/rbs/collection/sources/local_test.rb", - "test/rbs/collection/sources/stdlib_test.rb", - "test/rbs/definition_builder_test.rb", - "test/rbs/diff_test.rb", - "test/rbs/environment_loader_test.rb", - "test/rbs/environment_test.rb", - "test/rbs/environment_walker_test.rb", - "test/rbs/errors_test.rb", - "test/rbs/factory_test.rb", - "test/rbs/file_finder_test.rb", - "test/rbs/location_test.rb", - "test/rbs/locator_test.rb", - "test/rbs/method_builder_test.rb", - "test/rbs/method_type_parsing_test.rb", - "test/rbs/node_usage_test.rb", - "test/rbs/parser_test.rb", - "test/rbs/rb_prototype_test.rb", - "test/rbs/rbi_prototype_test.rb", - "test/rbs/rdoc/rbs_parser_test.rb", - "test/rbs/repository_test.rb", - "test/rbs/resolver/constant_resolver_test.rb", - "test/rbs/resolver/type_name_resolver_test.rb", - "test/rbs/runtime_prototype_test.rb", - "test/rbs/schema_test.rb", - "test/rbs/signature_parsing_test.rb", - "test/rbs/sorter_test.rb", - "test/rbs/subtractor_test.rb", - "test/rbs/test/hook_test.rb", - "test/rbs/test/runtime_test_test.rb", - "test/rbs/test/setup_helper_test.rb", - "test/rbs/test/tester_test.rb", - "test/rbs/test/type_check_test.rb", - "test/rbs/type_alias_dependency_test.rb", - "test/rbs/type_alias_regulartiry_test.rb", - "test/rbs/type_parsing_test.rb", - ], - "cwd": "${workspaceFolder}", - "stopOnEntry": false, - }, - { - "type": "lldb", - "request": "launch", - "name": "Debug test in LLDB", - "program": "/opt/rubies/3.3.4/bin/ruby", - "args": [ - "-Ilib", - "-Itest", - "${workspaceFolder}/bin/test_runner.rb", - "${workspaceFolder}/test/rbs/location_test.rb" - ], - "cwd": "${workspaceFolder}", - "stopOnEntry": false, - }, - { - "type": "lldb", - "request": "launch", - "name": "Debug test/rbs/location_test.rb", - "program": "/opt/rubies/3.3.4/bin/ruby", - "args": [ - "-Ilib", - "-Itest", - "${workspaceFolder}/bin/test_runner.rb", - "${workspaceFolder}/test/rbs/location_test.rb" - ], - "cwd": "${workspaceFolder}", - "stopOnEntry": false, - }, - { - "type": "lldb", - "request": "launch", - "name": "Debug test/rbs/ancestor_builder_test.rb", - "program": "/opt/rubies/3.3.4/bin/ruby", - "args": [ - "-Ilib", - "-Itest", - "${workspaceFolder}/bin/test_runner.rb", - "${workspaceFolder}/test/rbs/ancestor_builder_test.rb" - ], - "cwd": "${workspaceFolder}", - "stopOnEntry": false, - }, - - ] -} diff --git a/demo.rb b/demo.rb deleted file mode 100644 index 997901935..000000000 --- a/demo.rb +++ /dev/null @@ -1,23 +0,0 @@ -require 'rubyvm/instruction_sequence' - -# Example Ruby code -code = <<~RUBY - class C - def x - [1, 2, 3] - end - - def hash - [1, 2, 3].hash - end - end -RUBY - -# Compile the Ruby code into instruction sequences -iseq = RubyVM::InstructionSequence.compile(code) - -# Disassemble the instruction sequences -disassembled_code = iseq.disasm - -# Print disassembled code -puts disassembled_code diff --git a/make_signed_ruby.rb b/make_signed_ruby.rb deleted file mode 100644 index 11ded48e1..000000000 --- a/make_signed_ruby.rb +++ /dev/null @@ -1,190 +0,0 @@ -# This script copies and signs the current `ruby` executable, -# so that it can be run in a debugger or profiler, like Xcode's -# Instruments. -# -# The output folder is configured by `destination_folder` below, -# but it's just the current dir by default. -# This script will create a new `Ruby.app` bundle in that dest. -# -# Before running this script: -# -# 1. Create a signing certificate for yourself. -# -# Follow the instructions under the heading "To obtain a self-signed certificate using Certificate Assistant" -# https://developer.apple.com/library/archive/documentation/Security/Conceptual/CodeSigningGuide/Procedures/Procedures.html -# -# The defaults worked for me, so I didn't need to check "Let me override defaults". -# -# Once done, it should show up when you run `security find-identity` -# -# 2. Set `$signing_cert_name` to the name of your new certificate -# -# 3. Run `sudo DevToolsSecurity -enable` (Optional) -# This will make it so you don't get prompted for a password every time you run a debugger/profiler. -# -# After running this script, you can either use the `dest/Ruby.app/Contents/macOS/ruby` binary from there, -# or copy it to another place of your choosing. - -require "rbconfig" -require "pathname" -require "tempfile" - -# Inputs -$signing_cert_name = "'Alexander Momchilov (Shopify)'" -destination_folder = Pathname.pwd - -app_bundle = create_bundle_skeleton(at: destination_folder) - -copy_ruby_executable(into: app_bundle) - -sign_bundle(app_bundle) - -if verify_entitlements(app_bundle) - puts "Successfully created a signed Ruby at: #{app_bundle}" - exit(true) -else - puts "Something went wrong." - exit(false) -end - - - - - -BEGIN { # Helper methods - def create_bundle_skeleton(at:) - destination = at - - app_bundle = (destination / "Ruby.app").expand_path - contents_folder = app_bundle / "Contents" - - contents_folder.mkpath - - info_plist_content = <<~INFO_PLIST - - - - - CFBundleInfoDictionaryVersion - 6.0 - CFBundleExecutable - ruby - CFBundleIdentifier - com.shopify.amomchilov.Ruby - CFBundleName - Ruby - CFBundleDisplayName - Ruby - CFBundleShortVersionString - #{RUBY_VERSION} - CFBundleVersion - #{RUBY_VERSION}p#{RUBY_PATCHLEVEL} - NSHumanReadableCopyright - #{RUBY_COPYRIGHT.delete_prefix("ruby - ")} - - - INFO_PLIST - - (contents_folder / "Info.plist").write(info_plist_content) - - app_bundle - end - - def copy_ruby_executable(into:) - app_bundle = into - destination = (app_bundle / "Contents/MacOS") - - begin - destination.mkpath - rescue Errno::EEXIST - # Folder already exists. No problem. - end - - original_ruby = Pathname.new(RbConfig.ruby) - puts "Copying Ruby #{RUBY_VERSION} from #{original_ruby}" - FileUtils.cp(original_ruby, destination) - destination - end - - def sign_bundle(bundle_path) - entitlements_plist = <<~PLIST - - - - - com.apple.security.get-task-allow - - - - com.apple.security.cs.disable-library-validation - - - - PLIST - - Tempfile.create("entitlements.plist") do |entitlements_file| - args = [ - "/usr/bin/codesign", - "--force", # Replace the existing signiture, if any - "--sign", $signing_cert_name, - "-o", "runtime", - "--entitlements", entitlements_file.path, - "--timestamp\\=none", - "--generate-entitlement-der", # necessary? - "#{bundle_path}" - ] - - entitlements_file.puts(entitlements_plist) - entitlements_file.flush - - puts "\nSigning #{bundle_path}." - puts "----- codesign output:" - system(args.join(" "), exception: true) - puts "-----\n\n" - end - - nil - end - - def verify_entitlements(bundle_path) - puts "Verifying the code signature..." - entitlements_xml = `codesign --display --entitlements - --xml #{bundle_path} 2>/dev/null` - - disable_lib_validation = "com.apple.security.cs.disable-library-validation" - allow_debugging = "com.apple.security.get-task-allow" - - issues = [] - - # Doing dumb string matching, so we don't need to pull in an XML/Plist parser. - if entitlements_xml.include?(disable_lib_validation) - if entitlements_xml.include?("#{disable_lib_validation}") - puts "\t- ✅ Entitlement #{disable_lib_validation.inspect} was set correctly" - else - issues << "\t- ❌ #{disable_lib_validation.inspect} was not `true` in the bundle's entitlements." - end - else - issues << "\t- ❌ #{disable_lib_validation.inspect} was missing from the bundle's entitlements." - end - - if entitlements_xml.include?(allow_debugging) - if entitlements_xml.include?("#{allow_debugging}") - puts "\t- ✅ Entitlement #{allow_debugging.inspect} was set correctly" - else - issues << "\t- ❌ #{allow_debugging.inspect} was not `true` in the bundle's entitlements." - end - else - issues << "\t- ❌ #{allow_debugging.inspect} was missing from the bundle's entitlements." - end - - if issues.any? - puts "There were issues with the code-signing:" - puts issues - puts - return false - end - - puts - - true - end -} diff --git a/sync_from_prism.rb b/sync_from_prism.rb deleted file mode 100644 index a4b23ef9e..000000000 --- a/sync_from_prism.rb +++ /dev/null @@ -1,37 +0,0 @@ -require "Pathname" - -# This script assumes you have this layout: -# ├── Ruby -# │ └── prism -# └── Shopify -# └── rbs <---- current working directory - -import_from_prism("../../ruby/prism/include/prism/defines.h") -import_from_prism("../../ruby/prism/include/prism/util/pm_constant_pool.h") -import_from_prism("../../ruby/prism/src/util/pm_constant_pool.c") - -BEGIN { - def import_from_prism(prism_file_path_str) - prism_file_path = Pathname.new(prism_file_path_str).realpath - puts prism_file_path - - dest_file_path_str = "./" + prism_file_path_str.delete_prefix("../../ruby/prism/") - dest_file_path_str.gsub!("include/prism/", "include/rbs/") - dest_file_path_str.gsub!("pm_", "rbs_") - - dest_file_path = Pathname.new(dest_file_path_str).expand_path - puts dest_file_path.dirname.mkpath - - puts "Importing \"#{prism_file_path.relative_path_from(Pathname.pwd)}\" to \"#{dest_file_path.relative_path_from(Pathname.pwd)}\"" - - contents = File.read(prism_file_path) - - contents.gsub!("PRISM", "RBS") - contents.gsub!("Prism", "RBS") - contents.gsub!("prism", "rbs") - contents.gsub!("pm_", "rbs_") - contents.gsub!("PM_", "RBS_") - - File.write(dest_file_path, contents) - end -} diff --git a/trace_rbs.sh b/trace_rbs.sh deleted file mode 100755 index a88300336..000000000 --- a/trace_rbs.sh +++ /dev/null @@ -1,99 +0,0 @@ -# Profile the RBS tests in Instruments. -# -# Based on https://www.jviotti.com/2024/01/29/using-xcode-instruments-for-cpp-cpu-profiling.html -# -# The usual Instruments templates are: -# --template 'Allocations' -# --template 'CPU Profiler' -# -# ...but I made myself a custom template called "Ruby" with these instruments: -# - Allocations -# - Points of Integer -# - os_signpost -# - stdout/stderr -# - Time Profiler -# - Sampler -# -# The "Leaks" instrument doesn't work, and IDK why... some issue with libmalloc. - -cd /Users/alex/src/github.com/Shopify/rbs - -source /opt/dev/sh/chruby/chruby.sh -chruby 3.3.4 - -TRACE_FILE="$(mktemp -t trace_XXXXXX).trace" - -echo "Will save trace file to $TRACE_FILE" - -xcrun xctrace record \ - --template 'Ruby' \ - --no-prompt \ - --output "$TRACE_FILE" \ - --target-stdout - \ - --launch \ - -- \ - /Users/alex/Downloads/ruby \ - -w -Ilib -Itest \ - /Users/alex/.gem/ruby/3.3.0/gems/rake-13.2.1/lib/rake/rake_test_loader.rb \ - "test/rbs/signature_parsing_test.rb" \ - -# "test/rbs/ancestor_builder_test.rb" \ -# "test/rbs/ancestor_graph_test.rb" \ -# "test/rbs/annotate/annotations_test.rb" \ -# "test/rbs/annotate/rdoc_annotator_test.rb" \ -# "test/rbs/annotate/rdoc_source_test.rb" \ -# "test/rbs/ast/type_param_test.rb" \ -# "test/rbs/ast/visitor_test.rb" \ -# "test/rbs/buffer_test.rb" \ -# "test/rbs/cli_test.rb" \ -# "test/rbs/collection/cleaner_test.rb" \ -# "test/rbs/collection/config_test.rb" \ -# "test/rbs/collection/installer_test.rb" \ -# "test/rbs/collection/sources/git_test.rb" \ -# "test/rbs/collection/sources/local_test.rb" \ -# "test/rbs/collection/sources/stdlib_test.rb" \ -# "test/rbs/definition_builder_test.rb" \ -# "test/rbs/diff_test.rb" \ -# "test/rbs/environment_loader_test.rb" \ -# "test/rbs/environment_test.rb" \ -# "test/rbs/environment_walker_test.rb" \ -# "test/rbs/errors_test.rb" \ -# "test/rbs/factory_test.rb" \ -# "test/rbs/file_finder_test.rb" \ -# "test/rbs/location_test.rb" \ -# "test/rbs/locator_test.rb" \ -# "test/rbs/method_builder_test.rb" \ -# "test/rbs/method_type_parsing_test.rb" \ -# "test/rbs/node_usage_test.rb" \ -# "test/rbs/parser_test.rb" \ -# "test/rbs/rb_prototype_test.rb" \ -# "test/rbs/rbi_prototype_test.rb" \ -# "test/rbs/rdoc/rbs_parser_test.rb" \ -# "test/rbs/repository_test.rb" \ -# "test/rbs/resolver/constant_resolver_test.rb" \ -# "test/rbs/resolver/type_name_resolver_test.rb" \ -# "test/rbs/runtime_prototype_test.rb" \ -# "test/rbs/schema_test.rb" \ -# "test/rbs/sorter_test.rb" \ -# "test/rbs/subtractor_test.rb" \ -# "test/rbs/test/hook_test.rb" \ -# "test/rbs/test/runtime_test_test.rb" \ -# "test/rbs/test/setup_helper_test.rb" \ -# "test/rbs/test/tester_test.rb" \ -# "test/rbs/test/type_check_test.rb" \ -# "test/rbs/type_alias_dependency_test.rb" \ -# "test/rbs/type_alias_regulartiry_test.rb" \ -# "test/rbs/type_parsing_test.rb" \ -# "test/rbs/types_test.rb" \ -# "test/rbs/use_map_test.rb" \ -# "test/rbs/variance_calculator_test.rb" \ -# "test/rbs/vendorer_test.rb" \ -# "test/rbs/writer_test.rb" \ -# "test/validator_test.rb" - -echo "xtrace exited with code $?" -# 2 => fail -# 0 => success -# 54 => also success? - -open "$TRACE_FILE" From 0a8a4c52a7f3a8b7ed69b81a36f5eda1b326e80b Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 30 Apr 2025 21:23:00 +0800 Subject: [PATCH 109/111] Apply suggestions from code review Co-authored-by: Alexander Momchilov --- ext/rbs_extension/main.c | 10 +++++----- src/parser.c | 16 ++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/ext/rbs_extension/main.c b/ext/rbs_extension/main.c index 9dbb3747c..8ab04615d 100644 --- a/ext/rbs_extension/main.c +++ b/ext/rbs_extension/main.c @@ -273,7 +273,7 @@ static VALUE rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE start_pos } static VALUE parse_inline_leading_annotation_try(VALUE a) { - struct parse_type_arg *arg = (struct parse_type_arg *)a; + struct parse_type_arg *arg = (struct parse_type_arg *) a; rbs_parser_t *parser = arg->parser; rbs_ast_ruby_annotations_t *annotation = NULL; @@ -291,7 +291,7 @@ static VALUE parse_inline_leading_annotation_try(VALUE a) { arg->encoding ); - return rbs_struct_to_ruby_value(ctx, (rbs_node_t *)annotation); + return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) annotation); } static VALUE rbsparser_parse_inline_leading_annotation(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables) { @@ -316,7 +316,7 @@ static VALUE rbsparser_parse_inline_leading_annotation(VALUE self, VALUE buffer, } static VALUE parse_inline_trailing_annotation_try(VALUE a) { - struct parse_type_arg *arg = (struct parse_type_arg *)a; + struct parse_type_arg *arg = (struct parse_type_arg *) a; rbs_parser_t *parser = arg->parser; rbs_ast_ruby_annotations_t *annotation = NULL; @@ -334,7 +334,7 @@ static VALUE parse_inline_trailing_annotation_try(VALUE a) { arg->encoding ); - return rbs_struct_to_ruby_value(ctx, (rbs_node_t *)annotation); + return rbs_struct_to_ruby_value(ctx, (rbs_node_t *) annotation); } static VALUE rbsparser_parse_inline_trailing_annotation(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables) { @@ -351,7 +351,7 @@ static VALUE rbsparser_parse_inline_trailing_annotation(VALUE self, VALUE buffer .require_eof = Qfalse }; - VALUE result = rb_ensure(parse_inline_trailing_annotation_try, (VALUE)&arg, ensure_free_parser, (VALUE)parser); + VALUE result = rb_ensure(parse_inline_trailing_annotation_try, (VALUE) &arg, ensure_free_parser, (VALUE) parser); RB_GC_GUARD(string); diff --git a/src/parser.c b/src/parser.c index ced60ead1..557edcd18 100644 --- a/src/parser.c +++ b/src/parser.c @@ -3509,9 +3509,9 @@ static bool parse_inline_method_overloads(rbs_parser_t *parser, rbs_node_list_t ALLOCATOR(), location, annotations, - (rbs_node_t*)method_type + (rbs_node_t *) method_type ); - rbs_node_list_append(overloads, (rbs_node_t*)overload); + rbs_node_list_append(overloads, (rbs_node_t *) overload); if (parser->next_token.type == pBAR) { rbs_location_t *bar_location = rbs_location_new(ALLOCATOR(), parser->next_token.range); @@ -3564,12 +3564,12 @@ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_a rbs_location_t *full_loc = rbs_location_new(ALLOCATOR(), full_range); rbs_location_t *colon_loc = rbs_location_new(ALLOCATOR(), colon_range); - *annotation = (rbs_ast_ruby_annotations_t*)rbs_ast_ruby_annotations_colon_method_type_annotation_new( + *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_colon_method_type_annotation_new( ALLOCATOR(), full_loc, colon_loc, annotations, - (rbs_node_t*)method_type + (rbs_node_t *) method_type ); return true; } @@ -3597,7 +3597,7 @@ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_a rbs_location_t *full_loc = rbs_location_new(ALLOCATOR(), full_range); rbs_location_t *rbs_loc = rbs_location_new(ALLOCATOR(), rbs_range); - *annotation = (rbs_ast_ruby_annotations_t*)rbs_ast_ruby_annotations_method_types_annotation_new( + *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_method_types_annotation_new( ALLOCATOR(), full_loc, rbs_loc, @@ -3625,7 +3625,7 @@ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_a rbs_location_t *full_loc = rbs_location_new(ALLOCATOR(), full_range); rbs_location_t *rbs_loc = rbs_location_new(ALLOCATOR(), rbs_range); - *annotation = (rbs_ast_ruby_annotations_t*)rbs_ast_ruby_annotations_skip_annotation_new( + *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_skip_annotation_new( ALLOCATOR(), full_loc, rbs_loc, @@ -3663,7 +3663,7 @@ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_a rbs_location_t *full_loc = rbs_location_new(ALLOCATOR(), full_range); rbs_location_t *rbs_loc = rbs_location_new(ALLOCATOR(), rbs_range); - *annotation = (rbs_ast_ruby_annotations_t*)rbs_ast_ruby_annotations_return_type_annotation_new( + *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_return_type_annotation_new( ALLOCATOR(), full_loc, rbs_loc, @@ -3708,7 +3708,7 @@ static bool parse_inline_trailing_annotation(rbs_parser_t *parser, rbs_ast_ruby_ rbs_location_t *full_loc = rbs_location_new(ALLOCATOR(), full_range); rbs_location_t *prefix_loc = rbs_location_new(ALLOCATOR(), prefix_range); - *annotation = (rbs_ast_ruby_annotations_t*)rbs_ast_ruby_annotations_node_type_assertion_new( + *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_node_type_assertion_new( ALLOCATOR(), full_loc, prefix_loc, From a9e77021ee01723ea905d1756ad7f5af5b8a4205 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 30 Apr 2025 21:23:30 +0800 Subject: [PATCH 110/111] Update src/parser.c Co-authored-by: Alexander Momchilov --- src/parser.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/parser.c b/src/parser.c index 557edcd18..505c677f5 100644 --- a/src/parser.c +++ b/src/parser.c @@ -3529,15 +3529,15 @@ static bool parse_inline_method_overloads(rbs_parser_t *parser, rbs_node_list_t NODISCARD static bool parse_inline_comment(rbs_parser_t *parser, rbs_location_t **comment) { - if (parser->next_token.type == tINLINECOMMENT) { - rbs_range_t comment_range = parser->next_token.range; - rbs_parser_advance(parser); - - *comment = rbs_location_new(ALLOCATOR(), comment_range); + if (parser->next_token.type != tINLINECOMMENT) { + *comment = NULL; return true; } - *comment = NULL; + rbs_range_t comment_range = parser->next_token.range; + rbs_parser_advance(parser); + + *comment = rbs_location_new(ALLOCATOR(), comment_range); return true; } From 514745b3789d5d8ad6e69794c4746a4e831d25db Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Wed, 30 Apr 2025 21:24:11 +0800 Subject: [PATCH 111/111] Update src/parser.c --- src/parser.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/parser.c b/src/parser.c index 505c677f5..5b559e40a 100644 --- a/src/parser.c +++ b/src/parser.c @@ -3545,7 +3545,6 @@ NODISCARD static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation) { switch (parser->next_token.type) { case pCOLON: { - // : rbs_range_t colon_range = parser->next_token.range; rbs_parser_advance(parser);