Commit 3f132b86 authored by Alexander Weigl's avatar Alexander Weigl

started pretty printing

* cleanin up and introducing some new AST nodes
parent cef9b784
......@@ -99,9 +99,12 @@ theOnlyStmt
;
scriptCommand
: ID ((ID '=')? expression)* SEMICOLON
: cmd=ID parameter* SEMICOLON
;
parameter : ((pname=ID '=')? expr=expression)
;
callStmt
: CALL scriptCommand SEMICOLON
;
......
......@@ -2,24 +2,37 @@ package edu.kit.formatl.proofscriptparser;
import edu.kit.formal.proofscriptparser.ScriptLanguageLexer;
import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
import edu.kit.formatl.proofscriptparser.ast.ASTNode;
import edu.kit.formatl.proofscriptparser.ast.ProofScript;
import org.antlr.v4.runtime.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
* @author Alexander Weigl
* @version 1 (27.04.17)
*/
public class Facade {
public ScriptLanguageParser.StartContext parseStream(CharStream stream) {
public final class Facade {
public static ScriptLanguageParser.StartContext parseStream(CharStream stream) {
ScriptLanguageParser slp = new ScriptLanguageParser(new CommonTokenStream(new ScriptLanguageLexer(stream)));
return slp.start();
}
public List<ProofScript> asAST(CharStream stream) {
public static List<ProofScript> getAST(CharStream stream) {
TransformAst astt = new TransformAst();
parseStream(stream).accept(astt);
return astt.getScripts();
}
public static List<ProofScript> getAST(File inputfile) throws IOException {
return getAST(CharStreams.fromPath(inputfile.toPath()));
}
public static String prettyPrint(ASTNode node) {
PrettyPrinter prettyPrinter = new PrettyPrinter();
node.accept(prettyPrinter);
return prettyPrinter.toString();
}
}
package edu.kit.formatl.proofscriptparser;
import edu.kit.formatl.proofscriptparser.ast.*;
import java.util.Iterator;
import java.util.Map;
/**
* @author Alexander Weigl
* @version 1 (28.04.17)
*/
public class PrettyPrinter extends DefaultASTVisitor<Void> {
private final StringBuilder s = new StringBuilder();
private int indentation = 0;
@Override public String toString() {
return s.toString();
}
@Override public Void visit(ProofScript proofScript) {
s.append("script");
s.append(proofScript.getName());
s.append(" (");
printArglist(proofScript.getParameters());
s.append(") {");
proofScript.getBody().accept(this);
nl();
s.append("}");
return null;
}
private void printArglist(Map<String, String> parameters) {
Iterator<Map.Entry<String, String>> iter = parameters.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> next = iter.next();
s.append(next.getKey()).append(" : ").append(next.getValue());
if (iter.hasNext())
s.append(", ");
}
}
@Override public Void visit(AssignmentStatement assign) {
assign.getRhs().accept(this);
s.append(" := ");
assign.getLhs().accept(this);
s.append(";");
return null;
}
@Override public Void visit(BinaryExpression e) {
e.getLeft().accept(this);
s.append(e.getOperator().symbol());
e.getRight().accept(this);
return super.visit(e);
}
@Override public Void visit(MatchExpression matchExpression) {
return super.visit(matchExpression);
}
@Override public Void visit(CasesStatement casesStatement) {
s.append("cases {");
incrementIndent();
nl();
for (CaseStatement c : casesStatement.getCases()) {
c.accept(this);
nl();
}
decrementIndent();
cl();
s.append("}");
return null;
}
/**
* clear line
*/
private void cl() {
int i = s.length() - 1;
while (Character.isWhitespace(s.charAt(i))) {
s.deleteCharAt(i--);
}
nl();
}
@Override public Void visit(CaseStatement caseStatement) {
s.append("case ");
caseStatement.getGuard().accept(this);
s.append(" {");
caseStatement.getBody().accept(this);
nl();
s.append("}");
return super.visit(caseStatement);
}
@Override public Void visit(ScriptCallStatement call) {
s.append(call.getCommand()).append(' ');
call.getParameters().forEach((k, v) -> {
s.append(k).append(" = ");
v.accept(this);
s.append(" ");
});
s.append(";");
return null;
}
@Override public Void visit(TermLiteral termLiteral) {
s.append(String.format("`%s`", termLiteral.getText()));
return super.visit(termLiteral);
}
@Override public Void visit(StringLiteral stringLiteral) {
s.append(String.format("\"%s\"", stringLiteral.getText()));
return super.visit(stringLiteral);
}
@Override public Void visit(Variable variable) {
s.append(variable.getIdentifier());
return super.visit(variable);
}
@Override public Void visit(BooleanLiteral booleanLiteral) {
s.append(booleanLiteral.getValue());
return super.visit(booleanLiteral);
}
@Override public Void visit(Statements statements) {
incrementIndent();
for (Statement s : statements) {
nl();
s.accept(this);
}
decrementIndent();
return super.visit(statements);
}
@Override public Void visit(IntegerLiteral integer) {
s.append(integer.getValue().toString());
return null;
}
private void nl() {
s.append('\n');
for (int i = 0; i < indentation; i++)
s.append('\t');
}
private void decrementIndent() {
indentation--;
}
private void incrementIndent() {
indentation++;
}
}
......@@ -23,7 +23,8 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
ProofScript s = new ProofScript();
s.setName(ctx.name.getText());
s.setRuleContext(ctx);
s.setParameters((Map<String, String>) ctx.paramters.accept(this));
if (ctx.paramters != null)
s.setParameters((Map<String, String>) ctx.paramters.accept(this));
s.setBody((Statements) ctx.body.accept(this));
scripts.add(s);
return s;
......@@ -38,11 +39,11 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
}
@Override public Object visitVarDecl(ScriptLanguageParser.VarDeclContext ctx) {
return null;
throw new IllegalStateException("not implemented");
}
@Override public Object visitType(ScriptLanguageParser.TypeContext ctx) {
return null;
throw new IllegalStateException("not implemented");
}
@Override public Statements visitStmtList(ScriptLanguageParser.StmtListContext ctx) {
......@@ -60,7 +61,7 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
@Override public Object visitAssignment(ScriptLanguageParser.AssignmentContext ctx) {
AssignmentStatement assign = new AssignmentStatement();
assign.setRuleContext(ctx);
assign.setRhs(ctx.variable.getText());
assign.setRhs(new Variable(ctx.variable));
assign.setLhs((Expression) ctx.expression().accept(this));
return assign;
}
......@@ -79,11 +80,11 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
}
@Override public Object visitExprMinus(ScriptLanguageParser.ExprMinusContext ctx) {
return null;
throw new IllegalStateException("not implemented");
}
@Override public Object visitExprNegate(ScriptLanguageParser.ExprNegateContext ctx) {
return null;
throw new IllegalStateException("not implemented");
}
@Override public Object visitExprComparison(ScriptLanguageParser.ExprComparisonContext ctx) {
......@@ -95,7 +96,7 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
if (op.symbol().equals(n))
return op;
}
return null;
throw new IllegalStateException("Operator " + n + " not defined");
}
@Override public Object visitExprEquality(ScriptLanguageParser.ExprEqualityContext ctx) {
......@@ -141,11 +142,11 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
}
@Override public Object visitLiteralID(ScriptLanguageParser.LiteralIDContext ctx) {
return null;
return new Variable(ctx.ID().getSymbol());
}
@Override public Object visitLiteralDigits(ScriptLanguageParser.LiteralDigitsContext ctx) {
return null;
return new IntegerLiteral(ctx.DIGITS().getSymbol());
}
@Override public Object visitLiteralTerm(ScriptLanguageParser.LiteralTermContext ctx) {
......@@ -178,50 +179,71 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
}
@Override public Object visitScriptVar(ScriptLanguageParser.ScriptVarContext ctx) {
return null;
throw new IllegalStateException("not implemented");
}
@Override public Object visitRepeatStmt(ScriptLanguageParser.RepeatStmtContext ctx) {
return null;
throw new IllegalStateException("not implemented");
}
@Override public Object visitCasesStmt(ScriptLanguageParser.CasesStmtContext ctx) {
return null;
CasesStatement cases = new CasesStatement();
ctx.casesList().forEach(c -> cases.getCases().add((CaseStatement) c.accept(this)));
return cases;
}
@Override public Object visitCasesList(ScriptLanguageParser.CasesListContext ctx) {
return null;
CaseStatement caseStatement = new CaseStatement();
caseStatement.setRuleContext(ctx);
caseStatement.setGuard((Expression) ctx.expression().accept(this));
caseStatement.setBody((Statements) ctx.stmtList().accept(this));
return caseStatement;
}
@Override public Object visitForEachStmt(ScriptLanguageParser.ForEachStmtContext ctx) {
return null;
throw new IllegalStateException("not implemented");
}
@Override public Object visitTheOnlyStmt(ScriptLanguageParser.TheOnlyStmtContext ctx) {
return null;
throw new IllegalStateException("not implemented");
}
@Override public Object visitScriptCommand(ScriptLanguageParser.ScriptCommandContext ctx) {
ScriptCallStatement scs = new ScriptCallStatement();
scs.setRuleContext(ctx);
scs.setCommand(ctx.cmd.getText());
int i = 1;
if (ctx.parameter() != null) {
for (ScriptLanguageParser.ParameterContext p : ctx.parameter()) {
Expression expr = (Expression) p.expr.accept(this);
String key = p.ID() != null ? p.ID().getText() : "#" + (i++);
scs.getParameters().put(key, expr);
}
}
return scs;
}
@Override public Object visitParameter(ScriptLanguageParser.ParameterContext ctx) {
return null;
}
@Override public Object visitCallStmt(ScriptLanguageParser.CallStmtContext ctx) {
return null;
throw new IllegalStateException("not implemented");
}
@Override public Object visit(ParseTree parseTree) {
return null;
throw new IllegalStateException("not implemented");
}
@Override public Object visitChildren(RuleNode ruleNode) {
return null;
throw new IllegalStateException("not implemented");
}
@Override public Object visitTerminal(TerminalNode terminalNode) {
return null;
throw new IllegalStateException("not implemented");
}
@Override public Object visitErrorNode(ErrorNode errorNode) {
return null;
throw new IllegalStateException("not implemented");
}
}
......@@ -46,4 +46,5 @@ public abstract class ASTNode<T extends ParserRuleContext> implements Visitable,
public abstract <T> T accept(Visitor<T> visitor);
@Override public abstract ASTNode<T> clone();
}
......@@ -7,9 +7,11 @@ import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
* @version 1 (28.04.17)
*/
public class AssignmentStatement extends Statement<ScriptLanguageParser.AssignmentContext> {
private String rhs;
private Variable rhs;
private Expression lhs;
//TODO type missing, if initialization
@Override public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
......@@ -18,11 +20,11 @@ public class AssignmentStatement extends Statement<ScriptLanguageParser.Assignme
return null;
}
public void setRhs(String rhs) {
public void setRhs(Variable rhs) {
this.rhs = rhs;
}
public String getRhs() {
public Variable getRhs() {
return rhs;
}
......@@ -33,4 +35,8 @@ public class AssignmentStatement extends Statement<ScriptLanguageParser.Assignme
public Expression getLhs() {
return lhs;
}
@Override public String toString() {
return String.format("%s := %s", rhs, lhs);
}
}
......@@ -35,4 +35,17 @@ public class BinaryExpression extends Expression<ParserRuleContext> {
public Operator getOperator() {
return operator;
}
@Override public String toString() {
return "BinaryExpression{" + "left=" + left + ", right=" + right + ", operator=" + operator + '}';
}
public Expression getLeft() {
return left;
}
public Expression getRight() {
return right;
}
}
......@@ -12,8 +12,10 @@ import java.util.Optional;
public class BooleanLiteral extends Literal {
public static final BooleanLiteral FALSE = new BooleanLiteral(false);
public static final BooleanLiteral TRUE = new BooleanLiteral(true);
private final boolean value;
public BooleanLiteral(boolean value, Token token) {
this.value = value;
if (token != null)
this.token = Optional.of(token);
else
......@@ -32,4 +34,12 @@ public class BooleanLiteral extends Literal {
@Override public ASTNode<ParserRuleContext> clone() {
return null;
}
@Override public String toString() {
return "BooleanLiteral{" + "value=" + value + '}';
}
public boolean getValue() {
return value;
}
}
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
/**
* @author Alexander Weigl
* @version 1 (28.04.17)
*/
public class CaseStatement extends Statement<ScriptLanguageParser.CasesListContext> {
private Expression guard;
private Statements body;
public Expression getGuard() {
return guard;
}
public CaseStatement setGuard(Expression guard) {
this.guard = guard;
return this;
}
public Statements getBody() {
return body;
}
public CaseStatement setBody(Statements body) {
this.body = body;
return this;
}
@Override public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
@Override public ASTNode<ScriptLanguageParser.CasesListContext> clone() {
return null;
}
}
package edu.kit.formatl.proofscriptparser.ast;
import edu.kit.formal.proofscriptparser.ScriptLanguageParser;
import java.util.ArrayList;
import java.util.List;
/**
* @author Alexander Weigl
* @version 1 (28.04.17)
*/
public class CasesStatement extends Statement<ScriptLanguageParser.CasesListContext> {
private List<CaseStatement> cases = new ArrayList<>();
public List<CaseStatement> getCases() {
return cases;
}
@Override public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
@Override public ASTNode<ScriptLanguageParser.CasesListContext> clone() {
return null;
}
}
package edu.kit.formatl.proofscriptparser.ast;
/**
* @author Alexander Weigl
* @version 1 (28.04.17)
*/
public class DefaultASTVisitor<T> implements Visitor<T> {
@Override public T visit(ProofScript proofScript) {
return null;
}
@Override public T visit(AssignmentStatement assignmentStatement) {
return null;
}
@Override public T visit(BinaryExpression binaryExpression) {
return null;
}
@Override public T visit(MatchExpression matchExpression) {
return null;
}
@Override public T visit(TermLiteral termLiteral) {
return null;
}
@Override public T visit(StringLiteral stringLiteral) {
return null;
}
@Override public T visit(Variable variable) {
return null;
}
@Override public T visit(BooleanLiteral booleanLiteral) {
return null;
}
@Override public T visit(Statements statements) {
return null;
}
@Override public T visit(IntegerLiteral integer) {
return null;
}
@Override public T visit(CasesStatement casesStatement) {
return null;
}
@Override public T visit(CaseStatement caseStatement) {
return null;
}
@Override public T visit(ScriptCallStatement call) {
return null;
}
}
package edu.kit.formatl.proofscriptparser.ast;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.math.BigInteger;
/**
* @author Alexander Weigl
* @version 1 (28.04.17)
*/
public class IntegerLiteral extends Literal {
private final BigInteger value;
public IntegerLiteral(Token digits) {
setToken(digits);
value = BigInteger.valueOf(Long.parseLong(digits.getText()));
}
@Override public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
@Override public ASTNode<ParserRuleContext> clone() {