Search in sources :

Example 36 with Grammar

use of org.whole.lang.grammars.model.Grammar in project whole by wholeplatform.

the class IteratorFactoryTest method testChildIteratorRemove.

@Test
public void testChildIteratorRemove() {
    Grammar g = new TestXmlGrammar().create();
    Productions productions = EntityUtils.clone(g.getPhraseStructure());
    IEntityIterator<Production> ci = IteratorFactory.<Production>childIterator();
    ci.reset(productions);
    while (ci.hasNext()) {
        Production p = ci.next();
        assertSame(productions.wGet(0), p);
        ci.remove();
    }
    assertEquals(0, productions.wSize());
    productions = EntityUtils.clone(g.getPhraseStructure());
    ci = IteratorFactory.<Production>childScannerIterator();
    ci.reset(productions);
    while (ci.hasNext()) {
        Production p = ci.next();
        assertSame(productions.wGet(0), p);
        ci.remove();
    }
    assertEquals(0, productions.wSize());
    productions = EntityUtils.clone(g.getPhraseStructure());
    ci = IteratorFactory.<Production>childMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.Production);
    ci.reset(productions);
    while (ci.hasNext()) {
        Production p = ci.next();
        assertSame(productions.wGet(0), p);
        ci.remove();
    }
    assertEquals(0, productions.wSize());
}
Also used : Productions(org.whole.lang.grammars.model.Productions) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Production(org.whole.lang.grammars.model.Production) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) Test(org.junit.Test)

Example 37 with Grammar

use of org.whole.lang.grammars.model.Grammar in project whole by wholeplatform.

the class IteratorFactoryTest method testFilterByIndexIterator.

@Test
public void testFilterByIndexIterator() {
    Grammar g = new TestXmlGrammar().create();
    Productions productions = g.getPhraseStructure();
    Production p = (Production) productions.wGet(1);
    IEntityIterator<?> i = IteratorFactory.filterByIndexIterator(IteratorFactory.followingSiblingIterator(), 0);
    i.reset(p);
    assertSingleton(i, productions.wGet(2));
    i = IteratorFactory.filterByIndexIterator(IteratorFactory.followingSiblingIterator(), 1);
    i.reset(p);
    assertSingleton(i, productions.wGet(3));
    i = IteratorFactory.filterByIndexIterator(IteratorFactory.ancestorOrSelfIterator(), 2);
    i.reset(p);
    assertSingleton(i, productions.wGetParent());
    i = IteratorFactory.filterByIndexIterator(IteratorFactory.descendantOrSelfIterator(), 1);
    i.reset(p);
    assertSingleton(i, p.getName());
}
Also used : Productions(org.whole.lang.grammars.model.Productions) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Production(org.whole.lang.grammars.model.Production) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) Test(org.junit.Test)

Example 38 with Grammar

use of org.whole.lang.grammars.model.Grammar in project whole by wholeplatform.

the class IteratorFactoryTest method testFollowingIterator.

@Test
public void testFollowingIterator() {
    Grammar g = new TestXmlGrammar().create();
    IEntityIterator<IEntity> tdi = IteratorFactory.<IEntity>descendantOrSelfIterator();
    tdi.reset(g);
    for (IEntity e : tdi) {
        IEntityIterator<IEntity> fi = IteratorFactory.<IEntity>followingIterator();
        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);
            }
        }
    }
}
Also used : IEntity(org.whole.lang.model.IEntity) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) Test(org.junit.Test)

Example 39 with Grammar

use of org.whole.lang.grammars.model.Grammar in project whole by wholeplatform.

the class IteratorFactoryTest method testVariableIterator.

@Test
public void testVariableIterator() {
    Grammar g = new TestXmlGrammar().create();
    IBindingManager bindings = BindingManagerFactory.instance.createBindingManager();
    IEntityIterator<IEntity> i = IteratorFactory.variableIterator("testVar");
    i.setBindings(bindings);
    assertFalse(i.hasNext());
    i.reset(g);
    assertFalse(i.hasNext());
    bindings.wDef("testVar", g);
    assertTrue(i.hasNext());
    IEntity g1 = i.next();
    assertSame(g, g1);
    assertFalse(i.hasNext());
    i.reset(g);
    assertTrue(i.hasNext());
    g1 = i.next();
    assertSame(g, g1);
    assertFalse(i.hasNext());
    bindings = BindingManagerFactory.instance.createBindingManager();
    bindings.wDef("testVar", g);
    i = IteratorFactory.variableIterator("testVar");
    i.setBindings(bindings);
    assertTrue(i.hasNext());
    g1 = i.next();
    assertSame(g, g1);
    assertFalse(i.hasNext());
    i.reset(g);
    IEntityIterator<IEntity> i2 = i.clone();
    assertTrue(i.hasNext());
    g1 = i.next();
    assertSame(g, g1);
    assertFalse(i.hasNext());
    assertTrue(i2.hasNext());
    g1 = i2.next();
    assertSame(g, g1);
    assertFalse(i2.hasNext());
}
Also used : IEntity(org.whole.lang.model.IEntity) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) IBindingManager(org.whole.lang.bindings.IBindingManager) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) Test(org.junit.Test)

Example 40 with Grammar

use of org.whole.lang.grammars.model.Grammar in project whole by wholeplatform.

the class IteratorFactoryTest method testParentIteratorMarkReset.

@Test
public void testParentIteratorMarkReset() {
    Grammar g = new TestXmlGrammar().create();
    Rule r = Matcher.find(GrammarsEntityDescriptorEnum.As, g, false);
    IEntityIterator<IEntity> i = IteratorFactory.parentIterator();
    i.reset(r);
    IEntityIterator<IEntity> i2 = i.clone();
    assertTrue(i.hasNext());
    IEntity e1 = i.next();
    assertSame(r.wGetParent(), e1);
    assertTrue(i2.hasNext());
    assertSame(r.wGetParent(), i2.next());
}
Also used : IEntity(org.whole.lang.model.IEntity) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) Rule(org.whole.lang.grammars.model.Rule) Test(org.junit.Test)

Aggregations

Grammar (org.whole.lang.grammars.model.Grammar)96 Test (org.junit.Test)82 TestXmlGrammar (org.whole.lang.grammars.util.TestXmlGrammar)80 IEntity (org.whole.lang.model.IEntity)50 ITemplateManager (org.whole.lang.templates.ITemplateManager)46 Production (org.whole.lang.grammars.model.Production)43 PathExpression (org.whole.lang.queries.model.PathExpression)40 IBindingManager (org.whole.lang.bindings.IBindingManager)28 QueriesGrammar (org.whole.lang.grammars.codebase.QueriesGrammar)25 NonTerminal (org.whole.lang.grammars.model.NonTerminal)14 Productions (org.whole.lang.grammars.model.Productions)14 HashSet (java.util.HashSet)8 PrettyPrinterOperation.toPrettyPrintString (org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString)7 Rule (org.whole.lang.grammars.model.Rule)6 XmlGrammar (org.whole.lang.grammars.codebase.XmlGrammar)3 As (org.whole.lang.grammars.model.As)3 Feature (org.whole.lang.models.model.Feature)3 ILanguageKit (org.whole.lang.reflect.ILanguageKit)3 ArrayList (java.util.ArrayList)2 IGrammarProvider (org.whole.lang.grammars.codebase.IGrammarProvider)2