-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.rs
107 lines (100 loc) · 1.54 KB
/
token.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
use std::fmt::{Debug, Formatter, Result};
use TokenKind::*;
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum TokenKind {
Eof,
Literal,
Number,
Str,
Char,
Float,
Add,
Sub,
Mul,
Div,
Sur,
RArrow,
LArrow,
Dot,
Comma,
Colon,
Equal,
Semicolon,
Greater,
Less,
GrEq,
LeEq,
Addr,
Or,
Bang,
BangEq,
EqEq,
LBrace,
RBrace,
LParen,
RParen,
LBracket,
RBracket,
Underline,
Def,
Ret,
Aop,
For,
If,
Ef,
Nf,
Out,
Go,
New,
Use,
Nil,
}
type KwMap = (&'static str, TokenKind);
type KwType = &'static [KwMap];
pub const KEYWORDS: KwType = &[
("def", Def),
("ret", Ret),
("aop", Aop),
("for", For),
("if", If),
("ef", Ef),
("nf", Nf),
("out", Out),
("go", Go),
("new", New),
("use", Use),
("nil", Nil),
];
pub fn to_kw(lit: &String) -> TokenKind {
for v in KEYWORDS {
if lit == v.0 {
return v.1;
}
}
Literal
}
pub struct Token {
lit: String,
line: u32,
offset: i8,
kind: TokenKind,
}
impl Token {
pub fn new(lit: String, line: u32, offset: i8, kind: TokenKind) -> Self {
Token {
lit,
line,
offset,
kind,
}
}
}
impl Debug for Token {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(
f,
"{{ {}:{} {:?} {} }}",
self.line, self.offset, self.kind, self.lit
)
}
}