Skip to content

Commit 1ad502f

Browse files
committed
Added expr_ws_keyword_p and demand_expr_ws_keyword_p
1 parent 7a97458 commit 1ad502f

4 files changed

Lines changed: 52 additions & 30 deletions

File tree

rusty_parser/src/built_ins/field.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rusty_common::*;
22
use rusty_pc::*;
33

4-
use crate::expr::expr_pos_ws_p;
4+
use crate::expr::expr_ws_keyword_p;
55
use crate::expr::file_handle::file_handle_p;
66
use crate::input::StringView;
77
use crate::pc_specific::*;
@@ -23,12 +23,9 @@ pub fn parse() -> impl Parser<StringView, Output = Statement, Error = ParserErro
2323

2424
fn field_item_p() -> impl Parser<StringView, Output = (ExpressionPos, NamePos), Error = ParserError>
2525
{
26-
seq3(
27-
expr_pos_ws_p(),
28-
keyword_ws_p(Keyword::As),
26+
expr_ws_keyword_p(Keyword::As).and_tuple(demand_lead_ws(
2927
name_p().with_pos().or_expected("variable name"),
30-
|width, _, name| (width, name),
31-
)
28+
))
3229
}
3330

3431
fn build_args(

rusty_parser/src/core/for_loop.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use rusty_pc::*;
22

33
use crate::core::statements::zero_or_more_statements;
44
use crate::error::ParserError;
5-
use crate::expr::{expr_pos_ws_p, opt_second_expression_after_keyword, property, ws_expr_pos_p};
5+
use crate::expr::{
6+
demand_expr_ws_keyword_p, opt_second_expression_after_keyword, property, ws_expr_pos_p
7+
};
68
use crate::input::StringView;
79
use crate::pc_specific::*;
810
use crate::tokens::equal_sign_ws;
@@ -52,14 +54,13 @@ fn parse_for_step_p() -> impl Parser<
5254
fn parse_for_p()
5355
-> impl Parser<StringView, Output = (ExpressionPos, ExpressionPos, ExpressionPos), Error = ParserError>
5456
{
55-
seq6(
56-
keyword_ws_p(Keyword::For),
57-
property::parser().or_expected("name after FOR"),
57+
seq5(
58+
keyword_ignoring(Keyword::For),
59+
demand_lead_ws(property::parser().or_expected("name after FOR")),
5860
equal_sign_ws(),
59-
expr_pos_ws_p().or_expected("lower bound of FOR loop"),
60-
keyword(Keyword::To),
61+
demand_expr_ws_keyword_p("lower bound of FOR loop", Keyword::To),
6162
ws_expr_pos_p().or_expected("upper bound of FOR loop"),
62-
|_, name, _, lower_bound, _, upper_bound| (name, lower_bound, upper_bound),
63+
|_, name, _, lower_bound, upper_bound| (name, lower_bound, upper_bound),
6364
)
6465
}
6566

rusty_parser/src/expr/opt_second_expression.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{ExpressionPos, Keyword};
1313
/// If the keyword is present, the second expression is mandatory.
1414
///
1515
/// Example: `FOR I = 1 TO 100 [STEP 5]`
16+
#[deprecated]
1617
pub fn opt_second_expression_after_keyword<P, F>(
1718
first_parser: P,
1819
keyword: Keyword,

rusty_parser/src/expr/parsers.rs

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::expr::opt_second_expression::conditionally_opt_whitespace;
55
use crate::input::StringView;
66
use crate::pc_specific::*;
77
use crate::tokens::comma_ws;
8-
use crate::{ExpressionPos, ExpressionTrait, Expressions, ParserError};
8+
use crate::{ExpressionPos, ExpressionTrait, Expressions, Keyword, ParserError};
99

1010
/// `( expr [, expr]* )`
1111
pub fn in_parenthesis_csv_expressions_non_opt(
@@ -51,22 +51,6 @@ pub fn ws_expr_pos_p() -> impl Parser<StringView, Output = ExpressionPos, Error
5151
super::parenthesis::parser().or(lead_ws(expression_pos_p()))
5252
}
5353

54-
/// Parses an expression that is either followed by whitespace
55-
/// or is a parenthesis expression.
56-
///
57-
/// The whitespace is mandatory after a non-parenthesis
58-
/// expression.
59-
///
60-
/// ```text
61-
/// <expr-not-in-parenthesis> <ws> |
62-
/// <expr-in-parenthesis> <ws> |
63-
/// <expr-in-parenthesis>
64-
/// ```
65-
#[deprecated]
66-
pub fn expr_pos_ws_p() -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {
67-
followed_by_ws(expression_pos_p())
68-
}
69-
7054
/// Parses an expression that is either surrounded by whitespace
7155
/// or is a parenthesis expression.
7256
///
@@ -78,6 +62,7 @@ pub fn expr_pos_ws_p() -> impl Parser<StringView, Output = ExpressionPos, Error
7862
/// <ws> <expr-in-parenthesis> <ws> |
7963
/// <expr-in-parenthesis>
8064
/// ```
65+
#[deprecated]
8166
pub fn ws_expr_pos_ws_p() -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {
8267
followed_by_ws(ws_expr_pos_p())
8368
}
@@ -97,3 +82,41 @@ fn eager_expression_pos_p() -> impl Parser<StringView, Output = ExpressionPos, E
9782
{
9883
super::binary_expression::parser()
9984
}
85+
86+
/// Parses an expression,
87+
/// then demands whitespace, unless the expression is a parenthesis.
88+
/// Finally it demands the given keyword.
89+
pub fn expr_ws_keyword_p(
90+
keyword: Keyword,
91+
) -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {
92+
expr_ws_followed_by(expression_pos_p(), keyword_ignoring(keyword))
93+
}
94+
95+
/// Parses an expression, failing fatally with the given expectation message if it can't be parsed.
96+
/// Then it demands whitespace, unless the expression is a parenthesis.
97+
/// Finally it demands the given keyword.
98+
pub fn demand_expr_ws_keyword_p(
99+
expectation: &str,
100+
keyword: Keyword,
101+
) -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {
102+
expr_ws_followed_by(
103+
expression_pos_p().or_expected(expectation),
104+
keyword_ignoring(keyword),
105+
)
106+
}
107+
108+
/// Parses the expression using the given parser,
109+
/// then demands whitespace, unless the expression is a parenthesis.
110+
/// Finally it demands the second parser.
111+
pub fn expr_ws_followed_by(
112+
expr_parser: impl Parser<StringView, Output = ExpressionPos, Error = ParserError>,
113+
other_parser: impl Parser<StringView, Error = ParserError>,
114+
) -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {
115+
expr_parser.then_with_in_context(
116+
conditionally_opt_whitespace()
117+
.to_fatal()
118+
.and_keep_right(other_parser.to_fatal().no_context()),
119+
|e| e.is_parenthesis(),
120+
KeepLeftCombiner,
121+
)
122+
}

0 commit comments

Comments
 (0)