Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java7 and Java8 improvements #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 125 additions & 5 deletions src/main/javacc/Java1.1.jj
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public class JavaParser implements JavaParserInterface
public static final int TRANSIENT = 0x0100;
public static final int VOLATILE = 0x0200;
public static final int STRICTFP = 0x1000;
public static final int DEFAULT = 0x2000;

/** A set of accessors that indicate whether the specified modifier
is in the set. */
Expand Down Expand Up @@ -259,6 +260,11 @@ public class JavaParser implements JavaParserInterface
return (modifiers & STRICTFP) != 0;
}

public boolean isDefault(int modifiers)
{
return (modifiers & DEFAULT) != 0;
}

public boolean isSynchronized(int modifiers)
{
return (modifiers & SYNCHRONIZED) != 0;
Expand All @@ -282,6 +288,36 @@ public class JavaParser implements JavaParserInterface
return modifiers & ~mod;
}
}

public static void main(String args[]) {
JavaParser parser;
if (args.length == 0) {
System.out.println("Java Parser Version 1.1: Reading from standard input . . .");
parser = new JavaParser(System.in);
} else if (args.length == 1) {
System.out.println("Java Parser Version 1.1: Reading from file " + args[0] + " . . .");
try {
parser = new JavaParser(new java.io.FileInputStream(args[0]));
} catch (java.io.FileNotFoundException e) {
System.out.println("Java Parser Version 1.1: File " + args[0] + " not found.");
return;
}
} else {
System.out.println("Java Parser Version 1.1: Usage is one of:");
System.out.println(" java javancss.parser.JavaParser < inputfile");
System.out.println("OR");
System.out.println(" java javancss.parser.JavaParser inputfile");
return;
}
try {
parser.CompilationUnit();
System.out.println("Java Parser Version 1.1: Java program parsed successfully.");
} catch (ParseException e) {
System.out.println(e.getMessage());
System.out.println("Java Parser Version 1.1: Encountered errors during parse.");
}
}

}

PARSER_END(JavaParser)
Expand Down Expand Up @@ -808,6 +844,8 @@ TOKEN :
| < COMMA: "," >
| < DOT: "." >
| < AT: "@" >
| < LAMBDA: "->" >
| < DOUBLECOLON: "::" >
}

/* OPERATORS */
Expand Down Expand Up @@ -1357,7 +1395,7 @@ void ClassBodyDeclaration() :
void MethodDeclarationLookahead() :
{}
{
( "public" | "protected" | "private" | "static" | "abstract" | "final" | "native" | "synchronized" | "strictfp" | Annotation() )*
( "public" | "protected" | "private" | "static" | "abstract" | "final" | "native" | "synchronized" | "strictfp" | "default" | Annotation() )*
[ TypeParameters() ]
ResultType() Identifier() "("
}
Expand Down Expand Up @@ -1793,6 +1831,12 @@ void MethodDeclaration() :
}
}
| "strictfp"
{
if ( tmpToken == null ) {
tmpToken = getToken( 0 );
}
}
| "default"
{
if ( tmpToken == null ) {
tmpToken = getToken( 0 );
Expand Down Expand Up @@ -2282,7 +2326,14 @@ void Expression() :
//System.out.println( "Expression start" );
}
{
LOOKAHEAD(LambdaExpression()) LambdaExpression()
|
AssignmentExpression()
}

void AssignmentExpression() :
{}
{
LOOKAHEAD( PrimaryExpression() AssignmentOperator() )
//{ System.out.println( "Expression" ); }
Assignment()
Expand Down Expand Up @@ -2464,6 +2515,8 @@ void CastExpression() :
{
LOOKAHEAD("(" PrimitiveType())
"(" Type() ")" UnaryExpression()
|
LOOKAHEAD("(" Type() ")" LambdaExpression()) "(" Type() ")" LambdaExpression()
|
"(" Type() ")" UnaryExpressionNotPlusMinus()
}
Expand All @@ -2473,6 +2526,8 @@ void PrimaryExpression() :
// { System.out.println( "Before PrimaryExpression" ); }
}
{
LOOKAHEAD(MethodReference()) MethodReference()
|
PrimaryPrefix() ( LOOKAHEAD(2) PrimarySuffix() )*
}

Expand Down Expand Up @@ -2521,7 +2576,7 @@ void PrimarySuffix() :
// { System.out.println( "PrimarySuffix . Identifier()" ); }
"." Identifier()
|
Arguments()
Arguments() [ "{" MethodDeclaration() "}"]
}

void Literal() :
Expand Down Expand Up @@ -2781,11 +2836,27 @@ void SwitchStatement() :
_localCases = 0;
}
"switch" "(" Expression() ")" "{"
( SwitchLabel() ( BlockStatement() )* )*
(SwitchCase())*
"}"
{ _ncss++; Util.debug( "_ncss++" );}
}

void SwitchCase() :
{}
{
SwitchLabel() SwitchBlockStatements()
}

void SwitchBlockStatements() :
{}
{
LOOKAHEAD(SwitchLabel()) {}
|
BlockStatement() SwitchBlockStatements()
|
{}
}

void SwitchLabel() :
{}
{
Expand All @@ -2800,7 +2871,7 @@ void SwitchLabel() :
"default" ":"
{
_ncss++; Util.debug( "_ncss++" );
}
}
}

void IfStatement() :
Expand Down Expand Up @@ -2933,7 +3004,7 @@ void TryWithResources() :
{}
{
// LocalVariableDeclaration() [ ";" ]
LocalVariableDeclaration() ( ";" LocalVariableDeclaration() )*
LocalVariableDeclaration() ( LOOKAHEAD(2) ";" LocalVariableDeclaration() )* [ ";" ]
}

void Identifier() :
Expand Down Expand Up @@ -3140,6 +3211,11 @@ int Modifiers():
"strictfp" { modifiers |= ModifierSet.STRICTFP; if ( _tmpToken == null ) {
_tmpToken = getToken( 0 );
}
}
|
"default" { modifiers |= ModifierSet.DEFAULT; if ( _tmpToken == null ) {
_tmpToken = getToken( 0 );
}
}
|
Annotation()
Expand Down Expand Up @@ -3443,3 +3519,47 @@ void MemberSelector():
"." TypeArguments() <IDENTIFIER>
}

void LambdaExpression():
{}
{
LambdaParameters() "->" LambdaBody()
}

void LambdaParameters():
{}
{
<IDENTIFIER>
|
LOOKAHEAD("(" InferredFormalParameterList() ")") "(" InferredFormalParameterList() ")"
|
LOOKAHEAD( [FormalParameters()] ) [FormalParameters()]
}

void InferredFormalParameterList():
{}
{
<IDENTIFIER> ( "," <IDENTIFIER> )*
}


void LambdaBody():
{}
{
Expression()
|
Block()
}

void MethodReference():
{}
{
"super" "::" [TypeArguments()] <IDENTIFIER>
|
LOOKAHEAD( 4 ) Name() "." "super" "::" [TypeArguments()] <IDENTIFIER>
|
// ClassType() "::" [TypeArguments()] "new" and ArrayType() "::" "new" is included in below
LOOKAHEAD(ReferenceType() "::" [TypeArguments()] ( <IDENTIFIER> | "new" )) ReferenceType() "::" [TypeArguments()] ( <IDENTIFIER> | "new" )
|
// Name() "::" [TypeArguments()] <IDENTIFIER> is included in below
PrimaryPrefix() ( LOOKAHEAD(2) PrimarySuffix() )* "::" [TypeArguments()] <IDENTIFIER>
}
5 changes: 4 additions & 1 deletion src/test/java/javancss/test/ParseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ private void testParse()
_checkParse( 155 ); // JAVANCSS-28
_checkParse( 156 ); // hexadecimal floating-point literals
_checkParse( 157 ); // Java 7 literals
_checkParse( 158 ); // JAVANCSS-48
_checkParse(158); // JAVANCSS-48
_checkParse(159); // default and static method in interface
_checkParse(160); // java8 lambda and method reference
_checkParse(161); // found by cobertura

_exitSubTest();
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/resources/Test148.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ public void baz() {
// do nothing
}
}

public void baz2() {
String zipFileName = "";
String outputFileName = "";
java.nio.charset.Charset charset = java.nio.charset.Charset.forName("US-ASCII");
java.nio.file.Path outputFilePath = java.nio.file.Paths.get(outputFileName);

try (
java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName);
java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset);
) {
// do nothing
}
catch (java.beans.PropertyVetoException e) {
// do nothing
}
}

}

9 changes: 9 additions & 0 deletions src/test/resources/Test159.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
interface Interface1 {
static void staticTest(String str) {
System.out.println(str);
}

default int defaultTest() {
return 1;
}
}
Loading