depager 0.2.3 → 0.3.0.b20250423
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.
- checksums.yaml +7 -0
- data/.rubocop.yml +44 -0
- data/.simplecov +5 -0
- data/Gemfile +12 -0
- data/LICENSE.gpl +339 -0
- data/Manifest.txt +73 -0
- data/README.en +7 -21
- data/README.ja +19 -99
- data/Rakefile +31 -0
- data/bin/depager +7 -45
- data/examples/action_pl0d/pl0d.action.dr +421 -0
- data/examples/action_pl0d/test.pl0ds +48 -0
- data/examples/c89/c89.dr +493 -496
- data/examples/c89/test.c89 +10 -10
- data/examples/extension/astdf.rb +9 -0
- data/examples/extension/atree.dr +55 -0
- data/examples/{sample_calc → extension}/calc.atree.dr +42 -43
- data/examples/{sample_calc/calc.action.dr → extension/calc.simple_action.dr} +33 -33
- data/examples/extension/paction.dr +16 -15
- data/examples/extension/pactiontest.dr +14 -14
- data/examples/extension/simple_action.rb +46 -0
- data/examples/pl0d/pl0ds.dr +337 -334
- data/examples/pl0d/test.pl0ds +33 -33
- data/examples/rie_calc/calc.rie.dr +57 -0
- data/examples/rie_calc/test.calc +4 -0
- data/examples/rie_dcuse/dcuse.rie.dr +71 -0
- data/examples/rie_dcuse/test.dcuse +1 -0
- data/examples/rie_pl0/orig_ex/exerrdg.pl0 +44 -0
- data/examples/rie_pl0/orig_ex/exerrm.pl0 +19 -0
- data/examples/rie_pl0/orig_ex/exerrmre.pl0 +20 -0
- data/examples/rie_pl0/orig_ex/exerrtok.pl0 +18 -0
- data/examples/rie_pl0/orig_ex/exmdg.pl0 +40 -0
- data/examples/rie_pl0/orig_ex/exmdgwwl.pl0 +43 -0
- data/examples/rie_pl0/orig_ex/exmrw.pl0 +22 -0
- data/examples/rie_pl0/orig_ex/exmwwl.pl0 +18 -0
- data/examples/rie_pl0/orig_ex/exnorw.pl0 +17 -0
- data/examples/rie_pl0/pl0.rie.dr +450 -0
- data/examples/rie_pl0/test.pl0 +10 -0
- data/examples/slex_test/divreg.slex.dr +29 -29
- data/examples/slex_test/ljoin.slex.dr +36 -36
- data/examples/slex_test/test.divreg +1 -1
- data/examples/slex_test/test.ljoin +3 -3
- data/examples/{sample_calc/calc.nvaction.dr → tiny_calc/calc.action.dr} +33 -33
- data/examples/{sample_calc → tiny_calc}/calc.ast.action.dr +76 -66
- data/examples/{sample_calc → tiny_calc}/calc.ast.dr +67 -55
- data/examples/tiny_calc/calc.cst.dr +50 -0
- data/examples/{sample_calc → tiny_calc}/calc.dr +43 -43
- data/examples/{sample_calc → tiny_calc}/calc.lex.dr +29 -29
- data/examples/{sample_calc/calc_prec.nvaction.dr → tiny_calc/calc_prec.action.dr} +31 -31
- data/lib/depager/cli.rb +44 -0
- data/lib/depager/grammar.rb +253 -291
- data/lib/depager/lr.rb +589 -579
- data/lib/depager/parser.rb +269 -277
- data/lib/depager/plugins/_rie_debug.rb +63 -0
- data/lib/depager/plugins/action.rb +47 -0
- data/lib/depager/plugins/ast.dr +367 -0
- data/lib/depager/plugins/ast.rb +1329 -0
- data/lib/depager/{ruby/plugins → plugins}/cst.dr +174 -180
- data/lib/depager/plugins/cst.rb +591 -0
- data/lib/depager/{ruby/plugins → plugins}/lex.dr +85 -89
- data/lib/depager/plugins/lex.rb +313 -0
- data/lib/depager/plugins/rie.dr +725 -0
- data/lib/depager/plugins/rie.rb +1614 -0
- data/lib/depager/{ruby/plugins → plugins}/slex.dr +201 -200
- data/lib/depager/plugins/slex.rb +769 -0
- data/lib/depager/plugins/srp.rb +46 -0
- data/lib/depager/ruby/templates/extension_lalr_master.erb +40 -51
- data/lib/depager/ruby/templates/extension_lalr_slave.erb +113 -107
- data/lib/depager/ruby/templates/single_lalr_parser.erb +124 -117
- data/lib/depager/utils.rb +158 -318
- data/lib/depager/version.rb +3 -3
- data/lib/depager.rb +572 -670
- metadata +77 -80
- data/ChangeLog +0 -16
- data/data/depager/pre-setup.rb +0 -3
- data/examples/c89/c89.tab.rb +0 -7127
- data/examples/pl0d/pl0ds.tab.rb +0 -2698
- data/examples/sample_calc/calc.action.tab.rb +0 -457
- data/examples/sample_calc/calc.ast.action.tab.rb +0 -749
- data/examples/sample_calc/calc.ast.tab.rb +0 -665
- data/examples/sample_calc/calc.astdf.dr +0 -54
- data/examples/sample_calc/calc.astdf.tab.rb +0 -672
- data/examples/sample_calc/calc.atree.tab.rb +0 -451
- data/examples/sample_calc/calc.cst.dr +0 -45
- data/examples/sample_calc/calc.cst.tab.rb +0 -644
- data/examples/sample_calc/calc.lex.tab.rb +0 -374
- data/examples/sample_calc/calc.nvaction.tab.rb +0 -465
- data/examples/sample_calc/calc.tab.rb +0 -365
- data/examples/sample_calc/calc_prec.nvaction.tab.rb +0 -431
- data/examples/slex_test/divreg.slex.tab.rb +0 -303
- data/examples/slex_test/ljoin.slex.tab.rb +0 -370
- data/lib/depager/ruby/plugins/_ast_tmpl.rb +0 -73
- data/lib/depager/ruby/plugins/action.rb +0 -43
- data/lib/depager/ruby/plugins/ast.dr +0 -269
- data/lib/depager/ruby/plugins/ast.rb +0 -1308
- data/lib/depager/ruby/plugins/astdf.rb +0 -6
- data/lib/depager/ruby/plugins/atree.dr +0 -55
- data/lib/depager/ruby/plugins/atree.rb +0 -347
- data/lib/depager/ruby/plugins/cst.rb +0 -626
- data/lib/depager/ruby/plugins/lex.rb +0 -336
- data/lib/depager/ruby/plugins/nvaction.rb +0 -19
- data/lib/depager/ruby/plugins/slex.rb +0 -817
- data/lib/depager/ruby/plugins/srp.rb +0 -51
- data/lib/depager/ruby/templates/simple.erb +0 -23
- data/setup.rb +0 -1585
- /data/examples/{sample_calc → tiny_calc}/test.calc +0 -0
@@ -0,0 +1,591 @@
|
|
1
|
+
###
|
2
|
+
### Depager::CSTBuilderExtension - Depager Extension (master)
|
3
|
+
###
|
4
|
+
require 'depager/parser.rb'
|
5
|
+
|
6
|
+
|
7
|
+
module Depager ; end
|
8
|
+
|
9
|
+
class Depager::CSTBuilderExtension < Depager::Extension
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
super
|
13
|
+
@master = self
|
14
|
+
@slaves = []
|
15
|
+
end
|
16
|
+
|
17
|
+
def extension_registered g_parser
|
18
|
+
super g_parser
|
19
|
+
|
20
|
+
@slaves << Depager::CSTBuilderExtension::PreRuleList0::Parser.new(g_parser, self)
|
21
|
+
g_parser.hooks[:pre_rule_list].push [@slaves.last, :do_parse]
|
22
|
+
@slaves << Depager::CSTBuilderExtension::PostLhs1::Parser.new(g_parser, self)
|
23
|
+
g_parser.hooks[:post_lhs].push [@slaves.last, :do_parse]
|
24
|
+
end
|
25
|
+
|
26
|
+
module_eval <<'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/cst.dr', 7
|
27
|
+
attr_accessor :action_code, :nodes_code, :visitor_code
|
28
|
+
def init_extension
|
29
|
+
@n = 0
|
30
|
+
@visitor_code = ''
|
31
|
+
@nodes_code = ''
|
32
|
+
@action_code = ''
|
33
|
+
@on_reduce = []
|
34
|
+
end
|
35
|
+
|
36
|
+
def modify_action_code code
|
37
|
+
code.gsub(/\$\.([a-z_])/, 'node.\1')
|
38
|
+
end
|
39
|
+
|
40
|
+
def post_rhs
|
41
|
+
code, lineno = '', g_parser.file.lineno
|
42
|
+
if g_parser.line =~ /\s*\{/
|
43
|
+
@original_line, @line = g_parser.original_line, g_parser.line
|
44
|
+
code = parse_block
|
45
|
+
|
46
|
+
g_parser.update_context @line
|
47
|
+
end
|
48
|
+
|
49
|
+
node_name = "#{g_parser.lhs_name}_#{g_parser.nrhs}"
|
50
|
+
accept = ""
|
51
|
+
nodes = g_parser.rhs.map do |i|
|
52
|
+
x = g_parser.int_to_sym(i)
|
53
|
+
if x.class == String
|
54
|
+
"tx#{x.unpack("H*")[0]}"
|
55
|
+
else
|
56
|
+
x = x.to_s
|
57
|
+
accept << "#{x}.accept(v); " if x =~ /\A[a-z_]+\Z/
|
58
|
+
x.downcase
|
59
|
+
end
|
60
|
+
end
|
61
|
+
attrc = nodes.map{|i| ":#{i}" }.join(', ')
|
62
|
+
inic = nodes.map{|i| "@#{i} = #{i}"}.join('; ')
|
63
|
+
@nodes_code << <<-CODE
|
64
|
+
class Node_#{node_name} < Node_#{g_parser.lhs_name}
|
65
|
+
attr_accessor #{attrc}
|
66
|
+
def initialize #{nodes.join(', ')}
|
67
|
+
super()
|
68
|
+
#{inic}
|
69
|
+
end
|
70
|
+
def accept v
|
71
|
+
#{accept}
|
72
|
+
v.visit_Node_#{node_name}(self)
|
73
|
+
end
|
74
|
+
def children
|
75
|
+
[#{nodes.join(', ')}]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
CODE
|
79
|
+
|
80
|
+
@visitor_code <<
|
81
|
+
expand_inline_code(modify_action_code(code), lineno, :wrap => "def visit_Node_#{node_name} node")
|
82
|
+
|
83
|
+
args = nodes.inject([]){|r, i| r << "val[#{r.size}]"}
|
84
|
+
code = "#{target_namespace}::Node_#{node_name}.new(#{args.join(', ')})"
|
85
|
+
n = g_parser.rules.size-1
|
86
|
+
@action_code <<
|
87
|
+
expand_inline_code(code, lineno, :wrap => "def _cst_#{n} val")
|
88
|
+
|
89
|
+
@on_reduce[n] = ":_cst_#{n}"
|
90
|
+
end
|
91
|
+
|
92
|
+
def term_extension
|
93
|
+
g_parser.outer_code << "module #{target_namespace}\n"
|
94
|
+
g_parser.outer_code << @nodes_code
|
95
|
+
g_parser.outer_code << <<-CODE
|
96
|
+
class Visitor
|
97
|
+
#{@visitor_code}
|
98
|
+
end
|
99
|
+
CODE
|
100
|
+
g_parser.outer_code << "end\n"
|
101
|
+
g_parser.outer_code << generate_action_decorator_code(@on_reduce, @action_code)
|
102
|
+
end
|
103
|
+
|
104
|
+
DEPAGER_EXPANDED_CODE
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
###
|
109
|
+
### Depager::CSTBuilderExtension::PreRuleList0 - Part of Depager Extension (slave)
|
110
|
+
###
|
111
|
+
|
112
|
+
module Depager::CSTBuilderExtension::PreRuleList0 #:nodoc:all
|
113
|
+
class Parser < Depager::LALR::Basis #:nodoc:all
|
114
|
+
include Depager::Utils::ExtensionSlaveMethods
|
115
|
+
|
116
|
+
### Reduce Table
|
117
|
+
REDUCE_TABLE = [
|
118
|
+
[ -1, 1 ],
|
119
|
+
[ 0, 2 ],
|
120
|
+
[ 1, 0 ],
|
121
|
+
[ 1, 2 ],
|
122
|
+
[ 2, 0 ],
|
123
|
+
[ 2, 2 ],
|
124
|
+
]
|
125
|
+
def reduce_table; REDUCE_TABLE; end
|
126
|
+
|
127
|
+
### Term to Int
|
128
|
+
TERM_TO_INT = {
|
129
|
+
nil => 0,
|
130
|
+
false => 1,
|
131
|
+
:NODE => 2,
|
132
|
+
:ACTION => 3,
|
133
|
+
:VISITOR => 4,
|
134
|
+
}
|
135
|
+
def term_to_int; TERM_TO_INT; end
|
136
|
+
|
137
|
+
### Int to Term
|
138
|
+
INT_TO_TERM = [
|
139
|
+
nil,
|
140
|
+
false,
|
141
|
+
:NODE,
|
142
|
+
:ACTION,
|
143
|
+
:VISITOR,
|
144
|
+
]
|
145
|
+
def int_to_term; INT_TO_TERM; end
|
146
|
+
|
147
|
+
### Action Table
|
148
|
+
ACTION_TABLE = [
|
149
|
+
[ nil, nil, 3, nil, nil, ],
|
150
|
+
[ ACC, nil, nil, nil, nil, ],
|
151
|
+
[ nil, nil, nil, nil, 5, ],
|
152
|
+
[ nil, nil, nil, 6, nil, ],
|
153
|
+
[ nil, nil, nil, nil, nil, ],
|
154
|
+
[ nil, nil, nil, 7, nil, ],
|
155
|
+
[ nil, nil, nil, nil, nil, ],
|
156
|
+
[ nil, nil, nil, nil, nil, ],
|
157
|
+
]
|
158
|
+
def action_table; ACTION_TABLE; end
|
159
|
+
|
160
|
+
### Default Reduce Table
|
161
|
+
DEFRED_TABLE = [
|
162
|
+
-2,
|
163
|
+
nil,
|
164
|
+
-4,
|
165
|
+
nil,
|
166
|
+
-1,
|
167
|
+
nil,
|
168
|
+
-3,
|
169
|
+
-5,
|
170
|
+
]
|
171
|
+
def defred_table; DEFRED_TABLE; end
|
172
|
+
|
173
|
+
DEFRED_AFTER_SHIFT_TABLE = [
|
174
|
+
nil,
|
175
|
+
nil,
|
176
|
+
nil,
|
177
|
+
nil,
|
178
|
+
-1,
|
179
|
+
nil,
|
180
|
+
-3,
|
181
|
+
-5,
|
182
|
+
]
|
183
|
+
def defred_after_shift_table; DEFRED_AFTER_SHIFT_TABLE; end
|
184
|
+
|
185
|
+
### Nonterm to Int
|
186
|
+
NONTERM_TO_INT = {
|
187
|
+
:start => 0,
|
188
|
+
:opt_node => 1,
|
189
|
+
:opt_visitor => 2,
|
190
|
+
}
|
191
|
+
def nonterm_to_int; NONTERM_TO_INT; end
|
192
|
+
|
193
|
+
### Int to Nonterm
|
194
|
+
INT_TO_NONTERM = [
|
195
|
+
:start,
|
196
|
+
:opt_node,
|
197
|
+
:opt_visitor,
|
198
|
+
]
|
199
|
+
def int_to_nonterm; INT_TO_NONTERM; end
|
200
|
+
|
201
|
+
### Goto Table
|
202
|
+
GOTO_TABLE = [
|
203
|
+
[ 1, 2, nil, ],
|
204
|
+
[ nil, nil, nil, ],
|
205
|
+
[ nil, nil, 4, ],
|
206
|
+
[ nil, nil, nil, ],
|
207
|
+
[ nil, nil, nil, ],
|
208
|
+
[ nil, nil, nil, ],
|
209
|
+
[ nil, nil, nil, ],
|
210
|
+
[ nil, nil, nil, ],
|
211
|
+
]
|
212
|
+
def goto_table; GOTO_TABLE; end
|
213
|
+
|
214
|
+
def initialize g_parser, master
|
215
|
+
super()
|
216
|
+
@g_parser = g_parser
|
217
|
+
@d_parser = g_parser.d_parser
|
218
|
+
@master = master
|
219
|
+
@decorated = Action.new(self)
|
220
|
+
end
|
221
|
+
|
222
|
+
|
223
|
+
def do_parse?
|
224
|
+
if @line.match(/^\s*%CST\{\s*\Z/)
|
225
|
+
@line = $'
|
226
|
+
true
|
227
|
+
else
|
228
|
+
false
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
|
233
|
+
def banner
|
234
|
+
"%CST{ ... } / Depager::CSTBuilderExtension"
|
235
|
+
end
|
236
|
+
|
237
|
+
def lex
|
238
|
+
begin
|
239
|
+
until @line.empty?
|
240
|
+
case @line
|
241
|
+
when /\A\s+/, /\A#.*/
|
242
|
+
@line = $'
|
243
|
+
|
244
|
+
|
245
|
+
when /\A%\}\s*\Z/
|
246
|
+
@line = $'
|
247
|
+
yield nil,nil
|
248
|
+
|
249
|
+
when /\ANode/
|
250
|
+
@line = $'
|
251
|
+
yield token(:NODE, $&)
|
252
|
+
|
253
|
+
when /\AVisitor/
|
254
|
+
@line = $'
|
255
|
+
yield token(:VISITOR, $&)
|
256
|
+
|
257
|
+
when /\A\{/
|
258
|
+
#
|
259
|
+
lineno = file.lineno; yield token(:ACTION, parse_block, lineno)
|
260
|
+
|
261
|
+
|
262
|
+
else
|
263
|
+
raise RuntimeError, "must not happen #{@line}"
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end while @original_line = @line = file.gets
|
267
|
+
yield nil, nil
|
268
|
+
end
|
269
|
+
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
class Depager::CSTBuilderExtension::PreRuleList0::Action < Depager::LALR::Action #:nodoc:all
|
274
|
+
include Depager::Utils::ExtensionSlaveDecoratorMethods
|
275
|
+
ON_REDUCE = [
|
276
|
+
nil,
|
277
|
+
:_act_1,
|
278
|
+
:_act_2,
|
279
|
+
:_act_3,
|
280
|
+
:_act_4,
|
281
|
+
:_act_5,
|
282
|
+
|
283
|
+
]
|
284
|
+
def on_reduce; ON_REDUCE; end
|
285
|
+
|
286
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/cst.dr', 97
|
287
|
+
def _act_1 val
|
288
|
+
_opt_node, _opt_visitor, = *val
|
289
|
+
|
290
|
+
master.nodes_code << <<-CODE
|
291
|
+
class Node
|
292
|
+
def accept
|
293
|
+
end
|
294
|
+
#{_opt_node}
|
295
|
+
end
|
296
|
+
CODE
|
297
|
+
master.visitor_code << _opt_visitor
|
298
|
+
|
299
|
+
end
|
300
|
+
DEPAGER_EXPANDED_CODE
|
301
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/cst.dr', 109
|
302
|
+
def _act_2 val
|
303
|
+
# empty rule
|
304
|
+
''
|
305
|
+
|
306
|
+
end
|
307
|
+
DEPAGER_EXPANDED_CODE
|
308
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/cst.dr', 110
|
309
|
+
def _act_3 val
|
310
|
+
_NODE, _ACTION, = *val
|
311
|
+
_ACTION.value
|
312
|
+
|
313
|
+
end
|
314
|
+
DEPAGER_EXPANDED_CODE
|
315
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/cst.dr', 113
|
316
|
+
def _act_4 val
|
317
|
+
# empty rule
|
318
|
+
''
|
319
|
+
|
320
|
+
end
|
321
|
+
DEPAGER_EXPANDED_CODE
|
322
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/cst.dr', 114
|
323
|
+
def _act_5 val
|
324
|
+
_VISITOR, _ACTION, = *val
|
325
|
+
_ACTION.value
|
326
|
+
|
327
|
+
end
|
328
|
+
DEPAGER_EXPANDED_CODE
|
329
|
+
|
330
|
+
|
331
|
+
end
|
332
|
+
|
333
|
+
###
|
334
|
+
### Depager::CSTBuilderExtension::PostLhs1 - Part of Depager Extension (slave)
|
335
|
+
###
|
336
|
+
|
337
|
+
module Depager::CSTBuilderExtension::PostLhs1 #:nodoc:all
|
338
|
+
class Parser < Depager::LALR::Basis #:nodoc:all
|
339
|
+
include Depager::Utils::ExtensionSlaveMethods
|
340
|
+
|
341
|
+
### Reduce Table
|
342
|
+
REDUCE_TABLE = [
|
343
|
+
[ -1, 1 ],
|
344
|
+
[ 0, 3 ],
|
345
|
+
[ 1, 1 ],
|
346
|
+
[ 1, 3 ],
|
347
|
+
[ 2, 1 ],
|
348
|
+
[ 2, 3 ],
|
349
|
+
]
|
350
|
+
def reduce_table; REDUCE_TABLE; end
|
351
|
+
|
352
|
+
### Term to Int
|
353
|
+
TERM_TO_INT = {
|
354
|
+
nil => 0,
|
355
|
+
false => 1,
|
356
|
+
"[" => 2,
|
357
|
+
"]" => 3,
|
358
|
+
"," => 4,
|
359
|
+
:ID => 5,
|
360
|
+
"=" => 6,
|
361
|
+
:LIT => 7,
|
362
|
+
}
|
363
|
+
def term_to_int; TERM_TO_INT; end
|
364
|
+
|
365
|
+
### Int to Term
|
366
|
+
INT_TO_TERM = [
|
367
|
+
nil,
|
368
|
+
false,
|
369
|
+
"[",
|
370
|
+
"]",
|
371
|
+
",",
|
372
|
+
:ID,
|
373
|
+
"=",
|
374
|
+
:LIT,
|
375
|
+
]
|
376
|
+
def int_to_term; INT_TO_TERM; end
|
377
|
+
|
378
|
+
### Action Table
|
379
|
+
ACTION_TABLE = [
|
380
|
+
[ nil, nil, 2, nil, nil, nil, nil, nil, ],
|
381
|
+
[ ACC, nil, nil, nil, nil, nil, nil, nil, ],
|
382
|
+
[ nil, nil, nil, nil, nil, 5, nil, nil, ],
|
383
|
+
[ nil, nil, nil, 6, 7, nil, nil, nil, ],
|
384
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
385
|
+
[ nil, nil, nil, nil, nil, nil, 8, nil, ],
|
386
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
387
|
+
[ nil, nil, nil, nil, nil, 5, nil, nil, ],
|
388
|
+
[ nil, nil, nil, nil, nil, nil, nil, 10, ],
|
389
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
390
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
391
|
+
]
|
392
|
+
def action_table; ACTION_TABLE; end
|
393
|
+
|
394
|
+
### Default Reduce Table
|
395
|
+
DEFRED_TABLE = [
|
396
|
+
nil,
|
397
|
+
nil,
|
398
|
+
nil,
|
399
|
+
nil,
|
400
|
+
-2,
|
401
|
+
-4,
|
402
|
+
-1,
|
403
|
+
nil,
|
404
|
+
nil,
|
405
|
+
-3,
|
406
|
+
-5,
|
407
|
+
]
|
408
|
+
def defred_table; DEFRED_TABLE; end
|
409
|
+
|
410
|
+
DEFRED_AFTER_SHIFT_TABLE = [
|
411
|
+
nil,
|
412
|
+
nil,
|
413
|
+
nil,
|
414
|
+
nil,
|
415
|
+
-2,
|
416
|
+
nil,
|
417
|
+
-1,
|
418
|
+
nil,
|
419
|
+
nil,
|
420
|
+
-3,
|
421
|
+
-5,
|
422
|
+
]
|
423
|
+
def defred_after_shift_table; DEFRED_AFTER_SHIFT_TABLE; end
|
424
|
+
|
425
|
+
### Nonterm to Int
|
426
|
+
NONTERM_TO_INT = {
|
427
|
+
:start => 0,
|
428
|
+
:pair => 1,
|
429
|
+
:expr => 2,
|
430
|
+
}
|
431
|
+
def nonterm_to_int; NONTERM_TO_INT; end
|
432
|
+
|
433
|
+
### Int to Nonterm
|
434
|
+
INT_TO_NONTERM = [
|
435
|
+
:start,
|
436
|
+
:pair,
|
437
|
+
:expr,
|
438
|
+
]
|
439
|
+
def int_to_nonterm; INT_TO_NONTERM; end
|
440
|
+
|
441
|
+
### Goto Table
|
442
|
+
GOTO_TABLE = [
|
443
|
+
[ 1, nil, nil, ],
|
444
|
+
[ nil, nil, nil, ],
|
445
|
+
[ nil, 3, 4, ],
|
446
|
+
[ nil, nil, nil, ],
|
447
|
+
[ nil, nil, nil, ],
|
448
|
+
[ nil, nil, nil, ],
|
449
|
+
[ nil, nil, nil, ],
|
450
|
+
[ nil, nil, 9, ],
|
451
|
+
[ nil, nil, nil, ],
|
452
|
+
[ nil, nil, nil, ],
|
453
|
+
[ nil, nil, nil, ],
|
454
|
+
]
|
455
|
+
def goto_table; GOTO_TABLE; end
|
456
|
+
|
457
|
+
def initialize g_parser, master
|
458
|
+
super()
|
459
|
+
@g_parser = g_parser
|
460
|
+
@d_parser = g_parser.d_parser
|
461
|
+
@master = master
|
462
|
+
@decorated = Action.new(self)
|
463
|
+
end
|
464
|
+
|
465
|
+
|
466
|
+
def do_parse?
|
467
|
+
if @line.match(/^\s*\[/)
|
468
|
+
@line = $'
|
469
|
+
true
|
470
|
+
else
|
471
|
+
false
|
472
|
+
end
|
473
|
+
end
|
474
|
+
|
475
|
+
|
476
|
+
def banner
|
477
|
+
"[ ... ] / Depager::CSTBuilderExtension"
|
478
|
+
end
|
479
|
+
|
480
|
+
def lex
|
481
|
+
begin
|
482
|
+
until @line.empty?
|
483
|
+
case @line
|
484
|
+
when /\A\A\s+/
|
485
|
+
@line = $'
|
486
|
+
|
487
|
+
|
488
|
+
when /\A""/, /\A''/, /\A[0-9]+/, /\A\{\s*\}/, /\A\[\s*\]/, /\Anil/
|
489
|
+
@line = $'
|
490
|
+
yield token(:LIT, $&)
|
491
|
+
|
492
|
+
when /\A\A[a-z][a-z_]*/
|
493
|
+
@line = $'
|
494
|
+
yield token(:ID, $&)
|
495
|
+
|
496
|
+
when /\A\A(.)/
|
497
|
+
@line = $'
|
498
|
+
yield token($&, $&)
|
499
|
+
|
500
|
+
|
501
|
+
else
|
502
|
+
raise RuntimeError, "must not happen #{@line}"
|
503
|
+
end
|
504
|
+
end
|
505
|
+
end while @original_line = @line = file.gets
|
506
|
+
yield nil, nil
|
507
|
+
end
|
508
|
+
module_eval <<'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/cst.dr', 123
|
509
|
+
def do_default
|
510
|
+
master.nodes_code << <<-CODE
|
511
|
+
class Node_#{g_parser.lhs_name} < Node
|
512
|
+
def initialize
|
513
|
+
super()
|
514
|
+
end
|
515
|
+
end
|
516
|
+
CODE
|
517
|
+
end
|
518
|
+
|
519
|
+
DEPAGER_EXPANDED_CODE
|
520
|
+
|
521
|
+
end
|
522
|
+
end
|
523
|
+
|
524
|
+
class Depager::CSTBuilderExtension::PostLhs1::Action < Depager::LALR::Action #:nodoc:all
|
525
|
+
include Depager::Utils::ExtensionSlaveDecoratorMethods
|
526
|
+
ON_REDUCE = [
|
527
|
+
nil,
|
528
|
+
:_act_1,
|
529
|
+
:_act_2,
|
530
|
+
:_act_3,
|
531
|
+
:_act_4,
|
532
|
+
:_act_5,
|
533
|
+
|
534
|
+
]
|
535
|
+
def on_reduce; ON_REDUCE; end
|
536
|
+
|
537
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/cst.dr', 145
|
538
|
+
def _act_1 val
|
539
|
+
_, _pair, _, = *val
|
540
|
+
|
541
|
+
args = []; code = ''
|
542
|
+
_pair.each do |id, lit|
|
543
|
+
args << id
|
544
|
+
code << "#{id} = #{lit}; " if lit
|
545
|
+
end
|
546
|
+
attrc = args.map{|i| ":#{i}"}.join(', ')
|
547
|
+
master.nodes_code << <<-CODE
|
548
|
+
class Node_#{g_parser.lhs_name} < Node
|
549
|
+
attr_accessor #{attrc}
|
550
|
+
def initialize
|
551
|
+
super
|
552
|
+
#{code}
|
553
|
+
end
|
554
|
+
end
|
555
|
+
CODE
|
556
|
+
|
557
|
+
end
|
558
|
+
DEPAGER_EXPANDED_CODE
|
559
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/cst.dr', 164
|
560
|
+
def _act_2 val
|
561
|
+
_expr, = *val
|
562
|
+
[ _expr ]
|
563
|
+
|
564
|
+
end
|
565
|
+
DEPAGER_EXPANDED_CODE
|
566
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/cst.dr', 165
|
567
|
+
def _act_3 val
|
568
|
+
_pair, _, _expr, = *val
|
569
|
+
_pair << _expr
|
570
|
+
|
571
|
+
end
|
572
|
+
DEPAGER_EXPANDED_CODE
|
573
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/cst.dr', 168
|
574
|
+
def _act_4 val
|
575
|
+
_ID, = *val
|
576
|
+
[ _ID.value, nil]
|
577
|
+
|
578
|
+
end
|
579
|
+
DEPAGER_EXPANDED_CODE
|
580
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/cst.dr', 169
|
581
|
+
def _act_5 val
|
582
|
+
_ID, _, _LIT, = *val
|
583
|
+
[ _ID.value, _LIT.value ]
|
584
|
+
|
585
|
+
end
|
586
|
+
DEPAGER_EXPANDED_CODE
|
587
|
+
|
588
|
+
|
589
|
+
end
|
590
|
+
|
591
|
+
|