Search in sources :

Example 1 with Grammar

use of org.eclipse.xtext.Grammar 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 Grammar

use of org.eclipse.xtext.Grammar 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 Grammar

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

the class SuperCallScope method getElements.

@Override
public Iterable<IEObjectDescription> getElements(EObject object) {
    if (object instanceof AbstractRule) {
        Grammar grammar = GrammarUtil.getGrammar(context);
        AbstractRule rule = (AbstractRule) object;
        if (GrammarUtil.getGrammar(rule) == grammar) {
            return Lists.newArrayList(EObjectDescription.create(GrammarUtil.getSimpleName(grammar) + "." + rule.getName(), rule), EObjectDescription.create(grammar.getName() + "." + rule.getName(), rule));
        }
        List<IEObjectDescription> result = Lists.newArrayList(EObjectDescription.create(SUPER + "." + rule.getName(), rule), EObjectDescription.create(GrammarUtil.getSimpleName(grammar) + "." + rule.getName(), rule), EObjectDescription.create(grammar.getName() + "." + rule.getName(), rule));
        AbstractRule contextRule = GrammarUtil.containingRule(context);
        if (contextRule != null && contextRule.getName().equals(rule.getName())) {
            result.add(0, EObjectDescription.create(SUPER, rule));
        }
        return result;
    }
    return Collections.emptyList();
}
Also used : Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 4 with Grammar

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

the class XtextFragmentProvider method getEObject.

@Override
public EObject getEObject(Resource resource, String fragment, IFragmentProvider.Fallback fallback) {
    if (!fragment.startsWith(PREFIX))
        return fallback.getEObject(fragment);
    String fragmentWithoutPrefix = fragment.substring(PREFIX.length());
    List<String> splitted = Strings.split(fragmentWithoutPrefix, '/');
    if (splitted.isEmpty()) {
        return fallback.getEObject(fragment);
    }
    String firstPart = splitted.get(0);
    Grammar grammar = null;
    for (EObject content : resource.getContents()) {
        if (content instanceof Grammar) {
            Grammar g = (Grammar) content;
            if (firstPart.equals(g.getName())) {
                grammar = g;
                if (splitted.size() == 1)
                    return grammar;
                break;
            }
        }
    }
    if (splitted.size() == 2) {
        return GrammarUtil.findRuleForName(grammar, splitted.get(1));
    } else {
        return fallback.getEObject(fragment);
    }
}
Also used : EObject(org.eclipse.emf.ecore.EObject) Grammar(org.eclipse.xtext.Grammar)

Example 5 with Grammar

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

the class XtextLinker method linkModel.

@Override
public void linkModel(EObject model, IDiagnosticConsumer consumer) {
    if (model instanceof Grammar) {
        final Xtext2EcoreTransformer transformer = createTransformer((Grammar) model, consumer);
        // TODO duplicate
        transformer.removeGeneratedPackages();
        super.linkModel(model, consumer);
        updateOverriddenRules((Grammar) model);
        try {
            transformer.transform();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            consumer.consume(new ExceptionDiagnostic(e), Severity.ERROR);
        }
        if (!model.eResource().eAdapters().contains(packageRemover))
            model.eResource().eAdapters().add(packageRemover);
    } else {
        super.linkModel(model, consumer);
    }
}
Also used : Xtext2EcoreTransformer(org.eclipse.xtext.xtext.ecoreInference.Xtext2EcoreTransformer) ExceptionDiagnostic(org.eclipse.xtext.diagnostics.ExceptionDiagnostic) Grammar(org.eclipse.xtext.Grammar)

Aggregations

Grammar (org.eclipse.xtext.Grammar)356 Test (org.junit.Test)273 ParserRule (org.eclipse.xtext.ParserRule)151 AbstractRule (org.eclipse.xtext.AbstractRule)106 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)101 EObject (org.eclipse.emf.ecore.EObject)70 AbstractXtextRuleInspectorTest (org.eclipse.xtext.xtext.AbstractXtextRuleInspectorTest)49 XtextResource (org.eclipse.xtext.resource.XtextResource)33 RuleCall (org.eclipse.xtext.RuleCall)28 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)23 TypeReference (org.eclipse.xtext.xtext.generator.model.TypeReference)23 AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)21 Assignment (org.eclipse.xtext.Assignment)21 EPackage (org.eclipse.emf.ecore.EPackage)20 AbstractElement (org.eclipse.xtext.AbstractElement)17 TerminalRule (org.eclipse.xtext.TerminalRule)17 Resource (org.eclipse.emf.ecore.resource.Resource)16 EClassifier (org.eclipse.emf.ecore.EClassifier)13 InternalEObject (org.eclipse.emf.ecore.InternalEObject)13 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)12