grammar.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. rules[k] = v
  24. }
  25. rules[IgnoreRuleKey] = IgnoreRule
  26. return rules
  27. })({
  28. // RULES
  29. source_file: $ => seq($.ns, repeat($.alias), repeat($.stmt)),
  30. ns: $ => seq('namespace', optional($.name), '::'),
  31. name: $ => Name,
  32. alias: $ => seq(optional('#'), 'using', optional($.alias_name), $.alias_target),
  33. alias_name: $ => seq($.name, '='),
  34. alias_target: $ => choice($.alias_to_ns, $.alias_to_ref_base),
  35. alias_to_ns: $ => seq('namespace', $.name),
  36. alias_to_ref_base: $ => $.ref_base,
  37. stmt: $ => choice($.decl_entry, $.decl_type, $.decl_func, $.decl_const, $.decl_method),
  38. decl_entry: $ => seq(optional($.docs), optional('#'), 'entry', $.block),
  39. docs: $ => repeat1($.doc),
  40. doc: $ => Doc,
  41. decl_type: $ => seq(optional($.docs), optional('#'), 'type', $.name, optional($.type_params), optional($.impl), $.type_def),
  42. type_params: $ => seq('[', optional(seq($.name, repeat(seq(',', $.name)))), ']'),
  43. impl: $ => seq('(', optional(seq($.ref_base, repeat(seq(',', $.ref_base)))), ')'),
  44. ref_base: $ => seq(optional($.ns_prefix), $.name),
  45. ns_prefix: $ => seq($.name, '::'),
  46. type_def: $ => choice('native', $.record, $.interface, $.union, $.enum),
  47. record: $ => seq('record', $.record_def),
  48. record_def: $ => seq('{', optional(seq($.field, repeat(seq(',', $.field)))), '}'),
  49. field: $ => seq(optional($.docs), $.name, $.type, optional($.field_default)),
  50. field_default: $ => seq('(', $.expr, ')'),
  51. interface: $ => seq('interface', '{', optional(seq($.method, repeat(seq(',', $.method)))), '}'),
  52. method: $ => seq(optional($.docs), $.name, $.type),
  53. union: $ => seq('union', '{', seq($.type, repeat(seq(',', $.type))), '}'),
  54. enum: $ => seq('enum', '{', seq($.enum_item, repeat(seq(',', $.enum_item))), '}'),
  55. enum_item: $ => seq(optional($.docs), $.name),
  56. decl_func: $ => seq(optional($.docs), optional('#'), choice('function', 'operator'), optional('variadic'), $.name, $.sig, $.body),
  57. sig: $ => seq(optional($.type_params), $.inputs, optional($.implicit), $.output),
  58. inputs: $ => $.record_def,
  59. implicit: $ => $.inputs,
  60. output: $ => $.type,
  61. body: $ => choice($.native_body, $.block),
  62. native_body: $ => seq('native', '(', $.text, ')'),
  63. decl_const: $ => seq(optional($.docs), optional('#'), 'const', $.name, $.type, $.body),
  64. decl_method: $ => seq(optional($.docs), optional('#'), 'method', $.receiver, '.', $.name, $.type, $.body),
  65. receiver: $ => $.name,
  66. type: $ => $.ref,
  67. ref: $ => seq($.ref_base, optional($.type_args)),
  68. type_args: $ => seq('[', optional(seq($.type, repeat(seq(',', $.type)))), ']'),
  69. expr: $ => seq(repeat($.cast), $.term, repeat($.pipe)),
  70. cast: $ => seq('(', '[', $.type, ']', ')'),
  71. pipe: $ => choice($.pipe_call, $.pipe_infix, $.pipe_cast, $.pipe_get, $.pipe_interior),
  72. pipe_call: $ => choice($.call_ordered, $.call_unordered),
  73. call_ordered: $ => seq('(', optional(seq($.expr, repeat(seq(',', $.expr)))), ')'),
  74. call_unordered: $ => seq('{', optional(seq($.arg_mapping, repeat(seq(',', $.arg_mapping)))), '}'),
  75. arg_mapping: $ => seq($.name, optional($.arg_mapping_to)),
  76. arg_mapping_to: $ => seq(':', $.expr),
  77. pipe_infix: $ => seq(optional('#'), '|', $.ref, $.pipe_call),
  78. pipe_cast: $ => seq('.', $.cast),
  79. pipe_get: $ => seq('.', $.name),
  80. pipe_interior: $ => seq('.', '(', $.ref_base, ')'),
  81. term: $ => choice($.infix_term, $.lambda, $.if, $.when, $.each, $.block, $.ref_term, $.int, $.float, $.char, $.bytes, $.string),
  82. infix_term: $ => seq('(', $.infix_left, $.operator, $.infix_right, ')'),
  83. infix_left: $ => $.expr,
  84. operator: $ => $.ref,
  85. infix_right: $ => $.expr,
  86. lambda: $ => seq('{', optional($.pattern), optional($.lambda_self), '=>', $.expr, '}'),
  87. lambda_self: $ => seq('&', $.name),
  88. pattern: $ => choice($.pattern_single, $.pattern_multiple),
  89. pattern_single: $ => $.name,
  90. pattern_multiple: $ => seq('(', seq($.name, repeat(seq(',', $.name))), ')'),
  91. if: $ => seq('if', '(', seq($.cond, repeat(seq(',', $.cond))), ')', $.if_yes, repeat($.elif), 'else', $.if_no),
  92. cond: $ => seq(optional($.cond_pattern), $.expr),
  93. cond_pattern: $ => seq('let', $.pattern, '='),
  94. if_yes: $ => $.block,
  95. if_no: $ => $.block,
  96. elif: $ => seq('if', '(', seq($.cond, repeat(seq(',', $.cond))), ')', $.block),
  97. when: $ => seq('when', '(', $.expr, ')', '{', seq($.case, repeat(seq(',', $.case))), '}'),
  98. case: $ => seq(optional('#'), seq($.name, repeat(seq('|', $.name))), optional($.pattern), '=>', $.expr),
  99. each: $ => seq('each', '(', $.type, ')', '{', seq($.case, repeat(seq(',', $.case))), '}'),
  100. block: $ => seq('{', repeat($.binding), $.expr, '}'),
  101. binding: $ => choice($.binding_plain, $.binding_cps),
  102. binding_plain: $ => seq(optional('#'), choice('let', 'const'), $.pattern, '=', $.expr, ','),
  103. binding_cps: $ => seq(optional('#'), '@', $.ref, optional($.cps_pattern), $.expr, ','),
  104. cps_pattern: $ => seq($.pattern, '='),
  105. ref_term: $ => seq(optional($.new), $.ref),
  106. new: $ => seq('new', optional($.new_tag)),
  107. new_tag: $ => seq(':', $.name),
  108. int: $ => Int,
  109. float: $ => Float,
  110. char: $ => Char,
  111. bytes: $ => repeat1($.byte),
  112. byte: $ => Byte,
  113. string: $ => seq($.text, repeat($.string_part)),
  114. text: $ => Text,
  115. string_part: $ => seq('..', $.string_part_content),
  116. string_part_content: $ => choice($.text, $.char),
  117. })
  118. });