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:
networkException 2021-10-24 23:06:35 +02:00
parent 4d71efb394
commit 5afae11193
2 changed files with 20 additions and 35 deletions

View file

@ -2,6 +2,7 @@ import { Palette } from "./highlighter/palette";
import { Node, Position } from "./highlighter/node"; import { Node, Position } from "./highlighter/node";
import { State } from "./highlighter/state"; import { State } from "./highlighter/state";
import { Token, Type } from "./tokenizer/token"; import { Token, Type } from "./tokenizer/token";
import { TODO, VERIFY, VERIFY_NOT_REACHED } from "../util/assertions.js";
export class Highlighter { export class Highlighter {
private state: State = State.Undefined; private state: State = State.Undefined;
@ -28,7 +29,7 @@ export class Highlighter {
case Type.DOCTYPE: this.reconsumeIn(State.DOCTYPE); break; case Type.DOCTYPE: this.reconsumeIn(State.DOCTYPE); break;
case Type.Comment: this.reconsumeIn(State.Comment); break; case Type.Comment: this.reconsumeIn(State.Comment); break;
case Type.EndOfFile: this.finished = true; 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; break;
@ -39,7 +40,7 @@ export class Highlighter {
this.createNode({ position: { line: 0, character: 0 }, color: Palette.Plain, content: '' }); this.createNode({ position: { line: 0, character: 0 }, color: Palette.Plain, content: '' });
this.reconsumeIn(State.Plain); this.reconsumeIn(State.Plain);
break; break;
default: throw new Error('BeforePlain') default: VERIFY_NOT_REACHED();
} }
break; break;
@ -159,7 +160,7 @@ export class Highlighter {
this.state = State.Undefined; this.state = State.Undefined;
break; 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 } { private consumeNextTokenOfType<T extends Type>(type: T): Token & { type: T } {
this.currentToken = this.tokens[this.pointer]; this.currentToken = this.tokens[this.pointer];
console.assert(this.currentToken.type === type, { VERIFY(this.currentToken.type === type, `Highlighter#consumeNextOfType: Expected '${type}', got '${this.currentToken.type}' instead`);
message: `Highlighter#consumeNextOfType: Expected '${type}', got '${this.currentToken.type}' instead`
});
this.pointer++; 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 } { private consumeNextTokenOfEitherType<T extends Type, U extends Type>(a: T, b: U): Token & { type: T | U } {
this.currentToken = this.tokens[this.pointer]; this.currentToken = this.tokens[this.pointer];
console.assert(this.currentToken.type === a || this.currentToken.type === b, { VERIFY(this.currentToken.type === a || this.currentToken.type === b,
message: `Highlighter#consumeNextTokenOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead` `Highlighter#consumeNextTokenOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`);
});
this.pointer++; this.pointer++;
@ -214,17 +212,14 @@ export class Highlighter {
} }
private currentTokenOfType<T extends Type>(type: T): Token & { type: T } { private currentTokenOfType<T extends Type>(type: T): Token & { type: T } {
console.assert(this.currentToken.type === type, { VERIFY(this.currentToken.type === type, `Highlighter#currentTokenOfType: Expected '${type}', got '${this.currentToken.type}' instead`);
message: `Highlighter#currentTokenOfType: Expected '${type}', got '${this.currentToken.type}' instead`
});
return this.currentToken as Token & { type: T }; return this.currentToken as Token & { type: T };
} }
private currentTokenOfEitherType<T extends Type, U extends Type>(a: T, b: U): Token & { type: T | U } { 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, { VERIFY(this.currentToken.type === a || this.currentToken.type === b,
message: `Highlighter#currentTokenOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead` `Highlighter#currentTokenOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`);
});
return this.currentToken as Token & { type: T }; return this.currentToken as Token & { type: T };
} }

View file

@ -1,9 +1,9 @@
import { TODO, VERIFY } from "../util/assertions.js";
import { ParseError } from "./errors.js"; import { ParseError } from "./errors.js";
import { entities } from "./tokenizer/entities.js"; import { entities } from "./tokenizer/entities.js";
import { State } from "./tokenizer/state.js"; import { State } from "./tokenizer/state.js";
import { AttributeList, Token, Type } from "./tokenizer/token.js"; import { AttributeList, Token, Type } from "./tokenizer/token.js";
// FIXME: Replace console.assert calls will throwing errors
export class Tokenizer { export class Tokenizer {
private state: State = State.Data; private state: State = State.Data;
private returnState!: State; private returnState!: State;
@ -510,7 +510,7 @@ export class Tokenizer {
break; 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++) { for (let i = 0; i < input.length; i++) {
const consumed = this.consumeNext(); const consumed = this.consumeNext();
console.assert(consumed === input[i], { VERIFY(consumed === input[i], `Tokenizer#consumeNextFew: Expected '${input[i]}' (${input} at ${i}), got ${consumed} instead`);
message: `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++) { for (let i = 0; i < input.length; i++) {
const consumed = this.consumeNext()?.toLowerCase(); const consumed = this.consumeNext()?.toLowerCase();
console.assert(consumed === input[i].toLowerCase(), { VERIFY(consumed === input[i].toLowerCase(),
message: `Tokenizer#consumeNextFewCaseInsensitive: Expected '${input[i].toLowerCase()}' (${input.toLowerCase()} at ${i}), got ${consumed} instead` `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 { private emitCurrentOfType(type: Type): void {
console.assert(this.currentToken.type === type, { VERIFY(this.currentToken.type === type, `Tokenizer#emitCurrentOfType: Expected '${type}', got '${this.currentToken.type}' instead`);
message: `Tokenizer#emitCurrentOfType: Expected '${type}', got '${this.currentToken.type}' instead`
});
this.tokens.push(this.currentToken); this.tokens.push(this.currentToken);
} }
private emitCurrentOfEitherType(a: Type, b: Type): void { private emitCurrentOfEitherType(a: Type, b: Type): void {
console.assert(this.currentToken.type === a || this.currentToken.type === b, { VERIFY(this.currentToken.type === a || this.currentToken.type === b, `Tokenizer#emitCurrentOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`);
message: `Tokenizer#emitCurrentOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`
});
this.tokens.push(this.currentToken); this.tokens.push(this.currentToken);
} }
private currentOfType<T extends Type>(type: T): Token & { type: T } { private currentOfType<T extends Type>(type: T): Token & { type: T } {
console.assert(this.currentToken.type === type, { VERIFY(this.currentToken.type === type, `Tokenizer#currentOfType: Expected '${type}', got '${this.currentToken.type}' instead`);
message: `Tokenizer#currentOfType: Expected '${type}', got '${this.currentToken.type}' instead`
});
return this.currentToken as Token & { type: T }; return this.currentToken as Token & { type: T };
} }
private currentOfEitherType<T extends Type, U extends Type>(a: T, b: U): Token & { type: T | U } { 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, { VERIFY(this.currentToken.type === a || this.currentToken.type === b,
message: `Tokenizer#currentOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead` `Tokenizer#currentOfEitherType: Expected '${a}' or '${b}', got '${this.currentToken.type}' instead`);
});
return this.currentToken as Token & { type: T }; return this.currentToken as Token & { type: T };
} }