Skip to content

Commit 9d37097

Browse files
committed
chore: Removed obscure ExtractExpression trait
Removed obscure `ExtractExpression` trait in favor of a function.
1 parent 2ce1fd6 commit 9d37097

4 files changed

Lines changed: 34 additions & 45 deletions

File tree

rusty_parser/src/specific/core/dim_name.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,20 @@ mod array_dimensions {
137137
// expr ws+ TO ws+ expr (e.g. 1 TO 10)
138138
// paren_expr ws* TO ws* paren_expr
139139
fn array_dimension_p() -> impl Parser<RcStringView, Output = ArrayDimension> {
140-
opt_second_expression_after_keyword(expression_pos_p(), Keyword::To).map(|(l, opt_r)| {
141-
match opt_r {
142-
Some(r) => ArrayDimension {
143-
lbound: Some(l),
144-
ubound: r,
145-
},
146-
None => ArrayDimension {
147-
lbound: None,
148-
ubound: l,
149-
},
150-
}
140+
opt_second_expression_after_keyword(
141+
expression_pos_p(),
142+
Keyword::To,
143+
ExpressionTrait::is_parenthesis,
144+
)
145+
.map(|(l, opt_r)| match opt_r {
146+
Some(r) => ArrayDimension {
147+
lbound: Some(l),
148+
ubound: r,
149+
},
150+
None => ArrayDimension {
151+
lbound: None,
152+
ubound: l,
153+
},
151154
})
152155
}
153156
}

rusty_parser/src/specific/core/for_loop.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::specific::core::expression::expr_pos_ws_p;
44
use crate::specific::core::expression::property;
55
use crate::specific::core::expression::ws_expr_pos_p;
66
use crate::specific::core::opt_second_expression::opt_second_expression_after_keyword;
7-
use crate::specific::core::opt_second_expression::ExtractExpression;
87
use crate::specific::core::statements::ZeroOrMoreStatements;
98
use crate::specific::pc_specific::*;
109
use crate::specific::*;
@@ -42,14 +41,10 @@ fn parse_for_step_p() -> impl Parser<
4241
Option<ExpressionPos>,
4342
),
4443
> {
45-
opt_second_expression_after_keyword(parse_for_p(), Keyword::Step)
46-
.map(|((n, l, u), opt_step)| (n, l, u, opt_step))
47-
}
48-
49-
impl ExtractExpression for (ExpressionPos, ExpressionPos, ExpressionPos) {
50-
fn to_expression(&self) -> &ExpressionPos {
51-
&self.2
52-
}
44+
opt_second_expression_after_keyword(parse_for_p(), Keyword::Step, |(_var, _low, upper)| {
45+
upper.is_parenthesis()
46+
})
47+
.map(|((n, l, u), opt_step)| (n, l, u, opt_step))
5348
}
5449

5550
/// Parses the "FOR I = 1 TO 2" part

rusty_parser/src/specific/core/opt_second_expression.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,26 @@ use crate::pc::boxed::boxed;
33
use crate::pc::{And, AndWithoutUndo, Chain, Errors, Map, Parser, RcStringView, ToOption, Token};
44
use crate::specific::core::expression::ws_expr_pos_p;
55
use crate::specific::pc_specific::{keyword, opt_whitespace, whitespace};
6-
use crate::specific::{ExpressionPos, ExpressionTrait, Keyword};
7-
8-
/// Finds the rightmost expression of a given type,
9-
/// so that it can be determined if it ended in parenthesis or not.
10-
#[deprecated]
11-
pub trait ExtractExpression {
12-
fn to_expression(&self) -> &ExpressionPos;
13-
}
14-
15-
impl ExtractExpression for ExpressionPos {
16-
fn to_expression(&self) -> &ExpressionPos {
17-
self
18-
}
19-
}
6+
use crate::specific::{ExpressionPos, Keyword};
207

218
/// Parses an optional second expression that follows the first expression
229
/// and a keyword.
2310
///
2411
/// If the keyword is present, the second expression is mandatory.
2512
///
2613
/// Example: `FOR I = 1 TO 100 [STEP 5]`
27-
pub fn opt_second_expression_after_keyword<P>(
28-
first_expression_parser: P,
14+
pub fn opt_second_expression_after_keyword<P, F>(
15+
first_parser: P,
2916
keyword: Keyword,
17+
is_first_wrapped_in_parenthesis: F,
3018
) -> impl Parser<RcStringView, Output = (P::Output, Option<ExpressionPos>)>
3119
where
3220
P: Parser<RcStringView>,
33-
P::Output: ExtractExpression,
21+
F: Fn(&P::Output) -> bool,
3422
{
35-
first_expression_parser.chain(
23+
first_parser.chain(
3624
move |first| {
37-
let first_expr = first.to_expression();
38-
let is_paren = first_expr.is_parenthesis();
25+
let is_paren = is_first_wrapped_in_parenthesis(&first);
3926
parse_second(keyword, is_paren)
4027
},
4128
|first, opt_second| (first, opt_second),

rusty_parser/src/specific/core/select_case.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ mod case_expression_parser {
109109
use crate::specific::core::opt_second_expression::opt_second_expression_after_keyword;
110110
use crate::specific::pc_specific::*;
111111
use crate::specific::{CaseExpression, Keyword, Operator};
112+
use crate::ExpressionTrait;
112113
use rusty_common::Positioned;
113114

114115
pub fn parser() -> impl Parser<RcStringView, Output = CaseExpression> {
@@ -141,12 +142,15 @@ mod case_expression_parser {
141142
}
142143

143144
fn simple_or_range() -> impl Parser<RcStringView, Output = CaseExpression> {
144-
opt_second_expression_after_keyword(expression_pos_p(), Keyword::To).map(
145-
|(left, opt_right)| match opt_right {
146-
Some(right) => CaseExpression::Range(left, right),
147-
_ => CaseExpression::Simple(left),
148-
},
145+
opt_second_expression_after_keyword(
146+
expression_pos_p(),
147+
Keyword::To,
148+
ExpressionTrait::is_parenthesis,
149149
)
150+
.map(|(left, opt_right)| match opt_right {
151+
Some(right) => CaseExpression::Range(left, right),
152+
_ => CaseExpression::Simple(left),
153+
})
150154
}
151155
}
152156

0 commit comments

Comments
 (0)