Debian Codegraph @

src/generator/leaf.rs

raw symbols SCIP
1 use proc_macro2::TokenStream;
2 use quote::quote;
3
4 use crate::generator::{Context, Generator};
5 use crate::leaf::{Callback, Leaf};
6 use crate::parser::SkipCallback;
7 use crate::util::MaybeVoid;
8
9 impl Generator<'_> {
10 pub fn generate_leaf(&mut self, leaf: &Leaf, mut ctx: Context) -> TokenStream {
11 let bump = ctx.bump();
12
13 let ident = &leaf.ident;
14 let name = self.name;
15 let this = self.this;
16 let ty = &leaf.field;
17
18 let constructor = match leaf.field {
19 MaybeVoid::Some(_) => quote!(#name::#ident),
20 MaybeVoid::Void => quote!(|()| #name::#ident),
21 };
22
23 match &leaf.callback {
24 Some(Callback::Label(callback)) => quote! {
25 #bump
26 #callback(lex).construct(#constructor, lex);
27 },
28 Some(Callback::Inline(inline)) => {
29 let arg = &inline.arg;
30 let body = &inline.body;
31
32 #[cfg(not(rust_1_82))]
33 let ret = quote!(impl CallbackResult<'s, #ty, #this>);
34
35 #[cfg(rust_1_82)]
36 let ret = quote!(impl CallbackResult<'s, #ty, #this> + use<'s>);
37
38 quote! {
39 #bump
40
41 #[inline]
42 fn callback<'s>(#arg: &mut Lexer<'s>) -> #ret {
43 #body
44 }
45
46 callback(lex).construct(#constructor, lex);
47 }
48 }
49 Some(Callback::SkipCallback(SkipCallback::Label(label))) => {
50 quote! {
51 #bump
52
53 #label(lex).construct_skip(lex);
54 }
55 }
56 Some(Callback::SkipCallback(SkipCallback::Inline(inline))) => {
57 let arg = &inline.arg;
58 let body = &inline.body;
59
60 #[cfg(not(rust_1_82))]
61 let ret = quote!(impl SkipCallbackResult<'s, #this>);
62
63 #[cfg(rust_1_82)]
64 let ret = quote!(impl SkipCallbackResult<'s, #this> + use<'s>);
65
66 quote! {
67 #bump
68
69 fn callback<'s>(#arg: &mut Lexer) -> #ret {
70 #body
71 }
72
73 callback(lex).construct_skip(lex);
74 }
75 }
76 Some(Callback::Skip(_)) => {
77 quote! {
78 #bump
79
80 lex.trivia();
81 #name::lex(lex);
82 }
83 }
84 None if matches!(leaf.field, MaybeVoid::Void) => quote! {
85 #bump
86 lex.set(Ok(#name::#ident));
87 },
88 None => quote! {
89 #bump
90 let token = #name::#ident(lex.slice());
91 lex.set(Ok(token));
92 },
93 }
94 }
95 }