org.modelcc.parser.fence.FencePrecedenceConstraints Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ModelCC Show documentation
Show all versions of ModelCC Show documentation
ModelCC is a model-based parser generator (a.k.a. compiler compiler) that decouples language specification from language processing, avoiding some of the problems caused by grammar-driven parser generators. ModelCC receives a conceptual model as input, along with constraints that annotate it. It is then able to create a parser for the desired textual language and the generated parser fully automates the instantiation of the language conceptual model. ModelCC also includes a built-in reference resolution mechanism that results in abstract syntax graphs, rather than mere abstract syntax trees.
The newest version!
package org.modelcc.parser.fence;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.modelcc.language.syntax.Rule;
import org.modelcc.language.syntax.Symbol;
import org.modelcc.language.syntax.SyntaxConstraints;
/**
* Fence precedence constraints
*
* @author Luis Quesada ([email protected]), refactored by Fernando Berzal ([email protected])
*/
public class FencePrecedenceConstraints
{
private FenceConstraintEnforcer fence;
private SyntaxConstraints constraints;
private Set toremove;
public FencePrecedenceConstraints (FenceConstraintEnforcer fence, SyntaxConstraints constraints)
{
this.fence = fence;
this.constraints = constraints;
this.toremove = new HashSet();
}
public Set select (Set hs)
{
Set hsf = new HashSet();
while (!hs.isEmpty()) {
Set precf = new HashSet();
Set precall = new HashSet();
for (Symbol hs2: hsf) {
Set aux1 = constraints.getSelectionPrecedences(hs2.getRule());
if (aux1 != null) {
precf.addAll(aux1);
precall.addAll(aux1);
}
Set aux2 = constraints.getSelectionPrecedences(hs2.getRelevantRule());
if (aux2 != null) {
precf.addAll(aux2);
precall.addAll(aux2);
}
}
for (Symbol hs2: hs) {
Set aux1 = constraints.getSelectionPrecedences(hs2.getRule());
if (aux1 != null) {
precall.addAll(aux1);
}
Set aux2 = constraints.getSelectionPrecedences(hs2.getRelevantRule());
if (aux2 != null) {
precall.addAll(aux2);
}
}
for (Iterator ites = hs.iterator();ites.hasNext();) {
Symbol hs2 = ites.next();
if (precf.contains(hs2.getRule()) || precf.contains(hs2.getRelevantRule())) {
ites.remove();
} else if (!precall.contains(hs2.getRule()) && !precall.contains(hs2.getRelevantRule())) {
ites.remove();
hsf.add(hs2);
}
}
}
return hsf;
}
public void process (Rule rule, Symbol s)
{
Set ruleConstMe = constraints.getStartPrecedences(rule);
for (Symbol symbol: fence.getSymbolsAt(s.getStartIndex())) {
// symbol.getStartIndex()==s.getStartIndex()
if (symbol.getType().equals(s.getType())) {
Set ruleConst = constraints.getStartPrecedences(symbol.getRule());
boolean meInRuleConst = false;
if (ruleConst != null)
if (ruleConst.contains(rule))
meInRuleConst = true;
boolean otherInRuleConst = false;
if (ruleConstMe != null)
if (ruleConstMe.contains(symbol.getRule()))
otherInRuleConst = true;
if (meInRuleConst) {
toremove.add(s);
}
if (otherInRuleConst) {
toremove.add(symbol);
}
}
}
}
public void filter ()
{
for (Symbol s: fence.getSyntaxGraph().getSymbols()) {
if (!toremove.contains(s)) {
if (s.getRule() != null) {
if (!fence.postBuild(s.getRule(),s)) {
toremove.add(s);
}
}
}
}
for (Symbol s: toremove)
fence.removeSymbol(s);
}
}