-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpackage.ts
59 lines (54 loc) · 1.41 KB
/
package.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
import { ParseNode } from "./parsenode.ts";
import { Visitor } from "./visitor.ts";
import { nextTokenIs, Scanner, Token } from "./deps.ts";
import { expectFullIdent } from "./util.ts";
/**
* Represents a Package definition.
*
* The package specifier can be used to prevent name clashes between protocol
* message types.
*
* https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#package
*/
export class Package extends ParseNode {
constructor(
/**
* The given name of a package. This is a "fullIdent" so may contain dots.
*/
public name: string,
/**
* The starting [line, column]
*/
public start: [number, number] = [0, 0],
/**
* The ending [line, column]
*/
public end: [number, number] = [0, 0],
) {
super();
}
toProto() {
return `package ${this.name};`;
}
toJSON() {
return {
type: "Package",
start: this.start,
end: this.end,
name: this.name,
};
}
accept(visitor: Visitor) {
visitor.visit?.(this);
visitor.visitPackage?.(this);
}
static async parse(scanner: Scanner): Promise<Package> {
if (scanner.contents !== "package") {
await nextTokenIs(scanner, Token.keyword, "package");
}
const start = scanner.startPos;
const name = await expectFullIdent(scanner);
await nextTokenIs(scanner, Token.token, ";");
return new Package(name, start, scanner.endPos);
}
}