Search in sources :

Example 41 with Grammar

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));
}
Also used : Productions(org.whole.lang.grammars.model.Productions) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Production(org.whole.lang.grammars.model.Production) NonTerminal(org.whole.lang.grammars.model.NonTerminal) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) Test(org.junit.Test)

Example 42 with Grammar

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());
}
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 43 with Grammar

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());
}
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 44 with Grammar

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);
            }
        }
    }
}
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 45 with Grammar

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());
}
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)

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