Fix arrayindexed property access.

This commit is contained in:
Stephen Chung 2020-03-07 20:55:03 +08:00
parent d055638e83
commit df6950f8f7
2 changed files with 29 additions and 6 deletions

View File

@ -155,10 +155,22 @@ impl Engine<'_> {
} else if let Some(val) = def_value { } else if let Some(val) = def_value {
// Return default value // Return default value
Ok(val.clone()) Ok(val.clone())
} else if spec.name.starts_with(FUNC_GETTER) || spec.name.starts_with(FUNC_SETTER) { } else if spec.name.starts_with(FUNC_GETTER) {
// Getter or setter // Getter
Err(EvalAltResult::ErrorDotExpr( 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, pos,
)) ))
} else { } else {

View File

@ -1275,6 +1275,7 @@ fn parse_unary<'a>(input: &mut Peekable<TokenIterator<'a>>) -> Result<Expr, Pars
} }
fn parse_assignment(lhs: Expr, rhs: Expr, pos: Position) -> Result<Expr, ParseError> { fn parse_assignment(lhs: Expr, rhs: Expr, pos: Position) -> Result<Expr, ParseError> {
//println!("{:?} = {:?}", lhs, rhs);
fn all_dots(expr: &Expr) -> (bool, Position) { fn all_dots(expr: &Expr) -> (bool, Position) {
match expr { match expr {
Expr::Identifier(_, pos) => (true, *pos), Expr::Identifier(_, pos) => (true, *pos),
@ -1294,9 +1295,19 @@ fn parse_assignment(lhs: Expr, rhs: Expr, pos: Position) -> Result<Expr, ParseEr
} }
_ => (), _ => (),
}, },
Expr::Dot(_, _, _) => match all_dots(&lhs) { Expr::Dot(dot_lhs, dot_rhs, _) => match dot_lhs.as_ref() {
(true, _) => return Ok(Expr::Assignment(Box::new(lhs), Box::new(rhs), pos)), Expr::Identifier(_, _) => match all_dots(&lhs) {
(false, pos) => return Err(ParseError::new(PERR::AssignmentToInvalidLHS, pos)), (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)),
},
_ => (),
},
_ => (),
}, },
_ => (), _ => (),
} }