Move custom syntax into separate function.

This commit is contained in:
Stephen Chung 2020-10-19 19:21:40 +08:00
parent a9fd0ff4de
commit ccba5f2188

View File

@ -7,7 +7,7 @@ use crate::fn_native::{FnPtr, Shared};
use crate::module::{Module, ModuleRef}; use crate::module::{Module, ModuleRef};
use crate::optimize::{optimize_into_ast, OptimizationLevel}; use crate::optimize::{optimize_into_ast, OptimizationLevel};
use crate::scope::{EntryType as ScopeEntryType, Scope}; use crate::scope::{EntryType as ScopeEntryType, Scope};
use crate::syntax::FnCustomSyntaxEval; use crate::syntax::{CustomSyntax, FnCustomSyntaxEval};
use crate::token::{is_keyword_function, is_valid_identifier, Position, Token, TokenStream}; use crate::token::{is_keyword_function, is_valid_identifier, Position, Token, TokenStream};
use crate::utils::StraightHasherBuilder; use crate::utils::StraightHasherBuilder;
use crate::{calc_fn_hash, StaticVec}; use crate::{calc_fn_hash, StaticVec};
@ -2654,30 +2654,16 @@ fn parse_binary_op(
} }
} }
/// Parse an expression. /// Parse a custom syntax.
fn parse_expr( fn parse_custom(
input: &mut TokenStream, input: &mut TokenStream,
state: &mut ParseState, state: &mut ParseState,
lib: &mut FunctionsLib, lib: &mut FunctionsLib,
mut settings: ParseSettings, mut settings: ParseSettings,
key: &str,
syntax: &CustomSyntax,
pos: Position,
) -> Result<Expr, ParseError> { ) -> Result<Expr, ParseError> {
settings.pos = input.peek().unwrap().1;
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
// Check if it is a custom syntax.
if let Some(ref custom) = state.engine.custom_syntax {
let (token, pos) = input.peek().unwrap();
let token_pos = *pos;
match token {
Token::Custom(key) if custom.contains_key(key) => {
let custom = custom.get_key_value(key).unwrap();
let (key, syntax) = custom;
input.next().unwrap();
let mut exprs: StaticVec<Expr> = Default::default(); let mut exprs: StaticVec<Expr> = Default::default();
// Adjust the variables stack // Adjust the variables stack
@ -2688,9 +2674,7 @@ fn parse_expr(
("".to_string(), ScopeEntryType::Normal), ("".to_string(), ScopeEntryType::Normal),
); );
} }
delta if delta < 0 && state.stack.len() <= delta.abs() as usize => { delta if delta < 0 && state.stack.len() <= delta.abs() as usize => state.stack.clear(),
state.stack.clear()
}
delta if delta < 0 => state delta if delta < 0 => state
.stack .stack
.truncate(state.stack.len() - delta.abs() as usize), .truncate(state.stack.len() - delta.abs() as usize),
@ -2732,10 +2716,35 @@ fn parse_expr(
} }
} }
return Ok(Expr::Custom(Box::new(( Ok(Expr::Custom(Box::new((
CustomExpr(exprs, syntax.func.clone()), CustomExpr(exprs, syntax.func.clone()),
token_pos, pos,
)))); ))))
}
/// Parse an expression.
fn parse_expr(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
) -> Result<Expr, ParseError> {
settings.pos = input.peek().unwrap().1;
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
// Check if it is a custom syntax.
if let Some(ref custom) = state.engine.custom_syntax {
let (token, pos) = input.peek().unwrap();
let token_pos = *pos;
match token {
Token::Custom(key) if custom.contains_key(key) => {
let custom = custom.get_key_value(key).unwrap();
let (key, syntax) = custom;
input.next().unwrap();
return parse_custom(input, state, lib, settings, key, syntax, token_pos);
} }
_ => (), _ => (),
} }