Skip to content

Commit

Permalink
feat(visitor): deep first traverse
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed Jul 9, 2020
1 parent b02f276 commit 8bd49bf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
28 changes: 25 additions & 3 deletions src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,32 @@ export function traverseAst(traverser: Traverser, node: Token | Array<any> | Obj
traverseAst(traverser, item);
});
}

}

export function createTraverser(traverser: Traverser) {
return function t(node: Token | Array<any> | Object): void {
traverseAst(traverser, node);
export function traverseAstDeepFirst(traverser: Traverser, node: Token | Array<any> | Object): void {

if (isPlainObject(node) || isArray(node) || node instanceof Token) {
// @ts-ignore
forEach(node, (item) => {
traverseAstDeepFirst(traverser, item);
});
}

if (node instanceof Token) {
if (node.type in traverser) {
traverser[node.type](node);
}
}

}

export function createTraverser(traverser: Traverser, deepFirst = false) {
return (node: Token | Array<any> | Object): void => {
if (deepFirst) {
traverseAstDeepFirst(traverser, node);
} else {
traverseAst(traverser, node);
}
};
}
24 changes: 19 additions & 5 deletions test/visitor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('Visitor Parse Suite', () => {

const createSeqTokenProcessor = (key: string, arr: Array<any>) => () => arr.push(key);

const createSeqTraverser = () => {
const createSeqTraverser = (deepFirst = false) => {
const visitSequence = [];

const visit = createTraverser({
Expand All @@ -19,17 +19,17 @@ describe('Visitor Parse Suite', () => {
AndExpression: createSeqTokenProcessor('and', visitSequence),
BoolParenExpression: createSeqTokenProcessor('paren', visitSequence),
EqualsExpression: createSeqTokenProcessor('eq', visitSequence),
Literal: createSeqTokenProcessor('lit', visitSequence),
FirstMemberExpression: createSeqTokenProcessor('mem', visitSequence)
});
OrExpression: createSeqTokenProcessor('or', visitSequence),
Literal: createSeqTokenProcessor('lit', visitSequence)
}, deepFirst);

return { visit, visitSequence };
};


it('should visit filter', () => {

const expectedSeq = ['and', 'paren', 'eq', 'mem', 'lit', 'paren', 'eq', 'mem', 'lit'];
const expectedSeq = ['and', 'paren', 'eq', 'lit', 'paren', 'eq', 'lit'];

const { visit, visitSequence } = createSeqTraverser();

Expand All @@ -41,4 +41,18 @@ describe('Visitor Parse Suite', () => {

});

it('should visit filter deep first', () => {

const expectedSeq = ['lit', 'eq', 'lit', 'eq', 'or', 'paren', 'lit', 'eq', 'paren', 'and'];

const { visit, visitSequence } = createSeqTraverser(true);

const node = defaultParser.filter('(A eq 2 or A eq 3) and (V eq 3)');

visit(node);

expect(visitSequence).toEqual(expectedSeq);

});

});

0 comments on commit 8bd49bf

Please sign in to comment.