Skip to content

Commit ad3b318

Browse files
committed
Only allow newlines in triple-quoted literals.
Fixes #114.
1 parent e67f679 commit ad3b318

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/N3Lexer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ N3Lexer.prototype = {
4949

5050
_iri: /^<((?:[^ <>{}\\]|\\[uU])+)>[ \t]*/, // IRI with escape sequences; needs sanity check after unescaping
5151
_unescapedIri: /^<([^\x00-\x20<>\\"\{\}\|\^\`]*)>[ \t]*/, // IRI without escape sequences; no unescaping
52-
_unescapedString: /^"[^"\\]+"(?=[^"\\])/, // non-empty string without escape sequences
53-
_singleQuotedString: /^"[^"\\]*(?:\\.[^"\\]*)*"(?=[^"\\])|^'[^'\\]*(?:\\.[^'\\]*)*'(?=[^'\\])/,
52+
_unescapedString: /^"[^"\\\r\n]+"/, // non-empty string without escape sequences
53+
_singleQuotedString: /^"(?:[^"\\\r\n]|\\.)*"(?=[^"])|^'(?:[^'\\\r\n]|\\.)*'(?=[^'])/,
5454
_tripleQuotedString: /^""("[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*")""|^''('[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*')''/,
5555
_langcode: /^@([a-z]+(?:-[a-z0-9]+)*)(?=[^a-z0-9\-])/i,
5656
_prefix: /^((?:[A-Za-z\xc0-\xd6\xd8-\xf6\xf8-\u02ff\u0370-\u037d\u037f-\u1fff\u200c\u200d\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd]|[\ud800-\udb7f][\udc00-\udfff])(?:\.?[\-0-9A-Z_a-z\xb7\xc0-\xd6\xd8-\xf6\xf8-\u037d\u037f-\u1fff\u200c\u200d\u203f\u2040\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd]|[\ud800-\udb7f][\udc00-\udfff])*)?:(?=[#\s<])/,

test/N3Lexer-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,14 @@ describe('N3Lexer', function () {
265265
{ type: 'literal', value: '"string"', line: 1 },
266266
{ type: 'eof', line: 1 }));
267267

268+
it('should not tokenize a quoted string literal with a newline',
269+
shouldNotTokenize('"abc\ndef" ',
270+
'Unexpected ""abc" on line 1.'));
271+
272+
it('should not tokenize a quoted string literal with a carriage return',
273+
shouldNotTokenize('"abc\rdef" ',
274+
'Unexpected ""abc" on line 1.'));
275+
268276
it('should tokenize a triple quoted string literal',
269277
shouldTokenize('"""string"""',
270278
{ type: 'literal', value: '"string"', line: 1 },
@@ -325,6 +333,14 @@ describe('N3Lexer', function () {
325333
{ type: 'literal', value: '"string"', line: 1 },
326334
{ type: 'eof', line: 1 }));
327335

336+
it('should not tokenize a single-quoted string literal with a newline',
337+
shouldNotTokenize("'abc\ndef' ",
338+
'Unexpected "\'abc" on line 1.'));
339+
340+
it('should not tokenize a single-quoted string literal with a carriage return',
341+
shouldNotTokenize("'abc\rdef' ",
342+
'Unexpected "\'abc" on line 1.'));
343+
328344
it('should tokenize a triple single-quoted string literal with quotes newlines inside',
329345
shouldTokenize("'''st'r\ni''ng'''",
330346
{ type: 'literal', value: '"st\'r\ni\'\'ng"', line: 1 },

0 commit comments

Comments
 (0)