depager 0.2.3 → 0.3.0.b20250423

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 (106) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +44 -0
  3. data/.simplecov +5 -0
  4. data/Gemfile +12 -0
  5. data/LICENSE.gpl +339 -0
  6. data/Manifest.txt +73 -0
  7. data/README.en +7 -21
  8. data/README.ja +19 -99
  9. data/Rakefile +31 -0
  10. data/bin/depager +7 -45
  11. data/examples/action_pl0d/pl0d.action.dr +421 -0
  12. data/examples/action_pl0d/test.pl0ds +48 -0
  13. data/examples/c89/c89.dr +493 -496
  14. data/examples/c89/test.c89 +10 -10
  15. data/examples/extension/astdf.rb +9 -0
  16. data/examples/extension/atree.dr +55 -0
  17. data/examples/{sample_calc → extension}/calc.atree.dr +42 -43
  18. data/examples/{sample_calc/calc.action.dr → extension/calc.simple_action.dr} +33 -33
  19. data/examples/extension/paction.dr +16 -15
  20. data/examples/extension/pactiontest.dr +14 -14
  21. data/examples/extension/simple_action.rb +46 -0
  22. data/examples/pl0d/pl0ds.dr +337 -334
  23. data/examples/pl0d/test.pl0ds +33 -33
  24. data/examples/rie_calc/calc.rie.dr +57 -0
  25. data/examples/rie_calc/test.calc +4 -0
  26. data/examples/rie_dcuse/dcuse.rie.dr +71 -0
  27. data/examples/rie_dcuse/test.dcuse +1 -0
  28. data/examples/rie_pl0/orig_ex/exerrdg.pl0 +44 -0
  29. data/examples/rie_pl0/orig_ex/exerrm.pl0 +19 -0
  30. data/examples/rie_pl0/orig_ex/exerrmre.pl0 +20 -0
  31. data/examples/rie_pl0/orig_ex/exerrtok.pl0 +18 -0
  32. data/examples/rie_pl0/orig_ex/exmdg.pl0 +40 -0
  33. data/examples/rie_pl0/orig_ex/exmdgwwl.pl0 +43 -0
  34. data/examples/rie_pl0/orig_ex/exmrw.pl0 +22 -0
  35. data/examples/rie_pl0/orig_ex/exmwwl.pl0 +18 -0
  36. data/examples/rie_pl0/orig_ex/exnorw.pl0 +17 -0
  37. data/examples/rie_pl0/pl0.rie.dr +450 -0
  38. data/examples/rie_pl0/test.pl0 +10 -0
  39. data/examples/slex_test/divreg.slex.dr +29 -29
  40. data/examples/slex_test/ljoin.slex.dr +36 -36
  41. data/examples/slex_test/test.divreg +1 -1
  42. data/examples/slex_test/test.ljoin +3 -3
  43. data/examples/{sample_calc/calc.nvaction.dr → tiny_calc/calc.action.dr} +33 -33
  44. data/examples/{sample_calc → tiny_calc}/calc.ast.action.dr +76 -66
  45. data/examples/{sample_calc → tiny_calc}/calc.ast.dr +67 -55
  46. data/examples/tiny_calc/calc.cst.dr +50 -0
  47. data/examples/{sample_calc → tiny_calc}/calc.dr +43 -43
  48. data/examples/{sample_calc → tiny_calc}/calc.lex.dr +29 -29
  49. data/examples/{sample_calc/calc_prec.nvaction.dr → tiny_calc/calc_prec.action.dr} +31 -31
  50. data/lib/depager/cli.rb +44 -0
  51. data/lib/depager/grammar.rb +253 -291
  52. data/lib/depager/lr.rb +589 -579
  53. data/lib/depager/parser.rb +269 -277
  54. data/lib/depager/plugins/_rie_debug.rb +63 -0
  55. data/lib/depager/plugins/action.rb +47 -0
  56. data/lib/depager/plugins/ast.dr +367 -0
  57. data/lib/depager/plugins/ast.rb +1329 -0
  58. data/lib/depager/{ruby/plugins → plugins}/cst.dr +174 -180
  59. data/lib/depager/plugins/cst.rb +591 -0
  60. data/lib/depager/{ruby/plugins → plugins}/lex.dr +85 -89
  61. data/lib/depager/plugins/lex.rb +313 -0
  62. data/lib/depager/plugins/rie.dr +725 -0
  63. data/lib/depager/plugins/rie.rb +1614 -0
  64. data/lib/depager/{ruby/plugins → plugins}/slex.dr +201 -200
  65. data/lib/depager/plugins/slex.rb +769 -0
  66. data/lib/depager/plugins/srp.rb +46 -0
  67. data/lib/depager/ruby/templates/extension_lalr_master.erb +40 -51
  68. data/lib/depager/ruby/templates/extension_lalr_slave.erb +113 -107
  69. data/lib/depager/ruby/templates/single_lalr_parser.erb +124 -117
  70. data/lib/depager/utils.rb +158 -318
  71. data/lib/depager/version.rb +3 -3
  72. data/lib/depager.rb +572 -670
  73. metadata +77 -80
  74. data/ChangeLog +0 -16
  75. data/data/depager/pre-setup.rb +0 -3
  76. data/examples/c89/c89.tab.rb +0 -7127
  77. data/examples/pl0d/pl0ds.tab.rb +0 -2698
  78. data/examples/sample_calc/calc.action.tab.rb +0 -457
  79. data/examples/sample_calc/calc.ast.action.tab.rb +0 -749
  80. data/examples/sample_calc/calc.ast.tab.rb +0 -665
  81. data/examples/sample_calc/calc.astdf.dr +0 -54
  82. data/examples/sample_calc/calc.astdf.tab.rb +0 -672
  83. data/examples/sample_calc/calc.atree.tab.rb +0 -451
  84. data/examples/sample_calc/calc.cst.dr +0 -45
  85. data/examples/sample_calc/calc.cst.tab.rb +0 -644
  86. data/examples/sample_calc/calc.lex.tab.rb +0 -374
  87. data/examples/sample_calc/calc.nvaction.tab.rb +0 -465
  88. data/examples/sample_calc/calc.tab.rb +0 -365
  89. data/examples/sample_calc/calc_prec.nvaction.tab.rb +0 -431
  90. data/examples/slex_test/divreg.slex.tab.rb +0 -303
  91. data/examples/slex_test/ljoin.slex.tab.rb +0 -370
  92. data/lib/depager/ruby/plugins/_ast_tmpl.rb +0 -73
  93. data/lib/depager/ruby/plugins/action.rb +0 -43
  94. data/lib/depager/ruby/plugins/ast.dr +0 -269
  95. data/lib/depager/ruby/plugins/ast.rb +0 -1308
  96. data/lib/depager/ruby/plugins/astdf.rb +0 -6
  97. data/lib/depager/ruby/plugins/atree.dr +0 -55
  98. data/lib/depager/ruby/plugins/atree.rb +0 -347
  99. data/lib/depager/ruby/plugins/cst.rb +0 -626
  100. data/lib/depager/ruby/plugins/lex.rb +0 -336
  101. data/lib/depager/ruby/plugins/nvaction.rb +0 -19
  102. data/lib/depager/ruby/plugins/slex.rb +0 -817
  103. data/lib/depager/ruby/plugins/srp.rb +0 -51
  104. data/lib/depager/ruby/templates/simple.erb +0 -23
  105. data/setup.rb +0 -1585
  106. /data/examples/{sample_calc → tiny_calc}/test.calc +0 -0
data/lib/depager/utils.rb CHANGED
@@ -1,318 +1,158 @@
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
+ module Depager
2
+ class ErrorExit < ::RuntimeError; end
3
+
4
+ module Utils
5
+ module CommonMethods
6
+ def file
7
+ d_parser.file
8
+ end
9
+
10
+ def input_path
11
+ Depager.path_of(file)
12
+ end
13
+
14
+ def target_name
15
+ g_parser.target_name
16
+ end
17
+
18
+ def target_namespace
19
+ g_parser.target_namespace
20
+ end
21
+
22
+ def full_target_name
23
+ "#{target_namespace}::#{target_name}"
24
+ end
25
+
26
+ def error_message(msg, lineno = nil)
27
+ "#{input_path}:#{lineno || file.lineno}: #{msg}"
28
+ end
29
+
30
+ def error_exit(msg, lineno = nil)
31
+ warn error_message(msg, lineno)
32
+ raise Depager::ErrorExit
33
+ end
34
+
35
+ def warning(msg, lineno = nil)
36
+ warn error_message(" warning: #{msg}", lineno)
37
+ end
38
+
39
+ def expanded_code_delimiter
40
+ Depager.configuration[:expanded_code_delimiter] || "DEPAGER_EXPANDED_CODE"
41
+ end
42
+
43
+ def inspect
44
+ "#<#{self.class.name} object_id=#{object_id}>"
45
+ end
46
+ end
47
+
48
+ module CodeGeneratorMethods
49
+ def generate_decorator_code(name, super_class_name, code)
50
+ mixin_code = if d_parser.generator.is_a?(Depager::ExtensionGenerator)
51
+ "include Depager::Utils::ExtensionSlaveDecoratorMethods"
52
+ else
53
+ ""
54
+ end
55
+ <<~CODE
56
+ class #{target_namespace}::#{name} < #{super_class_name} #:nodoc:all
57
+ #{mixin_code}
58
+ #{code}
59
+ end
60
+ CODE
61
+ end
62
+
63
+ def generate_action_decorator_code(on_reduce, code)
64
+ on_reduce_code = on_reduce.map { |i| " #{i || 'nil'},\n" }
65
+
66
+ generate_decorator_code(decorator_name, "#{d_parser.parsing_method}::Action", <<~CODE)
67
+ ON_REDUCE = [
68
+ #{on_reduce_code.join}
69
+ ]
70
+ def on_reduce; ON_REDUCE; end
71
+
72
+ #{code}
73
+ CODE
74
+ end
75
+
76
+ def expand_inline_code(body, lineno, options = {})
77
+ delimiter = expanded_code_delimiter
78
+ delta = options[:delta] || 0
79
+
80
+ if (wrap = options[:wrap])
81
+ wrap = [wrap, "end"] if wrap.is_a?(String)
82
+ body = "#{wrap[0]}\n#{body}\n#{wrap[1]}"
83
+ delta += wrap[0].lines.size
84
+ end
85
+
86
+ <<~CODE
87
+ module_eval <<-'#{delimiter}', '#{input_path}', #{lineno - delta}
88
+ #{body}
89
+ #{delimiter}
90
+ CODE
91
+ end
92
+
93
+ def parse_block
94
+ result = ""
95
+ start_lineno = file.lineno
96
+ if @line =~ /\A\s*\{(.*)\}\s*(#.*)?\Z/
97
+ result = " #{$1}\n"
98
+ else
99
+ result.concat @line.sub(/\A\s*\{/, "")
100
+ ind = @original_line.match(/\A([ \t]*)/)[1]
101
+ until file.eof?
102
+ line = file.gets
103
+ break if line =~ /\A#{ind}\}\s*(#.*)?\Z/
104
+
105
+ result.concat line
106
+ end
107
+ error_exit "syntax error(parse_block).", start_lineno if file.eof?
108
+ end
109
+ @line = $'
110
+ result
111
+ end
112
+ end
113
+
114
+ module ExtensionSlaveMethods
115
+ include CommonMethods
116
+ include CodeGeneratorMethods
117
+
118
+ attr_reader :d_parser, :g_parser, :master
119
+
120
+ def do_default; end
121
+
122
+ def do_parse?
123
+ true
124
+ end
125
+
126
+ def do_parse
127
+ @line = g_parser.old_line
128
+ if do_parse?
129
+ ret = @decorated.parse g_parser.file, @line
130
+ g_parser.update_context @line
131
+ ret
132
+ else
133
+ do_default
134
+ end
135
+ end
136
+
137
+ def abort_driver
138
+ raise ErrorExit
139
+ end
140
+ end
141
+
142
+ module ExtensionSlaveDecoratorMethods
143
+ include CommonMethods
144
+
145
+ def master
146
+ basis.master
147
+ end
148
+
149
+ def g_parser
150
+ basis.g_parser
151
+ end
152
+
153
+ def d_parser
154
+ basis.d_parser
155
+ end
156
+ end
157
+ end
158
+ end
@@ -1,3 +1,3 @@
1
- module Depager
2
- VERSION = '0.2.3'
3
- end
1
+ module Depager
2
+ VERSION = "0.3.0.b20250423".freeze
3
+ end