Repository navigation
Expand file tree
/
Copy pathdump.rs
More file actions
746 lines (687 loc) · 23.1 KB
/
Copy pathdump.rs
File metadata and controls
746 lines (687 loc) · 23.1 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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
use std::fmt::Write;
use crate::{schema::Schema, Ast, Node, NodeContent, TreeId, CHILD_FIELD};
#[derive(Clone, Copy)]
struct TypeCheckContext<'a> {
schema: &'a Schema,
expected: Option<&'a [crate::schema::NodeType]>,
parent_field: Option<(&'a str, &'a str)>,
}
/// Options for controlling AST dump output.
pub struct DumpOptions {
/// Whether to include source locations in the output.
pub show_locations: bool,
/// Whether to include source text for leaf nodes.
pub show_content: bool,
/// Whether to include each node's source range with direct-child ranges
/// replaced by their field names in `⟨angle brackets⟩`.
pub show_abridged_source: bool,
}
impl Default for DumpOptions {
fn default() -> Self {
Self {
show_locations: false,
show_content: true,
show_abridged_source: false,
}
}
}
/// Dump a yeast AST as a human-readable indented text format.
///
/// Output format:
/// ```text
/// program
/// assignment
/// left:
/// left_assignment_list
/// identifier "x"
/// identifier "y"
/// right:
/// call
/// method:
/// identifier "foo"
/// ```
pub fn dump_ast<Id: TreeId>(ast: &Ast<Id>, root: Id, source: &str) -> String {
dump_ast_with_options(ast, root, source, &DumpOptions::default())
}
pub fn dump_ast_with_options<Id: TreeId>(
ast: &Ast<Id>,
root: Id,
source: &str,
options: &DumpOptions,
) -> String {
let mut out = String::new();
dump_node(ast, root, source, options, 0, None, false, &mut out);
out
}
/// Dump an AST and annotate type mismatches against a schema inline.
///
/// Any node that does not match the expected type set for its parent field is
/// rendered with a trailing `" <-- ERROR: ..."` annotation on the same line.
pub fn dump_ast_with_type_errors<Id: TreeId>(
ast: &Ast<Id>,
root: Id,
source: &str,
schema: &Schema,
) -> String {
dump_ast_with_type_errors_and_options(ast, root, source, schema, &DumpOptions::default())
}
/// Dump an AST and annotate type mismatches against a schema inline.
///
/// Any node that does not match the expected type set for its parent field is
/// rendered with a trailing `" <-- ERROR: ..."` annotation on the same line.
pub fn dump_ast_with_type_errors_and_options<Id: TreeId>(
ast: &Ast<Id>,
root: Id,
source: &str,
schema: &Schema,
options: &DumpOptions,
) -> String {
let mut out = String::new();
dump_node(
ast,
root,
source,
options,
0,
Some(TypeCheckContext {
schema,
expected: None,
parent_field: None,
}),
false,
&mut out,
);
out
}
fn format_node_types(node_types: &[crate::schema::NodeType]) -> String {
node_types
.iter()
.map(|t| {
if t.named {
t.kind.clone()
} else {
format!("\"{}\"", t.kind)
}
})
.collect::<Vec<_>>()
.join(" | ")
}
const EMPTY_NODE_TYPES: &[crate::schema::NodeType] = &[];
/// Generate a type-checking error message for a node if it doesn't match expected types.
///
/// # Arguments
/// - `schema`: The AST schema to validate against.
/// - `node`: The node being checked.
/// - `expected`: The set of allowed types for this node, or `None` if type-checking is disabled.
/// - `parent_field`: Optional tuple of (parent_kind, field_name) for context in error messages.
///
/// # Returns
/// `Some(error_message)` if the node violates the schema (e.g., wrong kind, missing field declaration).
/// `None` if the node matches the expected types or if type-checking is disabled.
fn type_error_for_node<Id: TreeId>(
schema: &Schema,
node: &Node<Id>,
expected: Option<&[crate::schema::NodeType]>,
parent_field: Option<(&str, &str)>,
) -> Option<String> {
if schema.id_for_node_kind(node.kind_name()).is_none()
&& schema.id_for_unnamed_node_kind(node.kind_name()).is_none()
{
return Some(format!("node kind '{}' not in schema", node.kind_name()));
}
let expected = expected?;
if expected.is_empty() {
if let Some((kind, field)) = parent_field {
return Some(format!("the node '{kind}' has no field '{field}'"));
}
return Some("field not declared in schema for this parent node".to_string());
}
if schema.node_matches_types(node.kind_name(), node.is_named(), expected) {
None
} else {
let actual = if node.is_named() {
node.kind_name().to_string()
} else {
format!("\"{}\"", node.kind_name())
};
if let Some((kind, field)) = parent_field {
Some(format!(
"The field {}.{} should contain {}, but got {}",
kind,
field,
format_node_types(expected),
actual
))
} else {
Some(format!(
"expected {}, got {}",
format_node_types(expected),
actual
))
}
}
}
/// Look up the allowed types for a field in the schema.
///
/// # Arguments
/// - `schema`: The AST schema to query.
/// - `parent_kind`: The node kind of the parent that contains this field.
/// - `field_id`: The field ID within that parent node.
///
/// # Returns
/// `Some(&[NodeType])` if the field is declared in the schema and has type constraints.
/// `None` if the field is not declared or has no constraints (undeclared field).
fn expected_for_field<'a>(
schema: &'a Schema,
parent_kind: &str,
field_name: &str,
) -> Option<&'a [crate::schema::NodeType]> {
// Resolve the field NAME in the validation schema's own id space, so the
// AST being dumped and the validation schema stay completely independent:
// they need not share field ids, only field names.
let field_id = schema.field_id_for_name(field_name)?;
schema
.field_types(parent_kind, field_id)
.map(|v| v.as_slice())
}
#[allow(clippy::too_many_arguments)]
fn dump_node<Id: TreeId>(
ast: &Ast<Id>,
id: Id,
source: &str,
options: &DumpOptions,
indent: usize,
type_check: Option<TypeCheckContext<'_>>,
external_to_parent: bool,
out: &mut String,
) {
let Some(node) = ast.get_node(id) else {
return;
};
let prefix = " ".repeat(indent);
// Node kind
write!(out, "{}{}", prefix, node.kind_name()).unwrap();
// Location
if options.show_locations {
let start = node.start_position();
let end = node.end_position();
write!(
out,
" [{},{}]-[{},{}]",
start.row + 1,
start.column + 1,
end.row + 1,
end.column + 1
)
.unwrap();
}
// Content for leaf nodes
if options.show_content && node.is_named() && is_leaf(node) {
let content = node_content(node, source);
if !content.is_empty() {
write!(out, " {content:?}").unwrap();
}
}
if options.show_abridged_source {
write_source_skeleton(ast, node, source, out);
if external_to_parent {
write!(out, " (external)").unwrap();
}
}
if let Some(context) = type_check {
if let Some(err) =
type_error_for_node(context.schema, node, context.expected, context.parent_field)
{
write!(out, " <-- ERROR: {err}").unwrap();
}
}
writeln!(out).unwrap();
// Named fields first, in the schema's declared order when available
// (front-end-independent), else in field-id order. Any present fields not
// covered by the declared order are appended in field-id order.
//
// The declared order lives in the validation schema, keyed by *its* field
// ids; the AST being dumped may key the same field names under different
// ids. So map the declared order through field NAMES into this AST's own id
// space, keeping the two schemas independent (they share names, not ids).
let named_field_ids: Vec<u16> = {
let present: Vec<u16> = node
.fields
.keys()
.copied()
.filter(|&f| f != CHILD_FIELD)
.collect();
match type_check.and_then(|context| {
context
.schema
.field_order(node.kind_name())
.map(|order| (context.schema, order))
}) {
Some((schema, order)) => {
let mut result: Vec<u16> = order
.iter()
.filter_map(|&f| schema.field_name_for_id(f))
.filter_map(|name| ast.field_id_for_name(name))
.filter(|&f| f != CHILD_FIELD && node.fields.contains_key(&f))
.collect();
for &f in &present {
if !result.contains(&f) {
result.push(f);
}
}
result
}
None => present,
}
};
for field_id in named_field_ids {
let children = &node.fields[&field_id];
let field_name = ast.field_name_for_id(field_id).unwrap_or("?");
let child_type_check = type_check.map(|context| {
let expected = expected_for_field(context.schema, node.kind_name(), field_name)
.or(Some(EMPTY_NODE_TYPES));
let parent_field = Some((node.kind_name(), field_name));
TypeCheckContext {
schema: context.schema,
expected,
parent_field,
}
});
if children.len() == 1 {
write!(out, "{prefix} {field_name}:").unwrap();
// Inline single child
let child = ast.get_node(children[0]);
let external = child.is_some_and(|child| is_external_child(node, child));
if child.is_some_and(is_leaf) {
write!(out, " ").unwrap();
dump_node_inline(
ast,
children[0],
source,
options,
child_type_check,
external,
out,
);
} else {
writeln!(out).unwrap();
dump_node(
ast,
children[0],
source,
options,
indent + 2,
child_type_check,
external,
out,
);
}
} else {
writeln!(out, "{prefix} {field_name}:").unwrap();
for &child_id in children {
let external = ast
.get_node(child_id)
.is_some_and(|child| is_external_child(node, child));
dump_node(
ast,
child_id,
source,
options,
indent + 2,
child_type_check,
external,
out,
);
}
}
}
// Check for required fields that are absent
if let Some(context) = type_check {
for (_field_id, field_name) in context.schema.required_fields_for_kind(node.kind_name()) {
let present = match field_name {
Some(n) => ast
.field_id_for_name(n)
.is_some_and(|fid| node.fields.contains_key(&fid)),
None => node.fields.contains_key(&CHILD_FIELD),
};
if !present {
let name = field_name.unwrap_or("child");
writeln!(out, "{prefix} <-- ERROR: missing required field '{name}'").unwrap();
}
}
}
// Unnamed children — skip unnamed tokens (keywords, punctuation)
if let Some(children) = node.fields.get(&CHILD_FIELD) {
let child_type_check = type_check.map(|context| {
let expected = context
.schema
.field_types(node.kind_name(), CHILD_FIELD)
.map(|v| v.as_slice())
.or(Some(EMPTY_NODE_TYPES));
let parent_field = Some((node.kind_name(), "children"));
TypeCheckContext {
schema: context.schema,
expected,
parent_field,
}
});
for &child_id in children {
if let Some(child) = ast.get_node(child_id) {
if child.is_named() {
let external = is_external_child(node, child);
dump_node(
ast,
child_id,
source,
options,
indent + 1,
child_type_check,
external,
out,
);
}
}
}
}
}
/// Dump a leaf node inline (no newline prefix, caller provides context).
fn dump_node_inline<Id: TreeId>(
ast: &Ast<Id>,
id: Id,
source: &str,
options: &DumpOptions,
type_check: Option<TypeCheckContext<'_>>,
external_to_parent: bool,
out: &mut String,
) {
let Some(node) = ast.get_node(id) else {
return;
};
write!(out, "{}", node.kind_name()).unwrap();
if options.show_locations {
let start = node.start_position();
let end = node.end_position();
write!(
out,
" [{},{}]-[{},{}]",
start.row + 1,
start.column + 1,
end.row + 1,
end.column + 1
)
.unwrap();
}
if options.show_content && node.is_named() {
let content = node_content(node, source);
if !content.is_empty() {
write!(out, " {content:?}").unwrap();
}
}
if options.show_abridged_source {
write_source_skeleton(ast, node, source, out);
if external_to_parent {
write!(out, " (external)").unwrap();
}
}
if let Some(context) = type_check {
if let Some(err) =
type_error_for_node(context.schema, node, context.expected, context.parent_field)
{
write!(out, " <-- ERROR: {err}").unwrap();
}
}
writeln!(out).unwrap();
}
fn is_leaf<Id: TreeId>(node: &Node<Id>) -> bool {
node.fields.is_empty()
}
enum SourceSkeleton {
Missing,
Text(String),
Invalid(String),
}
fn write_source_skeleton<Id: TreeId>(
ast: &Ast<Id>,
node: &Node<Id>,
source: &str,
out: &mut String,
) {
match source_skeleton(ast, node, source) {
SourceSkeleton::Missing => write!(out, " source=<no location>").unwrap(),
SourceSkeleton::Text(text) => write!(out, " source={text:?}").unwrap(),
SourceSkeleton::Invalid(error) => write!(out, " source=<invalid: {error}>").unwrap(),
}
}
fn node_source_range<Id: TreeId>(node: &Node<Id>) -> Option<crate::Range> {
match node.content {
NodeContent::Range(range) => Some(range),
_ => node.source_range,
}
}
fn is_external_child<Id: TreeId>(parent: &Node<Id>, child: &Node<Id>) -> bool {
let (Some(parent), Some(child)) = (node_source_range(parent), node_source_range(child)) else {
return false;
};
child.start_byte < parent.start_byte || child.end_byte > parent.end_byte
}
fn source_skeleton<Id: TreeId>(ast: &Ast<Id>, node: &Node<Id>, source: &str) -> SourceSkeleton {
let Some(parent) = node_source_range(node) else {
return SourceSkeleton::Missing;
};
let parent = parent.start_byte..parent.end_byte;
if parent.start > parent.end || source.get(parent.clone()).is_none() {
return SourceSkeleton::Invalid(format!(
"node range {}..{} is outside the source or not on UTF-8 boundaries",
parent.start, parent.end
));
}
let mut children = Vec::new();
for (field_id, child_ids) in &node.fields {
let field_name = if *field_id == CHILD_FIELD {
"child"
} else {
ast.field_name_for_id(*field_id).unwrap_or("?")
};
for child_id in child_ids {
let Some(child) = ast.get_node(*child_id) else {
continue;
};
if !child.is_named() {
continue;
}
let Some(child) = node_source_range(child) else {
continue;
};
let child = child.start_byte..child.end_byte;
if child.start > child.end || source.get(child.clone()).is_none() {
return SourceSkeleton::Invalid(format!(
"child range {}..{} is outside the source or not on UTF-8 boundaries",
child.start, child.end
));
}
if child.start < parent.start || child.end > parent.end {
continue;
}
if child.start == child.end {
continue;
}
children.push((child, field_name));
}
}
children.sort_by_key(|(range, field_name)| (range.start, range.end, *field_name));
let mut result = String::new();
let mut cursor = parent.start;
for (range, field_name) in children {
if cursor < range.start {
result.push_str(source.get(cursor..range.start).unwrap());
}
result.push('⟨');
result.push_str(field_name);
result.push('⟩');
cursor = cursor.max(range.end);
}
result.push_str(source.get(cursor..parent.end).unwrap());
SourceSkeleton::Text(result)
}
fn node_content<Id: TreeId>(node: &Node<Id>, source: &str) -> String {
match &node.content {
NodeContent::DynamicString(s) if !s.is_empty() => s.clone(),
_ => {
let range = node.byte_range();
if range.start < source.len() && range.end <= source.len() {
source[range.start..range.end].to_string()
} else {
String::new()
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{NodeContent, OutputAst, Point, Range};
use std::collections::BTreeMap;
fn range(start: usize, end: usize) -> Range {
Range {
start_byte: start,
end_byte: end,
start_point: Point::new(0, start),
end_point: Point::new(0, end),
}
}
fn dump_with_children(source: &str, parent_range: Range, children: &[(&str, Range)]) -> String {
let mut ast = OutputAst::with_schema(crate::schema::Schema::new());
let parent_kind = ast.register_kind("parent");
let child_kind = ast.register_kind("child");
let mut fields = BTreeMap::new();
for (field_name, range) in children {
let field = ast.register_field(field_name);
let child = ast.create_node_with_range(
child_kind,
NodeContent::Range(*range),
BTreeMap::new(),
true,
None,
);
fields.entry(field).or_insert_with(Vec::new).push(child);
}
let parent = ast.create_node_with_range(
parent_kind,
NodeContent::Range(parent_range),
fields,
true,
None,
);
ast.set_root(parent);
dump_ast_with_options(
&ast,
parent,
source,
&DumpOptions {
show_locations: false,
show_content: false,
show_abridged_source: true,
},
)
}
#[test]
fn source_skeleton_elides_direct_children_and_ignores_empty_ranges() {
let source = "αbefore(child)afterω";
let child_start = source.find("child").unwrap();
let child_end = child_start + "child".len();
let dump = dump_with_children(
source,
range(0, source.len()),
&[
("value", range(child_start, child_end)),
("marker", range(child_start, child_start)),
],
);
assert!(dump.starts_with("parent source=\"αbefore(⟨value⟩)afterω\"\n"));
}
#[test]
fn source_skeleton_preserves_unnamed_tokens() {
let source = "x = 1";
let language: tree_sitter::Language = tree_sitter_ruby::LANGUAGE.into();
let mut parser = tree_sitter::Parser::new();
parser.set_language(&language).unwrap();
let tree = parser.parse(source, None).unwrap();
let ast = Ast::from_tree_with_schema_and_source(
crate::schema::from_language(&language),
&tree,
&language,
source.as_bytes().to_vec(),
);
let dump = dump_ast_with_options(
&ast,
ast.get_root(),
source,
&DumpOptions {
show_locations: false,
show_content: false,
show_abridged_source: true,
},
);
assert!(dump.contains("assignment source=\"⟨left⟩ = ⟨right⟩\""));
}
#[test]
fn source_skeleton_validates_empty_child_ranges() {
let cases = [
(
"abcdef",
range(0, 6),
range(7, 7),
"child range 7..7 is outside the source or not on UTF-8 boundaries",
),
(
"αbc",
range(0, 4),
range(1, 1),
"child range 1..1 is outside the source or not on UTF-8 boundaries",
),
];
for (source, parent, child, error) in cases {
let dump = dump_with_children(source, parent, &[("marker", child)]);
assert!(
dump.starts_with(&format!("parent source=<invalid: {error}>\n")),
"unexpected dump: {dump}"
);
}
let dump = dump_with_children("abcdef", range(0, 3), &[("marker", range(4, 4))]);
assert!(dump.starts_with("parent source=\"abc\"\n"));
assert!(
dump.contains("marker: child source=\"\" (external)\n"),
"unexpected dump: {dump}"
);
}
#[test]
fn source_skeleton_keeps_adjacent_child_fields_separate() {
let source = "abcdef";
let dump = dump_with_children(
source,
range(0, source.len()),
&[("left", range(1, 3)), ("right", range(3, 5))],
);
assert!(dump.starts_with("parent source=\"a⟨left⟩⟨right⟩f\"\n"));
}
#[test]
fn source_skeleton_keeps_overlapping_child_fields_separate() {
let source = "abcdef";
let dump = dump_with_children(
source,
range(0, source.len()),
&[("left", range(1, 4)), ("right", range(3, 5))],
);
assert!(dump.starts_with("parent source=\"a⟨left⟩⟨right⟩f\"\n"));
}
#[test]
fn source_skeleton_marks_children_outside_the_parent() {
let source = "abcdefghi";
let dump = dump_with_children(source, range(0, 6), &[("child", range(7, 9))]);
assert!(dump.starts_with("parent source=\"abcdef\"\n"));
assert!(
dump.contains(" child source=\"hi\" (external)\n"),
"unexpected dump: {dump}"
);
}
}