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