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 nested macro_rules! #34925

Merged
merged 2 commits into from
Jul 23, 2016
Merged
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
6 changes: 5 additions & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ pub struct Parser<'a> {
pub tokens_consumed: usize,
pub restrictions: Restrictions,
pub quote_depth: usize, // not (yet) related to the quasiquoter
parsing_token_tree: bool,
pub reader: Box<Reader+'a>,
/// The set of seen errors about obsolete syntax. Used to suppress
/// extra detail when the same error is seen twice
Expand Down Expand Up @@ -374,6 +375,7 @@ impl<'a> Parser<'a> {
tokens_consumed: 0,
restrictions: Restrictions::empty(),
quote_depth: 0,
parsing_token_tree: false,
obsolete_set: HashSet::new(),
mod_path_stack: Vec::new(),
filename: filename,
Expand Down Expand Up @@ -2663,7 +2665,7 @@ impl<'a> Parser<'a> {
}

pub fn check_unknown_macro_variable(&mut self) {
if self.quote_depth == 0 {
if self.quote_depth == 0 && !self.parsing_token_tree {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe quote_depth and parsing_token_tree have the exact same intended function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried just using quote_depth and it caused other problems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically, it caused problems with parsing the left hand side of a macro_rules! body. I didn't look into it in too much detail since I wanted to do this PR as simply as possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we remove quote_depth then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so -- looking into it some more, the problem is that this condition needs to be false when parsing the lhs of a macro_rules! body.

Copy link
Member

@eddyb eddyb Jul 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always false because quote_depth is never anything other than 0.
EDIT: Oops, the modifications to quote_depth are in another module.

IMO, the whole distinction between $ ident (two tokens) and $ident (one token) is unnecessary and only creates complications.

Copy link
Contributor Author

@jseyfried jseyfried Jul 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed (out of the scope of this PR, though).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed quote_depth and parsing_token_tree in #39419.

match self.token {
token::SubstNt(name) =>
self.fatal(&format!("unknown macro variable `{}`", name)).emit(),
Expand Down Expand Up @@ -2723,6 +2725,7 @@ impl<'a> Parser<'a> {
Err(err)
},
token::OpenDelim(delim) => {
let parsing_token_tree = ::std::mem::replace(&mut self.parsing_token_tree, true);
// The span for beginning of the delimited section
let pre_span = self.span;

Expand Down Expand Up @@ -2787,6 +2790,7 @@ impl<'a> Parser<'a> {
_ => {}
}

self.parsing_token_tree = parsing_token_tree;
Ok(TokenTree::Delimited(span, Rc::new(Delimited {
delim: delim,
open_span: open_span,
Expand Down
9 changes: 9 additions & 0 deletions src/test/run-pass/macro-of-higher-order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ macro_rules! higher_order {
});
}

macro_rules! outer {
($x:expr; $fragment:ident) => {
macro_rules! inner { ($y:$fragment) => { $x + $y } }
}
}

fn main() {
let val = higher_order!(subst ($x:expr, $y:expr, $foo:expr) => (($x + $y, $foo)));
assert_eq!(val, (3, "foo"));

outer!(2; expr);
assert_eq!(inner!(3), 5);
}