Skip to content

Commit 3b733cf

Browse files
committed
Fix up ll1 parser to handle requires
1 parent 433e419 commit 3b733cf

File tree

7 files changed

+667
-444
lines changed

7 files changed

+667
-444
lines changed

luainlua-0.1-0.rockspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ build = {
4747
['luainlua.luac'] = 'luainlua/luac.lua',
4848
['luainlua.parsing.lex'] = 'luainlua/parsing/lex.lua',
4949
['luainlua.parsing.ll1_grammar'] = 'luainlua/parsing/ll1_grammar.lua',
50+
['luainlua.parsing.ll1_parsing_table'] = 'luainlua/parsing/ll1_parsing_table.lua',
5051
['luainlua.parsing.ll1_tokenizer'] = 'luainlua/parsing/ll1_tokenizer.lua',
5152
['luainlua.parsing.re'] = 'luainlua/parsing/re.lua',
5253
},

luainlua/ll1/ll1.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ local follow_algorithm = worklist {
8383
return {}
8484
end,
8585
transfer = function(self, node, follow_pred, graph, pred)
86+
if node == 'root' then return {[EOF] = true} end
87+
8688
local follow_set = self:initialize(node)
8789
local configuration = graph.configuration
8890
for suffix in pairs(graph.forward[pred][node]) do
@@ -299,7 +301,6 @@ end
299301
function ll1.yacc(actions)
300302
-- Associate the correct set of metatables to the nonterminals
301303
local configuration = ll1.configure(actions)
302-
303304
local first_sets = configuration:firsts()
304305
local follow_sets = configuration:follows()
305306
local terminals = get_terminals_from(configuration)
@@ -500,8 +501,8 @@ function ll1.create(actions)
500501
return ll1.yacc(actions)
501502
end
502503

503-
local bundle = require(file)
504-
if not bundle then
504+
local status, bundle = pcall(require, file:gsub('/', '.'):gsub('.lua$', ''))
505+
if not status then
505506
local transitions = ll1.yacc(actions)
506507
return transitions:save(file)
507508
end

luainlua/lua/parser.lua

Lines changed: 437 additions & 437 deletions
Large diffs are not rendered by default.

luainlua/lua/parser_table.lua

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

luainlua/parsing/ll1_grammar.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ end
7575
print("Warning", "Are you sure you want to disable caching of this grammar? Specify %FILE otherwise.")
7676
end
7777
if not self.requires then
78-
self.requires = {'ll1.ll1'}
78+
self.requires = {'luainlua.ll1.ll1'}
7979
end
8080
if not self.default_action then
8181
self.default_action = 'function(...) return {...} end'
@@ -298,8 +298,8 @@ local grammar = ll1 {
298298
end},
299299
{'REQUIRE', 'STRING', '$configuration_',
300300
action = function(_, namespace, last)
301-
if not last.requires then last.requires = {'ll1.ll1'} end
302-
if namespace[2] ~= 'll1.ll1' then
301+
if not last.requires then last.requires = {'luainlua.ll1.ll1'} end
302+
if namespace[2] ~= 'luainlua.ll1.ll1' then
303303
table.insert(last.requires, 1, namespace[2])
304304
end
305305
return last
@@ -649,6 +649,7 @@ local function parse(str)
649649
end
650650

651651
local code, configuration = parse(io.open('luainlua/lua/grammar.ylua'):read("*all"))
652+
652653
os.remove(configuration.file .. '_table.lua')
653654
local func, status = loadstring(code)
654655
if not func then

testing/ew.dot

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
digraph {
2+
rankdir=LR;
3+
size="8,5"
4+
node[shape=circle,label=""];
5+
funcbody;
6+
root;
7+
exp5';
8+
retstat'maybe#1;
9+
parlist'group#2;
10+
stat'star#1;
11+
exp_stop'star#1;
12+
level7;
13+
stat'maybe#1;
14+
explist'group#1;
15+
stat'group#4;
16+
level3;
17+
field'maybe#2;
18+
args'maybe#1;
19+
assignment'star#1;
20+
tableconstructor'star#1;
21+
funcname'star#1;
22+
exp4'group#1;
23+
funcname;
24+
fieldsep;
25+
level6;
26+
exp6;
27+
stat'group#2'maybe#1;
28+
funcname'group#2;
29+
level8;
30+
field'maybe#3;
31+
stat'group#1'maybe#1;
32+
assignment;
33+
functiondef;
34+
stat'group#3;
35+
exp3;
36+
assignment_or_call;
37+
assignment_or_call'star#1;
38+
namelist;
39+
assignment_or_call'group#1;
40+
label;
41+
exp8'maybe#1;
42+
exp4;
43+
exp2';
44+
level2;
45+
level1;
46+
unop;
47+
suffix;
48+
stat'group#1;
49+
binop;
50+
exp';
51+
explist'star#1;
52+
exp4'maybe#1;
53+
funcname'maybe#1;
54+
namelist'group#1;
55+
exp8'group#1;
56+
stat;
57+
parlist'star#1;
58+
exp8;
59+
parlist'group#1;
60+
field'maybe#1;
61+
primaryexp;
62+
retstat;
63+
args;
64+
explist;
65+
exp;
66+
stat'group#2;
67+
parlist;
68+
stat'group#1'group#1;
69+
level5;
70+
block'maybe#1;
71+
exp5;
72+
namelist'star#1;
73+
stat'group#2'group#1;
74+
block;
75+
retstat'maybe#2;
76+
exp3';
77+
level4;
78+
tableconstructor;
79+
assignment_or_call'maybe#1;
80+
exp_stop;
81+
funcbody'maybe#1;
82+
field;
83+
funcname'group#1;
84+
exp7;
85+
exp6';
86+
exp2;
87+
block'star#1;
88+
funcbody -> block[label="table: 0x106ce00"];
89+
funcbody -> funcbody'maybe#1[label="table: 0x1057710"];
90+
root -> block[label="table: 0x106df30"];
91+
exp5' -> exp6[label="table: 0x10853d0"];
92+
exp5' -> exp5'[label="table: 0x1057dd0"];
93+
exp5' -> level5[label="table: 0x1056c80"];
94+
exp5 -> exp5'[label="table: 0xfa0f50"];
95+
exp5 -> exp6[label="table: 0x10a6f60"];
96+
stat'star#1 -> stat'group#3[label="table: 0xfaf9f0"];
97+
stat'star#1 -> stat'star#1[label="table: 0x10234a0"];
98+
exp_stop'star#1 -> exp_stop'star#1[label="table: 0x108b280"];
99+
exp_stop'star#1 -> suffix[label="table: 0xfe3e40"];
100+
exp4'maybe#1 -> exp4'group#1[label="table: 0xf85c90"];
101+
explist'group#1 -> exp[label="table: 0x10cb020"];
102+
stat'group#4 -> block[label="table: 0xffb900"];
103+
field'maybe#2 -> fieldsep[label="table: 0x107b480"];
104+
args'maybe#1 -> explist[label="table: 0xf91450"];
105+
assignment'star#1 -> suffix[label="table: 0x10d00a0"];
106+
assignment'star#1 -> assignment'star#1[label="table: 0x10f19f0"];
107+
tableconstructor'star#1 -> tableconstructor'star#1[label="table: 0x1021c00"];
108+
tableconstructor'star#1 -> field[label="table: 0x10f3650"];
109+
funcname'star#1 -> funcname'group#1[label="table: 0x10d8600"];
110+
funcname'star#1 -> funcname'star#1[label="table: 0x10f97a0"];
111+
exp4'group#1 -> exp4[label="table: 0xf860d0"];
112+
exp4'group#1 -> level4[label="table: 0x1007aa0"];
113+
funcname -> funcname'maybe#1[label="table: 0x10d3d30"];
114+
funcname -> funcname'star#1[label="table: 0x1013030"];
115+
exp6 -> exp6'[label="table: 0x1091c30"];
116+
exp6 -> exp7[label="table: 0xfb9030"];
117+
stat'group#2'maybe#1 -> stat'group#2'group#1[label="table: 0x1026300"];
118+
field'maybe#3 -> fieldsep[label="table: 0x10016f0"];
119+
stat'group#1'maybe#1 -> stat'group#1'group#1[label="table: 0x101c970"];
120+
assignment -> primaryexp[label="table: 0xf9d0e0"];
121+
assignment -> explist[label="table: 0x10af940"];
122+
assignment -> assignment[label="table: 0xfb4db0"];
123+
assignment -> assignment'star#1[label="table: 0xffd5b0"];
124+
functiondef -> funcbody[label="table: 0x103ee30"];
125+
stat'group#3 -> block[label="table: 0x10406b0"];
126+
stat'group#3 -> exp[label="table: 0x1042660"];
127+
exp3 -> exp4[label="table: 0x1008bd0"];
128+
exp3 -> exp3'[label="table: 0xfcb8a0"];
129+
assignment_or_call -> primaryexp[label="table: 0xfb1fb0"];
130+
assignment_or_call -> assignment_or_call'star#1[label="table: 0x1045eb0"];
131+
assignment_or_call -> assignment_or_call'maybe#1[label="table: 0xfcbfe0"];
132+
assignment_or_call'star#1 -> assignment_or_call'star#1[label="table: 0x10408a0"];
133+
assignment_or_call'star#1 -> assignment_or_call'group#1[label="table: 0x10c9c40"];
134+
namelist -> namelist'star#1[label="table: 0x1039a30"];
135+
assignment_or_call'group#1 -> exp[label="table: 0x107fa00"];
136+
assignment_or_call'group#1 -> args[label="table: 0xf9ae00"];
137+
exp4 -> exp4'maybe#1[label="table: 0x1008c30"];
138+
exp4 -> exp5[label="table: 0xf95fa0"];
139+
exp2' -> level2[label="table: 0x1053430"];
140+
exp2' -> exp3[label="table: 0x10916d0"];
141+
exp2' -> exp2'[label="table: 0x1100c30"];
142+
exp8'maybe#1 -> exp8'group#1[label="table: 0x1110960"];
143+
suffix -> exp[label="table: 0x1083070"];
144+
suffix -> args[label="table: 0x10a2aa0"];
145+
stat'group#1 -> funcbody[label="table: 0xff56e0"];
146+
stat'group#1 -> namelist[label="table: 0x10722f0"];
147+
stat'group#1 -> stat'group#1'maybe#1[label="table: 0x10fd070"];
148+
exp' -> exp2[label="table: 0xfcef90"];
149+
exp' -> exp'[label="table: 0x1110710"];
150+
exp' -> level1[label="table: 0xfceb60"];
151+
explist'star#1 -> explist'group#1[label="table: 0x100a150"];
152+
explist'star#1 -> explist'star#1[label="table: 0x1098060"];
153+
funcname'maybe#1 -> funcname'group#2[label="table: 0x104a660"];
154+
stat'maybe#1 -> stat'group#4[label="table: 0x103d9b0"];
155+
exp8'group#1 -> exp8[label="table: 0x10667d0"];
156+
exp8'group#1 -> level8[label="table: 0xfed660"];
157+
stat -> funcbody[label="table: 0xffe280"];
158+
stat -> block[label="table: 0x101b7e0"];
159+
stat -> stat'maybe#1[label="table: 0x103d020"];
160+
stat -> assignment_or_call[label="table: 0xf7b4b0"];
161+
stat -> exp[label="table: 0x10f94c0"];
162+
stat -> stat'group#2[label="table: 0x101d370"];
163+
stat -> stat'group#1[label="table: 0xfd39e0"];
164+
stat -> label[label="table: 0xffba20"];
165+
stat -> funcname[label="table: 0x10d0740"];
166+
stat -> stat'star#1[label="table: 0xf898a0"];
167+
exp8 -> exp_stop[label="table: 0x1065f60"];
168+
exp8 -> exp8'maybe#1[label="table: 0x1095370"];
169+
parlist'star#1 -> parlist'group#1[label="table: 0x1109c50"];
170+
parlist'star#1 -> parlist'star#1[label="table: 0x102a180"];
171+
field'maybe#1 -> fieldsep[label="table: 0x10aab00"];
172+
primaryexp -> exp[label="table: 0xf94430"];
173+
stat'group#2'group#1 -> exp[label="table: 0xff0920"];
174+
args -> args'maybe#1[label="table: 0xfb4450"];
175+
args -> tableconstructor[label="table: 0x10d2000"];
176+
explist -> explist'star#1[label="table: 0x1018d70"];
177+
explist -> exp[label="table: 0x1047430"];
178+
exp -> exp'[label="table: 0xfa1f40"];
179+
exp -> exp2[label="table: 0xf84fc0"];
180+
stat'group#2 -> exp[label="table: 0x10456b0"];
181+
stat'group#2 -> block[label="table: 0xf89440"];
182+
stat'group#2 -> explist[label="table: 0x10bac50"];
183+
stat'group#2 -> namelist[label="table: 0x108bbd0"];
184+
stat'group#2 -> stat'group#2'maybe#1[label="table: 0x10ff0d0"];
185+
parlist -> parlist'group#2[label="table: 0x1067440"];
186+
parlist -> parlist'star#1[label="table: 0x10b0fc0"];
187+
stat'group#1'group#1 -> explist[label="table: 0x106d0c0"];
188+
exp2 -> exp3[label="table: 0x103a840"];
189+
exp2 -> exp2'[label="table: 0x1076760"];
190+
retstat'maybe#1 -> explist[label="table: 0x102a9a0"];
191+
namelist'star#1 -> namelist'star#1[label="table: 0x10f06b0"];
192+
namelist'star#1 -> namelist'group#1[label="table: 0x1083240"];
193+
block -> block'maybe#1[label="table: 0x1095bd0"];
194+
block -> block'star#1[label="table: 0x104a160"];
195+
block'maybe#1 -> retstat[label="table: 0x1097230"];
196+
retstat -> retstat'maybe#2[label="table: 0x107f3b0"];
197+
retstat -> retstat'maybe#1[label="table: 0x1111200"];
198+
tableconstructor -> tableconstructor'star#1[label="table: 0x103b7d0"];
199+
assignment_or_call'maybe#1 -> assignment[label="table: 0xffea10"];
200+
exp_stop -> exp_stop'star#1[label="table: 0xffacc0"];
201+
exp_stop -> functiondef[label="table: 0xfe18d0"];
202+
exp_stop -> tableconstructor[label="table: 0xfdaa50"];
203+
exp_stop -> primaryexp[label="table: 0xfd6c00"];
204+
funcbody'maybe#1 -> parlist[label="table: 0xffa900"];
205+
field -> field'maybe#2[label="table: 0x10550d0"];
206+
field -> exp[label="table: 0x10d0b80"];
207+
field -> field'maybe#3[label="table: 0xf81c50"];
208+
field -> field'maybe#1[label="table: 0x1093860"];
209+
exp3' -> exp3'[label="table: 0xff5f30"];
210+
exp3' -> level3[label="table: 0x110cb20"];
211+
exp3' -> exp4[label="table: 0xfe3700"];
212+
exp6' -> exp6'[label="table: 0x10ab7e0"];
213+
exp6' -> level6[label="table: 0x10bb210"];
214+
exp6' -> exp7[label="table: 0x10f0630"];
215+
exp7 -> level7[label="table: 0x1085180"];
216+
exp7 -> exp8[label="table: 0xfd6ee0"];
217+
exp7 -> exp7[label="table: 0x1063aa0"];
218+
block'star#1 -> stat[label="table: 0x1054900"];
219+
block'star#1 -> block'star#1[label="table: 0x10947f0"];
220+
}

0 commit comments

Comments
 (0)