pystachy 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. _pystachy_source/VERSION +1 -0
  2. _pystachy_source/lib/_argparse_help.py +188 -0
  3. _pystachy_source/lib/_markupbase.py +399 -0
  4. _pystachy_source/lib/_pyrandom.py +272 -0
  5. _pystachy_source/lib/_pystachy__io.py +43 -0
  6. _pystachy_source/lib/_pystachy_abc.py +105 -0
  7. _pystachy_source/lib/_pystachy_atexit.py +76 -0
  8. _pystachy_source/lib/_pystachy_bfile.py +245 -0
  9. _pystachy_source/lib/_pystachy_binascii.py +412 -0
  10. _pystachy_source/lib/_pystachy_bytescan.py +28 -0
  11. _pystachy_source/lib/_pystachy_codecs.py +524 -0
  12. _pystachy_source/lib/_pystachy_copy.py +43 -0
  13. _pystachy_source/lib/_pystachy_iterators.py +207 -0
  14. _pystachy_source/lib/_pystachy_itertools.py +996 -0
  15. _pystachy_source/lib/_pystachy_json_any.py +271 -0
  16. _pystachy_source/lib/_pystachy_json_int.py +6 -0
  17. _pystachy_source/lib/_pystachy_mappingproxy.py +94 -0
  18. _pystachy_source/lib/_pystachy_object.py +13 -0
  19. _pystachy_source/lib/_pystachy_percent.py +343 -0
  20. _pystachy_source/lib/_pystachy_range.py +139 -0
  21. _pystachy_source/lib/_pystachy_re_specialized.py +33 -0
  22. _pystachy_source/lib/_pystachy_string.py +175 -0
  23. _pystachy_source/lib/_pystachy_struct.py +447 -0
  24. _pystachy_source/lib/_pystachy_thread.py +433 -0
  25. _pystachy_source/lib/_pystachy_time.py +15 -0
  26. _pystachy_source/lib/_pystachy_types.py +13 -0
  27. _pystachy_source/lib/_pystachy_unicodedata.py +873 -0
  28. _pystachy_source/lib/_pystachy_unicodedata_db.py +1371 -0
  29. _pystachy_source/lib/_pystachy_unicodedata_names.py +7634 -0
  30. _pystachy_source/lib/_pystachy_weakref.py +111 -0
  31. _pystachy_source/lib/_weakrefset.py +205 -0
  32. _pystachy_source/lib/argparse.py +315 -0
  33. _pystachy_source/lib/bisect.py +118 -0
  34. _pystachy_source/lib/codecs.py +1135 -0
  35. _pystachy_source/lib/collections/__init__.py +1282 -0
  36. _pystachy_source/lib/colorsys.py +166 -0
  37. _pystachy_source/lib/contextlib.py +300 -0
  38. _pystachy_source/lib/copy.py +213 -0
  39. _pystachy_source/lib/csv.py +1039 -0
  40. _pystachy_source/lib/curses/__init__.py +4 -0
  41. _pystachy_source/lib/curses/ascii.py +99 -0
  42. _pystachy_source/lib/datetime.py +3044 -0
  43. _pystachy_source/lib/difflib.py +2083 -0
  44. _pystachy_source/lib/enum.py +854 -0
  45. _pystachy_source/lib/fnmatch.py +249 -0
  46. _pystachy_source/lib/functools.py +407 -0
  47. _pystachy_source/lib/genericpath.py +200 -0
  48. _pystachy_source/lib/getopt.py +214 -0
  49. _pystachy_source/lib/gettext.py +657 -0
  50. _pystachy_source/lib/glob.py +224 -0
  51. _pystachy_source/lib/heapq.py +636 -0
  52. _pystachy_source/lib/html/__init__.py +149 -0
  53. _pystachy_source/lib/html/entities.py +2514 -0
  54. _pystachy_source/lib/html/parser.py +602 -0
  55. _pystachy_source/lib/io.py +1183 -0
  56. _pystachy_source/lib/json.py +843 -0
  57. _pystachy_source/lib/keyword.py +74 -0
  58. _pystachy_source/lib/logging/__init__.py +2506 -0
  59. _pystachy_source/lib/mpz.py +127 -0
  60. _pystachy_source/lib/operator.py +455 -0
  61. _pystachy_source/lib/pathlib.py +739 -0
  62. _pystachy_source/lib/posixpath.py +578 -0
  63. _pystachy_source/lib/pyobj.py +475 -0
  64. _pystachy_source/lib/random.py +1174 -0
  65. _pystachy_source/lib/re/__init__.py +2516 -0
  66. _pystachy_source/lib/re/_constants.py +111 -0
  67. _pystachy_source/lib/re/_sre.py +2108 -0
  68. _pystachy_source/lib/re/_unicode.py +304 -0
  69. _pystachy_source/lib/shlex.py +381 -0
  70. _pystachy_source/lib/shutil.py +205 -0
  71. _pystachy_source/lib/stat.py +212 -0
  72. _pystachy_source/lib/statistics.py +1954 -0
  73. _pystachy_source/lib/string.py +352 -0
  74. _pystachy_source/lib/struct.py +15 -0
  75. _pystachy_source/lib/textwrap.py +520 -0
  76. _pystachy_source/lib/this.py +28 -0
  77. _pystachy_source/lib/threading.py +1674 -0
  78. _pystachy_source/lib/token.py +166 -0
  79. _pystachy_source/lib/tomllib/__init__.py +10 -0
  80. _pystachy_source/lib/tomllib/_parser.py +732 -0
  81. _pystachy_source/lib/tomllib/_re.py +114 -0
  82. _pystachy_source/lib/tomllib/_types.py +13 -0
  83. _pystachy_source/lib/traceback.py +184 -0
  84. _pystachy_source/lib/types.py +49 -0
  85. _pystachy_source/lib/urllib/__init__.py +0 -0
  86. _pystachy_source/lib/urllib/parse.py +1713 -0
  87. _pystachy_source/lib/warnings.py +764 -0
  88. _pystachy_source/lib/weakref.py +539 -0
  89. _pystachy_source/mpz/mpz.c +463 -0
  90. _pystachy_source/pyembed.c +373 -0
  91. _pystachy_source/pyext.c +266 -0
  92. _pystachy_source/pystachy.py +34 -0
  93. _pystachy_source/pystachy_cli.py +31 -0
  94. _pystachy_source/pystachy_toolchain.py +152 -0
  95. _pystachy_source/runtime.c +8564 -0
  96. _pystachy_source/runtime.py +2276 -0
  97. _pystachy_source/ryu/common.h +114 -0
  98. _pystachy_source/ryu/d2s.c +509 -0
  99. _pystachy_source/ryu/d2s_intrinsics.h +357 -0
  100. _pystachy_source/ryu/d2s_small_table.h +186 -0
  101. _pystachy_source/ryu/digit_table.h +35 -0
  102. _pystachy_source/ryu/ryu.h +46 -0
  103. _pystachy_source/src/__init__.py +1 -0
  104. _pystachy_source/src/pystachy_compiler/__init__.py +1 -0
  105. _pystachy_source/src/pystachy_compiler/analysis.py +2105 -0
  106. _pystachy_source/src/pystachy_compiler/argparse_schema.py +393 -0
  107. _pystachy_source/src/pystachy_compiler/bigints.py +1498 -0
  108. _pystachy_source/src/pystachy_compiler/builtins.py +3026 -0
  109. _pystachy_source/src/pystachy_compiler/calls.py +1698 -0
  110. _pystachy_source/src/pystachy_compiler/containers.py +1256 -0
  111. _pystachy_source/src/pystachy_compiler/conversions.py +399 -0
  112. _pystachy_source/src/pystachy_compiler/declarations.py +3000 -0
  113. _pystachy_source/src/pystachy_compiler/diagnostics.py +176 -0
  114. _pystachy_source/src/pystachy_compiler/driver.py +921 -0
  115. _pystachy_source/src/pystachy_compiler/dynamic.py +4513 -0
  116. _pystachy_source/src/pystachy_compiler/emission.py +666 -0
  117. _pystachy_source/src/pystachy_compiler/exceptions.py +1642 -0
  118. _pystachy_source/src/pystachy_compiler/expressions.py +5153 -0
  119. _pystachy_source/src/pystachy_compiler/flow.py +972 -0
  120. _pystachy_source/src/pystachy_compiler/functions.py +523 -0
  121. _pystachy_source/src/pystachy_compiler/gen.py +5142 -0
  122. _pystachy_source/src/pystachy_compiler/generators.py +1361 -0
  123. _pystachy_source/src/pystachy_compiler/ir.py +579 -0
  124. _pystachy_source/src/pystachy_compiler/ircheck.py +503 -0
  125. _pystachy_source/src/pystachy_compiler/lexer.py +788 -0
  126. _pystachy_source/src/pystachy_compiler/llvm_abi.py +19 -0
  127. _pystachy_source/src/pystachy_compiler/loader.py +3787 -0
  128. _pystachy_source/src/pystachy_compiler/local_versions.py +57 -0
  129. _pystachy_source/src/pystachy_compiler/lower_module.py +754 -0
  130. _pystachy_source/src/pystachy_compiler/lowering.py +625 -0
  131. _pystachy_source/src/pystachy_compiler/memory_ir.py +28 -0
  132. _pystachy_source/src/pystachy_compiler/module.py +55 -0
  133. _pystachy_source/src/pystachy_compiler/names.py +55 -0
  134. _pystachy_source/src/pystachy_compiler/optimizations.py +1445 -0
  135. _pystachy_source/src/pystachy_compiler/parser.py +2465 -0
  136. _pystachy_source/src/pystachy_compiler/program.py +1296 -0
  137. _pystachy_source/src/pystachy_compiler/reflection.py +3073 -0
  138. _pystachy_source/src/pystachy_compiler/scalar_ir.py +61 -0
  139. _pystachy_source/src/pystachy_compiler/scope.py +619 -0
  140. _pystachy_source/src/pystachy_compiler/specs.py +1102 -0
  141. _pystachy_source/src/pystachy_compiler/statements.py +2126 -0
  142. _pystachy_source/src/pystachy_compiler/structs.py +271 -0
  143. _pystachy_source/src/pystachy_compiler/syntax.py +245 -0
  144. _pystachy_source/src/pystachy_compiler/templates.py +998 -0
  145. _pystachy_source/src/pystachy_compiler/union_arithmetic.py +161 -0
  146. _pystachy_source/src/pystachy_compiler/union_proof.py +891 -0
  147. _pystachy_source/src/pystachy_compiler/union_state.py +251 -0
  148. _pystachy_source/src/pystachy_compiler/unions.py +591 -0
  149. _pystachy_source/src/pystachy_compiler/utils.py +1124 -0
  150. _pystachy_source/src/pystachy_compiler/variables.py +2323 -0
  151. pystachy-0.2.0.dist-info/METADATA +93 -0
  152. pystachy-0.2.0.dist-info/RECORD +158 -0
  153. pystachy-0.2.0.dist-info/WHEEL +5 -0
  154. pystachy-0.2.0.dist-info/entry_points.txt +2 -0
  155. pystachy-0.2.0.dist-info/licenses/LICENSE +21 -0
  156. pystachy-0.2.0.dist-info/licenses/THIRD_PARTY_NOTICES +1051 -0
  157. pystachy-0.2.0.dist-info/licenses/ryu/LICENSE-Boost +23 -0
  158. pystachy-0.2.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1 @@
1
+ v0.2.0
@@ -0,0 +1,188 @@
1
+ """Help and usage for the checked argparse schema (CPython 3.13 conventions)."""
2
+
3
+ import os
4
+ from textwrap import wrap
5
+
6
+
7
+ class ArgSpec:
8
+ def __init__(self, options: list[str], dest: str, action: str, converter: str,
9
+ nargs: str, required: bool, choices: list[str], help_text: str, metavar: str, version: str):
10
+ self.options = options
11
+ self.dest = dest
12
+ self.action = action
13
+ self.converter = converter
14
+ self.nargs = nargs
15
+ self.required = required
16
+ self.choices = choices
17
+ self.help_text = help_text
18
+ self.metavar = metavar
19
+ self.version = version
20
+
21
+
22
+ def action_name(spec: ArgSpec) -> str:
23
+ if spec.options:
24
+ return "/".join(spec.options)
25
+ return spec.metavar or spec.dest
26
+
27
+
28
+ def metavar_for(spec: ArgSpec) -> str:
29
+ if spec.metavar:
30
+ return spec.metavar
31
+ if spec.choices:
32
+ return "{" + ",".join(spec.choices) + "}"
33
+ if spec.options:
34
+ return spec.dest.upper()
35
+ return spec.dest
36
+
37
+
38
+ def _args(spec: ArgSpec) -> str:
39
+ metavar = metavar_for(spec)
40
+ if spec.nargs == "?":
41
+ return "[" + metavar + "]"
42
+ if spec.nargs == "*":
43
+ return "[" + metavar + " ...]"
44
+ if spec.nargs == "+":
45
+ return metavar + " [" + metavar + " ...]"
46
+ return metavar
47
+
48
+
49
+ def _invocation(spec: ArgSpec) -> str:
50
+ if not spec.options:
51
+ return metavar_for(spec)
52
+ header = ", ".join(spec.options)
53
+ if spec.action == "store":
54
+ header += " " + _args(spec)
55
+ return header
56
+
57
+
58
+ def _width() -> int:
59
+ # Honor the shell override; redirected output and terminals without an
60
+ # override use the standard 80-column fallback (no terminal ioctl).
61
+ try:
62
+ columns = int(os.getenv("COLUMNS", "80"))
63
+ except ValueError:
64
+ columns = 80
65
+ return columns - 2 if columns > 0 else 78
66
+
67
+
68
+ def _usage_lines(parts: list[str], indent: int, width: int, first: str = "") -> list[str]:
69
+ lines: list[str] = []
70
+ pad = " " * indent
71
+ line = first
72
+ for part in parts:
73
+ if line and len(line) + 1 + len(part) > width:
74
+ lines.append(line)
75
+ line = ""
76
+ if line:
77
+ line += " " + part
78
+ else:
79
+ line = pad + part
80
+ if line:
81
+ lines.append(line)
82
+ return lines
83
+
84
+
85
+ def _usage(prog: str, add_help: bool, specs: list[ArgSpec], width: int) -> str:
86
+ prefix = "usage: "
87
+ start = prefix + prog
88
+ options: list[str] = []
89
+ positions: list[str] = []
90
+ if add_help:
91
+ options.append("[-h]")
92
+ for spec in specs:
93
+ if spec.options:
94
+ part = spec.options[0]
95
+ if spec.action == "store":
96
+ part += " " + _args(spec)
97
+ options.append(part if spec.required else "[" + part + "]")
98
+ else:
99
+ positions.append(_args(spec))
100
+ parts = options + positions
101
+ if not parts:
102
+ return start + "\n"
103
+ line = start + (" " if prog else "") + " ".join(parts)
104
+ if len(line) <= width:
105
+ return line + "\n"
106
+ if len(start) * 4 <= 3 * width:
107
+ indent = len(start) + 1
108
+ if options:
109
+ lines = _usage_lines(options, indent, width, start)
110
+ lines.extend(_usage_lines(positions, indent, width))
111
+ else:
112
+ lines = _usage_lines(positions, indent, width, start)
113
+ else:
114
+ indent = len(prefix)
115
+ lines = _usage_lines(parts, indent, width)
116
+ if len(lines) > 1:
117
+ lines = _usage_lines(options, indent, width)
118
+ lines.extend(_usage_lines(positions, indent, width))
119
+ lines.insert(0, start)
120
+ return "\n".join(lines) + "\n"
121
+
122
+
123
+ def format_usage(prog: str, add_help: bool, specs: list[ArgSpec]) -> str:
124
+ return _usage(prog, add_help, specs, _width())
125
+
126
+
127
+ def _expand(text: str, prog: str) -> str:
128
+ # The schema accepts only the two escapes below. Splitting on %% first
129
+ # keeps an escaped %(prog)s literal and leaves prog's own '%' untouched.
130
+ return "%".join([part.replace("%(prog)s", prog) for part in text.split("%%")])
131
+
132
+
133
+ def _wrap(text: str, width: int) -> list[str]:
134
+ # argparse collapses ASCII whitespace before handing lines to textwrap.
135
+ for char in "\r\n\t\v\f":
136
+ text = text.replace(char, " ")
137
+ words = [word for word in text.split(" ") if word]
138
+ return wrap(" ".join(words).strip(), width)
139
+
140
+
141
+ def _entry(label: str, help_text: str, column: int, width: int) -> str:
142
+ header = " " + label
143
+ if not help_text:
144
+ return header + "\n"
145
+ if len(header) + 2 > column:
146
+ if not help_text.strip():
147
+ return header + "\n"
148
+ header += "\n" + " " * column
149
+ else:
150
+ header += " " * (column - len(header))
151
+ lines = _wrap(help_text, max(11, width - column))
152
+ return header + ("\n" + " " * column).join(lines) + "\n"
153
+
154
+
155
+ def _format_text(text: str, prog: str, width: int) -> str:
156
+ if "%(prog)" in text:
157
+ text = _expand(text, prog)
158
+ return "\n".join(_wrap(text, max(width, 11))) + "\n"
159
+
160
+
161
+ def format_version(text: str, prog: str) -> str:
162
+ return _format_text(text, prog, _width())
163
+
164
+
165
+ def format_help(prog: str, description: str, add_help: bool, specs: list[ArgSpec]) -> str:
166
+ width = _width()
167
+ text = _usage(prog, add_help, specs, width)
168
+ if description.strip():
169
+ text += "\n" + _format_text(description, prog, width)
170
+ longest = len("-h, --help") if add_help else 0
171
+ for spec in specs:
172
+ longest = max(longest, len(_invocation(spec)))
173
+ column = min(longest + 4, min(24, max(width - 20, 4)))
174
+ positional = ""
175
+ optional = ""
176
+ if add_help:
177
+ optional += _entry("-h, --help", "show this help message and exit", column, width)
178
+ for spec in specs:
179
+ item = _entry(_invocation(spec), _expand(spec.help_text, prog), column, width)
180
+ if spec.options:
181
+ optional += item
182
+ else:
183
+ positional += item
184
+ if positional:
185
+ text += "\npositional arguments:\n" + positional
186
+ if optional:
187
+ text += "\noptions:\n" + optional
188
+ return text.rstrip("\n") + "\n"
@@ -0,0 +1,399 @@
1
+ """Shared support for scanning document type declarations in HTML and XHTML.
2
+
3
+ This module is used as a foundation for the html.parser module. It has no
4
+ documented public API and should not be used directly.
5
+
6
+ """
7
+
8
+ import re
9
+
10
+ _declname_match = re.compile(r'[a-zA-Z][-_.a-zA-Z0-9]*\s*').match
11
+ _declstringlit_match = re.compile(r'(\'[^\']*\'|"[^"]*")\s*').match
12
+ _commentclose = re.compile(r'--\s*>')
13
+ _markedsectionclose = re.compile(r']\s*]\s*>')
14
+
15
+ # An analysis of the MS-Word extensions is available at
16
+ # http://www.planetpublish.com/xmlarena/xap/Thursday/WordtoXML.pdf
17
+
18
+ _msmarkedsectionclose = re.compile(r']\s*>')
19
+
20
+ # Pystachy port: keep the imported re name; module-name deletion is unsupported.
21
+
22
+
23
+ class ParserBase:
24
+ """Parser base class which provides some common support methods used
25
+ by the SGML/HTML and XHTML parsers."""
26
+
27
+ def __init__(self) -> None: # Pystachy port: declare the inherited field layout.
28
+ # Pystachy port: direct ParserBase construction is permitted; the abstract
29
+ # self.__class__ guard needs class objects, outside the supported subset.
30
+ self.rawdata: str = "" # Pystachy port: populated by the concrete parser.
31
+ self.lineno: int = 1 # Pystachy port: fields must be declared in __init__.
32
+ self.offset: int = 0 # Pystachy port: fields must be declared in __init__.
33
+ self._decl_otherchars: str = "" # Pystachy port: keep the class default.
34
+
35
+ def reset(self) -> None: # Pystachy port: annotate the inherited callback.
36
+ self.lineno = 1
37
+ self.offset = 0
38
+
39
+ def getpos(self) -> tuple[int, int]: # Pystachy port: annotate the inherited API.
40
+ """Return current line number and offset."""
41
+ return self.lineno, self.offset
42
+
43
+ # Internal -- update line number and offset. This should be
44
+ # called for each piece of data exactly once, in order -- in other
45
+ # words the concatenation of all the input strings to this
46
+ # function should be exactly the entire input.
47
+ def updatepos(self, i: int, j: int) -> int: # Pystachy port: integer positions.
48
+ if i >= j:
49
+ return j
50
+ rawdata = self.rawdata
51
+ nlines = rawdata.count("\n", i, j)
52
+ if nlines:
53
+ self.lineno = self.lineno + nlines
54
+ pos = rawdata.rindex("\n", i, j) # Should not fail
55
+ self.offset = j-(pos+1)
56
+ else:
57
+ self.offset = self.offset + j-i
58
+ return j
59
+
60
+ _decl_otherchars = ''
61
+
62
+ # Internal -- parse declaration (for use by subclasses).
63
+ def parse_declaration(self, i):
64
+ # This is some sort of declaration; in "HTML as
65
+ # deployed," this should only be the document type
66
+ # declaration ("<!DOCTYPE html...>").
67
+ # ISO 8879:1986, however, has more complex
68
+ # declaration syntax for elements in <!...>, including:
69
+ # --comment--
70
+ # [marked section]
71
+ # name in the following list: ENTITY, DOCTYPE, ELEMENT,
72
+ # ATTLIST, NOTATION, SHORTREF, USEMAP,
73
+ # LINKTYPE, LINK, IDLINK, USELINK, SYSTEM
74
+ rawdata = self.rawdata
75
+ j = i + 2
76
+ assert rawdata[i:j] == "<!", "unexpected call to parse_declaration"
77
+ if rawdata[j:j+1] == ">":
78
+ # the empty comment <!>
79
+ return j + 1
80
+ if rawdata[j:j+1] in ("-", ""):
81
+ # Start of comment followed by buffer boundary,
82
+ # or just a buffer boundary.
83
+ return -1
84
+ # A simple, practical version could look like: ((name|stringlit) S*) + '>'
85
+ n = len(rawdata)
86
+ if rawdata[j:j+2] == '--': #comment
87
+ # Locate --.*-- as the body of the comment
88
+ return self.parse_comment(i)
89
+ elif rawdata[j] == '[': #marked section
90
+ # Locate [statusWord [...arbitrary SGML...]] as the body of the marked section
91
+ # Where statusWord is one of TEMP, CDATA, IGNORE, INCLUDE, RCDATA
92
+ # Note that this is extended by Microsoft Office "Save as Web" function
93
+ # to include [if...] and [endif].
94
+ return self.parse_marked_section(i)
95
+ else: #all other declaration elements
96
+ decltype, j = self._scan_name(j, i)
97
+ if j < 0:
98
+ return j
99
+ if decltype == "doctype":
100
+ self._decl_otherchars = ''
101
+ while j < n:
102
+ c = rawdata[j]
103
+ if c == ">":
104
+ # end of declaration syntax
105
+ data = rawdata[i+2:j]
106
+ if decltype == "doctype":
107
+ self.handle_decl(data)
108
+ else:
109
+ # According to the HTML5 specs sections "8.2.4.44 Bogus
110
+ # comment state" and "8.2.4.45 Markup declaration open
111
+ # state", a comment token should be emitted.
112
+ # Calling unknown_decl provides more flexibility though.
113
+ self.unknown_decl(data)
114
+ return j + 1
115
+ if c in "\"'":
116
+ m = _declstringlit_match(rawdata, j)
117
+ if not m:
118
+ return -1 # incomplete
119
+ j = m.end()
120
+ elif c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
121
+ name, j = self._scan_name(j, i)
122
+ elif c in self._decl_otherchars:
123
+ j = j + 1
124
+ elif c == "[":
125
+ # this could be handled in a separate doctype parser
126
+ if decltype == "doctype":
127
+ j = self._parse_doctype_subset(j + 1, i)
128
+ elif decltype in {"attlist", "linktype", "link", "element"}:
129
+ # must tolerate []'d groups in a content model in an element declaration
130
+ # also in data attribute specifications of attlist declaration
131
+ # also link type declaration subsets in linktype declarations
132
+ # also link attribute specification lists in link declarations
133
+ raise AssertionError("unsupported '[' char in %s declaration" % decltype)
134
+ else:
135
+ raise AssertionError("unexpected '[' char in declaration")
136
+ else:
137
+ raise AssertionError("unexpected %r char in declaration" % rawdata[j])
138
+ if j < 0:
139
+ return j
140
+ return -1 # incomplete
141
+
142
+ # Internal -- parse a marked section
143
+ # Override this to handle MS-word extension syntax <![if word]>content<![endif]>
144
+ def parse_marked_section(self, i, report=1):
145
+ rawdata= self.rawdata
146
+ assert rawdata[i:i+3] == '<![', "unexpected call to parse_marked_section()"
147
+ sectName, j = self._scan_name( i+3, i )
148
+ if j < 0:
149
+ return j
150
+ if sectName in {"temp", "cdata", "ignore", "include", "rcdata"}:
151
+ # look for standard ]]> ending
152
+ match= _markedsectionclose.search(rawdata, i+3)
153
+ elif sectName in {"if", "else", "endif"}:
154
+ # look for MS Office ]> ending
155
+ match= _msmarkedsectionclose.search(rawdata, i+3)
156
+ else:
157
+ raise AssertionError(
158
+ 'unknown status keyword %r in marked section' % rawdata[i+3:j]
159
+ )
160
+ if not match:
161
+ return -1
162
+ if report:
163
+ j = match.start(0)
164
+ self.unknown_decl(rawdata[i+3: j])
165
+ return match.end(0)
166
+
167
+ # Internal -- parse comment, return length or -1 if not terminated
168
+ def parse_comment(self, i, report=1):
169
+ rawdata = self.rawdata
170
+ if rawdata[i:i+4] != '<!--':
171
+ raise AssertionError('unexpected call to parse_comment()')
172
+ match = _commentclose.search(rawdata, i+4)
173
+ if not match:
174
+ return -1
175
+ if report:
176
+ j = match.start(0)
177
+ self.handle_comment(rawdata[i+4: j])
178
+ return match.end(0)
179
+
180
+ # Internal -- scan past the internal subset in a <!DOCTYPE declaration,
181
+ # returning the index just past any whitespace following the trailing ']'.
182
+ def _parse_doctype_subset(self, i, declstartpos):
183
+ rawdata = self.rawdata
184
+ n = len(rawdata)
185
+ j = i
186
+ while j < n:
187
+ c = rawdata[j]
188
+ if c == "<":
189
+ s = rawdata[j:j+2]
190
+ if s == "<":
191
+ # end of buffer; incomplete
192
+ return -1
193
+ if s != "<!":
194
+ self.updatepos(declstartpos, j + 1)
195
+ raise AssertionError(
196
+ "unexpected char in internal subset (in %r)" % s
197
+ )
198
+ if (j + 2) == n:
199
+ # end of buffer; incomplete
200
+ return -1
201
+ if (j + 4) > n:
202
+ # end of buffer; incomplete
203
+ return -1
204
+ if rawdata[j:j+4] == "<!--":
205
+ j = self.parse_comment(j, report=0)
206
+ if j < 0:
207
+ return j
208
+ continue
209
+ name, j = self._scan_name(j + 2, declstartpos)
210
+ if j == -1:
211
+ return -1
212
+ if name not in {"attlist", "element", "entity", "notation"}:
213
+ self.updatepos(declstartpos, j + 2)
214
+ raise AssertionError(
215
+ "unknown declaration %r in internal subset" % name
216
+ )
217
+ # handle the individual names
218
+ meth = getattr(self, "_parse_doctype_" + name)
219
+ j = meth(j, declstartpos)
220
+ if j < 0:
221
+ return j
222
+ elif c == "%":
223
+ # parameter entity reference
224
+ if (j + 1) == n:
225
+ # end of buffer; incomplete
226
+ return -1
227
+ s, j = self._scan_name(j + 1, declstartpos)
228
+ if j < 0:
229
+ return j
230
+ if rawdata[j] == ";":
231
+ j = j + 1
232
+ elif c == "]":
233
+ j = j + 1
234
+ while j < n and rawdata[j].isspace():
235
+ j = j + 1
236
+ if j < n:
237
+ if rawdata[j] == ">":
238
+ return j
239
+ self.updatepos(declstartpos, j)
240
+ raise AssertionError("unexpected char after internal subset")
241
+ else:
242
+ return -1
243
+ elif c.isspace():
244
+ j = j + 1
245
+ else:
246
+ self.updatepos(declstartpos, j)
247
+ raise AssertionError("unexpected char %r in internal subset" % c)
248
+ # end of buffer reached
249
+ return -1
250
+
251
+ # Internal -- scan past <!ELEMENT declarations
252
+ def _parse_doctype_element(self, i, declstartpos):
253
+ name, j = self._scan_name(i, declstartpos)
254
+ if j == -1:
255
+ return -1
256
+ # style content model; just skip until '>'
257
+ rawdata = self.rawdata
258
+ if '>' in rawdata[j:]:
259
+ return rawdata.find(">", j) + 1
260
+ return -1
261
+
262
+ # Internal -- scan past <!ATTLIST declarations
263
+ def _parse_doctype_attlist(self, i, declstartpos):
264
+ rawdata = self.rawdata
265
+ name, j = self._scan_name(i, declstartpos)
266
+ c = rawdata[j:j+1]
267
+ if c == "":
268
+ return -1
269
+ if c == ">":
270
+ return j + 1
271
+ while 1:
272
+ # scan a series of attribute descriptions; simplified:
273
+ # name type [value] [#constraint]
274
+ name, j = self._scan_name(j, declstartpos)
275
+ if j < 0:
276
+ return j
277
+ c = rawdata[j:j+1]
278
+ if c == "":
279
+ return -1
280
+ if c == "(":
281
+ # an enumerated type; look for ')'
282
+ if ")" in rawdata[j:]:
283
+ j = rawdata.find(")", j) + 1
284
+ else:
285
+ return -1
286
+ while rawdata[j:j+1].isspace():
287
+ j = j + 1
288
+ if not rawdata[j:]:
289
+ # end of buffer, incomplete
290
+ return -1
291
+ else:
292
+ name, j = self._scan_name(j, declstartpos)
293
+ c = rawdata[j:j+1]
294
+ if not c:
295
+ return -1
296
+ if c in "'\"":
297
+ m = _declstringlit_match(rawdata, j)
298
+ if m:
299
+ j = m.end()
300
+ else:
301
+ return -1
302
+ c = rawdata[j:j+1]
303
+ if not c:
304
+ return -1
305
+ if c == "#":
306
+ if rawdata[j:] == "#":
307
+ # end of buffer
308
+ return -1
309
+ name, j = self._scan_name(j + 1, declstartpos)
310
+ if j < 0:
311
+ return j
312
+ c = rawdata[j:j+1]
313
+ if not c:
314
+ return -1
315
+ if c == '>':
316
+ # all done
317
+ return j + 1
318
+
319
+ # Internal -- scan past <!NOTATION declarations
320
+ def _parse_doctype_notation(self, i, declstartpos):
321
+ name, j = self._scan_name(i, declstartpos)
322
+ if j < 0:
323
+ return j
324
+ rawdata = self.rawdata
325
+ while 1:
326
+ c = rawdata[j:j+1]
327
+ if not c:
328
+ # end of buffer; incomplete
329
+ return -1
330
+ if c == '>':
331
+ return j + 1
332
+ if c in "'\"":
333
+ m = _declstringlit_match(rawdata, j)
334
+ if not m:
335
+ return -1
336
+ j = m.end()
337
+ else:
338
+ name, j = self._scan_name(j, declstartpos)
339
+ if j < 0:
340
+ return j
341
+
342
+ # Internal -- scan past <!ENTITY declarations
343
+ def _parse_doctype_entity(self, i, declstartpos):
344
+ rawdata = self.rawdata
345
+ if rawdata[i:i+1] == "%":
346
+ j = i + 1
347
+ while 1:
348
+ c = rawdata[j:j+1]
349
+ if not c:
350
+ return -1
351
+ if c.isspace():
352
+ j = j + 1
353
+ else:
354
+ break
355
+ else:
356
+ j = i
357
+ name, j = self._scan_name(j, declstartpos)
358
+ if j < 0:
359
+ return j
360
+ while 1:
361
+ c = self.rawdata[j:j+1]
362
+ if not c:
363
+ return -1
364
+ if c in "'\"":
365
+ m = _declstringlit_match(rawdata, j)
366
+ if m:
367
+ j = m.end()
368
+ else:
369
+ return -1 # incomplete
370
+ elif c == ">":
371
+ return j + 1
372
+ else:
373
+ name, j = self._scan_name(j, declstartpos)
374
+ if j < 0:
375
+ return j
376
+
377
+ # Internal -- scan a name token and the new position and the token, or
378
+ # return -1 if we've reached the end of the buffer.
379
+ def _scan_name(self, i, declstartpos):
380
+ rawdata = self.rawdata
381
+ n = len(rawdata)
382
+ if i == n:
383
+ return None, -1
384
+ m = _declname_match(rawdata, i)
385
+ if m:
386
+ s = m.group()
387
+ name = s.strip()
388
+ if (i + len(s)) == n:
389
+ return None, -1 # end of buffer
390
+ return name.lower(), m.end()
391
+ else:
392
+ self.updatepos(declstartpos, i)
393
+ raise AssertionError(
394
+ "expected name token at %r" % rawdata[declstartpos:declstartpos+20]
395
+ )
396
+
397
+ # To be overridden -- handlers for unknown objects
398
+ def unknown_decl(self, data: str) -> None: # Pystachy port: typed hierarchy callback.
399
+ pass