Exemple #1
0
def literal_term(ctx, parentData, node):
    term = node["children"][0]

    if term:
        return engine.do_eval(ctx, parentData, term)

    return [node["text"]]
def apply_parsed_path(resource, parsedPath, context={}, model=None):
    Constants.reset()
    dataRoot = arraify(resource)
    """
    do_eval takes a "ctx" object, and we store things in that as we parse, so we
    need to put user-provided variable data in a sub-object, ctx['vars'].
    Set up default standard variables, and allow override from the variables.
    However, we'll keep our own copy of dataRoot for internal processing.
    """
    vars = {"context": resource, "ucum": "http://unitsofmeasure.org"}
    vars.update(context)

    ctx = {"dataRoot": dataRoot, "vars": vars, "model": model}
    node = do_eval(ctx, dataRoot, parsedPath["children"][0])

    # Resolve any internal "ResourceNode" instances.  Continue to let FP_Type
    # subclasses through.

    def visit(node):
        data = get_data(node)

        if isinstance(node, list):
            return [visit(item) for item in data]

        if isinstance(data, dict) and not isinstance(data, FP_Type):
            for key, value in data.items():
                data[key] = visit(value)

        return data

    return visit(node)
Exemple #3
0
def indexer_expression(ctx, parentData, node):
    coll_node = node["children"][0]
    idx_node = node["children"][1]

    coll = engine.do_eval(ctx, parentData, coll_node)
    idx = engine.do_eval(ctx, parentData, idx_node)

    if util.is_empty(idx):
        return []

    idxNum = int(idx[0])

    if coll is not None and util.is_some(
            idxNum) and len(coll) > idxNum and idxNum >= 0:
        return [coll[idxNum]]

    return []
Exemple #4
0
def invocation_expression(ctx, parentData, node):
    return list(
        reduce(
            lambda accumulator, children: engine.do_eval(
                ctx, accumulator, children),
            node["children"],
            parentData,
        ))
Exemple #5
0
def function_invocation(ctx, parentData, node):
    args = engine.do_eval(ctx, parentData, node["children"][0])
    fn_name = args[0]
    args = args[1:]

    raw_params = None
    if isinstance(args, list) and len(args) > 0 and "children" in args[0]:
        raw_params = args[0]["children"]

    return engine.doInvoke(ctx, fn_name, parentData, raw_params)
Exemple #6
0
def polarity_expression(ctx, parentData, node):
    sign = node["terminalNodeText"][0]
    rtn = engine.do_eval(ctx, parentData, node["children"][0])

    if len(rtn) != 1:  # not yet in spec, but per Bryn Rhodes
        raise Exception("Unary " + sign +
                        " can only be applied to an individual number.")

    if not util.is_number(rtn[0]):
        raise Exception("Unary " + sign + " can only be applied to a number.")

    if sign == "-":
        rtn[0] = -rtn[0]

    return rtn
Exemple #7
0
def member_invocation(ctx, parentData, node):
    key = engine.do_eval(ctx, parentData, node["children"][0])[0]
    model = ctx["model"]

    if isinstance(parentData, list):
        if util.is_capitalized(key):
            try:
                filtered = [x for x in parentData if x["resourceType"] == key]
                mapped = [
                    nodes.ResourceNode.create_node(x, key) for x in filtered
                ]

                return mapped
            except TypeError:
                pass

        return list(
            reduce(create_reduce_member_invocation(model, key), parentData,
                   []))

    return []
Exemple #8
0
def parenthesized_term(ctx, parentData, node):
    return engine.do_eval(ctx, parentData, node["children"][0])
Exemple #9
0
def term_expression(ctx, parentData, node):
    return engine.do_eval(ctx, parentData, node["children"][0])
Exemple #10
0
def invocation_term(ctx, parentData, node):
    return engine.do_eval(ctx, parentData, node["children"][0])
Exemple #11
0
def functn(ctx, parentData, node):
    return [engine.do_eval(ctx, parentData, x) for x in node["children"]]