Skip to content

Various fixes for parsing string and char literals #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,14 @@ module.exports = grammar({

character_literal: _ => token(seq(
'\'',
// this accepts multiple characters while java doesn't
// hence multiple characters (even unicode ones) are allowed
repeat1(choice(
/[^\\'\n]/,
/\\./,
/\\\n/,
/\\u+005[cC]./,
/\\u+005[cC]\n/,
)),
'\'',
)),
Expand Down Expand Up @@ -213,12 +217,12 @@ module.exports = grammar({
prec(1, $.escape_sequence),
),
escape_sequence: _ => token.immediate(seq(
'\\',
choice('\\', /\\u+005[cC]/),
choice(
/[^xu0-7]/,
/[0-7]{1,3}/,
/x[0-9a-fA-F]{2}/,
/u[0-9a-fA-F]{4}/,
/u+[0-9a-fA-F]{4}/,
/u\{[0-9a-fA-F]+\}/,
))),

Expand Down
14 changes: 13 additions & 1 deletion test/corpus/literals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ character literals
'\uFFFF';
'\177';
'™';
'\u005cn';
'\u005c'';

---

Expand All @@ -137,6 +139,8 @@ character literals
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal))
(expression_statement (character_literal)))

===============
Expand All @@ -147,6 +151,8 @@ string literals
"\"";
"This is a string";
"'";
"\uuu0041";
"\u005c"";

---

Expand All @@ -161,7 +167,13 @@ string literals
(string_fragment)))
(expression_statement
(string_literal
(string_fragment))))
(string_fragment)))
(expression_statement
(string_literal
(escape_sequence)))
(expression_statement
(string_literal
(escape_sequence))))

===============
text block
Expand Down