1use std::collections::BTreeMap;
8use std::collections::BTreeSet;
9use std::fmt::Debug;
10
11use chumsky::{inspector::Inspector, prelude::Input};
12
13use super::manuscript::MacroDefinition;
14use super::symbol::SymbolName;
15
16#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
17pub enum NumeralMode {
18 #[default]
19 Octal,
20 Decimal,
21}
22
23impl NumeralMode {
24 pub(crate) fn radix(self, alternate: bool) -> u32 {
25 match (&self, alternate) {
26 (&NumeralMode::Octal, false) | (&NumeralMode::Decimal, true) => 8,
27 (&NumeralMode::Decimal, false) | (&NumeralMode::Octal, true) => 10,
28 }
29 }
30
31 pub(crate) fn set_numeral_mode(&mut self, mode: NumeralMode) {
32 *self = mode;
33 }
34}
35
36#[test]
37fn test_numeral_mode_default() {
38 assert_eq!(NumeralMode::default(), NumeralMode::Octal);
39}
40
41pub(crate) trait MacroLookup {
42 fn lookup_macro(&self, name: &SymbolName) -> Option<&MacroDefinition>;
43 fn all_names(&self) -> BTreeSet<&SymbolName>;
44}
45
46#[derive(Debug, PartialEq, Eq, Default, Clone)]
47pub(crate) struct MacroTable {
48 macros: BTreeMap<SymbolName, Vec<MacroDefinition>>,
49}
50
51impl MacroTable {
52 pub(crate) fn define_macro(&mut self, definition: MacroDefinition) {
53 self.macros
55 .entry(definition.name.clone())
56 .or_default()
57 .push(definition);
58 }
59
60 fn forget_after(&mut self, pos: usize) {
61 for values in self.macros.values_mut() {
62 values.retain(|macro_definition| macro_definition.span.end <= pos);
63 }
64 }
65
66 fn latest(&self) -> usize {
67 self.macros
68 .values()
69 .flat_map(|macro_definitions| macro_definitions.iter())
70 .map(|macro_definition| macro_definition.span.end)
71 .max()
72 .unwrap_or(0)
73 }
74}
75
76impl MacroLookup for MacroTable {
77 fn lookup_macro(&self, name: &SymbolName) -> Option<&MacroDefinition> {
78 match self.macros.get(name) {
79 None => None,
80 Some(values) => values.last(),
81 }
82 }
83
84 fn all_names(&self) -> BTreeSet<&SymbolName> {
85 self.macros.keys().collect()
86 }
87}
88
89impl FromIterator<(SymbolName, MacroDefinition)> for MacroTable {
90 fn from_iter<T: IntoIterator<Item = (SymbolName, MacroDefinition)>>(macros: T) -> Self {
91 Self {
92 macros: macros
93 .into_iter()
94 .map(|(name, definition)| (name, vec![definition]))
95 .collect(),
96 }
97 }
98}
99
100#[derive(Debug, PartialEq, Eq, Clone)]
101pub(crate) struct State<'src> {
102 pub(super) numeral_mode: NumeralMode,
103 pub(super) body: &'src str,
104 pub(super) macros: MacroTable,
105}
106
107impl<'src> State<'src> {
108 pub(crate) fn new(body: &'src str, numeral_mode: NumeralMode) -> Self {
109 State {
110 numeral_mode,
111 body,
112 macros: Default::default(),
113 }
114 }
115
116 pub(crate) fn with_setup<F: FnMut(&mut Self)>(
117 source_file_body: &'src str,
118 mut setup: F,
119 ) -> Self {
120 let mut state = State::new(source_file_body, NumeralMode::default());
121 setup(&mut state);
122 state
123 }
124
125 pub(crate) fn define_macro(&mut self, definition: MacroDefinition) {
126 self.macros.define_macro(definition);
127 }
128
129 pub(crate) fn get_macro_definition(&self, name: &SymbolName) -> Option<&MacroDefinition> {
130 self.macros.lookup_macro(name)
131 }
132
133 pub(crate) fn macros(&self) -> &impl MacroLookup {
134 &self.macros
135 }
136}
137
138impl<'src, I: Input<'src>> Inspector<'src, I> for State<'src> {
139 type Checkpoint = usize;
140
141 #[inline(always)]
142 fn on_token(&mut self, _: &<I as Input<'src>>::Token) {}
143
144 fn on_save<'parse>(
145 &self,
146 _cursor: &chumsky::input::Cursor<'src, 'parse, I>,
147 ) -> Self::Checkpoint {
148 self.macros.latest()
149 }
150
151 fn on_rewind<'parse>(
152 &mut self,
153 marker: &chumsky::input::Checkpoint<'src, 'parse, I, Self::Checkpoint>,
154 ) {
155 let pos: usize = *marker.inspector();
156 self.macros.forget_after(pos);
157 }
158}