Skip to content

Commit

Permalink
Merge 155768b into 9991a25
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Jun 3, 2017
2 parents 9991a25 + 155768b commit 05f07e4
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 110 deletions.
Expand Up @@ -52,7 +52,7 @@ protected void generateNode(BaseNodeMetaModel nodeMetaModel, CompilationUnit nod

body.addStatement("customInitialization();");

replaceWhenSameSignature(nodeCoid, constructor);
addOrReplaceWhenSameSignature(nodeCoid, constructor);
nodeCu.addImport(TokenRange.class);
annotateGenerated(constructor);
}
Expand Down
Expand Up @@ -131,16 +131,6 @@ public <N extends Node> ParseResult<N> parse(ParseStart<N> start, Provider provi

return new ParseResult<>(resultNode, parser.problems, parser.getTokens(),
parser.getCommentsCollection());
} catch (ParseException p) {
TokenRange tokenRange = null;
if (p.currentToken != null) {
if (p.currentToken instanceof GeneratedJavaParser.CustomToken) {
final JavaToken token = ((GeneratedJavaParser.CustomToken) p.currentToken).javaToken;
tokenRange = new TokenRange(token, token);
}
}
parser.problems.add(new Problem(makeMessageForParseException(p), tokenRange, p));
return new ParseResult<>(null, parser.problems, parser.getTokens(), parser.getCommentsCollection());
} catch (Exception e) {
final String message = e.getMessage() == null ? "Unknown error" : e.getMessage();
parser.problems.add(new Problem(message, null, e));
Expand All @@ -154,64 +144,6 @@ public <N extends Node> ParseResult<N> parse(ParseStart<N> start, Provider provi
}
}

/**
* This is the code from ParseException.initialise, modified to be more horizontal.
*/
private String makeMessageForParseException(ParseException exception) {
final StringBuilder sb = new StringBuilder("Parse error. Found ");
final StringBuilder expected = new StringBuilder();

int maxExpectedTokenSequenceLength = 0;
TreeSet<String> sortedOptions = new TreeSet<>();
for (int i = 0; i < exception.expectedTokenSequences.length; i++) {
if (maxExpectedTokenSequenceLength < exception.expectedTokenSequences[i].length) {
maxExpectedTokenSequenceLength = exception.expectedTokenSequences[i].length;
}
for (int j = 0; j < exception.expectedTokenSequences[i].length; j++) {
sortedOptions.add(exception.tokenImage[exception.expectedTokenSequences[i][j]]);
}
}

for (String option : sortedOptions) {
expected.append(" ").append(option);
}

sb.append("");

Token token = exception.currentToken.next;
for (int i = 0; i < maxExpectedTokenSequenceLength; i++) {
String tokenText = token.image;
String escapedTokenText = ParseException.add_escapes(tokenText);
if (i != 0) {
sb.append(" ");
}
if (token.kind == 0) {
sb.append(exception.tokenImage[0]);
break;
}
escapedTokenText = "\"" + escapedTokenText + "\"";
String image = exception.tokenImage[token.kind];
if (image.equals(escapedTokenText)) {
sb.append(image);
} else {
sb.append(" ")
.append(escapedTokenText)
.append(" ")
.append(image);
}
token = token.next;
}

if (exception.expectedTokenSequences.length != 0) {
int numExpectedTokens = exception.expectedTokenSequences.length;
sb.append(", expected")
.append(numExpectedTokens == 1 ? "" : " one of ")
.append(expected.toString());
}
return sb.toString();

}

/**
* Parses the Java code contained in the {@link InputStream} and returns a
* {@link CompilationUnit} that represents it.
Expand Down
Expand Up @@ -23,7 +23,7 @@
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParseStart;
import com.github.javaparser.Range;
import com.github.javaparser.TokenRange;
import com.github.javaparser.ast.body.AnnotationDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.EnumDeclaration;
Expand All @@ -33,6 +33,7 @@
import com.github.javaparser.ast.expr.Name;
import com.github.javaparser.ast.modules.ModuleDeclaration;
import com.github.javaparser.ast.nodeTypes.NodeWithName;
import com.github.javaparser.ast.nodeTypes.PossiblyBadNode;
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.ast.visitor.CloneVisitor;
import com.github.javaparser.ast.visitor.GenericVisitor;
Expand All @@ -43,6 +44,7 @@
import com.github.javaparser.printer.PrettyPrinter;
import com.github.javaparser.utils.ClassUtils;
import com.github.javaparser.utils.CodeGenerationUtils;

import javax.annotation.Generated;
import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -54,14 +56,12 @@
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.github.javaparser.JavaParser.parseName;
import static com.github.javaparser.Providers.UTF8;
import static com.github.javaparser.Providers.provider;
import static com.github.javaparser.utils.CodeGenerationUtils.f;
import static com.github.javaparser.utils.CodeGenerationUtils.subtractPaths;
import static com.github.javaparser.utils.Utils.assertNotNull;
import com.github.javaparser.ast.Node;
import com.github.javaparser.TokenRange;

/**
* <p>
Expand All @@ -77,7 +77,7 @@
* @see ImportDeclaration
* @see TypeDeclaration
*/
public final class CompilationUnit extends Node {
public class CompilationUnit extends Node implements PossiblyBadNode {

private PackageDeclaration packageDeclaration;

Expand All @@ -90,23 +90,27 @@ public final class CompilationUnit extends Node {
@InternalProperty
private Storage storage;

@InternalProperty
private boolean isBad;

public CompilationUnit() {
this(null, null, new NodeList<>(), new NodeList<>(), null);
this(null, false, null, new NodeList<>(), new NodeList<>(), null);
}

public CompilationUnit(String packageDeclaration) {
this(null, new PackageDeclaration(new Name(packageDeclaration)), new NodeList<>(), new NodeList<>(), null);
this(null, false, new PackageDeclaration(new Name(packageDeclaration)), new NodeList<>(), new NodeList<>(), null);
}

@AllFieldsConstructor
public CompilationUnit(PackageDeclaration packageDeclaration, NodeList<ImportDeclaration> imports, NodeList<TypeDeclaration<?>> types, ModuleDeclaration module) {
this(null, packageDeclaration, imports, types, module);
this(null, false, packageDeclaration, imports, types, module);
}

/**This constructor is used by the parser and is considered private.*/
@Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
public CompilationUnit(TokenRange tokenRange, PackageDeclaration packageDeclaration, NodeList<ImportDeclaration> imports, NodeList<TypeDeclaration<?>> types, ModuleDeclaration module) {
public CompilationUnit(TokenRange tokenRange, boolean isBad, PackageDeclaration packageDeclaration, NodeList<ImportDeclaration> imports, NodeList<TypeDeclaration<?>> types, ModuleDeclaration module) {
super(tokenRange);
this.isBad = isBad;
setPackageDeclaration(packageDeclaration);
setImports(imports);
setTypes(types);
Expand Down Expand Up @@ -547,6 +551,11 @@ public CompilationUnit setStorage(Path path) {
return this;
}

@Override
public boolean isBad() {
return isBad;
}

/**
* Information about where this compilation unit was loaded from.
* This class only stores the absolute location.
Expand Down
Expand Up @@ -21,10 +21,9 @@

package com.github.javaparser.ast.nodeTypes;

import com.github.javaparser.ast.Node;

/**
* A node with parse problems.
* A node that may have parse problems.
*/
public interface BadNode<N extends Node> {
public interface PossiblyBadNode {
boolean isBad();
}
Expand Up @@ -43,7 +43,7 @@ public Visitable visit(CompilationUnit n, Object arg) {
PackageDeclaration packageDeclaration = cloneNode(n.getPackageDeclaration(), arg);
NodeList<TypeDeclaration<?>> types = cloneList(n.getTypes(), arg);
Comment comment = cloneNode(n.getComment(), arg);
CompilationUnit r = new CompilationUnit(n.getTokenRange().orElse(null), packageDeclaration, imports, types, module);
CompilationUnit r = new CompilationUnit(n.getTokenRange().orElse(null), false, packageDeclaration, imports, types, module);
r.setComment(comment);
return r;
}
Expand Down
57 changes: 39 additions & 18 deletions javaparser-core/src/main/javacc/java.jj
Expand Up @@ -75,6 +75,21 @@ final class GeneratedJavaParser {
token_source.reset();
}

void recover(int recoveryTokenType, ParseException p) {
TokenRange tokenRange = null;
if (p.currentToken != null) {
if (p.currentToken instanceof GeneratedJavaParser.CustomToken) {
final JavaToken token = ((GeneratedJavaParser.CustomToken) p.currentToken).javaToken;
tokenRange = new TokenRange(token, token);
}
}
problems.add(new Problem(makeMessageForParseException(p), tokenRange, p));
Token t;
do {
t = getNextToken();
} while (t.kind != recoveryTokenType);
}

/**
* Return the list of tokens that have been encountered while parsing code using
* this parser.
Expand Down Expand Up @@ -659,25 +674,31 @@ CompilationUnit CompilationUnit():
ModuleDeclaration module = null;
}
{
( ";" )*
[ LOOKAHEAD(PackageDeclaration()) pakage = PackageDeclaration() ]
( in = ImportDeclaration() { imports = add(imports, in); } | ";" )*
(
modifier = Modifiers()
try {
( ";" )*
[ LOOKAHEAD(PackageDeclaration()) pakage = PackageDeclaration() ]
(
tn = ClassOrInterfaceDeclaration(modifier) { types = add(types, tn); }
|
tn = EnumDeclaration(modifier) { types = add(types, tn); }
|
tn = AnnotationTypeDeclaration(modifier) { types = add(types, tn); }
|
module = ModuleDeclaration(modifier)
|
";"
)
)*
(<EOF> | "\u001A" /** ctrl+z char **/)
{ return new CompilationUnit(range(token_source.getHomeToken(), token()), pakage, imports, types, module); }
in = ImportDeclaration() { imports = add(imports, in); }| ";" )*
(
modifier = Modifiers()
(
tn = ClassOrInterfaceDeclaration(modifier) { types = add(types, tn); }
|
tn = EnumDeclaration(modifier) { types = add(types, tn); }
|
tn = AnnotationTypeDeclaration(modifier) { types = add(types, tn); }
|
module = ModuleDeclaration(modifier)
|
";"
)
)*
(<EOF> | "\u001A" /** ctrl+z char **/)
{ return new CompilationUnit(range(token_source.getHomeToken(), token()), false, pakage, imports, types, module); }
} catch (ParseException e) {
recover(EOF, e);
return new CompilationUnit(range(token_source.getHomeToken(), token()), true, null, new NodeList<ImportDeclaration>(), new NodeList<TypeDeclaration<?>>(), null);
}
}

PackageDeclaration PackageDeclaration():
Expand Down
Expand Up @@ -12,13 +12,9 @@
import com.github.javaparser.ast.type.UnknownType;
import com.github.javaparser.utils.Pair;

import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.*;

import static com.github.javaparser.GeneratedJavaParser.CustomToken;
import static com.github.javaparser.Position.pos;
import static com.github.javaparser.ast.type.ArrayType.unwrapArrayTypes;
import static com.github.javaparser.ast.type.ArrayType.wrapInArrayTypes;

Expand Down Expand Up @@ -148,4 +144,63 @@ static JavaToken nodeListBegin(NodeList<?> l) {
}
return l.get(0).getTokenRange().get().getBegin();
}

/**
* This is the code from ParseException.initialise, modified to be more horizontal.
*/
static String makeMessageForParseException(ParseException exception) {
final StringBuilder sb = new StringBuilder("Parse error. Found ");
final StringBuilder expected = new StringBuilder();

int maxExpectedTokenSequenceLength = 0;
TreeSet<String> sortedOptions = new TreeSet<>();
for (int i = 0; i < exception.expectedTokenSequences.length; i++) {
if (maxExpectedTokenSequenceLength < exception.expectedTokenSequences[i].length) {
maxExpectedTokenSequenceLength = exception.expectedTokenSequences[i].length;
}
for (int j = 0; j < exception.expectedTokenSequences[i].length; j++) {
sortedOptions.add(exception.tokenImage[exception.expectedTokenSequences[i][j]]);
}
}

for (String option : sortedOptions) {
expected.append(" ").append(option);
}

sb.append("");

Token token = exception.currentToken.next;
for (int i = 0; i < maxExpectedTokenSequenceLength; i++) {
String tokenText = token.image;
String escapedTokenText = ParseException.add_escapes(tokenText);
if (i != 0) {
sb.append(" ");
}
if (token.kind == 0) {
sb.append(exception.tokenImage[0]);
break;
}
escapedTokenText = "\"" + escapedTokenText + "\"";
String image = exception.tokenImage[token.kind];
if (image.equals(escapedTokenText)) {
sb.append(image);
} else {
sb.append(" ")
.append(escapedTokenText)
.append(" ")
.append(image);
}
token = token.next;
}

if (exception.expectedTokenSequences.length != 0) {
int numExpectedTokens = exception.expectedTokenSequences.length;
sb.append(", expected")
.append(numExpectedTokens == 1 ? "" : " one of ")
.append(expected.toString());
}
return sb.toString();

}

}
Expand Up @@ -53,6 +53,7 @@ public class MetaModelGenerator {
//
add(ArrayCreationLevel.class);
add(CompilationUnit.class);
add(BadCompilationUnit.class);
add(PackageDeclaration.class);

add(AnnotationDeclaration.class);
Expand Down
Expand Up @@ -40,17 +40,19 @@ public void whenParsingSucceedsThenWeGetResultsAndNoProblems() {
ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class X{}"));

assertThat(result.getResult().isPresent()).isTrue();
assertThat(result.getResult().get().isBad()).isFalse();
assertThat(result.getProblems()).isEmpty();
assertThat(result.getTokens().isPresent()).isTrue();

assertThat(result.toString()).isEqualTo("Parsing successful");
}

@Test
public void whenParsingFailsThenWeGetProblemsAndNoResults() {
public void whenParsingFailsThenWeGetProblemsAndABadResult() {
ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider("class {"));

assertThat(result.getResult().isPresent()).isFalse();
assertThat(result.getResult().isPresent()).isTrue();
assertThat(result.getResult().get().isBad()).isTrue();
assertThat(result.getProblems().size()).isEqualTo(1);

Problem problem = result.getProblem(0);
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -3,8 +3,8 @@
<modules>
<module>javaparser-core</module>
<module>javaparser-testing</module>
<module>javaparser-core-generators</module>
<module>javaparser-metamodel-generator</module>
<!--<module>javaparser-core-generators</module>-->
<!--<module>javaparser-metamodel-generator</module>-->
</modules>

<groupId>com.github.javaparser</groupId>
Expand Down

0 comments on commit 05f07e4

Please sign in to comment.