def referenceTypeExp(self): def _action(input, begin, end, x, bindings): typeExp = x[0] for a in x[1]: typeExp = Schema.ArrayTypeExp(itemTypeExp=typeExp) return typeExp return ( self.classOrInterfaceTypeExp() + ( Literal( '[' ) + Literal( ']' ) ).zeroOrMore() ).action( _action ) | \ ( self.primitiveTypeRef() + ( Literal( '[' ) + Literal( ']' ) ).oneOrMore() ).action( _action )
def arrayAccess(self): def _action(input, begin, end, x, bindings): exp = x[0] for i in x[1]: exp = Schema.ArrayAccess(target=exp, index=i[1]) return exp return (self.primaryNoNewArray() + (Literal('[') + self.expression() + Literal(']')).oneOrMore()).action(_action)
def repeatRange(self): # Note - greedy option is inverted return (self.item() + Literal('{') + Tokens.decimalInteger + Literal(',') + Tokens.decimalInteger + Literal('}') + Literal('?').optional() ).action(lambda input, begin, end, x, bindings: Schema. RepeatRange(subexp=x[0], min=x[2], max=x[4], greedy=('1' if x[6] is None else None)))
def ops(self): opTable = OperatorTable([ InfixLeftLevel([ BinaryOperator( Literal('*'), lambda input, pos, end, left, right: ['mul', left, right]) ]), InfixLeftLevel([ BinaryOperator( Literal('+'), lambda input, pos, end, left, right: ['add', left, right]) ]), ], self.atom()) return opTable.buildParsers()
def genericWildcardArgument(self): return (Literal('?') + (Keyword(Keywords.superKeyword) | Keyword(Keywords.extendsKeyword)) + self.referenceTypeExp() ).action(lambda input, begin, end, x, bindings: Schema. WildCardTypeArgument(extendsOrSuper=('extends' if x[ 1] == Keywords.extendsKeyword else 'super'), typeExp=x[2]))
def classOrInterfaceTypeExp(self): def _action(input, begin, end, x, bindings): typeExp = x[0] for g in x[1]: typeExp = Schema.MemberTypeExp(target=typeExp, member=g[1]) return typeExp return (self.classOrInterfaceTypeExpComponent() + (Literal('.') + self.classOrInterfaceTypeExpComponent() ).zeroOrMore()).action(_action)
def voidClassExp(self): return ( Keyword(Keywords.voidKeyword) + Literal('.') + Keyword(Keywords.classKeyword) ).action(lambda input, begin, end, xs, bindings: Schema.VoidClassExp())
def parenForm(self): return (Literal('(') + self.expression() + ')').action( lambda input, begin, end, xs, bindings: _incrementParens(xs[1]))
def typeClassExp(self): return (self.typeExpression() + Literal('.') + Keyword(Keywords.classKeyword) ).action(lambda input, begin, end, xs, bindings: Schema. TypeClassExp(typeExp=xs[0]))
def qualifiedIdentifier(self): return (self.identifier() + (Literal('.') + self.identifier()).zeroOrMore() ).action(lambda input, begin, end, x, bindings: [x[0]] + [a[1] for a in x[1]])
def charSetChar(self): return self.escapedChar( ) | (RegEx('[^\\]\\-\\\\]') | Literal('-')).action( lambda input, begin, end, x, bindings: Schema.LiteralChar(char=x))
def fieldAccess(self): return ((self.primary() | self.superExp()) + Literal('.') + self.simpleName() ).action(lambda input, begin, end, xs, bindings: Schema. ClassInstanceCreation(target=xs[0], fieldName=xs[2]))
def newLine(self): return Literal('\n').action(lambda input, begin, end, x, bindings: Schema.PythonEscapedChar(char='n'))
def endOfLine(self): return Literal('$').action( lambda input, begin, end, x, bindings: Schema.EndOfLine())
def startOfLine(self): return Literal('^').action( lambda input, begin, end, x, bindings: Schema.StartOfLine())
def b(self): return Literal('b') | Literal('d')
def paren(self): return (Literal('(') + self.expr() + Literal(')') ).action(lambda input, begin, end, xs, bindings: xs[1])
def a(self): return Literal('a') | Literal('c')
def charSet(self): return (Literal('[') + Literal('^').optional() + self.charSetItem().oneOrMore() + Literal(']') ).action(lambda input, begin, end, x, bindings: Schema.CharSet( invert=('1' if x[1] is not None else None), items=x[2]))
def charSetItemRange(self): return ( self.charSetChar() + Literal('-') + self.charSetChar()).action(lambda input, begin, end, x, bindings: Schema.CharSetRange(min=x[0], max=x[2]))
def classInstanceCreationExpression(self): return ( Keyword( Keywords.newKeyword ) + self.classOrInterfaceTypeRef() + Literal( '(' ) + SeparatedList( self.expression(), 0, -1, SeparatedList.TrailingSeparatorPolicy.NEVER ) + Literal( ')' ) ).action( \ lambda input, begin, end, xs, bindings: Schema.ClassInstanceCreation( classTypeRef=xs[1], args=xs[3] ) )
def escapedRegexChar(self): return (Literal('\\') + RegEx('[^0-9]')).action(lambda input, begin, end, x, bindings: Schema.EscapedChar(char=x[1]))
def dimExpr(self): return ( Literal('[') + self.expression() + Literal(']')).action(lambda input, begin, end, xs, bindings: xs[1])
def genericTypeCall(self): return ( self.simpleName() + Literal( '<' ) + SeparatedList( self.genericTypeArgument(), 0, -1, SeparatedList.TrailingSeparatorPolicy.NEVER ) + Literal( '>' ) ).action( \ lambda input, begin, end, x, bindings: Schema.GenericTypeExp( target=x[0], args=x[2] ) )
def methodInvocation(self): return ((self.name() | self.primary() | self.superExp()) + Literal('.') + self.simpleName() ).action(lambda input, begin, end, xs, bindings: Schema. MethodInvocation(target=xs[0], fieldName=xs[2]))
def anyChar(self): return Literal('.').action( lambda input, begin, end, x, bindings: Schema.AnyChar())
class JavaGrammar(Grammar): __junk_regex__ = '[ ]*' decimalInteger = RegEx(r"[\-]?[1-9][0-9]*") | Literal("0") hexInteger = RegEx(r"0[xX][0-9A-Fa-f]+") octalInteger = RegEx(r"0[0-7]+") @Rule def identifier(self): return Tokens.javaIdentifier @Rule def qualifiedIdentifier(self): return (self.identifier() + (Literal('.') + self.identifier()).zeroOrMore() ).action(lambda input, begin, end, x, bindings: [x[0]] + [a[1] for a in x[1]]) # Integer literal @Rule def decimalIntLiteral(self): return Tokens.decimalIntegerNoOctal.action( lambda input, begin, end, x, bindings: Schema.IntLiteral( format='decimal', numType='int', value=x)) @Rule def decimalLongLiteral(self): return (Tokens.decimalIntegerNoOctal + Suppress(Literal('l') | Literal('L'))).action( lambda input, begin, end, x, bindings: Schema.IntLiteral( format='decimal', numType='long', value=x[0])) @Rule def hexIntLiteral(self): return Tokens.hexInteger.action( lambda input, begin, end, x, bindings: Schema.IntLiteral( format='hex', numType='int', value=x)) @Rule def hexLongLiteral(self): return (Tokens.hexInteger + Suppress(Literal('l') | Literal('L')) ).action(lambda input, begin, end, x, bindings: Schema. IntLiteral(format='hex', numType='long', value=x[0])) @Rule def octIntLiteral(self): return Tokens.octalInteger.action( lambda input, begin, end, x, bindings: Schema.IntLiteral( format='oct', numType='int', value=x)) @Rule def octLongLiteral(self): return (Tokens.octalInteger + Suppress(Literal('l') | Literal('L')) ).action(lambda input, begin, end, x, bindings: Schema. IntLiteral(format='oct', numType='long', value=x[0])) @Rule def integerLiteral(self): return self.hexLongLiteral( ) | self.hexIntLiteral() | self.octLongLiteral() | self.octIntLiteral( ) | self.decimalLongLiteral() | self.decimalIntLiteral() # Float literal @Rule def floatLiteral(self): return Tokens.floatingPoint.action( lambda input, begin, end, x, bindings: Schema.FloatLiteral(value=x )) # Character literal @Rule def charLiteral(self): return Tokens.javaCharacterLiteral.action( lambda input, begin, end, x, bindings: Schema.CharLiteral(value=x[ 1:-1])) # String literal @Rule def stringLiteral(self): return Tokens.javaStringLiteral.action( lambda input, begin, end, x, bindings: Schema.StringLiteral( value=x[1:-1])) # Boolean Literal @Rule def booleanLiteral(self): return (Keyword(Keywords.falseKeyword) | Keyword(Keywords.trueKeyword) ).action(lambda input, begin, end, x, bindings: Schema. BooleanLiteral(value=x)) # Null Literal @Rule def nullLiteral(self): return Keyword(Keywords.nullKeyword).action( lambda input, begin, end, x, bindings: Schema.NullLiteral()) # Java literal @Rule def literal(self): return self.floatLiteral() | self.integerLiteral() | self.charLiteral( ) | self.stringLiteral() | self.booleanLiteral() | self.nullLiteral() # Type reference @Rule def typeExpression(self): return self.referenceTypeExp() | self.primitiveTypeRef() @Rule def referenceTypeExp(self): def _action(input, begin, end, x, bindings): typeExp = x[0] for a in x[1]: typeExp = Schema.ArrayTypeExp(itemTypeExp=typeExp) return typeExp return ( self.classOrInterfaceTypeExp() + ( Literal( '[' ) + Literal( ']' ) ).zeroOrMore() ).action( _action ) | \ ( self.primitiveTypeRef() + ( Literal( '[' ) + Literal( ']' ) ).oneOrMore() ).action( _action ) @Rule def classOrInterfaceTypeExp(self): def _action(input, begin, end, x, bindings): typeExp = x[0] for g in x[1]: typeExp = Schema.MemberTypeExp(target=typeExp, member=g[1]) return typeExp return (self.classOrInterfaceTypeExpComponent() + (Literal('.') + self.classOrInterfaceTypeExpComponent() ).zeroOrMore()).action(_action) @Rule def classOrInterfaceTypeExpComponent(self): return self.genericTypeCall() | self.classOrInterfaceTypeRef() @Rule def genericTypeCall(self): return ( self.simpleName() + Literal( '<' ) + SeparatedList( self.genericTypeArgument(), 0, -1, SeparatedList.TrailingSeparatorPolicy.NEVER ) + Literal( '>' ) ).action( \ lambda input, begin, end, x, bindings: Schema.GenericTypeExp( target=x[0], args=x[2] ) ) @Rule def genericTypeArgument(self): return self.referenceTypeExp() | self.genericWildcardArgument() @Rule def genericWildcardArgument(self): return (Literal('?') + (Keyword(Keywords.superKeyword) | Keyword(Keywords.extendsKeyword)) + self.referenceTypeExp() ).action(lambda input, begin, end, x, bindings: Schema. WildCardTypeArgument(extendsOrSuper=('extends' if x[ 1] == Keywords.extendsKeyword else 'super'), typeExp=x[2])) @Rule def primitiveTypeRef(self): return self.byteTypeRef() | self.shortTypeRef() | self.intTypeRef( ) | self.longTypeRef() | self.charTypeRef() | self.floatTypeRef( ) | self.doubleTypeRef() | self.booleanTypeRef() @Rule def booleanTypeRef(self): return Keyword(Keywords.booleanKeyword).action( lambda input, begin, end, x, bindings: Schema.BooleanTypeRef()) @Rule def byteTypeRef(self): return Keyword(Keywords.byteKeyword).action( lambda input, begin, end, x, bindings: Schema.ByteTypeRef()) @Rule def shortTypeRef(self): return Keyword(Keywords.shortKeyword).action( lambda input, begin, end, x, bindings: Schema.ShortTypeRef()) @Rule def intTypeRef(self): return Keyword(Keywords.intKeyword).action( lambda input, begin, end, x, bindings: Schema.IntTypeRef()) @Rule def longTypeRef(self): return Keyword(Keywords.longKeyword).action( lambda input, begin, end, x, bindings: Schema.LongTypeRef()) @Rule def charTypeRef(self): return Keyword(Keywords.charKeyword).action( lambda input, begin, end, x, bindings: Schema.CharTypeRef()) @Rule def floatTypeRef(self): return Keyword(Keywords.floatKeyword).action( lambda input, begin, end, x, bindings: Schema.FloatTypeRef()) @Rule def doubleTypeRef(self): return Keyword(Keywords.doubleKeyword).action( lambda input, begin, end, x, bindings: Schema.DoubleTypeRef()) @Rule def classOrInterfaceTypeRef(self): return self.simpleName().action(lambda input, begin, end, x, bindings: Schema.ClassOrInterfaceTypeRef(name=x)) # Name @Rule def name(self): return (self.simpleName() + (Literal('.') + self.simpleName()).zeroOrMore() ).action(lambda input, begin, end, x, bindings: [x[0]] + [a[1] for a in x[1]]) @Rule def simpleName(self): return Tokens.javaIdentifier & (lambda input, begin, end, x, bindings: x not in Keywords.keywordsSet) # This @Rule def thisExp(self): return Keyword(Keywords.thisKeyword).action( lambda input, begin, end, x, bindings: Schema.ThisExp()) # Super @Rule def superExp(self): return Keyword(Keywords.superKeyword).action( lambda input, begin, end, x, bindings: Schema.SuperExp()) # Parentheses @Rule def parenForm(self): return (Literal('(') + self.expression() + ')').action( lambda input, begin, end, xs, bindings: _incrementParens(xs[1])) # Primary @Rule def primary(self): return self.primaryNoNewArray() | self.arrayCreationExpression() @Rule def primaryNoNewArray(self): return self.literal() | self.typeClassExp() | self.voidClassExp( ) | self.thisExp() | self.parenForm( ) | self.classInstanceCreationExpression() | self.fieldAccess( ) | self.methodInvocation() | self.arrayAccess() @Rule def typeClassExp(self): return (self.typeExpression() + Literal('.') + Keyword(Keywords.classKeyword) ).action(lambda input, begin, end, xs, bindings: Schema. TypeClassExp(typeExp=xs[0])) @Rule def voidClassExp(self): return ( Keyword(Keywords.voidKeyword) + Literal('.') + Keyword(Keywords.classKeyword) ).action(lambda input, begin, end, xs, bindings: Schema.VoidClassExp()) @Rule def classInstanceCreationExpression(self): return ( Keyword( Keywords.newKeyword ) + self.classOrInterfaceTypeRef() + Literal( '(' ) + SeparatedList( self.expression(), 0, -1, SeparatedList.TrailingSeparatorPolicy.NEVER ) + Literal( ')' ) ).action( \ lambda input, begin, end, xs, bindings: Schema.ClassInstanceCreation( classTypeRef=xs[1], args=xs[3] ) ) @Rule def dimExpr(self): return ( Literal('[') + self.expression() + Literal(']')).action(lambda input, begin, end, xs, bindings: xs[1]) @Rule def fieldAccess(self): return ((self.primary() | self.superExp()) + Literal('.') + self.simpleName() ).action(lambda input, begin, end, xs, bindings: Schema. ClassInstanceCreation(target=xs[0], fieldName=xs[2])) @Rule def methodInvocation(self): return ((self.name() | self.primary() | self.superExp()) + Literal('.') + self.simpleName() ).action(lambda input, begin, end, xs, bindings: Schema. MethodInvocation(target=xs[0], fieldName=xs[2])) @Rule def arrayAccess(self): def _action(input, begin, end, x, bindings): exp = x[0] for i in x[1]: exp = Schema.ArrayAccess(target=exp, index=i[1]) return exp return (self.primaryNoNewArray() + (Literal('[') + self.expression() + Literal(']')).oneOrMore()).action(_action) @Rule def arrayCreationExpression(self): return ( Keyword( Keywords.newKeyword ) + ( self.classOrInterfaceTypeRef() | self.primitiveTypeRef() ) + self.dimExpr().oneOrMore() ).action( \ lambda input, begin, end, xs, bindings: Schema.ClassInstanceCreation( classTypeRef=xs[1], args=xs[3] ) ) @Rule def expression(self): return self.primary()
def name(self): return (self.simpleName() + (Literal('.') + self.simpleName()).zeroOrMore() ).action(lambda input, begin, end, x, bindings: [x[0]] + [a[1] for a in x[1]])
def octLongLiteral(self): return (Tokens.octalInteger + Suppress(Literal('l') | Literal('L')) ).action(lambda input, begin, end, x, bindings: Schema. IntLiteral(format='oct', numType='long', value=x[0]))
def charClass(self): return (Literal('\\') + RegEx('[AbBdDsSwWZ]')).action( lambda input, begin, end, x, bindings: Schema.CharClass(cls=x[1]))