#[cfg(test)]
mod benchmark;
+mod token_map;
use std::fmt;
-pub use tt::{Delimiter, DelimiterKind, Punct};
-
use crate::{
parser::{parse_pattern, parse_template, MetaTemplate, Op},
tt_iter::TtIter,
};
+// FIXME: we probably should re-think `token_tree_to_syntax_node` interfaces
+pub use ::parser::FragmentKind;
+pub use tt::{Delimiter, DelimiterKind, Punct};
+
#[derive(Debug, PartialEq, Eq)]
pub enum ParseError {
UnexpectedToken(String),
UnexpectedToken,
BindingError(String),
ConversionError,
- ProcMacroError(tt::ExpansionError),
+ // FIXME: no way mbe should know about proc macros.
UnresolvedProcMacro,
Other(String),
}
-impl From<tt::ExpansionError> for ExpandError {
- fn from(it: tt::ExpansionError) -> Self {
- ExpandError::ProcMacroError(it)
- }
-}
-
impl fmt::Display for ExpandError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
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::ProcMacroError(e) => e.fmt(f),
ExpandError::UnresolvedProcMacro => f.write_str("unresolved proc macro"),
ExpandError::Other(e) => f.write_str(e),
}
}
}
-pub use crate::syntax_bridge::{
- ast_to_token_tree, parse_exprs_with_sep, parse_to_token_tree, syntax_node_to_token_tree,
- token_tree_to_syntax_node, TokenMap,
+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
rhs: MetaTemplate,
}
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-struct Shift(u32);
+#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
+pub struct Shift(u32);
impl Shift {
- fn new(tt: &tt::Subtree) -> Shift {
+ pub fn new(tt: &tt::Subtree) -> Shift {
// Note that TokenId is started from zero,
// We have to add 1 to prevent duplication.
let value = max_id(tt).map_or(0, |it| it + 1);
}
/// Shift given TokenTree token id
- fn shift_all(self, tt: &mut tt::Subtree) {
- for t in tt.token_trees.iter_mut() {
+ 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),
}
}
- fn shift(self, id: tt::TokenId) -> tt::TokenId {
+ pub fn shift(self, id: tt::TokenId) -> tt::TokenId {
if id == tt::TokenId::unspecified() {
return id;
}
tt::TokenId(id.0 + self.0)
}
- fn unshift(self, id: tt::TokenId) -> Option<tt::TokenId> {
+ pub fn unshift(self, id: tt::TokenId) -> Option<tt::TokenId> {
id.0.checked_sub(self.0).map(tt::TokenId)
}
}
}
}
- for rule in rules.iter() {
+ for rule in &rules {
validate(&rule.lhs)?;
}
}
rules.push(rule);
}
- for rule in rules.iter() {
+ for rule in &rules {
validate(&rule.lhs)?;
}
}
impl Rule {
- fn parse(src: &mut TtIter, expect_arrow: bool) -> Result<Rule, ParseError> {
+ fn parse(src: &mut TtIter, expect_arrow: bool) -> Result<Self, ParseError> {
let lhs = src
.expect_subtree()
.map_err(|()| ParseError::Expected("expected subtree".to_string()))?;
.expect_subtree()
.map_err(|()| ParseError::Expected("expected subtree".to_string()))?;
- let lhs = MetaTemplate(parse_pattern(&lhs)?);
- let rhs = MetaTemplate(parse_template(&rhs)?);
+ let lhs = MetaTemplate(parse_pattern(lhs)?);
+ let rhs = MetaTemplate(parse_template(rhs)?);
Ok(crate::Rule { lhs, rhs })
}
fn validate(pattern: &MetaTemplate) -> Result<(), ParseError> {
for op in pattern.iter() {
match op {
- Op::Subtree { tokens, .. } => validate(&tokens)?,
+ Op::Subtree { tokens, .. } => validate(tokens)?,
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() {
- if subtree.iter().all(|child_op| {
+ if separator.is_none()
+ && subtree.iter().all(|child_op| {
match child_op {
Op::Var { kind, .. } => {
// vis is optional
Op::Subtree { .. } => {}
}
false
- }) {
- return Err(ParseError::RepetitionEmptyTokenTree);
- }
+ })
+ {
+ return Err(ParseError::RepetitionEmptyTokenTree);
}
validate(subtree)?
}
}
pub fn result(self) -> Result<T, ExpandError> {
- self.err.map(Err).unwrap_or(Ok(self.value))
+ self.err.map_or(Ok(self.value), Err)
}
}