Skip to content

Commit da204bd

Browse files
committed
refactor: format unary
1 parent d16d953 commit da204bd

File tree

4 files changed

+51
-36
lines changed

4 files changed

+51
-36
lines changed

pegjs/mariadb.pegjs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,11 +2319,6 @@ logic_operator_expr
23192319
else return createBinaryExpr(rh.op, logicExpr, rh.right)
23202320
}
23212321

2322-
unary_expr
2323-
= op: additive_operator tail: (__ primary)+ {
2324-
return createUnaryExpr(op, tail[0][1]);
2325-
}
2326-
23272322
binary_column_expr
23282323
= head:expr tail:(__ (KW_AND / KW_OR / LOGIC_OPERATOR) __ expr)* {
23292324
const ast = head.ast
@@ -2386,12 +2381,12 @@ and_expr
23862381
not_expr
23872382
= comparison_expr
23882383
/ exists_expr
2389-
/ (KW_NOT / "!" !"=") __ expr:not_expr {
2384+
/ KW_NOT __ expr:not_expr {
23902385
return createUnaryExpr('NOT', expr);
23912386
}
23922387

23932388
comparison_expr
2394-
= left:(additive_expr / unary_expr) __ rh:comparison_op_right? {
2389+
= left:additive_expr __ rh:comparison_op_right? {
23952390
if (rh === null) return left;
23962391
else if (rh.type === 'arithmetic') return createBinaryExprChain(left, rh.tail);
23972392
else return createBinaryExpr(rh.op, left, rh.right);
@@ -2498,11 +2493,11 @@ additive_expr
24982493
}
24992494

25002495
additive_operator
2501-
= "+" / "-" / "~" / "!"
2496+
= "+" / "-"
25022497

25032498
multiplicative_expr
2504-
= head:primary
2505-
tail:(__ multiplicative_operator __ primary)* {
2499+
= head:unary_expr_or_primary
2500+
tail:(__ multiplicative_operator __ unary_expr_or_primary)* {
25062501
return createBinaryExprChain(head, tail)
25072502
}
25082503

@@ -2511,7 +2506,17 @@ multiplicative_operator
25112506
/ "div"i {
25122507
return 'DIV'
25132508
}
2514-
/ '&' / '>>' / '<<' / '^' / '|' / '~'
2509+
/ '&' / '>>' / '<<' / '^' / '|'
2510+
2511+
unary_expr_or_primary
2512+
= primary
2513+
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
2514+
// if (op === '!') op = 'NOT'
2515+
return createUnaryExpr(op, tail[1])
2516+
}
2517+
2518+
unary_operator
2519+
= '!' / '-' / '+' / '~'
25152520

25162521
primary
25172522
= cast_expr

pegjs/mysql.pegjs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2577,15 +2577,23 @@ case_else = KW_ELSE __ result:expr {
25772577
}
25782578

25792579
/**
2580-
* Borrowed from PL/SQL ,the priority of below list IS ORDER BY DESC
2580+
* From MySQL Manual
25812581
* ---------------------------------------------------------------------------------------------------
2582-
* | +, - | identity, negation |
2583-
* | *, / | multiplication, division |
2584-
* | +, - | addition, subtraction, concatenation |
2585-
* | =, <, >, <=, >=, <>, !=, IS, LIKE, BETWEEN, IN | comparion |
2586-
* | !, NOT | logical negation |
2587-
* | AND | conjunction |
2588-
* | OR | inclusion |
2582+
* * !
2583+
* - (unary minus), ~ (unary bit inversion)
2584+
* ^
2585+
* *, /, DIV, %, MOD
2586+
* -, +
2587+
* <<, >>
2588+
* &
2589+
* |
2590+
* = (comparison), <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN, MEMBER OF
2591+
* BETWEEN, CASE, WHEN, THEN, ELSE
2592+
* NOT
2593+
* AND, &&
2594+
* XOR
2595+
* OR, ||
2596+
* = (assignment), := |
25892597
* ---------------------------------------------------------------------------------------------------
25902598
*/
25912599

@@ -2604,11 +2612,6 @@ logic_operator_expr
26042612
else return createBinaryExpr(rh.op, logicExpr, rh.right)
26052613
}
26062614

2607-
unary_expr
2608-
= op:additive_operator tail: (__ primary)+ {
2609-
return createUnaryExpr(op, tail[0][1]);
2610-
}
2611-
26122615
binary_column_expr
26132616
= head:expr tail:(__ (KW_AND / KW_OR / LOGIC_OPERATOR) __ expr)* {
26142617
const ast = head.ast
@@ -2670,12 +2673,12 @@ and_expr
26702673
not_expr
26712674
= comparison_expr
26722675
/ exists_expr
2673-
/ (KW_NOT / "!" !"=") __ expr:not_expr {
2676+
/ KW_NOT __ expr:not_expr {
26742677
return createUnaryExpr('NOT', expr);
26752678
}
26762679

26772680
comparison_expr
2678-
= left:(additive_expr / unary_expr) __ rh:comparison_op_right? {
2681+
= left:additive_expr __ rh:comparison_op_right? {
26792682
if (rh === null) return left;
26802683
else if (rh.type === 'arithmetic') return createBinaryExprChain(left, rh.tail);
26812684
else return createBinaryExpr(rh.op, left, rh.right);
@@ -2780,11 +2783,11 @@ additive_expr
27802783
}
27812784

27822785
additive_operator
2783-
= "+" / "-" / "~" / "!"
2786+
= "+" / "-"
27842787

27852788
multiplicative_expr
2786-
= head:primary
2787-
tail:(__ multiplicative_operator __ primary)* {
2789+
= head:unary_expr_or_primary
2790+
tail:(__ multiplicative_operator __ unary_expr_or_primary)* {
27882791
return createBinaryExprChain(head, tail)
27892792
}
27902793

@@ -2793,7 +2796,17 @@ multiplicative_operator
27932796
/ "div"i {
27942797
return 'DIV'
27952798
}
2796-
/ '&' / '>>' / '<<' / '^' / '|' / '~'
2799+
/ '&' / '>>' / '<<' / '^' / '|'
2800+
2801+
unary_expr_or_primary
2802+
= primary
2803+
/ op:(unary_operator) tail:(__ unary_expr_or_primary) {
2804+
// if (op === '!') op = 'NOT'
2805+
return createUnaryExpr(op, tail[1])
2806+
}
2807+
2808+
unary_operator
2809+
= '!' / '-' / '+' / '~'
27972810

27982811
primary
27992812
= cast_expr

pegjs/postgresql.pegjs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4065,10 +4065,7 @@ ident_without_kw
40654065
= ident_name / quoted_ident
40664066

40674067
column_without_kw
4068-
= name:column_name {
4069-
return name;
4070-
}
4071-
/ quoted_ident
4068+
= column_name / quoted_ident
40724069

40734070
column
40744071
= name:column_name !{ return reservedMap[name.toUpperCase()] === true; } { /* => string */ return name; }

test/mysql-mariadb.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,8 +892,8 @@ describe('mysql', () => {
892892

893893
it('should throw error when covert args is not right', () => {
894894
const sql = `select convert(json_unquote(json_extract('{"thing": "252"}', "$.thing")));`
895-
expect(parser.astify.bind(parser, sql)).to.throw('Expected "!", "!=", "#", "%", "&", "&&", "*", "+", ",", "-", "--", "/", "/*", "<", "<<", "<=", "<>", "=", ">", ">=", ">>", "AND", "BETWEEN", "IN", "IS", "LIKE", "NOT", "ON", "OR", "OVER", "REGEXP", "RLIKE", "USING", "XOR", "^", "div", "|", "||", "~", or [ \\t\\n\\r] but ")" found.')
896-
expect(parser.astify.bind(parser, 'select convert("");')).to.throw('Expected "!", "!=", "#", "%", "&", "&&", "*", "+", ",", "-", "--", "/", "/*", "<", "<<", "<=", "<>", "=", ">", ">=", ">>", "AND", "BETWEEN", "COLLATE", "IN", "IS", "LIKE", "NOT", "OR", "REGEXP", "RLIKE", "USING", "XOR", "^", "div", "|", "||", "~", or [ \\t\\n\\r] but ")" found.')
895+
expect(parser.astify.bind(parser, sql)).to.throw('Expected "!=", "#", "%", "&", "&&", "*", "+", ",", "-", "--", "/", "/*", "<", "<<", "<=", "<>", "=", ">", ">=", ">>", "AND", "BETWEEN", "IN", "IS", "LIKE", "NOT", "ON", "OR", "OVER", "REGEXP", "RLIKE", "USING", "XOR", "^", "div", "|", "||", or [ \\t\\n\\r] but ")" found.')
896+
expect(parser.astify.bind(parser, 'select convert("");')).to.throw('Expected "!=", "#", "%", "&", "&&", "*", "+", ",", "-", "--", "/", "/*", "<", "<<", "<=", "<>", "=", ">", ">=", ">>", "AND", "BETWEEN", "COLLATE", "IN", "IS", "LIKE", "NOT", "OR", "REGEXP", "RLIKE", "USING", "XOR", "^", "div", "|", "||", or [ \\t\\n\\r] but ")" found.')
897897
})
898898

899899
it('should join multiple table with comma', () => {

0 commit comments

Comments
 (0)