Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Slightly annotate cpp.py
  • Loading branch information
erlend-aasland committed May 1, 2023
commit ca8532b9c0ea2111c5b4ce1d30e5711bfef5d9c7
26 changes: 15 additions & 11 deletions Tools/clinic/cpp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import re
import sys
from typing import NoReturn, Callable

def negate(condition):
TokenAndCondition = tuple[str, str]
TokenStack = list[TokenAndCondition]

def negate(condition: str) -> str:
"""
Returns a CPP conditional that is the opposite of the conditional passed in.
"""
Expand All @@ -22,28 +26,28 @@ class Monitor:
Anyway this implementation seems to work well enough for the CPython sources.
"""

is_a_simple_defined = re.compile(r'^defined\s*\(\s*[A-Za-z0-9_]+\s*\)$').match
is_a_simple_defined: Callable = re.compile(r'^defined\s*\(\s*[A-Za-z0-9_]+\s*\)$').match

def __init__(self, filename=None, *, verbose=False):
self.stack = []
def __init__(self, filename=None, *, verbose: bool = False):
self.stack: TokenStack = []
self.in_comment = False
self.continuation = None
self.continuation: str | None = None
self.line_number = 0
self.filename = filename
self.verbose = verbose

def __repr__(self):
def __repr__(self) -> str:
return ''.join((
'<Monitor ',
str(id(self)),
" line=", str(self.line_number),
" condition=", repr(self.condition()),
">"))

def status(self):
def status(self) -> str:
return str(self.line_number).rjust(4) + ": " + self.condition()

def condition(self):
def condition(self) -> str:
"""
Returns the current preprocessor state, as a single #if condition.
"""
Expand All @@ -62,15 +66,15 @@ def close(self):
if self.stack:
self.fail("Ended file while still in a preprocessor conditional block!")

def write(self, s):
def write(self, s: str):
for line in s.split("\n"):
self.writeline(line)

def writeline(self, line):
def writeline(self, line: str):
self.line_number += 1
line = line.strip()

def pop_stack():
def pop_stack() -> TokenAndCondition | NoReturn:
if not self.stack:
self.fail("#" + token + " without matching #if / #ifdef / #ifndef!")
return self.stack.pop()
Expand Down