From 89e4f2a248faaf77660853993df41d2705f4d24b Mon Sep 17 00:00:00 2001 From: Zachary Snow Date: Sun, 11 Aug 2019 17:55:42 -0400 Subject: [PATCH] allow newlines before left paren of macro arguments --- src/Language/SystemVerilog/Parser/Lex.x | 42 +++++++++++-------------- test/lex/macro_whitespace.sv | 12 +++++++ test/lex/macro_whitespace.v | 1 + 3 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 test/lex/macro_whitespace.sv create mode 100644 test/lex/macro_whitespace.v diff --git a/src/Language/SystemVerilog/Parser/Lex.x b/src/Language/SystemVerilog/Parser/Lex.x index 03e0c9e..c7bb4bb 100644 --- a/src/Language/SystemVerilog/Parser/Lex.x +++ b/src/Language/SystemVerilog/Parser/Lex.x @@ -485,35 +485,29 @@ takeChar = do -- drop spaces in the input until a non-space is reached or EOF dropSpaces :: Alex () dropSpaces = do - (_, _, _, str) <- alexGetInput - if null str || head str /= ' ' - then return () - else dropSpace >> dropSpaces - where - dropSpace :: Alex () - dropSpace = do - (pos, _, _, str) <- alexGetInput - case str of - [] -> return () - ' ' : rest -> alexSetInput (alexMove pos ' ', ' ', [], rest) - ch : _ -> lexicalError $ "expected ' ', but found: " ++ show ch + (pos, _, _, str) <- alexGetInput + case str of + ' ' : rest -> do + alexSetInput (alexMove pos ' ', ' ', [], rest) + dropSpaces + [] -> return () + _ -> return () isWhitespaceChar :: Char -> Bool isWhitespaceChar ch = elem ch [' ', '\t', '\n'] --- drop leading whitespace in the input +-- drop all leading whitespace in the input dropWhitespace :: Alex () dropWhitespace = do - (_, _, _, str) <- alexGetInput - if null str || not (isWhitespaceChar $ head str) - then return () - else dropChar >> dropWhitespace - where - dropChar :: Alex () - dropChar = do - (pos, _, _, chs) <- alexGetInput - let ch : rest = chs - alexSetInput (alexMove pos ch, ch, [], rest) + (pos, _, _, str) <- alexGetInput + case str of + ch : chs -> + if isWhitespaceChar ch + then do + alexSetInput (alexMove pos ch, ch, [], chs) + dropWhitespace + else return() + [] -> return () -- removes and returns a quoted string such as or "foo.bar" takeQuotedString :: Alex String @@ -572,7 +566,7 @@ takeMacroDefinition = do -- "", except to delimit arguments or end the list of arguments; see 22.5.1 takeMacroArguments :: Alex [String] takeMacroArguments = do - dropSpaces + dropWhitespace leadCh <- takeChar if leadCh == '(' then argLoop diff --git a/test/lex/macro_whitespace.sv b/test/lex/macro_whitespace.sv new file mode 100644 index 0000000..b1bb488 --- /dev/null +++ b/test/lex/macro_whitespace.sv @@ -0,0 +1,12 @@ +`define FOO(a, b) ((a)+(b)) + +module top; + initial begin + $display(`FOO + ( + 1 + , + 2 + )); + end +endmodule diff --git a/test/lex/macro_whitespace.v b/test/lex/macro_whitespace.v new file mode 100644 index 0000000..1bad827 --- /dev/null +++ b/test/lex/macro_whitespace.v @@ -0,0 +1 @@ +`include "macro_whitespace.sv"