Skip to content

Commit daab04c

Browse files
committed
Removed guard parser
1 parent f43d48a commit daab04c

5 files changed

Lines changed: 33 additions & 57 deletions

File tree

rusty_parser/src/expr/binary_expression.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rusty_pc::and::TupleCombiner;
33
use rusty_pc::*;
44

55
use crate::error::ParserError;
6-
use crate::expr::expression_pos_p;
6+
use crate::expr::{expression_pos_p, ws_expr_pos_p};
77
use crate::input::StringView;
88
use crate::pc_specific::{OrExpected, WithPos, lead_opt_ws, lead_ws};
99
use crate::tokens::{TokenType, any_token};
@@ -31,28 +31,29 @@ fn second_parser() -> impl Parser<
3131
Error = ParserError,
3232
> {
3333
operator()
34-
.then_with_in_context(third_parser(), is_keyword_op, TupleCombiner)
34+
.then_with_in_context(expr_after_binary_operator(), is_keyword_op, TupleCombiner)
3535
.to_option()
3636
}
3737

3838
fn is_keyword_op(op: &Positioned<Operator>) -> bool {
3939
op.element == Operator::And || op.element == Operator::Or || op.element == Operator::Modulo
4040
}
4141

42-
fn third_parser() -> impl Parser<StringView, bool, Output = ExpressionPos, Error = ParserError> {
42+
fn expr_after_binary_operator()
43+
-> impl Parser<StringView, bool, Output = ExpressionPos, Error = ParserError> {
44+
// boxed breaks apart the recursive type evaluation
4345
IifParser::new(
44-
super::guard::parser().to_fatal(),
45-
super::guard::parser().to_option().map_to_unit(),
46+
// the previous operator is a keyword op, must have whitespace or parenthesis
47+
ws_expr_pos_p()
48+
.boxed()
49+
.or_expected("expression after operator"),
50+
// the previous operator is a symbol, whitespace is optional
51+
lead_opt_ws(
52+
expression_pos_p()
53+
.boxed()
54+
.or_expected("expression after operator"),
55+
),
4656
)
47-
.and_keep_right(right_side_expr().no_context())
48-
}
49-
50-
/// Parses the right side expression, after having parsed the binary operator
51-
fn right_side_expr() -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {
52-
// boxed breaks apart the recursive type evaluation
53-
expression_pos_p()
54-
.or_expected("expression after operator")
55-
.boxed()
5657
}
5758

5859
fn non_bin_expr() -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {

rusty_parser/src/expr/guard.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.

rusty_parser/src/expr/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ mod binary_expression;
22
mod built_in_function_call;
33
pub mod file_handle;
44
mod function_call_or_array_element;
5-
mod guard;
65
mod integer_or_long_literal;
76
mod opt_second_expression;
87
mod parenthesis;

rusty_parser/src/expr/parsers.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ pub fn expression_pos_p() -> impl Parser<StringView, Output = ExpressionPos, Err
4949
/// <expr-in-parenthesis>
5050
/// ```
5151
pub fn ws_expr_pos_p() -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {
52-
// ws* ( expr )
53-
// ws+ expr
54-
preceded_by_ws(expression_pos_p())
52+
super::parenthesis::parser().or(lead_ws(expression_pos_p()))
5553
}
5654

5755
/// Parses an expression that is either followed by whitespace
@@ -84,12 +82,6 @@ pub fn ws_expr_pos_ws_p() -> impl Parser<StringView, Output = ExpressionPos, Err
8482
followed_by_ws(ws_expr_pos_p())
8583
}
8684

87-
fn preceded_by_ws(
88-
parser: impl Parser<StringView, Output = ExpressionPos, Error = ParserError>,
89-
) -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {
90-
super::guard::parser().and_keep_right(parser)
91-
}
92-
9385
fn followed_by_ws(
9486
parser: impl Parser<StringView, Output = ExpressionPos, Error = ParserError>,
9587
) -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {

rusty_parser/src/expr/unary_expression.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
use rusty_common::Positioned;
22
use rusty_pc::*;
33

4-
use crate::expr::{expression_pos_p, guard};
4+
use crate::expr::{expression_pos_p, ws_expr_pos_p};
55
use crate::input::StringView;
66
use crate::pc_specific::{OrExpected, WithPos, keyword};
77
use crate::tokens::minus_sign;
88
use crate::{ExpressionPos, ExpressionPosTrait, Keyword, ParserError, UnaryOperator};
99

1010
pub(super) fn parser() -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {
11-
seq2(
12-
unary_op(),
13-
expression_pos_p().or_expected("expression after unary operator"),
14-
|Positioned { element: op, pos }, expr| expr.apply_unary_priority_order(op, pos),
15-
)
11+
unary_minus()
12+
.or(unary_not())
13+
.map(|(Positioned { element: op, pos }, expr)| expr.apply_unary_priority_order(op, pos))
1614
}
1715

18-
fn unary_op() -> impl Parser<StringView, Output = Positioned<UnaryOperator>, Error = ParserError> {
16+
fn unary_minus()
17+
-> impl Parser<StringView, Output = (Positioned<UnaryOperator>, ExpressionPos), Error = ParserError>
18+
{
1919
minus_sign()
2020
.map(|_| UnaryOperator::Minus)
21-
.or(keyword(Keyword::Not)
22-
.and_keep_right(guard::parser().to_fatal())
23-
.map(|_| UnaryOperator::Not))
2421
.with_pos()
22+
.and_tuple(expression_pos_p().or_expected("expression after -"))
23+
}
24+
25+
fn unary_not()
26+
-> impl Parser<StringView, Output = (Positioned<UnaryOperator>, ExpressionPos), Error = ParserError>
27+
{
28+
keyword(Keyword::Not)
29+
.map(|_| UnaryOperator::Not)
30+
.with_pos()
31+
.and_tuple(ws_expr_pos_p().or_expected("expression after NOT"))
2532
}
2633

2734
#[cfg(test)]

0 commit comments

Comments
 (0)