UNPKG

cbon

Version:
94 lines (93 loc) 2.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("./utils"); const pos_1 = require("./pos"); exports.ReDo = Symbol('ReDo'); class WhenError extends Error { constructor(err) { super(); this.err = err; } } exports.WhenError = WhenError; class State { constructor(show_all_err) { this.count = 0; this.char = 0; this.line = 0; this.states = []; this.lines = []; this.errors = []; this.queue = []; this.show_all_err = show_all_err; } push(unit) { this.states.push(unit); } pop() { return this.states.pop(); } call(c) { const r = this.states[this.states.length - 1](c); if (!utils_1.isVoid(r)) { if (r === exports.ReDo) { this.queue.push(() => this.call(c)); } else { const { fn, ignoreFirst } = r; this.push(fn); if (!ignoreFirst) this.queue.push(() => this.call(c)); } } } get pos() { return new pos_1.TkPos(this.count, this.char, this.line); } get lastPos() { const np = new pos_1.TkPos(this.count, this.char, this.line); np.count--; if (np.char === 0) { np.line--; np.char = this.lines[np.line]; } else np.char--; return np; } } exports.State = State; class Context { constructor(state) { this.state = state; } end() { this.state.pop(); } call(f, ...p) { return { fn: f(new Context(this.state), ...p), ignoreFirst: false }; } callNoFirst(f, ...p) { return { fn: f(new Context(this.state), ...p), ignoreFirst: true }; } flag() { this.last_flag = this.state.pos; } range(last = false) { var _a; const n = last ? this.state.lastPos : this.state.pos; return new pos_1.TkRange((_a = this.last_flag, (_a !== null && _a !== void 0 ? _a : n)), n); } error(range, msg) { if (!this.state.show_all_err) throw new WhenError({ range, msg }); this.state.errors.push({ range, msg }); } } exports.Context = Context;