@@ -242,13 +242,17 @@ where
242242 show_warnings : bool ,
243243 visibility : Visibility ,
244244 rust_edition : RustEdition ,
245+ lexer_path : Option < PathBuf > ,
245246 // We want lifetimes that imply the callback can't capture the header or the grammar.
246- header_callback : Option <
247+ inspect_callback : Option <
247248 Box <
248- dyn for <' h , ' y > Fn (
249- & ' h mut Header ,
249+ dyn for < ' y > Fn (
250+ & ' y mut Header ,
250251 RecoveryKind ,
251252 & ' y YaccGrammar < LexerTypesT :: StorageT > ,
253+ & ' y StateTable < LexerTypesT :: StorageT > ,
254+ & ' y StateGraph < LexerTypesT :: StorageT > ,
255+ Option < PathBuf > ,
252256 ) -> Result < ( ) , Box < dyn Error > > ,
253257 > ,
254258 > ,
@@ -296,7 +300,8 @@ where
296300 show_warnings : true ,
297301 visibility : Visibility :: Private ,
298302 rust_edition : RustEdition :: Rust2021 ,
299- header_callback : None ,
303+ inspect_callback : None ,
304+ lexer_path : None ,
300305 phantom : PhantomData ,
301306 }
302307 }
@@ -427,17 +432,27 @@ where
427432 self
428433 }
429434
430- pub fn process_header (
435+ /// Sets the path to the lexer sources this is for usage from within callbacks only, and
436+ /// not used during the build process.
437+ pub fn lexer_path ( mut self , lexer_path : PathBuf ) -> Self {
438+ self . lexer_path = Some ( lexer_path) ;
439+ self
440+ }
441+
442+ pub fn inspect (
431443 mut self ,
432444 cb : Box <
433445 dyn for <' h , ' y > Fn (
434446 & ' h mut Header ,
435447 RecoveryKind ,
436448 & ' y YaccGrammar < StorageT > ,
449+ & ' y StateTable < StorageT > ,
450+ & ' y StateGraph < StorageT > ,
451+ Option < PathBuf > ,
437452 ) -> Result < ( ) , Box < dyn Error > > ,
438453 > ,
439454 ) -> Self {
440- self . header_callback = Some ( cb) ;
455+ self . inspect_callback = Some ( cb) ;
441456 self
442457 }
443458
@@ -637,27 +652,6 @@ where
637652 }
638653 } ;
639654
640- if let Some ( cb) = & self . header_callback {
641- cb (
642- & mut header,
643- self . recoverer . expect ( "has a default value" ) ,
644- & grm,
645- ) ?;
646- }
647-
648- let unused_keys = header. unused ( ) ;
649- if !unused_keys. is_empty ( ) {
650- return Err ( format ! ( "Unused keys in header: {}" , unused_keys. join( ", " ) ) . into ( ) ) ;
651- }
652- let missing_keys = header. missing ( ) ;
653- if !missing_keys. is_empty ( ) {
654- return Err ( format ! (
655- "Required values were missing from the header: {}" ,
656- unused_keys. join( ", " )
657- )
658- . into ( ) ) ;
659- }
660-
661655 let rule_ids = grm
662656 . tokens_map ( )
663657 . iter ( )
@@ -727,6 +721,31 @@ where
727721 fs:: remove_file ( outp) . ok ( ) ;
728722
729723 let ( sgraph, stable) = from_yacc ( & grm, Minimiser :: Pager ) ?;
724+
725+ if let Some ( cb) = & self . inspect_callback {
726+ cb (
727+ & mut header,
728+ self . recoverer . expect ( "has a default value" ) ,
729+ & grm,
730+ & stable,
731+ & sgraph,
732+ self . lexer_path . clone ( ) ,
733+ ) ?;
734+ }
735+
736+ let unused_keys = header. unused ( ) ;
737+ if !unused_keys. is_empty ( ) {
738+ return Err ( format ! ( "Unused keys in header: {}" , unused_keys. join( ", " ) ) . into ( ) ) ;
739+ }
740+ let missing_keys = header. missing ( ) ;
741+ if !missing_keys. is_empty ( ) {
742+ return Err ( format ! (
743+ "Required values were missing from the header: {}" ,
744+ unused_keys. join( ", " )
745+ )
746+ . into ( ) ) ;
747+ }
748+
730749 if self . error_on_conflicts {
731750 if let Some ( c) = stable. conflicts ( ) {
732751 match ( grm. expect ( ) , grm. expectrr ( ) ) {
@@ -853,7 +872,8 @@ where
853872 show_warnings : self . show_warnings ,
854873 visibility : self . visibility . clone ( ) ,
855874 rust_edition : self . rust_edition ,
856- header_callback : None ,
875+ inspect_callback : None ,
876+ lexer_path : self . lexer_path . clone ( ) ,
857877 phantom : PhantomData ,
858878 } ;
859879 Ok ( cl. build ( ) ?. rule_ids )
@@ -930,7 +950,7 @@ where
930950 // rustc forces a recompile, this will change this value, causing anything which depends on
931951 // this build of lrpar to be recompiled too.
932952 let Self {
933- // All variables except for `output_path`, `header_callback ` and `phantom` should
953+ // All variables except for `output_path`, `inspect_callback ` and `phantom` should
934954 // be written into the cache.
935955 grammar_path,
936956 mod_name,
@@ -942,11 +962,14 @@ where
942962 show_warnings,
943963 visibility,
944964 rust_edition,
945- header_callback : _,
965+ inspect_callback : _,
966+ lexer_path,
946967 phantom : _,
947968 } = self ;
948969 let build_time = env ! ( "VERGEN_BUILD_TIMESTAMP" ) ;
949970 let grammar_path = grammar_path. as_ref ( ) . unwrap ( ) . to_string_lossy ( ) ;
971+ let empty_path = PathBuf :: new ( ) ;
972+ let lexer_path = lexer_path. as_ref ( ) . unwrap_or ( & empty_path) . to_string_lossy ( ) ;
950973 let mod_name = QuoteOption ( mod_name. as_deref ( ) ) ;
951974 let visibility = visibility. to_variant_tokens ( ) ;
952975 let rust_edition = rust_edition. to_variant_tokens ( ) ;
@@ -973,6 +996,7 @@ where
973996 RUST_EDITION = #rust_edition
974997 RULE_IDS_MAP = [ #( #rule_map, ) * ]
975998 VISIBILITY = #visibility
999+ LEX_PATH = #lexer_path
9761000 } ;
9771001 let cache_info_str = cache_info. to_string ( ) ;
9781002 quote ! ( #cache_info_str)
0 commit comments