Skip to content

Commit

Permalink
Format the code taken from the grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
matozoid committed Nov 3, 2017
1 parent cde534d commit 8681e0a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
Expand Up @@ -26,13 +26,16 @@
abstract class GeneratedJavaParserBase { abstract class GeneratedJavaParserBase {
//// Interface with the generated code //// Interface with the generated code
abstract GeneratedJavaParserTokenManager getTokenSource(); abstract GeneratedJavaParserTokenManager getTokenSource();

abstract void ReInit(Provider provider); abstract void ReInit(Provider provider);

/* Returns the JavaParser specific token type of the last matched token */ /* Returns the JavaParser specific token type of the last matched token */
abstract JavaToken token(); abstract JavaToken token();

abstract Token getNextToken(); abstract Token getNextToken();


//// ////

/* The problems encountered while parsing */ /* The problems encountered while parsing */
List<Problem> problems = new ArrayList<>(); List<Problem> problems = new ArrayList<>();
/* Configuration flag whether we store tokens and tokenranges */ /* Configuration flag whether we store tokens and tokenranges */
Expand All @@ -46,8 +49,7 @@ void reset(Provider provider) {
} }


/** /**
* Return the list of JavaParser specific tokens that have been encountered while * Return the list of JavaParser specific tokens that have been encountered while parsing code using this parser.
* parsing code using this parser.
* *
* @return a list of tokens * @return a list of tokens
*/ */
Expand All @@ -68,7 +70,7 @@ void addProblem(String message) {


/* Returns a tokenRange that spans the last matched token */ /* Returns a tokenRange that spans the last matched token */
TokenRange tokenRange() { TokenRange tokenRange() {
if(storeTokens) { if (storeTokens) {
return new TokenRange(token(), token()); return new TokenRange(token(), token());
} }
return null; return null;
Expand All @@ -78,7 +80,7 @@ TokenRange tokenRange() {
* Return a TokenRange spanning from begin to end * Return a TokenRange spanning from begin to end
*/ */
TokenRange range(JavaToken begin, JavaToken end) { TokenRange range(JavaToken begin, JavaToken end) {
if(storeTokens) { if (storeTokens) {
return new TokenRange(begin, end); return new TokenRange(begin, end);
} }
return null; return null;
Expand All @@ -88,7 +90,7 @@ TokenRange range(JavaToken begin, JavaToken end) {
* Return a TokenRange spanning from begin to end * Return a TokenRange spanning from begin to end
*/ */
TokenRange range(Node begin, JavaToken end) { TokenRange range(Node begin, JavaToken end) {
if(storeTokens) { if (storeTokens) {
return new TokenRange(begin.getTokenRange().get().getBegin(), end); return new TokenRange(begin.getTokenRange().get().getBegin(), end);
} }
return null; return null;
Expand All @@ -98,7 +100,7 @@ TokenRange range(Node begin, JavaToken end) {
* Return a TokenRange spanning from begin to end * Return a TokenRange spanning from begin to end
*/ */
TokenRange range(JavaToken begin, Node end) { TokenRange range(JavaToken begin, Node end) {
if(storeTokens) { if (storeTokens) {
return new TokenRange(begin, end.getTokenRange().get().getEnd()); return new TokenRange(begin, end.getTokenRange().get().getEnd());
} }
return null; return null;
Expand All @@ -108,7 +110,7 @@ TokenRange range(JavaToken begin, Node end) {
* Return a TokenRange spanning from begin to end * Return a TokenRange spanning from begin to end
*/ */
TokenRange range(Node begin, Node end) { TokenRange range(Node begin, Node end) {
if(storeTokens) { if (storeTokens) {
return new TokenRange(begin.getTokenRange().get().getBegin(), end.getTokenRange().get().getEnd()); return new TokenRange(begin.getTokenRange().get().getBegin(), end.getTokenRange().get().getEnd());
} }
return null; return null;
Expand All @@ -118,7 +120,7 @@ TokenRange range(Node begin, Node end) {
* @return secondChoice if firstChoice is JavaToken.UNKNOWN, otherwise firstChoice * @return secondChoice if firstChoice is JavaToken.UNKNOWN, otherwise firstChoice
*/ */
JavaToken orIfInvalid(JavaToken firstChoice, JavaToken secondChoice) { JavaToken orIfInvalid(JavaToken firstChoice, JavaToken secondChoice) {
if(storeTokens) { if (storeTokens) {
assertNotNull(firstChoice); assertNotNull(firstChoice);
assertNotNull(secondChoice); assertNotNull(secondChoice);
if (firstChoice.valid() || secondChoice.invalid()) { if (firstChoice.valid() || secondChoice.invalid()) {
Expand All @@ -133,7 +135,7 @@ JavaToken orIfInvalid(JavaToken firstChoice, JavaToken secondChoice) {
* @return the begin-token secondChoice if firstChoice is JavaToken.UNKNOWN, otherwise firstChoice * @return the begin-token secondChoice if firstChoice is JavaToken.UNKNOWN, otherwise firstChoice
*/ */
JavaToken orIfInvalid(JavaToken firstChoice, Node secondChoice) { JavaToken orIfInvalid(JavaToken firstChoice, Node secondChoice) {
if(storeTokens) { if (storeTokens) {
return orIfInvalid(firstChoice, secondChoice.getTokenRange().get().getBegin()); return orIfInvalid(firstChoice, secondChoice.getTokenRange().get().getBegin());
} }
return null; return null;
Expand All @@ -156,7 +158,7 @@ void setTokenKind(int newKind) {


/* Makes the parser keep a list of tokens */ /* Makes the parser keep a list of tokens */
void setStoreTokens(boolean storeTokens) { void setStoreTokens(boolean storeTokens) {
this.storeTokens=storeTokens; this.storeTokens = storeTokens;
getTokenSource().setStoreTokens(storeTokens); getTokenSource().setStoreTokens(storeTokens);
} }


Expand All @@ -170,13 +172,13 @@ TokenRange recover(int recoveryTokenType, ParseException p) {
Token t; Token t;
do { do {
t = getNextToken(); t = getNextToken();
} while(t.kind != recoveryTokenType && t.kind!=EOF); } while (t.kind != recoveryTokenType && t.kind != EOF);


JavaToken end = token(); JavaToken end = token();


TokenRange tokenRange=null; TokenRange tokenRange = null;
if(begin!=null && end!=null){ if (begin != null && end != null) {
tokenRange=range(begin, end); tokenRange = range(begin, end);
} }


problems.add(new Problem(makeMessageForParseException(p), tokenRange, p)); problems.add(new Problem(makeMessageForParseException(p), tokenRange, p));
Expand All @@ -186,7 +188,7 @@ TokenRange recover(int recoveryTokenType, ParseException p) {
/** /**
* Quickly create a new NodeList * Quickly create a new NodeList
*/ */
<X extends Node> NodeList<X> emptyList() { <T extends Node> NodeList<T> emptyList() {
return new NodeList<>(); return new NodeList<>();
} }


Expand Down
Expand Up @@ -5,12 +5,12 @@
import com.github.javaparser.ast.comments.JavadocComment; import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.comments.LineComment; import com.github.javaparser.ast.comments.LineComment;


import static com.github.javaparser.GeneratedJavaParserConstants.JAVADOC_COMMENT; import static com.github.javaparser.GeneratedJavaParserConstants.*;
import static com.github.javaparser.GeneratedJavaParserConstants.MULTI_LINE_COMMENT;
import static com.github.javaparser.GeneratedJavaParserConstants.SINGLE_LINE_COMMENT;
import static com.github.javaparser.Position.pos; import static com.github.javaparser.Position.pos;


/** Base class for {@link com.github.javaparser.GeneratedJavaParserTokenManager} */ /**
* Base class for {@link com.github.javaparser.GeneratedJavaParserTokenManager}
*/
public class GeneratedJavaParserTokenManagerBase { public class GeneratedJavaParserTokenManagerBase {
/** /**
* Create a TokenRange that spans exactly one token * Create a TokenRange that spans exactly one token
Expand All @@ -20,9 +20,9 @@ private static TokenRange tokenRange(Token token) {
return new TokenRange(javaToken, javaToken); return new TokenRange(javaToken, javaToken);
} }


/** /**
* Since comments are completely captured in a single token, including their delimiters, * Since comments are completely captured in a single token, including their delimiters, deconstruct them here so we
* deconstruct them here so we can turn them into nodes later on. * can turn them into nodes later on.
*/ */
static Comment createCommentFromToken(Token token) { static Comment createCommentFromToken(Token token) {
String commentText = token.image; String commentText = token.image;
Expand Down
Expand Up @@ -12,13 +12,13 @@
* Helper class for {@link GeneratedJavaParser} * Helper class for {@link GeneratedJavaParser}
*/ */
class ModifierHolder { class ModifierHolder {
final EnumSet<Modifier> modifiers; final EnumSet<Modifier> modifiers;
final NodeList<AnnotationExpr> annotations; final NodeList<AnnotationExpr> annotations;
final JavaToken begin; final JavaToken begin;


public ModifierHolder(JavaToken begin, EnumSet<Modifier> modifiers, NodeList<AnnotationExpr> annotations) { ModifierHolder(JavaToken begin, EnumSet<Modifier> modifiers, NodeList<AnnotationExpr> annotations) {
this.begin = begin; this.begin = begin;
this.modifiers = assertNotNull(modifiers); this.modifiers = assertNotNull(modifiers);
this.annotations = annotations; this.annotations = annotations;
}
} }
}
Expand Up @@ -2,10 +2,16 @@


import static com.github.javaparser.GeneratedJavaParserConstants.GT; import static com.github.javaparser.GeneratedJavaParserConstants.GT;


/** Base class for the generated {@link Token} */ /**
* Base class for the generated {@link Token}
*/
class TokenBase { class TokenBase {
/** For tracking the >> >>> ambiguity. */ /**
* For tracking the >> >>> ambiguity.
*/
int realKind = GT; int realKind = GT;
/** This is the link to the token that JavaParser presents to the user */ /**
* This is the link to the token that JavaParser presents to the user
*/
JavaToken javaToken; JavaToken javaToken;
} }

0 comments on commit 8681e0a

Please sign in to comment.