Holds states related to parsing braces.
| 2898 | |
| 2899 | |
| 2900 | class NestingState(object): |
| 2901 | """Holds states related to parsing braces.""" |
| 2902 | |
| 2903 | def __init__(self): |
| 2904 | # Stack for tracking all braces. An object is pushed whenever we |
| 2905 | # see a "{", and popped when we see a "}". Only 3 types of |
| 2906 | # objects are possible: |
| 2907 | # - _ClassInfo: a class or struct. |
| 2908 | # - _NamespaceInfo: a namespace. |
| 2909 | # - _BlockInfo: some other type of block. |
| 2910 | self.stack = [] |
| 2911 | |
| 2912 | # Top of the previous stack before each Update(). |
| 2913 | # |
| 2914 | # Because the nesting_stack is updated at the end of each line, we |
| 2915 | # had to do some convoluted checks to find out what is the current |
| 2916 | # scope at the beginning of the line. This check is simplified by |
| 2917 | # saving the previous top of nesting stack. |
| 2918 | # |
| 2919 | # We could save the full stack, but we only need the top. Copying |
| 2920 | # the full nesting stack would slow down cpplint by ~10%. |
| 2921 | self.previous_stack_top = [] |
| 2922 | |
| 2923 | # Stack of _PreprocessorInfo objects. |
| 2924 | self.pp_stack = [] |
| 2925 | |
| 2926 | def SeenOpenBrace(self): |
| 2927 | """Check if we have seen the opening brace for the innermost block. |
| 2928 | |
| 2929 | Returns: |
| 2930 | True if we have seen the opening brace, False if the innermost |
| 2931 | block is still expecting an opening brace. |
| 2932 | """ |
| 2933 | return (not self.stack) or self.stack[-1].seen_open_brace |
| 2934 | |
| 2935 | def InNamespaceBody(self): |
| 2936 | """Check if we are currently one level inside a namespace body. |
| 2937 | |
| 2938 | Returns: |
| 2939 | True if top of the stack is a namespace block, False otherwise. |
| 2940 | """ |
| 2941 | return self.stack and isinstance(self.stack[-1], _NamespaceInfo) |
| 2942 | |
| 2943 | def InExternC(self): |
| 2944 | """Check if we are currently one level inside an 'extern "C"' block. |
| 2945 | |
| 2946 | Returns: |
| 2947 | True if top of the stack is an extern block, False otherwise. |
| 2948 | """ |
| 2949 | return self.stack and isinstance(self.stack[-1], _ExternCInfo) |
| 2950 | |
| 2951 | def InClassDeclaration(self): |
| 2952 | """Check if we are currently one level inside a class or struct declaration. |
| 2953 | |
| 2954 | Returns: |
| 2955 | True if top of the stack is a class/struct, False otherwise. |
| 2956 | """ |
| 2957 | return self.stack and isinstance(self.stack[-1], _ClassInfo) |