def test_integerLiteral(self): g = JavaGrammar() self._parseStringTest( g.literal(), '123', Schema.IntLiteral(format='decimal', numType='int', value='123')) self._parseStringTest( g.literal(), '123l', Schema.IntLiteral(format='decimal', numType='long', value='123')) self._parseStringTest( g.literal(), '123L', Schema.IntLiteral(format='decimal', numType='long', value='123')) self._parseStringFailTest(g.literal(), '12a') self._parseStringTest( g.literal(), '0x123abc', Schema.IntLiteral(format='hex', numType='int', value='0x123abc')) self._parseStringTest( g.literal(), '0x123abcl', Schema.IntLiteral(format='hex', numType='long', value='0x123abc')) self._parseStringTest( g.literal(), '0x123ABCL', Schema.IntLiteral(format='hex', numType='long', value='0x123ABC')) self._parseStringFailTest(g.literal(), '0x12g') self._parseStringTest( g.literal(), '0123', Schema.IntLiteral(format='oct', numType='int', value='0123')) self._parseStringTest( g.literal(), '0123l', Schema.IntLiteral(format='oct', numType='long', value='0123')) self._parseStringTest( g.literal(), '0123L', Schema.IntLiteral(format='oct', numType='long', value='0123')) self._parseStringFailTest(g.literal(), '012a') self._parseStringFailTest(g.literal(), '0128')
def test_primitiveTypeRef(self): g = JavaGrammar() self._parseStringTest(g.typeExpression(), 'boolean', Schema.BooleanTypeRef()) self._parseStringTest(g.typeExpression(), 'byte', Schema.ByteTypeRef()) self._parseStringTest(g.typeExpression(), 'short', Schema.ShortTypeRef()) self._parseStringTest(g.typeExpression(), 'int', Schema.IntTypeRef()) self._parseStringTest(g.typeExpression(), 'long', Schema.LongTypeRef()) self._parseStringTest(g.typeExpression(), 'char', Schema.CharTypeRef()) self._parseStringTest(g.typeExpression(), 'float', Schema.FloatTypeRef()) self._parseStringTest(g.typeExpression(), 'double', Schema.DoubleTypeRef())
def test_typeClassExp(self): g = JavaGrammar() self._parseStringTest( g.primary(), 'float.class', Schema.TypeClassExp(typeExp=Schema.FloatTypeRef())) self._parseStringTest( g.primary(), 'ArrayList<Integer>.class', Schema.TypeClassExp(typeExp=Schema.GenericTypeExp( target='ArrayList', args=[Schema.ClassOrInterfaceTypeRef(name='Integer')]))) self._parseStringTest(g.primary(), 'void.class', Schema.VoidClassExp())
def test_classOrInterfaceTypeRef(self): g = JavaGrammar() self._parseStringTest(g.typeExpression(), 'ArrayList', Schema.ClassOrInterfaceTypeRef(name='ArrayList')) self._parseStringTest( g.typeExpression(), 'java.util.List', Schema.MemberTypeExp( target=Schema.MemberTypeExp( target=Schema.ClassOrInterfaceTypeRef(name='java'), member=Schema.ClassOrInterfaceTypeRef(name='util')), member=Schema.ClassOrInterfaceTypeRef(name='List')))
def test_stringLiteral(self): g = JavaGrammar() self._parseStringTest(g.literal(), '"x"', Schema.StringLiteral(value='x'))
def test_charLiteral(self): g = JavaGrammar() self._parseStringTest(g.literal(), '\'a\'', Schema.CharLiteral(value='a')) self._parseStringTest(g.literal(), '\'\\n\'', Schema.CharLiteral(value='\\n'))
def shortTypeRef(self): return Keyword(Keywords.shortKeyword).action( lambda input, begin, end, x, bindings: Schema.ShortTypeRef())
def charTypeRef(self): return Keyword(Keywords.charKeyword).action( lambda input, begin, end, x, bindings: Schema.CharTypeRef())
def decimalIntLiteral(self): return Tokens.decimalIntegerNoOctal.action( lambda input, begin, end, x, bindings: Schema.IntLiteral( format='decimal', numType='int', value=x))
def test_genericTypeExp(self): g = JavaGrammar() self._parseStringTest( g.typeExpression(), 'ArrayList<Integer>', Schema.GenericTypeExp( target='ArrayList', args=[Schema.ClassOrInterfaceTypeRef(name='Integer')])) self._parseStringTest( g.typeExpression(), 'ArrayList<Integer,Double>', Schema.GenericTypeExp( target='ArrayList', args=[ Schema.ClassOrInterfaceTypeRef(name='Integer'), Schema.ClassOrInterfaceTypeRef(name='Double') ])) self._parseStringTest( g.typeExpression(), 'X.ArrayList<Integer>', Schema.MemberTypeExp( target=Schema.ClassOrInterfaceTypeRef(name='X'), member=Schema.GenericTypeExp( target='ArrayList', args=[Schema.ClassOrInterfaceTypeRef(name='Integer')]))) self._parseStringTest( g.typeExpression(), 'ArrayList<Integer>.X', Schema.MemberTypeExp( target=Schema.GenericTypeExp( target='ArrayList', args=[Schema.ClassOrInterfaceTypeRef(name='Integer')]), member=Schema.ClassOrInterfaceTypeRef(name='X'))) self._parseStringTest( g.typeExpression(), 'Vector<ArrayList<Integer>>', Schema.GenericTypeExp( target='Vector', args=[ Schema.GenericTypeExp( target='ArrayList', args=[Schema.ClassOrInterfaceTypeRef(name='Integer')]) ])) self._parseStringTest( g.typeExpression(), 'ArrayList<Integer[]>', Schema.GenericTypeExp( target='ArrayList', args=[ Schema.ArrayTypeExp( itemTypeExp=Schema.ClassOrInterfaceTypeRef( name='Integer')) ])) self._parseStringTest( g.typeExpression(), 'ArrayList<int[]>', Schema.GenericTypeExp( target='ArrayList', args=[Schema.ArrayTypeExp(itemTypeExp=Schema.IntTypeRef())])) self._parseStringTest( g.typeExpression(), 'ArrayList<? extends Q>', Schema.GenericTypeExp( target='ArrayList', args=[ Schema.WildCardTypeArgument( extendsOrSuper='extends', typeExp=Schema.ClassOrInterfaceTypeRef(name='Q')) ])) self._parseStringTest( g.typeExpression(), 'ArrayList<? super Q>', Schema.GenericTypeExp( target='ArrayList', args=[ Schema.WildCardTypeArgument( extendsOrSuper='super', typeExp=Schema.ClassOrInterfaceTypeRef(name='Q')) ])) self._parseStringTest( g.typeExpression(), 'ArrayList<?>', Schema.GenericTypeExp( target='ArrayList', args=[ Schema.WildCardTypeArgument( extendsOrSuper='super', typeExp=Schema.ClassOrInterfaceTypeRef(name='Q')) ])) self._parseStringFailTest(g.typeExpression(), 'ArrayList<int>')
def intTypeRef(self): return Keyword(Keywords.intKeyword).action( lambda input, begin, end, x, bindings: Schema.IntTypeRef())
def voidClassExp(self): return ( Keyword(Keywords.voidKeyword) + Literal('.') + Keyword(Keywords.classKeyword) ).action(lambda input, begin, end, xs, bindings: Schema.VoidClassExp())
def superExp(self): return Keyword(Keywords.superKeyword).action( lambda input, begin, end, x, bindings: Schema.SuperExp())
def thisExp(self): return Keyword(Keywords.thisKeyword).action( lambda input, begin, end, x, bindings: Schema.ThisExp())
def classOrInterfaceTypeRef(self): return self.simpleName().action(lambda input, begin, end, x, bindings: Schema.ClassOrInterfaceTypeRef(name=x))
def doubleTypeRef(self): return Keyword(Keywords.doubleKeyword).action( lambda input, begin, end, x, bindings: Schema.DoubleTypeRef())
def floatTypeRef(self): return Keyword(Keywords.floatKeyword).action( lambda input, begin, end, x, bindings: Schema.FloatTypeRef())
def test_booleanLiteral(self): g = JavaGrammar() self._parseStringTest(g.literal(), 'false', Schema.BooleanLiteral(value='false'))
def test_nullLiteral(self): g = JavaGrammar() self._parseStringTest(g.literal(), 'null', Schema.NullLiteral())
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 _action(input, begin, end, x, bindings): exp = x[0] for i in x[1]: exp = Schema.ArrayAccess(target=exp, index=i[1]) return exp
def test_thisExp(self): g = JavaGrammar() self._parseStringTest(g.thisExp(), 'this', Schema.ThisExp())
def test_arrayTypeExp(self): g = JavaGrammar() self._parseStringTest( g.typeExpression(), 'float []', Schema.ArrayTypeExp(itemTypeExp=Schema.FloatTypeRef())) self._parseStringTest( g.typeExpression(), 'float [][]', Schema.ArrayTypeExp(itemTypeExp=Schema.ArrayTypeExp( itemTypeExp=Schema.FloatTypeRef()))) self._parseStringTest( g.typeExpression(), 'ArrayList []', Schema.ArrayTypeExp(itemTypeExp=Schema.ClassOrInterfaceTypeRef( name='ArrayList'))) self._parseStringTest( g.typeExpression(), 'ArrayList<Integer> []', Schema.ArrayTypeExp(itemTypeExp=Schema.GenericTypeExp( target='ArrayList', args=[Schema.ClassOrInterfaceTypeRef(name='Integer')]))) self._parseStringTest( g.typeExpression(), 'java.util.List []', Schema.ArrayTypeExp(itemTypeExp=Schema.MemberTypeExp( target=Schema.MemberTypeExp( target=Schema.ClassOrInterfaceTypeRef(name='java'), member=Schema.ClassOrInterfaceTypeRef(name='util')), member=Schema.ClassOrInterfaceTypeRef(name='List'))))
def octIntLiteral(self): return Tokens.octalInteger.action( lambda input, begin, end, x, bindings: Schema.IntLiteral( format='oct', numType='int', value=x))
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] ) )
def longTypeRef(self): return Keyword(Keywords.longKeyword).action( lambda input, begin, end, x, bindings: Schema.LongTypeRef())
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]))
def test_floatLiteral(self): g = JavaGrammar() self._parseStringTest(g.literal(), '123.45', Schema.FloatLiteral(value='123.45'))
def floatLiteral(self): return Tokens.floatingPoint.action( lambda input, begin, end, x, bindings: Schema.FloatLiteral(value=x ))
def byteTypeRef(self): return Keyword(Keywords.byteKeyword).action( lambda input, begin, end, x, bindings: Schema.ByteTypeRef())