use of org.whole.lang.grammars.model.Rule in project whole by wholeplatform.
the class ParserTemplateFactory method updateRule.
protected void updateRule(Set<ActiveRule> completeSet, ActiveRule ar, Rule dotRule) {
Rule dotRuleParent = (Rule) dotRule.wGetParent();
switch(dotRuleParent.wGetEntityOrd()) {
case GrammarsEntityDescriptorEnum.Concatenate_ord:
int dotRuleIndex = dotRuleParent.wIndexOf(dotRule);
if (dotRuleIndex + 1 < dotRuleParent.wSize())
ar.setDotRule((Rule) dotRuleParent.wGet(dotRuleIndex + 1));
else
updateRule(completeSet, ar, dotRuleParent);
break;
case GrammarsEntityDescriptorEnum.Optional_ord:
case GrammarsEntityDescriptorEnum.As_ord:
updateRule(completeSet, ar, dotRuleParent);
break;
case GrammarsEntityDescriptorEnum.Production_ord:
ar.setDotRule(dotRuleParent);
completeSet.add(ar);
break;
}
}
use of org.whole.lang.grammars.model.Rule in project whole by wholeplatform.
the class ParserTemplateFactory method propagateActivation.
protected void propagateActivation(Set<ActiveRule> terminalSet, Set<NonTerminal> recursiveSet, ListIterator<ActiveRule> activeRules, ActiveRule ar) {
Rule dotRule = ar.getDotRule();
switch(dotRule.wGetEntityOrd()) {
case GrammarsEntityDescriptorEnum.DataTerminal_ord:
case GrammarsEntityDescriptorEnum.LiteralTerminal_ord:
terminalSet.add(ar);
break;
case GrammarsEntityDescriptorEnum.NonTerminal_ord:
NonTerminal nt = (NonTerminal) dotRule;
if (recursiveSet.add(nt)) {
activeRules.add(ar = createActiveRule(nt));
propagateActivation(terminalSet, recursiveSet, activeRules, ar);
} else
ar.setRecursive(true);
break;
case GrammarsEntityDescriptorEnum.Concatenate_ord:
Concatenate c = (Concatenate) dotRule;
if (c.wSize() > 0) {
ar.setDotRule((Rule) c.wGet(0));
propagateActivation(terminalSet, recursiveSet, activeRules, ar);
}
break;
case GrammarsEntityDescriptorEnum.Optional_ord:
Optional o = (Optional) dotRule;
ar.setDotRule(o.getRule());
propagateActivation(terminalSet, recursiveSet, activeRules, ar);
// TODO add also the rule after the optional
break;
case GrammarsEntityDescriptorEnum.Repeat_ord:
Repeat r = (Repeat) dotRule;
// TODO check lower bound and split active state for repeat continuation if 0
ar.setDotRule(r.getRule());
propagateActivation(terminalSet, recursiveSet, activeRules, ar);
break;
case GrammarsEntityDescriptorEnum.As_ord:
As as = (As) dotRule;
ar.setDotRule(as.getRule());
propagateActivation(terminalSet, recursiveSet, activeRules, ar);
break;
}
}
use of org.whole.lang.grammars.model.Rule in project whole by wholeplatform.
the class ParserTemplateFactory method matchTerminals.
/*
* For every ActiveRule in currentStates starting with a Terminal
* match it with the lexer and advance or remove it.
*
*/
public Set<ActiveRule> matchTerminals() {
Set<ActiveRule> matchSet = new HashSet<ActiveRule>();
java.util.regex.Pattern pattern = null;
for (ActivationState as : currentStates) {
ListIterator<ActiveRule> activeRules = as.getActiveRules().listIterator();
while (activeRules.hasNext()) {
ActiveRule ar = activeRules.next();
Rule dotRule = ar.getDotRule();
switch(dotRule.wGetEntityOrd()) {
case GrammarsEntityDescriptorEnum.DataTerminal_ord:
case GrammarsEntityDescriptorEnum.LiteralTerminal_ord:
java.util.regex.Pattern p = (java.util.regex.Pattern) dotRule.wGet(GrammarsFeatureDescriptorEnum.pattern).wGetValue();
lexer.withPattern(p);
if (lexer.hasNext()) {
if (pattern == null || p.pattern().equals(pattern.pattern())) {
pattern = p;
matchSet.add(ar);
String token = lexer.next();
if (dotRule.wGetEntityDescriptor().equals(GrammarsEntityDescriptorEnum.DataTerminal))
builder.wEntity(ed, token);
} else
throw new IllegalStateException("Backtracking is required for processing multiple matching patterns: ..., " + pattern.pattern());
} else
activeRules.remove();
}
}
}
return matchSet;
}
use of org.whole.lang.grammars.model.Rule in project whole by wholeplatform.
the class GenericGrammarBasedValidatorVisitor method calculateDataTerminals.
private void calculateDataTerminals(Grammar grammar) {
this.dataTerminals = new HashMap<EntityDescriptor<?>, DataTerminal>();
// TODO ensure grammar normalized
Map<String, Rule> productions = new HashMap<String, Rule>();
IEntityIterator<Production> pi = IteratorFactory.<Production>childIterator();
pi.reset(grammar.getPhraseStructure());
for (Production p : pi) productions.put(p.getName().getValue(), p.getRule());
Map<String, Rule> lexicon = new HashMap<String, Rule>();
IEntityIterator<Production> li = IteratorFactory.<Production>childIterator();
li.reset(grammar.getLexicalStructure());
for (Production p : li) lexicon.put(p.getName().getValue(), p.getRule());
ILanguageKit languageKit = ReflectionFactory.getLanguageKit(GrammarsUtils.getLanguageURI(grammar), false, null);
EntityDescriptorEnum edEnum = languageKit.getEntityDescriptorEnum();
for (EntityDescriptor<?> ed : edEnum) if (EntityUtils.isData(ed)) {
Rule production = productions.get(ed.getName());
if (!ed.getDataKind().isEnumValue()) {
DataTerminal dataTerminal = Matcher.find(GrammarsEntityDescriptorEnum.DataTerminal, production, false);
if (dataTerminal == null) {
NonTerminal nonTerminal = Matcher.find(GrammarsEntityDescriptorEnum.NonTerminal, production, false);
dataTerminal = Matcher.find(GrammarsEntityDescriptorEnum.DataTerminal, lexicon.get(nonTerminal.getValue()), false);
}
dataTerminals.put(ed, EntityUtils.clone(dataTerminal));
}
}
}
use of org.whole.lang.grammars.model.Rule in project whole by wholeplatform.
the class GrammarBasedUnparserVisitor method visit.
@Override
public void visit(Choose entity) {
if (!appendModelEntityAsFragment()) {
if (Matcher.match(GrammarsEntityDescriptorEnum.As, entity.wGet(0)))
append(getAsString(model));
else {
// get rule using model entity
EntityDescriptorEnum edEnum = model.wGetLanguageKit().getEntityDescriptorEnum();
Rule rule = null;
for (int size = entity.wSize(), i = 0; rule == null && i < size; i++) {
Rule child = (Rule) entity.wGet(i);
NonTerminal nt = (NonTerminal) Matcher.find(phraseNonTerminal, child, false);
if (// TODO test was isAssignableFrom
Matcher.isAssignableAsIsFrom(edEnum.valueOf(nt.getValue()), model))
rule = child;
}
if (rule != null)
rule.accept(this);
else
throw new IllegalStateException("missing choose rule for model entity");
}
}
}
Aggregations