I want to know if the following parser covers every possible cases, and if the code can be cleaner/better organized.Python 3 code:def add_element(cache, element):    if element != '':        cache[-1].append(element)    return ''def to_py(raw_tcl_list):    out = []    cache = [out]    element = ''    escape = False    for char in raw_tcl_list:        if escape:            if char not in [\\, {, }, [, ], $]:                raise ValueError(Incorrect escape character %s % char)            element += char            escape = False        elif char == \\:            escape = True        elif char in [ , \t, \r, \n]:            element = add_element(cache, element)        elif char == {:            a = []            cache[-1].append(a)            cache.append(a)        elif char == }:            element = add_element(cache, element)            if len(cache) < 2:                raise ValueError(Close bracket without opening bracket.)            cache.pop()        else:            element += char    if len(cache) != 1:        raise ValueError(Mismatched brackets.)    return out[0]def to_tcl(py_list):    if type(py_list) == list:        out_str = {         for item in py_list:            out_str += to_tcl(item)        out_str += }         return out_str    else:        out_str = str(py_list) +          for c in [\\, {, }, [, ], $]:            out_str = out_str.replace(c, \\ + c)        return out_strimport pprintx = { 12 apple {100} {} {{12 34}} \n {56\n { \\{78 {11 12 11} 10}}}y = to_py(x)z = to_tcl(y)print(# Original #)pprint.pprint(x)print(\n# Translated to Python List #)pprint.pprint(y)print(\n# Translat back to Tcl List #)print(z)Output:# Original #'{ 12 apple {100} {} {{12 34}} \n {56\n { \\{78 {11 12 11} 10}}}'# Translated to Python List #['12', 'apple', ['100'], [], [['12', '34']], ['56', ['{78', ['11', '12', '11'], '10']]]# Translat back to Tcl List #{ 12 apple { 100 } { } { { 12 34 } } { 56 { \{78 { 11 12 11 } 10 } } } list of Tcl special characters: http://wiki.tcl.tk/989#pagetoceaa9fda9
Translate between Tcl and Python List
python;tcl