Ergonomics: admit JS property-name syntax in PDict patterns

This commit is contained in:
Tony Garnock-Jones 2023-12-17 22:12:27 +13:00
parent 34f1617a62
commit 919ee891f6
1 changed files with 11 additions and 3 deletions

View File

@ -2,13 +2,13 @@
/// SPDX-FileCopyrightText: Copyright © 2016-2023 Tony Garnock-Jones <tonyg@leastfixedpoint.com> /// SPDX-FileCopyrightText: Copyright © 2016-2023 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
import { import {
Token, Items, Token, Items, TokenBase, TokenType,
Pattern, Pattern,
foldItems, match, anonymousTemplate as template, commaJoin, foldItems, match, anonymousTemplate as template, commaJoin,
scope, bind, seq, alt, upTo, atom, atomString, group, scope, bind, seq, alt, upTo, atom, atomString, group,
repeat, option, withoutSpace, map, mapm, rest, discard, repeat, option, withoutSpace, map, mapm, rest, discard,
value, succeed, fail, separatedOrTerminatedBy, not, TokenBase value, succeed, fail, separatedOrTerminatedBy, not,
} from '../syntax/index.js'; } from '../syntax/index.js';
import * as Matcher from '../syntax/matcher.js'; import * as Matcher from '../syntax/matcher.js';
@ -183,6 +183,14 @@ export class SyndicateParser {
return withoutSpace(upTo(alt(this.exprBoundary, ... extraStops))); return withoutSpace(upTo(alt(this.exprBoundary, ... extraStops)));
} }
propertyNameExpr(): Pattern<Expr> {
const dq = template`"`;
return alt<Expr>(
map(atom(), name => [... dq, name, ... dq]),
map(atom(void 0, { tokenType: TokenType.STRING }), str => [str]),
group('[', this.expr()));
}
readonly type: (... extraStops: Pattern<any>[]) => Pattern<Type> = this.expr; readonly type: (... extraStops: Pattern<any>[]) => Pattern<Type> = this.expr;
statement(acc: Items): Pattern<any> { statement(acc: Items): Pattern<any> {
@ -418,7 +426,7 @@ export class SyndicateParser {
value<[Expr, ValuePattern]>(e => { value<[Expr, ValuePattern]>(e => {
e.value = [] as any; e.value = [] as any;
return seq( return seq(
bind(e.value, '0', this.expr(atom(':'))), bind(e.value, '0', this.propertyNameExpr()),
atom(':'), atom(':'),
bind(e.value, '1', this.valuePattern(level))); bind(e.value, '1', this.valuePattern(level)));
}), }),