use of org.whole.lang.grammars.model.NonTerminal 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.NonTerminal 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.NonTerminal 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");
}
}
}
use of org.whole.lang.grammars.model.NonTerminal in project whole by wholeplatform.
the class GrammarsContentAssistVisitor method calculateNonTerminalValues.
public static Set<String> calculateNonTerminalValues(IEntity e) {
Grammar grammar = Matcher.findAncestor(Grammar, e);
if (grammar != null) {
Set<String> names = new HashSet<String>();
IEntity parent = e.wGetParent();
ILanguageKit languageKit = getLanguageKitIfExists(grammar);
if (languageKit != null) {
if (!grammar.getLexicalStructure().equals(parent.wGetParent()))
names.addAll(languageKit.getEntityDescriptorEnum().names());
if (!parent.wGetFeatureDescriptor(e).equals(name)) {
QueriesEntityFactory qef = QueriesEntityFactory.instance;
Path allTokenNames = qef.createPath(qef.createSequence(qef.createFeatureStep("lexicalStructure"), qef.createFeatureStep("phraseStructure")), qef.createChildStep(), qef.createFilter(qef.createFeatureStep("name"), qef.createKindTest(KindTestEnum.IMPL)));
for (NonTerminal nt : BehaviorUtils.<NonTerminal>compileAndLazyEvaluate(allTokenNames, grammar)) names.add(nt.getValue());
}
} else {
AbstractPatternFilterIterator<NonTerminal> i = IteratorFactory.<NonTerminal>descendantOrSelfMatcherIterator().withPattern(NonTerminal);
i.reset(grammar);
for (NonTerminal nt : i) if (EntityUtils.isNotResolver(nt))
names.add(nt.getValue());
}
return names;
}
return Collections.emptySet();
}
use of org.whole.lang.grammars.model.NonTerminal in project whole by wholeplatform.
the class PathExpressionsQueriesTest method testDistinct.
@Test
public void testDistinct() {
ITemplateManager tm = PathExpressionsQueriesTemplateManager.instance();
Grammar g = new TestXmlGrammar().create();
PathExpression pe1 = (PathExpression) tm.create("nonTerminalSet");
Set<String> set = new HashSet<String>();
for (NonTerminal nt : BehaviorUtils.<NonTerminal>compileAndLazyEvaluate(pe1, g)) if (!set.add(nt.getValue()))
Assert.fail();
Assert.assertEquals(22, set.size());
}
Aggregations