1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
use crate::tt_cursor::TtCursor;
/// This module parses a raw `tt::TokenStream` into macro-by-example token
/// stream. This is a *mostly* identify function, expect for handling of
/// `$var:tt_kind` and `$(repeat),*` constructs.
use crate::ParseError;
pub(crate) fn parse(tt: &tt::Subtree) -> Result<crate::MacroRules, ParseError> {
let mut parser = TtCursor::new(tt);
let mut rules = Vec::new();
while !parser.is_eof() {
rules.push(parse_rule(&mut parser)?);
if let Err(e) = parser.expect_char(';') {
if !parser.is_eof() {
return Err(e);
}
break;
}
}
Ok(crate::MacroRules { rules })
}
fn parse_rule(p: &mut TtCursor) -> Result<crate::Rule, ParseError> {
let lhs = parse_subtree(p.eat_subtree()?, false)?;
p.expect_char('=')?;
p.expect_char('>')?;
let mut rhs = parse_subtree(p.eat_subtree()?, true)?;
rhs.delimiter = crate::Delimiter::None;
Ok(crate::Rule { lhs, rhs })
}
fn is_boolean_literal(lit: Option<&tt::TokenTree>) -> bool {
if let Some(tt::TokenTree::Leaf(tt::Leaf::Literal(lit))) = lit {
if lit.text == "true" || lit.text == "false" {
return true;
}
}
false
}
fn parse_subtree(tt: &tt::Subtree, transcriber: bool) -> Result<crate::Subtree, ParseError> {
let mut token_trees = Vec::new();
let mut p = TtCursor::new(tt);
while let Some(tt) = p.eat() {
let child: crate::TokenTree = match tt {
tt::TokenTree::Leaf(leaf) => match leaf {
tt::Leaf::Punct(tt::Punct { char: '$', spacing }) => {
// mbe var can be an ident or keyword, including `true` and `false`
if p.at_ident().is_some() || is_boolean_literal(p.current()) {
crate::Leaf::from(parse_var(&mut p, transcriber)?).into()
} else if let Some(tt::TokenTree::Subtree(_)) = p.current() {
parse_repeat(&mut p, transcriber)?.into()
} else {
// Treat it as normal punct
crate::Leaf::from(tt::Punct { char: '$', spacing: *spacing }).into()
}
}
tt::Leaf::Punct(punct) => crate::Leaf::from(*punct).into(),
tt::Leaf::Ident(tt::Ident { text, .. }) => {
crate::Leaf::from(crate::Ident { text: text.clone() }).into()
}
tt::Leaf::Literal(tt::Literal { text }) => {
crate::Leaf::from(crate::Literal { text: text.clone() }).into()
}
},
tt::TokenTree::Subtree(subtree) => parse_subtree(&subtree, transcriber)?.into(),
};
token_trees.push(child);
}
Ok(crate::Subtree { token_trees, delimiter: tt.delimiter })
}
fn parse_var(p: &mut TtCursor, transcriber: bool) -> Result<crate::Var, ParseError> {
let text = {
if is_boolean_literal(p.current()) {
let lit = p.eat_literal().unwrap();
lit.text.clone()
} else {
let ident = p.eat_ident().unwrap();
ident.text.clone()
}
};
let kind = if !transcriber && p.at_char(':') {
p.bump();
if let Some(ident) = p.eat_ident() {
Some(ident.text.clone())
} else {
p.rev_bump();
None
}
} else {
None
};
Ok(crate::Var { text, kind })
}
fn mk_repeat(
rep: char,
subtree: crate::Subtree,
separator: Option<crate::Separator>,
) -> Result<crate::Repeat, ParseError> {
let kind = match rep {
'*' => crate::RepeatKind::ZeroOrMore,
'+' => crate::RepeatKind::OneOrMore,
'?' => crate::RepeatKind::ZeroOrOne,
_ => return Err(ParseError::Expected(String::from("repeat"))),
};
Ok(crate::Repeat { subtree, kind, separator })
}
fn parse_repeat(p: &mut TtCursor, transcriber: bool) -> Result<crate::Repeat, ParseError> {
let subtree = p.eat_subtree()?;
let mut subtree = parse_subtree(subtree, transcriber)?;
subtree.delimiter = crate::Delimiter::None;
if let Some(rep) = p.at_punct() {
match rep.char {
'*' | '+' | '?' => {
p.bump();
return mk_repeat(rep.char, subtree, None);
}
_ => {}
}
}
let sep = p.eat_seperator().ok_or_else(|| ParseError::Expected(String::from("separator")))?;
let rep = p.eat_punct().ok_or_else(|| ParseError::Expected(String::from("repeat")))?;
mk_repeat(rep.char, subtree, Some(sep))
}
#[cfg(test)]
mod tests {
use ra_syntax::{ast, AstNode};
use super::*;
use crate::ast_to_token_tree;
#[test]
fn test_invalid_parse() {
expect_err("invalid", "subtree");
is_valid("($i:ident) => ()");
is_valid("($($i:ident)*) => ($_)");
is_valid("($($true:ident)*) => ($true)");
is_valid("($($false:ident)*) => ($false)");
expect_err("$i:ident => ()", "subtree");
expect_err("($i:ident) ()", "`=`");
expect_err("($($i:ident)_) => ()", "repeat");
}
fn expect_err(macro_body: &str, expected: &str) {
assert_eq!(
create_rules(&format_macro(macro_body)),
Err(ParseError::Expected(String::from(expected)))
);
}
fn is_valid(macro_body: &str) {
assert!(create_rules(&format_macro(macro_body)).is_ok());
}
fn format_macro(macro_body: &str) -> String {
format!(
"
macro_rules! foo {{
{}
}}
",
macro_body
)
}
fn create_rules(macro_definition: &str) -> Result<crate::MacroRules, ParseError> {
let source_file = ast::SourceFile::parse(macro_definition).ok().unwrap();
let macro_definition =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
let (definition_tt, _) =
ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap();
parse(&definition_tt)
}
}
|