Skip to content

Commit

Permalink
parser: add CASE statement
Browse files Browse the repository at this point in the history
Adds mostly compatible case statement, `OR WHEN` branch conditions are not yet implemented.

Reference: https://docs.progress.com/bundle/openedge-abl-reference-117/page/CASE-statement.html
  • Loading branch information
jkbz64 committed Oct 24, 2023
1 parent 53ac8db commit ae60468
Show file tree
Hide file tree
Showing 5 changed files with 22,914 additions and 19,255 deletions.
34 changes: 34 additions & 0 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ module.exports = grammar({
$.find_statement,
$.transaction_statement,
$._stream_statement,
$.case_statement,
$.input_close_statement,
$.output_close_statement,
$.assign_statement,
Expand Down Expand Up @@ -544,6 +545,39 @@ module.exports = grammar({
$._terminator
),

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

_case_terminator: ($) =>
choice($._block_terminator, seq(kw("END"), kw("CASE"), $._terminator)),

_case_body: ($) =>
choice(
$.do_block,
$.repeat_statement,
$.for_statement,
$._terminated_statement
),

case_when_branch: ($) =>
seq(
kw("WHEN"),
field("value", $._expression),
kw("THEN"),
field("body", $._case_body)
),
case_otherwise_branch: ($) =>
seq(kw("OTHERWISE"), field("body", $._case_body)),

case_statement: ($) =>
seq(
kw("CASE"),
$.identifier,
":",
repeat1($.case_when_branch),
optional($.case_otherwise_branch),
$._case_terminator
),

/// ABL queries
where_clause: ($) => seq(kw("WHERE"), field("condition", $._expression)),

Expand Down
250 changes: 250 additions & 0 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,10 @@
"type": "SYMBOL",
"name": "_stream_statement"
},
{
"type": "SYMBOL",
"name": "case_statement"
},
{
"type": "SYMBOL",
"name": "input_close_statement"
Expand Down Expand Up @@ -3941,6 +3945,252 @@
}
]
},
"do_block": {
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 1,
"content": {
"type": "PATTERN",
"value": "[dD][oO]"
}
}
},
"named": false,
"value": "DO"
},
{
"type": "STRING",
"value": ":"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "body"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_block_terminator"
}
]
},
"_case_terminator": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_block_terminator"
},
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 1,
"content": {
"type": "PATTERN",
"value": "[eE][nN][dD]"
}
}
},
"named": false,
"value": "END"
},
{
"type": "ALIAS",
"content": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 1,
"content": {
"type": "PATTERN",
"value": "[cC][aA][sS][eE]"
}
}
},
"named": false,
"value": "CASE"
},
{
"type": "SYMBOL",
"name": "_terminator"
}
]
}
]
},
"_case_body": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "do_block"
},
{
"type": "SYMBOL",
"name": "repeat_statement"
},
{
"type": "SYMBOL",
"name": "for_statement"
},
{
"type": "SYMBOL",
"name": "_terminated_statement"
}
]
},
"case_when_branch": {
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 1,
"content": {
"type": "PATTERN",
"value": "[wW][hH][eE][nN]"
}
}
},
"named": false,
"value": "WHEN"
},
{
"type": "FIELD",
"name": "value",
"content": {
"type": "SYMBOL",
"name": "_expression"
}
},
{
"type": "ALIAS",
"content": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 1,
"content": {
"type": "PATTERN",
"value": "[tT][hH][eE][nN]"
}
}
},
"named": false,
"value": "THEN"
},
{
"type": "FIELD",
"name": "body",
"content": {
"type": "SYMBOL",
"name": "_case_body"
}
}
]
},
"case_otherwise_branch": {
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 1,
"content": {
"type": "PATTERN",
"value": "[oO][tT][hH][eE][rR][wW][iI][sS][eE]"
}
}
},
"named": false,
"value": "OTHERWISE"
},
{
"type": "FIELD",
"name": "body",
"content": {
"type": "SYMBOL",
"name": "_case_body"
}
}
]
},
"case_statement": {
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 1,
"content": {
"type": "PATTERN",
"value": "[cC][aA][sS][eE]"
}
}
},
"named": false,
"value": "CASE"
},
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "STRING",
"value": ":"
},
{
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "case_when_branch"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "case_otherwise_branch"
},
{
"type": "BLANK"
}
]
},
{
"type": "SYMBOL",
"name": "_case_terminator"
}
]
},
"where_clause": {
"type": "SEQ",
"members": [
Expand Down
Loading

0 comments on commit ae60468

Please sign in to comment.