UNPKG

ohm-js

Version:

An object-oriented language for parsing and pattern matching

61 lines (50 loc) 1.41 kB
import {Failure} from './Failure.js'; import {TerminalNode} from './nodes.js'; import {assert} from './common.js'; import {PExpr, Terminal} from './pexprs-main.js'; export class CaseInsensitiveTerminal extends PExpr { constructor(param) { super(); this.obj = param; } _getString(state) { const terminal = state.currentApplication().args[this.obj.index]; assert(terminal instanceof Terminal, 'expected a Terminal expression'); return terminal.obj; } // Implementation of the PExpr API allowsSkippingPrecedingSpace() { return true; } eval(state) { const {inputStream} = state; const origPos = inputStream.pos; const matchStr = this._getString(state); if (!inputStream.matchString(matchStr, true)) { state.processFailure(origPos, this); return false; } else { state.pushBinding(new TerminalNode(matchStr.length), origPos); return true; } } getArity() { return 1; } substituteParams(actuals) { return new CaseInsensitiveTerminal(this.obj.substituteParams(actuals)); } toDisplayString() { return this.obj.toDisplayString() + ' (case-insensitive)'; } toFailure(grammar) { return new Failure( this, this.obj.toFailure(grammar) + ' (case-insensitive)', 'description', ); } _isNullable(grammar, memo) { return this.obj._isNullable(grammar, memo); } }