-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParserTests.hs
167 lines (149 loc) · 7.61 KB
/
ParserTests.hs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module ParserTests where
import Control.Applicative
import Data.Char qualified as Char
import LoxArbitrary
import LoxParser
import LoxSyntax
import ParserLib (Parser)
import ParserLib qualified as P
import Test.HUnit (Assertion, Counts, Test (..), assert, runTestTT, (~:), (~?=))
import Test.QuickCheck qualified as QC
test_wsP :: Test
test_wsP =
TestList
[ P.parse (wsP P.alpha) "a" ~?= Right 'a',
P.parse (many (wsP P.alpha)) "a b \n \t c" ~?= Right "abc"
]
test_stringP :: Test
test_stringP =
TestList
[ P.parse (stringP "a") "a" ~?= Right (),
P.parse (stringP "a") "b" ~?= Left "No parses",
P.parse (many (stringP "a")) "a a" ~?= Right [(), ()]
]
test_constP :: Test
test_constP =
TestList
[ P.parse (constP "&" 'a') "& " ~?= Right 'a',
P.parse (many (constP "&" 'a')) "& &" ~?= Right "aa"
]
test_stringValP :: Test
test_stringValP =
TestList
[ P.parse stringValP "\"a\"" ~?= Right (StringVal "a"),
P.parse stringValP "\"a\\\"\"" ~?= Right (StringVal "a\\"),
P.parse (many stringValP) "\"a\" \"b\"" ~?= Right [StringVal "a", StringVal "b"],
P.parse (many stringValP) "\" a\" \"b\"" ~?= Right [StringVal " a", StringVal "b"]
]
test_funcValP :: Test
test_funcValP =
TestList
[ P.parse funcValP "\\(x1, x2) { y=4 }" ~?= Right (FunctionValIncomplete ["x1", "x2"] (Block [Assign (LName "y") (Val (IntVal 4))])),
P.parse (many funcValP) "\\(x1, x2) { y=4 } \\(x1, x2) { y=4 }" ~?= Right [FunctionValIncomplete ["x1", "x2"] (Block [Assign (LName "y") (Val (IntVal 4))]), FunctionValIncomplete ["x1", "x2"] (Block [Assign (LName "y") (Val (IntVal 4))])]
]
test_arrayValP :: Test
test_arrayValP =
TestList
[ P.parse arrayValP "[1, 2, 3]" ~?= Right (ArrayVal [IntVal 1, IntVal 2, IntVal 3]),
P.parse arrayValP "[\"a\", \"b\", \"c\"]" ~?= Right (ArrayVal [StringVal "a", StringVal "b", StringVal "c"]),
P.parse arrayValP "[]" ~?= Right (ArrayVal []),
P.parse (many arrayValP) "[1, 2, 3] [1, 2, 3]" ~?= Right [ArrayVal [IntVal 1, IntVal 2, IntVal 3], ArrayVal [IntVal 1, IntVal 2, IntVal 3]]
]
tParseFiles :: Test
tParseFiles =
"parse files"
~: TestList
[ "abs" ~: p "test/programs/1_abs.lox" loxAbs,
"exp" ~: p "test/programs/2_exp.lox" loxExp,
"scope" ~: p "test/programs/3_scope.lox" loxScope,
"func" ~: p "test/programs/4_func.lox" loxAdvFunc,
"anon func" ~: p "test/programs/5_anon_func.lox" loxAnonFunc,
"closure" ~: p "test/programs/6_closure.lox" loxClosure,
"array" ~: p "test/programs/7_array.lox" loxArray,
"first class func" ~: p "test/programs/8_first_class_func.lox" loxFstClassFunc,
"more closure" ~: p "test/programs/9_more_closure.lox" loxMoreClosure,
"recursion" ~: p "test/programs/10_recursion.lox" loxRecursion
]
where
p fn ast = do
result <- parseLoxFile fn
case result of
(Left _) -> assert False
(Right ast') -> assert (ast == ast')
test_comb :: Test
test_comb =
"parsing combinators"
~: TestList
[ P.parse (wsP P.alpha) "a" ~?= Right 'a',
P.parse (many (wsP P.alpha)) "a b \n \t c" ~?= Right "abc",
P.parse (stringP "a") "a" ~?= Right (),
P.parse (stringP "a") "b" ~?= Left "No parses",
P.parse (many (stringP "a")) "a a" ~?= Right [(), ()],
P.parse (constP "&" 'a') "& " ~?= Right 'a',
P.parse (many (constP "&" 'a')) "& &" ~?= Right "aa",
P.parse (many (brackets (constP "1" 1))) "[1] [ 1] [1 ]" ~?= Right [1, 1, 1]
]
test_value :: Test
test_value =
"parsing values"
~: TestList
[ P.parse (many intValP) "1 2\n 3" ~?= Right [IntVal 1, IntVal 2, IntVal 3],
P.parse (many boolValP) "true false\n true" ~?= Right [BoolVal True, BoolVal False, BoolVal True],
P.parse (many nilValP) "nil nil\n nil" ~?= Right [NilVal, NilVal, NilVal],
P.parse stringValP "\"a\"" ~?= Right (StringVal "a"),
P.parse stringValP "\"a\\\"\"" ~?= Right (StringVal "a\\"),
P.parse (many stringValP) "\"a\" \"b\"" ~?= Right [StringVal "a", StringVal "b"],
P.parse (many stringValP) "\" a\" \"b\"" ~?= Right [StringVal " a", StringVal "b"]
]
test_exp :: Test
test_exp =
"parsing expressions"
~: TestList
[ P.parse (many varP) "x y z" ~?= Right ["x", "y", "z"],
P.parse varP "(x.y[1]).z" ~?= Left "No parses",
P.parse (many varP) "x sfds _ nil" ~?= Right ["x", "sfds", "_"],
P.parse (many uopP) "- - !" ~?= Right [Neg, Neg, Not],
P.parse (many uopP) "! - test hi" ~?= Right [Not, Neg],
P.parse (many bopP) "+ >= test" ~?= Right [Plus, Ge],
P.parse (many bopP) "+ - * / % or and == != > >= < <=" ~?= Right [Plus, Minus, Times, Divide, Modulo, Or, And, Eq, Ne, Gt, Ge, Lt, Le],
P.parse (funcCallExpP expP) "f(a1)" ~?= Right (FunctionCall (Var "f") [Var "a1"]),
P.parse (funcCallExpP expP) "f(a1, a2)" ~?= Right (FunctionCall (Var "f") [Var "a1", Var "a2"]),
P.parse arrayConsP "[1, 2, 3]" ~?= Right (ArrayCons [Val (IntVal 1), Val (IntVal 2), Val (IntVal 3)]),
P.parse (arrayIndexExpP expP) "x[1]" ~?= Right (ArrayIndex (Var "x") (Val (IntVal 1))),
P.parse (many arrayConsP) "[1, 2, 3] [4, 5, 6]" ~?= Right [ArrayCons [Val (IntVal 1), Val (IntVal 2), Val (IntVal 3)], ArrayCons [Val (IntVal 4), Val (IntVal 5), Val (IntVal 6)]]
]
test_stat :: Test
test_stat =
"parsing statements"
~: TestList
[ P.parse statementP ";" ~?= Right Empty,
P.parse statementP "y=4" ~?= Right (Assign (LName "y") (Val (IntVal 4))),
P.parse statementP "var x=3" ~?= Right (VarDecl "x" (Val (IntVal 3))),
P.parse statementP "x[1] = 3" ~?= Right (Assign (LArrayIndex (LName "x") (Val (IntVal 1))) (Val (IntVal 3))),
P.parse statementP "x[1][2] = 2" ~?= Right (Assign (LArrayIndex (LArrayIndex (LName "x") (Val (IntVal 1))) (Val (IntVal 2))) (Val (IntVal 2))),
P.parse statementP "var x = [1, 2, 3]" ~?= Right (VarDecl "x" (ArrayCons [Val (IntVal 1), Val (IntVal 2), Val (IntVal 3)])),
P.parse statementP "if (x) { y=4 } else { y=5 }" ~?= Right (If (Var "x") (Block [Assign (LName "y") (Val (IntVal 4))]) (Block [Assign (LName "y") (Val (IntVal 5))])),
P.parse statementP "for (var x=3; x<6; x=x+1) { y=4 }" ~?= Right (For (VarDecl "x" (Val (IntVal 3))) (Op2 (Var "x") Lt (Val (IntVal 6))) (Assign (LName "x") (Op2 (Var "x") Plus (Val (IntVal 1)))) (Block [Assign (LName "y") (Val (IntVal 4))])),
P.parse statementP "while (x) { y=4 }" ~?= Right (While (Var "x") (Block [Assign (LName "y") (Val (IntVal 4))])),
P.parse statementP "f(a1)" ~?= Right (FunctionCallStatement (Var "f") [Var "a1"]),
P.parse statementP "fun f(x1, x2) { y=4 }" ~?= Right (FunctionDef "f" ["x1", "x2"] (Block [Assign (LName "y") (Val (IntVal 4))])),
P.parse statementP "return 4" ~?= Right (Return (Val (IntVal 4)))
]
test_all :: IO Counts
test_all = runTestTT $ TestList [test_wsP, test_stringP, test_constP, test_stringValP, test_funcValP, test_arrayValP, test_comb, test_value, test_exp, test_stat, tParseFiles]
-- >>> test_all
-- Counts {cases = 66, tried = 66, errors = 0, failures = 0}
prop_roundtrip_val :: Value -> Bool
prop_roundtrip_val v = P.parse valueP (pretty v) == Right v
prop_roundtrip_exp :: Expression -> Bool
prop_roundtrip_exp e = P.parse expP (pretty e) == Right e
prop_roundtrip_stat :: Statement -> Bool
prop_roundtrip_stat s = P.parse statementP (pretty s) == Right s
roundtrip_qc :: IO ()
roundtrip_qc = do
putStrLn "roundtrip_val"
QC.quickCheck prop_roundtrip_val
putStrLn "roundtrip_exp"
QC.quickCheck prop_roundtrip_exp
putStrLn "roundtrip_stat"
QC.quickCheck prop_roundtrip_stat