diff --git a/packages/java-parser/src/tokens.js b/packages/java-parser/src/tokens.js index c12ff761..89a10e8b 100644 --- a/packages/java-parser/src/tokens.js +++ b/packages/java-parser/src/tokens.js @@ -49,7 +49,7 @@ FRAGMENT( "OctalEscape", "\\\\({{OctalDigit}}|{{ZeroToThree}}?{{OctalDigit}}{2})" ); -FRAGMENT("EscapeSequence", "\\\\[btnfr\"'\\\\]|{{OctalEscape}}"); +FRAGMENT("EscapeSequence", "\\\\[bstnfr\"'\\\\]|{{OctalEscape}}"); // Not using InputCharacter terminology there because CR and LF are already captured in EscapeSequence FRAGMENT( "StringCharacter", diff --git a/packages/java-parser/test/string-literals.spec.js b/packages/java-parser/test/string-literals.spec.js new file mode 100644 index 00000000..6d0c86d9 --- /dev/null +++ b/packages/java-parser/test/string-literals.spec.js @@ -0,0 +1,43 @@ +"use strict"; + +const { expect } = require("chai"); +const javaParser = require("../src/index"); + +describe("String literals", () => { + it("should parse unicode", () => { + const inputs = [ + '"π or \\u03c0"', + '"\\uD83C\\uDF4F"', + '"\\uD83C\\uDF4C"', + '"\\uD83C\\uDF52"', + '"🍎"' + ]; + inputs.forEach(input => { + expect(() => javaParser.parse(input, "literal")).to.not.throw(); + }); + }); + + it("should parse octal literals", () => { + const inputs = ['"\\52"', '"\\133"']; + inputs.forEach(input => { + expect(() => javaParser.parse(input, "literal")).to.not.throw(); + }); + }); + + it("should parse escaped sequence", () => { + const inputs = [ + '"\\b"', + '"\\s"', + '"\\t"', + '"\\n"', + '"\\f"', + '"\\r"', + '"\\""', + '"\\\'"', + '"\\\\"' + ]; + inputs.forEach(input => { + expect(() => javaParser.parse(input, "literal")).to.not.throw(); + }); + }); +});