donet_core/parser/
mod.rs

1/*
2    This file is part of Donet.
3
4    Copyright © 2024 Max Rodriguez
5
6    Donet is free software; you can redistribute it and/or modify
7    it under the terms of the GNU Affero General Public License,
8    as published by the Free Software Foundation, either version 3
9    of the License, or (at your option) any later version.
10
11    Donet is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14    GNU Affero General Public License for more details.
15
16    You should have received a copy of the GNU Affero General Public
17    License along with Donet. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Module of libdonet that contains the [`Context Free Grammar`] definition
21//! of the DC file language, the process of generating the DC file
22//! [`Abstract Syntax Tree`], and the process of converting the AST into the
23//! final DC file class hierarchy structure that is used by the Donet daemon
24//! at runtime to be able to interpret network messages that follow the
25//! network contract defined in the DC file(s).
26//!
27//! [`Context Free Grammar`]: https://en.wikipedia.org/wiki/Context-free_grammar
28//! [`Abstract Syntax Tree`]: https://en.wikipedia.org/wiki/Abstract_syntax_tree
29
30pub(crate) mod ast;
31pub mod error;
32pub(crate) mod lexer;
33pub(crate) mod parser;
34pub(crate) mod pipeline;
35mod semantics;
36
37use crate::dcfile::DCFile;
38use crate::dconfig::*;
39use anyhow::Result;
40use error::DCReadError;
41use pipeline::PipelineData;
42
43/// Tuple that represents an input file for the DC parser.
44/// The first item is the filename, the second item is the file content.
45pub(crate) type InputFile = (String, String);
46
47/// Runs the entire DC parser pipeline. The input is an array of strings
48/// that represent the input DC files in UTF-8, and the output is the final
49/// DC element tree data structure to be used by Donet.
50pub(crate) fn dcparse_pipeline<'a>(
51    config: DCFileConfig,
52    inputs: Vec<InputFile>,
53) -> Result<DCFile<'a>, DCReadError> {
54    // Create new pipeline data struct with [`DCFileConfig`]
55    let mut pipeline_data: PipelineData<'_> = PipelineData::from(config);
56
57    // Create codespan files for each DC file
58    for input in &inputs {
59        let _: usize = pipeline_data.files.add(&input.0, &input.1);
60    }
61
62    // Create an abstract syntax tree per DC file
63    for input in &inputs {
64        let lexer: lexer::Lexer<'_> = lexer::Lexer::new(&input.1);
65
66        let ast: ast::Root = match parser::parse(lexer) {
67            // See issue #19 for why LALR parser cannot return custom errors.
68            Err(err) => {
69                if let Some(parser_err) = err.clone().0 {
70                    // Extract parser error details
71                    let span: lexer::Span = parser_err.1;
72                    let token: lexer::DCToken = parser_err.0;
73                    let msg: String = err.1.to_owned();
74
75                    let diag: error::Diagnostic = error::Diagnostic::error(
76                        span,
77                        &mut pipeline_data,
78                        error::PipelineError::Parser(error::ParseError::Error(token, msg)),
79                    );
80
81                    pipeline_data
82                        .emit_diagnostic(diag.into())
83                        .expect("Failed to emit diagnostic.");
84                }
85
86                return Err(DCReadError::Syntax);
87            }
88            Ok(ast) => ast,
89        };
90
91        pipeline_data.syntax_trees.push(ast);
92        pipeline_data.next_file();
93    }
94
95    // Process all abstract syntax trees in semantic analyzer.
96    semantics::semantic_analyzer(&mut pipeline_data)
97}