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,29 +1,29 @@
|
|
1
|
-
%class TinyCalc
|
2
|
-
%extend Lexer ('plugins/lex.rb')
|
3
|
-
%%
|
4
|
-
|
5
|
-
%LEX{
|
6
|
-
/\s+/, /\#.*/, /\n/ { }
|
7
|
-
/[1-9][0-9]*/ { yield token(:NUM, $&.to_i) }
|
8
|
-
/./ { yield token($&, $&) }
|
9
|
-
%}
|
10
|
-
|
11
|
-
#begin-rule
|
12
|
-
expr :
|
13
|
-
expr '+' term
|
14
|
-
| expr '-' term
|
15
|
-
| term
|
16
|
-
;
|
17
|
-
term :
|
18
|
-
term '*' fact
|
19
|
-
| term '/' fact
|
20
|
-
| fact
|
21
|
-
;
|
22
|
-
fact :
|
23
|
-
NUM
|
24
|
-
| '(' expr ')'
|
25
|
-
;
|
26
|
-
#end-rule
|
27
|
-
%%
|
28
|
-
parser = TinyCalc.
|
29
|
-
parser.parse(STDIN)
|
1
|
+
%class TinyCalc::Parser
|
2
|
+
%extend Depager::Lexer ('plugins/lex.rb')
|
3
|
+
%%
|
4
|
+
|
5
|
+
%LEX{
|
6
|
+
/\s+/, /\#.*/, /\n/ { }
|
7
|
+
/[1-9][0-9]*/ { yield token(:NUM, $&.to_i) }
|
8
|
+
/./ { yield token($&, $&) }
|
9
|
+
%}
|
10
|
+
|
11
|
+
#begin-rule
|
12
|
+
expr :
|
13
|
+
expr '+' term
|
14
|
+
| expr '-' term
|
15
|
+
| term
|
16
|
+
;
|
17
|
+
term :
|
18
|
+
term '*' fact
|
19
|
+
| term '/' fact
|
20
|
+
| fact
|
21
|
+
;
|
22
|
+
fact :
|
23
|
+
NUM
|
24
|
+
| '(' expr ')'
|
25
|
+
;
|
26
|
+
#end-rule
|
27
|
+
%%
|
28
|
+
parser = TinyCalc.create_decorated_parser()
|
29
|
+
parser.parse(STDIN)
|
@@ -1,31 +1,31 @@
|
|
1
|
-
%class TinyCalc
|
2
|
-
%extend Lexer ('plugins/lex.rb')
|
3
|
-
%extend
|
4
|
-
%decorate @
|
5
|
-
#%decorate ShiftReducePrinter ('plugins/srp.rb')
|
6
|
-
%prec{
|
7
|
-
left '*' '/'
|
8
|
-
left '+' '-'
|
9
|
-
%}
|
10
|
-
%%
|
11
|
-
|
12
|
-
%LEX{
|
13
|
-
/\s+/, /\#.*/ { }
|
14
|
-
/[1-9][0-9]*/ { yield token(:NUM, $&.to_i) }
|
15
|
-
/./ { yield token($&, $&) }
|
16
|
-
%}
|
17
|
-
|
18
|
-
#begin-rule
|
19
|
-
expr :
|
20
|
-
expr-left '+' expr-right { _left + _right }
|
21
|
-
| expr-left '-' expr-right { _left - _right }
|
22
|
-
| expr-left '*' expr-right { _left * _right }
|
23
|
-
| expr-left '/' expr-right { _left / _right }
|
24
|
-
| '(' expr ')' { _expr }
|
25
|
-
| NUM { _NUM.value }
|
26
|
-
;
|
27
|
-
#end-rule
|
28
|
-
%%
|
29
|
-
parser =
|
30
|
-
r, = parser.parse(STDIN)
|
31
|
-
puts r
|
1
|
+
%class TinyCalc::Parser
|
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
|
+
%prec{
|
7
|
+
left '*' '/'
|
8
|
+
left '+' '-'
|
9
|
+
%}
|
10
|
+
%%
|
11
|
+
|
12
|
+
%LEX{
|
13
|
+
/\s+/, /\#.*/ { }
|
14
|
+
/[1-9][0-9]*/ { yield token(:NUM, $&.to_i) }
|
15
|
+
/./ { yield token($&, $&) }
|
16
|
+
%}
|
17
|
+
|
18
|
+
#begin-rule
|
19
|
+
expr :
|
20
|
+
expr-left '+' expr-right { _left + _right }
|
21
|
+
| expr-left '-' expr-right { _left - _right }
|
22
|
+
| expr-left '*' expr-right { _left * _right }
|
23
|
+
| expr-left '/' expr-right { _left / _right }
|
24
|
+
| '(' expr ')' { _expr }
|
25
|
+
| NUM { _NUM.value }
|
26
|
+
;
|
27
|
+
#end-rule
|
28
|
+
%%
|
29
|
+
parser = TinyCalc.create_decorated_parser
|
30
|
+
r, = parser.parse(STDIN)
|
31
|
+
puts r
|
@@ -1,29 +1,29 @@
|
|
1
|
-
%class StatefulLexerTest1
|
2
|
-
%extend StatefulLexer ('plugins/slex.rb')
|
3
|
-
%extend
|
4
|
-
%decorate @
|
5
|
-
%decorate @StatefulLexer
|
6
|
-
#%decorate ShiftReducePrinter ('plugins/srp.rb')
|
7
|
-
%%
|
8
|
-
%LEX{
|
9
|
-
<START>
|
10
|
-
/\s+/ { }
|
11
|
-
/\/(([^\/\\]+|\\.)*)\// { yield token(:REGEXP, $&) }
|
12
|
-
/./ { yield token($&, $&) }
|
13
|
-
<OPMODE : START>
|
14
|
-
'/' { yield token($&, $&) }
|
15
|
-
%}
|
16
|
-
|
17
|
-
#begin-rule
|
18
|
-
expr:
|
19
|
-
expr '/' [>:START ?] fact
|
20
|
-
| fact
|
21
|
-
;
|
22
|
-
fact:
|
23
|
-
REGEXP [>:OPMODE ?]
|
24
|
-
{ warn "REGEXP: #{_REGEXP.value}" }
|
25
|
-
;
|
26
|
-
#end-rule
|
27
|
-
%%
|
28
|
-
p =
|
29
|
-
p.parse(STDIN)
|
1
|
+
%class StatefulLexerTest1::Parser
|
2
|
+
%extend Depager::StatefulLexer ('plugins/slex.rb')
|
3
|
+
%extend Depager::Action ('plugins/action.rb')
|
4
|
+
%decorate @Action
|
5
|
+
%decorate @StatefulLexer
|
6
|
+
#%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
|
7
|
+
%%
|
8
|
+
%LEX{
|
9
|
+
<START>
|
10
|
+
/\s+/ { }
|
11
|
+
/\/(([^\/\\]+|\\.)*)\// { yield token(:REGEXP, $&) }
|
12
|
+
/./ { yield token($&, $&) }
|
13
|
+
<OPMODE : START>
|
14
|
+
'/' { yield token($&, $&) }
|
15
|
+
%}
|
16
|
+
|
17
|
+
#begin-rule
|
18
|
+
expr:
|
19
|
+
expr '/' [>:START ?] fact
|
20
|
+
| fact
|
21
|
+
;
|
22
|
+
fact:
|
23
|
+
REGEXP [>:OPMODE ?]
|
24
|
+
{ warn "REGEXP: #{_REGEXP.value}" }
|
25
|
+
;
|
26
|
+
#end-rule
|
27
|
+
%%
|
28
|
+
p = StatefulLexerTest1.create_decorated_parser
|
29
|
+
p.parse(STDIN)
|
@@ -1,36 +1,36 @@
|
|
1
|
-
%class StatefulLexerTest2
|
2
|
-
%extend StatefulLexer ('plugins/slex.rb')
|
3
|
-
%extend
|
4
|
-
%decorate @
|
5
|
-
%decorate @StatefulLexer
|
6
|
-
#%decorate ShiftReducePrinter ('plugins/srp.rb')
|
7
|
-
%%
|
8
|
-
%LEX{
|
9
|
-
<START>
|
10
|
-
/[ \t]+/ { }
|
11
|
-
/\n/
|
12
|
-
/\w+/ { yield token(:ID, $&) }
|
13
|
-
/./ { yield token($&, $&) }
|
14
|
-
<LCONT : START>
|
15
|
-
/\r?\n/ { }
|
16
|
-
%}
|
17
|
-
|
18
|
-
#begin-rule
|
19
|
-
stmts:
|
20
|
-
| stmts expr EOL
|
21
|
-
{ warn "#{_expr} <EOL>:#{@lexstate}" }
|
22
|
-
;
|
23
|
-
expr:
|
24
|
-
expr '+' [>:LCONT] fact
|
25
|
-
{ "#{_expr} + #{_fact}" }
|
26
|
-
| fact
|
27
|
-
{ _fact }
|
28
|
-
;
|
29
|
-
fact:
|
30
|
-
ID [>:START]
|
31
|
-
{ _ID.value }
|
32
|
-
;
|
33
|
-
#end-rule
|
34
|
-
%%
|
35
|
-
p =
|
36
|
-
p.parse(STDIN)
|
1
|
+
%class StatefulLexerTest2::Parser
|
2
|
+
%extend Depager::StatefulLexer ('plugins/slex.rb')
|
3
|
+
%extend Depager::Action ('plugins/action.rb')
|
4
|
+
%decorate @Action
|
5
|
+
%decorate @StatefulLexer
|
6
|
+
#%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
|
7
|
+
%%
|
8
|
+
%LEX{
|
9
|
+
<START>
|
10
|
+
/[ \t]+/ { }
|
11
|
+
/\r?\n/ { yield token(:EOL, $&) }
|
12
|
+
/\w+/ { yield token(:ID, $&) }
|
13
|
+
/./ { yield token($&, $&) }
|
14
|
+
<LCONT : START>
|
15
|
+
/\r?\n/ { }
|
16
|
+
%}
|
17
|
+
|
18
|
+
#begin-rule
|
19
|
+
stmts:
|
20
|
+
| stmts expr EOL
|
21
|
+
{ warn "#{_expr} <EOL>:#{@lexstate}" }
|
22
|
+
;
|
23
|
+
expr:
|
24
|
+
expr '+' [>:LCONT] fact
|
25
|
+
{ "#{_expr} + #{_fact}" }
|
26
|
+
| fact
|
27
|
+
{ _fact }
|
28
|
+
;
|
29
|
+
fact:
|
30
|
+
ID [>:START]
|
31
|
+
{ _ID.value }
|
32
|
+
;
|
33
|
+
#end-rule
|
34
|
+
%%
|
35
|
+
p = StatefulLexerTest2.create_decorated_parser
|
36
|
+
p.parse(STDIN)
|
@@ -1 +1 @@
|
|
1
|
-
/aaa/ / /bbb/
|
1
|
+
/aaa/ / /bbb/
|
@@ -1,3 +1,3 @@
|
|
1
|
-
x +
|
2
|
-
y +
|
3
|
-
z
|
1
|
+
x +
|
2
|
+
y +
|
3
|
+
z
|
data/lib/depager.rb
CHANGED
@@ -1,670 +1,582 @@
|
|
1
|
-
|
2
|
-
require
|
3
|
-
require "depager/
|
4
|
-
require "depager/
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
end
|
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
|
-
def
|
135
|
-
|
136
|
-
end
|
137
|
-
|
138
|
-
def
|
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
|
-
parse_rhslist
|
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
|
-
when
|
248
|
-
|
249
|
-
when
|
250
|
-
|
251
|
-
val =
|
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
|
-
end
|
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
|
-
def
|
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
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
g_parser.parse(@d_parser.target_name, @mixin, @precs)
|
584
|
-
@optmain.push parse_block
|
585
|
-
return out_code(g_parser)
|
586
|
-
else
|
587
|
-
parse_common(line)
|
588
|
-
end
|
589
|
-
end
|
590
|
-
return nil
|
591
|
-
end
|
592
|
-
end
|
593
|
-
|
594
|
-
class ExtensionGenerator < Generator
|
595
|
-
TmplfileMaster = '**NOTHING**'
|
596
|
-
TmplfileSlave = '**NOTHING**'
|
597
|
-
attr_accessor :regs, :paramkey
|
598
|
-
def initialize d_parser
|
599
|
-
super
|
600
|
-
@parsing_method = Simple
|
601
|
-
@basis_name = 'self'
|
602
|
-
@paramkey = nil
|
603
|
-
@regs = { }
|
604
|
-
end
|
605
|
-
|
606
|
-
def parse_decl
|
607
|
-
until eof?
|
608
|
-
line = getline
|
609
|
-
case line
|
610
|
-
when /^%hook\s*(.*?)\s*$/
|
611
|
-
hs = $1
|
612
|
-
mtsk = if hs =~ /\/(([^\/\\]+|\\.)*)\/\s*(skip)?$/
|
613
|
-
hs = $`
|
614
|
-
[$1, $3 ? true : false]
|
615
|
-
end
|
616
|
-
hook = hs.split(' ')
|
617
|
-
hookname = hook.join('__')
|
618
|
-
hook.each do |i| i = i.to_sym
|
619
|
-
raise unless GrammarParser.hook_name? i
|
620
|
-
@regs[i] = "#{@d_parser.target_name}_#{hookname}"
|
621
|
-
end
|
622
|
-
@optouter.push(parse_hook(hookname, mtsk))
|
623
|
-
when /^%param\s*(.*)\s*$/
|
624
|
-
@paramkey = $1
|
625
|
-
else
|
626
|
-
parse_common(line)
|
627
|
-
end
|
628
|
-
end
|
629
|
-
return out_master_code
|
630
|
-
end
|
631
|
-
|
632
|
-
def parse_hook hookname, mtsk
|
633
|
-
inner = ''
|
634
|
-
precs = []
|
635
|
-
banner = nil
|
636
|
-
mixin = [].concat(@mixin)
|
637
|
-
target_name = "#{@d_parser.target_name}_#{hookname}"
|
638
|
-
until eof?
|
639
|
-
line = getline
|
640
|
-
case line
|
641
|
-
when /^%banner\s*'(([^'\\]+|\\.)*)'\s*$/
|
642
|
-
banner = $1
|
643
|
-
when /^%inner\s*\{\s*$/
|
644
|
-
inner = parse_block
|
645
|
-
when /^%mixin\s*(.+?)\s*(\((.+)\)\s*)?$/
|
646
|
-
mixin.push $1
|
647
|
-
@req.push $3 if $3
|
648
|
-
when /^%prec\s*(.*?)\s*$/
|
649
|
-
precs = parse_prec
|
650
|
-
when /^%%\s*$/
|
651
|
-
g_parser = GrammarParser.new(@d_parser)
|
652
|
-
g_parser.extend_paser @ext, @options
|
653
|
-
g_parser.parse(target_name, mixin, precs)
|
654
|
-
g_parser.optinner.push inner
|
655
|
-
return out_slave_code(g_parser, mixin, mtsk, banner)
|
656
|
-
else
|
657
|
-
warning "syntax error(declaration).\n> #{line}", lineno
|
658
|
-
end
|
659
|
-
end
|
660
|
-
end
|
661
|
-
|
662
|
-
def out_slave_code g_parser, mixin, mtsk, banner
|
663
|
-
ERB.new(File.read(self.class::TmplfileSlave)).result(binding)
|
664
|
-
end
|
665
|
-
def out_master_code
|
666
|
-
ERB.new(File.read(self.class::TmplfileMaster)).result(binding)
|
667
|
-
end
|
668
|
-
end
|
669
|
-
end
|
670
|
-
end
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'erb'
|
3
|
+
require "depager/parser.rb"
|
4
|
+
require "depager/grammar.rb"
|
5
|
+
require "depager/utils.rb"
|
6
|
+
|
7
|
+
module Depager
|
8
|
+
def self.configuration
|
9
|
+
@flags ||= {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.debug_mode? flag = ''
|
13
|
+
value = configuration[:debug] or return false
|
14
|
+
!! value.match(/#{flag.to_s}/)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.verbose_mode?
|
18
|
+
configuration[:verbose]
|
19
|
+
end
|
20
|
+
|
21
|
+
class DeclarationPartParser
|
22
|
+
include Depager::Utils::CommonMethods
|
23
|
+
|
24
|
+
attr_reader :file, :target_namespace, :target_name, :g_parser, :generator
|
25
|
+
|
26
|
+
def parse(file)
|
27
|
+
@file = file
|
28
|
+
@g_parser = GrammarPartParser.new(self)
|
29
|
+
|
30
|
+
until file.eof?
|
31
|
+
case line = file.gets
|
32
|
+
when /^\s*(#.*)?$/
|
33
|
+
#skip space and comment.
|
34
|
+
when /^%class\s+(\S+)(\s+based_on\s+(\S+)(\s+\(\s*'(.+)'\s*\))?)?\s*$/
|
35
|
+
@target_name = $1
|
36
|
+
if $2
|
37
|
+
require $5 if $5
|
38
|
+
nesting = $3.split(/::/).tap{|it| it[-1] = "#{it[-1]}Generator"}
|
39
|
+
generator_class = nesting.inject(Object){|c,name| c.const_get(name) }
|
40
|
+
else
|
41
|
+
require 'depager/lr.rb'
|
42
|
+
generator_class = LALR::ParserGenerator
|
43
|
+
end
|
44
|
+
|
45
|
+
if @target_name.match('(.+)::([^:]+)')
|
46
|
+
@target_namespace = $1
|
47
|
+
@target_name = $2
|
48
|
+
else
|
49
|
+
error_exit "Namespace required"
|
50
|
+
end
|
51
|
+
|
52
|
+
@generator = generator_class.new(self)
|
53
|
+
break
|
54
|
+
when /^%defext\s+(\S+)\s*$/
|
55
|
+
require 'depager/lr.rb'
|
56
|
+
|
57
|
+
@target_namespace = $1
|
58
|
+
@target_name = 'Master'
|
59
|
+
|
60
|
+
@generator = LALR::ExtensionGenerator.new(self)
|
61
|
+
break
|
62
|
+
else
|
63
|
+
error_exit "%class not found."
|
64
|
+
end
|
65
|
+
end
|
66
|
+
@generator.parse
|
67
|
+
end
|
68
|
+
|
69
|
+
def parsing_method
|
70
|
+
@generator.parsing_method
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class GrammarPartParser
|
75
|
+
include Depager::Utils::CommonMethods
|
76
|
+
|
77
|
+
Token = Struct.new(:tag, :value, :name, :lineno)
|
78
|
+
|
79
|
+
HOOK_KEYS = [
|
80
|
+
:pre_rule_list, :post_rule_list,
|
81
|
+
:pre_rule, :post_rule,
|
82
|
+
:post_lhs,
|
83
|
+
:post_rhs_list, :pre_rhs_list,
|
84
|
+
:pre_rhs, :post_rhs,
|
85
|
+
:post_symbol
|
86
|
+
] #:nodoc:
|
87
|
+
|
88
|
+
attr_accessor :rules, :terms, :nonterms, :precs, :table
|
89
|
+
attr_accessor :lhs, :rhs, :nrhs, :rhs_names, :nrules
|
90
|
+
attr_accessor :line, :original_line, :old_line, :token
|
91
|
+
attr_accessor :outer_code, :inner_code, :mixins
|
92
|
+
attr_reader :d_parser, :target_namespace, :target_name, :hooks
|
93
|
+
|
94
|
+
def initialize d_parser
|
95
|
+
@d_parser = d_parser
|
96
|
+
end
|
97
|
+
|
98
|
+
# parse and make LALR(1) table
|
99
|
+
def parse(full_target_name, precs = [], extensions = [], options = {})
|
100
|
+
@original_line = @old_line = @line = ''
|
101
|
+
@inner_code = ''
|
102
|
+
@outer_code = ''
|
103
|
+
|
104
|
+
full_target_name.match(/::([^:]+)$/) or raise
|
105
|
+
@target_namespace = $`
|
106
|
+
@target_name = $1
|
107
|
+
|
108
|
+
extend_paser(extensions, options)
|
109
|
+
init_grammar
|
110
|
+
parse_grammar
|
111
|
+
check_grammar
|
112
|
+
make_grammar precs
|
113
|
+
|
114
|
+
grammar = d_parser.parsing_method::Grammar.new(@rules, @terms, @nonterms, @precs)
|
115
|
+
@table = d_parser.parsing_method::Table.new(grammar)
|
116
|
+
@table.check_table d_parser
|
117
|
+
@extensions.each{|o| o.send :term_extension }
|
118
|
+
@table
|
119
|
+
end
|
120
|
+
|
121
|
+
def hook_name? s
|
122
|
+
HOOK_KEYS.include? s.to_sym
|
123
|
+
end
|
124
|
+
|
125
|
+
def update_context line
|
126
|
+
@line = line
|
127
|
+
get_token
|
128
|
+
end
|
129
|
+
|
130
|
+
def int_to_sym n
|
131
|
+
@int_to_sym[n]
|
132
|
+
end
|
133
|
+
|
134
|
+
def lhs_name
|
135
|
+
int_to_sym(lhs)
|
136
|
+
end
|
137
|
+
|
138
|
+
def add_nonterm sym
|
139
|
+
sym = sym.to_s.intern
|
140
|
+
isym = @nonterms[sym] = @nonterms.size
|
141
|
+
@int_to_sym[isym] = sym
|
142
|
+
return isym
|
143
|
+
end
|
144
|
+
|
145
|
+
def add_rule lhs, rhs, prec = nil, rhs_names = nil
|
146
|
+
@lhs_syms[lhs] = true
|
147
|
+
rule = @parsing_method::Rule[lhs, rhs, prec, rhs_names]
|
148
|
+
@rules.push rule
|
149
|
+
return rule
|
150
|
+
end
|
151
|
+
|
152
|
+
def name_to_rhs_index name
|
153
|
+
@rhs_names.index(name)
|
154
|
+
end
|
155
|
+
|
156
|
+
def insert_sym_to_rhs pos, token_value, token_name
|
157
|
+
if pos < 0
|
158
|
+
@rhs.push token_value
|
159
|
+
@rhs_names[@rhs.size - 1] = token_name
|
160
|
+
@rhs_syms[token_value] = true
|
161
|
+
else
|
162
|
+
@rhs.insert(pos, token_value)
|
163
|
+
@rhs_names.insert(pos, token_name)
|
164
|
+
@rhs_syms[token_value] = true
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
private
|
169
|
+
def parse_grammar
|
170
|
+
get_token
|
171
|
+
do_hook :pre_rule_list
|
172
|
+
parse_rulelist
|
173
|
+
do_hook :post_rule_list
|
174
|
+
unless @token.tag == nil
|
175
|
+
error_exit "syntax error(grammar). unexpected '#{@token.tag}' expect '%%'"
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def parse_rulelist
|
180
|
+
do_hook :pre_rule
|
181
|
+
parse_rule
|
182
|
+
do_hook :post_rule
|
183
|
+
if @token.tag == :NT
|
184
|
+
parse_rulelist
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def parse_rule
|
189
|
+
if @token[0] == :NT
|
190
|
+
@lhs = @token.value
|
191
|
+
|
192
|
+
get_token
|
193
|
+
do_hook :post_lhs
|
194
|
+
|
195
|
+
unless @token.tag == ':'
|
196
|
+
error_exit "syntax error(grammar). unexpected '#{@token.tag}' expecting ':'"
|
197
|
+
end
|
198
|
+
get_token
|
199
|
+
|
200
|
+
@nrhs = 0
|
201
|
+
do_hook :pre_rhs_list
|
202
|
+
parse_rhslist
|
203
|
+
do_hook :post_rhs_list
|
204
|
+
|
205
|
+
unless @token.tag == ';'
|
206
|
+
error_exit "syntax error(grammar). unexpected '#{@token.tag}' expecting ';'"
|
207
|
+
end
|
208
|
+
get_token
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
def parse_rhslist
|
213
|
+
@rhs = []; @rhs_names = []; @prec = nil
|
214
|
+
do_hook :pre_rhs
|
215
|
+
parse_syms
|
216
|
+
add_rule(@lhs, @rhs, @prec, @rhs_names)
|
217
|
+
do_hook :post_rhs
|
218
|
+
@nrhs += 1
|
219
|
+
|
220
|
+
if @token.tag == '|'
|
221
|
+
get_token
|
222
|
+
parse_rhslist
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
def parse_syms
|
227
|
+
if @token.tag == :NT || @token.tag == :T
|
228
|
+
insert_sym_to_rhs(-1, @token.value, @token.name)
|
229
|
+
get_token
|
230
|
+
|
231
|
+
do_hook :post_symbol
|
232
|
+
parse_syms
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
def make_token tag, value, name = nil
|
237
|
+
@token = Token.new(tag, value, name, file.lineno)
|
238
|
+
end
|
239
|
+
|
240
|
+
def get_token
|
241
|
+
while !@line.empty? || !file.eof?
|
242
|
+
@original_line = @old_line = @line = file.gets if @line.empty?
|
243
|
+
|
244
|
+
tag = s = name = val = nil
|
245
|
+
until @line.empty? do
|
246
|
+
case @line
|
247
|
+
when /^\s+/, /^\#.*/ ;
|
248
|
+
# skip
|
249
|
+
when /^=([a-zA-Z]+)/
|
250
|
+
@prec = s = $1.intern
|
251
|
+
val = @terms[s] ||= - (@terms.size + 1)
|
252
|
+
when /^([a-z][a-z0-9_]*)(-([a-z][_a-z0-9]*))?/
|
253
|
+
tag = :NT
|
254
|
+
s = $1.intern
|
255
|
+
name = $3 ? $3 : $1
|
256
|
+
val = @nonterms[s] ||= @nonterms.size
|
257
|
+
when /^([A-Z][A-Z0-9_]*)(-([a-z][_a-z0-9]*))?/
|
258
|
+
tag = :T
|
259
|
+
s = $1.intern
|
260
|
+
name = $3 ? $3 : $1
|
261
|
+
val = @terms[s] ||= - (@terms.size + 1)
|
262
|
+
when /^'(.+?)'/
|
263
|
+
tag = :T
|
264
|
+
name = s = $1
|
265
|
+
val = @terms[s] ||= - (@terms.size + 1)
|
266
|
+
when /^%%/
|
267
|
+
return make_token(nil, nil)
|
268
|
+
when /^([:;|])/
|
269
|
+
tag = $&
|
270
|
+
val = $&
|
271
|
+
else
|
272
|
+
return make_token(:UNKNOWN, @line[0].chr)
|
273
|
+
end
|
274
|
+
@old_line, @line = @line, $'
|
275
|
+
@int_to_sym[val] ||= s if val && s
|
276
|
+
return make_token(tag, val, name) if tag
|
277
|
+
end
|
278
|
+
end
|
279
|
+
return make_token(nil, nil)
|
280
|
+
end
|
281
|
+
|
282
|
+
def extend_paser extensions, options
|
283
|
+
init_hook
|
284
|
+
@extensions = []
|
285
|
+
extensions.each do |f, m|
|
286
|
+
if f && f.match(%r!^\./!)
|
287
|
+
require File.expand_path(f, File.dirname(file.path))
|
288
|
+
elsif f
|
289
|
+
require f
|
290
|
+
end
|
291
|
+
|
292
|
+
nesting = m.split(/::/).tap{|it| it[-1] = "#{it[-1]}Extension"}
|
293
|
+
extension = nesting.inject(Object){|c,name| c.const_get(name) }.new
|
294
|
+
options[m] and options[m].each do |k, v|
|
295
|
+
begin
|
296
|
+
e.send("#{k}=", v)
|
297
|
+
rescue NoMethodError
|
298
|
+
warning "no such a option '#{m}.#{k}'."
|
299
|
+
end
|
300
|
+
end
|
301
|
+
@extensions << extension
|
302
|
+
extension.extension_registered self
|
303
|
+
extension.init_extension
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
def init_grammar
|
308
|
+
@rules = [ d_parser.parsing_method::Rule[0 , [1]]]
|
309
|
+
@terms = { nil => -1, false => -2 }
|
310
|
+
@nonterms = { '$start' => 0 }
|
311
|
+
@int_to_sym = {}
|
312
|
+
@rhs_syms = {}
|
313
|
+
@lhs_syms = {}
|
314
|
+
end
|
315
|
+
|
316
|
+
def make_grammar precs
|
317
|
+
ts = @nonterms.size - 1
|
318
|
+
@terms.each{|k,v| @terms[k] = ts - v}
|
319
|
+
|
320
|
+
@precs = {}
|
321
|
+
precs.each_with_index do |p, x|
|
322
|
+
p[1].each do |i|
|
323
|
+
if @terms[i]
|
324
|
+
@precs[@terms[i]] = [p[0], x]
|
325
|
+
else
|
326
|
+
error_exit "prec error '#{i}'."
|
327
|
+
end
|
328
|
+
end
|
329
|
+
end
|
330
|
+
@rules.each do |rule|
|
331
|
+
rule.rhs.each_with_index{|i, x| rule.rhs[x] = ts - i if i < 0}
|
332
|
+
rule.prec &&= @precs[@terms[rule.prec]]
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
def check_grammar
|
337
|
+
lhs_warns = []
|
338
|
+
@lhs_syms.each do |s, _|
|
339
|
+
lhs_warns << s if s != 1 and !@rhs_syms[s]
|
340
|
+
end
|
341
|
+
rhs_warns = []
|
342
|
+
@rhs_syms.each do |s, _|
|
343
|
+
rhs_warns << s if s > 0 and !@lhs_syms[s]
|
344
|
+
end
|
345
|
+
lhs_warns.uniq.each do |s|
|
346
|
+
warning "the lhs '#{@int_to_sym[s]}' is not used"
|
347
|
+
end
|
348
|
+
rhs_warns.uniq.each do |s|
|
349
|
+
warning "the symbol '#{@int_to_sym[s]}' is undefined"
|
350
|
+
end
|
351
|
+
end
|
352
|
+
|
353
|
+
def init_hook
|
354
|
+
@hooks = HOOK_KEYS.inject({}){|r, i| r[i] = []; r }
|
355
|
+
end
|
356
|
+
|
357
|
+
def do_hook hook
|
358
|
+
@hooks[hook].each{|o, m| o.send m}
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
class Generator
|
363
|
+
include Depager::Utils::CommonMethods
|
364
|
+
|
365
|
+
TEMPLATES_DIR = File.expand_path('../depager/ruby/templates', __FILE__)
|
366
|
+
|
367
|
+
attr_reader :d_parser, :g_parser, :parsing_method
|
368
|
+
attr_accessor :outer_code, :inner_code
|
369
|
+
attr_accessor :decorators, :requirements, :options
|
370
|
+
|
371
|
+
def initialize d_parser
|
372
|
+
@d_parser = d_parser
|
373
|
+
@g_parser = d_parser.g_parser
|
374
|
+
@parsing_method = nil
|
375
|
+
|
376
|
+
@options = {}
|
377
|
+
@decorators = []
|
378
|
+
@requirements = []
|
379
|
+
@extensions = []
|
380
|
+
@inner_code = ''
|
381
|
+
@outer_code = ''
|
382
|
+
@precs = []
|
383
|
+
@mixins = []
|
384
|
+
end
|
385
|
+
|
386
|
+
def generate_code template
|
387
|
+
ERB.new(template || '', nil, '-').result(binding)
|
388
|
+
end
|
389
|
+
|
390
|
+
def parse_prec
|
391
|
+
precs = []
|
392
|
+
until file.eof?
|
393
|
+
case line = file.gets
|
394
|
+
when /^\s*$/, /^\s*#.*$/
|
395
|
+
# skip
|
396
|
+
when /^\s*(left|right|nonassoc)\s+(.*)$/
|
397
|
+
dir = $1.upcase.intern
|
398
|
+
syms = $2.split.map{|i| (i =~ /'(.+?)'/) ? $1 : i.intern}
|
399
|
+
precs.push [dir, syms]
|
400
|
+
when /^%\}\s*$/
|
401
|
+
break
|
402
|
+
else
|
403
|
+
error_exit "syntax error(prec).\n> #{line}"
|
404
|
+
end
|
405
|
+
end
|
406
|
+
return precs
|
407
|
+
end
|
408
|
+
|
409
|
+
def parse_block raw = false
|
410
|
+
lineno, val = self.file.lineno, ''
|
411
|
+
until file.eof?
|
412
|
+
line = file.gets
|
413
|
+
break if line =~ /^%\}/
|
414
|
+
val.concat line
|
415
|
+
end
|
416
|
+
return val if raw
|
417
|
+
|
418
|
+
delimiter = Depager.configuration[:expanded_code_delimiter] ||
|
419
|
+
".,.,#{Time.now.to_i}#{rand(0xffff)}.,.,"
|
420
|
+
return "module_eval <<'#{delimiter}', '#{file.path}', #{lineno + 1}\n" <<
|
421
|
+
"#{val.gsub('\\', '\\\\')}\n" <<
|
422
|
+
"#{delimiter}\n"
|
423
|
+
end
|
424
|
+
|
425
|
+
def parse_common line
|
426
|
+
case line
|
427
|
+
when /^\s*(#.*)?$/
|
428
|
+
#skip space and comment.
|
429
|
+
when /^%decorate\s+(.*?)\s*\((.*)\)\s*$/
|
430
|
+
@decorators.push $1
|
431
|
+
@requirements.push $2
|
432
|
+
when /^%decorate\s+(.*?)\s*$/
|
433
|
+
@decorators.push $1
|
434
|
+
when /^%extend\s+(.*?)\s*\(\s*'(.*)'\s*\)\s*$/
|
435
|
+
@extensions.push [$2.strip, $1] unless @extensions.find{|i| i[1] == $1}
|
436
|
+
when /^%option\s+(\w+).(\w+)\s+(.*)\s*$/
|
437
|
+
if $1 == 'Generator'
|
438
|
+
Depager.configuration[$2.to_sym] = $3
|
439
|
+
else
|
440
|
+
@options[$1] ||= []
|
441
|
+
@options[$1] << [$2, $3]
|
442
|
+
end
|
443
|
+
when /^%require\s+'(.+?)'\s*$/
|
444
|
+
@requirements.push "'#{$1}'"
|
445
|
+
when /^%mixin\s+(\S+)\s*(\((.+)\)\s*)?$/
|
446
|
+
@mixins.push $1
|
447
|
+
@requirements.push $3 if $3
|
448
|
+
when /^%inner\s*\{\s*$/
|
449
|
+
@inner_code << parse_block
|
450
|
+
when /^%prec\s*\{\s*$/
|
451
|
+
@precs = parse_prec
|
452
|
+
when /^%expanded_code_delimiter\s+(\S+)\s*$/
|
453
|
+
Depager.configuration[:expanded_code_delimiter] = $1
|
454
|
+
else
|
455
|
+
warning "syntax error(declaration).\n> #{line}"
|
456
|
+
end
|
457
|
+
end
|
458
|
+
end
|
459
|
+
|
460
|
+
class ParserGenerator < Generator
|
461
|
+
def initialize d_parser
|
462
|
+
super
|
463
|
+
end
|
464
|
+
|
465
|
+
def parse
|
466
|
+
until file.eof?
|
467
|
+
line = file.gets
|
468
|
+
case line
|
469
|
+
when /^%%\s*$/
|
470
|
+
g_parser.parse(@d_parser.full_target_name, @precs, @extensions, @options)
|
471
|
+
@outer_code << parse_block(true)
|
472
|
+
return generate_code parser_code_template
|
473
|
+
else
|
474
|
+
parse_common(line)
|
475
|
+
end
|
476
|
+
end
|
477
|
+
return nil
|
478
|
+
end
|
479
|
+
|
480
|
+
def parser_code_template
|
481
|
+
end
|
482
|
+
end
|
483
|
+
|
484
|
+
class ExtensionGenerator < Generator
|
485
|
+
attr_accessor :slaves
|
486
|
+
def initialize d_parser
|
487
|
+
super
|
488
|
+
@slaves = { }
|
489
|
+
end
|
490
|
+
|
491
|
+
def parse
|
492
|
+
until file.eof?
|
493
|
+
line = file.gets
|
494
|
+
case line
|
495
|
+
when /^%hook\s+([\w \t]+?)(\/((?:[^\/\\]+|\\.)+)\/(\s+skip)?)?\s*$/
|
496
|
+
hooks = $1.split(' ')
|
497
|
+
@banner = hooks.join(',')
|
498
|
+
@do_parse_re = $3
|
499
|
+
@do_parse_skip = $4
|
500
|
+
|
501
|
+
target_name = "#{d_parser.target_namespace}::Slave_#{ hooks.join('__') }::Parser"
|
502
|
+
hooks.each do |i| i = i.to_sym
|
503
|
+
error_exit "Unknown hook: #{i}" unless g_parser.hook_name? i
|
504
|
+
@slaves[i] = target_name
|
505
|
+
end
|
506
|
+
@outer_code << parse_hook(target_name)
|
507
|
+
when /^%%\s*$/
|
508
|
+
@outer_code << parse_block(true)
|
509
|
+
else
|
510
|
+
parse_common(line)
|
511
|
+
end
|
512
|
+
end
|
513
|
+
return generate_code master_code_template
|
514
|
+
end
|
515
|
+
|
516
|
+
def parse_hook target_name
|
517
|
+
inner = ''
|
518
|
+
precs = []
|
519
|
+
@slave_mixins = @mixins.dup
|
520
|
+
until file.eof?
|
521
|
+
line = file.gets
|
522
|
+
case line
|
523
|
+
when /^%banner\s*'(([^'\\]+|\\.)*)'\s*$/
|
524
|
+
@banner = $1
|
525
|
+
when /^%inner\s*\{\s*$/
|
526
|
+
inner = parse_block
|
527
|
+
when /^%mixin\s*(.+?)\s*(\((.+)\)\s*)?$/
|
528
|
+
@slave_mixins << $1
|
529
|
+
@requirements.push $3 if $3
|
530
|
+
when /^%prec\s*(.*?)\s*$/
|
531
|
+
precs = parse_prec
|
532
|
+
when /^%%\s*$/
|
533
|
+
g_parser.parse(target_name, precs, @extensions, @options)
|
534
|
+
g_parser.inner_code << inner
|
535
|
+
return generate_code slave_code_template
|
536
|
+
else
|
537
|
+
warning "syntax error(declaration).\n> #{line}", file.lineno
|
538
|
+
end
|
539
|
+
end
|
540
|
+
end
|
541
|
+
|
542
|
+
def slave_code_template
|
543
|
+
end
|
544
|
+
|
545
|
+
def master_code_template
|
546
|
+
end
|
547
|
+
end
|
548
|
+
|
549
|
+
class Extension
|
550
|
+
include Depager::Utils::CommonMethods
|
551
|
+
include Depager::Utils::CodeGeneratorMethods
|
552
|
+
|
553
|
+
attr_reader :d_parser, :g_parser
|
554
|
+
|
555
|
+
def init_extension
|
556
|
+
end
|
557
|
+
|
558
|
+
def term_extension
|
559
|
+
end
|
560
|
+
|
561
|
+
def extension_registered g_parser
|
562
|
+
@g_parser = g_parser
|
563
|
+
@d_parser = g_parser.d_parser
|
564
|
+
|
565
|
+
self.methods.sort.each do |m|
|
566
|
+
m = m.to_sym
|
567
|
+
g_parser.hooks[m].push [self, m] if g_parser.hook_name? m
|
568
|
+
end
|
569
|
+
end
|
570
|
+
|
571
|
+
def decorator_name
|
572
|
+
self.class.name.split('::').last.sub(/Extension$/, '')
|
573
|
+
end
|
574
|
+
end
|
575
|
+
end
|
576
|
+
|
577
|
+
class String
|
578
|
+
def unindent
|
579
|
+
indent = match(/\n(\s+)/) ? $1 : ''
|
580
|
+
gsub(/^#{indent}/m, '')
|
581
|
+
end
|
582
|
+
end
|