Skip to content

Commit

Permalink
Added Rules with conditions
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal <[email protected]>
  • Loading branch information
Vishalk91-4 committed Aug 3, 2024
1 parent 5621fed commit 4585159
Show file tree
Hide file tree
Showing 5 changed files with 298,321 additions and 111,916 deletions.
74 changes: 57 additions & 17 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ module.exports = grammar({

_compound_statement: $ => choice(
$.if_statement,
$.if_rule_statement,
$.schema_index_signature,
// TODO: schema and rule statement grammars
$.schema_statement,
Expand All @@ -269,24 +270,59 @@ module.exports = grammar({
$.decorated_definition,
),

if_statement: $ => choice(seq(
'if',
field('condition', $.expression),
':',
field('consequence', $._suite),
repeat(field('alternative', $.elif_clause)),
optional(field('alternative', $.else_clause)),
),
if_statement: $ => choice(
seq(
'if',
field('condition', $.expression),
optional(':'),
field('consequence', $._suite),
repeat(field('alternative', $.elif_clause)),
optional(field('alternative', $.else_clause)),
),
seq(
field('condition',$.expression),
',',
field('error_message', $.string),
),
),
$.conditional_expression,
seq(
field('expr1',$.expression),
'if',
field('condition',$.expression),
field('condition', $.expression),
)
),

if_rule_statement: $ => prec.left(4, choice(
seq(
'if',
field('condition', $.expression),
optional(seq(':', field('separator', $.string))),
field('consequence', $._suite),
repeat(field('alternative', $.elif_clause)),
optional(field('alternative', $.else_clause)),
),
seq(
field('condition', $.expression),
',',
field('error_message', $.string),
),
seq(
field('expr1', $.expression),
'if',
field('condition', $.expression),
)
)),

rule_statement: $ => seq(
'rule',
field('name', $.identifier),
optional(seq(
'for',
field('protocol', $.identifier)
)),
':',
field('body', $._suite)
),

elif_clause: $ => seq(
'elif',
Expand Down Expand Up @@ -352,7 +388,7 @@ module.exports = grammar({
field('quant_op', $.quant_op),
optional(seq(
field('identifier', $.identifier),
',',
','
)),
field('identifier', $.identifier),
'in',
Expand All @@ -369,7 +405,7 @@ module.exports = grammar({
'if',
field('expr2', $.expression)
)),
'}',
'}'
)),

quant_target: $ => prec(1, choice(
Expand Down Expand Up @@ -452,6 +488,10 @@ module.exports = grammar({
rule_statement: $ => seq(
'rule',
field('name', $.identifier),
optional(seq(
'for',
field('protocol', $.identifier),
)),
':',
field('body', $._suite),
),
Expand Down Expand Up @@ -542,14 +582,14 @@ module.exports = grammar({
// Expressions

expression: $ => prec(1, choice(
$.conditional_expression,
$.sequence_operation,
$.comparison_operator,
$.not_operator,
$.boolean_operator,
$.selector_expression,
$.primary_expression,
$.as_expression,
$.conditional_expression,
$.long_expression,
)),

Expand Down Expand Up @@ -595,7 +635,7 @@ module.exports = grammar({
$.null_coalesce,
$.string_literal_expr,
$.config_expr,
$.selector_expression
$.selector_expression,
)),

paren_expression: $ => seq(
Expand Down Expand Up @@ -969,12 +1009,12 @@ module.exports = grammar({
$.expression,
),

conditional_expression: $ => prec.right(PREC.conditional, seq(
conditional_expression: $ => prec.right(PREC.conditional + 24, seq(
$.expression,
'if',
$.expression,
'else',
$.expression,
optional('else'),
optional($.expression),
)),

raw_string: $ => prec(64, seq(
Expand Down
Loading

0 comments on commit 4585159

Please sign in to comment.