mech_core/
nodes.rs

1use crate::*; 
2#[cfg(feature = "no_std")]
3use core::cmp::Ordering;
4#[cfg(not(feature = "no_std"))] 
5use std::cmp::Ordering;
6
7#[cfg(feature = "serde")]
8pub fn compress_and_encode<T: serde::Serialize>(tree: &T) -> Result<String, Box<dyn std::error::Error>> {
9  let serialized_code = bincode::serde::encode_to_vec(tree,bincode::config::standard())?;
10  let mut compressed = Vec::new();
11  brotli::CompressorWriter::new(&mut compressed, 9, 4096, 22)
12      .write(&serialized_code)?;
13  Ok(base64::encode(compressed))
14}
15
16#[cfg(feature = "serde")]
17pub fn decode_and_decompress<T: serde::de::DeserializeOwned>(encoded: &str) -> Result<T, Box<dyn std::error::Error>> {
18  let decoded = base64::decode(encoded)?;
19  
20  let mut decompressed = Vec::new();
21  brotli::Decompressor::new(Cursor::new(decoded), 4096)
22      .read_to_end(&mut decompressed)?;
23  
24  let (decoded,red) = bincode::serde::decode_from_slice(&decompressed,bincode::config::standard())?;
25
26  Ok(decoded)
27}
28#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
29#[derive(Clone, Copy, PartialEq, Eq, Hash, Ord)]
30pub struct SourceLocation {
31  pub row: usize,
32  pub col: usize,
33}
34
35impl PartialOrd for SourceLocation {
36  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
37    if self.row < other.row {
38      Some(Ordering::Less)
39    } else if self.row > other.row {
40      Some(Ordering::Greater)
41    } else {
42      self.col.partial_cmp(&other.col)
43    }
44  }
45}
46
47impl fmt::Debug for SourceLocation {
48  #[inline]
49  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50    write!(f, "{}:{}", self.row, self.col);
51    Ok(())
52  }
53}
54
55#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
56#[derive(Clone, Hash, PartialEq, Eq)]
57pub struct SourceRange {
58  pub start: SourceLocation,
59  pub end:   SourceLocation,
60}
61
62/// Coordinates in SourceRange are 1-indexed, i.e. they directly translate
63/// human's view to line and column numbers.  Having value 0 means the 
64/// range is not initialized.
65impl Default for SourceRange {
66  fn default() -> Self {
67    SourceRange {
68      start: SourceLocation { row: 0, col: 0 },
69      end:   SourceLocation { row: 0, col: 0 },
70    }
71  }
72}
73
74impl fmt::Debug for SourceRange {
75  #[inline]
76  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77    write!(f, "[{:?}, {:?})", self.start, self.end);
78    Ok(())
79  }
80}
81
82pub fn merge_src_range(r1: SourceRange, r2: SourceRange) -> SourceRange {
83  SourceRange {
84    start: r1.start.min(r2.start),
85    end:   r2.end.max(r2.end),
86  }
87}
88
89pub trait Recoverable: Sized {
90  fn error_placeholder(skipped_tokens: Token, range: SourceRange) -> Self;
91}
92
93#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
94#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
95pub enum TokenKind {
96  AbstractSigil, Alpha, Ampersand, Any, Apostrophe, AssignOperator, Asterisk, AsyncTransitionOperator, At,
97  Backslash, Bar, BoxDrawing,
98  Caret, CarriageReturn, CarriageReturnNewLine, Colon, CodeBlock, Comma,
99  Dash, DefineOperator, Digit, Dollar,
100  Emoji, EmphasisSigil, Empty, Equal, EquationSigil, Error, ErrorSigil, Exclamation, 
101  False, FloatLeft, FloatRight, FootnotePrefix,
102  Grave, GraveCodeBlockSigil,
103  HashTag, HighlightSigil, HttpPrefix,
104  IdeaSigil, Identifier, ImgPrefix, InfoSigil, InlineCode, 
105  LeftAngle, LeftBrace, LeftBracket, LeftParenthesis,
106  Newline, Not, Number,
107  OutputOperator,
108  Percent, Period, Plus,
109  QueryOperator, Question, QuestionSigil, Quote, QuoteSigil,
110  RightAngle, RightBrace, RightBracket, RightParenthesis,
111  SectionSigil, Semicolon, Space, Slash, String, StrikeSigil, StrongSigil, SuccessSigil,
112  Tab, Text, Tilde, TildeCodeBlockSigil, Title, TransitionOperator, True,
113  UnderlineSigil, Underscore,
114  WarningSigil,
115}
116
117#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
118#[derive(Clone, Hash, PartialEq, Eq)]
119pub struct Token { 
120  pub kind: TokenKind, 
121  pub chars: Vec<char>, 
122  pub src_range: SourceRange 
123}
124
125impl fmt::Debug for Token {
126  #[inline]
127  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
128    write!(f, "{:?}:{:?}:{:?}", self.kind, String::from_iter(self.chars.iter().cloned()), self.src_range);
129    Ok(())
130  }
131}
132
133impl Default for Token {
134  fn default() -> Self {
135    Token{
136      kind: TokenKind::Empty,
137      chars: vec![],
138      src_range: SourceRange::default(),
139    }
140  }
141}
142
143impl Token {
144
145  pub fn new(kind: TokenKind, src_range: SourceRange, chars: Vec<char>) -> Token {
146    Token{kind, chars, src_range}
147  }
148
149  pub fn to_string(&self) -> String {
150    self.chars.iter().collect()
151  }
152
153  pub fn merge_tokens(tokens: &mut Vec<Token>) -> Option<Token> {
154    if tokens.len() == 0 {
155      None
156    } else if tokens.len() == 1 {
157      Some(tokens[0].clone())
158    } else {
159      let first = tokens[0].src_range.clone();
160      let kind = tokens[0].kind.clone();
161      let last = tokens.last().unwrap().src_range.clone();
162      let src_range = merge_src_range(first, last);
163      let chars: Vec<char> = tokens.iter_mut().fold(vec![],|mut m, ref mut t| {m.append(&mut t.chars.clone()); m});
164      let merged_token = Token{kind, chars, src_range};
165      Some(merged_token)
166    }
167  }
168}
169
170#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
171#[derive(Clone, Debug, Hash, PartialEq, Eq)]
172pub struct TableOfContents {
173    pub title: Option<Title>,
174    pub sections: Vec<Section>,
175}
176
177#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
178#[derive(Clone, Debug, Hash, PartialEq, Eq)]
179pub struct Program {
180  pub title: Option<Title>,
181  pub body: Body,
182}
183
184impl Program {
185  pub fn tokens(&self) -> Vec<Token> {
186    /*let mut title_tokens = match self.title.tokens() {
187      Some(tkns) => tkns,
188      None => vec![],
189    };*/
190    let body_tokens = self.body.tokens();
191    body_tokens
192  }
193
194  pub fn table_of_contents(&self) -> TableOfContents {
195    let mut sections = vec![];
196    for s in &self.body.sections {
197      sections.push(s.table_of_contents());
198    }
199    TableOfContents {
200      title: self.title.clone(),
201      sections,
202    }
203  }
204
205}
206
207#[cfg(feature = "pretty_print")]
208impl PrettyPrint for Program {
209fn pretty_print(&self) -> String {
210    #[cfg(feature = "serde")]
211    let json_string = serde_json::to_string_pretty(self).unwrap();
212    #[cfg(not(feature = "serde"))]
213    let json_string = "Error: Enable Serde to prettt print trees.".to_string();
214
215    let depth = |line: &str|->usize{line.chars().take_while(|&c| c == ' ').count()};
216    let mut result = String::new();
217    let lines: Vec<&str> = json_string.lines().collect();
218    result.push_str("Program\n");
219    for (index, line) in lines.iter().enumerate() {
220      let trm = line.trim();
221      if trm == "}" || 
222         trm == "},"|| 
223         trm == "{" || 
224         trm == "[" || 
225         trm == "],"|| 
226         trm == "]" {
227        continue;
228      }
229  
230      // Count leading spaces to determine depth
231      let d = depth(line);
232      // Construct the tree-like prefix
233      let mut prefix = String::new();
234      for _ in 0..d {
235        prefix.push_str(" ");
236      }
237      if index == lines.len() {
238        prefix.push_str("└ ");
239      } else {
240        if depth(lines[index + 1]) != d {
241          prefix.push_str("└ ");
242        } else {
243          prefix.push_str("├ ");
244        }
245      }
246      let trm = line.trim();
247      let trm = trm.trim_end_matches('{')
248                    .trim_start_matches('"')
249                    .trim_end_matches(':')
250                    .trim_end_matches('"')
251                    .trim_end_matches('[');
252      prefix.push_str(trm);
253  
254      // Append formatted line to result
255      result.push_str(&prefix);
256      result.push('\n');
257      result = result.replace("\":", "");
258    }
259    let mut indexed_str = IndexedString::new(&result);
260    'rows: for i in 0..indexed_str.rows {
261      let rowz = &indexed_str.index_map[i];
262      for j in 0..rowz.len() {
263        let c = match indexed_str.get(i,j) {
264          Some(c) => c,
265          None => continue,
266        };
267        if c == '└' {
268          for k in i+1..indexed_str.rows {
269            match indexed_str.get(k,j) {
270              Some(c2) => {
271                if c2 == '└' {
272                  indexed_str.set(i,j,'├');
273                  for l in i+1..k {
274                    match indexed_str.get(l,j) {
275                      Some(' ') => {indexed_str.set(l,j,'│');},
276                      Some('└') => {indexed_str.set(l,j,'├');},
277                      _ => (),
278                    }
279                  }
280                } else if c2 == ' ' {
281                  continue;
282                } else {
283                  continue 'rows;
284                }
285              },
286              None => continue,
287            }
288  
289          }
290        } else if c == ' ' || c == '│' {
291          continue;
292        } else {
293          continue 'rows;
294        }
295      }
296    }
297    indexed_str.to_string()
298  }
299}
300
301#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
302#[derive(Clone, Debug, Hash, PartialEq, Eq)]
303pub struct Title {
304  pub text: Token,
305}
306
307impl Title {
308
309  pub fn to_string(&self) -> String {
310    self.text.to_string()
311  }
312
313}
314
315#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
316#[derive(Clone, Debug, Hash, PartialEq, Eq)]
317pub struct Body {
318  pub sections: Vec<Section>,
319}
320
321impl Body {
322  pub fn tokens(&self) -> Vec<Token> {
323    let mut out = vec![];
324    for s in &self.sections {
325      let mut tkns = s.tokens();
326      out.append(&mut tkns);
327    }
328    out
329  }
330}
331
332#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
333#[derive(Clone, Debug, Hash, PartialEq, Eq)]
334pub enum ColumnAlignment {
335  Left,
336  Center,
337  Right,
338}
339
340#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
341#[derive(Clone, Debug, Hash, PartialEq, Eq)]
342pub struct MarkdownTable {
343  pub header: Vec<Paragraph>,
344  pub rows: Vec<Vec<Paragraph>>,
345  pub alignment: Vec<ColumnAlignment>,
346}
347
348#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
349#[derive(Clone, Debug, Hash, PartialEq, Eq)]
350pub struct Subtitle {
351  pub text: Paragraph,
352  pub level: u8,
353}
354
355impl Subtitle {
356  pub fn to_string(&self) -> String {
357    self.text.to_string()
358  }
359}
360
361#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
362#[derive(Clone, Debug, Hash, PartialEq, Eq)]
363pub struct Section {
364  pub subtitle: Option<Subtitle>,
365  pub elements: Vec<SectionElement>,
366}
367
368impl Section {
369
370  pub fn table_of_contents(&self) -> Section {
371    let elements: Vec<_> = self.elements.iter()
372      .filter(|e| matches!(e, SectionElement::Subtitle(_)))
373      .cloned()
374      .collect();
375    Section {
376      subtitle: self.subtitle.clone(),
377      elements,
378    }
379  }
380
381  pub fn tokens(&self) -> Vec<Token> {
382    let mut out = vec![];
383    for s in &self.elements {
384      let mut tkns = s.tokens();
385      out.append(&mut tkns);
386    }
387    out
388  }
389}
390
391#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
392#[derive(Clone, Debug, Hash, PartialEq, Eq)]
393pub struct Grammar {
394  pub rules: Vec<Rule>,
395}
396
397#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
398#[derive(Clone, Debug, Hash, PartialEq, Eq)]
399pub struct GrammarIdentifier {
400  pub name: Token,
401}
402
403impl GrammarIdentifier {
404  pub fn tokens(&self) -> Vec<Token> {
405    vec![self.name.clone()]
406  }
407
408  pub fn to_string(&self) -> String {
409    self.name.to_string()
410  }
411}
412
413#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
414#[derive(Clone, Debug, Hash, PartialEq, Eq)]
415pub struct Rule {
416  pub name: GrammarIdentifier,
417  pub expr: GrammarExpression,
418}
419
420#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
421#[derive(Clone, Debug, Hash, PartialEq, Eq)]
422pub enum GrammarExpression {
423  Choice(Vec<GrammarExpression>),
424  Definition(GrammarIdentifier),
425  Group(Box<GrammarExpression>),
426  List(Box<GrammarExpression>, Box<GrammarExpression>),
427  Not(Box<GrammarExpression>),
428  Optional(Box<GrammarExpression>),
429  Peek(Box<GrammarExpression>),
430  Repeat0(Box<GrammarExpression>),
431  Repeat1(Box<GrammarExpression>),
432  Range(Token,Token),
433  Sequence(Vec<GrammarExpression>),
434  Terminal(Token),
435}
436
437impl GrammarExpression {
438  pub fn tokens(&self) -> Vec<Token> {
439    match self {
440      GrammarExpression::Choice(exprs) => {
441        let mut tokens = vec![];
442        for expr in exprs {
443          tokens.append(&mut expr.tokens());
444        }
445        tokens
446      }
447      GrammarExpression::Definition(id) => id.tokens(),
448      GrammarExpression::Group(expr) => expr.tokens(),
449      GrammarExpression::List(expr1, expr2) => {
450        let mut tokens = expr1.tokens();
451        tokens.append(&mut expr2.tokens());
452        tokens
453      }
454      GrammarExpression::Not(expr) => expr.tokens(),
455      GrammarExpression::Optional(expr) => expr.tokens(),
456      GrammarExpression::Peek(expr) => expr.tokens(),
457      GrammarExpression::Repeat0(expr) => expr.tokens(),
458      GrammarExpression::Repeat1(expr) => expr.tokens(),
459      GrammarExpression::Range(t1, t2) => vec![t1.clone(), t2.clone()],
460      GrammarExpression::Sequence(exprs) => {
461        let mut tokens = vec![];
462        for expr in exprs {
463          tokens.append(&mut expr.tokens());
464        }
465        tokens
466      }
467      GrammarExpression::Terminal(t) => vec![t.clone()],
468    }
469  }
470
471
472}
473
474#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
475#[derive(Clone, Debug, Hash, PartialEq, Eq)]
476pub struct Citation {
477  pub id: Token,
478  pub text: Paragraph,
479}
480
481impl Citation {
482  pub fn to_string(&self) -> String {
483    format!("[{}]: {}", self.id.to_string(), self.text.to_string())
484  }
485}
486
487// Stores code block configuration
488#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
489#[derive(Clone, Debug, Hash, PartialEq, Eq)]
490pub struct BlockConfig {
491  pub namespace_str: String,
492  pub namespace: u64,
493  pub disabled: bool,
494  pub hidden: bool,
495}
496
497pub type Footnote = (Token, Vec<Paragraph>);
498
499#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
500#[derive(Clone, Debug, Hash, PartialEq, Eq)]
501pub enum FloatDirection {
502  Left,
503  Right,
504}
505
506#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
507#[derive(Clone, Debug, Hash, PartialEq, Eq)]
508pub struct FencedMechCode {
509  pub code: Vec<(MechCode,Option<Comment>)>,
510  pub config: BlockConfig,
511  pub options: Option<OptionMap>,
512}
513
514#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
515#[derive(Clone, Debug, Hash, PartialEq, Eq)]
516pub enum SectionElement {
517  Abstract(Vec<Paragraph>),
518  QuoteBlock(Vec<Paragraph>),
519  InfoBlock(Vec<Paragraph>),
520  SuccessBlock(Vec<Paragraph>),
521  IdeaBlock(Vec<Paragraph>),
522  WarningBlock(Vec<Paragraph>),
523  ErrorBlock(Vec<Paragraph>),
524  QuestionBlock(Vec<Paragraph>),
525  Citation(Citation),
526  CodeBlock(Token),
527  Comment(Comment),
528  Diagram(Token),
529  Equation(Token),
530  FencedMechCode(FencedMechCode),
531  Float((Box<SectionElement>, FloatDirection)),
532  Footnote(Footnote),
533  Grammar(Grammar),
534  Image(Image),
535  List(MDList),
536  MechCode(Vec<(MechCode,Option<Comment>)>),
537  Paragraph(Paragraph),
538  Subtitle(Subtitle),
539  Table(MarkdownTable),
540  ThematicBreak,
541  Error(Token, SourceRange),
542}
543
544impl Recoverable for SectionElement {
545  fn error_placeholder(skipped_tokens: Token, range: SourceRange) -> Self {
546    SectionElement::Error(skipped_tokens, range)
547  }
548}
549
550impl SectionElement {
551
552  pub fn tokens(&self) -> Vec<Token> {
553    match self {
554      SectionElement::FencedMechCode(c) => {
555        let mut tokens = vec![];
556        for (c, _) in &c.code {
557          tokens.append(&mut c.tokens());
558        }
559        tokens
560      }
561      SectionElement::MechCode(codes) => {
562        let mut tokens = vec![];
563        for (code, _) in codes {
564          tokens.append(&mut code.tokens());
565        }
566        tokens
567      }
568      SectionElement::Abstract(paragraphs)
569      | SectionElement::QuoteBlock(paragraphs)
570      | SectionElement::InfoBlock(paragraphs)
571      | SectionElement::SuccessBlock(paragraphs)
572      | SectionElement::IdeaBlock(paragraphs)
573      | SectionElement::WarningBlock(paragraphs)
574      | SectionElement::ErrorBlock(paragraphs)
575      | SectionElement::QuestionBlock(paragraphs) => {
576        let mut tokens = vec![];
577        for paragraph in paragraphs {
578          tokens.append(&mut paragraph.tokens());
579        }
580        tokens
581      }
582      SectionElement::Citation(citation) => citation.text.tokens(),
583      SectionElement::CodeBlock(token)
584      | SectionElement::Diagram(token)
585      | SectionElement::Equation(token) => vec![token.clone()],
586      SectionElement::Comment(comment) => comment.tokens(),
587      SectionElement::Float((element, _)) => element.tokens(),
588      SectionElement::Footnote((_, paragraphs)) => {
589        let mut tokens = vec![];
590        for paragraph in paragraphs {
591          tokens.append(&mut paragraph.tokens());
592        }
593        tokens
594      }
595        SectionElement::Grammar(grammar) => {
596        let mut tokens = vec![];
597        for rule in &grammar.rules {
598          tokens.append(&mut rule.name.tokens());
599          tokens.append(&mut rule.expr.tokens());
600        }
601        tokens
602      }
603      SectionElement::Image(image) => {
604        let mut tokens = vec![image.src.clone()];
605        if let Some(caption) = &image.caption {
606          tokens.append(&mut caption.tokens());
607        }
608        tokens
609      }
610      SectionElement::List(list) => match list {
611      MDList::Unordered(items) => {
612        let mut tokens = vec![];
613        for ((_, paragraph), sublist) in items {
614        tokens.append(&mut paragraph.tokens());
615        if let Some(sublist) = sublist {
616          tokens.append(&mut sublist.tokens());
617        }
618        }
619        tokens
620      }
621      MDList::Ordered(ordered_list) => {
622        let mut tokens = ordered_list.start.tokens();
623        for ((_, paragraph), sublist) in &ordered_list.items {
624        tokens.append(&mut paragraph.tokens());
625        if let Some(sublist) = sublist {
626          tokens.append(&mut sublist.tokens());
627        }
628        }
629        tokens
630      }
631      MDList::Check(items) => {
632        let mut tokens = vec![];
633        for ((_, paragraph), sublist) in items {
634        tokens.append(&mut paragraph.tokens());
635        if let Some(sublist) = sublist {
636          tokens.append(&mut sublist.tokens());
637        }
638        }
639        tokens
640      }
641      },
642      SectionElement::Paragraph(paragraph) => paragraph.tokens(),
643      SectionElement::Subtitle(subtitle) => subtitle.text.tokens(),
644      SectionElement::Table(table) => {
645        let mut tokens = vec![];
646        for header in &table.header {
647          tokens.append(&mut header.tokens());
648        }
649        for row in &table.rows {
650          for column in row {
651          tokens.append(&mut column.tokens());
652          }
653        }
654        tokens
655      }
656      SectionElement::ThematicBreak => vec![],
657      SectionElement::Error(token, _) => vec![token.clone()],
658    }
659  }
660}
661
662
663#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
664#[derive(Clone, Debug, Hash, PartialEq, Eq)]
665pub struct OptionMap {
666  pub elements: Vec<(Identifier, MechString)>,
667}
668
669
670#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
671#[derive(Clone, Debug, Hash, PartialEq, Eq)]
672pub struct Image {
673  pub src: Token,
674  pub caption: Option<Paragraph>,
675  pub style: Option<OptionMap>,
676}
677
678impl Image {
679  pub fn to_string(&self) -> String {
680    let caption = match &self.caption {
681      Some(c) => c.to_string(),
682      None => "".to_string(),
683    };
684    format!("![{}]({})", caption, self.src.to_string())
685  }
686}
687
688pub type UnorderedList = Vec<((Option<Token>,Paragraph),Option<MDList>)>;
689pub type CheckList = Vec<((bool,Paragraph),Option<MDList>)>;
690
691#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
692#[derive(Clone, Debug, Hash, PartialEq, Eq)]
693pub struct OrderedList {
694  pub start: Number,
695  pub items: Vec<((Number,Paragraph),Option<MDList>)>,
696}
697
698#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
699#[derive(Clone, Debug, Hash, PartialEq, Eq)]
700pub enum MDList {
701  Unordered(UnorderedList),
702  Ordered(OrderedList),
703  Check(CheckList)
704}
705
706impl MDList {
707
708  pub fn tokens(&self) -> Vec<Token> {
709    match self {
710      MDList::Unordered(items) => {
711        let mut tokens = vec![];
712        for ((_, paragraph), sublist) in items {
713          tokens.append(&mut paragraph.tokens());
714          if let Some(sublist) = sublist {
715            tokens.append(&mut sublist.tokens());
716          }
717        }
718        tokens
719      }
720      MDList::Ordered(ordered_list) => {
721        let mut tokens = ordered_list.start.tokens();
722        for ((_, paragraph), sublist) in &ordered_list.items {
723          tokens.append(&mut paragraph.tokens());
724          if let Some(sublist) = sublist {
725            tokens.append(&mut sublist.tokens());
726          }
727        }
728        tokens
729      }
730      MDList::Check(items) => {
731        let mut tokens = vec![];
732        for ((_, paragraph), sublist) in items {
733          tokens.append(&mut paragraph.tokens());
734          if let Some(sublist) = sublist {
735            tokens.append(&mut sublist.tokens());
736          }
737        }
738        tokens
739      }
740    }
741  }
742
743}
744
745#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
746#[derive(Clone, Debug, Hash, PartialEq, Eq)]
747pub enum MechCode {
748  Comment(Comment),
749  Expression(Expression),
750  //FsmImplementation(FsmImplementation),
751  //FsmSpecification(FsmSpecification),
752  FunctionDefine(FunctionDefine),
753  Statement(Statement),
754  Error(Token, SourceRange),
755}
756
757impl Recoverable for MechCode {
758  fn error_placeholder(skipped_tokens: Token, range: SourceRange) -> Self {
759    MechCode::Error(skipped_tokens, range)
760  }
761}
762
763impl MechCode {
764  pub fn tokens(&self) -> Vec<Token> {
765    match self {
766      MechCode::Expression(x) => x.tokens(),
767      MechCode::Statement(x) => x.tokens(),
768      MechCode::Comment(x) => x.tokens(),
769      MechCode::Error(t,_) => vec![t.clone()],
770      _ => todo!(),
771      //FunctionDefine(x) => x.tokens(),
772      //FsmSpecification(x) => x.tokens(),
773      //FsmImplementation(x) => x.tokens(),
774    }
775  }
776}
777
778#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
779#[derive(Clone, Debug, Hash, PartialEq, Eq)]
780pub struct FunctionDefine {
781  pub name: Identifier,
782  pub input: Vec<FunctionArgument>,
783  pub output: Vec<FunctionArgument>,
784  pub statements: Vec<Statement>,
785}
786
787#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
788#[derive(Clone, Debug, Hash, PartialEq, Eq)]
789pub struct FunctionArgument {
790  pub name: Identifier,
791  pub kind: KindAnnotation,
792}
793
794impl FunctionArgument {
795  pub fn tokens(&self) -> Vec<Token> {
796    let mut tokens = self.name.tokens();
797    tokens.append(&mut self.kind.tokens());
798    tokens
799  }
800}
801
802#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
803#[derive(Clone, Debug, Hash, PartialEq, Eq)]
804pub struct FsmImplementation {
805  pub name: Identifier,
806  pub input: Vec<Identifier>,
807  pub start: Pattern,
808  pub arms: Vec<FsmArm>,
809}
810
811#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
812#[derive(Clone, Debug, Hash, PartialEq, Eq)]
813pub enum FsmArm {
814  Guard(Pattern,Vec<Guard>),
815  Transition(Pattern,Vec<Transition>),
816}
817
818#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
819#[derive(Clone, Debug, Hash, PartialEq, Eq)]
820pub struct Guard { 
821  pub condition: Pattern,
822  pub transitions: Vec<Transition>,
823}
824
825#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
826#[derive(Clone, Debug, Hash, PartialEq, Eq)]
827pub enum Transition {
828  Async(Pattern),
829  CodeBlock(Vec<(MechCode, Option<Comment>)>),
830  Next(Pattern),
831  Output(Pattern),
832  Statement(Statement),
833}
834
835#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
836#[derive(Clone, Debug, Hash, PartialEq, Eq)]
837pub enum Pattern {
838  Expression(Expression),
839  TupleStruct(PatternTupleStruct),
840  Tuple(PatternTuple),
841  Wildcard,
842}
843
844impl Pattern {
845  pub fn tokens(&self) -> Vec<Token> {
846    match self {
847      Pattern::Expression(e) => e.tokens(),
848      Pattern::TupleStruct(ts) => ts.tokens(),
849      Pattern::Tuple(t) => t.tokens(),
850      Pattern::Wildcard => vec![],
851    }
852  }
853}
854
855#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
856#[derive(Clone, Debug, Hash, PartialEq, Eq)]
857pub struct PatternTupleStruct {
858  pub name: Identifier,
859  pub patterns: Vec<Pattern>,
860}
861
862impl PatternTupleStruct {
863  pub fn tokens(&self) -> Vec<Token> {
864    let mut tokens = self.name.tokens();
865    for p in &self.patterns {
866      tokens.append(&mut p.tokens());
867    }
868    tokens
869  }
870}
871
872#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
873#[derive(Clone, Debug, Hash, PartialEq, Eq)]
874pub struct PatternTuple(pub Vec<Pattern>);
875
876impl PatternTuple {
877  pub fn tokens(&self) -> Vec<Token> {
878    let mut tokens = vec![];
879    for p in &self.0 {
880        tokens.append(&mut p.tokens());
881    }
882    tokens
883  }
884}
885
886#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
887#[derive(Clone, Debug, Hash, PartialEq, Eq)]
888pub struct FsmSpecification {
889  pub name: Identifier,
890  pub input: Vec<Var>,
891  pub output: Option<KindAnnotation>,
892  pub states: Vec<StateDefinition>,
893}
894
895#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
896#[derive(Clone, Debug, Hash, PartialEq, Eq)]
897pub struct StateDefinition {
898  pub name: Identifier,
899  pub state_variables: Option<Vec<Var>>,
900}
901
902#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
903#[derive(Clone, Debug, Hash, PartialEq, Eq)]
904pub enum Statement {
905  EnumDefine(EnumDefine),
906  //FsmDeclare(FsmDeclare),
907  KindDefine(KindDefine),
908  OpAssign(OpAssign),
909  VariableAssign(VariableAssign),
910  VariableDefine(VariableDefine),
911  TupleDestructure(TupleDestructure),
912  SplitTable,     // todo
913  FlattenTable,   // todo
914}
915
916impl Statement {
917  pub fn tokens(&self) -> Vec<Token> {
918    match self {
919      Statement::EnumDefine(x) => x.tokens(),
920      //Statement::FsmDeclare(x) => x.tokens(),
921      Statement::KindDefine(x) => x.tokens(),
922      Statement::OpAssign(x) => x.tokens(),
923      Statement::VariableAssign(x) => x.tokens(),
924      Statement::VariableDefine(x) => x.tokens(),
925      Statement::TupleDestructure(x) => x.tokens(),
926      Statement::SplitTable => vec![], // todo
927      Statement::FlattenTable => vec![], // todo
928    }
929  }
930}
931
932#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
933#[derive(Clone, Debug, Hash, PartialEq, Eq)]
934pub struct TupleDestructure {
935  pub vars: Vec<Identifier>,
936  pub expression: Expression,
937}
938
939impl TupleDestructure {
940  pub fn tokens(&self) -> Vec<Token> {
941    let mut tokens = vec![];
942    for var in &self.vars {
943      tokens.append(&mut var.tokens());
944    }
945    tokens.append(&mut self.expression.tokens());
946    tokens
947  }
948}
949
950#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
951#[derive(Clone, Debug, Hash, PartialEq, Eq)]
952pub struct FsmPipe {
953  pub start: FsmInstance,
954  pub transitions: Vec<Transition>
955}
956
957#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
958#[derive(Clone, Debug, Hash, PartialEq, Eq)]
959pub enum PipeElement {
960  Expression(Expression),
961  FsmInstance(FsmInstance),
962  Timer // todo
963}
964
965#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
966#[derive(Clone, Debug, Hash, PartialEq, Eq)]
967pub struct FsmDeclare {
968  pub fsm: Fsm,
969  pub pipe: FsmPipe,
970}
971
972#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
973#[derive(Clone, Debug, Hash, PartialEq, Eq)]
974pub struct Fsm {
975  pub name: Identifier,
976  pub args: Option<ArgumentList>,
977  pub kind: Option<KindAnnotation>
978}
979
980pub type FsmArgs = Vec<(Option<Identifier>,Expression)>;
981
982#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
983#[derive(Clone, Debug, Hash, PartialEq, Eq)]
984pub struct FsmInstance {
985  pub name: Identifier,
986  pub args: Option<FsmArgs>,
987}
988
989#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
990#[derive(Clone, Debug, Hash, PartialEq, Eq)]
991pub struct EnumDefine {
992  pub name: Identifier,
993  pub variants: Vec<EnumVariant>,
994}
995
996impl EnumDefine {
997  pub fn tokens(&self) -> Vec<Token> {
998    let mut tokens = self.name.tokens();
999    for v in &self.variants {
1000      tokens.append(&mut v.tokens());
1001    }
1002    tokens
1003  }
1004}
1005
1006#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1007#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1008pub struct EnumVariant {
1009  pub name: Identifier,
1010  pub value: Option<KindAnnotation>,
1011}
1012
1013impl EnumVariant {
1014  pub fn tokens(&self) -> Vec<Token> {
1015    let mut tokens = self.name.tokens();
1016    if let Some(value) = &self.value {
1017      tokens.append(&mut value.tokens());
1018    }
1019    tokens
1020  }
1021}
1022
1023#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1024#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1025pub struct KindDefine {
1026  pub name: Identifier,
1027  pub kind: KindAnnotation,
1028}
1029
1030impl KindDefine {
1031  pub fn tokens(&self) -> Vec<Token> {
1032    let mut tokens = self.name.tokens();
1033    tokens.append(&mut self.kind.tokens());
1034    tokens
1035  }
1036}
1037
1038#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1039#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1040pub struct Record {
1041  pub bindings: Vec<Binding>,
1042}
1043
1044impl Record {
1045  pub fn tokens(&self) -> Vec<Token> {
1046    let mut tkns = vec![];
1047    for b in &self.bindings {
1048      let mut t = b.tokens();
1049      tkns.append(&mut t);
1050    }
1051    tkns
1052  }
1053}
1054
1055#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1056#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1057pub enum Structure {
1058  Empty,
1059  Map(Map),
1060  Matrix(Matrix),
1061  Record(Record),
1062  Set(Set),
1063  Table(Table),
1064  Tuple(Tuple),
1065  TupleStruct(TupleStruct),
1066}
1067
1068impl Structure {
1069  pub fn tokens(&self) -> Vec<Token> {
1070    match self {
1071      Structure::Empty => vec![],
1072      Structure::Map(map) => map.tokens(),
1073      Structure::Matrix(mat) => mat.tokens(),
1074      Structure::Record(rec) => rec.tokens(),
1075      Structure::Set(set) => set.tokens(),
1076      Structure::Table(tab) => tab.tokens(),
1077      Structure::Tuple(tup) => tup.tokens(),
1078      Structure::TupleStruct(ts) => ts.tokens(),
1079    }
1080  }
1081}
1082
1083#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1084#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1085pub struct Map {
1086  pub elements: Vec<Mapping>,
1087}
1088
1089impl Map {
1090  pub fn tokens(&self) -> Vec<Token> {
1091    let mut tkns = vec![];
1092    for e in &self.elements {
1093      let mut t = e.tokens();
1094      tkns.append(&mut t);
1095    }
1096    tkns
1097  }
1098}
1099
1100#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1101#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1102pub struct Mapping {
1103  pub key: Expression,
1104  pub value: Expression,
1105}
1106
1107impl Mapping {
1108  pub fn tokens(&self) -> Vec<Token> {
1109    let mut tkns = self.key.tokens();
1110    tkns.append(&mut self.value.tokens());
1111    tkns
1112  }
1113}
1114
1115#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1116#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1117pub struct Set {
1118  pub elements: Vec<Expression>,
1119}
1120
1121impl Set {
1122  pub fn tokens(&self) -> Vec<Token> {
1123    let mut tkns = vec![];
1124    for e in &self.elements {
1125      let mut t = e.tokens();
1126      tkns.append(&mut t);
1127    }
1128    tkns
1129  }
1130}
1131
1132#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1133#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1134pub struct Atom {
1135  pub name: Identifier,
1136}
1137
1138impl Atom {
1139  pub fn tokens(&self) -> Vec<Token> {
1140    self.name.tokens()
1141  }
1142}
1143
1144#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1145#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1146pub struct TupleStruct {
1147  pub name: Identifier,
1148  pub value: Box<Expression>,
1149}
1150
1151impl TupleStruct {
1152  pub fn tokens(&self) -> Vec<Token> {
1153    let mut tkns = self.name.tokens();
1154    tkns.append(&mut self.value.tokens());
1155    tkns
1156  }
1157}
1158
1159#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1160#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1161pub struct Matrix {
1162  pub rows: Vec<MatrixRow>,
1163}
1164
1165impl Matrix {
1166  pub fn tokens(&self) -> Vec<Token> {
1167    let mut tkns = vec![];
1168    for r in &self.rows {
1169      let mut t = r.tokens();
1170      tkns.append(&mut t);
1171    }
1172    tkns
1173  }
1174}
1175
1176#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1177#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1178pub struct TableHeader(pub Vec<Field>);
1179
1180impl TableHeader {
1181  pub fn new(fields: Vec<Field>) -> TableHeader {
1182    TableHeader(fields)
1183  }
1184  pub fn tokens(&self) -> Vec<Token> {
1185    let mut tkns = vec![];
1186    for f in &self.0 {
1187      let mut t = f.tokens();
1188      tkns.append(&mut t);
1189    }
1190    tkns
1191  }
1192}
1193
1194#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1195#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1196pub struct Table {
1197  pub header: TableHeader,
1198  pub rows: Vec<TableRow>,
1199}
1200
1201impl Table {
1202  pub fn tokens(&self) -> Vec<Token> {
1203    let mut tkns = vec![];
1204    for f in &self.header.0 {
1205      let mut t = f.tokens();
1206      tkns.append(&mut t);
1207    }
1208    for r in &self.rows {
1209      let mut t = r.tokens();
1210      tkns.append(&mut t);
1211    }
1212    tkns
1213  }
1214}
1215
1216#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1217#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1218pub struct Field {
1219  pub name: Identifier,
1220  pub kind: Option<KindAnnotation>,
1221}
1222
1223impl Field {
1224  pub fn tokens(&self) -> Vec<Token> {
1225    let mut tkns = self.name.tokens();
1226    if let Some(knd) = &self.kind {
1227      let mut t = knd.tokens();
1228      tkns.append(&mut t);
1229    }
1230    tkns
1231  }
1232}
1233
1234#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1235#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1236pub struct TableColumn {
1237  pub element: Expression,
1238}
1239
1240impl TableColumn {
1241  pub fn tokens(&self) -> Vec<Token> {
1242    self.element.tokens()
1243  }
1244}
1245
1246#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1247#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1248pub struct MatrixColumn {
1249  pub element: Expression,
1250}
1251
1252impl MatrixColumn {
1253  pub fn tokens(&self) -> Vec<Token> {
1254    self.element.tokens()
1255  }
1256}
1257
1258#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1259#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1260pub struct TableRow {
1261  pub columns: Vec<TableColumn>,
1262}
1263
1264impl TableRow {
1265  pub fn tokens(&self) -> Vec<Token> {
1266    let mut tkns = vec![];
1267    for r in &self.columns {
1268      let mut t = r.element.tokens();
1269      tkns.append(&mut t);
1270    }
1271    tkns
1272  }
1273}
1274
1275#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1276#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1277pub struct MatrixRow {
1278  pub columns: Vec<MatrixColumn>,
1279}
1280
1281impl MatrixRow {
1282  pub fn tokens(&self) -> Vec<Token> {
1283    let mut tkns = vec![];
1284    for r in &self.columns {
1285      let mut t = r.tokens();
1286      tkns.append(&mut t);
1287    }
1288    tkns
1289  }
1290}
1291
1292#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1293#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1294pub struct VariableDefine {
1295  pub mutable: bool,
1296  pub var: Var,
1297  pub expression: Expression,
1298}
1299
1300impl VariableDefine {
1301  pub fn tokens(&self) -> Vec<Token> {
1302    let mut tkns = self.var.tokens();
1303    tkns.append(&mut self.expression.tokens());
1304    tkns
1305  }
1306}
1307
1308#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1309#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1310pub struct Var {
1311  pub name: Identifier,
1312  pub kind: Option<KindAnnotation>,
1313}
1314
1315impl Var {
1316  pub fn tokens(&self) -> Vec<Token> {
1317    let mut tkns = self.name.tokens();
1318    if let Some(knd) = &self.kind {
1319      let mut t = knd.tokens();
1320      tkns.append(&mut t);
1321    }
1322    tkns
1323  }
1324}
1325
1326#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1327#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1328pub struct VariableAssign {
1329  pub target: SliceRef,
1330  pub expression: Expression,
1331}
1332
1333impl VariableAssign {
1334  pub fn tokens(&self) -> Vec<Token> {
1335    let mut tkns = self.target.tokens();
1336    tkns.append(&mut self.expression.tokens());
1337    tkns
1338  }
1339}
1340
1341#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1342#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1343pub struct Identifier {
1344  pub name: Token,
1345}
1346
1347impl Identifier {
1348  pub fn tokens(&self) -> Vec<Token> {
1349    vec![self.name.clone()]
1350  }
1351
1352  pub fn to_string(&self) -> String {
1353    self.name.to_string()
1354  }
1355
1356}
1357
1358impl Identifier {
1359  pub fn hash(&self) -> u64 {
1360    hash_chars(&self.name.chars)
1361  }
1362}
1363
1364#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1365#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1366pub struct Emoji {
1367  pub tokens: Vec<Token>,
1368}
1369
1370#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1371#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1372pub struct Word {
1373  pub tokens: Vec<Token>,
1374}
1375
1376#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1377#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1378pub struct Slice {
1379  pub name: Identifier,
1380  pub subscript: Vec<Subscript>
1381}
1382
1383impl Slice {
1384  pub fn tokens(&self) -> Vec<Token> {
1385    let mut tkns = self.name.tokens();
1386    for sub in &self.subscript {
1387      let mut sub_tkns = sub.tokens();
1388      tkns.append(&mut sub_tkns);
1389    }
1390    tkns
1391  }
1392}
1393
1394#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1395#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1396pub struct SliceRef {
1397  pub name: Identifier,
1398  pub subscript: Option<Vec<Subscript>>
1399}
1400
1401impl SliceRef {
1402  pub fn tokens(&self) -> Vec<Token> {
1403    let mut tkns = self.name.tokens();
1404    if let Some(subs) = &self.subscript {
1405      for sub in subs {
1406        let mut sub_tkns = sub.tokens();
1407        tkns.append(&mut sub_tkns);
1408      }
1409    }
1410    tkns
1411  }
1412}
1413
1414#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1415#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1416pub enum Subscript {
1417  All,                      // a[:]
1418  Brace(Vec<Subscript>),    // a{"foo"}
1419  Bracket(Vec<Subscript>),  // a[1,2,3]
1420  Dot(Identifier),          // a.b
1421  DotInt(RealNumber),       // a.1
1422  Formula(Factor),          // a[1 + 1]
1423  Range(RangeExpression),   // a[1 + 1]
1424  Swizzle(Vec<Identifier>), // a.b,c
1425}
1426
1427impl Subscript {
1428
1429  pub fn tokens(&self) -> Vec<Token> {
1430    match self {
1431      Subscript::All => vec![
1432        Token::new(TokenKind::Colon, SourceRange::default(), vec![':']),
1433      ],
1434      Subscript::Brace(subs) => {
1435        let mut tkns = vec![Token::new(TokenKind::LeftBrace, SourceRange::default(), vec!['{'])];
1436        for sub in subs {
1437          let mut sub_tkns = sub.tokens();
1438          tkns.append(&mut sub_tkns);
1439        }
1440        tkns.push(Token::new(TokenKind::RightBrace, SourceRange::default(), vec!['}']));
1441        tkns
1442      },
1443      Subscript::Bracket(subs) => {
1444        let mut tkns = vec![Token::new(TokenKind::LeftBracket, SourceRange::default(), vec!['['])];
1445        for sub in subs {
1446          let mut sub_tkns = sub.tokens();
1447          tkns.append(&mut sub_tkns);
1448        }
1449        tkns.push(Token::new(TokenKind::RightBracket, SourceRange::default(), vec![']']));
1450        tkns
1451      },
1452      Subscript::Dot(id) => id.tokens(),
1453      Subscript::DotInt(num) => num.tokens(),
1454      Subscript::Formula(factor) => factor.tokens(),
1455      Subscript::Range(range) => range.tokens(),
1456      Subscript::Swizzle(ids) => ids.iter().flat_map(|id| id.tokens()).collect(),
1457    }
1458  }
1459}
1460
1461#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1462#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1463pub enum Expression {
1464  Formula(Factor),
1465  FunctionCall(FunctionCall),
1466  //FsmPipe(FsmPipe),
1467  Literal(Literal),
1468  Range(Box<RangeExpression>),
1469  Slice(Slice),
1470  Structure(Structure),
1471  SetComprehension(Box<SetComprehension>),
1472  Var(Var),
1473}
1474
1475impl Expression {
1476  pub fn tokens(&self) -> Vec<Token> {
1477    match self {
1478      Expression::Var(v) => v.tokens(),
1479      Expression::Literal(ltrl) => ltrl.tokens(),
1480      Expression::Structure(strct) => strct.tokens(),
1481      Expression::Formula(fctr) => fctr.tokens(),
1482      Expression::Range(range) => range.tokens(),
1483      Expression::Slice(slice) => slice.tokens(),
1484      Expression::SetComprehension(sc) => sc.tokens(),
1485      _ => todo!(),
1486    }
1487  }
1488}
1489
1490#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1491#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1492pub struct SetComprehension {
1493  pub expression: Expression,
1494  pub qualifiers: Vec<ComprehensionQualifier>,
1495}
1496
1497impl SetComprehension {
1498  pub fn tokens(&self) -> Vec<Token> {
1499    let mut tokens = self.expression.tokens();
1500    for qualifier in &self.qualifiers {
1501      tokens.append(&mut qualifier.tokens());
1502    }
1503    tokens
1504  }
1505}
1506
1507#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1508#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1509pub enum ComprehensionQualifier {
1510  Generator((Pattern,Expression)),
1511  Filter(Expression),
1512  Let(VariableDefine),
1513}
1514
1515impl ComprehensionQualifier {
1516  pub fn tokens(&self) -> Vec<Token> {
1517    match self {
1518      ComprehensionQualifier::Generator((pattern, expr)) => {
1519        let mut tokens = pattern.tokens();
1520        tokens.append(&mut expr.tokens());
1521        tokens
1522      }
1523      ComprehensionQualifier::Filter(expr) => expr.tokens(),
1524      ComprehensionQualifier::Let(var_def) => var_def.tokens(),
1525    }
1526  }
1527}
1528
1529pub type ArgumentList = Vec<(Option<Identifier>,Expression)>;
1530
1531#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1532#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1533pub struct FunctionCall {
1534  pub name: Identifier,
1535  pub args: ArgumentList,
1536}
1537
1538impl FunctionCall {
1539  pub fn tokens(&self) -> Vec<Token> {
1540    self.name.tokens()
1541  }
1542}
1543
1544#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1545#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1546pub struct Tuple {
1547  pub elements: Vec<Expression>
1548}
1549
1550impl Tuple {
1551  pub fn tokens(&self) -> Vec<Token> {
1552    let mut tokens = vec![];
1553    for elem in &self.elements {
1554      tokens.append(&mut elem.tokens());
1555    }
1556    tokens
1557  }
1558}
1559
1560#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1561#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1562pub struct Binding {
1563  pub name: Identifier,
1564  pub kind: Option<KindAnnotation>,
1565  pub value: Expression,
1566}
1567
1568impl Binding {
1569  pub fn tokens(&self) -> Vec<Token> {
1570    let mut tokens = self.name.tokens();
1571    if let Some(knd) = &self.kind {
1572      tokens.append(&mut knd.tokens());
1573    }
1574    tokens.append(&mut self.value.tokens());
1575    tokens
1576  }
1577}
1578
1579#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1580#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1581pub struct KindAnnotation {
1582  pub kind: Kind,
1583}
1584
1585impl KindAnnotation {
1586
1587  pub fn hash(&self) -> u64 {
1588    #[cfg(feature = "no_std")]
1589    let mut hasher = FxHasher::default();
1590    #[cfg(not(feature = "no_std"))]
1591    let mut hasher = std::collections::hash_map::DefaultHasher::new();
1592    self.kind.hash(&mut hasher);
1593    hasher.finish()
1594  }
1595
1596  pub fn tokens(&self) -> Vec<Token> {
1597    self.kind.tokens()
1598  }
1599}
1600
1601#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1602#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1603pub enum Kind {
1604  Any,
1605  Atom(Identifier),
1606  Table((Vec<(Identifier,Kind)>,Box<Literal>)),
1607  Set(Box<Kind>,Option<Box<Literal>>),
1608  Record((Vec<(Identifier,Kind)>)),
1609  Empty,
1610  //Fsm(Vec<Kind>,Vec<Kind>),
1611  //Function(Vec<Kind>,Vec<Kind>),
1612  Map(Box<Kind>,Box<Kind>),
1613  Matrix((Box<Kind>,Vec<Literal>)),
1614  Option(Box<Kind>),
1615  Scalar(Identifier),
1616  Tuple(Vec<Kind>),
1617}
1618
1619impl Kind {
1620  pub fn tokens(&self) -> Vec<Token> {
1621    match self {
1622      Kind::Option(x) => x.tokens(),
1623      Kind::Tuple(x) => x.iter().flat_map(|k| k.tokens()).collect(),
1624      Kind::Matrix((kind, literals)) => {
1625        let mut tokens = kind.tokens();
1626        for l in literals {
1627          tokens.append(&mut l.tokens());
1628        }
1629        tokens
1630      },
1631      Kind::Record(kinds) => {
1632        let mut tokens = vec![];
1633        for (id, kind) in kinds {
1634          tokens.append(&mut id.tokens());
1635          tokens.append(&mut kind.tokens());
1636        }
1637        tokens
1638      }
1639      Kind::Set(kind, size) => {
1640        let mut tokens = kind.tokens();
1641        if let Some(literal) = size {
1642          tokens.append(&mut literal.tokens());
1643        }
1644        tokens
1645      }
1646      Kind::Table((kinds, literal)) => {
1647        let mut tokens = vec![];
1648        for (id, kind) in kinds {
1649          tokens.append(&mut id.tokens());
1650          tokens.append(&mut kind.tokens());
1651        }
1652        tokens.append(&mut literal.tokens());
1653        tokens
1654      }
1655      Kind::Map(x, y) => x.tokens().into_iter().chain(y.tokens()).collect(),
1656      Kind::Scalar(x) => x.tokens(),
1657      Kind::Atom(x) => x.tokens(),
1658      Kind::Empty => vec![],
1659      Kind::Any => vec![],
1660    }
1661  }
1662}
1663
1664#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1665#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1666pub enum Literal {
1667  Atom(Atom),
1668  Boolean(Token),
1669  Empty(Token),
1670  Number(Number),
1671  String(MechString),
1672  Kind(Kind),
1673  TypedLiteral((Box<Literal>,KindAnnotation))
1674}
1675
1676impl Literal {
1677  pub fn tokens(&self) -> Vec<Token> {
1678    match self {
1679      Literal::Atom(atm) => atm.name.tokens(),
1680      Literal::Boolean(tkn) => vec![tkn.clone()],
1681      Literal::Number(x) => x.tokens(),
1682      Literal::String(strng) => vec![strng.text.clone()],
1683      Literal::Empty(tkn) => vec![tkn.clone()],
1684      Literal::Kind(knd) => knd.tokens(),
1685      Literal::TypedLiteral((lit, knd)) => {
1686        let mut tokens = lit.tokens();
1687        tokens.append(&mut knd.tokens());
1688        tokens
1689      }
1690    }
1691  }
1692}
1693
1694#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1695#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1696pub struct MechString {
1697  pub text: Token,
1698}
1699
1700impl MechString {
1701  pub fn to_string(&self) -> String {
1702    self.text.to_string()
1703  }
1704}
1705
1706pub type Hyperlink = (Paragraph, Token);
1707
1708#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1709#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1710pub enum ParagraphElement {
1711  Emphasis(Box<ParagraphElement>),
1712  FootnoteReference(Token),
1713  Highlight(Box<ParagraphElement>),
1714  Hyperlink(Hyperlink),
1715  InlineCode(Token),
1716  EvalInlineMechCode(Expression),
1717  InlineMechCode(MechCode),
1718  InlineEquation(Token),
1719  Reference(Token),
1720  SectionReference(Token),
1721  Strikethrough(Box<ParagraphElement>),
1722  Strong(Box<ParagraphElement>),
1723  Text(Token),
1724  Underline(Box<ParagraphElement>),
1725  Error(Token, SourceRange),
1726}
1727
1728impl Recoverable for ParagraphElement {
1729  fn error_placeholder(skipped_tokens: Token, range: SourceRange) -> Self {
1730    ParagraphElement::Error(skipped_tokens, range)
1731  }
1732}
1733
1734impl ParagraphElement {
1735
1736  pub fn tokens(&self) -> Vec<Token> {
1737    match self {
1738      ParagraphElement::Emphasis(t) => t.tokens(),
1739      ParagraphElement::FootnoteReference(t) => vec![t.clone()],
1740      ParagraphElement::Highlight(t) => t.tokens(),
1741      ParagraphElement::Hyperlink((t, u)) => {
1742        let mut tokens = t.tokens();
1743        tokens.push(u.clone());
1744        tokens
1745      },
1746      ParagraphElement::InlineCode(t) => vec![t.clone()],
1747      ParagraphElement::InlineEquation(t) => vec![t.clone()],
1748      ParagraphElement::InlineMechCode(t) => t.tokens(),
1749      ParagraphElement::EvalInlineMechCode(t) => t.tokens(),
1750      ParagraphElement::Reference(t) => vec![t.clone()],
1751      ParagraphElement::SectionReference(t) => vec![t.clone()],
1752      ParagraphElement::Strikethrough(t) => t.tokens(),
1753      ParagraphElement::Strong(t) => t.tokens(),
1754      ParagraphElement::Text(t) => vec![t.clone()],
1755      ParagraphElement::Underline(t) => t.tokens(),
1756      ParagraphElement::Error(t, _) => vec![t.clone()],
1757    }
1758  }
1759
1760  pub fn to_string(&self) -> String {
1761    match self {
1762      ParagraphElement::Emphasis(t) => t.to_string(),
1763      ParagraphElement::FootnoteReference(t) => t.to_string(),
1764      ParagraphElement::Highlight(t) => t.to_string(),
1765      ParagraphElement::Hyperlink((t, u)) => {
1766        format!("[{}]({})", t.to_string(), u.to_string())
1767      }
1768      ParagraphElement::InlineCode(t) => t.to_string(),
1769      ParagraphElement::InlineEquation(t) => t.to_string(),
1770      ParagraphElement::InlineMechCode(t) => format!("{:?}", t),
1771      ParagraphElement::EvalInlineMechCode(t) => format!("{:?}", t),
1772      ParagraphElement::Reference(t) => t.to_string(),
1773      ParagraphElement::SectionReference(t) => t.to_string(),
1774      ParagraphElement::Strikethrough(t) => t.to_string(),
1775      ParagraphElement::Strong(t) => t.to_string(),
1776      ParagraphElement::Text(t) => t.to_string(),
1777      ParagraphElement::Underline(t) => t.to_string(),
1778      ParagraphElement::Error(t, s) => format!("{{ERROR: {} at {:?}}}", t.to_string(), s),
1779    }
1780  }
1781
1782}
1783
1784#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1785#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1786pub struct Paragraph {
1787  pub elements: Vec<ParagraphElement>,
1788  pub error_range: Option<SourceRange>,
1789}
1790
1791impl Paragraph {
1792  pub fn to_string(&self) -> String {
1793    let mut out = "".to_string();
1794    for e in &self.elements {
1795      out.push_str(&e.to_string());
1796    }
1797    out
1798  }
1799
1800  pub fn from_tokens(tokens: Vec<Token>) -> Paragraph {
1801    let elements = tokens.into_iter().map(|t| ParagraphElement::Text(t)).collect();
1802    Paragraph {
1803      elements,
1804      error_range: None,
1805    }
1806  }
1807
1808  pub fn has_errors(&self) -> bool {
1809    self.error_range.is_some()
1810  }
1811
1812  pub fn tokens(&self) -> Vec<Token> {
1813    let mut tkns = vec![];
1814    for e in &self.elements {
1815      let mut e_tkns = e.tokens();
1816      tkns.append(&mut e_tkns);
1817    }
1818    tkns
1819  }
1820
1821}
1822
1823impl Recoverable for Paragraph {
1824  fn error_placeholder(skipped_tokens: Token, range: SourceRange) -> Self {
1825    Paragraph {
1826      elements: vec![ParagraphElement::Text(Token::new(TokenKind::Error, range.clone(), skipped_tokens.chars.clone()))],
1827      error_range: Some(range),
1828    }
1829  }
1830}
1831
1832
1833pub type Sign = bool;
1834pub type Numerator = Token;
1835pub type Denominator = Token;
1836pub type Whole = Token;
1837pub type Part = Token;
1838pub type Real = Box<Number>;
1839pub type Imaginary = Box<Number>;
1840pub type Base = (Whole, Part);
1841pub type Exponent = (Sign, Whole, Part);
1842
1843#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1844#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1845pub enum Number {
1846  Real(RealNumber),
1847  Complex(C64Node),
1848}
1849
1850impl Number {
1851
1852  pub fn from_integer(x: i64) -> Number {
1853    Number::Real(RealNumber::Integer(Token::new(TokenKind::Digit, SourceRange::default(), x.to_string().chars().collect())))
1854  }
1855
1856  pub fn to_string(&self) -> String {
1857    match self {
1858      Number::Real(x) => x.to_string(),
1859      Number::Complex(x) => x.to_string(),
1860    }
1861  }
1862
1863  pub fn tokens(&self) -> Vec<Token> {
1864    match self {
1865      Number::Real(x) => x.tokens(),
1866      Number::Complex(x) => x.tokens(),
1867    }
1868  }
1869}
1870
1871#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1872#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1873pub enum RealNumber {
1874  Binary(Token),
1875  Decimal(Token),
1876  Float((Whole,Part)),
1877  Hexadecimal(Token),
1878  Integer(Token),
1879  Negated(Box<RealNumber>),
1880  Octal(Token),
1881  Rational((Numerator,Denominator)),
1882  Scientific((Base,Exponent)),
1883}
1884
1885impl RealNumber {
1886  pub fn tokens(&self) -> Vec<Token> {
1887    match self {
1888      RealNumber::Integer(tkn) => vec![tkn.clone()],
1889      _ => todo!(),
1890    }
1891  }
1892  pub fn to_string(&self) -> String {
1893    match self {
1894      RealNumber::Integer(tkn) => tkn.to_string(),
1895      RealNumber::Float((whole, part)) => format!("{}.{}", whole.to_string(), part.to_string()),
1896      RealNumber::Binary(tkn) => format!("0b{}", tkn.to_string()),
1897      RealNumber::Hexadecimal(tkn) => format!("0x{}", tkn.to_string()),
1898      RealNumber::Octal(tkn) => format!("0o{}", tkn.to_string()),
1899      RealNumber::Decimal(tkn) => format!("0d{}", tkn.to_string()),
1900      RealNumber::Rational((num, den)) => format!("{}/{}", num.to_string(), den.to_string()),
1901      RealNumber::Scientific(((whole,part), exponent)) => {
1902        let (sign, whole, part) = exponent;
1903        let sign_str = if *sign { "+" } else { "-" };
1904        let whole_str = whole.to_string();
1905        let part_str = part.to_string();
1906        format!("{}{}.{}/10^{}", whole.to_string(), part.to_string(), sign_str, whole_str)
1907      }
1908      RealNumber::Negated(x) => format!("-{}", x.to_string()),
1909    }
1910  }
1911}
1912
1913#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1914#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1915pub struct ImaginaryNumber {
1916  pub number: RealNumber,
1917}
1918
1919#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1920#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1921pub struct C64Node {
1922  pub real: Option<RealNumber>,
1923  pub imaginary: ImaginaryNumber
1924}
1925
1926impl C64Node {
1927  pub fn tokens(&self) -> Vec<Token> {
1928    let mut tkns = vec![];
1929    if let Some(r) = &self.real {
1930      tkns.append(&mut r.tokens());
1931    }
1932    tkns.append(&mut self.imaginary.number.tokens());
1933    tkns
1934  }
1935
1936  pub fn to_string(&self) -> String {
1937    let mut out = "".to_string();
1938    if let Some(r) = &self.real {
1939      out.push_str(&r.to_string());
1940    }
1941    out.push_str("i");
1942    out
1943  }
1944}
1945
1946#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1947#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1948pub struct Comment {
1949  pub paragraph: Paragraph,
1950}
1951
1952impl Comment {
1953  pub fn tokens(&self) -> Vec<Token> {
1954    self.paragraph.tokens()
1955  }
1956}
1957
1958#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1959#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1960pub struct OpAssign {
1961  pub target: SliceRef,
1962  pub op: OpAssignOp,
1963  pub expression: Expression,
1964}
1965
1966impl OpAssign {
1967  pub fn tokens(&self) -> Vec<Token> {
1968    let mut tkns = self.target.tokens();
1969    tkns.append(&mut self.op.tokens());
1970    tkns.append(&mut self.expression.tokens());
1971    tkns
1972  }
1973}
1974
1975#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1976#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1977pub enum OpAssignOp {
1978  Add,
1979  Div,
1980  Exp, 
1981  Mod,  
1982  Mul,
1983  Sub,   
1984}
1985
1986impl OpAssignOp {
1987  pub fn tokens(&self) -> Vec<Token> {
1988    match self {
1989      OpAssignOp::Add => vec![Token::new(TokenKind::Plus, SourceRange::default(), vec!['+'])],
1990      OpAssignOp::Div => vec![Token::new(TokenKind::Slash, SourceRange::default(), vec!['/'])],
1991      OpAssignOp::Exp => vec![Token::new(TokenKind::Caret, SourceRange::default(), vec!['^'])],
1992      OpAssignOp::Mod => vec![Token::new(TokenKind::Percent, SourceRange::default(), vec!['%'])],
1993      OpAssignOp::Mul => vec![Token::new(TokenKind::Asterisk, SourceRange::default(), vec!['*'])],
1994      OpAssignOp::Sub => vec![Token::new(TokenKind::Dash, SourceRange::default(), vec!['-'])],
1995    }
1996  }
1997}
1998
1999#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2000#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2001pub enum RangeOp {
2002  Exclusive,      
2003  Inclusive,
2004}
2005
2006#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2007#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2008pub enum AddSubOp {
2009  Add,
2010  Sub,
2011}
2012
2013#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2014#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2015pub enum MulDivOp {
2016  Div,
2017  Mod,
2018  Mul,
2019}
2020
2021#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2022#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2023pub enum VecOp {
2024  Cross,
2025  Dot,
2026  MatMul,
2027  Solve,
2028}
2029
2030#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2031#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2032pub enum PowerOp {
2033  Pow
2034}
2035
2036#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2037#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2038pub enum ComparisonOp {
2039  Equal,
2040  GreaterThan,
2041  GreaterThanEqual,
2042  LessThan,
2043  LessThanEqual,
2044  NotEqual,
2045  StrictEqual,
2046  StrictNotEqual,
2047}
2048
2049#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2050#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2051pub enum LogicOp {
2052  And,
2053  Not,
2054  Or,
2055  Xor,
2056}
2057
2058#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2059#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2060pub enum FormulaOperator {
2061  AddSub(AddSubOp),
2062  Comparison(ComparisonOp),
2063  Power(PowerOp),
2064  Logic(LogicOp),
2065  MulDiv(MulDivOp),
2066  Vec(VecOp),
2067  Table(TableOp),
2068  Set(SetOp),
2069}
2070
2071#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2072#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2073pub enum TableOp {
2074  InnerJoin,
2075  LeftOuterJoin,
2076  RightOuterJoin	,
2077  FullOuterJoin	,
2078  LeftSemiJoin,  
2079  LeftAntiJoin,
2080}
2081
2082#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2083#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2084pub enum SetOp {
2085  Union,
2086  Intersection,
2087  Difference,
2088  Complement,
2089  ElementOf,
2090  NotElementOf,
2091  ProperSubset,
2092  ProperSuperset,
2093  Subset,
2094  Superset,
2095  SymmetricDifference,
2096}
2097
2098#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2099#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2100pub struct RangeExpression {
2101  pub start: Factor,
2102  pub increment: Option<(RangeOp,Factor)>,
2103  pub operator: RangeOp,
2104  pub terminal: Factor,
2105}
2106
2107impl RangeExpression {
2108  pub fn tokens(&self) -> Vec<Token> {
2109    let mut tokens = self.start.tokens();
2110    tokens.append(&mut self.terminal.tokens());
2111    tokens
2112  }
2113}
2114
2115#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2116#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2117pub struct Term {
2118  pub lhs: Factor,
2119  pub rhs: Vec<(FormulaOperator,Factor)>
2120}
2121
2122impl Term {
2123  pub fn tokens(&self) -> Vec<Token> {
2124    let mut lhs_tkns = self.lhs.tokens();
2125    let mut rhs_tkns = vec![];
2126    for (op, r) in &self.rhs {
2127      let mut tkns = r.tokens();
2128      rhs_tkns.append(&mut tkns);
2129    }
2130    lhs_tkns.append(&mut rhs_tkns);
2131    lhs_tkns
2132  }
2133}
2134
2135#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2136#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2137pub enum Factor {
2138  Expression(Box<Expression>),
2139  Negate(Box<Factor>),
2140  Not(Box<Factor>),
2141  Parenthetical(Box<Factor>),
2142  Term(Box<Term>),
2143  Transpose(Box<Factor>),
2144}
2145
2146impl Factor {
2147  pub fn tokens(&self) -> Vec<Token> {
2148    match self {
2149      Factor::Expression(x) => x.tokens(),
2150      Factor::Negate(x) => x.tokens(),
2151      Factor::Not(x) => x.tokens(),
2152      Factor::Parenthetical(x) => x.tokens(),
2153      Factor::Term(x) => x.tokens(),
2154      Factor::Transpose(x) => x.tokens(),
2155    }
2156  }
2157}