From 858a6ad588aecda4461a3a6d3e6e092b62549e56 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Thu, 29 Dec 2022 12:18:41 +0800 Subject: [PATCH] Make parse call arguments uniform. --- src/eval/stmt.rs | 27 ++++++++++++--------------- src/parser.rs | 46 +++++++++++++++++++++++----------------------- 2 files changed, 35 insertions(+), 38 deletions(-) diff --git a/src/eval/stmt.rs b/src/eval/stmt.rs index 8bb47110..b5e60cce 100644 --- a/src/eval/stmt.rs +++ b/src/eval/stmt.rs @@ -129,9 +129,9 @@ impl Engine { let OpAssignment { hash_op_assign, hash_op, - op_assign: op_assign_token, - op: op_token, - pos: op_pos, + op_assign, + op, + pos, } = op_info; let mut lock_guard = target.write_lock::().unwrap(); @@ -141,15 +141,15 @@ impl Engine { if self.fast_operators() { if let Some((func, need_context)) = - get_builtin_op_assignment_fn(op_assign_token.clone(), args[0], args[1]) + get_builtin_op_assignment_fn(op_assign.clone(), args[0], args[1]) { // Built-in found - let op = op_assign_token.literal_syntax(); auto_restore! { let orig_level = global.level; global.level += 1 } let context = if need_context { + let op = op_assign.literal_syntax(); let source = global.source(); - Some((self, op, source, &*global, *op_pos).into()) + Some((self, op, source, &*global, *pos).into()) } else { None }; @@ -157,23 +157,20 @@ impl Engine { } } - let op_assign = op_assign_token.literal_syntax(); - let op = op_token.literal_syntax(); - let token = Some(op_assign_token.clone()); + let token = Some(op_assign.clone()); + let op_assign = op_assign.literal_syntax(); - match self - .exec_native_fn_call(global, caches, op_assign, token, hash, args, true, *op_pos) + match self.exec_native_fn_call(global, caches, op_assign, token, hash, args, true, *pos) { Ok(_) => (), Err(err) if matches!(*err, ERR::ErrorFunctionNotFound(ref f, ..) if f.starts_with(op_assign)) => { // Expand to `var = var op rhs` - let token = Some(op_token.clone()); + let token = Some(op.clone()); + let op = op.literal_syntax(); *args[0] = self - .exec_native_fn_call( - global, caches, op, token, *hash_op, args, true, *op_pos, - )? + .exec_native_fn_call(global, caches, op, token, *hash_op, args, true, *pos)? .0; } Err(err) => return Err(err), diff --git a/src/parser.rs b/src/parser.rs index 39c51d67..5098e42d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -546,11 +546,11 @@ impl Engine { input: &mut TokenStream, state: &mut ParseState, lib: &mut FnLib, + settings: ParseSettings, id: ImmutableString, no_args: bool, capture_parent_scope: bool, namespace: Namespace, - settings: ParseSettings, ) -> ParseResult { let (token, token_pos) = if no_args { &(Token::RightParen, Position::NONE) @@ -743,10 +743,10 @@ impl Engine { input: &mut TokenStream, state: &mut ParseState, lib: &mut FnLib, + settings: ParseSettings, lhs: Expr, options: ASTFlags, check_index_type: bool, - settings: ParseSettings, ) -> ParseResult { let mut settings = settings; @@ -869,7 +869,7 @@ impl Engine { _ => unreachable!("`[` or `?[`"), }; let idx_expr = self.parse_index_chain( - input, state, lib, idx_expr, options, false, settings, + input, state, lib, settings, idx_expr, options, false, )?; // Indexing binds to right Ok(Expr::Index( @@ -1307,8 +1307,8 @@ impl Engine { input: &mut TokenStream, state: &mut ParseState, lib: &mut FnLib, - is_property: bool, settings: ParseSettings, + is_property: bool, ) -> ParseResult { let (token, token_pos) = input.peek().expect(NEVER_ENDS); @@ -1460,7 +1460,7 @@ impl Engine { }; let result = - self.parse_anon_fn(input, new_state, state, lib, new_settings.level_up()?); + self.parse_anon_fn(input, new_state, lib, new_settings.level_up()?, state); // Restore the strings interner by swapping it back std::mem::swap(state.interned_strings, new_state.interned_strings); @@ -1696,7 +1696,7 @@ impl Engine { return Ok(root_expr); } - self.parse_postfix(input, state, lib, root_expr, settings) + self.parse_postfix(input, state, lib, settings, root_expr) } /// Tail processing of all possible postfix operators of a primary expression. @@ -1705,8 +1705,8 @@ impl Engine { input: &mut TokenStream, state: &mut ParseState, lib: &mut FnLib, - mut lhs: Expr, settings: ParseSettings, + mut lhs: Expr, ) -> ParseResult { let mut settings = settings; @@ -1753,14 +1753,14 @@ impl Engine { let (.., ns, _, name) = *x; settings.pos = pos; - self.parse_fn_call(input, state, lib, name, no_args, true, ns, settings)? + self.parse_fn_call(input, state, lib, settings, name, no_args, true, ns)? } // Function call (Expr::Variable(x, .., pos), t @ (Token::LeftParen | Token::Unit)) => { let (.., ns, _, name) = *x; let no_args = t == Token::Unit; settings.pos = pos; - self.parse_fn_call(input, state, lib, name, no_args, false, ns, settings)? + self.parse_fn_call(input, state, lib, settings, name, no_args, false, ns)? } // module access #[cfg(not(feature = "no_module"))] @@ -1786,7 +1786,7 @@ impl Engine { _ => unreachable!("`[` or `?[`"), }; let settings = settings.level_up()?; - self.parse_index_chain(input, state, lib, expr, opt, true, settings)? + self.parse_index_chain(input, state, lib, settings, expr, opt, true)? } // Property access #[cfg(not(feature = "no_object"))] @@ -1807,7 +1807,7 @@ impl Engine { (.., pos) => return Err(PERR::PropertyExpected.into_err(*pos)), } - let rhs = self.parse_primary(input, state, lib, true, settings.level_up()?)?; + let rhs = self.parse_primary(input, state, lib, settings.level_up()?, true)?; let op_flags = match op { Token::Period => ASTFlags::NONE, Token::Elvis => ASTFlags::NEGATED, @@ -1984,7 +1984,7 @@ impl Engine { // Token::EOF => Err(PERR::UnexpectedEOF.into_err(settings.pos)), // All other tokens - _ => self.parse_primary(input, state, lib, false, settings), + _ => self.parse_primary(input, state, lib, settings, false), } } @@ -2234,9 +2234,9 @@ impl Engine { input: &mut TokenStream, state: &mut ParseState, lib: &mut FnLib, + settings: ParseSettings, parent_precedence: Option, lhs: Expr, - settings: ParseSettings, ) -> ParseResult { let mut settings = settings; settings.pos = lhs.position(); @@ -2294,7 +2294,7 @@ impl Engine { // If same precedence, then check if the operator binds right let rhs = if (precedence == next_precedence && bind_right) || precedence < next_precedence { - self.parse_binary_op(input, state, lib, precedence, rhs, settings)? + self.parse_binary_op(input, state, lib, settings, precedence, rhs)? } else { // Otherwise bind to left (even if next operator has the same precedence) rhs @@ -2629,7 +2629,7 @@ impl Engine { let precedence = Precedence::new(1); let settings = settings.level_up()?; let lhs = self.parse_unary(input, state, lib, settings)?; - self.parse_binary_op(input, state, lib, precedence, lhs, settings) + self.parse_binary_op(input, state, lib, settings, precedence, lhs) } /// Parse an if statement. @@ -2863,9 +2863,9 @@ impl Engine { input: &mut TokenStream, state: &mut ParseState, lib: &mut FnLib, + settings: ParseSettings, access: AccessMode, is_export: bool, - settings: ParseSettings, ) -> ParseResult { // let/const... (specified in `var_type`) let mut settings = settings; @@ -3016,7 +3016,7 @@ impl Engine { let pos = *pos; let settings = settings.level_up()?; let mut stmt = - self.parse_let(input, state, lib, AccessMode::ReadWrite, true, settings)?; + self.parse_let(input, state, lib, settings, AccessMode::ReadWrite, true)?; stmt.set_position(pos); return Ok(stmt); } @@ -3024,7 +3024,7 @@ impl Engine { let pos = *pos; let settings = settings.level_up()?; let mut stmt = - self.parse_let(input, state, lib, AccessMode::ReadOnly, true, settings)?; + self.parse_let(input, state, lib, settings, AccessMode::ReadOnly, true)?; stmt.set_position(pos); return Ok(stmt); } @@ -3344,8 +3344,8 @@ impl Engine { input, new_state, lib, - access, new_settings, + access, #[cfg(feature = "metadata")] comments, )?; @@ -3455,9 +3455,9 @@ impl Engine { Token::Try => self.parse_try_catch(input, state, lib, settings.level_up()?), - Token::Let => self.parse_let(input, state, lib, ReadWrite, false, settings.level_up()?), + Token::Let => self.parse_let(input, state, lib, settings.level_up()?, ReadWrite, false), Token::Const => { - self.parse_let(input, state, lib, ReadOnly, false, settings.level_up()?) + self.parse_let(input, state, lib, settings.level_up()?, ReadOnly, false) } #[cfg(not(feature = "no_module"))] @@ -3555,8 +3555,8 @@ impl Engine { input: &mut TokenStream, state: &mut ParseState, lib: &mut FnLib, - access: crate::FnAccess, settings: ParseSettings, + access: crate::FnAccess, #[cfg(feature = "metadata")] comments: impl IntoIterator, ) -> ParseResult { let settings = settings.level_up()?; @@ -3712,9 +3712,9 @@ impl Engine { &self, input: &mut TokenStream, state: &mut ParseState, - _parent: &mut ParseState, lib: &mut FnLib, settings: ParseSettings, + _parent: &mut ParseState, ) -> ParseResult<(Expr, Shared)> { let settings = settings.level_up()?; let mut params_list = StaticVec::::new_const();