<s> import os <EOL> import ply . lex as lex <EOL> import ply . yacc as yacc <EOL> import calvin_rules <EOL> from calvin_rules import tokens <EOL> class CalvinSyntaxError ( Exception ) : <EOL> def __init__ ( self , message , token ) : <EOL> super ( CalvinSyntaxError , self ) . __init__ ( message ) <EOL> self . token = token <EOL> class CalvinEOFError ( Exception ) : <EOL> def __init__ ( self , message ) : <EOL> super ( CalvinEOFError , self ) . __init__ ( message ) <EOL> def p_script ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = { '<STR_LIT>' : p [ <NUM_LIT:1> ] , '<STR_LIT>' : p [ <NUM_LIT:2> ] , '<STR_LIT>' : p [ <NUM_LIT:3> ] } <EOL> def p_constdefs ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:3> : <EOL> p [ <NUM_LIT:1> ] . update ( p [ <NUM_LIT:2> ] ) <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] if len ( p ) > <NUM_LIT:1> else { } <EOL> def p_constdef ( p ) : <EOL> """<STR_LIT>""" <EOL> constdef = { p [ <NUM_LIT:2> ] : p [ <NUM_LIT:4> ] } <EOL> p [ <NUM_LIT:0> ] = constdef <EOL> def p_compdefs ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:3> : <EOL> p [ <NUM_LIT:1> ] . update ( p [ <NUM_LIT:2> ] ) <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] if len ( p ) > <NUM_LIT:1> else { } <EOL> def p_compdef ( p ) : <EOL> """<STR_LIT>""" <EOL> name = p [ <NUM_LIT:2> ] <EOL> arg_ids = p [ <NUM_LIT:4> ] <EOL> inputs = p [ <NUM_LIT:6> ] <EOL> outputs = p [ <NUM_LIT:8> ] <EOL> docstring = p [ <NUM_LIT:10> ] <EOL> structure = p [ <NUM_LIT:11> ] <EOL> comp = { <EOL> '<STR_LIT:name>' : name , <EOL> '<STR_LIT>' : inputs , <EOL> '<STR_LIT>' : outputs , <EOL> '<STR_LIT>' : arg_ids , <EOL> '<STR_LIT>' : structure , <EOL> '<STR_LIT>' : docstring , <EOL> '<STR_LIT>' : p . lineno ( <NUM_LIT:2> ) <EOL> } <EOL> p [ <NUM_LIT:0> ] = { name : comp } <EOL> def p_docstring ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:1> : <EOL> p [ <NUM_LIT:0> ] = "<STR_LIT>" <EOL> else : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_opt_program ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:1> : <EOL> p [ <NUM_LIT:0> ] = { '<STR_LIT>' : [ ] , '<STR_LIT>' : { } } <EOL> else : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_program ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:3> : <EOL> kind , stmt = p [ <NUM_LIT:2> ] <EOL> if kind is '<STR_LIT>' : <EOL> p [ <NUM_LIT:1> ] [ '<STR_LIT>' ] . append ( stmt ) <EOL> else : <EOL> p [ <NUM_LIT:1> ] [ '<STR_LIT>' ] . update ( stmt ) <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> else : <EOL> kind , stmt = p [ <NUM_LIT:1> ] <EOL> if kind is '<STR_LIT>' : <EOL> p [ <NUM_LIT:0> ] = { '<STR_LIT>' : [ stmt ] , '<STR_LIT>' : { } } <EOL> else : <EOL> p [ <NUM_LIT:0> ] = { '<STR_LIT>' : [ ] , '<STR_LIT>' : stmt } <EOL> def p_statement ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_assignment ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = ( '<STR_LIT>' , { p [ <NUM_LIT:1> ] : { '<STR_LIT>' : p [ <NUM_LIT:3> ] , '<STR_LIT:args>' : p [ <NUM_LIT:5> ] , '<STR_LIT>' : p . lineno ( <NUM_LIT:2> ) } } ) <EOL> def p_link ( p ) : <EOL> """<STR_LIT>""" <EOL> kind , value = p [ <NUM_LIT:1> ] <EOL> ( src , port ) = value if kind == '<STR_LIT>' else ( None , ( kind , value ) ) <EOL> d = { } <EOL> d [ '<STR_LIT:src>' ] = src <EOL> d [ '<STR_LIT>' ] = port <EOL> _ , ( dst , port ) = p [ <NUM_LIT:3> ] <EOL> d [ '<STR_LIT>' ] = dst <EOL> d [ '<STR_LIT>' ] = port <EOL> d [ '<STR_LIT>' ] = p . lineno ( <NUM_LIT:2> ) <EOL> p [ <NUM_LIT:0> ] = ( '<STR_LIT>' , d ) <EOL> def p_port ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = ( '<STR_LIT>' , ( p [ <NUM_LIT:1> ] , p [ <NUM_LIT:2> ] if len ( p ) == <NUM_LIT:3> else p [ <NUM_LIT:3> ] ) ) <EOL> def p_named_args ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) > <NUM_LIT:2> : <EOL> p [ <NUM_LIT:1> ] . update ( p [ <NUM_LIT:2> ] ) <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] if len ( p ) > <NUM_LIT:1> else { } <EOL> def p_named_arg ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = { p [ <NUM_LIT:1> ] : p [ <NUM_LIT:3> ] } <EOL> def p_argument ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = ( p . slice [ <NUM_LIT:1> ] . type . upper ( ) , p [ <NUM_LIT:1> ] ) <EOL> def p_value ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_bool ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = bool ( p . slice [ <NUM_LIT:1> ] . type == '<STR_LIT>' ) <EOL> def p_null ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = None <EOL> def p_dictionary ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = dict ( p [ <NUM_LIT:2> ] ) <EOL> def p_members ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:1> : <EOL> p [ <NUM_LIT:0> ] = list ( ) <EOL> else : <EOL> p [ <NUM_LIT:1> ] . append ( p [ <NUM_LIT:2> ] ) <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_member ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = ( p [ <NUM_LIT:1> ] , p [ <NUM_LIT:3> ] ) <EOL> def p_values ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:1> : <EOL> p [ <NUM_LIT:0> ] = list ( ) <EOL> else : <EOL> p [ <NUM_LIT:1> ] . append ( p [ <NUM_LIT:2> ] ) <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_array ( p ) : <EOL> """<STR_LIT>""" <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:2> ] <EOL> def p_identifiers ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) > <NUM_LIT:2> : <EOL> p [ <NUM_LIT:1> ] . append ( p [ <NUM_LIT:2> ] ) <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] if len ( p ) > <NUM_LIT:1> else [ ] <EOL> def p_qualified_name ( p ) : <EOL> """<STR_LIT>""" <EOL> if len ( p ) == <NUM_LIT:4> : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] + p [ <NUM_LIT:2> ] + p [ <NUM_LIT:3> ] <EOL> else : <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_error ( p ) : <EOL> if not p : <EOL> raise CalvinEOFError ( "<STR_LIT>" ) <EOL> else : <EOL> raise CalvinSyntaxError ( "<STR_LIT>" , p ) <EOL> def _calvin_parser ( ) : <EOL> lexer = lex . lex ( module = calvin_rules ) <EOL> lexer . zerocol = <NUM_LIT:0> <EOL> this_file = os . path . realpath ( __file__ ) <EOL> containing_dir = os . path . dirname ( this_file ) <EOL> parser = yacc . yacc ( debug = False , optimize = True , outputdir = containing_dir ) <EOL> return parser <EOL> def _find_column ( input , token ) : <EOL> last_cr = input . rfind ( '<STR_LIT:\n>' , <NUM_LIT:0> , token . lexpos ) <EOL> if last_cr < <NUM_LIT:0> : <EOL> last_cr = <NUM_LIT:0> <EOL> column = ( token . lexpos - last_cr ) + <NUM_LIT:1> <EOL> return column <EOL> def calvin_parser ( source_text , source_file = '<STR_LIT>' ) : <EOL> parser = _calvin_parser ( ) <EOL> result = { } <EOL> errors = [ ] <EOL> try : <EOL> result = parser . parse ( source_text ) <EOL> except CalvinSyntaxError as e : <EOL> error = { <EOL> '<STR_LIT>' : str ( e ) , <EOL> '<STR_LIT>' : e . token . lexer . lineno , <EOL> '<STR_LIT>' : _find_column ( source_text , e . token ) <EOL> } <EOL> errors . append ( error ) <EOL> except CalvinEOFError as e : <EOL> lines = source_text . splitlines ( ) <EOL> error = { <EOL> '<STR_LIT>' : str ( e ) , <EOL> '<STR_LIT>' : len ( lines ) , <EOL> '<STR_LIT>' : len ( lines [ - <NUM_LIT:1> ] ) <EOL> } <EOL> errors . append ( error ) <EOL> result [ '<STR_LIT>' ] = source_file <EOL> warnings = [ ] <EOL> return result , errors , warnings <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> import json <EOL> if len ( sys . argv ) < <NUM_LIT:2> : <EOL> script = '<STR_LIT>' <EOL> source_text = '''<STR_LIT>''' <EOL> else : <EOL> script = sys . argv [ <NUM_LIT:1> ] <EOL> script = os . path . expanduser ( script ) <EOL> try : <EOL> with open ( script , '<STR_LIT:r>' ) as source : <EOL> source_text = source . read ( ) <EOL> except : <EOL> print "<STR_LIT>" % script <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> result , errors , warnings = calvin_parser ( source_text , script ) <EOL> if errors : <EOL> print "<STR_LIT>" . format ( script = script , ** errors [ <NUM_LIT:0> ] ) <EOL> else : <EOL> print ( json . dumps ( result , indent = <NUM_LIT:4> , sort_keys = True ) ) </s>
