@atomic-ehr/fhirpath 0.0.1-canary.0c6931e.20250727185306

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 (85) hide show
  1. package/README.md +473 -0
  2. package/dist/index.d.ts +462 -0
  3. package/dist/index.js +10307 -0
  4. package/dist/index.js.map +1 -0
  5. package/package.json +58 -0
  6. package/src/analyzer/analyzer.ts +499 -0
  7. package/src/analyzer/model-provider.ts +244 -0
  8. package/src/analyzer/schemas/index.ts +2 -0
  9. package/src/analyzer/schemas/types.ts +40 -0
  10. package/src/analyzer/types.ts +142 -0
  11. package/src/api/builder.ts +157 -0
  12. package/src/api/errors.ts +145 -0
  13. package/src/api/expression.ts +156 -0
  14. package/src/api/index.ts +122 -0
  15. package/src/api/inspect.ts +99 -0
  16. package/src/api/registry.ts +128 -0
  17. package/src/api/types.ts +210 -0
  18. package/src/compiler/compiler.ts +546 -0
  19. package/src/compiler/index.ts +2 -0
  20. package/src/compiler/prototype-context-adapter.ts +99 -0
  21. package/src/compiler/types.ts +24 -0
  22. package/src/index.ts +107 -0
  23. package/src/interpreter/README.md +78 -0
  24. package/src/interpreter/interpreter.ts +475 -0
  25. package/src/interpreter/types.ts +108 -0
  26. package/src/lexer/char-tables.ts +37 -0
  27. package/src/lexer/errors.ts +31 -0
  28. package/src/lexer/index.ts +5 -0
  29. package/src/lexer/lexer.ts +745 -0
  30. package/src/lexer/token.ts +104 -0
  31. package/src/lexer2/index.md +232 -0
  32. package/src/lexer2/index.perf.test.ts +68 -0
  33. package/src/lexer2/index.test.ts +549 -0
  34. package/src/lexer2/index.ts +1251 -0
  35. package/src/lexer2/notes.md +173 -0
  36. package/src/lexer2/optimization-summary.md +718 -0
  37. package/src/parser/ast-factory.ts +220 -0
  38. package/src/parser/ast.ts +144 -0
  39. package/src/parser/collection-parser.ts +89 -0
  40. package/src/parser/diagnostic-messages.ts +216 -0
  41. package/src/parser/diagnostics.ts +85 -0
  42. package/src/parser/error-reporter.ts +230 -0
  43. package/src/parser/index.ts +3 -0
  44. package/src/parser/literal-parser.ts +103 -0
  45. package/src/parser/parse-error.ts +16 -0
  46. package/src/parser/parser-error-factory.ts +141 -0
  47. package/src/parser/parser-state.ts +134 -0
  48. package/src/parser/parser.ts +1272 -0
  49. package/src/parser/pprint.ts +169 -0
  50. package/src/parser/precedence-manager.ts +64 -0
  51. package/src/parser/source-mapper.ts +248 -0
  52. package/src/parser/special-constructs.ts +142 -0
  53. package/src/parser/token-navigator.ts +110 -0
  54. package/src/parser/types.ts +60 -0
  55. package/src/parser2/index.md +177 -0
  56. package/src/parser2/index.perf.test.ts +184 -0
  57. package/src/parser2/index.test.ts +305 -0
  58. package/src/parser2/index.ts +578 -0
  59. package/src/parser2/optimization-summary.md +176 -0
  60. package/src/registry/default-analyzers.ts +257 -0
  61. package/src/registry/default-compilers.ts +31 -0
  62. package/src/registry/index.ts +96 -0
  63. package/src/registry/operations/arithmetic.ts +506 -0
  64. package/src/registry/operations/collection.ts +425 -0
  65. package/src/registry/operations/comparison.ts +432 -0
  66. package/src/registry/operations/existence.ts +703 -0
  67. package/src/registry/operations/filtering.ts +358 -0
  68. package/src/registry/operations/literals.ts +341 -0
  69. package/src/registry/operations/logical.ts +439 -0
  70. package/src/registry/operations/math.ts +128 -0
  71. package/src/registry/operations/membership.ts +132 -0
  72. package/src/registry/operations/navigation.ts +52 -0
  73. package/src/registry/operations/string.ts +507 -0
  74. package/src/registry/operations/subsetting.ts +174 -0
  75. package/src/registry/operations/type-checking.ts +162 -0
  76. package/src/registry/operations/type-conversion.ts +404 -0
  77. package/src/registry/operations/type-operators.ts +308 -0
  78. package/src/registry/operations/utility.ts +644 -0
  79. package/src/registry/registry.ts +146 -0
  80. package/src/registry/types.ts +161 -0
  81. package/src/registry/utils/evaluation-helpers.ts +93 -0
  82. package/src/registry/utils/index.ts +3 -0
  83. package/src/registry/utils/type-system.ts +173 -0
  84. package/src/runtime/context.ts +158 -0
  85. package/src/runtime/debug-context.ts +135 -0
@@ -0,0 +1,462 @@
1
+ declare enum TokenType {
2
+ LITERAL = "LITERAL",// Generic literal token for registry-based literals
3
+ NULL = "NULL",// {} (nullLiteral in grammar)
4
+ TRUE = "TRUE",// true
5
+ FALSE = "FALSE",// false
6
+ STRING = "STRING",// 'string value'
7
+ NUMBER = "NUMBER",// 123, 45.67, 0123 (allows leading zeros)
8
+ DATE = "DATE",// @2024, @2024-01, @2024-01-15
9
+ DATETIME = "DATETIME",// @2024-01-15T10:30:00Z
10
+ TIME = "TIME",// @T14:30:00
11
+ IDENTIFIER = "IDENTIFIER",// [A-Za-z_][A-Za-z0-9_]*
12
+ DELIMITED_IDENTIFIER = "DELIMITED_IDENTIFIER",// `identifier`
13
+ THIS = "THIS",// $this
14
+ INDEX = "INDEX",// $index
15
+ TOTAL = "TOTAL",// $total
16
+ ENV_VAR = "ENV_VAR",// %context, %`vs-name`
17
+ DOT = "DOT",// .
18
+ LBRACKET = "LBRACKET",// [
19
+ RBRACKET = "RBRACKET",// ]
20
+ LPAREN = "LPAREN",// (
21
+ RPAREN = "RPAREN",// )
22
+ PLUS = "PLUS",// +
23
+ MINUS = "MINUS",// -
24
+ STAR = "STAR",// *
25
+ SLASH = "SLASH",// /
26
+ DIV = "DIV",// div
27
+ MOD = "MOD",// mod
28
+ CONCAT = "CONCAT",// &
29
+ IS = "IS",// is
30
+ AS = "AS",// as
31
+ PIPE = "PIPE",// |
32
+ LT = "LT",// <
33
+ LTE = "LTE",// <=
34
+ GT = "GT",// >
35
+ GTE = "GTE",// >=
36
+ EQ = "EQ",// =
37
+ NEQ = "NEQ",// !=
38
+ EQUIV = "EQUIV",// ~
39
+ NEQUIV = "NEQUIV",// !~
40
+ IN = "IN",// in
41
+ CONTAINS = "CONTAINS",// contains
42
+ AND = "AND",// and
43
+ OR = "OR",// or
44
+ XOR = "XOR",// xor
45
+ IMPLIES = "IMPLIES",// implies
46
+ NOT = "NOT",// not
47
+ LBRACE = "LBRACE",// {
48
+ RBRACE = "RBRACE",// }
49
+ COMMA = "COMMA",// ,
50
+ EOF = "EOF",
51
+ UNIT = "UNIT",// year, month, 'mg', etc.
52
+ WS = "WS",// Whitespace
53
+ COMMENT = "COMMENT",// /* Multi-line comment */
54
+ LINE_COMMENT = "LINE_COMMENT"
55
+ }
56
+ interface Position$2 {
57
+ line: number;
58
+ column: number;
59
+ offset: number;
60
+ }
61
+ declare enum Channel {
62
+ DEFAULT = 0,
63
+ HIDDEN = 1
64
+ }
65
+ interface Token {
66
+ type: TokenType;
67
+ value: string;
68
+ position: Position$2;
69
+ channel?: Channel;
70
+ operation?: any;
71
+ literalValue?: any;
72
+ }
73
+
74
+ declare enum ErrorCode {
75
+ PARSE_ERROR = "PARSE_ERROR",
76
+ SYNTAX_ERROR = "SYNTAX_ERROR",
77
+ UNEXPECTED_TOKEN = "UNEXPECTED_TOKEN",
78
+ UNTERMINATED_STRING = "UNTERMINATED_STRING",
79
+ INVALID_ESCAPE = "INVALID_ESCAPE",
80
+ UNCLOSED_PARENTHESIS = "UNCLOSED_PARENTHESIS",
81
+ UNCLOSED_BRACKET = "UNCLOSED_BRACKET",
82
+ UNCLOSED_BRACE = "UNCLOSED_BRACE",
83
+ MISSING_ARGUMENTS = "MISSING_ARGUMENTS",
84
+ INVALID_OPERATOR = "INVALID_OPERATOR",
85
+ EXPECTED_EXPRESSION = "EXPECTED_EXPRESSION",
86
+ EXPECTED_IDENTIFIER = "EXPECTED_IDENTIFIER",
87
+ MULTIPLE_ERRORS = "MULTIPLE_ERRORS",
88
+ INVALID_CHARACTER = "INVALID_CHARACTER",
89
+ TYPE_ERROR = "TYPE_ERROR",
90
+ TYPE_MISMATCH = "TYPE_MISMATCH",
91
+ UNKNOWN_TYPE = "UNKNOWN_TYPE",
92
+ CARDINALITY_ERROR = "CARDINALITY_ERROR",
93
+ RUNTIME_ERROR = "RUNTIME_ERROR",
94
+ UNDEFINED_VARIABLE = "UNDEFINED_VARIABLE",
95
+ UNDEFINED_FUNCTION = "UNDEFINED_FUNCTION",
96
+ INVALID_ARGUMENT = "INVALID_ARGUMENT",
97
+ DIVISION_BY_ZERO = "DIVISION_BY_ZERO",
98
+ ANALYSIS_ERROR = "ANALYSIS_ERROR",
99
+ UNREACHABLE_CODE = "UNREACHABLE_CODE",
100
+ AMBIGUOUS_TYPE = "AMBIGUOUS_TYPE"
101
+ }
102
+ declare class FHIRPathError extends Error {
103
+ code: ErrorCode;
104
+ location?: Location | undefined;
105
+ expression?: string | undefined;
106
+ constructor(message: string, code: ErrorCode, location?: Location | undefined, expression?: string | undefined);
107
+ toString(): string;
108
+ }
109
+
110
+ interface ParserOptions {
111
+ maxErrors?: number;
112
+ throwOnError?: boolean;
113
+ trackRanges?: boolean;
114
+ errorRecovery?: boolean;
115
+ }
116
+ interface ParseResult {
117
+ ast: ASTNode;
118
+ diagnostics: ParseDiagnostic[];
119
+ hasErrors: boolean;
120
+ isPartial?: boolean;
121
+ ranges?: Map<ASTNode, TextRange>;
122
+ }
123
+ interface ParseDiagnostic {
124
+ range: TextRange;
125
+ severity: DiagnosticSeverity;
126
+ code: ErrorCode;
127
+ message: string;
128
+ source: 'fhirpath-parser';
129
+ relatedInformation?: RelatedInformation[];
130
+ }
131
+ declare enum DiagnosticSeverity {
132
+ Error = 1,
133
+ Warning = 2,
134
+ Information = 3,
135
+ Hint = 4
136
+ }
137
+ interface TextRange {
138
+ start: Position$1;
139
+ end: Position$1;
140
+ }
141
+ interface Position$1 {
142
+ line: number;
143
+ character: number;
144
+ offset: number;
145
+ }
146
+ interface RelatedInformation {
147
+ location: TextRange;
148
+ message: string;
149
+ }
150
+
151
+ interface ASTNode {
152
+ type: NodeType;
153
+ position: Position;
154
+ range?: TextRange;
155
+ resultType?: unknown;
156
+ isSingleton?: boolean;
157
+ }
158
+ interface Position {
159
+ line: number;
160
+ column: number;
161
+ offset: number;
162
+ }
163
+ declare enum NodeType {
164
+ Identifier = 0,
165
+ TypeOrIdentifier = 1,// Uppercase identifiers that could be types (Patient, Observation)
166
+ Binary = 2,// All binary operators including dot
167
+ Unary = 3,// unary +, -, not
168
+ Union = 4,// | operator (special handling for multiple operands)
169
+ Function = 5,// Function calls
170
+ Literal = 6,// numbers, strings, booleans, dates, null
171
+ Variable = 7,// $this, $index, $total, %var
172
+ Collection = 8,// {} empty collection or {expr1, expr2, ...}
173
+ MembershipTest = 9,// 'is' operator
174
+ TypeCast = 10,// 'as' operator
175
+ TypeReference = 11,// Type name in ofType()
176
+ Index = 12,// [] indexing
177
+ Error = "Error",// Error recovery node
178
+ Incomplete = "Incomplete"
179
+ }
180
+ interface IdentifierNode extends ASTNode {
181
+ type: NodeType.Identifier;
182
+ name: string;
183
+ }
184
+ interface LiteralNode extends ASTNode {
185
+ type: NodeType.Literal;
186
+ value: any;
187
+ valueType: 'string' | 'number' | 'boolean' | 'date' | 'time' | 'datetime' | 'null';
188
+ raw?: string;
189
+ operation?: any;
190
+ }
191
+ interface BinaryNode extends ASTNode {
192
+ type: NodeType.Binary;
193
+ operator: TokenType;
194
+ operation?: any;
195
+ left: ASTNode;
196
+ right: ASTNode;
197
+ }
198
+ interface UnaryNode extends ASTNode {
199
+ type: NodeType.Unary;
200
+ operator: TokenType;
201
+ operation?: any;
202
+ operand: ASTNode;
203
+ }
204
+ interface FunctionNode extends ASTNode {
205
+ type: NodeType.Function;
206
+ name: ASTNode;
207
+ arguments: ASTNode[];
208
+ operation?: any;
209
+ }
210
+ interface VariableNode extends ASTNode {
211
+ type: NodeType.Variable;
212
+ name: string;
213
+ }
214
+ interface IndexNode extends ASTNode {
215
+ type: NodeType.Index;
216
+ expression: ASTNode;
217
+ index: ASTNode;
218
+ }
219
+ interface UnionNode extends ASTNode {
220
+ type: NodeType.Union;
221
+ operands: ASTNode[];
222
+ }
223
+ interface MembershipTestNode extends ASTNode {
224
+ type: NodeType.MembershipTest;
225
+ expression: ASTNode;
226
+ targetType: string;
227
+ }
228
+ interface TypeCastNode extends ASTNode {
229
+ type: NodeType.TypeCast;
230
+ expression: ASTNode;
231
+ targetType: string;
232
+ }
233
+ interface CollectionNode extends ASTNode {
234
+ type: NodeType.Collection;
235
+ elements: ASTNode[];
236
+ }
237
+ interface TypeReferenceNode extends ASTNode {
238
+ type: NodeType.TypeReference;
239
+ typeName: string;
240
+ }
241
+
242
+ /**
243
+ * Type system interfaces for FHIRPath type analysis
244
+ *
245
+ * Uses opaque type references to allow flexible implementation
246
+ */
247
+ type TypeRef = unknown;
248
+
249
+ /**
250
+ * Unified runtime context that works with both interpreter and compiler.
251
+ * Uses prototype-based inheritance for efficient context copying.
252
+ *
253
+ * Variable Storage Convention:
254
+ * - Special variables: $this, $index, $total (prefixed with $)
255
+ * - Environment variables: %context, %resource, %rootResource (stored with % prefix)
256
+ * - User-defined variables: stored with % prefix (e.g., %x, %y)
257
+ */
258
+ interface RuntimeContext {
259
+ input: any[];
260
+ focus: any[];
261
+ variables: Record<string, any>;
262
+ }
263
+
264
+ interface FHIRPathExpression {
265
+ readonly ast: ASTNode;
266
+ evaluate(input?: any, context?: EvaluationContext): any[];
267
+ compile(options?: CompileOptions): CompiledExpression;
268
+ analyze(options?: AnalyzeOptions): AnalysisResult;
269
+ toString(): string;
270
+ }
271
+ interface CompiledExpression {
272
+ (input?: any, context?: EvaluationContext): any[];
273
+ readonly source: string;
274
+ }
275
+ interface EvaluationContext {
276
+ variables?: Record<string, any>;
277
+ environment?: Record<string, any>;
278
+ modelProvider?: ModelProvider;
279
+ customFunctions?: CustomFunctionMap;
280
+ }
281
+ type CustomFunction = (context: RuntimeContext, input: any[], ...args: any[]) => any[];
282
+ type CustomFunctionMap = Record<string, CustomFunction>;
283
+ interface ModelProvider {
284
+ resolveType(typeName: string): TypeRef | undefined;
285
+ getTypeHierarchy(typeName: string): string[];
286
+ getProperties(typeName: string): PropertyDefinition[];
287
+ getTypeName?(type: TypeRef): string;
288
+ }
289
+ interface PropertyDefinition {
290
+ name: string;
291
+ type: string;
292
+ isCollection: boolean;
293
+ isRequired: boolean;
294
+ }
295
+ interface CompileOptions {
296
+ optimize?: boolean;
297
+ sourceMap?: boolean;
298
+ }
299
+ interface AnalyzeOptions {
300
+ modelProvider?: ModelProvider;
301
+ strict?: boolean;
302
+ }
303
+ interface AnalysisResult {
304
+ type: TypeRef;
305
+ isSingleton: boolean;
306
+ errors: AnalysisError[];
307
+ warnings: AnalysisWarning[];
308
+ }
309
+ interface AnalysisError {
310
+ message: string;
311
+ location?: Location;
312
+ code: string;
313
+ }
314
+ interface AnalysisWarning {
315
+ message: string;
316
+ location?: Location;
317
+ code: string;
318
+ }
319
+ interface Location {
320
+ line: number;
321
+ column: number;
322
+ offset: number;
323
+ length: number;
324
+ }
325
+ interface RegistryAPI {
326
+ listFunctions(): OperationMetadata[];
327
+ listOperators(): OperationMetadata[];
328
+ listAllOperations(): OperationMetadata[];
329
+ hasOperation(name: string): boolean;
330
+ hasFunction(name: string): boolean;
331
+ hasOperator(symbol: string): boolean;
332
+ getOperationInfo(name: string): OperationInfo | undefined;
333
+ canRegisterFunction(name: string): boolean;
334
+ }
335
+ interface OperationMetadata {
336
+ name: string;
337
+ kind: 'function' | 'operator' | 'literal';
338
+ syntax: {
339
+ notation: string;
340
+ };
341
+ }
342
+ interface OperationInfo extends OperationMetadata {
343
+ signature: {
344
+ input?: {
345
+ types?: string[];
346
+ cardinality?: 'singleton' | 'collection' | 'any';
347
+ };
348
+ parameters?: Array<{
349
+ name: string;
350
+ types?: string[];
351
+ cardinality?: 'singleton' | 'collection' | 'any';
352
+ optional?: boolean;
353
+ }>;
354
+ output?: {
355
+ type?: string | 'dynamic';
356
+ cardinality?: 'singleton' | 'collection' | 'preserve-input';
357
+ };
358
+ };
359
+ description?: string;
360
+ examples?: string[];
361
+ }
362
+ interface FHIRPathBuilder {
363
+ withModelProvider(provider: ModelProvider): this;
364
+ withCustomFunction(name: string, fn: CustomFunction): this;
365
+ withVariable(name: string, value: any): this;
366
+ build(): FHIRPathAPI;
367
+ }
368
+ interface FHIRPathAPI {
369
+ parse(expression: string): FHIRPathExpression;
370
+ evaluate(expression: string | FHIRPathExpression, input?: any): any[];
371
+ compile(expression: string | FHIRPathExpression): CompiledExpression;
372
+ analyze(expression: string | FHIRPathExpression): AnalysisResult;
373
+ inspect(expression: string | FHIRPathExpression, input?: any, context?: EvaluationContext): InspectResult;
374
+ registry: RegistryAPI;
375
+ }
376
+ interface InspectResult {
377
+ result: any[];
378
+ expression: string;
379
+ ast: ASTNode;
380
+ executionTime: number;
381
+ traces: TraceEntry[];
382
+ evaluationSteps?: EvaluationStep[];
383
+ errors?: ErrorInfo[];
384
+ warnings?: WarningInfo[];
385
+ }
386
+ interface TraceEntry {
387
+ name: string;
388
+ values: any[];
389
+ timestamp: number;
390
+ location?: SourceLocation;
391
+ depth: number;
392
+ }
393
+ interface EvaluationStep {
394
+ nodeType: string;
395
+ expression: string;
396
+ input: any[];
397
+ output: any[];
398
+ variables: Record<string, any>;
399
+ timestamp: number;
400
+ duration: number;
401
+ }
402
+ interface SourceLocation {
403
+ line: number;
404
+ column: number;
405
+ offset: number;
406
+ }
407
+ interface ErrorInfo {
408
+ message: string;
409
+ type: string;
410
+ location?: SourceLocation;
411
+ stack?: string;
412
+ }
413
+ interface WarningInfo {
414
+ message: string;
415
+ code: string;
416
+ location?: SourceLocation;
417
+ }
418
+
419
+ declare class PublicRegistryAPI implements RegistryAPI {
420
+ listFunctions(): OperationMetadata[];
421
+ listOperators(): OperationMetadata[];
422
+ listAllOperations(): OperationMetadata[];
423
+ hasOperation(name: string): boolean;
424
+ hasFunction(name: string): boolean;
425
+ hasOperator(symbol: string): boolean;
426
+ getOperationInfo(name: string): OperationInfo | undefined;
427
+ canRegisterFunction(name: string): boolean;
428
+ private toMetadata;
429
+ private toOperationInfo;
430
+ private extractTypes;
431
+ }
432
+
433
+ declare function parse(expression: string, options?: ParserOptions): ParseResult;
434
+ declare function parseForEvaluation(expression: string): ASTNode;
435
+ declare function isStandardResult(result: ParseResult): result is ParseResult;
436
+ declare function isDiagnosticResult(result: ParseResult): result is ParseResult & {
437
+ isPartial: boolean;
438
+ ranges: Map<ASTNode, TextRange>;
439
+ };
440
+ declare function validate(expression: string): {
441
+ valid: boolean;
442
+ diagnostics: ParseDiagnostic[];
443
+ };
444
+ declare function evaluate(expression: string | FHIRPathExpression, input?: any, context?: EvaluationContext): any[];
445
+ declare function compile(expression: string | FHIRPathExpression, options?: CompileOptions): CompiledExpression;
446
+ declare function analyze(expression: string | FHIRPathExpression, options?: AnalyzeOptions): AnalysisResult;
447
+ declare const registry: PublicRegistryAPI;
448
+
449
+ declare class FHIRPath {
450
+ static builder(): FHIRPathBuilder;
451
+ }
452
+
453
+ declare const _default: {
454
+ parse: typeof parse;
455
+ parseForEvaluation: typeof parseForEvaluation;
456
+ evaluate: typeof evaluate;
457
+ compile: typeof compile;
458
+ analyze: typeof analyze;
459
+ registry: PublicRegistryAPI;
460
+ };
461
+
462
+ export { type ASTNode, type AnalysisError, type AnalysisResult, type AnalysisWarning, type AnalyzeOptions, type BinaryNode, type CollectionNode, type CompileOptions, type CompiledExpression, type CustomFunction, type CustomFunctionMap, DiagnosticSeverity, ErrorCode, type EvaluationContext, FHIRPath, type FHIRPathAPI, type FHIRPathBuilder, FHIRPathError, type FHIRPathExpression, type FunctionNode, type IdentifierNode, type IndexNode, type LiteralNode, type Location, type MembershipTestNode, type ModelProvider, NodeType, type OperationInfo, type OperationMetadata, type ParseDiagnostic, type ParseResult, type ParserOptions, type Position, type PropertyDefinition, type RegistryAPI, type Position$1 as TextPosition, type TextRange, type Token, TokenType, type TypeCastNode, type TypeReferenceNode, type UnaryNode, type UnionNode, type VariableNode, analyze, compile, _default as default, evaluate, isDiagnosticResult, isStandardResult, parse, parseForEvaluation, registry, validate };