Search in sources :

Example 21 with FHIRLexer

use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method parsePartial.

/**
 * Parse a path for later use using execute
 *
 * @param path
 * @return
 * @throws PathEngineException
 * @throws Exception
 */
public ExpressionNodeWithOffset parsePartial(String path, int i) throws FHIRLexerException {
    FHIRLexer lexer = new FHIRLexer(path, i);
    if (lexer.done()) {
        throw lexer.error("Path cannot be empty");
    }
    ExpressionNode result = parseExpression(lexer, true);
    result.check();
    return new ExpressionNodeWithOffset(lexer.getCurrentStart(), result);
}
Also used : ExpressionNode(org.hl7.fhir.r5.model.ExpressionNode)

Example 22 with FHIRLexer

use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.

the class FHIRPathEngine method gatherPrecedence.

private ExpressionNode gatherPrecedence(FHIRLexer lexer, ExpressionNode start, EnumSet<Operation> ops) {
    assert (start.isProximal());
    // is there anything to do?
    boolean work = false;
    ExpressionNode focus = start.getOpNext();
    if (ops.contains(start.getOperation())) {
        while (focus != null && focus.getOperation() != null) {
            work = work || !ops.contains(focus.getOperation());
            focus = focus.getOpNext();
        }
    } else {
        while (focus != null && focus.getOperation() != null) {
            work = work || ops.contains(focus.getOperation());
            focus = focus.getOpNext();
        }
    }
    if (!work) {
        return start;
    }
    // entry point: tricky
    ExpressionNode group;
    if (ops.contains(start.getOperation())) {
        group = newGroup(lexer, start);
        group.setProximal(true);
        focus = start;
        start = group;
    } else {
        ExpressionNode node = start;
        focus = node.getOpNext();
        while (!ops.contains(focus.getOperation())) {
            node = focus;
            focus = focus.getOpNext();
        }
        group = newGroup(lexer, focus);
        node.setOpNext(group);
    }
    // focus points at the group.group
    do {
        // run until we find the end of the sequence
        while (ops.contains(focus.getOperation())) {
            focus = focus.getOpNext();
        }
        if (focus.getOperation() != null) {
            group.setOperation(focus.getOperation());
            group.setOpNext(focus.getOpNext());
            focus.setOperation(null);
            focus.setOpNext(null);
            // now look for another sequence, and start it
            ExpressionNode node = group;
            focus = group.getOpNext();
            if (focus != null) {
                while (focus != null && !ops.contains(focus.getOperation())) {
                    node = focus;
                    focus = focus.getOpNext();
                }
                if (focus != null) {
                    // && (focus.Operation in Ops) - must be true
                    group = newGroup(lexer, focus);
                    node.setOpNext(group);
                }
            }
        }
    } while (focus != null && focus.getOperation() != null);
    return start;
}
Also used : ExpressionNode(org.hl7.fhir.r5.model.ExpressionNode)

Example 23 with FHIRLexer

use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method parseGroup.

private void parseGroup(StructureMap result, FHIRLexer lexer) throws FHIRException {
    lexer.token("group");
    StructureMapGroupComponent group = result.addGroup();
    boolean newFmt = false;
    if (lexer.hasToken("for")) {
        lexer.token("for");
        if ("type".equals(lexer.getCurrent())) {
            lexer.token("type");
            lexer.token("+");
            lexer.token("types");
            group.setTypeMode(StructureMapGroupTypeMode.TYPEANDTYPES);
        } else {
            lexer.token("types");
            group.setTypeMode(StructureMapGroupTypeMode.TYPES);
        }
    } else
        group.setTypeMode(StructureMapGroupTypeMode.NONE);
    group.setName(lexer.take());
    if (lexer.hasToken("(")) {
        newFmt = true;
        lexer.take();
        while (!lexer.hasToken(")")) {
            parseInput(group, lexer, true);
            if (lexer.hasToken(","))
                lexer.token(",");
        }
        lexer.take();
    }
    if (lexer.hasToken("extends")) {
        lexer.next();
        group.setExtends(lexer.take());
    }
    if (newFmt) {
        group.setTypeMode(StructureMapGroupTypeMode.NONE);
        if (lexer.hasToken("<")) {
            lexer.token("<");
            lexer.token("<");
            if (lexer.hasToken("types")) {
                group.setTypeMode(StructureMapGroupTypeMode.TYPES);
                lexer.token("types");
            } else {
                lexer.token("type");
                lexer.token("+");
                group.setTypeMode(StructureMapGroupTypeMode.TYPEANDTYPES);
            }
            lexer.token(">");
            lexer.token(">");
        }
        lexer.token("{");
    }
    lexer.skipComments();
    if (newFmt) {
        while (!lexer.hasToken("}")) {
            if (lexer.done())
                throw lexer.error("premature termination expecting 'endgroup'");
            parseRule(result, group.getRule(), lexer, true);
        }
    } else {
        while (lexer.hasToken("input")) parseInput(group, lexer, false);
        while (!lexer.hasToken("endgroup")) {
            if (lexer.done())
                throw lexer.error("premature termination expecting 'endgroup'");
            parseRule(result, group.getRule(), lexer, false);
        }
    }
    lexer.next();
    if (newFmt && lexer.hasToken(";"))
        lexer.next();
    lexer.skipComments();
}
Also used : StructureMapGroupComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupComponent)

Example 24 with FHIRLexer

use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method parseRule.

private void parseRule(StructureMap map, List<StructureMapGroupRuleComponent> list, FHIRLexer lexer, boolean newFmt) throws FHIRException {
    StructureMapGroupRuleComponent rule = new StructureMapGroupRuleComponent();
    list.add(rule);
    if (!newFmt) {
        rule.setName(lexer.takeDottedToken());
        lexer.token(":");
        lexer.token("for");
    }
    boolean done = false;
    while (!done) {
        parseSource(rule, lexer);
        done = !lexer.hasToken(",");
        if (!done)
            lexer.next();
    }
    if ((newFmt && lexer.hasToken("->")) || (!newFmt && lexer.hasToken("make"))) {
        lexer.token(newFmt ? "->" : "make");
        done = false;
        while (!done) {
            parseTarget(rule, lexer);
            done = !lexer.hasToken(",");
            if (!done)
                lexer.next();
        }
    }
    if (lexer.hasToken("then")) {
        lexer.token("then");
        if (lexer.hasToken("{")) {
            lexer.token("{");
            if (lexer.hasComment()) {
                rule.setDocumentation(lexer.take().substring(2).trim());
            }
            lexer.skipComments();
            while (!lexer.hasToken("}")) {
                if (lexer.done())
                    throw lexer.error("premature termination expecting '}' in nested group");
                parseRule(map, rule.getRule(), lexer, newFmt);
            }
            lexer.token("}");
        } else {
            done = false;
            while (!done) {
                parseRuleReference(rule, lexer);
                done = !lexer.hasToken(",");
                if (!done)
                    lexer.next();
            }
        }
    } else if (lexer.hasComment()) {
        rule.setDocumentation(lexer.take().substring(2).trim());
    }
    if (isSimpleSyntax(rule)) {
        rule.getSourceFirstRep().setVariable(AUTO_VAR_NAME);
        rule.getTargetFirstRep().setVariable(AUTO_VAR_NAME);
        // with no parameter - e.g. imply what is to be created
        rule.getTargetFirstRep().setTransform(StructureMapTransform.CREATE);
    // no dependencies - imply what is to be done based on types
    }
    if (newFmt) {
        if (lexer.isConstant()) {
            if (lexer.isStringConstant()) {
                rule.setName(lexer.readConstant("ruleName"));
            } else {
                rule.setName(lexer.take());
            }
        } else {
            if (rule.getSource().size() != 1 || !rule.getSourceFirstRep().hasElement())
                throw lexer.error("Complex rules must have an explicit name");
            if (rule.getSourceFirstRep().hasType())
                rule.setName(rule.getSourceFirstRep().getElement() + "-" + rule.getSourceFirstRep().getType());
            else
                rule.setName(rule.getSourceFirstRep().getElement());
        }
        lexer.token(";");
    }
    lexer.skipComments();
}
Also used : StructureMapGroupRuleComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleComponent)

Example 25 with FHIRLexer

use of org.hl7.fhir.r4b.utils.FHIRLexer in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method parseInput.

private void parseInput(StructureMapGroupComponent group, FHIRLexer lexer, boolean newFmt) throws FHIRException {
    StructureMapGroupInputComponent input = group.addInput();
    if (newFmt) {
        input.setMode(StructureMapInputMode.fromCode(lexer.take()));
    } else
        lexer.token("input");
    input.setName(lexer.take());
    if (lexer.hasToken(":")) {
        lexer.token(":");
        input.setType(lexer.take());
    }
    if (!newFmt) {
        lexer.token("as");
        input.setMode(StructureMapInputMode.fromCode(lexer.take()));
        if (lexer.hasComment()) {
            input.setDocumentation(lexer.take().substring(2).trim());
        }
        lexer.skipToken(";");
        lexer.skipComments();
    }
}
Also used : StructureMapGroupInputComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupInputComponent)

Aggregations

ExpressionNode (org.hl7.fhir.r4.model.ExpressionNode)8 ExpressionNode (org.hl7.fhir.dstu2016may.model.ExpressionNode)7 ExpressionNode (org.hl7.fhir.dstu3.model.ExpressionNode)7 ExpressionNode (org.hl7.fhir.r4b.model.ExpressionNode)6 ExpressionNode (org.hl7.fhir.r5.model.ExpressionNode)6 ExpressionNode (org.hl7.fhir.dstu2.model.ExpressionNode)5 BigDecimal (java.math.BigDecimal)3 HashMap (java.util.HashMap)3 SourceLocation (org.hl7.fhir.utilities.SourceLocation)3 StringType (org.hl7.fhir.dstu3.model.StringType)2 StringType (org.hl7.fhir.r4.model.StringType)2 Function (org.hl7.fhir.dstu2.model.ExpressionNode.Function)1 SourceLocation (org.hl7.fhir.dstu2.model.ExpressionNode.SourceLocation)1 FunctionDetails (org.hl7.fhir.dstu2.utils.FHIRPathEngine.IEvaluationContext.FunctionDetails)1 ConceptMap (org.hl7.fhir.dstu2016may.model.ConceptMap)1 SourceElementComponent (org.hl7.fhir.dstu2016may.model.ConceptMap.SourceElementComponent)1 TargetElementComponent (org.hl7.fhir.dstu2016may.model.ConceptMap.TargetElementComponent)1 Function (org.hl7.fhir.dstu2016may.model.ExpressionNode.Function)1 SourceLocation (org.hl7.fhir.dstu2016may.model.ExpressionNode.SourceLocation)1 IdType (org.hl7.fhir.dstu2016may.model.IdType)1