Move StringStream into Parsely

This commit is contained in:
Sam Atkins 2024-05-24 14:41:07 +01:00
parent 873dee7e51
commit 0bc4c3fde2
2 changed files with 53 additions and 34 deletions

View File

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

View File

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