Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long-awaited rewrite to pyparsing and AST #8

Merged
merged 44 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
ad3a3a6
test: more tests
knopki Oct 6, 2023
c5ded6a
fix(parser): arithmetic operator
knopki Oct 16, 2023
0ef5672
feat(parser): initial pyparsing
knopki Oct 16, 2023
7a3aaee
feat: infix notation formula with convertion between source, ast,
knopki Nov 10, 2023
343d8cb
fix(tests): imports
knopki Nov 10, 2023
43797bd
fix(FallbackNode): match any non space
knopki Nov 10, 2023
ac08c92
fix(FormulaOps): match first
knopki Nov 10, 2023
cd0a715
chore: remove litter
knopki Nov 10, 2023
910c013
feat(RootNode): implement root
knopki Nov 10, 2023
c570ccc
feat: many features
knopki Nov 14, 2023
d3bcc6b
feat(cli): implement cli tools
knopki Nov 15, 2023
a94cdf8
chore: remove lark stuff
knopki Nov 15, 2023
fa2b572
feat(FallbackNode): cut text
knopki Nov 16, 2023
cf8ca24
feat: save line breaks
knopki Nov 17, 2023
79b147c
feat: ignore comments
knopki Nov 17, 2023
8aea1c9
fix: parse all
knopki Nov 17, 2023
068932b
feat(cli): ignore warnings
knopki Nov 17, 2023
e924e43
feat: print_dump diagnostic helper
knopki Nov 17, 2023
57f9c20
feat: full rewrite
knopki Nov 21, 2023
5f3ff35
docs(README.md): update
knopki Nov 21, 2023
2b1af57
fix(ast.py): Self import error when python < 3.11
knopki Nov 21, 2023
b403553
fix(grammar): root node + add tests
knopki Dec 1, 2023
e2784d7
style: apply linter hints
knopki Dec 9, 2023
fd7b69f
feat(TextNode): text node instead of fallback node
knopki Dec 9, 2023
5624f59
fix(grammar): use decimals
knopki Dec 19, 2023
5db11be
test(grammar): decimals
knopki Dec 19, 2023
3768aa6
feat(grammar,ast): local node
knopki Dec 19, 2023
6f7b5fb
feat(grammar,ast): existing_prm node
knopki Dec 19, 2023
25cd77c
feat(grammar,ast): num_runs node
knopki Dec 19, 2023
7489c97
feat(grammar,ast): xdd node
knopki Mar 29, 2024
b2252f1
feat(grammar,ast): axial_conv node
knopki Apr 4, 2024
d53145e
refactor(tests): split tests
knopki Apr 4, 2024
5db39e8
refactor(tests): split tests
knopki Apr 4, 2024
b20213d
test: 100% coverage
knopki Apr 5, 2024
06c2e65
fix(ast,tests): root node multiline fix
knopki Apr 11, 2024
44ade8b
fix(grammar,ast,tests): num_runs argument type added
knopki Apr 11, 2024
edeeb94
feat(grammar,ast,tests): macro node
knopki Apr 11, 2024
a197ec4
feat(grammar,ast,tests): bkg node
knopki Apr 11, 2024
711046c
feat(grammar,ast,tests): scale node
knopki Apr 11, 2024
2ccdacb
refactor: remove litter
knopki Apr 11, 2024
d5d26a9
style: fix lint errors
knopki Apr 11, 2024
e1f95fc
refactor: backport to 3.8
knopki Apr 11, 2024
32dfc9a
ci: fix linters
knopki Apr 11, 2024
a23d3de
ci: fix linters
knopki Apr 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ max-complexity = 16
# B = bugbear
# B9 = bugbear opinionated (incl line length)
select = C,E,F,W,B,B9
# B905 `zip()` without an explicit `strict=` parameter
# E203: whitespace before ':' (black behaviour)
# E501: flake8 line length (covered by bugbear B950)
# W503: line break before binary operator (black behaviour)
ignore = E203,E501,W503
ignore = B905,B907,E203,E501,W503
classmethod-decorators =
classmethod
validator
exclude = pytopas/lark_standalone.py
per-file-ignores = __init__.py:F401
6 changes: 3 additions & 3 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8"]
python-version: ["3.11"]

steps:
- name: Checkout repository
Expand Down Expand Up @@ -50,5 +50,5 @@ jobs:

- name: pyupgrade
run: |
find -name "*.py" -and -not -name "lark_standalone.py" -print0 |\
xargs -0 -n1 pyupgrade --py38-plus
find -name "*.py" -print0 |\
xargs -0 -n1 pyupgrade --py38-plus --keep-runtime-typing
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,31 @@ This is the parser for Bruker's TOPAS macro language. We have compared two canon
Parse TOPAS input and convert it to JSON:

```python
import json
from pytopas import TOPASParser

src = "a = a + 1 ; 0"
src = "a(b,c)"

parser = TOPASParser()
tree = parser.parse(src)
print(tree.to_json(compact=True))
tree = TOPASParser.parse(src)
print(json.dumps(tree))
```

Convert parser's JSON-encoded TOPAS code back into the TOPAS input format:

```python
import json
from pytopas import TOPASParseTree

input_json = '["topas", ["a = a + 1 ; 0"]]'

tree = TOPASParseTree.from_json(input_json)
print(tree.to_topas())
input_json = """
["topas",
["formula",
["func_call", "a",
["formula", ["p", {"n": ["parameter_name", "b"]}]],
["formula", ["p", {"n": ["parameter_name", "c"]}]]]]]
"""
serialized = json.loads(input_json)
src = TOPASParser.reconstruct(serialized)
print(src)

```

Expand All @@ -33,16 +40,16 @@ print(tree.to_topas())
After installing the package, two command line utilities will be available.

```
usage: topas2json [-h] [-c] file
usage: topas2json [-h] [--ignore-warnings] file

Parse TOPAS input and output JSON

positional arguments:
file Path to TOPAS file or '-' for stdin input
file Path to TOPAS file or '-' for stdin input

options:
-h, --help show this help message and exit
-c, --compact Use compact output
-h, --help show this help message and exit
--ignore-warnings Don't print parsing warnings
```

```
Expand All @@ -60,9 +67,7 @@ options:

## Development

Install package with optional dependencies: `pip install -e .[dev,lint,test,release]`

Regenerate standalone `lark` parser after `grammar.lark` change: `make parser`
Install package with optional dependencies: `pip install -e .[lint,test,release]`

## License

Expand Down
20 changes: 6 additions & 14 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "flit_core.buildapi"

[project]
name = "pytopas"
version = "0.0.1"
version = "0.0.3"
description = """Bruker's TOPAS macro language parser"""
authors = [{name = "Sergei Korolev", email = "[email protected]"}]
readme = "README.md"
Expand All @@ -30,12 +30,11 @@ classifiers = [
keywords = ["TOPAS", "XRPD", "Rietveld refinement", "pair distribution function", "stacking-faults", "charge flipping", "structure solution", "deconvolution"]
requires-python = ">=3.8"
dependencies = [
"pyparsing >= 3",
"typing-extensions >= 4.2.0; python_version < '3.11'",
]

[project.optional-dependencies]
dev = [
"lark >= 1.1.7",
]
lint = [
"autoflake",
"black >= 23.1",
Expand Down Expand Up @@ -71,10 +70,8 @@ ignore-init-module-imports = true
remove-all-unused-imports = true
remove-duplicate-keys = true
remove-unused-variables = true
exclude = "pytopas/lark_standalone.py"

[tool.black]
exclude = "pytopas/lark_standalone.py"
target-version = ['py38', 'py39', 'py310', 'py311']

[tool.commitizen]
Expand All @@ -83,27 +80,22 @@ tag_format = "v$version"
major_version_zero = true
version_files = [
"pyproject.toml:^version",
"pytopas/__init__.py:^__version__",
"pytopas/__init__.py:^__VERSION__",
]
version_provider = "pep621"
update_changelog_on_bump = true

[tool.coverage.run]
omit = ['pytopas/lark_standalone.py', 'tests/*']
omit = ['tests/*']

[tool.flit.module]
name = "pytopas"

[tool.flit.sdist]
include = ["pytopas/grammar.lark"]

[tool.isort]
profile = "black"
py_version = 38
skip = ["pytopas/lark_standalone.py"]

[tool.pylint.MASTER]
ignore = "lark_standalone.py"
load-plugins=[
"pylint_per_file_ignores",
]
Expand All @@ -115,7 +107,7 @@ recursive = true
suggestion-mode = true

[tool.pylint.basic]
good-names = ["id", "x", "xy", "y", "_", "__"]
good-names = ["id", "kv", "op", "x", "xy", "y", "_", "__"]

[tool.pylint.design]
max-args = 7
Expand Down
8 changes: 2 additions & 6 deletions pytopas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
"TOPAS parser"

from .lark_standalone import UnexpectedToken
from .parser import TOPASParser
from .tree import TOPASParseTree
from .parser import Parser as TOPASParser

__VERSION__ = "0.0.1"

__all__ = ["TOPASParser", "TOPASParseTree", "UnexpectedToken"]
__VERSION__ = "0.0.3"
Loading
Loading