MCPcopy Create free account
hub / github.com/LaBatata101/sith-language-server / lex_decimal_number

Method lex_decimal_number

crates/sith_python_parser/src/lexer.rs:1047–1157  ·  view source on GitHub ↗

Lex a normal number, that is, no octal, hex or binary number.

(&mut self, first_digit_or_dot: char)

Source from the content-addressed store, hash-verified

1045
1046 /// Lex a normal number, that is, no octal, hex or binary number.
1047 fn lex_decimal_number(&mut self, first_digit_or_dot: char) -> TokenKind {
1048 #[cfg(debug_assertions)]
1049 debug_assert!(self.cursor.previous().is_ascii_digit() || self.cursor.previous() == '.');
1050 let start_is_zero = first_digit_or_dot == '0';
1051
1052 let mut number = LexedText::new(self.token_start(), self.source);
1053 if first_digit_or_dot != '.' {
1054 number.push(first_digit_or_dot);
1055 self.radix_run(&mut number, Radix::Decimal);
1056 };
1057
1058 let is_float = if first_digit_or_dot == '.' || self.cursor.eat_char('.') {
1059 number.push('.');
1060
1061 if self.cursor.eat_char('_') {
1062 return self.push_error(LexicalError::new(
1063 LexicalErrorType::OtherError("Invalid Syntax".to_string().into_boxed_str()),
1064 TextRange::new(self.offset() - TextSize::new(1), self.offset()),
1065 ));
1066 }
1067
1068 self.radix_run(&mut number, Radix::Decimal);
1069 true
1070 } else {
1071 // Normal number:
1072 false
1073 };
1074
1075 let is_float = match self.cursor.rest().as_bytes() {
1076 [b'e' | b'E', b'0'..=b'9', ..] | [b'e' | b'E', b'-' | b'+', b'0'..=b'9', ..] => {
1077 // 'e' | 'E'
1078 number.push(self.cursor.bump().unwrap());
1079
1080 if let Some(sign) = self.cursor.eat_if(|c| matches!(c, '+' | '-')) {
1081 number.push(sign);
1082 }
1083
1084 self.radix_run(&mut number, Radix::Decimal);
1085
1086 true
1087 }
1088 _ => is_float,
1089 };
1090
1091 if is_float {
1092 let value = match Float::from_str(number.as_str()) {
1093 Ok(value) => value,
1094 Err(err) => {
1095 return self.push_error(LexicalError::new(
1096 LexicalErrorType::OtherError(format!("{err:?}").into_boxed_str()),
1097 self.token_range(),
1098 ));
1099 }
1100 };
1101
1102 // Parse trailing 'j':
1103 if self.cursor.eat_if(|c| matches!(c, 'j' | 'J')).is_some() {
1104 self.current_value = TokenValue::Complex {

Callers 2

lex_numberMethod · 0.80

Calls 14

FloatClass · 0.85
IntClass · 0.85
token_startMethod · 0.80
radix_runMethod · 0.80
eat_charMethod · 0.80
push_errorMethod · 0.80
restMethod · 0.80
eat_ifMethod · 0.80
token_rangeMethod · 0.80
as_u8Method · 0.80
pushMethod · 0.45
offsetMethod · 0.45

Tested by

no test coverage detected