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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use std::iter;
use std::str;
#[derive(Debug, PartialEq)]
pub struct ParseError {
pub line_number: usize,
pub message: String,
}
#[inline]
fn is_whitespace_except_newline(c: u8) -> bool {
c == b' ' || c == b'\t' || c == b'\r'
}
#[inline]
fn is_whitespace(c: u8) -> bool {
is_whitespace_except_newline(c) || c == b'\n'
}
#[derive(Clone)]
pub struct Lexer<'a> {
bytes: iter::Peekable<str::Bytes<'a>>,
current_line_number: usize,
}
impl<'a> Lexer<'a> {
pub fn new(input: &'a str) -> Lexer<'a> {
Lexer {
bytes: input.bytes().peekable(),
current_line_number: 1,
}
}
fn advance(&mut self) {
match self.bytes.next() {
Some(c) if c == b'\n' => {
self.current_line_number += 1;
}
_ => {},
}
}
fn peek(&mut self) -> Option<u8> {
self.bytes.peek().map(|&x| x)
}
fn skip_while<F: Fn(u8) -> bool>(&mut self, is_true: F) -> bool {
let mut was_anything_skipped = false;
loop {
match self.peek() {
None => break,
Some(c) if !is_true(c) => break,
_ => {
self.advance();
was_anything_skipped = true;
}
}
}
debug_assert!(self.peek().map(|c| !is_true(c)).unwrap_or(true));
was_anything_skipped
}
fn skip_unless<F: Fn(u8) -> bool>(&mut self, is_false: F) -> bool {
self.skip_while(|c| !is_false(c))
}
fn skip_comment(&mut self) -> bool {
match self.peek() {
None => false,
Some(c) => {
if c == b'#' {
self.skip_unless(|c| c == b'\n');
true
} else {
false
}
}
}
}
fn skip_whitespace_except_newline(&mut self) -> bool {
self.skip_while(is_whitespace_except_newline)
}
fn next_word(&mut self) -> Option<Vec<u8>> {
let mut ret: Vec<u8> = Vec::new();
self.skip_whitespace_except_newline();
loop {
match self.peek() {
None => break,
Some(c) => {
if c == b'#' {
assert!(self.skip_comment());
self.skip_whitespace_except_newline();
} else if is_whitespace(c) {
if c == b'\n' && ret.is_empty() {
ret.push(c);
self.advance();
}
break;
} else {
ret.push(c);
self.advance();
}
}
}
}
if ret.is_empty() {
debug_assert_eq!(self.peek(), None);
None
} else {
Some(ret)
}
}
}
impl<'a> Iterator for Lexer<'a> {
type Item = String;
fn next(&mut self) -> Option<String> {
self.next_word().map(|buf| {
match String::from_utf8(buf) {
Ok(s) => s,
Err(_) => panic!("Lex error: Invalid utf8 on line {}.", self.current_line_number),
}
})
}
fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
}
#[test]
fn test_next_word() {
let mut l = Lexer::new("hello wor\rld\n this# is\r\na \t test\n");
assert_eq!(l.next_word(), Some(b"hello".to_vec()));
assert_eq!(l.current_line_number, 1);
assert_eq!(l.next_word(), Some(b"wor".to_vec()));
assert_eq!(l.current_line_number, 1);
assert_eq!(l.next_word(), Some(b"ld".to_vec()));
assert_eq!(l.current_line_number, 1);
assert_eq!(l.next_word(), Some(b"\n".to_vec()));
assert_eq!(l.current_line_number, 2);
assert_eq!(l.next_word(), Some(b"this".to_vec()));
assert_eq!(l.current_line_number, 2);
assert_eq!(l.next_word(), Some(b"\n".to_vec()));
assert_eq!(l.current_line_number, 3);
assert_eq!(l.next_word(), Some(b"a".to_vec()));
assert_eq!(l.current_line_number, 3);
assert_eq!(l.next_word(), Some(b"test".to_vec()));
assert_eq!(l.current_line_number, 3);
assert_eq!(l.next_word(), Some(b"\n".to_vec()));
assert_eq!(l.current_line_number, 4);
assert_eq!(l.next_word(), None);
}