From 5084fe1a35ec59b1926ec5aa58a6af2e2c6f05d0 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Thu, 26 Sep 2024 13:51:02 +0200 Subject: [PATCH 1/9] Format the generated code at generation time --- cf_units/_udunits2_parser/compile.py | 20 +++++++++++++++++++ .../_udunits2_parser/parser/udunits2Lexer.py | 4 +--- .../_udunits2_parser/parser/udunits2Parser.py | 4 +--- .../parser/udunits2ParserVisitor.py | 2 +- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/cf_units/_udunits2_parser/compile.py b/cf_units/_udunits2_parser/compile.py index 5cd4d3b2..b83326de 100644 --- a/cf_units/_udunits2_parser/compile.py +++ b/cf_units/_udunits2_parser/compile.py @@ -103,7 +103,27 @@ def main(): ], check=True, ) + subprocess.run( + [ + "ruff", + "format", + "./parser/", + "--config=../../pyproject.toml", + ], + check=True, + ) + subprocess.run( + [ + "ruff", + "check", + "--fix", + "./parser/", + "--config=../../pyproject.toml", + "--ignore=E501,C408,E711", + ], + check=True, + ) print("Done.") diff --git a/cf_units/_udunits2_parser/parser/udunits2Lexer.py b/cf_units/_udunits2_parser/parser/udunits2Lexer.py index 0dc07819..a019f54c 100644 --- a/cf_units/_udunits2_parser/parser/udunits2Lexer.py +++ b/cf_units/_udunits2_parser/parser/udunits2Lexer.py @@ -1,7 +1,5 @@ -# Generated from /home/ruth/git_stuff/cf-units/cf_units/_udunits2_parser/parser/udunits2Lexer.g4 by ANTLR 4.11.1 -# encoding: utf-8 +# Generated from /media/important/github/scitools/cf-units/cf_units/_udunits2_parser/parser/udunits2Lexer.g4 by ANTLR 4.11.1 import sys -from io import StringIO from antlr4 import * diff --git a/cf_units/_udunits2_parser/parser/udunits2Parser.py b/cf_units/_udunits2_parser/parser/udunits2Parser.py index 1275018b..ea5244e0 100644 --- a/cf_units/_udunits2_parser/parser/udunits2Parser.py +++ b/cf_units/_udunits2_parser/parser/udunits2Parser.py @@ -1,7 +1,5 @@ -# Generated from /home/ruth/git_stuff/cf-units/cf_units/_udunits2_parser/parser/udunits2Lexer.g4 by ANTLR 4.11.1 -# encoding: utf-8 +# Generated from /media/important/github/scitools/cf-units/cf_units/_udunits2_parser/udunits2Parser.g4 by ANTLR 4.11.1 import sys -from io import StringIO from antlr4 import * diff --git a/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py b/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py index f83c692e..924b5ca3 100644 --- a/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py +++ b/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py @@ -1,4 +1,4 @@ -# Generated from /home/ruth/git_stuff/cf-units/cf_units/_udunits2_parser/udunits2Parser.g4 by ANTLR 4.11.1 +# Generated from /media/important/github/scitools/cf-units/cf_units/_udunits2_parser/udunits2Parser.g4 by ANTLR 4.11.1 from antlr4 import * if __name__ is not None and "." in __name__: From ebb35651eb4c605b21fc331cc0ee4cf88402cdf0 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Fri, 27 Sep 2024 08:23:47 +0200 Subject: [PATCH 2/9] Code to vendor the antlr runtime --- .gitattributes | 1 + cf_units/_udunits2_parser/__init__.py | 9 ++- cf_units/_udunits2_parser/compile.py | 81 +++++++++++++++++-- .../_udunits2_parser/parser/udunits2Lexer.py | 2 +- .../_udunits2_parser/parser/udunits2Parser.py | 10 +-- .../parser/udunits2ParserVisitor.py | 2 +- .../tests/integration/parse/test_graph.py | 3 - .../tests/integration/parse/test_parse.py | 4 - pyproject.toml | 4 +- 9 files changed, 90 insertions(+), 26 deletions(-) diff --git a/.gitattributes b/.gitattributes index 00a7b00c..bda29feb 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ .git_archival.txt export-subst +cf_units/_udunits2_parser/parser/**/*.py linguist-generated=true diff --git a/cf_units/_udunits2_parser/__init__.py b/cf_units/_udunits2_parser/__init__.py index aec51503..2c0b0fcc 100644 --- a/cf_units/_udunits2_parser/__init__.py +++ b/cf_units/_udunits2_parser/__init__.py @@ -5,8 +5,13 @@ import unicodedata -from antlr4 import CommonTokenStream, InputStream -from antlr4.error.ErrorListener import ErrorListener +from cf_units._udunits2_parser.parser._antlr4_runtime import ( + CommonTokenStream, + InputStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error import ( + ErrorListener, +) from . import graph from .parser.udunits2Lexer import udunits2Lexer diff --git a/cf_units/_udunits2_parser/compile.py b/cf_units/_udunits2_parser/compile.py index b83326de..1a175000 100644 --- a/cf_units/_udunits2_parser/compile.py +++ b/cf_units/_udunits2_parser/compile.py @@ -17,7 +17,9 @@ import collections import re +import shutil import subprocess +import sys import urllib.request from pathlib import Path @@ -26,8 +28,8 @@ except ImportError: raise ImportError("Jinja2 needed to compile the grammar.") - -JAR_NAME = "antlr-4.11.1-complete.jar" +ANTLR_VERSION = "4.11.1" +JAR_NAME = f"antlr-{ANTLR_VERSION}-complete.jar" JAR_URL = f"https://www.antlr.org/download/{JAR_NAME}" HERE = Path(__file__).resolve().parent @@ -66,6 +68,60 @@ def expand_lexer(source, target): fh.write(new_content) +def vendor_antlr4_runtime(parser_dir: Path): + antlr_dest = parser_dir / "_antlr4_runtime" + version_file = antlr_dest / "_antlr4_version.txt" + existing_version: str | None = None + if antlr_dest.exists(): + existing_version = version_file.read_text().strip() + else: + antlr_dest.mkdir() + if existing_version != ANTLR_VERSION: + print("Vendoring the antlr4 runtime") + if antlr_dest.exists(): + shutil.rmtree(antlr_dest) + + tmp_dest = Path("delme") + subprocess.run( + [ + sys.executable, + "-m", + "pip", + "install", + "--quiet", + f"--prefix={tmp_dest}", + "antlr4-python3-runtime", + ], + check=True, + ) + [antlr_code_dir] = tmp_dest.glob("lib/python3.*/site-packages/antlr4") + for py_file in antlr_code_dir.glob("**/*.py"): + py_file_dest = antlr_dest / py_file.relative_to(antlr_code_dir) + py_file_dest.parent.mkdir(exist_ok=True) + py_file_dest.write_text(py_file.read_text()) + shutil.rmtree(tmp_dest) + version_file.write_text(ANTLR_VERSION) + else: + print("Vendoring the antlr4 is already complete") + + # Re-write all imports relating to the antlr4 runtime to be the + # vendored location. + for py_file in Path(".").glob("**/*.py"): + if py_file.absolute() == Path(__file__).absolute(): + # Don't adapt for vendoring of this file. + continue + contents = py_file.read_text() + contents = contents.replace( + "import antlr4", + "import cf_units._udunits2_parser.parser._antlr4_runtime", + ) + contents = contents.replace( + "from antlr4", + "from cf_units._udunits2_parser.parser._antlr4_runtime", + ) + py_file.write_text(contents) + + def main(): if not JAR.exists(): print(f"Downloading {JAR_NAME}...") @@ -74,6 +130,8 @@ def main(): print("Expanding lexer...") expand_lexer(LEXER.parent.parent / (LEXER.name + ".jinja"), str(LEXER)) + parser_dir = Path("parser") + print("Compiling lexer...") subprocess.run( [ @@ -83,7 +141,7 @@ def main(): "-Dlanguage=Python3", str(LEXER), "-o", - "parser", + parser_dir, ], check=True, ) @@ -99,15 +157,19 @@ def main(): "-visitor", str(PARSER), "-o", - "parser", + parser_dir, ], check=True, ) + + vendor_antlr4_runtime(parser_dir) + + # Reformat and lint fix the generated code. subprocess.run( [ "ruff", "format", - "./parser/", + HERE, "--config=../../pyproject.toml", ], check=True, @@ -118,12 +180,17 @@ def main(): "ruff", "check", "--fix", - "./parser/", + ".", "--config=../../pyproject.toml", - "--ignore=E501,C408,E711", + # This is a best-efforts basis. No worries if ruff can't fix + # everything. + "--exit-zero", ], + cwd=HERE, check=True, + stdout=subprocess.DEVNULL, ) + print("Done.") diff --git a/cf_units/_udunits2_parser/parser/udunits2Lexer.py b/cf_units/_udunits2_parser/parser/udunits2Lexer.py index a019f54c..560fc244 100644 --- a/cf_units/_udunits2_parser/parser/udunits2Lexer.py +++ b/cf_units/_udunits2_parser/parser/udunits2Lexer.py @@ -1,7 +1,7 @@ # Generated from /media/important/github/scitools/cf-units/cf_units/_udunits2_parser/parser/udunits2Lexer.g4 by ANTLR 4.11.1 import sys -from antlr4 import * +from cf_units._udunits2_parser.parser._antlr4_runtime import * if sys.version_info[1] > 5: from typing import TextIO diff --git a/cf_units/_udunits2_parser/parser/udunits2Parser.py b/cf_units/_udunits2_parser/parser/udunits2Parser.py index ea5244e0..b57db82c 100644 --- a/cf_units/_udunits2_parser/parser/udunits2Parser.py +++ b/cf_units/_udunits2_parser/parser/udunits2Parser.py @@ -1,7 +1,7 @@ # Generated from /media/important/github/scitools/cf-units/cf_units/_udunits2_parser/udunits2Parser.g4 by ANTLR 4.11.1 import sys -from antlr4 import * +from cf_units._udunits2_parser.parser._antlr4_runtime import * if sys.version_info[1] > 5: from typing import TextIO @@ -1603,7 +1603,7 @@ def product(self, _p: int = 0): ) self.state = 51 if not self.precpred(self._ctx, 4): - from antlr4.error.Errors import ( + from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( FailedPredicateException, ) @@ -1623,7 +1623,7 @@ def product(self, _p: int = 0): ) self.state = 53 if not self.precpred(self._ctx, 3): - from antlr4.error.Errors import ( + from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( FailedPredicateException, ) @@ -1645,7 +1645,7 @@ def product(self, _p: int = 0): ) self.state = 56 if not self.precpred(self._ctx, 2): - from antlr4.error.Errors import ( + from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( FailedPredicateException, ) @@ -1667,7 +1667,7 @@ def product(self, _p: int = 0): ) self.state = 59 if not self.precpred(self._ctx, 1): - from antlr4.error.Errors import ( + from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( FailedPredicateException, ) diff --git a/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py b/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py index 924b5ca3..723066bd 100644 --- a/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py +++ b/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py @@ -1,5 +1,5 @@ # Generated from /media/important/github/scitools/cf-units/cf_units/_udunits2_parser/udunits2Parser.g4 by ANTLR 4.11.1 -from antlr4 import * +from cf_units._udunits2_parser.parser._antlr4_runtime import * if __name__ is not None and "." in __name__: from .udunits2Parser import udunits2Parser diff --git a/cf_units/tests/integration/parse/test_graph.py b/cf_units/tests/integration/parse/test_graph.py index fb1ad67f..24923f91 100644 --- a/cf_units/tests/integration/parse/test_graph.py +++ b/cf_units/tests/integration/parse/test_graph.py @@ -3,9 +3,6 @@ # This file is part of cf-units and is released under the BSD license. # See LICENSE in the root of the repository for full licensing details. # ruff: noqa: E402 -import pytest - -antlr4 = pytest.importorskip("antlr4") import cf_units._udunits2_parser.graph as g from cf_units._udunits2_parser import parse diff --git a/cf_units/tests/integration/parse/test_parse.py b/cf_units/tests/integration/parse/test_parse.py index 5e91fd5b..c2dbf029 100644 --- a/cf_units/tests/integration/parse/test_parse.py +++ b/cf_units/tests/integration/parse/test_parse.py @@ -3,10 +3,6 @@ # This file is part of cf-units and is released under the BSD license. # See LICENSE in the root of the repository for full licensing details. # ruff: noqa: E402 -import pytest - -antlr4 = pytest.importorskip("antlr4") - import re import pytest diff --git a/pyproject.toml b/pyproject.toml index 79577687..932266ee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -58,9 +58,7 @@ readme = "README.md" requires-python = ">=3.10" [project.optional-dependencies] -all = ["cf-units[latex]", "cf-units[docs]", "cf-units[test]"] -# To update antlr: see cf_units/_udunits2_parser/README.md" -latex = ["antlr4-python3-runtime ==4.11.1"] +all = ["cf-units[docs,test]"] docs = ["sphinx"] test = ["codecov", "cython", "jinja2", "pip", "pytest", "pytest-cov"] From 3a89502a161fafbdd69a1872e9e57c4689ff5c8e Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Fri, 27 Sep 2024 08:30:54 +0200 Subject: [PATCH 3/9] Vendor the antlr4 runtime library --- .../_antlr4_runtime/BufferedTokenStream.py | 317 +++ .../_antlr4_runtime/CommonTokenFactory.py | 73 + .../_antlr4_runtime/CommonTokenStream.py | 89 + .../parser/_antlr4_runtime/FileStream.py | 34 + .../parser/_antlr4_runtime/InputStream.py | 89 + .../parser/_antlr4_runtime/IntervalSet.py | 183 ++ .../parser/_antlr4_runtime/LL1Analyzer.py | 258 ++ .../parser/_antlr4_runtime/Lexer.py | 370 +++ .../parser/_antlr4_runtime/ListTokenSource.py | 153 ++ .../parser/_antlr4_runtime/Parser.py | 660 +++++ .../_antlr4_runtime/ParserInterpreter.py | 232 ++ .../_antlr4_runtime/ParserRuleContext.py | 203 ++ .../_antlr4_runtime/PredictionContext.py | 704 ++++++ .../parser/_antlr4_runtime/Recognizer.py | 167 ++ .../parser/_antlr4_runtime/RuleContext.py | 239 ++ .../parser/_antlr4_runtime/StdinStream.py | 15 + .../parser/_antlr4_runtime/Token.py | 177 ++ .../_antlr4_runtime/TokenStreamRewriter.py | 331 +++ .../parser/_antlr4_runtime/Utils.py | 35 + .../parser/_antlr4_runtime/__init__.py | 59 + .../_antlr4_runtime/_antlr4_version.txt | 1 + .../parser/_antlr4_runtime/_pygrun.py | 186 ++ .../parser/_antlr4_runtime/atn/ATN.py | 155 ++ .../parser/_antlr4_runtime/atn/ATNConfig.py | 231 ++ .../_antlr4_runtime/atn/ATNConfigSet.py | 245 ++ .../atn/ATNDeserializationOptions.py | 30 + .../_antlr4_runtime/atn/ATNDeserializer.py | 546 ++++ .../_antlr4_runtime/atn/ATNSimulator.py | 57 + .../parser/_antlr4_runtime/atn/ATNState.py | 282 +++ .../parser/_antlr4_runtime/atn/ATNType.py | 17 + .../_antlr4_runtime/atn/LexerATNSimulator.py | 763 ++++++ .../parser/_antlr4_runtime/atn/LexerAction.py | 308 +++ .../atn/LexerActionExecutor.py | 156 ++ .../_antlr4_runtime/atn/ParserATNSimulator.py | 2201 +++++++++++++++++ .../_antlr4_runtime/atn/PredictionMode.py | 514 ++++ .../_antlr4_runtime/atn/SemanticContext.py | 356 +++ .../parser/_antlr4_runtime/atn/Transition.py | 312 +++ .../parser/_antlr4_runtime/atn/__init__.py | 1 + .../parser/_antlr4_runtime/dfa/DFA.py | 150 ++ .../_antlr4_runtime/dfa/DFASerializer.py | 83 + .../parser/_antlr4_runtime/dfa/DFAState.py | 138 ++ .../parser/_antlr4_runtime/dfa/__init__.py | 1 + .../error/DiagnosticErrorListener.py | 139 ++ .../_antlr4_runtime/error/ErrorListener.py | 108 + .../_antlr4_runtime/error/ErrorStrategy.py | 755 ++++++ .../parser/_antlr4_runtime/error/Errors.py | 213 ++ .../parser/_antlr4_runtime/error/__init__.py | 1 + .../parser/_antlr4_runtime/tree/Chunk.py | 33 + .../_antlr4_runtime/tree/ParseTreeMatch.py | 129 + .../_antlr4_runtime/tree/ParseTreePattern.py | 81 + .../tree/ParseTreePatternMatcher.py | 452 ++++ .../_antlr4_runtime/tree/RuleTagToken.py | 53 + .../_antlr4_runtime/tree/TokenTagToken.py | 48 + .../parser/_antlr4_runtime/tree/Tree.py | 203 ++ .../parser/_antlr4_runtime/tree/Trees.py | 134 + .../parser/_antlr4_runtime/tree/__init__.py | 0 .../parser/_antlr4_runtime/xpath/XPath.py | 330 +++ .../_antlr4_runtime/xpath/XPathLexer.py | 570 +++++ .../parser/_antlr4_runtime/xpath/__init__.py | 1 + 59 files changed, 14371 insertions(+) create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/BufferedTokenStream.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/CommonTokenFactory.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/CommonTokenStream.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/FileStream.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/InputStream.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/IntervalSet.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/LL1Analyzer.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/Lexer.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/ListTokenSource.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/Parser.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/ParserInterpreter.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/ParserRuleContext.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/PredictionContext.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/Recognizer.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/RuleContext.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/StdinStream.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/Token.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/TokenStreamRewriter.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/Utils.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/__init__.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/_antlr4_version.txt create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/_pygrun.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATN.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNConfig.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNConfigSet.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNDeserializationOptions.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNDeserializer.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNSimulator.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNState.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNType.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerATNSimulator.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerAction.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerActionExecutor.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ParserATNSimulator.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/PredictionMode.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/SemanticContext.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/Transition.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/__init__.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFA.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFASerializer.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFAState.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/__init__.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/error/DiagnosticErrorListener.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/error/ErrorListener.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/error/ErrorStrategy.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/error/Errors.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/error/__init__.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Chunk.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreeMatch.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreePattern.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreePatternMatcher.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/RuleTagToken.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/TokenTagToken.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Tree.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Trees.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/__init__.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/XPath.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/XPathLexer.py create mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/__init__.py diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/BufferedTokenStream.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/BufferedTokenStream.py new file mode 100644 index 00000000..100219a8 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/BufferedTokenStream.py @@ -0,0 +1,317 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. + +# This implementation of {@link TokenStream} loads tokens from a +# {@link TokenSource} on-demand, and places the tokens in a buffer to provide +# access to any previous token by index. +# +#

+# This token stream ignores the value of {@link Token#getChannel}. If your +# parser requires the token stream filter tokens to only those on a particular +# channel, such as {@link Token#DEFAULT_CHANNEL} or +# {@link Token#HIDDEN_CHANNEL}, use a filtering token stream such a +# {@link CommonTokenStream}.

+from io import StringIO + +from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + IllegalStateException, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + +# need forward declaration +Lexer = None + + +# this is just to keep meaningful parameter types to Parser +class TokenStream: + pass + + +class BufferedTokenStream(TokenStream): + __slots__ = ("tokenSource", "tokens", "index", "fetchedEOF") + + def __init__(self, tokenSource: Lexer): + # The {@link TokenSource} from which tokens for this stream are fetched. + self.tokenSource = tokenSource + + # A collection of all tokens fetched from the token source. The list is + # considered a complete view of the input once {@link #fetchedEOF} is set + # to {@code true}. + self.tokens = [] + + # The index into {@link #tokens} of the current token (next token to + # {@link #consume}). {@link #tokens}{@code [}{@link #p}{@code ]} should be + # {@link #LT LT(1)}. + # + #

This field is set to -1 when the stream is first constructed or when + # {@link #setTokenSource} is called, indicating that the first token has + # not yet been fetched from the token source. For additional information, + # see the documentation of {@link IntStream} for a description of + # Initializing Methods.

+ self.index = -1 + + # Indicates whether the {@link Token#EOF} token has been fetched from + # {@link #tokenSource} and added to {@link #tokens}. This field improves + # performance for the following cases: + # + #
    + #
  • {@link #consume}: The lookahead check in {@link #consume} to prevent + # consuming the EOF symbol is optimized by checking the values of + # {@link #fetchedEOF} and {@link #p} instead of calling {@link #LA}.
  • + #
  • {@link #fetch}: The check to prevent adding multiple EOF symbols into + # {@link #tokens} is trivial with this field.
  • + #
      + self.fetchedEOF = False + + def mark(self): + return 0 + + def release(self, marker: int): + # no resources to release + pass + + def reset(self): + self.seek(0) + + def seek(self, index: int): + self.lazyInit() + self.index = self.adjustSeekIndex(index) + + def get(self, index: int): + self.lazyInit() + return self.tokens[index] + + def consume(self): + skipEofCheck = False + if self.index >= 0: + if self.fetchedEOF: + # the last token in tokens is EOF. skip check if p indexes any + # fetched token except the last. + skipEofCheck = self.index < len(self.tokens) - 1 + else: + # no EOF token in tokens. skip check if p indexes a fetched token. + skipEofCheck = self.index < len(self.tokens) + else: + # not yet initialized + skipEofCheck = False + + if not skipEofCheck and self.LA(1) == Token.EOF: + raise IllegalStateException("cannot consume EOF") + + if self.sync(self.index + 1): + self.index = self.adjustSeekIndex(self.index + 1) + + # Make sure index {@code i} in tokens has a token. + # + # @return {@code true} if a token is located at index {@code i}, otherwise + # {@code false}. + # @see #get(int i) + # / + def sync(self, i: int): + n = i - len(self.tokens) + 1 # how many more elements we need? + if n > 0: + fetched = self.fetch(n) + return fetched >= n + return True + + # Add {@code n} elements to buffer. + # + # @return The actual number of elements added to the buffer. + # / + def fetch(self, n: int): + if self.fetchedEOF: + return 0 + for i in range(0, n): + t = self.tokenSource.nextToken() + t.tokenIndex = len(self.tokens) + self.tokens.append(t) + if t.type == Token.EOF: + self.fetchedEOF = True + return i + 1 + return n + + # Get all tokens from start..stop inclusively#/ + def getTokens(self, start: int, stop: int, types: set = None): + if start < 0 or stop < 0: + return None + self.lazyInit() + subset = [] + if stop >= len(self.tokens): + stop = len(self.tokens) - 1 + for i in range(start, stop): + t = self.tokens[i] + if t.type == Token.EOF: + break + if types is None or t.type in types: + subset.append(t) + return subset + + def LA(self, i: int): + return self.LT(i).type + + def LB(self, k: int): + if (self.index - k) < 0: + return None + return self.tokens[self.index - k] + + def LT(self, k: int): + self.lazyInit() + if k == 0: + return None + if k < 0: + return self.LB(-k) + i = self.index + k - 1 + self.sync(i) + if i >= len(self.tokens): # return EOF token + # EOF must be last token + return self.tokens[len(self.tokens) - 1] + return self.tokens[i] + + # Allowed derived classes to modify the behavior of operations which change + # the current stream position by adjusting the target token index of a seek + # operation. The default implementation simply returns {@code i}. If an + # exception is thrown in this method, the current stream index should not be + # changed. + # + #

      For example, {@link CommonTokenStream} overrides this method to ensure that + # the seek target is always an on-channel token.

      + # + # @param i The target token index. + # @return The adjusted target token index. + + def adjustSeekIndex(self, i: int): + return i + + def lazyInit(self): + if self.index == -1: + self.setup() + + def setup(self): + self.sync(0) + self.index = self.adjustSeekIndex(0) + + # Reset this token stream by setting its token source.#/ + def setTokenSource(self, tokenSource: Lexer): + self.tokenSource = tokenSource + self.tokens = [] + self.index = -1 + self.fetchedEOF = False + + # Given a starting index, return the index of the next token on channel. + # Return i if tokens[i] is on channel. Return the index of the EOF token + # if there are no tokens on channel between i and EOF. + # / + def nextTokenOnChannel(self, i: int, channel: int): + self.sync(i) + if i >= len(self.tokens): + return len(self.tokens) - 1 + token = self.tokens[i] + while token.channel != channel: + if token.type == Token.EOF: + return i + i += 1 + self.sync(i) + token = self.tokens[i] + return i + + # Given a starting index, return the index of the previous token on channel. + # Return i if tokens[i] is on channel. Return -1 if there are no tokens + # on channel between i and 0. + def previousTokenOnChannel(self, i: int, channel: int): + while i >= 0 and self.tokens[i].channel != channel: + i -= 1 + return i + + # Collect all tokens on specified channel to the right of + # the current token up until we see a token on DEFAULT_TOKEN_CHANNEL or + # EOF. If channel is -1, find any non default channel token. + def getHiddenTokensToRight(self, tokenIndex: int, channel: int = -1): + self.lazyInit() + if tokenIndex < 0 or tokenIndex >= len(self.tokens): + raise Exception( + str(tokenIndex) + " not in 0.." + str(len(self.tokens) - 1) + ) + from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import ( + Lexer, + ) + + nextOnChannel = self.nextTokenOnChannel( + tokenIndex + 1, Lexer.DEFAULT_TOKEN_CHANNEL + ) + from_ = tokenIndex + 1 + # if none onchannel to right, nextOnChannel=-1 so set to = last token + to = (len(self.tokens) - 1) if nextOnChannel == -1 else nextOnChannel + return self.filterForChannel(from_, to, channel) + + # Collect all tokens on specified channel to the left of + # the current token up until we see a token on DEFAULT_TOKEN_CHANNEL. + # If channel is -1, find any non default channel token. + def getHiddenTokensToLeft(self, tokenIndex: int, channel: int = -1): + self.lazyInit() + if tokenIndex < 0 or tokenIndex >= len(self.tokens): + raise Exception( + str(tokenIndex) + " not in 0.." + str(len(self.tokens) - 1) + ) + from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import ( + Lexer, + ) + + prevOnChannel = self.previousTokenOnChannel( + tokenIndex - 1, Lexer.DEFAULT_TOKEN_CHANNEL + ) + if prevOnChannel == tokenIndex - 1: + return None + # if none on channel to left, prevOnChannel=-1 then from=0 + from_ = prevOnChannel + 1 + to = tokenIndex - 1 + return self.filterForChannel(from_, to, channel) + + def filterForChannel(self, left: int, right: int, channel: int): + hidden = [] + for i in range(left, right + 1): + t = self.tokens[i] + if channel == -1: + from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import ( + Lexer, + ) + + if t.channel != Lexer.DEFAULT_TOKEN_CHANNEL: + hidden.append(t) + elif t.channel == channel: + hidden.append(t) + if len(hidden) == 0: + return None + return hidden + + def getSourceName(self): + return self.tokenSource.getSourceName() + + # Get the text of all tokens in this buffer.#/ + def getText(self, start: int = None, stop: int = None): + self.lazyInit() + self.fill() + if isinstance(start, Token): + start = start.tokenIndex + elif start is None: + start = 0 + if isinstance(stop, Token): + stop = stop.tokenIndex + elif stop is None or stop >= len(self.tokens): + stop = len(self.tokens) - 1 + if start < 0 or stop < 0 or stop < start: + return "" + with StringIO() as buf: + for i in range(start, stop + 1): + t = self.tokens[i] + if t.type == Token.EOF: + break + buf.write(t.text) + return buf.getvalue() + + # Get all tokens from lexer until EOF#/ + def fill(self): + self.lazyInit() + while self.fetch(1000) == 1000: + pass diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/CommonTokenFactory.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/CommonTokenFactory.py new file mode 100644 index 00000000..b5ea3d6f --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/CommonTokenFactory.py @@ -0,0 +1,73 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +# +# This default implementation of {@link TokenFactory} creates +# {@link CommonToken} objects. +# +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import CommonToken + + +class TokenFactory: + pass + + +class CommonTokenFactory(TokenFactory): + __slots__ = "copyText" + + # + # The default {@link CommonTokenFactory} instance. + # + #

      + # This token factory does not explicitly copy token text when constructing + # tokens.

      + # + DEFAULT = None + + def __init__(self, copyText: bool = False): + # Indicates whether {@link CommonToken#setText} should be called after + # constructing tokens to explicitly set the text. This is useful for cases + # where the input stream might not be able to provide arbitrary substrings + # of text from the input after the lexer creates a token (e.g. the + # implementation of {@link CharStream#getText} in + # {@link UnbufferedCharStream} throws an + # {@link UnsupportedOperationException}). Explicitly setting the token text + # allows {@link Token#getText} to be called at any time regardless of the + # input stream implementation. + # + #

      + # The default value is {@code false} to avoid the performance and memory + # overhead of copying text for every token unless explicitly requested.

      + # + self.copyText = copyText + + def create( + self, + source, + type: int, + text: str, + channel: int, + start: int, + stop: int, + line: int, + column: int, + ): + t = CommonToken(source, type, channel, start, stop) + t.line = line + t.column = column + if text is not None: + t.text = text + elif self.copyText and source[1] is not None: + t.text = source[1].getText(start, stop) + return t + + def createThin(self, type: int, text: str): + t = CommonToken(type=type) + t.text = text + return t + + +CommonTokenFactory.DEFAULT = CommonTokenFactory() diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/CommonTokenStream.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/CommonTokenStream.py new file mode 100644 index 00000000..b39d2c1b --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/CommonTokenStream.py @@ -0,0 +1,89 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# / + +# +# This class extends {@link BufferedTokenStream} with functionality to filter +# token streams to tokens on a particular channel (tokens where +# {@link Token#getChannel} returns a particular value). +# +#

      +# This token stream provides access to all tokens by index or when calling +# methods like {@link #getText}. The channel filtering is only used for code +# accessing tokens via the lookahead methods {@link #LA}, {@link #LT}, and +# {@link #LB}.

      +# +#

      +# By default, tokens are placed on the default channel +# ({@link Token#DEFAULT_CHANNEL}), but may be reassigned by using the +# {@code ->channel(HIDDEN)} lexer command, or by using an embedded action to +# call {@link Lexer#setChannel}. +#

      +# +#

      +# Note: lexer rules which use the {@code ->skip} lexer command or call +# {@link Lexer#skip} do not produce tokens at all, so input text matched by +# such a rule will not be available as part of the token stream, regardless of +# channel.

      +# / + +from cf_units._udunits2_parser.parser._antlr4_runtime.BufferedTokenStream import ( + BufferedTokenStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import Lexer +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + + +class CommonTokenStream(BufferedTokenStream): + __slots__ = "channel" + + def __init__(self, lexer: Lexer, channel: int = Token.DEFAULT_CHANNEL): + super().__init__(lexer) + self.channel = channel + + def adjustSeekIndex(self, i: int): + return self.nextTokenOnChannel(i, self.channel) + + def LB(self, k: int): + if k == 0 or (self.index - k) < 0: + return None + i = self.index + n = 1 + # find k good tokens looking backwards + while n <= k: + # skip off-channel tokens + i = self.previousTokenOnChannel(i - 1, self.channel) + n += 1 + if i < 0: + return None + return self.tokens[i] + + def LT(self, k: int): + self.lazyInit() + if k == 0: + return None + if k < 0: + return self.LB(-k) + i = self.index + n = 1 # we know tokens[pos] is a good one + # find k good tokens + while n < k: + # skip off-channel tokens, but make sure to not look past EOF + if self.sync(i + 1): + i = self.nextTokenOnChannel(i + 1, self.channel) + n += 1 + return self.tokens[i] + + # Count EOF just once.#/ + def getNumberOfOnChannelTokens(self): + n = 0 + self.fill() + for i in range(0, len(self.tokens)): + t = self.tokens[i] + if t.channel == self.channel: + n += 1 + if t.type == Token.EOF: + break + return n diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/FileStream.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/FileStream.py new file mode 100644 index 00000000..a6c0d140 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/FileStream.py @@ -0,0 +1,34 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +# +# This is an InputStream that is loaded from a file all at once +# when you construct the object. +# + +import codecs + +from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( + InputStream, +) + + +class FileStream(InputStream): + __slots__ = "fileName" + + def __init__( + self, fileName: str, encoding: str = "ascii", errors: str = "strict" + ): + super().__init__(self.readDataFrom(fileName, encoding, errors)) + self.fileName = fileName + + def readDataFrom( + self, fileName: str, encoding: str, errors: str = "strict" + ): + # read binary to avoid line ending conversion + with open(fileName, "rb") as file: + bytes = file.read() + return codecs.decode(bytes, encoding, errors) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/InputStream.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/InputStream.py new file mode 100644 index 00000000..d229979b --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/InputStream.py @@ -0,0 +1,89 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + + +# +# Vacuum all input from a string and then treat it like a buffer. +# +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + + +class InputStream: + __slots__ = ("name", "strdata", "_index", "data", "_size") + + def __init__(self, data: str): + self.name = "" + self.strdata = data + self._loadString() + + def _loadString(self): + self._index = 0 + self.data = [ord(c) for c in self.strdata] + self._size = len(self.data) + + @property + def index(self): + return self._index + + @property + def size(self): + return self._size + + # Reset the stream so that it's in the same state it was + # when the object was created *except* the data array is not + # touched. + # + def reset(self): + self._index = 0 + + def consume(self): + if self._index >= self._size: + assert self.LA(1) == Token.EOF + raise Exception("cannot consume EOF") + self._index += 1 + + def LA(self, offset: int): + if offset == 0: + return 0 # undefined + if offset < 0: + offset += 1 # e.g., translate LA(-1) to use offset=0 + pos = self._index + offset - 1 + if pos < 0 or pos >= self._size: # invalid + return Token.EOF + return self.data[pos] + + def LT(self, offset: int): + return self.LA(offset) + + # mark/release do nothing; we have entire buffer + def mark(self): + return -1 + + def release(self, marker: int): + pass + + # consume() ahead until p==_index; can't just set p=_index as we must + # update line and column. If we seek backwards, just set p + # + def seek(self, _index: int): + if _index <= self._index: + self._index = ( + _index # just jump; don't update stream state (line, ...) + ) + return + # seek forward + self._index = min(_index, self._size) + + def getText(self, start: int, stop: int): + if stop >= self._size: + stop = self._size - 1 + if start >= self._size: + return "" + else: + return self.strdata[start : stop + 1] + + def __str__(self): + return self.strdata diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/IntervalSet.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/IntervalSet.py new file mode 100644 index 00000000..5789d324 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/IntervalSet.py @@ -0,0 +1,183 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +from io import StringIO + +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + +# need forward declarations +IntervalSet = None + + +class IntervalSet: + __slots__ = ("intervals", "readonly") + + def __init__(self): + self.intervals = None + self.readonly = False + + def __iter__(self): + if self.intervals is not None: + for i in self.intervals: + for c in i: + yield c + + def __getitem__(self, item): + i = 0 + for k in self: + if i == item: + return k + else: + i += 1 + return Token.INVALID_TYPE + + def addOne(self, v: int): + self.addRange(range(v, v + 1)) + + def addRange(self, v: range): + if self.intervals is None: + self.intervals = list() + self.intervals.append(v) + else: + # find insert pos + k = 0 + for i in self.intervals: + # distinct range -> insert + if v.stop < i.start: + self.intervals.insert(k, v) + return + # contiguous range -> adjust + elif v.stop == i.start: + self.intervals[k] = range(v.start, i.stop) + return + # overlapping range -> adjust and reduce + elif v.start <= i.stop: + self.intervals[k] = range( + min(i.start, v.start), max(i.stop, v.stop) + ) + self.reduce(k) + return + k += 1 + # greater than any existing + self.intervals.append(v) + + def addSet(self, other: IntervalSet): + if other.intervals is not None: + for i in other.intervals: + self.addRange(i) + return self + + def reduce(self, k: int): + # only need to reduce if k is not the last + if k < len(self.intervals) - 1: + l = self.intervals[k] + r = self.intervals[k + 1] + # if r contained in l + if l.stop >= r.stop: + self.intervals.pop(k + 1) + self.reduce(k) + elif l.stop >= r.start: + self.intervals[k] = range(l.start, r.stop) + self.intervals.pop(k + 1) + + def complement(self, start, stop): + result = IntervalSet() + result.addRange(range(start, stop + 1)) + for i in self.intervals: + result.removeRange(i) + return result + + def __contains__(self, item): + if self.intervals is None: + return False + else: + return any(item in i for i in self.intervals) + + def __len__(self): + return sum(len(i) for i in self.intervals) + + def removeRange(self, v): + if v.start == v.stop - 1: + self.removeOne(v.start) + elif self.intervals is not None: + k = 0 + for i in self.intervals: + # intervals are ordered + if v.stop <= i.start: + return + # check for including range, split it + elif v.start > i.start and v.stop < i.stop: + self.intervals[k] = range(i.start, v.start) + x = range(v.stop, i.stop) + self.intervals.insert(k, x) + return + # check for included range, remove it + elif v.start <= i.start and v.stop >= i.stop: + self.intervals.pop(k) + k -= 1 # need another pass + # check for lower boundary + elif v.start < i.stop: + self.intervals[k] = range(i.start, v.start) + # check for upper boundary + elif v.stop < i.stop: + self.intervals[k] = range(v.stop, i.stop) + k += 1 + + def removeOne(self, v): + if self.intervals is not None: + k = 0 + for i in self.intervals: + # intervals is ordered + if v < i.start: + return + # check for single value range + elif v == i.start and v == i.stop - 1: + self.intervals.pop(k) + return + # check for lower boundary + elif v == i.start: + self.intervals[k] = range(i.start + 1, i.stop) + return + # check for upper boundary + elif v == i.stop - 1: + self.intervals[k] = range(i.start, i.stop - 1) + return + # split existing range + elif v < i.stop - 1: + x = range(i.start, v) + self.intervals[k] = range(v + 1, i.stop) + self.intervals.insert(k, x) + return + k += 1 + + def toString(self, literalNames: list, symbolicNames: list): + if self.intervals is None: + return "{}" + with StringIO() as buf: + if len(self) > 1: + buf.write("{") + first = True + for i in self.intervals: + for j in i: + if not first: + buf.write(", ") + buf.write(self.elementName(literalNames, symbolicNames, j)) + first = False + if len(self) > 1: + buf.write("}") + return buf.getvalue() + + def elementName(self, literalNames: list, symbolicNames: list, a: int): + if a == Token.EOF: + return "" + elif a == Token.EPSILON: + return "" + else: + if a < len(literalNames) and literalNames[a] != "": + return literalNames[a] + if a < len(symbolicNames): + return symbolicNames[a] + return "" diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/LL1Analyzer.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/LL1Analyzer.py new file mode 100644 index 00000000..376e2901 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/LL1Analyzer.py @@ -0,0 +1,258 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# / +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfig import ( + ATNConfig, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( + ATNState, + RuleStopState, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.Transition import ( + AbstractPredicateTransition, + NotSetTransition, + RuleTransition, + WildcardTransition, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.IntervalSet import ( + IntervalSet, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( + PredictionContext, + PredictionContextFromRuleContext, + SingletonPredictionContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( + RuleContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + + +class LL1Analyzer: + __slots__ = "atn" + + # * Special value added to the lookahead sets to indicate that we hit + # a predicate during analysis if {@code seeThruPreds==false}. + # / + HIT_PRED = Token.INVALID_TYPE + + def __init__(self, atn: ATN): + self.atn = atn + + # * + # Calculates the SLL(1) expected lookahead set for each outgoing transition + # of an {@link ATNState}. The returned array has one element for each + # outgoing transition in {@code s}. If the closure from transition + # i leads to a semantic predicate before matching a symbol, the + # element at index i of the result will be {@code null}. + # + # @param s the ATN state + # @return the expected symbols for each outgoing transition of {@code s}. + # / + def getDecisionLookahead(self, s: ATNState): + if s is None: + return None + + count = len(s.transitions) + look = [] * count + for alt in range(0, count): + look[alt] = set() + lookBusy = set() + seeThruPreds = False # fail to get lookahead upon pred + self._LOOK( + s.transition(alt).target, + None, + PredictionContext.EMPTY, + look[alt], + lookBusy, + set(), + seeThruPreds, + False, + ) + # Wipe out lookahead for this alternative if we found nothing + # or we had a predicate when we !seeThruPreds + if len(look[alt]) == 0 or self.HIT_PRED in look[alt]: + look[alt] = None + return look + + # * + # Compute set of tokens that can follow {@code s} in the ATN in the + # specified {@code ctx}. + # + #

      If {@code ctx} is {@code null} and the end of the rule containing + # {@code s} is reached, {@link Token#EPSILON} is added to the result set. + # If {@code ctx} is not {@code null} and the end of the outermost rule is + # reached, {@link Token#EOF} is added to the result set.

      + # + # @param s the ATN state + # @param stopState the ATN state to stop at. This can be a + # {@link BlockEndState} to detect epsilon paths through a closure. + # @param ctx the complete parser context, or {@code null} if the context + # should be ignored + # + # @return The set of tokens that can follow {@code s} in the ATN in the + # specified {@code ctx}. + # / + def LOOK( + self, s: ATNState, stopState: ATNState = None, ctx: RuleContext = None + ): + r = IntervalSet() + seeThruPreds = True # ignore preds; get all lookahead + lookContext = ( + PredictionContextFromRuleContext(s.atn, ctx) + if ctx is not None + else None + ) + self._LOOK( + s, stopState, lookContext, r, set(), set(), seeThruPreds, True + ) + return r + + # * + # Compute set of tokens that can follow {@code s} in the ATN in the + # specified {@code ctx}. + # + #

      If {@code ctx} is {@code null} and {@code stopState} or the end of the + # rule containing {@code s} is reached, {@link Token#EPSILON} is added to + # the result set. If {@code ctx} is not {@code null} and {@code addEOF} is + # {@code true} and {@code stopState} or the end of the outermost rule is + # reached, {@link Token#EOF} is added to the result set.

      + # + # @param s the ATN state. + # @param stopState the ATN state to stop at. This can be a + # {@link BlockEndState} to detect epsilon paths through a closure. + # @param ctx The outer context, or {@code null} if the outer context should + # not be used. + # @param look The result lookahead set. + # @param lookBusy A set used for preventing epsilon closures in the ATN + # from causing a stack overflow. Outside code should pass + # {@code new HashSet} for this argument. + # @param calledRuleStack A set used for preventing left recursion in the + # ATN from causing a stack overflow. Outside code should pass + # {@code new BitSet()} for this argument. + # @param seeThruPreds {@code true} to true semantic predicates as + # implicitly {@code true} and "see through them", otherwise {@code false} + # to treat semantic predicates as opaque and add {@link #HIT_PRED} to the + # result if one is encountered. + # @param addEOF Add {@link Token#EOF} to the result if the end of the + # outermost context is reached. This parameter has no effect if {@code ctx} + # is {@code null}. + # / + def _LOOK( + self, + s: ATNState, + stopState: ATNState, + ctx: PredictionContext, + look: IntervalSet, + lookBusy: set, + calledRuleStack: set, + seeThruPreds: bool, + addEOF: bool, + ): + c = ATNConfig(s, 0, ctx) + + if c in lookBusy: + return + lookBusy.add(c) + + if s == stopState: + if ctx is None: + look.addOne(Token.EPSILON) + return + elif ctx.isEmpty() and addEOF: + look.addOne(Token.EOF) + return + + if isinstance(s, RuleStopState): + if ctx is None: + look.addOne(Token.EPSILON) + return + elif ctx.isEmpty() and addEOF: + look.addOne(Token.EOF) + return + + if ctx != PredictionContext.EMPTY: + removed = s.ruleIndex in calledRuleStack + try: + calledRuleStack.discard(s.ruleIndex) + # run thru all possible stack tops in ctx + for i in range(0, len(ctx)): + returnState = self.atn.states[ctx.getReturnState(i)] + self._LOOK( + returnState, + stopState, + ctx.getParent(i), + look, + lookBusy, + calledRuleStack, + seeThruPreds, + addEOF, + ) + finally: + if removed: + calledRuleStack.add(s.ruleIndex) + return + + for t in s.transitions: + if type(t) == RuleTransition: + if t.target.ruleIndex in calledRuleStack: + continue + + newContext = SingletonPredictionContext.create( + ctx, t.followState.stateNumber + ) + + try: + calledRuleStack.add(t.target.ruleIndex) + self._LOOK( + t.target, + stopState, + newContext, + look, + lookBusy, + calledRuleStack, + seeThruPreds, + addEOF, + ) + finally: + calledRuleStack.remove(t.target.ruleIndex) + elif isinstance(t, AbstractPredicateTransition): + if seeThruPreds: + self._LOOK( + t.target, + stopState, + ctx, + look, + lookBusy, + calledRuleStack, + seeThruPreds, + addEOF, + ) + else: + look.addOne(self.HIT_PRED) + elif t.isEpsilon: + self._LOOK( + t.target, + stopState, + ctx, + look, + lookBusy, + calledRuleStack, + seeThruPreds, + addEOF, + ) + elif type(t) == WildcardTransition: + look.addRange( + range(Token.MIN_USER_TOKEN_TYPE, self.atn.maxTokenType + 1) + ) + else: + set_ = t.label + if set_ is not None: + if isinstance(t, NotSetTransition): + set_ = set_.complement( + Token.MIN_USER_TOKEN_TYPE, self.atn.maxTokenType + ) + look.addSet(set_) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/Lexer.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/Lexer.py new file mode 100644 index 00000000..24311729 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/Lexer.py @@ -0,0 +1,370 @@ +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# / + +# A lexer is recognizer that draws input symbols from a character stream. +# lexer grammars result in a subclass of self object. A Lexer object +# uses simplified match() and error recovery mechanisms in the interest +# of speed. +# / +import sys +from io import StringIO + +if sys.version_info[1] > 5: + from typing import TextIO +else: + from typing.io import TextIO +from cf_units._udunits2_parser.parser._antlr4_runtime.CommonTokenFactory import ( + CommonTokenFactory, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + IllegalStateException, + LexerNoViableAltException, + RecognitionException, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( + InputStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Recognizer import ( + Recognizer, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + + +class TokenSource: + pass + + +class Lexer(Recognizer, TokenSource): + __slots__ = ( + "_input", + "_output", + "_factory", + "_tokenFactorySourcePair", + "_token", + "_tokenStartCharIndex", + "_tokenStartLine", + "_tokenStartColumn", + "_hitEOF", + "_channel", + "_type", + "_modeStack", + "_mode", + "_text", + ) + + DEFAULT_MODE = 0 + MORE = -2 + SKIP = -3 + + DEFAULT_TOKEN_CHANNEL = Token.DEFAULT_CHANNEL + HIDDEN = Token.HIDDEN_CHANNEL + MIN_CHAR_VALUE = 0x0000 + MAX_CHAR_VALUE = 0x10FFFF + + def __init__(self, input: InputStream, output: TextIO = sys.stdout): + super().__init__() + self._input = input + self._output = output + self._factory = CommonTokenFactory.DEFAULT + self._tokenFactorySourcePair = (self, input) + + self._interp = None # child classes must populate this + + # The goal of all lexer rules/methods is to create a token object. + # self is an instance variable as multiple rules may collaborate to + # create a single token. nextToken will return self object after + # matching lexer rule(s). If you subclass to allow multiple token + # emissions, then set self to the last token to be matched or + # something nonnull so that the auto token emit mechanism will not + # emit another token. + self._token = None + + # What character index in the stream did the current token start at? + # Needed, for example, to get the text for current token. Set at + # the start of nextToken. + self._tokenStartCharIndex = -1 + + # The line on which the first character of the token resides#/ + self._tokenStartLine = -1 + + # The character position of first character within the line#/ + self._tokenStartColumn = -1 + + # Once we see EOF on char stream, next token will be EOF. + # If you have DONE : EOF ; then you see DONE EOF. + self._hitEOF = False + + # The channel number for the current token#/ + self._channel = Token.DEFAULT_CHANNEL + + # The token type for the current token#/ + self._type = Token.INVALID_TYPE + + self._modeStack = [] + self._mode = self.DEFAULT_MODE + + # You can set the text for the current token to override what is in + # the input char buffer. Use setText() or can set self instance var. + # / + self._text = None + + def reset(self): + # wack Lexer state variables + if self._input is not None: + self._input.seek(0) # rewind the input + self._token = None + self._type = Token.INVALID_TYPE + self._channel = Token.DEFAULT_CHANNEL + self._tokenStartCharIndex = -1 + self._tokenStartColumn = -1 + self._tokenStartLine = -1 + self._text = None + + self._hitEOF = False + self._mode = Lexer.DEFAULT_MODE + self._modeStack = [] + + self._interp.reset() + + # Return a token from self source; i.e., match a token on the char + # stream. + def nextToken(self): + if self._input is None: + raise IllegalStateException( + "nextToken requires a non-null input stream." + ) + + # Mark start location in char stream so unbuffered streams are + # guaranteed at least have text of current token + tokenStartMarker = self._input.mark() + try: + while True: + if self._hitEOF: + self.emitEOF() + return self._token + self._token = None + self._channel = Token.DEFAULT_CHANNEL + self._tokenStartCharIndex = self._input.index + self._tokenStartColumn = self._interp.column + self._tokenStartLine = self._interp.line + self._text = None + continueOuter = False + while True: + self._type = Token.INVALID_TYPE + ttype = self.SKIP + try: + ttype = self._interp.match(self._input, self._mode) + except LexerNoViableAltException as e: + self.notifyListeners(e) # report error + self.recover(e) + if self._input.LA(1) == Token.EOF: + self._hitEOF = True + if self._type == Token.INVALID_TYPE: + self._type = ttype + if self._type == self.SKIP: + continueOuter = True + break + if self._type != self.MORE: + break + if continueOuter: + continue + if self._token is None: + self.emit() + return self._token + finally: + # make sure we release marker after match or + # unbuffered char stream will keep buffering + self._input.release(tokenStartMarker) + + # Instruct the lexer to skip creating a token for current lexer rule + # and look for another token. nextToken() knows to keep looking when + # a lexer rule finishes with token set to SKIP_TOKEN. Recall that + # if token==null at end of any token rule, it creates one for you + # and emits it. + # / + def skip(self): + self._type = self.SKIP + + def more(self): + self._type = self.MORE + + def mode(self, m: int): + self._mode = m + + def pushMode(self, m: int): + if self._interp.debug: + print("pushMode " + str(m), file=self._output) + self._modeStack.append(self._mode) + self.mode(m) + + def popMode(self): + if len(self._modeStack) == 0: + raise Exception("Empty Stack") + if self._interp.debug: + print("popMode back to " + self._modeStack[:-1], file=self._output) + self.mode(self._modeStack.pop()) + return self._mode + + # Set the char stream and reset the lexer#/ + @property + def inputStream(self): + return self._input + + @inputStream.setter + def inputStream(self, input: InputStream): + self._input = None + self._tokenFactorySourcePair = (self, self._input) + self.reset() + self._input = input + self._tokenFactorySourcePair = (self, self._input) + + @property + def sourceName(self): + return self._input.sourceName + + # By default does not support multiple emits per nextToken invocation + # for efficiency reasons. Subclass and override self method, nextToken, + # and getToken (to push tokens into a list and pull from that list + # rather than a single variable as self implementation does). + # / + def emitToken(self, token: Token): + self._token = token + + # The standard method called to automatically emit a token at the + # outermost lexical rule. The token object should point into the + # char buffer start..stop. If there is a text override in 'text', + # use that to set the token's text. Override self method to emit + # custom Token objects or provide a new factory. + # / + def emit(self): + t = self._factory.create( + self._tokenFactorySourcePair, + self._type, + self._text, + self._channel, + self._tokenStartCharIndex, + self.getCharIndex() - 1, + self._tokenStartLine, + self._tokenStartColumn, + ) + self.emitToken(t) + return t + + def emitEOF(self): + cpos = self.column + lpos = self.line + eof = self._factory.create( + self._tokenFactorySourcePair, + Token.EOF, + None, + Token.DEFAULT_CHANNEL, + self._input.index, + self._input.index - 1, + lpos, + cpos, + ) + self.emitToken(eof) + return eof + + @property + def type(self): + return self._type + + @type.setter + def type(self, type: int): + self._type = type + + @property + def line(self): + return self._interp.line + + @line.setter + def line(self, line: int): + self._interp.line = line + + @property + def column(self): + return self._interp.column + + @column.setter + def column(self, column: int): + self._interp.column = column + + # What is the index of the current character of lookahead?#/ + def getCharIndex(self): + return self._input.index + + # Return the text matched so far for the current token or any + # text override. + @property + def text(self): + if self._text is not None: + return self._text + else: + return self._interp.getText(self._input) + + # Set the complete text of self token; it wipes any previous + # changes to the text. + @text.setter + def text(self, txt: str): + self._text = txt + + # Return a list of all Token objects in input char stream. + # Forces load of all tokens. Does not include EOF token. + # / + def getAllTokens(self): + tokens = [] + t = self.nextToken() + while t.type != Token.EOF: + tokens.append(t) + t = self.nextToken() + return tokens + + def notifyListeners(self, e: LexerNoViableAltException): + start = self._tokenStartCharIndex + stop = self._input.index + text = self._input.getText(start, stop) + msg = ( + "token recognition error at: '" + self.getErrorDisplay(text) + "'" + ) + listener = self.getErrorListenerDispatch() + listener.syntaxError( + self, None, self._tokenStartLine, self._tokenStartColumn, msg, e + ) + + def getErrorDisplay(self, s: str): + with StringIO() as buf: + for c in s: + buf.write(self.getErrorDisplayForChar(c)) + return buf.getvalue() + + def getErrorDisplayForChar(self, c: str): + if ord(c[0]) == Token.EOF: + return "" + elif c == "\n": + return "\\n" + elif c == "\t": + return "\\t" + elif c == "\r": + return "\\r" + else: + return c + + def getCharErrorDisplay(self, c: str): + return "'" + self.getErrorDisplayForChar(c) + "'" + + # Lexers can normally match any char in it's vocabulary after matching + # a token, so do the easy thing and just kill a character and hope + # it all works out. You can instead use the rule invocation stack + # to do sophisticated error recovery if you are in a fragment rule. + # / + def recover(self, re: RecognitionException): + if self._input.LA(1) != Token.EOF: + if isinstance(re, LexerNoViableAltException): + # skip a char and try again + self._interp.consume(self._input) + else: + # TODO: Do we lose character or line position information? + self._input.consume() diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/ListTokenSource.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/ListTokenSource.py new file mode 100644 index 00000000..a0851a7c --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/ListTokenSource.py @@ -0,0 +1,153 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +# +# Provides an implementation of {@link TokenSource} as a wrapper around a list +# of {@link Token} objects. +# +#

      If the final token in the list is an {@link Token#EOF} token, it will be used +# as the EOF token for every call to {@link #nextToken} after the end of the +# list is reached. Otherwise, an EOF token will be created.

      +# +from cf_units._udunits2_parser.parser._antlr4_runtime.CommonTokenFactory import ( + CommonTokenFactory, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import TokenSource +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + + +class ListTokenSource(TokenSource): + __slots__ = ("tokens", "sourceName", "pos", "eofToken", "_factory") + + # Constructs a new {@link ListTokenSource} instance from the specified + # collection of {@link Token} objects and source name. + # + # @param tokens The collection of {@link Token} objects to provide as a + # {@link TokenSource}. + # @param sourceName The name of the {@link TokenSource}. If this value is + # {@code null}, {@link #getSourceName} will attempt to infer the name from + # the next {@link Token} (or the previous token if the end of the input has + # been reached). + # + # @exception NullPointerException if {@code tokens} is {@code null} + # + def __init__(self, tokens: list, sourceName: str = None): + if tokens is None: + raise ReferenceError("tokens cannot be null") + self.tokens = tokens + self.sourceName = sourceName + # The index into {@link #tokens} of token to return by the next call to + # {@link #nextToken}. The end of the input is indicated by this value + # being greater than or equal to the number of items in {@link #tokens}. + self.pos = 0 + # This field caches the EOF token for the token source. + self.eofToken = None + # This is the backing field for {@link #getTokenFactory} and + self._factory = CommonTokenFactory.DEFAULT + + # + # {@inheritDoc} + # + @property + def column(self): + if self.pos < len(self.tokens): + return self.tokens[self.pos].column + elif self.eofToken is not None: + return self.eofToken.column + elif len(self.tokens) > 0: + # have to calculate the result from the line/column of the previous + # token, along with the text of the token. + lastToken = self.tokens[len(self.tokens) - 1] + tokenText = lastToken.text + if tokenText is not None: + lastNewLine = tokenText.rfind("\n") + if lastNewLine >= 0: + return len(tokenText) - lastNewLine - 1 + return lastToken.column + lastToken.stop - lastToken.start + 1 + + # only reach this if tokens is empty, meaning EOF occurs at the first + # position in the input + return 0 + + # + # {@inheritDoc} + # + def nextToken(self): + if self.pos >= len(self.tokens): + if self.eofToken is None: + start = -1 + if len(self.tokens) > 0: + previousStop = self.tokens[len(self.tokens) - 1].stop + if previousStop != -1: + start = previousStop + 1 + stop = max(-1, start - 1) + self.eofToken = self._factory.create( + (self, self.getInputStream()), + Token.EOF, + "EOF", + Token.DEFAULT_CHANNEL, + start, + stop, + self.line, + self.column, + ) + return self.eofToken + t = self.tokens[self.pos] + if self.pos == len(self.tokens) - 1 and t.type == Token.EOF: + self.eofToken = t + self.pos += 1 + return t + + # + # {@inheritDoc} + # + @property + def line(self): + if self.pos < len(self.tokens): + return self.tokens[self.pos].line + elif self.eofToken is not None: + return self.eofToken.line + elif len(self.tokens) > 0: + # have to calculate the result from the line/column of the previous + # token, along with the text of the token. + lastToken = self.tokens[len(self.tokens) - 1] + line = lastToken.line + tokenText = lastToken.text + if tokenText is not None: + line += tokenText.count("\n") + + # if no text is available, assume the token did not contain any newline characters. + return line + + # only reach this if tokens is empty, meaning EOF occurs at the first + # position in the input + return 1 + + # + # {@inheritDoc} + # + def getInputStream(self): + if self.pos < len(self.tokens): + return self.tokens[self.pos].getInputStream() + elif self.eofToken is not None: + return self.eofToken.getInputStream() + elif len(self.tokens) > 0: + return self.tokens[len(self.tokens) - 1].getInputStream() + else: + # no input stream information is available + return None + + # + # {@inheritDoc} + # + def getSourceName(self): + if self.sourceName is not None: + return self.sourceName + inputStream = self.getInputStream() + if inputStream is not None: + return inputStream.getSourceName() + else: + return "List" diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/Parser.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/Parser.py new file mode 100644 index 00000000..3c17a9a3 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/Parser.py @@ -0,0 +1,660 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +import sys + +if sys.version_info[1] > 5: + from typing import TextIO +else: + from typing.io import TextIO +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNDeserializationOptions import ( + ATNDeserializationOptions, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNDeserializer import ( + ATNDeserializer, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.BufferedTokenStream import ( + TokenStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.CommonTokenFactory import ( + TokenFactory, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + RecognitionException, + UnsupportedOperationException, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error.ErrorStrategy import ( + DefaultErrorStrategy, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( + InputStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import Lexer +from cf_units._udunits2_parser.parser._antlr4_runtime.ParserRuleContext import ( + ParserRuleContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Recognizer import ( + Recognizer, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( + RuleContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.ParseTreePatternMatcher import ( + ParseTreePatternMatcher, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( + ErrorNode, + ParseTreeListener, + TerminalNode, +) + + +class TraceListener(ParseTreeListener): + __slots__ = "_parser" + + def __init__(self, parser): + self._parser = parser + + def enterEveryRule(self, ctx): + print( + "enter " + + self._parser.ruleNames[ctx.getRuleIndex()] + + ", LT(1)=" + + self._parser._input.LT(1).text, + file=self._parser._output, + ) + + def visitTerminal(self, node): + print( + "consume " + + str(node.symbol) + + " rule " + + self._parser.ruleNames[self._parser._ctx.getRuleIndex()], + file=self._parser._output, + ) + + def visitErrorNode(self, node): + pass + + def exitEveryRule(self, ctx): + print( + "exit " + + self._parser.ruleNames[ctx.getRuleIndex()] + + ", LT(1)=" + + self._parser._input.LT(1).text, + file=self._parser._output, + ) + + +# self is all the parsing support code essentially; most of it is error recovery stuff.# +class Parser(Recognizer): + __slots__ = ( + "_input", + "_output", + "_errHandler", + "_precedenceStack", + "_ctx", + "buildParseTrees", + "_tracer", + "_parseListeners", + "_syntaxErrors", + ) + # self field maps from the serialized ATN string to the deserialized {@link ATN} with + # bypass alternatives. + # + # @see ATNDeserializationOptions#isGenerateRuleBypassTransitions() + # + bypassAltsAtnCache = dict() + + def __init__(self, input: TokenStream, output: TextIO = sys.stdout): + super().__init__() + # The input stream. + self._input = None + self._output = output + # The error handling strategy for the parser. The default value is a new + # instance of {@link DefaultErrorStrategy}. + self._errHandler = DefaultErrorStrategy() + self._precedenceStack = list() + self._precedenceStack.append(0) + # The {@link ParserRuleContext} object for the currently executing rule. + # self is always non-null during the parsing process. + self._ctx = None + # Specifies whether or not the parser should construct a parse tree during + # the parsing process. The default value is {@code true}. + self.buildParseTrees = True + # When {@link #setTrace}{@code (true)} is called, a reference to the + # {@link TraceListener} is stored here so it can be easily removed in a + # later call to {@link #setTrace}{@code (false)}. The listener itself is + # implemented as a parser listener so self field is not directly used by + # other parser methods. + self._tracer = None + # The list of {@link ParseTreeListener} listeners registered to receive + # events during the parse. + self._parseListeners = None + # The number of syntax errors reported during parsing. self value is + # incremented each time {@link #notifyErrorListeners} is called. + self._syntaxErrors = 0 + self.setInputStream(input) + + # reset the parser's state# + def reset(self): + if self._input is not None: + self._input.seek(0) + self._errHandler.reset(self) + self._ctx = None + self._syntaxErrors = 0 + self.setTrace(False) + self._precedenceStack = list() + self._precedenceStack.append(0) + if self._interp is not None: + self._interp.reset() + + # Match current input symbol against {@code ttype}. If the symbol type + # matches, {@link ANTLRErrorStrategy#reportMatch} and {@link #consume} are + # called to complete the match process. + # + #

      If the symbol type does not match, + # {@link ANTLRErrorStrategy#recoverInline} is called on the current error + # strategy to attempt recovery. If {@link #getBuildParseTree} is + # {@code true} and the token index of the symbol returned by + # {@link ANTLRErrorStrategy#recoverInline} is -1, the symbol is added to + # the parse tree by calling {@link ParserRuleContext#addErrorNode}.

      + # + # @param ttype the token type to match + # @return the matched symbol + # @throws RecognitionException if the current input symbol did not match + # {@code ttype} and the error strategy could not recover from the + # mismatched symbol + + def match(self, ttype: int): + t = self.getCurrentToken() + if t.type == ttype: + self._errHandler.reportMatch(self) + self.consume() + else: + t = self._errHandler.recoverInline(self) + if self.buildParseTrees and t.tokenIndex == -1: + # we must have conjured up a new token during single token insertion + # if it's not the current symbol + self._ctx.addErrorNode(t) + return t + + # Match current input symbol as a wildcard. If the symbol type matches + # (i.e. has a value greater than 0), {@link ANTLRErrorStrategy#reportMatch} + # and {@link #consume} are called to complete the match process. + # + #

      If the symbol type does not match, + # {@link ANTLRErrorStrategy#recoverInline} is called on the current error + # strategy to attempt recovery. If {@link #getBuildParseTree} is + # {@code true} and the token index of the symbol returned by + # {@link ANTLRErrorStrategy#recoverInline} is -1, the symbol is added to + # the parse tree by calling {@link ParserRuleContext#addErrorNode}.

      + # + # @return the matched symbol + # @throws RecognitionException if the current input symbol did not match + # a wildcard and the error strategy could not recover from the mismatched + # symbol + + def matchWildcard(self): + t = self.getCurrentToken() + if t.type > 0: + self._errHandler.reportMatch(self) + self.consume() + else: + t = self._errHandler.recoverInline(self) + if self.buildParseTrees and t.tokenIndex == -1: + # we must have conjured up a new token during single token insertion + # if it's not the current symbol + self._ctx.addErrorNode(t) + + return t + + def getParseListeners(self): + return list() if self._parseListeners is None else self._parseListeners + + # Registers {@code listener} to receive events during the parsing process. + # + #

      To support output-preserving grammar transformations (including but not + # limited to left-recursion removal, automated left-factoring, and + # optimized code generation), calls to listener methods during the parse + # may differ substantially from calls made by + # {@link ParseTreeWalker#DEFAULT} used after the parse is complete. In + # particular, rule entry and exit events may occur in a different order + # during the parse than after the parser. In addition, calls to certain + # rule entry methods may be omitted.

      + # + #

      With the following specific exceptions, calls to listener events are + # deterministic, i.e. for identical input the calls to listener + # methods will be the same.

      + # + #
        + #
      • Alterations to the grammar used to generate code may change the + # behavior of the listener calls.
      • + #
      • Alterations to the command line options passed to ANTLR 4 when + # generating the parser may change the behavior of the listener calls.
      • + #
      • Changing the version of the ANTLR Tool used to generate the parser + # may change the behavior of the listener calls.
      • + #
      + # + # @param listener the listener to add + # + # @throws NullPointerException if {@code} listener is {@code null} + # + def addParseListener(self, listener: ParseTreeListener): + if listener is None: + raise ReferenceError("listener") + if self._parseListeners is None: + self._parseListeners = [] + self._parseListeners.append(listener) + + # + # Remove {@code listener} from the list of parse listeners. + # + #

      If {@code listener} is {@code null} or has not been added as a parse + # listener, self method does nothing.

      + # @param listener the listener to remove + # + def removeParseListener(self, listener: ParseTreeListener): + if self._parseListeners is not None: + self._parseListeners.remove(listener) + if len(self._parseListeners) == 0: + self._parseListeners = None + + # Remove all parse listeners. + def removeParseListeners(self): + self._parseListeners = None + + # Notify any parse listeners of an enter rule event. + def triggerEnterRuleEvent(self): + if self._parseListeners is not None: + for listener in self._parseListeners: + listener.enterEveryRule(self._ctx) + self._ctx.enterRule(listener) + + # + # Notify any parse listeners of an exit rule event. + # + # @see #addParseListener + # + def triggerExitRuleEvent(self): + if self._parseListeners is not None: + # reverse order walk of listeners + for listener in reversed(self._parseListeners): + self._ctx.exitRule(listener) + listener.exitEveryRule(self._ctx) + + # Gets the number of syntax errors reported during parsing. This value is + # incremented each time {@link #notifyErrorListeners} is called. + # + # @see #notifyErrorListeners + # + def getNumberOfSyntaxErrors(self): + return self._syntaxErrors + + def getTokenFactory(self): + return self._input.tokenSource._factory + + # Tell our token source and error strategy about a new way to create tokens.# + def setTokenFactory(self, factory: TokenFactory): + self._input.tokenSource._factory = factory + + # The ATN with bypass alternatives is expensive to create so we create it + # lazily. + # + # @throws UnsupportedOperationException if the current parser does not + # implement the {@link #getSerializedATN()} method. + # + def getATNWithBypassAlts(self): + serializedAtn = self.getSerializedATN() + if serializedAtn is None: + raise UnsupportedOperationException( + "The current parser does not support an ATN with bypass alternatives." + ) + result = self.bypassAltsAtnCache.get(serializedAtn, None) + if result is None: + deserializationOptions = ATNDeserializationOptions() + deserializationOptions.generateRuleBypassTransitions = True + result = ATNDeserializer(deserializationOptions).deserialize( + serializedAtn + ) + self.bypassAltsAtnCache[serializedAtn] = result + return result + + # The preferred method of getting a tree pattern. For example, here's a + # sample use: + # + #
      +    # ParseTree t = parser.expr();
      +    # ParseTreePattern p = parser.compileParseTreePattern("<ID>+0", MyParser.RULE_expr);
      +    # ParseTreeMatch m = p.match(t);
      +    # String id = m.get("ID");
      +    # 
      + # + def compileParseTreePattern( + self, pattern: str, patternRuleIndex: int, lexer: Lexer = None + ): + if lexer is None: + if self.getTokenStream() is not None: + tokenSource = self.getTokenStream().tokenSource + if isinstance(tokenSource, Lexer): + lexer = tokenSource + if lexer is None: + raise UnsupportedOperationException( + "Parser can't discover a lexer to use" + ) + + m = ParseTreePatternMatcher(lexer, self) + return m.compile(pattern, patternRuleIndex) + + def getInputStream(self): + return self.getTokenStream() + + def setInputStream(self, input: InputStream): + self.setTokenStream(input) + + def getTokenStream(self): + return self._input + + # Set the token stream and reset the parser.# + def setTokenStream(self, input: TokenStream): + self._input = None + self.reset() + self._input = input + + # Match needs to return the current input symbol, which gets put + # into the label for the associated token ref; e.g., x=ID. + # + def getCurrentToken(self): + return self._input.LT(1) + + def notifyErrorListeners( + self, + msg: str, + offendingToken: Token = None, + e: RecognitionException = None, + ): + if offendingToken is None: + offendingToken = self.getCurrentToken() + self._syntaxErrors += 1 + line = offendingToken.line + column = offendingToken.column + listener = self.getErrorListenerDispatch() + listener.syntaxError(self, offendingToken, line, column, msg, e) + + # + # Consume and return the {@linkplain #getCurrentToken current symbol}. + # + #

      E.g., given the following input with {@code A} being the current + # lookahead symbol, self function moves the cursor to {@code B} and returns + # {@code A}.

      + # + #
      +    #  A B
      +    #  ^
      +    # 
      + # + # If the parser is not in error recovery mode, the consumed symbol is added + # to the parse tree using {@link ParserRuleContext#addChild(Token)}, and + # {@link ParseTreeListener#visitTerminal} is called on any parse listeners. + # If the parser is in error recovery mode, the consumed symbol is + # added to the parse tree using + # {@link ParserRuleContext#addErrorNode(Token)}, and + # {@link ParseTreeListener#visitErrorNode} is called on any parse + # listeners. + # + def consume(self): + o = self.getCurrentToken() + if o.type != Token.EOF: + self.getInputStream().consume() + hasListener = ( + self._parseListeners is not None and len(self._parseListeners) > 0 + ) + if self.buildParseTrees or hasListener: + if self._errHandler.inErrorRecoveryMode(self): + node = self._ctx.addErrorNode(o) + else: + node = self._ctx.addTokenNode(o) + if hasListener: + for listener in self._parseListeners: + if isinstance(node, ErrorNode): + listener.visitErrorNode(node) + elif isinstance(node, TerminalNode): + listener.visitTerminal(node) + return o + + def addContextToParseTree(self): + # add current context to parent if we have a parent + if self._ctx.parentCtx is not None: + self._ctx.parentCtx.addChild(self._ctx) + + # Always called by generated parsers upon entry to a rule. Access field + # {@link #_ctx} get the current context. + # + def enterRule( + self, localctx: ParserRuleContext, state: int, ruleIndex: int + ): + self.state = state + self._ctx = localctx + self._ctx.start = self._input.LT(1) + if self.buildParseTrees: + self.addContextToParseTree() + if self._parseListeners is not None: + self.triggerEnterRuleEvent() + + def exitRule(self): + self._ctx.stop = self._input.LT(-1) + # trigger event on _ctx, before it reverts to parent + if self._parseListeners is not None: + self.triggerExitRuleEvent() + self.state = self._ctx.invokingState + self._ctx = self._ctx.parentCtx + + def enterOuterAlt(self, localctx: ParserRuleContext, altNum: int): + localctx.setAltNumber(altNum) + # if we have new localctx, make sure we replace existing ctx + # that is previous child of parse tree + if self.buildParseTrees and self._ctx != localctx: + if self._ctx.parentCtx is not None: + self._ctx.parentCtx.removeLastChild() + self._ctx.parentCtx.addChild(localctx) + self._ctx = localctx + + # Get the precedence level for the top-most precedence rule. + # + # @return The precedence level for the top-most precedence rule, or -1 if + # the parser context is not nested within a precedence rule. + # + def getPrecedence(self): + if len(self._precedenceStack) == 0: + return -1 + else: + return self._precedenceStack[-1] + + def enterRecursionRule( + self, + localctx: ParserRuleContext, + state: int, + ruleIndex: int, + precedence: int, + ): + self.state = state + self._precedenceStack.append(precedence) + self._ctx = localctx + self._ctx.start = self._input.LT(1) + if self._parseListeners is not None: + self.triggerEnterRuleEvent() # simulates rule entry for left-recursive rules + + # + # Like {@link #enterRule} but for recursive rules. + # + def pushNewRecursionContext( + self, localctx: ParserRuleContext, state: int, ruleIndex: int + ): + previous = self._ctx + previous.parentCtx = localctx + previous.invokingState = state + previous.stop = self._input.LT(-1) + + self._ctx = localctx + self._ctx.start = previous.start + if self.buildParseTrees: + self._ctx.addChild(previous) + + if self._parseListeners is not None: + self.triggerEnterRuleEvent() # simulates rule entry for left-recursive rules + + def unrollRecursionContexts(self, parentCtx: ParserRuleContext): + self._precedenceStack.pop() + self._ctx.stop = self._input.LT(-1) + retCtx = self._ctx # save current ctx (return value) + # unroll so _ctx is as it was before call to recursive method + if self._parseListeners is not None: + while self._ctx is not parentCtx: + self.triggerExitRuleEvent() + self._ctx = self._ctx.parentCtx + else: + self._ctx = parentCtx + + # hook into tree + retCtx.parentCtx = parentCtx + + if self.buildParseTrees and parentCtx is not None: + # add return ctx into invoking rule's tree + parentCtx.addChild(retCtx) + + def getInvokingContext(self, ruleIndex: int): + ctx = self._ctx + while ctx is not None: + if ctx.getRuleIndex() == ruleIndex: + return ctx + ctx = ctx.parentCtx + return None + + def precpred(self, localctx: RuleContext, precedence: int): + return precedence >= self._precedenceStack[-1] + + def inContext(self, context: str): + # TODO: useful in parser? + return False + + # + # Checks whether or not {@code symbol} can follow the current state in the + # ATN. The behavior of self method is equivalent to the following, but is + # implemented such that the complete context-sensitive follow set does not + # need to be explicitly constructed. + # + #
      +    # return getExpectedTokens().contains(symbol);
      +    # 
      + # + # @param symbol the symbol type to check + # @return {@code true} if {@code symbol} can follow the current state in + # the ATN, otherwise {@code false}. + # + def isExpectedToken(self, symbol: int): + atn = self._interp.atn + ctx = self._ctx + s = atn.states[self.state] + following = atn.nextTokens(s) + if symbol in following: + return True + if Token.EPSILON not in following: + return False + + while ( + ctx is not None + and ctx.invokingState >= 0 + and Token.EPSILON in following + ): + invokingState = atn.states[ctx.invokingState] + rt = invokingState.transitions[0] + following = atn.nextTokens(rt.followState) + if symbol in following: + return True + ctx = ctx.parentCtx + + if Token.EPSILON in following and symbol == Token.EOF: + return True + else: + return False + + # Computes the set of input symbols which could follow the current parser + # state and context, as given by {@link #getState} and {@link #getContext}, + # respectively. + # + # @see ATN#getExpectedTokens(int, RuleContext) + # + def getExpectedTokens(self): + return self._interp.atn.getExpectedTokens(self.state, self._ctx) + + def getExpectedTokensWithinCurrentRule(self): + atn = self._interp.atn + s = atn.states[self.state] + return atn.nextTokens(s) + + # Get a rule's index (i.e., {@code RULE_ruleName} field) or -1 if not found.# + def getRuleIndex(self, ruleName: str): + ruleIndex = self.getRuleIndexMap().get(ruleName, None) + if ruleIndex is not None: + return ruleIndex + else: + return -1 + + # Return List<String> of the rule names in your parser instance + # leading up to a call to the current rule. You could override if + # you want more details such as the file/line info of where + # in the ATN a rule is invoked. + # + # this is very useful for error messages. + # + def getRuleInvocationStack(self, p: RuleContext = None): + if p is None: + p = self._ctx + stack = list() + while p is not None: + # compute what follows who invoked us + ruleIndex = p.getRuleIndex() + if ruleIndex < 0: + stack.append("n/a") + else: + stack.append(self.ruleNames[ruleIndex]) + p = p.parentCtx + return stack + + # For debugging and other purposes.# + def getDFAStrings(self): + return [str(dfa) for dfa in self._interp.decisionToDFA] + + # For debugging and other purposes.# + def dumpDFA(self): + seenOne = False + for i in range(0, len(self._interp.decisionToDFA)): + dfa = self._interp.decisionToDFA[i] + if len(dfa.states) > 0: + if seenOne: + print(file=self._output) + print("Decision " + str(dfa.decision) + ":", file=self._output) + print( + dfa.toString(self.literalNames, self.symbolicNames), + end="", + file=self._output, + ) + seenOne = True + + def getSourceName(self): + return self._input.sourceName + + # During a parse is sometimes useful to listen in on the rule entry and exit + # events as well as token matches. self is for quick and dirty debugging. + # + def setTrace(self, trace: bool): + if not trace: + self.removeParseListener(self._tracer) + self._tracer = None + else: + if self._tracer is not None: + self.removeParseListener(self._tracer) + self._tracer = TraceListener(self) + self.addParseListener(self._tracer) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/ParserInterpreter.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/ParserInterpreter.py new file mode 100644 index 00000000..9461f67b --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/ParserInterpreter.py @@ -0,0 +1,232 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +# A parser simulator that mimics what ANTLR's generated +# parser code does. A ParserATNSimulator is used to make +# predictions via adaptivePredict but this class moves a pointer through the +# ATN to simulate parsing. ParserATNSimulator just +# makes us efficient rather than having to backtrack, for example. +# +# This properly creates parse trees even for left recursive rules. +# +# We rely on the left recursive rule invocation and special predicate +# transitions to make left recursive rules work. +# +# See TestParserInterpreter for examples. +# +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( + ATNState, + LoopEndState, + StarLoopEntryState, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ParserATNSimulator import ( + ParserATNSimulator, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.Transition import ( + Transition, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.BufferedTokenStream import ( + TokenStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFA import DFA +from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + FailedPredicateException, + RecognitionException, + UnsupportedOperationException, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import Lexer +from cf_units._udunits2_parser.parser._antlr4_runtime.Parser import Parser +from cf_units._udunits2_parser.parser._antlr4_runtime.ParserRuleContext import ( + InterpreterRuleContext, + ParserRuleContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( + PredictionContextCache, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + + +class ParserInterpreter(Parser): + __slots__ = ( + "grammarFileName", + "atn", + "tokenNames", + "ruleNames", + "decisionToDFA", + "sharedContextCache", + "_parentContextStack", + "pushRecursionContextStates", + ) + + def __init__( + self, + grammarFileName: str, + tokenNames: list, + ruleNames: list, + atn: ATN, + input: TokenStream, + ): + super().__init__(input) + self.grammarFileName = grammarFileName + self.atn = atn + self.tokenNames = tokenNames + self.ruleNames = ruleNames + self.decisionToDFA = [DFA(state) for state in atn.decisionToState] + self.sharedContextCache = PredictionContextCache() + self._parentContextStack = list() + # identify the ATN states where pushNewRecursionContext must be called + self.pushRecursionContextStates = set() + for state in atn.states: + if not isinstance(state, StarLoopEntryState): + continue + if state.isPrecedenceDecision: + self.pushRecursionContextStates.add(state.stateNumber) + # get atn simulator that knows how to do predictions + self._interp = ParserATNSimulator( + self, atn, self.decisionToDFA, self.sharedContextCache + ) + + # Begin parsing at startRuleIndex# + def parse(self, startRuleIndex: int): + startRuleStartState = self.atn.ruleToStartState[startRuleIndex] + rootContext = InterpreterRuleContext( + None, ATNState.INVALID_STATE_NUMBER, startRuleIndex + ) + if startRuleStartState.isPrecedenceRule: + self.enterRecursionRule( + rootContext, startRuleStartState.stateNumber, startRuleIndex, 0 + ) + else: + self.enterRule( + rootContext, startRuleStartState.stateNumber, startRuleIndex + ) + while True: + p = self.getATNState() + if p.stateType == ATNState.RULE_STOP: + # pop; return from rule + if len(self._ctx) == 0: + if startRuleStartState.isPrecedenceRule: + result = self._ctx + parentContext = self._parentContextStack.pop() + self.unrollRecursionContexts(parentContext.a) + return result + else: + self.exitRule() + return rootContext + self.visitRuleStopState(p) + + else: + try: + self.visitState(p) + except RecognitionException as e: + self.state = self.atn.ruleToStopState[ + p.ruleIndex + ].stateNumber + self._ctx.exception = e + self._errHandler.reportError(self, e) + self._errHandler.recover(self, e) + + def enterRecursionRule( + self, + localctx: ParserRuleContext, + state: int, + ruleIndex: int, + precedence: int, + ): + self._parentContextStack.append((self._ctx, localctx.invokingState)) + super().enterRecursionRule(localctx, state, ruleIndex, precedence) + + def getATNState(self): + return self.atn.states[self.state] + + def visitState(self, p: ATNState): + edge = 0 + if len(p.transitions) > 1: + self._errHandler.sync(self) + edge = self._interp.adaptivePredict( + self._input, p.decision, self._ctx + ) + else: + edge = 1 + + transition = p.transitions[edge - 1] + tt = transition.serializationType + if tt == Transition.EPSILON: + if self.pushRecursionContextStates[ + p.stateNumber + ] and not isinstance(transition.target, LoopEndState): + t = self._parentContextStack[-1] + ctx = InterpreterRuleContext(t[0], t[1], self._ctx.ruleIndex) + self.pushNewRecursionContext( + ctx, + self.atn.ruleToStartState[p.ruleIndex].stateNumber, + self._ctx.ruleIndex, + ) + + elif tt == Transition.ATOM: + self.match(transition.label) + + elif tt in [Transition.RANGE, Transition.SET, Transition.NOT_SET]: + if not transition.matches( + self._input.LA(1), + Token.MIN_USER_TOKEN_TYPE, + Lexer.MAX_CHAR_VALUE, + ): + self._errHandler.recoverInline(self) + self.matchWildcard() + + elif tt == Transition.WILDCARD: + self.matchWildcard() + + elif tt == Transition.RULE: + ruleStartState = transition.target + ruleIndex = ruleStartState.ruleIndex + ctx = InterpreterRuleContext(self._ctx, p.stateNumber, ruleIndex) + if ruleStartState.isPrecedenceRule: + self.enterRecursionRule( + ctx, + ruleStartState.stateNumber, + ruleIndex, + transition.precedence, + ) + else: + self.enterRule(ctx, transition.target.stateNumber, ruleIndex) + + elif tt == Transition.PREDICATE: + if not self.sempred( + self._ctx, transition.ruleIndex, transition.predIndex + ): + raise FailedPredicateException(self) + + elif tt == Transition.ACTION: + self.action( + self._ctx, transition.ruleIndex, transition.actionIndex + ) + + elif tt == Transition.PRECEDENCE: + if not self.precpred(self._ctx, transition.precedence): + msg = "precpred(_ctx, " + str(transition.precedence) + ")" + raise FailedPredicateException(self, msg) + + else: + raise UnsupportedOperationException( + "Unrecognized ATN transition type." + ) + + self.state = transition.target.stateNumber + + def visitRuleStopState(self, p: ATNState): + ruleStartState = self.atn.ruleToStartState[p.ruleIndex] + if ruleStartState.isPrecedenceRule: + parentContext = self._parentContextStack.pop() + self.unrollRecursionContexts(parentContext.a) + self.state = parentContext[1] + else: + self.exitRule() + + ruleTransition = self.atn.states[self.state].transitions[0] + self.state = ruleTransition.followState.stateNumber diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/ParserRuleContext.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/ParserRuleContext.py new file mode 100644 index 00000000..7c877002 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/ParserRuleContext.py @@ -0,0 +1,203 @@ +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. + +# * A rule invocation record for parsing. +# +# Contains all of the information about the current rule not stored in the +# RuleContext. It handles parse tree children list, Any ATN state +# tracing, and the default values available for rule indications: +# start, stop, rule index, current alt number, current +# ATN state. +# +# Subclasses made for each rule and grammar track the parameters, +# return values, locals, and labels specific to that rule. These +# are the objects that are returned from rules. +# +# Note text is not an actual field of a rule return value; it is computed +# from start and stop using the input stream's toString() method. I +# could add a ctor to this so that we can pass in and store the input +# stream, but I'm not sure we want to do that. It would seem to be undefined +# to get the .text property anyway if the rule matches tokens from multiple +# input streams. +# +# I do not use getters for fields of objects that are used simply to +# group values such as this aggregate. The getters/setters are there to +# satisfy the superclass interface. + +from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( + RuleContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( + INVALID_INTERVAL, + ErrorNodeImpl, + ParseTree, + ParseTreeListener, + TerminalNode, + TerminalNodeImpl, +) + +# need forward declaration +ParserRuleContext = None + + +class ParserRuleContext(RuleContext): + __slots__ = ("children", "start", "stop", "exception") + + def __init__( + self, parent: ParserRuleContext = None, invokingStateNumber: int = None + ): + super().__init__(parent, invokingStateNumber) + # * If we are debugging or building a parse tree for a visitor, + # we need to track all of the tokens and rule invocations associated + # with this rule's context. This is empty for parsing w/o tree constr. + # operation because we don't the need to track the details about + # how we parse this rule. + # / + self.children = None + self.start = None + self.stop = None + # The exception that forced this rule to return. If the rule successfully + # completed, this is {@code null}. + self.exception = None + + # * COPY a ctx (I'm deliberately not using copy constructor)#/ + # + # This is used in the generated parser code to flip a generic XContext + # node for rule X to a YContext for alt label Y. In that sense, it is + # not really a generic copy function. + # + # If we do an error sync() at start of a rule, we might add error nodes + # to the generic XContext so this function must copy those nodes to + # the YContext as well else they are lost! + # / + def copyFrom(self, ctx: ParserRuleContext): + # from RuleContext + self.parentCtx = ctx.parentCtx + self.invokingState = ctx.invokingState + self.children = None + self.start = ctx.start + self.stop = ctx.stop + + # copy any error nodes to alt label node + if ctx.children is not None: + self.children = [] + # reset parent pointer for any error nodes + for child in ctx.children: + if isinstance(child, ErrorNodeImpl): + self.children.append(child) + child.parentCtx = self + + # Double dispatch methods for listeners + def enterRule(self, listener: ParseTreeListener): + pass + + def exitRule(self, listener: ParseTreeListener): + pass + + # * Does not set parent link; other add methods do that#/ + def addChild(self, child: ParseTree): + if self.children is None: + self.children = [] + self.children.append(child) + return child + + # * Used by enterOuterAlt to toss out a RuleContext previously added as + # we entered a rule. If we have # label, we will need to remove + # generic ruleContext object. + # / + def removeLastChild(self): + if self.children is not None: + del self.children[len(self.children) - 1] + + def addTokenNode(self, token: Token): + node = TerminalNodeImpl(token) + self.addChild(node) + node.parentCtx = self + return node + + def addErrorNode(self, badToken: Token): + node = ErrorNodeImpl(badToken) + self.addChild(node) + node.parentCtx = self + return node + + def getChild(self, i: int, ttype: type = None): + if ttype is None: + return self.children[i] if len(self.children) > i else None + else: + for child in self.getChildren(): + if not isinstance(child, ttype): + continue + if i == 0: + return child + i -= 1 + return None + + def getChildren(self, predicate=None): + if self.children is not None: + for child in self.children: + if predicate is not None and not predicate(child): + continue + yield child + + def getToken(self, ttype: int, i: int): + for child in self.getChildren(): + if not isinstance(child, TerminalNode): + continue + if child.symbol.type != ttype: + continue + if i == 0: + return child + i -= 1 + return None + + def getTokens(self, ttype: int): + if self.getChildren() is None: + return [] + tokens = [] + for child in self.getChildren(): + if not isinstance(child, TerminalNode): + continue + if child.symbol.type != ttype: + continue + tokens.append(child) + return tokens + + def getTypedRuleContext(self, ctxType: type, i: int): + return self.getChild(i, ctxType) + + def getTypedRuleContexts(self, ctxType: type): + children = self.getChildren() + if children is None: + return [] + contexts = [] + for child in children: + if not isinstance(child, ctxType): + continue + contexts.append(child) + return contexts + + def getChildCount(self): + return len(self.children) if self.children else 0 + + def getSourceInterval(self): + if self.start is None or self.stop is None: + return INVALID_INTERVAL + else: + return (self.start.tokenIndex, self.stop.tokenIndex) + + +RuleContext.EMPTY = ParserRuleContext() + + +class InterpreterRuleContext(ParserRuleContext): + def __init__( + self, + parent: ParserRuleContext, + invokingStateNumber: int, + ruleIndex: int, + ): + super().__init__(parent, invokingStateNumber) + self.ruleIndex = ruleIndex diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/PredictionContext.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/PredictionContext.py new file mode 100644 index 00000000..58ae297a --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/PredictionContext.py @@ -0,0 +1,704 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# / +from io import StringIO + +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + IllegalStateException, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( + RuleContext, +) + +# dup ParserATNSimulator class var here to avoid circular import; no idea why this can't be in PredictionContext +_trace_atn_sim = False + + +class PredictionContext: + # Represents {@code $} in local context prediction, which means wildcard. + # {@code#+x =#}. + # / + EMPTY = None + + # Represents {@code $} in an array in full context mode, when {@code $} + # doesn't mean wildcard: {@code $ + x = [$,x]}. Here, + # {@code $} = {@link #EMPTY_RETURN_STATE}. + # / + EMPTY_RETURN_STATE = 0x7FFFFFFF + + globalNodeCount = 1 + id = globalNodeCount + + # Stores the computed hash code of this {@link PredictionContext}. The hash + # code is computed in parts to match the following reference algorithm. + # + #
      +    #  private int referenceHashCode() {
      +    #      int hash = {@link MurmurHash#initialize MurmurHash.initialize}({@link #INITIAL_HASH});
      +    #
      +    #      for (int i = 0; i < {@link #size()}; i++) {
      +    #          hash = {@link MurmurHash#update MurmurHash.update}(hash, {@link #getParent getParent}(i));
      +    #      }
      +    #
      +    #      for (int i = 0; i < {@link #size()}; i++) {
      +    #          hash = {@link MurmurHash#update MurmurHash.update}(hash, {@link #getReturnState getReturnState}(i));
      +    #      }
      +    #
      +    #      hash = {@link MurmurHash#finish MurmurHash.finish}(hash, 2# {@link #size()});
      +    #      return hash;
      +    #  }
      +    # 
      + # / + + def __init__(self, cachedHashCode: int): + self.cachedHashCode = cachedHashCode + + def __len__(self): + return 0 + + # This means only the {@link #EMPTY} context is in set. + def isEmpty(self): + return self is self.EMPTY + + def hasEmptyPath(self): + return self.getReturnState(len(self) - 1) == self.EMPTY_RETURN_STATE + + def getReturnState(self, index: int): + raise IllegalStateException("illegal!") + + def __hash__(self): + return self.cachedHashCode + + +def calculateHashCode(parent: PredictionContext, returnState: int): + return hash("") if parent is None else hash((hash(parent), returnState)) + + +def calculateListsHashCode(parents: [], returnStates: []): + h = 0 + for parent, returnState in zip(parents, returnStates, strict=False): + h = hash((h, calculateHashCode(parent, returnState))) + return h + + +# Used to cache {@link PredictionContext} objects. Its used for the shared +# context cash associated with contexts in DFA states. This cache +# can be used for both lexers and parsers. + + +class PredictionContextCache: + def __init__(self): + self.cache = dict() + + # Add a context to the cache and return it. If the context already exists, + # return that one instead and do not add a new context to the cache. + # Protect shared cache from unsafe thread access. + # + def add(self, ctx: PredictionContext): + if ctx == PredictionContext.EMPTY: + return PredictionContext.EMPTY + existing = self.cache.get(ctx, None) + if existing is not None: + return existing + self.cache[ctx] = ctx + return ctx + + def get(self, ctx: PredictionContext): + return self.cache.get(ctx, None) + + def __len__(self): + return len(self.cache) + + +class SingletonPredictionContext(PredictionContext): + @staticmethod + def create(parent: PredictionContext, returnState: int): + if ( + returnState == PredictionContext.EMPTY_RETURN_STATE + and parent is None + ): + # someone can pass in the bits of an array ctx that mean $ + return SingletonPredictionContext.EMPTY + else: + return SingletonPredictionContext(parent, returnState) + + def __init__(self, parent: PredictionContext, returnState: int): + hashCode = calculateHashCode(parent, returnState) + super().__init__(hashCode) + self.parentCtx = parent + self.returnState = returnState + + def __len__(self): + return 1 + + def getParent(self, index: int): + return self.parentCtx + + def getReturnState(self, index: int): + return self.returnState + + def __eq__(self, other): + if self is other: + return True + elif other is None: + return False + elif not isinstance(other, SingletonPredictionContext): + return False + else: + return ( + self.returnState == other.returnState + and self.parentCtx == other.parentCtx + ) + + def __hash__(self): + return self.cachedHashCode + + def __str__(self): + up = "" if self.parentCtx is None else str(self.parentCtx) + if len(up) == 0: + if self.returnState == self.EMPTY_RETURN_STATE: + return "$" + else: + return str(self.returnState) + else: + return str(self.returnState) + " " + up + + +class EmptyPredictionContext(SingletonPredictionContext): + def __init__(self): + super().__init__(None, PredictionContext.EMPTY_RETURN_STATE) + + def isEmpty(self): + return True + + def __eq__(self, other): + return self is other + + def __hash__(self): + return self.cachedHashCode + + def __str__(self): + return "$" + + +PredictionContext.EMPTY = EmptyPredictionContext() + + +class ArrayPredictionContext(PredictionContext): + # Parent can be null only if full ctx mode and we make an array + # from {@link #EMPTY} and non-empty. We merge {@link #EMPTY} by using null parent and + # returnState == {@link #EMPTY_RETURN_STATE}. + + def __init__(self, parents: list, returnStates: list): + super().__init__(calculateListsHashCode(parents, returnStates)) + self.parents = parents + self.returnStates = returnStates + + def isEmpty(self): + # since EMPTY_RETURN_STATE can only appear in the last position, we + # don't need to verify that size==1 + return self.returnStates[0] == PredictionContext.EMPTY_RETURN_STATE + + def __len__(self): + return len(self.returnStates) + + def getParent(self, index: int): + return self.parents[index] + + def getReturnState(self, index: int): + return self.returnStates[index] + + def __eq__(self, other): + if self is other: + return True + elif not isinstance(other, ArrayPredictionContext): + return False + elif hash(self) != hash(other): + return False # can't be same if hash is different + else: + return ( + self.returnStates == other.returnStates + and self.parents == other.parents + ) + + def __str__(self): + if self.isEmpty(): + return "[]" + with StringIO() as buf: + buf.write("[") + for i in range(0, len(self.returnStates)): + if i > 0: + buf.write(", ") + if ( + self.returnStates[i] + == PredictionContext.EMPTY_RETURN_STATE + ): + buf.write("$") + continue + buf.write(str(self.returnStates[i])) + if self.parents[i] is not None: + buf.write(" ") + buf.write(str(self.parents[i])) + else: + buf.write("null") + buf.write("]") + return buf.getvalue() + + def __hash__(self): + return self.cachedHashCode + + +# Convert a {@link RuleContext} tree to a {@link PredictionContext} graph. +# Return {@link #EMPTY} if {@code outerContext} is empty or null. +# / +def PredictionContextFromRuleContext( + atn: ATN, outerContext: RuleContext = None +): + if outerContext is None: + outerContext = RuleContext.EMPTY + + # if we are in RuleContext of start rule, s, then PredictionContext + # is EMPTY. Nobody called us. (if we are empty, return empty) + if outerContext.parentCtx is None or outerContext is RuleContext.EMPTY: + return PredictionContext.EMPTY + + # If we have a parent, convert it to a PredictionContext graph + parent = PredictionContextFromRuleContext(atn, outerContext.parentCtx) + state = atn.states[outerContext.invokingState] + transition = state.transitions[0] + return SingletonPredictionContext.create( + parent, transition.followState.stateNumber + ) + + +def merge( + a: PredictionContext, + b: PredictionContext, + rootIsWildcard: bool, + mergeCache: dict, +): + # share same graph if both same + if a == b: + return a + + if isinstance(a, SingletonPredictionContext) and isinstance( + b, SingletonPredictionContext + ): + return mergeSingletons(a, b, rootIsWildcard, mergeCache) + + # At least one of a or b is array + # If one is $ and rootIsWildcard, return $ as# wildcard + if rootIsWildcard: + if isinstance(a, EmptyPredictionContext): + return a + if isinstance(b, EmptyPredictionContext): + return b + + # convert singleton so both are arrays to normalize + if isinstance(a, SingletonPredictionContext): + a = ArrayPredictionContext([a.parentCtx], [a.returnState]) + if isinstance(b, SingletonPredictionContext): + b = ArrayPredictionContext([b.parentCtx], [b.returnState]) + return mergeArrays(a, b, rootIsWildcard, mergeCache) + + +# +# Merge two {@link SingletonPredictionContext} instances. +# +#

      Stack tops equal, parents merge is same; return left graph.
      +#

      +# +#

      Same stack top, parents differ; merge parents giving array node, then +# remainders of those graphs. A new root node is created to point to the +# merged parents.
      +#

      +# +#

      Different stack tops pointing to same parent. Make array node for the +# root where both element in the root point to the same (original) +# parent.
      +#

      +# +#

      Different stack tops pointing to different parents. Make array node for +# the root where each element points to the corresponding original +# parent.
      +#

      +# +# @param a the first {@link SingletonPredictionContext} +# @param b the second {@link SingletonPredictionContext} +# @param rootIsWildcard {@code true} if this is a local-context merge, +# otherwise false to indicate a full-context merge +# @param mergeCache +# / +def mergeSingletons( + a: SingletonPredictionContext, + b: SingletonPredictionContext, + rootIsWildcard: bool, + mergeCache: dict, +): + if mergeCache is not None: + previous = mergeCache.get((a, b), None) + if previous is not None: + return previous + previous = mergeCache.get((b, a), None) + if previous is not None: + return previous + + merged = mergeRoot(a, b, rootIsWildcard) + if merged is not None: + if mergeCache is not None: + mergeCache[(a, b)] = merged + return merged + + if a.returnState == b.returnState: + parent = merge(a.parentCtx, b.parentCtx, rootIsWildcard, mergeCache) + # if parent is same as existing a or b parent or reduced to a parent, return it + if parent == a.parentCtx: + return a # ax + bx = ax, if a=b + if parent == b.parentCtx: + return b # ax + bx = bx, if a=b + # else: ax + ay = a'[x,y] + # merge parents x and y, giving array node with x,y then remainders + # of those graphs. dup a, a' points at merged array + # new joined parent so create new singleton pointing to it, a' + merged = SingletonPredictionContext.create(parent, a.returnState) + if mergeCache is not None: + mergeCache[(a, b)] = merged + return merged + else: # a != b payloads differ + # see if we can collapse parents due to $+x parents if local ctx + singleParent = None + if a is b or ( + a.parentCtx is not None and a.parentCtx == b.parentCtx + ): # ax + bx = [a,b]x + singleParent = a.parentCtx + if singleParent is not None: # parents are same + # sort payloads and use same parent + payloads = [a.returnState, b.returnState] + if a.returnState > b.returnState: + payloads = [b.returnState, a.returnState] + parents = [singleParent, singleParent] + merged = ArrayPredictionContext(parents, payloads) + if mergeCache is not None: + mergeCache[(a, b)] = merged + return merged + # parents differ and can't merge them. Just pack together + # into array; can't merge. + # ax + by = [ax,by] + payloads = [a.returnState, b.returnState] + parents = [a.parentCtx, b.parentCtx] + if a.returnState > b.returnState: # sort by payload + payloads = [b.returnState, a.returnState] + parents = [b.parentCtx, a.parentCtx] + merged = ArrayPredictionContext(parents, payloads) + if mergeCache is not None: + mergeCache[(a, b)] = merged + return merged + + +# +# Handle case where at least one of {@code a} or {@code b} is +# {@link #EMPTY}. In the following diagrams, the symbol {@code $} is used +# to represent {@link #EMPTY}. +# +#

      Local-Context Merges

      +# +#

      These local-context merge operations are used when {@code rootIsWildcard} +# is true.

      +# +#

      {@link #EMPTY} is superset of any graph; return {@link #EMPTY}.
      +#

      +# +#

      {@link #EMPTY} and anything is {@code #EMPTY}, so merged parent is +# {@code #EMPTY}; return left graph.
      +#

      +# +#

      Special case of last merge if local context.
      +#

      +# +#

      Full-Context Merges

      +# +#

      These full-context merge operations are used when {@code rootIsWildcard} +# is false.

      +# +#

      +# +#

      Must keep all contexts; {@link #EMPTY} in array is a special value (and +# null parent).
      +#

      +# +#

      +# +# @param a the first {@link SingletonPredictionContext} +# @param b the second {@link SingletonPredictionContext} +# @param rootIsWildcard {@code true} if this is a local-context merge, +# otherwise false to indicate a full-context merge +# / +def mergeRoot( + a: SingletonPredictionContext, + b: SingletonPredictionContext, + rootIsWildcard: bool, +): + if rootIsWildcard: + if a == PredictionContext.EMPTY: + return PredictionContext.EMPTY ## + b =# + if b == PredictionContext.EMPTY: + return PredictionContext.EMPTY # a +# =# + else: + if a == PredictionContext.EMPTY and b == PredictionContext.EMPTY: + return PredictionContext.EMPTY # $ + $ = $ + elif a == PredictionContext.EMPTY: # $ + x = [$,x] + payloads = [b.returnState, PredictionContext.EMPTY_RETURN_STATE] + parents = [b.parentCtx, None] + return ArrayPredictionContext(parents, payloads) + elif ( + b == PredictionContext.EMPTY + ): # x + $ = [$,x] ($ is always first if present) + payloads = [a.returnState, PredictionContext.EMPTY_RETURN_STATE] + parents = [a.parentCtx, None] + return ArrayPredictionContext(parents, payloads) + return None + + +# +# Merge two {@link ArrayPredictionContext} instances. +# +#

      Different tops, different parents.
      +#

      +# +#

      Shared top, same parents.
      +#

      +# +#

      Shared top, different parents.
      +#

      +# +#

      Shared top, all shared parents.
      +#

      +# +#

      Equal tops, merge parents and reduce top to +# {@link SingletonPredictionContext}.
      +#

      +# / +def mergeArrays( + a: ArrayPredictionContext, + b: ArrayPredictionContext, + rootIsWildcard: bool, + mergeCache: dict, +): + if mergeCache is not None: + previous = mergeCache.get((a, b), None) + if previous is not None: + if _trace_atn_sim: + print( + "mergeArrays a=" + str(a) + ",b=" + str(b) + " -> previous" + ) + return previous + previous = mergeCache.get((b, a), None) + if previous is not None: + if _trace_atn_sim: + print( + "mergeArrays a=" + str(a) + ",b=" + str(b) + " -> previous" + ) + return previous + + # merge sorted payloads a + b => M + i = 0 # walks a + j = 0 # walks b + k = 0 # walks target M array + + mergedReturnStates = [None] * (len(a.returnStates) + len(b.returnStates)) + mergedParents = [None] * len(mergedReturnStates) + # walk and merge to yield mergedParents, mergedReturnStates + while i < len(a.returnStates) and j < len(b.returnStates): + a_parent = a.parents[i] + b_parent = b.parents[j] + if a.returnStates[i] == b.returnStates[j]: + # same payload (stack tops are equal), must yield merged singleton + payload = a.returnStates[i] + # $+$ = $ + bothDollars = ( + payload == PredictionContext.EMPTY_RETURN_STATE + and a_parent is None + and b_parent is None + ) + ax_ax = ( + a_parent is not None and b_parent is not None + ) and a_parent == b_parent # ax+ax -> ax + if bothDollars or ax_ax: + mergedParents[k] = a_parent # choose left + mergedReturnStates[k] = payload + else: # ax+ay -> a'[x,y] + mergedParent = merge( + a_parent, b_parent, rootIsWildcard, mergeCache + ) + mergedParents[k] = mergedParent + mergedReturnStates[k] = payload + i += 1 # hop over left one as usual + j += 1 # but also skip one in right side since we merge + elif a.returnStates[i] < b.returnStates[j]: # copy a[i] to M + mergedParents[k] = a_parent + mergedReturnStates[k] = a.returnStates[i] + i += 1 + else: # b > a, copy b[j] to M + mergedParents[k] = b_parent + mergedReturnStates[k] = b.returnStates[j] + j += 1 + k += 1 + + # copy over any payloads remaining in either array + if i < len(a.returnStates): + for p in range(i, len(a.returnStates)): + mergedParents[k] = a.parents[p] + mergedReturnStates[k] = a.returnStates[p] + k += 1 + else: + for p in range(j, len(b.returnStates)): + mergedParents[k] = b.parents[p] + mergedReturnStates[k] = b.returnStates[p] + k += 1 + + # trim merged if we combined a few that had same stack tops + if k < len(mergedParents): # write index < last position; trim + if k == 1: # for just one merged element, return singleton top + merged = SingletonPredictionContext.create( + mergedParents[0], mergedReturnStates[0] + ) + if mergeCache is not None: + mergeCache[(a, b)] = merged + return merged + mergedParents = mergedParents[0:k] + mergedReturnStates = mergedReturnStates[0:k] + + merged = ArrayPredictionContext(mergedParents, mergedReturnStates) + + # if we created same array as a or b, return that instead + # TODO: track whether this is possible above during merge sort for speed + if merged == a: + if mergeCache is not None: + mergeCache[(a, b)] = a + if _trace_atn_sim: + print("mergeArrays a=" + str(a) + ",b=" + str(b) + " -> a") + return a + if merged == b: + if mergeCache is not None: + mergeCache[(a, b)] = b + if _trace_atn_sim: + print("mergeArrays a=" + str(a) + ",b=" + str(b) + " -> b") + return b + combineCommonParents(mergedParents) + + if mergeCache is not None: + mergeCache[(a, b)] = merged + + if _trace_atn_sim: + print("mergeArrays a=" + str(a) + ",b=" + str(b) + " -> " + str(M)) + + return merged + + +# +# Make pass over all M {@code parents}; merge any {@code equals()} +# ones. +# / +def combineCommonParents(parents: list): + uniqueParents = dict() + + for p in range(0, len(parents)): + parent = parents[p] + if uniqueParents.get(parent, None) is None: + uniqueParents[parent] = parent + + for p in range(0, len(parents)): + parents[p] = uniqueParents[parents[p]] + + +def getCachedPredictionContext( + context: PredictionContext, + contextCache: PredictionContextCache, + visited: dict, +): + if context.isEmpty(): + return context + existing = visited.get(context) + if existing is not None: + return existing + existing = contextCache.get(context) + if existing is not None: + visited[context] = existing + return existing + changed = False + parents = [None] * len(context) + for i in range(0, len(parents)): + parent = getCachedPredictionContext( + context.getParent(i), contextCache, visited + ) + if changed or parent is not context.getParent(i): + if not changed: + parents = [context.getParent(j) for j in range(len(context))] + changed = True + parents[i] = parent + if not changed: + contextCache.add(context) + visited[context] = context + return context + + updated = None + if len(parents) == 0: + updated = PredictionContext.EMPTY + elif len(parents) == 1: + updated = SingletonPredictionContext.create( + parents[0], context.getReturnState(0) + ) + else: + updated = ArrayPredictionContext(parents, context.returnStates) + + contextCache.add(updated) + visited[updated] = updated + visited[context] = updated + + return updated + + +# # extra structures, but cut/paste/morphed works, so leave it. +# # seems to do a breadth-first walk +# public static List getAllNodes(PredictionContext context) { +# Map visited = +# new IdentityHashMap(); +# Deque workList = new ArrayDeque(); +# workList.add(context); +# visited.put(context, context); +# List nodes = new ArrayList(); +# while (!workList.isEmpty()) { +# PredictionContext current = workList.pop(); +# nodes.add(current); +# for (int i = 0; i < current.size(); i++) { +# PredictionContext parent = current.getParent(i); +# if ( parent!=null && visited.put(parent, parent) == null) { +# workList.push(parent); +# } +# } +# } +# return nodes; +# } + + +# ter's recursive version of Sam's getAllNodes() +def getAllContextNodes( + context: PredictionContext, nodes: list = None, visited: dict = None +): + if nodes is None: + nodes = list() + return getAllContextNodes(context, nodes, visited) + elif visited is None: + visited = dict() + return getAllContextNodes(context, nodes, visited) + else: + if context is None or visited.get(context, None) is not None: + return nodes + visited.put(context, context) + nodes.add(context) + for i in range(0, len(context)): + getAllContextNodes(context.getParent(i), nodes, visited) + return nodes diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/Recognizer.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/Recognizer.py new file mode 100644 index 00000000..f74fa66a --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/Recognizer.py @@ -0,0 +1,167 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# +from cf_units._udunits2_parser.parser._antlr4_runtime.error.ErrorListener import ( + ConsoleErrorListener, + ProxyErrorListener, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( + RuleContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + +# need forward delcaration +RecognitionException = None + + +class Recognizer: + __slots__ = ("_listeners", "_interp", "_stateNumber") + + tokenTypeMapCache = dict() + ruleIndexMapCache = dict() + + def __init__(self): + self._listeners = [ConsoleErrorListener.INSTANCE] + self._interp = None + self._stateNumber = -1 + + def extractVersion(self, version): + pos = version.find(".") + major = version[0:pos] + version = version[pos + 1 :] + pos = version.find(".") + if pos == -1: + pos = version.find("-") + if pos == -1: + pos = len(version) + minor = version[0:pos] + return major, minor + + def checkVersion(self, toolVersion): + runtimeVersion = "4.13.2" + rvmajor, rvminor = self.extractVersion(runtimeVersion) + tvmajor, tvminor = self.extractVersion(toolVersion) + if rvmajor != tvmajor or rvminor != tvminor: + print( + "ANTLR runtime and generated code versions disagree: " + + runtimeVersion + + "!=" + + toolVersion + ) + + def addErrorListener(self, listener): + self._listeners.append(listener) + + def removeErrorListener(self, listener): + self._listeners.remove(listener) + + def removeErrorListeners(self): + self._listeners = [] + + def getTokenTypeMap(self): + tokenNames = self.getTokenNames() + if tokenNames is None: + from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + UnsupportedOperationException, + ) + + raise UnsupportedOperationException( + "The current recognizer does not provide a list of token names." + ) + result = self.tokenTypeMapCache.get(tokenNames, None) + if result is None: + result = zip(tokenNames, range(0, len(tokenNames)), strict=False) + result["EOF"] = Token.EOF + self.tokenTypeMapCache[tokenNames] = result + return result + + # Get a map from rule names to rule indexes. + # + #

      Used for XPath and tree pattern compilation.

      + # + def getRuleIndexMap(self): + ruleNames = self.getRuleNames() + if ruleNames is None: + from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + UnsupportedOperationException, + ) + + raise UnsupportedOperationException( + "The current recognizer does not provide a list of rule names." + ) + result = self.ruleIndexMapCache.get(ruleNames, None) + if result is None: + result = zip(ruleNames, range(0, len(ruleNames)), strict=False) + self.ruleIndexMapCache[ruleNames] = result + return result + + def getTokenType(self, tokenName: str): + ttype = self.getTokenTypeMap().get(tokenName, None) + if ttype is not None: + return ttype + else: + return Token.INVALID_TYPE + + # What is the error header, normally line/character position information?# + def getErrorHeader(self, e: RecognitionException): + line = e.getOffendingToken().line + column = e.getOffendingToken().column + return "line " + line + ":" + column + + # How should a token be displayed in an error message? The default + # is to display just the text, but during development you might + # want to have a lot of information spit out. Override in that case + # to use t.toString() (which, for CommonToken, dumps everything about + # the token). This is better than forcing you to override a method in + # your token objects because you don't have to go modify your lexer + # so that it creates a new Java type. + # + # @deprecated This method is not called by the ANTLR 4 Runtime. Specific + # implementations of {@link ANTLRErrorStrategy} may provide a similar + # feature when necessary. For example, see + # {@link DefaultErrorStrategy#getTokenErrorDisplay}. + # + def getTokenErrorDisplay(self, t: Token): + if t is None: + return "" + s = t.text + if s is None: + if t.type == Token.EOF: + s = "" + else: + s = "<" + str(t.type) + ">" + s = s.replace("\n", "\\n") + s = s.replace("\r", "\\r") + s = s.replace("\t", "\\t") + return "'" + s + "'" + + def getErrorListenerDispatch(self): + return ProxyErrorListener(self._listeners) + + # subclass needs to override these if there are sempreds or actions + # that the ATN interp needs to execute + def sempred(self, localctx: RuleContext, ruleIndex: int, actionIndex: int): + return True + + def precpred(self, localctx: RuleContext, precedence: int): + return True + + @property + def state(self): + return self._stateNumber + + # Indicate that the recognizer has changed internal state that is + # consistent with the ATN state passed in. This way we always know + # where we are in the ATN as the parser goes along. The rule + # context objects form a stack that lets us see the stack of + # invoking rules. Combine this and we have complete ATN + # configuration information. + + @state.setter + def state(self, atnState: int): + self._stateNumber = atnState + + +del RecognitionException diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/RuleContext.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/RuleContext.py new file mode 100644 index 00000000..9378f5e3 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/RuleContext.py @@ -0,0 +1,239 @@ +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# / + + +# A rule context is a record of a single rule invocation. It knows +# which context invoked it, if any. If there is no parent context, then +# naturally the invoking state is not valid. The parent link +# provides a chain upwards from the current rule invocation to the root +# of the invocation tree, forming a stack. We actually carry no +# information about the rule associated with this context (except +# when parsing). We keep only the state number of the invoking state from +# the ATN submachine that invoked this. Contrast this with the s +# pointer inside ParserRuleContext that tracks the current state +# being "executed" for the current rule. +# +# The parent contexts are useful for computing lookahead sets and +# getting error information. +# +# These objects are used during parsing and prediction. +# For the special case of parsers, we use the subclass +# ParserRuleContext. +# +# @see ParserRuleContext +# / +from io import StringIO + +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( + INVALID_INTERVAL, + ParseTreeVisitor, + RuleNode, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Trees import Trees + +# need forward declarations +RuleContext = None +Parser = None + + +class RuleContext(RuleNode): + __slots__ = ("parentCtx", "invokingState") + EMPTY = None + + def __init__(self, parent: RuleContext = None, invokingState: int = -1): + super().__init__() + # What context invoked this rule? + self.parentCtx = parent + # What state invoked the rule associated with this context? + # The "return address" is the followState of invokingState + # If parent is null, this should be -1. + self.invokingState = invokingState + + def depth(self): + n = 0 + p = self + while p is not None: + p = p.parentCtx + n += 1 + return n + + # A context is empty if there is no invoking state; meaning nobody call + # current context. + def isEmpty(self): + return self.invokingState == -1 + + # satisfy the ParseTree / SyntaxTree interface + + def getSourceInterval(self): + return INVALID_INTERVAL + + def getRuleContext(self): + return self + + def getPayload(self): + return self + + # Return the combined text of all child nodes. This method only considers + # tokens which have been added to the parse tree. + #

      + # Since tokens on hidden channels (e.g. whitespace or comments) are not + # added to the parse trees, they will not appear in the output of this + # method. + # / + def getText(self): + if self.getChildCount() == 0: + return "" + with StringIO() as builder: + for child in self.getChildren(): + builder.write(child.getText()) + return builder.getvalue() + + def getRuleIndex(self): + return -1 + + # For rule associated with this parse tree internal node, return + # the outer alternative number used to match the input. Default + # implementation does not compute nor store this alt num. Create + # a subclass of ParserRuleContext with backing field and set + # option contextSuperClass. + # to set it. + def getAltNumber(self): + return 0 # should use ATN.INVALID_ALT_NUMBER but won't compile + + # Set the outer alternative number for this context node. Default + # implementation does nothing to avoid backing field overhead for + # trees that don't need it. Create + # a subclass of ParserRuleContext with backing field and set + # option contextSuperClass. + def setAltNumber(self, altNumber: int): + pass + + def getChild(self, i: int): + return None + + def getChildCount(self): + return 0 + + def getChildren(self): + for c in []: + yield c + + def accept(self, visitor: ParseTreeVisitor): + return visitor.visitChildren(self) + + # # Call this method to view a parse tree in a dialog box visually.#/ + # public Future inspect(@Nullable Parser parser) { + # List ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null; + # return inspect(ruleNames); + # } + # + # public Future inspect(@Nullable List ruleNames) { + # TreeViewer viewer = new TreeViewer(ruleNames, this); + # return viewer.open(); + # } + # + # # Save this tree in a postscript file#/ + # public void save(@Nullable Parser parser, String fileName) + # throws IOException, PrintException + # { + # List ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null; + # save(ruleNames, fileName); + # } + # + # # Save this tree in a postscript file using a particular font name and size#/ + # public void save(@Nullable Parser parser, String fileName, + # String fontName, int fontSize) + # throws IOException + # { + # List ruleNames = parser != null ? Arrays.asList(parser.getRuleNames()) : null; + # save(ruleNames, fileName, fontName, fontSize); + # } + # + # # Save this tree in a postscript file#/ + # public void save(@Nullable List ruleNames, String fileName) + # throws IOException, PrintException + # { + # Trees.writePS(this, ruleNames, fileName); + # } + # + # # Save this tree in a postscript file using a particular font name and size#/ + # public void save(@Nullable List ruleNames, String fileName, + # String fontName, int fontSize) + # throws IOException + # { + # Trees.writePS(this, ruleNames, fileName, fontName, fontSize); + # } + # + # # Print out a whole tree, not just a node, in LISP format + # # (root child1 .. childN). Print just a node if this is a leaf. + # # We have to know the recognizer so we can get rule names. + # #/ + # @Override + # public String toStringTree(@Nullable Parser recog) { + # return Trees.toStringTree(this, recog); + # } + # + # Print out a whole tree, not just a node, in LISP format + # (root child1 .. childN). Print just a node if this is a leaf. + # + def toStringTree(self, ruleNames: list = None, recog: Parser = None): + return Trees.toStringTree(self, ruleNames=ruleNames, recog=recog) + + # } + # + # @Override + # public String toStringTree() { + # return toStringTree((List)null); + # } + # + def __str__(self): + return self.toString(None, None) + + # @Override + # public String toString() { + # return toString((List)null, (RuleContext)null); + # } + # + # public final String toString(@Nullable Recognizer recog) { + # return toString(recog, ParserRuleContext.EMPTY); + # } + # + # public final String toString(@Nullable List ruleNames) { + # return toString(ruleNames, null); + # } + # + # // recog null unless ParserRuleContext, in which case we use subclass toString(...) + # public String toString(@Nullable Recognizer recog, @Nullable RuleContext stop) { + # String[] ruleNames = recog != null ? recog.getRuleNames() : null; + # List ruleNamesList = ruleNames != null ? Arrays.asList(ruleNames) : null; + # return toString(ruleNamesList, stop); + # } + + def toString(self, ruleNames: list, stop: RuleContext) -> str: + with StringIO() as buf: + p = self + buf.write("[") + while p is not None and p is not stop: + if ruleNames is None: + if not p.isEmpty(): + buf.write(str(p.invokingState)) + else: + ri = p.getRuleIndex() + ruleName = ( + ruleNames[ri] + if ri >= 0 and ri < len(ruleNames) + else str(ri) + ) + buf.write(ruleName) + + if p.parentCtx is not None and ( + ruleNames is not None or not p.parentCtx.isEmpty() + ): + buf.write(" ") + + p = p.parentCtx + + buf.write("]") + return buf.getvalue() diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/StdinStream.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/StdinStream.py new file mode 100644 index 00000000..631e3735 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/StdinStream.py @@ -0,0 +1,15 @@ +import codecs +import sys + +from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( + InputStream, +) + + +class StdinStream(InputStream): + def __init__( + self, encoding: str = "ascii", errors: str = "strict" + ) -> None: + bytes = sys.stdin.buffer.read() + data = codecs.decode(bytes, encoding, errors) + super().__init__(data) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/Token.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/Token.py new file mode 100644 index 00000000..1f5abc18 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/Token.py @@ -0,0 +1,177 @@ +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +# A token has properties: text, type, line, character position in the line +# (so we can ignore tabs), token channel, index, and source from which +# we obtained this token. +from io import StringIO + + +class Token: + __slots__ = ( + "source", + "type", + "channel", + "start", + "stop", + "tokenIndex", + "line", + "column", + "_text", + ) + + INVALID_TYPE = 0 + + # During lookahead operations, this "token" signifies we hit rule end ATN state + # and did not follow it despite needing to. + EPSILON = -2 + + MIN_USER_TOKEN_TYPE = 1 + + EOF = -1 + + # All tokens go to the parser (unless skip() is called in that rule) + # on a particular "channel". The parser tunes to a particular channel + # so that whitespace etc... can go to the parser on a "hidden" channel. + + DEFAULT_CHANNEL = 0 + + # Anything on different channel than DEFAULT_CHANNEL is not parsed + # by parser. + + HIDDEN_CHANNEL = 1 + + def __init__(self): + self.source = None + self.type = None # token type of the token + self.channel = ( + None # The parser ignores everything not on DEFAULT_CHANNEL + ) + self.start = None # optional; return -1 if not implemented. + self.stop = None # optional; return -1 if not implemented. + self.tokenIndex = ( + None # from 0..n-1 of the token object in the input stream + ) + self.line = None # line=1..n of the 1st character + self.column = None # beginning of the line at which it occurs, 0..n-1 + self._text = None # text of the token. + + @property + def text(self): + return self._text + + # Explicitly set the text for this token. If {code text} is not + # {@code null}, then {@link #getText} will return this value rather than + # extracting the text from the input. + # + # @param text The explicit text of the token, or {@code null} if the text + # should be obtained from the input along with the start and stop indexes + # of the token. + + @text.setter + def text(self, text: str): + self._text = text + + def getTokenSource(self): + return self.source[0] + + def getInputStream(self): + return self.source[1] + + +class CommonToken(Token): + # An empty {@link Pair} which is used as the default value of + # {@link #source} for tokens that do not have a source. + EMPTY_SOURCE = (None, None) + + def __init__( + self, + source: tuple = EMPTY_SOURCE, + type: int = None, + channel: int = Token.DEFAULT_CHANNEL, + start: int = -1, + stop: int = -1, + ): + super().__init__() + self.source = source + self.type = type + self.channel = channel + self.start = start + self.stop = stop + self.tokenIndex = -1 + if source[0] is not None: + self.line = source[0].line + self.column = source[0].column + else: + self.column = -1 + + # Constructs a new {@link CommonToken} as a copy of another {@link Token}. + # + #

      + # If {@code oldToken} is also a {@link CommonToken} instance, the newly + # constructed token will share a reference to the {@link #text} field and + # the {@link Pair} stored in {@link #source}. Otherwise, {@link #text} will + # be assigned the result of calling {@link #getText}, and {@link #source} + # will be constructed from the result of {@link Token#getTokenSource} and + # {@link Token#getInputStream}.

      + # + # @param oldToken The token to copy. + # + def clone(self): + t = CommonToken( + self.source, self.type, self.channel, self.start, self.stop + ) + t.tokenIndex = self.tokenIndex + t.line = self.line + t.column = self.column + t.text = self.text + return t + + @property + def text(self): + if self._text is not None: + return self._text + input = self.getInputStream() + if input is None: + return None + n = input.size + if self.start < n and self.stop < n: + return input.getText(self.start, self.stop) + else: + return "" + + @text.setter + def text(self, text: str): + self._text = text + + def __str__(self): + with StringIO() as buf: + buf.write("[@") + buf.write(str(self.tokenIndex)) + buf.write(",") + buf.write(str(self.start)) + buf.write(":") + buf.write(str(self.stop)) + buf.write("='") + txt = self.text + if txt is not None: + txt = txt.replace("\n", "\\n") + txt = txt.replace("\r", "\\r") + txt = txt.replace("\t", "\\t") + else: + txt = "" + buf.write(txt) + buf.write("',<") + buf.write(str(self.type)) + buf.write(">") + if self.channel > 0: + buf.write(",channel=") + buf.write(str(self.channel)) + buf.write(",") + buf.write(str(self.line)) + buf.write(":") + buf.write(str(self.column)) + buf.write("]") + return buf.getvalue() diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/TokenStreamRewriter.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/TokenStreamRewriter.py new file mode 100644 index 00000000..d7fe6506 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/TokenStreamRewriter.py @@ -0,0 +1,331 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +from io import StringIO + +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + + +class TokenStreamRewriter: + __slots__ = ("tokens", "programs", "lastRewriteTokenIndexes") + + DEFAULT_PROGRAM_NAME = "default" + PROGRAM_INIT_SIZE = 100 + MIN_TOKEN_INDEX = 0 + + def __init__(self, tokens): + """ + :type tokens: antlr4.BufferedTokenStream.BufferedTokenStream + :param tokens: + :return: + """ + super(TokenStreamRewriter, self).__init__() + self.tokens = tokens + self.programs = {self.DEFAULT_PROGRAM_NAME: []} + self.lastRewriteTokenIndexes = {} + + def getTokenStream(self): + return self.tokens + + def rollback(self, instruction_index, program_name): + ins = self.programs.get(program_name, None) + if ins: + self.programs[program_name] = ins[ + self.MIN_TOKEN_INDEX : instruction_index + ] + + def deleteProgram(self, program_name=DEFAULT_PROGRAM_NAME): + self.rollback(self.MIN_TOKEN_INDEX, program_name) + + def insertAfterToken(self, token, text, program_name=DEFAULT_PROGRAM_NAME): + self.insertAfter(token.tokenIndex, text, program_name) + + def insertAfter(self, index, text, program_name=DEFAULT_PROGRAM_NAME): + op = self.InsertAfterOp(self.tokens, index + 1, text) + rewrites = self.getProgram(program_name) + op.instructionIndex = len(rewrites) + rewrites.append(op) + + def insertBeforeIndex(self, index, text): + self.insertBefore(self.DEFAULT_PROGRAM_NAME, index, text) + + def insertBeforeToken( + self, token, text, program_name=DEFAULT_PROGRAM_NAME + ): + self.insertBefore(program_name, token.tokenIndex, text) + + def insertBefore(self, program_name, index, text): + op = self.InsertBeforeOp(self.tokens, index, text) + rewrites = self.getProgram(program_name) + op.instructionIndex = len(rewrites) + rewrites.append(op) + + def replaceIndex(self, index, text): + self.replace(self.DEFAULT_PROGRAM_NAME, index, index, text) + + def replaceRange(self, from_idx, to_idx, text): + self.replace(self.DEFAULT_PROGRAM_NAME, from_idx, to_idx, text) + + def replaceSingleToken(self, token, text): + self.replace( + self.DEFAULT_PROGRAM_NAME, token.tokenIndex, token.tokenIndex, text + ) + + def replaceRangeTokens( + self, from_token, to_token, text, program_name=DEFAULT_PROGRAM_NAME + ): + self.replace( + program_name, from_token.tokenIndex, to_token.tokenIndex, text + ) + + def replace(self, program_name, from_idx, to_idx, text): + if any( + ( + from_idx > to_idx, + from_idx < 0, + to_idx < 0, + to_idx >= len(self.tokens.tokens), + ) + ): + raise ValueError( + f"replace: range invalid: {from_idx}..{to_idx}(size={len(self.tokens.tokens)})" + ) + op = self.ReplaceOp(from_idx, to_idx, self.tokens, text) + rewrites = self.getProgram(program_name) + op.instructionIndex = len(rewrites) + rewrites.append(op) + + def deleteToken(self, token): + self.delete(self.DEFAULT_PROGRAM_NAME, token, token) + + def deleteIndex(self, index): + self.delete(self.DEFAULT_PROGRAM_NAME, index, index) + + def delete(self, program_name, from_idx, to_idx): + if isinstance(from_idx, Token): + self.replace( + program_name, from_idx.tokenIndex, to_idx.tokenIndex, "" + ) + else: + self.replace(program_name, from_idx, to_idx, "") + + def lastRewriteTokenIndex(self, program_name=DEFAULT_PROGRAM_NAME): + return self.lastRewriteTokenIndexes.get(program_name, -1) + + def setLastRewriteTokenIndex(self, program_name, i): + self.lastRewriteTokenIndexes[program_name] = i + + def getProgram(self, program_name): + return self.programs.setdefault(program_name, []) + + def getDefaultText(self): + return self.getText( + self.DEFAULT_PROGRAM_NAME, 0, len(self.tokens.tokens) - 1 + ) + + def getText(self, program_name, start: int, stop: int): + """ + :return: the text in tokens[start, stop](closed interval) + """ + rewrites = self.programs.get(program_name) + + # ensure start/end are in range + if stop > len(self.tokens.tokens) - 1: + stop = len(self.tokens.tokens) - 1 + if start < 0: + start = 0 + + # if no instructions to execute + if not rewrites: + return self.tokens.getText(start, stop) + buf = StringIO() + indexToOp = self._reduceToSingleOperationPerIndex(rewrites) + i = start + while all((i <= stop, i < len(self.tokens.tokens))): + op = indexToOp.pop(i, None) + token = self.tokens.get(i) + if op is None: + if token.type != Token.EOF: + buf.write(token.text) + i += 1 + else: + i = op.execute(buf) + + if stop == len(self.tokens.tokens) - 1: + for op in indexToOp.values(): + if op.index >= len(self.tokens.tokens) - 1: + buf.write(op.text) + + return buf.getvalue() + + def _reduceToSingleOperationPerIndex(self, rewrites): + # Walk replaces + for i, rop in enumerate(rewrites): + if any( + ( + rop is None, + not isinstance(rop, TokenStreamRewriter.ReplaceOp), + ) + ): + continue + # Wipe prior inserts within range + inserts = [ + op + for op in rewrites[:i] + if isinstance(op, TokenStreamRewriter.InsertBeforeOp) + ] + for iop in inserts: + if iop.index == rop.index: + rewrites[iop.instructionIndex] = None + rop.text = f"{iop.text}{rop.text}" + elif all((iop.index > rop.index, iop.index <= rop.last_index)): + rewrites[iop.instructionIndex] = None + + # Drop any prior replaces contained within + prevReplaces = [ + op + for op in rewrites[:i] + if isinstance(op, TokenStreamRewriter.ReplaceOp) + ] + for prevRop in prevReplaces: + if all( + ( + prevRop.index >= rop.index, + prevRop.last_index <= rop.last_index, + ) + ): + rewrites[prevRop.instructionIndex] = None + continue + isDisjoint = any( + ( + prevRop.last_index < rop.index, + prevRop.index > rop.last_index, + ) + ) + if all( + (prevRop.text is None, rop.text is None, not isDisjoint) + ): + rewrites[prevRop.instructionIndex] = None + rop.index = min(prevRop.index, rop.index) + rop.last_index = min(prevRop.last_index, rop.last_index) + print(f"New rop {rop}") + elif not (isDisjoint): + raise ValueError( + f"replace op boundaries of {rop} overlap with previous {prevRop}" + ) + + # Walk inserts + for i, iop in enumerate(rewrites): + if any( + ( + iop is None, + not isinstance(iop, TokenStreamRewriter.InsertBeforeOp), + ) + ): + continue + prevInserts = [ + op + for op in rewrites[:i] + if isinstance(op, TokenStreamRewriter.InsertBeforeOp) + ] + for prev_index, prevIop in enumerate(prevInserts): + if ( + prevIop.index == iop.index + and type(prevIop) is TokenStreamRewriter.InsertBeforeOp + ): + iop.text += prevIop.text + rewrites[prev_index] = None + elif ( + prevIop.index == iop.index + and type(prevIop) is TokenStreamRewriter.InsertAfterOp + ): + iop.text = prevIop.text + iop.text + rewrites[prev_index] = None + # look for replaces where iop.index is in range; error + prevReplaces = [ + op + for op in rewrites[:i] + if isinstance(op, TokenStreamRewriter.ReplaceOp) + ] + for rop in prevReplaces: + if iop.index == rop.index: + rop.text = iop.text + rop.text + rewrites[i] = None + continue + if all((iop.index >= rop.index, iop.index <= rop.last_index)): + raise ValueError( + f"insert op {iop} within boundaries of previous {rop}" + ) + + reduced = {} + for i, op in enumerate(rewrites): + if op is None: + continue + if reduced.get(op.index): + raise ValueError("should be only one op per index") + reduced[op.index] = op + + return reduced + + class RewriteOperation: + __slots__ = ("tokens", "index", "text", "instructionIndex") + + def __init__(self, tokens, index, text=""): + """ + :type tokens: CommonTokenStream + :param tokens: + :param index: + :param text: + :return: + """ + self.tokens = tokens + self.index = index + self.text = text + self.instructionIndex = 0 + + def execute(self, buf): + """ + :type buf: StringIO.StringIO + :param buf: + :return: + """ + return self.index + + def __str__(self): + return f'<{self.__class__.__name__}@{self.tokens.get(self.index)}:"{self.text}">' + + class InsertBeforeOp(RewriteOperation): + def __init__(self, tokens, index, text=""): + super(TokenStreamRewriter.InsertBeforeOp, self).__init__( + tokens, index, text + ) + + def execute(self, buf): + buf.write(self.text) + if self.tokens.get(self.index).type != Token.EOF: + buf.write(self.tokens.get(self.index).text) + return self.index + 1 + + class InsertAfterOp(InsertBeforeOp): + pass + + class ReplaceOp(RewriteOperation): + __slots__ = "last_index" + + def __init__(self, from_idx, to_idx, tokens, text): + super(TokenStreamRewriter.ReplaceOp, self).__init__( + tokens, from_idx, text + ) + self.last_index = to_idx + + def execute(self, buf): + if self.text: + buf.write(self.text) + return self.last_index + 1 + + def __str__(self): + if self.text: + return f'' diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/Utils.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/Utils.py new file mode 100644 index 00000000..f2ed0ecd --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/Utils.py @@ -0,0 +1,35 @@ +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +from io import StringIO + + +def str_list(val): + with StringIO() as buf: + buf.write("[") + first = True + for item in val: + if not first: + buf.write(", ") + buf.write(str(item)) + first = False + buf.write("]") + return buf.getvalue() + + +def escapeWhitespace(s: str, escapeSpaces: bool): + with StringIO() as buf: + for c in s: + if c == " " and escapeSpaces: + buf.write("\u00b7") + elif c == "\t": + buf.write("\\t") + elif c == "\n": + buf.write("\\n") + elif c == "\r": + buf.write("\\r") + else: + buf.write(c) + return buf.getvalue() diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/__init__.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/__init__.py new file mode 100644 index 00000000..8401d64d --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/__init__.py @@ -0,0 +1,59 @@ +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNDeserializer import ( + ATNDeserializer, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.LexerATNSimulator import ( + LexerATNSimulator, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ParserATNSimulator import ( + ParserATNSimulator, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.PredictionMode import ( + PredictionMode, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.BufferedTokenStream import ( + TokenStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.CommonTokenStream import ( + CommonTokenStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFA import DFA +from cf_units._udunits2_parser.parser._antlr4_runtime.error.DiagnosticErrorListener import ( + DiagnosticErrorListener, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + IllegalStateException, + NoViableAltException, + RecognitionException, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error.ErrorStrategy import ( + BailErrorStrategy, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.FileStream import ( + FileStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( + InputStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import Lexer +from cf_units._udunits2_parser.parser._antlr4_runtime.Parser import Parser +from cf_units._udunits2_parser.parser._antlr4_runtime.ParserRuleContext import ( + ParserRuleContext, + RuleContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( + PredictionContextCache, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.StdinStream import ( + StdinStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( + ErrorNode, + ParseTreeListener, + ParseTreeVisitor, + ParseTreeWalker, + RuleNode, + TerminalNode, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Utils import str_list diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/_antlr4_version.txt b/cf_units/_udunits2_parser/parser/_antlr4_runtime/_antlr4_version.txt new file mode 100644 index 00000000..012d0f05 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/_antlr4_version.txt @@ -0,0 +1 @@ +4.11.1 \ No newline at end of file diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/_pygrun.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/_pygrun.py new file mode 100644 index 00000000..4ad33430 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/_pygrun.py @@ -0,0 +1,186 @@ +#!python +__author__ = "jszheng" +import optparse +import os +import sys + +from cf_units._udunits2_parser.parser._antlr4_runtime import * + + +# this is a python version of TestRig +def beautify_lisp_string(in_string): + indent_size = 3 + add_indent = " " * indent_size + out_string = in_string[0] # no indent for 1st ( + indent = "" + for i in range(1, len(in_string)): + if in_string[i] == "(" and in_string[i + 1] != " ": + indent += add_indent + out_string += "\n" + indent + "(" + elif in_string[i] == ")": + out_string += ")" + if len(indent) > 0: + indent = indent.replace(add_indent, "", 1) + else: + out_string += in_string[i] + return out_string + + +def main(): + ############################################################# + # parse options + # not support -gui -encoding -ps + ############################################################# + usage = "Usage: %prog [options] Grammar_Name Start_Rule" + parser = optparse.OptionParser(usage=usage) + # parser.add_option('-t', '--tree', + # dest="out_file", + # default="default.out", + # help='set output file name', + # ) + parser.add_option( + "-t", + "--tree", + default=False, + action="store_true", + help="Print AST tree", + ) + parser.add_option( + "-k", + "--tokens", + dest="token", + default=False, + action="store_true", + help="Show Tokens", + ) + parser.add_option( + "-s", + "--sll", + dest="sll", + default=False, + action="store_true", + help="Show SLL", + ) + parser.add_option( + "-d", + "--diagnostics", + dest="diagnostics", + default=False, + action="store_true", + help="Enable diagnostics error listener", + ) + parser.add_option( + "-a", + "--trace", + dest="trace", + default=False, + action="store_true", + help="Enable Trace", + ) + + options, remainder = parser.parse_args() + if len(remainder) < 2: + print("ERROR: You have to provide at least 2 arguments!") + parser.print_help() + exit(1) + else: + grammar = remainder.pop(0) + start_rule = remainder.pop(0) + file_list = remainder + + ############################################################# + # check and load antlr generated files + ############################################################# + # dynamic load the module and class + lexerName = grammar + "Lexer" + parserName = grammar + "Parser" + # check if the generate file exist + lexer_file = lexerName + ".py" + parser_file = parserName + ".py" + if not os.path.exists(lexer_file): + print(f"[ERROR] Can't find lexer file {lexer_file}!") + print(os.path.realpath(".")) + exit(1) + if not os.path.exists(parser_file): + print(f"[ERROR] Can't find parser file {lexer_file}!") + print(os.path.realpath(".")) + exit(1) + + # current directory is where the generated file loaded + # the script might be in different place. + sys.path.append(".") + # print(sys.path) + + # add current directory to python global namespace in case of relative imports + globals().update({"__package__": os.path.basename(os.getcwd())}) + + # print("Load Lexer {}".format(lexerName)) + module_lexer = __import__(lexerName, globals(), locals(), lexerName) + class_lexer = getattr(module_lexer, lexerName) + # print(class_lexer) + + # print("Load Parser {}".format(parserName)) + module_parser = __import__(parserName, globals(), locals(), parserName) + class_parser = getattr(module_parser, parserName) + # print(class_parser) + + ############################################################# + # main process steps. + ############################################################# + def process(input_stream, class_lexer, class_parser): + lexer = class_lexer(input_stream) + token_stream = CommonTokenStream(lexer) + token_stream.fill() + if options.token: # need to show token + for tok in token_stream.tokens: + print(tok) + if start_rule == "tokens": + return + + parser = class_parser(token_stream) + + if options.diagnostics: + parser.addErrorListener(DiagnosticErrorListener()) + parser._interp.predictionMode = ( + PredictionMode.LL_EXACT_AMBIG_DETECTION + ) + if options.tree: + parser.buildParseTrees = True + if options.sll: + parser._interp.predictionMode = PredictionMode.SLL + # parser.setTokenStream(token_stream) + parser.setTrace(options.trace) + if hasattr(parser, start_rule): + func_start_rule = getattr(parser, start_rule) + parser_ret = func_start_rule() + if options.tree: + lisp_tree_str = parser_ret.toStringTree(recog=parser) + print(beautify_lisp_string(lisp_tree_str)) + else: + print( + f"[ERROR] Can't find start rule '{start_rule}' in parser '{parserName}'" + ) + + ############################################################# + # use stdin if not provide file as input stream + ############################################################# + if len(file_list) == 0: + input_stream = InputStream(sys.stdin.read()) + process(input_stream, class_lexer, class_parser) + exit(0) + + ############################################################# + # iterate all input file + ############################################################# + for file_name in file_list: + if os.path.exists(file_name) and os.path.isfile(file_name): + input_stream = FileStream(file_name) + process(input_stream, class_lexer, class_parser) + else: + print( + f"[ERROR] file {os.path.normpath(file_name)} not exist" + ) + + +if __name__ == "__main__": + main() diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATN.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATN.py new file mode 100644 index 00000000..780d5ddb --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATN.py @@ -0,0 +1,155 @@ +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# / +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( + ATNState, + DecisionState, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNType import ( + ATNType, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.IntervalSet import ( + IntervalSet, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( + RuleContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + + +class ATN: + __slots__ = ( + "grammarType", + "maxTokenType", + "states", + "decisionToState", + "ruleToStartState", + "ruleToStopState", + "modeNameToStartState", + "ruleToTokenType", + "lexerActions", + "modeToStartState", + ) + + INVALID_ALT_NUMBER = 0 + + # Used for runtime deserialization of ATNs from strings#/ + def __init__(self, grammarType: ATNType, maxTokenType: int): + # The type of the ATN. + self.grammarType = grammarType + # The maximum value for any symbol recognized by a transition in the ATN. + self.maxTokenType = maxTokenType + self.states = [] + # Each subrule/rule is a decision point and we must track them so we + # can go back later and build DFA predictors for them. This includes + # all the rules, subrules, optional blocks, ()+, ()* etc... + self.decisionToState = [] + # Maps from rule index to starting state number. + self.ruleToStartState = [] + # Maps from rule index to stop state number. + self.ruleToStopState = None + self.modeNameToStartState = dict() + # For lexer ATNs, this maps the rule index to the resulting token type. + # For parser ATNs, this maps the rule index to the generated bypass token + # type if the + # {@link ATNDeserializationOptions#isGenerateRuleBypassTransitions} + # deserialization option was specified; otherwise, this is {@code null}. + self.ruleToTokenType = None + # For lexer ATNs, this is an array of {@link LexerAction} objects which may + # be referenced by action transitions in the ATN. + self.lexerActions = None + self.modeToStartState = [] + + # Compute the set of valid tokens that can occur starting in state {@code s}. + # If {@code ctx} is null, the set of tokens will not include what can follow + # the rule surrounding {@code s}. In other words, the set will be + # restricted to tokens reachable staying within {@code s}'s rule. + def nextTokensInContext(self, s: ATNState, ctx: RuleContext): + from cf_units._udunits2_parser.parser._antlr4_runtime.LL1Analyzer import ( + LL1Analyzer, + ) + + anal = LL1Analyzer(self) + return anal.LOOK(s, ctx=ctx) + + # Compute the set of valid tokens that can occur starting in {@code s} and + # staying in same rule. {@link Token#EPSILON} is in set if we reach end of + # rule. + def nextTokensNoContext(self, s: ATNState): + if s.nextTokenWithinRule is not None: + return s.nextTokenWithinRule + s.nextTokenWithinRule = self.nextTokensInContext(s, None) + s.nextTokenWithinRule.readonly = True + return s.nextTokenWithinRule + + def nextTokens(self, s: ATNState, ctx: RuleContext = None): + if ctx == None: + return self.nextTokensNoContext(s) + else: + return self.nextTokensInContext(s, ctx) + + def addState(self, state: ATNState): + if state is not None: + state.atn = self + state.stateNumber = len(self.states) + self.states.append(state) + + def removeState(self, state: ATNState): + self.states[state.stateNumber] = ( + None # just free mem, don't shift states in list + ) + + def defineDecisionState(self, s: DecisionState): + self.decisionToState.append(s) + s.decision = len(self.decisionToState) - 1 + return s.decision + + def getDecisionState(self, decision: int): + if len(self.decisionToState) == 0: + return None + else: + return self.decisionToState[decision] + + # Computes the set of input symbols which could follow ATN state number + # {@code stateNumber} in the specified full {@code context}. This method + # considers the complete parser context, but does not evaluate semantic + # predicates (i.e. all predicates encountered during the calculation are + # assumed true). If a path in the ATN exists from the starting state to the + # {@link RuleStopState} of the outermost context without matching any + # symbols, {@link Token#EOF} is added to the returned set. + # + #

      If {@code context} is {@code null}, it is treated as + # {@link ParserRuleContext#EMPTY}.

      + # + # @param stateNumber the ATN state number + # @param context the full parse context + # @return The set of potentially valid input symbols which could follow the + # specified state in the specified context. + # @throws IllegalArgumentException if the ATN does not contain a state with + # number {@code stateNumber} + # / + def getExpectedTokens(self, stateNumber: int, ctx: RuleContext): + if stateNumber < 0 or stateNumber >= len(self.states): + raise Exception("Invalid state number.") + s = self.states[stateNumber] + following = self.nextTokens(s) + if Token.EPSILON not in following: + return following + expected = IntervalSet() + expected.addSet(following) + expected.removeOne(Token.EPSILON) + while ( + ctx != None + and ctx.invokingState >= 0 + and Token.EPSILON in following + ): + invokingState = self.states[ctx.invokingState] + rt = invokingState.transitions[0] + following = self.nextTokens(rt.followState) + expected.addSet(following) + expected.removeOne(Token.EPSILON) + ctx = ctx.parentCtx + if Token.EPSILON in following: + expected.addOne(Token.EOF) + return expected diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNConfig.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNConfig.py new file mode 100644 index 00000000..6b004c47 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNConfig.py @@ -0,0 +1,231 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# / + +# A tuple: (ATN state, predicted alt, syntactic, semantic context). +# The syntactic context is a graph-structured stack node whose +# path(s) to the root is the rule invocation(s) +# chain used to arrive at the state. The semantic context is +# the tree of semantic predicates encountered before reaching +# an ATN state. +# / +from io import StringIO + +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( + ATNState, + DecisionState, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.LexerActionExecutor import ( + LexerActionExecutor, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.SemanticContext import ( + SemanticContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( + PredictionContext, +) + +# need a forward declaration +ATNConfig = None + + +class ATNConfig: + __slots__ = ( + "state", + "alt", + "context", + "semanticContext", + "reachesIntoOuterContext", + "precedenceFilterSuppressed", + ) + + def __init__( + self, + state: ATNState = None, + alt: int = None, + context: PredictionContext = None, + semantic: SemanticContext = None, + config: ATNConfig = None, + ): + if config is not None: + if state is None: + state = config.state + if alt is None: + alt = config.alt + if context is None: + context = config.context + if semantic is None: + semantic = config.semanticContext + if semantic is None: + semantic = SemanticContext.NONE + # The ATN state associated with this configuration#/ + self.state = state + # What alt (or lexer rule) is predicted by this configuration#/ + self.alt = alt + # The stack of invoking states leading to the rule/states associated + # with this config. We track only those contexts pushed during + # execution of the ATN simulator. + self.context = context + self.semanticContext = semantic + # We cannot execute predicates dependent upon local context unless + # we know for sure we are in the correct context. Because there is + # no way to do this efficiently, we simply cannot evaluate + # dependent predicates unless we are in the rule that initially + # invokes the ATN simulator. + # + # closure() tracks the depth of how far we dip into the + # outer context: depth > 0. Note that it may not be totally + # accurate depth since I don't ever decrement. TODO: make it a boolean then + self.reachesIntoOuterContext = ( + 0 if config is None else config.reachesIntoOuterContext + ) + self.precedenceFilterSuppressed = ( + False if config is None else config.precedenceFilterSuppressed + ) + + # An ATN configuration is equal to another if both have + # the same state, they predict the same alternative, and + # syntactic/semantic contexts are the same. + # / + def __eq__(self, other): + if self is other: + return True + elif not isinstance(other, ATNConfig): + return False + else: + return ( + self.state.stateNumber == other.state.stateNumber + and self.alt == other.alt + and ( + (self.context is other.context) + or (self.context == other.context) + ) + and self.semanticContext == other.semanticContext + and self.precedenceFilterSuppressed + == other.precedenceFilterSuppressed + ) + + def __hash__(self): + return hash( + ( + self.state.stateNumber, + self.alt, + self.context, + self.semanticContext, + ) + ) + + def hashCodeForConfigSet(self): + return hash( + (self.state.stateNumber, self.alt, hash(self.semanticContext)) + ) + + def equalsForConfigSet(self, other): + if self is other: + return True + elif not isinstance(other, ATNConfig): + return False + else: + return ( + self.state.stateNumber == other.state.stateNumber + and self.alt == other.alt + and self.semanticContext == other.semanticContext + ) + + def __str__(self): + with StringIO() as buf: + buf.write("(") + buf.write(str(self.state)) + buf.write(",") + buf.write(str(self.alt)) + if self.context is not None: + buf.write(",[") + buf.write(str(self.context)) + buf.write("]") + if ( + self.semanticContext is not None + and self.semanticContext is not SemanticContext.NONE + ): + buf.write(",") + buf.write(str(self.semanticContext)) + if self.reachesIntoOuterContext > 0: + buf.write(",up=") + buf.write(str(self.reachesIntoOuterContext)) + buf.write(")") + return buf.getvalue() + + +# need a forward declaration +LexerATNConfig = None + + +class LexerATNConfig(ATNConfig): + __slots__ = ("lexerActionExecutor", "passedThroughNonGreedyDecision") + + def __init__( + self, + state: ATNState, + alt: int = None, + context: PredictionContext = None, + semantic: SemanticContext = SemanticContext.NONE, + lexerActionExecutor: LexerActionExecutor = None, + config: LexerATNConfig = None, + ): + super().__init__( + state=state, + alt=alt, + context=context, + semantic=semantic, + config=config, + ) + if config is not None: + if lexerActionExecutor is None: + lexerActionExecutor = config.lexerActionExecutor + # This is the backing field for {@link #getLexerActionExecutor}. + self.lexerActionExecutor = lexerActionExecutor + self.passedThroughNonGreedyDecision = ( + False + if config is None + else self.checkNonGreedyDecision(config, state) + ) + + def __hash__(self): + return hash( + ( + self.state.stateNumber, + self.alt, + self.context, + self.semanticContext, + self.passedThroughNonGreedyDecision, + self.lexerActionExecutor, + ) + ) + + def __eq__(self, other): + if self is other: + return True + elif not isinstance(other, LexerATNConfig): + return False + if ( + self.passedThroughNonGreedyDecision + != other.passedThroughNonGreedyDecision + ): + return False + if not (self.lexerActionExecutor == other.lexerActionExecutor): + return False + return super().__eq__(other) + + def hashCodeForConfigSet(self): + return hash(self) + + def equalsForConfigSet(self, other): + return self == other + + def checkNonGreedyDecision(self, source: LexerATNConfig, target: ATNState): + return ( + source.passedThroughNonGreedyDecision + or isinstance(target, DecisionState) + and target.nonGreedy + ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNConfigSet.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNConfigSet.py new file mode 100644 index 00000000..c22f94de --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNConfigSet.py @@ -0,0 +1,245 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. + +from functools import reduce + +# +# Specialized {@link Set}{@code <}{@link ATNConfig}{@code >} that can track +# info about the set, with support for combining similar configurations using a +# graph-structured stack. +# / +from io import StringIO + +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfig import ( + ATNConfig, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.SemanticContext import ( + SemanticContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + IllegalStateException, + UnsupportedOperationException, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( + merge, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Utils import str_list + +ATNSimulator = None + + +class ATNConfigSet: + __slots__ = ( + "configLookup", + "fullCtx", + "readonly", + "configs", + "uniqueAlt", + "conflictingAlts", + "hasSemanticContext", + "dipsIntoOuterContext", + "cachedHashCode", + ) + + # + # The reason that we need this is because we don't want the hash map to use + # the standard hash code and equals. We need all configurations with the same + # {@code (s,i,_,semctx)} to be equal. Unfortunately, this key effectively doubles + # the number of objects associated with ATNConfigs. The other solution is to + # use a hash table that lets us specify the equals/hashcode operation. + + def __init__(self, fullCtx: bool = True): + # All configs but hashed by (s, i, _, pi) not including context. Wiped out + # when we go readonly as this set becomes a DFA state. + self.configLookup = dict() + # Indicates that this configuration set is part of a full context + # LL prediction. It will be used to determine how to merge $. With SLL + # it's a wildcard whereas it is not for LL context merge. + self.fullCtx = fullCtx + # Indicates that the set of configurations is read-only. Do not + # allow any code to manipulate the set; DFA states will point at + # the sets and they must not change. This does not protect the other + # fields; in particular, conflictingAlts is set after + # we've made this readonly. + self.readonly = False + # Track the elements as they are added to the set; supports get(i)#/ + self.configs = [] + + # TODO: these fields make me pretty uncomfortable but nice to pack up info together, saves recomputation + # TODO: can we track conflicts as they are added to save scanning configs later? + self.uniqueAlt = 0 + self.conflictingAlts = None + + # Used in parser and lexer. In lexer, it indicates we hit a pred + # while computing a closure operation. Don't make a DFA state from this. + self.hasSemanticContext = False + self.dipsIntoOuterContext = False + + self.cachedHashCode = -1 + + def __iter__(self): + return self.configs.__iter__() + + # Adding a new config means merging contexts with existing configs for + # {@code (s, i, pi, _)}, where {@code s} is the + # {@link ATNConfig#state}, {@code i} is the {@link ATNConfig#alt}, and + # {@code pi} is the {@link ATNConfig#semanticContext}. We use + # {@code (s,i,pi)} as key. + # + #

      This method updates {@link #dipsIntoOuterContext} and + # {@link #hasSemanticContext} when necessary.

      + # / + def add(self, config: ATNConfig, mergeCache=None): + if self.readonly: + raise Exception("This set is readonly") + if config.semanticContext is not SemanticContext.NONE: + self.hasSemanticContext = True + if config.reachesIntoOuterContext > 0: + self.dipsIntoOuterContext = True + existing = self.getOrAdd(config) + if existing is config: + self.cachedHashCode = -1 + self.configs.append(config) # track order here + return True + # a previous (s,i,pi,_), merge with it and save result + rootIsWildcard = not self.fullCtx + merged = merge( + existing.context, config.context, rootIsWildcard, mergeCache + ) + # no need to check for existing.context, config.context in cache + # since only way to create new graphs is "call rule" and here. + # We cache at both places. + existing.reachesIntoOuterContext = max( + existing.reachesIntoOuterContext, config.reachesIntoOuterContext + ) + # make sure to preserve the precedence filter suppression during the merge + if config.precedenceFilterSuppressed: + existing.precedenceFilterSuppressed = True + existing.context = merged # replace context; no need to alt mapping + return True + + def getOrAdd(self, config: ATNConfig): + h = config.hashCodeForConfigSet() + l = self.configLookup.get(h, None) + if l is not None: + r = next( + (cfg for cfg in l if config.equalsForConfigSet(cfg)), None + ) + if r is not None: + return r + if l is None: + l = [config] + self.configLookup[h] = l + else: + l.append(config) + return config + + def getStates(self): + return set(c.state for c in self.configs) + + def getPredicates(self): + return list( + cfg.semanticContext + for cfg in self.configs + if cfg.semanticContext != SemanticContext.NONE + ) + + def get(self, i: int): + return self.configs[i] + + def optimizeConfigs(self, interpreter: ATNSimulator): + if self.readonly: + raise IllegalStateException("This set is readonly") + if len(self.configs) == 0: + return + for config in self.configs: + config.context = interpreter.getCachedContext(config.context) + + def addAll(self, coll: list): + for c in coll: + self.add(c) + return False + + def __eq__(self, other): + if self is other: + return True + elif not isinstance(other, ATNConfigSet): + return False + + same = ( + self.configs is not None + and self.configs == other.configs + and self.fullCtx == other.fullCtx + and self.uniqueAlt == other.uniqueAlt + and self.conflictingAlts == other.conflictingAlts + and self.hasSemanticContext == other.hasSemanticContext + and self.dipsIntoOuterContext == other.dipsIntoOuterContext + ) + + return same + + def __hash__(self): + if self.readonly: + if self.cachedHashCode == -1: + self.cachedHashCode = self.hashConfigs() + return self.cachedHashCode + return self.hashConfigs() + + def hashConfigs(self): + return reduce(lambda h, cfg: hash((h, cfg)), self.configs, 0) + + def __len__(self): + return len(self.configs) + + def isEmpty(self): + return len(self.configs) == 0 + + def __contains__(self, config): + if self.configLookup is None: + raise UnsupportedOperationException( + "This method is not implemented for readonly sets." + ) + h = config.hashCodeForConfigSet() + l = self.configLookup.get(h, None) + if l is not None: + for c in l: + if config.equalsForConfigSet(c): + return True + return False + + def clear(self): + if self.readonly: + raise IllegalStateException("This set is readonly") + self.configs.clear() + self.cachedHashCode = -1 + self.configLookup.clear() + + def setReadonly(self, readonly: bool): + self.readonly = readonly + self.configLookup = None # can't mod, no need for lookup cache + + def __str__(self): + with StringIO() as buf: + buf.write(str_list(self.configs)) + if self.hasSemanticContext: + buf.write(",hasSemanticContext=") + buf.write( + str(self.hasSemanticContext).lower() + ) # lower() to conform to java output + if self.uniqueAlt != ATN.INVALID_ALT_NUMBER: + buf.write(",uniqueAlt=") + buf.write(str(self.uniqueAlt)) + if self.conflictingAlts is not None: + buf.write(",conflictingAlts=") + buf.write(str(self.conflictingAlts)) + if self.dipsIntoOuterContext: + buf.write(",dipsIntoOuterContext") + return buf.getvalue() + + +class OrderedATNConfigSet(ATNConfigSet): + def __init__(self): + super().__init__() diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNDeserializationOptions.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNDeserializationOptions.py new file mode 100644 index 00000000..83490ab5 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNDeserializationOptions.py @@ -0,0 +1,30 @@ +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. + +# need a forward declaration +ATNDeserializationOptions = None + + +class ATNDeserializationOptions: + __slots__ = ("readonly", "verifyATN", "generateRuleBypassTransitions") + + defaultOptions = None + + def __init__(self, copyFrom: ATNDeserializationOptions = None): + self.readonly = False + self.verifyATN = True if copyFrom is None else copyFrom.verifyATN + self.generateRuleBypassTransitions = ( + False + if copyFrom is None + else copyFrom.generateRuleBypassTransitions + ) + + def __setattr__(self, key, value): + if key != "readonly" and self.readonly: + raise Exception("The object is read only.") + super(type(self), self).__setattr__(key, value) + + +ATNDeserializationOptions.defaultOptions = ATNDeserializationOptions() +ATNDeserializationOptions.defaultOptions.readonly = True diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNDeserializer.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNDeserializer.py new file mode 100644 index 00000000..101bcbf4 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNDeserializer.py @@ -0,0 +1,546 @@ +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# / +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNDeserializationOptions import ( + ATNDeserializationOptions, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import * +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNType import ( + ATNType, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.LexerAction import * +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.Transition import * +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + +SERIALIZED_VERSION = 4 + + +class ATNDeserializer: + __slots__ = ("deserializationOptions", "data", "pos") + + def __init__(self, options: ATNDeserializationOptions = None): + if options is None: + options = ATNDeserializationOptions.defaultOptions + self.deserializationOptions = options + + def deserialize(self, data: [int]): + self.data = data + self.pos = 0 + self.checkVersion() + atn = self.readATN() + self.readStates(atn) + self.readRules(atn) + self.readModes(atn) + sets = [] + self.readSets(atn, sets) + self.readEdges(atn, sets) + self.readDecisions(atn) + self.readLexerActions(atn) + self.markPrecedenceDecisions(atn) + self.verifyATN(atn) + if ( + self.deserializationOptions.generateRuleBypassTransitions + and atn.grammarType == ATNType.PARSER + ): + self.generateRuleBypassTransitions(atn) + # re-verify after modification + self.verifyATN(atn) + return atn + + def checkVersion(self): + version = self.readInt() + if version != SERIALIZED_VERSION: + raise Exception( + f"Could not deserialize ATN with version {ord(version)} (expected {SERIALIZED_VERSION})." + ) + + def readATN(self): + idx = self.readInt() + grammarType = ATNType.fromOrdinal(idx) + maxTokenType = self.readInt() + return ATN(grammarType, maxTokenType) + + def readStates(self, atn: ATN): + loopBackStateNumbers = [] + endStateNumbers = [] + nstates = self.readInt() + for i in range(0, nstates): + stype = self.readInt() + # ignore bad type of states + if stype == ATNState.INVALID_TYPE: + atn.addState(None) + continue + ruleIndex = self.readInt() + s = self.stateFactory(stype, ruleIndex) + if stype == ATNState.LOOP_END: # special case + loopBackStateNumber = self.readInt() + loopBackStateNumbers.append((s, loopBackStateNumber)) + elif isinstance(s, BlockStartState): + endStateNumber = self.readInt() + endStateNumbers.append((s, endStateNumber)) + + atn.addState(s) + + # delay the assignment of loop back and end states until we know all the state instances have been initialized + for pair in loopBackStateNumbers: + pair[0].loopBackState = atn.states[pair[1]] + + for pair in endStateNumbers: + pair[0].endState = atn.states[pair[1]] + + numNonGreedyStates = self.readInt() + for i in range(0, numNonGreedyStates): + stateNumber = self.readInt() + atn.states[stateNumber].nonGreedy = True + + numPrecedenceStates = self.readInt() + for i in range(0, numPrecedenceStates): + stateNumber = self.readInt() + atn.states[stateNumber].isPrecedenceRule = True + + def readRules(self, atn: ATN): + nrules = self.readInt() + if atn.grammarType == ATNType.LEXER: + atn.ruleToTokenType = [0] * nrules + + atn.ruleToStartState = [0] * nrules + for i in range(0, nrules): + s = self.readInt() + startState = atn.states[s] + atn.ruleToStartState[i] = startState + if atn.grammarType == ATNType.LEXER: + tokenType = self.readInt() + atn.ruleToTokenType[i] = tokenType + + atn.ruleToStopState = [0] * nrules + for state in atn.states: + if not isinstance(state, RuleStopState): + continue + atn.ruleToStopState[state.ruleIndex] = state + atn.ruleToStartState[state.ruleIndex].stopState = state + + def readModes(self, atn: ATN): + nmodes = self.readInt() + for i in range(0, nmodes): + s = self.readInt() + atn.modeToStartState.append(atn.states[s]) + + def readSets(self, atn: ATN, sets: list): + m = self.readInt() + for i in range(0, m): + iset = IntervalSet() + sets.append(iset) + n = self.readInt() + containsEof = self.readInt() + if containsEof != 0: + iset.addOne(-1) + for j in range(0, n): + i1 = self.readInt() + i2 = self.readInt() + iset.addRange( + range(i1, i2 + 1) + ) # range upper limit is exclusive + + def readEdges(self, atn: ATN, sets: list): + nedges = self.readInt() + for i in range(0, nedges): + src = self.readInt() + trg = self.readInt() + ttype = self.readInt() + arg1 = self.readInt() + arg2 = self.readInt() + arg3 = self.readInt() + trans = self.edgeFactory( + atn, ttype, src, trg, arg1, arg2, arg3, sets + ) + srcState = atn.states[src] + srcState.addTransition(trans) + + # edges for rule stop states can be derived, so they aren't serialized + for state in atn.states: + for i in range(0, len(state.transitions)): + t = state.transitions[i] + if not isinstance(t, RuleTransition): + continue + outermostPrecedenceReturn = -1 + if atn.ruleToStartState[t.target.ruleIndex].isPrecedenceRule: + if t.precedence == 0: + outermostPrecedenceReturn = t.target.ruleIndex + trans = EpsilonTransition( + t.followState, outermostPrecedenceReturn + ) + atn.ruleToStopState[t.target.ruleIndex].addTransition(trans) + + for state in atn.states: + if isinstance(state, BlockStartState): + # we need to know the end state to set its start state + if state.endState is None: + raise Exception("IllegalState") + # block end states can only be associated to a single block start state + if state.endState.startState is not None: + raise Exception("IllegalState") + state.endState.startState = state + + if isinstance(state, PlusLoopbackState): + for i in range(0, len(state.transitions)): + target = state.transitions[i].target + if isinstance(target, PlusBlockStartState): + target.loopBackState = state + elif isinstance(state, StarLoopbackState): + for i in range(0, len(state.transitions)): + target = state.transitions[i].target + if isinstance(target, StarLoopEntryState): + target.loopBackState = state + + def readDecisions(self, atn: ATN): + ndecisions = self.readInt() + for i in range(0, ndecisions): + s = self.readInt() + decState = atn.states[s] + atn.decisionToState.append(decState) + decState.decision = i + + def readLexerActions(self, atn: ATN): + if atn.grammarType == ATNType.LEXER: + count = self.readInt() + atn.lexerActions = [None] * count + for i in range(0, count): + actionType = self.readInt() + data1 = self.readInt() + data2 = self.readInt() + lexerAction = self.lexerActionFactory(actionType, data1, data2) + atn.lexerActions[i] = lexerAction + + def generateRuleBypassTransitions(self, atn: ATN): + count = len(atn.ruleToStartState) + atn.ruleToTokenType = [0] * count + for i in range(0, count): + atn.ruleToTokenType[i] = atn.maxTokenType + i + 1 + + for i in range(0, count): + self.generateRuleBypassTransition(atn, i) + + def generateRuleBypassTransition(self, atn: ATN, idx: int): + bypassStart = BasicBlockStartState() + bypassStart.ruleIndex = idx + atn.addState(bypassStart) + + bypassStop = BlockEndState() + bypassStop.ruleIndex = idx + atn.addState(bypassStop) + + bypassStart.endState = bypassStop + atn.defineDecisionState(bypassStart) + + bypassStop.startState = bypassStart + + excludeTransition = None + + if atn.ruleToStartState[idx].isPrecedenceRule: + # wrap from the beginning of the rule to the StarLoopEntryState + endState = None + for state in atn.states: + if self.stateIsEndStateFor(state, idx): + endState = state + excludeTransition = state.loopBackState.transitions[0] + break + + if excludeTransition is None: + raise Exception( + "Couldn't identify final state of the precedence rule prefix section." + ) + + else: + endState = atn.ruleToStopState[idx] + + # all non-excluded transitions that currently target end state need to target blockEnd instead + for state in atn.states: + for transition in state.transitions: + if transition == excludeTransition: + continue + if transition.target == endState: + transition.target = bypassStop + + # all transitions leaving the rule start state need to leave blockStart instead + ruleToStartState = atn.ruleToStartState[idx] + count = len(ruleToStartState.transitions) + while count > 0: + bypassStart.addTransition(ruleToStartState.transitions[count - 1]) + del ruleToStartState.transitions[-1] + + # link the new states + atn.ruleToStartState[idx].addTransition(EpsilonTransition(bypassStart)) + bypassStop.addTransition(EpsilonTransition(endState)) + + matchState = BasicState() + atn.addState(matchState) + matchState.addTransition( + AtomTransition(bypassStop, atn.ruleToTokenType[idx]) + ) + bypassStart.addTransition(EpsilonTransition(matchState)) + + def stateIsEndStateFor(self, state: ATNState, idx: int): + if state.ruleIndex != idx: + return None + if not isinstance(state, StarLoopEntryState): + return None + + maybeLoopEndState = state.transitions[ + len(state.transitions) - 1 + ].target + if not isinstance(maybeLoopEndState, LoopEndState): + return None + + if maybeLoopEndState.epsilonOnlyTransitions and isinstance( + maybeLoopEndState.transitions[0].target, RuleStopState + ): + return state + else: + return None + + # + # Analyze the {@link StarLoopEntryState} states in the specified ATN to set + # the {@link StarLoopEntryState#isPrecedenceDecision} field to the + # correct value. + # + # @param atn The ATN. + # + def markPrecedenceDecisions(self, atn: ATN): + for state in atn.states: + if not isinstance(state, StarLoopEntryState): + continue + + # We analyze the ATN to determine if this ATN decision state is the + # decision for the closure block that determines whether a + # precedence rule should continue or complete. + # + if atn.ruleToStartState[state.ruleIndex].isPrecedenceRule: + maybeLoopEndState = state.transitions[ + len(state.transitions) - 1 + ].target + if isinstance(maybeLoopEndState, LoopEndState): + if maybeLoopEndState.epsilonOnlyTransitions and isinstance( + maybeLoopEndState.transitions[0].target, RuleStopState + ): + state.isPrecedenceDecision = True + + def verifyATN(self, atn: ATN): + if not self.deserializationOptions.verifyATN: + return + # verify assumptions + for state in atn.states: + if state is None: + continue + + self.checkCondition( + state.epsilonOnlyTransitions or len(state.transitions) <= 1 + ) + + if isinstance(state, PlusBlockStartState): + self.checkCondition(state.loopBackState is not None) + + if isinstance(state, StarLoopEntryState): + self.checkCondition(state.loopBackState is not None) + self.checkCondition(len(state.transitions) == 2) + + if isinstance( + state.transitions[0].target, StarBlockStartState + ): + self.checkCondition( + isinstance(state.transitions[1].target, LoopEndState) + ) + self.checkCondition(not state.nonGreedy) + elif isinstance(state.transitions[0].target, LoopEndState): + self.checkCondition( + isinstance( + state.transitions[1].target, StarBlockStartState + ) + ) + self.checkCondition(state.nonGreedy) + else: + raise Exception("IllegalState") + + if isinstance(state, StarLoopbackState): + self.checkCondition(len(state.transitions) == 1) + self.checkCondition( + isinstance(state.transitions[0].target, StarLoopEntryState) + ) + + if isinstance(state, LoopEndState): + self.checkCondition(state.loopBackState is not None) + + if isinstance(state, RuleStartState): + self.checkCondition(state.stopState is not None) + + if isinstance(state, BlockStartState): + self.checkCondition(state.endState is not None) + + if isinstance(state, BlockEndState): + self.checkCondition(state.startState is not None) + + if isinstance(state, DecisionState): + self.checkCondition( + len(state.transitions) <= 1 or state.decision >= 0 + ) + else: + self.checkCondition( + len(state.transitions) <= 1 + or isinstance(state, RuleStopState) + ) + + def checkCondition(self, condition: bool, message=None): + if not condition: + if message is None: + message = "IllegalState" + raise Exception(message) + + def readInt(self): + i = self.data[self.pos] + self.pos += 1 + return i + + edgeFactories = [ + lambda args: None, + lambda atn, + src, + trg, + arg1, + arg2, + arg3, + sets, + target: EpsilonTransition(target), + lambda atn, src, trg, arg1, arg2, arg3, sets, target: RangeTransition( + target, Token.EOF, arg2 + ) + if arg3 != 0 + else RangeTransition(target, arg1, arg2), + lambda atn, src, trg, arg1, arg2, arg3, sets, target: RuleTransition( + atn.states[arg1], arg2, arg3, target + ), + lambda atn, + src, + trg, + arg1, + arg2, + arg3, + sets, + target: PredicateTransition(target, arg1, arg2, arg3 != 0), + lambda atn, src, trg, arg1, arg2, arg3, sets, target: AtomTransition( + target, Token.EOF + ) + if arg3 != 0 + else AtomTransition(target, arg1), + lambda atn, src, trg, arg1, arg2, arg3, sets, target: ActionTransition( + target, arg1, arg2, arg3 != 0 + ), + lambda atn, src, trg, arg1, arg2, arg3, sets, target: SetTransition( + target, sets[arg1] + ), + lambda atn, src, trg, arg1, arg2, arg3, sets, target: NotSetTransition( + target, sets[arg1] + ), + lambda atn, + src, + trg, + arg1, + arg2, + arg3, + sets, + target: WildcardTransition(target), + lambda atn, + src, + trg, + arg1, + arg2, + arg3, + sets, + target: PrecedencePredicateTransition(target, arg1), + ] + + def edgeFactory( + self, + atn: ATN, + type: int, + src: int, + trg: int, + arg1: int, + arg2: int, + arg3: int, + sets: list, + ): + target = atn.states[trg] + if type > len(self.edgeFactories) or self.edgeFactories[type] is None: + raise Exception( + "The specified transition type: " + + str(type) + + " is not valid." + ) + else: + return self.edgeFactories[type]( + atn, src, trg, arg1, arg2, arg3, sets, target + ) + + stateFactories = [ + lambda: None, + lambda: BasicState(), + lambda: RuleStartState(), + lambda: BasicBlockStartState(), + lambda: PlusBlockStartState(), + lambda: StarBlockStartState(), + lambda: TokensStartState(), + lambda: RuleStopState(), + lambda: BlockEndState(), + lambda: StarLoopbackState(), + lambda: StarLoopEntryState(), + lambda: PlusLoopbackState(), + lambda: LoopEndState(), + ] + + def stateFactory(self, type: int, ruleIndex: int): + if ( + type > len(self.stateFactories) + or self.stateFactories[type] is None + ): + raise Exception( + "The specified state type " + str(type) + " is not valid." + ) + else: + s = self.stateFactories[type]() + if s is not None: + s.ruleIndex = ruleIndex + return s + + CHANNEL = 0 # The type of a {@link LexerChannelAction} action. + CUSTOM = 1 # The type of a {@link LexerCustomAction} action. + MODE = 2 # The type of a {@link LexerModeAction} action. + MORE = 3 # The type of a {@link LexerMoreAction} action. + POP_MODE = 4 # The type of a {@link LexerPopModeAction} action. + PUSH_MODE = 5 # The type of a {@link LexerPushModeAction} action. + SKIP = 6 # The type of a {@link LexerSkipAction} action. + TYPE = 7 # The type of a {@link LexerTypeAction} action. + + actionFactories = [ + lambda data1, data2: LexerChannelAction(data1), + lambda data1, data2: LexerCustomAction(data1, data2), + lambda data1, data2: LexerModeAction(data1), + lambda data1, data2: LexerMoreAction.INSTANCE, + lambda data1, data2: LexerPopModeAction.INSTANCE, + lambda data1, data2: LexerPushModeAction(data1), + lambda data1, data2: LexerSkipAction.INSTANCE, + lambda data1, data2: LexerTypeAction(data1), + ] + + def lexerActionFactory(self, type: int, data1: int, data2: int): + if ( + type > len(self.actionFactories) + or self.actionFactories[type] is None + ): + raise Exception( + "The specified lexer action type " + + str(type) + + " is not valid." + ) + else: + return self.actionFactories[type](data1, data2) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNSimulator.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNSimulator.py new file mode 100644 index 00000000..b7895eef --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNSimulator.py @@ -0,0 +1,57 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# / +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfigSet import ( + ATNConfigSet, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFAState import ( + DFAState, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( + PredictionContext, + PredictionContextCache, + getCachedPredictionContext, +) + + +class ATNSimulator: + __slots__ = ("atn", "sharedContextCache", "__dict__") + + # Must distinguish between missing edge and edge we know leads nowhere#/ + ERROR = DFAState(configs=ATNConfigSet()) + ERROR.stateNumber = 0x7FFFFFFF + + # The context cache maps all PredictionContext objects that are == + # to a single cached copy. This cache is shared across all contexts + # in all ATNConfigs in all DFA states. We rebuild each ATNConfigSet + # to use only cached nodes/graphs in addDFAState(). We don't want to + # fill this during closure() since there are lots of contexts that + # pop up but are not used ever again. It also greatly slows down closure(). + # + #

      This cache makes a huge difference in memory and a little bit in speed. + # For the Java grammar on java.*, it dropped the memory requirements + # at the end from 25M to 16M. We don't store any of the full context + # graphs in the DFA because they are limited to local context only, + # but apparently there's a lot of repetition there as well. We optimize + # the config contexts before storing the config set in the DFA states + # by literally rebuilding them with cached subgraphs only.

      + # + #

      I tried a cache for use during closure operations, that was + # whacked after each adaptivePredict(). It cost a little bit + # more time I think and doesn't save on the overall footprint + # so it's not worth the complexity.

      + # / + def __init__(self, atn: ATN, sharedContextCache: PredictionContextCache): + self.atn = atn + self.sharedContextCache = sharedContextCache + + def getCachedContext(self, context: PredictionContext): + if self.sharedContextCache is None: + return context + visited = dict() + return getCachedPredictionContext( + context, self.sharedContextCache, visited + ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNState.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNState.py new file mode 100644 index 00000000..f99e3c53 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNState.py @@ -0,0 +1,282 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +# The following images show the relation of states and +# {@link ATNState#transitions} for various grammar constructs. +# +#
        +# +#
      • Solid edges marked with an ε indicate a required +# {@link EpsilonTransition}.
      • +# +#
      • Dashed edges indicate locations where any transition derived from +# {@link Transition} might appear.
      • +# +#
      • Dashed nodes are place holders for either a sequence of linked +# {@link BasicState} states or the inclusion of a block representing a nested +# construct in one of the forms below.
      • +# +#
      • Nodes showing multiple outgoing alternatives with a {@code ...} support +# any number of alternatives (one or more). Nodes without the {@code ...} only +# support the exact number of alternatives shown in the diagram.
      • +# +#
      +# +#

      Basic Blocks

      +# +#

      Rule

      +# +# +# +#

      Block of 1 or more alternatives

      +# +# +# +#

      Greedy Loops

      +# +#

      Greedy Closure: {@code (...)*}

      +# +# +# +#

      Greedy Positive Closure: {@code (...)+}

      +# +# +# +#

      Greedy Optional: {@code (...)?}

      +# +# +# +#

      Non-Greedy Loops

      +# +#

      Non-Greedy Closure: {@code (...)*?}

      +# +# +# +#

      Non-Greedy Positive Closure: {@code (...)+?}

      +# +# +# +#

      Non-Greedy Optional: {@code (...)??}

      +# +# +# + +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.Transition import ( + Transition, +) + +INITIAL_NUM_TRANSITIONS = 4 + + +class ATNState: + __slots__ = ( + "atn", + "stateNumber", + "stateType", + "ruleIndex", + "epsilonOnlyTransitions", + "transitions", + "nextTokenWithinRule", + ) + + # constants for serialization + INVALID_TYPE = 0 + BASIC = 1 + RULE_START = 2 + BLOCK_START = 3 + PLUS_BLOCK_START = 4 + STAR_BLOCK_START = 5 + TOKEN_START = 6 + RULE_STOP = 7 + BLOCK_END = 8 + STAR_LOOP_BACK = 9 + STAR_LOOP_ENTRY = 10 + PLUS_LOOP_BACK = 11 + LOOP_END = 12 + + serializationNames = [ + "INVALID", + "BASIC", + "RULE_START", + "BLOCK_START", + "PLUS_BLOCK_START", + "STAR_BLOCK_START", + "TOKEN_START", + "RULE_STOP", + "BLOCK_END", + "STAR_LOOP_BACK", + "STAR_LOOP_ENTRY", + "PLUS_LOOP_BACK", + "LOOP_END", + ] + + INVALID_STATE_NUMBER = -1 + + def __init__(self): + # Which ATN are we in? + self.atn = None + self.stateNumber = ATNState.INVALID_STATE_NUMBER + self.stateType = None + self.ruleIndex = 0 # at runtime, we don't have Rule objects + self.epsilonOnlyTransitions = False + # Track the transitions emanating from this ATN state. + self.transitions = [] + # Used to cache lookahead during parsing, not used during construction + self.nextTokenWithinRule = None + + def __hash__(self): + return self.stateNumber + + def __eq__(self, other): + return ( + isinstance(other, ATNState) + and self.stateNumber == other.stateNumber + ) + + def onlyHasEpsilonTransitions(self): + return self.epsilonOnlyTransitions + + def isNonGreedyExitState(self): + return False + + def __str__(self): + return str(self.stateNumber) + + def addTransition(self, trans: Transition, index: int = -1): + if len(self.transitions) == 0: + self.epsilonOnlyTransitions = trans.isEpsilon + elif self.epsilonOnlyTransitions != trans.isEpsilon: + self.epsilonOnlyTransitions = False + # TODO System.err.format(Locale.getDefault(), "ATN state %d has both epsilon and non-epsilon transitions.\n", stateNumber); + if index == -1: + self.transitions.append(trans) + else: + self.transitions.insert(index, trans) + + +class BasicState(ATNState): + def __init__(self): + super().__init__() + self.stateType = self.BASIC + + +class DecisionState(ATNState): + __slots__ = ("decision", "nonGreedy") + + def __init__(self): + super().__init__() + self.decision = -1 + self.nonGreedy = False + + +# The start of a regular {@code (...)} block. +class BlockStartState(DecisionState): + __slots__ = "endState" + + def __init__(self): + super().__init__() + self.endState = None + + +class BasicBlockStartState(BlockStartState): + def __init__(self): + super().__init__() + self.stateType = self.BLOCK_START + + +# Terminal node of a simple {@code (a|b|c)} block. +class BlockEndState(ATNState): + __slots__ = "startState" + + def __init__(self): + super().__init__() + self.stateType = self.BLOCK_END + self.startState = None + + +# The last node in the ATN for a rule, unless that rule is the start symbol. +# In that case, there is one transition to EOF. Later, we might encode +# references to all calls to this rule to compute FOLLOW sets for +# error handling. +# +class RuleStopState(ATNState): + def __init__(self): + super().__init__() + self.stateType = self.RULE_STOP + + +class RuleStartState(ATNState): + __slots__ = ("stopState", "isPrecedenceRule") + + def __init__(self): + super().__init__() + self.stateType = self.RULE_START + self.stopState = None + self.isPrecedenceRule = False + + +# Decision state for {@code A+} and {@code (A|B)+}. It has two transitions: +# one to the loop back to start of the block and one to exit. +# +class PlusLoopbackState(DecisionState): + def __init__(self): + super().__init__() + self.stateType = self.PLUS_LOOP_BACK + + +# Start of {@code (A|B|...)+} loop. Technically a decision state, but +# we don't use for code generation; somebody might need it, so I'm defining +# it for completeness. In reality, the {@link PlusLoopbackState} node is the +# real decision-making note for {@code A+}. +# +class PlusBlockStartState(BlockStartState): + __slots__ = "loopBackState" + + def __init__(self): + super().__init__() + self.stateType = self.PLUS_BLOCK_START + self.loopBackState = None + + +# The block that begins a closure loop. +class StarBlockStartState(BlockStartState): + def __init__(self): + super().__init__() + self.stateType = self.STAR_BLOCK_START + + +class StarLoopbackState(ATNState): + def __init__(self): + super().__init__() + self.stateType = self.STAR_LOOP_BACK + + +class StarLoopEntryState(DecisionState): + __slots__ = ("loopBackState", "isPrecedenceDecision") + + def __init__(self): + super().__init__() + self.stateType = self.STAR_LOOP_ENTRY + self.loopBackState = None + # Indicates whether this state can benefit from a precedence DFA during SLL decision making. + self.isPrecedenceDecision = None + + +# Mark the end of a * or + loop. +class LoopEndState(ATNState): + __slots__ = "loopBackState" + + def __init__(self): + super().__init__() + self.stateType = self.LOOP_END + self.loopBackState = None + + +# The Tokens rule start state linking to each lexer rule start state */ +class TokensStartState(DecisionState): + def __init__(self): + super().__init__() + self.stateType = self.TOKEN_START diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNType.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNType.py new file mode 100644 index 00000000..9c877db5 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNType.py @@ -0,0 +1,17 @@ +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# / + +from enum import IntEnum + +# Represents the type of recognizer an ATN applies to. + + +class ATNType(IntEnum): + LEXER = 0 + PARSER = 1 + + @classmethod + def fromOrdinal(cls, i: int): + return cls._value2member_map_[i] diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerATNSimulator.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerATNSimulator.py new file mode 100644 index 00000000..bc94eb39 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerATNSimulator.py @@ -0,0 +1,763 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# / + +# When we hit an accept state in either the DFA or the ATN, we +# have to notify the character stream to start buffering characters +# via {@link IntStream#mark} and record the current state. The current sim state +# includes the current index into the input, the current line, +# and current character position in that line. Note that the Lexer is +# tracking the starting line and characterization of the token. These +# variables track the "state" of the simulator when it hits an accept state. +# +#

      We track these variables separately for the DFA and ATN simulation +# because the DFA simulation often has to fail over to the ATN +# simulation. If the ATN simulation fails, we need the DFA to fall +# back to its previously accepted state, if any. If the ATN succeeds, +# then the ATN does the accept and the DFA simulator that invoked it +# can simply return the predicted token type.

      +# / + +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfig import ( + LexerATNConfig, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfigSet import ( + ATNConfigSet, + OrderedATNConfigSet, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNSimulator import ( + ATNSimulator, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( + ATNState, + RuleStopState, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.LexerActionExecutor import ( + LexerActionExecutor, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.Transition import ( + Transition, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFAState import ( + DFAState, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + LexerNoViableAltException, + UnsupportedOperationException, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( + InputStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( + PredictionContext, + PredictionContextCache, + SingletonPredictionContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + + +class SimState: + __slots__ = ("index", "line", "column", "dfaState") + + def __init__(self): + self.reset() + + def reset(self): + self.index = -1 + self.line = 0 + self.column = -1 + self.dfaState = None + + +# need forward declaration +Lexer = None +LexerATNSimulator = None + + +class LexerATNSimulator(ATNSimulator): + __slots__ = ( + "decisionToDFA", + "recog", + "startIndex", + "line", + "column", + "mode", + "DEFAULT_MODE", + "MAX_CHAR_VALUE", + "prevAccept", + ) + + debug = False + dfa_debug = False + + MIN_DFA_EDGE = 0 + MAX_DFA_EDGE = 127 # forces unicode to stay in ATN + + ERROR = None + + def __init__( + self, + recog: Lexer, + atn: ATN, + decisionToDFA: list, + sharedContextCache: PredictionContextCache, + ): + super().__init__(atn, sharedContextCache) + self.decisionToDFA = decisionToDFA + self.recog = recog + # The current token's starting index into the character stream. + # Shared across DFA to ATN simulation in case the ATN fails and the + # DFA did not have a previous accept state. In this case, we use the + # ATN-generated exception object. + self.startIndex = -1 + # line number 1..n within the input#/ + self.line = 1 + # The index of the character relative to the beginning of the line 0..n-1#/ + self.column = 0 + from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import ( + Lexer, + ) + + self.mode = Lexer.DEFAULT_MODE + # Cache Lexer properties to avoid further imports + self.DEFAULT_MODE = Lexer.DEFAULT_MODE + self.MAX_CHAR_VALUE = Lexer.MAX_CHAR_VALUE + # Used during DFA/ATN exec to record the most recent accept configuration info + self.prevAccept = SimState() + + def copyState(self, simulator: LexerATNSimulator): + self.column = simulator.column + self.line = simulator.line + self.mode = simulator.mode + self.startIndex = simulator.startIndex + + def match(self, input: InputStream, mode: int): + self.mode = mode + mark = input.mark() + try: + self.startIndex = input.index + self.prevAccept.reset() + dfa = self.decisionToDFA[mode] + if dfa.s0 is None: + return self.matchATN(input) + else: + return self.execATN(input, dfa.s0) + finally: + input.release(mark) + + def reset(self): + self.prevAccept.reset() + self.startIndex = -1 + self.line = 1 + self.column = 0 + self.mode = self.DEFAULT_MODE + + def matchATN(self, input: InputStream): + startState = self.atn.modeToStartState[self.mode] + + if LexerATNSimulator.debug: + print( + "matchATN mode " + + str(self.mode) + + " start: " + + str(startState) + ) + + old_mode = self.mode + s0_closure = self.computeStartState(input, startState) + suppressEdge = s0_closure.hasSemanticContext + s0_closure.hasSemanticContext = False + + next = self.addDFAState(s0_closure) + if not suppressEdge: + self.decisionToDFA[self.mode].s0 = next + + predict = self.execATN(input, next) + + if LexerATNSimulator.debug: + print( + "DFA after matchATN: " + + str(self.decisionToDFA[old_mode].toLexerString()) + ) + + return predict + + def execATN(self, input: InputStream, ds0: DFAState): + if LexerATNSimulator.debug: + print("start state closure=" + str(ds0.configs)) + + if ds0.isAcceptState: + # allow zero-length tokens + self.captureSimState(self.prevAccept, input, ds0) + + t = input.LA(1) + s = ds0 # s is current/from DFA state + + while True: # while more work + if LexerATNSimulator.debug: + print("execATN loop starting closure:", str(s.configs)) + + # As we move src->trg, src->trg, we keep track of the previous trg to + # avoid looking up the DFA state again, which is expensive. + # If the previous target was already part of the DFA, we might + # be able to avoid doing a reach operation upon t. If s!=null, + # it means that semantic predicates didn't prevent us from + # creating a DFA state. Once we know s!=null, we check to see if + # the DFA state has an edge already for t. If so, we can just reuse + # it's configuration set; there's no point in re-computing it. + # This is kind of like doing DFA simulation within the ATN + # simulation because DFA simulation is really just a way to avoid + # computing reach/closure sets. Technically, once we know that + # we have a previously added DFA state, we could jump over to + # the DFA simulator. But, that would mean popping back and forth + # a lot and making things more complicated algorithmically. + # This optimization makes a lot of sense for loops within DFA. + # A character will take us back to an existing DFA state + # that already has lots of edges out of it. e.g., .* in comments. + # print("Target for:" + str(s) + " and:" + str(t)) + target = self.getExistingTargetState(s, t) + # print("Existing:" + str(target)) + if target is None: + target = self.computeTargetState(input, s, t) + # print("Computed:" + str(target)) + + if target == self.ERROR: + break + + # If this is a consumable input element, make sure to consume before + # capturing the accept state so the input index, line, and char + # position accurately reflect the state of the interpreter at the + # end of the token. + if t != Token.EOF: + self.consume(input) + + if target.isAcceptState: + self.captureSimState(self.prevAccept, input, target) + if t == Token.EOF: + break + + t = input.LA(1) + + s = target # flip; current DFA target becomes new src/from state + + return self.failOrAccept(self.prevAccept, input, s.configs, t) + + # Get an existing target state for an edge in the DFA. If the target state + # for the edge has not yet been computed or is otherwise not available, + # this method returns {@code null}. + # + # @param s The current DFA state + # @param t The next input symbol + # @return The existing target DFA state for the given input symbol + # {@code t}, or {@code null} if the target state for this edge is not + # already cached + def getExistingTargetState(self, s: DFAState, t: int): + if s.edges is None or t < self.MIN_DFA_EDGE or t > self.MAX_DFA_EDGE: + return None + + target = s.edges[t - self.MIN_DFA_EDGE] + if LexerATNSimulator.debug and target is not None: + print( + "reuse state", + str(s.stateNumber), + "edge to", + str(target.stateNumber), + ) + + return target + + # Compute a target state for an edge in the DFA, and attempt to add the + # computed state and corresponding edge to the DFA. + # + # @param input The input stream + # @param s The current DFA state + # @param t The next input symbol + # + # @return The computed target DFA state for the given input symbol + # {@code t}. If {@code t} does not lead to a valid DFA state, this method + # returns {@link #ERROR}. + def computeTargetState(self, input: InputStream, s: DFAState, t: int): + reach = OrderedATNConfigSet() + + # if we don't find an existing DFA state + # Fill reach starting from closure, following t transitions + self.getReachableConfigSet(input, s.configs, reach, t) + + if len(reach) == 0: # we got nowhere on t from s + if not reach.hasSemanticContext: + # we got nowhere on t, don't throw out this knowledge; it'd + # cause a failover from DFA later. + self.addDFAEdge(s, t, self.ERROR) + + # stop when we can't match any more char + return self.ERROR + + # Add an edge from s to target DFA found/created for reach + return self.addDFAEdge(s, t, cfgs=reach) + + def failOrAccept( + self, + prevAccept: SimState, + input: InputStream, + reach: ATNConfigSet, + t: int, + ): + if self.prevAccept.dfaState is not None: + lexerActionExecutor = prevAccept.dfaState.lexerActionExecutor + self.accept( + input, + lexerActionExecutor, + self.startIndex, + prevAccept.index, + prevAccept.line, + prevAccept.column, + ) + return prevAccept.dfaState.prediction + else: + # if no accept and EOF is first char, return EOF + if t == Token.EOF and input.index == self.startIndex: + return Token.EOF + raise LexerNoViableAltException( + self.recog, input, self.startIndex, reach + ) + + # Given a starting configuration set, figure out all ATN configurations + # we can reach upon input {@code t}. Parameter {@code reach} is a return + # parameter. + def getReachableConfigSet( + self, + input: InputStream, + closure: ATNConfigSet, + reach: ATNConfigSet, + t: int, + ): + # this is used to skip processing for configs which have a lower priority + # than a config that already reached an accept state for the same rule + skipAlt = ATN.INVALID_ALT_NUMBER + for cfg in closure: + currentAltReachedAcceptState = cfg.alt == skipAlt + if ( + currentAltReachedAcceptState + and cfg.passedThroughNonGreedyDecision + ): + continue + + if LexerATNSimulator.debug: + print("testing", self.getTokenName(t), "at", str(cfg)) + + for trans in cfg.state.transitions: # for each transition + target = self.getReachableTarget(trans, t) + if target is not None: + lexerActionExecutor = cfg.lexerActionExecutor + if lexerActionExecutor is not None: + lexerActionExecutor = ( + lexerActionExecutor.fixOffsetBeforeMatch( + input.index - self.startIndex + ) + ) + + treatEofAsEpsilon = t == Token.EOF + config = LexerATNConfig( + state=target, + lexerActionExecutor=lexerActionExecutor, + config=cfg, + ) + if self.closure( + input, + config, + reach, + currentAltReachedAcceptState, + True, + treatEofAsEpsilon, + ): + # any remaining configs for this alt have a lower priority than + # the one that just reached an accept state. + skipAlt = cfg.alt + + def accept( + self, + input: InputStream, + lexerActionExecutor: LexerActionExecutor, + startIndex: int, + index: int, + line: int, + charPos: int, + ): + if LexerATNSimulator.debug: + print("ACTION", lexerActionExecutor) + + # seek to after last char in token + input.seek(index) + self.line = line + self.column = charPos + + if lexerActionExecutor is not None and self.recog is not None: + lexerActionExecutor.execute(self.recog, input, startIndex) + + def getReachableTarget(self, trans: Transition, t: int): + if trans.matches(t, 0, self.MAX_CHAR_VALUE): + return trans.target + else: + return None + + def computeStartState(self, input: InputStream, p: ATNState): + initialContext = PredictionContext.EMPTY + configs = OrderedATNConfigSet() + for i in range(0, len(p.transitions)): + target = p.transitions[i].target + c = LexerATNConfig(state=target, alt=i + 1, context=initialContext) + self.closure(input, c, configs, False, False, False) + return configs + + # Since the alternatives within any lexer decision are ordered by + # preference, this method stops pursuing the closure as soon as an accept + # state is reached. After the first accept state is reached by depth-first + # search from {@code config}, all other (potentially reachable) states for + # this rule would have a lower priority. + # + # @return {@code true} if an accept state is reached, otherwise + # {@code false}. + def closure( + self, + input: InputStream, + config: LexerATNConfig, + configs: ATNConfigSet, + currentAltReachedAcceptState: bool, + speculative: bool, + treatEofAsEpsilon: bool, + ): + if LexerATNSimulator.debug: + print("closure(" + str(config) + ")") + + if isinstance(config.state, RuleStopState): + if LexerATNSimulator.debug: + if self.recog is not None: + print( + "closure at", + self.recog.symbolicNames[config.state.ruleIndex], + "rule stop", + str(config), + ) + else: + print("closure at rule stop", str(config)) + + if config.context is None or config.context.hasEmptyPath(): + if config.context is None or config.context.isEmpty(): + configs.add(config) + return True + else: + configs.add( + LexerATNConfig( + state=config.state, + config=config, + context=PredictionContext.EMPTY, + ) + ) + currentAltReachedAcceptState = True + + if config.context is not None and not config.context.isEmpty(): + for i in range(0, len(config.context)): + if ( + config.context.getReturnState(i) + != PredictionContext.EMPTY_RETURN_STATE + ): + newContext = config.context.getParent( + i + ) # "pop" return state + returnState = self.atn.states[ + config.context.getReturnState(i) + ] + c = LexerATNConfig( + state=returnState, + config=config, + context=newContext, + ) + currentAltReachedAcceptState = self.closure( + input, + c, + configs, + currentAltReachedAcceptState, + speculative, + treatEofAsEpsilon, + ) + + return currentAltReachedAcceptState + + # optimization + if not config.state.epsilonOnlyTransitions: + if ( + not currentAltReachedAcceptState + or not config.passedThroughNonGreedyDecision + ): + configs.add(config) + + for t in config.state.transitions: + c = self.getEpsilonTarget( + input, config, t, configs, speculative, treatEofAsEpsilon + ) + if c is not None: + currentAltReachedAcceptState = self.closure( + input, + c, + configs, + currentAltReachedAcceptState, + speculative, + treatEofAsEpsilon, + ) + + return currentAltReachedAcceptState + + # side-effect: can alter configs.hasSemanticContext + def getEpsilonTarget( + self, + input: InputStream, + config: LexerATNConfig, + t: Transition, + configs: ATNConfigSet, + speculative: bool, + treatEofAsEpsilon: bool, + ): + c = None + if t.serializationType == Transition.RULE: + newContext = SingletonPredictionContext.create( + config.context, t.followState.stateNumber + ) + c = LexerATNConfig( + state=t.target, config=config, context=newContext + ) + + elif t.serializationType == Transition.PRECEDENCE: + raise UnsupportedOperationException( + "Precedence predicates are not supported in lexers." + ) + + elif t.serializationType == Transition.PREDICATE: + # Track traversing semantic predicates. If we traverse, + # we cannot add a DFA state for this "reach" computation + # because the DFA would not test the predicate again in the + # future. Rather than creating collections of semantic predicates + # like v3 and testing them on prediction, v4 will test them on the + # fly all the time using the ATN not the DFA. This is slower but + # semantically it's not used that often. One of the key elements to + # this predicate mechanism is not adding DFA states that see + # predicates immediately afterwards in the ATN. For example, + + # a : ID {p1}? | ID {p2}? ; + + # should create the start state for rule 'a' (to save start state + # competition), but should not create target of ID state. The + # collection of ATN states the following ID references includes + # states reached by traversing predicates. Since this is when we + # test them, we cannot cash the DFA state target of ID. + + if LexerATNSimulator.debug: + print("EVAL rule " + str(t.ruleIndex) + ":" + str(t.predIndex)) + configs.hasSemanticContext = True + if self.evaluatePredicate( + input, t.ruleIndex, t.predIndex, speculative + ): + c = LexerATNConfig(state=t.target, config=config) + + elif t.serializationType == Transition.ACTION: + if config.context is None or config.context.hasEmptyPath(): + # execute actions anywhere in the start rule for a token. + # + # TODO: if the entry rule is invoked recursively, some + # actions may be executed during the recursive call. The + # problem can appear when hasEmptyPath() is true but + # isEmpty() is false. In this case, the config needs to be + # split into two contexts - one with just the empty path + # and another with everything but the empty path. + # Unfortunately, the current algorithm does not allow + # getEpsilonTarget to return two configurations, so + # additional modifications are needed before we can support + # the split operation. + lexerActionExecutor = LexerActionExecutor.append( + config.lexerActionExecutor, + self.atn.lexerActions[t.actionIndex], + ) + c = LexerATNConfig( + state=t.target, + config=config, + lexerActionExecutor=lexerActionExecutor, + ) + + else: + # ignore actions in referenced rules + c = LexerATNConfig(state=t.target, config=config) + + elif t.serializationType == Transition.EPSILON: + c = LexerATNConfig(state=t.target, config=config) + + elif t.serializationType in [ + Transition.ATOM, + Transition.RANGE, + Transition.SET, + ]: + if treatEofAsEpsilon: + if t.matches(Token.EOF, 0, self.MAX_CHAR_VALUE): + c = LexerATNConfig(state=t.target, config=config) + + return c + + # Evaluate a predicate specified in the lexer. + # + #

      If {@code speculative} is {@code true}, this method was called before + # {@link #consume} for the matched character. This method should call + # {@link #consume} before evaluating the predicate to ensure position + # sensitive values, including {@link Lexer#getText}, {@link Lexer#getLine}, + # and {@link Lexer#getcolumn}, properly reflect the current + # lexer state. This method should restore {@code input} and the simulator + # to the original state before returning (i.e. undo the actions made by the + # call to {@link #consume}.

      + # + # @param input The input stream. + # @param ruleIndex The rule containing the predicate. + # @param predIndex The index of the predicate within the rule. + # @param speculative {@code true} if the current index in {@code input} is + # one character before the predicate's location. + # + # @return {@code true} if the specified predicate evaluates to + # {@code true}. + # / + def evaluatePredicate( + self, + input: InputStream, + ruleIndex: int, + predIndex: int, + speculative: bool, + ): + # assume true if no recognizer was provided + if self.recog is None: + return True + + if not speculative: + return self.recog.sempred(None, ruleIndex, predIndex) + + savedcolumn = self.column + savedLine = self.line + index = input.index + marker = input.mark() + try: + self.consume(input) + return self.recog.sempred(None, ruleIndex, predIndex) + finally: + self.column = savedcolumn + self.line = savedLine + input.seek(index) + input.release(marker) + + def captureSimState( + self, settings: SimState, input: InputStream, dfaState: DFAState + ): + settings.index = input.index + settings.line = self.line + settings.column = self.column + settings.dfaState = dfaState + + def addDFAEdge( + self, + from_: DFAState, + tk: int, + to: DFAState = None, + cfgs: ATNConfigSet = None, + ) -> DFAState: + if to is None and cfgs is not None: + # leading to this call, ATNConfigSet.hasSemanticContext is used as a + # marker indicating dynamic predicate evaluation makes this edge + # dependent on the specific input sequence, so the static edge in the + # DFA should be omitted. The target DFAState is still created since + # execATN has the ability to resynchronize with the DFA state cache + # following the predicate evaluation step. + # + # TJP notes: next time through the DFA, we see a pred again and eval. + # If that gets us to a previously created (but dangling) DFA + # state, we can continue in pure DFA mode from there. + # / + suppressEdge = cfgs.hasSemanticContext + cfgs.hasSemanticContext = False + + to = self.addDFAState(cfgs) + + if suppressEdge: + return to + + # add the edge + if tk < self.MIN_DFA_EDGE or tk > self.MAX_DFA_EDGE: + # Only track edges within the DFA bounds + return to + + if LexerATNSimulator.debug: + print("EDGE " + str(from_) + " -> " + str(to) + " upon " + chr(tk)) + + if from_.edges is None: + # make room for tokens 1..n and -1 masquerading as index 0 + from_.edges = [None] * (self.MAX_DFA_EDGE - self.MIN_DFA_EDGE + 1) + + from_.edges[tk - self.MIN_DFA_EDGE] = to # connect + + return to + + # Add a new DFA state if there isn't one with this set of + # configurations already. This method also detects the first + # configuration containing an ATN rule stop state. Later, when + # traversing the DFA, we will know which rule to accept. + def addDFAState(self, configs: ATNConfigSet) -> DFAState: + proposed = DFAState(configs=configs) + firstConfigWithRuleStopState = next( + (cfg for cfg in configs if isinstance(cfg.state, RuleStopState)), + None, + ) + + if firstConfigWithRuleStopState is not None: + proposed.isAcceptState = True + proposed.lexerActionExecutor = ( + firstConfigWithRuleStopState.lexerActionExecutor + ) + proposed.prediction = self.atn.ruleToTokenType[ + firstConfigWithRuleStopState.state.ruleIndex + ] + + dfa = self.decisionToDFA[self.mode] + existing = dfa.states.get(proposed, None) + if existing is not None: + return existing + + newState = proposed + + newState.stateNumber = len(dfa.states) + configs.setReadonly(True) + newState.configs = configs + dfa.states[newState] = newState + return newState + + def getDFA(self, mode: int): + return self.decisionToDFA[mode] + + # Get the text matched so far for the current token. + def getText(self, input: InputStream): + # index is first lookahead char, don't include. + return input.getText(self.startIndex, input.index - 1) + + def consume(self, input: InputStream): + curChar = input.LA(1) + if curChar == ord("\n"): + self.line += 1 + self.column = 0 + else: + self.column += 1 + input.consume() + + def getTokenName(self, t: int): + if t == -1: + return "EOF" + else: + return "'" + chr(t) + "'" + + +LexerATNSimulator.ERROR = DFAState(0x7FFFFFFF, ATNConfigSet()) + +del Lexer diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerAction.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerAction.py new file mode 100644 index 00000000..a2c217a1 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerAction.py @@ -0,0 +1,308 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +from enum import IntEnum + +# need forward declaration +Lexer = None + + +class LexerActionType(IntEnum): + CHANNEL = 0 # The type of a {@link LexerChannelAction} action. + CUSTOM = 1 # The type of a {@link LexerCustomAction} action. + MODE = 2 # The type of a {@link LexerModeAction} action. + MORE = 3 # The type of a {@link LexerMoreAction} action. + POP_MODE = 4 # The type of a {@link LexerPopModeAction} action. + PUSH_MODE = 5 # The type of a {@link LexerPushModeAction} action. + SKIP = 6 # The type of a {@link LexerSkipAction} action. + TYPE = 7 # The type of a {@link LexerTypeAction} action. + + +class LexerAction: + __slots__ = ("actionType", "isPositionDependent") + + def __init__(self, action: LexerActionType): + self.actionType = action + self.isPositionDependent = False + + def __hash__(self): + return hash(self.actionType) + + def __eq__(self, other): + return self is other + + +# +# Implements the {@code skip} lexer action by calling {@link Lexer#skip}. +# +#

      The {@code skip} command does not have any parameters, so this action is +# implemented as a singleton instance exposed by {@link #INSTANCE}.

      +class LexerSkipAction(LexerAction): + # Provides a singleton instance of this parameterless lexer action. + INSTANCE = None + + def __init__(self): + super().__init__(LexerActionType.SKIP) + + def execute(self, lexer: Lexer): + lexer.skip() + + def __str__(self): + return "skip" + + +LexerSkipAction.INSTANCE = LexerSkipAction() + + +# Implements the {@code type} lexer action by calling {@link Lexer#setType} +# with the assigned type. +class LexerTypeAction(LexerAction): + __slots__ = "type" + + def __init__(self, type: int): + super().__init__(LexerActionType.TYPE) + self.type = type + + def execute(self, lexer: Lexer): + lexer.type = self.type + + def __hash__(self): + return hash((self.actionType, self.type)) + + def __eq__(self, other): + if self is other: + return True + elif not isinstance(other, LexerTypeAction): + return False + else: + return self.type == other.type + + def __str__(self): + return "type(" + str(self.type) + ")" + + +# Implements the {@code pushMode} lexer action by calling +# {@link Lexer#pushMode} with the assigned mode. +class LexerPushModeAction(LexerAction): + __slots__ = "mode" + + def __init__(self, mode: int): + super().__init__(LexerActionType.PUSH_MODE) + self.mode = mode + + #

      This action is implemented by calling {@link Lexer#pushMode} with the + # value provided by {@link #getMode}.

      + def execute(self, lexer: Lexer): + lexer.pushMode(self.mode) + + def __hash__(self): + return hash((self.actionType, self.mode)) + + def __eq__(self, other): + if self is other: + return True + elif not isinstance(other, LexerPushModeAction): + return False + else: + return self.mode == other.mode + + def __str__(self): + return "pushMode(" + str(self.mode) + ")" + + +# Implements the {@code popMode} lexer action by calling {@link Lexer#popMode}. +# +#

      The {@code popMode} command does not have any parameters, so this action is +# implemented as a singleton instance exposed by {@link #INSTANCE}.

      +class LexerPopModeAction(LexerAction): + INSTANCE = None + + def __init__(self): + super().__init__(LexerActionType.POP_MODE) + + #

      This action is implemented by calling {@link Lexer#popMode}.

      + def execute(self, lexer: Lexer): + lexer.popMode() + + def __str__(self): + return "popMode" + + +LexerPopModeAction.INSTANCE = LexerPopModeAction() + + +# Implements the {@code more} lexer action by calling {@link Lexer#more}. +# +#

      The {@code more} command does not have any parameters, so this action is +# implemented as a singleton instance exposed by {@link #INSTANCE}.

      +class LexerMoreAction(LexerAction): + INSTANCE = None + + def __init__(self): + super().__init__(LexerActionType.MORE) + + #

      This action is implemented by calling {@link Lexer#popMode}.

      + def execute(self, lexer: Lexer): + lexer.more() + + def __str__(self): + return "more" + + +LexerMoreAction.INSTANCE = LexerMoreAction() + + +# Implements the {@code mode} lexer action by calling {@link Lexer#mode} with +# the assigned mode. +class LexerModeAction(LexerAction): + __slots__ = "mode" + + def __init__(self, mode: int): + super().__init__(LexerActionType.MODE) + self.mode = mode + + #

      This action is implemented by calling {@link Lexer#mode} with the + # value provided by {@link #getMode}.

      + def execute(self, lexer: Lexer): + lexer.mode(self.mode) + + def __hash__(self): + return hash((self.actionType, self.mode)) + + def __eq__(self, other): + if self is other: + return True + elif not isinstance(other, LexerModeAction): + return False + else: + return self.mode == other.mode + + def __str__(self): + return "mode(" + str(self.mode) + ")" + + +# Executes a custom lexer action by calling {@link Recognizer#action} with the +# rule and action indexes assigned to the custom action. The implementation of +# a custom action is added to the generated code for the lexer in an override +# of {@link Recognizer#action} when the grammar is compiled. +# +#

      This class may represent embedded actions created with the {...} +# syntax in ANTLR 4, as well as actions created for lexer commands where the +# command argument could not be evaluated when the grammar was compiled.

      + + +class LexerCustomAction(LexerAction): + __slots__ = ("ruleIndex", "actionIndex") + + # Constructs a custom lexer action with the specified rule and action + # indexes. + # + # @param ruleIndex The rule index to use for calls to + # {@link Recognizer#action}. + # @param actionIndex The action index to use for calls to + # {@link Recognizer#action}. + # / + def __init__(self, ruleIndex: int, actionIndex: int): + super().__init__(LexerActionType.CUSTOM) + self.ruleIndex = ruleIndex + self.actionIndex = actionIndex + self.isPositionDependent = True + + #

      Custom actions are implemented by calling {@link Lexer#action} with the + # appropriate rule and action indexes.

      + def execute(self, lexer: Lexer): + lexer.action(None, self.ruleIndex, self.actionIndex) + + def __hash__(self): + return hash((self.actionType, self.ruleIndex, self.actionIndex)) + + def __eq__(self, other): + if self is other: + return True + elif not isinstance(other, LexerCustomAction): + return False + else: + return ( + self.ruleIndex == other.ruleIndex + and self.actionIndex == other.actionIndex + ) + + +# Implements the {@code channel} lexer action by calling +# {@link Lexer#setChannel} with the assigned channel. +class LexerChannelAction(LexerAction): + __slots__ = "channel" + + # Constructs a new {@code channel} action with the specified channel value. + # @param channel The channel value to pass to {@link Lexer#setChannel}. + def __init__(self, channel: int): + super().__init__(LexerActionType.CHANNEL) + self.channel = channel + + #

      This action is implemented by calling {@link Lexer#setChannel} with the + # value provided by {@link #getChannel}.

      + def execute(self, lexer: Lexer): + lexer._channel = self.channel + + def __hash__(self): + return hash((self.actionType, self.channel)) + + def __eq__(self, other): + if self is other: + return True + elif not isinstance(other, LexerChannelAction): + return False + else: + return self.channel == other.channel + + def __str__(self): + return "channel(" + str(self.channel) + ")" + + +# This implementation of {@link LexerAction} is used for tracking input offsets +# for position-dependent actions within a {@link LexerActionExecutor}. +# +#

      This action is not serialized as part of the ATN, and is only required for +# position-dependent lexer actions which appear at a location other than the +# end of a rule. For more information about DFA optimizations employed for +# lexer actions, see {@link LexerActionExecutor#append} and +# {@link LexerActionExecutor#fixOffsetBeforeMatch}.

      +class LexerIndexedCustomAction(LexerAction): + __slots__ = ("offset", "action") + + # Constructs a new indexed custom action by associating a character offset + # with a {@link LexerAction}. + # + #

      Note: This class is only required for lexer actions for which + # {@link LexerAction#isPositionDependent} returns {@code true}.

      + # + # @param offset The offset into the input {@link CharStream}, relative to + # the token start index, at which the specified lexer action should be + # executed. + # @param action The lexer action to execute at a particular offset in the + # input {@link CharStream}. + def __init__(self, offset: int, action: LexerAction): + super().__init__(action.actionType) + self.offset = offset + self.action = action + self.isPositionDependent = True + + #

      This method calls {@link #execute} on the result of {@link #getAction} + # using the provided {@code lexer}.

      + def execute(self, lexer: Lexer): + # assume the input stream position was properly set by the calling code + self.action.execute(lexer) + + def __hash__(self): + return hash((self.actionType, self.offset, self.action)) + + def __eq__(self, other): + if self is other: + return True + elif not isinstance(other, LexerIndexedCustomAction): + return False + else: + return self.offset == other.offset and self.action == other.action diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerActionExecutor.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerActionExecutor.py new file mode 100644 index 00000000..b0b46dbc --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerActionExecutor.py @@ -0,0 +1,156 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# / + +# Represents an executor for a sequence of lexer actions which traversed during +# the matching operation of a lexer rule (token). +# +#

      The executor tracks position information for position-dependent lexer actions +# efficiently, ensuring that actions appearing only at the end of the rule do +# not cause bloating of the {@link DFA} created for the lexer.

      + + +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.LexerAction import ( + LexerAction, + LexerIndexedCustomAction, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( + InputStream, +) + +# need a forward declaration +Lexer = None +LexerActionExecutor = None + + +class LexerActionExecutor: + __slots__ = ("lexerActions", "hashCode") + + def __init__(self, lexerActions: list = list()): + self.lexerActions = lexerActions + # Caches the result of {@link #hashCode} since the hash code is an element + # of the performance-critical {@link LexerATNConfig#hashCode} operation. + self.hashCode = hash("".join([str(la) for la in lexerActions])) + + # Creates a {@link LexerActionExecutor} which executes the actions for + # the input {@code lexerActionExecutor} followed by a specified + # {@code lexerAction}. + # + # @param lexerActionExecutor The executor for actions already traversed by + # the lexer while matching a token within a particular + # {@link LexerATNConfig}. If this is {@code null}, the method behaves as + # though it were an empty executor. + # @param lexerAction The lexer action to execute after the actions + # specified in {@code lexerActionExecutor}. + # + # @return A {@link LexerActionExecutor} for executing the combine actions + # of {@code lexerActionExecutor} and {@code lexerAction}. + @staticmethod + def append( + lexerActionExecutor: LexerActionExecutor, lexerAction: LexerAction + ): + if lexerActionExecutor is None: + return LexerActionExecutor([lexerAction]) + + lexerActions = lexerActionExecutor.lexerActions + [lexerAction] + return LexerActionExecutor(lexerActions) + + # Creates a {@link LexerActionExecutor} which encodes the current offset + # for position-dependent lexer actions. + # + #

      Normally, when the executor encounters lexer actions where + # {@link LexerAction#isPositionDependent} returns {@code true}, it calls + # {@link IntStream#seek} on the input {@link CharStream} to set the input + # position to the end of the current token. This behavior provides + # for efficient DFA representation of lexer actions which appear at the end + # of a lexer rule, even when the lexer rule matches a variable number of + # characters.

      + # + #

      Prior to traversing a match transition in the ATN, the current offset + # from the token start index is assigned to all position-dependent lexer + # actions which have not already been assigned a fixed offset. By storing + # the offsets relative to the token start index, the DFA representation of + # lexer actions which appear in the middle of tokens remains efficient due + # to sharing among tokens of the same length, regardless of their absolute + # position in the input stream.

      + # + #

      If the current executor already has offsets assigned to all + # position-dependent lexer actions, the method returns {@code this}.

      + # + # @param offset The current offset to assign to all position-dependent + # lexer actions which do not already have offsets assigned. + # + # @return A {@link LexerActionExecutor} which stores input stream offsets + # for all position-dependent lexer actions. + # / + def fixOffsetBeforeMatch(self, offset: int): + updatedLexerActions = None + for i in range(0, len(self.lexerActions)): + if self.lexerActions[i].isPositionDependent and not isinstance( + self.lexerActions[i], LexerIndexedCustomAction + ): + if updatedLexerActions is None: + updatedLexerActions = [la for la in self.lexerActions] + updatedLexerActions[i] = LexerIndexedCustomAction( + offset, self.lexerActions[i] + ) + + if updatedLexerActions is None: + return self + else: + return LexerActionExecutor(updatedLexerActions) + + # Execute the actions encapsulated by this executor within the context of a + # particular {@link Lexer}. + # + #

      This method calls {@link IntStream#seek} to set the position of the + # {@code input} {@link CharStream} prior to calling + # {@link LexerAction#execute} on a position-dependent action. Before the + # method returns, the input position will be restored to the same position + # it was in when the method was invoked.

      + # + # @param lexer The lexer instance. + # @param input The input stream which is the source for the current token. + # When this method is called, the current {@link IntStream#index} for + # {@code input} should be the start of the following token, i.e. 1 + # character past the end of the current token. + # @param startIndex The token start index. This value may be passed to + # {@link IntStream#seek} to set the {@code input} position to the beginning + # of the token. + # / + def execute(self, lexer: Lexer, input: InputStream, startIndex: int): + requiresSeek = False + stopIndex = input.index + try: + for lexerAction in self.lexerActions: + if isinstance(lexerAction, LexerIndexedCustomAction): + offset = lexerAction.offset + input.seek(startIndex + offset) + lexerAction = lexerAction.action + requiresSeek = (startIndex + offset) != stopIndex + elif lexerAction.isPositionDependent: + input.seek(stopIndex) + requiresSeek = False + lexerAction.execute(lexer) + finally: + if requiresSeek: + input.seek(stopIndex) + + def __hash__(self): + return self.hashCode + + def __eq__(self, other): + if self is other: + return True + elif not isinstance(other, LexerActionExecutor): + return False + else: + return ( + self.hashCode == other.hashCode + and self.lexerActions == other.lexerActions + ) + + +del Lexer diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ParserATNSimulator.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ParserATNSimulator.py new file mode 100644 index 00000000..8f020397 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ParserATNSimulator.py @@ -0,0 +1,2201 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +# +# The embodiment of the adaptive LL(*), ALL(*), parsing strategy. +# +#

      +# The basic complexity of the adaptive strategy makes it harder to understand. +# We begin with ATN simulation to build paths in a DFA. Subsequent prediction +# requests go through the DFA first. If they reach a state without an edge for +# the current symbol, the algorithm fails over to the ATN simulation to +# complete the DFA path for the current input (until it finds a conflict state +# or uniquely predicting state).

      +# +#

      +# All of that is done without using the outer context because we want to create +# a DFA that is not dependent upon the rule invocation stack when we do a +# prediction. One DFA works in all contexts. We avoid using context not +# necessarily because it's slower, although it can be, but because of the DFA +# caching problem. The closure routine only considers the rule invocation stack +# created during prediction beginning in the decision rule. For example, if +# prediction occurs without invoking another rule's ATN, there are no context +# stacks in the configurations. When lack of context leads to a conflict, we +# don't know if it's an ambiguity or a weakness in the strong LL(*) parsing +# strategy (versus full LL(*)).

      +# +#

      +# When SLL yields a configuration set with conflict, we rewind the input and +# retry the ATN simulation, this time using full outer context without adding +# to the DFA. Configuration context stacks will be the full invocation stacks +# from the start rule. If we get a conflict using full context, then we can +# definitively say we have a true ambiguity for that input sequence. If we +# don't get a conflict, it implies that the decision is sensitive to the outer +# context. (It is not context-sensitive in the sense of context-sensitive +# grammars.)

      +# +#

      +# The next time we reach this DFA state with an SLL conflict, through DFA +# simulation, we will again retry the ATN simulation using full context mode. +# This is slow because we can't save the results and have to "interpret" the +# ATN each time we get that input.

      +# +#

      +# CACHING FULL CONTEXT PREDICTIONS

      +# +#

      +# We could cache results from full context to predicted alternative easily and +# that saves a lot of time but doesn't work in presence of predicates. The set +# of visible predicates from the ATN start state changes depending on the +# context, because closure can fall off the end of a rule. I tried to cache +# tuples (stack context, semantic context, predicted alt) but it was slower +# than interpreting and much more complicated. Also required a huge amount of +# memory. The goal is not to create the world's fastest parser anyway. I'd like +# to keep this algorithm simple. By launching multiple threads, we can improve +# the speed of parsing across a large number of files.

      +# +#

      +# There is no strict ordering between the amount of input used by SLL vs LL, +# which makes it really hard to build a cache for full context. Let's say that +# we have input A B C that leads to an SLL conflict with full context X. That +# implies that using X we might only use A B but we could also use A B C D to +# resolve conflict. Input A B C D could predict alternative 1 in one position +# in the input and A B C E could predict alternative 2 in another position in +# input. The conflicting SLL configurations could still be non-unique in the +# full context prediction, which would lead us to requiring more input than the +# original A B C. To make a prediction cache work, we have to track the exact +# input used during the previous prediction. That amounts to a cache that maps +# X to a specific DFA for that context.

      +# +#

      +# Something should be done for left-recursive expression predictions. They are +# likely LL(1) + pred eval. Easier to do the whole SLL unless error and retry +# with full LL thing Sam does.

      +# +#

      +# AVOIDING FULL CONTEXT PREDICTION

      +# +#

      +# We avoid doing full context retry when the outer context is empty, we did not +# dip into the outer context by falling off the end of the decision state rule, +# or when we force SLL mode.

      +# +#

      +# As an example of the not dip into outer context case, consider as super +# constructor calls versus function calls. One grammar might look like +# this:

      +# +#
      +# ctorBody
      +#   : '{' superCall? stat* '}'
      +#   ;
      +# 
      +# +#

      +# Or, you might see something like

      +# +#
      +# stat
      +#   : superCall ';'
      +#   | expression ';'
      +#   | ...
      +#   ;
      +# 
      +# +#

      +# In both cases I believe that no closure operations will dip into the outer +# context. In the first case ctorBody in the worst case will stop at the '}'. +# In the 2nd case it should stop at the ';'. Both cases should stay within the +# entry rule and not dip into the outer context.

      +# +#

      +# PREDICATES

      +# +#

      +# Predicates are always evaluated if present in either SLL or LL both. SLL and +# LL simulation deals with predicates differently. SLL collects predicates as +# it performs closure operations like ANTLR v3 did. It delays predicate +# evaluation until it reaches and accept state. This allows us to cache the SLL +# ATN simulation whereas, if we had evaluated predicates on-the-fly during +# closure, the DFA state configuration sets would be different and we couldn't +# build up a suitable DFA.

      +# +#

      +# When building a DFA accept state during ATN simulation, we evaluate any +# predicates and return the sole semantically valid alternative. If there is +# more than 1 alternative, we report an ambiguity. If there are 0 alternatives, +# we throw an exception. Alternatives without predicates act like they have +# true predicates. The simple way to think about it is to strip away all +# alternatives with false predicates and choose the minimum alternative that +# remains.

      +# +#

      +# When we start in the DFA and reach an accept state that's predicated, we test +# those and return the minimum semantically viable alternative. If no +# alternatives are viable, we throw an exception.

      +# +#

      +# During full LL ATN simulation, closure always evaluates predicates and +# on-the-fly. This is crucial to reducing the configuration set size during +# closure. It hits a landmine when parsing with the Java grammar, for example, +# without this on-the-fly evaluation.

      +# +#

      +# SHARING DFA

      +# +#

      +# All instances of the same parser share the same decision DFAs through a +# static field. Each instance gets its own ATN simulator but they share the +# same {@link #decisionToDFA} field. They also share a +# {@link PredictionContextCache} object that makes sure that all +# {@link PredictionContext} objects are shared among the DFA states. This makes +# a big size difference.

      +# +#

      +# THREAD SAFETY

      +# +#

      +# The {@link ParserATNSimulator} locks on the {@link #decisionToDFA} field when +# it adds a new DFA object to that array. {@link #addDFAEdge} +# locks on the DFA for the current decision when setting the +# {@link DFAState#edges} field. {@link #addDFAState} locks on +# the DFA for the current decision when looking up a DFA state to see if it +# already exists. We must make sure that all requests to add DFA states that +# are equivalent result in the same shared DFA object. This is because lots of +# threads will be trying to update the DFA at once. The +# {@link #addDFAState} method also locks inside the DFA lock +# but this time on the shared context cache when it rebuilds the +# configurations' {@link PredictionContext} objects using cached +# subgraphs/nodes. No other locking occurs, even during DFA simulation. This is +# safe as long as we can guarantee that all threads referencing +# {@code s.edge[t]} get the same physical target {@link DFAState}, or +# {@code null}. Once into the DFA, the DFA simulation does not reference the +# {@link DFA#states} map. It follows the {@link DFAState#edges} field to new +# targets. The DFA simulator will either find {@link DFAState#edges} to be +# {@code null}, to be non-{@code null} and {@code dfa.edges[t]} null, or +# {@code dfa.edges[t]} to be non-null. The +# {@link #addDFAEdge} method could be racing to set the field +# but in either case the DFA simulator works; if {@code null}, and requests ATN +# simulation. It could also race trying to get {@code dfa.edges[t]}, but either +# way it will work because it's not doing a test and set operation.

      +# +#

      +# Starting with SLL then failing to combined SLL/LL (Two-Stage +# Parsing)

      +# +#

      +# Sam pointed out that if SLL does not give a syntax error, then there is no +# point in doing full LL, which is slower. We only have to try LL if we get a +# syntax error. For maximum speed, Sam starts the parser set to pure SLL +# mode with the {@link BailErrorStrategy}:

      +# +#
      +# parser.{@link Parser#getInterpreter() getInterpreter()}.{@link #setPredictionMode setPredictionMode}{@code (}{@link PredictionMode#SLL}{@code )};
      +# parser.{@link Parser#setErrorHandler setErrorHandler}(new {@link BailErrorStrategy}());
      +# 
      +# +#

      +# If it does not get a syntax error, then we're done. If it does get a syntax +# error, we need to retry with the combined SLL/LL strategy.

      +# +#

      +# The reason this works is as follows. If there are no SLL conflicts, then the +# grammar is SLL (at least for that input set). If there is an SLL conflict, +# the full LL analysis must yield a set of viable alternatives which is a +# subset of the alternatives reported by SLL. If the LL set is a singleton, +# then the grammar is LL but not SLL. If the LL set is the same size as the SLL +# set, the decision is SLL. If the LL set has size > 1, then that decision +# is truly ambiguous on the current input. If the LL set is smaller, then the +# SLL conflict resolution might choose an alternative that the full LL would +# rule out as a possibility based upon better context information. If that's +# the case, then the SLL parse will definitely get an error because the full LL +# analysis says it's not viable. If SLL conflict resolution chooses an +# alternative within the LL set, them both SLL and LL would choose the same +# alternative because they both choose the minimum of multiple conflicting +# alternatives.

      +# +#

      +# Let's say we have a set of SLL conflicting alternatives {@code {1, 2, 3}} and +# a smaller LL set called s. If s is {@code {2, 3}}, then SLL +# parsing will get an error because SLL will pursue alternative 1. If +# s is {@code {1, 2}} or {@code {1, 3}} then both SLL and LL will +# choose the same alternative because alternative one is the minimum of either +# set. If s is {@code {2}} or {@code {3}} then SLL will get a syntax +# error. If s is {@code {1}} then SLL will succeed.

      +# +#

      +# Of course, if the input is invalid, then we will get an error for sure in +# both SLL and LL parsing. Erroneous input will therefore require 2 passes over +# the input.

      +# +import sys + +from cf_units._udunits2_parser.parser._antlr4_runtime import DFA +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfig import ( + ATNConfig, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfigSet import ( + ATNConfigSet, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNSimulator import ( + ATNSimulator, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( + ATNState, + DecisionState, + RuleStopState, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.PredictionMode import ( + PredictionMode, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.SemanticContext import ( + SemanticContext, + andContext, + orContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.Transition import ( + ActionTransition, + AtomTransition, + NotSetTransition, + PrecedencePredicateTransition, + PredicateTransition, + RuleTransition, + SetTransition, + Transition, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.BufferedTokenStream import ( + TokenStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFAState import ( + DFAState, + PredPrediction, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + NoViableAltException, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Parser import Parser +from cf_units._udunits2_parser.parser._antlr4_runtime.ParserRuleContext import ( + ParserRuleContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( + PredictionContext, + PredictionContextCache, + PredictionContextFromRuleContext, + SingletonPredictionContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( + RuleContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser.parser._antlr4_runtime.Utils import str_list + + +class ParserATNSimulator(ATNSimulator): + __slots__ = ( + "parser", + "decisionToDFA", + "predictionMode", + "_input", + "_startIndex", + "_outerContext", + "_dfa", + "mergeCache", + ) + + debug = False + trace_atn_sim = False + dfa_debug = False + retry_debug = False + + def __init__( + self, + parser: Parser, + atn: ATN, + decisionToDFA: list, + sharedContextCache: PredictionContextCache, + ): + super().__init__(atn, sharedContextCache) + self.parser = parser + self.decisionToDFA = decisionToDFA + # SLL, LL, or LL + exact ambig detection?# + self.predictionMode = PredictionMode.LL + # LAME globals to avoid parameters!!!!! I need these down deep in predTransition + self._input = None + self._startIndex = 0 + self._outerContext = None + self._dfa = None + # Each prediction operation uses a cache for merge of prediction contexts. + # Don't keep around as it wastes huge amounts of memory. DoubleKeyMap + # isn't synchronized but we're ok since two threads shouldn't reuse same + # parser/atnsim object because it can only handle one input at a time. + # This maps graphs a and b to merged result c. (a,b)→c. We can avoid + # the merge if we ever see a and b again. Note that (b,a)→c should + # also be examined during cache lookup. + # + self.mergeCache = None + + def reset(self): + pass + + def adaptivePredict( + self, + input: TokenStream, + decision: int, + outerContext: ParserRuleContext, + ): + if ParserATNSimulator.debug or ParserATNSimulator.trace_atn_sim: + print( + "adaptivePredict decision " + + str(decision) + + " exec LA(1)==" + + self.getLookaheadName(input) + + " line " + + str(input.LT(1).line) + + ":" + + str(input.LT(1).column) + ) + self._input = input + self._startIndex = input.index + self._outerContext = outerContext + + dfa = self.decisionToDFA[decision] + self._dfa = dfa + m = input.mark() + index = input.index + + # Now we are certain to have a specific decision's DFA + # But, do we still need an initial state? + try: + if dfa.precedenceDfa: + # the start state for a precedence DFA depends on the current + # parser precedence, and is provided by a DFA method. + s0 = dfa.getPrecedenceStartState(self.parser.getPrecedence()) + else: + # the start state for a "regular" DFA is just s0 + s0 = dfa.s0 + + if s0 is None: + if outerContext is None: + outerContext = ParserRuleContext.EMPTY + if ParserATNSimulator.debug: + print( + "predictATN decision " + + str(dfa.decision) + + " exec LA(1)==" + + self.getLookaheadName(input) + + ", outerContext=" + + str(outerContext) + ) # outerContext.toString(self.parser.literalNames, None)) + + fullCtx = False + s0_closure = self.computeStartState( + dfa.atnStartState, ParserRuleContext.EMPTY, fullCtx + ) + + if dfa.precedenceDfa: + # If this is a precedence DFA, we use applyPrecedenceFilter + # to convert the computed start state to a precedence start + # state. We then use DFA.setPrecedenceStartState to set the + # appropriate start state for the precedence level rather + # than simply setting DFA.s0. + # + dfa.s0.configs = s0_closure # not used for prediction but useful to know start configs anyway + s0_closure = self.applyPrecedenceFilter(s0_closure) + s0 = self.addDFAState(dfa, DFAState(configs=s0_closure)) + dfa.setPrecedenceStartState( + self.parser.getPrecedence(), s0 + ) + else: + s0 = self.addDFAState(dfa, DFAState(configs=s0_closure)) + dfa.s0 = s0 + + alt = self.execATN(dfa, s0, input, index, outerContext) + if ParserATNSimulator.debug: + print( + "DFA after predictATN: " + + dfa.toString(self.parser.literalNames) + ) + return alt + finally: + self._dfa = None + self.mergeCache = None # wack cache after each prediction + input.seek(index) + input.release(m) + + # Performs ATN simulation to compute a predicted alternative based + # upon the remaining input, but also updates the DFA cache to avoid + # having to traverse the ATN again for the same input sequence. + + # There are some key conditions we're looking for after computing a new + # set of ATN configs (proposed DFA state): + # if the set is empty, there is no viable alternative for current symbol + # does the state uniquely predict an alternative? + # does the state have a conflict that would prevent us from + # putting it on the work list? + + # We also have some key operations to do: + # add an edge from previous DFA state to potentially new DFA state, D, + # upon current symbol but only if adding to work list, which means in all + # cases except no viable alternative (and possibly non-greedy decisions?) + # collecting predicates and adding semantic context to DFA accept states + # adding rule context to context-sensitive DFA accept states + # consuming an input symbol + # reporting a conflict + # reporting an ambiguity + # reporting a context sensitivity + # reporting insufficient predicates + + # cover these cases: + # dead end + # single alt + # single alt + preds + # conflict + # conflict + preds + # + def execATN( + self, + dfa: DFA, + s0: DFAState, + input: TokenStream, + startIndex: int, + outerContext: ParserRuleContext, + ): + if ParserATNSimulator.debug or ParserATNSimulator.trace_atn_sim: + print( + "execATN decision " + + str(dfa.decision) + + ", DFA state " + + str(s0) + + ", LA(1)==" + + self.getLookaheadName(input) + + " line " + + str(input.LT(1).line) + + ":" + + str(input.LT(1).column) + ) + + previousD = s0 + + t = input.LA(1) + + while True: # while more work + D = self.getExistingTargetState(previousD, t) + if D is None: + D = self.computeTargetState(dfa, previousD, t) + if D is self.ERROR: + # if any configs in previous dipped into outer context, that + # means that input up to t actually finished entry rule + # at least for SLL decision. Full LL doesn't dip into outer + # so don't need special case. + # We will get an error no matter what so delay until after + # decision; better error message. Also, no reachable target + # ATN states in SLL implies LL will also get nowhere. + # If conflict in states that dip out, choose min since we + # will get error no matter what. + e = self.noViableAlt( + input, outerContext, previousD.configs, startIndex + ) + input.seek(startIndex) + alt = self.getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule( + previousD.configs, outerContext + ) + if alt != ATN.INVALID_ALT_NUMBER: + return alt + raise e + + if ( + D.requiresFullContext + and self.predictionMode != PredictionMode.SLL + ): + # IF PREDS, MIGHT RESOLVE TO SINGLE ALT => SLL (or syntax error) + conflictingAlts = D.configs.conflictingAlts + if D.predicates is not None: + if ParserATNSimulator.debug: + print("DFA state has preds in DFA sim LL failover") + conflictIndex = input.index + if conflictIndex != startIndex: + input.seek(startIndex) + + conflictingAlts = self.evalSemanticContext( + D.predicates, outerContext, True + ) + if len(conflictingAlts) == 1: + if ParserATNSimulator.debug: + print("Full LL avoided") + return min(conflictingAlts) + + if conflictIndex != startIndex: + # restore the index so reporting the fallback to full + # context occurs with the index at the correct spot + input.seek(conflictIndex) + + if ParserATNSimulator.dfa_debug: + print( + "ctx sensitive state " + + str(outerContext) + + " in " + + str(D) + ) + fullCtx = True + s0_closure = self.computeStartState( + dfa.atnStartState, outerContext, fullCtx + ) + self.reportAttemptingFullContext( + dfa, conflictingAlts, D.configs, startIndex, input.index + ) + alt = self.execATNWithFullContext( + dfa, D, s0_closure, input, startIndex, outerContext + ) + return alt + + if D.isAcceptState: + if D.predicates is None: + return D.prediction + + stopIndex = input.index + input.seek(startIndex) + alts = self.evalSemanticContext( + D.predicates, outerContext, True + ) + if len(alts) == 0: + raise self.noViableAlt( + input, outerContext, D.configs, startIndex + ) + elif len(alts) == 1: + return min(alts) + else: + # report ambiguity after predicate evaluation to make sure the correct + # set of ambig alts is reported. + self.reportAmbiguity( + dfa, D, startIndex, stopIndex, False, alts, D.configs + ) + return min(alts) + + previousD = D + + if t != Token.EOF: + input.consume() + t = input.LA(1) + + # + # Get an existing target state for an edge in the DFA. If the target state + # for the edge has not yet been computed or is otherwise not available, + # this method returns {@code null}. + # + # @param previousD The current DFA state + # @param t The next input symbol + # @return The existing target DFA state for the given input symbol + # {@code t}, or {@code null} if the target state for this edge is not + # already cached + # + def getExistingTargetState(self, previousD: DFAState, t: int): + edges = previousD.edges + if edges is None or t + 1 < 0 or t + 1 >= len(edges): + return None + else: + return edges[t + 1] + + # + # Compute a target state for an edge in the DFA, and attempt to add the + # computed state and corresponding edge to the DFA. + # + # @param dfa The DFA + # @param previousD The current DFA state + # @param t The next input symbol + # + # @return The computed target DFA state for the given input symbol + # {@code t}. If {@code t} does not lead to a valid DFA state, this method + # returns {@link #ERROR}. + # + def computeTargetState(self, dfa: DFA, previousD: DFAState, t: int): + reach = self.computeReachSet(previousD.configs, t, False) + if reach is None: + self.addDFAEdge(dfa, previousD, t, self.ERROR) + return self.ERROR + + # create new target state; we'll add to DFA after it's complete + D = DFAState(configs=reach) + + predictedAlt = self.getUniqueAlt(reach) + + if ParserATNSimulator.debug: + altSubSets = PredictionMode.getConflictingAltSubsets(reach) + print( + "SLL altSubSets=" + + str(altSubSets) + + ", configs=" + + str(reach) + + ", predict=" + + str(predictedAlt) + + ", allSubsetsConflict=" + + str(PredictionMode.allSubsetsConflict(altSubSets)) + + ", conflictingAlts=" + + str(self.getConflictingAlts(reach)) + ) + + if predictedAlt != ATN.INVALID_ALT_NUMBER: + # NO CONFLICT, UNIQUELY PREDICTED ALT + D.isAcceptState = True + D.configs.uniqueAlt = predictedAlt + D.prediction = predictedAlt + elif PredictionMode.hasSLLConflictTerminatingPrediction( + self.predictionMode, reach + ): + # MORE THAN ONE VIABLE ALTERNATIVE + D.configs.conflictingAlts = self.getConflictingAlts(reach) + D.requiresFullContext = True + # in SLL-only mode, we will stop at this state and return the minimum alt + D.isAcceptState = True + D.prediction = min(D.configs.conflictingAlts) + + if D.isAcceptState and D.configs.hasSemanticContext: + self.predicateDFAState(D, self.atn.getDecisionState(dfa.decision)) + if D.predicates is not None: + D.prediction = ATN.INVALID_ALT_NUMBER + + # all adds to dfa are done after we've created full D state + D = self.addDFAEdge(dfa, previousD, t, D) + return D + + def predicateDFAState( + self, dfaState: DFAState, decisionState: DecisionState + ): + # We need to test all predicates, even in DFA states that + # uniquely predict alternative. + nalts = len(decisionState.transitions) + # Update DFA so reach becomes accept state with (predicate,alt) + # pairs if preds found for conflicting alts + altsToCollectPredsFrom = self.getConflictingAltsOrUniqueAlt( + dfaState.configs + ) + altToPred = self.getPredsForAmbigAlts( + altsToCollectPredsFrom, dfaState.configs, nalts + ) + if altToPred is not None: + dfaState.predicates = self.getPredicatePredictions( + altsToCollectPredsFrom, altToPred + ) + dfaState.prediction = ( + ATN.INVALID_ALT_NUMBER + ) # make sure we use preds + else: + # There are preds in configs but they might go away + # when OR'd together like {p}? || NONE == NONE. If neither + # alt has preds, resolve to min alt + dfaState.prediction = min(altsToCollectPredsFrom) + + # comes back with reach.uniqueAlt set to a valid alt + def execATNWithFullContext( + self, + dfa: DFA, + D: DFAState, # how far we got before failing over + s0: ATNConfigSet, + input: TokenStream, + startIndex: int, + outerContext: ParserRuleContext, + ): + if ParserATNSimulator.debug or ParserATNSimulator.trace_atn_sim: + print("execATNWithFullContext", str(s0)) + fullCtx = True + foundExactAmbig = False + reach = None + previous = s0 + input.seek(startIndex) + t = input.LA(1) + predictedAlt = -1 + while True: # while more work + reach = self.computeReachSet(previous, t, fullCtx) + if reach is None: + # if any configs in previous dipped into outer context, that + # means that input up to t actually finished entry rule + # at least for LL decision. Full LL doesn't dip into outer + # so don't need special case. + # We will get an error no matter what so delay until after + # decision; better error message. Also, no reachable target + # ATN states in SLL implies LL will also get nowhere. + # If conflict in states that dip out, choose min since we + # will get error no matter what. + e = self.noViableAlt(input, outerContext, previous, startIndex) + input.seek(startIndex) + alt = self.getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule( + previous, outerContext + ) + if alt != ATN.INVALID_ALT_NUMBER: + return alt + else: + raise e + + altSubSets = PredictionMode.getConflictingAltSubsets(reach) + if ParserATNSimulator.debug: + print( + "LL altSubSets=" + + str(altSubSets) + + ", predict=" + + str(PredictionMode.getUniqueAlt(altSubSets)) + + ", resolvesToJustOneViableAlt=" + + str( + PredictionMode.resolvesToJustOneViableAlt(altSubSets) + ) + ) + + reach.uniqueAlt = self.getUniqueAlt(reach) + # unique prediction? + if reach.uniqueAlt != ATN.INVALID_ALT_NUMBER: + predictedAlt = reach.uniqueAlt + break + elif ( + self.predictionMode + is not PredictionMode.LL_EXACT_AMBIG_DETECTION + ): + predictedAlt = PredictionMode.resolvesToJustOneViableAlt( + altSubSets + ) + if predictedAlt != ATN.INVALID_ALT_NUMBER: + break + else: + # In exact ambiguity mode, we never try to terminate early. + # Just keeps scarfing until we know what the conflict is + if PredictionMode.allSubsetsConflict( + altSubSets + ) and PredictionMode.allSubsetsEqual(altSubSets): + foundExactAmbig = True + predictedAlt = PredictionMode.getSingleViableAlt( + altSubSets + ) + break + # else there are multiple non-conflicting subsets or + # we're not sure what the ambiguity is yet. + # So, keep going. + + previous = reach + if t != Token.EOF: + input.consume() + t = input.LA(1) + + # If the configuration set uniquely predicts an alternative, + # without conflict, then we know that it's a full LL decision + # not SLL. + if reach.uniqueAlt != ATN.INVALID_ALT_NUMBER: + self.reportContextSensitivity( + dfa, predictedAlt, reach, startIndex, input.index + ) + return predictedAlt + + # We do not check predicates here because we have checked them + # on-the-fly when doing full context prediction. + + # + # In non-exact ambiguity detection mode, we might actually be able to + # detect an exact ambiguity, but I'm not going to spend the cycles + # needed to check. We only emit ambiguity warnings in exact ambiguity + # mode. + # + # For example, we might know that we have conflicting configurations. + # But, that does not mean that there is no way forward without a + # conflict. It's possible to have nonconflicting alt subsets as in: + + # altSubSets=[{1, 2}, {1, 2}, {1}, {1, 2}] + + # from + # + # [(17,1,[5 $]), (13,1,[5 10 $]), (21,1,[5 10 $]), (11,1,[$]), + # (13,2,[5 10 $]), (21,2,[5 10 $]), (11,2,[$])] + # + # In this case, (17,1,[5 $]) indicates there is some next sequence that + # would resolve this without conflict to alternative 1. Any other viable + # next sequence, however, is associated with a conflict. We stop + # looking for input because no amount of further lookahead will alter + # the fact that we should predict alternative 1. We just can't say for + # sure that there is an ambiguity without looking further. + + self.reportAmbiguity( + dfa, D, startIndex, input.index, foundExactAmbig, None, reach + ) + + return predictedAlt + + def computeReachSet(self, closure: ATNConfigSet, t: int, fullCtx: bool): + if ParserATNSimulator.debug: + print("in computeReachSet, starting closure: " + str(closure)) + + if self.mergeCache is None: + self.mergeCache = dict() + + intermediate = ATNConfigSet(fullCtx) + + # Configurations already in a rule stop state indicate reaching the end + # of the decision rule (local context) or end of the start rule (full + # context). Once reached, these configurations are never updated by a + # closure operation, so they are handled separately for the performance + # advantage of having a smaller intermediate set when calling closure. + # + # For full-context reach operations, separate handling is required to + # ensure that the alternative matching the longest overall sequence is + # chosen when multiple such configurations can match the input. + + skippedStopStates = None + + # First figure out where we can reach on input t + for c in closure: + if ParserATNSimulator.debug: + print("testing " + self.getTokenName(t) + " at " + str(c)) + + if isinstance(c.state, RuleStopState): + if fullCtx or t == Token.EOF: + if skippedStopStates is None: + skippedStopStates = list() + skippedStopStates.append(c) + continue + + for trans in c.state.transitions: + target = self.getReachableTarget(trans, t) + if target is not None: + intermediate.add( + ATNConfig(state=target, config=c), self.mergeCache + ) + + # Now figure out where the reach operation can take us... + + reach = None + + # This block optimizes the reach operation for intermediate sets which + # trivially indicate a termination state for the overall + # adaptivePredict operation. + # + # The conditions assume that intermediate + # contains all configurations relevant to the reach set, but this + # condition is not true when one or more configurations have been + # withheld in skippedStopStates, or when the current symbol is EOF. + # + if skippedStopStates is None and t != Token.EOF: + if len(intermediate) == 1: + # Don't pursue the closure if there is just one state. + # It can only have one alternative; just add to result + # Also don't pursue the closure if there is unique alternative + # among the configurations. + reach = intermediate + elif self.getUniqueAlt(intermediate) != ATN.INVALID_ALT_NUMBER: + # Also don't pursue the closure if there is unique alternative + # among the configurations. + reach = intermediate + + # If the reach set could not be trivially determined, perform a closure + # operation on the intermediate set to compute its initial value. + # + if reach is None: + reach = ATNConfigSet(fullCtx) + closureBusy = set() + treatEofAsEpsilon = t == Token.EOF + for c in intermediate: + self.closure( + c, reach, closureBusy, False, fullCtx, treatEofAsEpsilon + ) + + if t == Token.EOF: + # After consuming EOF no additional input is possible, so we are + # only interested in configurations which reached the end of the + # decision rule (local context) or end of the start rule (full + # context). Update reach to contain only these configurations. This + # handles both explicit EOF transitions in the grammar and implicit + # EOF transitions following the end of the decision or start rule. + # + # When reach==intermediate, no closure operation was performed. In + # this case, removeAllConfigsNotInRuleStopState needs to check for + # reachable rule stop states as well as configurations already in + # a rule stop state. + # + # This is handled before the configurations in skippedStopStates, + # because any configurations potentially added from that list are + # already guaranteed to meet this condition whether or not it's + # required. + # + reach = self.removeAllConfigsNotInRuleStopState( + reach, reach is intermediate + ) + + # If skippedStopStates is not null, then it contains at least one + # configuration. For full-context reach operations, these + # configurations reached the end of the start rule, in which case we + # only add them back to reach if no configuration during the current + # closure operation reached such a state. This ensures adaptivePredict + # chooses an alternative matching the longest overall sequence when + # multiple alternatives are viable. + # + if skippedStopStates is not None and ( + (not fullCtx) + or (not PredictionMode.hasConfigInRuleStopState(reach)) + ): + for c in skippedStopStates: + reach.add(c, self.mergeCache) + + if ParserATNSimulator.trace_atn_sim: + print("computeReachSet", str(closure), "->", reach) + + if len(reach) == 0: + return None + else: + return reach + + # + # Return a configuration set containing only the configurations from + # {@code configs} which are in a {@link RuleStopState}. If all + # configurations in {@code configs} are already in a rule stop state, this + # method simply returns {@code configs}. + # + #

      When {@code lookToEndOfRule} is true, this method uses + # {@link ATN#nextTokens} for each configuration in {@code configs} which is + # not already in a rule stop state to see if a rule stop state is reachable + # from the configuration via epsilon-only transitions.

      + # + # @param configs the configuration set to update + # @param lookToEndOfRule when true, this method checks for rule stop states + # reachable by epsilon-only transitions from each configuration in + # {@code configs}. + # + # @return {@code configs} if all configurations in {@code configs} are in a + # rule stop state, otherwise return a new configuration set containing only + # the configurations from {@code configs} which are in a rule stop state + # + def removeAllConfigsNotInRuleStopState( + self, configs: ATNConfigSet, lookToEndOfRule: bool + ): + if PredictionMode.allConfigsInRuleStopStates(configs): + return configs + result = ATNConfigSet(configs.fullCtx) + for config in configs: + if isinstance(config.state, RuleStopState): + result.add(config, self.mergeCache) + continue + if lookToEndOfRule and config.state.epsilonOnlyTransitions: + nextTokens = self.atn.nextTokens(config.state) + if Token.EPSILON in nextTokens: + endOfRuleState = self.atn.ruleToStopState[ + config.state.ruleIndex + ] + result.add( + ATNConfig(state=endOfRuleState, config=config), + self.mergeCache, + ) + return result + + def computeStartState(self, p: ATNState, ctx: RuleContext, fullCtx: bool): + # always at least the implicit call to start rule + initialContext = PredictionContextFromRuleContext(self.atn, ctx) + configs = ATNConfigSet(fullCtx) + + if ParserATNSimulator.trace_atn_sim: + print( + "computeStartState from ATN state " + + str(p) + + " initialContext=" + + str(initialContext) + ) + + for i in range(0, len(p.transitions)): + target = p.transitions[i].target + c = ATNConfig(target, i + 1, initialContext) + closureBusy = set() + self.closure(c, configs, closureBusy, True, fullCtx, False) + return configs + + # + # This method transforms the start state computed by + # {@link #computeStartState} to the special start state used by a + # precedence DFA for a particular precedence value. The transformation + # process applies the following changes to the start state's configuration + # set. + # + #
        + #
      1. Evaluate the precedence predicates for each configuration using + # {@link SemanticContext#evalPrecedence}.
      2. + #
      3. Remove all configurations which predict an alternative greater than + # 1, for which another configuration that predicts alternative 1 is in the + # same ATN state with the same prediction context. This transformation is + # valid for the following reasons: + #
          + #
        • The closure block cannot contain any epsilon transitions which bypass + # the body of the closure, so all states reachable via alternative 1 are + # part of the precedence alternatives of the transformed left-recursive + # rule.
        • + #
        • The "primary" portion of a left recursive rule cannot contain an + # epsilon transition, so the only way an alternative other than 1 can exist + # in a state that is also reachable via alternative 1 is by nesting calls + # to the left-recursive rule, with the outer calls not being at the + # preferred precedence level.
        • + #
        + #
      4. + #
      + # + #

      + # The prediction context must be considered by this filter to address + # situations like the following. + #

      + # + #
      +    # grammar TA;
      +    # prog: statement* EOF;
      +    # statement: letterA | statement letterA 'b' ;
      +    # letterA: 'a';
      +    # 
      + #
      + #

      + # If the above grammar, the ATN state immediately before the token + # reference {@code 'a'} in {@code letterA} is reachable from the left edge + # of both the primary and closure blocks of the left-recursive rule + # {@code statement}. The prediction context associated with each of these + # configurations distinguishes between them, and prevents the alternative + # which stepped out to {@code prog} (and then back in to {@code statement} + # from being eliminated by the filter. + #

      + # + # @param configs The configuration set computed by + # {@link #computeStartState} as the start state for the DFA. + # @return The transformed configuration set representing the start state + # for a precedence DFA at a particular precedence level (determined by + # calling {@link Parser#getPrecedence}). + # + def applyPrecedenceFilter(self, configs: ATNConfigSet): + statesFromAlt1 = dict() + configSet = ATNConfigSet(configs.fullCtx) + for config in configs: + # handle alt 1 first + if config.alt != 1: + continue + updatedContext = config.semanticContext.evalPrecedence( + self.parser, self._outerContext + ) + if updatedContext is None: + # the configuration was eliminated + continue + + statesFromAlt1[config.state.stateNumber] = config.context + if updatedContext is not config.semanticContext: + configSet.add( + ATNConfig(config=config, semantic=updatedContext), + self.mergeCache, + ) + else: + configSet.add(config, self.mergeCache) + + for config in configs: + if config.alt == 1: + # already handled + continue + + # In the future, this elimination step could be updated to also + # filter the prediction context for alternatives predicting alt>1 + # (basically a graph subtraction algorithm). + # + if not config.precedenceFilterSuppressed: + context = statesFromAlt1.get(config.state.stateNumber, None) + if context == config.context: + # eliminated + continue + + configSet.add(config, self.mergeCache) + + return configSet + + def getReachableTarget(self, trans: Transition, ttype: int): + if trans.matches(ttype, 0, self.atn.maxTokenType): + return trans.target + else: + return None + + def getPredsForAmbigAlts( + self, ambigAlts: set, configs: ATNConfigSet, nalts: int + ): + # REACH=[1|1|[]|0:0, 1|2|[]|0:1] + # altToPred starts as an array of all null contexts. The entry at index i + # corresponds to alternative i. altToPred[i] may have one of three values: + # 1. null: no ATNConfig c is found such that c.alt==i + # 2. SemanticContext.NONE: At least one ATNConfig c exists such that + # c.alt==i and c.semanticContext==SemanticContext.NONE. In other words, + # alt i has at least one unpredicated config. + # 3. Non-NONE Semantic Context: There exists at least one, and for all + # ATNConfig c such that c.alt==i, c.semanticContext!=SemanticContext.NONE. + # + # From this, it is clear that NONE||anything==NONE. + # + altToPred = [None] * (nalts + 1) + for c in configs: + if c.alt in ambigAlts: + altToPred[c.alt] = orContext( + altToPred[c.alt], c.semanticContext + ) + + nPredAlts = 0 + for i in range(1, nalts + 1): + if altToPred[i] is None: + altToPred[i] = SemanticContext.NONE + elif altToPred[i] is not SemanticContext.NONE: + nPredAlts += 1 + + # nonambig alts are null in altToPred + if nPredAlts == 0: + altToPred = None + if ParserATNSimulator.debug: + print("getPredsForAmbigAlts result " + str_list(altToPred)) + return altToPred + + def getPredicatePredictions(self, ambigAlts: set, altToPred: list): + pairs = [] + containsPredicate = False + for i in range(1, len(altToPred)): + pred = altToPred[i] + # unpredicated is indicated by SemanticContext.NONE + if ambigAlts is not None and i in ambigAlts: + pairs.append(PredPrediction(pred, i)) + if pred is not SemanticContext.NONE: + containsPredicate = True + + if not containsPredicate: + return None + + return pairs + + # + # This method is used to improve the localization of error messages by + # choosing an alternative rather than throwing a + # {@link NoViableAltException} in particular prediction scenarios where the + # {@link #ERROR} state was reached during ATN simulation. + # + #

      + # The default implementation of this method uses the following + # algorithm to identify an ATN configuration which successfully parsed the + # decision entry rule. Choosing such an alternative ensures that the + # {@link ParserRuleContext} returned by the calling rule will be complete + # and valid, and the syntax error will be reported later at a more + # localized location.

      + # + #
        + #
      • If a syntactically valid path or paths reach the end of the decision rule and + # they are semantically valid if predicated, return the min associated alt.
      • + #
      • Else, if a semantically invalid but syntactically valid path exist + # or paths exist, return the minimum associated alt. + #
      • + #
      • Otherwise, return {@link ATN#INVALID_ALT_NUMBER}.
      • + #
      + # + #

      + # In some scenarios, the algorithm described above could predict an + # alternative which will result in a {@link FailedPredicateException} in + # the parser. Specifically, this could occur if the only configuration + # capable of successfully parsing to the end of the decision rule is + # blocked by a semantic predicate. By choosing this alternative within + # {@link #adaptivePredict} instead of throwing a + # {@link NoViableAltException}, the resulting + # {@link FailedPredicateException} in the parser will identify the specific + # predicate which is preventing the parser from successfully parsing the + # decision rule, which helps developers identify and correct logic errors + # in semantic predicates. + #

      + # + # @param configs The ATN configurations which were valid immediately before + # the {@link #ERROR} state was reached + # @param outerContext The is the \gamma_0 initial parser context from the paper + # or the parser stack at the instant before prediction commences. + # + # @return The value to return from {@link #adaptivePredict}, or + # {@link ATN#INVALID_ALT_NUMBER} if a suitable alternative was not + # identified and {@link #adaptivePredict} should report an error instead. + # + def getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule( + self, configs: ATNConfigSet, outerContext: ParserRuleContext + ): + semValidConfigs, semInvalidConfigs = ( + self.splitAccordingToSemanticValidity(configs, outerContext) + ) + alt = self.getAltThatFinishedDecisionEntryRule(semValidConfigs) + if ( + alt != ATN.INVALID_ALT_NUMBER + ): # semantically/syntactically viable path exists + return alt + # Is there a syntactically valid path with a failed pred? + if len(semInvalidConfigs) > 0: + alt = self.getAltThatFinishedDecisionEntryRule(semInvalidConfigs) + if ( + alt != ATN.INVALID_ALT_NUMBER + ): # syntactically viable path exists + return alt + return ATN.INVALID_ALT_NUMBER + + def getAltThatFinishedDecisionEntryRule(self, configs: ATNConfigSet): + alts = set() + for c in configs: + if c.reachesIntoOuterContext > 0 or ( + isinstance(c.state, RuleStopState) and c.context.hasEmptyPath() + ): + alts.add(c.alt) + if len(alts) == 0: + return ATN.INVALID_ALT_NUMBER + else: + return min(alts) + + # Walk the list of configurations and split them according to + # those that have preds evaluating to true/false. If no pred, assume + # true pred and include in succeeded set. Returns Pair of sets. + # + # Create a new set so as not to alter the incoming parameter. + # + # Assumption: the input stream has been restored to the starting point + # prediction, which is where predicates need to evaluate. + # + def splitAccordingToSemanticValidity( + self, configs: ATNConfigSet, outerContext: ParserRuleContext + ): + succeeded = ATNConfigSet(configs.fullCtx) + failed = ATNConfigSet(configs.fullCtx) + for c in configs: + if c.semanticContext is not SemanticContext.NONE: + predicateEvaluationResult = c.semanticContext.eval( + self.parser, outerContext + ) + if predicateEvaluationResult: + succeeded.add(c) + else: + failed.add(c) + else: + succeeded.add(c) + return (succeeded, failed) + + # Look through a list of predicate/alt pairs, returning alts for the + # pairs that win. A {@code NONE} predicate indicates an alt containing an + # unpredicated config which behaves as "always true." If !complete + # then we stop at the first predicate that evaluates to true. This + # includes pairs with null predicates. + # + def evalSemanticContext( + self, + predPredictions: list, + outerContext: ParserRuleContext, + complete: bool, + ): + predictions = set() + for pair in predPredictions: + if pair.pred is SemanticContext.NONE: + predictions.add(pair.alt) + if not complete: + break + continue + predicateEvaluationResult = pair.pred.eval( + self.parser, outerContext + ) + if ParserATNSimulator.debug or ParserATNSimulator.dfa_debug: + print( + "eval pred " + + str(pair) + + "=" + + str(predicateEvaluationResult) + ) + + if predicateEvaluationResult: + if ParserATNSimulator.debug or ParserATNSimulator.dfa_debug: + print("PREDICT " + str(pair.alt)) + predictions.add(pair.alt) + if not complete: + break + return predictions + + # TODO: If we are doing predicates, there is no point in pursuing + # closure operations if we reach a DFA state that uniquely predicts + # alternative. We will not be caching that DFA state and it is a + # waste to pursue the closure. Might have to advance when we do + # ambig detection thought :( + # + + def closure( + self, + config: ATNConfig, + configs: ATNConfigSet, + closureBusy: set, + collectPredicates: bool, + fullCtx: bool, + treatEofAsEpsilon: bool, + ): + initialDepth = 0 + self.closureCheckingStopState( + config, + configs, + closureBusy, + collectPredicates, + fullCtx, + initialDepth, + treatEofAsEpsilon, + ) + + def closureCheckingStopState( + self, + config: ATNConfig, + configs: ATNConfigSet, + closureBusy: set, + collectPredicates: bool, + fullCtx: bool, + depth: int, + treatEofAsEpsilon: bool, + ): + if ParserATNSimulator.trace_atn_sim: + print("closure(" + str(config) + ")") + + if isinstance(config.state, RuleStopState): + # We hit rule end. If we have context info, use it + # run thru all possible stack tops in ctx + if not config.context.isEmpty(): + for i in range(0, len(config.context)): + state = config.context.getReturnState(i) + if state is PredictionContext.EMPTY_RETURN_STATE: + if fullCtx: + configs.add( + ATNConfig( + state=config.state, + context=PredictionContext.EMPTY, + config=config, + ), + self.mergeCache, + ) + continue + else: + # we have no context info, just chase follow links (if greedy) + if ParserATNSimulator.debug: + print( + "FALLING off rule " + + self.getRuleName(config.state.ruleIndex) + ) + self.closure_( + config, + configs, + closureBusy, + collectPredicates, + fullCtx, + depth, + treatEofAsEpsilon, + ) + continue + returnState = self.atn.states[state] + newContext = config.context.getParent( + i + ) # "pop" return state + c = ATNConfig( + state=returnState, + alt=config.alt, + context=newContext, + semantic=config.semanticContext, + ) + # While we have context to pop back from, we may have + # gotten that context AFTER having falling off a rule. + # Make sure we track that we are now out of context. + c.reachesIntoOuterContext = config.reachesIntoOuterContext + self.closureCheckingStopState( + c, + configs, + closureBusy, + collectPredicates, + fullCtx, + depth - 1, + treatEofAsEpsilon, + ) + return + elif fullCtx: + # reached end of start rule + configs.add(config, self.mergeCache) + return + else: + # else if we have no context info, just chase follow links (if greedy) + if ParserATNSimulator.debug: + print( + "FALLING off rule " + + self.getRuleName(config.state.ruleIndex) + ) + + self.closure_( + config, + configs, + closureBusy, + collectPredicates, + fullCtx, + depth, + treatEofAsEpsilon, + ) + + # Do the actual work of walking epsilon edges# + def closure_( + self, + config: ATNConfig, + configs: ATNConfigSet, + closureBusy: set, + collectPredicates: bool, + fullCtx: bool, + depth: int, + treatEofAsEpsilon: bool, + ): + p = config.state + # optimization + if not p.epsilonOnlyTransitions: + configs.add(config, self.mergeCache) + # make sure to not return here, because EOF transitions can act as + # both epsilon transitions and non-epsilon transitions. + + first = True + for t in p.transitions: + if first: + first = False + if self.canDropLoopEntryEdgeInLeftRecursiveRule(config): + continue + + continueCollecting = collectPredicates and not isinstance( + t, ActionTransition + ) + c = self.getEpsilonTarget( + config, + t, + continueCollecting, + depth == 0, + fullCtx, + treatEofAsEpsilon, + ) + if c is not None: + newDepth = depth + if isinstance(config.state, RuleStopState): + # target fell off end of rule; mark resulting c as having dipped into outer context + # We can't get here if incoming config was rule stop and we had context + # track how far we dip into outer context. Might + # come in handy and we avoid evaluating context dependent + # preds if this is > 0. + if self._dfa is not None and self._dfa.precedenceDfa: + if ( + t.outermostPrecedenceReturn + == self._dfa.atnStartState.ruleIndex + ): + c.precedenceFilterSuppressed = True + c.reachesIntoOuterContext += 1 + if c in closureBusy: + # avoid infinite recursion for right-recursive rules + continue + closureBusy.add(c) + configs.dipsIntoOuterContext = True # TODO: can remove? only care when we add to set per middle of this method + newDepth -= 1 + if ParserATNSimulator.debug: + print("dips into outer ctx: " + str(c)) + else: + if not t.isEpsilon: + if c in closureBusy: + # avoid infinite recursion for EOF* and EOF+ + continue + closureBusy.add(c) + if isinstance(t, RuleTransition): + # latch when newDepth goes negative - once we step out of the entry context we can't return + if newDepth >= 0: + newDepth += 1 + + self.closureCheckingStopState( + c, + configs, + closureBusy, + continueCollecting, + fullCtx, + newDepth, + treatEofAsEpsilon, + ) + + # Implements first-edge (loop entry) elimination as an optimization + # during closure operations. See antlr/antlr4#1398. + # + # The optimization is to avoid adding the loop entry config when + # the exit path can only lead back to the same + # StarLoopEntryState after popping context at the rule end state + # (traversing only epsilon edges, so we're still in closure, in + # this same rule). + # + # We need to detect any state that can reach loop entry on + # epsilon w/o exiting rule. We don't have to look at FOLLOW + # links, just ensure that all stack tops for config refer to key + # states in LR rule. + # + # To verify we are in the right situation we must first check + # closure is at a StarLoopEntryState generated during LR removal. + # Then we check that each stack top of context is a return state + # from one of these cases: + # + # 1. 'not' expr, '(' type ')' expr. The return state points at loop entry state + # 2. expr op expr. The return state is the block end of internal block of (...)* + # 3. 'between' expr 'and' expr. The return state of 2nd expr reference. + # That state points at block end of internal block of (...)*. + # 4. expr '?' expr ':' expr. The return state points at block end, + # which points at loop entry state. + # + # If any is true for each stack top, then closure does not add a + # config to the current config set for edge[0], the loop entry branch. + # + # Conditions fail if any context for the current config is: + # + # a. empty (we'd fall out of expr to do a global FOLLOW which could + # even be to some weird spot in expr) or, + # b. lies outside of expr or, + # c. lies within expr but at a state not the BlockEndState + # generated during LR removal + # + # Do we need to evaluate predicates ever in closure for this case? + # + # No. Predicates, including precedence predicates, are only + # evaluated when computing a DFA start state. I.e., only before + # the lookahead (but not parser) consumes a token. + # + # There are no epsilon edges allowed in LR rule alt blocks or in + # the "primary" part (ID here). If closure is in + # StarLoopEntryState any lookahead operation will have consumed a + # token as there are no epsilon-paths that lead to + # StarLoopEntryState. We do not have to evaluate predicates + # therefore if we are in the generated StarLoopEntryState of a LR + # rule. Note that when making a prediction starting at that + # decision point, decision d=2, compute-start-state performs + # closure starting at edges[0], edges[1] emanating from + # StarLoopEntryState. That means it is not performing closure on + # StarLoopEntryState during compute-start-state. + # + # How do we know this always gives same prediction answer? + # + # Without predicates, loop entry and exit paths are ambiguous + # upon remaining input +b (in, say, a+b). Either paths lead to + # valid parses. Closure can lead to consuming + immediately or by + # falling out of this call to expr back into expr and loop back + # again to StarLoopEntryState to match +b. In this special case, + # we choose the more efficient path, which is to take the bypass + # path. + # + # The lookahead language has not changed because closure chooses + # one path over the other. Both paths lead to consuming the same + # remaining input during a lookahead operation. If the next token + # is an operator, lookahead will enter the choice block with + # operators. If it is not, lookahead will exit expr. Same as if + # closure had chosen to enter the choice block immediately. + # + # Closure is examining one config (some loopentrystate, some alt, + # context) which means it is considering exactly one alt. Closure + # always copies the same alt to any derived configs. + # + # How do we know this optimization doesn't mess up precedence in + # our parse trees? + # + # Looking through expr from left edge of stat only has to confirm + # that an input, say, a+b+c; begins with any valid interpretation + # of an expression. The precedence actually doesn't matter when + # making a decision in stat seeing through expr. It is only when + # parsing rule expr that we must use the precedence to get the + # right interpretation and, hence, parse tree. + # + # @since 4.6 + # + def canDropLoopEntryEdgeInLeftRecursiveRule(self, config): + # return False + p = config.state + # First check to see if we are in StarLoopEntryState generated during + # left-recursion elimination. For efficiency, also check if + # the context has an empty stack case. If so, it would mean + # global FOLLOW so we can't perform optimization + # Are we the special loop entry/exit state? or SLL wildcard + if ( + p.stateType != ATNState.STAR_LOOP_ENTRY + or not p.isPrecedenceDecision + or config.context.isEmpty() + or config.context.hasEmptyPath() + ): + return False + + # Require all return states to return back to the same rule + # that p is in. + numCtxs = len(config.context) + for i in range(0, numCtxs): # for each stack context + returnState = self.atn.states[config.context.getReturnState(i)] + if returnState.ruleIndex != p.ruleIndex: + return False + + decisionStartState = p.transitions[0].target + blockEndStateNum = decisionStartState.endState.stateNumber + blockEndState = self.atn.states[blockEndStateNum] + + # Verify that the top of each stack context leads to loop entry/exit + # state through epsilon edges and w/o leaving rule. + for i in range(0, numCtxs): # for each stack context + returnStateNumber = config.context.getReturnState(i) + returnState = self.atn.states[returnStateNumber] + # all states must have single outgoing epsilon edge + if ( + len(returnState.transitions) != 1 + or not returnState.transitions[0].isEpsilon + ): + return False + + # Look for prefix op case like 'not expr', (' type ')' expr + returnStateTarget = returnState.transitions[0].target + if ( + returnState.stateType == ATNState.BLOCK_END + and returnStateTarget is p + ): + continue + + # Look for 'expr op expr' or case where expr's return state is block end + # of (...)* internal block; the block end points to loop back + # which points to p but we don't need to check that + if returnState is blockEndState: + continue + + # Look for ternary expr ? expr : expr. The return state points at block end, + # which points at loop entry state + if returnStateTarget is blockEndState: + continue + + # Look for complex prefix 'between expr and expr' case where 2nd expr's + # return state points at block end state of (...)* internal block + if ( + returnStateTarget.stateType == ATNState.BLOCK_END + and len(returnStateTarget.transitions) == 1 + and returnStateTarget.transitions[0].isEpsilon + and returnStateTarget.transitions[0].target is p + ): + continue + + # anything else ain't conforming + return False + + return True + + def getRuleName(self, index: int): + if self.parser is not None and index >= 0: + return self.parser.ruleNames[index] + else: + return "" + + epsilonTargetMethods = dict() + epsilonTargetMethods[Transition.RULE] = ( + lambda sim, + config, + t, + collectPredicates, + inContext, + fullCtx, + treatEofAsEpsilon: sim.ruleTransition(config, t) + ) + epsilonTargetMethods[Transition.PRECEDENCE] = ( + lambda sim, + config, + t, + collectPredicates, + inContext, + fullCtx, + treatEofAsEpsilon: sim.precedenceTransition( + config, t, collectPredicates, inContext, fullCtx + ) + ) + epsilonTargetMethods[Transition.PREDICATE] = ( + lambda sim, + config, + t, + collectPredicates, + inContext, + fullCtx, + treatEofAsEpsilon: sim.predTransition( + config, t, collectPredicates, inContext, fullCtx + ) + ) + epsilonTargetMethods[Transition.ACTION] = ( + lambda sim, + config, + t, + collectPredicates, + inContext, + fullCtx, + treatEofAsEpsilon: sim.actionTransition(config, t) + ) + epsilonTargetMethods[Transition.EPSILON] = ( + lambda sim, + config, + t, + collectPredicates, + inContext, + fullCtx, + treatEofAsEpsilon: ATNConfig(state=t.target, config=config) + ) + epsilonTargetMethods[Transition.ATOM] = ( + lambda sim, + config, + t, + collectPredicates, + inContext, + fullCtx, + treatEofAsEpsilon: ATNConfig(state=t.target, config=config) + if treatEofAsEpsilon and t.matches(Token.EOF, 0, 1) + else None + ) + epsilonTargetMethods[Transition.RANGE] = ( + lambda sim, + config, + t, + collectPredicates, + inContext, + fullCtx, + treatEofAsEpsilon: ATNConfig(state=t.target, config=config) + if treatEofAsEpsilon and t.matches(Token.EOF, 0, 1) + else None + ) + epsilonTargetMethods[Transition.SET] = ( + lambda sim, + config, + t, + collectPredicates, + inContext, + fullCtx, + treatEofAsEpsilon: ATNConfig(state=t.target, config=config) + if treatEofAsEpsilon and t.matches(Token.EOF, 0, 1) + else None + ) + + def getEpsilonTarget( + self, + config: ATNConfig, + t: Transition, + collectPredicates: bool, + inContext: bool, + fullCtx: bool, + treatEofAsEpsilon: bool, + ): + m = self.epsilonTargetMethods.get(t.serializationType, None) + if m is None: + return None + else: + return m( + self, + config, + t, + collectPredicates, + inContext, + fullCtx, + treatEofAsEpsilon, + ) + + def actionTransition(self, config: ATNConfig, t: ActionTransition): + if ParserATNSimulator.debug: + print("ACTION edge " + str(t.ruleIndex) + ":" + str(t.actionIndex)) + return ATNConfig(state=t.target, config=config) + + def precedenceTransition( + self, + config: ATNConfig, + pt: PrecedencePredicateTransition, + collectPredicates: bool, + inContext: bool, + fullCtx: bool, + ): + if ParserATNSimulator.debug: + print( + "PRED (collectPredicates=" + + str(collectPredicates) + + ") " + + str(pt.precedence) + + ">=_p, ctx dependent=true" + ) + if self.parser is not None: + print( + "context surrounding pred is " + + str(self.parser.getRuleInvocationStack()) + ) + + c = None + if collectPredicates and inContext: + if fullCtx: + # In full context mode, we can evaluate predicates on-the-fly + # during closure, which dramatically reduces the size of + # the config sets. It also obviates the need to test predicates + # later during conflict resolution. + currentPosition = self._input.index + self._input.seek(self._startIndex) + predSucceeds = pt.getPredicate().eval( + self.parser, self._outerContext + ) + self._input.seek(currentPosition) + if predSucceeds: + c = ATNConfig( + state=pt.target, config=config + ) # no pred context + else: + newSemCtx = andContext( + config.semanticContext, pt.getPredicate() + ) + c = ATNConfig( + state=pt.target, semantic=newSemCtx, config=config + ) + else: + c = ATNConfig(state=pt.target, config=config) + + if ParserATNSimulator.debug: + print("config from pred transition=" + str(c)) + return c + + def predTransition( + self, + config: ATNConfig, + pt: PredicateTransition, + collectPredicates: bool, + inContext: bool, + fullCtx: bool, + ): + if ParserATNSimulator.debug: + print( + "PRED (collectPredicates=" + + str(collectPredicates) + + ") " + + str(pt.ruleIndex) + + ":" + + str(pt.predIndex) + + ", ctx dependent=" + + str(pt.isCtxDependent) + ) + if self.parser is not None: + print( + "context surrounding pred is " + + str(self.parser.getRuleInvocationStack()) + ) + + c = None + if collectPredicates and ( + not pt.isCtxDependent or (pt.isCtxDependent and inContext) + ): + if fullCtx: + # In full context mode, we can evaluate predicates on-the-fly + # during closure, which dramatically reduces the size of + # the config sets. It also obviates the need to test predicates + # later during conflict resolution. + currentPosition = self._input.index + self._input.seek(self._startIndex) + predSucceeds = pt.getPredicate().eval( + self.parser, self._outerContext + ) + self._input.seek(currentPosition) + if predSucceeds: + c = ATNConfig( + state=pt.target, config=config + ) # no pred context + else: + newSemCtx = andContext( + config.semanticContext, pt.getPredicate() + ) + c = ATNConfig( + state=pt.target, semantic=newSemCtx, config=config + ) + else: + c = ATNConfig(state=pt.target, config=config) + + if ParserATNSimulator.debug: + print("config from pred transition=" + str(c)) + return c + + def ruleTransition(self, config: ATNConfig, t: RuleTransition): + if ParserATNSimulator.debug: + print( + "CALL rule " + + self.getRuleName(t.target.ruleIndex) + + ", ctx=" + + str(config.context) + ) + returnState = t.followState + newContext = SingletonPredictionContext.create( + config.context, returnState.stateNumber + ) + return ATNConfig(state=t.target, context=newContext, config=config) + + def getConflictingAlts(self, configs: ATNConfigSet): + altsets = PredictionMode.getConflictingAltSubsets(configs) + return PredictionMode.getAlts(altsets) + + # Sam pointed out a problem with the previous definition, v3, of + # ambiguous states. If we have another state associated with conflicting + # alternatives, we should keep going. For example, the following grammar + # + # s : (ID | ID ID?) ';' ; + # + # When the ATN simulation reaches the state before ';', it has a DFA + # state that looks like: [12|1|[], 6|2|[], 12|2|[]]. Naturally + # 12|1|[] and 12|2|[] conflict, but we cannot stop processing this node + # because alternative to has another way to continue, via [6|2|[]]. + # The key is that we have a single state that has config's only associated + # with a single alternative, 2, and crucially the state transitions + # among the configurations are all non-epsilon transitions. That means + # we don't consider any conflicts that include alternative 2. So, we + # ignore the conflict between alts 1 and 2. We ignore a set of + # conflicting alts when there is an intersection with an alternative + # associated with a single alt state in the state→config-list map. + # + # It's also the case that we might have two conflicting configurations but + # also a 3rd nonconflicting configuration for a different alternative: + # [1|1|[], 1|2|[], 8|3|[]]. This can come about from grammar: + # + # a : A | A | A B ; + # + # After matching input A, we reach the stop state for rule A, state 1. + # State 8 is the state right before B. Clearly alternatives 1 and 2 + # conflict and no amount of further lookahead will separate the two. + # However, alternative 3 will be able to continue and so we do not + # stop working on this state. In the previous example, we're concerned + # with states associated with the conflicting alternatives. Here alt + # 3 is not associated with the conflicting configs, but since we can continue + # looking for input reasonably, I don't declare the state done. We + # ignore a set of conflicting alts when we have an alternative + # that we still need to pursue. + # + + def getConflictingAltsOrUniqueAlt(self, configs: ATNConfigSet): + conflictingAlts = None + if configs.uniqueAlt != ATN.INVALID_ALT_NUMBER: + conflictingAlts = set() + conflictingAlts.add(configs.uniqueAlt) + else: + conflictingAlts = configs.conflictingAlts + return conflictingAlts + + def getTokenName(self, t: int): + if t == Token.EOF: + return "EOF" + if ( + self.parser is not None + and self.parser.literalNames is not None + and t < len(self.parser.literalNames) + ): + return self.parser.literalNames[t] + "<" + str(t) + ">" + if ( + self.parser is not None + and self.parser.symbolicNames is not None + and t < len(self.parser.symbolicNames) + ): + return self.parser.symbolicNames[t] + "<" + str(t) + ">" + else: + return str(t) + + def getLookaheadName(self, input: TokenStream): + return self.getTokenName(input.LA(1)) + + # Used for debugging in adaptivePredict around execATN but I cut + # it out for clarity now that alg. works well. We can leave this + # "dead" code for a bit. + # + def dumpDeadEndConfigs(self, nvae: NoViableAltException): + print("dead end configs: ") + for c in nvae.getDeadEndConfigs(): + trans = "no edges" + if len(c.state.transitions) > 0: + t = c.state.transitions[0] + if isinstance(t, AtomTransition): + trans = "Atom " + self.getTokenName(t.label) + elif isinstance(t, SetTransition): + neg = isinstance(t, NotSetTransition) + trans = ("~" if neg else "") + "Set " + str(t.set) + print(c.toString(self.parser, True) + ":" + trans, file=sys.stderr) + + def noViableAlt( + self, + input: TokenStream, + outerContext: ParserRuleContext, + configs: ATNConfigSet, + startIndex: int, + ): + return NoViableAltException( + self.parser, + input, + input.get(startIndex), + input.LT(1), + configs, + outerContext, + ) + + def getUniqueAlt(self, configs: ATNConfigSet): + alt = ATN.INVALID_ALT_NUMBER + for c in configs: + if alt == ATN.INVALID_ALT_NUMBER: + alt = c.alt # found first alt + elif c.alt != alt: + return ATN.INVALID_ALT_NUMBER + return alt + + # + # Add an edge to the DFA, if possible. This method calls + # {@link #addDFAState} to ensure the {@code to} state is present in the + # DFA. If {@code from} is {@code null}, or if {@code t} is outside the + # range of edges that can be represented in the DFA tables, this method + # returns without adding the edge to the DFA. + # + #

      If {@code to} is {@code null}, this method returns {@code null}. + # Otherwise, this method returns the {@link DFAState} returned by calling + # {@link #addDFAState} for the {@code to} state.

      + # + # @param dfa The DFA + # @param from The source state for the edge + # @param t The input symbol + # @param to The target state for the edge + # + # @return If {@code to} is {@code null}, this method returns {@code null}; + # otherwise this method returns the result of calling {@link #addDFAState} + # on {@code to} + # + def addDFAEdge(self, dfa: DFA, from_: DFAState, t: int, to: DFAState): + if ParserATNSimulator.debug: + print( + "EDGE " + + str(from_) + + " -> " + + str(to) + + " upon " + + self.getTokenName(t) + ) + + if to is None: + return None + + to = self.addDFAState( + dfa, to + ) # used existing if possible not incoming + if from_ is None or t < -1 or t > self.atn.maxTokenType: + return to + + if from_.edges is None: + from_.edges = [None] * (self.atn.maxTokenType + 2) + from_.edges[t + 1] = to # connect + + if ParserATNSimulator.debug: + names = None if self.parser is None else self.parser.literalNames + print("DFA=\n" + dfa.toString(names)) + + return to + + # + # Add state {@code D} to the DFA if it is not already present, and return + # the actual instance stored in the DFA. If a state equivalent to {@code D} + # is already in the DFA, the existing state is returned. Otherwise this + # method returns {@code D} after adding it to the DFA. + # + #

      If {@code D} is {@link #ERROR}, this method returns {@link #ERROR} and + # does not change the DFA.

      + # + # @param dfa The dfa + # @param D The DFA state to add + # @return The state stored in the DFA. This will be either the existing + # state if {@code D} is already in the DFA, or {@code D} itself if the + # state was not already present. + # + def addDFAState(self, dfa: DFA, D: DFAState): + if D is self.ERROR: + return D + + existing = dfa.states.get(D, None) + if existing is not None: + if ParserATNSimulator.trace_atn_sim: + print("addDFAState", str(D), "exists") + return existing + + D.stateNumber = len(dfa.states) + if not D.configs.readonly: + D.configs.optimizeConfigs(self) + D.configs.setReadonly(True) + + if ParserATNSimulator.trace_atn_sim: + print("addDFAState new", str(D)) + + dfa.states[D] = D + return D + + def reportAttemptingFullContext( + self, + dfa: DFA, + conflictingAlts: set, + configs: ATNConfigSet, + startIndex: int, + stopIndex: int, + ): + if ParserATNSimulator.debug or ParserATNSimulator.retry_debug: + print( + "reportAttemptingFullContext decision=" + + str(dfa.decision) + + ":" + + str(configs) + + ", input=" + + self.parser.getTokenStream().getText(startIndex, stopIndex) + ) + if self.parser is not None: + self.parser.getErrorListenerDispatch().reportAttemptingFullContext( + self.parser, + dfa, + startIndex, + stopIndex, + conflictingAlts, + configs, + ) + + def reportContextSensitivity( + self, + dfa: DFA, + prediction: int, + configs: ATNConfigSet, + startIndex: int, + stopIndex: int, + ): + if ParserATNSimulator.debug or ParserATNSimulator.retry_debug: + print( + "reportContextSensitivity decision=" + + str(dfa.decision) + + ":" + + str(configs) + + ", input=" + + self.parser.getTokenStream().getText(startIndex, stopIndex) + ) + if self.parser is not None: + self.parser.getErrorListenerDispatch().reportContextSensitivity( + self.parser, dfa, startIndex, stopIndex, prediction, configs + ) + + # If context sensitive parsing, we know it's ambiguity not conflict# + def reportAmbiguity( + self, + dfa: DFA, + D: DFAState, + startIndex: int, + stopIndex: int, + exact: bool, + ambigAlts: set, + configs: ATNConfigSet, + ): + if ParserATNSimulator.debug or ParserATNSimulator.retry_debug: + # ParserATNPathFinder finder = new ParserATNPathFinder(parser, atn); + # int i = 1; + # for (Transition t : dfa.atnStartState.transitions) { + # print("ALT "+i+"="); + # print(startIndex+".."+stopIndex+", len(input)="+parser.getInputStream().size()); + # TraceTree path = finder.trace(t.target, parser.getContext(), (TokenStream)parser.getInputStream(), + # startIndex, stopIndex); + # if ( path!=null ) { + # print("path = "+path.toStringTree()); + # for (TraceTree leaf : path.leaves) { + # List states = path.getPathToNode(leaf); + # print("states="+states); + # } + # } + # i++; + # } + print( + "reportAmbiguity " + + str(ambigAlts) + + ":" + + str(configs) + + ", input=" + + self.parser.getTokenStream().getText(startIndex, stopIndex) + ) + if self.parser is not None: + self.parser.getErrorListenerDispatch().reportAmbiguity( + self.parser, + dfa, + startIndex, + stopIndex, + exact, + ambigAlts, + configs, + ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/PredictionMode.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/PredictionMode.py new file mode 100644 index 00000000..00141d8c --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/PredictionMode.py @@ -0,0 +1,514 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# +# +# This enumeration defines the prediction modes available in ANTLR 4 along with +# utility methods for analyzing configuration sets for conflicts and/or +# ambiguities. + + +from enum import Enum + +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfig import ( + ATNConfig, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfigSet import ( + ATNConfigSet, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( + RuleStopState, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.SemanticContext import ( + SemanticContext, +) + +PredictionMode = None + + +class PredictionMode(Enum): + # + # The SLL(*) prediction mode. This prediction mode ignores the current + # parser context when making predictions. This is the fastest prediction + # mode, and provides correct results for many grammars. This prediction + # mode is more powerful than the prediction mode provided by ANTLR 3, but + # may result in syntax errors for grammar and input combinations which are + # not SLL. + # + #

      + # When using this prediction mode, the parser will either return a correct + # parse tree (i.e. the same parse tree that would be returned with the + # {@link #LL} prediction mode), or it will report a syntax error. If a + # syntax error is encountered when using the {@link #SLL} prediction mode, + # it may be due to either an actual syntax error in the input or indicate + # that the particular combination of grammar and input requires the more + # powerful {@link #LL} prediction abilities to complete successfully.

      + # + #

      + # This prediction mode does not provide any guarantees for prediction + # behavior for syntactically-incorrect inputs.

      + # + SLL = 0 + # + # The LL(*) prediction mode. This prediction mode allows the current parser + # context to be used for resolving SLL conflicts that occur during + # prediction. This is the fastest prediction mode that guarantees correct + # parse results for all combinations of grammars with syntactically correct + # inputs. + # + #

      + # When using this prediction mode, the parser will make correct decisions + # for all syntactically-correct grammar and input combinations. However, in + # cases where the grammar is truly ambiguous this prediction mode might not + # report a precise answer for exactly which alternatives are + # ambiguous.

      + # + #

      + # This prediction mode does not provide any guarantees for prediction + # behavior for syntactically-incorrect inputs.

      + # + LL = 1 + # + # The LL(*) prediction mode with exact ambiguity detection. In addition to + # the correctness guarantees provided by the {@link #LL} prediction mode, + # this prediction mode instructs the prediction algorithm to determine the + # complete and exact set of ambiguous alternatives for every ambiguous + # decision encountered while parsing. + # + #

      + # This prediction mode may be used for diagnosing ambiguities during + # grammar development. Due to the performance overhead of calculating sets + # of ambiguous alternatives, this prediction mode should be avoided when + # the exact results are not necessary.

      + # + #

      + # This prediction mode does not provide any guarantees for prediction + # behavior for syntactically-incorrect inputs.

      + # + LL_EXACT_AMBIG_DETECTION = 2 + + # + # Computes the SLL prediction termination condition. + # + #

      + # This method computes the SLL prediction termination condition for both of + # the following cases.

      + # + #
        + #
      • The usual SLL+LL fallback upon SLL conflict
      • + #
      • Pure SLL without LL fallback
      • + #
      + # + #

      COMBINED SLL+LL PARSING

      + # + #

      When LL-fallback is enabled upon SLL conflict, correct predictions are + # ensured regardless of how the termination condition is computed by this + # method. Due to the substantially higher cost of LL prediction, the + # prediction should only fall back to LL when the additional lookahead + # cannot lead to a unique SLL prediction.

      + # + #

      Assuming combined SLL+LL parsing, an SLL configuration set with only + # conflicting subsets should fall back to full LL, even if the + # configuration sets don't resolve to the same alternative (e.g. + # {@code {1,2}} and {@code {3,4}}. If there is at least one non-conflicting + # configuration, SLL could continue with the hopes that more lookahead will + # resolve via one of those non-conflicting configurations.

      + # + #

      Here's the prediction termination rule them: SLL (for SLL+LL parsing) + # stops when it sees only conflicting configuration subsets. In contrast, + # full LL keeps going when there is uncertainty.

      + # + #

      HEURISTIC

      + # + #

      As a heuristic, we stop prediction when we see any conflicting subset + # unless we see a state that only has one alternative associated with it. + # The single-alt-state thing lets prediction continue upon rules like + # (otherwise, it would admit defeat too soon):

      + # + #

      {@code [12|1|[], 6|2|[], 12|2|[]]. s : (ID | ID ID?) ';' ;}

      + # + #

      When the ATN simulation reaches the state before {@code ';'}, it has a + # DFA state that looks like: {@code [12|1|[], 6|2|[], 12|2|[]]}. Naturally + # {@code 12|1|[]} and {@code 12|2|[]} conflict, but we cannot stop + # processing this node because alternative to has another way to continue, + # via {@code [6|2|[]]}.

      + # + #

      It also let's us continue for this rule:

      + # + #

      {@code [1|1|[], 1|2|[], 8|3|[]] a : A | A | A B ;}

      + # + #

      After matching input A, we reach the stop state for rule A, state 1. + # State 8 is the state right before B. Clearly alternatives 1 and 2 + # conflict and no amount of further lookahead will separate the two. + # However, alternative 3 will be able to continue and so we do not stop + # working on this state. In the previous example, we're concerned with + # states associated with the conflicting alternatives. Here alt 3 is not + # associated with the conflicting configs, but since we can continue + # looking for input reasonably, don't declare the state done.

      + # + #

      PURE SLL PARSING

      + # + #

      To handle pure SLL parsing, all we have to do is make sure that we + # combine stack contexts for configurations that differ only by semantic + # predicate. From there, we can do the usual SLL termination heuristic.

      + # + #

      PREDICATES IN SLL+LL PARSING

      + # + #

      SLL decisions don't evaluate predicates until after they reach DFA stop + # states because they need to create the DFA cache that works in all + # semantic situations. In contrast, full LL evaluates predicates collected + # during start state computation so it can ignore predicates thereafter. + # This means that SLL termination detection can totally ignore semantic + # predicates.

      + # + #

      Implementation-wise, {@link ATNConfigSet} combines stack contexts but not + # semantic predicate contexts so we might see two configurations like the + # following.

      + # + #

      {@code (s, 1, x, {}), (s, 1, x', {p})}

      + # + #

      Before testing these configurations against others, we have to merge + # {@code x} and {@code x'} (without modifying the existing configurations). + # For example, we test {@code (x+x')==x''} when looking for conflicts in + # the following configurations.

      + # + #

      {@code (s, 1, x, {}), (s, 1, x', {p}), (s, 2, x'', {})}

      + # + #

      If the configuration set has predicates (as indicated by + # {@link ATNConfigSet#hasSemanticContext}), this algorithm makes a copy of + # the configurations to strip out all of the predicates so that a standard + # {@link ATNConfigSet} will merge everything ignoring predicates.

      + # + @classmethod + def hasSLLConflictTerminatingPrediction( + cls, mode: PredictionMode, configs: ATNConfigSet + ): + # Configs in rule stop states indicate reaching the end of the decision + # rule (local context) or end of start rule (full context). If all + # configs meet this condition, then none of the configurations is able + # to match additional input so we terminate prediction. + # + if cls.allConfigsInRuleStopStates(configs): + return True + + # pure SLL mode parsing + if mode == PredictionMode.SLL: + # Don't bother with combining configs from different semantic + # contexts if we can fail over to full LL; costs more time + # since we'll often fail over anyway. + if configs.hasSemanticContext: + # dup configs, tossing out semantic predicates + dup = ATNConfigSet() + for c in configs: + c = ATNConfig(config=c, semantic=SemanticContext.NONE) + dup.add(c) + configs = dup + # now we have combined contexts for configs with dissimilar preds + + # pure SLL or combined SLL+LL mode parsing + altsets = cls.getConflictingAltSubsets(configs) + return cls.hasConflictingAltSet( + altsets + ) and not cls.hasStateAssociatedWithOneAlt(configs) + + # Checks if any configuration in {@code configs} is in a + # {@link RuleStopState}. Configurations meeting this condition have reached + # the end of the decision rule (local context) or end of start rule (full + # context). + # + # @param configs the configuration set to test + # @return {@code true} if any configuration in {@code configs} is in a + # {@link RuleStopState}, otherwise {@code false} + @classmethod + def hasConfigInRuleStopState(cls, configs: ATNConfigSet): + return any(isinstance(cfg.state, RuleStopState) for cfg in configs) + + # Checks if all configurations in {@code configs} are in a + # {@link RuleStopState}. Configurations meeting this condition have reached + # the end of the decision rule (local context) or end of start rule (full + # context). + # + # @param configs the configuration set to test + # @return {@code true} if all configurations in {@code configs} are in a + # {@link RuleStopState}, otherwise {@code false} + @classmethod + def allConfigsInRuleStopStates(cls, configs: ATNConfigSet): + return all(isinstance(cfg.state, RuleStopState) for cfg in configs) + + # + # Full LL prediction termination. + # + #

      Can we stop looking ahead during ATN simulation or is there some + # uncertainty as to which alternative we will ultimately pick, after + # consuming more input? Even if there are partial conflicts, we might know + # that everything is going to resolve to the same minimum alternative. That + # means we can stop since no more lookahead will change that fact. On the + # other hand, there might be multiple conflicts that resolve to different + # minimums. That means we need more look ahead to decide which of those + # alternatives we should predict.

      + # + #

      The basic idea is to split the set of configurations {@code C}, into + # conflicting subsets {@code (s, _, ctx, _)} and singleton subsets with + # non-conflicting configurations. Two configurations conflict if they have + # identical {@link ATNConfig#state} and {@link ATNConfig#context} values + # but different {@link ATNConfig#alt} value, e.g. {@code (s, i, ctx, _)} + # and {@code (s, j, ctx, _)} for {@code i!=j}.

      + # + #

      Reduce these configuration subsets to the set of possible alternatives. + # You can compute the alternative subsets in one pass as follows:

      + # + #

      {@code A_s,ctx = {i | (s, i, ctx, _)}} for each configuration in + # {@code C} holding {@code s} and {@code ctx} fixed.

      + # + #

      Or in pseudo-code, for each configuration {@code c} in {@code C}:

      + # + #
      +    # map[c] U= c.{@link ATNConfig#alt alt} # map hash/equals uses s and x, not
      +    # alt and not pred
      +    # 
      + # + #

      The values in {@code map} are the set of {@code A_s,ctx} sets.

      + # + #

      If {@code |A_s,ctx|=1} then there is no conflict associated with + # {@code s} and {@code ctx}.

      + # + #

      Reduce the subsets to singletons by choosing a minimum of each subset. If + # the union of these alternative subsets is a singleton, then no amount of + # more lookahead will help us. We will always pick that alternative. If, + # however, there is more than one alternative, then we are uncertain which + # alternative to predict and must continue looking for resolution. We may + # or may not discover an ambiguity in the future, even if there are no + # conflicting subsets this round.

      + # + #

      The biggest sin is to terminate early because it means we've made a + # decision but were uncertain as to the eventual outcome. We haven't used + # enough lookahead. On the other hand, announcing a conflict too late is no + # big deal; you will still have the conflict. It's just inefficient. It + # might even look until the end of file.

      + # + #

      No special consideration for semantic predicates is required because + # predicates are evaluated on-the-fly for full LL prediction, ensuring that + # no configuration contains a semantic context during the termination + # check.

      + # + #

      CONFLICTING CONFIGS

      + # + #

      Two configurations {@code (s, i, x)} and {@code (s, j, x')}, conflict + # when {@code i!=j} but {@code x=x'}. Because we merge all + # {@code (s, i, _)} configurations together, that means that there are at + # most {@code n} configurations associated with state {@code s} for + # {@code n} possible alternatives in the decision. The merged stacks + # complicate the comparison of configuration contexts {@code x} and + # {@code x'}. Sam checks to see if one is a subset of the other by calling + # merge and checking to see if the merged result is either {@code x} or + # {@code x'}. If the {@code x} associated with lowest alternative {@code i} + # is the superset, then {@code i} is the only possible prediction since the + # others resolve to {@code min(i)} as well. However, if {@code x} is + # associated with {@code j>i} then at least one stack configuration for + # {@code j} is not in conflict with alternative {@code i}. The algorithm + # should keep going, looking for more lookahead due to the uncertainty.

      + # + #

      For simplicity, I'm doing a equality check between {@code x} and + # {@code x'} that lets the algorithm continue to consume lookahead longer + # than necessary. The reason I like the equality is of course the + # simplicity but also because that is the test you need to detect the + # alternatives that are actually in conflict.

      + # + #

      CONTINUE/STOP RULE

      + # + #

      Continue if union of resolved alternative sets from non-conflicting and + # conflicting alternative subsets has more than one alternative. We are + # uncertain about which alternative to predict.

      + # + #

      The complete set of alternatives, {@code [i for (_,i,_)]}, tells us which + # alternatives are still in the running for the amount of input we've + # consumed at this point. The conflicting sets let us to strip away + # configurations that won't lead to more states because we resolve + # conflicts to the configuration with a minimum alternate for the + # conflicting set.

      + # + #

      CASES

      + # + #
        + # + #
      • no conflicts and more than 1 alternative in set => continue
      • + # + #
      • {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s, 3, z)}, + # {@code (s', 1, y)}, {@code (s', 2, y)} yields non-conflicting set + # {@code {3}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} = + # {@code {1,3}} => continue + #
      • + # + #
      • {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)}, + # {@code (s', 2, y)}, {@code (s'', 1, z)} yields non-conflicting set + # {@code {1}} U conflicting sets {@code min({1,2})} U {@code min({1,2})} = + # {@code {1}} => stop and predict 1
      • + # + #
      • {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 1, y)}, + # {@code (s', 2, y)} yields conflicting, reduced sets {@code {1}} U + # {@code {1}} = {@code {1}} => stop and predict 1, can announce + # ambiguity {@code {1,2}}
      • + # + #
      • {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 2, y)}, + # {@code (s', 3, y)} yields conflicting, reduced sets {@code {1}} U + # {@code {2}} = {@code {1,2}} => continue
      • + # + #
      • {@code (s, 1, x)}, {@code (s, 2, x)}, {@code (s', 3, y)}, + # {@code (s', 4, y)} yields conflicting, reduced sets {@code {1}} U + # {@code {3}} = {@code {1,3}} => continue
      • + # + #
      + # + #

      EXACT AMBIGUITY DETECTION

      + # + #

      If all states report the same conflicting set of alternatives, then we + # know we have the exact ambiguity set.

      + # + #

      |A_i|>1 and + # A_i = A_j for all i, j.

      + # + #

      In other words, we continue examining lookahead until all {@code A_i} + # have more than one alternative and all {@code A_i} are the same. If + # {@code A={{1,2}, {1,3}}}, then regular LL prediction would terminate + # because the resolved set is {@code {1}}. To determine what the real + # ambiguity is, we have to know whether the ambiguity is between one and + # two or one and three so we keep going. We can only stop prediction when + # we need exact ambiguity detection when the sets look like + # {@code A={{1,2}}} or {@code {{1,2},{1,2}}}, etc...

      + # + @classmethod + def resolvesToJustOneViableAlt(cls, altsets: list): + return cls.getSingleViableAlt(altsets) + + # + # Determines if every alternative subset in {@code altsets} contains more + # than one alternative. + # + # @param altsets a collection of alternative subsets + # @return {@code true} if every {@link BitSet} in {@code altsets} has + # {@link BitSet#cardinality cardinality} > 1, otherwise {@code false} + # + @classmethod + def allSubsetsConflict(cls, altsets: list): + return not cls.hasNonConflictingAltSet(altsets) + + # + # Determines if any single alternative subset in {@code altsets} contains + # exactly one alternative. + # + # @param altsets a collection of alternative subsets + # @return {@code true} if {@code altsets} contains a {@link BitSet} with + # {@link BitSet#cardinality cardinality} 1, otherwise {@code false} + # + @classmethod + def hasNonConflictingAltSet(cls, altsets: list): + return any(len(alts) == 1 for alts in altsets) + + # + # Determines if any single alternative subset in {@code altsets} contains + # more than one alternative. + # + # @param altsets a collection of alternative subsets + # @return {@code true} if {@code altsets} contains a {@link BitSet} with + # {@link BitSet#cardinality cardinality} > 1, otherwise {@code false} + # + @classmethod + def hasConflictingAltSet(cls, altsets: list): + return any(len(alts) > 1 for alts in altsets) + + # + # Determines if every alternative subset in {@code altsets} is equivalent. + # + # @param altsets a collection of alternative subsets + # @return {@code true} if every member of {@code altsets} is equal to the + # others, otherwise {@code false} + # + @classmethod + def allSubsetsEqual(cls, altsets: list): + if not altsets: + return True + first = next(iter(altsets)) + return all(alts == first for alts in iter(altsets)) + + # + # Returns the unique alternative predicted by all alternative subsets in + # {@code altsets}. If no such alternative exists, this method returns + # {@link ATN#INVALID_ALT_NUMBER}. + # + # @param altsets a collection of alternative subsets + # + @classmethod + def getUniqueAlt(cls, altsets: list): + all = cls.getAlts(altsets) + if len(all) == 1: + return next(iter(all)) + return ATN.INVALID_ALT_NUMBER + + # Gets the complete set of represented alternatives for a collection of + # alternative subsets. This method returns the union of each {@link BitSet} + # in {@code altsets}. + # + # @param altsets a collection of alternative subsets + # @return the set of represented alternatives in {@code altsets} + # + @classmethod + def getAlts(cls, altsets: list): + return set.union(*altsets) + + # + # This function gets the conflicting alt subsets from a configuration set. + # For each configuration {@code c} in {@code configs}: + # + #
      +    # map[c] U= c.{@link ATNConfig#alt alt} # map hash/equals uses s and x, not
      +    # alt and not pred
      +    # 
      + # + @classmethod + def getConflictingAltSubsets(cls, configs: ATNConfigSet): + configToAlts = dict() + for c in configs: + h = hash((c.state.stateNumber, c.context)) + alts = configToAlts.get(h, None) + if alts is None: + alts = set() + configToAlts[h] = alts + alts.add(c.alt) + return configToAlts.values() + + # + # Get a map from state to alt subset from a configuration set. For each + # configuration {@code c} in {@code configs}: + # + #
      +    # map[c.{@link ATNConfig#state state}] U= c.{@link ATNConfig#alt alt}
      +    # 
      + # + @classmethod + def getStateToAltMap(cls, configs: ATNConfigSet): + m = dict() + for c in configs: + alts = m.get(c.state, None) + if alts is None: + alts = set() + m[c.state] = alts + alts.add(c.alt) + return m + + @classmethod + def hasStateAssociatedWithOneAlt(cls, configs: ATNConfigSet): + return any( + len(alts) == 1 for alts in cls.getStateToAltMap(configs).values() + ) + + @classmethod + def getSingleViableAlt(cls, altsets: list): + viableAlts = set() + for alts in altsets: + minAlt = min(alts) + viableAlts.add(minAlt) + if len(viableAlts) > 1: # more than 1 viable alt + return ATN.INVALID_ALT_NUMBER + return min(viableAlts) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/SemanticContext.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/SemanticContext.py new file mode 100644 index 00000000..0d16ea6f --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/SemanticContext.py @@ -0,0 +1,356 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +# A tree structure used to record the semantic context in which +# an ATN configuration is valid. It's either a single predicate, +# a conjunction {@code p1&&p2}, or a sum of products {@code p1||p2}. +# +#

      I have scoped the {@link AND}, {@link OR}, and {@link Predicate} subclasses of +# {@link SemanticContext} within the scope of this outer class.

      +# +from io import StringIO + +from cf_units._udunits2_parser.parser._antlr4_runtime.Recognizer import ( + Recognizer, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( + RuleContext, +) + + +class SemanticContext: + # + # The default {@link SemanticContext}, which is semantically equivalent to + # a predicate of the form {@code {true}?}. + # + NONE = None + + # + # For context independent predicates, we evaluate them without a local + # context (i.e., null context). That way, we can evaluate them without + # having to create proper rule-specific context during prediction (as + # opposed to the parser, which creates them naturally). In a practical + # sense, this avoids a cast exception from RuleContext to myruleContext. + # + #

      For context dependent predicates, we must pass in a local context so that + # references such as $arg evaluate properly as _localctx.arg. We only + # capture context dependent predicates in the context in which we begin + # prediction, so we passed in the outer context here in case of context + # dependent predicate evaluation.

      + # + def eval(self, parser: Recognizer, outerContext: RuleContext): + pass + + # + # Evaluate the precedence predicates for the context and reduce the result. + # + # @param parser The parser instance. + # @param outerContext The current parser context object. + # @return The simplified semantic context after precedence predicates are + # evaluated, which will be one of the following values. + #
        + #
      • {@link #NONE}: if the predicate simplifies to {@code true} after + # precedence predicates are evaluated.
      • + #
      • {@code null}: if the predicate simplifies to {@code false} after + # precedence predicates are evaluated.
      • + #
      • {@code this}: if the semantic context is not changed as a result of + # precedence predicate evaluation.
      • + #
      • A non-{@code null} {@link SemanticContext}: the new simplified + # semantic context after precedence predicates are evaluated.
      • + #
      + # + def evalPrecedence(self, parser: Recognizer, outerContext: RuleContext): + return self + + +# need forward declaration +AND = None + + +def andContext(a: SemanticContext, b: SemanticContext): + if a is None or a is SemanticContext.NONE: + return b + if b is None or b is SemanticContext.NONE: + return a + result = AND(a, b) + if len(result.opnds) == 1: + return result.opnds[0] + else: + return result + + +# need forward declaration +OR = None + + +def orContext(a: SemanticContext, b: SemanticContext): + if a is None: + return b + if b is None: + return a + if a is SemanticContext.NONE or b is SemanticContext.NONE: + return SemanticContext.NONE + result = OR(a, b) + if len(result.opnds) == 1: + return result.opnds[0] + else: + return result + + +def filterPrecedencePredicates(collection: set): + return [ + context + for context in collection + if isinstance(context, PrecedencePredicate) + ] + + +class EmptySemanticContext(SemanticContext): + pass + + +class Predicate(SemanticContext): + __slots__ = ("ruleIndex", "predIndex", "isCtxDependent") + + def __init__( + self, + ruleIndex: int = -1, + predIndex: int = -1, + isCtxDependent: bool = False, + ): + self.ruleIndex = ruleIndex + self.predIndex = predIndex + self.isCtxDependent = isCtxDependent # e.g., $i ref in pred + + def eval(self, parser: Recognizer, outerContext: RuleContext): + localctx = outerContext if self.isCtxDependent else None + return parser.sempred(localctx, self.ruleIndex, self.predIndex) + + def __hash__(self): + return hash((self.ruleIndex, self.predIndex, self.isCtxDependent)) + + def __eq__(self, other): + if self is other: + return True + elif not isinstance(other, Predicate): + return False + return ( + self.ruleIndex == other.ruleIndex + and self.predIndex == other.predIndex + and self.isCtxDependent == other.isCtxDependent + ) + + def __str__(self): + return "{" + str(self.ruleIndex) + ":" + str(self.predIndex) + "}?" + + +class PrecedencePredicate(SemanticContext): + def __init__(self, precedence: int = 0): + self.precedence = precedence + + def eval(self, parser: Recognizer, outerContext: RuleContext): + return parser.precpred(outerContext, self.precedence) + + def evalPrecedence(self, parser: Recognizer, outerContext: RuleContext): + if parser.precpred(outerContext, self.precedence): + return SemanticContext.NONE + else: + return None + + def __lt__(self, other): + return self.precedence < other.precedence + + def __hash__(self): + return 31 + + def __eq__(self, other): + if self is other: + return True + elif not isinstance(other, PrecedencePredicate): + return False + else: + return self.precedence == other.precedence + + def __str__(self): + return "{" + str(self.precedence) + ">=prec}?" + + +# A semantic context which is true whenever none of the contained contexts +# is false. +del AND + + +class AND(SemanticContext): + __slots__ = "opnds" + + def __init__(self, a: SemanticContext, b: SemanticContext): + operands = set() + if isinstance(a, AND): + operands.update(a.opnds) + else: + operands.add(a) + if isinstance(b, AND): + operands.update(b.opnds) + else: + operands.add(b) + + precedencePredicates = filterPrecedencePredicates(operands) + if len(precedencePredicates) > 0: + # interested in the transition with the lowest precedence + reduced = min(precedencePredicates) + operands.add(reduced) + + self.opnds = list(operands) + + def __eq__(self, other): + if self is other: + return True + elif not isinstance(other, AND): + return False + else: + return self.opnds == other.opnds + + def __hash__(self): + h = 0 + for o in self.opnds: + h = hash((h, o)) + return hash((h, "AND")) + + # + # {@inheritDoc} + # + #

      + # The evaluation of predicates by this context is short-circuiting, but + # unordered.

      + # + def eval(self, parser: Recognizer, outerContext: RuleContext): + return all(opnd.eval(parser, outerContext) for opnd in self.opnds) + + def evalPrecedence(self, parser: Recognizer, outerContext: RuleContext): + differs = False + operands = [] + for context in self.opnds: + evaluated = context.evalPrecedence(parser, outerContext) + differs |= evaluated is not context + if evaluated is None: + # The AND context is false if any element is false + return None + elif evaluated is not SemanticContext.NONE: + # Reduce the result by skipping true elements + operands.append(evaluated) + + if not differs: + return self + + if len(operands) == 0: + # all elements were true, so the AND context is true + return SemanticContext.NONE + + result = None + for o in operands: + result = o if result is None else andContext(result, o) + + return result + + def __str__(self): + with StringIO() as buf: + first = True + for o in self.opnds: + if not first: + buf.write("&&") + buf.write(str(o)) + first = False + return buf.getvalue() + + +# +# A semantic context which is true whenever at least one of the contained +# contexts is true. +del OR + + +class OR(SemanticContext): + __slots__ = "opnds" + + def __init__(self, a: SemanticContext, b: SemanticContext): + operands = set() + if isinstance(a, OR): + operands.update(a.opnds) + else: + operands.add(a) + if isinstance(b, OR): + operands.update(b.opnds) + else: + operands.add(b) + + precedencePredicates = filterPrecedencePredicates(operands) + if len(precedencePredicates) > 0: + # interested in the transition with the highest precedence + s = sorted(precedencePredicates) + reduced = s[-1] + operands.add(reduced) + + self.opnds = list(operands) + + def __eq__(self, other): + if self is other: + return True + elif not isinstance(other, OR): + return False + else: + return self.opnds == other.opnds + + def __hash__(self): + h = 0 + for o in self.opnds: + h = hash((h, o)) + return hash((h, "OR")) + + #

      + # The evaluation of predicates by this context is short-circuiting, but + # unordered.

      + # + def eval(self, parser: Recognizer, outerContext: RuleContext): + return any(opnd.eval(parser, outerContext) for opnd in self.opnds) + + def evalPrecedence(self, parser: Recognizer, outerContext: RuleContext): + differs = False + operands = [] + for context in self.opnds: + evaluated = context.evalPrecedence(parser, outerContext) + differs |= evaluated is not context + if evaluated is SemanticContext.NONE: + # The OR context is true if any element is true + return SemanticContext.NONE + elif evaluated is not None: + # Reduce the result by skipping false elements + operands.append(evaluated) + + if not differs: + return self + + if len(operands) == 0: + # all elements were false, so the OR context is false + return None + + result = None + for o in operands: + result = o if result is None else orContext(result, o) + + return result + + def __str__(self): + with StringIO() as buf: + first = True + for o in self.opnds: + if not first: + buf.write("||") + buf.write(str(o)) + first = False + return buf.getvalue() + + +SemanticContext.NONE = EmptySemanticContext() diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/Transition.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/Transition.py new file mode 100644 index 00000000..0d04eadb --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/Transition.py @@ -0,0 +1,312 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +# An ATN transition between any two ATN states. Subclasses define +# atom, set, epsilon, action, predicate, rule transitions. +# +#

      This is a one way link. It emanates from a state (usually via a list of +# transitions) and has a target state.

      +# +#

      Since we never have to change the ATN transitions once we construct it, +# we can fix these transitions as specific classes. The DFA transitions +# on the other hand need to update the labels as it adds transitions to +# the states. We'll use the term Edge for the DFA to distinguish them from +# ATN transitions.

      +# +# need forward declarations +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.SemanticContext import ( + PrecedencePredicate, + Predicate, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.IntervalSet import ( + IntervalSet, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + +ATNState = None +RuleStartState = None + + +class Transition: + __slots__ = ("target", "isEpsilon", "label") + + # constants for serialization + EPSILON = 1 + RANGE = 2 + RULE = 3 + PREDICATE = 4 # e.g., {isType(input.LT(1))}? + ATOM = 5 + ACTION = 6 + SET = 7 # ~(A|B) or ~atom, wildcard, which convert to next 2 + NOT_SET = 8 + WILDCARD = 9 + PRECEDENCE = 10 + + serializationNames = [ + "INVALID", + "EPSILON", + "RANGE", + "RULE", + "PREDICATE", + "ATOM", + "ACTION", + "SET", + "NOT_SET", + "WILDCARD", + "PRECEDENCE", + ] + + serializationTypes = dict() + + def __init__(self, target: ATNState): + # The target of this transition. + if target is None: + raise Exception("target cannot be null.") + self.target = target + # Are we epsilon, action, sempred? + self.isEpsilon = False + self.label = None + + +# TODO: make all transitions sets? no, should remove set edges +class AtomTransition(Transition): + __slots__ = ("label_", "serializationType") + + def __init__(self, target: ATNState, label: int): + super().__init__(target) + self.label_ = label # The token type or character value; or, signifies special label. + self.label = self.makeLabel() + self.serializationType = self.ATOM + + def makeLabel(self): + s = IntervalSet() + s.addOne(self.label_) + return s + + def matches(self, symbol: int, minVocabSymbol: int, maxVocabSymbol: int): + return self.label_ == symbol + + def __str__(self): + return str(self.label_) + + +class RuleTransition(Transition): + __slots__ = ("ruleIndex", "precedence", "followState", "serializationType") + + def __init__( + self, + ruleStart: RuleStartState, + ruleIndex: int, + precedence: int, + followState: ATNState, + ): + super().__init__(ruleStart) + self.ruleIndex = ( + ruleIndex # ptr to the rule definition object for this rule ref + ) + self.precedence = precedence + self.followState = followState # what node to begin computations following ref to rule + self.serializationType = self.RULE + self.isEpsilon = True + + def matches(self, symbol: int, minVocabSymbol: int, maxVocabSymbol: int): + return False + + +class EpsilonTransition(Transition): + __slots__ = ("serializationType", "outermostPrecedenceReturn") + + def __init__(self, target, outermostPrecedenceReturn=-1): + super(EpsilonTransition, self).__init__(target) + self.serializationType = self.EPSILON + self.isEpsilon = True + self.outermostPrecedenceReturn = outermostPrecedenceReturn + + def matches(self, symbol: int, minVocabSymbol: int, maxVocabSymbol: int): + return False + + def __str__(self): + return "epsilon" + + +class RangeTransition(Transition): + __slots__ = ("serializationType", "start", "stop") + + def __init__(self, target: ATNState, start: int, stop: int): + super().__init__(target) + self.serializationType = self.RANGE + self.start = start + self.stop = stop + self.label = self.makeLabel() + + def makeLabel(self): + s = IntervalSet() + s.addRange(range(self.start, self.stop + 1)) + return s + + def matches(self, symbol: int, minVocabSymbol: int, maxVocabSymbol: int): + return symbol >= self.start and symbol <= self.stop + + def __str__(self): + return "'" + chr(self.start) + "'..'" + chr(self.stop) + "'" + + +class AbstractPredicateTransition(Transition): + def __init__(self, target: ATNState): + super().__init__(target) + + +class PredicateTransition(AbstractPredicateTransition): + __slots__ = ( + "serializationType", + "ruleIndex", + "predIndex", + "isCtxDependent", + ) + + def __init__( + self, + target: ATNState, + ruleIndex: int, + predIndex: int, + isCtxDependent: bool, + ): + super().__init__(target) + self.serializationType = self.PREDICATE + self.ruleIndex = ruleIndex + self.predIndex = predIndex + self.isCtxDependent = isCtxDependent # e.g., $i ref in pred + self.isEpsilon = True + + def matches(self, symbol: int, minVocabSymbol: int, maxVocabSymbol: int): + return False + + def getPredicate(self): + return Predicate(self.ruleIndex, self.predIndex, self.isCtxDependent) + + def __str__(self): + return "pred_" + str(self.ruleIndex) + ":" + str(self.predIndex) + + +class ActionTransition(Transition): + __slots__ = ( + "serializationType", + "ruleIndex", + "actionIndex", + "isCtxDependent", + ) + + def __init__( + self, + target: ATNState, + ruleIndex: int, + actionIndex: int = -1, + isCtxDependent: bool = False, + ): + super().__init__(target) + self.serializationType = self.ACTION + self.ruleIndex = ruleIndex + self.actionIndex = actionIndex + self.isCtxDependent = isCtxDependent # e.g., $i ref in pred + self.isEpsilon = True + + def matches(self, symbol: int, minVocabSymbol: int, maxVocabSymbol: int): + return False + + def __str__(self): + return "action_" + self.ruleIndex + ":" + self.actionIndex + + +# A transition containing a set of values. +class SetTransition(Transition): + __slots__ = "serializationType" + + def __init__(self, target: ATNState, set: IntervalSet): + super().__init__(target) + self.serializationType = self.SET + if set is not None: + self.label = set + else: + self.label = IntervalSet() + self.label.addRange( + range(Token.INVALID_TYPE, Token.INVALID_TYPE + 1) + ) + + def matches(self, symbol: int, minVocabSymbol: int, maxVocabSymbol: int): + return symbol in self.label + + def __str__(self): + return str(self.label) + + +class NotSetTransition(SetTransition): + def __init__(self, target: ATNState, set: IntervalSet): + super().__init__(target, set) + self.serializationType = self.NOT_SET + + def matches(self, symbol: int, minVocabSymbol: int, maxVocabSymbol: int): + return ( + symbol >= minVocabSymbol + and symbol <= maxVocabSymbol + and not super(type(self), self).matches( + symbol, minVocabSymbol, maxVocabSymbol + ) + ) + + def __str__(self): + return "~" + super(type(self), self).__str__() + + +class WildcardTransition(Transition): + __slots__ = "serializationType" + + def __init__(self, target: ATNState): + super().__init__(target) + self.serializationType = self.WILDCARD + + def matches(self, symbol: int, minVocabSymbol: int, maxVocabSymbol: int): + return symbol >= minVocabSymbol and symbol <= maxVocabSymbol + + def __str__(self): + return "." + + +class PrecedencePredicateTransition(AbstractPredicateTransition): + __slots__ = ("serializationType", "precedence") + + def __init__(self, target: ATNState, precedence: int): + super().__init__(target) + self.serializationType = self.PRECEDENCE + self.precedence = precedence + self.isEpsilon = True + + def matches(self, symbol: int, minVocabSymbol: int, maxVocabSymbol: int): + return False + + def getPredicate(self): + return PrecedencePredicate(self.precedence) + + def __str__(self): + return self.precedence + " >= _p" + + +Transition.serializationTypes = { + EpsilonTransition: Transition.EPSILON, + RangeTransition: Transition.RANGE, + RuleTransition: Transition.RULE, + PredicateTransition: Transition.PREDICATE, + AtomTransition: Transition.ATOM, + ActionTransition: Transition.ACTION, + SetTransition: Transition.SET, + NotSetTransition: Transition.NOT_SET, + WildcardTransition: Transition.WILDCARD, + PrecedencePredicateTransition: Transition.PRECEDENCE, +} + +del ATNState +del RuleStartState + +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import * diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/__init__.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/__init__.py new file mode 100644 index 00000000..80612724 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/__init__.py @@ -0,0 +1 @@ +__author__ = "ericvergnaud" diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFA.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFA.py new file mode 100644 index 00000000..710c0e09 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFA.py @@ -0,0 +1,150 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfigSet import ( + ATNConfigSet, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( + DecisionState, + StarLoopEntryState, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFAState import ( + DFAState, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + IllegalStateException, +) + + +class DFA: + __slots__ = ("atnStartState", "decision", "_states", "s0", "precedenceDfa") + + def __init__(self, atnStartState: DecisionState, decision: int = 0): + # From which ATN state did we create this DFA? + self.atnStartState = atnStartState + self.decision = decision + # A set of all DFA states. Use {@link Map} so we can get old state back + # ({@link Set} only allows you to see if it's there). + self._states = dict() + self.s0 = None + # {@code true} if this DFA is for a precedence decision; otherwise, + # {@code false}. This is the backing field for {@link #isPrecedenceDfa}, + # {@link #setPrecedenceDfa}. + self.precedenceDfa = False + + if isinstance(atnStartState, StarLoopEntryState): + if atnStartState.isPrecedenceDecision: + self.precedenceDfa = True + precedenceState = DFAState(configs=ATNConfigSet()) + precedenceState.edges = [] + precedenceState.isAcceptState = False + precedenceState.requiresFullContext = False + self.s0 = precedenceState + + # Get the start state for a specific precedence value. + # + # @param precedence The current precedence. + # @return The start state corresponding to the specified precedence, or + # {@code null} if no start state exists for the specified precedence. + # + # @throws IllegalStateException if this is not a precedence DFA. + # @see #isPrecedenceDfa() + + def getPrecedenceStartState(self, precedence: int): + if not self.precedenceDfa: + raise IllegalStateException( + "Only precedence DFAs may contain a precedence start state." + ) + + # s0.edges is never null for a precedence DFA + if precedence < 0 or precedence >= len(self.s0.edges): + return None + return self.s0.edges[precedence] + + # Set the start state for a specific precedence value. + # + # @param precedence The current precedence. + # @param startState The start state corresponding to the specified + # precedence. + # + # @throws IllegalStateException if this is not a precedence DFA. + # @see #isPrecedenceDfa() + # + def setPrecedenceStartState(self, precedence: int, startState: DFAState): + if not self.precedenceDfa: + raise IllegalStateException( + "Only precedence DFAs may contain a precedence start state." + ) + + if precedence < 0: + return + + # synchronization on s0 here is ok. when the DFA is turned into a + # precedence DFA, s0 will be initialized once and not updated again + # s0.edges is never null for a precedence DFA + if precedence >= len(self.s0.edges): + ext = [None] * (precedence + 1 - len(self.s0.edges)) + self.s0.edges.extend(ext) + self.s0.edges[precedence] = startState + + # + # Sets whether this is a precedence DFA. If the specified value differs + # from the current DFA configuration, the following actions are taken; + # otherwise no changes are made to the current DFA. + # + #
        + #
      • The {@link #states} map is cleared
      • + #
      • If {@code precedenceDfa} is {@code false}, the initial state + # {@link #s0} is set to {@code null}; otherwise, it is initialized to a new + # {@link DFAState} with an empty outgoing {@link DFAState#edges} array to + # store the start states for individual precedence values.
      • + #
      • The {@link #precedenceDfa} field is updated
      • + #
      + # + # @param precedenceDfa {@code true} if this is a precedence DFA; otherwise, + # {@code false} + + def setPrecedenceDfa(self, precedenceDfa: bool): + if self.precedenceDfa != precedenceDfa: + self._states = dict() + if precedenceDfa: + precedenceState = DFAState(configs=ATNConfigSet()) + precedenceState.edges = [] + precedenceState.isAcceptState = False + precedenceState.requiresFullContext = False + self.s0 = precedenceState + else: + self.s0 = None + self.precedenceDfa = precedenceDfa + + @property + def states(self): + return self._states + + # Return a list of all states in this DFA, ordered by state number. + def sortedStates(self): + return sorted(self._states.keys(), key=lambda state: state.stateNumber) + + def __str__(self): + return self.toString(None) + + def toString(self, literalNames: list = None, symbolicNames: list = None): + if self.s0 is None: + return "" + from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFASerializer import ( + DFASerializer, + ) + + serializer = DFASerializer(self, literalNames, symbolicNames) + return str(serializer) + + def toLexerString(self): + if self.s0 is None: + return "" + from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFASerializer import ( + LexerDFASerializer, + ) + + serializer = LexerDFASerializer(self) + return str(serializer) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFASerializer.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFASerializer.py new file mode 100644 index 00000000..66758775 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFASerializer.py @@ -0,0 +1,83 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# / + +# A DFA walker that knows how to dump them to serialized strings.#/ +from io import StringIO + +from cf_units._udunits2_parser.parser._antlr4_runtime import DFA +from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFAState import ( + DFAState, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Utils import str_list + + +class DFASerializer: + __slots__ = ("dfa", "literalNames", "symbolicNames") + + def __init__( + self, dfa: DFA, literalNames: list = None, symbolicNames: list = None + ): + self.dfa = dfa + self.literalNames = literalNames + self.symbolicNames = symbolicNames + + def __str__(self): + if self.dfa.s0 is None: + return None + with StringIO() as buf: + for s in self.dfa.sortedStates(): + n = 0 + if s.edges is not None: + n = len(s.edges) + for i in range(0, n): + t = s.edges[i] + if t is not None and t.stateNumber != 0x7FFFFFFF: + buf.write(self.getStateString(s)) + label = self.getEdgeLabel(i) + buf.write("-") + buf.write(label) + buf.write("->") + buf.write(self.getStateString(t)) + buf.write("\n") + output = buf.getvalue() + if len(output) == 0: + return None + else: + return output + + def getEdgeLabel(self, i: int): + if i == 0: + return "EOF" + if self.literalNames is not None and i <= len(self.literalNames): + return self.literalNames[i - 1] + elif self.symbolicNames is not None and i <= len(self.symbolicNames): + return self.symbolicNames[i - 1] + else: + return str(i - 1) + + def getStateString(self, s: DFAState): + n = s.stateNumber + baseStateStr = ( + (":" if s.isAcceptState else "") + + "s" + + str(n) + + ("^" if s.requiresFullContext else "") + ) + if s.isAcceptState: + if s.predicates is not None: + return baseStateStr + "=>" + str_list(s.predicates) + else: + return baseStateStr + "=>" + str(s.prediction) + else: + return baseStateStr + + +class LexerDFASerializer(DFASerializer): + def __init__(self, dfa: DFA): + super().__init__(dfa, None) + + def getEdgeLabel(self, i: int): + return "'" + chr(i) + "'" diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFAState.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFAState.py new file mode 100644 index 00000000..b58111b9 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFAState.py @@ -0,0 +1,138 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# / + +# Map a predicate to a predicted alternative.#/ +from io import StringIO + +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfigSet import ( + ATNConfigSet, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.SemanticContext import ( + SemanticContext, +) + + +class PredPrediction: + __slots__ = ("alt", "pred") + + def __init__(self, pred: SemanticContext, alt: int): + self.alt = alt + self.pred = pred + + def __str__(self): + return "(" + str(self.pred) + ", " + str(self.alt) + ")" + + +# A DFA state represents a set of possible ATN configurations. +# As Aho, Sethi, Ullman p. 117 says "The DFA uses its state +# to keep track of all possible states the ATN can be in after +# reading each input symbol. That is to say, after reading +# input a1a2..an, the DFA is in a state that represents the +# subset T of the states of the ATN that are reachable from the +# ATN's start state along some path labeled a1a2..an." +# In conventional NFA→DFA conversion, therefore, the subset T +# would be a bitset representing the set of states the +# ATN could be in. We need to track the alt predicted by each +# state as well, however. More importantly, we need to maintain +# a stack of states, tracking the closure operations as they +# jump from rule to rule, emulating rule invocations (method calls). +# I have to add a stack to simulate the proper lookahead sequences for +# the underlying LL grammar from which the ATN was derived. +# +#

      I use a set of ATNConfig objects not simple states. An ATNConfig +# is both a state (ala normal conversion) and a RuleContext describing +# the chain of rules (if any) followed to arrive at that state.

      +# +#

      A DFA state may have multiple references to a particular state, +# but with different ATN contexts (with same or different alts) +# meaning that state was reached via a different set of rule invocations.

      +# / +class DFAState: + __slots__ = ( + "stateNumber", + "configs", + "edges", + "isAcceptState", + "prediction", + "lexerActionExecutor", + "requiresFullContext", + "predicates", + ) + + def __init__( + self, stateNumber: int = -1, configs: ATNConfigSet = ATNConfigSet() + ): + self.stateNumber = stateNumber + self.configs = configs + # {@code edges[symbol]} points to target of symbol. Shift up by 1 so (-1) + # {@link Token#EOF} maps to {@code edges[0]}. + self.edges = None + self.isAcceptState = False + # if accept state, what ttype do we match or alt do we predict? + # This is set to {@link ATN#INVALID_ALT_NUMBER} when {@link #predicates}{@code !=null} or + # {@link #requiresFullContext}. + self.prediction = 0 + self.lexerActionExecutor = None + # Indicates that this state was created during SLL prediction that + # discovered a conflict between the configurations in the state. Future + # {@link ParserATNSimulator#execATN} invocations immediately jumped doing + # full context prediction if this field is true. + self.requiresFullContext = False + # During SLL parsing, this is a list of predicates associated with the + # ATN configurations of the DFA state. When we have predicates, + # {@link #requiresFullContext} is {@code false} since full context prediction evaluates predicates + # on-the-fly. If this is not null, then {@link #prediction} is + # {@link ATN#INVALID_ALT_NUMBER}. + # + #

      We only use these for non-{@link #requiresFullContext} but conflicting states. That + # means we know from the context (it's $ or we don't dip into outer + # context) that it's an ambiguity not a conflict.

      + # + #

      This list is computed by {@link ParserATNSimulator#predicateDFAState}.

      + self.predicates = None + + # Get the set of all alts mentioned by all ATN configurations in this + # DFA state. + def getAltSet(self): + if self.configs is not None: + return set(cfg.alt for cfg in self.configs) or None + return None + + def __hash__(self): + return hash(self.configs) + + # Two {@link DFAState} instances are equal if their ATN configuration sets + # are the same. This method is used to see if a state already exists. + # + #

      Because the number of alternatives and number of ATN configurations are + # finite, there is a finite number of DFA states that can be processed. + # This is necessary to show that the algorithm terminates.

      + # + #

      Cannot test the DFA state numbers here because in + # {@link ParserATNSimulator#addDFAState} we need to know if any other state + # exists that has this exact set of ATN configurations. The + # {@link #stateNumber} is irrelevant.

      + def __eq__(self, other): + # compare set of ATN configurations in this set with other + if self is other: + return True + elif not isinstance(other, DFAState): + return False + else: + return self.configs == other.configs + + def __str__(self): + with StringIO() as buf: + buf.write(str(self.stateNumber)) + buf.write(":") + buf.write(str(self.configs)) + if self.isAcceptState: + buf.write("=>") + if self.predicates is not None: + buf.write(str(self.predicates)) + else: + buf.write(str(self.prediction)) + return buf.getvalue() diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/__init__.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/__init__.py new file mode 100644 index 00000000..80612724 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/__init__.py @@ -0,0 +1 @@ +__author__ = "ericvergnaud" diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/DiagnosticErrorListener.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/DiagnosticErrorListener.py new file mode 100644 index 00000000..679a3205 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/DiagnosticErrorListener.py @@ -0,0 +1,139 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + + +# +# This implementation of {@link ANTLRErrorListener} can be used to identify +# certain potential correctness and performance problems in grammars. "Reports" +# are made by calling {@link Parser#notifyErrorListeners} with the appropriate +# message. +# +#
        +#
      • Ambiguities: These are cases where more than one path through the +# grammar can match the input.
      • +#
      • Weak context sensitivity: These are cases where full-context +# prediction resolved an SLL conflict to a unique alternative which equaled the +# minimum alternative of the SLL conflict.
      • +#
      • Strong (forced) context sensitivity: These are cases where the +# full-context prediction resolved an SLL conflict to a unique alternative, +# and the minimum alternative of the SLL conflict was found to not be +# a truly viable alternative. Two-stage parsing cannot be used for inputs where +# this situation occurs.
      • +#
      + +from io import StringIO + +from cf_units._udunits2_parser.parser._antlr4_runtime import DFA, Parser +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfigSet import ( + ATNConfigSet, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error.ErrorListener import ( + ErrorListener, +) + + +class DiagnosticErrorListener(ErrorListener): + def __init__(self, exactOnly: bool = True): + # whether all ambiguities or only exact ambiguities are reported. + self.exactOnly = exactOnly + + def reportAmbiguity( + self, + recognizer: Parser, + dfa: DFA, + startIndex: int, + stopIndex: int, + exact: bool, + ambigAlts: set, + configs: ATNConfigSet, + ): + if self.exactOnly and not exact: + return + + with StringIO() as buf: + buf.write("reportAmbiguity d=") + buf.write(self.getDecisionDescription(recognizer, dfa)) + buf.write(": ambigAlts=") + buf.write(str(self.getConflictingAlts(ambigAlts, configs))) + buf.write(", input='") + buf.write( + recognizer.getTokenStream().getText(startIndex, stopIndex) + ) + buf.write("'") + recognizer.notifyErrorListeners(buf.getvalue()) + + def reportAttemptingFullContext( + self, + recognizer: Parser, + dfa: DFA, + startIndex: int, + stopIndex: int, + conflictingAlts: set, + configs: ATNConfigSet, + ): + with StringIO() as buf: + buf.write("reportAttemptingFullContext d=") + buf.write(self.getDecisionDescription(recognizer, dfa)) + buf.write(", input='") + buf.write( + recognizer.getTokenStream().getText(startIndex, stopIndex) + ) + buf.write("'") + recognizer.notifyErrorListeners(buf.getvalue()) + + def reportContextSensitivity( + self, + recognizer: Parser, + dfa: DFA, + startIndex: int, + stopIndex: int, + prediction: int, + configs: ATNConfigSet, + ): + with StringIO() as buf: + buf.write("reportContextSensitivity d=") + buf.write(self.getDecisionDescription(recognizer, dfa)) + buf.write(", input='") + buf.write( + recognizer.getTokenStream().getText(startIndex, stopIndex) + ) + buf.write("'") + recognizer.notifyErrorListeners(buf.getvalue()) + + def getDecisionDescription(self, recognizer: Parser, dfa: DFA): + decision = dfa.decision + ruleIndex = dfa.atnStartState.ruleIndex + + ruleNames = recognizer.ruleNames + if ruleIndex < 0 or ruleIndex >= len(ruleNames): + return str(decision) + + ruleName = ruleNames[ruleIndex] + if ruleName is None or len(ruleName) == 0: + return str(decision) + + return str(decision) + " (" + ruleName + ")" + + # + # Computes the set of conflicting or ambiguous alternatives from a + # configuration set, if that information was not already provided by the + # parser. + # + # @param reportedAlts The set of conflicting or ambiguous alternatives, as + # reported by the parser. + # @param configs The conflicting or ambiguous configuration set. + # @return Returns {@code reportedAlts} if it is not {@code null}, otherwise + # returns the set of alternatives represented in {@code configs}. + # + def getConflictingAlts(self, reportedAlts: set, configs: ATNConfigSet): + if reportedAlts is not None: + return reportedAlts + + result = set() + for config in configs: + result.add(config.alt) + + return result diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/ErrorListener.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/ErrorListener.py new file mode 100644 index 00000000..1abe811f --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/ErrorListener.py @@ -0,0 +1,108 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. + +# Provides an empty default implementation of {@link ANTLRErrorListener}. The +# default implementation of each method does nothing, but can be overridden as +# necessary. + + +import sys + + +class ErrorListener: + def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e): + pass + + def reportAmbiguity( + self, recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs + ): + pass + + def reportAttemptingFullContext( + self, recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs + ): + pass + + def reportContextSensitivity( + self, recognizer, dfa, startIndex, stopIndex, prediction, configs + ): + pass + + +class ConsoleErrorListener(ErrorListener): + # + # Provides a default instance of {@link ConsoleErrorListener}. + # + INSTANCE = None + + # + # {@inheritDoc} + # + #

      + # This implementation prints messages to {@link System#err} containing the + # values of {@code line}, {@code charPositionInLine}, and {@code msg} using + # the following format.

      + # + #
      +    # line line:charPositionInLine msg
      +    # 
      + # + def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e): + print( + "line " + str(line) + ":" + str(column) + " " + msg, + file=sys.stderr, + ) + + +ConsoleErrorListener.INSTANCE = ConsoleErrorListener() + + +class ProxyErrorListener(ErrorListener): + def __init__(self, delegates): + super().__init__() + if delegates is None: + raise ReferenceError("delegates") + self.delegates = delegates + + def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e): + for delegate in self.delegates: + delegate.syntaxError( + recognizer, offendingSymbol, line, column, msg, e + ) + + def reportAmbiguity( + self, recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs + ): + for delegate in self.delegates: + delegate.reportAmbiguity( + recognizer, + dfa, + startIndex, + stopIndex, + exact, + ambigAlts, + configs, + ) + + def reportAttemptingFullContext( + self, recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs + ): + for delegate in self.delegates: + delegate.reportAttemptingFullContext( + recognizer, + dfa, + startIndex, + stopIndex, + conflictingAlts, + configs, + ) + + def reportContextSensitivity( + self, recognizer, dfa, startIndex, stopIndex, prediction, configs + ): + for delegate in self.delegates: + delegate.reportContextSensitivity( + recognizer, dfa, startIndex, stopIndex, prediction, configs + ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/ErrorStrategy.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/ErrorStrategy.py new file mode 100644 index 00000000..e79cf8fc --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/ErrorStrategy.py @@ -0,0 +1,755 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# +from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( + ATNState, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + FailedPredicateException, + InputMismatchException, + NoViableAltException, + ParseCancellationException, + RecognitionException, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.IntervalSet import ( + IntervalSet, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + +# need forward declaration +Parser = None + + +class ErrorStrategy: + def reset(self, recognizer: Parser): + pass + + def recoverInline(self, recognizer: Parser): + pass + + def recover(self, recognizer: Parser, e: RecognitionException): + pass + + def sync(self, recognizer: Parser): + pass + + def inErrorRecoveryMode(self, recognizer: Parser): + pass + + def reportError(self, recognizer: Parser, e: RecognitionException): + pass + + +# This is the default implementation of {@link ANTLRErrorStrategy} used for +# error reporting and recovery in ANTLR parsers. +# +class DefaultErrorStrategy(ErrorStrategy): + def __init__(self): + super().__init__() + # Indicates whether the error strategy is currently "recovering from an + # error". This is used to suppress reporting multiple error messages while + # attempting to recover from a detected syntax error. + # + # @see #inErrorRecoveryMode + # + self.errorRecoveryMode = False + + # The index into the input stream where the last error occurred. + # This is used to prevent infinite loops where an error is found + # but no token is consumed during recovery...another error is found, + # ad nauseum. This is a failsafe mechanism to guarantee that at least + # one token/tree node is consumed for two errors. + # + self.lastErrorIndex = -1 + self.lastErrorStates = None + self.nextTokensContext = None + self.nextTokenState = 0 + + #

      The default implementation simply calls {@link #endErrorCondition} to + # ensure that the handler is not in error recovery mode.

      + def reset(self, recognizer: Parser): + self.endErrorCondition(recognizer) + + # + # This method is called to enter error recovery mode when a recognition + # exception is reported. + # + # @param recognizer the parser instance + # + def beginErrorCondition(self, recognizer: Parser): + self.errorRecoveryMode = True + + def inErrorRecoveryMode(self, recognizer: Parser): + return self.errorRecoveryMode + + # + # This method is called to leave error recovery mode after recovering from + # a recognition exception. + # + # @param recognizer + # + def endErrorCondition(self, recognizer: Parser): + self.errorRecoveryMode = False + self.lastErrorStates = None + self.lastErrorIndex = -1 + + # + # {@inheritDoc} + # + #

      The default implementation simply calls {@link #endErrorCondition}.

      + # + def reportMatch(self, recognizer: Parser): + self.endErrorCondition(recognizer) + + # + # {@inheritDoc} + # + #

      The default implementation returns immediately if the handler is already + # in error recovery mode. Otherwise, it calls {@link #beginErrorCondition} + # and dispatches the reporting task based on the runtime type of {@code e} + # according to the following table.

      + # + #
        + #
      • {@link NoViableAltException}: Dispatches the call to + # {@link #reportNoViableAlternative}
      • + #
      • {@link InputMismatchException}: Dispatches the call to + # {@link #reportInputMismatch}
      • + #
      • {@link FailedPredicateException}: Dispatches the call to + # {@link #reportFailedPredicate}
      • + #
      • All other types: calls {@link Parser#notifyErrorListeners} to report + # the exception
      • + #
      + # + def reportError(self, recognizer: Parser, e: RecognitionException): + # if we've already reported an error and have not matched a token + # yet successfully, don't report any errors. + if self.inErrorRecoveryMode(recognizer): + return # don't report spurious errors + self.beginErrorCondition(recognizer) + if isinstance(e, NoViableAltException): + self.reportNoViableAlternative(recognizer, e) + elif isinstance(e, InputMismatchException): + self.reportInputMismatch(recognizer, e) + elif isinstance(e, FailedPredicateException): + self.reportFailedPredicate(recognizer, e) + else: + print("unknown recognition error type: " + type(e).__name__) + recognizer.notifyErrorListeners(e.message, e.offendingToken, e) + + # + # {@inheritDoc} + # + #

      The default implementation resynchronizes the parser by consuming tokens + # until we find one in the resynchronization set--loosely the set of tokens + # that can follow the current rule.

      + # + def recover(self, recognizer: Parser, e: RecognitionException): + if ( + self.lastErrorIndex == recognizer.getInputStream().index + and self.lastErrorStates is not None + and recognizer.state in self.lastErrorStates + ): + # uh oh, another error at same token index and previously-visited + # state in ATN; must be a case where LT(1) is in the recovery + # token set so nothing got consumed. Consume a single token + # at least to prevent an infinite loop; this is a failsafe. + recognizer.consume() + + self.lastErrorIndex = recognizer._input.index + if self.lastErrorStates is None: + self.lastErrorStates = [] + self.lastErrorStates.append(recognizer.state) + followSet = self.getErrorRecoverySet(recognizer) + self.consumeUntil(recognizer, followSet) + + # The default implementation of {@link ANTLRErrorStrategy#sync} makes sure + # that the current lookahead symbol is consistent with what were expecting + # at this point in the ATN. You can call this anytime but ANTLR only + # generates code to check before subrules/loops and each iteration. + # + #

      Implements Jim Idle's magic sync mechanism in closures and optional + # subrules. E.g.,

      + # + #
      +    # a : sync ( stuff sync )* ;
      +    # sync : {consume to what can follow sync} ;
      +    # 
      + # + # At the start of a sub rule upon error, {@link #sync} performs single + # token deletion, if possible. If it can't do that, it bails on the current + # rule and uses the default error recovery, which consumes until the + # resynchronization set of the current rule. + # + #

      If the sub rule is optional ({@code (...)?}, {@code (...)*}, or block + # with an empty alternative), then the expected set includes what follows + # the subrule.

      + # + #

      During loop iteration, it consumes until it sees a token that can start a + # sub rule or what follows loop. Yes, that is pretty aggressive. We opt to + # stay in the loop as long as possible.

      + # + #

      ORIGINS

      + # + #

      Previous versions of ANTLR did a poor job of their recovery within loops. + # A single mismatch token or missing token would force the parser to bail + # out of the entire rules surrounding the loop. So, for rule

      + # + #
      +    # classDef : 'class' ID '{' member* '}'
      +    # 
      + # + # input with an extra token between members would force the parser to + # consume until it found the next class definition rather than the next + # member definition of the current class. + # + #

      This functionality cost a little bit of effort because the parser has to + # compare token set at the start of the loop and at each iteration. If for + # some reason speed is suffering for you, you can turn off this + # functionality by simply overriding this method as a blank { }.

      + # + def sync(self, recognizer: Parser): + # If already recovering, don't try to sync + if self.inErrorRecoveryMode(recognizer): + return + + s = recognizer._interp.atn.states[recognizer.state] + la = recognizer.getTokenStream().LA(1) + # try cheaper subset first; might get lucky. seems to shave a wee bit off + nextTokens = recognizer.atn.nextTokens(s) + if la in nextTokens: + self.nextTokensContext = None + self.nextTokenState = ATNState.INVALID_STATE_NUMBER + return + elif Token.EPSILON in nextTokens: + if self.nextTokensContext is None: + # It's possible the next token won't match information tracked + # by sync is restricted for performance. + self.nextTokensContext = recognizer._ctx + self.nextTokensState = recognizer._stateNumber + return + + if s.stateType in [ + ATNState.BLOCK_START, + ATNState.STAR_BLOCK_START, + ATNState.PLUS_BLOCK_START, + ATNState.STAR_LOOP_ENTRY, + ]: + # report error and recover if possible + if self.singleTokenDeletion(recognizer) is not None: + return + else: + raise InputMismatchException(recognizer) + + elif s.stateType in [ATNState.PLUS_LOOP_BACK, ATNState.STAR_LOOP_BACK]: + self.reportUnwantedToken(recognizer) + expecting = recognizer.getExpectedTokens() + whatFollowsLoopIterationOrRule = expecting.addSet( + self.getErrorRecoverySet(recognizer) + ) + self.consumeUntil(recognizer, whatFollowsLoopIterationOrRule) + + else: + # do nothing if we can't identify the exact kind of ATN state + pass + + # This is called by {@link #reportError} when the exception is a + # {@link NoViableAltException}. + # + # @see #reportError + # + # @param recognizer the parser instance + # @param e the recognition exception + # + def reportNoViableAlternative( + self, recognizer: Parser, e: NoViableAltException + ): + tokens = recognizer.getTokenStream() + if tokens is not None: + if e.startToken.type == Token.EOF: + input = "" + else: + input = tokens.getText(e.startToken, e.offendingToken) + else: + input = "" + msg = "no viable alternative at input " + self.escapeWSAndQuote(input) + recognizer.notifyErrorListeners(msg, e.offendingToken, e) + + # + # This is called by {@link #reportError} when the exception is an + # {@link InputMismatchException}. + # + # @see #reportError + # + # @param recognizer the parser instance + # @param e the recognition exception + # + def reportInputMismatch( + self, recognizer: Parser, e: InputMismatchException + ): + msg = ( + "mismatched input " + + self.getTokenErrorDisplay(e.offendingToken) + + " expecting " + + e.getExpectedTokens().toString( + recognizer.literalNames, recognizer.symbolicNames + ) + ) + recognizer.notifyErrorListeners(msg, e.offendingToken, e) + + # + # This is called by {@link #reportError} when the exception is a + # {@link FailedPredicateException}. + # + # @see #reportError + # + # @param recognizer the parser instance + # @param e the recognition exception + # + def reportFailedPredicate(self, recognizer, e): + ruleName = recognizer.ruleNames[recognizer._ctx.getRuleIndex()] + msg = "rule " + ruleName + " " + e.message + recognizer.notifyErrorListeners(msg, e.offendingToken, e) + + # This method is called to report a syntax error which requires the removal + # of a token from the input stream. At the time this method is called, the + # erroneous symbol is current {@code LT(1)} symbol and has not yet been + # removed from the input stream. When this method returns, + # {@code recognizer} is in error recovery mode. + # + #

      This method is called when {@link #singleTokenDeletion} identifies + # single-token deletion as a viable recovery strategy for a mismatched + # input error.

      + # + #

      The default implementation simply returns if the handler is already in + # error recovery mode. Otherwise, it calls {@link #beginErrorCondition} to + # enter error recovery mode, followed by calling + # {@link Parser#notifyErrorListeners}.

      + # + # @param recognizer the parser instance + # + def reportUnwantedToken(self, recognizer: Parser): + if self.inErrorRecoveryMode(recognizer): + return + + self.beginErrorCondition(recognizer) + t = recognizer.getCurrentToken() + tokenName = self.getTokenErrorDisplay(t) + expecting = self.getExpectedTokens(recognizer) + msg = ( + "extraneous input " + + tokenName + + " expecting " + + expecting.toString( + recognizer.literalNames, recognizer.symbolicNames + ) + ) + recognizer.notifyErrorListeners(msg, t, None) + + # This method is called to report a syntax error which requires the + # insertion of a missing token into the input stream. At the time this + # method is called, the missing token has not yet been inserted. When this + # method returns, {@code recognizer} is in error recovery mode. + # + #

      This method is called when {@link #singleTokenInsertion} identifies + # single-token insertion as a viable recovery strategy for a mismatched + # input error.

      + # + #

      The default implementation simply returns if the handler is already in + # error recovery mode. Otherwise, it calls {@link #beginErrorCondition} to + # enter error recovery mode, followed by calling + # {@link Parser#notifyErrorListeners}.

      + # + # @param recognizer the parser instance + # + def reportMissingToken(self, recognizer: Parser): + if self.inErrorRecoveryMode(recognizer): + return + self.beginErrorCondition(recognizer) + t = recognizer.getCurrentToken() + expecting = self.getExpectedTokens(recognizer) + msg = ( + "missing " + + expecting.toString( + recognizer.literalNames, recognizer.symbolicNames + ) + + " at " + + self.getTokenErrorDisplay(t) + ) + recognizer.notifyErrorListeners(msg, t, None) + + #

      The default implementation attempts to recover from the mismatched input + # by using single token insertion and deletion as described below. If the + # recovery attempt fails, this method throws an + # {@link InputMismatchException}.

      + # + #

      EXTRA TOKEN (single token deletion)

      + # + #

      {@code LA(1)} is not what we are looking for. If {@code LA(2)} has the + # right token, however, then assume {@code LA(1)} is some extra spurious + # token and delete it. Then consume and return the next token (which was + # the {@code LA(2)} token) as the successful result of the match operation.

      + # + #

      This recovery strategy is implemented by {@link #singleTokenDeletion}.

      + # + #

      MISSING TOKEN (single token insertion)

      + # + #

      If current token (at {@code LA(1)}) is consistent with what could come + # after the expected {@code LA(1)} token, then assume the token is missing + # and use the parser's {@link TokenFactory} to create it on the fly. The + # "insertion" is performed by returning the created token as the successful + # result of the match operation.

      + # + #

      This recovery strategy is implemented by {@link #singleTokenInsertion}.

      + # + #

      EXAMPLE

      + # + #

      For example, Input {@code i=(3;} is clearly missing the {@code ')'}. When + # the parser returns from the nested call to {@code expr}, it will have + # call chain:

      + # + #
      +    # stat → expr → atom
      +    # 
      + # + # and it will be trying to match the {@code ')'} at this point in the + # derivation: + # + #
      +    # => ID '=' '(' INT ')' ('+' atom)* ';'
      +    #                    ^
      +    # 
      + # + # The attempt to match {@code ')'} will fail when it sees {@code ';'} and + # call {@link #recoverInline}. To recover, it sees that {@code LA(1)==';'} + # is in the set of tokens that can follow the {@code ')'} token reference + # in rule {@code atom}. It can assume that you forgot the {@code ')'}. + # + def recoverInline(self, recognizer: Parser): + # SINGLE TOKEN DELETION + matchedSymbol = self.singleTokenDeletion(recognizer) + if matchedSymbol is not None: + # we have deleted the extra token. + # now, move past ttype token as if all were ok + recognizer.consume() + return matchedSymbol + + # SINGLE TOKEN INSERTION + if self.singleTokenInsertion(recognizer): + return self.getMissingSymbol(recognizer) + + # even that didn't work; must throw the exception + raise InputMismatchException(recognizer) + + # + # This method implements the single-token insertion inline error recovery + # strategy. It is called by {@link #recoverInline} if the single-token + # deletion strategy fails to recover from the mismatched input. If this + # method returns {@code true}, {@code recognizer} will be in error recovery + # mode. + # + #

      This method determines whether or not single-token insertion is viable by + # checking if the {@code LA(1)} input symbol could be successfully matched + # if it were instead the {@code LA(2)} symbol. If this method returns + # {@code true}, the caller is responsible for creating and inserting a + # token with the correct type to produce this behavior.

      + # + # @param recognizer the parser instance + # @return {@code true} if single-token insertion is a viable recovery + # strategy for the current mismatched input, otherwise {@code false} + # + def singleTokenInsertion(self, recognizer: Parser): + currentSymbolType = recognizer.getTokenStream().LA(1) + # if current token is consistent with what could come after current + # ATN state, then we know we're missing a token; error recovery + # is free to conjure up and insert the missing token + atn = recognizer._interp.atn + currentState = atn.states[recognizer.state] + next = currentState.transitions[0].target + expectingAtLL2 = atn.nextTokens(next, recognizer._ctx) + if currentSymbolType in expectingAtLL2: + self.reportMissingToken(recognizer) + return True + else: + return False + + # This method implements the single-token deletion inline error recovery + # strategy. It is called by {@link #recoverInline} to attempt to recover + # from mismatched input. If this method returns null, the parser and error + # handler state will not have changed. If this method returns non-null, + # {@code recognizer} will not be in error recovery mode since the + # returned token was a successful match. + # + #

      If the single-token deletion is successful, this method calls + # {@link #reportUnwantedToken} to report the error, followed by + # {@link Parser#consume} to actually "delete" the extraneous token. Then, + # before returning {@link #reportMatch} is called to signal a successful + # match.

      + # + # @param recognizer the parser instance + # @return the successfully matched {@link Token} instance if single-token + # deletion successfully recovers from the mismatched input, otherwise + # {@code null} + # + def singleTokenDeletion(self, recognizer: Parser): + nextTokenType = recognizer.getTokenStream().LA(2) + expecting = self.getExpectedTokens(recognizer) + if nextTokenType in expecting: + self.reportUnwantedToken(recognizer) + # print("recoverFromMismatchedToken deleting " \ + # + str(recognizer.getTokenStream().LT(1)) \ + # + " since " + str(recognizer.getTokenStream().LT(2)) \ + # + " is what we want", file=sys.stderr) + recognizer.consume() # simply delete extra token + # we want to return the token we're actually matching + matchedSymbol = recognizer.getCurrentToken() + self.reportMatch(recognizer) # we know current token is correct + return matchedSymbol + else: + return None + + # Conjure up a missing token during error recovery. + # + # The recognizer attempts to recover from single missing + # symbols. But, actions might refer to that missing symbol. + # For example, x=ID {f($x);}. The action clearly assumes + # that there has been an identifier matched previously and that + # $x points at that token. If that token is missing, but + # the next token in the stream is what we want we assume that + # this token is missing and we keep going. Because we + # have to return some token to replace the missing token, + # we have to conjure one up. This method gives the user control + # over the tokens returned for missing tokens. Mostly, + # you will want to create something special for identifier + # tokens. For literals such as '{' and ',', the default + # action in the parser or tree parser works. It simply creates + # a CommonToken of the appropriate type. The text will be the token. + # If you change what tokens must be created by the lexer, + # override this method to create the appropriate tokens. + # + def getMissingSymbol(self, recognizer: Parser): + currentSymbol = recognizer.getCurrentToken() + expecting = self.getExpectedTokens(recognizer) + expectedTokenType = expecting[0] # get any element + if expectedTokenType == Token.EOF: + tokenText = "" + else: + name = None + if expectedTokenType < len(recognizer.literalNames): + name = recognizer.literalNames[expectedTokenType] + if name is None and expectedTokenType < len( + recognizer.symbolicNames + ): + name = recognizer.symbolicNames[expectedTokenType] + tokenText = "" + current = currentSymbol + lookback = recognizer.getTokenStream().LT(-1) + if current.type == Token.EOF and lookback is not None: + current = lookback + return recognizer.getTokenFactory().create( + current.source, + expectedTokenType, + tokenText, + Token.DEFAULT_CHANNEL, + -1, + -1, + current.line, + current.column, + ) + + def getExpectedTokens(self, recognizer: Parser): + return recognizer.getExpectedTokens() + + # How should a token be displayed in an error message? The default + # is to display just the text, but during development you might + # want to have a lot of information spit out. Override in that case + # to use t.toString() (which, for CommonToken, dumps everything about + # the token). This is better than forcing you to override a method in + # your token objects because you don't have to go modify your lexer + # so that it creates a new Java type. + # + def getTokenErrorDisplay(self, t: Token): + if t is None: + return "" + s = t.text + if s is None: + if t.type == Token.EOF: + s = "" + else: + s = "<" + str(t.type) + ">" + return self.escapeWSAndQuote(s) + + def escapeWSAndQuote(self, s: str): + s = s.replace("\n", "\\n") + s = s.replace("\r", "\\r") + s = s.replace("\t", "\\t") + return "'" + s + "'" + + # Compute the error recovery set for the current rule. During + # rule invocation, the parser pushes the set of tokens that can + # follow that rule reference on the stack; this amounts to + # computing FIRST of what follows the rule reference in the + # enclosing rule. See LinearApproximator.FIRST(). + # This local follow set only includes tokens + # from within the rule; i.e., the FIRST computation done by + # ANTLR stops at the end of a rule. + # + # EXAMPLE + # + # When you find a "no viable alt exception", the input is not + # consistent with any of the alternatives for rule r. The best + # thing to do is to consume tokens until you see something that + # can legally follow a call to r#or* any rule that called r. + # You don't want the exact set of viable next tokens because the + # input might just be missing a token--you might consume the + # rest of the input looking for one of the missing tokens. + # + # Consider grammar: + # + # a : '[' b ']' + # | '(' b ')' + # ; + # b : c '^' INT ; + # c : ID + # | INT + # ; + # + # At each rule invocation, the set of tokens that could follow + # that rule is pushed on a stack. Here are the various + # context-sensitive follow sets: + # + # FOLLOW(b1_in_a) = FIRST(']') = ']' + # FOLLOW(b2_in_a) = FIRST(')') = ')' + # FOLLOW(c_in_b) = FIRST('^') = '^' + # + # Upon erroneous input "[]", the call chain is + # + # a -> b -> c + # + # and, hence, the follow context stack is: + # + # depth follow set start of rule execution + # 0 a (from main()) + # 1 ']' b + # 2 '^' c + # + # Notice that ')' is not included, because b would have to have + # been called from a different context in rule a for ')' to be + # included. + # + # For error recovery, we cannot consider FOLLOW(c) + # (context-sensitive or otherwise). We need the combined set of + # all context-sensitive FOLLOW sets--the set of all tokens that + # could follow any reference in the call chain. We need to + # resync to one of those tokens. Note that FOLLOW(c)='^' and if + # we resync'd to that token, we'd consume until EOF. We need to + # sync to context-sensitive FOLLOWs for a, b, and c: {']','^'}. + # In this case, for input "[]", LA(1) is ']' and in the set, so we would + # not consume anything. After printing an error, rule c would + # return normally. Rule b would not find the required '^' though. + # At this point, it gets a mismatched token error and throws an + # exception (since LA(1) is not in the viable following token + # set). The rule exception handler tries to recover, but finds + # the same recovery set and doesn't consume anything. Rule b + # exits normally returning to rule a. Now it finds the ']' (and + # with the successful match exits errorRecovery mode). + # + # So, you can see that the parser walks up the call chain looking + # for the token that was a member of the recovery set. + # + # Errors are not generated in errorRecovery mode. + # + # ANTLR's error recovery mechanism is based upon original ideas: + # + # "Algorithms + Data Structures = Programs" by Niklaus Wirth + # + # and + # + # "A note on error recovery in recursive descent parsers": + # http:#portal.acm.org/citation.cfm?id=947902.947905 + # + # Later, Josef Grosch had some good ideas: + # + # "Efficient and Comfortable Error Recovery in Recursive Descent + # Parsers": + # ftp:#www.cocolab.com/products/cocktail/doca4.ps/ell.ps.zip + # + # Like Grosch I implement context-sensitive FOLLOW sets that are combined + # at run-time upon error to avoid overhead during parsing. + # + def getErrorRecoverySet(self, recognizer: Parser): + atn = recognizer._interp.atn + ctx = recognizer._ctx + recoverSet = IntervalSet() + while ctx is not None and ctx.invokingState >= 0: + # compute what follows who invoked us + invokingState = atn.states[ctx.invokingState] + rt = invokingState.transitions[0] + follow = atn.nextTokens(rt.followState) + recoverSet.addSet(follow) + ctx = ctx.parentCtx + recoverSet.removeOne(Token.EPSILON) + return recoverSet + + # Consume tokens until one matches the given token set.# + def consumeUntil(self, recognizer: Parser, set_: set): + ttype = recognizer.getTokenStream().LA(1) + while ttype != Token.EOF and ttype not in set_: + recognizer.consume() + ttype = recognizer.getTokenStream().LA(1) + + +# +# This implementation of {@link ANTLRErrorStrategy} responds to syntax errors +# by immediately canceling the parse operation with a +# {@link ParseCancellationException}. The implementation ensures that the +# {@link ParserRuleContext#exception} field is set for all parse tree nodes +# that were not completed prior to encountering the error. +# +#

      +# This error strategy is useful in the following scenarios.

      +# +#
        +#
      • Two-stage parsing: This error strategy allows the first +# stage of two-stage parsing to immediately terminate if an error is +# encountered, and immediately fall back to the second stage. In addition to +# avoiding wasted work by attempting to recover from errors here, the empty +# implementation of {@link BailErrorStrategy#sync} improves the performance of +# the first stage.
      • +#
      • Silent validation: When syntax errors are not being +# reported or logged, and the parse result is simply ignored if errors occur, +# the {@link BailErrorStrategy} avoids wasting work on recovering from errors +# when the result will be ignored either way.
      • +#
      +# +#

      +# {@code myparser.setErrorHandler(new BailErrorStrategy());}

      +# +# @see Parser#setErrorHandler(ANTLRErrorStrategy) +# +class BailErrorStrategy(DefaultErrorStrategy): + # Instead of recovering from exception {@code e}, re-throw it wrapped + # in a {@link ParseCancellationException} so it is not caught by the + # rule function catches. Use {@link Exception#getCause()} to get the + # original {@link RecognitionException}. + # + def recover(self, recognizer: Parser, e: RecognitionException): + context = recognizer._ctx + while context is not None: + context.exception = e + context = context.parentCtx + raise ParseCancellationException(e) + + # Make sure we don't attempt to recover inline; if the parser + # successfully recovers, it won't throw an exception. + # + def recoverInline(self, recognizer: Parser): + self.recover(recognizer, InputMismatchException(recognizer)) + + # Make sure we don't attempt to recover from problems in subrules.# + def sync(self, recognizer: Parser): + pass + + +del Parser diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/Errors.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/Errors.py new file mode 100644 index 00000000..051a231b --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/Errors.py @@ -0,0 +1,213 @@ +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +# need forward declaration +Token = None +Lexer = None +Parser = None +TokenStream = None +ATNConfigSet = None +ParserRulecontext = None +PredicateTransition = None +BufferedTokenStream = None + + +class UnsupportedOperationException(Exception): + def __init__(self, msg: str): + super().__init__(msg) + + +class IllegalStateException(Exception): + def __init__(self, msg: str): + super().__init__(msg) + + +class CancellationException(IllegalStateException): + def __init__(self, msg: str): + super().__init__(msg) + + +# The root of the ANTLR exception hierarchy. In general, ANTLR tracks just +# 3 kinds of errors: prediction errors, failed predicate errors, and +# mismatched input errors. In each case, the parser knows where it is +# in the input, where it is in the ATN, the rule invocation stack, +# and what kind of problem occurred. + +from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( + InputStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.ParserRuleContext import ( + ParserRuleContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Recognizer import ( + Recognizer, +) + + +class RecognitionException(Exception): + def __init__( + self, + message: str = None, + recognizer: Recognizer = None, + input: InputStream = None, + ctx: ParserRulecontext = None, + ): + super().__init__(message) + self.message = message + self.recognizer = recognizer + self.input = input + self.ctx = ctx + # The current {@link Token} when an error occurred. Since not all streams + # support accessing symbols by index, we have to track the {@link Token} + # instance itself. + self.offendingToken = None + # Get the ATN state number the parser was in at the time the error + # occurred. For {@link NoViableAltException} and + # {@link LexerNoViableAltException} exceptions, this is the + # {@link DecisionState} number. For others, it is the state whose outgoing + # edge we couldn't match. + self.offendingState = -1 + if recognizer is not None: + self.offendingState = recognizer.state + + #

      If the state number is not known, this method returns -1.

      + + # + # Gets the set of input symbols which could potentially follow the + # previously matched symbol at the time this exception was thrown. + # + #

      If the set of expected tokens is not known and could not be computed, + # this method returns {@code null}.

      + # + # @return The set of token types that could potentially follow the current + # state in the ATN, or {@code null} if the information is not available. + # / + def getExpectedTokens(self): + if self.recognizer is not None: + return self.recognizer.atn.getExpectedTokens( + self.offendingState, self.ctx + ) + else: + return None + + +class LexerNoViableAltException(RecognitionException): + def __init__( + self, + lexer: Lexer, + input: InputStream, + startIndex: int, + deadEndConfigs: ATNConfigSet, + ): + super().__init__(message=None, recognizer=lexer, input=input, ctx=None) + self.startIndex = startIndex + self.deadEndConfigs = deadEndConfigs + self.message = "" + + def __str__(self): + symbol = "" + if self.startIndex >= 0 and self.startIndex < self.input.size: + symbol = self.input.getText(self.startIndex, self.startIndex) + # TODO symbol = Utils.escapeWhitespace(symbol, false); + return "LexerNoViableAltException('" + symbol + "')" + + +# Indicates that the parser could not decide which of two or more paths +# to take based upon the remaining input. It tracks the starting token +# of the offending input and also knows where the parser was +# in the various paths when the error. Reported by reportNoViableAlternative() +# +class NoViableAltException(RecognitionException): + def __init__( + self, + recognizer: Parser, + input: TokenStream = None, + startToken: Token = None, + offendingToken: Token = None, + deadEndConfigs: ATNConfigSet = None, + ctx: ParserRuleContext = None, + ): + if ctx is None: + ctx = recognizer._ctx + if offendingToken is None: + offendingToken = recognizer.getCurrentToken() + if startToken is None: + startToken = recognizer.getCurrentToken() + if input is None: + input = recognizer.getInputStream() + super().__init__(recognizer=recognizer, input=input, ctx=ctx) + # Which configurations did we try at input.index() that couldn't match input.LT(1)?# + self.deadEndConfigs = deadEndConfigs + # The token object at the start index; the input stream might + # not be buffering tokens so get a reference to it. (At the + # time the error occurred, of course the stream needs to keep a + # buffer all of the tokens but later we might not have access to those.) + self.startToken = startToken + self.offendingToken = offendingToken + + +# This signifies any kind of mismatched input exceptions such as +# when the current input does not match the expected token. +# +class InputMismatchException(RecognitionException): + def __init__(self, recognizer: Parser): + super().__init__( + recognizer=recognizer, + input=recognizer.getInputStream(), + ctx=recognizer._ctx, + ) + self.offendingToken = recognizer.getCurrentToken() + + +# A semantic predicate failed during validation. Validation of predicates +# occurs when normally parsing the alternative just like matching a token. +# Disambiguating predicate evaluation occurs when we test a predicate during +# prediction. + + +class FailedPredicateException(RecognitionException): + def __init__( + self, recognizer: Parser, predicate: str = None, message: str = None + ): + super().__init__( + message=self.formatMessage(predicate, message), + recognizer=recognizer, + input=recognizer.getInputStream(), + ctx=recognizer._ctx, + ) + s = recognizer._interp.atn.states[recognizer.state] + trans = s.transitions[0] + from cf_units._udunits2_parser.parser._antlr4_runtime.atn.Transition import ( + PredicateTransition, + ) + + if isinstance(trans, PredicateTransition): + self.ruleIndex = trans.ruleIndex + self.predicateIndex = trans.predIndex + else: + self.ruleIndex = 0 + self.predicateIndex = 0 + self.predicate = predicate + self.offendingToken = recognizer.getCurrentToken() + + def formatMessage(self, predicate: str, message: str): + if message is not None: + return message + else: + return "failed predicate: {" + predicate + "}?" + + +class ParseCancellationException(CancellationException): + pass + + +del Token +del Lexer +del Parser +del TokenStream +del ATNConfigSet +del ParserRulecontext +del PredicateTransition +del BufferedTokenStream diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/__init__.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/__init__.py new file mode 100644 index 00000000..80612724 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/__init__.py @@ -0,0 +1 @@ +__author__ = "ericvergnaud" diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Chunk.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Chunk.py new file mode 100644 index 00000000..1a16a123 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Chunk.py @@ -0,0 +1,33 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + + +class Chunk: + pass + + +class TagChunk(Chunk): + __slots__ = ("tag", "label") + + def __init__(self, tag: str, label: str = None): + self.tag = tag + self.label = label + + def __str__(self): + if self.label is None: + return self.tag + else: + return self.label + ":" + self.tag + + +class TextChunk(Chunk): + __slots__ = "text" + + def __init__(self, text: str): + self.text = text + + def __str__(self): + return "'" + self.text + "'" diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreeMatch.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreeMatch.py new file mode 100644 index 00000000..570effeb --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreeMatch.py @@ -0,0 +1,129 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + + +# +# Represents the result of matching a {@link ParseTree} against a tree pattern. +# +from io import StringIO + +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.ParseTreePattern import ( + ParseTreePattern, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( + ParseTree, +) + + +class ParseTreeMatch: + __slots__ = ("tree", "pattern", "labels", "mismatchedNode") + + # + # Constructs a new instance of {@link ParseTreeMatch} from the specified + # parse tree and pattern. + # + # @param tree The parse tree to match against the pattern. + # @param pattern The parse tree pattern. + # @param labels A mapping from label names to collections of + # {@link ParseTree} objects located by the tree pattern matching process. + # @param mismatchedNode The first node which failed to match the tree + # pattern during the matching process. + # + # @exception IllegalArgumentException if {@code tree} is {@code null} + # @exception IllegalArgumentException if {@code pattern} is {@code null} + # @exception IllegalArgumentException if {@code labels} is {@code null} + # + def __init__( + self, + tree: ParseTree, + pattern: ParseTreePattern, + labels: dict, + mismatchedNode: ParseTree, + ): + if tree is None: + raise Exception("tree cannot be null") + if pattern is None: + raise Exception("pattern cannot be null") + if labels is None: + raise Exception("labels cannot be null") + self.tree = tree + self.pattern = pattern + self.labels = labels + self.mismatchedNode = mismatchedNode + + # + # Get the last node associated with a specific {@code label}. + # + #

      For example, for pattern {@code }, {@code get("id")} returns the + # node matched for that {@code ID}. If more than one node + # matched the specified label, only the last is returned. If there is + # no node associated with the label, this returns {@code null}.

      + # + #

      Pattern tags like {@code } and {@code } without labels are + # considered to be labeled with {@code ID} and {@code expr}, respectively.

      + # + # @param label The label to check. + # + # @return The last {@link ParseTree} to match a tag with the specified + # label, or {@code null} if no parse tree matched a tag with the label. + # + def get(self, label: str): + parseTrees = self.labels.get(label, None) + if parseTrees is None or len(parseTrees) == 0: + return None + else: + return parseTrees[len(parseTrees) - 1] + + # + # Return all nodes matching a rule or token tag with the specified label. + # + #

      If the {@code label} is the name of a parser rule or token in the + # grammar, the resulting list will contain both the parse trees matching + # rule or tags explicitly labeled with the label and the complete set of + # parse trees matching the labeled and unlabeled tags in the pattern for + # the parser rule or token. For example, if {@code label} is {@code "foo"}, + # the result will contain all of the following.

      + # + #
        + #
      • Parse tree nodes matching tags of the form {@code } and + # {@code }.
      • + #
      • Parse tree nodes matching tags of the form {@code }.
      • + #
      • Parse tree nodes matching tags of the form {@code }.
      • + #
      + # + # @param label The label. + # + # @return A collection of all {@link ParseTree} nodes matching tags with + # the specified {@code label}. If no nodes matched the label, an empty list + # is returned. + # + def getAll(self, label: str): + nodes = self.labels.get(label, None) + if nodes is None: + return list() + else: + return nodes + + # + # Gets a value indicating whether the match operation succeeded. + # + # @return {@code true} if the match operation succeeded; otherwise, + # {@code false}. + # + def succeeded(self): + return self.mismatchedNode is None + + # + # {@inheritDoc} + # + def __str__(self): + with StringIO() as buf: + buf.write("Match ") + buf.write("succeeded" if self.succeeded() else "failed") + buf.write("; found ") + buf.write(str(len(self.labels))) + buf.write(" labels") + return buf.getvalue() diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreePattern.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreePattern.py new file mode 100644 index 00000000..622c8d96 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreePattern.py @@ -0,0 +1,81 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +# +# A pattern like {@code = ;} converted to a {@link ParseTree} by +# {@link ParseTreePatternMatcher#compile(String, int)}. +# +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.ParseTreePatternMatcher import ( + ParseTreePatternMatcher, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( + ParseTree, +) + + +class ParseTreePattern: + __slots__ = ("matcher", "patternRuleIndex", "pattern", "patternTree") + + # Construct a new instance of the {@link ParseTreePattern} class. + # + # @param matcher The {@link ParseTreePatternMatcher} which created this + # tree pattern. + # @param pattern The tree pattern in concrete syntax form. + # @param patternRuleIndex The parser rule which serves as the root of the + # tree pattern. + # @param patternTree The tree pattern in {@link ParseTree} form. + # + def __init__( + self, + matcher: ParseTreePatternMatcher, + pattern: str, + patternRuleIndex: int, + patternTree: ParseTree, + ): + self.matcher = matcher + self.patternRuleIndex = patternRuleIndex + self.pattern = pattern + self.patternTree = patternTree + + # + # Match a specific parse tree against this tree pattern. + # + # @param tree The parse tree to match against this tree pattern. + # @return A {@link ParseTreeMatch} object describing the result of the + # match operation. The {@link ParseTreeMatch#succeeded()} method can be + # used to determine whether or not the match was successful. + # + def match(self, tree: ParseTree): + return self.matcher.match(tree, self) + + # + # Determine whether or not a parse tree matches this tree pattern. + # + # @param tree The parse tree to match against this tree pattern. + # @return {@code true} if {@code tree} is a match for the current tree + # pattern; otherwise, {@code false}. + # + def matches(self, tree: ParseTree): + return self.matcher.match(tree, self).succeeded() + + # Find all nodes using XPath and then try to match those subtrees against + # this tree pattern. + # + # @param tree The {@link ParseTree} to match against this pattern. + # @param xpath An expression matching the nodes + # + # @return A collection of {@link ParseTreeMatch} objects describing the + # successful matches. Unsuccessful matches are omitted from the result, + # regardless of the reason for the failure. + # + def findAll(self, tree: ParseTree, xpath: str): + subtrees = XPath.findAll(tree, xpath, self.matcher.parser) + matches = list() + for t in subtrees: + match = self.match(t) + if match.succeeded(): + matches.append(match) + return matches diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreePatternMatcher.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreePatternMatcher.py new file mode 100644 index 00000000..bc440c83 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreePatternMatcher.py @@ -0,0 +1,452 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +# +# A tree pattern matching mechanism for ANTLR {@link ParseTree}s. +# +#

      Patterns are strings of source input text with special tags representing +# token or rule references such as:

      +# +#

      {@code = ;}

      +# +#

      Given a pattern start rule such as {@code statement}, this object constructs +# a {@link ParseTree} with placeholders for the {@code ID} and {@code expr} +# subtree. Then the {@link #match} routines can compare an actual +# {@link ParseTree} from a parse with this pattern. Tag {@code } matches +# any {@code ID} token and tag {@code } references the result of the +# {@code expr} rule (generally an instance of {@code ExprContext}.

      +# +#

      Pattern {@code x = 0;} is a similar pattern that matches the same pattern +# except that it requires the identifier to be {@code x} and the expression to +# be {@code 0}.

      +# +#

      The {@link #matches} routines return {@code true} or {@code false} based +# upon a match for the tree rooted at the parameter sent in. The +# {@link #match} routines return a {@link ParseTreeMatch} object that +# contains the parse tree, the parse tree pattern, and a map from tag name to +# matched nodes (more below). A subtree that fails to match, returns with +# {@link ParseTreeMatch#mismatchedNode} set to the first tree node that did not +# match.

      +# +#

      For efficiency, you can compile a tree pattern in string form to a +# {@link ParseTreePattern} object.

      +# +#

      See {@code TestParseTreeMatcher} for lots of examples. +# {@link ParseTreePattern} has two static helper methods: +# {@link ParseTreePattern#findAll} and {@link ParseTreePattern#match} that +# are easy to use but not super efficient because they create new +# {@link ParseTreePatternMatcher} objects each time and have to compile the +# pattern in string form before using it.

      +# +#

      The lexer and parser that you pass into the {@link ParseTreePatternMatcher} +# constructor are used to parse the pattern in string form. The lexer converts +# the {@code = ;} into a sequence of four tokens (assuming lexer +# throws out whitespace or puts it on a hidden channel). Be aware that the +# input stream is reset for the lexer (but not the parser; a +# {@link ParserInterpreter} is created to parse the input.). Any user-defined +# fields you have put into the lexer might get changed when this mechanism asks +# it to scan the pattern string.

      +# +#

      Normally a parser does not accept token {@code } as a valid +# {@code expr} but, from the parser passed in, we create a special version of +# the underlying grammar representation (an {@link ATN}) that allows imaginary +# tokens representing rules ({@code }) to match entire rules. We call +# these bypass alternatives.

      +# +#

      Delimiters are {@code <} and {@code >}, with {@code \} as the escape string +# by default, but you can set them to whatever you want using +# {@link #setDelimiters}. You must escape both start and stop strings +# {@code \<} and {@code \>}.

      +# +from cf_units._udunits2_parser.parser._antlr4_runtime.CommonTokenStream import ( + CommonTokenStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + ParseCancellationException, + RecognitionException, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error.ErrorStrategy import ( + BailErrorStrategy, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( + InputStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import Lexer +from cf_units._udunits2_parser.parser._antlr4_runtime.ListTokenSource import ( + ListTokenSource, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.ParserRuleContext import ( + ParserRuleContext, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Chunk import ( + TagChunk, + TextChunk, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.RuleTagToken import ( + RuleTagToken, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.TokenTagToken import ( + TokenTagToken, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( + ParseTree, + RuleNode, + TerminalNode, +) + +# need forward declaration +Parser = None +ParseTreePattern = None + + +class CannotInvokeStartRule(Exception): + def __init__(self, e: Exception): + super().__init__(e) + + +class StartRuleDoesNotConsumeFullPattern(Exception): + pass + + +class ParseTreePatternMatcher: + __slots__ = ("lexer", "parser", "start", "stop", "escape") + + # Constructs a {@link ParseTreePatternMatcher} or from a {@link Lexer} and + # {@link Parser} object. The lexer input stream is altered for tokenizing + # the tree patterns. The parser is used as a convenient mechanism to get + # the grammar name, plus token, rule names. + def __init__(self, lexer: Lexer, parser: Parser): + self.lexer = lexer + self.parser = parser + self.start = "<" + self.stop = ">" + self.escape = "\\" # e.g., \< and \> must escape BOTH! + + # Set the delimiters used for marking rule and token tags within concrete + # syntax used by the tree pattern parser. + # + # @param start The start delimiter. + # @param stop The stop delimiter. + # @param escapeLeft The escape sequence to use for escaping a start or stop delimiter. + # + # @exception IllegalArgumentException if {@code start} is {@code null} or empty. + # @exception IllegalArgumentException if {@code stop} is {@code null} or empty. + # + def setDelimiters(self, start: str, stop: str, escapeLeft: str): + if start is None or len(start) == 0: + raise Exception("start cannot be null or empty") + if stop is None or len(stop) == 0: + raise Exception("stop cannot be null or empty") + self.start = start + self.stop = stop + self.escape = escapeLeft + + # Does {@code pattern} matched as rule {@code patternRuleIndex} match {@code tree}?# + def matchesRuleIndex( + self, tree: ParseTree, pattern: str, patternRuleIndex: int + ): + p = self.compileTreePattern(pattern, patternRuleIndex) + return self.matches(tree, p) + + # Does {@code pattern} matched as rule patternRuleIndex match tree? Pass in a + # compiled pattern instead of a string representation of a tree pattern. + # + def matchesPattern(self, tree: ParseTree, pattern: ParseTreePattern): + mismatchedNode = self.matchImpl(tree, pattern.patternTree, dict()) + return mismatchedNode is None + + # + # Compare {@code pattern} matched as rule {@code patternRuleIndex} against + # {@code tree} and return a {@link ParseTreeMatch} object that contains the + # matched elements, or the node at which the match failed. + # + def matchRuleIndex( + self, tree: ParseTree, pattern: str, patternRuleIndex: int + ): + p = self.compileTreePattern(pattern, patternRuleIndex) + return self.matchPattern(tree, p) + + # + # Compare {@code pattern} matched against {@code tree} and return a + # {@link ParseTreeMatch} object that contains the matched elements, or the + # node at which the match failed. Pass in a compiled pattern instead of a + # string representation of a tree pattern. + # + def matchPattern(self, tree: ParseTree, pattern: ParseTreePattern): + labels = dict() + mismatchedNode = self.matchImpl(tree, pattern.patternTree, labels) + from cf_units._udunits2_parser.parser._antlr4_runtime.tree.ParseTreeMatch import ( + ParseTreeMatch, + ) + + return ParseTreeMatch(tree, pattern, labels, mismatchedNode) + + # + # For repeated use of a tree pattern, compile it to a + # {@link ParseTreePattern} using this method. + # + def compileTreePattern(self, pattern: str, patternRuleIndex: int): + tokenList = self.tokenize(pattern) + tokenSrc = ListTokenSource(tokenList) + tokens = CommonTokenStream(tokenSrc) + from cf_units._udunits2_parser.parser._antlr4_runtime.ParserInterpreter import ( + ParserInterpreter, + ) + + parserInterp = ParserInterpreter( + self.parser.grammarFileName, + self.parser.tokenNames, + self.parser.ruleNames, + self.parser.getATNWithBypassAlts(), + tokens, + ) + tree = None + try: + parserInterp.setErrorHandler(BailErrorStrategy()) + tree = parserInterp.parse(patternRuleIndex) + except ParseCancellationException as e: + raise e.cause + except RecognitionException as e: + raise e + except Exception as e: + raise CannotInvokeStartRule(e) + + # Make sure tree pattern compilation checks for a complete parse + if tokens.LA(1) != Token.EOF: + raise StartRuleDoesNotConsumeFullPattern() + + from cf_units._udunits2_parser.parser._antlr4_runtime.tree.ParseTreePattern import ( + ParseTreePattern, + ) + + return ParseTreePattern(self, pattern, patternRuleIndex, tree) + + # + # Recursively walk {@code tree} against {@code patternTree}, filling + # {@code match.}{@link ParseTreeMatch#labels labels}. + # + # @return the first node encountered in {@code tree} which does not match + # a corresponding node in {@code patternTree}, or {@code null} if the match + # was successful. The specific node returned depends on the matching + # algorithm used by the implementation, and may be overridden. + # + def matchImpl(self, tree: ParseTree, patternTree: ParseTree, labels: dict): + if tree is None: + raise Exception("tree cannot be null") + if patternTree is None: + raise Exception("patternTree cannot be null") + + # x and , x and y, or x and x; or could be mismatched types + if isinstance(tree, TerminalNode) and isinstance( + patternTree, TerminalNode + ): + mismatchedNode = None + # both are tokens and they have same type + if tree.symbol.type == patternTree.symbol.type: + if isinstance(patternTree.symbol, TokenTagToken): # x and + tokenTagToken = patternTree.symbol + # track label->list-of-nodes for both token name and label (if any) + self.map(labels, tokenTagToken.tokenName, tree) + if tokenTagToken.label is not None: + self.map(labels, tokenTagToken.label, tree) + elif tree.getText() == patternTree.getText(): + # x and x + pass + else: + # x and y + if mismatchedNode is None: + mismatchedNode = tree + else: + if mismatchedNode is None: + mismatchedNode = tree + + return mismatchedNode + + if isinstance(tree, ParserRuleContext) and isinstance( + patternTree, ParserRuleContext + ): + mismatchedNode = None + # (expr ...) and + ruleTagToken = self.getRuleTagToken(patternTree) + if ruleTagToken is not None: + m = None + if ( + tree.ruleContext.ruleIndex + == patternTree.ruleContext.ruleIndex + ): + # track label->list-of-nodes for both rule name and label (if any) + self.map(labels, ruleTagToken.ruleName, tree) + if ruleTagToken.label is not None: + self.map(labels, ruleTagToken.label, tree) + else: + if mismatchedNode is None: + mismatchedNode = tree + + return mismatchedNode + + # (expr ...) and (expr ...) + if tree.getChildCount() != patternTree.getChildCount(): + if mismatchedNode is None: + mismatchedNode = tree + return mismatchedNode + + n = tree.getChildCount() + for i in range(0, n): + childMatch = self.matchImpl( + tree.getChild(i), patternTree.getChild(i), labels + ) + if childMatch is not None: + return childMatch + + return mismatchedNode + + # if nodes aren't both tokens or both rule nodes, can't match + return tree + + def map(self, labels, label, tree): + v = labels.get(label, None) + if v is None: + v = list() + labels[label] = v + v.append(tree) + + # Is {@code t} {@code (expr )} subtree?# + def getRuleTagToken(self, tree: ParseTree): + if isinstance(tree, RuleNode): + if tree.getChildCount() == 1 and isinstance( + tree.getChild(0), TerminalNode + ): + c = tree.getChild(0) + if isinstance(c.symbol, RuleTagToken): + return c.symbol + return None + + def tokenize(self, pattern: str): + # split pattern into chunks: sea (raw input) and islands (, ) + chunks = self.split(pattern) + + # create token stream from text and tags + tokens = list() + for chunk in chunks: + if isinstance(chunk, TagChunk): + # add special rule token or conjure up new token from name + if chunk.tag[0].isupper(): + ttype = self.parser.getTokenType(chunk.tag) + if ttype == Token.INVALID_TYPE: + raise Exception( + "Unknown token " + + str(chunk.tag) + + " in pattern: " + + pattern + ) + tokens.append(TokenTagToken(chunk.tag, ttype, chunk.label)) + elif chunk.tag[0].islower(): + ruleIndex = self.parser.getRuleIndex(chunk.tag) + if ruleIndex == -1: + raise Exception( + "Unknown rule " + + str(chunk.tag) + + " in pattern: " + + pattern + ) + ruleImaginaryTokenType = ( + self.parser.getATNWithBypassAlts().ruleToTokenType[ + ruleIndex + ] + ) + tokens.append( + RuleTagToken( + chunk.tag, ruleImaginaryTokenType, chunk.label + ) + ) + else: + raise Exception( + "invalid tag: " + + str(chunk.tag) + + " in pattern: " + + pattern + ) + else: + self.lexer.setInputStream(InputStream(chunk.text)) + t = self.lexer.nextToken() + while t.type != Token.EOF: + tokens.append(t) + t = self.lexer.nextToken() + return tokens + + # Split {@code = ;} into 4 chunks for tokenizing by {@link #tokenize}.# + def split(self, pattern: str): + p = 0 + n = len(pattern) + chunks = list() + # find all start and stop indexes first, then collect + starts = list() + stops = list() + while p < n: + if p == pattern.find(self.escape + self.start, p): + p += len(self.escape) + len(self.start) + elif p == pattern.find(self.escape + self.stop, p): + p += len(self.escape) + len(self.stop) + elif p == pattern.find(self.start, p): + starts.append(p) + p += len(self.start) + elif p == pattern.find(self.stop, p): + stops.append(p) + p += len(self.stop) + else: + p += 1 + + nt = len(starts) + + if nt > len(stops): + raise Exception("unterminated tag in pattern: " + pattern) + if nt < len(stops): + raise Exception("missing start tag in pattern: " + pattern) + + for i in range(0, nt): + if starts[i] >= stops[i]: + raise Exception( + "tag delimiters out of order in pattern: " + pattern + ) + + # collect into chunks now + if nt == 0: + chunks.append(TextChunk(pattern)) + + if nt > 0 and starts[0] > 0: # copy text up to first tag into chunks + text = pattern[0 : starts[0]] + chunks.add(TextChunk(text)) + + for i in range(0, nt): + # copy inside of + tag = pattern[starts[i] + len(self.start) : stops[i]] + ruleOrToken = tag + label = None + colon = tag.find(":") + if colon >= 0: + label = tag[0:colon] + ruleOrToken = tag[colon + 1 : len(tag)] + chunks.append(TagChunk(label, ruleOrToken)) + if i + 1 < len(starts): + # copy from end of to start of next + text = pattern[stops[i] + len(self.stop) : starts[i + 1]] + chunks.append(TextChunk(text)) + + if nt > 0: + afterLastTag = stops[nt - 1] + len(self.stop) + if afterLastTag < n: # copy text from end of last tag to end + text = pattern[afterLastTag:n] + chunks.append(TextChunk(text)) + + # strip out the escape sequences from text chunks but not tags + for i in range(0, len(chunks)): + c = chunks[i] + if isinstance(c, TextChunk): + unescaped = c.text.replace(self.escape, "") + if len(unescaped) < len(c.text): + chunks[i] = TextChunk(unescaped) + return chunks diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/RuleTagToken.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/RuleTagToken.py new file mode 100644 index 00000000..f914b903 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/RuleTagToken.py @@ -0,0 +1,53 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +# +# A {@link Token} object representing an entire subtree matched by a parser +# rule; e.g., {@code }. These tokens are created for {@link TagChunk} +# chunks where the tag corresponds to a parser rule. +# +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + + +class RuleTagToken(Token): + __slots__ = ("label", "ruleName") + # + # Constructs a new instance of {@link RuleTagToken} with the specified rule + # name, bypass token type, and label. + # + # @param ruleName The name of the parser rule this rule tag matches. + # @param bypassTokenType The bypass token type assigned to the parser rule. + # @param label The label associated with the rule tag, or {@code null} if + # the rule tag is unlabeled. + # + # @exception IllegalArgumentException if {@code ruleName} is {@code null} + # or empty. + + def __init__(self, ruleName: str, bypassTokenType: int, label: str = None): + if ruleName is None or len(ruleName) == 0: + raise Exception("ruleName cannot be null or empty.") + self.source = None + self.type = bypassTokenType # token type of the token + self.channel = ( + Token.DEFAULT_CHANNEL + ) # The parser ignores everything not on DEFAULT_CHANNEL + self.start = -1 # optional; return -1 if not implemented. + self.stop = -1 # optional; return -1 if not implemented. + self.tokenIndex = ( + -1 + ) # from 0..n-1 of the token object in the input stream + self.line = 0 # line=1..n of the 1st character + self.column = -1 # beginning of the line at which it occurs, 0..n-1 + self.label = label + self._text = self.getText() # text of the token. + + self.ruleName = ruleName + + def getText(self): + if self.label is None: + return "<" + self.ruleName + ">" + else: + return "<" + self.label + ":" + self.ruleName + ">" diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/TokenTagToken.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/TokenTagToken.py new file mode 100644 index 00000000..92639a71 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/TokenTagToken.py @@ -0,0 +1,48 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +# +# A {@link Token} object representing a token of a particular type; e.g., +# {@code }. These tokens are created for {@link TagChunk} chunks where the +# tag corresponds to a lexer rule or token type. +# +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import CommonToken + + +class TokenTagToken(CommonToken): + __slots__ = ("tokenName", "label") + + # Constructs a new instance of {@link TokenTagToken} with the specified + # token name, type, and label. + # + # @param tokenName The token name. + # @param type The token type. + # @param label The label associated with the token tag, or {@code null} if + # the token tag is unlabeled. + # + def __init__(self, tokenName: str, type: int, label: str = None): + super().__init__(type=type) + self.tokenName = tokenName + self.label = label + self._text = self.getText() + + # + # {@inheritDoc} + # + #

      The implementation for {@link TokenTagToken} returns the token tag + # formatted with {@code <} and {@code >} delimiters.

      + # + def getText(self): + if self.label is None: + return "<" + self.tokenName + ">" + else: + return "<" + self.label + ":" + self.tokenName + ">" + + #

      The implementation for {@link TokenTagToken} returns a string of the form + # {@code tokenName:type}.

      + # + def __str__(self): + return self.tokenName + ":" + str(self.type) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Tree.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Tree.py new file mode 100644 index 00000000..1d7e6220 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Tree.py @@ -0,0 +1,203 @@ +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# / + + +# The basic notion of a tree has a parent, a payload, and a list of children. +# It is the most abstract interface for all the trees used by ANTLR. +# / +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token + +INVALID_INTERVAL = (-1, -2) + + +class Tree: + pass + + +class SyntaxTree(Tree): + pass + + +class ParseTree(SyntaxTree): + pass + + +class RuleNode(ParseTree): + pass + + +class TerminalNode(ParseTree): + pass + + +class ErrorNode(TerminalNode): + pass + + +class ParseTreeVisitor: + def visit(self, tree): + return tree.accept(self) + + def visitChildren(self, node): + result = self.defaultResult() + n = node.getChildCount() + for i in range(n): + if not self.shouldVisitNextChild(node, result): + return result + + c = node.getChild(i) + childResult = c.accept(self) + result = self.aggregateResult(result, childResult) + + return result + + def visitTerminal(self, node): + return self.defaultResult() + + def visitErrorNode(self, node): + return self.defaultResult() + + def defaultResult(self): + return None + + def aggregateResult(self, aggregate, nextResult): + return nextResult + + def shouldVisitNextChild(self, node, currentResult): + return True + + +ParserRuleContext = None + + +class ParseTreeListener: + def visitTerminal(self, node: TerminalNode): + pass + + def visitErrorNode(self, node: ErrorNode): + pass + + def enterEveryRule(self, ctx: ParserRuleContext): + pass + + def exitEveryRule(self, ctx: ParserRuleContext): + pass + + +del ParserRuleContext + + +class TerminalNodeImpl(TerminalNode): + __slots__ = ("parentCtx", "symbol") + + def __init__(self, symbol: Token): + self.parentCtx = None + self.symbol = symbol + + def __setattr__(self, key, value): + super().__setattr__(key, value) + + def getChild(self, i: int): + return None + + def getSymbol(self): + return self.symbol + + def getParent(self): + return self.parentCtx + + def getPayload(self): + return self.symbol + + def getSourceInterval(self): + if self.symbol is None: + return INVALID_INTERVAL + tokenIndex = self.symbol.tokenIndex + return (tokenIndex, tokenIndex) + + def getChildCount(self): + return 0 + + def accept(self, visitor: ParseTreeVisitor): + return visitor.visitTerminal(self) + + def getText(self): + return self.symbol.text + + def __str__(self): + if self.symbol.type == Token.EOF: + return "" + else: + return self.symbol.text + + +# Represents a token that was consumed during resynchronization +# rather than during a valid match operation. For example, +# we will create this kind of a node during single token insertion +# and deletion as well as during "consume until error recovery set" +# upon no viable alternative exceptions. + + +class ErrorNodeImpl(TerminalNodeImpl, ErrorNode): + def __init__(self, token: Token): + super().__init__(token) + + def accept(self, visitor: ParseTreeVisitor): + return visitor.visitErrorNode(self) + + +class ParseTreeWalker: + DEFAULT = None + + def walk(self, listener: ParseTreeListener, t: ParseTree): + """ + Performs a walk on the given parse tree starting at the root and going down recursively + with depth-first search. On each node, {@link ParseTreeWalker#enterRule} is called before + recursively walking down into child nodes, then + {@link ParseTreeWalker#exitRule} is called after the recursive call to wind up. + @param listener The listener used by the walker to process grammar rules + @param t The parse tree to be walked on + """ + if isinstance(t, ErrorNode): + listener.visitErrorNode(t) + return + elif isinstance(t, TerminalNode): + listener.visitTerminal(t) + return + self.enterRule(listener, t) + for child in t.getChildren(): + self.walk(listener, child) + self.exitRule(listener, t) + + # + # The discovery of a rule node, involves sending two events: the generic + # {@link ParseTreeListener#enterEveryRule} and a + # {@link RuleContext}-specific event. First we trigger the generic and then + # the rule specific. We to them in reverse order upon finishing the node. + # + def enterRule(self, listener: ParseTreeListener, r: RuleNode): + """ + Enters a grammar rule by first triggering the generic event {@link ParseTreeListener#enterEveryRule} + then by triggering the event specific to the given parse tree node + @param listener The listener responding to the trigger events + @param r The grammar rule containing the rule context + """ + ctx = r.getRuleContext() + listener.enterEveryRule(ctx) + ctx.enterRule(listener) + + def exitRule(self, listener: ParseTreeListener, r: RuleNode): + """ + Exits a grammar rule by first triggering the event specific to the given parse tree node + then by triggering the generic event {@link ParseTreeListener#exitEveryRule} + @param listener The listener responding to the trigger events + @param r The grammar rule containing the rule context + """ + ctx = r.getRuleContext() + ctx.exitRule(listener) + listener.exitEveryRule(ctx) + + +ParseTreeWalker.DEFAULT = ParseTreeWalker() diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Trees.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Trees.py new file mode 100644 index 00000000..812cfe2c --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Trees.py @@ -0,0 +1,134 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + + +# A set of utility routines useful for all kinds of ANTLR trees.# +from io import StringIO + +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( + ErrorNode, + ParseTree, + RuleNode, + TerminalNode, + Tree, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Utils import ( + escapeWhitespace, +) + +# need forward declaration +Parser = None + + +class Trees: + # Print out a whole tree in LISP form. {@link #getNodeText} is used on the + # node payloads to get the text for the nodes. Detect + # parse trees and extract data appropriately. + @classmethod + def toStringTree( + cls, t: Tree, ruleNames: list = None, recog: Parser = None + ): + if recog is not None: + ruleNames = recog.ruleNames + s = escapeWhitespace(cls.getNodeText(t, ruleNames), False) + if t.getChildCount() == 0: + return s + with StringIO() as buf: + buf.write("(") + buf.write(s) + buf.write(" ") + for i in range(0, t.getChildCount()): + if i > 0: + buf.write(" ") + buf.write(cls.toStringTree(t.getChild(i), ruleNames)) + buf.write(")") + return buf.getvalue() + + @classmethod + def getNodeText( + cls, t: Tree, ruleNames: list = None, recog: Parser = None + ): + if recog is not None: + ruleNames = recog.ruleNames + if ruleNames is not None: + if isinstance(t, RuleNode): + if ( + t.getAltNumber() != 0 + ): # should use ATN.INVALID_ALT_NUMBER but won't compile + return ( + ruleNames[t.getRuleIndex()] + + ":" + + str(t.getAltNumber()) + ) + return ruleNames[t.getRuleIndex()] + elif isinstance(t, ErrorNode): + return str(t) + elif isinstance(t, TerminalNode): + if t.symbol is not None: + return t.symbol.text + # no recog for rule names + payload = t.getPayload() + if isinstance(payload, Token): + return payload.text + return str(t.getPayload()) + + # Return ordered list of all children of this node + @classmethod + def getChildren(cls, t: Tree): + return [t.getChild(i) for i in range(0, t.getChildCount())] + + # Return a list of all ancestors of this node. The first node of + # list is the root and the last is the parent of this node. + # + @classmethod + def getAncestors(cls, t: Tree): + ancestors = [] + t = t.getParent() + while t is not None: + ancestors.insert(0, t) # insert at start + t = t.getParent() + return ancestors + + @classmethod + def findAllTokenNodes(cls, t: ParseTree, ttype: int): + return cls.findAllNodes(t, ttype, True) + + @classmethod + def findAllRuleNodes(cls, t: ParseTree, ruleIndex: int): + return cls.findAllNodes(t, ruleIndex, False) + + @classmethod + def findAllNodes(cls, t: ParseTree, index: int, findTokens: bool): + nodes = [] + cls._findAllNodes(t, index, findTokens, nodes) + return nodes + + @classmethod + def _findAllNodes( + cls, t: ParseTree, index: int, findTokens: bool, nodes: list + ): + from cf_units._udunits2_parser.parser._antlr4_runtime.ParserRuleContext import ( + ParserRuleContext, + ) + + # check this node (the root) first + if findTokens and isinstance(t, TerminalNode): + if t.symbol.type == index: + nodes.append(t) + elif not findTokens and isinstance(t, ParserRuleContext): + if t.ruleIndex == index: + nodes.append(t) + # check children + for i in range(0, t.getChildCount()): + cls._findAllNodes(t.getChild(i), index, findTokens, nodes) + + @classmethod + def descendants(cls, t: ParseTree): + nodes = [t] + for i in range(0, t.getChildCount()): + nodes.extend(cls.descendants(t.getChild(i))) + return nodes diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/__init__.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/XPath.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/XPath.py new file mode 100644 index 00000000..1151089a --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/XPath.py @@ -0,0 +1,330 @@ +# +# Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. +# Use of this file is governed by the BSD 3-clause license that +# can be found in the LICENSE.txt file in the project root. +# + +# +# Represent a subset of XPath XML path syntax for use in identifying nodes in +# parse trees. +# +#

      +# Split path into words and separators {@code /} and {@code //} via ANTLR +# itself then walk path elements from left to right. At each separator-word +# pair, find set of nodes. Next stage uses those as work list.

      +# +#

      +# The basic interface is +# {@link XPath#findAll ParseTree.findAll}{@code (tree, pathString, parser)}. +# But that is just shorthand for:

      +# +#
      +# {@link XPath} p = new {@link XPath#XPath XPath}(parser, pathString);
      +# return p.{@link #evaluate evaluate}(tree);
      +# 
      +# +#

      +# See {@code org.antlr.v4.test.TestXPath} for descriptions. In short, this +# allows operators:

      +# +#
      +#
      /
      root
      +#
      //
      anywhere
      +#
      !
      invert; this must appear directly after root or anywhere +# operator
      +#
      +# +#

      +# and path elements:

      +# +#
      +#
      ID
      token name
      +#
      'string'
      any string literal token from the grammar
      +#
      expr
      rule name
      +#
      *
      wildcard matching any node
      +#
      +# +#

      +# Whitespace is not allowed.

      +# +from cf_units._udunits2_parser.parser._antlr4_runtime import ( + CommonTokenStream, + ParserRuleContext, + TerminalNode, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error.ErrorListener import ( + ErrorListener, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + LexerNoViableAltException, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( + InputStream, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.Parser import Parser +from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( + ParseTree, +) +from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Trees import Trees +from cf_units._udunits2_parser.parser._antlr4_runtime.xpath.XPathLexer import ( + XPathLexer, +) + + +class XPath: + WILDCARD = "*" # word not operator/separator + NOT = "!" # word for invert operator + + def __init__(self, parser: Parser, path: str): + self.parser = parser + self.path = path + self.elements = self.split(path) + + def split(self, path: str): + input = InputStream(path) + lexer = XPathLexer(input) + + def recover(self, e): + raise e + + lexer.recover = recover + lexer.removeErrorListeners() + lexer.addErrorListener( + ErrorListener() + ) # XPathErrorListener does no more + tokenStream = CommonTokenStream(lexer) + try: + tokenStream.fill() + except LexerNoViableAltException as e: + pos = lexer.column + msg = "Invalid tokens or characters at index %d in path '%s'" % ( + pos, + path, + ) + raise Exception(msg, e) + + tokens = iter(tokenStream.tokens) + elements = list() + for el in tokens: + invert = False + anywhere = False + # Check for path separators, if none assume root + if el.type in [XPathLexer.ROOT, XPathLexer.ANYWHERE]: + anywhere = el.type == XPathLexer.ANYWHERE + next_el = next(tokens, None) + if not next_el: + raise Exception("Missing element after %s" % el.getText()) + else: + el = next_el + # Check for bangs + if el.type == XPathLexer.BANG: + invert = True + next_el = next(tokens, None) + if not next_el: + raise Exception("Missing element after %s" % el.getText()) + else: + el = next_el + # Add searched element + if el.type in [ + XPathLexer.TOKEN_REF, + XPathLexer.RULE_REF, + XPathLexer.WILDCARD, + XPathLexer.STRING, + ]: + element = self.getXPathElement(el, anywhere) + element.invert = invert + elements.append(element) + elif el.type == Token.EOF: + break + else: + raise Exception( + "Unknown path element %s" % lexer.symbolicNames[el.type] + ) + return elements + + # + # Convert word like {@code#} or {@code ID} or {@code expr} to a path + # element. {@code anywhere} is {@code true} if {@code //} precedes the + # word. + # + def getXPathElement(self, wordToken: Token, anywhere: bool): + if wordToken.type == Token.EOF: + raise Exception("Missing path element at end of path") + + word = wordToken.text + if wordToken.type == XPathLexer.WILDCARD: + return ( + XPathWildcardAnywhereElement() + if anywhere + else XPathWildcardElement() + ) + + elif wordToken.type in [XPathLexer.TOKEN_REF, XPathLexer.STRING]: + tsource = self.parser.getTokenStream().tokenSource + + ttype = Token.INVALID_TYPE + if wordToken.type == XPathLexer.TOKEN_REF: + if word in tsource.ruleNames: + ttype = tsource.ruleNames.index(word) + 1 + else: + if word in tsource.literalNames: + ttype = tsource.literalNames.index(word) + + if ttype == Token.INVALID_TYPE: + raise Exception( + "%s at index %d isn't a valid token name" + % (word, wordToken.tokenIndex) + ) + return ( + XPathTokenAnywhereElement(word, ttype) + if anywhere + else XPathTokenElement(word, ttype) + ) + + else: + ruleIndex = ( + self.parser.ruleNames.index(word) + if word in self.parser.ruleNames + else -1 + ) + + if ruleIndex == -1: + raise Exception( + "%s at index %d isn't a valid rule name" + % (word, wordToken.tokenIndex) + ) + return ( + XPathRuleAnywhereElement(word, ruleIndex) + if anywhere + else XPathRuleElement(word, ruleIndex) + ) + + @staticmethod + def findAll(tree: ParseTree, xpath: str, parser: Parser): + p = XPath(parser, xpath) + return p.evaluate(tree) + + # + # Return a list of all nodes starting at {@code t} as root that satisfy the + # path. The root {@code /} is relative to the node passed to + # {@link #evaluate}. + # + def evaluate(self, t: ParseTree): + dummyRoot = ParserRuleContext() + dummyRoot.children = [t] # don't set t's parent. + + work = [dummyRoot] + for element in self.elements: + work_next = list() + for node in work: + if not isinstance(node, TerminalNode) and node.children: + # only try to match next element if it has children + # e.g., //func/*/stat might have a token node for which + # we can't go looking for stat nodes. + matching = element.evaluate(node) + + # See issue antlr#370 - Prevents XPath from returning the + # same node multiple times + matching = filter(lambda m: m not in work_next, matching) + + work_next.extend(matching) + work = work_next + + return work + + +class XPathElement: + def __init__(self, nodeName: str): + self.nodeName = nodeName + self.invert = False + + def __str__(self): + return ( + type(self).__name__ + + "[" + + ("!" if self.invert else "") + + self.nodeName + + "]" + ) + + +# +# Either {@code ID} at start of path or {@code ...//ID} in middle of path. +# +class XPathRuleAnywhereElement(XPathElement): + def __init__(self, ruleName: str, ruleIndex: int): + super().__init__(ruleName) + self.ruleIndex = ruleIndex + + def evaluate(self, t: ParseTree): + # return all ParserRuleContext descendants of t that match ruleIndex (or do not match if inverted) + return filter( + lambda c: isinstance(c, ParserRuleContext) + and (self.invert ^ (c.getRuleIndex() == self.ruleIndex)), + Trees.descendants(t), + ) + + +class XPathRuleElement(XPathElement): + def __init__(self, ruleName: str, ruleIndex: int): + super().__init__(ruleName) + self.ruleIndex = ruleIndex + + def evaluate(self, t: ParseTree): + # return all ParserRuleContext children of t that match ruleIndex (or do not match if inverted) + return filter( + lambda c: isinstance(c, ParserRuleContext) + and (self.invert ^ (c.getRuleIndex() == self.ruleIndex)), + Trees.getChildren(t), + ) + + +class XPathTokenAnywhereElement(XPathElement): + def __init__(self, ruleName: str, tokenType: int): + super().__init__(ruleName) + self.tokenType = tokenType + + def evaluate(self, t: ParseTree): + # return all TerminalNode descendants of t that match tokenType (or do not match if inverted) + return filter( + lambda c: isinstance(c, TerminalNode) + and (self.invert ^ (c.symbol.type == self.tokenType)), + Trees.descendants(t), + ) + + +class XPathTokenElement(XPathElement): + def __init__(self, ruleName: str, tokenType: int): + super().__init__(ruleName) + self.tokenType = tokenType + + def evaluate(self, t: ParseTree): + # return all TerminalNode children of t that match tokenType (or do not match if inverted) + return filter( + lambda c: isinstance(c, TerminalNode) + and (self.invert ^ (c.symbol.type == self.tokenType)), + Trees.getChildren(t), + ) + + +class XPathWildcardAnywhereElement(XPathElement): + def __init__(self): + super().__init__(XPath.WILDCARD) + + def evaluate(self, t: ParseTree): + if self.invert: + return list() # !* is weird but valid (empty) + else: + return Trees.descendants(t) + + +class XPathWildcardElement(XPathElement): + def __init__(self): + super().__init__(XPath.WILDCARD) + + def evaluate(self, t: ParseTree): + if self.invert: + return list() # !* is weird but valid (empty) + else: + return Trees.getChildren(t) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/XPathLexer.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/XPathLexer.py new file mode 100644 index 00000000..b8e960ff --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/XPathLexer.py @@ -0,0 +1,570 @@ +# Generated from XPathLexer.g4 by ANTLR 4.13.1 +import sys + +from cf_units._udunits2_parser.parser._antlr4_runtime import * + +if sys.version_info[1] > 5: + from typing import TextIO +else: + from typing.io import TextIO + + +def serializedATN(): + return [ + 4, + 0, + 8, + 50, + 6, + -1, + 2, + 0, + 7, + 0, + 2, + 1, + 7, + 1, + 2, + 2, + 7, + 2, + 2, + 3, + 7, + 3, + 2, + 4, + 7, + 4, + 2, + 5, + 7, + 5, + 2, + 6, + 7, + 6, + 2, + 7, + 7, + 7, + 1, + 0, + 1, + 0, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 2, + 1, + 2, + 1, + 3, + 1, + 3, + 1, + 4, + 1, + 4, + 5, + 4, + 29, + 8, + 4, + 10, + 4, + 12, + 4, + 32, + 9, + 4, + 1, + 4, + 1, + 4, + 1, + 5, + 1, + 5, + 3, + 5, + 38, + 8, + 5, + 1, + 6, + 1, + 6, + 1, + 7, + 1, + 7, + 5, + 7, + 44, + 8, + 7, + 10, + 7, + 12, + 7, + 47, + 9, + 7, + 1, + 7, + 1, + 7, + 1, + 45, + 0, + 8, + 1, + 3, + 3, + 4, + 5, + 5, + 7, + 6, + 9, + 7, + 11, + 0, + 13, + 0, + 15, + 8, + 1, + 0, + 2, + 5, + 0, + 48, + 57, + 95, + 95, + 183, + 183, + 768, + 879, + 8255, + 8256, + 13, + 0, + 65, + 90, + 97, + 122, + 192, + 214, + 216, + 246, + 248, + 767, + 880, + 893, + 895, + 8191, + 8204, + 8205, + 8304, + 8591, + 11264, + 12271, + 12289, + 55295, + 63744, + 64975, + 65008, + 65533, + 50, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 3, + 1, + 0, + 0, + 0, + 0, + 5, + 1, + 0, + 0, + 0, + 0, + 7, + 1, + 0, + 0, + 0, + 0, + 9, + 1, + 0, + 0, + 0, + 0, + 15, + 1, + 0, + 0, + 0, + 1, + 17, + 1, + 0, + 0, + 0, + 3, + 20, + 1, + 0, + 0, + 0, + 5, + 22, + 1, + 0, + 0, + 0, + 7, + 24, + 1, + 0, + 0, + 0, + 9, + 26, + 1, + 0, + 0, + 0, + 11, + 37, + 1, + 0, + 0, + 0, + 13, + 39, + 1, + 0, + 0, + 0, + 15, + 41, + 1, + 0, + 0, + 0, + 17, + 18, + 5, + 47, + 0, + 0, + 18, + 19, + 5, + 47, + 0, + 0, + 19, + 2, + 1, + 0, + 0, + 0, + 20, + 21, + 5, + 47, + 0, + 0, + 21, + 4, + 1, + 0, + 0, + 0, + 22, + 23, + 5, + 42, + 0, + 0, + 23, + 6, + 1, + 0, + 0, + 0, + 24, + 25, + 5, + 33, + 0, + 0, + 25, + 8, + 1, + 0, + 0, + 0, + 26, + 30, + 3, + 13, + 6, + 0, + 27, + 29, + 3, + 11, + 5, + 0, + 28, + 27, + 1, + 0, + 0, + 0, + 29, + 32, + 1, + 0, + 0, + 0, + 30, + 28, + 1, + 0, + 0, + 0, + 30, + 31, + 1, + 0, + 0, + 0, + 31, + 33, + 1, + 0, + 0, + 0, + 32, + 30, + 1, + 0, + 0, + 0, + 33, + 34, + 6, + 4, + 0, + 0, + 34, + 10, + 1, + 0, + 0, + 0, + 35, + 38, + 3, + 13, + 6, + 0, + 36, + 38, + 7, + 0, + 0, + 0, + 37, + 35, + 1, + 0, + 0, + 0, + 37, + 36, + 1, + 0, + 0, + 0, + 38, + 12, + 1, + 0, + 0, + 0, + 39, + 40, + 7, + 1, + 0, + 0, + 40, + 14, + 1, + 0, + 0, + 0, + 41, + 45, + 5, + 39, + 0, + 0, + 42, + 44, + 9, + 0, + 0, + 0, + 43, + 42, + 1, + 0, + 0, + 0, + 44, + 47, + 1, + 0, + 0, + 0, + 45, + 46, + 1, + 0, + 0, + 0, + 45, + 43, + 1, + 0, + 0, + 0, + 46, + 48, + 1, + 0, + 0, + 0, + 47, + 45, + 1, + 0, + 0, + 0, + 48, + 49, + 5, + 39, + 0, + 0, + 49, + 16, + 1, + 0, + 0, + 0, + 4, + 0, + 30, + 37, + 45, + 1, + 1, + 4, + 0, + ] + + +class XPathLexer(Lexer): + atn = ATNDeserializer().deserialize(serializedATN()) + + decisionsToDFA = [DFA(ds, i) for i, ds in enumerate(atn.decisionToState)] + + TOKEN_REF = 1 + RULE_REF = 2 + ANYWHERE = 3 + ROOT = 4 + WILDCARD = 5 + BANG = 6 + ID = 7 + STRING = 8 + + channelNames = ["DEFAULT_TOKEN_CHANNEL", "HIDDEN"] + + modeNames = ["DEFAULT_MODE"] + + literalNames = ["", "'//'", "'/'", "'*'", "'!'"] + + symbolicNames = [ + "", + "TOKEN_REF", + "RULE_REF", + "ANYWHERE", + "ROOT", + "WILDCARD", + "BANG", + "ID", + "STRING", + ] + + ruleNames = [ + "ANYWHERE", + "ROOT", + "WILDCARD", + "BANG", + "ID", + "NameChar", + "NameStartChar", + "STRING", + ] + + grammarFileName = "XPathLexer.g4" + + def __init__(self, input=None, output: TextIO = sys.stdout): + super().__init__(input, output) + self.checkVersion("4.13.1") + self._interp = LexerATNSimulator( + self, self.atn, self.decisionsToDFA, PredictionContextCache() + ) + self._actions = None + self._predicates = None + + def action(self, localctx: RuleContext, ruleIndex: int, actionIndex: int): + if self._actions is None: + actions = dict() + actions[4] = self.ID_action + self._actions = actions + action = self._actions.get(ruleIndex, None) + if action is not None: + action(localctx, actionIndex) + else: + raise Exception("No registered action for:" + str(ruleIndex)) + + def ID_action(self, localctx: RuleContext, actionIndex: int): + if actionIndex == 0: + char = self.text[0] + if char.isupper(): + self.type = XPathLexer.TOKEN_REF + else: + self.type = XPathLexer.RULE_REF diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/__init__.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/__init__.py new file mode 100644 index 00000000..80612724 --- /dev/null +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/__init__.py @@ -0,0 +1 @@ +__author__ = "ericvergnaud" From 373142e4ae43d6e67a3b4ca3b9d9b68410714fd3 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Fri, 27 Sep 2024 09:22:39 +0200 Subject: [PATCH 4/9] Remove the path from the generated code --- cf_units/_udunits2_parser/compile.py | 11 +++++++++++ .../parser/_antlr4_runtime/_pygrun.py | 4 +--- cf_units/_udunits2_parser/parser/udunits2Lexer.py | 2 +- cf_units/_udunits2_parser/parser/udunits2Parser.py | 2 +- .../_udunits2_parser/parser/udunits2ParserVisitor.py | 2 +- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/cf_units/_udunits2_parser/compile.py b/cf_units/_udunits2_parser/compile.py index 1a175000..43d3de8c 100644 --- a/cf_units/_udunits2_parser/compile.py +++ b/cf_units/_udunits2_parser/compile.py @@ -162,6 +162,17 @@ def main(): check=True, ) + # Fix up comments such as "Generated from /some/path.g4 by ANTLR 4.11.1" + pattern = re.compile( + r"# Generated from .+? by ANTLR (?P.*)" + ) + for py_file in parser_dir.glob("*.py"): + contents = py_file.read_text() + contents = re.sub( + pattern, r"# Generated by ANTLR \g", contents + ) + py_file.write_text(contents) + vendor_antlr4_runtime(parser_dir) # Reformat and lint fix the generated code. diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/_pygrun.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/_pygrun.py index 4ad33430..4069fdf4 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/_pygrun.py +++ b/cf_units/_udunits2_parser/parser/_antlr4_runtime/_pygrun.py @@ -177,9 +177,7 @@ def process(input_stream, class_lexer, class_parser): input_stream = FileStream(file_name) process(input_stream, class_lexer, class_parser) else: - print( - f"[ERROR] file {os.path.normpath(file_name)} not exist" - ) + print(f"[ERROR] file {os.path.normpath(file_name)} not exist") if __name__ == "__main__": diff --git a/cf_units/_udunits2_parser/parser/udunits2Lexer.py b/cf_units/_udunits2_parser/parser/udunits2Lexer.py index 560fc244..a1652ef5 100644 --- a/cf_units/_udunits2_parser/parser/udunits2Lexer.py +++ b/cf_units/_udunits2_parser/parser/udunits2Lexer.py @@ -1,4 +1,4 @@ -# Generated from /media/important/github/scitools/cf-units/cf_units/_udunits2_parser/parser/udunits2Lexer.g4 by ANTLR 4.11.1 +# Generated by ANTLR 4.11.1 import sys from cf_units._udunits2_parser.parser._antlr4_runtime import * diff --git a/cf_units/_udunits2_parser/parser/udunits2Parser.py b/cf_units/_udunits2_parser/parser/udunits2Parser.py index b57db82c..d081860e 100644 --- a/cf_units/_udunits2_parser/parser/udunits2Parser.py +++ b/cf_units/_udunits2_parser/parser/udunits2Parser.py @@ -1,4 +1,4 @@ -# Generated from /media/important/github/scitools/cf-units/cf_units/_udunits2_parser/udunits2Parser.g4 by ANTLR 4.11.1 +# Generated by ANTLR 4.11.1 import sys from cf_units._udunits2_parser.parser._antlr4_runtime import * diff --git a/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py b/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py index 723066bd..3817e550 100644 --- a/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py +++ b/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py @@ -1,4 +1,4 @@ -# Generated from /media/important/github/scitools/cf-units/cf_units/_udunits2_parser/udunits2Parser.g4 by ANTLR 4.11.1 +# Generated by ANTLR 4.11.1 from cf_units._udunits2_parser.parser._antlr4_runtime import * if __name__ is not None and "." in __name__: From 7a2d243e00b65180c1b48382801651ff50d3c704 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Fri, 27 Sep 2024 09:42:09 +0200 Subject: [PATCH 5/9] Move the antlr runtime outside of the parser directory, to avoid circular dependencies --- .gitattributes | 1 + cf_units/_udunits2_parser/__init__.py | 7 +-- .../_antlr4_runtime/BufferedTokenStream.py | 14 ++--- .../_antlr4_runtime/CommonTokenFactory.py | 2 +- .../_antlr4_runtime/CommonTokenStream.py | 6 +- .../_antlr4_runtime/FileStream.py | 4 +- .../_antlr4_runtime/InputStream.py | 2 +- .../_antlr4_runtime/IntervalSet.py | 2 +- .../_antlr4_runtime/LL1Analyzer.py | 22 +++---- .../{parser => }/_antlr4_runtime/Lexer.py | 14 ++--- .../_antlr4_runtime/ListTokenSource.py | 6 +- .../{parser => }/_antlr4_runtime/Parser.py | 34 +++++------ .../_antlr4_runtime/ParserInterpreter.py | 26 ++++---- .../_antlr4_runtime/ParserRuleContext.py | 8 +-- .../_antlr4_runtime/PredictionContext.py | 8 +-- .../_antlr4_runtime/Recognizer.py | 12 ++-- .../_antlr4_runtime/RuleContext.py | 4 +- .../_antlr4_runtime/StdinStream.py | 4 +- .../{parser => }/_antlr4_runtime/Token.py | 0 .../_antlr4_runtime/TokenStreamRewriter.py | 2 +- .../{parser => }/_antlr4_runtime/Utils.py | 0 .../_antlr4_runtime/__init__.py | 53 +++++++++++++++++ .../_antlr4_runtime/_antlr4_version.txt | 0 .../{parser => }/_antlr4_runtime/_pygrun.py | 6 +- .../{parser => }/_antlr4_runtime/atn/ATN.py | 18 ++---- .../_antlr4_runtime/atn/ATNConfig.py | 8 +-- .../_antlr4_runtime/atn/ATNConfigSet.py | 16 ++--- .../atn/ATNDeserializationOptions.py | 0 .../_antlr4_runtime/atn/ATNDeserializer.py | 16 +++-- .../_antlr4_runtime/atn/ATNSimulator.py | 10 ++-- .../_antlr4_runtime/atn/ATNState.py | 4 +- .../_antlr4_runtime/atn/ATNType.py | 0 .../_antlr4_runtime/atn/LexerATNSimulator.py | 34 ++++------- .../_antlr4_runtime/atn/LexerAction.py | 0 .../atn/LexerActionExecutor.py | 6 +- .../_antlr4_runtime/atn/ParserATNSimulator.py | 40 ++++++------- .../_antlr4_runtime/atn/PredictionMode.py | 12 ++-- .../_antlr4_runtime/atn/SemanticContext.py | 8 +-- .../_antlr4_runtime/atn/Transition.py | 10 ++-- .../_antlr4_runtime/atn/__init__.py | 0 .../{parser => }/_antlr4_runtime/dfa/DFA.py | 14 ++--- .../_antlr4_runtime/dfa/DFASerializer.py | 8 +-- .../_antlr4_runtime/dfa/DFAState.py | 4 +- .../_antlr4_runtime/dfa/__init__.py | 0 .../error/DiagnosticErrorListener.py | 6 +- .../_antlr4_runtime/error/ErrorListener.py | 0 .../_antlr4_runtime/error/ErrorStrategy.py | 12 ++-- .../_antlr4_runtime/error/Errors.py | 12 ++-- .../_antlr4_runtime/error/__init__.py | 0 .../_antlr4_runtime/tree/Chunk.py | 0 .../_antlr4_runtime/tree/ParseTreeMatch.py | 6 +- .../_antlr4_runtime/tree/ParseTreePattern.py | 6 +- .../tree/ParseTreePatternMatcher.py | 32 +++++----- .../_antlr4_runtime/tree/RuleTagToken.py | 2 +- .../_antlr4_runtime/tree/TokenTagToken.py | 2 +- .../{parser => }/_antlr4_runtime/tree/Tree.py | 2 +- .../_antlr4_runtime/tree/Trees.py | 10 ++-- .../_antlr4_runtime/tree/__init__.py | 0 .../_antlr4_runtime/xpath/XPath.py | 22 +++---- .../_antlr4_runtime/xpath/XPathLexer.py | 2 +- .../_antlr4_runtime/xpath/__init__.py | 0 cf_units/_udunits2_parser/compile.py | 10 ++-- .../parser/_antlr4_runtime/__init__.py | 59 ------------------- .../_udunits2_parser/parser/udunits2Lexer.py | 2 +- .../_udunits2_parser/parser/udunits2Parser.py | 10 ++-- .../parser/udunits2ParserVisitor.py | 2 +- pyproject.toml | 1 + 67 files changed, 273 insertions(+), 370 deletions(-) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/BufferedTokenStream.py (96%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/CommonTokenFactory.py (96%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/CommonTokenStream.py (92%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/FileStream.py (90%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/InputStream.py (96%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/IntervalSet.py (98%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/LL1Analyzer.py (92%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/Lexer.py (96%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/ListTokenSource.py (95%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/Parser.py (95%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/ParserInterpreter.py (88%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/ParserRuleContext.py (96%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/PredictionContext.py (99%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/Recognizer.py (92%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/RuleContext.py (98%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/StdinStream.py (74%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/Token.py (100%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/TokenStreamRewriter.py (99%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/Utils.py (100%) create mode 100644 cf_units/_udunits2_parser/_antlr4_runtime/__init__.py rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/_antlr4_version.txt (100%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/_pygrun.py (97%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/ATN.py (91%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/ATNConfig.py (95%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/ATNConfigSet.py (94%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/ATNDeserializationOptions.py (100%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/ATNDeserializer.py (97%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/ATNSimulator.py (87%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/ATNState.py (98%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/ATNType.py (100%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/LexerATNSimulator.py (96%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/LexerAction.py (100%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/LexerActionExecutor.py (97%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/ParserATNSimulator.py (98%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/PredictionMode.py (98%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/SemanticContext.py (98%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/Transition.py (96%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/atn/__init__.py (100%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/dfa/DFA.py (91%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/dfa/DFASerializer.py (91%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/dfa/DFAState.py (97%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/dfa/__init__.py (100%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/error/DiagnosticErrorListener.py (95%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/error/ErrorListener.py (100%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/error/ErrorStrategy.py (98%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/error/Errors.py (95%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/error/__init__.py (100%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/tree/Chunk.py (100%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/tree/ParseTreeMatch.py (96%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/tree/ParseTreePattern.py (93%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/tree/ParseTreePatternMatcher.py (93%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/tree/RuleTagToken.py (96%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/tree/TokenTagToken.py (95%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/tree/Tree.py (98%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/tree/Trees.py (92%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/tree/__init__.py (100%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/xpath/XPath.py (93%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/xpath/XPathLexer.py (99%) rename cf_units/_udunits2_parser/{parser => }/_antlr4_runtime/xpath/__init__.py (100%) delete mode 100644 cf_units/_udunits2_parser/parser/_antlr4_runtime/__init__.py diff --git a/.gitattributes b/.gitattributes index bda29feb..1df6e00c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,3 @@ .git_archival.txt export-subst cf_units/_udunits2_parser/parser/**/*.py linguist-generated=true +cf_units/_udunits2_parser/_antlr4_runtime/**/*.py linguist-generated=true diff --git a/cf_units/_udunits2_parser/__init__.py b/cf_units/_udunits2_parser/__init__.py index 2c0b0fcc..6041409a 100644 --- a/cf_units/_udunits2_parser/__init__.py +++ b/cf_units/_udunits2_parser/__init__.py @@ -5,15 +5,14 @@ import unicodedata -from cf_units._udunits2_parser.parser._antlr4_runtime import ( +from . import graph +from ._antlr4_runtime import ( CommonTokenStream, InputStream, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.error import ( +from ._antlr4_runtime.error import ( ErrorListener, ) - -from . import graph from .parser.udunits2Lexer import udunits2Lexer from .parser.udunits2Parser import udunits2Parser from .parser.udunits2ParserVisitor import udunits2ParserVisitor diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/BufferedTokenStream.py b/cf_units/_udunits2_parser/_antlr4_runtime/BufferedTokenStream.py similarity index 96% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/BufferedTokenStream.py rename to cf_units/_udunits2_parser/_antlr4_runtime/BufferedTokenStream.py index 100219a8..530272c3 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/BufferedTokenStream.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/BufferedTokenStream.py @@ -15,10 +15,10 @@ # {@link CommonTokenStream}.

      from io import StringIO -from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( +from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( IllegalStateException, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.Token import Token # need forward declaration Lexer = None @@ -233,9 +233,7 @@ def getHiddenTokensToRight(self, tokenIndex: int, channel: int = -1): raise Exception( str(tokenIndex) + " not in 0.." + str(len(self.tokens) - 1) ) - from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import ( - Lexer, - ) + from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer nextOnChannel = self.nextTokenOnChannel( tokenIndex + 1, Lexer.DEFAULT_TOKEN_CHANNEL @@ -254,9 +252,7 @@ def getHiddenTokensToLeft(self, tokenIndex: int, channel: int = -1): raise Exception( str(tokenIndex) + " not in 0.." + str(len(self.tokens) - 1) ) - from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import ( - Lexer, - ) + from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer prevOnChannel = self.previousTokenOnChannel( tokenIndex - 1, Lexer.DEFAULT_TOKEN_CHANNEL @@ -273,7 +269,7 @@ def filterForChannel(self, left: int, right: int, channel: int): for i in range(left, right + 1): t = self.tokens[i] if channel == -1: - from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import ( + from cf_units._udunits2_parser._antlr4_runtime.Lexer import ( Lexer, ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/CommonTokenFactory.py b/cf_units/_udunits2_parser/_antlr4_runtime/CommonTokenFactory.py similarity index 96% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/CommonTokenFactory.py rename to cf_units/_udunits2_parser/_antlr4_runtime/CommonTokenFactory.py index b5ea3d6f..0e5730bc 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/CommonTokenFactory.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/CommonTokenFactory.py @@ -8,7 +8,7 @@ # This default implementation of {@link TokenFactory} creates # {@link CommonToken} objects. # -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import CommonToken +from cf_units._udunits2_parser._antlr4_runtime.Token import CommonToken class TokenFactory: diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/CommonTokenStream.py b/cf_units/_udunits2_parser/_antlr4_runtime/CommonTokenStream.py similarity index 92% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/CommonTokenStream.py rename to cf_units/_udunits2_parser/_antlr4_runtime/CommonTokenStream.py index b39d2c1b..ad8a33a2 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/CommonTokenStream.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/CommonTokenStream.py @@ -29,11 +29,11 @@ # channel.

      # / -from cf_units._udunits2_parser.parser._antlr4_runtime.BufferedTokenStream import ( +from cf_units._udunits2_parser._antlr4_runtime.BufferedTokenStream import ( BufferedTokenStream, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import Lexer -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer +from cf_units._udunits2_parser._antlr4_runtime.Token import Token class CommonTokenStream(BufferedTokenStream): diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/FileStream.py b/cf_units/_udunits2_parser/_antlr4_runtime/FileStream.py similarity index 90% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/FileStream.py rename to cf_units/_udunits2_parser/_antlr4_runtime/FileStream.py index a6c0d140..54a8be25 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/FileStream.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/FileStream.py @@ -11,9 +11,7 @@ import codecs -from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( - InputStream, -) +from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream class FileStream(InputStream): diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/InputStream.py b/cf_units/_udunits2_parser/_antlr4_runtime/InputStream.py similarity index 96% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/InputStream.py rename to cf_units/_udunits2_parser/_antlr4_runtime/InputStream.py index d229979b..5c881bb7 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/InputStream.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/InputStream.py @@ -8,7 +8,7 @@ # # Vacuum all input from a string and then treat it like a buffer. # -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.Token import Token class InputStream: diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/IntervalSet.py b/cf_units/_udunits2_parser/_antlr4_runtime/IntervalSet.py similarity index 98% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/IntervalSet.py rename to cf_units/_udunits2_parser/_antlr4_runtime/IntervalSet.py index 5789d324..326c297f 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/IntervalSet.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/IntervalSet.py @@ -6,7 +6,7 @@ from io import StringIO -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.Token import Token # need forward declarations IntervalSet = None diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/LL1Analyzer.py b/cf_units/_udunits2_parser/_antlr4_runtime/LL1Analyzer.py similarity index 92% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/LL1Analyzer.py rename to cf_units/_udunits2_parser/_antlr4_runtime/LL1Analyzer.py index 376e2901..88410306 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/LL1Analyzer.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/LL1Analyzer.py @@ -3,32 +3,26 @@ # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. # / -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfig import ( - ATNConfig, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfig import ATNConfig +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( ATNState, RuleStopState, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.Transition import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.Transition import ( AbstractPredicateTransition, NotSetTransition, RuleTransition, WildcardTransition, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.IntervalSet import ( - IntervalSet, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( +from cf_units._udunits2_parser._antlr4_runtime.IntervalSet import IntervalSet +from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import ( PredictionContext, PredictionContextFromRuleContext, SingletonPredictionContext, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( - RuleContext, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext +from cf_units._udunits2_parser._antlr4_runtime.Token import Token class LL1Analyzer: diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/Lexer.py b/cf_units/_udunits2_parser/_antlr4_runtime/Lexer.py similarity index 96% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/Lexer.py rename to cf_units/_udunits2_parser/_antlr4_runtime/Lexer.py index 24311729..127edbc8 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/Lexer.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/Lexer.py @@ -15,21 +15,17 @@ from typing import TextIO else: from typing.io import TextIO -from cf_units._udunits2_parser.parser._antlr4_runtime.CommonTokenFactory import ( +from cf_units._udunits2_parser._antlr4_runtime.CommonTokenFactory import ( CommonTokenFactory, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( +from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( IllegalStateException, LexerNoViableAltException, RecognitionException, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( - InputStream, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Recognizer import ( - Recognizer, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream +from cf_units._udunits2_parser._antlr4_runtime.Recognizer import Recognizer +from cf_units._udunits2_parser._antlr4_runtime.Token import Token class TokenSource: diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/ListTokenSource.py b/cf_units/_udunits2_parser/_antlr4_runtime/ListTokenSource.py similarity index 95% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/ListTokenSource.py rename to cf_units/_udunits2_parser/_antlr4_runtime/ListTokenSource.py index a0851a7c..b3255bb3 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/ListTokenSource.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/ListTokenSource.py @@ -12,11 +12,11 @@ # as the EOF token for every call to {@link #nextToken} after the end of the # list is reached. Otherwise, an EOF token will be created.

      # -from cf_units._udunits2_parser.parser._antlr4_runtime.CommonTokenFactory import ( +from cf_units._udunits2_parser._antlr4_runtime.CommonTokenFactory import ( CommonTokenFactory, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import TokenSource -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.Lexer import TokenSource +from cf_units._udunits2_parser._antlr4_runtime.Token import Token class ListTokenSource(TokenSource): diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/Parser.py b/cf_units/_udunits2_parser/_antlr4_runtime/Parser.py similarity index 95% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/Parser.py rename to cf_units/_udunits2_parser/_antlr4_runtime/Parser.py index 3c17a9a3..e5aeaef4 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/Parser.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/Parser.py @@ -8,43 +8,37 @@ from typing import TextIO else: from typing.io import TextIO -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNDeserializationOptions import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNDeserializationOptions import ( ATNDeserializationOptions, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNDeserializer import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNDeserializer import ( ATNDeserializer, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.BufferedTokenStream import ( +from cf_units._udunits2_parser._antlr4_runtime.BufferedTokenStream import ( TokenStream, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.CommonTokenFactory import ( +from cf_units._udunits2_parser._antlr4_runtime.CommonTokenFactory import ( TokenFactory, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( +from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( RecognitionException, UnsupportedOperationException, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.error.ErrorStrategy import ( +from cf_units._udunits2_parser._antlr4_runtime.error.ErrorStrategy import ( DefaultErrorStrategy, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( - InputStream, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import Lexer -from cf_units._udunits2_parser.parser._antlr4_runtime.ParserRuleContext import ( +from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream +from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer +from cf_units._udunits2_parser._antlr4_runtime.ParserRuleContext import ( ParserRuleContext, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.Recognizer import ( - Recognizer, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( - RuleContext, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.ParseTreePatternMatcher import ( +from cf_units._udunits2_parser._antlr4_runtime.Recognizer import Recognizer +from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext +from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.tree.ParseTreePatternMatcher import ( ParseTreePatternMatcher, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( +from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ( ErrorNode, ParseTreeListener, TerminalNode, diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/ParserInterpreter.py b/cf_units/_udunits2_parser/_antlr4_runtime/ParserInterpreter.py similarity index 88% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/ParserInterpreter.py rename to cf_units/_udunits2_parser/_antlr4_runtime/ParserInterpreter.py index 9461f67b..f4ce5672 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/ParserInterpreter.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/ParserInterpreter.py @@ -17,37 +17,35 @@ # # See TestParserInterpreter for examples. # -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( ATNState, LoopEndState, StarLoopEntryState, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ParserATNSimulator import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ParserATNSimulator import ( ParserATNSimulator, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.Transition import ( - Transition, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.BufferedTokenStream import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.Transition import Transition +from cf_units._udunits2_parser._antlr4_runtime.BufferedTokenStream import ( TokenStream, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFA import DFA -from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( +from cf_units._udunits2_parser._antlr4_runtime.dfa.DFA import DFA +from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( FailedPredicateException, RecognitionException, UnsupportedOperationException, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import Lexer -from cf_units._udunits2_parser.parser._antlr4_runtime.Parser import Parser -from cf_units._udunits2_parser.parser._antlr4_runtime.ParserRuleContext import ( +from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer +from cf_units._udunits2_parser._antlr4_runtime.Parser import Parser +from cf_units._udunits2_parser._antlr4_runtime.ParserRuleContext import ( InterpreterRuleContext, ParserRuleContext, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( +from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import ( PredictionContextCache, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.Token import Token class ParserInterpreter(Parser): diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/ParserRuleContext.py b/cf_units/_udunits2_parser/_antlr4_runtime/ParserRuleContext.py similarity index 96% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/ParserRuleContext.py rename to cf_units/_udunits2_parser/_antlr4_runtime/ParserRuleContext.py index 7c877002..1f9e55fd 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/ParserRuleContext.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/ParserRuleContext.py @@ -25,11 +25,9 @@ # group values such as this aggregate. The getters/setters are there to # satisfy the superclass interface. -from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( - RuleContext, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( +from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext +from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ( INVALID_INTERVAL, ErrorNodeImpl, ParseTree, diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/PredictionContext.py b/cf_units/_udunits2_parser/_antlr4_runtime/PredictionContext.py similarity index 99% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/PredictionContext.py rename to cf_units/_udunits2_parser/_antlr4_runtime/PredictionContext.py index 58ae297a..603da8d7 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/PredictionContext.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/PredictionContext.py @@ -5,13 +5,11 @@ # / from io import StringIO -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( IllegalStateException, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( - RuleContext, -) +from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext # dup ParserATNSimulator class var here to avoid circular import; no idea why this can't be in PredictionContext _trace_atn_sim = False diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/Recognizer.py b/cf_units/_udunits2_parser/_antlr4_runtime/Recognizer.py similarity index 92% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/Recognizer.py rename to cf_units/_udunits2_parser/_antlr4_runtime/Recognizer.py index f74fa66a..75886836 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/Recognizer.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/Recognizer.py @@ -3,14 +3,12 @@ # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. # -from cf_units._udunits2_parser.parser._antlr4_runtime.error.ErrorListener import ( +from cf_units._udunits2_parser._antlr4_runtime.error.ErrorListener import ( ConsoleErrorListener, ProxyErrorListener, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( - RuleContext, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext +from cf_units._udunits2_parser._antlr4_runtime.Token import Token # need forward delcaration RecognitionException = None @@ -63,7 +61,7 @@ def removeErrorListeners(self): def getTokenTypeMap(self): tokenNames = self.getTokenNames() if tokenNames is None: - from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( UnsupportedOperationException, ) @@ -84,7 +82,7 @@ def getTokenTypeMap(self): def getRuleIndexMap(self): ruleNames = self.getRuleNames() if ruleNames is None: - from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( UnsupportedOperationException, ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/RuleContext.py b/cf_units/_udunits2_parser/_antlr4_runtime/RuleContext.py similarity index 98% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/RuleContext.py rename to cf_units/_udunits2_parser/_antlr4_runtime/RuleContext.py index 9378f5e3..31d83f49 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/RuleContext.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/RuleContext.py @@ -26,12 +26,12 @@ # / from io import StringIO -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( +from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ( INVALID_INTERVAL, ParseTreeVisitor, RuleNode, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Trees import Trees +from cf_units._udunits2_parser._antlr4_runtime.tree.Trees import Trees # need forward declarations RuleContext = None diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/StdinStream.py b/cf_units/_udunits2_parser/_antlr4_runtime/StdinStream.py similarity index 74% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/StdinStream.py rename to cf_units/_udunits2_parser/_antlr4_runtime/StdinStream.py index 631e3735..d41ae61e 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/StdinStream.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/StdinStream.py @@ -1,9 +1,7 @@ import codecs import sys -from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( - InputStream, -) +from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream class StdinStream(InputStream): diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/Token.py b/cf_units/_udunits2_parser/_antlr4_runtime/Token.py similarity index 100% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/Token.py rename to cf_units/_udunits2_parser/_antlr4_runtime/Token.py diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/TokenStreamRewriter.py b/cf_units/_udunits2_parser/_antlr4_runtime/TokenStreamRewriter.py similarity index 99% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/TokenStreamRewriter.py rename to cf_units/_udunits2_parser/_antlr4_runtime/TokenStreamRewriter.py index d7fe6506..cf57cb30 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/TokenStreamRewriter.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/TokenStreamRewriter.py @@ -6,7 +6,7 @@ from io import StringIO -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.Token import Token class TokenStreamRewriter: diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/Utils.py b/cf_units/_udunits2_parser/_antlr4_runtime/Utils.py similarity index 100% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/Utils.py rename to cf_units/_udunits2_parser/_antlr4_runtime/Utils.py diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/__init__.py b/cf_units/_udunits2_parser/_antlr4_runtime/__init__.py new file mode 100644 index 00000000..e4a633f9 --- /dev/null +++ b/cf_units/_udunits2_parser/_antlr4_runtime/__init__.py @@ -0,0 +1,53 @@ +from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNDeserializer import ( + ATNDeserializer, +) +from cf_units._udunits2_parser._antlr4_runtime.atn.LexerATNSimulator import ( + LexerATNSimulator, +) +from cf_units._udunits2_parser._antlr4_runtime.atn.ParserATNSimulator import ( + ParserATNSimulator, +) +from cf_units._udunits2_parser._antlr4_runtime.atn.PredictionMode import ( + PredictionMode, +) +from cf_units._udunits2_parser._antlr4_runtime.BufferedTokenStream import ( + TokenStream, +) +from cf_units._udunits2_parser._antlr4_runtime.CommonTokenStream import ( + CommonTokenStream, +) +from cf_units._udunits2_parser._antlr4_runtime.dfa.DFA import DFA +from cf_units._udunits2_parser._antlr4_runtime.error.DiagnosticErrorListener import ( + DiagnosticErrorListener, +) +from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( + IllegalStateException, + NoViableAltException, + RecognitionException, +) +from cf_units._udunits2_parser._antlr4_runtime.error.ErrorStrategy import ( + BailErrorStrategy, +) +from cf_units._udunits2_parser._antlr4_runtime.FileStream import FileStream +from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream +from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer +from cf_units._udunits2_parser._antlr4_runtime.Parser import Parser +from cf_units._udunits2_parser._antlr4_runtime.ParserRuleContext import ( + ParserRuleContext, + RuleContext, +) +from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import ( + PredictionContextCache, +) +from cf_units._udunits2_parser._antlr4_runtime.StdinStream import StdinStream +from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ( + ErrorNode, + ParseTreeListener, + ParseTreeVisitor, + ParseTreeWalker, + RuleNode, + TerminalNode, +) +from cf_units._udunits2_parser._antlr4_runtime.Utils import str_list diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/_antlr4_version.txt b/cf_units/_udunits2_parser/_antlr4_runtime/_antlr4_version.txt similarity index 100% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/_antlr4_version.txt rename to cf_units/_udunits2_parser/_antlr4_runtime/_antlr4_version.txt diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/_pygrun.py b/cf_units/_udunits2_parser/_antlr4_runtime/_pygrun.py similarity index 97% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/_pygrun.py rename to cf_units/_udunits2_parser/_antlr4_runtime/_pygrun.py index 4069fdf4..23089137 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/_pygrun.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/_pygrun.py @@ -4,7 +4,7 @@ import os import sys -from cf_units._udunits2_parser.parser._antlr4_runtime import * +from cf_units._udunits2_parser._antlr4_runtime import * # this is a python version of TestRig @@ -177,7 +177,9 @@ def process(input_stream, class_lexer, class_parser): input_stream = FileStream(file_name) process(input_stream, class_lexer, class_parser) else: - print(f"[ERROR] file {os.path.normpath(file_name)} not exist") + print( + f"[ERROR] file {os.path.normpath(file_name)} not exist" + ) if __name__ == "__main__": diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATN.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATN.py similarity index 91% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATN.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/ATN.py index 780d5ddb..ba7fb2b2 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATN.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATN.py @@ -2,20 +2,14 @@ # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. # / -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( ATNState, DecisionState, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNType import ( - ATNType, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.IntervalSet import ( - IntervalSet, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( - RuleContext, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNType import ATNType +from cf_units._udunits2_parser._antlr4_runtime.IntervalSet import IntervalSet +from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext +from cf_units._udunits2_parser._antlr4_runtime.Token import Token class ATN: @@ -66,7 +60,7 @@ def __init__(self, grammarType: ATNType, maxTokenType: int): # the rule surrounding {@code s}. In other words, the set will be # restricted to tokens reachable staying within {@code s}'s rule. def nextTokensInContext(self, s: ATNState, ctx: RuleContext): - from cf_units._udunits2_parser.parser._antlr4_runtime.LL1Analyzer import ( + from cf_units._udunits2_parser._antlr4_runtime.LL1Analyzer import ( LL1Analyzer, ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNConfig.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNConfig.py similarity index 95% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNConfig.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNConfig.py index 6b004c47..6aebaa3e 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNConfig.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNConfig.py @@ -13,17 +13,17 @@ # / from io import StringIO -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( ATNState, DecisionState, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.LexerActionExecutor import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.LexerActionExecutor import ( LexerActionExecutor, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.SemanticContext import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.SemanticContext import ( SemanticContext, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( +from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import ( PredictionContext, ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNConfigSet.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNConfigSet.py similarity index 94% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNConfigSet.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNConfigSet.py index c22f94de..97a9dd20 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNConfigSet.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNConfigSet.py @@ -12,21 +12,17 @@ # / from io import StringIO -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfig import ( - ATNConfig, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.SemanticContext import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfig import ATNConfig +from cf_units._udunits2_parser._antlr4_runtime.atn.SemanticContext import ( SemanticContext, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( +from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( IllegalStateException, UnsupportedOperationException, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( - merge, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Utils import str_list +from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import merge +from cf_units._udunits2_parser._antlr4_runtime.Utils import str_list ATNSimulator = None diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNDeserializationOptions.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNDeserializationOptions.py similarity index 100% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNDeserializationOptions.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNDeserializationOptions.py diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNDeserializer.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNDeserializer.py similarity index 97% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNDeserializer.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNDeserializer.py index 101bcbf4..a8e74536 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNDeserializer.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNDeserializer.py @@ -2,17 +2,15 @@ # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. # / -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNDeserializationOptions import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNDeserializationOptions import ( ATNDeserializationOptions, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import * -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNType import ( - ATNType, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.LexerAction import * -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.Transition import * -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import * +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNType import ATNType +from cf_units._udunits2_parser._antlr4_runtime.atn.LexerAction import * +from cf_units._udunits2_parser._antlr4_runtime.atn.Transition import * +from cf_units._udunits2_parser._antlr4_runtime.Token import Token SERIALIZED_VERSION = 4 diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNSimulator.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNSimulator.py similarity index 87% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNSimulator.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNSimulator.py index b7895eef..f9037583 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNSimulator.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNSimulator.py @@ -3,14 +3,12 @@ # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. # / -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfigSet import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfigSet import ( ATNConfigSet, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFAState import ( - DFAState, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( +from cf_units._udunits2_parser._antlr4_runtime.dfa.DFAState import DFAState +from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import ( PredictionContext, PredictionContextCache, getCachedPredictionContext, diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNState.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNState.py similarity index 98% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNState.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNState.py index f99e3c53..332b14e9 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNState.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNState.py @@ -64,9 +64,7 @@ # # -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.Transition import ( - Transition, -) +from cf_units._udunits2_parser._antlr4_runtime.atn.Transition import Transition INITIAL_NUM_TRANSITIONS = 4 diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNType.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNType.py similarity index 100% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ATNType.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNType.py diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerATNSimulator.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerATNSimulator.py similarity index 96% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerATNSimulator.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerATNSimulator.py index bc94eb39..a3e53f20 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerATNSimulator.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerATNSimulator.py @@ -20,43 +20,37 @@ # can simply return the predicted token type.

      # / -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfig import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfig import ( LexerATNConfig, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfigSet import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfigSet import ( ATNConfigSet, OrderedATNConfigSet, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNSimulator import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNSimulator import ( ATNSimulator, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( ATNState, RuleStopState, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.LexerActionExecutor import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.LexerActionExecutor import ( LexerActionExecutor, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.Transition import ( - Transition, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFAState import ( - DFAState, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.Transition import Transition +from cf_units._udunits2_parser._antlr4_runtime.dfa.DFAState import DFAState +from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( LexerNoViableAltException, UnsupportedOperationException, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( - InputStream, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( +from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream +from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import ( PredictionContext, PredictionContextCache, SingletonPredictionContext, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.Token import Token class SimState: @@ -117,9 +111,7 @@ def __init__( self.line = 1 # The index of the character relative to the beginning of the line 0..n-1#/ self.column = 0 - from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import ( - Lexer, - ) + from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer self.mode = Lexer.DEFAULT_MODE # Cache Lexer properties to avoid further imports diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerAction.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerAction.py similarity index 100% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerAction.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerAction.py diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerActionExecutor.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerActionExecutor.py similarity index 97% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerActionExecutor.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerActionExecutor.py index b0b46dbc..726d59a9 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/LexerActionExecutor.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerActionExecutor.py @@ -12,13 +12,11 @@ # not cause bloating of the {@link DFA} created for the lexer.

      -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.LexerAction import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.LexerAction import ( LexerAction, LexerIndexedCustomAction, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( - InputStream, -) +from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream # need a forward declaration Lexer = None diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ParserATNSimulator.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ParserATNSimulator.py similarity index 98% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ParserATNSimulator.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/ParserATNSimulator.py index 8f020397..dff63378 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/ParserATNSimulator.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ParserATNSimulator.py @@ -233,31 +233,29 @@ # import sys -from cf_units._udunits2_parser.parser._antlr4_runtime import DFA -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfig import ( - ATNConfig, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfigSet import ( +from cf_units._udunits2_parser._antlr4_runtime import DFA +from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfig import ATNConfig +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfigSet import ( ATNConfigSet, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNSimulator import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNSimulator import ( ATNSimulator, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( ATNState, DecisionState, RuleStopState, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.PredictionMode import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.PredictionMode import ( PredictionMode, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.SemanticContext import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.SemanticContext import ( SemanticContext, andContext, orContext, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.Transition import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.Transition import ( ActionTransition, AtomTransition, NotSetTransition, @@ -267,31 +265,29 @@ SetTransition, Transition, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.BufferedTokenStream import ( +from cf_units._udunits2_parser._antlr4_runtime.BufferedTokenStream import ( TokenStream, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFAState import ( +from cf_units._udunits2_parser._antlr4_runtime.dfa.DFAState import ( DFAState, PredPrediction, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( +from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( NoViableAltException, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.Parser import Parser -from cf_units._udunits2_parser.parser._antlr4_runtime.ParserRuleContext import ( +from cf_units._udunits2_parser._antlr4_runtime.Parser import Parser +from cf_units._udunits2_parser._antlr4_runtime.ParserRuleContext import ( ParserRuleContext, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( +from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import ( PredictionContext, PredictionContextCache, PredictionContextFromRuleContext, SingletonPredictionContext, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( - RuleContext, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token -from cf_units._udunits2_parser.parser._antlr4_runtime.Utils import str_list +from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext +from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.Utils import str_list class ParserATNSimulator(ATNSimulator): diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/PredictionMode.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/PredictionMode.py similarity index 98% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/PredictionMode.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/PredictionMode.py index 00141d8c..cc5a2d8b 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/PredictionMode.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/PredictionMode.py @@ -11,17 +11,15 @@ from enum import Enum -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfig import ( - ATNConfig, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfigSet import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfig import ATNConfig +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfigSet import ( ATNConfigSet, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( RuleStopState, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.SemanticContext import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.SemanticContext import ( SemanticContext, ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/SemanticContext.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/SemanticContext.py similarity index 98% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/SemanticContext.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/SemanticContext.py index 0d16ea6f..16d1c5cd 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/SemanticContext.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/SemanticContext.py @@ -13,12 +13,8 @@ # from io import StringIO -from cf_units._udunits2_parser.parser._antlr4_runtime.Recognizer import ( - Recognizer, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.RuleContext import ( - RuleContext, -) +from cf_units._udunits2_parser._antlr4_runtime.Recognizer import Recognizer +from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext class SemanticContext: diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/Transition.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/Transition.py similarity index 96% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/Transition.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/Transition.py index 0d04eadb..271d02cf 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/Transition.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/Transition.py @@ -17,14 +17,12 @@ # ATN transitions.

      # # need forward declarations -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.SemanticContext import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.SemanticContext import ( PrecedencePredicate, Predicate, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.IntervalSet import ( - IntervalSet, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.IntervalSet import IntervalSet +from cf_units._udunits2_parser._antlr4_runtime.Token import Token ATNState = None RuleStartState = None @@ -309,4 +307,4 @@ def __str__(self): del ATNState del RuleStartState -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import * +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import * diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/__init__.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/__init__.py similarity index 100% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/atn/__init__.py rename to cf_units/_udunits2_parser/_antlr4_runtime/atn/__init__.py diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFA.py b/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFA.py similarity index 91% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFA.py rename to cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFA.py index 710c0e09..e5c879a6 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFA.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFA.py @@ -2,17 +2,15 @@ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfigSet import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfigSet import ( ATNConfigSet, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( DecisionState, StarLoopEntryState, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFAState import ( - DFAState, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( +from cf_units._udunits2_parser._antlr4_runtime.dfa.DFAState import DFAState +from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( IllegalStateException, ) @@ -132,7 +130,7 @@ def __str__(self): def toString(self, literalNames: list = None, symbolicNames: list = None): if self.s0 is None: return "" - from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFASerializer import ( + from cf_units._udunits2_parser._antlr4_runtime.dfa.DFASerializer import ( DFASerializer, ) @@ -142,7 +140,7 @@ def toString(self, literalNames: list = None, symbolicNames: list = None): def toLexerString(self): if self.s0 is None: return "" - from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFASerializer import ( + from cf_units._udunits2_parser._antlr4_runtime.dfa.DFASerializer import ( LexerDFASerializer, ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFASerializer.py b/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFASerializer.py similarity index 91% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFASerializer.py rename to cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFASerializer.py index 66758775..c6dbc447 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFASerializer.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFASerializer.py @@ -7,11 +7,9 @@ # A DFA walker that knows how to dump them to serialized strings.#/ from io import StringIO -from cf_units._udunits2_parser.parser._antlr4_runtime import DFA -from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFAState import ( - DFAState, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Utils import str_list +from cf_units._udunits2_parser._antlr4_runtime import DFA +from cf_units._udunits2_parser._antlr4_runtime.dfa.DFAState import DFAState +from cf_units._udunits2_parser._antlr4_runtime.Utils import str_list class DFASerializer: diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFAState.py b/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFAState.py similarity index 97% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFAState.py rename to cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFAState.py index b58111b9..83587af9 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/DFAState.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFAState.py @@ -7,10 +7,10 @@ # Map a predicate to a predicted alternative.#/ from io import StringIO -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfigSet import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfigSet import ( ATNConfigSet, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.SemanticContext import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.SemanticContext import ( SemanticContext, ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/__init__.py b/cf_units/_udunits2_parser/_antlr4_runtime/dfa/__init__.py similarity index 100% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/dfa/__init__.py rename to cf_units/_udunits2_parser/_antlr4_runtime/dfa/__init__.py diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/DiagnosticErrorListener.py b/cf_units/_udunits2_parser/_antlr4_runtime/error/DiagnosticErrorListener.py similarity index 95% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/error/DiagnosticErrorListener.py rename to cf_units/_udunits2_parser/_antlr4_runtime/error/DiagnosticErrorListener.py index 679a3205..79637385 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/DiagnosticErrorListener.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/error/DiagnosticErrorListener.py @@ -26,11 +26,11 @@ from io import StringIO -from cf_units._udunits2_parser.parser._antlr4_runtime import DFA, Parser -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNConfigSet import ( +from cf_units._udunits2_parser._antlr4_runtime import DFA, Parser +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfigSet import ( ATNConfigSet, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.error.ErrorListener import ( +from cf_units._udunits2_parser._antlr4_runtime.error.ErrorListener import ( ErrorListener, ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/ErrorListener.py b/cf_units/_udunits2_parser/_antlr4_runtime/error/ErrorListener.py similarity index 100% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/error/ErrorListener.py rename to cf_units/_udunits2_parser/_antlr4_runtime/error/ErrorListener.py diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/ErrorStrategy.py b/cf_units/_udunits2_parser/_antlr4_runtime/error/ErrorStrategy.py similarity index 98% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/error/ErrorStrategy.py rename to cf_units/_udunits2_parser/_antlr4_runtime/error/ErrorStrategy.py index e79cf8fc..d9b22735 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/ErrorStrategy.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/error/ErrorStrategy.py @@ -3,20 +3,16 @@ # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. # -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNState import ( - ATNState, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( +from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ATNState +from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( FailedPredicateException, InputMismatchException, NoViableAltException, ParseCancellationException, RecognitionException, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.IntervalSet import ( - IntervalSet, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.IntervalSet import IntervalSet +from cf_units._udunits2_parser._antlr4_runtime.Token import Token # need forward declaration Parser = None diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/Errors.py b/cf_units/_udunits2_parser/_antlr4_runtime/error/Errors.py similarity index 95% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/error/Errors.py rename to cf_units/_udunits2_parser/_antlr4_runtime/error/Errors.py index 051a231b..ee571b52 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/Errors.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/error/Errors.py @@ -35,15 +35,11 @@ def __init__(self, msg: str): # in the input, where it is in the ATN, the rule invocation stack, # and what kind of problem occurred. -from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( - InputStream, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.ParserRuleContext import ( +from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream +from cf_units._udunits2_parser._antlr4_runtime.ParserRuleContext import ( ParserRuleContext, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.Recognizer import ( - Recognizer, -) +from cf_units._udunits2_parser._antlr4_runtime.Recognizer import Recognizer class RecognitionException(Exception): @@ -179,7 +175,7 @@ def __init__( ) s = recognizer._interp.atn.states[recognizer.state] trans = s.transitions[0] - from cf_units._udunits2_parser.parser._antlr4_runtime.atn.Transition import ( + from cf_units._udunits2_parser._antlr4_runtime.atn.Transition import ( PredicateTransition, ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/error/__init__.py b/cf_units/_udunits2_parser/_antlr4_runtime/error/__init__.py similarity index 100% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/error/__init__.py rename to cf_units/_udunits2_parser/_antlr4_runtime/error/__init__.py diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Chunk.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/Chunk.py similarity index 100% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Chunk.py rename to cf_units/_udunits2_parser/_antlr4_runtime/tree/Chunk.py diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreeMatch.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreeMatch.py similarity index 96% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreeMatch.py rename to cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreeMatch.py index 570effeb..92d9a80d 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreeMatch.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreeMatch.py @@ -10,12 +10,10 @@ # from io import StringIO -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.ParseTreePattern import ( +from cf_units._udunits2_parser._antlr4_runtime.tree.ParseTreePattern import ( ParseTreePattern, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( - ParseTree, -) +from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ParseTree class ParseTreeMatch: diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreePattern.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreePattern.py similarity index 93% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreePattern.py rename to cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreePattern.py index 622c8d96..00770c94 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreePattern.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreePattern.py @@ -8,12 +8,10 @@ # A pattern like {@code = ;} converted to a {@link ParseTree} by # {@link ParseTreePatternMatcher#compile(String, int)}. # -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.ParseTreePatternMatcher import ( +from cf_units._udunits2_parser._antlr4_runtime.tree.ParseTreePatternMatcher import ( ParseTreePatternMatcher, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( - ParseTree, -) +from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ParseTree class ParseTreePattern: diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreePatternMatcher.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreePatternMatcher.py similarity index 93% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreePatternMatcher.py rename to cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreePatternMatcher.py index bc440c83..18d554d8 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/ParseTreePatternMatcher.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreePatternMatcher.py @@ -61,38 +61,36 @@ # {@link #setDelimiters}. You must escape both start and stop strings # {@code \<} and {@code \>}.

      # -from cf_units._udunits2_parser.parser._antlr4_runtime.CommonTokenStream import ( +from cf_units._udunits2_parser._antlr4_runtime.CommonTokenStream import ( CommonTokenStream, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( +from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( ParseCancellationException, RecognitionException, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.error.ErrorStrategy import ( +from cf_units._udunits2_parser._antlr4_runtime.error.ErrorStrategy import ( BailErrorStrategy, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( - InputStream, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import Lexer -from cf_units._udunits2_parser.parser._antlr4_runtime.ListTokenSource import ( +from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream +from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer +from cf_units._udunits2_parser._antlr4_runtime.ListTokenSource import ( ListTokenSource, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.ParserRuleContext import ( +from cf_units._udunits2_parser._antlr4_runtime.ParserRuleContext import ( ParserRuleContext, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Chunk import ( +from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.tree.Chunk import ( TagChunk, TextChunk, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.RuleTagToken import ( +from cf_units._udunits2_parser._antlr4_runtime.tree.RuleTagToken import ( RuleTagToken, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.TokenTagToken import ( +from cf_units._udunits2_parser._antlr4_runtime.tree.TokenTagToken import ( TokenTagToken, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( +from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ( ParseTree, RuleNode, TerminalNode, @@ -179,7 +177,7 @@ def matchRuleIndex( def matchPattern(self, tree: ParseTree, pattern: ParseTreePattern): labels = dict() mismatchedNode = self.matchImpl(tree, pattern.patternTree, labels) - from cf_units._udunits2_parser.parser._antlr4_runtime.tree.ParseTreeMatch import ( + from cf_units._udunits2_parser._antlr4_runtime.tree.ParseTreeMatch import ( ParseTreeMatch, ) @@ -193,7 +191,7 @@ def compileTreePattern(self, pattern: str, patternRuleIndex: int): tokenList = self.tokenize(pattern) tokenSrc = ListTokenSource(tokenList) tokens = CommonTokenStream(tokenSrc) - from cf_units._udunits2_parser.parser._antlr4_runtime.ParserInterpreter import ( + from cf_units._udunits2_parser._antlr4_runtime.ParserInterpreter import ( ParserInterpreter, ) @@ -219,7 +217,7 @@ def compileTreePattern(self, pattern: str, patternRuleIndex: int): if tokens.LA(1) != Token.EOF: raise StartRuleDoesNotConsumeFullPattern() - from cf_units._udunits2_parser.parser._antlr4_runtime.tree.ParseTreePattern import ( + from cf_units._udunits2_parser._antlr4_runtime.tree.ParseTreePattern import ( ParseTreePattern, ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/RuleTagToken.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/RuleTagToken.py similarity index 96% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/RuleTagToken.py rename to cf_units/_udunits2_parser/_antlr4_runtime/tree/RuleTagToken.py index f914b903..3bb398c1 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/RuleTagToken.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/tree/RuleTagToken.py @@ -9,7 +9,7 @@ # rule; e.g., {@code }. These tokens are created for {@link TagChunk} # chunks where the tag corresponds to a parser rule. # -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.Token import Token class RuleTagToken(Token): diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/TokenTagToken.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/TokenTagToken.py similarity index 95% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/TokenTagToken.py rename to cf_units/_udunits2_parser/_antlr4_runtime/tree/TokenTagToken.py index 92639a71..7236f177 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/TokenTagToken.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/tree/TokenTagToken.py @@ -9,7 +9,7 @@ # {@code }. These tokens are created for {@link TagChunk} chunks where the # tag corresponds to a lexer rule or token type. # -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import CommonToken +from cf_units._udunits2_parser._antlr4_runtime.Token import CommonToken class TokenTagToken(CommonToken): diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Tree.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/Tree.py similarity index 98% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Tree.py rename to cf_units/_udunits2_parser/_antlr4_runtime/tree/Tree.py index 1d7e6220..77ea746e 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Tree.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/tree/Tree.py @@ -7,7 +7,7 @@ # The basic notion of a tree has a parent, a payload, and a list of children. # It is the most abstract interface for all the trees used by ANTLR. # / -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.Token import Token INVALID_INTERVAL = (-1, -2) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Trees.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/Trees.py similarity index 92% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Trees.py rename to cf_units/_udunits2_parser/_antlr4_runtime/tree/Trees.py index 812cfe2c..0005b441 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/Trees.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/tree/Trees.py @@ -8,17 +8,15 @@ # A set of utility routines useful for all kinds of ANTLR trees.# from io import StringIO -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( +from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ( ErrorNode, ParseTree, RuleNode, TerminalNode, Tree, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.Utils import ( - escapeWhitespace, -) +from cf_units._udunits2_parser._antlr4_runtime.Utils import escapeWhitespace # need forward declaration Parser = None @@ -111,7 +109,7 @@ def findAllNodes(cls, t: ParseTree, index: int, findTokens: bool): def _findAllNodes( cls, t: ParseTree, index: int, findTokens: bool, nodes: list ): - from cf_units._udunits2_parser.parser._antlr4_runtime.ParserRuleContext import ( + from cf_units._udunits2_parser._antlr4_runtime.ParserRuleContext import ( ParserRuleContext, ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/__init__.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/__init__.py similarity index 100% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/tree/__init__.py rename to cf_units/_udunits2_parser/_antlr4_runtime/tree/__init__.py diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/XPath.py b/cf_units/_udunits2_parser/_antlr4_runtime/xpath/XPath.py similarity index 93% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/XPath.py rename to cf_units/_udunits2_parser/_antlr4_runtime/xpath/XPath.py index 1151089a..86aa8baf 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/XPath.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/xpath/XPath.py @@ -47,27 +47,23 @@ #

      # Whitespace is not allowed.

      # -from cf_units._udunits2_parser.parser._antlr4_runtime import ( +from cf_units._udunits2_parser._antlr4_runtime import ( CommonTokenStream, ParserRuleContext, TerminalNode, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.error.ErrorListener import ( +from cf_units._udunits2_parser._antlr4_runtime.error.ErrorListener import ( ErrorListener, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( +from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( LexerNoViableAltException, ) -from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( - InputStream, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Parser import Parser -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( - ParseTree, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Trees import Trees -from cf_units._udunits2_parser.parser._antlr4_runtime.xpath.XPathLexer import ( +from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream +from cf_units._udunits2_parser._antlr4_runtime.Parser import Parser +from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ParseTree +from cf_units._udunits2_parser._antlr4_runtime.tree.Trees import Trees +from cf_units._udunits2_parser._antlr4_runtime.xpath.XPathLexer import ( XPathLexer, ) diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/XPathLexer.py b/cf_units/_udunits2_parser/_antlr4_runtime/xpath/XPathLexer.py similarity index 99% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/XPathLexer.py rename to cf_units/_udunits2_parser/_antlr4_runtime/xpath/XPathLexer.py index b8e960ff..ca5ae82e 100644 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/XPathLexer.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/xpath/XPathLexer.py @@ -1,7 +1,7 @@ # Generated from XPathLexer.g4 by ANTLR 4.13.1 import sys -from cf_units._udunits2_parser.parser._antlr4_runtime import * +from cf_units._udunits2_parser._antlr4_runtime import * if sys.version_info[1] > 5: from typing import TextIO diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/__init__.py b/cf_units/_udunits2_parser/_antlr4_runtime/xpath/__init__.py similarity index 100% rename from cf_units/_udunits2_parser/parser/_antlr4_runtime/xpath/__init__.py rename to cf_units/_udunits2_parser/_antlr4_runtime/xpath/__init__.py diff --git a/cf_units/_udunits2_parser/compile.py b/cf_units/_udunits2_parser/compile.py index 43d3de8c..be264ac5 100644 --- a/cf_units/_udunits2_parser/compile.py +++ b/cf_units/_udunits2_parser/compile.py @@ -68,8 +68,8 @@ def expand_lexer(source, target): fh.write(new_content) -def vendor_antlr4_runtime(parser_dir: Path): - antlr_dest = parser_dir / "_antlr4_runtime" +def vendor_antlr4_runtime(udunits2_parser_dir: Path): + antlr_dest = udunits2_parser_dir / "_antlr4_runtime" version_file = antlr_dest / "_antlr4_version.txt" existing_version: str | None = None if antlr_dest.exists(): @@ -113,11 +113,11 @@ def vendor_antlr4_runtime(parser_dir: Path): contents = py_file.read_text() contents = contents.replace( "import antlr4", - "import cf_units._udunits2_parser.parser._antlr4_runtime", + "import cf_units._udunits2_parser._antlr4_runtime", ) contents = contents.replace( "from antlr4", - "from cf_units._udunits2_parser.parser._antlr4_runtime", + "from cf_units._udunits2_parser._antlr4_runtime", ) py_file.write_text(contents) @@ -173,7 +173,7 @@ def main(): ) py_file.write_text(contents) - vendor_antlr4_runtime(parser_dir) + vendor_antlr4_runtime(HERE) # Reformat and lint fix the generated code. subprocess.run( diff --git a/cf_units/_udunits2_parser/parser/_antlr4_runtime/__init__.py b/cf_units/_udunits2_parser/parser/_antlr4_runtime/__init__.py deleted file mode 100644 index 8401d64d..00000000 --- a/cf_units/_udunits2_parser/parser/_antlr4_runtime/__init__.py +++ /dev/null @@ -1,59 +0,0 @@ -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ATNDeserializer import ( - ATNDeserializer, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.LexerATNSimulator import ( - LexerATNSimulator, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.ParserATNSimulator import ( - ParserATNSimulator, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.atn.PredictionMode import ( - PredictionMode, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.BufferedTokenStream import ( - TokenStream, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.CommonTokenStream import ( - CommonTokenStream, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.dfa.DFA import DFA -from cf_units._udunits2_parser.parser._antlr4_runtime.error.DiagnosticErrorListener import ( - DiagnosticErrorListener, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( - IllegalStateException, - NoViableAltException, - RecognitionException, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.error.ErrorStrategy import ( - BailErrorStrategy, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.FileStream import ( - FileStream, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.InputStream import ( - InputStream, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Lexer import Lexer -from cf_units._udunits2_parser.parser._antlr4_runtime.Parser import Parser -from cf_units._udunits2_parser.parser._antlr4_runtime.ParserRuleContext import ( - ParserRuleContext, - RuleContext, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.PredictionContext import ( - PredictionContextCache, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.StdinStream import ( - StdinStream, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Token import Token -from cf_units._udunits2_parser.parser._antlr4_runtime.tree.Tree import ( - ErrorNode, - ParseTreeListener, - ParseTreeVisitor, - ParseTreeWalker, - RuleNode, - TerminalNode, -) -from cf_units._udunits2_parser.parser._antlr4_runtime.Utils import str_list diff --git a/cf_units/_udunits2_parser/parser/udunits2Lexer.py b/cf_units/_udunits2_parser/parser/udunits2Lexer.py index a1652ef5..664d3604 100644 --- a/cf_units/_udunits2_parser/parser/udunits2Lexer.py +++ b/cf_units/_udunits2_parser/parser/udunits2Lexer.py @@ -1,7 +1,7 @@ # Generated by ANTLR 4.11.1 import sys -from cf_units._udunits2_parser.parser._antlr4_runtime import * +from cf_units._udunits2_parser._antlr4_runtime import * if sys.version_info[1] > 5: from typing import TextIO diff --git a/cf_units/_udunits2_parser/parser/udunits2Parser.py b/cf_units/_udunits2_parser/parser/udunits2Parser.py index d081860e..a7fc00ce 100644 --- a/cf_units/_udunits2_parser/parser/udunits2Parser.py +++ b/cf_units/_udunits2_parser/parser/udunits2Parser.py @@ -1,7 +1,7 @@ # Generated by ANTLR 4.11.1 import sys -from cf_units._udunits2_parser.parser._antlr4_runtime import * +from cf_units._udunits2_parser._antlr4_runtime import * if sys.version_info[1] > 5: from typing import TextIO @@ -1603,7 +1603,7 @@ def product(self, _p: int = 0): ) self.state = 51 if not self.precpred(self._ctx, 4): - from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( FailedPredicateException, ) @@ -1623,7 +1623,7 @@ def product(self, _p: int = 0): ) self.state = 53 if not self.precpred(self._ctx, 3): - from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( FailedPredicateException, ) @@ -1645,7 +1645,7 @@ def product(self, _p: int = 0): ) self.state = 56 if not self.precpred(self._ctx, 2): - from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( FailedPredicateException, ) @@ -1667,7 +1667,7 @@ def product(self, _p: int = 0): ) self.state = 59 if not self.precpred(self._ctx, 1): - from cf_units._udunits2_parser.parser._antlr4_runtime.error.Errors import ( + from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( FailedPredicateException, ) diff --git a/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py b/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py index 3817e550..193cf2c6 100644 --- a/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py +++ b/cf_units/_udunits2_parser/parser/udunits2ParserVisitor.py @@ -1,5 +1,5 @@ # Generated by ANTLR 4.11.1 -from cf_units._udunits2_parser.parser._antlr4_runtime import * +from cf_units._udunits2_parser._antlr4_runtime import * if __name__ is not None and "." in __name__: from .udunits2Parser import udunits2Parser diff --git a/pyproject.toml b/pyproject.toml index 932266ee..09480972 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -110,6 +110,7 @@ local_scheme = "dirty-tag" # Ignore generated code. exclude = [ "cf_units/_udunits2_parser/parser", + "cf_units/_udunits2_parser/_antlr4_runtime", ] line-length = 79 From b9413c90993bed1caa303e21931a0692817f5a70 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Sun, 29 Sep 2024 07:59:50 +0200 Subject: [PATCH 6/9] Use relative imports within the antlr4 runtime. This should be upstreamed --- cf_units/_udunits2_parser/__init__.py | 2 +- .../_antlr4_runtime/BufferedTokenStream.py | 14 +- .../_antlr4_runtime/CommonTokenFactory.py | 2 +- .../_antlr4_runtime/CommonTokenStream.py | 8 +- .../_antlr4_runtime/FileStream.py | 2 +- .../_antlr4_runtime/InputStream.py | 2 +- .../_antlr4_runtime/IntervalSet.py | 2 +- .../_antlr4_runtime/LL1Analyzer.py | 19 +- .../_udunits2_parser/_antlr4_runtime/Lexer.py | 12 +- .../_antlr4_runtime/ListTokenSource.py | 8 +- .../_antlr4_runtime/Parser.py | 49 ++--- .../_antlr4_runtime/ParserInterpreter.py | 37 ++-- .../_antlr4_runtime/ParserRuleContext.py | 6 +- .../_antlr4_runtime/PredictionContext.py | 27 +-- .../_antlr4_runtime/Recognizer.py | 19 +- .../_antlr4_runtime/RuleContext.py | 8 +- .../_antlr4_runtime/StdinStream.py | 2 +- .../_antlr4_runtime/TokenStreamRewriter.py | 2 +- .../_antlr4_runtime/__init__.py | 63 ++---- .../_antlr4_runtime/_pygrun.py | 186 ------------------ .../_antlr4_runtime/atn/ATN.py | 17 +- .../_antlr4_runtime/atn/ATNConfig.py | 17 +- .../_antlr4_runtime/atn/ATNConfigSet.py | 24 +-- .../_antlr4_runtime/atn/ATNDeserializer.py | 24 +-- .../_antlr4_runtime/atn/ATNSimulator.py | 10 +- .../_antlr4_runtime/atn/ATNState.py | 2 +- .../_antlr4_runtime/atn/LexerATNSimulator.py | 38 ++-- .../atn/LexerActionExecutor.py | 7 +- .../_antlr4_runtime/atn/ParserATNSimulator.py | 116 +++++------ .../_antlr4_runtime/atn/PredictionMode.py | 16 +- .../_antlr4_runtime/atn/SemanticContext.py | 7 +- .../_antlr4_runtime/atn/Transition.py | 11 +- .../_antlr4_runtime/dfa/DFA.py | 23 +-- .../_antlr4_runtime/dfa/DFASerializer.py | 6 +- .../_antlr4_runtime/dfa/DFAState.py | 8 +- .../error/DiagnosticErrorListener.py | 11 +- .../_antlr4_runtime/error/ErrorStrategy.py | 8 +- .../_antlr4_runtime/error/Errors.py | 13 +- .../_antlr4_runtime/tree/ParseTreeMatch.py | 6 +- .../_antlr4_runtime/tree/ParseTreePattern.py | 6 +- .../tree/ParseTreePatternMatcher.py | 58 ++---- .../_antlr4_runtime/tree/RuleTagToken.py | 2 +- .../_antlr4_runtime/tree/TokenTagToken.py | 2 +- .../_antlr4_runtime/tree/Tree.py | 2 +- .../_antlr4_runtime/tree/Trees.py | 16 +- .../_antlr4_runtime/xpath/XPath.py | 29 +-- .../_antlr4_runtime/xpath/XPathLexer.py | 11 +- cf_units/_udunits2_parser/compile.py | 63 +++++- cf_units/tests/test_coding_standards.py | 1 + cf_units/tests/test_tex.py | 4 - cf_units/tex.py | 4 +- 51 files changed, 327 insertions(+), 705 deletions(-) delete mode 100644 cf_units/_udunits2_parser/_antlr4_runtime/_pygrun.py diff --git a/cf_units/_udunits2_parser/__init__.py b/cf_units/_udunits2_parser/__init__.py index 6041409a..63113cc5 100644 --- a/cf_units/_udunits2_parser/__init__.py +++ b/cf_units/_udunits2_parser/__init__.py @@ -10,7 +10,7 @@ CommonTokenStream, InputStream, ) -from ._antlr4_runtime.error import ( +from ._antlr4_runtime.error.ErrorListener import ( ErrorListener, ) from .parser.udunits2Lexer import udunits2Lexer diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/BufferedTokenStream.py b/cf_units/_udunits2_parser/_antlr4_runtime/BufferedTokenStream.py index 530272c3..a9b6ec4f 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/BufferedTokenStream.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/BufferedTokenStream.py @@ -15,10 +15,8 @@ # {@link CommonTokenStream}.

      from io import StringIO -from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( - IllegalStateException, -) -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from .error.Errors import IllegalStateException +from .Token import Token # need forward declaration Lexer = None @@ -233,7 +231,7 @@ def getHiddenTokensToRight(self, tokenIndex: int, channel: int = -1): raise Exception( str(tokenIndex) + " not in 0.." + str(len(self.tokens) - 1) ) - from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer + from .Lexer import Lexer nextOnChannel = self.nextTokenOnChannel( tokenIndex + 1, Lexer.DEFAULT_TOKEN_CHANNEL @@ -252,7 +250,7 @@ def getHiddenTokensToLeft(self, tokenIndex: int, channel: int = -1): raise Exception( str(tokenIndex) + " not in 0.." + str(len(self.tokens) - 1) ) - from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer + from .Lexer import Lexer prevOnChannel = self.previousTokenOnChannel( tokenIndex - 1, Lexer.DEFAULT_TOKEN_CHANNEL @@ -269,9 +267,7 @@ def filterForChannel(self, left: int, right: int, channel: int): for i in range(left, right + 1): t = self.tokens[i] if channel == -1: - from cf_units._udunits2_parser._antlr4_runtime.Lexer import ( - Lexer, - ) + from .Lexer import Lexer if t.channel != Lexer.DEFAULT_TOKEN_CHANNEL: hidden.append(t) diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/CommonTokenFactory.py b/cf_units/_udunits2_parser/_antlr4_runtime/CommonTokenFactory.py index 0e5730bc..9e8d2d73 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/CommonTokenFactory.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/CommonTokenFactory.py @@ -8,7 +8,7 @@ # This default implementation of {@link TokenFactory} creates # {@link CommonToken} objects. # -from cf_units._udunits2_parser._antlr4_runtime.Token import CommonToken +from .Token import CommonToken class TokenFactory: diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/CommonTokenStream.py b/cf_units/_udunits2_parser/_antlr4_runtime/CommonTokenStream.py index ad8a33a2..75675675 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/CommonTokenStream.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/CommonTokenStream.py @@ -29,11 +29,9 @@ # channel.

      # / -from cf_units._udunits2_parser._antlr4_runtime.BufferedTokenStream import ( - BufferedTokenStream, -) -from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from .BufferedTokenStream import BufferedTokenStream +from .Lexer import Lexer +from .Token import Token class CommonTokenStream(BufferedTokenStream): diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/FileStream.py b/cf_units/_udunits2_parser/_antlr4_runtime/FileStream.py index 54a8be25..a27bb0d8 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/FileStream.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/FileStream.py @@ -11,7 +11,7 @@ import codecs -from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream +from .InputStream import InputStream class FileStream(InputStream): diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/InputStream.py b/cf_units/_udunits2_parser/_antlr4_runtime/InputStream.py index 5c881bb7..4a816212 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/InputStream.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/InputStream.py @@ -8,7 +8,7 @@ # # Vacuum all input from a string and then treat it like a buffer. # -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from .Token import Token class InputStream: diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/IntervalSet.py b/cf_units/_udunits2_parser/_antlr4_runtime/IntervalSet.py index 326c297f..bc5664a6 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/IntervalSet.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/IntervalSet.py @@ -6,7 +6,7 @@ from io import StringIO -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from .Token import Token # need forward declarations IntervalSet = None diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/LL1Analyzer.py b/cf_units/_udunits2_parser/_antlr4_runtime/LL1Analyzer.py index 88410306..f06d1952 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/LL1Analyzer.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/LL1Analyzer.py @@ -3,26 +3,23 @@ # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. # / -from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfig import ATNConfig -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( - ATNState, - RuleStopState, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.Transition import ( +from .atn.ATN import ATN +from .atn.ATNConfig import ATNConfig +from .atn.ATNState import ATNState, RuleStopState +from .atn.Transition import ( AbstractPredicateTransition, NotSetTransition, RuleTransition, WildcardTransition, ) -from cf_units._udunits2_parser._antlr4_runtime.IntervalSet import IntervalSet -from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import ( +from .IntervalSet import IntervalSet +from .PredictionContext import ( PredictionContext, PredictionContextFromRuleContext, SingletonPredictionContext, ) -from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from .RuleContext import RuleContext +from .Token import Token class LL1Analyzer: diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/Lexer.py b/cf_units/_udunits2_parser/_antlr4_runtime/Lexer.py index 127edbc8..d1e0c888 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/Lexer.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/Lexer.py @@ -15,17 +15,15 @@ from typing import TextIO else: from typing.io import TextIO -from cf_units._udunits2_parser._antlr4_runtime.CommonTokenFactory import ( - CommonTokenFactory, -) -from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( +from .CommonTokenFactory import CommonTokenFactory +from .error.Errors import ( IllegalStateException, LexerNoViableAltException, RecognitionException, ) -from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream -from cf_units._udunits2_parser._antlr4_runtime.Recognizer import Recognizer -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from .InputStream import InputStream +from .Recognizer import Recognizer +from .Token import Token class TokenSource: diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/ListTokenSource.py b/cf_units/_udunits2_parser/_antlr4_runtime/ListTokenSource.py index b3255bb3..05236021 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/ListTokenSource.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/ListTokenSource.py @@ -12,11 +12,9 @@ # as the EOF token for every call to {@link #nextToken} after the end of the # list is reached. Otherwise, an EOF token will be created.

      # -from cf_units._udunits2_parser._antlr4_runtime.CommonTokenFactory import ( - CommonTokenFactory, -) -from cf_units._udunits2_parser._antlr4_runtime.Lexer import TokenSource -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from .CommonTokenFactory import CommonTokenFactory +from .Lexer import TokenSource +from .Token import Token class ListTokenSource(TokenSource): diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/Parser.py b/cf_units/_udunits2_parser/_antlr4_runtime/Parser.py index e5aeaef4..d8e10ea0 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/Parser.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/Parser.py @@ -8,41 +8,20 @@ from typing import TextIO else: from typing.io import TextIO -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNDeserializationOptions import ( - ATNDeserializationOptions, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNDeserializer import ( - ATNDeserializer, -) -from cf_units._udunits2_parser._antlr4_runtime.BufferedTokenStream import ( - TokenStream, -) -from cf_units._udunits2_parser._antlr4_runtime.CommonTokenFactory import ( - TokenFactory, -) -from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( - RecognitionException, - UnsupportedOperationException, -) -from cf_units._udunits2_parser._antlr4_runtime.error.ErrorStrategy import ( - DefaultErrorStrategy, -) -from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream -from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer -from cf_units._udunits2_parser._antlr4_runtime.ParserRuleContext import ( - ParserRuleContext, -) -from cf_units._udunits2_parser._antlr4_runtime.Recognizer import Recognizer -from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext -from cf_units._udunits2_parser._antlr4_runtime.Token import Token -from cf_units._udunits2_parser._antlr4_runtime.tree.ParseTreePatternMatcher import ( - ParseTreePatternMatcher, -) -from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ( - ErrorNode, - ParseTreeListener, - TerminalNode, -) +from .atn.ATNDeserializationOptions import ATNDeserializationOptions +from .atn.ATNDeserializer import ATNDeserializer +from .BufferedTokenStream import TokenStream +from .CommonTokenFactory import TokenFactory +from .error.Errors import RecognitionException, UnsupportedOperationException +from .error.ErrorStrategy import DefaultErrorStrategy +from .InputStream import InputStream +from .Lexer import Lexer +from .ParserRuleContext import ParserRuleContext +from .Recognizer import Recognizer +from .RuleContext import RuleContext +from .Token import Token +from .tree.ParseTreePatternMatcher import ParseTreePatternMatcher +from .tree.Tree import ErrorNode, ParseTreeListener, TerminalNode class TraceListener(ParseTreeListener): diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/ParserInterpreter.py b/cf_units/_udunits2_parser/_antlr4_runtime/ParserInterpreter.py index f4ce5672..2298b813 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/ParserInterpreter.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/ParserInterpreter.py @@ -17,35 +17,22 @@ # # See TestParserInterpreter for examples. # -from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( - ATNState, - LoopEndState, - StarLoopEntryState, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.ParserATNSimulator import ( - ParserATNSimulator, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.Transition import Transition -from cf_units._udunits2_parser._antlr4_runtime.BufferedTokenStream import ( - TokenStream, -) -from cf_units._udunits2_parser._antlr4_runtime.dfa.DFA import DFA -from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( +from .atn.ATN import ATN +from .atn.ATNState import ATNState, LoopEndState, StarLoopEntryState +from .atn.ParserATNSimulator import ParserATNSimulator +from .atn.Transition import Transition +from .BufferedTokenStream import TokenStream +from .dfa.DFA import DFA +from .error.Errors import ( FailedPredicateException, RecognitionException, UnsupportedOperationException, ) -from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer -from cf_units._udunits2_parser._antlr4_runtime.Parser import Parser -from cf_units._udunits2_parser._antlr4_runtime.ParserRuleContext import ( - InterpreterRuleContext, - ParserRuleContext, -) -from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import ( - PredictionContextCache, -) -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from .Lexer import Lexer +from .Parser import Parser +from .ParserRuleContext import InterpreterRuleContext, ParserRuleContext +from .PredictionContext import PredictionContextCache +from .Token import Token class ParserInterpreter(Parser): diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/ParserRuleContext.py b/cf_units/_udunits2_parser/_antlr4_runtime/ParserRuleContext.py index 1f9e55fd..c71029bb 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/ParserRuleContext.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/ParserRuleContext.py @@ -25,9 +25,9 @@ # group values such as this aggregate. The getters/setters are there to # satisfy the superclass interface. -from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext -from cf_units._udunits2_parser._antlr4_runtime.Token import Token -from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ( +from .RuleContext import RuleContext +from .Token import Token +from .tree.Tree import ( INVALID_INTERVAL, ErrorNodeImpl, ParseTree, diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/PredictionContext.py b/cf_units/_udunits2_parser/_antlr4_runtime/PredictionContext.py index 603da8d7..96797116 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/PredictionContext.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/PredictionContext.py @@ -5,14 +5,9 @@ # / from io import StringIO -from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( - IllegalStateException, -) -from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext - -# dup ParserATNSimulator class var here to avoid circular import; no idea why this can't be in PredictionContext -_trace_atn_sim = False +from .atn.ATN import ATN +from .error.Errors import IllegalStateException +from .RuleContext import RuleContext class PredictionContext: @@ -488,17 +483,9 @@ def mergeArrays( if mergeCache is not None: previous = mergeCache.get((a, b), None) if previous is not None: - if _trace_atn_sim: - print( - "mergeArrays a=" + str(a) + ",b=" + str(b) + " -> previous" - ) return previous previous = mergeCache.get((b, a), None) if previous is not None: - if _trace_atn_sim: - print( - "mergeArrays a=" + str(a) + ",b=" + str(b) + " -> previous" - ) return previous # merge sorted payloads a + b => M @@ -576,23 +563,15 @@ def mergeArrays( if merged == a: if mergeCache is not None: mergeCache[(a, b)] = a - if _trace_atn_sim: - print("mergeArrays a=" + str(a) + ",b=" + str(b) + " -> a") return a if merged == b: if mergeCache is not None: mergeCache[(a, b)] = b - if _trace_atn_sim: - print("mergeArrays a=" + str(a) + ",b=" + str(b) + " -> b") return b combineCommonParents(mergedParents) if mergeCache is not None: mergeCache[(a, b)] = merged - - if _trace_atn_sim: - print("mergeArrays a=" + str(a) + ",b=" + str(b) + " -> " + str(M)) - return merged diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/Recognizer.py b/cf_units/_udunits2_parser/_antlr4_runtime/Recognizer.py index 75886836..f827ec86 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/Recognizer.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/Recognizer.py @@ -3,12 +3,9 @@ # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. # -from cf_units._udunits2_parser._antlr4_runtime.error.ErrorListener import ( - ConsoleErrorListener, - ProxyErrorListener, -) -from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from .error.ErrorListener import ConsoleErrorListener, ProxyErrorListener +from .RuleContext import RuleContext +from .Token import Token # need forward delcaration RecognitionException = None @@ -38,7 +35,7 @@ def extractVersion(self, version): return major, minor def checkVersion(self, toolVersion): - runtimeVersion = "4.13.2" + runtimeVersion = "4.11.1" rvmajor, rvminor = self.extractVersion(runtimeVersion) tvmajor, tvminor = self.extractVersion(toolVersion) if rvmajor != tvmajor or rvminor != tvminor: @@ -61,9 +58,7 @@ def removeErrorListeners(self): def getTokenTypeMap(self): tokenNames = self.getTokenNames() if tokenNames is None: - from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( - UnsupportedOperationException, - ) + from .error.Errors import UnsupportedOperationException raise UnsupportedOperationException( "The current recognizer does not provide a list of token names." @@ -82,9 +77,7 @@ def getTokenTypeMap(self): def getRuleIndexMap(self): ruleNames = self.getRuleNames() if ruleNames is None: - from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( - UnsupportedOperationException, - ) + from .error.Errors import UnsupportedOperationException raise UnsupportedOperationException( "The current recognizer does not provide a list of rule names." diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/RuleContext.py b/cf_units/_udunits2_parser/_antlr4_runtime/RuleContext.py index 31d83f49..ea9337c2 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/RuleContext.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/RuleContext.py @@ -26,12 +26,8 @@ # / from io import StringIO -from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ( - INVALID_INTERVAL, - ParseTreeVisitor, - RuleNode, -) -from cf_units._udunits2_parser._antlr4_runtime.tree.Trees import Trees +from .tree.Tree import INVALID_INTERVAL, ParseTreeVisitor, RuleNode +from .tree.Trees import Trees # need forward declarations RuleContext = None diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/StdinStream.py b/cf_units/_udunits2_parser/_antlr4_runtime/StdinStream.py index d41ae61e..05d85fc8 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/StdinStream.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/StdinStream.py @@ -1,7 +1,7 @@ import codecs import sys -from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream +from .InputStream import InputStream class StdinStream(InputStream): diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/TokenStreamRewriter.py b/cf_units/_udunits2_parser/_antlr4_runtime/TokenStreamRewriter.py index cf57cb30..bbca7a27 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/TokenStreamRewriter.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/TokenStreamRewriter.py @@ -6,7 +6,7 @@ from io import StringIO -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from .Token import Token class TokenStreamRewriter: diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/__init__.py b/cf_units/_udunits2_parser/_antlr4_runtime/__init__.py index e4a633f9..0691a3b1 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/__init__.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/__init__.py @@ -1,48 +1,27 @@ -from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNDeserializer import ( - ATNDeserializer, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.LexerATNSimulator import ( - LexerATNSimulator, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.ParserATNSimulator import ( - ParserATNSimulator, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.PredictionMode import ( - PredictionMode, -) -from cf_units._udunits2_parser._antlr4_runtime.BufferedTokenStream import ( - TokenStream, -) -from cf_units._udunits2_parser._antlr4_runtime.CommonTokenStream import ( - CommonTokenStream, -) -from cf_units._udunits2_parser._antlr4_runtime.dfa.DFA import DFA -from cf_units._udunits2_parser._antlr4_runtime.error.DiagnosticErrorListener import ( - DiagnosticErrorListener, -) -from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( +from .atn.ATN import ATN +from .atn.ATNDeserializer import ATNDeserializer +from .atn.LexerATNSimulator import LexerATNSimulator +from .atn.ParserATNSimulator import ParserATNSimulator +from .atn.PredictionMode import PredictionMode +from .BufferedTokenStream import TokenStream +from .CommonTokenStream import CommonTokenStream +from .dfa.DFA import DFA +from .error.DiagnosticErrorListener import DiagnosticErrorListener +from .error.Errors import ( IllegalStateException, NoViableAltException, RecognitionException, ) -from cf_units._udunits2_parser._antlr4_runtime.error.ErrorStrategy import ( - BailErrorStrategy, -) -from cf_units._udunits2_parser._antlr4_runtime.FileStream import FileStream -from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream -from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer -from cf_units._udunits2_parser._antlr4_runtime.Parser import Parser -from cf_units._udunits2_parser._antlr4_runtime.ParserRuleContext import ( - ParserRuleContext, - RuleContext, -) -from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import ( - PredictionContextCache, -) -from cf_units._udunits2_parser._antlr4_runtime.StdinStream import StdinStream -from cf_units._udunits2_parser._antlr4_runtime.Token import Token -from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ( +from .error.ErrorStrategy import BailErrorStrategy +from .FileStream import FileStream +from .InputStream import InputStream +from .Lexer import Lexer +from .Parser import Parser +from .ParserRuleContext import ParserRuleContext, RuleContext +from .PredictionContext import PredictionContextCache +from .StdinStream import StdinStream +from .Token import Token +from .tree.Tree import ( ErrorNode, ParseTreeListener, ParseTreeVisitor, @@ -50,4 +29,4 @@ RuleNode, TerminalNode, ) -from cf_units._udunits2_parser._antlr4_runtime.Utils import str_list +from .Utils import str_list diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/_pygrun.py b/cf_units/_udunits2_parser/_antlr4_runtime/_pygrun.py deleted file mode 100644 index 23089137..00000000 --- a/cf_units/_udunits2_parser/_antlr4_runtime/_pygrun.py +++ /dev/null @@ -1,186 +0,0 @@ -#!python -__author__ = "jszheng" -import optparse -import os -import sys - -from cf_units._udunits2_parser._antlr4_runtime import * - - -# this is a python version of TestRig -def beautify_lisp_string(in_string): - indent_size = 3 - add_indent = " " * indent_size - out_string = in_string[0] # no indent for 1st ( - indent = "" - for i in range(1, len(in_string)): - if in_string[i] == "(" and in_string[i + 1] != " ": - indent += add_indent - out_string += "\n" + indent + "(" - elif in_string[i] == ")": - out_string += ")" - if len(indent) > 0: - indent = indent.replace(add_indent, "", 1) - else: - out_string += in_string[i] - return out_string - - -def main(): - ############################################################# - # parse options - # not support -gui -encoding -ps - ############################################################# - usage = "Usage: %prog [options] Grammar_Name Start_Rule" - parser = optparse.OptionParser(usage=usage) - # parser.add_option('-t', '--tree', - # dest="out_file", - # default="default.out", - # help='set output file name', - # ) - parser.add_option( - "-t", - "--tree", - default=False, - action="store_true", - help="Print AST tree", - ) - parser.add_option( - "-k", - "--tokens", - dest="token", - default=False, - action="store_true", - help="Show Tokens", - ) - parser.add_option( - "-s", - "--sll", - dest="sll", - default=False, - action="store_true", - help="Show SLL", - ) - parser.add_option( - "-d", - "--diagnostics", - dest="diagnostics", - default=False, - action="store_true", - help="Enable diagnostics error listener", - ) - parser.add_option( - "-a", - "--trace", - dest="trace", - default=False, - action="store_true", - help="Enable Trace", - ) - - options, remainder = parser.parse_args() - if len(remainder) < 2: - print("ERROR: You have to provide at least 2 arguments!") - parser.print_help() - exit(1) - else: - grammar = remainder.pop(0) - start_rule = remainder.pop(0) - file_list = remainder - - ############################################################# - # check and load antlr generated files - ############################################################# - # dynamic load the module and class - lexerName = grammar + "Lexer" - parserName = grammar + "Parser" - # check if the generate file exist - lexer_file = lexerName + ".py" - parser_file = parserName + ".py" - if not os.path.exists(lexer_file): - print(f"[ERROR] Can't find lexer file {lexer_file}!") - print(os.path.realpath(".")) - exit(1) - if not os.path.exists(parser_file): - print(f"[ERROR] Can't find parser file {lexer_file}!") - print(os.path.realpath(".")) - exit(1) - - # current directory is where the generated file loaded - # the script might be in different place. - sys.path.append(".") - # print(sys.path) - - # add current directory to python global namespace in case of relative imports - globals().update({"__package__": os.path.basename(os.getcwd())}) - - # print("Load Lexer {}".format(lexerName)) - module_lexer = __import__(lexerName, globals(), locals(), lexerName) - class_lexer = getattr(module_lexer, lexerName) - # print(class_lexer) - - # print("Load Parser {}".format(parserName)) - module_parser = __import__(parserName, globals(), locals(), parserName) - class_parser = getattr(module_parser, parserName) - # print(class_parser) - - ############################################################# - # main process steps. - ############################################################# - def process(input_stream, class_lexer, class_parser): - lexer = class_lexer(input_stream) - token_stream = CommonTokenStream(lexer) - token_stream.fill() - if options.token: # need to show token - for tok in token_stream.tokens: - print(tok) - if start_rule == "tokens": - return - - parser = class_parser(token_stream) - - if options.diagnostics: - parser.addErrorListener(DiagnosticErrorListener()) - parser._interp.predictionMode = ( - PredictionMode.LL_EXACT_AMBIG_DETECTION - ) - if options.tree: - parser.buildParseTrees = True - if options.sll: - parser._interp.predictionMode = PredictionMode.SLL - # parser.setTokenStream(token_stream) - parser.setTrace(options.trace) - if hasattr(parser, start_rule): - func_start_rule = getattr(parser, start_rule) - parser_ret = func_start_rule() - if options.tree: - lisp_tree_str = parser_ret.toStringTree(recog=parser) - print(beautify_lisp_string(lisp_tree_str)) - else: - print( - f"[ERROR] Can't find start rule '{start_rule}' in parser '{parserName}'" - ) - - ############################################################# - # use stdin if not provide file as input stream - ############################################################# - if len(file_list) == 0: - input_stream = InputStream(sys.stdin.read()) - process(input_stream, class_lexer, class_parser) - exit(0) - - ############################################################# - # iterate all input file - ############################################################# - for file_name in file_list: - if os.path.exists(file_name) and os.path.isfile(file_name): - input_stream = FileStream(file_name) - process(input_stream, class_lexer, class_parser) - else: - print( - f"[ERROR] file {os.path.normpath(file_name)} not exist" - ) - - -if __name__ == "__main__": - main() diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATN.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATN.py index ba7fb2b2..906adcaa 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATN.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATN.py @@ -2,14 +2,11 @@ # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. # / -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( - ATNState, - DecisionState, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNType import ATNType -from cf_units._udunits2_parser._antlr4_runtime.IntervalSet import IntervalSet -from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from ..atn.ATNState import ATNState, DecisionState +from ..atn.ATNType import ATNType +from ..IntervalSet import IntervalSet +from ..RuleContext import RuleContext +from ..Token import Token class ATN: @@ -60,9 +57,7 @@ def __init__(self, grammarType: ATNType, maxTokenType: int): # the rule surrounding {@code s}. In other words, the set will be # restricted to tokens reachable staying within {@code s}'s rule. def nextTokensInContext(self, s: ATNState, ctx: RuleContext): - from cf_units._udunits2_parser._antlr4_runtime.LL1Analyzer import ( - LL1Analyzer, - ) + from ..LL1Analyzer import LL1Analyzer anal = LL1Analyzer(self) return anal.LOOK(s, ctx=ctx) diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNConfig.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNConfig.py index 6aebaa3e..4ae5d16e 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNConfig.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNConfig.py @@ -13,19 +13,10 @@ # / from io import StringIO -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( - ATNState, - DecisionState, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.LexerActionExecutor import ( - LexerActionExecutor, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.SemanticContext import ( - SemanticContext, -) -from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import ( - PredictionContext, -) +from ..atn.ATNState import ATNState, DecisionState +from ..atn.LexerActionExecutor import LexerActionExecutor +from ..atn.SemanticContext import SemanticContext +from ..PredictionContext import PredictionContext # need a forward declaration ATNConfig = None diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNConfigSet.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNConfigSet.py index 97a9dd20..a0998a0f 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNConfigSet.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNConfigSet.py @@ -3,26 +3,20 @@ # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. -from functools import reduce - # # Specialized {@link Set}{@code <}{@link ATNConfig}{@code >} that can track # info about the set, with support for combining similar configurations using a # graph-structured stack. # / +from functools import reduce from io import StringIO -from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfig import ATNConfig -from cf_units._udunits2_parser._antlr4_runtime.atn.SemanticContext import ( - SemanticContext, -) -from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( - IllegalStateException, - UnsupportedOperationException, -) -from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import merge -from cf_units._udunits2_parser._antlr4_runtime.Utils import str_list +from ..atn.ATN import ATN +from ..atn.ATNConfig import ATNConfig +from ..atn.SemanticContext import SemanticContext +from ..error.Errors import IllegalStateException, UnsupportedOperationException +from ..PredictionContext import merge +from ..Utils import str_list ATNSimulator = None @@ -222,9 +216,7 @@ def __str__(self): buf.write(str_list(self.configs)) if self.hasSemanticContext: buf.write(",hasSemanticContext=") - buf.write( - str(self.hasSemanticContext).lower() - ) # lower() to conform to java output + buf.write(str(self.hasSemanticContext)) if self.uniqueAlt != ATN.INVALID_ALT_NUMBER: buf.write(",uniqueAlt=") buf.write(str(self.uniqueAlt)) diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNDeserializer.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNDeserializer.py index a8e74536..021549f3 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNDeserializer.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNDeserializer.py @@ -2,15 +2,13 @@ # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. # / -from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNDeserializationOptions import ( - ATNDeserializationOptions, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import * -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNType import ATNType -from cf_units._udunits2_parser._antlr4_runtime.atn.LexerAction import * -from cf_units._udunits2_parser._antlr4_runtime.atn.Transition import * -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from ..atn.ATN import ATN +from ..atn.ATNDeserializationOptions import ATNDeserializationOptions +from ..atn.ATNState import * +from ..atn.ATNType import ATNType +from ..atn.LexerAction import * +from ..atn.Transition import * +from ..Token import Token SERIALIZED_VERSION = 4 @@ -23,7 +21,7 @@ def __init__(self, options: ATNDeserializationOptions = None): options = ATNDeserializationOptions.defaultOptions self.deserializationOptions = options - def deserialize(self, data: [int]): + def deserialize(self, data: int): self.data = data self.pos = 0 self.checkVersion() @@ -51,7 +49,11 @@ def checkVersion(self): version = self.readInt() if version != SERIALIZED_VERSION: raise Exception( - f"Could not deserialize ATN with version {ord(version)} (expected {SERIALIZED_VERSION})." + "Could not deserialize ATN with version " + + str(version) + + " (expected " + + str(SERIALIZED_VERSION) + + ")." ) def readATN(self): diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNSimulator.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNSimulator.py index f9037583..08b68e19 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNSimulator.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNSimulator.py @@ -3,12 +3,10 @@ # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. # / -from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfigSet import ( - ATNConfigSet, -) -from cf_units._udunits2_parser._antlr4_runtime.dfa.DFAState import DFAState -from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import ( +from ..atn.ATN import ATN +from ..atn.ATNConfigSet import ATNConfigSet +from ..dfa.DFAState import DFAState +from ..PredictionContext import ( PredictionContext, PredictionContextCache, getCachedPredictionContext, diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNState.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNState.py index 332b14e9..131ecbfc 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNState.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ATNState.py @@ -64,7 +64,7 @@ # # -from cf_units._udunits2_parser._antlr4_runtime.atn.Transition import Transition +from ..atn.Transition import Transition INITIAL_NUM_TRANSITIONS = 4 diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerATNSimulator.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerATNSimulator.py index a3e53f20..915b911f 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerATNSimulator.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerATNSimulator.py @@ -20,37 +20,25 @@ # can simply return the predicted token type.

      # / -from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfig import ( - LexerATNConfig, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfigSet import ( - ATNConfigSet, - OrderedATNConfigSet, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNSimulator import ( - ATNSimulator, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( - ATNState, - RuleStopState, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.LexerActionExecutor import ( - LexerActionExecutor, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.Transition import Transition -from cf_units._udunits2_parser._antlr4_runtime.dfa.DFAState import DFAState -from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( +from ..atn.ATN import ATN +from ..atn.ATNConfig import LexerATNConfig +from ..atn.ATNConfigSet import ATNConfigSet, OrderedATNConfigSet +from ..atn.ATNSimulator import ATNSimulator +from ..atn.ATNState import ATNState, RuleStopState +from ..atn.LexerActionExecutor import LexerActionExecutor +from ..atn.Transition import Transition +from ..dfa.DFAState import DFAState +from ..error.Errors import ( LexerNoViableAltException, UnsupportedOperationException, ) -from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream -from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import ( +from ..InputStream import InputStream +from ..PredictionContext import ( PredictionContext, PredictionContextCache, SingletonPredictionContext, ) -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from ..Token import Token class SimState: @@ -111,7 +99,7 @@ def __init__( self.line = 1 # The index of the character relative to the beginning of the line 0..n-1#/ self.column = 0 - from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer + from ..Lexer import Lexer self.mode = Lexer.DEFAULT_MODE # Cache Lexer properties to avoid further imports diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerActionExecutor.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerActionExecutor.py index 726d59a9..37945b59 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerActionExecutor.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/LexerActionExecutor.py @@ -12,11 +12,8 @@ # not cause bloating of the {@link DFA} created for the lexer.

      -from cf_units._udunits2_parser._antlr4_runtime.atn.LexerAction import ( - LexerAction, - LexerIndexedCustomAction, -) -from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream +from ..atn.LexerAction import LexerAction, LexerIndexedCustomAction +from ..InputStream import InputStream # need a forward declaration Lexer = None diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/atn/ParserATNSimulator.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ParserATNSimulator.py index dff63378..60425b1d 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/atn/ParserATNSimulator.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/ParserATNSimulator.py @@ -233,29 +233,18 @@ # import sys -from cf_units._udunits2_parser._antlr4_runtime import DFA -from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfig import ATNConfig -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfigSet import ( - ATNConfigSet, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNSimulator import ( - ATNSimulator, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( +from ..atn.ATN import ATN +from ..atn.ATNConfig import ATNConfig +from ..atn.ATNConfigSet import ATNConfigSet +from ..atn.ATNSimulator import ATNSimulator +from ..atn.ATNState import ( ATNState, DecisionState, RuleStopState, ) -from cf_units._udunits2_parser._antlr4_runtime.atn.PredictionMode import ( - PredictionMode, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.SemanticContext import ( - SemanticContext, - andContext, - orContext, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.Transition import ( +from ..atn.PredictionMode import PredictionMode +from ..atn.SemanticContext import SemanticContext, andContext, orContext +from ..atn.Transition import ( ActionTransition, AtomTransition, NotSetTransition, @@ -265,29 +254,21 @@ SetTransition, Transition, ) -from cf_units._udunits2_parser._antlr4_runtime.BufferedTokenStream import ( - TokenStream, -) -from cf_units._udunits2_parser._antlr4_runtime.dfa.DFAState import ( - DFAState, - PredPrediction, -) -from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( - NoViableAltException, -) -from cf_units._udunits2_parser._antlr4_runtime.Parser import Parser -from cf_units._udunits2_parser._antlr4_runtime.ParserRuleContext import ( - ParserRuleContext, -) -from cf_units._udunits2_parser._antlr4_runtime.PredictionContext import ( +from ..BufferedTokenStream import TokenStream +from ..dfa.DFA import DFA +from ..dfa.DFAState import DFAState, PredPrediction +from ..error.Errors import NoViableAltException +from ..Parser import Parser +from ..ParserRuleContext import ParserRuleContext +from ..PredictionContext import ( PredictionContext, PredictionContextCache, PredictionContextFromRuleContext, SingletonPredictionContext, ) -from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext -from cf_units._udunits2_parser._antlr4_runtime.Token import Token -from cf_units._udunits2_parser._antlr4_runtime.Utils import str_list +from ..RuleContext import RuleContext +from ..Token import Token +from ..Utils import str_list class ParserATNSimulator(ATNSimulator): @@ -303,7 +284,7 @@ class ParserATNSimulator(ATNSimulator): ) debug = False - trace_atn_sim = False + debug_list_atn_decisions = False dfa_debug = False retry_debug = False @@ -343,7 +324,10 @@ def adaptivePredict( decision: int, outerContext: ParserRuleContext, ): - if ParserATNSimulator.debug or ParserATNSimulator.trace_atn_sim: + if ( + ParserATNSimulator.debug + or ParserATNSimulator.debug_list_atn_decisions + ): print( "adaptivePredict decision " + str(decision) @@ -377,15 +361,18 @@ def adaptivePredict( if s0 is None: if outerContext is None: outerContext = ParserRuleContext.EMPTY - if ParserATNSimulator.debug: + if ( + ParserATNSimulator.debug + or ParserATNSimulator.debug_list_atn_decisions + ): print( "predictATN decision " + str(dfa.decision) + " exec LA(1)==" + self.getLookaheadName(input) + ", outerContext=" - + str(outerContext) - ) # outerContext.toString(self.parser.literalNames, None)) + + outerContext.toString(self.parser.literalNames, None) + ) fullCtx = False s0_closure = self.computeStartState( @@ -460,13 +447,14 @@ def execATN( startIndex: int, outerContext: ParserRuleContext, ): - if ParserATNSimulator.debug or ParserATNSimulator.trace_atn_sim: + if ( + ParserATNSimulator.debug + or ParserATNSimulator.debug_list_atn_decisions + ): print( "execATN decision " + str(dfa.decision) - + ", DFA state " - + str(s0) - + ", LA(1)==" + + " exec LA(1)==" + self.getLookaheadName(input) + " line " + str(input.LT(1).line) @@ -476,6 +464,9 @@ def execATN( previousD = s0 + if ParserATNSimulator.debug: + print("s0 = " + str(s0)) + t = input.LA(1) while True: # while more work @@ -694,7 +685,10 @@ def execATNWithFullContext( startIndex: int, outerContext: ParserRuleContext, ): - if ParserATNSimulator.debug or ParserATNSimulator.trace_atn_sim: + if ( + ParserATNSimulator.debug + or ParserATNSimulator.debug_list_atn_decisions + ): print("execATNWithFullContext", str(s0)) fullCtx = True foundExactAmbig = False @@ -927,10 +921,6 @@ def computeReachSet(self, closure: ATNConfigSet, t: int, fullCtx: bool): ): for c in skippedStopStates: reach.add(c, self.mergeCache) - - if ParserATNSimulator.trace_atn_sim: - print("computeReachSet", str(closure), "->", reach) - if len(reach) == 0: return None else: @@ -983,14 +973,6 @@ def computeStartState(self, p: ATNState, ctx: RuleContext, fullCtx: bool): initialContext = PredictionContextFromRuleContext(self.atn, ctx) configs = ATNConfigSet(fullCtx) - if ParserATNSimulator.trace_atn_sim: - print( - "computeStartState from ATN state " - + str(p) - + " initialContext=" - + str(initialContext) - ) - for i in range(0, len(p.transitions)): target = p.transitions[i].target c = ATNConfig(target, i + 1, initialContext) @@ -1334,7 +1316,7 @@ def closureCheckingStopState( depth: int, treatEofAsEpsilon: bool, ): - if ParserATNSimulator.trace_atn_sim: + if ParserATNSimulator.debug: print("closure(" + str(config) + ")") if isinstance(config.state, RuleStopState): @@ -1960,12 +1942,6 @@ def getTokenName(self, t: int): and t < len(self.parser.literalNames) ): return self.parser.literalNames[t] + "<" + str(t) + ">" - if ( - self.parser is not None - and self.parser.symbolicNames is not None - and t < len(self.parser.symbolicNames) - ): - return self.parser.symbolicNames[t] + "<" + str(t) + ">" else: return str(t) @@ -2085,19 +2061,15 @@ def addDFAState(self, dfa: DFA, D: DFAState): existing = dfa.states.get(D, None) if existing is not None: - if ParserATNSimulator.trace_atn_sim: - print("addDFAState", str(D), "exists") return existing D.stateNumber = len(dfa.states) if not D.configs.readonly: D.configs.optimizeConfigs(self) D.configs.setReadonly(True) - - if ParserATNSimulator.trace_atn_sim: - print("addDFAState new", str(D)) - dfa.states[D] = D + if ParserATNSimulator.debug: + print("adding new DFA state: " + str(D)) return D def reportAttemptingFullContext( diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/atn/PredictionMode.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/PredictionMode.py index cc5a2d8b..8466e371 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/atn/PredictionMode.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/PredictionMode.py @@ -11,17 +11,11 @@ from enum import Enum -from cf_units._udunits2_parser._antlr4_runtime.atn.ATN import ATN -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfig import ATNConfig -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfigSet import ( - ATNConfigSet, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( - RuleStopState, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.SemanticContext import ( - SemanticContext, -) +from ..atn.ATN import ATN +from ..atn.ATNConfig import ATNConfig +from ..atn.ATNConfigSet import ATNConfigSet +from ..atn.ATNState import RuleStopState +from ..atn.SemanticContext import SemanticContext PredictionMode = None diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/atn/SemanticContext.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/SemanticContext.py index 16d1c5cd..af7bac1a 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/atn/SemanticContext.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/SemanticContext.py @@ -13,8 +13,8 @@ # from io import StringIO -from cf_units._udunits2_parser._antlr4_runtime.Recognizer import Recognizer -from cf_units._udunits2_parser._antlr4_runtime.RuleContext import RuleContext +from ..Recognizer import Recognizer +from ..RuleContext import RuleContext class SemanticContext: @@ -170,9 +170,6 @@ def __eq__(self, other): else: return self.precedence == other.precedence - def __str__(self): - return "{" + str(self.precedence) + ">=prec}?" - # A semantic context which is true whenever none of the contained contexts # is false. diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/atn/Transition.py b/cf_units/_udunits2_parser/_antlr4_runtime/atn/Transition.py index 271d02cf..ed5195dd 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/atn/Transition.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/atn/Transition.py @@ -17,12 +17,9 @@ # ATN transitions.

      # # need forward declarations -from cf_units._udunits2_parser._antlr4_runtime.atn.SemanticContext import ( - PrecedencePredicate, - Predicate, -) -from cf_units._udunits2_parser._antlr4_runtime.IntervalSet import IntervalSet -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from ..atn.SemanticContext import PrecedencePredicate, Predicate +from ..IntervalSet import IntervalSet +from ..Token import Token ATNState = None RuleStartState = None @@ -307,4 +304,4 @@ def __str__(self): del ATNState del RuleStartState -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import * +from ..atn.ATNState import * diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFA.py b/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFA.py index e5c879a6..608814d0 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFA.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFA.py @@ -2,17 +2,10 @@ # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfigSet import ( - ATNConfigSet, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ( - DecisionState, - StarLoopEntryState, -) -from cf_units._udunits2_parser._antlr4_runtime.dfa.DFAState import DFAState -from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( - IllegalStateException, -) +from ..atn.ATNConfigSet import ATNConfigSet +from ..atn.ATNState import DecisionState, StarLoopEntryState +from ..dfa.DFAState import DFAState +from ..error.Errors import IllegalStateException class DFA: @@ -130,9 +123,7 @@ def __str__(self): def toString(self, literalNames: list = None, symbolicNames: list = None): if self.s0 is None: return "" - from cf_units._udunits2_parser._antlr4_runtime.dfa.DFASerializer import ( - DFASerializer, - ) + from ..dfa.DFASerializer import DFASerializer serializer = DFASerializer(self, literalNames, symbolicNames) return str(serializer) @@ -140,9 +131,7 @@ def toString(self, literalNames: list = None, symbolicNames: list = None): def toLexerString(self): if self.s0 is None: return "" - from cf_units._udunits2_parser._antlr4_runtime.dfa.DFASerializer import ( - LexerDFASerializer, - ) + from ..dfa.DFASerializer import LexerDFASerializer serializer = LexerDFASerializer(self) return str(serializer) diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFASerializer.py b/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFASerializer.py index c6dbc447..de4e719d 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFASerializer.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFASerializer.py @@ -7,9 +7,9 @@ # A DFA walker that knows how to dump them to serialized strings.#/ from io import StringIO -from cf_units._udunits2_parser._antlr4_runtime import DFA -from cf_units._udunits2_parser._antlr4_runtime.dfa.DFAState import DFAState -from cf_units._udunits2_parser._antlr4_runtime.Utils import str_list +from ..dfa.DFA import DFA +from ..dfa.DFAState import DFAState +from ..Utils import str_list class DFASerializer: diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFAState.py b/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFAState.py index 83587af9..a3a88db7 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFAState.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/dfa/DFAState.py @@ -7,12 +7,8 @@ # Map a predicate to a predicted alternative.#/ from io import StringIO -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfigSet import ( - ATNConfigSet, -) -from cf_units._udunits2_parser._antlr4_runtime.atn.SemanticContext import ( - SemanticContext, -) +from ..atn.ATNConfigSet import ATNConfigSet +from ..atn.SemanticContext import SemanticContext class PredPrediction: diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/error/DiagnosticErrorListener.py b/cf_units/_udunits2_parser/_antlr4_runtime/error/DiagnosticErrorListener.py index 79637385..5af9745d 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/error/DiagnosticErrorListener.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/error/DiagnosticErrorListener.py @@ -26,13 +26,10 @@ from io import StringIO -from cf_units._udunits2_parser._antlr4_runtime import DFA, Parser -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNConfigSet import ( - ATNConfigSet, -) -from cf_units._udunits2_parser._antlr4_runtime.error.ErrorListener import ( - ErrorListener, -) +from ..atn.ATNConfigSet import ATNConfigSet +from ..dfa.DFA import DFA +from ..error.ErrorListener import ErrorListener +from ..Parser import Parser class DiagnosticErrorListener(ErrorListener): diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/error/ErrorStrategy.py b/cf_units/_udunits2_parser/_antlr4_runtime/error/ErrorStrategy.py index d9b22735..642239c2 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/error/ErrorStrategy.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/error/ErrorStrategy.py @@ -3,16 +3,16 @@ # Use of this file is governed by the BSD 3-clause license that # can be found in the LICENSE.txt file in the project root. # -from cf_units._udunits2_parser._antlr4_runtime.atn.ATNState import ATNState -from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( +from ..atn.ATNState import ATNState +from ..error.Errors import ( FailedPredicateException, InputMismatchException, NoViableAltException, ParseCancellationException, RecognitionException, ) -from cf_units._udunits2_parser._antlr4_runtime.IntervalSet import IntervalSet -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from ..IntervalSet import IntervalSet +from ..Token import Token # need forward declaration Parser = None diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/error/Errors.py b/cf_units/_udunits2_parser/_antlr4_runtime/error/Errors.py index ee571b52..7a1dd586 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/error/Errors.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/error/Errors.py @@ -35,11 +35,9 @@ def __init__(self, msg: str): # in the input, where it is in the ATN, the rule invocation stack, # and what kind of problem occurred. -from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream -from cf_units._udunits2_parser._antlr4_runtime.ParserRuleContext import ( - ParserRuleContext, -) -from cf_units._udunits2_parser._antlr4_runtime.Recognizer import Recognizer +from ..InputStream import InputStream +from ..ParserRuleContext import ParserRuleContext +from ..Recognizer import Recognizer class RecognitionException(Exception): @@ -100,7 +98,6 @@ def __init__( super().__init__(message=None, recognizer=lexer, input=input, ctx=None) self.startIndex = startIndex self.deadEndConfigs = deadEndConfigs - self.message = "" def __str__(self): symbol = "" @@ -175,9 +172,7 @@ def __init__( ) s = recognizer._interp.atn.states[recognizer.state] trans = s.transitions[0] - from cf_units._udunits2_parser._antlr4_runtime.atn.Transition import ( - PredicateTransition, - ) + from ..atn.Transition import PredicateTransition if isinstance(trans, PredicateTransition): self.ruleIndex = trans.ruleIndex diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreeMatch.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreeMatch.py index 92d9a80d..1d029064 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreeMatch.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreeMatch.py @@ -10,10 +10,8 @@ # from io import StringIO -from cf_units._udunits2_parser._antlr4_runtime.tree.ParseTreePattern import ( - ParseTreePattern, -) -from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ParseTree +from ..tree.ParseTreePattern import ParseTreePattern +from ..tree.Tree import ParseTree class ParseTreeMatch: diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreePattern.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreePattern.py index 00770c94..58cdeafe 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreePattern.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreePattern.py @@ -8,10 +8,8 @@ # A pattern like {@code = ;} converted to a {@link ParseTree} by # {@link ParseTreePatternMatcher#compile(String, int)}. # -from cf_units._udunits2_parser._antlr4_runtime.tree.ParseTreePatternMatcher import ( - ParseTreePatternMatcher, -) -from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ParseTree +from ..tree.ParseTreePatternMatcher import ParseTreePatternMatcher +from ..tree.Tree import ParseTree class ParseTreePattern: diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreePatternMatcher.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreePatternMatcher.py index 18d554d8..88978707 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreePatternMatcher.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/tree/ParseTreePatternMatcher.py @@ -61,40 +61,18 @@ # {@link #setDelimiters}. You must escape both start and stop strings # {@code \<} and {@code \>}.

      # -from cf_units._udunits2_parser._antlr4_runtime.CommonTokenStream import ( - CommonTokenStream, -) -from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( - ParseCancellationException, - RecognitionException, -) -from cf_units._udunits2_parser._antlr4_runtime.error.ErrorStrategy import ( - BailErrorStrategy, -) -from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream -from cf_units._udunits2_parser._antlr4_runtime.Lexer import Lexer -from cf_units._udunits2_parser._antlr4_runtime.ListTokenSource import ( - ListTokenSource, -) -from cf_units._udunits2_parser._antlr4_runtime.ParserRuleContext import ( - ParserRuleContext, -) -from cf_units._udunits2_parser._antlr4_runtime.Token import Token -from cf_units._udunits2_parser._antlr4_runtime.tree.Chunk import ( - TagChunk, - TextChunk, -) -from cf_units._udunits2_parser._antlr4_runtime.tree.RuleTagToken import ( - RuleTagToken, -) -from cf_units._udunits2_parser._antlr4_runtime.tree.TokenTagToken import ( - TokenTagToken, -) -from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ( - ParseTree, - RuleNode, - TerminalNode, -) +from ..CommonTokenStream import CommonTokenStream +from ..error.Errors import ParseCancellationException, RecognitionException +from ..error.ErrorStrategy import BailErrorStrategy +from ..InputStream import InputStream +from ..Lexer import Lexer +from ..ListTokenSource import ListTokenSource +from ..ParserRuleContext import ParserRuleContext +from ..Token import Token +from ..tree.Chunk import TagChunk, TextChunk +from ..tree.RuleTagToken import RuleTagToken +from ..tree.TokenTagToken import TokenTagToken +from ..tree.Tree import ParseTree, RuleNode, TerminalNode # need forward declaration Parser = None @@ -177,9 +155,7 @@ def matchRuleIndex( def matchPattern(self, tree: ParseTree, pattern: ParseTreePattern): labels = dict() mismatchedNode = self.matchImpl(tree, pattern.patternTree, labels) - from cf_units._udunits2_parser._antlr4_runtime.tree.ParseTreeMatch import ( - ParseTreeMatch, - ) + from ..tree.ParseTreeMatch import ParseTreeMatch return ParseTreeMatch(tree, pattern, labels, mismatchedNode) @@ -191,9 +167,7 @@ def compileTreePattern(self, pattern: str, patternRuleIndex: int): tokenList = self.tokenize(pattern) tokenSrc = ListTokenSource(tokenList) tokens = CommonTokenStream(tokenSrc) - from cf_units._udunits2_parser._antlr4_runtime.ParserInterpreter import ( - ParserInterpreter, - ) + from ..ParserInterpreter import ParserInterpreter parserInterp = ParserInterpreter( self.parser.grammarFileName, @@ -217,9 +191,7 @@ def compileTreePattern(self, pattern: str, patternRuleIndex: int): if tokens.LA(1) != Token.EOF: raise StartRuleDoesNotConsumeFullPattern() - from cf_units._udunits2_parser._antlr4_runtime.tree.ParseTreePattern import ( - ParseTreePattern, - ) + from ..tree.ParseTreePattern import ParseTreePattern return ParseTreePattern(self, pattern, patternRuleIndex, tree) diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/tree/RuleTagToken.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/RuleTagToken.py index 3bb398c1..36ac1990 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/tree/RuleTagToken.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/tree/RuleTagToken.py @@ -9,7 +9,7 @@ # rule; e.g., {@code }. These tokens are created for {@link TagChunk} # chunks where the tag corresponds to a parser rule. # -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from ..Token import Token class RuleTagToken(Token): diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/tree/TokenTagToken.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/TokenTagToken.py index 7236f177..4a8066a1 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/tree/TokenTagToken.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/tree/TokenTagToken.py @@ -9,7 +9,7 @@ # {@code }. These tokens are created for {@link TagChunk} chunks where the # tag corresponds to a lexer rule or token type. # -from cf_units._udunits2_parser._antlr4_runtime.Token import CommonToken +from ..Token import CommonToken class TokenTagToken(CommonToken): diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/tree/Tree.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/Tree.py index 77ea746e..3901e7bb 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/tree/Tree.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/tree/Tree.py @@ -7,7 +7,7 @@ # The basic notion of a tree has a parent, a payload, and a list of children. # It is the most abstract interface for all the trees used by ANTLR. # / -from cf_units._udunits2_parser._antlr4_runtime.Token import Token +from ..Token import Token INVALID_INTERVAL = (-1, -2) diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/tree/Trees.py b/cf_units/_udunits2_parser/_antlr4_runtime/tree/Trees.py index 0005b441..7e99651c 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/tree/Trees.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/tree/Trees.py @@ -8,15 +8,9 @@ # A set of utility routines useful for all kinds of ANTLR trees.# from io import StringIO -from cf_units._udunits2_parser._antlr4_runtime.Token import Token -from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ( - ErrorNode, - ParseTree, - RuleNode, - TerminalNode, - Tree, -) -from cf_units._udunits2_parser._antlr4_runtime.Utils import escapeWhitespace +from ..Token import Token +from ..tree.Tree import ErrorNode, ParseTree, RuleNode, TerminalNode, Tree +from ..Utils import escapeWhitespace # need forward declaration Parser = None @@ -109,9 +103,7 @@ def findAllNodes(cls, t: ParseTree, index: int, findTokens: bool): def _findAllNodes( cls, t: ParseTree, index: int, findTokens: bool, nodes: list ): - from cf_units._udunits2_parser._antlr4_runtime.ParserRuleContext import ( - ParserRuleContext, - ) + from ..ParserRuleContext import ParserRuleContext # check this node (the root) first if findTokens and isinstance(t, TerminalNode): diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/xpath/XPath.py b/cf_units/_udunits2_parser/_antlr4_runtime/xpath/XPath.py index 86aa8baf..dc120ecc 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/xpath/XPath.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/xpath/XPath.py @@ -47,25 +47,16 @@ #

      # Whitespace is not allowed.

      # -from cf_units._udunits2_parser._antlr4_runtime import ( - CommonTokenStream, - ParserRuleContext, - TerminalNode, -) -from cf_units._udunits2_parser._antlr4_runtime.error.ErrorListener import ( - ErrorListener, -) -from cf_units._udunits2_parser._antlr4_runtime.error.Errors import ( - LexerNoViableAltException, -) -from cf_units._udunits2_parser._antlr4_runtime.InputStream import InputStream -from cf_units._udunits2_parser._antlr4_runtime.Parser import Parser -from cf_units._udunits2_parser._antlr4_runtime.Token import Token -from cf_units._udunits2_parser._antlr4_runtime.tree.Tree import ParseTree -from cf_units._udunits2_parser._antlr4_runtime.tree.Trees import Trees -from cf_units._udunits2_parser._antlr4_runtime.xpath.XPathLexer import ( - XPathLexer, -) +from ..CommonTokenStream import CommonTokenStream +from ..error.ErrorListener import ErrorListener +from ..error.Errors import LexerNoViableAltException +from ..InputStream import InputStream +from ..Parser import Parser +from ..ParserRuleContext import ParserRuleContext +from ..Token import Token +from ..tree.Tree import ParseTree, TerminalNode +from ..tree.Trees import Trees +from ..xpath.XPathLexer import XPathLexer class XPath: diff --git a/cf_units/_udunits2_parser/_antlr4_runtime/xpath/XPathLexer.py b/cf_units/_udunits2_parser/_antlr4_runtime/xpath/XPathLexer.py index ca5ae82e..8a972ec7 100644 --- a/cf_units/_udunits2_parser/_antlr4_runtime/xpath/XPathLexer.py +++ b/cf_units/_udunits2_parser/_antlr4_runtime/xpath/XPathLexer.py @@ -1,7 +1,12 @@ -# Generated from XPathLexer.g4 by ANTLR 4.13.1 +# Generated from XPathLexer.g4 by ANTLR 4.9.3 import sys -from cf_units._udunits2_parser._antlr4_runtime import * +from ..atn.ATNDeserializer import ATNDeserializer +from ..atn.LexerATNSimulator import LexerATNSimulator +from ..dfa.DFA import DFA +from ..Lexer import Lexer +from ..PredictionContext import PredictionContextCache +from ..RuleContext import RuleContext if sys.version_info[1] > 5: from typing import TextIO @@ -543,7 +548,7 @@ class XPathLexer(Lexer): def __init__(self, input=None, output: TextIO = sys.stdout): super().__init__(input, output) - self.checkVersion("4.13.1") + self.checkVersion("4.9.3") self._interp = LexerATNSimulator( self, self.atn, self.decisionsToDFA, PredictionContextCache() ) diff --git a/cf_units/_udunits2_parser/compile.py b/cf_units/_udunits2_parser/compile.py index be264ac5..304fb033 100644 --- a/cf_units/_udunits2_parser/compile.py +++ b/cf_units/_udunits2_parser/compile.py @@ -14,6 +14,7 @@ You're welcome ;). """ +# ruff: noqa: E501 import collections import re @@ -68,6 +69,55 @@ def expand_lexer(source, target): fh.write(new_content) +def fixup_antlr_imports(antlr_file_path: Path, contents: str) -> str: + depth = len(antlr_file_path.parents) - 1 + + # Straighten out some wonky imports. + if antlr_file_path.name == "XPathLexer.py": + contents = contents.replace( + "from antlr4 import *", + "\n".join( + [ + "from antlr4.Lexer import Lexer", + "from antlr4.atn.ATNDeserializer import ATNDeserializer", + "from antlr4.dfa.DFA import DFA", + "from antlr4.RuleContext import RuleContext", + "from antlr4.CommonTokenStream import CommonTokenStream", + "from antlr4.ParserRuleContext import ParserRuleContext", + "from antlr4.tree.Tree import TerminalNode", + "from antlr4.atn.LexerATNSimulator import LexerATNSimulator", + "from antlr4.PredictionContext import PredictionContextCache", + ] + ), + ) + if antlr_file_path.name == "XPath.py": + contents = contents.replace( + "from antlr4 import CommonTokenStream, DFA, " + "PredictionContextCache, " + "Lexer, LexerATNSimulator, ParserRuleContext, TerminalNode", + "\n".join( + [ + "from antlr4.Lexer import Lexer", + "from antlr4.CommonTokenStream import CommonTokenStream", + "from antlr4.ParserRuleContext import ParserRuleContext", + "from antlr4.tree.Tree import TerminalNode", + "from antlr4.atn.LexerATNSimulator import LexerATNSimulator", + "from antlr4.PredictionContext import PredictionContextCache", + "from antlr4.dfa.DFA import DFA", + ] + ), + ) + contents = contents.replace( + "from antlr4 import DFA", "from antlr4.dfa.DFA import DFA" + ) + contents = contents.replace( + "from antlr4 import Parser, DFA", + "from antlr4.dfa.DFA import DFA\nfrom antlr4.Parser import Parser", + ) + contents = contents.replace("from antlr4", "from " + "." * depth) + return contents + + def vendor_antlr4_runtime(udunits2_parser_dir: Path): antlr_dest = udunits2_parser_dir / "_antlr4_runtime" version_file = antlr_dest / "_antlr4_version.txt" @@ -90,15 +140,20 @@ def vendor_antlr4_runtime(udunits2_parser_dir: Path): "install", "--quiet", f"--prefix={tmp_dest}", - "antlr4-python3-runtime", + f"antlr4-python3-runtime=={ANTLR_VERSION}", ], check=True, ) [antlr_code_dir] = tmp_dest.glob("lib/python3.*/site-packages/antlr4") for py_file in antlr_code_dir.glob("**/*.py"): - py_file_dest = antlr_dest / py_file.relative_to(antlr_code_dir) + if py_file.name == "_pygrun.py": + continue + rel_to_antrl_root = py_file.relative_to(antlr_code_dir) + contents = py_file.read_text() + contents = fixup_antlr_imports(rel_to_antrl_root, contents) + py_file_dest = antlr_dest / rel_to_antrl_root py_file_dest.parent.mkdir(exist_ok=True) - py_file_dest.write_text(py_file.read_text()) + py_file_dest.write_text(contents) shutil.rmtree(tmp_dest) version_file.write_text(ANTLR_VERSION) else: @@ -106,7 +161,7 @@ def vendor_antlr4_runtime(udunits2_parser_dir: Path): # Re-write all imports relating to the antlr4 runtime to be the # vendored location. - for py_file in Path(".").glob("**/*.py"): + for py_file in Path("parser").glob("**/*.py"): if py_file.absolute() == Path(__file__).absolute(): # Don't adapt for vendoring of this file. continue diff --git a/cf_units/tests/test_coding_standards.py b/cf_units/tests/test_coding_standards.py index e0caa0fb..f3bbe519 100644 --- a/cf_units/tests/test_coding_standards.py +++ b/cf_units/tests/test_coding_standards.py @@ -90,6 +90,7 @@ def test_license_headers(self): "dist/*", "cf_units/_version.py", "cf_units/_udunits2_parser/parser/*", + "cf_units/_udunits2_parser/_antlr4_runtime/*", ) last_change_by_fname = self.last_change_by_fname() diff --git a/cf_units/tests/test_tex.py b/cf_units/tests/test_tex.py index ffb91b6e..ce3977ad 100644 --- a/cf_units/tests/test_tex.py +++ b/cf_units/tests/test_tex.py @@ -2,10 +2,6 @@ # # This file is part of cf-units and is released under the BSD license. # See LICENSE in the root of the repository for full licensing details. -# ruff: noqa: E402 -import pytest - -antlr4 = pytest.importorskip("antlr4") from cf_units.tex import tex diff --git a/cf_units/tex.py b/cf_units/tex.py index d0898664..4d716d2e 100644 --- a/cf_units/tex.py +++ b/cf_units/tex.py @@ -3,8 +3,8 @@ # This file is part of cf-units and is released under the BSD license. # See LICENSE in the root of the repository for full licensing details. -import cf_units._udunits2_parser.graph as graph # noqa: E402 -from cf_units._udunits2_parser import parse as _parse # noqa: E402 +from ._udunits2_parser import graph +from ._udunits2_parser import parse as _parse class TeXVisitor(graph.Visitor): From 0a0fc02edfc1224455c56f7011f4d48bb84e06d7 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Fri, 4 Oct 2024 05:47:40 +0200 Subject: [PATCH 7/9] Update the readme to explain more about ANTLR4 and the vendoring process --- cf_units/_udunits2_parser/README.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/cf_units/_udunits2_parser/README.md b/cf_units/_udunits2_parser/README.md index a746f3ed..fd6adf47 100644 --- a/cf_units/_udunits2_parser/README.md +++ b/cf_units/_udunits2_parser/README.md @@ -13,9 +13,10 @@ a number of convenient lexical elements. Once the Jinja2 template has been expanded, the [ANTLR Java library](https://github.com/antlr/antlr4) is used to -compile the grammar into the targetted runtime language. +compile the grammar into the targeted runtime language. [A script](compile.py) is provided to automate this as much as possible. +It has a dependency on pip, Jinja2, Java and ruff. The compiled parser is committed to the repository for ease of deployment and testing (we know it isn't ideal, but it really does make things easier). @@ -24,9 +25,20 @@ changes to the grammar being proposed so that the two can remain in synch. ### Updating the ANTLR version -The above script downloads a Java Jar which needs updating to the same version -as antlr4-python3-runtime specified in the python requirements. Once these have -been updated, run [the script](compile.py) to regenerate the parser. +The [compile.py script](compile.py) copies the ANTLR4 runtime into the _antlr4_runtime +directory, and this should be commited to the repository. This means that we do not +have a runtime dependency on ANTLR4 (which was found to be challenging due to the +fact that you need to pin to a specific version of the ANTLR4 runtime, and aligning +this version with other libraries which also have an ANTLR4 dependency is impractical). + +Given that the ANTRL4 runtime is vendored into this project, we have little need +to upgrade the version unless new features of the parser/lexer generators are needed. +Upgrading the version is a simple matter of changing the version in the compile.py +script, and then re-running it. This should re-generate the parser/lexer, and update +the content in the _antlr4_runtime directory. One complexity may be that the imports +of the ANTRL4 runtime need to be rewritten to support vendoring, and the code needed +to do so may change from version to version. This topic is being followed upstream +with the ANTRL4 project with the hope of making this easier and/or built-in to ANTLR4. ### Testing the grammar From 80921ad70421f11e5db691ffb9676315e1899733 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Wed, 30 Oct 2024 19:23:07 +0100 Subject: [PATCH 8/9] Update cf_units/_udunits2_parser/README.md Co-authored-by: Patrick Peglar --- cf_units/_udunits2_parser/README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cf_units/_udunits2_parser/README.md b/cf_units/_udunits2_parser/README.md index fd6adf47..655fcedd 100644 --- a/cf_units/_udunits2_parser/README.md +++ b/cf_units/_udunits2_parser/README.md @@ -31,8 +31,11 @@ have a runtime dependency on ANTLR4 (which was found to be challenging due to th fact that you need to pin to a specific version of the ANTLR4 runtime, and aligning this version with other libraries which also have an ANTLR4 dependency is impractical). -Given that the ANTRL4 runtime is vendored into this project, we have little need -to upgrade the version unless new features of the parser/lexer generators are needed. +Since the generated code is committed to this repo, and the ANTRL4 runtime is also vendored into it, we won't ever need to run ANTLR4 unless the grammar changes. + +So, we will only change the ANTLR4 version if we need new features of the +parser/lexer generators, or it becomes difficult to support the older version. + Upgrading the version is a simple matter of changing the version in the compile.py script, and then re-running it. This should re-generate the parser/lexer, and update the content in the _antlr4_runtime directory. One complexity may be that the imports From 9e542b789a86b03c4d26ed9a45880cb4b22eeede Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Wed, 30 Oct 2024 19:23:27 +0100 Subject: [PATCH 9/9] Update cf_units/_udunits2_parser/README.md Co-authored-by: Patrick Peglar --- cf_units/_udunits2_parser/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cf_units/_udunits2_parser/README.md b/cf_units/_udunits2_parser/README.md index 655fcedd..dae6f6c8 100644 --- a/cf_units/_udunits2_parser/README.md +++ b/cf_units/_udunits2_parser/README.md @@ -36,7 +36,7 @@ Since the generated code is committed to this repo, and the ANTRL4 runtime is al So, we will only change the ANTLR4 version if we need new features of the parser/lexer generators, or it becomes difficult to support the older version. -Upgrading the version is a simple matter of changing the version in the compile.py +Upgrading the ANTLR4 version is a simple matter of changing `ANTLR_VERSION` in the compile.py script, and then re-running it. This should re-generate the parser/lexer, and update the content in the _antlr4_runtime directory. One complexity may be that the imports of the ANTRL4 runtime need to be rewritten to support vendoring, and the code needed