Holds states related to parsing braces.
| 2409 | |
| 2410 | |
| 2411 | class NestingState(object): |
| 2412 | """Holds states related to parsing braces.""" |
| 2413 | |
| 2414 | def __init__(self): |
| 2415 | # Stack for tracking all braces. An object is pushed whenever we |
| 2416 | # see a "{", and popped when we see a "}". Only 3 types of |
| 2417 | # objects are possible: |
| 2418 | # - _ClassInfo: a class or struct. |
| 2419 | # - _NamespaceInfo: a namespace. |
| 2420 | # - _BlockInfo: some other type of block. |
| 2421 | self.stack = [] |
| 2422 | |
| 2423 | # Top of the previous stack before each Update(). |
| 2424 | # |
| 2425 | # Because the nesting_stack is updated at the end of each line, we |
| 2426 | # had to do some convoluted checks to find out what is the current |
| 2427 | # scope at the beginning of the line. This check is simplified by |
| 2428 | # saving the previous top of nesting stack. |
| 2429 | # |
| 2430 | # We could save the full stack, but we only need the top. Copying |
| 2431 | # the full nesting stack would slow down cpplint by ~10%. |
| 2432 | self.previous_stack_top = [] |
| 2433 | |
| 2434 | # Stack of _PreprocessorInfo objects. |
| 2435 | self.pp_stack = [] |
| 2436 | |
| 2437 | def SeenOpenBrace(self): |
| 2438 | """Check if we have seen the opening brace for the innermost block. |
| 2439 | |
| 2440 | Returns: |
| 2441 | True if we have seen the opening brace, False if the innermost |
| 2442 | block is still expecting an opening brace. |
| 2443 | """ |
| 2444 | return (not self.stack) or self.stack[-1].seen_open_brace |
| 2445 | |
| 2446 | def InNamespaceBody(self): |
| 2447 | """Check if we are currently one level inside a namespace body. |
| 2448 | |
| 2449 | Returns: |
| 2450 | True if top of the stack is a namespace block, False otherwise. |
| 2451 | """ |
| 2452 | return self.stack and isinstance(self.stack[-1], _NamespaceInfo) |
| 2453 | |
| 2454 | def InExternC(self): |
| 2455 | """Check if we are currently one level inside an 'extern "C"' block. |
| 2456 | |
| 2457 | Returns: |
| 2458 | True if top of the stack is an extern block, False otherwise. |
| 2459 | """ |
| 2460 | return self.stack and isinstance(self.stack[-1], _ExternCInfo) |
| 2461 | |
| 2462 | def InClassDeclaration(self): |
| 2463 | """Check if we are currently one level inside a class or struct declaration. |
| 2464 | |
| 2465 | Returns: |
| 2466 | True if top of the stack is a class/struct, False otherwise. |
| 2467 | """ |
| 2468 | return self.stack and isinstance(self.stack[-1], _ClassInfo) |