Skip to content

Commit

Permalink
parser: break up keyword in some phrases
Browse files Browse the repository at this point in the history
This makes the parser less rigid, allowing for comments, line-breaks,
includes and multiple white space between the keyword as well as
outputting a syntax tree that's more faithful to the actual content of
the code.

I also took the opportunity to make the "PROCEDURE" keyword of the block
terminator of the PROCEDURE statement optional and to remove the
useless space after the "AVAIL[ABLE]" keyword.
  • Loading branch information
jchros committed Sep 9, 2023
1 parent bae5240 commit 56a748c
Show file tree
Hide file tree
Showing 5 changed files with 35,408 additions and 63,793 deletions.
33 changes: 23 additions & 10 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,21 +285,24 @@ module.exports = grammar({
seq(
kw("IF"),
field("condition", $._expression),
kw("THEN DO"),
kw("THEN"),
kw("DO"),
":",
optional($.body),
$._block_terminator,
optional(repeat(choice($.else_do_statement, $.else_do_if_statement)))
),

else_do_statement: ($) =>
seq(kw("ELSE DO"), ":", optional($.body), $._block_terminator),
seq(kw("ELSE"), kw("DO"), ":", optional($.body), $._block_terminator),

else_do_if_statement: ($) =>
seq(
kw("ELSE IF"),
kw("ELSE"),
kw("IF"),
field("condition", $._expression),
kw("THEN DO"),
kw("THEN"),
kw("DO"),
":",
optional($.body),
$._block_terminator
Expand Down Expand Up @@ -337,7 +340,8 @@ module.exports = grammar({

do_while_statement: ($) =>
seq(
kw("DO WHILE"),
kw("DO"),
kw("WHILE"),
field("condition", $._expression),
":",
optional($.body),
Expand All @@ -356,15 +360,15 @@ module.exports = grammar({
),

/// Procedures
_procedure_terminator: ($) => kw("END PROCEDURE."),
_procedure_terminator: ($) => seq(kw("END"), kw("PROCEDURE"), "."),
procedure_statement: ($) =>
seq(
kw("PROCEDURE"),
$.identifier,
optional(kw("PRIVATE")),
":",
optional($.body),
$._procedure_terminator
choice($._block_terminator, $._procedure_terminator)
),

procedure_parameter_definition: ($) =>
Expand All @@ -381,7 +385,10 @@ module.exports = grammar({

/// Functions
_function_terminator: ($) =>
choice($._block_terminator, seq(kw("END FUNCTION"), ".")),
choice(
$._block_terminator,
seq(kw("END"), kw("FUNCTION"), $._terminator)
),
function_parameter_mode: ($) => choice(kw("INPUT"), kw("OUTPUT")),
function_parameter: ($) =>
seq($.function_parameter_mode, $.identifier, kw("AS"), $.primitive_type),
Expand Down Expand Up @@ -616,7 +623,13 @@ module.exports = grammar({

// DO TRANSACTION statement
transaction_statement: ($) =>
seq(kw("DO TRANSACTION"), ":", optional($.body), $._block_terminator),
seq(
kw("DO"),
kw("TRANSACTION"),
":",
optional($.body),
$._block_terminator
),

/// ABL statements
abl_statement: ($) =>
Expand Down Expand Up @@ -693,7 +706,7 @@ module.exports = grammar({
// Available
available_expression: ($) =>
seq(
choice(kw("AVAIL "), kw("AVAILABLE ")),
choice(kw("AVAIL"), kw("AVAILABLE")),
choice($.parenthesized_expression, $.identifier)
)
}
Expand Down
Loading

0 comments on commit 56a748c

Please sign in to comment.