@atomic-ehr/fhirpath 0.0.1 → 0.0.2-canary.3157434.20250804172319

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 (91) hide show
  1. package/README.md +2 -0
  2. package/dist/index.d.ts +59 -47
  3. package/dist/index.js +825 -489
  4. package/dist/index.js.map +1 -1
  5. package/package.json +1 -1
  6. package/src/boxing.ts +124 -0
  7. package/src/index.ts +3 -1
  8. package/src/interpreter.ts +85 -36
  9. package/src/operations/abs-function.ts +15 -7
  10. package/src/operations/aggregate-function.ts +1 -0
  11. package/src/operations/all-function.ts +15 -6
  12. package/src/operations/allFalse-function.ts +8 -6
  13. package/src/operations/allTrue-function.ts +8 -6
  14. package/src/operations/and-operator.ts +20 -11
  15. package/src/operations/anyFalse-function.ts +6 -4
  16. package/src/operations/anyTrue-function.ts +6 -4
  17. package/src/operations/as-operator.ts +1 -0
  18. package/src/operations/ceiling-function.ts +5 -2
  19. package/src/operations/combine-function.ts +1 -0
  20. package/src/operations/combine-operator.ts +4 -3
  21. package/src/operations/contains-function.ts +15 -4
  22. package/src/operations/contains-operator.ts +12 -5
  23. package/src/operations/count-function.ts +2 -1
  24. package/src/operations/defineVariable-function.ts +1 -0
  25. package/src/operations/distinct-function.ts +16 -4
  26. package/src/operations/div-operator.ts +15 -2
  27. package/src/operations/divide-operator.ts +10 -5
  28. package/src/operations/dot-operator.ts +1 -0
  29. package/src/operations/empty-function.ts +2 -1
  30. package/src/operations/endsWith-function.ts +15 -4
  31. package/src/operations/equal-operator.ts +14 -6
  32. package/src/operations/equivalent-operator.ts +15 -10
  33. package/src/operations/exclude-function.ts +9 -8
  34. package/src/operations/exists-function.ts +12 -6
  35. package/src/operations/first-function.ts +7 -4
  36. package/src/operations/floor-function.ts +5 -2
  37. package/src/operations/greater-operator.ts +9 -4
  38. package/src/operations/greater-or-equal-operator.ts +9 -4
  39. package/src/operations/iif-function.ts +7 -1
  40. package/src/operations/implies-operator.ts +22 -7
  41. package/src/operations/in-operator.ts +15 -6
  42. package/src/operations/indexOf-function.ts +15 -4
  43. package/src/operations/intersect-function.ts +14 -18
  44. package/src/operations/is-operator.ts +7 -6
  45. package/src/operations/isDistinct-function.ts +10 -5
  46. package/src/operations/join-function.ts +15 -4
  47. package/src/operations/last-function.ts +7 -4
  48. package/src/operations/length-function.ts +8 -2
  49. package/src/operations/less-operator.ts +9 -4
  50. package/src/operations/less-or-equal-operator.ts +9 -4
  51. package/src/operations/less-than.ts +25 -1
  52. package/src/operations/lower-function.ts +8 -2
  53. package/src/operations/minus-operator.ts +9 -4
  54. package/src/operations/mod-operator.ts +19 -2
  55. package/src/operations/multiply-operator.ts +11 -6
  56. package/src/operations/not-equal-operator.ts +10 -6
  57. package/src/operations/not-equivalent-operator.ts +15 -10
  58. package/src/operations/not-function.ts +12 -4
  59. package/src/operations/or-operator.ts +20 -11
  60. package/src/operations/plus-operator.ts +18 -6
  61. package/src/operations/power-function.ts +15 -4
  62. package/src/operations/replace-function.ts +21 -5
  63. package/src/operations/round-function.ts +17 -3
  64. package/src/operations/select-function.ts +9 -4
  65. package/src/operations/single-function.ts +2 -1
  66. package/src/operations/skip-function.ts +7 -1
  67. package/src/operations/split-function.ts +17 -3
  68. package/src/operations/sqrt-function.ts +5 -2
  69. package/src/operations/startsWith-function.ts +15 -4
  70. package/src/operations/subsetOf-function.ts +11 -9
  71. package/src/operations/substring-function.ts +29 -13
  72. package/src/operations/supersetOf-function.ts +11 -9
  73. package/src/operations/tail-function.ts +1 -0
  74. package/src/operations/take-function.ts +7 -1
  75. package/src/operations/toBoolean-function.ts +15 -9
  76. package/src/operations/toDecimal-function.ts +10 -4
  77. package/src/operations/toInteger-function.ts +10 -4
  78. package/src/operations/toString-function.ts +14 -8
  79. package/src/operations/trace-function.ts +7 -1
  80. package/src/operations/trim-function.ts +8 -2
  81. package/src/operations/truncate-function.ts +5 -2
  82. package/src/operations/unary-minus-operator.ts +22 -12
  83. package/src/operations/unary-plus-operator.ts +1 -0
  84. package/src/operations/union-function.ts +16 -22
  85. package/src/operations/union-operator.ts +1 -0
  86. package/src/operations/upper-function.ts +8 -2
  87. package/src/operations/where-function.ts +15 -8
  88. package/src/operations/xor-operator.ts +8 -3
  89. package/src/parser.ts +374 -5
  90. package/src/types.ts +6 -6
  91. package/src/parser-base.ts +0 -400
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # @atomic-ehr/fhirpath
2
2
 
3
+ [![npm version](https://badge.fury.io/js/@atomic-ehr%2Ffhirpath.svg)](https://www.npmjs.com/package/@atomic-ehr/fhirpath)
4
+
3
5
  A TypeScript implementation of FHIRPath, the path-based navigation and extraction language for FHIR (Fast Healthcare Interoperability Resources).
4
6
 
5
7
  ## Installation
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
  *
@@ -303,7 +335,7 @@ interface RuntimeContext {
303
335
  variables: Record<string, any>;
304
336
  }
305
337
  interface EvaluationResult {
306
- value: any[];
338
+ value: FHIRPathValue[];
307
339
  context: RuntimeContext;
308
340
  }
309
341
  declare enum DiagnosticSeverity {
@@ -346,51 +378,8 @@ interface ParseResult {
346
378
  availableCompletions: string[];
347
379
  };
348
380
  }
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
- }
381
+ type OperationEvaluator = (input: FHIRPathValue[], context: RuntimeContext, ...args: any[]) => EvaluationResult;
382
+ type FunctionEvaluator = (input: FHIRPathValue[], context: RuntimeContext, args: ASTNode[], evaluator: (node: ASTNode, input: FHIRPathValue[], context: RuntimeContext) => EvaluationResult) => EvaluationResult;
394
383
 
395
384
  interface ParserOptions {
396
385
  mode?: 'simple' | 'lsp';
@@ -401,7 +390,10 @@ interface ParserOptions {
401
390
  cursorPosition: number;
402
391
  };
403
392
  }
404
- declare class Parser extends BaseParser<ASTNode> {
393
+ declare class Parser {
394
+ protected lexer: Lexer;
395
+ protected tokens: Token[];
396
+ protected current: number;
405
397
  private mode;
406
398
  private options;
407
399
  private errors?;
@@ -419,6 +411,26 @@ declare class Parser extends BaseParser<ASTNode> {
419
411
  parse(): ParseResult;
420
412
  private parseSimple;
421
413
  private parseLSP;
414
+ protected expression(): ASTNode;
415
+ protected parseExpressionWithPrecedence(minPrecedence: number): ASTNode;
416
+ protected parsePrimary(): ASTNode;
417
+ protected parseInvocation(): ASTNode;
418
+ protected parseArgumentList(): ASTNode[];
419
+ protected parseCollectionElements(): ASTNode[];
420
+ protected parseTypeName(): string;
421
+ protected parseStringValue(raw: string): string;
422
+ protected parseIdentifierValue(raw: string): string;
423
+ protected isFunctionCall(node: ASTNode): boolean;
424
+ protected isBinaryOperatorToken(token: Token): boolean;
425
+ protected isKeywordAllowedAsMember(token: Token): boolean;
426
+ protected isKeywordAllowedAsIdentifier(token: Token): boolean;
427
+ protected peek(): Token;
428
+ protected previous(): Token;
429
+ protected isAtEnd(): boolean;
430
+ protected advance(): Token;
431
+ protected check(type: TokenType): boolean;
432
+ protected match(...types: TokenType[]): boolean;
433
+ protected consume(type: TokenType, message: string): Token;
422
434
  protected createIdentifierNode(name: string, token: Token): ASTNode;
423
435
  protected createLiteralNode(value: any, valueType: LiteralNode['valueType'], token: Token): LiteralNode;
424
436
  protected createBinaryNode(token: Token, left: ASTNode, right: ASTNode): BinaryNode;