Skip to content

Commit 286001c

Browse files
committed
Replace consume_parens with generic balanced consumer
1 parent 22f18b2 commit 286001c

File tree

1 file changed

+52
-14
lines changed

1 file changed

+52
-14
lines changed

CppHeaderParser/CppHeaderParser.py

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -419,21 +419,59 @@ def _fix_classname(self):
419419
return s
420420

421421

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,))
424444
sl = len(stack)
425-
nested = 1
426-
while i < sl:
427-
t = stack[i]
445+
446+
while True:
428447
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
435471

436-
raise CppParseError("Unmatched (")
472+
next_end = _start_balanced_items.get(tok)
473+
if next_end:
474+
match_stack.append(next_end)
437475

438476

439477
def _parse_template_decl(stack):
@@ -472,7 +510,7 @@ def _parse_template_decl(stack):
472510
require_ending = True
473511
elif t == "(":
474512
s = stack[i:]
475-
n = _consume_parens(s)
513+
n = _consume_balanced_items(s, ")", -1)
476514
i += n
477515
param["param"] = param["param"] + "".join(s[:n])
478516
else:
@@ -583,7 +621,7 @@ def _parse_cpp_base(stack, default_access):
583621

584622
if t == "(":
585623
s = stack[i:]
586-
n = _consume_parens(s)
624+
n = _consume_balanced_items(s, ")", -1)
587625
i += n
588626
base["decl_name"] = base["decl_name"] + "".join(s[:n])
589627
elif t == "...":

0 commit comments

Comments
 (0)