Skip to content

Commit

Permalink
parse negatable atoms
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Jun 11, 2021
1 parent 307e47b commit 0d61922
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
26 changes: 25 additions & 1 deletion src/Datalog.elm
Expand Up @@ -871,6 +871,7 @@ type Token
| GreaterThanOrEquals
| Equals
| OrToken
| NotToken


type alias Parser a =
Expand Down Expand Up @@ -936,7 +937,16 @@ ruleBodyParser =

bodyAtomParser : Parser (Rule -> Rule)
bodyAtomParser =
Parser.succeed with
Parser.succeed
(\negative name body ->
if negative then
without name body

else
with name body
)
|= notParser
|. Parser.spaces
|= nameParser
|. Parser.spaces
|= Parser.sequence
Expand All @@ -949,6 +959,15 @@ bodyAtomParser =
}


notParser : Parser Bool
notParser =
Parser.oneOf
[ Parser.succeed True
|. Parser.token notToken
, Parser.succeed False
]


filterParser : Parser (Rule -> Rule)
filterParser =
Parser.andThen
Expand Down Expand Up @@ -1143,6 +1162,11 @@ orToken =
Parser.Token "||" (ExpectedToken OrToken)


notToken : Parser.Token ParsingProblem
notToken =
Parser.Token "not" (ExpectedToken NotToken)


{-| This is down here because my editor's highlighting is busted and it
thinks everything after '"' is a string.
-}
Expand Down
15 changes: 14 additions & 1 deletion tests/DatalogTests.elm
Expand Up @@ -534,7 +534,20 @@ datalogTests =
)
]
]
, todo "rule with negation"
, test "rule with negation" <|
\_ ->
expectParses
"""
unreachable(a, b) :-
node(a),
node(b),
not reachable(a, b).
"""
[ rule "unreachable" [ "a", "b" ]
|> with "node" [ var "a" ]
|> with "node" [ var "b" ]
|> without "reachable" [ var "a", var "b" ]
]
]
, describe "errors"
[ todo "errors!" ]
Expand Down

0 comments on commit 0d61922

Please sign in to comment.