Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagetext
titleGrammar
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

grammar HL7v2ToFHIRConditions
;

// Not sure if IF and THEN are necessary, they seem redundant
IF
    : 'IF'
;

THEN
    : 'THEN'
;

SEGMENT_FIELD_DELIM
    : '-'
;

SUBFIELD_DELIM
    : '.'
;

LIST_DELIM
    : ','
;

DIGIT
  : INTEGER
: [0-9];

STRING
  : '"' STRINGCHAR* '"'
 ;

BINARY_OPERATOR_1
    : '=EQUAL'
    | 'NOT= EQUAL'
;

BINARY_OPERATOR_2
    : 'AND'
    | 'OR'
;

LIST_OPERATOR
    : 'IN'
;

UNARY_OPERATOR
    : 'NOT'
;

EXISTS
    : 'EXISTS'
;

fragment
INTEGER
  : [0-9]+
  ;

fragment
STRINGCHAR
  : ~["\\]
  | '\\' ["\\]
  ;

SEGMENT
    : [A-Z][A-Z0-9][A-Z0-9]
;

// HL7v2 element reference rules.
component
    : DIGIT+
;

subcomponent
    : DIGIT+
;

field
    : DIGIT+
;

segment
    : SEGMENT
;

// A nicely structured reference to an HL7v2 element. Do we want to specialize repeated field refs?
v2ref
    : segment (
        SEGMENT_FIELD_DELIM field (
            SUBFIELD_DELIM component (
                SUBFIELD_DELIM subcomponent
            )?
        )?
    )?
;

list
    : '()'                                  # EmptyList
    | '(' v2ref (LIST_DELIM v2ref)* ')'     # RefElementList
    | '(' STRING (LIST_DELIM STRING)* ')'   # StrElementList
    | '(' DIGIT (LIST_DELIM DIGIT)* ')'   # NumElementList

;

expression
    : expression BINARY_OPERATOR_1 expression # BiOpExpr
    | expression BINARY_OPERATOR_2 expression # BiOpExpr
    | UNARY_OPERATOR expression               # UnOpExpr
    | v2ref LIST_OPERATOR list                # ListExpr
    | '(' expression ')'                      # NestExpr
    | v2ref                                   # RefExpr
    | v2ref EXISTS                            # ExistRef
    | expression EXISTS                       # ExistExpr
;

// condition is our "root" rule.
condition
    : IF expression THEN
;

Whitespace
    : [ \t]+ -> skip
;


...