mirror of
https://github.com/HeyPuter/puter
synced 2024-11-14 22:06:00 +00:00
Move StringStream into Parsely
This commit is contained in:
parent
873dee7e51
commit
0bc4c3fde2
52
packages/phoenix/packages/parsely/streams.js
Normal file
52
packages/phoenix/packages/parsely/streams.js
Normal 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;
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user