From df6950f8f776d374574ca8045db6c09b77c2e18b Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Sat, 7 Mar 2020 20:55:03 +0800 Subject: [PATCH] Fix arrayindexed property access. --- src/engine.rs | 18 +++++++++++++++--- src/parser.rs | 17 ++++++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index bacd8982..b505950c 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -155,10 +155,22 @@ impl Engine<'_> { } else if let Some(val) = def_value { // Return default value Ok(val.clone()) - } else if spec.name.starts_with(FUNC_GETTER) || spec.name.starts_with(FUNC_SETTER) { - // Getter or setter + } else if spec.name.starts_with(FUNC_GETTER) { + // Getter Err(EvalAltResult::ErrorDotExpr( - "- invalid property access".to_string(), + format!( + "- unknown property '{}' or it is write-only", + &spec.name[FUNC_GETTER.len()..] + ), + pos, + )) + } else if spec.name.starts_with(FUNC_SETTER) { + // Setter + Err(EvalAltResult::ErrorDotExpr( + format!( + "- unknown property '{}' or it is read-only", + &spec.name[FUNC_SETTER.len()..] + ), pos, )) } else { diff --git a/src/parser.rs b/src/parser.rs index 5989b601..e14d081e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1275,6 +1275,7 @@ fn parse_unary<'a>(input: &mut Peekable>) -> Result Result { + //println!("{:?} = {:?}", lhs, rhs); fn all_dots(expr: &Expr) -> (bool, Position) { match expr { Expr::Identifier(_, pos) => (true, *pos), @@ -1294,9 +1295,19 @@ fn parse_assignment(lhs: Expr, rhs: Expr, pos: Position) -> Result (), }, - Expr::Dot(_, _, _) => match all_dots(&lhs) { - (true, _) => return Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)), - (false, pos) => return Err(ParseError::new(PERR::AssignmentToInvalidLHS, pos)), + Expr::Dot(dot_lhs, dot_rhs, _) => match dot_lhs.as_ref() { + Expr::Identifier(_, _) => match all_dots(&lhs) { + (true, _) => return Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)), + (false, pos) => return Err(ParseError::new(PERR::AssignmentToInvalidLHS, pos)), + }, + Expr::Index(idx_lhs, _, _) => match idx_lhs.as_ref() { + Expr::Identifier(_, _) => match all_dots(&dot_rhs) { + (true, _) => return Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)), + (false, pos) => return Err(ParseError::new(PERR::AssignmentToInvalidLHS, pos)), + }, + _ => (), + }, + _ => (), }, _ => (), }