import type { ParjsCombinator } from "../../"; import { Combinated } from "../combinated"; import type { ParsingState } from "../state"; import { defineCombinator } from "./combinator"; class Backtrack extends Combinated { type = "backtrack"; expecting = this.source.expecting; _apply(ps: ParsingState): void { const { position } = ps; this.source.apply(ps); if (ps.isOk) { // if inner succeeded, we backtrack. ps.position = position; } // whatever code ps had, we return it. } } /** * Applies the source parser. If it succeeds, backtracks to the current position in the input and * yields the result. */ export function backtrack(): ParjsCombinator { return defineCombinator(source => { return new Backtrack(source); }); }