def test_max_munch(): tc = TypeContext() loader.load_path("builtin_types/generic_parsers.ainix.yaml", tc, up_search_limit=3) foo_type = "MMTestType" AInixType(tc, foo_type, default_type_parser_name="max_munch_type_parser") def make_mock_with_parse_rep(representation: str): loader._load_object( { "name": representation, "type": foo_type, "preferred_object_parser": { "grammar": f'"{representation}"' } }, tc, "foopathsdf") assert tc.get_object_by_name( representation).preferred_object_parser_name is not None objects = [ make_mock_with_parse_rep(rep) for rep in ("fo", "bar", "f", "foo", "foot", 'baz') ] parser = StringParser(tc) ast = parser.create_parse_tree("foobar", foo_type, allow_partial_consume=True) assert ast.next_node_not_copy.implementation.name == "foo"
def fac(): type_context = TypeContext() for tf in type_files: loader.load_path(tf, type_context) type_context.finalize_data() index = ainix_kernel.indexing.exampleindex.ExamplesIndex(type_context) for ef in example_files: exampleloader.load_path(ef, index) return index
def numbers_type_context(): type_context = TypeContext() loader.load_path(f"builtin_types/generic_parsers.ainix.yaml", type_context, up_search_limit=3) loader.load_path(f"builtin_types/numbers.ainix.yaml", type_context, up_search_limit=3) type_context.finalize_data() return type_context
def get_toy_strings_context() -> TypeContext: context = TypeContext() loader.load_path(f"builtin_types/generic_parsers.ainix.yaml", context, up_search_limit=4) loader.load_path(f"{LOAD_PATH_ROOT}/twostr.ainix.yaml", context, up_search_limit=4) context.finalize_data() return context
def tc(): context = TypeContext() loader.load_path("builtin_types/paths.ainix.yaml", context, up_search_limit=2) loader.load_path("builtin_types/generic_parsers.ainix.yaml", context, up_search_limit=2) generic_strings.create_generic_strings(context) context.finalize_data() return context
def test_partial_copy_numbers(): tc = TypeContext() loader.load_path(f"builtin_types/generic_parsers.ainix.yaml", tc, up_search_limit=3) loader.load_path(f"builtin_types/numbers.ainix.yaml", tc, up_search_limit=3) tc.finalize_data() parser = StringParser(tc) tokenizer = NonLetterTokenizer() unparser = AstUnparser(tc, tokenizer) ast = parser.create_parse_tree("1000", "Number")
def test_generic_word(): context = TypeContext() loader.load_path("builtin_types/generic_parsers.ainix.yaml", context, up_search_limit=4) generic_strings.create_generic_strings(context) context.finalize_data() parser = StringParser(context) ast = parser.create_parse_tree("a", WORD_TYPE_NAME) generic_word_ob = ast.next_node_not_copy assert generic_word_ob.implementation.name == WORD_OBJ_NAME parts_arg = generic_word_ob.get_choice_node_for_arg("parts") parts_v = parts_arg.next_node_not_copy assert parts_v.implementation.name == "word_part_a" mod_type_choice = parts_v.get_choice_node_for_arg( WORD_PART_MODIFIER_ARG_NAME) mod_type_object = mod_type_choice.next_node_not_copy assert mod_type_object.implementation.name == MODIFIER_LOWER_NAME unparser = AstUnparser(context) result = unparser.to_string(ast) assert result.total_string == "a"
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)
def numbers_context(base_type_context): loader.load_path(f"{BUILTIN_TYPES_PATH}/numbers.ainix.yaml", base_type_context, 4) base_type_context.finalize_data() return base_type_context
def base_type_context(): type_context = TypeContext() loader.load_path(f"{BUILTIN_TYPES_PATH}/generic_parsers.ainix.yaml", type_context, 4) return type_context
def type_context(): context = TypeContext() import os dirname, filename = os.path.split(os.path.abspath(__file__)) loader.load_path(f"{dirname}/command.ainix.yaml", context) return context