1
1
mirror of https://github.com/github/semantic.git synced 2025-01-03 04:51:57 +03:00

🔥 withTokens.

This commit is contained in:
Rob Rix 2017-05-03 10:50:22 -04:00
parent 2a841a672b
commit 4c5a5b56c6
2 changed files with 1 additions and 11 deletions

View File

@ -6,7 +6,6 @@ module Data.Syntax.Assignment
, symbol , symbol
, source , source
, children , children
, withTokens
, Rose(..) , Rose(..)
, RoseF(..) , RoseF(..)
, Node , Node
@ -43,7 +42,6 @@ data AssignmentF node a where
Location :: AssignmentF node Location Location :: AssignmentF node Location
Source :: AssignmentF symbol ByteString Source :: AssignmentF symbol ByteString
Children :: Assignment symbol a -> AssignmentF symbol a Children :: Assignment symbol a -> AssignmentF symbol a
WithTokens :: Assignment symbol a -> AssignmentF symbol a
Choose :: IntMap.IntMap a -> AssignmentF node a Choose :: IntMap.IntMap a -> AssignmentF node a
Alt :: a -> a -> AssignmentF symbol a Alt :: a -> a -> AssignmentF symbol a
Empty :: AssignmentF symbol a Empty :: AssignmentF symbol a
@ -68,10 +66,6 @@ source = Source `Then` return
children :: Assignment symbol a -> Assignment symbol a children :: Assignment symbol a -> Assignment symbol a
children forEach = Children forEach `Then` return children forEach = Children forEach `Then` return
-- | Match the argument without automatically dropping tokens.
withTokens :: Assignment symbol a -> Assignment symbol a
withTokens sub = WithTokens sub `Then` return
-- | A rose tree. -- | A rose tree.
data Rose a = Rose { roseValue :: !a, roseChildren :: ![Rose a] } data Rose a = Rose { roseValue :: !a, roseChildren :: ![Rose a] }
@ -144,9 +138,6 @@ runAssignment = iterFreer run . fmap (\ a state -> Result [] (Just (state, a)))
(Children childAssignment, Rose _ children : _) -> case assignAllFrom childAssignment state { stateNodes = children } of (Children childAssignment, Rose _ children : _) -> case assignAllFrom childAssignment state { stateNodes = children } of
Result _ (Just (state', a)) -> yield a (advanceState state' { stateNodes = stateNodes }) Result _ (Just (state', a)) -> yield a (advanceState state' { stateNodes = stateNodes })
Result es Nothing -> Result es Nothing Result es Nothing -> Result es Nothing
(WithTokens sub, _) -> case runAssignment sub initialState { stateTypes = Anonymous : stateTypes } of
Result _ (Just (state', a)) -> yield a state' { stateTypes = stateTypes }
Result es Nothing -> Result es Nothing
(Choose choices, Rose (symbol :. _) _ : _) | Just a <- IntMap.lookup (fromEnum symbol) choices -> yield a state (Choose choices, Rose (symbol :. _) _ : _) | Just a <- IntMap.lookup (fromEnum symbol) choices -> yield a state
-- 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, _) -> yield a state <|> yield b state (Alt a b, _) -> yield a state <|> yield b state
@ -198,7 +189,6 @@ instance Show symbol => Show1 (AssignmentF (Node symbol)) where
Location -> showString "Location" . sp d (Info.Range 0 0 :. Info.SourceSpan (Info.SourcePos 0 0) (Info.SourcePos 0 0) :. Nil) Location -> showString "Location" . sp d (Info.Range 0 0 :. Info.SourceSpan (Info.SourcePos 0 0) (Info.SourcePos 0 0) :. Nil)
Source -> showString "Source" . showChar ' ' . sp d "" Source -> showString "Source" . showChar ' ' . sp d ""
Children a -> showsUnaryWith (liftShowsPrec sp sl) "Children" d a Children a -> showsUnaryWith (liftShowsPrec sp sl) "Children" d a
WithTokens a -> showsUnaryWith (liftShowsPrec sp sl) "WithTokens" d a
Choose choices -> showsUnaryWith (liftShowsPrec (liftShowsPrec sp sl) (liftShowList sp sl)) "Choose" d (IntMap.toList choices) Choose choices -> showsUnaryWith (liftShowsPrec (liftShowsPrec sp sl) (liftShowList sp sl)) "Choose" d (IntMap.toList choices)
Alt a b -> showsBinaryWith sp sp "Alt" d a b Alt a b -> showsBinaryWith sp sp "Alt" d a b
Empty -> showString "Empty" Empty -> showString "Empty"

View File

@ -84,7 +84,7 @@ statement = exit Statement.Return Return
<|> until <|> until
<|> for <|> for
<|> literal <|> literal
<|> symbol OperatorAssignment *> term <*> children (lvalue >>= \ var -> Statement.Assignment var <$ withTokens (symbol AnonPlusEqual) <*> (term <*> (Expression.Plus var <$> expression))) <|> symbol OperatorAssignment *> term <*> children (lvalue >>= \ var -> Statement.Assignment var <$ symbol AnonPlusEqual <*> (term <*> (Expression.Plus var <$> expression)))
where exit construct sym = symbol sym *> term <*> children (construct <$> optional (symbol ArgumentList *> children statement)) where exit construct sym = symbol sym *> term <*> children (construct <$> optional (symbol ArgumentList *> children statement))
lvalue :: Assignment (Node Grammar) (Term Syntax Location) lvalue :: Assignment (Node Grammar) (Term Syntax Location)