Search in sources :

Example 46 with AbstractElement

use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.

the class SyntacticSequencerPDAProvider method getType.

protected SynStateType getType(ISerState state) {
    switch(state.getType()) {
        case ELEMENT:
            AbstractElement ele = state.getGrammarElement();
            Assignment ass;
            if (ele instanceof Action) {
                if (((Action) ele).getFeature() == null)
                    return SynStateType.UNASSIGEND_ACTION_CALL;
                else
                    return SynStateType.ASSIGNED_ACTION_CALL;
            } else if (GrammarUtil.containingCrossReference(ele) != null) {
                if (ele instanceof RuleCall) {
                    RuleCall rc = (RuleCall) ele;
                    if (rc.getRule() instanceof ParserRule)
                        return SynStateType.ASSIGNED_CROSSREF_DATATYPE_RULE_CALL;
                    if (rc.getRule() instanceof TerminalRule)
                        return SynStateType.ASSIGNED_CROSSREF_TERMINAL_RULE_CALL;
                    if (rc.getRule() instanceof EnumRule)
                        return SynStateType.ASSIGNED_CROSSREF_ENUM_RULE_CALL;
                } else if (ele instanceof Keyword)
                    return SynStateType.ASSIGNED_CROSSREF_KEYWORD;
            } else if ((ass = GrammarUtil.containingAssignment(ele)) != null) {
                if (ele instanceof RuleCall) {
                    RuleCall rc = (RuleCall) ele;
                    if (rc.getRule() instanceof ParserRule) {
                        if (rc.getRule().getType().getClassifier() instanceof EClass)
                            return SynStateType.ASSIGNED_PARSER_RULE_CALL;
                        return SynStateType.ASSIGNED_DATATYPE_RULE_CALL;
                    }
                    if (rc.getRule() instanceof TerminalRule)
                        return SynStateType.ASSIGNED_TERMINAL_RULE_CALL;
                    if (rc.getRule() instanceof EnumRule)
                        return SynStateType.ASSIGNED_ENUM_RULE_CALL;
                } else if (ele instanceof Keyword) {
                    if (GrammarUtil.isBooleanAssignment(ass))
                        return SynStateType.ASSIGNED_BOOLEAN_KEYWORD;
                    else
                        return SynStateType.ASSIGNED_KEYWORD;
                }
            } else {
                if (ele instanceof RuleCall) {
                    RuleCall rc = (RuleCall) ele;
                    if (rc.getRule() instanceof ParserRule)
                        return SynStateType.UNASSIGNED_DATATYPE_RULE_CALL;
                    if (rc.getRule() instanceof TerminalRule)
                        return SynStateType.UNASSIGNED_TERMINAL_RULE_CALL;
                } else if (ele instanceof Keyword)
                    return SynStateType.UNASSIGEND_KEYWORD;
            }
            break;
        case PUSH:
            return SynStateType.UNASSIGNED_PARSER_RULE_ENTER;
        case POP:
            return SynStateType.UNASSIGNED_PARSER_RULE_EXIT;
        case START:
            return SynStateType.START;
        case STOP:
            return SynStateType.STOP;
    }
    throw new RuntimeException("no type found for " + state);
}
Also used : Assignment(org.eclipse.xtext.Assignment) ParserRule(org.eclipse.xtext.ParserRule) EnumRule(org.eclipse.xtext.EnumRule) Action(org.eclipse.xtext.Action) EClass(org.eclipse.emf.ecore.EClass) AbstractElement(org.eclipse.xtext.AbstractElement) Keyword(org.eclipse.xtext.Keyword) TerminalRule(org.eclipse.xtext.TerminalRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 47 with AbstractElement

use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.

the class AbstractSyntacticSequencer method getNodesFor.

protected List<INode> getNodesFor(List<INode> nodes, AbstractElementAlias ele) {
    if (nodes == null)
        return null;
    Set<AbstractElement> elments = Sets.newHashSet();
    collectAbstractElements(ele, elments);
    List<INode> result = Lists.newArrayList();
    for (INode n : nodes) if (elments.contains(n.getGrammarElement()))
        result.add(n);
    return result;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) AbstractElement(org.eclipse.xtext.AbstractElement)

Example 48 with AbstractElement

use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.

the class AssignmentFinder method findValidAssignmentsForContainmentRef.

protected Set<AbstractElement> findValidAssignmentsForContainmentRef(EObject semanticObj, Multimap<AbstractElement, ISerializationContext> assignments, EObject value) {
    Multimap<ISerializationContext, AbstractElement> children = ArrayListMultimap.create();
    for (Entry<AbstractElement, Collection<ISerializationContext>> e : assignments.asMap().entrySet()) {
        AbstractElement ele = e.getKey();
        if (ele instanceof RuleCall) {
            EClassifier classifier = ((RuleCall) ele).getRule().getType().getClassifier();
            if (!classifier.isInstance(value))
                continue;
        }
        for (ISerializationContext container : e.getValue()) {
            ISerializationContext child = SerializationContext.forChild(container, ele, value);
            children.put(child, ele);
        }
    }
    if (children.size() < 2)
        return Sets.newHashSet(children.values());
    Set<ISerializationContext> found = contextFinder.findByContents(value, children.keySet());
    Set<AbstractElement> result = Sets.newLinkedHashSet();
    for (ISerializationContext ctx : children.keySet()) if (found.contains(ctx))
        result.addAll(children.get(ctx));
    return result;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) Collection(java.util.Collection) EClassifier(org.eclipse.emf.ecore.EClassifier) RuleCall(org.eclipse.xtext.RuleCall)

Example 49 with AbstractElement

use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.

the class AssignmentFinder method findValidAssignmentsForCrossRef.

protected Set<AbstractElement> findValidAssignmentsForCrossRef(EObject semanticObj, Iterable<AbstractElement> assignedElements, EObject value, INode node) {
    Set<AbstractElement> result = Sets.newLinkedHashSet();
    for (AbstractElement ass : assignedElements) {
        CrossReference crossref = GrammarUtil.containingCrossReference(ass);
        EReference eref = GrammarUtil.getReference(crossref, semanticObj.eClass());
        if (EcoreUtil2.isAssignableFrom(eref.getEReferenceType(), value.eClass()) && crossRefSerializer.isValid(semanticObj, crossref, value, node, null))
            result.add(ass);
    }
    return result;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) CrossReference(org.eclipse.xtext.CrossReference) EReference(org.eclipse.emf.ecore.EReference)

Example 50 with AbstractElement

use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.

the class BacktrackingSemanticSequencer method createSequence.

@Override
public void createSequence(ISerializationContext context, EObject obj) {
    INodesForEObjectProvider nodes = nodeProvider.getNodesForSemanticObject(obj, null);
    SerializationContextMap<IConstraint> constraints = constraintProvider.getConstraints(grammar.getGrammar());
    IConstraint constraint = constraints.get(context);
    if (constraint == null)
        throw new IllegalStateException("Invalid context: " + context);
    Nfa<ISemState> nfa = constraint.getNfa();
    final SerializableObject object = new SerializableObject(context, obj, nodes);
    TraceItem co = new TraceItem(object);
    List<TraceItem> trace = new NfaUtil().backtrack(nfa, co, new NfaUtil.BacktrackHandler<ISemState, TraceItem>() {

        @Override
        public TraceItem handle(ISemState state, TraceItem previous) {
            if (!previous.canEnter(state))
                return null;
            if (state.getFeature() != null) {
                return previous.cloneAndConsume(state);
            } else
                return previous.clone(state);
        }

        @Override
        public boolean isSolution(TraceItem result) {
            return result.isConsumed();
        }

        @Override
        public Iterable<ISemState> sortFollowers(TraceItem result, Iterable<ISemState> followers) {
            AbstractElement next = result.getNextGrammarElement();
            List<ISemState> r = Lists.newArrayList(followers);
            Collections.sort(r, createFollowerSorter(object, next));
            return r;
        }
    });
    SequenceFeeder feeder = feederProvider.create(context, obj, nodes, masterSequencer, sequenceAcceptor, errorAcceptor);
    if (trace != null) {
        for (TraceItem ti : trace) if (ti.getState() != null && ti.getState().getFeature() != null)
            accept(ti, feeder);
    } else if (errorAcceptor != null)
        errorAcceptor.accept(diagnosticProvider.createBacktrackingFailedDiagnostic(object, context, constraint));
    feeder.finish();
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) IConstraint(org.eclipse.xtext.serializer.analysis.IGrammarConstraintProvider.IConstraint) SequenceFeeder(org.eclipse.xtext.serializer.acceptor.SequenceFeeder) INodesForEObjectProvider(org.eclipse.xtext.serializer.sequencer.ISemanticNodeProvider.INodesForEObjectProvider) NfaUtil(org.eclipse.xtext.util.formallang.NfaUtil) InternalEList(org.eclipse.emf.ecore.util.InternalEList) List(java.util.List) ISemState(org.eclipse.xtext.serializer.analysis.ISemanticSequencerNfaProvider.ISemState)

Aggregations

AbstractElement (org.eclipse.xtext.AbstractElement)126 EObject (org.eclipse.emf.ecore.EObject)39 AbstractRule (org.eclipse.xtext.AbstractRule)34 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)31 RuleCall (org.eclipse.xtext.RuleCall)26 ParserRule (org.eclipse.xtext.ParserRule)18 Grammar (org.eclipse.xtext.Grammar)17 EList (org.eclipse.emf.common.util.EList)15 Assignment (org.eclipse.xtext.Assignment)15 Test (org.junit.Test)14 Group (org.eclipse.xtext.Group)13 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)13 List (java.util.List)12 ArrayList (java.util.ArrayList)11 XtextResource (org.eclipse.xtext.resource.XtextResource)11 Action (org.eclipse.xtext.Action)10 TerminalRule (org.eclipse.xtext.TerminalRule)10 Alternatives (org.eclipse.xtext.Alternatives)9 EClassifier (org.eclipse.emf.ecore.EClassifier)8 InternalEObject (org.eclipse.emf.ecore.InternalEObject)8