Holds states related to parsing braces.
| 2309 | |
| 2310 | |
| 2311 | class NestingState(object): |
| 2312 | """Holds states related to parsing braces.""" |
| 2313 | |
| 2314 | def __init__(self): |
| 2315 | # Stack for tracking all braces. An object is pushed whenever we |
| 2316 | # see a "{", and popped when we see a "}". Only 3 types of |
| 2317 | # objects are possible: |
| 2318 | # - _ClassInfo: a class or struct. |
| 2319 | # - _NamespaceInfo: a namespace. |
| 2320 | # - _BlockInfo: some other type of block. |
| 2321 | self.stack = [] |
| 2322 | |
| 2323 | # Top of the previous stack before each Update(). |
| 2324 | # |
| 2325 | # Because the nesting_stack is updated at the end of each line, we |
| 2326 | # had to do some convoluted checks to find out what is the current |
| 2327 | # scope at the beginning of the line. This check is simplified by |
| 2328 | # saving the previous top of nesting stack. |
| 2329 | # |
| 2330 | # We could save the full stack, but we only need the top. Copying |
| 2331 | # the full nesting stack would slow down cpplint by ~10%. |
| 2332 | self.previous_stack_top = [] |
| 2333 | |
| 2334 | # Stack of _PreprocessorInfo objects. |
| 2335 | self.pp_stack = [] |
| 2336 | |
| 2337 | def SeenOpenBrace(self): |
| 2338 | """Check if we have seen the opening brace for the innermost block. |
| 2339 | |
| 2340 | Returns: |
| 2341 | True if we have seen the opening brace, False if the innermost |
| 2342 | block is still expecting an opening brace. |
| 2343 | """ |
| 2344 | return (not self.stack) or self.stack[-1].seen_open_brace |
| 2345 | |
| 2346 | def InNamespaceBody(self): |
| 2347 | """Check if we are currently one level inside a namespace body. |
| 2348 | |
| 2349 | Returns: |
| 2350 | True if top of the stack is a namespace block, False otherwise. |
| 2351 | """ |
| 2352 | return self.stack and isinstance(self.stack[-1], _NamespaceInfo) |
| 2353 | |
| 2354 | def InExternC(self): |
| 2355 | """Check if we are currently one level inside an 'extern "C"' block. |
| 2356 | |
| 2357 | Returns: |
| 2358 | True if top of the stack is an extern block, False otherwise. |
| 2359 | """ |
| 2360 | return self.stack and isinstance(self.stack[-1], _ExternCInfo) |
| 2361 | |
| 2362 | def InClassDeclaration(self): |
| 2363 | """Check if we are currently one level inside a class or struct declaration. |
| 2364 | |
| 2365 | Returns: |
| 2366 | True if top of the stack is a class/struct, False otherwise. |
| 2367 | """ |
| 2368 | return self.stack and isinstance(self.stack[-1], _ClassInfo) |