rbs 1.6.2 → 1.7.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +0 -4
  3. data/.gitignore +1 -0
  4. data/CHANGELOG.md +6 -0
  5. data/Gemfile +1 -0
  6. data/Rakefile +7 -22
  7. data/core/kernel.rbs +4 -4
  8. data/core/trace_point.rbs +1 -1
  9. data/ext/rbs/extension/constants.c +140 -0
  10. data/ext/rbs/extension/constants.h +72 -0
  11. data/ext/rbs/extension/extconf.rb +3 -0
  12. data/ext/rbs/extension/lexer.c +1070 -0
  13. data/ext/rbs/extension/lexer.h +145 -0
  14. data/ext/rbs/extension/location.c +295 -0
  15. data/ext/rbs/extension/location.h +59 -0
  16. data/ext/rbs/extension/main.c +9 -0
  17. data/ext/rbs/extension/parser.c +2418 -0
  18. data/ext/rbs/extension/parser.h +23 -0
  19. data/ext/rbs/extension/parserstate.c +313 -0
  20. data/ext/rbs/extension/parserstate.h +141 -0
  21. data/ext/rbs/extension/rbs_extension.h +40 -0
  22. data/ext/rbs/extension/ruby_objs.c +585 -0
  23. data/ext/rbs/extension/ruby_objs.h +46 -0
  24. data/ext/rbs/extension/unescape.c +65 -0
  25. data/goodcheck.yml +1 -1
  26. data/lib/rbs/ast/comment.rb +0 -12
  27. data/lib/rbs/buffer.rb +4 -0
  28. data/lib/rbs/cli.rb +5 -8
  29. data/lib/rbs/collection/sources/git.rb +18 -3
  30. data/lib/rbs/errors.rb +14 -1
  31. data/lib/rbs/location.rb +221 -217
  32. data/lib/rbs/location_aux.rb +108 -0
  33. data/lib/rbs/locator.rb +10 -7
  34. data/lib/rbs/parser_aux.rb +24 -0
  35. data/lib/rbs/types.rb +2 -3
  36. data/lib/rbs/version.rb +1 -1
  37. data/lib/rbs/writer.rb +4 -2
  38. data/lib/rbs.rb +3 -7
  39. data/rbs.gemspec +2 -1
  40. data/sig/ancestor_builder.rbs +2 -2
  41. data/sig/annotation.rbs +2 -2
  42. data/sig/comment.rbs +7 -7
  43. data/sig/constant_table.rbs +1 -1
  44. data/sig/declarations.rbs +9 -9
  45. data/sig/definition.rbs +1 -1
  46. data/sig/definition_builder.rbs +2 -2
  47. data/sig/errors.rbs +30 -25
  48. data/sig/location.rbs +42 -79
  49. data/sig/locator.rbs +2 -2
  50. data/sig/members.rbs +7 -7
  51. data/sig/method_types.rbs +3 -3
  52. data/sig/parser.rbs +11 -21
  53. data/sig/types.rbs +45 -27
  54. data/sig/writer.rbs +1 -1
  55. data/stdlib/json/0/json.rbs +3 -3
  56. metadata +24 -6
  57. data/lib/rbs/parser.rb +0 -3614
@@ -0,0 +1,145 @@
1
+ #ifndef RBS__LEXER_H
2
+ #define RBS__LEXER_H
3
+
4
+ enum TokenType {
5
+ NullType, /* (Nothing) */
6
+ pEOF, /* EOF */
7
+ ErrorToken, /* Error */
8
+
9
+ pLPAREN, /* ( */
10
+ pRPAREN, /* ) */
11
+ pCOLON, /* : */
12
+ pCOLON2, /* :: */
13
+ pLBRACKET, /* [ */
14
+ pRBRACKET, /* ] */
15
+ pLBRACE, /* { */
16
+ pRBRACE, /* } */
17
+ pHAT, /* ^ */
18
+ pARROW, /* -> */
19
+ pFATARROW, /* => */
20
+ pCOMMA, /* , */
21
+ pBAR, /* | */
22
+ pAMP, /* & */
23
+ pSTAR, /* * */
24
+ pSTAR2, /* ** */
25
+ pDOT, /* . */
26
+ pDOT3, /* ... */
27
+ pBANG, /* ! */
28
+ pQUESTION, /* ? */
29
+ pLT, /* < */
30
+ pEQ, /* = */
31
+
32
+ kBOOL, /* bool */
33
+ kBOT, /* bot */
34
+ kCLASS, /* class */
35
+ kFALSE, /* false */
36
+ kINSTANCE, /* instance */
37
+ kINTERFACE, /* interface */
38
+ kNIL, /* nil */
39
+ kSELF, /* self */
40
+ kSINGLETON, /* singleton */
41
+ kTOP, /* top */
42
+ kTRUE, /* true */
43
+ kVOID, /* void */
44
+ kTYPE, /* type */
45
+ kUNCHECKED, /* unchecked */
46
+ kIN, /* in */
47
+ kOUT, /* out */
48
+ kEND, /* end */
49
+ kDEF, /* def */
50
+ kINCLUDE, /* include */
51
+ kEXTEND, /* extend */
52
+ kPREPEND, /* prepend */
53
+ kALIAS, /* alias */
54
+ kMODULE, /* module */
55
+ kATTRREADER, /* attr_reader */
56
+ kATTRWRITER, /* attr_writer */
57
+ kATTRACCESSOR, /* attr_accessor */
58
+ kPUBLIC, /* public */
59
+ kPRIVATE, /* private */
60
+ kUNTYPED, /* untyped */
61
+
62
+ tLIDENT, /* Identifiers starting with lower case */
63
+ tUIDENT, /* Identifiers starting with upper case */
64
+ tULIDENT, /* Identifiers starting with `_` followed by upper case */
65
+ tULLIDENT, /* Identifiers starting with `_` followed by lower case */
66
+ tGIDENT, /* Identifiers starting with `$` */
67
+ tAIDENT, /* Identifiers starting with `@` */
68
+ tA2IDENT, /* Identifiers starting with `@@` */
69
+ tBANGIDENT, /* Identifiers ending with `!` */
70
+ tEQIDENT, /* Identifiers ending with `=` */
71
+ tQIDENT, /* Quoted identifier */
72
+ tOPERATOR, /* Operator identifier */
73
+
74
+ tCOMMENT, /* Comment */
75
+ tLINECOMMENT, /* Comment of all line */
76
+
77
+ tDQSTRING, /* Double quoted string */
78
+ tSQSTRING, /* Single quoted string */
79
+ tINTEGER, /* Integer */
80
+ tSYMBOL, /* Symbol */
81
+ tDQSYMBOL, /* Double quoted symbol */
82
+ tSQSYMBOL, /* Single quoted symbol */
83
+ tANNOTATION, /* Annotation */
84
+ };
85
+
86
+ /**
87
+ * The `byte_pos` (or `char_pos`) is the primary data.
88
+ * The rest are cache.
89
+ *
90
+ * 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).
91
+ * */
92
+ typedef struct {
93
+ int byte_pos;
94
+ int char_pos;
95
+ int line;
96
+ int column;
97
+ } position;
98
+
99
+ typedef struct {
100
+ position start;
101
+ position end;
102
+ } range;
103
+
104
+ typedef struct {
105
+ enum TokenType type;
106
+ range range;
107
+ } token;
108
+
109
+ /**
110
+ * The lexer state is the curren token.
111
+ *
112
+ * ```
113
+ * ... "a string token"
114
+ * ^ start position
115
+ * ^ current position
116
+ * ~~~~~~ Token => "a str
117
+ * ```
118
+ * */
119
+ typedef struct {
120
+ VALUE string;
121
+ position current; /* The current position */
122
+ position start; /* The start position of the current token */
123
+ bool first_token_of_line; /* This flag is used for tLINECOMMENT */
124
+ } lexstate;
125
+
126
+ extern token NullToken;
127
+ extern position NullPosition;
128
+ extern range NULL_RANGE;
129
+
130
+ token rbsparser_next_token(lexstate *state);
131
+
132
+ char *peek_token(lexstate *state, token tok);
133
+ int token_chars(token tok);
134
+ int token_bytes(token tok);
135
+
136
+ #define null_position_p(pos) (pos.byte_pos == -1)
137
+ #define null_range_p(range) (range.start.byte_pos == -1)
138
+ #define nonnull_pos_or(pos1, pos2) (null_position_p(pos1) ? pos2 : pos1)
139
+ #define RANGE_BYTES(range) (range.end.byte_pos - range.start.byte_pos)
140
+
141
+ const char *token_type_str(enum TokenType type);
142
+
143
+ void print_token(token tok);
144
+
145
+ #endif
@@ -0,0 +1,295 @@
1
+ #include "rbs_extension.h"
2
+
3
+ VALUE RBS_Location;
4
+
5
+ rbs_loc_list *rbs_loc_list_add(rbs_loc_list *list, const ID name, const range r) {
6
+ rbs_loc_list *new = malloc(sizeof(rbs_loc_list));
7
+ new->next = list;
8
+ new->name = name;
9
+ new->rg = r;
10
+ return new;
11
+ }
12
+
13
+ rbs_loc_list *rbs_loc_list_dup(rbs_loc_list *list) {
14
+ if (list) {
15
+ return rbs_loc_list_add(rbs_loc_list_dup(list->next), list->name, list->rg);
16
+ } else {
17
+ return NULL;
18
+ }
19
+ }
20
+
21
+ void rbs_loc_list_free(rbs_loc_list *list) {
22
+ while (list) {
23
+ rbs_loc_list *next = list->next;
24
+ free(list);
25
+ list = next;
26
+ }
27
+ }
28
+
29
+ bool rbs_loc_list_find(const rbs_loc_list *list, ID name, range *rg) {
30
+ while (list) {
31
+ if (list->name == name) {
32
+ *rg = list->rg;
33
+ return true;
34
+ }
35
+
36
+ list = list->next;
37
+ }
38
+
39
+ return false;
40
+ }
41
+
42
+ size_t rbs_loc_list_size(const rbs_loc_list *list) {
43
+ size_t size = 0;
44
+
45
+ while (list) {
46
+ size += 1;
47
+ list = list->next;
48
+ }
49
+
50
+ return size;
51
+ }
52
+
53
+ position rbs_loc_position(int char_pos) {
54
+ position pos = { 0, char_pos, -1, -1 };
55
+ return pos;
56
+ }
57
+
58
+ position rbs_loc_position3(int char_pos, int line, int column) {
59
+ position pos = { 0, char_pos, line, column };
60
+ return pos;
61
+ }
62
+
63
+ void rbs_loc_add_required_child(rbs_loc *loc, ID name, range r) {
64
+ loc->requireds = rbs_loc_list_add(loc->requireds, name, r);
65
+ }
66
+
67
+ void rbs_loc_add_optional_child(rbs_loc *loc, ID name, range r) {
68
+ loc->optionals = rbs_loc_list_add(loc->optionals, name, r);
69
+ }
70
+
71
+ void rbs_loc_init(rbs_loc *loc, VALUE buffer, range rg) {
72
+ loc->buffer = buffer;
73
+ loc->rg = rg;
74
+ loc->optionals = NULL;
75
+ loc->requireds = NULL;
76
+ }
77
+
78
+ void rbs_loc_free(rbs_loc *loc) {
79
+ rbs_loc_list_free(loc->optionals);
80
+ rbs_loc_list_free(loc->requireds);
81
+ ruby_xfree(loc);
82
+ }
83
+
84
+ static void rbs_loc_mark(void *ptr)
85
+ {
86
+ rbs_loc *loc = ptr;
87
+ rb_gc_mark(loc->buffer);
88
+ }
89
+
90
+ static size_t rbs_loc_memsize(const void *ptr) {
91
+ const rbs_loc *loc = ptr;
92
+ return sizeof(*loc) + (rbs_loc_list_size(loc->optionals) + rbs_loc_list_size(loc->requireds)) * sizeof(rbs_loc_list);
93
+ }
94
+
95
+ static rb_data_type_t location_type = {
96
+ "RBS::Location",
97
+ {rbs_loc_mark, (RUBY_DATA_FUNC)rbs_loc_free, rbs_loc_memsize},
98
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
99
+ };
100
+
101
+ static VALUE location_s_allocate(VALUE klass) {
102
+ rbs_loc *loc;
103
+ VALUE obj = TypedData_Make_Struct(klass, rbs_loc, &location_type, loc);
104
+
105
+ rbs_loc_init(loc, Qnil, NULL_RANGE);
106
+
107
+ return obj;
108
+ }
109
+
110
+ rbs_loc *rbs_check_location(VALUE obj) {
111
+ return rb_check_typeddata(obj, &location_type);
112
+ }
113
+
114
+ static VALUE location_initialize(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) {
115
+ rbs_loc *loc = rbs_check_location(self);
116
+
117
+ position start = rbs_loc_position(FIX2INT(start_pos));
118
+ position end = rbs_loc_position(FIX2INT(end_pos));
119
+
120
+ loc->buffer = buffer;
121
+ loc->rg.start = start;
122
+ loc->rg.end = end;
123
+
124
+ return Qnil;
125
+ }
126
+
127
+ static VALUE location_initialize_copy(VALUE self, VALUE other) {
128
+ rbs_loc *self_loc = rbs_check_location(self);
129
+ rbs_loc *other_loc = rbs_check_location(other);
130
+
131
+ self_loc->buffer = other_loc->buffer;
132
+ self_loc->rg = other_loc->rg;
133
+ self_loc->requireds = rbs_loc_list_dup(other_loc->requireds);
134
+ self_loc->optionals = rbs_loc_list_dup(other_loc->optionals);
135
+
136
+ return Qnil;
137
+ }
138
+
139
+ static VALUE location_buffer(VALUE self) {
140
+ rbs_loc *loc = rbs_check_location(self);
141
+ return loc->buffer;
142
+ }
143
+
144
+ static VALUE location_start_pos(VALUE self) {
145
+ rbs_loc *loc = rbs_check_location(self);
146
+ return INT2FIX(loc->rg.start.char_pos);
147
+ }
148
+
149
+ static VALUE location_end_pos(VALUE self) {
150
+ rbs_loc *loc = rbs_check_location(self);
151
+ return INT2FIX(loc->rg.end.char_pos);
152
+ }
153
+
154
+ static VALUE location_start_loc(VALUE self) {
155
+ rbs_loc *loc = rbs_check_location(self);
156
+
157
+ if (loc->rg.start.line >= 0) {
158
+ VALUE pair = rb_ary_new_capa(2);
159
+ rb_ary_push(pair, INT2FIX(loc->rg.start.line));
160
+ rb_ary_push(pair, INT2FIX(loc->rg.start.column));
161
+ return pair;
162
+ } else {
163
+ return Qnil;
164
+ }
165
+ }
166
+
167
+ static VALUE location_end_loc(VALUE self) {
168
+ rbs_loc *loc = rbs_check_location(self);
169
+
170
+ if (loc->rg.end.line >= 0) {
171
+ VALUE pair = rb_ary_new_capa(2);
172
+ rb_ary_push(pair, INT2FIX(loc->rg.end.line));
173
+ rb_ary_push(pair, INT2FIX(loc->rg.end.column));
174
+ return pair;
175
+ } else {
176
+ return Qnil;
177
+ }
178
+ }
179
+
180
+ static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VALUE end) {
181
+ rbs_loc *loc = rbs_check_location(self);
182
+
183
+ range rg;
184
+ rg.start = rbs_loc_position(FIX2INT(start));
185
+ rg.end = rbs_loc_position(FIX2INT(end));
186
+
187
+ rbs_loc_add_required_child(loc, SYM2ID(name), rg);
188
+
189
+ return Qnil;
190
+ }
191
+
192
+ static VALUE location_add_optional_child(VALUE self, VALUE name, VALUE start, VALUE end) {
193
+ rbs_loc *loc = rbs_check_location(self);
194
+
195
+ range rg;
196
+ rg.start = rbs_loc_position(FIX2INT(start));
197
+ rg.end = rbs_loc_position(FIX2INT(end));
198
+
199
+ rbs_loc_add_optional_child(loc, SYM2ID(name), rg);
200
+
201
+ return Qnil;
202
+ }
203
+
204
+ static VALUE location_add_optional_no_child(VALUE self, VALUE name) {
205
+ rbs_loc *loc = rbs_check_location(self);
206
+
207
+ rbs_loc_add_optional_child(loc, SYM2ID(name), NULL_RANGE);
208
+
209
+ return Qnil;
210
+ }
211
+
212
+ VALUE rbs_new_location(VALUE buffer, range rg) {
213
+ rbs_loc *loc;
214
+ VALUE obj = TypedData_Make_Struct(RBS_Location, rbs_loc, &location_type, loc);
215
+
216
+ rbs_loc_init(loc, buffer, rg);
217
+
218
+ return obj;
219
+ }
220
+
221
+ static VALUE location_aref(VALUE self, VALUE name) {
222
+ rbs_loc *loc = rbs_check_location(self);
223
+
224
+ range result;
225
+ ID id = SYM2ID(name);
226
+
227
+ if (rbs_loc_list_find(loc->requireds, id, &result)) {
228
+ return rbs_new_location(loc->buffer, result);
229
+ }
230
+
231
+ if (rbs_loc_list_find(loc->optionals, id, &result)) {
232
+ if (null_range_p(result)) {
233
+ return Qnil;
234
+ } else {
235
+ return rbs_new_location(loc->buffer, result);
236
+ }
237
+ }
238
+
239
+ VALUE string = rb_funcall(name, rb_intern("to_s"), 0);
240
+ rb_raise(rb_eRuntimeError, "Unknown child name given: %s", RSTRING_PTR(string));
241
+ }
242
+
243
+ static VALUE location_optional_keys(VALUE self) {
244
+ VALUE keys = rb_ary_new();
245
+
246
+ rbs_loc *loc = rbs_check_location(self);
247
+ rbs_loc_list *list = loc->optionals;
248
+
249
+ while (list) {
250
+ rb_ary_push(keys, ID2SYM(list->name));
251
+ list = list->next;
252
+ }
253
+
254
+ return keys;
255
+ }
256
+
257
+ static VALUE location_required_keys(VALUE self) {
258
+ VALUE keys = rb_ary_new();
259
+
260
+ rbs_loc *loc = rbs_check_location(self);
261
+ rbs_loc_list *list = loc->requireds;
262
+
263
+ while (list) {
264
+ rb_ary_push(keys, ID2SYM(list->name));
265
+ list = list->next;
266
+ }
267
+
268
+ return keys;
269
+ }
270
+
271
+ VALUE rbs_location_pp(VALUE buffer, const position *start_pos, const position *end_pos) {
272
+ range rg = { *start_pos, *end_pos };
273
+ rg.start = *start_pos;
274
+ rg.end = *end_pos;
275
+
276
+ return rbs_new_location(buffer, rg);
277
+ }
278
+
279
+ void rbs__init_location() {
280
+ RBS_Location = rb_define_class_under(RBS, "Location", rb_cObject);
281
+ rb_define_alloc_func(RBS_Location, location_s_allocate);
282
+ rb_define_private_method(RBS_Location, "initialize", location_initialize, 3);
283
+ rb_define_private_method(RBS_Location, "initialize_copy", location_initialize_copy, 1);
284
+ rb_define_method(RBS_Location, "buffer", location_buffer, 0);
285
+ rb_define_method(RBS_Location, "start_pos", location_start_pos, 0);
286
+ rb_define_method(RBS_Location, "end_pos", location_end_pos, 0);
287
+ rb_define_private_method(RBS_Location, "_start_loc", location_start_loc, 0);
288
+ rb_define_private_method(RBS_Location, "_end_loc", location_end_loc, 0);
289
+ rb_define_method(RBS_Location, "_add_required_child", location_add_required_child, 3);
290
+ rb_define_method(RBS_Location, "_add_optional_child", location_add_optional_child, 3);
291
+ rb_define_method(RBS_Location, "_add_optional_no_child", location_add_optional_no_child, 1);
292
+ rb_define_method(RBS_Location, "_optional_keys", location_optional_keys, 0);
293
+ rb_define_method(RBS_Location, "_required_keys", location_required_keys, 0);
294
+ rb_define_method(RBS_Location, "[]", location_aref, 1);
295
+ }
@@ -0,0 +1,59 @@
1
+ #ifndef RBS_LOCATION_H
2
+ #define RBS_LOCATION_H
3
+
4
+ #include "ruby.h"
5
+ #include "lexer.h"
6
+
7
+ /**
8
+ * RBS::Location class
9
+ * */
10
+ extern VALUE RBS_Location;
11
+
12
+ typedef struct rbs_loc_list {
13
+ ID name;
14
+ range rg;
15
+ struct rbs_loc_list *next;
16
+ } rbs_loc_list;
17
+
18
+ typedef struct {
19
+ VALUE buffer;
20
+ range rg;
21
+ rbs_loc_list *requireds;
22
+ rbs_loc_list *optionals;
23
+ } rbs_loc;
24
+
25
+ /**
26
+ * Returns new RBS::Location object, with given buffer and range.
27
+ * */
28
+ VALUE rbs_new_location(VALUE buffer, range rg);
29
+
30
+ /**
31
+ * Return rbs_loc assiciated with the RBS::Location object.
32
+ * */
33
+ rbs_loc *rbs_check_location(VALUE location);
34
+
35
+ /**
36
+ * Add a required child range with given name.
37
+ * */
38
+ void rbs_loc_add_required_child(rbs_loc *loc, ID name, range r);
39
+
40
+ /**
41
+ * Add an optional child range with given name.
42
+ * */
43
+ void rbs_loc_add_optional_child(rbs_loc *loc, ID name, range r);
44
+
45
+ /**
46
+ * Returns RBS::Location object with start/end positions.
47
+ *
48
+ * @param start_pos
49
+ * @param end_pos
50
+ * @return New RSS::Location object.
51
+ * */
52
+ VALUE rbs_location_pp(VALUE buffer, const position *start_pos, const position *end_pos);
53
+
54
+ /**
55
+ * Define RBS::Location class.
56
+ * */
57
+ void rbs__init_location();
58
+
59
+ #endif