depager 0.2.3 → 0.3.0.b20160729
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.en +4 -19
- data/README.ja +42 -79
- data/bin/depager +42 -45
- data/examples/action_pl0d/pl0d.action.dr +421 -0
- data/examples/action_pl0d/test.pl0ds +49 -0
- data/examples/c89/c89.dr +493 -496
- data/examples/c89/test.c89 +10 -10
- data/examples/extension/astdf.rb +10 -0
- data/examples/extension/atree.dr +55 -0
- data/examples/{sample_calc → extension}/calc.atree.dr +42 -43
- data/examples/extension/calc.simple_action.dr +33 -0
- data/examples/extension/paction.dr +16 -15
- data/examples/extension/pactiontest.dr +14 -14
- data/examples/extension/simple_action.rb +44 -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/sample_calc/calc.action.dr +33 -33
- data/examples/sample_calc/calc.ast.action.dr +65 -66
- data/examples/sample_calc/calc.ast.dr +55 -55
- data/examples/sample_calc/calc.cst.dr +45 -45
- data/examples/sample_calc/calc.dr +43 -43
- data/examples/sample_calc/calc.lex.dr +29 -29
- data/examples/sample_calc/{calc_prec.nvaction.dr → calc_prec.action.dr} +31 -31
- 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/lib/depager.rb +582 -670
- data/lib/depager/grammar.rb +256 -291
- data/lib/depager/lr.rb +574 -579
- data/lib/depager/parser.rb +282 -277
- data/lib/depager/ruby/plugins/_rie_debug.rb +35 -0
- data/lib/depager/ruby/plugins/action.rb +53 -43
- data/lib/depager/ruby/plugins/ast.dr +364 -269
- data/lib/depager/ruby/plugins/ast.rb +1367 -1308
- data/lib/depager/ruby/plugins/cst.dr +172 -180
- data/lib/depager/ruby/plugins/cst.rb +587 -626
- data/lib/depager/ruby/plugins/lex.dr +85 -89
- data/lib/depager/ruby/plugins/lex.rb +310 -336
- data/lib/depager/ruby/plugins/rie.dr +723 -0
- data/lib/depager/ruby/plugins/rie.rb +1653 -0
- data/lib/depager/ruby/plugins/slex.dr +202 -200
- data/lib/depager/ruby/plugins/slex.rb +780 -817
- data/lib/depager/ruby/plugins/srp.rb +56 -51
- data/lib/depager/ruby/templates/extension_lalr_master.erb +46 -51
- data/lib/depager/ruby/templates/extension_lalr_slave.erb +99 -107
- data/lib/depager/ruby/templates/single_lalr_parser.erb +115 -117
- data/lib/depager/utils.rb +148 -318
- data/lib/depager/version.rb +4 -3
- metadata +52 -60
- 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.tab.rb +0 -644
- data/examples/sample_calc/calc.lex.tab.rb +0 -374
- data/examples/sample_calc/calc.nvaction.dr +0 -33
- 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/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/nvaction.rb +0 -19
- data/lib/depager/ruby/templates/simple.erb +0 -23
- data/setup.rb +0 -1585
@@ -0,0 +1,35 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'pp'
|
3
|
+
class Depager::RieExtension
|
4
|
+
module Nodes
|
5
|
+
def self.defnode name, *args
|
6
|
+
code = ''; args.each_with_index{|a, x|
|
7
|
+
code << "def #{a};@_values[#{x}];end; def #{a}=(v);@_values[#{x}]=v;end;"
|
8
|
+
}; module_eval %{
|
9
|
+
class #{name}; attr_accessor :lineno, :_values; #{code}
|
10
|
+
def self.[] *args ; self.new *args ; end
|
11
|
+
def initialize n, *as; @lineno, @_values = n, as; end
|
12
|
+
def inspect ; "<#{name}:\#{@_values.inspect}>"; end
|
13
|
+
def pretty_print q ; q.group(1, "<#{name}:", ">"){ q.pp @_values }; end
|
14
|
+
end
|
15
|
+
} # end of eval
|
16
|
+
end
|
17
|
+
defnode :N_THREAD, :targets, :excepts
|
18
|
+
defnode :N_TRANSFER, :targets, :excepts
|
19
|
+
defnode :N_EXCEPT, :let_or_ids
|
20
|
+
defnode :N_LET, :atoc, :expr, :state
|
21
|
+
defnode :N_ACTION, :expr
|
22
|
+
defnode :N_ATOC, :sym_name, :attr_name, :rx
|
23
|
+
|
24
|
+
class N_ATOC
|
25
|
+
def inspect
|
26
|
+
name = sym_name == '$' ? '$'+$grammar.symname($grammar[rx].lhs) : sym_name
|
27
|
+
"<N_ATOC:#{[name, attr_name, rx].inspect}>"
|
28
|
+
end
|
29
|
+
def pretty_print q
|
30
|
+
name = sym_name == '$' ? '$'+$grammar.symname($grammar[rx].lhs) : sym_name rescue nil
|
31
|
+
q.group(1, "<N_ATOC:", ">"){ q.pp [name, attr_name, rx] }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -1,43 +1,53 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
def
|
6
|
-
@
|
7
|
-
@
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
def pre_rule_list
|
16
|
-
if g_parser.
|
17
|
-
while line =
|
18
|
-
break if line =~ /^%\}\s*$/
|
19
|
-
@
|
20
|
-
end
|
21
|
-
g_parser.update_context ''
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def post_rhs
|
26
|
-
if g_parser.line =~ /\s*\{/
|
27
|
-
@
|
28
|
-
lineno = g_parser.lineno
|
29
|
-
code, d = modify_action_code(
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
@
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'plugins/action.rb'
|
3
|
+
|
4
|
+
class Depager::ActionExtension < Depager::Extension
|
5
|
+
def init_extension
|
6
|
+
@action_code = ''
|
7
|
+
@on_reduce = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def term_extension
|
11
|
+
g_parser.outer_code <<
|
12
|
+
generate_action_decorator_code(@on_reduce, @action_code)
|
13
|
+
end
|
14
|
+
|
15
|
+
def pre_rule_list
|
16
|
+
if g_parser.original_line =~ /^%ACTION\{\s*$/
|
17
|
+
while line = file.gets
|
18
|
+
break if line =~ /^%\}\s*$/
|
19
|
+
@action_code << line
|
20
|
+
end
|
21
|
+
g_parser.update_context ''
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def post_rhs
|
26
|
+
if g_parser.line =~ /\s*\{/
|
27
|
+
@original_line, @line = g_parser.original_line, g_parser.line
|
28
|
+
lineno = g_parser.file.lineno
|
29
|
+
code, d = modify_action_code(parse_block)
|
30
|
+
n = g_parser.rules.size - 1
|
31
|
+
@action_code <<
|
32
|
+
expand_inline_code(code, lineno, :wrap => "def _act_#{n} val", :delta => d)
|
33
|
+
g_parser.update_context @line
|
34
|
+
@on_reduce[n] = ":_act_#{n}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def modify_action_code code
|
39
|
+
let_code = ""
|
40
|
+
if g_parser.rhs_names.size > 0
|
41
|
+
let_code = g_parser.rhs_names.map do |name|
|
42
|
+
if name =~ /[a-zA-Z][a-zA-Z0-9_]*/
|
43
|
+
"_#{name}"
|
44
|
+
else
|
45
|
+
'_'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
let_code = " #{let_code.join(', ')}, = *val \n"
|
49
|
+
end
|
50
|
+
return let_code << code, 1
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
@@ -1,269 +1,364 @@
|
|
1
|
-
%defext ASTBuilderExtension
|
2
|
-
%extend Lexer ('plugins/lex.rb')
|
3
|
-
%extend
|
4
|
-
%decorate @
|
5
|
-
#%decorate ShiftReducePrinter ('plugins/srp.rb')
|
6
|
-
%
|
7
|
-
|
8
|
-
attr_accessor :
|
9
|
-
def
|
10
|
-
|
11
|
-
@
|
12
|
-
@
|
13
|
-
@
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
%
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
}
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
end
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
| pass_action_list
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
%
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
'
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
;
|
226
|
-
|
227
|
-
|
228
|
-
|
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
|
250
|
-
;
|
251
|
-
|
252
|
-
|
253
|
-
|
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
1
|
+
%defext Depager::ASTBuilderExtension
|
2
|
+
%extend Depager::Lexer ('plugins/lex.rb')
|
3
|
+
%extend Depager::Action ('plugins/action.rb')
|
4
|
+
%decorate @Action
|
5
|
+
#%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
|
6
|
+
%expanded_code_delimiter DEPAGER_EXPANDED_CODE
|
7
|
+
%inner{
|
8
|
+
attr_accessor :action_code, :on_reduce, :visitors
|
9
|
+
def init_extension
|
10
|
+
@visitors = Hash.new{|hash, key| hash[key] = []}
|
11
|
+
@action_code = ''
|
12
|
+
@on_reduce = []
|
13
|
+
@output_file_name = nil
|
14
|
+
end
|
15
|
+
def term_extension
|
16
|
+
g_parser.outer_code <<
|
17
|
+
generate_action_decorator_code(@on_reduce, @action_code)
|
18
|
+
end
|
19
|
+
|
20
|
+
def modify_action_code code, nodes=[]
|
21
|
+
code = code.gsub(/\$\.([a-z_])/, 'node.\1')
|
22
|
+
code << %{
|
23
|
+
rescue
|
24
|
+
warn "raise at src:\#{node.lineno}/\#{node.class.name}"
|
25
|
+
raise
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
def gen_accept_code sym
|
30
|
+
return ''
|
31
|
+
end
|
32
|
+
|
33
|
+
def node_name name
|
34
|
+
name = name == '@List' ? "NodeList" : "Node_#{name}"
|
35
|
+
"#{target_namespace}::#{name}"
|
36
|
+
end
|
37
|
+
|
38
|
+
attr_reader :output_file_name
|
39
|
+
def output_file_name= name
|
40
|
+
name = name.match(/'(.+)'/) ? $1 : name
|
41
|
+
@output_file_name = name
|
42
|
+
end
|
43
|
+
%}
|
44
|
+
%hook pre_rule_list /%AST\{\s*\Z/ skip
|
45
|
+
%banner '%AST{ ... }'
|
46
|
+
%%
|
47
|
+
|
48
|
+
%LEX{
|
49
|
+
/\s+/, /#.*/ { }
|
50
|
+
/%\}\s*\Z/ { @line = $'; yield nil,nil }
|
51
|
+
'Node' { yield token(:NODE, $&) }
|
52
|
+
'Visitor' { yield token(:VISITOR, $&) }
|
53
|
+
/[a-zA-Z][a-zA-Z0-9_]*/ { yield token(:ID, $&) }
|
54
|
+
'{' ! { lineno = file.lineno; yield token(:ACTION, parse_block, lineno) }
|
55
|
+
/./ { yield token($&, $&) }
|
56
|
+
%}
|
57
|
+
|
58
|
+
#begin-rule
|
59
|
+
start:
|
60
|
+
opt_defnode opt_defvis defnode_list
|
61
|
+
{
|
62
|
+
code = "module #{target_namespace}\n"
|
63
|
+
code << _opt_defnode
|
64
|
+
code << val[2]
|
65
|
+
master.visitors.each do |name, body|
|
66
|
+
code << ERB.new(master.class::VISITOR_TEMPLATE, nil, '-').result(binding)
|
67
|
+
end
|
68
|
+
code << "end\n"
|
69
|
+
if master.output_file_name
|
70
|
+
File.open(master.output_file_name, 'w') do |f|
|
71
|
+
f.write code
|
72
|
+
end
|
73
|
+
g_parser.outer_code << %!require '#{master.output_file_name}'\n!
|
74
|
+
else
|
75
|
+
g_parser.outer_code << code
|
76
|
+
end
|
77
|
+
}
|
78
|
+
;
|
79
|
+
opt_defnode:
|
80
|
+
{ "" }
|
81
|
+
| NODE opt_attr ACTION
|
82
|
+
{
|
83
|
+
ini = %{
|
84
|
+
def initialize
|
85
|
+
#{_ACTION.value}
|
86
|
+
end
|
87
|
+
};#code
|
88
|
+
ERB.new(master.class::BASE_NODE_TEMPLATE, nil, '-').result(binding)
|
89
|
+
}
|
90
|
+
;
|
91
|
+
opt_defvis:
|
92
|
+
{ [] }
|
93
|
+
| VISITOR opt_action
|
94
|
+
{
|
95
|
+
_opt_action[nil] ||= Token.new('', 0)
|
96
|
+
_opt_action.each do |pass, act|
|
97
|
+
master.visitors[pass].unshift(
|
98
|
+
master.expand_inline_code(act.value.to_s, act.lineno))
|
99
|
+
end
|
100
|
+
}
|
101
|
+
;
|
102
|
+
defnode_list:
|
103
|
+
defnode { _defnode }
|
104
|
+
| defnode_list defnode { _defnode_list << _defnode}
|
105
|
+
;
|
106
|
+
defnode:
|
107
|
+
defnode_header opt_attr opt_action
|
108
|
+
{
|
109
|
+
name, args = _defnode_header
|
110
|
+
nodes, accept, attrs = [], '', _opt_attr
|
111
|
+
args.each do |i|
|
112
|
+
if i =~ /\-(.*)/
|
113
|
+
nodes.push $1
|
114
|
+
attrs.push $1
|
115
|
+
else
|
116
|
+
nodes.push i
|
117
|
+
accept << master.gen_accept_code(i)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
_opt_action[nil] = Token.new('', 0) unless _opt_action[nil]
|
121
|
+
_opt_action.each do |pass, act|
|
122
|
+
vis_code = master.modify_action_code(act.value, nodes - attrs)
|
123
|
+
master.visitors[pass] <<
|
124
|
+
master.expand_inline_code(vis_code, act.lineno, :wrap => "def visit_Node_#{name} node")
|
125
|
+
end
|
126
|
+
ERB.new(master.class::NODE_TEMPLATE, nil, '-').result(binding)
|
127
|
+
}
|
128
|
+
;
|
129
|
+
defnode_header:
|
130
|
+
ID '(' ')' { [_ID.value, []]}
|
131
|
+
| ID '(' fact_list ')' { [_ID.value, _fact_list] }
|
132
|
+
;
|
133
|
+
opt_attr:
|
134
|
+
{ [] }
|
135
|
+
| '[' ']' { [] }
|
136
|
+
| '[' id_list ']' { _id_list }
|
137
|
+
;
|
138
|
+
fact_list:
|
139
|
+
fact { [_fact] }
|
140
|
+
| fact_list ',' fact { _fact_list.push _fact }
|
141
|
+
;
|
142
|
+
fact:
|
143
|
+
ID { _ID.value }
|
144
|
+
| '-' ID { '-' + _ID.value }
|
145
|
+
;
|
146
|
+
id_list:
|
147
|
+
ID { [ _ID.value ] }
|
148
|
+
| id_list ',' ID { _id_list.push _ID.value }
|
149
|
+
;
|
150
|
+
opt_action:
|
151
|
+
{ {nil => Token.new('', 0)} }
|
152
|
+
| ACTION { {nil => _ACTION} }
|
153
|
+
| pass_action_list
|
154
|
+
{
|
155
|
+
Hash[ *_pass_action_list.flatten ].merge({nil => Token.new('', 0)})
|
156
|
+
}
|
157
|
+
;
|
158
|
+
pass_action_list:
|
159
|
+
pass_action { [ _pass_action ] }
|
160
|
+
| pass_action_list pass_action { _pass_action_list << _pass_action }
|
161
|
+
;
|
162
|
+
|
163
|
+
pass_action:
|
164
|
+
'<' ID '>' ACTION { [_ID.value, _ACTION]}
|
165
|
+
;
|
166
|
+
#end-rule
|
167
|
+
%%
|
168
|
+
%hook post_rhs /=>/ skip
|
169
|
+
%banner '=>...'
|
170
|
+
%prec{
|
171
|
+
left LL '@'
|
172
|
+
%}
|
173
|
+
%inner{
|
174
|
+
def do_default
|
175
|
+
n = g_parser.rules.size-1
|
176
|
+
master.action_code << %{
|
177
|
+
def _ast_#{n} val
|
178
|
+
NilNode.new(basis.file.lineno)
|
179
|
+
end
|
180
|
+
}.unindent #code
|
181
|
+
master.on_reduce[n] = ":_ast_#{n}"
|
182
|
+
end
|
183
|
+
%}
|
184
|
+
%%
|
185
|
+
|
186
|
+
%LEX{
|
187
|
+
/[ \t]+/ { }
|
188
|
+
/\r?\n/ { yield nil, nil }
|
189
|
+
/<</ { yield token(:LL, $&) }
|
190
|
+
/%([a-zA-Z0-9_]+)((::[a-zA-Z0-9_]+)*)/
|
191
|
+
{ yield token(:CONST, $1+$2) }
|
192
|
+
/%\((.+)\)%/ { yield token(:EMBED, $1) }
|
193
|
+
'nil' { yield token(:NIL, $&) }
|
194
|
+
'NilNode' { yield token(:NILNODE, $&) }
|
195
|
+
/[0-9]+/ { yield token(:NUMBER, $&.to_i) }
|
196
|
+
/:[a-zA-Z0-9_]+/ { yield token(:SYMBOL, $&) }
|
197
|
+
/[a-zA-Z][a-zA-Z0-9_]*/ { yield token(:ID, $&) }
|
198
|
+
/'(.+)'/ { yield token(:STR, $1) }
|
199
|
+
/./ { yield token($&, $&) }
|
200
|
+
%}
|
201
|
+
#begin-rule
|
202
|
+
start:
|
203
|
+
fnl
|
204
|
+
{
|
205
|
+
n = g_parser.rules.size-1
|
206
|
+
master.action_code <<
|
207
|
+
master.expand_inline_code(_fnl, basis.file.lineno, :wrap => "def _ast_#{n} val")
|
208
|
+
master.on_reduce[n] = ":_ast_#{n}"
|
209
|
+
}
|
210
|
+
;
|
211
|
+
node:
|
212
|
+
ID '(' ')' { "#{master.node_name _ID.value}.new(basis.file.lineno)" }
|
213
|
+
| ID '(' fnlpair ')'
|
214
|
+
{
|
215
|
+
if g_parser.rhs.size > 0
|
216
|
+
"#{master.node_name _ID.value}.new(val[0].lineno, #{_fnlpair.join(', ')})"
|
217
|
+
else
|
218
|
+
"#{master.node_name _ID.value}.new(basis.file.lineno, #{_fnlpair.join(', ')})"
|
219
|
+
end
|
220
|
+
}
|
221
|
+
;
|
222
|
+
fnlpair:
|
223
|
+
fnl { [_fnl] }
|
224
|
+
| fnlpair ',' fnl { _fnlpair << _fnl }
|
225
|
+
;
|
226
|
+
fnl:
|
227
|
+
fact { _fact }
|
228
|
+
| node { _node }
|
229
|
+
| list { _list }
|
230
|
+
;
|
231
|
+
fact:
|
232
|
+
ID opt_embed
|
233
|
+
{
|
234
|
+
unless i = g_parser.name_to_rhs_index(_ID.value)
|
235
|
+
warn "#{g_parser.lineno}: ?#{_ID.value}"
|
236
|
+
raise
|
237
|
+
end
|
238
|
+
"val[#{i}]#{val[1]}"
|
239
|
+
}
|
240
|
+
| CONST { "Depager::Token.new(#{_CONST.value})" }
|
241
|
+
| SYMBOL { "Depager::Token.new(#{_SYMBOL.value})" }
|
242
|
+
| STR { "Depager::Token.new('#{_STR.value}')" }
|
243
|
+
| NILNODE { "NilNode.new(basis.file.lineno)" }
|
244
|
+
| NUMBER { "Depager::Token.new(#{_NUMBER.value})" }
|
245
|
+
| NIL { "nil" }
|
246
|
+
;
|
247
|
+
opt_embed:
|
248
|
+
{ '' }
|
249
|
+
| EMBED { _EMBED.value }
|
250
|
+
;
|
251
|
+
list:
|
252
|
+
'['']' { "#{master.node_name '@List'}.new(basis.file.lineno, [])" }
|
253
|
+
| '['fnlpair']'
|
254
|
+
{
|
255
|
+
if g_parser.rhs.size > 0
|
256
|
+
"#{master.node_name '@List'}.new(val[0].lineno, [#{_fnlpair.join(', ')}])"
|
257
|
+
else
|
258
|
+
"#{master.node_name '@List'}.new(basis.file.lineno, #{_fnlpair.join(', ')})"
|
259
|
+
end
|
260
|
+
}
|
261
|
+
| fnl-l LL fnl-r { "#{_l}.push(#{_r})" }
|
262
|
+
| fnl-l '@' fnl-r { "#{_l}.concat(#{_r})" }
|
263
|
+
;
|
264
|
+
#end-rule
|
265
|
+
%%
|
266
|
+
%%
|
267
|
+
|
268
|
+
class Depager::ASTBuilderExtension
|
269
|
+
BASE_NODE_TEMPLATE = <<-'EOS'
|
270
|
+
class Node
|
271
|
+
attr_accessor :lineno
|
272
|
+
attr_accessor <%= _opt_attr.map{|i| ":#{i}" }.join(', ') %>
|
273
|
+
<%= ini %>
|
274
|
+
|
275
|
+
def self.[] lineno, *args
|
276
|
+
self.new lineno, *args
|
277
|
+
end
|
278
|
+
|
279
|
+
def accept v
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
class NodeList < Node
|
284
|
+
attr_accessor :lineno
|
285
|
+
def initialize(lineno, lst=[])
|
286
|
+
@lineno = lineno
|
287
|
+
@lst = lst.to_a.select{|i| ! i.is_a?(NilNode)}
|
288
|
+
end
|
289
|
+
|
290
|
+
<%- _opt_attr.each do |i| -%>
|
291
|
+
def all_<%= i %>
|
292
|
+
@lst.map{|i| i.<%= i %>}
|
293
|
+
end
|
294
|
+
<%- end -%>
|
295
|
+
|
296
|
+
def size
|
297
|
+
@lst.size
|
298
|
+
end
|
299
|
+
alias length size
|
300
|
+
|
301
|
+
def push(i)
|
302
|
+
@lst.push i unless i.is_a? NilNode
|
303
|
+
self
|
304
|
+
end
|
305
|
+
|
306
|
+
def concat(i)
|
307
|
+
@lst.concat i
|
308
|
+
self
|
309
|
+
end
|
310
|
+
|
311
|
+
def to_ary()
|
312
|
+
@lst
|
313
|
+
end
|
314
|
+
|
315
|
+
alias to_a to_ary
|
316
|
+
alias list to_ary
|
317
|
+
|
318
|
+
def accept(v)
|
319
|
+
@lst.each{|i| i.accept(v) }
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
class NilNode
|
324
|
+
attr_accessor :lineno
|
325
|
+
attr_accessor <%= _opt_attr.map{|i| ":#{i}" }.join(', ') %>
|
326
|
+
|
327
|
+
def initialize(lineno, *args)
|
328
|
+
@lineno = lineno
|
329
|
+
end
|
330
|
+
|
331
|
+
def accept v
|
332
|
+
end
|
333
|
+
end
|
334
|
+
EOS
|
335
|
+
|
336
|
+
NODE_TEMPLATE = <<-'EOS'
|
337
|
+
class Node_<%= name %> < Node
|
338
|
+
attr_accessor <%= nodes.map{|i| ":#{i}" }.join(', ') %>
|
339
|
+
attr_accessor <%= attrs.map{|i| ":#{i}" }.join(', ') %>
|
340
|
+
|
341
|
+
def initialize <%= nodes.unshift('lineno').join(', ') %>
|
342
|
+
super()
|
343
|
+
@lineno = lineno
|
344
|
+
<%= nodes.inject(''){|r,i| r << " @#{i} = #{i}\n"} %>
|
345
|
+
end
|
346
|
+
|
347
|
+
def accept v
|
348
|
+
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
349
|
+
<%= accept %>
|
350
|
+
v.visit_Node_<%= name %>(self)
|
351
|
+
self
|
352
|
+
end
|
353
|
+
end
|
354
|
+
EOS
|
355
|
+
|
356
|
+
VISITOR_TEMPLATE = <<-'EOS'
|
357
|
+
class Visitor<%= name ? '_' + name : '' %>
|
358
|
+
def visit node
|
359
|
+
node.accept(self)
|
360
|
+
end
|
361
|
+
<%= body.join %>
|
362
|
+
end # Visitor<%= name ? '_' + name : '' %>
|
363
|
+
EOS
|
364
|
+
end
|