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