-
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: implement optimized tree-sitter CST access
Refs #691
- Loading branch information
Showing
19 changed files
with
473 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,4 +69,4 @@ export { | |
visit, | ||
getNodeType, | ||
isNode, | ||
} from './visitor'; | ||
} from './traversal/visitor'; |
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
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
51 changes: 51 additions & 0 deletions
51
packages/apidom-parser-adapter-json/src/syntactic-analysis/PreOrderCursorChildrenIterator.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,51 @@ | ||
import { TreeCursor as NodeTreeCursor, Point as NodePoint } from 'tree-sitter'; | ||
import { TreeCursor as WebTreeCursor, Point as WebPoint } from 'web-tree-sitter'; | ||
|
||
type TreeCursor = NodeTreeCursor | WebTreeCursor; | ||
type Point = NodePoint | WebPoint; | ||
|
||
export interface SyntaxNodeSurrogate { | ||
type: string; | ||
startPosition: Point; | ||
endPosition: Point; | ||
children: SyntaxNodeSurrogate[]; | ||
[key: string]: unknown; | ||
} | ||
|
||
class PreOrderCursorChildrenIterator { | ||
protected cursor; | ||
|
||
constructor(cursor: TreeCursor) { | ||
this.cursor = cursor; | ||
} | ||
|
||
protected createNode(): SyntaxNodeSurrogate { | ||
return { | ||
type: this.cursor.nodeType, | ||
startPosition: this.cursor.startPosition, | ||
endPosition: this.cursor.endPosition, | ||
children: [], | ||
}; | ||
} | ||
|
||
public *[Symbol.iterator]() { | ||
// @ts-ignore | ||
const method = this[this.cursor.nodeType]; | ||
const currentNode = (method || this.createNode).call(this) as SyntaxNodeSurrogate; | ||
const constructor = this.constructor as any; | ||
|
||
if (this.cursor.gotoFirstChild()) { | ||
currentNode.children.push(...[...new constructor(this.cursor)]); | ||
|
||
while (this.cursor.gotoNextSibling()) { | ||
currentNode.children.push(...[...new constructor(this.cursor)]); | ||
} | ||
|
||
this.cursor.gotoParent(); | ||
} | ||
|
||
yield currentNode; | ||
} | ||
} | ||
|
||
export default PreOrderCursorChildrenIterator; |
61 changes: 61 additions & 0 deletions
61
packages/apidom-parser-adapter-json/src/syntactic-analysis/PreOrderCusrorIterator.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,61 @@ | ||
import { TreeCursor as NodeTreeCursor, Point as NodePoint } from 'tree-sitter'; | ||
import { TreeCursor as WebTreeCursor, Point as WebPoint } from 'web-tree-sitter'; | ||
|
||
type TreeCursor = NodeTreeCursor | WebTreeCursor; | ||
type Point = NodePoint | WebPoint; | ||
|
||
interface SyntaxNodeSurrogate { | ||
type: string; | ||
startPosition: Point; | ||
endPosition: Point; | ||
[key: string]: unknown; | ||
} | ||
|
||
class PreOrderCursorIterator { | ||
protected cursor; | ||
|
||
constructor(cursor: TreeCursor) { | ||
this.cursor = cursor; | ||
} | ||
|
||
protected createNode(): SyntaxNodeSurrogate { | ||
return { | ||
type: this.cursor.nodeType, | ||
startPosition: this.cursor.startPosition, | ||
endPosition: this.cursor.endPosition, | ||
}; | ||
} | ||
|
||
public *[Symbol.iterator]() { | ||
let reachedRoot = false; | ||
|
||
while (!reachedRoot) { | ||
// @ts-ignore | ||
const method = this[this.cursor.nodeType]; | ||
|
||
yield (method || this.createNode).call(this) as SyntaxNodeSurrogate; | ||
|
||
if (this.cursor.gotoFirstChild()) { | ||
continue; // eslint-disable-line no-continue | ||
} | ||
|
||
if (this.cursor.gotoNextSibling()) { | ||
continue; // eslint-disable-line no-continue | ||
} | ||
|
||
let retracting = true; | ||
while (retracting) { | ||
if (!this.cursor.gotoParent()) { | ||
retracting = false; | ||
reachedRoot = true; | ||
} | ||
|
||
if (this.cursor.gotoNextSibling()) { | ||
retracting = false; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default PreOrderCursorIterator; |
116 changes: 116 additions & 0 deletions
116
packages/apidom-parser-adapter-json/src/syntactic-analysis/direct/CursorIterator.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,116 @@ | ||
import PreOrderCursorChildrenIterator, { | ||
SyntaxNodeSurrogate, | ||
} from '../PreOrderCursorChildrenIterator'; | ||
|
||
class CursorIterator extends PreOrderCursorChildrenIterator { | ||
document(): SyntaxNodeSurrogate { | ||
return { | ||
type: this.cursor.nodeType, | ||
startPosition: this.cursor.startPosition, | ||
endPosition: this.cursor.endPosition, | ||
children: [], | ||
}; | ||
} | ||
|
||
object(): SyntaxNodeSurrogate { | ||
return { | ||
type: this.cursor.nodeType, | ||
startPosition: this.cursor.startPosition, | ||
endPosition: this.cursor.endPosition, | ||
fieldName: this.cursor.currentFieldName, | ||
children: [], | ||
}; | ||
} | ||
|
||
array(): SyntaxNodeSurrogate { | ||
return { | ||
type: this.cursor.nodeType, | ||
startPosition: this.cursor.startPosition, | ||
endPosition: this.cursor.endPosition, | ||
fieldName: this.cursor.currentFieldName, | ||
children: [], | ||
}; | ||
} | ||
|
||
pair(): SyntaxNodeSurrogate { | ||
return { | ||
type: this.cursor.nodeType, | ||
startPosition: this.cursor.startPosition, | ||
endPosition: this.cursor.endPosition, | ||
get keyNode() { | ||
return this.children.find((node: any) => node.fieldName === 'key'); | ||
}, | ||
get valueNode() { | ||
return this.children.find((node: any) => node.fieldName === 'value'); | ||
}, | ||
children: [], | ||
}; | ||
} | ||
|
||
string(): SyntaxNodeSurrogate { | ||
return { | ||
type: this.cursor.nodeType, | ||
startPosition: this.cursor.startPosition, | ||
endPosition: this.cursor.endPosition, | ||
text: this.cursor.nodeText, | ||
fieldName: this.cursor.currentFieldName, | ||
children: [], | ||
}; | ||
} | ||
|
||
number(): SyntaxNodeSurrogate { | ||
return { | ||
type: this.cursor.nodeType, | ||
startPosition: this.cursor.startPosition, | ||
endPosition: this.cursor.endPosition, | ||
text: this.cursor.nodeText, | ||
fieldName: this.cursor.currentFieldName, | ||
children: [], | ||
}; | ||
} | ||
|
||
null(): SyntaxNodeSurrogate { | ||
return { | ||
type: this.cursor.nodeType, | ||
startPosition: this.cursor.startPosition, | ||
endPosition: this.cursor.endPosition, | ||
fieldName: this.cursor.currentFieldName, | ||
children: [], | ||
}; | ||
} | ||
|
||
true(): SyntaxNodeSurrogate { | ||
return { | ||
type: this.cursor.nodeType, | ||
startPosition: this.cursor.startPosition, | ||
endPosition: this.cursor.endPosition, | ||
fieldName: this.cursor.currentFieldName, | ||
children: [], | ||
}; | ||
} | ||
|
||
false(): SyntaxNodeSurrogate { | ||
return { | ||
type: this.cursor.nodeType, | ||
startPosition: this.cursor.startPosition, | ||
endPosition: this.cursor.endPosition, | ||
fieldName: this.cursor.currentFieldName, | ||
children: [], | ||
}; | ||
} | ||
|
||
ERROR(): SyntaxNodeSurrogate { | ||
const { currentNode } = this.cursor; | ||
|
||
return { | ||
type: this.cursor.nodeType, | ||
startPosition: this.cursor.startPosition, | ||
endPosition: this.cursor.endPosition, | ||
// @ts-ignore | ||
hasError: () => currentNode.hasError(), | ||
children: [], | ||
}; | ||
} | ||
} | ||
|
||
export default CursorIterator; |
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
15 changes: 15 additions & 0 deletions
15
packages/apidom-parser-adapter-json/test/__snapshots__/adapter-browser.ts.snap
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,15 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`adapter-browser should parse 1`] = ` | ||
(ParseResultElement | ||
(ObjectElement | ||
(MemberElement | ||
(StringElement) | ||
(StringElement)) | ||
(MemberElement | ||
(StringElement) | ||
(BooleanElement)) | ||
(MemberElement | ||
(StringElement) | ||
(StringElement)))) | ||
`; |
15 changes: 15 additions & 0 deletions
15
packages/apidom-parser-adapter-json/test/__snapshots__/adapter-node.ts.snap
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,15 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`adapter-node should parse 1`] = ` | ||
(ParseResultElement | ||
(ObjectElement | ||
(MemberElement | ||
(StringElement) | ||
(StringElement)) | ||
(MemberElement | ||
(StringElement) | ||
(BooleanElement)) | ||
(MemberElement | ||
(StringElement) | ||
(StringElement)))) | ||
`; |
Oops, something went wrong.