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, EscapedChar, 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  Kind(Box<Kind>),
1618}
1619
1620impl Kind {
1621  pub fn tokens(&self) -> Vec<Token> {
1622    match self {
1623      Kind::Option(x) => x.tokens(),
1624      Kind::Tuple(x) => x.iter().flat_map(|k| k.tokens()).collect(),
1625      Kind::Matrix((kind, literals)) => {
1626        let mut tokens = kind.tokens();
1627        for l in literals {
1628          tokens.append(&mut l.tokens());
1629        }
1630        tokens
1631      },
1632      Kind::Record(kinds) => {
1633        let mut tokens = vec![];
1634        for (id, kind) in kinds {
1635          tokens.append(&mut id.tokens());
1636          tokens.append(&mut kind.tokens());
1637        }
1638        tokens
1639      }
1640      Kind::Set(kind, size) => {
1641        let mut tokens = kind.tokens();
1642        if let Some(literal) = size {
1643          tokens.append(&mut literal.tokens());
1644        }
1645        tokens
1646      }
1647      Kind::Table((kinds, literal)) => {
1648        let mut tokens = vec![];
1649        for (id, kind) in kinds {
1650          tokens.append(&mut id.tokens());
1651          tokens.append(&mut kind.tokens());
1652        }
1653        tokens.append(&mut literal.tokens());
1654        tokens
1655      }
1656      Kind::Map(x, y) => x.tokens().into_iter().chain(y.tokens()).collect(),
1657      Kind::Scalar(x) => x.tokens(),
1658      Kind::Atom(x) => x.tokens(),
1659      Kind::Kind(x) => x.tokens(),
1660      Kind::Empty => vec![],
1661      Kind::Any => vec![],
1662    }
1663  }
1664}
1665
1666#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1667#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1668pub enum Literal {
1669  Atom(Atom),
1670  Boolean(Token),
1671  Empty(Token),
1672  Number(Number),
1673  String(MechString),
1674  Kind(Kind),
1675  TypedLiteral((Box<Literal>,KindAnnotation))
1676}
1677
1678impl Literal {
1679  pub fn tokens(&self) -> Vec<Token> {
1680    match self {
1681      Literal::Atom(atm) => atm.name.tokens(),
1682      Literal::Boolean(tkn) => vec![tkn.clone()],
1683      Literal::Number(x) => x.tokens(),
1684      Literal::String(strng) => vec![strng.text.clone()],
1685      Literal::Empty(tkn) => vec![tkn.clone()],
1686      Literal::Kind(knd) => knd.tokens(),
1687      Literal::TypedLiteral((lit, knd)) => {
1688        let mut tokens = lit.tokens();
1689        tokens.append(&mut knd.tokens());
1690        tokens
1691      }
1692    }
1693  }
1694}
1695
1696#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1697#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1698pub struct MechString {
1699  pub text: Token,
1700}
1701
1702impl MechString {
1703  pub fn to_string(&self) -> String {
1704    self.text.to_string()
1705  }
1706}
1707
1708pub type Hyperlink = (Paragraph, Token);
1709
1710#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1711#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1712pub enum ParagraphElement {
1713  Emphasis(Box<ParagraphElement>),
1714  FootnoteReference(Token),
1715  Highlight(Box<ParagraphElement>),
1716  Hyperlink(Hyperlink),
1717  InlineCode(Token),
1718  EvalInlineMechCode(Expression),
1719  InlineMechCode(MechCode),
1720  InlineEquation(Token),
1721  Reference(Token),
1722  SectionReference(Token),
1723  Strikethrough(Box<ParagraphElement>),
1724  Strong(Box<ParagraphElement>),
1725  Text(Token),
1726  Underline(Box<ParagraphElement>),
1727  Error(Token, SourceRange),
1728}
1729
1730impl Recoverable for ParagraphElement {
1731  fn error_placeholder(skipped_tokens: Token, range: SourceRange) -> Self {
1732    ParagraphElement::Error(skipped_tokens, range)
1733  }
1734}
1735
1736impl ParagraphElement {
1737
1738  pub fn tokens(&self) -> Vec<Token> {
1739    match self {
1740      ParagraphElement::Emphasis(t) => t.tokens(),
1741      ParagraphElement::FootnoteReference(t) => vec![t.clone()],
1742      ParagraphElement::Highlight(t) => t.tokens(),
1743      ParagraphElement::Hyperlink((t, u)) => {
1744        let mut tokens = t.tokens();
1745        tokens.push(u.clone());
1746        tokens
1747      },
1748      ParagraphElement::InlineCode(t) => vec![t.clone()],
1749      ParagraphElement::InlineEquation(t) => vec![t.clone()],
1750      ParagraphElement::InlineMechCode(t) => t.tokens(),
1751      ParagraphElement::EvalInlineMechCode(t) => t.tokens(),
1752      ParagraphElement::Reference(t) => vec![t.clone()],
1753      ParagraphElement::SectionReference(t) => vec![t.clone()],
1754      ParagraphElement::Strikethrough(t) => t.tokens(),
1755      ParagraphElement::Strong(t) => t.tokens(),
1756      ParagraphElement::Text(t) => vec![t.clone()],
1757      ParagraphElement::Underline(t) => t.tokens(),
1758      ParagraphElement::Error(t, _) => vec![t.clone()],
1759    }
1760  }
1761
1762  pub fn to_string(&self) -> String {
1763    match self {
1764      ParagraphElement::Emphasis(t) => t.to_string(),
1765      ParagraphElement::FootnoteReference(t) => t.to_string(),
1766      ParagraphElement::Highlight(t) => t.to_string(),
1767      ParagraphElement::Hyperlink((t, u)) => {
1768        format!("[{}]({})", t.to_string(), u.to_string())
1769      }
1770      ParagraphElement::InlineCode(t) => t.to_string(),
1771      ParagraphElement::InlineEquation(t) => t.to_string(),
1772      ParagraphElement::InlineMechCode(t) => format!("{:?}", t),
1773      ParagraphElement::EvalInlineMechCode(t) => format!("{:?}", t),
1774      ParagraphElement::Reference(t) => t.to_string(),
1775      ParagraphElement::SectionReference(t) => t.to_string(),
1776      ParagraphElement::Strikethrough(t) => t.to_string(),
1777      ParagraphElement::Strong(t) => t.to_string(),
1778      ParagraphElement::Text(t) => t.to_string(),
1779      ParagraphElement::Underline(t) => t.to_string(),
1780      ParagraphElement::Error(t, s) => format!("{{ERROR: {} at {:?}}}", t.to_string(), s),
1781    }
1782  }
1783
1784}
1785
1786#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1787#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1788pub struct Paragraph {
1789  pub elements: Vec<ParagraphElement>,
1790  pub error_range: Option<SourceRange>,
1791}
1792
1793impl Paragraph {
1794  pub fn to_string(&self) -> String {
1795    let mut out = "".to_string();
1796    for e in &self.elements {
1797      out.push_str(&e.to_string());
1798    }
1799    out
1800  }
1801
1802  pub fn from_tokens(tokens: Vec<Token>) -> Paragraph {
1803    let elements = tokens.into_iter().map(|t| ParagraphElement::Text(t)).collect();
1804    Paragraph {
1805      elements,
1806      error_range: None,
1807    }
1808  }
1809
1810  pub fn has_errors(&self) -> bool {
1811    self.error_range.is_some()
1812  }
1813
1814  pub fn tokens(&self) -> Vec<Token> {
1815    let mut tkns = vec![];
1816    for e in &self.elements {
1817      let mut e_tkns = e.tokens();
1818      tkns.append(&mut e_tkns);
1819    }
1820    tkns
1821  }
1822
1823}
1824
1825impl Recoverable for Paragraph {
1826  fn error_placeholder(skipped_tokens: Token, range: SourceRange) -> Self {
1827    Paragraph {
1828      elements: vec![ParagraphElement::Text(Token::new(TokenKind::Error, range.clone(), skipped_tokens.chars.clone()))],
1829      error_range: Some(range),
1830    }
1831  }
1832}
1833
1834
1835pub type Sign = bool;
1836pub type Numerator = Token;
1837pub type Denominator = Token;
1838pub type Whole = Token;
1839pub type Part = Token;
1840pub type Real = Box<Number>;
1841pub type Imaginary = Box<Number>;
1842pub type Base = (Whole, Part);
1843pub type Exponent = (Sign, Whole, Part);
1844
1845#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1846#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1847pub enum Number {
1848  Real(RealNumber),
1849  Complex(C64Node),
1850}
1851
1852impl Number {
1853
1854  pub fn from_integer(x: i64) -> Number {
1855    Number::Real(RealNumber::Integer(Token::new(TokenKind::Digit, SourceRange::default(), x.to_string().chars().collect())))
1856  }
1857
1858  pub fn to_string(&self) -> String {
1859    match self {
1860      Number::Real(x) => x.to_string(),
1861      Number::Complex(x) => x.to_string(),
1862    }
1863  }
1864
1865  pub fn tokens(&self) -> Vec<Token> {
1866    match self {
1867      Number::Real(x) => x.tokens(),
1868      Number::Complex(x) => x.tokens(),
1869    }
1870  }
1871}
1872
1873#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1874#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1875pub enum RealNumber {
1876  Binary(Token),
1877  Decimal(Token),
1878  Float((Whole,Part)),
1879  Hexadecimal(Token),
1880  Integer(Token),
1881  TypedInteger((Token,KindAnnotation)),
1882  Negated(Box<RealNumber>),
1883  Octal(Token),
1884  Rational((Numerator,Denominator)),
1885  Scientific((Base,Exponent)),
1886}
1887
1888impl RealNumber {
1889  pub fn tokens(&self) -> Vec<Token> {
1890    match self {
1891      RealNumber::Integer(tkn) => vec![tkn.clone()],
1892      RealNumber::TypedInteger((tkn, _)) => vec![tkn.clone()],
1893      RealNumber::Float((whole, part)) => vec![whole.clone(), part.clone()],
1894      RealNumber::Binary(tkn) => vec![tkn.clone()],
1895      RealNumber::Hexadecimal(tkn) => vec![tkn.clone()],
1896      RealNumber::Octal(tkn) => vec![tkn.clone()],
1897      RealNumber::Decimal(tkn) => vec![tkn.clone()],
1898      RealNumber::Rational((num, den)) => vec![num.clone(), den.clone()],
1899      RealNumber::Scientific(((whole,part), exponent)) => {
1900        let mut tokens = vec![whole.clone(), part.clone()];
1901        let (sign, whole_exp, part_exp) = exponent;
1902        tokens.push(Token::new(TokenKind::Plus, SourceRange::default(), if *sign { vec!['+'] } else { vec!['-'] }));
1903        tokens.push(whole_exp.clone());
1904        tokens.push(part_exp.clone());
1905        tokens
1906      }
1907      RealNumber::Negated(x) => {
1908        let mut tokens = vec![Token::new(TokenKind::Dash, SourceRange::default(), vec!['-'])];
1909        tokens.append(&mut x.tokens());
1910        tokens
1911      }
1912    }
1913  }
1914  pub fn to_string(&self) -> String {
1915    match self {
1916      RealNumber::Integer(tkn) => tkn.to_string(),
1917      RealNumber::Float((whole, part)) => format!("{}.{}", whole.to_string(), part.to_string()),
1918      RealNumber::Binary(tkn) => format!("0b{}", tkn.to_string()),
1919      RealNumber::Hexadecimal(tkn) => format!("0x{}", tkn.to_string()),
1920      RealNumber::Octal(tkn) => format!("0o{}", tkn.to_string()),
1921      RealNumber::Decimal(tkn) => format!("0d{}", tkn.to_string()),
1922      RealNumber::Rational((num, den)) => format!("{}/{}", num.to_string(), den.to_string()),
1923      RealNumber::Scientific(((whole,part), exponent)) => {
1924        let (sign, whole, part) = exponent;
1925        let sign_str = if *sign { "+" } else { "-" };
1926        let whole_str = whole.to_string();
1927        let part_str = part.to_string();
1928        format!("{}{}.{}/10^{}", whole.to_string(), part.to_string(), sign_str, whole_str)
1929      }
1930      RealNumber::TypedInteger((tkn, kind)) => format!("{}{}", tkn.to_string(), kind.kind.tokens().iter().map(|t| t.to_string()).collect::<String>()),
1931      RealNumber::Negated(x) => format!("-{}", x.to_string()),
1932    }
1933  }
1934}
1935
1936#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1937#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1938pub struct ImaginaryNumber {
1939  pub number: RealNumber,
1940}
1941
1942#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1943#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1944pub struct C64Node {
1945  pub real: Option<RealNumber>,
1946  pub imaginary: ImaginaryNumber
1947}
1948
1949impl C64Node {
1950  pub fn tokens(&self) -> Vec<Token> {
1951    let mut tkns = vec![];
1952    if let Some(r) = &self.real {
1953      tkns.append(&mut r.tokens());
1954    }
1955    tkns.append(&mut self.imaginary.number.tokens());
1956    tkns
1957  }
1958
1959  pub fn to_string(&self) -> String {
1960    let mut out = "".to_string();
1961    if let Some(r) = &self.real {
1962      out.push_str(&r.to_string());
1963    }
1964    out.push_str("i");
1965    out
1966  }
1967}
1968
1969#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1970#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1971pub struct Comment {
1972  pub paragraph: Paragraph,
1973}
1974
1975impl Comment {
1976  pub fn tokens(&self) -> Vec<Token> {
1977    self.paragraph.tokens()
1978  }
1979}
1980
1981#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1982#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1983pub struct OpAssign {
1984  pub target: SliceRef,
1985  pub op: OpAssignOp,
1986  pub expression: Expression,
1987}
1988
1989impl OpAssign {
1990  pub fn tokens(&self) -> Vec<Token> {
1991    let mut tkns = self.target.tokens();
1992    tkns.append(&mut self.op.tokens());
1993    tkns.append(&mut self.expression.tokens());
1994    tkns
1995  }
1996}
1997
1998#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1999#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2000pub enum OpAssignOp {
2001  Add,
2002  Div,
2003  Exp, 
2004  Mod,  
2005  Mul,
2006  Sub,   
2007}
2008
2009impl OpAssignOp {
2010  pub fn tokens(&self) -> Vec<Token> {
2011    match self {
2012      OpAssignOp::Add => vec![Token::new(TokenKind::Plus, SourceRange::default(), vec!['+'])],
2013      OpAssignOp::Div => vec![Token::new(TokenKind::Slash, SourceRange::default(), vec!['/'])],
2014      OpAssignOp::Exp => vec![Token::new(TokenKind::Caret, SourceRange::default(), vec!['^'])],
2015      OpAssignOp::Mod => vec![Token::new(TokenKind::Percent, SourceRange::default(), vec!['%'])],
2016      OpAssignOp::Mul => vec![Token::new(TokenKind::Asterisk, SourceRange::default(), vec!['*'])],
2017      OpAssignOp::Sub => vec![Token::new(TokenKind::Dash, SourceRange::default(), vec!['-'])],
2018    }
2019  }
2020}
2021
2022#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2023#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2024pub enum RangeOp {
2025  Exclusive,      
2026  Inclusive,
2027}
2028
2029#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2030#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2031pub enum AddSubOp {
2032  Add,
2033  Sub,
2034}
2035
2036#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2037#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2038pub enum MulDivOp {
2039  Div,
2040  Mod,
2041  Mul,
2042}
2043
2044#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2045#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2046pub enum VecOp {
2047  Cross,
2048  Dot,
2049  MatMul,
2050  Solve,
2051}
2052
2053#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2054#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2055pub enum PowerOp {
2056  Pow
2057}
2058
2059#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2060#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2061pub enum ComparisonOp {
2062  Equal,
2063  GreaterThan,
2064  GreaterThanEqual,
2065  LessThan,
2066  LessThanEqual,
2067  NotEqual,
2068  StrictEqual,
2069  StrictNotEqual,
2070}
2071
2072#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2073#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2074pub enum LogicOp {
2075  And,
2076  Not,
2077  Or,
2078  Xor,
2079}
2080
2081#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2082#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2083pub enum FormulaOperator {
2084  AddSub(AddSubOp),
2085  Comparison(ComparisonOp),
2086  Power(PowerOp),
2087  Logic(LogicOp),
2088  MulDiv(MulDivOp),
2089  Vec(VecOp),
2090  Table(TableOp),
2091  Set(SetOp),
2092}
2093
2094#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2095#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2096pub enum TableOp {
2097  InnerJoin,
2098  LeftOuterJoin,
2099  RightOuterJoin	,
2100  FullOuterJoin	,
2101  LeftSemiJoin,  
2102  LeftAntiJoin,
2103}
2104
2105#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2106#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2107pub enum SetOp {
2108  Union,
2109  Intersection,
2110  Difference,
2111  Complement,
2112  ElementOf,
2113  NotElementOf,
2114  ProperSubset,
2115  ProperSuperset,
2116  Subset,
2117  Superset,
2118  SymmetricDifference,
2119}
2120
2121#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2122#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2123pub struct RangeExpression {
2124  pub start: Factor,
2125  pub increment: Option<(RangeOp,Factor)>,
2126  pub operator: RangeOp,
2127  pub terminal: Factor,
2128}
2129
2130impl RangeExpression {
2131  pub fn tokens(&self) -> Vec<Token> {
2132    let mut tokens = self.start.tokens();
2133    tokens.append(&mut self.terminal.tokens());
2134    tokens
2135  }
2136}
2137
2138#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2139#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2140pub struct Term {
2141  pub lhs: Factor,
2142  pub rhs: Vec<(FormulaOperator,Factor)>
2143}
2144
2145impl Term {
2146  pub fn tokens(&self) -> Vec<Token> {
2147    let mut lhs_tkns = self.lhs.tokens();
2148    let mut rhs_tkns = vec![];
2149    for (op, r) in &self.rhs {
2150      let mut tkns = r.tokens();
2151      rhs_tkns.append(&mut tkns);
2152    }
2153    lhs_tkns.append(&mut rhs_tkns);
2154    lhs_tkns
2155  }
2156}
2157
2158#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2159#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2160pub enum Factor {
2161  Expression(Box<Expression>),
2162  Negate(Box<Factor>),
2163  Not(Box<Factor>),
2164  Parenthetical(Box<Factor>),
2165  Term(Box<Term>),
2166  Transpose(Box<Factor>),
2167}
2168
2169impl Factor {
2170  pub fn tokens(&self) -> Vec<Token> {
2171    match self {
2172      Factor::Expression(x) => x.tokens(),
2173      Factor::Negate(x) => x.tokens(),
2174      Factor::Not(x) => x.tokens(),
2175      Factor::Parenthetical(x) => x.tokens(),
2176      Factor::Term(x) => x.tokens(),
2177      Factor::Transpose(x) => x.tokens(),
2178    }
2179  }
2180}