rbs 1.6.1 → 1.7.0.beta.3

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 (70) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +18 -3
  3. data/.gitignore +10 -1
  4. data/CHANGELOG.md +25 -0
  5. data/Gemfile +1 -0
  6. data/Rakefile +22 -22
  7. data/core/enumerator.rbs +1 -0
  8. data/core/io.rbs +1 -1
  9. data/core/kernel.rbs +4 -4
  10. data/core/trace_point.rbs +1 -1
  11. data/ext/rbs_extension/constants.c +139 -0
  12. data/ext/rbs_extension/constants.h +72 -0
  13. data/ext/rbs_extension/extconf.rb +3 -0
  14. data/ext/rbs_extension/lexer.c +2533 -0
  15. data/ext/rbs_extension/lexer.h +161 -0
  16. data/ext/rbs_extension/lexer.re +140 -0
  17. data/ext/rbs_extension/lexstate.c +139 -0
  18. data/ext/rbs_extension/location.c +295 -0
  19. data/ext/rbs_extension/location.h +59 -0
  20. data/ext/rbs_extension/main.c +9 -0
  21. data/ext/rbs_extension/parser.c +2390 -0
  22. data/ext/rbs_extension/parser.h +18 -0
  23. data/ext/rbs_extension/parserstate.c +313 -0
  24. data/ext/rbs_extension/parserstate.h +141 -0
  25. data/ext/rbs_extension/rbs_extension.h +40 -0
  26. data/ext/rbs_extension/ruby_objs.c +521 -0
  27. data/ext/rbs_extension/ruby_objs.h +46 -0
  28. data/ext/rbs_extension/unescape.c +65 -0
  29. data/goodcheck.yml +1 -1
  30. data/lib/rbs/ast/comment.rb +0 -12
  31. data/lib/rbs/buffer.rb +4 -0
  32. data/lib/rbs/cli.rb +5 -8
  33. data/lib/rbs/collection/installer.rb +1 -0
  34. data/lib/rbs/collection/sources/git.rb +18 -3
  35. data/lib/rbs/errors.rb +28 -1
  36. data/lib/rbs/location.rb +221 -217
  37. data/lib/rbs/location_aux.rb +121 -0
  38. data/lib/rbs/locator.rb +10 -7
  39. data/lib/rbs/parser_aux.rb +63 -0
  40. data/lib/rbs/parser_compat/lexer_error.rb +4 -0
  41. data/lib/rbs/parser_compat/located_value.rb +5 -0
  42. data/lib/rbs/parser_compat/semantics_error.rb +4 -0
  43. data/lib/rbs/parser_compat/syntax_error.rb +4 -0
  44. data/lib/rbs/types.rb +2 -3
  45. data/lib/rbs/version.rb +1 -1
  46. data/lib/rbs/writer.rb +4 -2
  47. data/lib/rbs.rb +14 -7
  48. data/rbs.gemspec +2 -1
  49. data/sig/ancestor_builder.rbs +2 -2
  50. data/sig/annotation.rbs +2 -2
  51. data/sig/comment.rbs +7 -7
  52. data/sig/constant_table.rbs +1 -1
  53. data/sig/declarations.rbs +9 -9
  54. data/sig/definition.rbs +1 -1
  55. data/sig/definition_builder.rbs +2 -2
  56. data/sig/errors.rbs +40 -25
  57. data/sig/location.rbs +46 -78
  58. data/sig/locator.rbs +2 -2
  59. data/sig/members.rbs +7 -7
  60. data/sig/method_types.rbs +3 -3
  61. data/sig/parser.rbs +15 -20
  62. data/sig/rbs.rbs +4 -0
  63. data/sig/types.rbs +45 -27
  64. data/sig/writer.rbs +1 -1
  65. data/stdlib/io-console/0/io-console.rbs +137 -0
  66. data/stdlib/json/0/json.rbs +3 -3
  67. data/stdlib/net-http/0/net-http.rbs +2 -1
  68. data/stdlib/tempfile/0/tempfile.rbs +4 -6
  69. metadata +32 -7
  70. data/lib/rbs/parser.rb +0 -3614
@@ -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
@@ -0,0 +1,9 @@
1
+ #include "rbs_extension.h"
2
+
3
+ void
4
+ Init_rbs_extension(void)
5
+ {
6
+ rbs__init_constants();
7
+ rbs__init_location();
8
+ rbs__init_parser();
9
+ }