Skip to content

Commit a8bc1bc

Browse files
author
Sascha Goldhofer
committed
Add node type
1 parent d7776b0 commit a8bc1bc

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

lib/interpreter/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { expand, select, cache } from "./nodes";
22
import { VALUE_INDEX, KEY_INDEX, PARENT_INDEX, POINTER_INDEX } from "./keys";
33
import { QueryResult, Input, JSONPointer } from "../types";
4+
import { IToken } from "ebnf";
45

56
type WorkingSet = Array<QueryResult>;
67

78

8-
function collect(func, input: WorkingSet, node, pointer: JSONPointer) {
9+
function collect(func, input: WorkingSet, node: IToken, pointer: JSONPointer): WorkingSet {
910
const result = [];
1011
for (let i = 0, l = input.length; i < l; i += 1) {
1112
result.push(...func(node, input[i], node, pointer));
@@ -14,7 +15,7 @@ function collect(func, input: WorkingSet, node, pointer: JSONPointer) {
1415
}
1516

1617

17-
function reduce(func, input, node, pointer: JSONPointer) {
18+
function reduce(func, input: WorkingSet, node: IToken, pointer: JSONPointer): WorkingSet {
1819
const result = [];
1920
for (let i = 0, l = input.length; i < l; i += 1) {
2021
const output = func(node, input[i], pointer);
@@ -26,7 +27,7 @@ function reduce(func, input, node, pointer: JSONPointer) {
2627
}
2728

2829

29-
function query(data: WorkingSet, ast, pointer: JSONPointer): WorkingSet {
30+
function query(data: WorkingSet, ast: IToken, pointer: JSONPointer): WorkingSet {
3031
let result = data;
3132
ast.children.forEach(node => {
3233
if (expand[node.type]) {
@@ -43,7 +44,7 @@ function query(data: WorkingSet, ast, pointer: JSONPointer): WorkingSet {
4344
}
4445

4546

46-
function runPatternOnce(inputSet: WorkingSet, ast, pointer: JSONPointer): WorkingSet {
47+
function runPatternOnce(inputSet: WorkingSet, ast: IToken, pointer: JSONPointer): WorkingSet {
4748
const resultingSet = [];
4849
let workingSet = inputSet;
4950
ast.children.forEach(node => {
@@ -58,7 +59,7 @@ function runPatternOnce(inputSet: WorkingSet, ast, pointer: JSONPointer): Workin
5859
return resultingSet;
5960
}
6061

61-
function getIterationCount(quantifier?: string) {
62+
function getIterationCount(quantifier?: string): number {
6263
if (quantifier == null) {
6364
return 1; // default, simple group
6465
}
@@ -70,7 +71,7 @@ function getIterationCount(quantifier?: string) {
7071
}
7172

7273

73-
function pattern(data, ast, pointer: JSONPointer) {
74+
function pattern(data: WorkingSet, ast: IToken, pointer: JSONPointer): WorkingSet {
7475
const result = [];
7576
const quantifier = ast.children.find(node => node.type === "quantifier");
7677
const iterationCount = getIterationCount(quantifier && quantifier.text);
@@ -88,14 +89,14 @@ function pattern(data, ast, pointer: JSONPointer) {
8889
}
8990

9091

91-
function skip(data: WorkingSet, ast, pointer: JSONPointer) {
92+
function skip(data: WorkingSet, ast: IToken, pointer: JSONPointer): WorkingSet {
9293
let result = data;
9394
ast.children.forEach(n => (result = runNode(result, n, pointer)));
9495
return result;
9596
}
9697

9798

98-
function runNode(data: WorkingSet, ast, pointer?: JSONPointer): WorkingSet {
99+
function runNode(data: WorkingSet, ast: IToken, pointer?: JSONPointer): WorkingSet {
99100
let result;
100101
if (ast.type === "query") {
101102
result = query(data, ast, pointer);
@@ -111,7 +112,7 @@ function runNode(data: WorkingSet, ast, pointer?: JSONPointer): WorkingSet {
111112
}
112113

113114

114-
export default function run(data: Input, ast): Array<QueryResult> {
115+
export default function run(data: Input, ast: IToken): Array<QueryResult> {
115116
cache.reset();
116117
cache.mem.push(data);
117118
return runNode([[data, null, null, "#"]], ast);

lib/interpreter/nodes.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import o from "gson-conform";
22
const join = (a, b) => `${a}/${b}`;
33
import { VALUE_INDEX, POINTER_INDEX } from "./keys";
4+
import { IToken } from "ebnf";
45

56
const toString = Object.prototype.toString;
67
const rContainer = /Object|Array/;
@@ -30,14 +31,14 @@ const cache = {
3031

3132

3233
const expand = {
33-
any(node, entry) {
34+
any(node: IToken, entry) {
3435
const value = entry[VALUE_INDEX];
3536
return o.keys(value)
3637
// .map(prop => cache.get(entry, prop));
3738
.map(prop => [value[prop], prop, value, join(entry[POINTER_INDEX], prop)]);
3839
},
3940

40-
all(node, entry) {
41+
all(node: IToken, entry) {
4142
const result = [entry];
4243
o.forEach(entry[VALUE_INDEX], (value, prop) => {
4344
const childEntry = cache.get(entry, prop);
@@ -47,7 +48,7 @@ const expand = {
4748
return result;
4849
},
4950

50-
regex(node, entry) {
51+
regex(node: IToken, entry) {
5152
const regex = nodeAsRegex(node);
5253
const value = entry[VALUE_INDEX];
5354
return o.keys(value)
@@ -59,9 +60,9 @@ const expand = {
5960

6061
const select = {
6162
// alias to property (but escaped)
62-
escaped: (node, entry) => select.property(node, entry),
63+
escaped: (node: IToken, entry) => select.property(node, entry),
6364

64-
property: (node, entry) => {
65+
property: (node: IToken, entry) => {
6566
const prop = node.text;
6667
if (entry[VALUE_INDEX] && entry[VALUE_INDEX][prop] !== undefined) {
6768
return [
@@ -73,7 +74,7 @@ const select = {
7374
}
7475
},
7576

76-
typecheck: (node, entry) => {
77+
typecheck: (node: IToken, entry) => {
7778
const checkedTyped = node.text.replace(/^\?:/, "");
7879
if (checkedTyped === "value") {
7980
return isContainer(entry[VALUE_INDEX]) ? undefined : entry;
@@ -85,7 +86,7 @@ const select = {
8586
}
8687
},
8788

88-
lookahead: (node, entry) => {
89+
lookahead: (node: IToken, entry) => {
8990
let valid = true;
9091
let or = false;
9192
node.children.forEach(expr => {
@@ -99,7 +100,7 @@ const select = {
99100
return valid ? entry : undefined;
100101
},
101102

102-
expression: (node, entry) => {
103+
expression: (node: IToken, entry) => {
103104
const prop = node.children[0].text;
104105
const cmp = node.children[1];
105106
const test = node.children[2];

lib/parser/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { default as EBNF } from "ebnf/dist/Grammars/W3CEBNF";
2+
import { IToken } from "ebnf";
23
import enbf from "./grammar";
34

45
const valid = /(children|text|type|start|end|rest|errors|fullText|\d+)/;
@@ -9,6 +10,6 @@ const toSmallJSON = (ast) => JSON.stringify(ast, (key, value) => (key === "" ||
910
const parser = new EBNF.Parser(enbf);
1011

1112

12-
export const parse = (query) => parser.getAST(query);
13-
export const reduce = (ast) => JSON.parse(toSmallJSON(ast));
13+
export const parse = (query): IToken => parser.getAST(query);
14+
export const reduce = (ast: IToken) => JSON.parse(toSmallJSON(ast));
1415
export { toJSON };

0 commit comments

Comments
 (0)