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