Skip to content

Commit 2ce1fd6

Browse files
committed
chore: Removed AllowNoneIf parser
1 parent 7938fd6 commit 2ce1fd6

5 files changed

Lines changed: 37 additions & 49 deletions

File tree

rusty_parser/src/pc/allow_none_if.rs

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

rusty_parser/src/pc/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! base module contains functionality that can be extracted into a library,
22
//! it's generic and not specific to QBasic
3-
mod allow_none_if;
43
mod and;
54
mod and_without_undo;
65
mod any;
@@ -30,7 +29,6 @@ pub mod supplier;
3029
mod to_option;
3130
mod tokenizers;
3231

33-
pub use allow_none_if::*;
3432
pub use and::*;
3533
pub use and_without_undo::AndWithoutUndo;
3634
pub use any::*;

rusty_parser/src/specific/core/expression.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rusty_variant::{MIN_INTEGER, MIN_LONG};
33

44
use crate::lazy_parser;
55
use crate::pc::*;
6+
use crate::specific::core::opt_second_expression::conditionally_opt_whitespace;
67
use crate::specific::pc_specific::*;
78
use crate::specific::{
89
ExpressionType, FileHandle, HasExpressionType, Name, Operator, TypeQualifier, UnaryOperator,
@@ -544,7 +545,7 @@ fn followed_by_ws(
544545
parser.chain(
545546
|expr_pos| {
546547
let is_paren = expr_pos.is_parenthesis();
547-
whitespace().allow_none_if(is_paren).no_incomplete()
548+
conditionally_opt_whitespace(is_paren).no_incomplete()
548549
},
549550
|expr_pos, _| expr_pos,
550551
)

rusty_parser/src/specific/core/opt_second_expression.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use crate::error::ParseError;
2-
use crate::pc::{AllowNoneIf, And, AndWithoutUndo, Chain, Errors, Parser, RcStringView, ToOption};
2+
use crate::pc::boxed::boxed;
3+
use crate::pc::{And, AndWithoutUndo, Chain, Errors, Map, Parser, RcStringView, ToOption, Token};
34
use crate::specific::core::expression::ws_expr_pos_p;
4-
use crate::specific::pc_specific::{keyword, whitespace};
5+
use crate::specific::pc_specific::{keyword, opt_whitespace, whitespace};
56
use crate::specific::{ExpressionPos, ExpressionTrait, Keyword};
67

78
/// Finds the rightmost expression of a given type,
89
/// so that it can be determined if it ended in parenthesis or not.
10+
#[deprecated]
911
pub trait ExtractExpression {
1012
fn to_expression(&self) -> &ExpressionPos;
1113
}
@@ -16,15 +18,21 @@ impl ExtractExpression for ExpressionPos {
1618
}
1719
}
1820

21+
/// Parses an optional second expression that follows the first expression
22+
/// and a keyword.
23+
///
24+
/// If the keyword is present, the second expression is mandatory.
25+
///
26+
/// Example: `FOR I = 1 TO 100 [STEP 5]`
1927
pub fn opt_second_expression_after_keyword<P>(
20-
parser: P,
28+
first_expression_parser: P,
2129
keyword: Keyword,
2230
) -> impl Parser<RcStringView, Output = (P::Output, Option<ExpressionPos>)>
2331
where
2432
P: Parser<RcStringView>,
2533
P::Output: ExtractExpression,
2634
{
27-
parser.chain(
35+
first_expression_parser.chain(
2836
move |first| {
2937
let first_expr = first.to_expression();
3038
let is_paren = first_expr.is_parenthesis();
@@ -38,8 +46,7 @@ fn parse_second(
3846
k: Keyword,
3947
is_paren: bool,
4048
) -> impl Parser<RcStringView, Output = Option<ExpressionPos>> {
41-
whitespace()
42-
.allow_none_if(is_paren)
49+
conditionally_opt_whitespace(is_paren)
4350
.and_tuple(keyword(k))
4451
.and_without_undo_keep_right(ws_expr_pos_p().or_fail(err(k)))
4552
.to_option()
@@ -48,3 +55,24 @@ fn parse_second(
4855
fn err(keyword: Keyword) -> ParseError {
4956
ParseError::SyntaxError(format!("Expected: expression after {}", keyword))
5057
}
58+
59+
/// Creates a parser that parses whitespace,
60+
/// conditionally allowing it to be missing.
61+
/// When [allow_none] is false, whitespace is mandatory.
62+
/// When [allow_none] is true, the whitespace can be missing.
63+
/// This is typically the case when the previously parsed
64+
/// token was a right side parenthesis.
65+
///
66+
/// Examples
67+
///
68+
/// * `(1 + 2)AND` no whitespace is required before `AND`
69+
/// * `1 + 2AND` the lack of whitespace before `AND` is an error
70+
pub(super) fn conditionally_opt_whitespace(
71+
allow_none: bool,
72+
) -> impl Parser<RcStringView, Output = Option<Token>> {
73+
if allow_none {
74+
boxed(opt_whitespace())
75+
} else {
76+
boxed(whitespace().map(Some))
77+
}
78+
}

rusty_parser/src/specific/pc_specific/token_kind_parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn whitespace() -> impl Parser<RcStringView, Output = Token> {
8181
}
8282

8383
/// Optional whitespace.
84-
fn opt_whitespace() -> impl Parser<RcStringView, Output = Option<Token>> {
84+
pub fn opt_whitespace() -> impl Parser<RcStringView, Output = Option<Token>> {
8585
whitespace().to_option()
8686
}
8787

0 commit comments

Comments
 (0)