-
Notifications
You must be signed in to change notification settings - Fork 1
/
testExpression.lua
65 lines (48 loc) · 1.69 KB
/
testExpression.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
local m = require'pegparser.parser'
local recovery = require'pegparser.recovery'
local pretty = require'pegparser.pretty'
local coder = require'pegparser.coder'
local function assertErr (p, s, lab)
local r, l, pos = p:match(s)
assert(not r, "Did not fail: r = " .. tostring(r))
if lab then
assert(l == lab, "Expected label '" .. tostring(lab) .. "' but got " .. tostring(l))
end
end
local function assertOk(p, s)
local r, l, pos = p:match(s)
assert(r, 'Failed: label = ' .. tostring(l) .. ', pos = ' .. tostring(pos))
assert(r == #s + 1, "Matched until " .. r)
end
local tree, rules = m.match[[
S <- (Expression !'=' / Assignment)* !.
Assignment <- Name '=' Expression
Expression <- Term ('+' Term)*
Term <- Factor ('*' Factor)*
Factor <- '(' Expression ')' / Name / Number
Name <- [a-z]+
Number <- [1-9][0-9]*]]
print(pretty.printg(tree, rules), '\n')
local treerec, rulesrec = recovery.addlab(tree, rules, false, true)
print(pretty.printg(treerec, rulesrec), '\n')
local tree, rules = m.match[[
S <- (Assignment / Expression)* !.
Assignment <- Name '=' Expression
Expression <- Term ('+' Term)*
Term <- Factor ('*' Factor)*
Factor <- '(' Expression ')' / Name / Number
Name <- [a-z]+
Number <- [1-9][0-9]*]]
print(pretty.printg(tree, rules), '\n')
local treerec, rulesrec = recovery.addlab(tree, rules, false, true)
print(pretty.printg(treerec, rulesrec), '\n')
local p = coder.makeg(treerec, rulesrec)
-- Valid programs
assertOk(p, "af=2")
assertOk(p, "test = (42)")
assertOk(p, "ok")
assertOk(p, "1 + 22 + (42*abc)")
assertOk(p, "a = 2\nb = 43")
-- Invalid programs
assertErr(p, "(3")
assertErr(p, "(3", "Err_006")