Skip to content

Commit a5814a5

Browse files
Move parse_cfg_select to rustc_builtin_macros
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
1 parent ab67c37 commit a5814a5

File tree

5 files changed

+84
-72
lines changed

5 files changed

+84
-72
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use rustc_ast::token::Token;
2+
use rustc_ast::tokenstream::TokenStream;
3+
use rustc_ast::{MetaItemInner, token};
4+
use rustc_errors::PResult;
5+
use rustc_parse::exp;
6+
use rustc_parse::parser::Parser;
7+
use rustc_span::Span;
8+
9+
pub enum CfgSelectPredicate {
10+
Cfg(MetaItemInner),
11+
Wildcard(Token),
12+
}
13+
14+
#[derive(Default)]
15+
pub struct CfgSelectBranches {
16+
/// All the conditional branches.
17+
pub reachable: Vec<(MetaItemInner, TokenStream, Span)>,
18+
/// The first wildcard `_ => { ... }` branch.
19+
pub wildcard: Option<(Token, TokenStream, Span)>,
20+
/// All branches after the first wildcard, including further wildcards.
21+
/// These branches are kept for formatting.
22+
pub unreachable: Vec<(CfgSelectPredicate, TokenStream, Span)>,
23+
}
24+
25+
pub fn parse_cfg_select<'a>(p: &mut Parser<'a>) -> PResult<'a, CfgSelectBranches> {
26+
let mut branches = CfgSelectBranches::default();
27+
28+
while p.token != token::Eof {
29+
if p.eat_keyword(exp!(Underscore)) {
30+
let underscore = p.prev_token;
31+
p.expect(exp!(FatArrow))?;
32+
33+
let tts = p.parse_delimited_token_tree()?;
34+
let span = underscore.span.to(p.token.span);
35+
36+
match branches.wildcard {
37+
None => branches.wildcard = Some((underscore, tts, span)),
38+
Some(_) => {
39+
branches.unreachable.push((CfgSelectPredicate::Wildcard(underscore), tts, span))
40+
}
41+
}
42+
} else {
43+
let meta_item = p.parse_meta_item_inner()?;
44+
p.expect(exp!(FatArrow))?;
45+
46+
let tts = p.parse_delimited_token_tree()?;
47+
let span = meta_item.span().to(p.token.span);
48+
49+
match branches.wildcard {
50+
None => branches.reachable.push((meta_item, tts, span)),
51+
Some(_) => {
52+
branches.unreachable.push((CfgSelectPredicate::Cfg(meta_item), tts, span))
53+
}
54+
}
55+
}
56+
}
57+
58+
Ok(branches)
59+
}

compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub(crate) mod allow_unstable;
3333
pub(crate) mod body;
3434
pub(crate) mod cfg;
3535
pub(crate) mod cfg_old;
36+
pub(crate) mod cfg_select;
3637
pub(crate) mod codegen_attrs;
3738
pub(crate) mod confusables;
3839
pub(crate) mod crate_level;

compiler/rustc_attr_parsing/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ pub use attributes::cfg::{
106106
CFG_TEMPLATE, EvalConfigResult, eval_config_entry, parse_cfg, parse_cfg_attr, parse_cfg_entry,
107107
};
108108
pub use attributes::cfg_old::*;
109+
pub use attributes::cfg_select::*;
109110
pub use attributes::util::{is_builtin_attr, is_doc_alias_attrs_contain_symbol, parse_version};
110111
pub use context::{Early, Late, OmitDoc, ShouldEmit};
111112
pub use interface::AttributeParser;

compiler/rustc_builtin_macros/src/cfg_select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_ast::tokenstream::TokenStream;
22
use rustc_attr_parsing as attr;
3+
use rustc_attr_parsing::{CfgSelectBranches, CfgSelectPredicate, parse_cfg_select};
34
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult};
4-
use rustc_parse::parser::cfg_select::{CfgSelectBranches, CfgSelectPredicate, parse_cfg_select};
55
use rustc_span::{Ident, Span, sym};
66

77
use crate::errors::{CfgSelectNoMatches, CfgSelectUnreachable};
Lines changed: 22 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,34 @@
1-
use rustc_ast::token::Token;
1+
use rustc_ast::token;
22
use rustc_ast::tokenstream::{TokenStream, TokenTree};
33
use rustc_ast::util::classify;
4-
use rustc_ast::{MetaItemInner, token};
54
use rustc_errors::PResult;
6-
use rustc_span::Span;
75

86
use crate::exp;
97
use crate::parser::{AttrWrapper, ForceCollect, Parser, Restrictions, Trailing, UsePreAttrPos};
108

11-
pub enum CfgSelectPredicate {
12-
Cfg(MetaItemInner),
13-
Wildcard(Token),
14-
}
15-
16-
#[derive(Default)]
17-
pub struct CfgSelectBranches {
18-
/// All the conditional branches.
19-
pub reachable: Vec<(MetaItemInner, TokenStream, Span)>,
20-
/// The first wildcard `_ => { ... }` branch.
21-
pub wildcard: Option<(Token, TokenStream, Span)>,
22-
/// All branches after the first wildcard, including further wildcards.
23-
/// These branches are kept for formatting.
24-
pub unreachable: Vec<(CfgSelectPredicate, TokenStream, Span)>,
25-
}
26-
27-
/// Parses a `TokenTree` consisting either of `{ /* ... */ }` (and strip the braces) or an
28-
/// expression followed by a comma (and strip the comma).
29-
fn parse_token_tree<'a>(p: &mut Parser<'a>) -> PResult<'a, TokenStream> {
30-
if p.token == token::OpenBrace {
31-
// Strip the outer '{' and '}'.
32-
match p.parse_token_tree() {
33-
TokenTree::Token(..) => unreachable!("because of the expect above"),
34-
TokenTree::Delimited(.., tts) => return Ok(tts),
35-
}
36-
}
37-
let expr = p.collect_tokens(None, AttrWrapper::empty(), ForceCollect::Yes, |p, _| {
38-
p.parse_expr_res(Restrictions::STMT_EXPR, AttrWrapper::empty())
39-
.map(|(expr, _)| (expr, Trailing::No, UsePreAttrPos::No))
40-
})?;
41-
if !classify::expr_is_complete(&expr) && p.token != token::CloseBrace && p.token != token::Eof {
42-
p.expect(exp!(Comma))?;
43-
} else {
44-
let _ = p.eat(exp!(Comma));
45-
}
46-
Ok(TokenStream::from_ast(&expr))
47-
}
48-
49-
pub fn parse_cfg_select<'a>(p: &mut Parser<'a>) -> PResult<'a, CfgSelectBranches> {
50-
let mut branches = CfgSelectBranches::default();
51-
52-
while p.token != token::Eof {
53-
if p.eat_keyword(exp!(Underscore)) {
54-
let underscore = p.prev_token;
55-
p.expect(exp!(FatArrow))?;
56-
57-
let tts = parse_token_tree(p)?;
58-
let span = underscore.span.to(p.token.span);
59-
60-
match branches.wildcard {
61-
None => branches.wildcard = Some((underscore, tts, span)),
62-
Some(_) => {
63-
branches.unreachable.push((CfgSelectPredicate::Wildcard(underscore), tts, span))
64-
}
9+
impl<'a> Parser<'a> {
10+
/// Parses a `TokenTree` consisting either of `{ /* ... */ }` (and strip the braces) or an
11+
/// expression followed by a comma (and strip the comma).
12+
pub fn parse_delimited_token_tree(&mut self) -> PResult<'a, TokenStream> {
13+
if self.token == token::OpenBrace {
14+
// Strip the outer '{' and '}'.
15+
match self.parse_token_tree() {
16+
TokenTree::Token(..) => unreachable!("because of the expect above"),
17+
TokenTree::Delimited(.., tts) => return Ok(tts),
6518
}
19+
}
20+
let expr = self.collect_tokens(None, AttrWrapper::empty(), ForceCollect::Yes, |p, _| {
21+
p.parse_expr_res(Restrictions::STMT_EXPR, AttrWrapper::empty())
22+
.map(|(expr, _)| (expr, Trailing::No, UsePreAttrPos::No))
23+
})?;
24+
if !classify::expr_is_complete(&expr)
25+
&& self.token != token::CloseBrace
26+
&& self.token != token::Eof
27+
{
28+
self.expect(exp!(Comma))?;
6629
} else {
67-
let meta_item = p.parse_meta_item_inner()?;
68-
p.expect(exp!(FatArrow))?;
69-
70-
let tts = parse_token_tree(p)?;
71-
let span = meta_item.span().to(p.token.span);
72-
73-
match branches.wildcard {
74-
None => branches.reachable.push((meta_item, tts, span)),
75-
Some(_) => {
76-
branches.unreachable.push((CfgSelectPredicate::Cfg(meta_item), tts, span))
77-
}
78-
}
30+
let _ = self.eat(exp!(Comma));
7931
}
32+
Ok(TokenStream::from_ast(&expr))
8033
}
81-
82-
Ok(branches)
8334
}

0 commit comments

Comments
 (0)