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