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
|
use crate::*;
use std::path::PathBuf;
pub struct Tokeniser {
/// Characters waiting to be parsed, in reverse order.
pub chars: Vec<char>,
/// Path of the whole source file.
pub source_path: Option<PathBuf>,
/// Original path of the embedded source file.
pub embedded_path: Option<PathBuf>,
/// Line where the embedded source file begins.
pub embedded_first_line: usize,
/// Position of the next character to be consumed.
pub position: Position,
/// Position of the most recently consumed character.
pub prev_position: Position,
/// Position of the first character of the current token.
pub start_position: Position,
/// The source characters consumed for the current token.
pub consumed: String,
/// List of characters that start a new token.
pub delimiters: Vec<char>,
/// List of characters that terminate a token.
pub terminators: Vec<char>,
}
impl Tokeniser {
pub fn new<P: Into<PathBuf>>(source_code: &str,path: Option<P>) -> Self {
Self {
chars: source_code.chars().rev().collect(),
source_path: path.map(|p| p.into()),
embedded_path: None,
embedded_first_line: 0,
position: Position::ZERO,
prev_position: Position::ZERO,
start_position: Position::ZERO,
consumed: String::new(),
delimiters: Vec::new(),
terminators: Vec::new(),
}
}
pub fn add_delimiters(&mut self, delimiters: &[char]) {
self.delimiters.extend_from_slice(delimiters);
}
pub fn add_terminators(&mut self, terminators: &[char]) {
self.terminators.extend_from_slice(terminators);
}
/// Return the next character without consuming it.
pub fn peek_char(&self) -> Option<char> {
self.chars.last().copied()
}
/// Consume and return the next character.
pub fn eat_char(&mut self) -> Option<char> {
let option = self.chars.pop();
if let Some(c) = option {
self.prev_position = self.position;
self.position.advance(c);
self.consumed.push(c);
}
return option;
}
/// Remove the next character.
pub fn drop_char(&mut self) {
if let Some(c) = self.chars.pop() {
self.prev_position = self.position;
self.position.advance(c);
}
}
/// Remove whitespace.
pub fn drop_whitespace(&mut self) {
while let Some(c) = self.peek_char() {
match c.is_whitespace() {
true => self.drop_char(),
false => break,
}
}
}
/// Remove a full token from the queue.
pub fn eat_token(&mut self) -> String {
let mut token = String::new();
while let Some(peek) = self.peek_char() {
if peek.is_whitespace() || self.delimiters.contains(&peek) {
break;
}
let c = self.eat_char().unwrap();
token.push(c);
if self.terminators.contains(&c) {
break;
}
}
token
}
/// Consume and return all characters up to and including the delimiter.
/// Returns None if end of source is reached before delimiter is found.
pub fn eat_to_delimiter(&mut self, delim: char) -> Option<String> {
let mut token = String::new();
while let Some(c) = self.eat_char() {
self.consumed.push(c);
match c == delim {
true => return Some(token),
false => token.push(c),
}
}
return None;
}
/// Returns true if the remainder of the line is whitespace.
pub fn end_of_line(&self) -> bool {
for c in self.chars.iter().rev() {
if *c == '\n' {
return true;
}
if !c.is_whitespace() {
return false
}
}
return true;
}
pub fn mark_start_position(&mut self) {
self.start_position = self.position;
}
pub fn get_source_span(&mut self) -> SourceSpan {
let in_merged = SourceLocation {
path: self.source_path.to_owned(),
start: self.start_position,
end: self.prev_position,
};
let in_source = if self.start_position.line >= self.embedded_first_line {
if let Some(embedded_path) = &self.embedded_path {
let offset = self.embedded_first_line;
Some(
SourceLocation {
path: Some(embedded_path.to_owned()),
start: Position {
line: in_merged.start.line.saturating_sub(offset),
column: in_merged.start.column,
},
end: Position {
line: in_merged.end.line.saturating_sub(offset),
column: in_merged.end.column,
}
}
)
} else {
None
}
} else {
None
};
let string = std::mem::take(&mut self.consumed);
SourceSpan { string, in_merged, in_source }
}
}
|