def whileStmt(self, ctx, state, node, condition, suite): conditionView = viewEval( ctx, condition ) return compoundStatementEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ keywordText( ctx, whileKeyword ), text( ctx, default_textStyle, ' ' ), conditionView, text( ctx, punctuation_textStyle, ':' ) ] ), PRECEDENCE_STMT, suite, state )
def listFor(self, ctx, state, node, target, source): targetView = viewEval( ctx, target, None, python25ViewState( Parser.targetList ) ) sourceView = viewEval( ctx, source, None, python25ViewState( Parser.oldTupleOrExpression ) ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ keywordText( ctx, forKeyword ), text( ctx, default_textStyle, ' ' ), targetView, text( ctx, default_textStyle, ' ' ), keywordText( ctx, inKeyword ), text( ctx, default_textStyle, ' ' ), sourceView ] ), PRECEDENCE_LISTCOMPREHENSION, state )
def keyValuePair(self, ctx, state, node, key, value): keyView = viewEval( ctx, key ) valueView = viewEval( ctx, value ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ keyView, text( ctx, punctuation_textStyle, ' : ' ), valueView ] ), None, state )
def subscriptSlice(self, ctx, state, node, x, y): xView = viewEval( ctx, x ) yView = viewEval( ctx, y ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ xView, text( ctx, punctuation_textStyle, ':' ), yView ] ), PRECEDENCE_SUBSCRIPTSLICE, state )
def augAssignStmt(self, ctx, state, node, op, target, value): targetView = viewEval( ctx, target, None, python25ViewState( Parser.targetItem ) ) valueView = viewEval( ctx, value, None, python25ViewState( Parser.tupleOrExpressionOrYieldExpression ) ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ targetView, text( ctx, punctuation_textStyle, ' ' + op + ' ' ), valueView ] ), PRECEDENCE_STMT, state )
def subscript(self, ctx, state, node, target, index): targetView = viewEval( ctx, target ) indexView = viewEval( ctx, index, None, python25ViewState( Parser.subscriptIndex ) ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ targetView, text( ctx, punctuation_textStyle, '[' ), indexView, text( ctx, punctuation_textStyle, ']' ) ] ), PRECEDENCE_SUBSCRIPT, state )
def fromImportAllStmt(self, ctx, state, node, moduleName): moduleNameView = viewEval( ctx, moduleName, None, python25ViewState( Parser.moduleContentImport ) ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ keywordText( ctx, fromKeyword ), text( ctx, default_textStyle, ' ' ), moduleNameView, text( ctx, default_textStyle, ' ' ), keywordText( ctx, importKeyword ), text( ctx, default_textStyle, ' ' ), text( ctx, punctuation_textStyle, '*' ) ] ), PRECEDENCE_STMT, state )
def genFor(self, ctx, state, node, target, source): targetView = viewEval( ctx, target, None, python25ViewState( Parser.targetList ) ) sourceView = viewEval( ctx, source, None, python25ViewState( Parser.orTest ) ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ keywordText( ctx, forKeyword ), text( ctx, default_textStyle, ' ' ), targetView, text( ctx, default_textStyle, ' ' ), keywordText( ctx, inKeyword ), text( ctx, default_textStyle, ' ' ), sourceView ] ), PRECEDENCE_GENERATOREXPRESSION, state )
def stringLiteral(self, ctx, state, node, format, quotation, value): boxContents = [] if format == 'ascii': pass elif format == 'unicode': boxContents.append( text( ctx, literalFormat_textStyle, 'u' ) ) elif format == 'ascii-regex': boxContents.append( text( ctx, literalFormat_textStyle, 'r' ) ) elif format == 'unicode-regex': boxContents.append( text( ctx, literalFormat_textStyle, 'ur' ) ) else: raise ValueError, 'invalid string literal format' if quotation == 'single': boxContents.append( text( ctx, punctuation_textStyle, "'" ) ) boxContents.append( None ) boxContents.append( text( ctx, punctuation_textStyle, "'" ) ) else: boxContents.append( text( ctx, punctuation_textStyle, '"' ) ) boxContents.append( None ) boxContents.append( text( ctx, punctuation_textStyle, '"' ) ) boxContents[-2] = text( ctx, default_textStyle, value ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, boxContents ), PRECEDENCE_LITERALVALUE, state )
def paragraphBinOpElement(ctx, x, y, op, precedence, bRightAssociative=False): if bRightAssociative: x = _precedenceGTE( ctx, x, precedence ) y = _precedenceGT( ctx, y, precedence ) else: x = _precedenceGT( ctx, x, precedence ) y = _precedenceGTE( ctx, y, precedence ) return paragraph( ctx, python_paragraphStyle, [ x, text( ctx, default_textStyle, ' ' ), op, text( ctx, default_textStyle, ' ' ), y ] )
def forStmt(self, ctx, state, node, target, source, suite): targetView = viewEval( ctx, target, None, python25ViewState( Parser.targetList ) ) sourceView = viewEval( ctx, source, None, python25ViewState( Parser.tupleOrExpression ) ) return compoundStatementEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ keywordText( ctx, forKeyword ), text( ctx, default_textStyle, ' ' ), targetView, text( ctx, default_textStyle, ' ' ), keywordText( ctx, inKeyword ), text( ctx, default_textStyle, ' ' ), sourceView, text( ctx, punctuation_textStyle, ':' ) ] ), PRECEDENCE_STMT, suite, state )
def assignmentStmt(self, ctx, state, node, targets, value): targetViews = mapViewEval( ctx, targets, None, python25ViewState( Parser.targetList ) ) valueView = viewEval( ctx, value, None, python25ViewState( Parser.tupleOrExpressionOrYieldExpression ) ) targetElements = [] for t in targetViews: targetElements.extend( [ t, text( ctx, punctuation_textStyle, ' = ' ) ] ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, targetElements + [ valueView ] ), PRECEDENCE_STMT, state )
def conditionalExpr(self, ctx, state, node, condition, expr, elseExpr): conditionView = viewEval( ctx, condition, None, python25ViewState( Parser.orTest ) ) exprView = viewEval( ctx, expr, None, python25ViewState( Parser.orTest ) ) elseExprView = viewEval( ctx, elseExpr, None, python25ViewState( Parser.expression ) ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ exprView, whitespace( ctx, ' ', 15.0 ), keywordText( ctx, ifKeyword ), text( ctx, default_textStyle, ' ' ), conditionView, whitespace( ctx, ' ', 15.0 ), keywordText( ctx, elseKeyword ), text( ctx, default_textStyle, ' ' ), elseExprView ] ), PRECEDENCE_CONDITIONALEXPRESSION, state )
def assertStmt(self, ctx, state, node, condition, fail): conditionView = viewEval( ctx, condition ) elements = [ keywordText( ctx, assertKeyword ), text( ctx, default_textStyle, ' ' ), conditionView ] if fail != '<nil>': failView = viewEval( ctx, fail ) elements.extend( [ text( ctx, punctuation_textStyle, ', ' ), failView ] ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, elements ), PRECEDENCE_STMT, state )
def globalStmt(self, ctx, state, node, *xs): xViews = mapViewEval( ctx, xs, None, python25ViewState( Parser.globalVar ) ) xElements = [] if len( xs ) > 0: for xv in xViews[:-1]: xElements.extend( [ xv, text( ctx, punctuation_textStyle, ', ' ) ] ) xElements.append( xViews[-1] ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ keywordText( ctx, globalKeyword ), text( ctx, default_textStyle, ' ' ) ] + xElements ), PRECEDENCE_STMT, state )
def withStmt(self, ctx, state, node, expr, target, suite): exprView = viewEval( ctx, expr ) elements = [ exprView ] if target != '<nil>': targetView = viewEval( ctx, target ) elements.extend( [ text( ctx, default_textStyle, ' ' ), keywordText( ctx, asKeyword ), text( ctx, default_textStyle, ' ' ), targetView ] ) elements.append( text( ctx, punctuation_textStyle, ':' ) ) return compoundStatementEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ keywordText( ctx, withKeyword ), text( ctx, default_textStyle, ' ' ) ] + elements ), PRECEDENCE_STMT, suite, state )
def raiseStmt(self, ctx, state, node, *xs): xs = [ x for x in xs if x != '<nil>' ] xViews = mapViewEval( ctx, xs ) xElements = [] if len( xs ) > 0: for x in xViews[:-1]: xElements.extend( [ x, text( ctx, punctuation_textStyle, ', ' ) ] ) xElements.append( xViews[-1] ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ keywordText( ctx, raiseKeyword ), text( ctx, default_textStyle, ' ' ) ] + xElements ), PRECEDENCE_STMT, state )
def compoundStatementEditor(ctx, node, headerContents, metadata, suite, state): if state is None: parser = Parser.statement mode = MODE_STATEMENT else: parser, mode = state headerParagraph = paragraph( ctx, python_paragraphStyle, [ headerContents, whitespace( ctx, '\n' ) ] ) headerElement = contentListener( ctx, headerParagraph, ParsedLineContentListener( ctx, node, parser ) ) statementElement = vbox( ctx, compoundStmt_vboxStyle, [ headerElement, indent( ctx, 30.0, suiteView( ctx, suite ) ) ] ) return statementElement, metadata
def call(self, ctx, state, node, target, *args): targetView = viewEval( ctx, target ) argViews = mapViewEval( ctx, args, None, python25ViewState( Parser.callArg ) ) argElements = [] if len( args ) > 0: for a in argViews[:-1]: argElements.append( a ) argElements.append( text( ctx, punctuation_textStyle, ', ' ) ) argElements.append( argViews[-1] ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ targetView, text( ctx, punctuation_textStyle, '(' ) ] + argElements + [ text( ctx, punctuation_textStyle, ')' ) ] ), PRECEDENCE_CALL, state )
def classStmt(self, ctx, state, node, name, inheritance, suite): if inheritance != '<nil>': inheritanceView = viewEval( ctx, inheritance, None, python25ViewState( Parser.tupleOrExpression ) ) inhElements = [ text( ctx, punctuation_textStyle, '(' ), inheritanceView, text( ctx, punctuation_textStyle, ')' ) ] else: inhElements = [] return compoundStatementEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ keywordText( ctx, classKeyword ), text( ctx, default_textStyle, ' ' ), text( ctx, default_textStyle, name ) ] + \ inhElements + [ text( ctx, punctuation_textStyle, ':' ) ] ), PRECEDENCE_STMT, suite, state )
def listComprehension(self, ctx, state, node, expr, *xs): exprView = viewEval( ctx, expr ) xViews = mapViewEval( ctx, xs, None, python25ViewState( Parser.listComprehensionItem ) ) xViewsSpaced = [] if len( xViews ) > 0: for x in xViews[:-1]: xViewsSpaced.append( x ) xViewsSpaced.append( whitespace( ctx, ' ', 15.0 ) ) xViewsSpaced.append( xViews[-1] ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ text( ctx, punctuation_textStyle, '[' ), exprView, whitespace( ctx, ' ', 15.0 ) ] + xViewsSpaced + [ text( ctx, punctuation_textStyle, ']' ) ] ), PRECEDENCE_LISTCOMPREHENSION, state )
def generatorExpression(self, ctx, state, node, expr, *xs): exprView = viewEval( ctx, expr ) xViews = mapViewEval( ctx, xs, None, python25ViewState( Parser.generatorExpressionItem ) ) xViewsSpaced = [] if len( xViews ) > 0: for x in xViews[:-1]: xViewsSpaced.append( x ) xViewsSpaced.append( whitespace( ctx, ' ', 15.0 ) ) xViewsSpaced.append( xViews[-1] ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ text( ctx, punctuation_textStyle, '(' ), exprView, whitespace( ctx, ' ', 15.0 ) ] + xViewsSpaced + [ text( ctx, punctuation_textStyle, ')' ) ] ), PRECEDENCE_GENERATOREXPRESSION, state )
def execStmt(self, ctx, state, node, src, loc, glob): srcView = viewEval( ctx, src, None, python25ViewState( Parser.orOp ) ) elements = [ srcView ] if loc != '<nil>': locView = viewEval( ctx, loc ) elements.extend( [ text( ctx, default_textStyle, ' ' ), keywordText( ctx, inKeyword ), text( ctx, default_textStyle, ' ' ), locView ] ) if glob != '<nil>': globView = viewEval( ctx, glob ) elements.extend( [ text( ctx, default_textStyle, ', ' ), globView ] ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ keywordText( ctx, execKeyword ), text( ctx, default_textStyle, ' ' ) ] + elements ), PRECEDENCE_STMT, state )
def fromImportStmt(self, ctx, state, node, moduleName, *xs): moduleNameView = viewEval( ctx, moduleName, None, python25ViewState( Parser.moduleContentImport ) ) xViews = mapViewEval( ctx, xs, None, python25ViewState( Parser.moduleImport ) ) xElements = [] if len( xs ) > 0: for xv in xViews[:-1]: xElements.extend( [ xv, text( ctx, punctuation_textStyle, ', ' ) ] ) xElements.append( xViews[-1] ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ keywordText( ctx, fromKeyword ), text( ctx, default_textStyle, ' ' ), moduleNameView, text( ctx, default_textStyle, ' ' ), keywordText( ctx, importKeyword ), text( ctx, default_textStyle, ' ' ) ] + xElements ), PRECEDENCE_STMT, state )
def exceptStmt(self, ctx, state, node, exc, target, suite): elements = [] if exc != '<nil>': excView = viewEval( ctx, exc ) elements.extend( [ text( ctx, default_textStyle, ' ' ), excView ] ) if target != '<nil>': targetView = viewEval( ctx, target ) elements.extend( [ text( ctx, default_textStyle, ', ' ), targetView ] ) elements.append( text( ctx, punctuation_textStyle, ':' ) ) return compoundStatementEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ keywordText( ctx, exceptKeyword ) ] + elements ), PRECEDENCE_STMT, suite, state )
def defStmt(self, ctx, state, node, name, params, suite): paramViews = mapViewEval( ctx, params, None, python25ViewState( Parser.param ) ) paramElements = [ text( ctx, punctuation_textStyle, '(' ) ] if len( params ) > 0: for p in paramViews[:-1]: paramElements.extend( [ p, text( ctx, punctuation_textStyle, ', ' ) ] ) paramElements.append( paramViews[-1] ) paramElements.append( text( ctx, punctuation_textStyle, ')' ) ) return compoundStatementEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ keywordText( ctx, defKeyword ), text( ctx, default_textStyle, ' ' ), text( ctx, default_textStyle, name ) ] + \ paramElements + [ text( ctx, punctuation_textStyle, ':' ) ] ), PRECEDENCE_STMT, suite, state )
def decoStmt(self, ctx, state, node, name, args): if args != '<nil>': argViews = mapViewEval( ctx, args, None, python25ViewState( Parser.callArg ) ) argElements = [ text( ctx, punctuation_textStyle, '(' ) ] if len( args ) > 0: for a in argViews[:-1]: argElements.extend( [ a, text( ctx, punctuation_textStyle, ', ' ) ] ) argElements.append( argViews[-1] ) argElements.append( text( ctx, punctuation_textStyle, ')' ) ) else: argElements = [] return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ text( ctx, punctuation_textStyle, '@' ), text( ctx, default_textStyle, name ) ] + argElements ), PRECEDENCE_STMT, state )
def intLiteral(self, ctx, state, node, format, numType, value): boxContents = [] if numType == 'int': if format == 'decimal': valueString = '%d' % int( value ) elif format == 'hex': valueString = '%x' % int( value, 16 ) boxContents.append( text( ctx, numericLiteral_textStyle, valueString ) ) elif numType == 'long': if format == 'decimal': valueString = '%d' % long( value ) elif format == 'hex': valueString = '%x' % long( value, 16 ) boxContents.append( text( ctx, numericLiteral_textStyle, valueString ) ) boxContents.append( text( ctx, literalFormat_textStyle, 'L' ) ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, boxContents ), PRECEDENCE_LITERALVALUE, state )
def lambdaExpr(self, ctx, 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( ctx, expr, None, python25ViewState( exprParser ) ) paramViews = mapViewEval( ctx, params, None, python25ViewState( Parser.param ) ) paramElements = [] if len( params ) > 0: for p in paramViews[:-1]: paramElements.append( p ) paramElements.append( text( ctx, punctuation_textStyle, ', ' ) ) paramElements.append( paramViews[-1] ) return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ keywordText( ctx, lambdaKeyword ), text( ctx, default_textStyle, ' ' ) ] + paramElements + [ text( ctx, punctuation_textStyle, ': ' ), exprView ] ), PRECEDENCE_LAMBDAEXPR, state )
def moduleContentImportAs(self, ctx, state, node, name, asName): return nodeEditor( ctx, node, paragraph( ctx, python_paragraphStyle, [ text( ctx, default_textStyle, name ), text( ctx, default_textStyle, ' ' ), keywordText( ctx, asKeyword ), text( ctx, default_textStyle, ' ' ), text( ctx, default_textStyle, asName ) ] ), PRECEDENCE_IMPORTCONTENT, state )