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
use std::fmt::{Debug, Display, Formatter, Result};
use crate::syntax::ast::keyword::Keyword;
use crate::syntax::ast::pos::Position;
use crate::syntax::ast::punc::Punctuator;
#[derive(Clone, PartialEq)]
#[derive(Debug)]
pub struct Token {
pub data: TokenData,
pub pos: Position,
}
impl Token {
pub fn new(data: TokenData, line_number: u64, column_number: u64) -> Token {
Token {
data: data,
pos: Position::new(line_number, column_number),
}
}
}
impl Display for Token {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{}", self.data)
}
}
pub struct VecToken(Vec<Token>);
impl Debug for VecToken {
fn fmt(&self, f: &mut Formatter) -> Result {
let mut buffer = String::new();
for token in &self.0 {
buffer.push_str(&token.to_string());
}
write!(f, "{}", buffer)
}
}
#[derive(Clone, PartialEq, Debug)]
pub enum TokenData {
BooleanLiteral(bool),
EOF,
Identifier(String),
Keyword(Keyword),
NullLiteral,
NumericLiteral(f64),
Punctuator(Punctuator),
StringLiteral(String),
RegularExpression(String),
Comment(String),
}
impl Display for TokenData {
fn fmt(&self, f: &mut Formatter) -> Result {
match self.clone() {
TokenData::BooleanLiteral(val) => write!(f, "{}", val),
TokenData::EOF => write!(f, "end of file"),
TokenData::Identifier(ident) => write!(f, "{}", ident),
TokenData::Keyword(word) => write!(f, "{}", word),
TokenData::NullLiteral => write!(f, "null"),
TokenData::NumericLiteral(num) => write!(f, "{}", num),
TokenData::Punctuator(punc) => write!(f, "{}", punc),
TokenData::StringLiteral(lit) => write!(f, "{}", lit),
TokenData::RegularExpression(reg) => write!(f, "{}", reg),
TokenData::Comment(comm) => write!(f, "/*{}*/", comm),
}
}
}