-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgrammar.js
148 lines (125 loc) · 2.8 KB
/
grammar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* @file JSDoc grammar for tree-sitter
* @author Max Brunsfeld <[email protected]>
* @author Amaan Qureshi <[email protected]>
* @license MIT
*/
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
module.exports = grammar({
name: 'jsdoc',
externals: $ => [
$.type,
],
extras: _ => [
token(choice(
// Skip over stars at the beginnings of lines
seq(/\n/, /[ \t]*/, repeat(seq('*', /[ \t]*/))),
/\s/,
)),
],
rules: {
document: $ => seq(
$._begin,
optional($.description),
repeat($.tag),
$._end,
),
description: $ => seq(
$._text,
repeat(choice($._text, $.inline_tag, $._inline_tag_false_positive)),
),
tag: $ => choice(
// type, name, and description
seq(
alias($.tag_name_with_argument, $.tag_name),
optional(seq('{', $.type, '}')),
optional($._expression),
optional($.description),
),
// type and description
seq(
alias($.tag_name_with_type, $.tag_name),
optional(seq('{', $.type, '}')),
optional($.description),
),
// description only
seq(
$.tag_name,
optional($.description),
),
),
inline_tag: $ => seq(
'{',
$.tag_name,
$.description,
'}',
),
_inline_tag_false_positive: _ => token(prec.left(1, /\{[^@}]+\}?/)),
tag_name_with_argument: _ => token(choice(
'@access',
'@alias',
'@api',
'@augments',
'@borrows',
'@callback',
'@constructor',
'@event',
'@exports',
'@external',
'@extends',
'@fires',
'@function',
'@mixes',
'@name',
'@namespace',
'@param',
'@property',
'@prop',
'@satisfies',
'@typedef',
)),
tag_name_with_type: _ => token(choice(
'@return',
'@returns',
'@throw',
'@throws',
'@type',
)),
tag_name: _ => /@[a-zA-Z_]+/,
_expression: $ => choice(
$.identifier,
$.optional_identifier,
$.member_expression,
$.path_expression,
$.qualified_expression,
),
qualified_expression: $ => prec(1, seq(
$.identifier,
':',
$._expression,
)),
path_expression: $ => prec(2, seq(
$.identifier,
token.immediate('/'),
$.identifier,
)),
member_expression: $ => seq(
$._expression,
choice(
'.',
'#',
'~',
),
choice(
$.identifier,
$.qualified_expression,
),
),
optional_identifier: $ => seq('[', $.identifier, ']'),
identifier: _ => /[a-zA-Z_$][a-zA-Z_$0-9]*/,
_text: _ => token(prec(-1, /[^*{}@\s][^*{}\n]*([^*/{}\n][^*{}\n]*\*+)*/)),
_begin: _ => token(seq('/', repeat('*'))),
_end: _ => '/',
},
});