Skip to content

Commit f63e6ae

Browse files
authored
Merge pull request #4115 from roccomao/fix-string-parse
Verilog: Skip the escaped characters in string
2 parents a8d6f6d + 6db6638 commit f63e6ae

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--sort=no
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
test_string input.sv /^package test_string;$/;" K
2+
test_var_a input.sv /^ int test_var_a = 1'b1;$/;" r package:test_string
3+
test_str_a input.sv /^ string test_str_a = "Hello World!\\n";$/;" r package:test_string
4+
test_var_b input.sv /^ int test_var_b = 100;$/;" r package:test_string
5+
test_str_b input.sv /^ string test_str_b = "\\"";$/;" r package:test_string
6+
test_var_c input.sv /^ int test_var_c = -1;$/;" r package:test_string
7+
test_str_c input.sv /^ string test_str_c = "\\b \\n \\t \\\\ \\" \\v \\f \\a \\0 \\1 \\11 \\377 \\x0 \\x01 \\xff \\e"/;" r package:test_string
8+
test_var_d input.sv /^ int test_var_d = 1024;$/;" r package:test_string
9+
test_str_d input.sv /^ string test_str_d = "\\"\\\\\\"";$/;" r package:test_string
10+
test_var_e input.sv /^ int test_var_e = -1;$/;" r package:test_string
11+
test_str_e input.sv /^ string test_str_e = "\\\\";$/;" r package:test_string
12+
test_str_f input.sv /^ string test_str_f = "\\\\\\\\\\\\\\\\";$/;" r package:test_string
13+
test_str_g input.sv /^ string test_str_g = "\\"\\"\\"";$/;" r package:test_string
14+
test_str_h input.sv /^ string test_str_h = "\\"\\"\\"\\"";$/;" r package:test_string
15+
test_str_i input.sv /^ string test_str_i = "\\\\\\\\\\\\\\"";$/;" r package:test_string
16+
test_str_j input.sv /^ string test_str_j = "\\"\\\\\\\\\\\\";$/;" r package:test_string
17+
test_str_k input.sv /^ string test_str_k = "\\0\\188\\xf\\"\\\\\\"\\\\\\"\\\\";$/;" r package:test_string
18+
test_str_l input.sv /^ localparam byte test_str_l = "\\\\" ;$/;" c package:test_string
19+
test_str_m input.sv /^ localparam byte test_str_m = "\\"" ;$/;" c package:test_string
20+
test_str_n input.sv /^ parameter string test_str_n = "\\t\\n\\\\\\"\\v\\f";$/;" c package:test_string
21+
test_struct_str input.sv /^ } test_struct_str;$/;" S package:test_string
22+
name input.sv /^ string name = "Hello \\" World!";$/;" w struct:test_string.test_struct_str
23+
addr input.sv /^ bit [23:0] addr;$/;" w struct:test_string.test_struct_str
24+
test_typedef_str input.sv /^ } test_typedef_str;$/;" T package:test_string
25+
name input.sv /^ string name = "\\0\\\\\\"\\\\";$/;" w typedef:test_string.test_typedef_str
26+
addr input.sv /^ bit [23:0] addr;$/;" w typedef:test_string.test_typedef_str
27+
type_id input.sv /^ string type_id = "\\a\\t\\0\\xddVerilog\\"";$/;" w typedef:test_string.test_typedef_str
28+
test_func_port input.sv /^ function void test_func_port (string test_str_port = "\\\\\\"", int test_var_port=1);$/;" f package:test_string
29+
test_str_port input.sv /^ function void test_func_port (string test_str_port = "\\\\\\"", int test_var_port=1);$/;" p function:test_string.test_func_port
30+
test_var_port input.sv /^ function void test_func_port (string test_str_port = "\\\\\\"", int test_var_port=1);$/;" p function:test_string.test_func_port
31+
test_task_port input.sv /^ task test_task_port (int test_int_port, string test_str_port="\\"\\\\", byte test_byte_port =/;" t package:test_string
32+
test_int_port input.sv /^ task test_task_port (int test_int_port, string test_str_port="\\"\\\\", byte test_byte_port =/;" p task:test_string.test_task_port
33+
test_str_port input.sv /^ task test_task_port (int test_int_port, string test_str_port="\\"\\\\", byte test_byte_port =/;" p task:test_string.test_task_port
34+
test_byte_port input.sv /^ task test_task_port (int test_int_port, string test_str_port="\\"\\\\", byte test_byte_port =/;" p task:test_string.test_task_port
35+
test_ending_var input.sv /^ bit [7:0] test_ending_var;$/;" r package:test_string
36+
test_ending_str input.sv /^ string test_ending_str = "'\\"'END;'";$/;" r package:test_string
37+
test_ending input.sv /^ function void test_ending(); endfunction$/;" f package:test_string
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package test_string;
2+
int test_var_a = 1'b1;
3+
string test_str_a = "Hello World!\n";
4+
int test_var_b = 100;
5+
string test_str_b = "\"";
6+
int test_var_c = -1;
7+
8+
// LRM 5.9.1 Special characters in strings
9+
// \n \t \\ \" \v \f \a \ddd \xdd ( Others: such as "\b" is treated the same as "b" )
10+
string test_str_c = "\b \n \t \\ \" \v \f \a \0 \1 \11 \377 \x0 \x01 \xff \e";
11+
12+
int test_var_d = 1024;
13+
string test_str_d = "\"\\\"";
14+
int test_var_e = -1;
15+
string test_str_e = "\\";
16+
string test_str_f = "\\\\\\\\";
17+
string test_str_g = "\"\"\"";
18+
string test_str_h = "\"\"\"\"";
19+
string test_str_i = "\\\\\\\"";
20+
string test_str_j = "\"\\\\\\";
21+
string test_str_k = "\0\188\xf\"\\\"\\\"\\";
22+
23+
localparam byte test_str_l = "\\" ;
24+
localparam byte test_str_m = "\"" ;
25+
parameter string test_str_n = "\t\n\\\"\v\f";
26+
27+
struct {
28+
string name = "Hello \" World!";
29+
bit [23:0] addr;
30+
} test_struct_str;
31+
32+
typedef struct {
33+
string name = "\0\\\"\\";
34+
bit [23:0] addr;
35+
string type_id = "\a\t\0\xddVerilog\"";
36+
} test_typedef_str;
37+
38+
function void test_func_port (string test_str_port = "\\\"", int test_var_port=1);
39+
endfunction
40+
task test_task_port (int test_int_port, string test_str_port="\"\\", byte test_byte_port = "\"");
41+
endtask
42+
43+
// All tags above should NOT be missed
44+
bit [7:0] test_ending_var;
45+
string test_ending_str = "'\"'END;'";
46+
function void test_ending(); endfunction
47+
endpackage

parsers/verilog.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,13 +768,24 @@ static int skipToSemiColon (int c)
768768
return c; // ';' or EOF
769769
}
770770

771+
static bool isEscapedCharacter (int c)
772+
{
773+
if (c != '\\')
774+
return false;
775+
c = vGetc ();
776+
if (c == '"')
777+
return true;
778+
else
779+
return false;
780+
}
781+
771782
static int skipString (int c)
772783
{
773784
if (c == '"')
774785
{
775786
do
776787
c = vGetc ();
777-
while (c != '"' && c != EOF);
788+
while (isEscapedCharacter(c) || (c != '"' && c != EOF));
778789
}
779790
c = skipWhite (vGetc ());
780791
return c;

0 commit comments

Comments
 (0)