Skip to content

Commit

Permalink
remove Input trait entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
kivikakk committed Dec 1, 2017
1 parent 05a027f commit a71f15e
Show file tree
Hide file tree
Showing 23 changed files with 645 additions and 748 deletions.
22 changes: 11 additions & 11 deletions pest/examples/parens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate pest;
use std::io::{self, Write};
use std::rc::Rc;

use pest::inputs::{Input, Position};
use pest::inputs::{StrInput, Position};
use pest::iterators::Pairs;
use pest::{Error, Parser, ParserState, state};

Expand All @@ -18,11 +18,11 @@ enum Rule {
struct ParenParser;

impl Parser<Rule> for ParenParser {
fn parse<'i, I: Input<'i>>(rule: Rule, input: Rc<I>) -> Result<Pairs<'i, Rule, I>, Error<'i, Rule, I>> {
fn expr<'i, I: Input<'i>>(
pos: Position<'i, I>,
state: &mut ParserState<'i, Rule, I>
) -> Result<Position<'i, I>, Position<'i, I>> {
fn parse<'i>(rule: Rule, input: Rc<StrInput<'i>>) -> Result<Pairs<'i, Rule>, Error<'i, Rule>> {
fn expr<'i>(
pos: Position<'i>,
state: &mut ParserState<'i, Rule>
) -> Result<Position<'i>, Position<'i>> {
state.sequence(move |state| {
pos.sequence(|p| {
p.repeat(|p| {
Expand All @@ -34,10 +34,10 @@ impl Parser<Rule> for ParenParser {
})
}

fn paren<'i, I: Input<'i>>(
pos: Position<'i, I>,
state: &mut ParserState<'i, Rule, I>
) -> Result<Position<'i, I>, Position<'i, I>> {
fn paren<'i>(
pos: Position<'i>,
state: &mut ParserState<'i, Rule>
) -> Result<Position<'i>, Position<'i>> {
state.rule(Rule::paren, pos, |state, pos| {
state.sequence(move |state| {
pos.sequence(|p| {
Expand Down Expand Up @@ -80,7 +80,7 @@ impl Parser<Rule> for ParenParser {
#[derive(Debug)]
struct Paren(Vec<Paren>);

fn expr<'i, I: Input<'i>>(pairs: Pairs<'i, Rule, I>) -> Vec<Paren> {
fn expr<'i>(pairs: Pairs<'i, Rule>) -> Vec<Paren> {
pairs.filter(|p| p.as_rule() == Rule::paren).map(|p| Paren(expr(p.into_inner()))).collect()
}

Expand Down
36 changes: 18 additions & 18 deletions pest/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,38 @@
use std::error;
use std::fmt;

use inputs::{Input, position, Position, Span};
use inputs::{position, Position, Span};
use RuleType;

/// An `enum` which defines possible errors.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Error<'i, R, I: Input<'i>> {
pub enum Error<'i, R> {
/// Generated parsing error with expected and unexpected `Rule`s and a position
ParsingError {
/// Positive attempts
positives: Vec<R>,
/// Negative attempts
negatives: Vec<R>,
/// Deepest position of attempts
pos: Position<'i, I>
pos: Position<'i>
},
/// Custom error with a message and a position
CustomErrorPos {
/// Short explanation
message: String,
/// Error `Position` for formatting
pos: Position<'i, I>
pos: Position<'i>
},
/// Custom error with a message and a span defined by a start and end position
CustomErrorSpan {
/// Short explanation
message: String,
/// Error `Span` for formatting
span: Span<'i, I>
span: Span<'i>
}
}

impl <'i, R: RuleType, I: Input<'i>> Error<'i, R, I> {
impl <'i, R: RuleType> Error<'i, R> {
/// Renames all `Rule`s from a `ParsingError` variant returning a `CustomErrorPos`. It does
/// nothing when called on `CustomErrorPos` and `CustomErrorSpan` variants.
///
Expand Down Expand Up @@ -71,7 +71,7 @@ impl <'i, R: RuleType, I: Input<'i>> Error<'i, R, I> {
/// }
/// });
/// ```
pub fn renamed_rules<F>(self, f: F) -> Error<'i, R, I>
pub fn renamed_rules<F>(self, f: F) -> Error<'i, R>
where
F: FnMut(&R) -> String
{
Expand All @@ -88,7 +88,7 @@ impl <'i, R: RuleType, I: Input<'i>> Error<'i, R, I> {
}
}

fn message<'i, R: fmt::Debug, I: Input<'i>>(error: &Error<'i, R, I>) -> String {
fn message<'i, R: fmt::Debug>(error: &Error<'i, R>) -> String {
match *error {
Error::ParsingError { ref positives, ref negatives, .. } => {
parsing_error_message(positives, negatives, |r| format!("{:?}", r))
Expand Down Expand Up @@ -139,7 +139,7 @@ where
}
}

fn underline<'i, R: fmt::Debug, I: Input<'i>>(error: &Error<'i, R, I>, offset: usize) -> String {
fn underline<'i, R: fmt::Debug>(error: &Error<'i, R>, offset: usize) -> String {
let mut underline = String::new();

for _ in 0..offset { underline.push(' '); }
Expand All @@ -160,7 +160,7 @@ fn underline<'i, R: fmt::Debug, I: Input<'i>>(error: &Error<'i, R, I>, offset: u
underline
}

fn format<'i, R: fmt::Debug, I: Input<'i>>(error: &Error<'i, R, I>) -> String {
fn format<'i, R: fmt::Debug>(error: &Error<'i, R>) -> String {
let pos = match *error {
Error::ParsingError { ref pos, .. } | Error::CustomErrorPos { ref pos, .. } => pos.clone(),
Error::CustomErrorSpan { ref span, .. } => span.clone().split().0.clone()
Expand Down Expand Up @@ -191,13 +191,13 @@ fn format<'i, R: fmt::Debug, I: Input<'i>>(error: &Error<'i, R, I>) -> String {
result
}

impl<'i, R: fmt::Debug, I: Input<'i>> fmt::Display for Error<'i, R, I> {
impl<'i, R: fmt::Debug> fmt::Display for Error<'i, R> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", format(self))
}
}

impl<'i, R: fmt::Debug, I: Input<'i>> error::Error for Error<'i, R, I> {
impl<'i, R: fmt::Debug> error::Error for Error<'i, R> {
fn description(&self) -> &str {
match *self {
Error::ParsingError { .. } => {
Expand All @@ -221,7 +221,7 @@ mod tests {
fn display_parsing_error_mixed() {
let input = StrInput::new("ab\ncd\nef");
let pos = unsafe { position::new(Rc::new(input), 4) };
let error: Error<u32, _> = Error::ParsingError {
let error: Error<u32> = Error::ParsingError {
positives: vec![1, 2, 3],
negatives: vec![4, 5, 6],
pos: pos
Expand All @@ -241,7 +241,7 @@ mod tests {
fn display_parsing_error_positives() {
let input = StrInput::new("ab\ncd\nef");
let pos = unsafe { position::new(Rc::new(input), 4) };
let error: Error<u32, _> = Error::ParsingError {
let error: Error<u32> = Error::ParsingError {
positives: vec![1, 2],
negatives: vec![],
pos: pos
Expand All @@ -261,7 +261,7 @@ mod tests {
fn display_parsing_error_negatives() {
let input = StrInput::new("ab\ncd\nef");
let pos = unsafe { position::new(Rc::new(input), 4) };
let error: Error<u32, _> = Error::ParsingError {
let error: Error<u32> = Error::ParsingError {
positives: vec![],
negatives: vec![4, 5, 6],
pos: pos
Expand All @@ -281,7 +281,7 @@ mod tests {
fn display_parsing_error_unknown() {
let input = StrInput::new("ab\ncd\nef");
let pos = unsafe { position::new(Rc::new(input), 4) };
let error: Error<u32, _> = Error::ParsingError {
let error: Error<u32> = Error::ParsingError {
positives: vec![],
negatives: vec![],
pos: pos
Expand All @@ -301,7 +301,7 @@ mod tests {
fn display_custom() {
let input = StrInput::new("ab\ncd\nef");
let pos = unsafe { position::new(Rc::new(input), 4) };
let error: Error<&str, _> = Error::CustomErrorPos {
let error: Error<&str> = Error::CustomErrorPos {
message: "error: big one".to_owned(),
pos: pos
};
Expand All @@ -320,7 +320,7 @@ mod tests {
fn mapped_parsing_error() {
let input = StrInput::new("ab\ncd\nef");
let pos = unsafe { position::new(Rc::new(input), 4) };
let error: Error<u32, _> = Error::ParsingError {
let error: Error<u32> = Error::ParsingError {
positives: vec![1, 2, 3],
negatives: vec![4, 5, 6],
pos: pos
Expand Down
96 changes: 0 additions & 96 deletions pest/src/inputs/input.rs

This file was deleted.

2 changes: 0 additions & 2 deletions pest/src/inputs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

//! A `mod` containing the `Input`-related constructs.
mod input;
pub(super) mod position;
pub(super) mod span;
mod str_input;

pub use self::input::Input;
pub use self::position::Position;
pub use self::str_input::StrInput;
pub use self::span::Span;
Loading

0 comments on commit a71f15e

Please sign in to comment.