lib_ruby_parser/
parser_result.rs

1use crate::source::Comment;
2use crate::source::DecodedInput;
3use crate::source::MagicComment;
4use crate::Diagnostic;
5use crate::Node;
6use crate::Token;
7
8/// Combination of all data that `Parser` can give you
9#[repr(C)]
10pub struct ParserResult {
11    /// Abstract Syntax Tree that was constructed from you code.
12    /// Contains `None` if the code gives no AST nodes
13    pub ast: Option<Box<Node>>,
14
15    /// List of tokens returned by a Lexer and consumed by a Parser.
16    /// Empty unless ParserOptions::record_tokens is set to true.
17    pub tokens: Vec<Token>,
18
19    /// List of all diagnostics (errors and warnings) that have been
20    /// recorded during lexing and parsing
21    pub diagnostics: Vec<Diagnostic>,
22
23    /// List of comments extracted from the source code.
24    pub comments: Vec<Comment>,
25
26    /// List of magic comments extracted from the source code.
27    pub magic_comments: Vec<MagicComment>,
28
29    /// Input that was used for parsing.
30    ///
31    /// Note: this input is not necessary the same byte array that
32    /// you passed to Parser::parse. If encoding of the input is
33    /// not `UTF-8` or `ASCII-8BIT/BINARY` Parser invokes `decoder`
34    /// that usually produces a different sequence of bytes.
35    ///
36    /// Pass **this** data to `Loc::source`, otherwise you'll get
37    /// incorrect source ranges.
38    pub input: DecodedInput,
39}
40
41impl std::fmt::Debug for ParserResult {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        f.debug_struct("ParserResult")
44            .field("ast", &self.ast)
45            .field("tokens", &self.tokens)
46            .field("diagnostics", &self.diagnostics)
47            .field("comments", &self.comments)
48            .field("magic_comments", &self.magic_comments)
49            .finish()
50    }
51}
52
53#[test]
54fn test_fmt() {
55    assert_eq!(
56        format!(
57            "{:?}",
58            ParserResult {
59                ast: None,
60                tokens: vec![],
61                diagnostics: vec![],
62                comments: vec![],
63                magic_comments: vec![],
64                input: DecodedInput::default()
65            }
66        ),
67        // All fields except `input`
68        "ParserResult { ast: None, tokens: [], diagnostics: [], comments: [], magic_comments: [] }"
69    )
70}