lex: implement string escape chars
This commit is contained in:
parent
efe80cb930
commit
5ad4b649ce
1 changed files with 20 additions and 3 deletions
|
@ -34,7 +34,7 @@ impl Iterator for Lexer<'_> {
|
||||||
type Item = Result<Token, SyntaxError>;
|
type Item = Result<Token, SyntaxError>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Result<Token, SyntaxError>> {
|
fn next(&mut self) -> Option<Result<Token, SyntaxError>> {
|
||||||
self.read_white();
|
self.read_whitespace();
|
||||||
|
|
||||||
Some(match self.cursor.next()? {
|
Some(match self.cursor.next()? {
|
||||||
';' => Ok(self.make_token(TokenKind::Semi)),
|
';' => Ok(self.make_token(TokenKind::Semi)),
|
||||||
|
@ -100,10 +100,14 @@ impl <'a> Lexer<'a> {
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// TODO: respect escapes
|
|
||||||
match self.cursor.next() {
|
match self.cursor.next() {
|
||||||
None | Some('"') => break,
|
None | Some('"') => break,
|
||||||
|
|
||||||
|
Some('\\') => match self.read_escape_seq() {
|
||||||
|
Ok(c) => s.push(c),
|
||||||
|
Err(e) => return Err(e),
|
||||||
|
},
|
||||||
|
|
||||||
Some('\n') => return Err(SyntaxError {
|
Some('\n') => return Err(SyntaxError {
|
||||||
msg: String::from("Unexpected EOL"),
|
msg: String::from("Unexpected EOL"),
|
||||||
line: self.cursor.line(),
|
line: self.cursor.line(),
|
||||||
|
@ -117,6 +121,19 @@ impl <'a> Lexer<'a> {
|
||||||
Ok(self.make_token(TokenKind::StringLiteral(s)))
|
Ok(self.make_token(TokenKind::StringLiteral(s)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn read_escape_seq(&mut self) -> Result<char, SyntaxError> {
|
||||||
|
match self.cursor.next() {
|
||||||
|
Some('\\') => Ok('\\'),
|
||||||
|
Some('n') => Ok('\n'),
|
||||||
|
Some('"') => Ok('"'),
|
||||||
|
_ => Err(SyntaxError {
|
||||||
|
msg: String::from("Illegal escape sequence"),
|
||||||
|
line: self.token_line,
|
||||||
|
col: self.token_col,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn read_int_rest(&mut self, first: char) -> Token {
|
fn read_int_rest(&mut self, first: char) -> Token {
|
||||||
let mut s = String::from(first);
|
let mut s = String::from(first);
|
||||||
|
|
||||||
|
@ -135,7 +152,7 @@ impl <'a> Lexer<'a> {
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_white(&mut self) -> Option<Token> {
|
fn read_whitespace(&mut self) -> Option<Token> {
|
||||||
let ws = self.cursor.next_until(|c| !is_whitespace(c));
|
let ws = self.cursor.next_until(|c| !is_whitespace(c));
|
||||||
|
|
||||||
if ws.len() > 0 {
|
if ws.len() > 0 {
|
||||||
|
|
Loading…
Reference in a new issue