Search in sources :

Example 36 with AbstractElement

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

the class GrammarUtilTest method testFindCurrentType_03.

@Test
public void testFindCurrentType_03() throws Exception {
    this.with(XtextStandaloneSetup.class);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar myLang with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("generate g \'http://1\'");
    _builder.newLine();
    _builder.append("Rule:");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Fragment;");
    _builder.newLine();
    _builder.append("fragment Fragment: name=ID SecondFragment;");
    _builder.newLine();
    _builder.append("fragment SecondFragment: {SubRule.named=current} value=ID;");
    _builder.newLine();
    String model = _builder.toString();
    final XtextResource r = this.getResourceFromString(model);
    EObject _get = r.getContents().get(0);
    final Grammar grammar = ((Grammar) _get);
    final AbstractRule rule = IterableExtensions.<AbstractRule>head(grammar.getRules());
    final AbstractElement fragmentCall = rule.getAlternatives();
    final EClassifier currentType = GrammarUtil.findCurrentType(fragmentCall);
    Assert.assertEquals("SubRule", currentType.getName());
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) EClassifier(org.eclipse.emf.ecore.EClassifier) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) Test(org.junit.Test)

Example 37 with AbstractElement

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

the class GrammarAccessExtensions method gaTypeAccessor.

/**
 * Returns the invocation of an element or rule accessor, including the .getType() call.
 * Example1: getFooRule().getType()
 * Example2: getBarRule().getFooAction().getType()
 */
public String gaTypeAccessor(final TypeRef ele) {
    String _switchResult = null;
    EObject _eContainer = ele.eContainer();
    final EObject cnt = _eContainer;
    boolean _matched = false;
    if (cnt instanceof AbstractElement) {
        _matched = true;
        String _gaRuleElementAccessor = this.gaRuleElementAccessor(((AbstractElement) cnt));
        _switchResult = (_gaRuleElementAccessor + ".getType()");
    }
    if (!_matched) {
        if (cnt instanceof AbstractRule) {
            _matched = true;
            String _gaRuleAccessor = this.gaRuleAccessor(((AbstractRule) cnt));
            _switchResult = (_gaRuleAccessor + ".getType()");
        }
    }
    if (!_matched) {
        String _name = ele.eContainer().eClass().getName();
        String _plus = ("<error: unknown type " + _name);
        _switchResult = (_plus + ">");
    }
    return _switchResult;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 38 with AbstractElement

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

the class AntlrGrammarGenUtil method doGetPredicatedElement.

private static AbstractElement doGetPredicatedElement(final AbstractElement element) {
    if (element instanceof Alternatives || element instanceof Group) {
        EcoreUtil.Copier copier = new EcoreUtil.Copier(true, true) {

            private static final long serialVersionUID = 1L;

            @Override
            public EObject copy(EObject eObject) {
                if (eObject instanceof AbstractElement) {
                    if (((AbstractElement) eObject).isFirstSetPredicated() && isLastInGroup(eObject)) {
                        Collection<AbstractElement> firstSet = EcoreUtil.copyAll(getFirstSet((AbstractElement) eObject));
                        if (firstSet.size() == 1) {
                            AbstractElement result = firstSet.iterator().next();
                            result.setCardinality(((AbstractElement) eObject).getCardinality());
                            return result;
                        }
                        Alternatives result = XtextFactory.eINSTANCE.createAlternatives();
                        for (AbstractElement firstElement : firstSet) {
                            firstElement.setCardinality(null);
                        }
                        result.getElements().addAll(firstSet);
                        result.setCardinality(((AbstractElement) eObject).getCardinality());
                        return result;
                    }
                }
                return super.copy(eObject);
            }

            private boolean isLastInGroup(EObject eObject) {
                if (eObject == element) {
                    return true;
                }
                EObject container = eObject.eContainer();
                if (container instanceof Group) {
                    List<AbstractElement> siblings = ((Group) container).getElements();
                    if (siblings.get(siblings.size() - 1) == eObject) {
                        return isLastInGroup(container);
                    }
                }
                if (container instanceof Alternatives) {
                    return isLastInGroup(container);
                }
                return false;
            }
        };
        AbstractElement clone = (AbstractElement) copier.copy(element);
        copier.copyReferences();
        return clone;
    }
    return element;
}
Also used : Group(org.eclipse.xtext.Group) AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) Alternatives(org.eclipse.xtext.Alternatives)

Example 39 with AbstractElement

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

the class AbstractNegatedTokenImpl method basicSetTerminal.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public NotificationChain basicSetTerminal(AbstractElement newTerminal, NotificationChain msgs) {
    AbstractElement oldTerminal = terminal;
    terminal = newTerminal;
    if (eNotificationRequired()) {
        ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, XtextPackage.ABSTRACT_NEGATED_TOKEN__TERMINAL, oldTerminal, newTerminal);
        if (msgs == null)
            msgs = notification;
        else
            msgs.add(notification);
    }
    return msgs;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) ENotificationImpl(org.eclipse.emf.ecore.impl.ENotificationImpl)

Example 40 with AbstractElement

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

the class AbstractRuleImpl method basicSetAlternatives.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public NotificationChain basicSetAlternatives(AbstractElement newAlternatives, NotificationChain msgs) {
    AbstractElement oldAlternatives = alternatives;
    alternatives = newAlternatives;
    if (eNotificationRequired()) {
        ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, XtextPackage.ABSTRACT_RULE__ALTERNATIVES, oldAlternatives, newAlternatives);
        if (msgs == null)
            msgs = notification;
        else
            msgs.add(notification);
    }
    return msgs;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) ENotificationImpl(org.eclipse.emf.ecore.impl.ENotificationImpl)

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