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

Factor repetitions in.

This commit is contained in:
Rob Rix 2017-08-31 11:02:34 -04:00
parent 0715f8a0b2
commit 2d65031643

View File

@ -182,8 +182,10 @@ advance = tracing Advance `Then` return
choice :: (Bounded grammar, Ix grammar, HasCallStack) => [Assignment ast grammar a] -> Assignment ast grammar a choice :: (Bounded grammar, Ix grammar, HasCallStack) => [Assignment ast grammar a] -> Assignment ast grammar a
choice alternatives = tracing (Choose symbols (IntMap.fromList choices) (asum (fmap Just atEnd))) `Then` id choice alternatives = tracing (Choose symbols (IntMap.fromList choices) (asum (fmap Just atEnd))) `Then` id
where (symbols, choices, atEnd) = foldr (<>) ([], [], []) (fmap toChoices alternatives) where (symbols, choices, atEnd) = foldr (<>) ([], [], []) (fmap toChoices alternatives)
toChoices :: Assignment ast grammar a -> ([grammar], [(Int, Assignment ast grammar a)], [Assignment ast grammar a])
toChoices rule = case rule of toChoices rule = case rule of
Tracing _ (Choose s c a) `Then` continue -> (s, IntMap.toList (fmap continue c), toList (fmap continue a)) Tracing _ (Choose s c a) `Then` continue -> (s, IntMap.toList (fmap continue c), toList (fmap continue a))
Tracing _ (Many child) `Then` _ -> let (s, c, _) = toChoices child in (s, fmap (rule <$) c, [rule])
_ -> ([], [], [rule]) _ -> ([], [], [rule])
-- | Match and advance past a node with the given symbol. -- | Match and advance past a node with the given symbol.