11use crate :: fields:: Field ;
2- use crate :: nom_packages:: apply_nom_namespaces;
2+ use crate :: nom_packages:: { apply_nom_namespaces, generate_tag_expression } ;
33use itertools:: Itertools ;
44use proc_macro:: Span ;
55use proc_macro2:: TokenStream ;
6- use quote:: { quote , ToTokens } ;
6+ use quote:: { quote_spanned , ToTokens } ;
77use syn:: parse:: { Parse , ParseStream } ;
8+ use syn:: spanned:: Spanned ;
89use syn:: { parse, Error , Expr , LitStr , Token } ;
910
1011pub enum ParseSettings {
@@ -23,39 +24,52 @@ impl ParseSettings {
2324
2425 pub fn generate_parse_expressions ( & self , fields : & [ Field ] ) -> Vec < TokenStream > {
2526 match self {
26- ParseSettings :: Split { prefix, split, suffix} => {
27+ ParseSettings :: Split {
28+ prefix,
29+ split,
30+ suffix,
31+ } => {
2732 let mut split = split. clone ( ) ;
2833 apply_nom_namespaces ( & mut split) ;
2934
3035 let mut expressions: Vec < _ > = Itertools :: intersperse (
3136 fields
3237 . iter ( )
3338 . map ( |field| field. generate_expression ( ) . into_token_stream ( ) ) ,
34- quote ! { let ( input, _) = split. parse( input) ?; } ,
39+ quote_spanned ! { split . span ( ) => let ( input, _) = split. parse( input) ?; } ,
3540 )
3641 . collect ( ) ;
3742
3843 if let Some ( prefix) = prefix {
3944 let mut prefix = prefix. clone ( ) ;
4045 apply_nom_namespaces ( & mut prefix) ;
41- expressions. insert ( 0 , quote ! { let ( input, _) = #prefix. parse( input) ?; } ) ;
46+ expressions. insert (
47+ 0 ,
48+ quote_spanned ! { prefix. span( ) => let ( input, _) = #prefix. parse( input) ?; } ,
49+ ) ;
4250 }
4351
4452 if let Some ( suffix) = suffix {
4553 let mut suffix = suffix. clone ( ) ;
4654 apply_nom_namespaces ( & mut suffix) ;
47- expressions. push ( quote ! { let ( input, _) = #suffix. parse( input) ?; } ) ;
55+ expressions. push (
56+ quote_spanned ! { suffix. span( ) => let ( input, _) = #suffix. parse( input) ?; } ,
57+ ) ;
4858 }
4959
50- expressions. insert ( 0 , quote ! { let mut split = #split; } ) ;
60+ expressions. insert (
61+ 0 ,
62+ quote_spanned ! { split. span( ) => let mut split = #split; } ,
63+ ) ;
5164 expressions
5265 }
5366 ParseSettings :: Match ( literal) => {
5467 let value = literal. value ( ) ;
5568 let parts: Vec < _ > = value
5669 . split ( "{}" )
5770 . map ( |part| {
58- quote ! { let ( input, _) = nom:: bytes:: complete:: tag( #part) ( input) ?; }
71+ let expr = generate_tag_expression ( part. as_bytes ( ) , literal. span ( ) ) ;
72+ quote_spanned ! { literal. span( ) => let ( input, _) = #expr. parse( input) ?; }
5973 } )
6074 . collect ( ) ;
6175
@@ -132,7 +146,10 @@ impl Parse for ParseSettings {
132146 suffix,
133147 } )
134148 } else {
135- Err ( Error :: new ( Span :: call_site ( ) . into ( ) , "Missing `split` keyword" ) )
149+ Err ( Error :: new (
150+ Span :: call_site ( ) . into ( ) ,
151+ "Missing `split` keyword" ,
152+ ) )
136153 }
137154 }
138155}
0 commit comments