Commit 317994ed by Zachary Snow

faster lexing by storing tokens in reverse order

parent b45217ba
...@@ -314,7 +314,7 @@ data Cond ...@@ -314,7 +314,7 @@ data Cond
-- our custom lexer state -- our custom lexer state
data AlexUserState = LS data AlexUserState = LS
{ lsToks :: [Token] -- tokens read so far { lsToks :: [Token] -- tokens read so far, *in reverse order* for efficiency
, lsCurrFile :: FilePath -- currently active filename , lsCurrFile :: FilePath -- currently active filename
, lsEnv :: Map.Map String (String, [String]) -- active macro definitions , lsEnv :: Map.Map String (String, [String]) -- active macro definitions
, lsCondStack :: [Cond] -- if-else cascade state , lsCondStack :: [Cond] -- if-else cascade state
...@@ -336,7 +336,7 @@ lexFile includePaths env path = do ...@@ -336,7 +336,7 @@ lexFile includePaths env path = do
Left msg -> error $ "Lexical Error: " ++ msg Left msg -> error $ "Lexical Error: " ++ msg
Right finalState -> Right finalState ->
if null $ lsCondStack finalState if null $ lsCondStack finalState
then lsToks finalState then reverse $ lsToks finalState
else error $ "unfinished conditional directives: " ++ else error $ "unfinished conditional directives: " ++
(show $ length $ lsCondStack finalState) (show $ length $ lsCondStack finalState)
where where
...@@ -749,7 +749,7 @@ removeUntil pattern _ _ = loop ...@@ -749,7 +749,7 @@ removeUntil pattern _ _ = loop
else loop else loop
push :: Token -> AlexUserState -> AlexUserState push :: Token -> AlexUserState -> AlexUserState
push t s = s { lsToks = (lsToks s) ++ [t] } push t s = s { lsToks = t : (lsToks s) }
tok :: TokenName -> Action tok :: TokenName -> Action
tok tokId (pos, _, _, input) len = do tok tokId (pos, _, _, input) len = do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment