rbs 4.0.0.dev.4 → 4.0.0.dev.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +14 -14
  3. data/.github/workflows/bundle-update.yml +60 -0
  4. data/.github/workflows/c-check.yml +11 -8
  5. data/.github/workflows/comments.yml +3 -3
  6. data/.github/workflows/dependabot.yml +1 -1
  7. data/.github/workflows/ruby.yml +17 -34
  8. data/.github/workflows/typecheck.yml +2 -2
  9. data/.github/workflows/valgrind.yml +42 -0
  10. data/.github/workflows/windows.yml +2 -2
  11. data/.rubocop.yml +1 -1
  12. data/README.md +1 -1
  13. data/Rakefile +32 -5
  14. data/config.yml +46 -0
  15. data/core/array.rbs +96 -46
  16. data/core/binding.rbs +0 -2
  17. data/core/builtin.rbs +2 -2
  18. data/core/comparable.rbs +13 -6
  19. data/core/complex.rbs +55 -41
  20. data/core/dir.rbs +4 -4
  21. data/core/encoding.rbs +7 -10
  22. data/core/enumerable.rbs +90 -3
  23. data/core/enumerator/arithmetic_sequence.rbs +70 -0
  24. data/core/enumerator.rbs +63 -1
  25. data/core/errno.rbs +8 -0
  26. data/core/errors.rbs +28 -1
  27. data/core/exception.rbs +2 -2
  28. data/core/fiber.rbs +40 -20
  29. data/core/file.rbs +108 -78
  30. data/core/file_test.rbs +1 -1
  31. data/core/float.rbs +225 -69
  32. data/core/gc.rbs +417 -281
  33. data/core/hash.rbs +1023 -727
  34. data/core/integer.rbs +104 -110
  35. data/core/io/buffer.rbs +21 -10
  36. data/core/io/wait.rbs +11 -33
  37. data/core/io.rbs +82 -19
  38. data/core/kernel.rbs +70 -59
  39. data/core/marshal.rbs +1 -1
  40. data/core/match_data.rbs +1 -1
  41. data/core/math.rbs +42 -3
  42. data/core/method.rbs +63 -27
  43. data/core/module.rbs +103 -26
  44. data/core/nil_class.rbs +3 -3
  45. data/core/numeric.rbs +43 -35
  46. data/core/object.rbs +3 -3
  47. data/core/object_space.rbs +21 -15
  48. data/core/pathname.rbs +1272 -0
  49. data/core/proc.rbs +30 -25
  50. data/core/process.rbs +4 -2
  51. data/core/ractor.rbs +361 -509
  52. data/core/random.rbs +17 -0
  53. data/core/range.rbs +113 -16
  54. data/core/rational.rbs +56 -85
  55. data/core/rbs/unnamed/argf.rbs +2 -2
  56. data/core/rbs/unnamed/env_class.rbs +1 -1
  57. data/core/rbs/unnamed/random.rbs +4 -113
  58. data/core/regexp.rbs +25 -20
  59. data/core/ruby.rbs +53 -0
  60. data/core/ruby_vm.rbs +6 -4
  61. data/core/rubygems/errors.rbs +3 -70
  62. data/core/rubygems/rubygems.rbs +11 -79
  63. data/core/rubygems/version.rbs +2 -3
  64. data/core/set.rbs +488 -359
  65. data/core/signal.rbs +24 -14
  66. data/core/string.rbs +3171 -1241
  67. data/core/struct.rbs +1 -1
  68. data/core/symbol.rbs +17 -11
  69. data/core/thread.rbs +95 -33
  70. data/core/time.rbs +35 -9
  71. data/core/trace_point.rbs +7 -4
  72. data/core/unbound_method.rbs +14 -6
  73. data/docs/aliases.md +79 -0
  74. data/docs/collection.md +2 -2
  75. data/docs/encoding.md +56 -0
  76. data/docs/gem.md +0 -1
  77. data/docs/inline.md +470 -0
  78. data/docs/sigs.md +3 -3
  79. data/docs/syntax.md +33 -4
  80. data/docs/type_fingerprint.md +21 -0
  81. data/exe/rbs +1 -1
  82. data/ext/rbs_extension/ast_translation.c +77 -3
  83. data/ext/rbs_extension/ast_translation.h +3 -0
  84. data/ext/rbs_extension/class_constants.c +8 -2
  85. data/ext/rbs_extension/class_constants.h +4 -0
  86. data/ext/rbs_extension/extconf.rb +5 -1
  87. data/ext/rbs_extension/legacy_location.c +5 -5
  88. data/ext/rbs_extension/main.c +37 -20
  89. data/include/rbs/ast.h +85 -38
  90. data/include/rbs/defines.h +27 -0
  91. data/include/rbs/lexer.h +30 -11
  92. data/include/rbs/parser.h +6 -6
  93. data/include/rbs/string.h +0 -2
  94. data/include/rbs/util/rbs_allocator.h +34 -13
  95. data/include/rbs/util/rbs_assert.h +12 -1
  96. data/include/rbs/util/rbs_encoding.h +2 -0
  97. data/include/rbs/util/rbs_unescape.h +2 -1
  98. data/lib/rbs/ast/annotation.rb +1 -1
  99. data/lib/rbs/ast/comment.rb +1 -1
  100. data/lib/rbs/ast/declarations.rb +10 -10
  101. data/lib/rbs/ast/members.rb +14 -14
  102. data/lib/rbs/ast/ruby/annotations.rb +137 -0
  103. data/lib/rbs/ast/ruby/comment_block.rb +24 -0
  104. data/lib/rbs/ast/ruby/declarations.rb +198 -3
  105. data/lib/rbs/ast/ruby/helpers/constant_helper.rb +4 -0
  106. data/lib/rbs/ast/ruby/members.rb +159 -1
  107. data/lib/rbs/ast/type_param.rb +24 -4
  108. data/lib/rbs/buffer.rb +20 -15
  109. data/lib/rbs/cli/diff.rb +16 -15
  110. data/lib/rbs/cli/validate.rb +38 -51
  111. data/lib/rbs/cli.rb +52 -19
  112. data/lib/rbs/collection/config/lockfile_generator.rb +8 -0
  113. data/lib/rbs/collection/sources/git.rb +1 -0
  114. data/lib/rbs/definition.rb +1 -1
  115. data/lib/rbs/definition_builder/ancestor_builder.rb +62 -9
  116. data/lib/rbs/definition_builder/method_builder.rb +20 -0
  117. data/lib/rbs/definition_builder.rb +91 -2
  118. data/lib/rbs/diff.rb +7 -1
  119. data/lib/rbs/environment.rb +227 -74
  120. data/lib/rbs/environment_loader.rb +0 -6
  121. data/lib/rbs/errors.rb +27 -7
  122. data/lib/rbs/inline_parser.rb +341 -5
  123. data/lib/rbs/location_aux.rb +1 -1
  124. data/lib/rbs/locator.rb +5 -1
  125. data/lib/rbs/method_type.rb +5 -3
  126. data/lib/rbs/parser_aux.rb +2 -2
  127. data/lib/rbs/prototype/rb.rb +2 -2
  128. data/lib/rbs/prototype/rbi.rb +2 -0
  129. data/lib/rbs/prototype/runtime.rb +8 -0
  130. data/lib/rbs/resolver/constant_resolver.rb +2 -2
  131. data/lib/rbs/resolver/type_name_resolver.rb +116 -38
  132. data/lib/rbs/subtractor.rb +3 -1
  133. data/lib/rbs/test/type_check.rb +16 -2
  134. data/lib/rbs/type_name.rb +1 -1
  135. data/lib/rbs/types.rb +27 -27
  136. data/lib/rbs/validator.rb +2 -2
  137. data/lib/rbs/version.rb +1 -1
  138. data/lib/rbs.rb +1 -1
  139. data/lib/rdoc/discover.rb +1 -1
  140. data/lib/rdoc_plugin/parser.rb +1 -1
  141. data/rbs.gemspec +3 -2
  142. data/schema/typeParam.json +17 -1
  143. data/sig/ast/ruby/annotations.rbs +124 -0
  144. data/sig/ast/ruby/comment_block.rbs +8 -0
  145. data/sig/ast/ruby/declarations.rbs +102 -4
  146. data/sig/ast/ruby/members.rbs +87 -1
  147. data/sig/cli/diff.rbs +5 -11
  148. data/sig/cli/validate.rbs +13 -4
  149. data/sig/cli.rbs +18 -18
  150. data/sig/definition.rbs +6 -1
  151. data/sig/environment.rbs +70 -12
  152. data/sig/errors.rbs +13 -6
  153. data/sig/inline_parser.rbs +39 -2
  154. data/sig/locator.rbs +0 -2
  155. data/sig/manifest.yaml +0 -1
  156. data/sig/method_builder.rbs +3 -1
  157. data/sig/method_types.rbs +1 -1
  158. data/sig/parser.rbs +16 -2
  159. data/sig/resolver/type_name_resolver.rbs +35 -7
  160. data/sig/source.rbs +3 -3
  161. data/sig/type_param.rbs +13 -8
  162. data/sig/types.rbs +4 -4
  163. data/src/ast.c +80 -1
  164. data/src/lexer.c +1392 -1313
  165. data/src/lexer.re +3 -0
  166. data/src/lexstate.c +58 -37
  167. data/src/location.c +4 -4
  168. data/src/parser.c +412 -145
  169. data/src/string.c +0 -48
  170. data/src/util/rbs_allocator.c +89 -71
  171. data/src/util/rbs_assert.c +1 -1
  172. data/src/util/rbs_buffer.c +2 -2
  173. data/src/util/rbs_constant_pool.c +10 -10
  174. data/src/util/rbs_encoding.c +4 -8
  175. data/src/util/rbs_unescape.c +56 -20
  176. data/stdlib/bigdecimal/0/big_decimal.rbs +100 -82
  177. data/stdlib/bigdecimal-math/0/big_math.rbs +169 -8
  178. data/stdlib/cgi/0/core.rbs +9 -393
  179. data/stdlib/cgi/0/manifest.yaml +1 -0
  180. data/stdlib/cgi-escape/0/escape.rbs +171 -0
  181. data/stdlib/coverage/0/coverage.rbs +3 -1
  182. data/stdlib/date/0/date.rbs +67 -59
  183. data/stdlib/date/0/date_time.rbs +1 -1
  184. data/stdlib/delegate/0/delegator.rbs +10 -7
  185. data/stdlib/digest/0/digest.rbs +110 -0
  186. data/stdlib/erb/0/erb.rbs +737 -347
  187. data/stdlib/fileutils/0/fileutils.rbs +20 -14
  188. data/stdlib/forwardable/0/forwardable.rbs +3 -0
  189. data/stdlib/json/0/json.rbs +82 -28
  190. data/stdlib/net-http/0/net-http.rbs +3 -0
  191. data/stdlib/objspace/0/objspace.rbs +9 -27
  192. data/stdlib/open-uri/0/open-uri.rbs +40 -0
  193. data/stdlib/open3/0/open3.rbs +459 -1
  194. data/stdlib/openssl/0/openssl.rbs +331 -228
  195. data/stdlib/optparse/0/optparse.rbs +8 -3
  196. data/stdlib/pathname/0/pathname.rbs +9 -1379
  197. data/stdlib/psych/0/psych.rbs +4 -4
  198. data/stdlib/random-formatter/0/random-formatter.rbs +277 -0
  199. data/stdlib/rdoc/0/code_object.rbs +2 -1
  200. data/stdlib/rdoc/0/parser.rbs +1 -1
  201. data/stdlib/rdoc/0/rdoc.rbs +1 -1
  202. data/stdlib/rdoc/0/store.rbs +1 -1
  203. data/stdlib/resolv/0/resolv.rbs +25 -68
  204. data/stdlib/ripper/0/ripper.rbs +2 -2
  205. data/stdlib/securerandom/0/manifest.yaml +2 -0
  206. data/stdlib/securerandom/0/securerandom.rbs +6 -19
  207. data/stdlib/singleton/0/singleton.rbs +3 -0
  208. data/stdlib/socket/0/socket.rbs +13 -1
  209. data/stdlib/socket/0/tcp_socket.rbs +10 -2
  210. data/stdlib/stringio/0/stringio.rbs +1176 -85
  211. data/stdlib/strscan/0/string_scanner.rbs +31 -31
  212. data/stdlib/tempfile/0/tempfile.rbs +3 -3
  213. data/stdlib/time/0/time.rbs +1 -1
  214. data/stdlib/timeout/0/timeout.rbs +63 -7
  215. data/stdlib/tsort/0/cyclic.rbs +3 -0
  216. data/stdlib/uri/0/common.rbs +16 -2
  217. data/stdlib/uri/0/file.rbs +1 -1
  218. data/stdlib/uri/0/generic.rbs +24 -16
  219. data/stdlib/uri/0/rfc2396_parser.rbs +6 -7
  220. data/stdlib/zlib/0/gzip_reader.rbs +2 -2
  221. data/stdlib/zlib/0/gzip_writer.rbs +1 -1
  222. data/stdlib/zlib/0/zstream.rbs +1 -0
  223. metadata +30 -4
data/src/parser.c CHANGED
@@ -7,6 +7,7 @@
7
7
  #include <string.h>
8
8
 
9
9
  #include "rbs/defines.h"
10
+ #include "rbs/lexer.h"
10
11
  #include "rbs/string.h"
11
12
  #include "rbs/util/rbs_unescape.h"
12
13
  #include "rbs/util/rbs_buffer.h"
@@ -19,12 +20,12 @@
19
20
  strlen(str) \
20
21
  )
21
22
 
22
- #define INTERN_TOKEN(parser, tok) \
23
- rbs_constant_pool_insert_shared_with_encoding( \
24
- &parser->constant_pool, \
25
- (const uint8_t *) rbs_peek_token(parser->rbs_lexer_t, tok), \
26
- rbs_token_bytes(tok), \
27
- (void *) parser->rbs_lexer_t->encoding \
23
+ #define INTERN_TOKEN(parser, tok) \
24
+ rbs_constant_pool_insert_shared_with_encoding( \
25
+ &parser->constant_pool, \
26
+ (const uint8_t *) rbs_peek_token(parser->lexer, tok), \
27
+ rbs_token_bytes(tok), \
28
+ parser->lexer->encoding \
28
29
  )
29
30
 
30
31
  #define KEYWORD_CASES \
@@ -120,8 +121,8 @@ static rbs_location_t *rbs_location_current_token(rbs_parser_t *parser) {
120
121
  return rbs_location_new(ALLOCATOR(), parser->current_token.range);
121
122
  }
122
123
 
123
- static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional);
124
- static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type);
124
+ static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional, bool void_allowed, bool self_allowed);
125
+ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed, bool self_allowed);
125
126
 
126
127
  /**
127
128
  * @returns A borrowed copy of the current token, which does *not* need to be freed.
@@ -129,7 +130,7 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type);
129
130
  static rbs_string_t rbs_parser_peek_current_token(rbs_parser_t *parser) {
130
131
  rbs_range_t rg = parser->current_token.range;
131
132
 
132
- const char *start = parser->rbs_lexer_t->string.start + rg.start.byte_pos;
133
+ const char *start = parser->lexer->string.start + rg.start.byte_pos;
133
134
  size_t length = rg.end.byte_pos - rg.start.byte_pos;
134
135
 
135
136
  return rbs_string_new(start, start + length);
@@ -190,7 +191,7 @@ static bool parse_type_name(rbs_parser_t *parser, TypeNameKind kind, rbs_range_t
190
191
  .end = parser->current_token.range.end
191
192
  };
192
193
  rbs_location_t *loc = rbs_location_new(ALLOCATOR(), namespace_range);
193
- rbs_namespace_t *namespace = rbs_namespace_new(ALLOCATOR(), loc, path, absolute);
194
+ rbs_namespace_t *ns = rbs_namespace_new(ALLOCATOR(), loc, path, absolute);
194
195
 
195
196
  switch (parser->current_token.type) {
196
197
  case tLIDENT:
@@ -216,7 +217,7 @@ success: {
216
217
  rbs_location_t *symbolLoc = rbs_location_current_token(parser);
217
218
  rbs_constant_id_t name = INTERN_TOKEN(parser, parser->current_token);
218
219
  rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, name);
219
- *type_name = rbs_type_name_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), *rg), namespace, symbol);
220
+ *type_name = rbs_type_name_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), *rg), ns, symbol);
220
221
  return true;
221
222
  }
222
223
 
@@ -232,7 +233,7 @@ error_handling: {
232
233
  ids = "class/module/constant name";
233
234
  }
234
235
 
235
- rbs_assert(ids != NULL, "Unknown kind of type: %i", kind);
236
+ RBS_ASSERT(ids != NULL, "Unknown kind of type: %i", kind);
236
237
 
237
238
  rbs_parser_set_error(parser, parser->current_token, true, "expected one of %s", ids);
238
239
  return false;
@@ -244,10 +245,10 @@ error_handling: {
244
245
  | {} type `,` ... `,` <type> eol
245
246
  */
246
247
  NODISCARD
247
- static bool parse_type_list(rbs_parser_t *parser, enum RBSTokenType eol, rbs_node_list_t *types) {
248
+ static bool parse_type_list(rbs_parser_t *parser, enum RBSTokenType eol, rbs_node_list_t *types, bool void_allowed, bool self_allowed) {
248
249
  while (true) {
249
250
  rbs_node_t *type;
250
- CHECK_PARSE(rbs_parse_type(parser, &type));
251
+ CHECK_PARSE(rbs_parse_type(parser, &type, void_allowed, self_allowed));
251
252
  rbs_node_list_append(types, type);
252
253
 
253
254
  if (parser->next_token.type == pCOMMA) {
@@ -269,6 +270,43 @@ static bool parse_type_list(rbs_parser_t *parser, enum RBSTokenType eol, rbs_nod
269
270
  return true;
270
271
  }
271
272
 
273
+ /*
274
+ type_list_with_commas ::= {} type `,` ... <`,`> eol
275
+ | {} type `,` ... `,` <type> eol
276
+ */
277
+ NODISCARD
278
+ static bool parse_type_list_with_commas(rbs_parser_t *parser, enum RBSTokenType eol, rbs_node_list_t *types, rbs_location_list_t *comma_locations, bool void_allowed, bool self_allowed) {
279
+ while (true) {
280
+ rbs_node_t *type;
281
+ CHECK_PARSE(rbs_parse_type(parser, &type, void_allowed, self_allowed));
282
+ rbs_node_list_append(types, type);
283
+
284
+ if (parser->next_token.type == pCOMMA) {
285
+ rbs_location_t *comma_loc = rbs_location_new(ALLOCATOR(), parser->next_token.range);
286
+ rbs_location_list_append(comma_locations, comma_loc);
287
+ rbs_parser_advance(parser);
288
+
289
+ if (parser->next_token.type == eol) {
290
+ // Handle trailing comma - for type applications, this is an error
291
+ if (eol == pRBRACKET) {
292
+ rbs_parser_set_error(parser, parser->next_token, true, "unexpected trailing comma");
293
+ return false;
294
+ }
295
+ break;
296
+ }
297
+ } else {
298
+ if (parser->next_token.type == eol) {
299
+ break;
300
+ } else {
301
+ rbs_parser_set_error(parser, parser->next_token, true, "comma delimited type list is expected");
302
+ return false;
303
+ }
304
+ }
305
+ }
306
+
307
+ return true;
308
+ }
309
+
272
310
  static bool is_keyword_token(enum RBSTokenType type) {
273
311
  switch (type) {
274
312
  case tLIDENT:
@@ -289,11 +327,11 @@ static bool is_keyword_token(enum RBSTokenType type) {
289
327
  | {} type <param>
290
328
  */
291
329
  NODISCARD
292
- static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_t **function_param) {
330
+ static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_t **function_param, bool self_allowed) {
293
331
  rbs_range_t type_range;
294
332
  type_range.start = parser->next_token.range.start;
295
333
  rbs_node_t *type;
296
- CHECK_PARSE(rbs_parse_type(parser, &type));
334
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, self_allowed));
297
335
  type_range.end = parser->current_token.range.end;
298
336
 
299
337
  if (parser->next_token.type == pCOMMA || parser->next_token.type == pRPAREN) {
@@ -320,7 +358,7 @@ static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_
320
358
  return false;
321
359
  }
322
360
 
323
- rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), rbs_parser_peek_current_token(parser));
361
+ rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), rbs_parser_peek_current_token(parser), parser->lexer->encoding);
324
362
  rbs_location_t *symbolLoc = rbs_location_current_token(parser);
325
363
  rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_str);
326
364
  rbs_ast_symbol_t *name = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, constant_id);
@@ -337,9 +375,9 @@ static bool parse_function_param(rbs_parser_t *parser, rbs_types_function_param_
337
375
  static rbs_constant_id_t intern_token_start_end(rbs_parser_t *parser, rbs_token_t start_token, rbs_token_t end_token) {
338
376
  return rbs_constant_pool_insert_shared_with_encoding(
339
377
  &parser->constant_pool,
340
- (const uint8_t *) rbs_peek_token(parser->rbs_lexer_t, start_token),
378
+ (const uint8_t *) rbs_peek_token(parser->lexer, start_token),
341
379
  end_token.range.end.byte_pos - start_token.range.start.byte_pos,
342
- parser->rbs_lexer_t->encoding
380
+ parser->lexer->encoding
343
381
  );
344
382
  }
345
383
 
@@ -372,7 +410,7 @@ static bool parse_keyword_key(rbs_parser_t *parser, rbs_ast_symbol_t **key) {
372
410
  keyword ::= {} keyword `:` <function_param>
373
411
  */
374
412
  NODISCARD
375
- static bool parse_keyword(rbs_parser_t *parser, rbs_hash_t *keywords, rbs_hash_t *memo) {
413
+ static bool parse_keyword(rbs_parser_t *parser, rbs_hash_t *keywords, rbs_hash_t *memo, bool self_allowed) {
376
414
  rbs_ast_symbol_t *key = NULL;
377
415
  CHECK_PARSE(parse_keyword_key(parser, &key));
378
416
 
@@ -386,7 +424,7 @@ static bool parse_keyword(rbs_parser_t *parser, rbs_hash_t *keywords, rbs_hash_t
386
424
 
387
425
  ADVANCE_ASSERT(parser, pCOLON);
388
426
  rbs_types_function_param_t *param = NULL;
389
- CHECK_PARSE(parse_function_param(parser, &param));
427
+ CHECK_PARSE(parse_function_param(parser, &param, self_allowed));
390
428
 
391
429
  rbs_hash_set(keywords, (rbs_node_t *) key, (rbs_node_t *) param);
392
430
 
@@ -457,7 +495,7 @@ static bool parser_advance_if(rbs_parser_t *parser, enum RBSTokenType type) {
457
495
  | {} `**` <function_param>
458
496
  */
459
497
  NODISCARD
460
- static bool parse_params(rbs_parser_t *parser, method_params *params) {
498
+ static bool parse_params(rbs_parser_t *parser, method_params *params, bool self_allowed) {
461
499
  if (parser->next_token.type == pQUESTION && parser->next_token2.type == pRPAREN) {
462
500
  params->required_positionals = NULL;
463
501
  rbs_parser_advance(parser);
@@ -486,7 +524,7 @@ static bool parse_params(rbs_parser_t *parser, method_params *params) {
486
524
  }
487
525
 
488
526
  rbs_types_function_param_t *param = NULL;
489
- CHECK_PARSE(parse_function_param(parser, &param));
527
+ CHECK_PARSE(parse_function_param(parser, &param, self_allowed));
490
528
  rbs_node_list_append(params->required_positionals, (rbs_node_t *) param);
491
529
 
492
530
  break;
@@ -504,13 +542,13 @@ PARSE_OPTIONAL_PARAMS:
504
542
  rbs_parser_advance(parser);
505
543
 
506
544
  if (is_keyword(parser)) {
507
- CHECK_PARSE(parse_keyword(parser, params->optional_keywords, memo));
545
+ CHECK_PARSE(parse_keyword(parser, params->optional_keywords, memo, self_allowed));
508
546
  parser_advance_if(parser, pCOMMA);
509
547
  goto PARSE_KEYWORDS;
510
548
  }
511
549
 
512
550
  rbs_types_function_param_t *param = NULL;
513
- CHECK_PARSE(parse_function_param(parser, &param));
551
+ CHECK_PARSE(parse_function_param(parser, &param, self_allowed));
514
552
  rbs_node_list_append(params->optional_positionals, (rbs_node_t *) param);
515
553
 
516
554
  break;
@@ -527,7 +565,7 @@ PARSE_REST_PARAM:
527
565
  if (parser->next_token.type == pSTAR) {
528
566
  rbs_parser_advance(parser);
529
567
  rbs_types_function_param_t *param = NULL;
530
- CHECK_PARSE(parse_function_param(parser, &param));
568
+ CHECK_PARSE(parse_function_param(parser, &param, self_allowed));
531
569
  params->rest_positionals = (rbs_node_t *) param;
532
570
 
533
571
  if (!parser_advance_if(parser, pCOMMA)) {
@@ -554,7 +592,7 @@ PARSE_TRAILING_PARAMS:
554
592
  }
555
593
 
556
594
  rbs_types_function_param_t *param = NULL;
557
- CHECK_PARSE(parse_function_param(parser, &param));
595
+ CHECK_PARSE(parse_function_param(parser, &param, self_allowed));
558
596
  rbs_node_list_append(params->trailing_positionals, (rbs_node_t *) param);
559
597
 
560
598
  break;
@@ -571,7 +609,7 @@ PARSE_KEYWORDS:
571
609
  case pQUESTION:
572
610
  rbs_parser_advance(parser);
573
611
  if (is_keyword(parser)) {
574
- CHECK_PARSE(parse_keyword(parser, params->optional_keywords, memo));
612
+ CHECK_PARSE(parse_keyword(parser, params->optional_keywords, memo, self_allowed));
575
613
  } else {
576
614
  rbs_parser_set_error(parser, parser->next_token, true, "optional keyword argument type is expected");
577
615
  return false;
@@ -581,7 +619,7 @@ PARSE_KEYWORDS:
581
619
  case pSTAR2:
582
620
  rbs_parser_advance(parser);
583
621
  rbs_types_function_param_t *param = NULL;
584
- CHECK_PARSE(parse_function_param(parser, &param));
622
+ CHECK_PARSE(parse_function_param(parser, &param, self_allowed));
585
623
  params->rest_keywords = (rbs_node_t *) param;
586
624
  break;
587
625
 
@@ -593,7 +631,7 @@ PARSE_KEYWORDS:
593
631
  case tBANGIDENT:
594
632
  KEYWORD_CASES
595
633
  if (is_keyword(parser)) {
596
- CHECK_PARSE(parse_keyword(parser, params->required_keywords, memo));
634
+ CHECK_PARSE(parse_keyword(parser, params->required_keywords, memo, self_allowed));
597
635
  } else {
598
636
  rbs_parser_set_error(parser, parser->next_token, true, "required keyword argument type is expected");
599
637
  return false;
@@ -623,14 +661,19 @@ EOP:
623
661
  | {} simple_type <`?`>
624
662
  */
625
663
  NODISCARD
626
- static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional) {
664
+ static bool parse_optional(rbs_parser_t *parser, rbs_node_t **optional, bool void_allowed, bool self_allowed) {
627
665
  rbs_range_t rg;
628
666
  rg.start = parser->next_token.range.start;
629
667
 
630
668
  rbs_node_t *type = NULL;
631
- CHECK_PARSE(parse_simple(parser, &type));
669
+ CHECK_PARSE(parse_simple(parser, &type, void_allowed, self_allowed));
632
670
 
633
671
  if (parser->next_token.type == pQUESTION) {
672
+ if (void_allowed && type->type == RBS_TYPES_BASES_VOID) {
673
+ rbs_parser_set_error(parser, parser->current_token, true, "void type is not allowed here");
674
+ return false;
675
+ }
676
+
634
677
  rbs_parser_advance(parser);
635
678
  rg.end = parser->current_token.range.end;
636
679
  rbs_location_t *location = rbs_location_new(ALLOCATOR(), rg);
@@ -659,13 +702,13 @@ static void initialize_method_params(method_params *params, rbs_allocator_t *all
659
702
  | {} `[` `self` `:` type <`]`>
660
703
  */
661
704
  NODISCARD
662
- static bool parse_self_type_binding(rbs_parser_t *parser, rbs_node_t **self_type) {
705
+ static bool parse_self_type_binding(rbs_parser_t *parser, rbs_node_t **self_type, bool self_allowed) {
663
706
  if (parser->next_token.type == pLBRACKET) {
664
707
  rbs_parser_advance(parser);
665
708
  ADVANCE_ASSERT(parser, kSELF);
666
709
  ADVANCE_ASSERT(parser, pCOLON);
667
710
  rbs_node_t *type;
668
- CHECK_PARSE(rbs_parse_type(parser, &type));
711
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, self_allowed));
669
712
  ADVANCE_ASSERT(parser, pRBRACKET);
670
713
  *self_type = type;
671
714
  }
@@ -687,7 +730,7 @@ typedef struct {
687
730
  | {} self_type_binding? `->` <optional>
688
731
  */
689
732
  NODISCARD
690
- static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse_function_result **result) {
733
+ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse_function_result **result, bool self_allowed) {
691
734
  rbs_node_t *function = NULL;
692
735
  rbs_types_block_t *block = NULL;
693
736
  rbs_node_t *function_self_type = NULL;
@@ -699,13 +742,13 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse
699
742
 
700
743
  if (parser->next_token.type == pLPAREN) {
701
744
  rbs_parser_advance(parser);
702
- CHECK_PARSE(parse_params(parser, &params));
745
+ CHECK_PARSE(parse_params(parser, &params, self_allowed));
703
746
  ADVANCE_ASSERT(parser, pRPAREN);
704
747
  }
705
748
 
706
749
  // Passing NULL to function_self_type means the function itself doesn't accept self type binding. (== method type)
707
750
  if (accept_type_binding) {
708
- CHECK_PARSE(parse_self_type_binding(parser, &function_self_type));
751
+ CHECK_PARSE(parse_self_type_binding(parser, &function_self_type, self_allowed));
709
752
  } else {
710
753
  if (rbs_is_untyped_params(&params)) {
711
754
  if (parser->next_token.type != pARROW) {
@@ -735,16 +778,16 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse
735
778
 
736
779
  if (parser->next_token.type == pLPAREN) {
737
780
  rbs_parser_advance(parser);
738
- CHECK_PARSE(parse_params(parser, &block_params));
781
+ CHECK_PARSE(parse_params(parser, &block_params, self_allowed));
739
782
  ADVANCE_ASSERT(parser, pRPAREN);
740
783
  }
741
784
 
742
785
  rbs_node_t *self_type = NULL;
743
- CHECK_PARSE(parse_self_type_binding(parser, &self_type));
786
+ CHECK_PARSE(parse_self_type_binding(parser, &self_type, self_allowed));
744
787
 
745
788
  ADVANCE_ASSERT(parser, pARROW);
746
789
  rbs_node_t *block_return_type = NULL;
747
- CHECK_PARSE(parse_optional(parser, &block_return_type));
790
+ CHECK_PARSE(parse_optional(parser, &block_return_type, true, self_allowed));
748
791
 
749
792
  ADVANCE_ASSERT(parser, pRBRACE);
750
793
 
@@ -774,7 +817,7 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse
774
817
 
775
818
  ADVANCE_ASSERT(parser, pARROW);
776
819
  rbs_node_t *type = NULL;
777
- CHECK_PARSE(parse_optional(parser, &type));
820
+ CHECK_PARSE(parse_optional(parser, &type, true, self_allowed));
778
821
 
779
822
  function_range.end = parser->current_token.range.end;
780
823
  rbs_location_t *loc = rbs_location_new(ALLOCATOR(), function_range);
@@ -805,10 +848,10 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse
805
848
  proc_type ::= {`^`} <function>
806
849
  */
807
850
  NODISCARD
808
- static bool parse_proc_type(rbs_parser_t *parser, rbs_types_proc_t **proc) {
851
+ static bool parse_proc_type(rbs_parser_t *parser, rbs_types_proc_t **proc, bool self_allowed) {
809
852
  rbs_position_t start = parser->current_token.range.start;
810
853
  parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
811
- CHECK_PARSE(parse_function(parser, true, &result));
854
+ CHECK_PARSE(parse_function(parser, true, &result, self_allowed));
812
855
 
813
856
  rbs_position_t end = parser->current_token.range.end;
814
857
  rbs_location_t *loc = rbs_location_new(ALLOCATOR(), (rbs_range_t) { .start = start, .end = end });
@@ -833,7 +876,7 @@ static void check_key_duplication(rbs_parser_t *parser, rbs_hash_t *fields, rbs_
833
876
  | {} literal_type `=>` <type>
834
877
  */
835
878
  NODISCARD
836
- static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields) {
879
+ static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields, bool self_allowed) {
837
880
  *fields = rbs_hash_new(ALLOCATOR());
838
881
 
839
882
  if (parser->next_token.type == pRBRACE) return true;
@@ -866,7 +909,7 @@ static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields) {
866
909
  case kTRUE:
867
910
  case kFALSE: {
868
911
  rbs_node_t *type = NULL;
869
- CHECK_PARSE(parse_simple(parser, &type));
912
+ CHECK_PARSE(parse_simple(parser, &type, false, self_allowed));
870
913
 
871
914
  key = (rbs_ast_symbol_t *) ((rbs_types_literal_t *) type)->literal;
872
915
  break;
@@ -883,7 +926,7 @@ static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields) {
883
926
  field_range.start = parser->current_token.range.end;
884
927
 
885
928
  rbs_node_t *type;
886
- CHECK_PARSE(rbs_parse_type(parser, &type));
929
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, self_allowed));
887
930
 
888
931
  field_range.end = parser->current_token.range.end;
889
932
  rbs_location_t *loc = rbs_location_new(ALLOCATOR(), field_range);
@@ -905,7 +948,7 @@ static bool parse_record_attributes(rbs_parser_t *parser, rbs_hash_t **fields) {
905
948
  */
906
949
  NODISCARD
907
950
  static bool parse_symbol(rbs_parser_t *parser, rbs_location_t *location, rbs_types_literal_t **symbol) {
908
- size_t offset_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) ":", (size_t) 1);
951
+ size_t offset_bytes = parser->lexer->encoding->char_width((const uint8_t *) ":", (size_t) 1);
909
952
  size_t bytes = rbs_token_bytes(parser->current_token) - offset_bytes;
910
953
 
911
954
  rbs_ast_symbol_t *literal;
@@ -914,7 +957,7 @@ static bool parse_symbol(rbs_parser_t *parser, rbs_location_t *location, rbs_typ
914
957
  case tSYMBOL: {
915
958
  rbs_location_t *symbolLoc = rbs_location_current_token(parser);
916
959
 
917
- char *buffer = rbs_peek_token(parser->rbs_lexer_t, parser->current_token);
960
+ char *buffer = rbs_peek_token(parser->lexer, parser->current_token);
918
961
  rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared(
919
962
  &parser->constant_pool,
920
963
  (const uint8_t *) buffer + offset_bytes,
@@ -930,7 +973,7 @@ static bool parse_symbol(rbs_parser_t *parser, rbs_location_t *location, rbs_typ
930
973
 
931
974
  rbs_string_t symbol = rbs_string_new(current_token.start + offset_bytes, current_token.end);
932
975
 
933
- rbs_string_t unquoted_symbol = rbs_unquote_string(ALLOCATOR(), symbol);
976
+ rbs_string_t unquoted_symbol = rbs_unquote_string(ALLOCATOR(), symbol, parser->lexer->encoding);
934
977
 
935
978
  rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_symbol);
936
979
 
@@ -954,9 +997,9 @@ static bool parse_symbol(rbs_parser_t *parser, rbs_location_t *location, rbs_typ
954
997
  */
955
998
  NODISCARD
956
999
  static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node_t **type) {
957
- TypeNameKind expected_kind = INTERFACE_NAME | CLASS_NAME;
1000
+ TypeNameKind expected_kind = (TypeNameKind) (INTERFACE_NAME | CLASS_NAME);
958
1001
  if (parse_alias) {
959
- expected_kind |= ALIAS_NAME;
1002
+ expected_kind = (TypeNameKind) (expected_kind | ALIAS_NAME);
960
1003
  }
961
1004
 
962
1005
  rbs_range_t name_range;
@@ -990,7 +1033,7 @@ static bool parse_instance_type(rbs_parser_t *parser, bool parse_alias, rbs_node
990
1033
  if (parser->next_token.type == pLBRACKET) {
991
1034
  rbs_parser_advance(parser);
992
1035
  args_range.start = parser->current_token.range.start;
993
- CHECK_PARSE(parse_type_list(parser, pRBRACKET, types));
1036
+ CHECK_PARSE(parse_type_list(parser, pRBRACKET, types, true, true));
994
1037
  ADVANCE_ASSERT(parser, pRBRACKET);
995
1038
  args_range.end = parser->current_token.range.end;
996
1039
  } else {
@@ -1077,13 +1120,13 @@ static bool parser_typevar_member(rbs_parser_t *parser, rbs_constant_id_t id) {
1077
1120
  | {} `^` <function>
1078
1121
  */
1079
1122
  NODISCARD
1080
- static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) {
1123
+ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed, bool self_allowed) {
1081
1124
  rbs_parser_advance(parser);
1082
1125
 
1083
1126
  switch (parser->current_token.type) {
1084
1127
  case pLPAREN: {
1085
1128
  rbs_node_t *lparen_type;
1086
- CHECK_PARSE(rbs_parse_type(parser, &lparen_type));
1129
+ CHECK_PARSE(rbs_parse_type(parser, &lparen_type, false, self_allowed));
1087
1130
  ADVANCE_ASSERT(parser, pRPAREN);
1088
1131
  *type = lparen_type;
1089
1132
  return true;
@@ -1114,6 +1157,11 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) {
1114
1157
  return true;
1115
1158
  }
1116
1159
  case kSELF: {
1160
+ if (!self_allowed) {
1161
+ rbs_parser_set_error(parser, parser->current_token, true, "self type is not allowed here");
1162
+ return false;
1163
+ }
1164
+
1117
1165
  rbs_location_t *loc = rbs_location_current_token(parser);
1118
1166
  *type = (rbs_node_t *) rbs_types_bases_self_new(ALLOCATOR(), loc);
1119
1167
  return true;
@@ -1124,6 +1172,11 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) {
1124
1172
  return true;
1125
1173
  }
1126
1174
  case kVOID: {
1175
+ if (!void_allowed) {
1176
+ rbs_parser_set_error(parser, parser->current_token, true, "void type is not allowed here");
1177
+ return false;
1178
+ }
1179
+
1127
1180
  rbs_location_t *loc = rbs_location_current_token(parser);
1128
1181
  *type = (rbs_node_t *) rbs_types_bases_void_new(ALLOCATOR(), loc);
1129
1182
  return true;
@@ -1162,7 +1215,7 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) {
1162
1215
  case tDQSTRING: {
1163
1216
  rbs_location_t *loc = rbs_location_current_token(parser);
1164
1217
 
1165
- rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), rbs_parser_peek_current_token(parser));
1218
+ rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), rbs_parser_peek_current_token(parser), parser->lexer->encoding);
1166
1219
  rbs_node_t *literal = (rbs_node_t *) rbs_ast_string_new(ALLOCATOR(), loc, unquoted_str);
1167
1220
  *type = (rbs_node_t *) rbs_types_literal_new(ALLOCATOR(), loc, literal);
1168
1221
  return true;
@@ -1177,7 +1230,7 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) {
1177
1230
  return true;
1178
1231
  }
1179
1232
  case tUIDENT: {
1180
- const char *name_str = rbs_peek_token(parser->rbs_lexer_t, parser->current_token);
1233
+ const char *name_str = rbs_peek_token(parser->lexer, parser->current_token);
1181
1234
  size_t name_len = rbs_token_bytes(parser->current_token);
1182
1235
 
1183
1236
  rbs_constant_id_t name = rbs_constant_pool_find(&parser->constant_pool, (const uint8_t *) name_str, name_len);
@@ -1212,7 +1265,7 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) {
1212
1265
  rg.start = parser->current_token.range.start;
1213
1266
  rbs_node_list_t *types = rbs_node_list_new(ALLOCATOR());
1214
1267
  if (parser->next_token.type != pRBRACKET) {
1215
- CHECK_PARSE(parse_type_list(parser, pRBRACKET, types));
1268
+ CHECK_PARSE(parse_type_list(parser, pRBRACKET, types, false, self_allowed));
1216
1269
  }
1217
1270
  ADVANCE_ASSERT(parser, pRBRACKET);
1218
1271
  rg.end = parser->current_token.range.end;
@@ -1230,7 +1283,7 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) {
1230
1283
  case pLBRACE: {
1231
1284
  rbs_position_t start = parser->current_token.range.start;
1232
1285
  rbs_hash_t *fields = NULL;
1233
- CHECK_PARSE(parse_record_attributes(parser, &fields));
1286
+ CHECK_PARSE(parse_record_attributes(parser, &fields, self_allowed));
1234
1287
  ADVANCE_ASSERT(parser, pRBRACE);
1235
1288
  rbs_position_t end = parser->current_token.range.end;
1236
1289
  rbs_location_t *loc = rbs_location_new(ALLOCATOR(), (rbs_range_t) { .start = start, .end = end });
@@ -1239,7 +1292,7 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) {
1239
1292
  }
1240
1293
  case pHAT: {
1241
1294
  rbs_types_proc_t *value = NULL;
1242
- CHECK_PARSE(parse_proc_type(parser, &value));
1295
+ CHECK_PARSE(parse_proc_type(parser, &value, self_allowed));
1243
1296
  *type = (rbs_node_t *) value;
1244
1297
  return true;
1245
1298
  }
@@ -1254,21 +1307,26 @@ static bool parse_simple(rbs_parser_t *parser, rbs_node_t **type) {
1254
1307
  | {} <optional>
1255
1308
  */
1256
1309
  NODISCARD
1257
- static bool parse_intersection(rbs_parser_t *parser, rbs_node_t **type) {
1310
+ static bool parse_intersection(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed, bool self_allowed) {
1258
1311
  rbs_range_t rg;
1259
1312
  rg.start = parser->next_token.range.start;
1260
1313
 
1261
1314
  rbs_node_t *optional = NULL;
1262
- CHECK_PARSE(parse_optional(parser, &optional));
1315
+ CHECK_PARSE(parse_optional(parser, &optional, void_allowed, self_allowed));
1263
1316
  *type = optional;
1264
1317
 
1265
1318
  rbs_node_list_t *intersection_types = rbs_node_list_new(ALLOCATOR());
1266
1319
 
1267
1320
  rbs_node_list_append(intersection_types, optional);
1268
1321
  while (parser->next_token.type == pAMP) {
1322
+ if (void_allowed && (*type)->type == RBS_TYPES_BASES_VOID) {
1323
+ rbs_parser_set_error(parser, parser->current_token, true, "void type is not allowed here");
1324
+ return false;
1325
+ }
1326
+
1269
1327
  rbs_parser_advance(parser);
1270
1328
  rbs_node_t *type = NULL;
1271
- CHECK_PARSE(parse_optional(parser, &type));
1329
+ CHECK_PARSE(parse_optional(parser, &type, false, self_allowed));
1272
1330
  rbs_node_list_append(intersection_types, type);
1273
1331
  }
1274
1332
 
@@ -1286,19 +1344,24 @@ static bool parse_intersection(rbs_parser_t *parser, rbs_node_t **type) {
1286
1344
  union ::= {} intersection '|' ... '|' <intersection>
1287
1345
  | {} <intersection>
1288
1346
  */
1289
- bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type) {
1347
+ bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type, bool void_allowed, bool self_allowed) {
1290
1348
  rbs_range_t rg;
1291
1349
  rg.start = parser->next_token.range.start;
1292
1350
  rbs_node_list_t *union_types = rbs_node_list_new(ALLOCATOR());
1293
1351
 
1294
- CHECK_PARSE(parse_intersection(parser, type));
1352
+ CHECK_PARSE(parse_intersection(parser, type, void_allowed, self_allowed));
1295
1353
 
1296
1354
  rbs_node_list_append(union_types, *type);
1297
1355
 
1298
1356
  while (parser->next_token.type == pBAR) {
1357
+ if (void_allowed && (*type)->type == RBS_TYPES_BASES_VOID) {
1358
+ rbs_parser_set_error(parser, parser->current_token, true, "void type is not allowed here");
1359
+ return false;
1360
+ }
1361
+
1299
1362
  rbs_parser_advance(parser);
1300
1363
  rbs_node_t *intersection = NULL;
1301
- CHECK_PARSE(parse_intersection(parser, &intersection));
1364
+ CHECK_PARSE(parse_intersection(parser, &intersection, false, self_allowed));
1302
1365
  rbs_node_list_append(union_types, intersection);
1303
1366
  }
1304
1367
 
@@ -1316,9 +1379,9 @@ bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type) {
1316
1379
  type_params ::= {} `[` type_param `,` ... <`]`>
1317
1380
  | {<>}
1318
1381
 
1319
- type_param ::= kUNCHECKED? (kIN|kOUT|) tUIDENT upper_bound? default_type? (module_type_params == true)
1382
+ type_param ::= kUNCHECKED? (kIN|kOUT|) tUIDENT upper_bound? lower_bound? default_type? (module_type_params == true)
1320
1383
 
1321
- type_param ::= tUIDENT upper_bound? default_type? (module_type_params == false)
1384
+ type_param ::= tUIDENT upper_bound? lower_bound? default_type? (module_type_params == false)
1322
1385
  */
1323
1386
  NODISCARD
1324
1387
  static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module_type_params, rbs_node_list_t **params) {
@@ -1335,6 +1398,7 @@ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module
1335
1398
  bool unchecked = false;
1336
1399
  rbs_keyword_t *variance = rbs_keyword_new(ALLOCATOR(), rbs_location_current_token(parser), INTERN("invariant"));
1337
1400
  rbs_node_t *upper_bound = NULL;
1401
+ rbs_node_t *lower_bound = NULL;
1338
1402
  rbs_node_t *default_type = NULL;
1339
1403
 
1340
1404
  rbs_range_t param_range;
@@ -1378,11 +1442,37 @@ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module
1378
1442
  CHECK_PARSE(rbs_parser_insert_typevar(parser, id));
1379
1443
 
1380
1444
  rbs_range_t upper_bound_range = NULL_RANGE;
1381
- if (parser->next_token.type == pLT) {
1382
- rbs_parser_advance(parser);
1383
- upper_bound_range.start = parser->current_token.range.start;
1384
- CHECK_PARSE(rbs_parse_type(parser, &upper_bound));
1385
- upper_bound_range.end = parser->current_token.range.end;
1445
+ rbs_range_t lower_bound_range = NULL_RANGE;
1446
+
1447
+ for (int bound_parse_attempt = 0; bound_parse_attempt < 2; bound_parse_attempt++) {
1448
+ switch (parser->next_token.type) {
1449
+ case pLT:
1450
+ if (upper_bound != NULL) {
1451
+ rbs_parser_set_error(parser, parser->next_token, true, "duplicate upper bound ('<') for type parameter");
1452
+ return false;
1453
+ }
1454
+
1455
+ rbs_parser_advance(parser);
1456
+ upper_bound_range.start = parser->current_token.range.start;
1457
+ CHECK_PARSE(rbs_parse_type(parser, &upper_bound, false, false));
1458
+ upper_bound_range.end = parser->current_token.range.end;
1459
+ break;
1460
+
1461
+ case pGT:
1462
+ if (lower_bound != NULL) {
1463
+ rbs_parser_set_error(parser, parser->next_token, true, "duplicate lower bound ('>') for type parameter");
1464
+ return false;
1465
+ }
1466
+
1467
+ rbs_parser_advance(parser);
1468
+ lower_bound_range.start = parser->current_token.range.start;
1469
+ CHECK_PARSE(rbs_parse_type(parser, &lower_bound, false, false));
1470
+ lower_bound_range.end = parser->current_token.range.end;
1471
+ break;
1472
+
1473
+ default:
1474
+ break;
1475
+ }
1386
1476
  }
1387
1477
 
1388
1478
  rbs_range_t default_type_range = NULL_RANGE;
@@ -1391,7 +1481,7 @@ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module
1391
1481
  rbs_parser_advance(parser);
1392
1482
 
1393
1483
  default_type_range.start = parser->current_token.range.start;
1394
- CHECK_PARSE(rbs_parse_type(parser, &default_type));
1484
+ CHECK_PARSE(rbs_parse_type(parser, &default_type, true, false));
1395
1485
  default_type_range.end = parser->current_token.range.end;
1396
1486
 
1397
1487
  required_param_allowed = false;
@@ -1406,23 +1496,25 @@ static bool parse_type_params(rbs_parser_t *parser, rbs_range_t *rg, bool module
1406
1496
  param_range.end = parser->current_token.range.end;
1407
1497
 
1408
1498
  rbs_location_t *loc = rbs_location_new(ALLOCATOR(), param_range);
1409
- rbs_loc_alloc_children(ALLOCATOR(), loc, 5);
1499
+ rbs_loc_alloc_children(ALLOCATOR(), loc, 6);
1410
1500
  rbs_loc_add_required_child(loc, INTERN("name"), name_range);
1411
1501
  rbs_loc_add_optional_child(loc, INTERN("variance"), variance_range);
1412
1502
  rbs_loc_add_optional_child(loc, INTERN("unchecked"), unchecked_range);
1413
1503
  rbs_loc_add_optional_child(loc, INTERN("upper_bound"), upper_bound_range);
1504
+ rbs_loc_add_optional_child(loc, INTERN("lower_bound"), lower_bound_range);
1414
1505
  rbs_loc_add_optional_child(loc, INTERN("default"), default_type_range);
1415
1506
 
1416
- rbs_ast_type_param_t *param = rbs_ast_type_param_new(ALLOCATOR(), loc, name, variance, upper_bound, default_type, unchecked);
1507
+ rbs_ast_type_param_t *param = rbs_ast_type_param_new(ALLOCATOR(), loc, name, variance, upper_bound, lower_bound, default_type, unchecked);
1417
1508
 
1418
1509
  rbs_node_list_append(*params, (rbs_node_t *) param);
1419
1510
 
1420
1511
  if (parser->next_token.type == pCOMMA) {
1421
1512
  rbs_parser_advance(parser);
1422
- }
1423
-
1424
- if (parser->next_token.type == pRBRACKET) {
1513
+ } else if (parser->next_token.type == pRBRACKET) {
1425
1514
  break;
1515
+ } else {
1516
+ rbs_parser_set_error(parser, parser->next_token, true, "expected ',' or ']' after type parameter, got %s", rbs_token_type_str(parser->next_token.type));
1517
+ return false;
1426
1518
  }
1427
1519
  }
1428
1520
 
@@ -1459,7 +1551,7 @@ static bool parser_pop_typevar_table(rbs_parser_t *parser) {
1459
1551
  method_type ::= {} type_params <function>
1460
1552
  */
1461
1553
  // TODO: Should this be NODISCARD?
1462
- bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type) {
1554
+ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type, bool require_eof) {
1463
1555
  rbs_parser_push_typevar_table(parser, false);
1464
1556
 
1465
1557
  rbs_range_t rg;
@@ -1473,12 +1565,20 @@ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type
1473
1565
  type_range.start = parser->next_token.range.start;
1474
1566
 
1475
1567
  parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
1476
- CHECK_PARSE(parse_function(parser, false, &result));
1568
+ CHECK_PARSE(parse_function(parser, false, &result, true));
1569
+
1570
+ CHECK_PARSE(parser_pop_typevar_table(parser));
1477
1571
 
1478
1572
  rg.end = parser->current_token.range.end;
1479
1573
  type_range.end = rg.end;
1480
1574
 
1481
- CHECK_PARSE(parser_pop_typevar_table(parser));
1575
+ if (require_eof) {
1576
+ rbs_parser_advance(parser);
1577
+ if (parser->current_token.type != pEOF) {
1578
+ rbs_parser_set_error(parser, parser->current_token, true, "expected a token `%s`", rbs_token_type_str(pEOF));
1579
+ return false;
1580
+ }
1581
+ }
1482
1582
 
1483
1583
  rbs_location_t *loc = rbs_location_new(ALLOCATOR(), rg);
1484
1584
  rbs_loc_alloc_children(ALLOCATOR(), loc, 2);
@@ -1508,7 +1608,7 @@ static bool parse_global_decl(rbs_parser_t *parser, rbs_node_list_t *annotations
1508
1608
  rbs_range_t colon_range = parser->current_token.range;
1509
1609
 
1510
1610
  rbs_node_t *type;
1511
- CHECK_PARSE(rbs_parse_type(parser, &type));
1611
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, false));
1512
1612
  decl_range.end = parser->current_token.range.end;
1513
1613
 
1514
1614
  rbs_location_t *loc = rbs_location_new(ALLOCATOR(), decl_range);
@@ -1538,7 +1638,7 @@ static bool parse_const_decl(rbs_parser_t *parser, rbs_node_list_t *annotations,
1538
1638
  rbs_range_t colon_range = parser->current_token.range;
1539
1639
 
1540
1640
  rbs_node_t *type;
1541
- CHECK_PARSE(rbs_parse_type(parser, &type));
1641
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, false));
1542
1642
 
1543
1643
  decl_range.end = parser->current_token.range.end;
1544
1644
 
@@ -1578,7 +1678,7 @@ static bool parse_type_decl(rbs_parser_t *parser, rbs_position_t comment_pos, rb
1578
1678
  rbs_range_t eq_range = parser->current_token.range;
1579
1679
 
1580
1680
  rbs_node_t *type;
1581
- CHECK_PARSE(rbs_parse_type(parser, &type));
1681
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, false));
1582
1682
 
1583
1683
  decl_range.end = parser->current_token.range.end;
1584
1684
 
@@ -1605,14 +1705,16 @@ static bool parse_annotation(rbs_parser_t *parser, rbs_ast_annotation_t **annota
1605
1705
  rbs_range_t rg = parser->current_token.range;
1606
1706
 
1607
1707
  size_t offset_bytes =
1608
- parser->rbs_lexer_t->encoding->char_width((const uint8_t *) "%", (size_t) 1) +
1609
- parser->rbs_lexer_t->encoding->char_width((const uint8_t *) "a", (size_t) 1);
1708
+ parser->lexer->encoding->char_width((const uint8_t *) "%", (size_t) 1) +
1709
+ parser->lexer->encoding->char_width((const uint8_t *) "a", (size_t) 1);
1610
1710
 
1611
1711
  rbs_string_t str = rbs_string_new(
1612
- parser->rbs_lexer_t->string.start + rg.start.byte_pos + offset_bytes,
1613
- parser->rbs_lexer_t->string.end
1712
+ parser->lexer->string.start + rg.start.byte_pos + offset_bytes,
1713
+ parser->lexer->string.end
1614
1714
  );
1615
- unsigned int open_char = rbs_utf8_string_to_codepoint(str);
1715
+
1716
+ // Assumes the input is ASCII compatible
1717
+ unsigned int open_char = str.start[0];
1616
1718
 
1617
1719
  unsigned int close_char;
1618
1720
 
@@ -1637,8 +1739,8 @@ static bool parse_annotation(rbs_parser_t *parser, rbs_ast_annotation_t **annota
1637
1739
  return false;
1638
1740
  }
1639
1741
 
1640
- size_t open_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) &open_char, (size_t) 1);
1641
- size_t close_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) &close_char, (size_t) 1);
1742
+ size_t open_bytes = parser->lexer->encoding->char_width((const uint8_t *) &open_char, (size_t) 1);
1743
+ size_t close_bytes = parser->lexer->encoding->char_width((const uint8_t *) &close_char, (size_t) 1);
1642
1744
 
1643
1745
  rbs_string_t current_token = rbs_parser_peek_current_token(parser);
1644
1746
  size_t total_offset = offset_bytes + open_bytes;
@@ -1702,9 +1804,9 @@ static bool parse_method_name(rbs_parser_t *parser, rbs_range_t *range, rbs_ast_
1702
1804
 
1703
1805
  rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared_with_encoding(
1704
1806
  &parser->constant_pool,
1705
- (const uint8_t *) parser->rbs_lexer_t->string.start + range->start.byte_pos,
1807
+ (const uint8_t *) parser->lexer->string.start + range->start.byte_pos,
1706
1808
  range->end.byte_pos - range->start.byte_pos,
1707
- parser->rbs_lexer_t->encoding
1809
+ parser->lexer->encoding
1708
1810
  );
1709
1811
 
1710
1812
  rbs_location_t *symbolLoc = rbs_location_new(ALLOCATOR(), *range);
@@ -1725,7 +1827,7 @@ static bool parse_method_name(rbs_parser_t *parser, rbs_range_t *range, rbs_ast_
1725
1827
  }
1726
1828
  case tQIDENT: {
1727
1829
  rbs_string_t string = rbs_parser_peek_current_token(parser);
1728
- rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), string);
1830
+ rbs_string_t unquoted_str = rbs_unquote_string(ALLOCATOR(), string, parser->lexer->encoding);
1729
1831
  rbs_constant_id_t constant_id = rbs_constant_pool_insert_string(&parser->constant_pool, unquoted_str);
1730
1832
  rbs_location_t *symbolLoc = rbs_location_current_token(parser);
1731
1833
  *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, constant_id);
@@ -1738,6 +1840,7 @@ static bool parse_method_name(rbs_parser_t *parser, rbs_range_t *range, rbs_ast_
1738
1840
  case pSTAR:
1739
1841
  case pSTAR2:
1740
1842
  case pLT:
1843
+ case pGT:
1741
1844
  case pAREF_OPR:
1742
1845
  case tOPERATOR: {
1743
1846
  *range = parser->current_token.range;
@@ -1886,7 +1989,7 @@ static bool parse_member_def(rbs_parser_t *parser, bool instance_only, bool acce
1886
1989
  case pLBRACKET:
1887
1990
  case pQUESTION: {
1888
1991
  rbs_method_type_t *method_type = NULL;
1889
- CHECK_PARSE(rbs_parse_method_type(parser, &method_type));
1992
+ CHECK_PARSE(rbs_parse_method_type(parser, &method_type, false));
1890
1993
 
1891
1994
  overload_range.end = parser->current_token.range.end;
1892
1995
  rbs_location_t *loc = rbs_location_new(ALLOCATOR(), overload_range);
@@ -1971,7 +2074,7 @@ static bool class_instance_name(rbs_parser_t *parser, TypeNameKind kind, rbs_nod
1971
2074
  if (parser->next_token.type == pLBRACKET) {
1972
2075
  rbs_parser_advance(parser);
1973
2076
  args_range->start = parser->current_token.range.start;
1974
- CHECK_PARSE(parse_type_list(parser, pRBRACKET, args));
2077
+ CHECK_PARSE(parse_type_list(parser, pRBRACKET, args, true, false));
1975
2078
  ADVANCE_ASSERT(parser, pRBRACKET);
1976
2079
  args_range->end = parser->current_token.range.end;
1977
2080
  } else {
@@ -2028,7 +2131,7 @@ static bool parse_mixin_member(rbs_parser_t *parser, bool from_interface, rbs_po
2028
2131
  rbs_type_name_t *name = NULL;
2029
2132
  CHECK_PARSE(class_instance_name(
2030
2133
  parser,
2031
- from_interface ? INTERFACE_NAME : (INTERFACE_NAME | CLASS_NAME),
2134
+ from_interface ? INTERFACE_NAME : (TypeNameKind) (INTERFACE_NAME | CLASS_NAME),
2032
2135
  args,
2033
2136
  &name_range,
2034
2137
  &args_range,
@@ -2146,7 +2249,7 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p
2146
2249
  rbs_range_t colon_range = parser->current_token.range;
2147
2250
 
2148
2251
  rbs_node_t *type;
2149
- CHECK_PARSE(rbs_parse_type(parser, &type));
2252
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, true));
2150
2253
  member_range.end = parser->current_token.range.end;
2151
2254
 
2152
2255
  rbs_location_t *loc = rbs_location_new(ALLOCATOR(), member_range);
@@ -2169,7 +2272,7 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p
2169
2272
  rbs_parser_push_typevar_table(parser, true);
2170
2273
 
2171
2274
  rbs_node_t *type;
2172
- CHECK_PARSE(rbs_parse_type(parser, &type));
2275
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, false));
2173
2276
 
2174
2277
  CHECK_PARSE(parser_pop_typevar_table(parser));
2175
2278
 
@@ -2208,7 +2311,7 @@ static bool parse_variable_member(rbs_parser_t *parser, rbs_position_t comment_p
2208
2311
  rbs_parser_push_typevar_table(parser, true);
2209
2312
 
2210
2313
  rbs_node_t *type;
2211
- CHECK_PARSE(rbs_parse_type(parser, &type));
2314
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, true));
2212
2315
 
2213
2316
  CHECK_PARSE(parser_pop_typevar_table(parser));
2214
2317
 
@@ -2349,7 +2452,7 @@ static bool parse_attribute_member(rbs_parser_t *parser, rbs_position_t comment_
2349
2452
  rbs_parser_push_typevar_table(parser, is_kind == SINGLETON_KIND);
2350
2453
 
2351
2454
  rbs_node_t *type;
2352
- CHECK_PARSE(rbs_parse_type(parser, &type));
2455
+ CHECK_PARSE(rbs_parse_type(parser, &type, false, true));
2353
2456
 
2354
2457
  CHECK_PARSE(parser_pop_typevar_table(parser));
2355
2458
 
@@ -2494,7 +2597,7 @@ static bool parse_module_self_types(rbs_parser_t *parser, rbs_node_list_t *array
2494
2597
 
2495
2598
  rbs_range_t name_range;
2496
2599
  rbs_type_name_t *module_name = NULL;
2497
- CHECK_PARSE(parse_type_name(parser, CLASS_NAME | INTERFACE_NAME, &name_range, &module_name));
2600
+ CHECK_PARSE(parse_type_name(parser, (TypeNameKind) (CLASS_NAME | INTERFACE_NAME), &name_range, &module_name));
2498
2601
  self_range.end = name_range.end;
2499
2602
 
2500
2603
  rbs_node_list_t *args = rbs_node_list_new(ALLOCATOR());
@@ -2502,7 +2605,7 @@ static bool parse_module_self_types(rbs_parser_t *parser, rbs_node_list_t *array
2502
2605
  if (parser->next_token.type == pLBRACKET) {
2503
2606
  rbs_parser_advance(parser);
2504
2607
  args_range.start = parser->current_token.range.start;
2505
- CHECK_PARSE(parse_type_list(parser, pRBRACKET, args));
2608
+ CHECK_PARSE(parse_type_list(parser, pRBRACKET, args, true, false));
2506
2609
  rbs_parser_advance(parser);
2507
2610
  self_range.end = args_range.end = parser->current_token.range.end;
2508
2611
  }
@@ -2958,7 +3061,7 @@ static bool parse_decl(rbs_parser_t *parser, rbs_node_t **decl) {
2958
3061
  | {} <> (empty -- returns empty namespace)
2959
3062
  */
2960
3063
  NODISCARD
2961
- static bool parse_namespace(rbs_parser_t *parser, rbs_range_t *rg, rbs_namespace_t **namespace) {
3064
+ static bool parse_namespace(rbs_parser_t *parser, rbs_range_t *rg, rbs_namespace_t **out_ns) {
2962
3065
  bool is_absolute = false;
2963
3066
 
2964
3067
  if (parser->next_token.type == pCOLON2) {
@@ -2989,7 +3092,7 @@ static bool parse_namespace(rbs_parser_t *parser, rbs_range_t *rg, rbs_namespace
2989
3092
  }
2990
3093
  }
2991
3094
 
2992
- *namespace = rbs_namespace_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), *rg), path, is_absolute);
3095
+ *out_ns = rbs_namespace_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), *rg), path, is_absolute);
2993
3096
  return true;
2994
3097
  }
2995
3098
 
@@ -3004,8 +3107,8 @@ NODISCARD
3004
3107
  static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) {
3005
3108
  while (true) {
3006
3109
  rbs_range_t namespace_range = NULL_RANGE;
3007
- rbs_namespace_t *namespace = NULL;
3008
- CHECK_PARSE(parse_namespace(parser, &namespace_range, &namespace));
3110
+ rbs_namespace_t *ns = NULL;
3111
+ CHECK_PARSE(parse_namespace(parser, &namespace_range, &ns));
3009
3112
 
3010
3113
  switch (parser->next_token.type) {
3011
3114
  case tLIDENT:
@@ -3019,7 +3122,7 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) {
3019
3122
 
3020
3123
  rbs_location_t *symbolLoc = rbs_location_current_token(parser);
3021
3124
  rbs_ast_symbol_t *symbol = rbs_ast_symbol_new(ALLOCATOR(), symbolLoc, &parser->constant_pool, INTERN_TOKEN(parser, parser->current_token));
3022
- rbs_type_name_t *type_name = rbs_type_name_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), type_name_range), namespace, symbol);
3125
+ rbs_type_name_t *type_name = rbs_type_name_new(ALLOCATOR(), rbs_location_new(ALLOCATOR(), type_name_range), ns, symbol);
3023
3126
 
3024
3127
  rbs_range_t keyword_range = NULL_RANGE;
3025
3128
  rbs_range_t new_name_range = NULL_RANGE;
@@ -3062,7 +3165,7 @@ static bool parse_use_clauses(rbs_parser_t *parser, rbs_node_list_t *clauses) {
3062
3165
  rbs_loc_add_required_child(loc, INTERN("namespace"), namespace_range);
3063
3166
  rbs_loc_add_required_child(loc, INTERN("star"), star_range);
3064
3167
 
3065
- rbs_ast_directives_use_wildcard_clause_t *clause = rbs_ast_directives_use_wildcard_clause_new(ALLOCATOR(), loc, namespace);
3168
+ rbs_ast_directives_use_wildcard_clause_t *clause = rbs_ast_directives_use_wildcard_clause_new(ALLOCATOR(), loc, ns);
3066
3169
  rbs_node_list_append(clauses, (rbs_node_t *) clause);
3067
3170
 
3068
3171
  break;
@@ -3109,23 +3212,25 @@ static bool parse_use_directive(rbs_parser_t *parser, rbs_ast_directives_use_t *
3109
3212
  }
3110
3213
 
3111
3214
  static rbs_ast_comment_t *parse_comment_lines(rbs_parser_t *parser, rbs_comment_t *com) {
3112
- size_t hash_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) "#", (size_t) 1);
3113
- size_t space_bytes = parser->rbs_lexer_t->encoding->char_width((const uint8_t *) " ", (size_t) 1);
3215
+ size_t hash_bytes = parser->lexer->encoding->char_width((const uint8_t *) "#", (size_t) 1);
3216
+ size_t space_bytes = parser->lexer->encoding->char_width((const uint8_t *) " ", (size_t) 1);
3114
3217
 
3115
3218
  rbs_buffer_t rbs_buffer;
3116
3219
  rbs_buffer_init(ALLOCATOR(), &rbs_buffer);
3117
3220
 
3118
- for (size_t i = 0; i < com->line_count; i++) {
3119
- rbs_token_t tok = com->tokens[i];
3221
+ for (size_t i = 0; i < com->line_tokens_count; i++) {
3222
+ rbs_token_t tok = com->line_tokens[i];
3120
3223
 
3121
- const char *comment_start = parser->rbs_lexer_t->string.start + tok.range.start.byte_pos + hash_bytes;
3224
+ const char *comment_start = parser->lexer->string.start + tok.range.start.byte_pos + hash_bytes;
3122
3225
  size_t comment_bytes = RBS_RANGE_BYTES(tok.range) - hash_bytes;
3123
3226
 
3124
3227
  rbs_string_t str = rbs_string_new(
3125
3228
  comment_start,
3126
- parser->rbs_lexer_t->string.end
3229
+ parser->lexer->string.end
3127
3230
  );
3128
- unsigned char c = rbs_utf8_string_to_codepoint(str);
3231
+
3232
+ // Assumes the input is ASCII compatible
3233
+ unsigned char c = str.start[0];
3129
3234
 
3130
3235
  if (c == ' ') {
3131
3236
  comment_start += space_bytes;
@@ -3160,42 +3265,43 @@ static rbs_comment_t *comment_get_comment(rbs_comment_t *com, int line) {
3160
3265
  }
3161
3266
 
3162
3267
  static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *com, rbs_token_t comment_token) {
3163
- if (com->line_count == 0) {
3164
- com->start = comment_token.range.start;
3165
- }
3166
-
3167
- if (com->line_count == com->line_size) {
3168
- com->line_size += 10;
3169
-
3170
- if (com->tokens) {
3171
- rbs_token_t *p = com->tokens;
3172
- com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
3173
- memcpy(com->tokens, p, sizeof(rbs_token_t) * com->line_count);
3174
- } else {
3175
- com->tokens = rbs_allocator_calloc(allocator, com->line_size, rbs_token_t);
3176
- }
3268
+ if (com->line_tokens_count == com->line_tokens_capacity) {
3269
+ size_t old_size = com->line_tokens_capacity;
3270
+ size_t new_size = old_size * 2;
3271
+ com->line_tokens_capacity = new_size;
3272
+
3273
+ com->line_tokens = rbs_allocator_realloc(
3274
+ allocator,
3275
+ com->line_tokens,
3276
+ sizeof(rbs_token_t) * old_size,
3277
+ sizeof(rbs_token_t) * new_size,
3278
+ rbs_token_t
3279
+ );
3177
3280
  }
3178
3281
 
3179
- com->tokens[com->line_count++] = comment_token;
3282
+ com->line_tokens[com->line_tokens_count++] = comment_token;
3180
3283
  com->end = comment_token.range.end;
3181
3284
  }
3182
3285
 
3183
3286
  static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, rbs_token_t comment_token, rbs_comment_t *last_comment) {
3184
3287
  rbs_comment_t *new_comment = rbs_allocator_alloc(allocator, rbs_comment_t);
3185
3288
 
3289
+ size_t initial_line_capacity = 10;
3290
+
3291
+ rbs_token_t *tokens = rbs_allocator_calloc(allocator, initial_line_capacity, rbs_token_t);
3292
+ tokens[0] = comment_token;
3293
+
3186
3294
  *new_comment = (rbs_comment_t) {
3187
3295
  .start = comment_token.range.start,
3188
3296
  .end = comment_token.range.end,
3189
3297
 
3190
- .line_size = 0,
3191
- .line_count = 0,
3192
- .tokens = NULL,
3298
+ .line_tokens_capacity = initial_line_capacity,
3299
+ .line_tokens_count = 1,
3300
+ .line_tokens = tokens,
3193
3301
 
3194
3302
  .next_comment = last_comment,
3195
3303
  };
3196
3304
 
3197
- comment_insert_new_line(allocator, new_comment, comment_token);
3198
-
3199
3305
  return new_comment;
3200
3306
  }
3201
3307
 
@@ -3340,7 +3446,7 @@ void rbs_parser_advance(rbs_parser_t *parser) {
3340
3446
  break;
3341
3447
  }
3342
3448
 
3343
- parser->next_token3 = rbs_lexer_next_token(parser->rbs_lexer_t);
3449
+ parser->next_token3 = rbs_lexer_next_token(parser->lexer);
3344
3450
 
3345
3451
  if (parser->next_token3.type == tCOMMENT) {
3346
3452
  // skip
@@ -3363,6 +3469,14 @@ void rbs_print_token(rbs_token_t tok) {
3363
3469
  );
3364
3470
  }
3365
3471
 
3472
+ void rbs_print_lexer(rbs_lexer_t *lexer) {
3473
+ printf("Lexer: (range = %d...%d, encoding = %s\n", lexer->start_pos, lexer->end_pos, lexer->encoding->name);
3474
+ printf(" start = { char_pos = %d, byte_pos = %d }\n", lexer->start.char_pos, lexer->start.byte_pos);
3475
+ printf(" current = { char_pos = %d, byte_pos = %d }\n", lexer->current.char_pos, lexer->current.byte_pos);
3476
+ printf(" character = { code_point = %d (%c), bytes = %zu }\n", lexer->current_code_point, lexer->current_code_point < 256 ? lexer->current_code_point : '?', lexer->current_character_bytes);
3477
+ printf(" first_token_of_line = %s\n", lexer->first_token_of_line ? "true" : "false");
3478
+ }
3479
+
3366
3480
  rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line) {
3367
3481
  int comment_line = subject_line - 1;
3368
3482
 
@@ -3391,14 +3505,28 @@ rbs_lexer_t *rbs_lexer_new(rbs_allocator_t *allocator, rbs_string_t string, cons
3391
3505
  .end_pos = end_pos,
3392
3506
  .current = start_position,
3393
3507
  .start = { 0 },
3394
- .first_token_of_line = false,
3395
- .last_char = 0,
3508
+ .first_token_of_line = true,
3509
+ .current_character_bytes = 0,
3510
+ .current_code_point = '\0',
3396
3511
  .encoding = encoding,
3397
3512
  };
3398
3513
 
3399
- rbs_skipn(lexer, start_pos);
3514
+ unsigned int codepoint;
3515
+ size_t bytes;
3516
+
3517
+ if (rbs_next_char(lexer, &codepoint, &bytes)) {
3518
+ lexer->current_code_point = codepoint;
3519
+ lexer->current_character_bytes = bytes;
3520
+ } else {
3521
+ lexer->current_code_point = '\0';
3522
+ lexer->current_character_bytes = 1;
3523
+ }
3524
+
3525
+ if (start_pos > 0) {
3526
+ rbs_skipn(lexer, start_pos);
3527
+ }
3528
+
3400
3529
  lexer->start = lexer->current;
3401
- lexer->first_token_of_line = lexer->current.column == 0;
3402
3530
 
3403
3531
  return lexer;
3404
3532
  }
@@ -3410,7 +3538,7 @@ rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding
3410
3538
  rbs_parser_t *parser = rbs_allocator_alloc(allocator, rbs_parser_t);
3411
3539
 
3412
3540
  *parser = (rbs_parser_t) {
3413
- .rbs_lexer_t = lexer,
3541
+ .lexer = lexer,
3414
3542
 
3415
3543
  .current_token = NullToken,
3416
3544
  .next_token = NullToken,
@@ -3491,7 +3619,7 @@ static bool parse_method_overload(rbs_parser_t *parser, rbs_node_list_t *annotat
3491
3619
  return false;
3492
3620
  }
3493
3621
 
3494
- return rbs_parse_method_type(parser, method_type);
3622
+ return rbs_parse_method_type(parser, method_type, false);
3495
3623
  }
3496
3624
 
3497
3625
  /*
@@ -3650,7 +3778,7 @@ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_a
3650
3778
  rbs_location_t *colon_loc = rbs_location_new(ALLOCATOR(), colon_range);
3651
3779
 
3652
3780
  rbs_node_t *return_type = NULL;
3653
- if (!rbs_parse_type(parser, &return_type)) {
3781
+ if (!rbs_parse_type(parser, &return_type, true, true)) {
3654
3782
  return false;
3655
3783
  }
3656
3784
 
@@ -3678,6 +3806,53 @@ static bool parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_a
3678
3806
  );
3679
3807
  return true;
3680
3808
  }
3809
+ case tAIDENT: {
3810
+ // Parse instance variable annotation: @rbs @name: Type
3811
+ rbs_parser_advance(parser);
3812
+
3813
+ rbs_range_t ivar_name_range = parser->current_token.range;
3814
+ rbs_location_t *ivar_name_loc = rbs_location_new(ALLOCATOR(), ivar_name_range);
3815
+
3816
+ // Extract the instance variable name as a symbol
3817
+ rbs_string_t ivar_string = rbs_parser_peek_current_token(parser);
3818
+ rbs_constant_id_t ivar_id = rbs_constant_pool_insert_string(&parser->constant_pool, ivar_string);
3819
+ rbs_ast_symbol_t *ivar_name = rbs_ast_symbol_new(ALLOCATOR(), ivar_name_loc, &parser->constant_pool, ivar_id);
3820
+
3821
+ ADVANCE_ASSERT(parser, pCOLON);
3822
+
3823
+ rbs_range_t colon_range = parser->current_token.range;
3824
+ rbs_location_t *colon_loc = rbs_location_new(ALLOCATOR(), colon_range);
3825
+
3826
+ rbs_node_t *type = NULL;
3827
+ if (!rbs_parse_type(parser, &type, false, true)) {
3828
+ return false;
3829
+ }
3830
+
3831
+ rbs_location_t *comment_loc = NULL;
3832
+ if (!parse_inline_comment(parser, &comment_loc)) {
3833
+ return false;
3834
+ }
3835
+
3836
+ rbs_range_t full_range = {
3837
+ .start = rbs_range.start,
3838
+ .end = parser->current_token.range.end
3839
+ };
3840
+
3841
+ rbs_location_t *full_loc = rbs_location_new(ALLOCATOR(), full_range);
3842
+ rbs_location_t *rbs_loc = rbs_location_new(ALLOCATOR(), rbs_range);
3843
+
3844
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_instance_variable_annotation_new(
3845
+ ALLOCATOR(),
3846
+ full_loc,
3847
+ rbs_loc,
3848
+ ivar_name,
3849
+ ivar_name_loc,
3850
+ colon_loc,
3851
+ type,
3852
+ comment_loc
3853
+ );
3854
+ return true;
3855
+ }
3681
3856
  default: {
3682
3857
  rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for @rbs annotation");
3683
3858
  return false;
@@ -3699,8 +3874,63 @@ static bool parse_inline_trailing_annotation(rbs_parser_t *parser, rbs_ast_ruby_
3699
3874
  case pCOLON: {
3700
3875
  rbs_parser_advance(parser);
3701
3876
 
3877
+ // Check for class-alias or module-alias keywords
3878
+ if (parser->next_token.type == kCLASSALIAS || parser->next_token.type == kMODULEALIAS) {
3879
+ bool is_class_alias = (parser->next_token.type == kCLASSALIAS);
3880
+ rbs_range_t keyword_range = parser->next_token.range;
3881
+ rbs_location_t *keyword_loc = rbs_location_new(ALLOCATOR(), keyword_range);
3882
+ rbs_parser_advance(parser);
3883
+
3884
+ rbs_type_name_t *type_name = NULL;
3885
+ rbs_location_t *type_name_loc = NULL;
3886
+ rbs_range_t full_range;
3887
+
3888
+ // Check if a type name is provided
3889
+ if (parser->next_token.type == tUIDENT || parser->next_token.type == pCOLON2) {
3890
+ rbs_parser_advance(parser);
3891
+
3892
+ rbs_range_t type_name_range;
3893
+ if (!parse_type_name(parser, CLASS_NAME, &type_name_range, &type_name)) {
3894
+ return false;
3895
+ }
3896
+ // parse_type_name leaves current_token at the last identifier, don't advance
3897
+ type_name_loc = rbs_location_new(ALLOCATOR(), type_name_range);
3898
+ full_range.start = prefix_range.start;
3899
+ full_range.end = type_name_range.end;
3900
+ } else {
3901
+ // No type name provided - will be inferred
3902
+ full_range.start = prefix_range.start;
3903
+ full_range.end = keyword_range.end;
3904
+ }
3905
+
3906
+ rbs_location_t *full_loc = rbs_location_new(ALLOCATOR(), full_range);
3907
+ rbs_location_t *prefix_loc = rbs_location_new(ALLOCATOR(), prefix_range);
3908
+
3909
+ if (is_class_alias) {
3910
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_class_alias_annotation_new(
3911
+ ALLOCATOR(),
3912
+ full_loc,
3913
+ prefix_loc,
3914
+ keyword_loc,
3915
+ type_name,
3916
+ type_name_loc
3917
+ );
3918
+ } else {
3919
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_module_alias_annotation_new(
3920
+ ALLOCATOR(),
3921
+ full_loc,
3922
+ prefix_loc,
3923
+ keyword_loc,
3924
+ type_name,
3925
+ type_name_loc
3926
+ );
3927
+ }
3928
+ return true;
3929
+ }
3930
+
3931
+ // Otherwise, parse as regular type assertion
3702
3932
  rbs_node_t *type = NULL;
3703
- if (!rbs_parse_type(parser, &type)) {
3933
+ if (!rbs_parse_type(parser, &type, true, true)) {
3704
3934
  return false;
3705
3935
  }
3706
3936
 
@@ -3720,6 +3950,43 @@ static bool parse_inline_trailing_annotation(rbs_parser_t *parser, rbs_ast_ruby_
3720
3950
  );
3721
3951
  return true;
3722
3952
  }
3953
+ case pLBRACKET: {
3954
+ rbs_parser_advance(parser);
3955
+
3956
+ rbs_node_list_t *type_args = rbs_node_list_new(ALLOCATOR());
3957
+ rbs_location_list_t *comma_locations = rbs_location_list_new(ALLOCATOR());
3958
+
3959
+ // Check for empty type args
3960
+ if (parser->next_token.type == pRBRACKET) {
3961
+ rbs_parser_set_error(parser, parser->next_token, true, "type application cannot be empty");
3962
+ return false;
3963
+ }
3964
+
3965
+ // Parse type list with comma tracking
3966
+ CHECK_PARSE(parse_type_list_with_commas(parser, pRBRACKET, type_args, comma_locations, true, true));
3967
+
3968
+ rbs_range_t close_bracket_range = parser->next_token.range;
3969
+ rbs_location_t *close_bracket_loc = rbs_location_new(ALLOCATOR(), close_bracket_range);
3970
+ rbs_parser_advance(parser); // consume ]
3971
+
3972
+ rbs_range_t full_range = {
3973
+ .start = prefix_range.start,
3974
+ .end = close_bracket_range.end
3975
+ };
3976
+
3977
+ rbs_location_t *full_loc = rbs_location_new(ALLOCATOR(), full_range);
3978
+ rbs_location_t *prefix_loc = rbs_location_new(ALLOCATOR(), prefix_range);
3979
+
3980
+ *annotation = (rbs_ast_ruby_annotations_t *) rbs_ast_ruby_annotations_type_application_annotation_new(
3981
+ ALLOCATOR(),
3982
+ full_loc,
3983
+ prefix_loc,
3984
+ type_args,
3985
+ close_bracket_loc,
3986
+ comma_locations
3987
+ );
3988
+ return true;
3989
+ }
3723
3990
  default: {
3724
3991
  rbs_parser_set_error(parser, parser->next_token, true, "unexpected token for inline trailing annotation");
3725
3992
  return false;