@fincity/kirun-js 3.1.4 → 3.2.0

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.
Files changed (40) hide show
  1. package/__tests__/engine/dsl/GraphDebugTest.ts +316 -0
  2. package/__tests__/engine/runtime/expression/ExpressionParsingTest.ts +402 -14
  3. package/dist/index.js +15 -1
  4. package/dist/index.js.map +1 -1
  5. package/dist/module.js +15 -1
  6. package/dist/module.js.map +1 -1
  7. package/dist/types.d.ts +416 -0
  8. package/dist/types.d.ts.map +1 -1
  9. package/package.json +1 -1
  10. package/src/engine/dsl/DSLCompiler.ts +104 -0
  11. package/src/engine/dsl/index.ts +30 -0
  12. package/src/engine/dsl/lexer/DSLLexer.ts +518 -0
  13. package/src/engine/dsl/lexer/DSLToken.ts +74 -0
  14. package/src/engine/dsl/lexer/Keywords.ts +80 -0
  15. package/src/engine/dsl/lexer/LexerError.ts +37 -0
  16. package/src/engine/dsl/monaco/DSLFunctionProvider.ts +187 -0
  17. package/src/engine/dsl/parser/DSLParser.ts +1075 -0
  18. package/src/engine/dsl/parser/DSLParserError.ts +29 -0
  19. package/src/engine/dsl/parser/ast/ASTNode.ts +23 -0
  20. package/src/engine/dsl/parser/ast/ArgumentNode.ts +43 -0
  21. package/src/engine/dsl/parser/ast/ComplexValueNode.ts +22 -0
  22. package/src/engine/dsl/parser/ast/EventDeclNode.ts +27 -0
  23. package/src/engine/dsl/parser/ast/ExpressionNode.ts +33 -0
  24. package/src/engine/dsl/parser/ast/FunctionCallNode.ts +29 -0
  25. package/src/engine/dsl/parser/ast/FunctionDefNode.ts +37 -0
  26. package/src/engine/dsl/parser/ast/ParameterDeclNode.ts +25 -0
  27. package/src/engine/dsl/parser/ast/SchemaLiteralNode.ts +26 -0
  28. package/src/engine/dsl/parser/ast/SchemaNode.ts +23 -0
  29. package/src/engine/dsl/parser/ast/StatementNode.ts +41 -0
  30. package/src/engine/dsl/parser/ast/index.ts +14 -0
  31. package/src/engine/dsl/transformer/ASTToJSON.ts +378 -0
  32. package/src/engine/dsl/transformer/ExpressionHandler.ts +48 -0
  33. package/src/engine/dsl/transformer/JSONToText.ts +694 -0
  34. package/src/engine/dsl/transformer/SchemaTransformer.ts +110 -0
  35. package/src/engine/model/FunctionDefinition.ts +23 -0
  36. package/src/engine/runtime/expression/Expression.ts +5 -1
  37. package/src/engine/runtime/expression/ExpressionEvaluator.ts +152 -139
  38. package/src/engine/runtime/expression/ExpressionParser.ts +80 -27
  39. package/src/engine/util/duplicate.ts +3 -1
  40. package/src/index.ts +1 -0
package/dist/types.d.ts CHANGED
@@ -864,6 +864,369 @@ export class StatementGroup extends AbstractStatement {
864
864
  static from(json: any): StatementGroup;
865
865
  toJSON(): any;
866
866
  }
867
+ /**
868
+ * Token types for DSL lexer
869
+ */
870
+ export enum DSLTokenType {
871
+ KEYWORD = "KEYWORD",
872
+ IDENTIFIER = "IDENTIFIER",
873
+ NUMBER = "NUMBER",
874
+ STRING = "STRING",
875
+ BACKTICK_STRING = "BACKTICK_STRING",// `...` for expressions
876
+ BOOLEAN = "BOOLEAN",
877
+ NULL = "NULL",
878
+ COLON = "COLON",// :
879
+ COMMA = "COMMA",// ,
880
+ DOT = "DOT",// .
881
+ EQUALS = "EQUALS",// =
882
+ OPERATOR = "OPERATOR",// + - * / % < > ! ? & | @ ^ ~
883
+ LEFT_PAREN = "LEFT_PAREN",// (
884
+ RIGHT_PAREN = "RIGHT_PAREN",// )
885
+ LEFT_BRACE = "LEFT_BRACE",// {
886
+ RIGHT_BRACE = "RIGHT_BRACE",// }
887
+ LEFT_BRACKET = "LEFT_BRACKET",// [
888
+ RIGHT_BRACKET = "RIGHT_BRACKET",// ]
889
+ NEWLINE = "NEWLINE",
890
+ COMMENT = "COMMENT",
891
+ WHITESPACE = "WHITESPACE",
892
+ EOF = "EOF"
893
+ }
894
+ /**
895
+ * Position information for tokens
896
+ */
897
+ export class SourceLocation {
898
+ line: number;
899
+ column: number;
900
+ startPos: number;
901
+ endPos: number;
902
+ constructor(line: number, column: number, startPos: number, endPos: number);
903
+ toString(): string;
904
+ }
905
+ /**
906
+ * Token class with value and position tracking
907
+ */
908
+ export class DSLToken {
909
+ type: DSLTokenType;
910
+ value: string;
911
+ location: SourceLocation;
912
+ constructor(type: DSLTokenType, value: string, location: SourceLocation);
913
+ toString(): string;
914
+ is(type: DSLTokenType, value?: string): boolean;
915
+ }
916
+ /**
917
+ * Check if a string is a keyword
918
+ */
919
+ export function isKeyword(str: string): boolean;
920
+ /**
921
+ * Check if a string is a block name
922
+ */
923
+ export function isBlockName(str: string): boolean;
924
+ /**
925
+ * Check if a string is a primitive type
926
+ */
927
+ export function isPrimitiveType(str: string): boolean;
928
+ /**
929
+ * Lexer error with position information
930
+ */
931
+ export class LexerError extends Error {
932
+ location?: SourceLocation | undefined;
933
+ context?: string | undefined;
934
+ constructor(message: string, location?: SourceLocation | undefined, context?: string | undefined);
935
+ }
936
+ /**
937
+ * DSL Lexer - Tokenizes DSL text into tokens
938
+ *
939
+ * Features:
940
+ * - Keyword recognition
941
+ * - String literals with escape sequences
942
+ * - Number literals (integers and floats)
943
+ * - Comment stripping (single-line and block comments)
944
+ * - Position tracking for error messages
945
+ */
946
+ export class DSLLexer {
947
+ constructor(input: string);
948
+ /**
949
+ * Tokenize the input string
950
+ */
951
+ tokenize(): DSLToken[];
952
+ }
953
+ /**
954
+ * Base class for all AST nodes
955
+ */
956
+ export abstract class ASTNode {
957
+ readonly type: string;
958
+ readonly location: SourceLocation;
959
+ constructor(type: string, location: SourceLocation);
960
+ /**
961
+ * Convert AST node to JSON (for debugging/inspection)
962
+ */
963
+ abstract toJSON(): any;
964
+ /**
965
+ * Pretty-print the AST node
966
+ */
967
+ toString(): string;
968
+ }
969
+ /**
970
+ * Complex value node - represents literal values
971
+ * Used for parameter values that are objects, arrays, or primitives (strings, numbers, booleans, null)
972
+ */
973
+ export class ComplexValueNode extends ASTNode {
974
+ value: any;
975
+ constructor(value: any, location: SourceLocation);
976
+ toJSON(): any;
977
+ }
978
+ /**
979
+ * Expression node - wraps KIRun Expression
980
+ */
981
+ export class ExpressionNode extends ASTNode {
982
+ expressionText: string;
983
+ parsedExpression?: Expression;
984
+ constructor(expressionText: string, location: SourceLocation);
985
+ /**
986
+ * Parse the expression using KIRun Expression parser
987
+ */
988
+ parse(): void;
989
+ toJSON(): any;
990
+ }
991
+ /**
992
+ * Schema node - represents a schema specification
993
+ * Can be either a simple string (e.g., "INTEGER", "ARRAY OF STRING")
994
+ * or a complex JSON Schema object
995
+ */
996
+ export class SchemaNode extends ASTNode {
997
+ schemaSpec: string | object;
998
+ constructor(schemaSpec: string | object, location: SourceLocation);
999
+ toJSON(): any;
1000
+ }
1001
+ /**
1002
+ * Schema literal node - represents inline schema definitions with default values
1003
+ * Example: (ARRAY OF INTEGER) WITH DEFAULT VALUE []
1004
+ */
1005
+ export class SchemaLiteralNode extends ASTNode {
1006
+ schema: SchemaNode;
1007
+ defaultValue: ExpressionNode | undefined;
1008
+ constructor(schema: SchemaNode, defaultValue: ExpressionNode | undefined, location: SourceLocation);
1009
+ toJSON(): any;
1010
+ }
1011
+ type ArgumentValue = ExpressionNode | ComplexValueNode | SchemaLiteralNode;
1012
+ /**
1013
+ * Argument node - represents a function argument
1014
+ * Can be a single value or multiple values (multi-value parameter)
1015
+ * Each value can be an expression, complex value (object/array), or schema literal
1016
+ */
1017
+ export class ArgumentNode extends ASTNode {
1018
+ key: string;
1019
+ values: ArgumentValue[];
1020
+ constructor(key: string, value: ArgumentValue | ArgumentValue[], location: SourceLocation);
1021
+ /** Get the first (or only) value - for backwards compatibility */
1022
+ get value(): ArgumentValue;
1023
+ /** Check if this is a multi-value parameter */
1024
+ isMultiValue(): boolean;
1025
+ toJSON(): any;
1026
+ }
1027
+ /**
1028
+ * Parameter declaration node
1029
+ * Example: n AS INTEGER
1030
+ */
1031
+ export class ParameterDeclNode extends ASTNode {
1032
+ name: string;
1033
+ schema: SchemaNode;
1034
+ constructor(name: string, schema: SchemaNode, location: SourceLocation);
1035
+ toJSON(): any;
1036
+ }
1037
+ /**
1038
+ * Event declaration node
1039
+ * Example:
1040
+ * output
1041
+ * result AS ARRAY OF INTEGER
1042
+ */
1043
+ export class EventDeclNode extends ASTNode {
1044
+ name: string;
1045
+ parameters: ParameterDeclNode[];
1046
+ constructor(name: string, parameters: ParameterDeclNode[] | undefined, location: SourceLocation);
1047
+ toJSON(): any;
1048
+ }
1049
+ /**
1050
+ * Function call node
1051
+ * Example: System.Array.InsertLast(source = Context.a, element = 10)
1052
+ */
1053
+ export class FunctionCallNode extends ASTNode {
1054
+ namespace: string;
1055
+ name: string;
1056
+ argumentsMap: Map<string, ArgumentNode>;
1057
+ constructor(namespace: string, name: string, argumentsMap: Map<string, ArgumentNode>, location: SourceLocation);
1058
+ toJSON(): any;
1059
+ }
1060
+ /**
1061
+ * Statement node - represents a single statement in the LOGIC block
1062
+ * Example:
1063
+ * create: System.Context.Create(name = "a") AFTER Steps.prev
1064
+ * iteration
1065
+ * if: System.If(condition = true)
1066
+ */
1067
+ export class StatementNode extends ASTNode {
1068
+ statementName: string;
1069
+ functionCall: FunctionCallNode;
1070
+ afterSteps: string[];
1071
+ executeIfSteps: string[];
1072
+ nestedBlocks: Map<string, StatementNode[]>;
1073
+ comment: string;
1074
+ constructor(statementName: string, functionCall: FunctionCallNode, afterSteps: string[] | undefined, executeIfSteps: string[] | undefined, nestedBlocks: Map<string, StatementNode[]> | undefined, location: SourceLocation, comment?: string);
1075
+ toJSON(): any;
1076
+ }
1077
+ /**
1078
+ * Function definition node - root AST node
1079
+ * Represents the entire function definition
1080
+ */
1081
+ export class FunctionDefNode extends ASTNode {
1082
+ name: string;
1083
+ namespace?: string | undefined;
1084
+ parameters: ParameterDeclNode[];
1085
+ events: EventDeclNode[];
1086
+ logic: StatementNode[];
1087
+ constructor(name: string, namespace?: string | undefined, parameters?: ParameterDeclNode[], events?: EventDeclNode[], logic?: StatementNode[], location?: SourceLocation);
1088
+ toJSON(): any;
1089
+ }
1090
+ /**
1091
+ * Parser error with position information and expected tokens
1092
+ */
1093
+ export class DSLParserError extends Error {
1094
+ location?: SourceLocation | undefined;
1095
+ expectedTokens?: string[] | undefined;
1096
+ actualToken?: DSLToken | undefined;
1097
+ constructor(message: string, location?: SourceLocation | undefined, expectedTokens?: string[] | undefined, actualToken?: DSLToken | undefined);
1098
+ }
1099
+ /**
1100
+ * DSL Parser - Recursive descent parser
1101
+ * Converts tokens into AST
1102
+ */
1103
+ export class DSLParser {
1104
+ constructor(tokens: DSLToken[], originalInput?: string);
1105
+ /**
1106
+ * Parse tokens into AST
1107
+ */
1108
+ parse(): FunctionDefNode;
1109
+ }
1110
+ /**
1111
+ * Schema Transformer
1112
+ * Converts between simple schema syntax and JSON Schema
1113
+ */
1114
+ export class SchemaTransformer {
1115
+ /**
1116
+ * Transform simple schema syntax to JSON Schema
1117
+ * Examples:
1118
+ * "INTEGER" → { type: "INTEGER" }
1119
+ * "ARRAY OF INTEGER" → { type: "ARRAY", items: { type: "INTEGER" } }
1120
+ * { type: "STRING", minLength: 5 } → { type: "STRING", minLength: 5 } (pass through)
1121
+ */
1122
+ static transform(schemaSpec: string | object): any;
1123
+ /**
1124
+ * Transform JSON Schema back to simple schema syntax (best effort)
1125
+ * Examples:
1126
+ * { type: "INTEGER" } → "INTEGER"
1127
+ * { type: "ARRAY", items: { type: "INTEGER" } } → "ARRAY OF INTEGER"
1128
+ * Complex schema → JSON.stringify(schema)
1129
+ */
1130
+ static toText(schema: any): string;
1131
+ /**
1132
+ * Check if a schema specification is simple (can be represented as text)
1133
+ */
1134
+ static isSimpleSchema(schema: any): boolean;
1135
+ }
1136
+ /**
1137
+ * AST to JSON Transformer
1138
+ * Converts parsed AST to FunctionDefinition JSON
1139
+ */
1140
+ export class ASTToJSONTransformer {
1141
+ /**
1142
+ * Transform AST to FunctionDefinition JSON
1143
+ * Only includes non-empty/non-default fields
1144
+ */
1145
+ transform(ast: FunctionDefNode): any;
1146
+ /**
1147
+ * Flatten nested blocks into steps
1148
+ * This adds all nested statements to the top-level steps object
1149
+ * and sets their dependentStatements appropriately
1150
+ */
1151
+ flattenNestedBlocks(ast: FunctionDefNode): void;
1152
+ }
1153
+ /**
1154
+ * Expression Handler
1155
+ * Utilities for working with KIRun expressions
1156
+ */
1157
+ export class ExpressionHandler {
1158
+ /**
1159
+ * Parse expression text using KIRun Expression parser
1160
+ */
1161
+ static parse(expressionText: string): Expression;
1162
+ /**
1163
+ * Validate expression syntax
1164
+ */
1165
+ static validate(expressionText: string): boolean;
1166
+ /**
1167
+ * Check if a value is an expression (has isExpression flag)
1168
+ */
1169
+ static isExpression(value: any): boolean;
1170
+ /**
1171
+ * Extract expression text from value object
1172
+ */
1173
+ static extractExpressionText(value: any): string | null;
1174
+ }
1175
+ /**
1176
+ * JSON to Text Transformer
1177
+ * Converts FunctionDefinition JSON back to DSL text
1178
+ * Extracts implicit dependencies from expressions using same logic as KIRuntime
1179
+ */
1180
+ export class JSONToTextTransformer {
1181
+ /**
1182
+ * Transform JSON to DSL text
1183
+ */
1184
+ transform(json: any): Promise<string>;
1185
+ }
1186
+ export interface ValidationResult {
1187
+ valid: boolean;
1188
+ errors: FormattedError[];
1189
+ }
1190
+ export interface FormattedError {
1191
+ message: string;
1192
+ line?: number;
1193
+ column?: number;
1194
+ snippet?: string;
1195
+ }
1196
+ /**
1197
+ * DSL Compiler - High-level API
1198
+ * Main entry point for DSL compilation and decompilation
1199
+ */
1200
+ export class DSLCompiler {
1201
+ /**
1202
+ * Compile DSL text to FunctionDefinition JSON
1203
+ *
1204
+ * @param text DSL source text
1205
+ * @returns FunctionDefinition JSON
1206
+ */
1207
+ static compile(text: string): any;
1208
+ /**
1209
+ * Decompile FunctionDefinition JSON to DSL text
1210
+ *
1211
+ * @param json FunctionDefinition JSON
1212
+ * @returns DSL source text
1213
+ */
1214
+ static decompile(json: any): Promise<string>;
1215
+ /**
1216
+ * Validate DSL syntax without full compilation
1217
+ *
1218
+ * @param text DSL source text
1219
+ * @returns Validation result with errors if any
1220
+ */
1221
+ static validate(text: string): ValidationResult;
1222
+ /**
1223
+ * Format DSL text (parse and regenerate with consistent formatting)
1224
+ *
1225
+ * @param text DSL source text
1226
+ * @returns Formatted DSL text
1227
+ */
1228
+ static format(text: string): Promise<string>;
1229
+ }
867
1230
  export class FunctionDefinition extends FunctionSignature {
868
1231
  static readonly SCHEMA: Schema;
869
1232
  constructor(name: string);
@@ -876,6 +1239,17 @@ export class FunctionDefinition extends FunctionSignature {
876
1239
  getParts(): FunctionDefinition[] | undefined;
877
1240
  setParts(parts: FunctionDefinition[]): FunctionDefinition;
878
1241
  static from(json: any): FunctionDefinition;
1242
+ /**
1243
+ * Create FunctionDefinition from DSL text
1244
+ * @param text DSL source code
1245
+ * @returns FunctionDefinition instance
1246
+ */
1247
+ static fromText(text: string): Promise<FunctionDefinition>;
1248
+ /**
1249
+ * Convert FunctionDefinition to DSL text
1250
+ * @returns DSL source code
1251
+ */
1252
+ toText(): Promise<string>;
879
1253
  toJSON(): any;
880
1254
  }
881
1255
  export class GraphVertex<K, T extends GraphVertexType<K>> {
@@ -1034,5 +1408,47 @@ export class Argument {
1034
1408
  setValue(value: any): Argument;
1035
1409
  static of(name: string, value: any): Argument;
1036
1410
  }
1411
+ export interface FunctionInfo {
1412
+ namespace: string;
1413
+ name: string;
1414
+ fullName: string;
1415
+ parameters: ParameterInfo[];
1416
+ events: EventInfo[];
1417
+ description?: string;
1418
+ }
1419
+ export interface ParameterInfo {
1420
+ name: string;
1421
+ type: string;
1422
+ required: boolean;
1423
+ }
1424
+ export interface EventInfo {
1425
+ name: string;
1426
+ parameters: {
1427
+ [key: string]: string;
1428
+ };
1429
+ }
1430
+ /**
1431
+ * DSL Function Provider
1432
+ * Provides list of available functions for Monaco autocomplete
1433
+ */
1434
+ export class DSLFunctionProvider {
1435
+ /**
1436
+ * Get all available functions from repository
1437
+ * Uses repository.filter("") to dynamically discover all available functions
1438
+ */
1439
+ static getAllFunctions(repo?: Repository<Function>): Promise<FunctionInfo[]>;
1440
+ /**
1441
+ * Get functions by namespace
1442
+ */
1443
+ static getFunctionsByNamespace(namespace: string, repo?: Repository<Function>): Promise<FunctionInfo[]>;
1444
+ /**
1445
+ * Get all unique namespaces
1446
+ */
1447
+ static getAllNamespaces(repo?: Repository<Function>): Promise<string[]>;
1448
+ /**
1449
+ * Clear cached functions
1450
+ */
1451
+ static clearCache(repo?: Repository<Function>): void;
1452
+ }
1037
1453
 
1038
1454
  //# sourceMappingURL=types.d.ts.map