Skip to content

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

FHIRPathParser(debug=False, lexer_class=None, cache_limit=1000)

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

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(input_string: str) -> FHIRPath | Any

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

is_valid(input_string: str) -> bool

Check if a FHIRPath expression string is valid without raising exceptions.

Parameters:

Name Type Description Default
input_string str

The FHIRPath expression string to validate.

required

Returns:

Name Type Description
bool bool

True if the expression is valid, False otherwise.

parse_token_stream

parse_token_stream(token_iterator: Iterator) -> FHIRPath

Parse a stream of tokens and return the corresponding FHIRPath object.

Parameters:

Name Type Description Default
token_iterator Iterator

An iterator that yields tokens from the lexer.

required

Returns:

Name Type Description
FHIRPath FHIRPath

The parsed FHIRPath object representing the expression.

p_fhirpath_term_expression

p_fhirpath_term_expression(p)

expression : term

p_fhirpath_invocation_expression

p_fhirpath_invocation_expression(p)

expression : expression '.' invocation

p_fhirpath_indexer_expression

p_fhirpath_indexer_expression(p)

expression : expression '[' expression ']'

p_fhirpath_multiplicative_operation

p_fhirpath_multiplicative_operation(p)

expression : expression '*' expression | expression '/' expression | expression DIV expression | expression MOD expression

p_fhirpath_additive_operation

p_fhirpath_additive_operation(p)

expression : expression '+' expression | expression '-' expression | expression '&' expression

p_fhirpath_type_operation

p_fhirpath_type_operation(p)

expression : expression IS type_specifier | expression AS type_specifier

p_fhirpath_union_operation

p_fhirpath_union_operation(p)

expression : expression '|' expression

p_fhirpath_equality_operation

p_fhirpath_equality_operation(p)

expression : expression EQUAL expression | expression EQUIVALENT expression | expression NOT_EQUAL expression | expression NOT_EQUIVALENT expression

p_fhirpath_inequality_operation

p_fhirpath_inequality_operation(p)

expression : expression GREATER_THAN expression | expression GREATER_EQUAL_THAN expression | expression LESS_THAN expression | expression LESS_EQUAL_THAN expression

p_fhirpath_membership_fhirpath_operation

p_fhirpath_membership_fhirpath_operation(p)

expression : expression IN expression | expression CONTAINS expression

p_fhirpath_and_operation

p_fhirpath_and_operation(p)

expression : expression AND expression

p_fhirpath_or_operation

p_fhirpath_or_operation(p)

expression : expression OR expression | expression XOR expression

p_fhirpath_implies_operation

p_fhirpath_implies_operation(p)

expression : expression IMPLIES expression

p_fhirpath_term

p_fhirpath_term(p)

term : invocation | literal | constant | parenthesized_expression

p_fhirpath_parenthesized_expression

p_fhirpath_parenthesized_expression(p)

parenthesized_expression : '(' expression ')'

p_fhirpath_invocation

p_fhirpath_invocation(p)

invocation : element | root | type_choice | function | contextual

p_fhirpath_root

p_fhirpath_root(p)

root : ROOT_NODE

p_fhirpath_element

p_fhirpath_element(p)

element : identifier

p_fhirpath_typechoice_invocation

p_fhirpath_typechoice_invocation(p)

type_choice : CHOICE_ELEMENT

p_fhirpath_constant

p_fhirpath_constant(p)

constant : ENVIRONMENTAL_VARIABLE

p_fhirpath_contextual

p_fhirpath_contextual(p)

contextual : CONTEXTUAL_OPERATOR

p_fhirpath_type_specifier

p_fhirpath_type_specifier(p)

type_specifier : identifier | ROOT_NODE

p_fhirpath_type_specifier_context

p_fhirpath_type_specifier_context(p)
identifier '.' identifier

| identifier '.' ROOT_NODE

p_fhirpath_function

p_fhirpath_function(p)

function : function_name '(' arguments ')'

p_fhirpath_type_function

p_fhirpath_type_function(p)

function : OFTYPE '(' type_specifier ')' | IS '(' type_specifier ')' | AS '(' type_specifier ')'

p_fhirpath_function_name

p_fhirpath_function_name(p)

function_name : identifier | CONTAINS | IN

p_fhirpath_function_arguments

p_fhirpath_function_arguments(p)
arguments ',' argument

| argument

p_fhirpath_function_argument

p_fhirpath_function_argument(p)

argument : expression | empty

p_fhirpath_identifier

p_fhirpath_identifier(p)

identifier : IDENTIFIER

p_fhirpath_polar_literal

p_fhirpath_polar_literal(p)
'+' number

| '-' number

p_fhirpath_literal

p_fhirpath_literal(p)

literal : number | boolean | STRING | date | time | datetime | quantity

p_fhirpath_literal_empty

p_fhirpath_literal_empty(p)

literal : '{' '}'

p_fhirpath_boolean

p_fhirpath_boolean(p)

boolean : BOOLEAN

p_fhirpath_datetime

p_fhirpath_datetime(p)

datetime : DATETIME

p_fhirpath_time

p_fhirpath_time(p)

time : TIME

p_fhirpath_date

p_fhirpath_date(p)

date : DATE

p_fhirpath_quantity

p_fhirpath_quantity(p)

quantity : number unit

p_fhirpath_unit

p_fhirpath_unit(p)

unit : STRING | CALENDAR_DURATION

p_fhirpath_number

p_fhirpath_number(p)

number : INTEGER | DECIMAL

p_fhirpath_empty

p_fhirpath_empty(p)

empty :

IteratorToTokenStream

Path: fhircraft.fhir.path.parser.IteratorToTokenStream

IteratorToTokenStream(iterator: Iterator)

A wrapper class that converts an iterator of tokens into a token stream compatible with PLY's parser.

Methods:

Name Description
token

Retrieve the next token from the iterator.

token

token()

Retrieve the next token from the iterator.

lexer

Classes:

Name Description
FHIRPathLexer

A Lexical analyzer for JsonPath.

FHIRPathLexer

Path: fhircraft.fhir.path.lexer.FHIRPathLexer

FHIRPathLexer(debug=False)

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

tokenize(string)

Maps a string to an iterator over tokens. In other words: [char] -> [token]

t_ignore_WHITESPACE

t_ignore_WHITESPACE(t)

[\s]

t_ignore_COMMENT

t_ignore_COMMENT(t)

\/*([\s\S]?)*\/|\/\/(.)

t_CHOICE_ELEMENT

t_CHOICE_ELEMENT(t)

(?:`[a-zA-Z][a-zA-Z0-9-][^`][x]`)|(?:(?:|[a-zA-Z])[a-zA-Z0-9][x])

t_ENVIRONMENTAL_VARIABLE

t_ENVIRONMENTAL_VARIABLE(t)

(?:\%[a-zA-Z][a-zA-Z0-9-]|\%`[a-zA-Z][a-zA-Z0-9-][^`]`)

t_CONTEXTUAL_OPERATOR

t_CONTEXTUAL_OPERATOR(t)

$(\w*)?

t_DATETIME

t_DATETIME(t)

@\d{4}(?:-\d{2}(?:-\d{2})?)?T(?:\d{2}(?::\d{2}(?::\d{2}(?:.\d{3}(?:[+|-]\d{2}(?::\d{2})?)?)?)?)?)?

t_DATE

t_DATE(t)

@\d{4}(?:-\d{2}(?:-\d{2})?)?

t_TIME

t_TIME(t)

\@T\d{2}(?::\d{2}(?::\d{2}(?:.\d{3}(?:[+|-]\d{2}(?::\d{2})?)?)?)?)?

t_NUMBER

t_NUMBER(t)

\d+(.\d+)?

t_STRING

t_STRING(t)

\'((?:[^\'\]|\.)*)\'

t_IDENTIFIER

t_IDENTIFIER(t)

(?:`[a-zA-Z][a-zA-Z0-9-][^`]`)|(?:(?:|[a-zA-Z])[a-zA-Z0-9])

t_NOT_EQUAL

t_NOT_EQUAL(t)

!=

t_NOT_EQUIVALENT

t_NOT_EQUIVALENT(t)

!~

t_GREATER_EQUAL_THAN

t_GREATER_EQUAL_THAN(t)

=

t_LESS_EQUAL_THAN

t_LESS_EQUAL_THAN(t)

<=

t_LESS_THAN

t_LESS_THAN(t)

<

t_GREATER_THAN

t_GREATER_THAN(t)

t_EQUAL

t_EQUAL(t)

=

t_EQUIVALENT

t_EQUIVALENT(t)

~

t_error_invalid_function

t_error_invalid_function(t)

[a-zA-Z][a-zA-Z_0-9]*\((?:.*)?\)

t_error_doublequote_string

t_error_doublequote_string(t)

\"([^\"]*)?\"