-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathSlangNode.ts
More file actions
171 lines (152 loc) · 5.01 KB
/
SlangNode.ts
File metadata and controls
171 lines (152 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {
TerminalKind,
TerminalKindExtensions,
TerminalNode
} from '@nomicfoundation/slang/cst';
import { MultiLineComment } from '../slang-nodes/MultiLineComment.js';
import { MultiLineNatSpecComment } from '../slang-nodes/MultiLineNatSpecComment.js';
import { SingleLineComment } from '../slang-nodes/SingleLineComment.js';
import { SingleLineNatSpecComment } from '../slang-nodes/SingleLineNatSpecComment.js';
import type { Edge, Node } from '@nomicfoundation/slang/cst';
import type { Comment, StrictAstNode } from '../slang-nodes/types.d.ts';
import type { AstLocation, SlangAstNode } from '../types.d.ts';
const offsets = new Map<number, number>();
export function clearOffsets(): void {
offsets.clear();
}
function reversedIterator<T>(children: T[]): Iterable<T> {
return {
[Symbol.iterator](): Iterator<T> {
let index = children.length;
return {
next: function (): IteratorResult<T, T> {
index--;
return { done: index < 0, value: children[index] };
}
};
}
};
}
function isNonTriviaNode(node: Node): boolean {
return (
node.isNonterminalNode() || !TerminalKindExtensions.isTrivia(node.kind)
);
}
function getOffset(children: Iterable<Edge>): number {
let offset = 0;
for (const { node } of children) {
if (isNonTriviaNode(node)) {
// The node's content starts when we find the first non-terminal token,
// or if we find a non-comment, non-whitespace token.
return offset;
}
offset += node.textLength.utf16;
}
return offset;
}
function collectComments(
comments: Comment[],
node: StrictAstNode | StrictAstNode[] | undefined
): Comment[] {
if (node) {
if (Array.isArray(node)) {
return node.reduce(collectComments, comments);
}
if (node.comments.length > 0) {
comments.push(...node.comments.splice(0));
}
}
return comments;
}
export class SlangNode {
comments: Comment[] = [];
loc: AstLocation;
constructor(
ast: SlangAstNode | TerminalNode,
enclosePeripheralComments = false
) {
if (ast instanceof TerminalNode) {
const offset = offsets.get(ast.id) || 0;
this.loc = {
start: offset,
end: offset + ast.textLength.utf16,
leadingOffset: 0,
trailingOffset: 0
};
return;
}
const parent = ast.cst;
const children = parent.children();
const initialOffset = offsets.get(parent.id) || 0;
let offset = initialOffset;
for (const { node } of children) {
if (isNonTriviaNode(node)) {
// Also tracking TerminalNodes since some variants that were not
// Identifier or YulIdentifier but were upgraded to TerminalNode
offsets.set(node.id, offset);
} else {
switch (node.kind) {
// Since the fetching the comments and calculating offsets are both done
// as we iterate over the children and the comment also depends on the
// offset, it's hard to separate these responsibilities into different
// functions without doing the iteration twice.
case TerminalKind.MultiLineComment:
this.comments.push(new MultiLineComment(node, offset));
break;
case TerminalKind.MultiLineNatSpecComment:
this.comments.push(new MultiLineNatSpecComment(node, offset));
break;
case TerminalKind.SingleLineComment:
this.comments.push(new SingleLineComment(node, offset));
break;
case TerminalKind.SingleLineNatSpecComment:
this.comments.push(new SingleLineNatSpecComment(node, offset));
break;
}
}
offset += node.textLength.utf16;
}
const [leadingOffset, trailingOffset] = enclosePeripheralComments
? [0, 0]
: [getOffset(children), getOffset(reversedIterator(children))];
this.loc = {
start: initialOffset + leadingOffset,
end: offset - trailingOffset,
leadingOffset,
trailingOffset
};
}
updateMetadata(
...childNodes: (StrictAstNode | StrictAstNode[] | undefined)[]
): void {
const { comments, loc } = this;
// Collect comments
this.comments = childNodes.reduce(collectComments, comments);
// calculate correct loc object
if (loc.leadingOffset === 0) {
for (const childNode of childNodes) {
if (typeof childNode === 'undefined' || Array.isArray(childNode))
continue;
const { leadingOffset, start } = childNode.loc;
if (start - leadingOffset === loc.start) {
loc.leadingOffset = leadingOffset;
loc.start += leadingOffset;
break;
}
}
}
if (loc.trailingOffset === 0) {
for (const childNode of reversedIterator(childNodes)) {
if (typeof childNode === 'undefined' || Array.isArray(childNode))
continue;
const { trailingOffset, end } = childNode.loc;
if (end + trailingOffset === loc.end) {
loc.trailingOffset = trailingOffset;
loc.end -= trailingOffset;
break;
}
}
}
this.loc = loc;
}
}