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
3 changed files with 84 additions and 33 deletions

View File

@ -1,19 +1,22 @@
module Datalog.Parser {-# LANGUAGE MultilineStrings #-}
( parseTerm
, parseAtom module Datalog.Parser (
, parseRule parseTerm,
, parseQuery parseAtom,
, parseProgram parseRule,
) parseQuery,
where parseProgram,
)
where
import Datalog.Syntax
import Data.Text (Text) import Data.Text (Text)
import Data.Text qualified as T import Data.Text qualified as T
import Data.Void (Void) import Data.Void (Void)
import Datalog.Syntax
import Text.Megaparsec import Text.Megaparsec
import Text.Megaparsec.Char import Text.Megaparsec.Char
import Text.Megaparsec.Char.Lexer qualified as L import Text.Megaparsec.Char.Lexer qualified as L
import Text.Pretty.Simple
type Parser = Parsec Void Text type Parser = Parsec Void Text
@ -24,7 +27,8 @@ symbol :: (MonadParsec e Text m) => Text -> m Text
symbol = L.symbol whitespace symbol = L.symbol whitespace
whitespace :: (MonadParsec e Text m) => m () whitespace :: (MonadParsec e Text m) => m ()
whitespace = L.space whitespace =
L.space
space1 space1
(L.skipLineComment "--") (L.skipLineComment "--")
(L.skipBlockComment "{-" "-}") (L.skipBlockComment "{-" "-}")
@ -45,27 +49,63 @@ parseVar = Var () . VarId . T.pack <$> lexeme (liftA2 (:) upperChar (many alphaN
parseTerm :: Parser Term parseTerm :: Parser Term
parseTerm = parseVar <|> parseCon parseTerm = parseVar <|> parseCon
parseAtom :: Parser Atom parseAtom :: Parser (Atom' SrcLoc)
parseAtom = do parseAtom = annotateSrcLoc $ do
rel <- RelId . T.pack <$> lexeme (liftA2 (:) lowerChar (many alphaNumChar)) rel <- RelId . T.pack <$> lexeme (liftA2 (:) lowerChar (many alphaNumChar))
args <- parens (parseTerm `sepBy` comma) args <- parens (parseTerm `sepBy` comma)
return (Atom () rel args) return (Atom () rel args)
parseQuery :: Parser [Atom] parseQuery :: Parser [Atom' SrcLoc]
parseQuery = parseAtom `sepBy` comma parseQuery = parseAtom `sepBy` comma
parseFact :: Parser Rule parseFact :: Parser (Rule' SrcLoc)
parseFact = do parseFact = annotateSrcLoc $ do
headAtom <- parseAtom headAtom <- parseAtom
period period
return (headAtom :- []) return (Rule NoLoc headAtom [])
parseRule :: Parser Rule parseRule :: Parser (Rule' SrcLoc)
parseRule = try parseFact <|> do parseRule =
annotateSrcLoc $
try parseFact <|> do
headAtom <- parseAtom <* symbol ":-" headAtom <- parseAtom <* symbol ":-"
bodyAtoms <- parseQuery bodyAtoms <- parseQuery
period period
return (headAtom :- bodyAtoms) return (Rule NoLoc headAtom bodyAtoms)
parseProgram :: Parser Program parseProgram :: Parser (Program' SrcLoc)
parseProgram = Program () <$> many parseRule parseProgram = annotateSrcLoc $ Program NoLoc <$> many parseRule
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 :: SourcePos
}
| NoLoc
deriving (Show)
test = do
let r = runParser parseProgram "???" prog
pPrint @IO r
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).
r(0,1).
r(1,2).
r(2,3).
r(3,4).
r(4,5).
r(X,Y) :- r(Y,X).
"""

View File

@ -48,6 +48,16 @@ type Program = Program' ()
data Program' a = Program a [Rule' a] data Program' a = Program a [Rule' a]
deriving (Eq, Ord, Show) 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 class HasConstants a where
constants :: a -> Set ConId constants :: a -> Set ConId

View File

@ -26,7 +26,8 @@ library datalog-parser
base, base,
text, text,
containers, containers,
megaparsec megaparsec,
pretty-simple,
exposed-modules: exposed-modules:
Datalog.Parser, Datalog.Parser,
Datalog.Syntax Datalog.Syntax