Repository navigation
Expand file tree
/
Copy pathast.rs
More file actions
266 lines (240 loc) · 7.94 KB
/
Copy pathast.rs
File metadata and controls
266 lines (240 loc) · 7.94 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use std::collections::BTreeSet;
use proc_macro2::{Ident, Literal};
/// A complete rewrite rule such as `(input) where guard => (output)`.
pub(crate) struct Rule {
/// Input pattern before `where` or `=>`.
pub(crate) pattern: Pattern,
/// Optional Rust guard expression after `where`.
pub(crate) guard: Option<syn::Expr>,
/// Template or annotated Rust replacement after `=>`.
pub(crate) replacement: Replacement,
}
pub(crate) enum Replacement {
/// One or more output templates such as `(call ...) {extra_nodes}`.
Templates(Vec<Template>),
/// Annotated Rust body such as `expr? { ... }`.
Rust {
/// Retained in the rule AST for validation and analysis; runtime
/// lowering only needs the Rust body.
#[allow(dead_code)]
annotation: ReturnAnnotation,
body: syn::Block,
},
}
#[allow(dead_code)]
pub(crate) struct ReturnAnnotation {
/// Declared output kind before the Rust body.
pub(crate) kind: Ident,
/// Declared output cardinality (`kind`, `kind?`, or `kind*`).
pub(crate) cardinality: Cardinality,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Cardinality {
pub(crate) multiple: bool,
pub(crate) required: bool,
}
impl Cardinality {
pub(crate) const SINGLE: Self = Self {
multiple: false,
required: true,
};
pub(crate) const OPTIONAL: Self = Self {
multiple: false,
required: false,
};
pub(crate) const ZERO_OR_MORE: Self = Self {
multiple: true,
required: false,
};
pub(crate) const ONE_OR_MORE: Self = Self {
multiple: true,
required: true,
};
}
/// An input pattern such as `(call method: (identifier) @name)`.
pub(crate) enum Pattern {
/// `_` or `(_)`, distinguished by whether unnamed nodes may match.
Any { match_unnamed: bool },
/// A named node and the pattern matched against each field.
Node {
kind: String,
fields: Vec<(String, Pattern)>,
},
/// An unnamed token pattern such as `"+"`.
Unnamed(String),
/// A captured pattern such as `(identifier) @name` or `_ @@raw`.
Capture {
/// Capture identifier after `@` or `@@`.
name: Ident,
/// Whether `@@` keeps the captured input node untranslated.
raw: bool,
pattern: Box<Pattern>,
cardinality: Cardinality,
},
/// An ordered sequence of patterns within a field.
Sequence(Vec<Pattern>),
/// A repeated or optional pattern.
Repeated {
pattern: Box<Pattern>,
cardinality: Cardinality,
},
}
impl Pattern {
pub(crate) fn matches_single_node(&self) -> bool {
match self {
Pattern::Any { .. } | Pattern::Node { .. } | Pattern::Unnamed(_) => true,
Pattern::Capture { cardinality, .. } => *cardinality == Cardinality::SINGLE,
Pattern::Sequence(_) | Pattern::Repeated { .. } => false,
}
}
}
pub(crate) enum Template {
/// A parenthesized node template such as `(call method: {name})`.
Node(Node),
/// An embedded Rust block such as `{args}`.
Splice(syn::Block),
/// Ordered alternatives such as `[(identifier #{name}) (missing)]`.
Choice(Vec<Alternative>),
}
pub(crate) struct Alternative {
/// Template to construct when the guard and dependencies succeed.
pub(crate) template: Template,
/// Optional postfix guard such as `if { name.is_some() }`.
pub(crate) guard: Option<syn::Block>,
}
pub(crate) struct Node {
/// The node kind at the start of the template, such as `call` in `(call)`.
pub(crate) kind: Ident,
/// Optional leaf content such as `"foo"` or `#{name}` in
/// `(identifier "foo")` or `(identifier #{name})`.
pub(crate) content: Option<Content>,
/// Named fields and source-location metadata.
pub(crate) fields: Vec<Field>,
}
pub(crate) enum Content {
/// Static leaf content such as `"foo"` in `(identifier "foo")`.
Static(Literal),
/// Computed leaf content such as `#{name}` in `(identifier #{name})`.
Computed(syn::Block),
/// Value inherited from the node matched by the enclosing rule.
Inherited,
}
pub(crate) enum Field {
/// A named field such as `argument: {args}` or
/// `label: (identifier #{name})`.
Named {
/// The field name before `:`, such as `argument`.
name: Ident,
/// The node or Rust splice after `:`, such as `(identifier "x")` or
/// `{args}`.
value: Template,
},
/// Source-location metadata such as `location: {source}`.
Location {
/// A Rust block yielding the node or nodes whose locations should be combined.
block: syn::Block,
},
/// Explicitly omit source-location metadata: `location: none`.
LocationNone,
}
impl Rule {
pub(crate) fn resolve_bare_leaf_content(
&mut self,
leaf_kinds: &BTreeSet<String>,
) -> syn::Result<()> {
let Replacement::Templates(templates) = &mut self.replacement else {
return Ok(());
};
for template in templates {
template.resolve_bare_leaf_content(leaf_kinds, true)?;
}
Ok(())
}
}
impl Template {
fn resolve_bare_leaf_content(
&mut self,
leaf_kinds: &BTreeSet<String>,
is_rule_result: bool,
) -> syn::Result<()> {
let Template::Node(node) = self else {
return Ok(());
};
for field in &mut node.fields {
if let Field::Named { value, .. } = field {
value.resolve_bare_leaf_content(leaf_kinds, false)?;
}
}
if node.content.is_none()
&& node.fields.is_empty()
&& leaf_kinds.contains(node.kind.to_string().as_str())
{
if !is_rule_result {
return Err(syn::Error::new_spanned(
&node.kind,
format!(
"bare leaf `{}` must be a top-level rule result or have explicit content",
node.kind
),
));
}
node.content = Some(Content::Inherited);
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use quote::quote;
fn output_leaf_kinds() -> BTreeSet<String> {
yeast_schema::node_types_yaml::leaf_kinds_from_yaml(
r"
named:
assignment:
left: identifier
identifier:
",
)
.unwrap()
}
#[test]
fn marks_top_level_bare_leaves_as_inherited() {
let mut rule = crate::rule_parse::parse_rule(quote!((source) => (identifier))).unwrap();
rule.resolve_bare_leaf_content(&output_leaf_kinds())
.unwrap();
let Replacement::Templates(templates) = rule.replacement else {
panic!("expected template replacement");
};
let [Template::Node(node)] = templates.as_slice() else {
panic!("expected one output node");
};
assert!(matches!(node.content, Some(Content::Inherited)));
}
#[test]
fn rejects_nested_bare_leaves() {
let mut rule =
crate::rule_parse::parse_rule(quote!((source) => (assignment left: (identifier))))
.unwrap();
let error = rule
.resolve_bare_leaf_content(&output_leaf_kinds())
.unwrap_err();
assert_eq!(
error.to_string(),
"bare leaf `identifier` must be a top-level rule result or have explicit content"
);
}
#[test]
fn leaves_fieldless_structural_nodes_unchanged() {
let mut rule = crate::rule_parse::parse_rule(quote!((source) => (assignment))).unwrap();
rule.resolve_bare_leaf_content(&output_leaf_kinds())
.unwrap();
let Replacement::Templates(templates) = rule.replacement else {
panic!("expected template replacement");
};
let [Template::Node(node)] = templates.as_slice() else {
panic!("expected one output node");
};
assert!(node.content.is_none());
}
}