diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..62d9a3f --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +charset = utf-8 +indent_style = tab +indent_size = 4 +insert_final_newline = true +end_of_line = lf + +[*.{yml,yaml,yug}] +indent_style = space +indent_size = 2 + +[grammars/*.txt] +insert_final_newline = false diff --git a/.github/.templateMarker b/.github/.templateMarker new file mode 100644 index 0000000..5e3a3e0 --- /dev/null +++ b/.github/.templateMarker @@ -0,0 +1 @@ +KOLANICH/python_project_boilerplate.py diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..89ff339 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,8 @@ +version: 2 +updates: + - package-ecosystem: "pip" + directory: "/" + schedule: + interval: "daily" + allow: + - dependency-type: "all" diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml new file mode 100644 index 0000000..7fe33b3 --- /dev/null +++ b/.github/workflows/CI.yml @@ -0,0 +1,15 @@ +name: CI +on: + push: + branches: [master] + pull_request: + branches: [master] + +jobs: + build: + runs-on: ubuntu-22.04 + steps: + - name: typical python workflow + uses: KOLANICH-GHActions/typical-python-workflow@master + with: + github_token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b8fd4e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +__pycache__ +*.pyc +*.pyo +*.pgt +*.dot +/*.egg-info +/build +/dist +/.eggs +/tests/grammars +monkeytype.sqlite3 +*.srctrlprj +*.srctrldb +*.srctrlbm diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..e84ca87 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,55 @@ +image: registry.gitlab.com/kolanich-subgroups/docker-images/fixed_python:latest + +variables: + DOCKER_DRIVER: overlay2 + SAST_ANALYZER_IMAGE_TAG: latest + SAST_DISABLE_DIND: "true" + SAST_CONFIDENCE_LEVEL: 5 + CODECLIMATE_VERSION: latest + +include: + - template: SAST.gitlab-ci.yml + - template: Code-Quality.gitlab-ci.yml + +.build: + tags: + - shared + - linux + stage: build + interruptible: true + variables: + GIT_DEPTH: "1" + PYTHONUSERBASE: ${CI_PROJECT_DIR}/python_user_packages + + before_script: + - export PATH="$PATH:$PYTHONUSERBASE/bin" # don't move into `variables` + #- git clone --depth=1 --filter=sparse:path=src/python https://github.com/waxeye-org/waxeye.git + - git clone --depth=1 https://github.com/waxeye-org/waxeye.git + - cd ./waxeye/src/python + - python3 ./setup.py bdist_wheel + - pip3 install --upgrade ./dist/*.whl + - cd ../../../ + + cache: + paths: + - /usr/local/site-packages + - /usr/local/lib/python*/site-packages + + script: + - python3 setup.py bdist_wheel + - pip3 install --user --upgrade ./dist/*.whl + - cd ./tests + #- coverage run -a --branch --source=UniGrammar -m pytest --junitxml=./rspec.xml --forked ./test*.py + #- coverage report -m || true + #- coveralls || true + #- codecov || true + #- cd .. + - mkdir wheels + - mv ./dist/*.whl ./wheels/AptSourcesList-0.CI-py3-none-any.whl + + artifacts: + paths: + - wheels + - $PYTHONUSERBASE + reports: + junit: ./rspec.xml diff --git a/Code_Of_Conduct.md b/Code_Of_Conduct.md new file mode 100644 index 0000000..bcaa2bf --- /dev/null +++ b/Code_Of_Conduct.md @@ -0,0 +1 @@ +No codes of conduct! \ No newline at end of file diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..602af25 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,6 @@ +include UNLICENSE +include *.md +include tests +global-include .editorconfig +global-include *.pgt +global-include *.pglr diff --git a/ReadMe.md b/ReadMe.md new file mode 100644 index 0000000..8535391 --- /dev/null +++ b/ReadMe.md @@ -0,0 +1,13 @@ +UniGrammarRuntimeCore.py [![Unlicensed work](https://raw.githubusercontent.com/unlicense/unlicense.org/master/static/favicon.png)](https://unlicense.org/) +=================== +![GitLab Build Status](https://gitlab.com/UniGrammar/UniGrammarRuntimeCore.py/badges/master/pipeline.svg) +[![Coveralls Coverage](https://img.shields.io/coveralls/UniGrammar/UniGrammarRuntimeCore.py.svg)](https://coveralls.io/r/UniGrammar/UniGrammarRuntimeCore.py) +![GitLab Coverage](https://gitlab.com/UniGrammar/UniGrammarRuntimeCore.py/badges/master/coverage.svg) +[![N∅ dependencies](https://shields.io/badge/-N%E2%88%85_deps!-0F0) +[![Code style: antiflash](https://img.shields.io/badge/code%20style-antiflash-FFF.svg)](https://codeberg.org/KOLANICH-tools/antiflash.py) + +Core of UniGrammar runtime contains a common framework with minimal dependencies that can be used when creating or wrapping parser generators without actually using `UniGrammar`. It is needed because if you are using OOP, you still have to have some interface, and it would be better if that interface is compatible to the one used in `UniGrammar`, so we won't have to rewrap stuff again and again. There are no stability guarantees though. You are expected to be able to do necessary changes in your code if you use it. + +It is splitted into a separate package because we don't want to require its users to install `UniGrammarRuntime` and its dependencies. + +It doesn't use `UniGrammar` package as a namespace because this stuff is incompatible to `pip install -e .` (components installed as `-e` break every other components, an it is probably a bug in python), so it has to have an own name `UniGrammarRuntimeCore`. diff --git a/UNLICENSE b/UNLICENSE new file mode 100644 index 0000000..efb9808 --- /dev/null +++ b/UNLICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/UniGrammarRuntimeCore/ICompiler.py b/UniGrammarRuntimeCore/ICompiler.py new file mode 100644 index 0000000..fd3df0e --- /dev/null +++ b/UniGrammarRuntimeCore/ICompiler.py @@ -0,0 +1,34 @@ +import typing +from abc import ABC, abstractmethod +from pathlib import Path + + +class ICompiler(ABC): + """Just a base class for compilers compiling grammars DSLs into objects.""" + + __slots__ = () + + @abstractmethod + def compileStr(self, grammarText: str, target: typing.Any = None, fileName: typing.Optional[typing.Union[Path, str]] = None): + """Parses the DSL string and generates internal object from it. `fileName` must not be used for retrieving source, instead it must provide hints, useful for caching or more meaningful error messages and may be just ignored if the backend doesn't support its usage this way.""" + raise NotImplementedError + + def compileFile(self, grammarFile: Path, target: typing.Any = None): + """Parses the DSL file and generates internal object from it. May be better because of caching, lower memory overhead and more meaningful error messages.""" + return self.compileStr(grammarFile.read_text(encoding="utf-8"), target, fileName=grammarFile) + + def compile(self, grammar: typing.Union[Path, str], target: typing.Any = None, fileName: typing.Optional[typing.Union[Path, str]] = None): + """Just a convenience method dispatching between `compileStr` and `compileFile`""" + if isinstance(grammar, Path): + assert fileName is None + return self.compileFile(grammar, target) + return self.compileStr(grammar, target, grammar) + + +class DummyCompiler(ICompiler): + """Returns text as a string""" + + __slots__ = () + + def compileStr(self, grammarText: str, target: typing.Any = None, fileName: typing.Optional[typing.Union[Path, str]] = None): + return grammarText diff --git a/UniGrammarRuntimeCore/IParser.py b/UniGrammarRuntimeCore/IParser.py new file mode 100644 index 0000000..85b53a2 --- /dev/null +++ b/UniGrammarRuntimeCore/IParser.py @@ -0,0 +1,70 @@ +import typing +from abc import ABC, abstractmethod +from pathlib import Path + +from .ICompiler import ICompiler + + +class IParser(ABC): + """Just initializes the parser and parses a string into a parse tree specific to backend""" + + __slots__ = () + + NAME = None # Must match the name of the backend used in `META.name` in the backend in `UniGrammar.backends` + + @abstractmethod + def __call__(self, s: str): + """parses the stuff""" + raise NotImplementedError + + +class IParserFactory(ABC): + """Produces instances of `IParser` classes and carries a common state. Should be a singleton.""" + + __slots__ = () + PARSER_CLASS = None + + def __init__(self): + self.__class__.ensureInitialized() + + @classmethod + def ensureInitialized(cls): + """Check if a module of the parser is immported, and if not, import it and save into a variable stored in the class""" + pass + + def fromInternal(self, internalRepr: typing.Any, target: str = None) -> typing.Any: + return self.__class__.PARSER_CLASS(internalRepr) + + +class IParserFactoryFromSource(IParserFactory, ICompiler): # pylint: disable=abstract-method + """A parser that is constructed from a text string representing a grammar in DSL rather than a precompiled file. This class has additional metods for DRY""" + + __slots__ = () + + def fromStr(self, grammarText: str, target: typing.Any = None, fileName: typing.Optional[typing.Union[Path, str]] = None): + """Constructs a parser from a `str` with parser-specific DSL""" + return self.fromInternal(self.compileStr(grammarText, target, fileName)) + + def fromFile(self, grammarFile: Path, target: typing.Any = None): + """Constructs a parser from a file with parser-specific DSL""" + return self.fromInternal(self.compileFile(grammarText, target, fileName)) + + +class IParserFactoryFromPrecompiled(IParserFactory): + """A parser that is constructed from a python file. This class has additional metods for DRY""" + + __slots__ = () + + @abstractmethod + def processEvaledGlobals(self, globalz: dict, grammarName: str): + """ + 1. returns the ctor + 2. does other stuff + """ + raise NotImplementedError + + def compile(self, sourceOrAST: typing.Union[str, "ast.Module"], grammarName): + compiled = compile(sourceOrAST, grammarName + ".py", "exec", optimize=2) + globalz = {} + eval(compiled, globalz) + return self.processEvaledGlobals(globalz, grammarName) diff --git a/UniGrammarRuntimeCore/PoolManager.py b/UniGrammarRuntimeCore/PoolManager.py new file mode 100644 index 0000000..1a1316d --- /dev/null +++ b/UniGrammarRuntimeCore/PoolManager.py @@ -0,0 +1,13 @@ +class PoolManager: + __slots__ = ("constructed",) + + def __init__(self): + self.constructed = {} + + def __call__(self, constuctorClass: type, *args, **kwargs): + if constuctorClass not in self.constructed: + obj = constuctorClass(*args, **kwargs) + self.constructed[obj.__class__] = obj + else: + obj = self.constructed[constuctorClass] + return obj diff --git a/UniGrammarRuntimeCore/__init__.py b/UniGrammarRuntimeCore/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..03761ca --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,37 @@ +[build-system] +requires = ["setuptools>=61.2.0", "wheel", "setuptools_scm[toml]>=3.4.3"] +build-backend = "setuptools.build_meta" + +[project] +name = "UniGrammarRuntimeCore" +authors = [{name = "KOLANICH"}] +description = "The core of framework for runtime for UniGrammar. Can be used by third-party tools not using UniGrammar, but when it is desireable to make integration with UniGrammar easier." +readme = "ReadMe.md" +keywords = ["grammars", "UniGrammar"] +license = {text = "Unlicense"} +classifiers = [ + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Development Status :: 4 - Beta", + "Environment :: Other Environment", + "Intended Audience :: Developers", + "License :: Public Domain", + "Operating System :: OS Independent", + "Topic :: Software Development :: Libraries :: Python Modules", + "Topic :: Security", + "Topic :: Text Processing", +] +requires-python = ">=3.4" +dynamic = ["version"] + +[project.urls] +Homepage = "https://codeberg.org/UniGrammar/UniGrammarRuntimeCore.py" + +[tool.setuptools] +zip-safe = true +include-package-data = true + +[tool.setuptools.packages] +find = {namespaces = false} + +[tool.setuptools_scm]