Reverse commit fa13588f69. Limit input script size not meaningful.

This commit is contained in:
Stephen Chung 2020-03-04 09:33:35 +08:00
parent b421c8ac50
commit b4d56accd4

View File

@ -5,9 +5,6 @@ use std::fmt;
use std::iter::Peekable; use std::iter::Peekable;
use std::str::Chars; use std::str::Chars;
const MAX_LINES: u16 = 65535;
const MAX_POS: u16 = 65535;
#[derive(Debug, Eq, PartialEq, Hash, Clone)] #[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub enum LexError { pub enum LexError {
UnexpectedChar(char), UnexpectedChar(char),
@ -67,8 +64,8 @@ type PERR = ParseErrorType;
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)] #[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)]
pub struct Position { pub struct Position {
line: u16, line: usize,
pos: u16, pos: usize,
} }
impl Position { impl Position {
@ -76,47 +73,29 @@ impl Position {
Self { line: 1, pos: 0 } Self { line: 1, pos: 0 }
} }
pub fn line(&self) -> u16 { pub fn line(&self) -> usize {
self.line self.line
} }
pub fn position(&self) -> u16 { pub fn position(&self) -> usize {
self.pos self.pos
} }
pub(crate) fn advance(&mut self) -> Result<u16, LexError> { pub(crate) fn advance(&mut self) {
if self.pos >= MAX_POS { self.pos += 1;
Err(LERR::InputError(format!(
"cannot advance beyond maximum line length ({})",
MAX_POS
)))
} else {
self.pos += 1;
Ok(self.pos)
}
} }
pub(crate) fn rewind(&mut self) -> Result<u16, LexError> { pub(crate) fn rewind(&mut self) {
if self.pos == 0 { if self.pos == 0 {
Err(LERR::InputError("cannot rewind at position 0".into())) panic!("cannot rewind at position 0");
} else { } else {
self.pos -= 1; self.pos -= 1;
Ok(self.pos)
} }
} }
pub(crate) fn new_line(&mut self) -> Result<u16, LexError> { pub(crate) fn new_line(&mut self) {
if self.line > MAX_LINES { self.line += 1;
Err(LERR::InputError(format!( self.pos = 0;
"reached maximum number of lines ({})",
MAX_LINES
)))
} else {
self.line += 1;
self.pos = 0;
Ok(self.pos)
}
} }
pub fn eof() -> Self { pub fn eof() -> Self {
@ -155,10 +134,10 @@ impl ParseError {
pub fn error_type(&self) -> &PERR { pub fn error_type(&self) -> &PERR {
&self.0 &self.0
} }
pub fn line(&self) -> u16 { pub fn line(&self) -> usize {
self.1.line() self.1.line()
} }
pub fn position(&self) -> u16 { pub fn position(&self) -> usize {
self.1.position() self.1.position()
} }
pub fn is_eof(&self) -> bool { pub fn is_eof(&self) -> bool {
@ -518,21 +497,14 @@ pub struct TokenIterator<'a> {
} }
impl<'a> TokenIterator<'a> { impl<'a> TokenIterator<'a> {
fn advance(&mut self) -> Result<u16, (LexError, Position)> { fn advance(&mut self) {
self.pos.advance().map_err(|err| (err, self.pos)) self.pos.advance();
} }
fn rewind(&mut self) -> Result<u16, (LexError, Position)> { fn rewind(&mut self) {
self.pos.rewind().map_err(|err| (err, self.pos)) self.pos.rewind();
} }
fn new_line(&mut self) -> Result<u16, (LexError, Position)> { fn new_line(&mut self) {
self.pos.new_line().map_err(|err| (err, self.pos)) self.pos.new_line()
}
fn advance_token(&mut self) -> Result<u16, (Token, Position)> {
self.advance().map_err(|err| (Token::LexErr(err.0), err.1))
}
fn new_line_token(&mut self) -> Result<u16, (Token, Position)> {
self.new_line().map_err(|err| (Token::LexErr(err.0), err.1))
} }
pub fn parse_string_const( pub fn parse_string_const(
@ -549,7 +521,7 @@ impl<'a> TokenIterator<'a> {
return Err((LERR::UnterminatedString, Position::eof())); return Err((LERR::UnterminatedString, Position::eof()));
} }
self.advance()?; self.advance();
match next_char.unwrap() { match next_char.unwrap() {
'\\' if escape.is_empty() => { '\\' if escape.is_empty() => {
@ -579,7 +551,7 @@ impl<'a> TokenIterator<'a> {
for _ in 0..2 { for _ in 0..2 {
if let Some(c) = self.char_stream.next() { if let Some(c) = self.char_stream.next() {
seq.push(c); seq.push(c);
self.advance()?; self.advance();
if let Some(d1) = c.to_digit(16) { if let Some(d1) = c.to_digit(16) {
out_val *= 16; out_val *= 16;
@ -606,7 +578,7 @@ impl<'a> TokenIterator<'a> {
for _ in 0..4 { for _ in 0..4 {
if let Some(c) = self.char_stream.next() { if let Some(c) = self.char_stream.next() {
seq.push(c); seq.push(c);
self.advance()?; self.advance();
if let Some(d1) = c.to_digit(16) { if let Some(d1) = c.to_digit(16) {
out_val *= 16; out_val *= 16;
@ -633,7 +605,7 @@ impl<'a> TokenIterator<'a> {
for _ in 0..8 { for _ in 0..8 {
if let Some(c) = self.char_stream.next() { if let Some(c) = self.char_stream.next() {
seq.push(c); seq.push(c);
self.advance()?; self.advance();
if let Some(d1) = c.to_digit(16) { if let Some(d1) = c.to_digit(16) {
out_val *= 16; out_val *= 16;
@ -658,7 +630,7 @@ impl<'a> TokenIterator<'a> {
return Err((LERR::MalformedEscapeSequence(escape), self.pos)) return Err((LERR::MalformedEscapeSequence(escape), self.pos))
} }
'\n' => { '\n' => {
self.rewind()?; self.rewind();
return Err((LERR::UnterminatedString, self.pos)); return Err((LERR::UnterminatedString, self.pos));
} }
x => { x => {
@ -674,18 +646,12 @@ impl<'a> TokenIterator<'a> {
fn inner_next(&mut self) -> Option<(Token, Position)> { fn inner_next(&mut self) -> Option<(Token, Position)> {
while let Some(c) = self.char_stream.next() { while let Some(c) = self.char_stream.next() {
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
let pos = self.pos; let pos = self.pos;
match c { match c {
'\n' => { '\n' => self.new_line(),
if let Err(err) = self.new_line_token() {
return Some(err);
}
}
'0'..='9' => { '0'..='9' => {
let mut result = Vec::new(); let mut result = Vec::new();
let mut radix_base: Option<u32> = None; let mut radix_base: Option<u32> = None;
@ -696,24 +662,18 @@ impl<'a> TokenIterator<'a> {
'0'..='9' | '_' => { '0'..='9' | '_' => {
result.push(next_char); result.push(next_char);
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
} }
'.' => { '.' => {
result.push(next_char); result.push(next_char);
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
while let Some(&next_char_in_float) = self.char_stream.peek() { while let Some(&next_char_in_float) = self.char_stream.peek() {
match next_char_in_float { match next_char_in_float {
'0'..='9' | '_' => { '0'..='9' | '_' => {
result.push(next_char_in_float); result.push(next_char_in_float);
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
} }
_ => break, _ => break,
} }
@ -722,17 +682,13 @@ impl<'a> TokenIterator<'a> {
'x' | 'X' if c == '0' => { 'x' | 'X' if c == '0' => {
result.push(next_char); result.push(next_char);
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
while let Some(&next_char_in_hex) = self.char_stream.peek() { while let Some(&next_char_in_hex) = self.char_stream.peek() {
match next_char_in_hex { match next_char_in_hex {
'0'..='9' | 'a'..='f' | 'A'..='F' | '_' => { '0'..='9' | 'a'..='f' | 'A'..='F' | '_' => {
result.push(next_char_in_hex); result.push(next_char_in_hex);
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
} }
_ => break, _ => break,
} }
@ -742,17 +698,13 @@ impl<'a> TokenIterator<'a> {
'o' | 'O' if c == '0' => { 'o' | 'O' if c == '0' => {
result.push(next_char); result.push(next_char);
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
while let Some(&next_char_in_oct) = self.char_stream.peek() { while let Some(&next_char_in_oct) = self.char_stream.peek() {
match next_char_in_oct { match next_char_in_oct {
'0'..='8' | '_' => { '0'..='8' | '_' => {
result.push(next_char_in_oct); result.push(next_char_in_oct);
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
} }
_ => break, _ => break,
} }
@ -762,17 +714,13 @@ impl<'a> TokenIterator<'a> {
'b' | 'B' if c == '0' => { 'b' | 'B' if c == '0' => {
result.push(next_char); result.push(next_char);
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
while let Some(&next_char_in_binary) = self.char_stream.peek() { while let Some(&next_char_in_binary) = self.char_stream.peek() {
match next_char_in_binary { match next_char_in_binary {
'0' | '1' | '_' => { '0' | '1' | '_' => {
result.push(next_char_in_binary); result.push(next_char_in_binary);
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
} }
_ => break, _ => break,
} }
@ -818,9 +766,7 @@ impl<'a> TokenIterator<'a> {
x if x.is_ascii_alphanumeric() || x == '_' => { x if x.is_ascii_alphanumeric() || x == '_' => {
result.push(x); result.push(x);
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
} }
_ => break, _ => break,
} }
@ -884,9 +830,7 @@ impl<'a> TokenIterator<'a> {
match self.char_stream.peek() { match self.char_stream.peek() {
Some(&'=') => { Some(&'=') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Token::PlusAssign Token::PlusAssign
} }
_ if self.last.is_next_unary() => Token::UnaryPlus, _ if self.last.is_next_unary() => Token::UnaryPlus,
@ -900,9 +844,7 @@ impl<'a> TokenIterator<'a> {
match self.char_stream.peek() { match self.char_stream.peek() {
Some(&'=') => { Some(&'=') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Token::MinusAssign Token::MinusAssign
} }
_ if self.last.is_next_unary() => Token::UnaryMinus, _ if self.last.is_next_unary() => Token::UnaryMinus,
@ -916,9 +858,7 @@ impl<'a> TokenIterator<'a> {
match self.char_stream.peek() { match self.char_stream.peek() {
Some(&'=') => { Some(&'=') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Token::MultiplyAssign Token::MultiplyAssign
} }
_ => Token::Multiply, _ => Token::Multiply,
@ -929,58 +869,38 @@ impl<'a> TokenIterator<'a> {
'/' => match self.char_stream.peek() { '/' => match self.char_stream.peek() {
Some(&'/') => { Some(&'/') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
while let Some(c) = self.char_stream.next() { while let Some(c) = self.char_stream.next() {
match c { match c {
'\n' => { '\n' => {
if let Err(err) = self.new_line_token() { self.advance();
return Some(err);
}
break; break;
} }
_ => { _ => self.advance(),
if let Err(err) = self.advance_token() {
return Some(err);
}
}
} }
} }
} }
Some(&'*') => { Some(&'*') => {
let mut level = 1; let mut level = 1;
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
while let Some(c) = self.char_stream.next() { while let Some(c) = self.char_stream.next() {
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
match c { match c {
'/' => { '/' => {
if let Some('*') = self.char_stream.next() { if let Some('*') = self.char_stream.next() {
level += 1; level += 1;
} }
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
} }
'*' => { '*' => {
if let Some('/') = self.char_stream.next() { if let Some('/') = self.char_stream.next() {
level -= 1; level -= 1;
} }
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
}
'\n' => {
if let Err(err) = self.new_line_token() {
return Some(err);
}
} }
'\n' => self.advance(),
_ => (), _ => (),
} }
@ -991,9 +911,7 @@ impl<'a> TokenIterator<'a> {
} }
Some(&'=') => { Some(&'=') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
return Some((Token::DivideAssign, pos)); return Some((Token::DivideAssign, pos));
} }
_ => return Some((Token::Divide, pos)), _ => return Some((Token::Divide, pos)),
@ -1005,9 +923,7 @@ impl<'a> TokenIterator<'a> {
'=' => match self.char_stream.peek() { '=' => match self.char_stream.peek() {
Some(&'=') => { Some(&'=') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
return Some((Token::EqualsTo, pos)); return Some((Token::EqualsTo, pos));
} }
_ => return Some((Token::Equals, pos)), _ => return Some((Token::Equals, pos)),
@ -1015,29 +931,21 @@ impl<'a> TokenIterator<'a> {
'<' => match self.char_stream.peek() { '<' => match self.char_stream.peek() {
Some(&'=') => { Some(&'=') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
return Some((Token::LessThanEqualsTo, pos)); return Some((Token::LessThanEqualsTo, pos));
} }
Some(&'<') => { Some(&'<') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
return match self.char_stream.peek() { return match self.char_stream.peek() {
Some(&'=') => { Some(&'=') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Some((Token::LeftShiftAssign, pos)) Some((Token::LeftShiftAssign, pos))
} }
_ => { _ => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Some((Token::LeftShift, pos)) Some((Token::LeftShift, pos))
} }
}; };
@ -1049,29 +957,21 @@ impl<'a> TokenIterator<'a> {
match self.char_stream.peek() { match self.char_stream.peek() {
Some(&'=') => { Some(&'=') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Token::GreaterThanEqualsTo Token::GreaterThanEqualsTo
} }
Some(&'>') => { Some(&'>') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
match self.char_stream.peek() { match self.char_stream.peek() {
Some(&'=') => { Some(&'=') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Token::RightShiftAssign Token::RightShiftAssign
} }
_ => { _ => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Token::RightShift Token::RightShift
} }
} }
@ -1086,9 +986,7 @@ impl<'a> TokenIterator<'a> {
match self.char_stream.peek() { match self.char_stream.peek() {
Some(&'=') => { Some(&'=') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Token::NotEqualsTo Token::NotEqualsTo
} }
_ => Token::Bang, _ => Token::Bang,
@ -1101,16 +999,12 @@ impl<'a> TokenIterator<'a> {
match self.char_stream.peek() { match self.char_stream.peek() {
Some(&'|') => { Some(&'|') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Token::Or Token::Or
} }
Some(&'=') => { Some(&'=') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Token::OrAssign Token::OrAssign
} }
_ => Token::Pipe, _ => Token::Pipe,
@ -1123,16 +1017,12 @@ impl<'a> TokenIterator<'a> {
match self.char_stream.peek() { match self.char_stream.peek() {
Some(&'&') => { Some(&'&') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Token::And Token::And
} }
Some(&'=') => { Some(&'=') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Token::AndAssign Token::AndAssign
} }
_ => Token::Ampersand, _ => Token::Ampersand,
@ -1145,9 +1035,7 @@ impl<'a> TokenIterator<'a> {
match self.char_stream.peek() { match self.char_stream.peek() {
Some(&'=') => { Some(&'=') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Token::XOrAssign Token::XOrAssign
} }
_ => Token::XOr, _ => Token::XOr,
@ -1160,9 +1048,7 @@ impl<'a> TokenIterator<'a> {
match self.char_stream.peek() { match self.char_stream.peek() {
Some(&'=') => { Some(&'=') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Token::ModuloAssign Token::ModuloAssign
} }
_ => Token::Modulo, _ => Token::Modulo,
@ -1175,9 +1061,7 @@ impl<'a> TokenIterator<'a> {
match self.char_stream.peek() { match self.char_stream.peek() {
Some(&'=') => { Some(&'=') => {
self.char_stream.next(); self.char_stream.next();
if let Err(err) = self.advance_token() { self.advance();
return Some(err);
}
Token::PowerOfAssign Token::PowerOfAssign
} }
_ => Token::PowerOf, _ => Token::PowerOf,