Skip to content

Commit 04405f6

Browse files
Fix the bug where a ; or // (start-of-comment characters) in a string would cause the rest of the line to be treated as a comment and would create a parse error.
Fixes #70.
1 parent 33f68ff commit 04405f6

File tree

4 files changed

+120
-15
lines changed

4 files changed

+120
-15
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# v0.13.3
2+
3+
## Bug Fixes
4+
5+
* ([#70](https://github.com/harrisont/fastbuild-vscode/issues/70)) Fix the bug where a `;` or `//` (start-of-comment characters) in a string would cause the rest of the line to be treated as a comment and would create a parse error.
6+
17
# v0.13.2
28

39
## Bug Fixes

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "fastbuild-support",
33
"displayName": "FASTBuild Support",
44
"description": "FASTBuild language support. Includes go-to definition, find references, variable evaluation, syntax errors, etc.",
5-
"version": "0.13.2",
5+
"version": "0.13.3",
66
"preview": true,
77
"publisher": "HarrisonT",
88
"author": {

server/src/parser.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -343,23 +343,30 @@ function removeComments(input: string): string {
343343
for (const line of lines) {
344344
let inSingleQuotedString = false;
345345
let inDoubleQuotedString = false;
346+
let escapeNextChar = false;
346347
let lineWithCommentRemoved = line;
347348

348349
for (let i = 0, lastNonSpaceIndex = -1; i < line.length; ++i) {
349350
const char = line[i];
350351

351-
if (char === "'" && !inDoubleQuotedString) {
352-
inSingleQuotedString = !inSingleQuotedString;
353-
} else if (char === '"' && !inSingleQuotedString) {
354-
inDoubleQuotedString = !inDoubleQuotedString;
355-
} else if (!inSingleQuotedString
356-
&& !inDoubleQuotedString
357-
&& ((char === ';')
358-
|| ((char === '/') && (i + 1 < line.length) && (line[i + 1] === '/'))))
359-
{
360-
// Comment. Strip the rest of the line.
361-
lineWithCommentRemoved = line.substring(0, lastNonSpaceIndex + 1);
362-
break;
352+
if (char === '^') {
353+
escapeNextChar = true;
354+
} else if (escapeNextChar) {
355+
escapeNextChar = false;
356+
} else {
357+
if (char === "'" && !inDoubleQuotedString) {
358+
inSingleQuotedString = !inSingleQuotedString;
359+
} else if (char === '"' && !inSingleQuotedString) {
360+
inDoubleQuotedString = !inDoubleQuotedString;
361+
} else if (!inSingleQuotedString
362+
&& !inDoubleQuotedString
363+
&& ((char === ';')
364+
|| ((char === '/') && (i + 1 < line.length) && (line[i + 1] === '/'))))
365+
{
366+
// Comment. Strip the rest of the line.
367+
lineWithCommentRemoved = line.substring(0, lastNonSpaceIndex + 1);
368+
break;
369+
}
363370
}
364371

365372
if (char !== ' ') {
@@ -369,4 +376,4 @@ function removeComments(input: string): string {
369376
modifiedLines.push(lineWithCommentRemoved);
370377
}
371378
return modifiedLines.join('\n');
372-
}
379+
}

server/src/test/1-parser.test.ts

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,52 @@ describe('parser', () => {
416416
]);
417417
});
418418

419+
it('should work on assigning a string literal with single quotes with an escaped single quote and a comment character (";") inside', () => {
420+
const input = `.MyVar = '^';'`;
421+
assertParseResultsEqual(input, [
422+
{
423+
type: 'variableDefinition',
424+
lhs: {
425+
name: {
426+
type: 'string',
427+
value: 'MyVar',
428+
range: createRange(0, 1, 0, 6)
429+
},
430+
scope: 'current',
431+
range: createRange(0, 0, 0, 6),
432+
},
433+
rhs: {
434+
type: 'string',
435+
value: "';",
436+
range: createRange(0, 9, 0, 14)
437+
}
438+
}
439+
]);
440+
});
441+
442+
it('should work on assigning a string literal with single quotes with an escaped single quote and a comment character ("//") inside', () => {
443+
const input = `.MyVar = '^'//'`;
444+
assertParseResultsEqual(input, [
445+
{
446+
type: 'variableDefinition',
447+
lhs: {
448+
name: {
449+
type: 'string',
450+
value: 'MyVar',
451+
range: createRange(0, 1, 0, 6)
452+
},
453+
scope: 'current',
454+
range: createRange(0, 0, 0, 6),
455+
},
456+
rhs: {
457+
type: 'string',
458+
value: "'//",
459+
range: createRange(0, 9, 0, 15)
460+
}
461+
}
462+
]);
463+
});
464+
419465
it('should work on assigning a string literal with double quotes with an escaped double quote inside', () => {
420466
const input = `.MyVar = "h^"i"`;
421467
assertParseResultsEqual(input, [
@@ -439,6 +485,52 @@ describe('parser', () => {
439485
]);
440486
});
441487

488+
it('should work on assigning a string literal with double quotes with an escaped double quote and a comment character (";") inside', () => {
489+
const input = `.MyVar = "^";"`;
490+
assertParseResultsEqual(input, [
491+
{
492+
type: 'variableDefinition',
493+
lhs: {
494+
name: {
495+
type: 'string',
496+
value: 'MyVar',
497+
range: createRange(0, 1, 0, 6)
498+
},
499+
scope: 'current',
500+
range: createRange(0, 0, 0, 6),
501+
},
502+
rhs: {
503+
type: 'string',
504+
value: '";',
505+
range: createRange(0, 9, 0, 14)
506+
}
507+
}
508+
]);
509+
});
510+
511+
it('should work on assigning a string literal with double quotes with an escaped double quote and a comment character ("//") inside', () => {
512+
const input = `.MyVar = "^"//"`;
513+
assertParseResultsEqual(input, [
514+
{
515+
type: 'variableDefinition',
516+
lhs: {
517+
name: {
518+
type: 'string',
519+
value: 'MyVar',
520+
range: createRange(0, 1, 0, 6)
521+
},
522+
scope: 'current',
523+
range: createRange(0, 0, 0, 6),
524+
},
525+
rhs: {
526+
type: 'string',
527+
value: '"//',
528+
range: createRange(0, 9, 0, 15)
529+
}
530+
}
531+
]);
532+
});
533+
442534
it('should work on assigning a string literal with single quotes with a comment-symbol inside (;)', () => {
443535
const input = `.MyVar = 'h;i'`;
444536
assertParseResultsEqual(input, [
@@ -3050,4 +3142,4 @@ Expecting to see one of the following:
30503142
assertInputsGenerateSameParseResult(withComments, withoutComments);
30513143
});
30523144
});
3053-
});
3145+
});

0 commit comments

Comments
 (0)