X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=crates%2Fmbe%2Fsrc%2Flib.rs;h=6402ceadaaae6d824e29bf2a95c344ae86fd3a4f;hb=0b53744f2d7e0694cd7207cca632fd6de1dc5bff;hp=6cd084eaeab6be3c7858f77d72828ff2106b4635;hpb=634f047d9083ec4f69ca77b564fafa866f102280;p=rust.git diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs index 6cd084eaeab..6402ceadaaa 100644 --- a/crates/mbe/src/lib.rs +++ b/crates/mbe/src/lib.rs @@ -10,7 +10,7 @@ mod expander; mod syntax_bridge; mod tt_iter; -mod subtree_source; +mod to_parser_input; #[cfg(test)] mod benchmark; @@ -24,17 +24,36 @@ }; // FIXME: we probably should re-think `token_tree_to_syntax_node` interfaces -pub use ::parser::ParserEntryPoint; +pub use ::parser::TopEntryPoint; pub use tt::{Delimiter, DelimiterKind, Punct}; +pub use crate::{ + syntax_bridge::{ + parse_exprs_with_sep, parse_to_token_tree, syntax_node_to_token_tree, + syntax_node_to_token_tree_with_modifications, token_tree_to_syntax_node, SyntheticToken, + SyntheticTokenId, + }, + token_map::TokenMap, +}; + #[derive(Debug, PartialEq, Eq, Clone)] pub enum ParseError { - UnexpectedToken(String), - Expected(String), + UnexpectedToken(Box), + Expected(Box), InvalidRepeat, RepetitionEmptyTokenTree, } +impl ParseError { + fn expected(e: &str) -> ParseError { + ParseError::Expected(e.into()) + } + + fn unexpected(e: &str) -> ParseError { + ParseError::UnexpectedToken(e.into()) + } +} + impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { @@ -48,13 +67,18 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { #[derive(Debug, PartialEq, Eq, Clone)] pub enum ExpandError { + BindingError(Box>), + LeftoverTokens, + ConversionError, + LimitExceeded, NoMatchingRule, UnexpectedToken, - BindingError(String), - ConversionError, - // FIXME: no way mbe should know about proc macros. - UnresolvedProcMacro, - Other(String), +} + +impl ExpandError { + fn binding_error(e: impl Into>) -> ExpandError { + ExpandError::BindingError(Box::new(e.into())) + } } impl fmt::Display for ExpandError { @@ -64,34 +88,18 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { ExpandError::UnexpectedToken => f.write_str("unexpected token in input"), ExpandError::BindingError(e) => f.write_str(e), ExpandError::ConversionError => f.write_str("could not convert tokens"), - ExpandError::UnresolvedProcMacro => f.write_str("unresolved proc macro"), - ExpandError::Other(e) => f.write_str(e), + ExpandError::LimitExceeded => f.write_str("Expand exceed limit"), + ExpandError::LeftoverTokens => f.write_str("leftover tokens"), } } } -pub use crate::{ - syntax_bridge::{ - parse_exprs_with_sep, parse_to_token_tree, syntax_node_to_token_tree, - syntax_node_to_token_tree_censored, token_tree_to_syntax_node, - }, - token_map::TokenMap, -}; - /// This struct contains AST for a single `macro_rules` definition. What might /// be very confusing is that AST has almost exactly the same shape as /// `tt::TokenTree`, but there's a crucial difference: in macro rules, `$ident` /// and `$()*` have special meaning (see `Var` and `Repeat` data structures) #[derive(Clone, Debug, PartialEq, Eq)] -pub struct MacroRules { - rules: Vec, - /// Highest id of the token we have in TokenMap - shift: Shift, -} - -/// For Macro 2.0 -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct MacroDef { +pub struct DeclarativeMacro { rules: Vec, /// Highest id of the token we have in TokenMap shift: Shift, @@ -115,27 +123,25 @@ pub fn new(tt: &tt::Subtree) -> Shift { // Find the max token id inside a subtree fn max_id(subtree: &tt::Subtree) -> Option { - subtree - .token_trees - .iter() - .filter_map(|tt| match tt { - tt::TokenTree::Subtree(subtree) => { - let tree_id = max_id(subtree); - match subtree.delimiter { - Some(it) if it.id != tt::TokenId::unspecified() => { - Some(tree_id.map_or(it.id.0, |t| t.max(it.id.0))) - } - _ => tree_id, + let filter = |tt: &_| match tt { + tt::TokenTree::Subtree(subtree) => { + let tree_id = max_id(subtree); + match subtree.delimiter { + Some(it) if it.id != tt::TokenId::unspecified() => { + Some(tree_id.map_or(it.id.0, |t| t.max(it.id.0))) } + _ => tree_id, } - tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) - if ident.id != tt::TokenId::unspecified() => - { - Some(ident.id.0) - } - _ => None, - }) - .max() + } + tt::TokenTree::Leaf(leaf) => { + let &(tt::Leaf::Ident(tt::Ident { id, .. }) + | tt::Leaf::Punct(tt::Punct { id, .. }) + | tt::Leaf::Literal(tt::Literal { id, .. })) = leaf; + + (id != tt::TokenId::unspecified()).then(|| id.0) + } + }; + subtree.token_trees.iter().filter_map(filter).max() } } @@ -143,15 +149,15 @@ fn max_id(subtree: &tt::Subtree) -> Option { pub fn shift_all(self, tt: &mut tt::Subtree) { for t in &mut tt.token_trees { match t { - tt::TokenTree::Leaf(leaf) => match leaf { - tt::Leaf::Ident(ident) => ident.id = self.shift(ident.id), - tt::Leaf::Punct(punct) => punct.id = self.shift(punct.id), - tt::Leaf::Literal(lit) => lit.id = self.shift(lit.id), - }, + tt::TokenTree::Leaf( + tt::Leaf::Ident(tt::Ident { id, .. }) + | tt::Leaf::Punct(tt::Punct { id, .. }) + | tt::Leaf::Literal(tt::Literal { id, .. }), + ) => *id = self.shift(*id), tt::TokenTree::Subtree(tt) => { if let Some(it) = tt.delimiter.as_mut() { it.id = self.shift(it.id); - }; + } self.shift_all(tt) } } @@ -160,9 +166,10 @@ pub fn shift_all(self, tt: &mut tt::Subtree) { pub fn shift(self, id: tt::TokenId) -> tt::TokenId { if id == tt::TokenId::unspecified() { - return id; + id + } else { + tt::TokenId(id.0 + self.0) } - tt::TokenId(id.0 + self.0) } pub fn unshift(self, id: tt::TokenId) -> Option { @@ -176,8 +183,9 @@ pub enum Origin { Call, } -impl MacroRules { - pub fn parse(tt: &tt::Subtree) -> Result { +impl DeclarativeMacro { + /// The old, `macro_rules! m {}` flavor. + pub fn parse_macro_rules(tt: &tt::Subtree) -> Result { // Note: this parsing can be implemented using mbe machinery itself, by // matching against `$($lhs:tt => $rhs:tt);*` pattern, but implementing // manually seems easier. @@ -188,40 +196,21 @@ pub fn parse(tt: &tt::Subtree) -> Result { rules.push(rule); if let Err(()) = src.expect_char(';') { if src.len() > 0 { - return Err(ParseError::Expected("expected `;`".to_string())); + return Err(ParseError::expected("expected `;`")); } break; } } - for rule in &rules { - validate(&rule.lhs)?; + for Rule { lhs, .. } in &rules { + validate(lhs)?; } - Ok(MacroRules { rules, shift: Shift::new(tt) }) - } - - pub fn expand(&self, tt: &tt::Subtree) -> ExpandResult { - // apply shift - let mut tt = tt.clone(); - self.shift.shift_all(&mut tt); - expander::expand_rules(&self.rules, &tt) - } - - pub fn map_id_down(&self, id: tt::TokenId) -> tt::TokenId { - self.shift.shift(id) - } - - pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, Origin) { - match self.shift.unshift(id) { - Some(id) => (id, Origin::Call), - None => (id, Origin::Def), - } + Ok(DeclarativeMacro { rules, shift: Shift::new(tt) }) } -} -impl MacroDef { - pub fn parse(tt: &tt::Subtree) -> Result { + /// The new, unstable `macro m {}` flavor. + pub fn parse_macro2(tt: &tt::Subtree) -> Result { let mut src = TtIter::new(tt); let mut rules = Vec::new(); @@ -232,9 +221,7 @@ pub fn parse(tt: &tt::Subtree) -> Result { rules.push(rule); if let Err(()) = src.expect_any_char(&[';', ',']) { if src.len() > 0 { - return Err(ParseError::Expected( - "expected `;` or `,` to delimit rules".to_string(), - )); + return Err(ParseError::expected("expected `;` or `,` to delimit rules")); } break; } @@ -243,15 +230,16 @@ pub fn parse(tt: &tt::Subtree) -> Result { cov_mark::hit!(parse_macro_def_simple); let rule = Rule::parse(&mut src, false)?; if src.len() != 0 { - return Err(ParseError::Expected("remain tokens in macro def".to_string())); + return Err(ParseError::expected("remaining tokens in macro def")); } rules.push(rule); } - for rule in &rules { - validate(&rule.lhs)?; + + for Rule { lhs, .. } in &rules { + validate(lhs)?; } - Ok(MacroDef { rules, shift: Shift::new(tt) }) + Ok(DeclarativeMacro { rules, shift: Shift::new(tt) }) } pub fn expand(&self, tt: &tt::Subtree) -> ExpandResult { @@ -271,20 +259,20 @@ pub fn map_id_up(&self, id: tt::TokenId) -> (tt::TokenId, Origin) { None => (id, Origin::Def), } } + + pub fn shift(&self) -> Shift { + self.shift + } } impl Rule { fn parse(src: &mut TtIter, expect_arrow: bool) -> Result { - let lhs = src - .expect_subtree() - .map_err(|()| ParseError::Expected("expected subtree".to_string()))?; + let lhs = src.expect_subtree().map_err(|()| ParseError::expected("expected subtree"))?; if expect_arrow { - src.expect_char('=').map_err(|()| ParseError::Expected("expected `=`".to_string()))?; - src.expect_char('>').map_err(|()| ParseError::Expected("expected `>`".to_string()))?; + src.expect_char('=').map_err(|()| ParseError::expected("expected `=`"))?; + src.expect_char('>').map_err(|()| ParseError::expected("expected `>`"))?; } - let rhs = src - .expect_subtree() - .map_err(|()| ParseError::Expected("expected subtree".to_string()))?; + let rhs = src.expect_subtree().map_err(|()| ParseError::expected("expected subtree"))?; let lhs = MetaTemplate::parse_pattern(lhs)?; let rhs = MetaTemplate::parse_template(rhs)?; @@ -300,28 +288,18 @@ fn validate(pattern: &MetaTemplate) -> Result<(), ParseError> { Op::Repeat { tokens: subtree, separator, .. } => { // Checks that no repetition which could match an empty token // https://github.com/rust-lang/rust/blob/a58b1ed44f5e06976de2bdc4d7dc81c36a96934f/src/librustc_expand/mbe/macro_rules.rs#L558 - - if separator.is_none() - && subtree.iter().all(|child_op| { - match child_op { - Op::Var { kind, .. } => { - // vis is optional - if kind.as_ref().map_or(false, |it| it == "vis") { - return true; - } - } - Op::Repeat { kind, .. } => { - return matches!( - kind, - parser::RepeatKind::ZeroOrMore | parser::RepeatKind::ZeroOrOne - ) - } - Op::Leaf(_) => {} - Op::Subtree { .. } => {} - } - false - }) - { + let lsh_is_empty_seq = separator.is_none() && subtree.iter().all(|child_op| { + match child_op { + // vis is optional + Op::Var { kind: Some(kind), .. } => kind == "vis", + Op::Repeat { + kind: parser::RepeatKind::ZeroOrMore | parser::RepeatKind::ZeroOrOne, + .. + } => true, + _ => false, + } + }); + if lsh_is_empty_seq { return Err(ParseError::RepetitionEmptyTokenTree); } validate(subtree)? @@ -332,42 +310,41 @@ fn validate(pattern: &MetaTemplate) -> Result<(), ParseError> { Ok(()) } +pub type ExpandResult = ValueResult; + #[derive(Debug, Clone, Eq, PartialEq)] -pub struct ExpandResult { +pub struct ValueResult { pub value: T, - pub err: Option, + pub err: Option, } -impl ExpandResult { +impl ValueResult { pub fn ok(value: T) -> Self { Self { value, err: None } } - pub fn only_err(err: ExpandError) -> Self + pub fn only_err(err: E) -> Self where T: Default, { Self { value: Default::default(), err: Some(err) } } - pub fn str_err(err: String) -> Self - where - T: Default, - { - Self::only_err(ExpandError::Other(err)) + pub fn map(self, f: impl FnOnce(T) -> U) -> ValueResult { + ValueResult { value: f(self.value), err: self.err } } - pub fn map(self, f: impl FnOnce(T) -> U) -> ExpandResult { - ExpandResult { value: f(self.value), err: self.err } + pub fn map_err(self, f: impl FnOnce(E) -> E2) -> ValueResult { + ValueResult { value: self.value, err: self.err.map(f) } } - pub fn result(self) -> Result { + pub fn result(self) -> Result { self.err.map_or(Ok(self.value), Err) } } -impl From> for ExpandResult { - fn from(result: Result) -> Self { +impl From> for ValueResult { + fn from(result: Result) -> Self { result.map_or_else(Self::only_err, Self::ok) } }