def decoStmt(self, state, node, name, args): atLabel = label( '@' ) atUnparsed = UnparsedText( '@' ) atUnparsed.associateWith( atLabel ) nameLabel = label( name ) nameUnparsed = UnparsedText( name ) nameUnparsed.associateWith( nameLabel ) if args != '<nil>': argViews = mapViewEval( args, None, python25ViewState( Parser.callArg ) ) argWidgets = [ label( '(', punctuationStyle ) ] if len( args ) > 0: for a in argViews[:-1]: argWidgets.append( ahbox( [ a, label( ',', punctuationStyle ) ] ) ) argWidgets.append( argViews[-1] ) argWidgets.append( label( ')', punctuationStyle ) ) argsUnparsed = '( ' + UnparsedText( ', ' ).join( [ a.text for a in argViews ] ) + ' )' else: argWidgets = [] argsUnparsed = '' return nodeEditor( node, ahbox( [ atLabel, nameLabel ] + argWidgets ), UnparsedText( atUnparsed + nameUnparsed + argsUnparsed, PRECEDENCE_STMT ), state )
def conditionalExpr(self, state, node, condition, expr, elseExpr): conditionView = viewEval( condition, None, python25ViewState( Parser.orTest ) ) exprView = viewEval( expr, None, python25ViewState( Parser.orTest ) ) elseExprView = viewEval( elseExpr, None, python25ViewState( Parser.expression ) ) return nodeEditor( node, ahbox( [ exprView, ahbox( [ keywordLabel( ifKeyword ), conditionView ] ), ahbox( [ keywordLabel( elseKeyword ), elseExprView ] ) ], spacing=15.0 ), UnparsedText( exprView.text + ' ' + ifKeyword + ' ' + conditionView.text + ' ' + elseKeyword + ' ' + elseExprView.text, PRECEDENCE_CONDITIONALEXPRESSION ), state )
def keyValuePair(self, state, node, key, value): keyView = viewEval( key ) valueView = viewEval( value ) return nodeEditor( node, ahbox( [ keyView, label( ':', punctuationStyle ), valueView ] ), UnparsedText( keyView.text + ':' + valueView.text, PRECEDENCE_DICTLITERAL ), state )
def horizontalPrefixOpView(state, node, x, op, precedence): xView = viewEval( x ) unparsed = _unparsePrefixOpView( xView.text, op, precedence ) return nodeEditor( node, ahbox( [ label( op, operatorStyle ), xView ] ), unparsed, state )
def classStmt(self, state, node, name, inheritance, suite): classLabel = keywordLabel( classKeyword ) classUnparsed = UnparsedText( classKeyword ) classUnparsed.associateWith( classLabel ) nameLabel = label( name ) nameUnparsed = UnparsedText( name ) nameUnparsed.associateWith( nameLabel ) if inheritance != '<nil>': inheritanceView = viewEval( inheritance, None, python25ViewState( Parser.tupleOrExpression ) ) inhWidget = ahbox( [ label( '(', punctuationStyle ), inheritanceView, label( ')', punctuationStyle ) ] ) inhUnparsed = UnparsedText( '(' ) + inheritanceView.text + ')' classLine = flow( [ classLabel, nameLabel, inhWidget, label( ':', punctuationStyle ) ], spacing=10.0 ) classLineUnparsed = classUnparsed + ' ' + nameUnparsed + inhUnparsed + ':' else: inhWidgets = [] classLine = flow( [ classLabel, nameLabel, label( ':', punctuationStyle ) ], spacing=10.0 ) classLineUnparsed = classUnparsed + ' ' + nameUnparsed + ':' return compoundStatementEditor( node, classLine, UnparsedText( classLineUnparsed, PRECEDENCE_STMT ), suite, state )
def intLiteral(self, state, node, format, numType, value): boxContents = [] if numType == 'int': if format == 'decimal': unparsed = '%d' % int( value ) elif format == 'hex': unparsed = '%x' % int( value ) valueLabel = label( unparsed, numericLiteralStyle ) boxContents.append( valueLabel ) elif numType == 'long': if format == 'decimal': unparsed = '%dL' % long( value ) elif format == 'hex': unparsed = '%xL' % long( value ) valueLabel = label( unparsed[:-1], numericLiteralStyle ) boxContents.append( valueLabel ) boxContents.append( label( 'L', literalFormatStyle ) ) valueUnparsed = UnparsedText( unparsed ) valueUnparsed.associateWith( valueLabel ) return nodeEditor( node, ahbox( boxContents ), valueUnparsed, state )
def stringLiteral(self, state, node, format, quotation, value): valueUnparsed = UnparsedText( repr( value ) ) valueLabel = label( value ) valueUnparsed.associateWith( valueLabel ) boxContents = [] if format == 'ascii': pass elif format == 'unicode': boxContents.append( label( 'u', literalFormatStyle ) ) elif format == 'ascii-regex': boxContents.append( label( 'r', literalFormatStyle ) ) elif format == 'unicode-regex': boxContents.append( label( 'ur', literalFormatStyle ) ) else: raise ValueError, 'invalid string literal format' if quotation == 'single': boxContents.append( label( "'", punctuationStyle ) ) boxContents.append( None ) boxContents.append( label( "'", punctuationStyle ) ) else: boxContents.append( label( '"', punctuationStyle ) ) boxContents.append( None ) boxContents.append( label( '"', punctuationStyle ) ) boxContents[-2] = valueLabel return nodeEditor( node, ahbox( boxContents ), valueUnparsed, state )
def horizontalBinOpView(state, node, x, y, op, precedence): xView = viewEval( x ) yView = viewEval( y ) unparsed = _unparseBinOpView( xView.text, yView.text, op, precedence ) return nodeEditor( node, ahbox( [ xView, label( op, operatorStyle ), yView ] ), unparsed, state )
def kwParamList(self, state, node, name): nameLabel = label( name ) nameUnparsed = UnparsedText( name ) nameUnparsed.associateWith( nameLabel ) return nodeEditor( node, ahbox( [ label( '**', punctuationStyle ), nameLabel ] ), UnparsedText( '**' + nameUnparsed, PRECEDENCE_PARAM ), state )
def defaultValueParam(self, state, node, name, value): nameLabel = label( name ) nameUnparsed = UnparsedText( name ) nameUnparsed.associateWith( nameLabel ) valueView = viewEval( value ) return nodeEditor( node, ahbox( [ nameLabel, label( '=', punctuationStyle ), valueView ] ), UnparsedText( nameUnparsed + '=' + valueView.text, PRECEDENCE_PARAM ), state )
def finallyStmt(self, state, node, suite): finallyLabel = keywordLabel( finallyKeyword ) finallyUnparsed = UnparsedText( finallyKeyword ) finallyUnparsed.associateWith( finallyLabel ) return compoundStatementEditor( node, ahbox( [ finallyLabel, label( ':', punctuationStyle ) ] ), UnparsedText( finallyUnparsed + ':', PRECEDENCE_STMT ), suite, state )
def elseStmt(self, state, node, suite): elseLabel = keywordLabel( elseKeyword ) elseUnparsed = UnparsedText( elseKeyword ) elseUnparsed.associateWith( elseLabel ) return compoundStatementEditor( node, ahbox( [ elseLabel, label( ':', punctuationStyle ) ] ), UnparsedText( elseUnparsed + ':', PRECEDENCE_STMT ), suite, state )
def whileStmt(self, state, node, condition, suite): whileLabel = keywordLabel( whileKeyword ) whileUnparsed = UnparsedText( whileKeyword ) whileUnparsed.associateWith( whileLabel ) conditionView = viewEval( condition ) return compoundStatementEditor( node, flow( [ whileLabel, ahbox( [ conditionView, label( ':', punctuationStyle ) ] ) ] ), UnparsedText( whileUnparsed + ' ' + conditionView.text + ':', PRECEDENCE_STMT ), suite, state )
def assignmentStmt(self, state, node, targets, value): targetViews = mapViewEval( targets, None, python25ViewState( Parser.targetList ) ) valueView = viewEval( value, None, python25ViewState( Parser.tupleOrExpressionOrYieldExpression ) ) targetWidgets = [] for t in targetViews: targetWidgets.append( ahbox( [ t, label( '=', punctuationStyle ) ] ) ) return nodeEditor( node, flow( targetWidgets + [ valueView ] ), UnparsedText( UnparsedText( ' = ' ).join( [ t.text for t in targetViews ] ) + ' = ' + valueView.text, PRECEDENCE_STMT ), state )
def forStmt(self, state, node, target, source, suite): forLabel = keywordLabel( forKeyword ) forUnparsed = UnparsedText( forKeyword ) forUnparsed.associateWith( forLabel ) targetView = viewEval( target, None, python25ViewState( Parser.targetList ) ) sourceView = viewEval( source, None, python25ViewState( Parser.tupleOrExpression ) ) return compoundStatementEditor( node, flow( [ forLabel, targetView, keywordLabel( inKeyword ), ahbox( [ sourceView, label( ':', punctuationStyle ) ] ) ] ), UnparsedText( forUnparsed + ' ' + targetView.text + ' ' + inKeyword + sourceView.text + ':', PRECEDENCE_LISTCOMPREHENSION ), suite, state )
def moduleContentImportAs(self, state, node, name, asName): nameLabel = label( name ) nameUnparsed = UnparsedText( name ) nameUnparsed.associateWith( nameLabel ) asNameLabel = label( asName ) asNameUnparsed = UnparsedText( asName ) asNameUnparsed.associateWith( asNameLabel ) return nodeEditor( node, ahbox( [ nameLabel, keywordLabel( asKeyword ), asNameLabel ] ), UnparsedText( nameUnparsed + ' ' + asKeyword + ' ' + asNameUnparsed, PRECEDENCE_IMPORTCONTENT ), state )
def subscript(self, state, node, target, index): targetView = viewEval( target ) indexView = viewEval( index, None, python25ViewState( Parser.subscriptIndex ) ) #return nodeEditor( node, #scriptRSub( targetView, ahbox( [ label( '[', punctuationStyle ), indexView, label( ']', punctuationStyle ) ] ) ), #UnparsedText( _unparsePrecedenceGT( targetView.text, PRECEDENCE_SUBSCRIPT ) + '[' + indexView.text + ']', PRECEDENCE_SUBSCRIPT ), #state ) return nodeEditor( node, ahbox( [ targetView, label( '[', punctuationStyle ), indexView, label( ']', punctuationStyle ) ] ), UnparsedText( _unparsePrecedenceGT( targetView.text, PRECEDENCE_SUBSCRIPT ) + '[' + indexView.text + ']', PRECEDENCE_SUBSCRIPT ), state )
def importStmt(self, state, node, *xs): xViews = mapViewEval( xs, None, python25ViewState( Parser.moduleImport ) ) xWidgets = [] if len( xs ) > 0: for xv in xViews[:-1]: xWidgets.append( ahbox( [ xv, label( ',', punctuationStyle ) ] ) ) xWidgets.append( xViews[-1] ) return nodeEditor( node, flow( [ keywordLabel( importKeyword ) ] + xWidgets, spacing=10.0 ), UnparsedText( importKeyword + ' ' + UnparsedText( ', ' ).join( [ xv.text for xv in xViews ] ), PRECEDENCE_STMT ), state )
def call(self, state, node, target, *args): targetView = viewEval( target ) argViews = mapViewEval( args, None, python25ViewState( Parser.callArg ) ) argWidgets = [] if len( args ) > 0: for a in argViews[:-1]: argWidgets.append( ahbox( [ a, label( ',', punctuationStyle ) ] ) ) argWidgets.append( argViews[-1] ) return nodeEditor( node, flow( [ viewEval( target ), label( '(', punctuationStyle ) ] + argWidgets + [ label( ')', punctuationStyle ) ] ), UnparsedText( _unparsePrecedenceGT( targetView.text, PRECEDENCE_CALL ) + '( ' + UnparsedText( ', ' ).join( [ a.text for a in argViews ] ) + ' )', PRECEDENCE_CALL ), state )
def globalStmt(self, state, node, *xs): globalLabel = keywordLabel( globalKeyword ) globalUnparsed = UnparsedText( globalKeyword ) globalUnparsed.associateWith( globalLabel ) xViews = mapViewEval( xs, None, python25ViewState( Parser.globalVar ) ) xWidgets = [] if len( xs ) > 0: for xv in xViews[:-1]: xWidgets.append( ahbox( [ xv, label( ',', punctuationStyle ) ] ) ) xWidgets.append( xViews[-1] ) return nodeEditor( node, flow( [ globalLabel ] + xWidgets, spacing=10.0 ), UnparsedText( globalUnparsed + ' ' + UnparsedText( ', ' ).join( [ xv.text for xv in xViews ] ), PRECEDENCE_STMT ), state )
def raiseStmt(self, state, node, *xs): xs = [ x for x in xs if x != '<nil>' ] xViews = mapViewEval( xs ) xWidgets = [] if len( xs ) > 0: for x in xViews[:-1]: xWidgets.append( ahbox( [ x, label( ',', punctuationStyle ) ] ) ) xWidgets.append( xViews[-1] ) xText = UnparsedText( ', ' ).join( [ x.text for x in xViews ] ) if len( xs ) > 0: xText = ' ' + xText return nodeEditor( node, flow( [ keywordLabel( raiseKeyword ) ] + xWidgets, spacing=10.0 ), UnparsedText( UnparsedText( raiseKeyword ) + xText, PRECEDENCE_STMT ), state )
def subscriptSlice(self, state, node, x, y): widgets = [] def _v(n): if n != '<nil>': nView = viewEval( n ) widgets.append( nView ) return nView, nView.text else: return None, '' xView, xText = _v( x ) widgets.append( label( ':', punctuationStyle ) ) yView, yText = _v( y ) return nodeEditor( node, ahbox( widgets ), UnparsedText( xText + ':' + yText, PRECEDENCE_SUBSCRIPTSLICE ), state )
def assertStmt(self, state, node, condition, fail): assertLabel = keywordLabel( assertKeyword ) assertUnparsed = UnparsedText( assertKeyword ) assertUnparsed.associateWith( assertLabel ) conditionView = viewEval( condition ) widgets = [ assertLabel ] if fail != '<nil>': failView = viewEval( fail ) widgets.append( ahbox( [ conditionView, label( ',', punctuationStyle ) ] ) ) widgets.append( viewEval( fail ) ) else: failView = None widgets.append( conditionView ) return nodeEditor( node, flow( widgets, spacing=10.0 ), UnparsedText( assertUnparsed + ' ' + conditionView.text + ( ', ' + failView.text ) if failView is not None else '', PRECEDENCE_STMT ), state )
def lambdaExpr(self, state, node, params, expr): # The Python 2.5 grammar has two versions of the lambda expression grammar; one what reckognises the full lambda expression, and one that # reckognises a lambda expression that cannot wrap conditional expression. # Ensure that we use the correct parser for @expr exprParser = Parser.expression if state is not None: parser, mode = state if parser is Parser.oldExpression or parser is Parser.oldLambdaExpr or parser is Parser.oldTupleOrExpression: exprParser = Parser.oldExpression exprView = viewEval( expr, None, python25ViewState( exprParser ) ) paramViews = mapViewEval( params, None, python25ViewState( Parser.param ) ) paramWidgets = [] if len( params ) > 0: for p in paramViews[:-1]: paramWidgets.append( ahbox( [ p, label( ',', punctuationStyle ) ] ) ) paramWidgets.append( paramViews[-1] ) return nodeEditor( node, flow( [ keywordLabel( lambdaKeyword ) ] + paramWidgets + [ label( ':', punctuationStyle ), exprView ], spacing=5.0 ), UnparsedText( lambdaKeyword + ' ' + UnparsedText( ', ' ).join( [ p.text for p in paramViews ] ) + ': ' + exprView.text, PRECEDENCE_LAMBDAEXPR ), state )
def defStmt(self, state, node, name, params, suite): defLabel = keywordLabel( defKeyword ) defUnparsed = UnparsedText( defKeyword ) defUnparsed.associateWith( defLabel ) nameLabel = label( name ) nameUnparsed = UnparsedText( name ) nameUnparsed.associateWith( nameLabel ) paramViews = mapViewEval( params, None, python25ViewState( Parser.param ) ) paramWidgets = [ label( '(', punctuationStyle ) ] if len( params ) > 0: for p in paramViews[:-1]: paramWidgets.append( ahbox( [ p, label( ',', punctuationStyle ) ] ) ) paramWidgets.append( paramViews[-1] ) paramWidgets.append( label( ')', punctuationStyle ) ) return compoundStatementEditor( node, flow( [ defLabel, nameLabel ] + paramWidgets + [ label( ':', punctuationStyle ) ], spacing=10.0 ), UnparsedText( defUnparsed + ' ' + nameUnparsed + '(' + UnparsedText( ', ' ).join( [ p.text for p in paramViews ] ) + '):', PRECEDENCE_STMT ), suite, state )
def execStmt(self, state, node, src, loc, glob): execLabel = keywordLabel( execKeyword ) execUnparsed = UnparsedText( execKeyword ) execUnparsed.associateWith( execLabel ) srcView = viewEval( src, None, python25ViewState( Parser.orOp ) ) widgets = [ srcView ] txt = execUnparsed + ' ' + srcView.text if loc != '<nil>': locView = viewEval( loc ) widgets.append( keywordLabel( inKeyword ) ) if glob != '<nil>': widgets.append( ahbox( [ locView, label( ',', punctuationStyle ) ] ) ) else: widgets.append( locView ) txt += ' ' + inKeyword + ' ' + locView.text if glob != '<nil>': globView = viewEval( glob ) widgets.append( globView ) txt += ', ' + globView.text return nodeEditor( node, flow( [ execLabel ] + widgets, spacing=10.0 ), UnparsedText( txt , PRECEDENCE_STMT ), state )
def exceptStmt(self, state, node, exc, target, suite): exceptLabel = keywordLabel( exceptKeyword ) exceptUnparsed = UnparsedText( exceptKeyword ) exceptUnparsed.associateWith( exceptLabel ) widgets = [] txt = exceptUnparsed if exc != '<nil>': excView = viewEval( exc ) if target != '<nil>': widgets.append( ahbox( [ excView, label( ',', punctuationStyle ) ] ) ) else: widgets.append( excView ) txt += ' ' + excView.text if target != '<nil>': targetView = viewEval( target ) widgets.append( targetView ) txt += ', ' + targetView.text widgets.append( label( ':', punctuationStyle ) ) return compoundStatementEditor( node, flow( [ exceptLabel ] + widgets, spacing=10.0 ), UnparsedText( txt + ':', PRECEDENCE_STMT ), suite, state )
def tupleWidget(x): if x.text.state == PRECEDENCE_TUPLE: return ahbox( [ label( '(', punctuationStyle ), x, label( ')', punctuationStyle ) ] ) else: return x
def kwArgList(self, state, node, value): valueView = viewEval( value ) return nodeEditor( node, ahbox( [ label( '**', punctuationStyle ), valueView ] ), UnparsedText( '**' + valueView.text, PRECEDENCE_ARG ), state )