2020-03-08 12:54:02 +01:00
|
|
|
//! Main module defining the lexer and parser.
|
|
|
|
|
2020-04-12 17:00:06 +02:00
|
|
|
use crate::any::{Dynamic, Union};
|
2020-05-07 18:19:08 +02:00
|
|
|
use crate::calc_fn_hash;
|
2020-06-05 09:14:42 +02:00
|
|
|
use crate::engine::{make_getter, make_setter, Engine};
|
2020-03-04 15:00:01 +01:00
|
|
|
use crate::error::{LexError, ParseError, ParseErrorType};
|
2020-06-05 09:14:42 +02:00
|
|
|
use crate::module::{Module, ModuleRef};
|
2020-04-08 04:19:03 +02:00
|
|
|
use crate::optimize::{optimize_into_ast, OptimizationLevel};
|
2020-04-10 06:16:39 +02:00
|
|
|
use crate::scope::{EntryType as ScopeEntryType, Scope};
|
2020-06-11 12:13:33 +02:00
|
|
|
use crate::token::{Position, Token, TokenStream};
|
2020-05-30 18:02:23 +02:00
|
|
|
use crate::utils::{StaticVec, StraightHasherBuilder};
|
2020-05-08 08:50:48 +02:00
|
|
|
|
2020-03-17 19:26:11 +01:00
|
|
|
use crate::stdlib::{
|
|
|
|
borrow::Cow,
|
|
|
|
boxed::Box,
|
2020-03-29 17:53:35 +02:00
|
|
|
char,
|
|
|
|
collections::HashMap,
|
2020-04-06 06:29:01 +02:00
|
|
|
format,
|
2020-06-11 17:08:00 +02:00
|
|
|
iter::empty,
|
2020-05-30 04:27:48 +02:00
|
|
|
mem,
|
2020-04-29 10:11:54 +02:00
|
|
|
num::NonZeroUsize,
|
2020-05-03 10:54:24 +02:00
|
|
|
ops::{Add, Deref, DerefMut},
|
2020-03-17 19:26:11 +01:00
|
|
|
string::{String, ToString},
|
2020-04-15 16:21:23 +02:00
|
|
|
vec,
|
2020-03-17 19:26:11 +01:00
|
|
|
vec::Vec,
|
2020-03-12 05:40:28 +01:00
|
|
|
};
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-03-11 04:03:18 +01:00
|
|
|
/// The system integer type.
|
|
|
|
///
|
|
|
|
/// If the `only_i32` feature is enabled, this will be `i32` instead.
|
2020-03-10 16:06:20 +01:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
pub type INT = i64;
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// The system integer type.
|
2020-03-11 04:03:18 +01:00
|
|
|
///
|
|
|
|
/// If the `only_i32` feature is not enabled, this will be `i64` instead.
|
2020-03-10 16:06:20 +01:00
|
|
|
#[cfg(feature = "only_i32")]
|
|
|
|
pub type INT = i32;
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// The system floating-point type.
|
2020-04-03 13:42:01 +02:00
|
|
|
///
|
|
|
|
/// Not available under the `no_float` feature.
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-03-10 16:06:20 +01:00
|
|
|
pub type FLOAT = f64;
|
|
|
|
|
2020-03-01 06:30:22 +01:00
|
|
|
type PERR = ParseErrorType;
|
|
|
|
|
2020-05-26 08:14:03 +02:00
|
|
|
pub use crate::utils::ImmutableString;
|
|
|
|
|
2020-03-04 15:00:01 +01:00
|
|
|
/// Compiled AST (abstract syntax tree) of a Rhai script.
|
2020-04-03 13:42:01 +02:00
|
|
|
///
|
|
|
|
/// Currently, `AST` is neither `Send` nor `Sync`. Turn on the `sync` feature to make it `Send + Sync`.
|
2020-05-05 09:00:10 +02:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2020-04-04 16:00:44 +02:00
|
|
|
pub struct AST(
|
2020-05-05 09:00:10 +02:00
|
|
|
/// Global statements.
|
|
|
|
Vec<Stmt>,
|
2020-05-19 16:25:57 +02:00
|
|
|
/// Script-defined functions.
|
2020-06-05 09:14:42 +02:00
|
|
|
Module,
|
2020-04-04 16:00:44 +02:00
|
|
|
);
|
2020-02-24 16:25:52 +01:00
|
|
|
|
2020-03-27 04:50:24 +01:00
|
|
|
impl AST {
|
2020-04-04 16:00:44 +02:00
|
|
|
/// Create a new `AST`.
|
2020-06-05 09:14:42 +02:00
|
|
|
pub fn new(statements: Vec<Stmt>, lib: Module) -> Self {
|
2020-05-24 10:40:00 +02:00
|
|
|
Self(statements, lib)
|
2020-05-05 09:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the statements.
|
|
|
|
pub(crate) fn statements(&self) -> &Vec<Stmt> {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a mutable reference to the statements.
|
|
|
|
pub(crate) fn statements_mut(&mut self) -> &mut Vec<Stmt> {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the script-defined functions.
|
2020-06-05 09:14:42 +02:00
|
|
|
pub(crate) fn lib(&self) -> &Module {
|
2020-05-19 16:25:57 +02:00
|
|
|
&self.1
|
2020-04-04 16:00:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Merge two `AST` into one. Both `AST`'s are untouched and a new, merged, version
|
2020-03-27 04:50:24 +01:00
|
|
|
/// is returned.
|
|
|
|
///
|
|
|
|
/// The second `AST` is simply appended to the end of the first _without any processing_.
|
|
|
|
/// Thus, the return value of the first `AST` (if using expression-statement syntax) is buried.
|
|
|
|
/// Of course, if the first `AST` uses a `return` statement at the end, then
|
|
|
|
/// the second `AST` will essentially be dead code.
|
|
|
|
///
|
|
|
|
/// All script-defined functions in the second `AST` overwrite similarly-named functions
|
|
|
|
/// in the first `AST` with the same number of parameters.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2020-04-21 17:25:12 +02:00
|
|
|
/// # fn main() -> Result<(), Box<rhai::EvalAltResult>> {
|
2020-04-01 04:21:49 +02:00
|
|
|
/// # #[cfg(not(feature = "no_function"))]
|
|
|
|
/// # {
|
2020-03-27 04:50:24 +01:00
|
|
|
/// use rhai::Engine;
|
|
|
|
///
|
2020-04-07 07:23:06 +02:00
|
|
|
/// let engine = Engine::new();
|
2020-03-27 04:50:24 +01:00
|
|
|
///
|
|
|
|
/// let ast1 = engine.compile(r#"fn foo(x) { 42 + x } foo(1)"#)?;
|
2020-04-01 04:21:49 +02:00
|
|
|
/// let ast2 = engine.compile(r#"fn foo(n) { "hello" + n } foo("!")"#)?;
|
2020-03-27 04:50:24 +01:00
|
|
|
///
|
2020-04-04 16:00:44 +02:00
|
|
|
/// let ast = ast1.merge(&ast2); // Merge 'ast2' into 'ast1'
|
2020-03-27 04:50:24 +01:00
|
|
|
///
|
|
|
|
/// // Notice that using the '+' operator also works:
|
2020-04-04 16:00:44 +02:00
|
|
|
/// // let ast = &ast1 + &ast2;
|
2020-03-27 04:50:24 +01:00
|
|
|
///
|
|
|
|
/// // 'ast' is essentially:
|
|
|
|
/// //
|
|
|
|
/// // fn foo(n) { "hello" + n } // <- definition of first 'foo' is overwritten
|
|
|
|
/// // foo(1) // <- notice this will be "hello1" instead of 43,
|
|
|
|
/// // // but it is no longer the return value
|
2020-04-01 04:21:49 +02:00
|
|
|
/// // foo("!") // returns "hello!"
|
2020-03-27 04:50:24 +01:00
|
|
|
///
|
|
|
|
/// // Evaluate it
|
2020-04-01 04:21:49 +02:00
|
|
|
/// assert_eq!(engine.eval_ast::<String>(&ast)?, "hello!");
|
|
|
|
/// # }
|
2020-03-27 04:50:24 +01:00
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2020-04-04 16:00:44 +02:00
|
|
|
pub fn merge(&self, other: &Self) -> Self {
|
2020-04-05 11:44:48 +02:00
|
|
|
let Self(statements, functions) = self;
|
2020-04-04 16:00:44 +02:00
|
|
|
|
2020-04-05 11:44:48 +02:00
|
|
|
let ast = match (statements.is_empty(), other.0.is_empty()) {
|
2020-04-04 16:00:44 +02:00
|
|
|
(false, false) => {
|
2020-04-05 11:44:48 +02:00
|
|
|
let mut statements = statements.clone();
|
|
|
|
statements.extend(other.0.iter().cloned());
|
|
|
|
statements
|
2020-04-04 16:00:44 +02:00
|
|
|
}
|
2020-04-05 11:44:48 +02:00
|
|
|
(false, true) => statements.clone(),
|
2020-04-04 16:00:44 +02:00
|
|
|
(true, false) => other.0.clone(),
|
|
|
|
(true, true) => vec![],
|
|
|
|
};
|
2020-03-27 04:50:24 +01:00
|
|
|
|
2020-06-05 09:14:42 +02:00
|
|
|
let mut functions = functions.clone();
|
|
|
|
functions.merge(&other.1);
|
|
|
|
|
|
|
|
Self::new(ast, functions)
|
2020-04-04 16:00:44 +02:00
|
|
|
}
|
2020-03-27 04:50:24 +01:00
|
|
|
|
2020-04-04 16:00:44 +02:00
|
|
|
/// Clear all function definitions in the `AST`.
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-04-04 16:00:44 +02:00
|
|
|
pub fn clear_functions(&mut self) {
|
2020-05-19 16:25:57 +02:00
|
|
|
self.1 = Default::default();
|
2020-04-04 16:00:44 +02:00
|
|
|
}
|
2020-03-27 04:50:24 +01:00
|
|
|
|
2020-04-04 16:00:44 +02:00
|
|
|
/// Clear all statements in the `AST`, leaving only function definitions.
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-04-04 16:00:44 +02:00
|
|
|
pub fn retain_functions(&mut self) {
|
|
|
|
self.0 = vec![];
|
2020-03-27 04:50:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 16:00:44 +02:00
|
|
|
impl Add<Self> for &AST {
|
|
|
|
type Output = AST;
|
2020-03-27 04:50:24 +01:00
|
|
|
|
|
|
|
fn add(self, rhs: Self) -> Self::Output {
|
|
|
|
self.merge(rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 05:29:30 +02:00
|
|
|
/// A type representing the access mode of a scripted function.
|
2020-05-12 12:48:25 +02:00
|
|
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
2020-05-09 05:29:30 +02:00
|
|
|
pub enum FnAccess {
|
|
|
|
/// Private function.
|
|
|
|
Private,
|
|
|
|
/// Public function.
|
|
|
|
Public,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A scripted function definition.
|
2020-03-15 15:39:58 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2020-06-05 09:14:42 +02:00
|
|
|
pub struct ScriptFnDef {
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Function name.
|
2020-03-11 16:43:04 +01:00
|
|
|
pub name: String,
|
2020-05-09 05:29:30 +02:00
|
|
|
/// Function access mode.
|
|
|
|
pub access: FnAccess,
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Names of function parameters.
|
2020-05-16 18:24:07 +02:00
|
|
|
pub params: StaticVec<String>,
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Function body.
|
2020-05-09 15:46:38 +02:00
|
|
|
pub body: Stmt,
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Position of the function definition.
|
2020-03-02 15:13:14 +01:00
|
|
|
pub pos: Position,
|
2016-03-01 21:16:10 +01:00
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// `return`/`throw` statement.
|
2020-05-12 12:48:25 +02:00
|
|
|
#[derive(Debug, Eq, PartialEq, Hash, Clone, Copy)]
|
2020-03-11 16:43:04 +01:00
|
|
|
pub enum ReturnType {
|
2020-03-18 03:36:50 +01:00
|
|
|
/// `return` statement.
|
2020-03-11 16:43:04 +01:00
|
|
|
Return,
|
2020-03-18 03:36:50 +01:00
|
|
|
/// `throw` statement.
|
2020-03-11 16:43:04 +01:00
|
|
|
Exception,
|
|
|
|
}
|
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
|
2020-05-18 13:32:22 +02:00
|
|
|
struct ParseState {
|
|
|
|
/// Encapsulates a local stack with variable names to simulate an actual runtime scope.
|
|
|
|
stack: Vec<(String, ScopeEntryType)>,
|
2020-06-11 16:03:47 +02:00
|
|
|
/// Maximum levels of expression nesting.
|
2020-05-18 13:32:22 +02:00
|
|
|
max_expr_depth: usize,
|
2020-06-14 08:25:47 +02:00
|
|
|
/// Maximum length of a string.
|
|
|
|
pub max_string_size: usize,
|
|
|
|
/// Maximum length of an array.
|
|
|
|
pub max_array_size: usize,
|
|
|
|
/// Maximum number of properties in a map.
|
|
|
|
pub max_map_size: usize,
|
2020-05-18 13:32:22 +02:00
|
|
|
}
|
2020-04-28 17:05:03 +02:00
|
|
|
|
2020-05-18 13:32:22 +02:00
|
|
|
impl ParseState {
|
|
|
|
/// Create a new `ParseState`.
|
2020-06-14 08:25:47 +02:00
|
|
|
pub fn new(
|
|
|
|
max_expr_depth: usize,
|
|
|
|
max_string_size: usize,
|
|
|
|
max_array_size: usize,
|
|
|
|
max_map_size: usize,
|
|
|
|
) -> Self {
|
2020-05-18 13:32:22 +02:00
|
|
|
Self {
|
|
|
|
max_expr_depth,
|
2020-06-14 08:25:47 +02:00
|
|
|
max_string_size,
|
|
|
|
max_array_size,
|
|
|
|
max_map_size,
|
2020-05-18 13:32:22 +02:00
|
|
|
..Default::default()
|
|
|
|
}
|
2020-04-28 17:05:03 +02:00
|
|
|
}
|
2020-05-18 13:32:22 +02:00
|
|
|
/// Find a variable by name in the `ParseState`, searching in reverse.
|
2020-04-29 10:11:54 +02:00
|
|
|
/// The return value is the offset to be deducted from `Stack::len`,
|
2020-05-18 13:32:22 +02:00
|
|
|
/// i.e. the top element of the `ParseState` is offset 1.
|
|
|
|
/// Return zero when the variable name is not found in the `ParseState`.
|
2020-04-29 10:11:54 +02:00
|
|
|
pub fn find(&self, name: &str) -> Option<NonZeroUsize> {
|
2020-05-18 13:32:22 +02:00
|
|
|
self.stack
|
2020-04-28 17:05:03 +02:00
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.enumerate()
|
2020-05-04 13:36:58 +02:00
|
|
|
.find(|(_, (n, typ))| match typ {
|
|
|
|
ScopeEntryType::Normal | ScopeEntryType::Constant => *n == name,
|
2020-05-05 04:39:12 +02:00
|
|
|
ScopeEntryType::Module => false,
|
2020-05-04 13:36:58 +02:00
|
|
|
})
|
|
|
|
.and_then(|(i, _)| NonZeroUsize::new(i + 1))
|
|
|
|
}
|
2020-05-18 13:32:22 +02:00
|
|
|
/// Find a module by name in the `ParseState`, searching in reverse.
|
2020-05-04 13:36:58 +02:00
|
|
|
/// The return value is the offset to be deducted from `Stack::len`,
|
2020-05-18 13:32:22 +02:00
|
|
|
/// i.e. the top element of the `ParseState` is offset 1.
|
|
|
|
/// Return zero when the variable name is not found in the `ParseState`.
|
2020-05-06 16:54:34 +02:00
|
|
|
pub fn find_module(&self, name: &str) -> Option<NonZeroUsize> {
|
2020-05-18 13:32:22 +02:00
|
|
|
self.stack
|
2020-05-04 13:36:58 +02:00
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.enumerate()
|
|
|
|
.find(|(_, (n, typ))| match typ {
|
2020-05-05 04:39:12 +02:00
|
|
|
ScopeEntryType::Module => *n == name,
|
2020-05-04 13:36:58 +02:00
|
|
|
ScopeEntryType::Normal | ScopeEntryType::Constant => false,
|
|
|
|
})
|
2020-04-29 10:11:54 +02:00
|
|
|
.and_then(|(i, _)| NonZeroUsize::new(i + 1))
|
2020-04-28 17:05:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 13:32:22 +02:00
|
|
|
impl Deref for ParseState {
|
2020-05-04 13:36:58 +02:00
|
|
|
type Target = Vec<(String, ScopeEntryType)>;
|
2020-05-03 10:54:24 +02:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2020-05-18 13:32:22 +02:00
|
|
|
&self.stack
|
2020-05-03 10:54:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 13:32:22 +02:00
|
|
|
impl DerefMut for ParseState {
|
2020-05-03 10:54:24 +02:00
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
2020-05-18 13:32:22 +02:00
|
|
|
&mut self.stack
|
2020-05-03 10:54:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
|
|
|
/// A type that encapsulates all the settings for a particular parsing function.
|
|
|
|
struct ParseSettings {
|
|
|
|
/// Current position.
|
|
|
|
pos: Position,
|
|
|
|
/// Is the construct being parsed located at global level?
|
|
|
|
is_global: bool,
|
|
|
|
/// Is the current position inside a loop?
|
|
|
|
is_breakable: bool,
|
|
|
|
/// Is if-expression allowed?
|
|
|
|
allow_if_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.
|
|
|
|
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.
|
|
|
|
pub fn ensure_level_within_max_limit(&self, limit: usize) -> Result<(), ParseError> {
|
2020-06-14 08:25:47 +02:00
|
|
|
if limit == 0 {
|
|
|
|
Ok(())
|
|
|
|
} else if self.level > limit {
|
2020-06-11 16:03:47 +02:00
|
|
|
Err(PERR::ExprTooDeep.into_err(self.pos))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// A statement.
|
2020-05-09 18:19:13 +02:00
|
|
|
///
|
|
|
|
/// Each variant is at most one pointer in size (for speed),
|
|
|
|
/// with everything being allocated together in one single tuple.
|
2020-03-02 07:28:42 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2016-04-17 04:32:18 +02:00
|
|
|
pub enum Stmt {
|
2020-03-18 03:36:50 +01:00
|
|
|
/// No-op.
|
2020-03-09 14:57:07 +01:00
|
|
|
Noop(Position),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// if expr { stmt } else { stmt }
|
2020-05-09 15:46:38 +02:00
|
|
|
IfThenElse(Box<(Expr, Stmt, Option<Stmt>)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// while expr { stmt }
|
2020-05-09 15:46:38 +02:00
|
|
|
While(Box<(Expr, Stmt)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// loop { stmt }
|
2017-10-30 16:08:44 +01:00
|
|
|
Loop(Box<Stmt>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// for id in expr { stmt }
|
2020-05-09 15:46:38 +02:00
|
|
|
For(Box<(String, Expr, Stmt)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// let id = expr
|
2020-05-09 18:19:13 +02:00
|
|
|
Let(Box<((String, Position), Option<Expr>)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// const id = expr
|
2020-05-09 18:19:13 +02:00
|
|
|
Const(Box<((String, Position), Expr)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// { stmt; ... }
|
2020-05-16 18:24:07 +02:00
|
|
|
Block(Box<(StaticVec<Stmt>, Position)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// { stmt }
|
2016-04-17 04:32:18 +02:00
|
|
|
Expr(Box<Expr>),
|
2020-04-01 10:22:18 +02:00
|
|
|
/// continue
|
|
|
|
Continue(Position),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// break
|
2020-03-02 15:13:14 +01:00
|
|
|
Break(Position),
|
2020-05-04 11:43:54 +02:00
|
|
|
/// return/throw
|
2020-05-09 18:19:13 +02:00
|
|
|
ReturnWithVal(Box<((ReturnType, Position), Option<Expr>)>),
|
2020-05-04 13:36:58 +02:00
|
|
|
/// import expr as module
|
2020-05-09 18:19:13 +02:00
|
|
|
Import(Box<(Expr, (String, Position))>),
|
2020-05-08 10:49:24 +02:00
|
|
|
/// expr id as name, ...
|
2020-05-16 18:24:07 +02:00
|
|
|
Export(Box<StaticVec<((String, Position), Option<(String, Position)>)>>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Stmt {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Noop(Default::default())
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-03-09 14:57:07 +01:00
|
|
|
impl Stmt {
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Get the `Position` of this statement.
|
2020-03-17 10:33:37 +01:00
|
|
|
pub fn position(&self) -> Position {
|
|
|
|
match self {
|
2020-05-09 15:46:38 +02:00
|
|
|
Stmt::Noop(pos) | Stmt::Continue(pos) | Stmt::Break(pos) => *pos,
|
2020-05-09 18:19:13 +02:00
|
|
|
Stmt::Let(x) => (x.0).1,
|
|
|
|
Stmt::Const(x) => (x.0).1,
|
|
|
|
Stmt::ReturnWithVal(x) => (x.0).1,
|
2020-05-09 15:46:38 +02:00
|
|
|
Stmt::Block(x) => x.1,
|
|
|
|
Stmt::IfThenElse(x) => x.0.position(),
|
|
|
|
Stmt::Expr(x) => x.position(),
|
|
|
|
Stmt::While(x) => x.1.position(),
|
|
|
|
Stmt::Loop(x) => x.position(),
|
|
|
|
Stmt::For(x) => x.2.position(),
|
2020-05-09 18:19:13 +02:00
|
|
|
Stmt::Import(x) => (x.1).1,
|
2020-05-17 16:19:49 +02:00
|
|
|
Stmt::Export(x) => (x.get(0).0).1,
|
2020-03-17 10:33:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Is this statement self-terminated (i.e. no need for a semicolon terminator)?
|
2020-03-17 07:29:22 +01:00
|
|
|
pub fn is_self_terminated(&self) -> bool {
|
|
|
|
match self {
|
2020-05-09 15:46:38 +02:00
|
|
|
Stmt::IfThenElse(_)
|
|
|
|
| Stmt::While(_)
|
2020-03-17 07:29:22 +01:00
|
|
|
| Stmt::Loop(_)
|
2020-05-09 15:46:38 +02:00
|
|
|
| Stmt::For(_)
|
|
|
|
| Stmt::Block(_) => true,
|
2020-03-17 07:29:22 +01:00
|
|
|
|
2020-03-18 11:41:18 +01:00
|
|
|
// A No-op requires a semicolon in order to know it is an empty statement!
|
|
|
|
Stmt::Noop(_) => false,
|
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Stmt::Let(_)
|
|
|
|
| Stmt::Const(_)
|
|
|
|
| Stmt::Import(_)
|
2020-05-08 10:49:24 +02:00
|
|
|
| Stmt::Export(_)
|
2020-03-17 07:29:22 +01:00
|
|
|
| Stmt::Expr(_)
|
2020-04-01 10:22:18 +02:00
|
|
|
| Stmt::Continue(_)
|
2020-03-17 07:29:22 +01:00
|
|
|
| Stmt::Break(_)
|
2020-05-09 15:46:38 +02:00
|
|
|
| Stmt::ReturnWithVal(_) => false,
|
2020-03-17 07:29:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Is this statement _pure_?
|
2020-03-17 10:33:37 +01:00
|
|
|
pub fn is_pure(&self) -> bool {
|
2020-03-11 16:43:04 +01:00
|
|
|
match self {
|
2020-03-17 10:33:37 +01:00
|
|
|
Stmt::Noop(_) => true,
|
|
|
|
Stmt::Expr(expr) => expr.is_pure(),
|
2020-05-09 15:46:38 +02:00
|
|
|
Stmt::IfThenElse(x) if x.2.is_some() => {
|
|
|
|
x.0.is_pure() && x.1.is_pure() && x.2.as_ref().unwrap().is_pure()
|
2020-03-17 10:33:37 +01:00
|
|
|
}
|
2020-05-09 15:46:38 +02:00
|
|
|
Stmt::IfThenElse(x) => x.1.is_pure(),
|
|
|
|
Stmt::While(x) => x.0.is_pure() && x.1.is_pure(),
|
|
|
|
Stmt::Loop(x) => x.is_pure(),
|
|
|
|
Stmt::For(x) => x.1.is_pure() && x.2.is_pure(),
|
|
|
|
Stmt::Let(_) | Stmt::Const(_) => false,
|
|
|
|
Stmt::Block(x) => x.0.iter().all(Stmt::is_pure),
|
|
|
|
Stmt::Continue(_) | Stmt::Break(_) | Stmt::ReturnWithVal(_) => false,
|
|
|
|
Stmt::Import(_) => false,
|
2020-05-08 10:49:24 +02:00
|
|
|
Stmt::Export(_) => false,
|
2020-03-11 16:43:04 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-09 14:57:07 +01:00
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// An expression.
|
2020-05-09 18:19:13 +02:00
|
|
|
///
|
|
|
|
/// Each variant is at most one pointer in size (for speed),
|
|
|
|
/// with everything being allocated together in one single tuple.
|
2020-03-02 07:28:42 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2016-04-17 04:32:18 +02:00
|
|
|
pub enum Expr {
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Integer constant.
|
2020-05-09 15:46:38 +02:00
|
|
|
IntegerConstant(Box<(INT, Position)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Floating-point constant.
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-05-09 15:46:38 +02:00
|
|
|
FloatConstant(Box<(FLOAT, Position)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Character constant.
|
2020-05-09 15:46:38 +02:00
|
|
|
CharConstant(Box<(char, Position)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// String constant.
|
2020-05-25 07:44:28 +02:00
|
|
|
StringConstant(Box<(ImmutableString, Position)>),
|
2020-05-09 18:19:13 +02:00
|
|
|
/// Variable access - ((variable name, position), optional modules, hash, optional index)
|
2020-05-22 12:28:13 +02:00
|
|
|
Variable(
|
|
|
|
Box<(
|
|
|
|
(String, Position),
|
|
|
|
Option<Box<ModuleRef>>,
|
|
|
|
u64,
|
|
|
|
Option<NonZeroUsize>,
|
|
|
|
)>,
|
|
|
|
),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Property access.
|
2020-05-13 04:19:18 +02:00
|
|
|
Property(Box<((String, String, String), Position)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// { stmt }
|
2020-05-09 15:46:38 +02:00
|
|
|
Stmt(Box<(Stmt, Position)>),
|
2020-05-30 04:27:48 +02:00
|
|
|
/// Wrapped expression - should not be optimized away.
|
|
|
|
Expr(Box<Expr>),
|
2020-05-23 12:59:28 +02:00
|
|
|
/// func(expr, ... ) - ((function name, native_only, position), optional modules, hash, arguments, optional default value)
|
2020-04-28 13:39:36 +02:00
|
|
|
/// Use `Cow<'static, str>` because a lot of operators (e.g. `==`, `>=`) are implemented as function calls
|
|
|
|
/// and the function names are predictable, so no need to allocate a new `String`.
|
2020-05-01 11:32:39 +02:00
|
|
|
FnCall(
|
2020-05-09 15:46:38 +02:00
|
|
|
Box<(
|
2020-05-23 12:59:28 +02:00
|
|
|
(Cow<'static, str>, bool, Position),
|
2020-05-22 12:28:13 +02:00
|
|
|
Option<Box<ModuleRef>>,
|
2020-05-09 15:46:38 +02:00
|
|
|
u64,
|
2020-05-16 18:24:07 +02:00
|
|
|
StaticVec<Expr>,
|
2020-05-09 18:19:13 +02:00
|
|
|
Option<Dynamic>,
|
2020-05-09 15:46:38 +02:00
|
|
|
)>,
|
2020-05-03 19:19:01 +02:00
|
|
|
),
|
2020-05-25 14:14:31 +02:00
|
|
|
/// expr op= expr
|
|
|
|
Assignment(Box<(Expr, Cow<'static, str>, Expr, Position)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// lhs.rhs
|
2020-05-09 15:46:38 +02:00
|
|
|
Dot(Box<(Expr, Expr, Position)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// expr[expr]
|
2020-05-09 15:46:38 +02:00
|
|
|
Index(Box<(Expr, Expr, Position)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// [ expr, ... ]
|
2020-05-16 18:24:07 +02:00
|
|
|
Array(Box<(StaticVec<Expr>, Position)>),
|
2020-03-30 11:40:26 +02:00
|
|
|
/// #{ name:expr, ... }
|
2020-05-16 18:24:07 +02:00
|
|
|
Map(Box<(StaticVec<((String, Position), Expr)>, Position)>),
|
2020-04-06 11:47:34 +02:00
|
|
|
/// lhs in rhs
|
2020-05-09 15:46:38 +02:00
|
|
|
In(Box<(Expr, Expr, Position)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// lhs && rhs
|
2020-05-09 15:46:38 +02:00
|
|
|
And(Box<(Expr, Expr, Position)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// lhs || rhs
|
2020-05-09 15:46:38 +02:00
|
|
|
Or(Box<(Expr, Expr, Position)>),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// true
|
2020-03-02 15:13:14 +01:00
|
|
|
True(Position),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// false
|
2020-03-02 15:13:14 +01:00
|
|
|
False(Position),
|
2020-03-18 03:36:50 +01:00
|
|
|
/// ()
|
2020-03-02 15:13:14 +01:00
|
|
|
Unit(Position),
|
|
|
|
}
|
|
|
|
|
2020-05-16 18:24:07 +02:00
|
|
|
impl Default for Expr {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Unit(Default::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 15:13:14 +01:00
|
|
|
impl Expr {
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Get the `Dynamic` value of a constant expression.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics when the expression is not constant.
|
2020-03-15 15:39:58 +01:00
|
|
|
pub fn get_constant_value(&self) -> Dynamic {
|
|
|
|
match self {
|
2020-05-30 04:27:48 +02:00
|
|
|
Self::Expr(x) => x.get_constant_value(),
|
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::IntegerConstant(x) => x.0.into(),
|
2020-04-12 17:00:06 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::FloatConstant(x) => x.0.into(),
|
|
|
|
Self::CharConstant(x) => x.0.into(),
|
|
|
|
Self::StringConstant(x) => x.0.clone().into(),
|
2020-04-22 06:12:13 +02:00
|
|
|
Self::True(_) => true.into(),
|
|
|
|
Self::False(_) => false.into(),
|
|
|
|
Self::Unit(_) => ().into(),
|
2020-04-12 17:00:06 +02:00
|
|
|
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::Array(x) if x.0.iter().all(Self::is_constant) => Dynamic(Union::Array(Box::new(
|
|
|
|
x.0.iter().map(Self::get_constant_value).collect::<Vec<_>>(),
|
|
|
|
))),
|
2020-04-12 17:00:06 +02:00
|
|
|
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-05-09 18:19:13 +02:00
|
|
|
Self::Map(x) if x.0.iter().all(|(_, v)| v.is_constant()) => {
|
2020-04-12 17:00:06 +02:00
|
|
|
Dynamic(Union::Map(Box::new(
|
2020-05-09 15:46:38 +02:00
|
|
|
x.0.iter()
|
2020-05-09 18:19:13 +02:00
|
|
|
.map(|((k, _), v)| (k.clone(), v.get_constant_value()))
|
2020-04-12 17:00:06 +02:00
|
|
|
.collect::<HashMap<_, _>>(),
|
|
|
|
)))
|
|
|
|
}
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2020-03-15 15:39:58 +01:00
|
|
|
_ => panic!("cannot get value of non-constant expression"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Get the display value of a constant expression.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics when the expression is not constant.
|
2020-03-15 15:39:58 +01:00
|
|
|
pub fn get_constant_str(&self) -> String {
|
2020-03-13 11:12:41 +01:00
|
|
|
match self {
|
2020-05-30 04:27:48 +02:00
|
|
|
Self::Expr(x) => x.get_constant_str(),
|
|
|
|
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::FloatConstant(x) => x.0.to_string(),
|
2020-05-06 13:45:17 +02:00
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::IntegerConstant(x) => x.0.to_string(),
|
|
|
|
Self::CharConstant(x) => x.0.to_string(),
|
|
|
|
Self::StringConstant(_) => "string".to_string(),
|
2020-04-06 11:47:34 +02:00
|
|
|
Self::True(_) => "true".to_string(),
|
|
|
|
Self::False(_) => "false".to_string(),
|
|
|
|
Self::Unit(_) => "()".to_string(),
|
2020-03-13 11:12:41 +01:00
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::Array(x) if x.0.iter().all(Self::is_constant) => "array".to_string(),
|
2020-03-15 15:39:58 +01:00
|
|
|
|
|
|
|
_ => panic!("cannot get value of non-constant expression"),
|
2020-03-13 11:12:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Get the `Position` of the expression.
|
2020-03-02 15:13:14 +01:00
|
|
|
pub fn position(&self) -> Position {
|
|
|
|
match self {
|
2020-05-30 04:27:48 +02:00
|
|
|
Self::Expr(x) => x.position(),
|
|
|
|
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::FloatConstant(x) => x.1,
|
|
|
|
|
|
|
|
Self::IntegerConstant(x) => x.1,
|
|
|
|
Self::CharConstant(x) => x.1,
|
|
|
|
Self::StringConstant(x) => x.1,
|
|
|
|
Self::Array(x) => x.1,
|
|
|
|
Self::Map(x) => x.1,
|
|
|
|
Self::Property(x) => x.1,
|
|
|
|
Self::Stmt(x) => x.1,
|
2020-05-09 18:19:13 +02:00
|
|
|
Self::Variable(x) => (x.0).1,
|
2020-05-23 12:59:28 +02:00
|
|
|
Self::FnCall(x) => (x.0).2,
|
2020-05-25 14:14:31 +02:00
|
|
|
Self::Assignment(x) => x.0.position(),
|
2020-05-09 15:46:38 +02:00
|
|
|
|
|
|
|
Self::And(x) | Self::Or(x) | Self::In(x) => x.2,
|
|
|
|
|
|
|
|
Self::True(pos) | Self::False(pos) | Self::Unit(pos) => *pos,
|
|
|
|
|
2020-05-25 14:14:31 +02:00
|
|
|
Self::Dot(x) | Self::Index(x) => x.0.position(),
|
2020-03-02 15:13:14 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-10 04:22:41 +01:00
|
|
|
|
2020-05-13 15:58:38 +02:00
|
|
|
/// Override the `Position` of the expression.
|
2020-04-10 15:02:13 +02:00
|
|
|
pub(crate) fn set_position(mut self, new_pos: Position) -> Self {
|
|
|
|
match &mut self {
|
2020-05-30 04:27:48 +02:00
|
|
|
Self::Expr(ref mut x) => {
|
|
|
|
let expr = mem::take(x);
|
|
|
|
*x = Box::new(expr.set_position(new_pos));
|
|
|
|
}
|
|
|
|
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::FloatConstant(x) => x.1 = new_pos,
|
|
|
|
|
|
|
|
Self::IntegerConstant(x) => x.1 = new_pos,
|
|
|
|
Self::CharConstant(x) => x.1 = new_pos,
|
|
|
|
Self::StringConstant(x) => x.1 = new_pos,
|
|
|
|
Self::Array(x) => x.1 = new_pos,
|
|
|
|
Self::Map(x) => x.1 = new_pos,
|
2020-05-09 18:19:13 +02:00
|
|
|
Self::Variable(x) => (x.0).1 = new_pos,
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::Property(x) => x.1 = new_pos,
|
|
|
|
Self::Stmt(x) => x.1 = new_pos,
|
2020-05-23 12:59:28 +02:00
|
|
|
Self::FnCall(x) => (x.0).2 = new_pos,
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::And(x) => x.2 = new_pos,
|
|
|
|
Self::Or(x) => x.2 = new_pos,
|
|
|
|
Self::In(x) => x.2 = new_pos,
|
|
|
|
Self::True(pos) => *pos = new_pos,
|
|
|
|
Self::False(pos) => *pos = new_pos,
|
|
|
|
Self::Unit(pos) => *pos = new_pos,
|
2020-05-25 14:14:31 +02:00
|
|
|
Self::Assignment(x) => x.3 = new_pos,
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::Dot(x) => x.2 = new_pos,
|
|
|
|
Self::Index(x) => x.2 = new_pos,
|
2020-04-10 15:02:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Is the expression pure?
|
2020-03-11 16:43:04 +01:00
|
|
|
///
|
|
|
|
/// A pure expression has no side effects.
|
|
|
|
pub fn is_pure(&self) -> bool {
|
2020-03-10 04:22:41 +01:00
|
|
|
match self {
|
2020-05-30 04:27:48 +02:00
|
|
|
Self::Expr(x) => x.is_pure(),
|
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::Array(x) => x.0.iter().all(Self::is_pure),
|
2020-03-15 15:39:58 +01:00
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::Index(x) | Self::And(x) | Self::Or(x) | Self::In(x) => {
|
2020-05-09 18:19:13 +02:00
|
|
|
let (lhs, rhs, _) = x.as_ref();
|
|
|
|
lhs.is_pure() && rhs.is_pure()
|
2020-04-06 11:47:34 +02:00
|
|
|
}
|
2020-03-15 15:39:58 +01:00
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::Stmt(x) => x.0.is_pure(),
|
2020-03-17 10:33:37 +01:00
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::Variable(_) => true,
|
2020-04-15 15:41:28 +02:00
|
|
|
|
2020-06-14 08:25:47 +02:00
|
|
|
_ => self.is_constant(),
|
2020-03-12 05:40:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Is the expression a constant?
|
2020-03-12 05:40:28 +01:00
|
|
|
pub fn is_constant(&self) -> bool {
|
|
|
|
match self {
|
2020-05-30 04:27:48 +02:00
|
|
|
Self::Expr(x) => x.is_constant(),
|
|
|
|
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::FloatConstant(_) => true,
|
2020-05-06 13:45:17 +02:00
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::IntegerConstant(_)
|
|
|
|
| Self::CharConstant(_)
|
|
|
|
| Self::StringConstant(_)
|
2020-04-06 11:47:34 +02:00
|
|
|
| Self::True(_)
|
|
|
|
| Self::False(_)
|
|
|
|
| Self::Unit(_) => true,
|
2020-03-10 04:22:41 +01:00
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
// An array literal is constant if all items are constant
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::Array(x) => x.0.iter().all(Self::is_constant),
|
2020-04-06 11:47:34 +02:00
|
|
|
|
|
|
|
// An map literal is constant if all items are constant
|
2020-05-09 18:19:13 +02:00
|
|
|
Self::Map(x) => x.0.iter().map(|(_, expr)| expr).all(Self::is_constant),
|
2020-04-06 11:47:34 +02:00
|
|
|
|
|
|
|
// Check in expression
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::In(x) => match (&x.0, &x.1) {
|
|
|
|
(Self::StringConstant(_), Self::StringConstant(_))
|
|
|
|
| (Self::CharConstant(_), Self::StringConstant(_)) => true,
|
2020-04-06 11:47:34 +02:00
|
|
|
_ => false,
|
|
|
|
},
|
2020-03-15 15:39:58 +01:00
|
|
|
|
2020-03-10 11:32:01 +01:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2020-04-17 13:00:52 +02:00
|
|
|
|
|
|
|
/// Is a particular token allowed as a postfix operator to this expression?
|
|
|
|
pub fn is_valid_postfix(&self, token: &Token) -> bool {
|
|
|
|
match self {
|
2020-05-30 04:27:48 +02:00
|
|
|
Self::Expr(x) => x.is_valid_postfix(token),
|
|
|
|
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::FloatConstant(_) => false,
|
2020-05-06 13:45:17 +02:00
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::IntegerConstant(_)
|
|
|
|
| Self::CharConstant(_)
|
|
|
|
| Self::In(_)
|
|
|
|
| Self::And(_)
|
|
|
|
| Self::Or(_)
|
2020-04-26 12:04:07 +02:00
|
|
|
| Self::True(_)
|
|
|
|
| Self::False(_)
|
2020-06-01 07:03:01 +02:00
|
|
|
| Self::Unit(_)
|
|
|
|
| Self::Assignment(_) => false,
|
2020-04-26 12:04:07 +02:00
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::StringConstant(_)
|
|
|
|
| Self::Stmt(_)
|
|
|
|
| Self::FnCall(_)
|
|
|
|
| Self::Dot(_)
|
|
|
|
| Self::Index(_)
|
|
|
|
| Self::Array(_)
|
|
|
|
| Self::Map(_) => match token {
|
2020-06-01 07:03:01 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-04-17 13:00:52 +02:00
|
|
|
Token::LeftBracket => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::Variable(_) => match token {
|
2020-06-01 07:03:01 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
Token::LeftBracket => true,
|
|
|
|
Token::LeftParen => true,
|
2020-05-04 11:43:54 +02:00
|
|
|
Token::DoubleColon => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Self::Property(_) => match token {
|
2020-06-01 07:03:01 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
|
|
|
Token::LeftBracket => true,
|
|
|
|
Token::LeftParen => true,
|
2020-04-17 13:00:52 +02:00
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-04-26 12:04:07 +02:00
|
|
|
|
2020-04-26 13:37:32 +02:00
|
|
|
/// Convert a `Variable` into a `Property`. All other variants are untouched.
|
2020-04-26 12:04:07 +02:00
|
|
|
pub(crate) fn into_property(self) -> Self {
|
|
|
|
match self {
|
2020-05-09 18:19:13 +02:00
|
|
|
Self::Variable(x) if x.1.is_none() => {
|
|
|
|
let (name, pos) = x.0;
|
2020-05-13 04:19:18 +02:00
|
|
|
let getter = make_getter(&name);
|
|
|
|
let setter = make_setter(&name);
|
|
|
|
Self::Property(Box::new(((name.clone(), getter, setter), pos)))
|
2020-05-09 18:19:13 +02:00
|
|
|
}
|
2020-04-26 12:04:07 +02:00
|
|
|
_ => self,
|
|
|
|
}
|
|
|
|
}
|
2016-04-17 04:32:18 +02:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-04-15 15:41:28 +02:00
|
|
|
/// Consume a particular 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 {
|
2020-04-23 07:24:24 +02:00
|
|
|
let (t, pos) = input.next().unwrap();
|
|
|
|
|
|
|
|
if t != token {
|
|
|
|
panic!(
|
|
|
|
"expecting {} (found {}) at {}",
|
|
|
|
token.syntax(),
|
|
|
|
t.syntax(),
|
|
|
|
pos
|
|
|
|
);
|
2020-04-15 15:41:28 +02:00
|
|
|
}
|
2020-04-23 07:24:24 +02:00
|
|
|
pos
|
2020-04-15 15:41:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Match a particular token, consuming it if matched.
|
2020-06-11 12:13:33 +02:00
|
|
|
fn match_token(input: &mut TokenStream, token: Token) -> Result<bool, ParseError> {
|
2020-04-17 14:01:41 +02:00
|
|
|
let (t, _) = input.peek().unwrap();
|
|
|
|
if *t == token {
|
|
|
|
eat_token(input, token);
|
|
|
|
Ok(true)
|
2020-04-15 15:41:28 +02:00
|
|
|
} else {
|
2020-04-17 14:01:41 +02:00
|
|
|
Ok(false)
|
2020-04-15 15:41:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-06-11 16:03:47 +02:00
|
|
|
settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Expr, ParseError> {
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2020-04-15 15:41:28 +02:00
|
|
|
if match_token(input, Token::RightParen)? {
|
2020-06-11 16:03:47 +02:00
|
|
|
return Ok(Expr::Unit(settings.pos));
|
2020-02-24 16:25:52 +01:00
|
|
|
}
|
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
let expr = parse_expr(input, state, settings.level_up())?;
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-04-17 14:01:41 +02:00
|
|
|
match input.next().unwrap() {
|
2020-03-18 03:36:50 +01:00
|
|
|
// ( xxx )
|
2020-04-17 14:01:41 +02:00
|
|
|
(Token::RightParen, _) => Ok(expr),
|
2020-04-20 18:11:25 +02:00
|
|
|
// ( <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 ???
|
2020-04-17 14:01:41 +02:00
|
|
|
(_, pos) => Err(PERR::MissingToken(
|
2020-05-04 13:36:58 +02:00
|
|
|
Token::RightParen.into(),
|
2020-04-17 14:01:41 +02:00
|
|
|
"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-06-11 12:13:33 +02:00
|
|
|
fn parse_call_expr(
|
|
|
|
input: &mut TokenStream,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-04-28 17:05:03 +02:00
|
|
|
id: String,
|
2020-05-23 18:29:06 +02:00
|
|
|
mut modules: Option<Box<ModuleRef>>,
|
2020-06-11 17:08:00 +02:00
|
|
|
settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Expr, ParseError> {
|
2020-06-11 17:08:00 +02:00
|
|
|
let (token, _) = input.peek().unwrap();
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2020-05-16 18:24:07 +02:00
|
|
|
let mut args = StaticVec::new();
|
2017-10-28 05:30:12 +02:00
|
|
|
|
2020-05-18 13:32:22 +02:00
|
|
|
match token {
|
2020-04-20 18:11:25 +02:00
|
|
|
// id <EOF>
|
2020-05-18 13:32:22 +02:00
|
|
|
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-11 16:03:47 +02:00
|
|
|
.into_err(settings.pos))
|
2020-04-06 06:29:01 +02:00
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
// id <error>
|
2020-06-14 10:56:36 +02:00
|
|
|
Token::LexError(err) => return Err(err.into_err(settings.pos)),
|
2020-04-06 06:29:01 +02:00
|
|
|
// id()
|
2020-05-18 13:32:22 +02:00
|
|
|
Token::RightParen => {
|
2020-04-15 15:41:28 +02:00
|
|
|
eat_token(input, Token::RightParen);
|
2020-05-08 05:34:56 +02:00
|
|
|
|
2020-06-11 12:13:33 +02:00
|
|
|
let hash_script = if let Some(modules) = modules.as_mut() {
|
2020-06-01 04:58:14 +02:00
|
|
|
modules.set_index(state.find_module(&modules.get(0).0));
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
let qualifiers = modules.iter().map(|(m, _)| m.as_str());
|
|
|
|
calc_fn_hash(qualifiers, &id, 0, empty())
|
|
|
|
} else {
|
|
|
|
// Qualifiers (none) + function name + no parameters.
|
|
|
|
calc_fn_hash(empty(), &id, 0, empty())
|
2020-05-09 10:15:50 +02:00
|
|
|
};
|
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
return Ok(Expr::FnCall(Box::new((
|
2020-06-11 16:03:47 +02:00
|
|
|
(id.into(), false, settings.pos),
|
2020-05-04 13:36:58 +02:00
|
|
|
modules,
|
2020-06-11 12:13:33 +02:00
|
|
|
hash_script,
|
2020-05-09 15:46:38 +02:00
|
|
|
args,
|
2020-05-01 11:32:39 +02:00
|
|
|
None,
|
2020-05-09 15:46:38 +02:00
|
|
|
))));
|
2020-04-06 06:29:01 +02:00
|
|
|
}
|
|
|
|
// id...
|
|
|
|
_ => (),
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
let settings = settings.level_up();
|
|
|
|
|
2016-02-29 22:43:45 +01:00
|
|
|
loop {
|
2020-06-11 16:03:47 +02:00
|
|
|
args.push(parse_expr(input, state, settings)?);
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-04-17 14:01:41 +02:00
|
|
|
match input.peek().unwrap() {
|
2020-05-08 05:34:56 +02:00
|
|
|
// id(...args)
|
2020-04-20 18:11:25 +02:00
|
|
|
(Token::RightParen, _) => {
|
|
|
|
eat_token(input, Token::RightParen);
|
2020-05-04 11:43:54 +02:00
|
|
|
|
2020-06-11 12:13:33 +02:00
|
|
|
let hash_script = if let Some(modules) = modules.as_mut() {
|
2020-06-01 04:58:14 +02:00
|
|
|
modules.set_index(state.find_module(&modules.get(0).0));
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
let qualifiers = modules.iter().map(|(m, _)| m.as_str());
|
|
|
|
calc_fn_hash(qualifiers, &id, args.len(), empty())
|
|
|
|
} else {
|
|
|
|
// Qualifiers (none) + function name + number of arguments.
|
|
|
|
calc_fn_hash(empty(), &id, args.len(), empty())
|
2020-05-09 10:15:50 +02:00
|
|
|
};
|
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
return Ok(Expr::FnCall(Box::new((
|
2020-06-11 16:03:47 +02:00
|
|
|
(id.into(), false, settings.pos),
|
2020-05-04 13:36:58 +02:00
|
|
|
modules,
|
2020-06-11 12:13:33 +02:00
|
|
|
hash_script,
|
2020-05-09 15:46:38 +02:00
|
|
|
args,
|
2020-05-01 11:32:39 +02:00
|
|
|
None,
|
2020-05-09 15:46:38 +02:00
|
|
|
))));
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
2020-05-08 05:34:56 +02:00
|
|
|
// id(...args,
|
2020-04-20 18:11:25 +02:00
|
|
|
(Token::Comma, _) => {
|
|
|
|
eat_token(input, Token::Comma);
|
|
|
|
}
|
2020-05-08 05:34:56 +02:00
|
|
|
// id(...args <EOF>
|
2020-04-17 14:01:41 +02:00
|
|
|
(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),
|
|
|
|
)
|
2020-04-17 14:01:41 +02:00
|
|
|
.into_err(*pos))
|
2020-04-06 06:29:01 +02:00
|
|
|
}
|
2020-05-08 05:34:56 +02:00
|
|
|
// id(...args <error>
|
2020-06-14 10:56:36 +02:00
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(*pos)),
|
2020-05-08 05:34:56 +02:00
|
|
|
// id(...args ???
|
2020-04-17 14:01:41 +02:00
|
|
|
(_, pos) => {
|
2020-03-29 07:44:27 +02:00
|
|
|
return Err(PERR::MissingToken(
|
2020-05-04 13:36:58 +02:00
|
|
|
Token::Comma.into(),
|
2020-03-29 07:44:27 +02:00
|
|
|
format!("to separate the arguments to function call '{}'", id),
|
|
|
|
)
|
2020-03-24 09:46:47 +01:00
|
|
|
.into_err(*pos))
|
2020-03-05 18:05:02 +01:00
|
|
|
}
|
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-06-11 12:13:33 +02:00
|
|
|
fn parse_index_chain(
|
|
|
|
input: &mut TokenStream,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-04-28 17:05:03 +02:00
|
|
|
lhs: Expr,
|
2020-06-11 17:08:00 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Expr, ParseError> {
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
let idx_expr = parse_expr(input, state, 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-05-09 15:46:38 +02:00
|
|
|
Expr::IntegerConstant(x) if x.0 < 0 => {
|
2020-03-24 09:46:47 +01:00
|
|
|
return Err(PERR::MalformedIndexExpr(format!(
|
|
|
|
"Array access expects non-negative index: {} < 0",
|
2020-05-09 15:46:38 +02:00
|
|
|
x.0
|
2020-03-09 03:41:17 +01:00
|
|
|
))
|
2020-05-09 15:46:38 +02:00
|
|
|
.into_err(x.1))
|
2020-03-09 03:41:17 +01:00
|
|
|
}
|
2020-05-09 15:46:38 +02:00
|
|
|
Expr::IntegerConstant(x) => match lhs {
|
|
|
|
Expr::Array(_) | Expr::StringConstant(_) => (),
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2020-05-09 15:46:38 +02: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-05-09 15:46:38 +02:00
|
|
|
.into_err(x.1))
|
2020-03-29 17:53:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-05-25 14:14:31 +02:00
|
|
|
Expr::FloatConstant(_) => {
|
2020-05-06 13:45:17 +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-05-06 13:45:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:14:31 +02:00
|
|
|
Expr::CharConstant(_)
|
|
|
|
| Expr::Assignment(_)
|
|
|
|
| Expr::And(_)
|
|
|
|
| Expr::Or(_)
|
|
|
|
| Expr::In(_)
|
|
|
|
| Expr::True(_)
|
|
|
|
| Expr::False(_)
|
|
|
|
| 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-05-09 15:46:38 +02:00
|
|
|
Expr::StringConstant(x) => match lhs {
|
|
|
|
Expr::Map(_) => (),
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2020-05-09 15:46:38 +02: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-05-09 15:46:38 +02:00
|
|
|
.into_err(x.1))
|
2020-03-29 17:53:35 +02:00
|
|
|
}
|
2020-05-06 13:45:17 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-05-25 14:14:31 +02:00
|
|
|
Expr::FloatConstant(_) => {
|
2020-05-09 15:46:38 +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-05-06 13:45:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:14:31 +02:00
|
|
|
Expr::CharConstant(_)
|
|
|
|
| Expr::Assignment(_)
|
|
|
|
| Expr::And(_)
|
|
|
|
| Expr::Or(_)
|
|
|
|
| Expr::In(_)
|
|
|
|
| Expr::True(_)
|
|
|
|
| Expr::False(_)
|
|
|
|
| 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]
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-05-25 14:14:31 +02: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-05-25 14:14:31 +02: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()))
|
2020-05-09 15:46:38 +02:00
|
|
|
}
|
|
|
|
// lhs[??? = ??? ]
|
2020-05-25 14:14:31 +02:00
|
|
|
x @ Expr::Assignment(_) => {
|
2020-05-09 15:46:38 +02:00
|
|
|
return Err(PERR::MalformedIndexExpr(
|
2020-05-25 14:14:31 +02:00
|
|
|
"Array access expects integer index, not an assignment".into(),
|
2020-05-09 15:46:38 +02:00
|
|
|
)
|
2020-05-25 14:14:31 +02:00
|
|
|
.into_err(x.position()))
|
2020-03-09 03:41:17 +01:00
|
|
|
}
|
2020-05-09 15:46:38 +02:00
|
|
|
// 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
|
|
|
}
|
2020-05-09 15:46:38 +02:00
|
|
|
// lhs[??? && ???], lhs[??? || ???], lhs[??? in ???]
|
2020-05-25 14:14:31 +02:00
|
|
|
x @ Expr::And(_) | x @ Expr::Or(_) | x @ Expr::In(_) => {
|
2020-05-09 15:46:38 +02: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-05-09 15:46:38 +02:00
|
|
|
}
|
|
|
|
// lhs[true], lhs[false]
|
2020-05-25 14:14:31 +02:00
|
|
|
x @ Expr::True(_) | x @ Expr::False(_) => {
|
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
|
2020-04-17 14:01:41 +02:00
|
|
|
match input.peek().unwrap() {
|
|
|
|
(Token::RightBracket, _) => {
|
2020-04-15 15:41:28 +02:00
|
|
|
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
|
2020-06-11 16:03:47 +02:00
|
|
|
let idx_expr = parse_index_chain(input, state, idx_expr, settings.level_up())?;
|
2020-04-26 13:37:32 +02:00
|
|
|
// Indexing binds to right
|
2020-06-11 17:08:00 +02:00
|
|
|
Ok(Expr::Index(Box::new((lhs, 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
|
2020-05-30 04:27:48 +02:00
|
|
|
_ => {
|
|
|
|
match idx_expr {
|
|
|
|
// Terminate with an `Expr::Expr` wrapper to prevent the last index expression
|
|
|
|
// inside brackets to be mis-parsed as another level of indexing, or a
|
|
|
|
// dot expression/function call to be mis-parsed as following the indexing chain.
|
|
|
|
Expr::Index(_) | Expr::Dot(_) | Expr::FnCall(_) => Ok(Expr::Index(
|
2020-06-11 16:03:47 +02:00
|
|
|
Box::new((lhs, Expr::Expr(Box::new(idx_expr)), settings.pos)),
|
2020-05-30 04:27:48 +02:00
|
|
|
)),
|
2020-06-11 16:03:47 +02:00
|
|
|
_ => Ok(Expr::Index(Box::new((lhs, idx_expr, settings.pos)))),
|
2020-05-30 04:27:48 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-26 12:04:07 +02:00
|
|
|
}
|
2020-03-05 18:05:02 +01:00
|
|
|
}
|
2020-06-14 10:56:36 +02:00
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(*pos)),
|
2020-04-17 14:01:41 +02:00
|
|
|
(_, pos) => Err(PERR::MissingToken(
|
2020-05-04 13:36:58 +02:00
|
|
|
Token::RightBracket.into(),
|
2020-04-17 14:01:41 +02:00
|
|
|
"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-06-11 12:13:33 +02:00
|
|
|
fn parse_array_literal(
|
|
|
|
input: &mut TokenStream,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-06-11 16:03:47 +02:00
|
|
|
settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Expr, ParseError> {
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2020-05-16 18:24:07 +02:00
|
|
|
let mut arr = StaticVec::new();
|
2016-03-26 18:46:28 +01:00
|
|
|
|
2020-04-15 15:41:28 +02:00
|
|
|
if !match_token(input, Token::RightBracket)? {
|
2020-04-17 14:01:41 +02:00
|
|
|
while !input.peek().unwrap().0.is_eof() {
|
2020-06-14 08:25:47 +02:00
|
|
|
if state.max_array_size > 0 && arr.len() >= state.max_array_size {
|
|
|
|
return Err(PERR::LiteralTooLarge(
|
|
|
|
"Size of array literal".to_string(),
|
|
|
|
state.max_array_size,
|
|
|
|
)
|
|
|
|
.into_err(input.peek().unwrap().1));
|
|
|
|
}
|
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
let expr = parse_expr(input, state, settings.level_up())?;
|
2020-06-01 07:26:20 +02:00
|
|
|
arr.push(expr);
|
2020-03-02 15:13:14 +01:00
|
|
|
|
2020-04-17 14:01:41 +02:00
|
|
|
match input.peek().unwrap() {
|
2020-04-20 18:11:25 +02:00
|
|
|
(Token::Comma, _) => eat_token(input, Token::Comma),
|
|
|
|
(Token::RightBracket, _) => {
|
|
|
|
eat_token(input, Token::RightBracket);
|
|
|
|
break;
|
|
|
|
}
|
2020-04-17 14:01:41 +02:00
|
|
|
(Token::EOF, pos) => {
|
2020-05-04 13:36:58 +02:00
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::RightBracket.into(),
|
|
|
|
"to end this array literal".into(),
|
2020-04-17 14:01:41 +02:00
|
|
|
)
|
2020-05-04 13:36:58 +02:00
|
|
|
.into_err(*pos))
|
2020-04-17 14:01:41 +02:00
|
|
|
}
|
2020-06-14 10:56:36 +02:00
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(*pos)),
|
2020-04-17 14:01:41 +02:00
|
|
|
(_, pos) => {
|
2020-03-29 07:44:27 +02:00
|
|
|
return Err(PERR::MissingToken(
|
2020-05-04 13:36:58 +02:00
|
|
|
Token::Comma.into(),
|
2020-03-29 17:53:35 +02:00
|
|
|
"to separate the items of this array literal".into(),
|
2020-03-27 16:47:23 +01:00
|
|
|
)
|
2020-03-29 07:44:27 +02:00
|
|
|
.into_err(*pos))
|
2020-03-03 09:23:55 +01:00
|
|
|
}
|
2020-04-06 11:47:34 +02:00
|
|
|
};
|
2016-04-14 03:01:08 +02:00
|
|
|
}
|
2016-03-26 18:46:28 +01:00
|
|
|
}
|
|
|
|
|
2020-06-11 16:03:47 +02: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-06-11 12:13:33 +02:00
|
|
|
fn parse_map_literal(
|
|
|
|
input: &mut TokenStream,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-06-11 16:03:47 +02:00
|
|
|
settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Expr, ParseError> {
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2020-05-16 18:24:07 +02:00
|
|
|
let mut map = StaticVec::new();
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2020-04-15 15:41:28 +02:00
|
|
|
if !match_token(input, Token::RightBrace)? {
|
2020-04-17 14:01:41 +02:00
|
|
|
while !input.peek().unwrap().0.is_eof() {
|
2020-04-15 15:41:28 +02:00
|
|
|
const MISSING_RBRACE: &str = "to end this object map literal";
|
|
|
|
|
2020-04-17 14:01:41 +02:00
|
|
|
let (name, pos) = match input.next().unwrap() {
|
|
|
|
(Token::Identifier(s), pos) => (s, pos),
|
|
|
|
(Token::StringConst(s), pos) => (s, pos),
|
2020-06-14 10:56:36 +02:00
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
2020-04-17 14:01:41 +02:00
|
|
|
(_, pos) if map.is_empty() => {
|
2020-05-04 13:36:58 +02:00
|
|
|
return Err(
|
|
|
|
PERR::MissingToken(Token::RightBrace.into(), MISSING_RBRACE.into())
|
|
|
|
.into_err(pos),
|
|
|
|
)
|
2020-04-15 15:41:28 +02:00
|
|
|
}
|
2020-04-17 14:01:41 +02:00
|
|
|
(Token::EOF, pos) => {
|
2020-05-04 13:36:58 +02:00
|
|
|
return Err(
|
|
|
|
PERR::MissingToken(Token::RightBrace.into(), MISSING_RBRACE.into())
|
|
|
|
.into_err(pos),
|
|
|
|
)
|
2020-03-29 17:53:35 +02:00
|
|
|
}
|
2020-04-17 14:01:41 +02:00
|
|
|
(_, pos) => return Err(PERR::PropertyExpected.into_err(pos)),
|
2020-03-29 17:53:35 +02:00
|
|
|
};
|
|
|
|
|
2020-04-17 14:01:41 +02:00
|
|
|
match input.next().unwrap() {
|
|
|
|
(Token::Colon, _) => (),
|
2020-06-14 10:56:36 +02:00
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
2020-04-17 14:01:41 +02:00
|
|
|
(_, pos) => {
|
2020-03-29 17:53:35 +02:00
|
|
|
return Err(PERR::MissingToken(
|
2020-05-04 13:36:58 +02:00
|
|
|
Token::Colon.into(),
|
2020-03-29 17:53:35 +02:00
|
|
|
format!(
|
|
|
|
"to follow the property '{}' in this object map literal",
|
|
|
|
name
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.into_err(pos))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-14 08:25:47 +02:00
|
|
|
if state.max_map_size > 0 && map.len() >= state.max_map_size {
|
|
|
|
return Err(PERR::LiteralTooLarge(
|
|
|
|
"Number of properties in object map literal".to_string(),
|
|
|
|
state.max_map_size,
|
|
|
|
)
|
|
|
|
.into_err(input.peek().unwrap().1));
|
|
|
|
}
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2020-06-14 08:25:47 +02:00
|
|
|
let expr = parse_expr(input, state, settings.level_up())?;
|
2020-05-09 18:19:13 +02:00
|
|
|
map.push(((name, pos), expr));
|
2020-03-29 17:53:35 +02:00
|
|
|
|
2020-04-17 14:01:41 +02:00
|
|
|
match input.peek().unwrap() {
|
|
|
|
(Token::Comma, _) => {
|
2020-04-17 13:00:52 +02:00
|
|
|
eat_token(input, Token::Comma);
|
|
|
|
}
|
2020-04-17 14:01:41 +02:00
|
|
|
(Token::RightBrace, _) => {
|
2020-04-15 15:41:28 +02:00
|
|
|
eat_token(input, Token::RightBrace);
|
|
|
|
break;
|
2020-03-29 17:53:35 +02:00
|
|
|
}
|
2020-04-17 14:01:41 +02:00
|
|
|
(Token::Identifier(_), pos) => {
|
2020-03-29 17:53:35 +02:00
|
|
|
return Err(PERR::MissingToken(
|
2020-05-04 13:36:58 +02:00
|
|
|
Token::Comma.into(),
|
2020-03-29 17:53:35 +02:00
|
|
|
"to separate the items of this object map literal".into(),
|
|
|
|
)
|
|
|
|
.into_err(*pos))
|
|
|
|
}
|
2020-06-14 10:56:36 +02:00
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(*pos)),
|
2020-04-17 14:01:41 +02:00
|
|
|
(_, pos) => {
|
2020-05-04 13:36:58 +02:00
|
|
|
return Err(
|
|
|
|
PERR::MissingToken(Token::RightBrace.into(), MISSING_RBRACE.into())
|
|
|
|
.into_err(*pos),
|
|
|
|
)
|
2020-04-15 15:41:28 +02:00
|
|
|
}
|
2020-03-29 17:53:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for duplicating properties
|
|
|
|
map.iter()
|
|
|
|
.enumerate()
|
2020-05-09 18:19:13 +02:00
|
|
|
.try_for_each(|(i, ((k1, _), _))| {
|
2020-03-29 17:53:35 +02:00
|
|
|
map.iter()
|
|
|
|
.skip(i + 1)
|
2020-05-09 18:19:13 +02:00
|
|
|
.find(|((k2, _), _)| k2 == k1)
|
|
|
|
.map_or_else(|| Ok(()), |((k2, pos), _)| Err((k2, *pos)))
|
2020-03-29 17:53:35 +02:00
|
|
|
})
|
|
|
|
.map_err(|(key, pos)| PERR::DuplicatedProperty(key.to_string()).into_err(pos))?;
|
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
Ok(Expr::Map(Box::new((map, settings.pos))))
|
2020-03-29 17:53:35 +02:00
|
|
|
}
|
|
|
|
|
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,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Expr, ParseError> {
|
2020-06-11 16:03:47 +02:00
|
|
|
let (token, pos1) = input.peek().unwrap();
|
|
|
|
settings.pos = *pos1;
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
|
|
|
let (token, _) = match token {
|
2020-03-20 12:50:58 +01:00
|
|
|
// { - block statement as expression
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::LeftBrace if settings.allow_stmt_expr => {
|
|
|
|
return parse_block(input, state, settings.level_up())
|
|
|
|
.map(|block| Expr::Stmt(Box::new((block, settings.pos))))
|
2020-03-07 03:39:00 +01:00
|
|
|
}
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::EOF => return Err(PERR::UnexpectedEOF.into_err(settings.pos)),
|
2020-04-17 14:01:41 +02:00
|
|
|
_ => input.next().unwrap(),
|
2020-03-20 12:50:58 +01:00
|
|
|
};
|
2020-03-05 13:28:03 +01:00
|
|
|
|
2020-03-20 12:50:58 +01:00
|
|
|
let mut root_expr = match token {
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::IntegerConstant(x) => Expr::IntegerConstant(Box::new((x, settings.pos))),
|
2020-04-17 14:08:41 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::FloatConstant(x) => Expr::FloatConstant(Box::new((x, settings.pos))),
|
|
|
|
Token::CharConstant(c) => Expr::CharConstant(Box::new((c, settings.pos))),
|
|
|
|
Token::StringConst(s) => Expr::StringConstant(Box::new((s.into(), settings.pos))),
|
2020-04-28 17:05:03 +02:00
|
|
|
Token::Identifier(s) => {
|
2020-05-18 13:32:22 +02:00
|
|
|
let index = state.find(&s);
|
2020-06-11 16:03:47 +02:00
|
|
|
Expr::Variable(Box::new(((s, settings.pos), None, 0, index)))
|
2020-04-28 17:05:03 +02:00
|
|
|
}
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::LeftParen => parse_paren_expr(input, state, settings.level_up())?,
|
2020-03-17 10:33:37 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::LeftBracket => parse_array_literal(input, state, settings.level_up())?,
|
2020-03-29 17:53:35 +02:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::MapStart => parse_map_literal(input, state, settings.level_up())?,
|
|
|
|
Token::True => Expr::True(settings.pos),
|
|
|
|
Token::False => Expr::False(settings.pos),
|
2020-06-14 10:56:36 +02:00
|
|
|
Token::LexError(err) => return Err(err.into_err(settings.pos)),
|
2020-06-14 08:25:47 +02:00
|
|
|
_ => {
|
2020-06-11 16:03:47 +02:00
|
|
|
return Err(
|
|
|
|
PERR::BadInput(format!("Unexpected '{}'", token.syntax())).into_err(settings.pos)
|
|
|
|
)
|
2020-04-17 13:00:52 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Tail processing all possible postfix operators
|
2020-04-17 14:01:41 +02:00
|
|
|
loop {
|
|
|
|
let (token, _) = input.peek().unwrap();
|
|
|
|
|
2020-04-17 13:00:52 +02:00
|
|
|
if !root_expr.is_valid_postfix(token) {
|
|
|
|
break;
|
2020-03-24 09:46:47 +01:00
|
|
|
}
|
2020-03-05 13:28:03 +01:00
|
|
|
|
2020-05-04 11:43:54 +02:00
|
|
|
let (token, token_pos) = input.next().unwrap();
|
2020-06-11 17:08:00 +02:00
|
|
|
settings.pos = token_pos;
|
2020-04-10 06:16:39 +02:00
|
|
|
|
2020-04-17 13:00:52 +02:00
|
|
|
root_expr = match (root_expr, token) {
|
|
|
|
// Function call
|
2020-05-09 15:46:38 +02:00
|
|
|
(Expr::Variable(x), Token::LeftParen) => {
|
2020-05-09 18:19:13 +02:00
|
|
|
let ((name, pos), modules, _, _) = *x;
|
2020-06-11 17:08:00 +02:00
|
|
|
settings.pos = pos;
|
2020-06-11 16:03:47 +02:00
|
|
|
parse_call_expr(input, state, name, modules, settings.level_up())?
|
2020-05-01 11:32:39 +02:00
|
|
|
}
|
2020-05-12 04:20:29 +02:00
|
|
|
(Expr::Property(_), _) => unreachable!(),
|
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() {
|
2020-05-09 15:46:38 +02:00
|
|
|
(Token::Identifier(id2), pos2) => {
|
2020-05-09 18:19:13 +02:00
|
|
|
let ((name, pos), mut modules, _, index) = *x;
|
2020-06-11 17:08:00 +02:00
|
|
|
|
2020-05-09 18:19:13 +02:00
|
|
|
if let Some(ref mut modules) = modules {
|
|
|
|
modules.push((name, pos));
|
2020-05-09 15:46:38 +02:00
|
|
|
} else {
|
|
|
|
let mut m: ModuleRef = Default::default();
|
2020-05-09 18:19:13 +02:00
|
|
|
m.push((name, pos));
|
|
|
|
modules = Some(Box::new(m));
|
2020-05-04 11:43:54 +02:00
|
|
|
}
|
2020-05-09 15:46:38 +02:00
|
|
|
|
2020-05-09 18:19:13 +02:00
|
|
|
Expr::Variable(Box::new(((id2, pos2), modules, 0, index)))
|
2020-05-04 11:43:54 +02:00
|
|
|
}
|
2020-05-09 15:46:38 +02:00
|
|
|
(_, pos2) => return Err(PERR::VariableExpected.into_err(pos2)),
|
|
|
|
},
|
2020-04-17 13:00:52 +02:00
|
|
|
// Indexing
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-04-28 17:05:03 +02:00
|
|
|
(expr, Token::LeftBracket) => {
|
2020-06-11 16:03:47 +02:00
|
|
|
parse_index_chain(input, state, expr, settings.level_up())?
|
2020-04-28 17:05:03 +02:00
|
|
|
}
|
2020-04-17 13:00:52 +02:00
|
|
|
// Unknown postfix operator
|
2020-06-01 07:03:01 +02:00
|
|
|
(expr, token) => panic!(
|
|
|
|
"unknown postfix operator '{}' for {:?}",
|
|
|
|
token.syntax(),
|
|
|
|
expr
|
|
|
|
),
|
2020-03-10 10:10:33 +01:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
2020-03-06 03:50:52 +01:00
|
|
|
|
2020-05-07 18:19:08 +02:00
|
|
|
match &mut root_expr {
|
2020-05-09 04:00:59 +02:00
|
|
|
// Cache the hash key for module-qualified variables
|
2020-05-09 15:46:38 +02:00
|
|
|
Expr::Variable(x) if x.1.is_some() => {
|
2020-05-09 18:19:13 +02:00
|
|
|
let ((name, _), modules, hash, _) = x.as_mut();
|
|
|
|
let modules = modules.as_mut().unwrap();
|
2020-05-09 15:46:38 +02:00
|
|
|
|
2020-05-09 04:00:59 +02:00
|
|
|
// Qualifiers + variable name
|
2020-05-19 14:07:51 +02:00
|
|
|
*hash = calc_fn_hash(modules.iter().map(|(v, _)| v.as_str()), name, 0, empty());
|
2020-05-18 13:32:22 +02:00
|
|
|
modules.set_index(state.find_module(&modules.get(0).0));
|
2020-05-07 18:19:08 +02:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2020-03-06 03:50:52 +01:00
|
|
|
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,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Expr, ParseError> {
|
2020-06-11 16:03:47 +02:00
|
|
|
let (token, token_pos) = input.peek().unwrap();
|
|
|
|
settings.pos = *token_pos;
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
|
|
|
match token {
|
2020-03-27 16:47:23 +01:00
|
|
|
// If statement is allowed to act as expressions
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::If if settings.allow_if_expr => Ok(Expr::Stmt(Box::new((
|
|
|
|
parse_if(input, state, settings.level_up())?,
|
|
|
|
settings.pos,
|
2020-05-18 13:32:22 +02:00
|
|
|
)))),
|
2020-03-18 03:36:50 +01:00
|
|
|
// -expr
|
2020-05-18 13:32:22 +02:00
|
|
|
Token::UnaryMinus => {
|
2020-04-17 13:00:52 +02:00
|
|
|
let pos = eat_token(input, Token::UnaryMinus);
|
2020-03-02 15:13:14 +01:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
match parse_unary(input, state, settings.level_up())? {
|
2020-03-09 03:10:19 +01:00
|
|
|
// Negative integer
|
2020-05-09 15:46:38 +02:00
|
|
|
Expr::IntegerConstant(x) => {
|
|
|
|
let (num, pos) = *x;
|
|
|
|
|
|
|
|
num.checked_neg()
|
|
|
|
.map(|i| Expr::IntegerConstant(Box::new((i, pos))))
|
|
|
|
.or_else(|| {
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
{
|
|
|
|
Some(Expr::FloatConstant(Box::new((-(x.0 as FLOAT), pos))))
|
|
|
|
}
|
|
|
|
#[cfg(feature = "no_float")]
|
|
|
|
{
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2020-06-14 10:56:36 +02:00
|
|
|
.ok_or_else(|| LexError::MalformedNumber(format!("-{}", x.0)).into_err(pos))
|
2020-05-09 15:46:38 +02:00
|
|
|
}
|
2020-03-10 11:32:01 +01:00
|
|
|
|
2020-03-09 03:10:19 +01:00
|
|
|
// Negative float
|
2020-03-10 11:32:01 +01:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-05-09 15:46:38 +02:00
|
|
|
Expr::FloatConstant(x) => Ok(Expr::FloatConstant(Box::new((-x.0, x.1)))),
|
2020-03-10 11:32:01 +01:00
|
|
|
|
2020-03-09 03:10:19 +01:00
|
|
|
// Call negative function
|
2020-05-16 18:24:07 +02:00
|
|
|
expr => {
|
2020-05-09 10:15:50 +02:00
|
|
|
let op = "-";
|
2020-05-19 14:07:51 +02:00
|
|
|
let hash = calc_fn_hash(empty(), op, 2, empty());
|
2020-05-16 18:24:07 +02:00
|
|
|
let mut args = StaticVec::new();
|
|
|
|
args.push(expr);
|
2020-05-09 10:15:50 +02:00
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Ok(Expr::FnCall(Box::new((
|
2020-05-23 12:59:28 +02:00
|
|
|
(op.into(), true, pos),
|
2020-05-09 10:15:50 +02:00
|
|
|
None,
|
|
|
|
hash,
|
2020-05-16 18:24:07 +02:00
|
|
|
args,
|
2020-05-09 10:15:50 +02:00
|
|
|
None,
|
2020-05-09 15:46:38 +02:00
|
|
|
))))
|
2020-05-09 10:15:50 +02:00
|
|
|
}
|
2020-03-09 03:10:19 +01:00
|
|
|
}
|
2019-09-18 12:21:07 +02:00
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// +expr
|
2020-05-18 13:32:22 +02:00
|
|
|
Token::UnaryPlus => {
|
2020-04-15 15:41:28 +02:00
|
|
|
eat_token(input, Token::UnaryPlus);
|
2020-06-11 16:03:47 +02:00
|
|
|
parse_unary(input, state, settings.level_up())
|
2019-09-18 12:21:07 +02:00
|
|
|
}
|
2020-03-18 03:36:50 +01:00
|
|
|
// !expr
|
2020-05-18 13:32:22 +02:00
|
|
|
Token::Bang => {
|
2020-04-17 13:00:52 +02:00
|
|
|
let pos = eat_token(input, Token::Bang);
|
2020-05-16 18:24:07 +02:00
|
|
|
let mut args = StaticVec::new();
|
2020-06-11 16:03:47 +02:00
|
|
|
let expr = parse_primary(input, state, settings.level_up())?;
|
2020-06-01 07:26:20 +02:00
|
|
|
args.push(expr);
|
2020-05-09 18:19:13 +02:00
|
|
|
|
2020-05-09 10:15:50 +02:00
|
|
|
let op = "!";
|
2020-05-19 14:07:51 +02:00
|
|
|
let hash = calc_fn_hash(empty(), op, 2, empty());
|
2020-05-09 10:15:50 +02:00
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Ok(Expr::FnCall(Box::new((
|
2020-05-23 12:59:28 +02:00
|
|
|
(op.into(), true, pos),
|
2020-05-04 11:43:54 +02:00
|
|
|
None,
|
2020-05-09 10:15:50 +02:00
|
|
|
hash,
|
2020-05-16 18:24:07 +02:00
|
|
|
args,
|
2020-05-09 18:19:13 +02:00
|
|
|
Some(false.into()), // NOT operator, when operating on invalid operand, defaults to false
|
2020-05-09 15:46:38 +02:00
|
|
|
))))
|
2019-09-18 12:21:07 +02:00
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
// <EOF>
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::EOF => Err(PERR::UnexpectedEOF.into_err(settings.pos)),
|
2020-04-17 14:01:41 +02:00
|
|
|
// All other tokens
|
2020-06-11 16:03:47 +02:00
|
|
|
_ => parse_primary(input, state, settings.level_up()),
|
2017-10-30 16:08:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 06:25:09 +02:00
|
|
|
fn make_assignment_stmt<'a>(
|
2020-05-25 14:14:31 +02:00
|
|
|
fn_name: Cow<'static, str>,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-04-22 11:37:06 +02:00
|
|
|
lhs: Expr,
|
2020-05-07 06:25:09 +02:00
|
|
|
rhs: Expr,
|
|
|
|
pos: Position,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Expr, ParseError> {
|
2020-05-07 06:25:09 +02:00
|
|
|
match &lhs {
|
2020-06-11 17:21:39 +02:00
|
|
|
// var (non-indexed) = rhs
|
2020-05-25 14:14:31 +02:00
|
|
|
Expr::Variable(x) if x.3.is_none() => {
|
|
|
|
Ok(Expr::Assignment(Box::new((lhs, fn_name.into(), rhs, pos))))
|
|
|
|
}
|
2020-06-11 17:21:39 +02:00
|
|
|
// var (indexed) = rhs
|
2020-05-09 15:46:38 +02:00
|
|
|
Expr::Variable(x) => {
|
2020-05-09 18:19:13 +02:00
|
|
|
let ((name, name_pos), _, _, index) = x.as_ref();
|
2020-05-18 13:32:22 +02:00
|
|
|
match state.stack[(state.len() - index.unwrap().get())].1 {
|
2020-05-25 14:14:31 +02:00
|
|
|
ScopeEntryType::Normal => {
|
|
|
|
Ok(Expr::Assignment(Box::new((lhs, fn_name.into(), rhs, pos))))
|
|
|
|
}
|
2020-05-07 06:25:09 +02:00
|
|
|
// Constant values cannot be assigned to
|
|
|
|
ScopeEntryType::Constant => {
|
2020-05-09 18:19:13 +02:00
|
|
|
Err(PERR::AssignmentToConstant(name.clone()).into_err(*name_pos))
|
2020-05-07 06:25:09 +02:00
|
|
|
}
|
|
|
|
ScopeEntryType::Module => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2020-06-11 17:21:39 +02:00
|
|
|
// xxx[???] = rhs, xxx.??? = rhs
|
2020-05-09 15:46:38 +02:00
|
|
|
Expr::Index(x) | Expr::Dot(x) => match &x.0 {
|
2020-06-11 17:21:39 +02:00
|
|
|
// var[???] (non-indexed) = rhs, var.??? (non-indexed) = rhs
|
2020-05-25 14:14:31 +02:00
|
|
|
Expr::Variable(x) if x.3.is_none() => {
|
|
|
|
Ok(Expr::Assignment(Box::new((lhs, fn_name.into(), rhs, pos))))
|
|
|
|
}
|
2020-06-11 17:21:39 +02:00
|
|
|
// var[???] (indexed) = rhs, var.??? (indexed) = rhs
|
2020-05-09 15:46:38 +02:00
|
|
|
Expr::Variable(x) => {
|
2020-05-09 18:19:13 +02:00
|
|
|
let ((name, name_pos), _, _, index) = x.as_ref();
|
2020-05-18 13:32:22 +02:00
|
|
|
match state.stack[(state.len() - index.unwrap().get())].1 {
|
2020-05-25 14:14:31 +02:00
|
|
|
ScopeEntryType::Normal => {
|
|
|
|
Ok(Expr::Assignment(Box::new((lhs, fn_name.into(), rhs, pos))))
|
|
|
|
}
|
2020-05-07 06:25:09 +02:00
|
|
|
// Constant values cannot be assigned to
|
|
|
|
ScopeEntryType::Constant => {
|
2020-05-09 18:19:13 +02:00
|
|
|
Err(PERR::AssignmentToConstant(name.clone()).into_err(*name_pos))
|
2020-05-07 06:25:09 +02:00
|
|
|
}
|
|
|
|
ScopeEntryType::Module => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2020-06-11 17:21:39 +02:00
|
|
|
// expr[???] = rhs, expr.??? = rhs
|
2020-05-09 15:46:38 +02:00
|
|
|
_ => Err(PERR::AssignmentToCopy.into_err(x.0.position())),
|
2020-05-07 06:25:09 +02:00
|
|
|
},
|
2020-06-11 17:21:39 +02:00
|
|
|
// const_expr = rhs
|
2020-05-07 06:25:09 +02:00
|
|
|
expr if expr.is_constant() => {
|
|
|
|
Err(PERR::AssignmentToConstant("".into()).into_err(lhs.position()))
|
|
|
|
}
|
2020-06-11 17:21:39 +02:00
|
|
|
// ??? && ??? = rhs, ??? || ??? = rhs
|
|
|
|
Expr::And(_) | Expr::Or(_) => {
|
|
|
|
Err(PERR::BadInput("Possibly a typo of '=='?".to_string()).into_err(pos))
|
|
|
|
}
|
|
|
|
// expr = rhs
|
2020-05-07 06:25:09 +02:00
|
|
|
_ => Err(PERR::AssignmentToCopy.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,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-04-06 06:29:01 +02:00
|
|
|
lhs: Expr,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Expr, ParseError> {
|
2020-06-11 16:03:47 +02:00
|
|
|
let (token, token_pos) = input.peek().unwrap();
|
|
|
|
settings.pos = *token_pos;
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
|
|
|
let op = match token {
|
2020-05-25 14:14:31 +02:00
|
|
|
Token::Equals => "".into(),
|
2020-05-27 07:22:10 +02:00
|
|
|
|
|
|
|
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-05-18 13:32:22 +02:00
|
|
|
|
|
|
|
_ => return Ok(lhs),
|
2020-04-22 11:37:06 +02:00
|
|
|
};
|
|
|
|
|
2020-05-25 14:14:31 +02:00
|
|
|
let (_, pos) = input.next().unwrap();
|
2020-06-11 16:03:47 +02:00
|
|
|
let rhs = parse_expr(input, state, 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-05-30 18:02:23 +02:00
|
|
|
fn make_dot_expr(lhs: Expr, rhs: Expr, op_pos: Position) -> Result<Expr, ParseError> {
|
2020-05-04 11:43:54 +02:00
|
|
|
Ok(match (lhs, rhs) {
|
2020-05-30 18:02:23 +02:00
|
|
|
// idx_lhs[idx_expr].rhs
|
2020-04-26 13:37:32 +02:00
|
|
|
// Attach dot chain to the bottom level of indexing chain
|
2020-05-09 15:46:38 +02:00
|
|
|
(Expr::Index(x), rhs) => {
|
2020-05-30 18:02:23 +02:00
|
|
|
let (idx_lhs, idx_expr, pos) = *x;
|
|
|
|
Expr::Index(Box::new((
|
|
|
|
idx_lhs,
|
|
|
|
make_dot_expr(idx_expr, rhs, op_pos)?,
|
|
|
|
pos,
|
|
|
|
)))
|
2020-05-09 15:46:38 +02:00
|
|
|
}
|
2020-04-26 12:04:07 +02:00
|
|
|
// lhs.id
|
2020-05-09 15:46:38 +02:00
|
|
|
(lhs, Expr::Variable(x)) if x.1.is_none() => {
|
2020-05-13 04:19:18 +02:00
|
|
|
let (name, pos) = x.0;
|
|
|
|
|
|
|
|
let getter = make_getter(&name);
|
|
|
|
let setter = make_setter(&name);
|
|
|
|
let rhs = Expr::Property(Box::new(((name, getter, setter), pos)));
|
|
|
|
|
2020-05-09 18:19:13 +02:00
|
|
|
Expr::Dot(Box::new((lhs, rhs, op_pos)))
|
2020-05-09 15:46:38 +02:00
|
|
|
}
|
2020-05-04 13:36:58 +02:00
|
|
|
// lhs.module::id - syntax error
|
2020-05-09 15:46:38 +02:00
|
|
|
(_, Expr::Variable(x)) if x.1.is_some() => {
|
2020-05-17 16:19:49 +02:00
|
|
|
return Err(PERR::PropertyExpected.into_err(x.1.unwrap().get(0).1));
|
2020-05-04 11:43:54 +02:00
|
|
|
}
|
2020-05-30 18:02:23 +02:00
|
|
|
// lhs.prop
|
|
|
|
(lhs, prop @ Expr::Property(_)) => Expr::Dot(Box::new((lhs, prop, op_pos))),
|
2020-04-26 12:04:07 +02:00
|
|
|
// lhs.dot_lhs.dot_rhs
|
2020-05-09 18:19:13 +02:00
|
|
|
(lhs, Expr::Dot(x)) => {
|
|
|
|
let (dot_lhs, dot_rhs, pos) = *x;
|
|
|
|
Expr::Dot(Box::new((
|
|
|
|
lhs,
|
2020-05-30 18:02:23 +02:00
|
|
|
Expr::Dot(Box::new((dot_lhs.into_property(), dot_rhs, pos))),
|
2020-05-09 18:19:13 +02:00
|
|
|
op_pos,
|
|
|
|
)))
|
|
|
|
}
|
2020-04-26 12:04:07 +02:00
|
|
|
// lhs.idx_lhs[idx_rhs]
|
2020-05-09 18:19:13 +02:00
|
|
|
(lhs, Expr::Index(x)) => {
|
2020-05-30 18:02:23 +02:00
|
|
|
let (dot_lhs, dot_rhs, pos) = *x;
|
2020-05-09 18:19:13 +02:00
|
|
|
Expr::Dot(Box::new((
|
|
|
|
lhs,
|
2020-05-30 18:02:23 +02:00
|
|
|
Expr::Index(Box::new((dot_lhs.into_property(), dot_rhs, pos))),
|
2020-05-09 18:19:13 +02:00
|
|
|
op_pos,
|
|
|
|
)))
|
|
|
|
}
|
2020-05-30 18:02:23 +02:00
|
|
|
// lhs.func()
|
|
|
|
(lhs, func @ Expr::FnCall(_)) => Expr::Dot(Box::new((lhs, func, op_pos))),
|
2020-04-26 12:04:07 +02:00
|
|
|
// lhs.rhs
|
2020-05-30 18:02:23 +02:00
|
|
|
_ => unreachable!(),
|
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.
|
2020-05-22 12:28:13 +02:00
|
|
|
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-05-25 14:14:31 +02:00
|
|
|
(_, x @ Expr::IntegerConstant(_))
|
|
|
|
| (_, x @ Expr::And(_))
|
|
|
|
| (_, x @ Expr::Or(_))
|
|
|
|
| (_, x @ Expr::In(_))
|
|
|
|
| (_, x @ Expr::Assignment(_))
|
|
|
|
| (_, x @ Expr::True(_))
|
|
|
|
| (_, x @ Expr::False(_))
|
|
|
|
| (_, 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
|
|
|
}
|
|
|
|
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-05-25 14:14:31 +02:00
|
|
|
(_, x @ Expr::FloatConstant(_)) => {
|
2020-05-06 13:45:17 +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-05-06 13:45:17 +02:00
|
|
|
}
|
|
|
|
|
2020-04-06 11:47:34 +02:00
|
|
|
// "xxx" in "xxxx", 'x' in "xxxx" - OK!
|
2020-05-09 15:46:38 +02:00
|
|
|
(Expr::StringConstant(_), Expr::StringConstant(_))
|
|
|
|
| (Expr::CharConstant(_), Expr::StringConstant(_)) => (),
|
2020-04-06 11:47:34 +02:00
|
|
|
|
|
|
|
// 123.456 in "xxxx"
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-05-25 14:14:31 +02: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-05-25 14:14:31 +02: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-05-25 14:14:31 +02:00
|
|
|
(x @ Expr::And(_), Expr::StringConstant(_))
|
|
|
|
| (x @ Expr::Or(_), Expr::StringConstant(_))
|
|
|
|
| (x @ Expr::In(_), Expr::StringConstant(_))
|
|
|
|
| (x @ Expr::True(_), Expr::StringConstant(_))
|
|
|
|
| (x @ Expr::False(_), 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-05-25 14:14:31 +02: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-05-25 14:14:31 +02: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()))
|
2020-05-09 15:46:38 +02:00
|
|
|
}
|
|
|
|
// (??? = ???) in "xxxx"
|
2020-05-25 14:14:31 +02:00
|
|
|
(x @ Expr::Assignment(_), Expr::StringConstant(_)) => {
|
2020-05-09 15:46:38 +02:00
|
|
|
return Err(PERR::MalformedInExpr(
|
2020-05-25 14:14:31 +02:00
|
|
|
"'in' expression for a string expects a string, not an assignment".into(),
|
2020-05-09 15:46:38 +02:00
|
|
|
)
|
2020-05-25 14:14:31 +02:00
|
|
|
.into_err(x.position()))
|
2020-04-06 11:47:34 +02:00
|
|
|
}
|
2020-05-09 15:46:38 +02:00
|
|
|
// () in "xxxx"
|
2020-05-25 14:14:31 +02: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-05-09 15:46:38 +02:00
|
|
|
(Expr::StringConstant(_), Expr::Map(_)) | (Expr::CharConstant(_), Expr::Map(_)) => (),
|
2020-04-06 11:47:34 +02:00
|
|
|
|
|
|
|
// 123.456 in #{...}
|
2020-05-06 13:45:17 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-05-25 14:14:31 +02: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-05-25 14:14:31 +02: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-05-25 14:14:31 +02:00
|
|
|
(x @ Expr::And(_), Expr::Map(_))
|
|
|
|
| (x @ Expr::Or(_), Expr::Map(_))
|
|
|
|
| (x @ Expr::In(_), Expr::Map(_))
|
|
|
|
| (x @ Expr::True(_), Expr::Map(_))
|
|
|
|
| (x @ Expr::False(_), 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-05-25 14:14:31 +02: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-05-25 14:14:31 +02: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()))
|
2020-05-09 15:46:38 +02:00
|
|
|
}
|
|
|
|
// (??? = ???) in #{...}
|
2020-05-25 14:14:31 +02:00
|
|
|
(x @ Expr::Assignment(_), Expr::Map(_)) => {
|
2020-05-09 15:46:38 +02:00
|
|
|
return Err(PERR::MalformedInExpr(
|
2020-05-25 14:14:31 +02:00
|
|
|
"'in' expression for an object map expects a string, not an assignment".into(),
|
2020-05-09 15:46:38 +02:00
|
|
|
)
|
2020-05-25 14:14:31 +02:00
|
|
|
.into_err(x.position()))
|
2020-04-06 11:47:34 +02:00
|
|
|
}
|
2020-05-09 15:46:38 +02:00
|
|
|
// () in #{...}
|
2020-05-25 14:14:31 +02: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-05-09 15:46:38 +02:00
|
|
|
Ok(Expr::In(Box::new((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,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-03-14 04:51:45 +01:00
|
|
|
parent_precedence: u8,
|
2019-09-18 12:21:07 +02:00
|
|
|
lhs: Expr,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Expr, ParseError> {
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.pos = lhs.position();
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
|
|
|
let mut root = lhs;
|
2016-02-29 22:43:45 +01:00
|
|
|
|
|
|
|
loop {
|
2020-05-18 13:32:22 +02:00
|
|
|
let (current_op, _) = input.peek().unwrap();
|
|
|
|
let precedence = current_op.precedence();
|
|
|
|
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
|
2020-05-18 13:32:22 +02:00
|
|
|
if precedence < parent_precedence || (precedence == parent_precedence && !bind_right) {
|
|
|
|
return Ok(root);
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
|
2020-04-23 07:24:24 +02:00
|
|
|
let (op_token, pos) = input.next().unwrap();
|
2016-04-14 03:01:08 +02:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
let rhs = parse_unary(input, state, settings)?;
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-04-23 07:24:24 +02:00
|
|
|
let next_precedence = input.peek().unwrap().0.precedence();
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-04-23 07:24:24 +02:00
|
|
|
// Bind to right if the next operator has higher precedence
|
|
|
|
// If same precedence, then check if the operator binds right
|
2020-05-18 13:32:22 +02:00
|
|
|
let rhs = if (precedence == next_precedence && bind_right) || precedence < next_precedence {
|
2020-06-11 16:03:47 +02:00
|
|
|
parse_binary_op(input, state, precedence, rhs, settings)?
|
2020-04-23 07:24:24 +02:00
|
|
|
} else {
|
|
|
|
// Otherwise bind to left (even if next operator has the same precedence)
|
|
|
|
rhs
|
|
|
|
};
|
2020-03-02 07:28:42 +01:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
settings = settings.level_up();
|
|
|
|
settings.pos = pos;
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2020-05-09 18:19:13 +02:00
|
|
|
let cmp_def = Some(false.into());
|
2020-05-09 15:46:38 +02:00
|
|
|
let op = op_token.syntax();
|
2020-05-19 14:07:51 +02:00
|
|
|
let hash = calc_fn_hash(empty(), &op, 2, empty());
|
2020-05-30 07:49:40 +02:00
|
|
|
let op = (op, true, pos);
|
2020-05-16 18:24:07 +02:00
|
|
|
|
|
|
|
let mut args = StaticVec::new();
|
2020-05-18 13:32:22 +02:00
|
|
|
args.push(root);
|
2020-05-16 18:24:07 +02:00
|
|
|
args.push(rhs);
|
2020-04-22 11:37:06 +02:00
|
|
|
|
2020-05-18 13:32:22 +02:00
|
|
|
root = match op_token {
|
2020-05-30 07:49:40 +02:00
|
|
|
Token::Plus
|
|
|
|
| Token::Minus
|
|
|
|
| Token::Multiply
|
|
|
|
| Token::Divide
|
|
|
|
| Token::LeftShift
|
|
|
|
| Token::RightShift
|
|
|
|
| Token::Modulo
|
|
|
|
| Token::PowerOf
|
|
|
|
| Token::Ampersand
|
|
|
|
| Token::Pipe
|
|
|
|
| Token::XOr => Expr::FnCall(Box::new((op, None, hash, args, None))),
|
|
|
|
|
|
|
|
// '!=' defaults to true when passed invalid operands
|
|
|
|
Token::NotEqualsTo => Expr::FnCall(Box::new((op, None, hash, args, Some(true.into())))),
|
2020-04-28 17:05:03 +02:00
|
|
|
|
|
|
|
// Comparison operators default to false when passed invalid operands
|
2020-05-30 07:49:40 +02:00
|
|
|
Token::EqualsTo
|
|
|
|
| Token::LessThan
|
|
|
|
| Token::LessThanEqualsTo
|
|
|
|
| Token::GreaterThan
|
|
|
|
| Token::GreaterThanEqualsTo => Expr::FnCall(Box::new((op, None, hash, args, cmp_def))),
|
2020-04-23 07:24:24 +02:00
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Token::Or => {
|
2020-05-16 18:24:07 +02:00
|
|
|
let rhs = args.pop();
|
|
|
|
let current_lhs = args.pop();
|
2020-05-09 15:46:38 +02:00
|
|
|
Expr::Or(Box::new((current_lhs, rhs, pos)))
|
|
|
|
}
|
|
|
|
Token::And => {
|
2020-05-16 18:24:07 +02:00
|
|
|
let rhs = args.pop();
|
|
|
|
let current_lhs = args.pop();
|
2020-05-09 15:46:38 +02:00
|
|
|
Expr::And(Box::new((current_lhs, rhs, pos)))
|
|
|
|
}
|
|
|
|
Token::In => {
|
2020-05-16 18:24:07 +02:00
|
|
|
let rhs = args.pop();
|
|
|
|
let current_lhs = args.pop();
|
2020-05-09 15:46:38 +02:00
|
|
|
make_in_expr(current_lhs, rhs, pos)?
|
|
|
|
}
|
2020-04-23 07:24:24 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-05-09 15:46:38 +02:00
|
|
|
Token::Period => {
|
2020-05-16 18:24:07 +02:00
|
|
|
let mut rhs = args.pop();
|
|
|
|
let current_lhs = args.pop();
|
2020-05-12 04:20:29 +02:00
|
|
|
|
|
|
|
match &mut rhs {
|
|
|
|
// current_lhs.rhs(...) - method call
|
|
|
|
Expr::FnCall(x) => {
|
2020-05-23 12:59:28 +02:00
|
|
|
let ((id, _, _), _, hash, args, _) = x.as_mut();
|
2020-05-12 04:20:29 +02:00
|
|
|
// Recalculate function call hash because there is an additional argument
|
2020-05-19 14:07:51 +02:00
|
|
|
*hash = calc_fn_hash(empty(), id, args.len() + 1, empty());
|
2020-05-12 04:20:29 +02:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2020-05-30 18:02:23 +02:00
|
|
|
make_dot_expr(current_lhs, rhs, pos)?
|
2020-05-09 15:46:38 +02:00
|
|
|
}
|
2020-04-23 07:24:24 +02:00
|
|
|
|
2020-06-14 08:25:47 +02:00
|
|
|
op_token => return Err(PERR::UnknownOperator(op_token.into()).into_err(pos)),
|
2020-04-23 07:24:24 +02:00
|
|
|
};
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Expr, ParseError> {
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.pos = input.peek().unwrap().1;
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
let lhs = parse_unary(input, state, settings.level_up())?;
|
|
|
|
parse_binary_op(input, state, 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> {
|
2020-04-17 14:01:41 +02:00
|
|
|
match input.peek().unwrap() {
|
2020-03-20 12:50:58 +01:00
|
|
|
// Disallow statement expressions
|
2020-04-17 14:01:41 +02:00
|
|
|
(Token::LeftBrace, pos) | (Token::EOF, pos) => {
|
|
|
|
Err(PERR::ExprExpected(type_name.to_string()).into_err(*pos))
|
|
|
|
}
|
2020-03-20 12:50:58 +01:00
|
|
|
// 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() {
|
|
|
|
(Token::Equals, pos) => {
|
|
|
|
return Err(PERR::BadInput("Possibly a typo of '=='?".to_string()).into_err(*pos))
|
|
|
|
}
|
|
|
|
(Token::PlusAssign, pos)
|
|
|
|
| (Token::MinusAssign, pos)
|
|
|
|
| (Token::MultiplyAssign, pos)
|
|
|
|
| (Token::DivideAssign, pos)
|
|
|
|
| (Token::LeftShiftAssign, pos)
|
|
|
|
| (Token::RightShiftAssign, pos)
|
|
|
|
| (Token::ModuloAssign, pos)
|
|
|
|
| (Token::PowerOfAssign, pos)
|
|
|
|
| (Token::AndAssign, pos)
|
|
|
|
| (Token::OrAssign, pos)
|
|
|
|
| (Token::XOrAssign, pos) => {
|
|
|
|
return Err(PERR::BadInput(
|
|
|
|
"Expecting a boolean expression, not an assignment".to_string(),
|
|
|
|
)
|
|
|
|
.into_err(*pos))
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => 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,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Stmt, ParseError> {
|
2020-03-18 11:41:18 +01:00
|
|
|
// if ...
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.pos = eat_token(input, Token::If);
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-03-20 12:50:58 +01:00
|
|
|
// if guard { if_body }
|
|
|
|
ensure_not_statement_expr(input, "a boolean")?;
|
2020-06-11 16:03:47 +02:00
|
|
|
let guard = parse_expr(input, state, settings.level_up())?;
|
2020-04-22 11:37:06 +02:00
|
|
|
ensure_not_assignment(input)?;
|
2020-06-11 16:03:47 +02:00
|
|
|
let if_body = parse_block(input, state, settings.level_up())?;
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-03-20 12:50:58 +01:00
|
|
|
// if guard { if_body } else ...
|
2020-04-15 15:41:28 +02:00
|
|
|
let else_body = if match_token(input, Token::Else).unwrap_or(false) {
|
2020-05-09 15:46:38 +02:00
|
|
|
Some(if let (Token::If, _) = input.peek().unwrap() {
|
2020-03-20 12:50:58 +01:00
|
|
|
// if guard { if_body } else if ...
|
2020-06-11 16:03:47 +02:00
|
|
|
parse_if(input, state, settings.level_up())?
|
2020-03-16 16:51:32 +01:00
|
|
|
} else {
|
2020-03-20 12:50:58 +01:00
|
|
|
// if guard { if_body } else { else-body }
|
2020-06-11 16:03:47 +02:00
|
|
|
parse_block(input, state, settings.level_up())?
|
2020-05-09 15:46:38 +02:00
|
|
|
})
|
2020-03-16 16:51:32 +01:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2020-03-02 10:04:56 +01:00
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Ok(Stmt::IfThenElse(Box::new((guard, if_body, else_body))))
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Parse a while loop.
|
2020-06-11 12:13:33 +02:00
|
|
|
fn parse_while(
|
|
|
|
input: &mut TokenStream,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Stmt, ParseError> {
|
2020-03-18 11:41:18 +01:00
|
|
|
// while ...
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.pos = eat_token(input, Token::While);
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-03-18 11:41:18 +01:00
|
|
|
// while guard { body }
|
2020-03-20 12:50:58 +01:00
|
|
|
ensure_not_statement_expr(input, "a boolean")?;
|
2020-06-11 16:03:47 +02:00
|
|
|
let guard = parse_expr(input, state, settings.level_up())?;
|
2020-04-22 11:37:06 +02:00
|
|
|
ensure_not_assignment(input)?;
|
2020-06-11 16:03:47 +02:00
|
|
|
|
|
|
|
settings.is_breakable = true;
|
|
|
|
let body = parse_block(input, state, settings.level_up())?;
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Ok(Stmt::While(Box::new((guard, body))))
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Parse a loop statement.
|
2020-06-11 12:13:33 +02:00
|
|
|
fn parse_loop(
|
|
|
|
input: &mut TokenStream,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Stmt, ParseError> {
|
2020-03-18 11:41:18 +01:00
|
|
|
// loop ...
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.pos = eat_token(input, Token::Loop);
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2017-10-30 16:08:44 +01:00
|
|
|
|
2020-03-18 11:41:18 +01:00
|
|
|
// loop { body }
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.is_breakable = true;
|
|
|
|
let body = parse_block(input, state, settings.level_up())?;
|
2017-10-30 16:08:44 +01:00
|
|
|
|
|
|
|
Ok(Stmt::Loop(Box::new(body)))
|
|
|
|
}
|
|
|
|
|
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,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Stmt, ParseError> {
|
2020-03-18 11:41:18 +01:00
|
|
|
// for ...
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.pos = eat_token(input, Token::For);
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-02-23 15:48:46 +01:00
|
|
|
|
2020-03-18 11:41:18 +01:00
|
|
|
// for name ...
|
2020-04-17 14:01:41 +02:00
|
|
|
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,
|
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-04-17 14:01:41 +02:00
|
|
|
// EOF
|
|
|
|
(Token::EOF, pos) => return Err(PERR::VariableExpected.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-02-23 15:48:46 +01:00
|
|
|
};
|
|
|
|
|
2020-03-18 11:41:18 +01:00
|
|
|
// for name in ...
|
2020-04-17 14:01:41 +02:00
|
|
|
match input.next().unwrap() {
|
|
|
|
(Token::In, _) => (),
|
2020-06-14 10:56:36 +02:00
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(pos)),
|
2020-04-17 14:01:41 +02:00
|
|
|
(_, pos) => {
|
|
|
|
return Err(
|
2020-05-04 13:36:58 +02:00
|
|
|
PERR::MissingToken(Token::In.into(), "after the iteration variable".into())
|
2020-04-17 14:01:41 +02:00
|
|
|
.into_err(pos),
|
|
|
|
)
|
2020-03-30 06:14:59 +02:00
|
|
|
}
|
2020-02-23 15:48:46 +01:00
|
|
|
}
|
|
|
|
|
2020-03-18 11:41:18 +01:00
|
|
|
// for name in expr { body }
|
2020-03-20 12:50:58 +01:00
|
|
|
ensure_not_statement_expr(input, "a boolean")?;
|
2020-06-11 16:03:47 +02:00
|
|
|
let expr = parse_expr(input, state, settings.level_up())?;
|
2020-04-28 17:05:03 +02:00
|
|
|
|
2020-05-18 13:32:22 +02:00
|
|
|
let prev_len = state.len();
|
|
|
|
state.push((name.clone(), ScopeEntryType::Normal));
|
2020-04-28 17:05:03 +02:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.is_breakable = true;
|
|
|
|
let body = parse_block(input, state, settings.level_up())?;
|
2020-04-28 17:05:03 +02:00
|
|
|
|
2020-05-18 13:32:22 +02:00
|
|
|
state.truncate(prev_len);
|
2020-02-23 15:48:46 +01:00
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Ok(Stmt::For(Box::new((name, expr, body))))
|
2020-02-23 15:48:46 +01:00
|
|
|
}
|
|
|
|
|
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,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-03-25 04:27:18 +01:00
|
|
|
var_type: ScopeEntryType,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Stmt, ParseError> {
|
2020-03-18 11:41:18 +01:00
|
|
|
// let/const... (specified in `var_type`)
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.pos = input.next().unwrap().1;
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-03-16 16:51:32 +01:00
|
|
|
|
2020-03-18 11:41:18 +01:00
|
|
|
// let name ...
|
2020-04-17 14:01:41 +02:00
|
|
|
let (name, pos) = match input.next().unwrap() {
|
2020-03-18 11:41:18 +01:00
|
|
|
(Token::Identifier(s), pos) => (s, 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
|
|
|
};
|
|
|
|
|
2020-03-18 11:41:18 +01:00
|
|
|
// let name = ...
|
2020-04-15 15:41:28 +02:00
|
|
|
if match_token(input, Token::Equals)? {
|
2020-03-18 11:41:18 +01:00
|
|
|
// let name = expr
|
2020-06-11 16:03:47 +02:00
|
|
|
let init_value = parse_expr(input, state, settings.level_up())?;
|
2020-03-13 11:12:41 +01:00
|
|
|
|
2020-03-14 16:41:15 +01:00
|
|
|
match var_type {
|
2020-03-18 11:41:18 +01:00
|
|
|
// let name = expr
|
2020-04-28 17:05:03 +02:00
|
|
|
ScopeEntryType::Normal => {
|
2020-05-18 13:32:22 +02:00
|
|
|
state.push((name.clone(), ScopeEntryType::Normal));
|
2020-05-09 18:19:13 +02:00
|
|
|
Ok(Stmt::Let(Box::new(((name, pos), Some(init_value)))))
|
2020-04-28 17:05:03 +02:00
|
|
|
}
|
2020-03-18 11:41:18 +01:00
|
|
|
// const name = { expr:constant }
|
2020-03-25 04:27:18 +01:00
|
|
|
ScopeEntryType::Constant if init_value.is_constant() => {
|
2020-05-18 13:32:22 +02:00
|
|
|
state.push((name.clone(), ScopeEntryType::Constant));
|
2020-05-09 18:19:13 +02:00
|
|
|
Ok(Stmt::Const(Box::new(((name, pos), init_value))))
|
2020-03-13 11:12:41 +01:00
|
|
|
}
|
2020-03-18 11:41:18 +01:00
|
|
|
// const name = expr - error
|
2020-03-27 16:47:23 +01:00
|
|
|
ScopeEntryType::Constant => {
|
|
|
|
Err(PERR::ForbiddenConstantExpr(name).into_err(init_value.position()))
|
|
|
|
}
|
2020-05-06 16:54:34 +02:00
|
|
|
// Variable cannot be a module
|
2020-05-05 04:39:12 +02:00
|
|
|
ScopeEntryType::Module => unreachable!(),
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
2020-03-14 16:41:15 +01:00
|
|
|
} else {
|
2020-03-18 11:41:18 +01:00
|
|
|
// let name
|
2020-05-06 16:54:34 +02:00
|
|
|
match var_type {
|
|
|
|
ScopeEntryType::Normal => {
|
2020-05-18 13:32:22 +02:00
|
|
|
state.push((name.clone(), ScopeEntryType::Normal));
|
2020-05-09 18:19:13 +02:00
|
|
|
Ok(Stmt::Let(Box::new(((name, pos), None))))
|
2020-05-06 16:54:34 +02:00
|
|
|
}
|
|
|
|
ScopeEntryType::Constant => {
|
2020-05-18 13:32:22 +02:00
|
|
|
state.push((name.clone(), ScopeEntryType::Constant));
|
2020-05-09 18:19:13 +02:00
|
|
|
Ok(Stmt::Const(Box::new(((name, pos), Expr::Unit(pos)))))
|
2020-05-06 16:54:34 +02:00
|
|
|
}
|
|
|
|
// Variable cannot be a module
|
|
|
|
ScopeEntryType::Module => unreachable!(),
|
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 13:36:58 +02:00
|
|
|
/// Parse an import statement.
|
2020-06-11 12:13:33 +02:00
|
|
|
fn parse_import(
|
|
|
|
input: &mut TokenStream,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Stmt, ParseError> {
|
2020-05-04 13:36:58 +02:00
|
|
|
// import ...
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.pos = eat_token(input, Token::Import);
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2020-05-04 13:36:58 +02:00
|
|
|
// import expr ...
|
2020-06-11 16:03:47 +02:00
|
|
|
let expr = parse_expr(input, state, settings.level_up())?;
|
2020-05-04 13:36:58 +02:00
|
|
|
|
|
|
|
// import expr as ...
|
|
|
|
match input.next().unwrap() {
|
|
|
|
(Token::As, _) => (),
|
|
|
|
(_, pos) => {
|
|
|
|
return Err(
|
|
|
|
PERR::MissingToken(Token::As.into(), "in this import statement".into())
|
|
|
|
.into_err(pos),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// import expr as name ...
|
|
|
|
let (name, _) = match input.next().unwrap() {
|
|
|
|
(Token::Identifier(s), pos) => (s, 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-05-18 13:32:22 +02:00
|
|
|
state.push((name.clone(), ScopeEntryType::Module));
|
2020-06-11 16:03:47 +02:00
|
|
|
Ok(Stmt::Import(Box::new((expr, (name, settings.pos)))))
|
2020-05-04 13:36:58 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 10:49:24 +02:00
|
|
|
/// Parse an export statement.
|
2020-06-01 04:58:14 +02:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2020-06-11 12:13:33 +02:00
|
|
|
fn parse_export(
|
|
|
|
input: &mut TokenStream,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Stmt, ParseError> {
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.pos = eat_token(input, Token::Export);
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-08 10:49:24 +02:00
|
|
|
|
2020-05-16 18:24:07 +02:00
|
|
|
let mut exports = StaticVec::new();
|
2020-05-08 10:49:24 +02:00
|
|
|
|
|
|
|
loop {
|
|
|
|
let (id, id_pos) = match input.next().unwrap() {
|
|
|
|
(Token::Identifier(s), pos) => (s.clone(), 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)),
|
|
|
|
};
|
|
|
|
|
|
|
|
let rename = if match_token(input, Token::As)? {
|
|
|
|
match input.next().unwrap() {
|
|
|
|
(Token::Identifier(s), pos) => Some((s.clone(), pos)),
|
|
|
|
(_, pos) => return Err(PERR::VariableExpected.into_err(pos)),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-05-09 18:19:13 +02:00
|
|
|
exports.push(((id, 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for duplicating parameters
|
|
|
|
exports
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2020-05-09 18:19:13 +02:00
|
|
|
.try_for_each(|(i, ((id1, _), _))| {
|
2020-05-08 10:49:24 +02:00
|
|
|
exports
|
|
|
|
.iter()
|
|
|
|
.skip(i + 1)
|
2020-05-09 18:19:13 +02:00
|
|
|
.find(|((id2, _), _)| id2 == id1)
|
|
|
|
.map_or_else(|| Ok(()), |((id2, pos), _)| Err((id2, *pos)))
|
2020-05-08 10:49:24 +02:00
|
|
|
})
|
2020-05-09 18:19:13 +02:00
|
|
|
.map_err(|(id2, pos)| PERR::DuplicatedExport(id2.to_string()).into_err(pos))?;
|
2020-05-08 10:49:24 +02:00
|
|
|
|
2020-05-09 15:46:38 +02:00
|
|
|
Ok(Stmt::Export(Box::new(exports)))
|
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,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Stmt, ParseError> {
|
2020-03-18 11:41:18 +01:00
|
|
|
// Must start with {
|
2020-06-11 16:03:47 +02:00
|
|
|
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)),
|
2020-03-29 07:44:27 +02:00
|
|
|
(_, pos) => {
|
2020-05-04 13:36:58 +02:00
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::LeftBrace.into(),
|
|
|
|
"to start a statement block".into(),
|
2020-03-29 07:44:27 +02:00
|
|
|
)
|
2020-05-04 13:36:58 +02:00
|
|
|
.into_err(pos))
|
2020-03-29 07:44:27 +02:00
|
|
|
}
|
2020-03-12 05:40:28 +01:00
|
|
|
};
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2020-05-16 18:24:07 +02:00
|
|
|
let mut statements = StaticVec::new();
|
2020-05-18 13:32:22 +02:00
|
|
|
let prev_len = state.len();
|
2016-03-01 21:16:10 +01:00
|
|
|
|
2020-04-15 15:41:28 +02:00
|
|
|
while !match_token(input, Token::RightBrace)? {
|
2020-03-17 10:33:37 +01:00
|
|
|
// Parse statements inside the block
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.is_global = false;
|
|
|
|
let stmt = parse_stmt(input, state, settings.level_up())?;
|
2016-03-01 21:16:10 +01:00
|
|
|
|
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);
|
2020-03-17 07:29:22 +01:00
|
|
|
|
2020-04-17 14:01:41 +02:00
|
|
|
match input.peek().unwrap() {
|
2020-03-18 11:41:18 +01:00
|
|
|
// { ... stmt }
|
2020-04-17 14:01:41 +02:00
|
|
|
(Token::RightBrace, _) => {
|
2020-04-15 15:41:28 +02:00
|
|
|
eat_token(input, Token::RightBrace);
|
|
|
|
break;
|
|
|
|
}
|
2020-03-18 11:41:18 +01:00
|
|
|
// { ... stmt;
|
2020-04-17 14:01:41 +02:00
|
|
|
(Token::SemiColon, _) if need_semicolon => {
|
2020-04-15 15:41:28 +02:00
|
|
|
eat_token(input, Token::SemiColon);
|
2020-03-17 10:33:37 +01:00
|
|
|
}
|
2020-03-18 11:41:18 +01:00
|
|
|
// { ... { stmt } ;
|
2020-04-17 14:01:41 +02:00
|
|
|
(Token::SemiColon, _) if !need_semicolon => (),
|
2020-03-18 11:41:18 +01:00
|
|
|
// { ... { stmt } ???
|
2020-04-17 14:01:41 +02:00
|
|
|
(_, _) if !need_semicolon => (),
|
2020-04-20 18:11:25 +02:00
|
|
|
// { ... stmt <error>
|
2020-06-14 10:56:36 +02:00
|
|
|
(Token::LexError(err), pos) => return Err(err.into_err(*pos)),
|
2020-04-20 18:11:25 +02:00
|
|
|
// { ... stmt ???
|
2020-04-17 14:01:41 +02:00
|
|
|
(_, 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-04-14 03:01:08 +02:00
|
|
|
}
|
2016-03-01 21:16:10 +01:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
|
2020-05-18 13:32:22 +02:00
|
|
|
state.truncate(prev_len);
|
2020-04-28 17:05:03 +02:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
Ok(Stmt::Block(Box::new((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,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Stmt, ParseError> {
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.pos = input.peek().unwrap().1;
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
let expr = parse_expr(input, state, settings.level_up())?;
|
|
|
|
let expr = parse_op_assignment_stmt(input, state, expr, settings.level_up())?;
|
2020-04-22 11:37:06 +02:00
|
|
|
Ok(Stmt::Expr(Box::new(expr)))
|
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,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-05-22 12:28:13 +02:00
|
|
|
) -> Result<Stmt, ParseError> {
|
2020-05-18 13:32:22 +02:00
|
|
|
use ScopeEntryType::{Constant, Normal};
|
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
let (token, token_pos) = match input.peek().unwrap() {
|
2020-04-17 14:01:41 +02:00
|
|
|
(Token::EOF, pos) => return Ok(Stmt::Noop(*pos)),
|
|
|
|
x => x,
|
2020-03-20 12:50:58 +01:00
|
|
|
};
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.pos = *token_pos;
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2020-03-20 12:50:58 +01:00
|
|
|
match token {
|
2020-03-18 11:41:18 +01:00
|
|
|
// Semicolon - empty statement
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::SemiColon => Ok(Stmt::Noop(settings.pos)),
|
2020-03-18 11:41:18 +01:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::LeftBrace => parse_block(input, state, settings.level_up()),
|
2020-04-01 10:22:18 +02:00
|
|
|
|
2020-03-18 11:41:18 +01:00
|
|
|
// fn ...
|
2020-06-01 04:58:14 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::Fn if !settings.is_global => Err(PERR::WrongFnDefinition.into_err(settings.pos)),
|
2020-06-01 04:58:14 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-05-08 16:38:56 +02:00
|
|
|
Token::Fn => unreachable!(),
|
2020-03-17 07:29:22 +01:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::If => parse_if(input, state, settings.level_up()),
|
|
|
|
Token::While => parse_while(input, state, settings.level_up()),
|
|
|
|
Token::Loop => parse_loop(input, state, settings.level_up()),
|
|
|
|
Token::For => parse_for(input, state, settings.level_up()),
|
2020-04-01 10:22:18 +02:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::Continue if settings.is_breakable => {
|
2020-04-17 13:00:52 +02:00
|
|
|
let pos = eat_token(input, Token::Continue);
|
2020-04-01 10:22:18 +02:00
|
|
|
Ok(Stmt::Continue(pos))
|
|
|
|
}
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::Break if settings.is_breakable => {
|
2020-04-17 13:00:52 +02:00
|
|
|
let pos = eat_token(input, Token::Break);
|
2020-03-02 15:13:14 +01:00
|
|
|
Ok(Stmt::Break(pos))
|
2016-04-17 04:32:18 +02:00
|
|
|
}
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::Continue | Token::Break => Err(PERR::LoopBreak.into_err(settings.pos)),
|
2020-04-01 10:22:18 +02:00
|
|
|
|
2020-04-17 14:01:41 +02:00
|
|
|
Token::Return | Token::Throw => {
|
2020-04-23 07:24:24 +02:00
|
|
|
let return_type = match input.next().unwrap() {
|
|
|
|
(Token::Return, _) => ReturnType::Return,
|
|
|
|
(Token::Throw, _) => ReturnType::Exception,
|
2020-03-15 15:39:58 +01:00
|
|
|
_ => panic!("token should be return or throw"),
|
2020-03-03 11:15:20 +01:00
|
|
|
};
|
|
|
|
|
2020-04-17 14:01:41 +02:00
|
|
|
match input.peek().unwrap() {
|
2020-04-20 18:11:25 +02:00
|
|
|
// `return`/`throw` at <EOF>
|
2020-05-09 18:19:13 +02:00
|
|
|
(Token::EOF, pos) => Ok(Stmt::ReturnWithVal(Box::new(((return_type, *pos), None)))),
|
2020-03-18 03:36:50 +01:00
|
|
|
// `return;` or `throw;`
|
2020-06-11 16:03:47 +02:00
|
|
|
(Token::SemiColon, _) => Ok(Stmt::ReturnWithVal(Box::new((
|
|
|
|
(return_type, settings.pos),
|
|
|
|
None,
|
|
|
|
)))),
|
2020-03-18 03:36:50 +01:00
|
|
|
// `return` or `throw` with expression
|
2020-04-17 14:01:41 +02:00
|
|
|
(_, _) => {
|
2020-06-11 16:03:47 +02:00
|
|
|
let expr = parse_expr(input, state, settings.level_up())?;
|
2020-03-19 12:53:42 +01:00
|
|
|
let pos = expr.position();
|
2020-05-09 15:46:38 +02:00
|
|
|
|
|
|
|
Ok(Stmt::ReturnWithVal(Box::new((
|
2020-05-09 18:19:13 +02:00
|
|
|
(return_type, pos),
|
2020-05-09 15:46:38 +02:00
|
|
|
Some(expr),
|
|
|
|
))))
|
2016-04-17 04:32:18 +02:00
|
|
|
}
|
2016-03-03 15:31:42 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-01 10:22:18 +02:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
Token::Let => parse_let(input, state, Normal, settings.level_up()),
|
|
|
|
Token::Const => parse_let(input, state, Constant, settings.level_up()),
|
|
|
|
Token::Import => parse_import(input, state, settings.level_up()),
|
2020-05-04 13:36:58 +02:00
|
|
|
|
2020-05-08 10:49:24 +02:00
|
|
|
#[cfg(not(feature = "no_module"))]
|
2020-06-11 16:03:47 +02:00
|
|
|
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-06-11 16:03:47 +02:00
|
|
|
Token::Export => parse_export(input, state, settings.level_up()),
|
2020-05-08 10:49:24 +02:00
|
|
|
|
2020-06-11 16:03:47 +02:00
|
|
|
_ => parse_expr_stmt(input, state, settings.level_up()),
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:50 +01:00
|
|
|
/// Parse a function definition.
|
2020-06-01 04:58:14 +02:00
|
|
|
#[cfg(not(feature = "no_function"))]
|
2020-06-11 12:13:33 +02:00
|
|
|
fn parse_fn(
|
|
|
|
input: &mut TokenStream,
|
2020-05-18 13:32:22 +02:00
|
|
|
state: &mut ParseState,
|
2020-05-09 05:29:30 +02:00
|
|
|
access: FnAccess,
|
2020-06-11 16:03:47 +02:00
|
|
|
mut settings: ParseSettings,
|
2020-06-05 09:14:42 +02:00
|
|
|
) -> Result<ScriptFnDef, ParseError> {
|
2020-06-11 16:03:47 +02:00
|
|
|
settings.pos = eat_token(input, Token::Fn);
|
|
|
|
settings.ensure_level_within_max_limit(state.max_expr_depth)?;
|
2020-05-18 13:32:22 +02:00
|
|
|
|
2020-04-17 14:01:41 +02:00
|
|
|
let name = match input.next().unwrap() {
|
|
|
|
(Token::Identifier(s), _) => s,
|
|
|
|
(_, pos) => return Err(PERR::FnMissingName.into_err(pos)),
|
2016-03-01 21:16:10 +01:00
|
|
|
};
|
|
|
|
|
2020-04-17 14:01:41 +02:00
|
|
|
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
|
|
|
};
|
2016-03-01 21:16:10 +01:00
|
|
|
|
|
|
|
let mut params = Vec::new();
|
|
|
|
|
2020-04-15 15:41:28 +02:00
|
|
|
if !match_token(input, Token::RightParen)? {
|
2020-03-29 07:44:27 +02:00
|
|
|
let end_err = format!("to close the parameters list of function '{}'", name);
|
|
|
|
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-04-17 14:01:41 +02:00
|
|
|
match input.next().unwrap() {
|
2020-04-28 17:05:03 +02:00
|
|
|
(Token::Identifier(s), pos) => {
|
2020-05-18 13:32:22 +02:00
|
|
|
state.push((s.clone(), ScopeEntryType::Normal));
|
2020-04-28 17:05:03 +02:00
|
|
|
params.push((s, 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::MissingToken(Token::RightParen.into(), end_err).into_err(pos))
|
|
|
|
}
|
2020-03-16 16:51:32 +01:00
|
|
|
}
|
|
|
|
|
2020-04-17 14:01:41 +02:00
|
|
|
match input.next().unwrap() {
|
|
|
|
(Token::RightParen, _) => break,
|
|
|
|
(Token::Comma, _) => (),
|
|
|
|
(Token::Identifier(_), pos) => {
|
2020-05-04 13:36:58 +02:00
|
|
|
return Err(PERR::MissingToken(Token::Comma.into(), sep_err).into_err(pos))
|
2020-03-29 07:44:27 +02:00
|
|
|
}
|
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))
|
|
|
|
}
|
2016-03-01 21:16:10 +01:00
|
|
|
}
|
2020-03-14 16:41:15 +01:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 09:46:08 +01:00
|
|
|
// Check for duplicating parameters
|
|
|
|
params
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.try_for_each(|(i, (p1, _))| {
|
|
|
|
params
|
|
|
|
.iter()
|
|
|
|
.skip(i + 1)
|
|
|
|
.find(|(p2, _)| p2 == p1)
|
|
|
|
.map_or_else(|| Ok(()), |(p2, pos)| Err((p2, *pos)))
|
|
|
|
})
|
|
|
|
.map_err(|(p, pos)| {
|
2020-03-29 07:44:27 +02:00
|
|
|
PERR::FnDuplicatedParam(name.to_string(), p.to_string()).into_err(pos)
|
2020-03-27 09:46:08 +01:00
|
|
|
})?;
|
|
|
|
|
|
|
|
// Parse function body
|
2020-05-09 15:46:38 +02:00
|
|
|
let body = match input.peek().unwrap() {
|
2020-06-11 16:03:47 +02:00
|
|
|
(Token::LeftBrace, _) => {
|
|
|
|
settings.is_breakable = false;
|
|
|
|
parse_block(input, state, settings.level_up())?
|
|
|
|
}
|
2020-04-17 14:01:41 +02:00
|
|
|
(_, pos) => return Err(PERR::FnMissingBody(name).into_err(*pos)),
|
2020-05-09 15:46:38 +02:00
|
|
|
};
|
2016-03-01 21:16:10 +01:00
|
|
|
|
2020-04-06 11:47:34 +02:00
|
|
|
let params = params.into_iter().map(|(p, _)| p).collect();
|
|
|
|
|
2020-06-05 09:14:42 +02:00
|
|
|
Ok(ScriptFnDef {
|
2020-03-11 16:43:04 +01:00
|
|
|
name,
|
2020-05-09 05:29:30 +02:00
|
|
|
access,
|
2020-04-06 11:47:34 +02:00
|
|
|
params,
|
2020-03-09 14:57:07 +01:00
|
|
|
body,
|
2020-06-11 16:03:47 +02:00
|
|
|
pos: settings.pos,
|
2016-04-17 04:32:18 +02:00
|
|
|
})
|
2016-03-01 21:16:10 +01:00
|
|
|
}
|
|
|
|
|
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> {
|
2020-06-14 08:25:47 +02:00
|
|
|
let mut state = ParseState::new(
|
|
|
|
self.max_expr_depth,
|
|
|
|
self.max_string_size,
|
|
|
|
self.max_array_size,
|
|
|
|
self.max_map_size,
|
|
|
|
);
|
2020-06-11 16:03:47 +02:00
|
|
|
let settings = ParseSettings {
|
|
|
|
allow_if_expr: false,
|
|
|
|
allow_stmt_expr: false,
|
|
|
|
is_global: true,
|
|
|
|
is_breakable: false,
|
|
|
|
level: 0,
|
|
|
|
pos: Position::none(),
|
|
|
|
};
|
|
|
|
let expr = parse_expr(input, &mut state, settings)?;
|
2020-06-03 04:44:36 +02:00
|
|
|
|
|
|
|
match input.peek().unwrap() {
|
|
|
|
(Token::EOF, _) => (),
|
|
|
|
// Return error if the expression doesn't end
|
|
|
|
(token, pos) => {
|
|
|
|
return Err(
|
|
|
|
PERR::BadInput(format!("Unexpected '{}'", token.syntax())).into_err(*pos)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let expr = vec![Stmt::Expr(Box::new(expr))];
|
|
|
|
|
|
|
|
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> {
|
|
|
|
let mut statements = Vec::<Stmt>::new();
|
|
|
|
let mut functions = HashMap::<u64, ScriptFnDef, _>::with_hasher(StraightHasherBuilder);
|
|
|
|
let mut state = ParseState::new(
|
|
|
|
self.max_expr_depth,
|
|
|
|
self.max_string_size,
|
|
|
|
self.max_array_size,
|
|
|
|
self.max_map_size,
|
|
|
|
);
|
|
|
|
|
|
|
|
while !input.peek().unwrap().0.is_eof() {
|
|
|
|
// Collect all the function definitions
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
{
|
|
|
|
let (access, must_be_fn) = if match_token(input, Token::Private)? {
|
|
|
|
(FnAccess::Private, true)
|
|
|
|
} else {
|
|
|
|
(FnAccess::Public, false)
|
|
|
|
};
|
|
|
|
|
|
|
|
match input.peek().unwrap() {
|
|
|
|
#[cfg(not(feature = "no_function"))]
|
|
|
|
(Token::Fn, pos) => {
|
|
|
|
let mut state = ParseState::new(
|
|
|
|
self.max_function_expr_depth,
|
|
|
|
self.max_string_size,
|
|
|
|
self.max_array_size,
|
|
|
|
self.max_map_size,
|
|
|
|
);
|
|
|
|
let settings = ParseSettings {
|
|
|
|
allow_if_expr: true,
|
|
|
|
allow_stmt_expr: true,
|
|
|
|
is_global: false,
|
|
|
|
is_breakable: false,
|
|
|
|
level: 0,
|
|
|
|
pos: *pos,
|
|
|
|
};
|
|
|
|
let func = parse_fn(input, &mut state, access, settings)?;
|
|
|
|
|
|
|
|
// Qualifiers (none) + function name + number of arguments.
|
|
|
|
let hash = calc_fn_hash(empty(), &func.name, func.params.len(), empty());
|
|
|
|
|
|
|
|
functions.insert(hash, func);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(_, pos) if must_be_fn => {
|
|
|
|
return Err(PERR::MissingToken(
|
|
|
|
Token::Fn.into(),
|
|
|
|
format!("following '{}'", Token::Private.syntax()),
|
|
|
|
)
|
|
|
|
.into_err(*pos))
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actual statement
|
|
|
|
let settings = ParseSettings {
|
|
|
|
allow_if_expr: true,
|
|
|
|
allow_stmt_expr: true,
|
|
|
|
is_global: true,
|
|
|
|
is_breakable: false,
|
|
|
|
level: 0,
|
|
|
|
pos: Position::none(),
|
|
|
|
};
|
|
|
|
let stmt = parse_stmt(input, &mut state, settings)?;
|
|
|
|
|
|
|
|
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-06-14 10:56:36 +02:00
|
|
|
(Token::LexError(err), pos) => return Err(err.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-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> {
|
2020-06-14 08:25:47 +02:00
|
|
|
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 {
|
2020-05-09 15:46:38 +02:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
Union::Float(value) => Some(Expr::FloatConstant(Box::new((value, pos)))),
|
|
|
|
|
2020-04-12 17:00:06 +02:00
|
|
|
Union::Unit(_) => Some(Expr::Unit(pos)),
|
2020-05-09 15:46:38 +02:00
|
|
|
Union::Int(value) => Some(Expr::IntegerConstant(Box::new((value, pos)))),
|
|
|
|
Union::Char(value) => Some(Expr::CharConstant(Box::new((value, pos)))),
|
2020-05-25 07:44:28 +02:00
|
|
|
Union::Str(value) => Some(Expr::StringConstant(Box::new((value.clone(), pos)))),
|
2020-04-12 17:00:06 +02:00
|
|
|
Union::Bool(true) => Some(Expr::True(pos)),
|
|
|
|
Union::Bool(false) => Some(Expr::False(pos)),
|
2020-04-10 06:16:39 +02:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-04-12 17:00:06 +02:00
|
|
|
Union::Array(array) => {
|
|
|
|
let items: Vec<_> = array
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| map_dynamic_to_expr(x, pos))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
if items.iter().all(Option::is_some) {
|
2020-05-09 15:46:38 +02:00
|
|
|
Some(Expr::Array(Box::new((
|
2020-04-12 17:00:06 +02:00
|
|
|
items.into_iter().map(Option::unwrap).collect(),
|
|
|
|
pos,
|
2020-05-09 15:46:38 +02:00
|
|
|
))))
|
2020-04-12 17:00:06 +02:00
|
|
|
} else {
|
|
|
|
None
|
2020-04-10 06:16:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(feature = "no_object"))]
|
2020-04-12 17:00:06 +02:00
|
|
|
Union::Map(map) => {
|
|
|
|
let items: Vec<_> = map
|
|
|
|
.into_iter()
|
2020-05-09 18:19:13 +02:00
|
|
|
.map(|(k, v)| ((k, pos), map_dynamic_to_expr(v, pos)))
|
2020-04-12 17:00:06 +02:00
|
|
|
.collect();
|
2020-05-09 18:19:13 +02:00
|
|
|
|
|
|
|
if items.iter().all(|(_, expr)| expr.is_some()) {
|
2020-05-09 15:46:38 +02:00
|
|
|
Some(Expr::Map(Box::new((
|
2020-04-12 17:00:06 +02:00
|
|
|
items
|
|
|
|
.into_iter()
|
2020-05-09 18:19:13 +02:00
|
|
|
.map(|((k, pos), expr)| ((k, pos), expr.unwrap()))
|
2020-04-12 17:00:06 +02:00
|
|
|
.collect(),
|
|
|
|
pos,
|
2020-05-09 15:46:38 +02:00
|
|
|
))))
|
2020-04-12 17:00:06 +02:00
|
|
|
} else {
|
|
|
|
None
|
2020-04-10 06:16:39 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-15 15:39:58 +01:00
|
|
|
|
2020-04-12 17:00:06 +02:00
|
|
|
_ => None,
|
2020-03-15 15:39:58 +01:00
|
|
|
}
|
2016-02-29 22:43:45 +01:00
|
|
|
}
|