-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.rs
138 lines (123 loc) · 3.69 KB
/
lexer.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
use std::str::from_utf8;
use crate::lazy_reader::LazyReader;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Token {
String(String),
OpenNode(String),
Quote(String),
CloseNodeNamed(String),
CloseNode,
EndOfOpenNode,
Eq,
EOF,
}
pub struct Lexer {
buf: Vec<u8>,
cursor: usize,
reader: LazyReader,
}
impl Lexer {
pub fn new(reader: LazyReader) -> Self {
Self { reader, buf: vec![], cursor: 0 }
}
pub fn parse(&mut self) -> Vec<Token> {
let mut tokens = Vec::new();
while let Some(ch) = self.peak() {
match ch {
b' ' | b'\n' | b'\t' => {
self.consume();
}
b'<' => {
self.consume();
if let Some(next) = self.peak() {
if next == b'/' {
self.consume();
tokens.push(Token::CloseNodeNamed(self.consume_str()));
self.consume();
} else {
tokens.push(Token::OpenNode(self.consume_str()));
}
}
}
b'/' => {
self.consume();
if let Some(next) = self.peak() {
if next == b'>' {
tokens.push(Token::CloseNode);
self.consume();
} else {
panic!("invalid syntax")
}
}
}
b'>' => {
tokens.push(Token::EndOfOpenNode);
self.consume();
}
b'=' => {
tokens.push(Token::Eq);
self.consume();
}
b'"' => {
tokens.push(Token::Quote(self.consume_quote()));
}
c if c.is_ascii_alphanumeric() => {
tokens.push(Token::String(self.consume_str()));
}
_ => {
println!("Unknown: {:?}", from_utf8(&[ch]));
self.consume();
}
}
}
tokens.push(Token::EOF);
tokens
}
fn consume_str(&mut self) -> String {
let mut tmp_str = Vec::new();
while let Some(ch) = self.peak() {
if ch.is_ascii_alphanumeric() {
tmp_str.push(ch);
self.consume();
} else { break; }
}
String::from_utf8(tmp_str).expect("must be a valid string")
}
fn consume_quote(&mut self) -> String {
self.consume();
let mut tmp_str = Vec::new();
while let Some(ch) = self.peak() {
if ch == b'"' {
self.consume();
break;
}
tmp_str.push(ch);
self.consume();
}
String::from_utf8(tmp_str).expect("must be a valid string")
}
fn peak(&mut self) -> Option<u8> {
if self.buf.len() == 0 {
self.buf = self.reader.next_chunk()?;
}
if self.cursor >= self.buf.len() {
self.buf = self.reader.next_chunk()?;
self.cursor = 0;
return match self.buf.get(0) {
None => None,
Some(ch) => { Some(*ch) }
};
}
match self.buf.get(self.cursor) {
None => None,
Some(ch) => { Some(*ch) }
}
}
fn consume(&mut self) -> Option<u8> {
if let Some(ch) = self.peak() {
self.cursor += 1;
Some(ch);
}
None
}
}