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