Holds states related to parsing braces.
| 3213 | |
| 3214 | |
| 3215 | class NestingState: |
| 3216 | """Holds states related to parsing braces.""" |
| 3217 | |
| 3218 | def __init__(self): |
| 3219 | # Stack for tracking all braces. An object is pushed whenever we |
| 3220 | # see a "{", and popped when we see a "}". Only 3 types of |
| 3221 | # objects are possible: |
| 3222 | # - _ClassInfo: a class or struct. |
| 3223 | # - _NamespaceInfo: a namespace. |
| 3224 | # - _BlockInfo: some other type of block. |
| 3225 | self.stack: list[_BlockInfo] = [] |
| 3226 | |
| 3227 | # Top of the previous stack before each Update(). |
| 3228 | # |
| 3229 | # Because the nesting_stack is updated at the end of each line, we |
| 3230 | # had to do some convoluted checks to find out what is the current |
| 3231 | # scope at the beginning of the line. This check is simplified by |
| 3232 | # saving the previous top of nesting stack. |
| 3233 | # |
| 3234 | # We could save the full stack, but we only need the top. Copying |
| 3235 | # the full nesting stack would slow down cpplint by ~10%. |
| 3236 | self.previous_stack_top: _BlockInfo | None = None |
| 3237 | |
| 3238 | # The number of open parentheses in the previous stack top before the last update. |
| 3239 | # Used to prevent false indentation detection when e.g. a function parameter is indented. |
| 3240 | # We can't use previous_stack_top, a shallow copy whose open_parentheses value is updated. |
| 3241 | self.previous_open_parentheses = 0 |
| 3242 | |
| 3243 | # The last stack item we popped. |
| 3244 | self.popped_top: _BlockInfo | None = None |
| 3245 | |
| 3246 | # Stack of _PreprocessorInfo objects. |
| 3247 | self.pp_stack = [] |
| 3248 | |
| 3249 | def SeenOpenBrace(self): |
| 3250 | """Check if we have seen the opening brace for the innermost block. |
| 3251 | |
| 3252 | Returns: |
| 3253 | True if we have seen the opening brace, False if the innermost |
| 3254 | block is still expecting an opening brace. |
| 3255 | """ |
| 3256 | return (not self.stack) or self.stack[-1].seen_open_brace |
| 3257 | |
| 3258 | def InNamespaceBody(self): |
| 3259 | """Check if we are currently one level inside a namespace body. |
| 3260 | |
| 3261 | Returns: |
| 3262 | True if top of the stack is a namespace block, False otherwise. |
| 3263 | """ |
| 3264 | return self.stack and isinstance(self.stack[-1], _NamespaceInfo) |
| 3265 | |
| 3266 | def InExternC(self): |
| 3267 | """Check if we are currently one level inside an 'extern "C"' block. |
| 3268 | |
| 3269 | Returns: |
| 3270 | True if top of the stack is an extern block, False otherwise. |
| 3271 | """ |
| 3272 | return self.stack and isinstance(self.stack[-1], _ExternCInfo) |