more parser/FromParser stuff
- top level semicolons optional - type optional [the def will need to be an elim] - `load` statement - namespaces
This commit is contained in:
parent
cd63eb2c67
commit
c81aabcc14
10 changed files with 716 additions and 538 deletions
|
@ -3,30 +3,29 @@ module Tests.FromPTerm
|
|||
import Quox.Parser.Syntax
|
||||
import Quox.Parser
|
||||
import TermImpls
|
||||
import Tests.Parser as TParser
|
||||
import TAP
|
||||
|
||||
import Derive.Prelude
|
||||
%language ElabReflection
|
||||
|
||||
%hide TParser.Failure
|
||||
%hide TParser.ExpectedFail
|
||||
|
||||
public export
|
||||
data Failure =
|
||||
LexError Lexer.Error
|
||||
| ParseError (List1 (ParsingError Token))
|
||||
| FromPTermError FromPTermError
|
||||
ParseError (Parser.Error)
|
||||
| FromParserError FromParserError
|
||||
| WrongResult String
|
||||
| ExpectedFail String
|
||||
|
||||
%runElab derive "Syntax.FromPTermError" [Show]
|
||||
%runElab derive "FromParser.FromParserError" [Show]
|
||||
|
||||
export
|
||||
ToInfo Failure where
|
||||
toInfo (LexError err) =
|
||||
[("type", "LexError"), ("info", show err)]
|
||||
toInfo (ParseError errs) =
|
||||
("type", "ParseError") ::
|
||||
map (bimap show show) ([1 .. length errs] `zip` toList errs)
|
||||
toInfo (FromPTermError err) =
|
||||
[("type", "FromPTermError"),
|
||||
toInfo (ParseError err) = toInfo err
|
||||
toInfo (FromParserError err) =
|
||||
[("type", "FromParserError"),
|
||||
("got", show err)]
|
||||
toInfo (WrongResult got) =
|
||||
[("type", "WrongResult"), ("got", got)]
|
||||
|
@ -35,14 +34,13 @@ ToInfo Failure where
|
|||
|
||||
|
||||
parameters {c : Bool} {auto _ : Show b}
|
||||
(grm : Grammar c a) (fromP : a -> Either FromPTermError b)
|
||||
(grm : Grammar c a) (fromP : a -> Either FromParserError b)
|
||||
(inp : String)
|
||||
parameters {default (ltrim inp) label : String}
|
||||
parsesWith : (b -> Bool) -> Test
|
||||
parsesWith p = test label $ do
|
||||
toks <- mapFst LexError $ lex inp
|
||||
(pres, _) <- mapFst ParseError $ parse (grm <* eof) toks
|
||||
res <- mapFst FromPTermError $ fromP pres
|
||||
pres <- mapFst ParseError $ lexParseWith grm inp
|
||||
res <- mapFst FromParserError $ fromP pres
|
||||
unless (p res) $ Left $ WrongResult $ show res
|
||||
|
||||
parses : Test
|
||||
|
@ -54,8 +52,7 @@ parameters {c : Bool} {auto _ : Show b}
|
|||
parameters {default "\{ltrim inp} # fails" label : String}
|
||||
parseFails : Test
|
||||
parseFails = test label $ do
|
||||
toks <- mapFst LexError $ lex inp
|
||||
(pres, _) <- mapFst ParseError $ parse (grm <* eof) toks
|
||||
pres <- mapFst ParseError $ lexParseWith grm inp
|
||||
either (const $ Right ()) (Left . ExpectedFail . show) $ fromP pres
|
||||
|
||||
|
||||
|
|
|
@ -7,18 +7,22 @@ import TAP
|
|||
|
||||
public export
|
||||
data Failure =
|
||||
LexError Lexer.Error
|
||||
| ParseError (List1 (ParsingError Token))
|
||||
ParseError Parser.Error
|
||||
| WrongResult String
|
||||
| ExpectedFail String
|
||||
|
||||
export
|
||||
ToInfo Failure where
|
||||
ToInfo Parser.Error where
|
||||
toInfo (LexError err) =
|
||||
[("type", "LexError"), ("info", show err)]
|
||||
toInfo (ParseError errs) =
|
||||
("type", "ParseError") ::
|
||||
map (bimap show show) ([1 .. length errs] `zip` toList errs)
|
||||
|
||||
export
|
||||
ToInfo Failure where
|
||||
toInfo (ParseError err) =
|
||||
toInfo err
|
||||
toInfo (WrongResult got) =
|
||||
[("type", "WrongResult"), ("got", got)]
|
||||
toInfo (ExpectedFail got) =
|
||||
|
@ -30,8 +34,7 @@ parameters {c : Bool} {auto _ : Show a} (grm : Grammar c a)
|
|||
parameters {default (ltrim inp) label : String}
|
||||
parsesWith : (a -> Bool) -> Test
|
||||
parsesWith p = test label $ do
|
||||
toks <- mapFst LexError $ lex inp
|
||||
(res, _) <- mapFst ParseError $ parse (grm <* eof) toks
|
||||
res <- mapFst ParseError $ lexParseWith grm inp
|
||||
unless (p res) $ Left $ WrongResult $ show res
|
||||
|
||||
parses : Test
|
||||
|
@ -43,9 +46,8 @@ parameters {c : Bool} {auto _ : Show a} (grm : Grammar c a)
|
|||
parameters {default "\{ltrim inp} # fails" label : String}
|
||||
parseFails : Test
|
||||
parseFails = test label $ do
|
||||
toks <- mapFst LexError $ lex inp
|
||||
either (const $ Right ()) (Left . ExpectedFail . show . fst) $
|
||||
parse (grm <* eof) toks
|
||||
either (const $ Right ()) (Left . ExpectedFail . show) $
|
||||
lexParseWith grm inp
|
||||
|
||||
|
||||
export
|
||||
|
@ -59,9 +61,9 @@ tests = "parser" :- [
|
|||
|
||||
"names" :- [
|
||||
parsesAs name "x"
|
||||
(MakeName [<] $ UN "x"),
|
||||
(MakePName [<] "x"),
|
||||
parsesAs name "Data.String.length"
|
||||
(MakeName [< "Data", "String"] $ UN "length"),
|
||||
(MakePName [< "Data", "String"] "length"),
|
||||
parseFails name "_"
|
||||
],
|
||||
|
||||
|
@ -119,7 +121,7 @@ tests = "parser" :- [
|
|||
|
||||
"applications" :- [
|
||||
parsesAs term "f" (V "f"),
|
||||
parsesAs term "f.x.y" (V $ MakeName [< "f", "x"] $ UN "y"),
|
||||
parsesAs term "f.x.y" (V $ MakePName [< "f", "x"] "y"),
|
||||
parsesAs term "f x" (V "f" :@ V "x"),
|
||||
parsesAs term "f x y" (V "f" :@ V "x" :@ V "y"),
|
||||
parsesAs term "(f x) y" (V "f" :@ V "x" :@ V "y"),
|
||||
|
@ -240,22 +242,36 @@ tests = "parser" :- [
|
|||
],
|
||||
|
||||
"definitions" :- [
|
||||
parsesAs definition "defω x : (_: {a}) × {b} ≔ ('a, 'b);" $
|
||||
MkPDef Any "x" (Sig Nothing (Enum ["a"]) (Enum ["b"]))
|
||||
parsesAs definition "defω x : (_: {a}) × {b} ≔ ('a, 'b)" $
|
||||
MkPDef Any "x" (Just $ Sig Nothing (Enum ["a"]) (Enum ["b"]))
|
||||
(Pair (Tag "a") (Tag "b")),
|
||||
parsesAs definition "def# x : (_: {a}) ** {b} ≔ ('a, 'b);" $
|
||||
MkPDef Any "x" (Sig Nothing (Enum ["a"]) (Enum ["b"]))
|
||||
parsesAs definition "defω x : (_: {a}) × {b} ≔ ('a, 'b)" $
|
||||
MkPDef Any "x" (Just $ Sig Nothing (Enum ["a"]) (Enum ["b"]))
|
||||
(Pair (Tag "a") (Tag "b")),
|
||||
parsesAs definition "def ω·x : (_: {a}) × {b} ≔ ('a, 'b);" $
|
||||
MkPDef Any "x" (Sig Nothing (Enum ["a"]) (Enum ["b"]))
|
||||
parsesAs definition "def# x : (_: {a}) ** {b} ≔ ('a, 'b)" $
|
||||
MkPDef Any "x" (Just $ Sig Nothing (Enum ["a"]) (Enum ["b"]))
|
||||
(Pair (Tag "a") (Tag "b")),
|
||||
parsesAs definition "def x : (_: {a}) × {b} ≔ ('a, 'b);" $
|
||||
MkPDef Any "x" (Sig Nothing (Enum ["a"]) (Enum ["b"]))
|
||||
parsesAs definition "def ω·x : (_: {a}) × {b} ≔ ('a, 'b)" $
|
||||
MkPDef Any "x" (Just $ Sig Nothing (Enum ["a"]) (Enum ["b"]))
|
||||
(Pair (Tag "a") (Tag "b")),
|
||||
parsesAs definition "def0 A : ★₀ ≔ {a, b, c};" $
|
||||
MkPDef Zero "A" (TYPE 0) (Enum ["a", "b", "c"]),
|
||||
parsesAs definition "def x : (_: {a}) × {b} ≔ ('a, 'b)" $
|
||||
MkPDef Any "x" (Just $ Sig Nothing (Enum ["a"]) (Enum ["b"]))
|
||||
(Pair (Tag "a") (Tag "b")),
|
||||
parsesAs definition "def0 A : ★₀ ≔ {a, b, c}" $
|
||||
MkPDef Zero "A" (Just $ TYPE 0) (Enum ["a", "b", "c"])
|
||||
],
|
||||
|
||||
"top level" :- [
|
||||
parsesAs input "def0 A : ★₀ ≔ {}; def0 B : ★₁ ≔ A;" $
|
||||
[MkPDef Zero "A" (TYPE 0) (Enum []),
|
||||
MkPDef Zero "B" (TYPE 1) (V "A")]
|
||||
[PD $ PDef $ MkPDef Zero "A" (Just $ TYPE 0) (Enum []),
|
||||
PD $ PDef $ MkPDef Zero "B" (Just $ TYPE 1) (V "A")],
|
||||
parsesAs input "def0 A : ★₀ ≔ {} def0 B : ★₁ ≔ A" $
|
||||
[PD $ PDef $ MkPDef Zero "A" (Just $ TYPE 0) (Enum []),
|
||||
PD $ PDef $ MkPDef Zero "B" (Just $ TYPE 1) (V "A")],
|
||||
parsesAs input "def0 A : ★₀ ≔ {};;; def0 B : ★₁ ≔ A" $
|
||||
[PD $ PDef $ MkPDef Zero "A" (Just $ TYPE 0) (Enum []),
|
||||
PD $ PDef $ MkPDef Zero "B" (Just $ TYPE 1) (V "A")],
|
||||
parsesAs input "" [] {label = "[empty input]"},
|
||||
parsesAs input ";;;;;;;;;;;;;;;;;;;;;;;;;;" []
|
||||
]
|
||||
]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue