| 1 | use crate::token::{Integer, PreprocessorError, Punct}; |
| 2 | |
| 3 | use super::{Define, Location, MacroProcessor, MeLexer, Step, StepExit, Token, TokenValue}; |
| 4 | use std::{collections::HashMap, convert::TryInto, rc::Rc, vec}; |
| 5 | |
| 6 | struct IfLexer<'macros> { |
| 7 | tokens: vec::IntoIter<Token>, |
| 8 | defines: &'macros HashMap<String, Rc<Define>>, |
| 9 | } |
| 10 | |
| 11 | pub(super) struct IfParser<'macros> { |
| 12 | lexer: IfLexer<'macros>, |
| 13 | macro_processor: MacroProcessor, |
| 14 | location: Location, |
| 15 | |
| 16 | parsing_if: bool, |
| 17 | carry: Option<Token>, |
| 18 | } |
| 19 | |
| 20 | impl<'macros> IfParser<'macros> { |
| 21 | /// Builds a new IfParser that can be reused |
| 22 | /// |
| 23 | /// `parsing_if` indicates wether or not non defined macros should be |
| 24 | /// replaced with 0 |
| 25 | pub fn new( |
| 26 | tokens: Vec<Token>, |
| 27 | defines: &'macros HashMap<String, Rc<Define>>, |
| 28 | location: Location, |
| 29 | parsing_if: bool, |
| 30 | ) -> Self { |
| 31 | IfParser { |
| 32 | lexer: IfLexer { |
| 33 | tokens: tokens.into_iter(), |
| 34 | defines, |
| 35 | }, |
| 36 | macro_processor: MacroProcessor::default(), |
| 37 | location, |
| 38 | |
| 39 | parsing_if, |
| 40 | carry: None, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /// Helper method to consume the next token without define expansion |
| 45 | fn raw_next(&mut self) -> Option<Token> { |
| 46 | self.carry |
| 47 | .take() |
| 48 | .or_else(|| self.macro_processor.step(&mut self.lexer).ok()) |
| 49 | } |
| 50 | |
| 51 | /// Helper method to consume the next token with define expansion |
| 52 | fn next(&mut self) -> Step<Option<Token>> { |
| 53 | let token = match self.raw_next() { |
| 54 | Some(t) => t, |
| 55 | None => return Ok(None), |
| 56 | }; |
| 57 | |
| 58 | Ok(match token.value { |
| 59 | TokenValue::Ident(ref name) if name != "defined" => { |
| 60 | match self.add_define(name, token.location)? { |
| 61 | Some(t) => Some(t), |
| 62 | None => self.next()?, |
| 63 | } |
| 64 | } |
| 65 | _ => Some(token), |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | /// Helper method to get the next token with define expansion |
| 70 | pub fn peek(&mut self) -> Step<Option<Token>> { |
| 71 | self.carry = self.next()?; |
| 72 | Ok(self.carry.clone()) |
| 73 | } |
| 74 | |
| 75 | /// Helper method to consume the next token without define expansion |
| 76 | /// |
| 77 | /// Returns an EOI error if there are no further tokens |
| 78 | fn expect_raw_next(&mut self) -> Step<Token> { |
| 79 | self.raw_next().ok_or(StepExit::Error(( |
| 80 | PreprocessorError::UnexpectedEndOfInput, |
| 81 | self.location, |
| 82 | ))) |
| 83 | } |
| 84 | |
| 85 | /// Helper method to consume the next token with define expansion |
| 86 | /// |
| 87 | /// Returns an EOI error if there are no further tokens |
| 88 | fn expect_next(&mut self) -> Step<Token> { |
| 89 | self.next()?.ok_or(StepExit::Error(( |
| 90 | PreprocessorError::UnexpectedEndOfInput, |
| 91 | self.location, |
| 92 | ))) |
| 93 | } |
| 94 | |
| 95 | /// Helper method to get the next token with define expansion |
| 96 | /// |
| 97 | /// Returns an EOI error if there are no further tokens |
| 98 | fn expect_peek(&mut self) -> Step<Token> { |
| 99 | self.peek()?.ok_or(StepExit::Error(( |
| 100 | PreprocessorError::UnexpectedEndOfInput, |
| 101 | self.location, |
| 102 | ))) |
| 103 | } |
| 104 | |
| 105 | fn add_define(&mut self, name: &str, location: Location) -> Step<Option<Token>> { |
| 106 | if self |
| 107 | .macro_processor |
| 108 | .start_define_invocation(name, location, &mut self.lexer)? |
| 109 | { |
| 110 | Ok(None) |
| 111 | } else if self.parsing_if { |
| 112 | Ok(Some(Token { |
| 113 | value: TokenValue::Integer(Integer { |
| 114 | value: 0, |
| 115 | signed: true, |
| 116 | width: 64, |
| 117 | }), |
| 118 | location, |
| 119 | })) |
| 120 | } else { |
| 121 | Err(StepExit::Error(( |
| 122 | PreprocessorError::UnexpectedToken(TokenValue::Ident(name.to_string())), |
| 123 | location, |
| 124 | ))) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | fn handle_defined(&mut self) -> Step<i64> { |
| 129 | let next = self.expect_raw_next()?; |
| 130 | |
| 131 | match next.value { |
| 132 | TokenValue::Ident(ref name) => Ok(self.lexer.defines.get(name).is_some() as i64), |
| 133 | TokenValue::Punct(Punct::LeftParen) => { |
| 134 | let name_token = self.expect_raw_next()?; |
| 135 | let name = match name_token.value { |
| 136 | TokenValue::Ident(name) => Ok(name), |
| 137 | value => Err(StepExit::Error(( |
| 138 | PreprocessorError::UnexpectedToken(value), |
| 139 | name_token.location, |
| 140 | ))), |
| 141 | }?; |
| 142 | |
| 143 | let close_brace = self.expect_next()?; |
| 144 | |
| 145 | match close_brace.value { |
| 146 | TokenValue::Punct(Punct::RightParen) => { |
| 147 | Ok(self.lexer.defines.get(&name).is_some() as i64) |
| 148 | } |
| 149 | value => Err(StepExit::Error(( |
| 150 | PreprocessorError::UnexpectedToken(value), |
| 151 | close_brace.location, |
| 152 | ))), |
| 153 | } |
| 154 | } |
| 155 | value => Err(StepExit::Error(( |
| 156 | PreprocessorError::UnexpectedToken(value), |
| 157 | next.location, |
| 158 | ))), |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | fn parse_atom(&mut self) -> Step<i64> { |
| 163 | let token = self.expect_next()?; |
| 164 | |
| 165 | match token.value { |
| 166 | TokenValue::Ident(name) => { |
| 167 | debug_assert_eq!(name, "defined"); |
| 168 | |
| 169 | self.handle_defined() |
| 170 | } |
| 171 | TokenValue::Integer(int) => Ok(int.value as i64), |
| 172 | TokenValue::Punct(Punct::LeftParen) => { |
| 173 | let val = self.parse_logical_or()?; |
| 174 | |
| 175 | let close_brace = self.expect_next()?; |
| 176 | |
| 177 | match close_brace.value { |
| 178 | TokenValue::Punct(Punct::RightParen) => Ok(val), |
| 179 | value => Err(StepExit::Error(( |
| 180 | PreprocessorError::UnexpectedToken(value), |
| 181 | close_brace.location, |
| 182 | ))), |
| 183 | } |
| 184 | } |
| 185 | value => Err(StepExit::Error(( |
| 186 | PreprocessorError::UnexpectedToken(value), |
| 187 | token.location, |
| 188 | ))), |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | fn parse_unary(&mut self) -> Step<i64> { |
| 193 | match self.expect_peek()?.value { |
| 194 | TokenValue::Punct(punct) => match punct { |
| 195 | Punct::Plus | Punct::Minus | Punct::Bang | Punct::Tilde => { |
| 196 | self.next()?; |
| 197 | |
| 198 | let val = self.parse_unary()?; |
| 199 | |
| 200 | Ok(match punct { |
| 201 | Punct::Plus => val, |
| 202 | Punct::Minus => -val, |
| 203 | Punct::Bang => (val == 0) as i64, |
| 204 | Punct::Tilde => !val, |
| 205 | _ => unreachable!(), |
| 206 | }) |
| 207 | } |
| 208 | _ => self.parse_atom(), |
| 209 | }, |
| 210 | _ => self.parse_atom(), |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | fn parse_multiplicative(&mut self) -> Step<i64> { |
| 215 | let mut left = self.parse_unary()?; |
| 216 | |
| 217 | while let Some(TokenValue::Punct(punct)) = self.peek()?.map(|t| t.value) { |
| 218 | if let Punct::Star | Punct::Slash | Punct::Percent = punct { |
| 219 | self.next()?; |
| 220 | |
| 221 | let right = self.parse_unary()?; |
| 222 | |
| 223 | match punct { |
| 224 | Punct::Star => { |
| 225 | left = left.checked_mul(right).ok_or(StepExit::Error(( |
| 226 | PreprocessorError::IntegerOverflow, |
| 227 | self.location, |
| 228 | )))? |
| 229 | } |
| 230 | Punct::Slash => { |
| 231 | left = left.checked_div(right).ok_or(StepExit::Error(( |
| 232 | PreprocessorError::DivisionByZero, |
| 233 | self.location, |
| 234 | )))? |
| 235 | } |
| 236 | Punct::Percent => { |
| 237 | left = left.checked_rem(right).ok_or(StepExit::Error(( |
| 238 | PreprocessorError::DivisionByZero, |
| 239 | self.location, |
| 240 | )))? |
| 241 | } |
| 242 | _ => unreachable!(), |
| 243 | } |
| 244 | } else { |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | Ok(left) |
| 250 | } |
| 251 | |
| 252 | fn parse_additive(&mut self) -> Step<i64> { |
| 253 | let mut left = self.parse_multiplicative()?; |
| 254 | |
| 255 | while let Some(TokenValue::Punct(punct)) = self.peek()?.map(|t| t.value) { |
| 256 | if let Punct::Plus | Punct::Minus = punct { |
| 257 | self.next()?; |
| 258 | |
| 259 | let right = self.parse_multiplicative()?; |
| 260 | |
| 261 | match punct { |
| 262 | Punct::Plus => { |
| 263 | left = left.checked_add(right).ok_or(StepExit::Error(( |
| 264 | PreprocessorError::IntegerOverflow, |
| 265 | self.location, |
| 266 | )))? |
| 267 | } |
| 268 | Punct::Minus => { |
| 269 | left = left.checked_sub(right).ok_or(StepExit::Error(( |
| 270 | PreprocessorError::IntegerOverflow, |
| 271 | self.location, |
| 272 | )))? |
| 273 | } |
| 274 | _ => unreachable!(), |
| 275 | } |
| 276 | } else { |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | Ok(left) |
| 282 | } |
| 283 | |
| 284 | fn parse_shift(&mut self) -> Step<i64> { |
| 285 | let mut left = self.parse_additive()?; |
| 286 | |
| 287 | while let Some(TokenValue::Punct(punct)) = self.peek()?.map(|t| t.value) { |
| 288 | if let Punct::LeftShift | Punct::RightShift = punct { |
| 289 | self.next()?; |
| 290 | |
| 291 | let right = self.parse_additive()?; |
| 292 | |
| 293 | match punct { |
| 294 | Punct::LeftShift => { |
| 295 | let right = right.try_into().map_err(|_| { |
| 296 | StepExit::Error((PreprocessorError::IntegerOverflow, self.location)) |
| 297 | })?; |
| 298 | left = left.checked_shl(right).ok_or(StepExit::Error(( |
| 299 | PreprocessorError::IntegerOverflow, |
| 300 | self.location, |
| 301 | )))? |
| 302 | } |
| 303 | Punct::RightShift => { |
| 304 | let right = right.try_into().map_err(|_| { |
| 305 | StepExit::Error((PreprocessorError::IntegerOverflow, self.location)) |
| 306 | })?; |
| 307 | left = left.checked_shr(right).ok_or(StepExit::Error(( |
| 308 | PreprocessorError::IntegerOverflow, |
| 309 | self.location, |
| 310 | )))? |
| 311 | } |
| 312 | _ => unreachable!(), |
| 313 | } |
| 314 | } else { |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | Ok(left) |
| 320 | } |
| 321 | |
| 322 | fn parse_comparative(&mut self) -> Step<i64> { |
| 323 | let mut left = self.parse_shift()?; |
| 324 | |
| 325 | while let Some(TokenValue::Punct(punct)) = self.peek()?.map(|t| t.value) { |
| 326 | if let Punct::LeftAngle | Punct::RightAngle | Punct::LessEqual | Punct::GreaterEqual = |
| 327 | punct |
| 328 | { |
| 329 | self.next()?; |
| 330 | |
| 331 | let right = self.parse_shift()?; |
| 332 | |
| 333 | match punct { |
| 334 | Punct::LeftAngle => left = (left < right) as i64, |
| 335 | Punct::RightAngle => left = (left > right) as i64, |
| 336 | Punct::LessEqual => left = (left <= right) as i64, |
| 337 | Punct::GreaterEqual => left = (left >= right) as i64, |
| 338 | _ => unreachable!(), |
| 339 | } |
| 340 | } else { |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | Ok(left) |
| 346 | } |
| 347 | |
| 348 | fn parse_equality(&mut self) -> Step<i64> { |
| 349 | let mut left = self.parse_comparative()?; |
| 350 | |
| 351 | while let Some(TokenValue::Punct(punct)) = self.peek()?.map(|t| t.value) { |
| 352 | if let Punct::EqualEqual | Punct::NotEqual = punct { |
| 353 | self.next()?; |
| 354 | |
| 355 | let right = self.parse_comparative()?; |
| 356 | |
| 357 | match punct { |
| 358 | Punct::EqualEqual => left = (left == right) as i64, |
| 359 | Punct::NotEqual => left = (left != right) as i64, |
| 360 | _ => unreachable!(), |
| 361 | } |
| 362 | } else { |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | Ok(left) |
| 368 | } |
| 369 | |
| 370 | fn parse_bit_and(&mut self) -> Step<i64> { |
| 371 | let mut left = self.parse_equality()?; |
| 372 | |
| 373 | while let Some(TokenValue::Punct(Punct::Ampersand)) = self.peek()?.map(|t| t.value) { |
| 374 | self.next()?; |
| 375 | |
| 376 | let right = self.parse_equality()?; |
| 377 | |
| 378 | left &= right |
| 379 | } |
| 380 | |
| 381 | Ok(left) |
| 382 | } |
| 383 | |
| 384 | fn parse_bit_xor(&mut self) -> Step<i64> { |
| 385 | let mut left = self.parse_bit_and()?; |
| 386 | |
| 387 | while let Some(TokenValue::Punct(Punct::Caret)) = self.peek()?.map(|t| t.value) { |
| 388 | self.next()?; |
| 389 | |
| 390 | let right = self.parse_bit_and()?; |
| 391 | |
| 392 | left ^= right |
| 393 | } |
| 394 | |
| 395 | Ok(left) |
| 396 | } |
| 397 | |
| 398 | fn parse_bit_or(&mut self) -> Step<i64> { |
| 399 | let mut left = self.parse_bit_xor()?; |
| 400 | |
| 401 | while let Some(TokenValue::Punct(Punct::Pipe)) = self.peek()?.map(|t| t.value) { |
| 402 | self.next()?; |
| 403 | |
| 404 | let right = self.parse_bit_xor()?; |
| 405 | |
| 406 | left |= right |
| 407 | } |
| 408 | |
| 409 | Ok(left) |
| 410 | } |
| 411 | |
| 412 | fn parse_logical_and(&mut self) -> Step<i64> { |
| 413 | let mut left = self.parse_bit_or()?; |
| 414 | |
| 415 | while let Some(TokenValue::Punct(Punct::LogicalAnd)) = self.peek()?.map(|t| t.value) { |
| 416 | self.next()?; |
| 417 | |
| 418 | let right = self.parse_bit_or()?; |
| 419 | |
| 420 | left = (left != 0 && right != 0) as i64; |
| 421 | } |
| 422 | |
| 423 | Ok(left) |
| 424 | } |
| 425 | |
| 426 | fn parse_logical_or(&mut self) -> Step<i64> { |
| 427 | let mut left = self.parse_logical_and()?; |
| 428 | |
| 429 | while let Some(TokenValue::Punct(Punct::LogicalOr)) = self.peek()?.map(|t| t.value) { |
| 430 | self.next()?; |
| 431 | |
| 432 | let right = self.parse_logical_and()?; |
| 433 | |
| 434 | left = (left != 0 || right != 0) as i64; |
| 435 | } |
| 436 | |
| 437 | Ok(left) |
| 438 | } |
| 439 | |
| 440 | pub fn evaluate_expression(&mut self) -> Step<i64> { |
| 441 | self.parse_logical_or() |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | impl<'macros> MeLexer for IfLexer<'macros> { |
| 446 | fn step(&mut self) -> Step<Token> { |
| 447 | self.tokens.next().ok_or(StepExit::Finished) |
| 448 | } |
| 449 | |
| 450 | fn get_define(&self, name: &str) -> Option<&Rc<Define>> { |
| 451 | self.defines.get(name) |
| 452 | } |
| 453 | |
| 454 | fn apply_line_offset(&self, line: u32, _: Location) -> Step<u32> { |
| 455 | Ok(line) |
| 456 | } |
| 457 | } |