Skip to content

Commit

Permalink
CSM refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Feb 22, 2017
1 parent 6366706 commit ae0540a
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 9 deletions.
Expand Up @@ -24,6 +24,7 @@
import com.github.javaparser.ASTParserConstants;
import com.github.javaparser.ast.*;
import com.github.javaparser.ast.body.*;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.expr.*;
import com.github.javaparser.ast.observer.*;
import com.github.javaparser.ast.observer.Observable;
Expand Down Expand Up @@ -166,7 +167,7 @@ private static CsmElement typeArguments() {
comment(),
memberAnnotations(),
modifiers(),
conditional(ObservableProperty.DEFAULT, FLAG, sequence(token(ASTParserConstants.DEFAULT), space())),
conditional(ObservableProperty.DEFAULT, FLAG, sequence(token(ASTParserConstants._DEFAULT), space())),
typeParameters(),
child(ObservableProperty.TYPE),
space(),
Expand Down Expand Up @@ -745,6 +746,53 @@ private static CsmElement typeArguments() {
annotations(),
CsmElement.list(ObservableProperty.ELEMENTS, CsmElement.sequence(CsmElement.space(), CsmElement.token(ASTParserConstants.BIT_OR), CsmElement.space()))
));

concreteSyntaxModelByClass.put(AnnotationDeclaration.class, CsmElement.sequence(
CsmElement.comment(),
memberAnnotations(),
modifiers(),
CsmElement.token(ASTParserConstants.AT),
CsmElement.token(ASTParserConstants.INTERFACE),
CsmElement.space(),
CsmElement.child(ObservableProperty.NAME),
CsmElement.space(),
CsmElement.token(LBRACE),
CsmElement.newline(),
CsmElement.indent(),
CsmElement.list(ObservableProperty.MEMBERS, CsmElement.newline(), CsmElement.none(), CsmElement.none(), CsmElement.newline()),
CsmElement.unindent(),
CsmElement.token(RBRACE)
));

concreteSyntaxModelByClass.put(AnnotationMemberDeclaration.class, CsmElement.sequence(
CsmElement.comment(),
memberAnnotations(),
modifiers(),
CsmElement.child(ObservableProperty.TYPE),
CsmElement.space(),
CsmElement.child(ObservableProperty.NAME),
CsmElement.token(LPAREN),
CsmElement.token(RPAREN),
CsmElement.conditional(ObservableProperty.DEFAULT_VALUE, IS_PRESENT, CsmElement.sequence(CsmElement.space(), CsmElement.token(ASTParserConstants._DEFAULT), CsmElement.space(), CsmElement.child(DEFAULT_VALUE))),
CsmElement.semicolon()
));
}

private static class JavadocContentTokenCalculator implements CsmToken.TokenContentCalculator {
@Override
public String calculate(Node node) {
return "/**" + ((JavadocComment)node).getContent() + "*";
}

@Override
public int hashCode() {
return 1;
}

@Override
public boolean equals(Object obj) {
return obj instanceof JavadocContentTokenCalculator;
}
}

private ConcreteSyntaxModel() {
Expand Down
Expand Up @@ -21,12 +21,17 @@

package com.github.javaparser.printer.concretesyntaxmodel;

import com.github.javaparser.ASTParserConstants;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.printer.ConcreteSyntaxModel;
import com.github.javaparser.printer.SourcePrinter;

public class CsmAttribute implements CsmElement {
public ObservableProperty getProperty() {
return property;
}

private ObservableProperty property;

public CsmAttribute(ObservableProperty property) {
Expand All @@ -38,4 +43,18 @@ public void prettyPrint(Node node, SourcePrinter printer) {
Object value = property.singleValueFor(node);
printer.print(PrintingHelper.printToString(value));
}

public int getTokenType(String text) {
if (property == ObservableProperty.IDENTIFIER) {
return ASTParserConstants.IDENTIFIER;
}
if (property == ObservableProperty.TYPE) {
for (int i=0;i<ASTParserConstants.tokenImage.length;i++) {
if (ASTParserConstants.tokenImage[i].equals("\"" + text.toLowerCase() + "\"")) {
return i;
}
}
}
throw new UnsupportedOperationException(property.name()+ " " + text);
}
}
Expand Up @@ -35,6 +35,22 @@ public class CsmConditional implements CsmElement {
private CsmElement thenElement;
private CsmElement elseElement;

public Condition getCondition() {
return condition;
}

public ObservableProperty getProperty() {
return property;
}

public CsmElement getThenElement() {
return thenElement;
}

public CsmElement getElseElement() {
return elseElement;
}

public enum Condition {
IS_EMPTY,
IS_NOT_EMPTY,
Expand All @@ -43,7 +59,7 @@ public enum Condition {

boolean evaluate(Node node, ObservableProperty property){
if (this == IS_PRESENT) {
return !property.isNullOrEmpty(node);
return !property.isNullOrNotPresent(node);
}
if (this == FLAG) {
return (Boolean)property.singleValueFor(node);
Expand Down
Expand Up @@ -67,6 +67,10 @@ static CsmElement token(int tokenType) {
return new CsmToken(tokenType);
}

static CsmElement token(int tokenType, CsmToken.TokenContentCalculator tokenContentCalculator) {
return new CsmToken(tokenType, tokenContentCalculator);
}

static CsmElement conditional(ObservableProperty property, CsmConditional.Condition condition, CsmElement thenElement) {
return new CsmConditional(property, condition, thenElement);
}
Expand All @@ -76,7 +80,7 @@ static CsmElement conditional(ObservableProperty property, CsmConditional.Condit
}

static CsmElement space() {
return new CsmToken(32, " ");
return new CsmToken(1, " ");
}

static CsmElement semicolon() {
Expand Down
Expand Up @@ -37,14 +37,32 @@ public class CsmList implements CsmElement {
private CsmElement preceeding;
private CsmElement following;

public ObservableProperty getProperty() {
return property;
}

public CsmElement getSeparatorPost() {
return separatorPost;
}

public CsmElement getSeparatorPre() {
return separatorPre;
}

public CsmElement getPreceeding() {
return preceeding;
}

public CsmElement getFollowing() {
return following;
}

public CsmList(ObservableProperty property, CsmElement separator) {
this.property = property;
this.separatorPost = separator;
this(property, new CsmNone(), separator, new CsmNone(), new CsmNone());
}

public CsmList(ObservableProperty property) {
this.property = property;
this.separatorPost = null;
this(property, new CsmNone(), new CsmNone(), new CsmNone(), new CsmNone());
}

public CsmList(ObservableProperty property, CsmElement separatorPre, CsmElement separatorPost, CsmElement preceeding, CsmElement following) {
Expand Down
Expand Up @@ -39,6 +39,10 @@ public CsmSequence(List<CsmElement> elements) {
this.elements = elements;
}

public List<CsmElement> getElements() {
return elements;
}

@Override
public void prettyPrint(Node node, SourcePrinter printer) {
elements.forEach(e -> e.prettyPrint(node, printer));
Expand Down
Expand Up @@ -27,9 +27,13 @@
import com.github.javaparser.printer.SourcePrinter;


class CsmSingleReference implements CsmElement {
public class CsmSingleReference implements CsmElement {
private ObservableProperty property;

public ObservableProperty getProperty() {
return property;
}

public CsmSingleReference(ObservableProperty property) {
this.property = property;
}
Expand Down
Expand Up @@ -31,25 +31,82 @@
public class CsmToken implements CsmElement {
private int tokenType;
private String content;
private TokenContentCalculator tokenContentCalculator;

public interface TokenContentCalculator {
String calculate(Node node);
}

public int getTokenType() {
return tokenType;
}

public String getContent(Node node) {
if (tokenContentCalculator != null) {
return tokenContentCalculator.calculate(node);
}
return content;
}

public CsmToken(int tokenType) {

this.tokenType = tokenType;
this.content = ASTParserConstants.tokenImage[tokenType];
if (content.startsWith("\"")) {
content = content.substring(1, content.length() - 1);
}
if (tokenType == 3) {
content = "\n";
} else if (tokenType == 32) {
content = " ";
}
}

public CsmToken(int tokenType, String content) {
this.tokenType = tokenType;
this.content = content;
}

public CsmToken(int tokenType, TokenContentCalculator tokenContentCalculator) {
this.tokenType = tokenType;
this.tokenContentCalculator = tokenContentCalculator;
}

@Override
public void prettyPrint(Node node, SourcePrinter printer) {
if (tokenType == 3) {
printer.println();
} else {
printer.print(content);
printer.print(getContent(node));
}
}

@Override
public String toString() {
return "token(" + ASTParserConstants.tokenImage[tokenType]+ ")";
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

CsmToken csmToken = (CsmToken) o;

if (tokenType != csmToken.tokenType) return false;
if (content != null ? !content.equals(csmToken.content) : csmToken.content != null) return false;
return tokenContentCalculator != null ? tokenContentCalculator.equals(csmToken.tokenContentCalculator) : csmToken.tokenContentCalculator == null;
}

@Override
public int hashCode() {
int result = tokenType;
result = 31 * result + (content != null ? content.hashCode() : 0);
result = 31 * result + (tokenContentCalculator != null ? tokenContentCalculator.hashCode() : 0);
return result;
}

public boolean isWhiteSpace() {
return tokenType == 3 || tokenType == 1 || tokenType == 0 || tokenType == 31 || tokenType == 32;
}
}

0 comments on commit ae0540a

Please sign in to comment.