Search in sources :

Example 16 with FHIRLexer

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

the class StructureMapUtilities method parseConceptMap.

private void parseConceptMap(StructureMap result, FHIRLexer lexer) throws FHIRLexerException {
    lexer.token("conceptmap");
    ConceptMap map = new ConceptMap();
    String id = lexer.readConstant("map id");
    if (id.startsWith("#")) {
        throw lexer.error("Concept Map identifier must start with #");
    }
    map.setId(id);
    // todo: how to add this to the text format
    map.setStatus(PublicationStatus.DRAFT);
    result.getContained().add(map);
    lexer.token("{");
    // lexer.token("source");
    // map.setSource(new UriType(lexer.readConstant("source")));
    // lexer.token("target");
    // map.setSource(new UriType(lexer.readConstant("target")));
    Map<String, String> prefixes = new HashMap<String, String>();
    while (lexer.hasToken("prefix")) {
        lexer.token("prefix");
        String n = lexer.take();
        lexer.token("=");
        String v = lexer.readConstant("prefix url");
        prefixes.put(n, v);
    }
    while (lexer.hasToken("unmapped")) {
        lexer.token("unmapped");
        lexer.token("for");
        String n = readPrefix(prefixes, lexer);
        ConceptMapGroupComponent g = getGroup(map, n, null);
        lexer.token("=");
        String v = lexer.take();
        if (v.equals("provided")) {
            g.getUnmapped().setMode(ConceptMapGroupUnmappedMode.PROVIDED);
        } else {
            throw lexer.error("Only unmapped mode PROVIDED is supported at this time");
        }
    }
    while (!lexer.hasToken("}")) {
        String srcs = readPrefix(prefixes, lexer);
        lexer.token(":");
        String sc = lexer.getCurrent().startsWith("\"") ? lexer.readConstant("code") : lexer.take();
        ConceptMapEquivalence eq = readEquivalence(lexer);
        String tgts = (eq != ConceptMapEquivalence.UNMATCHED) ? readPrefix(prefixes, lexer) : "";
        ConceptMapGroupComponent g = getGroup(map, srcs, tgts);
        SourceElementComponent e = g.addElement();
        e.setCode(sc);
        if (e.getCode().startsWith("\"")) {
            e.setCode(lexer.processConstant(e.getCode()));
        }
        TargetElementComponent tgt = e.addTarget();
        tgt.setEquivalence(eq);
        if (tgt.getEquivalence() != ConceptMapEquivalence.UNMATCHED) {
            lexer.token(":");
            tgt.setCode(lexer.take());
            if (tgt.getCode().startsWith("\"")) {
                tgt.setCode(lexer.processConstant(tgt.getCode()));
            }
        }
        tgt.setComment(lexer.getFirstComment());
    }
    lexer.token("}");
}
Also used : TargetElementComponent(org.hl7.fhir.r4b.model.ConceptMap.TargetElementComponent) ConceptMapEquivalence(org.hl7.fhir.r4b.model.ConceptMap.ConceptMapEquivalence) ConceptMapGroupComponent(org.hl7.fhir.r4b.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.r4b.model.ConceptMap.SourceElementComponent)

Example 17 with FHIRLexer

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

the class StructureMapUtilities method parse.

public StructureMap parse(String text, String srcName) throws FHIRException {
    FHIRLexer lexer = new FHIRLexer(text, srcName);
    if (lexer.done())
        throw lexer.error("Map Input cannot be empty");
    lexer.token("map");
    StructureMap result = new StructureMap();
    result.setUrl(lexer.readConstant("url"));
    lexer.token("=");
    result.setName(lexer.readConstant("name"));
    result.setDescription(lexer.getAllComments());
    while (lexer.hasToken("conceptmap")) parseConceptMap(result, lexer);
    while (lexer.hasToken("uses")) parseUses(result, lexer);
    while (lexer.hasToken("imports")) parseImports(result, lexer);
    while (!lexer.done()) {
        parseGroup(result, lexer);
    }
    return result;
}
Also used : StructureMap(org.hl7.fhir.r4b.model.StructureMap) FHIRLexer(org.hl7.fhir.r4b.utils.FHIRLexer)

Example 18 with FHIRLexer

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

the class FHIRPathEngine method processConstantString.

private String processConstantString(String s, FHIRLexer lexer) throws FHIRLexerException {
    StringBuilder b = new StringBuilder();
    int i = 1;
    while (i < s.length() - 1) {
        char ch = s.charAt(i);
        if (ch == '\\') {
            i++;
            switch(s.charAt(i)) {
                case 't':
                    b.append('\t');
                    break;
                case 'r':
                    b.append('\r');
                    break;
                case 'n':
                    b.append('\n');
                    break;
                case 'f':
                    b.append('\f');
                    break;
                case '\'':
                    b.append('\'');
                    break;
                case '"':
                    b.append('"');
                    break;
                case '`':
                    b.append('`');
                    break;
                case '\\':
                    b.append('\\');
                    break;
                case '/':
                    b.append('/');
                    break;
                case 'u':
                    i++;
                    int uc = Integer.parseInt(s.substring(i, i + 4), 16);
                    b.append((char) uc);
                    i = i + 3;
                    break;
                default:
                    throw lexer.error("Unknown character escape \\" + s.charAt(i));
            }
            i++;
        } else {
            b.append(ch);
            i++;
        }
    }
    return b.toString();
}
Also used : CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder)

Example 19 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) throws FHIRException {
    StructureMapGroupRuleComponent rule = new StructureMapGroupRuleComponent();
    list.add(rule);
    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 (lexer.hasToken("make")) {
        lexer.token("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);
            }
            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
    }
    lexer.skipComments();
}
Also used : StructureMapGroupRuleComponent(org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupRuleComponent)

Example 20 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.dstu3.model.ExpressionNode)

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