<s> def data_type ( value ) : <EOL> if '<STR_LIT::>' in value : <EOL> return [ x . strip ( ) for x in value . rsplit ( '<STR_LIT::>' , <NUM_LIT:1> ) ] <EOL> return ( '<STR_LIT>' , value . strip ( ) ) <EOL> START = '<STR_LIT>' <EOL> END = '<STR_LIT>' <EOL> SEQUENCE = '<STR_LIT>' <EOL> MAPPING = '<STR_LIT>' <EOL> RENAME = '<STR_LIT>' <EOL> IGNORE = '<STR_LIT:ignore>' <EOL> TYPS = ( SEQUENCE , MAPPING , RENAME , IGNORE ) <EOL> def parse ( tokens ) : <EOL> """<STR_LIT>""" <EOL> target = typ = None <EOL> out = [ ] <EOL> stack = [ ] <EOL> for token in tokens : <EOL> key , val = token <EOL> if key == START : <EOL> stack . append ( ( target , typ , out ) ) <EOL> target , typ = data_type ( val ) <EOL> if typ in TYPS : <EOL> out = [ ] <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % repr ( token ) ) <EOL> elif key == END : <EOL> if typ == SEQUENCE : <EOL> parsed = [ v for ( k , v ) in out ] <EOL> elif typ == MAPPING : <EOL> parsed = dict ( out ) <EOL> elif typ == RENAME : <EOL> parsed = out [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] if out else '<STR_LIT>' <EOL> elif typ == IGNORE : <EOL> parsed = None <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> prev_target , prev_typ , out = stack . pop ( ) <EOL> if parsed is not None : <EOL> out . append ( ( target , parsed ) ) <EOL> target = prev_target <EOL> typ = prev_typ <EOL> else : <EOL> out . append ( token ) <EOL> if stack : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return dict ( out ) </s>
