Commit 39f377e0 by Zachary Snow

lexer has useful tagging of tokens from a macro expansion

parent 2f91e25a
...@@ -749,18 +749,29 @@ handleDirective (posOrig, _, _, strOrig) len = do ...@@ -749,18 +749,29 @@ handleDirective (posOrig, _, _, strOrig) len = do
case Map.lookup directive env of case Map.lookup directive env of
Nothing -> lexicalError $ "Undefined macro: " ++ directive Nothing -> lexicalError $ "Undefined macro: " ++ directive
Just (body, formalArgs) -> do Just (body, formalArgs) -> do
-- TODO: How should we track the file position when we (AlexPn _ l c, _, _, _) <- alexGetInput
-- substitute in a macro?
replacement <- if null formalArgs replacement <- if null formalArgs
then return body then return body
else do else do
actualArgs <- takeMacroArguments actualArgs <- takeMacroArguments
defaultedArgs <- defaultMacroArgs (map snd formalArgs) actualArgs defaultedArgs <- defaultMacroArgs (map snd formalArgs) actualArgs
return $ substituteArgs body (map fst formalArgs) defaultedArgs return $ substituteArgs body (map fst formalArgs) defaultedArgs
let size = length replacement -- save our current state
(AlexPn f l c, _, [], str) <- alexGetInput currInput <- alexGetInput
let pos = AlexPn (f - size) l (c - size) currToks <- gets lsToks
alexSetInput (pos, ' ', [], replacement ++ str) modify $ \s -> s { lsToks = [] }
-- lex the macro expansion, preserving the file and line
alexSetInput (AlexPn 0 l 0, ' ' , [], replacement)
alexMonadScan
-- re-tag and save tokens from the macro expansion
newToks <- gets lsToks
currFile <- getCurrentFile
let loc = "macro expansion of " ++ directive ++ " at " ++ currFile
let pos = Position loc l (c - length directive - 1)
let reTag (Token a b _) = Token a b pos
modify $ \s -> s { lsToks = (map reTag newToks) ++ currToks }
-- continue lexing after the macro
alexSetInput currInput
alexMonadScan alexMonadScan
-- remove characters from the input until the pattern is reached -- remove characters from the input until the pattern is reached
......
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