mech_core/
nodes.rs

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