Skip to content

Commit

Permalink
feat: add support for escape sequences in char literral + clarify err…
Browse files Browse the repository at this point in the history
…ors when the quote isn't closed
  • Loading branch information
Larsouille25 committed Aug 10, 2023
1 parent 2a2e308 commit 84d383d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
2 changes: 1 addition & 1 deletion zom/src/ops/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn dev() -> Result<ExitStatus, Box<dyn Error>> {

let buffer =
// String::from("func foo(bar: i16, baz: str) void { foo(test, test); foo = 999 + 9 / 4; foo } extern foo_c(boobar: u32) void;");
String::from(r#" "test 123456789 \n \r \t \0" 't' "#);
String::from(r#" "test " 't' "#);

print!("input: ");
stdout().flush().expect("ERR: Flush the output failed.");
Expand Down
62 changes: 55 additions & 7 deletions zom_fe/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,18 @@ impl<'a> Lexer<'a> {
Some(c) => {
str.push(c);
}
_ => return Err(self.unexpected_eof())
_ => return Err(Box::new(ZomError::new(
Some(Position::try_from_range(
self.pos,
pos_start..=pos_start,
self.text.clone(),
self.filename.clone()
).unwrap()),
"unterminated double quote string".to_owned(),
false,
Some(r#"add `"` at the end of the string literal"#.to_owned()),
vec![],
)))
}
}
Ok(Token {
Expand All @@ -405,9 +416,13 @@ impl<'a> Lexer<'a> {

let text = self.text.clone();

let mut chars = text[self.pos + 1..self.pos + 3].chars();
let window = match text.get(self.pos + 1..self.pos + 3) {
Some(w) => w,
None => return Err(self.unexpected_eof())
};
let mut chars = window.chars();
dbg!(&chars);
let _first = chars.next();
let first = chars.next();
let second = chars.next();

// dbg!(first);
Expand All @@ -418,13 +433,19 @@ impl<'a> Lexer<'a> {

match second {
Some('\'') => is_char = true,
Some(_) => is_char = false,
Some(_) => {
match first {
Some('\\') => is_char = true,
Some(_) => is_char = false,
_ => return Err(self.unexpected_eof())
}
},
_ => return Err(self.unexpected_eof())
}

// dbg!(&text[self.pos..self.pos + 2]);
// dbg!(&text);
// dbg!(is_char);
dbg!(is_char);
// println!("\n\n");

if is_char {
Expand All @@ -443,11 +464,38 @@ impl<'a> Lexer<'a> {
self.incr_pos();

let content = match ch {
Some(c) => c,
Some('\\') => {
self.incr_pos();
match self.chars.next() {
Some(c @ '\'') => c,
Some(c) => self.lex_escape_sequence(c)?,
_ => return Err(self.unexpected_eof())
}
}
Some(c) => {
dbg!(c);
c
},
_ => return Err(self.unexpected_eof())
};
self.chars.next();
let next = self.chars.next();
self.incr_pos();
match next {
Some('\'') => {}
Some(_) => return Err(Box::new(ZomError::new(
Some(Position::try_from_range(
self.pos,
pos_start..=pos_start,
self.text.clone(),
self.filename.clone()
).unwrap()),
"unterminated simple quote string".to_owned(),
false,
Some("add `'` at the end of the char literal".to_owned()),
vec![],
))),
_ => return Err(self.unexpected_eof())
}

Ok(Token {
tt: TokenType::Char(content),
Expand Down

0 comments on commit 84d383d

Please sign in to comment.