Use bit-flags for options.
This commit is contained in:
parent
42d2718e24
commit
130b93d029
@ -201,11 +201,7 @@ impl Engine {
|
|||||||
scope: &Scope,
|
scope: &Scope,
|
||||||
scripts: impl AsRef<[S]>,
|
scripts: impl AsRef<[S]>,
|
||||||
) -> ParseResult<AST> {
|
) -> ParseResult<AST> {
|
||||||
self.compile_with_scope_and_optimization_level(
|
self.compile_with_scope_and_optimization_level(scope, scripts, self.optimization_level)
|
||||||
scope,
|
|
||||||
scripts,
|
|
||||||
self.options.optimization_level,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
/// Join a list of strings and compile into an [`AST`] using own scope at a specific optimization level.
|
/// Join a list of strings and compile into an [`AST`] using own scope at a specific optimization level.
|
||||||
///
|
///
|
||||||
@ -296,6 +292,6 @@ impl Engine {
|
|||||||
|
|
||||||
let mut peekable = stream.peekable();
|
let mut peekable = stream.peekable();
|
||||||
let mut state = ParseState::new(self, scope, tokenizer_control);
|
let mut state = ParseState::new(self, scope, tokenizer_control);
|
||||||
self.parse_global_expr(&mut peekable, &mut state, self.options.optimization_level)
|
self.parse_global_expr(&mut peekable, &mut state, self.optimization_level)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,7 @@ impl Engine {
|
|||||||
let ast = self.compile_with_scope_and_optimization_level(
|
let ast = self.compile_with_scope_and_optimization_level(
|
||||||
scope,
|
scope,
|
||||||
&[script],
|
&[script],
|
||||||
self.options.optimization_level,
|
self.optimization_level,
|
||||||
)?;
|
)?;
|
||||||
self.eval_ast_with_scope(scope, &ast)
|
self.eval_ast_with_scope(scope, &ast)
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ impl Engine {
|
|||||||
/// Not available under `no_optimize`.
|
/// Not available under `no_optimize`.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn set_optimization_level(&mut self, optimization_level: OptimizationLevel) -> &mut Self {
|
pub fn set_optimization_level(&mut self, optimization_level: OptimizationLevel) -> &mut Self {
|
||||||
self.options.optimization_level = optimization_level;
|
self.optimization_level = optimization_level;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ impl Engine {
|
|||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub const fn optimization_level(&self) -> OptimizationLevel {
|
pub const fn optimization_level(&self) -> OptimizationLevel {
|
||||||
self.options.optimization_level
|
self.optimization_level
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Optimize the [`AST`] with constants defined in an external Scope.
|
/// Optimize the [`AST`] with constants defined in an external Scope.
|
||||||
|
@ -1,62 +1,47 @@
|
|||||||
//! Settings for [`Engine`]'s language options.
|
//! Settings for [`Engine`]'s language options.
|
||||||
|
|
||||||
use crate::{Engine, OptimizationLevel};
|
use crate::Engine;
|
||||||
|
use bitflags::bitflags;
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
|
|
||||||
/// A type containing all language options for the [`Engine`].
|
bitflags! {
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
/// Bit-flags containing all language options for the [`Engine`].
|
||||||
pub struct LanguageOptions {
|
pub struct LangOptions: u8 {
|
||||||
/// Script optimization level.
|
/// Is `if`-expression allowed?
|
||||||
pub optimization_level: OptimizationLevel,
|
const IF_EXPR = 0b_00000001;
|
||||||
/// Is `if`-expression allowed?
|
/// Is `switch` expression allowed?
|
||||||
pub allow_if_expr: bool,
|
const SWITCH_EXPR = 0b_00000010;
|
||||||
/// Is `switch` expression allowed?
|
/// Is statement-expression allowed?
|
||||||
pub allow_switch_expr: bool,
|
const STMT_EXPR = 0b_00000100;
|
||||||
/// Is statement-expression allowed?
|
/// Is anonymous function allowed?
|
||||||
pub allow_stmt_expr: bool,
|
#[cfg(not(feature = "no_function"))]
|
||||||
/// Is anonymous function allowed?
|
const ANON_FN = 0b_00001000;
|
||||||
#[cfg(not(feature = "no_function"))]
|
/// Is looping allowed?
|
||||||
pub allow_anonymous_fn: bool,
|
const LOOPING = 0b_00010000;
|
||||||
/// Is looping allowed?
|
/// Is variables shadowing allowed?
|
||||||
pub allow_looping: bool,
|
const SHADOW = 0b_00100000;
|
||||||
/// Is variables shadowing allowed?
|
/// Strict variables mode?
|
||||||
pub allow_shadowing: bool,
|
const STRICT_VAR = 0b_01000000;
|
||||||
/// Strict variables mode?
|
/// Raise error if an object map property does not exist?
|
||||||
pub strict_var: bool,
|
/// Returns `()` if `false`.
|
||||||
/// Raise error if an object map property does not exist?
|
#[cfg(not(feature = "no_object"))]
|
||||||
/// Returns `()` if `false`.
|
const FAIL_ON_INVALID_MAP_PROPERTY = 0b_10000000;
|
||||||
#[cfg(not(feature = "no_object"))]
|
|
||||||
pub fail_on_invalid_map_property: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl LanguageOptions {
|
|
||||||
/// Create a new [`Options`] with default values.
|
|
||||||
#[inline(always)]
|
|
||||||
pub const fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
#[cfg(not(feature = "no_optimize"))]
|
|
||||||
optimization_level: OptimizationLevel::Simple,
|
|
||||||
#[cfg(feature = "no_optimize")]
|
|
||||||
optimization_level: (),
|
|
||||||
|
|
||||||
allow_if_expr: true,
|
|
||||||
allow_switch_expr: true,
|
|
||||||
allow_stmt_expr: true,
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
allow_anonymous_fn: true,
|
|
||||||
allow_looping: true,
|
|
||||||
strict_var: false,
|
|
||||||
allow_shadowing: true,
|
|
||||||
#[cfg(not(feature = "no_object"))]
|
|
||||||
fail_on_invalid_map_property: false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for LanguageOptions {
|
impl LangOptions {
|
||||||
fn default() -> Self {
|
/// Create a new [`Options`] with default values.
|
||||||
Self::new()
|
#[inline(always)]
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self::IF_EXPR | Self::SWITCH_EXPR | Self::STMT_EXPR | Self::LOOPING | Self::SHADOW | {
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
{
|
||||||
|
Self::ANON_FN
|
||||||
|
}
|
||||||
|
#[cfg(feature = "no_function")]
|
||||||
|
0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,34 +50,34 @@ impl Engine {
|
|||||||
/// Default is `true`.
|
/// Default is `true`.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub const fn allow_if_expression(&self) -> bool {
|
pub const fn allow_if_expression(&self) -> bool {
|
||||||
self.options.allow_if_expr
|
self.options.contains(LangOptions::IF_EXPR)
|
||||||
}
|
}
|
||||||
/// Set whether `if`-expression is allowed.
|
/// Set whether `if`-expression is allowed.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn set_allow_if_expression(&mut self, enable: bool) {
|
pub fn set_allow_if_expression(&mut self, enable: bool) {
|
||||||
self.options.allow_if_expr = enable;
|
self.options.set(LangOptions::IF_EXPR, enable)
|
||||||
}
|
}
|
||||||
/// Is `switch` expression allowed?
|
/// Is `switch` expression allowed?
|
||||||
/// Default is `true`.
|
/// Default is `true`.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub const fn allow_switch_expression(&self) -> bool {
|
pub const fn allow_switch_expression(&self) -> bool {
|
||||||
self.options.allow_switch_expr
|
self.options.contains(LangOptions::SWITCH_EXPR)
|
||||||
}
|
}
|
||||||
/// Set whether `switch` expression is allowed.
|
/// Set whether `switch` expression is allowed.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn set_allow_switch_expression(&mut self, enable: bool) {
|
pub fn set_allow_switch_expression(&mut self, enable: bool) {
|
||||||
self.options.allow_switch_expr = enable;
|
self.options.set(LangOptions::SWITCH_EXPR, enable);
|
||||||
}
|
}
|
||||||
/// Is statement-expression allowed?
|
/// Is statement-expression allowed?
|
||||||
/// Default is `true`.
|
/// Default is `true`.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub const fn allow_statement_expression(&self) -> bool {
|
pub const fn allow_statement_expression(&self) -> bool {
|
||||||
self.options.allow_stmt_expr
|
self.options.contains(LangOptions::STMT_EXPR)
|
||||||
}
|
}
|
||||||
/// Set whether statement-expression is allowed.
|
/// Set whether statement-expression is allowed.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn set_allow_statement_expression(&mut self, enable: bool) {
|
pub fn set_allow_statement_expression(&mut self, enable: bool) {
|
||||||
self.options.allow_stmt_expr = enable;
|
self.options.set(LangOptions::STMT_EXPR, enable);
|
||||||
}
|
}
|
||||||
/// Is anonymous function allowed?
|
/// Is anonymous function allowed?
|
||||||
/// Default is `true`.
|
/// Default is `true`.
|
||||||
@ -101,7 +86,7 @@ impl Engine {
|
|||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub const fn allow_anonymous_fn(&self) -> bool {
|
pub const fn allow_anonymous_fn(&self) -> bool {
|
||||||
self.options.allow_anonymous_fn
|
self.options.contains(LangOptions::ANON_FN)
|
||||||
}
|
}
|
||||||
/// Set whether anonymous function is allowed.
|
/// Set whether anonymous function is allowed.
|
||||||
///
|
///
|
||||||
@ -109,40 +94,40 @@ impl Engine {
|
|||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn set_allow_anonymous_fn(&mut self, enable: bool) {
|
pub fn set_allow_anonymous_fn(&mut self, enable: bool) {
|
||||||
self.options.allow_anonymous_fn = enable;
|
self.options.set(LangOptions::ANON_FN, enable);
|
||||||
}
|
}
|
||||||
/// Is looping allowed?
|
/// Is looping allowed?
|
||||||
/// Default is `true`.
|
/// Default is `true`.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub const fn allow_looping(&self) -> bool {
|
pub const fn allow_looping(&self) -> bool {
|
||||||
self.options.allow_looping
|
self.options.contains(LangOptions::LOOPING)
|
||||||
}
|
}
|
||||||
/// Set whether looping is allowed.
|
/// Set whether looping is allowed.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn set_allow_looping(&mut self, enable: bool) {
|
pub fn set_allow_looping(&mut self, enable: bool) {
|
||||||
self.options.allow_looping = enable;
|
self.options.set(LangOptions::LOOPING, enable);
|
||||||
}
|
}
|
||||||
/// Is variables shadowing allowed?
|
/// Is variables shadowing allowed?
|
||||||
/// Default is `true`.
|
/// Default is `true`.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub const fn allow_shadowing(&self) -> bool {
|
pub const fn allow_shadowing(&self) -> bool {
|
||||||
self.options.allow_shadowing
|
self.options.contains(LangOptions::SHADOW)
|
||||||
}
|
}
|
||||||
/// Set whether variables shadowing is allowed.
|
/// Set whether variables shadowing is allowed.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn set_allow_shadowing(&mut self, enable: bool) {
|
pub fn set_allow_shadowing(&mut self, enable: bool) {
|
||||||
self.options.allow_shadowing = enable;
|
self.options.set(LangOptions::SHADOW, enable);
|
||||||
}
|
}
|
||||||
/// Is strict variables mode enabled?
|
/// Is strict variables mode enabled?
|
||||||
/// Default is `false`.
|
/// Default is `false`.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub const fn strict_variables(&self) -> bool {
|
pub const fn strict_variables(&self) -> bool {
|
||||||
self.options.strict_var
|
self.options.contains(LangOptions::STRICT_VAR)
|
||||||
}
|
}
|
||||||
/// Set whether strict variables mode is enabled.
|
/// Set whether strict variables mode is enabled.
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn set_strict_variables(&mut self, enable: bool) {
|
pub fn set_strict_variables(&mut self, enable: bool) {
|
||||||
self.options.strict_var = enable;
|
self.options.set(LangOptions::STRICT_VAR, enable);
|
||||||
}
|
}
|
||||||
/// Raise error if an object map property does not exist?
|
/// Raise error if an object map property does not exist?
|
||||||
/// Default is `false`.
|
/// Default is `false`.
|
||||||
@ -151,7 +136,8 @@ impl Engine {
|
|||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub const fn fail_on_invalid_map_property(&self) -> bool {
|
pub const fn fail_on_invalid_map_property(&self) -> bool {
|
||||||
self.options.fail_on_invalid_map_property
|
self.options
|
||||||
|
.contains(LangOptions::FAIL_ON_INVALID_MAP_PROPERTY)
|
||||||
}
|
}
|
||||||
/// Set whether to raise error if an object map property does not exist.
|
/// Set whether to raise error if an object map property does not exist.
|
||||||
///
|
///
|
||||||
@ -159,6 +145,7 @@ impl Engine {
|
|||||||
#[cfg(not(feature = "no_object"))]
|
#[cfg(not(feature = "no_object"))]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn set_fail_on_invalid_map_property(&mut self, enable: bool) {
|
pub fn set_fail_on_invalid_map_property(&mut self, enable: bool) {
|
||||||
self.options.fail_on_invalid_map_property = enable;
|
self.options
|
||||||
|
.set(LangOptions::FAIL_ON_INVALID_MAP_PROPERTY, enable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,11 +26,7 @@ impl Engine {
|
|||||||
self.lex_raw(&scripts, self.token_mapper.as_ref().map(Box::as_ref));
|
self.lex_raw(&scripts, self.token_mapper.as_ref().map(Box::as_ref));
|
||||||
let mut state = ParseState::new(self, scope, tokenizer_control);
|
let mut state = ParseState::new(self, scope, tokenizer_control);
|
||||||
|
|
||||||
let ast = self.parse(
|
let ast = self.parse(&mut stream.peekable(), &mut state, self.optimization_level)?;
|
||||||
&mut stream.peekable(),
|
|
||||||
&mut state,
|
|
||||||
self.options.optimization_level,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
self.run_ast_with_scope(scope, &ast)
|
self.run_ast_with_scope(scope, &ast)
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ pub enum FnAccess {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
/// _(internals)_ A type that holds a configuration option with bit-flags.
|
/// _(internals)_ Bit-flags containing [`AST`][crate::AST] node configuration options.
|
||||||
/// Exported under the `internals` feature only.
|
/// Exported under the `internals` feature only.
|
||||||
pub struct ASTFlags: u8 {
|
pub struct ASTFlags: u8 {
|
||||||
/// No options for the [`AST`][crate::AST] node.
|
/// No options for the [`AST`][crate::AST] node.
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! Main module defining the script evaluation [`Engine`].
|
//! Main module defining the script evaluation [`Engine`].
|
||||||
|
|
||||||
use crate::api::custom_syntax::CustomSyntax;
|
use crate::api::custom_syntax::CustomSyntax;
|
||||||
use crate::api::options::LanguageOptions;
|
use crate::api::options::LangOptions;
|
||||||
use crate::func::native::{
|
use crate::func::native::{
|
||||||
OnDebugCallback, OnDefVarCallback, OnParseTokenCallback, OnPrintCallback, OnVarCallback,
|
OnDebugCallback, OnDefVarCallback, OnParseTokenCallback, OnPrintCallback, OnVarCallback,
|
||||||
};
|
};
|
||||||
@ -9,7 +9,8 @@ use crate::packages::{Package, StandardPackage};
|
|||||||
use crate::tokenizer::Token;
|
use crate::tokenizer::Token;
|
||||||
use crate::types::dynamic::Union;
|
use crate::types::dynamic::Union;
|
||||||
use crate::{
|
use crate::{
|
||||||
Dynamic, Identifier, ImmutableString, Module, Position, RhaiResult, Shared, StaticVec,
|
Dynamic, Identifier, ImmutableString, Module, OptimizationLevel, Position, RhaiResult, Shared,
|
||||||
|
StaticVec,
|
||||||
};
|
};
|
||||||
#[cfg(feature = "no_std")]
|
#[cfg(feature = "no_std")]
|
||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
@ -129,7 +130,10 @@ pub struct Engine {
|
|||||||
pub(crate) progress: Option<Box<crate::func::native::OnProgressCallback>>,
|
pub(crate) progress: Option<Box<crate::func::native::OnProgressCallback>>,
|
||||||
|
|
||||||
/// Language options.
|
/// Language options.
|
||||||
pub(crate) options: LanguageOptions,
|
pub(crate) options: LangOptions,
|
||||||
|
|
||||||
|
/// Script optimization level.
|
||||||
|
pub optimization_level: OptimizationLevel,
|
||||||
|
|
||||||
/// Max limits.
|
/// Max limits.
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
@ -274,7 +278,12 @@ impl Engine {
|
|||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
progress: None,
|
progress: None,
|
||||||
|
|
||||||
options: LanguageOptions::new(),
|
options: LangOptions::new(),
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_optimize"))]
|
||||||
|
optimization_level: OptimizationLevel::Simple,
|
||||||
|
#[cfg(feature = "no_optimize")]
|
||||||
|
optimization_level: (),
|
||||||
|
|
||||||
#[cfg(not(feature = "unchecked"))]
|
#[cfg(not(feature = "unchecked"))]
|
||||||
limits: crate::api::limits::Limits::new(),
|
limits: crate::api::limits::Limits::new(),
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use crate::api::custom_syntax::{markers::*, CustomSyntax};
|
use crate::api::custom_syntax::{markers::*, CustomSyntax};
|
||||||
use crate::api::events::VarDefInfo;
|
use crate::api::events::VarDefInfo;
|
||||||
use crate::api::options::LanguageOptions;
|
use crate::api::options::LangOptions;
|
||||||
use crate::ast::{
|
use crate::ast::{
|
||||||
ASTFlags, BinaryExpr, ConditionalStmtBlock, CustomExpr, Expr, FnCallExpr, FnCallHashes, Ident,
|
ASTFlags, BinaryExpr, ConditionalStmtBlock, CustomExpr, Expr, FnCallExpr, FnCallHashes, Ident,
|
||||||
OpAssignment, ScriptFnDef, Stmt, StmtBlock, StmtBlockContainer, SwitchCases, TryCatchBlock,
|
OpAssignment, ScriptFnDef, Stmt, StmtBlock, StmtBlockContainer, SwitchCases, TryCatchBlock,
|
||||||
@ -215,7 +215,7 @@ struct ParseSettings {
|
|||||||
/// Is the current position inside a loop?
|
/// Is the current position inside a loop?
|
||||||
is_breakable: bool,
|
is_breakable: bool,
|
||||||
/// Language options in effect (overrides Engine options).
|
/// Language options in effect (overrides Engine options).
|
||||||
options: LanguageOptions,
|
options: LangOptions,
|
||||||
/// Current expression nesting level.
|
/// Current expression nesting level.
|
||||||
level: usize,
|
level: usize,
|
||||||
/// Current position.
|
/// Current position.
|
||||||
@ -501,7 +501,10 @@ impl Engine {
|
|||||||
#[cfg(feature = "no_function")]
|
#[cfg(feature = "no_function")]
|
||||||
let relax = false;
|
let relax = false;
|
||||||
|
|
||||||
if !relax && settings.options.strict_var && index.is_none() {
|
if !relax
|
||||||
|
&& settings.options.contains(LangOptions::STRICT_VAR)
|
||||||
|
&& index.is_none()
|
||||||
|
{
|
||||||
return Err(PERR::ModuleUndefined(namespace.root().to_string())
|
return Err(PERR::ModuleUndefined(namespace.root().to_string())
|
||||||
.into_err(namespace.position()));
|
.into_err(namespace.position()));
|
||||||
}
|
}
|
||||||
@ -561,7 +564,10 @@ impl Engine {
|
|||||||
#[cfg(feature = "no_function")]
|
#[cfg(feature = "no_function")]
|
||||||
let relax = false;
|
let relax = false;
|
||||||
|
|
||||||
if !relax && settings.options.strict_var && index.is_none() {
|
if !relax
|
||||||
|
&& settings.options.contains(LangOptions::STRICT_VAR)
|
||||||
|
&& index.is_none()
|
||||||
|
{
|
||||||
return Err(PERR::ModuleUndefined(namespace.root().to_string())
|
return Err(PERR::ModuleUndefined(namespace.root().to_string())
|
||||||
.into_err(namespace.position()));
|
.into_err(namespace.position()));
|
||||||
}
|
}
|
||||||
@ -1234,7 +1240,7 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// { - block statement as expression
|
// { - block statement as expression
|
||||||
Token::LeftBrace if settings.options.allow_stmt_expr => {
|
Token::LeftBrace if settings.options.contains(LangOptions::STMT_EXPR) => {
|
||||||
match self.parse_block(input, state, lib, settings.level_up())? {
|
match self.parse_block(input, state, lib, settings.level_up())? {
|
||||||
block @ Stmt::Block(..) => Expr::Stmt(Box::new(block.into())),
|
block @ Stmt::Block(..) => Expr::Stmt(Box::new(block.into())),
|
||||||
stmt => unreachable!("Stmt::Block expected but gets {:?}", stmt),
|
stmt => unreachable!("Stmt::Block expected but gets {:?}", stmt),
|
||||||
@ -1245,19 +1251,21 @@ impl Engine {
|
|||||||
Token::LeftParen => self.parse_paren_expr(input, state, lib, settings.level_up())?,
|
Token::LeftParen => self.parse_paren_expr(input, state, lib, settings.level_up())?,
|
||||||
|
|
||||||
// If statement is allowed to act as expressions
|
// If statement is allowed to act as expressions
|
||||||
Token::If if settings.options.allow_if_expr => Expr::Stmt(Box::new(
|
Token::If if settings.options.contains(LangOptions::IF_EXPR) => Expr::Stmt(Box::new(
|
||||||
self.parse_if(input, state, lib, settings.level_up())?
|
self.parse_if(input, state, lib, settings.level_up())?
|
||||||
.into(),
|
.into(),
|
||||||
)),
|
)),
|
||||||
// Switch statement is allowed to act as expressions
|
// Switch statement is allowed to act as expressions
|
||||||
Token::Switch if settings.options.allow_switch_expr => Expr::Stmt(Box::new(
|
Token::Switch if settings.options.contains(LangOptions::SWITCH_EXPR) => {
|
||||||
self.parse_switch(input, state, lib, settings.level_up())?
|
Expr::Stmt(Box::new(
|
||||||
.into(),
|
self.parse_switch(input, state, lib, settings.level_up())?
|
||||||
)),
|
.into(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
// | ...
|
// | ...
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
Token::Pipe | Token::Or if settings.options.allow_anonymous_fn => {
|
Token::Pipe | Token::Or if settings.options.contains(LangOptions::ANON_FN) => {
|
||||||
let mut new_state =
|
let mut new_state =
|
||||||
ParseState::new(self, state.scope, state.tokenizer_control.clone());
|
ParseState::new(self, state.scope, state.tokenizer_control.clone());
|
||||||
|
|
||||||
@ -1266,6 +1274,17 @@ impl Engine {
|
|||||||
new_state.max_expr_depth = self.max_function_expr_depth();
|
new_state.max_expr_depth = self.max_function_expr_depth();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut options = self.options;
|
||||||
|
options.set(
|
||||||
|
LangOptions::STRICT_VAR,
|
||||||
|
if cfg!(feature = "no_closure") {
|
||||||
|
settings.options.contains(LangOptions::STRICT_VAR)
|
||||||
|
} else {
|
||||||
|
// A capturing closure can access variables not defined locally
|
||||||
|
false
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
let new_settings = ParseSettings {
|
let new_settings = ParseSettings {
|
||||||
is_global: false,
|
is_global: false,
|
||||||
is_function_scope: true,
|
is_function_scope: true,
|
||||||
@ -1273,15 +1292,7 @@ impl Engine {
|
|||||||
is_closure_scope: true,
|
is_closure_scope: true,
|
||||||
is_breakable: false,
|
is_breakable: false,
|
||||||
level: 0,
|
level: 0,
|
||||||
options: LanguageOptions {
|
options,
|
||||||
strict_var: if cfg!(feature = "no_closure") {
|
|
||||||
settings.options.strict_var
|
|
||||||
} else {
|
|
||||||
// A capturing closure can access variables not defined locally
|
|
||||||
false
|
|
||||||
},
|
|
||||||
..self.options
|
|
||||||
},
|
|
||||||
..settings
|
..settings
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1292,7 +1303,7 @@ impl Engine {
|
|||||||
|crate::ast::Ident { name, pos }| {
|
|crate::ast::Ident { name, pos }| {
|
||||||
let index = state.access_var(name, *pos);
|
let index = state.access_var(name, *pos);
|
||||||
|
|
||||||
if settings.options.strict_var
|
if settings.options.contains(LangOptions::STRICT_VAR)
|
||||||
&& !settings.is_closure_scope
|
&& !settings.is_closure_scope
|
||||||
&& index.is_none()
|
&& index.is_none()
|
||||||
&& !state.scope.contains(name)
|
&& !state.scope.contains(name)
|
||||||
@ -1439,7 +1450,7 @@ impl Engine {
|
|||||||
_ => {
|
_ => {
|
||||||
let index = state.access_var(&s, settings.pos);
|
let index = state.access_var(&s, settings.pos);
|
||||||
|
|
||||||
if settings.options.strict_var
|
if settings.options.contains(LangOptions::STRICT_VAR)
|
||||||
&& index.is_none()
|
&& index.is_none()
|
||||||
&& !state.scope.contains(&s)
|
&& !state.scope.contains(&s)
|
||||||
{
|
{
|
||||||
@ -1674,7 +1685,10 @@ impl Engine {
|
|||||||
#[cfg(feature = "no_function")]
|
#[cfg(feature = "no_function")]
|
||||||
let relax = false;
|
let relax = false;
|
||||||
|
|
||||||
if !relax && settings.options.strict_var && index.is_none() {
|
if !relax
|
||||||
|
&& settings.options.contains(LangOptions::STRICT_VAR)
|
||||||
|
&& index.is_none()
|
||||||
|
{
|
||||||
return Err(PERR::ModuleUndefined(namespace.root().to_string())
|
return Err(PERR::ModuleUndefined(namespace.root().to_string())
|
||||||
.into_err(namespace.position()));
|
.into_err(namespace.position()));
|
||||||
}
|
}
|
||||||
@ -3072,6 +3086,12 @@ impl Engine {
|
|||||||
new_state.max_expr_depth = self.max_function_expr_depth();
|
new_state.max_expr_depth = self.max_function_expr_depth();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut options = self.options;
|
||||||
|
options.set(
|
||||||
|
LangOptions::STRICT_VAR,
|
||||||
|
settings.options.contains(LangOptions::STRICT_VAR),
|
||||||
|
);
|
||||||
|
|
||||||
let new_settings = ParseSettings {
|
let new_settings = ParseSettings {
|
||||||
is_global: false,
|
is_global: false,
|
||||||
is_function_scope: true,
|
is_function_scope: true,
|
||||||
@ -3079,10 +3099,7 @@ impl Engine {
|
|||||||
is_closure_scope: false,
|
is_closure_scope: false,
|
||||||
is_breakable: false,
|
is_breakable: false,
|
||||||
level: 0,
|
level: 0,
|
||||||
options: LanguageOptions {
|
options,
|
||||||
strict_var: settings.options.strict_var,
|
|
||||||
..self.options
|
|
||||||
},
|
|
||||||
pos,
|
pos,
|
||||||
..settings
|
..settings
|
||||||
};
|
};
|
||||||
@ -3541,6 +3558,11 @@ impl Engine {
|
|||||||
) -> ParseResult<AST> {
|
) -> ParseResult<AST> {
|
||||||
let mut functions = BTreeMap::new();
|
let mut functions = BTreeMap::new();
|
||||||
|
|
||||||
|
let mut options = self.options;
|
||||||
|
options.remove(LangOptions::IF_EXPR | LangOptions::SWITCH_EXPR | LangOptions::STMT_EXPR);
|
||||||
|
#[cfg(not(feature = "no_function"))]
|
||||||
|
options.remove(LangOptions::ANON_FN);
|
||||||
|
|
||||||
let settings = ParseSettings {
|
let settings = ParseSettings {
|
||||||
is_global: true,
|
is_global: true,
|
||||||
#[cfg(not(feature = "no_function"))]
|
#[cfg(not(feature = "no_function"))]
|
||||||
@ -3550,14 +3572,7 @@ impl Engine {
|
|||||||
is_closure_scope: false,
|
is_closure_scope: false,
|
||||||
is_breakable: false,
|
is_breakable: false,
|
||||||
level: 0,
|
level: 0,
|
||||||
options: LanguageOptions {
|
options,
|
||||||
allow_if_expr: false,
|
|
||||||
allow_switch_expr: false,
|
|
||||||
allow_stmt_expr: false,
|
|
||||||
#[cfg(not(feature = "no_function"))]
|
|
||||||
allow_anonymous_fn: false,
|
|
||||||
..self.options
|
|
||||||
},
|
|
||||||
pos: Position::NONE,
|
pos: Position::NONE,
|
||||||
};
|
};
|
||||||
let expr = self.parse_expr(input, state, &mut functions, settings)?;
|
let expr = self.parse_expr(input, state, &mut functions, settings)?;
|
||||||
|
Loading…
Reference in New Issue
Block a user