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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! JSON source locator for tracking line/column positions.
//!
//! Provides utilities to map JSON Pointer paths to source locations
//! in the original JSON text.
use std::collections::HashMap;
use crate::types::JsonLocation;
/// Locates positions in JSON source text.
///
/// Maps JSON Pointer paths to their source locations (line/column)
/// in the original JSON document.
#[derive(Debug, Clone, Default)]
pub struct JsonSourceLocator {
/// Map from JSON Pointer paths to source locations.
locations: HashMap<String, JsonLocation>,
}
impl JsonSourceLocator {
/// Creates a new source locator by parsing the given JSON text.
#[must_use]
pub fn new(json_text: &str) -> Self {
let mut locator = Self {
locations: HashMap::new(),
};
locator.parse(json_text);
locator
}
/// Parses JSON text and builds the location map.
fn parse(&mut self, text: &str) {
let mut line = 1usize;
let mut column = 1usize;
let mut path_stack: Vec<PathSegment> = Vec::new();
let chars: Vec<char> = text.chars().collect();
let len = chars.len();
let mut i = 0;
// Record root location
self.skip_whitespace(&chars, &mut i, &mut line, &mut column);
if i < len {
self.locations.insert(String::new(), JsonLocation::new(line, column));
}
while i < len {
let ch = chars[i];
match ch {
'{' => {
// Start of object
i += 1;
column += 1;
self.skip_whitespace(&chars, &mut i, &mut line, &mut column);
// Parse object properties
while i < len && chars[i] != '}' {
self.skip_whitespace(&chars, &mut i, &mut line, &mut column);
if i >= len || chars[i] == '}' {
break;
}
// Expect property name (string)
if chars[i] == '\"' {
let _key_line = line;
let _key_column = column;
let key = self.parse_string(&chars, &mut i, &mut line, &mut column);
// Skip colon
self.skip_whitespace(&chars, &mut i, &mut line, &mut column);
if i < len && chars[i] == ':' {
i += 1;
column += 1;
}
// Record value location
self.skip_whitespace(&chars, &mut i, &mut line, &mut column);
path_stack.push(PathSegment::Property(key.clone()));
let path = self.build_path(&path_stack);
self.locations.insert(path, JsonLocation::new(line, column));
// Skip value
self.skip_value(&chars, &mut i, &mut line, &mut column, &mut path_stack);
path_stack.pop();
// Skip comma
self.skip_whitespace(&chars, &mut i, &mut line, &mut column);
if i < len && chars[i] == ',' {
i += 1;
column += 1;
}
} else {
// Invalid JSON, skip character
i += 1;
column += 1;
}
}
// Skip closing brace
if i < len && chars[i] == '}' {
i += 1;
column += 1;
}
}
'[' => {
// Start of array
i += 1;
column += 1;
let mut index = 0usize;
self.skip_whitespace(&chars, &mut i, &mut line, &mut column);
while i < len && chars[i] != ']' {
self.skip_whitespace(&chars, &mut i, &mut line, &mut column);
if i >= len || chars[i] == ']' {
break;
}
// Record element location
path_stack.push(PathSegment::Index(index));
let path = self.build_path(&path_stack);
self.locations.insert(path, JsonLocation::new(line, column));
// Skip value
self.skip_value(&chars, &mut i, &mut line, &mut column, &mut path_stack);
path_stack.pop();
index += 1;
// Skip comma
self.skip_whitespace(&chars, &mut i, &mut line, &mut column);
if i < len && chars[i] == ',' {
i += 1;
column += 1;
}
}
// Skip closing bracket
if i < len && chars[i] == ']' {
i += 1;
column += 1;
}
}
'"' => {
self.parse_string(&chars, &mut i, &mut line, &mut column);
}
'\n' => {
i += 1;
line += 1;
column = 1;
}
_ => {
// Skip other characters (numbers, booleans, null, whitespace)
i += 1;
column += 1;
}
}
}
}
/// Skips whitespace characters.
fn skip_whitespace(&self, chars: &[char], i: &mut usize, line: &mut usize, column: &mut usize) {
while *i < chars.len() {
match chars[*i] {
' ' | '\t' | '\r' => {
*i += 1;
*column += 1;
}
'\n' => {
*i += 1;
*line += 1;
*column = 1;
}
_ => break,
}
}
}
/// Parses a JSON string and returns its content.
fn parse_string(&self, chars: &[char], i: &mut usize, line: &mut usize, column: &mut usize) -> String {
let mut result = String::new();
// Skip opening quote
if *i < chars.len() && chars[*i] == '"' {
*i += 1;
*column += 1;
}
while *i < chars.len() {
let ch = chars[*i];
if ch == '"' {
// End of string
*i += 1;
*column += 1;
break;
} else if ch == '\\' && *i + 1 < chars.len() {
// Escape sequence
*i += 1;
*column += 1;
let escaped = chars[*i];
*i += 1;
*column += 1;
match escaped {
'n' => result.push('\n'),
'r' => result.push('\r'),
't' => result.push('\t'),
'\\' => result.push('\\'),
'"' => result.push('"'),
'/' => result.push('/'),
'u' => {
// Unicode escape - skip 4 hex digits
for _ in 0..4 {
if *i < chars.len() {
*i += 1;
*column += 1;
}
}
}
_ => result.push(escaped),
}
} else if ch == '\n' {
*i += 1;
*line += 1;
*column = 1;
} else {
result.push(ch);
*i += 1;
*column += 1;
}
}
result
}
/// Skips a JSON value (recursively handles objects and arrays).
fn skip_value(
&mut self,
chars: &[char],
i: &mut usize,
line: &mut usize,
column: &mut usize,
path_stack: &mut Vec<PathSegment>,
) {
self.skip_whitespace(chars, i, line, column);
if *i >= chars.len() {
return;
}
match chars[*i] {
'{' => {
*i += 1;
*column += 1;
self.skip_whitespace(chars, i, line, column);
while *i < chars.len() && chars[*i] != '}' {
self.skip_whitespace(chars, i, line, column);
if *i >= chars.len() || chars[*i] == '}' {
break;
}
// Parse property name
if chars[*i] == '"' {
let key = self.parse_string(chars, i, line, column);
// Skip colon
self.skip_whitespace(chars, i, line, column);
if *i < chars.len() && chars[*i] == ':' {
*i += 1;
*column += 1;
}
// Record and skip value
self.skip_whitespace(chars, i, line, column);
path_stack.push(PathSegment::Property(key.clone()));
let path = self.build_path(path_stack);
self.locations.insert(path, JsonLocation::new(*line, *column));
self.skip_value(chars, i, line, column, path_stack);
path_stack.pop();
// Skip comma
self.skip_whitespace(chars, i, line, column);
if *i < chars.len() && chars[*i] == ',' {
*i += 1;
*column += 1;
}
} else {
*i += 1;
*column += 1;
}
}
if *i < chars.len() && chars[*i] == '}' {
*i += 1;
*column += 1;
}
}
'[' => {
*i += 1;
*column += 1;
let mut index = 0usize;
self.skip_whitespace(chars, i, line, column);
while *i < chars.len() && chars[*i] != ']' {
self.skip_whitespace(chars, i, line, column);
if *i >= chars.len() || chars[*i] == ']' {
break;
}
// Record and skip element
path_stack.push(PathSegment::Index(index));
let path = self.build_path(path_stack);
self.locations.insert(path, JsonLocation::new(*line, *column));
self.skip_value(chars, i, line, column, path_stack);
path_stack.pop();
index += 1;
// Skip comma
self.skip_whitespace(chars, i, line, column);
if *i < chars.len() && chars[*i] == ',' {
*i += 1;
*column += 1;
}
}
if *i < chars.len() && chars[*i] == ']' {
*i += 1;
*column += 1;
}
}
'"' => {
self.parse_string(chars, i, line, column);
}
_ => {
// Number, boolean, null - skip until delimiter
while *i < chars.len() {
let ch = chars[*i];
if ch == ',' || ch == '}' || ch == ']' || ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' {
break;
}
if ch == '\n' {
*line += 1;
*column = 1;
} else {
*column += 1;
}
*i += 1;
}
}
}
}
/// Builds a JSON Pointer path from the path stack.
fn build_path(&self, stack: &[PathSegment]) -> String {
if stack.is_empty() {
return String::new();
}
let mut path = String::new();
for segment in stack {
path.push('/');
match segment {
PathSegment::Property(key) => {
// Escape ~ and / in property names
for ch in key.chars() {
match ch {
'~' => path.push_str("~0"),
'/' => path.push_str("~1"),
_ => path.push(ch),
}
}
}
PathSegment::Index(idx) => {
path.push_str(&idx.to_string());
}
}
}
path
}
/// Gets the source location for a JSON Pointer path.
pub fn get_location(&self, path: impl AsRef<str>) -> JsonLocation {
self.locations
.get(path.as_ref())
.copied()
.unwrap_or_else(JsonLocation::unknown)
}
/// Returns true if the locator has a location for the given path.
pub fn has_location(&self, path: &str) -> bool {
self.locations.contains_key(path)
}
}
/// A segment in a JSON Pointer path.
#[derive(Debug, Clone)]
enum PathSegment {
Property(String),
Index(usize),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simple_object() {
let json = r#"{"name": "test", "value": 42}"#;
let locator = JsonSourceLocator::new(json);
let root = locator.get_location("");
assert_eq!(root.line, 1);
assert_eq!(root.column, 1);
let name = locator.get_location("/name");
assert!(!name.is_unknown());
let value = locator.get_location("/value");
assert!(!value.is_unknown());
}
#[test]
fn test_nested_object() {
let json = r#"{
"outer": {
"inner": "value"
}
}"#;
let locator = JsonSourceLocator::new(json);
let inner = locator.get_location("/outer/inner");
assert!(!inner.is_unknown());
assert_eq!(inner.line, 3);
}
#[test]
fn test_array() {
let json = r#"[1, 2, 3]"#;
let locator = JsonSourceLocator::new(json);
let first = locator.get_location("/0");
assert!(!first.is_unknown());
let second = locator.get_location("/1");
assert!(!second.is_unknown());
}
#[test]
fn test_unknown_path() {
let json = r#"{"name": "test"}"#;
let locator = JsonSourceLocator::new(json);
let unknown = locator.get_location("/nonexistent");
assert!(unknown.is_unknown());
}
}