-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.lua
109 lines (69 loc) · 2.33 KB
/
test.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
local m = require'pegparser.parser'
local errinfo = require'pegparser.syntax_errors'
local pretty = require'pegparser.pretty'
local coder = require'pegparser.coder'
local first = require'pegparser.first'
local function assertlab (g, lab)
local r, msg, pos = m.match(g)
local errMsg = errinfo[lab]
assert(errMsg, "InvalidLabel '" .. lab .. "'")
if r then pretty.printg(r, msg) end
assert(string.find(msg, errMsg, 1, true), "Expected label '" .. lab .. "'")
end
--testing labels
io.write("Testing labels... ")
assertlab([[a <- 'b' 3 ]], 'Extra')
assertlab([[]], 'Rule')
assertlab([[a <- ]], 'ExpRule')
assertlab([[a ]], 'Arrow')
assertlab([[a <- 'b' / 3]], 'SeqExp')
assertlab([[a <- 'b'& ]], 'AndPred')
assertlab([[a <- ! ]], 'NotPred')
assertlab([[a <- () ]], 'ExpPri')
assertlab([[a <- ( 'b' ]], 'RParPri')
assertlab([[a <- ( 'b" ]], 'SingQuote')
assertlab([[a <- ( "b' ]], 'DoubQuote')
assertlab([[a <- []], 'EmptyClass')
assertlab([[a <- [a-z]], 'RBraClass')
assertlab([[a <- %{ } ]], 'NameThrow')
assertlab([[a <- %{ ops ]], 'RCurThrow')
print("Ok")
--testing undefined nonterminal
local r, msg = pcall(m.match, [[a <- 'a' b]])
print(r, msg)
assert(not r and string.find(msg, "'b' was not defined", 1, true))
print(pretty.printg(m.match[[a <- 'b'*]]))
local r, l, pos = m.match[[a <- 'b' / 'c' d <- 'a'^bola]]
print(pretty.printg(r, l))
local r, l, pos = m.match[[a <- 'bc' 'd' 'c' [a-zA-Z0-9_] ]]
print(pretty.printg(r, l))
local r, l, pos = m.match([[a <- 'b' ('c' / 'd') / 'e']])
print(pretty.printg(r, l))
local r, l, pos = m.match([[new <- 'x' ('c' / 'd') / 'e' %{Nada}]])
print(pretty.printg(r, l))
local r, l, pos = m.match([[
Start <- X ('c' / 'd') / 'e' %{Nada}
X <- &'a' !'b' {.} {} {| [a-z]* |} Z
Z <- 'a'? 'b'* 'c'+]])
print(pretty.printg(r, l))
-- testing coder
local g = [[
S <- "0" B / "1" A / "" -- balanced strings
A <- "0" S / "1" A A -- one more 0
B <- "1" S / "0" B B -- one more 1
]]
local p = coder.makeg(m.match(g))
assert(p:match("00011011") == 9)
local g = [[
S <- ("0" B / "1" A)*
A <- "0" / "1" A A
B <- "1" / "0" B B
]]
local tree, r = m.match(g)
print(pretty.printg(tree, r))
local p = coder.makeg(m.match(g))
assert(p:match("00011011") == 9)
assert(p:match("000110110") == 9)
assert(p:match("011110110") == 3)
print(p:match("000110010"))
assert(p:match("000110010") == 1)