Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save arockwell/df93e36d53f42d97d8b8c5510e636b1d to your computer and use it in GitHub Desktop.

Select an option

Save arockwell/df93e36d53f42d97d8b8c5510e636b1d to your computer and use it in GitHub Desktop.
Improved scientific notation parsing for lexer
// Handle scientific notation (e.g., 1.23e45 or 1e-10)
} else if grapheme == "e" || grapheme == "E" {
if is_scientific {
self.errors.push(LexError::new(
graphemes.concat(),
self.line,
self.column,
"number may not contain multiple exponential parts".to_owned(),
));
return;
}
// Mark as float and scientific notation
is_float = true;
is_scientific = true;
number.push_str(grapheme);
self.column += 1;
// Check for end of input
if self.column > graphemes.len() {
self.errors.push(LexError::new(
graphemes.concat(),
self.line,
self.column - 1,
"expected digits after 'e'".to_owned(),
));
return;
}
// Handle optional sign
if graphemes[self.column - 1] == "+" || graphemes[self.column - 1] == "-" {
number.push_str(graphemes[self.column - 1]);
self.column += 1;
// Check again for end of input after sign
if self.column > graphemes.len() {
self.errors.push(LexError::new(
graphemes.concat(),
self.line,
self.column - 1,
"expected digits after exponent sign".to_owned(),
));
return;
}
}
// Ensure we have at least one digit in the exponent
if self.column <= graphemes.len()
&& graphemes[self.column - 1].chars().next().unwrap_or('\0').is_ascii_digit()
{
// Consume the first digit
number.push_str(graphemes[self.column - 1]);
self.column += 1;
// Now continue in the next loop iteration to consume any additional exponent digits
// (they'll be handled by the main digit-consuming case at the top of the loop)
} else {
self.errors.push(LexError::new(
graphemes.concat(),
self.line,
self.column - 1,
"expected at least one digit in exponent".to_owned(),
));
return;
}
}
EOF < /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment