rbs 3.9.5 → 3.10.0.pre.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 (171) hide show
  1. checksums.yaml +4 -4
  2. data/.clang-format +74 -0
  3. data/.clangd +2 -0
  4. data/.github/workflows/c-check.yml +54 -0
  5. data/.github/workflows/comments.yml +3 -3
  6. data/.github/workflows/ruby.yml +34 -19
  7. data/.github/workflows/typecheck.yml +1 -1
  8. data/.github/workflows/windows.yml +1 -1
  9. data/.gitignore +4 -0
  10. data/README.md +38 -1
  11. data/Rakefile +152 -23
  12. data/config.yml +190 -62
  13. data/core/array.rbs +44 -43
  14. data/core/dir.rbs +2 -2
  15. data/core/encoding.rbs +3 -2
  16. data/core/enumerable.rbs +89 -2
  17. data/core/errno.rbs +8 -0
  18. data/core/errors.rbs +28 -1
  19. data/core/exception.rbs +2 -2
  20. data/core/fiber.rbs +3 -3
  21. data/core/file.rbs +26 -11
  22. data/core/float.rbs +1 -1
  23. data/core/gc.rbs +422 -281
  24. data/core/hash.rbs +1024 -727
  25. data/core/io/wait.rbs +11 -33
  26. data/core/io.rbs +6 -4
  27. data/core/kernel.rbs +49 -43
  28. data/core/marshal.rbs +1 -1
  29. data/core/match_data.rbs +1 -1
  30. data/core/math.rbs +42 -3
  31. data/core/method.rbs +14 -6
  32. data/core/module.rbs +71 -11
  33. data/core/nil_class.rbs +3 -3
  34. data/core/numeric.rbs +8 -8
  35. data/core/object.rbs +3 -3
  36. data/core/object_space.rbs +13 -0
  37. data/{stdlib/pathname/0 → core}/pathname.rbs +253 -352
  38. data/core/proc.rbs +15 -8
  39. data/core/process.rbs +2 -2
  40. data/core/ractor.rbs +278 -437
  41. data/core/range.rbs +6 -7
  42. data/core/rbs/unnamed/argf.rbs +1 -1
  43. data/core/rbs/unnamed/env_class.rbs +1 -1
  44. data/core/rbs/unnamed/random.rbs +4 -2
  45. data/core/regexp.rbs +22 -17
  46. data/core/ruby_vm.rbs +6 -4
  47. data/core/rubygems/errors.rbs +3 -70
  48. data/core/rubygems/rubygems.rbs +11 -79
  49. data/core/set.rbs +439 -332
  50. data/core/string.rbs +2897 -1117
  51. data/core/struct.rbs +1 -1
  52. data/core/symbol.rbs +4 -4
  53. data/core/thread.rbs +83 -20
  54. data/core/time.rbs +35 -9
  55. data/core/unbound_method.rbs +14 -6
  56. data/docs/aliases.md +79 -0
  57. data/docs/collection.md +2 -2
  58. data/docs/gem.md +0 -1
  59. data/docs/sigs.md +3 -3
  60. data/ext/rbs_extension/ast_translation.c +1016 -0
  61. data/ext/rbs_extension/ast_translation.h +37 -0
  62. data/ext/rbs_extension/class_constants.c +157 -0
  63. data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +7 -1
  64. data/ext/rbs_extension/compat.h +10 -0
  65. data/ext/rbs_extension/extconf.rb +25 -1
  66. data/ext/rbs_extension/legacy_location.c +317 -0
  67. data/ext/rbs_extension/legacy_location.h +45 -0
  68. data/ext/rbs_extension/main.c +365 -14
  69. data/ext/rbs_extension/rbs_extension.h +6 -21
  70. data/ext/rbs_extension/rbs_string_bridging.c +9 -0
  71. data/ext/rbs_extension/rbs_string_bridging.h +24 -0
  72. data/include/rbs/ast.h +687 -0
  73. data/include/rbs/defines.h +86 -0
  74. data/include/rbs/lexer.h +199 -0
  75. data/include/rbs/location.h +59 -0
  76. data/include/rbs/parser.h +135 -0
  77. data/include/rbs/string.h +49 -0
  78. data/include/rbs/util/rbs_allocator.h +59 -0
  79. data/include/rbs/util/rbs_assert.h +20 -0
  80. data/include/rbs/util/rbs_buffer.h +83 -0
  81. data/include/rbs/util/rbs_constant_pool.h +6 -67
  82. data/include/rbs/util/rbs_encoding.h +282 -0
  83. data/include/rbs/util/rbs_unescape.h +23 -0
  84. data/include/rbs.h +1 -2
  85. data/lib/rbs/annotate/formatter.rb +3 -13
  86. data/lib/rbs/annotate/rdoc_annotator.rb +3 -1
  87. data/lib/rbs/annotate/rdoc_source.rb +1 -1
  88. data/lib/rbs/cli/validate.rb +2 -2
  89. data/lib/rbs/cli.rb +1 -1
  90. data/lib/rbs/collection/config/lockfile_generator.rb +1 -0
  91. data/lib/rbs/definition_builder/ancestor_builder.rb +5 -5
  92. data/lib/rbs/environment.rb +64 -59
  93. data/lib/rbs/environment_loader.rb +1 -1
  94. data/lib/rbs/errors.rb +1 -1
  95. data/lib/rbs/parser_aux.rb +5 -0
  96. data/lib/rbs/resolver/constant_resolver.rb +2 -2
  97. data/lib/rbs/resolver/type_name_resolver.rb +124 -38
  98. data/lib/rbs/test/type_check.rb +13 -0
  99. data/lib/rbs/types.rb +3 -1
  100. data/lib/rbs/version.rb +1 -1
  101. data/lib/rbs.rb +1 -1
  102. data/lib/rdoc/discover.rb +1 -1
  103. data/lib/rdoc_plugin/parser.rb +3 -3
  104. data/sig/annotate/formatter.rbs +2 -2
  105. data/sig/annotate/rdoc_annotater.rbs +1 -1
  106. data/sig/environment.rbs +57 -6
  107. data/sig/manifest.yaml +0 -1
  108. data/sig/parser.rbs +20 -0
  109. data/sig/resolver/type_name_resolver.rbs +38 -7
  110. data/sig/types.rbs +4 -1
  111. data/src/ast.c +1256 -0
  112. data/src/lexer.c +2956 -0
  113. data/src/lexer.re +147 -0
  114. data/src/lexstate.c +205 -0
  115. data/src/location.c +71 -0
  116. data/src/parser.c +3495 -0
  117. data/src/string.c +90 -0
  118. data/src/util/rbs_allocator.c +152 -0
  119. data/src/util/rbs_assert.c +21 -0
  120. data/src/util/rbs_buffer.c +54 -0
  121. data/src/util/rbs_constant_pool.c +16 -86
  122. data/src/util/rbs_encoding.c +21308 -0
  123. data/src/util/rbs_unescape.c +131 -0
  124. data/stdlib/cgi/0/core.rbs +2 -396
  125. data/stdlib/cgi/0/manifest.yaml +1 -0
  126. data/stdlib/cgi-escape/0/escape.rbs +153 -0
  127. data/stdlib/coverage/0/coverage.rbs +3 -1
  128. data/stdlib/delegate/0/delegator.rbs +10 -7
  129. data/stdlib/erb/0/erb.rbs +737 -347
  130. data/stdlib/fileutils/0/fileutils.rbs +18 -13
  131. data/stdlib/forwardable/0/forwardable.rbs +3 -0
  132. data/stdlib/json/0/json.rbs +67 -48
  133. data/stdlib/net-http/0/net-http.rbs +3 -0
  134. data/stdlib/objspace/0/objspace.rbs +8 -3
  135. data/stdlib/open-uri/0/open-uri.rbs +40 -0
  136. data/stdlib/openssl/0/openssl.rbs +182 -149
  137. data/stdlib/optparse/0/optparse.rbs +3 -3
  138. data/stdlib/rdoc/0/code_object.rbs +2 -2
  139. data/stdlib/rdoc/0/comment.rbs +2 -0
  140. data/stdlib/rdoc/0/options.rbs +76 -0
  141. data/stdlib/rdoc/0/rdoc.rbs +7 -5
  142. data/stdlib/rdoc/0/store.rbs +1 -1
  143. data/stdlib/resolv/0/resolv.rbs +25 -68
  144. data/stdlib/ripper/0/ripper.rbs +5 -2
  145. data/stdlib/singleton/0/singleton.rbs +3 -0
  146. data/stdlib/socket/0/socket.rbs +13 -1
  147. data/stdlib/socket/0/tcp_socket.rbs +10 -2
  148. data/stdlib/stringio/0/stringio.rbs +412 -80
  149. data/stdlib/strscan/0/string_scanner.rbs +31 -31
  150. data/stdlib/tempfile/0/tempfile.rbs +1 -1
  151. data/stdlib/tsort/0/cyclic.rbs +3 -0
  152. data/stdlib/uri/0/common.rbs +11 -2
  153. data/stdlib/uri/0/file.rbs +1 -1
  154. data/stdlib/uri/0/generic.rbs +16 -15
  155. data/stdlib/uri/0/rfc2396_parser.rbs +6 -7
  156. data/stdlib/zlib/0/zstream.rbs +1 -0
  157. metadata +41 -18
  158. data/ext/rbs_extension/lexer.c +0 -2728
  159. data/ext/rbs_extension/lexer.h +0 -179
  160. data/ext/rbs_extension/lexer.re +0 -147
  161. data/ext/rbs_extension/lexstate.c +0 -175
  162. data/ext/rbs_extension/location.c +0 -325
  163. data/ext/rbs_extension/location.h +0 -85
  164. data/ext/rbs_extension/parser.c +0 -2982
  165. data/ext/rbs_extension/parser.h +0 -18
  166. data/ext/rbs_extension/parserstate.c +0 -411
  167. data/ext/rbs_extension/parserstate.h +0 -163
  168. data/ext/rbs_extension/unescape.c +0 -32
  169. data/include/rbs/ruby_objs.h +0 -72
  170. data/src/constants.c +0 -153
  171. data/src/ruby_objs.c +0 -799
@@ -0,0 +1,86 @@
1
+ /**
2
+ * @file defines.h
3
+ *
4
+ * Macro definitions used throughout the rbs library.
5
+ *
6
+ * This file should be included first by any *.h or *.c in rbs for consistency
7
+ * and to ensure that the macros are defined before they are used.
8
+ */
9
+
10
+ #ifndef RBS_DEFINES_H
11
+ #define RBS_DEFINES_H
12
+
13
+ /***********************************************************************************************************************
14
+ * Copied+modified subset of Prism's `include/prism/defines.h` *
15
+ **********************************************************************************************************************/
16
+
17
+ /**
18
+ * Certain compilers support specifying that a function accepts variadic
19
+ * parameters that look like printf format strings to provide a better developer
20
+ * experience when someone is using the function. This macro does that in a
21
+ * compiler-agnostic way.
22
+ */
23
+ #if defined(__GNUC__)
24
+ #if defined(__MINGW_PRINTF_FORMAT)
25
+ #define RBS_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(__MINGW_PRINTF_FORMAT, string_index, argument_index)))
26
+ #else
27
+ #define RBS_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((format(printf, string_index, argument_index)))
28
+ #endif
29
+ #elif defined(__clang__)
30
+ #define RBS_ATTRIBUTE_FORMAT(string_index, argument_index) __attribute__((__format__(__printf__, string_index, argument_index)))
31
+ #else
32
+ #define RBS_ATTRIBUTE_FORMAT(string_index, argument_index)
33
+ #endif
34
+
35
+ /**
36
+ * Support RBS_LIKELY and RBS_UNLIKELY to help the compiler optimize its
37
+ * branch predication.
38
+ */
39
+ #if defined(__GNUC__) || defined(__clang__)
40
+ /** The compiler should predicate that this branch will be taken. */
41
+ #define RBS_LIKELY(x) __builtin_expect(!!(x), 1)
42
+
43
+ /** The compiler should predicate that this branch will not be taken. */
44
+ #define RBS_UNLIKELY(x) __builtin_expect(!!(x), 0)
45
+ #else
46
+ /** Void because this platform does not support branch prediction hints. */
47
+ #define RBS_LIKELY(x) (x)
48
+
49
+ /** Void because this platform does not support branch prediction hints. */
50
+ #define RBS_UNLIKELY(x) (x)
51
+ #endif
52
+
53
+ /**
54
+ * We use -Wimplicit-fallthrough to guard potentially unintended fall-through between cases of a switch.
55
+ * Use RBS_FALLTHROUGH to explicitly annotate cases where the fallthrough is intentional.
56
+ */
57
+ #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L // C23 or later
58
+ #define RBS_FALLTHROUGH [[fallthrough]];
59
+ #elif defined(__GNUC__) || defined(__clang__)
60
+ #define RBS_FALLTHROUGH __attribute__((fallthrough));
61
+ #elif defined(_MSC_VER)
62
+ #define RBS_FALLTHROUGH __fallthrough;
63
+ #else
64
+ #define RBS_FALLTHROUGH
65
+ #endif
66
+
67
+ /***********************************************************************************************************************
68
+ * Custom defines for RBS *
69
+ **********************************************************************************************************************/
70
+
71
+ #if defined(_MSC_VER)
72
+ #define NODISCARD _Check_return_
73
+ #else
74
+ #define NODISCARD __attribute__((warn_unused_result))
75
+ #endif
76
+
77
+ /**
78
+ * Mark a function or variable as potentially unused to suppress compiler warnings.
79
+ */
80
+ #if defined(__GNUC__) || defined(__clang__)
81
+ #define RBS_ATTRIBUTE_UNUSED __attribute__((unused))
82
+ #else
83
+ #define RBS_ATTRIBUTE_UNUSED
84
+ #endif
85
+
86
+ #endif
@@ -0,0 +1,199 @@
1
+ #ifndef RBS__LEXER_H
2
+ #define RBS__LEXER_H
3
+
4
+ #include "string.h"
5
+ #include "util/rbs_encoding.h"
6
+
7
+ enum RBSTokenType {
8
+ NullType, /* (Nothing) */
9
+ pEOF, /* EOF */
10
+ ErrorToken, /* Error */
11
+
12
+ pLPAREN, /* ( */
13
+ pRPAREN, /* ) */
14
+ pCOLON, /* : */
15
+ pCOLON2, /* :: */
16
+ pLBRACKET, /* [ */
17
+ pRBRACKET, /* ] */
18
+ pLBRACE, /* { */
19
+ pRBRACE, /* } */
20
+ pHAT, /* ^ */
21
+ pARROW, /* -> */
22
+ pFATARROW, /* => */
23
+ pCOMMA, /* , */
24
+ pBAR, /* | */
25
+ pAMP, /* & */
26
+ pSTAR, /* * */
27
+ pSTAR2, /* ** */
28
+ pDOT, /* . */
29
+ pDOT3, /* ... */
30
+ pBANG, /* ! */
31
+ pQUESTION, /* ? */
32
+ pLT, /* < */
33
+ pEQ, /* = */
34
+
35
+ kALIAS, /* alias */
36
+ kATTRACCESSOR, /* attr_accessor */
37
+ kATTRREADER, /* attr_reader */
38
+ kATTRWRITER, /* attr_writer */
39
+ kBOOL, /* bool */
40
+ kBOT, /* bot */
41
+ kCLASS, /* class */
42
+ kDEF, /* def */
43
+ kEND, /* end */
44
+ kEXTEND, /* extend */
45
+ kFALSE, /* false */
46
+ kIN, /* in */
47
+ kINCLUDE, /* include */
48
+ kINSTANCE, /* instance */
49
+ kINTERFACE, /* interface */
50
+ kMODULE, /* module */
51
+ kNIL, /* nil */
52
+ kOUT, /* out */
53
+ kPREPEND, /* prepend */
54
+ kPRIVATE, /* private */
55
+ kPUBLIC, /* public */
56
+ kSELF, /* self */
57
+ kSINGLETON, /* singleton */
58
+ kTOP, /* top */
59
+ kTRUE, /* true */
60
+ kTYPE, /* type */
61
+ kUNCHECKED, /* unchecked */
62
+ kUNTYPED, /* untyped */
63
+ kVOID, /* void */
64
+ kUSE, /* use */
65
+ kAS, /* as */
66
+ k__TODO__, /* __todo__ */
67
+
68
+ tLIDENT, /* Identifiers starting with lower case */
69
+ tUIDENT, /* Identifiers starting with upper case */
70
+ tULIDENT, /* Identifiers starting with `_` followed by upper case */
71
+ tULLIDENT, /* Identifiers starting with `_` followed by lower case */
72
+ tGIDENT, /* Identifiers starting with `$` */
73
+ tAIDENT, /* Identifiers starting with `@` */
74
+ tA2IDENT, /* Identifiers starting with `@@` */
75
+ tBANGIDENT, /* Identifiers ending with `!` */
76
+ tEQIDENT, /* Identifiers ending with `=` */
77
+ tQIDENT, /* Quoted identifier */
78
+ pAREF_OPR, /* [] */
79
+ tOPERATOR, /* Operator identifier */
80
+
81
+ tCOMMENT, /* Comment */
82
+ tLINECOMMENT, /* Comment of all line */
83
+
84
+ tTRIVIA, /* Trivia tokens -- space and new line */
85
+
86
+ tDQSTRING, /* Double quoted string */
87
+ tSQSTRING, /* Single quoted string */
88
+ tINTEGER, /* Integer */
89
+ tSYMBOL, /* Symbol */
90
+ tDQSYMBOL, /* Double quoted symbol */
91
+ tSQSYMBOL, /* Single quoted symbol */
92
+ tANNOTATION, /* Annotation */
93
+ };
94
+
95
+ /**
96
+ * The `byte_pos` (or `char_pos`) is the primary data.
97
+ * The rest are cache.
98
+ *
99
+ * They can be computed from `byte_pos` (or `char_pos`), but it needs full scan from the beginning of the string (depending on the encoding).
100
+ * */
101
+ typedef struct {
102
+ int byte_pos;
103
+ int char_pos;
104
+ int line;
105
+ int column;
106
+ } rbs_position_t;
107
+
108
+ typedef struct {
109
+ rbs_position_t start;
110
+ rbs_position_t end;
111
+ } rbs_range_t;
112
+
113
+ typedef struct {
114
+ enum RBSTokenType type;
115
+ rbs_range_t range;
116
+ } rbs_token_t;
117
+
118
+ /**
119
+ * The lexer state is the curren token.
120
+ *
121
+ * ```
122
+ #. 0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6
123
+ * ... " a s t r i n g t o k e n "
124
+ * ^ start position (0)
125
+ * ^ current position (6)
126
+ * ^ current character ('i', bytes = 1)
127
+ * ~~~~~~~~~~~ Token => "a str
128
+ * ```
129
+ * */
130
+ typedef struct {
131
+ rbs_string_t string;
132
+ int start_pos; /* The character position that defines the start of the input */
133
+ int end_pos; /* The character position that defines the end of the input */
134
+ rbs_position_t current; /* The current position: just before the current_character */
135
+ rbs_position_t start; /* The start position of the current token */
136
+
137
+ unsigned int current_code_point; /* Current character code point */
138
+ size_t current_character_bytes; /* Current character byte length (0 or 1~4) */
139
+
140
+ bool first_token_of_line; /* This flag is used for tLINECOMMENT */
141
+
142
+ const rbs_encoding_t *encoding;
143
+ } rbs_lexer_t;
144
+
145
+ extern const rbs_token_t NullToken;
146
+ extern const rbs_position_t NullPosition;
147
+ extern const rbs_range_t NULL_RANGE;
148
+
149
+ char *rbs_peek_token(rbs_lexer_t *lexer, rbs_token_t tok);
150
+ int rbs_token_chars(rbs_token_t tok);
151
+ int rbs_token_bytes(rbs_token_t tok);
152
+
153
+ #define rbs_null_position_p(pos) (pos.byte_pos == -1)
154
+ #define rbs_null_range_p(range) (range.start.byte_pos == -1)
155
+ #define rbs_nonnull_pos_or(pos1, pos2) (rbs_null_position_p(pos1) ? pos2 : pos1)
156
+ #define RBS_RANGE_BYTES(range) (range.end.byte_pos - range.start.byte_pos)
157
+
158
+ const char *rbs_token_type_str(enum RBSTokenType type);
159
+
160
+ /**
161
+ * Returns the next character.
162
+ * */
163
+ unsigned int rbs_peek(rbs_lexer_t *lexer);
164
+
165
+ /**
166
+ * Advances the current position by one character.
167
+ * */
168
+ void rbs_skip(rbs_lexer_t *lexer);
169
+
170
+ /**
171
+ * Read next character and store the codepoint and byte length to the given pointers.
172
+ *
173
+ * This doesn't update the lexer state.
174
+ * Returns `true` if succeeded, or `false` if reached to EOF.
175
+ * */
176
+ bool rbs_next_char(rbs_lexer_t *lexer, unsigned int *codepoint, size_t *bytes);
177
+
178
+ /**
179
+ * Skip n characters.
180
+ * */
181
+ void rbs_skipn(rbs_lexer_t *lexer, size_t size);
182
+
183
+ /**
184
+ * Return new rbs_token_t with given type.
185
+ * */
186
+ rbs_token_t rbs_next_token(rbs_lexer_t *lexer, enum RBSTokenType type);
187
+
188
+ /**
189
+ * Return new rbs_token_t with EOF type.
190
+ * */
191
+ rbs_token_t rbs_next_eof_token(rbs_lexer_t *lexer);
192
+
193
+ rbs_token_t rbs_lexer_next_token(rbs_lexer_t *lexer);
194
+
195
+ void rbs_print_token(rbs_token_t tok);
196
+
197
+ void rbs_print_lexer(rbs_lexer_t *lexer);
198
+
199
+ #endif
@@ -0,0 +1,59 @@
1
+ #ifndef RBS__RBS_LOCATION_H
2
+ #define RBS__RBS_LOCATION_H
3
+
4
+ #include "lexer.h"
5
+
6
+ #include "rbs/util/rbs_constant_pool.h"
7
+ #include "rbs/util/rbs_allocator.h"
8
+
9
+ typedef struct {
10
+ int start;
11
+ int end;
12
+ } rbs_loc_range;
13
+
14
+ typedef struct {
15
+ rbs_constant_id_t name;
16
+ rbs_loc_range rg;
17
+ } rbs_loc_entry;
18
+
19
+ typedef unsigned int rbs_loc_entry_bitmap;
20
+
21
+ // The flexible array always allocates, but it's okay.
22
+ // This struct is not allocated when the `rbs_loc` doesn't have children.
23
+ typedef struct {
24
+ unsigned short len;
25
+ unsigned short cap;
26
+ rbs_loc_entry_bitmap required_p;
27
+ rbs_loc_entry entries[1];
28
+ } rbs_loc_children;
29
+
30
+ typedef struct rbs_location {
31
+ rbs_range_t rg;
32
+ rbs_loc_children *children;
33
+ } rbs_location_t;
34
+
35
+ typedef struct rbs_location_list_node {
36
+ rbs_location_t *loc;
37
+ struct rbs_location_list_node *next;
38
+ } rbs_location_list_node_t;
39
+
40
+ typedef struct rbs_location_list {
41
+ rbs_allocator_t *allocator;
42
+ rbs_location_list_node_t *head;
43
+ rbs_location_list_node_t *tail;
44
+ size_t length;
45
+ } rbs_location_list_t;
46
+
47
+ void rbs_loc_alloc_children(rbs_allocator_t *, rbs_location_t *loc, size_t capacity);
48
+ void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, rbs_range_t r);
49
+ void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, rbs_range_t r);
50
+
51
+ /**
52
+ * Allocate new rbs_location_t object through the given allocator.
53
+ * */
54
+ rbs_location_t *rbs_location_new(rbs_allocator_t *, rbs_range_t rg);
55
+
56
+ rbs_location_list_t *rbs_location_list_new(rbs_allocator_t *allocator);
57
+ void rbs_location_list_append(rbs_location_list_t *list, rbs_location_t *loc);
58
+
59
+ #endif
@@ -0,0 +1,135 @@
1
+ #ifndef RBS__PARSER_H
2
+ #define RBS__PARSER_H
3
+
4
+ #include "rbs/defines.h"
5
+ #include "rbs/util/rbs_allocator.h"
6
+ #include "rbs/util/rbs_constant_pool.h"
7
+ #include "rbs/lexer.h"
8
+ #include "rbs/ast.h"
9
+
10
+ #include <stdbool.h>
11
+ #include <stddef.h>
12
+
13
+ /**
14
+ * comment represents a sequence of comment lines.
15
+ *
16
+ * # Comment for the method.
17
+ * #
18
+ * # ```rb
19
+ * # object.foo() # Do something
20
+ * # ```
21
+ * #
22
+ * def foo: () -> void
23
+ *
24
+ * A comment object represents the six lines of comments.
25
+ * */
26
+ typedef struct rbs_comment_t {
27
+ rbs_position_t start;
28
+ rbs_position_t end;
29
+
30
+ size_t line_tokens_capacity;
31
+ size_t line_tokens_count;
32
+ rbs_token_t *line_tokens;
33
+
34
+ struct rbs_comment_t *next_comment;
35
+ } rbs_comment_t;
36
+
37
+ typedef struct rbs_error_t {
38
+ char *message;
39
+ rbs_token_t token;
40
+ bool syntax_error;
41
+ } rbs_error_t;
42
+
43
+ /**
44
+ * An RBS parser is a LL(3) parser.
45
+ * */
46
+ typedef struct {
47
+ rbs_lexer_t *rbs_lexer_t;
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 */
53
+
54
+ struct id_table *vars; /* Known type variables */
55
+ rbs_comment_t *last_comment; /* Last read comment */
56
+
57
+ rbs_constant_pool_t constant_pool;
58
+ rbs_allocator_t *allocator;
59
+ rbs_error_t *error;
60
+ } rbs_parser_t;
61
+
62
+ /**
63
+ * Insert new table entry.
64
+ * Setting `reset` inserts a _reset_ entry, which stops searching.
65
+ *
66
+ * ```
67
+ * class Foo[A]
68
+ * ^^^ <= push new table with reset
69
+ * def foo: [B] () -> [A, B]
70
+ * ^^^ <= push new table without reset
71
+ *
72
+ * class Baz[C]
73
+ * ^^^ <= push new table with reset
74
+ * end
75
+ * end
76
+ * ```
77
+ * */
78
+ void rbs_parser_push_typevar_table(rbs_parser_t *parser, bool reset);
79
+
80
+ /**
81
+ * Insert new type variable into the latest table.
82
+ * */
83
+ NODISCARD bool rbs_parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id);
84
+
85
+ /**
86
+ * Allocate new rbs_lexer_t object.
87
+ *
88
+ * ```
89
+ * VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
90
+ * rbs_lexer_new(string, 0, 31) // New rbs_lexer_t with buffer content
91
+ * ```
92
+ * */
93
+ rbs_lexer_t *rbs_lexer_new(rbs_allocator_t *, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos);
94
+
95
+ /**
96
+ * Allocate new rbs_parser_t object.
97
+ *
98
+ * ```
99
+ * rbs_parser_new(buffer, string, encoding, 0, 1);
100
+ * ```
101
+ * */
102
+ rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos);
103
+ void rbs_parser_free(rbs_parser_t *parser);
104
+
105
+ /**
106
+ * Advance one token.
107
+ * */
108
+ void rbs_parser_advance(rbs_parser_t *parser);
109
+
110
+ void rbs_parser_print(rbs_parser_t *parser);
111
+
112
+ /**
113
+ * Returns a RBS::Comment object associated with an subject at `subject_line`.
114
+ *
115
+ * ```rbs
116
+ * # Comment1
117
+ * class Foo # This is the subject line for Comment1
118
+ *
119
+ * # Comment2
120
+ * %a{annotation} # This is the subject line for Comment2
121
+ * def foo: () -> void
122
+ * end
123
+ * ```
124
+ * */
125
+ rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line);
126
+
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
+
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);
131
+ bool rbs_parse_signature(rbs_parser_t *parser, rbs_signature_t **signature);
132
+
133
+ bool rbs_parse_type_params(rbs_parser_t *parser, bool module_type_params, rbs_node_list_t **params);
134
+
135
+ #endif
@@ -0,0 +1,49 @@
1
+ #ifndef RBS__RBS_STRING_H
2
+ #define RBS__RBS_STRING_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdbool.h>
6
+ #include "rbs/util/rbs_allocator.h"
7
+
8
+ typedef struct {
9
+ const char *start;
10
+ const char *end;
11
+ } rbs_string_t;
12
+
13
+ #define RBS_STRING_NULL ((rbs_string_t) { \
14
+ .start = NULL, \
15
+ .end = NULL, \
16
+ })
17
+
18
+ /**
19
+ * Returns a new `rbs_string_t` struct
20
+ */
21
+ rbs_string_t rbs_string_new(const char *start, const char *end);
22
+
23
+ /**
24
+ * Copies a portion of the input string into a new owned string.
25
+ * @param start_inset Number of characters to exclude from the start
26
+ * @param length Number of characters to include
27
+ * @return A new owned string that will be freed when the allocator is freed.
28
+ */
29
+ rbs_string_t rbs_string_copy_slice(rbs_allocator_t *, rbs_string_t *self, size_t start_inset, size_t length);
30
+
31
+ /**
32
+ * Drops the leading and trailing whitespace from the given string, in-place.
33
+ * @returns A new string that provides a view into the original string `self`.
34
+ */
35
+ rbs_string_t rbs_string_strip_whitespace(rbs_string_t *self);
36
+
37
+ /**
38
+ * Returns the length of the string.
39
+ */
40
+ size_t rbs_string_len(const rbs_string_t self);
41
+
42
+ /**
43
+ * Compares two strings for equality.
44
+ */
45
+ bool rbs_string_equal(const rbs_string_t lhs, const rbs_string_t rhs);
46
+
47
+ unsigned int rbs_utf8_string_to_codepoint(const rbs_string_t string);
48
+
49
+ #endif
@@ -0,0 +1,59 @@
1
+ #ifndef RBS_ALLOCATOR_H
2
+ #define RBS_ALLOCATOR_H
3
+
4
+ #include <stddef.h>
5
+
6
+ /* Include stdalign.h for C11 and later for alignof support */
7
+ #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
8
+ #include <stdalign.h>
9
+ #endif
10
+
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;
40
+
41
+ rbs_allocator_t *rbs_allocator_init(void);
42
+ 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);
46
+
47
+ void *rbs_allocator_realloc_impl(rbs_allocator_t *, void *ptr, size_t old_size, size_t new_size, size_t alignment);
48
+
49
+ // 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)))
51
+ // Use this when allocating memory that will be immediately written to in full.
52
+ // 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)))
54
+ // Use this when allocating memory that will NOT be immediately written to in full.
55
+ // 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)))
58
+
59
+ #endif
@@ -0,0 +1,20 @@
1
+ #ifndef RBS_ASSERT_H
2
+ #define RBS_ASSERT_H
3
+
4
+ #include "rbs/defines.h"
5
+ #include <stdbool.h>
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);
19
+
20
+ #endif
@@ -0,0 +1,83 @@
1
+ #ifndef RBS__RBS_BUFFER_H
2
+ #define RBS__RBS_BUFFER_H
3
+
4
+ #include "rbs/util/rbs_allocator.h"
5
+ #include "rbs/string.h"
6
+
7
+ #include <stdbool.h>
8
+ #include <stdlib.h>
9
+ #include <string.h>
10
+
11
+ /**
12
+ * The default capacity of a rbs_buffer_t.
13
+ * If the buffer needs to grow beyond this capacity, it will be doubled.
14
+ */
15
+ #define RBS_BUFFER_DEFAULT_CAPACITY 128
16
+
17
+ /**
18
+ * A rbs_buffer_t is a simple memory buffer that stores data in a contiguous block of memory.
19
+ */
20
+ typedef struct {
21
+ /** The length of the buffer in bytes. */
22
+ size_t length;
23
+
24
+ /** The capacity of the buffer in bytes that has been allocated. */
25
+ size_t capacity;
26
+
27
+ /** A pointer to the start of the buffer. */
28
+ char *value;
29
+ } rbs_buffer_t;
30
+
31
+ /**
32
+ * Initialize a rbs_buffer_t with its default values.
33
+ *
34
+ * @param allocator The allocator to use.
35
+ * @param buffer The buffer to initialize.
36
+ * @returns True if the buffer was initialized successfully, false otherwise.
37
+ */
38
+ bool rbs_buffer_init(rbs_allocator_t *, rbs_buffer_t *buffer);
39
+
40
+ /**
41
+ * Return the value of the buffer.
42
+ *
43
+ * @param buffer The buffer to get the value of.
44
+ * @returns The value of the buffer.
45
+ */
46
+ char *rbs_buffer_value(const rbs_buffer_t *buffer);
47
+
48
+ /**
49
+ * Return the length of the buffer.
50
+ *
51
+ * @param buffer The buffer to get the length of.
52
+ * @returns The length of the buffer.
53
+ */
54
+ size_t rbs_buffer_length(const rbs_buffer_t *buffer);
55
+
56
+ /**
57
+ * Append a C string to the buffer.
58
+ *
59
+ * @param allocator The allocator to use.
60
+ * @param buffer The buffer to append to.
61
+ * @param value The C string to append.
62
+ */
63
+ void rbs_buffer_append_cstr(rbs_allocator_t *, rbs_buffer_t *buffer, const char *value);
64
+
65
+ /**
66
+ * Append a string to the buffer.
67
+ *
68
+ * @param allocator The allocator to use.
69
+ * @param buffer The buffer to append to.
70
+ * @param value The string to append.
71
+ * @param length The length of the string to append.
72
+ */
73
+ void rbs_buffer_append_string(rbs_allocator_t *, rbs_buffer_t *buffer, const char *value, size_t length);
74
+
75
+ /**
76
+ * Convert the buffer to a rbs_string_t.
77
+ *
78
+ * @param buffer The buffer to convert.
79
+ * @returns The converted rbs_string_t.
80
+ */
81
+ rbs_string_t rbs_buffer_to_string(rbs_buffer_t *buffer);
82
+
83
+ #endif