def parse_sexp()

in utils/sexp_reader.py [0:0]


def parse_sexp(toks, start_index, end_index, parent_group_mapping):
    '''
    This is a utility function to convert the EXR-format flat string into a tree format

    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
    :param start_index: (int) Starting index in the `toks` list.
    :param end_index: (int) End index in the `toks` list 
    :param parent_group_mapping: (dict) Mapping of start index to end index 
        for every (sub-)tree in the `toks` list.

    :return: (AnyNode) A node specifying the root of a (sub-)tree constructed 
        from a subsequence in `toks` list.
    '''
    if toks[start_index] != '(':
        return AnyNode(id=toks[start_index])
    else:
        root_node = toks[start_index+1]
        args = []
        # Points to the beginning of the first leftmost sub-tree
        i = start_index+2
        # We run a loop to construct a sub-tree from a sub-sequence in the
        # `toks` list.
        while i < end_index-1:
            j = parent_group_mapping[i]+1 if toks[i] == '(' else i+1
            # Construct a node from the sub-sequence for which 
            # streches from index {i,...,j-1}.
            child = parse_sexp(toks, i, j, parent_group_mapping)
            args.append(child)
            i = j
        return AnyNode(id=root_node,children=args)