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  AbstractSigil, 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, EmphasisSigil, Empty, Equal, EquationSigil, Exclamation, 
93  False, FloatLeft, FloatRight, FootnotePrefix,
94  Grave,
95  HashTag, HighlightSigil, HttpPrefix,
96  Identifier, ImgPrefix, InlineCode, 
97  LeftAngle, LeftBrace, LeftBracket, LeftParenthesis,
98  Newline, Not, Number,
99  OutputOperator,
100  Percent, Period, Plus,
101  QuerySigil, Question, Quote, QuoteSigil,
102  RightAngle, RightBrace, RightBracket, RightParenthesis,
103  Semicolon, Space, Slash, String, StrikeSigil, StrongSigil,
104  Tab, Text, Tilde, Title, TransitionOperator, True,
105  UnderlineSigil, 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 elements: Vec<_> = self.elements.iter()
348      .filter(|e| matches!(e, SectionElement::Subtitle(_)))
349      .cloned()
350      .collect();
351    Section {
352      subtitle: self.subtitle.clone(),
353      elements,
354    }
355  }
356
357  pub fn tokens(&self) -> Vec<Token> {
358    let mut out = vec![];
359    for s in &self.elements {
360      let mut tkns = s.tokens();
361      out.append(&mut tkns);
362    }
363    out
364  }
365}
366
367#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
368pub struct Grammar {
369  pub rules: Vec<Rule>,
370}
371
372#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
373pub struct GrammarIdentifier {
374  pub name: Token,
375}
376
377impl GrammarIdentifier {
378  pub fn tokens(&self) -> Vec<Token> {
379    vec![self.name.clone()]
380  }
381
382  pub fn to_string(&self) -> String {
383    self.name.to_string()
384  }
385}
386
387#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
388pub struct Rule {
389  pub name: GrammarIdentifier,
390  pub expr: GrammarExpression,
391}
392
393#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
394pub enum GrammarExpression {
395  Choice(Vec<GrammarExpression>),
396  Definition(GrammarIdentifier),
397  Group(Box<GrammarExpression>),
398  List(Box<GrammarExpression>, Box<GrammarExpression>),
399  Not(Box<GrammarExpression>),
400  Optional(Box<GrammarExpression>),
401  Peek(Box<GrammarExpression>),
402  Repeat0(Box<GrammarExpression>),
403  Repeat1(Box<GrammarExpression>),
404  Range(Token,Token),
405  Sequence(Vec<GrammarExpression>),
406  Terminal(Token),
407}
408
409#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
410pub struct Citation {
411  pub id: Token,
412  pub text: Paragraph,
413}
414
415impl Citation {
416  pub fn to_string(&self) -> String {
417    format!("[{}]: {}", self.id.to_string(), self.text.to_string())
418  }
419}
420// This is just a temporary flag to store block state, 
421// I don't know exactly what I want to put in here yet
422type BlockConfig = u64; 
423
424pub type Footnote = (Token, Paragraph);
425
426#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
427pub enum FloatDirection {
428  Left,
429  Right,
430}
431
432#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
433pub enum SectionElement {
434  Abstract(Paragraph),
435  BlockQuote(Paragraph),
436  Citation(Citation),
437  CodeBlock(Token),
438  Comment(Comment),
439  Equation(Token),
440  FencedMechCode((Vec<MechCode>, BlockConfig)),
441  Float((Box<SectionElement>, FloatDirection)),
442  Footnote(Footnote),
443  Grammar(Grammar),
444  Image(Image),
445  List(MDList),
446  MechCode(Vec<MechCode>),
447  Paragraph(Paragraph),
448  Subtitle(Subtitle),
449  Table(MarkdownTable),
450  ThematicBreak,
451}
452
453impl SectionElement {
454  pub fn tokens(&self) -> Vec<Token> {
455    match self {
456      SectionElement::MechCode(codes) => {
457        let mut tokens = vec![];
458        for code in codes {
459          tokens.append(&mut code.tokens());
460        }
461        tokens
462      },
463      _ => todo!(),
464    }
465  }
466}
467
468#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
469pub struct Image {
470  pub src: Token,
471  pub caption: Option<Paragraph>,
472}
473
474impl Image {
475  pub fn to_string(&self) -> String {
476    let caption = match &self.caption {
477      Some(c) => c.to_string(),
478      None => "".to_string(),
479    };
480    format!("![{}]({})", caption, self.src.to_string())
481  }
482}
483
484pub type UnorderedList = Vec<((Option<Token>,Paragraph),Option<MDList>)>;
485pub type CheckList = Vec<((bool,Paragraph),Option<MDList>)>;
486
487#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
488pub struct OrderedList {
489  pub start: Number,
490  pub items: Vec<((Number,Paragraph),Option<MDList>)>,
491}
492
493#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
494pub enum MDList {
495  Unordered(UnorderedList),
496  Ordered(OrderedList),
497  Check(CheckList)
498}
499
500#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
501pub enum MechCode {
502  Comment(Comment),
503  Expression(Expression),
504  FsmImplementation(FsmImplementation),
505  FsmSpecification(FsmSpecification),
506  FunctionDefine(FunctionDefine),
507  Statement(Statement),
508}
509
510impl MechCode {
511  pub fn tokens(&self) -> Vec<Token> {
512    match self {
513      MechCode::Expression(x) => x.tokens(),
514      _ => todo!(),
515      //Statement(x) => x.tokens(),
516      //FunctionDefine(x) => x.tokens(),
517      //FsmSpecification(x) => x.tokens(),
518      //FsmImplementation(x) => x.tokens(),
519    }
520  }
521}
522
523#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
524pub struct FunctionDefine {
525  pub name: Identifier,
526  pub input: Vec<FunctionArgument>,
527  pub output: Vec<FunctionArgument>,
528  pub statements: Vec<Statement>,
529}
530
531#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
532pub struct FunctionArgument {
533  pub name: Identifier,
534  pub kind: KindAnnotation,
535}
536
537impl FunctionArgument {
538  pub fn tokens(&self) -> Vec<Token> {
539    let mut tokens = self.name.tokens();
540    tokens.append(&mut self.kind.tokens());
541    tokens
542  }
543}
544
545#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
546pub struct FsmImplementation {
547  pub name: Identifier,
548  pub input: Vec<Identifier>,
549  pub start: Pattern,
550  pub arms: Vec<FsmArm>,
551}
552
553#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
554pub enum FsmArm {
555  Guard(Pattern,Vec<Guard>),
556  Transition(Pattern,Vec<Transition>),
557}
558
559#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
560pub struct Guard { 
561  pub condition: Pattern,
562  pub transitions: Vec<Transition>,
563}
564
565#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
566pub enum Transition {
567  Async(Pattern),
568  CodeBlock(Vec<MechCode>),
569  Next(Pattern),
570  Output(Pattern),
571  Statement(Statement),
572}
573
574#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
575pub enum Pattern {
576  Expression(Expression),
577  Formula(Factor),
578  TupleStruct(PatternTupleStruct),
579  Wildcard,
580}
581
582#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
583pub struct PatternTupleStruct {
584  pub name: Identifier,
585  pub patterns: Vec<Pattern>,
586}
587
588pub type PatternTuple = Vec<Pattern>;
589
590#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
591pub struct FsmSpecification {
592  pub name: Identifier,
593  pub input: Vec<Var>,
594  pub output: Option<KindAnnotation>,
595  pub states: Vec<StateDefinition>,
596}
597
598#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
599pub struct StateDefinition {
600  pub name: Identifier,
601  pub state_variables: Option<Vec<Var>>,
602}
603
604#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
605pub enum Statement {
606  EnumDefine(EnumDefine),
607  FsmDeclare(FsmDeclare),
608  KindDefine(KindDefine),
609  OpAssign(OpAssign),
610  VariableAssign(VariableAssign),
611  VariableDefine(VariableDefine),
612  SplitTable,     // todo
613  FlattenTable,   // todo
614}
615
616#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
617pub struct FsmPipe {
618  pub start: FsmInstance,
619  pub transitions: Vec<Transition>
620}
621
622#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
623pub enum PipeElement {
624  Expression(Expression),
625  FsmInstance(FsmInstance),
626  Timer // todo
627}
628
629#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
630pub struct FsmDeclare {
631  pub fsm: Fsm,
632  pub pipe: FsmPipe,
633}
634
635#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
636pub struct Fsm {
637  pub name: Identifier,
638  pub args: Option<ArgumentList>,
639  pub kind: Option<KindAnnotation>
640}
641
642pub type FsmArgs = Vec<(Option<Identifier>,Expression)>;
643
644#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
645pub struct FsmInstance {
646  pub name: Identifier,
647  pub args: Option<FsmArgs>,
648}
649
650#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
651pub struct EnumDefine {
652  pub name: Identifier,
653  pub variants: Vec<EnumVariant>,
654}
655
656#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
657pub struct EnumVariant {
658  pub name: Identifier,
659  pub value: Option<KindAnnotation>,
660}
661
662#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
663pub struct KindDefine {
664  pub name: Identifier,
665  pub kind: KindAnnotation,
666}
667
668#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
669pub struct Record {
670  pub bindings: Vec<Binding>,
671}
672
673#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
674pub enum Structure {
675  Empty,
676  Map(Map),
677  Matrix(Matrix),
678  Record(Record),
679  Set(Set),
680  Table(Table),
681  Tuple(Tuple),
682  TupleStruct(TupleStruct),
683}
684
685impl Structure {
686  pub fn tokens(&self) -> Vec<Token> {
687    match self {
688      Structure::Matrix(mat) => mat.tokens(),
689      _ => todo!(),
690    }
691  }
692}
693
694#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
695pub struct Map {
696  pub elements: Vec<Mapping>,
697}
698
699#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
700pub struct Mapping {
701  pub key: Expression,
702  pub value: Expression,
703}
704
705#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
706pub struct Set {
707  pub elements: Vec<Expression>,
708}
709
710#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
711pub struct Atom {
712  pub name: Identifier,
713}
714
715#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
716pub struct TupleStruct {
717  pub name: Identifier,
718  pub value: Box<Expression>,
719}
720
721#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
722pub struct Matrix {
723  pub rows: Vec<MatrixRow>,
724}
725
726impl Matrix {
727  pub fn tokens(&self) -> Vec<Token> {
728    let mut tkns = vec![];
729    for r in &self.rows {
730      let mut t = r.tokens();
731      tkns.append(&mut t);
732    }
733    tkns
734  }
735}
736
737pub type TableHeader = Vec<Field>;
738
739#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
740pub struct Table {
741  pub header: TableHeader,
742  pub rows: Vec<TableRow>,
743}
744
745#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
746pub struct Field {
747  pub name: Identifier,
748  pub kind: Option<KindAnnotation>,
749}
750
751#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
752pub struct TableColumn {
753  pub element: Expression,
754}
755
756#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
757pub struct MatrixColumn {
758  pub element: Expression,
759}
760
761impl MatrixColumn {
762  pub fn tokens(&self) -> Vec<Token> {
763    self.element.tokens()
764  }
765}
766
767#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
768pub struct TableRow {
769  pub columns: Vec<TableColumn>,
770}
771
772#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
773pub struct MatrixRow {
774  pub columns: Vec<MatrixColumn>,
775}
776
777impl MatrixRow {
778  pub fn tokens(&self) -> Vec<Token> {
779    let mut tkns = vec![];
780    for r in &self.columns {
781      let mut t = r.tokens();
782      tkns.append(&mut t);
783    }
784    tkns
785  }
786}
787
788#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
789pub struct VariableDefine {
790  pub mutable: bool,
791  pub var: Var,
792  pub expression: Expression,
793}
794
795#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
796pub struct Var {
797  pub name: Identifier,
798  pub kind: Option<KindAnnotation>,
799}
800
801impl Var {
802  pub fn tokens(&self) -> Vec<Token> {
803    let mut tkns = self.name.tokens();
804    if let Some(knd) = &self.kind {
805      let mut t = knd.tokens();
806      tkns.append(&mut t);
807    }
808    tkns
809  }
810}
811
812
813#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
814pub struct VariableAssign {
815  pub target: SliceRef,
816  pub expression: Expression,
817}
818
819#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
820pub struct Identifier {
821  pub name: Token,
822}
823
824impl Identifier {
825  pub fn tokens(&self) -> Vec<Token> {
826    vec![self.name.clone()]
827  }
828
829  pub fn to_string(&self) -> String {
830    self.name.to_string()
831  }
832
833}
834
835
836impl Identifier {
837  pub fn hash(&self) -> u64 {
838    hash_chars(&self.name.chars)
839  }
840}
841
842#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
843pub struct Emoji {
844  pub tokens: Vec<Token>,
845}
846
847#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
848pub struct Word {
849  pub tokens: Vec<Token>,
850}
851
852#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
853pub struct Slice {
854  pub name: Identifier,
855  pub subscript: Vec<Subscript>
856}
857
858#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
859pub struct SliceRef {
860  pub name: Identifier,
861  pub subscript: Option<Vec<Subscript>>
862}
863
864#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
865pub enum Subscript {
866  All,                      // a[:]
867  Brace(Vec<Subscript>),    // a{"foo"}
868  Bracket(Vec<Subscript>),  // a[1,2,3]
869  Dot(Identifier),          // a.b
870  DotInt(RealNumber),       // a.1
871  Formula(Factor),          // a[1 + 1]
872  Range(RangeExpression),   // a[1 + 1]
873  Swizzle(Vec<Identifier>), // a.b,c
874}
875
876#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
877pub enum Expression {
878  Formula(Factor),
879  FunctionCall(FunctionCall),
880  FsmPipe(FsmPipe),
881  Literal(Literal),
882  Range(Box<RangeExpression>),
883  Slice(Slice),
884  Structure(Structure),
885  Var(Var),
886}
887
888impl Expression {
889  pub fn tokens(&self) -> Vec<Token> {
890    match self {
891      Expression::Var(v) => v.tokens(),
892      Expression::Literal(ltrl) => ltrl.tokens(),
893      Expression::Structure(strct) => strct.tokens(),
894      Expression::Formula(fctr) => fctr.tokens(),
895      _ => todo!(),
896    }
897  }
898}
899
900pub type ArgumentList = Vec<(Option<Identifier>,Expression)>;
901
902#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
903pub struct FunctionCall {
904  pub name: Identifier,
905  pub args: ArgumentList,
906}
907
908impl FunctionCall {
909  pub fn tokens(&self) -> Vec<Token> {
910    self.name.tokens()
911  }
912}
913
914#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
915pub struct Tuple {
916  pub elements: Vec<Expression>
917}
918
919#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
920pub struct Binding {
921  pub name: Identifier,
922  pub kind: Option<KindAnnotation>,
923  pub value: Expression,
924}
925
926#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
927pub struct KindAnnotation {
928  pub kind: Kind
929}
930
931impl KindAnnotation {
932
933  pub fn hash(&self) -> u64 {
934    match &self.kind {
935      Kind::Scalar(id) => id.hash(),
936      _ => todo!(),
937    }
938  }
939
940  pub fn tokens(&self) -> Vec<Token> {
941    self.kind.tokens()
942  }
943}
944
945#[derive(Clone, Debug, Hash, Serialize, Deserialize,Eq, PartialEq)]
946pub enum Kind {
947  Atom(Identifier),
948  Brace((Vec<Kind>,Vec<Literal>)),
949  Bracket((Vec<Kind>,Vec<Literal>)),
950  Empty,
951  Fsm(Vec<Kind>,Vec<Kind>),
952  Function(Vec<Kind>,Vec<Kind>),
953  Map(Box<Kind>,Box<Kind>),
954  Scalar(Identifier),
955  Tuple(Vec<Kind>),
956}
957
958impl Kind {
959  pub fn tokens(&self) -> Vec<Token> {
960    match self {
961      Kind::Tuple(x) => x.iter().flat_map(|k| k.tokens()).collect(),
962      Kind::Bracket((kinds, literals)) => {
963        kinds.iter().flat_map(|k| k.tokens())
964            .chain(literals.iter().flat_map(|l| l.tokens()))
965            .collect()
966      },
967      Kind::Brace((kinds, literals)) => {
968        kinds.iter().flat_map(|k| k.tokens())
969            .chain(literals.iter().flat_map(|l| l.tokens()))
970            .collect()
971      }
972      Kind::Map(x, y) => x.tokens().into_iter().chain(y.tokens()).collect(),
973      Kind::Scalar(x) => x.tokens(),
974      Kind::Atom(x) => x.tokens(),
975      Kind::Function(args, rets) => {
976        args.iter().flat_map(|k| k.tokens())
977            .chain(rets.iter().flat_map(|k| k.tokens()))
978            .collect()
979      }
980      Kind::Fsm(args, rets) => {
981        args.iter().flat_map(|k| k.tokens())
982            .chain(rets.iter().flat_map(|k| k.tokens()))
983            .collect()
984      }
985      Kind::Empty => vec![],
986    }
987  }
988}
989
990#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
991pub enum Literal {
992  Atom(Atom),
993  Boolean(Token),
994  Empty(Token),
995  Number(Number),
996  String(MechString),
997  TypedLiteral((Box<Literal>,KindAnnotation))
998}
999
1000impl Literal {
1001  pub fn tokens(&self) -> Vec<Token> {
1002    match self {
1003      Literal::Atom(atm) => atm.name.tokens(),
1004      Literal::Boolean(tkn) => vec![tkn.clone()],
1005      Literal::Number(x) => x.tokens(),
1006      Literal::String(strng) => vec![strng.text.clone()],
1007      _ => todo!(),
1008    }
1009  }
1010}
1011
1012#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1013pub struct MechString {
1014  pub text: Token,
1015}
1016
1017pub type Hyperlink = (Token, Token);
1018
1019#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1020pub enum ParagraphElement {
1021  Emphasis(Box<ParagraphElement>),
1022  FootnoteReference(Token),
1023  Highlight(Box<ParagraphElement>),
1024  Hyperlink(Hyperlink),
1025  InlineCode(Token),
1026  InlineMechCode(Expression),
1027  InlineEquation(Token),
1028  Reference(Token),
1029  Strikethrough(Box<ParagraphElement>),
1030  Strong(Box<ParagraphElement>),
1031  Text(Token),
1032  Underline(Box<ParagraphElement>),
1033}
1034
1035impl ParagraphElement {
1036
1037  pub fn to_string(&self) -> String {
1038    match self {
1039      ParagraphElement::Emphasis(t) => t.to_string(),
1040      ParagraphElement::FootnoteReference(t) => t.to_string(),
1041      ParagraphElement::Highlight(t) => t.to_string(),
1042      ParagraphElement::Hyperlink((t, u)) => {
1043        format!("[{}]({})", t.to_string(), u.to_string())
1044      }
1045      ParagraphElement::InlineCode(t) => t.to_string(),
1046      ParagraphElement::InlineEquation(t) => t.to_string(),
1047      ParagraphElement::InlineMechCode(t) => format!("{:?}", t),
1048      ParagraphElement::Reference(t) => t.to_string(),
1049      ParagraphElement::Strikethrough(t) => t.to_string(),
1050      ParagraphElement::Strong(t) => t.to_string(),
1051      ParagraphElement::Text(t) => t.to_string(),
1052      ParagraphElement::Underline(t) => t.to_string(),
1053    }
1054  }
1055
1056}
1057
1058#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1059pub struct Paragraph {
1060  pub elements: Vec<ParagraphElement>,
1061}
1062
1063impl Paragraph {
1064  pub fn to_string(&self) -> String {
1065    let mut out = "".to_string();
1066    for e in &self.elements {
1067      out.push_str(&e.to_string());
1068    }
1069    out
1070  }
1071}
1072
1073pub type Sign = bool;
1074pub type Numerator = Token;
1075pub type Denominator = Token;
1076pub type Whole = Token;
1077pub type Part = Token;
1078pub type Real = Box<Number>;
1079pub type Imaginary = Box<Number>;
1080pub type Base = (Whole, Part);
1081pub type Exponent = (Sign, Whole, Part);
1082
1083#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
1084pub enum Number {
1085  Real(RealNumber),
1086  Imaginary(ComplexNumber),
1087}
1088
1089impl Number {
1090
1091  pub fn from_integer(x: i64) -> Number {
1092    Number::Real(RealNumber::Integer(Token::new(TokenKind::Digit, SourceRange::default(), x.to_string().chars().collect())))
1093  }
1094
1095  pub fn to_string(&self) -> String {
1096    match self {
1097      Number::Real(x) => x.to_string(),
1098      Number::Imaginary(x) => x.to_string(),
1099    }
1100  }
1101
1102  pub fn tokens(&self) -> Vec<Token> {
1103    match self {
1104      Number::Real(x) => x.tokens(),
1105      _ => todo!(),
1106    }
1107  }
1108}
1109
1110#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
1111pub enum RealNumber {
1112  Binary(Token),
1113  Decimal(Token),
1114  Float((Whole,Part)),
1115  Hexadecimal(Token),
1116  Integer(Token),
1117  Negated(Box<RealNumber>),
1118  Octal(Token),
1119  Rational((Numerator,Denominator)),
1120  Scientific((Base,Exponent)),
1121}
1122
1123impl RealNumber {
1124  pub fn tokens(&self) -> Vec<Token> {
1125    match self {
1126      RealNumber::Integer(tkn) => vec![tkn.clone()],
1127      _ => todo!(),
1128    }
1129  }
1130  pub fn to_string(&self) -> String {
1131    match self {
1132      RealNumber::Integer(tkn) => tkn.to_string(),
1133      RealNumber::Float((whole, part)) => format!("{}.{}", whole.to_string(), part.to_string()),
1134      RealNumber::Binary(tkn) => format!("0b{}", tkn.to_string()),
1135      RealNumber::Hexadecimal(tkn) => format!("0x{}", tkn.to_string()),
1136      RealNumber::Octal(tkn) => format!("0o{}", tkn.to_string()),
1137      RealNumber::Decimal(tkn) => format!("0d{}", tkn.to_string()),
1138      RealNumber::Rational((num, den)) => format!("{}/{}", num.to_string(), den.to_string()),
1139      RealNumber::Scientific(((whole,part), exponent)) => {
1140        let (sign, whole, part) = exponent;
1141        let sign_str = if *sign { "+" } else { "-" };
1142        let whole_str = whole.to_string();
1143        let part_str = part.to_string();
1144        format!("{}{}.{}/10^{}", whole.to_string(), part.to_string(), sign_str, whole_str)
1145      }
1146      RealNumber::Negated(x) => format!("-{}", x.to_string()),
1147    }
1148  }
1149}
1150
1151#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
1152pub struct ImaginaryNumber {
1153  pub number: RealNumber,
1154}
1155
1156#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
1157pub struct ComplexNumber {
1158  pub real: Option<RealNumber>,
1159  pub imaginary: ImaginaryNumber
1160}
1161
1162impl ComplexNumber {
1163  pub fn tokens(&self) -> Vec<Token> {
1164    let mut tkns = vec![];
1165    if let Some(r) = &self.real {
1166      tkns.append(&mut r.tokens());
1167    }
1168    tkns.append(&mut self.imaginary.number.tokens());
1169    tkns
1170  }
1171
1172  pub fn to_string(&self) -> String {
1173    let mut out = "".to_string();
1174    if let Some(r) = &self.real {
1175      out.push_str(&r.to_string());
1176    }
1177    out.push_str("i");
1178    out
1179  }
1180}
1181
1182#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1183pub struct Comment {
1184  pub text: Token,
1185}
1186
1187#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1188pub struct OpAssign {
1189  pub target: SliceRef,
1190  pub op: OpAssignOp,
1191  pub expression: Expression,
1192}
1193
1194#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1195pub enum OpAssignOp {
1196  Add,
1197  Div,
1198  Exp, 
1199  Mod,  
1200  Mul,
1201  Sub,   
1202}
1203
1204#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1205pub enum RangeOp {
1206  Exclusive,      
1207  Inclusive,
1208}
1209
1210#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1211pub enum AddSubOp {
1212  Add,
1213  Sub,
1214}
1215
1216#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1217pub enum MulDivOp {
1218  Div,
1219  Mod,
1220  Mul,
1221}
1222
1223#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1224pub enum VecOp {
1225  Cross,
1226  Dot,
1227  MatMul,
1228  Solve,
1229}
1230
1231#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1232pub enum ExponentOp {
1233  Exp
1234}
1235
1236#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1237pub enum ComparisonOp {
1238  Equal,
1239  GreaterThan,
1240  GreaterThanEqual,
1241  LessThan,
1242  LessThanEqual,
1243  NotEqual,
1244}
1245
1246#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1247pub enum LogicOp {
1248  And,
1249  Not,
1250  Or,
1251  Xor,
1252}
1253
1254#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1255pub enum FormulaOperator {
1256  AddSub(AddSubOp),
1257  Comparison(ComparisonOp),
1258  Exponent(ExponentOp),
1259  Logic(LogicOp),
1260  MulDiv(MulDivOp),
1261  Vec(VecOp),
1262}
1263
1264#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1265pub struct RangeExpression {
1266  pub start: Factor,
1267  pub increment: Option<(RangeOp,Factor)>,
1268  pub operator: RangeOp,
1269  pub terminal: Factor,
1270}
1271
1272impl RangeExpression {
1273  pub fn tokens(&self) -> Vec<Token> {
1274    let mut tokens = self.start.tokens();
1275    tokens.append(&mut self.terminal.tokens());
1276    tokens
1277  }
1278}
1279
1280#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1281pub struct Term {
1282  pub lhs: Factor,
1283  pub rhs: Vec<(FormulaOperator,Factor)>
1284}
1285
1286impl Term {
1287  pub fn tokens(&self) -> Vec<Token> {
1288    let mut lhs_tkns = self.lhs.tokens();
1289    let mut rhs_tkns = vec![];
1290    for (op, r) in &self.rhs {
1291      let mut tkns = r.tokens();
1292      rhs_tkns.append(&mut tkns);
1293    }
1294    lhs_tkns.append(&mut rhs_tkns);
1295    lhs_tkns
1296  }
1297}
1298
1299#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1300pub enum Factor {
1301  Expression(Box<Expression>),
1302  Negate(Box<Factor>),
1303  Not(Box<Factor>),
1304  Parenthetical(Box<Factor>),
1305  Term(Box<Term>),
1306  Transpose(Box<Factor>),
1307}
1308
1309impl Factor {
1310  pub fn tokens(&self) -> Vec<Token> {
1311    match self {
1312      Factor::Expression(x) => x.tokens(),
1313      Factor::Negate(x) => x.tokens(),
1314      Factor::Not(x) => x.tokens(),
1315      Factor::Parenthetical(x) => x.tokens(),
1316      Factor::Term(x) => x.tokens(),
1317      Factor::Transpose(x) => x.tokens(),
1318    }
1319  }
1320}