From 7fccf79591de916b63e39b858644f7b91d11b852 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 24 May 2024 15:57:59 +0100 Subject: [PATCH] Make stringOf() take a callback instead of an array of accepted values THe `a.stringOf(' \r\n\t'.split('')),` pattern works fine for small sets of characters, but is horrible for situations like "any alphanumeric". Instead, let's make it take a callback function that is run on each character. --- packages/phoenix/packages/parsely/parsers/terminals.js | 10 +++++----- .../src/puter-shell/coreutils/concept-parser.js | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/phoenix/packages/parsely/parsers/terminals.js b/packages/phoenix/packages/parsely/parsers/terminals.js index 712c688fd..10936540e 100644 --- a/packages/phoenix/packages/parsely/parsers/terminals.js +++ b/packages/phoenix/packages/parsely/parsers/terminals.js @@ -23,12 +23,12 @@ export class Literal extends Parser { } /** - * Parses a string composed of the given values. - * @param values An array of strings that will be parsed as the result. + * Parses matching characters as a string. + * @param test Function that takes a character, and returns whether to include it. */ export class StringOf extends Parser { - _create (values) { - this.values = values; + _create (test) { + this.test = test; } _parse (stream) { @@ -38,7 +38,7 @@ export class StringOf extends Parser { while (true) { let { done, value } = subStream.look(); if ( done ) break; - if ( ! this.values.includes(value) ) break; + if ( ! this.test(value) ) break; subStream.next(); text += value; diff --git a/packages/phoenix/src/puter-shell/coreutils/concept-parser.js b/packages/phoenix/src/puter-shell/coreutils/concept-parser.js index a24b611d8..64369858b 100644 --- a/packages/phoenix/src/puter-shell/coreutils/concept-parser.js +++ b/packages/phoenix/src/puter-shell/coreutils/concept-parser.js @@ -232,7 +232,7 @@ export default { number: a => new NumberParser(), string: a => new StringParser(), whitespace: a => a.optional( - a.stringOf(' \r\n\t'.split('')), + a.stringOf(c => ' \r\n\t'.includes(c)), ), }, { element: it => it[0].value,