Skip to content

Commit

Permalink
Change nested expression statement parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
TommYDeeee committed Apr 6, 2024
1 parent 00ed329 commit f3faf4e
Show file tree
Hide file tree
Showing 19 changed files with 227 additions and 143 deletions.
29 changes: 21 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,36 @@ fn api_walktrough() {

// On the left hand side we have a LITERAL token
let lhs = boolean_expr.lhs().unwrap();
let lhs_literal = lhs.variable_token().unwrap();
assert!(lhs_literal.kind() == SyntaxKind::VARIABLE);
assert_eq!(lhs_literal.text(), "$a");
let lhs_literal = match &lhs {
Expression::BooleanTerm(l) => l,
_ => unreachable!(),
};
assert!(lhs_literal.variable_token().unwrap().kind() == SyntaxKind::VARIABLE);
assert_eq!(lhs_literal.variable_token().unwrap().text(), "$a");

// On the right hand side we have a `BOOLEAN_EXPT` node
let rhs = boolean_expr.rhs().unwrap();

// It contains prefix expression which is essentially a `BOOLEAN_TERM` node
// in this case we have `NOT` node and nested `VARIABLE` node
let rhs_term = rhs.lhs().unwrap();
assert!(rhs_term.not_token().is_some());
let rhs_literal = match &rhs {
Expression::BooleanExpr(r) => r,
_ => unreachable!(),
};

let lhs_of_rhs = rhs_literal.lhs().unwrap();

let lhs = match &lhs_of_rhs {
Expression::BooleanTerm(l) => l,
_ => unreachable!(),
};

assert!(lhs.not_token().is_some());
assert!(
rhs_term.boolean_term().unwrap().bool_lit_token().unwrap().kind()
== SyntaxKind::BOOL_LIT
lhs.boolean_term().unwrap().bool_lit_token().unwrap().kind() == SyntaxKind::BOOL_LIT
);

assert_eq!(rhs_term.boolean_term().unwrap().bool_lit_token().unwrap().text(), "true");
assert_eq!(lhs.boolean_term().unwrap().bool_lit_token().unwrap().text(), "true");

//Last but not least, in any point we can obtain the syntax node
//for example let's obtain the syntax node for `EXPRESSION_STMT`
Expand Down
2 changes: 1 addition & 1 deletion src/parser/grammar/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ fn expr(p: &mut Parser, m: Option<Marker>, bp: u8) -> Option<CompletedMarker> {
Associativity::Right => op_bp,
};
expr(p, None, op_bp);
lhs = m.complete(p, EXPRESSION);
lhs = m.complete(p, EXPR_BODY);
}
Some(lhs)
}
Expand Down
3 changes: 3 additions & 0 deletions src/parser/syntax_kind/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ pub enum SyntaxKind {
FUNCTION_CALL_EXPR,
IN_RANGE,
EXPR_INDEX,
TERM,
EXPR_BODY,
NESTED_EXPR,
#[doc(hidden)]
__LAST,
}
Expand Down
48 changes: 43 additions & 5 deletions src/syntax/ast/expr_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::{
syntax::ast::{
self,
operators::{BinaryOp, LogicOp, UnaryOp},
operators::{BinaryOp, ExprOp, LogicOp},
support, AstNode, AstToken,
},
SyntaxToken, T,
Expand All @@ -26,6 +26,44 @@ use crate::{
// }
//}
//
impl ast::ExprBody {
pub fn op_details(&self) -> Option<(SyntaxToken, BinaryOp)> {
self.syntax().children_with_tokens().filter_map(|it| it.into_token()).find_map(|c| {
let bin_op = match c.kind() {
T![+] => BinaryOp::ExprOp(ExprOp::Add),
T![-] => BinaryOp::ExprOp(ExprOp::Sub),
T![*] => BinaryOp::ExprOp(ExprOp::Mul),
T![backslash] => BinaryOp::ExprOp(ExprOp::Div),
T![%] => BinaryOp::ExprOp(ExprOp::Mod),
T![&] => BinaryOp::ExprOp(ExprOp::BitAnd),
T![|] => BinaryOp::ExprOp(ExprOp::BitOr),
T![^] => BinaryOp::ExprOp(ExprOp::BitXor),
T![<<] => BinaryOp::ExprOp(ExprOp::Shl),
T![>>] => BinaryOp::ExprOp(ExprOp::Shr),
T![.] => BinaryOp::ExprOp(ExprOp::Dot),
_ => return None,
};
Some((c, bin_op))
})
}

pub fn op_kind(&self) -> Option<BinaryOp> {
self.op_details().map(|t| t.1)
}

pub fn op_token(&self) -> Option<SyntaxToken> {
self.op_details().map(|t| t.0)
}

pub fn lhs(&self) -> Option<ast::Expr> {
support::children(self.syntax()).next()
}

pub fn rhs(&self) -> Option<ast::Expr> {
support::children(self.syntax()).nth(1)
}
}

impl ast::XorRange {
pub fn lhs(&self) -> SyntaxToken {
self.syntax()
Expand All @@ -51,7 +89,7 @@ impl ast::HexJump {
self.syntax()
.children_with_tokens()
.filter(|e| !e.kind().is_trivia())
.nth(0)
.next()
.and_then(|e| e.into_token())
.unwrap()
}
Expand Down Expand Up @@ -86,12 +124,12 @@ impl ast::BooleanExpr {
self.op_details().map(|t| t.0)
}

pub fn lhs(&self) -> Option<ast::BooleanTerm> {
pub fn lhs(&self) -> Option<ast::Expression> {
support::children(self.syntax()).next()
}

pub fn rhs(&self) -> Option<ast::BooleanExpr> {
support::children(self.syntax()).next()
pub fn rhs(&self) -> Option<ast::Expression> {
support::children(self.syntax()).nth(1)
}

pub fn sub_exprs(&self) -> (Option<ast::Expr>, Option<ast::Expr>) {
Expand Down
Loading

0 comments on commit f3faf4e

Please sign in to comment.