-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(parser-adapter-json): use tree-sitter cursor for CST traversal
Refs #691
- Loading branch information
Showing
13 changed files
with
353 additions
and
312 deletions.
There are no files selected for viewing
51 changes: 0 additions & 51 deletions
51
packages/apidom-parser-adapter-json/src/syntactic-analysis/PreOrderCursorChildrenIterator.ts
This file was deleted.
Oops, something went wrong.
48 changes: 0 additions & 48 deletions
48
packages/apidom-parser-adapter-json/src/syntactic-analysis/PreOrderCusrorIterator.ts
This file was deleted.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
packages/apidom-parser-adapter-json/src/syntactic-analysis/TreeCursorIterator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
75 changes: 75 additions & 0 deletions
75
packages/apidom-parser-adapter-json/src/syntactic-analysis/TreeCursorSyntaxNode.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
116 changes: 0 additions & 116 deletions
116
packages/apidom-parser-adapter-json/src/syntactic-analysis/direct/CursorIterator.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.