How to use the gdtoolkit.parser.parser function in gdtoolkit

To help you get started, we’ve selected a few gdtoolkit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Scony / godot-gdscript-toolkit / tests / formatter / common.py View on Github external
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
github Scony / godot-gdscript-toolkit / tests / test_parser.py View on Github external
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")
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / formatter.py View on Github external
def format_code(
    gdscript_code: str,
    max_line_length: int,
    parse_tree: Optional[Tree] = None,
    comment_parse_tree: Optional[Tree] = None,
) -> str:
    parse_tree = (
        parse_tree
        if parse_tree is not None
        else parser.parse(gdscript_code, gather_metadata=True)
    )
    comment_parse_tree = (
        comment_parse_tree
        if comment_parse_tree is not None
        else parser.parse_comments(gdscript_code)
    )
    gdscript_code_lines = [
        "",
        *gdscript_code.splitlines(),
    ]  # type: List[str]
    formatted_lines = []  # type: FormattedLines
    context = Context(
        indent=0,
        previously_processed_line_number=0,
        max_line_length=max_line_length,
        gdscript_code_lines=gdscript_code_lines,
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / __init__.py View on Github external
def check_formatting_safety(
    given_code: str,
    formatted_code: str,
    max_line_length: int,
    given_code_parse_tree: Optional[Tree] = None,
    given_code_comment_parse_tree: Optional[Tree] = None,
) -> None:
    if given_code == formatted_code:
        return
    formatted_code_parse_tree = parser.parse(formatted_code, gather_metadata=True)
    formatted_code_comment_parse_tree = parser.parse_comments(formatted_code)
    check_comment_persistence(
        given_code,
        formatted_code,
        given_code_comment_parse_tree=given_code_comment_parse_tree,
        formatted_code_comment_parse_tree=formatted_code_comment_parse_tree,
    )
    check_tree_invariant(
        given_code,
        formatted_code,
        given_code_parse_tree=given_code_parse_tree,
        formatted_code_parse_tree=formatted_code_parse_tree,
    )
    check_formatting_stability(
        formatted_code,
        max_line_length,
github Scony / godot-gdscript-toolkit / gdtoolkit / linter / __init__.py View on Github external
def lint_code(
    gdscript_code: str, config: MappingProxyType = DEFAULT_CONFIG
) -> List[Problem]:
    parse_tree = parser.parse(gdscript_code, gather_metadata=True)
    problems = design_checks.lint(parse_tree, config)
    problems += format_checks.lint(gdscript_code, config)
    problems += name_checks.lint(parse_tree, config)
    problems += class_checks.lint(parse_tree, config)
    problems += basic_checks.lint(parse_tree, config)
    return problems
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / safety_checks.py View on Github external
def check_tree_invariant(
    given_code: str,
    formatted_code: str,
    given_code_parse_tree: Optional[Tree] = None,
    formatted_code_parse_tree: Optional[Tree] = None,
) -> None:
    given_code_parse_tree = (
        given_code_parse_tree
        if given_code_parse_tree is not None
        else parser.parse(given_code)
    )
    formatted_code_parse_tree = (
        formatted_code_parse_tree
        if formatted_code_parse_tree is not None
        else parser.parse(formatted_code)
    )
    loosen_tree_transformer = LoosenTreeTransformer()
    given_code_parse_tree = loosen_tree_transformer.transform(given_code_parse_tree)
    formatted_code_parse_tree = loosen_tree_transformer.transform(
        formatted_code_parse_tree
    )
    if given_code_parse_tree != formatted_code_parse_tree:
        raise TreeInvariantViolation
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / safety_checks.py View on Github external
def check_tree_invariant(
    given_code: str,
    formatted_code: str,
    given_code_parse_tree: Optional[Tree] = None,
    formatted_code_parse_tree: Optional[Tree] = None,
) -> None:
    given_code_parse_tree = (
        given_code_parse_tree
        if given_code_parse_tree is not None
        else parser.parse(given_code)
    )
    formatted_code_parse_tree = (
        formatted_code_parse_tree
        if formatted_code_parse_tree is not None
        else parser.parse(formatted_code)
    )
    loosen_tree_transformer = LoosenTreeTransformer()
    given_code_parse_tree = loosen_tree_transformer.transform(given_code_parse_tree)
    formatted_code_parse_tree = loosen_tree_transformer.transform(
        formatted_code_parse_tree
    )
    if given_code_parse_tree != formatted_code_parse_tree:
        raise TreeInvariantViolation
github Scony / godot-gdscript-toolkit / gdtoolkit / formatter / __main__.py View on Github external
)
        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: