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, GraveCodeBlockSigil,
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  Question, QuestionSigil, Quote, QuoteSigil,
105  RightAngle, RightBrace, RightBracket, RightParenthesis,
106  SectionSigil, Semicolon, Space, Slash, String, StrikeSigil, StrongSigil,
107  Tab, Text, Tilde, TildeCodeBlockSigil, 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 OptionMap {
511  pub elements: Vec<(Identifier, MechString)>,
512}
513
514
515#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
516#[derive(Clone, Debug, Hash, PartialEq, Eq)]
517pub struct Image {
518  pub src: Token,
519  pub caption: Option<Paragraph>,
520  pub style: Option<OptionMap>,
521}
522
523impl Image {
524  pub fn to_string(&self) -> String {
525    let caption = match &self.caption {
526      Some(c) => c.to_string(),
527      None => "".to_string(),
528    };
529    format!("![{}]({})", caption, self.src.to_string())
530  }
531}
532
533pub type UnorderedList = Vec<((Option<Token>,Paragraph),Option<MDList>)>;
534pub type CheckList = Vec<((bool,Paragraph),Option<MDList>)>;
535
536#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
537#[derive(Clone, Debug, Hash, PartialEq, Eq)]
538pub struct OrderedList {
539  pub start: Number,
540  pub items: Vec<((Number,Paragraph),Option<MDList>)>,
541}
542
543#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
544#[derive(Clone, Debug, Hash, PartialEq, Eq)]
545pub enum MDList {
546  Unordered(UnorderedList),
547  Ordered(OrderedList),
548  Check(CheckList)
549}
550
551#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
552#[derive(Clone, Debug, Hash, PartialEq, Eq)]
553pub enum MechCode {
554  Comment(Comment),
555  Expression(Expression),
556  //FsmImplementation(FsmImplementation),
557  //FsmSpecification(FsmSpecification),
558  FunctionDefine(FunctionDefine),
559  Statement(Statement),
560}
561
562impl MechCode {
563  pub fn tokens(&self) -> Vec<Token> {
564    match self {
565      MechCode::Expression(x) => x.tokens(),
566      MechCode::Statement(x) => x.tokens(),
567      _ => todo!(),
568      //FunctionDefine(x) => x.tokens(),
569      //FsmSpecification(x) => x.tokens(),
570      //FsmImplementation(x) => x.tokens(),
571    }
572  }
573}
574
575#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
576#[derive(Clone, Debug, Hash, PartialEq, Eq)]
577pub struct FunctionDefine {
578  pub name: Identifier,
579  pub input: Vec<FunctionArgument>,
580  pub output: Vec<FunctionArgument>,
581  pub statements: Vec<Statement>,
582}
583
584#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
585#[derive(Clone, Debug, Hash, PartialEq, Eq)]
586pub struct FunctionArgument {
587  pub name: Identifier,
588  pub kind: KindAnnotation,
589}
590
591impl FunctionArgument {
592  pub fn tokens(&self) -> Vec<Token> {
593    let mut tokens = self.name.tokens();
594    tokens.append(&mut self.kind.tokens());
595    tokens
596  }
597}
598
599#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
600#[derive(Clone, Debug, Hash, PartialEq, Eq)]
601pub struct FsmImplementation {
602  pub name: Identifier,
603  pub input: Vec<Identifier>,
604  pub start: Pattern,
605  pub arms: Vec<FsmArm>,
606}
607
608#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
609#[derive(Clone, Debug, Hash, PartialEq, Eq)]
610pub enum FsmArm {
611  Guard(Pattern,Vec<Guard>),
612  Transition(Pattern,Vec<Transition>),
613}
614
615#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
616#[derive(Clone, Debug, Hash, PartialEq, Eq)]
617pub struct Guard { 
618  pub condition: Pattern,
619  pub transitions: Vec<Transition>,
620}
621
622#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
623#[derive(Clone, Debug, Hash, PartialEq, Eq)]
624pub enum Transition {
625  Async(Pattern),
626  CodeBlock(Vec<(MechCode, Option<Comment>)>),
627  Next(Pattern),
628  Output(Pattern),
629  Statement(Statement),
630}
631
632#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
633#[derive(Clone, Debug, Hash, PartialEq, Eq)]
634pub enum Pattern {
635  Expression(Expression),
636  Formula(Factor),
637  TupleStruct(PatternTupleStruct),
638  Wildcard,
639}
640
641#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
642#[derive(Clone, Debug, Hash, PartialEq, Eq)]
643pub struct PatternTupleStruct {
644  pub name: Identifier,
645  pub patterns: Vec<Pattern>,
646}
647
648pub type PatternTuple = Vec<Pattern>;
649
650#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
651#[derive(Clone, Debug, Hash, PartialEq, Eq)]
652pub struct FsmSpecification {
653  pub name: Identifier,
654  pub input: Vec<Var>,
655  pub output: Option<KindAnnotation>,
656  pub states: Vec<StateDefinition>,
657}
658
659#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
660#[derive(Clone, Debug, Hash, PartialEq, Eq)]
661pub struct StateDefinition {
662  pub name: Identifier,
663  pub state_variables: Option<Vec<Var>>,
664}
665
666#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
667#[derive(Clone, Debug, Hash, PartialEq, Eq)]
668pub enum Statement {
669  EnumDefine(EnumDefine),
670  //FsmDeclare(FsmDeclare),
671  KindDefine(KindDefine),
672  OpAssign(OpAssign),
673  VariableAssign(VariableAssign),
674  VariableDefine(VariableDefine),
675  TupleDestructure(TupleDestructure),
676  SplitTable,     // todo
677  FlattenTable,   // todo
678}
679
680impl Statement {
681  pub fn tokens(&self) -> Vec<Token> {
682    match self {
683      Statement::EnumDefine(x) => x.tokens(),
684      //Statement::FsmDeclare(x) => x.tokens(),
685      Statement::KindDefine(x) => x.tokens(),
686      Statement::OpAssign(x) => x.tokens(),
687      Statement::VariableAssign(x) => x.tokens(),
688      Statement::VariableDefine(x) => x.tokens(),
689      Statement::TupleDestructure(x) => x.tokens(),
690      Statement::SplitTable => vec![], // todo
691      Statement::FlattenTable => vec![], // todo
692    }
693  }
694}
695
696#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
697#[derive(Clone, Debug, Hash, PartialEq, Eq)]
698pub struct TupleDestructure {
699  pub vars: Vec<Identifier>,
700  pub expression: Expression,
701}
702
703impl TupleDestructure {
704  pub fn tokens(&self) -> Vec<Token> {
705    let mut tokens = vec![];
706    for var in &self.vars {
707      tokens.append(&mut var.tokens());
708    }
709    tokens.append(&mut self.expression.tokens());
710    tokens
711  }
712}
713
714#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
715#[derive(Clone, Debug, Hash, PartialEq, Eq)]
716pub struct FsmPipe {
717  pub start: FsmInstance,
718  pub transitions: Vec<Transition>
719}
720
721#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
722#[derive(Clone, Debug, Hash, PartialEq, Eq)]
723pub enum PipeElement {
724  Expression(Expression),
725  FsmInstance(FsmInstance),
726  Timer // todo
727}
728
729#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
730#[derive(Clone, Debug, Hash, PartialEq, Eq)]
731pub struct FsmDeclare {
732  pub fsm: Fsm,
733  pub pipe: FsmPipe,
734}
735
736#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
737#[derive(Clone, Debug, Hash, PartialEq, Eq)]
738pub struct Fsm {
739  pub name: Identifier,
740  pub args: Option<ArgumentList>,
741  pub kind: Option<KindAnnotation>
742}
743
744pub type FsmArgs = Vec<(Option<Identifier>,Expression)>;
745
746#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
747#[derive(Clone, Debug, Hash, PartialEq, Eq)]
748pub struct FsmInstance {
749  pub name: Identifier,
750  pub args: Option<FsmArgs>,
751}
752
753#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
754#[derive(Clone, Debug, Hash, PartialEq, Eq)]
755pub struct EnumDefine {
756  pub name: Identifier,
757  pub variants: Vec<EnumVariant>,
758}
759
760impl EnumDefine {
761  pub fn tokens(&self) -> Vec<Token> {
762    let mut tokens = self.name.tokens();
763    for v in &self.variants {
764      tokens.append(&mut v.tokens());
765    }
766    tokens
767  }
768}
769
770#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
771#[derive(Clone, Debug, Hash, PartialEq, Eq)]
772pub struct EnumVariant {
773  pub name: Identifier,
774  pub value: Option<KindAnnotation>,
775}
776
777impl EnumVariant {
778  pub fn tokens(&self) -> Vec<Token> {
779    let mut tokens = self.name.tokens();
780    if let Some(value) = &self.value {
781      tokens.append(&mut value.tokens());
782    }
783    tokens
784  }
785}
786
787#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
788#[derive(Clone, Debug, Hash, PartialEq, Eq)]
789pub struct KindDefine {
790  pub name: Identifier,
791  pub kind: KindAnnotation,
792}
793
794impl KindDefine {
795  pub fn tokens(&self) -> Vec<Token> {
796    let mut tokens = self.name.tokens();
797    tokens.append(&mut self.kind.tokens());
798    tokens
799  }
800}
801
802#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
803#[derive(Clone, Debug, Hash, PartialEq, Eq)]
804pub struct Record {
805  pub bindings: Vec<Binding>,
806}
807
808#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
809#[derive(Clone, Debug, Hash, PartialEq, Eq)]
810pub enum Structure {
811  Empty,
812  Map(Map),
813  Matrix(Matrix),
814  Record(Record),
815  Set(Set),
816  Table(Table),
817  Tuple(Tuple),
818  TupleStruct(TupleStruct),
819}
820
821impl Structure {
822  pub fn tokens(&self) -> Vec<Token> {
823    match self {
824      Structure::Matrix(mat) => mat.tokens(),
825      _ => todo!(),
826    }
827  }
828}
829
830#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
831#[derive(Clone, Debug, Hash, PartialEq, Eq)]
832pub struct Map {
833  pub elements: Vec<Mapping>,
834}
835
836#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
837#[derive(Clone, Debug, Hash, PartialEq, Eq)]
838pub struct Mapping {
839  pub key: Expression,
840  pub value: Expression,
841}
842
843#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
844#[derive(Clone, Debug, Hash, PartialEq, Eq)]
845pub struct Set {
846  pub elements: Vec<Expression>,
847}
848
849#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
850#[derive(Clone, Debug, Hash, PartialEq, Eq)]
851pub struct Atom {
852  pub name: Identifier,
853}
854
855#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
856#[derive(Clone, Debug, Hash, PartialEq, Eq)]
857pub struct TupleStruct {
858  pub name: Identifier,
859  pub value: Box<Expression>,
860}
861
862#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
863#[derive(Clone, Debug, Hash, PartialEq, Eq)]
864pub struct Matrix {
865  pub rows: Vec<MatrixRow>,
866}
867
868impl Matrix {
869  pub fn tokens(&self) -> Vec<Token> {
870    let mut tkns = vec![];
871    for r in &self.rows {
872      let mut t = r.tokens();
873      tkns.append(&mut t);
874    }
875    tkns
876  }
877}
878
879pub type TableHeader = Vec<Field>;
880
881#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
882#[derive(Clone, Debug, Hash, PartialEq, Eq)]
883pub struct Table {
884  pub header: TableHeader,
885  pub rows: Vec<TableRow>,
886}
887
888#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
889#[derive(Clone, Debug, Hash, PartialEq, Eq)]
890pub struct Field {
891  pub name: Identifier,
892  pub kind: Option<KindAnnotation>,
893}
894
895#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
896#[derive(Clone, Debug, Hash, PartialEq, Eq)]
897pub struct TableColumn {
898  pub element: Expression,
899}
900
901#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
902#[derive(Clone, Debug, Hash, PartialEq, Eq)]
903pub struct MatrixColumn {
904  pub element: Expression,
905}
906
907impl MatrixColumn {
908  pub fn tokens(&self) -> Vec<Token> {
909    self.element.tokens()
910  }
911}
912
913#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
914#[derive(Clone, Debug, Hash, PartialEq, Eq)]
915pub struct TableRow {
916  pub columns: Vec<TableColumn>,
917}
918
919#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
920#[derive(Clone, Debug, Hash, PartialEq, Eq)]
921pub struct MatrixRow {
922  pub columns: Vec<MatrixColumn>,
923}
924
925impl MatrixRow {
926  pub fn tokens(&self) -> Vec<Token> {
927    let mut tkns = vec![];
928    for r in &self.columns {
929      let mut t = r.tokens();
930      tkns.append(&mut t);
931    }
932    tkns
933  }
934}
935
936#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
937#[derive(Clone, Debug, Hash, PartialEq, Eq)]
938pub struct VariableDefine {
939  pub mutable: bool,
940  pub var: Var,
941  pub expression: Expression,
942}
943
944impl VariableDefine {
945  pub fn tokens(&self) -> Vec<Token> {
946    let mut tkns = self.var.tokens();
947    tkns.append(&mut self.expression.tokens());
948    tkns
949  }
950}
951
952#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
953#[derive(Clone, Debug, Hash, PartialEq, Eq)]
954pub struct Var {
955  pub name: Identifier,
956  pub kind: Option<KindAnnotation>,
957}
958
959impl Var {
960  pub fn tokens(&self) -> Vec<Token> {
961    let mut tkns = self.name.tokens();
962    if let Some(knd) = &self.kind {
963      let mut t = knd.tokens();
964      tkns.append(&mut t);
965    }
966    tkns
967  }
968}
969
970
971#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
972#[derive(Clone, Debug, Hash, PartialEq, Eq)]
973pub struct VariableAssign {
974  pub target: SliceRef,
975  pub expression: Expression,
976}
977
978impl VariableAssign {
979  pub fn tokens(&self) -> Vec<Token> {
980    let mut tkns = self.target.tokens();
981    tkns.append(&mut self.expression.tokens());
982    tkns
983  }
984}
985
986#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
987#[derive(Clone, Debug, Hash, PartialEq, Eq)]
988pub struct Identifier {
989  pub name: Token,
990}
991
992impl Identifier {
993  pub fn tokens(&self) -> Vec<Token> {
994    vec![self.name.clone()]
995  }
996
997  pub fn to_string(&self) -> String {
998    self.name.to_string()
999  }
1000
1001}
1002
1003
1004impl Identifier {
1005  pub fn hash(&self) -> u64 {
1006    hash_chars(&self.name.chars)
1007  }
1008}
1009
1010#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1011#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1012pub struct Emoji {
1013  pub tokens: Vec<Token>,
1014}
1015
1016#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1017#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1018pub struct Word {
1019  pub tokens: Vec<Token>,
1020}
1021
1022#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1023#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1024pub struct Slice {
1025  pub name: Identifier,
1026  pub subscript: Vec<Subscript>
1027}
1028
1029impl Slice {
1030  pub fn tokens(&self) -> Vec<Token> {
1031    let mut tkns = self.name.tokens();
1032    for sub in &self.subscript {
1033      let mut sub_tkns = sub.tokens();
1034      tkns.append(&mut sub_tkns);
1035    }
1036    tkns
1037  }
1038}
1039
1040#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1041#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1042pub struct SliceRef {
1043  pub name: Identifier,
1044  pub subscript: Option<Vec<Subscript>>
1045}
1046
1047impl SliceRef {
1048  pub fn tokens(&self) -> Vec<Token> {
1049    let mut tkns = self.name.tokens();
1050    if let Some(subs) = &self.subscript {
1051      for sub in subs {
1052        let mut sub_tkns = sub.tokens();
1053        tkns.append(&mut sub_tkns);
1054      }
1055    }
1056    tkns
1057  }
1058}
1059
1060#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1061#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1062pub enum Subscript {
1063  All,                      // a[:]
1064  Brace(Vec<Subscript>),    // a{"foo"}
1065  Bracket(Vec<Subscript>),  // a[1,2,3]
1066  Dot(Identifier),          // a.b
1067  DotInt(RealNumber),       // a.1
1068  Formula(Factor),          // a[1 + 1]
1069  Range(RangeExpression),   // a[1 + 1]
1070  Swizzle(Vec<Identifier>), // a.b,c
1071}
1072
1073impl Subscript {
1074
1075  pub fn tokens(&self) -> Vec<Token> {
1076    match self {
1077      Subscript::All => vec![
1078        Token::new(TokenKind::Colon, SourceRange::default(), vec![':']),
1079      ],
1080      Subscript::Brace(subs) => {
1081        let mut tkns = vec![Token::new(TokenKind::LeftBrace, SourceRange::default(), vec!['{'])];
1082        for sub in subs {
1083          let mut sub_tkns = sub.tokens();
1084          tkns.append(&mut sub_tkns);
1085        }
1086        tkns.push(Token::new(TokenKind::RightBrace, SourceRange::default(), vec!['}']));
1087        tkns
1088      },
1089      Subscript::Bracket(subs) => {
1090        let mut tkns = vec![Token::new(TokenKind::LeftBracket, SourceRange::default(), vec!['['])];
1091        for sub in subs {
1092          let mut sub_tkns = sub.tokens();
1093          tkns.append(&mut sub_tkns);
1094        }
1095        tkns.push(Token::new(TokenKind::RightBracket, SourceRange::default(), vec![']']));
1096        tkns
1097      },
1098      Subscript::Dot(id) => id.tokens(),
1099      Subscript::DotInt(num) => num.tokens(),
1100      Subscript::Formula(factor) => factor.tokens(),
1101      Subscript::Range(range) => range.tokens(),
1102      Subscript::Swizzle(ids) => ids.iter().flat_map(|id| id.tokens()).collect(),
1103    }
1104  }
1105}
1106
1107#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1108#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1109pub enum Expression {
1110  Formula(Factor),
1111  FunctionCall(FunctionCall),
1112  //FsmPipe(FsmPipe),
1113  Literal(Literal),
1114  Range(Box<RangeExpression>),
1115  Slice(Slice),
1116  Structure(Structure),
1117  Var(Var),
1118}
1119
1120impl Expression {
1121  pub fn tokens(&self) -> Vec<Token> {
1122    match self {
1123      Expression::Var(v) => v.tokens(),
1124      Expression::Literal(ltrl) => ltrl.tokens(),
1125      Expression::Structure(strct) => strct.tokens(),
1126      Expression::Formula(fctr) => fctr.tokens(),
1127      Expression::Range(range) => range.tokens(),
1128      Expression::Slice(slice) => slice.tokens(),
1129      _ => todo!(),
1130    }
1131  }
1132}
1133
1134pub type ArgumentList = Vec<(Option<Identifier>,Expression)>;
1135
1136#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1137#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1138pub struct FunctionCall {
1139  pub name: Identifier,
1140  pub args: ArgumentList,
1141}
1142
1143impl FunctionCall {
1144  pub fn tokens(&self) -> Vec<Token> {
1145    self.name.tokens()
1146  }
1147}
1148
1149#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1150#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1151pub struct Tuple {
1152  pub elements: Vec<Expression>
1153}
1154
1155#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1156#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1157pub struct Binding {
1158  pub name: Identifier,
1159  pub kind: Option<KindAnnotation>,
1160  pub value: Expression,
1161}
1162
1163#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1164#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1165pub struct KindAnnotation {
1166  pub kind: Kind,
1167}
1168
1169impl KindAnnotation {
1170
1171  pub fn hash(&self) -> u64 {
1172    #[cfg(feature = "no_std")]
1173    let mut hasher = FxHasher::default();
1174    #[cfg(not(feature = "no_std"))]
1175    let mut hasher = std::collections::hash_map::DefaultHasher::new();
1176    self.kind.hash(&mut hasher);
1177    hasher.finish()
1178  }
1179
1180  pub fn tokens(&self) -> Vec<Token> {
1181    self.kind.tokens()
1182  }
1183}
1184
1185#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1186#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1187pub enum Kind {
1188  Any,
1189  Atom(Identifier),
1190  Table((Vec<(Identifier,Kind)>,Box<Literal>)),
1191  Set(Box<Kind>,Option<Box<Literal>>),
1192  Record((Vec<(Identifier,Kind)>)),
1193  Empty,
1194  //Fsm(Vec<Kind>,Vec<Kind>),
1195  //Function(Vec<Kind>,Vec<Kind>),
1196  Map(Box<Kind>,Box<Kind>),
1197  Matrix((Box<Kind>,Vec<Literal>)),
1198  Option(Box<Kind>),
1199  Scalar(Identifier),
1200  Tuple(Vec<Kind>),
1201}
1202
1203impl Kind {
1204  pub fn tokens(&self) -> Vec<Token> {
1205    match self {
1206      Kind::Option(x) => x.tokens(),
1207      Kind::Tuple(x) => x.iter().flat_map(|k| k.tokens()).collect(),
1208      Kind::Matrix((kind, literals)) => {
1209        let mut tokens = kind.tokens();
1210        for l in literals {
1211          tokens.append(&mut l.tokens());
1212        }
1213        tokens
1214      },
1215      Kind::Record(kinds) => {
1216        let mut tokens = vec![];
1217        for (id, kind) in kinds {
1218          tokens.append(&mut id.tokens());
1219          tokens.append(&mut kind.tokens());
1220        }
1221        tokens
1222      }
1223      Kind::Set(kind, size) => {
1224        let mut tokens = kind.tokens();
1225        if let Some(literal) = size {
1226          tokens.append(&mut literal.tokens());
1227        }
1228        tokens
1229      }
1230      Kind::Table((kinds, literal)) => {
1231        let mut tokens = vec![];
1232        for (id, kind) in kinds {
1233          tokens.append(&mut id.tokens());
1234          tokens.append(&mut kind.tokens());
1235        }
1236        tokens.append(&mut literal.tokens());
1237        tokens
1238      }
1239      Kind::Map(x, y) => x.tokens().into_iter().chain(y.tokens()).collect(),
1240      Kind::Scalar(x) => x.tokens(),
1241      Kind::Atom(x) => x.tokens(),
1242      Kind::Empty => vec![],
1243      Kind::Any => vec![],
1244    }
1245  }
1246}
1247
1248#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1249#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1250pub enum Literal {
1251  Atom(Atom),
1252  Boolean(Token),
1253  Empty(Token),
1254  Number(Number),
1255  String(MechString),
1256  Kind(Kind),
1257  TypedLiteral((Box<Literal>,KindAnnotation))
1258}
1259
1260impl Literal {
1261  pub fn tokens(&self) -> Vec<Token> {
1262    match self {
1263      Literal::Atom(atm) => atm.name.tokens(),
1264      Literal::Boolean(tkn) => vec![tkn.clone()],
1265      Literal::Number(x) => x.tokens(),
1266      Literal::String(strng) => vec![strng.text.clone()],
1267      Literal::Empty(tkn) => vec![tkn.clone()],
1268      Literal::Kind(knd) => knd.tokens(),
1269      Literal::TypedLiteral((lit, knd)) => {
1270        let mut tokens = lit.tokens();
1271        tokens.append(&mut knd.tokens());
1272        tokens
1273      }
1274    }
1275  }
1276}
1277
1278#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1279#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1280pub struct MechString {
1281  pub text: Token,
1282}
1283
1284impl MechString {
1285  pub fn to_string(&self) -> String {
1286    self.text.to_string()
1287  }
1288}
1289
1290pub type Hyperlink = (Token, Token);
1291
1292#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1293#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1294pub enum ParagraphElement {
1295  Emphasis(Box<ParagraphElement>),
1296  FootnoteReference(Token),
1297  Highlight(Box<ParagraphElement>),
1298  Hyperlink(Hyperlink),
1299  InlineCode(Token),
1300  EvalInlineMechCode(Expression),
1301  InlineMechCode(MechCode),
1302  InlineEquation(Token),
1303  Reference(Token),
1304  SectionReference(Token),
1305  Strikethrough(Box<ParagraphElement>),
1306  Strong(Box<ParagraphElement>),
1307  Text(Token),
1308  Underline(Box<ParagraphElement>),
1309}
1310
1311impl ParagraphElement {
1312
1313  pub fn to_string(&self) -> String {
1314    match self {
1315      ParagraphElement::Emphasis(t) => t.to_string(),
1316      ParagraphElement::FootnoteReference(t) => t.to_string(),
1317      ParagraphElement::Highlight(t) => t.to_string(),
1318      ParagraphElement::Hyperlink((t, u)) => {
1319        format!("[{}]({})", t.to_string(), u.to_string())
1320      }
1321      ParagraphElement::InlineCode(t) => t.to_string(),
1322      ParagraphElement::InlineEquation(t) => t.to_string(),
1323      ParagraphElement::InlineMechCode(t) => format!("{:?}", t),
1324      ParagraphElement::EvalInlineMechCode(t) => format!("{:?}", t),
1325      ParagraphElement::Reference(t) => t.to_string(),
1326      ParagraphElement::SectionReference(t) => t.to_string(),
1327      ParagraphElement::Strikethrough(t) => t.to_string(),
1328      ParagraphElement::Strong(t) => t.to_string(),
1329      ParagraphElement::Text(t) => t.to_string(),
1330      ParagraphElement::Underline(t) => t.to_string(),
1331    }
1332  }
1333
1334}
1335
1336#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1337#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1338pub struct Paragraph {
1339  pub elements: Vec<ParagraphElement>,
1340}
1341
1342impl Paragraph {
1343  pub fn to_string(&self) -> String {
1344    let mut out = "".to_string();
1345    for e in &self.elements {
1346      out.push_str(&e.to_string());
1347    }
1348    out
1349  }
1350}
1351
1352pub type Sign = bool;
1353pub type Numerator = Token;
1354pub type Denominator = Token;
1355pub type Whole = Token;
1356pub type Part = Token;
1357pub type Real = Box<Number>;
1358pub type Imaginary = Box<Number>;
1359pub type Base = (Whole, Part);
1360pub type Exponent = (Sign, Whole, Part);
1361
1362#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1363#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1364pub enum Number {
1365  Real(RealNumber),
1366  Complex(C64Node),
1367}
1368
1369impl Number {
1370
1371  pub fn from_integer(x: i64) -> Number {
1372    Number::Real(RealNumber::Integer(Token::new(TokenKind::Digit, SourceRange::default(), x.to_string().chars().collect())))
1373  }
1374
1375  pub fn to_string(&self) -> String {
1376    match self {
1377      Number::Real(x) => x.to_string(),
1378      Number::Complex(x) => x.to_string(),
1379    }
1380  }
1381
1382  pub fn tokens(&self) -> Vec<Token> {
1383    match self {
1384      Number::Real(x) => x.tokens(),
1385      Number::Complex(x) => x.tokens(),
1386    }
1387  }
1388}
1389
1390#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1391#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1392pub enum RealNumber {
1393  Binary(Token),
1394  Decimal(Token),
1395  Float((Whole,Part)),
1396  Hexadecimal(Token),
1397  Integer(Token),
1398  Negated(Box<RealNumber>),
1399  Octal(Token),
1400  Rational((Numerator,Denominator)),
1401  Scientific((Base,Exponent)),
1402}
1403
1404impl RealNumber {
1405  pub fn tokens(&self) -> Vec<Token> {
1406    match self {
1407      RealNumber::Integer(tkn) => vec![tkn.clone()],
1408      _ => todo!(),
1409    }
1410  }
1411  pub fn to_string(&self) -> String {
1412    match self {
1413      RealNumber::Integer(tkn) => tkn.to_string(),
1414      RealNumber::Float((whole, part)) => format!("{}.{}", whole.to_string(), part.to_string()),
1415      RealNumber::Binary(tkn) => format!("0b{}", tkn.to_string()),
1416      RealNumber::Hexadecimal(tkn) => format!("0x{}", tkn.to_string()),
1417      RealNumber::Octal(tkn) => format!("0o{}", tkn.to_string()),
1418      RealNumber::Decimal(tkn) => format!("0d{}", tkn.to_string()),
1419      RealNumber::Rational((num, den)) => format!("{}/{}", num.to_string(), den.to_string()),
1420      RealNumber::Scientific(((whole,part), exponent)) => {
1421        let (sign, whole, part) = exponent;
1422        let sign_str = if *sign { "+" } else { "-" };
1423        let whole_str = whole.to_string();
1424        let part_str = part.to_string();
1425        format!("{}{}.{}/10^{}", whole.to_string(), part.to_string(), sign_str, whole_str)
1426      }
1427      RealNumber::Negated(x) => format!("-{}", x.to_string()),
1428    }
1429  }
1430}
1431
1432#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1433#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1434pub struct ImaginaryNumber {
1435  pub number: RealNumber,
1436}
1437
1438#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1439#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1440pub struct C64Node {
1441  pub real: Option<RealNumber>,
1442  pub imaginary: ImaginaryNumber
1443}
1444
1445impl C64Node {
1446  pub fn tokens(&self) -> Vec<Token> {
1447    let mut tkns = vec![];
1448    if let Some(r) = &self.real {
1449      tkns.append(&mut r.tokens());
1450    }
1451    tkns.append(&mut self.imaginary.number.tokens());
1452    tkns
1453  }
1454
1455  pub fn to_string(&self) -> String {
1456    let mut out = "".to_string();
1457    if let Some(r) = &self.real {
1458      out.push_str(&r.to_string());
1459    }
1460    out.push_str("i");
1461    out
1462  }
1463}
1464
1465#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1466#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1467pub struct Comment {
1468  pub paragraph: Paragraph,
1469}
1470
1471#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1472#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1473pub struct OpAssign {
1474  pub target: SliceRef,
1475  pub op: OpAssignOp,
1476  pub expression: Expression,
1477}
1478
1479impl OpAssign {
1480  pub fn tokens(&self) -> Vec<Token> {
1481    let mut tkns = self.target.tokens();
1482    tkns.append(&mut self.op.tokens());
1483    tkns.append(&mut self.expression.tokens());
1484    tkns
1485  }
1486}
1487
1488#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1489#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1490pub enum OpAssignOp {
1491  Add,
1492  Div,
1493  Exp, 
1494  Mod,  
1495  Mul,
1496  Sub,   
1497}
1498
1499impl OpAssignOp {
1500  pub fn tokens(&self) -> Vec<Token> {
1501    match self {
1502      OpAssignOp::Add => vec![Token::new(TokenKind::Plus, SourceRange::default(), vec!['+'])],
1503      OpAssignOp::Div => vec![Token::new(TokenKind::Slash, SourceRange::default(), vec!['/'])],
1504      OpAssignOp::Exp => vec![Token::new(TokenKind::Caret, SourceRange::default(), vec!['^'])],
1505      OpAssignOp::Mod => vec![Token::new(TokenKind::Percent, SourceRange::default(), vec!['%'])],
1506      OpAssignOp::Mul => vec![Token::new(TokenKind::Asterisk, SourceRange::default(), vec!['*'])],
1507      OpAssignOp::Sub => vec![Token::new(TokenKind::Dash, SourceRange::default(), vec!['-'])],
1508    }
1509  }
1510}
1511
1512#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1513#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1514pub enum RangeOp {
1515  Exclusive,      
1516  Inclusive,
1517}
1518
1519#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1520#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1521pub enum AddSubOp {
1522  Add,
1523  Sub,
1524}
1525
1526#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1527#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1528pub enum MulDivOp {
1529  Div,
1530  Mod,
1531  Mul,
1532}
1533
1534#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1535#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1536pub enum VecOp {
1537  Cross,
1538  Dot,
1539  MatMul,
1540  Solve,
1541}
1542
1543#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1544#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1545pub enum ExponentOp {
1546  Exp
1547}
1548
1549#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1550#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1551pub enum ComparisonOp {
1552  Equal,
1553  GreaterThan,
1554  GreaterThanEqual,
1555  LessThan,
1556  LessThanEqual,
1557  NotEqual,
1558  StrictEqual,
1559  StrictNotEqual,
1560}
1561
1562#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1563#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1564pub enum LogicOp {
1565  And,
1566  Not,
1567  Or,
1568  Xor,
1569}
1570
1571#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1572#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1573pub enum FormulaOperator {
1574  AddSub(AddSubOp),
1575  Comparison(ComparisonOp),
1576  Exponent(ExponentOp),
1577  Logic(LogicOp),
1578  MulDiv(MulDivOp),
1579  Vec(VecOp),
1580  Table(TableOp),
1581  Set(SetOp),
1582}
1583
1584#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1585#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1586pub enum TableOp {
1587  InnerJoin,
1588  LeftOuterJoin,
1589  RightOuterJoin	,
1590  FullOuterJoin	,
1591  LeftSemiJoin,  
1592  LeftAntiJoin,
1593}
1594
1595#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1596#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1597pub enum SetOp {
1598  Union,
1599  Intersection,
1600  Difference,
1601  Complement,
1602  Subset,
1603  Superset,
1604  ProperSubset,
1605  ProperSuperset,
1606  ElementOf,
1607  NotElementOf,
1608}
1609
1610#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1611#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1612pub struct RangeExpression {
1613  pub start: Factor,
1614  pub increment: Option<(RangeOp,Factor)>,
1615  pub operator: RangeOp,
1616  pub terminal: Factor,
1617}
1618
1619impl RangeExpression {
1620  pub fn tokens(&self) -> Vec<Token> {
1621    let mut tokens = self.start.tokens();
1622    tokens.append(&mut self.terminal.tokens());
1623    tokens
1624  }
1625}
1626
1627#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1628#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1629pub struct Term {
1630  pub lhs: Factor,
1631  pub rhs: Vec<(FormulaOperator,Factor)>
1632}
1633
1634impl Term {
1635  pub fn tokens(&self) -> Vec<Token> {
1636    let mut lhs_tkns = self.lhs.tokens();
1637    let mut rhs_tkns = vec![];
1638    for (op, r) in &self.rhs {
1639      let mut tkns = r.tokens();
1640      rhs_tkns.append(&mut tkns);
1641    }
1642    lhs_tkns.append(&mut rhs_tkns);
1643    lhs_tkns
1644  }
1645}
1646
1647#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1648#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1649pub enum Factor {
1650  Expression(Box<Expression>),
1651  Negate(Box<Factor>),
1652  Not(Box<Factor>),
1653  Parenthetical(Box<Factor>),
1654  Term(Box<Term>),
1655  Transpose(Box<Factor>),
1656}
1657
1658impl Factor {
1659  pub fn tokens(&self) -> Vec<Token> {
1660    match self {
1661      Factor::Expression(x) => x.tokens(),
1662      Factor::Negate(x) => x.tokens(),
1663      Factor::Not(x) => x.tokens(),
1664      Factor::Parenthetical(x) => x.tokens(),
1665      Factor::Term(x) => x.tokens(),
1666      Factor::Transpose(x) => x.tokens(),
1667    }
1668  }
1669}