1
0

Continue revisions to the lexer

This commit is contained in:
Jeff 2024-11-07 19:22:22 -05:00
parent 2e51112b4b
commit d15247a799

View File

@ -352,21 +352,30 @@ impl<'src> Lexer<'src> {
} }
fn lex_minus(&mut self) -> Result<(Token<'src>, Span), LexError> { fn lex_minus(&mut self) -> Result<(Token<'src>, Span), LexError> {
let start_pos = self.position; let start_position = self.position;
if self.peek_char().is_some_and(|char| char.is_ascii_digit()) {
return self.lex_numeric();
}
self.next_char(); self.next_char();
if let Some('=') = self.peek_char() { if let Some('=') = self.peek_char() {
self.next_char(); self.next_char();
Ok((Token::MinusEqual, Span(start_pos, self.position))) return Ok((Token::MinusEqual, Span(start_position, self.position)));
} else if self.peek_chars(8) == "Infinity" { }
if self.peek_chars(8) == "Infinity" {
self.position += 8; self.position += 8;
Ok((Token::Float("Infinity"), Span(start_pos, self.position))) return Ok((
} else { Token::Float("-Infinity"),
Ok((Token::Minus, Span(start_pos, self.position))) Span(start_position, self.position),
));
} }
Ok((Token::Minus, Span(start_position, self.position)))
} }
fn lex_star(&mut self) -> Result<(Token<'src>, Span), LexError> { fn lex_star(&mut self) -> Result<(Token<'src>, Span), LexError> {
@ -553,6 +562,22 @@ impl<'src> Lexer<'src> {
Ok((Token::RightBracket, Span(start_pos, self.position))) Ok((Token::RightBracket, Span(start_pos, self.position)))
} }
fn lex_left_brace(&mut self) -> Result<(Token<'src>, Span), LexError> {
let start_pos = self.position;
self.next_char();
Ok((Token::LeftBrace, Span(start_pos, self.position)))
}
fn lex_right_brace(&mut self) -> Result<(Token<'src>, Span), LexError> {
let start_pos = self.position;
self.next_char();
Ok((Token::RightBrace, Span(start_pos, self.position)))
}
} }
type LexerFn<'src> = fn(&mut Lexer<'src>) -> Result<(Token<'src>, Span), LexError>; type LexerFn<'src> = fn(&mut Lexer<'src>) -> Result<(Token<'src>, Span), LexError>;
@ -567,7 +592,7 @@ impl<'src> From<&char> for LexRule<'src> {
'0'..='9' => LexRule { '0'..='9' => LexRule {
lexer: Lexer::lex_numeric, lexer: Lexer::lex_numeric,
}, },
'Z'..='a' => LexRule { char if char.is_alphabetic() => LexRule {
lexer: Lexer::lex_keyword_or_identifier, lexer: Lexer::lex_keyword_or_identifier,
}, },
'"' => LexRule { '"' => LexRule {
@ -621,6 +646,12 @@ impl<'src> From<&char> for LexRule<'src> {
']' => LexRule { ']' => LexRule {
lexer: Lexer::lex_right_bracket, lexer: Lexer::lex_right_bracket,
}, },
'{' => LexRule {
lexer: Lexer::lex_left_brace,
},
'}' => LexRule {
lexer: Lexer::lex_right_brace,
},
_ => LexRule { _ => LexRule {
lexer: Lexer::lex_unexpected, lexer: Lexer::lex_unexpected,
}, },