Search in sources :

Example 46 with Keyword

use of org.eclipse.xtext.Keyword in project xtext-eclipse by eclipse.

the class ParameterContextInformationProvider method getParameterListOffset.

protected int getParameterListOffset(XExpression containerCall) {
    List<XExpression> arguments = getArguments(containerCall);
    if (arguments.isEmpty()) {
        Keyword parameterListOpenParenthesis = getParameterListOpenParenthesis(containerCall);
        for (ILeafNode leafNode : NodeModelUtils.findActualNodeFor(containerCall).getLeafNodes()) {
            if (leafNode.getGrammarElement() == parameterListOpenParenthesis) {
                return leafNode.getEndOffset();
            }
        }
        int offset = 0;
        for (INode node : NodeModelUtils.findNodesForFeature(containerCall, getCalledFeatureReference(containerCall))) offset = Math.max(offset, node.getEndOffset());
        return offset;
    } else {
        INode node = NodeModelUtils.findActualNodeFor(arguments.get(0));
        return node.getOffset();
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) Keyword(org.eclipse.xtext.Keyword) XExpression(org.eclipse.xtext.xbase.XExpression)

Example 47 with Keyword

use of org.eclipse.xtext.Keyword in project xtext-eclipse by eclipse.

the class XtextLocationInFileProviderTest method testKeywordLocation.

@Test
public void testKeywordLocation() {
    Keyword keyword = (Keyword) grammar.getRules().get(0).getAlternatives();
    ITextRegion region = locationInFileProvider.getFullTextRegion(keyword);
    String fullRegion = grammarText.substring(region.getOffset(), region.getOffset() + region.getLength());
    assertEquals("'keyword'*", fullRegion);
    region = locationInFileProvider.getSignificantTextRegion(keyword);
    String significantRegion = grammarText.substring(region.getOffset(), region.getOffset() + region.getLength());
    assertEquals("'keyword'*", significantRegion);
}
Also used : Keyword(org.eclipse.xtext.Keyword) ITextRegion(org.eclipse.xtext.util.ITextRegion) Test(org.junit.Test)

Example 48 with Keyword

use of org.eclipse.xtext.Keyword in project xtext-eclipse by eclipse.

the class GrammarAccessExtensions method toStringLiteral.

public CharSequence toStringLiteral(final AbstractElement it) {
    CharSequence _switchResult = null;
    boolean _matched = false;
    if (it instanceof RuleCall) {
        AbstractRule _rule = ((RuleCall) it).getRule();
        boolean _tripleNotEquals = (_rule != null);
        if (_tripleNotEquals) {
            _matched = true;
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("\"");
            String _name = ((RuleCall) it).getRule().getName();
            _builder.append(_name);
            _builder.append("\"");
            _switchResult = _builder;
        }
    }
    if (!_matched) {
        if (it instanceof Keyword) {
            _matched = true;
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("\"");
            String _stringInAntlrAction = AntlrGrammarGenUtil.toStringInAntlrAction(((Keyword) it).getValue());
            _builder.append(_stringInAntlrAction);
            _builder.append("\"");
            _switchResult = _builder;
        }
    }
    if (!_matched) {
        _switchResult = "null";
    }
    return _switchResult;
}
Also used : Keyword(org.eclipse.xtext.Keyword) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 49 with Keyword

use of org.eclipse.xtext.Keyword in project n4js by eclipse.

the class ContentAssistTokenTypeMapper method getInternalTokenType.

/**
 * Converts a grammar element to an Antlr token type (int).
 */
public int getInternalTokenType(EObject grammarElement) {
    Integer type = grammarElements.get(grammarElement);
    if (type == null) {
        if (grammarElement instanceof Keyword) {
            String keyword = ((Keyword) grammarElement).getValue();
            type = tokenTypes.get("'" + keyword + "'");
        } else if (grammarElement instanceof RuleCall) {
            AbstractRule rule = ((RuleCall) grammarElement).getRule();
            type = tokenTypes.get("RULE_" + rule.getName().toUpperCase());
        } else if (grammarElement instanceof AbstractRule) {
            AbstractRule rule = (AbstractRule) grammarElement;
            type = tokenTypes.get("RULE_" + rule.getName().toUpperCase());
        } else if (grammarElement instanceof EnumLiteralDeclaration) {
            String keyword = ((EnumLiteralDeclaration) grammarElement).getLiteral().getValue();
            type = tokenTypes.get("'" + keyword + "'");
        } else if (grammarElement instanceof CrossReference) {
            type = getInternalTokenType(((CrossReference) grammarElement).getTerminal());
        } else {
            throw new IllegalArgumentException(String.valueOf(grammarElement));
        }
        grammarElements.put(grammarElement, type);
    }
    return type;
}
Also used : Keyword(org.eclipse.xtext.Keyword) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) CrossReference(org.eclipse.xtext.CrossReference) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 50 with Keyword

use of org.eclipse.xtext.Keyword in project n4js by eclipse.

the class TokenTypeRewriter method rewriteKeywords.

private static void rewriteKeywords(AbstractRule rule, N4JSKeywords keywords, ImmutableMap.Builder<AbstractElement, Integer> builder) {
    for (EObject obj : EcoreUtil2.eAllContents(rule.getAlternatives())) {
        if (obj instanceof Keyword) {
            Keyword keyword = (Keyword) obj;
            Integer type = keywords.getTokenType(keyword);
            if (type != null) {
                if (keyword.eContainer() instanceof EnumLiteralDeclaration) {
                    builder.put((AbstractElement) keyword.eContainer(), type);
                } else {
                    builder.put(keyword, type);
                }
            }
        }
    }
}
Also used : Keyword(org.eclipse.xtext.Keyword) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) EObject(org.eclipse.emf.ecore.EObject)

Aggregations

Keyword (org.eclipse.xtext.Keyword)70 RuleCall (org.eclipse.xtext.RuleCall)17 EObject (org.eclipse.emf.ecore.EObject)16 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)14 INode (org.eclipse.xtext.nodemodel.INode)13 AbstractElement (org.eclipse.xtext.AbstractElement)10 AbstractRule (org.eclipse.xtext.AbstractRule)10 CrossReference (org.eclipse.xtext.CrossReference)9 Grammar (org.eclipse.xtext.Grammar)9 ParserRule (org.eclipse.xtext.ParserRule)9 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)9 Test (org.junit.Test)9 Action (org.eclipse.xtext.Action)8 Assignment (org.eclipse.xtext.Assignment)8 Alternatives (org.eclipse.xtext.Alternatives)7 EnumLiteralDeclaration (org.eclipse.xtext.EnumLiteralDeclaration)7 EnumRule (org.eclipse.xtext.EnumRule)7 TerminalRule (org.eclipse.xtext.TerminalRule)6 Group (org.eclipse.xtext.Group)5 ArrayList (java.util.ArrayList)4