Commit d5ebe0af authored by Paul Ogris's avatar Paul Ogris
Browse files

Add support for #const

parent 971d25b2
......@@ -53,11 +53,13 @@ import qualified Text.Megaparsec.Char.Lexer as L
data Statement
= StmtRule Rule
| StmtInstance Signature
| StmtConst Text
deriving (Eq, Show, Ord, Read)
instance Pretty Statement where
pretty (StmtRule r) = PP.pretty r
pretty (StmtInstance s) = "#instance" <+> PP.pretty s <> PP.dot
pretty (StmtConst x) = "#const" <+> PP.textStrict x <> PP.dot
data Signature = Signature Text Int
deriving (Eq, Show, Ord, Read)
......@@ -217,6 +219,9 @@ slash = symbol "/"
instanceDirective :: Parser Text
instanceDirective = symbol "#instance"
constDirective :: Parser Text
constDirective = symbol "#const"
signedAtom :: Parser SignedAtom
signedAtom = try (negation *> (Negative <$> atom)) <|> (Positive <$> atom)
......@@ -231,12 +236,18 @@ relation = lexeme $ do
pure $ a <> b
stmt :: Parser Statement
stmt = (StmtInstance <$> inst) <|> (StmtRule <$> rule)
stmt = (StmtInstance <$> inst) <|> (StmtConst <$> cnst) <|> (StmtRule <$> rule)
where inst = do
_ <- instanceDirective
s <- sig
_ <- dot
pure s
cnst = do
_ <- constDirective
s <- takeWhileP Nothing (/= '.')
_ <- dot
pure s
sig :: Parser Signature
sig = Signature <$> takeWhileP Nothing (/= '/') <*> (slash *> L.decimal)
......
#const units = 10.
unit(1..units).
......@@ -87,6 +87,9 @@ pprint = parallel $ do
it "pretty prints instance statements"
$ prettyOneLine (StmtInstance (Signature "quux" 2))
`shouldBe` "#instance quux/2."
it "pretty prints #const statements"
$ prettyOneLine (StmtConst "foo = 3")
`shouldBe` "#const foo = 3."
parser :: Spec
parser = parallel $ do
......@@ -141,3 +144,6 @@ parser = parallel $ do
it "parses instance directives"
$ parse stmt "" "#instance someSig/3."
`shouldParse` StmtInstance (Signature "someSig" 3)
it "parses const directives"
$ parse stmt "" "#const foo = 3."
`shouldParse` StmtConst "foo = 3"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment