1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::borrow::Cow;
use std::collections::{HashSet, VecDeque, LinkedList};
use std::iter::FromIterator;
pub static INLINE_MATH_TAG: &'static str = "[inline-math]";
#[derive(Debug, Clone)]
pub enum LayoutKind {
Block,
Inline,
}
pub type Atom<'a> = Cow<'a, str>;
#[derive(Debug, Clone, Hash, Eq, Default)]
pub struct Text<'a>(pub Cow<'a, str>);
impl<'a> Text<'a> {
pub fn new(value: &'a str) -> Self {
Text(Cow::Borrowed(value))
}
pub fn from_string(value: String) -> Self {
Text(Cow::Owned(value))
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn append(self, other: Text<'a>) -> Self {
Text(self.0 + other.0)
}
}
impl<'a> std::fmt::Display for Text<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl<'a> PartialEq for Text<'a> {
fn eq(&self, other: &Self) -> bool {
let left = &*self.0;
let right = &*other.0;
left == right
}
}
impl<'a> PartialEq<str> for Text<'a> {
fn eq(&self, other: &str) -> bool {
let left = &*self.0;
let right = other;
left == right
}
}
pub static TOKEN_SET: &'static [&'static str] = &["\\", "[", "]", "{", "}", "(", ")", "=>", "_", "^"];
fn get_end_kind_for(begin_kind: &str) -> &str {
match begin_kind {
"{" => "}",
"[" => "]",
"(" => ")",
_ => unreachable!()
}
}
fn get_begin_kind_for(end_kind: &str) -> &str {
match end_kind {
"}" => "{",
"]" => "[",
")" => "(",
_ => unreachable!()
}
}
pub fn is_token<'a>(value: &'a str) -> bool {
for tk in TOKEN_SET {
if *tk == value {
return true;
}
}
false
}
#[derive(Debug, Clone, PartialEq)]
pub struct RewriteRule<T> {
pub from: T,
pub to: T,
}
#[derive(Debug, Clone)]
pub struct CurlyBrace<T>(pub Vec<T>);
#[derive(Debug, Clone)]
pub struct SquareParen<T>(pub Vec<T>);
#[derive(Debug, Clone, PartialEq)]
pub enum EnclosureKind<'a> {
CurlyBrace,
SquareParen,
Parens,
Fragment,
Error {
open: &'a str,
close: &'a str,
},
}
impl<'a> EnclosureKind<'a> {
pub fn new(open: &'a str, close: &'a str) -> EnclosureKind<'a> {
match (open, close) {
("{", "}") => EnclosureKind::CurlyBrace,
("[", "]") => EnclosureKind::SquareParen,
("(", ")") => EnclosureKind::Parens,
(open, close) => EnclosureKind::Error {open, close},
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Enclosure<'a, T> {
pub kind: EnclosureKind<'a>,
pub children: Vec<T>,
}
impl<'a, T> Enclosure<'a, T> {
pub fn is_curly_brace(&self) -> bool {
match self.kind {
EnclosureKind::CurlyBrace => true,
_ => false,
}
}
pub fn is_square_parens(&self) -> bool {
match self.kind {
EnclosureKind::SquareParen => true,
_ => false,
}
}
pub fn is_parens(&self) -> bool {
match self.kind {
EnclosureKind::Parens => true,
_ => false,
}
}
pub fn is_error(&self) -> bool {
match self.kind {
EnclosureKind::Error{..} => true,
_ => false,
}
}
}
impl<'a> Enclosure<'a, crate::backend::Ast<'a>> {
pub fn new_curly_brace(children: Vec<crate::backend::Ast<'a>>) -> Self {
Enclosure {
kind: EnclosureKind::CurlyBrace,
children
}
}
}