@@ -23,8 +23,7 @@ const Formatter = struct {
23
23
ast : * const AST2 ,
24
24
source : []const u8 ,
25
25
ident_store : * const Ident.Store ,
26
- tokens : std .ArrayList (Token ),
27
- token_regions : std .ArrayList (base .Region ), // Region for each token
26
+ tokens : []const Token , // Now uses pre-collected tokens from parser
28
27
output : std .ArrayList (u8 ),
29
28
30
29
// Formatting state - matches original formatter
@@ -33,28 +32,21 @@ const Formatter = struct {
33
32
last_formatted_pos : usize = 0 , // Current position in source being formatted
34
33
last_comment_end : usize = 0 , // Track last comment position to avoid duplicates
35
34
36
- fn init (allocator : std.mem.Allocator , ast : * const AST2 , source : []const u8 , ident_store : * const Ident.Store ) ! Formatter {
37
- var formatter = Formatter {
35
+ fn init (allocator : std.mem.Allocator , ast : * const AST2 , source : []const u8 , ident_store : * const Ident.Store , tokens : [] const Token ) ! Formatter {
36
+ return Formatter {
38
37
.allocator = allocator ,
39
38
.ast = ast ,
40
39
.source = source ,
41
40
.ident_store = ident_store ,
42
- .tokens = std .ArrayList (Token ).init (allocator ),
43
- .token_regions = std .ArrayList (base .Region ).init (allocator ),
41
+ .tokens = tokens , // Use pre-collected tokens
44
42
.output = std .ArrayList (u8 ).init (allocator ),
45
43
.last_formatted_pos = 0 ,
46
44
.last_comment_end = 0 ,
47
45
};
48
-
49
- // Tokenize once at the beginning
50
- try formatter .tokenizeSource ();
51
-
52
- return formatter ;
53
46
}
54
47
55
48
fn deinit (self : * Formatter ) void {
56
- self .tokens .deinit ();
57
- self .token_regions .deinit ();
49
+ // tokens are now owned externally, don't deinit them
58
50
self .output .deinit ();
59
51
}
60
52
@@ -79,7 +71,7 @@ const Formatter = struct {
79
71
}
80
72
81
73
// Flush any trailing comments using token-based approach
82
- if (self .token_regions . items .len > 0 ) {
74
+ if (self .tokens .len > 0 ) {
83
75
_ = try self .flushTrailingComments ();
84
76
}
85
77
}
@@ -124,8 +116,8 @@ const Formatter = struct {
124
116
125
117
/// Find the token index that contains or is after the given position
126
118
fn findTokenAtPosition (self : * const Formatter , pos : Position ) ? usize {
127
- for (self .token_regions . items , 0.. ) | region , i | {
128
- if (region .start .offset >= pos .offset ) {
119
+ for (self .tokens , 0.. ) | token , i | {
120
+ if (token . region .start .offset >= pos .offset ) {
129
121
return i ;
130
122
}
131
123
}
@@ -135,8 +127,8 @@ const Formatter = struct {
135
127
/// Find the token index that is before the given position
136
128
fn findTokenBeforePosition (self : * const Formatter , pos : Position ) ? usize {
137
129
var result : ? usize = null ;
138
- for (self .token_regions . items , 0.. ) | region , i | {
139
- if (region .end .offset <= pos .offset ) {
130
+ for (self .tokens , 0.. ) | token , i | {
131
+ if (token . region .end .offset <= pos .offset ) {
140
132
result = i ;
141
133
} else {
142
134
break ;
@@ -2928,7 +2920,7 @@ const Formatter = struct {
2928
2920
if (token_idx == 0 ) return false ;
2929
2921
2930
2922
// Get the region between previous token and this position
2931
- const prev_token_end = self .token_regions . items [token_idx - 1 ].end .offset ;
2923
+ const prev_token_end = self .tokens [token_idx - 1 ]. region .end .offset ;
2932
2924
const end_offset = pos .offset ;
2933
2925
2934
2926
if (end_offset <= prev_token_end ) return false ;
@@ -2940,11 +2932,11 @@ const Formatter = struct {
2940
2932
fn flushCommentsAfterToken (self : * Formatter , pos : Position ) ! bool {
2941
2933
// Find the token before this position
2942
2934
const token_idx = self .findTokenBeforePosition (pos ) orelse return false ;
2943
- if (token_idx >= self .token_regions . items .len - 1 ) return false ;
2935
+ if (token_idx >= self .tokens .len - 1 ) return false ;
2944
2936
2945
2937
// Get the region between this position and next token
2946
2938
const start_offset = pos .offset ;
2947
- const next_token_start = self .token_regions . items [token_idx + 1 ].start .offset ;
2939
+ const next_token_start = self .tokens [token_idx + 1 ]. region .start .offset ;
2948
2940
2949
2941
if (next_token_start <= start_offset ) return false ;
2950
2942
@@ -3052,9 +3044,9 @@ const Formatter = struct {
3052
3044
}
3053
3045
3054
3046
fn flushTrailingComments (self : * Formatter ) ! bool {
3055
- if (self .token_regions . items .len == 0 ) return false ;
3047
+ if (self .tokens .len == 0 ) return false ;
3056
3048
3057
- const last_token_end = self .token_regions . items [self .token_regions . items . len - 1 ].end .offset ;
3049
+ const last_token_end = self .tokens [self .tokens . len - 1 ]. region .end .offset ;
3058
3050
if (last_token_end >= self .source .len ) return false ;
3059
3051
3060
3052
const remaining = self .source [last_token_end .. ];
@@ -3063,8 +3055,39 @@ const Formatter = struct {
3063
3055
};
3064
3056
3065
3057
/// Formats AST2 with source code
3066
- pub fn formatAst (allocator : std.mem.Allocator , ast : * const AST2 , source : []const u8 , ident_store : * const Ident.Store , root_node : ? Node.Idx ) ! []u8 {
3067
- var formatter = try Formatter .init (allocator , ast , source , ident_store );
3058
+ pub fn formatAst (
3059
+ allocator : std.mem.Allocator ,
3060
+ ast : * const AST2 ,
3061
+ source : []const u8 ,
3062
+ ident_store : * const Ident.Store ,
3063
+ root_node : ? Node.Idx ,
3064
+ ) ! []u8 {
3065
+ // Parse and collect tokens using the new token-fed architecture
3066
+ var env = try CommonEnv .init (allocator , source );
3067
+ defer env .deinit (allocator );
3068
+
3069
+ var byte_slices = collections.ByteSlices { .entries = .{} };
3070
+ defer byte_slices .entries .deinit (allocator );
3071
+
3072
+ var messages : [256 ]tokenize_iter.Diagnostic = undefined ;
3073
+
3074
+ // Create a temporary AST for parsing (we'll use the provided one for formatting)
3075
+ var parse_ast = try AST2 .initCapacity (allocator , 1024 );
3076
+ defer parse_ast .deinit (allocator );
3077
+
3078
+ // Parse and collect tokens
3079
+ const parse_result = try Parser2 .parseAndCollectTokens (
3080
+ & env ,
3081
+ allocator ,
3082
+ source ,
3083
+ & messages ,
3084
+ & parse_ast ,
3085
+ & byte_slices ,
3086
+ );
3087
+ var tokens = parse_result .tokens ;
3088
+ defer tokens .deinit ();
3089
+
3090
+ var formatter = try Formatter .init (allocator , ast , source , ident_store , tokens .items );
3068
3091
defer formatter .deinit ();
3069
3092
3070
3093
try formatter .format (root_node );
@@ -3075,4 +3098,5 @@ pub fn formatAst(allocator: std.mem.Allocator, ast: *const AST2, source: []const
3075
3098
return output ;
3076
3099
}
3077
3100
3101
+
3078
3102
// Keep the old interface for compatibility
0 commit comments