depager 0.2.3 → 0.3.0.b20160729

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.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/README.en +4 -19
  3. data/README.ja +42 -79
  4. data/bin/depager +42 -45
  5. data/examples/action_pl0d/pl0d.action.dr +421 -0
  6. data/examples/action_pl0d/test.pl0ds +49 -0
  7. data/examples/c89/c89.dr +493 -496
  8. data/examples/c89/test.c89 +10 -10
  9. data/examples/extension/astdf.rb +10 -0
  10. data/examples/extension/atree.dr +55 -0
  11. data/examples/{sample_calc → extension}/calc.atree.dr +42 -43
  12. data/examples/extension/calc.simple_action.dr +33 -0
  13. data/examples/extension/paction.dr +16 -15
  14. data/examples/extension/pactiontest.dr +14 -14
  15. data/examples/extension/simple_action.rb +44 -0
  16. data/examples/pl0d/pl0ds.dr +337 -334
  17. data/examples/pl0d/test.pl0ds +33 -33
  18. data/examples/rie_calc/calc.rie.dr +57 -0
  19. data/examples/rie_calc/test.calc +4 -0
  20. data/examples/rie_dcuse/dcuse.rie.dr +71 -0
  21. data/examples/rie_dcuse/test.dcuse +1 -0
  22. data/examples/rie_pl0/orig_ex/exerrdg.pl0 +44 -0
  23. data/examples/rie_pl0/orig_ex/exerrm.pl0 +19 -0
  24. data/examples/rie_pl0/orig_ex/exerrmre.pl0 +20 -0
  25. data/examples/rie_pl0/orig_ex/exerrtok.pl0 +18 -0
  26. data/examples/rie_pl0/orig_ex/exmdg.pl0 +40 -0
  27. data/examples/rie_pl0/orig_ex/exmdgwwl.pl0 +43 -0
  28. data/examples/rie_pl0/orig_ex/exmrw.pl0 +22 -0
  29. data/examples/rie_pl0/orig_ex/exmwwl.pl0 +18 -0
  30. data/examples/rie_pl0/orig_ex/exnorw.pl0 +17 -0
  31. data/examples/rie_pl0/pl0.rie.dr +450 -0
  32. data/examples/rie_pl0/test.pl0 +10 -0
  33. data/examples/sample_calc/calc.action.dr +33 -33
  34. data/examples/sample_calc/calc.ast.action.dr +65 -66
  35. data/examples/sample_calc/calc.ast.dr +55 -55
  36. data/examples/sample_calc/calc.cst.dr +45 -45
  37. data/examples/sample_calc/calc.dr +43 -43
  38. data/examples/sample_calc/calc.lex.dr +29 -29
  39. data/examples/sample_calc/{calc_prec.nvaction.dr → calc_prec.action.dr} +31 -31
  40. data/examples/slex_test/divreg.slex.dr +29 -29
  41. data/examples/slex_test/ljoin.slex.dr +36 -36
  42. data/examples/slex_test/test.divreg +1 -1
  43. data/examples/slex_test/test.ljoin +3 -3
  44. data/lib/depager.rb +582 -670
  45. data/lib/depager/grammar.rb +256 -291
  46. data/lib/depager/lr.rb +574 -579
  47. data/lib/depager/parser.rb +282 -277
  48. data/lib/depager/ruby/plugins/_rie_debug.rb +35 -0
  49. data/lib/depager/ruby/plugins/action.rb +53 -43
  50. data/lib/depager/ruby/plugins/ast.dr +364 -269
  51. data/lib/depager/ruby/plugins/ast.rb +1367 -1308
  52. data/lib/depager/ruby/plugins/cst.dr +172 -180
  53. data/lib/depager/ruby/plugins/cst.rb +587 -626
  54. data/lib/depager/ruby/plugins/lex.dr +85 -89
  55. data/lib/depager/ruby/plugins/lex.rb +310 -336
  56. data/lib/depager/ruby/plugins/rie.dr +723 -0
  57. data/lib/depager/ruby/plugins/rie.rb +1653 -0
  58. data/lib/depager/ruby/plugins/slex.dr +202 -200
  59. data/lib/depager/ruby/plugins/slex.rb +780 -817
  60. data/lib/depager/ruby/plugins/srp.rb +56 -51
  61. data/lib/depager/ruby/templates/extension_lalr_master.erb +46 -51
  62. data/lib/depager/ruby/templates/extension_lalr_slave.erb +99 -107
  63. data/lib/depager/ruby/templates/single_lalr_parser.erb +115 -117
  64. data/lib/depager/utils.rb +148 -318
  65. data/lib/depager/version.rb +4 -3
  66. metadata +52 -60
  67. data/ChangeLog +0 -16
  68. data/data/depager/pre-setup.rb +0 -3
  69. data/examples/c89/c89.tab.rb +0 -7127
  70. data/examples/pl0d/pl0ds.tab.rb +0 -2698
  71. data/examples/sample_calc/calc.action.tab.rb +0 -457
  72. data/examples/sample_calc/calc.ast.action.tab.rb +0 -749
  73. data/examples/sample_calc/calc.ast.tab.rb +0 -665
  74. data/examples/sample_calc/calc.astdf.dr +0 -54
  75. data/examples/sample_calc/calc.astdf.tab.rb +0 -672
  76. data/examples/sample_calc/calc.atree.tab.rb +0 -451
  77. data/examples/sample_calc/calc.cst.tab.rb +0 -644
  78. data/examples/sample_calc/calc.lex.tab.rb +0 -374
  79. data/examples/sample_calc/calc.nvaction.dr +0 -33
  80. data/examples/sample_calc/calc.nvaction.tab.rb +0 -465
  81. data/examples/sample_calc/calc.tab.rb +0 -365
  82. data/examples/sample_calc/calc_prec.nvaction.tab.rb +0 -431
  83. data/examples/slex_test/divreg.slex.tab.rb +0 -303
  84. data/examples/slex_test/ljoin.slex.tab.rb +0 -370
  85. data/lib/depager/ruby/plugins/_ast_tmpl.rb +0 -73
  86. data/lib/depager/ruby/plugins/astdf.rb +0 -6
  87. data/lib/depager/ruby/plugins/atree.dr +0 -55
  88. data/lib/depager/ruby/plugins/atree.rb +0 -347
  89. data/lib/depager/ruby/plugins/nvaction.rb +0 -19
  90. data/lib/depager/ruby/templates/simple.erb +0 -23
  91. data/setup.rb +0 -1585
@@ -1,277 +1,282 @@
1
- module Depager; end
2
- #
3
- # Token
4
- #
5
- class Depager::Token
6
- def self.[] *args
7
- self.new(*args)
8
- end
9
- attr_accessor :value, :lineno, :filename
10
- def initialize value, lineno=nil, filename=nil
11
- @value = value
12
- @lineno = lineno
13
- @filename = filename
14
- end
15
- def [](n)
16
- case n
17
- when 0 ; @value
18
- when 1 ; @lineno
19
- when 2 ; @filename
20
- else ; nil
21
- end
22
- end
23
- def to_s
24
- "#{@value.inspect}:#{@lineno}@#{@filename}"
25
- end
26
- def inspect
27
- "#<#{self.class.name}:0x#{object_id.to_s(16)}" <<
28
- " #{@value.inspect}:#{@lineno}@#{@filename}>"
29
- end
30
- end
31
-
32
- # LALR runtime
33
- module Depager::LALR
34
- # = Parser runtime
35
- # == Stack
36
- # s:: state
37
- # la:: look ahead
38
- # v:: value
39
- # stack top->
40
- # [0, [la0, v0], s0, [la1, v1], s1, .....]
41
- #
42
- class Parser
43
- ACC = 0x03ffffff
44
-
45
- # parse method
46
- def parse f
47
- @basis.file = f
48
- @basis.la = [0, nil]
49
- @basis.lex do |sym, val|
50
- @basis.la = [@basis.t2i[sym] || @basis.t2i[false], val]
51
- break if driver
52
- end
53
- @basis.stack[1][2..(@basis.params.size+1)]
54
- end
55
-
56
- protected
57
- # driver method
58
- def driver
59
- while true
60
- @basis.val = @basis.action_table[@basis.stack.last][@basis.la[0]]
61
- @basis.val ||= @basis.defred_table[@basis.stack.last]
62
-
63
- if @basis.val == nil
64
- #warn "#{@basis.stack}#{@basis.la[0]}"
65
- error
66
- exit 1
67
- elsif @basis.val == ACC
68
- if @i2t[@basis.la[0]] == nil
69
- accept
70
- else
71
- error
72
- end
73
- return true
74
- elsif @basis.val > 0
75
- shift
76
- while val = @basis.defred_after_shift_table[@basis.stack.last]
77
- @basis.val = val
78
- reduce
79
- end
80
- return false
81
- else
82
- reduce
83
- end
84
- end
85
- end
86
-
87
- def enable?(act, sym)
88
- case act
89
- when :shift
90
- la = @basis.t2i[sym] || @basis.t2i[false]
91
- val = @basis.action_table[@basis.stack.last][la]
92
- else
93
- end
94
- end
95
-
96
- def enables(act, sym)
97
- case act
98
- when :shift
99
- la = @basis.t2i[sym] || @basis.t2i[false]
100
- res = []
101
- @basis.action_table.each_with_index{|i, x|
102
- i[la] and res.push(x)
103
- }
104
- return res
105
- else
106
- end
107
- end
108
-
109
- # template methods
110
- def init_parser ; end
111
- def lex ; end
112
- def accept ; end
113
- def error ; end
114
- def shift ; end
115
- def reduce ; end
116
- end
117
-
118
- class Basis < Parser
119
- attr_accessor :state, :stack, :laq, :la, :val, :basis, :params
120
- attr_accessor :file, :oldline, :lineno, :line, :line0
121
- attr_reader :action_table, :defred_table, :defred_after_shift_table
122
- attr_reader :goto_table, :reduce_table
123
- attr_reader :i2t, :t2i, :nt2i, :i2nt
124
-
125
- private
126
- def __init_parser__
127
- @state = 0
128
- @stack = [@state]
129
- @laq = []
130
- @la = nil
131
- @val = nil
132
- @line0 = @line = ''
133
- end
134
-
135
- def initialize
136
- __init_parser__
137
- @file = nil
138
- @basis = self
139
- @reduce_table, @params, @action_table,
140
- @defred_table, @defred_after_shift_table, @goto_table,
141
- @t2i, @i2t, @nt2i, @i2nt = self.class::Tables
142
- end
143
-
144
- def token sym, val=nil, lineno=nil
145
- lineno ||= file.lineno
146
- return sym, Depager::Token[val, lineno, file.path]
147
- end
148
-
149
- public
150
- def banner
151
- self.class.name
152
- end
153
-
154
- def getline
155
- @file.gets unless @file.eof?
156
- end
157
-
158
- def accept
159
- #warn "acc."
160
- end
161
- def error
162
- fname = @file.respond_to?(:path) ? @file.path : '-'
163
- exp = []
164
- @action_table[@stack.last].each_with_index do |i, x|
165
- exp << @i2t[x] if i
166
- end
167
- la = @la[1].value rescue @la[1]
168
- la = (@i2t[@la[0]] || la).inspect
169
- exp = exp.map{|i| i ? i.inspect : '$' }.join(', ')
170
- warn "#{fname}:#{@file.lineno}: syntax error(#{self.banner}), " <<
171
- "unexpected #{la}, expecting #{exp}."
172
- warn @stack.pretty_inspect if $MP_DEBUG
173
- end
174
- def shift
175
- @stack.push(@la).push(@val)
176
- end
177
- def reduce
178
- (@reduce_table[-@val][1] * 2).times{@stack.pop}
179
- r = @reduce_table[-@val][0]
180
- cs = @goto_table[@stack.last][r]
181
- @stack.push([r, :NT]).push(cs)
182
- end
183
- end
184
-
185
- #
186
- # Decorator Base class
187
- #
188
- class AdvancedParser < Parser
189
- attr_reader :basis
190
- attr_reader :action_table, :goto_table, :reduce_table
191
- attr_reader :i2t, :t2i, :nt2i, :i2nt
192
- def initialize inside, paramkey = nil
193
- @inside = inside
194
- @basis = @inside.basis
195
- @reduce_table, @params, @action_table, @defred_table, @goto_table,
196
- @t2i, @i2t, @nt2i, @i2nt = @basis.class::Tables
197
- @nparam = @params[paramkey]
198
- end
199
-
200
- protected
201
- def _state
202
- @basis.state
203
- end
204
- def _stack
205
- @basis.stack
206
- end
207
- def _lookaheadque
208
- @basis.laq
209
- end
210
- def _lookahead
211
- @basis.la
212
- end
213
- def _actvalue
214
- @basis.val
215
- end
216
-
217
- def accept
218
- before_accept
219
- @inside.accept
220
- after_accept
221
- end
222
- def error
223
- before_error
224
- @inside.error
225
- after_error
226
- end
227
- def shift
228
- before_shift
229
- @inside.shift
230
- after_shift
231
- end
232
- def reduce
233
- before_reduce
234
- @inside.reduce
235
- after_reduce
236
- end
237
-
238
- # template methods
239
- def before_accept ; end
240
- def after_accept ; end
241
- def before_error ; end
242
- def after_error ; end
243
- def before_shift ; end
244
- def after_shift ; end
245
- def before_reduce ; end
246
- def after_reduce ; end
247
- end
248
-
249
- #
250
- # Action Decorator Base class
251
- #
252
- class Action < AdvancedParser
253
- def initialize inside, nparamkey=nil
254
- super inside, nparamkey
255
- @_yy_val_ = []
256
- end
257
- def before_reduce
258
- @_yy_val_ = []
259
- n = @reduce_table[-_actvalue][1]
260
- n.times do |i|
261
- x = _stack[_stack.size-2*(i+1)]
262
- @_yy_val_[n-i-1] = x[1] == :NT ? x[@nparam] : x[1]
263
- end
264
- end
265
- def after_reduce
266
- mes = @on_reduce[-_actvalue]
267
- r = mes ? self.send(mes, @_yy_val_) : nil
268
- _stack[_stack.size-2][@nparam] = r
269
- end
270
- end
271
- end
272
-
273
- class IO
274
- def path
275
- self.to_s
276
- end
277
- end
1
+ # -*- coding: utf-8 -*-
2
+ lib = File.expand_path("../ruby", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ module Depager
6
+ class Token
7
+ attr_accessor :value, :lineno, :filename
8
+
9
+ def initialize value, lineno=nil, filename=nil
10
+ @value = value
11
+ @lineno = lineno
12
+ @filename = filename
13
+ end
14
+
15
+ def to_s
16
+ "#{@value.inspect}:#{@lineno}@#{@filename}"
17
+ end
18
+
19
+ def inspect
20
+ "<#{@value.inspect}:#{@lineno}>"
21
+ end
22
+ end
23
+
24
+ module LALR
25
+ # = Parser runtime
26
+ # == Stack
27
+ # s:: state
28
+ # la:: look ahead
29
+ # v:: value
30
+ # stack top->
31
+ # [0, [la0, v0], s0, [la1, v1], s1, .....]
32
+ #
33
+ class Parser
34
+ ACC = 0x03ffffff
35
+
36
+ attr_reader :action_table, :defred_table, :defred_after_shift_table
37
+ attr_reader :goto_table, :reduce_table
38
+ attr_reader :int_to_term, :term_to_int, :nonterm_to_int, :int_to_nonterm
39
+
40
+ def initialize
41
+ @reduce_table,
42
+ @action_table,
43
+ @defred_table,
44
+ @defred_after_shift_table,
45
+ @goto_table,
46
+ @term_to_int,
47
+ @int_to_term,
48
+ @nonterm_to_int,
49
+ @int_to_nonterm = basis.class::Tables
50
+ end
51
+
52
+ def parse f, line = ''
53
+ basis.__send__ :internal_initialize, f, line, [0, nil]
54
+
55
+ do_hook :before_parse
56
+ lex do |sym, val|
57
+ basis.lookahead = [term_to_int[sym] || term_to_int[false], val]
58
+ break if driver
59
+ end
60
+ do_hook :after_parse
61
+
62
+ stack[1][2..basis.parser_size]
63
+ end
64
+
65
+ def accept
66
+ end
67
+
68
+ def error
69
+ end
70
+
71
+ def shift
72
+ end
73
+
74
+ def reduce
75
+ end
76
+
77
+ def stack
78
+ basis.stack
79
+ end
80
+
81
+ def lookahead
82
+ basis.lookahead
83
+ end
84
+
85
+ def action_value
86
+ basis.action_value
87
+ end
88
+
89
+ protected
90
+ def lex &block
91
+ basis.lex &block
92
+ end
93
+
94
+ def driver
95
+ while true
96
+ basis.action_value = action_table[stack.last][lookahead[0]] || defred_table[stack.last]
97
+
98
+ if action_value == nil
99
+ error
100
+ exit 1
101
+ elsif action_value == ACC
102
+ if int_to_term[lookahead[0]] == nil
103
+ accept
104
+ else
105
+ error
106
+ end
107
+ return true
108
+ elsif action_value > 0
109
+ shift
110
+ while v = defred_after_shift_table[stack.last]
111
+ basis.action_value = v
112
+ reduce
113
+ end
114
+ return false
115
+ else
116
+ reduce
117
+ end
118
+ end
119
+ end
120
+
121
+ def do_hook hook
122
+ if @basis == self
123
+ send hook
124
+ elsif hook.to_s.match(/^before_/)
125
+ send hook
126
+ @inside.do_hook hook
127
+ else
128
+ @inside.do_hook hook
129
+ send hook
130
+ end
131
+ end
132
+
133
+ private
134
+ def before_parse
135
+ end
136
+
137
+ def after_parse
138
+ end
139
+ end
140
+
141
+ class Basis < Parser
142
+ attr_reader :basis, :file, :line, :parser_size
143
+ attr_accessor :lookahead, :action_value, :stack
144
+
145
+ def initialize
146
+ @basis = self
147
+ @parser_size = 1
148
+ super()
149
+ end
150
+
151
+ def banner
152
+ self.class.name
153
+ end
154
+
155
+ def get_decorator_index
156
+ @parser_size += 1
157
+ end
158
+
159
+ def accept
160
+ end
161
+
162
+ def error
163
+ path = file.respond_to?(:path) ? file.path : '-'
164
+ exp = []
165
+ action_table[stack.last].each_with_index do |i, x|
166
+ exp << int_to_term[x] if i
167
+ end
168
+ la = lookahead[1].value rescue lookahead[1]
169
+ la = (int_to_term[lookahead[0]] || la).inspect
170
+ exp = exp.map{|i| i ? i.inspect : '$' }.join(', ')
171
+ warn "#{path}:#{file.lineno}: syntax error(#{banner}), unexpected #{la}, expecting #{exp}."
172
+ warn @stack.pretty_inspect if Depager.debug_mode?
173
+ end
174
+
175
+ def shift
176
+ stack << lookahead << action_value
177
+ end
178
+
179
+ def reduce
180
+ r, x = reduce_table[-action_value]
181
+ (x * 2).times{ stack.pop }
182
+ v = goto_table[stack.last][r]
183
+ stack << [r, :NT] << v
184
+ end
185
+
186
+ private
187
+ def internal_initialize file, line, lookahead
188
+ @stack, @action_value, @file, @line, @lookahead =
189
+ [0], nil, file, line, lookahead
190
+ end
191
+
192
+ def token sym, value = nil, lineno = nil
193
+ lineno ||= file.lineno
194
+ path = file.respond_to?(:path) ? file.path : '-'
195
+ return sym, Depager::Token.new(value, lineno, path)
196
+ end
197
+ end
198
+
199
+ class AdvancedParser < Parser
200
+ attr_reader :basis, :decorator_index
201
+
202
+ def initialize inside
203
+ @inside = inside
204
+ @basis = inside.basis
205
+ @decorator_index = @basis.get_decorator_index
206
+ super()
207
+ end
208
+
209
+ def accept
210
+ before_accept
211
+ @inside.accept
212
+ after_accept
213
+ end
214
+
215
+ def error
216
+ before_error
217
+ @inside.error
218
+ after_error
219
+ end
220
+
221
+ def shift
222
+ before_shift
223
+ @inside.shift
224
+ after_shift
225
+ end
226
+
227
+ def reduce
228
+ before_reduce
229
+ @inside.reduce
230
+ after_reduce
231
+ end
232
+
233
+ private
234
+ def before_accept
235
+ end
236
+
237
+ def after_accept
238
+ end
239
+
240
+ def before_error
241
+ end
242
+
243
+ def after_error
244
+ end
245
+
246
+ def before_shift
247
+ end
248
+
249
+ def after_shift
250
+ end
251
+
252
+ def before_reduce
253
+ end
254
+
255
+ def after_reduce
256
+ end
257
+ end
258
+
259
+ class Action < AdvancedParser
260
+ def initialize inside
261
+ super inside
262
+ @_arguments = []
263
+ @on_reduce, = self.class::Tables
264
+ end
265
+
266
+ def before_reduce
267
+ @_arguments = []
268
+ n = reduce_table[-action_value][1]
269
+ n.times do |i|
270
+ v = stack[stack.size - 2 * (i + 1)]
271
+ @_arguments[n - i - 1] = v[1] == :NT ? v[decorator_index] : v[1]
272
+ end
273
+ end
274
+
275
+ def after_reduce
276
+ method = @on_reduce[-action_value]
277
+ result = method ? send(method, @_arguments) : nil
278
+ stack[stack.size - 2][decorator_index] = result
279
+ end
280
+ end
281
+ end
282
+ end