use of org.whole.lang.grammars.model.Grammar 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.Grammar in project whole by wholeplatform.
the class IteratorFactoryTest method testChildIteratorAdd.
@Test
public void testChildIteratorAdd() {
Grammar g = new TestXmlGrammar().create();
Productions productions = g.getPhraseStructure();
int size = productions.wSize();
int count = 0;
Production pAdded1 = null;
Production pAdded2 = null;
IEntityIterator<Production> i = IteratorFactory.<Production>childIterator();
i.reset(productions);
while (i.hasNext()) {
Production p = i.next();
assertNotSame(pAdded1, p);
assertNotSame(pAdded2, p);
count++;
if (count == 1)
i.add(pAdded1 = GrammarsEntityFactory.instance.createProduction());
if (count == 3) {
i.add(pAdded1 = GrammarsEntityFactory.instance.createProduction());
i.add(pAdded2 = GrammarsEntityFactory.instance.createProduction());
}
}
assertEquals(size, count);
assertEquals(size + 3, productions.wSize());
}
use of org.whole.lang.grammars.model.Grammar in project whole by wholeplatform.
the class IteratorFactoryTest method testRootBehavior.
@Test
public void testRootBehavior() {
Grammar g = new TestXmlGrammar().create();
CommonsEntityFactory.instance.createRootFragment(g.wGetAdapter(CommonsEntityDescriptorEnum.Any));
// rootParent
IEntity gParent = g.wGetParent();
IEntityIterator<IEntity> i = IteratorFactory.rootIterator();
i.reset(g);
assertTrue(i.hasNext());
IEntity e1 = i.next();
assertSame(g, e1);
i = IteratorFactory.rootIterator();
i.reset(gParent);
assertTrue(i.hasNext());
e1 = i.next();
assertSame(g, e1);
i = IteratorFactory.fragmentRootIterator();
i.reset(gParent);
assertTrue(i.hasNext());
e1 = i.next();
assertSame(g, e1);
i = IteratorFactory.parentIterator();
i.reset(g);
assertTrue(i.hasNext());
e1 = i.next();
assertSame(g.wGetParent(), e1);
i = IteratorFactory.parentIterator();
i.reset(gParent);
assertFalse(i.hasNext());
assertNull(i.lookahead());
i = IteratorFactory.ancestorIterator();
i.reset(g);
assertTrue(i.hasNext());
e1 = i.next();
assertSame(g.wGetParent(), e1);
i = IteratorFactory.ancestorIterator();
i.reset(gParent);
assertFalse(i.hasNext());
assertNull(i.lookahead());
i = IteratorFactory.ancestorOrSelfIterator();
i.reset(g);
assertTrue(i.hasNext());
e1 = i.next();
assertSame(g, e1);
assertTrue(i.hasNext());
e1 = i.next();
assertSame(g.wGetParent(), e1);
i = IteratorFactory.ancestorOrSelfIterator();
i.reset(gParent);
assertTrue(i.hasNext());
e1 = i.next();
assertSame(g.wGetParent(), e1);
assertFalse(i.hasNext());
assertNull(i.lookahead());
IEntityIterator<Grammar> i2 = IteratorFactory.selfIterator();
i2.reset(g);
assertTrue(i2.hasNext());
e1 = i2.next();
assertSame(g, e1);
assertFalse(i2.hasNext());
assertNull(i2.lookahead());
i = IteratorFactory.selfIterator();
i.reset(gParent);
assertTrue(i.hasNext());
e1 = i.next();
assertSame(gParent, e1);
assertFalse(i.hasNext());
assertNull(i.lookahead());
g = GrammarsEntityFactory.instance.createGrammar();
// nullEntity
gParent = g.wGetParent();
i = IteratorFactory.rootIterator();
i.reset(g);
assertTrue(i.hasNext());
e1 = i.next();
assertSame(g, e1);
i = IteratorFactory.rootIterator();
i.reset(gParent);
assertFalse(i.hasNext());
assertNull(i.lookahead());
i = IteratorFactory.parentIterator();
i.reset(g);
assertFalse(i.hasNext());
assertNull(i.lookahead());
i = IteratorFactory.parentIterator();
i.reset(gParent);
assertFalse(i.hasNext());
assertNull(i.lookahead());
i = IteratorFactory.ancestorIterator();
i.reset(g);
assertFalse(i.hasNext());
assertNull(i.lookahead());
i = IteratorFactory.ancestorIterator();
i.reset(gParent);
assertFalse(i.hasNext());
assertNull(i.lookahead());
i = IteratorFactory.ancestorOrSelfIterator();
i.reset(g);
assertTrue(i.hasNext());
e1 = i.next();
assertSame(g, e1);
assertFalse(i.hasNext());
assertNull(i.lookahead());
i = IteratorFactory.ancestorOrSelfIterator();
i.reset(gParent);
assertFalse(i.hasNext());
assertNull(i.lookahead());
i2 = IteratorFactory.selfIterator();
i2.reset(g);
assertTrue(i2.hasNext());
e1 = i2.next();
assertSame(g, e1);
assertFalse(i2.hasNext());
assertNull(i2.lookahead());
i = IteratorFactory.selfIterator();
i.reset(gParent);
assertFalse(i.hasNext());
assertNull(i.lookahead());
}
use of org.whole.lang.grammars.model.Grammar in project whole by wholeplatform.
the class IteratorFactoryTest method testPrecedingIterator.
@Test
public void testPrecedingIterator() {
Grammar g = new TestXmlGrammar().create();
IEntityIterator<IEntity> tdi = IteratorFactory.<IEntity>descendantOrSelfReverseIterator();
tdi.reset(g);
for (IEntity e : tdi) {
IEntityIterator<IEntity> fi = IteratorFactory.<IEntity>precedingIterator();
fi.reset(e);
if (fi.hasNext()) {
IEntityIterator<IEntity> tdi2 = tdi.clone();
tdi2.prune();
assertSame(tdi2.hasNext(), fi.hasNext());
assertSame(tdi2.lookahead(), fi.lookahead());
for (IEntity fe : fi) {
IEntity e1 = tdi2.next();
assertSame(e1, fe);
}
}
}
}
use of org.whole.lang.grammars.model.Grammar in project whole by wholeplatform.
the class IteratorFactoryTest method testTopDownScannerIteratorMarkReset.
@Test
public void testTopDownScannerIteratorMarkReset() {
Grammar g = new TestXmlGrammar().create();
IEntityIterator<IEntity> i = IteratorFactory.<IEntity>descendantOrSelfScannerIterator();
i.reset(g);
for (int j = 0; j < 15; j++) i.next();
IEntityIterator<IEntity> i2 = i.clone();
IEntityIterator<IEntity> i3 = i.clone();
IEntity e1 = i.next();
IEntity e2 = i.next();
for (int j = 0; j < 7; j++) i.next();
assertSame(e1, i2.next());
assertSame(e1, i3.next());
assertSame(e2, i3.next());
}
Aggregations