Change precedence to own type.

This commit is contained in:
Stephen Chung
2021-03-14 10:47:29 +08:00
parent 008ef0a41b
commit d9df6aabc8
4 changed files with 35 additions and 37 deletions

View File

@@ -5,7 +5,7 @@ use crate::ast::{
Stmt, StmtBlock,
};
use crate::dynamic::{AccessMode, Union};
use crate::engine::{KEYWORD_THIS, OP_CONTAINS};
use crate::engine::{Precedence, KEYWORD_THIS, OP_CONTAINS};
use crate::module::NamespaceRef;
use crate::optimize::optimize_into_ast;
use crate::optimize::OptimizationLevel;
@@ -1611,7 +1611,7 @@ fn parse_binary_op(
input: &mut TokenStream,
state: &mut ParseState,
lib: &mut FunctionsLib,
parent_precedence: u8,
parent_precedence: Option<Precedence>,
lhs: Expr,
mut settings: ParseSettings,
) -> Result<Expr, ParseError> {
@@ -1625,18 +1625,12 @@ fn parse_binary_op(
loop {
let (current_op, current_pos) = input.peek().unwrap();
let precedence = match current_op {
Token::Custom(c) => {
if state
.engine
.custom_keywords
.get(c)
.map_or(false, Option::is_some)
{
state.engine.custom_keywords.get(c).unwrap().unwrap().get()
} else {
return Err(PERR::Reserved(c.clone()).into_err(*current_pos));
}
}
Token::Custom(c) => state
.engine
.custom_keywords
.get(c)
.cloned()
.ok_or_else(|| PERR::Reserved(c.clone()).into_err(*current_pos))?,
Token::Reserved(c) if !is_valid_identifier(c.chars()) => {
return Err(PERR::UnknownOperator(c.into()).into_err(*current_pos))
}
@@ -1656,18 +1650,12 @@ fn parse_binary_op(
let (next_op, next_pos) = input.peek().unwrap();
let next_precedence = match next_op {
Token::Custom(c) => {
if state
.engine
.custom_keywords
.get(c)
.map_or(false, Option::is_some)
{
state.engine.custom_keywords.get(c).unwrap().unwrap().get()
} else {
return Err(PERR::Reserved(c.clone()).into_err(*next_pos));
}
}
Token::Custom(c) => state
.engine
.custom_keywords
.get(c)
.cloned()
.ok_or_else(|| PERR::Reserved(c.clone()).into_err(*next_pos))?,
Token::Reserved(c) if !is_valid_identifier(c.chars()) => {
return Err(PERR::UnknownOperator(c.into()).into_err(*next_pos))
}
@@ -1937,7 +1925,14 @@ fn parse_expr(
// Parse expression normally.
let lhs = parse_unary(input, state, lib, settings.level_up())?;
parse_binary_op(input, state, lib, 1, lhs, settings.level_up())
parse_binary_op(
input,
state,
lib,
Precedence::new(1),
lhs,
settings.level_up(),
)
}
/// Make sure that the expression is not a statement expression (i.e. wrapped in `{}`).