mirror of https://github.com/zachjs/sv2v.git
add support for `line directive
This commit is contained in:
parent
e23d68a652
commit
149d16d8fc
|
|
@ -529,12 +529,10 @@ lexFile includePaths env path = do
|
||||||
where finalToks = coalesce $ reverse $ lsToks finalState
|
where finalToks = coalesce $ reverse $ lsToks finalState
|
||||||
where
|
where
|
||||||
setEnv = do
|
setEnv = do
|
||||||
-- standardize the file path format
|
|
||||||
path' <- includeSearch path
|
|
||||||
modify $ \s -> s
|
modify $ \s -> s
|
||||||
{ lsEnv = env
|
{ lsEnv = env
|
||||||
, lsIncludePaths = includePaths
|
, lsIncludePaths = includePaths
|
||||||
, lsCurrFile = path'
|
, lsCurrFile = path
|
||||||
}
|
}
|
||||||
|
|
||||||
-- combines identifiers and numbers that cross macro boundaries
|
-- combines identifiers and numbers that cross macro boundaries
|
||||||
|
|
@ -716,6 +714,27 @@ takeQuotedString = do
|
||||||
then lexicalError $ "library includes are not supported: " ++ res
|
then lexicalError $ "library includes are not supported: " ++ res
|
||||||
else return res
|
else return res
|
||||||
|
|
||||||
|
-- removes and returns a decimal number
|
||||||
|
takeNumber :: Alex Int
|
||||||
|
takeNumber = do
|
||||||
|
dropSpaces
|
||||||
|
leadCh <- peekChar
|
||||||
|
if '0' <= leadCh && leadCh <= '9'
|
||||||
|
then step 0
|
||||||
|
else lexicalError $ "expected number, but found unexpected char: "
|
||||||
|
++ show leadCh
|
||||||
|
where
|
||||||
|
step number = do
|
||||||
|
ch <- takeChar
|
||||||
|
if ch == ' ' || ch == '\n' then
|
||||||
|
return number
|
||||||
|
else if '0' <= ch && ch <= '9' then do
|
||||||
|
let digit = ord ch - ord '0'
|
||||||
|
step $ number * 10 + digit
|
||||||
|
else
|
||||||
|
lexicalError $ "unexpected char while reading number: "
|
||||||
|
++ show ch
|
||||||
|
|
||||||
peekChar :: Alex Char
|
peekChar :: Alex Char
|
||||||
peekChar = do
|
peekChar = do
|
||||||
(_, _, _, str) <- alexGetInput
|
(_, _, _, str) <- alexGetInput
|
||||||
|
|
@ -881,6 +900,16 @@ handleDirective (posOrig, _, _, strOrig) len = do
|
||||||
modify $ push $ Token Lit_number tokStr tokPos
|
modify $ push $ Token Lit_number tokStr tokPos
|
||||||
alexMonadScan
|
alexMonadScan
|
||||||
|
|
||||||
|
"line" -> do
|
||||||
|
lineNumber <- takeNumber
|
||||||
|
quotedFilename <- takeQuotedString
|
||||||
|
_ <- takeNumber -- level, ignored
|
||||||
|
let filename = init $ tail quotedFilename
|
||||||
|
setCurrentFile filename
|
||||||
|
(AlexPn f _ c, prev, _, str) <- alexGetInput
|
||||||
|
alexSetInput (AlexPn f (lineNumber + 1) c, prev, [], str)
|
||||||
|
alexMonadScan
|
||||||
|
|
||||||
"include" -> do
|
"include" -> do
|
||||||
quotedFilename <- takeQuotedString
|
quotedFilename <- takeQuotedString
|
||||||
inputFollow <- alexGetInput
|
inputFollow <- alexGetInput
|
||||||
|
|
@ -968,7 +997,7 @@ handleDirective (posOrig, _, _, strOrig) len = do
|
||||||
currToks <- gets lsToks
|
currToks <- gets lsToks
|
||||||
modify $ \s -> s { lsToks = [] }
|
modify $ \s -> s { lsToks = [] }
|
||||||
-- lex the macro expansion, preserving the file and line
|
-- lex the macro expansion, preserving the file and line
|
||||||
alexSetInput (AlexPn 0 l 0, ' ' , [], replacement)
|
alexSetInput (AlexPn 0 l 0, ' ', [], replacement)
|
||||||
alexMonadScan
|
alexMonadScan
|
||||||
-- re-tag and save tokens from the macro expansion
|
-- re-tag and save tokens from the macro expansion
|
||||||
newToks <- gets lsToks
|
newToks <- gets lsToks
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,8 @@
|
||||||
|
`ifndef INCLUDED
|
||||||
|
`define INCLUDED
|
||||||
|
`include "file_line.sv"
|
||||||
|
`else
|
||||||
|
|
||||||
`define MACRO(arg) $display(arg, `__FILE__, `__LINE__);
|
`define MACRO(arg) $display(arg, `__FILE__, `__LINE__);
|
||||||
`define MACRO_NO_ARG $display(`__FILE__, `__LINE__);
|
`define MACRO_NO_ARG $display(`__FILE__, `__LINE__);
|
||||||
module top;
|
module top;
|
||||||
|
|
@ -14,3 +19,5 @@ initial begin
|
||||||
`MACRO("f")
|
`MACRO("f")
|
||||||
end
|
end
|
||||||
endmodule
|
endmodule
|
||||||
|
|
||||||
|
`endif
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
module top;
|
||||||
|
initial begin
|
||||||
|
$display(`__FILE__, `__LINE__);
|
||||||
|
`line 101 "fake.v" 1
|
||||||
|
$display(`__FILE__, `__LINE__);
|
||||||
|
end
|
||||||
|
endmodule
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
module top;
|
||||||
|
initial begin
|
||||||
|
$display("line.sv", `__LINE__);
|
||||||
|
;
|
||||||
|
$display("fake.v", 102);
|
||||||
|
end
|
||||||
|
endmodule
|
||||||
Loading…
Reference in New Issue