Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: complete unary operator/expression support #163

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,26 @@ export const lexer = compile({
op_like: /~~/, // ~~ =LIKE
op_mod: '%',
op_exp: '^',
op_square_root: '|/', // https://www.postgresql.org/docs/16/functions-math.html
op_cube_root: '||/', // https://www.postgresql.org/docs/16/functions-math.html
op_double_at: '@@', // https://www.postgresql.org/docs/16/functions-geometry.html and https://www.postgresql.org/docs/16/functions-json.html
op_tilde_star: '~*', // https://www.postgresql.org/docs/16/functions-matching.html
op_tilde: '~', // https://www.postgresql.org/docs/16/functions-bitstring.html and https://www.postgresql.org/docs/current/functions-matching.html
op_question_mark_dash: '?-', // https://www.postgresql.org/docs/16/functions-geometry.html
op_question_mark_pipe: '?|', // https://www.postgresql.org/docs/16/functions-geometry.html
op_hash_rangle_rangle: '#>>', // https://www.postgresql.org/docs/16/functions-json.html
op_hash: '#', // https://www.postgresql.org/docs/16/functions-geometry.html
op_additive: {
// group other additive operators
match: ['||', '-', '#-', '&&'],
},
op_compare: {
// group other comparison operators
// ... to add: "IN" and "NOT IN" that are matched by keywords
match: ['>', '>=', '<', '<=', '@>', '<@', '?', '?|', '?&', '#>>', '>>', '<<', '~', '~*', '!~', '!~*', '@@'],
match: ['>', '>=', '<', '<=', '@>', '<@', '?', '?&', '>>', '<<', '!~', '!~*'],
},
op_others_unary: {
match: ['@', '@-@', '!!'],
},
ops_others: {
// referenced as (any other operator) in https://www.postgresql.org/docs/12/sql-syntax-lexical.html#SQL-PRECEDENCE
Expand Down
21 changes: 20 additions & 1 deletion src/syntax/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,26 @@ export interface ExprCast extends PGNode {
}


export type UnaryOperator = '+' | '-' | 'NOT' | 'IS NULL' | 'IS NOT NULL' | 'IS TRUE' | 'IS FALSE' | 'IS NOT TRUE' | 'IS NOT FALSE';
export type UnaryOperator = '+'
| '-'
| 'NOT'
| 'IS NULL'
| 'IS NOT NULL'
| 'IS TRUE'
| 'IS FALSE'
| 'IS NOT TRUE'
| 'IS NOT FALSE'
| '|/'
| '||/'
| '@'
| '~'
| '@-@'
| '@@'
| '#'
| '?-'
| '?|'
| '!!'

export interface ExprUnary extends PGNode {
type: 'unary';
operand: Expr;
Expand Down
4 changes: 2 additions & 2 deletions src/syntax/expr.ne
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ expr_is
| expr_compare {% unwrap %}


expr_compare -> expr_binary[op_scopable[%op_compare], expr_compare, expr_range]
expr_compare -> expr_binary[op_scopable[(%op_question_mark_pipe | %op_tilde_star | %op_tilde | %op_double_at | %op_hash_rangle_rangle | %op_compare)], expr_compare, expr_range]
expr_range -> expr_ternary[ops_between, %kw_and, expr_range, expr_others]
expr_others -> expr_binary[op_scopable[%ops_others], expr_others, expr_like]
expr_like -> expr_binary[op_single[ops_like], expr_like, expr_in]
expr_in -> expr_binary[op_single[ops_in], expr_in, expr_add]
expr_add -> expr_binary[op_scopable[(%op_plus | %op_minus | %op_additive)], expr_add, expr_mult]
expr_mult -> expr_binary[op_scopable[(%star | %op_div | %op_mod)], expr_mult, expr_exp]
expr_exp -> expr_binary[op_scopable[%op_exp], expr_exp, expr_unary_add]
expr_unary_add -> expr_left_unary[op_scopable[(%op_plus | %op_minus)], expr_unary_add, expr_various_constructs]
expr_unary_add -> expr_left_unary[op_scopable[(%op_plus | %op_minus | %op_square_root | %op_cube_root | %op_double_at | %op_tilde | %op_question_mark_dash | %op_question_mark_pipe | %op_hash | %op_others_unary)], expr_unary_add, expr_various_constructs]
expr_various_constructs -> expr_binary[op_single[various_binaries], expr_various_constructs, expr_array_index]

expr_array_index
Expand Down
62 changes: 61 additions & 1 deletion src/syntax/expr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,66 @@ line`,
operand: { type: 'ref', name: 'a' }
});

checkTreeExpr('|/ a', {
type: 'unary',
op: '|/',
operand: { type: 'ref', name: 'a' }
});

checkTreeExpr('||/ a', {
type: 'unary',
op: '||/',
operand: { type: 'ref', name: 'a' }
});

checkTreeExpr('@ a', {
type: 'unary',
op: '@',
operand: { type: 'ref', name: 'a' }
});

checkTreeExpr('~ a', {
type: 'unary',
op: '~',
operand: { type: 'ref', name: 'a' }
});

checkTreeExpr('@-@ a', {
type: 'unary',
op: '@-@',
operand: { type: 'ref', name: 'a' }
});

checkTreeExpr('@@ a', {
type: 'unary',
op: '@@',
operand: { type: 'ref', name: 'a' }
});

checkTreeExpr('# a', {
type: 'unary',
op: '#',
operand: { type: 'ref', name: 'a' }
});

checkTreeExpr('?- a', {
type: 'unary',
op: '?-',
operand: { type: 'ref', name: 'a' }
});

checkTreeExpr('?| a', {
type: 'unary',
op: '?|',
operand: { type: 'ref', name: 'a' }
});

checkTreeExpr('!! a', {
type: 'unary',
op: '!!',
operand: { type: 'ref', name: 'a' }
});

});


Expand Down Expand Up @@ -1635,4 +1695,4 @@ line`,
args: [],
});
})
});
});
10 changes: 10 additions & 0 deletions src/to-sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,16 @@ const visitor = astVisitor<IAstFullVisitor>(m => ({
switch (t.op) {
case '+':
case '-':
case '|/':
case '||/':
case '@':
case '~':
case '@-@':
case '@@':
case '#':
case '?-':
case '?|':
case '!!':
// prefix ops
visitOp(t);
m.expr(t.operand);
Expand Down