solang_parser/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2
3#![doc = include_str!("../README.md")]
4#![warn(missing_debug_implementations, missing_docs)]
5
6use crate::lexer::LexicalError;
7use crate::lexer::Token;
8use crate::pt::CodeLocation;
9use crate::pt::Loc;
10use diagnostics::Diagnostic;
11use lalrpop_util::ParseError;
12
13pub mod diagnostics;
14pub mod doccomment;
15pub mod helpers;
16pub mod lexer;
17pub mod pt;
18
19#[cfg(test)]
20mod tests;
21
22#[allow(
23    clippy::needless_lifetimes,
24    clippy::type_complexity,
25    clippy::ptr_arg,
26    clippy::just_underscores_and_digits,
27    clippy::empty_line_after_outer_attr,
28    clippy::large_enum_variant
29)]
30mod solidity {
31    include!(concat!(env!("OUT_DIR"), "/solidity.rs"));
32}
33
34/// Parses a Solidity file.
35pub fn parse(
36    src: &str,
37    file_no: usize,
38) -> Result<(pt::SourceUnit, Vec<pt::Comment>), Vec<Diagnostic>> {
39    // parse phase
40    let mut comments = Vec::new();
41    let mut lexer_errors = Vec::new();
42    let mut lex = lexer::Lexer::new(src, file_no, &mut comments, &mut lexer_errors);
43
44    let mut parser_errors = Vec::new();
45    let res = solidity::SourceUnitParser::new().parse(src, file_no, &mut parser_errors, &mut lex);
46
47    let mut diagnostics = Vec::with_capacity(lex.errors.len() + parser_errors.len());
48    for lexical_error in lex.errors {
49        diagnostics.push(Diagnostic::parser_error(
50            lexical_error.loc(),
51            lexical_error.to_string(),
52        ))
53    }
54
55    for e in parser_errors {
56        diagnostics.push(parser_error_to_diagnostic(&e.error, file_no));
57    }
58
59    match res {
60        Err(e) => {
61            diagnostics.push(parser_error_to_diagnostic(&e, file_no));
62            Err(diagnostics)
63        }
64        _ if !diagnostics.is_empty() => Err(diagnostics),
65        Ok(res) => Ok((res, comments)),
66    }
67}
68
69/// Convert lalrop parser error to a Diagnostic
70fn parser_error_to_diagnostic(
71    error: &ParseError<usize, Token, LexicalError>,
72    file_no: usize,
73) -> Diagnostic {
74    match error {
75        ParseError::InvalidToken { location } => Diagnostic::parser_error(
76            Loc::File(file_no, *location, *location),
77            "invalid token".to_string(),
78        ),
79        ParseError::UnrecognizedToken {
80            token: (l, token, r),
81            expected,
82        } => Diagnostic::parser_error(
83            Loc::File(file_no, *l, *r),
84            format!(
85                "unrecognised token '{}', expected {}",
86                token,
87                expected.join(", ")
88            ),
89        ),
90        ParseError::User { error } => Diagnostic::parser_error(error.loc(), error.to_string()),
91        ParseError::ExtraToken { token } => Diagnostic::parser_error(
92            Loc::File(file_no, token.0, token.2),
93            format!("extra token '{}' encountered", token.0),
94        ),
95        ParseError::UnrecognizedEof { expected, location } => Diagnostic::parser_error(
96            Loc::File(file_no, *location, *location),
97            format!("unexpected end of file, expecting {}", expected.join(", ")),
98        ),
99    }
100}