-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgrammar-declarations.js
109 lines (101 loc) · 2.89 KB
/
grammar-declarations.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
const { commaSep, commaSep1 } = require('./utils');
module.exports = {
// Declarations
declaration: ($) =>
choice(
$.interface_declaration,
$.class_declaration,
$.typedef_declaration,
$.function_declaration,
$.variable_declaration,
),
_access_identifier: ($) => choice('default', 'null', 'get', 'set', 'dynamic', 'never'),
access_identifiers: ($) =>
seq('(', $._access_identifier, optional(seq(',', $._access_identifier)), ')'),
type_params: ($) => prec.right(1, seq('<', commaSep1($.type), '>')),
_modifier: ($) =>
choice(
'macro',
'abstract',
'static',
'public',
'private',
'extern',
'inline',
'overload',
'override',
'final',
),
class_declaration: ($) =>
seq(
repeat($.metadata),
repeat($._modifier),
'class',
field('name', $._lhs_expression),
optional($.type_params),
optional(
seq('extends', field('super_class_name', $._type_path), optional($.type_params)),
),
optional(
repeat(
seq('implements', field('interface_name', $._type_path), optional($.type_params)),
),
),
field('body', $.block),
),
interface_declaration: ($) =>
seq(
repeat($._modifier),
'interface',
field('name', $._lhs_expression),
optional($.type_params),
optional(
repeat(seq('extends', field('interface_name', $._type_path), optional($.type_params))),
),
field('body', $.block),
),
typedef_declaration: ($) =>
seq(
repeat($.metadata),
repeat($._modifier),
'typedef',
field('name', $._lhs_expression),
optional($.type_params),
seq('=', choice($.block, $._lhs_expression, $.type)),
$._lookback_semicolon,
),
function_declaration: ($) =>
seq(
repeat($.metadata),
repeat($._modifier),
'function',
field('name', choice($._lhs_expression, 'new')),
optional($.type_params),
$._function_arg_list,
optional(seq(':', field('return_type', $.type))),
optional(field('body', $.block)),
$._lookback_semicolon,
),
_function_arg_list: ($) => prec(1, seq('(', commaSep($.function_arg), ')')),
function_arg: ($) =>
prec(
1,
seq(
field('name', $._lhs_expression),
optional('?'),
optional(seq(':', alias(choice($._lhs_expression, $.type, $.structure_type), $.type))),
optional(seq($._assignmentOperator, $._literal)),
),
),
variable_declaration: ($) =>
seq(
repeat($.metadata),
repeat($._modifier),
choice('var', 'final'),
field('name', $._lhs_expression),
optional($.access_identifiers),
optional(seq(':', optional(repeat('(')), field('type', $.type), optional(repeat(')')))),
optional(seq(($._assignmentOperator, $.operator), $.expression)),
$._lookback_semicolon,
),
};