Skip to content

Commit

Permalink
Get synom compiling again
Browse files Browse the repository at this point in the history
Add a `Synom` trait along with impls for all the base types here, to be used
later.
  • Loading branch information
alexcrichton committed May 30, 2017
1 parent 1c48953 commit 7b9e02f
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 351 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "syn"
version = "0.12.0" # don't forget to update version in readme for breaking changes
version = "0.11.11" # don't forget to update version in readme for breaking changes
authors = ["David Tolnay <dtolnay@gmail.com>"]
license = "MIT/Apache-2.0"
description = "Nom parser for Rust source code"
Expand All @@ -18,14 +18,14 @@ parsing = ["unicode-xid", "synom/parsing"]
printing = ["quote", "synom/printing"]
visit = []
fold = []
clone-impls = []
extra-traits = []
clone-impls = ["synom/clone-impls"]
extra-traits = ["synom/extra-traits"]

[dependencies]
quote = { git = 'https://github.com/dtolnay/quote', optional = true }
proc-macro2 = { git = 'https://github.com/alexcrichton/proc-macro2' }
unicode-xid = { version = "0.0.4", optional = true }
synom = { version = "0.11", path = "synom", default-features = true }
synom = { version = "0.11", path = "synom" }

[dev-dependencies]
syntex_pos = "0.58"
Expand Down
12 changes: 6 additions & 6 deletions synom/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "synom"
version = "0.12.0"
version = "0.11.3"
authors = ["David Tolnay <dtolnay@gmail.com>"]
license = "MIT/Apache-2.0"
description = "Stripped-down Nom parser used by Syn"
Expand All @@ -14,17 +14,17 @@ publish = false # this branch contains breaking changes
[dependencies]
quote = { git = 'https://github.com/dtolnay/quote', optional = true }
unicode-xid = "0.0.4"
proc-macro2 = { git = 'https://github.com/alexcrichton/proc-macro2' }

[features]
printing = ["quote"]
parsing = []
default = ["parsing"]

[dev-dependencies]
proc-macro2 = { git = 'https://github.com/alexcrichton/proc-macro2' }
default = []
clone-impls = []
extra-traits = []

[dev-dependencies.syn]
version = "0.12"
version = "0.11"
path = ".."
features = ["parsing", "full", "extra-traits"]
default-features = false
71 changes: 70 additions & 1 deletion synom/src/delimited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::iter::FromIterator;
use std::slice;
use std::vec;

#[derive(Eq, PartialEq, Hash, Debug, Clone)]
#[cfg_attr(feature = "extra-traits", derive(Eq, PartialEq, Hash, Debug))]
#[cfg_attr(feature = "clone-impls", derive(Clone))]
pub struct Delimited<T, D> {
inner: Vec<(T, Option<D>)>
}
Expand Down Expand Up @@ -221,6 +222,74 @@ impl<T, D> Element<T, D> {
}
}

#[cfg(feature = "parsing")]
mod parsing {
use proc_macro2::TokenTree;

use super::Delimited;
use {IResult, Synom};

impl<T, D> Delimited<T, D>
where T: Synom,
D: Synom,
{
pub fn parse_separated(input: &[TokenTree])
-> IResult<&[TokenTree], Self>
{
Self::parse(input, false)
}

pub fn parse_terminated(input: &[TokenTree])
-> IResult<&[TokenTree], Self>
{
Self::parse(input, true)
}

fn parse(mut input: &[TokenTree], terminated: bool)
-> IResult<&[TokenTree], Self>
{
let mut res = Delimited::new();

// get the first element
match T::parse(input) {
IResult::Error => IResult::Done(input, res),
IResult::Done(i, o) => {
if i.len() == input.len() {
return IResult::Error
}
input = i;
res.push_first(o);

// get the separator first
while let IResult::Done(i2, s) = D::parse(input) {
if i2.len() == input.len() {
break;
}

// get the element next
if let IResult::Done(i3, o3) = T::parse(i2) {
if i3.len() == i2.len() {
break;
}
res.push_next(o3, s);
input = i3;
} else {
break;
}
}
if terminated {
if let IResult::Done(after, sep) = D::parse(input) {
res.push_trailing(sep);
input = after;
}
}
IResult::Done(input, res)
}
}
}
}
}

#[cfg(feature = "printing")]
mod printing {
use super::*;
Expand Down
Loading

0 comments on commit 7b9e02f

Please sign in to comment.