feat(parsely): Add a fail() parser

I've found this useful for stubbing out parts of a grammar during
development.
This commit is contained in:
Sam Atkins 2024-05-31 10:00:09 +01:00
parent c9a43ce5c0
commit 5656d9d42f
2 changed files with 13 additions and 1 deletions

View File

@ -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,

View File

@ -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;
}
}