Search in sources :

Example 1 with TerminalRule

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

the class AbstractInternalContentAssistParser method createLookAheadTerminal.

@Override
public LookAheadTerminal createLookAheadTerminal(Token token) {
    Grammar grammar = getGrammar();
    String tokenName = getTokenNames()[token.getType()];
    tokenName = getValueForTokenName(tokenName);
    if (tokenName.charAt(0) == '\'') {
        LookaheadKeyword result = new LookaheadKeyword();
        result.setKeyword(tokenName.substring(1, tokenName.length() - 1));
        result.setToken(token);
        return result;
    }
    LookAheadTerminalRuleCall result = new LookAheadTerminalRuleCall();
    result.setToken(token);
    String ruleName = TokenTool.getLexerRuleName(tokenName);
    if (terminalRules == null)
        terminalRules = GrammarUtil.allTerminalRules(grammar);
    for (TerminalRule rule : terminalRules) {
        if (rule.getName().equalsIgnoreCase(ruleName)) {
            result.setRule(rule);
            return result;
        }
    }
    throw new IllegalArgumentException("tokenType " + token.getType() + " seems to be invalid.");
}
Also used : LookaheadKeyword(org.eclipse.xtext.ide.editor.contentassist.antlr.LookaheadKeyword) LookAheadTerminalRuleCall(org.eclipse.xtext.ide.editor.contentassist.antlr.LookAheadTerminalRuleCall) Grammar(org.eclipse.xtext.Grammar) TerminalRule(org.eclipse.xtext.TerminalRule)

Example 2 with TerminalRule

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

the class KeywordInspector method inspectKeywordHidesTerminalRule.

public void inspectKeywordHidesTerminalRule(Keyword keyword) {
    AbstractRule container = GrammarUtil.containingRule(keyword);
    if (container instanceof TerminalRule)
        return;
    Grammar grammar = GrammarUtil.getGrammar(container);
    List<TerminalRule> rules = GrammarUtil.allTerminalRules(grammar);
    for (TerminalRule rule : rules) {
        if (!rule.isFragment()) {
            AbstractElement element = rule.getAlternatives();
            if (element instanceof Keyword && Strings.isEmpty(element.getCardinality())) {
                String value = ((Keyword) element).getValue();
                if (value.equals(keyword.getValue()))
                    acceptor.acceptError("The keyword '" + value + "' hides the terminal rule " + rule.getName() + ".", keyword, XtextPackage.Literals.KEYWORD__VALUE, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, null);
            }
        }
    }
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) Keyword(org.eclipse.xtext.Keyword) Grammar(org.eclipse.xtext.Grammar) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 3 with TerminalRule

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

the class OverriddenValueInspector method caseRuleCall.

@Override
public Boolean caseRuleCall(RuleCall object) {
    AbstractRule calledRule = object.getRule();
    if (calledRule == null || calledRule.eIsProxy() || calledRule instanceof TerminalRule || calledRule instanceof EnumRule)
        return Boolean.FALSE;
    ParserRule parserRule = (ParserRule) calledRule;
    if (GrammarUtil.isDatatypeRule(parserRule))
        return Boolean.FALSE;
    if (parserRule.isFragment()) {
        visitFragment(object);
        if (GrammarUtil.isMultipleCardinality(object))
            visitFragment(object);
    }
    if (!addVisited(parserRule))
        return Boolean.FALSE;
    Multimap<String, AbstractElement> prevAssignedFeatures = assignedFeatures;
    assignedFeatures = newMultimap();
    doSwitch(parserRule.getAlternatives());
    for (String feature : assignedFeatures.keySet()) prevAssignedFeatures.put(feature, object);
    assignedFeatures = prevAssignedFeatures;
    removeVisited(parserRule);
    return Boolean.FALSE;
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) ParserRule(org.eclipse.xtext.ParserRule) AbstractElement(org.eclipse.xtext.AbstractElement) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 4 with TerminalRule

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

the class XtextTransientValueService2 method isValueTransient.

@Override
public ValueTransient isValueTransient(EObject owner, EStructuralFeature feature) {
    if (feature == XtextPackage.eINSTANCE.getTypeRef_Metamodel()) {
        final TypeRef typeRef = (TypeRef) owner;
        final AbstractMetamodelDeclaration m = typeRef.getMetamodel();
        if (m == null || Strings.isEmpty(m.getAlias()))
            return YES;
    } else if (feature == XtextPackage.eINSTANCE.getAbstractRule_Type()) {
        final AbstractRule rule = (AbstractRule) owner;
        if (!GrammarUtil.isDatatypeRule(rule) && (rule instanceof ParserRule || rule instanceof EnumRule)) {
            final TypeRef returnType = rule.getType();
            if (returnType == null || returnType.getClassifier() == null)
                return YES;
            else if (rule.getName().equals(returnType.getClassifier().getName())) {
                if (isValueTransient(returnType, XtextPackage.eINSTANCE.getTypeRef_Metamodel()) == YES)
                    return PREFERABLY;
                else
                    return NO;
            } else if (GrammarUtil.isDatatypeRule(rule)) {
                return NodeModelUtils.getNode(returnType) == null ? YES : NO;
            }
        } else if (rule instanceof TerminalRule || GrammarUtil.isDatatypeRule(rule)) {
            final TypeRef returnType = rule.getType();
            if (returnType == null)
                return YES;
            if (rule instanceof TerminalRule) {
                if (((TerminalRule) rule).isFragment()) {
                    return YES;
                }
            }
            if (GrammarUtil.findEString(GrammarUtil.getGrammar(owner)).equals(rule.getType().getClassifier()))
                return PREFERABLY;
            return NO;
        }
    } else if (feature == XtextPackage.eINSTANCE.getCrossReference_Terminal()) {
        final CrossReference ref = (CrossReference) owner;
        if (ref.getTerminal() instanceof RuleCall && ((RuleCall) ref.getTerminal()).getRule() != null) {
            if ("ID".equals(((RuleCall) ref.getTerminal()).getRule().getName()))
                return PREFERABLY;
            else
                return NO;
        }
        return YES;
    } else if (feature == XtextPackage.eINSTANCE.getEnumLiteralDeclaration_Literal()) {
        final EnumLiteralDeclaration decl = (EnumLiteralDeclaration) owner;
        if (decl.getEnumLiteral() != null && decl.getLiteral() != null) {
            if (Strings.equal(decl.getLiteral().getValue(), decl.getEnumLiteral().getName()))
                return PREFERABLY;
            return NO;
        }
        return YES;
    } else if (feature == XtextPackage.eINSTANCE.getRuleCall_ExplicitlyCalled()) {
        return YES;
    } else if (feature == XtextPackage.eINSTANCE.getNamedArgument_Parameter()) {
        if (((NamedArgument) owner).isCalledByName()) {
            return NO;
        }
        return YES;
    }
    return super.isValueTransient(owner, feature);
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) EnumRule(org.eclipse.xtext.EnumRule) EnumLiteralDeclaration(org.eclipse.xtext.EnumLiteralDeclaration) TypeRef(org.eclipse.xtext.TypeRef) CrossReference(org.eclipse.xtext.CrossReference) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 5 with TerminalRule

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

the class ToEcoreTrafoTest method testAbstractLanguageToMetamodel.

@Test
public void testAbstractLanguageToMetamodel() throws Exception {
    XtextResource r = getResource("classpath:/" + AbstractTestLanguage.class.getName().replace('.', '/') + ".xtext");
    Grammar element = (Grammar) r.getParseResult().getRootASTElement();
    if (!r.getErrors().isEmpty()) {
        EList<Diagnostic> errors = r.getErrors();
        for (Diagnostic syntaxError : errors) {
            logger.debug(syntaxError.getMessage() + " - " + syntaxError.getLine());
        }
        fail(errors.toString());
    }
    List<TerminalRule> lexerRules = GrammarUtil.allTerminalRules(element);
    assertEquals(8, lexerRules.size());
    List<EPackage> list = Xtext2EcoreTransformer.doGetGeneratedPackages(element);
    assertNotNull(list);
    assertEquals(0, list.size());
}
Also used : Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) TerminalRule(org.eclipse.xtext.TerminalRule) EPackage(org.eclipse.emf.ecore.EPackage) Test(org.junit.Test)

Aggregations

TerminalRule (org.eclipse.xtext.TerminalRule)51 ParserRule (org.eclipse.xtext.ParserRule)21 AbstractRule (org.eclipse.xtext.AbstractRule)20 RuleCall (org.eclipse.xtext.RuleCall)19 EnumRule (org.eclipse.xtext.EnumRule)17 Grammar (org.eclipse.xtext.Grammar)17 Test (org.junit.Test)16 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)12 AbstractElement (org.eclipse.xtext.AbstractElement)10 EObject (org.eclipse.emf.ecore.EObject)6 Action (org.eclipse.xtext.Action)6 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)5 Assignment (org.eclipse.xtext.Assignment)5 Function1 (org.eclipse.xtext.xbase.lib.Functions.Function1)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 EClass (org.eclipse.emf.ecore.EClass)4 Alternatives (org.eclipse.xtext.Alternatives)4 Group (org.eclipse.xtext.Group)4 Keyword (org.eclipse.xtext.Keyword)4