support macro names comprised of macro args and macros

This commit is contained in:
Zachary Snow 2020-02-16 22:13:36 -05:00
parent 9af38e7870
commit ad21277eb5
3 changed files with 54 additions and 1 deletions

View File

@ -154,8 +154,35 @@ takeIdentifier = do
str <- getInput
let (ident, rest) = span isIdentChar str
advancePositions ident
macroStack <- getMacroStack
setInput rest
return ident
if null macroStack
then return ident
else do
identFollow <- takeIdentifierFollow
return $ ident ++ identFollow
takeIdentifierFollow :: PPS String
takeIdentifierFollow = do
str <- getInput
case str of
'`' : '`' : '`' : _ ->
process $ handleDirective True
'`' : '`' : _ ->
process consumeWithSubstitution
_ -> return ""
where
process :: (PPS ()) -> PPS String
process action = do
'`' <- takeChar
'`' <- takeChar
outputFollow <- getOutput
setOutput []
() <- action
outputIdent <- getOutput
setOutput outputFollow
let ident = reverse $ map fst outputIdent
identFollow <- takeIdentifierFollow
return $ ident ++ identFollow
-- read tokens after the name until the first (un-escaped) newline
takeUntilNewline :: PPS String

19
test/lex/macro_macro.sv Normal file
View File

@ -0,0 +1,19 @@
`define SUFFIX _MAGIC
`define CHOICE_FOO 1
`define CHOICE_BAR 2
`define CHOICE_FOO_MAGIC 3
`define CHOICE_BAR_MAGIC 4
`define CHOICE__MAGIC 5
`define MACRO1(A, B) \
`CHOICE_``A , `CHOICE_``B
`define MACRO2(A, B) \
`CHOICE_``A```SUFFIX , `CHOICE_``B```SUFFIX
`define MACRO3 \
`CHOICE_```SUFFIX , `CHOICE_```SUFFIX
module top;
initial begin
$display(`MACRO1(FOO, BAR));
$display(`MACRO2(FOO, BAR));
$display(`MACRO3);
end
endmodule

7
test/lex/macro_macro.v Normal file
View File

@ -0,0 +1,7 @@
module top;
initial begin
$display(1, 2);
$display(3, 4);
$display(5, 5);
end
endmodule