Holds states related to parsing braces.
| 2197 | |
| 2198 | |
| 2199 | class NestingState(object): |
| 2200 | """Holds states related to parsing braces.""" |
| 2201 | |
| 2202 | def __init__(self): |
| 2203 | # Stack for tracking all braces. An object is pushed whenever we |
| 2204 | # see a "{", and popped when we see a "}". Only 3 types of |
| 2205 | # objects are possible: |
| 2206 | # - _ClassInfo: a class or struct. |
| 2207 | # - _NamespaceInfo: a namespace. |
| 2208 | # - _BlockInfo: some other type of block. |
| 2209 | self.stack = [] |
| 2210 | |
| 2211 | # Top of the previous stack before each Update(). |
| 2212 | # |
| 2213 | # Because the nesting_stack is updated at the end of each line, we |
| 2214 | # had to do some convoluted checks to find out what is the current |
| 2215 | # scope at the beginning of the line. This check is simplified by |
| 2216 | # saving the previous top of nesting stack. |
| 2217 | # |
| 2218 | # We could save the full stack, but we only need the top. Copying |
| 2219 | # the full nesting stack would slow down cpplint by ~10%. |
| 2220 | self.previous_stack_top = [] |
| 2221 | |
| 2222 | # Stack of _PreprocessorInfo objects. |
| 2223 | self.pp_stack = [] |
| 2224 | |
| 2225 | def SeenOpenBrace(self): |
| 2226 | """Check if we have seen the opening brace for the innermost block. |
| 2227 | |
| 2228 | Returns: |
| 2229 | True if we have seen the opening brace, False if the innermost |
| 2230 | block is still expecting an opening brace. |
| 2231 | """ |
| 2232 | return (not self.stack) or self.stack[-1].seen_open_brace |
| 2233 | |
| 2234 | def InNamespaceBody(self): |
| 2235 | """Check if we are currently one level inside a namespace body. |
| 2236 | |
| 2237 | Returns: |
| 2238 | True if top of the stack is a namespace block, False otherwise. |
| 2239 | """ |
| 2240 | return self.stack and isinstance(self.stack[-1], _NamespaceInfo) |
| 2241 | |
| 2242 | def InExternC(self): |
| 2243 | """Check if we are currently one level inside an 'extern "C"' block. |
| 2244 | |
| 2245 | Returns: |
| 2246 | True if top of the stack is an extern block, False otherwise. |
| 2247 | """ |
| 2248 | return self.stack and isinstance(self.stack[-1], _ExternCInfo) |
| 2249 | |
| 2250 | def InClassDeclaration(self): |
| 2251 | """Check if we are currently one level inside a class or struct declaration. |
| 2252 | |
| 2253 | Returns: |
| 2254 | True if top of the stack is a class/struct, False otherwise. |
| 2255 | """ |
| 2256 | return self.stack and isinstance(self.stack[-1], _ClassInfo) |