1
+ import dataclasses as dc
1
2
import re
2
3
import sys
3
4
from collections .abc import Callable
@@ -15,6 +16,11 @@ def negate(condition: str) -> str:
15
16
return condition [1 :]
16
17
return "!" + condition
17
18
19
+
20
+ is_a_simple_defined = re .compile (r'^defined\s*\(\s*[A-Za-z0-9_]+\s*\)$' ).match
21
+
22
+
23
+ @dc .dataclass (repr = False )
18
24
class Monitor :
19
25
"""
20
26
A simple C preprocessor that scans C source and computes, line by line,
@@ -27,25 +33,20 @@ class Monitor:
27
33
28
34
Anyway this implementation seems to work well enough for the CPython sources.
29
35
"""
36
+ filename : str | None = None
37
+ _ : dc .KW_ONLY
38
+ verbose : bool = False
30
39
31
- is_a_simple_defined : Callable [[str ], re .Match [str ] | None ]
32
- is_a_simple_defined = re .compile (r'^defined\s*\(\s*[A-Za-z0-9_]+\s*\)$' ).match
33
-
34
- def __init__ (self , filename : str | None = None , * , verbose : bool = False ) -> None :
40
+ def __post_init__ (self ) -> None :
35
41
self .stack : TokenStack = []
36
42
self .in_comment = False
37
43
self .continuation : str | None = None
38
44
self .line_number = 0
39
- self .filename = filename
40
- self .verbose = verbose
41
45
42
46
def __repr__ (self ) -> str :
43
- return '' .join ((
44
- '<Monitor ' ,
45
- str (id (self )),
46
- " line=" , str (self .line_number ),
47
- " condition=" , repr (self .condition ()),
48
- ">" ))
47
+ return (
48
+ f"<Monitor { id (self )} line={ self .line_number } condition={ self .condition ()!r} >"
49
+ )
49
50
50
51
def status (self ) -> str :
51
52
return str (self .line_number ).rjust (4 ) + ": " + self .condition ()
@@ -152,7 +153,7 @@ def pop_stack() -> TokenAndCondition:
152
153
if not condition :
153
154
self .fail ("Invalid format for #" + token + " line: no argument!" )
154
155
if token in {'if' , 'elif' }:
155
- if not self . is_a_simple_defined (condition ):
156
+ if not is_a_simple_defined (condition ):
156
157
condition = "(" + condition + ")"
157
158
if token == 'elif' :
158
159
previous_token , previous_condition = pop_stack ()
0 commit comments