Search in sources :

Example 31 with Production

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);
}
Also used : Production(org.whole.lang.grammars.model.Production)

Example 32 with Production

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);
}
Also used : Production(org.whole.lang.grammars.model.Production) Grammar(org.whole.lang.grammars.model.Grammar)

Example 33 with Production

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);
}
Also used : Production(org.whole.lang.grammars.model.Production) Model(org.whole.lang.models.model.Model) LanguageDescriptor(org.whole.lang.grammars.model.LanguageDescriptor) ModelsEntityFactory(org.whole.lang.models.factories.ModelsEntityFactory)

Example 34 with Production

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());
}
Also used : IEntity(org.whole.lang.model.IEntity) Production(org.whole.lang.grammars.model.Production) NonTerminal(org.whole.lang.grammars.model.NonTerminal) HashSet(java.util.HashSet)

Example 35 with Production

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());
}
Also used : PathExpression(org.whole.lang.queries.model.PathExpression) Production(org.whole.lang.grammars.model.Production) ITemplateManager(org.whole.lang.templates.ITemplateManager) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) QueriesGrammar(org.whole.lang.grammars.codebase.QueriesGrammar) QueriesGrammar(org.whole.lang.grammars.codebase.QueriesGrammar) Test(org.junit.Test)

Aggregations

Production (org.whole.lang.grammars.model.Production)55 Grammar (org.whole.lang.grammars.model.Grammar)43 Test (org.junit.Test)41 TestXmlGrammar (org.whole.lang.grammars.util.TestXmlGrammar)41 ITemplateManager (org.whole.lang.templates.ITemplateManager)26 PathExpression (org.whole.lang.queries.model.PathExpression)20 QueriesGrammar (org.whole.lang.grammars.codebase.QueriesGrammar)18 IEntity (org.whole.lang.model.IEntity)18 IBindingManager (org.whole.lang.bindings.IBindingManager)14 Productions (org.whole.lang.grammars.model.Productions)13 NonTerminal (org.whole.lang.grammars.model.NonTerminal)8 HashSet (java.util.HashSet)5 ArrayList (java.util.ArrayList)3 Rule (org.whole.lang.grammars.model.Rule)3 PrettyPrinterOperation.toPrettyPrintString (org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString)3 ModelsEntityFactory (org.whole.lang.models.factories.ModelsEntityFactory)2 ILanguageKit (org.whole.lang.reflect.ILanguageKit)2 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Category (org.junit.experimental.categories.Category)1