forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.ts
304 lines (263 loc) · 6.79 KB
/
ast.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (c) Microsoft Corporation.
import { Namespace } from "@typespec/compiler";
/**
* This module describes an AST for Protobuf.
*/
/**
* A single .proto file.
*/
export interface ProtoFile {
/**
* The package name, if one is known.
*/
package?: string;
/**
* The `option` specifiers to include in the file.
*/
options: Partial<WellKnownFileOptions>;
/**
* Paths imported by this file.
*/
imports: string[];
/**
* The declarations in the file.
*
* Only `service` and `message` declarations may exist at the root of the file.
*/
declarations: Iterable<ProtoTopLevelDeclaration>;
/**
* The original namespace node from which this ProtoFile originated.
*/
source: Namespace;
/**
* The package-level documentation comment, if any.
*/
doc?: string | undefined;
}
/**
* The built-in options that are defined by Protobuf.
*/
export interface WellKnownFileOptions {
java_package: string;
java_outer_classname: string;
optimize_for: "SPEED" | "CODE_SIZE" | "LITE_RUNTIME";
cc_enable_arenas: boolean;
}
/**
* A top-level declaration.
*/
export type ProtoTopLevelDeclaration =
| ProtoServiceDeclaration
| ProtoMessageDeclaration
| ProtoEnumDeclaration;
/**
* A declaration. One of `service`, `message`, a field within a message, `one_of`, `enum`, or an `rpc` method.
*/
export type ProtoDeclaration =
| ProtoServiceDeclaration
| ProtoMessageDeclaration
| ProtoFieldDeclaration
| ProtoOneOfDeclaration
| ProtoEnumDeclaration
| ProtoMethodDeclaration
| ProtoEnumVariantDeclaration;
/**
* A Protobuf scalar type.
*/
export type ScalarName = ScalarIntegralName | "double" | "float" | "bytes" | "string";
/**
* A Protobuf integral type.
*/
export type ScalarIntegralName = ScalarIntegerName | ScalarFixedName | "bool";
/**
* A Protobuf variable-length integer type.
*/
export type ScalarIntegerName = `${"u" | "s" | ""}int${"32" | "64"}`;
/**
* A Protobuf fixed-length integer type.
*/
export type ScalarFixedName = `${"s" | ""}fixed${"32" | "64"}`;
// Symbols for type destructuring
const $scalar = Symbol("$scalar");
const $ref = Symbol("$ref");
const $map = Symbol("$map");
/**
* A map type. Map keys can be any integral or string type (any scalar except float, double, and bytes).
*
* The value may be any type other than another map.
*/
export type ProtoMap = [typeof $map, ScalarIntegralName | "string", ProtoRef | ProtoScalar];
/**
* A reference to a named message type.
*/
export type ProtoRef = [typeof $ref, string];
/**
* A scalar type.
*/
export type ProtoScalar = [typeof $scalar, ScalarName];
/**
* A Protobuf type.
*/
export type ProtoType = ProtoScalar | ProtoRef | ProtoMap;
/**
* Create a scalar type by name.
*/
export function scalar(t: ScalarName): ProtoScalar {
return [$scalar, t];
}
/**
* Create a type reference (symbol) to a named message.
*/
export function ref(t: string): ProtoRef {
return [$ref, t];
}
/**
* Create a map from a key type to a value type.
*/
export function map(k: ScalarIntegralName | "string", v: Exclude<ProtoType, ProtoMap>): ProtoMap {
return [$map, k, v];
}
/* c8 ignore start */
// Unreachable, by definition, should not be covered :)
/**
* Creates a type that will throw an internal error if the system attempts to emit it.
*
* @param message - optional message that should be printed
*/
export function unreachable(message: string = "tried to emit unreachable type"): never {
// This little "array-like" object will throw an internal error as soon as the "tag" is inspected.
return Object.freeze({
get [0]() {
throw new Error("Internal Error: " + message);
},
}) as never;
}
/* c8 ignore stop */
/**
* A "pattern" object with variants for each Protobuf type.
*/
export interface ProtoTypeMatchPattern<T> {
scalar: (s: ScalarName) => T;
ref: (r: string) => T;
map: (k: ScalarIntegralName | "string", v: Exclude<ProtoType, ProtoMap>) => T;
}
/**
* A helper function that matches and delegates a Protobuf type to a handler per type.
*
* @param type - the Protobuf type to match and delegate
* @param pattern - the matching pattern of delegates to apply
* @returns
*/
export function matchType<Result>(type: ProtoType, pattern: ProtoTypeMatchPattern<Result>): Result {
switch (type[0]) {
case $ref:
return pattern.ref(type[1]);
case $scalar:
return pattern.scalar(type[1]);
case $map:
return pattern.map(type[1], type[2] as Exclude<ProtoType, ProtoMap>);
/* c8 ignore next 5 */
default:
const __exhaust: never = type[0];
throw new Error(`Internal Error: unreachable matchType variant ${__exhaust}`);
}
}
/**
* Elements common to all protobuf declarations.
*/
export interface ProtoDeclarationCommon {
/**
* Documentation comment text, if any.
*/
doc?: string | undefined;
}
/**
* A `service` declaration.
*/
export interface ProtoServiceDeclaration extends ProtoDeclarationCommon {
kind: "service";
name: string;
operations: ProtoMethodDeclaration[];
}
/**
* An operation's streaming mode.
*/
export const enum StreamingMode {
Duplex = 3,
In = 2,
Out = 1,
None = 0,
}
/**
* An `rfc` method declaration.
*/
export interface ProtoMethodDeclaration extends ProtoDeclarationCommon {
kind: "method";
stream: StreamingMode;
name: string;
input: ProtoRef;
returns: ProtoRef;
}
/**
* A declaration that can fit within the body of a message declaration.
*/
export type ProtoMessageBodyDeclaration =
| ProtoFieldDeclaration
| ProtoMessageDeclaration
| ProtoOneOfDeclaration
| ProtoEnumDeclaration;
/**
* A `message` declaration.
*/
export interface ProtoMessageDeclaration extends ProtoDeclarationCommon {
kind: "message";
name: string;
declarations: Array<ProtoMessageBodyDeclaration>;
reservations?: Array<string | number | [number, number]>;
}
/**
* A field declaration within a message.
*/
export interface ProtoFieldDeclaration extends ProtoDeclarationCommon {
kind: "field";
name: string;
/**
* Whether or not the field is repeated (i.e. an array).
*/
repeated?: boolean;
options?: Partial<DefaultFieldOptions>;
type: ProtoType;
index: number;
}
/**
* The options for fields defined by the Protobuf specification.
*/
export interface DefaultFieldOptions {
packed: true;
deprecated: true;
}
/**
* A `one_of` declaration.
*/
export interface ProtoOneOfDeclaration extends ProtoDeclarationCommon {
kind: "oneof";
name: string;
declarations: ProtoFieldDeclaration[];
}
/**
* An `enum` declaration.
*/
export interface ProtoEnumDeclaration extends ProtoDeclarationCommon {
kind: "enum";
name: string;
allowAlias?: boolean;
variants: ProtoEnumVariantDeclaration[];
}
/**
* A variant within an `enum` declaration.
*/
export interface ProtoEnumVariantDeclaration extends ProtoDeclarationCommon {
kind: "variant";
name: string;
value: number;
}