Skip to content

Commit

Permalink
fix: fix parsing unary expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Tidyzq committed Oct 25, 2023
1 parent 4bffcfe commit b4bb3c6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
26 changes: 26 additions & 0 deletions glass-easel-template-compiler/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub(crate) enum TmplExpr {
BitReverse(Box<TmplExpr>),
Positive(Box<TmplExpr>),
Negative(Box<TmplExpr>),
TypeOf(Box<TmplExpr>),
Void(Box<TmplExpr>),

Multiply(Box<TmplExpr>, Box<TmplExpr>),
Divide(Box<TmplExpr>, Box<TmplExpr>),
Expand Down Expand Up @@ -94,6 +96,8 @@ impl TmplExpr {
TmplExpr::BitReverse(_) => TmplExprLevel::Unary,
TmplExpr::Positive(_) => TmplExprLevel::Unary,
TmplExpr::Negative(_) => TmplExprLevel::Unary,
TmplExpr::TypeOf(_) => TmplExprLevel::Unary,
TmplExpr::Void(_) => TmplExprLevel::Unary,
TmplExpr::Multiply(_, _) => TmplExprLevel::Multiply,
TmplExpr::Divide(_, _) => TmplExprLevel::Multiply,
TmplExpr::Mod(_, _) => TmplExprLevel::Multiply,
Expand Down Expand Up @@ -237,6 +241,12 @@ impl TmplExpr {
TmplExpr::Negative(x) => {
format!("-{}", x.to_expr_string(TmplExprLevel::Unary, is_js_target))
}
TmplExpr::TypeOf(x) => {
format!("typeof {}", x.to_expr_string(TmplExprLevel::Unary, is_js_target))
}
TmplExpr::Void(x) => {
format!("void {}", x.to_expr_string(TmplExprLevel::Unary, is_js_target))
}

TmplExpr::Multiply(x, y) => {
format!(
Expand Down Expand Up @@ -848,6 +858,16 @@ impl TmplExpr {
x.to_proc_gen_rec_and_end_path(w, scopes, TmplExprLevel::Unary, path_calc, value)?;
PathAnalysisState::NotInPath
}
TmplExpr::TypeOf(x) => {
write!(value, "typeof ")?;
x.to_proc_gen_rec_and_end_path(w, scopes, TmplExprLevel::Unary, path_calc, value)?;
PathAnalysisState::NotInPath
}
TmplExpr::Void(x) => {
write!(value, "void ")?;
x.to_proc_gen_rec_and_end_path(w, scopes, TmplExprLevel::Unary, path_calc, value)?;
PathAnalysisState::NotInPath
}

TmplExpr::Multiply(x, y) => {
x.to_proc_gen_rec_and_end_path(
Expand Down Expand Up @@ -1168,6 +1188,12 @@ impl TmplExpr {
TmplExpr::Negative(x) => {
x.get_binding_map_keys_rec(bmc, scope_names, should_disable, bmk);
}
TmplExpr::TypeOf(x) => {
x.get_binding_map_keys_rec(bmc, scope_names, should_disable, bmk);
}
TmplExpr::Void(x) => {
x.get_binding_map_keys_rec(bmc, scope_names, should_disable, bmk);
}

TmplExpr::Multiply(x, y) => {
x.get_binding_map_keys_rec(bmc, scope_names, should_disable, bmk);
Expand Down
2 changes: 2 additions & 0 deletions glass-easel-template-compiler/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ pub fn parse_tmpl(tmpl_str: &str, path: &str) -> Result<TmplTree, TmplParseError
Rule::bit_reverse => TmplExpr::BitReverse(next),
Rule::positive => TmplExpr::Positive(next),
Rule::negative => TmplExpr::Negative(next),
Rule::type_of => TmplExpr::TypeOf(next),
Rule::void => TmplExpr::Void(next),
_ => unreachable!(),
});
ret
Expand Down
17 changes: 16 additions & 1 deletion glass-easel-template-compiler/src/tmpl.pest
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,26 @@ multi = { "*" }
div = { "/" }
rem = { "%" }

unary = { (reverse | bit_reverse | positive | negative) ~ unary | member }
unary = _{ unary_positive | unary_negative | unary_base }
unary_base = { (void | type_of | reverse | bit_reverse) ~ unary | member }
unary_positive = { positive ~ (unary_negative | unary_base) }
unary_negative = { negative ~ (unary_positive | unary_base) }
reverse = { "!" }
bit_reverse = { "~" }
positive = { "+" }
negative = { "-" }
void = ${
&"void " ~ "void"
| &"void\r" ~ "void"
| &"void\n" ~ "void"
| &"void\t" ~ "void"
}
type_of = ${
&"typeof " ~ "typeof"
| &"typeof\r" ~ "typeof"
| &"typeof\n" ~ "typeof"
| &"typeof\t" ~ "typeof"
}
member = { value ~ (static_member | dynamic_member | func_call)* }
static_member = { "." ~ ident }
dynamic_member = { "[" ~ cond ~ "]" }
Expand Down
4 changes: 2 additions & 2 deletions glass-easel-template-compiler/tests/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ fn basic_entities_parsing() {
#[test]
fn basic_expr_parsing() {
let tree =
parse_tmpl(r#"<div attr=" {{ (a + 1).b }} "> {{ c - ( d + e ) * 3 }} </div>"#, r#""#).unwrap();
parse_tmpl(r#"<div attr=" {{ (a + 1).b }} "> {{ c - ( d + e ) * 3 }} {{+-+-!1 + typeof !typeof void 0}}</div>"#, r#""#).unwrap();
assert_eq!(
tree.to_string(),
r#"<div attr="{{" "+Y(X(a+1).b)+" "}}">{{" "+Y(c-(d+e)*3)+" "}}</div>"#
r#"<div attr="{{" "+Y(X(a+1).b)+" "}}">{{" "+Y(c-(d+e)*3)+" "+Y(+-+-!1+typeof !typeof void 0)}}</div>"#
);
}

Expand Down

0 comments on commit b4bb3c6

Please sign in to comment.