1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 22:31:36 +03:00

Running an assignment returns the Source.

This commit is contained in:
Rob Rix 2017-04-21 12:19:50 -04:00
parent 41848c415d
commit 35e3643724

View File

@ -89,13 +89,13 @@ data Result a = Result a | Error [Text]
-- | Run an assignment of nodes in a grammar onto terms in a syntax, discarding any unparsed nodes. -- | Run an assignment of nodes in a grammar onto terms in a syntax, discarding any unparsed nodes.
assignAll :: (Symbol grammar, Eq grammar, Show grammar) => Assignment (Node grammar) a -> Source -> [AST grammar] -> Result a assignAll :: (Symbol grammar, Eq grammar, Show grammar) => Assignment (Node grammar) a -> Source -> [AST grammar] -> Result a
assignAll assignment source nodes = case runAssignment assignment source nodes of assignAll assignment source nodes = case runAssignment assignment source nodes of
Result (rest, a) -> case dropAnonymous rest of Result (_, rest, a) -> case dropAnonymous rest of
[] -> Result a [] -> Result a
c:_ -> Error ["Expected end of input, but got: " <> show c] c:_ -> Error ["Expected end of input, but got: " <> show c]
Error e -> Error e Error e -> Error e
-- | Run an assignment of nodes in a grammar onto terms in a syntax. -- | Run an assignment of nodes in a grammar onto terms in a syntax.
runAssignment :: (Symbol grammar, Eq grammar, Show grammar) => Assignment (Node grammar) a -> Source -> [AST grammar] -> Result ([AST grammar], a) runAssignment :: (Symbol grammar, Eq grammar, Show grammar) => Assignment (Node grammar) a -> Source -> [AST grammar] -> Result (Source, [AST grammar], a)
runAssignment = iterFreer (\ assignment yield source nodes -> case (assignment, dropAnonymous nodes) of runAssignment = iterFreer (\ assignment yield source nodes -> case (assignment, dropAnonymous nodes) of
-- Nullability: some rules, e.g. 'pure a' and 'many a', should match at the end of input. Either side of an alternation may be nullable, ergo Alt can match at the end of input. -- Nullability: some rules, e.g. 'pure a' and 'many a', should match at the end of input. Either side of an alternation may be nullable, ergo Alt can match at the end of input.
(Alt a b, nodes) -> yield a source nodes <|> yield b source nodes -- FIXME: Symbol `Alt` Symbol `Alt` Symbol is inefficient, should build and match against an IntMap instead. (Alt a b, nodes) -> yield a source nodes <|> yield b source nodes -- FIXME: Symbol `Alt` Symbol `Alt` Symbol is inefficient, should build and match against an IntMap instead.
@ -110,7 +110,7 @@ runAssignment = iterFreer (\ assignment yield source nodes -> case (assignment,
(Source, []) -> Error [ "Expected leaf node but got end of input." ] (Source, []) -> Error [ "Expected leaf node but got end of input." ]
(Children _, []) -> Error [ "Expected branch node but got end of input." ] (Children _, []) -> Error [ "Expected branch node but got end of input." ]
_ -> Error ["No rule to match at end of input."]) _ -> Error ["No rule to match at end of input."])
. fmap (\ a _ rest -> Result (rest, a)) . fmap (\ a source rest -> Result (source, rest, a))
dropAnonymous :: Symbol grammar => [AST grammar] -> [AST grammar] dropAnonymous :: Symbol grammar => [AST grammar] -> [AST grammar]
dropAnonymous = dropWhile ((/= Regular) . symbolType . rhead . roseValue) dropAnonymous = dropWhile ((/= Regular) . symbolType . rhead . roseValue)