1use std::{
4 any::type_name,
5 collections::{HashMap, HashSet},
6 env::{current_dir, var},
7 error::Error,
8 fmt::{self, Debug, Write as fmtWrite},
9 fs::{self, File, create_dir_all, read_to_string},
10 hash::Hash,
11 io::Write,
12 marker::PhantomData,
13 path::{Path, PathBuf},
14 sync::{LazyLock, Mutex},
15};
16
17use crate::{
18 LexerTypes, RTParserBuilder, RecoveryKind,
19 diagnostics::{DiagnosticFormatter, SpannedDiagnosticFormatter},
20};
21
22#[cfg(feature = "_unstable_api")]
23use crate::unstable_api::UnstableApi;
24
25use bincode::{Decode, Encode, decode_from_slice, encode_to_vec};
26use cfgrammar::{
27 Location, RIdx, Symbol,
28 header::{GrmtoolsSectionParser, Header, HeaderValue, Value},
29 markmap::{Entry, MergeBehavior},
30 yacc::{YaccGrammar, YaccKind, YaccOriginalActionKind, ast::ASTWithValidityInfo},
31};
32use filetime::FileTime;
33use lrtable::{Minimiser, StateGraph, StateTable, from_yacc, statetable::Conflicts};
34use num_traits::{AsPrimitive, PrimInt, Unsigned};
35use proc_macro2::{Literal, TokenStream};
36use quote::{ToTokens, TokenStreamExt, format_ident, quote};
37use syn::{Generics, parse_quote};
38
39const ACTION_PREFIX: &str = "__gt_";
40const GLOBAL_PREFIX: &str = "__GT_";
41const ACTIONS_KIND: &str = "__GtActionsKind";
42const ACTIONS_KIND_PREFIX: &str = "Ak";
43const ACTIONS_KIND_HIDDEN: &str = "__GtActionsKindHidden";
44
45const RUST_FILE_EXT: &str = "rs";
46
47const WARNING: &str = "[Warning]";
48const ERROR: &str = "[Error]";
49
50static GENERATED_PATHS: LazyLock<Mutex<HashSet<PathBuf>>> =
51 LazyLock::new(|| Mutex::new(HashSet::new()));
52
53struct CTConflictsError<StorageT: Eq + Hash> {
54 conflicts_diagnostic: String,
55 #[cfg(test)]
56 #[cfg_attr(test, allow(dead_code))]
57 stable: StateTable<StorageT>,
58 phantom: PhantomData<StorageT>,
59}
60
61struct QuoteOption<T>(Option<T>);
67
68impl<T: ToTokens> ToTokens for QuoteOption<T> {
69 fn to_tokens(&self, tokens: &mut TokenStream) {
70 tokens.append_all(match self.0 {
71 Some(ref t) => quote! { ::std::option::Option::Some(#t) },
72 None => quote! { ::std::option::Option::None },
73 });
74 }
75}
76
77struct UnsuffixedUsize(usize);
82
83impl ToTokens for UnsuffixedUsize {
84 fn to_tokens(&self, tokens: &mut TokenStream) {
85 tokens.append(Literal::usize_unsuffixed(self.0))
86 }
87}
88
89struct QuoteTuple<T>(T);
92
93impl<A: ToTokens, B: ToTokens> ToTokens for QuoteTuple<(A, B)> {
94 fn to_tokens(&self, tokens: &mut TokenStream) {
95 let (a, b) = &self.0;
96 tokens.append_all(quote!((#a, #b)));
97 }
98}
99
100struct QuoteToString<'a>(&'a str);
102
103impl ToTokens for QuoteToString<'_> {
104 fn to_tokens(&self, tokens: &mut TokenStream) {
105 let x = &self.0;
106 tokens.append_all(quote! { #x.to_string() });
107 }
108}
109
110impl<StorageT> fmt::Display for CTConflictsError<StorageT>
111where
112 StorageT: 'static + Debug + Hash + PrimInt + Unsigned,
113 usize: AsPrimitive<StorageT>,
114{
115 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
116 write!(f, "{}", self.conflicts_diagnostic)
117 }
118}
119
120impl<StorageT> fmt::Debug for CTConflictsError<StorageT>
121where
122 StorageT: 'static + Debug + Hash + PrimInt + Unsigned,
123 usize: AsPrimitive<StorageT>,
124{
125 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
126 write!(f, "{}", self.conflicts_diagnostic)
127 }
128}
129
130impl<StorageT> Error for CTConflictsError<StorageT>
131where
132 StorageT: 'static + Debug + Hash + PrimInt + Unsigned,
133 usize: AsPrimitive<StorageT>,
134{
135}
136
137struct ErrorString(String);
139impl fmt::Display for ErrorString {
140 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141 let ErrorString(s) = self;
142 write!(f, "{}", s)
143 }
144}
145impl fmt::Debug for ErrorString {
146 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
147 let ErrorString(s) = self;
148 write!(f, "{}", s)
149 }
150}
151impl Error for ErrorString {}
152
153#[derive(Clone, PartialEq, Eq, Debug)]
155#[non_exhaustive]
156pub enum Visibility {
157 Private,
159 Public,
161 PublicSuper,
163 PublicSelf,
165 PublicCrate,
167 PublicIn(String),
169}
170
171#[derive(Clone, Copy, PartialEq, Eq, Debug)]
175#[non_exhaustive]
176pub enum RustEdition {
177 Rust2015,
178 Rust2018,
179 Rust2021,
180}
181
182impl RustEdition {
183 fn to_variant_tokens(self) -> TokenStream {
184 match self {
185 RustEdition::Rust2015 => quote!(::lrpar::RustEdition::Rust2015),
186 RustEdition::Rust2018 => quote!(::lrpar::RustEdition::Rust2018),
187 RustEdition::Rust2021 => quote!(::lrpar::RustEdition::Rust2021),
188 }
189 }
190}
191
192impl ToTokens for Visibility {
193 fn to_tokens(&self, tokens: &mut TokenStream) {
194 tokens.extend(match self {
195 Visibility::Private => quote!(),
196 Visibility::Public => quote! {pub},
197 Visibility::PublicSuper => quote! {pub(super)},
198 Visibility::PublicSelf => quote! {pub(self)},
199 Visibility::PublicCrate => quote! {pub(crate)},
200 Visibility::PublicIn(data) => {
201 let other = str::parse::<TokenStream>(data).unwrap();
202 quote! {pub(in #other)}
203 }
204 })
205 }
206}
207
208impl Visibility {
209 fn to_variant_tokens(&self) -> TokenStream {
210 match self {
211 Visibility::Private => quote!(::lrpar::Visibility::Private),
212 Visibility::Public => quote!(::lrpar::Visibility::Public),
213 Visibility::PublicSuper => quote!(::lrpar::Visibility::PublicSuper),
214 Visibility::PublicSelf => quote!(::lrpar::Visibility::PublicSelf),
215 Visibility::PublicCrate => quote!(::lrpar::Visibility::PublicCrate),
216 Visibility::PublicIn(data) => {
217 let data = QuoteToString(data);
218 quote!(::lrpar::Visibility::PublicIn(#data))
219 }
220 }
221 }
222}
223
224pub struct CTParserBuilder<'a, LexerTypesT: LexerTypes>
227where
228 LexerTypesT::StorageT: Eq + Hash,
229 usize: AsPrimitive<LexerTypesT::StorageT>,
230{
231 grammar_path: Option<PathBuf>,
235 grammar_src: Option<String>,
237 from_ast: Option<ASTWithValidityInfo>,
239 output_path: Option<PathBuf>,
240 mod_name: Option<&'a str>,
241 recoverer: Option<RecoveryKind>,
242 yacckind: Option<YaccKind>,
243 error_on_conflicts: bool,
244 warnings_are_errors: bool,
245 show_warnings: bool,
246 visibility: Visibility,
247 rust_edition: RustEdition,
248 inspect_rt: Option<
249 Box<
250 dyn for<'b> FnMut(
251 &'b mut Header<Location>,
252 RTParserBuilder<LexerTypesT::StorageT, LexerTypesT>,
253 &'b HashMap<String, LexerTypesT::StorageT>,
254 &PathBuf,
255 ) -> Result<(), Box<dyn Error>>,
256 >,
257 >,
258 #[cfg(test)]
260 inspect_callback: Option<Box<dyn Fn(RecoveryKind) -> Result<(), Box<dyn Error>>>>,
261 phantom: PhantomData<LexerTypesT>,
262}
263
264impl<
265 'a,
266 StorageT: 'static + Debug + Hash + PrimInt + Encode + Unsigned,
267 LexerTypesT: LexerTypes<StorageT = StorageT>,
268> CTParserBuilder<'a, LexerTypesT>
269where
270 usize: AsPrimitive<StorageT>,
271{
272 pub fn new() -> Self {
294 CTParserBuilder {
295 grammar_path: None,
296 grammar_src: None,
297 from_ast: None,
298 output_path: None,
299 mod_name: None,
300 recoverer: None,
301 yacckind: None,
302 error_on_conflicts: true,
303 warnings_are_errors: true,
304 show_warnings: true,
305 visibility: Visibility::Private,
306 rust_edition: RustEdition::Rust2021,
307 inspect_rt: None,
308 #[cfg(test)]
309 inspect_callback: None,
310 phantom: PhantomData,
311 }
312 }
313
314 pub fn grammar_in_src_dir<P>(mut self, srcp: P) -> Result<Self, Box<dyn Error>>
331 where
332 P: AsRef<Path>,
333 {
334 if !srcp.as_ref().is_relative() {
335 return Err(format!(
336 "Grammar path '{}' must be a relative path.",
337 srcp.as_ref().to_str().unwrap_or("<invalid UTF-8>")
338 )
339 .into());
340 }
341
342 let mut grmp = current_dir()?;
343 grmp.push("src");
344 grmp.push(srcp.as_ref());
345 self.grammar_path = Some(grmp);
346
347 let mut outp = PathBuf::new();
348 outp.push(var("OUT_DIR").unwrap());
349 outp.push(srcp.as_ref().parent().unwrap().to_str().unwrap());
350 create_dir_all(&outp)?;
351 let mut leaf = srcp
352 .as_ref()
353 .file_name()
354 .unwrap()
355 .to_str()
356 .unwrap()
357 .to_owned();
358 write!(leaf, ".{}", RUST_FILE_EXT).ok();
359 outp.push(leaf);
360 Ok(self.output_path(outp))
361 }
362
363 #[cfg(feature = "_unstable_api")]
366 pub fn grammar_ast(mut self, valid_ast: ASTWithValidityInfo, _api_key: UnstableApi) -> Self {
367 self.from_ast = Some(valid_ast);
368 self
369 }
370
371 pub fn grammar_path<P>(mut self, inp: P) -> Self
375 where
376 P: AsRef<Path>,
377 {
378 self.grammar_path = Some(inp.as_ref().to_owned());
379 self
380 }
381
382 #[cfg(feature = "_unstable_api")]
383 pub fn with_grammar_src(mut self, src: String, _api_key: UnstableApi) -> Self {
384 self.grammar_src = Some(src);
385 self
386 }
387
388 pub fn output_path<P>(mut self, outp: P) -> Self
393 where
394 P: AsRef<Path>,
395 {
396 self.output_path = Some(outp.as_ref().to_owned());
397 self
398 }
399
400 pub fn mod_name(mut self, mod_name: &'a str) -> Self {
404 self.mod_name = Some(mod_name);
405 self
406 }
407
408 pub fn visibility(mut self, vis: Visibility) -> Self {
410 self.visibility = vis;
411 self
412 }
413
414 pub fn recoverer(mut self, rk: RecoveryKind) -> Self {
416 self.recoverer = Some(rk);
417 self
418 }
419
420 pub fn yacckind(mut self, yk: YaccKind) -> Self {
422 self.yacckind = Some(yk);
423 self
424 }
425
426 pub fn error_on_conflicts(mut self, b: bool) -> Self {
429 self.error_on_conflicts = b;
430 self
431 }
432
433 pub fn warnings_are_errors(mut self, b: bool) -> Self {
436 self.warnings_are_errors = b;
437 self
438 }
439
440 pub fn show_warnings(mut self, b: bool) -> Self {
443 self.show_warnings = b;
444 self
445 }
446
447 pub fn rust_edition(mut self, edition: RustEdition) -> Self {
450 self.rust_edition = edition;
451 self
452 }
453
454 #[cfg(test)]
455 pub fn inspect_recoverer(
456 mut self,
457 cb: Box<dyn for<'h, 'y> Fn(RecoveryKind) -> Result<(), Box<dyn Error>>>,
458 ) -> Self {
459 self.inspect_callback = Some(cb);
460 self
461 }
462
463 #[doc(hidden)]
464 pub fn inspect_rt(
465 mut self,
466 cb: Box<
467 dyn for<'b, 'y> FnMut(
468 &'b mut Header<Location>,
469 RTParserBuilder<'y, StorageT, LexerTypesT>,
470 &'b HashMap<String, StorageT>,
471 &PathBuf,
472 ) -> Result<(), Box<dyn Error>>,
473 >,
474 ) -> Self {
475 self.inspect_rt = Some(cb);
476 self
477 }
478
479 pub fn build(mut self) -> Result<CTParser<StorageT>, Box<dyn Error>> {
532 let grmp = self
533 .grammar_path
534 .as_ref()
535 .expect("grammar_path must be specified before processing.");
536 let outp = self
537 .output_path
538 .as_ref()
539 .expect("output_path must be specified before processing.");
540 let mut header = Header::new();
541
542 match header.entry("yacckind".to_string()) {
543 Entry::Occupied(_) => unreachable!(),
544 Entry::Vacant(mut v) => match self.yacckind {
545 Some(YaccKind::Eco) => panic!("Eco compile-time grammar generation not supported."),
546 Some(yk) => {
547 let yk_value = Value::try_from(yk)?;
548 let mut o = v.insert_entry(HeaderValue(
549 Location::Other("CTParserBuilder".to_string()),
550 yk_value,
551 ));
552 o.set_merge_behavior(MergeBehavior::Ours);
553 }
554 None => {
555 v.mark_required();
556 }
557 },
558 }
559 if let Some(recoverer) = self.recoverer {
560 match header.entry("recoverer".to_string()) {
561 Entry::Occupied(_) => unreachable!(),
562 Entry::Vacant(v) => {
563 let rk_value: Value<Location> = Value::try_from(recoverer)?;
564 let mut o = v.insert_entry(HeaderValue(
565 Location::Other("CTParserBuilder".to_string()),
566 rk_value,
567 ));
568 o.set_merge_behavior(MergeBehavior::Ours);
569 }
570 }
571 }
572
573 {
574 let mut lk = GENERATED_PATHS.lock().unwrap();
575 if lk.contains(outp.as_path()) {
576 return Err(format!("Generating two parsers to the same path ('{}') is not allowed: use CTParserBuilder::output_path (and, optionally, CTParserBuilder::mod_name) to differentiate them.", &outp.to_str().unwrap()).into());
577 }
578 lk.insert(outp.clone());
579 }
580
581 let inc = if let Some(grammar_src) = &self.grammar_src {
582 grammar_src.clone()
583 } else {
584 read_to_string(grmp).map_err(|e| format!("When reading '{}': {e}", grmp.display()))?
585 };
586
587 let yacc_diag = SpannedDiagnosticFormatter::new(&inc, grmp);
588 let parsed_header = GrmtoolsSectionParser::new(&inc, false).parse();
589 if let Err(errs) = parsed_header {
590 let mut out = String::new();
591 out.push_str(&format!(
592 "\n{ERROR}{}\n",
593 yacc_diag.file_location_msg(" parsing the `%grmtools` section", None)
594 ));
595 for e in errs {
596 out.push_str(&indent(" ", &yacc_diag.format_error(e).to_string()));
597 }
598 return Err(ErrorString(out))?;
599 }
600 let (parsed_header, _) = parsed_header.unwrap();
601 header.merge_from(parsed_header)?;
602 self.yacckind = header
603 .get("yacckind")
604 .map(|HeaderValue(_, val)| val)
605 .map(YaccKind::try_from)
606 .transpose()?;
607 header.mark_used(&"yacckind".to_string());
608 let ast_validation = if let Some(ast) = &self.from_ast {
609 ast.clone()
610 } else if let Some(yk) = self.yacckind {
611 ASTWithValidityInfo::new(yk, &inc)
612 } else {
613 Err("Missing 'yacckind'".to_string())?
614 };
615
616 header.mark_used(&"recoverer".to_string());
617 let rk_val = header.get("recoverer").map(|HeaderValue(_, rk_val)| rk_val);
618
619 if let Some(rk_val) = rk_val {
620 self.recoverer = Some(RecoveryKind::try_from(rk_val)?);
621 } else {
622 self.recoverer = Some(RecoveryKind::CPCTPlus);
624 }
625 self.yacckind = Some(ast_validation.yacc_kind());
626 let warnings = ast_validation.ast().warnings();
627 let res = YaccGrammar::<StorageT>::new_from_ast_with_validity_info(&ast_validation);
628 let grm = match res {
629 Ok(_) if self.warnings_are_errors && !warnings.is_empty() => {
630 let mut out = String::new();
631 out.push_str(&format!(
632 "\n{ERROR}{}\n",
633 yacc_diag.file_location_msg("", None)
634 ));
635 for e in warnings {
636 out.push_str(&format!(
637 "{}\n",
638 indent(" ", &yacc_diag.format_warning(e).to_string())
639 ));
640 }
641 return Err(ErrorString(out))?;
642 }
643 Ok(grm) => {
644 if !warnings.is_empty() {
645 for w in warnings {
646 let ws_loc = yacc_diag.file_location_msg("", None);
647 let ws = indent(" ", &yacc_diag.format_warning(w).to_string());
648 if std::env::var("OUT_DIR").is_ok() && self.show_warnings {
650 for line in ws_loc.lines().chain(ws.lines()) {
651 println!("cargo:warning={}", line);
652 }
653 } else if self.show_warnings {
654 eprintln!("{}", ws_loc);
655 eprintln!("{WARNING} {}", ws);
656 }
657 }
658 }
659 grm
660 }
661 Err(errs) => {
662 let mut out = String::new();
663 out.push_str(&format!(
664 "\n{ERROR}{}\n",
665 yacc_diag.file_location_msg("", None)
666 ));
667 for e in errs {
668 out.push_str(&indent(" ", &yacc_diag.format_error(e).to_string()));
669 out.push('\n');
670 }
671
672 return Err(ErrorString(out))?;
673 }
674 };
675
676 #[cfg(test)]
677 if let Some(cb) = &self.inspect_callback {
678 cb(self.recoverer.expect("has a default value"))?;
679 }
680
681 let rule_ids = grm
682 .tokens_map()
683 .iter()
684 .map(|(&n, &i)| (n.to_owned(), i.as_storaget()))
685 .collect::<HashMap<_, _>>();
686
687 let derived_mod_name = match self.mod_name {
688 Some(s) => s.to_owned(),
689 None => {
690 let mut stem = grmp.to_str().unwrap();
695 loop {
696 let new_stem = Path::new(stem).file_stem().unwrap().to_str().unwrap();
697 if stem == new_stem {
698 break;
699 }
700 stem = new_stem;
701 }
702 format!("{}_y", stem)
703 }
704 };
705
706 let cache = self.rebuild_cache(&derived_mod_name, &grm);
707
708 if let Ok(ref inmd) = fs::metadata(grmp) {
716 if let Ok(ref out_rs_md) = fs::metadata(outp) {
717 if FileTime::from_last_modification_time(out_rs_md)
718 > FileTime::from_last_modification_time(inmd)
719 {
720 if let Ok(outc) = read_to_string(outp) {
721 if outc.contains(&cache.to_string()) {
722 return Ok(CTParser {
723 regenerated: false,
724 rule_ids,
725 yacc_grammar: grm,
726 grammar_src: inc,
727 grammar_path: self.grammar_path.unwrap(),
728 conflicts: None,
729 });
730 } else {
731 #[cfg(grmtools_extra_checks)]
732 if std::env::var("CACHE_EXPECTED").is_ok() {
733 eprintln!("outc: {}", outc);
734 eprintln!("using cache: {}", cache,);
735 panic!("The cache regenerated however, it was expected to match");
737 }
738 }
739 }
740 }
741 }
742 }
743
744 fs::remove_file(outp).ok();
751
752 let (sgraph, stable) = from_yacc(&grm, Minimiser::Pager)?;
753 if self.error_on_conflicts {
754 if let Some(c) = stable.conflicts() {
755 match (grm.expect(), grm.expectrr()) {
756 (Some(i), Some(j)) if i == c.sr_len() && j == c.rr_len() => (),
757 (Some(i), None) if i == c.sr_len() && 0 == c.rr_len() => (),
758 (None, Some(j)) if 0 == c.sr_len() && j == c.rr_len() => (),
759 (None, None) if 0 == c.rr_len() && 0 == c.sr_len() => (),
760 _ => {
761 let conflicts_diagnostic = yacc_diag.format_conflicts::<LexerTypesT>(
762 &grm,
763 ast_validation.ast(),
764 c,
765 &sgraph,
766 &stable,
767 );
768 return Err(Box::new(CTConflictsError {
769 conflicts_diagnostic,
770 phantom: PhantomData,
771 #[cfg(test)]
772 stable,
773 }));
774 }
775 }
776 }
777 }
778
779 if let Some(ref mut inspector_rt) = self.inspect_rt {
780 let rt: RTParserBuilder<'_, StorageT, LexerTypesT> =
781 RTParserBuilder::new(&grm, &stable);
782 let rt = if let Some(rk) = self.recoverer {
783 rt.recoverer(rk)
784 } else {
785 rt
786 };
787 inspector_rt(&mut header, rt, &rule_ids, grmp)?
788 }
789
790 let unused_keys = header.unused();
791 if !unused_keys.is_empty() {
792 return Err(format!("Unused keys in header: {}", unused_keys.join(", ")).into());
793 }
794 let missing_keys = header
795 .missing()
796 .iter()
797 .map(|s| s.as_str())
798 .collect::<Vec<_>>();
799 if !missing_keys.is_empty() {
800 return Err(format!(
801 "Required values were missing from the header: {}",
802 missing_keys.join(", ")
803 )
804 .into());
805 }
806
807 self.output_file(
808 &grm,
809 &stable,
810 &derived_mod_name,
811 outp,
812 &format!("/* CACHE INFORMATION {} */\n", cache),
813 )?;
814 let conflicts = if stable.conflicts().is_some() {
815 Some((sgraph, stable))
816 } else {
817 None
818 };
819 Ok(CTParser {
820 regenerated: true,
821 rule_ids,
822 yacc_grammar: grm,
823 grammar_src: inc,
824 grammar_path: self.grammar_path.unwrap(),
825 conflicts,
826 })
827 }
828
829 #[deprecated(
836 since = "0.11.0",
837 note = "Please use grammar_in_src_dir(), build(), and token_map() instead"
838 )]
839 #[allow(deprecated)]
840 pub fn process_file_in_src(
841 &mut self,
842 srcp: &str,
843 ) -> Result<HashMap<String, StorageT>, Box<dyn Error>> {
844 let mut inp = current_dir()?;
845 inp.push("src");
846 inp.push(srcp);
847 let mut outp = PathBuf::new();
848 outp.push(var("OUT_DIR").unwrap());
849 outp.push(Path::new(srcp).parent().unwrap().to_str().unwrap());
850 create_dir_all(&outp)?;
851 let mut leaf = Path::new(srcp)
852 .file_name()
853 .unwrap()
854 .to_str()
855 .unwrap()
856 .to_owned();
857 write!(leaf, ".{}", RUST_FILE_EXT).ok();
858 outp.push(leaf);
859 self.process_file(inp, outp)
860 }
861
862 #[deprecated(
898 since = "0.11.0",
899 note = "Please use grammar_path(), output_path(), build(), and token_map() instead"
900 )]
901 pub fn process_file<P, Q>(
902 &mut self,
903 inp: P,
904 outp: Q,
905 ) -> Result<HashMap<String, StorageT>, Box<dyn Error>>
906 where
907 P: AsRef<Path>,
908 Q: AsRef<Path>,
909 {
910 self.grammar_path = Some(inp.as_ref().to_owned());
911 self.output_path = Some(outp.as_ref().to_owned());
912 let cl: CTParserBuilder<LexerTypesT> = CTParserBuilder {
913 grammar_path: self.grammar_path.clone(),
914 grammar_src: None,
915 from_ast: None,
916 output_path: self.output_path.clone(),
917 mod_name: self.mod_name,
918 recoverer: self.recoverer,
919 yacckind: self.yacckind,
920 error_on_conflicts: self.error_on_conflicts,
921 warnings_are_errors: self.warnings_are_errors,
922 show_warnings: self.show_warnings,
923 visibility: self.visibility.clone(),
924 rust_edition: self.rust_edition,
925 inspect_rt: None,
926 #[cfg(test)]
927 inspect_callback: None,
928 phantom: PhantomData,
929 };
930 Ok(cl.build()?.rule_ids)
931 }
932
933 fn output_file<P: AsRef<Path>>(
934 &self,
935 grm: &YaccGrammar<StorageT>,
936 stable: &StateTable<StorageT>,
937 mod_name: &str,
938 outp_rs: P,
939 cache: &str,
940 ) -> Result<(), Box<dyn Error>> {
941 let visibility = self.visibility.clone();
942 let user_actions = if let Some(
943 YaccKind::Original(YaccOriginalActionKind::UserAction) | YaccKind::Grmtools,
944 ) = self.yacckind
945 {
946 Some(self.gen_user_actions(grm)?)
947 } else {
948 None
949 };
950 let rule_consts = self.gen_rule_consts(grm)?;
951 let token_epp = self.gen_token_epp(grm)?;
952 let parse_function = self.gen_parse_function(grm, stable)?;
953 let action_wrappers = match self.yacckind.unwrap() {
954 YaccKind::Original(YaccOriginalActionKind::UserAction) | YaccKind::Grmtools => {
955 Some(self.gen_wrappers(grm)?)
956 }
957 YaccKind::Original(YaccOriginalActionKind::NoAction)
958 | YaccKind::Original(YaccOriginalActionKind::GenericParseTree) => None,
959 _ => unreachable!(),
960 };
961
962 let additional_decls =
963 if let Some(YaccKind::Original(YaccOriginalActionKind::GenericParseTree)) =
964 self.yacckind
965 {
966 Some(quote! {
969 #[allow(unused_imports)]
970 pub use ::lrpar::parser::_deprecated_moved_::Node;
971 })
972 } else {
973 None
974 };
975
976 let mod_name = format_ident!("{}", mod_name);
977 let out_tokens = quote! {
978 #visibility mod #mod_name {
979 #user_actions
981 mod _parser_ {
982 #![allow(clippy::type_complexity)]
983 #![allow(clippy::unnecessary_wraps)]
984 #![deny(unsafe_code)]
985 #[allow(unused_imports)]
986 use super::*;
987 #additional_decls
988 #parse_function
989 #rule_consts
990 #token_epp
991 #action_wrappers
992 } #[allow(unused_imports)]
994 pub use _parser_::*;
995 #[allow(unused_imports)]
996 use ::lrpar::Lexeme;
997 } };
999 let unformatted = out_tokens.to_string();
1001 let outs = syn::parse_str(&unformatted)
1002 .map(|syntax_tree| prettyplease::unparse(&syntax_tree))
1003 .unwrap_or(unformatted);
1004 let mut f = File::create(outp_rs)?;
1005 f.write_all(outs.as_bytes())?;
1006 f.write_all(cache.as_bytes())?;
1007 Ok(())
1008 }
1009
1010 fn rebuild_cache(&self, derived_mod_name: &'_ str, grm: &YaccGrammar<StorageT>) -> TokenStream {
1013 let Self {
1020 grammar_path,
1023 grammar_src: _,
1025 from_ast: _,
1027 mod_name,
1028 recoverer,
1029 yacckind,
1030 output_path: _,
1031 error_on_conflicts,
1032 warnings_are_errors,
1033 show_warnings,
1034 visibility,
1035 rust_edition,
1036 inspect_rt: _,
1037 #[cfg(test)]
1038 inspect_callback: _,
1039 phantom: _,
1040 } = self;
1041 let build_time = env!("VERGEN_BUILD_TIMESTAMP");
1042 let grammar_path = grammar_path.as_ref().unwrap().to_string_lossy();
1043 let mod_name = QuoteOption(mod_name.as_deref());
1044 let visibility = visibility.to_variant_tokens();
1045 let rust_edition = rust_edition.to_variant_tokens();
1046 let yacckind = yacckind.expect("is_some() by this point");
1047 let rule_map = grm
1048 .iter_tidxs()
1049 .map(|tidx| {
1050 QuoteTuple((
1051 usize::from(tidx),
1052 grm.token_name(tidx).unwrap_or("<unknown>"),
1053 ))
1054 })
1055 .collect::<Vec<_>>();
1056 let cache_info = quote! {
1057 BUILD_TIME = #build_time
1058 DERIVED_MOD_NAME = #derived_mod_name
1059 GRAMMAR_PATH = #grammar_path
1060 MOD_NAME = #mod_name
1061 RECOVERER = #recoverer
1062 YACC_KIND = #yacckind
1063 ERROR_ON_CONFLICTS = #error_on_conflicts
1064 SHOW_WARNINGS = #show_warnings
1065 WARNINGS_ARE_ERRORS = #warnings_are_errors
1066 RUST_EDITION = #rust_edition
1067 RULE_IDS_MAP = [#(#rule_map,)*]
1068 VISIBILITY = #visibility
1069 };
1070 let cache_info_str = cache_info.to_string();
1071 quote!(#cache_info_str)
1072 }
1073
1074 fn gen_parse_function(
1076 &self,
1077 grm: &YaccGrammar<StorageT>,
1078 stable: &StateTable<StorageT>,
1079 ) -> Result<TokenStream, Box<dyn Error>> {
1080 let storaget = str::parse::<TokenStream>(type_name::<StorageT>())?;
1081 let lexertypest = str::parse::<TokenStream>(type_name::<LexerTypesT>())?;
1082 let recoverer = self.recoverer;
1083 let run_parser = match self.yacckind.unwrap() {
1084 YaccKind::Original(YaccOriginalActionKind::GenericParseTree) => {
1085 quote! {
1086 ::lrpar::RTParserBuilder::new(&grm, &stable)
1087 .recoverer(#recoverer)
1088 .parse_map(
1089 lexer,
1090 &|lexeme| Node::Term{lexeme},
1091 &|ridx, nodes| Node::Nonterm{ridx, nodes}
1092 )
1093 }
1094 }
1095 YaccKind::Original(YaccOriginalActionKind::NoAction) => {
1096 quote! {
1097 ::lrpar::RTParserBuilder::new(&grm, &stable)
1098 .recoverer(#recoverer)
1099 .parse_map(lexer, &|_| (), &|_, _| ()).1
1100 }
1101 }
1102 YaccKind::Original(YaccOriginalActionKind::UserAction) | YaccKind::Grmtools => {
1103 let actionskind = str::parse::<TokenStream>(ACTIONS_KIND)?;
1104 let parsed_parse_generics = make_generics(grm.parse_generics().as_deref())?;
1105 let (_, type_generics, _) = parsed_parse_generics.split_for_impl();
1106 let (action_fn_parse_param, action_fn_parse_param_ty) = match grm.parse_param() {
1109 Some((name, ty)) => {
1110 let name = str::parse::<TokenStream>(name)?;
1111 let ty = str::parse::<TokenStream>(ty)?;
1112 (quote!(#name), quote!(#ty))
1113 }
1114 None => (quote!(()), quote!(())),
1115 };
1116 let wrappers = grm.iter_pidxs().map(|pidx| {
1117 let pidx = usize::from(pidx);
1118 format_ident!("{}wrapper_{}", ACTION_PREFIX, pidx)
1119 });
1120 let edition_lifetime = if self.rust_edition != RustEdition::Rust2015 {
1121 quote!('_,)
1122 } else {
1123 quote!()
1124 };
1125 let ridx = usize::from(self.user_start_ridx(grm));
1126 let action_ident = format_ident!("{}{}", ACTIONS_KIND_PREFIX, ridx);
1127
1128 quote! {
1129 let actions: ::std::vec::Vec<
1130 &dyn Fn(
1131 ::cfgrammar::RIdx<#storaget>,
1132 &'lexer dyn ::lrpar::NonStreamingLexer<'input, #lexertypest>,
1133 ::cfgrammar::Span,
1134 ::std::vec::Drain<#edition_lifetime ::lrpar::parser::AStackType<<#lexertypest as ::lrpar::LexerTypes>::LexemeT, #actionskind #type_generics>>,
1135 #action_fn_parse_param_ty
1136 ) -> #actionskind #type_generics
1137 > = ::std::vec![#(&#wrappers,)*];
1138 match ::lrpar::RTParserBuilder::new(&grm, &stable)
1139 .recoverer(#recoverer)
1140 .parse_actions(lexer, &actions, #action_fn_parse_param) {
1141 (Some(#actionskind::#action_ident(x)), y) => (Some(x), y),
1142 (None, y) => (None, y),
1143 _ => unreachable!()
1144 }
1145 }
1146 }
1147 kind => panic!("YaccKind {:?} not supported", kind),
1148 };
1149
1150 let parsed_parse_generics: Generics = match self.yacckind.unwrap() {
1151 YaccKind::Original(YaccOriginalActionKind::UserAction) | YaccKind::Grmtools => {
1152 make_generics(grm.parse_generics().as_deref())?
1153 }
1154 _ => make_generics(None)?,
1155 };
1156 let (generics, _, where_clause) = parsed_parse_generics.split_for_impl();
1157
1158 let parse_fn_parse_param = match self.yacckind.unwrap() {
1160 YaccKind::Original(YaccOriginalActionKind::UserAction) | YaccKind::Grmtools => {
1161 if let Some((name, tyname)) = grm.parse_param() {
1162 let name = str::parse::<TokenStream>(name)?;
1163 let tyname = str::parse::<TokenStream>(tyname)?;
1164 Some(quote! {#name: #tyname})
1165 } else {
1166 None
1167 }
1168 }
1169 _ => None,
1170 };
1171 let parse_fn_return_ty = match self.yacckind.unwrap() {
1172 YaccKind::Original(YaccOriginalActionKind::UserAction) | YaccKind::Grmtools => {
1173 let actiont = grm
1174 .actiontype(self.user_start_ridx(grm))
1175 .as_ref()
1176 .map(|at| str::parse::<TokenStream>(at))
1177 .transpose()?;
1178 quote! {
1179 (::std::option::Option<#actiont>, ::std::vec::Vec<::lrpar::LexParseError<#storaget, #lexertypest>>)
1180 }
1181 }
1182 YaccKind::Original(YaccOriginalActionKind::GenericParseTree) => quote! {
1183 (::std::option::Option<Node<<#lexertypest as ::lrpar::LexerTypes>::LexemeT, #storaget>>,
1184 ::std::vec::Vec<::lrpar::LexParseError<#storaget, #lexertypest>>)
1185 },
1186 YaccKind::Original(YaccOriginalActionKind::NoAction) => quote! {
1187 ::std::vec::Vec<::lrpar::LexParseError<#storaget, #lexertypest>>
1188 },
1189 _ => unreachable!(),
1190 };
1191
1192 let grm_data = encode_to_vec(grm, bincode::config::standard())?;
1193 let stable_data = encode_to_vec(stable, bincode::config::standard())?;
1194 Ok(quote! {
1195 const __GRM_DATA: &[u8] = &[#(#grm_data,)*];
1196 const __STABLE_DATA: &[u8] = &[#(#stable_data,)*];
1197
1198 #[allow(dead_code)]
1199 pub fn parse #generics (
1200 lexer: &'lexer dyn ::lrpar::NonStreamingLexer<'input, #lexertypest>,
1201 #parse_fn_parse_param
1202 ) -> #parse_fn_return_ty
1203 #where_clause
1204 {
1205 let (grm, stable) = ::lrpar::ctbuilder::_reconstitute(__GRM_DATA, __STABLE_DATA);
1206 #run_parser
1207 }
1208 })
1209 }
1210
1211 fn gen_rule_consts(
1212 &self,
1213 grm: &YaccGrammar<StorageT>,
1214 ) -> Result<TokenStream, proc_macro2::LexError> {
1215 let mut toks = TokenStream::new();
1216 for ridx in grm.iter_rules() {
1217 if !grm.rule_to_prods(ridx).contains(&grm.start_prod()) {
1218 let r_const = format_ident!("R_{}", grm.rule_name_str(ridx).to_ascii_uppercase());
1219 let storage_ty = str::parse::<TokenStream>(type_name::<StorageT>())?;
1220 let ridx = UnsuffixedUsize(usize::from(ridx));
1221 toks.extend(quote! {
1222 #[allow(dead_code)]
1223 pub const #r_const: #storage_ty = #ridx;
1224 });
1225 }
1226 }
1227 Ok(toks)
1228 }
1229
1230 fn gen_token_epp(
1231 &self,
1232 grm: &YaccGrammar<StorageT>,
1233 ) -> Result<TokenStream, proc_macro2::LexError> {
1234 let mut tidxs = Vec::new();
1235 for tidx in grm.iter_tidxs() {
1236 tidxs.push(QuoteOption(grm.token_epp(tidx)));
1237 }
1238 let const_epp_ident = format_ident!("{}EPP", GLOBAL_PREFIX);
1239 let storage_ty = str::parse::<TokenStream>(type_name::<StorageT>())?;
1240 Ok(quote! {
1241 const #const_epp_ident: &[::std::option::Option<&str>] = &[
1242 #(#tidxs,)*
1243 ];
1244
1245 #[allow(dead_code)]
1248 pub fn token_epp<'a>(tidx: ::cfgrammar::TIdx<#storage_ty>) -> ::std::option::Option<&'a str> {
1249 #const_epp_ident[usize::from(tidx)]
1250 }
1251 })
1252 }
1253
1254 fn gen_wrappers(&self, grm: &YaccGrammar<StorageT>) -> Result<TokenStream, Box<dyn Error>> {
1256 let parsed_parse_generics = make_generics(grm.parse_generics().as_deref())?;
1257 let (generics, type_generics, where_clause) = parsed_parse_generics.split_for_impl();
1258
1259 let (parse_paramname, parse_paramdef);
1260 match grm.parse_param() {
1261 Some((name, tyname)) => {
1262 parse_paramname = str::parse::<TokenStream>(name)?;
1263 let ty = str::parse::<TokenStream>(tyname)?;
1264 parse_paramdef = quote!(#parse_paramname: #ty);
1265 }
1266 None => {
1267 parse_paramname = quote!(());
1268 parse_paramdef = quote! {_: ()};
1269 }
1270 };
1271
1272 let mut wrappers = TokenStream::new();
1273 for pidx in grm.iter_pidxs() {
1274 let ridx = grm.prod_to_rule(pidx);
1275
1276 let wrapper_fn = format_ident!("{}wrapper_{}", ACTION_PREFIX, usize::from(pidx));
1280 let ridx_var = format_ident!("{}ridx", ACTION_PREFIX);
1281 let lexer_var = format_ident!("{}lexer", ACTION_PREFIX);
1282 let span_var = format_ident!("{}span", ACTION_PREFIX);
1283 let args_var = format_ident!("{}args", ACTION_PREFIX);
1284 let storaget = str::parse::<TokenStream>(type_name::<StorageT>())?;
1285 let lexertypest = str::parse::<TokenStream>(type_name::<LexerTypesT>())?;
1286 let actionskind = str::parse::<TokenStream>(ACTIONS_KIND)?;
1287 let edition_lifetime = if self.rust_edition != RustEdition::Rust2015 {
1288 Some(quote!('_,))
1289 } else {
1290 None
1291 };
1292 let mut wrapper_fn_body = TokenStream::new();
1293 if grm.action(pidx).is_some() {
1294 for i in 0..grm.prod(pidx).len() {
1296 let arg = format_ident!("{}arg_{}", ACTION_PREFIX, i + 1);
1297 wrapper_fn_body.extend(match grm.prod(pidx)[i] {
1298 Symbol::Rule(ref_ridx) => {
1299 let ref_ridx = usize::from(ref_ridx);
1300 let actionvariant = format_ident!("{}{}", ACTIONS_KIND_PREFIX, ref_ridx);
1301 quote! {
1302 #[allow(clippy::let_unit_value)]
1303 let #arg = match #args_var.next().unwrap() {
1304 ::lrpar::parser::AStackType::ActionType(#actionskind::#type_generics::#actionvariant(x)) => x,
1305 _ => unreachable!()
1306 };
1307 }
1308 }
1309 Symbol::Token(_) => {
1310 quote! {
1311 let #arg = match #args_var.next().unwrap() {
1312 ::lrpar::parser::AStackType::Lexeme(l) => {
1313 if l.faulty() {
1314 Err(l)
1315 } else {
1316 Ok(l)
1317 }
1318 },
1319 ::lrpar::parser::AStackType::ActionType(_) => unreachable!()
1320 };
1321 }
1322 }
1323 })
1324 }
1325
1326 let args = (0..grm.prod(pidx).len())
1328 .map(|i| format_ident!("{}arg_{}", ACTION_PREFIX, i + 1))
1329 .collect::<Vec<_>>();
1330 let action_fn = format_ident!("{}action_{}", ACTION_PREFIX, usize::from(pidx));
1331 let actionsvariant = format_ident!("{}{}", ACTIONS_KIND_PREFIX, usize::from(ridx));
1332
1333 wrapper_fn_body.extend(match grm.actiontype(ridx) {
1334 Some(s) if s == "()" => {
1335 quote! {
1339 #action_fn(#ridx_var, #lexer_var, #span_var, #parse_paramname, #(#args,)*);
1340 #actionskind::#type_generics::#actionsvariant(())
1341 }
1342 }
1343 _ => {
1344 quote! {
1345 #actionskind::#type_generics::#actionsvariant(#action_fn(#ridx_var, #lexer_var, #span_var, #parse_paramname, #(#args,)*))
1346 }
1347 }
1348 })
1349 } else if pidx == grm.start_prod() {
1350 wrapper_fn_body.extend(quote!(unreachable!()));
1351 } else {
1352 panic!(
1353 "Production in rule '{}' must have an action body.",
1354 grm.rule_name_str(grm.prod_to_rule(pidx))
1355 );
1356 };
1357
1358 let attrib = if pidx == grm.start_prod() {
1359 Some(quote!(#[allow(unused_variables)]))
1361 } else {
1362 None
1363 };
1364 wrappers.extend(quote! {
1365 #attrib
1366 fn #wrapper_fn #generics (
1367 #ridx_var: ::cfgrammar::RIdx<#storaget>,
1368 #lexer_var: &'lexer dyn ::lrpar::NonStreamingLexer<'input, #lexertypest>,
1369 #span_var: ::cfgrammar::Span,
1370 mut #args_var: ::std::vec::Drain<#edition_lifetime ::lrpar::parser::AStackType<<#lexertypest as ::lrpar::LexerTypes>::LexemeT, #actionskind #type_generics>>,
1371 #parse_paramdef
1372 ) -> #actionskind #type_generics
1373 #where_clause
1374 {
1375 #wrapper_fn_body
1376 }
1377 })
1378 }
1379 let mut actionskindvariants = Vec::new();
1380 let actionskindhidden = format_ident!("_{}", ACTIONS_KIND_HIDDEN);
1381 let actionskind = str::parse::<TokenStream>(ACTIONS_KIND).unwrap();
1382 let mut phantom_data_type = Vec::new();
1383 for ridx in grm.iter_rules() {
1384 if let Some(actiont) = grm.actiontype(ridx) {
1385 let actionskindvariant =
1386 format_ident!("{}{}", ACTIONS_KIND_PREFIX, usize::from(ridx));
1387 let actiont = str::parse::<TokenStream>(actiont).unwrap();
1388 actionskindvariants.push(quote! {
1389 #actionskindvariant(#actiont)
1390 })
1391 }
1392 }
1393 for lifetime in parsed_parse_generics.lifetimes() {
1394 let lifetime = &lifetime.lifetime;
1395 phantom_data_type.push(quote! { &#lifetime () });
1396 }
1397 for type_param in parsed_parse_generics.type_params() {
1398 let ident = &type_param.ident;
1399 phantom_data_type.push(quote! { #ident });
1400 }
1401 actionskindvariants.push(quote! {
1402 #actionskindhidden(::std::marker::PhantomData<(#(#phantom_data_type,)*)>)
1403 });
1404 wrappers.extend(quote! {
1405 #[allow(dead_code)]
1406 enum #actionskind #generics #where_clause {
1407 #(#actionskindvariants,)*
1408 }
1409 });
1410 Ok(wrappers)
1411 }
1412
1413 fn gen_user_actions(&self, grm: &YaccGrammar<StorageT>) -> Result<TokenStream, Box<dyn Error>> {
1415 let programs = grm
1416 .programs()
1417 .as_ref()
1418 .map(|s| str::parse::<TokenStream>(s))
1419 .transpose()?;
1420 let mut action_fns = TokenStream::new();
1421 let parsed_parse_generics = make_generics(grm.parse_generics().as_deref())?;
1423 let (generics, _, where_clause) = parsed_parse_generics.split_for_impl();
1424 let (parse_paramname, parse_paramdef, parse_param_unit);
1425 match grm.parse_param() {
1426 Some((name, tyname)) => {
1427 parse_param_unit = tyname.trim() == "()";
1428 parse_paramname = str::parse::<TokenStream>(name)?;
1429 let ty = str::parse::<TokenStream>(tyname)?;
1430 parse_paramdef = quote!(#parse_paramname: #ty);
1431 }
1432 None => {
1433 parse_param_unit = true;
1434 parse_paramname = quote!(());
1435 parse_paramdef = quote! {_: ()};
1436 }
1437 };
1438 for pidx in grm.iter_pidxs() {
1439 if pidx == grm.start_prod() {
1440 continue;
1441 }
1442
1443 let mut args = Vec::with_capacity(grm.prod(pidx).len());
1445 for i in 0..grm.prod(pidx).len() {
1446 let argt = match grm.prod(pidx)[i] {
1447 Symbol::Rule(ref_ridx) => {
1448 str::parse::<TokenStream>(grm.actiontype(ref_ridx).as_ref().unwrap())?
1449 }
1450 Symbol::Token(_) => {
1451 let lexemet =
1452 str::parse::<TokenStream>(type_name::<LexerTypesT::LexemeT>())?;
1453 quote!(::std::result::Result<#lexemet, #lexemet>)
1454 }
1455 };
1456 let arg = format_ident!("{}arg_{}", ACTION_PREFIX, i + 1);
1457 args.push(quote!(mut #arg: #argt));
1458 }
1459
1460 let returnt = {
1464 let actiont = grm.actiontype(grm.prod_to_rule(pidx)).as_ref().unwrap();
1465 if actiont == "()" {
1466 None
1467 } else {
1468 let actiont = str::parse::<TokenStream>(actiont)?;
1469 Some(quote!( -> #actiont))
1470 }
1471 };
1472 let action_fn = format_ident!("{}action_{}", ACTION_PREFIX, usize::from(pidx));
1473 let lexer_var = format_ident!("{}lexer", ACTION_PREFIX);
1474 let span_var = format_ident!("{}span", ACTION_PREFIX);
1475 let ridx_var = format_ident!("{}ridx", ACTION_PREFIX);
1476 let storaget = str::parse::<TokenStream>(type_name::<StorageT>())?;
1477 let lexertypest = str::parse::<TokenStream>(type_name::<LexerTypesT>())?;
1478 let bind_parse_param = if !parse_param_unit {
1479 Some(quote! {let _ = #parse_paramname;})
1480 } else {
1481 None
1482 };
1483
1484 let pre_action = grm.action(pidx).as_ref().ok_or_else(|| {
1487 format!(
1488 "Rule {} has a production which is missing action code",
1489 grm.rule_name_str(grm.prod_to_rule(pidx))
1490 )
1491 })?;
1492 let mut last = 0;
1493 let mut outs = String::new();
1494 loop {
1495 match pre_action[last..].find('$') {
1496 Some(off) => {
1497 if pre_action[last + off..].starts_with("$$") {
1498 outs.push_str(&pre_action[last..last + off + "$".len()]);
1499 last = last + off + "$$".len();
1500 } else if pre_action[last + off..].starts_with("$lexer") {
1501 outs.push_str(&pre_action[last..last + off]);
1502 write!(outs, "{prefix}lexer", prefix = ACTION_PREFIX).ok();
1503 last = last + off + "$lexer".len();
1504 } else if pre_action[last + off..].starts_with("$span") {
1505 outs.push_str(&pre_action[last..last + off]);
1506 write!(outs, "{prefix}span", prefix = ACTION_PREFIX).ok();
1507 last = last + off + "$span".len();
1508 } else if last + off + 1 < pre_action.len()
1509 && pre_action[last + off + 1..].starts_with(|c: char| c.is_numeric())
1510 {
1511 outs.push_str(&pre_action[last..last + off]);
1512 write!(outs, "{prefix}arg_", prefix = ACTION_PREFIX).ok();
1513 last = last + off + "$".len();
1514 } else {
1515 panic!(
1516 "Unknown text following '$' operator: {}",
1517 &pre_action[last + off..]
1518 );
1519 }
1520 }
1521 None => {
1522 outs.push_str(&pre_action[last..]);
1523 break;
1524 }
1525 }
1526 }
1527
1528 let action_body = str::parse::<TokenStream>(&outs)?;
1529 action_fns.extend(quote! {
1530 #[allow(clippy::too_many_arguments)]
1531 fn #action_fn #generics (
1532 #ridx_var: ::cfgrammar::RIdx<#storaget>,
1533 #lexer_var: &'lexer dyn ::lrpar::NonStreamingLexer<'input, #lexertypest>,
1534 #span_var: ::cfgrammar::Span,
1535 #parse_paramdef,
1536 #(#args,)*
1537 ) #returnt
1538 #where_clause
1539 {
1540 #bind_parse_param
1541 #action_body
1542 }
1543 })
1544 }
1545 Ok(quote! {
1546 #programs
1547 #action_fns
1548 })
1549 }
1550
1551 fn user_start_ridx(&self, grm: &YaccGrammar<StorageT>) -> RIdx<StorageT> {
1555 debug_assert_eq!(grm.prod(grm.start_prod()).len(), 1);
1556 match grm.prod(grm.start_prod())[0] {
1557 Symbol::Rule(ridx) => ridx,
1558 _ => unreachable!(),
1559 }
1560 }
1561}
1562
1563#[doc(hidden)]
1566pub fn _reconstitute<StorageT: Decode<()> + Hash + PrimInt + Unsigned + 'static>(
1567 grm_buf: &[u8],
1568 stable_buf: &[u8],
1569) -> (YaccGrammar<StorageT>, StateTable<StorageT>) {
1570 let (grm, _) = decode_from_slice(grm_buf, bincode::config::standard()).unwrap();
1571 let (stable, _) = decode_from_slice(stable_buf, bincode::config::standard()).unwrap();
1572 (grm, stable)
1573}
1574
1575pub struct CTParser<StorageT = u32>
1577where
1578 StorageT: Eq + Hash,
1579{
1580 regenerated: bool,
1581 rule_ids: HashMap<String, StorageT>,
1582 yacc_grammar: YaccGrammar<StorageT>,
1583 grammar_src: String,
1584 grammar_path: PathBuf,
1585 conflicts: Option<(StateGraph<StorageT>, StateTable<StorageT>)>,
1586}
1587
1588impl<StorageT> CTParser<StorageT>
1589where
1590 StorageT: 'static + Debug + Hash + PrimInt + Unsigned,
1591 usize: AsPrimitive<StorageT>,
1592{
1593 pub fn regenerated(&self) -> bool {
1595 self.regenerated
1596 }
1597
1598 pub fn token_map(&self) -> &HashMap<String, StorageT> {
1601 &self.rule_ids
1602 }
1603
1604 #[allow(private_interfaces)]
1610 pub fn conflicts(
1611 &self,
1612 _: crate::unstable::UnstableApi,
1613 ) -> Option<(
1614 &YaccGrammar<StorageT>,
1615 &StateGraph<StorageT>,
1616 &StateTable<StorageT>,
1617 &Conflicts<StorageT>,
1618 )> {
1619 if let Some((sgraph, stable)) = &self.conflicts {
1620 return Some((
1621 &self.yacc_grammar,
1622 sgraph,
1623 stable,
1624 stable.conflicts().unwrap(),
1625 ));
1626 }
1627 None
1628 }
1629
1630 #[doc(hidden)]
1631 pub fn yacc_grammar(&self) -> &YaccGrammar<StorageT> {
1632 &self.yacc_grammar
1633 }
1634 #[doc(hidden)]
1635 pub fn grammar_src(&self) -> &str {
1636 &self.grammar_src
1637 }
1638 #[doc(hidden)]
1639 pub fn grammar_path(&self) -> &Path {
1640 self.grammar_path.as_path()
1641 }
1642}
1643
1644fn indent(indent: &str, s: &str) -> String {
1655 format!("{indent}{}\n", s.trim_end_matches('\n')).replace('\n', &format!("\n{}", indent))
1656}
1657
1658fn make_generics(parse_generics: Option<&str>) -> Result<Generics, Box<dyn Error>> {
1659 if let Some(parse_generics) = parse_generics {
1660 let tokens = str::parse::<TokenStream>(parse_generics)?;
1661 match syn::parse2(quote!(<'lexer, 'input: 'lexer, #tokens>)) {
1662 Ok(res) => Ok(res),
1663 Err(err) => Err(format!("unable to parse %parse-generics: {}", err).into()),
1664 }
1665 } else {
1666 Ok(parse_quote!(<'lexer, 'input: 'lexer>))
1667 }
1668}
1669
1670#[cfg(all(not(target_arch = "wasm32"), test))]
1672mod test {
1673 use std::{fs::File, io::Write, path::PathBuf};
1674
1675 use super::{CTConflictsError, CTParserBuilder};
1676 use crate::test_utils::TestLexerTypes;
1677 use cfgrammar::yacc::{YaccKind, YaccOriginalActionKind};
1678 use tempfile::TempDir;
1679
1680 #[test]
1681 fn test_conflicts() {
1682 let temp = TempDir::new().unwrap();
1683 let mut file_path = PathBuf::from(temp.as_ref());
1684 file_path.push("grm.y");
1685 let mut f = File::create(&file_path).unwrap();
1686 let _ = f.write_all(
1687 "%start A
1688%%
1689A : 'a' 'b' | B 'b';
1690B : 'a' | C;
1691C : 'a';"
1692 .as_bytes(),
1693 );
1694
1695 match CTParserBuilder::<TestLexerTypes>::new()
1696 .error_on_conflicts(false)
1697 .yacckind(YaccKind::Original(YaccOriginalActionKind::GenericParseTree))
1698 .grammar_path(file_path.to_str().unwrap())
1699 .output_path(file_path.with_extension("ignored"))
1700 .build()
1701 .unwrap()
1702 .conflicts(crate::unstable::UnstableApi)
1703 {
1704 Some((_, _, _, conflicts)) => {
1705 assert_eq!(conflicts.sr_len(), 1);
1706 assert_eq!(conflicts.rr_len(), 1);
1707 }
1708 None => panic!("Expected error data"),
1709 }
1710 }
1711
1712 #[test]
1713 fn test_conflicts_error() {
1714 let temp = TempDir::new().unwrap();
1715 let mut file_path = PathBuf::from(temp.as_ref());
1716 file_path.push("grm.y");
1717 let mut f = File::create(&file_path).unwrap();
1718 let _ = f.write_all(
1719 "%start A
1720%%
1721A : 'a' 'b' | B 'b';
1722B : 'a' | C;
1723C : 'a';"
1724 .as_bytes(),
1725 );
1726
1727 match CTParserBuilder::<TestLexerTypes>::new()
1728 .yacckind(YaccKind::Original(YaccOriginalActionKind::GenericParseTree))
1729 .grammar_path(file_path.to_str().unwrap())
1730 .output_path(file_path.with_extension("ignored"))
1731 .build()
1732 {
1733 Ok(_) => panic!("Expected error"),
1734 Err(e) => {
1735 let cs = e.downcast_ref::<CTConflictsError<u16>>();
1736 assert_eq!(cs.unwrap().stable.conflicts().unwrap().rr_len(), 1);
1737 assert_eq!(cs.unwrap().stable.conflicts().unwrap().sr_len(), 1);
1738 }
1739 }
1740 }
1741
1742 #[test]
1743 fn test_expect_error() {
1744 let temp = TempDir::new().unwrap();
1745 let mut file_path = PathBuf::from(temp.as_ref());
1746 file_path.push("grm.y");
1747 let mut f = File::create(&file_path).unwrap();
1748 let _ = f.write_all(
1749 "%start A
1750%expect 2
1751%%
1752A: 'a' 'b' | B 'b';
1753B: 'a';"
1754 .as_bytes(),
1755 );
1756
1757 match CTParserBuilder::<TestLexerTypes>::new()
1758 .yacckind(YaccKind::Original(YaccOriginalActionKind::GenericParseTree))
1759 .grammar_path(file_path.to_str().unwrap())
1760 .output_path(file_path.with_extension("ignored"))
1761 .build()
1762 {
1763 Ok(_) => panic!("Expected error"),
1764 Err(e) => {
1765 let cs = e.downcast_ref::<CTConflictsError<u16>>();
1766 assert_eq!(cs.unwrap().stable.conflicts().unwrap().rr_len(), 0);
1767 assert_eq!(cs.unwrap().stable.conflicts().unwrap().sr_len(), 1);
1768 }
1769 }
1770 }
1771
1772 #[test]
1773 fn test_expectrr_error() {
1774 let temp = TempDir::new().unwrap();
1775 let mut file_path = PathBuf::from(temp.as_ref());
1776 file_path.push("grm.y");
1777 let mut f = File::create(&file_path).unwrap();
1778 let _ = f.write_all(
1779 "%start A
1780%expect 1
1781%expect-rr 2
1782%%
1783A : 'a' 'b' | B 'b';
1784B : 'a' | C;
1785C : 'a';"
1786 .as_bytes(),
1787 );
1788
1789 match CTParserBuilder::<TestLexerTypes>::new()
1790 .yacckind(YaccKind::Original(YaccOriginalActionKind::GenericParseTree))
1791 .grammar_path(file_path.to_str().unwrap())
1792 .output_path(file_path.with_extension("ignored"))
1793 .build()
1794 {
1795 Ok(_) => panic!("Expected error"),
1796 Err(e) => {
1797 let cs = e.downcast_ref::<CTConflictsError<u16>>();
1798 assert_eq!(cs.unwrap().stable.conflicts().unwrap().rr_len(), 1);
1799 assert_eq!(cs.unwrap().stable.conflicts().unwrap().sr_len(), 1);
1800 }
1801 }
1802 }
1803
1804 #[cfg(test)]
1805 #[test]
1806 fn test_recoverer_header() -> Result<(), Box<dyn std::error::Error>> {
1807 use crate::RecoveryKind as RK;
1808 #[rustfmt::skip]
1809 let recovery_kinds = [
1810 (Some(RK::None), Some(RK::None), Some(RK::None)),
1813 (Some(RK::None), Some(RK::CPCTPlus), Some(RK::None)),
1814 (Some(RK::CPCTPlus), Some(RK::CPCTPlus), Some(RK::CPCTPlus)),
1815 (Some(RK::CPCTPlus), Some(RK::None), Some(RK::CPCTPlus)),
1816 (None, Some(RK::CPCTPlus), Some(RK::CPCTPlus)),
1817 (None, Some(RK::None), Some(RK::None)),
1818 (None, None, Some(RK::CPCTPlus)),
1819 (Some(RK::None), None, Some(RK::None)),
1820 (Some(RK::CPCTPlus), None, Some(RK::CPCTPlus)),
1821 ];
1822
1823 for (i, (builder_arg, header_arg, expected_rk)) in
1824 recovery_kinds.iter().cloned().enumerate()
1825 {
1826 let y_src = if let Some(header_arg) = header_arg {
1827 format!(
1828 "\
1829 %grmtools{{yacckind: Original(NoAction), recoverer: {}}} \
1830 %% \
1831 start: ; \
1832 ",
1833 match header_arg {
1834 RK::None => "RecoveryKind::None",
1835 RK::CPCTPlus => "RecoveryKind::CPCTPlus",
1836 }
1837 )
1838 } else {
1839 r#"
1840 %grmtools{yacckind: Original(NoAction)}
1841 %%
1842 Start: ;
1843 "#
1844 .to_string()
1845 };
1846 let out_dir = std::env::var("OUT_DIR").unwrap();
1847 let y_path = format!("{out_dir}/recoverykind_test_{i}.y");
1848 let y_out_path = format!("{y_path}.rs");
1849 std::fs::File::create(y_path.clone()).unwrap();
1850 std::fs::write(y_path.clone(), y_src).unwrap();
1851 let mut cp_builder = CTParserBuilder::<TestLexerTypes>::new();
1852 cp_builder = cp_builder
1853 .output_path(y_out_path.clone())
1854 .grammar_path(y_path.clone());
1855 cp_builder = if let Some(builder_arg) = builder_arg {
1856 cp_builder.recoverer(builder_arg)
1857 } else {
1858 cp_builder
1859 }
1860 .inspect_recoverer(Box::new(move |rk| {
1861 if matches!(
1862 (rk, expected_rk),
1863 (RK::None, Some(RK::None)) | (RK::CPCTPlus, Some(RK::CPCTPlus))
1864 ) {
1865 Ok(())
1866 } else {
1867 panic!("Unexpected recovery kind")
1868 }
1869 }));
1870 cp_builder.build()?;
1871 }
1872 Ok(())
1873 }
1874}