(
input_text: str, variables: Dict[str, Any], input_path: str = "codegen"
)
| 583 | |
| 584 | # https://github.com/google/XNNPACK/blob/master/tools/xngen.py |
| 585 | def preprocess( |
| 586 | input_text: str, variables: Dict[str, Any], input_path: str = "codegen" |
| 587 | ) -> str: |
| 588 | # Workaround to handle source files using \ to extend mecros to a new line |
| 589 | input_text = re.sub(r"\\$", r"\\\\", input_text, flags=re.MULTILINE) |
| 590 | |
| 591 | input_lines = input_text.splitlines() |
| 592 | python_lines = [] |
| 593 | |
| 594 | blank_lines = 0 |
| 595 | |
| 596 | last_indent = "" |
| 597 | |
| 598 | # List of tuples (total_index, python_indent) |
| 599 | indent_stack = [("", "")] |
| 600 | |
| 601 | # Indicates whether this is the first line inside Python |
| 602 | # code block (i.e. for, while, if, elif, else) |
| 603 | python_block_start = True |
| 604 | for input_line in input_lines: |
| 605 | if input_line == "": |
| 606 | blank_lines += 1 |
| 607 | continue |
| 608 | # Skip lint markers. |
| 609 | if "LINT" in input_line: |
| 610 | continue |
| 611 | |
| 612 | input_indent = extract_leading_whitespace(input_line) |
| 613 | if python_block_start: |
| 614 | assert input_indent.startswith(last_indent) |
| 615 | extra_python_indent = input_indent[len(last_indent) :] |
| 616 | python_indent = indent_stack[-1][1] + extra_python_indent |
| 617 | indent_stack.append((input_indent, python_indent)) |
| 618 | assert input_indent.startswith(indent_stack[-1][0]) |
| 619 | else: |
| 620 | while not input_indent.startswith(indent_stack[-1][0]): |
| 621 | del indent_stack[-1] |
| 622 | python_block_start = False |
| 623 | |
| 624 | python_indent = indent_stack[-1][1] |
| 625 | stripped_input_line = input_line.strip() |
| 626 | if stripped_input_line.startswith("$") and not stripped_input_line.startswith( |
| 627 | "${" |
| 628 | ): |
| 629 | if stripped_input_line.endswith(":"): |
| 630 | python_block_start = True |
| 631 | while blank_lines != 0: |
| 632 | python_lines.append(python_indent + "print(file=OUT_STREAM)") |
| 633 | blank_lines -= 1 |
| 634 | python_lines.append(python_indent + stripped_input_line.replace("$", "")) |
| 635 | else: |
| 636 | assert input_line.startswith(python_indent) |
| 637 | while blank_lines != 0: |
| 638 | python_lines.append(python_indent + "print(file=OUT_STREAM)") |
| 639 | blank_lines -= 1 |
| 640 | python_lines.append( |
| 641 | python_indent |
| 642 | + "print(%s, file=OUT_STREAM)" |
no test coverage detected