Compare commits

..

3 Commits

Author SHA1 Message Date
Patrick Aldis
d28638e286 Current progress 2026-03-03 11:05:11 +00:00
Patrick Aldis
902fb4e0b5 Refactor parser to decorate with void 2026-03-03 10:29:22 +00:00
Patrick Aldis
e7340f9a4d add pretty-simple 2026-03-03 10:29:01 +00:00
2 changed files with 73 additions and 53 deletions

View File

@ -1,18 +1,18 @@
{-# LANGUAGE MultilineStrings #-}
module Datalog.Parser
( parseTerm
, parseAtom
, parseRule
, parseQuery
, parseProgram
)
where
module Datalog.Parser (
parseTerm,
parseAtom,
parseRule,
parseQuery,
parseProgram,
)
where
import Datalog.Syntax
import Data.Text (Text)
import Data.Text qualified as T
import Data.Void (Void)
import Datalog.Syntax
import Text.Megaparsec
import Text.Megaparsec.Char
import Text.Megaparsec.Char.Lexer qualified as L
@ -27,7 +27,8 @@ symbol :: (MonadParsec e Text m) => Text -> m Text
symbol = L.symbol whitespace
whitespace :: (MonadParsec e Text m) => m ()
whitespace = L.space
whitespace =
L.space
space1
(L.skipLineComment "--")
(L.skipBlockComment "{-" "-}")
@ -39,54 +40,63 @@ comma, period :: (MonadParsec e Text m) => m ()
comma = () <$ symbol ","
period = () <$ symbol "."
parseCon :: (MonadParsec e Text m) => m (Term' SrcLoc)
parseCon = Con dummy . ConId . T.pack <$> lexeme (liftA2 (:) (numberChar <|> lowerChar) (many alphaNumChar))
parseCon :: (MonadParsec e Text m) => m Term
parseCon = Con () . ConId . T.pack <$> lexeme (liftA2 (:) (numberChar <|> lowerChar) (many alphaNumChar))
parseVar :: (MonadParsec e Text m) => m (Term' SrcLoc)
parseVar = Var dummy . VarId . T.pack <$> lexeme (liftA2 (:) upperChar (many alphaNumChar))
parseVar :: (MonadParsec e Text m) => m Term
parseVar = Var () . VarId . T.pack <$> lexeme (liftA2 (:) upperChar (many alphaNumChar))
parseTerm :: Parser (Term' SrcLoc)
parseTerm :: Parser Term
parseTerm = parseVar <|> parseCon
parseAtom :: Parser (Atom' SrcLoc)
parseAtom = do
p <- getSourcePos
parseAtom = annotateSrcLoc $ do
rel <- RelId . T.pack <$> lexeme (liftA2 (:) lowerChar (many alphaNumChar))
args <- parens (parseTerm `sepBy` comma)
return (Atom SrcLoc{start = p} rel args)
return (Atom () rel args)
parseQuery :: Parser [Atom' SrcLoc]
parseQuery = parseAtom `sepBy` comma
parseFact :: Parser (Rule' SrcLoc)
parseFact = do
p <- getSourcePos
parseFact = annotateSrcLoc $ do
headAtom <- parseAtom
period
return (Rule SrcLoc{start = p} headAtom [])
return (Rule NoLoc headAtom [])
parseRule :: Parser (Rule' SrcLoc)
parseRule = try parseFact <|> do
p <- getSourcePos
parseRule =
annotateSrcLoc $
try parseFact <|> do
headAtom <- parseAtom <* symbol ":-"
bodyAtoms <- parseQuery
period
return (Rule SrcLoc{start = p} headAtom bodyAtoms)
return (Rule NoLoc headAtom bodyAtoms)
parseProgram :: Parser (Program' SrcLoc)
parseProgram = Program dummy <$> many parseRule
parseProgram = annotateSrcLoc $ Program NoLoc <$> many parseRule
data SrcLoc = SrcLoc
annotateSrcLoc :: (Functor f) => Parser (f a) -> Parser (f SrcLoc)
annotateSrcLoc p = do
s <- getSourcePos
res <- p
f <- getSourcePos
pure (SrcLoc s f <$ res)
data SrcLoc
= SrcLoc
{ start :: SourcePos
-- , end :: (Word, Word)
} deriving Show
dummy = SrcLoc{start = initialPos "dummy-file"}
, end :: SourcePos
}
| NoLoc
deriving (Show)
test = do
let r = runParser parseProgram "???" prog
pPrint @IO r
prog = """
prog =
"""
odd(X,Y) :- r(X,Y).
odd(X,Y) :- even(X,Z), r(Z,Y).
even(X,Y) :- odd(X,Z), r(Z,Y).

View File

@ -48,6 +48,16 @@ type Program = Program' ()
data Program' a = Program a [Rule' a]
deriving (Eq, Ord, Show)
class Decorable t where
decorateNode :: a -> t a -> t a
instance (Decorable Program') where decorateNode x (Program _ rs) = Program x rs
instance (Decorable Rule') where decorateNode x (Rule _ a as) = Rule x a as
instance (Decorable Atom') where decorateNode x (Atom _ relId ts) = Atom x relId ts
instance (Decorable Term') where
decorateNode x (Var _ varId) = Var x varId
decorateNode x (Con _ conId) = Con x conId
class HasConstants a where
constants :: a -> Set ConId