use of org.whole.lang.grammars.model.Production in project whole by wholeplatform.
the class ParserTemplateFactory method getProduction.
protected Production getProduction(NonTerminal nt) {
// TODO cache in a map
IEntityIterator<Production> pi = IteratorFactory.<Production>childIterator();
pi.reset(grammar.getPhraseStructure());
for (Production p : pi) if (p.getName().wEquals(nt))
return p;
IEntityIterator<Production> li = IteratorFactory.<Production>childIterator();
li.reset(grammar.getLexicalStructure());
for (Production p : li) if (p.getName().wEquals(nt))
return p;
throw new IllegalArgumentException("Production not found: " + nt);
}
use of org.whole.lang.grammars.model.Production in project whole by wholeplatform.
the class GrammarBasedUnparserVisitor method normalize.
protected void normalize(Grammar entity) {
// FIXME ensure normalized: NormalizerOperation.normalize(entity);
Grammar g = entity;
ScannerIterator<Production> li = IteratorFactory.<Production>childScannerIterator();
li.reset(g.getLexicalStructure());
for (Production p : li) {
String name = p.getName().getValue();
nameProductionMap.put(name, p);
lexiconSet.add(name);
}
ScannerIterator<Production> pi = IteratorFactory.<Production>childScannerIterator();
pi.reset(g.getPhraseStructure());
for (Production p : pi) nameProductionMap.put(p.getName().getValue(), p);
}
use of org.whole.lang.grammars.model.Production in project whole by wholeplatform.
the class Grammars2ModelsVisitor method visit.
@Override
public void visit(Grammar entity) {
entity = normalize(entity);
IEntityIterator<Production> lexiconIiterator = IteratorFactory.<Production>childIterator();
lexiconIiterator.reset(entity.getLexicalStructure());
for (Production p : lexiconIiterator) lexiconMap.put(p.getName().getValue(), p);
LanguageDescriptor ld = (LanguageDescriptor) entity.getTargetLanguage();
ModelsEntityFactory mf = ModelsEntityFactory.instance;
Model model = mf.createModel(mf.createSimpleName(ld.getName().getValue()), mf.createTypeRelations(), modelDeclarations = mf.createModelDeclarations(), mf.createNamespace(ld.getNamespace().getValue()), EntityUtils.isResolver(ld.getVersion()) ? CommonsEntityAdapterFactory.createResolver(ModelsEntityDescriptorEnum.Version) : mf.createVersion(ld.getVersion().getValue()), mf.createURI(ld.getUri().getValue()));
entity.getPhraseStructure().accept(this);
setResult(model);
}
use of org.whole.lang.grammars.model.Production in project whole by wholeplatform.
the class GrammarsValidatorVisitor method defUseAnalisys.
public void defUseAnalisys(Grammar entity) {
Set<String> ntDefs = new HashSet<String>();
Set<String> ntUses = new HashSet<String>();
Set<NonTerminal> nts = new HashSet<NonTerminal>();
AbstractPatternFilterIterator<NonTerminal> i = IteratorFactory.<NonTerminal>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.NonTerminal);
i.reset(entity);
for (NonTerminal nt : i) {
IEntity parent = nt.wGetParent();
if (Matcher.match(GrammarsEntityDescriptorEnum.Production, parent) && parent.wGet(GrammarsFeatureDescriptorEnum.name) == nt)
ntDefs.add(nt.getValue());
else {
ntUses.add(nt.getValue());
nts.add(nt);
}
}
Set<String> ntNotUsed = new HashSet<String>(ntDefs);
ntNotUsed.removeAll(ntUses);
Set<String> ntNotDef = new HashSet<String>(ntUses);
ntNotDef.removeAll(ntDefs);
for (NonTerminal nt : nts) if (!ntDefs.contains(nt.getValue()))
// TODO location with production
getDecorationManager().addError(nt, "Production not defined", nt.getValue());
AbstractPatternFilterIterator<Production> i2 = IteratorFactory.<Production>childMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.Production);
i2.reset(entity.getPhraseStructure());
for (Production p : i2) if (!ntUses.contains(p.getName().wGetValue()))
getDecorationManager().addInfo(p, "Production not used", p.getName().getValue());
}
use of org.whole.lang.grammars.model.Production in project whole by wholeplatform.
the class PathExpressionsQueriesTest method testVariableJoinTest4.
@Test
public void testVariableJoinTest4() {
ITemplateManager tm = PathExpressionsQueriesTemplateManager.instance();
Grammar g = new QueriesGrammar().create();
PathExpression pe1 = (PathExpression) tm.create("recursiveProduction4");
StringBuilder names = new StringBuilder();
for (Production p : BehaviorUtils.<Production>compileAndLazyEvaluate(pe1, g)) names.append(p.getName().getValue());
Assert.assertEquals("ExpressionPathExpressionStepExpressionPredicate", names.toString());
}
Aggregations