mech_core/
nodes.rs

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