This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
352 lines (301 loc) · 9.65 KB
/
index.d.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
export = ohm;
declare namespace ohm {
/**
* Instantiate the Grammar defined by source. If specified, namespace is
* the Namespace to use when resolving external references in the grammar.
*/
function grammar(source: string, namespace?: Namespace): Grammar;
/**
* Create a Grammar instance from the contents of a <script> tag.
* node, if specified, is a script tag with the attribute
* type="text/ohm-js". If it is not specified, the result of
* document.querySelector(script[type="text/ohm-js"]) will be used instead.
* If specified, namespace is
* the Namespace to use when resolving external references in the grammar.
*/
function grammarFromScriptElement(node?: Node, namespace?: Namespace): Grammar;
/**
* Create a new Namespace containing Grammar instances for all of the
* grammars defined in source.
* If namespace is specified, it will be the prototype of the new
* Namespace.
*/
function grammars(source: string, namespace?: Namespace): Namespace;
/**
* Create a new Namespace containing Grammar instances for all of the
* grammars defined in the <script> tags in nodeList. If nodeList is
* not specified, the result of
* document.querySelectorAll('script[type="text/ohm-js"]') will be used.
* If namespace is specified, it will be the prototype of the new
* Namespace.
*/
function grammarFromScriptElements(
nodeList?: NodeList,
namespace?: Namespace): Namespace;
/**
* Create a new namespace. If props is specified, all of its properties
* will be copied to the new namespace.
*/
function namespace(props?: Object): Namespace;
/**
* Create a new namespace which inherits from namespace. If props is
* specified, all of its properties will be copied to the new namespace.
*/
function extendNamespace(namespace: Namespace, props?: Object): Namespace;
/**
* A Namespace is a dictionary of Grammars
*/
interface Namespace {
[index: string]: Grammar;
}
/**
* An Ohm Grammar.
*/
interface Grammar {
/**
* Try to match input with this grammar, returning a MatchResult. If
* startRule is given, it specifies the rule on which to start
* matching. By default, the start rule is inherited from the
* supergrammar, or if there is no supergrammar specified, it is the
* first rule in this grammar.
*/
match(input: string, startRule?: string): MatchResult;
/**
* Create a new Matcher object which supports incrementally matching
* this grammar against a changing input string.
*/
matcher(): Matcher;
/**
* Like match() except returns a trace object whose toString() returns
* a summary of each parsing step useful for debugging.
*/
trace(input: string, startRule?: string): Object;
/**
* Create a new Semantics object for this Grammar.
*/
createSemantics(): Semantics;
/**
* Create a new Semantics object for this Grammar that inherits all
* of the operations and attributes in superSemantics.
* This Grammar must be a descendant of the grammar associated with
* superSemantics.
*/
extendSemantics(superSemantics: Semantics): Semantics;
}
/**
* Matcher objects are used to incrementally match a changing input
* against a Grammar, e.g. in an editor or IDE.
*/
interface Matcher {
/**
* Return the current input string.
*/
getInput(): string;
/**
* Set the input string to `str`.
*/
setInput(str: string): void;
/**
* Edit the current input string, replacing the characters between
* `startIdx` and `endIdx` with `str`.
*/
replaceInputRange(startIdx: number, endIdx: number, str: string): Matcher;
/**
* Like Grammar#match, but operates incrementally.
*/
match(optStartRule?: string): MatchResult;
/**
* Like Grammar#trace, but operates incrementally.
*/
trace(optStartRule?: string): Object;
}
/**
* Result of Grammar#match
*/
interface MatchResult {
/**
* True iff match succeeded
*/
succeeded(): boolean;
/**
* True iff match did not succeed
*/
failed(): boolean;
/**
* If match failed contains an error message indicating where and
* why the match failed. This message is suitable for end users of a
* language (i.e., people who do not have access to the grammar source).
*/
message?: string;
/**
* If match failed contains an abbreviated version of this.message that
* does not include an excerpt from the invalid input.
*/
shortMessage?: string;
}
/**
* A Semantics is a family of operations and/or attributes for a given
* grammar. Each operation/attribute has a unique name within the
* Semantics. A grammar may have any number of Semantics instances
* associated with it -- this means that the clients of a grammar
* (even in the same program) never have to worry about
* operation/attribute name clashes.
*/
interface Semantics {
/**
* Returns a dictionary containing operations and attributes defined by
* this Semantics on the result of a matched grammar. Operations are
* no-arg functions and attributes are properties.
*/
(match: MatchResult): Dict;
/**
* Add a new operation named name to this Semantics, using the
* semantic actions contained in actionDict. It is an error if there
* is already an operation or attribute called name in this semantics.
* Returns this Semantics.
*/
addOperation(name: string, actionDict: ActionDict): Semantics;
/**
* Add a new attribute named name to this Semantics, using the
* semantic actions contained in actionDict. It is an error if there
* is already an operation or attribute called name in this semantics.
* Returns this Semantics.
*/
addAttribute(name: string, actionDict: ActionDict): Semantics;
/**
* Extend the operation named name with the semantic actions contained
* in actionDict. name must be the name of an operation in the super
* semantics.
* Returns this Semantics.
*/
extendOperation(name: string, actionDict: ActionDict): Semantics;
/**
* Extend the attribute named name with the semantic actions contained
* in actionDict. name must be the name of an attribute in the super
* semantics.
* Returns this Semantics.
*/
extendAttribute(name: string, actionDict: ActionDict): Semantics;
}
/**
* A dictionary is indexed by strings.
*/
interface Dict {
[index: string]: any;
}
/**
* An ActionDict is a dictionary of Actions indexed by rule names.
*/
interface ActionDict {
[index: string]: Action;
}
/**
* An Action is a function from ParseNodes, called with the children nodes
* of the node it is being executed on.
* The current node is passed as a dynamic this, requiring an ES5
* anonymous function with this typed as any.
*/
type Action = (...args: Node[]) => any;
/**
* A node in the parse tree, passed to Action functions.
*/
interface Node {
/**
* Returns the child at index idx.
*/
child(idx: number): Node;
/**
* true if the node is a terminal node, otherwise false.
*/
isTerminal(): boolean;
/**
* true if the node is an iteration node, which corresponds to a
* +, *, or ? expression in the grammar.
*/
isIteration(): boolean;
/**
* An array containing the node's children.
*/
children: Node[];
/**
* The name of grammar rule that created the node.
*/
ctorName: string;
/**
* Captures the portion of the input that was consumed by the node.
*/
source: Interval;
/**
* Returns the contents of the input stream consumed by this node.
*/
sourceString: string;
/**
* The number of child nodes that the node has.
*/
numChildren: number;
/**
* True if Node is ? option
*/
isOptional: boolean;
/**
* For a terminal node, the raw value that was consumed from the
* input stream.
*/
primitiveValue: string;
/**
* In addition to the properties defined above, within a given
* semantics, every node also has a method/property corresponding to
* each operation/attribute in the semantics.
* For example, in a semantics that has an operation named 'prettyPrint'
* and an attribute named 'freeVars', every node has a prettyPrint()
* method and a freeVars property.
* NOTE this means the above node properties can not be used as
* operation/attribute names.
*/
[index: string]: any;
}
/**
* Interval in input string
*/
interface Interval {
/**
* Input stream of parse
*/
inputStream: any;
/**
* Starting index in input
*/
startIdx: number;
/**
* Ending index in input
*/
endIdx: number;
/**
* Contents of interval
*/
contents: string;
/**
* Returns a new Interval at the start of this one
*/
collapsedLeft(): Interval;
/**
* Returns a new Interval at the end of this one
*/
collapsedRight(): Interval;
/**
* Returns a new Interval which contains the same contents as this one,
* but with whitespace trimmed from both ends.
*/
trimmed(): Interval;
/**
* Returns a new Interval that covers this Interval and all the
* argument Intervals. The new Interval will start at the lowest start
* index and end at the largest end index.
*/
coverageWith(...intervals: Interval[]): Interval;
/**
* Return a nicely-formatted string describing the start of the Interval
*/
getLineAndColumnMessage(): string;
}
}