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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
use crate::*;
use std::path::PathBuf;
/// Translate raw source code characters into syntactic tokens.
pub struct SyntacticParser {
/// Path of file from which the source was read.
path: Option<PathBuf>,
/// Path of the original source file.
source_path: Option<PathBuf>,
/// Position of the next character to be read.
position: Position,
/// Previous value of the position field.
prev_position: Position,
/// Line where the embedded source file begins.
source_line_start: usize,
/// Characters waiting to be parsed, in reverse order.
chars: Vec<char>,
/// The token currently being parsed.
token_source_string: String,
/// The name of the most recently parsed label.
label: String,
}
impl SyntacticParser {
/// Parse source code.
pub fn from_source_code<P: Into<PathBuf>>(source_code: &str, path: Option<P>) -> Self {
Self {
path: path.map(|p| p.into()),
source_path: None,
position: Position { line: 0, column: 0 },
prev_position: Position { line: 0, column: 0 },
source_line_start: 0,
chars: source_code.chars().rev().collect(),
token_source_string: String::new(),
label: String::new(),
}
}
/// Return the next character, keeping it on the queue.
fn peek_char(&self) -> Option<char> {
self.chars.last().copied()
}
/// Return the next character, removing it from the queue.
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.token_source_string.push(c);
}
return option;
}
/// Remove the next character from the queue.
fn drop_char(&mut self) {
if let Some(c) = self.chars.pop() {
self.prev_position = self.position;
self.position.advance(c);
}
}
/// Remove leading whitespace.
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.
fn eat_token(&mut self) -> String {
const DELIMITERS: [char; 13] =
['@', '&', '%', ';', '[', ']', '{', '}', '(', '"', '\'', '#', '~'];
let mut token = String::new();
while let Some(peek) = self.peek_char() {
if peek.is_whitespace() || DELIMITERS.contains(&peek) {
break;
}
let c = self.eat_char().unwrap();
token.push(c);
if c == ':' {
break;
}
}
token
}
/// Return all characters until the delimiter, removing all returned
/// characters and the delimiter from the queue. Returns None if end
/// of source is reached before delimiter is found.
fn eat_to_delim(&mut self, delim: char) -> Option<String> {
let mut token = String::new();
while let Some(c) = self.eat_char() {
self.token_source_string.push(c);
match c == delim {
true => return Some(token),
false => token.push(c),
}
}
return None;
}
fn is_line_empty(&self) -> bool {
for c in self.chars.iter().rev() {
if *c == '\n' {
return true;
}
if !c.is_whitespace() {
return false
}
}
return false;
}
}
impl Iterator for SyntacticParser {
type Item = SyntacticToken;
/// Sequentially parse tokens from the source code.
fn next(&mut self) -> Option<SyntacticToken> {
use SyntacticTokenVariant as SynVar;
use SyntacticParseError as SynErr;
self.drop_whitespace();
let start = self.position;
let variant = match self.eat_char()? {
'@' => {
self.label = self.eat_token();
SynVar::LabelDefinition(self.label.clone())
}
'&' => {
let token = self.eat_token();
let sublabel = format!("{}/{token}", self.label);
SynVar::LabelDefinition(sublabel)
}
'%' => SynVar::MacroDefinition(self.eat_token()),
';' => SynVar::MacroDefinitionTerminator,
'[' => SynVar::MarkOpen,
']' => SynVar::MarkClose,
'{' => SynVar::BlockOpen,
'}' => SynVar::BlockClose,
'(' => match self.eat_to_delim(')') {
Some(string) => SynVar::Comment(string),
None => SynVar::Error(SynErr::UnterminatedComment),
}
'\'' => match self.eat_to_delim('\'') {
Some(string) => SynVar::String(string.as_bytes().to_vec()),
None => SynVar::Error(SynErr::UnterminatedRawString),
}
'"' => match self.eat_to_delim('"') {
Some(string) => {
let mut bytes = string.as_bytes().to_vec();
bytes.push(0x00);
SynVar::String(bytes)
}
None => SynVar::Error(SynErr::UnterminatedNullString),
}
'#' => {
let token = self.eat_token();
match token.parse::<Value>() {
Ok(value) => SynVar::Padding(value),
Err(_) => SynVar::Error(SynErr::InvalidPaddingValue(token)),
}
},
'~' => {
let token = self.eat_token();
let symbol = format!("{}/{token}", self.label);
SynVar::Symbol(symbol)
}
':' => SynVar::Symbol(String::from(':')),
c => {
let token = format!("{c}{}", self.eat_token());
match token.parse::<Value>() {
Ok(value) => SynVar::Literal(value),
Err(_) => match token.parse::<Instruction>() {
Ok(instruction) => SynVar::Instruction(instruction),
Err(_) => SynVar::Symbol(token),
}
}
}
};
// Parse source path comments.
if let SynVar::Comment(comment) = &variant {
// Check that the comment fills the entire line.
if start.column == 0 && self.is_line_empty() {
if let Some(path) = comment.strip_prefix(": ") {
self.source_path = Some(PathBuf::from(path.trim()));
self.source_line_start = start.line + 1;
}
}
}
// Find location in current merged file.
let in_merged = SourceLocation {
path: self.path.to_owned(),
start,
end: self.prev_position,
};
// Find location in original source file.
let in_source = if start.line >= self.source_line_start {
match &self.source_path {
Some(path) => {
let offset = self.source_line_start;
Some( SourceLocation {
path: Some(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,
}
})
}
None => None,
}
} else {
None
};
let string = std::mem::take(&mut self.token_source_string);
let source = SourceSpan { string, in_merged, in_source };
Some( SyntacticToken { source, variant } )
}
}
#[derive(Debug)]
pub enum ParseError {
InvalidExtension,
NotFound,
NotReadable,
IsADirectory,
InvalidUtf8,
Unknown,
}
|