rhai/src/parser.rs

3168 lines
108 KiB
Rust
Raw Normal View History

2020-03-08 12:54:02 +01:00
//! Main module defining the lexer and parser.
2020-12-22 09:45:56 +01:00
use crate::ast::{BinaryExpr, CustomExpr, Expr, FnCallExpr, Ident, ReturnType, ScriptFnDef, Stmt};
use crate::dynamic::{AccessMode, Union};
use crate::engine::KEYWORD_THIS;
2020-11-10 16:26:50 +01:00
use crate::module::NamespaceRef;
2020-11-16 16:10:14 +01:00
use crate::optimize::optimize_into_ast;
2020-11-16 16:32:44 +01:00
use crate::optimize::OptimizationLevel;
2020-03-17 19:26:11 +01:00
use crate::stdlib::{
borrow::Cow,
boxed::Box,
2020-11-08 03:56:33 +01:00
collections::HashMap,
2020-10-29 04:37:51 +01:00
format,
2020-11-13 11:32:18 +01:00
hash::{Hash, Hasher},
2020-06-11 17:08:00 +02:00
iter::empty,
2020-12-24 09:32:43 +01:00
num::{NonZeroU64, NonZeroUsize},
2020-03-17 19:26:11 +01:00
string::{String, ToString},
vec,
2020-03-17 19:26:11 +01:00
vec::Vec,
2020-03-12 05:40:28 +01:00
};
use crate::syntax::{CustomSyntax, MARKER_BLOCK, MARKER_EXPR, MARKER_IDENT};
2020-12-29 05:29:45 +01:00
use crate::token::{is_keyword_function, is_valid_identifier, Token, TokenStream};
2020-11-16 16:10:14 +01:00
use crate::utils::{get_hasher, StraightHasherBuilder};
use crate::{
2020-11-27 16:37:59 +01:00
calc_script_fn_hash, Dynamic, Engine, ImmutableString, LexError, ParseError, ParseErrorType,
Position, Scope, StaticVec, AST,
2020-11-16 16:10:14 +01:00
};
#[cfg(not(feature = "no_float"))]
use crate::FLOAT;
2016-02-29 22:43:45 +01:00
2020-11-27 16:37:59 +01:00
#[cfg(not(feature = "no_function"))]
use crate::FnAccess;
2020-03-01 06:30:22 +01:00
type PERR = ParseErrorType;
2020-12-24 09:32:43 +01:00
type FunctionsLib = HashMap<NonZeroU64, ScriptFnDef, StraightHasherBuilder>;
2020-11-25 02:36:06 +01:00
/// A type that encapsulates the current state of the parser.
2020-11-13 11:32:18 +01:00
#[derive(Debug)]
2020-07-05 11:41:45 +02:00
struct ParseState<'e> {
2020-11-25 02:36:06 +01:00
/// Reference to the scripting [`Engine`].
2020-07-05 11:41:45 +02:00
engine: &'e Engine,
/// Interned strings.
strings: HashMap<String, ImmutableString>,
/// Encapsulates a local stack with variable names to simulate an actual runtime scope.
stack: Vec<(ImmutableString, AccessMode)>,
/// Size of the local variables stack upon entry of the current block scope.
entry_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"))]
2020-11-13 03:43:54 +01:00
externals: HashMap<ImmutableString, Position>,
/// An indicator that disables variable capturing into externals one single time
/// up until the nearest consumed Identifier token.
2020-08-03 06:10:20 +02:00
/// If set to false the next call to `access_var` will not capture the variable.
/// All consequent calls to `access_var` will not be affected
2020-08-03 06:10:20 +02:00
#[cfg(not(feature = "no_closure"))]
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"))]
2020-11-15 05:07:35 +01:00
modules: StaticVec<ImmutableString>,
/// Maximum levels of expression nesting.
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
2021-01-06 06:46:53 +01:00
max_expr_depth: Option<NonZeroUsize>,
/// Maximum levels of expression nesting in functions.
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
#[cfg(not(feature = "no_function"))]
2021-01-06 06:46:53 +01:00
max_function_expr_depth: Option<NonZeroUsize>,
}
2020-04-28 17:05:03 +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)]
2020-07-26 09:53:22 +02:00
pub fn new(
engine: &'e Engine,
2021-01-06 06:46:53 +01:00
#[cfg(not(feature = "unchecked"))] max_expr_depth: Option<NonZeroUsize>,
#[cfg(not(feature = "unchecked"))]
#[cfg(not(feature = "no_function"))]
2021-01-06 06:46:53 +01:00
max_function_expr_depth: Option<NonZeroUsize>,
2020-07-26 09:53:22 +02:00
) -> Self {
Self {
2020-07-05 11:41:45 +02:00
engine,
2020-07-29 16:43:50 +02:00
#[cfg(not(feature = "unchecked"))]
max_expr_depth,
#[cfg(not(feature = "unchecked"))]
#[cfg(not(feature = "no_function"))]
2020-07-29 16:43:50 +02:00
max_function_expr_depth,
2020-08-03 06:10:20 +02:00
#[cfg(not(feature = "no_closure"))]
2020-11-15 06:49:54 +01:00
externals: Default::default(),
2020-08-03 06:10:20 +02:00
#[cfg(not(feature = "no_closure"))]
allow_capture: true,
2020-11-15 05:07:35 +01:00
strings: HashMap::with_capacity(64),
stack: Vec::with_capacity(16),
entry_stack_len: 0,
#[cfg(not(feature = "no_module"))]
2020-07-05 11:41:45 +02:00
modules: Default::default(),
}
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.
///
2020-07-29 16:43:50 +02:00
/// If the variable is not present in the scope adds it to the list of external variables
///
2020-04-29 10:11:54 +02:00
/// The return value is 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.
///
/// Return `None` when the variable name is not found in the `stack`.
2020-10-08 16:25:50 +02:00
#[inline]
2020-07-30 07:28:06 +02:00
fn access_var(&mut self, name: &str, _pos: Position) -> Option<NonZeroUsize> {
let mut barrier = false;
2020-07-29 16:43:50 +02:00
let index = self
.stack
2020-04-28 17:05:03 +02:00
.iter()
.rev()
.enumerate()
.find(|(_, (n, _))| {
if n.is_empty() {
// Do not go beyond empty variable names
barrier = true;
false
} else {
*n == name
}
})
.and_then(|(i, _)| NonZeroUsize::new(i + 1));
2020-08-03 06:10:20 +02:00
#[cfg(not(feature = "no_closure"))]
if self.allow_capture {
if index.is_none() && !self.externals.contains_key(name) {
2020-11-13 03:43:54 +01:00
self.externals.insert(name.into(), _pos);
}
} else {
2020-08-03 06:10:20 +02:00
self.allow_capture = true
}
if barrier {
None
} else {
index
}
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"))]
2020-10-08 16:25:50 +02:00
#[inline(always)]
pub fn find_module(&self, name: &str) -> Option<NonZeroUsize> {
2020-10-18 16:10:08 +02:00
self.modules
2020-05-04 13:36:58 +02:00
.iter()
.rev()
.enumerate()
2020-11-13 03:43:54 +01:00
.find(|(_, n)| **n == name)
2020-10-18 16:10:08 +02:00
.and_then(|(i, _)| NonZeroUsize::new(i + 1))
2020-04-28 17:05:03 +02:00
}
/// Get an interned string, creating one if it is not yet interned.
2020-11-16 16:17:34 +01:00
pub fn get_interned_string(
2020-11-16 16:10:14 +01:00
&mut self,
text: impl AsRef<str> + Into<ImmutableString>,
) -> ImmutableString {
2020-11-12 16:09:27 +01:00
#[allow(clippy::map_entry)]
2020-11-13 03:43:54 +01:00
if !self.strings.contains_key(text.as_ref()) {
2021-01-02 16:30:10 +01:00
let value = text.into();
self.strings.insert(value.clone().into(), value.clone());
value
} else {
2020-11-13 03:43:54 +01:00
self.strings.get(text.as_ref()).unwrap().clone()
}
}
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 {
/// Current position.
pos: Position,
/// Is the construct being parsed located at global level?
is_global: bool,
2020-07-16 06:09:31 +02:00
/// Is the construct being parsed located at function definition level?
is_function_scope: bool,
/// Is the current position inside a loop?
is_breakable: bool,
/// Is anonymous function allowed?
allow_anonymous_fn: bool,
/// Is if-expression allowed?
allow_if_expr: bool,
2020-11-14 16:43:36 +01:00
/// Is switch expression allowed?
allow_switch_expr: bool,
/// Is statement-expression allowed?
allow_stmt_expr: bool,
/// Current expression nesting level.
level: usize,
}
impl ParseSettings {
/// Create a new `ParseSettings` with one higher expression level.
2020-10-08 16:25:50 +02:00
#[inline(always)]
pub 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.
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
2021-01-06 06:46:53 +01:00
#[inline(always)]
pub fn ensure_level_within_max_limit(
&self,
limit: Option<NonZeroUsize>,
) -> Result<(), ParseError> {
if let Some(limit) = limit {
if self.level > limit.get() {
return Err(PERR::ExprTooDeep.into_err(self.pos));
}
}
2021-01-06 06:46:53 +01:00
Ok(())
}
}
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]
fn into_property(self, state: &mut ParseState) -> Self {
match self {
Self::Variable(x) if x.1.is_none() => {
2020-12-24 16:22:50 +01:00
let ident = x.2;
2020-11-16 09:28:04 +01:00
let getter = state.get_interned_string(crate::engine::make_getter(&ident.name));
let setter = state.get_interned_string(crate::engine::make_setter(&ident.name));
2020-12-25 04:02:29 +01:00
Self::Property(Box::new((getter, setter, ident.into())))
}
_ => self,
}
}
}
2020-11-25 02:36:06 +01:00
/// Consume a particular [token][Token], checking that it is the expected one.
2020-06-11 12:13:33 +02:00
fn eat_token(input: &mut TokenStream, token: Token) -> Position {
let (t, pos) = input.next().unwrap();
if t != token {
2020-06-16 16:14:46 +02:00
unreachable!(
"expecting {} (found {}) at {}",
token.syntax(),
t.syntax(),
pos
);
}
pos
}
2020-11-25 02:36:06 +01:00
/// Match a particular [token][Token], consuming it if matched.
2020-10-20 17:16:03 +02:00
fn match_token(input: &mut TokenStream, token: Token) -> (bool, Position) {
let (t, pos) = input.peek().unwrap();
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)
}
}
2020-03-18 03:36:50 +01:00
/// Parse ( expr )
2020-06-11 12:13:33 +02:00
fn parse_paren_expr(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
2020-12-27 09:50:48 +01:00
mut settings: ParseSettings,
) -> Result<Expr, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
// ( ...
settings.pos = eat_token(input, Token::LeftParen);
2020-10-20 17:16:03 +02:00
if match_token(input, Token::RightParen).0 {
return Ok(Expr::Unit(settings.pos));
}
let expr = parse_expr(input, state, lib, settings.level_up())?;
2016-02-29 22:43:45 +01:00
match input.next().unwrap() {
2020-03-18 03:36:50 +01:00
// ( xxx )
(Token::RightParen, _) => Ok(expr),
// ( <error>
2020-06-14 10:56:36 +02:00
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
2020-03-18 03:36:50 +01:00
// ( xxx ???
(_, pos) => Err(PERR::MissingToken(
2020-05-04 13:36:58 +02:00
Token::RightParen.into(),
"for a matching ( in this expression".into(),
)
2020-06-11 17:08:00 +02:00
.into_err(pos)),
2016-02-29 22:43:45 +01:00
}
}
2020-03-18 03:36:50 +01:00
/// Parse a function call.
2020-07-26 16:25:30 +02:00
fn parse_fn_call(
2020-06-11 12:13:33 +02:00
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
id: ImmutableString,
2020-07-30 12:18:28 +02:00
capture: bool,
2020-12-24 16:22:50 +01:00
mut namespace: Option<NamespaceRef>,
2020-06-11 17:08:00 +02:00
settings: ParseSettings,
) -> Result<Expr, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
let (token, token_pos) = input.peek().unwrap();
let mut args = StaticVec::new();
2017-10-28 05:30:12 +02:00
match token {
2020-06-29 17:55:28 +02:00
// id( <EOF>
Token::EOF => {
2020-04-06 06:29:01 +02:00
return Err(PERR::MissingToken(
2020-05-04 13:36:58 +02:00
Token::RightParen.into(),
2020-04-06 06:29:01 +02:00
format!("to close the arguments list of this function call '{}'", id),
)
2020-06-29 17:55:28 +02:00
.into_err(*token_pos))
2020-04-06 06:29:01 +02:00
}
2020-06-29 17:55:28 +02:00
// id( <error>
2020-11-25 02:36:06 +01:00
Token::LexError(err) => return Err(err.clone().into_err(*token_pos)),
2020-04-06 06:29:01 +02:00
// id()
Token::RightParen => {
eat_token(input, Token::RightParen);
2020-12-24 16:22:50 +01:00
let mut hash_script = if let Some(ref mut modules) = namespace {
2020-10-18 16:10:08 +02:00
#[cfg(not(feature = "no_module"))]
2020-10-31 17:04:02 +01:00
modules.set_index(state.find_module(&modules[0].name));
// Rust functions are indexed in two steps:
// 1) Calculate a hash in a similar manner to script-defined functions,
// i.e. qualifiers + function name + number of arguments.
// 2) Calculate a second hash with no qualifiers, empty function name,
// zero number of arguments, and the actual list of argument `TypeId`'s.
// 3) The final hash is the XOR of the two hashes.
2020-10-31 17:04:02 +01:00
let qualifiers = modules.iter().map(|m| m.name.as_str());
2020-10-28 15:18:44 +01:00
calc_script_fn_hash(qualifiers, &id, 0)
} else {
2020-10-28 15:18:44 +01:00
calc_script_fn_hash(empty(), &id, 0)
2020-05-09 10:15:50 +02:00
};
2020-12-24 09:32:43 +01:00
// script functions can only be valid identifiers
if !is_valid_identifier(id.chars()) {
hash_script = None;
}
2020-10-31 16:26:21 +01:00
return Ok(Expr::FnCall(
2020-11-10 16:26:50 +01:00
Box::new(FnCallExpr {
name: id.to_string().into(),
2020-10-31 16:26:21 +01:00
capture,
namespace,
2020-12-24 09:32:43 +01:00
hash_script,
2020-10-31 16:26:21 +01:00
args,
..Default::default()
}),
settings.pos,
));
2020-04-06 06:29:01 +02:00
}
// id...
_ => (),
2016-02-29 22:43:45 +01:00
}
let settings = settings.level_up();
2016-02-29 22:43:45 +01:00
loop {
2020-06-16 16:14:46 +02:00
match input.peek().unwrap() {
// id(...args, ) - handle trailing comma
(Token::RightParen, _) => (),
_ => args.push(parse_expr(input, state, lib, settings)?),
2020-06-16 16:14:46 +02:00
}
2016-02-29 22:43:45 +01:00
match input.peek().unwrap() {
// id(...args)
(Token::RightParen, _) => {
eat_token(input, Token::RightParen);
2020-05-04 11:43:54 +02:00
2020-12-24 09:32:43 +01:00
let mut hash_script = if let Some(modules) = namespace.as_mut() {
2020-10-18 16:10:08 +02:00
#[cfg(not(feature = "no_module"))]
2020-10-31 17:04:02 +01:00
modules.set_index(state.find_module(&modules[0].name));
// Rust functions are indexed in two steps:
// 1) Calculate a hash in a similar manner to script-defined functions,
// i.e. qualifiers + function name + number of arguments.
// 2) Calculate a second hash with no qualifiers, empty function name,
// zero number of arguments, and the actual list of argument `TypeId`'s.
// 3) The final hash is the XOR of the two hashes.
2020-10-31 17:04:02 +01:00
let qualifiers = modules.iter().map(|m| m.name.as_str());
2020-10-28 15:18:44 +01:00
calc_script_fn_hash(qualifiers, &id, args.len())
} else {
2020-10-28 15:18:44 +01:00
calc_script_fn_hash(empty(), &id, args.len())
2020-05-09 10:15:50 +02:00
};
2020-12-24 09:32:43 +01:00
// script functions can only be valid identifiers
if !is_valid_identifier(id.chars()) {
hash_script = None;
}
2020-10-31 16:26:21 +01:00
return Ok(Expr::FnCall(
2020-11-10 16:26:50 +01:00
Box::new(FnCallExpr {
name: id.to_string().into(),
2020-10-31 16:26:21 +01:00
capture,
namespace,
2020-12-24 09:32:43 +01:00
hash_script,
2020-10-31 16:26:21 +01:00
args,
..Default::default()
}),
settings.pos,
));
}
// id(...args,
(Token::Comma, _) => {
eat_token(input, Token::Comma);
}
// id(...args <EOF>
(Token::EOF, pos) => {
2020-04-06 06:29:01 +02:00
return Err(PERR::MissingToken(
2020-05-04 13:36:58 +02:00
Token::RightParen.into(),
2020-04-06 06:29:01 +02:00
format!("to close the arguments list of this function call '{}'", id),
)
.into_err(*pos))
2020-04-06 06:29:01 +02:00
}
// id(...args <error>
2020-11-25 02:36:06 +01:00
(Token::LexError(err), pos) => return Err(err.clone().into_err(*pos)),
// id(...args ???
(_, pos) => {
return Err(PERR::MissingToken(
2020-05-04 13:36:58 +02:00
Token::Comma.into(),
format!("to separate the arguments to function call '{}'", id),
)
2020-03-24 09:46:47 +01:00
.into_err(*pos))
}
2016-02-29 22:43:45 +01:00
}
}
}
2020-04-26 13:37:32 +02:00
/// Parse an indexing chain.
/// Indexing binds to the right, so this call parses all possible levels of indexing following in the input.
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "no_index"))]
2020-06-11 12:13:33 +02:00
fn parse_index_chain(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
2020-04-28 17:05:03 +02:00
lhs: Expr,
2020-06-11 17:08:00 +02:00
mut settings: ParseSettings,
) -> Result<Expr, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
let idx_expr = parse_expr(input, state, lib, settings.level_up())?;
2020-03-09 03:41:17 +01:00
2020-03-29 17:53:35 +02:00
// Check type of indexing - must be integer or string
2020-03-09 03:41:17 +01:00
match &idx_expr {
2020-03-18 03:36:50 +01:00
// lhs[int]
2020-10-31 07:13:45 +01:00
Expr::IntegerConstant(x, pos) if *x < 0 => {
2020-03-24 09:46:47 +01:00
return Err(PERR::MalformedIndexExpr(format!(
"Array access expects non-negative index: {} < 0",
2020-10-31 07:13:45 +01:00
*x
2020-03-09 03:41:17 +01:00
))
2020-10-31 07:13:45 +01:00
.into_err(*pos))
2020-03-09 03:41:17 +01:00
}
2020-10-31 07:13:45 +01:00
Expr::IntegerConstant(_, pos) => match lhs {
2020-11-13 03:43:54 +01:00
Expr::Array(_, _) | Expr::StringConstant(_, _) => (),
2020-03-29 17:53:35 +02:00
2020-10-31 16:26:21 +01:00
Expr::Map(_, _) => {
2020-03-29 17:53:35 +02:00
return Err(PERR::MalformedIndexExpr(
"Object map access expects string index, not a number".into(),
)
2020-10-31 07:13:45 +01:00
.into_err(*pos))
2020-03-29 17:53:35 +02:00
}
#[cfg(not(feature = "no_float"))]
2020-10-31 07:13:45 +01:00
Expr::FloatConstant(_, _) => {
return Err(PERR::MalformedIndexExpr(
"Only arrays, object maps and strings can be indexed".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(lhs.position()))
}
2020-10-31 07:13:45 +01:00
Expr::CharConstant(_, _)
2020-10-31 16:26:21 +01:00
| Expr::And(_, _)
| Expr::Or(_, _)
| Expr::In(_, _)
| Expr::BoolConstant(_, _)
2020-05-25 14:14:31 +02:00
| Expr::Unit(_) => {
2020-03-29 17:53:35 +02:00
return Err(PERR::MalformedIndexExpr(
"Only arrays, object maps and strings can be indexed".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(lhs.position()))
2020-03-29 17:53:35 +02:00
}
_ => (),
},
// lhs[string]
2020-11-13 03:43:54 +01:00
Expr::StringConstant(_, pos) => match lhs {
2020-10-31 16:26:21 +01:00
Expr::Map(_, _) => (),
2020-03-29 17:53:35 +02:00
2020-11-13 03:43:54 +01:00
Expr::Array(_, _) | Expr::StringConstant(_, _) => {
2020-03-29 17:53:35 +02:00
return Err(PERR::MalformedIndexExpr(
"Array or string expects numeric index, not a string".into(),
)
2020-11-13 03:43:54 +01:00
.into_err(*pos))
2020-03-29 17:53:35 +02:00
}
#[cfg(not(feature = "no_float"))]
2020-10-31 07:13:45 +01:00
Expr::FloatConstant(_, _) => {
return Err(PERR::MalformedIndexExpr(
"Only arrays, object maps and strings can be indexed".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(lhs.position()))
}
2020-10-31 07:13:45 +01:00
Expr::CharConstant(_, _)
2020-10-31 16:26:21 +01:00
| Expr::And(_, _)
| Expr::Or(_, _)
| Expr::In(_, _)
| Expr::BoolConstant(_, _)
2020-05-25 14:14:31 +02:00
| Expr::Unit(_) => {
2020-03-29 17:53:35 +02:00
return Err(PERR::MalformedIndexExpr(
"Only arrays, object maps and strings can be indexed".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(lhs.position()))
2020-03-29 17:53:35 +02:00
}
_ => (),
},
2020-03-18 03:36:50 +01:00
// lhs[float]
#[cfg(not(feature = "no_float"))]
2020-10-31 07:13:45 +01:00
x @ Expr::FloatConstant(_, _) => {
2020-03-24 09:46:47 +01:00
return Err(PERR::MalformedIndexExpr(
"Array access expects integer index, not a float".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
2020-03-09 03:41:17 +01:00
}
2020-03-18 03:36:50 +01:00
// lhs[char]
2020-10-31 07:13:45 +01:00
x @ Expr::CharConstant(_, _) => {
2020-03-24 09:46:47 +01:00
return Err(PERR::MalformedIndexExpr(
"Array access expects integer index, not a character".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
}
// lhs[()]
2020-05-25 14:14:31 +02:00
x @ Expr::Unit(_) => {
2020-03-24 09:46:47 +01:00
return Err(PERR::MalformedIndexExpr(
"Array access expects integer index, not ()".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
2020-03-09 03:41:17 +01:00
}
// lhs[??? && ???], lhs[??? || ???], lhs[??? in ???]
2020-10-31 16:26:21 +01:00
x @ Expr::And(_, _) | x @ Expr::Or(_, _) | x @ Expr::In(_, _) => {
return Err(PERR::MalformedIndexExpr(
"Array access expects integer index, not a boolean".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
}
// lhs[true], lhs[false]
x @ Expr::BoolConstant(_, _) => {
2020-03-24 09:46:47 +01:00
return Err(PERR::MalformedIndexExpr(
"Array access expects integer index, not a boolean".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
2020-03-09 03:41:17 +01:00
}
2020-03-18 03:36:50 +01:00
// All other expressions
2020-03-09 03:41:17 +01:00
_ => (),
}
// Check if there is a closing bracket
match input.peek().unwrap() {
(Token::RightBracket, _) => {
eat_token(input, Token::RightBracket);
2020-04-26 12:04:07 +02:00
2020-04-26 13:37:32 +02:00
// Any more indexing following?
2020-04-26 12:04:07 +02:00
match input.peek().unwrap() {
2020-04-26 13:37:32 +02:00
// If another indexing level, right-bind it
2020-04-26 12:04:07 +02:00
(Token::LeftBracket, _) => {
2020-06-11 17:08:00 +02:00
let prev_pos = settings.pos;
settings.pos = eat_token(input, Token::LeftBracket);
2020-04-26 13:37:32 +02:00
// Recursively parse the indexing chain, right-binding each
let idx_expr =
parse_index_chain(input, state, lib, idx_expr, settings.level_up())?;
2020-04-26 13:37:32 +02:00
// Indexing binds to right
2020-10-31 16:26:21 +01:00
Ok(Expr::Index(
Box::new(BinaryExpr { lhs, rhs: idx_expr }),
prev_pos,
))
2020-04-26 12:04:07 +02:00
}
2020-04-26 13:37:32 +02:00
// Otherwise terminate the indexing chain
_ => Ok(Expr::Index(
Box::new(BinaryExpr { lhs, rhs: idx_expr }),
settings.pos,
)),
2020-04-26 12:04:07 +02:00
}
}
2020-11-25 02:36:06 +01:00
(Token::LexError(err), pos) => return Err(err.clone().into_err(*pos)),
(_, pos) => Err(PERR::MissingToken(
2020-05-04 13:36:58 +02:00
Token::RightBracket.into(),
"for a matching [ in this index expression".into(),
)
.into_err(*pos)),
2020-03-09 03:41:17 +01:00
}
2016-03-26 18:46:28 +01:00
}
2020-03-18 03:36:50 +01:00
/// Parse an array literal.
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "no_index"))]
2020-06-11 12:13:33 +02:00
fn parse_array_literal(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
2020-12-27 09:50:48 +01:00
mut settings: ParseSettings,
) -> Result<Expr, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
// [ ...
settings.pos = eat_token(input, Token::LeftBracket);
let mut arr = StaticVec::new();
2016-03-26 18:46:28 +01:00
2020-11-13 11:32:18 +01:00
loop {
const MISSING_RBRACKET: &str = "to end this array literal";
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
if state.engine.max_array_size() > 0 && arr.len() >= state.engine.max_array_size() {
2020-06-16 16:14:46 +02:00
return Err(PERR::LiteralTooLarge(
"Size of array literal".to_string(),
state.engine.max_array_size(),
2020-06-16 16:14:46 +02:00
)
.into_err(input.peek().unwrap().1));
}
2020-06-16 16:14:46 +02:00
match input.peek().unwrap() {
(Token::RightBracket, _) => {
eat_token(input, Token::RightBracket);
break;
}
2020-11-13 11:32:18 +01:00
(Token::EOF, pos) => {
return Err(
PERR::MissingToken(Token::RightBracket.into(), MISSING_RBRACKET.into())
.into_err(*pos),
)
}
2020-06-16 16:14:46 +02:00
_ => {
let expr = parse_expr(input, state, lib, settings.level_up())?;
2020-06-16 16:14:46 +02:00
arr.push(expr);
}
}
2020-06-16 16:14:46 +02:00
match input.peek().unwrap() {
(Token::Comma, _) => {
eat_token(input, Token::Comma);
}
(Token::RightBracket, _) => (),
(Token::EOF, pos) => {
2020-11-13 11:32:18 +01:00
return Err(
PERR::MissingToken(Token::RightBracket.into(), MISSING_RBRACKET.into())
.into_err(*pos),
2020-06-16 16:14:46 +02:00
)
}
2020-11-25 02:36:06 +01:00
(Token::LexError(err), pos) => return Err(err.clone().into_err(*pos)),
2020-06-16 16:14:46 +02:00
(_, pos) => {
return Err(PERR::MissingToken(
Token::Comma.into(),
"to separate the items of this array literal".into(),
)
.into_err(*pos))
}
};
2016-03-26 18:46:28 +01:00
}
2020-10-31 16:26:21 +01:00
Ok(Expr::Array(Box::new(arr), settings.pos))
2016-03-26 18:46:28 +01:00
}
2020-03-29 17:53:35 +02:00
/// Parse a map literal.
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "no_object"))]
2020-06-11 12:13:33 +02:00
fn parse_map_literal(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
2020-12-27 09:50:48 +01:00
mut settings: ParseSettings,
) -> Result<Expr, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
// #{ ...
settings.pos = eat_token(input, Token::MapStart);
2020-12-22 09:45:56 +01:00
let mut map: StaticVec<(Ident, Expr)> = Default::default();
2020-03-29 17:53:35 +02:00
2020-11-13 11:32:18 +01:00
loop {
2020-06-16 16:14:46 +02:00
const MISSING_RBRACE: &str = "to end this object map literal";
2020-06-16 16:14:46 +02:00
match input.peek().unwrap() {
(Token::RightBrace, _) => {
eat_token(input, Token::RightBrace);
break;
}
2020-11-13 11:32:18 +01:00
(Token::EOF, pos) => {
return Err(
PERR::MissingToken(Token::RightBrace.into(), MISSING_RBRACE.into())
.into_err(*pos),
)
}
_ => (),
}
2020-06-16 16:14:46 +02:00
let (name, pos) = match input.next().unwrap() {
2020-11-13 11:32:18 +01:00
(Token::Identifier(s), pos) | (Token::StringConstant(s), pos) => {
if map.iter().any(|(p, _)| p.name == &s) {
return Err(PERR::DuplicatedProperty(s).into_err(pos));
}
(s, pos)
}
(Token::Reserved(s), pos) if is_valid_identifier(s.chars()) => {
return Err(PERR::Reserved(s).into_err(pos));
}
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
(_, pos) if map.is_empty() => {
return Err(
PERR::MissingToken(Token::RightBrace.into(), MISSING_RBRACE.into())
.into_err(pos),
);
}
(Token::EOF, pos) => {
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
match input.next().unwrap() {
(Token::Colon, _) => (),
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
(_, pos) => {
return Err(PERR::MissingToken(
Token::Colon.into(),
format!(
"to follow the property '{}' in this object map literal",
name
),
)
.into_err(pos))
2020-06-14 08:25:47 +02:00
}
};
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
if state.engine.max_map_size() > 0 && map.len() >= state.engine.max_map_size() {
return Err(PERR::LiteralTooLarge(
"Number of properties in object map literal".to_string(),
state.engine.max_map_size(),
)
.into_err(input.peek().unwrap().1));
2020-06-16 16:14:46 +02:00
}
2020-03-29 17:53:35 +02:00
let expr = parse_expr(input, state, lib, settings.level_up())?;
2020-12-22 09:45:56 +01:00
let name = state.get_interned_string(name);
map.push((Ident { name, pos }, expr));
2020-06-16 16:14:46 +02:00
match input.peek().unwrap() {
(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))
}
2020-11-25 02:36:06 +01:00
(Token::LexError(err), pos) => return Err(err.clone().into_err(*pos)),
2020-06-16 16:14:46 +02:00
(_, pos) => {
return Err(
PERR::MissingToken(Token::RightBrace.into(), MISSING_RBRACE.into())
.into_err(*pos),
)
2020-03-29 17:53:35 +02:00
}
}
}
2020-10-31 16:26:21 +01:00
Ok(Expr::Map(Box::new(map), settings.pos))
2020-03-29 17:53:35 +02:00
}
2020-11-13 11:32:18 +01:00
/// Parse a switch expression.
fn parse_switch(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
2020-11-14 16:43:36 +01:00
mut settings: ParseSettings,
) -> Result<Stmt, ParseError> {
2020-11-13 11:32:18 +01:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
// switch ...
settings.pos = eat_token(input, Token::Switch);
2020-11-13 11:32:18 +01:00
let item = parse_expr(input, state, lib, settings.level_up())?;
match input.next().unwrap() {
(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-12-11 05:57:07 +01:00
let mut table = HashMap::new();
2020-11-13 11:32:18 +01:00
let mut def_stmt = None;
loop {
const MISSING_RBRACE: &str = "to end this switch block";
let expr = match input.peek().unwrap() {
(Token::RightBrace, _) => {
eat_token(input, Token::RightBrace);
break;
}
(Token::EOF, pos) => {
return Err(
PERR::MissingToken(Token::RightBrace.into(), MISSING_RBRACE.into())
.into_err(*pos),
)
}
(Token::Underscore, _) if def_stmt.is_none() => {
eat_token(input, Token::Underscore);
None
}
(Token::Underscore, pos) => return Err(PERR::DuplicatedSwitchCase.into_err(*pos)),
_ => Some(parse_expr(input, state, lib, settings.level_up())?),
};
let hash = if let Some(expr) = expr {
if let Some(value) = expr.get_constant_value() {
let hasher = &mut get_hasher();
value.hash(hasher);
let hash = hasher.finish();
if table.contains_key(&hash) {
return Err(PERR::DuplicatedSwitchCase.into_err(expr.position()));
}
Some(hash)
} else {
return Err(PERR::ExprExpected("a literal".to_string()).into_err(expr.position()));
}
} else {
None
};
match input.next().unwrap() {
(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))
}
};
2020-12-29 03:41:20 +01:00
let stmt = parse_stmt(input, state, lib, settings.level_up())?;
2020-11-13 11:32:18 +01:00
let need_comma = !stmt.is_self_terminated();
def_stmt = if let Some(hash) = hash {
table.insert(hash, stmt);
None
} else {
Some(stmt)
};
match input.peek().unwrap() {
(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),
)
}
2020-11-25 02:36:06 +01:00
(Token::LexError(err), pos) => return Err(err.clone().into_err(*pos)),
2020-11-13 11:32:18 +01:00
(_, pos) if need_comma => {
return Err(PERR::MissingToken(
Token::Comma.into(),
"to separate the items in this switch block".into(),
)
.into_err(*pos))
}
(_, _) => (),
}
}
2020-12-11 05:57:07 +01:00
let mut final_table = HashMap::with_capacity_and_hasher(table.len(), StraightHasherBuilder);
final_table.extend(table.into_iter());
2020-11-14 16:43:36 +01:00
Ok(Stmt::Switch(
item,
Box::new((final_table.into(), def_stmt)),
2020-11-13 11:32:18 +01:00
settings.pos,
))
}
2020-03-18 03:36:50 +01:00
/// Parse a primary expression.
2020-06-11 12:13:33 +02:00
fn parse_primary(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
) -> Result<Expr, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
let (token, token_pos) = input.peek().unwrap();
settings.pos = *token_pos;
2020-12-27 09:50:48 +01:00
let mut root_expr = match token {
Token::EOF => return Err(PERR::UnexpectedEOF.into_err(settings.pos)),
Token::IntegerConstant(_)
| Token::CharConstant(_)
| Token::StringConstant(_)
| Token::True
| Token::False => match input.next().unwrap().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)
}
Token::True => Expr::BoolConstant(true, settings.pos),
Token::False => Expr::BoolConstant(false, settings.pos),
t => unreachable!("unexpected token: {:?}", t),
2020-12-27 09:50:48 +01:00
},
#[cfg(not(feature = "no_float"))]
Token::FloatConstant(x) => {
let x = (*x).into();
2020-12-27 09:50:48 +01:00
input.next().unwrap();
Expr::FloatConstant(x, settings.pos)
}
2021-02-13 13:57:56 +01:00
#[cfg(feature = "decimal")]
Token::DecimalConstant(x) => {
let x = (*x).into();
input.next().unwrap();
Expr::DynamicConstant(Box::new(x), settings.pos)
}
2020-12-27 09:50:48 +01:00
// { - block statement as expression
Token::LeftBrace if settings.allow_stmt_expr => {
2020-12-27 09:50:48 +01:00
match parse_block(input, state, lib, settings.level_up())? {
2020-11-04 04:49:02 +01:00
Stmt::Block(statements, pos) => Expr::Stmt(Box::new(statements.into()), pos),
stmt => unreachable!("expecting Stmt::Block, but gets {:?}", stmt),
2020-12-27 09:50:48 +01:00
}
2020-03-07 03:39:00 +01:00
}
2020-12-27 09:50:48 +01:00
// ( - grouped expression
Token::LeftParen => parse_paren_expr(input, state, lib, settings.level_up())?,
2020-07-30 12:18:28 +02:00
2020-12-27 09:50:48 +01:00
// If statement is allowed to act as expressions
Token::If if settings.allow_if_expr => Expr::Stmt(
Box::new(vec![parse_if(input, state, lib, settings.level_up())?].into()),
settings.pos,
),
// Switch statement is allowed to act as expressions
Token::Switch if settings.allow_switch_expr => Expr::Stmt(
Box::new(vec![parse_switch(input, state, lib, settings.level_up())?].into()),
settings.pos,
),
// | ...
#[cfg(not(feature = "no_function"))]
Token::Pipe | Token::Or if settings.allow_anonymous_fn => {
let mut new_state = ParseState::new(
state.engine,
#[cfg(not(feature = "unchecked"))]
state.max_function_expr_depth,
#[cfg(not(feature = "unchecked"))]
state.max_function_expr_depth,
);
2020-08-08 16:59:05 +02:00
2020-12-27 09:50:48 +01:00
let settings = ParseSettings {
allow_if_expr: true,
allow_switch_expr: true,
allow_stmt_expr: true,
allow_anonymous_fn: true,
is_global: false,
is_function_scope: true,
is_breakable: false,
level: 0,
2020-12-22 09:45:56 +01:00
pos: settings.pos,
};
2020-12-27 09:50:48 +01:00
let (expr, func) = parse_anon_fn(input, &mut new_state, lib, settings)?;
2020-08-22 16:44:24 +02:00
#[cfg(not(feature = "no_closure"))]
2020-12-27 09:50:48 +01:00
new_state.externals.iter().for_each(|(closure, pos)| {
state.access_var(closure, *pos);
});
2020-12-29 03:41:20 +01:00
lib.insert(
calc_script_fn_hash(empty(), &func.name, func.params.len()).unwrap(),
func,
);
2020-12-27 09:50:48 +01:00
expr
2020-08-22 16:44:24 +02:00
}
2020-12-27 09:50:48 +01:00
// Array literal
#[cfg(not(feature = "no_index"))]
Token::LeftBracket => parse_array_literal(input, state, lib, settings.level_up())?,
// Map literal
#[cfg(not(feature = "no_object"))]
Token::MapStart => parse_map_literal(input, state, lib, settings.level_up())?,
// Identifier
Token::Identifier(_) => {
let s = match input.next().unwrap().0 {
Token::Identifier(s) => s,
t => unreachable!("expecting Token::Identifier, but gets {:?}", t),
2020-12-22 09:45:56 +01:00
};
2020-08-22 16:44:24 +02:00
2020-12-27 09:50:48 +01:00
match input.peek().unwrap().0 {
// Function call
Token::LeftParen | Token::Bang => {
#[cfg(not(feature = "no_closure"))]
{
2021-02-03 12:14:26 +01:00
// Once the identifier consumed we must enable next variables capturing
2020-12-27 09:50:48 +01:00
state.allow_capture = true;
}
let var_name_def = Ident {
name: state.get_interned_string(s),
pos: settings.pos,
};
Expr::Variable(Box::new((None, None, var_name_def)))
}
// Namespace qualification
#[cfg(not(feature = "no_module"))]
Token::DoubleColon => {
#[cfg(not(feature = "no_closure"))]
{
2021-02-03 12:14:26 +01:00
// Once the identifier consumed we must enable next variables capturing
2020-12-27 09:50:48 +01:00
state.allow_capture = true;
}
let var_name_def = Ident {
name: state.get_interned_string(s),
pos: settings.pos,
};
Expr::Variable(Box::new((None, None, var_name_def)))
}
// Normal variable access
_ => {
let index = state.access_var(&s, settings.pos);
let var_name_def = Ident {
name: state.get_interned_string(s),
pos: settings.pos,
};
Expr::Variable(Box::new((index, None, var_name_def)))
}
2020-07-26 16:25:30 +02:00
}
2020-07-16 06:09:31 +02:00
}
2020-08-22 16:44:24 +02:00
2020-12-27 09:50:48 +01:00
// Reserved keyword or symbol
Token::Reserved(_) => {
let s = match input.next().unwrap().0 {
Token::Reserved(s) => s,
t => unreachable!("expecting Token::Reserved, but gets {:?}", t),
2020-12-27 09:50:48 +01:00
};
match input.peek().unwrap().0 {
// Function call is allowed to have reserved keyword
2021-02-03 12:14:26 +01:00
Token::LeftParen | Token::Bang if is_keyword_function(&s) => {
let var_name_def = Ident {
name: state.get_interned_string(s),
pos: settings.pos,
};
Expr::Variable(Box::new((None, None, var_name_def)))
}
// Access to `this` as a variable is OK within a function scope
_ if s == KEYWORD_THIS && settings.is_function_scope => {
let var_name_def = Ident {
name: state.get_interned_string(s),
pos: settings.pos,
};
Expr::Variable(Box::new((None, None, var_name_def)))
2020-12-27 09:50:48 +01:00
}
2021-02-03 12:14:26 +01:00
// Cannot access to `this` as a variable not in a function scope
2020-12-27 09:50:48 +01:00
_ if s == KEYWORD_THIS => {
2021-02-03 12:14:26 +01:00
let msg = format!("'{}' can only be used in functions", s);
return Err(LexError::ImproperSymbol(s, msg).into_err(settings.pos));
2020-12-27 09:50:48 +01:00
}
_ if is_valid_identifier(s.chars()) => {
2021-02-03 12:14:26 +01:00
return Err(PERR::Reserved(s).into_err(settings.pos))
2020-12-27 09:50:48 +01:00
}
2021-02-03 12:14:26 +01:00
_ => return Err(LexError::UnexpectedInput(s).into_err(settings.pos)),
2020-07-16 06:09:31 +02:00
}
}
2020-08-22 16:44:24 +02:00
2020-12-27 09:50:48 +01:00
Token::LexError(_) => {
let err = match input.next().unwrap().0 {
Token::LexError(err) => err,
t => unreachable!("expecting Token::LexError, but gets {:?}", t),
2020-12-27 09:50:48 +01:00
};
2020-08-22 16:44:24 +02:00
2020-12-27 09:50:48 +01:00
return Err(err.into_err(settings.pos));
}
2020-08-22 16:44:24 +02:00
2020-06-14 08:25:47 +02:00
_ => {
2021-02-03 12:14:26 +01:00
return Err(LexError::UnexpectedInput(token.syntax().to_string()).into_err(settings.pos))
2020-04-17 13:00:52 +02:00
}
};
// Tail processing all possible postfix operators
loop {
2020-12-27 09:50:48 +01:00
let (tail_token, _) = input.peek().unwrap();
2020-12-27 09:50:48 +01:00
if !root_expr.is_valid_postfix(tail_token) {
2020-04-17 13:00:52 +02:00
break;
2020-03-24 09:46:47 +01:00
}
2020-03-05 13:28:03 +01:00
2020-12-27 09:50:48 +01:00
let (tail_token, tail_pos) = input.next().unwrap();
settings.pos = tail_pos;
2020-04-10 06:16:39 +02:00
2020-12-27 09:50:48 +01:00
root_expr = match (root_expr, tail_token) {
2020-10-12 10:59:59 +02:00
// Qualified function call with !
(Expr::Variable(x), Token::Bang) if x.1.is_some() => {
2020-10-20 17:16:03 +02:00
return Err(if !match_token(input, Token::LeftParen).0 {
2020-12-27 09:50:48 +01:00
LexError::UnexpectedInput(Token::Bang.syntax().to_string()).into_err(tail_pos)
2020-10-12 10:59:59 +02:00
} else {
2020-12-22 04:55:51 +01:00
LexError::ImproperSymbol(
"!".to_string(),
2020-11-02 05:50:27 +01:00
"'!' cannot be used to call module functions".to_string(),
2020-12-22 04:55:51 +01:00
)
2020-12-27 09:50:48 +01:00
.into_err(tail_pos)
2020-10-12 10:59:59 +02:00
});
}
// Function call with !
2020-07-30 12:18:28 +02:00
(Expr::Variable(x), Token::Bang) => {
2020-10-20 17:16:03 +02:00
let (matched, pos) = match_token(input, Token::LeftParen);
if !matched {
2020-07-30 12:18:28 +02:00
return Err(PERR::MissingToken(
Token::LeftParen.syntax().into(),
"to start arguments list of function call".into(),
)
2020-10-20 17:16:03 +02:00
.into_err(pos));
2020-07-30 12:18:28 +02:00
}
2020-12-24 16:22:50 +01:00
let (_, namespace, Ident { name, pos }) = *x;
2020-07-30 12:18:28 +02:00
settings.pos = pos;
2020-12-24 16:22:50 +01:00
let ns = namespace.map(|(_, ns)| ns);
parse_fn_call(input, state, lib, name, true, ns, settings.level_up())?
2020-07-30 12:18:28 +02:00
}
2020-04-17 13:00:52 +02:00
// Function call
(Expr::Variable(x), Token::LeftParen) => {
2020-12-24 16:22:50 +01:00
let (_, namespace, Ident { name, pos }) = *x;
2020-06-11 17:08:00 +02:00
settings.pos = pos;
2020-12-24 16:22:50 +01:00
let ns = namespace.map(|(_, ns)| ns);
parse_fn_call(input, state, lib, name, false, ns, settings.level_up())?
}
2020-05-04 17:07:42 +02:00
// module access
2020-05-09 18:19:13 +02:00
(Expr::Variable(x), Token::DoubleColon) => match input.next().unwrap() {
(Token::Identifier(id2), pos2) => {
2020-12-24 16:22:50 +01:00
let (index, mut namespace, var_name_def) = *x;
2020-06-11 17:08:00 +02:00
2020-12-24 16:22:50 +01:00
if let Some((_, ref mut namespace)) = namespace {
namespace.push(var_name_def);
} else {
2020-12-24 16:22:50 +01:00
let mut ns: NamespaceRef = Default::default();
ns.push(var_name_def);
let index = NonZeroU64::new(42).unwrap(); // Dummy
namespace = Some((index, ns));
2020-05-04 11:43:54 +02:00
}
2020-12-22 09:45:56 +01:00
let var_name_def = Ident {
name: state.get_interned_string(id2),
pos: pos2,
};
2020-12-24 16:22:50 +01:00
Expr::Variable(Box::new((index, namespace, var_name_def)))
2020-05-04 11:43:54 +02:00
}
(Token::Reserved(id2), pos2) if is_valid_identifier(id2.chars()) => {
return Err(PERR::Reserved(id2).into_err(pos2));
}
(_, pos2) => return Err(PERR::VariableExpected.into_err(pos2)),
},
2020-04-17 13:00:52 +02:00
// Indexing
#[cfg(not(feature = "no_index"))]
2020-04-28 17:05:03 +02:00
(expr, Token::LeftBracket) => {
parse_index_chain(input, state, lib, expr, settings.level_up())?
2020-04-28 17:05:03 +02:00
}
2020-12-27 09:50:48 +01:00
// Property access
2020-12-21 10:39:37 +01:00
#[cfg(not(feature = "no_object"))]
(expr, Token::Period) => {
// Expression after dot must start with an identifier
match input.peek().unwrap() {
(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
}
2020-12-28 02:49:54 +01:00
let rhs = parse_primary(input, state, lib, settings.level_up())?;
2020-12-27 09:50:48 +01:00
make_dot_expr(state, expr, rhs, tail_pos)?
2020-12-21 10:39:37 +01:00
}
2020-04-17 13:00:52 +02:00
// Unknown postfix operator
2020-06-16 16:14:46 +02:00
(expr, token) => unreachable!(
"unknown postfix operator '{}' for {:?}",
token.syntax(),
expr
),
}
2016-02-29 22:43:45 +01:00
}
2020-12-21 10:39:37 +01:00
// Cache the hash key for namespace-qualified variables
match &mut root_expr {
2020-12-21 10:39:37 +01:00
Expr::Variable(x) if x.1.is_some() => Some(x),
Expr::Index(x, _) | Expr::Dot(x, _) => match &mut x.lhs {
Expr::Variable(x) if x.1.is_some() => Some(x),
_ => None,
},
_ => None,
}
2020-12-28 02:49:54 +01:00
.map(|x| match x.as_mut() {
(_, Some((ref mut hash, ref mut namespace)), Ident { name, .. }) => {
2020-12-24 16:22:50 +01:00
*hash =
calc_script_fn_hash(namespace.iter().map(|v| v.name.as_str()), name, 0).unwrap();
2020-12-24 16:22:50 +01:00
#[cfg(not(feature = "no_module"))]
namespace.set_index(state.find_module(&namespace[0].name));
}
_ => unreachable!("expecting namespace-qualified variable access"),
2020-12-21 10:39:37 +01:00
});
2020-07-16 06:09:31 +02:00
// Make sure identifiers are valid
Ok(root_expr)
2016-02-29 22:43:45 +01:00
}
2020-03-18 03:36:50 +01:00
/// Parse a potential unary operator.
2020-06-11 12:13:33 +02:00
fn parse_unary(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
) -> Result<Expr, ParseError> {
let (token, token_pos) = input.peek().unwrap();
settings.pos = *token_pos;
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
match token {
2020-03-18 03:36:50 +01:00
// -expr
Token::UnaryMinus => {
2020-04-17 13:00:52 +02:00
let pos = eat_token(input, Token::UnaryMinus);
match parse_unary(input, state, lib, settings.level_up())? {
// Negative integer
2020-10-31 07:13:45 +01:00
Expr::IntegerConstant(num, pos) => num
.checked_neg()
.map(|i| Expr::IntegerConstant(i, pos))
.or_else(|| {
#[cfg(not(feature = "no_float"))]
return Some(Expr::FloatConstant((-(num as FLOAT)).into(), pos));
2020-10-31 07:13:45 +01:00
#[cfg(feature = "no_float")]
return None;
})
.ok_or_else(|| LexError::MalformedNumber(format!("-{}", num)).into_err(pos)),
// Negative float
#[cfg(not(feature = "no_float"))]
Expr::FloatConstant(x, pos) => Ok(Expr::FloatConstant((-(*x)).into(), pos)),
// Call negative function
expr => {
2020-05-09 10:15:50 +02:00
let op = "-";
let mut args = StaticVec::new();
args.push(expr);
2020-05-09 10:15:50 +02:00
2020-10-31 16:26:21 +01:00
Ok(Expr::FnCall(
2020-11-10 16:26:50 +01:00
Box::new(FnCallExpr {
2020-10-31 16:26:21 +01:00
name: op.into(),
args,
..Default::default()
}),
2020-10-31 07:13:45 +01:00
pos,
2020-10-31 16:26:21 +01:00
))
2020-05-09 10:15:50 +02:00
}
}
2019-09-18 12:21:07 +02:00
}
2020-03-18 03:36:50 +01:00
// +expr
Token::UnaryPlus => {
2020-12-21 10:39:37 +01:00
let pos = eat_token(input, Token::UnaryPlus);
match 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 op = "+";
let mut args = StaticVec::new();
args.push(expr);
Ok(Expr::FnCall(
Box::new(FnCallExpr {
name: op.into(),
args,
..Default::default()
}),
pos,
))
}
}
2019-09-18 12:21:07 +02:00
}
2020-03-18 03:36:50 +01:00
// !expr
Token::Bang => {
2020-04-17 13:00:52 +02:00
let pos = eat_token(input, Token::Bang);
let mut args = StaticVec::new();
2021-01-24 14:21:15 +01:00
let expr = parse_unary(input, state, lib, settings.level_up())?;
args.push(expr);
2020-05-09 18:19:13 +02:00
2020-05-09 10:15:50 +02:00
let op = "!";
2020-10-31 16:26:21 +01:00
Ok(Expr::FnCall(
2020-11-10 16:26:50 +01:00
Box::new(FnCallExpr {
2020-10-31 16:26:21 +01:00
name: op.into(),
args,
..Default::default()
}),
2020-10-31 07:13:45 +01:00
pos,
2020-10-31 16:26:21 +01:00
))
2019-09-18 12:21:07 +02:00
}
// <EOF>
Token::EOF => Err(PERR::UnexpectedEOF.into_err(settings.pos)),
// All other tokens
_ => parse_primary(input, state, lib, settings.level_up()),
2017-10-30 16:08:44 +01:00
}
}
2020-12-26 06:05:57 +01:00
/// Make an assignment statement.
fn make_assignment_stmt<'a>(
2020-05-25 14:14:31 +02:00
fn_name: Cow<'static, str>,
state: &mut ParseState,
2020-04-22 11:37:06 +02:00
lhs: Expr,
rhs: Expr,
2020-12-22 09:45:56 +01:00
op_pos: Position,
2020-10-27 16:21:20 +01:00
) -> Result<Stmt, ParseError> {
2020-12-29 03:41:20 +01:00
fn check_lvalue(expr: &Expr, parent_is_dot: bool) -> Position {
match expr {
Expr::Index(x, _) | Expr::Dot(x, _) if parent_is_dot => match x.lhs {
Expr::Property(_) => check_lvalue(&x.rhs, matches!(expr, Expr::Dot(_, _))),
ref e => e.position(),
},
Expr::Index(x, _) | Expr::Dot(x, _) => match x.lhs {
Expr::Property(_) => unreachable!("unexpected Expr::Property in indexing"),
_ => check_lvalue(&x.rhs, matches!(expr, Expr::Dot(_, _))),
},
Expr::Property(_) if parent_is_dot => Position::NONE,
Expr::Property(_) => unreachable!("unexpected Expr::Property in indexing"),
e if parent_is_dot => e.position(),
_ => Position::NONE,
}
}
match &lhs {
2020-12-29 03:41:20 +01:00
// const_expr = rhs
expr if expr.is_constant() => {
Err(PERR::AssignmentToConstant("".into()).into_err(lhs.position()))
}
2020-06-11 17:21:39 +02:00
// var (non-indexed) = rhs
2020-12-22 09:45:56 +01:00
Expr::Variable(x) if x.0.is_none() => Ok(Stmt::Assignment(
Box::new((lhs, fn_name.into(), rhs)),
op_pos,
)),
2020-06-11 17:21:39 +02:00
// var (indexed) = rhs
Expr::Variable(x) => {
2020-12-24 16:22:50 +01:00
let (index, _, Ident { name, pos }) = x.as_ref();
match state.stack[(state.stack.len() - index.unwrap().get())].1 {
2020-12-22 09:45:56 +01:00
AccessMode::ReadWrite => Ok(Stmt::Assignment(
Box::new((lhs, fn_name.into(), rhs)),
op_pos,
)),
// Constant values cannot be assigned to
AccessMode::ReadOnly => {
2020-12-22 09:45:56 +01:00
Err(PERR::AssignmentToConstant(name.to_string()).into_err(*pos))
}
}
}
2020-12-29 03:41:20 +01:00
// xxx[???]... = rhs, xxx.prop... = rhs
Expr::Index(x, _) | Expr::Dot(x, _) => {
match check_lvalue(&x.rhs, matches!(lhs, Expr::Dot(_, _))) {
Position::NONE => match &x.lhs {
// var[???] (non-indexed) = rhs, var.??? (non-indexed) = rhs
Expr::Variable(x) if x.0.is_none() => Ok(Stmt::Assignment(
2020-12-22 09:45:56 +01:00
Box::new((lhs, fn_name.into(), rhs)),
op_pos,
)),
2020-12-29 03:41:20 +01:00
// var[???] (indexed) = rhs, var.??? (indexed) = rhs
Expr::Variable(x) => {
let (index, _, Ident { name, pos }) = x.as_ref();
match state.stack[(state.stack.len() - index.unwrap().get())].1 {
AccessMode::ReadWrite => Ok(Stmt::Assignment(
Box::new((lhs, fn_name.into(), rhs)),
op_pos,
)),
// Constant values cannot be assigned to
AccessMode::ReadOnly => {
Err(PERR::AssignmentToConstant(name.to_string()).into_err(*pos))
}
}
}
2020-12-29 03:41:20 +01:00
// expr[???] = rhs, expr.??? = rhs
expr => {
Err(PERR::AssignmentToInvalidLHS("".to_string()).into_err(expr.position()))
}
},
pos => Err(PERR::AssignmentToInvalidLHS("".to_string()).into_err(pos)),
}
}
2020-06-11 17:21:39 +02:00
// ??? && ??? = rhs, ??? || ??? = rhs
2020-12-22 04:55:51 +01:00
Expr::And(_, _) | Expr::Or(_, _) => Err(LexError::ImproperSymbol(
"=".to_string(),
2020-11-02 05:50:27 +01:00
"Possibly a typo of '=='?".to_string(),
2020-12-22 04:55:51 +01:00
)
2020-12-22 09:45:56 +01:00
.into_err(op_pos)),
2020-06-11 17:21:39 +02:00
// expr = rhs
_ => Err(PERR::AssignmentToInvalidLHS("".to_string()).into_err(lhs.position())),
}
2020-04-22 11:37:06 +02:00
}
2020-03-18 03:36:50 +01:00
/// Parse an operator-assignment expression.
2020-06-11 12:13:33 +02:00
fn parse_op_assignment_stmt(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
2020-04-06 06:29:01 +02:00
lhs: Expr,
mut settings: ParseSettings,
2020-10-27 16:21:20 +01:00
) -> Result<Stmt, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
let (token, token_pos) = input.peek().unwrap();
settings.pos = *token_pos;
let op = match token {
2020-05-25 14:14:31 +02:00
Token::Equals => "".into(),
Token::PlusAssign
| Token::MinusAssign
| Token::MultiplyAssign
| Token::DivideAssign
| Token::LeftShiftAssign
| Token::RightShiftAssign
| Token::ModuloAssign
| Token::PowerOfAssign
| Token::AndAssign
| Token::OrAssign
| Token::XOrAssign => token.syntax(),
2020-10-27 16:21:20 +01:00
_ => return Ok(Stmt::Expr(lhs)),
2020-04-22 11:37:06 +02:00
};
2020-05-25 14:14:31 +02:00
let (_, pos) = input.next().unwrap();
let rhs = parse_expr(input, state, lib, settings.level_up())?;
2020-05-25 14:14:31 +02:00
make_assignment_stmt(op, state, lhs, rhs, pos)
2020-04-26 12:04:07 +02:00
}
/// Make a dot expression.
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "no_object"))]
fn make_dot_expr(
state: &mut ParseState,
lhs: Expr,
rhs: Expr,
op_pos: Position,
) -> Result<Expr, ParseError> {
2020-05-04 11:43:54 +02:00
Ok(match (lhs, rhs) {
// idx_lhs[idx_expr].rhs
2020-04-26 13:37:32 +02:00
// Attach dot chain to the bottom level of indexing chain
2020-10-31 16:26:21 +01:00
(Expr::Index(mut x, pos), rhs) => {
x.rhs = make_dot_expr(state, x.rhs, rhs, op_pos)?;
2020-10-31 16:26:21 +01:00
Expr::Index(x, pos)
}
2020-04-26 12:04:07 +02:00
// lhs.id
(lhs, Expr::Variable(x)) if x.1.is_none() => {
2020-12-24 16:22:50 +01:00
let ident = x.2;
2020-11-16 09:28:04 +01:00
let getter = state.get_interned_string(crate::engine::make_getter(&ident.name));
let setter = state.get_interned_string(crate::engine::make_setter(&ident.name));
2020-12-25 04:02:29 +01:00
let rhs = Expr::Property(Box::new((getter, setter, ident)));
2020-10-31 16:26:21 +01:00
Expr::Dot(Box::new(BinaryExpr { lhs, rhs }), op_pos)
}
2020-05-04 13:36:58 +02:00
// lhs.module::id - syntax error
(_, Expr::Variable(x)) if x.1.is_some() => {
return Err(PERR::PropertyExpected.into_err(x.1.unwrap().1[0].pos))
2020-05-04 11:43:54 +02:00
}
// lhs.prop
2020-10-31 16:26:21 +01:00
(lhs, prop @ Expr::Property(_)) => {
Expr::Dot(Box::new(BinaryExpr { lhs, rhs: prop }), op_pos)
}
2020-04-26 12:04:07 +02:00
// lhs.dot_lhs.dot_rhs
(lhs, Expr::Dot(x, pos)) => match x.lhs {
Expr::Variable(_) | Expr::Property(_) | Expr::FnCall(_, _) => {
let rhs = Expr::Dot(
Box::new(BinaryExpr {
lhs: x.lhs.into_property(state),
rhs: x.rhs,
}),
pos,
);
Expr::Dot(Box::new(BinaryExpr { lhs, rhs }), op_pos)
}
_ => unreachable!("invalid dot expression: {:?}", x.lhs),
},
2020-04-26 12:04:07 +02:00
// lhs.idx_lhs[idx_rhs]
2020-10-31 16:26:21 +01:00
(lhs, Expr::Index(x, pos)) => {
let rhs = Expr::Index(
Box::new(BinaryExpr {
lhs: x.lhs.into_property(state),
2020-10-31 16:26:21 +01:00
rhs: x.rhs,
}),
pos,
);
Expr::Dot(Box::new(BinaryExpr { lhs, rhs }), op_pos)
2020-05-09 18:19:13 +02:00
}
// lhs.Fn() or lhs.eval()
2020-10-31 16:26:21 +01:00
(_, Expr::FnCall(x, pos))
if x.args.len() == 0
&& [crate::engine::KEYWORD_FN_PTR, crate::engine::KEYWORD_EVAL]
.contains(&x.name.as_ref()) =>
{
2020-12-22 04:55:51 +01:00
return Err(LexError::ImproperSymbol(
x.name.to_string(),
format!(
"'{}' should not be called in method style. Try {}(...);",
x.name, x.name
),
2020-12-22 04:55:51 +01:00
)
.into_err(pos))
}
2020-07-30 12:18:28 +02:00
// lhs.func!(...)
2020-10-31 16:26:21 +01:00
(_, Expr::FnCall(x, pos)) if x.capture => {
2020-07-30 12:18:28 +02:00
return Err(PERR::MalformedCapture(
"method-call style does not support capturing".into(),
)
.into_err(pos))
2020-07-30 12:18:28 +02:00
}
// lhs.func(...)
2020-10-31 16:26:21 +01:00
(lhs, func @ Expr::FnCall(_, _)) => {
Expr::Dot(Box::new(BinaryExpr { lhs, rhs: func }), op_pos)
}
2020-04-26 12:04:07 +02:00
// lhs.rhs
2020-07-03 16:48:33 +02:00
(_, rhs) => return Err(PERR::PropertyExpected.into_err(rhs.position())),
2020-05-04 11:43:54 +02:00
})
2020-03-07 06:39:28 +01:00
}
2020-04-26 12:04:07 +02:00
/// Make an 'in' expression.
fn make_in_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result<Expr, ParseError> {
2020-04-06 11:47:34 +02:00
match (&lhs, &rhs) {
2020-10-31 07:13:45 +01:00
(_, x @ Expr::IntegerConstant(_, _))
2020-10-31 16:26:21 +01:00
| (_, x @ Expr::And(_, _))
| (_, x @ Expr::Or(_, _))
| (_, x @ Expr::In(_, _))
| (_, x @ Expr::BoolConstant(_, _))
2020-05-25 14:14:31 +02:00
| (_, x @ Expr::Unit(_)) => {
2020-04-06 11:47:34 +02:00
return Err(PERR::MalformedInExpr(
"'in' expression expects a string, array or object map".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
2020-04-06 11:47:34 +02:00
}
#[cfg(not(feature = "no_float"))]
2020-10-31 07:13:45 +01:00
(_, x @ Expr::FloatConstant(_, _)) => {
return Err(PERR::MalformedInExpr(
"'in' expression expects a string, array or object map".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
}
2020-04-06 11:47:34 +02:00
// "xxx" in "xxxx", 'x' in "xxxx" - OK!
2020-11-13 03:43:54 +01:00
(Expr::StringConstant(_, _), Expr::StringConstant(_, _))
| (Expr::CharConstant(_, _), Expr::StringConstant(_, _)) => (),
2020-04-06 11:47:34 +02:00
// 123.456 in "xxxx"
#[cfg(not(feature = "no_float"))]
2020-11-13 03:43:54 +01:00
(x @ Expr::FloatConstant(_, _), Expr::StringConstant(_, _)) => {
2020-04-06 11:47:34 +02:00
return Err(PERR::MalformedInExpr(
"'in' expression for a string expects a string, not a float".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
2020-04-06 11:47:34 +02:00
}
// 123 in "xxxx"
2020-11-13 03:43:54 +01:00
(x @ Expr::IntegerConstant(_, _), Expr::StringConstant(_, _)) => {
2020-04-06 11:47:34 +02:00
return Err(PERR::MalformedInExpr(
"'in' expression for a string expects a string, not a number".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
2020-04-06 11:47:34 +02:00
}
// (??? && ???) in "xxxx", (??? || ???) in "xxxx", (??? in ???) in "xxxx",
// true in "xxxx", false in "xxxx"
2020-11-13 03:43:54 +01:00
(x @ Expr::And(_, _), Expr::StringConstant(_, _))
| (x @ Expr::Or(_, _), Expr::StringConstant(_, _))
| (x @ Expr::In(_, _), Expr::StringConstant(_, _))
| (x @ Expr::BoolConstant(_, _), Expr::StringConstant(_, _)) => {
2020-04-06 11:47:34 +02:00
return Err(PERR::MalformedInExpr(
"'in' expression for a string expects a string, not a boolean".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
2020-04-06 11:47:34 +02:00
}
// [???, ???, ???] in "xxxx"
2020-11-13 03:43:54 +01:00
(x @ Expr::Array(_, _), Expr::StringConstant(_, _)) => {
2020-04-06 11:47:34 +02:00
return Err(PERR::MalformedInExpr(
"'in' expression for a string expects a string, not an array".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
2020-04-06 11:47:34 +02:00
}
// #{...} in "xxxx"
2020-11-13 03:43:54 +01:00
(x @ Expr::Map(_, _), Expr::StringConstant(_, _)) => {
2020-04-06 11:47:34 +02:00
return Err(PERR::MalformedInExpr(
"'in' expression for a string expects a string, not an object map".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
}
// () in "xxxx"
2020-11-13 03:43:54 +01:00
(x @ Expr::Unit(_), Expr::StringConstant(_, _)) => {
2020-04-06 11:47:34 +02:00
return Err(PERR::MalformedInExpr(
"'in' expression for a string expects a string, not ()".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
2020-04-06 11:47:34 +02:00
}
// "xxx" in #{...}, 'x' in #{...} - OK!
2020-11-13 03:43:54 +01:00
(Expr::StringConstant(_, _), Expr::Map(_, _))
2020-10-31 16:26:21 +01:00
| (Expr::CharConstant(_, _), Expr::Map(_, _)) => (),
2020-04-06 11:47:34 +02:00
// 123.456 in #{...}
#[cfg(not(feature = "no_float"))]
2020-10-31 16:26:21 +01:00
(x @ Expr::FloatConstant(_, _), Expr::Map(_, _)) => {
2020-04-06 11:47:34 +02:00
return Err(PERR::MalformedInExpr(
"'in' expression for an object map expects a string, not a float".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
2020-04-06 11:47:34 +02:00
}
// 123 in #{...}
2020-10-31 16:26:21 +01:00
(x @ Expr::IntegerConstant(_, _), Expr::Map(_, _)) => {
2020-04-06 11:47:34 +02:00
return Err(PERR::MalformedInExpr(
"'in' expression for an object map expects a string, not a number".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
2020-04-06 11:47:34 +02:00
}
// (??? && ???) in #{...}, (??? || ???) in #{...}, (??? in ???) in #{...},
// true in #{...}, false in #{...}
2020-10-31 16:26:21 +01:00
(x @ Expr::And(_, _), Expr::Map(_, _))
| (x @ Expr::Or(_, _), Expr::Map(_, _))
| (x @ Expr::In(_, _), Expr::Map(_, _))
| (x @ Expr::BoolConstant(_, _), Expr::Map(_, _)) => {
2020-04-06 11:47:34 +02:00
return Err(PERR::MalformedInExpr(
"'in' expression for an object map expects a string, not a boolean".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
2020-04-06 11:47:34 +02:00
}
// [???, ???, ???] in #{..}
2020-10-31 16:26:21 +01:00
(x @ Expr::Array(_, _), Expr::Map(_, _)) => {
2020-04-06 11:47:34 +02:00
return Err(PERR::MalformedInExpr(
"'in' expression for an object map expects a string, not an array".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
2020-04-06 11:47:34 +02:00
}
// #{...} in #{..}
2020-10-31 16:26:21 +01:00
(x @ Expr::Map(_, _), Expr::Map(_, _)) => {
2020-04-06 11:47:34 +02:00
return Err(PERR::MalformedInExpr(
"'in' expression for an object map expects a string, not an object map".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
}
// () in #{...}
2020-10-31 16:26:21 +01:00
(x @ Expr::Unit(_), Expr::Map(_, _)) => {
2020-04-06 11:47:34 +02:00
return Err(PERR::MalformedInExpr(
"'in' expression for an object map expects a string, not ()".into(),
)
2020-05-25 14:14:31 +02:00
.into_err(x.position()))
2020-04-06 11:47:34 +02:00
}
_ => (),
}
2020-10-31 16:26:21 +01:00
Ok(Expr::In(Box::new(BinaryExpr { lhs, rhs }), op_pos))
2020-04-06 11:47:34 +02:00
}
2020-03-18 03:36:50 +01:00
/// Parse a binary expression.
2020-06-11 12:13:33 +02:00
fn parse_binary_op(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
2020-03-14 04:51:45 +01:00
parent_precedence: u8,
2019-09-18 12:21:07 +02:00
lhs: Expr,
mut settings: ParseSettings,
) -> Result<Expr, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
settings.pos = lhs.position();
let mut root = lhs;
2016-02-29 22:43:45 +01:00
loop {
2020-10-25 14:57:18 +01:00
let (current_op, current_pos) = input.peek().unwrap();
2020-12-26 16:21:09 +01:00
let precedence = match current_op {
Token::Custom(c) => {
if state
.engine
.custom_keywords
.get(c)
.map_or(false, Option::is_some)
2020-12-26 16:21:09 +01:00
{
state.engine.custom_keywords.get(c).unwrap().unwrap().get()
} else {
return Err(PERR::Reserved(c.clone()).into_err(*current_pos));
}
2020-10-25 14:57:18 +01:00
}
2020-12-26 16:21:09 +01:00
Token::Reserved(c) if !is_valid_identifier(c.chars()) => {
return Err(PERR::UnknownOperator(c.into()).into_err(*current_pos))
}
_ => current_op.precedence(),
2020-10-25 14:57:18 +01:00
};
let bind_right = current_op.is_bind_right();
2020-03-14 04:51:45 +01:00
// 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);
2016-02-29 22:43:45 +01:00
}
let (op_token, pos) = input.next().unwrap();
let rhs = parse_unary(input, state, lib, settings)?;
2016-02-29 22:43:45 +01:00
2020-10-25 14:57:18 +01:00
let (next_op, next_pos) = input.peek().unwrap();
2020-12-26 16:21:09 +01:00
let next_precedence = match next_op {
Token::Custom(c) => {
if state
.engine
.custom_keywords
.get(c)
.map_or(false, Option::is_some)
2020-12-26 16:21:09 +01:00
{
state.engine.custom_keywords.get(c).unwrap().unwrap().get()
} else {
return Err(PERR::Reserved(c.clone()).into_err(*next_pos));
}
2020-10-25 14:57:18 +01:00
}
2020-12-26 16:21:09 +01:00
Token::Reserved(c) if !is_valid_identifier(c.chars()) => {
return Err(PERR::UnknownOperator(c.into()).into_err(*next_pos))
}
_ => next_op.precedence(),
2020-10-25 14:57:18 +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 {
parse_binary_op(input, state, lib, precedence, rhs, settings)?
} else {
// Otherwise bind to left (even if next operator has the same precedence)
rhs
};
settings = settings.level_up();
settings.pos = pos;
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
let op = op_token.syntax();
2020-10-31 07:13:45 +01:00
2020-11-10 16:26:50 +01:00
let op_base = FnCallExpr {
2020-10-31 07:13:45 +01:00
name: op,
capture: false,
..Default::default()
};
let mut args = StaticVec::new();
args.push(root);
args.push(rhs);
2020-04-22 11:37:06 +02:00
root = match op_token {
Token::Plus
| Token::Minus
| Token::Multiply
| Token::Divide
| Token::LeftShift
| Token::RightShift
| Token::Modulo
| Token::PowerOf
| Token::Ampersand
| Token::Pipe
2020-12-24 09:32:43 +01:00
| Token::XOr => Expr::FnCall(Box::new(FnCallExpr { args, ..op_base }), pos),
// '!=' defaults to true when passed invalid operands
Token::NotEqualsTo => Expr::FnCall(Box::new(FnCallExpr { args, ..op_base }), pos),
2020-04-28 17:05:03 +02:00
// Comparison operators default to false when passed invalid operands
Token::EqualsTo
| Token::LessThan
| Token::LessThanEqualsTo
| Token::GreaterThan
| Token::GreaterThanEqualsTo => {
Expr::FnCall(Box::new(FnCallExpr { args, ..op_base }), pos)
}
Token::Or => {
2020-07-29 10:10:06 +02:00
let rhs = args.pop().unwrap();
let current_lhs = args.pop().unwrap();
2020-10-31 16:26:21 +01:00
Expr::Or(
Box::new(BinaryExpr {
lhs: current_lhs,
rhs,
}),
2020-10-27 16:00:05 +01:00
pos,
2020-10-31 16:26:21 +01:00
)
}
Token::And => {
2020-07-29 10:10:06 +02:00
let rhs = args.pop().unwrap();
let current_lhs = args.pop().unwrap();
2020-10-31 16:26:21 +01:00
Expr::And(
Box::new(BinaryExpr {
lhs: current_lhs,
rhs,
}),
2020-10-27 16:00:05 +01:00
pos,
2020-10-31 16:26:21 +01:00
)
}
Token::In => {
2020-07-29 10:10:06 +02:00
let rhs = args.pop().unwrap();
let current_lhs = args.pop().unwrap();
make_in_expr(current_lhs, rhs, pos)?
}
2020-12-26 16:21:09 +01:00
Token::Custom(s)
if state
.engine
.custom_keywords
.get(&s)
.map_or(false, Option::is_some) =>
2020-12-26 16:21:09 +01:00
{
let hash_script = if is_valid_identifier(s.chars()) {
// Accept non-native functions for custom operators
calc_script_fn_hash(empty(), &s, 2)
} else {
None
};
2020-10-31 16:26:21 +01:00
Expr::FnCall(
2020-11-10 16:26:50 +01:00
Box::new(FnCallExpr {
2020-12-26 16:21:09 +01:00
hash_script,
2020-10-31 16:26:21 +01:00
args,
..op_base
}),
pos,
)
2020-07-05 11:41:45 +02:00
}
2020-06-14 08:25:47 +02:00
op_token => return Err(PERR::UnknownOperator(op_token.into()).into_err(pos)),
};
2016-02-29 22:43:45 +01:00
}
}
/// Parse a custom syntax.
2020-10-25 14:57:18 +01:00
fn parse_custom_syntax(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
key: &str,
syntax: &CustomSyntax,
pos: Position,
) -> Result<Expr, ParseError> {
let mut keywords: StaticVec<Expr> = Default::default();
let mut segments: StaticVec<_> = Default::default();
let mut tokens: Vec<_> = Default::default();
// Adjust the variables stack
match syntax.scope_delta {
delta if delta > 0 => {
// Add enough empty variable names to the stack.
// Empty variable names act as a barrier so earlier variables will not be matched.
// Variable searches stop at the first empty variable name.
state.stack.resize(
state.stack.len() + delta as usize,
("".into(), AccessMode::ReadWrite),
);
}
delta if delta < 0 && state.stack.len() <= delta.abs() as usize => state.stack.clear(),
delta if delta < 0 => state
.stack
.truncate(state.stack.len() - delta.abs() as usize),
_ => (),
}
2020-10-26 14:49:49 +01:00
let parse_func = &syntax.parse;
segments.push(key.into());
tokens.push(key.into());
2020-10-25 14:57:18 +01:00
loop {
let (fwd_token, fwd_pos) = input.peek().unwrap();
settings.pos = *fwd_pos;
let settings = settings.level_up();
let required_token = if let Some(seg) = parse_func(&segments, fwd_token.syntax().as_ref())
.map_err(|err| err.0.into_err(settings.pos))?
{
seg
} else {
break;
};
match required_token.as_str() {
MARKER_IDENT => match input.next().unwrap() {
(Token::Identifier(s), pos) => {
2020-12-22 09:45:56 +01:00
let name = state.get_interned_string(s);
segments.push(name.clone());
tokens.push(state.get_interned_string(MARKER_IDENT));
2020-12-22 09:45:56 +01:00
let var_name_def = Ident { name, pos };
2020-12-24 16:22:50 +01:00
keywords.push(Expr::Variable(Box::new((None, None, var_name_def))));
}
(Token::Reserved(s), pos) if is_valid_identifier(s.chars()) => {
return Err(PERR::Reserved(s).into_err(pos));
}
(_, pos) => return Err(PERR::VariableExpected.into_err(pos)),
},
2020-10-25 14:57:18 +01:00
MARKER_EXPR => {
keywords.push(parse_expr(input, state, lib, settings)?);
let keyword = state.get_interned_string(MARKER_EXPR);
segments.push(keyword.clone());
tokens.push(keyword);
2020-10-25 14:57:18 +01:00
}
2020-11-04 04:49:02 +01:00
MARKER_BLOCK => match parse_block(input, state, lib, settings)? {
Stmt::Block(statements, pos) => {
keywords.push(Expr::Stmt(Box::new(statements.into()), pos));
let keyword = state.get_interned_string(MARKER_BLOCK);
segments.push(keyword.clone());
tokens.push(keyword);
2020-11-04 04:49:02 +01:00
}
stmt => unreachable!("expecting Stmt::Block, but gets {:?}", stmt),
2020-11-04 04:49:02 +01:00
},
2020-10-26 14:49:49 +01:00
s => match input.next().unwrap() {
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
(t, _) if t.syntax().as_ref() == s => {
segments.push(required_token.clone());
tokens.push(required_token.clone());
}
(_, pos) => {
return Err(PERR::MissingToken(
s.to_string(),
2020-10-25 14:57:18 +01:00
format!("for '{}' expression", segments[0]),
)
2020-10-26 14:49:49 +01:00
.into_err(pos))
}
},
}
}
2021-01-12 16:52:50 +01:00
Ok(Expr::Custom(
Box::new(CustomExpr {
keywords,
tokens,
scope_delta: syntax.scope_delta,
}),
pos,
))
}
2020-03-18 03:36:50 +01:00
/// Parse an expression.
2020-06-11 12:13:33 +02:00
fn parse_expr(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
) -> Result<Expr, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
settings.pos = input.peek().unwrap().1;
2020-07-09 13:54:28 +02:00
// Check if it is a custom syntax.
2020-10-25 14:57:18 +01:00
if !state.engine.custom_syntax.is_empty() {
2020-07-09 13:54:28 +02:00
let (token, pos) = input.peek().unwrap();
let token_pos = *pos;
match token {
2020-10-25 14:57:18 +01:00
Token::Custom(key) | Token::Reserved(key) | Token::Identifier(key) => {
match state.engine.custom_syntax.get_key_value(key) {
Some((key, syntax)) => {
input.next().unwrap();
return parse_custom_syntax(
input, state, lib, settings, key, syntax, token_pos,
);
}
_ => (),
}
2020-07-09 13:54:28 +02:00
}
_ => (),
}
}
// Parse expression normally.
let lhs = parse_unary(input, state, lib, settings.level_up())?;
parse_binary_op(input, state, lib, 1, lhs, settings.level_up())
2016-02-29 22:43:45 +01:00
}
2020-04-22 11:37:06 +02:00
/// Make sure that the expression is not a statement expression (i.e. wrapped in `{}`).
2020-06-11 12:13:33 +02:00
fn ensure_not_statement_expr(input: &mut TokenStream, type_name: &str) -> Result<(), ParseError> {
match input.peek().unwrap() {
// Disallow statement expressions
(Token::LeftBrace, pos) | (Token::EOF, pos) => {
Err(PERR::ExprExpected(type_name.to_string()).into_err(*pos))
}
// No need to check for others at this time - leave it for the expr parser
_ => Ok(()),
}
}
2020-04-22 11:37:06 +02:00
/// Make sure that the expression is not a mis-typed assignment (i.e. `a = b` instead of `a == b`).
2020-06-11 12:13:33 +02:00
fn ensure_not_assignment(input: &mut TokenStream) -> Result<(), ParseError> {
2020-04-22 11:37:06 +02:00
match input.peek().unwrap() {
2020-12-22 04:55:51 +01:00
(Token::Equals, pos) => Err(LexError::ImproperSymbol(
"=".to_string(),
2020-11-02 05:50:27 +01:00
"Possibly a typo of '=='?".to_string(),
2020-12-22 04:55:51 +01:00
)
2020-11-02 05:50:27 +01:00
.into_err(*pos)),
(token @ Token::PlusAssign, pos)
| (token @ Token::MinusAssign, pos)
| (token @ Token::MultiplyAssign, pos)
| (token @ Token::DivideAssign, pos)
| (token @ Token::LeftShiftAssign, pos)
| (token @ Token::RightShiftAssign, pos)
| (token @ Token::ModuloAssign, pos)
| (token @ Token::PowerOfAssign, pos)
| (token @ Token::AndAssign, pos)
| (token @ Token::OrAssign, pos)
2020-12-22 04:55:51 +01:00
| (token @ Token::XOrAssign, pos) => Err(LexError::ImproperSymbol(
token.syntax().to_string(),
"Expecting a boolean expression, not an assignment".to_string(),
2020-12-22 04:55:51 +01:00
)
.into_err(*pos)),
2020-04-22 11:37:06 +02:00
_ => Ok(()),
}
}
2020-03-18 03:36:50 +01:00
/// Parse an if statement.
2020-06-11 12:13:33 +02:00
fn parse_if(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
) -> Result<Stmt, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2016-02-29 22:43:45 +01:00
2020-12-28 02:49:54 +01:00
// if ...
settings.pos = eat_token(input, Token::If);
// if guard { if_body }
ensure_not_statement_expr(input, "a boolean")?;
let guard = parse_expr(input, state, lib, settings.level_up())?;
2020-04-22 11:37:06 +02:00
ensure_not_assignment(input)?;
2020-10-27 12:23:43 +01:00
let if_body = parse_block(input, state, lib, settings.level_up())?;
2016-02-29 22:43:45 +01:00
// if guard { if_body } else ...
2020-10-20 17:16:03 +02:00
let else_body = if match_token(input, Token::Else).0 {
2020-10-27 12:23:43 +01:00
Some(if let (Token::If, _) = input.peek().unwrap() {
// if guard { if_body } else if ...
parse_if(input, state, lib, settings.level_up())?
2020-03-16 16:51:32 +01:00
} else {
// if guard { if_body } else { else-body }
parse_block(input, state, lib, settings.level_up())?
2020-10-27 12:23:43 +01:00
})
2020-03-16 16:51:32 +01:00
} else {
None
};
2020-03-02 10:04:56 +01:00
2020-12-28 02:49:54 +01:00
Ok(Stmt::If(
guard,
Box::new((if_body, else_body)),
settings.pos,
))
2016-02-29 22:43:45 +01:00
}
2020-03-18 03:36:50 +01:00
/// Parse a while loop.
2020-11-20 15:23:37 +01:00
fn parse_while_loop(
2020-06-11 12:13:33 +02:00
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
) -> Result<Stmt, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2016-02-29 22:43:45 +01:00
2020-11-20 15:23:37 +01:00
// while|loops ...
let (guard, token_pos) = match input.next().unwrap() {
(Token::While, pos) => {
ensure_not_statement_expr(input, "a boolean")?;
(parse_expr(input, state, lib, settings.level_up())?, pos)
}
(Token::Loop, pos) => (Expr::BoolConstant(true, pos), pos),
(t, _) => unreachable!("expecting Token::While or Token::Loop, but gets {:?}", t),
2020-11-20 15:23:37 +01:00
};
settings.pos = token_pos;
2020-11-20 15:23:37 +01:00
ensure_not_assignment(input)?;
settings.is_breakable = true;
2020-10-27 11:18:19 +01:00
let body = Box::new(parse_block(input, state, lib, settings.level_up())?);
2016-02-29 22:43:45 +01:00
2020-12-28 02:49:54 +01:00
Ok(Stmt::While(guard, body, settings.pos))
2016-02-29 22:43:45 +01:00
}
2020-11-20 15:23:37 +01:00
/// Parse a do loop.
fn parse_do(
2020-06-11 12:13:33 +02:00
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
) -> Result<Stmt, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2017-10-30 16:08:44 +01:00
2020-12-28 02:49:54 +01:00
// do ...
settings.pos = eat_token(input, Token::Do);
2020-11-20 15:23:37 +01:00
// do { body } [while|until] guard
settings.is_breakable = true;
2020-10-27 11:18:19 +01:00
let body = Box::new(parse_block(input, state, lib, settings.level_up())?);
2017-10-30 16:08:44 +01:00
2020-11-20 15:23:37 +01:00
let is_while = match input.next().unwrap() {
(Token::While, _) => true,
(Token::Until, _) => false,
(_, pos) => {
return Err(
PERR::MissingToken(Token::While.into(), "for the do statement".into())
.into_err(pos),
)
}
};
ensure_not_statement_expr(input, "a boolean")?;
settings.is_breakable = false;
let guard = parse_expr(input, state, lib, settings.level_up())?;
ensure_not_assignment(input)?;
2020-12-28 02:49:54 +01:00
Ok(Stmt::Do(body, guard, is_while, settings.pos))
2017-10-30 16:08:44 +01:00
}
2020-03-18 03:36:50 +01:00
/// Parse a for loop.
2020-06-11 12:13:33 +02:00
fn parse_for(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
) -> Result<Stmt, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
// for ...
settings.pos = eat_token(input, Token::For);
2020-03-18 11:41:18 +01:00
// for name ...
let name = match input.next().unwrap() {
2020-03-18 11:41:18 +01:00
// Variable name
2020-03-16 16:51:32 +01:00
(Token::Identifier(s), _) => s,
// Reserved keyword
(Token::Reserved(s), pos) if is_valid_identifier(s.chars()) => {
return Err(PERR::Reserved(s).into_err(pos));
}
2020-03-18 11:41:18 +01:00
// Bad identifier
2020-06-14 10:56:36 +02:00
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
2020-03-18 11:41:18 +01:00
// Not a variable name
2020-03-24 09:46:47 +01:00
(_, pos) => return Err(PERR::VariableExpected.into_err(pos)),
};
2020-03-18 11:41:18 +01:00
// for name in ...
match input.next().unwrap() {
(Token::In, _) => (),
2020-06-14 10:56:36 +02:00
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
(_, pos) => {
return Err(
2020-05-04 13:36:58 +02:00
PERR::MissingToken(Token::In.into(), "after the iteration variable".into())
.into_err(pos),
)
}
}
2020-03-18 11:41:18 +01:00
// for name in expr { body }
ensure_not_statement_expr(input, "a boolean")?;
let expr = parse_expr(input, state, lib, settings.level_up())?;
2020-04-28 17:05:03 +02:00
2020-11-13 03:43:54 +01:00
let loop_var = state.get_interned_string(name.clone());
2020-06-28 09:49:24 +02:00
let prev_stack_len = state.stack.len();
state.stack.push((loop_var, AccessMode::ReadWrite));
2020-04-28 17:05:03 +02:00
settings.is_breakable = true;
2020-10-27 12:23:43 +01:00
let body = parse_block(input, state, lib, settings.level_up())?;
2020-04-28 17:05:03 +02:00
2020-06-28 09:49:24 +02:00
state.stack.truncate(prev_stack_len);
2020-12-28 02:49:54 +01:00
Ok(Stmt::For(expr, Box::new((name, body)), settings.pos))
}
2020-03-18 03:36:50 +01:00
/// Parse a variable definition statement.
2020-06-11 12:13:33 +02:00
fn parse_let(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
var_type: AccessMode,
export: bool,
mut settings: ParseSettings,
) -> Result<Stmt, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-03-16 16:51:32 +01:00
2020-12-28 02:49:54 +01:00
// let/const... (specified in `var_type`)
settings.pos = input.next().unwrap().1;
2020-03-18 11:41:18 +01:00
// let name ...
let (name, pos) = match input.next().unwrap() {
2020-03-18 11:41:18 +01:00
(Token::Identifier(s), pos) => (s, pos),
(Token::Reserved(s), pos) if is_valid_identifier(s.chars()) => {
return Err(PERR::Reserved(s).into_err(pos));
}
2020-06-14 10:56:36 +02:00
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
2020-03-24 09:46:47 +01:00
(_, pos) => return Err(PERR::VariableExpected.into_err(pos)),
2016-02-29 22:43:45 +01:00
};
2021-02-03 12:14:26 +01:00
let name = state.get_interned_string(name);
let var_def = Ident {
name: name.clone(),
pos,
};
2020-03-18 11:41:18 +01:00
// let name = ...
2020-12-28 02:49:54 +01:00
let expr = if match_token(input, Token::Equals).0 {
2020-03-18 11:41:18 +01:00
// let name = expr
2020-10-09 05:15:25 +02:00
Some(parse_expr(input, state, lib, settings.level_up())?)
2020-03-14 16:41:15 +01:00
} else {
2020-10-09 05:15:25 +02:00
None
};
2021-02-03 12:14:26 +01:00
state.stack.push((name, var_type));
2020-10-09 05:15:25 +02:00
match var_type {
// let name = expr
2021-02-03 12:14:26 +01:00
AccessMode::ReadWrite => Ok(Stmt::Let(Box::new(var_def), expr, export, settings.pos)),
2020-10-09 05:15:25 +02:00
// const name = { expr:constant }
2021-02-03 12:14:26 +01:00
AccessMode::ReadOnly => Ok(Stmt::Const(Box::new(var_def), expr, export, settings.pos)),
2016-02-29 22:43:45 +01:00
}
}
2020-05-04 13:36:58 +02:00
/// Parse an import statement.
2020-07-01 16:21:43 +02:00
#[cfg(not(feature = "no_module"))]
2020-06-11 12:13:33 +02:00
fn parse_import(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
) -> Result<Stmt, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
// import ...
settings.pos = eat_token(input, Token::Import);
2020-05-04 13:36:58 +02:00
// import expr ...
let expr = parse_expr(input, state, lib, settings.level_up())?;
2020-05-04 13:36:58 +02:00
// import expr as ...
2020-10-20 17:16:03 +02:00
if !match_token(input, Token::As).0 {
2020-12-28 02:49:54 +01:00
return Ok(Stmt::Import(expr, None, settings.pos));
2020-05-04 13:36:58 +02:00
}
// import expr as name ...
2020-12-28 02:49:54 +01:00
let (name, name_pos) = match input.next().unwrap() {
2020-05-04 13:36:58 +02:00
(Token::Identifier(s), pos) => (s, pos),
(Token::Reserved(s), pos) if is_valid_identifier(s.chars()) => {
return Err(PERR::Reserved(s).into_err(pos));
}
2020-06-14 10:56:36 +02:00
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
2020-05-04 13:36:58 +02:00
(_, pos) => return Err(PERR::VariableExpected.into_err(pos)),
};
2020-11-13 03:43:54 +01:00
let name = state.get_interned_string(name);
state.modules.push(name.clone());
2020-10-27 11:18:19 +01:00
Ok(Stmt::Import(
expr,
2020-12-22 09:45:56 +01:00
Some(Box::new(Ident {
name,
2020-12-28 02:49:54 +01:00
pos: name_pos,
2020-12-22 09:45:56 +01:00
})),
2020-12-28 02:49:54 +01:00
settings.pos,
2020-10-27 11:18:19 +01:00
))
2020-05-04 13:36:58 +02:00
}
2020-05-08 10:49:24 +02:00
/// Parse an export statement.
#[cfg(not(feature = "no_module"))]
2020-06-11 12:13:33 +02:00
fn parse_export(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
) -> Result<Stmt, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
settings.pos = eat_token(input, Token::Export);
match input.peek().unwrap() {
(Token::Let, pos) => {
let pos = *pos;
let mut stmt = 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 = parse_let(input, state, lib, AccessMode::ReadOnly, true, settings)?;
stmt.set_position(pos);
return Ok(stmt);
}
_ => (),
}
2020-05-08 10:49:24 +02:00
2020-11-15 06:49:54 +01:00
let mut exports = Vec::with_capacity(4);
2020-05-08 10:49:24 +02:00
loop {
let (id, id_pos) = match input.next().unwrap() {
(Token::Identifier(s), pos) => (s.clone(), pos),
(Token::Reserved(s), pos) if is_valid_identifier(s.chars()) => {
return Err(PERR::Reserved(s).into_err(pos));
}
2020-06-14 10:56:36 +02:00
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
2020-05-08 10:49:24 +02:00
(_, pos) => return Err(PERR::VariableExpected.into_err(pos)),
};
2020-10-20 17:16:03 +02:00
let rename = if match_token(input, Token::As).0 {
2020-05-08 10:49:24 +02:00
match input.next().unwrap() {
2020-12-22 09:45:56 +01:00
(Token::Identifier(s), pos) => Some(Ident {
name: state.get_interned_string(s),
pos,
}),
(Token::Reserved(s), pos) if is_valid_identifier(s.chars()) => {
return Err(PERR::Reserved(s).into_err(pos));
}
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
2020-05-08 10:49:24 +02:00
(_, pos) => return Err(PERR::VariableExpected.into_err(pos)),
}
} else {
None
};
2020-12-22 09:45:56 +01:00
exports.push((
Ident {
name: state.get_interned_string(id),
pos: id_pos,
},
rename,
));
2020-05-08 10:49:24 +02:00
match input.peek().unwrap() {
(Token::Comma, _) => {
eat_token(input, Token::Comma);
}
(Token::Identifier(_), pos) => {
return Err(PERR::MissingToken(
Token::Comma.into(),
"to separate the list of exports".into(),
)
.into_err(*pos))
}
_ => break,
}
}
2020-12-28 02:49:54 +01:00
Ok(Stmt::Export(exports, settings.pos))
2020-05-08 10:49:24 +02:00
}
2020-03-18 03:36:50 +01:00
/// Parse a statement block.
2020-06-11 12:13:33 +02:00
fn parse_block(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
) -> Result<Stmt, ParseError> {
2020-03-18 11:41:18 +01:00
// Must start with {
settings.pos = match input.next().unwrap() {
2020-03-16 16:51:32 +01:00
(Token::LeftBrace, pos) => pos,
2020-06-14 10:56:36 +02:00
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
(_, pos) => {
2020-05-04 13:36:58 +02:00
return Err(PERR::MissingToken(
Token::LeftBrace.into(),
"to start a statement block".into(),
)
2020-05-04 13:36:58 +02:00
.into_err(pos))
}
2020-03-12 05:40:28 +01:00
};
2016-02-29 22:43:45 +01:00
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-11-15 06:49:54 +01:00
let mut statements = Vec::with_capacity(8);
let prev_entry_stack_len = state.entry_stack_len;
state.entry_stack_len = state.stack.len();
2020-10-18 16:10:08 +02:00
#[cfg(not(feature = "no_module"))]
2020-06-28 09:49:24 +02:00
let prev_mods_len = state.modules.len();
2020-10-20 17:16:03 +02:00
while !match_token(input, Token::RightBrace).0 {
2020-03-17 10:33:37 +01:00
// Parse statements inside the block
settings.is_global = false;
2020-12-29 03:41:20 +01:00
let stmt = parse_stmt(input, state, lib, settings.level_up())?;
if stmt.is_noop() {
continue;
}
2020-03-17 10:33:37 +01:00
// See if it needs a terminating semicolon
let need_semicolon = !stmt.is_self_terminated();
2017-10-02 23:44:45 +02:00
2020-03-17 10:33:37 +01:00
statements.push(stmt);
match input.peek().unwrap() {
2020-03-18 11:41:18 +01:00
// { ... stmt }
(Token::RightBrace, _) => {
eat_token(input, Token::RightBrace);
break;
}
2020-03-18 11:41:18 +01:00
// { ... stmt;
(Token::SemiColon, _) if need_semicolon => {
eat_token(input, Token::SemiColon);
2020-03-17 10:33:37 +01:00
}
2020-03-18 11:41:18 +01:00
// { ... { stmt } ;
(Token::SemiColon, _) if !need_semicolon => (),
2020-03-18 11:41:18 +01:00
// { ... { stmt } ???
(_, _) if !need_semicolon => (),
// { ... stmt <error>
2020-11-25 02:36:06 +01:00
(Token::LexError(err), pos) => return Err(err.clone().into_err(*pos)),
// { ... stmt ???
(_, pos) => {
2020-03-17 10:33:37 +01:00
// Semicolons are not optional between statements
2020-05-04 13:36:58 +02:00
return Err(PERR::MissingToken(
Token::SemiColon.into(),
"to terminate this statement".into(),
)
.into_err(*pos));
2019-09-18 12:21:07 +02:00
}
}
}
2016-02-29 22:43:45 +01:00
state.stack.truncate(state.entry_stack_len);
state.entry_stack_len = prev_entry_stack_len;
2020-10-18 16:10:08 +02:00
#[cfg(not(feature = "no_module"))]
2020-06-28 09:49:24 +02:00
state.modules.truncate(prev_mods_len);
2020-04-28 17:05:03 +02:00
2020-10-27 11:18:19 +01:00
Ok(Stmt::Block(statements, settings.pos))
2016-02-29 22:43:45 +01:00
}
2020-03-18 03:36:50 +01:00
/// Parse an expression as a statement.
2020-06-11 12:13:33 +02:00
fn parse_expr_stmt(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
) -> Result<Stmt, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
settings.pos = input.peek().unwrap().1;
let expr = parse_expr(input, state, lib, settings.level_up())?;
2020-10-27 16:21:20 +01:00
let stmt = parse_op_assignment_stmt(input, state, lib, expr, settings.level_up())?;
Ok(stmt)
2016-02-29 22:43:45 +01:00
}
2020-03-18 03:36:50 +01:00
/// Parse a single statement.
2020-06-11 12:13:33 +02:00
fn parse_stmt(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
2020-12-29 03:41:20 +01:00
) -> Result<Stmt, ParseError> {
use AccessMode::{ReadOnly, ReadWrite};
2020-12-29 05:29:45 +01:00
let mut _comments: Vec<String> = Default::default();
2020-12-12 13:09:29 +01:00
#[cfg(not(feature = "no_function"))]
2020-12-29 05:29:45 +01:00
{
let mut comments_pos = Position::NONE;
2020-12-12 13:09:29 +01:00
2020-12-29 05:29:45 +01:00
// Handle doc-comments.
while let (Token::Comment(ref comment), pos) = input.peek().unwrap() {
if comments_pos.is_none() {
comments_pos = *pos;
}
2020-12-12 13:09:29 +01:00
2020-12-29 05:29:45 +01:00
if !crate::token::is_doc_comment(comment) {
unreachable!("expecting doc-comment, but gets {:?}", comment);
}
if !settings.is_global {
return Err(PERR::WrongDocComment.into_err(comments_pos));
}
2020-12-12 13:09:29 +01:00
2020-12-29 05:29:45 +01:00
match input.next().unwrap().0 {
Token::Comment(comment) => {
_comments.push(comment);
2020-12-12 13:09:29 +01:00
2020-12-29 05:29:45 +01:00
match input.peek().unwrap() {
(Token::Fn, _) | (Token::Private, _) => break,
(Token::Comment(_), _) => (),
_ => return Err(PERR::WrongDocComment.into_err(comments_pos)),
}
}
2020-12-29 05:29:45 +01:00
t => unreachable!("expecting Token::Comment, but gets {:?}", t),
2020-12-12 13:09:29 +01:00
}
}
}
let (token, token_pos) = match input.peek().unwrap() {
2020-12-29 03:41:20 +01:00
(Token::EOF, pos) => return Ok(Stmt::Noop(*pos)),
x => x,
};
settings.pos = *token_pos;
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
match token {
// ; - empty statement
2021-02-09 07:08:17 +01:00
Token::SemiColon => {
eat_token(input, Token::SemiColon);
Ok(Stmt::Noop(settings.pos))
}
2020-03-18 11:41:18 +01:00
// { - statements block
2020-12-29 03:41:20 +01:00
Token::LeftBrace => Ok(parse_block(input, state, lib, settings.level_up())?),
2020-04-01 10:22:18 +02:00
2020-03-18 11:41:18 +01:00
// fn ...
#[cfg(not(feature = "no_function"))]
Token::Fn if !settings.is_global => Err(PERR::FnWrongDefinition.into_err(settings.pos)),
#[cfg(not(feature = "no_function"))]
Token::Fn | Token::Private => {
let access = if matches!(token, Token::Private) {
eat_token(input, Token::Private);
2020-11-17 05:23:53 +01:00
FnAccess::Private
} else {
2020-11-17 05:23:53 +01:00
FnAccess::Public
};
match input.next().unwrap() {
(Token::Fn, pos) => {
2020-07-30 07:28:06 +02:00
let mut new_state = ParseState::new(
state.engine,
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
state.max_function_expr_depth,
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
state.max_function_expr_depth,
);
let settings = ParseSettings {
allow_if_expr: true,
2020-11-14 16:43:36 +01:00
allow_switch_expr: true,
allow_stmt_expr: true,
allow_anonymous_fn: true,
is_global: false,
2020-07-16 06:09:31 +02:00
is_function_scope: true,
is_breakable: false,
level: 0,
pos: pos,
};
2020-12-29 05:29:45 +01:00
let func = parse_fn(input, &mut new_state, lib, access, settings, _comments)?;
let hash = calc_script_fn_hash(empty(), &func.name, func.params.len()).unwrap();
if lib.contains_key(&hash) {
return Err(PERR::FnDuplicatedDefinition(
func.name.into_owned(),
func.params.len(),
)
.into_err(pos));
}
lib.insert(hash, func);
Ok(Stmt::Noop(pos))
}
(_, pos) => Err(PERR::MissingToken(
Token::Fn.into(),
format!("following '{}'", Token::Private.syntax()),
)
.into_err(pos)),
}
}
2020-12-29 03:41:20 +01:00
Token::If => parse_if(input, state, lib, settings.level_up()),
Token::Switch => parse_switch(input, state, lib, settings.level_up()),
Token::While | Token::Loop => parse_while_loop(input, state, lib, settings.level_up()),
Token::Do => parse_do(input, state, lib, settings.level_up()),
Token::For => parse_for(input, state, lib, settings.level_up()),
2020-04-01 10:22:18 +02:00
Token::Continue if settings.is_breakable => {
2020-04-17 13:00:52 +02:00
let pos = eat_token(input, Token::Continue);
2020-12-29 03:41:20 +01:00
Ok(Stmt::Continue(pos))
2020-04-01 10:22:18 +02:00
}
Token::Break if settings.is_breakable => {
2020-04-17 13:00:52 +02:00
let pos = eat_token(input, Token::Break);
2020-12-29 03:41:20 +01:00
Ok(Stmt::Break(pos))
}
Token::Continue | Token::Break => Err(PERR::LoopBreak.into_err(settings.pos)),
2020-04-01 10:22:18 +02:00
Token::Return | Token::Throw => {
let (return_type, token_pos) = input
.next()
.map(|(token, pos)| {
(
match token {
Token::Return => ReturnType::Return,
Token::Throw => ReturnType::Exception,
t => unreachable!(
"expecting Token::Return or Token::Throw, but gets {:?}",
t
),
},
pos,
)
})
.unwrap();
2020-03-03 11:15:20 +01:00
match input.peek().unwrap() {
// `return`/`throw` at <EOF>
2020-12-29 03:41:20 +01:00
(Token::EOF, pos) => Ok(Stmt::Return((return_type, token_pos), None, *pos)),
2020-03-18 03:36:50 +01:00
// `return;` or `throw;`
2020-12-29 03:41:20 +01:00
(Token::SemiColon, _) => {
Ok(Stmt::Return((return_type, token_pos), None, settings.pos))
}
2020-03-18 03:36:50 +01:00
// `return` or `throw` with expression
(_, _) => {
let expr = parse_expr(input, state, lib, settings.level_up())?;
2020-03-19 12:53:42 +01:00
let pos = expr.position();
2020-12-29 03:41:20 +01:00
Ok(Stmt::Return((return_type, token_pos), Some(expr), pos))
}
}
}
2020-04-01 10:22:18 +02:00
2020-12-29 03:41:20 +01:00
Token::Try => parse_try_catch(input, state, lib, settings.level_up()),
2020-10-20 17:16:03 +02:00
2020-12-29 03:41:20 +01:00
Token::Let => parse_let(input, state, lib, ReadWrite, false, settings.level_up()),
Token::Const => parse_let(input, state, lib, ReadOnly, false, settings.level_up()),
2020-07-01 16:21:43 +02:00
#[cfg(not(feature = "no_module"))]
2020-12-29 03:41:20 +01:00
Token::Import => parse_import(input, state, lib, settings.level_up()),
2020-05-04 13:36:58 +02:00
2020-05-08 10:49:24 +02:00
#[cfg(not(feature = "no_module"))]
Token::Export if !settings.is_global => Err(PERR::WrongExport.into_err(settings.pos)),
2020-05-08 10:49:24 +02:00
#[cfg(not(feature = "no_module"))]
2020-12-29 03:41:20 +01:00
Token::Export => parse_export(input, state, lib, settings.level_up()),
2020-05-08 10:49:24 +02:00
2020-12-29 03:41:20 +01:00
_ => parse_expr_stmt(input, state, lib, settings.level_up()),
2016-02-29 22:43:45 +01:00
}
}
2020-10-20 17:16:03 +02:00
/// Parse a try/catch statement.
fn parse_try_catch(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
) -> Result<Stmt, ParseError> {
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-12-28 02:49:54 +01:00
// try ...
settings.pos = eat_token(input, Token::Try);
2020-10-20 17:16:03 +02:00
// try { body }
let body = parse_block(input, state, lib, settings.level_up())?;
// try { body } catch
let (matched, catch_pos) = match_token(input, Token::Catch);
if !matched {
return Err(
PERR::MissingToken(Token::Catch.into(), "for the 'try' statement".into())
.into_err(catch_pos),
);
}
// try { body } catch (
let var_def = if match_token(input, Token::LeftParen).0 {
let id = match input.next().unwrap() {
2020-12-22 09:45:56 +01:00
(Token::Identifier(s), pos) => Ident {
name: state.get_interned_string(s),
pos,
},
2020-10-20 17:16:03 +02:00
(_, pos) => return Err(PERR::VariableExpected.into_err(pos)),
};
let (matched, pos) = match_token(input, Token::RightParen);
if !matched {
return Err(PERR::MissingToken(
Token::RightParen.into(),
"to enclose the catch variable".into(),
)
.into_err(pos));
}
Some(id)
} else {
None
};
// try { body } catch ( var ) { catch_block }
let catch_body = parse_block(input, state, lib, settings.level_up())?;
2020-11-06 09:27:40 +01:00
Ok(Stmt::TryCatch(
Box::new((body, var_def, catch_body)),
2020-12-28 02:49:54 +01:00
settings.pos,
2020-11-06 09:27:40 +01:00
catch_pos,
))
2020-10-20 17:16:03 +02:00
}
2020-03-18 03:36:50 +01:00
/// Parse a function definition.
#[cfg(not(feature = "no_function"))]
2020-06-11 12:13:33 +02:00
fn parse_fn(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
2020-11-17 05:23:53 +01:00
access: FnAccess,
mut settings: ParseSettings,
comments: Vec<String>,
) -> Result<ScriptFnDef, ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-07-26 16:25:30 +02:00
let (token, pos) = input.next().unwrap();
2020-07-30 12:18:28 +02:00
let name = token
.into_function_name_for_override()
.map_err(|t| match t {
Token::Reserved(s) => PERR::Reserved(s).into_err(pos),
_ => PERR::FnMissingName.into_err(pos),
})?;
match input.peek().unwrap() {
(Token::LeftParen, _) => eat_token(input, Token::LeftParen),
(_, pos) => return Err(PERR::FnMissingParams(name).into_err(*pos)),
2020-04-06 11:47:34 +02:00
};
2020-11-15 06:49:54 +01:00
let mut params: StaticVec<_> = Default::default();
2020-10-20 17:16:03 +02:00
if !match_token(input, Token::RightParen).0 {
let sep_err = format!("to separate the parameters of function '{}'", name);
2020-03-24 09:46:47 +01:00
2020-03-14 16:41:15 +01:00
loop {
2020-07-19 11:14:55 +02:00
match input.next().unwrap() {
(Token::RightParen, _) => break,
(Token::Identifier(s), pos) => {
2020-11-13 11:32:18 +01:00
if params.iter().any(|(p, _)| p == &s) {
2020-11-13 12:35:51 +01:00
return Err(PERR::FnDuplicatedParam(name, s).into_err(pos));
2020-11-13 11:32:18 +01:00
}
2020-11-13 03:43:54 +01:00
let s = state.get_interned_string(s);
state.stack.push((s.clone(), AccessMode::ReadWrite));
2020-07-19 11:14:55 +02:00
params.push((s, pos))
}
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
(_, pos) => {
return Err(PERR::MissingToken(
Token::RightParen.into(),
format!("to close the parameters list of function '{}'", name),
)
.into_err(pos))
}
2020-03-16 16:51:32 +01:00
}
match input.next().unwrap() {
(Token::RightParen, _) => break,
(Token::Comma, _) => (),
2020-06-14 10:56:36 +02:00
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
2020-05-04 13:36:58 +02:00
(_, pos) => {
return Err(PERR::MissingToken(Token::Comma.into(), sep_err).into_err(pos))
}
}
2020-03-14 16:41:15 +01:00
}
2016-02-29 22:43:45 +01:00
}
// Parse function body
let body = match input.peek().unwrap() {
(Token::LeftBrace, _) => {
settings.is_breakable = false;
parse_block(input, state, lib, settings.level_up())?
}
(_, pos) => return Err(PERR::FnMissingBody(name).into_err(*pos)),
};
2020-07-30 07:28:06 +02:00
let params: StaticVec<_> = params.into_iter().map(|(p, _)| p).collect();
2020-08-03 06:10:20 +02:00
#[cfg(not(feature = "no_closure"))]
2020-11-15 05:07:35 +01:00
let externals = state
2020-07-30 07:28:06 +02:00
.externals
.iter()
.map(|(name, _)| name)
.filter(|name| !params.contains(name))
.cloned()
.collect();
2020-04-06 11:47:34 +02:00
Ok(ScriptFnDef {
name: name.into(),
access,
2020-04-06 11:47:34 +02:00
params,
2020-08-03 06:10:20 +02:00
#[cfg(not(feature = "no_closure"))]
2020-11-09 14:52:23 +01:00
externals,
2020-03-09 14:57:07 +01:00
body,
lib: None,
2020-11-09 14:52:23 +01:00
#[cfg(not(feature = "no_module"))]
mods: Default::default(),
comments,
})
}
2020-07-29 16:43:50 +02:00
/// Creates a curried expression from a list of external variables
2020-08-05 16:53:01 +02:00
#[cfg(not(feature = "no_function"))]
2021-01-25 04:31:54 +01:00
#[cfg(not(feature = "no_closure"))]
2020-12-22 09:45:56 +01:00
fn make_curry_from_externals(fn_expr: Expr, externals: StaticVec<Ident>, pos: Position) -> Expr {
2021-01-25 04:31:54 +01:00
// If there are no captured variables, no need to curry
2020-07-30 07:28:06 +02:00
if externals.is_empty() {
2020-07-29 16:43:50 +02:00
return fn_expr;
}
2020-07-30 07:28:06 +02:00
let num_externals = externals.len();
2020-07-29 16:43:50 +02:00
let mut args: StaticVec<_> = Default::default();
args.push(fn_expr);
2020-10-28 12:11:17 +01:00
externals.iter().for_each(|x| {
2021-01-25 04:31:54 +01:00
args.push(Expr::Variable(Box::new((None, None, x.clone()))));
2020-07-29 16:43:50 +02:00
});
let curry_func = crate::engine::KEYWORD_FN_PTR_CURRY;
2020-12-24 09:32:43 +01:00
let hash_script = calc_script_fn_hash(empty(), curry_func, num_externals + 1);
2020-07-29 16:43:50 +02:00
2020-10-31 16:26:21 +01:00
let expr = Expr::FnCall(
2020-11-10 16:26:50 +01:00
Box::new(FnCallExpr {
name: curry_func.into(),
2020-12-24 09:32:43 +01:00
hash_script,
2020-10-31 16:26:21 +01:00
args,
..Default::default()
}),
2020-10-31 07:13:45 +01:00
pos,
2020-10-31 16:26:21 +01:00
);
2020-07-29 16:43:50 +02:00
2021-01-25 04:31:54 +01:00
// Convert the entire expression into a statement block, then insert the relevant
// [`Share`][Stmt::Share] statements.
let mut statements: StaticVec<_> = Default::default();
statements.extend(externals.into_iter().map(Stmt::Share));
statements.push(Stmt::Expr(expr));
Expr::Stmt(Box::new(statements), pos)
2020-07-29 16:43:50 +02:00
}
2020-07-19 11:14:55 +02:00
/// Parse an anonymous function definition.
#[cfg(not(feature = "no_function"))]
fn parse_anon_fn(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
mut settings: ParseSettings,
) -> Result<(Expr, ScriptFnDef), ParseError> {
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
2020-07-19 11:14:55 +02:00
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
2020-11-15 06:49:54 +01:00
let mut params: StaticVec<_> = Default::default();
2020-07-19 11:14:55 +02:00
if input.next().unwrap().0 != Token::Or {
2020-10-20 17:16:03 +02:00
if !match_token(input, Token::Pipe).0 {
2020-07-19 11:14:55 +02:00
loop {
match input.next().unwrap() {
(Token::Pipe, _) => break,
(Token::Identifier(s), pos) => {
2020-11-13 11:32:18 +01:00
if params.iter().any(|(p, _)| p == &s) {
return Err(PERR::FnDuplicatedParam("".to_string(), s).into_err(pos));
}
2020-11-13 03:43:54 +01:00
let s = state.get_interned_string(s);
state.stack.push((s.clone(), AccessMode::ReadWrite));
2020-07-19 11:14:55 +02:00
params.push((s, pos))
}
(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))
}
}
match input.next().unwrap() {
(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))
}
}
}
}
}
// Parse function body
settings.is_breakable = false;
2020-12-29 03:41:20 +01:00
let body = parse_stmt(input, state, lib, settings.level_up())?;
2020-07-19 11:14:55 +02:00
2020-07-30 07:28:06 +02:00
// External variables may need to be processed in a consistent order,
// so extract them into a list.
2020-12-22 09:45:56 +01:00
let externals: StaticVec<Ident> = {
2020-08-03 06:10:20 +02:00
#[cfg(not(feature = "no_closure"))]
2020-07-31 16:30:23 +02:00
{
state
.externals
.iter()
2020-12-22 09:45:56 +01:00
.map(|(name, &pos)| Ident {
name: name.clone(),
pos,
})
2020-07-31 16:30:23 +02:00
.collect()
}
2020-08-03 06:10:20 +02:00
#[cfg(feature = "no_closure")]
2020-07-31 16:30:23 +02:00
Default::default()
};
2020-07-30 07:28:06 +02:00
2020-08-03 06:10:20 +02:00
let params: StaticVec<_> = if cfg!(not(feature = "no_closure")) {
2020-07-31 16:30:23 +02:00
externals
.iter()
2020-10-28 12:11:17 +01:00
.map(|k| k.name.clone())
2020-07-31 16:30:23 +02:00
.chain(params.into_iter().map(|(v, _)| v))
.collect()
} else {
params.into_iter().map(|(v, _)| v).collect()
};
2020-07-19 11:14:55 +02:00
// Create unique function name by hashing the script body plus the parameters.
2020-11-13 11:32:18 +01:00
let hasher = &mut get_hasher();
params.iter().for_each(|p| p.as_str().hash(hasher));
body.hash(hasher);
2020-11-13 11:32:18 +01:00
let hash = hasher.finish();
2020-07-19 11:14:55 +02:00
2020-11-16 09:28:04 +01:00
let fn_name: ImmutableString = format!("{}{:016x}", crate::engine::FN_ANONYMOUS, hash).into();
2020-07-19 11:14:55 +02:00
2020-07-30 07:28:06 +02:00
// Define the function
2020-07-19 11:14:55 +02:00
let script = ScriptFnDef {
name: fn_name.clone(),
2020-11-17 05:23:53 +01:00
access: FnAccess::Public,
2020-07-29 17:34:48 +02:00
params,
2020-08-03 06:10:20 +02:00
#[cfg(not(feature = "no_closure"))]
2020-11-15 06:49:54 +01:00
externals: Default::default(),
2020-07-19 11:14:55 +02:00
body,
lib: None,
2020-11-09 14:52:23 +01:00
#[cfg(not(feature = "no_module"))]
mods: Default::default(),
comments: Default::default(),
2020-07-19 11:14:55 +02:00
};
2020-11-13 03:43:54 +01:00
let expr = Expr::FnPointer(fn_name, settings.pos);
2020-07-19 11:14:55 +02:00
2021-01-25 04:31:54 +01:00
#[cfg(not(feature = "no_closure"))]
let expr = make_curry_from_externals(expr, externals, settings.pos);
2020-07-29 16:43:50 +02:00
2020-07-19 11:14:55 +02:00
Ok((expr, script))
}
2020-06-03 04:44:36 +02:00
impl Engine {
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,
2020-06-03 04:44:36 +02:00
scope: &Scope,
optimization_level: OptimizationLevel,
) -> Result<AST, ParseError> {
let mut functions = Default::default();
2020-07-26 09:53:22 +02:00
let mut state = ParseState::new(
self,
#[cfg(not(feature = "unchecked"))]
2021-01-06 06:46:53 +01:00
NonZeroUsize::new(self.max_expr_depth()),
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
#[cfg(not(feature = "no_function"))]
2021-01-06 06:46:53 +01:00
NonZeroUsize::new(self.max_function_expr_depth()),
2020-07-26 09:53:22 +02:00
);
let settings = ParseSettings {
allow_if_expr: false,
2020-11-14 16:43:36 +01:00
allow_switch_expr: false,
allow_stmt_expr: false,
allow_anonymous_fn: false,
is_global: true,
2020-07-16 06:09:31 +02:00
is_function_scope: false,
is_breakable: false,
level: 0,
2020-11-20 09:52:28 +01:00
pos: Position::NONE,
};
let expr = parse_expr(input, &mut state, &mut functions, settings)?;
2020-06-03 04:44:36 +02:00
assert!(functions.is_empty());
2020-06-03 04:44:36 +02:00
match input.peek().unwrap() {
(Token::EOF, _) => (),
// 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
}
}
2020-10-27 11:18:19 +01:00
let expr = vec![Stmt::Expr(expr)];
2020-06-03 04:44:36 +02:00
Ok(
// Optimize AST
optimize_into_ast(self, scope, expr, Default::default(), optimization_level),
)
}
2020-06-14 08:25:47 +02:00
/// Parse the global level statements.
fn parse_global_level(
&self,
input: &mut TokenStream,
) -> Result<(Vec<Stmt>, Vec<ScriptFnDef>), ParseError> {
2020-11-15 06:49:54 +01:00
let mut statements = Vec::with_capacity(16);
let mut functions = HashMap::with_capacity_and_hasher(16, StraightHasherBuilder);
2020-07-26 09:53:22 +02:00
let mut state = ParseState::new(
self,
#[cfg(not(feature = "unchecked"))]
2021-01-06 06:46:53 +01:00
NonZeroUsize::new(self.max_expr_depth()),
2020-07-26 09:53:22 +02:00
#[cfg(not(feature = "unchecked"))]
#[cfg(not(feature = "no_function"))]
2021-01-06 06:46:53 +01:00
NonZeroUsize::new(self.max_function_expr_depth()),
2020-07-26 09:53:22 +02:00
);
2020-06-14 08:25:47 +02:00
while !input.peek().unwrap().0.is_eof() {
let settings = ParseSettings {
allow_if_expr: true,
2020-11-14 16:43:36 +01:00
allow_switch_expr: true,
2020-06-14 08:25:47 +02:00
allow_stmt_expr: true,
allow_anonymous_fn: true,
2020-06-14 08:25:47 +02:00
is_global: true,
2020-07-16 06:09:31 +02:00
is_function_scope: false,
2020-06-14 08:25:47 +02:00
is_breakable: false,
level: 0,
2020-11-20 09:52:28 +01:00
pos: Position::NONE,
2020-06-14 08:25:47 +02:00
};
2020-12-29 03:41:20 +01:00
let stmt = parse_stmt(input, &mut state, &mut functions, settings)?;
if stmt.is_noop() {
continue;
}
2020-06-14 08:25:47 +02:00
let need_semicolon = !stmt.is_self_terminated();
statements.push(stmt);
match input.peek().unwrap() {
// EOF
(Token::EOF, _) => break,
// stmt ;
(Token::SemiColon, _) if need_semicolon => {
eat_token(input, Token::SemiColon);
}
// stmt ;
(Token::SemiColon, _) if !need_semicolon => (),
// { stmt } ???
(_, _) if !need_semicolon => (),
// 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 ???
(_, pos) => {
// Semicolons are not optional between statements
return Err(PERR::MissingToken(
Token::SemiColon.into(),
"to terminate this statement".into(),
)
.into_err(*pos));
}
}
}
Ok((statements, functions.into_iter().map(|(_, v)| v).collect()))
}
2020-06-03 04:44:36 +02:00
/// Run the parser on an input stream, returning an AST.
2020-10-08 16:25:50 +02:00
#[inline(always)]
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,
2020-06-03 04:44:36 +02:00
scope: &Scope,
optimization_level: OptimizationLevel,
) -> Result<AST, ParseError> {
let (statements, lib) = self.parse_global_level(input)?;
2020-06-03 04:44:36 +02:00
Ok(
// Optimize AST
optimize_into_ast(self, scope, statements, lib, optimization_level),
)
}
2016-02-29 22:43:45 +01:00
}
2020-03-18 03:36:50 +01:00
/// Map a `Dynamic` value to an expression.
///
/// Returns Some(expression) if conversion is successful. Otherwise None.
2020-04-05 11:44:48 +02:00
pub fn map_dynamic_to_expr(value: Dynamic, pos: Position) -> Option<Expr> {
2020-04-12 17:00:06 +02:00
match value.0 {
#[cfg(not(feature = "no_float"))]
2020-12-08 15:47:38 +01:00
Union::Float(value, _) => Some(Expr::FloatConstant(value, pos)),
#[cfg(feature = "decimal")]
Union::Decimal(value, _) => Some(Expr::DynamicConstant(Box::new((*value).into()), pos)),
2020-12-08 15:47:38 +01:00
Union::Unit(_, _) => Some(Expr::Unit(pos)),
Union::Int(value, _) => Some(Expr::IntegerConstant(value, pos)),
Union::Char(value, _) => Some(Expr::CharConstant(value, pos)),
Union::Str(value, _) => Some(Expr::StringConstant(value, pos)),
Union::Bool(value, _) => Some(Expr::BoolConstant(value, pos)),
2020-04-10 06:16:39 +02:00
#[cfg(not(feature = "no_index"))]
Union::Array(array, _) => Some(Expr::DynamicConstant(Box::new((*array).into()), pos)),
2020-04-10 06:16:39 +02:00
#[cfg(not(feature = "no_object"))]
Union::Map(map, _) => Some(Expr::DynamicConstant(Box::new((*map).into()), pos)),
2020-04-12 17:00:06 +02:00
_ => None,
}
2016-02-29 22:43:45 +01:00
}