|
| 1 | +use proc_macro2::{Span, TokenStream}; |
| 2 | +use quote::{format_ident, quote}; |
| 3 | +use syn::{punctuated::Punctuated, visit_mut::VisitMut}; |
| 4 | + |
| 5 | +use crate::lexer::middle::{Action, LexerImpl, Middle}; |
| 6 | + |
| 7 | +pub struct Context { |
| 8 | + crate_name: TokenStream, |
| 9 | + lexbuf: syn::Ident, |
| 10 | + debug: bool, |
| 11 | +} |
| 12 | + |
| 13 | +impl Middle { |
| 14 | + pub fn expand(self) -> Result<TokenStream, TokenStream> { |
| 15 | + let mut result = TokenStream::new(); |
| 16 | + let ctx = Context { |
| 17 | + crate_name: self.crate_name, |
| 18 | + lexbuf: format_ident!("r#__lexbuf", span = Span::call_site()), |
| 19 | + debug: self.debug, |
| 20 | + }; |
| 21 | + |
| 22 | + for lexer in self.lexers { |
| 23 | + result.extend(lexer.expand(&ctx)?); |
| 24 | + } |
| 25 | + |
| 26 | + let mod_name = self.mod_name; |
| 27 | + let attrs = self.attrs; |
| 28 | + let items = self.items; |
| 29 | + Ok(quote! { |
| 30 | + #[allow(non_snake_case)] |
| 31 | + #(#attrs)* |
| 32 | + mod #mod_name { |
| 33 | + #(#items)* |
| 34 | + #result |
| 35 | + } |
| 36 | + }) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl LexerImpl { |
| 41 | + pub fn expand(self, ctx: &Context) -> Result<TokenStream, TokenStream> { |
| 42 | + let name = self.name; |
| 43 | + let vis = self.vis; |
| 44 | + let inputs = self.inputs; |
| 45 | + let ret_ty = if let Some(ref ret_ty) = self.ret_ty { |
| 46 | + quote! { #ret_ty } |
| 47 | + } else { |
| 48 | + quote! { () } |
| 49 | + }; |
| 50 | + |
| 51 | + let mut regexes = vec![]; |
| 52 | + let mut actions = vec![]; |
| 53 | + for (i, rule) in self.rules.into_iter().enumerate() { |
| 54 | + regexes.push(rule.pattern); |
| 55 | + let (action, _) = rule.actions.1.into_iter().try_fold( |
| 56 | + rule.actions.0.expand(ctx)?, |
| 57 | + |(inner, inner_ty), it| -> Result<_, TokenStream> { |
| 58 | + let (action, ret_ty) = it.expand(ctx)?; |
| 59 | + Ok(( |
| 60 | + quote! {{ |
| 61 | + let __self: #inner_ty = #inner; |
| 62 | + #action |
| 63 | + }}, |
| 64 | + ret_ty, |
| 65 | + )) |
| 66 | + }, |
| 67 | + )?; |
| 68 | + actions.push(quote! { |
| 69 | + #i => #action |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + let crate_name = &ctx.crate_name; |
| 74 | + let lexbuf = &ctx.lexbuf; |
| 75 | + Ok(quote! { |
| 76 | + #vis struct #name; |
| 77 | + |
| 78 | + impl #name { |
| 79 | + thread_local! { |
| 80 | + static REGEX: #crate_name::lexer::Regex = #crate_name::lexer::Regex::new_many( |
| 81 | + &[#(#regexes),*] |
| 82 | + ).unwrap(); |
| 83 | + } |
| 84 | + |
| 85 | + #[allow( |
| 86 | + dead_code, |
| 87 | + unreachable_code, |
| 88 | + clippy::never_loop, |
| 89 | + clippy::let_unit_value, |
| 90 | + clippy::unit_arg, |
| 91 | + clippy::useless_conversion |
| 92 | + )] |
| 93 | + pub fn run( |
| 94 | + #lexbuf: &mut #crate_name::lexer::LexerState, |
| 95 | + #(#inputs),* |
| 96 | + ) -> Result<Option<#ret_ty>, ()> { |
| 97 | + Self::REGEX.with(|regex| { |
| 98 | + 'lex: loop { |
| 99 | + if let Some(pat) = #lexbuf.run(regex) { |
| 100 | + let __self = #lexbuf.lexeme(); |
| 101 | + let value = match pat.as_u32() as usize { |
| 102 | + #(#actions,)* |
| 103 | + _ => unreachable!(), |
| 104 | + }; |
| 105 | + return Ok(Some(value)); |
| 106 | + } else { |
| 107 | + return Err(()); |
| 108 | + } |
| 109 | + } |
| 110 | + Ok(None) |
| 111 | + }) |
| 112 | + } |
| 113 | + } |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +struct ExpandLexMacroVisitor { |
| 119 | + crate_name: TokenStream, |
| 120 | + lexbuf: syn::Ident, |
| 121 | + failure: Vec<TokenStream>, |
| 122 | +} |
| 123 | + |
| 124 | +impl ExpandLexMacroVisitor { |
| 125 | + pub fn new(crate_name: TokenStream, lexbuf: syn::Ident) -> Self { |
| 126 | + Self { |
| 127 | + crate_name, |
| 128 | + lexbuf, |
| 129 | + failure: vec![], |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + pub fn failure(self) -> Option<TokenStream> { |
| 134 | + self.failure.into_iter().reduce(|mut a, b| { |
| 135 | + a.extend(b); |
| 136 | + a |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +impl VisitMut for ExpandLexMacroVisitor { |
| 142 | + fn visit_macro_mut(&mut self, i: &mut syn::Macro) { |
| 143 | + if i.path.is_ident("lex") { |
| 144 | + struct LexMacro { |
| 145 | + pub lexer: syn::Ident, |
| 146 | + pub args: Vec<syn::Expr>, |
| 147 | + } |
| 148 | + impl syn::parse::Parse for LexMacro { |
| 149 | + fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { |
| 150 | + let lexer = input.parse()?; |
| 151 | + let args = if input.peek(syn::token::Paren) { |
| 152 | + let content; |
| 153 | + syn::parenthesized!(content in input); |
| 154 | + |
| 155 | + let args = |
| 156 | + Punctuated::<syn::Expr, syn::Token![,]>::parse_terminated(&content)?; |
| 157 | + args.into_iter().collect() |
| 158 | + } else { |
| 159 | + vec![] |
| 160 | + }; |
| 161 | + Ok(Self { lexer, args }) |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + let crate_name = &self.crate_name; |
| 166 | + let lexbuf = &self.lexbuf; |
| 167 | + match syn::parse2::<LexMacro>(i.tokens.clone()) { |
| 168 | + Ok(lex_macro) => { |
| 169 | + let LexMacro { lexer, args } = lex_macro; |
| 170 | + i.path = syn::parse_quote!(#crate_name::identity); |
| 171 | + i.tokens = quote! { #lexer::run(#lexbuf, #(#args),*)? }; |
| 172 | + } |
| 173 | + Err(e) => self.failure.push(e.to_compile_error()), |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +impl Action { |
| 180 | + pub fn expand(&self, ctx: &Context) -> Result<(TokenStream, TokenStream), TokenStream> { |
| 181 | + let mut action = self.action.clone(); |
| 182 | + |
| 183 | + let mut visitor = ExpandLexMacroVisitor::new(ctx.crate_name.clone(), ctx.lexbuf.clone()); |
| 184 | + visitor.visit_expr_mut(&mut action); |
| 185 | + if let Some(failure) = visitor.failure() { |
| 186 | + return Err(failure); |
| 187 | + } |
| 188 | + |
| 189 | + let ret_ty = self.ret_ty(); |
| 190 | + Ok(( |
| 191 | + quote! { |
| 192 | + #action |
| 193 | + }, |
| 194 | + ret_ty, |
| 195 | + )) |
| 196 | + } |
| 197 | + |
| 198 | + pub fn ret_ty(&self) -> TokenStream { |
| 199 | + if let Some(ref ret_ty) = self.ret_ty { |
| 200 | + quote! { #ret_ty } |
| 201 | + } else { |
| 202 | + quote! { () } |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments