def build_parent_group_mapping()

in utils/sexp_reader.py [0:0]


def build_parent_group_mapping(toks):
    '''
    Utility function to construct a mapping for the start index to end index
    for every (sub-)tree in the `toks` list. Example the input `toks` list:
    ['(','(ORDER', '(', 'PIZZAORDER', '(', 'NUMBER', '1', ')', '(', 'TOPPING', 'HAM', ')',
        '(', 'COMPLEX_TOPPING', '(', 'TOPPING', 'ONIONS', ')', '(', 'QUANTITY', 'EXTRA',')',
         ')', ')', ')']

    :param toks: (list) List of tokens in the tokenized EXR-format flat string
    :return: (dict) Mapping of start index to end index for every (sub-)tree 
        in the `toks` list.
    '''
    i, N = 0, len(toks)
    stack = []
    parent_group_mapping = {}

    while i < N:
        if toks[i] == '(':
            stack.append(i)
        if toks[i] == ')':
            parent_group_mapping[stack.pop()] = i
        i += 1
    return parent_group_mapping