Skip to content

Commit

Permalink
Fix handling of unfinished plain string literals
Browse files Browse the repository at this point in the history
Previously, unfinished string literals consisting only of the opening
quotation mark would not raise an error.
  • Loading branch information
fstirlitz committed Oct 24, 2019
1 parent 84e7a67 commit d49b641
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 8 deletions.
14 changes: 7 additions & 7 deletions luaparse.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,18 +829,18 @@
, string = ''
, charCode;

while (index < length) {
for (;;) {
charCode = input.charCodeAt(index++);
if (delimiter === charCode) break;
if (92 === charCode) { // backslash
string += fixupHighCharacters(input.slice(stringStart, index - 1)) + readEscapeSequence();
stringStart = index;
}
// EOF or `\n` terminates a string literal. If we haven't found the
// ending delimiter by now, raise an exception.
else if (index >= length || isLineTerminator(charCode)) {
if (index > length || isLineTerminator(charCode)) {
string += input.slice(stringStart, index - 1);
raise(null, errors.unfinishedString, string + String.fromCharCode(charCode));
raise(null, errors.unfinishedString, String.fromCharCode(delimiter) + string);
}
if (92 === charCode) { // backslash
string += fixupHighCharacters(input.slice(stringStart, index - 1)) + readEscapeSequence();
stringStart = index;
}
}
string += fixupHighCharacters(input.slice(stringStart, index - 1));
Expand Down
8 changes: 8 additions & 0 deletions test/scaffolding/literals
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ a = 0xfp-1
a = 0xFP+9
a = 1 .. 3 .. -2
a = 1 .. "bar"
a = " -- FAIL
a = "' -- FAIL
a = '" -- FAIL
a = "@n@" -- FAIL
a = ' -- FAIL
a = '@n@' -- FAIL
a = [[ -- FAIL
a = [[@n@]]
a = "bar -- FAIL
a = 'bar'
a = "bar"
Expand Down
133 changes: 132 additions & 1 deletion test/spec/literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -1913,9 +1913,140 @@
]
}
},
{
"source": "a = \"",
"result": "[1:7] unfinished string near '\"'"
},
{
"source": "a = \"'",
"result": "[1:8] unfinished string near '\"''"
},
{
"source": "a = '\"",
"result": "[1:8] unfinished string near ''\"'"
},
{
"source": "a = \"\n\"",
"result": "[1:7] unfinished string near '\"'"
},
{
"source": "a = '",
"result": "[1:7] unfinished string near '''"
},
{
"source": "a = '\n'",
"result": "[1:7] unfinished string near '''"
},
{
"source": "a = [[",
"result": "[1:7] unfinished long string (starting at line 1) near '<eof>'"
},
{
"source": "a = [[\n]]",
"result": {
"type": "Chunk",
"body": [
{
"type": "AssignmentStatement",
"variables": [
{
"type": "Identifier",
"name": "a",
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
}
},
"range": [
0,
1
],
"isLocal": false
}
],
"init": [
{
"type": "StringLiteral",
"value": "",
"raw": "[[\n]]",
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 2,
"column": 2
}
},
"range": [
4,
9
]
}
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 2
}
},
"range": [
0,
9
]
}
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 2
}
},
"range": [
0,
9
],
"comments": [],
"globals": [
{
"type": "Identifier",
"name": "a",
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
}
},
"range": [
0,
1
],
"isLocal": false
}
]
}
},
{
"source": "a = \"bar",
"result": "[1:9] unfinished string near 'bar'"
"result": "[1:10] unfinished string near '\"bar'"
},
{
"source": "a = 'bar'",
Expand Down

0 comments on commit d49b641

Please sign in to comment.