@@ -419,21 +419,59 @@ def _fix_classname(self):
419
419
return s
420
420
421
421
422
- def _consume_parens (stack ):
423
- i = 0
422
+ _end_balanced_items = {">" , "}" , "]" , ")" , "]]" }
423
+ _start_balanced_items = {
424
+ "<" : ">" ,
425
+ "{" : "}" ,
426
+ "(" : ")" ,
427
+ "[" : "]" ,
428
+ "[[" : "]]" ,
429
+ }
430
+
431
+
432
+ def _consume_balanced_items (stack , init_expected , i ):
433
+ """
434
+ identical to consume_balanced_tokens, but works on a stack instead
435
+ TODO: make them the same function
436
+
437
+ :param stack: Stack of tokens to search
438
+ :param init_expected: expected token to balance
439
+ :param i: Position in stack of initial token
440
+
441
+ :returns: position of next token after balanced token
442
+ """
443
+ match_stack = deque ((init_expected ,))
424
444
sl = len (stack )
425
- nested = 1
426
- while i < sl :
427
- t = stack [i ]
445
+
446
+ while True :
428
447
i += 1
429
- if t == ")" :
430
- nested -= 1
431
- if nested == 0 :
432
- return i
433
- elif t == "(" :
434
- nested += 1
448
+ if i >= sl :
449
+ errmsg = "Did not find matching '%s'" % init_expected
450
+ raise CppParseError (errmsg )
451
+
452
+ tok = stack [i ]
453
+ if tok in _end_balanced_items :
454
+ expected = match_stack .pop ()
455
+ if tok != expected :
456
+ # hack: ambiguous right-shift issues here, really
457
+ # should be looking at the context
458
+ if tok == ">" :
459
+ i += 1
460
+ if i < sl and stack [i ] == ">" :
461
+ match_stack .append (expected )
462
+ continue
463
+
464
+ errmsg = "Expected '%s', found '%s'" % (expected , tok )
465
+ raise CppParseError (errmsg )
466
+
467
+ if len (match_stack ) == 0 :
468
+ return i + 1
469
+
470
+ continue
435
471
436
- raise CppParseError ("Unmatched (" )
472
+ next_end = _start_balanced_items .get (tok )
473
+ if next_end :
474
+ match_stack .append (next_end )
437
475
438
476
439
477
def _parse_template_decl (stack ):
@@ -472,7 +510,7 @@ def _parse_template_decl(stack):
472
510
require_ending = True
473
511
elif t == "(" :
474
512
s = stack [i :]
475
- n = _consume_parens ( s )
513
+ n = _consume_balanced_items ( s , ")" , - 1 )
476
514
i += n
477
515
param ["param" ] = param ["param" ] + "" .join (s [:n ])
478
516
else :
@@ -583,7 +621,7 @@ def _parse_cpp_base(stack, default_access):
583
621
584
622
if t == "(" :
585
623
s = stack [i :]
586
- n = _consume_parens ( s )
624
+ n = _consume_balanced_items ( s , ")" , - 1 )
587
625
i += n
588
626
base ["decl_name" ] = base ["decl_name" ] + "" .join (s [:n ])
589
627
elif t == "..." :
0 commit comments