Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support parsing multi-character Punct separators #20

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl From<parser::Matcher> for Matcher {
Matcher::Repeat { content: content.into_iter().map(|m| m.into()).collect(),
seperator: separator.map(|s| match s {
parser::Separator::Punct(p) => p.to_string(),
parser::Separator::PunctGroup(s) => s.to_string(),
parser::Separator::Literal(l) => l.to_string(),
parser::Separator::Ident(i) => i.to_string(),
}),
Expand Down
108 changes: 107 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Parses a raw `macro_rules!`-string.

use std::fmt;

use proc_macro2::{Delimiter, Ident, Literal, Punct, TokenStream, TokenTree};

use syn;
Expand Down Expand Up @@ -51,8 +53,18 @@ pub enum Repetition {
AtMostOnce,
}

#[derive(Debug)]
pub struct PunctGroup(&'static str);

impl fmt::Display for PunctGroup {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}

#[derive(Debug)]
pub enum Separator {
PunctGroup(PunctGroup),
Punct(Punct),
Ident(Ident),
Literal(Literal),
Expand Down Expand Up @@ -195,6 +207,94 @@ impl Parse for Matcher {
}
}

impl PunctGroup {
fn peek(input: ParseStream) -> bool {
input.peek(Token![+=])
|| input.peek(Token![&&])
|| input.peek(Token![&=])
|| input.peek(Token![^=])
|| input.peek(Token![::])
|| input.peek(Token![/=])
|| input.peek(Token![..])
|| input.peek(Token![...])
|| input.peek(Token![..=])
|| input.peek(Token![==])
|| input.peek(Token![>=])
|| input.peek(Token![<=])
|| input.peek(Token![*=])
|| input.peek(Token![!=])
|| input.peek(Token![|=])
|| input.peek(Token![||])
|| input.peek(Token![->])
|| input.peek(Token![<-])
|| input.peek(Token![%=])
|| input.peek(Token![=>])
|| input.peek(Token![<<])
|| input.peek(Token![<<=])
|| input.peek(Token![>>])
|| input.peek(Token![>>=])
|| input.peek(Token![-=])
}
}

impl Parse for PunctGroup {
fn parse(input: ParseStream) -> Result<Self> {
if input.parse::<Option<Token![+=]>>()?.is_some() {
Ok(PunctGroup("+="))
} else if input.parse::<Option<Token![&&]>>()?.is_some() {
Ok(PunctGroup("&&"))
} else if input.parse::<Option<Token![&=]>>()?.is_some() {
Ok(PunctGroup("&="))
} else if input.parse::<Option<Token![^=]>>()?.is_some() {
Ok(PunctGroup("^="))
} else if input.parse::<Option<Token![::]>>()?.is_some() {
Ok(PunctGroup("::"))
} else if input.parse::<Option<Token![/=]>>()?.is_some() {
Ok(PunctGroup("/="))
} else if input.parse::<Option<Token![..]>>()?.is_some() {
Ok(PunctGroup(".."))
} else if input.parse::<Option<Token![...]>>()?.is_some() {
Ok(PunctGroup("..."))
} else if input.parse::<Option<Token![..=]>>()?.is_some() {
Ok(PunctGroup("..="))
} else if input.parse::<Option<Token![==]>>()?.is_some() {
Ok(PunctGroup("=="))
} else if input.parse::<Option<Token![>=]>>()?.is_some() {
Ok(PunctGroup(">="))
} else if input.parse::<Option<Token![<=]>>()?.is_some() {
Ok(PunctGroup("<="))
} else if input.parse::<Option<Token![*=]>>()?.is_some() {
Ok(PunctGroup("*="))
} else if input.parse::<Option<Token![!=]>>()?.is_some() {
Ok(PunctGroup("!="))
} else if input.parse::<Option<Token![|=]>>()?.is_some() {
Ok(PunctGroup("|="))
} else if input.parse::<Option<Token![||]>>()?.is_some() {
Ok(PunctGroup("||"))
} else if input.parse::<Option<Token![->]>>()?.is_some() {
Ok(PunctGroup("->"))
} else if input.parse::<Option<Token![<-]>>()?.is_some() {
Ok(PunctGroup("<-"))
} else if input.parse::<Option<Token![%=]>>()?.is_some() {
Ok(PunctGroup("%="))
} else if input.parse::<Option<Token![=>]>>()?.is_some() {
Ok(PunctGroup("=>"))
} else if input.parse::<Option<Token![<<]>>()?.is_some() {
Ok(PunctGroup("<<"))
} else if input.parse::<Option<Token![<<=]>>()?.is_some() {
Ok(PunctGroup("<<="))
} else if input.parse::<Option<Token![>>]>>()?.is_some() {
Ok(PunctGroup(">>"))
} else if input.parse::<Option<Token![>>=]>>()?.is_some() {
Ok(PunctGroup(">>="))
} else if input.parse::<Option<Token![-=]>>()?.is_some() {
Ok(PunctGroup("-="))
} else {
Err(input.error("expected punct group"))
}
}
}

impl Separator {
fn parse_optional(input: ParseStream) -> Result<Option<Self>> {
if input.peek(Token![*]) || input.peek(Token![+]) || input.peek(Token![?]) {
Expand All @@ -207,9 +307,12 @@ impl Separator {

impl Parse for Separator {
fn parse(input: ParseStream) -> Result<Self> {
if PunctGroup::peek(input) {
return Ok(Separator::PunctGroup(input.parse::<PunctGroup>()?));
}

Ok(match input.parse()? {
TokenTree::Ident(ident) => Separator::Ident(ident),
// FIXME: multi-character punctuation
TokenTree::Punct(punct) => Separator::Punct(punct),
TokenTree::Literal(literal) => Separator::Literal(literal),
TokenTree::Group(group) => {
Expand Down Expand Up @@ -343,6 +446,9 @@ r#"macro_rules! input_end {
r#"macro_rules! apply {
($i:expr, $fun:expr, $($args:expr),* ) => { ... };
}"#,
r#"macro_rules! a {
($($m:ident)::+) => {};
}"#
][..];
for src in fixture {
parse(&src).expect(src);
Expand Down