forked from smart-on-fhir/fhir-parser
-
Notifications
You must be signed in to change notification settings - Fork 9
/
FHIRPathExpression.g4
175 lines (141 loc) · 5.53 KB
/
FHIRPathExpression.g4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
grammar FHIRPathExpression;
// Grammar rules
// [FHIRPath](http://hl7.org/fhirpath/N1) Normative Release
//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
| DATE #dateLiteral
| 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
*****************************************************************/
/*
NOTE: The goal of these rules in the grammar is to provide a date
token to the parser. As such it is not attempting to validate that
the date is a correct date, that task is for the parser or interpreter.
*/
DATE
: '@' DATEFORMAT
;
DATETIME
: '@' DATEFORMAT 'T' (TIMEFORMAT TIMEZONEOFFSETFORMAT?)?
;
TIME
: '@' 'T' TIMEFORMAT
;
fragment DATEFORMAT
: [0-9][0-9][0-9][0-9] ('-'[0-9][0-9] ('-'[0-9][0-9])?)?
;
fragment TIMEFORMAT
: [0-9][0-9] (':'[0-9][0-9] (':'[0-9][0-9] ('.'[0-9]+)?)?)?
;
fragment TIMEZONEOFFSETFORMAT
: ('Z' | ('+' | '-') [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]
;
// Credit: copied from https://raw.githubusercontent.com/HL7/FHIRPath/master/spec/fhirpath.g4