From 0bc4c3fde2a1e6137b08a9285b7d8f028456b4ae Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 24 May 2024 14:41:07 +0100 Subject: [PATCH] Move StringStream into Parsely --- packages/phoenix/packages/parsely/streams.js | 52 +++++++++++++++++++ .../puter-shell/coreutils/concept-parser.js | 35 +------------ 2 files changed, 53 insertions(+), 34 deletions(-) create mode 100644 packages/phoenix/packages/parsely/streams.js diff --git a/packages/phoenix/packages/parsely/streams.js b/packages/phoenix/packages/parsely/streams.js new file mode 100644 index 00000000..23254b62 --- /dev/null +++ b/packages/phoenix/packages/parsely/streams.js @@ -0,0 +1,52 @@ +/** + * Base class for input streams. + * Defines which methods are expected for any stream implementations. + */ +export class ParserStream { + value_at (index) { throw new Error(`${this.constructor.name}.value_at() not implemented`); } + look () { throw new Error(`${this.constructor.name}.look() not implemented`); } + next () { throw new Error(`${this.constructor.name}.next() not implemented`); } + fork () { throw new Error(`${this.constructor.name}.fork() not implemented`); } + join () { throw new Error(`${this.constructor.name}.join() not implemented`); } + + is_eof () { + return this.look().done; + } +} + +/** + * ParserStream that takes a string, and processes it character by character. + */ +export class StringStream extends ParserStream { + constructor (str, startIndex = 0) { + super(); + this.str = str; + this.i = startIndex; + } + + value_at (index) { + if ( index >= this.str.length ) { + return { done: true, value: undefined }; + } + + return { done: false, value: this.str[index] }; + } + + look () { + return this.value_at(this.i); + } + + next () { + const result = this.value_at(this.i); + this.i++; + return result; + } + + fork () { + return new StringStream(this.str, this.i); + } + + join (forked) { + this.i = forked.i; + } +} diff --git a/packages/phoenix/src/puter-shell/coreutils/concept-parser.js b/packages/phoenix/src/puter-shell/coreutils/concept-parser.js index 7036fab4..a24b611d 100644 --- a/packages/phoenix/src/puter-shell/coreutils/concept-parser.js +++ b/packages/phoenix/src/puter-shell/coreutils/concept-parser.js @@ -1,5 +1,6 @@ import { GrammarContext, standard_parsers } from '../../../packages/parsely/exports.js'; import { Parser, UNRECOGNIZED, VALUE } from '../../../packages/parsely/parser.js'; +import { StringStream } from '../../../packages/parsely/streams.js'; class NumberParser extends Parser { static data = { @@ -163,39 +164,6 @@ class StringParser extends Parser { } } -class StringStream { - constructor (str, startIndex = 0) { - this.str = str; - this.i = startIndex; - } - - value_at (index) { - if ( index >= this.str.length ) { - return { done: true, value: undefined }; - } - - return { done: false, value: this.str[index] }; - } - - look () { - return this.value_at(this.i); - } - - next () { - const result = this.value_at(this.i); - this.i++; - return result; - } - - fork () { - return new StringStream(this.str, this.i); - } - - join (forked) { - this.i = forked.i; - } -} - export default { name: 'concept-parser', args: { @@ -305,7 +273,6 @@ export default { whitespace: _ => {}, }); - // TODO: What do we want our streams to be like? const input = ctx.locals.positionals.shift(); const stream = new StringStream(input); try {