MCPcopy Index your code
hub / github.com/endbasic/endbasic / consume_integer

Method consume_integer

core/src/lexer.rs:349–385  ·  view source on GitHub ↗

Consumes the integer at the current position, whose first digit is `first` and which is expected to be expressed in the given `base`. `prefix_len` indicates how many characters were already consumed for this token, without counting `first`.

(
        &mut self,
        base: u8,
        pos: LineCol,
        prefix_len: usize,
    )

Source from the content-addressed store, hash-verified

347 /// expected to be expressed in the given `base`. `prefix_len` indicates how many characters
348 /// were already consumed for this token, without counting `first`.
349 fn consume_integer(
350 &mut self,
351 base: u8,
352 pos: LineCol,
353 prefix_len: usize,
354 ) -> io::Result<TokenSpan> {
355 let mut s = String::new();
356 loop {
357 match self.input.peek() {
358 Some(Ok(ch_span)) => match ch_span.ch {
359 '.' => {
360 self.input.next().unwrap()?;
361 return self
362 .handle_bad_read("Numbers in base syntax must be integers", pos);
363 }
364 ch if ch.is_ascii_digit() => s.push(self.input.next().unwrap()?.ch),
365 'a'..='f' | 'A'..='F' => s.push(self.input.next().unwrap()?.ch),
366 ch if ch.is_separator() => break,
367 ch => {
368 self.input.next().unwrap()?;
369 let msg = format!("Unexpected character in numeric literal: {}", ch);
370 return self.handle_bad_read(msg, pos);
371 }
372 },
373 Some(Err(_)) => return Err(self.input.next().unwrap().unwrap_err()),
374 None => break,
375 }
376 }
377 if s.is_empty() {
378 return self.handle_bad_read("No digits in integer literal", pos);
379 }
380
381 match u32::from_str_radix(&s, u32::from(base)) {
382 Ok(i) => Ok(TokenSpan::new(Token::Integer(i as i32), pos, s.len() + prefix_len)),
383 Err(e) => self.handle_bad_read(format!("Bad integer {}: {}", s, e), pos),
384 }
385 }
386
387 /// Consumes the integer at the current position `pos`.
388 fn consume_integer_with_base(&mut self, pos: LineCol) -> io::Result<TokenSpan> {

Callers 1

Calls 7

handle_bad_readMethod · 0.80
pushMethod · 0.80
is_separatorMethod · 0.80
peekMethod · 0.45
nextMethod · 0.45
is_emptyMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected