mirror of https://github.com/xemu-project/xemu.git
qapi: add must_match helper
Mypy cannot generally understand that these regex functions cannot possibly fail. Add a "must_match" helper that makes this clear for mypy. Signed-off-by: John Snow <jsnow@redhat.com> Message-Id: <20210519183951.3946870-10-jsnow@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Markus Armbruster <armbru@redhat.com>
This commit is contained in:
parent
43b1be65f0
commit
e0e8a0ac2e
|
@ -12,7 +12,7 @@
|
||||||
# See the COPYING file in the top-level directory.
|
# See the COPYING file in the top-level directory.
|
||||||
|
|
||||||
import re
|
import re
|
||||||
from typing import Optional, Sequence
|
from typing import Match, Optional, Sequence
|
||||||
|
|
||||||
|
|
||||||
#: Magic string that gets removed along with all space to its right.
|
#: Magic string that gets removed along with all space to its right.
|
||||||
|
@ -210,3 +210,9 @@ def gen_endif(ifcond: Sequence[str]) -> str:
|
||||||
#endif /* %(cond)s */
|
#endif /* %(cond)s */
|
||||||
''', cond=ifc)
|
''', cond=ifc)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
def must_match(pattern: str, string: str) -> Match[str]:
|
||||||
|
match = re.match(pattern, string)
|
||||||
|
assert match is not None
|
||||||
|
return match
|
||||||
|
|
|
@ -8,11 +8,11 @@ This is the main entry point for generating C code from the QAPI schema.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import re
|
|
||||||
import sys
|
import sys
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from .commands import gen_commands
|
from .commands import gen_commands
|
||||||
|
from .common import must_match
|
||||||
from .error import QAPIError
|
from .error import QAPIError
|
||||||
from .events import gen_events
|
from .events import gen_events
|
||||||
from .introspect import gen_introspect
|
from .introspect import gen_introspect
|
||||||
|
@ -22,9 +22,7 @@ from .visit import gen_visit
|
||||||
|
|
||||||
|
|
||||||
def invalid_prefix_char(prefix: str) -> Optional[str]:
|
def invalid_prefix_char(prefix: str) -> Optional[str]:
|
||||||
match = re.match(r'([A-Za-z_.-][A-Za-z0-9_.-]*)?', prefix)
|
match = must_match(r'([A-Za-z_.-][A-Za-z0-9_.-]*)?', prefix)
|
||||||
# match cannot be None, but mypy cannot infer that.
|
|
||||||
assert match is not None
|
|
||||||
if match.end() != len(prefix):
|
if match.end() != len(prefix):
|
||||||
return prefix[match.end()]
|
return prefix[match.end()]
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -18,6 +18,7 @@ from collections import OrderedDict
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
from .common import must_match
|
||||||
from .error import QAPISemError, QAPISourceError
|
from .error import QAPISemError, QAPISourceError
|
||||||
from .source import QAPISourceInfo
|
from .source import QAPISourceInfo
|
||||||
|
|
||||||
|
@ -238,8 +239,8 @@ class QAPISchemaParser:
|
||||||
elif not self.tok.isspace():
|
elif not self.tok.isspace():
|
||||||
# Show up to next structural, whitespace or quote
|
# Show up to next structural, whitespace or quote
|
||||||
# character
|
# character
|
||||||
match = re.match('[^[\\]{}:,\\s\'"]+',
|
match = must_match('[^[\\]{}:,\\s\'"]+',
|
||||||
self.src[self.cursor-1:])
|
self.src[self.cursor-1:])
|
||||||
raise QAPIParseError(self, "stray '%s'" % match.group(0))
|
raise QAPIParseError(self, "stray '%s'" % match.group(0))
|
||||||
|
|
||||||
def get_members(self):
|
def get_members(self):
|
||||||
|
@ -369,7 +370,7 @@ class QAPIDoc:
|
||||||
# Strip leading spaces corresponding to the expected indent level
|
# Strip leading spaces corresponding to the expected indent level
|
||||||
# Blank lines are always OK.
|
# Blank lines are always OK.
|
||||||
if line:
|
if line:
|
||||||
indent = re.match(r'\s*', line).end()
|
indent = must_match(r'\s*', line).end()
|
||||||
if indent < self._indent:
|
if indent < self._indent:
|
||||||
raise QAPIParseError(
|
raise QAPIParseError(
|
||||||
self._parser,
|
self._parser,
|
||||||
|
@ -505,7 +506,7 @@ class QAPIDoc:
|
||||||
# from line and replace it with spaces so that 'f' has the
|
# from line and replace it with spaces so that 'f' has the
|
||||||
# same index as it did in the original line and can be
|
# same index as it did in the original line and can be
|
||||||
# handled the same way we will handle following lines.
|
# handled the same way we will handle following lines.
|
||||||
indent = re.match(r'@\S*:\s*', line).end()
|
indent = must_match(r'@\S*:\s*', line).end()
|
||||||
line = line[indent:]
|
line = line[indent:]
|
||||||
if not line:
|
if not line:
|
||||||
# Line was just the "@arg:" header; following lines
|
# Line was just the "@arg:" header; following lines
|
||||||
|
@ -540,7 +541,7 @@ class QAPIDoc:
|
||||||
# from line and replace it with spaces so that 'f' has the
|
# from line and replace it with spaces so that 'f' has the
|
||||||
# same index as it did in the original line and can be
|
# same index as it did in the original line and can be
|
||||||
# handled the same way we will handle following lines.
|
# handled the same way we will handle following lines.
|
||||||
indent = re.match(r'@\S*:\s*', line).end()
|
indent = must_match(r'@\S*:\s*', line).end()
|
||||||
line = line[indent:]
|
line = line[indent:]
|
||||||
if not line:
|
if not line:
|
||||||
# Line was just the "@arg:" header; following lines
|
# Line was just the "@arg:" header; following lines
|
||||||
|
@ -586,7 +587,7 @@ class QAPIDoc:
|
||||||
# from line and replace it with spaces so that 'f' has the
|
# from line and replace it with spaces so that 'f' has the
|
||||||
# same index as it did in the original line and can be
|
# same index as it did in the original line and can be
|
||||||
# handled the same way we will handle following lines.
|
# handled the same way we will handle following lines.
|
||||||
indent = re.match(r'\S*:\s*', line).end()
|
indent = must_match(r'\S*:\s*', line).end()
|
||||||
line = line[indent:]
|
line = line[indent:]
|
||||||
if not line:
|
if not line:
|
||||||
# Line was just the "Section:" header; following lines
|
# Line was just the "Section:" header; following lines
|
||||||
|
|
Loading…
Reference in New Issue