Commit 7c42f49c authored by LULUDBR\Lulu's avatar LULUDBR\Lulu

Merge remote-tracking branch 'origin/master'

parents 7933e262 c467d761
...@@ -2,3 +2,6 @@ target/ ...@@ -2,3 +2,6 @@ target/
*.iml *.iml
.idea .idea
website/site/ website/site/
*/build/
*/out/
.gradle
...@@ -46,6 +46,7 @@ assignment ...@@ -46,6 +46,7 @@ assignment
| variable=ID (COLON type=ID)? ASSIGN expression SEMICOLON | variable=ID (COLON type=ID)? ASSIGN expression SEMICOLON
; ;
expression expression
: :
expression MUL expression #exprMultiplication expression MUL expression #exprMultiplication
...@@ -56,6 +57,7 @@ expression ...@@ -56,6 +57,7 @@ expression
| expression AND expression #exprAnd | expression AND expression #exprAnd
| expression OR expression #exprOr | expression OR expression #exprOr
| expression IMP expression #exprIMP | expression IMP expression #exprIMP
| expression USING LBRACKET argList RBRACKET #namespaceset
//| expression EQUIV expression already covered by EQ/NEQ //| expression EQUIV expression already covered by EQ/NEQ
| expression LBRACKET substExpressionList RBRACKET #exprSubst | expression LBRACKET substExpressionList RBRACKET #exprSubst
| ID LPAREN (expression (',' expression)*)? RPAREN #function | ID LPAREN (expression (',' expression)*)? RPAREN #function
......
...@@ -26,7 +26,9 @@ package edu.kit.iti.formal.psdbg.parser; ...@@ -26,7 +26,9 @@ package edu.kit.iti.formal.psdbg.parser;
import edu.kit.iti.formal.psdbg.parser.ast.*; import edu.kit.iti.formal.psdbg.parser.ast.*;
import edu.kit.iti.formal.psdbg.parser.types.Type; import edu.kit.iti.formal.psdbg.parser.types.Type;
import java.util.Map; import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** /**
* {@link ASTTraversal} provides a visitor with a a default traversal of the given AST. * {@link ASTTraversal} provides a visitor with a a default traversal of the given AST.
...@@ -35,6 +37,41 @@ import java.util.Map; ...@@ -35,6 +37,41 @@ import java.util.Map;
* @version 1 (29.04.17) * @version 1 (29.04.17)
*/ */
public interface ASTTraversal<T> extends Visitor<T> { public interface ASTTraversal<T> extends Visitor<T> {
default Stream<T> allOf(Stream<ASTNode> nodes) {
return nodes.map(n -> (T) n.accept(this));
}
default List<T> allOf(Collection<ASTNode> nodes) {
if (nodes.size() == 0) return Collections.emptyList();
return allOf(nodes.stream()).collect(Collectors.toList());
}
default Stream<T> allOf(ASTNode... nodes) {
if (nodes.length == 0) return (Stream<T>) Collections.emptyList().stream();
return allOf(Arrays.stream(nodes));
}
default T oneOf(Stream<? extends ASTNode> nodes) {
return (T) nodes
.filter(Objects::nonNull)
.map(n -> (T) n.accept(this))
.filter(Objects::nonNull)
.findFirst()
.orElse((T) null);
}
default T oneOf(ASTNode... nodes) {
if (nodes.length == 0) return null;
return oneOf(Arrays.stream(nodes));
}
default T oneOf(Collection<ASTNode> nodes) {
if (nodes.size() == 0) return null;
return oneOf(nodes.stream());
}
@Override @Override
default T visit(ProofScript proofScript) { default T visit(ProofScript proofScript) {
proofScript.getSignature().accept(this); proofScript.getSignature().accept(this);
...@@ -226,4 +263,11 @@ public interface ASTTraversal<T> extends Visitor<T> { ...@@ -226,4 +263,11 @@ public interface ASTTraversal<T> extends Visitor<T> {
relaxBlock.getBody().accept(this); relaxBlock.getBody().accept(this);
return null; return null;
} }
@Override
default T visit(NamespaceSetExpression nss) {
nss.getExpression().accept(this);
nss.getSignature().accept(this);
return null;
}
} }
...@@ -176,5 +176,10 @@ public class DefaultASTVisitor<T> implements Visitor<T> { ...@@ -176,5 +176,10 @@ public class DefaultASTVisitor<T> implements Visitor<T> {
public T visit(RelaxBlock relaxBlock) { public T visit(RelaxBlock relaxBlock) {
return defaultVisit(relaxBlock); return defaultVisit(relaxBlock);
} }
@Override
public T visit(NamespaceSetExpression nss) {
return defaultVisit(nss);
}
} }
...@@ -197,7 +197,7 @@ public class PrettyPrinter extends DefaultASTVisitor<Void> { ...@@ -197,7 +197,7 @@ public class PrettyPrinter extends DefaultASTVisitor<Void> {
@Override @Override
public Void visit(TermLiteral termLiteral) { public Void visit(TermLiteral termLiteral) {
String termLit = termLiteral.getText(); String termLit = termLiteral.getContent();
if (termLit.contains("\n")) { if (termLit.contains("\n")) {
termLit = termLit.trim(); termLit = termLit.trim();
} }
......
...@@ -121,12 +121,12 @@ public class TransformAst implements ScriptLanguageVisitor<Object> { ...@@ -121,12 +121,12 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
@Override @Override
public Signature visitArgList(ScriptLanguageParser.ArgListContext ctx) { public Signature visitArgList(ScriptLanguageParser.ArgListContext ctx) {
Signature signature = new Signature(); Signature signature = new Signature();
signature.setRuleContext(ctx);
for (ScriptLanguageParser.VarDeclContext decl : ctx.varDecl()) { for (ScriptLanguageParser.VarDeclContext decl : ctx.varDecl()) {
Variable key = new Variable(decl.name); Variable key = new Variable(decl.name);
key.setParent(signature); key.setParent(signature);
signature.put(key, TypeFacade.findType(decl.type.getText())); signature.put(key, TypeFacade.findType(decl.type.getText()));
} }
signature.setRuleContext(ctx);
return signature; return signature;
} }
...@@ -148,6 +148,7 @@ public class TransformAst implements ScriptLanguageVisitor<Object> { ...@@ -148,6 +148,7 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
@Override @Override
public Statements visitStmtList(ScriptLanguageParser.StmtListContext ctx) { public Statements visitStmtList(ScriptLanguageParser.StmtListContext ctx) {
Statements statements = new Statements(); Statements statements = new Statements();
statements.setRuleContext(ctx);
for (ScriptLanguageParser.StatementContext stmt : ctx.statement()) { for (ScriptLanguageParser.StatementContext stmt : ctx.statement()) {
Statement<ParserRuleContext> statement = (Statement<ParserRuleContext>) stmt.accept(this); Statement<ParserRuleContext> statement = (Statement<ParserRuleContext>) stmt.accept(this);
statement.setParent(statements); statement.setParent(statements);
...@@ -244,6 +245,14 @@ public class TransformAst implements ScriptLanguageVisitor<Object> { ...@@ -244,6 +245,14 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
return ctx.matchPattern().accept(this); return ctx.matchPattern().accept(this);
} }
@Override
public Object visitNamespaceset(ScriptLanguageParser.NamespacesetContext ctx) {
NamespaceSetExpression nse = new NamespaceSetExpression();
nse.setExpression((Expression) ctx.expression().accept(this));
nse.setSignature((Signature) ctx.argList().accept(this));
return nse;
}
@Override @Override
public Object visitExprIMP(ScriptLanguageParser.ExprIMPContext ctx) { public Object visitExprIMP(ScriptLanguageParser.ExprIMPContext ctx) {
return createBinaryExpression(ctx, ctx.expression(), Operator.IMPLICATION); return createBinaryExpression(ctx, ctx.expression(), Operator.IMPLICATION);
...@@ -325,7 +334,7 @@ public class TransformAst implements ScriptLanguageVisitor<Object> { ...@@ -325,7 +334,7 @@ public class TransformAst implements ScriptLanguageVisitor<Object> {
@Override @Override
public Object visitLiteralTerm(ScriptLanguageParser.LiteralTermContext ctx) { public Object visitLiteralTerm(ScriptLanguageParser.LiteralTermContext ctx) {
return new TermLiteral(ctx.getText()); return new TermLiteral(ctx.TERM_LITERAL().getSymbol());
} }
@Override @Override
......
...@@ -90,4 +90,5 @@ public interface Visitor<T> { ...@@ -90,4 +90,5 @@ public interface Visitor<T> {
T visit(RelaxBlock relaxBlock); T visit(RelaxBlock relaxBlock);
T visit(NamespaceSetExpression nss);
} }
...@@ -81,8 +81,8 @@ public abstract class ASTNode<T extends ParserRuleContext> ...@@ -81,8 +81,8 @@ public abstract class ASTNode<T extends ParserRuleContext>
} }
public void setRuleContext(T c) { public void setRuleContext(T c) {
startPosition = Position.from(c.getStart()); startPosition = Position.start(c);
endPosition = Position.from(c.getStop()); endPosition = Position.end(c);
ruleContext = c; ruleContext = c;
} }
......
...@@ -51,7 +51,7 @@ public class BooleanLiteral extends Literal { ...@@ -51,7 +51,7 @@ public class BooleanLiteral extends Literal {
public BooleanLiteral(boolean value, Token token) { public BooleanLiteral(boolean value, Token token) {
this.value = value; this.value = value;
this.token = token; setToken(token);
} }
BooleanLiteral(boolean b) { BooleanLiteral(boolean b) {
...@@ -76,7 +76,7 @@ public class BooleanLiteral extends Literal { ...@@ -76,7 +76,7 @@ public class BooleanLiteral extends Literal {
*/ */
@Override @Override
public BooleanLiteral copy() { public BooleanLiteral copy() {
return new BooleanLiteral(value, token); return new BooleanLiteral(value, getToken());
} }
/** /**
......
...@@ -71,7 +71,7 @@ public class IntegerLiteral extends Literal { ...@@ -71,7 +71,7 @@ public class IntegerLiteral extends Literal {
*/ */
@Override public IntegerLiteral copy() { @Override public IntegerLiteral copy() {
IntegerLiteral il = new IntegerLiteral(value); IntegerLiteral il = new IntegerLiteral(value);
il.token = token; il.setToken(getToken());
return il; return il;
} }
......
...@@ -23,7 +23,6 @@ package edu.kit.iti.formal.psdbg.parser.ast; ...@@ -23,7 +23,6 @@ package edu.kit.iti.formal.psdbg.parser.ast;
*/ */
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.ParserRuleContext;
...@@ -38,7 +37,15 @@ import java.util.Optional; ...@@ -38,7 +37,15 @@ import java.util.Optional;
public abstract class Literal extends Expression<ParserRuleContext> { public abstract class Literal extends Expression<ParserRuleContext> {
@Getter @Getter
@Setter @Setter
protected Token token; private Token token;
public void setToken(Token token) {
this.token = token;
if (token != null) {
startPosition = Position.start(token);
endPosition = Position.end(token);
}
}
/** /**
* {@inheritDoc} * {@inheritDoc}
......
package edu.kit.iti.formal.psdbg.parser.ast;
import edu.kit.iti.formal.psdbg.parser.NotWelldefinedException;
import edu.kit.iti.formal.psdbg.parser.ScriptLanguageParser;
import edu.kit.iti.formal.psdbg.parser.Visitor;
import edu.kit.iti.formal.psdbg.parser.types.Type;
import lombok.Getter;
import lombok.Setter;
public class NamespaceSetExpression extends Expression<ScriptLanguageParser.NamespacesetContext>{
@Getter @Setter
private Expression expression;
@Getter @Setter
private Signature signature = new Signature();
@Override
public <T> T accept(Visitor<T> visitor) {
return visitor.visit(this);
}
@Override
public boolean hasMatchExpression() {
return expression.hasMatchExpression();
}
@Override
public int getPrecedence() {
return 5;
}
@Override
public NamespaceSetExpression copy() {
NamespaceSetExpression nse = new NamespaceSetExpression();
nse.expression = expression.copy();
nse.signature = signature.copy();
return nse;
}
@Override
public Type getType(Signature signature) throws NotWelldefinedException {
return expression.getType(signature);
}
}
...@@ -23,26 +23,51 @@ package edu.kit.iti.formal.psdbg.parser.ast; ...@@ -23,26 +23,51 @@ package edu.kit.iti.formal.psdbg.parser.ast;
*/ */
import lombok.Data;
import lombok.*; import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
/** /**
* @author Alexander Weigl * @author Alexander Weigl
*/ */
@Data @Value @RequiredArgsConstructor public class Position implements Copyable<Position> { @Data
@Value
@RequiredArgsConstructor
public class Position implements Copyable<Position> {
private final int offset;
private final int lineNumber; private final int lineNumber;
private final int charInLine; private final int charInLine;
public Position() { public Position() {
this(-1, -1); this(-1, -1, -1);
}
public static Position start(Token token) {
return new Position(
token.getStartIndex(),
token.getLine(),
token.getCharPositionInLine());
}
public static Position end(Token token) {
return new Position(
token.getStopIndex(),
token.getLine(),
token.getCharPositionInLine());
}
public static Position start(ParserRuleContext token) {
return start(token.start);
} }
@Override public Position copy() { public static Position end(ParserRuleContext token) {
return new Position(lineNumber, charInLine); return end(token.stop);
} }
public static Position from(Token token) { @Override
return new Position(token.getLine(), token.getCharPositionInLine()); public Position copy() {
return new Position(offset, lineNumber, charInLine);
} }
} }
...@@ -23,12 +23,12 @@ package edu.kit.iti.formal.psdbg.parser.ast; ...@@ -23,12 +23,12 @@ package edu.kit.iti.formal.psdbg.parser.ast;
*/ */
import edu.kit.iti.formal.psdbg.parser.NotWelldefinedException; import edu.kit.iti.formal.psdbg.parser.NotWelldefinedException;
import edu.kit.iti.formal.psdbg.parser.Visitor; import edu.kit.iti.formal.psdbg.parser.Visitor;
import edu.kit.iti.formal.psdbg.parser.types.Type; import edu.kit.iti.formal.psdbg.parser.types.Type;
import edu.kit.iti.formal.psdbg.parser.types.TypeFacade; import edu.kit.iti.formal.psdbg.parser.types.TypeFacade;
import lombok.Data; import lombok.Data;
import org.antlr.v4.runtime.Token;
/** /**
* @author Alexander Weigl * @author Alexander Weigl
...@@ -36,16 +36,27 @@ import lombok.Data; ...@@ -36,16 +36,27 @@ import lombok.Data;
*/ */
@Data @Data
public class TermLiteral extends Literal { public class TermLiteral extends Literal {
private final String text; private final String content;
public TermLiteral(String text) { public TermLiteral(Token token) {
setToken(token);
String text = token.getText();
if (text.charAt(0) == '`') if (text.charAt(0) == '`')
text = text.substring(1); text = text.substring(1);
if (text.charAt(text.length() - 1) == '`') //remove last backtick if (text.charAt(text.length() - 1) == '`') //remove last backtick
text = text.substring(0, text.length() - 1); text = text.substring(0, text.length() - 1);
if (text.charAt(0) == '`') if (text.charAt(0) == '`')
text = text.substring(0, text.length() - 2); text = text.substring(0, text.length() - 2);
this.text = text; content = text;
}
private TermLiteral(String sfTerm) {
content = sfTerm;
}
public static TermLiteral from(String sfTerm) {
TermLiteral tl = new TermLiteral(sfTerm);
return tl;
} }
/** /**
...@@ -67,7 +78,7 @@ public class TermLiteral extends Literal { ...@@ -67,7 +78,7 @@ public class TermLiteral extends Literal {
@Override @Override
public TermLiteral copy() { public TermLiteral copy() {
return new TermLiteral(text); return new TermLiteral(getToken());
} }
/** /**
...@@ -77,5 +88,4 @@ public class TermLiteral extends Literal { ...@@ -77,5 +88,4 @@ public class TermLiteral extends Literal {
public Type getType(Signature signature) throws NotWelldefinedException { public Type getType(Signature signature) throws NotWelldefinedException {
return TypeFacade.ANY_TERM; return TypeFacade.ANY_TERM;
} }
} }
...@@ -61,7 +61,7 @@ public class Variable extends Literal implements Comparable<Variable> { ...@@ -61,7 +61,7 @@ public class Variable extends Literal implements Comparable<Variable> {
@Override @Override
public Variable copy() { public Variable copy() {
Variable v = new Variable(identifier); Variable v = new Variable(identifier);
v.token = token; v.setToken(getToken());
return v; return v;
} }
......
...@@ -2,6 +2,7 @@ package edu.kit.iti.formal.psdbg.parser.data; ...@@ -2,6 +2,7 @@ package edu.kit.iti.formal.psdbg.parser.data;
import edu.kit.iti.formal.psdbg.parser.ast.*; import edu.kit.iti.formal.psdbg.parser.ast.*;
import edu.kit.iti.formal.psdbg.parser.types.SimpleType; import edu.kit.iti.formal.psdbg.parser.types.SimpleType;
import edu.kit.iti.formal.psdbg.parser.types.TermType;
import edu.kit.iti.formal.psdbg.parser.types.Type; import edu.kit.iti.formal.psdbg.parser.types.Type;
import edu.kit.iti.formal.psdbg.parser.types.TypeFacade; import edu.kit.iti.formal.psdbg.parser.types.TypeFacade;
import lombok.Getter; import lombok.Getter;
...@@ -56,7 +57,7 @@ public class Value<T> { ...@@ -56,7 +57,7 @@ public class Value<T> {
} }
public static Value<String> from(TermLiteral term) { public static Value<String> from(TermLiteral term) {
return new Value<>(TypeFacade.ANY_TERM, term.getText()); return new Value<>(TypeFacade.ANY_TERM, term.getContent());
} }
@Override @Override
......
...@@ -23,27 +23,52 @@ package edu.kit.iti.formal.psdbg.parser.types; ...@@ -23,27 +23,52 @@ package edu.kit.iti.formal.psdbg.parser.types;
*/ */
import javax.annotation.Nullable;
/** /**
* Represents the possible types (including scriptVarTypes). * Represents the possible types (including scriptVarTypes).
* <p> * <p>
* Created at 30.04.2017 * Created at 30.04.2017
* * INT("\\term int"),
BOOL("\\term bool"),
ANY("\\term any"),
INT_ARRAY("\\term int[]"),
OBJECT("\\term Object"),
HEAP("\\term Heap"),
FIELD("\\term Field"),
LOCSET("\\term LocSet"),
FORMULA("\\formula"),
SEQ("\\term Seq");
* @author Sarah Grebing * @author Sarah Grebing
*/ */
public enum SimpleType implements Type {