1use std::cmp::Ordering;
2use crate::*;
3use std::fmt;
4use std::io::{Write, Cursor, Read};
5
6
7pub fn compress_and_encode<T: serde::Serialize>(tree: &T) -> Result<String, Box<dyn std::error::Error>> {
8 let serialized_code = bincode::serde::encode_to_vec(tree,bincode::config::standard())?;
9 let mut compressed = Vec::new();
10 brotli::CompressorWriter::new(&mut compressed, 9, 4096, 22)
11 .write(&serialized_code)?;
12 Ok(base64::encode(compressed))
13}
14
15pub fn decode_and_decompress<T: serde::de::DeserializeOwned>(encoded: &str) -> Result<T, Box<dyn std::error::Error>> {
16 let decoded = base64::decode(encoded)?;
17
18 let mut decompressed = Vec::new();
19 brotli::Decompressor::new(Cursor::new(decoded), 4096)
20 .read_to_end(&mut decompressed)?;
21
22 let (decoded,red) = bincode::serde::decode_from_slice(&decompressed,bincode::config::standard())?;
23
24 Ok(decoded)
25}
26
27#[derive(Clone, Copy, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)]
28pub struct SourceLocation {
29 pub row: usize,
30 pub col: usize,
31}
32
33impl PartialOrd for SourceLocation {
34 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
35 if self.row < other.row {
36 Some(Ordering::Less)
37 } else if self.row > other.row {
38 Some(Ordering::Greater)
39 } else {
40 self.col.partial_cmp(&other.col)
41 }
42 }
43}
44
45impl fmt::Debug for SourceLocation {
46 #[inline]
47 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48 write!(f, "{}:{}", self.row, self.col);
49 Ok(())
50 }
51}
52
53#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
54pub struct SourceRange {
55 pub start: SourceLocation,
56 pub end: SourceLocation,
57}
58
59impl Default for SourceRange {
63 fn default() -> Self {
64 SourceRange {
65 start: SourceLocation { row: 0, col: 0 },
66 end: SourceLocation { row: 0, col: 0 },
67 }
68 }
69}
70
71impl fmt::Debug for SourceRange {
72 #[inline]
73 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74 write!(f, "[{:?}, {:?})", self.start, self.end);
75 Ok(())
76 }
77}
78
79pub fn merge_src_range(r1: SourceRange, r2: SourceRange) -> SourceRange {
80 SourceRange {
81 start: r1.start.min(r2.start),
82 end: r2.end.max(r2.end),
83 }
84}
85
86#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
87pub enum TokenKind {
88 AbstractSigil, Alpha, Ampersand, Any, Apostrophe, AssignOperator, Asterisk, AsyncTransitionOperator, At,
89 Backslash, Bar, BoxDrawing,
90 Caret, CarriageReturn, CarriageReturnNewLine, Colon, CodeBlock, Comma,
91 Dash, DefineOperator, Digit, Dollar,
92 Emoji, EmphasisSigil, Empty, Equal, EquationSigil, Exclamation,
93 False, FloatLeft, FloatRight, FootnotePrefix,
94 Grave,
95 HashTag, HighlightSigil, HttpPrefix,
96 Identifier, ImgPrefix, InlineCode,
97 LeftAngle, LeftBrace, LeftBracket, LeftParenthesis,
98 Newline, Not, Number,
99 OutputOperator,
100 Percent, Period, Plus,
101 QuerySigil, Question, Quote, QuoteSigil,
102 RightAngle, RightBrace, RightBracket, RightParenthesis,
103 Semicolon, Space, Slash, String, StrikeSigil, StrongSigil,
104 Tab, Text, Tilde, Title, TransitionOperator, True,
105 UnderlineSigil, Underscore,
106}
107
108#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
109pub struct Token {
110 pub kind: TokenKind,
111 pub chars: Vec<char>,
112 pub src_range: SourceRange
113}
114
115impl fmt::Debug for Token {
116 #[inline]
117 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
118 write!(f, "{:?}:{:?}:{:?}", self.kind, String::from_iter(self.chars.iter().cloned()), self.src_range);
119 Ok(())
120 }
121}
122
123impl Default for Token {
124 fn default() -> Self {
125 Token{
126 kind: TokenKind::Empty,
127 chars: vec![],
128 src_range: SourceRange::default(),
129 }
130 }
131}
132
133impl Token {
134
135 pub fn new(kind: TokenKind, src_range: SourceRange, chars: Vec<char>) -> Token {
136 Token{kind, chars, src_range}
137 }
138
139 pub fn to_string(&self) -> String {
140 self.chars.iter().collect()
141 }
142
143 pub fn merge_tokens(tokens: &mut Vec<Token>) -> Option<Token> {
144 if tokens.len() == 0 {
145 None
146 } else if tokens.len() == 1 {
147 Some(tokens[0].clone())
148 } else {
149 let first = tokens[0].src_range.clone();
150 let kind = tokens[0].kind.clone();
151 let last = tokens.last().unwrap().src_range.clone();
152 let src_range = merge_src_range(first, last);
153 let chars: Vec<char> = tokens.iter_mut().fold(vec![],|mut m, ref mut t| {m.append(&mut t.chars.clone()); m});
154 let merged_token = Token{kind, chars, src_range};
155 Some(merged_token)
156 }
157 }
158}
159
160#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
161pub struct TableOfContents {
162 pub title: Option<Title>,
163 pub sections: Vec<Section>,
164}
165
166#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
167pub struct Program {
168 pub title: Option<Title>,
169 pub body: Body,
170}
171
172impl Program {
173 pub fn tokens(&self) -> Vec<Token> {
174 let body_tokens = self.body.tokens();
179 body_tokens
180 }
181
182 pub fn table_of_contents(&self) -> TableOfContents {
183 let mut sections = vec![];
184 for s in &self.body.sections {
185 sections.push(s.table_of_contents());
186 }
187 TableOfContents {
188 title: self.title.clone(),
189 sections,
190 }
191 }
192
193 pub fn pretty_print(&self) -> String {
194 let json_string = serde_json::to_string_pretty(self).unwrap();
195
196 let depth = |line: &str|->usize{line.chars().take_while(|&c| c == ' ').count()};
197 let mut result = String::new();
198 let lines: Vec<&str> = json_string.lines().collect();
199 result.push_str("Program\n");
200 for (index, line) in lines.iter().enumerate() {
201 let trm = line.trim();
202 if trm == "}" ||
203 trm == "},"||
204 trm == "{" ||
205 trm == "[" ||
206 trm == "],"||
207 trm == "]" {
208 continue;
209 }
210
211 let d = depth(line);
213 let mut prefix = String::new();
215 for _ in 0..d {
216 prefix.push_str(" ");
217 }
218 if index == lines.len() {
219 prefix.push_str("└ ");
220 } else {
221 if depth(lines[index + 1]) != d {
222 prefix.push_str("└ ");
223 } else {
224 prefix.push_str("├ ");
225 }
226 }
227 let trm = line.trim();
228 let trm = trm.trim_end_matches('{')
229 .trim_start_matches('"')
230 .trim_end_matches(':')
231 .trim_end_matches('"')
232 .trim_end_matches('[');
233 prefix.push_str(trm);
234
235 result.push_str(&prefix);
237 result.push('\n');
238 result = result.replace("\":", "");
239 }
240 let mut indexed_str = IndexedString::new(&result);
241 'rows: for i in 0..indexed_str.rows {
242 let rowz = &indexed_str.index_map[i];
243 for j in 0..rowz.len() {
244 let c = match indexed_str.get(i,j) {
245 Some(c) => c,
246 None => continue,
247 };
248 if c == '└' {
249 for k in i+1..indexed_str.rows {
250 match indexed_str.get(k,j) {
251 Some(c2) => {
252 if c2 == '└' {
253 indexed_str.set(i,j,'├');
254 for l in i+1..k {
255 match indexed_str.get(l,j) {
256 Some(' ') => {indexed_str.set(l,j,'│');},
257 Some('└') => {indexed_str.set(l,j,'├');},
258 _ => (),
259 }
260 }
261 } else if c2 == ' ' {
262 continue;
263 } else {
264 continue 'rows;
265 }
266 },
267 None => continue,
268 }
269
270 }
271 } else if c == ' ' || c == '│' {
272 continue;
273 } else {
274 continue 'rows;
275 }
276 }
277 }
278 indexed_str.to_string()
279 }
280
281}
282
283#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
284pub struct Title {
285 pub text: Token,
286}
287
288impl Title {
289
290 pub fn to_string(&self) -> String {
291 self.text.to_string()
292 }
293
294}
295
296#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
297pub struct Body {
298 pub sections: Vec<Section>,
299}
300
301impl Body {
302 pub fn tokens(&self) -> Vec<Token> {
303 let mut out = vec![];
304 for s in &self.sections {
305 let mut tkns = s.tokens();
306 out.append(&mut tkns);
307 }
308 out
309 }
310}
311
312#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
313pub enum ColumnAlignment {
314 Left,
315 Center,
316 Right,
317}
318
319#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
320pub struct MarkdownTable {
321 pub header: Vec<Paragraph>,
322 pub rows: Vec<Vec<Paragraph>>,
323 pub alignment: Vec<ColumnAlignment>,
324}
325
326#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
327pub struct Subtitle {
328 pub text: Paragraph,
329 pub level: u8,
330}
331
332impl Subtitle {
333 pub fn to_string(&self) -> String {
334 self.text.to_string()
335 }
336}
337
338#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
339pub struct Section {
340 pub subtitle: Option<Subtitle>,
341 pub elements: Vec<SectionElement>,
342}
343
344impl Section {
345
346 pub fn table_of_contents(&self) -> Section {
347 let elements: Vec<_> = self.elements.iter()
348 .filter(|e| matches!(e, SectionElement::Subtitle(_)))
349 .cloned()
350 .collect();
351 Section {
352 subtitle: self.subtitle.clone(),
353 elements,
354 }
355 }
356
357 pub fn tokens(&self) -> Vec<Token> {
358 let mut out = vec![];
359 for s in &self.elements {
360 let mut tkns = s.tokens();
361 out.append(&mut tkns);
362 }
363 out
364 }
365}
366
367#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
368pub struct Grammar {
369 pub rules: Vec<Rule>,
370}
371
372#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
373pub struct GrammarIdentifier {
374 pub name: Token,
375}
376
377impl GrammarIdentifier {
378 pub fn tokens(&self) -> Vec<Token> {
379 vec![self.name.clone()]
380 }
381
382 pub fn to_string(&self) -> String {
383 self.name.to_string()
384 }
385}
386
387#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
388pub struct Rule {
389 pub name: GrammarIdentifier,
390 pub expr: GrammarExpression,
391}
392
393#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
394pub enum GrammarExpression {
395 Choice(Vec<GrammarExpression>),
396 Definition(GrammarIdentifier),
397 Group(Box<GrammarExpression>),
398 List(Box<GrammarExpression>, Box<GrammarExpression>),
399 Not(Box<GrammarExpression>),
400 Optional(Box<GrammarExpression>),
401 Peek(Box<GrammarExpression>),
402 Repeat0(Box<GrammarExpression>),
403 Repeat1(Box<GrammarExpression>),
404 Range(Token,Token),
405 Sequence(Vec<GrammarExpression>),
406 Terminal(Token),
407}
408
409#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
410pub struct Citation {
411 pub id: Token,
412 pub text: Paragraph,
413}
414
415impl Citation {
416 pub fn to_string(&self) -> String {
417 format!("[{}]: {}", self.id.to_string(), self.text.to_string())
418 }
419}
420type BlockConfig = u64;
423
424pub type Footnote = (Token, Paragraph);
425
426#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
427pub enum FloatDirection {
428 Left,
429 Right,
430}
431
432#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
433pub enum SectionElement {
434 Abstract(Paragraph),
435 BlockQuote(Paragraph),
436 Citation(Citation),
437 CodeBlock(Token),
438 Comment(Comment),
439 Equation(Token),
440 FencedMechCode((Vec<(MechCode,Option<Comment>)>, BlockConfig)),
441 Float((Box<SectionElement>, FloatDirection)),
442 Footnote(Footnote),
443 Grammar(Grammar),
444 Image(Image),
445 List(MDList),
446 MechCode(Vec<(MechCode,Option<Comment>)>),
447 Paragraph(Paragraph),
448 Subtitle(Subtitle),
449 Table(MarkdownTable),
450 ThematicBreak,
451}
452
453impl SectionElement {
454 pub fn tokens(&self) -> Vec<Token> {
455 match self {
456 SectionElement::FencedMechCode((code,config)) => {
457 let mut tokens = vec![];
458 for (c,_) in code {
459 tokens.append(&mut c.tokens());
460 }
461 tokens
462 }
463 SectionElement::MechCode(codes) => {
464 let mut tokens = vec![];
465 for (code,_) in codes {
466 tokens.append(&mut code.tokens());
467 }
468 tokens
469 },
470 _ => todo!(),
471 }
472 }
473}
474
475#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
476pub struct Image {
477 pub src: Token,
478 pub caption: Option<Paragraph>,
479}
480
481impl Image {
482 pub fn to_string(&self) -> String {
483 let caption = match &self.caption {
484 Some(c) => c.to_string(),
485 None => "".to_string(),
486 };
487 format!("", caption, self.src.to_string())
488 }
489}
490
491pub type UnorderedList = Vec<((Option<Token>,Paragraph),Option<MDList>)>;
492pub type CheckList = Vec<((bool,Paragraph),Option<MDList>)>;
493
494#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
495pub struct OrderedList {
496 pub start: Number,
497 pub items: Vec<((Number,Paragraph),Option<MDList>)>,
498}
499
500#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
501pub enum MDList {
502 Unordered(UnorderedList),
503 Ordered(OrderedList),
504 Check(CheckList)
505}
506
507#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
508pub enum MechCode {
509 Comment(Comment),
510 Expression(Expression),
511 FsmImplementation(FsmImplementation),
512 FsmSpecification(FsmSpecification),
513 FunctionDefine(FunctionDefine),
514 Statement(Statement),
515}
516
517impl MechCode {
518 pub fn tokens(&self) -> Vec<Token> {
519 match self {
520 MechCode::Expression(x) => x.tokens(),
521 _ => todo!(),
522 }
527 }
528}
529
530#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
531pub struct FunctionDefine {
532 pub name: Identifier,
533 pub input: Vec<FunctionArgument>,
534 pub output: Vec<FunctionArgument>,
535 pub statements: Vec<Statement>,
536}
537
538#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
539pub struct FunctionArgument {
540 pub name: Identifier,
541 pub kind: KindAnnotation,
542}
543
544impl FunctionArgument {
545 pub fn tokens(&self) -> Vec<Token> {
546 let mut tokens = self.name.tokens();
547 tokens.append(&mut self.kind.tokens());
548 tokens
549 }
550}
551
552#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
553pub struct FsmImplementation {
554 pub name: Identifier,
555 pub input: Vec<Identifier>,
556 pub start: Pattern,
557 pub arms: Vec<FsmArm>,
558}
559
560#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
561pub enum FsmArm {
562 Guard(Pattern,Vec<Guard>),
563 Transition(Pattern,Vec<Transition>),
564}
565
566#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
567pub struct Guard {
568 pub condition: Pattern,
569 pub transitions: Vec<Transition>,
570}
571
572#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
573pub enum Transition {
574 Async(Pattern),
575 CodeBlock(Vec<(MechCode, Option<Comment>)>),
576 Next(Pattern),
577 Output(Pattern),
578 Statement(Statement),
579}
580
581#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
582pub enum Pattern {
583 Expression(Expression),
584 Formula(Factor),
585 TupleStruct(PatternTupleStruct),
586 Wildcard,
587}
588
589#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
590pub struct PatternTupleStruct {
591 pub name: Identifier,
592 pub patterns: Vec<Pattern>,
593}
594
595pub type PatternTuple = Vec<Pattern>;
596
597#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
598pub struct FsmSpecification {
599 pub name: Identifier,
600 pub input: Vec<Var>,
601 pub output: Option<KindAnnotation>,
602 pub states: Vec<StateDefinition>,
603}
604
605#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
606pub struct StateDefinition {
607 pub name: Identifier,
608 pub state_variables: Option<Vec<Var>>,
609}
610
611#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
612pub enum Statement {
613 EnumDefine(EnumDefine),
614 FsmDeclare(FsmDeclare),
615 KindDefine(KindDefine),
616 OpAssign(OpAssign),
617 VariableAssign(VariableAssign),
618 VariableDefine(VariableDefine),
619 SplitTable, FlattenTable, }
622
623#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
624pub struct FsmPipe {
625 pub start: FsmInstance,
626 pub transitions: Vec<Transition>
627}
628
629#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
630pub enum PipeElement {
631 Expression(Expression),
632 FsmInstance(FsmInstance),
633 Timer }
635
636#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
637pub struct FsmDeclare {
638 pub fsm: Fsm,
639 pub pipe: FsmPipe,
640}
641
642#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
643pub struct Fsm {
644 pub name: Identifier,
645 pub args: Option<ArgumentList>,
646 pub kind: Option<KindAnnotation>
647}
648
649pub type FsmArgs = Vec<(Option<Identifier>,Expression)>;
650
651#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
652pub struct FsmInstance {
653 pub name: Identifier,
654 pub args: Option<FsmArgs>,
655}
656
657#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
658pub struct EnumDefine {
659 pub name: Identifier,
660 pub variants: Vec<EnumVariant>,
661}
662
663#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
664pub struct EnumVariant {
665 pub name: Identifier,
666 pub value: Option<KindAnnotation>,
667}
668
669#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
670pub struct KindDefine {
671 pub name: Identifier,
672 pub kind: KindAnnotation,
673}
674
675#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
676pub struct Record {
677 pub bindings: Vec<Binding>,
678}
679
680#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
681pub enum Structure {
682 Empty,
683 Map(Map),
684 Matrix(Matrix),
685 Record(Record),
686 Set(Set),
687 Table(Table),
688 Tuple(Tuple),
689 TupleStruct(TupleStruct),
690}
691
692impl Structure {
693 pub fn tokens(&self) -> Vec<Token> {
694 match self {
695 Structure::Matrix(mat) => mat.tokens(),
696 _ => todo!(),
697 }
698 }
699}
700
701#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
702pub struct Map {
703 pub elements: Vec<Mapping>,
704}
705
706#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
707pub struct Mapping {
708 pub key: Expression,
709 pub value: Expression,
710}
711
712#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
713pub struct Set {
714 pub elements: Vec<Expression>,
715}
716
717#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
718pub struct Atom {
719 pub name: Identifier,
720}
721
722#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
723pub struct TupleStruct {
724 pub name: Identifier,
725 pub value: Box<Expression>,
726}
727
728#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
729pub struct Matrix {
730 pub rows: Vec<MatrixRow>,
731}
732
733impl Matrix {
734 pub fn tokens(&self) -> Vec<Token> {
735 let mut tkns = vec![];
736 for r in &self.rows {
737 let mut t = r.tokens();
738 tkns.append(&mut t);
739 }
740 tkns
741 }
742}
743
744pub type TableHeader = Vec<Field>;
745
746#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
747pub struct Table {
748 pub header: TableHeader,
749 pub rows: Vec<TableRow>,
750}
751
752#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
753pub struct Field {
754 pub name: Identifier,
755 pub kind: Option<KindAnnotation>,
756}
757
758#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
759pub struct TableColumn {
760 pub element: Expression,
761}
762
763#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
764pub struct MatrixColumn {
765 pub element: Expression,
766}
767
768impl MatrixColumn {
769 pub fn tokens(&self) -> Vec<Token> {
770 self.element.tokens()
771 }
772}
773
774#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
775pub struct TableRow {
776 pub columns: Vec<TableColumn>,
777}
778
779#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
780pub struct MatrixRow {
781 pub columns: Vec<MatrixColumn>,
782}
783
784impl MatrixRow {
785 pub fn tokens(&self) -> Vec<Token> {
786 let mut tkns = vec![];
787 for r in &self.columns {
788 let mut t = r.tokens();
789 tkns.append(&mut t);
790 }
791 tkns
792 }
793}
794
795#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
796pub struct VariableDefine {
797 pub mutable: bool,
798 pub var: Var,
799 pub expression: Expression,
800}
801
802#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
803pub struct Var {
804 pub name: Identifier,
805 pub kind: Option<KindAnnotation>,
806}
807
808impl Var {
809 pub fn tokens(&self) -> Vec<Token> {
810 let mut tkns = self.name.tokens();
811 if let Some(knd) = &self.kind {
812 let mut t = knd.tokens();
813 tkns.append(&mut t);
814 }
815 tkns
816 }
817}
818
819
820#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
821pub struct VariableAssign {
822 pub target: SliceRef,
823 pub expression: Expression,
824}
825
826#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
827pub struct Identifier {
828 pub name: Token,
829}
830
831impl Identifier {
832 pub fn tokens(&self) -> Vec<Token> {
833 vec![self.name.clone()]
834 }
835
836 pub fn to_string(&self) -> String {
837 self.name.to_string()
838 }
839
840}
841
842
843impl Identifier {
844 pub fn hash(&self) -> u64 {
845 hash_chars(&self.name.chars)
846 }
847}
848
849#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
850pub struct Emoji {
851 pub tokens: Vec<Token>,
852}
853
854#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
855pub struct Word {
856 pub tokens: Vec<Token>,
857}
858
859#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
860pub struct Slice {
861 pub name: Identifier,
862 pub subscript: Vec<Subscript>
863}
864
865#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
866pub struct SliceRef {
867 pub name: Identifier,
868 pub subscript: Option<Vec<Subscript>>
869}
870
871#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
872pub enum Subscript {
873 All, Brace(Vec<Subscript>), Bracket(Vec<Subscript>), Dot(Identifier), DotInt(RealNumber), Formula(Factor), Range(RangeExpression), Swizzle(Vec<Identifier>), }
882
883#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
884pub enum Expression {
885 Formula(Factor),
886 FunctionCall(FunctionCall),
887 FsmPipe(FsmPipe),
888 Literal(Literal),
889 Range(Box<RangeExpression>),
890 Slice(Slice),
891 Structure(Structure),
892 Var(Var),
893}
894
895impl Expression {
896 pub fn tokens(&self) -> Vec<Token> {
897 match self {
898 Expression::Var(v) => v.tokens(),
899 Expression::Literal(ltrl) => ltrl.tokens(),
900 Expression::Structure(strct) => strct.tokens(),
901 Expression::Formula(fctr) => fctr.tokens(),
902 _ => todo!(),
903 }
904 }
905}
906
907pub type ArgumentList = Vec<(Option<Identifier>,Expression)>;
908
909#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
910pub struct FunctionCall {
911 pub name: Identifier,
912 pub args: ArgumentList,
913}
914
915impl FunctionCall {
916 pub fn tokens(&self) -> Vec<Token> {
917 self.name.tokens()
918 }
919}
920
921#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
922pub struct Tuple {
923 pub elements: Vec<Expression>
924}
925
926#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
927pub struct Binding {
928 pub name: Identifier,
929 pub kind: Option<KindAnnotation>,
930 pub value: Expression,
931}
932
933#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
934pub struct KindAnnotation {
935 pub kind: Kind
936}
937
938impl KindAnnotation {
939
940 pub fn hash(&self) -> u64 {
941 match &self.kind {
942 Kind::Scalar(id) => id.hash(),
943 _ => todo!(),
944 }
945 }
946
947 pub fn tokens(&self) -> Vec<Token> {
948 self.kind.tokens()
949 }
950}
951
952#[derive(Clone, Debug, Hash, Serialize, Deserialize,Eq, PartialEq)]
953pub enum Kind {
954 Atom(Identifier),
955 Brace((Vec<Kind>,Vec<Literal>)),
956 Bracket((Vec<Kind>,Vec<Literal>)),
957 Empty,
958 Fsm(Vec<Kind>,Vec<Kind>),
959 Function(Vec<Kind>,Vec<Kind>),
960 Map(Box<Kind>,Box<Kind>),
961 Scalar(Identifier),
962 Tuple(Vec<Kind>),
963}
964
965impl Kind {
966 pub fn tokens(&self) -> Vec<Token> {
967 match self {
968 Kind::Tuple(x) => x.iter().flat_map(|k| k.tokens()).collect(),
969 Kind::Bracket((kinds, literals)) => {
970 kinds.iter().flat_map(|k| k.tokens())
971 .chain(literals.iter().flat_map(|l| l.tokens()))
972 .collect()
973 },
974 Kind::Brace((kinds, literals)) => {
975 kinds.iter().flat_map(|k| k.tokens())
976 .chain(literals.iter().flat_map(|l| l.tokens()))
977 .collect()
978 }
979 Kind::Map(x, y) => x.tokens().into_iter().chain(y.tokens()).collect(),
980 Kind::Scalar(x) => x.tokens(),
981 Kind::Atom(x) => x.tokens(),
982 Kind::Function(args, rets) => {
983 args.iter().flat_map(|k| k.tokens())
984 .chain(rets.iter().flat_map(|k| k.tokens()))
985 .collect()
986 }
987 Kind::Fsm(args, rets) => {
988 args.iter().flat_map(|k| k.tokens())
989 .chain(rets.iter().flat_map(|k| k.tokens()))
990 .collect()
991 }
992 Kind::Empty => vec![],
993 }
994 }
995}
996
997#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
998pub enum Literal {
999 Atom(Atom),
1000 Boolean(Token),
1001 Empty(Token),
1002 Number(Number),
1003 String(MechString),
1004 TypedLiteral((Box<Literal>,KindAnnotation))
1005}
1006
1007impl Literal {
1008 pub fn tokens(&self) -> Vec<Token> {
1009 match self {
1010 Literal::Atom(atm) => atm.name.tokens(),
1011 Literal::Boolean(tkn) => vec![tkn.clone()],
1012 Literal::Number(x) => x.tokens(),
1013 Literal::String(strng) => vec![strng.text.clone()],
1014 _ => todo!(),
1015 }
1016 }
1017}
1018
1019#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1020pub struct MechString {
1021 pub text: Token,
1022}
1023
1024pub type Hyperlink = (Token, Token);
1025
1026#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1027pub enum ParagraphElement {
1028 Emphasis(Box<ParagraphElement>),
1029 FootnoteReference(Token),
1030 Highlight(Box<ParagraphElement>),
1031 Hyperlink(Hyperlink),
1032 InlineCode(Token),
1033 EvalInlineMechCode(Expression),
1034 InlineMechCode(MechCode),
1035 InlineEquation(Token),
1036 Reference(Token),
1037 Strikethrough(Box<ParagraphElement>),
1038 Strong(Box<ParagraphElement>),
1039 Text(Token),
1040 Underline(Box<ParagraphElement>),
1041}
1042
1043impl ParagraphElement {
1044
1045 pub fn to_string(&self) -> String {
1046 match self {
1047 ParagraphElement::Emphasis(t) => t.to_string(),
1048 ParagraphElement::FootnoteReference(t) => t.to_string(),
1049 ParagraphElement::Highlight(t) => t.to_string(),
1050 ParagraphElement::Hyperlink((t, u)) => {
1051 format!("[{}]({})", t.to_string(), u.to_string())
1052 }
1053 ParagraphElement::InlineCode(t) => t.to_string(),
1054 ParagraphElement::InlineEquation(t) => t.to_string(),
1055 ParagraphElement::InlineMechCode(t) => format!("{:?}", t),
1056 ParagraphElement::EvalInlineMechCode(t) => format!("{:?}", t),
1057 ParagraphElement::Reference(t) => t.to_string(),
1058 ParagraphElement::Strikethrough(t) => t.to_string(),
1059 ParagraphElement::Strong(t) => t.to_string(),
1060 ParagraphElement::Text(t) => t.to_string(),
1061 ParagraphElement::Underline(t) => t.to_string(),
1062 }
1063 }
1064
1065}
1066
1067#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1068pub struct Paragraph {
1069 pub elements: Vec<ParagraphElement>,
1070}
1071
1072impl Paragraph {
1073 pub fn to_string(&self) -> String {
1074 let mut out = "".to_string();
1075 for e in &self.elements {
1076 out.push_str(&e.to_string());
1077 }
1078 out
1079 }
1080}
1081
1082pub type Sign = bool;
1083pub type Numerator = Token;
1084pub type Denominator = Token;
1085pub type Whole = Token;
1086pub type Part = Token;
1087pub type Real = Box<Number>;
1088pub type Imaginary = Box<Number>;
1089pub type Base = (Whole, Part);
1090pub type Exponent = (Sign, Whole, Part);
1091
1092#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
1093pub enum Number {
1094 Real(RealNumber),
1095 Imaginary(ComplexNumber),
1096}
1097
1098impl Number {
1099
1100 pub fn from_integer(x: i64) -> Number {
1101 Number::Real(RealNumber::Integer(Token::new(TokenKind::Digit, SourceRange::default(), x.to_string().chars().collect())))
1102 }
1103
1104 pub fn to_string(&self) -> String {
1105 match self {
1106 Number::Real(x) => x.to_string(),
1107 Number::Imaginary(x) => x.to_string(),
1108 }
1109 }
1110
1111 pub fn tokens(&self) -> Vec<Token> {
1112 match self {
1113 Number::Real(x) => x.tokens(),
1114 _ => todo!(),
1115 }
1116 }
1117}
1118
1119#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
1120pub enum RealNumber {
1121 Binary(Token),
1122 Decimal(Token),
1123 Float((Whole,Part)),
1124 Hexadecimal(Token),
1125 Integer(Token),
1126 Negated(Box<RealNumber>),
1127 Octal(Token),
1128 Rational((Numerator,Denominator)),
1129 Scientific((Base,Exponent)),
1130}
1131
1132impl RealNumber {
1133 pub fn tokens(&self) -> Vec<Token> {
1134 match self {
1135 RealNumber::Integer(tkn) => vec![tkn.clone()],
1136 _ => todo!(),
1137 }
1138 }
1139 pub fn to_string(&self) -> String {
1140 match self {
1141 RealNumber::Integer(tkn) => tkn.to_string(),
1142 RealNumber::Float((whole, part)) => format!("{}.{}", whole.to_string(), part.to_string()),
1143 RealNumber::Binary(tkn) => format!("0b{}", tkn.to_string()),
1144 RealNumber::Hexadecimal(tkn) => format!("0x{}", tkn.to_string()),
1145 RealNumber::Octal(tkn) => format!("0o{}", tkn.to_string()),
1146 RealNumber::Decimal(tkn) => format!("0d{}", tkn.to_string()),
1147 RealNumber::Rational((num, den)) => format!("{}/{}", num.to_string(), den.to_string()),
1148 RealNumber::Scientific(((whole,part), exponent)) => {
1149 let (sign, whole, part) = exponent;
1150 let sign_str = if *sign { "+" } else { "-" };
1151 let whole_str = whole.to_string();
1152 let part_str = part.to_string();
1153 format!("{}{}.{}/10^{}", whole.to_string(), part.to_string(), sign_str, whole_str)
1154 }
1155 RealNumber::Negated(x) => format!("-{}", x.to_string()),
1156 }
1157 }
1158}
1159
1160#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
1161pub struct ImaginaryNumber {
1162 pub number: RealNumber,
1163}
1164
1165#[derive(Clone, Debug, Hash, Serialize, Deserialize, Eq, PartialEq)]
1166pub struct ComplexNumber {
1167 pub real: Option<RealNumber>,
1168 pub imaginary: ImaginaryNumber
1169}
1170
1171impl ComplexNumber {
1172 pub fn tokens(&self) -> Vec<Token> {
1173 let mut tkns = vec![];
1174 if let Some(r) = &self.real {
1175 tkns.append(&mut r.tokens());
1176 }
1177 tkns.append(&mut self.imaginary.number.tokens());
1178 tkns
1179 }
1180
1181 pub fn to_string(&self) -> String {
1182 let mut out = "".to_string();
1183 if let Some(r) = &self.real {
1184 out.push_str(&r.to_string());
1185 }
1186 out.push_str("i");
1187 out
1188 }
1189}
1190
1191#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1192pub struct Comment {
1193 pub paragraph: Paragraph,
1194}
1195
1196#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1197pub struct OpAssign {
1198 pub target: SliceRef,
1199 pub op: OpAssignOp,
1200 pub expression: Expression,
1201}
1202
1203#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1204pub enum OpAssignOp {
1205 Add,
1206 Div,
1207 Exp,
1208 Mod,
1209 Mul,
1210 Sub,
1211}
1212
1213#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1214pub enum RangeOp {
1215 Exclusive,
1216 Inclusive,
1217}
1218
1219#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1220pub enum AddSubOp {
1221 Add,
1222 Sub,
1223}
1224
1225#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1226pub enum MulDivOp {
1227 Div,
1228 Mod,
1229 Mul,
1230}
1231
1232#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1233pub enum VecOp {
1234 Cross,
1235 Dot,
1236 MatMul,
1237 Solve,
1238}
1239
1240#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1241pub enum ExponentOp {
1242 Exp
1243}
1244
1245#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1246pub enum ComparisonOp {
1247 Equal,
1248 GreaterThan,
1249 GreaterThanEqual,
1250 LessThan,
1251 LessThanEqual,
1252 NotEqual,
1253}
1254
1255#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1256pub enum LogicOp {
1257 And,
1258 Not,
1259 Or,
1260 Xor,
1261}
1262
1263#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1264pub enum FormulaOperator {
1265 AddSub(AddSubOp),
1266 Comparison(ComparisonOp),
1267 Exponent(ExponentOp),
1268 Logic(LogicOp),
1269 MulDiv(MulDivOp),
1270 Vec(VecOp),
1271}
1272
1273#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1274pub struct RangeExpression {
1275 pub start: Factor,
1276 pub increment: Option<(RangeOp,Factor)>,
1277 pub operator: RangeOp,
1278 pub terminal: Factor,
1279}
1280
1281impl RangeExpression {
1282 pub fn tokens(&self) -> Vec<Token> {
1283 let mut tokens = self.start.tokens();
1284 tokens.append(&mut self.terminal.tokens());
1285 tokens
1286 }
1287}
1288
1289#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1290pub struct Term {
1291 pub lhs: Factor,
1292 pub rhs: Vec<(FormulaOperator,Factor)>
1293}
1294
1295impl Term {
1296 pub fn tokens(&self) -> Vec<Token> {
1297 let mut lhs_tkns = self.lhs.tokens();
1298 let mut rhs_tkns = vec![];
1299 for (op, r) in &self.rhs {
1300 let mut tkns = r.tokens();
1301 rhs_tkns.append(&mut tkns);
1302 }
1303 lhs_tkns.append(&mut rhs_tkns);
1304 lhs_tkns
1305 }
1306}
1307
1308#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
1309pub enum Factor {
1310 Expression(Box<Expression>),
1311 Negate(Box<Factor>),
1312 Not(Box<Factor>),
1313 Parenthetical(Box<Factor>),
1314 Term(Box<Term>),
1315 Transpose(Box<Factor>),
1316}
1317
1318impl Factor {
1319 pub fn tokens(&self) -> Vec<Token> {
1320 match self {
1321 Factor::Expression(x) => x.tokens(),
1322 Factor::Negate(x) => x.tokens(),
1323 Factor::Not(x) => x.tokens(),
1324 Factor::Parenthetical(x) => x.tokens(),
1325 Factor::Term(x) => x.tokens(),
1326 Factor::Transpose(x) => x.tokens(),
1327 }
1328 }
1329}