-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconstant.ts
102 lines (97 loc) · 2.89 KB
/
constant.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 { Scanner, Token, TokenError } from "./deps.ts";
import { expectFullIdent } from "./util.ts";
/**
* Represents a Constant, aka a Literal value.
*
* https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#constant
*/
export class Constant extends ParseNode {
/**
* The value of the Constant, converted to a first class JS type.
*/
public value: boolean | string | number | null;
constructor(
/**
* What ProtoBuf type the value of the Constant is.
*/
public literalType: "identifier" | "string" | "int" | "float" | "boolean",
/**
* A raw string containing the contents of the Constant value, including delimiters.
*/
private raw: string,
/**
* The starting [line, column]
*/
public start: [number, number] = [0, 0],
/**
* The ending [line, column]
*/
public end: [number, number] = [0, 0],
) {
super();
this.value = this.raw;
if (this.literalType === "int" || this.literalType === "float") {
this.value = Number(this.value);
if (!Number.isFinite(this.value) || Number.isNaN(this.value)) {
this.value = null;
}
} else if (this.literalType === "string") {
this.value = this.value.slice(1, -1);
} else if (this.literalType === "boolean") {
this.value = this.value === "true";
}
}
toProto() {
return this.raw;
}
toJSON() {
return {
type: "Constant",
start: this.start,
end: this.end,
literalType: this.literalType,
raw: this.raw,
value: this.value,
};
}
accept(visitor: Visitor) {
visitor.visit?.(this);
visitor.visitConstant?.(this);
}
static async parse(scanner: Scanner): Promise<Constant> {
const token = await scanner.scan();
const start = scanner.startPos;
let value = scanner.contents;
let type: "identifier" | "int" | "float" | "string" | "boolean" = "string";
if (token === Token.string) {
type = "string";
} else if (
token === Token.identifier && value === "false" || value === "true"
) {
type = "boolean";
} else if (token === Token.identifier) {
type = "identifier";
await expectFullIdent(scanner, false);
} else if (token === Token.int) {
type = "int";
} else if (token === Token.float) {
type = "float";
} else if (token === Token.token && (value === "-" || value === "+")) {
const sign = value;
const token = await scanner.scan();
if (token === Token.keyword && scanner.contents === "inf") {
type = "int";
value = `${sign}${scanner.contents}`;
}
} else if (
token === Token.keyword && (value === "inf" || value === "nan")
) {
type = "int";
} else {
throw new TokenError(scanner, token);
}
return new Constant(type, value, start, scanner.endPos);
}
}