Skip to content

Commit 3631d6e

Browse files
committed
use more standard parsing terminology
not sure why I called it *op* when it's just a symbol
1 parent 4460fbc commit 3631d6e

File tree

3 files changed

+53
-50
lines changed

3 files changed

+53
-50
lines changed

crates/formality-macros/src/debug.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use syn::{spanned::Spanned, Attribute};
66

77
use crate::{
88
attrs,
9-
spec::{self, FieldMode, FormalitySpec, FormalitySpecOp},
9+
spec::{self, FieldMode, FormalitySpec, FormalitySpecSymbol},
1010
};
1111

1212
/// Derive the `Parse` impl, using an optional grammar supplied "from the outside".
@@ -122,23 +122,26 @@ fn debug_variant_with_attr(
122122

123123
stream.extend(quote!(let mut sep = "";));
124124

125-
let mut prev_op: Option<&FormalitySpecOp> = None;
126-
for op in &spec.ops {
125+
let mut prev_op: Option<&FormalitySpecSymbol> = None;
126+
for op in &spec.symbols {
127127
// insert whitespace if needed
128128
let suppress_space = match (prev_op, op) {
129129
// consecutive characters don't need spaces
130-
(Some(spec::FormalitySpecOp::Char { .. }), spec::FormalitySpecOp::Char { .. }) => true,
130+
(
131+
Some(spec::FormalitySpecSymbol::Char { .. }),
132+
spec::FormalitySpecSymbol::Char { .. },
133+
) => true,
131134

132135
// `foo(` looks better than `foo (`
133136
(
134-
Some(spec::FormalitySpecOp::Keyword { .. }),
135-
spec::FormalitySpecOp::Delimeter { .. },
137+
Some(spec::FormalitySpecSymbol::Keyword { .. }),
138+
spec::FormalitySpecSymbol::Delimeter { .. },
136139
) => true,
137140

138141
// consecutive delimeters don't need spaces
139142
(
140-
Some(spec::FormalitySpecOp::Delimeter { .. }),
141-
spec::FormalitySpecOp::Delimeter { .. },
143+
Some(spec::FormalitySpecSymbol::Delimeter { .. }),
144+
spec::FormalitySpecSymbol::Delimeter { .. },
142145
) => true,
143146

144147
_ => false,
@@ -150,7 +153,7 @@ fn debug_variant_with_attr(
150153
}
151154

152155
stream.extend(match op {
153-
spec::FormalitySpecOp::Field {
156+
spec::FormalitySpecSymbol::Field {
154157
name,
155158
mode: FieldMode::Single | FieldMode::Optional,
156159
} => {
@@ -162,7 +165,7 @@ fn debug_variant_with_attr(
162165
}
163166
}
164167

165-
spec::FormalitySpecOp::Field {
168+
spec::FormalitySpecSymbol::Field {
166169
name,
167170
mode: FieldMode::Many,
168171
} => {
@@ -176,7 +179,7 @@ fn debug_variant_with_attr(
176179
}
177180
}
178181

179-
spec::FormalitySpecOp::Field {
182+
spec::FormalitySpecSymbol::Field {
180183
name,
181184
mode: FieldMode::Comma,
182185
} => {
@@ -191,7 +194,7 @@ fn debug_variant_with_attr(
191194
}
192195
}
193196

194-
spec::FormalitySpecOp::Keyword { ident } => {
197+
spec::FormalitySpecSymbol::Keyword { ident } => {
195198
let literal = as_literal(ident);
196199
quote_spanned!(ident.span() =>
197200
write!(fmt, "{}", sep)?;
@@ -200,7 +203,7 @@ fn debug_variant_with_attr(
200203
)
201204
}
202205

203-
spec::FormalitySpecOp::Char { punct } => {
206+
spec::FormalitySpecSymbol::Char { punct } => {
204207
let literal = Literal::character(punct.as_char());
205208
quote_spanned!(punct.span() =>
206209
write!(fmt, "{}", sep)?;
@@ -209,7 +212,7 @@ fn debug_variant_with_attr(
209212
)
210213
}
211214

212-
spec::FormalitySpecOp::Delimeter { text } => match text {
215+
spec::FormalitySpecSymbol::Delimeter { text } => match text {
213216
'{' | '}' => {
214217
let literal = Literal::character(*text);
215218
quote!(

crates/formality-macros/src/parse.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use syn::{spanned::Spanned, Attribute};
66
use synstructure::BindingInfo;
77

88
use crate::{
9-
spec::{self, FieldMode, FormalitySpec, FormalitySpecOp},
9+
spec::{self, FieldMode, FormalitySpec, FormalitySpecSymbol},
1010
attrs::{has_variable_attr, has_cast_attr},
1111
};
1212

@@ -154,11 +154,11 @@ fn parse_variant_with_attr(
154154
) -> syn::Result<TokenStream> {
155155
let mut stream = TokenStream::new();
156156

157-
for i in 0..spec.ops.len() {
158-
let op = &spec.ops[i];
159-
let next_op = spec.ops.get(i + 1);
160-
stream.extend(match op {
161-
spec::FormalitySpecOp::Field {
157+
for i in 0..spec.symbols.len() {
158+
let symbol = &spec.symbols[i];
159+
let next_symbol = spec.symbols.get(i + 1);
160+
stream.extend(match symbol {
161+
spec::FormalitySpecSymbol::Field {
162162
name,
163163
mode: FieldMode::Single,
164164
} => {
@@ -167,7 +167,7 @@ fn parse_variant_with_attr(
167167
}
168168
}
169169

170-
spec::FormalitySpecOp::Field {
170+
spec::FormalitySpecSymbol::Field {
171171
name,
172172
mode: FieldMode::Optional,
173173
} => {
@@ -178,11 +178,11 @@ fn parse_variant_with_attr(
178178
}
179179
}
180180

181-
spec::FormalitySpecOp::Field {
181+
spec::FormalitySpecSymbol::Field {
182182
name,
183183
mode: FieldMode::Many,
184184
} => {
185-
match lookahead(next_op) {
185+
match lookahead(next_symbol) {
186186
Some(lookahead) => quote_spanned! {
187187
name.span() => let (#name, text) = parse::CoreParse::parse_many(scope, text, #lookahead)?;
188188
},
@@ -192,11 +192,11 @@ fn parse_variant_with_attr(
192192
}
193193
}
194194

195-
spec::FormalitySpecOp::Field {
195+
spec::FormalitySpecSymbol::Field {
196196
name,
197197
mode: FieldMode::Comma,
198198
} => {
199-
match lookahead(next_op) {
199+
match lookahead(next_symbol) {
200200
Some(lookahead) => quote_spanned! {
201201
name.span() => let (#name, text) = parse::CoreParse::parse_comma(scope, text, #lookahead)?;
202202
},
@@ -210,17 +210,17 @@ fn parse_variant_with_attr(
210210

211211
}
212212

213-
spec::FormalitySpecOp::Keyword { ident } => {
213+
spec::FormalitySpecSymbol::Keyword { ident } => {
214214
let literal = as_literal(ident);
215215
quote_spanned!(ident.span() => let ((), text) = parse::expect_keyword(#literal, text)?;)
216216
}
217217

218-
spec::FormalitySpecOp::Char { punct } => {
218+
spec::FormalitySpecSymbol::Char { punct } => {
219219
let literal = Literal::character(punct.as_char());
220220
quote_spanned!(punct.span() => let ((), text) = parse::expect_char(#literal, text)?;)
221221
}
222222

223-
spec::FormalitySpecOp::Delimeter { text } => {
223+
spec::FormalitySpecSymbol::Delimeter { text } => {
224224
let literal = Literal::character(*text);
225225
quote!(let ((), text) = parse::expect_char(#literal, text)?;)
226226
}
@@ -236,11 +236,11 @@ fn parse_variant_with_attr(
236236
Ok(stream)
237237
}
238238

239-
fn lookahead(op: Option<&FormalitySpecOp>) -> Option<Literal> {
240-
match op {
241-
Some(FormalitySpecOp::Char { punct }) => Some(Literal::character(punct.as_char())),
242-
Some(FormalitySpecOp::Delimeter { text }) => Some(Literal::character(*text)),
243-
Some(FormalitySpecOp::Keyword { .. }) | Some(FormalitySpecOp::Field { .. }) | None => None,
239+
fn lookahead(next_symbol: Option<&FormalitySpecSymbol>) -> Option<Literal> {
240+
match next_symbol {
241+
Some(FormalitySpecSymbol::Char { punct }) => Some(Literal::character(punct.as_char())),
242+
Some(FormalitySpecSymbol::Delimeter { text }) => Some(Literal::character(*text)),
243+
Some(FormalitySpecSymbol::Keyword { .. }) | Some(FormalitySpecSymbol::Field { .. }) | None => None,
244244
}
245245
}
246246

crates/formality-macros/src/spec.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use proc_macro2::{Ident, Punct, TokenStream, TokenTree};
1010
/// * a character like `<` is parsed as is; a group like `[..]` parses a `[`, the contents, and then `]`
1111
#[derive(Debug)]
1212
pub struct FormalitySpec {
13-
pub ops: Vec<FormalitySpecOp>,
13+
pub symbols: Vec<FormalitySpecSymbol>,
1414
}
1515

1616
#[derive(Debug)]
17-
pub enum FormalitySpecOp {
17+
pub enum FormalitySpecSymbol {
1818
/// `$foo` or `$foo*` -- indicates we should parse the type of the given field.
1919
Field { name: Ident, mode: FieldMode },
2020

@@ -53,17 +53,17 @@ pub enum FieldMode {
5353
impl syn::parse::Parse for FormalitySpec {
5454
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
5555
let token_stream: TokenStream = input.parse()?;
56-
let mut ops = vec![];
57-
token_stream_to_ops(&mut ops, token_stream)?;
58-
Ok(FormalitySpec { ops })
56+
let mut symbols = vec![];
57+
token_stream_to_symbols(&mut symbols, token_stream)?;
58+
Ok(FormalitySpec { symbols })
5959
}
6060
}
6161

62-
fn token_stream_to_ops(
63-
ops: &mut Vec<FormalitySpecOp>,
62+
fn token_stream_to_symbols(
63+
symbols: &mut Vec<FormalitySpecSymbol>,
6464
token_stream: TokenStream,
6565
) -> syn::Result<()> {
66-
use FormalitySpecOp::*;
66+
use FormalitySpecSymbol::*;
6767

6868
let mut tokens = token_stream.into_iter();
6969

@@ -78,17 +78,17 @@ fn token_stream_to_ops(
7878
};
7979

8080
if let Some(ch) = open_text {
81-
ops.push(Delimeter { text: ch });
81+
symbols.push(Delimeter { text: ch });
8282
}
83-
token_stream_to_ops(ops, v.stream())?;
83+
token_stream_to_symbols(symbols, v.stream())?;
8484
if let Some(ch) = close_text {
85-
ops.push(Delimeter { text: ch });
85+
symbols.push(Delimeter { text: ch });
8686
}
8787
}
88-
proc_macro2::TokenTree::Ident(ident) => ops.push(Keyword { ident }),
88+
proc_macro2::TokenTree::Ident(ident) => symbols.push(Keyword { ident }),
8989
proc_macro2::TokenTree::Punct(punct) => match punct.as_char() {
90-
'$' => ops.push(parse_variable_binding(punct, &mut tokens)?),
91-
_ => ops.push(Char { punct }),
90+
'$' => symbols.push(parse_variable_binding(punct, &mut tokens)?),
91+
_ => symbols.push(Char { punct }),
9292
},
9393
proc_macro2::TokenTree::Literal(_) => {
9494
let message = "unexpected literal in parse string";
@@ -109,7 +109,7 @@ fn token_stream_to_ops(
109109
fn parse_variable_binding(
110110
dollar_token: Punct,
111111
tokens: &mut impl Iterator<Item = TokenTree>,
112-
) -> syn::Result<FormalitySpecOp> {
112+
) -> syn::Result<FormalitySpecSymbol> {
113113
let error = || {
114114
let message = "expected field name or field mode (`,`, `*`)";
115115
Err(syn::Error::new(dollar_token.span(), message))
@@ -128,7 +128,7 @@ fn parse_variable_binding(
128128
',' => FieldMode::Comma,
129129
'*' => FieldMode::Many,
130130
'?' => FieldMode::Optional,
131-
'$' => return Ok(FormalitySpecOp::Char { punct }),
131+
'$' => return Ok(FormalitySpecSymbol::Char { punct }),
132132
_ => return error(),
133133
};
134134

@@ -148,5 +148,5 @@ fn parse_variable_binding(
148148
_ => return error(),
149149
};
150150

151-
Ok(FormalitySpecOp::Field { name, mode })
151+
Ok(FormalitySpecSymbol::Field { name, mode })
152152
}

0 commit comments

Comments
 (0)