1use crate::token::{Tin, TIN_CM, TIN_LN, TIN_MAX, TIN_NR, TIN_SP, TIN_ST, TIN_TX, TIN_VL};
4use indexmap::IndexMap;
5use regex::Regex;
6use std::collections::HashMap;
7use std::fmt;
8use std::sync::Arc;
9
10use crate::context::Context;
11use crate::lexer::Lexer;
12use crate::rule::Rule;
13use crate::token::Token;
14
15type ConfigModifierCallback = dyn Fn(&mut Options, &Options) + Send + Sync;
16
17#[derive(Clone)]
18pub struct ConfigModifier {
19 callback: Arc<ConfigModifierCallback>,
20}
21
22impl ConfigModifier {
23 pub(crate) fn new(callback: impl Fn(&mut Options) + Send + Sync + 'static) -> Self {
24 Self {
25 callback: Arc::new(move |config, _options| callback(config)),
26 }
27 }
28
29 pub(crate) fn with_options(
30 callback: impl Fn(&mut Options, &Options) + Send + Sync + 'static,
31 ) -> Self {
32 Self {
33 callback: Arc::new(callback),
34 }
35 }
36
37 pub(crate) fn run(&self, config: &mut Options, options: &Options) {
38 (self.callback)(config, options);
39 }
40
41 pub(crate) fn same_callback(&self, other: &Self) -> bool {
42 Arc::ptr_eq(&self.callback, &other.callback)
43 }
44}
45
46impl fmt::Debug for ConfigModifier {
47 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
48 formatter.write_str("ConfigModifier(<function>)")
49 }
50}
51
52#[derive(Debug, Clone, PartialEq, Eq)]
53pub struct ColorOptions {
54 pub active: bool,
55 pub reset: String,
56 pub hi: String,
57 pub lo: String,
58 pub line: String,
59}
60
61impl ColorOptions {
62 pub(crate) fn codes(&self) -> (&str, &str, &str, &str) {
63 if self.active {
64 (&self.hi, &self.lo, &self.line, &self.reset)
65 } else {
66 ("", "", "", "")
67 }
68 }
69}
70
71impl Default for ColorOptions {
72 fn default() -> Self {
73 Self {
74 active: true,
75 reset: "\x1b[0m".into(),
76 hi: "\x1b[91m".into(),
77 lo: "\x1b[2m".into(),
78 line: "\x1b[34m".into(),
79 }
80 }
81}
82
83#[derive(Debug, Clone, PartialEq, Eq)]
84pub struct ErrorSuffixContext {
85 pub code: String,
86 pub source: String,
87 pub message: String,
88 pub hint: String,
89 pub pos: usize,
90 pub row: usize,
91 pub col: usize,
92 pub name: String,
93 pub tag: String,
94 pub rule: String,
95 pub rule_state: String,
96 pub token: String,
97 pub why: String,
98 pub plugins: Vec<String>,
99 pub color: ColorOptions,
100}
101
102pub type ErrorSuffixCallback = Arc<dyn Fn(&ErrorSuffixContext) -> String + Send + Sync>;
103
104#[derive(Clone)]
105pub enum ErrorSuffix {
106 Standard,
107 Disabled,
108 Text(String),
109 Callback(ErrorSuffixCallback),
110}
111
112impl fmt::Debug for ErrorSuffix {
113 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
114 match self {
115 Self::Standard => formatter.write_str("Standard"),
116 Self::Disabled => formatter.write_str("Disabled"),
117 Self::Text(text) => formatter.debug_tuple("Text").field(text).finish(),
118 Self::Callback(_) => formatter.write_str("Callback(<function>)"),
119 }
120 }
121}
122
123impl PartialEq for ErrorSuffix {
124 fn eq(&self, other: &Self) -> bool {
125 match (self, other) {
126 (Self::Standard, Self::Standard) | (Self::Disabled, Self::Disabled) => true,
127 (Self::Text(left), Self::Text(right)) => left == right,
128 (Self::Callback(left), Self::Callback(right)) => Arc::ptr_eq(left, right),
129 _ => false,
130 }
131 }
132}
133
134impl Eq for ErrorSuffix {}
135
136#[derive(Debug, Clone, PartialEq, Eq)]
137pub struct ErrMsgOptions {
138 pub name: String,
139 pub suffix: ErrorSuffix,
140 pub link: String,
141}
142
143impl Default for ErrMsgOptions {
144 fn default() -> Self {
145 Self {
146 name: "tabnas".into(),
147 suffix: ErrorSuffix::Standard,
148 link: String::new(),
149 }
150 }
151}
152
153#[derive(Debug, Clone, PartialEq)]
154pub struct LexCheckToken {
155 pub name: String,
156 pub tin: Tin,
157 pub source: String,
158 pub value: crate::Value,
159}
160
161impl LexCheckToken {
162 pub fn new(
163 name: impl Into<String>,
164 tin: Tin,
165 source: impl Into<String>,
166 value: crate::Value,
167 ) -> Self {
168 Self {
169 name: name.into(),
170 tin,
171 source: source.into(),
172 value,
173 }
174 }
175
176 pub fn named(name: impl Into<String>, source: impl Into<String>, value: crate::Value) -> Self {
179 let name = name.into();
180 Self {
181 name: if name.starts_with('#') {
182 name
183 } else {
184 format!("#{name}")
185 },
186 tin: -1,
187 source: source.into(),
188 value,
189 }
190 }
191}
192
193#[derive(Debug, Clone, PartialEq)]
195pub enum LexCheckResult {
196 Continue,
198 Skip,
200 Token(Box<LexCheckToken>),
202 NativeToken(Box<Token>),
205}
206
207impl LexCheckResult {
208 pub fn token(token: LexCheckToken) -> Self {
209 Self::Token(Box::new(token))
210 }
211
212 pub fn native_token(token: Token) -> Self {
213 Self::NativeToken(Box::new(token))
214 }
215}
216
217pub type ImperativeLexCheck =
218 Arc<dyn for<'source> Fn(&mut Lexer<'source>) -> LexCheckResult + Send + Sync>;
219
220#[derive(Clone)]
221pub struct LexCheck {
222 callback: Option<Arc<LexCheckEffect>>,
223 imperative: Option<ImperativeLexCheck>,
224}
225
226type LexCheckEffect = dyn Fn(&str) -> LexCheckResult + Send + Sync;
227
228impl LexCheck {
229 pub(crate) fn new(callback: impl Fn(&str) -> LexCheckResult + Send + Sync + 'static) -> Self {
230 Self {
231 callback: Some(Arc::new(callback)),
232 imperative: None,
233 }
234 }
235
236 pub(crate) fn new_imperative(
237 callback: impl for<'source> Fn(&mut Lexer<'source>) -> LexCheckResult + Send + Sync + 'static,
238 ) -> Self {
239 Self {
240 callback: None,
241 imperative: Some(Arc::new(callback)),
242 }
243 }
244
245 pub(crate) fn run(&self, source: &str) -> Option<LexCheckResult> {
246 self.callback.as_ref().map(|callback| callback(source))
247 }
248
249 pub(crate) fn run_imperative(&self, lexer: &mut Lexer<'_>) -> Option<LexCheckResult> {
250 self.imperative.as_ref().map(|callback| callback(lexer))
251 }
252
253 pub(crate) fn same_callback(&self, other: &Self) -> bool {
254 match (
255 &self.callback,
256 &self.imperative,
257 &other.callback,
258 &other.imperative,
259 ) {
260 (Some(left), None, Some(right), None) => Arc::ptr_eq(left, right),
261 (None, Some(left), None, Some(right)) => Arc::ptr_eq(left, right),
262 _ => false,
263 }
264 }
265}
266
267impl fmt::Debug for LexCheck {
268 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
269 formatter.write_str(if self.imperative.is_some() {
270 "LexCheck(<live lexer function>)"
271 } else {
272 "LexCheck(<effect function>)"
273 })
274 }
275}
276
277#[derive(Debug, Clone)]
278pub struct FixedToken {
279 pub name: String,
280 pub tin: Tin,
281 pub source: String,
282}
283
284#[derive(Debug, Clone)]
285pub struct FixedOptions {
286 pub lex: bool,
287 pub tokens: IndexMap<String, FixedToken>,
288 pub check: Option<LexCheck>,
289}
290
291impl Default for FixedOptions {
292 fn default() -> Self {
293 let mut tokens = IndexMap::new();
294 for (name, tin, source) in [
295 ("#OB", crate::token::TIN_OB, "{"),
296 ("#CB", crate::token::TIN_CB, "}"),
297 ("#OS", crate::token::TIN_OS, "["),
298 ("#CS", crate::token::TIN_CS, "]"),
299 ("#CL", crate::token::TIN_CL, ":"),
300 ("#CA", crate::token::TIN_CA, ","),
301 ] {
302 tokens.insert(
303 name.to_string(),
304 FixedToken {
305 name: name.to_string(),
306 tin,
307 source: source.to_string(),
308 },
309 );
310 }
311 Self {
312 lex: true,
313 tokens,
314 check: None,
315 }
316 }
317}
318
319pub type ValueTextModifier = Arc<dyn Fn(crate::Value) -> crate::Value + Send + Sync>;
320pub type ImperativeTextModifier = Arc<
321 dyn for<'source> Fn(
322 crate::Value,
323 &mut Lexer<'source>,
324 &mut Rule,
325 &mut Context,
326 &Options,
327 ) -> crate::Value
328 + Send
329 + Sync,
330>;
331
332#[derive(Clone)]
338pub enum TextModifier {
339 Value(ValueTextModifier),
340 Imperative(ImperativeTextModifier),
341}
342
343impl TextModifier {
344 pub(crate) fn new(
345 modifier: impl Fn(crate::Value) -> crate::Value + Send + Sync + 'static,
346 ) -> Self {
347 Self::Value(Arc::new(modifier))
348 }
349
350 pub(crate) fn new_imperative(
351 modifier: impl for<'source> Fn(
352 crate::Value,
353 &mut Lexer<'source>,
354 &mut Rule,
355 &mut Context,
356 &Options,
357 ) -> crate::Value
358 + Send
359 + Sync
360 + 'static,
361 ) -> Self {
362 Self::Imperative(Arc::new(modifier))
363 }
364
365 pub(crate) fn run(
366 &self,
367 value: crate::Value,
368 lexer: &mut Lexer<'_>,
369 rule: &mut Rule,
370 context: &mut Context,
371 options: &Options,
372 ) -> crate::Value {
373 match self {
374 Self::Value(modifier) => modifier(value),
375 Self::Imperative(modifier) => modifier(value, lexer, rule, context, options),
376 }
377 }
378
379 pub(crate) fn same_callback(&self, other: &Self) -> bool {
380 match (self, other) {
381 (Self::Value(left), Self::Value(right)) => Arc::ptr_eq(left, right),
382 (Self::Imperative(left), Self::Imperative(right)) => Arc::ptr_eq(left, right),
383 _ => false,
384 }
385 }
386}
387
388impl fmt::Debug for TextModifier {
389 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
390 formatter.write_str(match self {
391 Self::Value(_) => "TextModifier::Value(<function>)",
392 Self::Imperative(_) => "TextModifier::Imperative(<function>)",
393 })
394 }
395}
396
397#[derive(Clone)]
398pub struct TextOptions {
399 pub lex: bool,
400 pub modify: Vec<TextModifier>,
401 pub check: Option<LexCheck>,
402}
403
404impl fmt::Debug for TextOptions {
405 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
406 formatter
407 .debug_struct("TextOptions")
408 .field("lex", &self.lex)
409 .field("modify", &self.modify.len())
410 .field("check", &self.check)
411 .finish()
412 }
413}
414
415impl Default for TextOptions {
416 fn default() -> Self {
417 TextOptions {
418 lex: true,
419 modify: Vec::new(),
420 check: None,
421 }
422 }
423}
424
425#[derive(Debug, Clone)]
426pub struct SpaceOptions {
427 pub lex: bool,
428 pub chars: String,
429 pub check: Option<LexCheck>,
430}
431
432impl Default for SpaceOptions {
433 fn default() -> Self {
434 Self {
435 lex: true,
436 chars: " \t".into(),
437 check: None,
438 }
439 }
440}
441
442#[derive(Debug, Clone)]
443pub struct NumberOptions {
444 pub lex: bool,
445 pub hex: bool,
446 pub oct: bool,
447 pub bin: bool,
448 pub sep: Option<String>,
449 pub exclude: Option<String>, pub check: Option<LexCheck>,
451}
452
453impl Default for NumberOptions {
454 fn default() -> Self {
455 NumberOptions {
456 lex: true,
457 hex: true,
458 oct: true,
459 bin: true,
460 sep: Some("_".to_string()),
461 exclude: None,
462 check: None,
463 }
464 }
465}
466
467#[derive(Debug, Clone)]
468pub struct StringOptions {
469 pub lex: bool,
470 pub chars: String,
471 pub multi_chars: String,
472 pub escape_char: char,
473 pub escape: HashMap<char, String>,
474 pub replace: HashMap<char, String>,
475 pub allow_unknown: bool,
476 pub escape_strict: bool,
477 pub allow_control: bool,
478 pub abandon: bool,
479 pub check: Option<LexCheck>,
480}
481
482impl Default for StringOptions {
483 fn default() -> Self {
484 let escape = [
485 ('b', "\u{0008}"),
486 ('f', "\u{000c}"),
487 ('n', "\n"),
488 ('r', "\r"),
489 ('t', "\t"),
490 ('v', "\u{000b}"),
491 ('"', "\""),
492 ('\'', "'"),
493 ('`', "`"),
494 ('\\', "\\"),
495 ('/', "/"),
496 ]
497 .into_iter()
498 .map(|(key, value)| (key, value.into()))
499 .collect();
500 StringOptions {
501 lex: true,
502 chars: "\"'`".to_string(),
503 multi_chars: "`".to_string(),
504 escape_char: '\\',
505 escape,
506 replace: HashMap::new(),
507 allow_unknown: true,
508 escape_strict: false,
509 allow_control: false,
510 abandon: false,
511 check: None,
512 }
513 }
514}
515
516#[derive(Debug, Clone)]
517pub struct LineOptions {
518 pub lex: bool,
519 pub chars: String,
520 pub row_chars: String,
521 pub single: bool,
522 pub fixed: Vec<char>,
525 pub check: Option<LexCheck>,
526}
527
528impl Default for LineOptions {
529 fn default() -> Self {
530 Self {
531 lex: true,
532 chars: "\r\n".into(),
533 row_chars: "\n".into(),
534 single: false,
535 fixed: Vec::new(),
536 check: None,
537 }
538 }
539}
540
541#[derive(Debug, Clone)]
542pub struct CommentDef {
543 pub line: bool,
544 pub start: String,
545 pub end: String,
546 pub lex: bool,
547 pub suffixes: Vec<String>,
548 pub suffix_matcher: Option<CommentSuffixMatcher>,
549 pub eat_line: bool,
550}
551
552#[derive(Clone)]
553pub struct CommentSuffixMatcher {
554 callback: Option<Arc<CommentSuffixCallback>>,
555 imperative: Option<ImperativeCommentSuffixMatcher>,
556}
557
558type CommentSuffixCallback = dyn Fn(&str) -> Option<String> + Send + Sync;
559pub type ImperativeCommentSuffixMatcher =
560 Arc<dyn for<'source> Fn(&mut Lexer<'source>) -> Option<Token> + Send + Sync>;
561
562impl CommentSuffixMatcher {
563 pub(crate) fn new(callback: impl Fn(&str) -> Option<String> + Send + Sync + 'static) -> Self {
564 Self {
565 callback: Some(Arc::new(callback)),
566 imperative: None,
567 }
568 }
569
570 pub(crate) fn new_imperative(
571 callback: impl for<'source> Fn(&mut Lexer<'source>) -> Option<Token> + Send + Sync + 'static,
572 ) -> Self {
573 Self {
574 callback: None,
575 imperative: Some(Arc::new(callback)),
576 }
577 }
578
579 pub(crate) fn run(&self, source: &str) -> Option<String> {
580 self.callback.as_ref().and_then(|callback| callback(source))
581 }
582
583 pub(crate) fn run_imperative(&self, lexer: &mut Lexer<'_>) -> Option<Token> {
584 self.imperative
585 .as_ref()
586 .and_then(|callback| callback(lexer))
587 }
588
589 pub(crate) fn same_callback(&self, other: &Self) -> bool {
590 match (
591 &self.callback,
592 &self.imperative,
593 &other.callback,
594 &other.imperative,
595 ) {
596 (Some(left), None, Some(right), None) => Arc::ptr_eq(left, right),
597 (None, Some(left), None, Some(right)) => Arc::ptr_eq(left, right),
598 _ => false,
599 }
600 }
601}
602
603impl fmt::Debug for CommentSuffixMatcher {
604 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
605 formatter.write_str(if self.imperative.is_some() {
606 "CommentSuffixMatcher(<live lexer function>)"
607 } else {
608 "CommentSuffixMatcher(<effect function>)"
609 })
610 }
611}
612
613#[derive(Debug, Clone)]
614pub struct CommentOptions {
615 pub lex: bool,
616 pub definitions: IndexMap<String, CommentDef>,
617 pub check: Option<LexCheck>,
618}
619
620#[derive(Clone)]
621pub struct ValueDef {
622 pub val: Option<crate::Value>,
623 pub matcher: Option<Regex>,
624 pub transform: Option<ValueTransform>,
625 pub consume: bool,
626}
627
628pub type ValueTransform = Arc<dyn Fn(&[String]) -> crate::Value + Send + Sync>;
629
630impl fmt::Debug for ValueDef {
631 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
632 formatter
633 .debug_struct("ValueDef")
634 .field("val", &self.val)
635 .field("matcher", &self.matcher)
636 .field("transform", &self.transform.as_ref().map(|_| "<function>"))
637 .field("consume", &self.consume)
638 .finish()
639 }
640}
641
642#[derive(Debug, Clone)]
643pub struct ValueOptions {
644 pub lex: bool,
645 pub definitions: IndexMap<String, ValueDef>,
646}
647
648impl Default for ValueOptions {
649 fn default() -> Self {
650 let mut definitions = IndexMap::new();
651 for (source, value) in [
652 ("true", crate::Value::Bool(true)),
653 ("false", crate::Value::Bool(false)),
654 ("null", crate::Value::Null),
655 ] {
656 definitions.insert(
657 source.into(),
658 ValueDef {
659 val: Some(value),
660 matcher: None,
661 transform: None,
662 consume: false,
663 },
664 );
665 }
666 Self {
667 lex: true,
668 definitions,
669 }
670 }
671}
672
673impl Default for CommentOptions {
674 fn default() -> Self {
675 let mut definitions = IndexMap::new();
676 for (name, line, start, end) in [
677 ("hash", true, "#", ""),
678 ("slash", true, "//", ""),
679 ("multi", false, "/*", "*/"),
680 ] {
681 definitions.insert(
682 name.into(),
683 CommentDef {
684 line,
685 start: start.into(),
686 end: end.into(),
687 lex: true,
688 suffixes: Vec::new(),
689 suffix_matcher: None,
690 eat_line: false,
691 },
692 );
693 }
694 CommentOptions {
695 lex: true,
696 definitions,
697 check: None,
698 }
699 }
700}
701
702#[derive(Debug, Clone)]
703pub struct SafeOptions {
704 pub key: bool,
705}
706
707impl Default for SafeOptions {
708 fn default() -> Self {
709 Self { key: true }
710 }
711}
712
713pub type MapMerge =
714 Arc<dyn Fn(crate::Value, crate::Value, &mut Rule, &mut Context) -> crate::Value + Send + Sync>;
715
716#[derive(Clone)]
717pub struct MapOptions {
718 pub extend: bool,
719 pub merge: Option<MapMerge>,
720 pub child: bool,
721 pub ordered: bool,
726}
727
728impl fmt::Debug for MapOptions {
729 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
730 formatter
731 .debug_struct("MapOptions")
732 .field("extend", &self.extend)
733 .field("merge", &self.merge.as_ref().map(|_| "<function>"))
734 .field("child", &self.child)
735 .field("ordered", &self.ordered)
736 .finish()
737 }
738}
739
740impl Default for MapOptions {
741 fn default() -> Self {
742 MapOptions {
743 extend: true,
744 merge: None,
745 child: false,
746 ordered: false,
747 }
748 }
749}
750
751#[derive(Debug, Clone)]
752pub struct ListOptions {
753 pub property: bool,
754 pub pair: bool,
755 pub child: bool,
756}
757
758impl Default for ListOptions {
759 fn default() -> Self {
760 Self {
761 property: true,
762 pair: false,
763 child: false,
764 }
765 }
766}
767
768#[derive(Debug, Clone)]
773pub struct InfoOptions {
774 pub map: bool,
775 pub list: bool,
776 pub text: bool,
777 pub marker: String,
778}
779
780impl Default for InfoOptions {
781 fn default() -> Self {
782 Self {
783 map: false,
784 list: false,
785 text: false,
786 marker: "__info__".into(),
787 }
788 }
789}
790
791#[derive(Debug, Clone)]
792pub struct LexOptions {
793 pub empty: bool,
794 pub empty_result: crate::Value,
795 pub relex: bool,
796 pub matchers: IndexMap<String, LexMatcher>,
797}
798
799#[derive(Debug, Clone)]
800pub struct RewindOptions {
801 pub history: Option<usize>,
807}
808
809impl Default for RewindOptions {
810 fn default() -> Self {
811 Self { history: Some(64) }
812 }
813}
814
815impl Default for LexOptions {
816 fn default() -> Self {
817 LexOptions {
818 empty: true,
819 empty_result: crate::Value::Undefined,
820 relex: false,
821 matchers: IndexMap::new(),
822 }
823 }
824}
825
826#[derive(Debug, Clone, Default)]
827pub struct ResultOptions {
828 pub fail: Vec<crate::Value>,
829}
830
831pub const MAX_RULE_HISTORY: usize = 16;
843
844pub const DEFAULT_RULE_HISTORY: usize = 3;
847
848pub(crate) fn effective_rule_history(history: Option<usize>) -> Option<usize> {
853 history.map(|history| history.clamp(1, MAX_RULE_HISTORY))
854}
855
856#[derive(Debug, Clone)]
857pub struct RuleOptions {
858 pub finish: bool,
865 pub maxmul: usize,
866 pub include: String,
867 pub exclude: String,
868 pub start: String,
869 pub history: Option<usize>,
877}
878
879#[derive(Debug, Clone)]
880pub struct MatchToken {
881 pub name: String,
882 pub tin: Tin,
883 pub matcher: MatchTokenMatcher,
884 pub eager: bool,
885}
886
887#[derive(Clone)]
888pub struct MatchValue {
889 pub name: String,
890 pub matcher: MatchTokenMatcher,
891 pub val: Option<crate::Value>,
892 pub transform: Option<ValueTransform>,
893}
894
895impl fmt::Debug for MatchValue {
896 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
897 formatter
898 .debug_struct("MatchValue")
899 .field("name", &self.name)
900 .field("matcher", &self.matcher)
901 .field("val", &self.val)
902 .field("transform", &self.transform.as_ref().map(|_| "<function>"))
903 .finish()
904 }
905}
906
907#[derive(Debug, Clone, PartialEq)]
908pub struct MatchTokenResult {
909 pub source: String,
911 pub value: crate::Value,
913}
914
915impl MatchTokenResult {
916 pub fn new(source: impl Into<String>, value: crate::Value) -> Self {
917 Self {
918 source: source.into(),
919 value,
920 }
921 }
922}
923
924pub type MatchTokenCallback = Arc<dyn Fn(&str) -> Option<MatchTokenResult> + Send + Sync>;
925
926pub type LexMatcherCallback = Arc<dyn Fn(&str) -> Option<LexCheckToken> + Send + Sync>;
930
931pub type ImperativeLexMatcher = Arc<
934 dyn for<'source> Fn(&mut Lexer<'source>, &mut Rule, &mut Context) -> Option<Token>
935 + Send
936 + Sync,
937>;
938
939pub type LexMatcherFactory = Arc<dyn Fn(&Options) -> Option<ImperativeLexMatcher> + Send + Sync>;
942
943#[derive(Clone)]
944pub struct LexMatcher {
945 pub name: String,
946 pub order: f64,
947 pub matcher: Option<LexMatcherCallback>,
948 pub imperative: Option<ImperativeLexMatcher>,
949 pub factory: Option<LexMatcherFactory>,
950}
951
952impl fmt::Debug for LexMatcher {
953 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
954 formatter
955 .debug_struct("LexMatcher")
956 .field("name", &self.name)
957 .field("order", &self.order)
958 .field("matcher", &self.matcher.as_ref().map(|_| "<function>"))
959 .field(
960 "imperative",
961 &self.imperative.as_ref().map(|_| "<function>"),
962 )
963 .field("factory", &self.factory.as_ref().map(|_| "<function>"))
964 .finish()
965 }
966}
967
968#[derive(Clone)]
969pub enum MatchTokenMatcher {
970 Regex(Regex),
971 Callback(MatchTokenCallback),
972}
973
974impl fmt::Debug for MatchTokenMatcher {
975 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
976 match self {
977 Self::Regex(regex) => formatter.debug_tuple("Regex").field(regex).finish(),
978 Self::Callback(_) => formatter.write_str("Callback(<function>)"),
979 }
980 }
981}
982
983impl Default for RuleOptions {
984 fn default() -> Self {
985 RuleOptions {
986 finish: true,
987 maxmul: 3,
988 include: String::new(),
989 exclude: String::new(),
990 start: "val".to_string(),
991 history: Some(DEFAULT_RULE_HISTORY),
992 }
993 }
994}
995
996pub type BudgetCheck = Arc<dyn Fn(&Context) -> bool + Send + Sync>;
997
998#[derive(Clone, Default)]
999pub struct BudgetOptions {
1000 pub check_every_n: usize,
1001 pub on_check: Option<BudgetCheck>,
1002}
1003
1004impl fmt::Debug for BudgetOptions {
1005 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1006 formatter
1007 .debug_struct("BudgetOptions")
1008 .field("check_every_n", &self.check_every_n)
1009 .field("on_check", &self.on_check.as_ref().map(|_| "<callback>"))
1010 .finish()
1011 }
1012}
1013
1014#[derive(Debug, Clone)]
1015pub struct RecoverOptions {
1016 pub enabled: bool,
1017 pub sync_groups: Vec<String>,
1018 pub sync_tokens: Vec<String>,
1019 pub pop_until_valid: bool,
1020 pub max_skip: usize,
1021 pub max_recoveries: usize,
1022 pub suppress: usize,
1023}
1024
1025impl Default for RecoverOptions {
1026 fn default() -> Self {
1027 Self {
1028 enabled: false,
1029 sync_groups: vec!["close".into(), "comma".into(), "end".into()],
1030 sync_tokens: Vec::new(),
1031 pop_until_valid: true,
1032 max_skip: 64,
1033 max_recoveries: 32,
1034 suppress: 4,
1035 }
1036 }
1037}
1038
1039pub type ContextParsePrepare = Arc<dyn Fn(&mut Context) + Send + Sync>;
1040pub type ParsePrepareWithInstance =
1041 Arc<dyn Fn(&crate::Tabnas, &mut Context, &crate::Value) + Send + Sync>;
1042
1043#[derive(Clone)]
1047pub enum ParsePrepare {
1048 Context(ContextParsePrepare),
1049 WithInstance(ParsePrepareWithInstance),
1050}
1051
1052impl ParsePrepare {
1053 pub(crate) fn same_callback(&self, other: &Self) -> bool {
1054 match (self, other) {
1055 (Self::Context(left), Self::Context(right)) => Arc::ptr_eq(left, right),
1056 (Self::WithInstance(left), Self::WithInstance(right)) => Arc::ptr_eq(left, right),
1057 _ => false,
1058 }
1059 }
1060
1061 pub(crate) fn run(
1062 &self,
1063 owner: Option<&crate::Tabnas>,
1064 context: &mut Context,
1065 meta: &crate::Value,
1066 ) -> Result<(), &'static str> {
1067 match self {
1068 Self::Context(callback) => {
1069 callback(context);
1070 Ok(())
1071 }
1072 Self::WithInstance(callback) => {
1073 let owner = owner.ok_or(
1074 "parse.prepare requires an owning Tabnas instance; call Tabnas::parse",
1075 )?;
1076 callback(owner, context, meta);
1077 Ok(())
1078 }
1079 }
1080 }
1081}
1082
1083impl fmt::Debug for ParsePrepare {
1084 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1085 match self {
1086 Self::Context(_) => formatter.write_str("ParsePrepare::Context(<function>)"),
1087 Self::WithInstance(_) => formatter.write_str("ParsePrepare::WithInstance(<function>)"),
1088 }
1089 }
1090}
1091
1092pub type ParserStart =
1093 Arc<dyn Fn(&str) -> Result<crate::Value, Box<crate::TabnasError>> + Send + Sync>;
1094pub type ParserStartWithInstance = Arc<
1095 dyn Fn(&str, &crate::Tabnas, &crate::Value) -> Result<crate::Value, Box<crate::TabnasError>>
1096 + Send
1097 + Sync,
1098>;
1099pub type ParserStartWithContext = Arc<
1100 dyn Fn(
1101 &str,
1102 &crate::Tabnas,
1103 &crate::Value,
1104 Option<&crate::ContextSeed>,
1105 ) -> Result<crate::Value, Box<crate::TabnasError>>
1106 + Send
1107 + Sync,
1108>;
1109
1110pub type DebugOutput = Arc<dyn Fn(&str) + Send + Sync>;
1111pub type DebugSourceFormatter = Arc<dyn Fn(&crate::Value) -> String + Send + Sync>;
1112
1113#[derive(Clone, Default)]
1114pub struct DebugPrintOptions {
1115 pub config: bool,
1116 pub source: Option<DebugSourceFormatter>,
1117}
1118
1119impl fmt::Debug for DebugPrintOptions {
1120 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1121 formatter
1122 .debug_struct("DebugPrintOptions")
1123 .field("config", &self.config)
1124 .field("source", &self.source.as_ref().map(|_| "<callback>"))
1125 .finish()
1126 }
1127}
1128
1129#[derive(Clone)]
1130pub struct DebugOptions {
1131 pub maxlen: usize,
1134 pub print: DebugPrintOptions,
1135 pub output: Option<DebugOutput>,
1138}
1139
1140impl Default for DebugOptions {
1141 fn default() -> Self {
1142 Self {
1143 maxlen: 99,
1144 print: DebugPrintOptions::default(),
1145 output: None,
1146 }
1147 }
1148}
1149
1150impl fmt::Debug for DebugOptions {
1151 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1152 formatter
1153 .debug_struct("DebugOptions")
1154 .field("maxlen", &self.maxlen)
1155 .field("print", &self.print)
1156 .field("output", &self.output.as_ref().map(|_| "<callback>"))
1157 .finish()
1158 }
1159}
1160
1161impl DebugOptions {
1162 pub fn format_source(&self, value: &crate::Value) -> String {
1163 if let Some(formatter) = &self.print.source {
1164 return formatter(value);
1165 }
1166 let rendered = match value {
1167 crate::Value::Undefined | crate::Value::Null => String::new(),
1168 value => serde_json::to_string(&value.to_json()).unwrap_or_else(|_| value.to_string()),
1169 };
1170 let mut chars = rendered.chars();
1171 let prefix = chars.by_ref().take(self.maxlen).collect::<String>();
1172 if chars.next().is_some() {
1173 format!("{prefix}...")
1174 } else {
1175 prefix
1176 }
1177 }
1178
1179 pub fn write(&self, message: &str) {
1180 if let Some(output) = &self.output {
1181 let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| output(message)));
1185 } else {
1186 eprintln!("{message}");
1187 }
1188 }
1189}
1190
1191#[derive(Clone, Default)]
1192pub struct ParserOptions {
1193 pub start: Option<ParserStart>,
1194 pub start_with_instance: Option<ParserStartWithInstance>,
1195 pub start_with_context: Option<ParserStartWithContext>,
1196}
1197
1198impl fmt::Debug for ParserOptions {
1199 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1200 formatter
1201 .debug_struct("ParserOptions")
1202 .field("start", &self.start.as_ref().map(|_| "<callback>"))
1203 .field(
1204 "start_with_instance",
1205 &self.start_with_instance.as_ref().map(|_| "<callback>"),
1206 )
1207 .field(
1208 "start_with_context",
1209 &self.start_with_context.as_ref().map(|_| "<callback>"),
1210 )
1211 .finish()
1212 }
1213}
1214
1215#[derive(Clone, Default)]
1216pub struct ParseOptions {
1217 pub prepare: Vec<ParsePrepare>,
1218 pub named_prepare: IndexMap<String, ParsePrepare>,
1219 pub budget: BudgetOptions,
1220 pub recover: RecoverOptions,
1221}
1222
1223impl fmt::Debug for ParseOptions {
1224 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1225 formatter
1226 .debug_struct("ParseOptions")
1227 .field("prepare", &self.prepare.len())
1228 .field("named_prepare", &self.named_prepare.len())
1229 .field("budget", &self.budget)
1230 .field("recover", &self.recover)
1231 .finish()
1232 }
1233}
1234
1235#[derive(Debug, Clone)]
1236pub struct Options {
1237 pub safe: SafeOptions,
1238 pub fixed: FixedOptions,
1239 pub space: SpaceOptions,
1240 pub text: TextOptions,
1241 pub number: NumberOptions,
1242 pub string: StringOptions,
1243 pub line: LineOptions,
1244 pub comment: CommentOptions,
1245 pub value: ValueOptions,
1246 pub ender: Vec<String>,
1247 pub map: MapOptions,
1248 pub list: ListOptions,
1249 pub info: InfoOptions,
1250 pub lex: LexOptions,
1251 pub rewind: RewindOptions,
1252 pub rule: RuleOptions,
1253 pub result: ResultOptions,
1254 pub parse: ParseOptions,
1255 pub parser: ParserOptions,
1256 pub debug: DebugOptions,
1257 pub plugin: IndexMap<String, crate::Value>,
1259 pub token_set: HashMap<String, Vec<Tin>>,
1260 pub tokens: IndexMap<String, Tin>,
1263 pub match_lex: bool,
1265 pub match_check: Option<LexCheck>,
1266 pub match_tokens: IndexMap<String, MatchToken>,
1267 pub match_values: IndexMap<String, MatchValue>,
1268 pub error: HashMap<String, String>,
1269 pub hint: HashMap<String, String>,
1270 pub errmsg: ErrMsgOptions,
1271 pub color: ColorOptions,
1272 pub config_modify: IndexMap<String, ConfigModifier>,
1273 pub tag: String,
1274}
1275
1276impl Default for Options {
1277 fn default() -> Self {
1278 let mut token_set = HashMap::new();
1279 token_set.insert("IGNORE".to_string(), vec![TIN_SP, TIN_LN, TIN_CM]);
1280 token_set.insert("VAL".to_string(), vec![TIN_TX, TIN_NR, TIN_ST, TIN_VL]);
1281 token_set.insert("KEY".to_string(), vec![TIN_TX, TIN_NR, TIN_ST, TIN_VL]);
1282
1283 Options {
1284 safe: SafeOptions::default(),
1285 fixed: FixedOptions::default(),
1286 space: SpaceOptions::default(),
1287 text: TextOptions::default(),
1288 number: NumberOptions::default(),
1289 string: StringOptions::default(),
1290 line: LineOptions::default(),
1291 comment: CommentOptions::default(),
1292 value: ValueOptions::default(),
1293 ender: Vec::new(),
1294 map: MapOptions::default(),
1295 list: ListOptions::default(),
1296 info: InfoOptions::default(),
1297 lex: LexOptions::default(),
1298 rewind: RewindOptions::default(),
1299 rule: RuleOptions::default(),
1300 result: ResultOptions::default(),
1301 parse: ParseOptions::default(),
1302 parser: ParserOptions::default(),
1303 debug: DebugOptions::default(),
1304 plugin: IndexMap::new(),
1305 token_set,
1306 tokens: IndexMap::new(),
1307 match_lex: true,
1308 match_check: None,
1309 match_tokens: IndexMap::new(),
1310 match_values: IndexMap::new(),
1311 error: crate::error::default_error_messages(),
1312 hint: crate::error::default_error_hints(),
1313 errmsg: ErrMsgOptions::default(),
1314 color: ColorOptions::default(),
1315 config_modify: IndexMap::new(),
1316 tag: "-".to_string(),
1317 }
1318 }
1319}
1320
1321impl Options {
1322 pub(crate) fn validate_comment_definitions(&self) -> Result<(), String> {
1328 for (name, definition) in &self.comment.definitions {
1329 if !definition.line && definition.end.is_empty() {
1330 return Err(format!(
1331 "options.comment.def.{name}.end: block comments require a non-empty end marker"
1332 ));
1333 }
1334 }
1335 Ok(())
1336 }
1337
1338 pub fn refresh_configuration(&mut self) -> Result<(), String> {
1342 let raw_options = self.clone();
1343 let modifiers: Vec<_> = self
1344 .config_modify
1345 .iter()
1346 .map(|(name, modifier)| (name.clone(), modifier.clone()))
1347 .collect();
1348 for (name, modifier) in modifiers {
1349 std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
1350 modifier.run(self, &raw_options)
1351 }))
1352 .map_err(|_| format!("config modifier {name} panicked"))?;
1353 }
1354 self.validate_comment_definitions()?;
1355 self.refresh_lex_matchers()
1356 }
1357
1358 pub fn refresh_lex_matchers(&mut self) -> Result<(), String> {
1361 let factories: Vec<_> = self
1362 .lex
1363 .matchers
1364 .iter()
1365 .filter_map(|(name, matcher)| {
1366 matcher
1367 .factory
1368 .as_ref()
1369 .map(|factory| (name.clone(), factory.clone()))
1370 })
1371 .collect();
1372 for (name, factory) in factories {
1373 let matcher = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| factory(self)))
1374 .map_err(|_| format!("lexer matcher factory {name} panicked"))?;
1375 if let Some(entry) = self.lex.matchers.get_mut(&name) {
1376 entry.imperative = matcher;
1377 }
1378 }
1379 Ok(())
1380 }
1381
1382 pub fn empty() -> Self {
1386 let mut options = Self::default();
1387 options.fixed.lex = false;
1388 options.fixed.tokens.clear();
1389 options.space.lex = false;
1390 options.text.lex = false;
1391 options.number.lex = false;
1392 options.string.lex = false;
1393 options.line.lex = false;
1394 options.comment.lex = false;
1395 options.comment.definitions.clear();
1396 options.value.lex = false;
1397 options.value.definitions.clear();
1398 options.match_lex = false;
1399 options.match_tokens.clear();
1400 options.match_values.clear();
1401 options.lex.matchers.clear();
1402 options.token_set.clear();
1403 options.tokens.clear();
1404 options
1405 }
1406
1407 pub(crate) fn sort_for_lexing(&mut self) {
1413 self.match_tokens
1414 .sort_by(|_, left, _, right| left.tin.cmp(&right.tin));
1415 self.match_values.sort_keys();
1416 self.lex.matchers.sort_by(|name_a, left, name_b, right| {
1417 left.order
1418 .total_cmp(&right.order)
1419 .then_with(|| name_a.cmp(name_b))
1420 });
1421 }
1422
1423 pub fn is_ignored(&self, tin: Tin) -> bool {
1424 self.token_set
1425 .get("IGNORE")
1426 .is_some_and(|ignored| ignored.contains(&tin))
1427 }
1428
1429 pub(crate) fn ignore_tins(&self) -> Vec<Tin> {
1434 self.token_set.get("IGNORE").cloned().unwrap_or_default()
1435 }
1436
1437 pub(crate) fn char_sets(&self) -> crate::text::CharSets {
1447 crate::text::CharSets {
1448 space: crate::text::CharSet::new(&self.space.chars),
1449 line_ends: crate::text::CharSet::with_extra(&self.line.chars, &self.line.fixed),
1450 line: crate::text::CharSet::new(&self.line.chars),
1451 row: crate::text::CharSet::new(&self.line.row_chars),
1452 string: crate::text::CharSet::new(&self.string.chars),
1453 }
1454 }
1455
1456 pub fn token(&self, name: &str) -> Option<Tin> {
1457 crate::token::name_to_tin(name).or_else(|| {
1458 let name = if name.starts_with('#') {
1459 name.to_string()
1460 } else {
1461 format!("#{name}")
1462 };
1463 self.tokens
1464 .get(&name)
1465 .copied()
1466 .or_else(|| self.match_tokens.get(&name).map(|matcher| matcher.tin))
1467 .or_else(|| self.fixed.tokens.get(&name).map(|token| token.tin))
1468 })
1469 }
1470
1471 pub fn register_token(&mut self, name: impl Into<String>) -> Tin {
1472 let name = name.into();
1473 let name = if name.starts_with('#') {
1474 name
1475 } else {
1476 format!("#{name}")
1477 };
1478 if let Some(tin) = self.token(&name) {
1479 return tin;
1480 }
1481 let tin = self.next_tin();
1482 self.tokens.insert(name, tin);
1483 tin
1484 }
1485
1486 pub fn next_tin(&self) -> Tin {
1487 self.match_tokens
1488 .values()
1489 .map(|matcher| matcher.tin)
1490 .chain(self.fixed.tokens.values().map(|token| token.tin))
1491 .chain(self.tokens.values().copied())
1492 .max()
1493 .unwrap_or(TIN_MAX - 1)
1494 + 1
1495 }
1496
1497 pub fn token_name(&self, tin: Tin) -> String {
1498 self.match_tokens
1499 .values()
1500 .find(|matcher| matcher.tin == tin)
1501 .map(|matcher| matcher.name.clone())
1502 .or_else(|| {
1503 self.fixed
1504 .tokens
1505 .values()
1506 .find(|token| token.tin == tin)
1507 .map(|token| token.name.clone())
1508 })
1509 .or_else(|| {
1510 self.tokens
1511 .iter()
1512 .find(|(_, token_tin)| **token_tin == tin)
1513 .map(|(name, _)| name.clone())
1514 })
1515 .unwrap_or_else(|| crate::token::tin_name(tin).to_string())
1516 }
1517}