Current progress
This commit is contained in:
parent
902fb4e0b5
commit
d28638e286
@ -1,18 +1,18 @@
|
|||||||
{-# LANGUAGE MultilineStrings #-}
|
{-# LANGUAGE MultilineStrings #-}
|
||||||
|
|
||||||
module Datalog.Parser
|
module Datalog.Parser (
|
||||||
( parseTerm
|
parseTerm,
|
||||||
, parseAtom
|
parseAtom,
|
||||||
, parseRule
|
parseRule,
|
||||||
, parseQuery
|
parseQuery,
|
||||||
, parseProgram
|
parseProgram,
|
||||||
)
|
)
|
||||||
where
|
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
|
||||||
@ -27,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 "{-" "-}")
|
||||||
@ -48,48 +49,54 @@ 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 (Rule () 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 (Rule () 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 :: (Functor f) => Parser (f a) -> Parser (f SrcLoc)
|
||||||
annotateSrcLoc p = do
|
annotateSrcLoc p = do
|
||||||
s <- getSourcePos
|
s <- getSourcePos
|
||||||
res <- p
|
res <- p
|
||||||
f <- getSourcePos
|
f <- getSourcePos
|
||||||
pure (SrcLoc s f <$ res)
|
pure (SrcLoc s f <$ res)
|
||||||
|
|
||||||
data SrcLoc = SrcLoc
|
data SrcLoc
|
||||||
|
= SrcLoc
|
||||||
{ start :: SourcePos
|
{ start :: SourcePos
|
||||||
, end :: SourcePos
|
, end :: SourcePos
|
||||||
} deriving Show
|
}
|
||||||
|
| NoLoc
|
||||||
|
deriving (Show)
|
||||||
|
|
||||||
test = do
|
test = do
|
||||||
let r = runParser parseProgram "???" prog
|
let r = runParser parseProgram "???" prog
|
||||||
pPrint @IO r
|
pPrint @IO r
|
||||||
|
|
||||||
prog = """
|
prog =
|
||||||
|
"""
|
||||||
odd(X,Y) :- r(X,Y).
|
odd(X,Y) :- r(X,Y).
|
||||||
odd(X,Y) :- even(X,Z), r(Z,Y).
|
odd(X,Y) :- even(X,Z), r(Z,Y).
|
||||||
even(X,Y) :- odd(X,Z), r(Z,Y).
|
even(X,Y) :- odd(X,Z), r(Z,Y).
|
||||||
|
|||||||
@ -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
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user