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