Skip to content

The Unified and Autonomous Development Platform

Add necessary code assist comments

robin shine80965d8602e1
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/AlternativeSpec.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/AlternativeSpec.java
index 777c1d334e..87694be3ef 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/AlternativeSpec.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/AlternativeSpec.java
@@ -31,8 +31,8 @@ public class AlternativeSpec extends Spec {
}
@Override
- public SpecMatch match(AssistStream stream,
- Node parent, Node previous, Map<String, Integer> checkedIndexes) {
+ public SpecMatch match(AssistStream stream, Node parent, Node previous,
+ Map<String, Integer> checkedIndexes) {
parent = new Node(this, parent, previous);
List<TokenNode> paths = new ArrayList<>();
for (ElementSpec elementSpec: elements) {
@@ -40,8 +40,9 @@ public class AlternativeSpec extends Spec {
previous = paths.get(paths.size()-1);
else
previous = parent;
- SpecMatch elementMatch = elementSpec.match(
- stream, parent, previous, new HashMap<>(checkedIndexes));
+ SpecMatch elementMatch = elementSpec.match(stream, parent, previous,
+ new HashMap<>(checkedIndexes));
+
if (!elementMatch.getPaths().isEmpty())
paths = elementMatch.getPaths();
if (!elementMatch.isMatched())
@@ -57,6 +58,8 @@ public class AlternativeSpec extends Spec {
parent = new Node(this, parent, null);
for (ElementSpec element: elements) {
first.addAll(element.suggestFirst(parseTree, parent, matchWith, new HashSet<>(checkedRules)));
+
+ // consider next element if current element is optional
if (!element.matches(codeAssist.lex("")))
break;
}
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/CodeAssist.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/CodeAssist.java
index 749707f557..8b9a6dfc70 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/CodeAssist.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/CodeAssist.java
@@ -294,6 +294,7 @@ public abstract class CodeAssist implements Serializable {
|| lexerAtomContext.DOT() != null
|| lexerAtomContext.LEXER_CHAR_SET()!=null
|| lexerAtomContext.range() != null) {
+ // Use AnyTokenElementSpec here to as it does not affect our code assist analysis
return new AnyTokenElementSpec(this, label, multiplicity);
} else {
throw new IllegalStateException();
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementSpec.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementSpec.java
index a52cad0254..223e265f24 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementSpec.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ElementSpec.java
@@ -53,6 +53,12 @@ public abstract class ElementSpec extends Spec {
if (!match.isMatched()) {
return new SpecMatch(match.getPaths(), false);
} else if (match.getPaths().isEmpty()) {
+ /*
+ * does not consume any token of the stream but match is successful? This
+ * tells us the element spec does not match the stream but itself (or
+ * everything inside it) is optional, so we do not waste any cpu cycles
+ * here
+ */
return new SpecMatch(match.getPaths(), true);
} else {
List<TokenNode> paths = match.getPaths();
@@ -61,14 +67,32 @@ public abstract class ElementSpec extends Spec {
match = matchOnce(stream, parent, previous, checkedIndexes);
if (!match.isMatched()) {
if (!match.getPaths().isEmpty()) {
+ /*
+ * we are always greedy when matching specs against stream for performance
+ * reason, so if there are any tokens consumed in the stream, we use those
+ * tokens, and consider the whole spec as not-matched although previous
+ * iteration matches. This false-positive is acceptable as our purpose is
+ * for auto-completion...
+ */
paths = match.getPaths();
return new SpecMatch(paths, false);
} else {
+ /*
+ * does not consume any token from stream, so we will return result of
+ * previous match
+ */
return new SpecMatch(paths, true);
}
} else if (match.getPaths().isEmpty()) {
+ /*
+ * does not consume any tokens of the stream, so further iteration will be
+ * meaningless, let's return result of previous match
+ */
return new SpecMatch(paths, true);
} else {
+ /*
+ * can still consume tokens from stream, let's continue...
+ */
paths = match.getPaths();
}
}
@@ -112,9 +136,23 @@ public abstract class ElementSpec extends Spec {
Node parent, Node previous, Map<String, Integer> checkedIndexes);
public abstract MandatoryScan scanMandatories(Set<String> checkedRules);
-
+
+ /**
+ * Provide suggestions after current element spec.
+ *
+ * @param parseTree
+ * current parse tree
+ * @param parent
+ * parent node of current element spec
+ * @param matchWith
+ * string to match with
+ * @return
+ * list of element suggestions of current element spec
+ */
public List<ElementSuggestion> suggestNext(ParseTree parseTree, Node parent, String matchWith) {
List<ElementSuggestion> suggestions = new ArrayList<>();
+
+ // if element spec can be repeated, next input candidate can also be taken from the element itself
if (multiplicity == Multiplicity.ONE_OR_MORE || multiplicity == Multiplicity.ZERO_OR_MORE)
suggestions.addAll(suggestFirst(parseTree, parent, matchWith, new HashSet<String>()));
suggestions.addAll(doSuggestNext(parseTree, parent, matchWith));
@@ -136,6 +174,9 @@ public abstract class ElementSpec extends Spec {
ElementSpec nextElementSpec = alternativeSpec.getElements().get(specIndex+1);
suggestions.addAll(nextElementSpec.suggestFirst(
parseTree, parent, matchWith, new HashSet<String>()));
+ /*
+ * if next element is optional, the next next element can also be input candidate
+ */
if (nextElementSpec.matches(codeAssist.lex("")))
suggestions.addAll(nextElementSpec.doSuggestNext(parseTree, parent, matchWith));
}
@@ -149,10 +190,13 @@ public abstract class ElementSpec extends Spec {
List<InputSuggestion> suggestions = codeAssist.suggest(parseTree, elementNode, matchWith);
if (suggestions != null) {
if (suggestions.isEmpty() && !scanMandatories(new HashSet<String>()).getMandatories().isEmpty())
+ // user suppresses suggestion explicitly, so we only provide suggestion if there are
+ // mandatory followings
return doSuggestFirst(parseTree, parent, matchWith, checkedRules);
else
return Lists.newArrayList(new ElementSuggestion(parseTree, elementNode, matchWith, suggestions));
} else {
+ // user does not care about suggestion, so we continue with our default logic to drill down
return doSuggestFirst(parseTree, parent, matchWith, checkedRules);
}
}
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/LexerRuleRefElementSpec.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/LexerRuleRefElementSpec.java
index bbc2b40942..db9c1bfc3e 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/LexerRuleRefElementSpec.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/LexerRuleRefElementSpec.java
@@ -39,7 +39,7 @@ public class LexerRuleRefElementSpec extends TokenElementSpec {
@Override
public List<ElementSuggestion> doSuggestFirst(ParseTree parseTree, Node parent,
String matchWith, Set<String> checkedRules) {
- if (!checkedRules.contains(ruleName) && getRule() != null) {
+ if (!checkedRules.contains(ruleName) && getRule() != null) { // to avoid infinite loop
checkedRules.add(ruleName);
return getRule().suggestFirst(parseTree, new Node(this, parent, null), matchWith, checkedRules);
} else {
@@ -49,23 +49,29 @@ public class LexerRuleRefElementSpec extends TokenElementSpec {
@Override
public MandatoryScan scanMandatories(Set<String> checkedRules) {
- if (!checkedRules.contains(ruleName) && getRule() != null) {
+ if (!checkedRules.contains(ruleName) && getRule() != null) { // to avoid infinite loop
checkedRules.add(ruleName);
List<AlternativeSpec> alternatives = getRule().getAlternatives();
+ // nothing will be mandatory if we have multiple alternatives
if (alternatives.size() == 1) {
List<String> mandatories = new ArrayList<>();
for (ElementSpec elementSpec: alternatives.get(0).getElements()) {
if (elementSpec.getMultiplicity() == Multiplicity.ZERO_OR_ONE
|| elementSpec.getMultiplicity() == Multiplicity.ZERO_OR_MORE) {
+ // next input can either be current element, or other elements, so
+ // mandatory scan can be stopped
return new MandatoryScan(mandatories, true);
} else if (elementSpec.getMultiplicity() == Multiplicity.ONE_OR_MORE) {
MandatoryScan scan = elementSpec.scanMandatories(new HashSet<>(checkedRules));
mandatories.addAll(scan.getMandatories());
+ // next input can either be current element, or other elements, so
+ // mandatory scan can be stopped
return new MandatoryScan(mandatories, true);
} else {
MandatoryScan scan = elementSpec.scanMandatories(new HashSet<>(checkedRules));
mandatories.addAll(scan.getMandatories());
+ // if internal of the element tells use to stop, let's stop
if (scan.isStop())
return new MandatoryScan(mandatories, true);
}
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/MandatoryScan.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/MandatoryScan.java
index 187cb3cbab..0a8e845756 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/MandatoryScan.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/MandatoryScan.java
@@ -9,6 +9,15 @@ public class MandatoryScan {
private final boolean stop;
+ /**
+ * Construct mandatory scan object
+ *
+ * @param mandatories
+ * list of mandatory literals
+ * @param stop
+ * sign to whether or not the mandatory scan should be
+ * stopped
+ */
public MandatoryScan(List<String> mandatories, boolean stop) {
this.mandatories = mandatories;
this.stop = stop;
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/Node.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/Node.java
index 69c292aa1d..0cc78de329 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/Node.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/Node.java
@@ -10,6 +10,16 @@ public class Node {
protected final Node previous;
+ /**
+ * Construct the node
+ *
+ * @param spec
+ * spec of the node
+ * @param parent
+ * parent of the node
+ * @param previous
+ * previous node, which is used to track all nodes in the parse tree
+ */
public Node(Spec spec, @Nullable Node parent, @Nullable Node previous) {
this.spec = spec;
this.parent = parent;
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ParseTree.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ParseTree.java
index b1c96972e5..33151b015b 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ParseTree.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/ParseTree.java
@@ -8,12 +8,26 @@ import java.util.Set;
import javax.annotation.Nullable;
+/**
+ * Parse tree represents parse state. It can be partial if only part of the
+ * spec matches in the stream. Suggestion providers can use this to check
+ * current state of the parsing, to provide meaningful suggestions if
+ * necessary.
+ *
+ * @author robin
+ *
+ */
public class ParseTree {
private final TokenNode lastNode;
private transient List<Node> allNodes;
+ /**
+ * Construct parse tree from last parsed token node from stream.
+ *
+ * @param lastNode
+ */
public ParseTree(TokenNode lastNode) {
this.lastNode = lastNode;
}
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/RuleRefElementSpec.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/RuleRefElementSpec.java
index c26d1afd71..1706e228cc 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/RuleRefElementSpec.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/RuleRefElementSpec.java
@@ -49,19 +49,25 @@ public class RuleRefElementSpec extends ElementSpec {
checkedRules.add(ruleName);
List<AlternativeSpec> alternatives = getRule().getAlternatives();
+ // nothing will be mandatory if we have multiple alternatives
if (alternatives.size() == 1) {
List<String> mandatories = new ArrayList<>();
for (ElementSpec elementSpec: alternatives.get(0).getElements()) {
if (elementSpec.getMultiplicity() == Multiplicity.ZERO_OR_ONE
|| elementSpec.getMultiplicity() == Multiplicity.ZERO_OR_MORE) {
+ // next input can either be current element, or other elements, so
+ // mandatory scan can be stopped
return new MandatoryScan(mandatories, true);
} else if (elementSpec.getMultiplicity() == Multiplicity.ONE_OR_MORE) {
MandatoryScan scan = elementSpec.scanMandatories(new HashSet<>(checkedRules));
mandatories.addAll(scan.getMandatories());
+ // next input can either be current element, or other elements, so
+ // mandatory scan can be stopped
return new MandatoryScan(mandatories, true);
} else {
MandatoryScan scan = elementSpec.scanMandatories(new HashSet<>(checkedRules));
mandatories.addAll(scan.getMandatories());
+ // if internal of the element tells use to stop, let's stop
if (scan.isStop())
return new MandatoryScan(mandatories, true);
}
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/RuleSpec.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/RuleSpec.java
index 2346011de5..55b0df424f 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/RuleSpec.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/RuleSpec.java
@@ -71,6 +71,7 @@ public class RuleSpec extends Spec {
stream.setIndex(index);
}
+ // always select the path matching most tokens in the stream
stream.setIndex(Math.max(maxMatchIndex, maxUnmatchIndex));
if (maxMatchIndex == maxUnmatchIndex) {
matchedPaths.addAll(unmatchedPaths);
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/Spec.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/Spec.java
index 6a3b15bdf0..dbc1525ed8 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/Spec.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/Spec.java
@@ -16,10 +16,34 @@ public abstract class Spec implements Serializable {
this.codeAssist = codeAssist;
}
+ /**
+ * Whether or there is an exact match between the spec and the the stream
+ *
+ * @param stream
+ * stream to be matched
+ * @return
+ * true if there is an exact match between spec and stream
+ */
public boolean matches(AssistStream stream) {
return match(stream, null, null, new HashMap<String, Integer>()).isMatched() && stream.isEof();
}
+ /**
+ * Match current spec against the stream.
+ *
+ * @param stream
+ * stream to match the spec against
+ * @param parent
+ * parent node of the newly created node to form parse tree hierarchy
+ * @param previous
+ * previous node of newly created node to form all nodes in certain parse tree
+ * @param checkedIndexes
+ * checked indexes to avoid infinite loop
+ * @return
+ * match result representing common paths between the spec and the stream. Note
+ * that we can have match paths even if the whole spec is not matched, and in
+ * that case, the paths tells to which point the match goes to
+ */
public abstract SpecMatch match(AssistStream stream,
Node parent, Node previous, Map<String, Integer> checkedIndexes);
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/SpecMatch.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/SpecMatch.java
index 36ac886b9a..86b8b636a5 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/SpecMatch.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/SpecMatch.java
@@ -7,16 +7,39 @@ public class SpecMatch {
private final List<TokenNode> paths;
private final boolean matched;
-
+
public SpecMatch(List<TokenNode> paths, boolean matched) {
this.paths = paths;
this.matched = matched;
}
+ /**
+ * Get different paths in this match
+ *
+ * @return
+ * get different match paths, each path represents an alternative match
+ * (full or partial) between the spec and the stream, and each of them
+ * represents parse tree (nodes of the parse tree can be deducted by calling
+ * {@link TokenNode#getPrevious()}. Also all these path point to the same token
+ * as those shorter paths have been discarded due to our greedy match strategy.
+ * A parse tree can be deducted from each path via
+ * {@link TokenNode#getPrevious()} Note that this list may not be empty even if
+ * {@link #isMatched()} return false, and in this case, the spec is only
+ * partially matched, and these paths represent to which point this partial
+ * match goes.
+ * Specifically if the list is empty, no tokens are consumed from the stream
+ */
public List<TokenNode> getPaths() {
return paths;
}
+ /**
+ * Whether or not there is a match in above paths.
+ *
+ * @return
+ * true as long as one of paths above represents a full match (the whole spec
+ * can be matched)
+ */
public boolean isMatched() {
return matched;
}
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/SurroundingAware.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/SurroundingAware.java
index 9468aaed55..74df6be263 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/SurroundingAware.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/codeassist/SurroundingAware.java
@@ -59,6 +59,12 @@ public abstract class SurroundingAware {
if (suggestion.getContent().equals(matchWith))
matchWithIncluded = true;
}
+ /*
+ * if matchWith does not appear in suggestion list, we check to see if it should be surrounded.
+ * For instance, you may have a rule requiring that value containing spaces should be quoted,
+ * in this case, below code will suggest you to quote the value if it contains spaces as
+ * otherwise it will fail the match below
+ */
if (!matchWithIncluded && !matches(elementNode.getSpec(), matchWith)) {
matchWith = prefix + matchWith + suffix;
if (matches(elementNode.getSpec(), matchWith))
@@ -71,5 +77,15 @@ public abstract class SurroundingAware {
return null;
}
+ /**
+ * Match with provided string to give a list of suggestions
+ *
+ * @param surroundlessMatchWith
+ * string with surrounding literals removed
+ * @return
+ * a list of suggestions, value containing spaces does not have to be
+ * surrounded with prefix/suffix as the {@link SurroundingAware} class
+ * will add them if necessary
+ */
protected abstract List<InputSuggestion> match(String surroundlessMatchWith);
}