#NEWLINE# def parse( s , name = None ) : #NEWLINE# #INDENT# '\n    Parses a string into a kind of AST\n\n        >>> parse(\'{{x}}\')\n        [(\'expr\', (1, 3), \'x\')]\n        >>> parse(\'foo\')\n        [\'foo\']\n        >>> parse(\'{{if x}}test{{endif}}\')\n        [(\'cond\', (1, 3), (\'if\', (1, 3), \'x\', [\'test\']))]\n        >>> parse(\'series->{{for x in y}}x={{x}}{{endfor}}\')\n        [\'series->\', (\'for\', (1, 11), (\'x\',), \'y\', [\'x=\', (\'expr\', (1, 27), \'x\')])]\n        >>> parse(\'{{for x, y in z:}}{{continue}}{{endfor}}\')\n        [(\'for\', (1, 3), (\'x\', \'y\'), \'z\', [(\'continue\', (1, 21))])]\n        >>> parse(\'{{py:x=1}}\')\n        [(\'py\', (1, 3), \'x=1\')]\n        >>> parse(\'{{if x}}a{{elif y}}b{{else}}c{{endif}}\')\n        [(\'cond\', (1, 3), (\'if\', (1, 3), \'x\', [\'a\']), (\'elif\', (1, 12), \'y\', [\'b\']), (\'else\', (1, 23), None, [\'c\']))]\n\n    Some exceptions::\n\n        >>> parse(\'{{continue}}\')\n        Traceback (most recent call last):\n            ...\n        TemplateError: continue outside of for loop at line 1 column 3\n        >>> parse(\'{{if x}}foo\')\n        Traceback (most recent call last):\n            ...\n        TemplateError: No {{endif}} at line 1 column 3\n        >>> parse(\'{{else}}\')\n        Traceback (most recent call last):\n            ...\n        TemplateError: else outside of an if block at line 1 column 3\n        >>> parse(\'{{if x}}{{for x in y}}{{endif}}{{endfor}}\')\n        Traceback (most recent call last):\n            ...\n        TemplateError: Unexpected endif at line 1 column 25\n        >>> parse(\'{{if}}{{endif}}\')\n        Traceback (most recent call last):\n            ...\n        TemplateError: if with no expression at line 1 column 3\n        >>> parse(\'{{for x y}}{{endfor}}\')\n        Traceback (most recent call last):\n            ...\n        TemplateError: Bad for (no "in") in \'x y\' at line 1 column 3\n        >>> parse(\'{{py:x=1\\ny=2}}\')\n        Traceback (most recent call last):\n            ...\n        TemplateError: Multi-line py blocks must start with a newline at line 1 column 3\n    ' #NEWLINE# tokens = lex ( s , name = s ) #NEWLINE# result = [ ] #NEWLINE# while tokens : #NEWLINE# #INDENT# ( next , tokens ) = parse_expr ( tokens , name ) #NEWLINE# result . append ( next ) #NEWLINE# #UNINDENT# return result