|
1 | 1 | //! A CDDL AST preprocessor. |
2 | | -//! First processing step, which takes a CDDL `AST` and returning a list of CDDL |
3 | | -//! `Expression`. |
4 | 2 | //! |
5 | | -//! Preprocessor steps: |
6 | | -//! - Resolve #include and #import directives, by just adding the imported rules into the |
7 | | -//! final expression list |
8 | | -//! - Resolves all generics by taking the generic arguments and substituting it. |
| 3 | +//! - Validates the root rule of the AST to be a `cddl` rule. |
| 4 | +//! - Filters out all rules that are not `expr` rules. |
| 5 | +//! - (TODO) Resolve #include and #import directives, by just adding the imported rules |
| 6 | +//! into the final expression list |
9 | 7 |
|
10 | 8 | use anyhow::{anyhow, ensure}; |
11 | | -use pest::{ |
12 | | - iterators::{Pair, Pairs}, |
13 | | - RuleType, |
14 | | -}; |
| 9 | +use pest::{iterators::Pair, RuleType}; |
15 | 10 |
|
16 | | -use crate::parser::{cddl, rfc_8610, rfc_9165, PestAst}; |
| 11 | +use crate::parser::{cddl, rfc_8610, rfc_9165, Ast}; |
17 | 12 |
|
18 | 13 | /// Processes the AST. |
19 | | -pub(crate) fn process_ast(ast: PestAst) -> anyhow::Result<()> { |
| 14 | +pub(crate) fn process_ast(ast: Ast) -> anyhow::Result<Ast> { |
20 | 15 | match ast { |
21 | | - PestAst::Rfc8610(ast) => { |
22 | | - let _exprs = process_root(ast, rfc_8610::Rule::cddl, rfc_8610::Rule::expr)?; |
| 16 | + Ast::Rfc8610(ast) => { |
| 17 | + process_root(ast, rfc_8610::Rule::cddl, rfc_8610::Rule::expr).map(Ast::Rfc8610) |
23 | 18 | }, |
24 | | - PestAst::Rfc9165(ast) => { |
25 | | - let _exprs = process_root(ast, rfc_9165::Rule::cddl, rfc_9165::Rule::expr)?; |
26 | | - }, |
27 | | - PestAst::Cddl(ast) => { |
28 | | - let exprs = process_root(ast, cddl::Rule::cddl, cddl::Rule::expr)?; |
29 | | - |
30 | | - for expr in exprs { |
31 | | - println!("{:?}", expr.as_rule()); |
32 | | - } |
| 19 | + Ast::Rfc9165(ast) => { |
| 20 | + process_root(ast, rfc_9165::Rule::cddl, rfc_9165::Rule::expr).map(Ast::Rfc9165) |
33 | 21 | }, |
| 22 | + Ast::Cddl(ast) => process_root(ast, cddl::Rule::cddl, cddl::Rule::expr).map(Ast::Cddl), |
34 | 23 | } |
35 | | - Ok(()) |
36 | 24 | } |
37 | 25 |
|
38 | 26 | /// Process the root rule of the AST. |
39 | 27 | /// Returns a vector of expressions of the underlying AST. |
40 | 28 | fn process_root<R: RuleType>( |
41 | | - mut ast: Pairs<'_, R>, root_rule: R, expr_rule: R, |
| 29 | + ast: Vec<Pair<'_, R>>, root_rule: R, expr_rule: R, |
42 | 30 | ) -> anyhow::Result<Vec<Pair<'_, R>>> { |
43 | | - let ast_root = ast.next().ok_or(anyhow!("Empty AST."))?; |
| 31 | + let mut ast_iter = ast.into_iter(); |
| 32 | + let ast_root = ast_iter.next().ok_or(anyhow!("Empty AST."))?; |
44 | 33 | ensure!( |
45 | | - ast_root.as_rule() == root_rule && ast.next().is_none(), |
| 34 | + ast_root.as_rule() == root_rule && ast_iter.next().is_none(), |
46 | 35 | "AST must have only one root rule, which must be a `{root_rule:?}` rule." |
47 | 36 | ); |
48 | 37 | Ok(ast_root |
|
0 commit comments