slicec/parsers/preprocessor/
parser.rs1use super::super::common::{ParserResult, SourceBlock};
4use super::construct_error_from;
5use super::grammar::lalrpop;
6use super::lexer::Lexer;
7use crate::diagnostics::Diagnostics;
8use std::collections::HashSet;
9
10macro_rules! implement_parse_function {
12 ($function_name:ident, $underlying_parser:ident, $return_type:ty $(,)?) => {
13 #[allow(clippy::result_unit_err)]
14 pub fn $function_name<'input>(mut self, input: impl Into<Lexer<'input>>) -> ParserResult<$return_type> {
15 match lalrpop::$underlying_parser::new().parse(&mut self, input.into()) {
16 Err(parse_error) => {
17 let error = construct_error_from(parse_error, self.file_name);
18 error.push_into(self.diagnostics);
19 Err(())
20 }
21 Ok(parse_value) => match self.diagnostics.has_errors() {
22 false => Ok(parse_value),
23 true => Err(()),
24 },
25 }
26 }
27 };
28}
29
30pub struct Preprocessor<'a> {
31 pub file_name: &'a str,
32 pub(super) defined_symbols: &'a mut HashSet<String>,
33 pub(super) diagnostics: &'a mut Diagnostics,
34}
35
36impl<'a> Preprocessor<'a> {
37 implement_parse_function!(
38 parse_slice_file,
39 SliceFileParser,
40 impl Iterator<Item = SourceBlock<'input>>,
41 );
42
43 pub fn new(file_name: &'a str, defined_symbols: &'a mut HashSet<String>, diagnostics: &'a mut Diagnostics) -> Self {
44 Preprocessor {
45 file_name,
46 defined_symbols,
47 diagnostics,
48 }
49 }
50}