Run var def filter during parsing.

This commit is contained in:
Stephen Chung
2022-02-13 18:46:25 +08:00
parent 664e3d31e5
commit 5bb6ce835f
9 changed files with 115 additions and 49 deletions

View File

@@ -36,6 +36,8 @@ pub enum EvalAltResult {
/// Shadowing of an existing variable disallowed. Wrapped value is the variable name.
ErrorVariableExists(String, Position),
/// Forbidden variable name. Wrapped value is the variable name.
ErrorForbiddenVariable(String, Position),
/// Access of an unknown variable. Wrapped value is the variable name.
ErrorVariableNotFound(String, Position),
/// Access of an unknown object map property. Wrapped value is the property name.
@@ -148,6 +150,7 @@ impl fmt::Display for EvalAltResult {
Self::ErrorInModule(s, err, ..) => write!(f, "Error in module {}: {}", s, err)?,
Self::ErrorVariableExists(s, ..) => write!(f, "Variable is already defined: {}", s)?,
Self::ErrorForbiddenVariable(s, ..) => write!(f, "Forbidden variable name: {}", s)?,
Self::ErrorVariableNotFound(s, ..) => write!(f, "Variable not found: {}", s)?,
Self::ErrorPropertyNotFound(s, ..) => write!(f, "Property not found: {}", s)?,
Self::ErrorFunctionNotFound(s, ..) => write!(f, "Function not found: {}", s)?,
@@ -285,6 +288,7 @@ impl EvalAltResult {
| Self::ErrorIndexingType(..)
| Self::ErrorFor(..)
| Self::ErrorVariableExists(..)
| Self::ErrorForbiddenVariable(..)
| Self::ErrorVariableNotFound(..)
| Self::ErrorPropertyNotFound(..)
| Self::ErrorModuleNotFound(..)
@@ -377,6 +381,7 @@ impl EvalAltResult {
map.insert("type".into(), t.into());
}
Self::ErrorVariableExists(v, ..)
| Self::ErrorForbiddenVariable(v, ..)
| Self::ErrorVariableNotFound(v, ..)
| Self::ErrorPropertyNotFound(v, ..)
| Self::ErrorDataRace(v, ..)
@@ -440,6 +445,7 @@ impl EvalAltResult {
| Self::ErrorIndexingType(.., pos)
| Self::ErrorFor(pos)
| Self::ErrorVariableExists(.., pos)
| Self::ErrorForbiddenVariable(.., pos)
| Self::ErrorVariableNotFound(.., pos)
| Self::ErrorPropertyNotFound(.., pos)
| Self::ErrorModuleNotFound(.., pos)
@@ -490,6 +496,7 @@ impl EvalAltResult {
| Self::ErrorIndexingType(.., pos)
| Self::ErrorFor(pos)
| Self::ErrorVariableExists(.., pos)
| Self::ErrorForbiddenVariable(.., pos)
| Self::ErrorVariableNotFound(.., pos)
| Self::ErrorPropertyNotFound(.., pos)
| Self::ErrorModuleNotFound(.., pos)

View File

@@ -109,6 +109,8 @@ pub enum ParseErrorType {
PropertyExpected,
/// Missing a variable name after the `let`, `const`, `for` or `catch` keywords.
VariableExpected,
/// Forbidden variable name. Wrapped value is the variable name.
ForbiddenVariable(String),
/// An identifier is a reserved symbol.
Reserved(String),
/// An expression is of the wrong type.
@@ -240,6 +242,7 @@ impl fmt::Display for ParseErrorType {
Self::WrongSwitchCaseCondition => f.write_str("This switch case cannot have a condition"),
Self::PropertyExpected => f.write_str("Expecting name of a property"),
Self::VariableExpected => f.write_str("Expecting name of a variable"),
Self::ForbiddenVariable(s) => write!(f, "Forbidden variable name: {}", s),
Self::WrongFnDefinition => f.write_str("Function definitions must be at global level and cannot be inside a block or another function"),
Self::FnMissingName => f.write_str("Expecting function name in function declaration"),
Self::WrongDocComment => f.write_str("Doc-comment must be followed immediately by a function definition"),

View File

@@ -595,6 +595,16 @@ impl Scope<'_> {
.zip(self.values.iter())
.map(|((name, ..), value)| (name.as_ref(), value.is_read_only(), value))
}
/// Get a reverse iterator to entries in the [`Scope`].
/// Shared values are not expanded.
#[inline]
pub(crate) fn iter_rev_raw(&self) -> impl Iterator<Item = (&str, bool, &Dynamic)> {
self.names
.iter()
.rev()
.zip(self.values.iter().rev())
.map(|((name, ..), value)| (name.as_ref(), value.is_read_only(), value))
}
/// Remove a range of entries within the [`Scope`].
///
/// # Panics