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
  • Loading branch information
char0n committed Jun 28, 2023
1 parent 0b1af95 commit 13a8567
Show file tree
Hide file tree
Showing 12 changed files with 352 additions and 311 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 13a8567

Please sign in to comment.