Skip to content

Commit

Permalink
perf(parser-adapter-json): use tree-sitter cursor for CST traversal
Browse files Browse the repository at this point in the history
Refs #691
  • Loading branch information
char0n committed Jun 27, 2023
1 parent 7931fb4 commit 96c8135
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 312 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { TreeCursor as NodeTreeCursor } from 'tree-sitter';
import { TreeCursor as WebTreeCursor } from 'web-tree-sitter';

import TreeCursorSyntaxNode from './TreeCursorSyntaxNode';

class TreeCursorIterator {
protected readonly cursor;

constructor(cursor: NodeTreeCursor | WebTreeCursor) {
this.cursor = cursor;
}

document() {
return new TreeCursorSyntaxNode(this.cursor);
}

object() {
return new TreeCursorSyntaxNode(this.cursor).setFieldName(this.cursor);
}

array() {
return new TreeCursorSyntaxNode(this.cursor).setFieldName(this.cursor);
}

pair() {
return new TreeCursorSyntaxNode(this.cursor);
}

string() {
return new TreeCursorSyntaxNode(this.cursor).setFieldName(this.cursor);
}

number() {
return new TreeCursorSyntaxNode(this.cursor).setFieldName(this.cursor);
}

null() {
return new TreeCursorSyntaxNode(this.cursor).setFieldName(this.cursor);
}

true() {
return new TreeCursorSyntaxNode(this.cursor).setFieldName(this.cursor);
}

false() {
return new TreeCursorSyntaxNode(this.cursor).setFieldName(this.cursor);
}

ERROR() {
return new TreeCursorSyntaxNode(this.cursor).setHasError(this.cursor);
}

public *[Symbol.iterator]() {
let node: TreeCursorSyntaxNode;

if (this.cursor.nodeType in this) {
// @ts-ignore
node = this[this.cursor.nodeType]() as TreeCursorSyntaxNode;
} else {
node = new TreeCursorSyntaxNode(this.cursor);
}

if (this.cursor.gotoFirstChild()) {
const [firstChild] = new TreeCursorIterator(this.cursor);

node.pushChildren(firstChild);

while (this.cursor.gotoNextSibling()) {
const firstChildSiblings = new TreeCursorIterator(this.cursor);
node.pushChildren(...firstChildSiblings);
}

this.cursor.gotoParent();
}

yield node;
}
}

export default TreeCursorIterator;
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { TreeCursor as NodeTreeCursor, Point as NodePoint } from 'tree-sitter';
import { TreeCursor as WebTreeCursor, Point as WebPoint } from 'web-tree-sitter';

class TreeCursorSyntaxNode {
public readonly type: string;

public readonly startPosition: NodePoint | WebPoint;

public readonly endPosition: NodePoint | WebPoint;

public readonly startIndex: number;

public readonly endIndex: number;

public readonly text: string;

public readonly isNamed: boolean;

public readonly isMissing: boolean;

public fieldName: string | undefined;

public hasError = false;

public readonly children: TreeCursorSyntaxNode[] = [];

constructor(cursor: NodeTreeCursor | WebTreeCursor) {
this.type = cursor.nodeType;
this.startPosition = cursor.startPosition;
this.endPosition = cursor.endPosition;
this.startIndex = cursor.startIndex;
this.endIndex = cursor.endIndex;
this.text = cursor.nodeText;
this.isNamed = cursor.nodeIsNamed;
this.isMissing = cursor.nodeIsMissing;
}

get keyNode(): TreeCursorSyntaxNode | undefined {
if (this.type === 'pair') {
return this.children.find((node) => node.fieldName === 'key');
}
return undefined;
}

get valueNode(): TreeCursorSyntaxNode | undefined {
if (this.type === 'pair') {
return this.children.find((node) => node.fieldName === 'value');
}
return undefined;
}

setFieldName(cursor: NodeTreeCursor | WebTreeCursor) {
if (typeof cursor.currentFieldName === 'function') {
this.fieldName = cursor.currentFieldName();
} else {
this.fieldName = cursor.currentFieldName;
}
return this;
}

setHasError(cursor: NodeTreeCursor | WebTreeCursor) {
if (typeof cursor.currentNode === 'function') {
this.hasError = cursor.currentNode().hasError();
} else {
this.hasError = cursor.currentNode.hasError();
}
return this;
}

pushChildren(...children: TreeCursorSyntaxNode[]) {
this.children.push(...children);
}
}

export default TreeCursorSyntaxNode;

This file was deleted.

Loading

0 comments on commit 96c8135

Please sign in to comment.