Skip to content

Commit 6ad71d8

Browse files
authored
small tweak to the SlangNode class (#1396)
* avoid reassigning the same values when we know nothing changes * reverting the early exit after having done some profiling * instead of keeping leading and trailing offsets we just remember the outer bounds
1 parent 36a6798 commit 6ad71d8

3 files changed

Lines changed: 32 additions & 28 deletions

File tree

src/slang-nodes/ModifierDefinition.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import { FunctionBody } from './FunctionBody.js';
1010

1111
import type * as ast from '@nomicfoundation/slang/ast';
1212
import type { AstPath, Doc, ParserOptions } from 'prettier';
13-
import type { CollectedMetadata, PrintFunction } from '../types.d.ts';
13+
import type {
14+
AstLocation,
15+
CollectedMetadata,
16+
PrintFunction
17+
} from '../types.d.ts';
1418
import type { AstNode } from './types.d.ts';
1519

1620
export class ModifierDefinition extends SlangNode {
@@ -45,14 +49,12 @@ export class ModifierDefinition extends SlangNode {
4549
this.updateMetadata(this.parameters, this.attributes, this.body);
4650

4751
if (!this.parameters) {
48-
const attributesLoc = this.attributes.loc;
49-
const parametersOffset =
50-
attributesLoc.start - attributesLoc.leadingOffset;
51-
const parametersLoc = {
52+
const parametersOffset = this.attributes.loc.outerStart;
53+
const parametersLoc: AstLocation = {
54+
outerStart: parametersOffset,
55+
outerEnd: parametersOffset,
5256
start: parametersOffset,
53-
end: parametersOffset,
54-
leadingOffset: 0,
55-
trailingOffset: 0
57+
end: parametersOffset
5658
};
5759

5860
this.parameters = Object.assign(

src/slang-nodes/SlangNode.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import { MultiLineNatSpecComment } from './MultiLineNatSpecComment.js';
88
import { SingleLineComment } from './SingleLineComment.js';
99
import { SingleLineNatSpecComment } from './SingleLineNatSpecComment.js';
1010

11+
import type { NonterminalKind } from '@nomicfoundation/slang/cst';
1112
import type {
1213
AstLocation,
1314
CollectedMetadata,
1415
SlangAstNode
1516
} from '../types.d.ts';
1617
import type { Comment, StrictAstNode } from './types.d.ts';
17-
import type { TerminalNode } from './TerminalNode.js';
18+
import type { TerminalNode } from './TerminalNode.ts';
1819

1920
function reversedIterator<T>(children: T[]): Iterable<T> {
2021
return {
@@ -30,7 +31,9 @@ function reversedIterator<T>(children: T[]): Iterable<T> {
3031
};
3132
}
3233

33-
export class SlangNode {
34+
export abstract class SlangNode {
35+
abstract readonly kind: TerminalKind | NonterminalKind;
36+
3437
comments?: Comment[];
3538

3639
loc: AstLocation;
@@ -41,12 +44,13 @@ export class SlangNode {
4144
enclosePeripheralComments = false
4245
) {
4346
if (ast instanceof SlangTerminalNode) {
44-
const offset = collected.offsets.get(ast.id) || 0;
47+
const start = collected.offsets.get(ast.id) || 0;
48+
const end = start + ast.textLength.utf16;
4549
this.loc = {
46-
start: offset,
47-
end: offset + ast.textLength.utf16,
48-
leadingOffset: 0,
49-
trailingOffset: 0
50+
outerStart: start,
51+
outerEnd: end,
52+
start,
53+
end
5054
};
5155
return;
5256
}
@@ -109,10 +113,10 @@ export class SlangNode {
109113
trailingOffset ??= triviaLength;
110114

111115
this.loc = {
116+
outerStart: initialOffset,
117+
outerEnd: offset,
112118
start: initialOffset + leadingOffset,
113-
end: offset - trailingOffset,
114-
leadingOffset,
115-
trailingOffset
119+
end: offset - trailingOffset
116120
};
117121
}
118122

@@ -121,26 +125,24 @@ export class SlangNode {
121125
): void {
122126
const { loc } = this;
123127
// calculate correct loc object
124-
if (loc.leadingOffset === 0) {
128+
if (loc.outerStart === loc.start) {
125129
for (const childNode of childNodes) {
126130
if (childNode === undefined) continue;
127-
const { leadingOffset, start } = childNode.loc;
131+
const { outerStart, start } = childNode.loc;
128132

129-
if (start - leadingOffset === loc.start) {
130-
loc.leadingOffset = leadingOffset;
133+
if (outerStart === loc.start) {
131134
loc.start = start;
132135
break;
133136
}
134137
}
135138
}
136139

137-
if (loc.trailingOffset === 0) {
140+
if (loc.outerEnd === loc.end) {
138141
for (const childNode of reversedIterator(childNodes)) {
139142
if (childNode === undefined) continue;
140-
const { trailingOffset, end } = childNode.loc;
143+
const { outerEnd, end } = childNode.loc;
141144

142-
if (end + trailingOffset === loc.end) {
143-
loc.trailingOffset = trailingOffset;
145+
if (outerEnd === loc.end) {
144146
loc.end = end;
145147
break;
146148
}

src/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ interface Location {
2020
}
2121

2222
interface AstLocation extends Location {
23-
leadingOffset: number;
24-
trailingOffset: number;
23+
outerStart: number;
24+
outerEnd: number;
2525
}
2626

2727
type PrintFunction = (path: AstPath<AstNode>) => Doc;

0 commit comments

Comments
 (0)