Skip to content

Commit 5b2830c

Browse files
committed
(parser) ref: separate modifiers from types
1 parent d24285d commit 5b2830c

26 files changed

+192
-178
lines changed

src/parser/keyword/attributes.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
use core::fmt;
44

5-
use super::super::tree::binary::Binary;
6-
use super::super::tree::blocks::Block;
7-
use super::super::tree::literal::{Literal, Variable};
8-
use super::super::tree::unary::Unary;
9-
use super::super::tree::{FunctionCall, ListInitialiser, Ternary};
5+
use super::super::types::binary::Binary;
6+
use super::super::types::blocks::Block;
7+
use super::super::types::literal::{Literal, Variable};
8+
use super::super::types::unary::Unary;
9+
use super::super::types::{FunctionCall, ListInitialiser};
1010
use super::Ast;
11-
use super::types::PushInNode;
11+
use super::sort::PushInNode;
1212
use crate::lexer::api::Keyword;
13+
use crate::parser::types::ternary::Ternary;
1314

1415
macro_rules! define_attribute_keywords {
1516
($($name:ident: $($variant:ident)*,)*) => {

src/parser/keyword/control_flow/keyword.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::fmt;
22

3+
use super::super::super::types::Ast;
34
use super::node::ControlFlowNode;
4-
use crate::parser::tree::ast::Ast;
55

66
#[derive(Debug, PartialEq, Eq)]
77
pub enum ControlFlowKeyword {

src/parser/keyword/control_flow/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ pub mod node;
44
use keyword::ControlFlowKeyword;
55
use node::ControlFlowNode;
66

7+
use super::super::types::blocks::Block;
78
use super::Ast;
8-
use super::types::PushInNode;
9-
use crate::parser::tree::blocks::Block;
9+
use super::sort::PushInNode;
1010

1111
impl PushInNode for ControlFlowKeyword {
1212
fn push_in_node(self, node: &mut Ast) -> Result<(), String> {

src/parser/keyword/control_flow/node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use super::super::super::types::blocks::Block;
2+
use super::super::super::types::{Ast, ParensBlock};
13
use super::keyword::ControlFlowKeyword;
2-
use crate::parser::tree::ast::{Ast, ParensBlock};
3-
use crate::parser::tree::blocks::Block;
44

55
#[derive(Debug, PartialEq)]
66
pub enum ControlFlowNode {

src/parser/keyword/functions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use core::fmt;
22

3+
use super::super::types::literal::{Literal, Variable};
34
use super::Ast;
4-
use super::types::PushInNode;
5-
use crate::parser::tree::literal::{Literal, Variable};
5+
use super::sort::PushInNode;
66

77
#[derive(Debug, PartialEq, Eq)]
88
pub enum FunctionKeyword {

src/parser/keyword/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ extern crate alloc;
22
pub mod attributes;
33
pub mod control_flow;
44
pub mod functions;
5-
pub mod types;
5+
pub mod sort;
66

77
use alloc::vec::IntoIter;
88

99
use control_flow::is_node_case_context;
10-
use types::{KeywordParsing, PushInNode as _};
10+
use sort::{KeywordParsing, PushInNode as _};
1111

1212
use super::parse_content::parse_block;
1313
use super::state::ParsingState;
14-
use super::tree::ast::Ast;
14+
use super::types::Ast;
1515
use crate::Location;
1616
use crate::errors::api::CompileError;
1717
use crate::lexer::api::{Keyword, Token};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use super::super::tree::ast::Ast;
1+
use super::super::types::Ast;
2+
use super::super::types::literal::Literal;
23
use super::attributes::{AttributeKeyword as Attr, UnsortedAttributeKeyword as UnsortedAttr};
34
use super::control_flow::keyword::ControlFlowKeyword as CtrlFlow;
45
use super::functions::FunctionKeyword as Func;
56
use crate::lexer::api::Keyword;
6-
use crate::parser::tree::literal::Literal;
77

88
pub enum KeywordParsing {
99
Attr(Attr),

src/parser/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ pub mod api {
55
}
66

77
mod keyword;
8+
mod modifiers;
89
mod parse_content;
910
mod state;
1011
mod symbols;
11-
mod tree;
12+
mod types;
Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,16 @@
11
use core::cmp::Ordering;
22
use core::{fmt, mem};
33

4-
use super::binary::Binary;
5-
use super::blocks::Block;
4+
use super::super::types::binary::Binary;
5+
use super::super::types::blocks::Block;
6+
use super::super::types::literal::{Attribute, Literal, Variable, VariableName};
7+
use super::super::types::operator::{Associativity, Operator as _};
8+
use super::super::types::unary::Unary;
9+
use super::super::types::{FunctionCall, ListInitialiser};
610
use super::conversions::OperatorConversions;
7-
use super::literal::{Attribute, Literal, Variable, VariableName};
8-
use super::traits::{Associativity, Operator as _};
9-
use super::unary::Unary;
10-
use super::{FunctionCall, ListInitialiser, Ternary};
1111
use crate::EMPTY;
12-
use crate::parser::keyword::control_flow::node::ControlFlowNode;
13-
14-
/// Struct to represent the AST
15-
#[derive(Debug, Default, PartialEq)]
16-
pub enum Ast {
17-
Binary(Binary),
18-
Block(Block),
19-
ControlFlow(ControlFlowNode),
20-
#[default]
21-
Empty,
22-
FunctionCall(FunctionCall),
23-
Leaf(Literal),
24-
ListInitialiser(ListInitialiser),
25-
ParensBlock(ParensBlock),
26-
Ternary(Ternary),
27-
Unary(Unary),
28-
// TODO: CompoundLiteral(CompoundLiteral), Cast, & SpecialUnary(SpecialUnary),
29-
}
12+
use crate::parser::types::Ast;
13+
use crate::parser::types::ternary::Ternary;
3014

3115
impl Ast {
3216
/// Finds the leaf the most left possible, checks it is a variable and
@@ -133,9 +117,7 @@ impl Ast {
133117
//
134118
//
135119
// atomic: failure
136-
Self::ParensBlock(ParensBlock(old)) => {
137-
Err(successive_literal_error("Parenthesis group", old, node))
138-
}
120+
Self::ParensBlock(old) => Err(successive_literal_error("Parenthesis group", old, node)),
139121
Self::Leaf(old) => Err(successive_literal_error("Literal", old, node)),
140122
//
141123
//
@@ -339,36 +321,12 @@ impl fmt::Display for Ast {
339321
Self::Unary(val) => val.fmt(f),
340322
Self::Block(block) => block.fmt(f),
341323
Self::ListInitialiser(list_initialiser) => list_initialiser.fmt(f),
342-
Self::ParensBlock(ParensBlock(node)) => write!(f, "({node})"),
324+
Self::ParensBlock(parens) => parens.fmt(f),
343325
Self::ControlFlow(_) => todo!(),
344326
}
345327
}
346328
}
347329

348-
/// Struct to represent parenthesis
349-
///
350-
/// The [`Ast`] is what is inside of the parenthesis.
351-
///
352-
/// # Examples
353-
///
354-
/// If the C source is `(x = 2)`, the node is a [`ParensBlock`] with value the
355-
/// [`Ast`] of `x=2`.
356-
#[derive(Debug, Default, PartialEq)]
357-
pub struct ParensBlock(Box<Ast>);
358-
359-
impl ParensBlock {
360-
/// Adds parenthesis around an [`Ast`].
361-
///
362-
/// # Examples
363-
///
364-
/// ```ignore
365-
/// assert!(ParensBlock::make_parens_ast(Ast::Empty) == Ast::ParensBlock(Box::new(Ast::Empty)));
366-
/// ```
367-
pub fn make_parens_ast(node: Ast) -> Ast {
368-
Ast::ParensBlock(Self(Box::new(node)))
369-
}
370-
}
371-
372330
/// Makes an error [`String`] for consecutive literals.
373331
///
374332
/// If two consecutive literals are found, the [`crate::parser`] fails, and this
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use core::mem;
22

3-
use super::ast::Ast;
4-
use super::binary::{Binary, BinaryOperator};
3+
use super::super::types::Ast;
4+
use super::super::types::binary::{Binary, BinaryOperator};
5+
use super::super::types::operator::Operator;
6+
use super::super::types::unary::{Unary, UnaryOperator};
57
use super::make_lhs::make_lhs;
6-
use super::traits::Operator;
7-
use super::unary::{Unary, UnaryOperator};
8-
use super::{Ternary, TernaryOperator};
8+
use crate::parser::types::ternary::{Ternary, TernaryOperator};
99

1010
#[expect(clippy::missing_trait_methods)]
1111
impl OperatorConversions for BinaryOperator {

0 commit comments

Comments
 (0)