asciidork_parser/
parse_result.rs1use std::fmt::{Debug, Formatter};
2
3use crate::internal::*;
4
5pub struct ParseResult<'arena> {
6 pub document: Document<'arena>,
7 pub warnings: Vec<Diagnostic>,
8 pub(crate) attr_locs: Vec<(SourceLocation, bool)>,
9 #[cfg(feature = "attr_ref_observation")]
10 pub attr_ref_observer: Option<Box<dyn AttrRefObserver>>,
11 lexer: Lexer<'arena>,
12}
13
14impl ParseResult<'_> {
15 pub fn line_number_with_offset(&self, loc: SourceLocation) -> (u32, u32) {
16 self.lexer.line_number_with_offset(loc)
17 }
18
19 pub fn source_file_at(&self, idx: u16) -> &SourceFile {
20 self.lexer.source_file_at(idx)
21 }
22
23 #[cfg(feature = "attr_ref_observation")]
24 pub fn take_attr_ref_observer<T: 'static>(&mut self) -> Option<T> {
25 let observer = self.attr_ref_observer.take()?;
26 let observer = observer as Box<dyn std::any::Any>;
27 Some(*observer.downcast::<T>().unwrap())
28 }
29}
30
31impl<'arena> From<Parser<'arena>> for ParseResult<'arena> {
32 fn from(parser: Parser<'arena>) -> Self {
33 ParseResult {
34 document: parser.document,
35 warnings: parser.errors.into_inner(),
36 attr_locs: parser.attr_locs,
37 #[cfg(feature = "attr_ref_observation")]
38 attr_ref_observer: parser.attr_ref_observer,
39 lexer: parser.lexer,
40 }
41 }
42}
43
44impl Debug for ParseResult<'_> {
45 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46 f.debug_struct("ParseResult")
47 .field("document", &self.document)
48 .field("warnings", &self.warnings)
49 .finish()
50 }
51}