Ejemplo n.º 1
0
def test_string_parse_e2e_multiword2(type_context):
    fooType = AInixType(type_context, "FooType")
    fo = AInixObject(
        type_context,
        "fo",
        "FooType", [],
        preferred_object_parser_name=create_object_parser_from_grammar(
            type_context, "fooname", '"foo bar"').name)
    twoargs = AInixObject(type_context,
                          "FooProgram",
                          "Program", [
                              AInixArgument(type_context,
                                            "a",
                                            None,
                                            arg_data={"short_name": "a"},
                                            parent_object_name="sdf"),
                              AInixArgument(type_context,
                                            "p1",
                                            "FooType",
                                            arg_data={
                                                POSITION: 0,
                                                MULTIWORD_POS_ARG: True
                                            },
                                            parent_object_name="sdf")
                          ],
                          type_data={"invoke_name": "hello"})
    type_context.finalize_data()
    parser = StringParser(type_context)
    ast = parser.create_parse_tree("hello foo bar -a", "Program")
    unparser = AstUnparser(type_context)
    to_string = unparser.to_string(ast)
    assert to_string.total_string == "hello -a foo bar"
Ejemplo n.º 2
0
def test_string_parse_e2e_sequence(type_context):
    twoargs = AInixObject(type_context,
                          "FooProgram",
                          "Program", [
                              AInixArgument(type_context,
                                            "a",
                                            None,
                                            arg_data={"short_name": "a"},
                                            parent_object_name="sdf"),
                              AInixArgument(type_context,
                                            "barg",
                                            None,
                                            arg_data={"short_name": "b"},
                                            parent_object_name="bw")
                          ],
                          type_data={"invoke_name": "hello"})
    parser = StringParser(type_context)
    unparser = AstUnparser(type_context)
    string = "hello -a | hello -b"
    ast = parser.create_parse_tree(string, "CommandSequence")
    to_string = unparser.to_string(ast)
    assert to_string.total_string == string

    no_space = "hello -a|hello -b"
    ast = parser.create_parse_tree(no_space, "CommandSequence")
    to_string = unparser.to_string(ast)
    assert to_string.total_string == string

    string = "hello -a | hello"
    ast = parser.create_parse_tree(string, "CommandSequence")
    to_string = unparser.to_string(ast)
    assert to_string.total_string == string
Ejemplo n.º 3
0
def test_string_parse_e2e_multiword3(type_context):
    fooType = AInixType(type_context, "FooType")
    fo = AInixObject(
        type_context,
        "fo",
        "FooType", [],
        preferred_object_parser_name=create_object_parser_from_grammar(
            type_context, "fooname", '"foo"').name)
    twoargs = AInixObject(type_context,
                          "FooProgram",
                          "Program", [
                              AInixArgument(type_context,
                                            "a",
                                            None,
                                            arg_data={"short_name": "a"},
                                            parent_object_name="sdf"),
                              AInixArgument(type_context,
                                            "barg",
                                            None,
                                            arg_data={"short_name": "b"},
                                            parent_object_name="bw"),
                              _make_positional()
                          ],
                          type_data={"invoke_name": "hello"})
    type_context.finalize_data()
    parser = StringParser(type_context)
    ast = parser.create_parse_tree("hello -a", "CommandSequence")
    unparser = AstUnparser(type_context)
    to_string = unparser.to_string(ast)
    assert to_string.total_string == "hello -a"
Ejemplo n.º 4
0
def _create_root_types(type_context: TypeContext):
    """Creates the underlying types for handling generid strings"""
    AInixType(type_context, WORD_PART_TYPE_NAME, WORD_PART_TYPE_PARSER_NAME)
    AInixObject(type_context, WORD_PART_TERMINAL_NAME, WORD_PART_TYPE_NAME)
    _create_modifier_types(type_context)
    AInixType(type_context, WORD_TYPE_NAME)
    # Create a word which cannot be empty str
    parts = AInixArgument(type_context,
                          "parts",
                          WORD_PART_TYPE_NAME,
                          required=True)

    def non_empty_word_parser(run: parse_primitives.ObjectParserRun,
                              string: str,
                              result: parse_primitives.ObjectParserResult):
        if string == "":
            raise AInixParseError("Expect a non-empty word")
        deleg = yield run.left_fill_arg(parts, (0, len(string)))
        result.accept_delegation(deleg)

    def unparser(arg_map: parse_primitives.ObjectNodeArgMap,
                 result: parse_primitives.ObjectToStringResult):
        result.add_arg_tostring(parts)

    parser = ObjectParser(type_context, "non_empty_word_parser",
                          non_empty_word_parser, unparser)
    AInixObject(type_context, WORD_OBJ_NAME, WORD_TYPE_NAME, [parts],
                parser.name)
Ejemplo n.º 5
0
def test_make_copy_optional_arg():
    tc = TypeContext()
    ft = AInixType(tc, "ft")
    bt = AInixType(tc, "bt")
    arg1 = AInixArgument(tc,
                         "arg1",
                         "bt",
                         required=False,
                         parent_object_name="fo")
    fo = AInixObject(
        tc,
        "fo",
        "ft", [arg1],
        preferred_object_parser_name=create_object_parser_from_grammar(
            tc, "masfoo_parser", '"foo" arg1?').name)
    bo = AInixObject(
        tc,
        "bo",
        "bt",
        None,
        preferred_object_parser_name=create_object_parser_from_grammar(
            tc, "masdfo_parser", '"bar"').name)
    tc.finalize_data()
    parser = StringParser(tc)
    unparser = AstUnparser(tc)
    ast = parser.create_parse_tree("foobar", "ft")
    tokenizer = SpaceTokenizer()
    in_str = "Hello bar sdf cow"
    tokens, metadata = tokenizer.tokenize(in_str)
    unpar_res = unparser.to_string(ast)
    assert unpar_res.total_string == "foobar"
    result = make_copy_version_of_tree(ast, unparser, metadata)
    assert result.next_node_not_copy.get_choice_node_for_arg(
        "arg1").copy_was_chosen
Ejemplo n.º 6
0
def test_pointer_change_here(freeze_first):
    """Test an arg change"""
    # Establish types
    tc = TypeContext()
    AInixType(tc, "footype")
    bartype = AInixType(tc, "bartype")
    arg1 = AInixArgument(tc, "arg1", "bartype", required=True)
    foo_object = AInixObject(tc, "foo_object", "footype", [arg1])
    bar_object = AInixObject(tc, "bar_object", "bartype")
    other_bar_obj = AInixObject(tc, "other_bar_ob", "bartype")
    # Make an ast
    arg_choice = ObjectChoiceNode(bartype)
    ob_chosen = ObjectNode(bar_object)
    arg_choice.set_choice(ob_chosen)
    instance = ObjectNode(foo_object)
    instance.set_arg_value("arg1", arg_choice)
    if freeze_first:
        instance.freeze()
    # Try change
    deepest = list(instance.depth_first_iter())[-1]
    assert deepest.cur_node == ob_chosen
    new_node = ObjectNode(other_bar_obj)
    new_point = deepest.change_here(new_node)
    assert new_point.cur_node == new_node
    assert new_point.cur_node.is_frozen == freeze_first
Ejemplo n.º 7
0
def test_objectnode_copy_simple_with_arg():
    """Copy with an arg"""
    # Establish types
    tc = TypeContext()
    AInixType(tc, "footype")
    bartype = AInixType(tc, "bartype")
    arg1 = AInixArgument(tc, "arg1", "bartype", required=True)
    foo_object = AInixObject(tc, "foo_object", "footype", [arg1])
    bar_object = AInixObject(tc, "bar_object", "bartype")
    # Make an ast
    arg_choice = ObjectChoiceNode(bartype)
    ob_chosen = ObjectNode(bar_object)
    arg_choice.set_choice(ob_chosen)
    instance = ObjectNode(foo_object)
    instance.set_arg_value("arg1", arg_choice)
    # Do the tests:
    # Unfrozen
    clone, leaf = instance.path_clone()
    assert id(clone) != id(instance)
    assert leaf is None
    # with a path
    clone, leaf = instance.path_clone([instance])
    assert id(clone) != id(instance)
    assert leaf.cur_node == clone
    assert leaf.parent is None
    # with a deep path
    clone, leaf = instance.path_clone([instance, arg_choice])
    assert id(clone) != id(instance)
    assert leaf.cur_node.choice is None
    # with a deeper path
    clone, leaf = instance.path_clone([instance, arg_choice, ob_chosen])
    assert id(clone) != id(instance)
    assert clone == instance
    assert leaf.cur_node == ob_chosen
    assert leaf.parent.cur_node.choice == ob_chosen
Ejemplo n.º 8
0
def test_dfs_in_set():
    tc = get_toy_strings_context()
    footype = AInixType(tc, "footype")
    bartype = AInixType(tc, "bartype")
    arg1 = AInixArgument(tc, "arg1", "bartype", required=True)
    foo_object = AInixObject(tc, "foo_object", "footype", [arg1])
    bar_object = AInixObject(tc, "bar_object", "bartype")
    other_bar_obj = AInixObject(tc, "other_bar_ob", "bartype")
    #
    arg_choice = ObjectChoiceNode(bartype)
    ob_chosen = ObjectNode(bar_object)
    arg_choice.set_choice(ob_chosen)
    instance = ObjectNode(foo_object)
    instance.set_arg_value("arg1", arg_choice)
    ast = ObjectChoiceNode(footype)
    ast.set_choice(instance)
    #
    ast_set = AstObjectChoiceSet(footype)
    ast_set.add(ast, True, 1, 1)
    path = [pointer.cur_node for pointer in list(ast.depth_first_iter())]
    assert path == [ast, instance, arg_choice, ob_chosen]
    result = list(depth_first_iterate_ast_set_along_path(ast_set, path))
    assert len(result) == len(path)
    assert isinstance(result[0], AstObjectChoiceSet)
    assert result[0].type_to_choose == footype
    assert isinstance(result[1], ObjectNodeSet)
    assert result[1].implementation == foo_object
    assert isinstance(result[2], AstObjectChoiceSet)
    assert result[2].type_to_choose == bartype
    assert isinstance(result[3], ObjectNodeSet)
    assert result[3].implementation == bar_object
Ejemplo n.º 9
0
def _make_flag(short_name: str) -> AInixArgument:
    """Shorthand for constructing a flag argument"""
    return AInixArgument(MagicMock(),
                         short_name,
                         None,
                         arg_data={SHORT_NAME: short_name},
                         parent_object_name=short_name + "fooparent")
Ejemplo n.º 10
0
def test_parse_set_optional():
    """Manually build up an ast, and make sure the ast set is representing
    optional args correctly."""
    tc = TypeContext()
    foo_type = AInixType(tc, "FooType")
    test_arg = AInixArgument(tc,
                             "test_arg",
                             "FooType",
                             parent_object_name="foo_ob")
    foo_ob = AInixObject(tc, "foo_ob", "FooType", [test_arg])
    ast_set = AstObjectChoiceSet(foo_type, None)
    ast = ObjectChoiceNode(foo_type)
    next_o = ObjectNode(foo_ob)
    ast.set_choice(next_o)
    arg_choice_node = ObjectChoiceNode(test_arg.present_choice_type)
    arg_choice_node.set_choice(ObjectNode(test_arg.not_present_object))
    next_o.set_arg_value("test_arg", arg_choice_node)

    ast.freeze()
    ast_set.add(ast, True, 1, 1)

    data = ast_set._impl_name_to_data["foo_ob"].next_node.get_arg_set_data(
        next_o.as_childless_node())
    assert data is not None
    assert data.arg_to_choice_set["test_arg"].type_to_choose_name == \
           test_arg.present_choice_type.name
Ejemplo n.º 11
0
def builder_store() -> TorchLatentStore:
    tc = TypeContext()
    ft = AInixType(tc, "FT")
    fo1 = AInixObject(tc, "FO1", "FT")
    fo2 = AInixObject(tc, "FO2", "FT",
                      [AInixArgument(tc, "arg1", "BT", required=True)])
    bt = AInixType(tc, "BT")
    bo1 = AInixObject(tc, "BO1", "BT")
    AInixObject(tc, "BO2", "BT")
    tc.finalize_data()
    builder = TorchLatentStore.get_builder(tc.get_type_count(), 3)
    builder.add_example(0, ObjectChoiceNode(ft, ObjectNode(fo1, pmap())))
    res: TorchLatentStore = builder.produce_result()
    assert res.example_to_depths_to_ind_to_types[0][0] == 0
    builder.add_example(1, ObjectChoiceNode(ft, ObjectNode(fo1, pmap())))
    builder.add_example(
        2,
        ObjectChoiceNode(
            ft,
            ObjectNode(
                fo2,
                pmap({"arg1": ObjectChoiceNode(bt, ObjectNode(bo1,
                                                              pmap()))}))))
    store: TorchLatentStore = builder.produce_result()
    return store
Ejemplo n.º 12
0
def test_string_parse_e2e_pos_unparse(type_context):
    fooType = AInixType(type_context, "FooType")
    fo = AInixObject(
        type_context,
        "fo",
        "FooType", [],
        preferred_object_parser_name=create_object_parser_from_grammar(
            type_context, "fooname", '"foo"').name)
    twoargs = AInixObject(type_context,
                          "FooProgram",
                          "Program", [
                              AInixArgument(type_context,
                                            "p1",
                                            "FooType",
                                            arg_data={
                                                POSITION: 0,
                                                MULTIWORD_POS_ARG: False
                                            },
                                            parent_object_name="bw",
                                            required=True)
                          ],
                          type_data={"invoke_name": "hello"})
    type_context.finalize_data()
    parser = StringParser(type_context)
    ast = parser.create_parse_tree("hello foo", "Program")
    unparser = AstUnparser(type_context)
    unparse_result = unparser.to_string(ast)
    assert unparse_result.total_string == "hello foo"
    for p in ast.depth_first_iter():
        n = p.cur_node
        if isinstance(n,
                      ObjectChoiceNode) and n.type_to_choose.name == "FooType":
            arg_node_pointer = p
            break
    assert unparse_result.pointer_to_string(arg_node_pointer) == "foo"
Ejemplo n.º 13
0
def test_objectnode_copy_with_child():
    """Copy with an arg"""
    # Establish types
    tc = TypeContext()
    AInixType(tc, "footype")
    bartype = AInixType(tc, "bartype")
    arg1 = AInixArgument(tc,
                         "arg1",
                         "bartype",
                         parent_object_name="foo_object")
    foo_object = AInixObject(tc, "foo_object", "footype", [arg1])
    bar_object = AInixObject(tc, "bar_object", "bartype")
    # Make an ast
    fin_choice = ObjectNode(bar_object)
    is_pres = ObjectChoiceNode(bartype)
    is_pres.set_choice(fin_choice)
    arg_node = ObjectNode(arg1.is_present_object)
    arg_node.set_arg_value(OPTIONAL_ARGUMENT_NEXT_ARG_NAME, is_pres)
    is_pres_top = ObjectChoiceNode(arg1.present_choice_type)
    is_pres_top.set_choice(arg_node)
    instance = ObjectNode(foo_object)
    instance.set_arg_value("arg1", is_pres_top)
    # Do the tests:
    # Unfrozen
    clone, leaf = instance.path_clone()
    assert id(clone) != id(instance)
    assert leaf is None
    # Freeze part
    is_pres_top.freeze()
    clone, leaf = instance.path_clone()
    assert id(clone) != id(instance)
    assert not clone.is_frozen
    assert clone == instance
    assert id(clone.get_choice_node_for_arg("arg1")) == id(is_pres_top)
    # Freeze all
    instance.freeze()
    clone, leaf = instance.path_clone()
    assert id(clone) == id(instance)
    assert clone == instance
    assert id(clone.get_choice_node_for_arg("arg1")) == id(is_pres_top)
    # Full unfreeze path
    clone, leaf = instance.path_clone(
        [instance, is_pres_top, arg_node, is_pres, fin_choice])
    assert id(clone) != id(instance)
    assert not clone.is_frozen
    assert clone == instance
    assert leaf.get_nodes_to_here() == [
        instance, is_pres_top, arg_node, is_pres, fin_choice
    ]
    # Partial unfreeze path (stop early)
    clone, leaf = instance.path_clone([instance, is_pres_top, arg_node])
    assert id(clone) != id(instance)
    assert not clone.is_frozen
    assert clone != instance
    path = leaf.get_nodes_to_here()
    assert len(path) == 3
    new_arg_node: ObjectNode = leaf.cur_node
    assert new_arg_node.get_choice_node_for_arg(
        OPTIONAL_ARGUMENT_NEXT_ARG_NAME) is None
def basic_string_tc(basic_classify_tc):
    tc = basic_classify_tc
    AInixType(tc, "FooStringType")
    lhsArg = AInixArgument(tc,
                           "lhs",
                           "FooBarBazType",
                           required=True,
                           parent_object_name="wer")
    rhsArg = AInixArgument(tc,
                           "rhs",
                           "FooStringType",
                           required=False,
                           parent_object_name="sdf")
    AInixObject(tc,
                "foo_string",
                "FooStringType", [lhsArg, rhsArg],
                preferred_object_parser_name=create_object_parser_from_grammar(
                    tc, "itasdf", 'lhs (" " rhs)?').name)
    return tc
Ejemplo n.º 15
0
def test_prog_object_parser_twoargs(type_context):
    twoargs = AInixObject(type_context, "FooProgram", "Program", [
        AInixArgument(type_context,
                      "a",
                      None,
                      arg_data={SHORT_NAME: "a"},
                      parent_object_name="sfd"),
        AInixArgument(type_context,
                      "barg",
                      None,
                      arg_data={SHORT_NAME: "b"},
                      parent_object_name="sfd")
    ])
    parser = type_context.get_object_parser_by_name("ProgramObjectParser")
    result = gen_result(parser.parse_string("-a -b", twoargs))
    assert result.get_arg_present("a") is not None
    assert result.get_arg_present("barg") is not None
    result = gen_result(parser.parse_string("-b", twoargs))
    assert result.get_arg_present("a") is None
    assert result.get_arg_present("barg") is not None
Ejemplo n.º 16
0
def test_prog_object_tostring_basic_with_type(type_context):
    a_arg = AInixArgument(type_context,
                          "a",
                          "Program",
                          arg_data={"short_name": "a"},
                          parent_object_name="sdf")
    onearg = AInixObject(type_context, "FooProgram", "Program", [a_arg])
    parser = type_context.get_object_parser_by_name("ProgramObjectParser")
    # Unparse
    unparse = parser.to_string(ObjectNodeArgMap(onearg, {"a": True}))
    assert unparse.unparse_seq == ["-a", " ", ArgToStringDelegation(a_arg)]
Ejemplo n.º 17
0
def test_prog_object_parser_2posarg(type_context):
    fooType = AInixType(type_context, "FooType")
    argval = AInixObject(type_context, "FooProgram", "Program", [
        AInixArgument(type_context,
                      "p1",
                      fooType.name,
                      arg_data={POSITION: 0},
                      required=True),
        AInixArgument(type_context,
                      "p2",
                      fooType.name,
                      arg_data={POSITION: 1},
                      required=True)
    ])
    parser = type_context.get_object_parser_by_name("ProgramObjectParser")
    result = gen_result(parser.parse_string("hello there", argval))
    assert result.get_arg_present("p1") is not None
    assert result.get_arg_present("p1").slice_string == "hello"
    assert result.get_arg_present("p2") is not None
    assert result.get_arg_present("p2").slice_string == "there"
Ejemplo n.º 18
0
def test_prog_object_parser_basic(type_context):
    onearg = AInixObject(type_context, "FooProgram", "Program", [
        AInixArgument(type_context,
                      "a",
                      None,
                      arg_data={"short_name": "a"},
                      parent_object_name="sdf")
    ])
    parser = type_context.get_object_parser_by_name("ProgramObjectParser")
    result = gen_result(parser.parse_string("-a", onearg))
    assert result.get_arg_present("a") is not None
    assert result.get_arg_present("a").slice_string == ""
Ejemplo n.º 19
0
def test_prog_object_parser_argval_combined_style(type_context):
    fooType = AInixType(type_context, "FooType")
    argval = AInixObject(type_context, "FooProgram", "Program", [
        AInixArgument(type_context,
                      "a",
                      fooType.name,
                      arg_data={"short_name": "a"},
                      parent_object_name="sdf")
    ])
    parser = type_context.get_object_parser_by_name("ProgramObjectParser")
    # Combined style
    result = gen_result(parser.parse_string("-afoo", argval))
    assert result.get_arg_present("a") is not None
    assert result.get_arg_present("a").slice_string == "foo"
Ejemplo n.º 20
0
def test_prog_object_parser_argval(type_context):
    fooType = AInixType(type_context, "FooType")
    argval = AInixObject(type_context, "FooProgram", "Program", [
        AInixArgument(type_context,
                      "a",
                      fooType.name,
                      arg_data={"short_name": "a"},
                      parent_object_name="sdf")
    ])
    parser = type_context.get_object_parser_by_name("ProgramObjectParser")
    result = gen_result(parser.parse_string("-a hello", argval))
    assert result.get_arg_present("a") is not None
    assert result.get_arg_present("a").slice_string == "hello"
    assert result.remaining_start_i == len("-a hello")
Ejemplo n.º 21
0
def _make_positional(name="p1",
                     position: int = 0,
                     multiword: bool = False,
                     required: bool = False) -> AInixArgument:
    """Shorthand for constructing a positional argument"""
    return AInixArgument(MagicMock(),
                         name,
                         "FooType",
                         arg_data={
                             POSITION: position,
                             MULTIWORD_POS_ARG: multiword
                         },
                         required=required,
                         parent_object_name=str(random.randint(1, 999)))
Ejemplo n.º 22
0
def test_short_name_match():
    mock_context = MagicMock()
    result = get_arg_with_short_name([
        AInixArgument(mock_context,
                      "a",
                      None,
                      arg_data={"short_name": "a"},
                      parent_object_name="a")
    ], "a")
    assert result.name == "a"

    result = get_arg_with_short_name([
        AInixArgument(mock_context,
                      "b",
                      None,
                      arg_data={"short_name": "b"},
                      parent_object_name="safd"),
        AInixArgument(mock_context,
                      "a",
                      None,
                      arg_data={"short_name": "a"},
                      parent_object_name="sdf")
    ], "a")
    assert result.name == "a"
Ejemplo n.º 23
0
def test_add_arg():
    mock_context = MagicMock()
    obj = AInixObject(mock_context, "FooObj", "footype", [
        AInixArgument(
            mock_context, "FooArg", None, parent_object_name="FooObj")
    ])
    test_string = "test string"
    result = ObjectParserResult(obj, test_string)

    # Invalid arg
    with pytest.raises(Exception):
        result.set_arg_present("badname", 0, 4)
    with pytest.raises(Exception):
        result.get_arg_present("badname")
    # Valid arg
    result.set_arg_present("FooArg", 0, 4)
    data = result.get_arg_present("FooArg")
    assert data is not None
    assert data.slice == (0, 4)
    assert data.slice_string == test_string[0:4]
    assert result.remaining_start_i == 4
Ejemplo n.º 24
0
 def magic_get_arg_by_name(name):
     new_arg = AInixArgument(MagicMock(),
                             name,
                             None,
                             parent_object_name="sdf")
     return new_arg
Ejemplo n.º 25
0
def test_objectnode_copy_with_2children():
    """Copypasta of the above test, just with an extra arg thrown in"""
    # Establish types
    tc = TypeContext()
    AInixType(tc, "footype")
    bartype = AInixType(tc, "bartype")
    arg1 = AInixArgument(tc, "arg1", "bartype", parent_object_name="foo_obj")
    arg2 = AInixArgument(tc, "arg2", "bartype", parent_object_name="bar_obj")
    foo_object = AInixObject(tc, "foo_object", "footype", [arg1, arg2])
    bar_object = AInixObject(tc, "bar_object", "bartype")
    # Make an ast
    fin_choice = ObjectNode(bar_object)
    is_pres = ObjectChoiceNode(bartype)
    is_pres.set_choice(fin_choice)
    arg_node = ObjectNode(arg1.is_present_object)
    arg_node.set_arg_value(OPTIONAL_ARGUMENT_NEXT_ARG_NAME, is_pres)
    is_pres_top = ObjectChoiceNode(arg1.present_choice_type)
    is_pres_top.set_choice(arg_node)
    instance = ObjectNode(foo_object)
    instance.set_arg_value("arg1", is_pres_top)

    fin_choice2 = ObjectNode(bar_object)
    is_pres2 = ObjectChoiceNode(bartype)
    is_pres2.set_choice(fin_choice2)
    arg_node2 = ObjectNode(arg2.is_present_object)
    arg_node2.set_arg_value(OPTIONAL_ARGUMENT_NEXT_ARG_NAME, is_pres2)
    is_pres_top2 = ObjectChoiceNode(arg2.present_choice_type)
    is_pres_top2.set_choice(arg_node2)
    instance.set_arg_value("arg2", is_pres_top2)
    # Do the tests:
    # Unfrozen
    clone, leaf_pointer = instance.path_clone()
    assert id(clone) != id(instance)
    assert clone == instance
    # Freeze part
    is_pres_top.freeze()
    clone, leaf_pointer = instance.path_clone()
    assert id(clone) != id(instance)
    assert not clone.is_frozen
    assert clone == instance
    assert id(clone.get_choice_node_for_arg("arg1")) == id(is_pres_top)
    assert id(clone.get_choice_node_for_arg("arg2")) != id(is_pres_top2)
    # Freeze all
    instance.freeze()
    clone, leaf_pointer = instance.path_clone()
    assert id(clone) == id(instance)
    assert clone == instance
    assert id(clone.get_choice_node_for_arg("arg1")) == id(is_pres_top)
    assert id(clone.get_choice_node_for_arg("arg2")) == id(is_pres_top2)
    # Full unfreeze path
    clone, leaf_pointer = instance.path_clone(
        [instance, is_pres_top, arg_node, is_pres, fin_choice])
    assert id(clone) != id(instance)
    assert not clone.is_frozen
    assert clone == instance
    assert leaf_pointer.get_nodes_to_here() == \
           [instance, is_pres_top, arg_node, is_pres, fin_choice]
    assert id(clone.get_choice_node_for_arg("arg2")) == id(is_pres_top2)
    assert clone.get_choice_node_for_arg("arg2").is_frozen
    # Partial unfreeze path (stop early)
    clone, leaf_pointer = instance.path_clone(
        [instance, is_pres_top, arg_node])
    assert id(clone) != id(instance)
    assert not clone.is_frozen
    assert clone != instance
    assert len(leaf_pointer.get_nodes_to_here()) == 3
    new_arg_node: ObjectNode = leaf_pointer.cur_node
    assert new_arg_node.get_choice_node_for_arg(
        OPTIONAL_ARGUMENT_NEXT_ARG_NAME) is None
    assert new_arg_node.get_choice_node_for_arg(
        OPTIONAL_ARGUMENT_NEXT_ARG_NAME) is None
    assert clone.get_choice_node_for_arg("arg2") == is_pres_top2
    assert id(clone.get_choice_node_for_arg("arg2")) == id(is_pres_top2)
Ejemplo n.º 26
0
def test_multi_copy():
    tc = TypeContext()
    loader.load_path(f"builtin_types/generic_parsers.ainix.yaml",
                     tc,
                     up_search_limit=3)
    ft = AInixType(tc, "ft")
    bt = AInixType(tc, "bt", default_type_parser_name="max_munch_type_parser")
    arg1 = AInixArgument(tc,
                         "lhs",
                         "bt",
                         required=True,
                         parent_object_name="fo")
    arg2 = AInixArgument(tc,
                         "right",
                         "bt",
                         required=True,
                         parent_object_name="sg")
    fo = AInixObject(
        tc,
        "fo",
        "ft", [arg1, arg2],
        preferred_object_parser_name=create_object_parser_from_grammar(
            tc, "mp", 'lhs right').name)
    bfoo = AInixObject(
        tc,
        "bfoo",
        "bt",
        None,
        preferred_object_parser_name=create_object_parser_from_grammar(
            tc, "masdfo_parser", '"foo"').name)
    bbar = AInixObject(
        tc,
        "bbar",
        "bt",
        None,
        preferred_object_parser_name=create_object_parser_from_grammar(
            tc, "mdf", '"bar"').name)
    tc.finalize_data()

    parser = StringParser(tc)
    unparser = AstUnparser(tc)
    ast = parser.create_parse_tree("foofoo", "ft")
    tokenizer = SpaceTokenizer()
    in_str = "Hello foo"
    tokens, metadata = tokenizer.tokenize(in_str)
    unpar_res = unparser.to_string(ast)
    assert unpar_res.total_string == "foofoo"
    cset = AstObjectChoiceSet(ft)
    cset.add(ast, True, 1, 1)
    assert cset.is_node_known_valid(ast)
    add_copies_to_ast_set(ast, cset, unparser, metadata)
    copy_left = ObjectChoiceNode(
        ft,
        ObjectNode(
            fo,
            pmap({
                "lhs": ObjectChoiceNode(bt, CopyNode(bt, 1, 1)),
                "right": ObjectChoiceNode(bt, ObjectNode(bfoo, pmap()))
            })))
    assert cset.is_node_known_valid(copy_left)
    copy_right = ObjectChoiceNode(
        ft,
        ObjectNode(
            fo,
            pmap({
                "lhs": ObjectChoiceNode(bt, ObjectNode(bfoo, pmap())),
                "right": ObjectChoiceNode(bt, CopyNode(bt, 1, 1))
            })))
    assert cset.is_node_known_valid(copy_right)
    copy_both = ObjectChoiceNode(
        ft,
        ObjectNode(
            fo,
            pmap({
                "lhs": ObjectChoiceNode(bt, CopyNode(bt, 1, 1)),
                "right": ObjectChoiceNode(bt, CopyNode(bt, 1, 1))
            })))
    assert cset.is_node_known_valid(copy_both)
Ejemplo n.º 27
0
def _create_word_part_obj(tc: TypeContext, symb: str,
                          allow_modifier: bool) -> AInixObject:
    """Creates an individual word part object with the proper parsers"""
    if allow_modifier:

        def modifier_type_unparser(
                result: parse_primitives.TypeToStringResult):
            impl = result.implementation
            if impl.name == MODIFIER_LOWER_NAME:
                result.add_string(symb)
            elif impl.name == MODIFIER_ALL_UPPER:
                result.add_string(symb.upper())
            elif impl.name == MODIFIER_FIRST_CAP_NAME:
                result.add_string(symb[0].upper() + symb[1:])
            else:
                raise parse_primitives.AInixParseError(
                    f"Unexpected impl {impl.name}")
            result.add_impl_unparse()

        mod_tp_name = f"modifier_parser_for_{symb}"
        TypeParser(tc, mod_tp_name, mod_type_parser_func,
                   modifier_type_unparser)

        children = [
            AInixArgument(tc,
                          WORD_PART_MODIFIER_ARG_NAME,
                          "GenericWordPartModifier",
                          required=True,
                          type_parser_name=mod_tp_name)
        ]
    else:
        children = []
    children += [
        AInixArgument(tc,
                      WORD_PART_NEXT_ARG_NAME,
                      WORD_PART_TYPE_NAME,
                      required=True)
    ]

    def parser(run: parse_primitives.ObjectParserRun, string: str,
               result: parse_primitives.ObjectParserResult):
        if not string.lower().startswith(symb):
            raise parse_primitives.AInixParseError(
                f"Expected string {string} to start with {symb}")
        if allow_modifier:
            mod_arg = run.all_arguments[0]
            next_arg = run.all_arguments[1]
            deleg = yield run.left_fill_arg(mod_arg, (0, len(symb)))
            result.accept_delegation(deleg)
        else:
            next_arg = run.all_arguments[0]
        deleg = yield run.left_fill_arg(next_arg, (len(symb), len(string)))
        result.accept_delegation(deleg)

    def unparser(arg_map: parse_primitives.ObjectNodeArgMap,
                 result: parse_primitives.ObjectToStringResult):
        if allow_modifier:
            result.add_argname_tostring(WORD_PART_MODIFIER_ARG_NAME)
        else:
            result.add_string(symb)
        result.add_argname_tostring(WORD_PART_NEXT_ARG_NAME)

    p = ObjectParser(tc, f"parser_for_word_part_{symb}", parser, unparser)
    part = AInixObject(tc, _name_for_word_part(symb), WORD_PART_TYPE_NAME,
                       children, p.name)
    return part