]> git.lizzy.rs Git - rust.git/blobdiff - crates/mbe/src/parser.rs
Simpilfy mbe parsing
[rust.git] / crates / mbe / src / parser.rs
index 6b46a1673c38e17e012f26478e42143c4f37483d..f891ec29c6e80b1dacf5fb88b6aac9a74de23da2 100644 (file)
@@ -3,17 +3,19 @@
 
 use smallvec::SmallVec;
 use syntax::SmolStr;
+use tt::Delimiter;
 
-use crate::{tt_iter::TtIter, ExpandError};
+use crate::{tt_iter::TtIter, MetaTemplate, ParseError};
 
-#[derive(Debug)]
-pub(crate) enum Op<'a> {
-    Var { name: &'a SmolStr, kind: Option<&'a SmolStr> },
-    Repeat { subtree: &'a tt::Subtree, kind: RepeatKind, separator: Option<Separator> },
-    TokenTree(&'a tt::TokenTree),
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub(crate) enum Op {
+    Var { name: SmolStr, kind: Option<SmolStr>, id: tt::TokenId },
+    Repeat { tokens: MetaTemplate, kind: RepeatKind, separator: Option<Separator> },
+    Leaf(tt::Leaf),
+    Subtree { tokens: MetaTemplate, delimiter: Option<Delimiter> },
 }
 
-#[derive(Clone, Debug, PartialEq, Eq)]
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
 pub(crate) enum RepeatKind {
     ZeroOrMore,
     OneOrMore,
@@ -45,16 +47,12 @@ fn eq(&self, other: &Separator) -> bool {
     }
 }
 
-pub(crate) fn parse_template(
-    template: &tt::Subtree,
-) -> impl Iterator<Item = Result<Op<'_>, ExpandError>> {
-    parse_inner(template, Mode::Template)
+pub(crate) fn parse_template(template: &tt::Subtree) -> Result<Vec<Op>, ParseError> {
+    parse_inner(&template, Mode::Template).into_iter().collect()
 }
 
-pub(crate) fn parse_pattern(
-    pattern: &tt::Subtree,
-) -> impl Iterator<Item = Result<Op<'_>, ExpandError>> {
-    parse_inner(pattern, Mode::Pattern)
+pub(crate) fn parse_pattern(pattern: &tt::Subtree) -> Result<Vec<Op>, ParseError> {
+    parse_inner(&pattern, Mode::Pattern).into_iter().collect()
 }
 
 #[derive(Clone, Copy)]
@@ -63,17 +61,18 @@ enum Mode {
     Template,
 }
 
-fn parse_inner(src: &tt::Subtree, mode: Mode) -> impl Iterator<Item = Result<Op<'_>, ExpandError>> {
-    let mut src = TtIter::new(src);
+fn parse_inner(tt: &tt::Subtree, mode: Mode) -> Vec<Result<Op, ParseError>> {
+    let mut src = TtIter::new(&tt);
     std::iter::from_fn(move || {
         let first = src.next()?;
         Some(next_op(first, &mut src, mode))
     })
+    .collect()
 }
 
 macro_rules! err {
     ($($tt:tt)*) => {
-        ExpandError::UnexpectedToken
+        ParseError::UnexpectedToken(($($tt)*).to_string())
     };
 }
 
@@ -83,35 +82,50 @@ macro_rules! bail {
     };
 }
 
-fn next_op<'a>(
-    first: &'a tt::TokenTree,
-    src: &mut TtIter<'a>,
-    mode: Mode,
-) -> Result<Op<'a>, ExpandError> {
+fn next_op<'a>(first: &tt::TokenTree, src: &mut TtIter<'a>, mode: Mode) -> Result<Op, ParseError> {
     let res = match first {
-        tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: '$', .. })) => {
+        tt::TokenTree::Leaf(leaf @ tt::Leaf::Punct(tt::Punct { char: '$', .. })) => {
             // Note that the '$' itself is a valid token inside macro_rules.
             let second = match src.next() {
-                None => return Ok(Op::TokenTree(first)),
+                None => return Ok(Op::Leaf(leaf.clone())),
                 Some(it) => it,
             };
             match second {
                 tt::TokenTree::Subtree(subtree) => {
                     let (separator, kind) = parse_repeat(src)?;
-                    Op::Repeat { subtree, separator, kind }
+                    let tokens = parse_inner(&subtree, mode)
+                        .into_iter()
+                        .collect::<Result<Vec<Op>, ParseError>>()?;
+                    Op::Repeat { tokens: MetaTemplate(tokens), separator, kind }
                 }
                 tt::TokenTree::Leaf(leaf) => match leaf {
-                    tt::Leaf::Punct(..) => return Err(ExpandError::UnexpectedToken),
+                    tt::Leaf::Punct(punct) => {
+                        static UNDERSCORE: SmolStr = SmolStr::new_inline("_");
+
+                        if punct.char != '_' {
+                            return Err(ParseError::Expected("_".to_string()));
+                        }
+                        let name = UNDERSCORE.clone();
+                        let kind = eat_fragment_kind(src, mode)?;
+                        let id = punct.id;
+                        Op::Var { name, kind, id }
+                    }
+                    tt::Leaf::Ident(ident) if ident.text == "crate" => {
+                        // We simply produce identifier `$crate` here. And it will be resolved when lowering ast to Path.
+                        Op::Leaf(tt::Leaf::from(tt::Ident { text: "$crate".into(), id: ident.id }))
+                    }
                     tt::Leaf::Ident(ident) => {
-                        let name = &ident.text;
+                        let name = ident.text.clone();
                         let kind = eat_fragment_kind(src, mode)?;
-                        Op::Var { name, kind }
+                        let id = ident.id;
+                        Op::Var { name, kind, id }
                     }
                     tt::Leaf::Literal(lit) => {
-                        if is_boolean_literal(lit) {
-                            let name = &lit.text;
+                        if is_boolean_literal(&lit) {
+                            let name = lit.text.clone();
                             let kind = eat_fragment_kind(src, mode)?;
-                            Op::Var { name, kind }
+                            let id = lit.id;
+                            Op::Var { name, kind, id }
                         } else {
                             bail!("bad var 2");
                         }
@@ -119,19 +133,21 @@ fn next_op<'a>(
                 },
             }
         }
-        tt => Op::TokenTree(tt),
+        tt::TokenTree::Leaf(tt) => Op::Leaf(tt.clone()),
+        tt::TokenTree::Subtree(subtree) => {
+            let tokens =
+                parse_inner(&subtree, mode).into_iter().collect::<Result<Vec<Op>, ParseError>>()?;
+            Op::Subtree { tokens: MetaTemplate(tokens), delimiter: subtree.delimiter }
+        }
     };
     Ok(res)
 }
 
-fn eat_fragment_kind<'a>(
-    src: &mut TtIter<'a>,
-    mode: Mode,
-) -> Result<Option<&'a SmolStr>, ExpandError> {
+fn eat_fragment_kind<'a>(src: &mut TtIter<'a>, mode: Mode) -> Result<Option<SmolStr>, ParseError> {
     if let Mode::Pattern = mode {
         src.expect_char(':').map_err(|()| err!("bad fragment specifier 1"))?;
         let ident = src.expect_ident().map_err(|()| err!("bad fragment specifier 1"))?;
-        return Ok(Some(&ident.text));
+        return Ok(Some(ident.text.clone()));
     };
     Ok(None)
 }
@@ -140,12 +156,12 @@ fn is_boolean_literal(lit: &tt::Literal) -> bool {
     matches!(lit.text.as_str(), "true" | "false")
 }
 
-fn parse_repeat(src: &mut TtIter) -> Result<(Option<Separator>, RepeatKind), ExpandError> {
+fn parse_repeat(src: &mut TtIter) -> Result<(Option<Separator>, RepeatKind), ParseError> {
     let mut separator = Separator::Puncts(SmallVec::new());
     for tt in src {
         let tt = match tt {
             tt::TokenTree::Leaf(leaf) => leaf,
-            tt::TokenTree::Subtree(_) => return Err(ExpandError::InvalidRepeat),
+            tt::TokenTree::Subtree(_) => return Err(ParseError::InvalidRepeat),
         };
         let has_sep = match &separator {
             Separator::Puncts(puncts) => !puncts.is_empty(),
@@ -153,7 +169,7 @@ fn parse_repeat(src: &mut TtIter) -> Result<(Option<Separator>, RepeatKind), Exp
         };
         match tt {
             tt::Leaf::Ident(_) | tt::Leaf::Literal(_) if has_sep => {
-                return Err(ExpandError::InvalidRepeat)
+                return Err(ParseError::InvalidRepeat)
             }
             tt::Leaf::Ident(ident) => separator = Separator::Ident(ident.clone()),
             tt::Leaf::Literal(lit) => separator = Separator::Literal(lit.clone()),
@@ -166,11 +182,11 @@ fn parse_repeat(src: &mut TtIter) -> Result<(Option<Separator>, RepeatKind), Exp
                         match &mut separator {
                             Separator::Puncts(puncts) => {
                                 if puncts.len() == 3 {
-                                    return Err(ExpandError::InvalidRepeat);
+                                    return Err(ParseError::InvalidRepeat);
                                 }
                                 puncts.push(punct.clone())
                             }
-                            _ => return Err(ExpandError::InvalidRepeat),
+                            _ => return Err(ParseError::InvalidRepeat),
                         }
                         continue;
                     }
@@ -180,5 +196,5 @@ fn parse_repeat(src: &mut TtIter) -> Result<(Option<Separator>, RepeatKind), Exp
             }
         }
     }
-    Err(ExpandError::InvalidRepeat)
+    Err(ParseError::InvalidRepeat)
 }