coffee-lex
Advanced tools
Comparing version 9.2.1 to 9.3.0
@@ -261,2 +261,32 @@ /** | ||
declare enum ContextType { | ||
STRING = "STRING", | ||
INTERPOLATION = "INTERPOLATION", | ||
CSX_OPEN_TAG = "CSX_OPEN_TAG", | ||
CSX_CLOSE_TAG = "CSX_CLOSE_TAG", | ||
CSX_BODY = "CSX_BODY", | ||
BRACE = "BRACE", | ||
PAREN = "PAREN" | ||
} | ||
declare type Context = { | ||
readonly type: ContextType.STRING; | ||
readonly allowComments: boolean; | ||
readonly allowInterpolations: boolean; | ||
readonly endingDelimiter: string; | ||
readonly endSourceType: SourceType; | ||
} | { | ||
readonly type: ContextType.INTERPOLATION; | ||
readonly interpolationType: SourceType; | ||
} | { | ||
readonly type: ContextType.CSX_OPEN_TAG; | ||
} | { | ||
readonly type: ContextType.CSX_CLOSE_TAG; | ||
} | { | ||
readonly type: ContextType.CSX_BODY; | ||
} | { | ||
readonly type: ContextType.BRACE; | ||
} | { | ||
readonly type: ContextType.PAREN; | ||
readonly sourceType: SourceType; | ||
}; | ||
interface Options { | ||
@@ -267,2 +297,10 @@ readonly useCS2: boolean; | ||
/** | ||
* A lexer error at a specific location. | ||
*/ | ||
declare class CoffeeLexError extends SyntaxError { | ||
readonly index: number; | ||
readonly context?: Context | undefined; | ||
constructor(message: string, index: number, context?: Context | undefined); | ||
} | ||
/** | ||
* Generate a list of tokens from CoffeeScript source code. | ||
@@ -277,2 +315,2 @@ */ | ||
export { DEFAULT_OPTIONS, Options, SourceLocation, SourceToken, SourceTokenList, SourceTokenListIndex, SourceTokenListIndexRange, SourceType, consumeStream, lex as default, lex, stream }; | ||
export { CoffeeLexError, DEFAULT_OPTIONS, Options, SourceLocation, SourceToken, SourceTokenList, SourceTokenListIndex, SourceTokenListIndexRange, SourceType, consumeStream, lex as default, lex, stream }; |
@@ -22,2 +22,3 @@ var __defProp = Object.defineProperty; | ||
__export(src_exports, { | ||
CoffeeLexError: () => CoffeeLexError, | ||
DEFAULT_OPTIONS: () => DEFAULT_OPTIONS, | ||
@@ -44,2 +45,12 @@ SourceLocation: () => SourceLocation, | ||
// src/utils/assert.ts | ||
function assert(condition, message = `expected '${condition}' to be truthy`) { | ||
if (!condition) { | ||
throw new Error(message); | ||
} | ||
} | ||
function assertNever(value, message = `unexpected value: ${value}`) { | ||
throw new Error(message); | ||
} | ||
// src/SourceToken.ts | ||
@@ -51,5 +62,3 @@ var SourceToken = class { | ||
this.end = end; | ||
if (start > end) { | ||
throw new Error(`Token start may not be after end. Got ${type}, ${start}, ${end}`); | ||
} | ||
assert(start <= end, `Token start may not be after end. Got ${type}, ${start}, ${end}`); | ||
} | ||
@@ -87,5 +96,3 @@ }; | ||
distance(other) { | ||
if (other._sourceTokenList !== this._sourceTokenList) { | ||
throw new Error("cannot compare indexes from different lists"); | ||
} | ||
assert(other._sourceTokenList === this._sourceTokenList, "cannot compare indexes from different lists"); | ||
return other._index - this._index; | ||
@@ -228,5 +235,3 @@ } | ||
slice(start, end) { | ||
if (start["_sourceTokenList"] !== this || end["_sourceTokenList"] !== this) { | ||
throw new Error("cannot slice a list using indexes from another list"); | ||
} | ||
assert(start["_sourceTokenList"] !== this || end["_sourceTokenList"] !== this, "cannot slice a list using indexes from another list"); | ||
return new SourceTokenList(this._tokens.slice(start["_index"], end["_index"])); | ||
@@ -404,5 +409,3 @@ } | ||
const nextIndex = index.next(); | ||
if (!nextIndex) { | ||
throw new Error(`unexpected null index before the end index`); | ||
} | ||
assert(nextIndex, `unexpected null index before the end index`); | ||
index = nextIndex; | ||
@@ -416,22 +419,12 @@ return result; | ||
for (let i = 0; i < tokens.length - 1; i++) { | ||
if (tokens[i].end > tokens[i + 1].start) { | ||
throw new Error(`Tokens not in order. Expected ${JSON.stringify(tokens[i])} before ${JSON.stringify(tokens[i + 1])}`); | ||
} | ||
assert(tokens[i].end <= tokens[i + 1].start, `Tokens not in order. Expected ${JSON.stringify(tokens[i])} before ${JSON.stringify(tokens[i + 1])}`); | ||
} | ||
} | ||
_validateIndex(index) { | ||
if (!index) { | ||
throw new Error(`unexpected 'null' index, perhaps you forgot to check the result of 'indexOfTokenContainingSourceIndex'?`); | ||
} | ||
if (typeof index === "number") { | ||
throw new Error(`to get a token at index ${index}, use list.tokenAtIndex(list.startIndex.advance(${index}))`); | ||
} | ||
if (index["_sourceTokenList"] !== this) { | ||
throw new Error("cannot get token in one list using an index from another"); | ||
} | ||
assert(index, `unexpected 'null' index, perhaps you forgot to check the result of 'indexOfTokenContainingSourceIndex'?`); | ||
assert(typeof index !== "number", `to get a token at index ${index}, use list.tokenAtIndex(list.startIndex.advance(${index}))`); | ||
assert(index["_sourceTokenList"] === this, "cannot get token in one list using an index from another"); | ||
} | ||
_validateSourceIndex(index) { | ||
if (typeof index !== "number") { | ||
throw new Error(`expected source index to be a number, got: ${index}`); | ||
} | ||
assert(typeof index === "number", `expected source index to be a number, got: ${index}`); | ||
} | ||
@@ -451,7 +444,2 @@ _getIndex(index) { | ||
// src/utils/assertNever.ts | ||
function assertNever(value, message = `unexpected value: ${value}`) { | ||
throw new Error(message); | ||
} | ||
// src/utils/BufferedStream.ts | ||
@@ -521,5 +509,3 @@ var BufferedStream = class { | ||
} | ||
if (rangeIndex !== this.fragments.length) { | ||
throw new Error("Expected ranges to correspond to original locations."); | ||
} | ||
assert(rangeIndex === this.fragments.length, "Expected ranges to correspond to original locations."); | ||
return resultLocations; | ||
@@ -575,5 +561,3 @@ } | ||
} | ||
if (paddingDepth < 0 || lineSeparatorDepth < 0 || paddingDepth > 0 && lineSeparatorDepth > 0) { | ||
throw new Error(`Illegal padding state: paddingDepth: ${paddingDepth}, lineSeparatorDepth: ${lineSeparatorDepth}`); | ||
} | ||
assert(paddingDepth >= 0 && lineSeparatorDepth >= 0 && (paddingDepth <= 0 || lineSeparatorDepth <= 0), `Illegal padding state: paddingDepth: ${paddingDepth}, lineSeparatorDepth: ${lineSeparatorDepth}`); | ||
let sourceType; | ||
@@ -827,2 +811,9 @@ if (paddingDepth > 0) { | ||
}; | ||
var CoffeeLexError = class extends SyntaxError { | ||
constructor(message, index, context) { | ||
super(message); | ||
this.index = index; | ||
this.context = context; | ||
} | ||
}; | ||
function lex(source, options = DEFAULT_OPTIONS) { | ||
@@ -986,2 +977,5 @@ let location; | ||
} | ||
function lexError(message) { | ||
throw new CoffeeLexError(`${message} at ${index}`, index, currentContext() ?? void 0); | ||
} | ||
return function step() { | ||
@@ -1138,3 +1132,3 @@ const lastLocation = location; | ||
if (!context || context.type !== "PAREN" /* PAREN */) { | ||
throw new Error(`unexpected ')' at ${start}`); | ||
throw lexError(`unexpected ')'`); | ||
} | ||
@@ -1150,3 +1144,3 @@ const { sourceType } = context; | ||
default: | ||
throw new Error(`unexpected token type for '(' matching ')' at ${start}: ${sourceType ? sourceType.toString() : "??"}`); | ||
throw lexError(`unexpected token type for '(' matching ')': ${sourceType ? sourceType.toString() : "??"}`); | ||
} | ||
@@ -1167,3 +1161,3 @@ } else if (consume("[")) { | ||
} else { | ||
throw new Error(`Unexpected context type: ${currentContextType()}`); | ||
throw lexError(`Unexpected context type: ${currentContextType()}`); | ||
} | ||
@@ -1354,3 +1348,3 @@ } else if (consumeCSXOpenTagStart()) { | ||
if (!context || context.type !== "STRING" /* STRING */) { | ||
throw new Error("Unexpected STRING_CONTENT without anything on the string stack."); | ||
throw lexError("Unexpected STRING_CONTENT without anything on the string stack."); | ||
} | ||
@@ -1401,3 +1395,3 @@ const { | ||
if (!context || context.type !== "INTERPOLATION" /* INTERPOLATION */) { | ||
throw new Error(`found interpolation end without any interpolation start`); | ||
throw lexError(`found interpolation end without any interpolation start`); | ||
} | ||
@@ -1460,3 +1454,3 @@ setType(context.interpolationType); | ||
if (context !== null) { | ||
throw new Error(`unexpected EOF while in context ${context.type}`); | ||
throw lexError(`unexpected EOF while in context ${context.type}`); | ||
} | ||
@@ -1470,3 +1464,3 @@ break; | ||
case "STRING_PADDING" /* STRING_PADDING */: | ||
throw new Error(`unexpected source type at offset ${location.index}: ${location.type}`); | ||
throw lexError(`unexpected source type at offset ${location.index}: ${location.type}`); | ||
default: | ||
@@ -1514,3 +1508,3 @@ assertNever(location.type, `unexpected source type at offset ${location.index}: ${location.type}`); | ||
if (!closed) { | ||
throw new Error("missing / (unclosed regex)"); | ||
throw lexError("missing / (unclosed regex)"); | ||
} | ||
@@ -1564,3 +1558,3 @@ index += regex.length; | ||
if (currentContextType() !== "INTERPOLATION" /* INTERPOLATION */) { | ||
throw new Error(`unexpected '}' found in string at ${index}: ${JSON.stringify(source)}`); | ||
throw lexError(`unexpected '}' found in string at ${index}: ${JSON.stringify(source)}`); | ||
} | ||
@@ -1581,2 +1575,3 @@ setType("INTERPOLATION_END" /* INTERPOLATION_END */); | ||
0 && (module.exports = { | ||
CoffeeLexError, | ||
DEFAULT_OPTIONS, | ||
@@ -1583,0 +1578,0 @@ SourceLocation, |
{ | ||
"name": "coffee-lex", | ||
"version": "9.2.1", | ||
"version": "9.3.0", | ||
"description": "Stupid lexer for CoffeeScript.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
121537
3374