Skip to content

Commit

Permalink
Changes due to easier integration
Browse files Browse the repository at this point in the history
  • Loading branch information
TommYDeeee committed Apr 4, 2024
1 parent e00625a commit 00ed329
Show file tree
Hide file tree
Showing 54 changed files with 1,072 additions and 917 deletions.
34 changes: 25 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,14 @@ fn api_walktrough() {
// For the condition part, we can similarly get its body which is
// an `BOOLEAN_EXPR` node
let condition = block.condition().unwrap();
let boolean_expr = condition.boolean_expr().unwrap();
let expression_stmt = condition.expression_stmt().unwrap();

let expression = expression_stmt.expression().unwrap();

let boolean_expr = match &expression {
Expression::BooleanExpr(e) => e,
_ => unreachable!(),
};

// Now we can obtain `lhs`, `rhs` or `op` nodes for top level expression
// in this case we have `OR` operator
Expand All @@ -152,13 +159,12 @@ fn api_walktrough() {
assert!(lhs_literal.kind() == SyntaxKind::VARIABLE);
assert_eq!(lhs_literal.text(), "$a");

println!("{:#?}", boolean_expr);
// 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.boolean_term().unwrap();
let rhs_term = rhs.lhs().unwrap();
assert!(rhs_term.not_token().is_some());
assert!(
rhs_term.boolean_term().unwrap().bool_lit_token().unwrap().kind()
Expand All @@ -169,7 +175,7 @@ fn api_walktrough() {

//Last but not least, in any point we can obtain the syntax node
//for example let's obtain the syntax node for `EXPRESSION_STMT`
let expression_stmt_syntax = boolean_expr.syntax();
let expression_stmt_syntax = expression_stmt.syntax();

assert_eq!(expression_stmt_syntax.text().to_string(), "$a or not true");

Expand All @@ -181,7 +187,7 @@ fn api_walktrough() {

// We can also obtain the children
let children = expression_stmt_syntax.first_child_or_token().unwrap();
assert_eq!(children.kind(), SyntaxKind::BOOLEAN_TERM);
assert_eq!(children.kind(), SyntaxKind::BOOLEAN_EXPR);

// and also the next sibling, which in this layer can be also a whitespace
let next_sibling = parent.next_sibling_or_token().unwrap();
Expand Down Expand Up @@ -210,9 +216,12 @@ fn api_walktrough() {
NodeOrToken::Token(it) => it.kind(),
};
if i == 0 {
assert_eq!(kind, SyntaxKind::BOOLEAN_EXPR);
assert_eq!(kind, SyntaxKind::EXPRESSION_STMT);
}
if i == 1 {
assert_eq!(kind, SyntaxKind::BOOLEAN_EXPR);
}
if i == 2 {
assert_eq!(kind, SyntaxKind::BOOLEAN_TERM);
}
if i == 3 {
Expand All @@ -225,7 +234,7 @@ fn api_walktrough() {
NodeOrToken::Token(it) => it.kind(),
};
if i == 4 {
assert_eq!(kind, SyntaxKind::BOOLEAN_TERM);
assert_eq!(kind, SyntaxKind::VARIABLE);
}
}
}
Expand Down Expand Up @@ -263,11 +272,18 @@ fn api_walktrough() {
assert!(rule.identifier_token().unwrap().text() == "test_rule");
let block = rule.body().unwrap();
let condition = block.condition().unwrap();
let condition_body = condition.boolean_term().unwrap();
let condition_body = condition.expression_stmt().unwrap();

let expression = condition_body.expression().unwrap();

let boolean_term = match &expression {
Expression::BooleanTerm(e) => e,
_ => unreachable!(),
};

// The operator is wrong, therefore we only have
// a variable
assert!(condition_body.variable_token().unwrap().kind() == SyntaxKind::VARIABLE);
assert!(boolean_term.variable_token().unwrap().kind() == SyntaxKind::VARIABLE);

// and we can obtain the error token
let error_token = block
Expand Down
2 changes: 2 additions & 0 deletions src/parser/grammar/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ fn int_range(p: &mut Parser) {
/// It consists of a list of expressions
/// Pratt parser is used to parse expressions
pub(super) fn condition_body(p: &mut Parser) {
let m = p.start();
boolean_expr(p, None, 1);
m.complete(p, EXPRESSION_STMT);
}

enum Associativity {
Expand Down
58 changes: 39 additions & 19 deletions src/syntax/ast/expr_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,45 @@ use crate::{
// }
//}
//
//impl ast::XorRange {
// pub fn lhs(&self) -> Option<ast::Literal> {
// support::children(self.syntax()).next()
// }
//
// pub fn rhs(&self) -> Option<ast::Literal> {
// support::children(self.syntax()).nth(1)
// }
//}
//
//impl ast::HexJump {
// pub fn lhs(&self) -> Option<ast::Literal> {
// support::children(self.syntax()).next()
// }
//
// pub fn rhs(&self) -> Option<ast::Literal> {
// support::children(self.syntax()).nth(1)
// }
//}
impl ast::XorRange {
pub fn lhs(&self) -> SyntaxToken {
self.syntax()
.children_with_tokens()
.filter(|e| !e.kind().is_trivia())
.nth(1)
.and_then(|e| e.into_token())
.unwrap()
}

pub fn rhs(&self) -> SyntaxToken {
self.syntax()
.children_with_tokens()
.filter(|e| !e.kind().is_trivia())
.nth(3)
.and_then(|e| e.into_token())
.unwrap()
}
}

impl ast::HexJump {
pub fn lhs(&self) -> SyntaxToken {
self.syntax()
.children_with_tokens()
.filter(|e| !e.kind().is_trivia())
.nth(0)
.and_then(|e| e.into_token())
.unwrap()
}

pub fn rhs(&self) -> SyntaxToken {
self.syntax()
.children_with_tokens()
.filter(|e| !e.kind().is_trivia())
.last()
.and_then(|e| e.into_token())
.unwrap()
}
}
//
impl ast::BooleanExpr {
pub fn op_details(&self) -> Option<(SyntaxToken, BinaryOp)> {
Expand Down
77 changes: 70 additions & 7 deletions src/syntax/ast/generated/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,7 @@ impl Condition {
pub fn colon_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![:])
}
pub fn boolean_expr(&self) -> Option<BooleanExpr> {
support::child(&self.syntax)
}
pub fn boolean_term(&self) -> Option<BooleanTerm> {
pub fn expression_stmt(&self) -> Option<ExpressionStmt> {
support::child(&self.syntax)
}
}
Expand Down Expand Up @@ -437,15 +434,21 @@ impl XorRange {
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BooleanExpr {
pub struct ExpressionStmt {
pub(crate) syntax: SyntaxNode,
}
impl BooleanExpr {
pub fn boolean_term(&self) -> Option<BooleanTerm> {
impl ExpressionStmt {
pub fn expression(&self) -> Option<Expression> {
support::child(&self.syntax)
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BooleanExpr {
pub(crate) syntax: SyntaxNode,
}
impl BooleanExpr {}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct BooleanTerm {
pub(crate) syntax: SyntaxNode,
Expand Down Expand Up @@ -934,6 +937,12 @@ impl VariableWildcard {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Expression {
BooleanExpr(BooleanExpr),
BooleanTerm(BooleanTerm),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Term {
PrimaryExpr(PrimaryExpr),
Expand Down Expand Up @@ -1328,6 +1337,21 @@ impl AstNode for XorRange {
&self.syntax
}
}
impl AstNode for ExpressionStmt {
fn can_cast(kind: SyntaxKind) -> bool {
kind == EXPRESSION_STMT
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
if Self::can_cast(syntax.kind()) {
Some(Self { syntax })
} else {
None
}
}
fn syntax(&self) -> &SyntaxNode {
&self.syntax
}
}
impl AstNode for BooleanExpr {
fn can_cast(kind: SyntaxKind) -> bool {
kind == BOOLEAN_EXPR
Expand Down Expand Up @@ -1643,6 +1667,35 @@ impl AstNode for VariableWildcard {
&self.syntax
}
}
impl From<BooleanExpr> for Expression {
fn from(node: BooleanExpr) -> Expression {
Expression::BooleanExpr(node)
}
}
impl From<BooleanTerm> for Expression {
fn from(node: BooleanTerm) -> Expression {
Expression::BooleanTerm(node)
}
}
impl AstNode for Expression {
fn can_cast(kind: SyntaxKind) -> bool {
matches!(kind, BOOLEAN_EXPR | BOOLEAN_TERM)
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() {
BOOLEAN_EXPR => Expression::BooleanExpr(BooleanExpr { syntax }),
BOOLEAN_TERM => Expression::BooleanTerm(BooleanTerm { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxNode {
match self {
Expression::BooleanExpr(it) => &it.syntax,
Expression::BooleanTerm(it) => &it.syntax,
}
}
}
impl From<PrimaryExpr> for Term {
fn from(node: PrimaryExpr) -> Term {
Term::PrimaryExpr(node)
Expand Down Expand Up @@ -1732,6 +1785,11 @@ impl AstNode for AnyHasComments {
&self.syntax
}
}
impl std::fmt::Display for Expression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for Term {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
Expand Down Expand Up @@ -1867,6 +1925,11 @@ impl std::fmt::Display for XorRange {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for ExpressionStmt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for BooleanExpr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
Expand Down
5 changes: 3 additions & 2 deletions tests/test1.out
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ [email protected]
[email protected] "condition"
[email protected] ":"
[email protected] "\n\t\t"
[email protected]
[email protected] "$a"
[email protected]
[email protected]
[email protected] "$a"
[email protected] "\n"
[email protected] "}"
[email protected] "\n"
5 changes: 3 additions & 2 deletions tests/test10.out
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ [email protected]
[email protected] "condition"
[email protected] ":"
[email protected] "\n\t\t"
[email protected]
[email protected] "$a"
[email protected]
[email protected]
[email protected] "$a"
[email protected] "\n"
[email protected] "}"
[email protected] "\n"
5 changes: 3 additions & 2 deletions tests/test11.out
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ [email protected]
[email protected] "condition"
[email protected] ":"
[email protected] "\n\t\t"
[email protected]
[email protected] "$a"
[email protected]
[email protected]
[email protected] "$a"
[email protected] "\n"
[email protected] "}"
10 changes: 6 additions & 4 deletions tests/test12.out
5 changes: 3 additions & 2 deletions tests/test13.out
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ [email protected]
[email protected] "condition"
[email protected] ":"
[email protected] "\n\t\t"
[email protected]
[email protected] "$a"
[email protected]
[email protected]
[email protected] "$a"
[email protected] "\n"
[email protected] "}"
[email protected] "\n"
5 changes: 3 additions & 2 deletions tests/test14.out
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ [email protected]
[email protected] "condition"
[email protected] ":"
[email protected] "\n\t\t"
[email protected]
[email protected] "$a"
[email protected]
[email protected]
[email protected] "$a"
[email protected] "\n"
[email protected] "}"
[email protected] "\n"
5 changes: 3 additions & 2 deletions tests/test15.out
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ [email protected]
[email protected] "condition"
[email protected] ":"
[email protected] "\n\t\t"
[email protected]
[email protected] "$a"
[email protected]
[email protected]
[email protected] "$a"
[email protected] "\n"
[email protected] "}"
[email protected] "\n"
Loading

0 comments on commit 00ed329

Please sign in to comment.