예제 #1
0
def test_parsing_failure(gdscript_nok_path):
    with open(gdscript_nok_path, "r") as fh:
        code = fh.read()
        try:
            parser.parse(code)
        except:  # pylint: disable=bare-except
            return
        raise Exception("shall fail")
예제 #2
0
def _tree_invariant_check(input_code, formatted_code):
    input_code_parse_tree = parser.parse(input_code, loosen_grammar=True)
    formatted_code_parse_tree = parser.parse(formatted_code,
                                             loosen_grammar=True)
    diff = "\n".join(
        difflib.unified_diff(
            str(input_code_parse_tree.pretty()).splitlines(),
            str(formatted_code_parse_tree.pretty()).splitlines(),
        ))
    assert input_code_parse_tree == formatted_code_parse_tree, diff
예제 #3
0
def format_and_compare(input_code, expected_output_code):
    input_code_parse_tree = parser.parse(input_code, loosen_grammar=True)
    formatted_code = format_code(input_code, max_line_length=MAX_LINE_LENGTH)
    formatted_code_parse_tree = parser.parse(formatted_code,
                                             loosen_grammar=True)
    _invariant_check(input_code_parse_tree, formatted_code_parse_tree)
    _compare(formatted_code, expected_output_code)
    input_code_comments = _gather_comment_statistics_from_code(input_code)
    formatted_code_comments = _gather_comment_statistics_from_code(
        formatted_code)
    _comment_preservation_check(input_code_comments, formatted_code_comments)
예제 #4
0
def _tree_invariant_check(input_code, formatted_code):
    input_code_parse_tree = parser.parse(input_code)
    formatted_code_parse_tree = parser.parse(formatted_code)
    loosen_tree_transformer = LoosenTreeTransformer()
    input_code_parse_tree = loosen_tree_transformer.transform(
        input_code_parse_tree)
    formatted_code_parse_tree = loosen_tree_transformer.transform(
        formatted_code_parse_tree)
    diff = "\n".join(
        difflib.unified_diff(
            str(input_code_parse_tree.pretty()).splitlines(),
            str(formatted_code_parse_tree.pretty()).splitlines(),
        ))
    assert input_code_parse_tree == formatted_code_parse_tree, diff
예제 #5
0
def main():
    arguments = docopt(
        __doc__,
        version="gdparse {}".format(
            pkg_resources.get_distribution("gdtoolkit").version),
    )

    if not isinstance(arguments, dict):
        print(arguments)
        sys.exit(0)

    for file_path in arguments["<file>"]:
        with open(file_path, "r") as fh:  # TODO: handle exception
            content = fh.read()
            tree = parser.parse(content)  # TODO: handle exception
            if arguments["--pretty"]:
                print(tree.pretty())
            elif arguments["--verbose"]:
                print(tree)
예제 #6
0
def main():
    arguments = docopt(
        __doc__,
        version="gdformat {}".format(
            pkg_resources.get_distribution("gdtoolkit").version),
    )
    files: List[str] = find_files_from(arguments["<path>"])
    print(files)

    line_length = int(arguments["--line-length"])
    if files == ["-"]:
        code = sys.stdin.read()
        code_parse_tree = parser.parse(code, gather_metadata=True)
        comment_parse_tree = parser.parse_comments(code)
        formatted_code = format_code(
            gdscript_code=code,
            max_line_length=line_length,
            parse_tree=code_parse_tree,
            comment_parse_tree=comment_parse_tree,
        )
        check_formatting_safety(
            code,
            formatted_code,
            max_line_length=line_length,
            given_code_parse_tree=code_parse_tree,
            given_code_comment_parse_tree=comment_parse_tree,
        )
        print(formatted_code, end="")
    elif arguments["--check"]:
        formattable_files = set()
        for file_path in files:
            with open(file_path, "r") as fh:
                code = fh.read()
                try:
                    code_parse_tree = parser.parse(code, gather_metadata=True)
                    comment_parse_tree = parser.parse_comments(code)
                    formatted_code = format_code(
                        gdscript_code=code,
                        max_line_length=line_length,
                        parse_tree=code_parse_tree,
                        comment_parse_tree=comment_parse_tree,
                    )
                except Exception as e:
                    print(
                        "exception during formatting of {}".format(file_path),
                        file=sys.stderr,
                    )
                    raise e
                if code != formatted_code:
                    print("would reformat {}".format(file_path),
                          file=sys.stderr)
                    try:
                        check_formatting_safety(
                            code,
                            formatted_code,
                            max_line_length=line_length,
                            given_code_parse_tree=code_parse_tree,
                            given_code_comment_parse_tree=comment_parse_tree,
                        )
                    except Exception as e:
                        print(
                            "exception during formatting of {}".format(
                                file_path),
                            file=sys.stderr,
                        )
                        raise e
                    formattable_files.add(file_path)
        if len(formattable_files) == 0:
            print("{} file{} would be left unchanged".format(
                len(files),
                "s" if len(files) != 1 else "",
            ))
            sys.exit(0)
        formattable_num = len(formattable_files)
        left_unchanged_num = len(files) - formattable_num
        print(
            "{} file{} would be reformatted, {} file{} would be left unchanged."
            .format(
                formattable_num,
                "s" if formattable_num != 1 else "",
                left_unchanged_num,
                "s" if left_unchanged_num != 1 else "",
            ),
            file=sys.stderr,
        )
        sys.exit(1)
    else:
        formatted_files = set()
        for file_path in files:
            with open(file_path, "r+") as fh:
                code = fh.read()
                try:
                    code_parse_tree = parser.parse(code, gather_metadata=True)
                    comment_parse_tree = parser.parse_comments(code)
                    formatted_code = format_code(
                        gdscript_code=code,
                        max_line_length=line_length,
                        parse_tree=code_parse_tree,
                        comment_parse_tree=comment_parse_tree,
                    )
                except Exception as e:
                    print(
                        "exception during formatting of {}".format(file_path),
                        file=sys.stderr,
                    )
                    raise e
                if code != formatted_code:
                    try:
                        check_formatting_safety(
                            code,
                            formatted_code,
                            max_line_length=line_length,
                            given_code_parse_tree=code_parse_tree,
                            given_code_comment_parse_tree=comment_parse_tree,
                        )
                    except Exception as e:
                        print(
                            "exception during formatting of {}".format(
                                file_path),
                            file=sys.stderr,
                        )
                        raise e
                    print("reformatted {}".format(file_path))
                    formatted_files.add(file_path)
                    fh.seek(0)
                    fh.truncate(0)
                    fh.write(formatted_code)
        reformatted_num = len(formatted_files)
        left_unchanged_num = len(files) - reformatted_num
        print("{} file{} reformatted, {} file{} left unchanged.".format(
            reformatted_num,
            "s" if reformatted_num != 1 else "",
            left_unchanged_num,
            "s" if left_unchanged_num != 1 else "",
        ))
예제 #7
0
def test_parsing_success(gdscript_ok_path):
    with open(gdscript_ok_path, "r") as fh:
        code = fh.read()
        parser.parse(code)  # just checking if not throwing