parser stuff

This commit is contained in:
rhiannon morris 2022-05-08 05:41:55 +02:00
parent f5a98e6584
commit 1e7a6cf01f
2 changed files with 59 additions and 15 deletions

View file

@ -4,6 +4,7 @@ import Quox.Syntax
import Quox.Token import Quox.Token
import Quox.Lexer import Quox.Lexer
import Data.Maybe
import Data.SnocVect import Data.SnocVect
import Data.SnocList import Data.SnocList
import Text.Parser import Text.Parser
@ -86,9 +87,9 @@ find _ _ = Nothing
export export
bound : Vars k -> Grammar True (Var k) bound : (what : String) -> Vars k -> Grammar True (Var k)
bound vs = bound what vs = terminal "bound \{what} variable" $
terminal "bound variable" $ \case Name x => find1 vs x; _ => Nothing \case Name x => find1 vs x; _ => Nothing
export export
sname : Grammar True String sname : Grammar True String
@ -101,10 +102,13 @@ qname = do
pure $ MakeName {mods = cast $ init parts, base = UN $ last parts} pure $ MakeName {mods = cast $ init parts, base = UN $ last parts}
export export
nameWith : Vars k -> Grammar True (Either (Var k) Name) nameWith : (bound : Vars k) -> (avoid : Vars n) ->
nameWith vs = do Grammar True (Either (Var k) Name)
nameWith bound avoid = do
y <- qname y <- qname
pure $ maybe (Right y) Left $ find vs y when (isJust $ find avoid y) $
fail "wrong type of bound variable: \{show y}"
pure $ maybe (Right y) Left $ find bound y
export export
@ -112,4 +116,16 @@ dimension : Vars d -> Grammar True (Dim d)
dimension vs = dimension vs =
K Zero <$ zero K Zero <$ zero
<|> K One <$ one <|> K One <$ one
<|> B <$> bound vs <|> B <$> bound "dimension" vs
mutual
export
term : (dvars : Vars d) -> (tvars : Vars n) -> Grammar True (Term d n)
term dvars tvars =
E <$> squares (elim dvars tvars)
export
elim : (dvars : Vars d) -> (tvars : Vars n) -> Grammar True (Elim d n)
elim dvars tvars =
either B F <$> nameWith {bound = tvars, avoid = dvars}

View file

@ -4,7 +4,9 @@ import Quox.Syntax
import Quox.Parser import Quox.Parser
import Quox.Lexer import Quox.Lexer
import Tests.Lexer import Tests.Lexer
import Quox.Pretty
import TermImpls
import Data.SnocVect import Data.SnocVect
import Text.Parser import Text.Parser
import TAP import TAP
@ -50,21 +52,29 @@ Show a => ToInfo (Error a) where
toInfo (ShouldFail got) = toInfo [("success", got)] toInfo (ShouldFail got) = toInfo [("success", got)]
parameters {c : Bool} (grm : Grammar c a) (input : String) parameters {c : Bool} (grm : Grammar c a) (note : String) (input : String)
parses : (Show a, Eq a) => a -> Test parsesNote : (Show a, Eq a) => a -> Test
parses exp = test "\"\{input}\"" $ delay $ parsesNote exp = test "\"\{input}\"\{note}" $ delay $
case lexParseAll grm input of case lexParseAll grm input of
Right got => if got == exp then Right () Right got => if got == exp then Right ()
else Left $ Unexpected exp got else Left $ Unexpected exp got
Left err => Left $ Parser err Left err => Left $ Parser err
rejects : Show a => Test rejectsNote : Show a => Test
rejects = test "\"\{input}\" (reject)" $ do rejectsNote = test "\"\{input}\"\{note} (reject)" $ do
case lexParseAll grm input of case lexParseAll grm input of
Left err => Right () Left err => Right ()
Right val => Left $ ShouldFail val Right val => Left $ ShouldFail val
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
tests = "parser" :- [ tests = "parser" :- [
skip $
"numbers" :- "numbers" :-
let parses = parses number let parses = parses number
in [ in [
@ -73,8 +83,9 @@ tests = "parser" :- [
parses "1000" 1000 parses "1000" 1000
], ],
skip $
"bound vars (x, y, z ⊢)" :- "bound vars (x, y, z ⊢)" :-
let grm = bound [< "x", "y", "z"] let grm = bound "test" [< "x", "y", "z"]
parses = parses grm; rejects = rejects grm parses = parses grm; rejects = rejects grm
in [ in [
parses "x" (V 2), parses "x" (V 2),
@ -85,8 +96,9 @@ tests = "parser" :- [
rejects "a" rejects "a"
], ],
skip $
"bound or free vars (x, y, z ⊢)" :- "bound or free vars (x, y, z ⊢)" :-
let parses = parses $ nameWith [< "x", "y", "z"] let parses = parses $ nameWith {bound = [< "x", "y", "z"], avoid = [<]}
in [ in [
parses "x" (Left (V 2)), parses "x" (Left (V 2)),
parses "y" (Left (V 1)), parses "y" (Left (V 1)),
@ -98,7 +110,8 @@ tests = "parser" :- [
parses "x.a" (Right (MakeName [< "x"] (UN "a"))) parses "x.a" (Right (MakeName [< "x"] (UN "a")))
], ],
"dimension (x, y, z ⊢)" :- skip $
"dimension (x, y, z | · ⊢)" :-
let grm = dimension [< "x", "y", "z"] let grm = dimension [< "x", "y", "z"]
parses = parses grm; rejects = rejects grm parses = parses grm; rejects = rejects grm
in [ in [
@ -107,5 +120,20 @@ tests = "parser" :- [
rejects "2", rejects "2",
parses "x" (B (V 2)), parses "x" (B (V 2)),
rejects "a" rejects "a"
],
"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)
] ]
] ]