use of org.whole.lang.grammars.model.NonTerminal in project whole by wholeplatform.
the class IteratorFactoryTest method testTopDownMatcherIteratorRemove.
@Test
public void testTopDownMatcherIteratorRemove() {
Grammar g = new TestXmlGrammar().create();
g.wRemove(GrammarsFeatureDescriptorEnum.lexicalStructure);
Productions productions = g.getPhraseStructure();
IEntityIterator<Production> ci = IteratorFactory.<Production>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.Production);
ci.reset(g);
while (ci.hasNext()) {
Production p = ci.next();
assertSame(productions.wGet(0), p);
ci.remove();
}
assertEquals(0, productions.wSize());
g = new TestXmlGrammar().create();
IEntityIterator<NonTerminal> ci2 = IteratorFactory.<NonTerminal>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.NonTerminal);
ci2.reset(g);
while (ci2.hasNext()) {
NonTerminal nt = ci2.next();
ci2.remove();
}
assertNull(Matcher.find(GenericTraversalFactory.instance.sequence(GenericMatcherFactory.instance.hasTypeMatcher(GrammarsEntityDescriptorEnum.NonTerminal), GenericMatcherFactory.instance.isImplMatcher()), g, false));
}
use of org.whole.lang.grammars.model.NonTerminal in project whole by wholeplatform.
the class IteratorFactoryTest method testComposeIterator.
@Test
public void testComposeIterator() {
Grammar g = new TestXmlGrammar().create();
// query: g/phraseStructure/child()[ED = Production]/rule//[ED = Production]
IEntityIterator<Production> pi = IteratorFactory.composeIterator(IteratorFactory.<Production>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.Production), IteratorFactory.featureByNameIterator("rule"), IteratorFactory.<Production>childMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.Production), IteratorFactory.featureByNameIterator("phraseStructure"));
pi.reset(g);
Set<String> l = new HashSet<String>();
for (Production p : pi) l.add(p.getName().getValue());
assertEquals(16, l.size());
NonTerminal prologNt = ((Production) ((Production) g.getPhraseStructure().wGet(0)).getRule().wGet(0)).getName();
// query: g/phraseStructure/0/rule/0/name
IEntityIterator<? extends IEntity> nti = IteratorFactory.composeIterator(IteratorFactory.featureByNameIterator("name"), IteratorFactory.featureByIndexIterator(0), IteratorFactory.featureByNameIterator("rule"), IteratorFactory.featureByIndexIterator(0), IteratorFactory.featureByNameIterator("phraseStructure"));
nti.reset(g);
assertTrue(nti.hasNext());
assertSame(prologNt, nti.next());
nti.reset(g.getPhraseStructure());
assertFalse(nti.hasNext());
nti.reset(g);
assertTrue(nti.hasNext());
assertSame(prologNt, nti.next());
}
use of org.whole.lang.grammars.model.NonTerminal in project whole by wholeplatform.
the class Grammars2ModelsVisitor method visit.
@Override
public void visit(Production entity) {
ModelsEntityFactory mf = ModelsEntityFactory.instance;
String eName = getMappedEntityName(entity);
Rule rule = entity.getRule();
AbstractPatternFilterIterator<Rule> ruleIterator = IteratorFactory.<Rule>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.Rule);
ruleIterator.reset(rule);
while (ruleIterator.hasNext()) {
rule = ruleIterator.next();
switch(rule.wGetEntityOrd()) {
// map productions with a single lexical non-terminal as data entities
case GrammarsEntityDescriptorEnum.NonTerminal_ord:
if (!isLexicalNonTerminal((NonTerminal) rule) || !EntityUtils.hasParent(rule) || !Matcher.match(GrammarsEntityDescriptorEnum.Production, rule.wGetParent()) || !Matcher.match(GrammarsEntityDescriptorEnum.DataTerminal, lexiconMap.get(((NonTerminal) rule).getValue()).getRule()))
break;
case GrammarsEntityDescriptorEnum.DataTerminal_ord:
DataEntity de = getModelDeclaration(eName, ModelsEntityDescriptorEnum.DataEntity);
Template template = entity.getTemplate();
if (Matcher.matchImpl(GrammarsEntityDescriptorEnum.DataType, template))
de.getDataType().wSetValue(GrammarsUtils.getDataType((DataType) template));
else if (Matcher.matchImpl(GrammarsEntityDescriptorEnum.CustomDataType, template))
de.getDataType().wSetValue(template.wStringValue());
return;
case GrammarsEntityDescriptorEnum.Choose_ord:
boolean isPolymorphic = false;
AbstractPatternFilterIterator<NonTerminal> ruleIterator2 = IteratorFactory.<NonTerminal>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.NonTerminal);
ruleIterator2.reset(rule);
for (NonTerminal nt2 : ruleIterator2) if (!isLexicalNonTerminal(nt2)) {
ensureType(getModelDeclaration(getMappedEntityName(nt2)).getTypes(), eName);
isPolymorphic = true;
}
if (isPolymorphic) {
SimpleEntity se = getModelDeclaration(eName, ModelsEntityDescriptorEnum.SimpleEntity);
se.getModifiers().wAdd(mf.createEntityModifier(EntityModifierEnum._abstract));
return;
} else if (!rule.wIsEmpty() && Matcher.match(GrammarsEntityDescriptorEnum.As, rule.wGet(0))) {
EnumEntity ee = getModelDeclaration(eName, ModelsEntityDescriptorEnum.EnumEntity);
EnumValues enumValues = ModelsEntityFactory.instance.createEnumValues(0);
AbstractPatternFilterIterator<As> ruleIterator3 = IteratorFactory.<As>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.As);
ruleIterator3.reset(rule);
for (As as : ruleIterator3) enumValues.wAdd(ModelsEntityFactory.instance.createEnumValue(as.getName().getValue()));
ee.setValues(enumValues);
return;
}
// }
break;
case GrammarsEntityDescriptorEnum.Repeat_ord:
if (ruleIterator.skipTo(GrammarsEntityDescriptorEnum.NonTerminal)) {
// FIXME wrongly assumes that the separator, if present, is a lexical token
if (!EntityUtils.isResolver(((Repeat) rule).getSeparator())) {
// if (Matcher.matchImpl(GrammarsEntityDescriptorEnum.NonTerminal, ((Repeat) rule).getSeparator())) {
ruleIterator.next();
ruleIterator.skipTo(GrammarsEntityDescriptorEnum.NonTerminal);
}
NonTerminal nt = null;
AbstractPatternFilterIterator<NonTerminal> ruleIterator4 = IteratorFactory.<NonTerminal>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.NonTerminal);
ruleIterator4.reset(rule);
for (NonTerminal nt2 : ruleIterator4) if (!isLexicalNonTerminal(nt2)) {
nt = nt2;
break;
}
// by now we assume that the separator, if present, is a lexical token
if (nt == null)
break;
// the above statements replace this code, allowing lexical Repeat rule
// NonTerminal nt = ruleIterator.next(GrammarsEntityDescriptorEnum.NonTerminal);
// while (isLexicalNonTerminal(nt)) {
// nt = ruleIterator.next(GrammarsEntityDescriptorEnum.NonTerminal);
// }
CompositeEntity ce = getModelDeclaration(eName, ModelsEntityDescriptorEnum.CompositeEntity);
ce.setComponentType(mf.createSimpleName(getMappedEntityName(nt)));
ce.getComponentModifiers().wAdd(mf.createComponentModifier(ComponentModifierEnum.ordered));
}
return;
case GrammarsEntityDescriptorEnum.As_ord:
As asRule = (As) rule;
// normalization invariants assure that this branch is unreachable
// if (Matcher.match(GrammarsEntityDescriptorEnum.LiteralTerminal, asRule.getRule())) {
// EnumEntity ee = getModelDeclaration(eName, ModelsEntityDescriptorEnum.EnumEntity);
//
// ee.getValues().wAdd(mf.createEnumValue(getMappedName(asRule)));
//
// while (ruleIterator.skipTo(GrammarsEntityDescriptorEnum.As)) {
// rule = ruleIterator.next(GrammarsEntityDescriptorEnum.As);
// asRule = (As) rule;
//
// ee.getValues().wAdd(mf.createEnumValue(getMappedName(asRule)));
// }
// } else {
SimpleEntity se = getModelDeclaration(eName, ModelsEntityDescriptorEnum.SimpleEntity);
Features features = se.getFeatures();
ensureFeature(features, ruleIterator, asRule);
while (ruleIterator.hasNext()) {
rule = ruleIterator.next();
switch(rule.wGetEntityOrd()) {
case GrammarsEntityDescriptorEnum.As_ord:
asRule = (As) rule;
ensureFeature(features, ruleIterator, asRule);
break;
}
}
// }
return;
case GrammarsEntityDescriptorEnum.Concatenate_ord:
break;
}
}
getModelDeclaration(eName, ModelsEntityDescriptorEnum.SimpleEntity);
}
use of org.whole.lang.grammars.model.NonTerminal in project whole by wholeplatform.
the class GrammarsValidatorVisitor method defUseAnalisys.
public void defUseAnalisys(Grammar entity) {
Set<String> ntDefs = new HashSet<String>();
Set<String> ntUses = new HashSet<String>();
Set<NonTerminal> nts = new HashSet<NonTerminal>();
AbstractPatternFilterIterator<NonTerminal> i = IteratorFactory.<NonTerminal>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.NonTerminal);
i.reset(entity);
for (NonTerminal nt : i) {
IEntity parent = nt.wGetParent();
if (Matcher.match(GrammarsEntityDescriptorEnum.Production, parent) && parent.wGet(GrammarsFeatureDescriptorEnum.name) == nt)
ntDefs.add(nt.getValue());
else {
ntUses.add(nt.getValue());
nts.add(nt);
}
}
Set<String> ntNotUsed = new HashSet<String>(ntDefs);
ntNotUsed.removeAll(ntUses);
Set<String> ntNotDef = new HashSet<String>(ntUses);
ntNotDef.removeAll(ntDefs);
for (NonTerminal nt : nts) if (!ntDefs.contains(nt.getValue()))
// TODO location with production
getDecorationManager().addError(nt, "Production not defined", nt.getValue());
AbstractPatternFilterIterator<Production> i2 = IteratorFactory.<Production>childMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.Production);
i2.reset(entity.getPhraseStructure());
for (Production p : i2) if (!ntUses.contains(p.getName().wGetValue()))
getDecorationManager().addInfo(p, "Production not used", p.getName().getValue());
}
use of org.whole.lang.grammars.model.NonTerminal in project whole by wholeplatform.
the class PathExpressionsQueriesTest method testSingleVariableTestInPathLastStep.
@Test
public void testSingleVariableTestInPathLastStep() {
ITemplateManager tm = PathExpressionsQueriesTemplateManager.instance();
Grammar g = new TestXmlGrammar().create();
PathExpression pe1 = (PathExpression) tm.create("findNonTerminalOccurrences");
PathExpression pe2 = (PathExpression) tm.create("bindNonTerminalOccurrences");
IBindingManager bm = BindingManagerFactory.instance.createArguments();
IEntityIterator<NonTerminal> i1 = BehaviorUtils.<NonTerminal>compileAndLazyEvaluate(pe1, g);
for (NonTerminal nt : BehaviorUtils.<NonTerminal>compileAndLazyEvaluate(pe2, g, bm)) {
Assert.assertSame(nt, i1.next());
Assert.assertEquals(nt.getValue(), bm.wStringValue("nt"));
}
Assert.assertFalse(i1.hasNext());
}
Aggregations