Skip to content

Commit

Permalink
C, simple type specifier parsing, handling macro-keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobandersen committed Dec 1, 2021
1 parent c09643c commit f8f2628
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
21 changes: 17 additions & 4 deletions sphinx/domains/c.py
Expand Up @@ -92,9 +92,10 @@
_string_re = re.compile(r"[LuU8]?('([^'\\]*(?:\\.[^'\\]*)*)'"
r'|"([^"\\]*(?:\\.[^"\\]*)*)")', re.S)

# bool, complex, and imaginary are macro "keywords", so they are handled seperately
_simple_type_specifiers_re = re.compile(r"""(?x)
\b(
void|_Bool|bool
void|_Bool
|signed|unsigned
|short|long
|char
Expand All @@ -103,7 +104,7 @@
|__int(8|16|32|64|128) # extension
|float|double
|_Decimal(32|64|128)
|_Complex|complex|_Imaginary|imaginary
|_Complex|_Imaginary
|__float80|_Float64x|__float128|_Float128|__ibm128 # extension
|__fp16 # extension
|_Sat|_Fract|fract|_Accum|accum # extension
Expand Down Expand Up @@ -2571,12 +2572,24 @@ def _parse_nested_name(self) -> ASTNestedName:
break
return ASTNestedName(names, rooted)

def _parse_simple_type_specifier(self) -> Optional[str]:
if self.match(_simple_type_specifiers_re):
return self.matched_text
for t in ('bool', 'complex', 'imaginary'):
if t in self.config.c_extra_keywords:
if self.skip_word(t):
return t
return None

def _parse_simple_type_specifiers(self) -> ASTTrailingTypeSpecFundamental:
names: List[str] = []

self.skip_ws()
while self.match(_simple_type_specifiers_re):
names.append(self.matched_text)
while True:
t = self._parse_simple_type_specifier()
if t is None:
break
names.append(t)
self.skip_ws()
if len(names) == 0:
return None
Expand Down
5 changes: 1 addition & 4 deletions tests/test_domain_c.py
Expand Up @@ -595,10 +595,7 @@ def test_domain_c_ast_attributes():

def test_extra_keywords():
with pytest.raises(DefinitionError,
match='Expected identifier, got user-defined keyword: complex.'):
parse('function', 'void f(int complex)')
with pytest.raises(DefinitionError,
match='Expected identifier, got user-defined keyword: complex.'):
match='Expected identifier in nested name'):
parse('function', 'void complex(void)')


Expand Down

0 comments on commit f8f2628

Please sign in to comment.