Commit b838c725 authored by Johannes Roith's avatar Johannes Roith
Browse files

We now compare local Declarations as well. Fixes syntax refreshing.

parent 80b72144
package de.in.tum.www2.cup;
import java.util.HashMap;
import java.util.HashSet;
public class Declarations {
......@@ -7,6 +8,24 @@ public class Declarations {
private HashSet<String> terminals = new HashSet<String>();
private HashSet<String> non_terminals = new HashSet<String>();
// This map stores the declared symbols for a right hand side at a
// particular index within the production definition.
//
// Note, that this is a superset of the valid symbols for a particular
// action block. Therefore, blocks will still need to compute their own sets.
// However this is a good approximation to avoid refreshing the syntax
// unnecessarily.
// Won't work if: righthand sides are reordered or declared symbols name
// within a righthand side are moved in front of an action block.
//
// The key in this map is computed like as:
// Non-Terminal-Name + "::" + number of right hand
// sides + "::" + right hand side index.
//
// see also: DeclarationsExtractorVisitor
//
private HashMap<String,HashSet<String>> locals = new HashMap<String,HashSet<String>>();
public HashSet<String> getTerminals() {
return terminals;
}
......@@ -14,19 +33,36 @@ public class Declarations {
public HashSet<String> getNonTerminals() {
return non_terminals;
}
public boolean isDeclaredTerminal(String name) {
return terminals.contains(name);
}
public boolean isDeclaredNonTerminal(String name) {
return non_terminals.contains(name);
}
public boolean isDeclaredLocal(String name) {
for (HashSet<String> s : locals.values())
if (s.contains(name))
return true;
return false;
}
public boolean isDeclared(String name) {
return isDeclaredTerminal(name) || isDeclaredNonTerminal (name);
return isDeclaredTerminal(name)
|| isDeclaredNonTerminal (name)
|| isDeclaredLocal(name);
}
public HashMap<String,HashSet<String>> getLocals() {
return locals;
}
public HashSet<String> getLocalsFor(String path) {
return locals.get(path);
}
@Override
public boolean equals(Object other) {
if (other == null)
......@@ -37,8 +73,9 @@ public class Declarations {
if (this.getClass().equals(other.getClass())) {
Declarations o = (Declarations) other;
return terminals.equals(o.terminals) &&
non_terminals.equals(o.non_terminals);
return terminals.equals(o.terminals)
&& non_terminals.equals(o.non_terminals)
&& locals.equals(o.locals);
}
return false;
......
package de.in.tum.www2.cup.analysis;
import java.util.HashMap;
import java.util.HashSet;
import de.in.tum.www2.cup.Declarations;
import de.in.tum.www2.cup.ast.ActionCodeBlock;
import de.in.tum.www2.cup.ast.IProductionRightPart;
import de.in.tum.www2.cup.ast.LabeledProductionSymbolRef;
import de.in.tum.www2.cup.ast.NonTerminal;
import de.in.tum.www2.cup.ast.NonTerminalDeclaration;
import de.in.tum.www2.cup.ast.Production;
import de.in.tum.www2.cup.ast.ProductionRight;
import de.in.tum.www2.cup.ast.ProductionSymbolRef;
import de.in.tum.www2.cup.ast.Terminal;
import de.in.tum.www2.cup.ast.TerminalDeclaration;
import de.in.tum.www2.cup.ast.TypedNonTerminalDeclaration;
......@@ -17,6 +25,40 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
return result;
}
@Override
public void postVisit (ProductionRight node, Object data) {
Production parent = (Production) node.getParent();
int index=0;
for (ProductionRight pr : parent.getRightHandSides()) {
if (pr == node)
break;
index++;
}
StringBuilder builder = new StringBuilder();
builder.append (parent.getName().name);
builder.append ("::");
builder.append (parent.getRightHandSides().size());
builder.append ("::");
builder.append (index);
String key = builder.toString();
HashMap<String,HashSet<String>> locals = result.getLocals();
if (!locals.containsKey(key))
locals.put(key, new HashSet<String>());
HashSet<String> set = locals.get(key);
for (IProductionRightPart part : node.getList()) {
if (part instanceof LabeledProductionSymbolRef) {
LabeledProductionSymbolRef lpsr = (LabeledProductionSymbolRef) part;
set.add(lpsr.label);
}
}
}
@Override
public void postVisit (TerminalDeclaration node, Object data) {
if (node != null && node.getTerminals() != null) {
......@@ -50,11 +92,5 @@ public class DeclarationsExtractorVisitor extends Visitor<Object>
result.getNonTerminals().add(nt.getName().name);
}
}
@Override
public Object preVisit (Production node, Object data) {
// We don't want to visit the productions.
noDescend();
return data;
}
}
......@@ -21,6 +21,10 @@ public class Production extends AbstractNode
public AbstractNode getDeclaration() {
return declarationRef;
}
public List<ProductionRight> getRightHandSides() {
return rhs;
}
@Override
public void setDeclaration(AbstractNode node) {
......
......@@ -96,24 +96,29 @@ public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
if (result != null)
cupTokenScanner.updateDeclarations(result.findDeclarations());
}
private void fullRefresh() {
System.out.println(">>>>>>>>>>>>>> FULL REFRESH OF SYNTAX HIGHLIGHTING! <<<<<<<<<<<<<<<");
sourceViewer.invalidateTextPresentation();
}
@Override
public void modelChanged(Model model) {
ParserResult result = model.getAstModel();
if (result != null) {
javaTokenScanner.updateParserResult(result);
Declarations newDecls = result.findDeclarations();
if (newDecls != null) {
if (!newDecls.equals(cupTokenScanner.getDeclarations())) {
cupTokenScanner.updateDeclarations(result
.findDeclarations());
if (updatedFromModelOnce)
sourceViewer.invalidateTextPresentation();
fullRefresh();
}
}
if (!updatedFromModelOnce) {
javaTokenScanner.updateParserResult(result);
System.out.println(">>>>>>>>>>>>>>>>> INITIAL invalidateTextPresentation !!!!");
sourceViewer.invalidateTextPresentation();
fullRefresh();
updatedFromModelOnce = true;
}
}
......@@ -154,15 +159,6 @@ public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
pr.setDamager(sLCommentDamagerRepairer,
CupPartionScanner.SINGLE_COMMENT);
// DefaultDamagerRepairer javaCommentDamagerRepairer = new
// DefaultDamagerRepairer(new SingleTokenScanner(new
// TextAttribute(CupTextEditor.KEYWORD)));
// pr.setRepairer(javaCommentDamagerRepairer,
// CupPartionScanner.JAVA_CODE);
// pr.setDamager(javaCommentDamagerRepairer,
// CupPartionScanner.JAVA_CODE);
CupDamagerRepairer javaDamagerRepairer = new CupDamagerRepairer(
javaTokenScanner);
pr.setRepairer(javaDamagerRepairer, CupPartionScanner.JAVA_CODE);
......@@ -188,23 +184,23 @@ public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
}
public void updateParserResult(ParserResult result) {
System.out.println("UPDATING PARSER RESULT");
this.ast = result;
this.previousCodeBlockRange = null;
}
private void refreshSpecialSymbols(Position pos) {
System.out.println("TRYING TO FIND POSITION: " + pos);
private void refreshSpecialSymbols(Position pos) {
// System.out.println("TRYING TO FIND POSITION: " + pos);
this.currentLabels.clear();
AbstractNode node = ast.findAtPosition(pos);
if (node == null)
return;
previousCodeBlockRange = node.getRange();
if (node instanceof ActionCodeBlock) {
ActionCodeBlock codeBlock = (ActionCodeBlock) node;
AbstractNode parent = codeBlock.getParent();
......@@ -291,25 +287,19 @@ public class CupSourceViewerConfiguration extends SourceViewerConfiguration {
if (last.isEOF())
return Token.EOF;
if (last.isBooleanLiteral() || last.isNullLiteral())
return cupDefinedVariable;
if (last.isKeyword() || last.isBooleanLiteral() || last.isNullLiteral())
return javaKeyword;
if (last.isTextualLiteral())
return javaTextualLiteral;
if (last.getValue() instanceof String) {
String lastValueStr = (String) last.getValue();
if (lastValueStr.equals("RESULT"))
return cupDefinedVariable;
if (currentLabels.contains(lastValueStr))
return cupDefinedVariable;
if (lastValueStr.equals("RESULT")
|| currentLabels.contains(lastValueStr))
return cupDefinedVariable;
}
if (last.isKeyword())
return javaKeyword;
return Token.UNDEFINED;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment