-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathparse.rs
More file actions
751 lines (676 loc) · 24.1 KB
/
parse.rs
File metadata and controls
751 lines (676 loc) · 24.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
747
748
749
750
751
use std::{
fs, io,
ops::Range,
path::{Path, PathBuf},
};
use log::{debug, trace};
use miette::{NamedSource, SourceSpan};
use proc_macro2::Span;
use quote::quote;
use syn::{
spanned::Spanned as _, visit::Visit, Attribute, Error, Expr, ImplItemFn, ItemEnum, ItemFn,
ItemImpl, ItemMod, ItemStruct, ItemTrait, ItemUnion, Lit, Meta,
};
use crate::errors::HermesError;
/// A custom error type that associates a `syn::Error` with the file path
/// it originated from.
#[derive(Debug)]
pub struct ParseError {
error: Error,
source_file: Option<PathBuf>,
}
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(path) = &self.source_file {
write!(f, "{}: {}", path.display(), self.error)
} else {
write!(f, "{}", self.error)
}
}
}
impl std::error::Error for ParseError {}
/// The item from the original source code.
#[derive(Clone, Debug)]
pub enum ParsedItem {
Fn(ItemFn),
Struct(ItemStruct),
Enum(ItemEnum),
Union(ItemUnion),
Trait(ItemTrait),
Impl(ItemImpl),
ImplItemFn(ImplItemFn),
}
impl ParsedItem {
pub fn name(&self) -> Option<String> {
match self {
Self::Fn(item) => Some(item.sig.ident.to_string()),
Self::Struct(item) => Some(item.ident.to_string()),
Self::Enum(item) => Some(item.ident.to_string()),
Self::Union(item) => Some(item.ident.to_string()),
Self::Trait(item) => Some(item.ident.to_string()),
Self::ImplItemFn(item) => Some(item.sig.ident.to_string()),
Self::Impl(_) => None,
}
}
/// Returns the attributes on this item.
fn attrs(&self) -> &[Attribute] {
match self {
Self::Fn(item) => &item.attrs,
Self::Struct(item) => &item.attrs,
Self::Enum(item) => &item.attrs,
Self::Union(item) => &item.attrs,
Self::Trait(item) => &item.attrs,
Self::Impl(item) => &item.attrs,
Self::ImplItemFn(item) => &item.attrs,
}
}
}
/// A complete parsed item including its module path and the extracted Lean block.
#[derive(Debug)]
pub struct ParsedLeanItem {
pub item: ParsedItem,
pub module_path: Vec<String>,
lean_block: String,
source_file: Option<PathBuf>,
}
/// Represents a `mod foo;` declaration found in the source.
#[derive(Debug, Clone)]
pub struct UnloadedModule {
pub name: String,
/// The value of `#[path = "..."]` if present.
pub path_attr: Option<String>,
pub span: proc_macro2::Span,
}
/// Parses the given Rust source code and invokes the callback `f` for each item
/// annotated with a `/// ```lean` block.
///
/// While parsing, collects every `mod foo;` declaration and returns them all.
///
/// If parsing fails, or if any item has multiple Lean blocks, the callback is
/// invoked with an `Err`.
pub fn scan_compilation_unit<F>(source: &str, f: F) -> Vec<UnloadedModule>
where
F: FnMut(&str, Result<ParsedLeanItem, HermesError>),
{
let mut unloaded_modules = Vec::new();
scan_compilation_unit_internal(source, None, f, |m| unloaded_modules.push(m));
unloaded_modules
}
/// Like [`scan_compilation_unit`], but reads the source code from a file path.
///
/// Parsing errors and generated items will be associated with this file path.
pub fn read_file_and_scan_compilation_unit<F>(
path: &Path,
f: F,
) -> Result<(String, Vec<UnloadedModule>), io::Error>
where
F: FnMut(&str, Result<ParsedLeanItem, HermesError>),
{
log::trace!("read_file_and_scan_compilation_unit({:?})", path);
let source = fs::read_to_string(path)?;
let unloaded_modules = scan_compilation_unit(&source, f);
Ok((source, unloaded_modules))
}
fn scan_compilation_unit_internal<I, M>(
source: &str,
source_file: Option<PathBuf>,
mut item_cb: I,
mod_cb: M,
) where
I: FnMut(&str, Result<ParsedLeanItem, HermesError>),
M: FnMut(UnloadedModule),
{
trace!("Parsing source code into syn::File");
let file_name = {
let f = source_file
.as_ref()
.map(|p| p.display().to_string())
.unwrap_or_else(|| "<input>".to_string());
f
};
let _x = source_file
.as_ref()
.map(|p| p.display().to_string())
.unwrap_or_else(|| "<input>".to_string());
let named_source = miette::NamedSource::new(file_name, source.to_string());
let file = match syn::parse_file(source) {
Ok(file) => {
debug!("Successfully parsed source code into syn::File");
file
}
Err(e) => {
debug!("Failed to parse source code: {}", e);
item_cb(
source,
Err(HermesError::SynError {
src: named_source.clone(),
span: span_to_miette(e.span()),
msg: e.to_string(),
}),
);
return;
}
};
trace!("Initializing HermesVisitor to traverse AST");
let mut visitor = HermesVisitor {
current_path: Vec::new(),
inside_block: false,
item_cb,
mod_cb,
source_file,
source_code: source.to_string(),
named_source,
};
syn::visit::visit_file(&mut visitor, &file);
trace!("Finished traversing AST");
}
struct HermesVisitor<I, M> {
current_path: Vec<String>,
inside_block: bool,
item_cb: I,
mod_cb: M,
source_file: Option<PathBuf>,
source_code: String,
named_source: NamedSource<String>,
}
impl<I, M> HermesVisitor<I, M>
where
I: FnMut(&str, Result<ParsedLeanItem, HermesError>),
M: FnMut(UnloadedModule),
{
fn check_and_add(&mut self, item: ParsedItem, span: Span) {
// NEW: Check nesting
if self.inside_block {
// Only check attributes if we are in a body to see if we need to
// error. We want to avoid erroring on un-annotated local items.
if let Ok(Some(_)) = extract_lean_block(item.attrs()) {
// NOTE: It might be tempting to simply walk "out" until we find
// an item that we *can* name and pass that to Charon as a root.
// Unfortunately, that's unsound because there's no guarantee
// that the inner, annotated item is actually *called* from the
// outer function. Thus, it might be skipped by Charon entirely.
(self.item_cb)(
&self.source_code.as_str()[span.byte_range()],
Err(HermesError::NestedItemError {
src: self.named_source.clone(),
span: span_to_miette(span),
msg: "Hermes cannot verify items defined inside function bodies or other blocks.".to_string(),
}),
);
}
return;
}
let Range { start, end } = span.byte_range();
let source = &self.source_code.as_str()[start..end];
let attrs = item.attrs();
trace!("Checking item in module path `{:?}` for ```lean block", self.current_path);
match extract_lean_block(attrs) {
Ok(Some(lean_block)) => {
debug!("Found valid ```lean block for item in `{:?}`", self.current_path);
(self.item_cb)(
source,
Ok(ParsedLeanItem {
item,
module_path: self.current_path.clone(),
lean_block,
source_file: self.source_file.clone(),
}),
);
}
Ok(None) => {
trace!("No ```lean block found for item");
} // Skip item
Err(e) => {
debug!("Error extracting ```lean block: {}", e);
(self.item_cb)(
source,
Err(HermesError::DocBlockError {
src: self.named_source.clone(),
span: span_to_miette(e.span()),
msg: e.to_string(),
}),
);
}
}
}
}
impl<'ast, I, M> Visit<'ast> for HermesVisitor<I, M>
where
I: FnMut(&str, Result<ParsedLeanItem, HermesError>),
M: FnMut(UnloadedModule),
{
fn visit_item_mod(&mut self, node: &'ast ItemMod) {
let mod_name = node.ident.to_string();
// Check for unloaded modules (mod foo;)
if node.content.is_none() {
(self.mod_cb)(UnloadedModule {
name: mod_name.clone(),
path_attr: extract_path_attr(&node.attrs),
span: node.span(),
});
}
if let Some(path_attr) = extract_cfg_attr_path(&node.attrs) {
log::warn!(
"Module '{}' uses a #[cfg_attr(..., path = \"{}\")] directive. \
Hermes does not currently evaluate conditional paths; \
Hermes annotations in this file may be ignored.",
mod_name,
path_attr
);
}
trace!("Entering module: {}", mod_name);
self.current_path.push(mod_name);
syn::visit::visit_item_mod(self, node);
let popped = self.current_path.pop();
trace!("Exiting module: {}", popped.unwrap_or_default());
}
fn visit_item_fn(&mut self, node: &'ast ItemFn) {
trace!("Visiting Fn {}", node.sig.ident);
self.check_and_add(ParsedItem::Fn(node.clone()), node.span());
syn::visit::visit_item_fn(self, node);
}
fn visit_item_struct(&mut self, node: &'ast ItemStruct) {
trace!("Visiting Struct {}", node.ident);
self.check_and_add(ParsedItem::Struct(node.clone()), node.span());
syn::visit::visit_item_struct(self, node);
}
fn visit_item_enum(&mut self, node: &'ast ItemEnum) {
trace!("Visiting Enum {}", node.ident);
self.check_and_add(ParsedItem::Enum(node.clone()), node.span());
syn::visit::visit_item_enum(self, node);
}
fn visit_item_union(&mut self, node: &'ast ItemUnion) {
trace!("Visiting Union {}", node.ident);
self.check_and_add(ParsedItem::Union(node.clone()), node.span());
syn::visit::visit_item_union(self, node);
}
fn visit_item_trait(&mut self, node: &'ast ItemTrait) {
let name = node.ident.to_string();
trace!("Visiting Trait {}", name);
self.check_and_add(ParsedItem::Trait(node.clone()), node.span());
self.current_path.push(name);
syn::visit::visit_item_trait(self, node);
self.current_path.pop();
}
fn visit_block(&mut self, node: &'ast syn::Block) {
let old_inside = self.inside_block;
self.inside_block = true;
syn::visit::visit_block(self, node);
self.inside_block = old_inside;
}
fn visit_item_impl(&mut self, node: &'ast ItemImpl) {
trace!("Visiting Impl");
self.check_and_add(ParsedItem::Impl(node.clone()), node.span());
// If this is a trait impl, we use UFCS syntax (`<Type as Trait>`). If
// this is an inherent impl, we use the `Type: Type` syntax.
let self_ty = &node.self_ty;
let segment = if let Some((_, path, _)) = &node.trait_ {
quote! { <#self_ty as #path> }
} else {
quote! { #self_ty }
};
self.current_path.push(segment.to_string());
syn::visit::visit_item_impl(self, node);
self.current_path.pop();
}
fn visit_impl_item_fn(&mut self, node: &'ast ImplItemFn) {
trace!("Visiting ImplItemFn {}", node.sig.ident);
self.check_and_add(ParsedItem::ImplItemFn(node.clone()), node.span());
syn::visit::visit_impl_item_fn(self, node);
}
}
/// Helper to extract exactly one Lean block from a slice of attributes.
/// Returns `Ok(None)` if no block is found.
/// Returns `Err` if the block is malformed or multiple blocks are found.
fn extract_lean_block(attrs: &[Attribute]) -> Result<Option<String>, Error> {
let mut lean_blocks = Vec::new();
let mut in_block = false;
let mut current_block = String::new();
let mut block_start_span = None;
trace!("Searching {} attributes for ```lean blocks", attrs.len());
for attr in attrs {
if attr.path().is_ident("doc") {
if let Meta::NameValue(nv) = &attr.meta {
if let Expr::Lit(expr_lit) = &nv.value {
if let Lit::Str(lit_str) = &expr_lit.lit {
let text = lit_str.value();
let span = lit_str.span();
// Split by newlines in case it's a multiline `/** ... */` block
// or multi-line string literal in a `#[doc = "..."]` attribute.
for line in text.lines() {
// Trim leading whitespace but leave rest intact so we can identify "```lean"
let mut trimmed = line.trim_start();
// Let's strip any trailing whitespace for comparison purposes
let trimmed_end = trimmed.trim_end();
// Handle block comment `*` prefix heuristics
if trimmed_end.starts_with('*')
&& trimmed_end != "*"
&& !trimmed_end.starts_with("**")
{
trimmed = trimmed[1..].trim_start();
}
let check_val = trimmed.trim_end();
if !in_block {
if check_val == "```lean" {
trace!("Found opening ```lean tag");
in_block = true;
block_start_span = Some(span);
current_block.push_str(line);
current_block.push('\n');
}
} else {
current_block.push_str(line);
current_block.push('\n');
if check_val == "```" {
trace!("Found closing ``` tag");
in_block = false;
lean_blocks
.push((current_block.clone(), block_start_span.unwrap()));
current_block.clear();
}
}
}
}
}
}
}
}
if in_block {
debug!("Unclosed ```lean block detected");
return Err(Error::new(
block_start_span.unwrap(),
"Unclosed ```lean block in documentation",
));
}
if lean_blocks.is_empty() {
trace!("Finished attribute scan: no lean blocks found");
Ok(None)
} else if lean_blocks.len() > 1 {
debug!("Multiple ```lean blocks found");
let mut err = Error::new(lean_blocks[1].1, "Multiple lean blocks found on a single item");
for block in &lean_blocks[2..] {
err.combine(Error::new(block.1, "Additional lean block found here"));
}
Err(err)
} else {
debug!("Successfully extracted exactly one ```lean block");
Ok(Some(lean_blocks.into_iter().next().unwrap().0))
}
}
/// Extracts the `...` from the first `#[path = "..."]` attribute found, if any.
fn extract_path_attr(attrs: &[Attribute]) -> Option<String> {
for attr in attrs {
if attr.path().is_ident("path") {
if let Meta::NameValue(nv) = &attr.meta {
if let Expr::Lit(expr_lit) = &nv.value {
if let Lit::Str(lit_str) = &expr_lit.lit {
return Some(lit_str.value());
}
}
}
}
}
None
}
/// Extracts the `...` from `#[cfg_attr(..., path = "...")]` if present.
fn extract_cfg_attr_path(attrs: &[Attribute]) -> Option<String> {
for attr in attrs {
if attr.path().is_ident("cfg_attr") {
let mut found_path = None;
// The syntax is `#[cfg_attr(condition, attr1, attr2, ...)]`; we
// parse the nested meta items.
let _ = attr.parse_nested_meta(|meta| {
if meta.path.is_ident("path") {
if let Ok(value) = meta.value() {
if let Ok(lit) = value.parse::<Lit>() {
if let Lit::Str(lit_str) = lit {
found_path = Some(lit_str.value());
}
}
}
}
Ok(())
});
if let Some(p) = found_path {
return Some(p);
}
}
}
None
}
fn span_to_miette(span: proc_macro2::Span) -> SourceSpan {
let range = span.byte_range();
SourceSpan::new(range.start.into(), range.end - range.start)
}
#[cfg(test)]
mod tests {
use syn::spanned::Spanned as _;
use super::*;
fn parse_to_vec(code: &str) -> Vec<(String, Result<ParsedLeanItem, HermesError>)> {
let mut items = Vec::new();
scan_compilation_unit(code, |src, res| items.push((src.to_string(), res)));
items
}
#[test]
fn test_parse_lean_block() {
let code = r#"
/// ```lean
/// theorem foo : True := by trivial
/// ```
fn foo() {}
"#;
let items = parse_to_vec(code);
let (src, res) = items.into_iter().next().unwrap();
let item = res.unwrap();
assert_eq!(
src,
"/// ```lean
/// theorem foo : True := by trivial
/// ```
fn foo() {}"
);
assert!(matches!(item.item, ParsedItem::Fn(_)));
assert_eq!(item.lean_block, " ```lean\n theorem foo : True := by trivial\n ```\n");
assert!(item.source_file.is_none());
}
#[test]
fn test_multiple_lean_blocks_error() {
let code = r#"
/// ```lean
/// a
/// ```
/// ```lean
/// b
/// ```
fn foo() {}
"#;
let items = parse_to_vec(code);
let (src, res) = items.into_iter().next().unwrap();
let err = res.unwrap_err();
assert_eq!(
src,
"/// ```lean
/// a
/// ```
/// ```lean
/// b
/// ```
fn foo() {}"
);
assert!(err.to_string().contains("Multiple lean blocks"));
}
#[test]
fn test_unclosed_lean_block() {
let code = r#"
/// ```lean
/// a
fn foo() {}
"#;
let items = parse_to_vec(code);
let (src, res) = items.into_iter().next().unwrap();
let err = res.unwrap_err();
assert_eq!(
src,
"/// ```lean
/// a
fn foo() {}"
);
assert!(err.to_string().contains("Unclosed"));
}
#[test]
fn test_module_paths() {
let code = r#"
mod a {
mod b {
/// ```lean
/// ```
fn foo() {}
}
}
"#;
let items = parse_to_vec(code);
let (src, res) = items.into_iter().next().unwrap();
let item = res.unwrap();
assert_eq!(
src,
"/// ```lean
/// ```
fn foo() {}"
);
assert_eq!(item.module_path, vec!["a", "b"]);
}
#[test]
fn test_visit_in_file() {
let code = r#"
/// ```lean
/// a
fn foo() {}
"#;
let mut items = Vec::new();
scan_compilation_unit_internal(
code,
Some(Path::new("src/foo.rs").to_path_buf()),
|source: &str, res| items.push((source.to_string(), res)),
|_| {},
);
let (src, res) = items.into_iter().next().unwrap();
assert_eq!(
src,
"/// ```lean
/// a
fn foo() {}"
);
let rep = format!("{:?}", miette::Report::new(res.unwrap_err()));
assert!(rep.contains("src/foo.rs"));
assert!(rep.contains("Unclosed"));
}
#[test]
fn test_span_multiple_modules_precision() {
let source = "mod a {
mod b {
/// ```lean
/// theorem a : True := trivial
/// ```
fn bar() {}
}
}
mod c {
/// ```lean
/// theorem b : False := sorry
/// ```
fn baz() {}
}
";
let mut items = Vec::new();
scan_compilation_unit(source, |_src, res| items.push(res));
let i1 = items[0].as_ref().unwrap();
let i2 = items[1].as_ref().unwrap();
// Note that the span of `attrs()[0]` is only the very first line of the doc comment:
// `/// ```lean`.
// The rest of the comment lines are actually separate attributes `attrs()[1]`, `attrs()[2]`, etc.
// because `///` style doc comments generate one `#[doc="..."]` attribute per line.
let span1_start = i1.item.attrs().first().unwrap().span().byte_range().start;
let span1_end = i1.item.attrs().last().unwrap().span().byte_range().end;
let span2_start = i2.item.attrs().first().unwrap().span().byte_range().start;
let span2_end = i2.item.attrs().last().unwrap().span().byte_range().end;
let text1 = &source[span1_start..span1_end];
let text2 = &source[span2_start..span2_end];
assert_eq!(text1, "/// ```lean\n /// theorem a : True := trivial\n /// ```");
assert_eq!(text2, "/// ```lean\n /// theorem b : False := sorry\n /// ```");
}
#[test]
fn test_multiple_parsing_failures_output() {
let code1 = r#"
/// ```lean
/// unclosed block 1
fn bad_doc_1() {}
/// ```lean
/// unclosed block 2
fn bad_doc_2() {}
"#;
let code2 = r#"
fn bad_syntax_1() {
let x =
}
fn bad_syntax_2() {
let y =
}
"#;
let path = std::path::Path::new("src/foo.rs");
let mut items = Vec::new();
scan_compilation_unit_internal(
code1,
Some(path.to_path_buf()),
|_src, res| items.push(res),
|_| {},
);
scan_compilation_unit_internal(
code2,
Some(path.to_path_buf()),
|_src, res| items.push(res),
|_| {},
);
let mut report_string = String::new();
for err in items.into_iter().filter_map(|r| r.err()) {
let mut buf = String::new();
miette::GraphicalReportHandler::new()
.with_width(80)
.with_links(false)
.with_theme(miette::GraphicalTheme::unicode_nocolor())
.render_report(&mut buf, &err)
.unwrap();
report_string.push_str(&buf);
report_string.push('\n');
}
let expected = r#"hermes::doc_block
× Documentation block error: Unclosed ```lean block in documentation
╭─[src/foo.rs:2:13]
1 │
2 │ /// ```lean
· ─────┬─────
· ╰── problematic block
3 │ /// unclosed block 1
╰────
hermes::doc_block
× Documentation block error: Unclosed ```lean block in documentation
╭─[src/foo.rs:6:13]
5 │
6 │ /// ```lean
· ─────┬─────
· ╰── problematic block
7 │ /// unclosed block 2
╰────
hermes::syn_error
× Syntax error in Rust source: unexpected end of input, expected an
│ expression
╭─[src/foo.rs:4:13]
3 │ let x =
4 │ }
· ┬
· ╰── here
5 │
╰────
"#;
assert_eq!(report_string.trim(), expected.trim());
}
}