-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example about how to write back the parsed tree, as requested a…
…t issue #3
- Loading branch information
1 parent
19cad56
commit 2a9d0b4
Showing
6 changed files
with
209 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,11 @@ jobs: | |
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: [ "3.7", "3.8", "3.9", "3.10", "3.11" ] | ||
python-version: [ "3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13" ] | ||
name: Pre-commit python ${{ matrix.python-version }} | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-python@v4 | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-python@v5 | ||
id: cachepy | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
@@ -34,17 +34,17 @@ jobs: | |
- name: 'Install dev requirements' | ||
run: pip install -r dev-requirements.txt -r mypy-requirements.txt | ||
- name: MyPy cache | ||
uses: actions/cache@v3 | ||
uses: actions/cache@v4 | ||
with: | ||
path: .mypy_cache/${{ matrix.python-version }} | ||
key: mypy-${{ matrix.python-version }} | ||
- name: 'pre-commit' | ||
uses: pre-commit/[email protected].0 | ||
uses: pre-commit/[email protected].1 | ||
# if: ${{ matrix.python-version != '3.6' }} | ||
with: | ||
extra_args: --all -c .pre-commit-config.yaml | ||
# - name: 'pre-commit (custom Python ${{ matrix.python-version }})' | ||
# uses: pre-commit/[email protected].0 | ||
# uses: pre-commit/[email protected].1 | ||
# if: ${{ matrix.python-version == '3.6' }} | ||
# with: | ||
# extra_args: --all -c .pre-commit-config-gh-${{ matrix.python-version }}.yaml | ||
|
@@ -60,7 +60,7 @@ jobs: | |
- name: Print licences report | ||
if: ${{ always() }} | ||
run: echo "${{ steps.license_check_report.outputs.report }}" | ||
- uses: actions/upload-artifact@v3 | ||
- uses: actions/upload-artifact@v4 | ||
with: | ||
retention-days: 2 | ||
path: constraints-${{ matrix.python-version }}.txt | ||
|
@@ -71,8 +71,8 @@ jobs: | |
needs: | ||
- pre-commit | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/download-artifact@v3 | ||
- uses: actions/checkout@v4 | ||
- uses: actions/download-artifact@v4 | ||
with: | ||
path: changes-dir | ||
- name: Move artifacts to their right place | ||
|
@@ -81,7 +81,7 @@ jobs: | |
rm -r changes-dir/artifact | ||
- name: Create Pull Request | ||
id: cpr | ||
uses: peter-evans/create-pull-request@v4 | ||
uses: peter-evans/create-pull-request@v7 | ||
with: | ||
title: Updated constraints (triggered by ${{ github.sha }}) | ||
delete-branch: true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
# SPDX-License-Identifier: Apache-2.0 | ||
# groovy-parser, a proof of concept Groovy parser based on Pygments and Lark | ||
# Copyright (C) 2024 Barcelona Supercomputing Center, José M. Fernández | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import json | ||
import logging | ||
import os | ||
import re | ||
import sys | ||
|
||
from typing import ( | ||
cast, | ||
NamedTuple, | ||
TYPE_CHECKING, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from typing import ( | ||
IO, | ||
Iterator, | ||
MutableSequence, | ||
Optional, | ||
Sequence, | ||
Tuple, | ||
Union, | ||
) | ||
|
||
from groovy_parser.parser import ( | ||
EmptyNode, | ||
LeafNode, | ||
RuleNode, | ||
) | ||
|
||
from pygments.token import Token | ||
|
||
from groovy_parser.parser import ( | ||
parse_groovy_content, | ||
digest_lark_tree, | ||
) | ||
|
||
from lark import ( | ||
Lark, | ||
Transformer, | ||
v_args, | ||
) | ||
from lark.visitors import Discard | ||
|
||
prev_wants_space = False | ||
|
||
|
||
def write_groovy( | ||
t_tree: "Union[RuleNode, LeafNode, EmptyNode]", | ||
mH: "IO[str]", | ||
reset_prev_wants_space: "bool" = False, | ||
) -> None: | ||
global prev_wants_space | ||
if reset_prev_wants_space: | ||
prev_wants_space = False | ||
children = cast("RuleNode", t_tree).get("children") | ||
if children is not None: | ||
for child in children: | ||
write_groovy(child, mH=mH) | ||
else: | ||
leaf = cast("LeafNode", t_tree).get("leaf") | ||
value = cast("LeafNode", t_tree).get("value") | ||
if value is not None and leaf is not None: | ||
wants_space = False | ||
print(f"Leaf {leaf} value {value}") | ||
if prev_wants_space and leaf in ( | ||
"STRING_LITERAL", | ||
"IDENTIFIER", | ||
"CAPITALIZED_IDENTIFIER", | ||
"LBRACE", | ||
"GSTRING_BEGIN", | ||
): | ||
# These whitespace separators were silenced | ||
mH.write(" ") | ||
|
||
if leaf == "STRING_LITERAL": | ||
mH.write("'") | ||
|
||
mH.write(value) | ||
if leaf in ("IDENTIFIER", "CAPITALIZED_IDENTIFIER", "RBRACE", "COMMA"): | ||
wants_space = True | ||
elif leaf == "STRING_LITERAL": | ||
mH.write("'") | ||
|
||
prev_wants_space = wants_space | ||
|
||
|
||
def mirror_groovy_source( | ||
filename: "str", jsonfile: "str", mirror_filename: "str" | ||
) -> "Union[RuleNode, LeafNode, EmptyNode]": | ||
with open(filename, mode="r", encoding="utf-8") as wfH: | ||
content = wfH.read() | ||
|
||
tree = parse_groovy_content(content) | ||
|
||
t_tree = digest_lark_tree(tree, prune=[]) | ||
|
||
# These are for debugging purposes | ||
# logging.debug(tree.pretty()) | ||
# with open(jsonfile, mode="w", encoding="utf-8") as jH: | ||
# json.dump(tree, jH, indent=4, cls=LarkFilteringTreeEncoder) | ||
with open(jsonfile, mode="w", encoding="utf-8") as jH: | ||
json.dump(t_tree, jH, indent=4) | ||
|
||
with open(mirror_filename, mode="w", encoding="utf-8") as mH: | ||
write_groovy(t_tree, mH, reset_prev_wants_space=True) | ||
|
||
return t_tree | ||
|
||
|
||
if __name__ == "__main__": | ||
logging.basicConfig( | ||
level=logging.DEBUG, | ||
) | ||
log = logging.getLogger() # root logger | ||
for filename in sys.argv[1:]: | ||
print(f"* Parsing {filename}") | ||
logfile = filename + ".lark" | ||
jsonfile = logfile + ".json" | ||
mirrored_filename = filename + ".mirrored" | ||
fH = logging.FileHandler(logfile, mode="w", encoding="utf-8") | ||
for hdlr in log.handlers[:]: # remove all old handlers | ||
log.removeHandler(hdlr) | ||
log.addHandler(fH) # set the new handler | ||
try: | ||
mirror_groovy_source(filename, jsonfile, mirrored_filename) | ||
except Exception as e: | ||
print(f"\tParse failed, see {logfile}") | ||
logging.exception("Parse failed") | ||
fH.close() |