This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokens.rs
168 lines (156 loc) · 4.61 KB
/
tokens.rs
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
// This file is part of "jup"
// Under the MIT License
// Copyright (c) Junon, Antonin Hérault
use std::string::ToString;
/// All tokens list for the Junon programming
#[allow(unused)] // for debug
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Token {
Assembly,
Assign,
BracketOpen,
BracketClose,
Comma,
Comment,
ConditionElse,
ConditionIf,
Divide,
Equal,
Function,
LessThan,
LessThanOrEqual,
Loop,
LoopBreak,
LoopContinue,
Minus,
MoreThan,
MoreThanOrEqual,
Multiply,
ParenOpen,
ParenClose,
Point,
Plus,
Return,
SemiColon,
SquareBracketOpen,
SquareBracketClose,
Static,
StringDot,
TypeDef,
Variable,
/// Example : "foo" or something like that is not a real token
/// Note : It can be a string, an value like an integer or a float
Other(String),
// Avoid using a 2D table and permits to do not respect line breaks
NewLine,
/// Avoid `Option` usage
None,
}
impl ToString for Token {
/// A void string is returned when it cannot be converted to `String`
fn to_string(&self) -> String {
match *self {
Self::Assembly => "@",
Self::Assign => "=",
Self::BracketOpen => "{",
Self::BracketClose => "}",
Self::Comma => ",",
Self::Comment => "//",
Self::ConditionElse => "else",
Self::ConditionIf => "if",
Self::Divide => "/",
Self::Equal => "==",
Self::Function => "fun",
Self::LessThan => "<",
Self::LessThanOrEqual => "<=",
Self::Loop => "loop",
Self::LoopBreak => "break",
Self::LoopContinue => "continue",
Self::Minus => "-",
Self::MoreThan => ">",
Self::MoreThanOrEqual => ">=",
Self::Multiply => "*",
Self::ParenOpen => "(",
Self::ParenClose => ")",
Self::Point => ".",
Self::Plus => "+",
Self::Return => "ret",
Self::SemiColon => ";",
Self::SquareBracketOpen => "[",
Self::SquareBracketClose => "]",
Self::Static => "static",
Self::StringDot => "'",
Self::TypeDef => ":",
Self::Variable => "let",
Self::Other(ref string) => &*string,
Self::NewLine => "\n",
Self::None => "",
}
.to_string()
}
}
impl Token {
/// Convert a string into a `Token` object \
/// If the string does not correspond to any token, it will return a
/// `Token::Other` object with contained string into
pub fn token_from_str(string: &str) -> Self {
match string {
"@" => Self::Assembly,
"=" => Self::Assign,
"{" => Self::BracketOpen,
"}" => Self::BracketClose,
"," => Self::Comma,
"//" => Self::Comment,
"else" => Self::ConditionElse,
"if" => Self::ConditionIf,
"/" => Self::Divide,
"==" => Self::Equal,
"fun" => Self::Function,
"<" => Self::LessThan,
"<=" => Self::LessThanOrEqual,
"loop" => Self::Loop,
"break" => Self::LoopBreak,
"continue" => Self::LoopContinue,
"-" => Self::Minus,
">" => Self::MoreThan,
">=" => Self::MoreThanOrEqual,
"*" => Self::Multiply,
"(" => Self::ParenOpen,
")" => Self::ParenClose,
"." => Self::Point,
"+" => Self::Plus,
"ret" => Self::Return,
";" => Self::SemiColon,
"[" => Self::SquareBracketOpen,
"]" => Self::SquareBracketClose,
"static" => Self::Static,
"'" => Self::StringDot,
":" => Self::TypeDef,
"let" => Self::Variable,
"\n" => Self::NewLine,
_ => Self::Other(string.to_string()),
}
}
/// The string as `&String` is converted into a `&str` before returning
/// a `::from_str()` call
pub fn from_string(string: &str) -> Self {
Self::token_from_str(string)
}
}
// Don't forget to add "-- --nocapture" flags to the command line arguments
// when you execute `cargo test`
#[test]
fn convert_to_string() {
let token_function = Token::Function;
println!("{}", token_function.to_string());
}
#[test]
fn from_string() {
let string = String::from("fun");
println!("{:?}", Token::from_string(&string));
}
#[test]
fn from_str() {
let string: &str = "fun";
println!("{:?}", Token::token_from_str(string));
}