forked from utdscheld/name
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.rs
166 lines (151 loc) · 5.63 KB
/
parser.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
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
use crate::tokens::Token;
use crate::definitions::structs::LineComponent;
use logos::Logos;
// This function uses a regex library to simplify parsing.
pub fn parse_components(line: String) -> Result<Option<Vec<LineComponent>>, String> {
let mut components = vec![];
let mut lexer = Token::lexer(&line);
let mut line_position: i32 = 0;
let mut label_encountered: bool = false;
let mut mnemonic_expected: bool = true;
while let Some(token) = lexer.next() {
let unwrapped_token = match token {
Ok(tok) => tok,
Err(_) => {
return Err(format!(
" - NAME did not like that token: {:?}\n - In context: {}",
lexer.slice(),
line
))
}
};
let slice = lexer.slice();
let component_result = token_to_line_component(unwrapped_token, slice, mnemonic_expected);
let component: LineComponent;
match component_result {
Ok(found) => component = found,
Err(e) => return Err(e),
}
match component {
LineComponent::Label(_) => label_encountered = true,
_ => {}
}
line_position += 1;
mnemonic_expected = line_position == 1 && label_encountered;
components.push(component);
}
match components.len() {
0 => {
return Ok(None);
}
_ => {
return Ok(Some(components));
}
}
}
// Required implementation for regex library
fn token_to_line_component(
token: Token,
slice: &str,
mnemonic_expected: bool,
) -> Result<LineComponent, String> {
match token {
Token::Directive => return Ok(LineComponent::Directive(slice.to_string())),
Token::Label => {
return Ok(LineComponent::Label(slice[..slice.len() - 1].to_string()));
}
Token::Identifier => {
if mnemonic_expected {
return Ok(LineComponent::Mnemonic(slice.to_string()));
} else {
return Ok(LineComponent::Identifier(slice.to_string()));
}
}
Token::Register => return Ok(LineComponent::Register(slice.to_string())),
Token::HexNumber => {
if let Ok(value) = i32::from_str_radix(&slice[2..], 16) {
return Ok(LineComponent::Immediate(value));
} else {
return Err("Failed to parse as hexadecimal.".to_string());
}
}
Token::BinaryNumber => {
if let Ok(value) = i32::from_str_radix(&slice[2..], 2) {
return Ok(LineComponent::Immediate(value));
} else {
return Err("Failed to parse as binary.".to_string());
}
}
Token::OctalNumber => {
if let Ok(value) = i32::from_str_radix(&slice[1..], 8) {
return Ok(LineComponent::Immediate(value));
} else {
return Err("Failed to parse as octal.".to_string());
}
}
Token::DecimalNumber => {
if let Ok(value) = i32::from_str_radix(&slice, 10) {
return Ok(LineComponent::Immediate(value));
} else {
return Err("Failed to parse as decimal.".to_string());
}
}
Token::SingleQuote => {
let mut new_slice = slice.chars().skip(1);
let c = new_slice.next().ok_or("Empty char literal supplied")?;
if c as u32 == 0x5c {
// 0x5c is backslash
let escape_sequence_char =
new_slice.next().ok_or("Escape sequence without chars")?;
let escaped_slice: char = parse_escape_sequence(escape_sequence_char);
return Ok(LineComponent::Immediate(escaped_slice as i32));
}
return Ok(LineComponent::Immediate(c as i32));
}
Token::DoubleQuote => {
// return Ok(LineComponent::DoubleQuote(slice[1..slice.len()-1].to_string()));
let mut chars = slice[1..slice.len() - 1].chars().peekable();
let mut result = String::new();
// this works for now i'll deal with it later
while let Some(c) = chars.next() {
if c == '\\' {
if let Some(&next_char) = chars.peek() {
let parsed_char = parse_escape_sequence(next_char);
result.push(parsed_char);
chars.next();
} else {
result.push(c);
}
} else {
result.push(c);
}
}
return Ok(LineComponent::DoubleQuote(result));
}
Token::Colon => {
return Ok(LineComponent::Colon);
}
_ => {
return Err(format!(
"pattern \"{slice}\" could not be matched by parser."
))
}
}
}
// This function just maps a character to its respective escape sequence.
fn parse_escape_sequence(escaped_char: char) -> char {
match escaped_char {
't' => 9 as char, // tab
'n' => 10 as char, // newline
'r' => 13 as char, // carriage return (who uses this)
'b' => 8 as char, // backspace (what.)
'f' => 12 as char, // form feed (yeah okay)
'\'' => '\'', // take this out if it doesn't work or is redundant
'\"' => '\"', // (this might be the only non redundant part actually)
'\\' => '\\',
_ => {
eprintln!("Escape sequence \\{} not implemented.", escaped_char);
return escaped_char;
}
}
}