Search in sources :

Example 1 with NonTerminal

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

the class ParserTemplateFactory method propagateActivation.

protected void propagateActivation(Set<ActiveRule> terminalSet, Set<NonTerminal> recursiveSet, ListIterator<ActiveRule> activeRules, ActiveRule ar) {
    Rule dotRule = ar.getDotRule();
    switch(dotRule.wGetEntityOrd()) {
        case GrammarsEntityDescriptorEnum.DataTerminal_ord:
        case GrammarsEntityDescriptorEnum.LiteralTerminal_ord:
            terminalSet.add(ar);
            break;
        case GrammarsEntityDescriptorEnum.NonTerminal_ord:
            NonTerminal nt = (NonTerminal) dotRule;
            if (recursiveSet.add(nt)) {
                activeRules.add(ar = createActiveRule(nt));
                propagateActivation(terminalSet, recursiveSet, activeRules, ar);
            } else
                ar.setRecursive(true);
            break;
        case GrammarsEntityDescriptorEnum.Concatenate_ord:
            Concatenate c = (Concatenate) dotRule;
            if (c.wSize() > 0) {
                ar.setDotRule((Rule) c.wGet(0));
                propagateActivation(terminalSet, recursiveSet, activeRules, ar);
            }
            break;
        case GrammarsEntityDescriptorEnum.Optional_ord:
            Optional o = (Optional) dotRule;
            ar.setDotRule(o.getRule());
            propagateActivation(terminalSet, recursiveSet, activeRules, ar);
            // TODO add also the rule after the optional
            break;
        case GrammarsEntityDescriptorEnum.Repeat_ord:
            Repeat r = (Repeat) dotRule;
            // TODO check lower bound and split active state for repeat continuation if 0
            ar.setDotRule(r.getRule());
            propagateActivation(terminalSet, recursiveSet, activeRules, ar);
            break;
        case GrammarsEntityDescriptorEnum.As_ord:
            As as = (As) dotRule;
            ar.setDotRule(as.getRule());
            propagateActivation(terminalSet, recursiveSet, activeRules, ar);
            break;
    }
}
Also used : As(org.whole.lang.grammars.model.As) Optional(org.whole.lang.grammars.model.Optional) Concatenate(org.whole.lang.grammars.model.Concatenate) NonTerminal(org.whole.lang.grammars.model.NonTerminal) Repeat(org.whole.lang.grammars.model.Repeat) Rule(org.whole.lang.grammars.model.Rule)

Example 2 with NonTerminal

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

the class GenericGrammarBasedValidatorVisitor method calculateDataTerminals.

private void calculateDataTerminals(Grammar grammar) {
    this.dataTerminals = new HashMap<EntityDescriptor<?>, DataTerminal>();
    // TODO ensure grammar normalized
    Map<String, Rule> productions = new HashMap<String, Rule>();
    IEntityIterator<Production> pi = IteratorFactory.<Production>childIterator();
    pi.reset(grammar.getPhraseStructure());
    for (Production p : pi) productions.put(p.getName().getValue(), p.getRule());
    Map<String, Rule> lexicon = new HashMap<String, Rule>();
    IEntityIterator<Production> li = IteratorFactory.<Production>childIterator();
    li.reset(grammar.getLexicalStructure());
    for (Production p : li) lexicon.put(p.getName().getValue(), p.getRule());
    ILanguageKit languageKit = ReflectionFactory.getLanguageKit(GrammarsUtils.getLanguageURI(grammar), false, null);
    EntityDescriptorEnum edEnum = languageKit.getEntityDescriptorEnum();
    for (EntityDescriptor<?> ed : edEnum) if (EntityUtils.isData(ed)) {
        Rule production = productions.get(ed.getName());
        if (!ed.getDataKind().isEnumValue()) {
            DataTerminal dataTerminal = Matcher.find(GrammarsEntityDescriptorEnum.DataTerminal, production, false);
            if (dataTerminal == null) {
                NonTerminal nonTerminal = Matcher.find(GrammarsEntityDescriptorEnum.NonTerminal, production, false);
                dataTerminal = Matcher.find(GrammarsEntityDescriptorEnum.DataTerminal, lexicon.get(nonTerminal.getValue()), false);
            }
            dataTerminals.put(ed, EntityUtils.clone(dataTerminal));
        }
    }
}
Also used : EntityDescriptorEnum(org.whole.lang.reflect.EntityDescriptorEnum) GrammarsEntityDescriptorEnum(org.whole.lang.grammars.reflect.GrammarsEntityDescriptorEnum) HashMap(java.util.HashMap) NonTerminal(org.whole.lang.grammars.model.NonTerminal) ILanguageKit(org.whole.lang.reflect.ILanguageKit) EntityDescriptor(org.whole.lang.reflect.EntityDescriptor) Production(org.whole.lang.grammars.model.Production) DataTerminal(org.whole.lang.grammars.model.DataTerminal) Rule(org.whole.lang.grammars.model.Rule)

Example 3 with NonTerminal

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

the class GrammarBasedUnparserVisitor method visit.

@Override
public void visit(Choose entity) {
    if (!appendModelEntityAsFragment()) {
        if (Matcher.match(GrammarsEntityDescriptorEnum.As, entity.wGet(0)))
            append(getAsString(model));
        else {
            // get rule using model entity
            EntityDescriptorEnum edEnum = model.wGetLanguageKit().getEntityDescriptorEnum();
            Rule rule = null;
            for (int size = entity.wSize(), i = 0; rule == null && i < size; i++) {
                Rule child = (Rule) entity.wGet(i);
                NonTerminal nt = (NonTerminal) Matcher.find(phraseNonTerminal, child, false);
                if (// TODO test was isAssignableFrom
                Matcher.isAssignableAsIsFrom(edEnum.valueOf(nt.getValue()), model))
                    rule = child;
            }
            if (rule != null)
                rule.accept(this);
            else
                throw new IllegalStateException("missing choose rule for model entity");
        }
    }
}
Also used : EntityDescriptorEnum(org.whole.lang.reflect.EntityDescriptorEnum) GrammarsEntityDescriptorEnum(org.whole.lang.grammars.reflect.GrammarsEntityDescriptorEnum) NonTerminal(org.whole.lang.grammars.model.NonTerminal) Rule(org.whole.lang.grammars.model.Rule)

Example 4 with NonTerminal

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

the class GrammarsContentAssistVisitor method calculateNonTerminalValues.

public static Set<String> calculateNonTerminalValues(IEntity e) {
    Grammar grammar = Matcher.findAncestor(Grammar, e);
    if (grammar != null) {
        Set<String> names = new HashSet<String>();
        IEntity parent = e.wGetParent();
        ILanguageKit languageKit = getLanguageKitIfExists(grammar);
        if (languageKit != null) {
            if (!grammar.getLexicalStructure().equals(parent.wGetParent()))
                names.addAll(languageKit.getEntityDescriptorEnum().names());
            if (!parent.wGetFeatureDescriptor(e).equals(name)) {
                QueriesEntityFactory qef = QueriesEntityFactory.instance;
                Path allTokenNames = qef.createPath(qef.createSequence(qef.createFeatureStep("lexicalStructure"), qef.createFeatureStep("phraseStructure")), qef.createChildStep(), qef.createFilter(qef.createFeatureStep("name"), qef.createKindTest(KindTestEnum.IMPL)));
                for (NonTerminal nt : BehaviorUtils.<NonTerminal>compileAndLazyEvaluate(allTokenNames, grammar)) names.add(nt.getValue());
            }
        } else {
            AbstractPatternFilterIterator<NonTerminal> i = IteratorFactory.<NonTerminal>descendantOrSelfMatcherIterator().withPattern(NonTerminal);
            i.reset(grammar);
            for (NonTerminal nt : i) if (EntityUtils.isNotResolver(nt))
                names.add(nt.getValue());
        }
        return names;
    }
    return Collections.emptySet();
}
Also used : Path(org.whole.lang.queries.model.Path) IEntity(org.whole.lang.model.IEntity) QueriesEntityFactory(org.whole.lang.queries.factories.QueriesEntityFactory) NonTerminal(org.whole.lang.grammars.reflect.GrammarsEntityDescriptorEnum.NonTerminal) NonTerminal(org.whole.lang.grammars.model.NonTerminal) Grammar(org.whole.lang.grammars.reflect.GrammarsEntityDescriptorEnum.Grammar) Grammar(org.whole.lang.grammars.model.Grammar) ILanguageKit(org.whole.lang.reflect.ILanguageKit) HashSet(java.util.HashSet)

Example 5 with NonTerminal

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

the class PathExpressionsQueriesTest method testDistinct.

@Test
public void testDistinct() {
    ITemplateManager tm = PathExpressionsQueriesTemplateManager.instance();
    Grammar g = new TestXmlGrammar().create();
    PathExpression pe1 = (PathExpression) tm.create("nonTerminalSet");
    Set<String> set = new HashSet<String>();
    for (NonTerminal nt : BehaviorUtils.<NonTerminal>compileAndLazyEvaluate(pe1, g)) if (!set.add(nt.getValue()))
        Assert.fail();
    Assert.assertEquals(22, set.size());
}
Also used : PathExpression(org.whole.lang.queries.model.PathExpression) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) ITemplateManager(org.whole.lang.templates.ITemplateManager) NonTerminal(org.whole.lang.grammars.model.NonTerminal) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) QueriesGrammar(org.whole.lang.grammars.codebase.QueriesGrammar) PrettyPrinterOperation.toPrettyPrintString(org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

NonTerminal (org.whole.lang.grammars.model.NonTerminal)19 Grammar (org.whole.lang.grammars.model.Grammar)14 Test (org.junit.Test)13 TestXmlGrammar (org.whole.lang.grammars.util.TestXmlGrammar)13 ITemplateManager (org.whole.lang.templates.ITemplateManager)9 Production (org.whole.lang.grammars.model.Production)8 PathExpression (org.whole.lang.queries.model.PathExpression)8 HashSet (java.util.HashSet)5 QueriesGrammar (org.whole.lang.grammars.codebase.QueriesGrammar)5 Rule (org.whole.lang.grammars.model.Rule)5 IEntity (org.whole.lang.model.IEntity)5 IBindingManager (org.whole.lang.bindings.IBindingManager)3 As (org.whole.lang.grammars.model.As)2 Productions (org.whole.lang.grammars.model.Productions)2 Repeat (org.whole.lang.grammars.model.Repeat)2 GrammarsEntityDescriptorEnum (org.whole.lang.grammars.reflect.GrammarsEntityDescriptorEnum)2 PrettyPrinterOperation.toPrettyPrintString (org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString)2 EntityDescriptorEnum (org.whole.lang.reflect.EntityDescriptorEnum)2 ILanguageKit (org.whole.lang.reflect.ILanguageKit)2 ArrayList (java.util.ArrayList)1