Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace error throwing with panic in "Lexer::next()" #229

Merged
merged 2 commits into from
Jan 20, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Replace
  • Loading branch information
croraf committed Jan 20, 2020
commit fe6a203a124ef2a0dd8a010914f334f1a1b070a4
49 changes: 24 additions & 25 deletions src/lib/syntax/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ macro_rules! vop {
let preview = $this.preview_next().unwrap();
match preview {
'=' => {
$this.next()?;
$this.next();
$assign_op
}
_ => $op,
Expand All @@ -28,11 +28,11 @@ macro_rules! vop {
let preview = $this.preview_next().unwrap();
match preview {
'=' => {
$this.next()?;
$this.next();
$assign_op
},
$($case => {
$this.next()?;
$this.next();
$block
})+,
_ => $op
Expand Down Expand Up @@ -143,18 +143,18 @@ impl<'a> Lexer<'a> {
}

/// next fetches the next token and return it, or a LexerError if there are no more.
fn next(&mut self) -> Result<char, LexerError> {
fn next(&mut self) -> char {
match self.buffer.next() {
Some(ch) => Ok(ch),
None => Err(LexerError::new("finished")),
Some(ch) => ch,
None => panic!(),
croraf marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// read_line attempts to read until the end of the line and returns the String object or a LexerError
fn read_line(&mut self) -> Result<String, LexerError> {
let mut buf = String::new();
while self.preview_next().is_some() {
let ch = self.next()?;
let ch = self.next();
match ch {
_ if ch.is_ascii_control() => {
break;
Expand All @@ -164,7 +164,6 @@ impl<'a> Lexer<'a> {
}
}
}

Ok(buf)
}

Expand All @@ -183,7 +182,7 @@ impl<'a> Lexer<'a> {
while self.buffer.peek().is_some()
&& f(self.preview_next().expect("Could not preview next value"))
{
s.push(self.next()?);
s.push(self.next());
}

Ok(s)
Expand All @@ -205,20 +204,20 @@ impl<'a> Lexer<'a> {
return Ok(());
}
self.column_number += 1;
let ch = self.next()?;
let ch = self.next();
match ch {
'"' | '\'' => {
let mut buf = String::new();
loop {
match self.next()? {
match self.next() {
'\'' if ch == '\'' => {
break;
}
'"' if ch == '"' => {
break;
}
'\\' => {
let escape = self.next()?;
let escape = self.next();
if escape != '\n' {
let escaped_ch = match escape {
'n' => '\n',
Expand All @@ -230,7 +229,7 @@ impl<'a> Lexer<'a> {
'x' => {
let mut nums = String::with_capacity(2);
for _ in 0_u8..2 {
nums.push(self.next()?);
nums.push(self.next());
}
self.column_number += 2;
let as_num = match u64::from_str_radix(&nums, 16) {
Expand Down Expand Up @@ -265,7 +264,7 @@ impl<'a> Lexer<'a> {
let c = from_u32(as_num)
.expect("Invalid Unicode escape sequence");

self.next()?; // '}'
self.next(); // '}'
self.column_number +=
(s.len() as u64).wrapping_add(3);
c
Expand Down Expand Up @@ -326,7 +325,7 @@ impl<'a> Lexer<'a> {
let num = if self.next_is('x') {
while let Some(ch) = self.preview_next() {
if ch.is_digit(16) {
buf.push(self.next()?);
buf.push(self.next());
} else {
break;
}
Expand All @@ -335,7 +334,7 @@ impl<'a> Lexer<'a> {
} else if self.next_is('b') {
while let Some(ch) = self.preview_next() {
if ch.is_digit(2) {
buf.push(self.next()?);
buf.push(self.next());
} else {
break;
}
Expand All @@ -348,15 +347,15 @@ impl<'a> Lexer<'a> {
match next_ch {
c if next_ch.is_digit(8) => {
buf.push(c);
self.next()?;
self.next();
}
'O' | 'o' => {
self.next()?;
self.next();
}
'8' | '9' | '.' => {
gone_decimal = true;
buf.push(next_ch);
self.next()?;
self.next();
}
_ => break,
}
Expand All @@ -376,7 +375,7 @@ impl<'a> Lexer<'a> {
'digitloop: while let Some(ch) = self.preview_next() {
match ch {
'.' => loop {
buf.push(self.next()?);
buf.push(self.next());

let c = match self.preview_next() {
Some(ch) => ch,
Expand All @@ -388,10 +387,10 @@ impl<'a> Lexer<'a> {
}
},
'e' | '+' | '-' => {
buf.push(self.next()?);
buf.push(self.next());
}
_ if ch.is_digit(10) => {
buf.push(self.next()?);
buf.push(self.next());
}
_ => break,
}
Expand All @@ -405,7 +404,7 @@ impl<'a> Lexer<'a> {
let mut buf = ch.to_string();
while let Some(ch) = self.preview_next() {
if ch.is_alphabetic() || ch.is_digit(10) || ch == '_' {
buf.push(self.next()?);
buf.push(self.next());
} else {
break;
}
Expand Down Expand Up @@ -463,7 +462,7 @@ impl<'a> Lexer<'a> {
'*' => {
let mut buf = String::new();
loop {
match self.next()? {
match self.next() {
'*' => {
if self.next_is('/') {
break;
Expand Down Expand Up @@ -497,7 +496,7 @@ impl<'a> Lexer<'a> {
// escape sequence
Some('\\') => {
body.push('\\');
match self.next()? {
match self.next() {
// newline not allowed in regex literal
'\n' | '\r' | '\u{2028}' | '\u{2029}' => break,
ch => body.push(ch),
Expand Down