From 5656d9d42f76202a534ad640d3a4e287e0e40418 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 31 May 2024 10:00:09 +0100 Subject: [PATCH] feat(parsely): Add a fail() parser I've found this useful for stubbing out parts of a grammar during development. --- packages/phoenix/packages/parsely/exports.js | 3 ++- .../phoenix/packages/parsely/parsers/terminals.js | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/phoenix/packages/parsely/exports.js b/packages/phoenix/packages/parsely/exports.js index b729a589..29f956b9 100644 --- a/packages/phoenix/packages/parsely/exports.js +++ b/packages/phoenix/packages/parsely/exports.js @@ -1,6 +1,6 @@ import { adapt_parser, VALUE } from './parser.js'; import { Discard, FirstMatch, Optional, Repeat, Sequence } from './parsers/combinators.js'; -import { Literal, None, StringOf, Symbol } from './parsers/terminals.js'; +import { Fail, Literal, None, StringOf, Symbol } from './parsers/terminals.js'; class ParserWithAction { #parser; @@ -81,6 +81,7 @@ export class GrammarContext { export const standard_parsers = () => { return { discard: Discard, + fail: Fail, firstMatch: FirstMatch, literal: Literal, none: None, diff --git a/packages/phoenix/packages/parsely/parsers/terminals.js b/packages/phoenix/packages/parsely/parsers/terminals.js index 10936540..62c19df6 100644 --- a/packages/phoenix/packages/parsely/parsers/terminals.js +++ b/packages/phoenix/packages/parsely/parsers/terminals.js @@ -91,3 +91,14 @@ export class None extends Parser { return { status: VALUE, $: 'none', $discard: true }; } } + +/** + * Always fails parsing. + */ +export class Fail extends Parser { + _create () {} + + _parse (stream) { + return UNRECOGNIZED; + } +}