Fix bug with parsing improper module separator.
This commit is contained in:
parent
2c631daa07
commit
62696853b4
@ -9,6 +9,7 @@ Bug fixes
|
|||||||
|
|
||||||
* Complex indexing/dotting chains now parse correctly, for example: `a[b][c[d]].e`
|
* Complex indexing/dotting chains now parse correctly, for example: `a[b][c[d]].e`
|
||||||
* `map` and `filter` for arrays are marked `pure`. Warnings are added to the documentation of pure array methods that take `this` closures.
|
* `map` and `filter` for arrays are marked `pure`. Warnings are added to the documentation of pure array methods that take `this` closures.
|
||||||
|
* Syntax such as `foo.bar::baz` no longer panics, but returns a proper parse error.
|
||||||
|
|
||||||
|
|
||||||
Version 1.12.0
|
Version 1.12.0
|
||||||
|
@ -65,12 +65,15 @@ pub struct ParseState<'e, 's> {
|
|||||||
/// Tracks a list of external variables (variables that are not explicitly declared in the scope).
|
/// Tracks a list of external variables (variables that are not explicitly declared in the scope).
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
pub external_vars: Option<Box<crate::FnArgsVec<Ident>>>,
|
pub external_vars: Option<Box<crate::FnArgsVec<Ident>>>,
|
||||||
/// An indicator that disables variable capturing into externals one single time
|
/// An indicator that, when set to `false`, disables variable capturing into externals one
|
||||||
/// up until the nearest consumed Identifier token.
|
/// single time up until the nearest consumed Identifier token.
|
||||||
/// If set to false the next call to [`access_var`][ParseState::access_var] will not capture the variable.
|
/// If set to false the next call to [`access_var`][ParseState::access_var] will not capture the
|
||||||
/// All consequent calls to [`access_var`][ParseState::access_var] will not be affected.
|
/// variable. All consequent calls to [`access_var`][ParseState::access_var] will not be affected.
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
pub allow_capture: bool,
|
pub allow_capture: bool,
|
||||||
|
/// If set to `false`, no namespace-qualified identifiers are parsed.
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
pub allow_namespace: bool,
|
||||||
/// Encapsulates a local stack with imported [module][crate::Module] names.
|
/// Encapsulates a local stack with imported [module][crate::Module] names.
|
||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
pub imports: Option<Box<StaticVec<ImmutableString>>>,
|
pub imports: Option<Box<StaticVec<ImmutableString>>>,
|
||||||
@ -98,7 +101,8 @@ impl fmt::Debug for ParseState<'_, '_> {
|
|||||||
|
|
||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
f.field("imports", &self.imports)
|
f.field("imports", &self.imports)
|
||||||
.field("global_imports", &self.global_imports);
|
.field("global_imports", &self.global_imports)
|
||||||
|
.field("allow_namespace", &self.allow_namespace);
|
||||||
|
|
||||||
f.finish()
|
f.finish()
|
||||||
}
|
}
|
||||||
@ -120,6 +124,8 @@ impl<'e, 's> ParseState<'e, 's> {
|
|||||||
external_vars: None,
|
external_vars: None,
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
allow_capture: true,
|
allow_capture: true,
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
allow_namespace: true,
|
||||||
interned_strings,
|
interned_strings,
|
||||||
external_constants,
|
external_constants,
|
||||||
global: None,
|
global: None,
|
||||||
@ -456,8 +462,8 @@ fn ensure_not_statement_expr(
|
|||||||
/// Make sure that the next expression is not a mis-typed assignment (i.e. `a = b` instead of `a == b`).
|
/// Make sure that the next expression is not a mis-typed assignment (i.e. `a = b` instead of `a == b`).
|
||||||
fn ensure_not_assignment(input: &mut TokenStream) -> ParseResult<()> {
|
fn ensure_not_assignment(input: &mut TokenStream) -> ParseResult<()> {
|
||||||
match input.peek().expect(NEVER_ENDS) {
|
match input.peek().expect(NEVER_ENDS) {
|
||||||
(Token::Equals, pos) => Err(LexError::ImproperSymbol(
|
(token @ Token::Equals, pos) => Err(LexError::ImproperSymbol(
|
||||||
"=".into(),
|
token.literal_syntax().into(),
|
||||||
"Possibly a typo of '=='?".into(),
|
"Possibly a typo of '=='?".into(),
|
||||||
)
|
)
|
||||||
.into_err(*pos)),
|
.into_err(*pos)),
|
||||||
@ -1593,9 +1599,9 @@ impl Engine {
|
|||||||
token => unreachable!("Token::Identifier expected but gets {:?}", token),
|
token => unreachable!("Token::Identifier expected but gets {:?}", token),
|
||||||
};
|
};
|
||||||
|
|
||||||
match input.peek().expect(NEVER_ENDS).0 {
|
match input.peek().expect(NEVER_ENDS) {
|
||||||
// Function call
|
// Function call
|
||||||
Token::LeftParen | Token::Bang | Token::Unit => {
|
(Token::LeftParen | Token::Bang | Token::Unit, _) => {
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
{
|
{
|
||||||
// Once the identifier consumed we must enable next variables capturing
|
// Once the identifier consumed we must enable next variables capturing
|
||||||
@ -1609,7 +1615,15 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
// Namespace qualification
|
// Namespace qualification
|
||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
Token::DoubleColon => {
|
(token @ Token::DoubleColon, pos) => {
|
||||||
|
if !state.allow_namespace {
|
||||||
|
return Err(LexError::ImproperSymbol(
|
||||||
|
token.literal_syntax().into(),
|
||||||
|
String::new(),
|
||||||
|
)
|
||||||
|
.into_err(*pos));
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "no_closure"))]
|
#[cfg(not(feature = "no_closure"))]
|
||||||
{
|
{
|
||||||
// Once the identifier consumed we must enable next variables capturing
|
// Once the identifier consumed we must enable next variables capturing
|
||||||
@ -1751,6 +1765,10 @@ impl Engine {
|
|||||||
|
|
||||||
let (.., ns, _, name) = *x;
|
let (.., ns, _, name) = *x;
|
||||||
settings.pos = pos;
|
settings.pos = pos;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
auto_restore! { let orig_allow_namespace = state.allow_namespace; state.allow_namespace = true }
|
||||||
|
|
||||||
self.parse_fn_call(input, state, lib, settings, name, no_args, true, ns)?
|
self.parse_fn_call(input, state, lib, settings, name, no_args, true, ns)?
|
||||||
}
|
}
|
||||||
// Function call
|
// Function call
|
||||||
@ -1758,8 +1776,21 @@ impl Engine {
|
|||||||
let (.., ns, _, name) = *x;
|
let (.., ns, _, name) = *x;
|
||||||
let no_args = t == Token::Unit;
|
let no_args = t == Token::Unit;
|
||||||
settings.pos = pos;
|
settings.pos = pos;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
auto_restore! { let orig_allow_namespace = state.allow_namespace; state.allow_namespace = true }
|
||||||
|
|
||||||
self.parse_fn_call(input, state, lib, settings, name, no_args, false, ns)?
|
self.parse_fn_call(input, state, lib, settings, name, no_args, false, ns)?
|
||||||
}
|
}
|
||||||
|
// Disallowed module separator
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
(_, token @ Token::DoubleColon) if !state.allow_namespace => {
|
||||||
|
return Err(LexError::ImproperSymbol(
|
||||||
|
token.literal_syntax().into(),
|
||||||
|
String::new(),
|
||||||
|
)
|
||||||
|
.into_err(tail_pos))
|
||||||
|
}
|
||||||
// module access
|
// module access
|
||||||
#[cfg(not(feature = "no_module"))]
|
#[cfg(not(feature = "no_module"))]
|
||||||
(Expr::Variable(x, .., pos), Token::DoubleColon) => {
|
(Expr::Variable(x, .., pos), Token::DoubleColon) => {
|
||||||
@ -1769,15 +1800,16 @@ impl Engine {
|
|||||||
|
|
||||||
namespace.push(var_name_def);
|
namespace.push(var_name_def);
|
||||||
|
|
||||||
Expr::Variable(
|
let var_name = state.get_interned_string(id2);
|
||||||
(None, namespace, 0, state.get_interned_string(id2)).into(),
|
|
||||||
None,
|
Expr::Variable((None, namespace, 0, var_name).into(), None, pos2)
|
||||||
pos2,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
// Indexing
|
// Indexing
|
||||||
#[cfg(not(feature = "no_index"))]
|
#[cfg(not(feature = "no_index"))]
|
||||||
(expr, token @ (Token::LeftBracket | Token::QuestionBracket)) => {
|
(expr, token @ (Token::LeftBracket | Token::QuestionBracket)) => {
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
auto_restore! { let orig_allow_namespace = state.allow_namespace; state.allow_namespace = true }
|
||||||
|
|
||||||
let opt = match token {
|
let opt = match token {
|
||||||
Token::LeftBracket => ASTFlags::NONE,
|
Token::LeftBracket => ASTFlags::NONE,
|
||||||
Token::QuestionBracket => ASTFlags::NEGATED,
|
Token::QuestionBracket => ASTFlags::NEGATED,
|
||||||
@ -1805,7 +1837,13 @@ impl Engine {
|
|||||||
(.., pos) => return Err(PERR::PropertyExpected.into_err(*pos)),
|
(.., pos) => return Err(PERR::PropertyExpected.into_err(*pos)),
|
||||||
}
|
}
|
||||||
|
|
||||||
let rhs = self.parse_primary(input, state, lib, settings.level_up()?, true)?;
|
let rhs = {
|
||||||
|
#[cfg(not(feature = "no_module"))]
|
||||||
|
auto_restore! { let orig_allow_namespace = state.allow_namespace; state.allow_namespace = false }
|
||||||
|
|
||||||
|
self.parse_primary(input, state, lib, settings.level_up()?, true)?
|
||||||
|
};
|
||||||
|
|
||||||
let op_flags = match op {
|
let op_flags = match op {
|
||||||
Token::Period => ASTFlags::NONE,
|
Token::Period => ASTFlags::NONE,
|
||||||
Token::Elvis => ASTFlags::NEGATED,
|
Token::Elvis => ASTFlags::NEGATED,
|
||||||
@ -2081,11 +2119,13 @@ impl Engine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ??? && ??? = rhs, ??? || ??? = rhs, xxx ?? xxx = rhs
|
// ??? && ??? = rhs, ??? || ??? = rhs, xxx ?? xxx = rhs
|
||||||
Expr::And(..) | Expr::Or(..) | Expr::Coalesce(..) => Err(LexError::ImproperSymbol(
|
Expr::And(..) | Expr::Or(..) | Expr::Coalesce(..) if !op_info.is_op_assignment() => {
|
||||||
"=".into(),
|
Err(LexError::ImproperSymbol(
|
||||||
|
Token::Equals.literal_syntax().into(),
|
||||||
"Possibly a typo of '=='?".into(),
|
"Possibly a typo of '=='?".into(),
|
||||||
)
|
)
|
||||||
.into_err(op_pos)),
|
.into_err(op_pos))
|
||||||
|
}
|
||||||
// expr = rhs
|
// expr = rhs
|
||||||
_ => Err(PERR::AssignmentToInvalidLHS(String::new()).into_err(lhs.position())),
|
_ => Err(PERR::AssignmentToInvalidLHS(String::new()).into_err(lhs.position())),
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,13 @@ fn test_module() {
|
|||||||
assert_eq!(module.get_var_value::<INT>("answer").unwrap(), 42);
|
assert_eq!(module.get_var_value::<INT>("answer").unwrap(), 42);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_module_syntax() {
|
||||||
|
let engine = Engine::new();
|
||||||
|
assert!(engine.compile("abc.def::xyz").is_err());
|
||||||
|
assert!(engine.compile("abc.def::xyz()").is_err());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_module_sub_module() -> Result<(), Box<EvalAltResult>> {
|
fn test_module_sub_module() -> Result<(), Box<EvalAltResult>> {
|
||||||
let mut module = Module::new();
|
let mut module = Module::new();
|
||||||
|
Loading…
Reference in New Issue
Block a user