Skip to content
5 changes: 2 additions & 3 deletions rusty_parser/src/core/dim_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,12 @@ mod type_definition {
Box::new(built_in_numeric_type()),
Box::new(built_in_string()),
];
let mut expected_message =
"Expected: INTEGER or LONG or SINGLE or DOUBLE or STRING";
let mut expected_message = "INTEGER or LONG or SINGLE or DOUBLE or STRING";

if allow_user_defined {
parsers.push(Box::new(user_defined_type()));
expected_message =
"Expected: INTEGER or LONG or SINGLE or DOUBLE or STRING or identifier";
"INTEGER or LONG or SINGLE or DOUBLE or STRING or identifier";
}

OrParser::new(parsers).with_expected_message(expected_message)
Expand Down
4 changes: 1 addition & 3 deletions rusty_parser/src/core/param_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ fn extended_type() -> impl Parser<StringView, VarNameCtx, Output = ParamType, Er
// allow user defined
built_in_extended_type()
.or(user_defined_type())
.with_expected_message(
"Expected: INTEGER or LONG or SINGLE or DOUBLE or STRING or identifier",
),
.with_expected_message("INTEGER or LONG or SINGLE or DOUBLE or STRING or identifier"),
// do not allow user defined
built_in_extended_type(),
)
Expand Down
1 change: 0 additions & 1 deletion rusty_parser/src/core/sub_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use rusty_pc::*;
use crate::error::ParserError;
use crate::expr::{csv_expressions_first_guarded, expression_pos_p, property};
use crate::input::StringView;
use crate::pc_specific::*;
use crate::tokens::equal_sign_ws;
use crate::*;

Expand Down
16 changes: 15 additions & 1 deletion rusty_parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl ParserError {
/// Creates a syntax error that starts with "Expected: "
/// followed by the given string.
pub fn expected(expectation: &str) -> Self {
Self::Expected(format!("Expected: {}", expectation))
Self::from(expectation)
}
}

Expand Down Expand Up @@ -98,3 +98,17 @@ impl From<std::num::ParseIntError> for ParserError {
Self::ParseNumError(e.to_string())
}
}

// Needed in order to support with_expected_message

impl From<String> for ParserError {
fn from(e: String) -> Self {
Self::Expected(format!("Expected: {}", e))
}
}

impl From<&str> for ParserError {
fn from(e: &str) -> Self {
Self::Expected(format!("Expected: {}", e))
}
}
2 changes: 1 addition & 1 deletion rusty_parser/src/expr/binary_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rusty_pc::*;
use crate::error::ParserError;
use crate::expr::{expression_pos_p, ws_expr_pos_p};
use crate::input::StringView;
use crate::pc_specific::{OrExpected, WithPos, lead_opt_ws, lead_ws};
use crate::pc_specific::{WithPos, lead_opt_ws, lead_ws};
use crate::tokens::{TokenType, any_token};
use crate::{ExpressionPos, ExpressionPosTrait, ExpressionTrait, Keyword, Operator};

Expand Down
2 changes: 1 addition & 1 deletion rusty_parser/src/expr/parenthesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rusty_pc::*;

use crate::expr::expression_pos_p;
use crate::input::StringView;
use crate::pc_specific::{OrExpected, WithPos, in_parenthesis};
use crate::pc_specific::{WithPos, in_parenthesis};
use crate::{Expression, ExpressionPos, ParserError};

pub(super) fn parser() -> impl Parser<StringView, Output = ExpressionPos, Error = ParserError> {
Expand Down
2 changes: 1 addition & 1 deletion rusty_parser/src/expr/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,6 @@ fn opt_keyword_expr(
let msg = format!("expression after {}", keyword);
conditionally_opt_whitespace()
.and_keep_right(keyword_ignoring(keyword).no_context())
.and_keep_right(ws_expr_pos_p().or_expected(&msg).no_context())
.and_keep_right(ws_expr_pos_p().or_expected(msg).no_context())
.to_option()
}
1 change: 0 additions & 1 deletion rusty_parser/src/expr/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use rusty_pc::*;
use crate::core::{name_as_tokens_p, token_to_type_qualifier};
use crate::error::ParserError;
use crate::input::StringView;
use crate::pc_specific::OrExpected;
use crate::tokens::dot;
use crate::{BareName, Expression, ExpressionPos, ExpressionType, Name, NameAsTokens};

Expand Down
2 changes: 1 addition & 1 deletion rusty_parser/src/expr/unary_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rusty_pc::*;

use crate::expr::{expression_pos_p, ws_expr_pos_p};
use crate::input::StringView;
use crate::pc_specific::{OrExpected, WithPos, keyword};
use crate::pc_specific::{WithPos, keyword};
use crate::tokens::minus_sign;
use crate::{ExpressionPos, ExpressionPosTrait, Keyword, ParserError, UnaryOperator};

Expand Down
1 change: 0 additions & 1 deletion rusty_parser/src/pc_specific/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use rusty_pc::*;

use crate::error::ParserError;
use crate::input::StringView;
use crate::pc_specific::OrExpected;
use crate::tokens::comma_ws;

/// Comma separated list of items.
Expand Down
4 changes: 2 additions & 2 deletions rusty_parser/src/pc_specific/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rusty_pc::and::IgnoringBothCombiner;
use rusty_pc::*;

use crate::input::StringView;
use crate::pc_specific::{WithExpected, whitespace_ignoring};
use crate::pc_specific::whitespace_ignoring;
use crate::tokens::{TokenMatcher, TokenType, any_token};
use crate::{Keyword, ParserError};

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

/// Parses the given keyword, followed by mandatory whitespace.
Expand Down
4 changes: 0 additions & 4 deletions rusty_parser/src/pc_specific/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ mod whitespace;
#[cfg(debug_assertions)]
pub mod logging;

mod specific_trait;
mod with_expected_message;
mod with_pos;

pub use self::csv::*;
pub use self::in_parenthesis::*;
pub use self::keyword::*;
pub use self::keyword_map::*;
pub use self::specific_trait::*;
pub use self::whitespace::*;
pub use self::with_expected_message::*;
pub use self::with_pos::*;
19 changes: 0 additions & 19 deletions rusty_parser/src/pc_specific/specific_trait.rs

This file was deleted.

51 changes: 0 additions & 51 deletions rusty_parser/src/pc_specific/with_expected_message.rs

This file was deleted.

1 change: 1 addition & 0 deletions rusty_parser/src/pc_specific/with_pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ where
WithPosMapper::new(self)
}
}

impl<I, C, P> WithPos<I, C> for P
where
P: Parser<I, C>,
Expand Down
1 change: 0 additions & 1 deletion rusty_parser/src/tokens/any_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use rusty_pc::text::{many_str, many_str_with_combiner, one_char_to_str};
use rusty_pc::*;

use crate::input::StringView;
use crate::pc_specific::WithExpected;
use crate::tokens::TokenType;
use crate::tokens::any_symbol::any_symbol;
use crate::{Keyword, ParserError};
Expand Down
17 changes: 12 additions & 5 deletions rusty_pc/src/and_then.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::map_decorator::{MapDecorator, MapDecoratorMarker};
use crate::{InputTrait, Parser};

pub struct AndThenParser<P, F> {
Expand All @@ -11,19 +12,25 @@ impl<P, F> AndThenParser<P, F> {
}
}

impl<I, C, P, F, U> Parser<I, C> for AndThenParser<P, F>
impl<I, C, P, F, U> MapDecorator<I, C> for AndThenParser<P, F>
where
I: InputTrait,
P: Parser<I, C>,
F: Fn(P::Output) -> Result<U, P::Error>,
{
type OriginalOutput = P::Output;
type Output = U;
type Error = P::Error;
fn parse(&mut self, tokenizer: &mut I) -> Result<Self::Output, Self::Error> {
self.parser.parse(tokenizer).and_then(&self.mapper)

fn decorated(
&mut self,
) -> &mut impl Parser<I, C, Output = Self::OriginalOutput, Error = Self::Error> {
&mut self.parser
}

fn set_context(&mut self, ctx: &C) {
self.parser.set_context(ctx)
fn map_ok(&self, ok: Self::OriginalOutput) -> Result<Self::Output, Self::Error> {
(self.mapper)(ok)
}
}

impl<P, F> MapDecoratorMarker for AndThenParser<P, F> {}
27 changes: 17 additions & 10 deletions rusty_pc/src/and_then_err.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{InputTrait, Parser, ParserErrorTrait};
use crate::map_decorator::{MapDecorator, MapDecoratorMarker};
use crate::{InputTrait, Parser};

pub struct AndThenErrParser<P, F> {
parser: P,
Expand All @@ -11,23 +12,29 @@ impl<P, F> AndThenErrParser<P, F> {
}
}

impl<I, C, P, F> Parser<I, C> for AndThenErrParser<P, F>
impl<I, C, P, F> MapDecorator<I, C> for AndThenErrParser<P, F>
where
I: InputTrait,
P: Parser<I, C>,
F: Fn(P::Error) -> Result<P::Output, P::Error>,
{
type OriginalOutput = P::Output;
type Output = P::Output;
type Error = P::Error;
fn parse(&mut self, input: &mut I) -> Result<Self::Output, Self::Error> {
match self.parser.parse(input) {
Ok(value) => Ok(value),
Err(err) if err.is_soft() => (self.mapper)(err),
Err(err) => Err(err),
}

fn decorated(
&mut self,
) -> &mut impl Parser<I, C, Output = Self::OriginalOutput, Error = Self::Error> {
&mut self.parser
}

fn map_ok(&self, ok: Self::OriginalOutput) -> Result<Self::Output, Self::Error> {
Ok(ok)
}

fn set_context(&mut self, ctx: &C) {
self.parser.set_context(ctx)
fn map_soft_error(&self, err: Self::Error) -> Result<Self::Output, Self::Error> {
(self.mapper)(err)
}
}

impl<P, F> MapDecoratorMarker for AndThenErrParser<P, F> {}
26 changes: 14 additions & 12 deletions rusty_pc/src/lazy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::map_decorator::{MapDecorator, MapDecoratorMarker};
use crate::{InputTrait, Parser};

pub fn lazy<I, C, F, P>(factory: F) -> impl Parser<I, C, Output = P::Output, Error = P::Error>
Expand All @@ -17,28 +18,29 @@ struct LazyParser<F, P> {
parser_holder: Option<P>,
}

impl<I, C, F, P> Parser<I, C> for LazyParser<F, P>
impl<I, C, F, P> MapDecorator<I, C> for LazyParser<F, P>
where
F: Fn() -> P,
I: InputTrait,
P: Parser<I, C>,
{
type OriginalOutput = P::Output;
type Output = P::Output;
type Error = P::Error;

fn parse(&mut self, input: &mut I) -> Result<Self::Output, Self::Error> {
match self.parser_holder.as_mut() {
Some(parser) => parser.parse(input),
None => {
let mut parser = (self.factory)();
let result = parser.parse(input);
self.parser_holder = Some(parser);
result
}
fn decorated(
&mut self,
) -> &mut impl Parser<I, C, Output = Self::OriginalOutput, Error = Self::Error> {
if self.parser_holder.is_none() {
let parser = (self.factory)();
self.parser_holder = Some(parser);
}
self.parser_holder.as_mut().unwrap()
}

fn set_context(&mut self, _ctx: &C) {
unimplemented!()
fn map_ok(&self, ok: Self::OriginalOutput) -> Result<Self::Output, Self::Error> {
Ok(ok)
}
}

impl<F, P> MapDecoratorMarker for LazyParser<F, P> {}
5 changes: 4 additions & 1 deletion rusty_pc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ pub mod many;
pub mod many_ctx;
pub mod map;
pub mod map_ctx;
pub mod map_err;
pub mod map_decorator;
pub mod map_fatal_err;
pub mod map_soft_err;
pub mod no_context;
mod or;
pub mod or_default;
Expand All @@ -26,6 +28,7 @@ mod supplier;
mod surround;
pub mod text;
mod then_with;
pub mod to_fatal;
pub mod to_option;
mod token;
mod top_level;
Expand Down
Loading