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
@@ -1,180 +1,174 @@
1
- %defext CSTBuilderExtension
2
- %extend Lexer ('plugins/lex.rb')
3
- %extend NVAction ('plugins/nvaction.rb')
4
- %decorate @NVAction
5
- #%decorate ShiftReducePrinter ('plugins/srp.rb')
6
- %inner{
7
- attr_accessor :optouter, :nodes_code
8
- def init_parser
9
- super
10
- @n = 0
11
- @visitor_code = []
12
- @nodes_code = []
13
- @optouter = []
14
- @on_reduce = []
15
- end
16
- def modify_action_code code
17
- code.gsub(/~([a-z_])/, 'node.\1')
18
- end
19
- def post_rhs
20
- code, lineno = '', g_parser.lineno
21
- if g_parser.line =~ /\s*\{/
22
- @line0, @line = g_parser.line0, g_parser.line
23
- code = parse_action
24
-
25
- g_parser.update_context @line
26
- end
27
-
28
- node_name = "#{lhs_name}_#{nrhs}"
29
- accept = ""
30
- nodes = g_parser.rhs.map do |i|
31
- x = g_parser.int_to_sym(i)
32
- if x.class == String
33
- "tx#{x.unpack("H*")[0]}"
34
- else
35
- x = x.to_s
36
- accept << "#{x}.accept(v); " if x =~ /\A[a-z_]+\Z/
37
- x.downcase
38
- end
39
- end
40
- attrc = nodes.map{|i| ":#{i}" }.join(', ')
41
- inic = nodes.map{|i| "@#{i} = #{i}"}.join('; ')
42
- @nodes_code << %{
43
- class Node_#{node_name} < Node_#{lhs_name}
44
- attr_accessor #{attrc}
45
- def initialize #{nodes.join(', ')}
46
- super()
47
- #{inic}
48
- end
49
- def accept v
50
- #{accept}
51
- v.visit_Node_#{node_name}(self)
52
- end
53
- end
54
- }; #code
55
-
56
- @visitor_code <<
57
- gen_defm_code("visit_Node_#{node_name} node",
58
- modify_action_code(code), lineno)
59
-
60
- args = nodes.inject([]){|r, i| r << "val[#{r.size}]"}
61
- code = "Node_#{node_name}.new(#{args.join(', ')})"
62
- @optouter <<
63
- gen_defm_code("_cst_#{nrules} val", code, lineno)
64
-
65
- @on_reduce[nrules] = ":_cst_#{nrules}"
66
- end
67
- def post_rule_list
68
- code = unindent %{
69
- class Node
70
- def accept
71
- end
72
- end
73
- }; #code
74
- g_parser.optouter << code
75
-
76
- code = unindent %{
77
- class Visitor
78
- $$!!@ CST @!!$$
79
- end
80
- }; #code
81
- g_parser.optouter <<
82
- code.sub!('$$!!@ CST @!!$$', @visitor_code.join) <<
83
- unindent(@nodes_code) <<
84
- gen_action_decorator_code(target_name, paramkey,
85
- @on_reduce, @optouter)
86
- end
87
- %}
88
- %hook pre_rule_list /%CST\{\s*\Z/ skip
89
- %banner '%CST{ ... }'
90
- %%
91
-
92
- %LEX{
93
- /\s+/, /#.*/ { }
94
- /%\}\s*\Z/ { yield nil,nil }
95
- 'Node' { yield token(:NODE, $&) }
96
- '{' ! { ln = lineno; yield token(:ACTION, parse_action, ln) }
97
- %}
98
-
99
- #begin-rule
100
- start:
101
- opt_node opt_visitor
102
- {
103
- code = %{
104
- class Node
105
- $$!!@ NODE @!!$$
106
- end
107
- class Visitor
108
- $$!!@ VISITOR @!!$$
109
- end
110
- }; #code
111
- master.nodes_code <<
112
- code.sub!('$$!!@ NODE @!!$$', _opt_node) \
113
- .sub!('$$!!@ VISITOR @!!$$', _opt_visitor)
114
- }
115
- ;
116
- opt_node:
117
- { '' }
118
- | NODE ACTION { _ACTION.value }
119
- ;
120
- opt_visitor:
121
- { '' }
122
- | VISITOR ACTION { _ACTION.value }
123
- ;
124
- #end-rule
125
- %%
126
- %hook post_lhs /\[/ skip
127
- %banner '[ ... ]'
128
- %inner{
129
- def do_default
130
- master.nodes_code << %{
131
- class Node_#{master.lhs_name} < Node
132
- def initialize
133
- super()
134
- end
135
- end
136
- }; #code
137
- end
138
- %}
139
- %%
140
-
141
- %LEX{
142
- /\A\s+/ { } #skip blank
143
- /""/,/''/,/[0-9]+/,
144
- /\{\s*\}/,/\[\s*\]/,
145
- /nil/ { yield token(:LIT, $&) }
146
- /\A[a-z][a-z_]*/ { yield token(:ID, $&) }
147
- /\A(.)/ { yield token($&, $&) }
148
- %}
149
-
150
- #begin-rule
151
- start:
152
- '[' pair ']'
153
- {
154
- args = []; code = ''
155
- _pair.each do |id, lit|
156
- args << id
157
- code << "#{id} = #{lit}; " if lit
158
- end
159
- attrc = args.map{|i| ":#{i}"}.join(', ')
160
- master.nodes_code << %{
161
- class Node_#{master.lhs_name} < Node
162
- attr_accessor #{attrc}
163
- def initialize
164
- super()
165
- #{code}
166
- end
167
- end
168
- }; #code
169
- }
170
- ;
171
- pair:
172
- expr { [ _expr ] }
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 ('depager/plugins/lex.rb')
3
+ %extend Depager::Action ('depager/plugins/action.rb')
4
+ %decorate @Action
5
+ #%decorate Depager::LALR::ShiftReducePrinter ('depager/plugins/srp.rb')
6
+ %inner{
7
+ attr_accessor :action_code, :nodes_code, :visitor_code
8
+ def init_extension
9
+ @n = 0
10
+ @visitor_code = ''
11
+ @nodes_code = ''
12
+ @action_code = ''
13
+ @on_reduce = []
14
+ end
15
+
16
+ def modify_action_code code
17
+ code.gsub(/\$\.([a-z_])/, 'node.\1')
18
+ end
19
+
20
+ def post_rhs
21
+ code, lineno = '', g_parser.file.lineno
22
+ if g_parser.line =~ /\s*\{/
23
+ @original_line, @line = g_parser.original_line, g_parser.line
24
+ code = parse_block
25
+
26
+ g_parser.update_context @line
27
+ end
28
+
29
+ node_name = "#{g_parser.lhs_name}_#{g_parser.nrhs}"
30
+ accept = ""
31
+ nodes = g_parser.rhs.map do |i|
32
+ x = g_parser.int_to_sym(i)
33
+ if x.class == String
34
+ "tx#{x.unpack("H*")[0]}"
35
+ else
36
+ x = x.to_s
37
+ accept << "#{x}.accept(v); " if x =~ /\A[a-z_]+\Z/
38
+ x.downcase
39
+ end
40
+ end
41
+ attrc = nodes.map{|i| ":#{i}" }.join(', ')
42
+ inic = nodes.map{|i| "@#{i} = #{i}"}.join('; ')
43
+ @nodes_code << <<-CODE
44
+ class Node_#{node_name} < Node_#{g_parser.lhs_name}
45
+ attr_accessor #{attrc}
46
+ def initialize #{nodes.join(', ')}
47
+ super()
48
+ #{inic}
49
+ end
50
+ def accept v
51
+ #{accept}
52
+ v.visit_Node_#{node_name}(self)
53
+ end
54
+ def children
55
+ [#{nodes.join(', ')}]
56
+ end
57
+ end
58
+ CODE
59
+
60
+ @visitor_code <<
61
+ expand_inline_code(modify_action_code(code), lineno, :wrap => "def visit_Node_#{node_name} node")
62
+
63
+ args = nodes.inject([]){|r, i| r << "val[#{r.size}]"}
64
+ code = "#{target_namespace}::Node_#{node_name}.new(#{args.join(', ')})"
65
+ n = g_parser.rules.size-1
66
+ @action_code <<
67
+ expand_inline_code(code, lineno, :wrap => "def _cst_#{n} val")
68
+
69
+ @on_reduce[n] = ":_cst_#{n}"
70
+ end
71
+
72
+ def term_extension
73
+ g_parser.outer_code << "module #{target_namespace}\n"
74
+ g_parser.outer_code << @nodes_code
75
+ g_parser.outer_code << <<-CODE
76
+ class Visitor
77
+ #{@visitor_code}
78
+ end
79
+ CODE
80
+ g_parser.outer_code << "end\n"
81
+ g_parser.outer_code << generate_action_decorator_code(@on_reduce, @action_code)
82
+ end
83
+ %}
84
+ %hook pre_rule_list /%CST\{\s*\Z/ skip
85
+ %banner '%CST{ ... }'
86
+ %%
87
+
88
+ %LEX{
89
+ /\s+/, /#.*/ { }
90
+ /%\}\s*\Z/ { yield nil,nil }
91
+ 'Node' { yield token(:NODE, $&) }
92
+ 'Visitor' { yield token(:VISITOR, $&) }
93
+ '{' ! { lineno = file.lineno; yield token(:ACTION, parse_block, lineno) }
94
+ %}
95
+
96
+ #begin-rule
97
+ start:
98
+ opt_node opt_visitor
99
+ {
100
+ master.nodes_code << <<-CODE
101
+ class Node
102
+ def accept
103
+ end
104
+ #{_opt_node}
105
+ end
106
+ CODE
107
+ master.visitor_code << _opt_visitor
108
+ }
109
+ ;
110
+ opt_node:
111
+ { '' }
112
+ | NODE ACTION { _ACTION.value }
113
+ ;
114
+ opt_visitor:
115
+ { '' }
116
+ | VISITOR ACTION { _ACTION.value }
117
+ ;
118
+ #end-rule
119
+ %%
120
+ %hook post_lhs /\[/ skip
121
+ %banner '[ ... ]'
122
+ %inner{
123
+ def do_default
124
+ master.nodes_code << <<-CODE
125
+ class Node_#{g_parser.lhs_name} < Node
126
+ def initialize
127
+ super()
128
+ end
129
+ end
130
+ CODE
131
+ end
132
+ %}
133
+ %%
134
+
135
+ %LEX{
136
+ /\A\s+/ { } #skip blank
137
+ /""/,/''/,/[0-9]+/,
138
+ /\{\s*\}/,/\[\s*\]/,
139
+ /nil/ { yield token(:LIT, $&) }
140
+ /\A[a-z][a-z_]*/ { yield token(:ID, $&) }
141
+ /\A(.)/ { yield token($&, $&) }
142
+ %}
143
+
144
+ #begin-rule
145
+ start:
146
+ '[' pair ']'
147
+ {
148
+ args = []; code = ''
149
+ _pair.each do |id, lit|
150
+ args << id
151
+ code << "#{id} = #{lit}; " if lit
152
+ end
153
+ attrc = args.map{|i| ":#{i}"}.join(', ')
154
+ master.nodes_code << <<-CODE
155
+ class Node_#{g_parser.lhs_name} < Node
156
+ attr_accessor #{attrc}
157
+ def initialize
158
+ super
159
+ #{code}
160
+ end
161
+ end
162
+ CODE
163
+ }
164
+ ;
165
+ pair:
166
+ expr { [ _expr ] }
167
+ | pair ',' expr { _pair << _expr }
168
+ ;
169
+ expr:
170
+ ID { [ _ID.value, nil] }
171
+ | ID '=' LIT { [ _ID.value, _LIT.value ] }
172
+ ;
173
+ #end-rule
174
+ %%