]> git.r.bdr.sh - rbdr/tree-sitter-api-notation/blob - grammar.js
db1219dab138467c5861408015009e6a81f216e9
[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 choice(
87 $.optional_value,
88 $._label,
89 ),
90 optional($.type_annotation),
91 optional($.parameter_list),
92 optional($.return_statement),
93 optional($.throw_statement),
94 )),
95 optional_value: $ => seq(
96 '[',
97 $._label,
98 ']'
99 ),
100 type_annotation: $ => seq(
101 '<',
102 $._entry_list,
103 '>'
104 ),
105 comment: $ => seq(
106 '//',
107 /[^\n]*/,
108 $._newline,
109 ),
110 _label: $ => /[a-zA-Z0-9][a-zA-Z0-9_-]*/,
111 _newline: $ => token('\n')
112 }
113 });