@sdeverywhere/parse 0.1.5 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts DELETED
@@ -1,607 +0,0 @@
1
- import { XmlElement } from '@rgrove/parse-xml';
2
-
3
- /**
4
- * Format a model variable or subscript/dimension name into a valid C identifier (with
5
- * special characters converted to underscore).
6
- *
7
- * Note that this should only be called with an individual variable base name (e.g.,
8
- * 'Variable name') or a subscript/dimension name (e.g., 'DimA'). In the case where
9
- * you have a full variable name that includes subscripts/dimensions (e.g.,
10
- * 'Variable name[DimA,B2]'), use `canonicalVarId` to convert the base variable name
11
- * and subscript/dimension parts to canonical form indepdendently.
12
- *
13
- * @param {string} name The name of the variable in the source model, e.g., "Variable name".
14
- * @returns {string} The C identifier for the given name, e.g., "_variable_name".
15
- */
16
- declare function canonicalId(name: string): string;
17
- /**
18
- * Format a (subscripted or non-subscripted) model variable name into a canonical identifier,
19
- * (with special characters converted to underscore, and subscript/dimension parts separated
20
- * by commas).
21
- *
22
- * @param {string} name The name of the variable in the source model, e.g., "Variable name[DimA, B2]".
23
- * @returns {string} The canonical identifier for the given name, e.g., "_variable_name[_dima,_b2]".
24
- */
25
- declare function canonicalVarId(name: string): string;
26
- /**
27
- * Format a model function name into a valid C identifier (with special characters
28
- * converted to underscore, and the ID converted to uppercase).
29
- *
30
- * @param {string} name The name of the variable in the source model, e.g., "FUNCTION name".
31
- * @returns {string} The C identifier for the given name, e.g., "_FUNCTION_NAME".
32
- */
33
- declare function canonicalFunctionId(name: string): string;
34
-
35
- /** The simulation parameters, such as start time, end time, and time step. */
36
- interface SimulationSpec {
37
- /** The start time of the simulation. */
38
- startTime: number;
39
- /** The end time of the simulation. */
40
- endTime: number;
41
- /** The time step of the simulation. */
42
- timeStep: number;
43
- }
44
- /** The original name of a dimension, as it appears in the model. */
45
- type DimName = string;
46
- /** The canonical identifier of a dimension, as it appears in generated code. */
47
- type DimId = string;
48
- /** The original name of a subscript/index, as it appears in the model. */
49
- type SubName = string;
50
- /** The canonical identifier of a subscript/index, as it appears in generated code. */
51
- type SubId = string;
52
- /**
53
- * The original name of a dimension or subscript/index, as it appears in the model.
54
- *
55
- * This type is used in cases where either a dimension or an individual subscript/index
56
- * can appear, and more analysis is needed to resolve the reference.
57
- */
58
- type DimOrSubName = string;
59
- /**
60
- * The canonical identifier of a dimension or subscript/index, as it appears in
61
- * generated code.
62
- *
63
- * This type is used in cases where either a dimension or an individual subscript/index
64
- * can appear, and more analysis is needed to resolve the reference.
65
- */
66
- type DimOrSubId = string;
67
- /**
68
- * A reference to a dimension or an individual subscript/index, as used in a dimension
69
- * definition or in a variable reference inside an equation definition.
70
- *
71
- * This type can be used in cases where either a dimension or an individual subscript/index
72
- * can appear, and more analysis is needed to resolve the reference.
73
- */
74
- interface SubscriptRef {
75
- /**
76
- * The original name of the dimension or subscript/index, as it appears in the model.
77
- */
78
- subName: DimOrSubName;
79
- /**
80
- * The canonical identifier of the dimension or subscript/index, as it appears in
81
- * generated code.
82
- */
83
- subId: DimOrSubId;
84
- }
85
- /**
86
- * A subscript mapping, as used in a dimension definition.
87
- */
88
- interface SubscriptMapping {
89
- /**
90
- * The original name of the "target" dimension for the mapping, as it appears in
91
- * the model.
92
- */
93
- toDimName: DimName;
94
- /**
95
- * The canonical identifier of the "target" dimension for the mapping, as it appears
96
- * in generated code.
97
- */
98
- toDimId: DimId;
99
- /**
100
- * The mapped subscripts.
101
- */
102
- subscriptRefs: SubscriptRef[];
103
- }
104
- /**
105
- * A definition of a dimension (aka "subscript range" in Vensim).
106
- */
107
- interface DimensionDef {
108
- /**
109
- * The original name of the dimension being defined, as it appears in the model.
110
- */
111
- dimName: DimName;
112
- /**
113
- * The canonical identifier of the dimension being defined, as it appears in generated code.
114
- */
115
- dimId: DimId;
116
- /**
117
- * The original name of the family associated with the dimension being defined, as it
118
- * appears in the model.
119
- *
120
- * For a typical dimension, the family name is the same as the dimension name, but in the
121
- * case of an alias (e.g., in Vensim, `DimA <-> DimB`), this will be the name used on the
122
- * right side (e.g., `DimB`).
123
- */
124
- familyName: DimName;
125
- /**
126
- * The canonical identifier of the family associated with the dimension being defined, as
127
- * it appears in generated code.
128
- *
129
- * For a typical dimension, the family name is the same as the dimension name, but in the
130
- * case of an alias (e.g., in Vensim, `DimA <-> DimB`), this will be the ID used on the
131
- * right side (e.g., `_dimb`).
132
- */
133
- familyId: DimId;
134
- /**
135
- * The array of subscripts/indices that make up this dimension.
136
- */
137
- subscriptRefs: SubscriptRef[];
138
- /**
139
- * The array of subscript mappings, if defined for this dimension.
140
- */
141
- subscriptMappings: SubscriptMapping[];
142
- /**
143
- * The optional comment text that accompanies the dimension definition in the model.
144
- */
145
- comment?: string;
146
- /**
147
- * The optional group name, if this dimension definition is contained within a group.
148
- */
149
- group?: string;
150
- }
151
- /** A number literal that appears in an expression. */
152
- interface NumberLiteral {
153
- kind: 'number';
154
- /** The numeric value. */
155
- value: number;
156
- /** The original string representation from the model. */
157
- text: string;
158
- }
159
- /** A string literal that appears in an expression. */
160
- interface StringLiteral {
161
- kind: 'string';
162
- /** The string value without quotes. */
163
- text: string;
164
- }
165
- /** A keyword (e.g., ":NA:") that appears in an expression. */
166
- interface Keyword {
167
- kind: 'keyword';
168
- /** The original string representation from the model. */
169
- text: string;
170
- }
171
- /** The original name of a variable, as it appears in the model. */
172
- type VariableName = string;
173
- /** The canonical identifier of a variable, as it appears in generated code. */
174
- type VariableId = string;
175
- /**
176
- * A reference to a variable that appears in an expression.
177
- */
178
- interface VariableRef {
179
- kind: 'variable-ref';
180
- /**
181
- * The original name of the variable, as it appears in the model.
182
- */
183
- varName: VariableName;
184
- /**
185
- * The canonical identifier of the variable, as it appears in generated code.
186
- */
187
- varId: VariableId;
188
- /**
189
- * The optional array of subscript/dimension references, if the referenced variable
190
- * is subscripted.
191
- */
192
- subscriptRefs?: SubscriptRef[];
193
- }
194
- /** An operator used in a unary expression. */
195
- type UnaryOp = '+' | '-' | ':NOT:';
196
- /** A unary expression. */
197
- interface UnaryOpExpr {
198
- kind: 'unary-op';
199
- /** The operator. */
200
- op: UnaryOp;
201
- /** The child expression that the operator applies to. */
202
- expr: Expr;
203
- }
204
- /** An operator used in a binary expression. */
205
- type BinaryOp = '+' | '-' | '*' | '/' | '^' | '=' | '<>' | '<' | '>' | '<=' | '>=' | ':AND:' | ':OR:';
206
- /** A binary expression. */
207
- interface BinaryOpExpr {
208
- kind: 'binary-op';
209
- /** The left-hand side child expression. */
210
- lhs: Expr;
211
- /** The operator. */
212
- op: BinaryOp;
213
- /** The right-hand side child expression. */
214
- rhs: Expr;
215
- }
216
- /** An expression that was contained within parentheses in the original model. */
217
- interface ParensExpr {
218
- kind: 'parens';
219
- /** The child expression that was defined within parentheses. */
220
- expr: Expr;
221
- }
222
- /** A single (x,y) point in a lookup definition. */
223
- type LookupPoint = [number, number];
224
- /** The range for a lookup definition. */
225
- interface LookupRange {
226
- min: LookupPoint;
227
- max: LookupPoint;
228
- }
229
- /** A lookup definition. */
230
- interface LookupDef {
231
- kind: 'lookup-def';
232
- /** The optional range that declares the minimum and maximum points for this lookup. */
233
- range?: LookupRange;
234
- /** The array of points that define this lookup. */
235
- points: LookupPoint[];
236
- }
237
- /**
238
- * A lookup call, as used in an expression. This is similar to a `FunctionCall`, except
239
- * that instead of a function name, there is a reference to a lookup variable, and the
240
- * single argument determines the x coordinate for the lookup.
241
- */
242
- interface LookupCall {
243
- kind: 'lookup-call';
244
- /** The reference to a lookup variable. */
245
- varRef: VariableRef;
246
- /** The single argument that determines the x coordinate for the lookup. */
247
- arg: Expr;
248
- }
249
- /** The original name of a function, as it appears in the model. */
250
- type FunctionName = string;
251
- /** The canonical identifier of a function, as it appears in generated code. */
252
- type FunctionId = string;
253
- /** A function call, as used in an expression. */
254
- interface FunctionCall {
255
- kind: 'function-call';
256
- /**
257
- * The original name of the function, as it appears in the model.
258
- */
259
- fnName: FunctionName;
260
- /**
261
- * The canonical identifier of the function, as it appears in generated code.
262
- */
263
- fnId: FunctionId;
264
- /**
265
- * The array of argument expressions that are passed to the function.
266
- */
267
- args: Expr[];
268
- }
269
- /**
270
- * A union type that includes all possible expression types. Each expression type includes
271
- * a unique `kind` property that can be used to identify the type of the expression.
272
- */
273
- type Expr = NumberLiteral | StringLiteral | Keyword | VariableRef | UnaryOpExpr | BinaryOpExpr | ParensExpr | LookupDef | LookupCall | FunctionCall;
274
- /**
275
- * A variable definition that appears on the LHS of an equation definition. Note that
276
- * this is mostly the same as `VariableRef` that is used in an expression; the difference
277
- * is that a `VariableDef` may contain an "except" clause whereas a `VariableRef` will not.
278
- */
279
- interface VariableDef {
280
- kind: 'variable-def';
281
- /**
282
- * The original name of the variable, as it appears in the model.
283
- */
284
- varName: VariableName;
285
- /**
286
- * The canonical identifier of the variable, as it appears in generated code.
287
- */
288
- varId: VariableId;
289
- /**
290
- * The optional array of subscript/dimension references, if the variable is subscripted.
291
- */
292
- subscriptRefs?: SubscriptRef[];
293
- /**
294
- * The optional array of "exceptions". For example, in Vensim it is possible to express
295
- * that an equation to applies to all subscripts in a dimension except for one (or a subset),
296
- * e.g., `x[DimA] :EXCEPT: [A1] = 5`.
297
- */
298
- exceptSubscriptRefSets?: SubscriptRef[][];
299
- }
300
- /**
301
- * The left-hand side of an equation definition.
302
- */
303
- interface EquationLhs {
304
- /** The variable definition that appears on the LHS. */
305
- varDef: VariableDef;
306
- }
307
- /** The right-hand side of a typical equation that is defined with an expression. */
308
- interface EquationRhsExpr {
309
- kind: 'expr';
310
- /** The expression that appears on the right-hand side of the equation. */
311
- expr: Expr;
312
- }
313
- /** The right-hand side of a constant list definition. */
314
- interface EquationRhsConstList {
315
- kind: 'const-list';
316
- /** The array of constant values. */
317
- constants: NumberLiteral[];
318
- /**
319
- * @hidden The original string representation from the model. This is only needed for
320
- * compatibility with the legacy parser, which includes the original string representation
321
- * including semicolons, which is used for the `modelFormula`. Ideally we could remove this
322
- * field if we fix antlr4-vensim to preserve the groupings as described above.
323
- */
324
- text: string;
325
- }
326
- /** The right-hand side of a "lookup variable" definition. */
327
- interface EquationRhsLookup {
328
- kind: 'lookup';
329
- /** The lookup definition that appears on the right-hand side of the variable definition. */
330
- lookupDef: LookupDef;
331
- }
332
- /**
333
- * The right-hand side of a "data variable" definition. In Vensim, a data variable does
334
- * not contain any information in the model file, and the data is read from an external
335
- * file, so this type is merely used to indicate the equation kind (to differentiate it
336
- * from other kinds of equations).
337
- */
338
- interface EquationRhsData {
339
- kind: 'data';
340
- }
341
- /**
342
- * A union type that includes all possible equation right-hand side types. Each type includes
343
- * a unique `kind` property that can be used to identify the type of the equation.
344
- */
345
- type EquationRhs = EquationRhsExpr | EquationRhsConstList | EquationRhsLookup | EquationRhsData;
346
- /** An equation definition. */
347
- interface Equation {
348
- /** The left-hand side of the equation. */
349
- lhs: EquationLhs;
350
- /** The right-hand side of the equation. */
351
- rhs: EquationRhs;
352
- /**
353
- * The optional units text that accompanies the equation definition in the model.
354
- */
355
- units?: string;
356
- /**
357
- * The optional comment text that accompanies the equation definition in the model.
358
- */
359
- comment?: string;
360
- /**
361
- * The optional group name, if this equation definition is contained within a group.
362
- */
363
- group?: string;
364
- }
365
- /** A complete model definition, including all defined dimensions and equations. */
366
- interface Model {
367
- /**
368
- * The simulation parameters, such as start time, end time, and time step.
369
- *
370
- * NOTE: This will be defined for XMILE models, but may be undefined for Vensim models
371
- * for which the parameters are not compile-time constants.
372
- */
373
- simulationSpec?: SimulationSpec;
374
- /** The array of all dimension definitions in the model. */
375
- dimensions: DimensionDef[];
376
- /** The array of all variable/equation definitions in the model. */
377
- equations: Equation[];
378
- }
379
-
380
- /**
381
- * @hidden This is not yet part of the public API.
382
- */
383
- declare function debugPrintExpr(expr: Expr, indent?: number): void;
384
- /**
385
- * @hidden This is not yet part of the public API.
386
- */
387
- type FormatVariableRefFunc = (varRef: VariableRef) => string;
388
- /**
389
- * @hidden This is not yet part of the public API.
390
- */
391
- interface PrettyOpts {
392
- /**
393
- * Whether to use a compact representation without additional spaces. (The compact form mimics
394
- * the behavior of the legacy parser.
395
- */
396
- compact?: boolean;
397
- html?: boolean;
398
- formatVariableRef?: FormatVariableRefFunc;
399
- }
400
- /**
401
- * @hidden This is not yet part of the public API.
402
- */
403
- declare function toPrettyString(expr: Expr, opts?: PrettyOpts): string;
404
- /**
405
- * @hidden This is not yet part of the public API.
406
- */
407
- declare function prettyPrintExpr(expr: Expr, indent?: number): void;
408
- /**
409
- * @hidden This is not yet part of the public API.
410
- */
411
- declare function printExprStats(exprs: Expr[]): void;
412
-
413
- /**
414
- * @hidden This is not yet part of the public API.
415
- */
416
- interface ReduceExprOptions {
417
- /** A callback that returns the possibly reduced expression for the referenced variable. */
418
- resolveVarRef?: (varRef: VariableRef) => Expr | undefined;
419
- }
420
- /**
421
- * @hidden This is not yet part of the public API.
422
- */
423
- declare function reduceExpr(expr: Expr, opts?: ReduceExprOptions): Expr;
424
- /**
425
- * A variant of `reduceExpr` that does not aggressively reduce the expression, but only
426
- * tries to eliminate the unused branch if a conditional (`IF THEN ELSE`) has a condition
427
- * that resolves to a constant.
428
- *
429
- * @hidden This is not yet part of the public API.
430
- *
431
- * @param expr The expression to reduce.
432
- * @param opts The reduce options.
433
- * @returns A possibly reduced expression.
434
- */
435
- declare function reduceConditionals(expr: Expr, opts?: ReduceExprOptions): Expr;
436
-
437
- /**
438
- * Context interface that provides access to file system resources (such as external
439
- * data files) that are needed when parsing a Vensim model.
440
- */
441
- interface VensimParseContext {
442
- /**
443
- * Called when a `GET DIRECT SUBSCRIPTS` function call is encountered when parsing a
444
- * Vensim subscript range definition. The arguments are the same as those passed
445
- * to `GET DIRECT SUBSCRIPTS` in the model, except that the enclosing quotes have
446
- * already been removed.
447
- *
448
- * @param fileName The CSV or XLS[X] file path, or an indirect tag (e.g., '?data').
449
- * @param tabOrDelimiter The tab name (for XLS[X] files) or the delimiter (for CSV files).
450
- * @param firstCell The location of the first subscript element.
451
- * @param lastCell The location of the last subscript element.
452
- * @param prefix A string that is prepended to every subscript element.
453
- * @returns An array of subscript names read from the external data file.
454
- */
455
- getDirectSubscripts(fileName: string, tabOrDelimiter: string, firstCell: string, lastCell: string, prefix: string): SubName[];
456
- }
457
-
458
- /**
459
- * Parse the given Vensim subscript range definition and return a `DimensionDef` AST node.
460
- *
461
- * @param input A string containing the Vensim subscript range definition.
462
- * @param context An object that provides access to file system resources (such as
463
- * external data files) that are referenced during the parse phase.
464
- * @returns A `DimensionDef` AST node.
465
- */
466
- declare function parseVensimSubscriptRange(input: string, context?: VensimParseContext): DimensionDef;
467
-
468
- /**
469
- * Parse the given Vensim expression definition and return an `Expr` AST node.
470
- *
471
- * @param input A string containing the Vensim expression.
472
- * @returns An `Expr` AST node.
473
- */
474
- declare function parseVensimExpr(input: string): Expr;
475
-
476
- /**
477
- * Parse the given Vensim equation definition and return an `Equation` AST node.
478
- *
479
- * @param input A string containing the Vensim equation definition.
480
- * @returns An `Equation` AST node.
481
- */
482
- declare function parseVensimEquation(input: string): Equation;
483
-
484
- /**
485
- * Parse the given Vensim model definition and return a `Model` AST node.
486
- *
487
- * @param input A string containing the Vensim model.
488
- * @param context An object that provides access to file system resources (such as
489
- * external data files) that are referenced during the parse phase.
490
- * @param sort Whether to sort definitions alphabetically during the preprocessing phase.
491
- * @returns A `Model` AST node.
492
- */
493
- declare function parseVensimModel(input: string, context?: VensimParseContext, sort?: boolean): Model;
494
-
495
- /**
496
- * A single Vensim definition (either a subscript range definition or an
497
- * equation definition). This contains the definition's text and metadata
498
- * that was extracted during preprocessing.
499
- */
500
- interface VensimDef {
501
- /**
502
- * A simplified key for the LHS of the definition, used for sorting
503
- * and/or flattening.
504
- */
505
- key: string;
506
- /**
507
- * The preprocessed equation or subscript range definition (with
508
- * units and comment replaced with `~~|`).
509
- */
510
- def: string;
511
- /**
512
- * The kind of definition; either 'eqn' for an equation containing an equals
513
- * sign, 'dim' for a dimension (subscript range) definition, or 'decl' for
514
- * all other declarations (e.g., a lookup or data variable definition).
515
- */
516
- kind: 'eqn' | 'dim' | 'decl';
517
- /**
518
- * The (1-based) line number where the definition begins.
519
- */
520
- line: number;
521
- /**
522
- * The units text.
523
- */
524
- units: string;
525
- /**
526
- * The comment text.
527
- */
528
- comment: string;
529
- /**
530
- * The optional group name, if the definition is contained within a group.
531
- */
532
- group?: string;
533
- }
534
- /**
535
- * Result type for the `preprocessVensimModel` function.
536
- */
537
- interface PreprocessedVensimModel {
538
- /**
539
- * The preprocessed definitions that were preserved.
540
- */
541
- defs: VensimDef[];
542
- /**
543
- * The macros that were removed by the preprocessor.
544
- */
545
- removedMacros: string[];
546
- /**
547
- * The text blocks that were removed by the preprocessor. These include
548
- * unsupported functions (such as `TABBED ARRAY`) and other definitions
549
- * that were requested for removal.
550
- */
551
- removedBlocks: string[];
552
- }
553
- /**
554
- * Process the given Vensim model content so that it can be parsed
555
- * by `antlr4-vensim`. This will:
556
- * - strip out group markers
557
- * - remove macro definitions, which are currently unsupported
558
- * - remove equations that reference certain unsupported functions
559
- * (e.g., `TABBED ARRAY`)
560
- * - remove everything in the private Vensim sketch section
561
- * - join lines that are separated by a continuation (backslash)
562
- * - split the input into distinct definitions (equations and
563
- * subscript ranges)
564
- *
565
- * The definitions are further processed to preserve the units and
566
- * comment text in separate properties, but strips them from the
567
- * equation string (replaced with `~~`) to make it easier for
568
- * `antlr4-vensim` to process.
569
- *
570
- * @param input The original Vensim mdl file content.
571
- * @param options The options that control preprocessing.
572
- * @return A `PreprocessedVensimModel` instance containing the preprocessed
573
- * Vensim definitions.
574
- */
575
- declare function preprocessVensimModel(input: string, options?: {
576
- removalKeys?: string[];
577
- }): PreprocessedVensimModel;
578
-
579
- /**
580
- * Parse the given XMILE dimension (`<dim>`) definition and return a `DimensionDef` AST node.
581
- *
582
- * @param input A string containing the XMILE `<dim>` definition.
583
- * @returns A `DimensionDef` AST node.
584
- */
585
- declare function parseXmileDimensionDef(dimElem: XmlElement): DimensionDef;
586
-
587
- /**
588
- * Parse the given XMILE model definition and return a `Model` AST node.
589
- *
590
- * @param input A string containing the XMILE model.
591
- * @param context An object that provides access to file system resources (such as
592
- * external data files) that are referenced during the parse phase.
593
- * @returns A `Model` AST node.
594
- */
595
- declare function parseXmileModel(input: string): Model;
596
-
597
- /**
598
- * Parse the given XMILE variable definition and return an array of `Equation` AST nodes
599
- * corresponding to the variable definition (or definitions, in the case of a
600
- * non-apply-to-all variable that is defined with an `<element>` for each subscript).
601
- *
602
- * @param input A string containing the XMILE equation definition.
603
- * @returns An `Equation` AST node.
604
- */
605
- declare function parseXmileVariableDef(varElem: XmlElement): Equation[];
606
-
607
- export { type BinaryOp, type BinaryOpExpr, type DimId, type DimName, type DimOrSubId, type DimOrSubName, type DimensionDef, type Equation, type EquationLhs, type EquationRhs, type EquationRhsConstList, type EquationRhsData, type EquationRhsExpr, type EquationRhsLookup, type Expr, type FormatVariableRefFunc, type FunctionCall, type FunctionId, type FunctionName, type Keyword, type LookupCall, type LookupDef, type LookupPoint, type LookupRange, type Model, type NumberLiteral, type ParensExpr, type PreprocessedVensimModel, type PrettyOpts, type ReduceExprOptions, type SimulationSpec, type StringLiteral, type SubId, type SubName, type SubscriptMapping, type SubscriptRef, type UnaryOp, type UnaryOpExpr, type VariableDef, type VariableId, type VariableName, type VariableRef, type VensimDef, type VensimParseContext, canonicalFunctionId, canonicalId, canonicalVarId, debugPrintExpr, parseVensimEquation, parseVensimExpr, parseVensimModel, parseVensimSubscriptRange, parseXmileDimensionDef, parseXmileModel, parseXmileVariableDef, preprocessVensimModel, prettyPrintExpr, printExprStats, reduceConditionals, reduceExpr, toPrettyString };