grammar.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // @ts-nocheck
  2. // DO NOT EDIT TOKENS/RULES (generated from syntax_test.go)
  3. // TOKENS
  4. const Doc = /\/\/\/[^\n]*/
  5. const Comment = /\/\/[^\n]*|\/\*([^\*\/]|\*[^\/]|[^\*]\/)*\*\//
  6. const Blank = /[ \t\r ]+/
  7. const LF = /[\n]+/
  8. const Text = /'[^']*'|"[^"]*"/
  9. const Int = /\-?0[xX][0-9A-Fa-f]+|\-?0[oO][0-7]+|\-?0[bB][01]+|\-?\d+/
  10. const Float = /\-?\d+(\.\d+)?[Ee][\+\-]?\d+|\-?\d+\.\d+/
  11. const Byte = /\\x[0-9A-Fa-f][0-9A-Fa-f]/
  12. const Char = /`.`|\\u[0-9A-Fa-f]+|\\[a-z]/
  13. const Name = /[^0-9\{\}\[\]\(\)\.,:;@\&\|\\'"` \t\r \n][^\{\}\[\]\(\)\.,:;@\&\|\\'"` \t\r \n]*/
  14. const IgnoreRule = $ => Comment
  15. const IgnoreTokens = [Blank, LF]
  16. const IgnoreRuleKey = 'tree_sitter_ignore_rule'
  17. module.exports = grammar({
  18. name: 'kumachan',
  19. extras: $ => [$[IgnoreRuleKey], ...IgnoreTokens],
  20. rules: (raw => {
  21. let rules = {}
  22. for (let [k,v] of Object.entries(raw)) {
  23. /*
  24. let decorate = (g, f) => $ => f(g($))
  25. if (k == 'inline_type_args' || k == 'inline_ref') {
  26. v = decorate(v, x => prec.left(x))
  27. } else {
  28. v = decorate(v, x => prec.right(x))
  29. }
  30. if (k == 'call_cps') {
  31. v = decorate(v, x => prec(1, x))
  32. }
  33. */
  34. rules[k] = v
  35. }
  36. rules[IgnoreRuleKey] = IgnoreRule
  37. return rules
  38. })({
  39. // RULES
  40. source_file: $ => seq($.ns, repeat($.stmt)),
  41. ns: $ => seq('namespace', optional($.name), '::'),
  42. name: $ => Name,
  43. stmt: $ => choice($.decl_entry, $.decl_type, $.decl_func, $.decl_const, $.decl_method),
  44. decl_entry: $ => seq('entry', $.block),
  45. decl_type: $ => seq(optional($.docs), 'type', $.name, optional($.type_params), optional($.impl), $.type_def),
  46. type_params: $ => seq('[', optional(seq($.name, repeat(seq(',', $.name)))), ']'),
  47. docs: $ => repeat1($.doc),
  48. doc: $ => Doc,
  49. impl: $ => seq('(', optional(seq($.ref_base, repeat(seq(',', $.ref_base)))), ')'),
  50. ref_base: $ => seq(optional($.ns_prefix), $.name),
  51. ns_prefix: $ => seq($.name, '::'),
  52. type_def: $ => choice('native', $.record, $.interface, $.union, $.enum),
  53. record: $ => seq('record', $.record_def),
  54. record_def: $ => seq('{', optional(seq($.field, repeat(seq(',', $.field)))), '}'),
  55. field: $ => seq(optional($.docs), $.name, $.type, optional($.field_default)),
  56. field_default: $ => seq('default', '(', $.expr, ')'),
  57. interface: $ => seq('interface', '{', optional(seq($.method, repeat(seq(',', $.method)))), '}'),
  58. method: $ => seq(optional($.docs), $.name, $.type),
  59. union: $ => seq('union', '{', seq($.type, repeat(seq(',', $.type))), '}'),
  60. enum: $ => seq('enum', '{', seq($.enum_item, repeat(seq(',', $.enum_item))), '}'),
  61. enum_item: $ => seq(optional($.docs), $.name),
  62. decl_func: $ => seq(optional($.docs), choice('function', 'operator'), optional('variadic'), $.name, $.sig, $.body),
  63. sig: $ => seq(optional($.type_params), $.inputs, optional($.implicit), $.output),
  64. inputs: $ => $.record_def,
  65. implicit: $ => $.inputs,
  66. output: $ => $.type,
  67. body: $ => choice($.native_body, $.block),
  68. native_body: $ => seq('native', '(', $.text, ')'),
  69. decl_const: $ => seq(optional($.docs), 'const', $.name, $.type, $.body),
  70. decl_method: $ => seq(optional($.docs), 'method', $.receiver, '.', $.name, $.type, $.body),
  71. receiver: $ => $.name,
  72. type: $ => $.ref,
  73. ref: $ => seq($.ref_base, optional($.type_args)),
  74. type_args: $ => seq('[', optional(seq($.type, repeat(seq(',', $.type)))), ']'),
  75. expr: $ => seq(repeat($.cast), $.term, repeat($.pipe)),
  76. cast: $ => seq('(', '[', $.type, ']', ')'),
  77. pipe: $ => choice($.pipe_call, $.pipe_infix, $.pipe_cast, $.pipe_get, $.pipe_interior),
  78. pipe_call: $ => choice($.call_ordered, $.call_unordered),
  79. call_ordered: $ => seq('(', optional(seq($.expr, repeat(seq(',', $.expr)))), ')'),
  80. call_unordered: $ => seq('{', optional(seq($.arg_mapping, repeat(seq(',', $.arg_mapping)))), '}'),
  81. arg_mapping: $ => seq($.name, optional($.arg_mapping_to)),
  82. arg_mapping_to: $ => seq(':', $.expr),
  83. pipe_infix: $ => seq('|', $.ref, $.pipe_call),
  84. pipe_cast: $ => seq('.', $.cast),
  85. pipe_get: $ => seq('.', $.name),
  86. pipe_interior: $ => seq('.', '(', $.ref_base, ')'),
  87. term: $ => choice($.infix_term, $.lambda, $.if, $.when, $.block, $.ref_term, $.int, $.float, $.char, $.bytes, $.string),
  88. infix_term: $ => seq('(', $.infix_left, $.operator, $.infix_right, ')'),
  89. infix_left: $ => $.expr,
  90. operator: $ => $.ref,
  91. infix_right: $ => $.expr,
  92. lambda: $ => seq('{', optional($.pattern), optional($.lambda_self), '=>', $.expr, '}'),
  93. lambda_self: $ => seq('&', $.name),
  94. pattern: $ => choice($.pattern_single, $.pattern_multiple),
  95. pattern_single: $ => $.name,
  96. pattern_multiple: $ => seq('(', seq($.name, repeat(seq(',', $.name))), ')'),
  97. if: $ => seq('if', '(', seq($.cond, repeat(seq(',', $.cond))), ')', $.if_yes, repeat($.elif), 'else', $.if_no),
  98. cond: $ => seq(optional($.cond_pattern), $.expr),
  99. cond_pattern: $ => seq('let', $.pattern, '='),
  100. if_yes: $ => $.block,
  101. if_no: $ => $.block,
  102. elif: $ => seq('if', '(', seq($.cond, repeat(seq(',', $.cond))), ')', $.block),
  103. when: $ => seq('when', '(', $.expr, ')', '{', seq($.case, repeat(seq(',', $.case))), '}'),
  104. case: $ => seq(seq($.name, repeat(seq('|', $.name))), optional($.pattern), '=>', $.expr),
  105. block: $ => seq('{', repeat($.binding), $.expr, '}'),
  106. binding: $ => choice($.binding_plain, $.binding_cps),
  107. binding_plain: $ => seq('let', $.pattern, '=', $.expr, ','),
  108. binding_cps: $ => seq('@', $.ref, optional($.cps_pattern), $.expr, ','),
  109. cps_pattern: $ => seq($.pattern, '='),
  110. ref_term: $ => seq(optional('new'), $.ref),
  111. int: $ => Int,
  112. float: $ => Float,
  113. char: $ => Char,
  114. bytes: $ => repeat1($.byte),
  115. byte: $ => Byte,
  116. string: $ => seq($.text, repeat($.string_part)),
  117. text: $ => Text,
  118. string_part: $ => seq('..', $.string_part_content),
  119. string_part_content: $ => choice($.text, $.char),
  120. })
  121. });