2020-03-08 12:54:02 +01:00
|
|
|
//! Main module defining the lexer and parser.
|
|
|
|
|
2022-02-15 03:56:05 +01:00
|
|
|
use crate::api::events::VarDefInfo;
|
2022-05-19 15:40:22 +02:00
|
|
|
use crate::api::options::LangOptions;
|
2021-03-08 08:30:32 +01:00
|
|
|
use crate::ast::{
|
2022-07-19 07:33:53 +02:00
|
|
|
ASTFlags, BinaryExpr, CaseBlocksList, ConditionalExpr, Expr, FnCallExpr, FnCallHashes, Ident,
|
2022-11-28 16:24:22 +01:00
|
|
|
Namespace, OpAssignment, RangeCase, ScriptFnDef, Stmt, StmtBlock, StmtBlockContainer,
|
2022-07-19 07:33:53 +02:00
|
|
|
SwitchCasesCollection, TryCatchBlock,
|
2021-03-08 08:30:32 +01:00
|
|
|
};
|
2021-03-14 03:47:29 +01:00
|
|
|
use crate::engine::{Precedence, KEYWORD_THIS, OP_CONTAINS};
|
2022-11-08 08:01:40 +01:00
|
|
|
use crate::eval::{Caches, GlobalRuntimeState};
|
2022-09-05 06:13:06 +02:00
|
|
|
use crate::func::{hashing::get_hasher, StraightHashMap};
|
2021-11-13 15:36:23 +01:00
|
|
|
use crate::tokenizer::{
|
2022-02-16 10:51:14 +01:00
|
|
|
is_keyword_function, is_valid_function_name, is_valid_identifier, Token, TokenStream,
|
2022-11-28 16:24:22 +01:00
|
|
|
TokenizerControl, NO_TOKEN,
|
2021-04-04 17:08:27 +02:00
|
|
|
};
|
2021-11-13 15:36:23 +01:00
|
|
|
use crate::types::dynamic::AccessMode;
|
2021-12-27 14:56:50 +01:00
|
|
|
use crate::types::StringsInterner;
|
2020-11-16 16:10:14 +01:00
|
|
|
use crate::{
|
2022-11-28 16:24:22 +01:00
|
|
|
calc_fn_hash, Dynamic, Engine, EvalAltResult, EvalContext, ExclusiveRange, Identifier,
|
|
|
|
ImmutableString, InclusiveRange, LexError, OptimizationLevel, ParseError, Position, Scope,
|
|
|
|
Shared, SmartString, StaticVec, AST, INT, PERR,
|
2020-11-16 16:10:14 +01:00
|
|
|
};
|
2022-11-23 04:36:30 +01:00
|
|
|
use bitflags::bitflags;
|
2021-04-17 09:15:54 +02:00
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
|
|
|
use std::{
|
|
|
|
collections::BTreeMap,
|
2022-07-04 11:42:24 +02:00
|
|
|
fmt,
|
2021-04-17 09:15:54 +02:00
|
|
|
hash::{Hash, Hasher},
|
|
|
|
num::{NonZeroU8, NonZeroUsize},
|
|
|
|
};
|
2020-11-16 16:10:14 +01:00
|
|
|
|
2021-12-25 16:49:14 +01:00
|
|
|
pub type ParseResult<T> = Result<T, ParseError>;
|
|
|
|
|
2022-09-12 17:08:38 +02:00
|
|
|
type FnLib = StraightHashMap<Shared<ScriptFnDef>>;
|
2020-07-06 16:57:50 +02:00
|
|
|
|
2021-06-28 12:06:05 +02:00
|
|
|
/// Invalid variable name that acts as a search barrier in a [`Scope`].
|
2022-02-28 09:32:08 +01:00
|
|
|
const SCOPE_SEARCH_BARRIER_MARKER: &str = "$ BARRIER $";
|
2021-06-28 12:06:05 +02:00
|
|
|
|
2021-08-26 17:58:41 +02:00
|
|
|
/// The message: `TokenStream` never ends
|
2022-02-28 09:32:08 +01:00
|
|
|
const NEVER_ENDS: &str = "`Token`";
|
2021-05-22 13:14:24 +02:00
|
|
|
|
2022-07-18 02:54:10 +02:00
|
|
|
/// Unroll `switch` ranges no larger than this.
|
2022-08-27 10:26:41 +02:00
|
|
|
const SMALL_SWITCH_RANGE: INT = 16;
|
2022-07-18 02:54:10 +02:00
|
|
|
|
2021-09-24 03:26:35 +02:00
|
|
|
/// _(internals)_ A type that encapsulates the current state of the parser.
|
|
|
|
/// Exported under the `internals` feature only.
|
2022-11-24 08:10:17 +01:00
|
|
|
pub struct ParseState<'e, 's> {
|
2021-04-04 07:13:07 +02:00
|
|
|
/// Input stream buffer containing the next character to read.
|
2021-09-24 03:26:35 +02:00
|
|
|
pub tokenizer_control: TokenizerControl,
|
2022-08-14 08:32:16 +02:00
|
|
|
/// Controls whether parsing of an expression should stop given the next token.
|
|
|
|
pub expr_filter: fn(&Token) -> bool,
|
2022-11-24 09:05:23 +01:00
|
|
|
/// Strings interner.
|
2022-11-24 10:08:43 +01:00
|
|
|
pub interned_strings: &'s mut StringsInterner,
|
2022-04-21 07:21:53 +02:00
|
|
|
/// External [scope][Scope] with constants.
|
|
|
|
pub scope: &'e Scope<'e>,
|
2022-05-01 18:03:45 +02:00
|
|
|
/// Global runtime state.
|
2022-11-24 10:08:43 +01:00
|
|
|
pub global: Option<Box<GlobalRuntimeState>>,
|
2020-05-18 13:32:22 +02:00
|
|
|
/// Encapsulates a local stack with variable names to simulate an actual runtime scope.
|
2022-11-24 10:08:43 +01:00
|
|
|
pub stack: Option<Box<Scope<'e>>>,
|
2020-11-21 06:05:57 +01:00
|
|
|
/// Size of the local variables stack upon entry of the current block scope.
|
2022-02-18 08:04:46 +01:00
|
|
|
pub block_stack_len: usize,
|
2020-07-29 16:43:50 +02:00
|
|
|
/// Tracks a list of external variables (variables that are not explicitly declared in the scope).
|
2020-08-03 06:10:20 +02:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
2022-11-28 16:24:22 +01:00
|
|
|
pub external_vars: Option<Box<crate::FnArgsVec<Ident>>>,
|
2020-08-09 13:35:29 +02:00
|
|
|
/// An indicator that disables variable capturing into externals one single time
|
|
|
|
/// up until the nearest consumed Identifier token.
|
2021-06-12 16:47:43 +02:00
|
|
|
/// If set to false the next call to [`access_var`][ParseState::access_var] will not capture the variable.
|
2022-01-31 06:38:27 +01:00
|
|
|
/// All consequent calls to [`access_var`][ParseState::access_var] will not be affected.
|
2020-08-03 06:10:20 +02:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
2021-09-24 03:26:35 +02:00
|
|
|
pub allow_capture: bool,
|
2020-11-25 02:36:06 +01:00
|
|
|
/// Encapsulates a local stack with imported [module][crate::Module] names.
|
2020-10-18 11:02:17 +02:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-11-25 05:14:40 +01:00
|
|
|
pub imports: Option<Box<StaticVec<ImmutableString>>>,
|
2022-08-09 15:35:45 +02:00
|
|
|
/// List of globally-imported [module][crate::Module] names.
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-11-25 05:14:40 +01:00
|
|
|
pub global_imports: Option<Box<StaticVec<ImmutableString>>>,
|
2020-05-18 13:32:22 +02:00
|
|
|
}
|
2020-04-28 17:05:03 +02:00
|
|
|
|
2022-11-24 08:10:17 +01:00
|
|
|
impl fmt::Debug for ParseState<'_, '_> {
|
2022-09-27 02:52:51 +02:00
|
|
|
#[cold]
|
|
|
|
#[inline(never)]
|
2022-07-04 11:42:24 +02:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2022-07-04 11:47:59 +02:00
|
|
|
let mut f = f.debug_struct("ParseState");
|
|
|
|
|
|
|
|
f.field("tokenizer_control", &self.tokenizer_control)
|
2022-07-04 11:42:24 +02:00
|
|
|
.field("interned_strings", &self.interned_strings)
|
|
|
|
.field("scope", &self.scope)
|
|
|
|
.field("global", &self.global)
|
|
|
|
.field("stack", &self.stack)
|
2022-07-04 11:47:59 +02:00
|
|
|
.field("block_stack_len", &self.block_stack_len);
|
2022-11-30 07:11:09 +01:00
|
|
|
|
2022-07-04 11:47:59 +02:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
f.field("external_vars", &self.external_vars)
|
|
|
|
.field("allow_capture", &self.allow_capture);
|
2022-11-30 07:11:09 +01:00
|
|
|
|
2022-07-04 11:47:59 +02:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-08-09 15:35:45 +02:00
|
|
|
f.field("imports", &self.imports)
|
|
|
|
.field("global_imports", &self.global_imports);
|
2022-11-30 07:11:09 +01:00
|
|
|
|
2022-07-04 11:47:59 +02:00
|
|
|
f.finish()
|
2022-07-04 11:42:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 08:10:17 +01:00
|
|
|
impl<'e, 's> ParseState<'e, 's> {
|
2020-11-25 02:36:06 +01:00
|
|
|
/// Create a new [`ParseState`].
|
2022-09-28 06:06:22 +02:00
|
|
|
#[inline]
|
2021-06-12 16:47:43 +02:00
|
|
|
#[must_use]
|
2022-08-12 10:34:57 +02:00
|
|
|
pub fn new(
|
|
|
|
scope: &'e Scope,
|
2022-11-24 08:10:17 +01:00
|
|
|
interned_strings: &'s mut StringsInterner,
|
2022-08-12 10:34:57 +02:00
|
|
|
tokenizer_control: TokenizerControl,
|
|
|
|
) -> Self {
|
2020-05-18 13:32:22 +02:00
|
|
|
Self {
|
2021-04-04 17:08:27 +02:00
|
|
|
tokenizer_control,
|
2022-07-04 11:42:24 +02:00
|
|
|
expr_filter: |_| true,
|
2020-08-03 06:10:20 +02:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
2022-11-24 10:08:43 +01:00
|
|
|
external_vars: None,
|
2020-08-03 06:10:20 +02:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
allow_capture: true,
|
2022-08-14 08:32:16 +02:00
|
|
|
interned_strings,
|
2022-04-21 07:21:53 +02:00
|
|
|
scope,
|
2022-11-24 10:08:43 +01:00
|
|
|
global: None,
|
|
|
|
stack: None,
|
2022-02-18 08:04:46 +01:00
|
|
|
block_stack_len: 0,
|
2020-10-18 11:02:17 +02:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-11-25 05:14:40 +01:00
|
|
|
imports: None,
|
2022-08-09 15:35:45 +02:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-11-25 05:14:40 +01:00
|
|
|
global_imports: None,
|
2020-05-18 13:32:22 +02:00
|
|
|
}
|
2020-04-28 17:05:03 +02:00
|
|
|
}
|
2020-07-26 04:18:18 +02:00
|
|
|
|
2020-11-25 02:36:06 +01:00
|
|
|
/// Find explicitly declared variable by name in the [`ParseState`], searching in reverse order.
|
2020-07-26 04:18:18 +02:00
|
|
|
///
|
2022-02-28 15:13:03 +01:00
|
|
|
/// The first return value is the offset to be deducted from `ParseState::stack::len()`,
|
|
|
|
/// i.e. the top element of [`ParseState`]'s variables stack is offset 1.
|
|
|
|
///
|
|
|
|
/// If the variable is not present in the scope, the first return value is zero.
|
|
|
|
///
|
|
|
|
/// The second return value indicates whether the barrier has been hit before finding the variable.
|
2022-07-27 12:04:59 +02:00
|
|
|
#[must_use]
|
2022-02-28 15:13:03 +01:00
|
|
|
pub fn find_var(&self, name: &str) -> (usize, bool) {
|
|
|
|
let mut hit_barrier = false;
|
|
|
|
|
2022-11-30 07:11:09 +01:00
|
|
|
let index = self
|
|
|
|
.stack
|
|
|
|
.as_deref()
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|s| s.iter_rev_raw())
|
|
|
|
.enumerate()
|
|
|
|
.find(|&(.., (n, ..))| {
|
|
|
|
if n == SCOPE_SEARCH_BARRIER_MARKER {
|
|
|
|
// Do not go beyond the barrier
|
|
|
|
hit_barrier = true;
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
n == name
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map_or(0, |(i, ..)| i + 1);
|
|
|
|
|
|
|
|
(index, hit_barrier)
|
2022-02-28 15:13:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Find explicitly declared variable by name in the [`ParseState`], searching in reverse order.
|
|
|
|
///
|
|
|
|
/// If the variable is not present in the scope adds it to the list of external variables.
|
2020-07-26 04:18:18 +02:00
|
|
|
///
|
2021-07-04 10:40:15 +02:00
|
|
|
/// The return value is the offset to be deducted from `ParseState::stack::len()`,
|
2021-06-12 16:47:43 +02:00
|
|
|
/// i.e. the top element of [`ParseState`]'s variables stack is offset 1.
|
2020-11-21 06:05:57 +01:00
|
|
|
///
|
2022-08-10 06:48:37 +02:00
|
|
|
/// # Return value: `(index, is_func_name)`
|
2022-08-05 17:30:44 +02:00
|
|
|
///
|
2022-10-30 11:43:18 +01:00
|
|
|
/// * `index`: [`None`] when the variable name is not found in the `stack`,
|
2022-08-05 17:30:44 +02:00
|
|
|
/// otherwise the index value.
|
|
|
|
///
|
2022-08-10 06:48:37 +02:00
|
|
|
/// * `is_func_name`: `true` if the variable is actually the name of a function
|
|
|
|
/// (in which case it will be converted into a function pointer).
|
2021-12-04 10:57:28 +01:00
|
|
|
#[must_use]
|
2022-08-05 17:30:44 +02:00
|
|
|
pub fn access_var(
|
|
|
|
&mut self,
|
|
|
|
name: &str,
|
|
|
|
lib: &FnLib,
|
|
|
|
pos: Position,
|
|
|
|
) -> (Option<NonZeroUsize>, bool) {
|
2022-08-18 15:16:42 +02:00
|
|
|
let _lib = lib;
|
2021-08-13 07:42:39 +02:00
|
|
|
let _pos = pos;
|
2020-11-21 06:05:57 +01:00
|
|
|
|
2022-02-28 15:13:03 +01:00
|
|
|
let (index, hit_barrier) = self.find_var(name);
|
2020-07-26 04:18:18 +02:00
|
|
|
|
2022-08-05 17:30:44 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2022-08-18 15:16:42 +02:00
|
|
|
let is_func_name = _lib.values().any(|f| f.name == name);
|
2022-08-05 17:30:44 +02:00
|
|
|
#[cfg(feature = "no_function")]
|
2022-08-10 06:48:37 +02:00
|
|
|
let is_func_name = false;
|
2022-08-05 17:30:44 +02:00
|
|
|
|
2020-08-03 06:10:20 +02:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
if self.allow_capture {
|
2022-11-24 10:08:43 +01:00
|
|
|
if !is_func_name
|
|
|
|
&& index == 0
|
|
|
|
&& !self
|
|
|
|
.external_vars
|
2022-11-25 16:03:20 +01:00
|
|
|
.as_deref()
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
|
|
|
.any(|v| v.name == name)
|
2022-08-10 06:48:37 +02:00
|
|
|
{
|
2022-11-24 15:25:19 +01:00
|
|
|
self.external_vars
|
2022-11-25 02:46:13 +01:00
|
|
|
.get_or_insert_with(Default::default)
|
2022-11-24 15:25:19 +01:00
|
|
|
.push(Ident {
|
|
|
|
name: name.into(),
|
|
|
|
pos: _pos,
|
|
|
|
});
|
2020-07-31 00:34:20 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-07-27 12:04:59 +02:00
|
|
|
self.allow_capture = true;
|
2020-07-26 04:18:18 +02:00
|
|
|
}
|
|
|
|
|
2022-08-05 17:30:44 +02:00
|
|
|
let index = if hit_barrier {
|
2020-11-21 06:05:57 +01:00
|
|
|
None
|
|
|
|
} else {
|
2022-02-28 15:13:03 +01:00
|
|
|
NonZeroUsize::new(index)
|
2022-08-05 17:30:44 +02:00
|
|
|
};
|
2022-08-05 17:45:40 +02:00
|
|
|
|
2022-08-10 06:48:37 +02:00
|
|
|
(index, is_func_name)
|
2020-05-04 13:36:58 +02:00
|
|
|
}
|
2020-07-26 04:18:18 +02:00
|
|
|
|
2020-11-25 02:36:06 +01:00
|
|
|
/// Find a module by name in the [`ParseState`], searching in reverse.
|
2020-10-18 11:29:11 +02:00
|
|
|
///
|
|
|
|
/// Returns the offset to be deducted from `Stack::len`,
|
2020-11-25 02:36:06 +01:00
|
|
|
/// i.e. the top element of the [`ParseState`] is offset 1.
|
2020-10-18 11:29:11 +02:00
|
|
|
///
|
2022-10-30 11:43:18 +01:00
|
|
|
/// Returns [`None`] when the variable name is not found in the [`ParseState`].
|
2020-10-18 11:29:11 +02:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics when called under `no_module`.
|
2020-10-18 16:10:08 +02:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2021-06-12 16:47:43 +02:00
|
|
|
#[must_use]
|
2022-01-04 08:22:48 +01:00
|
|
|
pub fn find_module(&self, name: &str) -> Option<NonZeroUsize> {
|
2022-01-24 10:04:40 +01:00
|
|
|
self.imports
|
2022-11-25 16:03:20 +01:00
|
|
|
.as_deref()
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
2020-05-04 13:36:58 +02:00
|
|
|
.rev()
|
|
|
|
.enumerate()
|
2022-08-13 12:07:42 +02:00
|
|
|
.find(|(.., n)| n.as_str() == name)
|
2022-02-08 02:02:15 +01:00
|
|
|
.and_then(|(i, ..)| NonZeroUsize::new(i + 1))
|
2020-04-28 17:05:03 +02:00
|
|
|
}
|
2020-11-12 15:36:13 +01:00
|
|
|
|
2022-08-13 12:07:42 +02:00
|
|
|
/// Get an interned string, creating one if it is not yet interned.
|
2022-08-11 16:56:23 +02:00
|
|
|
#[inline(always)]
|
|
|
|
#[must_use]
|
2022-08-13 12:07:42 +02:00
|
|
|
pub fn get_interned_string(
|
2022-08-11 16:56:23 +02:00
|
|
|
&mut self,
|
2022-08-12 10:34:57 +02:00
|
|
|
text: impl AsRef<str> + Into<ImmutableString>,
|
2022-08-13 12:07:42 +02:00
|
|
|
) -> ImmutableString {
|
2022-09-27 02:52:51 +02:00
|
|
|
self.interned_strings.get(text)
|
2021-12-27 14:56:50 +01:00
|
|
|
}
|
|
|
|
|
2022-08-13 12:07:42 +02:00
|
|
|
/// Get an interned property getter, creating one if it is not yet interned.
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-09-28 06:06:22 +02:00
|
|
|
#[inline]
|
2021-06-12 16:47:43 +02:00
|
|
|
#[must_use]
|
2022-08-13 12:07:42 +02:00
|
|
|
pub fn get_interned_getter(
|
2022-08-12 10:34:57 +02:00
|
|
|
&mut self,
|
|
|
|
text: impl AsRef<str> + Into<ImmutableString>,
|
|
|
|
) -> ImmutableString {
|
2022-09-27 02:52:51 +02:00
|
|
|
self.interned_strings.get_with_mapper(
|
|
|
|
crate::engine::FN_GET,
|
|
|
|
|s| crate::engine::make_getter(s.as_ref()).into(),
|
|
|
|
text,
|
|
|
|
)
|
2022-08-11 16:56:23 +02:00
|
|
|
}
|
|
|
|
|
2022-08-13 12:07:42 +02:00
|
|
|
/// Get an interned property setter, creating one if it is not yet interned.
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-09-28 06:06:22 +02:00
|
|
|
#[inline]
|
2022-08-11 16:56:23 +02:00
|
|
|
#[must_use]
|
2022-08-13 12:07:42 +02:00
|
|
|
pub fn get_interned_setter(
|
2021-12-27 14:56:50 +01:00
|
|
|
&mut self,
|
2022-08-12 10:34:57 +02:00
|
|
|
text: impl AsRef<str> + Into<ImmutableString>,
|
2021-12-27 14:56:50 +01:00
|
|
|
) -> ImmutableString {
|
2022-09-27 02:52:51 +02:00
|
|
|
self.interned_strings.get_with_mapper(
|
|
|
|
crate::engine::FN_SET,
|
|
|
|
|s| crate::engine::make_setter(s.as_ref()).into(),
|
|
|
|
text,
|
|
|
|
)
|
2020-11-12 15:36:13 +01:00
|
|
|
}
|
2020-04-28 17:05:03 +02:00
|
|
|
}
|
|
|
|
|
2022-11-23 04:36:30 +01:00
|
|
|
bitflags! {
|
|
|
|
/// Bit-flags containing all status for [`ParseSettings`].
|
|
|
|
pub struct ParseSettingFlags: u8 {
|
|
|
|
/// Is the construct being parsed located at global level?
|
|
|
|
const GLOBAL_LEVEL = 0b0000_0001;
|
|
|
|
/// Is the construct being parsed located inside a function definition?
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
const FN_SCOPE = 0b0000_0010;
|
|
|
|
/// Is the construct being parsed located inside a closure definition?
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
const CLOSURE_SCOPE = 0b0000_0100;
|
|
|
|
/// Is the construct being parsed located inside a breakable loop?
|
|
|
|
const BREAKABLE = 0b0000_1000;
|
2022-11-30 07:11:09 +01:00
|
|
|
|
2022-11-23 04:36:30 +01:00
|
|
|
/// Disallow statements in blocks?
|
|
|
|
const DISALLOW_STATEMENTS_IN_BLOCKS = 0b0001_0000;
|
|
|
|
/// Disallow unquoted map properties?
|
|
|
|
const DISALLOW_UNQUOTED_MAP_PROPERTIES = 0b0010_0000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
/// A type that encapsulates all the settings for a particular parsing function.
|
2020-11-25 02:36:06 +01:00
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
2022-11-23 04:36:30 +01:00
|
|
|
pub struct ParseSettings {
|
|
|
|
/// Flags.
|
|
|
|
pub flags: ParseSettingFlags,
|
2022-02-28 09:32:08 +01:00
|
|
|
/// Language options in effect (overrides Engine options).
|
2022-09-29 16:46:59 +02:00
|
|
|
pub options: LangOptions,
|
2020-06-11 16:03:47 +02:00
|
|
|
/// Current expression nesting level.
|
2022-09-29 16:46:59 +02:00
|
|
|
pub level: usize,
|
2022-02-28 09:32:08 +01:00
|
|
|
/// Current position.
|
2022-09-29 16:46:59 +02:00
|
|
|
pub pos: Position,
|
2022-11-24 10:08:43 +01:00
|
|
|
/// Maximum levels of expression nesting (0 for unlimited).
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
pub max_expr_depth: usize,
|
2020-06-11 16:03:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ParseSettings {
|
2022-11-23 04:36:30 +01:00
|
|
|
/// Is a particular flag on?
|
|
|
|
#[inline(always)]
|
|
|
|
#[must_use]
|
|
|
|
pub const fn has_flag(&self, flag: ParseSettingFlags) -> bool {
|
|
|
|
self.flags.contains(flag)
|
|
|
|
}
|
|
|
|
/// Is a particular language option on?
|
|
|
|
#[inline(always)]
|
|
|
|
#[must_use]
|
|
|
|
pub const fn has_option(&self, option: LangOptions) -> bool {
|
|
|
|
self.options.contains(option)
|
|
|
|
}
|
2020-06-11 16:03:47 +02:00
|
|
|
/// Create a new `ParseSettings` with one higher expression level.
|
2022-09-28 06:06:22 +02:00
|
|
|
#[inline]
|
2022-11-24 10:08:43 +01:00
|
|
|
pub fn level_up(&self) -> ParseResult<Self> {
|
|
|
|
let level = self.level + 1;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
if self.max_expr_depth > 0 && level > self.max_expr_depth {
|
2022-07-27 10:04:24 +02:00
|
|
|
return Err(PERR::ExprTooDeep.into_err(self.pos));
|
2020-06-11 16:03:47 +02:00
|
|
|
}
|
2022-11-24 10:08:43 +01:00
|
|
|
|
|
|
|
Ok(Self { level, ..*self })
|
2020-06-11 16:03:47 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-12 15:36:13 +01:00
|
|
|
|
2022-01-12 01:12:28 +01:00
|
|
|
/// Make an anonymous function.
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[inline]
|
|
|
|
#[must_use]
|
2022-11-22 16:30:43 +01:00
|
|
|
pub fn make_anonymous_fn(hash: u64) -> Identifier {
|
|
|
|
use std::fmt::Write;
|
|
|
|
|
|
|
|
let mut buf = Identifier::new_const();
|
|
|
|
write!(&mut buf, "{}{:016x}", crate::engine::FN_ANONYMOUS, hash).unwrap();
|
|
|
|
buf
|
2022-01-12 01:12:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Is this function an anonymous function?
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[inline(always)]
|
|
|
|
#[must_use]
|
|
|
|
pub fn is_anonymous_fn(fn_name: &str) -> bool {
|
|
|
|
fn_name.starts_with(crate::engine::FN_ANONYMOUS)
|
|
|
|
}
|
|
|
|
|
2020-11-12 15:36:13 +01:00
|
|
|
impl Expr {
|
2020-11-25 02:36:06 +01:00
|
|
|
/// Convert a [`Variable`][Expr::Variable] into a [`Property`][Expr::Property].
|
|
|
|
/// All other variants are untouched.
|
2020-11-12 15:36:13 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2021-07-10 05:06:13 +02:00
|
|
|
#[inline]
|
2021-06-12 16:47:43 +02:00
|
|
|
#[must_use]
|
2020-11-12 15:36:13 +01:00
|
|
|
fn into_property(self, state: &mut ParseState) -> Self {
|
|
|
|
match self {
|
2022-01-29 04:09:43 +01:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-03-05 10:57:23 +01:00
|
|
|
Self::Variable(x, ..) if !x.1.is_empty() => unreachable!("qualified property"),
|
|
|
|
Self::Variable(x, .., pos) => {
|
2022-08-13 12:07:42 +02:00
|
|
|
let ident = x.3.clone();
|
|
|
|
let getter = state.get_interned_getter(ident.as_str());
|
2022-09-21 05:46:23 +02:00
|
|
|
let hash_get = calc_fn_hash(None, &getter, 1);
|
2022-08-13 12:07:42 +02:00
|
|
|
let setter = state.get_interned_setter(ident.as_str());
|
2022-09-21 05:46:23 +02:00
|
|
|
let hash_set = calc_fn_hash(None, &setter, 2);
|
2021-03-08 08:30:32 +01:00
|
|
|
|
2022-01-28 03:11:40 +01:00
|
|
|
Self::Property(
|
2022-08-13 12:07:42 +02:00
|
|
|
Box::new(((getter, hash_get), (setter, hash_set), ident)),
|
2022-01-28 03:11:40 +01:00
|
|
|
pos,
|
|
|
|
)
|
2020-11-12 15:36:13 +01:00
|
|
|
}
|
|
|
|
_ => self,
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 18:15:27 +02:00
|
|
|
/// Raise an error if the expression can never yield a boolean value.
|
2021-12-25 16:49:14 +01:00
|
|
|
fn ensure_bool_expr(self) -> ParseResult<Expr> {
|
2021-07-03 18:15:27 +02:00
|
|
|
let type_name = match self {
|
2022-02-08 02:46:14 +01:00
|
|
|
Expr::Unit(..) => "()",
|
2022-11-09 05:44:57 +01:00
|
|
|
Expr::DynamicConstant(ref v, ..) if !v.is_bool() => v.type_name(),
|
2022-02-08 02:02:15 +01:00
|
|
|
Expr::IntegerConstant(..) => "a number",
|
2021-07-03 18:15:27 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Expr::FloatConstant(..) => "a floating-point number",
|
|
|
|
Expr::CharConstant(..) => "a character",
|
|
|
|
Expr::StringConstant(..) => "a string",
|
|
|
|
Expr::InterpolatedString(..) => "a string",
|
|
|
|
Expr::Array(..) => "an array",
|
|
|
|
Expr::Map(..) => "an object map",
|
2021-07-03 18:15:27 +02:00
|
|
|
_ => return Ok(self),
|
|
|
|
};
|
|
|
|
|
|
|
|
Err(
|
2022-11-22 16:30:43 +01:00
|
|
|
PERR::MismatchedType("a boolean expression".into(), type_name.into())
|
2022-02-04 05:04:33 +01:00
|
|
|
.into_err(self.start_position()),
|
2021-07-03 18:15:27 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
/// Raise an error if the expression can never yield an iterable value.
|
2021-12-25 16:49:14 +01:00
|
|
|
fn ensure_iterable(self) -> ParseResult<Expr> {
|
2021-07-03 18:15:27 +02:00
|
|
|
let type_name = match self {
|
2022-02-08 02:46:14 +01:00
|
|
|
Expr::Unit(..) => "()",
|
2022-02-08 02:02:15 +01:00
|
|
|
Expr::BoolConstant(..) => "a boolean",
|
|
|
|
Expr::IntegerConstant(..) => "a number",
|
2021-07-03 18:15:27 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2022-02-08 02:02:15 +01:00
|
|
|
Expr::FloatConstant(..) => "a floating-point number",
|
|
|
|
Expr::CharConstant(..) => "a character",
|
|
|
|
Expr::Map(..) => "an object map",
|
2021-07-03 18:15:27 +02:00
|
|
|
_ => return Ok(self),
|
|
|
|
};
|
|
|
|
|
|
|
|
Err(
|
2022-11-22 16:30:43 +01:00
|
|
|
PERR::MismatchedType("an iterable value".into(), type_name.into())
|
2022-02-04 05:04:33 +01:00
|
|
|
.into_err(self.start_position()),
|
2021-07-03 18:15:27 +02:00
|
|
|
)
|
|
|
|
}
|
2020-11-12 15:36:13 +01:00
|
|
|
}
|
|
|
|
|
2021-07-04 10:40:15 +02:00
|
|
|
/// Make sure that the next expression is not a statement expression (i.e. wrapped in `{}`).
|
2022-07-27 12:04:59 +02:00
|
|
|
fn ensure_not_statement_expr(
|
|
|
|
input: &mut TokenStream,
|
|
|
|
type_name: &(impl ToString + ?Sized),
|
|
|
|
) -> ParseResult<()> {
|
2021-07-04 10:40:15 +02:00
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
(Token::LeftBrace, pos) => Err(PERR::ExprExpected(type_name.to_string()).into_err(*pos)),
|
|
|
|
_ => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Make sure that the next expression is not a mis-typed assignment (i.e. `a = b` instead of `a == b`).
|
2021-12-25 16:49:14 +01:00
|
|
|
fn ensure_not_assignment(input: &mut TokenStream) -> ParseResult<()> {
|
2021-07-04 10:40:15 +02:00
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
(Token::Equals, pos) => Err(LexError::ImproperSymbol(
|
2022-11-22 16:30:43 +01:00
|
|
|
"=".into(),
|
|
|
|
"Possibly a typo of '=='?".into(),
|
2021-07-04 10:40:15 +02:00
|
|
|
)
|
|
|
|
.into_err(*pos)),
|
|
|
|
_ => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 02:36:06 +01:00
|
|
|
/// Consume a particular [token][Token], checking that it is the expected one.
|
2021-11-13 02:50:49 +01:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
2022-11-22 16:30:43 +01:00
|
|
|
/// Panics if the next token is not the expected one, or either tokens is not a literal symbol.
|
2021-11-13 02:50:49 +01:00
|
|
|
fn eat_token(input: &mut TokenStream, expected_token: Token) -> Position {
|
2021-05-22 13:14:24 +02:00
|
|
|
let (t, pos) = input.next().expect(NEVER_ENDS);
|
2020-04-23 07:24:24 +02:00
|
|
|
|
2021-11-13 02:50:49 +01:00
|
|
|
if t != expected_token {
|
2020-06-16 16:14:46 +02:00
|
|
|
unreachable!(
|
2021-12-30 05:19:41 +01:00
|
|
|
"{} expected but gets {} at {}",
|
2022-11-22 16:30:43 +01:00
|
|
|
expected_token.literal_syntax(),
|
|
|
|
t.literal_syntax(),
|
2020-04-23 07:24:24 +02:00
|
|
|
pos
|
|
|
|
);
|
2020-04-15 15:41:28 +02:00
|
|
|
}
|
2020-04-23 07:24:24 +02:00
|
|
|
pos
|
2020-04-15 15:41:28 +02:00
|
|
|
}
|
|
|
|
|
2020-11-25 02:36:06 +01:00
|
|
|
/// Match a particular [token][Token], consuming it if matched.
|
2020-10-20 17:16:03 +02:00
|
|
|
fn match_token(input: &mut TokenStream, token: Token) -> (bool, Position) {
|
2021-05-22 13:14:24 +02:00
|
|
|
let (t, pos) = input.peek().expect(NEVER_ENDS);
|
2020-04-17 14:01:41 +02:00
|
|
|
if *t == token {
|
2020-10-20 17:16:03 +02:00
|
|
|
(true, eat_token(input, token))
|
2020-04-15 15:41:28 +02:00
|
|
|
} else {
|
2020-10-20 17:16:03 +02:00
|
|
|
(false, *pos)
|
2020-04-15 15:41:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 05:43:00 +02:00
|
|
|
/// Parse a variable name.
|
2022-02-26 10:28:58 +01:00
|
|
|
fn parse_var_name(input: &mut TokenStream) -> ParseResult<(SmartString, Position)> {
|
2021-06-07 05:43:00 +02:00
|
|
|
match input.next().expect(NEVER_ENDS) {
|
|
|
|
// Variable name
|
2022-09-25 17:03:18 +02:00
|
|
|
(Token::Identifier(s), pos) => Ok((*s, pos)),
|
2021-06-07 05:43:00 +02:00
|
|
|
// Reserved keyword
|
2022-10-30 08:45:25 +01:00
|
|
|
(Token::Reserved(s), pos) if is_valid_identifier(s.as_str()) => {
|
2021-11-11 06:55:52 +01:00
|
|
|
Err(PERR::Reserved(s.to_string()).into_err(pos))
|
2021-06-07 05:43:00 +02:00
|
|
|
}
|
|
|
|
// Bad identifier
|
|
|
|
(Token::LexError(err), pos) => Err(err.into_err(pos)),
|
|
|
|
// Not a variable name
|
2022-02-08 02:02:15 +01:00
|
|
|
(.., pos) => Err(PERR::VariableExpected.into_err(pos)),
|
2021-06-07 05:43:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-10 09:50:31 +02:00
|
|
|
/// Parse a symbol.
|
2022-07-05 16:59:03 +02:00
|
|
|
#[cfg(not(feature = "no_custom_syntax"))]
|
2022-02-26 10:28:58 +01:00
|
|
|
fn parse_symbol(input: &mut TokenStream) -> ParseResult<(SmartString, Position)> {
|
2021-07-10 09:50:31 +02:00
|
|
|
match input.next().expect(NEVER_ENDS) {
|
|
|
|
// Symbol
|
|
|
|
(token, pos) if token.is_standard_symbol() => Ok((token.literal_syntax().into(), pos)),
|
|
|
|
// Reserved symbol
|
2022-10-30 08:45:25 +01:00
|
|
|
(Token::Reserved(s), pos) if !is_valid_identifier(s.as_str()) => Ok((*s, pos)),
|
2022-02-28 09:32:08 +01:00
|
|
|
// Bad symbol
|
2021-07-10 09:50:31 +02:00
|
|
|
(Token::LexError(err), pos) => Err(err.into_err(pos)),
|
|
|
|
// Not a symbol
|
2022-02-08 02:02:15 +01:00
|
|
|
(.., pos) => Err(PERR::MissingSymbol(String::new()).into_err(pos)),
|
2021-07-10 09:50:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
impl Engine {
|
|
|
|
/// Parse `(` expr `)`
|
|
|
|
fn parse_paren_expr(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Expr> {
|
|
|
|
// ( ...
|
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = eat_token(input, Token::LeftParen);
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
let expr = self.parse_expr(input, state, lib, settings.level_up()?)?;
|
2017-10-28 05:30:12 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match input.next().expect(NEVER_ENDS) {
|
2022-02-28 09:32:08 +01:00
|
|
|
// ( ... )
|
2022-02-28 07:37:46 +01:00
|
|
|
(Token::RightParen, ..) => Ok(expr),
|
|
|
|
// ( <error>
|
|
|
|
(Token::LexError(err), pos) => Err(err.into_err(pos)),
|
2022-02-28 09:32:08 +01:00
|
|
|
// ( ... ???
|
2022-02-28 07:37:46 +01:00
|
|
|
(.., pos) => Err(PERR::MissingToken(
|
2020-05-04 13:36:58 +02:00
|
|
|
Token::RightParen.into(),
|
2022-02-28 07:37:46 +01:00
|
|
|
"for a matching ( in this expression".into(),
|
2020-04-06 06:29:01 +02:00
|
|
|
)
|
2022-02-28 07:37:46 +01:00
|
|
|
.into_err(pos)),
|
2020-04-06 06:29:01 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2022-01-29 04:09:43 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse a function call.
|
|
|
|
fn parse_fn_call(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
2022-08-13 12:07:42 +02:00
|
|
|
id: ImmutableString,
|
2022-04-21 04:04:46 +02:00
|
|
|
no_args: bool,
|
2022-02-28 07:37:46 +01:00
|
|
|
capture_parent_scope: bool,
|
2022-11-28 16:24:22 +01:00
|
|
|
namespace: Namespace,
|
2022-02-28 07:37:46 +01:00
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Expr> {
|
2022-04-21 04:04:46 +02:00
|
|
|
let (token, token_pos) = if no_args {
|
|
|
|
&(Token::RightParen, Position::NONE)
|
|
|
|
} else {
|
|
|
|
input.peek().expect(NEVER_ENDS)
|
|
|
|
};
|
2021-03-28 17:06:59 +02:00
|
|
|
|
2022-11-28 16:24:22 +01:00
|
|
|
let mut _namespace = namespace;
|
2022-02-28 07:37:46 +01:00
|
|
|
let mut args = StaticVec::new_const();
|
2021-05-05 12:38:52 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match token {
|
|
|
|
// id( <EOF>
|
|
|
|
Token::EOF => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::RightParen.into(),
|
2022-08-11 13:01:23 +02:00
|
|
|
format!("to close the arguments list of this function call '{id}'"),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
|
|
|
.into_err(*token_pos))
|
2021-06-16 12:36:33 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// id( <error>
|
|
|
|
Token::LexError(err) => return Err(err.clone().into_err(*token_pos)),
|
|
|
|
// id()
|
|
|
|
Token::RightParen => {
|
2022-04-21 04:04:46 +02:00
|
|
|
if !no_args {
|
|
|
|
eat_token(input, Token::RightParen);
|
|
|
|
}
|
2020-05-04 11:43:54 +02:00
|
|
|
|
2022-01-29 04:09:43 +01:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-11-28 16:24:22 +01:00
|
|
|
let hash = if _namespace.is_empty() {
|
2022-09-21 05:46:23 +02:00
|
|
|
calc_fn_hash(None, &id, 0)
|
2022-07-27 12:04:59 +02:00
|
|
|
} else {
|
2022-11-28 16:24:22 +01:00
|
|
|
let root = _namespace.root();
|
2022-06-09 11:59:28 +02:00
|
|
|
let index = state.find_module(root);
|
2022-11-28 16:24:22 +01:00
|
|
|
let is_global = false;
|
2021-12-04 11:07:27 +01:00
|
|
|
|
2022-06-09 12:22:53 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-11-28 16:24:22 +01:00
|
|
|
let is_global = is_global || root == crate::engine::KEYWORD_GLOBAL;
|
2022-06-09 12:22:53 +02:00
|
|
|
|
2022-11-23 04:36:30 +01:00
|
|
|
if settings.has_option(LangOptions::STRICT_VAR)
|
2022-07-27 10:04:24 +02:00
|
|
|
&& index.is_none()
|
|
|
|
&& !is_global
|
2022-11-25 05:14:40 +01:00
|
|
|
&& !state
|
|
|
|
.global_imports
|
2022-11-25 16:03:20 +01:00
|
|
|
.as_deref()
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
2022-11-25 05:14:40 +01:00
|
|
|
.any(|m| m.as_str() == root)
|
2022-11-24 15:58:42 +01:00
|
|
|
&& !self
|
|
|
|
.global_sub_modules
|
2022-11-25 13:42:16 +01:00
|
|
|
.as_deref()
|
2022-11-24 15:58:42 +01:00
|
|
|
.map_or(false, |m| m.contains_key(root))
|
2022-07-27 10:04:24 +02:00
|
|
|
{
|
|
|
|
return Err(
|
2022-11-28 16:24:22 +01:00
|
|
|
PERR::ModuleUndefined(root.into()).into_err(_namespace.position())
|
2022-07-27 10:04:24 +02:00
|
|
|
);
|
2021-12-04 10:57:28 +01:00
|
|
|
}
|
|
|
|
|
2022-11-28 16:24:22 +01:00
|
|
|
_namespace.set_index(index);
|
2022-01-29 04:09:43 +01:00
|
|
|
|
2022-11-28 16:24:22 +01:00
|
|
|
calc_fn_hash(_namespace.iter().map(Ident::as_str), &id, 0)
|
2021-12-04 10:57:28 +01:00
|
|
|
};
|
2022-01-29 04:09:43 +01:00
|
|
|
#[cfg(feature = "no_module")]
|
2022-09-21 05:46:23 +02:00
|
|
|
let hash = calc_fn_hash(None, &id, 0);
|
2020-05-09 10:15:50 +02:00
|
|
|
|
2022-10-30 08:45:25 +01:00
|
|
|
let hashes = if is_valid_function_name(&id) {
|
2021-11-16 06:42:46 +01:00
|
|
|
hash.into()
|
2021-03-28 17:06:59 +02:00
|
|
|
} else {
|
2021-04-20 16:26:08 +02:00
|
|
|
FnCallHashes::from_native(hash)
|
2021-03-28 17:06:59 +02:00
|
|
|
};
|
|
|
|
|
2021-05-05 12:38:52 +02:00
|
|
|
args.shrink_to_fit();
|
|
|
|
|
2021-06-16 12:36:33 +02:00
|
|
|
return Ok(FnCallExpr {
|
2022-09-02 17:45:25 +02:00
|
|
|
name: state.get_interned_string(id),
|
2021-11-13 05:23:35 +01:00
|
|
|
capture_parent_scope,
|
2022-11-28 16:24:22 +01:00
|
|
|
op_token: NO_TOKEN,
|
|
|
|
namespace: _namespace,
|
2021-06-16 12:36:33 +02:00
|
|
|
hashes,
|
|
|
|
args,
|
|
|
|
}
|
|
|
|
.into_fn_call_expr(settings.pos));
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// id...
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
let settings = settings.level_up()?;
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
loop {
|
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
// id(...args, ) - handle trailing comma
|
|
|
|
(Token::RightParen, ..) => (),
|
|
|
|
_ => args.push(self.parse_expr(input, state, lib, settings)?),
|
2020-04-06 06:29:01 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
// id(...args)
|
|
|
|
(Token::RightParen, ..) => {
|
|
|
|
eat_token(input, Token::RightParen);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-11-28 16:24:22 +01:00
|
|
|
let hash = if _namespace.is_empty() {
|
2022-09-21 05:46:23 +02:00
|
|
|
calc_fn_hash(None, &id, args.len())
|
2022-07-27 12:04:59 +02:00
|
|
|
} else {
|
2022-11-28 16:24:22 +01:00
|
|
|
let root = _namespace.root();
|
2022-06-09 11:59:28 +02:00
|
|
|
let index = state.find_module(root);
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-06-09 12:22:53 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
let is_global = root == crate::engine::KEYWORD_GLOBAL;
|
|
|
|
#[cfg(any(feature = "no_function", feature = "no_module"))]
|
|
|
|
let is_global = false;
|
|
|
|
|
2022-11-23 04:36:30 +01:00
|
|
|
if settings.has_option(LangOptions::STRICT_VAR)
|
2022-07-27 10:04:24 +02:00
|
|
|
&& index.is_none()
|
|
|
|
&& !is_global
|
2022-11-25 05:14:40 +01:00
|
|
|
&& !state
|
|
|
|
.global_imports
|
2022-11-25 16:03:20 +01:00
|
|
|
.as_deref()
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
2022-11-25 05:14:40 +01:00
|
|
|
.any(|m| m.as_str() == root)
|
2022-11-24 15:58:42 +01:00
|
|
|
&& !self
|
|
|
|
.global_sub_modules
|
2022-11-25 13:42:16 +01:00
|
|
|
.as_deref()
|
2022-11-24 15:58:42 +01:00
|
|
|
.map_or(false, |m| m.contains_key(root))
|
2022-07-27 10:04:24 +02:00
|
|
|
{
|
2022-11-22 16:30:43 +01:00
|
|
|
return Err(
|
2022-11-28 16:24:22 +01:00
|
|
|
PERR::ModuleUndefined(root.into()).into_err(_namespace.position())
|
2022-11-22 16:30:43 +01:00
|
|
|
);
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
|
2022-11-28 16:24:22 +01:00
|
|
|
_namespace.set_index(index);
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-11-28 16:24:22 +01:00
|
|
|
calc_fn_hash(_namespace.iter().map(Ident::as_str), &id, args.len())
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
|
|
|
#[cfg(feature = "no_module")]
|
2022-09-21 05:46:23 +02:00
|
|
|
let hash = calc_fn_hash(None, &id, args.len());
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-10-30 08:45:25 +01:00
|
|
|
let hashes = if is_valid_function_name(&id) {
|
2022-02-28 07:37:46 +01:00
|
|
|
hash.into()
|
|
|
|
} else {
|
|
|
|
FnCallHashes::from_native(hash)
|
|
|
|
};
|
|
|
|
|
|
|
|
args.shrink_to_fit();
|
|
|
|
|
|
|
|
return Ok(FnCallExpr {
|
2022-08-13 12:07:42 +02:00
|
|
|
name: state.get_interned_string(id),
|
2022-02-28 07:37:46 +01:00
|
|
|
capture_parent_scope,
|
2022-11-28 16:24:22 +01:00
|
|
|
op_token: NO_TOKEN,
|
|
|
|
namespace: _namespace,
|
2022-02-28 07:37:46 +01:00
|
|
|
hashes,
|
|
|
|
args,
|
|
|
|
}
|
|
|
|
.into_fn_call_expr(settings.pos));
|
|
|
|
}
|
|
|
|
// id(...args,
|
|
|
|
(Token::Comma, ..) => {
|
|
|
|
eat_token(input, Token::Comma);
|
|
|
|
}
|
|
|
|
// id(...args <EOF>
|
|
|
|
(Token::EOF, pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::RightParen.into(),
|
2022-08-11 13:01:23 +02:00
|
|
|
format!("to close the arguments list of this function call '{id}'"),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
|
|
|
.into_err(*pos))
|
|
|
|
}
|
|
|
|
// id(...args <error>
|
|
|
|
(Token::LexError(err), pos) => return Err(err.clone().into_err(*pos)),
|
|
|
|
// id(...args ???
|
|
|
|
(.., pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::Comma.into(),
|
2022-08-11 13:01:23 +02:00
|
|
|
format!("to separate the arguments to function call '{id}'"),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
|
|
|
.into_err(*pos))
|
|
|
|
}
|
2020-03-05 18:05:02 +01:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse an indexing chain.
|
|
|
|
/// Indexing binds to the right, so this call parses all possible levels of indexing following in the input.
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
fn parse_index_chain(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
lhs: Expr,
|
2022-06-11 18:32:12 +02:00
|
|
|
options: ASTFlags,
|
2022-05-19 08:41:48 +02:00
|
|
|
check_index_type: bool,
|
2022-02-28 07:37:46 +01:00
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Expr> {
|
|
|
|
let mut settings = settings;
|
2021-11-16 16:13:53 +01:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
let idx_expr = self.parse_expr(input, state, lib, settings.level_up()?)?;
|
2020-03-09 03:41:17 +01:00
|
|
|
|
2022-05-17 05:06:34 +02:00
|
|
|
// Check types of indexing that cannot be overridden
|
|
|
|
// - arrays, maps, strings, bit-fields
|
|
|
|
match lhs {
|
2022-05-19 08:41:48 +02:00
|
|
|
_ if !check_index_type => (),
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2022-05-17 05:06:34 +02:00
|
|
|
Expr::Map(..) => match idx_expr {
|
|
|
|
// lhs[int]
|
|
|
|
Expr::IntegerConstant(..) => {
|
2022-02-28 07:37:46 +01:00
|
|
|
return Err(PERR::MalformedIndexExpr(
|
2022-05-17 05:06:34 +02:00
|
|
|
"Object map expects string index, not a number".into(),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
2022-05-17 05:06:34 +02:00
|
|
|
.into_err(idx_expr.start_position()))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2022-05-17 05:06:34 +02:00
|
|
|
// lhs[string]
|
|
|
|
Expr::StringConstant(..) | Expr::InterpolatedString(..) => (),
|
|
|
|
|
|
|
|
// lhs[float]
|
2022-02-28 07:37:46 +01:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
Expr::FloatConstant(..) => {
|
|
|
|
return Err(PERR::MalformedIndexExpr(
|
2022-05-17 05:06:34 +02:00
|
|
|
"Object map expects string index, not a float".into(),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
2022-05-17 05:06:34 +02:00
|
|
|
.into_err(idx_expr.start_position()))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2022-05-17 05:06:34 +02:00
|
|
|
// lhs[char]
|
|
|
|
Expr::CharConstant(..) => {
|
2022-02-28 07:37:46 +01:00
|
|
|
return Err(PERR::MalformedIndexExpr(
|
2022-05-17 05:06:34 +02:00
|
|
|
"Object map expects string index, not a character".into(),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
2022-05-17 05:06:34 +02:00
|
|
|
.into_err(idx_expr.start_position()))
|
|
|
|
}
|
|
|
|
// lhs[()]
|
|
|
|
Expr::Unit(..) => {
|
|
|
|
return Err(PERR::MalformedIndexExpr(
|
|
|
|
"Object map expects string index, not ()".into(),
|
|
|
|
)
|
|
|
|
.into_err(idx_expr.start_position()))
|
|
|
|
}
|
|
|
|
// lhs[??? && ???], lhs[??? || ???], lhs[true], lhs[false]
|
|
|
|
Expr::And(..) | Expr::Or(..) | Expr::BoolConstant(..) => {
|
|
|
|
return Err(PERR::MalformedIndexExpr(
|
|
|
|
"Object map expects string index, not a boolean".into(),
|
|
|
|
)
|
|
|
|
.into_err(idx_expr.start_position()))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
|
2022-05-17 05:06:34 +02:00
|
|
|
Expr::IntegerConstant(..)
|
|
|
|
| Expr::Array(..)
|
|
|
|
| Expr::StringConstant(..)
|
|
|
|
| Expr::InterpolatedString(..) => match idx_expr {
|
|
|
|
// lhs[int]
|
|
|
|
Expr::IntegerConstant(..) => (),
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-05-17 05:06:34 +02:00
|
|
|
// lhs[string]
|
|
|
|
Expr::StringConstant(..) | Expr::InterpolatedString(..) => {
|
2022-02-28 07:37:46 +01:00
|
|
|
return Err(PERR::MalformedIndexExpr(
|
2022-05-17 05:06:34 +02:00
|
|
|
"Array, string or bit-field expects numeric index, not a string".into(),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
|
|
|
.into_err(idx_expr.start_position()))
|
|
|
|
}
|
2022-05-17 05:06:34 +02:00
|
|
|
// lhs[float]
|
2022-02-28 07:37:46 +01:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
Expr::FloatConstant(..) => {
|
|
|
|
return Err(PERR::MalformedIndexExpr(
|
2022-05-17 05:06:34 +02:00
|
|
|
"Array, string or bit-field expects integer index, not a float".into(),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
2022-05-17 05:06:34 +02:00
|
|
|
.into_err(idx_expr.start_position()))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2022-05-17 05:06:34 +02:00
|
|
|
// lhs[char]
|
|
|
|
Expr::CharConstant(..) => {
|
2022-02-28 07:37:46 +01:00
|
|
|
return Err(PERR::MalformedIndexExpr(
|
2022-05-17 05:06:34 +02:00
|
|
|
"Array, string or bit-field expects integer index, not a character".into(),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
2022-05-17 05:06:34 +02:00
|
|
|
.into_err(idx_expr.start_position()))
|
|
|
|
}
|
|
|
|
// lhs[()]
|
|
|
|
Expr::Unit(..) => {
|
|
|
|
return Err(PERR::MalformedIndexExpr(
|
|
|
|
"Array, string or bit-field expects integer index, not ()".into(),
|
|
|
|
)
|
|
|
|
.into_err(idx_expr.start_position()))
|
|
|
|
}
|
|
|
|
// lhs[??? && ???], lhs[??? || ???], lhs[true], lhs[false]
|
|
|
|
Expr::And(..) | Expr::Or(..) | Expr::BoolConstant(..) => {
|
|
|
|
return Err(PERR::MalformedIndexExpr(
|
|
|
|
"Array, string or bit-field expects integer index, not a boolean".into(),
|
|
|
|
)
|
|
|
|
.into_err(idx_expr.start_position()))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
},
|
2020-03-29 17:53:35 +02:00
|
|
|
_ => (),
|
2020-03-09 03:41:17 +01:00
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Check if there is a closing bracket
|
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
(Token::RightBracket, ..) => {
|
|
|
|
eat_token(input, Token::RightBracket);
|
2020-04-26 12:04:07 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Any more indexing following?
|
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
// If another indexing level, right-bind it
|
2022-07-27 12:04:59 +02:00
|
|
|
(Token::LeftBracket | Token::QuestionBracket, ..) => {
|
2022-06-11 18:32:12 +02:00
|
|
|
let (token, pos) = input.next().expect(NEVER_ENDS);
|
2022-02-28 07:37:46 +01:00
|
|
|
let prev_pos = settings.pos;
|
2022-06-11 18:32:12 +02:00
|
|
|
settings.pos = pos;
|
2022-11-30 07:11:09 +01:00
|
|
|
let settings = settings.level_up()?;
|
2022-02-28 07:37:46 +01:00
|
|
|
// Recursively parse the indexing chain, right-binding each
|
2022-11-30 07:11:09 +01:00
|
|
|
let options = match token {
|
|
|
|
Token::LeftBracket => ASTFlags::NONE,
|
|
|
|
Token::QuestionBracket => ASTFlags::NEGATED,
|
|
|
|
_ => unreachable!("`[` or `?[`"),
|
|
|
|
};
|
2022-02-28 07:37:46 +01:00
|
|
|
let idx_expr = self.parse_index_chain(
|
2022-11-30 07:11:09 +01:00
|
|
|
input, state, lib, idx_expr, options, false, settings,
|
2022-02-28 07:37:46 +01:00
|
|
|
)?;
|
|
|
|
// Indexing binds to right
|
|
|
|
Ok(Expr::Index(
|
|
|
|
BinaryExpr { lhs, rhs: idx_expr }.into(),
|
2022-06-11 18:32:12 +02:00
|
|
|
options,
|
2022-02-28 07:37:46 +01:00
|
|
|
prev_pos,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
// Otherwise terminate the indexing chain
|
|
|
|
_ => Ok(Expr::Index(
|
2021-06-29 12:25:20 +02:00
|
|
|
BinaryExpr { lhs, rhs: idx_expr }.into(),
|
2022-06-11 18:32:12 +02:00
|
|
|
options | ASTFlags::BREAK,
|
2022-02-28 07:37:46 +01:00
|
|
|
settings.pos,
|
|
|
|
)),
|
2020-04-26 12:04:07 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
(Token::LexError(err), pos) => Err(err.clone().into_err(*pos)),
|
|
|
|
(.., pos) => Err(PERR::MissingToken(
|
|
|
|
Token::RightBracket.into(),
|
|
|
|
"for a matching [ in this index expression".into(),
|
|
|
|
)
|
|
|
|
.into_err(*pos)),
|
2020-03-05 18:05:02 +01:00
|
|
|
}
|
2020-03-09 03:41:17 +01:00
|
|
|
}
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse an array literal.
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
fn parse_array_literal(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Expr> {
|
|
|
|
// [ ...
|
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = eat_token(input, Token::LeftBracket);
|
2016-03-26 18:46:28 +01:00
|
|
|
|
2022-08-27 10:26:41 +02:00
|
|
|
let mut array = StaticVec::new_const();
|
2020-11-13 11:32:18 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
loop {
|
|
|
|
const MISSING_RBRACKET: &str = "to end this array literal";
|
2020-03-02 15:13:14 +01:00
|
|
|
|
2022-08-27 10:26:41 +02:00
|
|
|
if self.max_array_size() > 0 && array.len() >= self.max_array_size() {
|
2022-02-28 07:37:46 +01:00
|
|
|
return Err(PERR::LiteralTooLarge(
|
2022-11-22 16:30:43 +01:00
|
|
|
"Size of array literal".into(),
|
2022-02-28 07:37:46 +01:00
|
|
|
self.max_array_size(),
|
2020-11-13 11:32:18 +01:00
|
|
|
)
|
2022-02-28 07:37:46 +01:00
|
|
|
.into_err(input.peek().expect(NEVER_ENDS).1));
|
2020-11-13 11:32:18 +01:00
|
|
|
}
|
2020-06-16 16:14:46 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
(Token::RightBracket, ..) => {
|
|
|
|
eat_token(input, Token::RightBracket);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(Token::EOF, pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::RightBracket.into(),
|
|
|
|
MISSING_RBRACKET.into(),
|
|
|
|
)
|
|
|
|
.into_err(*pos))
|
|
|
|
}
|
|
|
|
_ => {
|
2022-11-24 10:08:43 +01:00
|
|
|
let expr = self.parse_expr(input, state, lib, settings.level_up()?)?;
|
2022-08-27 10:26:41 +02:00
|
|
|
array.push(expr);
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2020-06-16 16:14:46 +02:00
|
|
|
}
|
2016-03-26 18:46:28 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
(Token::Comma, ..) => {
|
|
|
|
eat_token(input, Token::Comma);
|
|
|
|
}
|
|
|
|
(Token::RightBracket, ..) => (),
|
|
|
|
(Token::EOF, pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::RightBracket.into(),
|
|
|
|
MISSING_RBRACKET.into(),
|
|
|
|
)
|
|
|
|
.into_err(*pos))
|
|
|
|
}
|
|
|
|
(Token::LexError(err), pos) => return Err(err.clone().into_err(*pos)),
|
|
|
|
(.., pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::Comma.into(),
|
|
|
|
"to separate the items of this array literal".into(),
|
|
|
|
)
|
|
|
|
.into_err(*pos))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-03-29 05:36:02 +02:00
|
|
|
|
2022-08-27 10:26:41 +02:00
|
|
|
array.shrink_to_fit();
|
2016-03-26 18:46:28 +01:00
|
|
|
|
2022-08-27 10:26:41 +02:00
|
|
|
Ok(Expr::Array(array.into(), settings.pos))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse a map literal.
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
fn parse_map_literal(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Expr> {
|
|
|
|
// #{ ...
|
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = eat_token(input, Token::MapStart);
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let mut map = StaticVec::<(Ident, Expr)>::new();
|
|
|
|
let mut template = BTreeMap::<Identifier, crate::Dynamic>::new();
|
2020-04-15 15:41:28 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
loop {
|
|
|
|
const MISSING_RBRACE: &str = "to end this object map literal";
|
2020-06-16 16:14:46 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
(Token::RightBrace, ..) => {
|
|
|
|
eat_token(input, Token::RightBrace);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(Token::EOF, pos) => {
|
|
|
|
return Err(
|
|
|
|
PERR::MissingToken(Token::RightBrace.into(), MISSING_RBRACE.into())
|
|
|
|
.into_err(*pos),
|
|
|
|
)
|
2020-11-13 11:32:18 +01:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
_ => (),
|
2020-11-13 11:32:18 +01:00
|
|
|
}
|
2020-07-17 04:18:07 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let (name, pos) = match input.next().expect(NEVER_ENDS) {
|
2022-11-23 04:36:30 +01:00
|
|
|
(Token::Identifier(..), pos)
|
|
|
|
if settings.has_flag(ParseSettingFlags::DISALLOW_UNQUOTED_MAP_PROPERTIES) =>
|
|
|
|
{
|
2022-09-29 16:46:59 +02:00
|
|
|
return Err(PERR::PropertyExpected.into_err(pos))
|
|
|
|
}
|
2022-04-21 04:04:46 +02:00
|
|
|
(Token::Identifier(s) | Token::StringConstant(s), pos) => {
|
2022-09-25 17:03:18 +02:00
|
|
|
if map.iter().any(|(p, ..)| **p == *s) {
|
2022-02-28 07:37:46 +01:00
|
|
|
return Err(PERR::DuplicatedProperty(s.to_string()).into_err(pos));
|
|
|
|
}
|
2022-09-25 17:03:18 +02:00
|
|
|
(*s, pos)
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
(Token::InterpolatedString(..), pos) => {
|
|
|
|
return Err(PERR::PropertyExpected.into_err(pos))
|
|
|
|
}
|
2022-10-30 08:45:25 +01:00
|
|
|
(Token::Reserved(s), pos) if is_valid_identifier(s.as_str()) => {
|
2022-02-28 07:37:46 +01:00
|
|
|
return Err(PERR::Reserved(s.to_string()).into_err(pos));
|
|
|
|
}
|
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
|
|
|
(Token::EOF, pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::RightBrace.into(),
|
|
|
|
MISSING_RBRACE.into(),
|
|
|
|
)
|
|
|
|
.into_err(pos));
|
|
|
|
}
|
|
|
|
(.., pos) if map.is_empty() => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::RightBrace.into(),
|
|
|
|
MISSING_RBRACE.into(),
|
|
|
|
)
|
|
|
|
.into_err(pos));
|
|
|
|
}
|
|
|
|
(.., pos) => return Err(PERR::PropertyExpected.into_err(pos)),
|
|
|
|
};
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::Colon, ..) => (),
|
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
|
|
|
(.., pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::Colon.into(),
|
2022-08-11 13:01:23 +02:00
|
|
|
format!("to follow the property '{name}' in this object map literal"),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
|
|
|
.into_err(pos))
|
|
|
|
}
|
|
|
|
};
|
2020-07-17 04:18:07 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
if self.max_map_size() > 0 && map.len() >= self.max_map_size() {
|
|
|
|
return Err(PERR::LiteralTooLarge(
|
2022-11-22 16:30:43 +01:00
|
|
|
"Number of properties in object map literal".into(),
|
2022-02-28 07:37:46 +01:00
|
|
|
self.max_map_size(),
|
2020-06-16 16:14:46 +02:00
|
|
|
)
|
2022-02-28 07:37:46 +01:00
|
|
|
.into_err(input.peek().expect(NEVER_ENDS).1));
|
2020-06-16 16:14:46 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
let expr = self.parse_expr(input, state, lib, settings.level_up()?)?;
|
2022-02-28 07:37:46 +01:00
|
|
|
template.insert(name.clone(), crate::Dynamic::UNIT);
|
2022-08-13 12:07:42 +02:00
|
|
|
|
|
|
|
let name = state.get_interned_string(name);
|
2022-02-28 07:37:46 +01:00
|
|
|
map.push((Ident { name, pos }, expr));
|
|
|
|
|
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
(Token::Comma, ..) => {
|
|
|
|
eat_token(input, Token::Comma);
|
|
|
|
}
|
|
|
|
(Token::RightBrace, ..) => (),
|
|
|
|
(Token::Identifier(..), pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::Comma.into(),
|
|
|
|
"to separate the items of this object map literal".into(),
|
|
|
|
)
|
|
|
|
.into_err(*pos))
|
|
|
|
}
|
|
|
|
(Token::LexError(err), pos) => return Err(err.clone().into_err(*pos)),
|
|
|
|
(.., pos) => {
|
|
|
|
return Err(
|
|
|
|
PERR::MissingToken(Token::RightBrace.into(), MISSING_RBRACE.into())
|
|
|
|
.into_err(*pos),
|
|
|
|
)
|
|
|
|
}
|
2020-03-29 17:53:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
map.shrink_to_fit();
|
2021-03-29 05:36:02 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(Expr::Map((map, template).into(), settings.pos))
|
|
|
|
}
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse a switch expression.
|
|
|
|
fn parse_switch(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Stmt> {
|
|
|
|
// switch ...
|
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = eat_token(input, Token::Switch);
|
2020-12-28 02:49:54 +01:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
let item = self.parse_expr(input, state, lib, settings.level_up()?)?;
|
2020-11-13 11:32:18 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::LeftBrace, ..) => (),
|
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
|
|
|
(.., pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::LeftBrace.into(),
|
|
|
|
"to start a switch block".into(),
|
|
|
|
)
|
|
|
|
.into_err(pos))
|
|
|
|
}
|
2020-11-13 11:32:18 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 07:33:53 +02:00
|
|
|
let mut expressions = StaticVec::<ConditionalExpr>::new();
|
2022-07-18 07:40:41 +02:00
|
|
|
let mut cases = BTreeMap::<u64, CaseBlocksList>::new();
|
2022-07-04 11:42:24 +02:00
|
|
|
let mut ranges = StaticVec::<RangeCase>::new();
|
2022-07-18 16:30:09 +02:00
|
|
|
let mut def_case = None;
|
|
|
|
let mut def_case_pos = Position::NONE;
|
2020-11-13 11:32:18 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
loop {
|
|
|
|
const MISSING_RBRACE: &str = "to end this switch block";
|
2020-11-13 11:32:18 +01:00
|
|
|
|
2022-07-04 11:42:24 +02:00
|
|
|
let (case_expr_list, condition) = match input.peek().expect(NEVER_ENDS) {
|
2022-02-28 07:37:46 +01:00
|
|
|
(Token::RightBrace, ..) => {
|
|
|
|
eat_token(input, Token::RightBrace);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(Token::EOF, pos) => {
|
|
|
|
return Err(
|
|
|
|
PERR::MissingToken(Token::RightBrace.into(), MISSING_RBRACE.into())
|
|
|
|
.into_err(*pos),
|
|
|
|
)
|
|
|
|
}
|
2022-07-18 16:30:09 +02:00
|
|
|
(Token::Underscore, pos) if def_case.is_none() => {
|
|
|
|
def_case_pos = *pos;
|
2022-02-28 07:37:46 +01:00
|
|
|
eat_token(input, Token::Underscore);
|
2021-04-16 07:28:36 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let (if_clause, if_pos) = match_token(input, Token::If);
|
2021-04-16 07:28:36 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
if if_clause {
|
|
|
|
return Err(PERR::WrongSwitchCaseCondition.into_err(if_pos));
|
|
|
|
}
|
|
|
|
|
2022-07-27 12:04:59 +02:00
|
|
|
(
|
|
|
|
StaticVec::default(),
|
|
|
|
Expr::BoolConstant(true, Position::NONE),
|
|
|
|
)
|
2021-04-16 07:28:36 +02:00
|
|
|
}
|
2022-07-18 16:30:09 +02:00
|
|
|
_ if def_case.is_some() => {
|
|
|
|
return Err(PERR::WrongSwitchDefaultCase.into_err(def_case_pos))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2021-12-15 05:06:17 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
_ => {
|
2022-07-04 11:42:24 +02:00
|
|
|
let mut case_expr_list = StaticVec::new();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let filter = state.expr_filter;
|
|
|
|
state.expr_filter = |t| t != &Token::Pipe;
|
2022-11-24 10:08:43 +01:00
|
|
|
let expr = self.parse_expr(input, state, lib, settings.level_up()?);
|
2022-07-04 11:42:24 +02:00
|
|
|
state.expr_filter = filter;
|
|
|
|
|
|
|
|
match expr {
|
|
|
|
Ok(expr) => case_expr_list.push(expr),
|
|
|
|
Err(err) => {
|
|
|
|
return Err(PERR::ExprExpected("literal".into()).into_err(err.1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !match_token(input, Token::Pipe).0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-04-16 06:04:33 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let condition = if match_token(input, Token::If).0 {
|
2022-04-19 15:45:11 +02:00
|
|
|
ensure_not_statement_expr(input, "a boolean")?;
|
|
|
|
let guard = self
|
2022-11-24 10:08:43 +01:00
|
|
|
.parse_expr(input, state, lib, settings.level_up()?)?
|
2022-04-19 15:45:11 +02:00
|
|
|
.ensure_bool_expr()?;
|
|
|
|
ensure_not_assignment(input)?;
|
|
|
|
guard
|
2022-02-28 07:37:46 +01:00
|
|
|
} else {
|
2022-04-19 10:20:43 +02:00
|
|
|
Expr::BoolConstant(true, Position::NONE)
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
2022-07-04 11:42:24 +02:00
|
|
|
(case_expr_list, condition)
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
};
|
2021-12-12 05:33:22 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::DoubleArrow, ..) => (),
|
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
|
|
|
(.., pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::DoubleArrow.into(),
|
2022-11-22 16:30:43 +01:00
|
|
|
"in this switch case".into(),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
|
|
|
.into_err(pos))
|
2021-12-15 05:06:17 +01:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
2020-11-13 11:32:18 +01:00
|
|
|
|
2022-11-23 04:36:30 +01:00
|
|
|
let (action_expr, need_comma) =
|
|
|
|
if !settings.has_flag(ParseSettingFlags::DISALLOW_STATEMENTS_IN_BLOCKS) {
|
2022-11-24 10:08:43 +01:00
|
|
|
let stmt = self.parse_stmt(input, state, lib, settings.level_up()?)?;
|
2022-11-23 04:36:30 +01:00
|
|
|
let need_comma = !stmt.is_self_terminated();
|
2022-08-26 17:10:58 +02:00
|
|
|
|
2022-11-23 04:36:30 +01:00
|
|
|
let stmt_block: StmtBlock = stmt.into();
|
|
|
|
(Expr::Stmt(stmt_block.into()), need_comma)
|
|
|
|
} else {
|
|
|
|
(
|
2022-11-24 10:08:43 +01:00
|
|
|
self.parse_expr(input, state, lib, settings.level_up()?)?,
|
2022-11-23 04:36:30 +01:00
|
|
|
true,
|
|
|
|
)
|
|
|
|
};
|
2022-07-18 02:54:10 +02:00
|
|
|
let has_condition = !matches!(condition, Expr::BoolConstant(true, ..));
|
2021-12-22 03:48:19 +01:00
|
|
|
|
2022-08-26 17:10:58 +02:00
|
|
|
expressions.push((condition, action_expr).into());
|
2022-07-19 07:33:53 +02:00
|
|
|
let index = expressions.len() - 1;
|
2022-07-04 11:42:24 +02:00
|
|
|
|
2022-07-27 12:04:59 +02:00
|
|
|
if case_expr_list.is_empty() {
|
|
|
|
def_case = Some(index);
|
|
|
|
} else {
|
2022-07-04 11:42:24 +02:00
|
|
|
for expr in case_expr_list {
|
|
|
|
let value = expr.get_literal_value().ok_or_else(|| {
|
2022-11-22 16:30:43 +01:00
|
|
|
PERR::ExprExpected("a literal".into()).into_err(expr.start_position())
|
2022-07-04 11:42:24 +02:00
|
|
|
})?;
|
|
|
|
|
|
|
|
let mut range_value: Option<RangeCase> = None;
|
2021-12-22 03:48:19 +01:00
|
|
|
|
2022-07-04 11:42:24 +02:00
|
|
|
let guard = value.read_lock::<ExclusiveRange>();
|
|
|
|
if let Some(range) = guard {
|
|
|
|
range_value = Some(range.clone().into());
|
|
|
|
} else if let Some(range) = value.read_lock::<InclusiveRange>() {
|
|
|
|
range_value = Some(range.clone().into());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(mut r) = range_value {
|
|
|
|
if !r.is_empty() {
|
2022-07-18 02:54:10 +02:00
|
|
|
// Do not unroll ranges if there are previous non-unrolled ranges
|
|
|
|
if !has_condition && ranges.is_empty() && r.len() <= SMALL_SWITCH_RANGE
|
|
|
|
{
|
|
|
|
// Unroll small range
|
|
|
|
for n in r {
|
|
|
|
let hasher = &mut get_hasher();
|
|
|
|
Dynamic::from_int(n).hash(hasher);
|
2022-07-18 07:40:41 +02:00
|
|
|
cases
|
|
|
|
.entry(hasher.finish())
|
|
|
|
.and_modify(|cases| cases.push(index))
|
|
|
|
.or_insert_with(|| [index].into());
|
2022-07-18 02:54:10 +02:00
|
|
|
}
|
2022-07-04 11:42:24 +02:00
|
|
|
} else {
|
|
|
|
// Other range
|
|
|
|
r.set_index(index);
|
|
|
|
ranges.push(r);
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2022-02-16 10:51:14 +01:00
|
|
|
}
|
2022-07-04 11:42:24 +02:00
|
|
|
continue;
|
2021-12-22 03:48:19 +01:00
|
|
|
}
|
2022-07-04 11:42:24 +02:00
|
|
|
|
2022-11-09 05:44:57 +01:00
|
|
|
if value.is_int() && !ranges.is_empty() {
|
2022-07-04 11:42:24 +02:00
|
|
|
return Err(PERR::WrongSwitchIntegerCase.into_err(expr.start_position()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let hasher = &mut get_hasher();
|
|
|
|
value.hash(hasher);
|
|
|
|
let hash = hasher.finish();
|
|
|
|
|
2022-07-18 07:40:41 +02:00
|
|
|
cases
|
|
|
|
.entry(hash)
|
|
|
|
.and_modify(|cases| cases.push(index))
|
|
|
|
.or_insert_with(|| [index].into());
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2022-07-04 11:42:24 +02:00
|
|
|
}
|
2020-11-13 11:32:18 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
(Token::Comma, ..) => {
|
|
|
|
eat_token(input, Token::Comma);
|
|
|
|
}
|
|
|
|
(Token::RightBrace, ..) => (),
|
|
|
|
(Token::EOF, pos) => {
|
|
|
|
return Err(
|
|
|
|
PERR::MissingToken(Token::RightParen.into(), MISSING_RBRACE.into())
|
|
|
|
.into_err(*pos),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
(Token::LexError(err), pos) => return Err(err.clone().into_err(*pos)),
|
|
|
|
(.., pos) if need_comma => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::Comma.into(),
|
|
|
|
"to separate the items in this switch block".into(),
|
|
|
|
)
|
|
|
|
.into_err(*pos))
|
|
|
|
}
|
|
|
|
_ => (),
|
2020-11-13 11:32:18 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-07-18 07:40:41 +02:00
|
|
|
let cases = SwitchCasesCollection {
|
2022-07-19 07:33:53 +02:00
|
|
|
expressions,
|
2022-02-28 07:37:46 +01:00
|
|
|
cases,
|
2022-07-04 11:42:24 +02:00
|
|
|
def_case,
|
2022-02-28 07:37:46 +01:00
|
|
|
ranges,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Stmt::Switch((item, cases).into(), settings.pos))
|
2020-11-13 11:32:18 +01:00
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse a primary expression.
|
|
|
|
fn parse_primary(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
2022-08-29 16:26:07 +02:00
|
|
|
is_property: bool,
|
2022-02-28 07:37:46 +01:00
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Expr> {
|
|
|
|
let (token, token_pos) = input.peek().expect(NEVER_ENDS);
|
2021-06-29 12:25:20 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = *token_pos;
|
2020-11-13 11:32:18 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let root_expr = match token {
|
2022-07-04 11:42:24 +02:00
|
|
|
_ if !(state.expr_filter)(token) => {
|
2022-11-22 16:30:43 +01:00
|
|
|
return Err(LexError::UnexpectedInput(token.to_string()).into_err(settings.pos))
|
2022-07-04 11:42:24 +02:00
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Token::EOF => return Err(PERR::UnexpectedEOF.into_err(settings.pos)),
|
2020-12-27 09:50:48 +01:00
|
|
|
|
2022-04-21 04:04:46 +02:00
|
|
|
Token::Unit => {
|
|
|
|
input.next();
|
|
|
|
Expr::Unit(settings.pos)
|
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Token::IntegerConstant(..)
|
|
|
|
| Token::CharConstant(..)
|
|
|
|
| Token::StringConstant(..)
|
|
|
|
| Token::True
|
|
|
|
| Token::False => match input.next().expect(NEVER_ENDS).0 {
|
|
|
|
Token::IntegerConstant(x) => Expr::IntegerConstant(x, settings.pos),
|
|
|
|
Token::CharConstant(c) => Expr::CharConstant(c, settings.pos),
|
|
|
|
Token::StringConstant(s) => {
|
2022-09-25 17:03:18 +02:00
|
|
|
Expr::StringConstant(state.get_interned_string(*s), settings.pos)
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
Token::True => Expr::BoolConstant(true, settings.pos),
|
|
|
|
Token::False => Expr::BoolConstant(false, settings.pos),
|
|
|
|
token => unreachable!("token is {:?}", token),
|
|
|
|
},
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
Token::FloatConstant(x) => {
|
|
|
|
let x = *x;
|
2022-04-21 04:04:46 +02:00
|
|
|
input.next();
|
2022-02-28 07:37:46 +01:00
|
|
|
Expr::FloatConstant(x, settings.pos)
|
|
|
|
}
|
|
|
|
#[cfg(feature = "decimal")]
|
|
|
|
Token::DecimalConstant(x) => {
|
2022-09-25 17:03:18 +02:00
|
|
|
let x = (**x).into();
|
2022-04-21 04:04:46 +02:00
|
|
|
input.next();
|
2022-02-28 07:37:46 +01:00
|
|
|
Expr::DynamicConstant(Box::new(x), settings.pos)
|
2020-12-27 09:50:48 +01:00
|
|
|
}
|
2021-04-04 18:05:56 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// { - block statement as expression
|
2022-11-23 04:36:30 +01:00
|
|
|
Token::LeftBrace if settings.has_option(LangOptions::STMT_EXPR) => {
|
2022-11-24 10:08:43 +01:00
|
|
|
match self.parse_block(input, state, lib, settings.level_up()?)? {
|
2022-02-28 07:37:46 +01:00
|
|
|
block @ Stmt::Block(..) => Expr::Stmt(Box::new(block.into())),
|
|
|
|
stmt => unreachable!("Stmt::Block expected but gets {:?}", stmt),
|
|
|
|
}
|
2021-04-04 18:05:56 +02:00
|
|
|
}
|
2022-04-21 04:04:46 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// ( - grouped expression
|
2022-11-24 10:08:43 +01:00
|
|
|
Token::LeftParen => self.parse_paren_expr(input, state, lib, settings.level_up()?)?,
|
2020-08-08 16:59:05 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// If statement is allowed to act as expressions
|
2022-11-23 04:36:30 +01:00
|
|
|
Token::If if settings.has_option(LangOptions::IF_EXPR) => Expr::Stmt(Box::new(
|
2022-11-24 10:08:43 +01:00
|
|
|
self.parse_if(input, state, lib, settings.level_up()?)?
|
2022-02-28 07:37:46 +01:00
|
|
|
.into(),
|
|
|
|
)),
|
2022-10-29 06:09:18 +02:00
|
|
|
// Loops are allowed to act as expressions
|
2022-11-23 04:36:30 +01:00
|
|
|
Token::While | Token::Loop if settings.has_option(LangOptions::LOOP_EXPR) => {
|
2022-10-29 06:09:18 +02:00
|
|
|
Expr::Stmt(Box::new(
|
2022-11-24 10:08:43 +01:00
|
|
|
self.parse_while_loop(input, state, lib, settings.level_up()?)?
|
2022-10-29 06:09:18 +02:00
|
|
|
.into(),
|
|
|
|
))
|
|
|
|
}
|
2022-11-23 04:36:30 +01:00
|
|
|
Token::Do if settings.has_option(LangOptions::LOOP_EXPR) => Expr::Stmt(Box::new(
|
2022-11-24 10:08:43 +01:00
|
|
|
self.parse_do(input, state, lib, settings.level_up()?)?
|
2022-10-29 06:09:18 +02:00
|
|
|
.into(),
|
|
|
|
)),
|
2022-11-23 04:36:30 +01:00
|
|
|
Token::For if settings.has_option(LangOptions::LOOP_EXPR) => Expr::Stmt(Box::new(
|
2022-11-24 10:08:43 +01:00
|
|
|
self.parse_for(input, state, lib, settings.level_up()?)?
|
2022-11-23 04:36:30 +01:00
|
|
|
.into(),
|
|
|
|
)),
|
2022-02-28 07:37:46 +01:00
|
|
|
// Switch statement is allowed to act as expressions
|
2022-11-23 04:36:30 +01:00
|
|
|
Token::Switch if settings.has_option(LangOptions::SWITCH_EXPR) => Expr::Stmt(Box::new(
|
2022-11-24 10:08:43 +01:00
|
|
|
self.parse_switch(input, state, lib, settings.level_up()?)?
|
2022-11-23 04:36:30 +01:00
|
|
|
.into(),
|
|
|
|
)),
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
// | ...
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
2022-11-23 04:36:30 +01:00
|
|
|
Token::Pipe | Token::Or if settings.has_option(LangOptions::ANON_FN) => {
|
2022-08-14 08:32:16 +02:00
|
|
|
// Build new parse state
|
2022-11-24 08:10:17 +01:00
|
|
|
let new_interner = &mut StringsInterner::new();
|
2022-11-25 13:42:16 +01:00
|
|
|
let new_state = &mut ParseState::new(
|
|
|
|
state.scope,
|
|
|
|
new_interner,
|
|
|
|
state.tokenizer_control.clone(),
|
|
|
|
);
|
2022-06-09 12:22:53 +02:00
|
|
|
|
2022-11-24 08:10:17 +01:00
|
|
|
// We move the strings interner to the new parse state object by swapping it...
|
|
|
|
std::mem::swap(state.interned_strings, new_state.interned_strings);
|
|
|
|
|
2022-06-09 12:22:53 +02:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-08-09 15:35:45 +02:00
|
|
|
{
|
2022-08-09 15:46:34 +02:00
|
|
|
// Do not allow storing an index to a globally-imported module
|
|
|
|
// just in case the function is separated from this `AST`.
|
|
|
|
//
|
|
|
|
// Keep them in `global_imports` instead so that strict variables
|
|
|
|
// mode will not complain.
|
2022-08-09 15:35:45 +02:00
|
|
|
new_state.global_imports.clone_from(&state.global_imports);
|
2022-08-09 15:46:34 +02:00
|
|
|
new_state
|
|
|
|
.global_imports
|
2022-11-25 05:14:40 +01:00
|
|
|
.get_or_insert_with(Default::default)
|
2022-11-25 16:03:20 +01:00
|
|
|
.extend(state.imports.as_deref().into_iter().flatten().cloned());
|
2022-08-09 15:35:45 +02:00
|
|
|
}
|
2020-12-27 09:50:48 +01:00
|
|
|
|
2022-11-30 07:11:09 +01:00
|
|
|
// Brand new options
|
2022-11-23 04:36:30 +01:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
2022-11-30 07:11:09 +01:00
|
|
|
let options = self.options & !LangOptions::STRICT_VAR; // a capturing closure can access variables not defined locally, so turn off Strict Variables mode
|
2022-11-23 04:36:30 +01:00
|
|
|
#[cfg(feature = "no_closure")]
|
|
|
|
let options = self.options | (settings.options & LangOptions::STRICT_VAR);
|
|
|
|
|
2022-11-30 07:11:09 +01:00
|
|
|
// Brand new flags, turn on function scope
|
|
|
|
let flags = ParseSettingFlags::FN_SCOPE
|
|
|
|
| (settings.flags
|
|
|
|
& (ParseSettingFlags::DISALLOW_UNQUOTED_MAP_PROPERTIES
|
|
|
|
| ParseSettingFlags::DISALLOW_STATEMENTS_IN_BLOCKS));
|
2022-11-23 04:36:30 +01:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
2022-11-30 07:11:09 +01:00
|
|
|
let flags = flags | ParseSettingFlags::CLOSURE_SCOPE; // turn on closure scope
|
2022-05-19 15:40:22 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let new_settings = ParseSettings {
|
2022-11-23 04:36:30 +01:00
|
|
|
flags,
|
2022-05-19 15:40:22 +02:00
|
|
|
options,
|
2022-02-28 07:37:46 +01:00
|
|
|
..settings
|
|
|
|
};
|
2020-12-27 09:50:48 +01:00
|
|
|
|
2022-11-30 07:11:09 +01:00
|
|
|
let result =
|
|
|
|
self.parse_anon_fn(input, new_state, state, lib, new_settings.level_up()?);
|
2022-08-12 10:34:57 +02:00
|
|
|
|
2022-11-24 08:10:17 +01:00
|
|
|
// Restore the strings interner by swapping it back
|
|
|
|
std::mem::swap(state.interned_strings, new_state.interned_strings);
|
2022-08-12 10:34:57 +02:00
|
|
|
|
2022-11-29 08:50:58 +01:00
|
|
|
let (expr, f) = result?;
|
2020-12-27 09:50:48 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
2022-11-23 09:14:11 +01:00
|
|
|
new_state
|
|
|
|
.external_vars
|
2022-11-25 16:03:20 +01:00
|
|
|
.as_deref()
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
2022-11-23 09:14:11 +01:00
|
|
|
.try_for_each(|Ident { name, pos }| {
|
2022-08-05 17:30:44 +02:00
|
|
|
let (index, is_func) = state.access_var(name, lib, *pos);
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-08-29 16:26:07 +02:00
|
|
|
if !is_func
|
2022-02-28 09:32:08 +01:00
|
|
|
&& index.is_none()
|
2022-11-23 04:36:30 +01:00
|
|
|
&& !settings.has_flag(ParseSettingFlags::CLOSURE_SCOPE)
|
|
|
|
&& settings.has_option(LangOptions::STRICT_VAR)
|
2022-04-22 06:12:36 +02:00
|
|
|
&& !state.scope.contains(name)
|
2022-02-28 09:32:08 +01:00
|
|
|
{
|
2022-02-28 07:37:46 +01:00
|
|
|
// If the parent scope is not inside another capturing closure
|
|
|
|
// then we can conclude that the captured variable doesn't exist.
|
|
|
|
// Under Strict Variables mode, this is not allowed.
|
|
|
|
Err(PERR::VariableUndefined(name.to_string()).into_err(*pos))
|
|
|
|
} else {
|
2022-10-24 17:33:24 +02:00
|
|
|
Ok(())
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2022-11-23 09:14:11 +01:00
|
|
|
})?;
|
2020-12-27 09:50:48 +01:00
|
|
|
|
2022-11-29 08:50:58 +01:00
|
|
|
let hash_script = calc_fn_hash(None, &f.name, f.params.len());
|
|
|
|
lib.insert(hash_script, f.into());
|
2021-04-04 07:13:07 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
expr
|
2021-04-04 07:13:07 +02:00
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Interpolated string
|
|
|
|
Token::InterpolatedString(..) => {
|
|
|
|
let mut segments = StaticVec::<Expr>::new();
|
|
|
|
|
|
|
|
match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::InterpolatedString(s), ..) if s.is_empty() => (),
|
|
|
|
(Token::InterpolatedString(s), pos) => {
|
2022-09-25 17:03:18 +02:00
|
|
|
segments.push(Expr::StringConstant(state.get_interned_string(*s), pos))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
token => {
|
|
|
|
unreachable!("Token::InterpolatedString expected but gets {:?}", token)
|
|
|
|
}
|
2022-02-10 10:55:32 +01:00
|
|
|
}
|
2021-04-04 07:13:07 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
loop {
|
2022-11-24 10:08:43 +01:00
|
|
|
let expr = match self.parse_block(input, state, lib, settings.level_up()?)? {
|
2022-02-28 07:37:46 +01:00
|
|
|
block @ Stmt::Block(..) => Expr::Stmt(Box::new(block.into())),
|
|
|
|
stmt => unreachable!("Stmt::Block expected but gets {:?}", stmt),
|
|
|
|
};
|
|
|
|
match expr {
|
|
|
|
Expr::StringConstant(s, ..) if s.is_empty() => (),
|
|
|
|
_ => segments.push(expr),
|
|
|
|
}
|
2021-04-04 07:13:07 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Make sure to parse the following as text
|
2022-07-25 07:40:23 +02:00
|
|
|
state.tokenizer_control.borrow_mut().is_within_text = true;
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::StringConstant(s), pos) => {
|
|
|
|
if !s.is_empty() {
|
2022-09-25 17:03:18 +02:00
|
|
|
segments
|
|
|
|
.push(Expr::StringConstant(state.get_interned_string(*s), pos));
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
// End the interpolated string if it is terminated by a back-tick.
|
|
|
|
break;
|
2021-04-04 07:13:07 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
(Token::InterpolatedString(s), pos) => {
|
|
|
|
if !s.is_empty() {
|
2022-09-25 17:03:18 +02:00
|
|
|
segments
|
|
|
|
.push(Expr::StringConstant(state.get_interned_string(*s), pos));
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2021-04-04 07:13:07 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
(Token::LexError(err), pos)
|
|
|
|
if matches!(*err, LexError::UnterminatedString) =>
|
|
|
|
{
|
|
|
|
return Err(err.into_err(pos))
|
|
|
|
}
|
|
|
|
(token, ..) => unreachable!(
|
|
|
|
"string within an interpolated string literal expected but gets {:?}",
|
|
|
|
token
|
|
|
|
),
|
2021-04-04 07:13:07 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if segments.is_empty() {
|
2022-08-11 16:56:23 +02:00
|
|
|
Expr::StringConstant(state.get_interned_string(""), settings.pos)
|
2022-02-28 07:37:46 +01:00
|
|
|
} else {
|
|
|
|
segments.shrink_to_fit();
|
|
|
|
Expr::InterpolatedString(segments.into(), settings.pos)
|
2021-04-04 07:13:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Array literal
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
Token::LeftBracket => {
|
2022-11-24 10:08:43 +01:00
|
|
|
self.parse_array_literal(input, state, lib, settings.level_up()?)?
|
2022-02-10 10:55:32 +01:00
|
|
|
}
|
2021-04-04 07:13:07 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Map literal
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-11-24 10:08:43 +01:00
|
|
|
Token::MapStart => self.parse_map_literal(input, state, lib, settings.level_up()?)?,
|
2020-12-27 09:50:48 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Custom syntax.
|
2022-07-05 16:59:03 +02:00
|
|
|
#[cfg(not(feature = "no_custom_syntax"))]
|
2022-02-28 07:37:46 +01:00
|
|
|
Token::Custom(key) | Token::Reserved(key) | Token::Identifier(key)
|
2022-11-24 15:58:42 +01:00
|
|
|
if self
|
|
|
|
.custom_syntax
|
2022-11-25 13:42:16 +01:00
|
|
|
.as_deref()
|
2022-11-24 15:58:42 +01:00
|
|
|
.map_or(false, |m| m.contains_key(&**key)) =>
|
2022-02-28 07:37:46 +01:00
|
|
|
{
|
2022-11-24 15:58:42 +01:00
|
|
|
let (key, syntax) = self
|
|
|
|
.custom_syntax
|
2022-11-25 13:42:16 +01:00
|
|
|
.as_deref()
|
2022-11-24 15:58:42 +01:00
|
|
|
.and_then(|m| m.get_key_value(&**key))
|
|
|
|
.unwrap();
|
2022-02-28 07:37:46 +01:00
|
|
|
let (.., pos) = input.next().expect(NEVER_ENDS);
|
2022-11-30 07:11:09 +01:00
|
|
|
let settings = settings.level_up()?;
|
|
|
|
self.parse_custom_syntax(input, state, lib, settings, key, syntax, pos)?
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2020-12-27 09:50:48 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Identifier
|
|
|
|
Token::Identifier(..) => {
|
2022-11-28 16:24:22 +01:00
|
|
|
let ns = Namespace::NONE;
|
2021-12-16 15:40:10 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let s = match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::Identifier(s), ..) => s,
|
|
|
|
token => unreachable!("Token::Identifier expected but gets {:?}", token),
|
|
|
|
};
|
2022-01-29 04:09:43 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match input.peek().expect(NEVER_ENDS).0 {
|
|
|
|
// Function call
|
2022-04-21 04:04:46 +02:00
|
|
|
Token::LeftParen | Token::Bang | Token::Unit => {
|
2022-02-28 07:37:46 +01:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
{
|
|
|
|
// Once the identifier consumed we must enable next variables capturing
|
|
|
|
state.allow_capture = true;
|
|
|
|
}
|
|
|
|
Expr::Variable(
|
2022-09-25 17:03:18 +02:00
|
|
|
(None, ns, 0, state.get_interned_string(*s)).into(),
|
2022-02-28 07:37:46 +01:00
|
|
|
None,
|
|
|
|
settings.pos,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
// Namespace qualification
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
Token::DoubleColon => {
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
{
|
|
|
|
// Once the identifier consumed we must enable next variables capturing
|
|
|
|
state.allow_capture = true;
|
|
|
|
}
|
2022-09-25 17:03:18 +02:00
|
|
|
let name = state.get_interned_string(*s);
|
2022-08-29 16:26:07 +02:00
|
|
|
Expr::Variable((None, ns, 0, name).into(), None, settings.pos)
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
// Normal variable access
|
|
|
|
_ => {
|
2022-08-05 17:30:44 +02:00
|
|
|
let (index, is_func) = state.access_var(&s, lib, settings.pos);
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-08-29 16:26:07 +02:00
|
|
|
if !is_property
|
|
|
|
&& !is_func
|
2022-04-21 07:21:53 +02:00
|
|
|
&& index.is_none()
|
2022-11-23 04:36:30 +01:00
|
|
|
&& settings.has_option(LangOptions::STRICT_VAR)
|
2022-04-22 06:12:36 +02:00
|
|
|
&& !state.scope.contains(&s)
|
2022-04-21 07:21:53 +02:00
|
|
|
{
|
2022-02-28 07:37:46 +01:00
|
|
|
return Err(
|
|
|
|
PERR::VariableUndefined(s.to_string()).into_err(settings.pos)
|
|
|
|
);
|
|
|
|
}
|
2020-08-22 16:44:24 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let short_index = index.and_then(|x| {
|
|
|
|
if x.get() <= u8::MAX as usize {
|
|
|
|
NonZeroU8::new(x.get() as u8)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
2022-09-25 17:03:18 +02:00
|
|
|
let name = state.get_interned_string(*s);
|
2022-08-29 16:26:07 +02:00
|
|
|
Expr::Variable((index, ns, 0, name).into(), short_index, settings.pos)
|
2020-12-27 09:50:48 +01:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reserved keyword or symbol
|
|
|
|
Token::Reserved(..) => {
|
2022-11-28 16:24:22 +01:00
|
|
|
let ns = Namespace::NONE;
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
let s = match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::Reserved(s), ..) => s,
|
|
|
|
token => unreachable!("Token::Reserved expected but gets {:?}", token),
|
|
|
|
};
|
|
|
|
|
|
|
|
match input.peek().expect(NEVER_ENDS).0 {
|
|
|
|
// Function call is allowed to have reserved keyword
|
2022-04-21 04:04:46 +02:00
|
|
|
Token::LeftParen | Token::Bang | Token::Unit if is_keyword_function(&s) => {
|
|
|
|
Expr::Variable(
|
2022-09-25 17:03:18 +02:00
|
|
|
(None, ns, 0, state.get_interned_string(*s)).into(),
|
2022-04-21 04:04:46 +02:00
|
|
|
None,
|
|
|
|
settings.pos,
|
|
|
|
)
|
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// Access to `this` as a variable is OK within a function scope
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
2022-11-25 02:46:13 +01:00
|
|
|
_ if *s == KEYWORD_THIS && settings.has_flag(ParseSettingFlags::FN_SCOPE) => {
|
2022-11-23 04:36:30 +01:00
|
|
|
Expr::Variable(
|
|
|
|
(None, ns, 0, state.get_interned_string(*s)).into(),
|
|
|
|
None,
|
|
|
|
settings.pos,
|
|
|
|
)
|
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// Cannot access to `this` as a variable not in a function scope
|
2022-11-25 02:46:13 +01:00
|
|
|
_ if *s == KEYWORD_THIS => {
|
2022-08-11 13:01:23 +02:00
|
|
|
let msg = format!("'{s}' can only be used in functions");
|
2022-02-28 07:37:46 +01:00
|
|
|
return Err(
|
|
|
|
LexError::ImproperSymbol(s.to_string(), msg).into_err(settings.pos)
|
|
|
|
);
|
2021-12-04 10:57:28 +01:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
_ => return Err(PERR::Reserved(s.to_string()).into_err(settings.pos)),
|
2020-12-27 09:50:48 +01:00
|
|
|
}
|
2020-07-26 16:25:30 +02:00
|
|
|
}
|
2022-01-29 04:09:43 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Token::LexError(..) => match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::LexError(err), ..) => return Err(err.into_err(settings.pos)),
|
|
|
|
token => unreachable!("Token::LexError expected but gets {:?}", token),
|
|
|
|
},
|
2020-12-27 09:50:48 +01:00
|
|
|
|
2022-11-22 16:30:43 +01:00
|
|
|
_ => return Err(LexError::UnexpectedInput(token.to_string()).into_err(settings.pos)),
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
2020-08-22 16:44:24 +02:00
|
|
|
|
2022-07-04 11:42:24 +02:00
|
|
|
if !(state.expr_filter)(&input.peek().expect(NEVER_ENDS).0) {
|
|
|
|
return Ok(root_expr);
|
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
self.parse_postfix(input, state, lib, root_expr, settings)
|
|
|
|
}
|
2020-08-22 16:44:24 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Tail processing of all possible postfix operators of a primary expression.
|
|
|
|
fn parse_postfix(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
mut lhs: Expr,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Expr> {
|
|
|
|
let mut settings = settings;
|
2020-04-17 13:00:52 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Tail processing all possible postfix operators
|
|
|
|
loop {
|
|
|
|
let (tail_token, ..) = input.peek().expect(NEVER_ENDS);
|
2021-12-16 15:40:10 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
if !lhs.is_valid_postfix(tail_token) {
|
|
|
|
break;
|
|
|
|
}
|
2020-03-05 13:28:03 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let (tail_token, tail_pos) = input.next().expect(NEVER_ENDS);
|
|
|
|
settings.pos = tail_pos;
|
2020-04-10 06:16:39 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
lhs = match (lhs, tail_token) {
|
|
|
|
// Qualified function call with !
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-03-05 10:57:23 +01:00
|
|
|
(Expr::Variable(x, ..), Token::Bang) if !x.1.is_empty() => {
|
2022-04-21 04:04:46 +02:00
|
|
|
return match input.peek().expect(NEVER_ENDS) {
|
|
|
|
(Token::LeftParen | Token::Unit, ..) => {
|
2022-11-22 16:30:43 +01:00
|
|
|
Err(LexError::UnexpectedInput(Token::Bang.into()).into_err(tail_pos))
|
2022-04-21 04:04:46 +02:00
|
|
|
}
|
|
|
|
_ => Err(LexError::ImproperSymbol(
|
2022-11-22 16:30:43 +01:00
|
|
|
"!".into(),
|
|
|
|
"'!' cannot be used to call module functions".into(),
|
2021-11-16 16:13:53 +01:00
|
|
|
)
|
2022-04-21 04:04:46 +02:00
|
|
|
.into_err(tail_pos)),
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
2020-07-30 12:18:28 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// Function call with !
|
2022-03-05 10:57:23 +01:00
|
|
|
(Expr::Variable(x, .., pos), Token::Bang) => {
|
2022-04-21 04:04:46 +02:00
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
(Token::LeftParen | Token::Unit, ..) => (),
|
|
|
|
(_, pos) => {
|
2022-02-28 07:37:46 +01:00
|
|
|
return Err(PERR::MissingToken(
|
2022-11-22 16:30:43 +01:00
|
|
|
Token::LeftParen.into(),
|
2022-02-28 07:37:46 +01:00
|
|
|
"to start arguments list of function call".into(),
|
|
|
|
)
|
2022-04-21 04:04:46 +02:00
|
|
|
.into_err(*pos))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-30 12:18:28 +02:00
|
|
|
|
2022-04-21 04:04:46 +02:00
|
|
|
let no_args = input.next().expect(NEVER_ENDS).0 == Token::Unit;
|
|
|
|
|
2022-11-28 16:24:22 +01:00
|
|
|
let (.., ns, _, name) = *x;
|
2022-02-28 07:37:46 +01:00
|
|
|
settings.pos = pos;
|
2022-11-28 16:24:22 +01:00
|
|
|
let settings = settings.level_up()?;
|
|
|
|
self.parse_fn_call(input, state, lib, name, no_args, true, ns, settings)?
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
// Function call
|
2022-04-21 04:04:46 +02:00
|
|
|
(Expr::Variable(x, .., pos), t @ (Token::LeftParen | Token::Unit)) => {
|
2022-11-28 16:24:22 +01:00
|
|
|
let (.., ns, _, name) = *x;
|
|
|
|
let no_args = t == Token::Unit;
|
2022-02-28 07:37:46 +01:00
|
|
|
settings.pos = pos;
|
2022-11-28 16:24:22 +01:00
|
|
|
let settings = settings.level_up()?;
|
|
|
|
self.parse_fn_call(input, state, lib, name, no_args, false, ns, settings)?
|
2020-07-17 04:18:07 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// module access
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-03-05 10:57:23 +01:00
|
|
|
(Expr::Variable(x, .., pos), Token::DoubleColon) => {
|
2022-02-28 07:37:46 +01:00
|
|
|
let (id2, pos2) = parse_var_name(input)?;
|
2022-03-05 10:57:23 +01:00
|
|
|
let (.., mut namespace, _, name) = *x;
|
2022-02-28 07:37:46 +01:00
|
|
|
let var_name_def = Ident { name, pos };
|
2021-06-07 05:43:00 +02:00
|
|
|
|
2022-03-05 10:57:23 +01:00
|
|
|
namespace.push(var_name_def);
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
Expr::Variable(
|
2022-08-13 12:07:42 +02:00
|
|
|
(None, namespace, 0, state.get_interned_string(id2)).into(),
|
2022-02-28 07:37:46 +01:00
|
|
|
None,
|
|
|
|
pos2,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
// Indexing
|
|
|
|
#[cfg(not(feature = "no_index"))]
|
2022-07-27 12:04:59 +02:00
|
|
|
(expr, token @ (Token::LeftBracket | Token::QuestionBracket)) => {
|
2022-06-11 18:32:12 +02:00
|
|
|
let opt = match token {
|
|
|
|
Token::LeftBracket => ASTFlags::NONE,
|
|
|
|
Token::QuestionBracket => ASTFlags::NEGATED,
|
2022-07-23 15:00:58 +02:00
|
|
|
_ => unreachable!("`[` or `?[`"),
|
2022-06-11 18:32:12 +02:00
|
|
|
};
|
2022-11-30 07:11:09 +01:00
|
|
|
let settings = settings.level_up()?;
|
|
|
|
self.parse_index_chain(input, state, lib, expr, opt, true, settings)?
|
2020-12-27 04:50:24 +01:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// Property access
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2022-07-27 12:04:59 +02:00
|
|
|
(expr, op @ (Token::Period | Token::Elvis)) => {
|
2022-02-28 07:37:46 +01:00
|
|
|
// Expression after dot must start with an identifier
|
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
(Token::Identifier(..), ..) => {
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
{
|
|
|
|
// Prevents capturing of the object properties as vars: xxx.<var>
|
|
|
|
state.allow_capture = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Token::Reserved(s), ..) if is_keyword_function(s) => (),
|
|
|
|
(.., pos) => return Err(PERR::PropertyExpected.into_err(*pos)),
|
|
|
|
}
|
2020-12-27 04:50:24 +01:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
let rhs = self.parse_primary(input, state, lib, true, settings.level_up()?)?;
|
2022-06-10 04:26:06 +02:00
|
|
|
let op_flags = match op {
|
|
|
|
Token::Period => ASTFlags::NONE,
|
|
|
|
Token::Elvis => ASTFlags::NEGATED,
|
2022-07-23 15:00:58 +02:00
|
|
|
_ => unreachable!("`.` or `?.`"),
|
2022-06-10 04:26:06 +02:00
|
|
|
};
|
|
|
|
Self::make_dot_expr(state, expr, rhs, ASTFlags::NONE, op_flags, tail_pos)?
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
// Unknown postfix operator
|
2022-11-22 16:30:43 +01:00
|
|
|
(expr, token) => {
|
|
|
|
unreachable!("unknown postfix operator '{}' for {:?}", token, expr)
|
|
|
|
}
|
2020-12-21 10:39:37 +01:00
|
|
|
}
|
2020-03-10 10:10:33 +01:00
|
|
|
}
|
2020-03-06 03:50:52 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Cache the hash key for namespace-qualified variables
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
let namespaced_variable = match lhs {
|
2022-07-05 10:26:38 +02:00
|
|
|
Expr::Variable(ref mut x, ..) if !x.1.is_empty() => Some(&mut **x),
|
2022-02-28 07:37:46 +01:00
|
|
|
Expr::Index(ref mut x, ..) | Expr::Dot(ref mut x, ..) => match x.lhs {
|
2022-07-05 10:26:38 +02:00
|
|
|
Expr::Variable(ref mut x, ..) if !x.1.is_empty() => Some(&mut **x),
|
2022-02-28 07:37:46 +01:00
|
|
|
_ => None,
|
|
|
|
},
|
2020-12-21 10:39:37 +01:00
|
|
|
_ => None,
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
2021-07-24 08:11:16 +02:00
|
|
|
|
2021-12-04 10:57:28 +01:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-03-05 10:57:23 +01:00
|
|
|
if let Some((.., namespace, hash, name)) = namespaced_variable {
|
|
|
|
if !namespace.is_empty() {
|
2022-09-21 05:46:23 +02:00
|
|
|
*hash = crate::calc_var_hash(namespace.iter().map(Ident::as_str), name);
|
2021-12-04 11:07:27 +01:00
|
|
|
|
2022-03-05 10:57:23 +01:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
{
|
2022-06-09 11:59:28 +02:00
|
|
|
let root = namespace.root();
|
|
|
|
let index = state.find_module(root);
|
2022-11-30 07:11:09 +01:00
|
|
|
let is_global = false;
|
2021-12-04 10:57:28 +01:00
|
|
|
|
2022-06-09 12:22:53 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-11-30 07:11:09 +01:00
|
|
|
let is_global = is_global || root == crate::engine::KEYWORD_GLOBAL;
|
2022-06-09 12:22:53 +02:00
|
|
|
|
2022-11-23 04:36:30 +01:00
|
|
|
if settings.has_option(LangOptions::STRICT_VAR)
|
2022-07-27 10:04:24 +02:00
|
|
|
&& index.is_none()
|
|
|
|
&& !is_global
|
2022-11-25 05:14:40 +01:00
|
|
|
&& !state
|
|
|
|
.global_imports
|
2022-11-25 16:03:20 +01:00
|
|
|
.as_deref()
|
|
|
|
.into_iter()
|
|
|
|
.flatten()
|
2022-11-25 05:14:40 +01:00
|
|
|
.any(|m| m.as_str() == root)
|
2022-11-24 15:58:42 +01:00
|
|
|
&& !self
|
|
|
|
.global_sub_modules
|
2022-11-25 13:42:16 +01:00
|
|
|
.as_deref()
|
2022-11-24 15:58:42 +01:00
|
|
|
.map_or(false, |m| m.contains_key(root))
|
2022-07-27 10:04:24 +02:00
|
|
|
{
|
|
|
|
return Err(
|
2022-11-22 16:30:43 +01:00
|
|
|
PERR::ModuleUndefined(root.into()).into_err(namespace.position())
|
2022-07-27 10:04:24 +02:00
|
|
|
);
|
2022-03-05 10:57:23 +01:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2022-03-05 10:57:23 +01:00
|
|
|
namespace.set_index(index);
|
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-10 11:32:01 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Make sure identifiers are valid
|
|
|
|
Ok(lhs)
|
|
|
|
}
|
2020-03-10 11:32:01 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse a potential unary operator.
|
|
|
|
fn parse_unary(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Expr> {
|
|
|
|
let (token, token_pos) = input.peek().expect(NEVER_ENDS);
|
|
|
|
|
2022-07-04 11:42:24 +02:00
|
|
|
if !(state.expr_filter)(token) {
|
2022-11-22 16:30:43 +01:00
|
|
|
return Err(LexError::UnexpectedInput(token.to_string()).into_err(*token_pos));
|
2022-07-04 11:42:24 +02:00
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = *token_pos;
|
|
|
|
|
|
|
|
match token {
|
|
|
|
// -expr
|
|
|
|
Token::Minus | Token::UnaryMinus => {
|
|
|
|
let token = token.clone();
|
2022-09-25 17:03:18 +02:00
|
|
|
let pos = eat_token(input, token.clone());
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
match self.parse_unary(input, state, lib, settings.level_up()?)? {
|
2022-02-28 07:37:46 +01:00
|
|
|
// Negative integer
|
|
|
|
Expr::IntegerConstant(num, ..) => num
|
|
|
|
.checked_neg()
|
|
|
|
.map(|i| Expr::IntegerConstant(i, pos))
|
|
|
|
.or_else(|| {
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
return Some(Expr::FloatConstant((-(num as crate::FLOAT)).into(), pos));
|
|
|
|
#[cfg(feature = "no_float")]
|
|
|
|
return None;
|
|
|
|
})
|
2022-08-11 13:01:23 +02:00
|
|
|
.ok_or_else(|| LexError::MalformedNumber(format!("-{num}")).into_err(pos)),
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
// Negative float
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
Expr::FloatConstant(x, ..) => Ok(Expr::FloatConstant((-(*x)).into(), pos)),
|
|
|
|
|
|
|
|
// Call negative function
|
|
|
|
expr => {
|
|
|
|
let mut args = StaticVec::new_const();
|
|
|
|
args.push(expr);
|
|
|
|
args.shrink_to_fit();
|
|
|
|
|
|
|
|
Ok(FnCallExpr {
|
2022-11-28 16:24:22 +01:00
|
|
|
namespace: Namespace::NONE,
|
2022-08-13 12:07:42 +02:00
|
|
|
name: state.get_interned_string("-"),
|
2022-09-21 05:46:23 +02:00
|
|
|
hashes: FnCallHashes::from_native(calc_fn_hash(None, "-", 1)),
|
2022-02-28 07:37:46 +01:00
|
|
|
args,
|
2022-11-25 13:42:16 +01:00
|
|
|
op_token: token,
|
2022-10-14 12:31:40 +02:00
|
|
|
capture_parent_scope: false,
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
.into_fn_call_expr(pos))
|
2021-06-16 12:36:33 +02:00
|
|
|
}
|
2020-05-09 10:15:50 +02:00
|
|
|
}
|
2020-03-09 03:10:19 +01:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// +expr
|
|
|
|
Token::Plus | Token::UnaryPlus => {
|
|
|
|
let token = token.clone();
|
2022-09-25 17:03:18 +02:00
|
|
|
let pos = eat_token(input, token.clone());
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
match self.parse_unary(input, state, lib, settings.level_up()?)? {
|
2022-02-28 07:37:46 +01:00
|
|
|
expr @ Expr::IntegerConstant(..) => Ok(expr),
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
expr @ Expr::FloatConstant(..) => Ok(expr),
|
|
|
|
|
|
|
|
// Call plus function
|
|
|
|
expr => {
|
|
|
|
let mut args = StaticVec::new_const();
|
|
|
|
args.push(expr);
|
|
|
|
args.shrink_to_fit();
|
|
|
|
|
|
|
|
Ok(FnCallExpr {
|
2022-11-28 16:24:22 +01:00
|
|
|
namespace: Namespace::NONE,
|
2022-08-13 12:07:42 +02:00
|
|
|
name: state.get_interned_string("+"),
|
2022-09-21 05:46:23 +02:00
|
|
|
hashes: FnCallHashes::from_native(calc_fn_hash(None, "+", 1)),
|
2022-02-28 07:37:46 +01:00
|
|
|
args,
|
2022-11-25 13:42:16 +01:00
|
|
|
op_token: token,
|
2022-10-14 12:31:40 +02:00
|
|
|
capture_parent_scope: false,
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
.into_fn_call_expr(pos))
|
2021-06-16 12:36:33 +02:00
|
|
|
}
|
2020-12-21 10:39:37 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// !expr
|
|
|
|
Token::Bang => {
|
2022-09-25 17:03:18 +02:00
|
|
|
let token = token.clone();
|
2022-02-28 07:37:46 +01:00
|
|
|
let pos = eat_token(input, Token::Bang);
|
2022-09-25 17:03:18 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let mut args = StaticVec::new_const();
|
2022-11-24 10:08:43 +01:00
|
|
|
args.push(self.parse_unary(input, state, lib, settings.level_up()?)?);
|
2022-02-28 07:37:46 +01:00
|
|
|
args.shrink_to_fit();
|
2020-05-09 18:19:13 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(FnCallExpr {
|
2022-11-28 16:24:22 +01:00
|
|
|
namespace: Namespace::NONE,
|
2022-08-13 12:07:42 +02:00
|
|
|
name: state.get_interned_string("!"),
|
2022-09-21 05:46:23 +02:00
|
|
|
hashes: FnCallHashes::from_native(calc_fn_hash(None, "!", 1)),
|
2022-02-28 07:37:46 +01:00
|
|
|
args,
|
2022-11-25 13:42:16 +01:00
|
|
|
op_token: token,
|
2022-10-14 12:31:40 +02:00
|
|
|
capture_parent_scope: false,
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
.into_fn_call_expr(pos))
|
2021-06-16 12:36:33 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// <EOF>
|
|
|
|
Token::EOF => Err(PERR::UnexpectedEOF.into_err(settings.pos)),
|
|
|
|
// All other tokens
|
2022-11-24 10:08:43 +01:00
|
|
|
_ => self.parse_primary(input, state, lib, false, settings.level_up()?),
|
2019-09-18 12:21:07 +02:00
|
|
|
}
|
2017-10-30 16:08:44 +01:00
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Make an assignment statement.
|
|
|
|
fn make_assignment_stmt(
|
2022-11-25 13:42:16 +01:00
|
|
|
op: Token,
|
2022-02-28 07:37:46 +01:00
|
|
|
state: &mut ParseState,
|
|
|
|
lhs: Expr,
|
|
|
|
rhs: Expr,
|
|
|
|
op_pos: Position,
|
|
|
|
) -> ParseResult<Stmt> {
|
|
|
|
#[must_use]
|
|
|
|
fn check_lvalue(expr: &Expr, parent_is_dot: bool) -> Option<Position> {
|
|
|
|
match expr {
|
|
|
|
Expr::Index(x, options, ..) | Expr::Dot(x, options, ..) if parent_is_dot => {
|
|
|
|
match x.lhs {
|
|
|
|
Expr::Property(..) if !options.contains(ASTFlags::BREAK) => {
|
|
|
|
check_lvalue(&x.rhs, matches!(expr, Expr::Dot(..)))
|
|
|
|
}
|
|
|
|
Expr::Property(..) => None,
|
|
|
|
// Anything other than a property after dotting (e.g. a method call) is not an l-value
|
|
|
|
ref e => Some(e.position()),
|
|
|
|
}
|
2022-02-25 04:42:59 +01:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
Expr::Index(x, options, ..) | Expr::Dot(x, options, ..) => match x.lhs {
|
|
|
|
Expr::Property(..) => unreachable!("unexpected Expr::Property in indexing"),
|
|
|
|
_ if !options.contains(ASTFlags::BREAK) => {
|
|
|
|
check_lvalue(&x.rhs, matches!(expr, Expr::Dot(..)))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
Expr::Property(..) if parent_is_dot => None,
|
2022-02-08 02:02:15 +01:00
|
|
|
Expr::Property(..) => unreachable!("unexpected Expr::Property in indexing"),
|
2022-02-28 07:37:46 +01:00
|
|
|
e if parent_is_dot => Some(e.position()),
|
2021-11-01 02:55:50 +01:00
|
|
|
_ => None,
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2020-12-29 03:41:20 +01:00
|
|
|
}
|
|
|
|
|
2022-11-28 16:24:22 +01:00
|
|
|
let op_info = if op != NO_TOKEN {
|
2022-04-18 17:12:47 +02:00
|
|
|
OpAssignment::new_op_assignment_from_token(op, op_pos)
|
|
|
|
} else {
|
|
|
|
OpAssignment::new_assignment(op_pos)
|
|
|
|
};
|
2021-03-08 08:30:32 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match lhs {
|
|
|
|
// const_expr = rhs
|
|
|
|
ref expr if expr.is_constant() => {
|
|
|
|
Err(PERR::AssignmentToConstant("".into()).into_err(lhs.start_position()))
|
|
|
|
}
|
|
|
|
// var (non-indexed) = rhs
|
2022-04-18 17:12:47 +02:00
|
|
|
Expr::Variable(ref x, None, _) if x.0.is_none() => {
|
|
|
|
Ok(Stmt::Assignment((op_info, (lhs, rhs).into()).into()))
|
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// var (indexed) = rhs
|
2022-03-05 10:57:23 +01:00
|
|
|
Expr::Variable(ref x, i, var_pos) => {
|
2022-11-25 02:46:13 +01:00
|
|
|
let stack = state.stack.get_or_insert_with(Default::default);
|
2022-07-05 10:26:38 +02:00
|
|
|
let (index, .., name) = &**x;
|
2022-02-28 07:37:46 +01:00
|
|
|
let index = i.map_or_else(
|
|
|
|
|| index.expect("either long or short index is `None`").get(),
|
|
|
|
|n| n.get() as usize,
|
|
|
|
);
|
2022-11-24 10:08:43 +01:00
|
|
|
|
|
|
|
match stack.get_mut_by_index(stack.len() - index).access_mode() {
|
2022-04-18 17:12:47 +02:00
|
|
|
AccessMode::ReadWrite => {
|
|
|
|
Ok(Stmt::Assignment((op_info, (lhs, rhs).into()).into()))
|
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// Constant values cannot be assigned to
|
|
|
|
AccessMode::ReadOnly => {
|
|
|
|
Err(PERR::AssignmentToConstant(name.to_string()).into_err(var_pos))
|
|
|
|
}
|
2020-05-07 06:25:09 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// xxx[???]... = rhs, xxx.prop... = rhs
|
|
|
|
Expr::Index(ref x, options, ..) | Expr::Dot(ref x, options, ..) => {
|
|
|
|
let valid_lvalue = if options.contains(ASTFlags::BREAK) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
check_lvalue(&x.rhs, matches!(lhs, Expr::Dot(..)))
|
|
|
|
};
|
2021-11-01 02:55:50 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match valid_lvalue {
|
|
|
|
None => {
|
|
|
|
match x.lhs {
|
|
|
|
// var[???] = rhs, var.??? = rhs
|
2022-04-18 17:12:47 +02:00
|
|
|
Expr::Variable(..) => {
|
|
|
|
Ok(Stmt::Assignment((op_info, (lhs, rhs).into()).into()))
|
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// expr[???] = rhs, expr.??? = rhs
|
2022-08-21 11:35:44 +02:00
|
|
|
ref expr => Err(PERR::AssignmentToInvalidLHS(String::new())
|
2022-02-28 07:37:46 +01:00
|
|
|
.into_err(expr.position())),
|
2022-02-06 14:24:02 +01:00
|
|
|
}
|
2020-12-29 03:41:20 +01:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
Some(err_pos) => {
|
2022-08-21 11:35:44 +02:00
|
|
|
Err(PERR::AssignmentToInvalidLHS(String::new()).into_err(err_pos))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2021-11-01 02:55:50 +01:00
|
|
|
}
|
2020-05-07 06:25:09 +02:00
|
|
|
}
|
2022-06-10 05:22:33 +02:00
|
|
|
// ??? && ??? = rhs, ??? || ??? = rhs, xxx ?? xxx = rhs
|
|
|
|
Expr::And(..) | Expr::Or(..) | Expr::Coalesce(..) => Err(LexError::ImproperSymbol(
|
2022-11-22 16:30:43 +01:00
|
|
|
"=".into(),
|
|
|
|
"Possibly a typo of '=='?".into(),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
|
|
|
.into_err(op_pos)),
|
|
|
|
// expr = rhs
|
2022-08-21 11:35:44 +02:00
|
|
|
_ => Err(PERR::AssignmentToInvalidLHS(String::new()).into_err(lhs.position())),
|
2020-05-07 06:25:09 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-22 11:37:06 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse an operator-assignment expression (if any).
|
|
|
|
fn parse_op_assignment_stmt(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
lhs: Expr,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Stmt> {
|
|
|
|
let (op, pos) = match input.peek().expect(NEVER_ENDS) {
|
|
|
|
// var = ...
|
2022-11-28 16:24:22 +01:00
|
|
|
(Token::Equals, ..) => (NO_TOKEN, eat_token(input, Token::Equals)),
|
2022-02-28 07:37:46 +01:00
|
|
|
// var op= ...
|
2022-11-25 13:42:16 +01:00
|
|
|
(token, ..) if token.is_op_assignment() => {
|
|
|
|
input.next().map(|(op, pos)| (op, pos)).expect(NEVER_ENDS)
|
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// Not op-assignment
|
|
|
|
_ => return Ok(Stmt::Expr(lhs.into())),
|
|
|
|
};
|
2022-02-18 04:05:58 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = pos;
|
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
let rhs = self.parse_expr(input, state, lib, settings.level_up()?)?;
|
2022-02-28 07:37:46 +01:00
|
|
|
Self::make_assignment_stmt(op, state, lhs, rhs, pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Make a dot expression.
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
|
|
|
fn make_dot_expr(
|
|
|
|
state: &mut ParseState,
|
|
|
|
lhs: Expr,
|
|
|
|
rhs: Expr,
|
2022-06-10 04:26:06 +02:00
|
|
|
parent_options: ASTFlags,
|
|
|
|
op_flags: ASTFlags,
|
2022-02-28 07:37:46 +01:00
|
|
|
op_pos: Position,
|
|
|
|
) -> ParseResult<Expr> {
|
|
|
|
match (lhs, rhs) {
|
|
|
|
// lhs[idx_expr].rhs
|
|
|
|
(Expr::Index(mut x, options, pos), rhs) => {
|
2022-11-30 07:11:09 +01:00
|
|
|
let options = options | parent_options;
|
|
|
|
x.rhs = Self::make_dot_expr(state, x.rhs, rhs, options, op_flags, op_pos)?;
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(Expr::Index(x, ASTFlags::NONE, pos))
|
|
|
|
}
|
|
|
|
// lhs.module::id - syntax error
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-03-05 10:57:23 +01:00
|
|
|
(.., Expr::Variable(x, ..)) if !x.1.is_empty() => {
|
|
|
|
Err(PERR::PropertyExpected.into_err(x.1.position()))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
// lhs.id
|
|
|
|
(lhs, var_expr @ Expr::Variable(..)) => {
|
|
|
|
let rhs = var_expr.into_property(state);
|
2022-06-10 04:26:06 +02:00
|
|
|
Ok(Expr::Dot(BinaryExpr { lhs, rhs }.into(), op_flags, op_pos))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
// lhs.prop
|
|
|
|
(lhs, prop @ Expr::Property(..)) => Ok(Expr::Dot(
|
|
|
|
BinaryExpr { lhs, rhs: prop }.into(),
|
2022-06-10 04:26:06 +02:00
|
|
|
op_flags,
|
2022-02-25 04:42:59 +01:00
|
|
|
op_pos,
|
2022-02-28 07:37:46 +01:00
|
|
|
)),
|
|
|
|
// lhs.nnn::func(...) - syntax error
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-11-29 08:50:58 +01:00
|
|
|
(.., Expr::FnCall(f, ..)) if f.is_qualified() => {
|
|
|
|
Err(PERR::PropertyExpected.into_err(f.namespace.position()))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
// lhs.Fn() or lhs.eval()
|
2022-11-29 08:50:58 +01:00
|
|
|
(.., Expr::FnCall(f, func_pos))
|
|
|
|
if f.args.is_empty()
|
2022-02-28 07:37:46 +01:00
|
|
|
&& [crate::engine::KEYWORD_FN_PTR, crate::engine::KEYWORD_EVAL]
|
2022-11-29 08:50:58 +01:00
|
|
|
.contains(&f.name.as_str()) =>
|
2022-02-28 07:37:46 +01:00
|
|
|
{
|
|
|
|
let err_msg = format!(
|
|
|
|
"'{}' should not be called in method style. Try {}(...);",
|
2022-11-29 08:50:58 +01:00
|
|
|
f.name, f.name
|
2022-02-28 07:37:46 +01:00
|
|
|
);
|
2022-11-29 08:50:58 +01:00
|
|
|
Err(LexError::ImproperSymbol(f.name.to_string(), err_msg).into_err(func_pos))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
// lhs.func!(...)
|
2022-11-29 08:50:58 +01:00
|
|
|
(.., Expr::FnCall(f, func_pos)) if f.capture_parent_scope => {
|
2022-02-28 07:37:46 +01:00
|
|
|
Err(PERR::MalformedCapture(
|
|
|
|
"method-call style does not support running within the caller's scope".into(),
|
|
|
|
)
|
|
|
|
.into_err(func_pos))
|
|
|
|
}
|
|
|
|
// lhs.func(...)
|
2022-11-29 08:50:58 +01:00
|
|
|
(lhs, Expr::FnCall(mut f, func_pos)) => {
|
2022-02-28 07:37:46 +01:00
|
|
|
// Recalculate hash
|
2022-11-29 08:50:58 +01:00
|
|
|
f.hashes = if is_valid_function_name(&f.name) {
|
2022-10-30 08:45:25 +01:00
|
|
|
FnCallHashes::from_all(
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
2022-11-29 08:50:58 +01:00
|
|
|
calc_fn_hash(None, &f.name, f.args.len()),
|
|
|
|
calc_fn_hash(None, &f.name, f.args.len() + 1),
|
2022-10-30 08:45:25 +01:00
|
|
|
)
|
|
|
|
} else {
|
2022-11-29 08:50:58 +01:00
|
|
|
FnCallHashes::from_native(calc_fn_hash(None, &f.name, f.args.len() + 1))
|
2022-10-30 08:45:25 +01:00
|
|
|
};
|
2021-08-17 09:32:48 +02:00
|
|
|
|
2022-11-29 08:50:58 +01:00
|
|
|
let rhs = Expr::MethodCall(f, func_pos);
|
2022-06-10 04:26:06 +02:00
|
|
|
Ok(Expr::Dot(BinaryExpr { lhs, rhs }.into(), op_flags, op_pos))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
// lhs.dot_lhs.dot_rhs or lhs.dot_lhs[idx_rhs]
|
2022-04-21 04:04:46 +02:00
|
|
|
(lhs, rhs @ (Expr::Dot(..) | Expr::Index(..))) => {
|
2022-06-10 04:26:06 +02:00
|
|
|
let (x, options, pos, is_dot) = match rhs {
|
|
|
|
Expr::Dot(x, options, pos) => (x, options, pos, true),
|
|
|
|
Expr::Index(x, options, pos) => (x, options, pos, false),
|
2022-02-28 07:37:46 +01:00
|
|
|
expr => unreachable!("Expr::Dot or Expr::Index expected but gets {:?}", expr),
|
|
|
|
};
|
2021-08-17 09:32:48 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match x.lhs {
|
|
|
|
// lhs.module::id.dot_rhs or lhs.module::id[idx_rhs] - syntax error
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-03-05 10:57:23 +01:00
|
|
|
Expr::Variable(x, ..) if !x.1.is_empty() => {
|
|
|
|
Err(PERR::PropertyExpected.into_err(x.1.position()))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
// lhs.module::func().dot_rhs or lhs.module::func()[idx_rhs] - syntax error
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-11-29 08:50:58 +01:00
|
|
|
Expr::FnCall(f, ..) if f.is_qualified() => {
|
|
|
|
Err(PERR::PropertyExpected.into_err(f.namespace.position()))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
// lhs.id.dot_rhs or lhs.id[idx_rhs]
|
|
|
|
Expr::Variable(..) | Expr::Property(..) => {
|
2022-06-10 04:26:06 +02:00
|
|
|
let new_binary = BinaryExpr {
|
2022-02-28 07:37:46 +01:00
|
|
|
lhs: x.lhs.into_property(state),
|
|
|
|
rhs: x.rhs,
|
|
|
|
}
|
|
|
|
.into();
|
2021-08-17 09:32:48 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let rhs = if is_dot {
|
2022-06-10 04:26:06 +02:00
|
|
|
Expr::Dot(new_binary, options, pos)
|
2022-02-28 07:37:46 +01:00
|
|
|
} else {
|
2022-06-10 04:26:06 +02:00
|
|
|
Expr::Index(new_binary, options, pos)
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
2022-06-10 04:26:06 +02:00
|
|
|
Ok(Expr::Dot(BinaryExpr { lhs, rhs }.into(), op_flags, op_pos))
|
2021-06-29 12:25:20 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// lhs.func().dot_rhs or lhs.func()[idx_rhs]
|
2022-11-29 08:50:58 +01:00
|
|
|
Expr::FnCall(mut f, func_pos) => {
|
2022-02-28 07:37:46 +01:00
|
|
|
// Recalculate hash
|
2022-11-29 08:50:58 +01:00
|
|
|
f.hashes = if is_valid_function_name(&f.name) {
|
2022-10-30 08:45:25 +01:00
|
|
|
FnCallHashes::from_all(
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
2022-11-29 08:50:58 +01:00
|
|
|
calc_fn_hash(None, &f.name, f.args.len()),
|
|
|
|
calc_fn_hash(None, &f.name, f.args.len() + 1),
|
2022-10-30 08:45:25 +01:00
|
|
|
)
|
|
|
|
} else {
|
2022-11-29 08:50:58 +01:00
|
|
|
FnCallHashes::from_native(calc_fn_hash(None, &f.name, f.args.len() + 1))
|
2022-10-30 08:45:25 +01:00
|
|
|
};
|
2021-08-17 09:32:48 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let new_lhs = BinaryExpr {
|
2022-11-29 08:50:58 +01:00
|
|
|
lhs: Expr::MethodCall(f, func_pos),
|
2022-02-28 07:37:46 +01:00
|
|
|
rhs: x.rhs,
|
|
|
|
}
|
|
|
|
.into();
|
|
|
|
|
|
|
|
let rhs = if is_dot {
|
2022-06-10 04:26:06 +02:00
|
|
|
Expr::Dot(new_lhs, options, pos)
|
2022-02-28 07:37:46 +01:00
|
|
|
} else {
|
2022-06-10 04:26:06 +02:00
|
|
|
Expr::Index(new_lhs, options, pos)
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
2022-06-10 04:26:06 +02:00
|
|
|
Ok(Expr::Dot(BinaryExpr { lhs, rhs }.into(), op_flags, op_pos))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
expr => unreachable!("invalid dot expression: {:?}", expr),
|
2021-06-29 12:25:20 +02:00
|
|
|
}
|
2021-08-17 09:32:48 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
// lhs.rhs
|
|
|
|
(.., rhs) => Err(PERR::PropertyExpected.into_err(rhs.start_position())),
|
2020-05-09 18:19:13 +02:00
|
|
|
}
|
2021-11-16 16:13:53 +01:00
|
|
|
}
|
2020-07-26 09:53:22 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse a binary expression (if any).
|
|
|
|
fn parse_binary_op(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
parent_precedence: Option<Precedence>,
|
|
|
|
lhs: Expr,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Expr> {
|
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = lhs.position();
|
2020-10-31 07:13:45 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let mut root = lhs;
|
2021-03-09 06:44:54 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
loop {
|
|
|
|
let (current_op, current_pos) = input.peek().expect(NEVER_ENDS);
|
2022-07-04 11:42:24 +02:00
|
|
|
|
|
|
|
if !(state.expr_filter)(current_op) {
|
|
|
|
return Ok(root);
|
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let precedence = match current_op {
|
2022-07-05 16:59:03 +02:00
|
|
|
#[cfg(not(feature = "no_custom_syntax"))]
|
2022-02-28 07:37:46 +01:00
|
|
|
Token::Custom(c) => self
|
|
|
|
.custom_keywords
|
2022-11-25 13:42:16 +01:00
|
|
|
.as_deref()
|
2022-11-24 15:58:42 +01:00
|
|
|
.and_then(|m| m.get(&**c))
|
2022-07-27 12:04:59 +02:00
|
|
|
.copied()
|
2022-02-28 07:37:46 +01:00
|
|
|
.ok_or_else(|| PERR::Reserved(c.to_string()).into_err(*current_pos))?,
|
2022-10-30 08:45:25 +01:00
|
|
|
Token::Reserved(c) if !is_valid_identifier(c) => {
|
2022-02-28 07:37:46 +01:00
|
|
|
return Err(PERR::UnknownOperator(c.to_string()).into_err(*current_pos))
|
2021-06-16 12:36:33 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
_ => current_op.precedence(),
|
|
|
|
};
|
|
|
|
let bind_right = current_op.is_bind_right();
|
|
|
|
|
|
|
|
// Bind left to the parent lhs expression if precedence is higher
|
|
|
|
// If same precedence, then check if the operator binds right
|
|
|
|
if precedence < parent_precedence || (precedence == parent_precedence && !bind_right) {
|
|
|
|
return Ok(root);
|
2020-05-09 15:46:38 +02:00
|
|
|
}
|
2020-04-23 07:24:24 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let (op_token, pos) = input.next().expect(NEVER_ENDS);
|
2020-12-26 16:21:09 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let rhs = self.parse_unary(input, state, lib, settings)?;
|
|
|
|
|
|
|
|
let (next_op, next_pos) = input.peek().expect(NEVER_ENDS);
|
|
|
|
let next_precedence = match next_op {
|
2022-07-05 16:59:03 +02:00
|
|
|
#[cfg(not(feature = "no_custom_syntax"))]
|
2022-02-28 07:37:46 +01:00
|
|
|
Token::Custom(c) => self
|
|
|
|
.custom_keywords
|
2022-11-25 13:42:16 +01:00
|
|
|
.as_deref()
|
2022-11-24 15:58:42 +01:00
|
|
|
.and_then(|m| m.get(&**c))
|
2022-07-27 12:04:59 +02:00
|
|
|
.copied()
|
2022-02-28 07:37:46 +01:00
|
|
|
.ok_or_else(|| PERR::Reserved(c.to_string()).into_err(*next_pos))?,
|
2022-10-30 08:45:25 +01:00
|
|
|
Token::Reserved(c) if !is_valid_identifier(c) => {
|
2022-02-28 07:37:46 +01:00
|
|
|
return Err(PERR::UnknownOperator(c.to_string()).into_err(*next_pos))
|
2021-06-16 12:36:33 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
_ => next_op.precedence(),
|
|
|
|
};
|
2020-07-05 11:41:45 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Bind to right if the next operator has higher precedence
|
|
|
|
// 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)?
|
|
|
|
} else {
|
|
|
|
// Otherwise bind to left (even if next operator has the same precedence)
|
|
|
|
rhs
|
|
|
|
};
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
settings = settings.level_up()?;
|
2022-02-28 07:37:46 +01:00
|
|
|
settings.pos = pos;
|
2020-10-19 13:21:40 +02:00
|
|
|
|
2022-11-22 16:30:43 +01:00
|
|
|
let op = op_token.to_string();
|
2022-09-21 05:46:23 +02:00
|
|
|
let hash = calc_fn_hash(None, &op, 2);
|
2022-10-30 08:45:25 +01:00
|
|
|
let is_valid_script_function = is_valid_function_name(&op);
|
|
|
|
let operator_token = if is_valid_script_function {
|
2022-11-28 16:24:22 +01:00
|
|
|
NO_TOKEN
|
2022-09-25 17:03:18 +02:00
|
|
|
} else {
|
2022-11-25 13:42:16 +01:00
|
|
|
op_token.clone()
|
2022-09-25 17:03:18 +02:00
|
|
|
};
|
2020-10-25 14:57:18 +01:00
|
|
|
|
2022-10-30 11:43:18 +01:00
|
|
|
let mut args = StaticVec::new_const();
|
|
|
|
args.push(root);
|
|
|
|
args.push(rhs);
|
|
|
|
args.shrink_to_fit();
|
|
|
|
|
|
|
|
let mut op_base = FnCallExpr {
|
2022-11-28 16:24:22 +01:00
|
|
|
namespace: Namespace::NONE,
|
2022-11-22 16:30:43 +01:00
|
|
|
name: state.get_interned_string(&op),
|
2022-02-28 07:37:46 +01:00
|
|
|
hashes: FnCallHashes::from_native(hash),
|
2022-10-30 11:43:18 +01:00
|
|
|
args,
|
|
|
|
op_token: operator_token,
|
2022-10-14 12:31:40 +02:00
|
|
|
capture_parent_scope: false,
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
2020-10-19 13:21:40 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
root = match op_token {
|
|
|
|
// '!=' defaults to true when passed invalid operands
|
2022-10-30 11:43:18 +01:00
|
|
|
Token::NotEqualsTo => op_base.into_fn_call_expr(pos),
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
// Comparison operators default to false when passed invalid operands
|
|
|
|
Token::EqualsTo
|
|
|
|
| Token::LessThan
|
|
|
|
| Token::LessThanEqualsTo
|
|
|
|
| Token::GreaterThan
|
|
|
|
| Token::GreaterThanEqualsTo => {
|
2022-10-30 11:43:18 +01:00
|
|
|
let pos = op_base.args[0].start_position();
|
|
|
|
op_base.into_fn_call_expr(pos)
|
2020-11-04 04:49:02 +01:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
Token::Or => {
|
2022-10-30 11:43:18 +01:00
|
|
|
let rhs = op_base.args.pop().unwrap().ensure_bool_expr()?;
|
|
|
|
let lhs = op_base.args.pop().unwrap().ensure_bool_expr()?;
|
2022-11-23 04:36:30 +01:00
|
|
|
Expr::Or(BinaryExpr { lhs, rhs }.into(), pos)
|
2021-06-10 04:16:39 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
Token::And => {
|
2022-10-30 11:43:18 +01:00
|
|
|
let rhs = op_base.args.pop().unwrap().ensure_bool_expr()?;
|
|
|
|
let lhs = op_base.args.pop().unwrap().ensure_bool_expr()?;
|
2022-11-23 04:36:30 +01:00
|
|
|
Expr::And(BinaryExpr { lhs, rhs }.into(), pos)
|
2021-06-10 04:16:39 +02:00
|
|
|
}
|
2022-06-10 05:22:33 +02:00
|
|
|
Token::DoubleQuestion => {
|
2022-10-30 11:43:18 +01:00
|
|
|
let rhs = op_base.args.pop().unwrap();
|
|
|
|
let lhs = op_base.args.pop().unwrap();
|
|
|
|
Expr::Coalesce(BinaryExpr { lhs, rhs }.into(), pos)
|
2022-06-10 05:22:33 +02:00
|
|
|
}
|
2022-11-30 07:11:09 +01:00
|
|
|
Token::In | Token::NotIn => {
|
2022-02-28 07:37:46 +01:00
|
|
|
// Swap the arguments
|
2022-10-30 11:43:18 +01:00
|
|
|
let lhs = op_base.args.remove(0);
|
|
|
|
let pos = lhs.start_position();
|
|
|
|
op_base.args.push(lhs);
|
|
|
|
op_base.args.shrink_to_fit();
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
// Convert into a call to `contains`
|
2022-10-30 11:43:18 +01:00
|
|
|
op_base.hashes = calc_fn_hash(None, OP_CONTAINS, 2).into();
|
|
|
|
op_base.name = state.get_interned_string(OP_CONTAINS);
|
2022-11-30 07:11:09 +01:00
|
|
|
let fn_call = op_base.into_fn_call_expr(pos);
|
|
|
|
|
|
|
|
if op_token == Token::In {
|
|
|
|
fn_call
|
|
|
|
} else {
|
|
|
|
// Put a `!` call in front
|
|
|
|
let op = Token::Bang.literal_syntax();
|
|
|
|
let mut args = StaticVec::new_const();
|
|
|
|
args.push(fn_call);
|
|
|
|
|
|
|
|
let not_base = FnCallExpr {
|
|
|
|
namespace: Namespace::NONE,
|
|
|
|
name: state.get_interned_string(op),
|
|
|
|
hashes: FnCallHashes::from_native(calc_fn_hash(None, op, 1).into()),
|
|
|
|
args,
|
|
|
|
op_token: Token::Bang,
|
|
|
|
capture_parent_scope: false,
|
|
|
|
};
|
|
|
|
not_base.into_fn_call_expr(pos)
|
|
|
|
}
|
2021-06-10 04:16:39 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-07-05 16:59:03 +02:00
|
|
|
#[cfg(not(feature = "no_custom_syntax"))]
|
2022-02-28 07:37:46 +01:00
|
|
|
Token::Custom(s)
|
|
|
|
if self
|
|
|
|
.custom_keywords
|
2022-11-25 13:42:16 +01:00
|
|
|
.as_deref()
|
2022-11-24 15:58:42 +01:00
|
|
|
.and_then(|m| m.get(s.as_str()))
|
2022-02-28 07:37:46 +01:00
|
|
|
.map_or(false, Option::is_some) =>
|
|
|
|
{
|
2022-10-30 11:43:18 +01:00
|
|
|
op_base.hashes = if is_valid_script_function {
|
|
|
|
calc_fn_hash(None, &s, 2).into()
|
|
|
|
} else {
|
|
|
|
FnCallHashes::from_native(calc_fn_hash(None, &s, 2))
|
|
|
|
};
|
|
|
|
op_base.into_fn_call_expr(pos)
|
2021-06-10 04:16:39 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
_ => {
|
2022-10-30 11:43:18 +01:00
|
|
|
let pos = op_base.args[0].start_position();
|
|
|
|
op_base.into_fn_call_expr(pos)
|
2021-06-10 04:16:39 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a custom syntax.
|
2022-07-05 16:59:03 +02:00
|
|
|
#[cfg(not(feature = "no_custom_syntax"))]
|
2022-02-28 07:37:46 +01:00
|
|
|
fn parse_custom_syntax(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
key: impl Into<ImmutableString>,
|
2022-07-05 16:59:03 +02:00
|
|
|
syntax: &crate::api::custom_syntax::CustomSyntax,
|
2022-02-28 07:37:46 +01:00
|
|
|
pos: Position,
|
|
|
|
) -> ParseResult<Expr> {
|
2022-11-23 11:02:10 +01:00
|
|
|
#[allow(clippy::wildcard_imports)]
|
2022-07-05 16:59:03 +02:00
|
|
|
use crate::api::custom_syntax::markers::*;
|
2022-11-23 11:02:10 +01:00
|
|
|
|
2022-10-05 09:15:42 +02:00
|
|
|
const KEYWORD_SEMICOLON: &str = Token::SemiColon.literal_syntax();
|
|
|
|
const KEYWORD_CLOSE_BRACE: &str = Token::RightBrace.literal_syntax();
|
2022-07-05 16:59:03 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let mut settings = settings;
|
2022-09-24 10:59:01 +02:00
|
|
|
let mut inputs = StaticVec::new_const();
|
2022-02-28 07:37:46 +01:00
|
|
|
let mut segments = StaticVec::new_const();
|
|
|
|
let mut tokens = StaticVec::new_const();
|
|
|
|
|
|
|
|
// Adjust the variables stack
|
|
|
|
if syntax.scope_may_be_changed {
|
|
|
|
// Add a barrier variable to the stack so earlier variables will not be matched.
|
|
|
|
// Variable searches stop at the first barrier.
|
2022-08-13 12:07:42 +02:00
|
|
|
let marker = state.get_interned_string(SCOPE_SEARCH_BARRIER_MARKER);
|
2022-11-24 15:25:19 +01:00
|
|
|
state
|
|
|
|
.stack
|
2022-11-25 02:46:13 +01:00
|
|
|
.get_or_insert_with(Default::default)
|
2022-11-24 15:25:19 +01:00
|
|
|
.push(marker, ());
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
|
2022-09-12 06:03:32 +02:00
|
|
|
let mut user_state = Dynamic::UNIT;
|
2022-07-05 10:26:38 +02:00
|
|
|
let parse_func = &*syntax.parse;
|
2022-02-28 07:37:46 +01:00
|
|
|
let mut required_token: ImmutableString = key.into();
|
|
|
|
|
2022-08-13 12:07:42 +02:00
|
|
|
tokens.push(required_token.clone());
|
2022-02-28 07:37:46 +01:00
|
|
|
segments.push(required_token.clone());
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let (fwd_token, fwd_pos) = input.peek().expect(NEVER_ENDS);
|
|
|
|
settings.pos = *fwd_pos;
|
2022-11-24 10:08:43 +01:00
|
|
|
let settings = settings.level_up()?;
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-11-22 16:30:43 +01:00
|
|
|
required_token = match parse_func(&segments, &fwd_token.to_string(), &mut user_state) {
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(Some(seg))
|
|
|
|
if seg.starts_with(CUSTOM_SYNTAX_MARKER_SYNTAX_VARIANT)
|
|
|
|
&& seg.len() > CUSTOM_SYNTAX_MARKER_SYNTAX_VARIANT.len() =>
|
|
|
|
{
|
2022-08-11 16:56:23 +02:00
|
|
|
inputs.push(Expr::StringConstant(state.get_interned_string(seg), pos));
|
2022-02-28 07:37:46 +01:00
|
|
|
break;
|
2021-12-27 15:02:34 +01:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(Some(seg)) => seg,
|
|
|
|
Ok(None) => break,
|
|
|
|
Err(err) => return Err(err.0.into_err(settings.pos)),
|
|
|
|
};
|
|
|
|
|
|
|
|
match required_token.as_str() {
|
|
|
|
CUSTOM_SYNTAX_MARKER_IDENT => {
|
|
|
|
let (name, pos) = parse_var_name(input)?;
|
2022-08-13 12:07:42 +02:00
|
|
|
let name = state.get_interned_string(name);
|
2022-03-05 10:57:23 +01:00
|
|
|
|
2022-11-28 16:24:22 +01:00
|
|
|
let ns = Namespace::NONE;
|
2022-03-05 10:57:23 +01:00
|
|
|
|
2022-11-23 11:02:10 +01:00
|
|
|
segments.push(name.clone());
|
2022-08-13 12:07:42 +02:00
|
|
|
tokens.push(state.get_interned_string(CUSTOM_SYNTAX_MARKER_IDENT));
|
2022-03-05 10:57:23 +01:00
|
|
|
inputs.push(Expr::Variable((None, ns, 0, name).into(), None, pos));
|
2021-06-10 04:16:39 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
CUSTOM_SYNTAX_MARKER_SYMBOL => {
|
|
|
|
let (symbol, pos) = parse_symbol(input)?;
|
2022-08-11 16:56:23 +02:00
|
|
|
let symbol = state.get_interned_string(symbol);
|
2022-02-28 07:37:46 +01:00
|
|
|
segments.push(symbol.clone());
|
2022-08-13 12:07:42 +02:00
|
|
|
tokens.push(state.get_interned_string(CUSTOM_SYNTAX_MARKER_SYMBOL));
|
2022-02-28 07:37:46 +01:00
|
|
|
inputs.push(Expr::StringConstant(symbol, pos));
|
2021-06-10 04:16:39 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
CUSTOM_SYNTAX_MARKER_EXPR => {
|
|
|
|
inputs.push(self.parse_expr(input, state, lib, settings)?);
|
2022-08-13 12:07:42 +02:00
|
|
|
let keyword = state.get_interned_string(CUSTOM_SYNTAX_MARKER_EXPR);
|
2022-11-23 11:02:10 +01:00
|
|
|
segments.push(keyword.clone());
|
2022-02-28 07:37:46 +01:00
|
|
|
tokens.push(keyword);
|
2020-10-19 13:21:40 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
CUSTOM_SYNTAX_MARKER_BLOCK => {
|
|
|
|
match self.parse_block(input, state, lib, settings)? {
|
|
|
|
block @ Stmt::Block(..) => {
|
|
|
|
inputs.push(Expr::Stmt(Box::new(block.into())));
|
2022-08-13 12:07:42 +02:00
|
|
|
let keyword = state.get_interned_string(CUSTOM_SYNTAX_MARKER_BLOCK);
|
2022-11-23 11:02:10 +01:00
|
|
|
segments.push(keyword.clone());
|
2022-02-28 07:37:46 +01:00
|
|
|
tokens.push(keyword);
|
|
|
|
}
|
|
|
|
stmt => unreachable!("Stmt::Block expected but gets {:?}", stmt),
|
|
|
|
}
|
2020-10-19 13:21:40 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
CUSTOM_SYNTAX_MARKER_BOOL => match input.next().expect(NEVER_ENDS) {
|
2022-04-21 04:04:46 +02:00
|
|
|
(b @ (Token::True | Token::False), pos) => {
|
2022-02-28 07:37:46 +01:00
|
|
|
inputs.push(Expr::BoolConstant(b == Token::True, pos));
|
2022-08-11 16:56:23 +02:00
|
|
|
segments.push(state.get_interned_string(b.literal_syntax()));
|
2022-08-13 12:07:42 +02:00
|
|
|
tokens.push(state.get_interned_string(CUSTOM_SYNTAX_MARKER_BOOL));
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
(.., pos) => {
|
|
|
|
return Err(
|
2022-11-22 16:30:43 +01:00
|
|
|
PERR::MissingSymbol("Expecting 'true' or 'false'".into()).into_err(pos)
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CUSTOM_SYNTAX_MARKER_INT => match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::IntegerConstant(i), pos) => {
|
|
|
|
inputs.push(Expr::IntegerConstant(i, pos));
|
|
|
|
segments.push(i.to_string().into());
|
2022-08-13 12:07:42 +02:00
|
|
|
tokens.push(state.get_interned_string(CUSTOM_SYNTAX_MARKER_INT));
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
(.., pos) => {
|
|
|
|
return Err(
|
2022-11-22 16:30:43 +01:00
|
|
|
PERR::MissingSymbol("Expecting an integer number".into()).into_err(pos)
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
CUSTOM_SYNTAX_MARKER_FLOAT => match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::FloatConstant(f), pos) => {
|
|
|
|
inputs.push(Expr::FloatConstant(f, pos));
|
|
|
|
segments.push(f.to_string().into());
|
2022-08-13 12:07:42 +02:00
|
|
|
tokens.push(state.get_interned_string(CUSTOM_SYNTAX_MARKER_FLOAT));
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
(.., pos) => {
|
2022-11-22 16:30:43 +01:00
|
|
|
return Err(
|
|
|
|
PERR::MissingSymbol("Expecting a floating-point number".into())
|
|
|
|
.into_err(pos),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
CUSTOM_SYNTAX_MARKER_STRING => match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::StringConstant(s), pos) => {
|
2022-09-25 17:03:18 +02:00
|
|
|
let s = state.get_interned_string(*s);
|
2022-02-28 07:37:46 +01:00
|
|
|
inputs.push(Expr::StringConstant(s.clone(), pos));
|
|
|
|
segments.push(s);
|
2022-08-13 12:07:42 +02:00
|
|
|
tokens.push(state.get_interned_string(CUSTOM_SYNTAX_MARKER_STRING));
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
(.., pos) => {
|
2022-11-22 16:30:43 +01:00
|
|
|
return Err(PERR::MissingSymbol("Expecting a string".into()).into_err(pos))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
s => match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
2022-11-23 04:36:30 +01:00
|
|
|
(Token::Identifier(t) | Token::Reserved(t) | Token::Custom(t), ..)
|
2022-11-22 16:30:43 +01:00
|
|
|
if *t == s =>
|
|
|
|
{
|
|
|
|
segments.push(required_token.clone());
|
2022-11-23 11:02:10 +01:00
|
|
|
tokens.push(required_token.clone());
|
2022-11-22 16:30:43 +01:00
|
|
|
}
|
|
|
|
(t, ..) if t.is_literal() && t.literal_syntax() == s => {
|
2022-02-28 07:37:46 +01:00
|
|
|
segments.push(required_token.clone());
|
2022-11-23 11:02:10 +01:00
|
|
|
tokens.push(required_token.clone());
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
(.., pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
2022-11-22 16:30:43 +01:00
|
|
|
s.into(),
|
2022-02-28 07:37:46 +01:00
|
|
|
format!("for '{}' expression", segments[0]),
|
|
|
|
)
|
|
|
|
.into_err(pos))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2020-10-19 13:21:40 +02:00
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
inputs.shrink_to_fit();
|
|
|
|
tokens.shrink_to_fit();
|
2020-10-19 13:21:40 +02:00
|
|
|
|
2022-07-27 10:04:24 +02:00
|
|
|
let self_terminated = matches!(
|
|
|
|
required_token.as_str(),
|
2022-02-28 07:37:46 +01:00
|
|
|
// It is self-terminating if the last symbol is a block
|
2022-07-27 10:04:24 +02:00
|
|
|
CUSTOM_SYNTAX_MARKER_BLOCK |
|
2022-02-28 07:37:46 +01:00
|
|
|
// If the last symbol is `;` or `}`, it is self-terminating
|
2022-07-27 10:04:24 +02:00
|
|
|
KEYWORD_SEMICOLON | KEYWORD_CLOSE_BRACE
|
|
|
|
);
|
2020-12-28 02:49:54 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(Expr::Custom(
|
2022-07-05 16:59:03 +02:00
|
|
|
crate::ast::CustomExpr {
|
2022-02-28 07:37:46 +01:00
|
|
|
inputs,
|
|
|
|
tokens,
|
2022-09-12 06:03:32 +02:00
|
|
|
state: user_state,
|
2022-02-28 07:37:46 +01:00
|
|
|
scope_may_be_changed: syntax.scope_may_be_changed,
|
|
|
|
self_terminated,
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
pos,
|
|
|
|
))
|
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse an expression.
|
|
|
|
fn parse_expr(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Expr> {
|
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = input.peek().expect(NEVER_ENDS).1;
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Parse expression normally.
|
|
|
|
let precedence = Precedence::new(1);
|
2022-11-24 10:08:43 +01:00
|
|
|
let lhs = self.parse_unary(input, state, lib, settings.level_up()?)?;
|
|
|
|
self.parse_binary_op(input, state, lib, precedence, lhs, settings.level_up()?)
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse an if statement.
|
|
|
|
fn parse_if(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Stmt> {
|
|
|
|
// if ...
|
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = eat_token(input, Token::If);
|
|
|
|
|
|
|
|
// if guard { if_body }
|
|
|
|
ensure_not_statement_expr(input, "a boolean")?;
|
|
|
|
let guard = self
|
2022-11-24 10:08:43 +01:00
|
|
|
.parse_expr(input, state, lib, settings.level_up()?)?
|
2022-02-28 07:37:46 +01:00
|
|
|
.ensure_bool_expr()?;
|
|
|
|
ensure_not_assignment(input)?;
|
2022-11-24 10:08:43 +01:00
|
|
|
let if_body = self.parse_block(input, state, lib, settings.level_up()?)?;
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
// if guard { if_body } else ...
|
|
|
|
let else_body = if match_token(input, Token::Else).0 {
|
|
|
|
if let (Token::If, ..) = input.peek().expect(NEVER_ENDS) {
|
|
|
|
// if guard { if_body } else if ...
|
2022-11-24 10:08:43 +01:00
|
|
|
self.parse_if(input, state, lib, settings.level_up()?)?
|
2022-02-28 07:37:46 +01:00
|
|
|
} else {
|
|
|
|
// if guard { if_body } else { else-body }
|
2022-11-24 10:08:43 +01:00
|
|
|
self.parse_block(input, state, lib, settings.level_up()?)?
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Stmt::Noop(Position::NONE)
|
|
|
|
};
|
2021-07-04 10:40:15 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(Stmt::If(
|
|
|
|
(guard, if_body.into(), else_body.into()).into(),
|
|
|
|
settings.pos,
|
|
|
|
))
|
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse a while loop.
|
|
|
|
fn parse_while_loop(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Stmt> {
|
|
|
|
let mut settings = settings;
|
2017-10-30 16:08:44 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// while|loops ...
|
|
|
|
let (guard, token_pos) = match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::While, pos) => {
|
|
|
|
ensure_not_statement_expr(input, "a boolean")?;
|
|
|
|
let expr = self
|
2022-11-24 10:08:43 +01:00
|
|
|
.parse_expr(input, state, lib, settings.level_up()?)?
|
2022-02-28 07:37:46 +01:00
|
|
|
.ensure_bool_expr()?;
|
|
|
|
ensure_not_assignment(input)?;
|
|
|
|
(expr, pos)
|
|
|
|
}
|
|
|
|
(Token::Loop, pos) => (Expr::Unit(Position::NONE), pos),
|
|
|
|
token => unreachable!("Token::While or Token::Loop expected but gets {:?}", token),
|
|
|
|
};
|
|
|
|
settings.pos = token_pos;
|
2022-11-23 04:36:30 +01:00
|
|
|
settings.flags |= ParseSettingFlags::BREAKABLE;
|
2020-12-28 02:49:54 +01:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
let body = self.parse_block(input, state, lib, settings.level_up()?)?;
|
2017-10-30 16:08:44 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(Stmt::While((guard, body.into()).into(), settings.pos))
|
|
|
|
}
|
2020-11-20 15:23:37 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse a do loop.
|
|
|
|
fn parse_do(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Stmt> {
|
|
|
|
// do ...
|
|
|
|
let mut settings = settings;
|
2022-11-30 07:11:09 +01:00
|
|
|
let orig_breakable = settings.flags.contains(ParseSettingFlags::BREAKABLE);
|
|
|
|
settings.flags |= ParseSettingFlags::BREAKABLE;
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
settings.pos = eat_token(input, Token::Do);
|
2020-11-20 15:23:37 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// do { body } [while|until] guard
|
2022-11-30 07:11:09 +01:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
let body = self.parse_block(input, state, lib, settings.level_up()?)?;
|
2017-10-30 16:08:44 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let negated = match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::While, ..) => ASTFlags::NONE,
|
|
|
|
(Token::Until, ..) => ASTFlags::NEGATED,
|
|
|
|
(.., pos) => {
|
|
|
|
return Err(
|
|
|
|
PERR::MissingToken(Token::While.into(), "for the do statement".into())
|
|
|
|
.into_err(pos),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
2021-06-07 05:01:16 +02:00
|
|
|
|
2022-11-30 07:11:09 +01:00
|
|
|
if !orig_breakable {
|
|
|
|
settings.flags &= !ParseSettingFlags::BREAKABLE;
|
|
|
|
}
|
2021-06-07 05:01:16 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
ensure_not_statement_expr(input, "a boolean")?;
|
|
|
|
let guard = self
|
2022-11-24 10:08:43 +01:00
|
|
|
.parse_expr(input, state, lib, settings.level_up()?)?
|
2022-02-28 07:37:46 +01:00
|
|
|
.ensure_bool_expr()?;
|
|
|
|
ensure_not_assignment(input)?;
|
2020-02-23 15:48:46 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(Stmt::Do((guard, body.into()).into(), negated, settings.pos))
|
2020-02-23 15:48:46 +01:00
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse a for loop.
|
|
|
|
fn parse_for(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Stmt> {
|
|
|
|
// for ...
|
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = eat_token(input, Token::For);
|
2020-04-28 17:05:03 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// for name ...
|
|
|
|
let (name, name_pos, counter_name, counter_pos) = if match_token(input, Token::LeftParen).0
|
|
|
|
{
|
|
|
|
// ( name, counter )
|
|
|
|
let (name, name_pos) = parse_var_name(input)?;
|
|
|
|
let (has_comma, pos) = match_token(input, Token::Comma);
|
|
|
|
if !has_comma {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::Comma.into(),
|
|
|
|
"after the iteration variable name".into(),
|
|
|
|
)
|
|
|
|
.into_err(pos));
|
|
|
|
}
|
|
|
|
let (counter_name, counter_pos) = parse_var_name(input)?;
|
2021-06-07 05:01:16 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
if counter_name == name {
|
2022-11-22 16:30:43 +01:00
|
|
|
return Err(PERR::DuplicatedVariable(counter_name.into()).into_err(counter_pos));
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2020-02-23 15:48:46 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let (has_close_paren, pos) = match_token(input, Token::RightParen);
|
|
|
|
if !has_close_paren {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::RightParen.into(),
|
|
|
|
"to close the iteration variable".into(),
|
|
|
|
)
|
|
|
|
.into_err(pos));
|
|
|
|
}
|
2022-03-05 10:57:23 +01:00
|
|
|
(name, name_pos, counter_name, counter_pos)
|
2022-02-28 07:37:46 +01:00
|
|
|
} else {
|
|
|
|
// name
|
|
|
|
let (name, name_pos) = parse_var_name(input)?;
|
2022-03-05 10:57:23 +01:00
|
|
|
(name, name_pos, Identifier::new_const(), Position::NONE)
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
2020-03-16 16:51:32 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// for name in ...
|
|
|
|
match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::In, ..) => (),
|
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
|
|
|
(.., pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::In.into(),
|
|
|
|
"after the iteration variable".into(),
|
|
|
|
)
|
|
|
|
.into_err(pos))
|
|
|
|
}
|
|
|
|
}
|
2020-12-28 02:49:54 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// for name in expr { body }
|
|
|
|
ensure_not_statement_expr(input, "a boolean")?;
|
|
|
|
let expr = self
|
2022-11-24 10:08:43 +01:00
|
|
|
.parse_expr(input, state, lib, settings.level_up()?)?
|
2022-02-28 07:37:46 +01:00
|
|
|
.ensure_iterable()?;
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2022-03-05 10:57:23 +01:00
|
|
|
let counter_var = Ident {
|
2022-08-13 12:07:42 +02:00
|
|
|
name: state.get_interned_string(counter_name),
|
2022-03-05 10:57:23 +01:00
|
|
|
pos: counter_pos,
|
|
|
|
};
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
let loop_var = Ident {
|
2022-11-24 15:25:19 +01:00
|
|
|
name: state.get_interned_string(name),
|
2022-02-28 07:37:46 +01:00
|
|
|
pos: name_pos,
|
2022-02-13 11:46:25 +01:00
|
|
|
};
|
|
|
|
|
2022-11-24 15:25:19 +01:00
|
|
|
let prev_stack_len = {
|
2022-11-25 02:46:13 +01:00
|
|
|
let stack = state.stack.get_or_insert_with(Default::default);
|
2022-11-24 15:25:19 +01:00
|
|
|
|
|
|
|
let prev_stack_len = stack.len();
|
|
|
|
|
|
|
|
if !counter_var.name.is_empty() {
|
|
|
|
stack.push(counter_var.name.clone(), ());
|
|
|
|
}
|
|
|
|
stack.push(&loop_var.name, ());
|
|
|
|
|
|
|
|
prev_stack_len
|
|
|
|
};
|
|
|
|
|
2022-11-23 04:36:30 +01:00
|
|
|
settings.flags |= ParseSettingFlags::BREAKABLE;
|
2022-11-24 10:08:43 +01:00
|
|
|
let body = self.parse_block(input, state, lib, settings.level_up()?)?;
|
2022-02-13 11:46:25 +01:00
|
|
|
|
2022-11-25 13:42:16 +01:00
|
|
|
state.stack.as_deref_mut().unwrap().rewind(prev_stack_len);
|
2021-02-03 12:14:26 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(Stmt::For(
|
|
|
|
Box::new((loop_var, counter_var, expr, body.into())),
|
|
|
|
settings.pos,
|
|
|
|
))
|
|
|
|
}
|
2020-10-09 05:15:25 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse a variable definition statement.
|
|
|
|
fn parse_let(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
access: AccessMode,
|
|
|
|
is_export: bool,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Stmt> {
|
|
|
|
// let/const... (specified in `var_type`)
|
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = input.next().expect(NEVER_ENDS).1;
|
2022-02-18 08:04:46 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// let name ...
|
|
|
|
let (name, pos) = parse_var_name(input)?;
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
{
|
2022-11-25 02:46:13 +01:00
|
|
|
let stack = state.stack.get_or_insert_with(Default::default);
|
2022-11-24 10:08:43 +01:00
|
|
|
|
2022-11-24 15:25:19 +01:00
|
|
|
if !self.allow_shadowing() && stack.iter().any(|(v, ..)| v == name) {
|
|
|
|
return Err(PERR::VariableExists(name.into()).into_err(pos));
|
2022-11-24 10:08:43 +01:00
|
|
|
}
|
|
|
|
|
2022-11-24 15:25:19 +01:00
|
|
|
if let Some(ref filter) = self.def_var_filter {
|
|
|
|
let will_shadow = stack.iter().any(|(v, ..)| v == name);
|
2022-11-08 08:01:40 +01:00
|
|
|
|
2022-11-24 15:25:19 +01:00
|
|
|
let global = state
|
|
|
|
.global
|
|
|
|
.get_or_insert_with(|| GlobalRuntimeState::new(self).into());
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2022-11-24 15:25:19 +01:00
|
|
|
global.level = settings.level;
|
|
|
|
let is_const = access == AccessMode::ReadOnly;
|
|
|
|
let info = VarDefInfo {
|
|
|
|
name: &name,
|
|
|
|
is_const,
|
|
|
|
nesting_level: settings.level,
|
|
|
|
will_shadow,
|
|
|
|
};
|
|
|
|
let caches = &mut Caches::new();
|
|
|
|
let mut this = Dynamic::NULL;
|
|
|
|
|
|
|
|
let context = EvalContext::new(self, global, caches, stack, &mut this);
|
|
|
|
|
|
|
|
match filter(false, info, context) {
|
|
|
|
Ok(true) => (),
|
|
|
|
Ok(false) => return Err(PERR::ForbiddenVariable(name.into()).into_err(pos)),
|
|
|
|
Err(err) => match *err {
|
|
|
|
EvalAltResult::ErrorParsing(e, pos) => return Err(e.into_err(pos)),
|
|
|
|
_ => return Err(PERR::ForbiddenVariable(name.into()).into_err(pos)),
|
|
|
|
},
|
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-28 02:49:54 +01:00
|
|
|
|
2022-08-13 12:07:42 +02:00
|
|
|
let name = state.get_interned_string(name);
|
2020-05-04 13:36:58 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// let name = ...
|
|
|
|
let expr = if match_token(input, Token::Equals).0 {
|
|
|
|
// let name = expr
|
2022-11-24 10:08:43 +01:00
|
|
|
self.parse_expr(input, state, lib, settings.level_up()?)?
|
2022-02-28 07:37:46 +01:00
|
|
|
} else {
|
|
|
|
Expr::Unit(Position::NONE)
|
|
|
|
};
|
2020-05-04 13:36:58 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let export = if is_export {
|
|
|
|
ASTFlags::EXPORTED
|
|
|
|
} else {
|
|
|
|
ASTFlags::NONE
|
|
|
|
};
|
2020-07-28 04:26:20 +02:00
|
|
|
|
2022-02-28 15:13:03 +01:00
|
|
|
let (existing, hit_barrier) = state.find_var(&name);
|
2022-11-24 10:08:43 +01:00
|
|
|
|
2022-11-25 13:42:16 +01:00
|
|
|
let stack = state.stack.as_deref_mut().unwrap();
|
2022-11-24 10:08:43 +01:00
|
|
|
|
2022-02-28 15:13:03 +01:00
|
|
|
let existing = if !hit_barrier && existing > 0 {
|
2022-11-24 10:08:43 +01:00
|
|
|
let offset = stack.len() - existing;
|
2022-02-28 15:13:03 +01:00
|
|
|
if offset < state.block_stack_len {
|
2022-02-28 07:37:46 +01:00
|
|
|
// Defined in parent block
|
|
|
|
None
|
|
|
|
} else {
|
2022-02-28 15:13:03 +01:00
|
|
|
Some(offset)
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2022-02-28 15:13:03 +01:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2020-05-04 13:36:58 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let idx = if let Some(n) = existing {
|
2022-11-24 10:08:43 +01:00
|
|
|
stack.get_mut_by_index(n).set_access_mode(access);
|
2022-11-24 15:25:19 +01:00
|
|
|
Some(NonZeroUsize::new(stack.len() - n).unwrap())
|
2022-02-28 07:37:46 +01:00
|
|
|
} else {
|
2022-11-24 10:08:43 +01:00
|
|
|
stack.push_entry(name.as_str(), access, Dynamic::UNIT);
|
2022-02-28 07:37:46 +01:00
|
|
|
None
|
|
|
|
};
|
2020-11-09 05:21:11 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let var_def = (Ident { name, pos }, expr, idx).into();
|
2020-12-28 02:49:54 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(match access {
|
|
|
|
// let name = expr
|
|
|
|
AccessMode::ReadWrite => Stmt::Var(var_def, export, settings.pos),
|
|
|
|
// const name = { expr:constant }
|
|
|
|
AccessMode::ReadOnly => Stmt::Var(var_def, ASTFlags::CONSTANT | export, settings.pos),
|
|
|
|
})
|
2020-11-09 05:21:11 +01:00
|
|
|
}
|
2020-05-08 10:49:24 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse an import statement.
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
fn parse_import(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Stmt> {
|
|
|
|
// import ...
|
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = eat_token(input, Token::Import);
|
2020-05-08 10:49:24 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// import expr ...
|
2022-11-24 10:08:43 +01:00
|
|
|
let expr = self.parse_expr(input, state, lib, settings.level_up()?)?;
|
2020-05-08 10:49:24 +02:00
|
|
|
|
2022-11-23 11:02:10 +01:00
|
|
|
let export = if match_token(input, Token::As).0 {
|
2022-10-14 06:14:28 +02:00
|
|
|
// import expr as name ...
|
|
|
|
let (name, pos) = parse_var_name(input)?;
|
|
|
|
Ident {
|
|
|
|
name: state.get_interned_string(name),
|
|
|
|
pos,
|
|
|
|
}
|
2022-11-23 11:02:10 +01:00
|
|
|
} else {
|
|
|
|
// import expr;
|
|
|
|
Ident {
|
|
|
|
name: state.get_interned_string(""),
|
|
|
|
pos: Position::NONE,
|
|
|
|
}
|
2022-10-14 06:14:28 +02:00
|
|
|
};
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2022-11-25 05:14:40 +01:00
|
|
|
state
|
|
|
|
.imports
|
|
|
|
.get_or_insert_with(Default::default)
|
|
|
|
.push(export.name.clone());
|
2020-11-21 06:05:57 +01:00
|
|
|
|
2022-10-14 06:14:28 +02:00
|
|
|
Ok(Stmt::Import((expr, export).into(), settings.pos))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2020-10-18 16:10:08 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse an export statement.
|
2020-10-18 16:10:08 +02:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-02-28 07:37:46 +01:00
|
|
|
fn parse_export(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Stmt> {
|
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = eat_token(input, Token::Export);
|
2016-03-01 21:16:10 +01:00
|
|
|
|
2021-05-22 13:14:24 +02:00
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
2022-02-28 07:37:46 +01:00
|
|
|
(Token::Let, pos) => {
|
|
|
|
let pos = *pos;
|
|
|
|
let mut stmt =
|
|
|
|
self.parse_let(input, state, lib, AccessMode::ReadWrite, true, settings)?;
|
|
|
|
stmt.set_position(pos);
|
|
|
|
return Ok(stmt);
|
|
|
|
}
|
|
|
|
(Token::Const, pos) => {
|
|
|
|
let pos = *pos;
|
|
|
|
let mut stmt =
|
|
|
|
self.parse_let(input, state, lib, AccessMode::ReadOnly, true, settings)?;
|
|
|
|
stmt.set_position(pos);
|
|
|
|
return Ok(stmt);
|
2021-04-20 05:22:38 +02:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let (id, id_pos) = parse_var_name(input)?;
|
2020-07-06 16:57:50 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let (alias, alias_pos) = if match_token(input, Token::As).0 {
|
|
|
|
let (name, pos) = parse_var_name(input)?;
|
|
|
|
(Some(name), pos)
|
|
|
|
} else {
|
|
|
|
(None, Position::NONE)
|
|
|
|
};
|
2020-12-29 03:41:20 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let export = (
|
|
|
|
Ident {
|
2022-08-13 12:07:42 +02:00
|
|
|
name: state.get_interned_string(id),
|
2022-02-28 07:37:46 +01:00
|
|
|
pos: id_pos,
|
|
|
|
},
|
|
|
|
Ident {
|
2022-11-25 13:42:16 +01:00
|
|
|
name: state.get_interned_string(alias.as_deref().unwrap_or("")),
|
2022-02-28 07:37:46 +01:00
|
|
|
pos: alias_pos,
|
|
|
|
},
|
|
|
|
);
|
2016-03-01 21:16:10 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(Stmt::Export(export.into(), settings.pos))
|
|
|
|
}
|
2017-10-02 23:44:45 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse a statement block.
|
|
|
|
fn parse_block(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Stmt> {
|
|
|
|
// Must start with {
|
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::LeftBrace, pos) => pos,
|
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
2022-02-08 02:02:15 +01:00
|
|
|
(.., pos) => {
|
2020-05-04 13:36:58 +02:00
|
|
|
return Err(PERR::MissingToken(
|
2022-02-28 07:37:46 +01:00
|
|
|
Token::LeftBrace.into(),
|
|
|
|
"to start a statement block".into(),
|
2020-05-04 13:36:58 +02:00
|
|
|
)
|
2022-02-28 07:37:46 +01:00
|
|
|
.into_err(pos))
|
2019-09-18 12:21:07 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2022-06-08 11:06:49 +02:00
|
|
|
let mut statements = StaticVec::new_const();
|
2020-10-18 16:10:08 +02:00
|
|
|
|
2022-11-23 04:36:30 +01:00
|
|
|
if settings.has_flag(ParseSettingFlags::DISALLOW_STATEMENTS_IN_BLOCKS) {
|
2022-11-24 10:08:43 +01:00
|
|
|
let stmt = self.parse_expr_stmt(input, state, lib, settings.level_up()?)?;
|
2022-08-26 17:10:58 +02:00
|
|
|
statements.push(stmt);
|
|
|
|
|
|
|
|
// Must end with }
|
|
|
|
return match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::RightBrace, pos) => Ok((statements, settings.pos, pos).into()),
|
|
|
|
(Token::LexError(err), pos) => Err(err.into_err(pos)),
|
|
|
|
(.., pos) => Err(PERR::MissingToken(
|
|
|
|
Token::LeftBrace.into(),
|
|
|
|
"to start a statement block".into(),
|
|
|
|
)
|
|
|
|
.into_err(pos)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let prev_entry_stack_len = state.block_stack_len;
|
2022-11-25 13:42:16 +01:00
|
|
|
state.block_stack_len = state.stack.as_deref().map_or(0, Scope::len);
|
2020-04-28 17:05:03 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-11-25 13:42:16 +01:00
|
|
|
let orig_imports_len = state.imports.as_deref().map_or(0, StaticVec::len);
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let end_pos = loop {
|
|
|
|
// Terminated?
|
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
(Token::RightBrace, ..) => break eat_token(input, Token::RightBrace),
|
|
|
|
(Token::EOF, pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::RightBrace.into(),
|
|
|
|
"to terminate this block".into(),
|
|
|
|
)
|
|
|
|
.into_err(*pos));
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Parse statements inside the block
|
2022-11-23 04:36:30 +01:00
|
|
|
settings.flags &= !ParseSettingFlags::GLOBAL_LEVEL;
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
let stmt = self.parse_stmt(input, state, lib, settings.level_up()?)?;
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
if stmt.is_noop() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if it needs a terminating semicolon
|
|
|
|
let need_semicolon = !stmt.is_self_terminated();
|
|
|
|
|
|
|
|
statements.push(stmt);
|
|
|
|
|
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
// { ... stmt }
|
|
|
|
(Token::RightBrace, ..) => break eat_token(input, Token::RightBrace),
|
|
|
|
// { ... stmt;
|
|
|
|
(Token::SemiColon, ..) if need_semicolon => {
|
|
|
|
eat_token(input, Token::SemiColon);
|
|
|
|
}
|
|
|
|
// { ... { stmt } ;
|
|
|
|
(Token::SemiColon, ..) if !need_semicolon => {
|
|
|
|
eat_token(input, Token::SemiColon);
|
|
|
|
}
|
|
|
|
// { ... { stmt } ???
|
|
|
|
_ if !need_semicolon => (),
|
|
|
|
// { ... stmt <error>
|
|
|
|
(Token::LexError(err), err_pos) => return Err(err.clone().into_err(*err_pos)),
|
|
|
|
// { ... stmt ???
|
|
|
|
(.., pos) => {
|
|
|
|
// Semicolons are not optional between statements
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::SemiColon.into(),
|
|
|
|
"to terminate this statement".into(),
|
|
|
|
)
|
|
|
|
.into_err(*pos));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
if let Some(ref mut s) = state.stack {
|
|
|
|
s.rewind(state.block_stack_len);
|
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
state.block_stack_len = prev_entry_stack_len;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-11-25 05:14:40 +01:00
|
|
|
if let Some(ref mut imports) = state.imports {
|
|
|
|
imports.truncate(orig_imports_len);
|
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
Ok((statements, settings.pos, end_pos).into())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse an expression as a statement.
|
|
|
|
fn parse_expr_stmt(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Stmt> {
|
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = input.peek().expect(NEVER_ENDS).1;
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
let expr = self.parse_expr(input, state, lib, settings.level_up()?)?;
|
|
|
|
let stmt = self.parse_op_assignment_stmt(input, state, lib, expr, settings.level_up()?)?;
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(stmt)
|
|
|
|
}
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse a single statement.
|
|
|
|
fn parse_stmt(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Stmt> {
|
|
|
|
use AccessMode::{ReadOnly, ReadWrite};
|
2021-08-13 07:42:39 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let mut settings = settings;
|
2020-12-12 13:09:29 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[cfg(feature = "metadata")]
|
|
|
|
let comments = {
|
|
|
|
let mut comments = StaticVec::<SmartString>::new();
|
|
|
|
let mut comments_pos = Position::NONE;
|
|
|
|
|
|
|
|
// Handle doc-comments.
|
|
|
|
while let (Token::Comment(ref comment), pos) = input.peek().expect(NEVER_ENDS) {
|
|
|
|
if comments_pos.is_none() {
|
|
|
|
comments_pos = *pos;
|
|
|
|
}
|
2020-12-12 13:09:29 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
if !crate::tokenizer::is_doc_comment(comment) {
|
|
|
|
unreachable!("doc-comment expected but gets {:?}", comment);
|
|
|
|
}
|
2020-12-29 05:29:45 +01:00
|
|
|
|
2022-11-23 04:36:30 +01:00
|
|
|
if !settings.has_flag(ParseSettingFlags::GLOBAL_LEVEL) {
|
2022-02-28 07:37:46 +01:00
|
|
|
return Err(PERR::WrongDocComment.into_err(comments_pos));
|
|
|
|
}
|
2020-12-12 13:09:29 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match input.next().expect(NEVER_ENDS).0 {
|
|
|
|
Token::Comment(comment) => {
|
2022-09-25 17:03:18 +02:00
|
|
|
comments.push(*comment);
|
2020-12-12 13:09:29 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
2022-04-21 04:04:46 +02:00
|
|
|
(Token::Fn | Token::Private, ..) => break,
|
2022-02-28 07:37:46 +01:00
|
|
|
(Token::Comment(..), ..) => (),
|
|
|
|
_ => return Err(PERR::WrongDocComment.into_err(comments_pos)),
|
|
|
|
}
|
2020-12-29 05:29:45 +01:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
token => unreachable!("Token::Comment expected but gets {:?}", token),
|
2020-12-28 07:21:13 +01:00
|
|
|
}
|
2020-12-12 13:09:29 +01:00
|
|
|
}
|
2021-04-09 16:49:47 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
comments
|
|
|
|
};
|
2020-07-26 09:53:22 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let (token, token_pos) = match input.peek().expect(NEVER_ENDS) {
|
|
|
|
(Token::EOF, pos) => return Ok(Stmt::Noop(*pos)),
|
|
|
|
(x, pos) => (x, *pos),
|
|
|
|
};
|
|
|
|
settings.pos = token_pos;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match token {
|
|
|
|
// ; - empty statement
|
|
|
|
Token::SemiColon => {
|
|
|
|
eat_token(input, Token::SemiColon);
|
|
|
|
Ok(Stmt::Noop(token_pos))
|
|
|
|
}
|
2020-04-01 10:22:18 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// { - statements block
|
2022-11-24 10:08:43 +01:00
|
|
|
Token::LeftBrace => Ok(self.parse_block(input, state, lib, settings.level_up()?)?),
|
2020-07-06 16:57:50 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// fn ...
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
2022-11-23 04:36:30 +01:00
|
|
|
Token::Fn if !settings.has_flag(ParseSettingFlags::GLOBAL_LEVEL) => {
|
2022-08-10 06:48:37 +02:00
|
|
|
Err(PERR::WrongFnDefinition.into_err(token_pos))
|
|
|
|
}
|
2020-07-06 16:57:50 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
Token::Fn | Token::Private => {
|
|
|
|
let access = if matches!(token, Token::Private) {
|
|
|
|
eat_token(input, Token::Private);
|
|
|
|
crate::FnAccess::Private
|
|
|
|
} else {
|
|
|
|
crate::FnAccess::Public
|
|
|
|
};
|
2021-04-04 18:05:56 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::Fn, pos) => {
|
2022-08-14 08:32:16 +02:00
|
|
|
// Build new parse state
|
2022-11-25 13:42:16 +01:00
|
|
|
let new_state = &mut ParseState::new(
|
2022-08-12 10:34:57 +02:00
|
|
|
state.scope,
|
2022-11-24 08:10:17 +01:00
|
|
|
state.interned_strings,
|
2022-08-12 10:34:57 +02:00
|
|
|
state.tokenizer_control.clone(),
|
|
|
|
);
|
2022-06-09 12:22:53 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-08-09 15:35:45 +02:00
|
|
|
{
|
|
|
|
// Do not allow storing an index to a globally-imported module
|
|
|
|
// just in case the function is separated from this `AST`.
|
|
|
|
//
|
|
|
|
// Keep them in `global_imports` instead so that strict variables
|
|
|
|
// mode will not complain.
|
|
|
|
new_state.global_imports.clone_from(&state.global_imports);
|
|
|
|
new_state
|
|
|
|
.global_imports
|
2022-11-25 05:14:40 +01:00
|
|
|
.get_or_insert_with(Default::default)
|
2022-11-25 16:03:20 +01:00
|
|
|
.extend(state.imports.as_deref().into_iter().flatten().cloned());
|
2022-08-09 15:35:45 +02:00
|
|
|
}
|
2020-07-06 16:57:50 +02:00
|
|
|
|
2022-11-30 07:11:09 +01:00
|
|
|
// Brand new options
|
2022-11-23 04:36:30 +01:00
|
|
|
let options = self.options | (settings.options & LangOptions::STRICT_VAR);
|
|
|
|
|
2022-11-30 07:11:09 +01:00
|
|
|
// Brand new flags, turn on function scope
|
2022-11-23 04:36:30 +01:00
|
|
|
let flags = ParseSettingFlags::FN_SCOPE
|
|
|
|
| (settings.flags
|
|
|
|
& ParseSettingFlags::DISALLOW_UNQUOTED_MAP_PROPERTIES);
|
2022-05-19 15:40:22 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let new_settings = ParseSettings {
|
2022-11-23 04:36:30 +01:00
|
|
|
flags,
|
2022-02-28 07:37:46 +01:00
|
|
|
level: 0,
|
2022-05-19 15:40:22 +02:00
|
|
|
options,
|
2022-02-28 07:37:46 +01:00
|
|
|
pos,
|
2022-11-24 10:08:43 +01:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
max_expr_depth: self.max_function_expr_depth(),
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
|
|
|
|
2022-11-29 08:50:58 +01:00
|
|
|
let f = self.parse_fn(
|
2022-02-28 07:37:46 +01:00
|
|
|
input,
|
2022-11-25 13:42:16 +01:00
|
|
|
new_state,
|
2022-02-28 07:37:46 +01:00
|
|
|
lib,
|
|
|
|
access,
|
|
|
|
new_settings,
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[cfg(feature = "metadata")]
|
|
|
|
comments,
|
2022-11-29 08:50:58 +01:00
|
|
|
)?;
|
2022-08-12 10:34:57 +02:00
|
|
|
|
2022-08-14 08:32:16 +02:00
|
|
|
// Restore parse state
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-11-29 08:50:58 +01:00
|
|
|
let hash = calc_fn_hash(None, &f.name, f.params.len());
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-03-03 06:02:57 +01:00
|
|
|
if !lib.is_empty() && lib.contains_key(&hash) {
|
2022-02-28 07:37:46 +01:00
|
|
|
return Err(PERR::FnDuplicatedDefinition(
|
2022-11-29 08:50:58 +01:00
|
|
|
f.name.to_string(),
|
|
|
|
f.params.len(),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
|
|
|
.into_err(pos));
|
|
|
|
}
|
2021-04-09 16:49:47 +02:00
|
|
|
|
2022-11-29 08:50:58 +01:00
|
|
|
lib.insert(hash, f.into());
|
2021-01-03 13:54:08 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(Stmt::Noop(pos))
|
2021-01-03 13:54:08 +01:00
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
(.., pos) => Err(PERR::MissingToken(
|
|
|
|
Token::Fn.into(),
|
2022-11-22 16:30:43 +01:00
|
|
|
format!("following '{}'", Token::Private),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
|
|
|
.into_err(pos)),
|
2020-07-06 16:57:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
Token::If => self.parse_if(input, state, lib, settings.level_up()?),
|
|
|
|
Token::Switch => self.parse_switch(input, state, lib, settings.level_up()?),
|
2022-02-28 09:32:08 +01:00
|
|
|
Token::While | Token::Loop if self.allow_looping() => {
|
2022-11-24 10:08:43 +01:00
|
|
|
self.parse_while_loop(input, state, lib, settings.level_up()?)
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2022-02-28 09:32:08 +01:00
|
|
|
Token::Do if self.allow_looping() => {
|
2022-11-24 10:08:43 +01:00
|
|
|
self.parse_do(input, state, lib, settings.level_up()?)
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2022-02-28 09:32:08 +01:00
|
|
|
Token::For if self.allow_looping() => {
|
2022-11-24 10:08:43 +01:00
|
|
|
self.parse_for(input, state, lib, settings.level_up()?)
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2020-04-01 10:22:18 +02:00
|
|
|
|
2022-11-23 04:36:30 +01:00
|
|
|
Token::Continue
|
|
|
|
if self.allow_looping() && settings.has_flag(ParseSettingFlags::BREAKABLE) =>
|
|
|
|
{
|
2022-02-28 07:37:46 +01:00
|
|
|
let pos = eat_token(input, Token::Continue);
|
2022-10-29 06:09:18 +02:00
|
|
|
Ok(Stmt::BreakLoop(None, ASTFlags::NONE, pos))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2022-11-23 04:36:30 +01:00
|
|
|
Token::Break
|
|
|
|
if self.allow_looping() && settings.has_flag(ParseSettingFlags::BREAKABLE) =>
|
|
|
|
{
|
2022-02-28 07:37:46 +01:00
|
|
|
let pos = eat_token(input, Token::Break);
|
2022-10-29 06:09:18 +02:00
|
|
|
|
|
|
|
let expr = match input.peek().expect(NEVER_ENDS) {
|
|
|
|
// `break` at <EOF>
|
|
|
|
(Token::EOF, ..) => None,
|
|
|
|
// `break` at end of block
|
|
|
|
(Token::RightBrace, ..) => None,
|
|
|
|
// `break;`
|
|
|
|
(Token::SemiColon, ..) => None,
|
|
|
|
// `break` with expression
|
|
|
|
_ => Some(
|
2022-11-24 10:08:43 +01:00
|
|
|
self.parse_expr(input, state, lib, settings.level_up()?)?
|
2022-10-29 06:09:18 +02:00
|
|
|
.into(),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Stmt::BreakLoop(expr, ASTFlags::BREAK, pos))
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2022-02-28 09:32:08 +01:00
|
|
|
Token::Continue | Token::Break if self.allow_looping() => {
|
2022-02-28 07:37:46 +01:00
|
|
|
Err(PERR::LoopBreak.into_err(token_pos))
|
|
|
|
}
|
2020-04-01 10:22:18 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Token::Return | Token::Throw => {
|
|
|
|
let (return_type, token_pos) = input
|
|
|
|
.next()
|
|
|
|
.map(|(token, pos)| {
|
|
|
|
let flags = match token {
|
|
|
|
Token::Return => ASTFlags::NONE,
|
|
|
|
Token::Throw => ASTFlags::BREAK,
|
|
|
|
token => unreachable!(
|
|
|
|
"Token::Return or Token::Throw expected but gets {:?}",
|
|
|
|
token
|
|
|
|
),
|
|
|
|
};
|
|
|
|
(flags, pos)
|
|
|
|
})
|
|
|
|
.expect(NEVER_ENDS);
|
2020-03-03 11:15:20 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
|
|
|
// `return`/`throw` at <EOF>
|
|
|
|
(Token::EOF, ..) => Ok(Stmt::Return(None, return_type, token_pos)),
|
|
|
|
// `return`/`throw` at end of block
|
2022-11-23 04:36:30 +01:00
|
|
|
(Token::RightBrace, ..)
|
|
|
|
if !settings.has_flag(ParseSettingFlags::GLOBAL_LEVEL) =>
|
|
|
|
{
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(Stmt::Return(None, return_type, token_pos))
|
|
|
|
}
|
|
|
|
// `return;` or `throw;`
|
|
|
|
(Token::SemiColon, ..) => Ok(Stmt::Return(None, return_type, token_pos)),
|
|
|
|
// `return` or `throw` with expression
|
|
|
|
_ => {
|
2022-11-24 10:08:43 +01:00
|
|
|
let expr = self.parse_expr(input, state, lib, settings.level_up()?)?;
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(Stmt::Return(Some(expr.into()), return_type, token_pos))
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
}
|
2020-04-01 10:22:18 +02:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
Token::Try => self.parse_try_catch(input, state, lib, settings.level_up()?),
|
2020-10-20 17:16:03 +02:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
Token::Let => self.parse_let(input, state, lib, ReadWrite, false, settings.level_up()?),
|
|
|
|
Token::Const => {
|
|
|
|
self.parse_let(input, state, lib, ReadOnly, false, settings.level_up()?)
|
|
|
|
}
|
2020-07-01 16:21:43 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-11-24 10:08:43 +01:00
|
|
|
Token::Import => self.parse_import(input, state, lib, settings.level_up()?),
|
2020-05-04 13:36:58 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-11-23 04:36:30 +01:00
|
|
|
Token::Export if !settings.has_flag(ParseSettingFlags::GLOBAL_LEVEL) => {
|
2022-08-10 06:48:37 +02:00
|
|
|
Err(PERR::WrongExport.into_err(token_pos))
|
|
|
|
}
|
2020-05-08 10:49:24 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2022-11-24 10:08:43 +01:00
|
|
|
Token::Export => self.parse_export(input, state, lib, settings.level_up()?),
|
2020-05-08 10:49:24 +02:00
|
|
|
|
2022-11-24 10:08:43 +01:00
|
|
|
_ => self.parse_expr_stmt(input, state, lib, settings.level_up()?),
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse a try/catch statement.
|
|
|
|
fn parse_try_catch(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<Stmt> {
|
|
|
|
// try ...
|
|
|
|
let mut settings = settings;
|
|
|
|
settings.pos = eat_token(input, Token::Try);
|
2020-10-20 17:16:03 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// try { try_block }
|
2022-11-24 10:08:43 +01:00
|
|
|
let try_block = self.parse_block(input, state, lib, settings.level_up()?)?;
|
2020-10-20 17:16:03 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// try { try_block } catch
|
|
|
|
let (matched, catch_pos) = match_token(input, Token::Catch);
|
2020-10-20 17:16:03 +02:00
|
|
|
|
|
|
|
if !matched {
|
2022-02-28 07:37:46 +01:00
|
|
|
return Err(
|
|
|
|
PERR::MissingToken(Token::Catch.into(), "for the 'try' statement".into())
|
|
|
|
.into_err(catch_pos),
|
|
|
|
);
|
2020-10-20 17:16:03 +02:00
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// try { try_block } catch (
|
|
|
|
let catch_var = if match_token(input, Token::LeftParen).0 {
|
|
|
|
let (name, pos) = parse_var_name(input)?;
|
|
|
|
let (matched, err_pos) = match_token(input, Token::RightParen);
|
|
|
|
|
|
|
|
if !matched {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::RightParen.into(),
|
|
|
|
"to enclose the catch variable".into(),
|
|
|
|
)
|
|
|
|
.into_err(err_pos));
|
|
|
|
}
|
2020-10-20 17:16:03 +02:00
|
|
|
|
2022-08-13 12:07:42 +02:00
|
|
|
let name = state.get_interned_string(name);
|
2022-11-24 15:25:19 +01:00
|
|
|
state
|
|
|
|
.stack
|
2022-11-25 02:46:13 +01:00
|
|
|
.get_or_insert_with(Default::default)
|
2022-11-24 15:25:19 +01:00
|
|
|
.push(name.clone(), ());
|
2022-03-05 10:57:23 +01:00
|
|
|
Ident { name, pos }
|
2022-02-28 07:37:46 +01:00
|
|
|
} else {
|
2022-08-13 12:07:42 +02:00
|
|
|
Ident {
|
|
|
|
name: state.get_interned_string(""),
|
|
|
|
pos: Position::NONE,
|
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
2020-10-20 17:16:03 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// try { try_block } catch ( var ) { catch_block }
|
2022-11-24 10:08:43 +01:00
|
|
|
let catch_block = self.parse_block(input, state, lib, settings.level_up()?)?;
|
2021-09-03 04:05:58 +02:00
|
|
|
|
2022-03-05 10:57:23 +01:00
|
|
|
if !catch_var.is_empty() {
|
2022-02-28 07:37:46 +01:00
|
|
|
// Remove the error variable from the stack
|
2022-11-25 13:42:16 +01:00
|
|
|
state.stack.as_deref_mut().unwrap().pop();
|
2022-01-28 03:11:40 +01:00
|
|
|
}
|
2020-10-20 17:16:03 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(Stmt::TryCatch(
|
|
|
|
TryCatchBlock {
|
|
|
|
try_block: try_block.into(),
|
|
|
|
catch_var,
|
|
|
|
catch_block: catch_block.into(),
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
settings.pos,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a function definition.
|
2021-04-09 16:49:47 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2022-02-28 07:37:46 +01:00
|
|
|
fn parse_fn(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
|
|
|
lib: &mut FnLib,
|
|
|
|
access: crate::FnAccess,
|
|
|
|
settings: ParseSettings,
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[cfg(feature = "metadata")]
|
|
|
|
comments: StaticVec<SmartString>,
|
|
|
|
) -> ParseResult<ScriptFnDef> {
|
2022-11-30 07:11:09 +01:00
|
|
|
let settings = settings;
|
2021-11-16 16:13:53 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let (token, pos) = input.next().expect(NEVER_ENDS);
|
2020-07-26 16:25:30 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let name = match token.into_function_name_for_override() {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(Token::Reserved(s)) => return Err(PERR::Reserved(s.to_string()).into_err(pos)),
|
|
|
|
Err(_) => return Err(PERR::FnMissingName.into_err(pos)),
|
|
|
|
};
|
2016-03-01 21:16:10 +01:00
|
|
|
|
2022-04-21 04:04:46 +02:00
|
|
|
let no_params = match input.peek().expect(NEVER_ENDS) {
|
|
|
|
(Token::LeftParen, ..) => {
|
|
|
|
eat_token(input, Token::LeftParen);
|
|
|
|
match_token(input, Token::RightParen).0
|
|
|
|
}
|
|
|
|
(Token::Unit, ..) => {
|
|
|
|
eat_token(input, Token::Unit);
|
|
|
|
true
|
|
|
|
}
|
2022-11-22 16:30:43 +01:00
|
|
|
(.., pos) => return Err(PERR::FnMissingParams(name.into()).into_err(*pos)),
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
2016-03-01 21:16:10 +01:00
|
|
|
|
2022-08-13 12:07:42 +02:00
|
|
|
let mut params = StaticVec::<(ImmutableString, _)>::new_const();
|
2016-03-01 21:16:10 +01:00
|
|
|
|
2022-04-21 04:04:46 +02:00
|
|
|
if !no_params {
|
2022-08-11 13:01:23 +02:00
|
|
|
let sep_err = format!("to separate the parameters of function '{name}'");
|
2020-03-24 09:46:47 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
loop {
|
|
|
|
match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::RightParen, ..) => break,
|
|
|
|
(Token::Identifier(s), pos) => {
|
2022-11-23 11:02:10 +01:00
|
|
|
if params.iter().any(|(p, _)| p.as_str() == *s) {
|
2022-11-22 16:30:43 +01:00
|
|
|
return Err(
|
|
|
|
PERR::FnDuplicatedParam(name.into(), s.to_string()).into_err(pos)
|
|
|
|
);
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2022-11-24 10:08:43 +01:00
|
|
|
|
2022-09-25 17:03:18 +02:00
|
|
|
let s = state.get_interned_string(*s);
|
2022-11-24 15:25:19 +01:00
|
|
|
state
|
|
|
|
.stack
|
2022-11-25 02:46:13 +01:00
|
|
|
.get_or_insert_with(Default::default)
|
2022-11-24 15:25:19 +01:00
|
|
|
.push(s.clone(), ());
|
2022-07-27 12:04:59 +02:00
|
|
|
params.push((s, pos));
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
|
|
|
(.., pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::RightParen.into(),
|
2022-08-11 13:01:23 +02:00
|
|
|
format!("to close the parameters list of function '{name}'"),
|
2022-02-28 07:37:46 +01:00
|
|
|
)
|
|
|
|
.into_err(pos))
|
2020-11-13 11:32:18 +01:00
|
|
|
}
|
2020-07-19 11:14:55 +02:00
|
|
|
}
|
2020-03-16 16:51:32 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::RightParen, ..) => break,
|
|
|
|
(Token::Comma, ..) => (),
|
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
|
|
|
(.., pos) => {
|
|
|
|
return Err(PERR::MissingToken(Token::Comma.into(), sep_err).into_err(pos))
|
|
|
|
}
|
2020-05-04 13:36:58 +02:00
|
|
|
}
|
2016-03-01 21:16:10 +01:00
|
|
|
}
|
2020-03-14 16:41:15 +01:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Parse function body
|
|
|
|
let body = match input.peek().expect(NEVER_ENDS) {
|
2022-11-30 07:11:09 +01:00
|
|
|
(Token::LeftBrace, ..) => self.parse_block(input, state, lib, settings.level_up()?)?,
|
2022-11-22 16:30:43 +01:00
|
|
|
(.., pos) => return Err(PERR::FnMissingBody(name.into()).into_err(*pos)),
|
2020-06-11 16:03:47 +02:00
|
|
|
}
|
2022-02-28 07:37:46 +01:00
|
|
|
.into();
|
2016-03-01 21:16:10 +01:00
|
|
|
|
2022-11-28 16:24:22 +01:00
|
|
|
let mut params: crate::FnArgsVec<_> = params.into_iter().map(|(p, ..)| p).collect();
|
2022-02-28 07:37:46 +01:00
|
|
|
params.shrink_to_fit();
|
2016-03-01 21:16:10 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok(ScriptFnDef {
|
2022-08-13 12:07:42 +02:00
|
|
|
name: state.get_interned_string(name),
|
2022-02-28 07:37:46 +01:00
|
|
|
access,
|
|
|
|
params,
|
|
|
|
body,
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
environ: None,
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[cfg(feature = "metadata")]
|
2022-03-06 09:37:27 +01:00
|
|
|
comments: comments
|
|
|
|
.into_iter()
|
|
|
|
.map(|s| s.to_string().into_boxed_str())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.into_boxed_slice(),
|
2022-02-28 07:37:46 +01:00
|
|
|
})
|
2020-07-29 16:43:50 +02:00
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Creates a curried expression from a list of external variables
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
|
|
|
fn make_curry_from_externals(
|
|
|
|
state: &mut ParseState,
|
2022-10-24 17:33:24 +02:00
|
|
|
parent: &mut ParseState,
|
|
|
|
lib: &FnLib,
|
2022-02-28 07:37:46 +01:00
|
|
|
fn_expr: Expr,
|
2022-11-28 16:24:22 +01:00
|
|
|
externals: crate::FnArgsVec<Ident>,
|
2022-02-28 07:37:46 +01:00
|
|
|
pos: Position,
|
|
|
|
) -> Expr {
|
|
|
|
// If there are no captured variables, no need to curry
|
|
|
|
if externals.is_empty() {
|
|
|
|
return fn_expr;
|
|
|
|
}
|
2020-07-29 16:43:50 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let num_externals = externals.len();
|
|
|
|
let mut args = StaticVec::with_capacity(externals.len() + 1);
|
2020-08-23 11:46:39 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
args.push(fn_expr);
|
|
|
|
|
2022-11-23 09:14:11 +01:00
|
|
|
args.extend(externals.iter().cloned().map(|Ident { name, pos }| {
|
|
|
|
let (index, is_func) = parent.access_var(&name, lib, pos);
|
|
|
|
let idx = match index {
|
2022-11-23 11:02:10 +01:00
|
|
|
#[allow(clippy::cast_possible_truncation)]
|
2022-11-23 09:14:11 +01:00
|
|
|
Some(n) if !is_func && n.get() <= u8::MAX as usize => NonZeroU8::new(n.get() as u8),
|
|
|
|
_ => None,
|
|
|
|
};
|
2022-11-23 11:02:10 +01:00
|
|
|
Expr::Variable((index, Default::default(), 0, name).into(), idx, pos)
|
2022-11-23 09:14:11 +01:00
|
|
|
}));
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
let expr = FnCallExpr {
|
2022-11-28 16:24:22 +01:00
|
|
|
namespace: Namespace::NONE,
|
2022-08-13 12:07:42 +02:00
|
|
|
name: state.get_interned_string(crate::engine::KEYWORD_FN_PTR_CURRY),
|
2022-02-28 07:37:46 +01:00
|
|
|
hashes: FnCallHashes::from_native(calc_fn_hash(
|
2022-09-21 05:46:23 +02:00
|
|
|
None,
|
2022-02-28 07:37:46 +01:00
|
|
|
crate::engine::KEYWORD_FN_PTR_CURRY,
|
|
|
|
num_externals + 1,
|
|
|
|
)),
|
|
|
|
args,
|
2022-11-28 16:24:22 +01:00
|
|
|
op_token: NO_TOKEN,
|
2022-10-14 12:31:40 +02:00
|
|
|
capture_parent_scope: false,
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
.into_fn_call_expr(pos);
|
|
|
|
|
|
|
|
// Convert the entire expression into a statement block, then insert the relevant
|
|
|
|
// [`Share`][Stmt::Share] statements.
|
2022-10-29 09:17:12 +02:00
|
|
|
let mut statements = StaticVec::with_capacity(2);
|
|
|
|
statements.push(Stmt::Share(
|
2022-02-28 07:37:46 +01:00
|
|
|
externals
|
|
|
|
.into_iter()
|
2022-11-23 09:14:11 +01:00
|
|
|
.map(|Ident { name, pos }| {
|
2022-10-25 04:05:31 +02:00
|
|
|
let (index, _) = parent.access_var(&name, lib, pos);
|
2022-10-29 09:17:12 +02:00
|
|
|
(name, index, pos)
|
|
|
|
})
|
2022-11-28 16:24:22 +01:00
|
|
|
.collect::<crate::FnArgsVec<_>>()
|
2022-10-29 09:17:12 +02:00
|
|
|
.into(),
|
|
|
|
));
|
2022-02-28 07:37:46 +01:00
|
|
|
statements.push(Stmt::Expr(expr.into()));
|
2022-11-23 09:14:11 +01:00
|
|
|
Expr::Stmt(StmtBlock::new(statements, pos, Position::NONE).into())
|
2021-06-16 12:36:33 +02:00
|
|
|
}
|
2020-07-29 16:43:50 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
/// Parse an anonymous function definition.
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
fn parse_anon_fn(
|
|
|
|
&self,
|
|
|
|
input: &mut TokenStream,
|
|
|
|
state: &mut ParseState,
|
2022-11-24 04:00:47 +01:00
|
|
|
_parent: &mut ParseState,
|
2022-02-28 07:37:46 +01:00
|
|
|
lib: &mut FnLib,
|
|
|
|
settings: ParseSettings,
|
|
|
|
) -> ParseResult<(Expr, ScriptFnDef)> {
|
2022-11-30 07:11:09 +01:00
|
|
|
let settings = settings;
|
2022-08-13 12:07:42 +02:00
|
|
|
let mut params_list = StaticVec::<ImmutableString>::new_const();
|
2020-07-19 11:14:55 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
if input.next().expect(NEVER_ENDS).0 != Token::Or && !match_token(input, Token::Pipe).0 {
|
|
|
|
loop {
|
|
|
|
match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::Pipe, ..) => break,
|
|
|
|
(Token::Identifier(s), pos) => {
|
2022-11-23 09:14:11 +01:00
|
|
|
if params_list.iter().any(|p| p.as_str() == *s) {
|
2022-08-21 11:35:44 +02:00
|
|
|
return Err(
|
|
|
|
PERR::FnDuplicatedParam(String::new(), s.to_string()).into_err(pos)
|
|
|
|
);
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
2022-11-24 10:08:43 +01:00
|
|
|
|
2022-09-25 17:03:18 +02:00
|
|
|
let s = state.get_interned_string(*s);
|
2022-11-24 15:25:19 +01:00
|
|
|
state
|
|
|
|
.stack
|
2022-11-25 02:46:13 +01:00
|
|
|
.get_or_insert_with(Default::default)
|
2022-11-24 15:25:19 +01:00
|
|
|
.push(s.clone(), ());
|
2022-07-27 12:04:59 +02:00
|
|
|
params_list.push(s);
|
2022-02-28 07:37:46 +01:00
|
|
|
}
|
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
|
|
|
(.., pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::Pipe.into(),
|
|
|
|
"to close the parameters list of anonymous function".into(),
|
|
|
|
)
|
|
|
|
.into_err(pos))
|
2020-07-19 11:14:55 +02:00
|
|
|
}
|
2021-07-24 08:11:16 +02:00
|
|
|
}
|
2020-07-19 11:14:55 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
match input.next().expect(NEVER_ENDS) {
|
|
|
|
(Token::Pipe, ..) => break,
|
|
|
|
(Token::Comma, ..) => (),
|
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
|
|
|
(.., pos) => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::Comma.into(),
|
|
|
|
"to separate the parameters of anonymous function".into(),
|
|
|
|
)
|
|
|
|
.into_err(pos))
|
|
|
|
}
|
2020-07-19 11:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// Parse function body
|
2022-11-24 10:08:43 +01:00
|
|
|
let body = self.parse_stmt(input, state, lib, settings.level_up()?)?;
|
2020-07-30 07:28:06 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
// External variables may need to be processed in a consistent order,
|
|
|
|
// so extract them into a list.
|
|
|
|
#[cfg(not(feature = "no_closure"))]
|
2022-11-24 10:08:43 +01:00
|
|
|
let (mut params, externals) = if let Some(ref external_vars) = state.external_vars {
|
2022-11-28 16:24:22 +01:00
|
|
|
let externals: crate::FnArgsVec<_> = external_vars.iter().cloned().collect();
|
2022-02-28 07:37:46 +01:00
|
|
|
|
2022-11-28 16:24:22 +01:00
|
|
|
let mut params = crate::FnArgsVec::with_capacity(params_list.len() + externals.len());
|
2022-11-23 09:14:11 +01:00
|
|
|
params.extend(externals.iter().map(|Ident { name, .. }| name.clone()));
|
2022-01-31 06:38:27 +01:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
(params, externals)
|
2022-11-24 10:08:43 +01:00
|
|
|
} else {
|
|
|
|
(
|
2022-11-28 16:24:22 +01:00
|
|
|
crate::FnArgsVec::with_capacity(params_list.len()),
|
|
|
|
crate::FnArgsVec::new_const(),
|
2022-11-24 10:08:43 +01:00
|
|
|
)
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
|
|
|
#[cfg(feature = "no_closure")]
|
2022-11-28 16:24:22 +01:00
|
|
|
let mut params = crate::FnArgsVec::with_capacity(params_list.len());
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
params.append(&mut params_list);
|
|
|
|
|
|
|
|
// Create unique function name by hashing the script body plus the parameters.
|
|
|
|
let hasher = &mut get_hasher();
|
|
|
|
params.iter().for_each(|p| p.hash(hasher));
|
|
|
|
body.hash(hasher);
|
|
|
|
let hash = hasher.finish();
|
2022-08-13 12:07:42 +02:00
|
|
|
let fn_name = state.get_interned_string(make_anonymous_fn(hash));
|
2022-02-28 07:37:46 +01:00
|
|
|
|
|
|
|
// Define the function
|
|
|
|
let script = ScriptFnDef {
|
|
|
|
name: fn_name.clone(),
|
|
|
|
access: crate::FnAccess::Public,
|
|
|
|
params,
|
|
|
|
body: body.into(),
|
|
|
|
#[cfg(not(feature = "no_module"))]
|
|
|
|
environ: None,
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
#[cfg(feature = "metadata")]
|
2022-03-06 09:37:27 +01:00
|
|
|
comments: Box::default(),
|
2022-02-28 07:37:46 +01:00
|
|
|
};
|
2020-07-19 11:14:55 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
let fn_ptr = crate::FnPtr::new_unchecked(fn_name, StaticVec::new_const());
|
|
|
|
let expr = Expr::DynamicConstant(Box::new(fn_ptr.into()), settings.pos);
|
2020-07-19 11:14:55 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
#[cfg(not(feature = "no_closure"))]
|
2022-10-24 17:33:24 +02:00
|
|
|
let expr =
|
2022-11-24 04:00:47 +01:00
|
|
|
Self::make_curry_from_externals(state, _parent, lib, expr, externals, settings.pos);
|
2020-07-29 16:43:50 +02:00
|
|
|
|
2022-02-28 07:37:46 +01:00
|
|
|
Ok((expr, script))
|
|
|
|
}
|
2020-07-19 11:14:55 +02:00
|
|
|
|
2021-06-12 16:47:43 +02:00
|
|
|
/// Parse a global level expression.
|
2020-06-11 12:13:33 +02:00
|
|
|
pub(crate) fn parse_global_expr(
|
2020-06-03 04:44:36 +02:00
|
|
|
&self,
|
2022-11-25 13:42:16 +01:00
|
|
|
mut input: TokenStream,
|
2021-04-04 07:13:07 +02:00
|
|
|
state: &mut ParseState,
|
2022-11-21 16:42:29 +01:00
|
|
|
process_settings: impl FnOnce(&mut ParseSettings),
|
2022-04-11 10:29:16 +02:00
|
|
|
_optimization_level: OptimizationLevel,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> ParseResult<AST> {
|
2022-09-05 06:13:06 +02:00
|
|
|
let mut functions = StraightHashMap::default();
|
2020-07-26 09:53:22 +02:00
|
|
|
|
2022-11-24 04:00:47 +01:00
|
|
|
let options = self.options & !LangOptions::STMT_EXPR & !LangOptions::LOOP_EXPR;
|
2022-05-19 15:40:22 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2022-11-24 04:00:47 +01:00
|
|
|
let options = options & !LangOptions::ANON_FN;
|
2022-05-19 15:40:22 +02:00
|
|
|
|
2022-09-29 16:46:59 +02:00
|
|
|
let mut settings = ParseSettings {
|
2020-06-11 16:03:47 +02:00
|
|
|
level: 0,
|
2022-11-23 04:36:30 +01:00
|
|
|
flags: ParseSettingFlags::GLOBAL_LEVEL
|
|
|
|
| ParseSettingFlags::DISALLOW_STATEMENTS_IN_BLOCKS,
|
2022-05-19 15:40:22 +02:00
|
|
|
options,
|
2022-09-29 16:46:59 +02:00
|
|
|
pos: Position::START,
|
2022-11-24 10:08:43 +01:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
max_expr_depth: self.max_expr_depth(),
|
2020-06-11 16:03:47 +02:00
|
|
|
};
|
2022-09-29 16:46:59 +02:00
|
|
|
process_settings(&mut settings);
|
|
|
|
|
2022-11-25 13:42:16 +01:00
|
|
|
let expr = self.parse_expr(&mut input, state, &mut functions, settings)?;
|
2020-06-03 04:44:36 +02:00
|
|
|
|
2020-08-22 05:08:27 +02:00
|
|
|
assert!(functions.is_empty());
|
|
|
|
|
2021-05-22 13:14:24 +02:00
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
2022-02-08 02:02:15 +01:00
|
|
|
(Token::EOF, ..) => (),
|
2020-06-03 04:44:36 +02:00
|
|
|
// Return error if the expression doesn't end
|
2022-11-22 16:30:43 +01:00
|
|
|
(token, pos) => return Err(LexError::UnexpectedInput(token.to_string()).into_err(*pos)),
|
2020-06-03 04:44:36 +02:00
|
|
|
}
|
|
|
|
|
2022-02-16 10:51:14 +01:00
|
|
|
let mut statements = StmtBlockContainer::new_const();
|
|
|
|
statements.push(Stmt::Expr(expr.into()));
|
2020-06-03 04:44:36 +02:00
|
|
|
|
2021-10-21 13:17:34 +02:00
|
|
|
#[cfg(not(feature = "no_optimize"))]
|
2022-11-25 13:42:16 +01:00
|
|
|
return Ok(self.optimize_into_ast(
|
2022-04-21 07:21:53 +02:00
|
|
|
state.scope,
|
2021-10-21 13:17:34 +02:00
|
|
|
statements,
|
2021-11-29 02:43:35 +01:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2021-11-25 10:09:00 +01:00
|
|
|
StaticVec::new_const(),
|
2022-04-11 10:29:16 +02:00
|
|
|
_optimization_level,
|
2021-10-21 13:17:34 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
#[cfg(feature = "no_optimize")]
|
2021-11-29 03:17:04 +01:00
|
|
|
return Ok(AST::new(
|
|
|
|
statements,
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
crate::Module::new(),
|
|
|
|
));
|
2020-06-03 04:44:36 +02:00
|
|
|
}
|
|
|
|
|
2020-06-14 08:25:47 +02:00
|
|
|
/// Parse the global level statements.
|
|
|
|
fn parse_global_level(
|
|
|
|
&self,
|
2022-11-25 13:42:16 +01:00
|
|
|
mut input: TokenStream,
|
2021-04-04 07:13:07 +02:00
|
|
|
state: &mut ParseState,
|
2022-11-21 16:42:29 +01:00
|
|
|
process_settings: impl FnOnce(&mut ParseSettings),
|
2022-02-16 05:57:26 +01:00
|
|
|
) -> ParseResult<(StmtBlockContainer, StaticVec<Shared<ScriptFnDef>>)> {
|
2022-02-16 10:51:14 +01:00
|
|
|
let mut statements = StmtBlockContainer::new_const();
|
2022-09-05 06:13:06 +02:00
|
|
|
let mut functions = StraightHashMap::default();
|
2022-11-23 04:36:30 +01:00
|
|
|
|
2022-09-29 16:46:59 +02:00
|
|
|
let mut settings = ParseSettings {
|
|
|
|
level: 0,
|
2022-11-23 04:36:30 +01:00
|
|
|
flags: ParseSettingFlags::GLOBAL_LEVEL,
|
|
|
|
options: self.options,
|
2022-09-29 16:46:59 +02:00
|
|
|
pos: Position::START,
|
2022-11-24 10:08:43 +01:00
|
|
|
#[cfg(not(feature = "unchecked"))]
|
|
|
|
max_expr_depth: self.max_expr_depth(),
|
2022-09-29 16:46:59 +02:00
|
|
|
};
|
|
|
|
process_settings(&mut settings);
|
2020-06-14 08:25:47 +02:00
|
|
|
|
2022-11-25 13:42:16 +01:00
|
|
|
while input.peek().expect(NEVER_ENDS).0 != Token::EOF {
|
|
|
|
let stmt = self.parse_stmt(&mut input, state, &mut functions, settings)?;
|
2020-12-29 03:41:20 +01:00
|
|
|
|
|
|
|
if stmt.is_noop() {
|
|
|
|
continue;
|
|
|
|
}
|
2020-06-14 08:25:47 +02:00
|
|
|
|
|
|
|
let need_semicolon = !stmt.is_self_terminated();
|
|
|
|
|
|
|
|
statements.push(stmt);
|
|
|
|
|
2021-05-22 13:14:24 +02:00
|
|
|
match input.peek().expect(NEVER_ENDS) {
|
2020-06-14 08:25:47 +02:00
|
|
|
// EOF
|
2022-02-08 02:02:15 +01:00
|
|
|
(Token::EOF, ..) => break,
|
2020-06-14 08:25:47 +02:00
|
|
|
// stmt ;
|
2022-02-08 02:02:15 +01:00
|
|
|
(Token::SemiColon, ..) if need_semicolon => {
|
2022-11-25 13:42:16 +01:00
|
|
|
eat_token(&mut input, Token::SemiColon);
|
2020-06-14 08:25:47 +02:00
|
|
|
}
|
|
|
|
// stmt ;
|
2022-02-08 02:02:15 +01:00
|
|
|
(Token::SemiColon, ..) if !need_semicolon => (),
|
2020-06-14 08:25:47 +02:00
|
|
|
// { stmt } ???
|
2022-02-08 02:46:14 +01:00
|
|
|
_ if !need_semicolon => (),
|
2020-06-14 08:25:47 +02:00
|
|
|
// stmt <error>
|
2020-11-25 02:36:06 +01:00
|
|
|
(Token::LexError(err), pos) => return Err(err.clone().into_err(*pos)),
|
2020-06-14 08:25:47 +02:00
|
|
|
// stmt ???
|
2022-02-08 02:02:15 +01:00
|
|
|
(.., pos) => {
|
2020-06-14 08:25:47 +02:00
|
|
|
// Semicolons are not optional between statements
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::SemiColon.into(),
|
|
|
|
"to terminate this statement".into(),
|
|
|
|
)
|
|
|
|
.into_err(*pos));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 02:02:15 +01:00
|
|
|
Ok((statements, functions.into_iter().map(|(.., v)| v).collect()))
|
2020-06-14 08:25:47 +02:00
|
|
|
}
|
|
|
|
|
2020-06-03 04:44:36 +02:00
|
|
|
/// Run the parser on an input stream, returning an AST.
|
2021-10-21 11:26:43 +02:00
|
|
|
#[inline]
|
2020-06-11 12:13:33 +02:00
|
|
|
pub(crate) fn parse(
|
2020-06-03 04:44:36 +02:00
|
|
|
&self,
|
2022-11-25 13:42:16 +01:00
|
|
|
input: TokenStream,
|
2021-04-04 07:13:07 +02:00
|
|
|
state: &mut ParseState,
|
2022-04-11 10:29:16 +02:00
|
|
|
_optimization_level: OptimizationLevel,
|
2021-12-25 16:49:14 +01:00
|
|
|
) -> ParseResult<AST> {
|
2022-09-29 16:46:59 +02:00
|
|
|
let (statements, _lib) = self.parse_global_level(input, state, |_| {})?;
|
2020-06-03 04:44:36 +02:00
|
|
|
|
2021-10-21 13:17:34 +02:00
|
|
|
#[cfg(not(feature = "no_optimize"))]
|
2022-11-25 13:42:16 +01:00
|
|
|
return Ok(self.optimize_into_ast(
|
2022-04-21 07:21:53 +02:00
|
|
|
state.scope,
|
2021-10-21 13:17:34 +02:00
|
|
|
statements,
|
2021-11-29 02:43:35 +01:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2021-10-21 13:30:58 +02:00
|
|
|
_lib,
|
2022-04-11 10:29:16 +02:00
|
|
|
_optimization_level,
|
2021-10-21 13:17:34 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
#[cfg(feature = "no_optimize")]
|
2021-10-21 13:30:58 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2021-10-21 13:17:34 +02:00
|
|
|
{
|
|
|
|
let mut m = crate::Module::new();
|
|
|
|
|
2022-01-28 11:59:18 +01:00
|
|
|
for fn_def in _lib {
|
2021-10-21 13:17:34 +02:00
|
|
|
m.set_script_fn(fn_def);
|
2022-01-28 11:59:18 +01:00
|
|
|
}
|
2021-10-21 13:17:34 +02:00
|
|
|
|
|
|
|
return Ok(AST::new(statements, m));
|
|
|
|
}
|
2021-10-21 13:30:58 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "no_optimize")]
|
|
|
|
#[cfg(feature = "no_function")]
|
2021-11-29 03:17:04 +01:00
|
|
|
return Ok(AST::new(
|
|
|
|
statements,
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
crate::Module::new(),
|
|
|
|
));
|
2020-06-03 04:44:36 +02:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|