Everywhere: Replace calls to console.assert and throws with assertions
This patch replaces console.assert calls and random throw new Error calls to crash on undefined behavior with predefined assertion functions from utils.
This commit is contained in:
parent
4d71efb394
commit
5afae11193
2 changed files with 20 additions and 35 deletions
|
@ -2,6 +2,7 @@ import { Palette } from "./highlighter/palette";
|
|||
import { Node, Position } from "./highlighter/node";
|
||||
import { State } from "./highlighter/state";
|
||||
import { Token, Type } from "./tokenizer/token";
|
||||
import { TODO, VERIFY, VERIFY_NOT_REACHED } from "../util/assertions.js";
|
||||
|
||||
export class Highlighter {
|
||||
private state: State = State.Undefined;
|
||||
|
@ -28,7 +29,7 @@ export class Highlighter {
|
|||
case Type.DOCTYPE: this.reconsumeIn(State.DOCTYPE); break;
|
||||
case Type.Comment: this.reconsumeIn(State.Comment); break;
|
||||
case Type.EndOfFile: this.finished = true; break;
|
||||
default: throw new Error(`FIXME (Highlighter#spin, Unimplemented token type '${this.currentToken.type}')`);
|
||||
default: TODO(`Highlighter#spin, Unimplemented token type '${this.currentToken.type}'`);
|
||||
}
|
||||
|
||||
break;
|
||||
|
@ -39,7 +40,7 @@ export class Highlighter {
|
|||
this.createNode({ position: { line: 0, character: 0 }, color: Palette.Plain, content: '' });
|
||||
this.reconsumeIn(State.Plain);
|
||||
break;
|
||||
default: throw new Error('BeforePlain')
|
||||
default: VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
break;
|
||||
|
@ -159,7 +160,7 @@ export class Highlighter {
|
|||
|
||||
this.state = State.Undefined;
|
||||
break;
|
||||
default: throw new Error(`FIXME (Highlighter#iterate, Unimplemented state '${this.state}')`);
|
||||
default: TODO(`Highlighter#iterate, Unimplemented state '${this.state}'`);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -178,9 +179,7 @@ export class Highlighter {
|
|||
private consumeNextTokenOfType<T extends Type>(type: T): Token & { type: T } {
|
||||
this.currentToken = this.tokens[this.pointer];
|
||||
|
||||
console.assert(this.currentToken.type === type, {
|
||||
message: `Highlighter#consumeNextOfType: Expected '${type}', got '${this.currentToken.type}' instead`
|
||||
});
|
||||
VERIFY(this.currentToken.type === type, `Highlighter#consumeNextOfType: Expected '${type}', got '${this.currentToken.type}' instead`);
|
||||
|
||||
this.pointer++;
|
||||
|
||||
|
@ -190,9 +189,8 @@ export class Highlighter {
|
|||
private consumeNextTokenOfEitherType<T extends Type, U extends Type>(a: T, b: U): Token & { type: T | U } {
|
||||
this.currentToken = this.tokens[this.pointer];
|
||||
|
||||
console.assert(this.currentToken.type === a || this.currentToken.type === b, {
|
||||
message: `Highlighter#consumeNextTokenOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`
|
||||
});
|
||||
VERIFY(this.currentToken.type === a || this.currentToken.type === b,
|
||||
`Highlighter#consumeNextTokenOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`);
|
||||
|
||||
this.pointer++;
|
||||
|
||||
|
@ -214,17 +212,14 @@ export class Highlighter {
|
|||
}
|
||||
|
||||
private currentTokenOfType<T extends Type>(type: T): Token & { type: T } {
|
||||
console.assert(this.currentToken.type === type, {
|
||||
message: `Highlighter#currentTokenOfType: Expected '${type}', got '${this.currentToken.type}' instead`
|
||||
});
|
||||
VERIFY(this.currentToken.type === type, `Highlighter#currentTokenOfType: Expected '${type}', got '${this.currentToken.type}' instead`);
|
||||
|
||||
return this.currentToken as Token & { type: T };
|
||||
}
|
||||
|
||||
private currentTokenOfEitherType<T extends Type, U extends Type>(a: T, b: U): Token & { type: T | U } {
|
||||
console.assert(this.currentToken.type === a || this.currentToken.type === b, {
|
||||
message: `Highlighter#currentTokenOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`
|
||||
});
|
||||
VERIFY(this.currentToken.type === a || this.currentToken.type === b,
|
||||
`Highlighter#currentTokenOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`);
|
||||
|
||||
return this.currentToken as Token & { type: T };
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import { TODO, VERIFY } from "../util/assertions.js";
|
||||
import { ParseError } from "./errors.js";
|
||||
import { entities } from "./tokenizer/entities.js";
|
||||
import { State } from "./tokenizer/state.js";
|
||||
import { AttributeList, Token, Type } from "./tokenizer/token.js";
|
||||
|
||||
// FIXME: Replace console.assert calls will throwing errors
|
||||
export class Tokenizer {
|
||||
private state: State = State.Data;
|
||||
private returnState!: State;
|
||||
|
@ -510,7 +510,7 @@ export class Tokenizer {
|
|||
|
||||
break;
|
||||
}
|
||||
default: throw new Error(`FIXME (Tokenizer#iterate, Unimplemented state '${this.state}')`);
|
||||
default: TODO(`Tokenizer#iterate, Unimplemented state '${this.state}'`);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -581,9 +581,7 @@ export class Tokenizer {
|
|||
for (let i = 0; i < input.length; i++) {
|
||||
const consumed = this.consumeNext();
|
||||
|
||||
console.assert(consumed === input[i], {
|
||||
message: `Tokenizer#consumeNextFew: Expected '${input[i]}' (${input} at ${i}), got ${consumed} instead`
|
||||
});
|
||||
VERIFY(consumed === input[i], `Tokenizer#consumeNextFew: Expected '${input[i]}' (${input} at ${i}), got ${consumed} instead`);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -591,9 +589,8 @@ export class Tokenizer {
|
|||
for (let i = 0; i < input.length; i++) {
|
||||
const consumed = this.consumeNext()?.toLowerCase();
|
||||
|
||||
console.assert(consumed === input[i].toLowerCase(), {
|
||||
message: `Tokenizer#consumeNextFewCaseInsensitive: Expected '${input[i].toLowerCase()}' (${input.toLowerCase()} at ${i}), got ${consumed} instead`
|
||||
});
|
||||
VERIFY(consumed === input[i].toLowerCase(),
|
||||
`Tokenizer#consumeNextFewCaseInsensitive: Expected '${input[i].toLowerCase()}' (${input.toLowerCase()} at ${i}), got ${consumed} instead`);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -602,33 +599,26 @@ export class Tokenizer {
|
|||
}
|
||||
|
||||
private emitCurrentOfType(type: Type): void {
|
||||
console.assert(this.currentToken.type === type, {
|
||||
message: `Tokenizer#emitCurrentOfType: Expected '${type}', got '${this.currentToken.type}' instead`
|
||||
});
|
||||
VERIFY(this.currentToken.type === type, `Tokenizer#emitCurrentOfType: Expected '${type}', got '${this.currentToken.type}' instead`);
|
||||
|
||||
this.tokens.push(this.currentToken);
|
||||
}
|
||||
|
||||
private emitCurrentOfEitherType(a: Type, b: Type): void {
|
||||
console.assert(this.currentToken.type === a || this.currentToken.type === b, {
|
||||
message: `Tokenizer#emitCurrentOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`
|
||||
});
|
||||
VERIFY(this.currentToken.type === a || this.currentToken.type === b, `Tokenizer#emitCurrentOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`);
|
||||
|
||||
this.tokens.push(this.currentToken);
|
||||
}
|
||||
|
||||
private currentOfType<T extends Type>(type: T): Token & { type: T } {
|
||||
console.assert(this.currentToken.type === type, {
|
||||
message: `Tokenizer#currentOfType: Expected '${type}', got '${this.currentToken.type}' instead`
|
||||
});
|
||||
VERIFY(this.currentToken.type === type, `Tokenizer#currentOfType: Expected '${type}', got '${this.currentToken.type}' instead`);
|
||||
|
||||
return this.currentToken as Token & { type: T };
|
||||
}
|
||||
|
||||
private currentOfEitherType<T extends Type, U extends Type>(a: T, b: U): Token & { type: T | U } {
|
||||
console.assert(this.currentToken.type === a || this.currentToken.type === b, {
|
||||
message: `Tokenizer#currentOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`
|
||||
});
|
||||
VERIFY(this.currentToken.type === a || this.currentToken.type === b,
|
||||
`Tokenizer#currentOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`);
|
||||
|
||||
return this.currentToken as Token & { type: T };
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue