summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pyparsing/core.py24
-rw-r--r--pyparsing/helpers.py15
-rw-r--r--pyparsing/results.py5
-rw-r--r--pyparsing/unicode.py37
-rw-r--r--pyparsing/util.py6
5 files changed, 50 insertions, 37 deletions
diff --git a/pyparsing/core.py b/pyparsing/core.py
index b61a8f8..1bb13b8 100644
--- a/pyparsing/core.py
+++ b/pyparsing/core.py
@@ -407,7 +407,7 @@ class ParserElement(ABC):
def __init__(self, savelist: bool = False):
self.parseAction: List[ParseAction] = list()
- self.failAction = None
+ self.failAction: OptionalType[ParseFailAction] = None
self.customName = None
self._defaultName = None
self.resultsName = None
@@ -428,7 +428,9 @@ class ParserElement(ABC):
self.modalResults = True
# custom debug actions
self.debugActions: Tuple[
- DebugStartAction, DebugSuccessAction, DebugExceptionAction
+ OptionalType[DebugStartAction],
+ OptionalType[DebugSuccessAction],
+ OptionalType[DebugExceptionAction],
] = (None, None, None)
self.re = None
# avoid redundant calls to preParse
@@ -461,7 +463,7 @@ class ParserElement(ABC):
cpy.parseAction = self.parseAction[:]
cpy.ignoreExprs = self.ignoreExprs[:]
if self.copyDefaultWhiteChars:
- cpy.whiteChars = ParserElement.DEFAULT_WHITE_CHARS
+ cpy.whiteChars = set(ParserElement.DEFAULT_WHITE_CHARS)
return cpy
def set_results_name(
@@ -855,7 +857,7 @@ class ParserElement(ABC):
_parse = _parseNoCache
@staticmethod
- def reset_cache() -> NoReturn:
+ def reset_cache() -> None:
ParserElement.packrat_cache.clear()
ParserElement.packrat_cache_stats[:] = [0] * len(
ParserElement.packrat_cache_stats
@@ -866,7 +868,7 @@ class ParserElement(ABC):
_left_recursion_enabled = False
@staticmethod
- def disable_memoization() -> NoReturn:
+ def disable_memoization() -> None:
"""
Disables active Packrat or Left Recursion parsing and their memoization
@@ -882,7 +884,7 @@ class ParserElement(ABC):
@staticmethod
def enable_left_recursion(
cache_size_limit: OptionalType[int] = None, *, force=False
- ) -> NoReturn:
+ ) -> None:
"""
Enables "bounded recursion" parsing, which allows for both direct and indirect
left-recursion. During parsing, left-recursive :class:`Forward` elements are
@@ -928,7 +930,7 @@ class ParserElement(ABC):
ParserElement._left_recursion_enabled = True
@staticmethod
- def enable_packrat(cache_size_limit: int = 128, *, force: bool = False) -> NoReturn:
+ def enable_packrat(cache_size_limit: int = 128, *, force: bool = False) -> None:
"""
Enables "packrat" parsing, which adds memoizing to the parsing logic.
Repeated parse attempts at the same string location (which happens
@@ -1169,7 +1171,7 @@ class ParserElement(ABC):
def search_string(
self, instring: str, max_matches: int = _MAX_INT, *, maxMatches: int = _MAX_INT
- ) -> List[ParseResults]:
+ ) -> ParseResults:
"""
Another extension to :class:`scan_string`, simplifying the access to the tokens found
to match the given parse expression. May be called with optional
@@ -1205,7 +1207,7 @@ class ParserElement(ABC):
def split(
self,
instring: str,
- maxsplit: bool = _MAX_INT,
+ maxsplit: int = _MAX_INT,
include_separators: bool = False,
*,
includeSeparators=False,
@@ -3565,8 +3567,8 @@ class And(ParseExpression):
def _generateDefaultName(self):
return "-"
- def __init__(self, exprs: IterableType[ParserElement], savelist: bool = True):
- exprs: List["ParserElement"] = list(exprs)
+ def __init__(self, exprs_arg: IterableType[ParserElement], savelist: bool = True):
+ exprs: List["ParserElement"] = list(exprs_arg)
if exprs and Ellipsis in exprs:
tmp = []
for i, expr in enumerate(exprs):
diff --git a/pyparsing/helpers.py b/pyparsing/helpers.py
index 03cf695..6b7b909 100644
--- a/pyparsing/helpers.py
+++ b/pyparsing/helpers.py
@@ -665,7 +665,12 @@ class OpAssoc(Enum):
InfixNotationOperatorSpec = Tuple[
- Union[ParserElement, str], int, OpAssoc, OptionalType[ParseAction]
+ Union[
+ ParserElement, str, Tuple[Union[ParserElement, str], Union[ParserElement, str]]
+ ],
+ int,
+ OpAssoc,
+ OptionalType[ParseAction],
]
@@ -765,6 +770,9 @@ def infix_notation(
"if numterms=3, opExpr must be a tuple or list of two expressions"
)
opExpr1, opExpr2 = opExpr
+ term_name = "{}{} term".format(opExpr1, opExpr2)
+ else:
+ term_name = "{} term".format(opExpr)
if not 1 <= arity <= 3:
raise ValueError("operator must be unary (1), binary (2), or ternary (3)")
@@ -772,8 +780,7 @@ def infix_notation(
if rightLeftAssoc not in (OpAssoc.LEFT, OpAssoc.RIGHT):
raise ValueError("operator must indicate right or left associativity")
- termName = "%s term" % opExpr if arity < 3 else "%s%s term" % opExpr
- thisExpr = Forward().set_name(termName)
+ thisExpr = Forward().set_name(term_name)
if rightLeftAssoc is OpAssoc.LEFT:
if arity == 1:
matchExpr = _FB(lastExpr + opExpr) + Group(
@@ -816,7 +823,7 @@ def infix_notation(
matchExpr.set_parse_action(*pa)
else:
matchExpr.set_parse_action(pa)
- thisExpr <<= matchExpr.set_name(termName) | lastExpr
+ thisExpr <<= matchExpr.set_name(term_name) | lastExpr
lastExpr = thisExpr
ret <<= lastExpr
return ret
diff --git a/pyparsing/results.py b/pyparsing/results.py
index 172074d..3d52fbb 100644
--- a/pyparsing/results.py
+++ b/pyparsing/results.py
@@ -3,9 +3,10 @@
from collections.abc import MutableMapping, Mapping, MutableSequence
import pprint
from weakref import ref as wkref
+from typing import Tuple, Any
str_type = (str, bytes)
-_generator_type = type((x for x in ()))
+_generator_type = type((_ for _ in ()))
class _ParseResultsWithOffset:
@@ -70,7 +71,7 @@ class ParseResults:
- year: 1999
"""
- _null_values = (None, "", [], ())
+ _null_values: Tuple[Any, ...] = (None, "", [], ())
__slots__ = [
"_name",
diff --git a/pyparsing/unicode.py b/pyparsing/unicode.py
index f41d2a0..cbf6865 100644
--- a/pyparsing/unicode.py
+++ b/pyparsing/unicode.py
@@ -2,6 +2,7 @@
import sys
from itertools import filterfalse
+from typing import List, Tuple
class _lazyclassproperty:
@@ -39,7 +40,7 @@ class unicode_set:
pass
"""
- _ranges = []
+ _ranges: List[Tuple[int, ...]] = []
@classmethod
def _get_chars_for_ranges(cls):
@@ -95,30 +96,30 @@ class pyparsing_unicode(unicode_set):
A namespace class for defining common language unicode_sets.
"""
- _ranges = [(32, sys.maxunicode)]
+ _ranges: List[Tuple[int, ...]] = [(32, sys.maxunicode)]
class Latin1(unicode_set):
"Unicode set for Latin-1 Unicode Character Range"
- _ranges = [
+ _ranges: List[Tuple[int, ...]] = [
(0x0020, 0x007E),
(0x00A0, 0x00FF),
]
class LatinA(unicode_set):
"Unicode set for Latin-A Unicode Character Range"
- _ranges = [
+ _ranges: List[Tuple[int, ...]] = [
(0x0100, 0x017F),
]
class LatinB(unicode_set):
"Unicode set for Latin-B Unicode Character Range"
- _ranges = [
+ _ranges: List[Tuple[int, ...]] = [
(0x0180, 0x024F),
]
class Greek(unicode_set):
"Unicode set for Greek Unicode Character Ranges"
- _ranges = [
+ _ranges: List[Tuple[int, ...]] = [
(0x0342, 0x0345),
(0x0370, 0x0377),
(0x037A, 0x037F),
@@ -158,7 +159,7 @@ class pyparsing_unicode(unicode_set):
class Cyrillic(unicode_set):
"Unicode set for Cyrillic Unicode Character Range"
- _ranges = [
+ _ranges: List[Tuple[int, ...]] = [
(0x0400, 0x052F),
(0x1C80, 0x1C88),
(0x1D2B,),
@@ -171,7 +172,7 @@ class pyparsing_unicode(unicode_set):
class Chinese(unicode_set):
"Unicode set for Chinese Unicode Character Range"
- _ranges = [
+ _ranges: List[Tuple[int, ...]] = [
(0x2E80, 0x2E99),
(0x2E9B, 0x2EF3),
(0x31C0, 0x31E3),
@@ -194,18 +195,18 @@ class pyparsing_unicode(unicode_set):
class Japanese(unicode_set):
"Unicode set for Japanese Unicode Character Range, combining Kanji, Hiragana, and Katakana ranges"
- _ranges = []
+ _ranges: List[Tuple[int, ...]] = []
class Kanji(unicode_set):
"Unicode set for Kanji Unicode Character Range"
- _ranges = [
+ _ranges: List[Tuple[int, ...]] = [
(0x4E00, 0x9FBF),
(0x3000, 0x303F),
]
class Hiragana(unicode_set):
"Unicode set for Hiragana Unicode Character Range"
- _ranges = [
+ _ranges: List[Tuple[int, ...]] = [
(0x3041, 0x3096),
(0x3099, 0x30A0),
(0x30FC,),
@@ -217,7 +218,7 @@ class pyparsing_unicode(unicode_set):
class Katakana(unicode_set):
"Unicode set for Katakana Unicode Character Range"
- _ranges = [
+ _ranges: List[Tuple[int, ...]] = [
(0x3099, 0x309C),
(0x30A0, 0x30FF),
(0x31F0, 0x31FF),
@@ -231,7 +232,7 @@ class pyparsing_unicode(unicode_set):
class Hangul(unicode_set):
"Unicode set for Hangul (Korean) Unicode Character Range"
- _ranges = [
+ _ranges: List[Tuple[int, ...]] = [
(0x1100, 0x11FF),
(0x302E, 0x302F),
(0x3131, 0x318E),
@@ -251,17 +252,17 @@ class pyparsing_unicode(unicode_set):
Korean = Hangul
- class CJK(Chinese, Japanese, Korean):
+ class CJK(Chinese, Japanese, Hangul):
"Unicode set for combined Chinese, Japanese, and Korean (CJK) Unicode Character Range"
pass
class Thai(unicode_set):
"Unicode set for Thai Unicode Character Range"
- _ranges = [(0x0E01, 0x0E3A), (0x0E3F, 0x0E5B)]
+ _ranges: List[Tuple[int, ...]] = [(0x0E01, 0x0E3A), (0x0E3F, 0x0E5B)]
class Arabic(unicode_set):
"Unicode set for Arabic Unicode Character Range"
- _ranges = [
+ _ranges: List[Tuple[int, ...]] = [
(0x0600, 0x061B),
(0x061E, 0x06FF),
(0x0700, 0x077F),
@@ -269,7 +270,7 @@ class pyparsing_unicode(unicode_set):
class Hebrew(unicode_set):
"Unicode set for Hebrew Unicode Character Range"
- _ranges = [
+ _ranges: List[Tuple[int, ...]] = [
(0x0591, 0x05C7),
(0x05D0, 0x05EA),
(0x05EF, 0x05F4),
@@ -283,7 +284,7 @@ class pyparsing_unicode(unicode_set):
class Devanagari(unicode_set):
"Unicode set for Devanagari Unicode Character Range"
- _ranges = [(0x0900, 0x097F), (0xA8E0, 0xA8FF)]
+ _ranges: List[Tuple[int, ...]] = [(0x0900, 0x097F), (0xA8E0, 0xA8FF)]
pyparsing_unicode.Japanese._ranges = (
diff --git a/pyparsing/util.py b/pyparsing/util.py
index bbbfcd5..004476d 100644
--- a/pyparsing/util.py
+++ b/pyparsing/util.py
@@ -4,6 +4,8 @@ import types
import collections
import itertools
from functools import lru_cache
+from typing import List
+
_bslash = chr(92)
@@ -11,8 +13,8 @@ _bslash = chr(92)
class __config_flags:
"""Internal class for defining compatibility and debugging flags"""
- _all_names = []
- _fixed_names = []
+ _all_names: List[str] = []
+ _fixed_names: List[str] = []
_type_desc = "configuration"
@classmethod