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
use std::fmt;
#[derive(Debug, Clone)]
pub struct TokenRef<'a> {
pub value: TokenValueRef<'a>,
pub line: usize,
}
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum TokenValueRef<'a> {
Text(&'a str),
BlockStart,
VarStart,
BlockEnd,
VarEnd,
Name(&'a str),
Value(ConstRef<'a>),
Operator(&'a str),
Punctuation(char),
InterpolationStart,
InterpolationEnd,
CommentStart,
}
impl<'a> Into<TokenValue> for TokenValueRef<'a> {
fn into(self) -> TokenValue {
match self {
TokenValueRef::Text(t) => TokenValue::Text(t.into()),
TokenValueRef::BlockStart => TokenValue::BlockStart,
TokenValueRef::VarStart => TokenValue::VarStart,
TokenValueRef::BlockEnd => TokenValue::BlockEnd,
TokenValueRef::VarEnd => TokenValue::VarEnd,
TokenValueRef::Name(n) => TokenValue::Name(n.into()),
TokenValueRef::Value(v) => TokenValue::Value(v.into()),
TokenValueRef::Operator(s) => TokenValue::Operator(s.into()),
TokenValueRef::Punctuation(s) => TokenValue::Punctuation(s.into()),
TokenValueRef::InterpolationStart => TokenValue::InterpolationStart,
TokenValueRef::InterpolationEnd => TokenValue::InterpolationEnd,
TokenValueRef::CommentStart => TokenValue::CommentStart,
}
}
}
#[derive(PartialEq, Debug, Clone)]
pub enum TokenValue {
Text(String),
BlockStart,
VarStart,
BlockEnd,
VarEnd,
Name(String),
Value(Const),
Operator(String),
Punctuation(char),
InterpolationStart,
InterpolationEnd,
CommentStart,
}
impl TokenValue {
pub fn get_english(&self) -> (&'static str, Option<String>) {
match *self {
TokenValue::Text(ref v) => ("text", Some(v.to_string())),
TokenValue::BlockStart => ("begin of statement block", None),
TokenValue::VarStart => ("begin of print statement", None),
TokenValue::BlockEnd => ("end of statement block", None),
TokenValue::VarEnd => ("end of print statement", None),
TokenValue::Name(ref n) => ("name", Some(n.to_string())),
TokenValue::Value(Const::Num(ref n)) => ("number", Some(n.to_string())),
TokenValue::Value(Const::Str(ref s)) => ("string", Some(s.to_string())),
TokenValue::Operator(ref s) => ("operator", Some(s.to_string())),
TokenValue::Punctuation(s) => ("punctuation", Some(s.to_string())),
TokenValue::InterpolationStart => ("begin of string interpolation", None),
TokenValue::InterpolationEnd => ("end of string interpolation", None),
TokenValue::CommentStart => ("comment start", None),
}
}
}
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum ConstRef<'a> {
Num(ConstNumberRef<'a>),
Str(&'a str),
}
impl<'a> ConstRef<'a> {
pub fn new_big_num<'c>(num: &'c str) -> ConstRef<'c> {
ConstRef::Num(ConstNumberRef::Big(num))
}
pub fn new_float<'c>(num: f64) -> ConstRef<'c> {
ConstRef::Num(ConstNumberRef::Float(num))
}
pub fn new_int<'c>(num: i64) -> ConstRef<'c> {
ConstRef::Num(ConstNumberRef::Int(num))
}
pub fn new_str<'c>(s: &'c str) -> ConstRef<'c> {
ConstRef::Str(s)
}
}
impl<'a> Into<Const> for ConstRef<'a> {
fn into(self) -> Const {
match self {
ConstRef::Num(n) => Const::Num(n.into()),
ConstRef::Str(s) => Const::Str(s.into()),
}
}
}
#[derive(PartialEq, Debug, Clone)]
pub enum Const {
Num(ConstNumber),
Str(String),
}
impl fmt::Display for Const {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Const::Num(ref n) => write!(f, "{}", n),
Const::Str(ref s) => write!(f, "{}", s),
}
}
}
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum ConstNumberRef<'a> {
Big(&'a str),
Float(f64),
Int(i64),
}
#[derive(PartialEq, Debug, Clone)]
pub enum ConstNumber {
Big(String),
Float(f64),
Int(i64),
}
impl fmt::Display for ConstNumber {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ConstNumber::Big(ref n) => write!(f, "{}", n),
ConstNumber::Float(v) => write!(f, "{}", v),
ConstNumber::Int(i) => write!(f, "{}", i),
}
}
}
impl<'a> Into<ConstNumber> for ConstNumberRef<'a> {
fn into(self) -> ConstNumber {
match self {
ConstNumberRef::Big(n) => ConstNumber::Big(n.to_string()),
ConstNumberRef::Float(v) => ConstNumber::Float(v),
ConstNumberRef::Int(v) => ConstNumber::Int(v),
}
}
}