aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/mbe_parser.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe/src/mbe_parser.rs')
-rw-r--r--crates/ra_mbe/src/mbe_parser.rs83
1 files changed, 68 insertions, 15 deletions
diff --git a/crates/ra_mbe/src/mbe_parser.rs b/crates/ra_mbe/src/mbe_parser.rs
index 58e2533f1..e3669f66c 100644
--- a/crates/ra_mbe/src/mbe_parser.rs
+++ b/crates/ra_mbe/src/mbe_parser.rs
@@ -1,33 +1,34 @@
1/// This module parses a raw `tt::TokenStream` into macro-by-example token 1/// This module parses a raw `tt::TokenStream` into macro-by-example token
2/// stream. This is a *mostly* identify function, expect for handling of 2/// stream. This is a *mostly* identify function, expect for handling of
3/// `$var:tt_kind` and `$(repeat),*` constructs. 3/// `$var:tt_kind` and `$(repeat),*` constructs.
4use crate::ParseError;
4use crate::tt_cursor::TtCursor; 5use crate::tt_cursor::TtCursor;
5 6
6pub(crate) fn parse(tt: &tt::Subtree) -> Option<crate::MacroRules> { 7pub(crate) fn parse(tt: &tt::Subtree) -> Result<crate::MacroRules, ParseError> {
7 let mut parser = TtCursor::new(tt); 8 let mut parser = TtCursor::new(tt);
8 let mut rules = Vec::new(); 9 let mut rules = Vec::new();
9 while !parser.is_eof() { 10 while !parser.is_eof() {
10 rules.push(parse_rule(&mut parser)?); 11 rules.push(parse_rule(&mut parser)?);
11 if parser.expect_char(';') == None { 12 if let Err(e) = parser.expect_char(';') {
12 if !parser.is_eof() { 13 if !parser.is_eof() {
13 return None; 14 return Err(e);
14 } 15 }
15 break; 16 break;
16 } 17 }
17 } 18 }
18 Some(crate::MacroRules { rules }) 19 Ok(crate::MacroRules { rules })
19} 20}
20 21
21fn parse_rule(p: &mut TtCursor) -> Option<crate::Rule> { 22fn parse_rule(p: &mut TtCursor) -> Result<crate::Rule, ParseError> {
22 let lhs = parse_subtree(p.eat_subtree()?)?; 23 let lhs = parse_subtree(p.eat_subtree()?)?;
23 p.expect_char('=')?; 24 p.expect_char('=')?;
24 p.expect_char('>')?; 25 p.expect_char('>')?;
25 let mut rhs = parse_subtree(p.eat_subtree()?)?; 26 let mut rhs = parse_subtree(p.eat_subtree()?)?;
26 rhs.delimiter = crate::Delimiter::None; 27 rhs.delimiter = crate::Delimiter::None;
27 Some(crate::Rule { lhs, rhs }) 28 Ok(crate::Rule { lhs, rhs })
28} 29}
29 30
30fn parse_subtree(tt: &tt::Subtree) -> Option<crate::Subtree> { 31fn parse_subtree(tt: &tt::Subtree) -> Result<crate::Subtree, ParseError> {
31 let mut token_trees = Vec::new(); 32 let mut token_trees = Vec::new();
32 let mut p = TtCursor::new(tt); 33 let mut p = TtCursor::new(tt);
33 while let Some(tt) = p.eat() { 34 while let Some(tt) = p.eat() {
@@ -52,10 +53,10 @@ fn parse_subtree(tt: &tt::Subtree) -> Option<crate::Subtree> {
52 }; 53 };
53 token_trees.push(child); 54 token_trees.push(child);
54 } 55 }
55 Some(crate::Subtree { token_trees, delimiter: tt.delimiter }) 56 Ok(crate::Subtree { token_trees, delimiter: tt.delimiter })
56} 57}
57 58
58fn parse_var(p: &mut TtCursor) -> Option<crate::Var> { 59fn parse_var(p: &mut TtCursor) -> Result<crate::Var, ParseError> {
59 let ident = p.eat_ident().unwrap(); 60 let ident = p.eat_ident().unwrap();
60 let text = ident.text.clone(); 61 let text = ident.text.clone();
61 let kind = if p.at_char(':') { 62 let kind = if p.at_char(':') {
@@ -69,25 +70,77 @@ fn parse_var(p: &mut TtCursor) -> Option<crate::Var> {
69 } else { 70 } else {
70 None 71 None
71 }; 72 };
72 Some(crate::Var { text, kind }) 73 Ok(crate::Var { text, kind })
73} 74}
74 75
75fn parse_repeat(p: &mut TtCursor) -> Option<crate::Repeat> { 76fn parse_repeat(p: &mut TtCursor) -> Result<crate::Repeat, ParseError> {
76 let subtree = p.eat_subtree().unwrap(); 77 let subtree = p.eat_subtree().unwrap();
77 let mut subtree = parse_subtree(subtree)?; 78 let mut subtree = parse_subtree(subtree)?;
78 subtree.delimiter = crate::Delimiter::None; 79 subtree.delimiter = crate::Delimiter::None;
79 let sep = p.eat_punct()?; 80 let sep = p.eat_punct().ok_or(ParseError::Expected(String::from("separator")))?;
80 let (separator, rep) = match sep.char { 81 let (separator, rep) = match sep.char {
81 '*' | '+' | '?' => (None, sep.char), 82 '*' | '+' | '?' => (None, sep.char),
82 char => (Some(char), p.eat_punct()?.char), 83 char => {
84 (Some(char), p.eat_punct().ok_or(ParseError::Expected(String::from("separator")))?.char)
85 }
83 }; 86 };
84 87
85 let kind = match rep { 88 let kind = match rep {
86 '*' => crate::RepeatKind::ZeroOrMore, 89 '*' => crate::RepeatKind::ZeroOrMore,
87 '+' => crate::RepeatKind::OneOrMore, 90 '+' => crate::RepeatKind::OneOrMore,
88 '?' => crate::RepeatKind::ZeroOrOne, 91 '?' => crate::RepeatKind::ZeroOrOne,
89 _ => return None, 92 _ => return Err(ParseError::Expected(String::from("repeat"))),
90 }; 93 };
91 p.bump(); 94 p.bump();
92 Some(crate::Repeat { subtree, kind, separator }) 95 Ok(crate::Repeat { subtree, kind, separator })
96}
97
98#[cfg(test)]
99mod tests {
100 use ra_syntax::{ast, AstNode};
101
102 use super::*;
103 use crate::ast_to_token_tree;
104
105 #[test]
106 fn test_invalid_parse() {
107 expect_err("invalid", "subtree");
108
109 is_valid("($i:ident) => ()");
110 expect_err("$i:ident => ()", "subtree");
111 expect_err("($i:ident) ()", "`=`");
112 expect_err("($($i:ident)_) => ()", "separator");
113 }
114
115 fn expect_err(macro_body: &str, expected: &str) {
116 assert_eq!(
117 create_rules(&format_macro(macro_body)),
118 Err(ParseError::Expected(String::from(expected)))
119 );
120 }
121
122 fn is_valid(macro_body: &str) {
123 assert!(create_rules(&format_macro(macro_body)).is_ok());
124 }
125
126 fn format_macro(macro_body: &str) -> String {
127 format!(
128 "
129 macro_rules! foo {{
130 {}
131 }}
132",
133 macro_body
134 )
135 }
136
137 fn create_rules(macro_definition: &str) -> Result<crate::MacroRules, ParseError> {
138 let source_file = ast::SourceFile::parse(macro_definition);
139 let macro_definition =
140 source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
141
142 let (definition_tt, _) = ast_to_token_tree(macro_definition.token_tree().unwrap()).unwrap();
143 parse(&definition_tt)
144 }
145
93} 146}