Skip to content

Commit

Permalink
Merge pull request #1664 from jim-lake/master
Browse files Browse the repository at this point in the history
fix unary operation
  • Loading branch information
taozhi8833998 authored Nov 12, 2023
2 parents 21ae881 + 6625609 commit 57baeff
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
4 changes: 4 additions & 0 deletions ast/postgresql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,10 @@ export type ident_without_kw = ident_name | quoted_ident;



export type column_without_kw = string | quoted_ident;



export type column = string | quoted_ident;


Expand Down
52 changes: 33 additions & 19 deletions pegjs/mysql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -2577,22 +2577,29 @@ case_else = KW_ELSE __ result:expr {
}

/**
* Borrowed from PL/SQL ,the priority of below list IS ORDER BY DESC
* From MySQL Manual
* ---------------------------------------------------------------------------------------------------
* | +, - | identity, negation |
* | *, / | multiplication, division |
* | +, - | addition, subtraction, concatenation |
* | =, <, >, <=, >=, <>, !=, IS, LIKE, BETWEEN, IN | comparion |
* | !, NOT | logical negation |
* | AND | conjunction |
* | OR | inclusion |
* !
* - (unary minus), ~ (unary bit inversion)
* ^
* *, /, DIV, %, MOD
* -, +
* <<, >>
* &
* |
* = (comparison), <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN, MEMBER OF
* BETWEEN, CASE, WHEN, THEN, ELSE
* NOT
* AND, &&
* XOR
* OR, ||
* = (assignment), :=
* ---------------------------------------------------------------------------------------------------
*/

_expr
= logic_operator_expr // support concatenation operator || and &&
/ or_expr
/ unary_expr

expr
= _expr / set_op_stmt
Expand All @@ -2605,11 +2612,6 @@ logic_operator_expr
else return createBinaryExpr(rh.op, logicExpr, rh.right)
}

unary_expr
= op: additive_operator tail: (__ primary)+ {
return createUnaryExpr(op, tail[0][1]);
}

binary_column_expr
= head:expr tail:(__ (KW_AND / KW_OR / LOGIC_OPERATOR) __ expr)* {
const ast = head.ast
Expand Down Expand Up @@ -2671,7 +2673,7 @@ and_expr
not_expr
= comparison_expr
/ exists_expr
/ (KW_NOT / "!" !"=") __ expr:not_expr {
/ KW_NOT __ expr:not_expr {
return createUnaryExpr('NOT', expr);
}

Expand Down Expand Up @@ -2780,11 +2782,11 @@ additive_expr
}

additive_operator
= "+" / "-" / "~"
= "+" / "-"

multiplicative_expr
= head:primary
tail:(__ multiplicative_operator __ primary)* {
= head:(unary_expr_or_primary)
tail:(__ multiplicative_operator __ (unary_expr_or_primary))* {
return createBinaryExprChain(head, tail)
}

Expand All @@ -2793,7 +2795,19 @@ multiplicative_operator
/ "div"i {
return 'DIV'
}
/ '&' / '>>' / '<<' / '^' / '|' / '~'
/ '&' / '>>' / '<<' / '^' / '|'

unary_expr_or_primary
= primary
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
if (op === '!') {
op = 'NOT';
}
return createUnaryExpr(op, tail[1]);
}

unary_operator
= '!' / '-' / '+' / '~'

primary
= cast_expr
Expand Down
2 changes: 1 addition & 1 deletion pegjs/postgresql.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -4066,7 +4066,7 @@ ident_without_kw

column_without_kw
= name:column_name {
return name;
/* => string */ return name;
}
/ quoted_ident

Expand Down

0 comments on commit 57baeff

Please sign in to comment.