FHIRPath Parser & Lexer¶
FHIRPath lexer and parser components.
parser ¶
FHIRPath parser implementation using PLY (Python Lex-Yacc).
Classes:
| Name | Description |
|---|---|
FHIRPathParser |
An LALR-parser for FHIRPath with LRU cache for parsed expressions. |
IteratorToTokenStream |
A wrapper class that converts an iterator of tokens into a token stream compatible with PLY's parser. |
FHIRPathParser
Path: fhircraft.fhir.path.parser.FHIRPathParser
An LALR-parser for FHIRPath with LRU cache for parsed expressions.
The parser uses an OrderedDict-based LRU (Least Recently Used) cache to store parsed expressions. The cache has a configurable size limit (default: 1000 expressions) to prevent unbounded memory growth. When the cache reaches its limit, the least recently used expression is evicted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
debug
|
bool
|
Whether to enable debug output for the parser. Defaults to False. |
False
|
lexer_class
|
type
|
Custom lexer class to use. Defaults to FHIRPathLexer. |
None
|
cache_limit
|
int
|
Maximum number of expressions to cache. Defaults to 1000. When the cache exceeds this limit, the least recently used expression is evicted. |
1000
|
Raises:
| Type | Description |
|---|---|
FHIRPathParsingError
|
If docstrings have been removed (required by PLY). |
Methods:
| Name | Description |
|---|---|
parse |
Parse a FHIRPath expression string and return the corresponding FHIRPath object. |
is_valid |
Check if a FHIRPath expression string is valid without raising exceptions. |
parse_token_stream |
Parse a stream of tokens and return the corresponding FHIRPath object. |
p_fhirpath_term_expression |
expression : term |
p_fhirpath_invocation_expression |
expression : expression '.' invocation |
p_fhirpath_indexer_expression |
expression : expression '[' expression ']' |
p_fhirpath_multiplicative_operation |
expression : expression '*' expression |
p_fhirpath_additive_operation |
expression : expression '+' expression |
p_fhirpath_type_operation |
expression : expression IS type_specifier |
p_fhirpath_union_operation |
expression : expression '|' expression |
p_fhirpath_equality_operation |
expression : expression EQUAL expression |
p_fhirpath_inequality_operation |
expression : expression GREATER_THAN expression |
p_fhirpath_membership_fhirpath_operation |
expression : expression IN expression |
p_fhirpath_and_operation |
expression : expression AND expression |
p_fhirpath_or_operation |
expression : expression OR expression |
p_fhirpath_implies_operation |
expression : expression IMPLIES expression |
p_fhirpath_term |
term : invocation |
p_fhirpath_parenthesized_expression |
parenthesized_expression : '(' expression ')' |
p_fhirpath_invocation |
invocation : element |
p_fhirpath_root |
root : ROOT_NODE |
p_fhirpath_element |
element : identifier |
p_fhirpath_typechoice_invocation |
type_choice : CHOICE_ELEMENT |
p_fhirpath_constant |
constant : ENVIRONMENTAL_VARIABLE |
p_fhirpath_contextual |
contextual : CONTEXTUAL_OPERATOR |
p_fhirpath_type_specifier |
type_specifier : identifier |
p_fhirpath_type_specifier_context |
type_specifier : identifier '.' identifier |
p_fhirpath_function |
function : function_name '(' arguments ')' |
p_fhirpath_type_function |
function : OFTYPE '(' type_specifier ')' |
p_fhirpath_function_name |
function_name : identifier |
p_fhirpath_function_arguments |
arguments : arguments ',' argument |
p_fhirpath_function_argument |
argument : expression |
p_fhirpath_identifier |
identifier : IDENTIFIER |
p_fhirpath_polar_literal |
literal : '+' number |
p_fhirpath_literal |
literal : number |
p_fhirpath_literal_empty |
literal : '{' '}' |
p_fhirpath_boolean |
boolean : BOOLEAN |
p_fhirpath_datetime |
datetime : DATETIME |
p_fhirpath_time |
time : TIME |
p_fhirpath_date |
date : DATE |
p_fhirpath_quantity |
quantity : number unit |
p_fhirpath_unit |
unit : STRING |
p_fhirpath_number |
number : INTEGER |
p_fhirpath_empty |
empty : |
Attributes:
| Name | Type | Description |
|---|---|---|
tokens |
The list of token names used by the parser. This is required by PLY. |
|
input_string |
str
|
The input string being parsed. Used for error reporting. |
debug |
bool
|
Whether to enable debug output for the parser. |
cache_limit |
int
|
Maximum number of expressions to cache. Oldest (least recently used) are evicted when limit is reached. |
lexer_class |
FHIRPathLexer | type
|
The lexer class used by the parser. Defaults to FHIRPathLexer. |
lexer |
FHIRPathLexer | None
|
The lexer instance used by the parser. |
lr_parser |
LRParser | None
|
The LALR parser instance generated by PLY. |
cache |
OrderedDict[str, FHIRPath]
|
A cache of parsed FHIRPath expressions to avoid re-parsing the same expression. Uses LRU eviction policy. |
cache_info |
dict
|
Return cache statistics including size, limit, and stored expression strings. |
tokens
class-attribute
instance-attribute
¶
tokens = FHIRPathLexer.tokens
The list of token names used by the parser. This is required by PLY.
input_string
instance-attribute
¶
input_string: str
The input string being parsed. Used for error reporting.
debug
class-attribute
instance-attribute
¶
debug: bool = debug
Whether to enable debug output for the parser.
cache_limit
class-attribute
instance-attribute
¶
cache_limit: int = cache_limit
Maximum number of expressions to cache. Oldest (least recently used) are evicted when limit is reached.
lexer_class
class-attribute
instance-attribute
¶
lexer_class: FHIRPathLexer | type = lexer_class or FHIRPathLexer
The lexer class used by the parser. Defaults to FHIRPathLexer.
lexer
class-attribute
instance-attribute
¶
lexer: FHIRPathLexer | None = self.lexer_class()
The lexer instance used by the parser.
lr_parser
class-attribute
instance-attribute
¶
lr_parser: LRParser | None = ply.yacc.yacc(module=self, debug=self.debug, tabmodule=parsing_table_module, outputdir=output_directory, write_tables=False, start=start_symbol, errorlog=logger)
The LALR parser instance generated by PLY.
cache
class-attribute
instance-attribute
¶
cache: OrderedDict[str, FHIRPath] = OrderedDict()
A cache of parsed FHIRPath expressions to avoid re-parsing the same expression. Uses LRU eviction policy.
cache_info
property
¶
cache_info: dict
Return cache statistics including size, limit, and stored expression strings.
parse ¶
Parse a FHIRPath expression string and return the corresponding FHIRPath object.
Results are cached using an LRU policy. Cached expressions are returned immediately
without re-parsing. Accessing a cached expression updates its position in the LRU
queue to mark it as recently used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_string
|
str
|
The FHIRPath expression string to parse. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
FHIRPath |
FHIRPath | Any
|
The parsed FHIRPath object representing the expression. |
Raises:
| Type | Description |
|---|---|
FHIRPathParsingError
|
If there is a parsing error in the input string. |
is_valid ¶
parse_token_stream ¶
p_fhirpath_invocation_expression ¶
expression : expression '.' invocation
p_fhirpath_indexer_expression ¶
expression : expression '[' expression ']'
p_fhirpath_multiplicative_operation ¶
expression : expression '*' expression | expression '/' expression | expression DIV expression | expression MOD expression
p_fhirpath_additive_operation ¶
expression : expression '+' expression | expression '-' expression | expression '&' expression
p_fhirpath_type_operation ¶
expression : expression IS type_specifier | expression AS type_specifier
p_fhirpath_equality_operation ¶
expression : expression EQUAL expression | expression EQUIVALENT expression | expression NOT_EQUAL expression | expression NOT_EQUIVALENT expression
p_fhirpath_inequality_operation ¶
expression : expression GREATER_THAN expression | expression GREATER_EQUAL_THAN expression | expression LESS_THAN expression | expression LESS_EQUAL_THAN expression
p_fhirpath_membership_fhirpath_operation ¶
expression : expression IN expression | expression CONTAINS expression
p_fhirpath_or_operation ¶
expression : expression OR expression | expression XOR expression
p_fhirpath_implies_operation ¶
expression : expression IMPLIES expression
p_fhirpath_term ¶
term : invocation | literal | constant | parenthesized_expression
p_fhirpath_parenthesized_expression ¶
parenthesized_expression : '(' expression ')'
p_fhirpath_invocation ¶
invocation : element | root | type_choice | function | contextual
p_fhirpath_type_specifier_context ¶
identifier '.' identifier
| identifier '.' ROOT_NODE
p_fhirpath_type_function ¶
function : OFTYPE '(' type_specifier ')' | IS '(' type_specifier ')' | AS '(' type_specifier ')'
p_fhirpath_literal ¶
literal : number | boolean | STRING | date | time | datetime | quantity
lexer ¶
Classes:
| Name | Description |
|---|---|
FHIRPathLexer |
A Lexical analyzer for JsonPath. |
FHIRPathLexer
Path: fhircraft.fhir.path.lexer.FHIRPathLexer
A Lexical analyzer for JsonPath.
Methods:
| Name | Description |
|---|---|
tokenize |
Maps a string to an iterator over tokens. In other words: [char] -> [token] |
t_ignore_WHITESPACE |
[\s] |
t_ignore_COMMENT |
\/*([\s\S]?)*\/|\/\/(.) |
t_CHOICE_ELEMENT |
(?:`[a-zA-Z][a-zA-Z0-9-][^`][x]`)|(?:(?:|[a-zA-Z])[a-zA-Z0-9][x]) |
t_ENVIRONMENTAL_VARIABLE |
(?:\%[a-zA-Z][a-zA-Z0-9-]|\%`[a-zA-Z][a-zA-Z0-9-][^`]`) |
t_CONTEXTUAL_OPERATOR |
$(\w*)? |
t_DATETIME |
@\d{4}(?:-\d{2}(?:-\d{2})?)?T(?:\d{2}(?::\d{2}(?::\d{2}(?:.\d{3}(?:[+|-]\d{2}(?::\d{2})?)?)?)?)?)? |
t_DATE |
@\d{4}(?:-\d{2}(?:-\d{2})?)? |
t_TIME |
\@T\d{2}(?::\d{2}(?::\d{2}(?:.\d{3}(?:[+|-]\d{2}(?::\d{2})?)?)?)?)? |
t_NUMBER |
\d+(.\d+)? |
t_STRING |
\'((?:[^\'\]|\.)*)\' |
t_IDENTIFIER |
(?:`[a-zA-Z][a-zA-Z0-9-][^`]`)|(?:(?:|[a-zA-Z])[a-zA-Z0-9]) |
t_NOT_EQUAL |
!= |
t_NOT_EQUIVALENT |
!~ |
t_GREATER_EQUAL_THAN |
|
t_LESS_EQUAL_THAN |
<= |
t_LESS_THAN |
< |
t_GREATER_THAN |
|
t_EQUAL |
= |
t_EQUIVALENT |
~ |
t_error_invalid_function |
[a-zA-Z][a-zA-Z_0-9]*\((?:.*)?\) |
t_error_doublequote_string |
\"([^\"]*)?\" |
tokenize ¶
Maps a string to an iterator over tokens. In other words: [char] -> [token]
t_CHOICE_ELEMENT ¶
(?:`[a-zA-Z][a-zA-Z0-9-][^`][x]`)|(?:(?:|[a-zA-Z])[a-zA-Z0-9][x])
t_ENVIRONMENTAL_VARIABLE ¶
(?:\%[a-zA-Z][a-zA-Z0-9-]|\%`[a-zA-Z][a-zA-Z0-9-][^`]`)
t_DATETIME ¶
@\d{4}(?:-\d{2}(?:-\d{2})?)?T(?:\d{2}(?::\d{2}(?::\d{2}(?:.\d{3}(?:[+|-]\d{2}(?::\d{2})?)?)?)?)?)?