mork-parser 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,503 @@
1
+ #
2
+ # DO NOT MODIFY!!!!
3
+ # This file is automatically generated by Racc 1.7.3
4
+ # from Racc grammar file "mork.y".
5
+ #
6
+
7
+ require 'racc/parser.rb'
8
+
9
+
10
+
11
+ module Mork
12
+ class Parser < Racc::Parser
13
+
14
+ module_eval(<<'...end mork.y/module_eval...', 'mork.y', 59)
15
+
16
+ require "mork/lexer"
17
+ require "mork/raw"
18
+ require "mork/raw/alias"
19
+ require "mork/raw/cell"
20
+ require "mork/raw/dictionary"
21
+ require "mork/raw/group"
22
+ require "mork/raw/meta_alias"
23
+ require "mork/raw/meta_table"
24
+ require "mork/raw/row"
25
+ require "mork/raw/table"
26
+
27
+ attr_reader :lexer
28
+
29
+ def parse(str)
30
+ @lexer = Mork::Lexer.new
31
+ lexer.scan_setup(str)
32
+ do_parse
33
+ end
34
+
35
+ def next_token
36
+ lexer.next_token
37
+ end
38
+
39
+ def data(str)
40
+ raw = parse(str)
41
+ raw.data
42
+ end
43
+
44
+ ...end mork.y/module_eval...
45
+ ##### State transition tables begin ###
46
+
47
+ racc_action_table = [
48
+ 22, 22, 22, 22, 9, 9, 2, 11, 60, 61,
49
+ 3, 10, 10, 11, 11, 35, 36, 37, 13, 12,
50
+ 12, 24, 24, 24, 24, 26, 26, 26, 26, 9,
51
+ 9, 31, 9, 9, 9, 38, 10, 10, 11, 11,
52
+ 10, 11, 11, 11, 12, 12, 9, 12, 12, 12,
53
+ 9, 60, 61, 42, 11, 11, 60, 61, 11, 11,
54
+ 24, 12, 35, 36, 37, 12, 35, 36, 37, 11,
55
+ 44, 26, 11, 46, 47, 53, 54, 35, 36, 37,
56
+ 35, 36, 37, 62, 63, 64, 65, 53, 67, 68,
57
+ 71, 72, 73, 74 ]
58
+
59
+ racc_action_check = [
60
+ 9, 19, 20, 21, 2, 5, 0, 12, 37, 37,
61
+ 1, 2, 5, 2, 5, 12, 12, 12, 3, 2,
62
+ 5, 9, 19, 20, 21, 9, 19, 20, 21, 6,
63
+ 7, 11, 10, 8, 28, 18, 6, 7, 6, 7,
64
+ 8, 10, 8, 28, 6, 7, 29, 10, 8, 28,
65
+ 30, 60, 60, 22, 33, 29, 61, 61, 34, 30,
66
+ 23, 29, 33, 33, 33, 30, 34, 34, 34, 35,
67
+ 24, 25, 36, 26, 27, 31, 32, 35, 35, 35,
68
+ 36, 36, 36, 42, 44, 46, 51, 52, 53, 59,
69
+ 62, 63, 67, 71 ]
70
+
71
+ racc_action_pointer = [
72
+ 4, 10, 1, 18, nil, 2, 26, 27, 30, -5,
73
+ 29, 18, -5, nil, nil, nil, nil, nil, 31, -4,
74
+ -3, -2, 47, 34, 43, 41, 42, 63, 31, 43,
75
+ 47, 60, 57, 42, 46, 57, 60, -16, nil, nil,
76
+ nil, nil, 76, nil, 56, nil, 53, nil, nil, nil,
77
+ nil, 72, 72, 72, nil, nil, nil, nil, nil, 66,
78
+ 27, 32, 82, 62, nil, nil, nil, 75, nil, nil,
79
+ nil, 84, nil, nil, nil ]
80
+
81
+ racc_action_default = [
82
+ -38, -38, -2, -38, -1, -2, -2, -2, -2, -8,
83
+ -14, -38, -23, 75, -3, -4, -5, -6, -38, -8,
84
+ -8, -8, -38, -32, -38, -35, -38, -38, -14, -14,
85
+ -14, -19, -38, -23, -23, -23, -23, -29, -7, -9,
86
+ -10, -11, -38, -33, -38, -36, -38, -13, -15, -16,
87
+ -17, -38, -19, -38, -22, -24, -25, -26, -27, -38,
88
+ -29, -29, -38, -38, -37, -18, -20, -38, -28, -30,
89
+ -31, -38, -34, -21, -12 ]
90
+
91
+ racc_goto_table = [
92
+ 29, 51, 34, 1, 45, 43, 32, 4, 59, nil,
93
+ 14, 15, 16, 17, nil, 28, nil, nil, 29, 29,
94
+ 29, 30, 66, 34, 34, 34, 34, 55, 56, 57,
95
+ 58, 69, 70, 28, 28, 28, 27, 18, nil, 30,
96
+ 30, 30, nil, nil, nil, nil, nil, 39, 40, 41,
97
+ nil, nil, nil, nil, 48, 49, 50 ]
98
+
99
+ racc_goto_check = [
100
+ 5, 12, 5, 1, 9, 10, 14, 2, 16, nil,
101
+ 2, 2, 2, 2, nil, 3, nil, nil, 5, 5,
102
+ 5, 6, 12, 5, 5, 5, 5, 14, 14, 14,
103
+ 14, 16, 16, 3, 3, 3, 11, 7, nil, 6,
104
+ 6, 6, nil, nil, nil, nil, nil, 7, 7, 7,
105
+ nil, nil, nil, nil, 11, 11, 11 ]
106
+
107
+ racc_goto_pointer = [
108
+ nil, 3, 5, 5, nil, -10, 11, 28, nil, -21,
109
+ -18, 26, -30, nil, -6, nil, -29, nil, nil ]
110
+
111
+ racc_goto_default = [
112
+ nil, nil, nil, 5, 6, 7, 8, nil, 19, 20,
113
+ 21, nil, nil, 52, nil, 33, nil, 23, 25 ]
114
+
115
+ racc_reduce_table = [
116
+ 0, 0, :racc_error,
117
+ 2, 34, :_reduce_1,
118
+ 0, 35, :_reduce_2,
119
+ 2, 35, :_reduce_3,
120
+ 2, 35, :_reduce_4,
121
+ 2, 35, :_reduce_5,
122
+ 2, 35, :_reduce_6,
123
+ 3, 36, :_reduce_7,
124
+ 0, 40, :_reduce_8,
125
+ 2, 40, :_reduce_9,
126
+ 2, 40, :_reduce_10,
127
+ 2, 40, :_reduce_11,
128
+ 5, 41, :_reduce_12,
129
+ 3, 37, :_reduce_13,
130
+ 0, 44, :_reduce_14,
131
+ 2, 44, :_reduce_15,
132
+ 2, 44, :_reduce_16,
133
+ 2, 44, :_reduce_17,
134
+ 4, 38, :_reduce_18,
135
+ 0, 45, :_reduce_none,
136
+ 2, 45, :_reduce_20,
137
+ 3, 46, :_reduce_21,
138
+ 3, 39, :_reduce_22,
139
+ 0, 47, :_reduce_23,
140
+ 2, 47, :_reduce_24,
141
+ 2, 47, :_reduce_25,
142
+ 2, 47, :_reduce_26,
143
+ 2, 47, :_reduce_27,
144
+ 3, 48, :_reduce_28,
145
+ 0, 49, :_reduce_none,
146
+ 2, 49, :_reduce_none,
147
+ 2, 49, :_reduce_none,
148
+ 0, 43, :_reduce_none,
149
+ 2, 43, :_reduce_33,
150
+ 4, 50, :_reduce_34,
151
+ 0, 42, :_reduce_none,
152
+ 2, 42, :_reduce_none,
153
+ 3, 51, :_reduce_none ]
154
+
155
+ racc_reduce_n = 38
156
+
157
+ racc_shift_n = 75
158
+
159
+ racc_token_table = {
160
+ false => 0,
161
+ :error => 1,
162
+ :magic => 2,
163
+ :dictionary_in => 3,
164
+ :dictionary_out => 4,
165
+ :meta_in => 5,
166
+ :meta_alias_in => 6,
167
+ :meta_alias => 7,
168
+ :meta_alias_out => 8,
169
+ :meta_out => 9,
170
+ :group_in => 10,
171
+ :group_out => 11,
172
+ :row_in => 12,
173
+ :row_mid => 13,
174
+ :row_out => 14,
175
+ :cell_in => 15,
176
+ :cell_value => 16,
177
+ :cell_out => 17,
178
+ :table_in => 18,
179
+ :table_out => 19,
180
+ :row_delete => 20,
181
+ :table_row_ref => 21,
182
+ :meta_table_in => 22,
183
+ :meta_table_out => 23,
184
+ :meta_table_cell => 24,
185
+ :meta_table_row_ref => 25,
186
+ :alias_in => 26,
187
+ :alias_key => 27,
188
+ :alias_value => 28,
189
+ :alias_out => 29,
190
+ :comment_in => 30,
191
+ :comment_text => 31,
192
+ :comment_out => 32 }
193
+
194
+ racc_nt_base = 33
195
+
196
+ racc_use_result_var = true
197
+
198
+ Racc_arg = [
199
+ racc_action_table,
200
+ racc_action_check,
201
+ racc_action_default,
202
+ racc_action_pointer,
203
+ racc_goto_table,
204
+ racc_goto_check,
205
+ racc_goto_default,
206
+ racc_goto_pointer,
207
+ racc_nt_base,
208
+ racc_reduce_table,
209
+ racc_token_table,
210
+ racc_shift_n,
211
+ racc_reduce_n,
212
+ racc_use_result_var ]
213
+ Ractor.make_shareable(Racc_arg) if defined?(Ractor)
214
+
215
+ Racc_token_to_s_table = [
216
+ "$end",
217
+ "error",
218
+ "magic",
219
+ "dictionary_in",
220
+ "dictionary_out",
221
+ "meta_in",
222
+ "meta_alias_in",
223
+ "meta_alias",
224
+ "meta_alias_out",
225
+ "meta_out",
226
+ "group_in",
227
+ "group_out",
228
+ "row_in",
229
+ "row_mid",
230
+ "row_out",
231
+ "cell_in",
232
+ "cell_value",
233
+ "cell_out",
234
+ "table_in",
235
+ "table_out",
236
+ "row_delete",
237
+ "table_row_ref",
238
+ "meta_table_in",
239
+ "meta_table_out",
240
+ "meta_table_cell",
241
+ "meta_table_row_ref",
242
+ "alias_in",
243
+ "alias_key",
244
+ "alias_value",
245
+ "alias_out",
246
+ "comment_in",
247
+ "comment_text",
248
+ "comment_out",
249
+ "$start",
250
+ "start",
251
+ "top_level",
252
+ "dictionary",
253
+ "group",
254
+ "row",
255
+ "table",
256
+ "dictionary_contents",
257
+ "meta",
258
+ "comments",
259
+ "aliases",
260
+ "group_content",
261
+ "cells",
262
+ "cell",
263
+ "table_content",
264
+ "meta_table",
265
+ "meta_table_content",
266
+ "alias",
267
+ "comment" ]
268
+ Ractor.make_shareable(Racc_token_to_s_table) if defined?(Ractor)
269
+
270
+ Racc_debug_parser = false
271
+
272
+ ##### State transition tables end #####
273
+
274
+ # reduce 0 omitted
275
+
276
+ module_eval(<<'.,.,', 'mork.y', 8)
277
+ def _reduce_1(val, _values, result)
278
+ result = Mork::Raw.new(values: val[1])
279
+ result
280
+ end
281
+ .,.,
282
+
283
+ module_eval(<<'.,.,', 'mork.y', 9)
284
+ def _reduce_2(val, _values, result)
285
+ result = []
286
+ result
287
+ end
288
+ .,.,
289
+
290
+ module_eval(<<'.,.,', 'mork.y', 10)
291
+ def _reduce_3(val, _values, result)
292
+ result = [val[0]] + val[1]
293
+ result
294
+ end
295
+ .,.,
296
+
297
+ module_eval(<<'.,.,', 'mork.y', 11)
298
+ def _reduce_4(val, _values, result)
299
+ result = [val[0]] + val[1]
300
+ result
301
+ end
302
+ .,.,
303
+
304
+ module_eval(<<'.,.,', 'mork.y', 12)
305
+ def _reduce_5(val, _values, result)
306
+ result = [val[0]] + val[1]
307
+ result
308
+ end
309
+ .,.,
310
+
311
+ module_eval(<<'.,.,', 'mork.y', 13)
312
+ def _reduce_6(val, _values, result)
313
+ result = [val[0]] + val[1]
314
+ result
315
+ end
316
+ .,.,
317
+
318
+ module_eval(<<'.,.,', 'mork.y', 15)
319
+ def _reduce_7(val, _values, result)
320
+ result = Mork::Raw::Dictionary.new(values: val[1].flatten.compact)
321
+ result
322
+ end
323
+ .,.,
324
+
325
+ module_eval(<<'.,.,', 'mork.y', 16)
326
+ def _reduce_8(val, _values, result)
327
+ result = []
328
+ result
329
+ end
330
+ .,.,
331
+
332
+ module_eval(<<'.,.,', 'mork.y', 17)
333
+ def _reduce_9(val, _values, result)
334
+ result = [val[0]] + val[1]
335
+ result
336
+ end
337
+ .,.,
338
+
339
+ module_eval(<<'.,.,', 'mork.y', 19)
340
+ def _reduce_10(val, _values, result)
341
+ result = val[1]
342
+ result
343
+ end
344
+ .,.,
345
+
346
+ module_eval(<<'.,.,', 'mork.y', 20)
347
+ def _reduce_11(val, _values, result)
348
+ result = [val[0]] + val[1]
349
+ result
350
+ end
351
+ .,.,
352
+
353
+ module_eval(<<'.,.,', 'mork.y', 21)
354
+ def _reduce_12(val, _values, result)
355
+ result = Mork::Raw::MetaAlias.new(raw: val[2])
356
+ result
357
+ end
358
+ .,.,
359
+
360
+ module_eval(<<'.,.,', 'mork.y', 23)
361
+ def _reduce_13(val, _values, result)
362
+ result = Mork::Raw::Group.new(values: val[1])
363
+ result
364
+ end
365
+ .,.,
366
+
367
+ module_eval(<<'.,.,', 'mork.y', 24)
368
+ def _reduce_14(val, _values, result)
369
+ result = []
370
+ result
371
+ end
372
+ .,.,
373
+
374
+ module_eval(<<'.,.,', 'mork.y', 25)
375
+ def _reduce_15(val, _values, result)
376
+ result = [val[0]] + val[1]
377
+ result
378
+ end
379
+ .,.,
380
+
381
+ module_eval(<<'.,.,', 'mork.y', 26)
382
+ def _reduce_16(val, _values, result)
383
+ result = [val[0]] + val[1]
384
+ result
385
+ end
386
+ .,.,
387
+
388
+ module_eval(<<'.,.,', 'mork.y', 27)
389
+ def _reduce_17(val, _values, result)
390
+ result = [val[0]] + val[1]
391
+ result
392
+ end
393
+ .,.,
394
+
395
+ module_eval(<<'.,.,', 'mork.y', 29)
396
+ def _reduce_18(val, _values, result)
397
+ result = Mork::Raw::Row.new(raw_id: val[1], cells: val[2])
398
+ result
399
+ end
400
+ .,.,
401
+
402
+ # reduce 19 omitted
403
+
404
+ module_eval(<<'.,.,', 'mork.y', 31)
405
+ def _reduce_20(val, _values, result)
406
+ result = val.flatten.compact
407
+ result
408
+ end
409
+ .,.,
410
+
411
+ module_eval(<<'.,.,', 'mork.y', 32)
412
+ def _reduce_21(val, _values, result)
413
+ result = Mork::Raw::Cell.new(raw: val[1])
414
+ result
415
+ end
416
+ .,.,
417
+
418
+ module_eval(<<'.,.,', 'mork.y', 34)
419
+ def _reduce_22(val, _values, result)
420
+ result = Mork::Raw::Table.new(raw_id: val[0], values: val[1])
421
+ result
422
+ end
423
+ .,.,
424
+
425
+ module_eval(<<'.,.,', 'mork.y', 35)
426
+ def _reduce_23(val, _values, result)
427
+ result = []
428
+ result
429
+ end
430
+ .,.,
431
+
432
+ module_eval(<<'.,.,', 'mork.y', 36)
433
+ def _reduce_24(val, _values, result)
434
+ result = [val[0]] + val[1]
435
+ result
436
+ end
437
+ .,.,
438
+
439
+ module_eval(<<'.,.,', 'mork.y', 37)
440
+ def _reduce_25(val, _values, result)
441
+ result = [val[0]] + val[1]
442
+ result
443
+ end
444
+ .,.,
445
+
446
+ module_eval(<<'.,.,', 'mork.y', 38)
447
+ def _reduce_26(val, _values, result)
448
+ result = [Mork::Raw::Row.new(raw_id: val[0], cells: [])] + val[1]
449
+ result
450
+ end
451
+ .,.,
452
+
453
+ module_eval(<<'.,.,', 'mork.y', 39)
454
+ def _reduce_27(val, _values, result)
455
+ result = [val[0]] + val[1]
456
+ result
457
+ end
458
+ .,.,
459
+
460
+ module_eval(<<'.,.,', 'mork.y', 40)
461
+ def _reduce_28(val, _values, result)
462
+ result = Mork::Raw::MetaTable.new(raw: val[1])
463
+ result
464
+ end
465
+ .,.,
466
+
467
+ # reduce 29 omitted
468
+
469
+ # reduce 30 omitted
470
+
471
+ # reduce 31 omitted
472
+
473
+ # reduce 32 omitted
474
+
475
+ module_eval(<<'.,.,', 'mork.y', 46)
476
+ def _reduce_33(val, _values, result)
477
+ result = val.flatten.compact
478
+ result
479
+ end
480
+ .,.,
481
+
482
+ module_eval(<<'.,.,', 'mork.y', 47)
483
+ def _reduce_34(val, _values, result)
484
+ result = Mork::Raw::Alias.new(key: val[1], value: val[2])
485
+ result
486
+ end
487
+ .,.,
488
+
489
+ # reduce 35 omitted
490
+
491
+ # reduce 36 omitted
492
+
493
+ # reduce 37 omitted
494
+
495
+ def _reduce_none(val, _values, result)
496
+ val[0]
497
+ end
498
+
499
+ end # class Parser
500
+ end # module Mork
501
+
502
+
503
+ #
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mork
4
+ class Raw; end # rubocop:disable Lint/EmptyClass
5
+
6
+ # A key-value alias pair
7
+ class Raw::Alias
8
+ attr_reader :key
9
+ attr_reader :value
10
+
11
+ def initialize(key:, value:)
12
+ @key = key
13
+ @value = value
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mork/resolved/cell"
4
+
5
+ module Mork
6
+ class Raw; end # rubocop:disable Lint/EmptyClass
7
+
8
+ # A cell in a row
9
+ class Raw::Cell
10
+ attr_reader :raw
11
+
12
+ def initialize(raw:)
13
+ @raw = raw
14
+ end
15
+
16
+ def resolve(dictionaries:)
17
+ key, value = parse(dictionaries)
18
+ Resolved::Cell.new(key: key, value: value)
19
+ end
20
+
21
+ private
22
+
23
+ PAIR_MATCH = /
24
+ \A
25
+ \^ # Cell keys are always references
26
+ (?<key>[89A-F][0-9A-F]) # Reference names are hex >= 80
27
+ (?<value_type>[\^\=]) # Cell values can be references or direct values
28
+ (?<value>.*)
29
+ /x.freeze
30
+
31
+ def parse(dictionaries)
32
+ raw_key, value_type, raw_value = raw_parts
33
+ key = resolve_value(raw_key, dictionaries["c"])
34
+ value =
35
+ if value_type == "^"
36
+ resolve_value(raw_value, dictionaries["a"])
37
+ else
38
+ raw_value
39
+ end
40
+ [key, value]
41
+ end
42
+
43
+ def raw_parts
44
+ m = PAIR_MATCH.match(raw)
45
+ raw_key = m[:key]
46
+ value_type = m[:value_type]
47
+ raw_value = m[:value]
48
+ [raw_key, value_type, raw_value]
49
+ end
50
+
51
+ def resolve_value(reference, dictionary)
52
+ dictionary.fetch(reference)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mork/raw/alias"
4
+ require "mork/raw/meta_alias"
5
+
6
+ module Mork
7
+ class Raw; end # rubocop:disable Lint/EmptyClass
8
+
9
+ # A key-value mapping with a namespace ("scope")
10
+ class Raw::Dictionary
11
+ attr_reader :values
12
+
13
+ def initialize(values:)
14
+ @values = values
15
+ end
16
+
17
+ def to_h
18
+ aliases.each.with_object({}) { |a, acc| acc[a.key] = a.value }
19
+ end
20
+
21
+ # a - "Atom" - the data
22
+ # c - "Column" - the column name
23
+ def scope
24
+ meta&.scope || "a"
25
+ end
26
+
27
+ private
28
+
29
+ def aliases
30
+ @aliases ||= values.filter { |c| c.is_a?(Raw::Alias) }
31
+ end
32
+
33
+ def meta
34
+ @meta ||= values.find { |c| c.is_a?(Raw::MetaAlias) }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mork/data"
4
+ require "mork/raw/dictionary"
5
+
6
+ module Mork
7
+ class Raw; end # rubocop:disable Lint/EmptyClass
8
+
9
+ # A group of updates to the data
10
+ class Raw::Group
11
+ attr_reader :values
12
+
13
+ def initialize(values:)
14
+ @values = values
15
+ end
16
+
17
+ def dictionaries
18
+ @dictionaries ||= values.filter { |r| r.is_a?(Raw::Dictionary) }
19
+ end
20
+
21
+ def resolve(dictionaries:)
22
+ {
23
+ rows: resolved_rows(dictionaries),
24
+ tables: resolved_tables(dictionaries)
25
+ }
26
+ end
27
+
28
+ private
29
+
30
+ def raw_rows
31
+ @raw_rows ||= values.filter { |v| v.is_a?(Raw::Row) }
32
+ end
33
+
34
+ def raw_tables
35
+ @raw_tables ||= values.filter { |v| v.is_a?(Raw::Table) }
36
+ end
37
+
38
+ def resolved_rows(dictionaries)
39
+ raw_rows.map { |r| r.resolve(dictionaries: dictionaries) }
40
+ end
41
+
42
+ def resolved_tables(dictionaries)
43
+ raw_tables.map { |t| t.resolve(dictionaries: dictionaries) }
44
+ end
45
+ end
46
+ end