Skip to main content

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