-
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-yaml-1-2): use tree-sitter cursor for CST travers…
- Loading branch information
Showing
12 changed files
with
923 additions
and
115 deletions.
There are no files selected for viewing
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
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
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
121 changes: 121 additions & 0 deletions
121
packages/apidom-parser-adapter-yaml-1-2/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,121 @@ | ||
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; | ||
} | ||
|
||
stream() { | ||
return new TreeCursorSyntaxNode(this.cursor); | ||
} | ||
|
||
yaml_directive() { | ||
return new TreeCursorSyntaxNode(this.cursor); | ||
} | ||
|
||
tag_directive() { | ||
return new TreeCursorSyntaxNode(this.cursor); | ||
} | ||
|
||
reserved_directive() { | ||
return new TreeCursorSyntaxNode(this.cursor); | ||
} | ||
|
||
document() { | ||
return new TreeCursorSyntaxNode(this.cursor); | ||
} | ||
|
||
block_node() { | ||
return new TreeCursorSyntaxNode(this.cursor).setFieldName(this.cursor); | ||
} | ||
|
||
flow_node() { | ||
return new TreeCursorSyntaxNode(this.cursor).setFieldName(this.cursor); | ||
} | ||
|
||
block_mapping() { | ||
return new TreeCursorSyntaxNode(this.cursor); | ||
} | ||
|
||
block_mapping_pair() { | ||
return new TreeCursorSyntaxNode(this.cursor); | ||
} | ||
|
||
flow_mapping() { | ||
return new TreeCursorSyntaxNode(this.cursor); | ||
} | ||
|
||
flow_pair() { | ||
return new TreeCursorSyntaxNode(this.cursor); | ||
} | ||
|
||
block_sequence() { | ||
return new TreeCursorSyntaxNode(this.cursor); | ||
} | ||
|
||
block_sequence_item() { | ||
return new TreeCursorSyntaxNode(this.cursor); | ||
} | ||
|
||
flow_sequence() { | ||
return new TreeCursorSyntaxNode(this.cursor); | ||
} | ||
|
||
plain_scalar() { | ||
return new TreeCursorSyntaxNode(this.cursor); | ||
} | ||
|
||
single_quote_scalar() { | ||
return new TreeCursorSyntaxNode(this.cursor); | ||
} | ||
|
||
double_quote_scalar() { | ||
return new TreeCursorSyntaxNode(this.cursor); | ||
} | ||
|
||
block_scalar() { | ||
return new TreeCursorSyntaxNode(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 = Array.from(new TreeCursorIterator(this.cursor)); | ||
node.pushChildren(...firstChildSiblings); | ||
} | ||
|
||
node.children.reduce((previousNode: TreeCursorSyntaxNode | undefined, currentNode) => { | ||
currentNode.setPreviousSibling(previousNode); | ||
return currentNode; | ||
}, undefined); | ||
|
||
this.cursor.gotoParent(); | ||
} | ||
|
||
yield node; | ||
} | ||
} | ||
|
||
export default TreeCursorIterator; |
105 changes: 105 additions & 0 deletions
105
packages/apidom-parser-adapter-yaml-1-2/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,105 @@ | ||
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[] = []; | ||
|
||
public previousSibling: TreeCursorSyntaxNode | undefined; | ||
|
||
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 === 'flow_pair' || this.type === 'block_mapping_pair') { | ||
return this.children.find((node) => node.fieldName === 'key'); | ||
} | ||
return undefined; | ||
} | ||
|
||
get valueNode(): TreeCursorSyntaxNode | undefined { | ||
if (this.type === 'flow_pair' || this.type === 'block_mapping_pair') { | ||
return this.children.find((node) => node.fieldName === 'value'); | ||
} | ||
return undefined; | ||
} | ||
|
||
get tag(): TreeCursorSyntaxNode | undefined { | ||
let { previousSibling } = this; | ||
|
||
while (typeof previousSibling !== 'undefined' && previousSibling.type !== 'tag') { | ||
({ previousSibling } = previousSibling); | ||
} | ||
|
||
return previousSibling; | ||
} | ||
|
||
get anchor(): TreeCursorSyntaxNode | undefined { | ||
let { previousSibling } = this; | ||
|
||
while (typeof previousSibling !== 'undefined' && previousSibling.type !== 'anchor') { | ||
({ previousSibling } = previousSibling); | ||
} | ||
|
||
return previousSibling; | ||
} | ||
|
||
get firstNamedChild(): TreeCursorSyntaxNode | undefined { | ||
return this.children.find((node) => node.isNamed); | ||
} | ||
|
||
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; | ||
} | ||
|
||
setPreviousSibling(previousSibling: TreeCursorSyntaxNode | undefined) { | ||
this.previousSibling = previousSibling; | ||
} | ||
|
||
pushChildren(...children: TreeCursorSyntaxNode[]) { | ||
this.children.push(...children); | ||
} | ||
} | ||
|
||
export default TreeCursorSyntaxNode; |
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
Oops, something went wrong.