add support for special characters in strings: vertical tab, null

This commit is contained in:
Pekka Laiho 2020-12-21 09:13:27 +07:00
parent 7049d54427
commit c39064a682
4 changed files with 13 additions and 7 deletions

View File

@ -53,6 +53,8 @@ class Printer
$a = str_replace("\n", "\\n", $a); $a = str_replace("\n", "\\n", $a);
$a = str_replace("\r", "\\r", $a); $a = str_replace("\r", "\\r", $a);
$a = str_replace("\t", "\\t", $a); $a = str_replace("\t", "\\t", $a);
$a = str_replace("\v", "\\v", $a);
$a = str_replace("\0", "\\0", $a);
$a = str_replace("\"", "\\\"", $a); $a = str_replace("\"", "\\\"", $a);
return '"' . $a . '"'; return '"' . $a . '"';
} else { } else {

View File

@ -48,6 +48,10 @@ class Tokenizer
$current .= "\r"; $current .= "\r";
} elseif ($c == 't') { } elseif ($c == 't') {
$current .= "\t"; $current .= "\t";
} elseif ($c == 'v') {
$current .= "\v";
} elseif ($c == '0') {
$current .= "\0";
} elseif ($c == "\\" || $c == '"') { } elseif ($c == "\\" || $c == '"') {
$current .= $c; $current .= $c;
} else { } else {
@ -81,7 +85,7 @@ class Tokenizer
// Start of comment // Start of comment
$addCurrent(); $addCurrent();
$isComment = true; $isComment = true;
} elseif ($c == ' ' || $c == "\t" || $c == "\n" || $c == "\r" || $c == ':') { } elseif ($c == ' ' || $c == "\t" || $c == "\n" || $c == "\r" || $c == "\v" || $c == "\0" || $c == ':') {
// Whitespace and colon are ignored // Whitespace and colon are ignored
$addCurrent(); $addCurrent();
} elseif ($c == '(' || $c == '[' || $c == '{') { } elseif ($c == '(' || $c == '[' || $c == '{') {

View File

@ -40,7 +40,7 @@ class PrinterTest extends TestCase
// Test strings // Test strings
['abc', 'abc'], ['abc', 'abc'],
["a\\b\nc\rd\te\"f", "a\\b\nc\rd\te\"f"], ["a\\b\nc\rd\te\"f\vg\0h", "a\\b\nc\rd\te\"f\vg\0h"],
]; ];
} }
@ -77,7 +77,7 @@ class PrinterTest extends TestCase
// Test strings // Test strings
['abc', '"abc"'], ['abc', '"abc"'],
["a\\b\nc\rd\te\"f", "\"a\\\\b\\nc\\rd\\te\\\"f\""], ["a\\b\nc\rd\te\"f\vg\0h", "\"a\\\\b\\nc\\rd\\te\\\"f\\vg\\0h\""],
]; ];
} }

View File

@ -58,8 +58,8 @@ class TokenizerTest extends TestCase
["\n", []], ["\n", []],
["\r", []], ["\r", []],
[":", []], [":", []],
[" \t\n\r: ", []], [" \t\n\r\v\0: ", []],
[" aa\t\n\rbb:\r\ncc\t ", ['aa', 'bb', 'cc']], [" aa\t\n\rbb:\r\ncc\t\v\0dd ", ['aa', 'bb', 'cc', 'dd']],
// Comments // Comments
[";comment", []], [";comment", []],
@ -93,8 +93,8 @@ class TokenizerTest extends TestCase
// Test everything together // Test everything together
[ [
"(abc<+=-_!?>\"str\n\\r;\\\"\";com\"ment\r{\"a\":\"b\"})", "(abc<+=-_!?>\"str\n\\r;\v\\\"\";com\"me\0nt\r{\"a\":\"b\"})",
['(', 'abc<+=-_!?>', "\"str\n\r;\"\"", '{', '"a"', '"b"', '}', ')'] ['(', 'abc<+=-_!?>', "\"str\n\r;\v\"\"", '{', '"a"', '"b"', '}', ')']
], ],
]; ];
} }