@sdeverywhere/parse 0.1.1 → 0.1.3

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 CHANGED
@@ -1,3 +1,46 @@
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
+ }
1
44
  /** The original name of a dimension, as it appears in the model. */
2
45
  type DimName = string;
3
46
  /** The canonical identifier of a dimension, as it appears in generated code. */
@@ -321,6 +364,13 @@ interface Equation {
321
364
  }
322
365
  /** A complete model definition, including all defined dimensions and equations. */
323
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;
324
374
  /** The array of all dimension definitions in the model. */
325
375
  dimensions: DimensionDef[];
326
376
  /** The array of all variable/equation definitions in the model. */
@@ -442,4 +492,116 @@ declare function parseVensimEquation(input: string): Equation;
442
492
  */
443
493
  declare function parseVensimModel(input: string, context?: VensimParseContext, sort?: boolean): Model;
444
494
 
445
- 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 PrettyOpts, type ReduceExprOptions, type StringLiteral, type SubId, type SubName, type SubscriptMapping, type SubscriptRef, type UnaryOp, type UnaryOpExpr, type VariableDef, type VariableId, type VariableName, type VariableRef, type VensimParseContext, debugPrintExpr, parseVensimEquation, parseVensimExpr, parseVensimModel, parseVensimSubscriptRange, prettyPrintExpr, printExprStats, reduceConditionals, reduceExpr, toPrettyString };
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 };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,46 @@
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
+ }
1
44
  /** The original name of a dimension, as it appears in the model. */
2
45
  type DimName = string;
3
46
  /** The canonical identifier of a dimension, as it appears in generated code. */
@@ -321,6 +364,13 @@ interface Equation {
321
364
  }
322
365
  /** A complete model definition, including all defined dimensions and equations. */
323
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;
324
374
  /** The array of all dimension definitions in the model. */
325
375
  dimensions: DimensionDef[];
326
376
  /** The array of all variable/equation definitions in the model. */
@@ -442,4 +492,116 @@ declare function parseVensimEquation(input: string): Equation;
442
492
  */
443
493
  declare function parseVensimModel(input: string, context?: VensimParseContext, sort?: boolean): Model;
444
494
 
445
- 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 PrettyOpts, type ReduceExprOptions, type StringLiteral, type SubId, type SubName, type SubscriptMapping, type SubscriptRef, type UnaryOp, type UnaryOpExpr, type VariableDef, type VariableId, type VariableName, type VariableRef, type VensimParseContext, debugPrintExpr, parseVensimEquation, parseVensimExpr, parseVensimModel, parseVensimSubscriptRange, prettyPrintExpr, printExprStats, reduceConditionals, reduceExpr, toPrettyString };
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 };