<s> import ast , _ast , sys , collections <EOL> sys . path . append ( '<STR_LIT:..>' ) <EOL> from runac import parser <EOL> TITLE = '<STR_LIT>' <EOL> PARSER_FILE = '<STR_LIT>' <EOL> INTRO = '''<STR_LIT>''' <EOL> def get_rules ( ) : <EOL> with open ( PARSER_FILE ) as f : <EOL> src = f . read ( ) <EOL> rules = collections . OrderedDict ( ) <EOL> for node in ast . parse ( src ) . body : <EOL> if not isinstance ( node , _ast . FunctionDef ) : <EOL> continue <EOL> if not node . decorator_list : <EOL> continue <EOL> assert len ( node . decorator_list ) == <NUM_LIT:1> <EOL> decorator = node . decorator_list [ <NUM_LIT:0> ] <EOL> if not isinstance ( decorator , _ast . Call ) : <EOL> continue <EOL> func = decorator . func <EOL> if not isinstance ( func , _ast . Attribute ) : <EOL> continue <EOL> assert func . attr == '<STR_LIT>' <EOL> ln = decorator . args [ <NUM_LIT:0> ] . s <EOL> name , match = ln . split ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> rules . setdefault ( name , [ ] ) . append ( tuple ( match . split ( ) ) ) <EOL> return rules <EOL> def get_tokens ( ) : <EOL> tokens = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> for rule in parser . LEXER . rules : <EOL> name , pattern = rule . name , rule . re . pattern <EOL> tokens [ rule . name ] = rule . re . pattern <EOL> for word in parser . NAME_LIKE : <EOL> tokens [ word . upper ( ) ] = word <EOL> return tokens <EOL> def main ( ) : <EOL> rules , tokens = get_rules ( ) , get_tokens ( ) <EOL> lines , columns = [ ] , [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> for name , expands in rules . iteritems ( ) : <EOL> columns [ <NUM_LIT:0> ] = max ( columns [ <NUM_LIT:0> ] , len ( name ) ) <EOL> for i , expand in enumerate ( expands ) : <EOL> bits = list ( expand ) <EOL> for idx , s in enumerate ( expand ) : <EOL> if s . upper ( ) == s : <EOL> bits [ idx ] = '<STR_LIT>' + tokens [ s ] + '<STR_LIT>' <EOL> defn = '<STR_LIT:U+0020>' . join ( bits ) <EOL> columns [ <NUM_LIT:1> ] = max ( columns [ <NUM_LIT:1> ] , len ( defn ) ) <EOL> lines . append ( ( name if not i else '<STR_LIT>' , defn ) ) <EOL> separator = '<STR_LIT>' . join ( ( <EOL> '<STR_LIT:+>' , <EOL> '<STR_LIT:->' * ( columns [ <NUM_LIT:0> ] + <NUM_LIT:2> ) , <EOL> '<STR_LIT:+>' , <EOL> '<STR_LIT:->' * ( columns [ <NUM_LIT:1> ] + <NUM_LIT:2> ) , <EOL> '<STR_LIT:+>' , <EOL> ) ) <EOL> fmt = '<STR_LIT>' % tuple ( columns ) <EOL> print '<STR_LIT:*>' * len ( TITLE ) <EOL> print TITLE <EOL> print '<STR_LIT:*>' * len ( TITLE ) <EOL> print <EOL> print INTRO <EOL> print <EOL> for i , ln in enumerate ( lines ) : <EOL> print separator <EOL> print fmt % ln <EOL> print separator <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
