mirror of
https://github.com/MorizzG/rlox.git
synced 2025-12-06 12:22:42 +00:00
added escape sequences
This commit is contained in:
parent
f6dbc17f92
commit
ca6e092b38
2 changed files with 48 additions and 6 deletions
|
|
@ -238,23 +238,63 @@ impl Lexer {
|
|||
}
|
||||
|
||||
fn try_parse_string(&mut self) {
|
||||
// first '"' already consumed
|
||||
|
||||
// advance until second "
|
||||
while self.advance() != '"' {
|
||||
/* while self.advance() != '"' {
|
||||
if self.source_is_empty() {
|
||||
self.errors.push(LexerError::UnterminatedStringLiteral {
|
||||
code_pos: self.code_pos,
|
||||
});
|
||||
return;
|
||||
}
|
||||
} */
|
||||
|
||||
let mut s = String::new();
|
||||
|
||||
let starting_pos = self.code_pos;
|
||||
|
||||
loop {
|
||||
if self.source_is_empty() {
|
||||
self.errors.push(LexerError::UnterminatedStringLiteral {
|
||||
code_pos: starting_pos,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
match self.advance() {
|
||||
'"' => break,
|
||||
'\\' => {
|
||||
// escape sequence -> handle later
|
||||
if self.source_is_empty() {
|
||||
self.errors.push(LexerError::UnterminatedStringLiteral {
|
||||
code_pos: starting_pos,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
match self.advance() {
|
||||
'n' => s.push('\n'),
|
||||
'r' => s.push('\r'),
|
||||
'\\' => s.push('\\'),
|
||||
c => self.errors.push(LexerError::InvalidEscapeSequence {
|
||||
code_pos: self.code_pos,
|
||||
c,
|
||||
}),
|
||||
}
|
||||
}
|
||||
c => {
|
||||
s.push(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let string_literal: Box<str> = self.source[self.start + 1..self.current - 1]
|
||||
.iter()
|
||||
.collect();
|
||||
// let string_literal: Box<str> = self.source[self.start + 1..self.current - 1]
|
||||
// .iter()
|
||||
// .collect();
|
||||
|
||||
// Some(TokenType::String(Box::new(string_literal)))
|
||||
self.tokens
|
||||
.push(Token::new_string(string_literal, self.code_pos));
|
||||
self.tokens.push(Token::new_string(s, self.code_pos));
|
||||
}
|
||||
|
||||
fn try_parse_number(&mut self) {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@ pub enum LexerError {
|
|||
UnexpectedCharacter { c: char, code_pos: CodePos },
|
||||
#[error("unterminated string literal starting at {code_pos}.")]
|
||||
UnterminatedStringLiteral { code_pos: CodePos },
|
||||
#[error("invalid escape sequence \\{c} at {code_pos}")]
|
||||
InvalidEscapeSequence { code_pos: CodePos, c: char },
|
||||
#[error("unterminated block comment starting at {code_pos}.")]
|
||||
UnterminatedBlockComment { code_pos: CodePos },
|
||||
#[error("invalid number literal {lexeme} at {code_pos}: {msg}")]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue