This is the 3rd normative ballot of FHIRPath, v1.3.0 in its permanent home.
For a full list of available versions, see the Directory of published versions
.
| Implementable Technology Specifications Work Group | Maturity Level: N | Standards Status: Normative |
This page contains the Antlr 4.0 grammar for FHIRPath.
grammar fhirpath;
// Grammar rules
// [FHIRPath](http://hl7.org/fhirpath/2019May) Normative Ballot 3
//prog: line (line)*;
//line: ID ( '(' expr ')') ':' expr '\r'? '\n';
expression
: term #termExpression
| expression '.' invocation #invocationExpression
| expression '[' expression ']' #indexerExpression
| ('+' | '-') expression #polarityExpression
| expression ('*' | '/' | 'div' | 'mod') expression #multiplicativeExpression
| expression ('+' | '-' | '&') expression #additiveExpression
| expression ('is' | 'as') typeSpecifier #typeExpression
| expression '|' expression #unionExpression
| expression ('<=' | '<' | '>' | '>=') expression #inequalityExpression
| expression ('=' | '~' | '!=' | '!~') expression #equalityExpression
| expression ('in' | 'contains') expression #membershipExpression
| expression 'and' expression #andExpression
| expression ('or' | 'xor') expression #orExpression
| expression 'implies' expression #impliesExpression
//| (IDENTIFIER)? '=>' expression #lambdaExpression
;
term
: invocation #invocationTerm
| literal #literalTerm
| externalConstant #externalConstantTerm
| '(' expression ')' #parenthesizedTerm
;
literal
: '{' '}' #nullLiteral
| ('true' | 'false') #booleanLiteral
| STRING #stringLiteral
| NUMBER #numberLiteral
| DATETIME #dateTimeLiteral
| TIME #timeLiteral
| quantity #quantityLiteral
;
externalConstant
: '%' ( identifier | STRING )
;
invocation // Terms that can be used after the function/member invocation '.'
: identifier #memberInvocation
| function #functionInvocation
| '$this' #thisInvocation
| '$index' #indexInvocation
| '$total' #totalInvocation
;
function
: identifier '(' paramList? ')'
;
paramList
: expression (',' expression)*
;
quantity
: NUMBER unit?
;
unit
: dateTimePrecision
| pluralDateTimePrecision
| STRING // UCUM syntax for units of measure
;
dateTimePrecision
: 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond'
;
pluralDateTimePrecision
: 'years' | 'months' | 'weeks' | 'days' | 'hours' | 'minutes' | 'seconds' | 'milliseconds'
;
typeSpecifier
: qualifiedIdentifier
;
qualifiedIdentifier
: identifier ('.' identifier)*
;
identifier
: IDENTIFIER
| DELIMITEDIDENTIFIER
| 'as'
| 'contains'
| 'in'
| 'is'
;
/****************************************************************
Lexical rules
*****************************************************************/
// Not sure why, but with these as lexical rules, when the grammar is imported into CQL, they are not correctly recognized
// Moving the same rules into the literal production rule above corrects the issue
//EMPTY
// : '{' '}'
// ; // To create an empty array (and avoid a NULL literal)
//BOOL
// : 'true'
// | 'false'
// ;
DATETIME
: '@'
[0-9][0-9][0-9][0-9] // year
(
(
'-'[0-9][0-9] // month
(
(
'-'[0-9][0-9] // day
('T' TIMEFORMAT?)?
)
| 'T'
)?
)
| 'T'
)?
('Z' | ('+' | '-') [0-9][0-9]':'[0-9][0-9])? // timezone offset
;
TIME
: '@' 'T' TIMEFORMAT
;
fragment TIMEFORMAT
: [0-9][0-9] (':'[0-9][0-9] (':'[0-9][0-9] ('.'[0-9]+)?)?)?
;
IDENTIFIER
: ([A-Za-z] | '_')([A-Za-z0-9] | '_')* // Added _ to support CQL (FHIR could constrain it out)
;
DELIMITEDIDENTIFIER
: '`' (ESC | .)*? '`'
;
STRING
: '\'' (ESC | .)*? '\''
;
// Also allows leading zeroes now (just like CQL and XSD)
NUMBER
: [0-9]+('.' [0-9]+)?
;
// Pipe whitespace to the HIDDEN channel to support retrieving source text through the parser.
WS
: [ \r\n\t]+ -> channel(HIDDEN)
;
COMMENT
: '/*' .*? '*/' -> channel(HIDDEN)
;
LINE_COMMENT
: '//' ~[\r\n]* -> channel(HIDDEN)
;
fragment ESC
: '\\' ([`'\\/fnrt] | UNICODE) // allow \`, \', \\, \/, \f, etc. and \uXXX
;
fragment UNICODE
: 'u' HEX HEX HEX HEX
;
fragment HEX
: [0-9a-fA-F]
;