Merge all MissingXXX errors into MissingToken.

This commit is contained in:
Stephen Chung 2020-03-29 13:44:27 +08:00
parent ef6dd9414a
commit a8a4ed2967
3 changed files with 84 additions and 84 deletions

View File

@ -47,19 +47,8 @@ pub enum ParseErrorType {
UnexpectedEOF, UnexpectedEOF,
/// An unknown operator is encountered. Wrapped value is the operator. /// An unknown operator is encountered. Wrapped value is the operator.
UnknownOperator(String), UnknownOperator(String),
/// An open `(` is missing the corresponding closing `)`. /// Expecting a particular token but not finding one. Wrapped values are the token and usage.
MissingRightParen(String), MissingToken(String, String),
/// Expecting `(` but not finding one.
MissingLeftBrace,
/// An open `{` is missing the corresponding closing `}`.
MissingRightBrace(String),
/// An open `[` is missing the corresponding closing `]`.
#[cfg(not(feature = "no_index"))]
MissingRightBracket(String),
/// A list of expressions is missing the separating ','.
MissingComma(String),
/// A statement is missing the ending ';'.
MissingSemicolon(String),
/// An expression in function call arguments `()` has syntax error. /// An expression in function call arguments `()` has syntax error.
MalformedCallExpr(String), MalformedCallExpr(String),
/// An expression in indexing brackets `[]` has syntax error. /// An expression in indexing brackets `[]` has syntax error.
@ -71,8 +60,6 @@ pub enum ParseErrorType {
VariableExpected, VariableExpected,
/// Missing an expression. /// Missing an expression.
ExprExpected(String), ExprExpected(String),
/// A `for` statement is missing the `in` keyword.
MissingIn,
/// Defining a function `fn` in an appropriate place (e.g. inside another function). /// Defining a function `fn` in an appropriate place (e.g. inside another function).
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
WrongFnDefinition, WrongFnDefinition,
@ -84,7 +71,7 @@ pub enum ParseErrorType {
FnMissingParams(String), FnMissingParams(String),
/// A function definition has duplicated parameters. Wrapped values are the function name and parameter name. /// A function definition has duplicated parameters. Wrapped values are the function name and parameter name.
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
FnDuplicateParam(String, String), FnDuplicatedParam(String, String),
/// A function definition is missing the body. Wrapped value is the function name. /// A function definition is missing the body. Wrapped value is the function name.
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
FnMissingBody(String), FnMissingBody(String),
@ -130,18 +117,11 @@ impl ParseError {
ParseErrorType::BadInput(ref p) => p, ParseErrorType::BadInput(ref p) => p,
ParseErrorType::UnexpectedEOF => "Script is incomplete", ParseErrorType::UnexpectedEOF => "Script is incomplete",
ParseErrorType::UnknownOperator(_) => "Unknown operator", ParseErrorType::UnknownOperator(_) => "Unknown operator",
ParseErrorType::MissingRightParen(_) => "Expecting ')'", ParseErrorType::MissingToken(_, _) => "Expecting a certain token that is missing",
ParseErrorType::MissingLeftBrace => "Expecting '{'",
ParseErrorType::MissingRightBrace(_) => "Expecting '}'",
#[cfg(not(feature = "no_index"))]
ParseErrorType::MissingRightBracket(_) => "Expecting ']'",
ParseErrorType::MissingComma(_) => "Expecting ','",
ParseErrorType::MissingSemicolon(_) => "Expecting ';'",
ParseErrorType::MalformedCallExpr(_) => "Invalid expression in function call arguments", ParseErrorType::MalformedCallExpr(_) => "Invalid expression in function call arguments",
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
ParseErrorType::MalformedIndexExpr(_) => "Invalid index in indexing expression", ParseErrorType::MalformedIndexExpr(_) => "Invalid index in indexing expression",
ParseErrorType::ForbiddenConstantExpr(_) => "Expecting a constant", ParseErrorType::ForbiddenConstantExpr(_) => "Expecting a constant",
ParseErrorType::MissingIn => "Expecting 'in'",
ParseErrorType::VariableExpected => "Expecting name of a variable", ParseErrorType::VariableExpected => "Expecting name of a variable",
ParseErrorType::ExprExpected(_) => "Expecting an expression", ParseErrorType::ExprExpected(_) => "Expecting an expression",
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
@ -149,7 +129,7 @@ impl ParseError {
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
ParseErrorType::FnMissingParams(_) => "Expecting parameters in function declaration", ParseErrorType::FnMissingParams(_) => "Expecting parameters in function declaration",
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
ParseErrorType::FnDuplicateParam(_,_) => "Duplicated parameters in function declaration", ParseErrorType::FnDuplicatedParam(_,_) => "Duplicated parameters in function declaration",
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
ParseErrorType::FnMissingBody(_) => "Expecting body statement block for function declaration", ParseErrorType::FnMissingBody(_) => "Expecting body statement block for function declaration",
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
@ -193,19 +173,12 @@ impl fmt::Display for ParseError {
} }
#[cfg(not(feature = "no_function"))] #[cfg(not(feature = "no_function"))]
ParseErrorType::FnDuplicateParam(ref s, ref arg) => { ParseErrorType::FnDuplicatedParam(ref s, ref arg) => {
write!(f, "Duplicated parameter '{}' for function '{}'", arg, s)? write!(f, "Duplicated parameter '{}' for function '{}'", arg, s)?
} }
ParseErrorType::MissingRightParen(ref s) | ParseErrorType::MissingRightBrace(ref s) => { ParseErrorType::MissingToken(ref token, ref s) => {
write!(f, "{} for {}", self.desc(), s)? write!(f, "Expecting '{}' {}", token, s)?
}
#[cfg(not(feature = "no_index"))]
ParseErrorType::MissingRightBracket(ref s) => write!(f, "{} for {}", self.desc(), s)?,
ParseErrorType::MissingSemicolon(ref s) | ParseErrorType::MissingComma(ref s) => {
write!(f, "{} for {}", self.desc(), s)?
} }
ParseErrorType::AssignmentToConstant(ref s) if s.is_empty() => { ParseErrorType::AssignmentToConstant(ref s) if s.is_empty() => {

View File

@ -1370,13 +1370,16 @@ fn parse_paren_expr<'a>(
// ( xxx ) // ( xxx )
Some((Token::RightParen, _)) => Ok(expr), Some((Token::RightParen, _)) => Ok(expr),
// ( xxx ??? // ( xxx ???
Some((_, pos)) => { Some((_, pos)) => Err(PERR::MissingToken(
Err(PERR::MissingRightParen("a matching ( in the expression".into()).into_err(pos)) ")".into(),
} "for a matching ( in this expression".into(),
)
.into_err(pos)),
// ( xxx // ( xxx
None => { None => Err(
Err(PERR::MissingRightParen("a matching ( in the expression".into()).into_err_eof()) PERR::MissingToken(")".into(), "for a matching ( in this expression".into())
} .into_err_eof(),
),
} }
} }
@ -1391,10 +1394,10 @@ fn parse_call_expr<'a>(
// id() // id()
if let (Token::RightParen, _) = input.peek().ok_or_else(|| { if let (Token::RightParen, _) = input.peek().ok_or_else(|| {
PERR::MissingRightParen(format!( PERR::MissingToken(
"closing the arguments to call of function '{}'", ")".into(),
id format!("to close the arguments list of this function call '{}'", id),
)) )
.into_err_eof() .into_err_eof()
})? { })? {
input.next(); input.next();
@ -1405,10 +1408,10 @@ fn parse_call_expr<'a>(
args_expr_list.push(parse_expr(input, allow_stmt_expr)?); args_expr_list.push(parse_expr(input, allow_stmt_expr)?);
match input.peek().ok_or_else(|| { match input.peek().ok_or_else(|| {
PERR::MissingRightParen(format!( PERR::MissingToken(
"closing the arguments to call of function '{}'", ")".into(),
id format!("to close the arguments list of this function call '{}'", id),
)) )
.into_err_eof() .into_err_eof()
})? { })? {
(Token::RightParen, _) => { (Token::RightParen, _) => {
@ -1417,10 +1420,10 @@ fn parse_call_expr<'a>(
} }
(Token::Comma, _) => (), (Token::Comma, _) => (),
(_, pos) => { (_, pos) => {
return Err(PERR::MissingComma(format!( return Err(PERR::MissingToken(
"separating the arguments to call of function '{}'", ",".into(),
id format!("to separate the arguments to function call '{}'", id),
)) )
.into_err(*pos)) .into_err(*pos))
} }
} }
@ -1497,15 +1500,22 @@ fn parse_index_expr<'a>(
} }
// Check if there is a closing bracket // Check if there is a closing bracket
match input match input.peek().ok_or_else(|| {
.peek() PERR::MissingToken(
.ok_or_else(|| PERR::MissingRightBracket("index expression".into()).into_err_eof())? "]".into(),
{ "for a matching [ in this index expression".into(),
)
.into_err_eof()
})? {
(Token::RightBracket, _) => { (Token::RightBracket, _) => {
input.next(); input.next();
Ok(Expr::Index(lhs, Box::new(idx_expr), pos)) Ok(Expr::Index(lhs, Box::new(idx_expr), pos))
} }
(_, pos) => Err(PERR::MissingRightBracket("index expression".into()).into_err(*pos)), (_, pos) => Err(PERR::MissingToken(
"]".into(),
"for a matching [ in this index expression".into(),
)
.into_err(*pos)),
} }
} }
@ -1555,31 +1565,32 @@ fn parse_array_literal<'a>(
arr.push(parse_expr(input, allow_stmt_expr)?); arr.push(parse_expr(input, allow_stmt_expr)?);
match input.peek().ok_or_else(|| { match input.peek().ok_or_else(|| {
PERR::MissingRightBracket("separating items in array literal".into()).into_err_eof() PERR::MissingToken("]".into(), "to end this array literal".into()).into_err_eof()
})? { })? {
(Token::Comma, _) => { (Token::Comma, _) => {
input.next(); input.next();
} }
(Token::RightBracket, _) => break, (Token::RightBracket, _) => break,
(_, pos) => { (_, pos) => {
return Err( return Err(PERR::MissingToken(
PERR::MissingComma("separating items in array literal".into()) ",".into(),
.into_err(*pos), "separate the item of this array literal".into(),
) )
.into_err(*pos))
} }
} }
} }
} }
match input.peek().ok_or_else(|| { match input.peek().ok_or_else(|| {
PERR::MissingRightBracket("the end of array literal".into()).into_err_eof() PERR::MissingToken("]".into(), "to end this array literal".into()).into_err_eof()
})? { })? {
(Token::RightBracket, _) => { (Token::RightBracket, _) => {
input.next(); input.next();
Ok(Expr::Array(arr, begin)) Ok(Expr::Array(arr, begin))
} }
(_, pos) => { (_, pos) => {
Err(PERR::MissingRightBracket("the end of array literal".into()).into_err(*pos)) Err(PERR::MissingToken("]".into(), "to end this array literal".into()).into_err(*pos))
} }
} }
} }
@ -2070,9 +2081,12 @@ fn parse_for<'a>(
}; };
// for name in ... // for name in ...
match input.next().ok_or_else(|| PERR::MissingIn.into_err_eof())? { match input
.next()
.ok_or_else(|| PERR::MissingToken("in".into(), "here".into()).into_err_eof())?
{
(Token::In, _) => (), (Token::In, _) => (),
(_, pos) => return Err(PERR::MissingIn.into_err(pos)), (_, pos) => return Err(PERR::MissingToken("in".into(), "here".into()).into_err(pos)),
} }
// for name in expr { body } // for name in expr { body }
@ -2136,10 +2150,14 @@ fn parse_block<'a>(
// Must start with { // Must start with {
let pos = match input let pos = match input
.next() .next()
.ok_or_else(|| PERR::MissingLeftBrace.into_err_eof())? .ok_or_else(|| PERR::UnexpectedEOF.into_err_eof())?
{ {
(Token::LeftBrace, pos) => pos, (Token::LeftBrace, pos) => pos,
(_, pos) => return Err(PERR::MissingLeftBrace.into_err(pos)), (_, pos) => {
return Err(
PERR::MissingToken("{".into(), "to start a statement block".into()).into_err(pos),
)
}
}; };
let mut statements = Vec::new(); let mut statements = Vec::new();
@ -2169,20 +2187,24 @@ fn parse_block<'a>(
// { ... stmt ??? - error // { ... stmt ??? - error
Some((_, pos)) => { Some((_, pos)) => {
// Semicolons are not optional between statements // Semicolons are not optional between statements
return Err(PERR::MissingSemicolon("terminating a statement".into()).into_err(*pos)); return Err(
PERR::MissingToken(";".into(), "to terminate this statement".into())
.into_err(*pos),
);
} }
} }
} }
match input match input.peek().ok_or_else(|| {
.peek() PERR::MissingToken("}".into(), "to end this statement block".into()).into_err_eof()
.ok_or_else(|| PERR::MissingRightBrace("end of block".into()).into_err_eof())? })? {
{
(Token::RightBrace, _) => { (Token::RightBrace, _) => {
input.next(); input.next();
Ok(Stmt::Block(statements, pos)) Ok(Stmt::Block(statements, pos))
} }
(_, pos) => Err(PERR::MissingRightBrace("end of block".into()).into_err(*pos)), (_, pos) => {
Err(PERR::MissingToken("}".into(), "to end this statement block".into()).into_err(*pos))
}
} }
} }
@ -2284,28 +2306,30 @@ fn parse_fn<'a>(
if matches!(input.peek(), Some((Token::RightParen, _))) { if matches!(input.peek(), Some((Token::RightParen, _))) {
input.next(); input.next();
} else { } else {
let end_err = format!("closing the parameters list of function '{}'", name); let end_err = format!("to close the parameters list of function '{}'", name);
let sep_err = format!("separating the parameters of function '{}'", name); let sep_err = format!("to separate the parameters of function '{}'", name);
loop { loop {
match input match input
.next() .next()
.ok_or_else(|| PERR::MissingRightParen(end_err.to_string()).into_err_eof())? .ok_or_else(|| PERR::MissingToken(")".into(), end_err.to_string()).into_err_eof())?
{ {
(Token::Identifier(s), pos) => { (Token::Identifier(s), pos) => {
params.push((s, pos)); params.push((s, pos));
} }
(_, pos) => return Err(PERR::MissingRightParen(end_err).into_err(pos)), (_, pos) => return Err(PERR::MissingToken(")".into(), end_err).into_err(pos)),
} }
match input match input
.next() .next()
.ok_or_else(|| PERR::MissingRightParen(end_err.to_string()).into_err_eof())? .ok_or_else(|| PERR::MissingToken(")".into(), end_err.to_string()).into_err_eof())?
{ {
(Token::RightParen, _) => break, (Token::RightParen, _) => break,
(Token::Comma, _) => (), (Token::Comma, _) => (),
(Token::Identifier(_), _) => return Err(PERR::MissingComma(sep_err).into_err(pos)), (Token::Identifier(_), pos) => {
(_, pos) => return Err(PERR::MissingRightParen(sep_err).into_err(pos)), return Err(PERR::MissingToken(",".into(), sep_err).into_err(pos))
}
(_, pos) => return Err(PERR::MissingToken(",".into(), sep_err).into_err(pos)),
} }
} }
} }
@ -2322,7 +2346,7 @@ fn parse_fn<'a>(
.map_or_else(|| Ok(()), |(p2, pos)| Err((p2, *pos))) .map_or_else(|| Ok(()), |(p2, pos)| Err((p2, *pos)))
}) })
.map_err(|(p, pos)| { .map_err(|(p, pos)| {
PERR::FnDuplicateParam(name.to_string(), p.to_string()).into_err(pos) PERR::FnDuplicatedParam(name.to_string(), p.to_string()).into_err(pos)
})?; })?;
// Parse function body // Parse function body
@ -2408,7 +2432,10 @@ fn parse_global_level<'a>(
// stmt ??? - error // stmt ??? - error
Some((_, pos)) => { Some((_, pos)) => {
// Semicolons are not optional between statements // Semicolons are not optional between statements
return Err(PERR::MissingSemicolon("terminating a statement".into()).into_err(*pos)); return Err(
PERR::MissingToken(";".into(), "to terminate this statement".into())
.into_err(*pos),
);
} }
} }
} }

View File

@ -11,7 +11,7 @@ fn test_fn() -> Result<(), EvalAltResult> {
.expect_err("should be error") .expect_err("should be error")
.error_type() .error_type()
{ {
ParseErrorType::FnDuplicateParam(f, p) if f == "hello" && p == "x" => (), ParseErrorType::FnDuplicatedParam(f, p) if f == "hello" && p == "x" => (),
_ => assert!(false, "wrong error"), _ => assert!(false, "wrong error"),
} }