rbs 3.10.0 → 4.0.0.dev.1

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 (202) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/comments.yml +3 -3
  3. data/.github/workflows/ruby.yml +24 -35
  4. data/.github/workflows/typecheck.yml +3 -1
  5. data/.github/workflows/windows.yml +2 -2
  6. data/.gitignore +0 -4
  7. data/CHANGELOG.md +0 -88
  8. data/README.md +1 -38
  9. data/Rakefile +20 -142
  10. data/Steepfile +1 -0
  11. data/config.yml +43 -1
  12. data/core/array.rbs +46 -100
  13. data/core/complex.rbs +21 -32
  14. data/core/dir.rbs +2 -2
  15. data/core/encoding.rbs +9 -6
  16. data/core/enumerable.rbs +3 -90
  17. data/core/enumerator.rbs +1 -18
  18. data/core/errno.rbs +0 -8
  19. data/core/errors.rbs +1 -28
  20. data/core/exception.rbs +2 -2
  21. data/core/fiber.rbs +4 -5
  22. data/core/file.rbs +12 -27
  23. data/core/file_test.rbs +1 -1
  24. data/core/float.rbs +22 -209
  25. data/core/gc.rbs +281 -417
  26. data/core/hash.rbs +727 -1024
  27. data/core/integer.rbs +38 -78
  28. data/core/io/buffer.rbs +7 -18
  29. data/core/io/wait.rbs +33 -11
  30. data/core/io.rbs +12 -14
  31. data/core/kernel.rbs +51 -57
  32. data/core/marshal.rbs +1 -1
  33. data/core/match_data.rbs +1 -1
  34. data/core/math.rbs +3 -42
  35. data/core/method.rbs +6 -14
  36. data/core/module.rbs +17 -88
  37. data/core/nil_class.rbs +3 -3
  38. data/core/numeric.rbs +16 -16
  39. data/core/object.rbs +3 -3
  40. data/core/object_space.rbs +15 -21
  41. data/core/proc.rbs +8 -15
  42. data/core/process.rbs +2 -2
  43. data/core/ractor.rbs +437 -278
  44. data/core/range.rbs +8 -7
  45. data/core/rational.rbs +24 -37
  46. data/core/rbs/unnamed/argf.rbs +2 -2
  47. data/core/rbs/unnamed/env_class.rbs +1 -1
  48. data/core/rbs/unnamed/random.rbs +2 -4
  49. data/core/regexp.rbs +20 -25
  50. data/core/ruby_vm.rbs +4 -6
  51. data/core/rubygems/errors.rbs +70 -3
  52. data/core/rubygems/rubygems.rbs +79 -11
  53. data/core/rubygems/version.rbs +3 -2
  54. data/core/set.rbs +359 -488
  55. data/core/string.rbs +1228 -3153
  56. data/core/struct.rbs +1 -1
  57. data/core/symbol.rbs +4 -4
  58. data/core/thread.rbs +29 -92
  59. data/core/time.rbs +9 -35
  60. data/core/trace_point.rbs +4 -7
  61. data/core/unbound_method.rbs +6 -14
  62. data/docs/collection.md +2 -2
  63. data/docs/gem.md +1 -0
  64. data/docs/sigs.md +3 -3
  65. data/ext/rbs_extension/ast_translation.c +1077 -944
  66. data/ext/rbs_extension/ast_translation.h +0 -7
  67. data/ext/rbs_extension/class_constants.c +83 -71
  68. data/ext/rbs_extension/class_constants.h +7 -4
  69. data/ext/rbs_extension/extconf.rb +2 -24
  70. data/ext/rbs_extension/legacy_location.c +172 -173
  71. data/ext/rbs_extension/legacy_location.h +3 -8
  72. data/ext/rbs_extension/main.c +289 -239
  73. data/ext/rbs_extension/rbs_extension.h +0 -3
  74. data/ext/rbs_extension/rbs_string_bridging.h +0 -4
  75. data/include/rbs/ast.h +98 -37
  76. data/include/rbs/defines.h +12 -38
  77. data/include/rbs/lexer.h +114 -126
  78. data/include/rbs/location.h +14 -14
  79. data/include/rbs/parser.h +37 -21
  80. data/include/rbs/string.h +5 -3
  81. data/include/rbs/util/rbs_allocator.h +19 -40
  82. data/include/rbs/util/rbs_assert.h +1 -12
  83. data/include/rbs/util/rbs_constant_pool.h +3 -3
  84. data/include/rbs/util/rbs_encoding.h +1 -3
  85. data/include/rbs/util/rbs_unescape.h +1 -2
  86. data/lib/rbs/ast/ruby/annotations.rb +119 -0
  87. data/lib/rbs/ast/ruby/comment_block.rb +221 -0
  88. data/lib/rbs/ast/ruby/declarations.rb +86 -0
  89. data/lib/rbs/ast/ruby/helpers/constant_helper.rb +24 -0
  90. data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
  91. data/lib/rbs/ast/ruby/members.rb +213 -0
  92. data/lib/rbs/buffer.rb +104 -24
  93. data/lib/rbs/cli/validate.rb +40 -35
  94. data/lib/rbs/cli.rb +5 -6
  95. data/lib/rbs/collection/config/lockfile_generator.rb +0 -1
  96. data/lib/rbs/collection.rb +0 -1
  97. data/lib/rbs/definition.rb +6 -1
  98. data/lib/rbs/definition_builder/ancestor_builder.rb +65 -62
  99. data/lib/rbs/definition_builder/method_builder.rb +45 -30
  100. data/lib/rbs/definition_builder.rb +44 -9
  101. data/lib/rbs/environment/class_entry.rb +69 -0
  102. data/lib/rbs/environment/module_entry.rb +66 -0
  103. data/lib/rbs/environment.rb +244 -218
  104. data/lib/rbs/environment_loader.rb +3 -3
  105. data/lib/rbs/errors.rb +5 -4
  106. data/lib/rbs/inline_parser/comment_association.rb +117 -0
  107. data/lib/rbs/inline_parser.rb +206 -0
  108. data/lib/rbs/location_aux.rb +35 -3
  109. data/lib/rbs/parser_aux.rb +11 -6
  110. data/lib/rbs/prototype/runtime.rb +2 -2
  111. data/lib/rbs/resolver/constant_resolver.rb +2 -2
  112. data/lib/rbs/resolver/type_name_resolver.rb +38 -124
  113. data/lib/rbs/source.rb +99 -0
  114. data/lib/rbs/subtractor.rb +4 -3
  115. data/lib/rbs/test/type_check.rb +0 -14
  116. data/lib/rbs/types.rb +1 -3
  117. data/lib/rbs/version.rb +1 -1
  118. data/lib/rbs.rb +13 -1
  119. data/lib/rdoc/discover.rb +1 -1
  120. data/lib/rdoc_plugin/parser.rb +1 -1
  121. data/rbs.gemspec +1 -0
  122. data/sig/ancestor_builder.rbs +1 -1
  123. data/sig/ast/ruby/annotations.rbs +110 -0
  124. data/sig/ast/ruby/comment_block.rbs +119 -0
  125. data/sig/ast/ruby/declarations.rbs +60 -0
  126. data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
  127. data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
  128. data/sig/ast/ruby/members.rbs +72 -0
  129. data/sig/buffer.rbs +63 -5
  130. data/sig/definition.rbs +1 -0
  131. data/sig/definition_builder.rbs +1 -1
  132. data/sig/environment/class_entry.rbs +50 -0
  133. data/sig/environment/module_entry.rbs +50 -0
  134. data/sig/environment.rbs +28 -133
  135. data/sig/errors.rbs +13 -6
  136. data/sig/inline_parser/comment_association.rbs +71 -0
  137. data/sig/inline_parser.rbs +87 -0
  138. data/sig/location.rbs +32 -7
  139. data/sig/manifest.yaml +1 -0
  140. data/sig/method_builder.rbs +7 -4
  141. data/sig/parser.rbs +16 -20
  142. data/sig/resolver/type_name_resolver.rbs +7 -38
  143. data/sig/source.rbs +48 -0
  144. data/sig/types.rbs +1 -4
  145. data/src/ast.c +290 -201
  146. data/src/lexer.c +2813 -2902
  147. data/src/lexer.re +4 -0
  148. data/src/lexstate.c +155 -169
  149. data/src/location.c +40 -40
  150. data/src/parser.c +2665 -2433
  151. data/src/string.c +48 -0
  152. data/src/util/rbs_allocator.c +77 -80
  153. data/src/util/rbs_assert.c +10 -10
  154. data/src/util/rbs_buffer.c +2 -2
  155. data/src/util/rbs_constant_pool.c +15 -13
  156. data/src/util/rbs_encoding.c +4062 -20097
  157. data/src/util/rbs_unescape.c +48 -85
  158. data/stdlib/bigdecimal/0/big_decimal.rbs +82 -100
  159. data/stdlib/bigdecimal-math/0/big_math.rbs +8 -169
  160. data/stdlib/cgi/0/core.rbs +396 -2
  161. data/stdlib/cgi/0/manifest.yaml +0 -1
  162. data/stdlib/coverage/0/coverage.rbs +1 -3
  163. data/stdlib/date/0/date.rbs +59 -67
  164. data/stdlib/date/0/date_time.rbs +1 -1
  165. data/stdlib/delegate/0/delegator.rbs +7 -10
  166. data/stdlib/erb/0/erb.rbs +347 -737
  167. data/stdlib/fileutils/0/fileutils.rbs +13 -18
  168. data/stdlib/forwardable/0/forwardable.rbs +0 -3
  169. data/stdlib/json/0/json.rbs +48 -68
  170. data/stdlib/net-http/0/net-http.rbs +0 -3
  171. data/stdlib/objspace/0/objspace.rbs +4 -9
  172. data/stdlib/open-uri/0/open-uri.rbs +0 -40
  173. data/stdlib/openssl/0/openssl.rbs +228 -331
  174. data/stdlib/optparse/0/optparse.rbs +3 -3
  175. data/{core → stdlib/pathname/0}/pathname.rbs +355 -255
  176. data/stdlib/psych/0/psych.rbs +3 -3
  177. data/stdlib/rdoc/0/rdoc.rbs +1 -1
  178. data/stdlib/resolv/0/resolv.rbs +68 -25
  179. data/stdlib/ripper/0/ripper.rbs +2 -5
  180. data/stdlib/singleton/0/singleton.rbs +0 -3
  181. data/stdlib/socket/0/socket.rbs +1 -13
  182. data/stdlib/socket/0/tcp_socket.rbs +2 -10
  183. data/stdlib/stringio/0/stringio.rbs +85 -1176
  184. data/stdlib/strscan/0/string_scanner.rbs +31 -31
  185. data/stdlib/tempfile/0/tempfile.rbs +3 -3
  186. data/stdlib/time/0/time.rbs +1 -1
  187. data/stdlib/timeout/0/timeout.rbs +7 -63
  188. data/stdlib/tsort/0/cyclic.rbs +0 -3
  189. data/stdlib/uri/0/common.rbs +2 -11
  190. data/stdlib/uri/0/file.rbs +1 -1
  191. data/stdlib/uri/0/generic.rbs +16 -17
  192. data/stdlib/uri/0/rfc2396_parser.rbs +7 -6
  193. data/stdlib/zlib/0/zstream.rbs +0 -1
  194. metadata +40 -12
  195. data/.clang-format +0 -74
  196. data/.clangd +0 -2
  197. data/.github/workflows/c-check.yml +0 -54
  198. data/core/ruby.rbs +0 -53
  199. data/docs/aliases.md +0 -79
  200. data/docs/encoding.md +0 -56
  201. data/ext/rbs_extension/compat.h +0 -10
  202. data/stdlib/cgi-escape/0/escape.rbs +0 -153
data/include/rbs/parser.h CHANGED
@@ -24,39 +24,39 @@
24
24
  * A comment object represents the six lines of comments.
25
25
  * */
26
26
  typedef struct rbs_comment_t {
27
- rbs_position_t start;
28
- rbs_position_t end;
27
+ rbs_position_t start;
28
+ rbs_position_t end;
29
29
 
30
- size_t line_tokens_capacity;
31
- size_t line_tokens_count;
32
- rbs_token_t *line_tokens;
30
+ size_t line_size;
31
+ size_t line_count;
32
+ rbs_token_t *tokens;
33
33
 
34
- struct rbs_comment_t *next_comment;
34
+ struct rbs_comment_t *next_comment;
35
35
  } rbs_comment_t;
36
36
 
37
37
  typedef struct rbs_error_t {
38
- char *message;
39
- rbs_token_t token;
40
- bool syntax_error;
38
+ char *message;
39
+ rbs_token_t token;
40
+ bool syntax_error;
41
41
  } rbs_error_t;
42
42
 
43
43
  /**
44
44
  * An RBS parser is a LL(3) parser.
45
45
  * */
46
46
  typedef struct {
47
- rbs_lexer_t *lexer;
47
+ rbs_lexer_t *rbs_lexer_t;
48
48
 
49
- rbs_token_t current_token;
50
- rbs_token_t next_token; /* The first lookahead token */
51
- rbs_token_t next_token2; /* The second lookahead token */
52
- rbs_token_t next_token3; /* The third lookahead token */
49
+ rbs_token_t current_token;
50
+ rbs_token_t next_token; /* The first lookahead token */
51
+ rbs_token_t next_token2; /* The second lookahead token */
52
+ rbs_token_t next_token3; /* The third lookahead token */
53
53
 
54
- struct id_table *vars; /* Known type variables */
55
- rbs_comment_t *last_comment; /* Last read comment */
54
+ struct id_table *vars; /* Known type variables */
55
+ rbs_comment_t *last_comment; /* Last read comment */
56
56
 
57
- rbs_constant_pool_t constant_pool;
58
- rbs_allocator_t *allocator;
59
- rbs_error_t *error;
57
+ rbs_constant_pool_t constant_pool;
58
+ rbs_allocator_t *allocator;
59
+ rbs_error_t *error;
60
60
  } rbs_parser_t;
61
61
 
62
62
  /**
@@ -127,9 +127,25 @@ rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line
127
127
  void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_error, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(4, 5);
128
128
 
129
129
  bool rbs_parse_type(rbs_parser_t *parser, rbs_node_t **type);
130
- bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type, bool require_eof);
130
+ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type);
131
131
  bool rbs_parse_signature(rbs_parser_t *parser, rbs_signature_t **signature);
132
132
 
133
- bool rbs_parse_type_params(rbs_parser_t *parser, bool module_type_params, rbs_node_list_t **params);
133
+ /**
134
+ * Parse an inline leading annotation from a string.
135
+ *
136
+ * @param parser The parser to use
137
+ * @param annotation Pointer to store the resulting annotation
138
+ * @return true if parsing succeeded, false otherwise
139
+ */
140
+ bool rbs_parse_inline_leading_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation);
141
+
142
+ /**
143
+ * Parse an inline trailing annotation from a string.
144
+ *
145
+ * @param parser The parser to use
146
+ * @param annotation Pointer to store the resulting annotation
147
+ * @return true if parsing succeeded, false otherwise
148
+ */
149
+ bool rbs_parse_inline_trailing_annotation(rbs_parser_t *parser, rbs_ast_ruby_annotations_t **annotation);
134
150
 
135
151
  #endif
data/include/rbs/string.h CHANGED
@@ -6,14 +6,14 @@
6
6
  #include "rbs/util/rbs_allocator.h"
7
7
 
8
8
  typedef struct {
9
- const char *start;
10
- const char *end;
9
+ const char *start;
10
+ const char *end;
11
11
  } rbs_string_t;
12
12
 
13
13
  #define RBS_STRING_NULL ((rbs_string_t) { \
14
14
  .start = NULL, \
15
15
  .end = NULL, \
16
- })
16
+ })
17
17
 
18
18
  /**
19
19
  * Returns a new `rbs_string_t` struct
@@ -44,4 +44,6 @@ size_t rbs_string_len(const rbs_string_t self);
44
44
  */
45
45
  bool rbs_string_equal(const rbs_string_t lhs, const rbs_string_t rhs);
46
46
 
47
+ unsigned int rbs_utf8_string_to_codepoint(const rbs_string_t string);
48
+
47
49
  #endif
@@ -3,57 +3,36 @@
3
3
 
4
4
  #include <stddef.h>
5
5
 
6
- /* Include stdalign.h for C11 and later for alignof support */
7
- #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
8
- #include <stdalign.h>
6
+ #ifndef alignof
7
+ #if defined(__GNUC__) || defined(__clang__)
8
+ #define alignof(type) __alignof__(type)
9
+ #elif defined(_MSC_VER)
10
+ #define alignof(type) __alignof(type)
11
+ #else
12
+ // Fallback using offset trick
13
+ #define alignof(type) offsetof(struct { char c; type member; }, member)
14
+ #endif
9
15
  #endif
10
16
 
11
- /*
12
- * Define a portable alignment macro that works across all supported environments
13
- */
14
- #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
15
- /* C11 or later - use _Alignof directly (always available in C11+) */
16
- #define rbs_alignof(type) _Alignof(type)
17
- #elif defined(__cplusplus) && __cplusplus >= 201103L
18
- /* C++11 or later has alignof keyword */
19
- #define rbs_alignof(type) alignof(type)
20
- #elif defined(__GNUC__) || defined(__clang__)
21
- /* GCC and Clang provide __alignof__ */
22
- #define rbs_alignof(type) __alignof__(type)
23
- #elif defined(_MSC_VER)
24
- /* MSVC provides __alignof */
25
- #define rbs_alignof(type) __alignof(type)
26
- #else
27
- /* Fallback using offset trick for other compilers */
28
- #define rbs_alignof(type) offsetof( \
29
- struct { char c; type member; }, \
30
- member \
31
- )
32
- #endif
33
-
34
- typedef struct rbs_allocator {
35
- // The head of a linked list of pages, starting with the most recently allocated page.
36
- struct rbs_allocator_page *page;
37
-
38
- size_t default_page_payload_size;
39
- } rbs_allocator_t;
17
+ struct rbs_allocator;
18
+ typedef struct rbs_allocator rbs_allocator_t;
40
19
 
41
20
  rbs_allocator_t *rbs_allocator_init(void);
42
21
  void rbs_allocator_free(rbs_allocator_t *);
43
- void *rbs_allocator_malloc_impl(rbs_allocator_t *, /* 1 */ size_t size, size_t alignment);
44
- void *rbs_allocator_malloc_many_impl(rbs_allocator_t *, size_t count, size_t size, size_t alignment);
45
- void *rbs_allocator_calloc_impl(rbs_allocator_t *, size_t count, size_t size, size_t alignment);
22
+ void *rbs_allocator_malloc_impl (rbs_allocator_t *, /* 1 */ size_t size, size_t alignment);
23
+ void *rbs_allocator_malloc_many_impl (rbs_allocator_t *, size_t count, size_t size, size_t alignment);
24
+ void *rbs_allocator_calloc_impl (rbs_allocator_t *, size_t count, size_t size, size_t alignment);
46
25
 
47
- void *rbs_allocator_realloc_impl(rbs_allocator_t *, void *ptr, size_t old_size, size_t new_size, size_t alignment);
26
+ void *rbs_allocator_realloc_impl (rbs_allocator_t *, void *ptr, size_t old_size, size_t new_size, size_t alignment);
48
27
 
49
28
  // Use this when allocating memory for a single instance of a type.
50
- #define rbs_allocator_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), rbs_alignof(type)))
29
+ #define rbs_allocator_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), alignof(type)))
51
30
  // Use this when allocating memory that will be immediately written to in full.
52
31
  // Such as allocating strings
53
- #define rbs_allocator_alloc_many(allocator, count, type) ((type *) rbs_allocator_malloc_many_impl((allocator), (count), sizeof(type), rbs_alignof(type)))
32
+ #define rbs_allocator_alloc_many(allocator, count, type) ((type *) rbs_allocator_malloc_many_impl((allocator), (count), sizeof(type), alignof(type)))
54
33
  // Use this when allocating memory that will NOT be immediately written to in full.
55
34
  // Such as allocating buffers
56
- #define rbs_allocator_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), rbs_alignof(type)))
57
- #define rbs_allocator_realloc(allocator, ptr, old_size, new_size, type) ((type *) rbs_allocator_realloc_impl((allocator), (ptr), (old_size), (new_size), rbs_alignof(type)))
35
+ #define rbs_allocator_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), alignof(type)))
36
+ #define rbs_allocator_realloc(allocator, ptr, old_size, new_size, type) ((type *) rbs_allocator_realloc_impl((allocator), (ptr), (old_size), (new_size), alignof(type)))
58
37
 
59
38
  #endif
@@ -4,17 +4,6 @@
4
4
  #include "rbs/defines.h"
5
5
  #include <stdbool.h>
6
6
 
7
- /**
8
- * RBS_ASSERT macro that calls rbs_assert in debug builds and is removed in release builds.
9
- * In debug mode, it forwards all arguments to the rbs_assert function.
10
- * In release mode, it expands to nothing.
11
- */
12
- #ifdef NDEBUG
13
- #define RBS_ASSERT(condition, ...) ((void) 0)
14
- #else
15
- #define RBS_ASSERT(condition, ...) rbs_assert_impl(condition, __VA_ARGS__)
16
- #endif
17
-
18
- void rbs_assert_impl(bool condition, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(2, 3);
7
+ void rbs_assert(bool condition, const char *fmt, ...) RBS_ATTRIBUTE_FORMAT(2, 3);
19
8
 
20
9
  #endif
@@ -48,10 +48,10 @@ static const rbs_constant_pool_bucket_type_t RBS_CONSTANT_POOL_BUCKET_CONSTANT =
48
48
  /** A bucket in the hash map. */
49
49
  typedef struct {
50
50
  /** The incremental ID used for indexing back into the pool. */
51
- unsigned int id : 30;
51
+ unsigned int id: 30;
52
52
 
53
53
  /** The type of the bucket, which determines how to free it. */
54
- rbs_constant_pool_bucket_type_t type : 2;
54
+ rbs_constant_pool_bucket_type_t type: 2;
55
55
 
56
56
  /** The hash of the bucket. */
57
57
  uint32_t hash;
@@ -100,7 +100,7 @@ bool rbs_constant_pool_init(rbs_constant_pool_t *pool, uint32_t capacity);
100
100
  * @param constant_id The id of the constant to get.
101
101
  * @return A pointer to the constant.
102
102
  */
103
- rbs_constant_t *rbs_constant_pool_id_to_constant(const rbs_constant_pool_t *pool, rbs_constant_id_t constant_id);
103
+ rbs_constant_t * rbs_constant_pool_id_to_constant(const rbs_constant_pool_t *pool, rbs_constant_id_t constant_id);
104
104
 
105
105
  /**
106
106
  * Find a constant in a constant pool. Returns the id of the constant, or 0 if
@@ -6,8 +6,6 @@
6
6
  #ifndef RBS_RBS_ENCODING_H
7
7
  #define RBS_RBS_ENCODING_H
8
8
 
9
- #include "rbs/defines.h"
10
-
11
9
  #include <assert.h>
12
10
  #include <stdbool.h>
13
11
  #include <stddef.h>
@@ -277,6 +275,6 @@ extern const rbs_encoding_t rbs_encodings[RBS_ENCODING_MAXIMUM];
277
275
  * @param end A pointer to the last byte of the name.
278
276
  * @returns A pointer to the encoding struct if one is found, otherwise NULL.
279
277
  */
280
- const rbs_encoding_t *rbs_encoding_find(const uint8_t *start, const uint8_t *end);
278
+ const rbs_encoding_t * rbs_encoding_find(const uint8_t *start, const uint8_t *end);
281
279
 
282
280
  #endif
@@ -4,7 +4,6 @@
4
4
  #include <stddef.h>
5
5
  #include "rbs/util/rbs_allocator.h"
6
6
  #include "rbs/string.h"
7
- #include "rbs/util/rbs_encoding.h"
8
7
 
9
8
  /**
10
9
  * Receives `rbs_parser_t` and `range`, which represents a string token or symbol token, and returns a string VALUE.
@@ -19,6 +18,6 @@
19
18
  *
20
19
  * @returns A new owned string that will be freed when the allocator is freed.
21
20
  * */
22
- rbs_string_t rbs_unquote_string(rbs_allocator_t *, const rbs_string_t input, const rbs_encoding_t *encoding);
21
+ rbs_string_t rbs_unquote_string(rbs_allocator_t *, const rbs_string_t input);
23
22
 
24
23
  #endif // RBS_RBS_UNESCAPE_H
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RBS
4
+ module AST
5
+ module Ruby
6
+ module Annotations
7
+ class Base
8
+ attr_reader :location, :prefix_location
9
+
10
+ def initialize(location, prefix_location)
11
+ @location = location
12
+ @prefix_location = prefix_location
13
+ end
14
+
15
+ def buffer
16
+ location.buffer
17
+ end
18
+ end
19
+
20
+ class NodeTypeAssertion < Base
21
+ attr_reader :type
22
+
23
+ def initialize(location:, prefix_location:, type:)
24
+ super(location, prefix_location)
25
+ @type = type
26
+ end
27
+
28
+ def map_type_name
29
+ self.class.new(
30
+ location:, prefix_location:,
31
+ type: type.map_type_name { yield _1 }
32
+ ) #: self
33
+ end
34
+ end
35
+
36
+ class ColonMethodTypeAnnotation < Base
37
+ attr_reader :annotations, :method_type
38
+
39
+ def initialize(location:, prefix_location:, annotations:, method_type:)
40
+ super(location, prefix_location)
41
+ @annotations = annotations
42
+ @method_type = method_type
43
+ end
44
+
45
+ def map_type_name
46
+ self.class.new(
47
+ location:,
48
+ prefix_location:,
49
+ annotations: annotations,
50
+ method_type: method_type.map_type {|type| type.map_type_name { yield _1 }}
51
+ ) #: self
52
+ end
53
+ end
54
+
55
+ class MethodTypesAnnotation < Base
56
+ Overload = AST::Members::MethodDefinition::Overload
57
+
58
+ attr_reader :overloads, :vertical_bar_locations
59
+
60
+ def initialize(location:, prefix_location:, overloads:, vertical_bar_locations:)
61
+ super(location, prefix_location)
62
+ @overloads = overloads
63
+ @vertical_bar_locations = vertical_bar_locations
64
+ end
65
+
66
+ def map_type_name(&block)
67
+ ovs = overloads.map do |overload|
68
+ Overload.new(
69
+ method_type: overload.method_type.map_type {|type| type.map_type_name { yield _1 } },
70
+ annotations: overload.annotations
71
+ )
72
+ end
73
+
74
+ self.class.new(location:, prefix_location:, overloads: ovs, vertical_bar_locations:) #: self
75
+ end
76
+ end
77
+
78
+ class SkipAnnotation < Base
79
+ attr_reader :skip_location, :comment_location
80
+
81
+ def initialize(location:, prefix_location:, skip_location:, comment_location:)
82
+ super(location, prefix_location)
83
+ @skip_location = skip_location
84
+ @comment_location = comment_location
85
+ end
86
+ end
87
+
88
+ class ReturnTypeAnnotation < Base
89
+ attr_reader :return_location
90
+
91
+ attr_reader :colon_location
92
+
93
+ attr_reader :return_type
94
+
95
+ attr_reader :comment_location
96
+
97
+ def initialize(location:, prefix_location:, return_location:, colon_location:, return_type:, comment_location:)
98
+ super(location, prefix_location)
99
+ @return_location = return_location
100
+ @colon_location = colon_location
101
+ @return_type = return_type
102
+ @comment_location = comment_location
103
+ end
104
+
105
+ def map_type_name(&block)
106
+ self.class.new(
107
+ location:,
108
+ prefix_location:,
109
+ return_location: return_location,
110
+ colon_location: colon_location,
111
+ return_type: return_type.map_type_name { yield _1 },
112
+ comment_location: comment_location
113
+ ) #: self
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,221 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RBS
4
+ module AST
5
+ module Ruby
6
+ class CommentBlock
7
+ attr_reader :name, :offsets, :comment_buffer
8
+
9
+ def initialize(source_buffer, comments)
10
+ @name = source_buffer.name
11
+
12
+ @offsets = []
13
+
14
+ # Assume the comment starts with a prefix whitespace
15
+ prefix_str = "# "
16
+
17
+ ranges = [] #: Array[Range[Integer]]
18
+
19
+ comments.each do |comment|
20
+ tuple = [comment, 2] #: [Prism::Comment, Integer]
21
+
22
+ unless comment.location.slice.start_with?(prefix_str)
23
+ tuple[1] = 1
24
+ end
25
+
26
+ offsets << tuple
27
+
28
+ start_char = comment.location.start_character_offset + tuple[1]
29
+ end_char = comment.location.end_character_offset
30
+ ranges << (start_char ... end_char)
31
+ end
32
+
33
+ @comment_buffer = source_buffer.sub_buffer(lines: ranges)
34
+ end
35
+
36
+ def leading?
37
+ comment = offsets[0][0] or raise
38
+ comment.location.start_line_slice.index(/\S/) ? false : true
39
+ end
40
+
41
+ def trailing?
42
+ comment = offsets[0][0] or raise
43
+ comment.location.start_line_slice.index(/\S/) ? true : false
44
+ end
45
+
46
+ def start_line
47
+ comments[0].location.start_line
48
+ end
49
+
50
+ def end_line
51
+ comments[-1].location.end_line
52
+ end
53
+
54
+ def line_starts
55
+ offsets.map do |comment, prefix_size|
56
+ comment.location.start_character_offset + prefix_size
57
+ end
58
+ end
59
+
60
+ def self.build(buffer, comments)
61
+ blocks = [] #: Array[CommentBlock]
62
+
63
+ comments = comments.filter {|comment| comment.is_a?(Prism::InlineComment) }
64
+
65
+ until comments.empty?
66
+ block_comments = [] #: Array[Prism::Comment]
67
+
68
+ until comments.empty?
69
+ comment = comments.first or raise
70
+ last_comment = block_comments.last
71
+
72
+ if last_comment
73
+ if last_comment.location.end_line + 1 == comment.location.start_line
74
+ if last_comment.location.start_column == comment.location.start_column
75
+ unless comment.location.start_line_slice.index(/\S/)
76
+ block_comments << comments.shift
77
+ next
78
+ end
79
+ end
80
+ end
81
+
82
+ break
83
+ else
84
+ block_comments << comments.shift
85
+ end
86
+ end
87
+
88
+ unless block_comments.empty?
89
+ blocks << CommentBlock.new(buffer, block_comments.dup)
90
+ end
91
+ end
92
+
93
+ blocks
94
+ end
95
+
96
+ AnnotationSyntaxError = _ = Struct.new(:location, :error)
97
+
98
+ def each_paragraph(variables, &block)
99
+ if block
100
+ if leading_annotation?(0)
101
+ yield_annotation(0, 0, 0, variables, &block)
102
+ else
103
+ yield_paragraph(0, 0, variables, &block)
104
+ end
105
+ else
106
+ enum_for :each_paragraph, variables
107
+ end
108
+ end
109
+
110
+ def yield_paragraph(start_line, current_line, variables, &block)
111
+ # We already know at start_line..current_line are paragraph.
112
+
113
+ while true
114
+ next_line = current_line + 1
115
+
116
+ if next_line >= comment_buffer.line_count
117
+ yield line_location(start_line, current_line)
118
+ return
119
+ end
120
+
121
+ if leading_annotation?(next_line)
122
+ yield line_location(start_line, current_line)
123
+ return yield_annotation(next_line, next_line, next_line, variables, &block)
124
+ else
125
+ current_line = next_line
126
+ end
127
+ end
128
+ end
129
+
130
+ def yield_annotation(start_line, end_line, current_line, variables, &block)
131
+ # We already know at start_line..end_line are annotation.
132
+ while true
133
+ next_line = current_line + 1
134
+
135
+ if next_line >= comment_buffer.line_count
136
+ annotation = parse_annotation_lines(start_line, end_line, variables)
137
+ yield annotation
138
+
139
+ if end_line > current_line
140
+ yield_paragraph(end_line + 1, end_line + 1, variables, &block)
141
+ end
142
+
143
+ return
144
+ end
145
+
146
+ line_text = text(next_line)
147
+ if leading_spaces = line_text.index(/\S/)
148
+ if leading_spaces == 0
149
+ # End of annotation
150
+ yield parse_annotation_lines(start_line, end_line, variables)
151
+
152
+ if leading_annotation?(end_line + 1)
153
+ yield_annotation(end_line + 1, end_line + 1, end_line + 1, variables, &block)
154
+ else
155
+ yield_paragraph(end_line + 1, end_line + 1, variables, &block)
156
+ end
157
+
158
+ return
159
+ else
160
+ current_line = next_line
161
+ end_line = next_line
162
+ end
163
+ else
164
+ current_line = next_line
165
+ end
166
+ end
167
+ end
168
+
169
+ def text(comment_index)
170
+ range = comment_buffer.ranges[comment_index]
171
+ comment_buffer.content[range] or raise
172
+ end
173
+
174
+ def line_location(start_line, end_line)
175
+ start_offset = comment_buffer.ranges[start_line].begin
176
+ end_offset = comment_buffer.ranges[end_line].end
177
+ Location.new(comment_buffer, start_offset, end_offset)
178
+ end
179
+
180
+ def parse_annotation_lines(start_line, end_line, variables)
181
+ start_pos = comment_buffer.ranges[start_line].begin
182
+ end_pos = comment_buffer.ranges[end_line].end
183
+ begin
184
+ Parser.parse_inline_leading_annotation(comment_buffer, start_pos...end_pos, variables: variables)
185
+ rescue ParsingError => error
186
+ AnnotationSyntaxError.new(line_location(start_line, end_line), error)
187
+ end
188
+ end
189
+
190
+ def trailing_annotation(variables)
191
+ if trailing?
192
+ comment = comments[0] or raise
193
+ if comment.location.slice.start_with?(/#[:\[]/)
194
+ begin
195
+ Parser.parse_inline_trailing_annotation(comment_buffer, 0...comment_buffer.last_position, variables: variables)
196
+ rescue ParsingError => error
197
+ location = line_location(0, offsets.size - 1)
198
+ AnnotationSyntaxError.new(location, error)
199
+ end
200
+ end
201
+ end
202
+ end
203
+
204
+ def comments
205
+ offsets.map { _1[0]}
206
+ end
207
+
208
+ def leading_annotation?(index)
209
+ if index < comment_buffer.line_count
210
+ text(index).start_with?(/@rbs\b/) and return true
211
+
212
+ comment = offsets[index][0]
213
+ comment.location.slice.start_with?(/\#:/) and return true
214
+ end
215
+
216
+ false
217
+ end
218
+ end
219
+ end
220
+ end
221
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RBS
4
+ module AST
5
+ module Ruby
6
+ module Declarations
7
+ class Base
8
+ attr_reader :buffer
9
+
10
+ include Helpers::ConstantHelper
11
+ include Helpers::LocationHelper
12
+
13
+ def initialize(buffer)
14
+ @buffer = buffer
15
+ end
16
+ end
17
+
18
+ class ClassDecl < Base
19
+ attr_reader :class_name
20
+
21
+ attr_reader :members
22
+
23
+ attr_reader :node
24
+
25
+ def initialize(buffer, name, node)
26
+ super(buffer)
27
+ @class_name = name
28
+ @node = node
29
+ @members = []
30
+ end
31
+
32
+ def each_decl(&block)
33
+ return enum_for(:each_decl) unless block
34
+
35
+ @members.each do |member|
36
+ if member.is_a?(Base)
37
+ yield member
38
+ end
39
+ end
40
+ end
41
+
42
+ def super_class = nil
43
+
44
+ def type_params = []
45
+
46
+ def location
47
+ rbs_location(node.location)
48
+ end
49
+ end
50
+
51
+ class ModuleDecl < Base
52
+ attr_reader :module_name
53
+
54
+ attr_reader :members
55
+
56
+ attr_reader :node
57
+
58
+ def initialize(buffer, name, node)
59
+ super(buffer)
60
+ @module_name = name
61
+ @node = node
62
+ @members = []
63
+ end
64
+
65
+ def each_decl(&block)
66
+ return enum_for(:each_decl) unless block
67
+
68
+ @members.each do |member|
69
+ if member.is_a?(Base)
70
+ yield member
71
+ end
72
+ end
73
+ end
74
+
75
+ def type_params = []
76
+
77
+ def self_types = []
78
+
79
+ def location
80
+ rbs_location(node.location)
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end