Make parse call arguments uniform.

This commit is contained in:
Stephen Chung 2022-12-29 12:18:41 +08:00
parent 00c434eb71
commit 858a6ad588
2 changed files with 35 additions and 38 deletions

View File

@ -129,9 +129,9 @@ impl Engine {
let OpAssignment { let OpAssignment {
hash_op_assign, hash_op_assign,
hash_op, hash_op,
op_assign: op_assign_token, op_assign,
op: op_token, op,
pos: op_pos, pos,
} = op_info; } = op_info;
let mut lock_guard = target.write_lock::<Dynamic>().unwrap(); let mut lock_guard = target.write_lock::<Dynamic>().unwrap();
@ -141,15 +141,15 @@ impl Engine {
if self.fast_operators() { if self.fast_operators() {
if let Some((func, need_context)) = 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 // Built-in found
let op = op_assign_token.literal_syntax();
auto_restore! { let orig_level = global.level; global.level += 1 } auto_restore! { let orig_level = global.level; global.level += 1 }
let context = if need_context { let context = if need_context {
let op = op_assign.literal_syntax();
let source = global.source(); let source = global.source();
Some((self, op, source, &*global, *op_pos).into()) Some((self, op, source, &*global, *pos).into())
} else { } else {
None None
}; };
@ -157,23 +157,20 @@ impl Engine {
} }
} }
let op_assign = op_assign_token.literal_syntax(); let token = Some(op_assign.clone());
let op = op_token.literal_syntax(); let op_assign = op_assign.literal_syntax();
let token = Some(op_assign_token.clone());
match self match self.exec_native_fn_call(global, caches, op_assign, token, hash, args, true, *pos)
.exec_native_fn_call(global, caches, op_assign, token, hash, args, true, *op_pos)
{ {
Ok(_) => (), Ok(_) => (),
Err(err) if matches!(*err, ERR::ErrorFunctionNotFound(ref f, ..) if f.starts_with(op_assign)) => Err(err) if matches!(*err, ERR::ErrorFunctionNotFound(ref f, ..) if f.starts_with(op_assign)) =>
{ {
// Expand to `var = var op rhs` // 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 *args[0] = self
.exec_native_fn_call( .exec_native_fn_call(global, caches, op, token, *hash_op, args, true, *pos)?
global, caches, op, token, *hash_op, args, true, *op_pos,
)?
.0; .0;
} }
Err(err) => return Err(err), Err(err) => return Err(err),

View File

@ -546,11 +546,11 @@ impl Engine {
input: &mut TokenStream, input: &mut TokenStream,
state: &mut ParseState, state: &mut ParseState,
lib: &mut FnLib, lib: &mut FnLib,
settings: ParseSettings,
id: ImmutableString, id: ImmutableString,
no_args: bool, no_args: bool,
capture_parent_scope: bool, capture_parent_scope: bool,
namespace: Namespace, namespace: Namespace,
settings: ParseSettings,
) -> ParseResult<Expr> { ) -> ParseResult<Expr> {
let (token, token_pos) = if no_args { let (token, token_pos) = if no_args {
&(Token::RightParen, Position::NONE) &(Token::RightParen, Position::NONE)
@ -743,10 +743,10 @@ impl Engine {
input: &mut TokenStream, input: &mut TokenStream,
state: &mut ParseState, state: &mut ParseState,
lib: &mut FnLib, lib: &mut FnLib,
settings: ParseSettings,
lhs: Expr, lhs: Expr,
options: ASTFlags, options: ASTFlags,
check_index_type: bool, check_index_type: bool,
settings: ParseSettings,
) -> ParseResult<Expr> { ) -> ParseResult<Expr> {
let mut settings = settings; let mut settings = settings;
@ -869,7 +869,7 @@ impl Engine {
_ => unreachable!("`[` or `?[`"), _ => unreachable!("`[` or `?[`"),
}; };
let idx_expr = self.parse_index_chain( 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 // Indexing binds to right
Ok(Expr::Index( Ok(Expr::Index(
@ -1307,8 +1307,8 @@ impl Engine {
input: &mut TokenStream, input: &mut TokenStream,
state: &mut ParseState, state: &mut ParseState,
lib: &mut FnLib, lib: &mut FnLib,
is_property: bool,
settings: ParseSettings, settings: ParseSettings,
is_property: bool,
) -> ParseResult<Expr> { ) -> ParseResult<Expr> {
let (token, token_pos) = input.peek().expect(NEVER_ENDS); let (token, token_pos) = input.peek().expect(NEVER_ENDS);
@ -1460,7 +1460,7 @@ impl Engine {
}; };
let result = 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 // Restore the strings interner by swapping it back
std::mem::swap(state.interned_strings, new_state.interned_strings); std::mem::swap(state.interned_strings, new_state.interned_strings);
@ -1696,7 +1696,7 @@ impl Engine {
return Ok(root_expr); 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. /// Tail processing of all possible postfix operators of a primary expression.
@ -1705,8 +1705,8 @@ impl Engine {
input: &mut TokenStream, input: &mut TokenStream,
state: &mut ParseState, state: &mut ParseState,
lib: &mut FnLib, lib: &mut FnLib,
mut lhs: Expr,
settings: ParseSettings, settings: ParseSettings,
mut lhs: Expr,
) -> ParseResult<Expr> { ) -> ParseResult<Expr> {
let mut settings = settings; let mut settings = settings;
@ -1753,14 +1753,14 @@ impl Engine {
let (.., ns, _, name) = *x; let (.., ns, _, name) = *x;
settings.pos = pos; 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 // Function call
(Expr::Variable(x, .., pos), t @ (Token::LeftParen | Token::Unit)) => { (Expr::Variable(x, .., pos), t @ (Token::LeftParen | Token::Unit)) => {
let (.., ns, _, name) = *x; let (.., ns, _, name) = *x;
let no_args = t == Token::Unit; let no_args = t == Token::Unit;
settings.pos = pos; 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 // module access
#[cfg(not(feature = "no_module"))] #[cfg(not(feature = "no_module"))]
@ -1786,7 +1786,7 @@ impl Engine {
_ => unreachable!("`[` or `?[`"), _ => unreachable!("`[` or `?[`"),
}; };
let settings = settings.level_up()?; 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 // Property access
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
@ -1807,7 +1807,7 @@ impl Engine {
(.., pos) => return Err(PERR::PropertyExpected.into_err(*pos)), (.., 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 { let op_flags = match op {
Token::Period => ASTFlags::NONE, Token::Period => ASTFlags::NONE,
Token::Elvis => ASTFlags::NEGATED, Token::Elvis => ASTFlags::NEGATED,
@ -1984,7 +1984,7 @@ impl Engine {
// <EOF> // <EOF>
Token::EOF => Err(PERR::UnexpectedEOF.into_err(settings.pos)), Token::EOF => Err(PERR::UnexpectedEOF.into_err(settings.pos)),
// All other tokens // 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, input: &mut TokenStream,
state: &mut ParseState, state: &mut ParseState,
lib: &mut FnLib, lib: &mut FnLib,
settings: ParseSettings,
parent_precedence: Option<Precedence>, parent_precedence: Option<Precedence>,
lhs: Expr, lhs: Expr,
settings: ParseSettings,
) -> ParseResult<Expr> { ) -> ParseResult<Expr> {
let mut settings = settings; let mut settings = settings;
settings.pos = lhs.position(); settings.pos = lhs.position();
@ -2294,7 +2294,7 @@ impl Engine {
// If same precedence, then check if the operator binds right // If same precedence, then check if the operator binds right
let rhs = let rhs =
if (precedence == next_precedence && bind_right) || precedence < next_precedence { 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 { } else {
// Otherwise bind to left (even if next operator has the same precedence) // Otherwise bind to left (even if next operator has the same precedence)
rhs rhs
@ -2629,7 +2629,7 @@ impl Engine {
let precedence = Precedence::new(1); let precedence = Precedence::new(1);
let settings = settings.level_up()?; let settings = settings.level_up()?;
let lhs = self.parse_unary(input, state, lib, settings)?; 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. /// Parse an if statement.
@ -2863,9 +2863,9 @@ impl Engine {
input: &mut TokenStream, input: &mut TokenStream,
state: &mut ParseState, state: &mut ParseState,
lib: &mut FnLib, lib: &mut FnLib,
settings: ParseSettings,
access: AccessMode, access: AccessMode,
is_export: bool, is_export: bool,
settings: ParseSettings,
) -> ParseResult<Stmt> { ) -> ParseResult<Stmt> {
// let/const... (specified in `var_type`) // let/const... (specified in `var_type`)
let mut settings = settings; let mut settings = settings;
@ -3016,7 +3016,7 @@ impl Engine {
let pos = *pos; let pos = *pos;
let settings = settings.level_up()?; let settings = settings.level_up()?;
let mut stmt = 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); stmt.set_position(pos);
return Ok(stmt); return Ok(stmt);
} }
@ -3024,7 +3024,7 @@ impl Engine {
let pos = *pos; let pos = *pos;
let settings = settings.level_up()?; let settings = settings.level_up()?;
let mut stmt = 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); stmt.set_position(pos);
return Ok(stmt); return Ok(stmt);
} }
@ -3344,8 +3344,8 @@ impl Engine {
input, input,
new_state, new_state,
lib, lib,
access,
new_settings, new_settings,
access,
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
comments, comments,
)?; )?;
@ -3455,9 +3455,9 @@ impl Engine {
Token::Try => self.parse_try_catch(input, state, lib, settings.level_up()?), 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 => { 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"))] #[cfg(not(feature = "no_module"))]
@ -3555,8 +3555,8 @@ impl Engine {
input: &mut TokenStream, input: &mut TokenStream,
state: &mut ParseState, state: &mut ParseState,
lib: &mut FnLib, lib: &mut FnLib,
access: crate::FnAccess,
settings: ParseSettings, settings: ParseSettings,
access: crate::FnAccess,
#[cfg(feature = "metadata")] comments: impl IntoIterator<Item = Identifier>, #[cfg(feature = "metadata")] comments: impl IntoIterator<Item = Identifier>,
) -> ParseResult<ScriptFnDef> { ) -> ParseResult<ScriptFnDef> {
let settings = settings.level_up()?; let settings = settings.level_up()?;
@ -3712,9 +3712,9 @@ impl Engine {
&self, &self,
input: &mut TokenStream, input: &mut TokenStream,
state: &mut ParseState, state: &mut ParseState,
_parent: &mut ParseState,
lib: &mut FnLib, lib: &mut FnLib,
settings: ParseSettings, settings: ParseSettings,
_parent: &mut ParseState,
) -> ParseResult<(Expr, Shared<ScriptFnDef>)> { ) -> ParseResult<(Expr, Shared<ScriptFnDef>)> {
let settings = settings.level_up()?; let settings = settings.level_up()?;
let mut params_list = StaticVec::<ImmutableString>::new_const(); let mut params_list = StaticVec::<ImmutableString>::new_const();