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,769 @@
|
|
1
|
+
###
|
2
|
+
### Depager::StatefulLexerExtension - Depager Extension (master)
|
3
|
+
###
|
4
|
+
require 'depager/parser.rb'
|
5
|
+
|
6
|
+
|
7
|
+
module Depager ; end
|
8
|
+
|
9
|
+
class Depager::StatefulLexerExtension < 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::StatefulLexerExtension::PreRuleList0::Parser.new(g_parser, self)
|
21
|
+
g_parser.hooks[:pre_rule_list].push [@slaves.last, :do_parse]
|
22
|
+
@slaves << Depager::StatefulLexerExtension::PreRhsPostSymbol1::Parser.new(g_parser, self)
|
23
|
+
g_parser.hooks[:pre_rhs].push [@slaves.last, :do_parse]
|
24
|
+
g_parser.hooks[:post_symbol].push [@slaves.last, :do_parse]
|
25
|
+
end
|
26
|
+
|
27
|
+
module_eval <<'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 7
|
28
|
+
attr_accessor :ins, :code, :optval
|
29
|
+
def init_extension
|
30
|
+
@ins = []
|
31
|
+
@action_code = ''
|
32
|
+
@on_reduce = []
|
33
|
+
@addition = []
|
34
|
+
end
|
35
|
+
|
36
|
+
def term_extension
|
37
|
+
@action_code << <<-CODE
|
38
|
+
def after_error
|
39
|
+
warn "StatefulLex: lex_state==\#{@basis.lex_state}" if Depager.debug_mode?
|
40
|
+
end
|
41
|
+
CODE
|
42
|
+
g_parser.outer_code <<
|
43
|
+
generate_action_decorator_code(@on_reduce, @action_code)
|
44
|
+
end
|
45
|
+
|
46
|
+
def post_rhs
|
47
|
+
j = 0
|
48
|
+
rhs = g_parser.rhs
|
49
|
+
@ins.each do |i, option_list, debug|
|
50
|
+
state_name = "#{g_parser.lhs_name}_#{g_parser.nrhs}_#{i}"
|
51
|
+
m_name = "_lex_#{state_name}"
|
52
|
+
if i != rhs.size
|
53
|
+
nt_name = "__#{state_name}__"
|
54
|
+
isym = g_parser.add_nonterm(nt_name)
|
55
|
+
g_parser.insert_sym_to_rhs(i + j, isym, nt_name)
|
56
|
+
@addition << [ isym, m_name ]
|
57
|
+
j += 1
|
58
|
+
else
|
59
|
+
@on_reduce[g_parser.rules.size-1] = ':'+m_name
|
60
|
+
end
|
61
|
+
|
62
|
+
codes = option_list.map do |k, m|
|
63
|
+
case k
|
64
|
+
when :GOTO
|
65
|
+
"@basis.lex_state = #{m}" <<
|
66
|
+
(debug ? ";warn 'MODE:->#{m}'" : '')
|
67
|
+
when :ADD
|
68
|
+
"@basis.lex_context[#{m}] = true" <<
|
69
|
+
(debug ? ";warn 'CONTEXT:+#{m}/\#{@context.inspect}'" : '')
|
70
|
+
when :SUB
|
71
|
+
"@basis.lex_context.delete #{m}" <<
|
72
|
+
(debug ? ";warn 'CONTEXT:-#{m}'/\#{@context.inspect}" : '')
|
73
|
+
else
|
74
|
+
end
|
75
|
+
end
|
76
|
+
@action_code << <<-CODE
|
77
|
+
def #{m_name} val
|
78
|
+
#{codes.join("\n ")}
|
79
|
+
end
|
80
|
+
CODE
|
81
|
+
end
|
82
|
+
@ins.clear
|
83
|
+
end
|
84
|
+
|
85
|
+
def post_rhs_list
|
86
|
+
@addition.each do |isym, m_name|
|
87
|
+
g_parser.add_rule isym, []
|
88
|
+
@on_reduce[g_parser.rules.size-1] = ':'+m_name
|
89
|
+
end
|
90
|
+
@addition.clear
|
91
|
+
end
|
92
|
+
|
93
|
+
DEPAGER_EXPANDED_CODE
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
###
|
98
|
+
### Depager::StatefulLexerExtension::PreRuleList0 - Part of Depager Extension (slave)
|
99
|
+
###
|
100
|
+
|
101
|
+
module Depager::StatefulLexerExtension::PreRuleList0 #:nodoc:all
|
102
|
+
class Parser < Depager::LALR::Basis #:nodoc:all
|
103
|
+
include Depager::Utils::ExtensionSlaveMethods
|
104
|
+
|
105
|
+
### Reduce Table
|
106
|
+
REDUCE_TABLE = [
|
107
|
+
[ -1, 1 ],
|
108
|
+
[ 0, 1 ],
|
109
|
+
[ 1, 0 ],
|
110
|
+
[ 1, 3 ],
|
111
|
+
[ 2, 4 ],
|
112
|
+
[ 4, 0 ],
|
113
|
+
[ 4, 2 ],
|
114
|
+
[ 3, 1 ],
|
115
|
+
[ 3, 2 ],
|
116
|
+
[ 5, 3 ],
|
117
|
+
[ 7, 0 ],
|
118
|
+
[ 7, 1 ],
|
119
|
+
[ 6, 1 ],
|
120
|
+
[ 6, 3 ],
|
121
|
+
]
|
122
|
+
def reduce_table; REDUCE_TABLE; end
|
123
|
+
|
124
|
+
### Term to Int
|
125
|
+
TERM_TO_INT = {
|
126
|
+
nil => 0,
|
127
|
+
false => 1,
|
128
|
+
"<" => 2,
|
129
|
+
:ID => 3,
|
130
|
+
">" => 4,
|
131
|
+
":" => 5,
|
132
|
+
:ACTION => 6,
|
133
|
+
"!" => 7,
|
134
|
+
:LEX => 8,
|
135
|
+
"," => 9,
|
136
|
+
}
|
137
|
+
def term_to_int; TERM_TO_INT; end
|
138
|
+
|
139
|
+
### Int to Term
|
140
|
+
INT_TO_TERM = [
|
141
|
+
nil,
|
142
|
+
false,
|
143
|
+
"<",
|
144
|
+
:ID,
|
145
|
+
">",
|
146
|
+
":",
|
147
|
+
:ACTION,
|
148
|
+
"!",
|
149
|
+
:LEX,
|
150
|
+
",",
|
151
|
+
]
|
152
|
+
def int_to_term; INT_TO_TERM; end
|
153
|
+
|
154
|
+
### Action Table
|
155
|
+
ACTION_TABLE = [
|
156
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
157
|
+
[ ACC, nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
158
|
+
[ nil, nil, 4, nil, nil, nil, nil, nil, nil, nil, ],
|
159
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, 8, nil, ],
|
160
|
+
[ nil, nil, nil, 9, nil, nil, nil, nil, nil, nil, ],
|
161
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, 8, nil, ],
|
162
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
163
|
+
[ nil, nil, nil, nil, nil, nil, nil, 12, nil, 13, ],
|
164
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
165
|
+
[ nil, nil, nil, nil, nil, 15, nil, nil, nil, nil, ],
|
166
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
167
|
+
[ nil, nil, nil, nil, nil, nil, 16, nil, nil, nil, ],
|
168
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
169
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, 17, nil, ],
|
170
|
+
[ nil, nil, nil, nil, 18, nil, nil, nil, nil, nil, ],
|
171
|
+
[ nil, nil, nil, 19, nil, nil, nil, nil, nil, nil, ],
|
172
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
173
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
174
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
175
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
176
|
+
]
|
177
|
+
def action_table; ACTION_TABLE; end
|
178
|
+
|
179
|
+
### Default Reduce Table
|
180
|
+
DEFRED_TABLE = [
|
181
|
+
-2,
|
182
|
+
nil,
|
183
|
+
-1,
|
184
|
+
nil,
|
185
|
+
nil,
|
186
|
+
-3,
|
187
|
+
-7,
|
188
|
+
-10,
|
189
|
+
-12,
|
190
|
+
-5,
|
191
|
+
-8,
|
192
|
+
nil,
|
193
|
+
-11,
|
194
|
+
nil,
|
195
|
+
nil,
|
196
|
+
nil,
|
197
|
+
-9,
|
198
|
+
-13,
|
199
|
+
-4,
|
200
|
+
-6,
|
201
|
+
]
|
202
|
+
def defred_table; DEFRED_TABLE; end
|
203
|
+
|
204
|
+
DEFRED_AFTER_SHIFT_TABLE = [
|
205
|
+
-2,
|
206
|
+
nil,
|
207
|
+
nil,
|
208
|
+
nil,
|
209
|
+
nil,
|
210
|
+
nil,
|
211
|
+
-7,
|
212
|
+
nil,
|
213
|
+
-12,
|
214
|
+
nil,
|
215
|
+
-8,
|
216
|
+
nil,
|
217
|
+
-11,
|
218
|
+
nil,
|
219
|
+
nil,
|
220
|
+
nil,
|
221
|
+
-9,
|
222
|
+
-13,
|
223
|
+
-4,
|
224
|
+
-6,
|
225
|
+
]
|
226
|
+
def defred_after_shift_table; DEFRED_AFTER_SHIFT_TABLE; end
|
227
|
+
|
228
|
+
### Nonterm to Int
|
229
|
+
NONTERM_TO_INT = {
|
230
|
+
:start => 0,
|
231
|
+
:mode_list => 1,
|
232
|
+
:mode => 2,
|
233
|
+
:lexactlist => 3,
|
234
|
+
:opt_super => 4,
|
235
|
+
:lexact => 5,
|
236
|
+
:lexlist => 6,
|
237
|
+
:opt_noskip => 7,
|
238
|
+
}
|
239
|
+
def nonterm_to_int; NONTERM_TO_INT; end
|
240
|
+
|
241
|
+
### Int to Nonterm
|
242
|
+
INT_TO_NONTERM = [
|
243
|
+
:start,
|
244
|
+
:mode_list,
|
245
|
+
:mode,
|
246
|
+
:lexactlist,
|
247
|
+
:opt_super,
|
248
|
+
:lexact,
|
249
|
+
:lexlist,
|
250
|
+
:opt_noskip,
|
251
|
+
]
|
252
|
+
def int_to_nonterm; INT_TO_NONTERM; end
|
253
|
+
|
254
|
+
### Goto Table
|
255
|
+
GOTO_TABLE = [
|
256
|
+
[ 1, 2, nil, nil, nil, nil, nil, nil, ],
|
257
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
258
|
+
[ nil, nil, 3, nil, nil, nil, nil, nil, ],
|
259
|
+
[ nil, nil, nil, 5, nil, 6, 7, nil, ],
|
260
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
261
|
+
[ nil, nil, nil, nil, nil, 10, 7, nil, ],
|
262
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
263
|
+
[ nil, nil, nil, nil, nil, nil, nil, 11, ],
|
264
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
265
|
+
[ nil, nil, nil, nil, 14, nil, nil, nil, ],
|
266
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
267
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
268
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
269
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
270
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
271
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
272
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
273
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
274
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
275
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, ],
|
276
|
+
]
|
277
|
+
def goto_table; GOTO_TABLE; end
|
278
|
+
|
279
|
+
def initialize g_parser, master
|
280
|
+
super()
|
281
|
+
@g_parser = g_parser
|
282
|
+
@d_parser = g_parser.d_parser
|
283
|
+
@master = master
|
284
|
+
@decorated = Action.new(self)
|
285
|
+
end
|
286
|
+
|
287
|
+
|
288
|
+
def do_parse?
|
289
|
+
if @line.match(/^\s*%LEX\{\s*\Z/)
|
290
|
+
@line = $'
|
291
|
+
true
|
292
|
+
else
|
293
|
+
false
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
|
298
|
+
def banner
|
299
|
+
"%LEX{ ... } / Depager::StatefulLexerExtension"
|
300
|
+
end
|
301
|
+
|
302
|
+
def lex
|
303
|
+
begin
|
304
|
+
until @line.empty?
|
305
|
+
case @line
|
306
|
+
when /\A\s+/, /\A#.*\Z/
|
307
|
+
@line = $'
|
308
|
+
|
309
|
+
|
310
|
+
when /\A[A-Z]+/
|
311
|
+
@line = $'
|
312
|
+
yield token(:ID, $&)
|
313
|
+
|
314
|
+
when /\A\%\}\s*\Z/
|
315
|
+
@line = $'
|
316
|
+
yield nil,nil
|
317
|
+
|
318
|
+
when /\A\/(([^\/\\]+|\\.)*)\//
|
319
|
+
@line = $'
|
320
|
+
yield token(:LEX, "/\\A#{$1}/")
|
321
|
+
|
322
|
+
when /\A'([^'\\]+|\\.)*'/, /\A"([^"\\]+|\\.)*"/
|
323
|
+
@line = $'
|
324
|
+
|
325
|
+
yield token(:LEX, "/\\A#{Regexp.escape($1).gsub('/', '\/')}/")
|
326
|
+
|
327
|
+
when /\A\{/
|
328
|
+
#
|
329
|
+
|
330
|
+
lineno = file.lineno
|
331
|
+
yield token(:ACTION, parse_block, lineno)
|
332
|
+
|
333
|
+
when /\A./
|
334
|
+
@line = $'
|
335
|
+
yield token($&, $&)
|
336
|
+
|
337
|
+
|
338
|
+
else
|
339
|
+
raise RuntimeError, "must not happen #{@line}"
|
340
|
+
end
|
341
|
+
end
|
342
|
+
end while @original_line = @line = file.gets
|
343
|
+
yield nil, nil
|
344
|
+
end
|
345
|
+
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
class Depager::StatefulLexerExtension::PreRuleList0::Action < Depager::LALR::Action #:nodoc:all
|
350
|
+
include Depager::Utils::ExtensionSlaveDecoratorMethods
|
351
|
+
ON_REDUCE = [
|
352
|
+
nil,
|
353
|
+
:_act_1,
|
354
|
+
:_act_2,
|
355
|
+
:_act_3,
|
356
|
+
:_act_4,
|
357
|
+
:_act_5,
|
358
|
+
:_act_6,
|
359
|
+
:_act_7,
|
360
|
+
:_act_8,
|
361
|
+
:_act_9,
|
362
|
+
:_act_10,
|
363
|
+
:_act_11,
|
364
|
+
:_act_12,
|
365
|
+
:_act_13,
|
366
|
+
|
367
|
+
]
|
368
|
+
def on_reduce; ON_REDUCE; end
|
369
|
+
|
370
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 96
|
371
|
+
def _act_1 val
|
372
|
+
_mode_list, = *val
|
373
|
+
|
374
|
+
ll = ''
|
375
|
+
_mode_list.each do |m, s, l|
|
376
|
+
else_code = if s
|
377
|
+
"lex_#{s}(&block)"
|
378
|
+
else
|
379
|
+
'raise RuntimeError, "must not happen #{@line}"'
|
380
|
+
end
|
381
|
+
ll << <<-CODE
|
382
|
+
def lex_#{m}(&block)
|
383
|
+
case @line
|
384
|
+
#{l}
|
385
|
+
else
|
386
|
+
#{else_code}
|
387
|
+
end
|
388
|
+
end
|
389
|
+
CODE
|
390
|
+
end
|
391
|
+
g_parser.inner_code << ll << <<-CODE
|
392
|
+
attr_accessor :lex_state, :lex_context
|
393
|
+
def lex(&block)
|
394
|
+
@lex_state ||= :START
|
395
|
+
@lex_context ||= {}
|
396
|
+
begin
|
397
|
+
until @line.empty?
|
398
|
+
self.send "lex_\#{@lex_state}", &block
|
399
|
+
end
|
400
|
+
end while @original_line = @line = file.gets
|
401
|
+
yield nil, nil
|
402
|
+
end
|
403
|
+
CODE
|
404
|
+
|
405
|
+
end
|
406
|
+
DEPAGER_EXPANDED_CODE
|
407
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 130
|
408
|
+
def _act_2 val
|
409
|
+
# empty rule
|
410
|
+
[ ]
|
411
|
+
|
412
|
+
end
|
413
|
+
DEPAGER_EXPANDED_CODE
|
414
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 131
|
415
|
+
def _act_3 val
|
416
|
+
_mode_list, _mode, _lexactlist, = *val
|
417
|
+
_mode_list << [_mode[0], _mode[1], _lexactlist]
|
418
|
+
|
419
|
+
end
|
420
|
+
DEPAGER_EXPANDED_CODE
|
421
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 134
|
422
|
+
def _act_4 val
|
423
|
+
_, _ID, _opt_super, _, = *val
|
424
|
+
[ _ID.value, _opt_super ]
|
425
|
+
|
426
|
+
end
|
427
|
+
DEPAGER_EXPANDED_CODE
|
428
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 137
|
429
|
+
def _act_5 val
|
430
|
+
# empty rule
|
431
|
+
nil
|
432
|
+
|
433
|
+
end
|
434
|
+
DEPAGER_EXPANDED_CODE
|
435
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 138
|
436
|
+
def _act_6 val
|
437
|
+
_, _ID, = *val
|
438
|
+
_ID.value
|
439
|
+
|
440
|
+
end
|
441
|
+
DEPAGER_EXPANDED_CODE
|
442
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 141
|
443
|
+
def _act_7 val
|
444
|
+
_lexact, = *val
|
445
|
+
_lexact
|
446
|
+
|
447
|
+
end
|
448
|
+
DEPAGER_EXPANDED_CODE
|
449
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 142
|
450
|
+
def _act_8 val
|
451
|
+
_lexactlist, _lexact, = *val
|
452
|
+
_lexactlist << _lexact
|
453
|
+
|
454
|
+
end
|
455
|
+
DEPAGER_EXPANDED_CODE
|
456
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 146
|
457
|
+
def _act_9 val
|
458
|
+
_lexlist, _opt_noskip, _ACTION, = *val
|
459
|
+
|
460
|
+
<<-CODE
|
461
|
+
when #{_lexlist.join(', ')}
|
462
|
+
#{ _opt_noskip ? '#' : "@line = $'" }
|
463
|
+
#{ _ACTION.value }
|
464
|
+
CODE
|
465
|
+
|
466
|
+
end
|
467
|
+
DEPAGER_EXPANDED_CODE
|
468
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 155
|
469
|
+
def _act_10 val
|
470
|
+
# empty rule
|
471
|
+
false
|
472
|
+
|
473
|
+
end
|
474
|
+
DEPAGER_EXPANDED_CODE
|
475
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 156
|
476
|
+
def _act_11 val
|
477
|
+
_, = *val
|
478
|
+
true
|
479
|
+
|
480
|
+
end
|
481
|
+
DEPAGER_EXPANDED_CODE
|
482
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 159
|
483
|
+
def _act_12 val
|
484
|
+
_LEX, = *val
|
485
|
+
[ _LEX.value ]
|
486
|
+
|
487
|
+
end
|
488
|
+
DEPAGER_EXPANDED_CODE
|
489
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 160
|
490
|
+
def _act_13 val
|
491
|
+
_lexlist, _, _LEX, = *val
|
492
|
+
_lexlist.push _LEX.value
|
493
|
+
|
494
|
+
end
|
495
|
+
DEPAGER_EXPANDED_CODE
|
496
|
+
|
497
|
+
|
498
|
+
end
|
499
|
+
|
500
|
+
###
|
501
|
+
### Depager::StatefulLexerExtension::PreRhsPostSymbol1 - Part of Depager Extension (slave)
|
502
|
+
###
|
503
|
+
|
504
|
+
module Depager::StatefulLexerExtension::PreRhsPostSymbol1 #:nodoc:all
|
505
|
+
class Parser < Depager::LALR::Basis #:nodoc:all
|
506
|
+
include Depager::Utils::ExtensionSlaveMethods
|
507
|
+
|
508
|
+
### Reduce Table
|
509
|
+
REDUCE_TABLE = [
|
510
|
+
[ -1, 1 ],
|
511
|
+
[ 0, 3 ],
|
512
|
+
[ 0, 4 ],
|
513
|
+
[ 1, 1 ],
|
514
|
+
[ 1, 2 ],
|
515
|
+
[ 2, 2 ],
|
516
|
+
[ 2, 2 ],
|
517
|
+
[ 2, 2 ],
|
518
|
+
]
|
519
|
+
def reduce_table; REDUCE_TABLE; end
|
520
|
+
|
521
|
+
### Term to Int
|
522
|
+
TERM_TO_INT = {
|
523
|
+
nil => 0,
|
524
|
+
false => 1,
|
525
|
+
"[" => 2,
|
526
|
+
"]" => 3,
|
527
|
+
"?" => 4,
|
528
|
+
">" => 5,
|
529
|
+
:SYMBOL => 6,
|
530
|
+
"+" => 7,
|
531
|
+
"-" => 8,
|
532
|
+
}
|
533
|
+
def term_to_int; TERM_TO_INT; end
|
534
|
+
|
535
|
+
### Int to Term
|
536
|
+
INT_TO_TERM = [
|
537
|
+
nil,
|
538
|
+
false,
|
539
|
+
"[",
|
540
|
+
"]",
|
541
|
+
"?",
|
542
|
+
">",
|
543
|
+
:SYMBOL,
|
544
|
+
"+",
|
545
|
+
"-",
|
546
|
+
]
|
547
|
+
def int_to_term; INT_TO_TERM; end
|
548
|
+
|
549
|
+
### Action Table
|
550
|
+
ACTION_TABLE = [
|
551
|
+
[ nil, nil, 2, nil, nil, nil, nil, nil, nil, ],
|
552
|
+
[ ACC, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
553
|
+
[ nil, nil, nil, nil, nil, 5, nil, 6, 7, ],
|
554
|
+
[ nil, nil, nil, 8, 9, 5, nil, 6, 7, ],
|
555
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
556
|
+
[ nil, nil, nil, nil, nil, nil, 11, nil, nil, ],
|
557
|
+
[ nil, nil, nil, nil, nil, nil, 12, nil, nil, ],
|
558
|
+
[ nil, nil, nil, nil, nil, nil, 13, nil, nil, ],
|
559
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
560
|
+
[ nil, nil, nil, 14, nil, nil, nil, nil, nil, ],
|
561
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
562
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
563
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
564
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
565
|
+
[ nil, nil, nil, nil, nil, nil, nil, nil, nil, ],
|
566
|
+
]
|
567
|
+
def action_table; ACTION_TABLE; end
|
568
|
+
|
569
|
+
### Default Reduce Table
|
570
|
+
DEFRED_TABLE = [
|
571
|
+
nil,
|
572
|
+
nil,
|
573
|
+
nil,
|
574
|
+
nil,
|
575
|
+
-3,
|
576
|
+
nil,
|
577
|
+
nil,
|
578
|
+
nil,
|
579
|
+
-1,
|
580
|
+
nil,
|
581
|
+
-4,
|
582
|
+
-5,
|
583
|
+
-6,
|
584
|
+
-7,
|
585
|
+
-2,
|
586
|
+
]
|
587
|
+
def defred_table; DEFRED_TABLE; end
|
588
|
+
|
589
|
+
DEFRED_AFTER_SHIFT_TABLE = [
|
590
|
+
nil,
|
591
|
+
nil,
|
592
|
+
nil,
|
593
|
+
nil,
|
594
|
+
-3,
|
595
|
+
nil,
|
596
|
+
nil,
|
597
|
+
nil,
|
598
|
+
-1,
|
599
|
+
nil,
|
600
|
+
-4,
|
601
|
+
-5,
|
602
|
+
-6,
|
603
|
+
-7,
|
604
|
+
-2,
|
605
|
+
]
|
606
|
+
def defred_after_shift_table; DEFRED_AFTER_SHIFT_TABLE; end
|
607
|
+
|
608
|
+
### Nonterm to Int
|
609
|
+
NONTERM_TO_INT = {
|
610
|
+
:start => 0,
|
611
|
+
:option_list => 1,
|
612
|
+
:option => 2,
|
613
|
+
}
|
614
|
+
def nonterm_to_int; NONTERM_TO_INT; end
|
615
|
+
|
616
|
+
### Int to Nonterm
|
617
|
+
INT_TO_NONTERM = [
|
618
|
+
:start,
|
619
|
+
:option_list,
|
620
|
+
:option,
|
621
|
+
]
|
622
|
+
def int_to_nonterm; INT_TO_NONTERM; end
|
623
|
+
|
624
|
+
### Goto Table
|
625
|
+
GOTO_TABLE = [
|
626
|
+
[ 1, nil, nil, ],
|
627
|
+
[ nil, nil, nil, ],
|
628
|
+
[ nil, 3, 4, ],
|
629
|
+
[ nil, nil, 10, ],
|
630
|
+
[ nil, nil, nil, ],
|
631
|
+
[ nil, nil, nil, ],
|
632
|
+
[ nil, nil, nil, ],
|
633
|
+
[ nil, nil, nil, ],
|
634
|
+
[ nil, nil, nil, ],
|
635
|
+
[ nil, nil, nil, ],
|
636
|
+
[ nil, nil, nil, ],
|
637
|
+
[ nil, nil, nil, ],
|
638
|
+
[ nil, nil, nil, ],
|
639
|
+
[ nil, nil, nil, ],
|
640
|
+
[ nil, nil, nil, ],
|
641
|
+
]
|
642
|
+
def goto_table; GOTO_TABLE; end
|
643
|
+
|
644
|
+
def initialize g_parser, master
|
645
|
+
super()
|
646
|
+
@g_parser = g_parser
|
647
|
+
@d_parser = g_parser.d_parser
|
648
|
+
@master = master
|
649
|
+
@decorated = Action.new(self)
|
650
|
+
end
|
651
|
+
|
652
|
+
|
653
|
+
def do_parse?
|
654
|
+
if @line.match(/^\s*\[/)
|
655
|
+
|
656
|
+
true
|
657
|
+
else
|
658
|
+
false
|
659
|
+
end
|
660
|
+
end
|
661
|
+
|
662
|
+
|
663
|
+
def banner
|
664
|
+
"[> ...] / Depager::StatefulLexerExtension"
|
665
|
+
end
|
666
|
+
|
667
|
+
def lex
|
668
|
+
begin
|
669
|
+
until @line.empty?
|
670
|
+
case @line
|
671
|
+
when /\A\s+/
|
672
|
+
@line = $'
|
673
|
+
|
674
|
+
|
675
|
+
when /\A:[a-zA-Z_]+\??/
|
676
|
+
@line = $'
|
677
|
+
yield token(:SYMBOL, $&)
|
678
|
+
|
679
|
+
when /\A\]/
|
680
|
+
@line = $'
|
681
|
+
yield token($&, $&); @line = $'; yield nil, nil
|
682
|
+
|
683
|
+
when /\A./
|
684
|
+
@line = $'
|
685
|
+
yield token($&, $&)
|
686
|
+
|
687
|
+
|
688
|
+
else
|
689
|
+
raise RuntimeError, "must not happen #{@line}"
|
690
|
+
end
|
691
|
+
end
|
692
|
+
end while @original_line = @line = file.gets
|
693
|
+
yield nil, nil
|
694
|
+
end
|
695
|
+
|
696
|
+
end
|
697
|
+
end
|
698
|
+
|
699
|
+
class Depager::StatefulLexerExtension::PreRhsPostSymbol1::Action < Depager::LALR::Action #:nodoc:all
|
700
|
+
include Depager::Utils::ExtensionSlaveDecoratorMethods
|
701
|
+
ON_REDUCE = [
|
702
|
+
nil,
|
703
|
+
:_act_1,
|
704
|
+
:_act_2,
|
705
|
+
:_act_3,
|
706
|
+
:_act_4,
|
707
|
+
:_act_5,
|
708
|
+
:_act_6,
|
709
|
+
:_act_7,
|
710
|
+
|
711
|
+
]
|
712
|
+
def on_reduce; ON_REDUCE; end
|
713
|
+
|
714
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 180
|
715
|
+
def _act_1 val
|
716
|
+
_, _option_list, _, = *val
|
717
|
+
|
718
|
+
master.ins.push [g_parser.rhs.size, _option_list, false]
|
719
|
+
|
720
|
+
end
|
721
|
+
DEPAGER_EXPANDED_CODE
|
722
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 184
|
723
|
+
def _act_2 val
|
724
|
+
_, _option_list, _, _, = *val
|
725
|
+
|
726
|
+
master.ins.push [g_parser.rhs.size, _option_list, true]
|
727
|
+
|
728
|
+
end
|
729
|
+
DEPAGER_EXPANDED_CODE
|
730
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 190
|
731
|
+
def _act_3 val
|
732
|
+
_option, = *val
|
733
|
+
[_option]
|
734
|
+
|
735
|
+
end
|
736
|
+
DEPAGER_EXPANDED_CODE
|
737
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 191
|
738
|
+
def _act_4 val
|
739
|
+
_option_list, _option, = *val
|
740
|
+
_option_list << _option
|
741
|
+
|
742
|
+
end
|
743
|
+
DEPAGER_EXPANDED_CODE
|
744
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 194
|
745
|
+
def _act_5 val
|
746
|
+
_, _SYMBOL, = *val
|
747
|
+
[:GOTO, _SYMBOL.value]
|
748
|
+
|
749
|
+
end
|
750
|
+
DEPAGER_EXPANDED_CODE
|
751
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 195
|
752
|
+
def _act_6 val
|
753
|
+
_, _SYMBOL, = *val
|
754
|
+
[:ADD, _SYMBOL.value]
|
755
|
+
|
756
|
+
end
|
757
|
+
DEPAGER_EXPANDED_CODE
|
758
|
+
module_eval <<-'DEPAGER_EXPANDED_CODE', 'lib/depager/plugins/slex.dr', 196
|
759
|
+
def _act_7 val
|
760
|
+
_, _SYMBOL, = *val
|
761
|
+
[:SUB, _SYMBOL.value]
|
762
|
+
|
763
|
+
end
|
764
|
+
DEPAGER_EXPANDED_CODE
|
765
|
+
|
766
|
+
|
767
|
+
end
|
768
|
+
|
769
|
+
|