Skip to content

Commit

Permalink
Make a VarType and a Java 10 language level. Make a postprocessing fr…
Browse files Browse the repository at this point in the history
…amework so "var" can be turned into a VarType.
  • Loading branch information
matozoid committed Feb 3, 2018
1 parent 6036e99 commit d7bb34f
Show file tree
Hide file tree
Showing 29 changed files with 461 additions and 33 deletions.
Expand Up @@ -23,30 +23,55 @@

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.validator.Java8Validator;
import com.github.javaparser.ast.validator.ProblemReporter;
import com.github.javaparser.ast.validator.Validator;
import com.github.javaparser.ast.validator.*;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;
import com.github.javaparser.resolution.SymbolResolver;
import com.github.javaparser.version.Java10Processor;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static com.github.javaparser.utils.Utils.assertNotNull;

/**
* The configuration that is used by the parser.
* Note that this can be changed even when reusing the same JavaParser instance.
* It will pick up the changes.
*/
public class ParserConfiguration {
public enum LanguageLevel {
ANY(null, null),
JAVA_1_0(new Java1_0Validator(), null),
JAVA_1_1(new Java1_1Validator(), null),
JAVA_1_2(new Java1_2Validator(), null),
JAVA_1_3(new Java1_3Validator(), null),
JAVA_1_4(new Java1_4Validator(), null),
JAVA_5(new Java5Validator(), null),
JAVA_6(new Java6Validator(), null),
JAVA_7(new Java7Validator(), null),
JAVA_8(new Java8Validator(), null),
JAVA_9(new Java9Validator(), null),
JAVA_10(null, new Java10Processor());

final Validator validator;
final ParseResult.PostProcessor postProcessor;

LanguageLevel(Validator validator, ParseResult.PostProcessor postProcessor) {
this.validator = validator;
this.postProcessor = postProcessor;
}
}

private boolean storeTokens = true;
private boolean attributeComments = true;
private boolean doNotAssignCommentsPrecedingEmptyLines = true;
private boolean doNotConsiderAnnotationsAsNodeStartForCodeAttribution = false;
private boolean lexicalPreservationEnabled = false;
private SymbolResolver symbolResolver = null;
private int tabSize = 1;
private Validator validator = new Java8Validator();
private LanguageLevel languageLevel;

private final List<ParseResult.PostProcessor> postProcessors = new ArrayList<>();

public ParserConfiguration() {
Expand All @@ -64,16 +89,23 @@ public ParserConfiguration() {
new CommentsInserter(configuration).insertComments(resultNode, comments.copy().getComments())));
}
});
postProcessors.add((result, configuration) ->
getValidator().ifPresent(validator ->
validator.accept(result.getResult().get(), new ProblemReporter(newProblem -> result.getProblems().add(newProblem)))));
postProcessors.add((result, configuration) -> {
LanguageLevel languageLevel = getLanguageLevel();
if (languageLevel.postProcessor != null) {
languageLevel.postProcessor.process(result, configuration);
}
if (languageLevel.validator != null) {
languageLevel.validator.accept(result.getResult().get(), new ProblemReporter(newProblem -> result.getProblems().add(newProblem)));
}
});
postProcessors.add((result, configuration) -> configuration.getSymbolResolver().ifPresent(symbolResolver ->
result.ifSuccessful(resultNode -> {
if (resultNode instanceof CompilationUnit) {
resultNode.setData(Node.SYMBOL_RESOLVER_KEY, symbolResolver);
}
})
));
setLanguageLevel(LanguageLevel.JAVA_8);
}

public boolean isAttributeComments() {
Expand Down Expand Up @@ -132,18 +164,20 @@ public ParserConfiguration setTabSize(int tabSize) {
return this;
}

/**
* @deprecated use getLanguageLevel
*/
@Deprecated
public Optional<Validator> getValidator() {
return Optional.of(validator);
throw new IllegalStateException("method is deprecated");
}

/**
* The language level validator to run directly after parsing.
* By default it is {@link Java8Validator}
* If it is null, all validation is turned off and only hard parse errors will be reported.
* @deprecated use setLanguageLevel, or getPostProcessors if you use a custom validator.
*/
@Deprecated
public ParserConfiguration setValidator(Validator validator) {
this.validator = validator;
return this;
throw new IllegalStateException("method is deprecated");
}

/**
Expand Down Expand Up @@ -178,4 +212,13 @@ public ParserConfiguration setSymbolResolver(SymbolResolver symbolResolver) {
public List<ParseResult.PostProcessor> getPostProcessors() {
return postProcessors;
}

public ParserConfiguration setLanguageLevel(LanguageLevel languageLevel) {
this.languageLevel = assertNotNull(languageLevel);
return this;
}

public LanguageLevel getLanguageLevel() {
return languageLevel;
}
}
Expand Up @@ -108,11 +108,13 @@ public enum ObserverRegistrationMode {
/**
* Notify exclusively for changes happening on this node alone.
*/
JUST_THIS_NODE, /**
JUST_THIS_NODE,
/**
* Notify for changes happening on this node and all its descendants existing at the moment in
* which the observer was registered. Nodes attached later will not be observed.
*/
THIS_NODE_AND_EXISTING_DESCENDANTS, /**
THIS_NODE_AND_EXISTING_DESCENDANTS,
/**
* Notify for changes happening on this node and all its descendants. The descendants existing at the moment in
* which the observer was registered will be observed immediately. As new nodes are attached later they are
* automatically registered to be observed.
Expand Down
Expand Up @@ -24,19 +24,14 @@
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;

import static com.github.javaparser.utils.Utils.assertNotNull;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.visitor.CloneVisitor;
import com.github.javaparser.metamodel.AssignExprMetaModel;
import com.github.javaparser.metamodel.JavaParserMetaModel;
import com.github.javaparser.printer.Printable;

import javax.annotation.Generated;

import com.github.javaparser.TokenRange;

import java.util.function.Consumer;
import java.util.Optional;

Expand Down Expand Up @@ -78,7 +73,7 @@ public String asString() {
}

public Optional<BinaryExpr.Operator> toBinaryOperator() {
switch (this) {
switch(this) {
case PLUS:
return Optional.of(BinaryExpr.Operator.PLUS);
case MINUS:
Expand Down
Expand Up @@ -24,19 +24,14 @@
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;

import static com.github.javaparser.utils.Utils.assertNotNull;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.visitor.CloneVisitor;
import com.github.javaparser.metamodel.BinaryExprMetaModel;
import com.github.javaparser.metamodel.JavaParserMetaModel;
import com.github.javaparser.printer.Printable;

import javax.annotation.Generated;

import com.github.javaparser.TokenRange;

import java.util.function.Consumer;
import java.util.Optional;

Expand Down Expand Up @@ -83,7 +78,7 @@ public String asString() {
}

public Optional<AssignExpr.Operator> toAssignOperator() {
switch (this) {
switch(this) {
case BINARY_OR:
return Optional.of(AssignExpr.Operator.BINARY_OR);
case BINARY_AND:
Expand Down
Expand Up @@ -32,8 +32,6 @@
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static com.github.javaparser.utils.Utils.assertNotNull;
import com.github.javaparser.ast.Node;
Expand All @@ -44,7 +42,6 @@
import com.github.javaparser.TokenRange;
import com.github.javaparser.metamodel.OptionalProperty;
import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import java.util.function.Consumer;

/**
Expand Down
Expand Up @@ -61,7 +61,8 @@ public enum Origin {
/**
* The [] were found on the name, like "int a[]" or "String abc()[][]"
*/
NAME, /**
NAME,
/**
* The [] were found on the type, like "int[] a" or "String[][] abc()"
*/
TYPE
Expand Down
Expand Up @@ -349,4 +349,23 @@ public Optional<VoidType> toVoidType() {
public Optional<WildcardType> toWildcardType() {
return Optional.empty();
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public boolean isVarType() {
return false;
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public VarType asVarType() {
throw new IllegalStateException(f("%s is not an VarType", this));
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public Optional<VarType> toVarType() {
return Optional.empty();
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public void ifVarType(Consumer<VarType> action) {
}
}
@@ -0,0 +1,133 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2016 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.ast.type;

import com.github.javaparser.TokenRange;
import com.github.javaparser.ast.AllFieldsConstructor;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations;
import com.github.javaparser.ast.visitor.CloneVisitor;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
import com.github.javaparser.metamodel.JavaParserMetaModel;
import com.github.javaparser.metamodel.VoidTypeMetaModel;
import com.github.javaparser.resolution.types.ResolvedVoidType;
import javax.annotation.Generated;
import java.util.Optional;
import java.util.function.Consumer;
import com.github.javaparser.metamodel.VarTypeMetaModel;

public final class VarType extends Type {

@AllFieldsConstructor
public VarType() {
this(null);
}

/**
* This constructor is used by the parser and is considered private.
*/
@Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
public VarType(TokenRange tokenRange) {
super(tokenRange);
customInitialization();
}

@Override
public VarType setAnnotations(NodeList<AnnotationExpr> annotations) {
return (VarType) super.setAnnotations(annotations);
}

@Override
@Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator")
public boolean remove(Node node) {
if (node == null)
return false;
return super.remove(node);
}

@Override
public String asString() {
return "var";
}

@Override
@Generated("com.github.javaparser.generator.core.node.CloneGenerator")
public VarType clone() {
return (VarType) accept(new CloneVisitor(), null);
}

@Override
@Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator")
public VarTypeMetaModel getMetaModel() {
return JavaParserMetaModel.varTypeMetaModel;
}

@Override
@Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator")
public boolean replace(Node node, Node replacementNode) {
if (node == null)
return false;
return super.replace(node, replacementNode);
}

@Override
public ResolvedVoidType resolve() {
return getSymbolResolver().toResolvedType(this, ResolvedVoidType.class);
}

@Override
@Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) {
return v.visit(this, arg);
}

@Override
@Generated("com.github.javaparser.generator.core.node.AcceptGenerator")
public <A> void accept(final VoidVisitor<A> v, final A arg) {
v.visit(this, arg);
}

@Override
@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public boolean isVarType() {
return true;
}

@Override
@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public VarType asVarType() {
return this;
}

@Override
@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public Optional<VarType> toVarType() {
return Optional.of(this);
}

@Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator")
public void ifVarType(Consumer<VarType> action) {
action.accept(this);
}
}

0 comments on commit d7bb34f

Please sign in to comment.