def explode_list(dct, lst, pos): t = OptionTree() if pos < len(lst): for obj in dct[lst[pos]]: c = explode_list(dct, lst, pos + 1) c.element = obj t.append(c) return t
def parse_from(fsa, tokens, index, state): output = OptionTree() expected_final = index == len(tokens) is_final = state in fsa.get_final() if expected_final: if is_final: return output else: raise ExpectedStopError(tokens[:index+1], state) transitions = fsa.transitions_from(state) token = tokens[index] matching = [(label, end, tag) for label, end, tag in transitions if self.match(label, token)] if len(matching) == 0: raise ParseError(tokens[:index+1], state) m_pe = None for label, end, tag in matching: try: following = parse_from(fsa, tokens, index + 1, end) following.element = (self.process(label, token), tag) output.append(following) except ParseError, pe: if (not m_pe) or len(pe) > len(m_pe): m_pe = pe