@@ -8,13 +8,14 @@ import { MultiLineNatSpecComment } from './MultiLineNatSpecComment.js';
88import { SingleLineComment } from './SingleLineComment.js' ;
99import { SingleLineNatSpecComment } from './SingleLineNatSpecComment.js' ;
1010
11+ import type { NonterminalKind } from '@nomicfoundation/slang/cst' ;
1112import type {
1213 AstLocation ,
1314 CollectedMetadata ,
1415 SlangAstNode
1516} from '../types.d.ts' ;
1617import type { Comment , StrictAstNode } from './types.d.ts' ;
17- import type { TerminalNode } from './TerminalNode.js ' ;
18+ import type { TerminalNode } from './TerminalNode.ts ' ;
1819
1920function 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 }
0 commit comments