From 919ee891f6eeede8c3d0f93dd89cf480bae908c1 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sun, 17 Dec 2023 22:12:27 +1300 Subject: [PATCH] Ergonomics: admit JS property-name syntax in PDict patterns --- packages/compiler/src/compiler/grammar.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/compiler/src/compiler/grammar.ts b/packages/compiler/src/compiler/grammar.ts index 5332e61..32f2bba 100644 --- a/packages/compiler/src/compiler/grammar.ts +++ b/packages/compiler/src/compiler/grammar.ts @@ -2,13 +2,13 @@ /// SPDX-FileCopyrightText: Copyright © 2016-2023 Tony Garnock-Jones import { - Token, Items, + Token, Items, TokenBase, TokenType, Pattern, foldItems, match, anonymousTemplate as template, commaJoin, scope, bind, seq, alt, upTo, atom, atomString, group, repeat, option, withoutSpace, map, mapm, rest, discard, - value, succeed, fail, separatedOrTerminatedBy, not, TokenBase + value, succeed, fail, separatedOrTerminatedBy, not, } from '../syntax/index.js'; import * as Matcher from '../syntax/matcher.js'; @@ -183,6 +183,14 @@ export class SyndicateParser { return withoutSpace(upTo(alt(this.exprBoundary, ... extraStops))); } + propertyNameExpr(): Pattern { + const dq = template`"`; + return alt( + map(atom(), name => [... dq, name, ... dq]), + map(atom(void 0, { tokenType: TokenType.STRING }), str => [str]), + group('[', this.expr())); + } + readonly type: (... extraStops: Pattern[]) => Pattern = this.expr; statement(acc: Items): Pattern { @@ -418,7 +426,7 @@ export class SyndicateParser { value<[Expr, ValuePattern]>(e => { e.value = [] as any; return seq( - bind(e.value, '0', this.expr(atom(':'))), + bind(e.value, '0', this.propertyNameExpr()), atom(':'), bind(e.value, '1', this.valuePattern(level))); }),