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.
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,318 +1,148 @@
1
- module Depager
2
- #
3
- # file utilities
4
- #
5
- module FileUtils
6
- # end of file?
7
- def eof?
8
- self.files.eof?
9
- end
10
- # get file name
11
- def fname
12
- self.files.fname
13
- end
14
- # get file path
15
- def path
16
- self.files.path
17
- end
18
- # get current line no
19
- def lineno
20
- self.files.lineno
21
- end
22
- # get next line
23
- def getline
24
- self.files.getline
25
- end
26
- alias gets getline
27
- # error
28
- def error_exit *args
29
- self.d_parser.error_exit(*args)
30
- end
31
- # warning
32
- def warning *args
33
- self.d_parser.warning(*args)
34
- end
35
- end
36
-
37
- #
38
- # Code Utilities
39
- #
40
- module CodeUtils
41
- #
42
- # generate action decorator code
43
- #
44
- # bname:: basis name
45
- # dname:: decorator name
46
- # on_reduce:: on reduce infos
47
- # optouter:: outer code
48
- # mixin:: mixin infos
49
- #
50
- def gen_decorator_code cname, dname, sname, tables, optouter=''
51
- %{
52
- class D4#{cname}::#{dname} < #{sname} #:nodoc:all
53
- def initialize inside
54
- super inside, '#{dname}'
55
- init_parser
56
- end
57
- #{optouter}
58
- end
59
- }; #code
60
- end
61
-
62
- #
63
- # generate action decorator code
64
- #
65
- # bname:: basis name
66
- # dname:: decorator name
67
- # on_reduce:: on reduce info
68
- # optouter:: outer code
69
- # mixin:: mixin file names
70
- #
71
- def gen_action_decorator_code bname, dname, on_reduce, optouter='', mixin=[]
72
- mixin_code = mixin.map{|i| " include #{i}\n"}
73
- on_reduce_code = on_reduce.map{|i| " #{i||'nil'},\n"}
74
- ans = @g_parser.parsing_method
75
- code = %{
76
- class D4#{bname}::#{dname} < #{ans}::Action #:nodoc:all
77
- include Depager::DecoratorUtils
78
- #{mixin_code}
79
- on_reduce = [
80
- nil,
81
- #{on_reduce_code.join}
82
- ]
83
- Tables = [on_reduce]
84
- def initialize inside
85
- super inside, '#{dname}'
86
- @on_reduce, = self.class::Tables
87
- init_parser
88
- end
89
- \n$$!! @OUTER @!!$$
90
- end
91
- }; #code
92
- optouter = optouter.join if optouter.is_a? Array
93
- unindent(code).sub!('$$!! @OUTER @!!$$', optouter)
94
- end
95
-
96
- #
97
- # generate module eval code
98
- #
99
- # lineno:: line no
100
- # d:: input line no - output line no
101
- # body:: code body
102
- #
103
- def gen_meval_code lineno, d, body
104
- d ||= 2
105
- t = Time.now.to_i
106
- r = rand(0xffff)
107
- "\nmodule_eval <<\-'.,.,#{t}#{r}.,.,', '#{files.path}', #{lineno-d}\n" +
108
- body +
109
- "\n.,.,#{t}#{r}.,.,\n"
110
- end
111
-
112
- #
113
- # generate def code
114
- #
115
- # sig:: signature
116
- # body:: code body
117
- # lineno:: line no
118
- # d:: input line no - output line no
119
- #
120
- def gen_defm_code sig, body, lineno=1, d=0
121
- t = Time.now.to_i
122
- r = rand(0xffff)
123
- body = "def #{sig}\n#{body}\nend"
124
- code = %{
125
- module_eval <<\-'.,.,#{t}#{r}.,.,', '#{files.path}', #{lineno-d-1}
126
- $$!!@ BODY @!!$$
127
- .,.,#{t}#{r}.,.,
128
- }
129
- unindent(code).sub!('$$!!@ BODY @!!$$', body)
130
- end
131
-
132
- # unindent
133
- def unindent s
134
- s = s.join rescue s.to_s
135
- ind = s.match(/\n(\s+)/)
136
- s.gsub(/^#{ind[1]}/m, '')
137
- end
138
-
139
- def parse_action
140
- val = ''
141
- start_lineno = lineno
142
- if @line =~ /\A\s*\{(.*)\}\s*(#.*)?\Z/
143
- val = " #{$1}\n"
144
- else
145
- val.concat @line.sub(/\A\s*\{/, '')
146
- ind = @line0.match(/\A([ \t]*)/)[1]
147
- until file.eof?
148
- getline
149
- break if line =~ /\A#{ind}\}\s*(#.*)?\Z/
150
- val.concat line
151
- end
152
- if file.eof?
153
- error_exit 'syntax error(parse_action).', start_lineno
154
- end
155
- end
156
- @line = $'
157
- # warn "--action--\n#{val}"
158
- return val
159
- end
160
- end
161
-
162
- #
163
- # Utilities for Extension
164
- #
165
- module ExtensionUtils
166
- include CodeUtils
167
- # get GrammarParser
168
- def g_parser
169
- @g_parser
170
- end
171
-
172
- # master parser
173
- def master
174
- @master
175
- end
176
-
177
- # get output class name
178
- def target_name
179
- @g_parser.target_name
180
- end
181
-
182
- # get current line
183
- def line
184
- @g_parser.line
185
- end
186
-
187
- # end of file?
188
- def eof?
189
- @g_parser.eof?
190
- end
191
-
192
- # get input file name
193
- def fname
194
- @g_parser.fname
195
- end
196
-
197
- # get input file path
198
- def path
199
- @g_parser.path
200
- end
201
-
202
- # get current line no
203
- def lineno
204
- @g_parser.lineno
205
- end
206
-
207
- # get next line
208
- def getline
209
- @line0 = @line = @g_parser.getline
210
- end
211
-
212
- # get FileManager object
213
- def files
214
- @g_parser.files
215
- end
216
- alias file files
217
-
218
- # get nunber of rules
219
- def nrules
220
- @g_parser.rules.size - 2
221
- end
222
-
223
- # get nunber of rhs rules
224
- def nrhs
225
- @g_parser.nrhs
226
- end
227
-
228
- # get lhs name
229
- def lhs_name
230
- @g_parser.int_to_sym(@g_parser.lhs)
231
- end
232
-
233
- # get rhs names
234
- def rhs_names
235
- @g_parser.rhs.map do |i|
236
- @g_parser.int_to_sym(i)
237
- end
238
- end
239
-
240
- protected
241
- # initialize method
242
- def init_parser ; end
243
-
244
- # do default action
245
- # for generated extensions
246
- def do_default ; end
247
-
248
- # do default action?
249
- # for generated extensions
250
- def do_parse?
251
- true
252
- end
253
-
254
- # start parse
255
- # for generated extensions
256
- def do_parse
257
- __init_parser__
258
- init_parser
259
- @line = @g_parser.oldline
260
- if do_parse?
261
- ret = @dect.parse @g_parser.files
262
- @g_parser.update_context @line
263
- ret
264
- else
265
- do_default
266
- end
267
- end
268
-
269
- # register extension (for users)
270
- def regext p ; end
271
-
272
- # register extension (default)
273
- def __regext__ p, key=:default, reg_p=true
274
- @g_parser = @p = p
275
- @nparam = case key
276
- when :none ; nil
277
- when :default ; @g_parser.getnparam(paramkey)
278
- else ; @g_parser.getnparam(key)
279
- end
280
- self.methods.sort.each do |m| m = m.to_sym
281
- p.hooks[m].push [self, m] if GrammarParser.hook_name? m
282
- end
283
- regext p if reg_p
284
- end
285
- # get param key
286
- def paramkey
287
- self.class.name.split('::').last.sub(/Extension$/, '')
288
- end
289
-
290
- # error
291
- def error_exit *args
292
- @g_parser.error_exit(*args)
293
- end
294
- # warning
295
- def warning *args
296
- @g_parser.warning(*args)
297
- end
298
- end
299
-
300
- #
301
- # Utilities for Decorator
302
- #
303
- module DecoratorUtils
304
- include CodeUtils
305
- # get master parser
306
- def master
307
- @basis.master
308
- end
309
- # get grammar parser
310
- def g_parser
311
- @basis.g_parser
312
- end
313
- # get basis
314
- def basis
315
- @basis
316
- end
317
- end
318
- end
1
+ # -*- coding: utf-8 -*-
2
+ module Depager
3
+ module Utils
4
+ module CommonMethods
5
+ def file
6
+ d_parser.file
7
+ end
8
+
9
+ def target_name
10
+ g_parser.target_name
11
+ end
12
+
13
+ def target_namespace
14
+ g_parser.target_namespace
15
+ end
16
+
17
+ def full_target_name
18
+ "#{target_namespace}::#{target_name}"
19
+ end
20
+
21
+ def error_message msg, lineno = nil
22
+ "#{file.path}:#{lineno || file.lineno}: #{msg}"
23
+ end
24
+
25
+ def error_exit msg, lineno = nil
26
+ warn error_message(msg, lineno)
27
+ exit 1
28
+ end
29
+
30
+ def warning msg, lineno = nil
31
+ warn error_message(" warning: #{msg}", lineno)
32
+ end
33
+ end
34
+
35
+ module CodeGeneratorMethods
36
+ def generate_decorator_code name, super_class_name, code
37
+ if d_parser.generator.is_a?(Depager::ExtensionGenerator)
38
+ mixin_code = 'include Depager::Utils::ExtensionSlaveDecoratorMethods'
39
+ else
40
+ mixin_code = ''
41
+ end
42
+
43
+ %{
44
+ \ class #{target_namespace}::#{name} < #{super_class_name} #:nodoc:all
45
+ #{mixin_code}
46
+ %s
47
+ end
48
+ }.unindent % code; #code
49
+ end
50
+
51
+ def generate_action_decorator_code on_reduce, code
52
+ on_reduce_code = on_reduce.map{|i| " #{i || 'nil'},\n"}
53
+
54
+ generate_decorator_code(decorator_name, "#{d_parser.parsing_method}::Action", %{
55
+ on_reduce = [
56
+ #{ on_reduce_code.join }
57
+ ]
58
+ Tables = [ on_reduce ]
59
+
60
+ %s
61
+ } % code)
62
+ end
63
+
64
+ def expand_inline_code body, lineno, options = {}
65
+ delimiter = expanded_code_delimiter
66
+
67
+ if wrap = options[:wrap]
68
+ wrap = [wrap, "end"] if wrap.is_a?(String)
69
+ body = "#{wrap[0]}\n#{body}\n#{wrap[1]}"
70
+ end
71
+
72
+ delta = options[:delta] || 0
73
+
74
+ %{
75
+ \ module_eval <<\-'#{delimiter}', '#{file.path}', #{lineno - delta - 1}
76
+ %s
77
+ #{delimiter}
78
+ }.unindent % body
79
+ end
80
+
81
+ def expanded_code_delimiter
82
+ Depager.configuration[:expanded_code_delimiter] || ".,.,#{Time.now.to_i}#{rand(0xffff)}.,.,"
83
+ end
84
+
85
+ def parse_block
86
+ result = ''
87
+ start_lineno = file.lineno
88
+ if @line =~ /\A\s*\{(.*)\}\s*(#.*)?\Z/
89
+ result = " #{$1}\n"
90
+ else
91
+ result.concat @line.sub(/\A\s*\{/, '')
92
+ ind = @original_line.match(/\A([ \t]*)/)[1]
93
+ until file.eof?
94
+ line = file.gets
95
+ break if line =~ /\A#{ind}\}\s*(#.*)?\Z/
96
+ result.concat line
97
+ end
98
+ if file.eof?
99
+ error_exit 'syntax error(parse_block).', start_lineno
100
+ end
101
+ end
102
+ @line = $'
103
+ return result
104
+ end
105
+ end
106
+
107
+ module ExtensionSlaveMethods
108
+ include CommonMethods
109
+ include CodeGeneratorMethods
110
+
111
+ attr_reader :d_parser, :g_parser, :master
112
+
113
+ def do_default
114
+ end
115
+
116
+ def do_parse?
117
+ true
118
+ end
119
+
120
+ def do_parse
121
+ @line = g_parser.old_line
122
+ if do_parse?
123
+ ret = @decorated.parse g_parser.file, @line
124
+ g_parser.update_context @line
125
+ ret
126
+ else
127
+ do_default
128
+ end
129
+ end
130
+ end
131
+
132
+ module ExtensionSlaveDecoratorMethods
133
+ include CommonMethods
134
+
135
+ def master
136
+ basis.master
137
+ end
138
+
139
+ def g_parser
140
+ basis.g_parser
141
+ end
142
+
143
+ def d_parser
144
+ basis.d_parser
145
+ end
146
+ end
147
+ end
148
+ end