Skip to content
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Custom

local_test/
tmp/
Cargo.lock
node_modules
Expand Down
26 changes: 20 additions & 6 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default grammar({
[$.between_expression, $.binary_expression],
[$.time],
[$.timestamp],
[$.term],
],

precedences: $ => [
Expand All @@ -56,14 +57,27 @@ export default grammar({
rules: {
program: $ => seq(
// any number of transactions, statements, or blocks with a terminating ;
// set_term, declare_external_function, and fb_* are self-terminating (Firebird dialect)
// '^' is a valid statement terminator in Firebird (after SET TERM ^ ;)
repeat(
seq(
choice(
$.transaction,
$.statement,
$.block,
choice(
$.set_term,
$.declare_external_function,
$.fb_proc_or_trigger,
$.fb_connect_statement,
$.fb_update_or_insert,
$.fb_grant_revoke,
seq(
choice(
$.transaction,
$.statement,
$.block,
// Firebird: standalone COMMIT WORK / ROLLBACK WORK outside BEGIN..END
$._commit,
$._rollback,
),
';',
),
';',
),
),
// optionally, a single statement without a terminating ;
Expand Down
74 changes: 66 additions & 8 deletions grammar/column-lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,24 @@ export default {
')',
),

column_definition: $ => prec.left(seq(
field('name', $._column),
field('type', $._type),
repeat($._column_constraint),
)),
column_definition: $ => prec.left(
choice(
// Firebird COMPUTED [BY] (expression) column — no explicit type
prec(5, seq(
field('name', $._column),
$.keyword_computed,
optional($.keyword_by),
wrapped_in_parenthesis($._expression),
repeat($._column_constraint),
)),
// Normal column: name type [constraints...]
seq(
field('name', $._column),
field('type', $._type),
repeat($._column_constraint),
),
)
),

_column_comment: $ => seq(
$.keyword_comment,
Expand Down Expand Up @@ -74,7 +87,13 @@ export default {
$.keyword_stored,
$.keyword_virtual,
),
$.keyword_unique
$.keyword_unique,
// Firebird: CHARACTER SET <charset> on VARCHAR/CHAR columns
seq($.keyword_character, $.keyword_set, $.identifier),
// Firebird: COLLATE <collation>
seq($.keyword_collate, $.identifier),
// Firebird: COMPUTED [BY] (expression) — optional after explicit type
seq($.keyword_computed, optional($.keyword_by), wrapped_in_parenthesis($._expression)),
)),

_check_constraint: $ => seq(
Expand All @@ -85,7 +104,7 @@ export default {
)
),
$.keyword_check,
wrapped_in_parenthesis($.binary_expression)
wrapped_in_parenthesis($._expression)
),

_default_expression: $ => seq(
Expand Down Expand Up @@ -119,23 +138,62 @@ export default {
$._check_constraint
),

// Firebird: USING [ASC|DESC|ASCENDING|DESCENDING] INDEX indexname
_fb_using_index: $ => seq(
$.keyword_using,
optional(choice($.keyword_ascending, $.keyword_descending, $.direction)),
$.keyword_index,
$.identifier,
),

_constraint_literal: $ => seq(
$.keyword_constraint,
field('name', $.identifier),
choice(
seq(
$._primary_key,
$.ordered_columns,
optional($._fb_using_index),
),
seq(
$._check_constraint
)
),
// Firebird/MySQL: CONSTRAINT name FOREIGN KEY (cols) REFERENCES table (cols) ON DELETE/UPDATE ...
seq(
optional($.keyword_foreign),
$.keyword_key,
$.ordered_columns,
optional(
seq(
$.keyword_references,
$.object_reference,
paren_list($.identifier, true),
repeat(
seq(
$.keyword_on,
choice($.keyword_delete, $.keyword_update),
choice(
seq($.keyword_no, $.keyword_action),
$.keyword_restrict,
$.keyword_cascade,
seq($.keyword_set, choice($.keyword_null, $.keyword_default),
optional(paren_list($.identifier, true))),
),
),
),
),
),
optional($._fb_using_index),
),
// CONSTRAINT name UNIQUE (cols)
seq($.keyword_unique, $.ordered_columns, optional($._fb_using_index)),
)
),

_primary_key_constraint: $ => seq(
$._primary_key,
$.ordered_columns,
optional($._fb_using_index),
),

_key_constraint: $ => seq(
Expand Down
33 changes: 29 additions & 4 deletions grammar/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default {
field('name', $.identifier),
),

parameter: $ => /\?|(\$[0-9]+)/,
parameter: $ => /\?|(\$[0-9]+)|(:[A-Za-z_][0-9A-Za-z_]*)/,

case: $ => seq(
$.keyword_case,
Expand Down Expand Up @@ -151,6 +151,25 @@ export default {
$.term
)
),
// Firebird DATEADD: DATEADD(quantity unit TO date)
wrapped_in_parenthesis(
seq(
field('parameter', $.term),
field('unit', $.object_reference),
$.keyword_to,
field('date', $.term),
)
),
// Firebird/SQL SUBSTRING(expr FROM pos FOR len) — FOR required to avoid conflict with EXTRACT
wrapped_in_parenthesis(
seq(
field('parameter', $.term),
$.keyword_from,
field('start', $.term),
$.keyword_for,
field('length', $.term),
)
),
// _aggregate_function, e.g. group_concat
wrapped_in_parenthesis(
seq(
Expand Down Expand Up @@ -360,7 +379,13 @@ export default {
$._dml_read
),

list: $ => paren_list($._expression),
list: $ => seq(
'(',
$._expression,
repeat(seq(',', $._expression)),
optional(','),
')',
),

literal: $ => prec(2,
choice(
Expand Down Expand Up @@ -415,8 +440,8 @@ export default {
seq("`", $._identifier, "`"),
),
_tsql_parameter: $ => seq('@', $._identifier),
// support nordic chars and umlaue
_identifier: _ => /[A-Za-z_\u00C0-\u017F][0-9A-Za-z_\u00C0-\u017F]*/,
// support nordic chars, umlauts, and Firebird $ in identifiers (e.g. REC$LastChanged)
_identifier: _ => /[A-Za-z_\u00C0-\u017F][0-9A-Za-z_$\u00C0-\u017F]*/,

object_id: $ => seq(
$.keyword_object_id,
Expand Down
23 changes: 23 additions & 0 deletions grammar/keywords.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export default {
keyword_engine: _ => make_keyword("engine"),
keyword_default: _ => make_keyword("default"),
keyword_cascade: _ => make_keyword("cascade"),
keyword_computed: _ => make_keyword("computed"),
keyword_restrict: _ => make_keyword("restrict"),
keyword_with: _ => make_keyword("with"),
keyword_without: _ => make_keyword("without"),
Expand Down Expand Up @@ -426,4 +427,26 @@ export default {

keyword_array: _ => make_keyword("array"), // not included in _type since it's a constructor literal

// Firebird dialect keywords
keyword_term: _ => make_keyword("term"),
keyword_domain: _ => make_keyword("domain"),
keyword_exception: _ => make_keyword("exception"),
keyword_generator: _ => make_keyword("generator"),
keyword_active: _ => make_keyword("active"),
keyword_inactive: _ => make_keyword("inactive"),
keyword_position: _ => make_keyword("position"),
keyword_suspend: _ => make_keyword("suspend"),
keyword_exit: _ => make_keyword("exit"),
keyword_variable: _ => make_keyword("variable"),
keyword_entry_point: _ => make_keyword("entry_point"),
keyword_module_name: _ => make_keyword("module_name"),
keyword_ascending: _ => make_keyword("ascending"),
keyword_descending: _ => make_keyword("descending"),
keyword_dialect: _ => make_keyword("dialect"),
keyword_sql: _ => make_keyword("sql"),
keyword_work: _ => make_keyword("work"),
keyword_global: _ => make_keyword("global"),
keyword_preserve: _ => make_keyword("preserve"),
keyword_rows: _ => make_keyword("rows"),

}
5 changes: 4 additions & 1 deletion grammar/statements/alter.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@ export default {
field("option", $.identifier),
)),
),
)
),
// Firebird: ALTER ROLE name SET AUTO ADMIN MAPPING [ON|OFF]
seq($.keyword_set, $.identifier, $.identifier, $.identifier,
optional(choice($.keyword_on, $.keyword_off))),
),
)),

Expand Down
18 changes: 18 additions & 0 deletions grammar/statements/create-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ export default {
$.keyword_end,
),

_fb_function_body_statement: $ => seq(
optional($.keyword_as),
repeat1($.fb_var_declaration),
$.keyword_begin,
optional($.var_declarations),
choice(
repeat($.statement),
repeat1(seq(
$.keyword_begin,
repeat($.statement),
$.keyword_end,
)),
),
optional($._function_return),
$.keyword_end,
),

function_body: $ => choice(
seq(
$._function_return,
Expand Down Expand Up @@ -175,6 +192,7 @@ export default {
alias($._dollar_quoted_string_end_tag, $.dollar_quote),
),
$._tsql_function_body_statement,
$._fb_function_body_statement,
),

function_language: $ => seq(
Expand Down
22 changes: 21 additions & 1 deletion grammar/statements/create-procedure.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export default {
optional($._if_not_exists),
$.object_reference,
optional($.function_arguments),
// Firebird output parameters
optional(seq($.keyword_returns, $.function_arguments)),
repeat(
choice(
$.function_language,
Expand Down Expand Up @@ -81,8 +83,10 @@ export default {
optional(';'),
alias($._dollar_quoted_string_end_tag, $.dollar_quote),
),
// T-SQL style (no required RETURN)
// T-SQL style (no required RETURN): AS BEGIN ... END
$._tsql_procedure_body_statement,
// Firebird style: [AS] DECLARE VARIABLE x type; ... BEGIN ... END
$._fb_procedure_body_statement,
),

_tsql_procedure_body_statement: $ => seq(
Expand All @@ -100,4 +104,20 @@ export default {
$.keyword_end,
),

_fb_procedure_body_statement: $ => seq(
optional($.keyword_as),
repeat1($.fb_var_declaration),
$.keyword_begin,
optional($.var_declarations),
choice(
repeat($.statement),
repeat1(seq(
$.keyword_begin,
repeat($.statement),
$.keyword_end,
)),
),
$.keyword_end,
),

};
Loading