mech_core/
nodes.rs

1use std::cmp::Ordering;
2use crate::*; 
3use std::fmt;
4use std::io::{Write, Cursor, Read};
5
6
7pub fn compress_and_encode<T: serde::Serialize>(tree: &T) -> Result<String, Box<dyn std::error::Error>> {
8  let serialized_code = bincode::serde::encode_to_vec(tree,bincode::config::standard())?;
9  let mut compressed = Vec::new();
10  brotli::CompressorWriter::new(&mut compressed, 9, 4096, 22)
11      .write(&serialized_code)?;
12  Ok(base64::encode(compressed))
13}
14
15pub fn decode_and_decompress<T: serde::de::DeserializeOwned>(encoded: &str) -> Result<T, Box<dyn std::error::Error>> {
16  let decoded = base64::decode(encoded)?;
17  
18  let mut decompressed = Vec::new();
19  brotli::Decompressor::new(Cursor::new(decoded), 4096)
20      .read_to_end(&mut decompressed)?;
21  
22  let (decoded,red) = bincode::serde::decode_from_slice(&decompressed,bincode::config::standard())?;
23
24  Ok(decoded)
25}
26
27#[derive(Clone, Copy, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)]
28pub struct SourceLocation {
29  pub row: usize,
30  pub col: usize,
31}
32
33impl PartialOrd for SourceLocation {
34  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
35    if self.row < other.row {
36      Some(Ordering::Less)
37    } else if self.row > other.row {
38      Some(Ordering::Greater)
39    } else {
40      self.col.partial_cmp(&other.col)
41    }
42  }
43}
44
45impl fmt::Debug for SourceLocation {
46  #[inline]
47  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48    write!(f, "{}:{}", self.row, self.col);
49    Ok(())
50  }
51}
52
53#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
54pub struct SourceRange {
55  pub start: SourceLocation,
56  pub end:   SourceLocation,
57}
58
59/// Coordinates in SourceRange are 1-indexed, i.e. they directly translate
60/// human's view to line and column numbers.  Having value 0 means the 
61/// range is not initialized.
62impl Default for SourceRange {
63  fn default() -> Self {
64    SourceRange {
65      start: SourceLocation { row: 0, col: 0 },
66      end:   SourceLocation { row: 0, col: 0 },
67    }
68  }
69}
70
71impl fmt::Debug for SourceRange {
72  #[inline]
73  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74    write!(f, "[{:?}, {:?})", self.start, self.end);
75    Ok(())
76  }
77}
78
79pub fn merge_src_range(r1: SourceRange, r2: SourceRange) -> SourceRange {
80  SourceRange {
81    start: r1.start.min(r2.start),
82    end:   r2.end.max(r2.end),
83  }
84}
85
86#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
87pub enum TokenKind {
88  Alpha, Ampersand, Any, Apostrophe, AssignOperator, Asterisk, AsyncTransitionOperator, At,
89  Backslash, Bar, BoxDrawing,
90  Caret, CarriageReturn, CarriageReturnNewLine, Colon, CodeBlock, Comma,
91  Dash, DefineOperator, Digit, Dollar,
92  Emoji, Empty, Equal, Exclamation,
93  False,
94  Grave,
95  HashTag, Http,
96  Identifier, InlineCode, 
97  LeftAngle, LeftBrace, LeftBracket, LeftParenthesis,
98  Newline, Not, Number,
99  OutputOperator,
100  Percent, Period, Plus,
101  Question, Quote,
102  RightAngle, RightBrace, RightBracket, RightParenthesis,
103  Semicolon, Space, Slash, String,
104  Tab, Text, Tilde, Title, TransitionOperator, True,
105  Underscore,
106}
107
108#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
109pub struct Token { 
110  pub kind: TokenKind, 
111  pub chars: Vec<char>, 
112  pub src_range: SourceRange 
113}
114
115impl fmt::Debug for Token {
116  #[inline]
117  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
118    write!(f, "{:?}:{:?}:{:?}", self.kind, String::from_iter(self.chars.iter().cloned()), self.src_range);
119    Ok(())
120  }
121}
122
123impl Default for Token {
124  fn default() -> Self {
125    Token{
126      kind: TokenKind::Empty,
127      chars: vec![],
128      src_range: SourceRange::default(),
129    }
130  }
131}
132
133impl Token {
134
135  pub fn new(kind: TokenKind, src_range: SourceRange, chars: Vec<char>) -> Token {
136    Token{kind, chars, src_range}
137  }
138
139  pub fn to_string(&self) -> String {
140    self.chars.iter().collect()
141  }
142
143  pub fn merge_tokens(tokens: &mut Vec<Token>) -> Option<Token> {
144    if tokens.len() == 0 {
145      None
146    } else if tokens.len() == 1 {
147      Some(tokens[0].clone())
148    } else {
149      let first = tokens[0].src_range.clone();
150      let kind = tokens[0].kind.clone();
151      let last = tokens.last().unwrap().src_range.clone();
152      let src_range = merge_src_range(first, last);
153      let chars: Vec<char> = tokens.iter_mut().fold(vec![],|mut m, ref mut t| {m.append(&mut t.chars.clone()); m});
154      let merged_token = Token{kind, chars, src_range};
155      Some(merged_token)
156    }
157  }
158}
159
160#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
161pub struct TableOfContents {
162    pub title: Option<Title>,
163    pub sections: Vec<Section>,
164}
165
166#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
167pub struct Program {
168  pub title: Option<Title>,
169  pub body: Body,
170}
171
172impl Program {
173  pub fn tokens(&self) -> Vec<Token> {
174    /*let mut title_tokens = match self.title.tokens() {
175      Some(tkns) => tkns,
176      None => vec![],
177    };*/
178    let body_tokens = self.body.tokens();
179    body_tokens
180  }
181
182  pub fn table_of_contents(&self) -> TableOfContents {
183    let mut sections = vec![];
184    for s in &self.body.sections {
185      sections.push(s.table_of_contents());
186    }
187    TableOfContents {
188      title: self.title.clone(),
189      sections,
190    }
191  }
192
193  pub fn pretty_print(&self) -> String {
194    let json_string = serde_json::to_string_pretty(self).unwrap();
195  
196    let depth = |line: &str|->usize{line.chars().take_while(|&c| c == ' ').count()};
197    let mut result = String::new();
198    let lines: Vec<&str> = json_string.lines().collect();
199    result.push_str("Program\n");
200    for (index, line) in lines.iter().enumerate() {
201      let trm = line.trim();
202      if trm == "}" || 
203         trm == "},"|| 
204         trm == "{" || 
205         trm == "[" || 
206         trm == "],"|| 
207         trm == "]" {
208        continue;
209      }
210  
211      // Count leading spaces to determine depth
212      let d = depth(line);
213      // Construct the tree-like prefix
214      let mut prefix = String::new();
215      for _ in 0..d {
216        prefix.push_str(" ");
217      }
218      if index == lines.len() {
219        prefix.push_str("└ ");
220      } else {
221        if depth(lines[index + 1]) != d {
222          prefix.push_str("└ ");
223        } else {
224          prefix.push_str("├ ");
225        }
226      }
227      let trm = line.trim();
228      let trm = trm.trim_end_matches('{')
229                    .trim_start_matches('"')
230                    .trim_end_matches(':')
231                    .trim_end_matches('"')
232                    .trim_end_matches('[');
233      prefix.push_str(trm);
234  
235      // Append formatted line to result
236      result.push_str(&prefix);
237      result.push('\n');
238      result = result.replace("\":", "");
239    }
240    let mut indexed_str = IndexedString::new(&result);
241    'rows: for i in 0..indexed_str.rows {
242      let rowz = &indexed_str.index_map[i];
243      for j in 0..rowz.len() {
244        let c = match indexed_str.get(i,j) {
245          Some(c) => c,
246          None => continue,
247        };
248        if c == '└' {
249          for k in i+1..indexed_str.rows {
250            match indexed_str.get(k,j) {
251              Some(c2) => {
252                if c2 == '└' {
253                  indexed_str.set(i,j,'├');
254                  for l in i+1..k {
255                    match indexed_str.get(l,j) {
256                      Some(' ') => {indexed_str.set(l,j,'│');},
257                      Some('└') => {indexed_str.set(l,j,'├');},
258                      _ => (),
259                    }
260                  }
261                } else if c2 == ' ' {
262                  continue;
263                } else {
264                  continue 'rows;
265                }
266              },
267              None => continue,
268            }
269  
270          }
271        } else if c == ' ' || c == '│' {
272          continue;
273        } else {
274          continue 'rows;
275        }
276      }
277    }
278    indexed_str.to_string()
279  }
280
281}
282
283#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
284pub struct Title {
285  pub text: Token,
286}
287
288impl Title {
289
290  pub fn to_string(&self) -> String {
291    self.text.to_string()
292  }
293
294}
295
296#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
297pub struct Body {
298  pub sections: Vec<Section>,
299}
300
301impl Body {
302  pub fn tokens(&self) -> Vec<Token> {
303    let mut out = vec![];
304    for s in &self.sections {
305      let mut tkns = s.tokens();
306      out.append(&mut tkns);
307    }
308    out
309  }
310}
311
312#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
313pub enum ColumnAlignment {
314  Left,
315  Center,
316  Right,
317}
318
319#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
320pub struct MarkdownTable {
321  pub header: Vec<Paragraph>,
322  pub rows: Vec<Vec<Paragraph>>,
323  pub alignment: Vec<ColumnAlignment>,
324}
325
326#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
327pub struct Subtitle {
328  pub text: Paragraph,
329  pub level: u8,
330}
331
332impl Subtitle {
333  pub fn to_string(&self) -> String {
334    self.text.to_string()
335  }
336}
337
338#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
339pub struct Section {
340  pub subtitle: Option<Subtitle>,
341  pub elements: Vec<SectionElement>,
342}
343
344impl Section {
345
346  pub fn table_of_contents(&self) -> Section {
347    let mut elements = vec![];
348    for e in &self.elements {
349      match e {
350        SectionElement::Section(s) => {
351          elements.push(SectionElement::Section(Box::new(s.table_of_contents())));
352        },
353        _ => (),
354      }
355    }
356    Section {
357      subtitle: self.subtitle.clone(),
358      elements,
359    }
360  }
361
362  pub fn tokens(&self) -> Vec<Token> {
363    let mut out = vec![];
364    for s in &self.elements {
365      let mut tkns = s.tokens();
366      out.append(&mut tkns);
367    }
368    out
369  }
370}
371
372#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
373pub struct Grammar {
374  pub rules: Vec<Rule>,
375}
376
377#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
378pub struct GrammarIdentifier {
379  pub name: Token,
380}
381
382impl GrammarIdentifier {
383  pub fn tokens(&self) -> Vec<Token> {
384    vec![self.name.clone()]
385  }
386
387  pub fn to_string(&self) -> String {
388    self.name.to_string()
389  }
390}
391
392#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
393pub struct Rule {
394  pub name: GrammarIdentifier,
395  pub expr: GrammarExpression,
396}
397
398#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
399pub enum GrammarExpression {
400  Choice(Vec<GrammarExpression>),
401  Sequence(Vec<GrammarExpression>),
402  Repeat0(Box<GrammarExpression>),
403  Repeat1(Box<GrammarExpression>),
404  Optional(Box<GrammarExpression>),
405  Terminal(Token),
406  List(Box<GrammarExpression>, Box<GrammarExpression>),
407  Definition(GrammarIdentifier),
408  Peek(Box<GrammarExpression>),
409  Not(Box<GrammarExpression>),
410  Range(Token,Token),
411  Group(Box<GrammarExpression>),
412}
413
414#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
415pub enum SectionElement {
416  Section(Box<Section>),
417  Comment(Comment),
418  Paragraph(Paragraph),
419  MechCode(Vec<MechCode>),
420  UnorderedList(UnorderedList),
421  Table(MarkdownTable),
422  CodeBlock(Token),
423  Grammar(Grammar),
424  BlockQuote(Paragraph),
425  ThematicBreak,
426  OrderedList,     // todo
427  Image,           // todo
428}
429
430impl SectionElement {
431  pub fn tokens(&self) -> Vec<Token> {
432    match self {
433      SectionElement::MechCode(codes) => {
434        let mut tokens = vec![];
435        for code in codes {
436          tokens.append(&mut code.tokens());
437        }
438        tokens
439      },
440      _ => todo!(),
441    }
442  }
443}
444
445pub type ListItem = Paragraph;
446
447#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
448pub struct UnorderedList {
449  pub items: Vec<ListItem>,
450}
451
452#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
453pub enum MechCode {
454  Expression(Expression),
455  Statement(Statement),
456  FsmSpecification(FsmSpecification),
457  FsmImplementation(FsmImplementation),
458  FunctionDefine(FunctionDefine),
459  Comment(Comment),
460}
461
462impl MechCode {
463  pub fn tokens(&self) -> Vec<Token> {
464    match self {
465      MechCode::Expression(x) => x.tokens(),
466      _ => todo!(),
467      //Statement(x) => x.tokens(),
468      //FunctionDefine(x) => x.tokens(),
469      //FsmSpecification(x) => x.tokens(),
470      //FsmImplementation(x) => x.tokens(),
471    }
472  }
473}
474
475#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
476pub struct FunctionDefine {
477  pub name: Identifier,
478  pub input: Vec<FunctionArgument>,
479  pub output: Vec<FunctionArgument>,
480  pub statements: Vec<Statement>,
481}
482
483#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
484pub struct FunctionArgument {
485  pub name: Identifier,
486  pub kind: KindAnnotation,
487}
488
489impl FunctionArgument {
490  pub fn tokens(&self) -> Vec<Token> {
491    let mut tokens = self.name.tokens();
492    tokens.append(&mut self.kind.tokens());
493    tokens
494  }
495}
496
497#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
498pub struct FsmImplementation {
499  pub name: Identifier,
500  pub input: Vec<Identifier>,
501  pub start: Pattern,
502  pub arms: Vec<FsmArm>,
503}
504
505#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
506pub enum FsmArm {
507  Guard(Pattern,Vec<Guard>),
508  Transition(Pattern,Vec<Transition>),
509}
510
511#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
512pub struct Guard { 
513  pub condition: Pattern,
514  pub transitions: Vec<Transition>,
515}
516
517#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
518pub enum Transition {
519  Next(Pattern),
520  Output(Pattern),
521  Async(Pattern),
522  CodeBlock(Vec<MechCode>),
523  Statement(Statement),
524}
525
526#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
527pub enum Pattern {
528  Wildcard,
529  Formula(Factor),
530  Expression(Expression),
531  TupleStruct(PatternTupleStruct),
532}
533
534#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
535pub struct PatternTupleStruct {
536  pub name: Identifier,
537  pub patterns: Vec<Pattern>,
538}
539
540pub type PatternTuple = Vec<Pattern>;
541
542#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
543pub struct FsmSpecification {
544  pub name: Identifier,
545  pub input: Vec<Var>,
546  pub output: Option<KindAnnotation>,
547  pub states: Vec<StateDefinition>,
548}
549
550#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
551pub struct StateDefinition {
552  pub name: Identifier,
553  pub state_variables: Option<Vec<Var>>,
554}
555
556#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
557pub enum Statement {
558  VariableDefine(VariableDefine),
559  VariableAssign(VariableAssign),
560  KindDefine(KindDefine),
561  EnumDefine(EnumDefine),
562  FsmDeclare(FsmDeclare),    
563  OpAssign(OpAssign), 
564  SplitTable,     // todo
565  FlattenTable,   // todo
566}
567
568#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
569pub struct FsmPipe {
570  pub start: FsmInstance,
571  pub transitions: Vec<Transition>
572}
573
574#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
575pub enum PipeElement {
576  Expression(Expression),
577  FsmInstance(FsmInstance),
578  Timer // todo
579}
580
581#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
582pub struct FsmDeclare {
583  pub fsm: Fsm,
584  pub pipe: FsmPipe,
585}
586
587#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
588pub struct Fsm {
589  pub name: Identifier,
590  pub args: Option<ArgumentList>,
591  pub kind: Option<KindAnnotation>
592}
593
594pub type FsmArgs = Vec<(Option<Identifier>,Expression)>;
595
596#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
597pub struct FsmInstance {
598  pub name: Identifier,
599  pub args: Option<FsmArgs>,
600}
601
602#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
603pub struct EnumDefine {
604  pub name: Identifier,
605  pub variants: Vec<EnumVariant>,
606}
607
608#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
609pub struct EnumVariant {
610  pub name: Identifier,
611  pub value: Option<KindAnnotation>,
612}
613
614#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
615pub struct KindDefine {
616  pub name: Identifier,
617  pub kind: KindAnnotation,
618}
619
620#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
621pub struct Record {
622  pub bindings: Vec<Binding>,
623}
624
625#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
626pub enum Structure {
627  Empty,
628  Record(Record),
629  Matrix(Matrix),
630  Table(Table),
631  Tuple(Tuple),
632  TupleStruct(TupleStruct),
633  Set(Set),
634  Map(Map),
635}
636
637impl Structure {
638  pub fn tokens(&self) -> Vec<Token> {
639    match self {
640      Structure::Matrix(mat) => mat.tokens(),
641      _ => todo!(),
642    }
643  }
644}
645
646#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
647pub struct Map {
648  pub elements: Vec<Mapping>,
649}
650
651#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
652pub struct Mapping {
653  pub key: Expression,
654  pub value: Expression,
655}
656
657#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
658pub struct Set {
659  pub elements: Vec<Expression>,
660}
661
662#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
663pub struct Atom {
664  pub name: Identifier,
665}
666
667#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
668pub struct TupleStruct {
669  pub name: Identifier,
670  pub value: Box<Expression>,
671}
672
673#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
674pub struct Matrix {
675  pub rows: Vec<MatrixRow>,
676}
677
678impl Matrix {
679  pub fn tokens(&self) -> Vec<Token> {
680    let mut tkns = vec![];
681    for r in &self.rows {
682      let mut t = r.tokens();
683      tkns.append(&mut t);
684    }
685    tkns
686  }
687}
688
689pub type TableHeader = Vec<Field>;
690
691#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
692pub struct Table {
693  pub header: TableHeader,
694  pub rows: Vec<TableRow>,
695}
696
697#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
698pub struct Field {
699  pub name: Identifier,
700  pub kind: Option<KindAnnotation>,
701}
702
703#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
704pub struct TableColumn {
705  pub element: Expression,
706}
707
708#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
709pub struct MatrixColumn {
710  pub element: Expression,
711}
712
713impl MatrixColumn {
714  pub fn tokens(&self) -> Vec<Token> {
715    self.element.tokens()
716  }
717}
718
719#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
720pub struct TableRow {
721  pub columns: Vec<TableColumn>,
722}
723
724#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
725pub struct MatrixRow {
726  pub columns: Vec<MatrixColumn>,
727}
728
729impl MatrixRow {
730  pub fn tokens(&self) -> Vec<Token> {
731    let mut tkns = vec![];
732    for r in &self.columns {
733      let mut t = r.tokens();
734      tkns.append(&mut t);
735    }
736    tkns
737  }
738}
739
740#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
741pub struct VariableDefine {
742  pub mutable: bool,
743  pub var: Var,
744  pub expression: Expression,
745}
746
747#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
748pub struct Var {
749  pub name: Identifier,
750  pub kind: Option<KindAnnotation>,
751}
752
753impl Var {
754  pub fn tokens(&self) -> Vec<Token> {
755    let mut tkns = self.name.tokens();
756    if let Some(knd) = &self.kind {
757      let mut t = knd.tokens();
758      tkns.append(&mut t);
759    }
760    tkns
761  }
762}
763
764
765#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
766pub struct VariableAssign {
767  pub target: SliceRef,
768  pub expression: Expression,
769}
770
771#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
772pub struct Identifier {
773  pub name: Token,
774}
775
776impl Identifier {
777  pub fn tokens(&self) -> Vec<Token> {
778    vec![self.name.clone()]
779  }
780
781  pub fn to_string(&self) -> String {
782    self.name.to_string()
783  }
784
785}
786
787
788impl Identifier {
789  pub fn hash(&self) -> u64 {
790    hash_chars(&self.name.chars)
791  }
792}
793
794#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
795pub struct Emoji {
796  pub tokens: Vec<Token>,
797}
798
799#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
800pub struct Word {
801  pub tokens: Vec<Token>,
802}
803
804#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
805pub struct Slice {
806  pub name: Identifier,
807  pub subscript: Vec<Subscript>
808}
809
810#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
811pub struct SliceRef {
812  pub name: Identifier,
813  pub subscript: Option<Vec<Subscript>>
814}
815
816#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
817pub enum Subscript {
818  Dot(Identifier),          // a.b
819  Swizzle(Vec<Identifier>), // a.b,c
820  Range(RangeExpression),   // a[1 + 1]
821  Formula(Factor),          // a[1 + 1]
822  All,                      // a[:]
823  Bracket(Vec<Subscript>),  // a[1,2,3]
824  Brace(Vec<Subscript>),    // a{"foo"}
825  DotInt(RealNumber)        // a.1
826}
827
828#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
829pub enum Expression {
830  Var(Var),
831  Range(Box<RangeExpression>),
832  Slice(Slice),
833  Formula(Factor),
834  Structure(Structure),
835  Literal(Literal),
836  FunctionCall(FunctionCall),
837  FsmPipe(FsmPipe),
838}
839
840impl Expression {
841  pub fn tokens(&self) -> Vec<Token> {
842    match self {
843      Expression::Var(v) => v.tokens(),
844      Expression::Literal(ltrl) => ltrl.tokens(),
845      Expression::Structure(strct) => strct.tokens(),
846      Expression::Formula(fctr) => fctr.tokens(),
847      _ => todo!(),
848    }
849  }
850}
851
852pub type ArgumentList = Vec<(Option<Identifier>,Expression)>;
853
854#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
855pub struct FunctionCall {
856  pub name: Identifier,
857  pub args: ArgumentList,
858}
859
860impl FunctionCall {
861  pub fn tokens(&self) -> Vec<Token> {
862    self.name.tokens()
863  }
864}
865
866#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
867pub struct Tuple {
868  pub elements: Vec<Expression>
869}
870
871#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
872pub struct Binding {
873  pub name: Identifier,
874  pub kind: Option<KindAnnotation>,
875  pub value: Expression,
876}
877
878#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
879pub struct KindAnnotation {
880  pub kind: Kind
881}
882
883impl KindAnnotation {
884
885  pub fn hash(&self) -> u64 {
886    match &self.kind {
887      Kind::Scalar(id) => id.hash(),
888      _ => todo!(),
889    }
890  }
891
892  pub fn tokens(&self) -> Vec<Token> {
893    self.kind.tokens()
894  }
895}
896
897#[derive(Clone, Debug, Hash, Serialize, Deserialize,Eq, PartialEq)]
898pub enum Kind {
899  Tuple(Vec<Kind>),
900  Bracket((Vec<Kind>,Vec<Literal>)),
901  Brace((Vec<Kind>,Vec<Literal>)),
902  Map(Box<Kind>,Box<Kind>),
903  Scalar(Identifier),
904  Atom(Identifier),
905  Function(Vec<Kind>,Vec<Kind>),
906  Fsm(Vec<Kind>,Vec<Kind>),
907  Empty,
908}
909
910impl Kind {
911  pub fn tokens(&self) -> Vec<Token> {
912    match self {
913      Kind::Tuple(x) => x.iter().flat_map(|k| k.tokens()).collect(),
914      Kind::Bracket((kinds, literals)) => {
915        kinds.iter().flat_map(|k| k.tokens())
916            .chain(literals.iter().flat_map(|l| l.tokens()))
917            .collect()
918      },
919      Kind::Brace((kinds, literals)) => {
920        kinds.iter().flat_map(|k| k.tokens())
921            .chain(literals.iter().flat_map(|l| l.tokens()))
922            .collect()
923      }
924      Kind::Map(x, y) => x.tokens().into_iter().chain(y.tokens()).collect(),
925      Kind::Scalar(x) => x.tokens(),
926      Kind::Atom(x) => x.tokens(),
927      Kind::Function(args, rets) => {
928        args.iter().flat_map(|k| k.tokens())
929            .chain(rets.iter().flat_map(|k| k.tokens()))
930            .collect()
931      }
932      Kind::Fsm(args, rets) => {
933        args.iter().flat_map(|k| k.tokens())
934            .chain(rets.iter().flat_map(|k| k.tokens()))
935            .collect()
936      }
937      Kind::Empty => vec![],
938    }
939  }
940}
941
942#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
943pub enum Literal {
944  Empty(Token),
945  Boolean(Token),
946  Number(Number),
947  String(MechString),
948  Atom(Atom),
949  TypedLiteral((Box<Literal>,KindAnnotation))
950}
951
952impl Literal {
953  pub fn tokens(&self) -> Vec<Token> {
954    match self {
955      Literal::Number(x) => x.tokens(),
956      Literal::Boolean(tkn) => vec![tkn.clone()],
957      Literal::String(strng) => vec![strng.text.clone()],
958      Literal::Atom(atm) => atm.name.tokens(),
959      _ => todo!(),
960    }
961  }
962}
963
964#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
965pub struct MechString {
966  pub text: Token,
967}
968
969#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
970pub enum ParagraphElement {
971  Text(Token),
972  Strong(Box<ParagraphElement>),            
973  Emphasis(Box<ParagraphElement>),
974  Underline(Box<ParagraphElement>),
975  Strikethrough(Box<ParagraphElement>),
976  Hyperlink((Token, Token)),
977  InlineCode(Token),                 
978  Link
979}
980
981impl ParagraphElement {
982
983  pub fn to_string(&self) -> String {
984    match self {
985      ParagraphElement::Text(t) => t.to_string(),
986      ParagraphElement::Strong(t) => t.to_string(),
987      ParagraphElement::Emphasis(t) => t.to_string(),
988      ParagraphElement::Underline(t) => t.to_string(),
989      ParagraphElement::Strikethrough(t) => t.to_string(),
990      ParagraphElement::InlineCode(t) => t.to_string(),
991      ParagraphElement::Hyperlink((t, u)) => {
992        format!("[{}]({})", t.to_string(), u.to_string())
993      }
994      _ => todo!(),
995    }
996  }
997
998}
999
1000#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1001pub struct Paragraph {
1002  pub elements: Vec<ParagraphElement>,
1003}
1004
1005impl Paragraph {
1006  pub fn to_string(&self) -> String {
1007    let mut out = "".to_string();
1008    for e in &self.elements {
1009      out.push_str(&e.to_string());
1010    }
1011    out
1012  }
1013}
1014
1015pub type Sign = bool;
1016pub type Numerator = Token;
1017pub type Denominator = Token;
1018pub type Whole = Token;
1019pub type Part = Token;
1020pub type Real = Box<Number>;
1021pub type Imaginary = Box<Number>;
1022pub type Base = (Whole, Part);
1023pub type Exponent = (Sign, Whole, Part);
1024
1025#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
1026pub enum Number {
1027  Real(RealNumber),
1028  Imaginary(ComplexNumber),
1029}
1030
1031impl Number {
1032  pub fn tokens(&self) -> Vec<Token> {
1033    match self {
1034      Number::Real(x) => x.tokens(),
1035      _ => todo!(),
1036    }
1037  }
1038}
1039
1040#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
1041pub enum RealNumber {
1042  Negated(Box<RealNumber>),
1043  Integer(Token),
1044  Float((Whole,Part)),
1045  Decimal(Token),
1046  Hexadecimal(Token),
1047  Octal(Token),
1048  Binary(Token),
1049  Scientific((Base,Exponent)),
1050  Rational((Numerator,Denominator)),
1051}
1052
1053impl RealNumber {
1054  pub fn tokens(&self) -> Vec<Token> {
1055    match self {
1056      RealNumber::Integer(tkn) => vec![tkn.clone()],
1057      _ => todo!(),
1058    }
1059  }
1060}
1061
1062#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
1063pub struct ImaginaryNumber {
1064  pub number: RealNumber,
1065}
1066
1067#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
1068pub struct ComplexNumber {
1069  pub real: Option<RealNumber>,
1070  pub imaginary: ImaginaryNumber
1071}
1072
1073#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1074pub struct Comment {
1075  pub text: Token,
1076}
1077
1078#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1079pub struct OpAssign {
1080  pub target: SliceRef,
1081  pub op: OpAssignOp,
1082  pub expression: Expression,
1083}
1084
1085#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1086pub enum OpAssignOp {
1087  Add,
1088  Sub,   
1089  Mul,
1090  Div,
1091  Exp,   
1092}
1093
1094#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1095pub enum RangeOp {
1096  Inclusive,
1097  Exclusive,      
1098}
1099
1100#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1101pub enum AddSubOp {
1102  Add,
1103  Sub
1104}
1105
1106#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1107pub enum MulDivOp {
1108  Mul,
1109  Div
1110}
1111
1112#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1113pub enum VecOp {
1114  MatMul,
1115  Solve,
1116  Dot,
1117  Cross,
1118}
1119
1120#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1121pub enum ExponentOp {
1122  Exp
1123}
1124
1125#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1126pub enum ComparisonOp {
1127  LessThan,
1128  GreaterThan,
1129  LessThanEqual,
1130  GreaterThanEqual,
1131  Equal,
1132  NotEqual,
1133}
1134
1135#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1136pub enum LogicOp {
1137  And,
1138  Or,
1139  Not,
1140  Xor,
1141}
1142
1143#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1144pub enum FormulaOperator {
1145  Logic(LogicOp),
1146  Comparison(ComparisonOp),
1147  AddSub(AddSubOp),
1148  MulDiv(MulDivOp),
1149  Exponent(ExponentOp),
1150  Vec(VecOp),
1151}
1152
1153#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1154pub struct RangeExpression {
1155  pub start: Factor,
1156  pub increment: Option<(RangeOp,Factor)>,
1157  pub operator: RangeOp,
1158  pub terminal: Factor,
1159}
1160
1161impl RangeExpression {
1162  pub fn tokens(&self) -> Vec<Token> {
1163    let mut tokens = self.start.tokens();
1164    tokens.append(&mut self.terminal.tokens());
1165    tokens
1166  }
1167}
1168
1169#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1170pub struct Term {
1171  pub lhs: Factor,
1172  pub rhs: Vec<(FormulaOperator,Factor)>
1173}
1174
1175impl Term {
1176  pub fn tokens(&self) -> Vec<Token> {
1177    let mut lhs_tkns = self.lhs.tokens();
1178    let mut rhs_tkns = vec![];
1179    for (op, r) in &self.rhs {
1180      let mut tkns = r.tokens();
1181      rhs_tkns.append(&mut tkns);
1182    }
1183    lhs_tkns.append(&mut rhs_tkns);
1184    lhs_tkns
1185  }
1186}
1187
1188#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1189pub enum Factor {
1190  Term(Box<Term>),
1191  Parenthetical(Box<Factor>),
1192  Expression(Box<Expression>),
1193  Negate(Box<Factor>),
1194  Not(Box<Factor>),
1195  Transpose(Box<Factor>),
1196}
1197
1198impl Factor {
1199  pub fn tokens(&self) -> Vec<Token> {
1200    match self {
1201      Factor::Term(x) => x.tokens(),
1202      Factor::Expression(x) => x.tokens(),
1203      Factor::Negate(x) => x.tokens(),
1204      Factor::Not(x) => x.tokens(),
1205      Factor::Transpose(x) => x.tokens(),
1206      Factor::Parenthetical(x) => x.tokens(),
1207    }
1208  }
1209}