-
Notifications
You must be signed in to change notification settings - Fork 3
/
option.ts
102 lines (97 loc) · 3 KB
/
option.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
import { ParseNode } from "./parsenode.ts";
import { Visitor } from "./visitor.ts";
import { Constant } from "./constant.ts";
import { nextTokenIs, Scanner, Token, TokenError } from "./deps.ts";
import { expectFullIdent } from "./util.ts";
/**
* Represents an Option definition, that may be globally scoped, scoped to an
* Enum, Message, Service, RPC. Fields use a separate `FieldOption` node.
*
* Options can be used in proto files, messages, enums and services. An option
* can be a protobuf defined option or a custom option. For more information,
* see Options in the language guide.
*
* https://developers.google.com/protocol-buffers/docs/reference/proto2-spec#option
*/
export class Option extends ParseNode {
constructor(
/**
* The key of the option.
*/
public key: string[],
/**
* If the key is an extension (wrapped in parens)
*/
public isExtension: boolean,
/**
* The value of the option - as a Constant node.
*/
public value: Constant,
/**
* The starting [line, column]
*/
public start: [number, number] = [0, 0],
/**
* The ending [line, column]
*/
public end: [number, number] = [0, 0],
) {
super();
}
toProto() {
let key = this.key.join(".");
if (this.isExtension) {
key = `(${this.key[0]})`;
if (this.key.length > 1) key += `.${this.key.slice(1).join(".")}`;
} else {
key = this.key.join(".");
}
return `option ${key} = ${this.value.toProto()};`;
}
toJSON() {
return {
type: "Option",
start: this.start,
isExtension: this.isExtension,
end: this.end,
key: this.key,
value: this.value.toJSON(),
};
}
accept(visitor: Visitor) {
visitor.visit?.(this);
visitor.visitOption?.(this);
this.value.accept(visitor);
}
static async parse(scanner: Scanner): Promise<Option> {
if (scanner.contents !== "option") {
await nextTokenIs(scanner, Token.keyword, "option");
}
const start = scanner.startPos;
const token = await scanner.scan();
let contents = scanner.contents;
const key = [];
let isExtension = false;
if (token === Token.token && contents === "(") {
isExtension = true;
key.push(await expectFullIdent(scanner));
await nextTokenIs(scanner, Token.token, ")");
await nextTokenIs(scanner, Token.token);
contents = scanner.contents;
if (contents === ".") {
key.push(await expectFullIdent(scanner));
await nextTokenIs(scanner, Token.token, "=");
} else if (contents !== "=") {
throw new TokenError(scanner, token, Token.token);
}
} else if (token === Token.identifier) {
key.push(await expectFullIdent(scanner, false));
await nextTokenIs(scanner, Token.token, "=");
} else {
throw new TokenError(scanner, token, Token.identifier);
}
const value = await Constant.parse(scanner);
await nextTokenIs(scanner, Token.token, ";");
return new Option(key, isExtension, value, start, scanner.endPos);
}
}