2022-05-06 15:58:32 -04:00
|
|
|
module Tests.Parser
|
|
|
|
|
|
|
|
import Quox.Syntax
|
|
|
|
import Quox.Parser
|
|
|
|
import Quox.Lexer
|
|
|
|
import Tests.Lexer
|
2022-05-07 23:41:55 -04:00
|
|
|
import Quox.Pretty
|
2022-05-06 15:58:32 -04:00
|
|
|
|
2022-05-07 23:41:55 -04:00
|
|
|
import TermImpls
|
2022-05-06 15:58:32 -04:00
|
|
|
import Data.SnocVect
|
|
|
|
import Text.Parser
|
|
|
|
import TAP
|
|
|
|
|
|
|
|
|
|
|
|
export
|
|
|
|
Show tok => ToInfo (ParsingError tok) where
|
|
|
|
toInfo (Error msg Nothing) = [("msg", msg)]
|
|
|
|
toInfo (Error msg (Just loc)) = [("loc", show loc), ("msg", msg)]
|
|
|
|
|
|
|
|
|
|
|
|
numberErrs : List1 Info -> Info
|
|
|
|
numberErrs (head ::: []) = head
|
|
|
|
numberErrs (head ::: tail) = go 0 (head :: tail) where
|
|
|
|
number1 : Nat -> Info -> Info
|
|
|
|
number1 n = map $ \(k, v) => (show n ++ k, v)
|
|
|
|
|
|
|
|
go : Nat -> List Info -> Info
|
|
|
|
go k [] = []
|
|
|
|
go k (x :: xs) = number1 k x ++ go (S k) xs
|
|
|
|
|
|
|
|
export
|
|
|
|
ToInfo Parser.Error where
|
|
|
|
toInfo (Lex err) = toInfo err
|
|
|
|
toInfo (Parse errs) = numberErrs $ map toInfo errs
|
|
|
|
toInfo (Leftover toks) = toInfo [("leftover", toks)]
|
|
|
|
|
|
|
|
|
|
|
|
RealError = Quox.Parser.Error
|
|
|
|
%hide Lexer.RealError
|
|
|
|
%hide Quox.Parser.Error
|
|
|
|
|
|
|
|
data Error a
|
|
|
|
= Parser RealError
|
|
|
|
| Unexpected a a
|
|
|
|
| ShouldFail a
|
|
|
|
|
|
|
|
export
|
|
|
|
Show a => ToInfo (Error a) where
|
|
|
|
toInfo (Parser err) = toInfo err
|
|
|
|
toInfo (Unexpected exp got) = toInfo $
|
|
|
|
[("expected", exp), ("received", got)]
|
|
|
|
toInfo (ShouldFail got) = toInfo [("success", got)]
|
|
|
|
|
|
|
|
|
2022-05-07 23:41:55 -04:00
|
|
|
parameters {c : Bool} (grm : Grammar c a) (note : String) (input : String)
|
|
|
|
parsesNote : (Show a, Eq a) => a -> Test
|
|
|
|
parsesNote exp = test "\"\{input}\"\{note}" $ delay $
|
2022-05-06 15:58:32 -04:00
|
|
|
case lexParseAll grm input of
|
|
|
|
Right got => if got == exp then Right ()
|
|
|
|
else Left $ Unexpected exp got
|
|
|
|
Left err => Left $ Parser err
|
|
|
|
|
2022-05-07 23:41:55 -04:00
|
|
|
rejectsNote : Show a => Test
|
|
|
|
rejectsNote = test "\"\{input}\"\{note} (reject)" $ do
|
2022-05-06 15:58:32 -04:00
|
|
|
case lexParseAll grm input of
|
|
|
|
Left err => Right ()
|
|
|
|
Right val => Left $ ShouldFail val
|
|
|
|
|
2022-05-07 23:41:55 -04:00
|
|
|
parameters {c : Bool} (grm : Grammar c a) (input : String)
|
|
|
|
parses : (Show a, Eq a) => a -> Test
|
|
|
|
parses = parsesNote grm "" input
|
|
|
|
|
|
|
|
rejects : Show a => Test
|
|
|
|
rejects = rejectsNote grm "" input
|
|
|
|
|
2022-05-06 15:58:32 -04:00
|
|
|
tests = "parser" :- [
|
2022-05-07 23:41:55 -04:00
|
|
|
skip $
|
2022-05-06 18:58:03 -04:00
|
|
|
"numbers" :-
|
|
|
|
let parses = parses number
|
|
|
|
in [
|
|
|
|
parses "0" 0,
|
|
|
|
parses "1" 1,
|
2022-05-06 15:58:32 -04:00
|
|
|
parses "1000" 1000
|
|
|
|
],
|
|
|
|
|
2022-05-07 23:41:55 -04:00
|
|
|
skip $
|
2022-05-06 18:58:03 -04:00
|
|
|
"bound vars (x, y, z ⊢)" :-
|
2022-05-07 23:41:55 -04:00
|
|
|
let grm = bound "test" [< "x", "y", "z"]
|
2022-05-06 18:58:03 -04:00
|
|
|
parses = parses grm; rejects = rejects grm
|
|
|
|
in [
|
|
|
|
parses "x" (V 2),
|
|
|
|
parses "y" (V 1),
|
|
|
|
parses "z" (V 0),
|
|
|
|
rejects "M.x",
|
|
|
|
rejects "x.a",
|
|
|
|
rejects "a"
|
|
|
|
],
|
|
|
|
|
2022-05-07 23:41:55 -04:00
|
|
|
skip $
|
2022-05-06 18:58:03 -04:00
|
|
|
"bound or free vars (x, y, z ⊢)" :-
|
2022-05-07 23:41:55 -04:00
|
|
|
let parses = parses $ nameWith {bound = [< "x", "y", "z"], avoid = [<]}
|
2022-05-06 18:58:03 -04:00
|
|
|
in [
|
|
|
|
parses "x" (Left (V 2)),
|
|
|
|
parses "y" (Left (V 1)),
|
|
|
|
parses "z" (Left (V 0)),
|
|
|
|
parses "a" (Right (MakeName [<] (UN "a"))),
|
|
|
|
parses "a.b.c" (Right (MakeName [< "a", "b"] (UN "c"))),
|
|
|
|
parses "a . b . c" (Right (MakeName [< "a", "b"] (UN "c"))),
|
|
|
|
parses "M.x" (Right (MakeName [< "M"] (UN "x"))),
|
|
|
|
parses "x.a" (Right (MakeName [< "x"] (UN "a")))
|
|
|
|
],
|
2022-05-06 15:58:32 -04:00
|
|
|
|
2022-05-07 23:41:55 -04:00
|
|
|
skip $
|
|
|
|
"dimension (x, y, z | · ⊢)" :-
|
2022-05-06 18:58:03 -04:00
|
|
|
let grm = dimension [< "x", "y", "z"]
|
|
|
|
parses = parses grm; rejects = rejects grm
|
|
|
|
in [
|
|
|
|
parses "0" (K Zero),
|
|
|
|
parses "1" (K One),
|
|
|
|
rejects "2",
|
|
|
|
parses "x" (B (V 2)),
|
|
|
|
rejects "a"
|
2022-05-07 23:41:55 -04:00
|
|
|
],
|
|
|
|
|
|
|
|
"terms & elims (i, j | x, y, z ⊢)" :-
|
|
|
|
let dvars = [< "i", "j"]; tvars = [< "x", "y", "z"]
|
|
|
|
tgrm = term {dvars, tvars}; egrm = elim {dvars, tvars}
|
|
|
|
tparses = parsesNote tgrm " (term)"
|
|
|
|
eparses = parsesNote egrm " (elim)"
|
|
|
|
trejects = rejectsNote tgrm " (term)"
|
|
|
|
erejects = rejectsNote egrm " (elim)"
|
|
|
|
in [
|
|
|
|
eparses "a" (F "a"),
|
|
|
|
eparses "x" (BV 2),
|
|
|
|
trejects "a",
|
|
|
|
tparses "[a]" (FT "a"),
|
|
|
|
tparses "[x]" (BVT 2)
|
2022-05-06 15:58:32 -04:00
|
|
|
]
|
|
|
|
]
|