depager 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +4 -0
- data/Manifest.txt +52 -0
- data/README.en +64 -0
- data/README.ja +128 -0
- data/bin/depager +47 -0
- data/data/depager/misc/depager-mode.el +209 -0
- data/data/depager/sample/extension/paction.dr +15 -0
- data/data/depager/sample/extension/pactiontest.dr +14 -0
- data/data/depager/sample/pl0d/pl0ds.dr +334 -0
- data/data/depager/sample/pl0d/pl0test.pl0 +34 -0
- data/data/depager/sample/sample_calc/calc.action.dr +33 -0
- data/data/depager/sample/sample_calc/calc.astdf.dr +54 -0
- data/data/depager/sample/sample_calc/calc.astl.action.dr +66 -0
- data/data/depager/sample/sample_calc/calc.astl.dr +55 -0
- data/data/depager/sample/sample_calc/calc.atree.dr +43 -0
- data/data/depager/sample/sample_calc/calc.cst.dr +45 -0
- data/data/depager/sample/sample_calc/calc.dr +43 -0
- data/data/depager/sample/sample_calc/calc.lex.dr +29 -0
- data/data/depager/sample/sample_calc/calc.nvaction.dr +33 -0
- data/data/depager/sample/sample_calc/calc_prec.nvaction.dr +31 -0
- data/data/depager/sample/slex_test/slextest1.dr +37 -0
- data/data/depager/sample/slex_test/slextest2.dr +33 -0
- data/lib/depager.rb +608 -0
- data/lib/depager/Rakefile +30 -0
- data/lib/depager/action.rb +47 -0
- data/lib/depager/ast_base.dr +232 -0
- data/lib/depager/ast_base.rb +1249 -0
- data/lib/depager/astdf.rb +10 -0
- data/lib/depager/astl.rb +14 -0
- data/lib/depager/atree.dr +55 -0
- data/lib/depager/atree.rb +336 -0
- data/lib/depager/cst.dr +182 -0
- data/lib/depager/cst.rb +625 -0
- data/lib/depager/lex.dr +76 -0
- data/lib/depager/lex.rb +306 -0
- data/lib/depager/lr.rb +604 -0
- data/lib/depager/nvaction.rb +21 -0
- data/lib/depager/parse_action.rb +24 -0
- data/lib/depager/parser.rb +248 -0
- data/lib/depager/psrtmpl.rb +33 -0
- data/lib/depager/slex.dr +161 -0
- data/lib/depager/slex.rb +646 -0
- data/lib/depager/srp.rb +50 -0
- data/lib/depager/template/astdf.erbs +57 -0
- data/lib/depager/template/astl.erbs +57 -0
- data/lib/depager/template/extension_lalr_master.erb +51 -0
- data/lib/depager/template/extension_lalr_slave.erb +107 -0
- data/lib/depager/template/simple.erb +21 -0
- data/lib/depager/template/single_lalr_parser.erb +97 -0
- data/lib/depager/utils.rb +355 -0
- data/lib/depager/version.rb +9 -0
- data/setup.rb +1585 -0
- metadata +103 -0
data/lib/depager/srp.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
class ShiftReducePrinter < Depager::AdvancedParser
|
2
|
+
def initialize inside
|
3
|
+
super
|
4
|
+
end
|
5
|
+
def dumpstack stack
|
6
|
+
r = ''
|
7
|
+
_stack.each_with_index do |v,x|
|
8
|
+
r += if x % 2 == 0
|
9
|
+
"(#{v}) "
|
10
|
+
else
|
11
|
+
if v[1] == :NT
|
12
|
+
"#{@i2nt[v[0]]}:#{v[1..-1].inspect} "
|
13
|
+
elsif @i2t[v[0]].class == String
|
14
|
+
"'#{@i2t[v[0]]}' "
|
15
|
+
else
|
16
|
+
"#{@i2t[v[0]]}:#{v[1..-1].inspect} "
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
r
|
21
|
+
end
|
22
|
+
def afterShift
|
23
|
+
st0 = _stack[_stack.size-3]
|
24
|
+
st = _stack.last
|
25
|
+
sh = @i2t[_stack[_stack.size-2][0]]
|
26
|
+
la = @i2t[_lookahead[0]]
|
27
|
+
warn "LA:<#{la}> shift:<#{sh}> state:#{st0}->#{st}"
|
28
|
+
warn "LINE:<#{@basis.line}>"
|
29
|
+
warn dumpstack(_stack) + "\n"
|
30
|
+
end
|
31
|
+
def afterReduce
|
32
|
+
st = _stack.last
|
33
|
+
re = @i2nt[_stack[_stack.size-2][0]]
|
34
|
+
la = @i2t[_lookahead[0]]
|
35
|
+
warn "LA:<#{la}> reduce:<#{re}> state: ->#{st}"
|
36
|
+
warn "LINE:<#{@basis.line}>"
|
37
|
+
warn dumpstack(_stack) + "\n"
|
38
|
+
end
|
39
|
+
def afterAccept
|
40
|
+
warn "acc."
|
41
|
+
end
|
42
|
+
def afterError
|
43
|
+
st = _stack.last
|
44
|
+
re = @i2nt[_stack[_stack.size-2][0]]
|
45
|
+
la = @i2t[_lookahead[0]]
|
46
|
+
warn "LA:<#{la}> last_state:#{st}"
|
47
|
+
warn "LINE:<#{@basis.line}>"
|
48
|
+
warn dumpstack(_stack) + "\n"
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
===:defnode
|
2
|
+
class Node
|
3
|
+
attr_accessor :lineno
|
4
|
+
attr_accessor <%= _opt_attr.map{|i| ":#{i}" }.join(', ') %>
|
5
|
+
<%= ini %>
|
6
|
+
def to_ary
|
7
|
+
[self]
|
8
|
+
end
|
9
|
+
def self.[] lineno, *args
|
10
|
+
self.new lineno, *args
|
11
|
+
end
|
12
|
+
def accept v
|
13
|
+
end
|
14
|
+
end
|
15
|
+
require 'forwardable'
|
16
|
+
class NodeList < Node
|
17
|
+
attr_accessor :lineno
|
18
|
+
extend Forwardable
|
19
|
+
_methods = Array.instance_methods -
|
20
|
+
Object.instance_methods -
|
21
|
+
['push', 'concat', 'to_ary', 'map']
|
22
|
+
def_delegators(:@lst, *_methods)
|
23
|
+
def initialize(lineno, lst=[])
|
24
|
+
@lineno = lineno
|
25
|
+
@lst = lst.to_a
|
26
|
+
end
|
27
|
+
<%=
|
28
|
+
_opt_attr.map {|i| ;%{
|
29
|
+
def #{i}
|
30
|
+
@lst.map{|i| "i.#{i}"}
|
31
|
+
end
|
32
|
+
}}.join("\n ")
|
33
|
+
%>
|
34
|
+
def push(i) @lst.push i; self; end
|
35
|
+
def concat(i) @lst.concat i; self; end
|
36
|
+
def map(&b) @lst.map(&b); end
|
37
|
+
def to_ary() @lst; end
|
38
|
+
def accept(v)
|
39
|
+
@lst.each{|i| i.accept(v) }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
===:node
|
43
|
+
class Node_<%= name %> < Node
|
44
|
+
attr_accessor <%= nodes.map{|i| ":#{i}" }.join(', ') %>
|
45
|
+
attr_accessor <%= attrs.map{|i| ":#{i}" }.join(', ') %>
|
46
|
+
def initialize <%= nodes.unshift('lineno').join(', ') %>
|
47
|
+
super()
|
48
|
+
@lineno = lineno
|
49
|
+
<%= nodes.map{|i| " @#{i} = #{i}\n"} %>
|
50
|
+
end
|
51
|
+
def accept v
|
52
|
+
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
53
|
+
<%= accept %>
|
54
|
+
v.visit_Node_<%= name %>(self)
|
55
|
+
self
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
===:defnode
|
2
|
+
class Node
|
3
|
+
attr_accessor :lineno
|
4
|
+
attr_accessor <%= _opt_attr.map{|i| ":#{i}" }.join(', ') %>
|
5
|
+
<%= ini %>
|
6
|
+
def to_ary
|
7
|
+
[self]
|
8
|
+
end
|
9
|
+
def self.[] lineno, *args
|
10
|
+
self.new lineno, *args
|
11
|
+
end
|
12
|
+
def accept v
|
13
|
+
end
|
14
|
+
end
|
15
|
+
require 'forwardable'
|
16
|
+
class NodeList < Node
|
17
|
+
attr_accessor :lineno
|
18
|
+
extend Forwardable
|
19
|
+
_methods = Array.instance_methods -
|
20
|
+
Object.instance_methods -
|
21
|
+
['push', 'concat', 'to_ary', 'map']
|
22
|
+
def_delegators(:@lst, *_methods)
|
23
|
+
def initialize(lineno, lst=[])
|
24
|
+
@lineno = lineno
|
25
|
+
@lst = lst.to_a
|
26
|
+
end
|
27
|
+
<%=
|
28
|
+
_opt_attr.map {|i| ;%{
|
29
|
+
def #{i}
|
30
|
+
@lst.map{|i| "i.#{i}"}
|
31
|
+
end
|
32
|
+
}}.join("\n ")
|
33
|
+
%>
|
34
|
+
def push(i) @lst.push i; self; end
|
35
|
+
def concat(i) @lst.concat i; self; end
|
36
|
+
def map(&b) @lst.map(&b); end
|
37
|
+
def to_ary() @lst; end
|
38
|
+
def accept(v)
|
39
|
+
@lst.each{|i| i.accept(v) }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
===:node
|
43
|
+
class Node_<%= name %> < Node
|
44
|
+
attr_accessor <%= nodes.map{|i| ":#{i}" }.join(', ') %>
|
45
|
+
attr_accessor <%= attrs.map{|i| ":#{i}" }.join(', ') %>
|
46
|
+
def initialize <%= nodes.unshift('lineno').join(', ') %>
|
47
|
+
super()
|
48
|
+
@lineno = lineno
|
49
|
+
<%= nodes.map{|i| " @#{i} = #{i}\n"} %>
|
50
|
+
end
|
51
|
+
<%= accept %>
|
52
|
+
def accept v
|
53
|
+
warn @lineno.to_s+':'+self.class.to_s if $DEBUG
|
54
|
+
v.visit_Node_<%= name %>(self)
|
55
|
+
self
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
<%
|
2
|
+
target_name = @d_parser.target_name
|
3
|
+
optouter = @optouter
|
4
|
+
optinner = @optinner
|
5
|
+
optmain = @optmain
|
6
|
+
req = @req.uniq
|
7
|
+
paramkey = @paramkey || ":default"
|
8
|
+
regs = @regs
|
9
|
+
mixin = @mixin.uniq
|
10
|
+
%>
|
11
|
+
###
|
12
|
+
### <%=target_name%> - Depager Extension (master)
|
13
|
+
###
|
14
|
+
require 'pp.rb'
|
15
|
+
require 'depager/parser.rb'
|
16
|
+
<%req.each do |i|%>require <%=i%>
|
17
|
+
<%end%>
|
18
|
+
|
19
|
+
module D4<%= target_name %> #:nodoc:all
|
20
|
+
end
|
21
|
+
|
22
|
+
class <%= target_name %> #:nodoc:all
|
23
|
+
include Depager::ExtensionUtils
|
24
|
+
<% mixin.each do |i| %>
|
25
|
+
include <%=i%><%end%>
|
26
|
+
attr_accessor :h2p
|
27
|
+
|
28
|
+
def initialize
|
29
|
+
@master = self
|
30
|
+
@hook = []
|
31
|
+
@h2p = {}
|
32
|
+
init_parser
|
33
|
+
end
|
34
|
+
|
35
|
+
def __regext__ p
|
36
|
+
super p, <%= paramkey %>, nil
|
37
|
+
<% x = 0; h = {}; regs.each do |i, m|; if m%>
|
38
|
+
<%unless h[m[1]]%>@hook[<%=x%>] = <%=m[1]%>.new(p, @nparam, self)<%h[m[1]]=x%>
|
39
|
+
@h2p['<%=m[1].sub(/#{target_name}_/,'')%>'] = @hook[<%=h[m[1]]%>] <%else
|
40
|
+
%># use @hook[<%=h[m[1]]%>](<%=m[1]%>)<%end%>
|
41
|
+
p.<%=i%>.push [@hook[<%=h[m[1]]%>], :do_parse]
|
42
|
+
<% x+=1; end; end%>
|
43
|
+
regext p
|
44
|
+
end
|
45
|
+
|
46
|
+
### Inner Code
|
47
|
+
<%= optinner %>
|
48
|
+
end
|
49
|
+
|
50
|
+
### Outer Code
|
51
|
+
<%= optouter %>
|
@@ -0,0 +1,107 @@
|
|
1
|
+
<%
|
2
|
+
target_name = g_parser.target_name
|
3
|
+
table = g_parser.table
|
4
|
+
g = table.g
|
5
|
+
rulelist = g.rulelist
|
6
|
+
t2i = (g.nssize ... g.syms.size).map{|i| [ g.syms[i].inspect, i - g.nssize] }
|
7
|
+
nt2i = (1 ... g.nssize).map{|i| [ ":#{g.syms[i]}", i-1] }
|
8
|
+
taction = table.action_table
|
9
|
+
tgoto = table.goto_table
|
10
|
+
tdefred = table.defred_table
|
11
|
+
tdefreds = table.defred_after_shift_table
|
12
|
+
optouter = g_parser.optouter
|
13
|
+
optinner = g_parser.optinner
|
14
|
+
optmain = g_parser.optmain
|
15
|
+
nparams = g_parser.nparams
|
16
|
+
decparser = @deco.inject(@basis_name) { |r, i|
|
17
|
+
"#{i.sub(/^@/, "D4#{target_name}::")}.new(#{r})"
|
18
|
+
}
|
19
|
+
banner ||= target_name
|
20
|
+
mixin = mixin.uniq
|
21
|
+
%>
|
22
|
+
###
|
23
|
+
### <%=target_name%> - Part of Depager Extension (slave)
|
24
|
+
###
|
25
|
+
module D4<%= target_name %> #:nodoc:all
|
26
|
+
end
|
27
|
+
|
28
|
+
class <%= target_name %> < Basis #:nodoc:all
|
29
|
+
include Depager::ExtensionUtils
|
30
|
+
<% mixin.each do |i| %>
|
31
|
+
include <%=i%><%end%>
|
32
|
+
|
33
|
+
### Reduce Table
|
34
|
+
reduce_table = [<% rulelist.each do |i| %>
|
35
|
+
[ <%= i.l - 1%>, <%= i.r.size %> ],<% end %>
|
36
|
+
]
|
37
|
+
### Extension Params
|
38
|
+
nparams = {<% nparams.each do |c, i| %>
|
39
|
+
'<%= c %>' => <%= i %>, <% end %>
|
40
|
+
}
|
41
|
+
### Term to Int
|
42
|
+
t2i = {<% t2i.each do |k, v| %>
|
43
|
+
<%= k %> => <%= v %>, <% end %>
|
44
|
+
}
|
45
|
+
### Int to Term
|
46
|
+
i2t = [<% t2i.each do |k, v| %>
|
47
|
+
<%= k %>, <% end %>
|
48
|
+
]
|
49
|
+
### Action Table
|
50
|
+
action_table = [<% taction.each do |i| %>
|
51
|
+
[ <% i.each do |j| %><%= j ? "#{j}" : 'nil' %>, <% end %>], <% end %>
|
52
|
+
]
|
53
|
+
### Default Reduce Table
|
54
|
+
defred_table = [<% tdefred.each do |i| %>
|
55
|
+
<%= i ? "#{i}" : 'nil' %>, <% end %>
|
56
|
+
]
|
57
|
+
defred_after_shift_table = [<% tdefreds.each do |i| %>
|
58
|
+
<%= i ? "#{i}" : 'nil' %>, <% end %>
|
59
|
+
]
|
60
|
+
### Nonterm to Int
|
61
|
+
nt2i = {<% nt2i.each do |k, v| %>
|
62
|
+
<%= k %> => <%= v %>, <% end %>
|
63
|
+
}
|
64
|
+
### Int to Nonterm
|
65
|
+
i2nt = [<% nt2i.each do |k, v| %>
|
66
|
+
<%= k %>, <% end %>
|
67
|
+
]
|
68
|
+
### Goto Table
|
69
|
+
goto_table = [ <% tgoto.each do |i| %>
|
70
|
+
[ <% i.each do |j| %><%= j ? "#{j}" : 'nil' %>, <% end %>], <% end %>
|
71
|
+
]
|
72
|
+
|
73
|
+
Tables = [ reduce_table, nparams, action_table,
|
74
|
+
defred_table, defred_after_shift_table, goto_table,
|
75
|
+
t2i, i2t, nt2i, i2nt ]
|
76
|
+
|
77
|
+
attr_accessor :p, :m
|
78
|
+
def initialize p, nparam, master = nil
|
79
|
+
super()
|
80
|
+
@g_parser = @p = p
|
81
|
+
@master = @m = master
|
82
|
+
@nparam = nparam
|
83
|
+
@dect = <%=decparser%>
|
84
|
+
init_parser
|
85
|
+
end
|
86
|
+
|
87
|
+
<% if mtsk %>
|
88
|
+
def do_parse?
|
89
|
+
if @line =~ /^\s*<%= mtsk[0] %>/
|
90
|
+
<% if mtsk[1] %> @line = $' <% end %>
|
91
|
+
true
|
92
|
+
else
|
93
|
+
false
|
94
|
+
end
|
95
|
+
end
|
96
|
+
<% end %>
|
97
|
+
|
98
|
+
def banner
|
99
|
+
"<%= banner %> / #{@master.class.name}"
|
100
|
+
end
|
101
|
+
|
102
|
+
### Inner Code
|
103
|
+
<%= optinner %>
|
104
|
+
end
|
105
|
+
|
106
|
+
### Outer Code
|
107
|
+
<%= optouter %>
|
@@ -0,0 +1,21 @@
|
|
1
|
+
<%
|
2
|
+
optouter = @optouter + g_parser.optouter
|
3
|
+
optmain = @optmain
|
4
|
+
req = @req.uniq
|
5
|
+
mixin = @mixin
|
6
|
+
%>
|
7
|
+
begin; require 'rubygems'; rescue Exception; end
|
8
|
+
require 'depager/parser.rb'
|
9
|
+
<%req.each do |i|%>require <%=i%>
|
10
|
+
<%end%>
|
11
|
+
|
12
|
+
module D4<%= target_name %> ; end
|
13
|
+
|
14
|
+
### Outer Code
|
15
|
+
<%= optouter %>
|
16
|
+
|
17
|
+
### main
|
18
|
+
if __FILE__ == $0
|
19
|
+
### Main Code
|
20
|
+
<%= optmain %>
|
21
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
<%
|
2
|
+
target_name = g_parser.target_name
|
3
|
+
table = g_parser.table
|
4
|
+
g = table.g
|
5
|
+
nparams = g_parser.nparams
|
6
|
+
rulelist = g.rulelist
|
7
|
+
t2i = (g.nssize ... g.syms.size).map{|i| [ g.syms[i].inspect, i - g.nssize] }
|
8
|
+
nt2i = (1 ... g.nssize).map{|i| [ ":#{g.syms[i]}", i-1] }
|
9
|
+
taction = table.action_table
|
10
|
+
tgoto = table.goto_table
|
11
|
+
tdefred = table.defred_table
|
12
|
+
tdefreds = table.defred_after_shift_table
|
13
|
+
optouter = @optouter + g_parser.optouter
|
14
|
+
optinner = @optinner + g_parser.optinner
|
15
|
+
optmain = @optmain
|
16
|
+
decparser = @deco.inject(@basis_name) { |r, i|
|
17
|
+
"#{i.sub(/^@/, "D4#{target_name}::")}.new(#{r})"
|
18
|
+
}
|
19
|
+
req = @req.uniq
|
20
|
+
mixin = @mixin
|
21
|
+
%>
|
22
|
+
begin; require 'rubygems'; rescue Exception; end
|
23
|
+
require 'depager/parser.rb'
|
24
|
+
<%req.each do |i|%>require <%=i%>
|
25
|
+
<%end%>
|
26
|
+
|
27
|
+
module D4<%= target_name %>
|
28
|
+
end
|
29
|
+
|
30
|
+
class <%= target_name %> < Depager::Basis
|
31
|
+
<% mixin.each do |i| %>
|
32
|
+
include <%=i%><%end%>
|
33
|
+
|
34
|
+
### Reduce Table
|
35
|
+
reduce_table = [<% rulelist.each do |i| %>
|
36
|
+
[ <%= '%04s'%(i.l - 1)%>, <%= "%04s"%i.r.size %> ], # <%= i %><% end %>
|
37
|
+
]
|
38
|
+
### Extension Params
|
39
|
+
nparams = {<% nparams.each do |c, i| %>
|
40
|
+
'<%= c %>' => <%= i %>, <% end %>
|
41
|
+
}
|
42
|
+
### Term to Int
|
43
|
+
t2i = {<% t2i.each do |k, v| %>
|
44
|
+
<%= k %> => <%= v %>, <% end %>
|
45
|
+
}
|
46
|
+
### Int to Term
|
47
|
+
i2t = [<% t2i.each do |k, v| %>
|
48
|
+
<%= k %>, <% end %>
|
49
|
+
]
|
50
|
+
### Action Table
|
51
|
+
action_table = [<% taction.each do |i| %>
|
52
|
+
[ <% i.each do |j| %><%= j ? "#{j}" : 'nil' %>, <% end %>], <% end %>
|
53
|
+
]
|
54
|
+
### Default Reduce Table
|
55
|
+
defred_table = [<% tdefred.each do |i| %>
|
56
|
+
<%= i ? "#{i}" : 'nil' %>, <% end %>
|
57
|
+
]
|
58
|
+
defred_after_shift_table = [<% tdefreds.each do |i| %>
|
59
|
+
<%= i ? "#{i}" : 'nil' %>, <% end %>
|
60
|
+
]
|
61
|
+
### Nonterm to Int
|
62
|
+
nt2i = {<% nt2i.each do |k, v| %>
|
63
|
+
<%= k %> => <%= v %>, <% end %>
|
64
|
+
}
|
65
|
+
### Int to Nonterm
|
66
|
+
i2nt = [<% nt2i.each do |k, v| %>
|
67
|
+
<%= k %>, <% end %>
|
68
|
+
]
|
69
|
+
### Goto Table
|
70
|
+
goto_table = [ <% tgoto.each do |i| %>
|
71
|
+
[ <% i.each do |j| %><%= j ? "#{j}" : 'nil' %>, <% end %>], <% end %>
|
72
|
+
]
|
73
|
+
|
74
|
+
Tables = [ reduce_table, nparams, action_table,
|
75
|
+
defred_table, defred_after_shift_table, goto_table,
|
76
|
+
t2i, i2t, nt2i, i2nt ]
|
77
|
+
|
78
|
+
def self.createDecoratedParser
|
79
|
+
<%= decparser %>
|
80
|
+
end
|
81
|
+
|
82
|
+
### Inner Code
|
83
|
+
<%= optinner %>
|
84
|
+
end
|
85
|
+
|
86
|
+
### Outer Code
|
87
|
+
<%= optouter %>
|
88
|
+
|
89
|
+
def createDecorated<%= target_name %>
|
90
|
+
<%= decparser %>
|
91
|
+
end
|
92
|
+
|
93
|
+
### main
|
94
|
+
if __FILE__ == $0
|
95
|
+
### Main Code
|
96
|
+
<%= optmain %>
|
97
|
+
end
|