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
43 changes: 43 additions & 0 deletions grammar/keywords.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,49 @@ 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_constant: _ => make_keyword("constant"),
keyword_alias: _ => make_keyword("alias"),
keyword_rowtype: _ => make_keyword("rowtype"),
keyword_cursor: _ => make_keyword("cursor"),
keyword_scroll: _ => make_keyword("scroll"),
keyword_open: _ => make_keyword("open"),
keyword_fetch: _ => make_keyword("fetch"),
keyword_move: _ => make_keyword("move"),
keyword_close: _ => make_keyword("close"),
keyword_next: _ => make_keyword("next"),
keyword_prior: _ => make_keyword("prior"),
keyword_absolute: _ => make_keyword("absolute"),
keyword_relative: _ => make_keyword("relative"),
keyword_forward: _ => make_keyword("forward"),
keyword_backward: _ => make_keyword("backward"),
keyword_perform: _ => make_keyword("perform"),
keyword_query: _ => make_keyword("query"),
keyword_call: _ => make_keyword("call"),

keyword_session: _ => make_keyword("session"),
keyword_isolation: _ => make_keyword("isolation"),
Expand Down
151 changes: 151 additions & 0 deletions grammar/statements/control-flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
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,
$.open_statement,
$.fetch_statement,
$.move_statement,
$.close_statement,
$.perform_statement,
$.execute_statement,
),

_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)),
),

};
115 changes: 81 additions & 34 deletions grammar/statements/create-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default {
function_argument: $ => seq(
optional($._argmode),
optional($.identifier),
$._type,
choice($._type, $.type_attribute),
optional(
seq(
choice($.keyword_default, '='),
Expand All @@ -78,29 +78,86 @@ export default {

_function_return: $ => seq(
$.keyword_return,
$._expression,
optional(
choice(
seq($.keyword_next, $._expression),
seq(
$.keyword_query,
choice(
$.execute_statement,
$.statement,
),
),
$._expression,
),
),
),

function_declaration: $ => seq(
$.identifier,
$._type,
optional(
seq(
':=',
choice(
wrapped_in_parenthesis($.statement),
// TODO are there more possibilities here? We can't use `_expression` since
// that includes subqueries
$.literal,
_declare_section: $ => seq(
$.keyword_declare,
repeat1(
choice(
$.function_declaration,
$.cursor_declaration,
),
),
),

function_declaration: $ => choice(
seq(
$.identifier,
optional($.keyword_constant),
choice($._type, $.type_attribute),
optional($._not_null),
optional(
seq(
choice(':=', '=', $.keyword_default),
choice(
wrapped_in_parenthesis($.statement),
// TODO are there more possibilities here? We can't use `_expression` since
// that includes subqueries
$.literal,
),
),
),
';',
),
seq(
$.identifier,
$.keyword_alias,
$.keyword_for,
choice($.parameter, $.identifier),
';',
),
),

// `employees.salary%type`, `employees%rowtype`
type_attribute: $ => seq(
$.object_reference,
'%',
choice($.keyword_type, $.keyword_rowtype),
),

cursor_declaration: $ => seq(
choice(
seq(
$.identifier,
optional(seq(optional($.keyword_no), $.keyword_scroll)),
$.keyword_cursor,
),
seq($.keyword_cursor, $.identifier),
),
optional($.function_arguments),
optional(seq($.keyword_return, choice($._type, $.type_attribute))),
choice($.keyword_for, $.keyword_is),
$.statement,
';',
),

_function_body_statement: $ => choice(
$.statement,
$._function_return,
$._procedural_statement,
),

_tsql_function_body_statement: $ => seq(
Expand All @@ -127,32 +184,17 @@ 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(
seq(
$.keyword_declare,
repeat1(
$.function_declaration,
),
),
),
optional($.label),
optional($._declare_section),
$.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 +212,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
Loading