lexer: Rework lookahead

This commit is contained in:
Evan Shaw 2022-09-15 07:57:19 +12:00
parent fd118ea0da
commit cbed61165f

View File

@ -19,7 +19,6 @@ pub struct Lexer {
chars: Vec<u8>,
cursor: usize,
current: Option<u8>,
peek: Option<u8>,
col: usize,
line: usize,
}
@ -32,7 +31,6 @@ impl Lexer {
chars: Vec::new(),
cursor: 0,
current: None,
peek: None,
line: 1,
col: 0,
}
@ -45,10 +43,9 @@ impl Lexer {
let mut tokens = Vec::new();
self.chars = input.as_ref().to_vec();
self.next();
self.next();
self.current = self.chars.get(0).copied();
while self.peek.is_some() {
while self.current.is_some() {
match self.state {
// The "Initial" state is used to parse inline HTML. It is essentially a catch-all
// state that will build up a single token buffer until it encounters an open tag
@ -59,7 +56,7 @@ impl Lexer {
// The scripting state is entered when an open tag is encountered in the source code.
// This tells the lexer to start analysing characters at PHP tokens instead of inline HTML.
LexerState::Scripting => {
while let Some(c) = self.peek {
while let Some(c) = self.current {
if !c.is_ascii_whitespace() && ![b'\n', b'\t', b'\r'].contains(&c) {
break;
}
@ -75,7 +72,7 @@ impl Lexer {
}
// If we have consumed whitespace and then reached the end of the file, we should break.
if self.peek.is_none() {
if self.current.is_none() {
break;
}
@ -91,21 +88,8 @@ impl Lexer {
fn initial(&mut self) -> Result<Vec<Token>, LexerError> {
let mut buffer = Vec::new();
while let Some(char) = self.current {
match char {
b'<' => {
// This is disgusting and can most definitely be tidied up with a multi-peek iterator.
if let Some(b'?') = self.peek {
self.next();
if let Some(b'p') = self.peek {
self.next();
if let Some(b'h') = self.peek {
self.next();
if let Some(b'p') = self.peek {
self.next();
if self.try_read(b"<?php") {
self.skip(5);
self.col += 4;
self.enter_state(LexerState::Scripting);
@ -126,30 +110,10 @@ impl Lexer {
return Ok(tokens);
}
} else {
self.col += 3;
buffer.push(b'h');
}
} else {
self.col += 2;
buffer.push(b'?');
}
} else {
self.next();
self.col += 1;
buffer.push(char);
}
}
_ => {
self.next();
buffer.push(char);
}
}
}
Ok(vec![Token {
kind: TokenKind::InlineHtml(buffer.into()),
@ -160,156 +124,139 @@ impl Lexer {
fn scripting(&mut self) -> Result<Token, LexerError> {
// We should never reach this point since we have the empty checks surrounding
// the call to this function, but it's better to be safe than sorry.
if self.peek.is_none() {
return Err(LexerError::UnexpectedEndOfFile);
}
let char = match self.current {
Some(c) => c,
None => return Err(LexerError::UnexpectedEndOfFile),
};
// Since we have the check above, we can safely unwrap the result of `.next()`
// to help reduce the amount of indentation.
let kind = match self.peek() {
[b'@', ..] => {
self.next();
let char = self.current.unwrap();
let kind = match char {
b'@' => {
self.col += 1;
TokenKind::At
}
b'!' => {
self.col += 1;
if let Some(b'=') = self.peek {
self.col += 1;
self.next();
if let Some(b'=') = self.peek {
self.col += 1;
self.next();
[b'!', b'=', b'=', ..] => {
self.skip(3);
self.col += 2;
TokenKind::BangDoubleEquals
} else {
}
[b'!', b'=', ..] => {
self.skip(2);
self.col += 2;
TokenKind::BangEquals
}
} else {
TokenKind::Bang
}
}
b'&' => {
self.col += 1;
if let Some(b'&') = self.peek {
self.col += 1;
[b'!', ..] => {
self.next();
self.col += 1;
TokenKind::BangEquals
}
[b'&', b'&', ..] => {
self.skip(2);
self.col += 2;
TokenKind::BooleanAnd
} else {
}
[b'&', ..] => {
self.next();
self.col += 1;
TokenKind::Ampersand
}
}
b'?' => {
[b'?', b'>', ..] => {
// This is a close tag, we can enter "Initial" mode again.
if let Some(b'>') = self.peek {
self.next();
self.next();
self.skip(2);
self.col += 2;
self.enter_state(LexerState::Initial);
TokenKind::CloseTag
} else if let Some(b'?') = self.peek {
self.col += 1;
self.next();
if let Some(b'=') = self.peek {
self.col += 1;
self.next();
}
[b'?', b'?', b'=', ..] => {
self.skip(3);
self.col += 3;
TokenKind::CoalesceEqual
} else {
}
[b'?', b'?', ..] => {
self.skip(2);
self.col += 2;
TokenKind::Coalesce
}
} else if let Some(b':') = self.peek {
self.col += 1;
self.next();
TokenKind::QuestionColon
} else if self.try_read(b"->") {
self.col += 1;
[b'?', b':', ..] => {
self.skip(2);
self.col += 2;
TokenKind::QuestionColon
}
[b'?', b'-', b'>', ..] => {
self.skip(3);
self.col += 3;
TokenKind::NullsafeArrow
} else {
}
[b'?', ..] => {
self.next();
self.col += 1;
TokenKind::Question
}
}
b'=' => {
if let Some(b'=') = self.peek {
self.next();
if let Some(b'=') = self.peek {
self.next();
self.col += 3;
TokenKind::TripleEquals
} else {
[b'=', b'>', ..] => {
self.skip(2);
self.col += 2;
TokenKind::DoubleArrow
}
[b'=', b'=', b'=', ..] => {
self.skip(3);
self.col += 3;
TokenKind::TripleEquals
}
[b'=', b'=', ..] => {
self.skip(2);
self.col += 2;
TokenKind::DoubleEquals
}
} else if let Some(b'>') = self.peek {
[b'=', ..] => {
self.next();
self.col += 1;
TokenKind::DoubleArrow
} else {
self.col += 1;
TokenKind::Equals
}
}
// Single quoted string.
b'\'' => self.tokenize_single_quote_string(),
b'"' => self.tokenize_double_quote_string(),
b'$' => self.tokenize_variable(),
b'.' => {
self.col += 1;
if let Some(b'0'..=b'9') = self.peek {
self.tokenize_number(String::from("0."), true)?
} else if let Some(b'.') = self.peek {
[b'\'', ..] => {
self.next();
self.col += 1;
if let Some(b'.') = self.peek {
self.next();
self.col += 1;
TokenKind::Ellipsis
} else {
todo!("don't know how to handle this case yet, it should just be 2 Dot tokens...")
self.tokenize_single_quote_string()
}
} else if let Some(b'=') = self.peek {
[b'"', ..] => {
self.next();
self.col += 1;
self.tokenize_double_quote_string()
}
[b'$', ..] => {
self.next();
self.col += 1;
self.tokenize_variable()
}
[b'.', b'=', ..] => {
self.skip(2);
self.col += 2;
TokenKind::DotEquals
} else {
}
[b'.', b'0'..=b'9', ..] => {
self.next();
self.tokenize_number(String::from("0."), true)?
}
[b'.', b'.', b'.', ..] => {
self.skip(3);
self.col += 3;
TokenKind::Ellipsis
}
[b'.', ..] => {
self.next();
self.col += 1;
TokenKind::Dot
}
[b'0'..=b'9', ..] => {
self.next();
self.tokenize_number(String::from(char as char), false)?
}
b'0'..=b'9' => self.tokenize_number(String::from(char as char), false)?,
b'\\' => {
&[b'\\', n, ..] if n == b'_' || n.is_ascii_alphabetic() => {
self.col += 1;
if self
.peek
.map_or(false, |n| n == b'_' || n.is_ascii_alphabetic())
{
match self.scripting()? {
Token {
kind:
@ -322,18 +269,21 @@ impl Lexer {
}
s => unreachable!("{:?}", s),
}
} else {
}
[b'\\', ..] => {
self.next();
self.col += 1;
TokenKind::NamespaceSeparator
}
}
_ if char.is_ascii_alphabetic() || char == b'_' => {
self.next();
self.col += 1;
let mut qualified = false;
let mut last_was_slash = false;
let mut buffer = vec![char];
while let Some(next) = self.peek {
while let Some(next) = self.current {
if next.is_ascii_alphanumeric() || next == b'_' {
buffer.push(next);
self.next();
@ -361,126 +311,119 @@ impl Lexer {
.unwrap_or_else(|| TokenKind::Identifier(buffer.into()))
}
}
b'/' | b'#' => {
[b'/', b'*', ..] => {
self.next();
self.col += 1;
fn read_till_end_of_line(s: &mut Lexer) -> Vec<u8> {
s.col += 1;
let mut buffer = Vec::new();
while let Some(c) = s.peek {
if c == b'\n' {
break;
}
buffer.push(c);
s.next();
}
buffer
}
if char == b'/' && self.peek == Some(b'*') {
let mut buffer = vec![char];
while self.peek.is_some() {
self.next();
let t = self.current.unwrap();
match t {
b'*' => {
if let Some(b'/') = self.peek {
while self.current.is_some() {
match self.peek() {
[b'*', b'/', ..] => {
self.col += 2;
buffer.extend_from_slice(b"*/");
self.next();
break;
} else {
self.col += 1;
buffer.push(t);
}
}
b'\n' => {
[b'\n', ..] => {
self.line += 1;
self.col = 0;
buffer.push(b'\n');
}
_ => {
&[t, ..] => {
self.col += 1;
buffer.push(t);
}
[] => {}
}
self.next();
}
self.next();
self.col += 1;
if buffer.starts_with(b"/**") {
TokenKind::DocComment(buffer.into())
} else {
TokenKind::Comment(buffer.into())
}
} else if let Some(b'=') = self.peek {
self.col += 1;
self.next();
TokenKind::SlashEquals
} else if char == b'/' && self.peek != Some(b'/') {
TokenKind::Slash
} else if char == b'#' && self.peek == Some(b'[') {
}
[b'#', b'[', ..] => {
self.skip(2);
self.col += 2;
TokenKind::Attribute
}
&[ch @ b'/', b'/', ..] | &[ch @ b'#', ..] => {
let mut buffer = if ch == b'/' {
self.skip(2);
self.col += 2;
b"//".to_vec()
} else {
self.next();
let current = self.current.unwrap();
let mut buffer = read_till_end_of_line(self);
buffer.splice(0..0, [char, current]);
self.col += 1;
b"#".to_vec()
};
while let Some(c) = self.current {
if c == b'\n' {
break;
}
buffer.push(c);
self.next();
}
self.next();
TokenKind::Comment(buffer.into())
}
[b'/', b'=', ..] => {
self.skip(2);
self.col += 2;
TokenKind::SlashEquals
}
b'*' => {
self.col += 1;
if let Some(b'*') = self.peek {
self.col += 1;
[b'/', ..] => {
self.next();
if let Some(b'=') = self.peek {
self.col += 1;
self.next();
TokenKind::PowEquals
} else {
TokenKind::Slash
}
[b'*', b'*', ..] => {
self.skip(2);
self.col += 2;
TokenKind::Pow
}
} else if let Some(b'=') = self.peek {
self.col += 1;
self.next();
[b'*', b'=', ..] => {
self.skip(2);
self.col += 2;
TokenKind::AsteriskEqual
} else {
}
[b'*', ..] => {
self.next();
self.col += 1;
TokenKind::Asterisk
}
}
b'|' => {
self.col += 1;
if let Some(b'|') = self.peek {
self.col += 1;
self.next();
TokenKind::BooleanOr
} else {
[b'|', b'|', ..] => {
self.skip(2);
self.col += 2;
TokenKind::Pipe
}
[b'|', ..] => {
self.next();
self.col += 1;
TokenKind::Pipe
}
b'{' => {
[b'{', ..] => {
self.next();
self.col += 1;
TokenKind::LeftBrace
}
b'}' => {
[b'}', ..] => {
self.next();
self.col += 1;
TokenKind::RightBrace
}
b'(' => {
[b'(', ..] => {
self.next();
self.col += 1;
if self.try_read(b"string)") {
@ -509,116 +452,112 @@ impl Lexer {
TokenKind::LeftParen
}
}
b')' => {
[b')', ..] => {
self.next();
self.col += 1;
TokenKind::RightParen
}
b';' => {
[b';', ..] => {
self.next();
self.col += 1;
TokenKind::SemiColon
}
b'+' => {
self.col += 1;
if let Some(b'=') = self.peek {
self.col += 1;
self.next();
TokenKind::PlusEquals
} else if let Some(b'+') = self.peek {
self.col += 1;
self.next();
[b'+', b'+', ..] => {
self.skip(2);
self.col += 2;
TokenKind::Increment
} else {
}
[b'+', b'=', ..] => {
self.skip(2);
self.col += 2;
TokenKind::PlusEquals
}
[b'+', ..] => {
self.next();
self.col += 1;
TokenKind::Plus
}
[b'-', b'-', ..] => {
self.skip(2);
self.col += 2;
TokenKind::Decrement
}
b'-' => {
self.col += 1;
if let Some(b'>') = self.peek {
self.col += 1;
self.next();
[b'-', b'>', ..] => {
self.skip(2);
self.col += 2;
TokenKind::Arrow
} else if let Some(b'=') = self.peek {
self.col += 1;
self.next();
}
[b'-', b'=', ..] => {
self.skip(2);
self.col += 2;
TokenKind::MinusEquals
} else {
}
[b'-', ..] => {
self.next();
self.col += 1;
TokenKind::Minus
}
}
b'<' => {
self.col += 1;
if let Some(b'=') = self.peek {
self.next();
self.col += 1;
TokenKind::LessThanEquals
} else if let Some(b'<') = self.peek {
self.next();
if let Some(b'<') = self.peek {
[b'<', b'<', b'<', ..] => {
// TODO: Handle both heredocs and nowdocs.
self.next();
self.skip(3);
self.col += 3;
todo!("heredocs & nowdocs");
} else {
}
[b'<', b'<', ..] => {
self.skip(2);
self.col += 2;
TokenKind::LeftShift
}
} else {
[b'<', b'=', ..] => {
self.skip(2);
self.col += 2;
TokenKind::LessThanEquals
}
[b'<', ..] => {
self.next();
self.col += 1;
TokenKind::LessThan
}
}
b'>' => {
self.col += 1;
if let Some(b'=') = self.peek {
self.next();
self.col += 1;
TokenKind::GreaterThanEquals
} else if let Some(b'>') = self.peek {
self.next();
self.col += 1;
[b'>', b'>', ..] => {
self.skip(2);
self.col += 2;
TokenKind::RightShift
} else {
}
[b'>', b'=', ..] => {
self.skip(2);
self.col += 2;
TokenKind::GreaterThanEquals
}
[b'>', ..] => {
self.next();
self.col += 1;
TokenKind::GreaterThan
}
}
b',' => {
[b',', ..] => {
self.next();
self.col += 1;
TokenKind::Comma
}
b'[' => {
[b'[', ..] => {
self.next();
self.col += 1;
TokenKind::LeftBracket
}
b']' => {
[b']', ..] => {
self.next();
self.col += 1;
TokenKind::RightBracket
}
b':' => {
self.col += 1;
if let Some(b':') = self.peek {
self.col += 1;
self.next();
[b':', b':', ..] => {
self.skip(2);
self.col += 2;
TokenKind::DoubleColon
} else {
TokenKind::Colon
}
[b':', ..] => {
self.next();
self.col += 1;
TokenKind::Colon
}
_ => unimplemented!(
"<scripting> char: {}, line: {}, col: {}",
@ -640,7 +579,7 @@ impl Lexer {
let mut buffer = Vec::new();
let mut escaping = false;
while let Some(n) = self.peek {
while let Some(n) = self.current {
if !escaping && n == b'\'' {
self.next();
@ -682,7 +621,7 @@ impl Lexer {
let mut buffer = Vec::new();
let mut escaping = false;
while let Some(n) = self.peek {
while let Some(n) = self.current {
if !escaping && n == b'"' {
self.next();
@ -723,7 +662,7 @@ impl Lexer {
self.col += 1;
while let Some(n) = self.peek {
while let Some(n) = self.current {
match n {
b'0'..=b'9' if !buffer.is_empty() => {
self.col += 1;
@ -753,7 +692,7 @@ impl Lexer {
self.col += 1;
while let Some(n) = self.peek {
while let Some(n) = self.current {
match n {
b'0'..=b'9' => {
underscore = false;
@ -797,13 +736,12 @@ impl Lexer {
self.state = state;
}
fn try_read(&self, search: &'static [u8]) -> bool {
if self.current.is_none() || self.peek.is_none() {
return false;
fn peek(&self) -> &[u8] {
&self.chars[self.cursor..]
}
let start = self.cursor.saturating_sub(1);
self.chars[start..].starts_with(search)
fn try_read(&self, search: &'static [u8]) -> bool {
self.peek().starts_with(search)
}
fn skip(&mut self, count: usize) {
@ -813,9 +751,8 @@ impl Lexer {
}
fn next(&mut self) {
self.current = self.peek;
self.peek = self.chars.get(self.cursor).cloned();
self.cursor += 1;
self.current = self.chars.get(self.cursor).copied();
}
}