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