(self)
| 2097 | _latin_alphabets = set(string.ascii_letters) |
| 2098 | |
| 2099 | def __init__(self) -> None: |
| 2100 | p = types.SimpleNamespace() |
| 2101 | |
| 2102 | def set_names_and_parse_actions() -> None: |
| 2103 | for key, val in vars(p).items(): |
| 2104 | if not key.startswith('_'): |
| 2105 | # Set names on (almost) everything -- very useful for debugging |
| 2106 | # token, placeable, and auto_delim are forward references which |
| 2107 | # are left without names to ensure useful error messages |
| 2108 | if key not in ("token", "placeable", "auto_delim"): |
| 2109 | val.set_name(key) |
| 2110 | # Set actions |
| 2111 | if hasattr(self, key): |
| 2112 | val.set_parse_action(getattr(self, key)) |
| 2113 | |
| 2114 | # Root definitions. |
| 2115 | |
| 2116 | # In TeX parlance, a csname is a control sequence name (a "\foo"). |
| 2117 | def csnames(group: str, names: Iterable[str]) -> Regex: |
| 2118 | ends_with_alpha = [] |
| 2119 | ends_with_nonalpha = [] |
| 2120 | for name in names: |
| 2121 | if name[-1].isalpha(): |
| 2122 | ends_with_alpha.append(name) |
| 2123 | else: |
| 2124 | ends_with_nonalpha.append(name) |
| 2125 | return Regex( |
| 2126 | r"\\(?P<{group}>(?:{alpha})(?![A-Za-z]){additional}{nonalpha})".format( |
| 2127 | group=group, |
| 2128 | alpha="|".join(map(re.escape, ends_with_alpha)), |
| 2129 | additional="|" if ends_with_nonalpha else "", |
| 2130 | nonalpha="|".join(map(re.escape, ends_with_nonalpha)), |
| 2131 | ) |
| 2132 | ) |
| 2133 | |
| 2134 | p.float_literal = Regex(r"[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)") |
| 2135 | p.space = one_of(self._space_widths)("space") |
| 2136 | |
| 2137 | p.style_literal = one_of( |
| 2138 | [str(e.value) for e in self._MathStyle])("style_literal") |
| 2139 | |
| 2140 | p.symbol = Regex( |
| 2141 | r"[a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|\U00000080-\U0001ffff]" |
| 2142 | r"|\\[%${}\[\]_|]" |
| 2143 | + r"|\\(?:{})(?![A-Za-z])".format( |
| 2144 | "|".join(map(re.escape, tex2uni))) |
| 2145 | )("sym").leave_whitespace() |
| 2146 | p.unknown_symbol = Regex(r"\\[A-Za-z]+")("name") |
| 2147 | |
| 2148 | p.font = csnames("font", self._fontnames) |
| 2149 | p.start_group = Optional(r"\math" + one_of(self._fontnames)("font")) + "{" |
| 2150 | p.end_group = Literal("}") |
| 2151 | |
| 2152 | p.delim = one_of(self._delims) |
| 2153 | |
| 2154 | # Mutually recursive definitions. (Minimizing the number of Forward |
| 2155 | # elements is important for speed.) |
| 2156 | p.auto_delim = Forward() |
nothing calls this directly
no test coverage detected