Skip to content

Commit 01c3e1d

Browse files
committed
refactor Ast
1 parent c74d50f commit 01c3e1d

File tree

3 files changed

+30
-38
lines changed

3 files changed

+30
-38
lines changed

rust/cbork-cddl-parser/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ pub enum Extension {
2424
/// - If there is an issue with parsing the CDDL input.
2525
pub fn validate_cddl(input: &mut String, extension: &Extension) -> anyhow::Result<()> {
2626
let ast = parser::parse_cddl(input, extension)?;
27-
preprocessor::process_ast(ast)?;
27+
let _ast = preprocessor::process_ast(ast)?;
2828
Ok(())
2929
}

rust/cbork-cddl-parser/src/parser.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! A parser for CDDL using the [pest](https://github.com/pest-parser/pest).
22
//! Utilized for parsing in accordance with RFC-8610, RFC-9165.
33
4-
use pest::{iterators::Pairs, Parser};
4+
use pest::{iterators::Pair, Parser};
55

66
use crate::Extension;
77

@@ -54,14 +54,13 @@ const POSTLUDE: &str = include_str!("grammar/postlude.cddl");
5454

5555
/// PEST Abstract Syntax Tree (AST) representing parsed CDDL syntax.
5656
#[derive(Debug)]
57-
#[allow(dead_code)]
58-
pub(crate) enum PestAst<'a> {
57+
pub(crate) enum Ast<'a> {
5958
/// Represents the AST for RFC-8610 CDDL rules.
60-
Rfc8610(Pairs<'a, rfc_8610::Rule>),
59+
Rfc8610(Vec<Pair<'a, rfc_8610::Rule>>),
6160
/// Represents the AST for RFC-9165 CDDL rules.
62-
Rfc9165(Pairs<'a, rfc_9165::Rule>),
61+
Rfc9165(Vec<Pair<'a, rfc_9165::Rule>>),
6362
/// Represents the AST for CDDL Modules rules.
64-
Cddl(Pairs<'a, cddl::Rule>),
63+
Cddl(Vec<Pair<'a, cddl::Rule>>),
6564
}
6665

6766
/// Parses and checks semantically a CDDL input string.
@@ -82,18 +81,22 @@ pub(crate) enum PestAst<'a> {
8281
/// - If there is an issue with parsing the CDDL input.
8382
pub(crate) fn parse_cddl<'a>(
8483
input: &'a mut String, extension: &Extension,
85-
) -> anyhow::Result<PestAst<'a>> {
84+
) -> anyhow::Result<Ast<'a>> {
8685
input.push_str("\n\n");
8786
input.push_str(POSTLUDE);
8887

8988
let ast = match extension {
9089
Extension::RFC8610 => {
91-
rfc_8610::Parser::parse(rfc_8610::Rule::cddl, input).map(PestAst::Rfc8610)?
90+
rfc_8610::Parser::parse(rfc_8610::Rule::cddl, input)
91+
.map(|p| Ast::Rfc8610(p.collect()))?
9292
},
9393
Extension::RFC9165 => {
94-
rfc_9165::Parser::parse(rfc_9165::Rule::cddl, input).map(PestAst::Rfc9165)?
94+
rfc_9165::Parser::parse(rfc_9165::Rule::cddl, input)
95+
.map(|p| Ast::Rfc9165(p.collect()))?
96+
},
97+
Extension::CDDL => {
98+
cddl::Parser::parse(cddl::Rule::cddl, input).map(|p| Ast::Cddl(p.collect()))?
9599
},
96-
Extension::CDDL => cddl::Parser::parse(cddl::Rule::cddl, input).map(PestAst::Cddl)?,
97100
};
98101
Ok(ast)
99102
}

rust/cbork-cddl-parser/src/preprocessor/mod.rs

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,37 @@
11
//! A CDDL AST preprocessor.
2-
//! First processing step, which takes a CDDL `AST` and returning a list of CDDL
3-
//! `Expression`.
42
//!
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
97
108
use anyhow::{anyhow, ensure};
11-
use pest::{
12-
iterators::{Pair, Pairs},
13-
RuleType,
14-
};
9+
use pest::{iterators::Pair, RuleType};
1510

16-
use crate::parser::{cddl, rfc_8610, rfc_9165, PestAst};
11+
use crate::parser::{cddl, rfc_8610, rfc_9165, Ast};
1712

1813
/// Processes the AST.
19-
pub(crate) fn process_ast(ast: PestAst) -> anyhow::Result<()> {
14+
pub(crate) fn process_ast(ast: Ast) -> anyhow::Result<Ast> {
2015
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)
2318
},
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)
3321
},
22+
Ast::Cddl(ast) => process_root(ast, cddl::Rule::cddl, cddl::Rule::expr).map(Ast::Cddl),
3423
}
35-
Ok(())
3624
}
3725

3826
/// Process the root rule of the AST.
3927
/// Returns a vector of expressions of the underlying AST.
4028
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,
4230
) -> 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."))?;
4433
ensure!(
45-
ast_root.as_rule() == root_rule && ast.next().is_none(),
34+
ast_root.as_rule() == root_rule && ast_iter.next().is_none(),
4635
"AST must have only one root rule, which must be a `{root_rule:?}` rule."
4736
);
4837
Ok(ast_root

0 commit comments

Comments
 (0)