Search in sources :

Example 51 with Grammar

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

the class IteratorFactoryTest method testPrecedingSiblingIterator.

@Test
public void testPrecedingSiblingIterator() {
    Grammar g = new TestXmlGrammar().create();
    Productions productions = g.getPhraseStructure();
    int index = 0;
    IEntityIterator<Production> ci = IteratorFactory.<Production>childIterator();
    ci.reset(productions);
    while (ci.hasNext()) {
        Production p = ci.next();
        IEntityIterator<Production> fi = IteratorFactory.<Production>precedingSiblingIterator();
        fi.reset(p);
        int findex = index++;
        for (Production fp : fi) assertSame(productions.wGet(--findex), fp);
    }
    for (int i = g.wSize() - 1; i >= 0; i--) {
        IEntityIterator<IEntity> fi = IteratorFactory.<IEntity>precedingSiblingIterator();
        fi.reset(g.wGet(i));
        for (int j = i - 1; j >= 0; j--) assertSame(g.wGet(j), fi.next());
    }
    for (int i = productions.wSize() - 1; i >= 0; i--) {
        IEntityIterator<Production> fi = IteratorFactory.<Production>precedingSiblingIterator();
        fi.reset(productions.wGet(i));
        for (int j = i - 1; j >= 0; j--) assertSame(productions.wGet(j), fi.next());
    }
}
Also used : Productions(org.whole.lang.grammars.model.Productions) IEntity(org.whole.lang.model.IEntity) 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 52 with Grammar

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

the class IteratorFactoryTest method testAxesPartitionInvariant.

@Test
public void testAxesPartitionInvariant() {
    Grammar g = new TestXmlGrammar().create();
    IEntityIterator<IEntity> i = IteratorFactory.<IEntity>descendantOrSelfIterator();
    i.reset(g);
    Set<IEntity> s = entities(i);
    IEntityIterator<IEntity> i2 = IteratorFactory.<IEntity>descendantOrSelfIterator();
    i2.reset(g);
    for (IEntity e : i2) {
        IEntityIterator<IEntity> i1 = IteratorFactory.<IEntity>ancestorIterator();
        i1.reset(e);
        Set<IEntity> s1 = entities(i1);
        IEntityIterator<IEntity> i22 = IteratorFactory.selfIterator();
        i22.reset(e);
        Set<IEntity> s2 = entities(i22);
        IEntityIterator<IEntity> i3 = IteratorFactory.<IEntity>descendantIterator();
        i3.reset(e);
        Set<IEntity> s3 = entities(i3);
        IEntityIterator<IEntity> i4 = IteratorFactory.<IEntity>precedingIterator();
        i4.reset(e);
        Set<IEntity> s4 = entities(i4);
        IEntityIterator<IEntity> i5 = IteratorFactory.<IEntity>followingIterator();
        i5.reset(e);
        Set<IEntity> s5 = entities(i5);
        assertFalse(s1.removeAll(s2));
        assertFalse(s1.removeAll(s3));
        assertFalse(s1.removeAll(s4));
        assertFalse(s1.removeAll(s5));
        assertFalse(s2.removeAll(s3));
        assertFalse(s2.removeAll(s4));
        assertFalse(s2.removeAll(s5));
        assertFalse(s3.removeAll(s4));
        assertFalse(s3.removeAll(s5));
        assertFalse(s4.removeAll(s5));
        assertFalse(s.addAll(s1));
        assertFalse(s.addAll(s2));
        assertFalse(s.addAll(s3));
        assertFalse(s.addAll(s4));
        assertFalse(s.addAll(s5));
        assertEquals(s.size(), s1.size() + s2.size() + s3.size() + s4.size() + s5.size());
    }
}
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 53 with Grammar

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

the class GenericGrammarBasedPrettyPrinterVisitor method visit.

@Override
public void visit(IEntity entity) {
    IBindingManager bm = getBindings();
    Grammar grammar = null;
    String grammarUri = GrammarsUtils.getGrammarUri(bm);
    if (grammarUri != null)
        grammar = GrammarsRegistry.instance().getGrammar(grammarUri);
    if (grammar == null) {
        Collection<Grammar> grammars = GrammarsRegistry.instance().getGrammarsFor(entity);
        if (grammars.isEmpty())
            throw new IllegalArgumentException("cannot find a suitable grammar to unparse the entity");
        grammar = grammars.iterator().next();
    }
    new GrammarBasedPrettyPrinterVisitor(entity, out, bm).visit(grammar);
}
Also used : IBindingManager(org.whole.lang.bindings.IBindingManager) Grammar(org.whole.lang.grammars.model.Grammar)

Example 54 with Grammar

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

the class GrammarsPersistenceKit method doWriteModel.

@Override
protected void doWriteModel(IEntity model, IPersistenceProvider pp) throws Exception {
    PrintStream printStream = new PrintStream(pp.getOutputStream(), false, pp.getEncoding());
    Collection<Grammar> grammars = GrammarsRegistry.instance().getGrammarsFor(model);
    GrammarsUtils.unparse(model, printStream, grammars.size() == 1 ? grammars.iterator().next().getUri().getValue() : getGrammarUri(pp));
    printStream.flush();
}
Also used : PrintStream(java.io.PrintStream) Grammar(org.whole.lang.grammars.model.Grammar)

Example 55 with Grammar

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

the class GrammarPart method getModelSpecificChildren.

protected List<IEntity> getModelSpecificChildren() {
    Grammar entity = getModelEntity();
    List<IEntity> list = new ArrayList<IEntity>(12);
    list.add(entity.getUri());
    list.add(entity.getNamespace());
    list.add(entity.getName());
    list.add(entity.getVersion());
    list.add(entity.getTargetLanguage());
    list.add(entity.getStartSymbol());
    list.add(entity.getDelimiter());
    list.add(entity.getPhraseStructure());
    list.add(entity.getLexicalStructure());
    list.add(entity.getSpaceLiteral());
    list.add(entity.getIndentLiteral());
    list.add(entity.getNewLineLiteral());
    return list;
}
Also used : IEntity(org.whole.lang.model.IEntity) ArrayList(java.util.ArrayList) Grammar(org.whole.lang.grammars.model.Grammar)

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