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