Skip to content

Commit 6cb19d8

Browse files
committed
Implemented with_expected_message in pc
1 parent 0b02594 commit 6cb19d8

8 files changed

Lines changed: 31 additions & 63 deletions

File tree

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/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/pc_specific/keyword.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rusty_pc::and::IgnoringBothCombiner;
44
use rusty_pc::*;
55

66
use crate::input::StringView;
7-
use crate::pc_specific::{WithExpected, whitespace_ignoring};
7+
use crate::pc_specific::whitespace_ignoring;
88
use crate::tokens::{TokenMatcher, TokenType, any_token};
99
use crate::{Keyword, ParserError};
1010

@@ -42,7 +42,7 @@ pub fn keyword_ignoring(k: Keyword) -> impl Parser<StringView, Output = (), Erro
4242
any_token()
4343
.filter(move |t| k.matches_token(t))
4444
.map_to_unit()
45-
.with_expected_message(format!("Expected: {}", k))
45+
.with_expected_message(format!("{}", k))
4646
}
4747

4848
/// Parses the given keyword, followed by mandatory whitespace.

rusty_parser/src/pc_specific/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ mod whitespace;
88
pub mod logging;
99

1010
mod or_expected;
11-
mod with_expected_message;
1211
mod with_pos;
1312

1413
pub use self::csv::*;
@@ -17,5 +16,4 @@ pub use self::keyword::*;
1716
pub use self::keyword_map::*;
1817
pub use self::or_expected::*;
1918
pub use self::whitespace::*;
20-
pub use self::with_expected_message::*;
2119
pub use self::with_pos::*;

rusty_parser/src/pc_specific/with_expected_message.rs

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

rusty_parser/src/tokens/any_token.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rusty_pc::text::{many_str, many_str_with_combiner, one_char_to_str};
44
use rusty_pc::*;
55

66
use crate::input::StringView;
7-
use crate::pc_specific::WithExpected;
87
use crate::tokens::TokenType;
98
use crate::tokens::any_symbol::any_symbol;
109
use crate::{Keyword, ParserError};

rusty_pc/src/parser.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,17 @@ where
318318
MapSoftErrParser::new(self, err)
319319
}
320320

321+
/// If this parser returns a soft error, the soft error will be replaced
322+
/// by transforming the given message into an error.
323+
/// The parameter will be converted with the standard `From` trait.
324+
fn with_expected_message<E>(self, msg: E) -> MapSoftErrParser<Self, Self::Error>
325+
where
326+
Self: Sized,
327+
Self::Error: From<E>,
328+
{
329+
self.with_soft_err(Self::Error::from(msg))
330+
}
331+
321332
/// If this parser returns a soft error, the soft error will be replaced by
322333
/// the given error, which must be fatal.
323334
fn or_fail(self, err: Self::Error) -> MapSoftErrParser<Self, Self::Error>

0 commit comments

Comments
 (0)