Skip to content
Draft
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
25 changes: 25 additions & 0 deletions grammar/keywords.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,31 @@ export default {
keyword_inout: _ => make_keyword("inout"),
keyword_variadic: _ => make_keyword("variadic"),
keyword_ordinality: _ => make_keyword("ordinality"),
keyword_loop: _ => make_keyword("loop"),
keyword_exit: _ => make_keyword("exit"),
keyword_continue: _ => make_keyword("continue"),
keyword_elsif: _ => make_keyword("elsif"),
keyword_elseif: _ => make_keyword("elseif"),
keyword_reverse: _ => make_keyword("reverse"),
keyword_foreach: _ => make_keyword("foreach"),
keyword_slice: _ => make_keyword("slice"),
keyword_raise: _ => make_keyword("raise"),
keyword_exception: _ => make_keyword("exception"),
keyword_debug: _ => make_keyword("debug"),
keyword_log: _ => make_keyword("log"),
keyword_info: _ => make_keyword("info"),
keyword_notice: _ => make_keyword("notice"),
keyword_warning: _ => make_keyword("warning"),
keyword_sqlstate: _ => make_keyword("sqlstate"),
keyword_message: _ => make_keyword("message"),
keyword_detail: _ => make_keyword("detail"),
keyword_hint: _ => make_keyword("hint"),
keyword_errcode: _ => make_keyword("errcode"),
keyword_datatype: _ => make_keyword("datatype"),
keyword_assert: _ => make_keyword("assert"),
keyword_get: _ => make_keyword("get"),
keyword_stacked: _ => make_keyword("stacked"),
keyword_diagnostics: _ => make_keyword("diagnostics"),

keyword_session: _ => make_keyword("session"),
keyword_isolation: _ => make_keyword("isolation"),
Expand Down
145 changes: 145 additions & 0 deletions grammar/statements/control-flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { comma_list, paren_list } from "../helpers.js";

// Procedural statements shared by PL/pgSQL and PL/SQL. They are only reachable
// from a function or procedure body and from a BEGIN ... END block, never from
// a plain top-level statement.
export default {

_procedural_statement: $ => choice(
alias($._variable_assignment, $.assignment),
$.if_statement,
$.case_statement,
$.loop_statement,
$.exit_statement,
$.continue_statement,
$.keyword_null,
$.block,
$.raise_statement,
$.assert_statement,
$.get_diagnostics,
),

_procedural_statements: $ => repeat1(
seq(
$._function_body_statement,
';',
),
),

_variable_assignment: $ => seq(
field('left', alias($._qualified_field, $.field)),
choice(':=', '='),
field('right', $._expression),
),

label: $ => seq('<<', $.identifier, '>>'),

if_statement: $ => seq(
$.keyword_if,
$._expression,
$.keyword_then,
optional($._procedural_statements),
repeat(
seq(
choice($.keyword_elsif, $.keyword_elseif),
$._expression,
$.keyword_then,
optional($._procedural_statements),
),
),
optional(
seq(
$.keyword_else,
optional($._procedural_statements),
),
),
$.keyword_end,
$.keyword_if,
),

case_statement: $ => seq(
$.keyword_case,
optional($._expression),
repeat1(
seq(
$.keyword_when,
comma_list($._expression, true),
$.keyword_then,
optional($._procedural_statements),
),
),
optional(
seq(
$.keyword_else,
optional($._procedural_statements),
),
),
$.keyword_end,
$.keyword_case,
),

loop_statement: $ => seq(
optional($.label),
optional(
choice(
seq($.keyword_while, $._expression),
seq(
$.keyword_for,
comma_list($.identifier, true),
$.keyword_in,
choice(
$._for_range,
$.statement,
// a bound cursor, with or without arguments
alias($._qualified_field, $.field),
$.invocation,
),
),
seq(
$.keyword_foreach,
comma_list($.identifier, true),
optional(seq($.keyword_slice, $._natural_number)),
$.keyword_in,
$.keyword_array,
$._expression,
),
),
),
$.keyword_loop,
optional($._procedural_statements),
$.keyword_end,
$.keyword_loop,
optional($.identifier),
),

_for_range: $ => seq(
optional($.keyword_reverse),
choice(
alias($._range_start_integer, $.literal),
$._expression,
),
'..',
$._expression,
optional(seq($.keyword_by, $._expression)),
),

// `1..10` would otherwise lex as the two decimals `1.` and `.10`; a higher
// lexical precedence stops the integer from absorbing the dot.
_range_start_integer: $ => seq(
optional(choice('-', '+')),
token(prec(1, /\d+/)),
),

exit_statement: $ => seq(
$.keyword_exit,
optional($.identifier),
optional(seq($.keyword_when, $._expression)),
),

continue_statement: $ => seq(
$.keyword_continue,
optional($.identifier),
optional(seq($.keyword_when, $._expression)),
),

};
26 changes: 12 additions & 14 deletions grammar/statements/create-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default {

_function_return: $ => seq(
$.keyword_return,
$._expression,
optional($._expression),
),

function_declaration: $ => seq(
Expand All @@ -101,6 +101,7 @@ export default {
_function_body_statement: $ => choice(
$.statement,
$._function_return,
$._procedural_statement,
),

_tsql_function_body_statement: $ => seq(
Expand All @@ -127,17 +128,13 @@ export default {
seq(
$.keyword_begin,
$.keyword_atomic,
repeat1(
seq(
$._function_body_statement,
';',
),
),
$._procedural_statements,
$.keyword_end,
),
seq(
$.keyword_as,
alias($._dollar_quoted_string_start_tag, $.dollar_quote),
optional($.label),
optional(
seq(
$.keyword_declare,
Expand All @@ -147,12 +144,8 @@ export default {
),
),
$.keyword_begin,
repeat1(
seq(
$._function_body_statement,
';',
),
),
$._procedural_statements,
optional($._exception_handlers),
$.keyword_end,
optional(';'),
alias($._dollar_quoted_string_end_tag, $.dollar_quote),
Expand All @@ -170,7 +163,12 @@ export default {
seq(
$.keyword_as,
alias($._dollar_quoted_string_start_tag, $.dollar_quote),
$._function_body_statement,
// a single SQL statement, e.g. a `language sql` body; a procedural
// statement here would be ambiguous with the `begin ... end` form
choice(
$.statement,
$._function_return,
),
optional(';'),
alias($._dollar_quoted_string_end_tag, $.dollar_quote),
),
Expand Down
23 changes: 10 additions & 13 deletions grammar/statements/create-procedure.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,14 @@ export default {
seq(
$.keyword_begin,
$.keyword_atomic,
repeat1(
seq(
$._function_body_statement,
';',
),
),
$._procedural_statements,
$.keyword_end,
),
// Dollar-quoted with optional DECLARE (PostgreSQL)
seq(
$.keyword_as,
alias($._dollar_quoted_string_start_tag, $.dollar_quote),
optional($.label),
optional(
seq(
$.keyword_declare,
Expand All @@ -52,12 +48,8 @@ export default {
),
),
$.keyword_begin,
repeat1(
seq(
$._function_body_statement,
';',
),
),
$._procedural_statements,
optional($._exception_handlers),
$.keyword_end,
optional(';'),
alias($._dollar_quoted_string_end_tag, $.dollar_quote),
Expand All @@ -77,7 +69,12 @@ export default {
seq(
$.keyword_as,
alias($._dollar_quoted_string_start_tag, $.dollar_quote),
$._function_body_statement,
// a single SQL statement, e.g. a `language sql` body; a procedural
// statement here would be ambiguous with the `begin ... end` form
choice(
$.statement,
$._function_return,
),
optional(';'),
alias($._dollar_quoted_string_end_tag, $.dollar_quote),
),
Expand Down
90 changes: 90 additions & 0 deletions grammar/statements/error-handling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { comma_list } from "../helpers.js";

// RAISE, ASSERT, EXCEPTION handlers and GET DIAGNOSTICS, as used by PL/pgSQL
// and PL/SQL bodies.
export default {

raise_statement: $ => seq(
$.keyword_raise,
optional(
choice(
$.keyword_debug,
$.keyword_log,
$.keyword_info,
$.keyword_notice,
$.keyword_warning,
$.keyword_exception,
),
),
optional(
choice(
seq($.keyword_sqlstate, alias($._literal_string, $.literal)),
// a format string with its arguments, or a bare condition name
comma_list($._expression, true),
),
),
optional(
seq(
$.keyword_using,
comma_list($.raise_option, true),
),
),
),

raise_option: $ => seq(
choice(
$.keyword_message,
$.keyword_detail,
$.keyword_hint,
$.keyword_errcode,
$.keyword_column,
$.keyword_constraint,
$.keyword_datatype,
$.keyword_table,
$.keyword_schema,
),
choice('=', ':='),
$._expression,
),

assert_statement: $ => seq(
$.keyword_assert,
$._expression,
optional(seq(',', $._expression)),
),

// `exception when ... then ...`, at the end of a block or body
_exception_handlers: $ => seq(
$.keyword_exception,
repeat1($.exception_handler),
),

exception_handler: $ => seq(
$.keyword_when,
$._exception_condition,
repeat(seq($.keyword_or, $._exception_condition)),
$.keyword_then,
optional($._procedural_statements),
),

_exception_condition: $ => choice(
$.keyword_others,
seq($.keyword_sqlstate, alias($._literal_string, $.literal)),
$.identifier,
),

get_diagnostics: $ => seq(
$.keyword_get,
optional(choice($.keyword_current, $.keyword_stacked)),
$.keyword_diagnostics,
comma_list(
seq(
alias($._qualified_field, $.field),
choice('=', ':='),
$.identifier,
),
true,
),
),

};
Loading