Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@atsushieno
Created August 11, 2020 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atsushieno/4dc555221c738e2c641b2c85ac8b2f03 to your computer and use it in GitHub Desktop.
Save atsushieno/4dc555221c738e2c641b2c85ac8b2f03 to your computer and use it in GitHub Desktop.
experimental mugene2 grammar.js
module.exports = grammar({
name: 'mugene2',
rules: {
source_file: $ => $.expression_or_operation_uses,
expression_or_operation_uses: $ => choice(
$.operation_uses,
),
operation_uses: $ => repeat1($.operation_use),
operation_use: $ => choice(
seq($.can_be_identifier, optional($.arguments_opt_curly))
),
arguments_opt_curly: $ => choice(
seq('{', $.arguments, '}'),
seq('{', '}'),
$.arguments
),
arguments: $ => choice(
seq($.arguments, ',', $.argument),
$.argument
),
argument: $ => $.expression,
expression: $ => $.conditional_expr,
conditional_expr: $ => choice(
$.comparison_expr,
seq($.comparison_expr, '?', $.conditional_expr, ',', $.conditional_expr)
),
comparison_expr: $ => choice(
$.add_sub_expr,
seq($.add_sub_expr, $.comparison_operator, $.comparison_expr)
),
comparison_operator: $ => choice(
"\\<",
"\\<=",
"\\>",
"\\>="
),
add_sub_expr: $ => choice(
$.mul_div_mod_expr,
seq($.add_sub_expr, '+', $.mul_div_mod_expr),
seq($.add_sub_expr, '^', $.mul_div_mod_expr),
seq($.add_sub_expr, '-', $.mul_div_mod_expr)
),
mul_div_mod_expr: $ => choice(
$.primary_expr,
seq($.mul_div_mod_expr, '*', $.primary_expr),
seq($.mul_div_mod_expr, '/', $.primary_expr),
seq($.mul_div_mod_expr, '%', $.primary_expr)
),
primary_expr: $ => choice(
$.variable_reference,
$.string_constant,
//seq('{', $.expression, '}'),
$.step_constant,
$.unary_expr
),
unary_expr: $ => choice(
$.number_or_length_constant,
seq('-', $.number_or_length_constant),
seq('^', $.number_or_length_constant)
),
variable_reference: $ => seq('$', $.can_be_identifier),
string_constant: $ => $.string_literal,
step_constant: $ => seq('%', $.number_literal),
number_or_length_constant: $ => choice(
$.number_literal,
seq($.number_literal, $.dots),
$.dots
),
dots: $ => repeat1('.'),
can_be_identifier: $ => choice(
$.identifier,
':'
),
// terminals
string_literal: $ => choice(
seq('\'', /[A-Za-z_]+/, '\''),
seq('"', /[A-Za-z_]+/, '"')
),
number_literal: $=> /[0-9]+/,
identifier: $ => /[A-Za-z_!%&\(\)=~|\\\[\]<>@]+/
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment