rhai/src/parser.rs

3962 lines
148 KiB
Rust
Raw Normal View History

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,
OpAssignment, RangeCase, ScriptFnDef, Stmt, StmtBlock, StmtBlockContainer,
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-04-16 17:32:14 +02:00
use crate::eval::GlobalRuntimeState;
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,
2021-08-30 09:42:47 +02:00
TokenizerControl,
};
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-02-13 11:46:25 +01:00
calc_fn_hash, Dynamic, Engine, EvalAltResult, EvalContext, ExclusiveRange, Identifier,
ImmutableString, InclusiveRange, LexError, OptimizationLevel, ParseError, Position, Scope,
2022-02-26 10:28:58 +01:00
Shared, SmartString, StaticVec, AST, INT, PERR,
2020-11-16 16:10:14 +01:00
};
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>>;
2022-08-27 10:26:41 +02:00
const KEYWORD_SEMICOLON: &str = Token::SemiColon.literal_syntax();
const KEYWORD_CLOSE_BRACE: &str = Token::RightBrace.literal_syntax();
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.
2021-04-04 07:13:07 +02:00
pub struct ParseState<'e> {
/// 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-08-13 12:07:42 +02:00
/// String interners.
2022-09-27 02:52:51 +02:00
interned_strings: StringsInterner<'e>,
/// External [scope][Scope] with constants.
pub scope: &'e Scope<'e>,
2022-05-01 18:03:45 +02:00
/// Global runtime state.
pub global: GlobalRuntimeState<'e>,
/// Encapsulates a local stack with variable names to simulate an actual runtime scope.
2022-02-13 11:46:25 +01:00
pub stack: Scope<'e>,
/// 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"))]
pub external_vars: Vec<crate::ast::Ident>,
/// 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.
#[cfg(not(feature = "no_module"))]
2022-08-13 12:07:42 +02:00
pub imports: StaticVec<ImmutableString>,
2022-08-09 15:35:45 +02:00
/// List of globally-imported [module][crate::Module] names.
#[cfg(not(feature = "no_module"))]
2022-08-13 12:07:42 +02:00
pub global_imports: StaticVec<ImmutableString>,
2022-02-28 09:32:08 +01:00
/// Maximum levels of expression nesting (0 for unlimited).
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
2022-02-28 09:32:08 +01:00
pub max_expr_depth: usize,
}
2020-04-28 17:05:03 +02:00
2022-07-04 11:42:24 +02: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);
#[cfg(not(feature = "no_closure"))]
f.field("external_vars", &self.external_vars)
.field("allow_capture", &self.allow_capture);
#[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-07-04 11:47:59 +02:00
#[cfg(not(feature = "unchecked"))]
f.field("max_expr_depth", &self.max_expr_depth);
f.finish()
2022-07-04 11:42:24 +02:00
}
}
2020-07-05 11:41:45 +02:00
impl<'e> ParseState<'e> {
2020-11-25 02:36:06 +01:00
/// Create a new [`ParseState`].
2020-10-08 16:25:50 +02:00
#[inline(always)]
2021-06-12 16:47:43 +02:00
#[must_use]
2022-08-12 10:34:57 +02:00
pub fn new(
engine: &Engine,
scope: &'e Scope,
2022-09-27 02:52:51 +02:00
interned_strings: StringsInterner<'e>,
2022-08-12 10:34:57 +02:00
tokenizer_control: TokenizerControl,
) -> Self {
Self {
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"))]
external_vars: Vec::new(),
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,
scope,
2022-05-01 18:03:45 +02:00
global: GlobalRuntimeState::new(engine),
2022-02-13 11:46:25 +01:00
stack: Scope::new(),
2022-02-18 08:04:46 +01:00
block_stack_len: 0,
#[cfg(not(feature = "no_module"))]
2022-01-24 10:04:40 +01:00
imports: StaticVec::new_const(),
2022-08-09 15:35:45 +02:00
#[cfg(not(feature = "no_module"))]
global_imports: StaticVec::new_const(),
2022-02-28 09:32:08 +01:00
#[cfg(not(feature = "unchecked"))]
max_expr_depth: engine.max_expr_depth(),
}
2020-04-28 17:05:03 +02:00
}
2020-11-25 02:36:06 +01:00
/// Find explicitly declared variable by name in the [`ParseState`], searching in reverse order.
///
/// 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]
pub fn find_var(&self, name: &str) -> (usize, bool) {
let mut hit_barrier = false;
(
self.stack
.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),
hit_barrier,
)
}
/// 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.
///
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.
///
2022-08-10 06:48:37 +02:00
/// # Return value: `(index, is_func_name)`
2022-08-05 17:30:44 +02:00
///
/// * `index`: `None` when the variable name is not found in the `stack`,
/// 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).
#[inline]
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;
let (index, hit_barrier) = self.find_var(name);
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-08-10 06:48:37 +02:00
if !is_func_name && index == 0 && !self.external_vars.iter().any(|v| v.as_str() == name)
{
2022-01-31 06:38:27 +01:00
self.external_vars.push(crate::ast::Ident {
name: name.into(),
pos: _pos,
});
}
} else {
2022-07-27 12:04:59 +02:00
self.allow_capture = true;
}
2022-08-05 17:30:44 +02:00
let index = if hit_barrier {
None
} else {
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-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
///
2020-11-25 02:36:06 +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"))]
#[inline]
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
2020-05-04 13:36:58 +02:00
.iter()
.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
}
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"))]
2021-03-24 06:17:52 +01:00
#[inline(always)]
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-08-11 16:56:23 +02:00
#[inline(always)]
#[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-04-28 17:05:03 +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)]
struct ParseSettings {
/// Is the construct being parsed located at global level?
2022-08-10 06:48:37 +02:00
at_global_level: bool,
/// Is the construct being parsed located inside a function definition?
2021-11-28 03:49:48 +01:00
#[cfg(not(feature = "no_function"))]
2022-08-10 06:48:37 +02:00
in_fn_scope: bool,
/// Is the construct being parsed located inside a closure definition?
2021-12-04 10:57:28 +01:00
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_closure"))]
2022-08-10 06:48:37 +02:00
in_closure: bool,
/// Is the construct being parsed located inside a breakable loop?
is_breakable: bool,
/// Allow statements in blocks?
allow_statements: bool,
2022-02-28 09:32:08 +01:00
/// Language options in effect (overrides Engine options).
2022-05-19 15:40:22 +02:00
options: LangOptions,
/// Current expression nesting level.
level: usize,
2022-02-28 09:32:08 +01:00
/// Current position.
pos: Position,
}
impl ParseSettings {
/// Create a new `ParseSettings` with one higher expression level.
2020-10-08 16:25:50 +02:00
#[inline(always)]
2021-06-12 16:47:43 +02:00
#[must_use]
2021-06-28 12:06:05 +02:00
pub const fn level_up(&self) -> Self {
Self {
level: self.level + 1,
..*self
}
}
/// Make sure that the current level of expression nesting is within the maximum limit.
2022-02-28 09:32:08 +01:00
///
/// If `limit` is zero, then checking is disabled.
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
#[inline]
2022-02-28 09:32:08 +01:00
pub fn ensure_level_within_max_limit(&self, limit: usize) -> ParseResult<()> {
2022-07-27 10:04:24 +02:00
if limit > 0 && self.level > limit {
return Err(PERR::ExprTooDeep.into_err(self.pos));
}
2021-01-06 06:46:53 +01:00
Ok(())
}
}
2022-01-12 01:12:28 +01:00
/// Make an anonymous function.
#[cfg(not(feature = "no_function"))]
#[inline]
#[must_use]
pub fn make_anonymous_fn(hash: u64) -> String {
format!("{}{:016x}", crate::engine::FN_ANONYMOUS, hash)
}
/// 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)
}
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.
#[cfg(not(feature = "no_object"))]
#[inline]
2021-06-12 16:47:43 +02:00
#[must_use]
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
Self::Property(
2022-08-13 12:07:42 +02:00
Box::new(((getter, hash_get), (setter, hash_set), ident)),
pos,
)
}
_ => 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-02-08 02:02:15 +01:00
Expr::DynamicConstant(ref v, ..) if !v.is::<bool>() => v.type_name(),
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(
PERR::MismatchedType("a boolean expression".to_string(), type_name.to_string())
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(
PERR::MismatchedType("an iterable value".to_string(), type_name.to_string())
2022-02-04 05:04:33 +01:00
.into_err(self.start_position()),
2021-07-03 18:15:27 +02:00
)
}
}
2021-07-04 10:40:15 +02:00
/// Make sure that the next expression is not a statement expression (i.e. wrapped in `{}`).
#[inline]
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`).
#[inline]
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(
"=".to_string(),
"Possibly a typo of '=='?".to_string(),
)
.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
///
/// Panics if the next token is not the expected one.
2021-07-04 10:40:15 +02:00
#[inline]
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);
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 {}",
2021-11-13 02:50:49 +01:00
expected_token.syntax(),
t.syntax(),
pos
);
}
pos
}
2020-11-25 02:36:06 +01:00
/// Match a particular [token][Token], consuming it if matched.
2021-07-04 10:40:15 +02:00
#[inline]
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);
if *t == token {
2020-10-20 17:16:03 +02:00
(true, eat_token(input, token))
} else {
2020-10-20 17:16:03 +02:00
(false, *pos)
}
}
2021-06-07 05:43:00 +02:00
/// Parse a variable name.
2022-05-19 08:41:48 +02:00
#[inline]
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
(Token::Identifier(s), pos) => Ok((*s, pos)),
2021-06-07 05:43:00 +02:00
// Reserved keyword
(Token::Reserved(s), pos) if is_valid_identifier(s.chars()) => {
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-05-19 08:41:48 +02:00
#[inline]
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
(Token::Reserved(s), pos) if !is_valid_identifier(s.chars()) => 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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2016-02-29 22:43:45 +01:00
2022-02-28 07:37:46 +01:00
// ( ...
let mut settings = settings;
settings.pos = eat_token(input, Token::LeftParen);
2022-02-28 07:37:46 +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-03-05 10:57:23 +01:00
#[cfg(not(feature = "no_module"))] namespace: crate::ast::Namespace,
2022-02-28 07:37:46 +01:00
settings: ParseSettings,
) -> ParseResult<Expr> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-05-09 10:15:50 +02:00
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)
};
2022-02-28 07:37:46 +01:00
#[cfg(not(feature = "no_module"))]
let mut namespace = namespace;
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-07-27 12:04:59 +02: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 {
let root = namespace.root();
let index = state.find_module(root);
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"))]
let is_global = root == crate::engine::KEYWORD_GLOBAL;
#[cfg(any(feature = "no_function", feature = "no_module"))]
let is_global = false;
2022-07-27 10:04:24 +02:00
if settings.options.contains(LangOptions::STRICT_VAR)
&& index.is_none()
&& !is_global
2022-08-13 12:07:42 +02:00
&& !state.global_imports.iter().any(|m| m.as_str() == root)
2022-07-27 10:04:24 +02:00
&& !self.global_sub_modules.contains_key(root)
{
return Err(
PERR::ModuleUndefined(root.to_string()).into_err(namespace.position())
);
2021-12-04 10:57:28 +01:00
}
2022-03-03 06:02:57 +01:00
namespace.set_index(index);
2022-01-29 04:09:43 +01:00
2022-09-21 05:46:23 +02:00
crate::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
2021-08-30 09:42:47 +02:00
let hashes = if is_valid_function_name(&id) {
hash.into()
} else {
2021-04-20 16:26:08 +02:00
FnCallHashes::from_native(hash)
};
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,
operator_token: None,
2022-01-29 04:09:43 +01:00
#[cfg(not(feature = "no_module"))]
2021-06-16 12:36:33 +02:00
namespace,
hashes,
args,
2022-02-04 05:04:33 +01:00
pos: settings.pos,
2021-06-16 12:36:33 +02:00
}
.into_fn_call_expr(settings.pos));
}
2022-02-28 07:37:46 +01:00
// id...
_ => (),
}
let settings = settings.level_up();
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-07-27 12:04:59 +02: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 {
let root = namespace.root();
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-07-27 10:04:24 +02:00
if settings.options.contains(LangOptions::STRICT_VAR)
&& index.is_none()
&& !is_global
2022-08-13 12:07:42 +02:00
&& !state.global_imports.iter().any(|m| m.as_str() == root)
2022-07-27 10:04:24 +02:00
&& !self.global_sub_modules.contains_key(root)
{
return Err(PERR::ModuleUndefined(root.to_string())
.into_err(namespace.position()));
2022-02-28 07:37:46 +01:00
}
2022-03-03 06:02:57 +01:00
namespace.set_index(index);
2022-02-28 07:37:46 +01:00
2022-09-21 05:46:23 +02:00
crate::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
let hashes = if is_valid_function_name(&id) {
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,
operator_token: None,
2022-02-28 07:37:46 +01:00
#[cfg(not(feature = "no_module"))]
namespace,
hashes,
args,
pos: settings.pos,
}
.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))
}
}
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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2022-02-28 07:37:46 +01:00
let mut settings = settings;
2021-11-16 16:13:53 +01:00
2022-02-28 07:37:46 +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-02-28 07:37:46 +01:00
// Recursively parse the indexing chain, right-binding each
let idx_expr = self.parse_index_chain(
input,
state,
lib,
idx_expr,
2022-06-11 18:32:12 +02:00
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-05-19 08:41:48 +02:00
false,
2022-02-28 07:37:46 +01:00
settings.level_up(),
)?;
// 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-09 03:41:17 +01: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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
2022-02-28 07:37:46 +01:00
// [ ...
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";
2022-02-28 07:37:46 +01:00
#[cfg(not(feature = "unchecked"))]
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(
"Size of array literal".to_string(),
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))
}
_ => {
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
}
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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
2022-02-28 07:37:46 +01:00
// #{ ...
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();
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
}
2022-02-28 07:37:46 +01:00
let (name, pos) = match input.next().expect(NEVER_ENDS) {
2022-04-21 04:04:46 +02:00
(Token::Identifier(s) | Token::StringConstant(s), pos) => {
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));
}
(*s, pos)
2022-02-28 07:37:46 +01:00
}
(Token::InterpolatedString(..), pos) => {
return Err(PERR::PropertyExpected.into_err(pos))
}
(Token::Reserved(s), pos) if is_valid_identifier(s.chars()) => {
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))
}
};
2022-02-28 07:37:46 +01:00
#[cfg(not(feature = "unchecked"))]
if self.max_map_size() > 0 && map.len() >= self.max_map_size() {
return Err(PERR::LiteralTooLarge(
"Number of properties in object map literal".to_string(),
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
let expr = self.parse_expr(input, state, lib, settings.level_up())?;
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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-11-13 11:32:18 +01:00
2022-02-28 07:37:46 +01:00
// switch ...
let mut settings = settings;
settings.pos = eat_token(input, Token::Switch);
2020-12-28 02:49:54 +01:00
2022-02-28 07:37:46 +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;
let expr = self.parse_expr(input, state, lib, settings.level_up());
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 {
ensure_not_statement_expr(input, "a boolean")?;
let guard = self
.parse_expr(input, state, lib, settings.level_up())?
.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(),
"in this switch case".to_string(),
)
.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
let (action_expr, need_comma) = if settings.allow_statements {
let stmt = self.parse_stmt(input, state, lib, settings.level_up())?;
let need_comma = !stmt.is_self_terminated();
let stmt_block: StmtBlock = stmt.into();
(Expr::Stmt(stmt_block.into()), need_comma)
} else {
(
self.parse_expr(input, state, lib, settings.level_up())?,
true,
)
};
2022-07-18 02:54:10 +02:00
let has_condition = !matches!(condition, Expr::BoolConstant(true, ..));
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(|| {
PERR::ExprExpected("a literal".to_string()).into_err(expr.start_position())
})?;
let mut range_value: Option<RangeCase> = None;
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;
}
2022-07-04 11:42:24 +02:00
if value.is::<INT>() && !ranges.is_empty() {
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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
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) => {
return Err(
LexError::UnexpectedInput(token.syntax().to_string()).into_err(settings.pos)
)
}
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) => {
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) => {
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-05-19 15:40:22 +02:00
Token::LeftBrace if settings.options.contains(LangOptions::STMT_EXPR) => {
2022-02-28 07:37:46 +01:00
match self.parse_block(input, state, lib, settings.level_up())? {
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
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-05-19 15:40:22 +02:00
Token::If if settings.options.contains(LangOptions::IF_EXPR) => Expr::Stmt(Box::new(
2022-02-28 07:37:46 +01:00
self.parse_if(input, state, lib, settings.level_up())?
.into(),
)),
// Switch statement is allowed to act as expressions
2022-05-19 15:40:22 +02:00
Token::Switch if settings.options.contains(LangOptions::SWITCH_EXPR) => {
Expr::Stmt(Box::new(
self.parse_switch(input, state, lib, settings.level_up())?
.into(),
))
}
2022-02-28 07:37:46 +01:00
// | ...
#[cfg(not(feature = "no_function"))]
2022-05-19 15:40:22 +02:00
Token::Pipe | Token::Or if settings.options.contains(LangOptions::ANON_FN) => {
2022-08-14 08:32:16 +02:00
// Build new parse state
let interned_strings = std::mem::take(&mut state.interned_strings);
2022-08-12 10:34:57 +02:00
let mut new_state = ParseState::new(
self,
state.scope,
2022-08-14 08:32:16 +02:00
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.
2022-08-09 15:35:45 +02:00
new_state.global_imports.clone_from(&state.global_imports);
new_state
.global_imports
.extend(state.imports.iter().cloned());
2022-08-09 15:35:45 +02:00
}
2020-12-27 09:50:48 +01:00
2022-02-28 07:37:46 +01:00
#[cfg(not(feature = "unchecked"))]
{
2022-02-28 09:32:08 +01:00
new_state.max_expr_depth = self.max_function_expr_depth();
2022-02-28 07:37:46 +01:00
}
2020-12-27 09:50:48 +01:00
2022-05-19 15:40:22 +02:00
let mut options = self.options;
options.set(
LangOptions::STRICT_VAR,
if cfg!(feature = "no_closure") {
settings.options.contains(LangOptions::STRICT_VAR)
} else {
// A capturing closure can access variables not defined locally
false
},
);
2022-02-28 07:37:46 +01:00
let new_settings = ParseSettings {
2022-08-10 06:48:37 +02:00
at_global_level: false,
in_fn_scope: true,
2022-02-28 07:37:46 +01:00
#[cfg(not(feature = "no_closure"))]
2022-08-10 06:48:37 +02:00
in_closure: true,
2022-02-28 07:37:46 +01:00
is_breakable: false,
level: 0,
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-08-12 10:34:57 +02:00
let result = self.parse_anon_fn(input, &mut new_state, lib, new_settings);
2022-08-14 08:32:16 +02:00
// Restore parse state
2022-08-12 10:34:57 +02:00
state.interned_strings = new_state.interned_strings;
let (expr, func) = result?;
2020-12-27 09:50:48 +01:00
2022-02-28 07:37:46 +01:00
#[cfg(not(feature = "no_closure"))]
new_state.external_vars.iter().try_for_each(
|crate::ast::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-08-29 16:26:07 +02:00
&& !settings.in_closure
&& settings.options.contains(LangOptions::STRICT_VAR)
&& !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 {
Ok::<_, ParseError>(())
}
},
)?;
2020-12-27 09:50:48 +01:00
2022-09-21 05:46:23 +02:00
let hash_script = calc_fn_hash(None, &func.name, func.params.len());
2022-02-28 07:37:46 +01:00
lib.insert(hash_script, func.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) => {
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 {
let expr = match self.parse_block(input, state, lib, settings.level_up())? {
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() {
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() {
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 => {
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"))]
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-03-03 06:02:57 +01:00
if !self.custom_syntax.is_empty() && self.custom_syntax.contains_key(&**key) =>
2022-02-28 07:37:46 +01:00
{
let (key, syntax) = self.custom_syntax.get_key_value(&**key).unwrap();
let (.., pos) = input.next().expect(NEVER_ENDS);
let settings2 = settings.level_up();
self.parse_custom_syntax(input, state, lib, settings2, key, syntax, pos)?
}
2020-12-27 09:50:48 +01:00
2022-02-28 07:37:46 +01:00
// Identifier
Token::Identifier(..) => {
#[cfg(not(feature = "no_module"))]
2022-03-05 10:57:23 +01:00
let ns = crate::ast::Namespace::NONE;
2022-02-28 07:37:46 +01:00
#[cfg(feature = "no_module")]
2022-03-05 10:57:23 +01:00
let ns = ();
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(
(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;
}
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
&& index.is_none()
2022-08-29 16:26:07 +02:00
&& settings.options.contains(LangOptions::STRICT_VAR)
&& !state.scope.contains(&s)
{
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
}
});
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(..) => {
#[cfg(not(feature = "no_module"))]
2022-03-05 10:57:23 +01:00
let ns = crate::ast::Namespace::NONE;
2022-02-28 07:37:46 +01:00
#[cfg(feature = "no_module")]
2022-03-05 10:57:23 +01:00
let ns = ();
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(
(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-08-10 06:48:37 +02:00
_ if &*s == KEYWORD_THIS && settings.in_fn_scope => Expr::Variable(
(None, ns, 0, state.get_interned_string(*s)).into(),
2021-04-05 17:59:15 +02:00
None,
settings.pos,
2022-02-28 07:37:46 +01:00
),
// Cannot access to `this` as a variable not in a function scope
_ 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-02-28 07:37:46 +01:00
_ => {
return Err(
LexError::UnexpectedInput(token.syntax().to_string()).into_err(settings.pos)
)
2020-07-16 06:09:31 +02:00
}
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, ..) => {
Err(LexError::UnexpectedInput(Token::Bang.syntax().to_string())
.into_err(tail_pos))
}
_ => Err(LexError::ImproperSymbol(
2022-02-28 07:37:46 +01:00
"!".to_string(),
"'!' cannot be used to call module functions".to_string(),
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(
Token::LeftParen.syntax().into(),
"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-03-05 10:57:23 +01:00
let (.., _ns, _, name) = *x;
2022-02-28 07:37:46 +01:00
settings.pos = pos;
self.parse_fn_call(
input,
state,
lib,
name,
2022-04-21 04:04:46 +02:00
no_args,
2022-02-28 07:37:46 +01:00
true,
#[cfg(not(feature = "no_module"))]
_ns,
settings.level_up(),
)?
}
// Function call
2022-04-21 04:04:46 +02:00
(Expr::Variable(x, .., pos), t @ (Token::LeftParen | Token::Unit)) => {
2022-03-05 10:57:23 +01:00
let (.., _ns, _, name) = *x;
2022-02-28 07:37:46 +01:00
settings.pos = pos;
self.parse_fn_call(
input,
state,
lib,
name,
2022-04-21 04:04:46 +02:00
t == Token::Unit,
2022-02-28 07:37:46 +01:00
false,
#[cfg(not(feature = "no_module"))]
_ns,
settings.level_up(),
)?
}
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
};
self.parse_index_chain(input, state, lib, expr, opt, true, settings.level_up())?
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-08-29 16:26:07 +02: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
(expr, token) => unreachable!(
"unknown postfix operator '{}' for {:?}",
token.syntax(),
expr
),
2020-12-21 10:39:37 +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"))]
{
let root = namespace.root();
let index = state.find_module(root);
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"))]
let is_global = root == crate::engine::KEYWORD_GLOBAL;
#[cfg(any(feature = "no_function", feature = "no_module"))]
let is_global = false;
2022-07-27 10:04:24 +02:00
if settings.options.contains(LangOptions::STRICT_VAR)
&& index.is_none()
&& !is_global
2022-08-13 12:07:42 +02:00
&& !state.global_imports.iter().any(|m| m.as_str() == root)
2022-07-27 10:04:24 +02:00
&& !self.global_sub_modules.contains_key(root)
{
return Err(
PERR::ModuleUndefined(root.to_string()).into_err(namespace.position())
);
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
}
}
2022-02-28 07:37:46 +01:00
// Make sure identifiers are valid
Ok(lhs)
}
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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-05-09 10:15:50 +02:00
2022-02-28 07:37:46 +01:00
let (token, token_pos) = input.peek().expect(NEVER_ENDS);
2022-07-04 11:42:24 +02:00
if !(state.expr_filter)(token) {
return Err(LexError::UnexpectedInput(token.syntax().to_string()).into_err(*token_pos));
}
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();
let pos = eat_token(input, token.clone());
2022-02-28 07:37:46 +01:00
match self.parse_unary(input, state, lib, settings.level_up())? {
// 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-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,
pos,
operator_token: Some(token),
2022-02-28 07:37:46 +01:00
..Default::default()
}
.into_fn_call_expr(pos))
2021-06-16 12:36:33 +02:00
}
2020-05-09 10:15:50 +02:00
}
}
2022-02-28 07:37:46 +01:00
// +expr
Token::Plus | Token::UnaryPlus => {
let token = token.clone();
let pos = eat_token(input, token.clone());
2022-02-28 07:37:46 +01:00
match self.parse_unary(input, state, lib, settings.level_up())? {
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-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,
pos,
operator_token: Some(token),
2022-02-28 07:37:46 +01:00
..Default::default()
}
.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 => {
let token = token.clone();
2022-02-28 07:37:46 +01:00
let pos = eat_token(input, Token::Bang);
2022-02-28 07:37:46 +01:00
let mut args = StaticVec::new_const();
args.push(self.parse_unary(input, state, lib, settings.level_up())?);
args.shrink_to_fit();
2020-05-09 18:19:13 +02:00
2022-02-28 07:37:46 +01:00
Ok(FnCallExpr {
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,
pos,
operator_token: Some(token),
2022-02-28 07:37:46 +01:00
..Default::default()
}
.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-08-29 16:26:07 +02: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(
op: Option<Token>,
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-08-27 10:26:41 +02:00
let op_info = if let Some(ref op) = op {
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-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,
);
match state
.stack
.get_mut_by_index(state.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))
}
}
}
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
}
}
2022-06-10 05:22:33 +02:00
// ??? && ??? = rhs, ??? || ??? = rhs, xxx ?? xxx = rhs
Expr::And(..) | Expr::Or(..) | Expr::Coalesce(..) => Err(LexError::ImproperSymbol(
2022-02-28 07:37:46 +01:00
"=".to_string(),
"Possibly a typo of '=='?".to_string(),
)
.into_err(op_pos)),
// expr = rhs
2022-08-21 11:35:44 +02:00
_ => Err(PERR::AssignmentToInvalidLHS(String::new()).into_err(lhs.position())),
}
}
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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-04-26 12:04:07 +02:00
2022-02-28 07:37:46 +01:00
let (op, pos) = match input.peek().expect(NEVER_ENDS) {
// var = ...
(Token::Equals, ..) => (None, eat_token(input, Token::Equals)),
// var op= ...
(token, ..) if token.is_op_assignment() => input
.next()
.map(|(op, pos)| (Some(op), pos))
.expect(NEVER_ENDS),
// 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;
let rhs = self.parse_expr(input, state, lib, settings.level_up())?;
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-06-10 04:26:06 +02:00
x.rhs = Self::make_dot_expr(
state,
x.rhs,
rhs,
options | parent_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"))]
(.., Expr::FnCall(func, ..)) if func.is_qualified() => {
2022-03-05 10:57:23 +01:00
Err(PERR::PropertyExpected.into_err(func.namespace.position()))
2022-02-28 07:37:46 +01:00
}
// lhs.Fn() or lhs.eval()
(.., Expr::FnCall(func, func_pos))
if func.args.is_empty()
&& [crate::engine::KEYWORD_FN_PTR, crate::engine::KEYWORD_EVAL]
2022-07-05 10:26:38 +02:00
.contains(&func.name.as_str()) =>
2022-02-28 07:37:46 +01:00
{
let err_msg = format!(
"'{}' should not be called in method style. Try {}(...);",
func.name, func.name
);
Err(LexError::ImproperSymbol(func.name.to_string(), err_msg).into_err(func_pos))
}
// lhs.func!(...)
(.., Expr::FnCall(func, func_pos)) if func.capture_parent_scope => {
Err(PERR::MalformedCapture(
"method-call style does not support running within the caller's scope".into(),
)
.into_err(func_pos))
}
// lhs.func(...)
(lhs, Expr::FnCall(mut func, func_pos)) => {
// Recalculate hash
func.hashes = FnCallHashes::from_all(
#[cfg(not(feature = "no_function"))]
2022-09-21 05:46:23 +02:00
calc_fn_hash(None, &func.name, func.args.len()),
calc_fn_hash(None, &func.name, func.args.len() + 1),
2022-02-28 07:37:46 +01:00
);
2021-08-17 09:32:48 +02:00
let rhs = Expr::MethodCall(func, 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"))]
Expr::FnCall(func, ..) if func.is_qualified() => {
2022-03-05 10:57:23 +01:00
Err(PERR::PropertyExpected.into_err(func.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]
Expr::FnCall(mut func, func_pos) => {
// Recalculate hash
func.hashes = FnCallHashes::from_all(
#[cfg(not(feature = "no_function"))]
2022-09-21 05:46:23 +02:00
calc_fn_hash(None, &func.name, func.args.len()),
calc_fn_hash(None, &func.name, func.args.len() + 1),
2022-02-28 07:37:46 +01:00
);
2021-08-17 09:32:48 +02:00
2022-02-28 07:37:46 +01:00
let new_lhs = BinaryExpr {
lhs: Expr::MethodCall(func, 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> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2022-02-28 07:37:46 +01:00
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;
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
.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))?,
Token::Reserved(c) if !is_valid_identifier(c.chars()) => {
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);
}
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
.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))?,
Token::Reserved(c) if !is_valid_identifier(c.chars()) => {
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-02-28 07:37:46 +01:00
settings = settings.level_up();
settings.pos = pos;
2022-02-28 07:37:46 +01:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-10-26 14:49:49 +01:00
2022-02-28 07:37:46 +01:00
let op = op_token.syntax();
2022-09-21 05:46:23 +02:00
let hash = calc_fn_hash(None, &op, 2);
let operator_token = if is_valid_function_name(&op) {
None
} else {
Some(op_token.clone())
};
2020-10-25 14:57:18 +01:00
2022-02-28 07:37:46 +01:00
let op_base = FnCallExpr {
2022-08-13 12:07:42 +02:00
name: state.get_interned_string(op.as_ref()),
2022-02-28 07:37:46 +01:00
hashes: FnCallHashes::from_native(hash),
pos,
operator_token,
2022-02-28 07:37:46 +01:00
..Default::default()
};
2022-02-28 07:37:46 +01:00
let mut args = StaticVec::new_const();
args.push(root);
args.push(rhs);
args.shrink_to_fit();
2022-02-28 07:37:46 +01:00
root = match op_token {
// '!=' defaults to true when passed invalid operands
Token::NotEqualsTo => FnCallExpr { args, ..op_base }.into_fn_call_expr(pos),
// Comparison operators default to false when passed invalid operands
Token::EqualsTo
| Token::LessThan
| Token::LessThanEqualsTo
| Token::GreaterThan
| Token::GreaterThanEqualsTo => {
let pos = args[0].start_position();
FnCallExpr { args, ..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 => {
let rhs = args.pop().unwrap();
let current_lhs = args.pop().unwrap();
Expr::Or(
BinaryExpr {
lhs: current_lhs.ensure_bool_expr()?,
rhs: rhs.ensure_bool_expr()?,
}
.into(),
pos,
)
2021-06-10 04:16:39 +02:00
}
2022-02-28 07:37:46 +01:00
Token::And => {
let rhs = args.pop().unwrap();
let current_lhs = args.pop().unwrap();
Expr::And(
BinaryExpr {
lhs: current_lhs.ensure_bool_expr()?,
rhs: rhs.ensure_bool_expr()?,
}
.into(),
pos,
2021-06-10 04:16:39 +02:00
)
}
2022-06-10 05:22:33 +02:00
Token::DoubleQuestion => {
let rhs = args.pop().unwrap();
let current_lhs = args.pop().unwrap();
Expr::Coalesce(
BinaryExpr {
lhs: current_lhs,
rhs,
}
.into(),
pos,
)
}
2022-02-28 07:37:46 +01:00
Token::In => {
// Swap the arguments
let current_lhs = args.remove(0);
let pos = current_lhs.start_position();
args.push(current_lhs);
args.shrink_to_fit();
// Convert into a call to `contains`
FnCallExpr {
2022-09-21 05:46:23 +02:00
hashes: calc_fn_hash(None, OP_CONTAINS, 2).into(),
2022-02-28 07:37:46 +01:00
args,
2022-08-13 12:07:42 +02:00
name: state.get_interned_string(OP_CONTAINS),
2022-02-28 07:37:46 +01:00
..op_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
.get(s.as_str())
.map_or(false, Option::is_some) =>
{
2022-09-21 05:46:23 +02:00
let hash = calc_fn_hash(None, &s, 2);
2022-02-28 07:37:46 +01:00
let pos = args[0].start_position();
FnCallExpr {
hashes: if is_valid_function_name(&s) {
hash.into()
} else {
FnCallHashes::from_native(hash)
},
args,
..op_base
}
.into_fn_call_expr(pos)
2021-06-10 04:16:39 +02:00
}
2022-02-28 07:37:46 +01:00
_ => {
let pos = args[0].start_position();
FnCallExpr { args, ..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-07-05 16:59:03 +02:00
use crate::api::custom_syntax::markers::*;
2022-02-28 07:37:46 +01:00
let mut settings = settings;
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-02-28 07:37:46 +01:00
state.stack.push(marker, ());
}
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;
let settings = settings.level_up();
2022-09-12 06:03:32 +02:00
required_token = match parse_func(&segments, &*fwd_token.syntax(), &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
#[cfg(not(feature = "no_module"))]
let ns = crate::ast::Namespace::NONE;
#[cfg(feature = "no_module")]
let ns = ();
2022-02-28 07:37:46 +01:00
segments.push(name.clone().into());
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-02-28 07:37:46 +01:00
segments.push(keyword.clone().into());
tokens.push(keyword);
}
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-02-28 07:37:46 +01:00
segments.push(keyword.clone().into());
tokens.push(keyword);
}
stmt => unreachable!("Stmt::Block expected but gets {:?}", stmt),
}
}
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(
PERR::MissingSymbol("Expecting 'true' or 'false'".to_string())
.into_err(pos),
)
}
},
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(
PERR::MissingSymbol("Expecting an integer number".to_string())
.into_err(pos),
)
}
},
#[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) => {
return Err(PERR::MissingSymbol(
"Expecting a floating-point number".to_string(),
)
.into_err(pos))
}
},
CUSTOM_SYNTAX_MARKER_STRING => match input.next().expect(NEVER_ENDS) {
(Token::StringConstant(s), pos) => {
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) => {
return Err(
PERR::MissingSymbol("Expecting a string".to_string()).into_err(pos)
)
}
},
s => match input.next().expect(NEVER_ENDS) {
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
(t, ..) if &*t.syntax() == s => {
segments.push(required_token.clone());
tokens.push(required_token.clone().into());
}
(.., pos) => {
return Err(PERR::MissingToken(
s.to_string(),
format!("for '{}' expression", segments[0]),
)
.into_err(pos))
}
},
}
}
2022-02-28 07:37:46 +01:00
inputs.shrink_to_fit();
tokens.shrink_to_fit();
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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-03-02 10:04:56 +01:00
2022-02-28 07:37:46 +01:00
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);
let lhs = self.parse_unary(input, state, lib, settings.level_up())?;
self.parse_binary_op(input, state, lib, precedence, lhs, settings.level_up())
}
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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2021-11-16 16:13:53 +01:00
2022-02-28 07:37:46 +01:00
// 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
.parse_expr(input, state, lib, settings.level_up())?
.ensure_bool_expr()?;
ensure_not_assignment(input)?;
let if_body = self.parse_block(input, state, lib, settings.level_up())?;
// 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 ...
self.parse_if(input, state, lib, settings.level_up())?
} else {
// if guard { if_body } else { else-body }
self.parse_block(input, state, lib, settings.level_up())?
}
} 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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2016-02-29 22:43:45 +01:00
2022-02-28 07:37:46 +01:00
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
.parse_expr(input, state, lib, settings.level_up())?
.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;
settings.is_breakable = true;
2020-12-28 02:49:54 +01:00
2022-02-28 07:37:46 +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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2021-07-04 10:40:15 +02:00
2022-02-28 07:37:46 +01:00
// do ...
let mut settings = settings;
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
settings.is_breakable = true;
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-02-28 07:37:46 +01:00
settings.is_breakable = false;
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
.parse_expr(input, state, lib, settings.level_up())?
.ensure_bool_expr()?;
ensure_not_assignment(input)?;
2022-02-28 07:37:46 +01:00
Ok(Stmt::Do((guard, body.into()).into(), negated, settings.pos))
}
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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
// 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 {
return Err(
PERR::DuplicatedVariable(counter_name.to_string()).into_err(counter_pos)
);
}
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
.parse_expr(input, state, lib, settings.level_up())?
.ensure_iterable()?;
2016-02-29 22:43:45 +01:00
2022-02-28 07:37:46 +01:00
let prev_stack_len = state.stack.len();
2022-02-04 06:20:47 +01:00
2022-03-05 10:57:23 +01:00
if !counter_name.is_empty() {
2022-02-28 07:37:46 +01:00
state.stack.push(name.clone(), ());
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
2022-08-13 12:07:42 +02:00
let loop_var = state.get_interned_string(name);
2022-02-28 07:37:46 +01:00
state.stack.push(loop_var.clone(), ());
let loop_var = Ident {
name: loop_var,
pos: name_pos,
2022-02-13 11:46:25 +01:00
};
2022-02-28 07:37:46 +01:00
settings.is_breakable = true;
let body = self.parse_block(input, state, lib, settings.level_up())?;
2022-02-13 11:46:25 +01:00
2022-02-28 07:37:46 +01:00
state.stack.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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2022-02-28 07:37:46 +01:00
// 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-07-27 10:04:24 +02:00
if !self.allow_shadowing() && state.stack.iter().any(|(v, ..)| v == name) {
2022-02-28 07:37:46 +01:00
return Err(PERR::VariableExists(name.to_string()).into_err(pos));
}
if let Some(ref filter) = self.def_var_filter {
2022-07-27 10:04:24 +02:00
let will_shadow = state.stack.iter().any(|(v, ..)| v == name);
2022-02-28 07:37:46 +01:00
let level = settings.level;
let is_const = access == AccessMode::ReadOnly;
let info = VarDefInfo {
name: &name,
is_const,
nesting_level: level,
will_shadow,
};
2022-05-19 08:41:48 +02:00
let mut this_ptr = None;
let context = EvalContext::new(
self,
&mut state.stack,
&mut state.global,
None,
&[],
&mut this_ptr,
2022-02-28 07:37:46 +01:00
level,
2022-05-19 08:41:48 +02:00
);
2022-05-01 18:03:45 +02:00
match filter(false, info, context) {
2022-02-28 07:37:46 +01:00
Ok(true) => (),
Ok(false) => return Err(PERR::ForbiddenVariable(name.to_string()).into_err(pos)),
Err(err) => match *err {
2022-08-27 10:26:41 +02:00
EvalAltResult::ErrorParsing(e, pos) => return Err(e.into_err(pos)),
2022-02-28 07:37:46 +01:00
_ => return Err(PERR::ForbiddenVariable(name.to_string()).into_err(pos)),
},
}
}
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
self.parse_expr(input, state, lib, settings.level_up())?
} 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
};
let (existing, hit_barrier) = state.find_var(&name);
let existing = if !hit_barrier && existing > 0 {
let offset = state.stack.len() - existing;
if offset < state.block_stack_len {
2022-02-28 07:37:46 +01:00
// Defined in parent block
None
} else {
Some(offset)
2022-02-28 07:37:46 +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 {
state.stack.get_mut_by_index(n).set_access_mode(access);
Some(NonZeroUsize::new(state.stack.len() - n).unwrap())
} else {
state.stack.push_entry(name.as_str(), access, Dynamic::UNIT);
None
};
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-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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
// 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 ...
let expr = self.parse_expr(input, state, lib, settings.level_up())?;
2020-05-08 10:49:24 +02:00
2022-08-13 12:07:42 +02:00
// import expr;
2022-02-28 07:37:46 +01:00
if !match_token(input, Token::As).0 {
2022-08-13 12:07:42 +02:00
let empty = Ident {
name: state.get_interned_string(""),
pos: Position::NONE,
};
return Ok(Stmt::Import((expr, empty).into(), settings.pos));
}
2016-02-29 22:43:45 +01:00
2022-02-28 07:37:46 +01:00
// import expr as name ...
let (name, pos) = parse_var_name(input)?;
2022-08-13 12:07:42 +02:00
let name = state.get_interned_string(name);
2022-02-28 07:37:46 +01:00
state.imports.push(name.clone());
2022-02-28 07:37:46 +01:00
Ok(Stmt::Import(
2022-03-05 10:57:23 +01:00
(expr, Ident { name, pos }).into(),
2022-02-28 07:37:46 +01:00
settings.pos,
))
}
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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
let mut settings = settings;
settings.pos = eat_token(input, Token::Export);
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);
}
_ => (),
}
2022-02-28 07:37:46 +01:00
let (id, id_pos) = parse_var_name(input)?;
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-08-13 12:07:42 +02:00
name: state.get_interned_string(alias.as_ref().map_or("", <_>::as_ref)),
2022-02-28 07:37:46 +01:00
pos: alias_pos,
},
);
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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2022-02-28 07:37:46 +01:00
// 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
if !settings.allow_statements {
let stmt = self.parse_expr_stmt(input, state, lib, settings.level_up())?;
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;
state.block_stack_len = state.stack.len();
2020-04-28 17:05:03 +02:00
2022-02-28 07:37:46 +01:00
#[cfg(not(feature = "no_module"))]
let orig_imports_len = state.imports.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));
}
_ => (),
}
2022-02-28 07:37:46 +01:00
// Parse statements inside the block
2022-08-10 06:48:37 +02:00
settings.at_global_level = false;
2022-02-28 07:37:46 +01:00
let stmt = self.parse_stmt(input, state, lib, settings.level_up())?;
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));
}
}
};
state.stack.rewind(state.block_stack_len);
state.block_stack_len = prev_entry_stack_len;
#[cfg(not(feature = "no_module"))]
state.imports.truncate(orig_imports_len);
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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
2022-02-28 07:37:46 +01:00
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
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())?;
Ok(stmt)
}
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-08-10 06:48:37 +02:00
if !settings.at_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) => {
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-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;
2022-02-28 07:37:46 +01:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-03-18 11:41:18 +01: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
Token::LeftBrace => Ok(self.parse_block(input, state, lib, settings.level_up())?),
2022-02-28 07:37:46 +01:00
// fn ...
#[cfg(not(feature = "no_function"))]
2022-08-10 06:48:37 +02:00
Token::Fn if !settings.at_global_level => {
Err(PERR::WrongFnDefinition.into_err(token_pos))
}
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
let interned_strings = std::mem::take(&mut state.interned_strings);
2022-08-12 10:34:57 +02:00
let mut new_state = ParseState::new(
self,
state.scope,
2022-08-14 08:32:16 +02:00
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
.extend(state.imports.iter().cloned());
}
2022-02-28 07:37:46 +01:00
#[cfg(not(feature = "unchecked"))]
{
2022-02-28 09:32:08 +01:00
new_state.max_expr_depth = self.max_function_expr_depth();
2022-02-28 07:37:46 +01:00
}
2022-05-19 15:40:22 +02:00
let mut options = self.options;
options.set(
LangOptions::STRICT_VAR,
settings.options.contains(LangOptions::STRICT_VAR),
);
2022-02-28 07:37:46 +01:00
let new_settings = ParseSettings {
2022-08-10 06:48:37 +02:00
at_global_level: false,
in_fn_scope: true,
2022-02-28 07:37:46 +01:00
#[cfg(not(feature = "no_closure"))]
2022-08-10 06:48:37 +02:00
in_closure: false,
2022-02-28 07:37:46 +01:00
is_breakable: false,
allow_statements: true,
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,
};
let func = self.parse_fn(
input,
&mut new_state,
lib,
access,
new_settings,
#[cfg(not(feature = "no_function"))]
#[cfg(feature = "metadata")]
comments,
2022-08-12 10:34:57 +02:00
);
2022-08-14 08:32:16 +02:00
// Restore parse state
2022-08-12 10:34:57 +02:00
state.interned_strings = new_state.interned_strings;
let func = func?;
2022-02-28 07:37:46 +01:00
2022-09-21 05:46:23 +02:00
let hash = calc_fn_hash(None, &func.name, func.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(
func.name.to_string(),
func.params.len(),
)
.into_err(pos));
}
2021-04-09 16:49:47 +02:00
2022-02-28 07:37:46 +01:00
lib.insert(hash, func.into());
2022-02-28 07:37:46 +01:00
Ok(Stmt::Noop(pos))
}
2022-02-28 07:37:46 +01:00
(.., pos) => Err(PERR::MissingToken(
Token::Fn.into(),
format!("following '{}'", Token::Private.syntax()),
)
.into_err(pos)),
}
}
2022-02-28 07:37:46 +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-02-28 07:37:46 +01:00
self.parse_while_loop(input, state, lib, settings.level_up())
}
2022-02-28 09:32:08 +01:00
Token::Do if self.allow_looping() => {
2022-02-28 07:37:46 +01:00
self.parse_do(input, state, lib, settings.level_up())
}
2022-02-28 09:32:08 +01:00
Token::For if self.allow_looping() => {
2022-02-28 07:37:46 +01:00
self.parse_for(input, state, lib, settings.level_up())
}
2020-04-01 10:22:18 +02:00
2022-02-28 09:32:08 +01:00
Token::Continue if self.allow_looping() && settings.is_breakable => {
2022-02-28 07:37:46 +01:00
let pos = eat_token(input, Token::Continue);
Ok(Stmt::BreakLoop(ASTFlags::NONE, pos))
}
2022-02-28 09:32:08 +01:00
Token::Break if self.allow_looping() && settings.is_breakable => {
2022-02-28 07:37:46 +01:00
let pos = eat_token(input, Token::Break);
Ok(Stmt::BreakLoop(ASTFlags::BREAK, pos))
}
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-08-10 06:48:37 +02:00
(Token::RightBrace, ..) if !settings.at_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
_ => {
let expr = self.parse_expr(input, state, lib, settings.level_up())?;
Ok(Stmt::Return(Some(expr.into()), return_type, token_pos))
}
}
}
2020-04-01 10:22:18 +02:00
2022-02-28 07:37:46 +01:00
Token::Try => self.parse_try_catch(input, state, lib, settings.level_up()),
2020-10-20 17:16:03 +02:00
2022-02-28 07:37:46 +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"))]
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-08-10 06:48:37 +02:00
Token::Export if !settings.at_global_level => {
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"))]
Token::Export => self.parse_export(input, state, lib, settings.level_up()),
2020-05-08 10:49:24 +02:00
2022-02-28 07:37:46 +01:00
_ => self.parse_expr_stmt(input, state, lib, settings.level_up()),
}
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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-10-20 17:16:03 +02:00
2022-02-28 07:37:46 +01:00
// 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 }
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-02-28 07:37:46 +01:00
state.stack.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 }
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
state.stack.rewind(state.stack.len() - 1);
}
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> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2022-02-28 07:37:46 +01:00
let mut 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)),
};
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-02-28 07:37:46 +01:00
(.., pos) => return Err(PERR::FnMissingParams(name.to_string()).into_err(*pos)),
};
2022-08-13 12:07:42 +02:00
let mut params = StaticVec::<(ImmutableString, _)>::new_const();
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-08-13 12:07:42 +02:00
if params.iter().any(|(p, _)| p.as_str() == &*s) {
2022-02-28 07:37:46 +01:00
return Err(PERR::FnDuplicatedParam(name.to_string(), s.to_string())
.into_err(pos));
}
let s = state.get_interned_string(*s);
2022-02-28 07:37:46 +01:00
state.stack.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
}
}
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) {
(Token::LeftBrace, ..) => {
settings.is_breakable = false;
self.parse_block(input, state, lib, settings.level_up())?
}
(.., pos) => return Err(PERR::FnMissingBody(name.to_string()).into_err(*pos)),
}
2022-02-28 07:37:46 +01:00
.into();
2022-02-28 07:37:46 +01:00
let mut params: StaticVec<_> = params.into_iter().map(|(p, ..)| p).collect();
params.shrink_to_fit();
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,
fn_expr: Expr,
externals: StaticVec<crate::ast::Ident>,
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);
2022-02-28 07:37:46 +01:00
args.push(fn_expr);
args.extend(
externals
.iter()
.cloned()
.map(|crate::ast::Ident { name, pos }| {
2022-03-05 10:57:23 +01:00
#[cfg(not(feature = "no_module"))]
let ns = crate::ast::Namespace::NONE;
#[cfg(feature = "no_module")]
let ns = ();
Expr::Variable((None, ns, 0, name).into(), None, pos)
2022-02-28 07:37:46 +01:00
}),
);
let expr = FnCallExpr {
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,
pos,
..Default::default()
}
.into_fn_call_expr(pos);
// Convert the entire expression into a statement block, then insert the relevant
// [`Share`][Stmt::Share] statements.
let mut statements = StaticVec::with_capacity(externals.len() + 1);
statements.extend(
externals
.into_iter()
2022-08-13 12:07:42 +02:00
.map(|crate::ast::Ident { name, pos }| Stmt::Share(name, pos)),
2022-02-28 07:37:46 +01:00
);
statements.push(Stmt::Expr(expr.into()));
Expr::Stmt(crate::ast::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,
lib: &mut FnLib,
settings: ParseSettings,
) -> ParseResult<(Expr, ScriptFnDef)> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-07-19 11:14:55 +02:00
2022-02-28 07:37:46 +01:00
let mut 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-08-13 12:07:42 +02: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
}
let s = state.get_interned_string(*s);
2022-02-28 07:37:46 +01:00
state.stack.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
settings.is_breakable = false;
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"))]
let (mut params, externals) = {
let externals: StaticVec<_> = state.external_vars.iter().cloned().collect();
let mut params = StaticVec::with_capacity(params_list.len() + externals.len());
params.extend(
externals
.iter()
.map(|crate::ast::Ident { name, .. }| name.clone()),
);
2022-01-31 06:38:27 +01:00
2022-02-28 07:37:46 +01:00
(params, externals)
};
#[cfg(feature = "no_closure")]
let mut params = StaticVec::with_capacity(params_list.len());
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"))]
let expr = Self::make_curry_from_externals(state, 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,
2020-06-11 12:13:33 +02:00
input: &mut 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> {
let mut functions = StraightHashMap::default();
2020-07-26 09:53:22 +02:00
2022-05-19 15:40:22 +02:00
let mut options = self.options;
options.remove(LangOptions::STMT_EXPR);
2022-05-19 15:40:22 +02:00
#[cfg(not(feature = "no_function"))]
options.remove(LangOptions::ANON_FN);
let settings = ParseSettings {
2022-08-10 06:48:37 +02:00
at_global_level: true,
2021-11-28 03:49:48 +01:00
#[cfg(not(feature = "no_function"))]
2022-08-10 06:48:37 +02:00
in_fn_scope: false,
2021-12-04 10:57:28 +01:00
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_closure"))]
2022-08-10 06:48:37 +02:00
in_closure: false,
is_breakable: false,
allow_statements: false,
level: 0,
2022-05-19 15:40:22 +02:00
options,
2020-11-20 09:52:28 +01:00
pos: Position::NONE,
};
2022-02-28 07:37:46 +01:00
let expr = self.parse_expr(input, state, &mut functions, settings)?;
2020-06-03 04:44:36 +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
(token, pos) => {
2020-12-22 04:55:51 +01:00
return Err(LexError::UnexpectedInput(token.syntax().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
#[cfg(not(feature = "no_optimize"))]
2021-11-13 15:36:23 +01:00
return Ok(crate::optimizer::optimize_into_ast(
self,
state.scope,
statements,
#[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,
));
#[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,
input: &mut TokenStream,
2021-04-04 07:13:07 +02:00
state: &mut ParseState,
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();
let mut functions = StraightHashMap::default();
2020-06-14 08:25:47 +02:00
2021-05-22 13:14:24 +02:00
while !input.peek().expect(NEVER_ENDS).0.is_eof() {
2020-06-14 08:25:47 +02:00
let settings = ParseSettings {
2022-08-10 06:48:37 +02:00
at_global_level: true,
2021-11-28 03:49:48 +01:00
#[cfg(not(feature = "no_function"))]
2022-08-10 06:48:37 +02:00
in_fn_scope: false,
2021-12-04 10:57:28 +01:00
#[cfg(not(feature = "no_function"))]
#[cfg(not(feature = "no_closure"))]
2022-08-10 06:48:37 +02:00
in_closure: false,
2020-06-14 08:25:47 +02:00
is_breakable: false,
allow_statements: true,
2022-02-28 09:32:08 +01:00
options: self.options,
2020-06-14 08:25:47 +02:00
level: 0,
2020-11-20 09:52:28 +01:00
pos: Position::NONE,
2020-06-14 08:25:47 +02:00
};
2022-02-28 07:37:46 +01:00
let stmt = self.parse_stmt(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 => {
2020-06-14 08:25:47 +02:00
eat_token(input, Token::SemiColon);
}
// 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.
#[inline]
2020-06-11 12:13:33 +02:00
pub(crate) fn parse(
2020-06-03 04:44:36 +02:00
&self,
2020-06-11 12:13:33 +02:00
input: &mut 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> {
2021-10-21 13:30:58 +02:00
let (statements, _lib) = self.parse_global_level(input, state)?;
2020-06-03 04:44:36 +02:00
#[cfg(not(feature = "no_optimize"))]
2021-11-13 15:36:23 +01:00
return Ok(crate::optimizer::optimize_into_ast(
self,
state.scope,
statements,
#[cfg(not(feature = "no_function"))]
2021-10-21 13:30:58 +02:00
_lib,
2022-04-11 10:29:16 +02:00
_optimization_level,
));
#[cfg(feature = "no_optimize")]
2021-10-21 13:30:58 +02:00
#[cfg(not(feature = "no_function"))]
{
let mut m = crate::Module::new();
2022-01-28 11:59:18 +01:00
for fn_def in _lib {
m.set_script_fn(fn_def);
2022-01-28 11:59:18 +01: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
}