@atomic-ehr/fhirpath 0.0.1 → 0.0.2-canary.261bb9b.20250811212042

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 (104) hide show
  1. package/README.md +62 -56
  2. package/dist/index.d.ts +368 -99
  3. package/dist/index.js +3623 -1253
  4. package/dist/index.js.map +1 -1
  5. package/package.json +1 -1
  6. package/src/analyzer.ts +779 -120
  7. package/src/boxing.ts +124 -0
  8. package/src/completion-provider.ts +636 -0
  9. package/src/cursor-nodes.ts +111 -0
  10. package/src/errors.ts +247 -0
  11. package/src/index.ts +97 -14
  12. package/src/inspect.ts +309 -176
  13. package/src/interpreter.ts +380 -71
  14. package/src/lexer.ts +1 -0
  15. package/src/model-provider.ts +282 -39
  16. package/src/operations/abs-function.ts +40 -16
  17. package/src/operations/aggregate-function.ts +21 -10
  18. package/src/operations/all-function.ts +23 -11
  19. package/src/operations/allFalse-function.ts +14 -9
  20. package/src/operations/allTrue-function.ts +14 -9
  21. package/src/operations/and-operator.ts +21 -12
  22. package/src/operations/anyFalse-function.ts +11 -7
  23. package/src/operations/anyTrue-function.ts +11 -7
  24. package/src/operations/as-operator.ts +2 -1
  25. package/src/operations/ceiling-function.ts +14 -8
  26. package/src/operations/children-function.ts +98 -0
  27. package/src/operations/combine-function.ts +10 -6
  28. package/src/operations/combine-operator.ts +19 -5
  29. package/src/operations/contains-function.ts +26 -12
  30. package/src/operations/contains-operator.ts +15 -7
  31. package/src/operations/count-function.ts +7 -4
  32. package/src/operations/defineVariable-function.ts +11 -7
  33. package/src/operations/descendants-function.ts +64 -0
  34. package/src/operations/distinct-function.ts +21 -7
  35. package/src/operations/div-operator.ts +16 -3
  36. package/src/operations/divide-operator.ts +11 -6
  37. package/src/operations/dot-operator.ts +4 -2
  38. package/src/operations/empty-function.ts +7 -4
  39. package/src/operations/endsWith-function.ts +28 -14
  40. package/src/operations/equal-operator.ts +19 -7
  41. package/src/operations/equivalent-operator.ts +16 -11
  42. package/src/operations/exclude-function.ts +18 -14
  43. package/src/operations/exists-function.ts +19 -10
  44. package/src/operations/first-function.ts +12 -7
  45. package/src/operations/floor-function.ts +14 -8
  46. package/src/operations/greater-operator.ts +10 -5
  47. package/src/operations/greater-or-equal-operator.ts +10 -5
  48. package/src/operations/iif-function.ts +27 -23
  49. package/src/operations/implies-operator.ts +23 -8
  50. package/src/operations/in-operator.ts +18 -8
  51. package/src/operations/index.ts +3 -0
  52. package/src/operations/indexOf-function.ts +28 -14
  53. package/src/operations/intersect-function.ts +23 -24
  54. package/src/operations/is-operator.ts +67 -15
  55. package/src/operations/isDistinct-function.ts +17 -9
  56. package/src/operations/join-function.ts +22 -9
  57. package/src/operations/last-function.ts +12 -7
  58. package/src/operations/length-function.ts +17 -8
  59. package/src/operations/less-operator.ts +10 -5
  60. package/src/operations/less-or-equal-operator.ts +10 -5
  61. package/src/operations/less-than.ts +26 -2
  62. package/src/operations/lower-function.ts +17 -8
  63. package/src/operations/minus-operator.ts +10 -5
  64. package/src/operations/mod-operator.ts +20 -3
  65. package/src/operations/multiply-operator.ts +12 -7
  66. package/src/operations/not-equal-operator.ts +16 -7
  67. package/src/operations/not-equivalent-operator.ts +16 -11
  68. package/src/operations/not-function.ts +17 -7
  69. package/src/operations/ofType-function.ts +139 -0
  70. package/src/operations/or-operator.ts +21 -12
  71. package/src/operations/plus-operator.ts +19 -7
  72. package/src/operations/power-function.ts +27 -13
  73. package/src/operations/replace-function.ts +33 -14
  74. package/src/operations/round-function.ts +29 -12
  75. package/src/operations/select-function.ts +17 -9
  76. package/src/operations/single-function.ts +10 -6
  77. package/src/operations/skip-function.ts +18 -9
  78. package/src/operations/split-function.ts +30 -13
  79. package/src/operations/sqrt-function.ts +14 -8
  80. package/src/operations/startsWith-function.ts +26 -12
  81. package/src/operations/subsetOf-function.ts +20 -15
  82. package/src/operations/substring-function.ts +43 -24
  83. package/src/operations/supersetOf-function.ts +20 -15
  84. package/src/operations/tail-function.ts +8 -4
  85. package/src/operations/take-function.ts +18 -9
  86. package/src/operations/toBoolean-function.ts +23 -14
  87. package/src/operations/toDecimal-function.ts +18 -9
  88. package/src/operations/toInteger-function.ts +22 -12
  89. package/src/operations/toString-function.ts +26 -16
  90. package/src/operations/trace-function.ts +19 -10
  91. package/src/operations/trim-function.ts +16 -7
  92. package/src/operations/truncate-function.ts +14 -8
  93. package/src/operations/unary-minus-operator.ts +23 -13
  94. package/src/operations/unary-plus-operator.ts +2 -1
  95. package/src/operations/union-function.ts +25 -28
  96. package/src/operations/union-operator.ts +2 -1
  97. package/src/operations/upper-function.ts +17 -8
  98. package/src/operations/where-function.ts +20 -11
  99. package/src/operations/xor-operator.ts +9 -4
  100. package/src/parser.ts +527 -8
  101. package/src/quantity-value.ts +4 -8
  102. package/src/registry.ts +147 -5
  103. package/src/types.ts +42 -19
  104. package/src/parser-base.ts +0 -400
package/dist/index.d.ts CHANGED
@@ -1,5 +1,37 @@
1
1
  import { FHIRSchema } from '@atomic-ehr/fhirschema';
2
2
 
3
+ /**
4
+ * Symbol to mark boxed values
5
+ */
6
+ declare const BOXED_SYMBOL: unique symbol;
7
+ /**
8
+ * Extension structure for FHIR primitive elements
9
+ */
10
+ interface Extension {
11
+ url: string;
12
+ [key: string]: any;
13
+ }
14
+ /**
15
+ * Metadata for FHIR primitive elements (e.g., _gender for gender property)
16
+ */
17
+ interface PrimitiveElement {
18
+ id?: string;
19
+ extension?: Extension[];
20
+ }
21
+ /**
22
+ * Boxed FHIRPath value with metadata
23
+ */
24
+ interface FHIRPathValue<T = any> {
25
+ /** The actual value */
26
+ value: T;
27
+ /** Type information from ModelProvider */
28
+ typeInfo?: TypeInfo;
29
+ /** Primitive element extension (for primitives only) */
30
+ primitiveElement?: PrimitiveElement;
31
+ /** Internal marker to identify boxed values */
32
+ [BOXED_SYMBOL]: true;
33
+ }
34
+
3
35
  /**
4
36
  * Simplified FHIRPath Lexer
5
37
  *
@@ -32,6 +64,7 @@ declare enum TokenType {
32
64
  RBRACE = 57,// }
33
65
  SPECIAL_IDENTIFIER = 60,// $...
34
66
  ENVIRONMENT_VARIABLE = 70,// %identifier, %`delimited`, %'string'
67
+ CURSOR = 71,// Virtual cursor token for LSP support
35
68
  WHITESPACE = 80,
36
69
  LINE_COMMENT = 81,
37
70
  BLOCK_COMMENT = 82
@@ -135,10 +168,19 @@ interface TypeInfo<TypeContext = unknown> {
135
168
  modelContext?: TypeContext;
136
169
  }
137
170
  interface ModelProvider<TypeContext = unknown> {
138
- getType(typeName: string): TypeInfo<TypeContext> | undefined;
139
- getElementType(parentType: TypeInfo<TypeContext>, propertyName: string): TypeInfo<TypeContext> | undefined;
171
+ getType(typeName: string): Promise<TypeInfo<TypeContext> | undefined>;
172
+ getElementType(parentType: TypeInfo<TypeContext>, propertyName: string): Promise<TypeInfo<TypeContext> | undefined>;
140
173
  ofType(type: TypeInfo<TypeContext>, typeName: TypeName): TypeInfo<TypeContext> | undefined;
141
174
  getElementNames(parentType: TypeInfo<TypeContext>): string[];
175
+ getChildrenType(parentType: TypeInfo<TypeContext>): Promise<TypeInfo<TypeContext> | undefined>;
176
+ getElements(typeName: string): Promise<Array<{
177
+ name: string;
178
+ type: string;
179
+ documentation?: string;
180
+ }>>;
181
+ getResourceTypes(): Promise<string[]>;
182
+ getComplexTypes(): Promise<string[]>;
183
+ getPrimitiveTypes(): Promise<string[]>;
142
184
  }
143
185
  interface OperatorSignature {
144
186
  name: string;
@@ -157,21 +199,23 @@ interface OperatorDefinition {
157
199
  signatures: OperatorSignature[];
158
200
  evaluate: OperationEvaluator;
159
201
  }
202
+ interface FunctionSignature {
203
+ name: string;
204
+ input: TypeInfo;
205
+ parameters: Array<{
206
+ name: string;
207
+ optional?: boolean;
208
+ type: TypeInfo;
209
+ expression?: boolean;
210
+ }>;
211
+ result: TypeInfo | 'inputType' | 'inputTypeSingleton' | 'parameterType';
212
+ }
160
213
  interface FunctionDefinition {
161
214
  name: string;
162
215
  category: string[];
163
216
  description: string;
164
217
  examples: string[];
165
- signature: {
166
- input: TypeInfo;
167
- parameters: Array<{
168
- name: string;
169
- optional?: boolean;
170
- type: TypeInfo;
171
- expression?: boolean;
172
- }>;
173
- result: TypeInfo | 'inputType' | 'inputTypeSingleton' | 'parameterType';
174
- };
218
+ signatures: FunctionSignature[];
175
219
  evaluate: FunctionEvaluator;
176
220
  }
177
221
  declare enum NodeType {
@@ -301,9 +345,11 @@ interface RuntimeContext {
301
345
  input: any[];
302
346
  focus: any[];
303
347
  variables: Record<string, any>;
348
+ currentNode?: ASTNode;
349
+ modelProvider?: ModelProvider;
304
350
  }
305
351
  interface EvaluationResult {
306
- value: any[];
352
+ value: FHIRPathValue[];
307
353
  context: RuntimeContext;
308
354
  }
309
355
  declare enum DiagnosticSeverity {
@@ -346,51 +392,8 @@ interface ParseResult {
346
392
  availableCompletions: string[];
347
393
  };
348
394
  }
349
- type OperationEvaluator = (input: any[], context: RuntimeContext, ...args: any[]) => EvaluationResult;
350
- type FunctionEvaluator = (input: any[], context: RuntimeContext, args: ASTNode[], evaluator: (node: ASTNode, input: any[], context: RuntimeContext) => EvaluationResult) => EvaluationResult;
351
-
352
- /**
353
- * Abstract base parser with shared parsing logic
354
- * Subclasses must implement node creation and error handling
355
- */
356
- declare abstract class BaseParser<TNode extends BaseASTNode = BaseASTNode> {
357
- protected lexer: Lexer;
358
- protected tokens: Token[];
359
- protected current: number;
360
- constructor(input: string, lexerOptions?: LexerOptions);
361
- protected abstract createIdentifierNode(name: string, token: Token): TNode;
362
- protected abstract createLiteralNode(value: any, valueType: 'string' | 'number' | 'boolean' | 'date' | 'time' | 'datetime' | 'null', token: Token): TNode;
363
- protected abstract createBinaryNode(token: Token, left: TNode, right: TNode): TNode;
364
- protected abstract createUnaryNode(token: Token, operand: TNode): TNode;
365
- protected abstract createFunctionNode(name: TNode, args: TNode[], startToken: Token): TNode;
366
- protected abstract createVariableNode(name: string, token: Token): TNode;
367
- protected abstract createIndexNode(expression: TNode, index: TNode, startToken: Token): TNode;
368
- protected abstract createMembershipTestNode(expression: TNode, targetType: string, startToken: Token): TNode;
369
- protected abstract createTypeCastNode(expression: TNode, targetType: string, startToken: Token): TNode;
370
- protected abstract createCollectionNode(elements: TNode[], startToken: Token): TNode;
371
- protected abstract createQuantityNode(value: number, unit: string, isCalendarUnit: boolean, startToken: Token, endToken: Token): TNode;
372
- protected abstract handleError(message: string, token?: Token): never | TNode;
373
- protected expression(): TNode;
374
- protected parseExpressionWithPrecedence(minPrecedence: number): TNode;
375
- protected parsePrimary(): TNode;
376
- protected parseInvocation(): TNode;
377
- protected parseArgumentList(): TNode[];
378
- protected parseCollectionElements(): TNode[];
379
- protected parseTypeName(): string;
380
- protected parseStringValue(raw: string): string;
381
- protected parseIdentifierValue(raw: string): string;
382
- protected isFunctionCall(node: TNode): boolean;
383
- protected isBinaryOperatorToken(token: Token): boolean;
384
- protected isKeywordAllowedAsMember(token: Token): boolean;
385
- protected isKeywordAllowedAsIdentifier(token: Token): boolean;
386
- protected peek(): Token;
387
- protected previous(): Token;
388
- protected isAtEnd(): boolean;
389
- protected advance(): Token;
390
- protected check(type: TokenType): boolean;
391
- protected match(...types: TokenType[]): boolean;
392
- protected consume(type: TokenType, message: string): Token;
393
- }
395
+ type OperationEvaluator = (input: FHIRPathValue[], context: RuntimeContext, ...args: any[]) => Promise<EvaluationResult>;
396
+ type FunctionEvaluator = (input: FHIRPathValue[], context: RuntimeContext, args: ASTNode[], evaluator: (node: ASTNode, input: FHIRPathValue[], context: RuntimeContext) => Promise<EvaluationResult>) => Promise<EvaluationResult>;
394
397
 
395
398
  interface ParserOptions {
396
399
  mode?: 'simple' | 'lsp';
@@ -400,8 +403,12 @@ interface ParserOptions {
400
403
  partialParse?: {
401
404
  cursorPosition: number;
402
405
  };
406
+ cursorPosition?: number;
403
407
  }
404
- declare class Parser extends BaseParser<ASTNode> {
408
+ declare class Parser {
409
+ protected lexer: Lexer;
410
+ protected tokens: Token[];
411
+ protected current: number;
405
412
  private mode;
406
413
  private options;
407
414
  private errors?;
@@ -413,12 +420,34 @@ declare class Parser extends BaseParser<ASTNode> {
413
420
  private input;
414
421
  private readonly synchronizationTokens;
415
422
  constructor(input: string, options?: ParserOptions);
423
+ private checkCursor;
424
+ private injectCursorToken;
416
425
  private getRangeFromToken;
417
426
  private getRangeFromTokens;
418
427
  private getRangeFromNodes;
419
428
  parse(): ParseResult;
420
429
  private parseSimple;
421
430
  private parseLSP;
431
+ protected expression(): ASTNode;
432
+ protected parseExpressionWithPrecedence(minPrecedence: number): ASTNode;
433
+ protected parsePrimary(): ASTNode;
434
+ protected parseInvocation(): ASTNode;
435
+ protected parseArgumentList(): ASTNode[];
436
+ protected parseCollectionElements(): ASTNode[];
437
+ protected parseTypeName(): string;
438
+ protected parseStringValue(raw: string): string;
439
+ protected parseIdentifierValue(raw: string): string;
440
+ protected isFunctionCall(node: ASTNode): boolean;
441
+ protected isBinaryOperatorToken(token: Token): boolean;
442
+ protected isKeywordAllowedAsMember(token: Token): boolean;
443
+ protected isKeywordAllowedAsIdentifier(token: Token): boolean;
444
+ protected peek(): Token;
445
+ protected previous(): Token;
446
+ protected isAtEnd(): boolean;
447
+ protected advance(): Token;
448
+ protected check(type: TokenType): boolean;
449
+ protected match(...types: TokenType[]): boolean;
450
+ protected consume(type: TokenType, message: string): Token;
422
451
  protected createIdentifierNode(name: string, token: Token): ASTNode;
423
452
  protected createLiteralNode(value: any, valueType: LiteralNode['valueType'], token: Token): LiteralNode;
424
453
  protected createBinaryNode(token: Token, left: ASTNode, right: ASTNode): BinaryNode;
@@ -465,6 +494,22 @@ declare class Registry {
465
494
  listOperators(): string[];
466
495
  listAllOperations(): string[];
467
496
  getOperationInfo(name: string): OperatorDefinition | FunctionDefinition | undefined;
497
+ /**
498
+ * Get functions applicable to a specific type
499
+ */
500
+ getFunctionsForType(typeName: TypeName | string): FunctionDefinition[];
501
+ /**
502
+ * Get operators applicable to a specific type
503
+ */
504
+ getOperatorsForType(typeName: TypeName | string): OperatorDefinition[];
505
+ /**
506
+ * Check if a function is applicable to a type
507
+ */
508
+ isFunctionApplicableToType(functionName: string, typeName: TypeName | string): boolean;
509
+ /**
510
+ * Check if an operator is applicable to a type
511
+ */
512
+ isOperatorApplicableToType(operatorSymbol: string, typeName: TypeName | string): boolean;
468
513
  }
469
514
  declare const registry: Registry;
470
515
 
@@ -473,9 +518,10 @@ declare class Interpreter {
473
518
  private nodeEvaluators;
474
519
  private operationEvaluators;
475
520
  private functionEvaluators;
476
- constructor(registry?: Registry);
521
+ private modelProvider?;
522
+ constructor(registry?: Registry, modelProvider?: ModelProvider<any>);
477
523
  private registerOperationEvaluators;
478
- evaluate(node: ASTNode, input?: any[], context?: RuntimeContext): EvaluationResult;
524
+ evaluate(node: ASTNode, input?: any[], context?: RuntimeContext): Promise<EvaluationResult>;
479
525
  private createInitialContext;
480
526
  private evaluateLiteral;
481
527
  private evaluateIdentifier;
@@ -491,19 +537,82 @@ declare class Interpreter {
491
537
  private evaluateQuantity;
492
538
  }
493
539
 
540
+ declare enum CursorContext {
541
+ Operator = "operator",
542
+ Identifier = "identifier",
543
+ Argument = "argument",
544
+ Index = "index",
545
+ Type = "type"
546
+ }
547
+ interface CursorNode {
548
+ type: 'CursorNode';
549
+ context: CursorContext;
550
+ position: number;
551
+ range: {
552
+ start: {
553
+ line: number;
554
+ character: number;
555
+ offset: number;
556
+ };
557
+ end: {
558
+ line: number;
559
+ character: number;
560
+ offset: number;
561
+ };
562
+ };
563
+ }
564
+ interface CursorOperatorNode extends CursorNode {
565
+ context: CursorContext.Operator;
566
+ }
567
+ interface CursorIdentifierNode extends CursorNode {
568
+ context: CursorContext.Identifier;
569
+ }
570
+ interface CursorArgumentNode extends CursorNode {
571
+ context: CursorContext.Argument;
572
+ functionName: string;
573
+ argumentIndex: number;
574
+ }
575
+ interface CursorIndexNode extends CursorNode {
576
+ context: CursorContext.Index;
577
+ }
578
+ interface CursorTypeNode extends CursorNode {
579
+ context: CursorContext.Type;
580
+ typeOperator: 'is' | 'as' | 'ofType';
581
+ }
582
+ type AnyCursorNode = CursorOperatorNode | CursorIdentifierNode | CursorArgumentNode | CursorIndexNode | CursorTypeNode;
583
+ declare function isCursorNode(node: any): node is CursorNode;
584
+
585
+ interface AnalyzerOptions {
586
+ cursorMode?: boolean;
587
+ }
588
+ interface AnalysisResultWithCursor extends AnalysisResult {
589
+ stoppedAtCursor?: boolean;
590
+ cursorContext?: {
591
+ typeBeforeCursor?: TypeInfo;
592
+ expectedType?: TypeInfo;
593
+ cursorNode?: AnyCursorNode;
594
+ };
595
+ }
494
596
  declare class Analyzer {
495
597
  private diagnostics;
496
598
  private variables;
497
599
  private modelProvider?;
498
600
  private userVariableTypes;
601
+ private systemVariableTypes;
602
+ private cursorMode;
603
+ private stoppedAtCursor;
604
+ private cursorContext?;
499
605
  constructor(modelProvider?: ModelProvider);
500
- analyze(ast: ASTNode, userVariables?: Record<string, any>, inputType?: TypeInfo): AnalysisResult;
606
+ analyze(ast: ASTNode, userVariables?: Record<string, any>, inputType?: TypeInfo, options?: AnalyzerOptions): Promise<AnalysisResultWithCursor>;
501
607
  private visitNode;
502
608
  private visitBinaryOperator;
503
609
  private visitIdentifier;
504
610
  private visitFunctionCall;
611
+ private visitMembershipTest;
612
+ private visitTypeCast;
505
613
  private validateVariable;
506
- private addDiagnostic;
614
+ private collectDefinedVariables;
615
+ private collectDefinedVariablesWithTypes;
507
616
  private inferType;
508
617
  private inferErrorNodeType;
509
618
  private inferLiteralType;
@@ -524,6 +633,10 @@ declare class Analyzer {
524
633
  private checkBinaryOperatorTypes;
525
634
  private checkFunctionArgumentTypes;
526
635
  private typeToString;
636
+ /**
637
+ * Infer the expected type at a cursor position based on context
638
+ */
639
+ private inferExpectedTypeForCursor;
527
640
  /**
528
641
  * Annotate AST with type information
529
642
  */
@@ -565,68 +678,224 @@ declare class FHIRModelProvider implements ModelProvider<FHIRModelContext> {
565
678
  private schemaCache;
566
679
  private hierarchyCache;
567
680
  private initialized;
681
+ private complexTypesCache?;
682
+ private primitiveTypesCache?;
683
+ private resourceTypesCache?;
568
684
  private readonly typeMapping;
569
- private readonly commonTypes;
570
- constructor(config: FHIRModelProviderConfig);
685
+ private readonly primitiveTypeMapping;
686
+ constructor(config?: FHIRModelProviderConfig);
571
687
  initialize(): Promise<void>;
572
688
  private buildCanonicalUrl;
573
- private loadSchemaAsync;
574
- private loadSchemaCached;
689
+ getSchema(typeName: string): Promise<FHIRSchema | undefined>;
575
690
  private getSchemaHierarchyAsync;
691
+ private extractTypeName;
576
692
  private getSchemaHierarchyCached;
577
693
  private mapToFHIRPathType;
578
694
  private isChoiceType;
579
695
  private createUnionContext;
580
- getType(typeName: string): TypeInfo<FHIRModelContext> | undefined;
581
- getElementType(parentType: TypeInfo<FHIRModelContext>, propertyName: string): TypeInfo<FHIRModelContext> | undefined;
696
+ getType(typeName: string): Promise<TypeInfo<FHIRModelContext> | undefined>;
697
+ getElementType(parentType: TypeInfo<FHIRModelContext>, propertyName: string): Promise<TypeInfo<FHIRModelContext> | undefined>;
582
698
  ofType(type: TypeInfo<FHIRModelContext>, typeName: TypeName): TypeInfo<FHIRModelContext> | undefined;
583
699
  getElementNames(parentType: TypeInfo<FHIRModelContext>): string[];
700
+ getChildrenType(parentType: TypeInfo<FHIRModelContext>): Promise<TypeInfo<FHIRModelContext> | undefined>;
584
701
  loadType(typeName: string): Promise<TypeInfo<FHIRModelContext> | undefined>;
702
+ getElements(typeName: string): Promise<Array<{
703
+ name: string;
704
+ type: string;
705
+ documentation?: string;
706
+ }>>;
707
+ getResourceTypes(): Promise<string[]>;
708
+ getComplexTypes(): Promise<string[]>;
709
+ getPrimitiveTypes(): Promise<string[]>;
710
+ getTypeFromCache(typeName: string): TypeInfo<FHIRModelContext> | undefined;
585
711
  }
586
712
 
587
- interface TraceEntry {
588
- name: string;
589
- values: any[];
713
+ interface ASTMetadata {
714
+ complexity: number;
590
715
  depth: number;
591
- timestamp: number;
592
- projection?: any[];
593
- }
594
- interface InspectOptions {
595
- input?: unknown;
596
- variables?: Record<string, unknown>;
597
- maxTraces?: number;
716
+ operationCount: Map<string, number>;
598
717
  }
599
718
  interface InspectResult {
600
- expression: string;
601
- result: any[];
602
- ast: ASTNode;
603
- traces: TraceEntry[];
604
- executionTime: number;
605
- errors?: Array<{
606
- type: string;
607
- message: string;
608
- location?: {
609
- line: number;
610
- column: number;
611
- };
612
- }>;
613
- warnings?: Array<{
614
- code: string;
615
- message: string;
719
+ result: FHIRPathValue[];
720
+ ast: {
721
+ node: ASTNode;
722
+ metadata: ASTMetadata;
723
+ };
724
+ diagnostics: {
725
+ warnings: Diagnostic[];
726
+ hints: Array<{
727
+ message: string;
728
+ suggestion?: string;
729
+ }>;
730
+ };
731
+ performance: {
732
+ parseTime: number;
733
+ analyzeTime: number;
734
+ evalTime: number;
735
+ totalTime: number;
736
+ operationTimings: Map<string, number>;
737
+ };
738
+ traces?: Array<{
739
+ label: string;
740
+ value: FHIRPathValue[];
741
+ timestamp: number;
616
742
  }>;
617
743
  }
618
- declare function inspect(expression: string, options?: InspectOptions): InspectResult;
744
+ interface InspectOptions {
745
+ input?: any;
746
+ variables?: Record<string, any>;
747
+ includeTraces?: boolean;
748
+ maxDepth?: number;
749
+ }
750
+ declare function inspect(expression: string, options?: InspectOptions): Promise<InspectResult>;
751
+
752
+ /**
753
+ * Base error class for all FHIRPath errors
754
+ */
755
+ declare class FHIRPathError extends Error {
756
+ code: string;
757
+ location?: Range | undefined;
758
+ constructor(code: string, message: string, location?: Range | undefined);
759
+ }
760
+ /**
761
+ * Error factory with specialized constructors
762
+ */
763
+ declare const Errors: {
764
+ unknownOperator(operator: string, location?: Range): FHIRPathError;
765
+ unknownFunction(name: string, location?: Range): FHIRPathError;
766
+ unknownVariable(name: string, location?: Range): FHIRPathError;
767
+ unknownUserVariable(name: string, location?: Range): FHIRPathError;
768
+ unknownProperty(property: string, type: string, location?: Range): FHIRPathError;
769
+ unknownNodeType(nodeType: string, location?: Range): FHIRPathError;
770
+ noEvaluatorFound(evaluatorType: string, name: string, location?: Range): FHIRPathError;
771
+ variableNotDefined(name: string, location?: Range): FHIRPathError;
772
+ wrongArgumentCount(funcName: string, expected: number, actual: number, location?: Range): FHIRPathError;
773
+ wrongArgumentCountRange(funcName: string, min: number, max: number, actual: number, location?: Range): FHIRPathError;
774
+ singletonRequired(funcName: string, actualCount: number, location?: Range): FHIRPathError;
775
+ stringSingletonRequired(funcName: string, actualCount: number, location?: Range): FHIRPathError;
776
+ emptyNotAllowed(funcName: string, location?: Range): FHIRPathError;
777
+ argumentRequired(funcName: string, argumentName: string, location?: Range): FHIRPathError;
778
+ singletonTypeRequired(funcName: string, actualType: string, location?: Range): FHIRPathError;
779
+ operatorTypeMismatch(operator: string, leftType: string, rightType: string, location?: Range): FHIRPathError;
780
+ argumentTypeMismatch(argIndex: number, funcName: string, expected: string, actual: string, location?: Range): FHIRPathError;
781
+ conversionFailed(value: string, targetType: string, location?: Range): FHIRPathError;
782
+ invalidValueType(expected: string, actual: string, location?: Range): FHIRPathError;
783
+ invalidOperandType(operation: string, type: string, location?: Range): FHIRPathError;
784
+ stringOperationOnNonString(operation: string, location?: Range): FHIRPathError;
785
+ numericOperationOnNonNumeric(operation: string, location?: Range): FHIRPathError;
786
+ booleanOperationOnNonBoolean(operation: string, index: number, actualType: string, location?: Range): FHIRPathError;
787
+ modelProviderRequired(operation: string, location?: Range): FHIRPathError;
788
+ unexpectedToken(token: string, location?: Range): FHIRPathError;
789
+ expectedToken(expected: string, actual: string, location?: Range): FHIRPathError;
790
+ invalidSyntax(details: string, location?: Range): FHIRPathError;
791
+ expectedIdentifier(after: string, actual: string, location?: Range): FHIRPathError;
792
+ expectedTypeName(actual: string, location?: Range): FHIRPathError;
793
+ divisionByZero(location?: Range): FHIRPathError;
794
+ invalidDateTimeFormat(format: string, location?: Range): FHIRPathError;
795
+ incompatibleUnits(unit1: string, unit2: string, location?: Range): FHIRPathError;
796
+ indexOutOfBounds(index: number, size: number, location?: Range): FHIRPathError;
797
+ invalidOperation(details: string, location?: Range): FHIRPathError;
798
+ invalidPrecision(operation: string, location?: Range): FHIRPathError;
799
+ invalidStringOperation(operation: string, paramName: string, location?: Range): FHIRPathError;
800
+ invalidNumericOperation(operation: string, paramName: string, expectedType: string, location?: Range): FHIRPathError;
801
+ };
802
+ declare enum ErrorCodes {
803
+ UNKNOWN_OPERATOR = "FP1001",
804
+ UNKNOWN_FUNCTION = "FP1002",
805
+ UNKNOWN_VARIABLE = "FP1003",
806
+ UNKNOWN_USER_VARIABLE = "FP1004",
807
+ UNKNOWN_PROPERTY = "FP1005",
808
+ UNKNOWN_NODE_TYPE = "FP1006",
809
+ NO_EVALUATOR_FOUND = "FP1007",
810
+ VARIABLE_NOT_DEFINED = "FP1008",
811
+ WRONG_ARGUMENT_COUNT = "FP2001",
812
+ WRONG_ARGUMENT_COUNT_RANGE = "FP2002",
813
+ SINGLETON_REQUIRED = "FP2003",
814
+ EMPTY_NOT_ALLOWED = "FP2004",
815
+ ARGUMENT_REQUIRED = "FP2005",
816
+ OPERATOR_TYPE_MISMATCH = "FP3002",
817
+ ARGUMENT_TYPE_MISMATCH = "FP3003",
818
+ CONVERSION_FAILED = "FP3004",
819
+ INVALID_VALUE_TYPE = "FP3005",
820
+ INVALID_OPERAND_TYPE = "FP3006",
821
+ STRING_OPERATION_ON_NON_STRING = "FP3007",
822
+ NUMERIC_OPERATION_ON_NON_NUMERIC = "FP3008",
823
+ BOOLEAN_OPERATION_ON_NON_BOOLEAN = "FP3009",
824
+ MODEL_PROVIDER_REQUIRED = "FP4001",
825
+ UNEXPECTED_TOKEN = "FP5001",
826
+ EXPECTED_TOKEN = "FP5002",
827
+ INVALID_SYNTAX = "FP5003",
828
+ EXPECTED_IDENTIFIER = "FP5004",
829
+ EXPECTED_TYPE_NAME = "FP5005",
830
+ DIVISION_BY_ZERO = "FP6001",
831
+ INVALID_DATE_TIME_FORMAT = "FP6002",
832
+ INCOMPATIBLE_UNITS = "FP6003",
833
+ INDEX_OUT_OF_BOUNDS = "FP6004",
834
+ INVALID_OPERATION = "FP6005",
835
+ INVALID_PRECISION = "FP6006",
836
+ INVALID_STRING_OPERATION = "FP6007",
837
+ INVALID_NUMERIC_OPERATION = "FP6008"
838
+ }
839
+
840
+ /**
841
+ * Kind of completion item
842
+ */
843
+ declare enum CompletionKind {
844
+ Property = "property",
845
+ Function = "function",
846
+ Variable = "variable",
847
+ Operator = "operator",
848
+ Type = "type",
849
+ Keyword = "keyword",
850
+ Constant = "constant"
851
+ }
852
+ /**
853
+ * Represents a completion item
854
+ */
855
+ interface CompletionItem {
856
+ /** Display text */
857
+ label: string;
858
+ /** Kind of completion */
859
+ kind: CompletionKind;
860
+ /** Short description */
861
+ detail?: string;
862
+ /** Full documentation */
863
+ documentation?: string;
864
+ /** Text to insert (if different from label) */
865
+ insertText?: string;
866
+ /** Sort order */
867
+ sortText?: string;
868
+ }
869
+ /**
870
+ * Options for completion provider
871
+ */
872
+ interface CompletionOptions {
873
+ /** Model provider for FHIR types */
874
+ modelProvider?: ModelProvider;
875
+ /** Variables in scope */
876
+ variables?: Record<string, any>;
877
+ /** Input type for the expression */
878
+ inputType?: TypeInfo;
879
+ /** Maximum number of completions to return */
880
+ maxCompletions?: number;
881
+ }
882
+ /**
883
+ * Provides context-aware completions for FHIRPath expressions
884
+ */
885
+ declare function provideCompletions(expression: string, cursorPosition: number, options?: CompletionOptions): Promise<CompletionItem[]>;
619
886
 
620
887
  interface EvaluateOptions {
621
888
  input?: unknown;
622
889
  variables?: Record<string, unknown>;
890
+ modelProvider?: ModelProvider;
891
+ inputType?: TypeInfo;
623
892
  }
624
- declare function evaluate(expression: string, options?: EvaluateOptions): any[];
893
+ declare function evaluate(expression: string, options?: EvaluateOptions): Promise<any[]>;
625
894
  declare function analyze(expression: string, options?: {
626
895
  variables?: Record<string, unknown>;
627
896
  modelProvider?: ModelProvider;
628
897
  inputType?: TypeInfo;
629
898
  errorRecovery?: boolean;
630
- }): AnalysisResult;
899
+ }): Promise<AnalysisResult>;
631
900
 
632
- export { type ASTNode, type AnalysisResult, Analyzer, type Diagnostic, DiagnosticSeverity, type EvaluateOptions, type FHIRModelContext, FHIRModelProvider, type FHIRModelProviderConfig, type FunctionDefinition, type InspectOptions, type InspectResult, Interpreter, type ModelProvider as ModelTypeProvider, type OperatorDefinition, type ParseResult, Parser, Registry, type TraceEntry, type TypeInfo, type TypeName, analyze, evaluate, inspect, parse, registry };
901
+ export { type ASTMetadata, type ASTNode, type AnalysisResult, Analyzer, type AnyCursorNode, type CompletionItem, CompletionKind, type CompletionOptions, type CursorArgumentNode, CursorContext, type CursorIdentifierNode, type CursorIndexNode, type CursorNode, type CursorOperatorNode, type CursorTypeNode, type Diagnostic, DiagnosticSeverity, ErrorCodes, Errors, type EvaluateOptions, type FHIRModelContext, FHIRModelProvider, type FHIRModelProviderConfig, FHIRPathError, type FunctionDefinition, type InspectOptions, type InspectResult, Interpreter, type ModelProvider as ModelTypeProvider, type OperatorDefinition, type ParseResult, Parser, Registry, type TypeInfo, type TypeName, analyze, evaluate, inspect, isCursorNode, parse, provideCompletions, registry };