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  GenOperator, 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, PromptSigil,
112  Question, QuestionSigil, Quote, QuoteSigil,
113  RightAngle, RightBrace, RightBracket, RightParenthesis,
114  SectionSigil, Semicolon, Space, Slash, String, StrikeSigil, StrongSigil, SuccessSigil, SynthOperator,
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  Prompt(Box<SectionElement>),
546  ThematicBreak,
547  Error(Token, SourceRange),
548}
549
550impl Recoverable for SectionElement {
551  fn error_placeholder(skipped_tokens: Token, range: SourceRange) -> Self {
552    SectionElement::Error(skipped_tokens, range)
553  }
554}
555
556impl SectionElement {
557
558  pub fn tokens(&self) -> Vec<Token> {
559    match self {
560      SectionElement::Prompt(prompt) => prompt.tokens(),
561      #[cfg(feature = "mika")]
562      SectionElement::Mika((mika, mika_section)) => {
563        let mut tokens = vec![];
564        tokens.append(&mut mika.tokens());
565        if let Some(mika_section) = mika_section {
566          for mika_section_element in &mika_section.elements.elements {
567            tokens.append(&mut mika_section_element.tokens());
568          }
569        }
570        tokens
571      },
572      SectionElement::FencedMechCode(c) => {
573        let mut tokens = vec![];
574        for (c, _) in &c.code {
575          tokens.append(&mut c.tokens());
576        }
577        tokens
578      }
579      SectionElement::MechCode(codes) => {
580        let mut tokens = vec![];
581        for (code, _) in codes {
582          tokens.append(&mut code.tokens());
583        }
584        tokens
585      }
586      SectionElement::Abstract(paragraphs)
587      | SectionElement::QuoteBlock(paragraphs)
588      | SectionElement::InfoBlock(paragraphs)
589      | SectionElement::SuccessBlock(paragraphs)
590      | SectionElement::IdeaBlock(paragraphs)
591      | SectionElement::WarningBlock(paragraphs)
592      | SectionElement::ErrorBlock(paragraphs)
593      | SectionElement::QuestionBlock(paragraphs) => {
594        let mut tokens = vec![];
595        for paragraph in paragraphs {
596          tokens.append(&mut paragraph.tokens());
597        }
598        tokens
599      }
600      SectionElement::Citation(citation) => citation.text.tokens(),
601      SectionElement::CodeBlock(token)
602      | SectionElement::Diagram(token)
603      | SectionElement::Equation(token) => vec![token.clone()],
604      SectionElement::Comment(comment) => comment.tokens(),
605      SectionElement::Float((element, _)) => element.tokens(),
606      SectionElement::Footnote((_, paragraphs)) => {
607        let mut tokens = vec![];
608        for paragraph in paragraphs {
609          tokens.append(&mut paragraph.tokens());
610        }
611        tokens
612      }
613        SectionElement::Grammar(grammar) => {
614        let mut tokens = vec![];
615        for rule in &grammar.rules {
616          tokens.append(&mut rule.name.tokens());
617          tokens.append(&mut rule.expr.tokens());
618        }
619        tokens
620      }
621      SectionElement::Image(image) => {
622        let mut tokens = vec![image.src.clone()];
623        if let Some(caption) = &image.caption {
624          tokens.append(&mut caption.tokens());
625        }
626        tokens
627      }
628      SectionElement::List(list) => match list {
629      MDList::Unordered(items) => {
630        let mut tokens = vec![];
631        for ((_, paragraph), sublist) in items {
632        tokens.append(&mut paragraph.tokens());
633        if let Some(sublist) = sublist {
634          tokens.append(&mut sublist.tokens());
635        }
636        }
637        tokens
638      }
639      MDList::Ordered(ordered_list) => {
640        let mut tokens = ordered_list.start.tokens();
641        for ((_, paragraph), sublist) in &ordered_list.items {
642        tokens.append(&mut paragraph.tokens());
643        if let Some(sublist) = sublist {
644          tokens.append(&mut sublist.tokens());
645        }
646        }
647        tokens
648      }
649      MDList::Check(items) => {
650        let mut tokens = vec![];
651        for ((_, paragraph), sublist) in items {
652        tokens.append(&mut paragraph.tokens());
653        if let Some(sublist) = sublist {
654          tokens.append(&mut sublist.tokens());
655        }
656        }
657        tokens
658      }
659      },
660      SectionElement::Paragraph(paragraph) => paragraph.tokens(),
661      SectionElement::Subtitle(subtitle) => subtitle.text.tokens(),
662      SectionElement::Table(table) => {
663        let mut tokens = vec![];
664        for header in &table.header {
665          tokens.append(&mut header.tokens());
666        }
667        for row in &table.rows {
668          for column in row {
669          tokens.append(&mut column.tokens());
670          }
671        }
672        tokens
673      }
674      SectionElement::ThematicBreak => vec![],
675      SectionElement::Error(token, _) => vec![token.clone()],
676    }
677  }
678}
679
680
681#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
682#[derive(Clone, Debug, Hash, PartialEq, Eq)]
683pub struct OptionMap {
684  pub elements: Vec<(Identifier, MechString)>,
685}
686
687
688#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
689#[derive(Clone, Debug, Hash, PartialEq, Eq)]
690pub struct Image {
691  pub src: Token,
692  pub caption: Option<Paragraph>,
693  pub style: Option<OptionMap>,
694}
695
696impl Image {
697  pub fn to_string(&self) -> String {
698    let caption = match &self.caption {
699      Some(c) => c.to_string(),
700      None => "".to_string(),
701    };
702    format!("![{}]({})", caption, self.src.to_string())
703  }
704}
705
706pub type UnorderedList = Vec<((Option<Token>,Paragraph),Option<MDList>)>;
707pub type CheckList = Vec<((bool,Paragraph),Option<MDList>)>;
708
709#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
710#[derive(Clone, Debug, Hash, PartialEq, Eq)]
711pub struct OrderedList {
712  pub start: Number,
713  pub items: Vec<((Number,Paragraph),Option<MDList>)>,
714}
715
716#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
717#[derive(Clone, Debug, Hash, PartialEq, Eq)]
718pub enum MDList {
719  Unordered(UnorderedList),
720  Ordered(OrderedList),
721  Check(CheckList)
722}
723
724impl MDList {
725
726  pub fn tokens(&self) -> Vec<Token> {
727    match self {
728      MDList::Unordered(items) => {
729        let mut tokens = vec![];
730        for ((_, paragraph), sublist) in items {
731          tokens.append(&mut paragraph.tokens());
732          if let Some(sublist) = sublist {
733            tokens.append(&mut sublist.tokens());
734          }
735        }
736        tokens
737      }
738      MDList::Ordered(ordered_list) => {
739        let mut tokens = ordered_list.start.tokens();
740        for ((_, paragraph), sublist) in &ordered_list.items {
741          tokens.append(&mut paragraph.tokens());
742          if let Some(sublist) = sublist {
743            tokens.append(&mut sublist.tokens());
744          }
745        }
746        tokens
747      }
748      MDList::Check(items) => {
749        let mut tokens = vec![];
750        for ((_, paragraph), sublist) in items {
751          tokens.append(&mut paragraph.tokens());
752          if let Some(sublist) = sublist {
753            tokens.append(&mut sublist.tokens());
754          }
755        }
756        tokens
757      }
758    }
759  }
760
761}
762
763#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
764#[derive(Clone, Debug, Hash, PartialEq, Eq)]
765pub enum MechCode {
766  Comment(Comment),
767  Expression(Expression),
768  //FsmImplementation(FsmImplementation),
769  //FsmSpecification(FsmSpecification),
770  FunctionDefine(FunctionDefine),
771  Statement(Statement),
772  Error(Token, SourceRange),
773}
774
775impl Recoverable for MechCode {
776  fn error_placeholder(skipped_tokens: Token, range: SourceRange) -> Self {
777    MechCode::Error(skipped_tokens, range)
778  }
779}
780
781impl MechCode {
782  pub fn tokens(&self) -> Vec<Token> {
783    match self {
784      MechCode::Expression(x) => x.tokens(),
785      MechCode::Statement(x) => x.tokens(),
786      MechCode::Comment(x) => x.tokens(),
787      MechCode::Error(t,_) => vec![t.clone()],
788      _ => todo!(),
789      //FunctionDefine(x) => x.tokens(),
790      //FsmSpecification(x) => x.tokens(),
791      //FsmImplementation(x) => x.tokens(),
792    }
793  }
794}
795
796#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
797#[derive(Clone, Debug, Hash, PartialEq, Eq)]
798pub struct FunctionDefine {
799  pub name: Identifier,
800  pub input: Vec<FunctionArgument>,
801  pub output: Vec<FunctionArgument>,
802  pub statements: Vec<Statement>,
803}
804
805#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
806#[derive(Clone, Debug, Hash, PartialEq, Eq)]
807pub struct FunctionArgument {
808  pub name: Identifier,
809  pub kind: KindAnnotation,
810}
811
812impl FunctionArgument {
813  pub fn tokens(&self) -> Vec<Token> {
814    let mut tokens = self.name.tokens();
815    tokens.append(&mut self.kind.tokens());
816    tokens
817  }
818}
819
820#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
821#[derive(Clone, Debug, Hash, PartialEq, Eq)]
822pub struct FsmImplementation {
823  pub name: Identifier,
824  pub input: Vec<Identifier>,
825  pub start: Pattern,
826  pub arms: Vec<FsmArm>,
827}
828
829#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
830#[derive(Clone, Debug, Hash, PartialEq, Eq)]
831pub enum FsmArm {
832  Guard(Pattern,Vec<Guard>),
833  Transition(Pattern,Vec<Transition>),
834}
835
836#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
837#[derive(Clone, Debug, Hash, PartialEq, Eq)]
838pub struct Guard { 
839  pub condition: Pattern,
840  pub transitions: Vec<Transition>,
841}
842
843#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
844#[derive(Clone, Debug, Hash, PartialEq, Eq)]
845pub enum Transition {
846  Async(Pattern),
847  CodeBlock(Vec<(MechCode, Option<Comment>)>),
848  Next(Pattern),
849  Output(Pattern),
850  Statement(Statement),
851}
852
853#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
854#[derive(Clone, Debug, Hash, PartialEq, Eq)]
855pub enum Pattern {
856  Expression(Expression),
857  TupleStruct(PatternTupleStruct),
858  Tuple(PatternTuple),
859  Wildcard,
860}
861
862impl Pattern {
863  pub fn tokens(&self) -> Vec<Token> {
864    match self {
865      Pattern::Expression(e) => e.tokens(),
866      Pattern::TupleStruct(ts) => ts.tokens(),
867      Pattern::Tuple(t) => t.tokens(),
868      Pattern::Wildcard => vec![],
869    }
870  }
871}
872
873#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
874#[derive(Clone, Debug, Hash, PartialEq, Eq)]
875pub struct PatternTupleStruct {
876  pub name: Identifier,
877  pub patterns: Vec<Pattern>,
878}
879
880impl PatternTupleStruct {
881  pub fn tokens(&self) -> Vec<Token> {
882    let mut tokens = self.name.tokens();
883    for p in &self.patterns {
884      tokens.append(&mut p.tokens());
885    }
886    tokens
887  }
888}
889
890#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
891#[derive(Clone, Debug, Hash, PartialEq, Eq)]
892pub struct PatternTuple(pub Vec<Pattern>);
893
894impl PatternTuple {
895  pub fn tokens(&self) -> Vec<Token> {
896    let mut tokens = vec![];
897    for p in &self.0 {
898        tokens.append(&mut p.tokens());
899    }
900    tokens
901  }
902}
903
904#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
905#[derive(Clone, Debug, Hash, PartialEq, Eq)]
906pub struct FsmSpecification {
907  pub name: Identifier,
908  pub input: Vec<Var>,
909  pub output: Option<KindAnnotation>,
910  pub states: Vec<StateDefinition>,
911}
912
913#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
914#[derive(Clone, Debug, Hash, PartialEq, Eq)]
915pub struct StateDefinition {
916  pub name: Identifier,
917  pub state_variables: Option<Vec<Var>>,
918}
919
920#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
921#[derive(Clone, Debug, Hash, PartialEq, Eq)]
922pub enum Statement {
923  EnumDefine(EnumDefine),
924  //FsmDeclare(FsmDeclare),
925  KindDefine(KindDefine),
926  OpAssign(OpAssign),
927  VariableAssign(VariableAssign),
928  VariableDefine(VariableDefine),
929  TupleDestructure(TupleDestructure),
930  SplitTable,     // todo
931  FlattenTable,   // todo
932}
933
934impl Statement {
935  pub fn tokens(&self) -> Vec<Token> {
936    match self {
937      Statement::EnumDefine(x) => x.tokens(),
938      //Statement::FsmDeclare(x) => x.tokens(),
939      Statement::KindDefine(x) => x.tokens(),
940      Statement::OpAssign(x) => x.tokens(),
941      Statement::VariableAssign(x) => x.tokens(),
942      Statement::VariableDefine(x) => x.tokens(),
943      Statement::TupleDestructure(x) => x.tokens(),
944      Statement::SplitTable => vec![], // todo
945      Statement::FlattenTable => vec![], // todo
946    }
947  }
948}
949
950#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
951#[derive(Clone, Debug, Hash, PartialEq, Eq)]
952pub struct TupleDestructure {
953  pub vars: Vec<Identifier>,
954  pub expression: Expression,
955}
956
957impl TupleDestructure {
958  pub fn tokens(&self) -> Vec<Token> {
959    let mut tokens = vec![];
960    for var in &self.vars {
961      tokens.append(&mut var.tokens());
962    }
963    tokens.append(&mut self.expression.tokens());
964    tokens
965  }
966}
967
968#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
969#[derive(Clone, Debug, Hash, PartialEq, Eq)]
970pub struct FsmPipe {
971  pub start: FsmInstance,
972  pub transitions: Vec<Transition>
973}
974
975#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
976#[derive(Clone, Debug, Hash, PartialEq, Eq)]
977pub enum PipeElement {
978  Expression(Expression),
979  FsmInstance(FsmInstance),
980  Timer // todo
981}
982
983#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
984#[derive(Clone, Debug, Hash, PartialEq, Eq)]
985pub struct FsmDeclare {
986  pub fsm: Fsm,
987  pub pipe: FsmPipe,
988}
989
990#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
991#[derive(Clone, Debug, Hash, PartialEq, Eq)]
992pub struct Fsm {
993  pub name: Identifier,
994  pub args: Option<ArgumentList>,
995  pub kind: Option<KindAnnotation>
996}
997
998pub type FsmArgs = Vec<(Option<Identifier>,Expression)>;
999
1000#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1001#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1002pub struct FsmInstance {
1003  pub name: Identifier,
1004  pub args: Option<FsmArgs>,
1005}
1006
1007#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1008#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1009pub struct EnumDefine {
1010  pub name: Identifier,
1011  pub variants: Vec<EnumVariant>,
1012}
1013
1014impl EnumDefine {
1015  pub fn tokens(&self) -> Vec<Token> {
1016    let mut tokens = self.name.tokens();
1017    for v in &self.variants {
1018      tokens.append(&mut v.tokens());
1019    }
1020    tokens
1021  }
1022}
1023
1024#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1025#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1026pub struct EnumVariant {
1027  pub name: Identifier,
1028  pub value: Option<KindAnnotation>,
1029}
1030
1031impl EnumVariant {
1032  pub fn tokens(&self) -> Vec<Token> {
1033    let mut tokens = self.name.tokens();
1034    if let Some(value) = &self.value {
1035      tokens.append(&mut value.tokens());
1036    }
1037    tokens
1038  }
1039}
1040
1041#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1042#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1043pub struct KindDefine {
1044  pub name: Identifier,
1045  pub kind: KindAnnotation,
1046}
1047
1048impl KindDefine {
1049  pub fn tokens(&self) -> Vec<Token> {
1050    let mut tokens = self.name.tokens();
1051    tokens.append(&mut self.kind.tokens());
1052    tokens
1053  }
1054}
1055
1056#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1057#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1058pub struct Record {
1059  pub bindings: Vec<Binding>,
1060}
1061
1062impl Record {
1063  pub fn tokens(&self) -> Vec<Token> {
1064    let mut tkns = vec![];
1065    for b in &self.bindings {
1066      let mut t = b.tokens();
1067      tkns.append(&mut t);
1068    }
1069    tkns
1070  }
1071}
1072
1073#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1074#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1075pub enum Structure {
1076  Empty,
1077  Map(Map),
1078  Matrix(Matrix),
1079  Record(Record),
1080  Set(Set),
1081  Table(Table),
1082  Tuple(Tuple),
1083  TupleStruct(TupleStruct),
1084}
1085
1086impl Structure {
1087  pub fn tokens(&self) -> Vec<Token> {
1088    match self {
1089      Structure::Empty => vec![],
1090      Structure::Map(map) => map.tokens(),
1091      Structure::Matrix(mat) => mat.tokens(),
1092      Structure::Record(rec) => rec.tokens(),
1093      Structure::Set(set) => set.tokens(),
1094      Structure::Table(tab) => tab.tokens(),
1095      Structure::Tuple(tup) => tup.tokens(),
1096      Structure::TupleStruct(ts) => ts.tokens(),
1097    }
1098  }
1099}
1100
1101#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1102#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1103pub struct Map {
1104  pub elements: Vec<Mapping>,
1105}
1106
1107impl Map {
1108  pub fn tokens(&self) -> Vec<Token> {
1109    let mut tkns = vec![];
1110    for e in &self.elements {
1111      let mut t = e.tokens();
1112      tkns.append(&mut t);
1113    }
1114    tkns
1115  }
1116}
1117
1118#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1119#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1120pub struct Mapping {
1121  pub key: Expression,
1122  pub value: Expression,
1123}
1124
1125impl Mapping {
1126  pub fn tokens(&self) -> Vec<Token> {
1127    let mut tkns = self.key.tokens();
1128    tkns.append(&mut self.value.tokens());
1129    tkns
1130  }
1131}
1132
1133#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1134#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1135pub struct Set {
1136  pub elements: Vec<Expression>,
1137}
1138
1139impl Set {
1140  pub fn tokens(&self) -> Vec<Token> {
1141    let mut tkns = vec![];
1142    for e in &self.elements {
1143      let mut t = e.tokens();
1144      tkns.append(&mut t);
1145    }
1146    tkns
1147  }
1148}
1149
1150#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1151#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1152pub struct Atom {
1153  pub name: Identifier,
1154}
1155
1156impl Atom {
1157  pub fn tokens(&self) -> Vec<Token> {
1158    self.name.tokens()
1159  }
1160}
1161
1162#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1163#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1164pub struct TupleStruct {
1165  pub name: Identifier,
1166  pub value: Box<Expression>,
1167}
1168
1169impl TupleStruct {
1170  pub fn tokens(&self) -> Vec<Token> {
1171    let mut tkns = self.name.tokens();
1172    tkns.append(&mut self.value.tokens());
1173    tkns
1174  }
1175}
1176
1177#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1178#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1179pub struct Matrix {
1180  pub rows: Vec<MatrixRow>,
1181}
1182
1183impl Matrix {
1184  pub fn tokens(&self) -> Vec<Token> {
1185    let mut tkns = vec![];
1186    for r in &self.rows {
1187      let mut t = r.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 TableHeader(pub Vec<Field>);
1197
1198impl TableHeader {
1199  pub fn new(fields: Vec<Field>) -> TableHeader {
1200    TableHeader(fields)
1201  }
1202  pub fn tokens(&self) -> Vec<Token> {
1203    let mut tkns = vec![];
1204    for f in &self.0 {
1205      let mut t = f.tokens();
1206      tkns.append(&mut t);
1207    }
1208    tkns
1209  }
1210}
1211
1212#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1213#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1214pub struct Table {
1215  pub header: TableHeader,
1216  pub rows: Vec<TableRow>,
1217}
1218
1219impl Table {
1220  pub fn tokens(&self) -> Vec<Token> {
1221    let mut tkns = vec![];
1222    for f in &self.header.0 {
1223      let mut t = f.tokens();
1224      tkns.append(&mut t);
1225    }
1226    for r in &self.rows {
1227      let mut t = r.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 Field {
1237  pub name: Identifier,
1238  pub kind: Option<KindAnnotation>,
1239}
1240
1241impl Field {
1242  pub fn tokens(&self) -> Vec<Token> {
1243    let mut tkns = self.name.tokens();
1244    if let Some(knd) = &self.kind {
1245      let mut t = knd.tokens();
1246      tkns.append(&mut t);
1247    }
1248    tkns
1249  }
1250}
1251
1252#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1253#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1254pub struct TableColumn {
1255  pub element: Expression,
1256}
1257
1258impl TableColumn {
1259  pub fn tokens(&self) -> Vec<Token> {
1260    self.element.tokens()
1261  }
1262}
1263
1264#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1265#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1266pub struct MatrixColumn {
1267  pub element: Expression,
1268}
1269
1270impl MatrixColumn {
1271  pub fn tokens(&self) -> Vec<Token> {
1272    self.element.tokens()
1273  }
1274}
1275
1276#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1277#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1278pub struct TableRow {
1279  pub columns: Vec<TableColumn>,
1280}
1281
1282impl TableRow {
1283  pub fn tokens(&self) -> Vec<Token> {
1284    let mut tkns = vec![];
1285    for r in &self.columns {
1286      let mut t = r.element.tokens();
1287      tkns.append(&mut t);
1288    }
1289    tkns
1290  }
1291}
1292
1293#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1294#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1295pub struct MatrixRow {
1296  pub columns: Vec<MatrixColumn>,
1297}
1298
1299impl MatrixRow {
1300  pub fn tokens(&self) -> Vec<Token> {
1301    let mut tkns = vec![];
1302    for r in &self.columns {
1303      let mut t = r.tokens();
1304      tkns.append(&mut t);
1305    }
1306    tkns
1307  }
1308}
1309
1310#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1311#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1312pub struct VariableDefine {
1313  pub mutable: bool,
1314  pub var: Var,
1315  pub expression: Expression,
1316}
1317
1318impl VariableDefine {
1319  pub fn tokens(&self) -> Vec<Token> {
1320    let mut tkns = self.var.tokens();
1321    tkns.append(&mut self.expression.tokens());
1322    tkns
1323  }
1324}
1325
1326#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1327#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1328pub struct Var {
1329  pub name: Identifier,
1330  pub kind: Option<KindAnnotation>,
1331}
1332
1333impl Var {
1334  pub fn tokens(&self) -> Vec<Token> {
1335    let mut tkns = self.name.tokens();
1336    if let Some(knd) = &self.kind {
1337      let mut t = knd.tokens();
1338      tkns.append(&mut t);
1339    }
1340    tkns
1341  }
1342}
1343
1344#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1345#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1346pub struct VariableAssign {
1347  pub target: SliceRef,
1348  pub expression: Expression,
1349}
1350
1351impl VariableAssign {
1352  pub fn tokens(&self) -> Vec<Token> {
1353    let mut tkns = self.target.tokens();
1354    tkns.append(&mut self.expression.tokens());
1355    tkns
1356  }
1357}
1358
1359#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1360#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1361pub struct Identifier {
1362  pub name: Token,
1363}
1364
1365impl Identifier {
1366  pub fn tokens(&self) -> Vec<Token> {
1367    vec![self.name.clone()]
1368  }
1369
1370  pub fn to_string(&self) -> String {
1371    self.name.to_string()
1372  }
1373
1374}
1375
1376impl Identifier {
1377  pub fn hash(&self) -> u64 {
1378    hash_chars(&self.name.chars)
1379  }
1380}
1381
1382#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1383#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1384pub struct Emoji {
1385  pub tokens: Vec<Token>,
1386}
1387
1388#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1389#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1390pub struct Word {
1391  pub tokens: Vec<Token>,
1392}
1393
1394#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1395#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1396pub struct Slice {
1397  pub name: Identifier,
1398  pub subscript: Vec<Subscript>
1399}
1400
1401impl Slice {
1402  pub fn tokens(&self) -> Vec<Token> {
1403    let mut tkns = self.name.tokens();
1404    for sub in &self.subscript {
1405      let mut sub_tkns = sub.tokens();
1406      tkns.append(&mut sub_tkns);
1407    }
1408    tkns
1409  }
1410}
1411
1412#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1413#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1414pub struct SliceRef {
1415  pub name: Identifier,
1416  pub subscript: Option<Vec<Subscript>>
1417}
1418
1419impl SliceRef {
1420  pub fn tokens(&self) -> Vec<Token> {
1421    let mut tkns = self.name.tokens();
1422    if let Some(subs) = &self.subscript {
1423      for sub in subs {
1424        let mut sub_tkns = sub.tokens();
1425        tkns.append(&mut sub_tkns);
1426      }
1427    }
1428    tkns
1429  }
1430}
1431
1432#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1433#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1434pub enum Subscript {
1435  All,                      // a[:]
1436  Brace(Vec<Subscript>),    // a{"foo"}
1437  Bracket(Vec<Subscript>),  // a[1,2,3]
1438  Dot(Identifier),          // a.b
1439  DotInt(RealNumber),       // a.1
1440  Formula(Factor),          // a[1 + 1]
1441  Range(RangeExpression),   // a[1 + 1]
1442  Swizzle(Vec<Identifier>), // a.b,c
1443}
1444
1445impl Subscript {
1446
1447  pub fn tokens(&self) -> Vec<Token> {
1448    match self {
1449      Subscript::All => vec![
1450        Token::new(TokenKind::Colon, SourceRange::default(), vec![':']),
1451      ],
1452      Subscript::Brace(subs) => {
1453        let mut tkns = vec![Token::new(TokenKind::LeftBrace, SourceRange::default(), vec!['{'])];
1454        for sub in subs {
1455          let mut sub_tkns = sub.tokens();
1456          tkns.append(&mut sub_tkns);
1457        }
1458        tkns.push(Token::new(TokenKind::RightBrace, SourceRange::default(), vec!['}']));
1459        tkns
1460      },
1461      Subscript::Bracket(subs) => {
1462        let mut tkns = vec![Token::new(TokenKind::LeftBracket, SourceRange::default(), vec!['['])];
1463        for sub in subs {
1464          let mut sub_tkns = sub.tokens();
1465          tkns.append(&mut sub_tkns);
1466        }
1467        tkns.push(Token::new(TokenKind::RightBracket, SourceRange::default(), vec![']']));
1468        tkns
1469      },
1470      Subscript::Dot(id) => id.tokens(),
1471      Subscript::DotInt(num) => num.tokens(),
1472      Subscript::Formula(factor) => factor.tokens(),
1473      Subscript::Range(range) => range.tokens(),
1474      Subscript::Swizzle(ids) => ids.iter().flat_map(|id| id.tokens()).collect(),
1475    }
1476  }
1477}
1478
1479#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1480#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1481pub enum Expression {
1482  Formula(Factor),
1483  FunctionCall(FunctionCall),
1484  //FsmPipe(FsmPipe),
1485  Literal(Literal),
1486  Range(Box<RangeExpression>),
1487  Slice(Slice),
1488  Structure(Structure),
1489  SetComprehension(Box<SetComprehension>),
1490  Var(Var),
1491}
1492
1493impl Expression {
1494  pub fn tokens(&self) -> Vec<Token> {
1495    match self {
1496      Expression::Var(v) => v.tokens(),
1497      Expression::Literal(ltrl) => ltrl.tokens(),
1498      Expression::Structure(strct) => strct.tokens(),
1499      Expression::Formula(fctr) => fctr.tokens(),
1500      Expression::Range(range) => range.tokens(),
1501      Expression::Slice(slice) => slice.tokens(),
1502      Expression::SetComprehension(sc) => sc.tokens(),
1503      _ => todo!(),
1504    }
1505  }
1506}
1507
1508#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1509#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1510pub struct SetComprehension {
1511  pub expression: Expression,
1512  pub qualifiers: Vec<ComprehensionQualifier>,
1513}
1514
1515impl SetComprehension {
1516  pub fn tokens(&self) -> Vec<Token> {
1517    let mut tokens = self.expression.tokens();
1518    for qualifier in &self.qualifiers {
1519      tokens.append(&mut qualifier.tokens());
1520    }
1521    tokens
1522  }
1523}
1524
1525#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1526#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1527pub enum ComprehensionQualifier {
1528  Generator((Pattern,Expression)),
1529  Filter(Expression),
1530  Let(VariableDefine),
1531}
1532
1533impl ComprehensionQualifier {
1534  pub fn tokens(&self) -> Vec<Token> {
1535    match self {
1536      ComprehensionQualifier::Generator((pattern, expr)) => {
1537        let mut tokens = pattern.tokens();
1538        tokens.append(&mut expr.tokens());
1539        tokens
1540      }
1541      ComprehensionQualifier::Filter(expr) => expr.tokens(),
1542      ComprehensionQualifier::Let(var_def) => var_def.tokens(),
1543    }
1544  }
1545}
1546
1547pub type ArgumentList = Vec<(Option<Identifier>,Expression)>;
1548
1549#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1550#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1551pub struct FunctionCall {
1552  pub name: Identifier,
1553  pub args: ArgumentList,
1554}
1555
1556impl FunctionCall {
1557  pub fn tokens(&self) -> Vec<Token> {
1558    self.name.tokens()
1559  }
1560}
1561
1562#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1563#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1564pub struct Tuple {
1565  pub elements: Vec<Expression>
1566}
1567
1568impl Tuple {
1569  pub fn tokens(&self) -> Vec<Token> {
1570    let mut tokens = vec![];
1571    for elem in &self.elements {
1572      tokens.append(&mut elem.tokens());
1573    }
1574    tokens
1575  }
1576}
1577
1578#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1579#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1580pub struct Binding {
1581  pub name: Identifier,
1582  pub kind: Option<KindAnnotation>,
1583  pub value: Expression,
1584}
1585
1586impl Binding {
1587  pub fn tokens(&self) -> Vec<Token> {
1588    let mut tokens = self.name.tokens();
1589    if let Some(knd) = &self.kind {
1590      tokens.append(&mut knd.tokens());
1591    }
1592    tokens.append(&mut self.value.tokens());
1593    tokens
1594  }
1595}
1596
1597#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1598#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1599pub struct KindAnnotation {
1600  pub kind: Kind,
1601}
1602
1603impl KindAnnotation {
1604
1605  pub fn hash(&self) -> u64 {
1606    #[cfg(feature = "no_std")]
1607    let mut hasher = FxHasher::default();
1608    #[cfg(not(feature = "no_std"))]
1609    let mut hasher = std::collections::hash_map::DefaultHasher::new();
1610    self.kind.hash(&mut hasher);
1611    hasher.finish()
1612  }
1613
1614  pub fn tokens(&self) -> Vec<Token> {
1615    self.kind.tokens()
1616  }
1617}
1618
1619#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1620#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1621pub enum Kind {
1622  Any,
1623  Atom(Identifier),
1624  Table((Vec<(Identifier,Kind)>,Box<Literal>)),
1625  Set(Box<Kind>,Option<Box<Literal>>),
1626  Record((Vec<(Identifier,Kind)>)),
1627  Empty,
1628  //Fsm(Vec<Kind>,Vec<Kind>),
1629  //Function(Vec<Kind>,Vec<Kind>),
1630  Map(Box<Kind>,Box<Kind>),
1631  Matrix((Box<Kind>,Vec<Literal>)),
1632  Option(Box<Kind>),
1633  Scalar(Identifier),
1634  Tuple(Vec<Kind>),
1635  Kind(Box<Kind>),
1636}
1637
1638impl Kind {
1639  pub fn tokens(&self) -> Vec<Token> {
1640    match self {
1641      Kind::Option(x) => x.tokens(),
1642      Kind::Tuple(x) => x.iter().flat_map(|k| k.tokens()).collect(),
1643      Kind::Matrix((kind, literals)) => {
1644        let mut tokens = kind.tokens();
1645        for l in literals {
1646          tokens.append(&mut l.tokens());
1647        }
1648        tokens
1649      },
1650      Kind::Record(kinds) => {
1651        let mut tokens = vec![];
1652        for (id, kind) in kinds {
1653          tokens.append(&mut id.tokens());
1654          tokens.append(&mut kind.tokens());
1655        }
1656        tokens
1657      }
1658      Kind::Set(kind, size) => {
1659        let mut tokens = kind.tokens();
1660        if let Some(literal) = size {
1661          tokens.append(&mut literal.tokens());
1662        }
1663        tokens
1664      }
1665      Kind::Table((kinds, literal)) => {
1666        let mut tokens = vec![];
1667        for (id, kind) in kinds {
1668          tokens.append(&mut id.tokens());
1669          tokens.append(&mut kind.tokens());
1670        }
1671        tokens.append(&mut literal.tokens());
1672        tokens
1673      }
1674      Kind::Map(x, y) => x.tokens().into_iter().chain(y.tokens()).collect(),
1675      Kind::Scalar(x) => x.tokens(),
1676      Kind::Atom(x) => x.tokens(),
1677      Kind::Kind(x) => x.tokens(),
1678      Kind::Empty => vec![],
1679      Kind::Any => vec![],
1680    }
1681  }
1682}
1683
1684#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1685#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1686pub enum Literal {
1687  Atom(Atom),
1688  Boolean(Token),
1689  Empty(Token),
1690  Number(Number),
1691  String(MechString),
1692  Kind(Kind),
1693  TypedLiteral((Box<Literal>,KindAnnotation))
1694}
1695
1696impl Literal {
1697  pub fn tokens(&self) -> Vec<Token> {
1698    match self {
1699      Literal::Atom(atm) => atm.name.tokens(),
1700      Literal::Boolean(tkn) => vec![tkn.clone()],
1701      Literal::Number(x) => x.tokens(),
1702      Literal::String(strng) => vec![strng.text.clone()],
1703      Literal::Empty(tkn) => vec![tkn.clone()],
1704      Literal::Kind(knd) => knd.tokens(),
1705      Literal::TypedLiteral((lit, knd)) => {
1706        let mut tokens = lit.tokens();
1707        tokens.append(&mut knd.tokens());
1708        tokens
1709      }
1710    }
1711  }
1712}
1713
1714#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1715#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1716pub struct MechString {
1717  pub text: Token,
1718}
1719
1720impl MechString {
1721  pub fn to_string(&self) -> String {
1722    self.text.to_string()
1723  }
1724}
1725
1726pub type Hyperlink = (Paragraph, Token);
1727
1728#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1729#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1730pub enum ParagraphElement {
1731  Emphasis(Box<ParagraphElement>),
1732  FootnoteReference(Token),
1733  Highlight(Box<ParagraphElement>),
1734  Hyperlink(Hyperlink),
1735  InlineCode(Token),
1736  EvalInlineMechCode(Expression),
1737  InlineMechCode(MechCode),
1738  InlineEquation(Token),
1739  Reference(Token),
1740  SectionReference(Token),
1741  Strikethrough(Box<ParagraphElement>),
1742  Strong(Box<ParagraphElement>),
1743  Text(Token),
1744  Underline(Box<ParagraphElement>),
1745  Error(Token, SourceRange),
1746}
1747
1748impl Recoverable for ParagraphElement {
1749  fn error_placeholder(skipped_tokens: Token, range: SourceRange) -> Self {
1750    ParagraphElement::Error(skipped_tokens, range)
1751  }
1752}
1753
1754impl ParagraphElement {
1755
1756  pub fn tokens(&self) -> Vec<Token> {
1757    match self {
1758      ParagraphElement::Emphasis(t) => t.tokens(),
1759      ParagraphElement::FootnoteReference(t) => vec![t.clone()],
1760      ParagraphElement::Highlight(t) => t.tokens(),
1761      ParagraphElement::Hyperlink((t, u)) => {
1762        let mut tokens = t.tokens();
1763        tokens.push(u.clone());
1764        tokens
1765      },
1766      ParagraphElement::InlineCode(t) => vec![t.clone()],
1767      ParagraphElement::InlineEquation(t) => vec![t.clone()],
1768      ParagraphElement::InlineMechCode(t) => t.tokens(),
1769      ParagraphElement::EvalInlineMechCode(t) => t.tokens(),
1770      ParagraphElement::Reference(t) => vec![t.clone()],
1771      ParagraphElement::SectionReference(t) => vec![t.clone()],
1772      ParagraphElement::Strikethrough(t) => t.tokens(),
1773      ParagraphElement::Strong(t) => t.tokens(),
1774      ParagraphElement::Text(t) => vec![t.clone()],
1775      ParagraphElement::Underline(t) => t.tokens(),
1776      ParagraphElement::Error(t, _) => vec![t.clone()],
1777    }
1778  }
1779
1780  pub fn to_string(&self) -> String {
1781    match self {
1782      ParagraphElement::Emphasis(t) => t.to_string(),
1783      ParagraphElement::FootnoteReference(t) => t.to_string(),
1784      ParagraphElement::Highlight(t) => t.to_string(),
1785      ParagraphElement::Hyperlink((t, u)) => {
1786        format!("[{}]({})", t.to_string(), u.to_string())
1787      }
1788      ParagraphElement::InlineCode(t) => t.to_string(),
1789      ParagraphElement::InlineEquation(t) => t.to_string(),
1790      ParagraphElement::InlineMechCode(t) => format!("{:?}", t),
1791      ParagraphElement::EvalInlineMechCode(t) => format!("{:?}", t),
1792      ParagraphElement::Reference(t) => t.to_string(),
1793      ParagraphElement::SectionReference(t) => t.to_string(),
1794      ParagraphElement::Strikethrough(t) => t.to_string(),
1795      ParagraphElement::Strong(t) => t.to_string(),
1796      ParagraphElement::Text(t) => t.to_string(),
1797      ParagraphElement::Underline(t) => t.to_string(),
1798      ParagraphElement::Error(t, s) => format!("{{ERROR: {} at {:?}}}", t.to_string(), s),
1799    }
1800  }
1801
1802}
1803
1804#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1805#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1806pub struct Paragraph {
1807  pub elements: Vec<ParagraphElement>,
1808  pub error_range: Option<SourceRange>,
1809}
1810
1811impl Paragraph {
1812  pub fn to_string(&self) -> String {
1813    let mut out = "".to_string();
1814    for e in &self.elements {
1815      out.push_str(&e.to_string());
1816    }
1817    out
1818  }
1819
1820  pub fn from_tokens(tokens: Vec<Token>) -> Paragraph {
1821    let elements = tokens.into_iter().map(|t| ParagraphElement::Text(t)).collect();
1822    Paragraph {
1823      elements,
1824      error_range: None,
1825    }
1826  }
1827
1828  pub fn has_errors(&self) -> bool {
1829    self.error_range.is_some()
1830  }
1831
1832  pub fn tokens(&self) -> Vec<Token> {
1833    let mut tkns = vec![];
1834    for e in &self.elements {
1835      let mut e_tkns = e.tokens();
1836      tkns.append(&mut e_tkns);
1837    }
1838    tkns
1839  }
1840
1841}
1842
1843impl Recoverable for Paragraph {
1844  fn error_placeholder(skipped_tokens: Token, range: SourceRange) -> Self {
1845    Paragraph {
1846      elements: vec![ParagraphElement::Text(Token::new(TokenKind::Error, range.clone(), skipped_tokens.chars.clone()))],
1847      error_range: Some(range),
1848    }
1849  }
1850}
1851
1852
1853pub type Sign = bool;
1854pub type Numerator = Token;
1855pub type Denominator = Token;
1856pub type Whole = Token;
1857pub type Part = Token;
1858pub type Real = Box<Number>;
1859pub type Imaginary = Box<Number>;
1860pub type Base = (Whole, Part);
1861pub type Exponent = (Sign, Whole, Part);
1862
1863#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1864#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1865pub enum Number {
1866  Real(RealNumber),
1867  Complex(C64Node),
1868}
1869
1870impl Number {
1871
1872  pub fn from_integer(x: i64) -> Number {
1873    Number::Real(RealNumber::Integer(Token::new(TokenKind::Digit, SourceRange::default(), x.to_string().chars().collect())))
1874  }
1875
1876  pub fn to_string(&self) -> String {
1877    match self {
1878      Number::Real(x) => x.to_string(),
1879      Number::Complex(x) => x.to_string(),
1880    }
1881  }
1882
1883  pub fn tokens(&self) -> Vec<Token> {
1884    match self {
1885      Number::Real(x) => x.tokens(),
1886      Number::Complex(x) => x.tokens(),
1887    }
1888  }
1889}
1890
1891#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1892#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1893pub enum RealNumber {
1894  Binary(Token),
1895  Decimal(Token),
1896  Float((Whole,Part)),
1897  Hexadecimal(Token),
1898  Integer(Token),
1899  TypedInteger((Token,KindAnnotation)),
1900  Negated(Box<RealNumber>),
1901  Octal(Token),
1902  Rational((Numerator,Denominator)),
1903  Scientific((Base,Exponent)),
1904}
1905
1906impl RealNumber {
1907  pub fn tokens(&self) -> Vec<Token> {
1908    match self {
1909      RealNumber::Integer(tkn) => vec![tkn.clone()],
1910      RealNumber::TypedInteger((tkn, _)) => vec![tkn.clone()],
1911      RealNumber::Float((whole, part)) => vec![whole.clone(), part.clone()],
1912      RealNumber::Binary(tkn) => vec![tkn.clone()],
1913      RealNumber::Hexadecimal(tkn) => vec![tkn.clone()],
1914      RealNumber::Octal(tkn) => vec![tkn.clone()],
1915      RealNumber::Decimal(tkn) => vec![tkn.clone()],
1916      RealNumber::Rational((num, den)) => vec![num.clone(), den.clone()],
1917      RealNumber::Scientific(((whole,part), exponent)) => {
1918        let mut tokens = vec![whole.clone(), part.clone()];
1919        let (sign, whole_exp, part_exp) = exponent;
1920        tokens.push(Token::new(TokenKind::Plus, SourceRange::default(), if *sign { vec!['+'] } else { vec!['-'] }));
1921        tokens.push(whole_exp.clone());
1922        tokens.push(part_exp.clone());
1923        tokens
1924      }
1925      RealNumber::Negated(x) => {
1926        let mut tokens = vec![Token::new(TokenKind::Dash, SourceRange::default(), vec!['-'])];
1927        tokens.append(&mut x.tokens());
1928        tokens
1929      }
1930    }
1931  }
1932  pub fn to_string(&self) -> String {
1933    match self {
1934      RealNumber::Integer(tkn) => tkn.to_string(),
1935      RealNumber::Float((whole, part)) => format!("{}.{}", whole.to_string(), part.to_string()),
1936      RealNumber::Binary(tkn) => format!("0b{}", tkn.to_string()),
1937      RealNumber::Hexadecimal(tkn) => format!("0x{}", tkn.to_string()),
1938      RealNumber::Octal(tkn) => format!("0o{}", tkn.to_string()),
1939      RealNumber::Decimal(tkn) => format!("0d{}", tkn.to_string()),
1940      RealNumber::Rational((num, den)) => format!("{}/{}", num.to_string(), den.to_string()),
1941      RealNumber::Scientific(((whole,part), exponent)) => {
1942        let (sign, whole, part) = exponent;
1943        let sign_str = if *sign { "+" } else { "-" };
1944        let whole_str = whole.to_string();
1945        let part_str = part.to_string();
1946        format!("{}{}.{}/10^{}", whole.to_string(), part.to_string(), sign_str, whole_str)
1947      }
1948      RealNumber::TypedInteger((tkn, kind)) => format!("{}{}", tkn.to_string(), kind.kind.tokens().iter().map(|t| t.to_string()).collect::<String>()),
1949      RealNumber::Negated(x) => format!("-{}", x.to_string()),
1950    }
1951  }
1952}
1953
1954#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1955#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1956pub struct ImaginaryNumber {
1957  pub number: RealNumber,
1958}
1959
1960#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1961#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1962pub struct C64Node {
1963  pub real: Option<RealNumber>,
1964  pub imaginary: ImaginaryNumber
1965}
1966
1967impl C64Node {
1968  pub fn tokens(&self) -> Vec<Token> {
1969    let mut tkns = vec![];
1970    if let Some(r) = &self.real {
1971      tkns.append(&mut r.tokens());
1972    }
1973    tkns.append(&mut self.imaginary.number.tokens());
1974    tkns
1975  }
1976
1977  pub fn to_string(&self) -> String {
1978    let mut out = "".to_string();
1979    if let Some(r) = &self.real {
1980      out.push_str(&r.to_string());
1981    }
1982    out.push_str("i");
1983    out
1984  }
1985}
1986
1987#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1988#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1989pub struct Comment {
1990  pub paragraph: Paragraph,
1991}
1992
1993impl Comment {
1994  pub fn tokens(&self) -> Vec<Token> {
1995    self.paragraph.tokens()
1996  }
1997}
1998
1999#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2000#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2001pub struct OpAssign {
2002  pub target: SliceRef,
2003  pub op: OpAssignOp,
2004  pub expression: Expression,
2005}
2006
2007impl OpAssign {
2008  pub fn tokens(&self) -> Vec<Token> {
2009    let mut tkns = self.target.tokens();
2010    tkns.append(&mut self.op.tokens());
2011    tkns.append(&mut self.expression.tokens());
2012    tkns
2013  }
2014}
2015
2016#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2017#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2018pub enum OpAssignOp {
2019  Add,
2020  Div,
2021  Exp, 
2022  Mod,  
2023  Mul,
2024  Sub,   
2025}
2026
2027impl OpAssignOp {
2028  pub fn tokens(&self) -> Vec<Token> {
2029    match self {
2030      OpAssignOp::Add => vec![Token::new(TokenKind::Plus, SourceRange::default(), vec!['+'])],
2031      OpAssignOp::Div => vec![Token::new(TokenKind::Slash, SourceRange::default(), vec!['/'])],
2032      OpAssignOp::Exp => vec![Token::new(TokenKind::Caret, SourceRange::default(), vec!['^'])],
2033      OpAssignOp::Mod => vec![Token::new(TokenKind::Percent, SourceRange::default(), vec!['%'])],
2034      OpAssignOp::Mul => vec![Token::new(TokenKind::Asterisk, SourceRange::default(), vec!['*'])],
2035      OpAssignOp::Sub => vec![Token::new(TokenKind::Dash, SourceRange::default(), vec!['-'])],
2036    }
2037  }
2038}
2039
2040#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2041#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2042pub enum RangeOp {
2043  Exclusive,      
2044  Inclusive,
2045}
2046
2047#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2048#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2049pub enum AddSubOp {
2050  Add,
2051  Sub,
2052}
2053
2054#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2055#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2056pub enum MulDivOp {
2057  Div,
2058  Mod,
2059  Mul,
2060}
2061
2062#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2063#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2064pub enum VecOp {
2065  Cross,
2066  Dot,
2067  MatMul,
2068  Solve,
2069}
2070
2071#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2072#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2073pub enum PowerOp {
2074  Pow
2075}
2076
2077#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2078#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2079pub enum ComparisonOp {
2080  Equal,
2081  GreaterThan,
2082  GreaterThanEqual,
2083  LessThan,
2084  LessThanEqual,
2085  NotEqual,
2086  StrictEqual,
2087  StrictNotEqual,
2088}
2089
2090#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2091#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2092pub enum LogicOp {
2093  And,
2094  Not,
2095  Or,
2096  Xor,
2097}
2098
2099#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2100#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2101pub enum FormulaOperator {
2102  AddSub(AddSubOp),
2103  Comparison(ComparisonOp),
2104  Power(PowerOp),
2105  Logic(LogicOp),
2106  MulDiv(MulDivOp),
2107  Vec(VecOp),
2108  Table(TableOp),
2109  Set(SetOp),
2110}
2111
2112#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2113#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2114pub enum TableOp {
2115  InnerJoin,
2116  LeftOuterJoin,
2117  RightOuterJoin	,
2118  FullOuterJoin	,
2119  LeftSemiJoin,  
2120  LeftAntiJoin,
2121}
2122
2123#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2124#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2125pub enum SetOp {
2126  Union,
2127  Intersection,
2128  Difference,
2129  Complement,
2130  ElementOf,
2131  NotElementOf,
2132  ProperSubset,
2133  ProperSuperset,
2134  Subset,
2135  Superset,
2136  SymmetricDifference,
2137}
2138
2139#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2140#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2141pub struct RangeExpression {
2142  pub start: Factor,
2143  pub increment: Option<(RangeOp,Factor)>,
2144  pub operator: RangeOp,
2145  pub terminal: Factor,
2146}
2147
2148impl RangeExpression {
2149  pub fn tokens(&self) -> Vec<Token> {
2150    let mut tokens = self.start.tokens();
2151    tokens.append(&mut self.terminal.tokens());
2152    tokens
2153  }
2154}
2155
2156#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2157#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2158pub struct Term {
2159  pub lhs: Factor,
2160  pub rhs: Vec<(FormulaOperator,Factor)>
2161}
2162
2163impl Term {
2164  pub fn tokens(&self) -> Vec<Token> {
2165    let mut lhs_tkns = self.lhs.tokens();
2166    let mut rhs_tkns = vec![];
2167    for (op, r) in &self.rhs {
2168      let mut tkns = r.tokens();
2169      rhs_tkns.append(&mut tkns);
2170    }
2171    lhs_tkns.append(&mut rhs_tkns);
2172    lhs_tkns
2173  }
2174}
2175
2176#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2177#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2178pub enum Factor {
2179  Expression(Box<Expression>),
2180  Negate(Box<Factor>),
2181  Not(Box<Factor>),
2182  Parenthetical(Box<Factor>),
2183  Term(Box<Term>),
2184  Transpose(Box<Factor>),
2185}
2186
2187impl Factor {
2188  pub fn tokens(&self) -> Vec<Token> {
2189    match self {
2190      Factor::Expression(x) => x.tokens(),
2191      Factor::Negate(x) => x.tokens(),
2192      Factor::Not(x) => x.tokens(),
2193      Factor::Parenthetical(x) => x.tokens(),
2194      Factor::Term(x) => x.tokens(),
2195      Factor::Transpose(x) => x.tokens(),
2196    }
2197  }
2198}