|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# groovy-parser, a proof of concept Groovy parser based on Pygments and Lark |
| 6 | +# Copyright (C) 2024 Barcelona Supercomputing Center, José M. Fernández |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | + |
| 20 | +import json |
| 21 | +import logging |
| 22 | +import os |
| 23 | +import re |
| 24 | +import sys |
| 25 | + |
| 26 | +from typing import ( |
| 27 | + cast, |
| 28 | + NamedTuple, |
| 29 | + TYPE_CHECKING, |
| 30 | +) |
| 31 | + |
| 32 | +if TYPE_CHECKING: |
| 33 | + from typing import ( |
| 34 | + IO, |
| 35 | + Iterator, |
| 36 | + MutableSequence, |
| 37 | + Optional, |
| 38 | + Sequence, |
| 39 | + Tuple, |
| 40 | + Union, |
| 41 | + ) |
| 42 | + |
| 43 | + from groovy_parser.parser import ( |
| 44 | + EmptyNode, |
| 45 | + LeafNode, |
| 46 | + RuleNode, |
| 47 | + ) |
| 48 | + |
| 49 | +from pygments.token import Token |
| 50 | + |
| 51 | +from groovy_parser.parser import ( |
| 52 | + parse_groovy_content, |
| 53 | + digest_lark_tree, |
| 54 | +) |
| 55 | + |
| 56 | +from lark import ( |
| 57 | + Lark, |
| 58 | + Transformer, |
| 59 | + v_args, |
| 60 | +) |
| 61 | +from lark.visitors import Discard |
| 62 | + |
| 63 | +prev_wants_space = False |
| 64 | + |
| 65 | + |
| 66 | +def write_groovy( |
| 67 | + t_tree: "Union[RuleNode, LeafNode, EmptyNode]", |
| 68 | + mH: "IO[str]", |
| 69 | + reset_prev_wants_space: "bool" = False, |
| 70 | +) -> None: |
| 71 | + global prev_wants_space |
| 72 | + if reset_prev_wants_space: |
| 73 | + prev_wants_space = False |
| 74 | + children = cast("RuleNode", t_tree).get("children") |
| 75 | + if children is not None: |
| 76 | + for child in children: |
| 77 | + write_groovy(child, mH=mH) |
| 78 | + else: |
| 79 | + leaf = cast("LeafNode", t_tree).get("leaf") |
| 80 | + value = cast("LeafNode", t_tree).get("value") |
| 81 | + if value is not None and leaf is not None: |
| 82 | + wants_space = False |
| 83 | + print(f"Leaf {leaf} value {value}") |
| 84 | + if prev_wants_space and leaf in ( |
| 85 | + "STRING_LITERAL", |
| 86 | + "IDENTIFIER", |
| 87 | + "CAPITALIZED_IDENTIFIER", |
| 88 | + "LBRACE", |
| 89 | + "GSTRING_BEGIN", |
| 90 | + ): |
| 91 | + # These whitespace separators were silenced |
| 92 | + mH.write(" ") |
| 93 | + |
| 94 | + if leaf == "STRING_LITERAL": |
| 95 | + mH.write("'") |
| 96 | + |
| 97 | + mH.write(value) |
| 98 | + if leaf in ("IDENTIFIER", "CAPITALIZED_IDENTIFIER", "RBRACE", "COMMA"): |
| 99 | + wants_space = True |
| 100 | + elif leaf == "STRING_LITERAL": |
| 101 | + mH.write("'") |
| 102 | + |
| 103 | + prev_wants_space = wants_space |
| 104 | + |
| 105 | + |
| 106 | +def mirror_groovy_source( |
| 107 | + filename: "str", jsonfile: "str", mirror_filename: "str" |
| 108 | +) -> "Union[RuleNode, LeafNode, EmptyNode]": |
| 109 | + with open(filename, mode="r", encoding="utf-8") as wfH: |
| 110 | + content = wfH.read() |
| 111 | + |
| 112 | + tree = parse_groovy_content(content) |
| 113 | + |
| 114 | + t_tree = digest_lark_tree(tree, prune=[]) |
| 115 | + |
| 116 | + # These are for debugging purposes |
| 117 | + # logging.debug(tree.pretty()) |
| 118 | + # with open(jsonfile, mode="w", encoding="utf-8") as jH: |
| 119 | + # json.dump(tree, jH, indent=4, cls=LarkFilteringTreeEncoder) |
| 120 | + with open(jsonfile, mode="w", encoding="utf-8") as jH: |
| 121 | + json.dump(t_tree, jH, indent=4) |
| 122 | + |
| 123 | + with open(mirror_filename, mode="w", encoding="utf-8") as mH: |
| 124 | + write_groovy(t_tree, mH, reset_prev_wants_space=True) |
| 125 | + |
| 126 | + return t_tree |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + logging.basicConfig( |
| 131 | + level=logging.DEBUG, |
| 132 | + ) |
| 133 | + log = logging.getLogger() # root logger |
| 134 | + for filename in sys.argv[1:]: |
| 135 | + print(f"* Parsing {filename}") |
| 136 | + logfile = filename + ".lark" |
| 137 | + jsonfile = logfile + ".json" |
| 138 | + mirrored_filename = filename + ".mirrored" |
| 139 | + fH = logging.FileHandler(logfile, mode="w", encoding="utf-8") |
| 140 | + for hdlr in log.handlers[:]: # remove all old handlers |
| 141 | + log.removeHandler(hdlr) |
| 142 | + log.addHandler(fH) # set the new handler |
| 143 | + try: |
| 144 | + mirror_groovy_source(filename, jsonfile, mirrored_filename) |
| 145 | + except Exception as e: |
| 146 | + print(f"\tParse failed, see {logfile}") |
| 147 | + logging.exception("Parse failed") |
| 148 | + fH.close() |
0 commit comments