Skip to content

Commit

Permalink
feat(parse): add ArrayReader, update pkg info
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 16, 2020
1 parent 43f62c5 commit 3bec0db
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
4 changes: 3 additions & 1 deletion packages/parse/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"build:test": "rimraf build && tsc -p test/tsconfig.json",
"test": "mocha test",
"cover": "nyc mocha test && nyc report --reporter=lcov",
"clean": "rimraf *.js *.d.ts *.map .nyc_output build coverage doc lib combinators prims xform",
"clean": "rimraf *.js *.d.ts *.map .nyc_output build coverage doc lib combinators presets prims readers xform",
"doc:readme": "ts-node -P ../../tools/tsconfig.json ../../tools/src/readme.ts",
"doc:ae": "mkdir -p .ae/doc .ae/temp && node_modules/.bin/api-extractor run --local --verbose",
"doc": "node_modules/.bin/typedoc --mode modules --out doc src",
Expand All @@ -47,7 +47,9 @@
"*.d.ts",
"lib",
"combinators",
"presets",
"prims",
"readers",
"xform"
],
"keywords": [
Expand Down
14 changes: 8 additions & 6 deletions packages/parse/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from "./api";
export * from "./context";
export * from "./error";
export * from "./string-reader";

export * from "./combinators/alt";
export * from "./combinators/check";
Expand All @@ -16,6 +15,11 @@ export * from "./combinators/repeat";
export * from "./combinators/seq";
export * from "./combinators/xform";

export * from "./presets/alpha";
export * from "./presets/digits";
export * from "./presets/numbers";
export * from "./presets/whitespace";

export * from "./prims/always";
export * from "./prims/anchor";
export * from "./prims/lift";
Expand All @@ -26,13 +30,11 @@ export * from "./prims/range";
export * from "./prims/satisfy";
export * from "./prims/string";

export * from "./readers/array-reader";
export * from "./readers/string-reader";

export * from "./xform/collect";
export * from "./xform/comp";
export * from "./xform/number";
export * from "./xform/merge";
export * from "./xform/print";

export * from "./presets/alpha";
export * from "./presets/digits";
export * from "./presets/numbers";
export * from "./presets/whitespace";
26 changes: 26 additions & 0 deletions packages/parse/src/readers/array-reader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { ParseState, IReader } from "../api";

class ArrayReader<T> implements IReader<T> {
constructor(protected input: ArrayLike<T>) {}

read(state: ParseState<T>): T {
return this.input[state.p];
}

next(state: ParseState<T>): void {
if (state.done) return;
state.last = this.input[state.p];
state.done = ++state.p >= this.input.length;
}

isDone(state: ParseState<T>) {
return (state.done = state.p >= this.input.length);
}

format(state: ParseState<T>) {
return `offset ${state.p}`;
}
}

export const defArrayReader = <T>(input: ArrayLike<T>) =>
new ArrayReader(input);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ParseState, IReader } from "./api";
import type { ParseState, IReader } from "../api";

class StringReader implements IReader<string> {
constructor(protected input: string) {}
Expand Down

0 comments on commit 3bec0db

Please sign in to comment.