Skip to content

Commit fef8a35

Browse files
authored
refactor(pc): Introducing MapDecorator (#122)
* refactor(pc): Introducing MapDecorator Makes parsers that rely on a single underlying parser and only map the successful result of that parser slightly less boilerplate in the implementation. Using a marker trait MapDecoratorMarker to resolve conflicts in blanket trait implementation of Parser. * Migrated OrDefaultParser to MapDecorator * Migrated ToOptionParser to MapDecorator * Migrated AndThenErrParser to MapDecorator * Migrated AndThenParser to MapDecorator * Migrated LazyParser to MapDecorator * Renamed `specific_trait` to `or_expected` * Created dedicated ToFatalParser * Created dedicated MapSoftErrParser * Created dedicated parser for MapFatalErr * Implemented `with_expected_message` in pc * Moved `or_expected` into pc
1 parent ba8c050 commit fef8a35

29 files changed

Lines changed: 341 additions & 270 deletions

rusty_parser/src/core/dim_name.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,12 @@ mod type_definition {
172172
Box::new(built_in_numeric_type()),
173173
Box::new(built_in_string()),
174174
];
175-
let mut expected_message =
176-
"Expected: INTEGER or LONG or SINGLE or DOUBLE or STRING";
175+
let mut expected_message = "INTEGER or LONG or SINGLE or DOUBLE or STRING";
177176

178177
if allow_user_defined {
179178
parsers.push(Box::new(user_defined_type()));
180179
expected_message =
181-
"Expected: INTEGER or LONG or SINGLE or DOUBLE or STRING or identifier";
180+
"INTEGER or LONG or SINGLE or DOUBLE or STRING or identifier";
182181
}
183182

184183
OrParser::new(parsers).with_expected_message(expected_message)

rusty_parser/src/core/param_name.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ fn extended_type() -> impl Parser<StringView, VarNameCtx, Output = ParamType, Er
101101
// allow user defined
102102
built_in_extended_type()
103103
.or(user_defined_type())
104-
.with_expected_message(
105-
"Expected: INTEGER or LONG or SINGLE or DOUBLE or STRING or identifier",
106-
),
104+
.with_expected_message("INTEGER or LONG or SINGLE or DOUBLE or STRING or identifier"),
107105
// do not allow user defined
108106
built_in_extended_type(),
109107
)

rusty_parser/src/core/sub_call.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rusty_pc::*;
44
use crate::error::ParserError;
55
use crate::expr::{csv_expressions_first_guarded, expression_pos_p, property};
66
use crate::input::StringView;
7-
use crate::pc_specific::*;
87
use crate::tokens::equal_sign_ws;
98
use crate::*;
109

rusty_parser/src/error.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl ParserError {
6363
/// Creates a syntax error that starts with "Expected: "
6464
/// followed by the given string.
6565
pub fn expected(expectation: &str) -> Self {
66-
Self::Expected(format!("Expected: {}", expectation))
66+
Self::from(expectation)
6767
}
6868
}
6969

@@ -98,3 +98,17 @@ impl From<std::num::ParseIntError> for ParserError {
9898
Self::ParseNumError(e.to_string())
9999
}
100100
}
101+
102+
// Needed in order to support with_expected_message
103+
104+
impl From<String> for ParserError {
105+
fn from(e: String) -> Self {
106+
Self::Expected(format!("Expected: {}", e))
107+
}
108+
}
109+
110+
impl From<&str> for ParserError {
111+
fn from(e: &str) -> Self {
112+
Self::Expected(format!("Expected: {}", e))
113+
}
114+
}

rusty_parser/src/expr/binary_expression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rusty_pc::*;
55
use crate::error::ParserError;
66
use crate::expr::{expression_pos_p, ws_expr_pos_p};
77
use crate::input::StringView;
8-
use crate::pc_specific::{OrExpected, WithPos, lead_opt_ws, lead_ws};
8+
use crate::pc_specific::{WithPos, lead_opt_ws, lead_ws};
99
use crate::tokens::{TokenType, any_token};
1010
use crate::{ExpressionPos, ExpressionPosTrait, ExpressionTrait, Keyword, Operator};
1111

rusty_parser/src/expr/parenthesis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rusty_pc::*;
22

33
use crate::expr::expression_pos_p;
44
use crate::input::StringView;
5-
use crate::pc_specific::{OrExpected, WithPos, in_parenthesis};
5+
use crate::pc_specific::{WithPos, in_parenthesis};
66
use crate::{Expression, ExpressionPos, ParserError};
77

88
pub(super) fn parser() -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {

rusty_parser/src/expr/parsers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,6 @@ fn opt_keyword_expr(
149149
let msg = format!("expression after {}", keyword);
150150
conditionally_opt_whitespace()
151151
.and_keep_right(keyword_ignoring(keyword).no_context())
152-
.and_keep_right(ws_expr_pos_p().or_expected(&msg).no_context())
152+
.and_keep_right(ws_expr_pos_p().or_expected(msg).no_context())
153153
.to_option()
154154
}

rusty_parser/src/expr/property.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rusty_pc::*;
44
use crate::core::{name_as_tokens_p, token_to_type_qualifier};
55
use crate::error::ParserError;
66
use crate::input::StringView;
7-
use crate::pc_specific::OrExpected;
87
use crate::tokens::dot;
98
use crate::{BareName, Expression, ExpressionPos, ExpressionType, Name, NameAsTokens};
109

rusty_parser/src/expr/unary_expression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rusty_pc::*;
33

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

rusty_parser/src/pc_specific/csv.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use rusty_pc::*;
22

33
use crate::error::ParserError;
44
use crate::input::StringView;
5-
use crate::pc_specific::OrExpected;
65
use crate::tokens::comma_ws;
76

87
/// Comma separated list of items.

0 commit comments

Comments
 (0)