madduck's git repository
Every one of the projects in this repository is available at the canonical
URL git://git.madduck.net/madduck/pub/<projectpath> — see
each project's metadata for the exact URL.
All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
SSH access, as well as push access can be individually
arranged.
If you use my repositories frequently, consider adding the following
snippet to ~/.gitconfig and using the third clone URL listed for each
project:
[url "git://git.madduck.net/madduck/"]
insteadOf = madduck:
from typing import (
Any,
Dict,
from typing import (
Any,
Dict,
Iterator,
List,
Optional,
Iterator,
List,
Optional,
from blib2to3.pgen2.grammar import Grammar
__author__ = "Guido van Rossum <guido@python.org>"
from blib2to3.pgen2.grammar import Grammar
__author__ = "Guido van Rossum <guido@python.org>"
HUGE: int = 0x7FFFFFFF # maximum repeat count, default max
HUGE: int = 0x7FFFFFFF # maximum repeat count, default max
-_type_reprs: Dict[int, Union[Text, int]] = {}
+_type_reprs: Dict[int, Union[str, int]] = {}
-def type_repr(type_num: int) -> Union[Text, int]:
+def type_repr(type_num: int) -> Union[str, int]:
global _type_reprs
if not _type_reprs:
from .pygram import python_symbols
global _type_reprs
if not _type_reprs:
from .pygram import python_symbols
_P = TypeVar("_P", bound="Base")
NL = Union["Node", "Leaf"]
_P = TypeVar("_P", bound="Base")
NL = Union["Node", "Leaf"]
-Context = Tuple[Text, Tuple[int, int]]
-RawNode = Tuple[int, Optional[Text], Optional[Context], Optional[List[NL]]]
-
+Context = Tuple[str, Tuple[int, int]]
+RawNode = Tuple[int, Optional[str], Optional[Context], Optional[List[NL]]]
"""
Abstract base class for Node and Leaf.
"""
Abstract base class for Node and Leaf.
return self._eq(other)
@property
return self._eq(other)
@property
- def prefix(self) -> Text:
+ def prefix(self) -> str:
raise NotImplementedError
def _eq(self: _P, other: _P) -> bool:
raise NotImplementedError
def _eq(self: _P, other: _P) -> bool:
return 0
return 1 + self.parent.depth()
return 0
return 1 + self.parent.depth()
- def get_suffix(self) -> Text:
+ def get_suffix(self) -> str:
"""
Return the string immediately following the invocant node. This is
effectively equivalent to node.next_sibling.prefix
"""
Return the string immediately following the invocant node. This is
effectively equivalent to node.next_sibling.prefix
"""Concrete implementation for interior nodes."""
fixers_applied: Optional[List[Any]]
"""Concrete implementation for interior nodes."""
fixers_applied: Optional[List[Any]]
- used_names: Optional[Set[Text]]
+ used_names: Optional[Set[str]]
def __init__(
self,
type: int,
children: List[NL],
context: Optional[Any] = None,
def __init__(
self,
type: int,
children: List[NL],
context: Optional[Any] = None,
- prefix: Optional[Text] = None,
+ prefix: Optional[str] = None,
fixers_applied: Optional[List[Any]] = None,
) -> None:
"""
fixers_applied: Optional[List[Any]] = None,
) -> None:
"""
else:
self.fixers_applied = None
else:
self.fixers_applied = None
- def __repr__(self) -> Text:
+ def __repr__(self) -> str:
"""Return a canonical string representation."""
assert self.type is not None
"""Return a canonical string representation."""
assert self.type is not None
- return "%s(%s, %r)" % (
+ return "{}({}, {!r})".format(
self.__class__.__name__,
type_repr(self.type),
self.children,
)
self.__class__.__name__,
type_repr(self.type),
self.children,
)
- def __str__(self) -> Text:
+ def __str__(self) -> str:
"""
Return a pretty string representation.
"""
Return a pretty string representation.
yield from child.pre_order()
@property
yield from child.pre_order()
@property
- def prefix(self) -> Text:
+ def prefix(self) -> str:
"""
The whitespace and comments preceding this node in the input.
"""
"""
The whitespace and comments preceding this node in the input.
"""
return self.children[0].prefix
@prefix.setter
return self.children[0].prefix
@prefix.setter
- def prefix(self, prefix: Text) -> None:
+ def prefix(self, prefix: str) -> None:
if self.children:
self.children[0].prefix = prefix
if self.children:
self.children[0].prefix = prefix
"""Concrete implementation for leaf nodes."""
# Default values for instance variables
"""Concrete implementation for leaf nodes."""
# Default values for instance variables
fixers_applied: List[Any]
bracket_depth: int
# Changed later in brackets.py
opening_bracket: Optional["Leaf"] = None
fixers_applied: List[Any]
bracket_depth: int
# Changed later in brackets.py
opening_bracket: Optional["Leaf"] = None
- used_names: Optional[Set[Text]]
+ used_names: Optional[Set[str]]
_prefix = "" # Whitespace and comments preceding this token in the input
lineno: int = 0 # Line where this token starts in the input
column: int = 0 # Column where this token starts in the input
_prefix = "" # Whitespace and comments preceding this token in the input
lineno: int = 0 # Line where this token starts in the input
column: int = 0 # Column where this token starts in the input
def __init__(
self,
type: int,
def __init__(
self,
type: int,
context: Optional[Context] = None,
context: Optional[Context] = None,
- prefix: Optional[Text] = None,
+ prefix: Optional[str] = None,
fixers_applied: List[Any] = [],
opening_bracket: Optional["Leaf"] = None,
fmt_pass_converted_first_leaf: Optional["Leaf"] = None,
fixers_applied: List[Any] = [],
opening_bracket: Optional["Leaf"] = None,
fmt_pass_converted_first_leaf: Optional["Leaf"] = None,
from .pgen2.token import tok_name
assert self.type is not None
from .pgen2.token import tok_name
assert self.type is not None
- return "%s(%s, %r)" % (
+ return "{}({}, {!r})".format(
self.__class__.__name__,
tok_name.get(self.type, self.type),
self.value,
)
self.__class__.__name__,
tok_name.get(self.type, self.type),
self.value,
)
- def __str__(self) -> Text:
+ def __str__(self) -> str:
"""
Return a pretty string representation.
"""
Return a pretty string representation.
- def prefix(self) -> Text:
+ def prefix(self) -> str:
"""
The whitespace and comments preceding this token in the input.
"""
return self._prefix
@prefix.setter
"""
The whitespace and comments preceding this token in the input.
"""
return self._prefix
@prefix.setter
- def prefix(self, prefix: Text) -> None:
+ def prefix(self, prefix: str) -> None:
self.changed()
self._prefix = prefix
self.changed()
self._prefix = prefix
return Leaf(type, value or "", context=context)
return Leaf(type, value or "", context=context)
-_Results = Dict[Text, NL]
-
+_Results = Dict[str, NL]
-class BasePattern(object):
"""
A pattern is a tree matching pattern.
"""
A pattern is a tree matching pattern.
type: Optional[int]
type = None # Node type (token if < 256, symbol if >= 256)
content: Any = None # Optional content matching pattern
type: Optional[int]
type = None # Node type (token if < 256, symbol if >= 256)
content: Any = None # Optional content matching pattern
- name: Optional[Text] = None # Optional name used to store match in results dict
+ name: Optional[str] = None # Optional name used to store match in results dict
def __new__(cls, *args, **kwds):
"""Constructor that prevents BasePattern from being instantiated."""
assert cls is not BasePattern, "Cannot instantiate BasePattern"
return object.__new__(cls)
def __new__(cls, *args, **kwds):
"""Constructor that prevents BasePattern from being instantiated."""
assert cls is not BasePattern, "Cannot instantiate BasePattern"
return object.__new__(cls)
- def __repr__(self) -> Text:
+ def __repr__(self) -> str:
assert self.type is not None
args = [type_repr(self.type), self.content, self.name]
while args and args[-1] is None:
del args[-1]
assert self.type is not None
args = [type_repr(self.type), self.content, self.name]
while args and args[-1] is None:
del args[-1]
- return "%s(%s)" % (self.__class__.__name__, ", ".join(map(repr, args)))
+ return "{}({})".format(self.__class__.__name__, ", ".join(map(repr, args)))
def _submatch(self, node, results=None) -> bool:
raise NotImplementedError
def _submatch(self, node, results=None) -> bool:
raise NotImplementedError
def __init__(
self,
type: Optional[int] = None,
def __init__(
self,
type: Optional[int] = None,
- content: Optional[Text] = None,
- name: Optional[Text] = None,
+ content: Optional[str] = None,
+ name: Optional[str] = None,
) -> None:
"""
Initializer. Takes optional type, content, and name.
) -> None:
"""
Initializer. Takes optional type, content, and name.
class NodePattern(BasePattern):
class NodePattern(BasePattern):
wildcards: bool = False
def __init__(
self,
type: Optional[int] = None,
wildcards: bool = False
def __init__(
self,
type: Optional[int] = None,
- content: Optional[Iterable[Text]] = None,
- name: Optional[Text] = None,
+ content: Optional[Iterable[str]] = None,
+ name: Optional[str] = None,
) -> None:
"""
Initializer. Takes optional type, content, and name.
) -> None:
"""
Initializer. Takes optional type, content, and name.
class WildcardPattern(BasePattern):
class WildcardPattern(BasePattern):
"""
A wildcard pattern can match zero or more nodes.
"""
A wildcard pattern can match zero or more nodes.
- content: Optional[Text] = None,
+ content: Optional[str] = None,
min: int = 0,
max: int = HUGE,
min: int = 0,
max: int = HUGE,
- name: Optional[Text] = None,
+ name: Optional[str] = None,
) -> None:
"""
Initializer.
) -> None:
"""
Initializer.