Commit 7b3268fe authored by Alexander Weigl's avatar Alexander Weigl

auto completion and inline action bar

parent 7e68964f
Pipeline #19642 failed with stages
in 2 minutes and 58 seconds
...@@ -2,3 +2,6 @@ target/ ...@@ -2,3 +2,6 @@ target/
*.iml *.iml
.idea .idea
website/site/ website/site/
*/build/
*/out/
.gradle
...@@ -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);
......
...@@ -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);
...@@ -333,7 +334,7 @@ public class TransformAst implements ScriptLanguageVisitor<Object> { ...@@ -333,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
......
...@@ -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}
......
...@@ -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;
} }
......
...@@ -57,7 +57,7 @@ public class Value<T> { ...@@ -57,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
......
...@@ -17,7 +17,6 @@ import jdk.nashorn.internal.objects.annotations.Getter; ...@@ -17,7 +17,6 @@ import jdk.nashorn.internal.objects.annotations.Getter;
import jdk.nashorn.internal.objects.annotations.Setter; import jdk.nashorn.internal.objects.annotations.Setter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.Value; import lombok.Value;
import sun.security.jca.GetInstance;
import java.io.StringReader; import java.io.StringReader;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
......
...@@ -142,7 +142,7 @@ public class KeyInterpreter extends Interpreter<KeyData> { ...@@ -142,7 +142,7 @@ public class KeyInterpreter extends Interpreter<KeyData> {
eval.setTermValueFactory(new Function<TermLiteral, Value>() { eval.setTermValueFactory(new Function<TermLiteral, Value>() {
@Override @Override
public Value apply(TermLiteral termLiteral) { public Value apply(TermLiteral termLiteral) {
return new Value(TypeFacade.ANY_TERM, new TermValue(termLiteral.getText())); return new Value(TypeFacade.ANY_TERM, new TermValue(termLiteral.getContent()));
} }
}); });
return eval; return eval;
......
package edu.kit.iti.formal.psdbg.gui.actions.inline;
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIcon;
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView;
import edu.kit.iti.formal.psdbg.parser.ast.ASTNode;
import edu.kit.iti.formal.psdbg.parser.ast.StringLiteral;
import javafx.event.ActionEvent;
import java.util.Collection;
import java.util.Collections;
/**
* @author Alexander Weigl
* @version 1 (12.03.18)
*/
public class FindLabelInGoalList implements InlineActionSupplier {
@Override
public Collection<InlineAction> get(ASTNode node) {
if (node instanceof StringLiteral) {
return Collections.singletonList(new FindLabelInGoalListAction());
}
return Collections.emptyList();
}
public static class FindLabelInGoalListAction extends InlineAction {
public FindLabelInGoalListAction() {
setName(getClass().getSimpleName());
setGraphics(new MaterialDesignIconView(MaterialDesignIcon.ACCOUNT));
setPriority(2);
setEventHandler(this::handle);
}
private void handle(ActionEvent event) {
System.out.println("FindLabelInGoalListAction.handle");
}
}
}
package edu.kit.iti.formal.psdbg.gui.actions.inline;
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIcon;
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView;
import edu.kit.iti.formal.psdbg.parser.ast.ASTNode;
import edu.kit.iti.formal.psdbg.parser.ast.TermLiteral;
import javafx.event.ActionEvent;
import java.util.Collection;
import java.util.Collections;
/**
* @author Alexander Weigl
* @version 1 (12.03.18)
*/
public class FindTermLiteralInSequence implements InlineActionSupplier {
@Override
public Collection<InlineAction> get(ASTNode node) {
if (node instanceof TermLiteral) {
return Collections.singletonList(
new FindTermLiteralInSequenceAction()
);
}
return Collections.emptyList();
}
public static class FindTermLiteralInSequenceAction extends InlineAction {
public FindTermLiteralInSequenceAction() {
setName(getClass().getSimpleName());
setGraphics(new MaterialDesignIconView(MaterialDesignIcon.MAGNIFY_PLUS));
setEventHandler(this::handle);
}
private void handle(ActionEvent event) {
System.out.println("FindTermLiteralInSequenceAction.handle");
}
}
}
package edu.kit.iti.formal.psdbg.gui.actions.inline;
import javafx.beans.DefaultProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class InlineAction implements Comparable<InlineAction> {
private String name;
private int priority;
private Node graphics;
private EventHandler<ActionEvent> eventHandler;
@Override
public int compareTo(InlineAction o) {
return priority - o.priority;
}
}
\ No newline at end of file
package edu.kit.iti.formal.psdbg.gui.actions.inline;
import edu.kit.iti.formal.psdbg.parser.ast.ASTNode;
import java.util.Collection;
/**
* @author Alexander Weigl
* @version 1 (12.03.18)
*/
public interface InlineActionSupplier {
Collection<InlineAction> get(ASTNode node);
}
...@@ -175,9 +175,9 @@ public class InteractiveModeController { ...@@ -175,9 +175,9 @@ public class InteractiveModeController {
int occ = rch.getOccurence(tap.getApp()); int occ = rch.getOccurence(tap.getApp());
Parameters callp = new Parameters(); Parameters callp = new Parameters();
callp.put(new Variable("formula"), new TermLiteral(sfTerm)); callp.put(new Variable("formula"), TermLiteral.from(sfTerm));
callp.put(new Variable("occ"), new IntegerLiteral(BigInteger.valueOf(occ))); callp.put(new Variable("occ"), new IntegerLiteral(BigInteger.valueOf(occ)));
callp.put(new Variable("on"), new TermLiteral(onTerm)); callp.put(new Variable("on"), TermLiteral.from(onTerm));
VariableAssignment va = new VariableAssignment(null); VariableAssignment va = new VariableAssignment(null);
CallStatement call = new CallStatement(tapName, callp); CallStatement call = new CallStatement(tapName, callp);
......
package edu.kit.iti.formal.psdbg.gui.controls;
import com.google.common.collect.Streams;
import edu.kit.iti.formal.psdbg.parser.ASTTraversal;
import edu.kit.iti.formal.psdbg.parser.ast.*;
import lombok.RequiredArgsConstructor;
import java.util.*;
import java.util.stream.Stream;
/**
* @author Alexander Weigl
* @version 1 (12.03.18)
*/
@RequiredArgsConstructor
public class FindNearestASTNode implements ASTTraversal<ASTNode> {
private final int pos;
@Override
public ASTNode visit(ProofScript proofScript) {
return childOrMe(proofScript, proofScript.getSignature(), proofScript.getBody());
}
private ASTNode childOrMe(ASTNode me, Stream<? extends ASTNode> nodes) {
// range check
if (me == null) {
return null;
}
try {
int start = me.getStartPosition().getOffset();
int stop = me.getEndPosition().getOffset();
if (start <= pos && pos <= stop) {
ASTNode a = null;
try {
a = oneOf(nodes);
} catch (NullPointerException e1) {
e1.printStackTrace();
}
return a != null ? a : me;
}
} catch (NullPointerException e) {
System.err.println("No rule context set for instance " + me);
e.printStackTrace();
}
return null;
}
private ASTNode childOrMe(ASTNode me, Collection<? extends ASTNode> children) {
return childOrMe(me, children.stream());
}
private ASTNode childOrMe(ASTNode me, ASTNode... nodes) {
return childOrMe(me, Arrays.stream(nodes));
}
@Override
public ASTNode visit(AssignmentStatement assign) {
return childOrMe(assign, assign.getLhs(), assign.getRhs());
}
@Override
public ASTNode visit(BinaryExpression e) {
return childOrMe(e, e.getLeft(), e.getRight());
}
@Override
public ASTNode visit(MatchExpression match) {
return childOrMe(match, match.getSignature(), match.getDerivableTerm(), match.getPattern());
}
@Override
public ASTNode visit(TermLiteral term) {
return childOrMe(term);
}
@Override
public ASTNode visit(StringLiteral string) {
return childOrMe(string);
}
@Override
public ASTNode visit(Variable variable) {
return childOrMe(variable);
}
@Override
public ASTNode visit(BooleanLiteral bool) {
return childOrMe(bool);
}
@Override
public ASTNode visit(Statements statements) {
return childOrMe(statements, statements.stream());
}