Skip to content

Commit

Permalink
pest@1 doesn't emit eoi, remove handling
Browse files Browse the repository at this point in the history
cc rust-lang/rust#46969
The reason that the PhantomData approach doesn't work
Not that the generic EOI does either
  • Loading branch information
CAD97 committed Sep 2, 2018
1 parent ea4e96e commit 21e9ead
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 44 deletions.
9 changes: 4 additions & 5 deletions examples/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ mod parser {
#[grammar = "../examples/csv.pest"]
pub struct CSVParser;
const _GRAMMAR: &str = include_str!("csv.pest");
eoi_from_pest_impl!(Rule);
}

mod ast {
use std::marker::PhantomData;
use super::parser::Rule;
use pest::Span;
use pest_deconstruct::EOI;

#[derive(Debug, FromPest)]
#[pest(rule = "Rule")]
Expand All @@ -38,7 +35,6 @@ mod ast {
pub struct File<'i> {
pub span: Span<'i>,
pub records: Vec<Record<'i>>,
eoi: PhantomData<EOI<'i>>, // TODO: make this unnecessary?
}
}

Expand All @@ -49,8 +45,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
use ast::File;

let unparsed_file = String::from_utf8(std::fs::read("./examples/csv.csv")?)?;
let parsed_file = CSVParser::parse(Rule::File, &unparsed_file)?.next().unwrap();
let parsed_file = CSVParser::parse(Rule::File, &unparsed_file).unwrap().next().unwrap();
println!("ppt: {:#?}", parsed_file);
let file = File::from_pest(parsed_file);
println!("ast: {:#?}", file);
println!();

let mut field_sum: f64 = 0.0;
let mut record_count: u64 = 0;
Expand Down
41 changes: 2 additions & 39 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use pest::{
iterators::{Pair, Pairs},
RuleType,
};
use std::{fmt, iter::Peekable, marker::PhantomData};
use std::iter::Peekable;

pub use derive::*;

/// Convert from the Pest parse tree to a typed AST node.
pub trait FromPest<'a> {
pub trait FromPest<'a>: Sized {
/// The rule enum that this AST pairs with.
type Rule: RuleType;
/// The specific rule that this AST represents.
Expand All @@ -31,43 +31,6 @@ pub trait FromPest<'a> {
fn from_pest(pest: Pair<'a, Self::Rule>) -> Self;
}

impl<'a, T: FromPest<'a> + Sized> FromPest<'a> for PhantomData<T> {
type Rule = T::Rule;
const RULE: T::Rule = T::RULE;
fn from_pest(pest: Pair<'a, T::Rule>) -> Self {
let _ = T::from_pest(pest);
PhantomData
}
}

pub struct EOI<'i> {
span: PhantomData<pest::Span<'i>>,
}

impl<'i> fmt::Debug for EOI<'i> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "EOI")
}
}

#[macro_export]
macro_rules! eoi_from_pest_impl {
($path:path) => {{
#[cfg_attr(feature = "cargo-clippy", allow(useless_attribute))]
#[allow(rust_2018_idioms)]
extern crate pest_deconstruct as __crate;
use __crate::*;
use std::marker::PhantomData;
impl<'i> FromPest<'i> for EOI<'i> {
type Rule = $path;
const RULE: $path = $path::eoi;
fn from_pest(pest: Pair<'i, $path>) -> Self {
EOI { span: PhantomData }
}
}
}};
}

/// Deconstruct a Pest `Pair` into its inner productions in a strongly-typed, panic-enforced manner.
/// See [`PestDeconstructor`] for more information.
pub trait PestDeconstruct<'i> {
Expand Down

0 comments on commit 21e9ead

Please sign in to comment.