Skip to content

Commit 93028a5

Browse files
committed
Replace _cache_info_ module with a constant string.
This should make the cache more robust to changes that are caused by the pretty printer.
1 parent 13e3a05 commit 93028a5

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

lrpar/src/lib/ctbuilder.rs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -599,13 +599,8 @@ where
599599
if FileTime::from_last_modification_time(out_rs_md)
600600
> FileTime::from_last_modification_time(inmd)
601601
{
602-
if let Ok(mut outc) = read_to_string(outp) {
603-
// Strip whitespace from the output file and the cache since the copy of
604-
// the cache in the output file may be affected by pretty printing.
605-
let mut cache = cache.to_string();
606-
outc.retain(|c| !c.is_whitespace());
607-
cache.retain(|c| !c.is_whitespace());
608-
if outc.contains(&cache) {
602+
if let Ok(outc) = read_to_string(outp) {
603+
if outc.contains(&cache.to_string()) {
609604
return Ok(CTParser {
610605
regenerated: false,
611606
rule_ids,
@@ -614,6 +609,8 @@ where
614609
} else {
615610
#[cfg(grmtools_extra_checks)]
616611
if std::env::var("CACHE_EXPECTED").is_ok() {
612+
eprintln!("outc: {}", outc);
613+
eprintln!("using cache: {}", cache,);
617614
// Primarily for use in the testsuite.
618615
panic!("The cache regenerated however, it was expected to match");
619616
}
@@ -644,7 +641,20 @@ where
644641
}
645642
}
646643

647-
self.output_file(&grm, &stable, &derived_mod_name, outp, &cache)?;
644+
self.output_file(
645+
&grm,
646+
&stable,
647+
&derived_mod_name,
648+
outp,
649+
&quote! {
650+
// This declaration can be affected by the pretty printer.
651+
// But we would hope the actual cache string is not.
652+
//
653+
// This is emitted for the purposes of performing the cache check.
654+
// on the output source, but is not used by generated parser.
655+
const _: &str = #cache;
656+
},
657+
)?;
648658
let conflicts = if stable.conflicts().is_some() {
649659
Some((grm, sgraph, stable))
650660
} else {
@@ -862,31 +872,22 @@ where
862872
))
863873
})
864874
.collect::<Vec<_>>();
865-
let rule_map_len = rule_map.len();
866-
quote! {
867-
#[allow(unused)]
868-
mod _cache_information_ {
869-
use ::lrpar::{RecoveryKind, Visibility, RustEdition};
870-
use ::cfgrammar::yacc::YaccKind;
871-
872-
const BUILD_TIME: &str = #build_time;
873-
// May differ from `MOD_NAME` by being derived from the grammar path.
874-
const DERIVED_MOD_NAME: &str = #derived_mod_name;
875-
const GRAMMAR_PATH: &str = #grammar_path;
876-
// As explicitly set by the builder.
877-
const MOD_NAME: Option<&str> = #mod_name;
878-
const RECOVERER: RecoveryKind = #recoverer;
879-
const YACC_KIND: YaccKind = #yacckind;
880-
const ERROR_ON_CONFLICTS: bool = #error_on_conflicts;
881-
const SHOW_WARNINGS: bool = #show_warnings;
882-
const WARNINGS_ARE_ERRORS: bool = #warnings_are_errors;
883-
const RUST_EDITION: RustEdition = #rust_edition;
884-
const RULE_IDS_MAP: [(usize, &str); #rule_map_len] = [#(#rule_map,)*];
885-
fn visibility() -> Visibility {
886-
#visibility
887-
}
888-
}
889-
}
875+
let cache_info = quote! {
876+
BUILD_TIME = #build_time
877+
DERIVED_MOD_NAME = #derived_mod_name
878+
GRAMMAR_PATH = #grammar_path
879+
MOD_NAME = #mod_name
880+
RECOVERER = #recoverer
881+
YACC_KIND = #yacckind
882+
ERROR_ON_CONFLICTS = #error_on_conflicts
883+
SHOW_WARNINGS = #show_warnings
884+
WARNINGS_ARE_ERRORS = #warnings_are_errors
885+
RUST_EDITION = #rust_edition
886+
RULE_IDS_MAP = [#(#rule_map,)*]
887+
VISIBILITY = #visibility
888+
};
889+
let cache_info_str = cache_info.to_string();
890+
quote!(#cache_info_str)
890891
}
891892

892893
/// Generate the main parse() function for the output file.

0 commit comments

Comments
 (0)