-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAST.ts
226 lines (206 loc) · 6.88 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
import ct = require("chevrotain");
/**
* The code in this file follows a general pattern. First, I introduce a string literal
* to use as the "tag" in the tagged unions. I assign that literal to a variable so that
* we don't actually use string literals in the code. Forcing the use of the variable
* allows us to catch potential errors.
*
* In addition, an interface for each node type is then defined. Generally speaking, this
* interface, while exported, isn't actually that useful outside this file. But there are
* some cases where they are used (hence the export). The main reason to include the
* interface definition is just to explicitly state the contents of the node.
*
* Finally, I include a "constructor" function for each interface. Since these are interfaces
* and not classes, we need to define our constructor function separately. Now, a reasonable
* question would be "why not uses classes and a constructor defined in the class?". The
* reason is because once you have a class, you can use `instanceof` on the instances and
* this is dangerous because classes are values and if classes and instances are imported
* from different packages, things will not line up and you'll get very confusing results.
* Using these tagged unions and interfaces avoids this but still gives a type safe way
* to "switch" on different node types.
*/
// Table headers. We keep the Token for reporting errors during the loading phase.
export const tableHeader: "tableHeader" = "tableHeader";
export interface TomlTableHeader {
type: typeof tableHeader;
headers: string[];
token: ct.IToken;
}
export function tomlTableHeader(
headers: string[],
token: ct.IToken
): TomlTableHeader {
return { type: tableHeader, headers: headers, token: token };
}
export const tableArrayEntryHeader: "tableArrayEntryHeader" =
"tableArrayEntryHeader";
export interface TomlTableArrayEntryHeader {
type: typeof tableArrayEntryHeader;
headers: string[];
token: ct.IToken;
}
export function tomlTableArrayEntryHeader(
headers: string[],
token: ct.IToken
): TomlTableArrayEntryHeader {
return { type: tableArrayEntryHeader, headers: headers, token: token };
}
// Bindings
export const keysValue: "keysValue" = "keysValue";
export interface TomlKeysValue {
type: typeof keysValue;
keys: string[];
value: any;
token: ct.IToken;
}
export function tomlKeysValue(
keys: string[],
value: any,
token: ct.IToken
): TomlKeysValue {
return { type: keysValue, keys: keys, value: value, token: token };
}
// Structures
export const inlineTable: "inlineTable" = "inlineTable";
export interface TomlInlineTable {
type: typeof inlineTable;
bindings: TomlKeysValue[];
}
export function tomlInlineTable(bindings: TomlKeysValue[]): TomlInlineTable {
return { type: inlineTable, bindings: bindings };
}
export const arrayType: "tomlArray" = "tomlArray";
export interface TomlArray {
type: typeof arrayType;
contents: TomlValue[];
token: ct.IToken;
}
export function tomlArray(contents: TomlValue[], token: ct.IToken) {
return { type: arrayType, contents: contents, token: token };
}
// Atomic Values
export enum TomlAtomicValueType {
OffsetDateTime,
LocalDateTime,
LocalDate,
LocalTime,
String,
Integer,
Float,
Boolean
}
export interface TomlAtomicGeneric<T> {
image: string;
value: T;
}
// For atomic data, first we list all the node types
export const offsetDateTime: "offsetDateTime" = "offsetDateTime";
export const localDateTime: "localDateTime" = "localDateTime";
export const localDate: "localDate" = "localDate";
export const localTime: "localTime" = "localTime";
// then we list a single parameterized interface for all nodes (since they all have
// the same structure and only the type of the data changes).
export interface TomlAtomicDateTime extends TomlAtomicGeneric<Date> {
type:
| typeof offsetDateTime
| typeof localDateTime
| typeof localDate
| typeof localTime;
}
// Now, the constructor functions.
export function tomlAtomicOffsetDateTime(
image: string,
value: Date
): TomlAtomicDateTime {
return { type: offsetDateTime, image: image, value: value };
}
export function tomlAtomicLocalDateTime(
image: string,
value: Date
): TomlAtomicDateTime {
return { type: localDateTime, image: image, value: value };
}
export function tomlAtomicLocalDate(
image: string,
value: Date
): TomlAtomicDateTime {
return { type: localDate, image: image, value: value };
}
export function tomlAtomicLocalTime(
image: string,
value: Date
): TomlAtomicDateTime {
return { type: localTime, image: image, value: value };
}
export const atomicString: "atomicString" = "atomicString";
export interface TomlAtomicString extends TomlAtomicGeneric<string> {
type: typeof atomicString;
}
export function tomlAtomicString(
image: string,
value: string
): TomlAtomicString {
return { type: atomicString, image: image, value: value };
}
export const atomicInteger: "atomicInteger" = "atomicInteger";
export interface TomlAtomicInteger extends TomlAtomicGeneric<number> {
type: typeof atomicInteger;
}
export function tomlAtomicInteger(
image: string,
value: number
): TomlAtomicInteger {
return { type: atomicInteger, image: image, value: value };
}
export const atomicFloat: "atomicFloat" = "atomicFloat";
export interface TomlAtomicFloat extends TomlAtomicGeneric<number> {
type: typeof atomicFloat;
}
export function tomlAtomicFloat(image: string, value: number): TomlAtomicFloat {
return { type: atomicFloat, image: image, value: value };
}
export const atomicNotANumber: "atomicNotANumber" = "atomicNotANumber";
export interface TomlAtomicNotANumber extends TomlAtomicGeneric<number> {
type: typeof atomicNotANumber;
}
export function tomlAtomicNotANumber(
image: string,
value: number
): TomlAtomicNotANumber {
return { type: atomicNotANumber, image: image, value: value };
}
export const atomicInfinity: "atomicInfinity" = "atomicInfinity";
export interface TomlAtomicInfinity extends TomlAtomicGeneric<number> {
type: typeof atomicInfinity;
}
export function tomlAtomicInfinity(
image: string,
value: number
): TomlAtomicInfinity {
return { type: atomicInfinity, image: image, value: value };
}
export const atomicBoolean: "atomicBoolean" = "atomicBoolean";
export interface TomlAtomicBoolean extends TomlAtomicGeneric<boolean> {
type: typeof atomicBoolean;
}
export function tomlAtomicBoolean(
image: string,
value: boolean
): TomlAtomicBoolean {
return { type: atomicBoolean, image: image, value: value };
}
// Now we define a few convenient union types to represent essentially different
// grammatic productions
export type TomlAtomicValue =
| TomlAtomicDateTime
| TomlAtomicString
| TomlAtomicInteger
| TomlAtomicFloat
| TomlAtomicBoolean
| TomlAtomicInfinity
| TomlAtomicNotANumber;
export type TopLevelTomlDocumentEntry =
| TomlKeysValue
| TomlTableHeader
| TomlTableArrayEntryHeader;
export type TomlValue = TomlAtomicValue | TomlInlineTable | TomlArray;