Skip to content

Commit

Permalink
parse comments pretty much anywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Jun 11, 2021
1 parent ffcfbd0 commit fa7769e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
48 changes: 35 additions & 13 deletions src/Datalog.elm
Expand Up @@ -872,6 +872,7 @@ type Token
| Equals
| OrToken
| NotToken
| LineComment


type alias Parser a =
Expand All @@ -881,7 +882,7 @@ type alias Parser a =
parser : Parser (List Rule)
parser =
Parser.succeed identity
|. Parser.spaces
|. spacesAndComments
|= Parser.loop [] parserHelp


Expand All @@ -890,7 +891,7 @@ parserHelp soFar =
Parser.oneOf
[ Parser.succeed (\newRule -> Parser.Loop (newRule :: soFar))
|= ruleParser
|. Parser.spaces
|. spacesAndComments
, Parser.lazy (\_ -> Parser.succeed (Parser.Done (List.reverse soFar)))
]

Expand All @@ -899,20 +900,20 @@ ruleParser : Parser Rule
ruleParser =
Parser.succeed (List.foldl (\with_ rule_ -> with_ rule_))
|= ruleHeadParser
|. Parser.spaces
|. spacesAndComments
|= ruleBodyParser


ruleHeadParser : Parser Rule
ruleHeadParser =
Parser.succeed rule
|= Parser.inContext NameOfRule nameParser
|. Parser.spaces
|. spacesAndComments
|= Parser.sequence
{ start = openParenToken
, separator = commaToken
, end = closeParenToken
, spaces = Parser.spaces
, spaces = spacesAndComments
, item = Parser.inContext VariableInRuleHead nameParser
, trailing = Parser.Forbidden
}
Expand All @@ -925,7 +926,7 @@ ruleBodyParser =
{ start = hornToken
, separator = commaToken
, end = periodToken
, spaces = Parser.spaces
, spaces = spacesAndComments
, item =
Parser.oneOf
[ Parser.inContext AtomInBody (Parser.backtrackable bodyAtomParser)
Expand All @@ -946,14 +947,14 @@ bodyAtomParser =
with name body
)
|= notParser
|. Parser.spaces
|. spacesAndComments
|= nameParser
|. Parser.spaces
|. spacesAndComments
|= Parser.sequence
{ start = openParenToken
, separator = commaToken
, end = closeParenToken
, spaces = Parser.spaces
, spaces = spacesAndComments
, item = Parser.inContext TermInAtom termParser
, trailing = Parser.Forbidden
}
Expand All @@ -979,9 +980,9 @@ filterParserHelp : Filter -> Parser (Parser.Step Filter (Rule -> Rule))
filterParserHelp lastFilter =
Parser.oneOf
[ Parser.succeed (\nextFilter -> Parser.Loop (or lastFilter nextFilter))
|. Parser.spaces
|. Parser.backtrackable spacesAndComments
|. Parser.token orToken
|. Parser.spaces
|. spacesAndComments
|= filterClauseParser
, Parser.lazy (\_ -> Parser.succeed (Parser.Done (filter lastFilter)))
]
Expand All @@ -991,9 +992,9 @@ filterClauseParser : Parser Filter
filterClauseParser =
Parser.succeed (\lhs op rhs -> op lhs rhs)
|= nameParser
|. Parser.spaces
|. spacesAndComments
|= opParser
|. Parser.spaces
|. spacesAndComments
|= termParser
|> Parser.inContext FilterInClause

Expand Down Expand Up @@ -1098,6 +1099,22 @@ disallowedNameChar =
]


spacesAndComments : Parser ()
spacesAndComments =
Parser.oneOf
[ Parser.succeed ()
|. Parser.backtrackable Parser.spaces
|. lineCommentParser
|. Parser.spaces
, Parser.spaces
]


lineCommentParser : Parser ()
lineCommentParser =
Parser.lineComment lineCommentToken



-- TOKENS

Expand Down Expand Up @@ -1167,6 +1184,11 @@ notToken =
Parser.Token "not" (ExpectedToken NotToken)


lineCommentToken : Parser.Token ParsingProblem
lineCommentToken =
Parser.Token "--" (ExpectedToken LineComment)


{-| This is down here because my editor's highlighting is busted and it
thinks everything after '"' is a string.
-}
Expand Down
14 changes: 14 additions & 0 deletions tests/DatalogTests.elm
Expand Up @@ -548,6 +548,20 @@ datalogTests =
|> with "node" [ var "b" ]
|> without "reachable" [ var "a", var "b" ]
]
, test "comments" <|
\_ ->
expectParses
"""
-- before
adult(name) :- -- before body
person(name, age), -- after rule
age > 17 -- before close
. -- after
"""
[ rule "adult" [ "name" ]
|> with "person" [ var "name", var "age" ]
|> filter (gt "age" (int 17))
]
]
]
]
Expand Down

0 comments on commit fa7769e

Please sign in to comment.