-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathSlangNode.ts
More file actions
170 lines (152 loc) · 5.05 KB
/
SlangNode.ts
File metadata and controls
170 lines (152 loc) · 5.05 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
import {
TerminalKind,
TerminalNode,
TerminalKindExtensions
} 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 { 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 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 cst = ast.cst;
const initialOffset = offsets.get(cst.id) || 0;
let offset = initialOffset;
let triviaLength = 0;
let leadingOffset;
let trailingOffset;
if (enclosePeripheralComments) {
// We initialize the offsets to 0 to avoid them being updated later.
leadingOffset = 0;
trailingOffset = 0;
}
for (const { node } of cst.children()) {
const textLength = node.textLength.utf16;
if (
node.isNonterminalNode() ||
!TerminalKindExtensions.isTrivia(node.kind)
) {
// Also tracking TerminalNodes since some variants that were not
// Identifier or YulIdentifier but were upgraded to TerminalNode
offsets.set(node.id, offset);
// We assign the `leadingOffset` only once.
leadingOffset ??= triviaLength;
// Since this is a non trivia node, we reset the accumulated length
triviaLength = 0;
} 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;
}
// We accumulate the trivia length
triviaLength += textLength;
}
offset += textLength;
}
// In case the `leadingOffset` was not initialized
leadingOffset ??= 0;
// The remaining `triviaLength` is the `trailingOffset`
trailingOffset ??= triviaLength;
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;
}
}