Comparing version 1.3.1 to 1.3.2
@@ -104,3 +104,5 @@ "use strict"; | ||
this._comments = !!options.comments; | ||
this._comments = !!options.comments; // Cache the last tested closing position of long literals | ||
this._literalClosingPos = 0; | ||
} // ## Private methods | ||
@@ -133,3 +135,3 @@ // ### `_tokenizeToEnd` tokenizes as for as possible, emitting tokens through the callback | ||
if (whiteSpaceMatch = this._whitespace.exec(input)) input = input.substr(whiteSpaceMatch[0].length, input.length); // Stop for now if we're at the end | ||
if (!whiteSpaceMatch && (whiteSpaceMatch = this._whitespace.exec(input))) input = input.substr(whiteSpaceMatch[0].length, input.length); // Stop for now if we're at the end | ||
@@ -416,30 +418,37 @@ if (this._endOfFile.test(input)) { | ||
_parseLiteral(input) { | ||
// Identify the opening quote(s) | ||
const opening = input.match(/^(?:"""|"|'''|'|)/)[0]; | ||
const openingLength = opening.length; // Find the next candidate closing quotes | ||
// Ensure we have enough lookahead to identify triple-quoted strings | ||
if (input.length >= 3) { | ||
// Identify the opening quote(s) | ||
const opening = input.match(/^(?:"""|"|'''|'|)/)[0]; | ||
const openingLength = opening.length; // Find the next candidate closing quotes | ||
let closingPos = openingLength; | ||
let closingPos = Math.max(this._literalClosingPos, openingLength); | ||
while ((closingPos = input.indexOf(opening, closingPos)) > 0) { | ||
// Count backslashes right before the closing quotes | ||
let backslashCount = 0; | ||
while ((closingPos = input.indexOf(opening, closingPos)) > 0) { | ||
// Count backslashes right before the closing quotes | ||
let backslashCount = 0; | ||
while (input[closingPos - backslashCount - 1] === '\\') backslashCount++; // An even number of backslashes (in particular 0) | ||
// means these are actual, non-escaped closing quotes | ||
while (input[closingPos - backslashCount - 1] === '\\') backslashCount++; // An even number of backslashes (in particular 0) | ||
// means these are actual, non-escaped closing quotes | ||
if (backslashCount % 2 === 0) { | ||
// Extract and unescape the value | ||
const raw = input.substring(openingLength, closingPos); | ||
const lines = raw.split(/\r\n|\r|\n/).length - 1; // Only triple-quoted strings can be multi-line | ||
if (backslashCount % 2 === 0) { | ||
// Extract and unescape the value | ||
const raw = input.substring(openingLength, closingPos); | ||
const lines = raw.split(/\r\n|\r|\n/).length - 1; | ||
const matchLength = closingPos + openingLength; // Only triple-quoted strings can be multi-line | ||
if (openingLength === 1 && lines !== 0 || openingLength === 3 && this._lineMode) break; | ||
this._line += lines; | ||
return { | ||
value: this._unescape(raw), | ||
matchLength: closingPos + openingLength | ||
}; | ||
if (openingLength === 1 && lines !== 0 || openingLength === 3 && this._lineMode) break; | ||
this._line += lines; | ||
this._literalClosingPos = 0; | ||
return { | ||
value: this._unescape(raw), | ||
matchLength | ||
}; | ||
} | ||
closingPos++; | ||
} | ||
closingPos++; | ||
this._literalClosingPos = input.length - openingLength + 1; | ||
} | ||
@@ -446,0 +455,0 @@ |
{ | ||
"name": "n3", | ||
"version": "1.3.1", | ||
"version": "1.3.2", | ||
"description": "Lightning fast, asynchronous, streaming Turtle / N3 / RDF library.", | ||
@@ -5,0 +5,0 @@ "author": "Ruben Verborgh <ruben.verborgh@gmail.com>", |
@@ -72,2 +72,4 @@ // **N3Lexer** tokenizes N3 documents. | ||
this._comments = !!options.comments; | ||
// Cache the last tested closing position of long literals | ||
this._literalClosingPos = 0; | ||
} | ||
@@ -93,3 +95,3 @@ | ||
// Skip whitespace on current line | ||
if (whiteSpaceMatch = this._whitespace.exec(input)) | ||
if (!whiteSpaceMatch && (whiteSpaceMatch = this._whitespace.exec(input))) | ||
input = input.substr(whiteSpaceMatch[0].length, input.length); | ||
@@ -376,28 +378,34 @@ | ||
_parseLiteral(input) { | ||
// Identify the opening quote(s) | ||
const opening = input.match(/^(?:"""|"|'''|'|)/)[0]; | ||
const openingLength = opening.length; | ||
// Ensure we have enough lookahead to identify triple-quoted strings | ||
if (input.length >= 3) { | ||
// Identify the opening quote(s) | ||
const opening = input.match(/^(?:"""|"|'''|'|)/)[0]; | ||
const openingLength = opening.length; | ||
// Find the next candidate closing quotes | ||
let closingPos = openingLength; | ||
while ((closingPos = input.indexOf(opening, closingPos)) > 0) { | ||
// Count backslashes right before the closing quotes | ||
let backslashCount = 0; | ||
while (input[closingPos - backslashCount - 1] === '\\') | ||
backslashCount++; | ||
// Find the next candidate closing quotes | ||
let closingPos = Math.max(this._literalClosingPos, openingLength); | ||
while ((closingPos = input.indexOf(opening, closingPos)) > 0) { | ||
// Count backslashes right before the closing quotes | ||
let backslashCount = 0; | ||
while (input[closingPos - backslashCount - 1] === '\\') | ||
backslashCount++; | ||
// An even number of backslashes (in particular 0) | ||
// means these are actual, non-escaped closing quotes | ||
if (backslashCount % 2 === 0) { | ||
// Extract and unescape the value | ||
const raw = input.substring(openingLength, closingPos); | ||
const lines = raw.split(/\r\n|\r|\n/).length - 1; | ||
// Only triple-quoted strings can be multi-line | ||
if (openingLength === 1 && lines !== 0 || | ||
openingLength === 3 && this._lineMode) | ||
break; | ||
this._line += lines; | ||
return { value: this._unescape(raw), matchLength: closingPos + openingLength }; | ||
// An even number of backslashes (in particular 0) | ||
// means these are actual, non-escaped closing quotes | ||
if (backslashCount % 2 === 0) { | ||
// Extract and unescape the value | ||
const raw = input.substring(openingLength, closingPos); | ||
const lines = raw.split(/\r\n|\r|\n/).length - 1; | ||
const matchLength = closingPos + openingLength; | ||
// Only triple-quoted strings can be multi-line | ||
if (openingLength === 1 && lines !== 0 || | ||
openingLength === 3 && this._lineMode) | ||
break; | ||
this._line += lines; | ||
this._literalClosingPos = 0; | ||
return { value: this._unescape(raw), matchLength }; | ||
} | ||
closingPos++; | ||
} | ||
closingPos++; | ||
this._literalClosingPos = input.length - openingLength + 1; | ||
} | ||
@@ -404,0 +412,0 @@ return { value: '', matchLength: 0 }; |
247069
5331