rip-parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +15 -0
  3. data/.gitignore +9 -0
  4. data/.rspec +1 -0
  5. data/.ruby-version +1 -0
  6. data/Gemfile +3 -0
  7. data/LICENSE.md +9 -0
  8. data/README.md +13 -0
  9. data/Rakefile +1 -0
  10. data/bin/console +8 -0
  11. data/bin/setup +8 -0
  12. data/legacy/normalizer.rb +279 -0
  13. data/legacy/parser_spec.rb +999 -0
  14. data/legacy/rules.rb +250 -0
  15. data/legacy/rules_spec.rb +1700 -0
  16. data/rip-parser.gemspec +20 -0
  17. data/source/rip/parser/about.rb +9 -0
  18. data/source/rip/parser/error.rb +36 -0
  19. data/source/rip/parser/grammar.rb +23 -0
  20. data/source/rip/parser/keywords.rb +84 -0
  21. data/source/rip/parser/location.rb +47 -0
  22. data/source/rip/parser/node.rb +115 -0
  23. data/source/rip/parser/rules/assignment.rb +23 -0
  24. data/source/rip/parser/rules/binary_condition.rb +24 -0
  25. data/source/rip/parser/rules/character.rb +40 -0
  26. data/source/rip/parser/rules/class.rb +60 -0
  27. data/source/rip/parser/rules/common.rb +47 -0
  28. data/source/rip/parser/rules/date_time.rb +31 -0
  29. data/source/rip/parser/rules/expression.rb +122 -0
  30. data/source/rip/parser/rules/import.rb +23 -0
  31. data/source/rip/parser/rules/invocation.rb +15 -0
  32. data/source/rip/parser/rules/invocation_index.rb +15 -0
  33. data/source/rip/parser/rules/keyword.rb +12 -0
  34. data/source/rip/parser/rules/lambda.rb +45 -0
  35. data/source/rip/parser/rules/list.rb +18 -0
  36. data/source/rip/parser/rules/map.rb +15 -0
  37. data/source/rip/parser/rules/module.rb +29 -0
  38. data/source/rip/parser/rules/number.rb +21 -0
  39. data/source/rip/parser/rules/pair.rb +13 -0
  40. data/source/rip/parser/rules/property.rb +33 -0
  41. data/source/rip/parser/rules/range.rb +15 -0
  42. data/source/rip/parser/rules/reference.rb +16 -0
  43. data/source/rip/parser/rules/string.rb +41 -0
  44. data/source/rip/parser/rules/unit.rb +17 -0
  45. data/source/rip/parser/rules.rb +7 -0
  46. data/source/rip/parser/utilities/normalizer.rb +638 -0
  47. data/source/rip/parser.rb +24 -0
  48. data/source/rip-parser.rb +1 -0
  49. data/spec/fixtures/syntax_sample.rip +96 -0
  50. data/spec/spec_helper.rb +20 -0
  51. data/spec/support/helpers.rb +57 -0
  52. data/spec/support/parslet.rb +1 -0
  53. data/spec/support/shared_examples.rb +9 -0
  54. data/spec/unit/rip/parser/grammar_spec.rb +8 -0
  55. data/spec/unit/rip/parser/location_spec.rb +89 -0
  56. data/spec/unit/rip/parser/node_spec.rb +157 -0
  57. data/spec/unit/rip/parser/rules/assignment_spec.rb +59 -0
  58. data/spec/unit/rip/parser/rules/binary_condition_spec.rb +41 -0
  59. data/spec/unit/rip/parser/rules/character_spec.rb +29 -0
  60. data/spec/unit/rip/parser/rules/class_spec.rb +181 -0
  61. data/spec/unit/rip/parser/rules/common_spec.rb +88 -0
  62. data/spec/unit/rip/parser/rules/date_time_spec.rb +61 -0
  63. data/spec/unit/rip/parser/rules/expression_spec.rb +95 -0
  64. data/spec/unit/rip/parser/rules/import_spec.rb +64 -0
  65. data/spec/unit/rip/parser/rules/invocation_index_spec.rb +46 -0
  66. data/spec/unit/rip/parser/rules/invocation_spec.rb +46 -0
  67. data/spec/unit/rip/parser/rules/keyword_spec.rb +17 -0
  68. data/spec/unit/rip/parser/rules/lambda_spec.rb +174 -0
  69. data/spec/unit/rip/parser/rules/list_spec.rb +45 -0
  70. data/spec/unit/rip/parser/rules/map_spec.rb +36 -0
  71. data/spec/unit/rip/parser/rules/module_spec.rb +63 -0
  72. data/spec/unit/rip/parser/rules/number_spec.rb +40 -0
  73. data/spec/unit/rip/parser/rules/pair_spec.rb +25 -0
  74. data/spec/unit/rip/parser/rules/property_spec.rb +27 -0
  75. data/spec/unit/rip/parser/rules/range_spec.rb +37 -0
  76. data/spec/unit/rip/parser/rules/reference_spec.rb +43 -0
  77. data/spec/unit/rip/parser/rules/string_spec.rb +166 -0
  78. data/spec/unit/rip/parser/rules/unit_spec.rb +17 -0
  79. data/spec/unit/rip/parser_spec.rb +106 -0
  80. metadata +192 -0
@@ -0,0 +1,638 @@
1
+ require 'parslet'
2
+
3
+ module Rip::Parser::Utilities
4
+ class Normalizer < Parslet::Transform
5
+ def apply(raw_tree, context = nil)
6
+ super(raw_tree, context)
7
+ end
8
+
9
+ def self.apply(origin, raw_tree)
10
+ new.apply(raw_tree, origin: origin).tap do |tree|
11
+ validate_branches(tree, origin)
12
+ validate_leaves(tree, origin)
13
+ end
14
+ end
15
+
16
+ def self.validate_branches(tree, origin)
17
+ case tree
18
+ when Array
19
+ tree.each do |branch|
20
+ validate_branches(branch, origin)
21
+ end
22
+ when Hash, Rip::Parser::Node
23
+ tree.each do |_, branch|
24
+ validate_branches(branch, origin)
25
+ end
26
+
27
+ if tree.key?(:expression_chain)
28
+ shape = tree[:expression_chain].map do |key, value|
29
+ [ key, value.class ]
30
+ end.to_h
31
+ warn shape
32
+ raise Rip::Parser::NormalizeError.new('Unhandled expression_chain node', origin, tree)
33
+ end
34
+ end
35
+ end
36
+
37
+ def self.validate_leaves(tree, origin)
38
+ case tree
39
+ when Array
40
+ tree.each do |branch|
41
+ validate_leaves(branch, origin)
42
+ end
43
+ when Rip::Parser::Node
44
+ tree.each do |_, branch|
45
+ validate_leaves(branch, origin)
46
+ end
47
+ when Parslet::Slice
48
+ warn tree
49
+ raise Rip::Parser::NormalizeError.new('Unconverted parslet slice', origin, tree)
50
+ end
51
+ end
52
+
53
+
54
+ rule(expression_chain: simple(:part)) do |part:, origin:|
55
+ part
56
+ end
57
+
58
+ rule(expression_chain: sequence(:parts)) do |parts:, origin:|
59
+ parts.inject do |base, link|
60
+ case
61
+ when link.property_access? then link.merge(object: base)
62
+ when link.pair_value? then link.merge(type: :pair, key: base)
63
+ when link.range_end? then link.merge(type: :range, start: base)
64
+ when link.invocation? then link.merge(callable: base)
65
+ when link.invocation_index?
66
+ Rip::Parser::Node.new(
67
+ type: :invocation_index,
68
+ callable: {
69
+ type: :property_access,
70
+ object: base,
71
+ property_name: '[]',
72
+ location: link.location
73
+ },
74
+ arguments: link.index_arguments,
75
+ location: link.location
76
+ )
77
+ else
78
+ warn link
79
+ raise Rip::Parser::NormalizeError.new('Unhandled expression link node', origin, link)
80
+ end
81
+ end
82
+ end
83
+
84
+ rule(lhs: simple(:lhs), location: simple(:location), rhs: simple(:rhs)) do |lhs:, location:, rhs:, origin:|
85
+ Rip::Parser::Node.new(
86
+ type: :assignment,
87
+ lhs: lhs,
88
+ rhs: rhs,
89
+ location: Rip::Parser::Location.from_slice(origin, location, location.length + rhs.location.offset - location.offset)
90
+ )
91
+ end
92
+
93
+ rule(location: simple(:location), property_name: simple(:property_name)) do |location:, property_name:, origin:|
94
+ Rip::Parser::Node.new(
95
+ type: :property_access,
96
+ property_name: property_name.to_s,
97
+ location: Rip::Parser::Location.from_slice(origin, location, location.length + property_name.offset - location.offset)
98
+ )
99
+ end
100
+
101
+ rule(object: simple(:object), location: simple(:location), property_name: simple(:property_name)) do |object:, location:, property_name:, origin:|
102
+ Rip::Parser::Node.new(
103
+ type: :property_access,
104
+ object: object,
105
+ property_name: property_name.to_s,
106
+ location: Rip::Parser::Location.from_slice(origin, location, location.length + property_name.offset - location.offset)
107
+ )
108
+ end
109
+
110
+ rule(location: simple(:location), value: simple(:value)) do |location:, value:, origin:|
111
+ Rip::Parser::Node.new(
112
+ type: :pair_value,
113
+ value: value,
114
+ location: Rip::Parser::Location.from_slice(origin, location, location.length + value.location.length)
115
+ )
116
+ end
117
+
118
+ rule(location: simple(:location), end: simple(:_end)) do |location:, _end:, origin:|
119
+ Rip::Parser::Node.new(
120
+ type: :range_end,
121
+ end: _end,
122
+ location: Rip::Parser::Location.from_slice(origin, location, location.length + _end.location.length)
123
+ )
124
+ end
125
+
126
+ rule(location: simple(:location), arguments: sequence(:arguments)) do |location:, arguments:, origin:|
127
+ Rip::Parser::Node.new(
128
+ type: :invocation,
129
+ arguments: arguments,
130
+ location: Rip::Parser::Location.from_slice(origin, location, location.length + arguments.map(&:location).map(&:length).inject(0, &:+))
131
+ )
132
+ end
133
+
134
+ rule(location: simple(:location), index_arguments: sequence(:arguments)) do |location:, arguments:, origin:|
135
+ Rip::Parser::Node.new(
136
+ type: :invocation_index,
137
+ index_arguments: arguments,
138
+ location: Rip::Parser::Location.from_slice(origin, location, location.length + arguments.map(&:location).map(&:length).inject(0, &:+))
139
+ )
140
+ end
141
+
142
+
143
+ rule(module: simple(:expression)) do |expression:, origin:|
144
+ Rip::Parser::Node.new(
145
+ type: :module,
146
+ expressions: [ expression ],
147
+ location: Rip::Parser::Location.new(origin, 0, 0, 0)
148
+ )
149
+ end
150
+
151
+ rule(module: sequence(:expressions)) do |expressions:, origin:|
152
+ Rip::Parser::Node.new(
153
+ type: :module,
154
+ expressions: expressions,
155
+ location: Rip::Parser::Location.new(origin, 0, 0, 0)
156
+ )
157
+ end
158
+
159
+
160
+ rule(import: simple(:location), module_name: simple(:module_name)) do |location:, module_name:, origin:|
161
+ Rip::Parser::Node.new(
162
+ type: :import,
163
+ module_name: module_name,
164
+ location: Rip::Parser::Location.from_slice(origin, location, location.length + module_name.location.offset - location.offset)
165
+ )
166
+ end
167
+
168
+
169
+ rule(date: simple(:date), time: simple(:time)) do |date:, time:, origin:|
170
+ Rip::Parser::Node.new(
171
+ type: :date_time,
172
+ date: date,
173
+ time: time,
174
+ location: date.location.add_character(1 + time.length)
175
+ )
176
+ end
177
+
178
+ rule(year: simple(:year), month: simple(:month), day: simple(:day)) do |year:, month:, day:, origin:|
179
+ Rip::Parser::Node.new(
180
+ type: :date,
181
+ year: year.to_s,
182
+ month: month.to_s,
183
+ day: day.to_s,
184
+ location: Rip::Parser::Location.from_slice(origin, year + '-' + month + '-' + day)
185
+ )
186
+ end
187
+
188
+ rule(hour: simple(:hour), minute: simple(:minute), second: simple(:second)) do |hour:, minute:, second:, origin:|
189
+ Rip::Parser::Node.new(
190
+ type: :time,
191
+ hour: hour.to_s,
192
+ minute: minute.to_s,
193
+ second: second.to_s,
194
+ location: Rip::Parser::Location.from_slice(origin, hour + '-' + minute + '-' + second)
195
+ )
196
+ end
197
+
198
+ rule(hour: simple(:hour), minute: simple(:minute), second: simple(:second), sub_second: simple(:sub_second)) do |hour:, minute:, second:, sub_second:, origin:|
199
+ Rip::Parser::Node.new(
200
+ type: :time,
201
+ hour: hour.to_s,
202
+ minute: minute.to_s,
203
+ second: second.to_s,
204
+ sub_second: sub_second.to_s,
205
+ location: Rip::Parser::Location.from_slice(origin, hour + '-' + minute + '-' + second + '.' + sub_second)
206
+ )
207
+ end
208
+
209
+ rule(hour: simple(:hour), minute: simple(:minute), second: simple(:second), offset: simple(:offset)) do |hour:, minute:, second:, offset:, origin:|
210
+ slice = hour + '-' + minute + '-' + second
211
+
212
+ Rip::Parser::Node.new(
213
+ type: :time,
214
+ hour: hour.to_s,
215
+ minute: minute.to_s,
216
+ second: second.to_s,
217
+ offset: offset,
218
+ location: Rip::Parser::Location.from_slice(origin, slice, slice.length + offset.length)
219
+ )
220
+ end
221
+
222
+ rule(hour: simple(:hour), minute: simple(:minute), second: simple(:second), sub_second: simple(:sub_second), offset: simple(:offset)) do |hour:, minute:, second:, sub_second:, offset:, origin:|
223
+ slice = hour + '-' + minute + '-' + second + '.' + sub_second
224
+
225
+ Rip::Parser::Node.new(
226
+ type: :time,
227
+ hour: hour.to_s,
228
+ minute: minute.to_s,
229
+ second: second.to_s,
230
+ sub_second: sub_second.to_s,
231
+ offset: offset,
232
+ location: Rip::Parser::Location.from_slice(origin, slice, slice.length + offset.length)
233
+ )
234
+ end
235
+
236
+ rule(sign: simple(:sign), hour: simple(:hour), minute: simple(:minute)) do |sign:, hour:, minute:, origin:|
237
+ Rip::Parser::Node.new(
238
+ type: :time_offset,
239
+ sign: sign.to_sym,
240
+ hour: hour.to_s,
241
+ minute: minute.to_s,
242
+ location: Rip::Parser::Location.from_slice(origin, sign + hour + minute)
243
+ )
244
+ end
245
+
246
+
247
+ rule(magnitude: simple(:magnitude), label: simple(:label)) do |magnitude:, label:, origin:|
248
+ Rip::Parser::Node.new(
249
+ type: :unit,
250
+ magnitude: magnitude,
251
+ label: label.to_s,
252
+ location: magnitude.location.add_character(label.length)
253
+ )
254
+ end
255
+
256
+
257
+ rule(integer: simple(:integer)) do |integer:, origin:|
258
+ Rip::Parser::Node.new(
259
+ type: :integer,
260
+ sign: :+,
261
+ integer: integer.to_s,
262
+ location: Rip::Parser::Location.from_slice(origin, integer, integer.length)
263
+ )
264
+ end
265
+
266
+ rule(integer: simple(:integer), decimal: simple(:decimal)) do |integer:, decimal:, origin:|
267
+ Rip::Parser::Node.new(
268
+ type: :decimal,
269
+ sign: :+,
270
+ integer: integer.to_s,
271
+ decimal: decimal.to_s,
272
+ location: Rip::Parser::Location.from_slice(origin, integer, integer.length + decimal.length)
273
+ )
274
+ end
275
+
276
+ rule(sign: simple(:sign), integer: simple(:integer)) do |sign:, integer:, origin:|
277
+ Rip::Parser::Node.new(
278
+ type: :integer,
279
+ sign: sign.to_sym,
280
+ integer: integer.to_s,
281
+ location: Rip::Parser::Location.from_slice(origin, sign, sign.length + integer.length)
282
+ )
283
+ end
284
+
285
+ rule(sign: simple(:sign), integer: simple(:integer), decimal: simple(:decimal)) do |sign:, integer:, decimal:, origin:|
286
+ Rip::Parser::Node.new(
287
+ type: :decimal,
288
+ sign: sign.to_sym,
289
+ integer: integer.to_s,
290
+ decimal: decimal.to_s,
291
+ location: Rip::Parser::Location.from_slice(origin, sign, sign.length + integer.length + decimal.length)
292
+ )
293
+ end
294
+
295
+
296
+ rule(escape_unicode: simple(:sequence), escape_location: simple(:escape_location)) do |sequence:, escape_location:, origin:|
297
+ Rip::Parser::Node.new(
298
+ type: :escape_unicode,
299
+ sequence: sequence.to_s,
300
+ location: Rip::Parser::Location.from_slice(origin, escape_location, escape_location.length + sequence.length)
301
+ )
302
+ end
303
+
304
+ rule(escape_unicode: simple(:sequence), escape_location: simple(:escape_location), location: simple(:location)) do |sequence:, location:, escape_location:, origin:|
305
+ Rip::Parser::Node.new(
306
+ type: :escape_unicode,
307
+ sequence: sequence.to_s,
308
+ location: Rip::Parser::Location.from_slice(origin, location, [ location, escape_location, sequence ].map(&:length).inject(&:+))
309
+ )
310
+ end
311
+
312
+
313
+ rule(escape_special: simple(:sequence), escape_location: simple(:escape_location)) do |sequence:, escape_location:, origin:|
314
+ Rip::Parser::Node.new(
315
+ type: :escape_special,
316
+ sequence: sequence.to_s,
317
+ location: Rip::Parser::Location.from_slice(origin, escape_location, escape_location.length + sequence.length)
318
+ )
319
+ end
320
+
321
+ rule(escape_special: simple(:sequence), escape_location: simple(:escape_location), location: simple(:location)) do |sequence:, location:, escape_location:, origin:|
322
+ Rip::Parser::Node.new(
323
+ type: :escape_special,
324
+ sequence: sequence.to_s,
325
+ location: Rip::Parser::Location.from_slice(origin, location, [ location, escape_location, sequence ].map(&:length).inject(&:+))
326
+ )
327
+ end
328
+
329
+
330
+ rule(escape_any: simple(:sequence), escape_location: simple(:escape_location)) do |sequence:, escape_location:, origin:|
331
+ Rip::Parser::Node.new(
332
+ type: :escape_any,
333
+ sequence: sequence.to_s,
334
+ location: Rip::Parser::Location.from_slice(origin, escape_location, escape_location.length + sequence.length)
335
+ )
336
+ end
337
+
338
+ rule(escape_any: simple(:sequence), escape_location: simple(:escape_location), location: simple(:location)) do |sequence:, location:, escape_location:, origin:|
339
+ Rip::Parser::Node.new(
340
+ type: :escape_any,
341
+ sequence: sequence.to_s,
342
+ location: Rip::Parser::Location.from_slice(origin, location, [ location, escape_location, sequence ].map(&:length).inject(&:+))
343
+ )
344
+ end
345
+
346
+
347
+ rule(character: simple(:character)) do |character:, origin:|
348
+ Rip::Parser::Node.new(
349
+ type: :character,
350
+ data: character.to_s,
351
+ location: Rip::Parser::Location.from_slice(origin, character, character.length)
352
+ )
353
+ end
354
+
355
+ rule(character: simple(:character), location: simple(:location)) do |character:, location:, origin:|
356
+ Rip::Parser::Node.new(
357
+ type: :character,
358
+ data: character.to_s,
359
+ location: Rip::Parser::Location.from_slice(origin, location, location.length + character.length)
360
+ )
361
+ end
362
+
363
+
364
+ rule(regular_expression: sequence(:characters), location: simple(:location)) do |characters:, location:, origin:|
365
+ length = characters.inject(location.length + 1) do |memo, character|
366
+ memo + character.location.length
367
+ end
368
+
369
+ Rip::Parser::Node.new(
370
+ type: :regular_expression,
371
+ pattern: characters,
372
+ location: Rip::Parser::Location.from_slice(origin, location, length)
373
+ )
374
+ end
375
+
376
+
377
+ rule(interpolation: simple(:expression), location: simple(:location)) do |expression:, location:, origin:|
378
+ Rip::Parser::Node.new(
379
+ type: :interpolation,
380
+ expression: expression,
381
+ location: Rip::Parser::Location.from_slice(origin, location, location.length + expression.length)
382
+ )
383
+ end
384
+
385
+
386
+ rule(string: sequence(:characters), location: simple(:location)) do |characters:, location:, origin:|
387
+ closing_delimiter_length = case location
388
+ when /:/ then 0
389
+ when /"/ then 1
390
+ end
391
+
392
+ length = characters.inject(location.length + closing_delimiter_length) do |memo, character|
393
+ memo + character.location.length
394
+ end
395
+
396
+ Rip::Parser::Node.new(
397
+ type: :string,
398
+ characters: characters,
399
+ location: Rip::Parser::Location.from_slice(origin, location, length)
400
+ )
401
+ end
402
+
403
+ rule(string: sequence(:characters), label: simple(:label), location: simple(:location)) do |characters:, label:, location:, origin:|
404
+ length = characters.inject(location.length + (label.length * 2) + 1) do |memo, character|
405
+ memo + character.location.length
406
+ end
407
+
408
+ Rip::Parser::Node.new(
409
+ type: :string,
410
+ label: label.to_s,
411
+ characters: characters,
412
+ location: Rip::Parser::Location.from_slice(origin, location, length)
413
+ )
414
+ end
415
+
416
+
417
+ rule(list: sequence(:items), location: simple(:location)) do |items:, location:, origin:|
418
+ length = items.inject(location.length) do |memo, item|
419
+ memo + item.location.length
420
+ end
421
+
422
+ Rip::Parser::Node.new(
423
+ type: :list,
424
+ items: items,
425
+ location: Rip::Parser::Location.from_slice(origin, location, length)
426
+ )
427
+ end
428
+
429
+ rule(map: sequence(:pairs), location: simple(:location)) do |pairs:, location:, origin:|
430
+ length = pairs.inject(location.length) do |memo, pair|
431
+ memo + pair.location.length
432
+ end
433
+
434
+ Rip::Parser::Node.new(
435
+ type: :map,
436
+ pairs: pairs,
437
+ location: Rip::Parser::Location.from_slice(origin, location, length)
438
+ )
439
+ end
440
+
441
+
442
+ rule(reference: simple(:reference)) do |reference:, origin:|
443
+ Rip::Parser::Node.new(
444
+ type: :reference,
445
+ name: reference.to_s,
446
+ location: Rip::Parser::Location.from_slice(origin, reference)
447
+ )
448
+ end
449
+
450
+
451
+ rule(dash_rocket: simple(:location), body: simple(:expression)) do |location:, expression:, origin:|
452
+ Rip::Parser::Node.new(
453
+ type: :overload,
454
+ parameters: [],
455
+ body: [ expression ],
456
+ location: Rip::Parser::Location.from_slice(origin, location)
457
+ )
458
+ end
459
+
460
+ rule(dash_rocket: simple(:location), parameters: sequence(:parameters), body: simple(:expression)) do |location:, parameters:, expression:, origin:|
461
+ Rip::Parser::Node.new(
462
+ type: :overload,
463
+ parameters: parameters,
464
+ body: [ expression ],
465
+ location: Rip::Parser::Location.from_slice(origin, location)
466
+ )
467
+ end
468
+
469
+ rule(dash_rocket: simple(:location), body: sequence(:body)) do |location:, body:, origin:|
470
+ Rip::Parser::Node.new(
471
+ type: :overload,
472
+ parameters: [],
473
+ body: body,
474
+ location: Rip::Parser::Location.from_slice(origin, location)
475
+ )
476
+ end
477
+
478
+ rule(dash_rocket: simple(:location), parameters: sequence(:parameters), body: sequence(:body)) do |location:, parameters:, body:, origin:|
479
+ Rip::Parser::Node.new(
480
+ type: :overload,
481
+ parameters: parameters,
482
+ body: body,
483
+ location: Rip::Parser::Location.from_slice(origin, location)
484
+ )
485
+ end
486
+
487
+
488
+ rule(parameter: simple(:parameter)) do |parameter:, origin:|
489
+ Rip::Parser::Node.new(
490
+ type: :required_parameter,
491
+ name: parameter.to_s,
492
+ location: Rip::Parser::Location.from_slice(origin, parameter)
493
+ )
494
+ end
495
+
496
+ rule(parameter: simple(:parameter), type_argument: simple(:type_argument)) do |parameter:, type_argument:, origin:|
497
+ Rip::Parser::Node.new(
498
+ type: :required_parameter,
499
+ name: parameter.to_s,
500
+ type_argument: type_argument,
501
+ location: Rip::Parser::Location.from_slice(origin, parameter)
502
+ )
503
+ end
504
+
505
+ rule(parameter: simple(:parameter), default: simple(:default)) do |parameter:, default:, origin:|
506
+ Rip::Parser::Node.new(
507
+ type: :optional_parameter,
508
+ name: parameter.to_s,
509
+ default: default,
510
+ location: Rip::Parser::Location.from_slice(origin, parameter)
511
+ )
512
+ end
513
+
514
+ rule(parameter: simple(:parameter), type_argument: simple(:type_argument), default: simple(:default)) do |parameter:, type_argument:, default:, origin:|
515
+ Rip::Parser::Node.new(
516
+ type: :optional_parameter,
517
+ name: parameter.to_s,
518
+ type_argument: type_argument,
519
+ default: default,
520
+ location: Rip::Parser::Location.from_slice(origin, parameter)
521
+ )
522
+ end
523
+
524
+
525
+ rule(fat_rocket: simple(:location), overloads: sequence(:overloads)) do |location:, overloads:, origin:|
526
+ Rip::Parser::Node.new(
527
+ type: :lambda,
528
+ overloads: overloads,
529
+ location: Rip::Parser::Location.from_slice(origin, location)
530
+ )
531
+ end
532
+
533
+
534
+ rule(swerve_rocket: simple(:location), body: simple(:expression)) do |location:, expression:, origin:|
535
+ Rip::Parser::Node.new(
536
+ type: :property,
537
+ body: [ expression ],
538
+ location: Rip::Parser::Location.from_slice(origin, location)
539
+ )
540
+ end
541
+
542
+ rule(swerve_rocket: simple(:location), body: sequence(:expressions)) do |location:, expressions:, origin:|
543
+ Rip::Parser::Node.new(
544
+ type: :property,
545
+ body: expressions,
546
+ location: Rip::Parser::Location.from_slice(origin, location)
547
+ )
548
+ end
549
+
550
+
551
+ rule(property_name: simple(:name)) do |name:, origin:|
552
+ Rip::Parser::Node.new(
553
+ type: :class_property,
554
+ name: name.to_s,
555
+ location: Rip::Parser::Location.from_slice(origin, name)
556
+ )
557
+ end
558
+
559
+ rule(class_self: simple(:self), location: simple(:location), property_name: simple(:name)) do |self:, location:, name:, origin:|
560
+ Rip::Parser::Node.new(
561
+ type: :class_property,
562
+ name: name.to_s,
563
+ location: Rip::Parser::Location.from_slice(origin, location)
564
+ )
565
+ end
566
+
567
+ rule(class_prototype: simple(:prototype), location: simple(:location), property_name: simple(:name)) do |prototype:, location:, name:, origin:|
568
+ Rip::Parser::Node.new(
569
+ type: :prototype_property,
570
+ name: name.to_s,
571
+ location: Rip::Parser::Location.from_slice(origin, location)
572
+ )
573
+ end
574
+
575
+
576
+ rule(property: simple(:property), location: simple(:location), property_value: simple(:value)) do |property:, location:, value:, origin:|
577
+ property.merge(value: value)
578
+ end
579
+
580
+
581
+ rule(class: simple(:location), body: simple(:class_property)) do |location:, class_property:, origin:|
582
+ Rip::Parser::Node.new(
583
+ type: :class,
584
+ properties: Array(class_property),
585
+ location: Rip::Parser::Location.from_slice(origin, location)
586
+ )
587
+ end
588
+
589
+ rule(class: simple(:location), body: sequence(:class_properties)) do |location:, class_properties:, origin:|
590
+ Rip::Parser::Node.new(
591
+ type: :class,
592
+ properties: class_properties,
593
+ location: Rip::Parser::Location.from_slice(origin, location)
594
+ )
595
+ end
596
+
597
+
598
+ rule(if: simple(:location), condition: simple(:condition), consequence: simple(:consequence), else: 'else', alternative: simple(:alternative)) do |location:, condition:, consequence:, alternative:, origin:|
599
+ Rip::Parser::Node.new(
600
+ type: :binary_condition,
601
+ condition: condition,
602
+ consequence: [ consequence ],
603
+ alternative: [ alternative ],
604
+ location: Rip::Parser::Location.from_slice(origin, location)
605
+ )
606
+ end
607
+
608
+ rule(if: simple(:location), condition: simple(:condition), consequence: simple(:consequence), else: 'else', alternative: sequence(:alternative)) do |location:, condition:, consequence:, alternative:, origin:|
609
+ Rip::Parser::Node.new(
610
+ type: :binary_condition,
611
+ condition: condition,
612
+ consequence: [ consequence ],
613
+ alternative: alternative,
614
+ location: Rip::Parser::Location.from_slice(origin, location)
615
+ )
616
+ end
617
+
618
+ rule(if: simple(:location), condition: simple(:condition), consequence: sequence(:consequence), else: 'else', alternative: simple(:alternative)) do |location:, condition:, consequence:, alternative:, origin:|
619
+ Rip::Parser::Node.new(
620
+ type: :binary_condition,
621
+ condition: condition,
622
+ consequence: consequence,
623
+ alternative: [ alternative ],
624
+ location: Rip::Parser::Location.from_slice(origin, location)
625
+ )
626
+ end
627
+
628
+ rule(if: simple(:location), condition: simple(:condition), consequence: sequence(:consequence), else: 'else', alternative: sequence(:alternative)) do |location:, condition:, consequence:, alternative:, origin:|
629
+ Rip::Parser::Node.new(
630
+ type: :binary_condition,
631
+ condition: condition,
632
+ consequence: consequence,
633
+ alternative: alternative,
634
+ location: Rip::Parser::Location.from_slice(origin, location)
635
+ )
636
+ end
637
+ end
638
+ end
@@ -0,0 +1,24 @@
1
+ module Rip
2
+ module Parser
3
+ def self.load(origin, source_code)
4
+ Rip::Parser::Grammar.parse(origin, source_code)
5
+ end
6
+
7
+ def self.load_file(module_path)
8
+ _module_path = Pathname.new(module_path).expand_path
9
+ load(_module_path, _module_path.read)
10
+ end
11
+
12
+ def self.root
13
+ Pathname.new(__dir__).parent.parent
14
+ end
15
+ end
16
+ end
17
+
18
+ require_relative './parser/about'
19
+ require_relative './parser/error'
20
+ require_relative './parser/grammar'
21
+ require_relative './parser/keywords'
22
+ require_relative './parser/location'
23
+ require_relative './parser/node'
24
+ require_relative './parser/rules'
@@ -0,0 +1 @@
1
+ require_relative './rip/parser'