mech_core/
nodes.rs

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