]> git.r.bdr.sh - rbdr/tree-sitter-api-notation/blob - grammar.js
e74c27b6a9cd718f5f73b087acc0ea31fc57669c
[rbdr/tree-sitter-api-notation] / grammar.js
1 module.exports = grammar({
2 name: 'apinotation',
3
4 rules: {
5 source_file: $ => repeat(
6 seq(
7 choice(
8 $.definition,
9 $.comment
10 ),
11 repeat($._newline)
12 )
13 ),
14 definition: $ => prec.right(seq(
15 $.entry,
16 $._newline,
17 repeat(
18 choice(
19 $.member,
20 $.comment
21 )
22 )
23 )),
24 entry: $ => prec.right(seq(
25 $.identifier,
26 repeat(
27 seq(
28 token('.'),
29 $.identifier
30 )
31 )
32 )),
33 member: $ => seq(
34 $.member_type,
35 $._label,
36 optional($.type_annotation),
37 optional($.parameter_list),
38 optional($.return_statement),
39 optional($.throw_statement),
40 $._newline,
41 ),
42 member_type: $ => choice(
43 '::',
44 '->',
45 '<-',
46 '~>',
47 '<~',
48 '+>',
49 '<+',
50 '#',
51 '+',
52 '-'
53 ),
54 parameter_list: $ => seq(
55 '(',
56 optional($._identifier_list),
57 ')'
58 ),
59 return_statement: $ => seq(
60 '=>',
61 $.entry
62 ),
63 throw_statement: $ => seq(
64 '#>',
65 $._entry_list
66 ),
67 _entry_list: $ => prec.left(seq(
68 $.entry,
69 repeat(
70 seq(
71 ',',
72 $.entry
73 )
74 )
75 )),
76 _identifier_list: $ => seq(
77 $.identifier,
78 repeat(
79 seq(
80 ',',
81 $.identifier
82 )
83 )
84 ),
85 identifier: $ => prec.left(seq(
86 $._label,
87 optional($.type_annotation),
88 optional($.parameter_list),
89 optional($.return_statement),
90 optional($.throw_statement),
91 )),
92 type_annotation: $ => seq(
93 '<',
94 $._entry_list,
95 '>'
96 ),
97 comment: $ => seq(
98 '//',
99 /[^\n]*/,
100 $._newline,
101 ),
102 _label: $ => /[a-zA-Z0-9][a-zA-Z0-9_-]*/,
103 _newline: $ => token('\n')
104 }
105 });