Skip to content

Commit

Permalink
HeaderParser.pm - parse single quoted backslash properly
Browse files Browse the repository at this point in the history
Karl gave me a test script which was breaking because we did not handle
single quoted escapes like '\\' and '\n' and the like. This fixes the
tokenizer pattern to handle these without dieing, although we do not
validate that the escape sequence itself is legal, we no longer
choke on having backslash something in the single quote.

This includes tests for this, as well as for constructs like

    #if 1

which we had no tests for previously.
  • Loading branch information
demerphq committed Feb 13, 2025
1 parent 280818b commit 15bf1b6
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion regen/HeaderParser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ BEGIN {
(?<literal>
(?<define> defined\(\w+\) )
| (?<func> \w+\s*\(\s*\w+(?:\s*,\s*\w+)*\s*\) )
| (?<const> (?:0x[a-fA-F0-9]+|\d+[LU]*|'.') )
| (?<const> (?:0x[a-fA-F0-9]+|\d+[LU]*|'.'|'\\.') )
| (?<sym> \w+ )
)
| (?<op> $binop_pat | $unop_pat )
Expand Down
101 changes: 101 additions & 0 deletions t/porting/header_parser.t
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ $hp->parse_text(<<~'EOF');
#error #pragma
#error #include
#error #define
#if 1
#if CH == 'x' || CH == '\\'
#define CH_IS_IT
#endif
#endif
EOF
my $normal= $hp->lines_as_str();
my $lines= $hp->lines();
Expand Down Expand Up @@ -353,6 +358,97 @@ is($lines_as_str,<<~'DUMP_EOF', "Simple data structure as expected") or show_tex
"start_line_num" => 27,
"sub_type" => "#error",
"type" => "content"
}, 'HeaderLine' ),
bless( {
"cond" => [
[
1
]
],
"flat" => "#if 1",
"level" => 0,
"line" => "#if 1\n",
"n_lines" => 1,
"raw" => "#if 1\n",
"source" => "(buffer)",
"start_line_num" => 28,
"sub_type" => "#if",
"type" => "cond"
}, 'HeaderLine' ),
bless( {
"cond" => [
[
1
],
[
"CH == '\\\\' || CH == 'x'"
]
],
"flat" => "#if CH == '\\\\' || CH == 'x'",
"level" => 1,
"line" => "# if CH == '\\\\' || CH == 'x'\n",
"n_lines" => 1,
"raw" => "#if CH == 'x' || CH == '\\\\'\n",
"source" => "(buffer)",
"start_line_num" => 29,
"sub_type" => "#if",
"type" => "cond"
}, 'HeaderLine' ),
bless( {
"cond" => [
[
1
],
[
"CH == '\\\\' || CH == 'x'"
]
],
"flat" => "#define CH_IS_IT",
"level" => 2,
"line" => "# define CH_IS_IT\n",
"n_lines" => 1,
"raw" => "#define CH_IS_IT\n",
"source" => "(buffer)",
"start_line_num" => 30,
"sub_type" => "#define",
"type" => "content"
}, 'HeaderLine' ),
bless( {
"cond" => [
[
1
],
[
"CH == '\\\\' || CH == 'x'"
]
],
"flat" => "#endif",
"inner_lines" => 2,
"level" => 1,
"line" => "# endif\n",
"n_lines" => 1,
"raw" => "#endif\n",
"source" => "(buffer)",
"start_line_num" => 31,
"sub_type" => "#endif",
"type" => "cond"
}, 'HeaderLine' ),
bless( {
"cond" => [
[
1
]
],
"flat" => "#endif",
"inner_lines" => 4,
"level" => 0,
"line" => "#endif\n",
"n_lines" => 1,
"raw" => "#endif\n",
"source" => "(buffer)",
"start_line_num" => 32,
"sub_type" => "#endif",
"type" => "cond"
}, 'HeaderLine' )
];
DUMP_EOF
Expand Down Expand Up @@ -383,6 +479,11 @@ is($normal,<<~'EOF',"Normalized text as expected");
#error #pragma
#error #include
#error #define
#if 1
# if CH == '\\' || CH == 'x'
# define CH_IS_IT
# endif
#endif
EOF

{
Expand Down

0 comments on commit 15bf1b6

Please sign in to comment.