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

refactor: programs as delimited sequences of statements #164

Merged
merged 2 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
147 changes: 71 additions & 76 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ module.exports = grammar({
conflicts: $ => [
[$.object_reference, $._qualified_field],
[$.object_reference],
// TODO these two have internal conflicts because of optional parenthesized
// settings which can be interpreted as _subsequent statements_ due to our
// current handling of statement delimiters in program nodes. Remove once
// we have expressed programs as delimited sequences of statements.
[$._vacuum_table],
[$._compute_stats],
],

precedences: $ => [
Expand All @@ -40,12 +34,21 @@ module.exports = grammar({
word: $ => $._identifier,

rules: {
program: $ => repeat(
// TODO: other kinds of definitions
choice(
$.transaction,
program: $ => seq(
// any number of transactions, statements, or blocks with a terminating ;
repeat(
seq(
choice(
$.transaction,
$.statement,
$.block,
),
';',
),
),
// optionally, a single statement without a terminating ;
optional(
$.statement,
$.compound_statement,
),
),

Expand Down Expand Up @@ -489,13 +492,48 @@ module.exports = grammar({
// https://stackoverflow.com/questions/13014947/regex-to-match-a-c-style-multiline-comment
marginalia: _ => seq('/*', /[^*]*\*+(?:[^/*][^*]*\*+)*/, '/' ),

compound_statement: $ => seq(
Copy link
Collaborator

@matthias-Q matthias-Q Jun 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In nvim-treesitter there is a indentens.scm, which refenrences compound_Statement. We should have added a copy of this to our repo as well, so that the tests fail here when we rename nodes.

transaction: $ => seq(
$.keyword_begin,
repeat1(
$.statement,
optional(
$.keyword_transaction,
),
$.keyword_end,
optional(';'),
repeat(
seq(
$.statement,
';'
),
),
choice(
$._commit,
$._rollback,
),
),

_commit: $ => seq(
$.keyword_commit,
optional(
$.keyword_transaction,
),
),

_rollback: $ => seq(
$.keyword_rollback,
optional(
$.keyword_transaction,
),
),

block: $ => seq(
$.keyword_begin,
optional(';'),
repeat(
seq(
$.statement,
';'
),
),
$.keyword_end,
),

statement: $ => seq(
Expand All @@ -509,7 +547,6 @@ module.exports = grammar({
')',
),
),
optional(';'),
),

_ddl_statement: $ => choice(
Expand Down Expand Up @@ -576,41 +613,6 @@ module.exports = grammar({
')',
),

transaction: $ => seq(
$._begin,
repeat(
$.statement,
),
choice(
$._commit,
$._rollback,
),
),

_begin: $ => seq(
$.keyword_begin,
optional(
$.keyword_transaction,
),
';',
),

_commit: $ => seq(
$.keyword_commit,
optional(
$.keyword_transaction,
),
';',
),

_rollback: $ => seq(
$.keyword_rollback,
optional(
$.keyword_transaction,
),
';',
),

_select_statement: $ => seq(
$.select,
optional($.from),
Expand Down Expand Up @@ -841,13 +843,7 @@ module.exports = grammar({
$.keyword_create,
optional($._or_replace),
$.keyword_function,
optional(
seq(
field('schema', $.identifier),
'.',
),
),
field('name', $.identifier),
$.object_reference,
choice(
$.column_definitions, // TODO `default` will require own node type
seq('(', ')'),
Expand Down Expand Up @@ -891,7 +887,6 @@ module.exports = grammar({
_function_return: $ => seq(
$.keyword_return,
$._expression,
';',
),

function_declaration: $ => seq(
Expand All @@ -915,15 +910,23 @@ module.exports = grammar({
';',
),

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

function_body: $ => choice(
seq(
$._function_return,
';'
),
seq(
$.keyword_begin,
$.keyword_atomic,
repeat1(
choice(
$.statement,
$._function_return,
seq(
$._function_body_statement,
';',
),
),
$.keyword_end,
Expand All @@ -941,9 +944,9 @@ module.exports = grammar({
),
$.keyword_begin,
repeat1(
choice(
$.statement,
$._function_return,
seq(
$._function_body_statement,
';',
),
),
$.keyword_end,
Expand All @@ -952,20 +955,12 @@ module.exports = grammar({
),
seq(
$.keyword_as,
'\'',
choice(
$.statement,
$._function_return,
),
'\'',
alias($._literal_string, $.literal),
),
seq(
$.keyword_as,
$.dollar_quote,
choice(
$.statement,
$._function_return,
),
$._function_body_statement,
$.dollar_quote,
),
),
Expand Down
2 changes: 1 addition & 1 deletion test/corpus/compound_statements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ END;
--------------------------------------------------------------------------------

(program
(compound_statement
(block
(keyword_begin)
(statement
(create_table
Expand Down
72 changes: 32 additions & 40 deletions test/corpus/functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,9 @@ return 1;
(keyword_or)
(keyword_replace)
(keyword_function)
(identifier)
(identifier)
(object_reference
(identifier)
(identifier))
(keyword_returns)
(int
(keyword_int))
Expand Down Expand Up @@ -505,8 +506,9 @@ return 1;
(keyword_or)
(keyword_replace)
(keyword_function)
(identifier)
(identifier)
(object_reference
(identifier)
(identifier))
(column_definitions
(column_definition
(identifier)
Expand Down Expand Up @@ -548,8 +550,9 @@ return 1;
(keyword_or)
(keyword_replace)
(keyword_function)
(identifier)
(identifier)
(object_reference
(identifier)
(identifier))
(keyword_returns)
(int
(keyword_int))
Expand Down Expand Up @@ -596,8 +599,9 @@ create or replace function public.fn()
(keyword_or)
(keyword_replace)
(keyword_function)
schema: (identifier)
name: (identifier)
(object_reference
schema: (identifier)
name: (identifier))
(keyword_returns)
(int
(keyword_int))
Expand Down Expand Up @@ -650,21 +654,17 @@ as 'select 1;';
(keyword_or)
(keyword_replace)
(keyword_function)
(identifier)
(identifier)
(object_reference
(identifier)
(identifier))
(keyword_returns)
(int
(keyword_int))
(function_language
(keyword_sql))
(function_body
(keyword_as)
(statement
(select
(keyword_select)
(select_expression
(term
(literal)))))))))
(literal)))))

================================================================================
Precedence between string body and `create table` with string options
Expand All @@ -684,29 +684,17 @@ as 'create table x (id int) row_format=dynamic';
(keyword_or)
(keyword_replace)
(keyword_function)
(identifier)
(identifier)
(object_reference
(identifier)
(identifier))
(keyword_returns)
(int
(keyword_int))
(function_language
(keyword_sql))
(function_body
(keyword_as)
(statement
(create_table
(keyword_create)
(keyword_table)
(object_reference
(identifier))
(column_definitions
(column_definition
(identifier)
(int
(keyword_int))))
(table_option
(identifier)
(identifier))))))))
(literal)))))

================================================================================
With `begin atomic`
Expand All @@ -728,8 +716,9 @@ end;
(keyword_or)
(keyword_replace)
(keyword_function)
(identifier)
(identifier)
(object_reference
(identifier)
(identifier))
(keyword_returns)
(int
(keyword_int))
Expand Down Expand Up @@ -764,8 +753,9 @@ $function$;
(keyword_or)
(keyword_replace)
(keyword_function)
(identifier)
(identifier)
(object_reference
(identifier)
(identifier))
(keyword_returns)
(int
(keyword_int))
Expand Down Expand Up @@ -806,8 +796,9 @@ $function$;
(keyword_or)
(keyword_replace)
(keyword_function)
(identifier)
(identifier)
(object_reference
(identifier)
(identifier))
(keyword_returns)
(int
(keyword_int))
Expand Down Expand Up @@ -886,8 +877,9 @@ $function$
(keyword_or)
(keyword_replace)
(keyword_function)
(identifier)
(identifier)
(object_reference
(identifier)
(identifier))
(keyword_returns)
(keyword_trigger)
(function_language
Expand Down