Search in sources :

Example 26 with TerminalRule

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

the class ToEcoreTrafoTest method testConcreteLanguageToMetamodel.

// TODO Review test expectation change - see Bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=475692
@Test
public void testConcreteLanguageToMetamodel() throws Exception {
    XtextResource r = getResource("classpath:/" + ConcreteTestLanguage.class.getName().replace('.', '/') + ".xtext");
    Grammar element = (Grammar) r.getContents().get(0);
    List<TerminalRule> lexerRules = GrammarUtil.allTerminalRules(element);
    assertEquals("Number of lexer-rules in Grammar", 9, lexerRules.size());
    List<EPackage> list = Xtext2EcoreTransformer.doGetGeneratedPackages(element);
    EPackage metaModel = list.get(0);
    assertNotNull(metaModel);
    assertNotNull(metaModel.getNsPrefix());
    assertNotNull(metaModel.getNsURI());
    assertNotNull(metaModel.getName());
    EList<EClassifier> types = metaModel.getEClassifiers();
    EClass type = (EClass) types.get(1);
    EStructuralFeature feature = type.getEStructuralFeature("elements");
    assertNotNull(feature.getEType());
}
Also used : EClass(org.eclipse.emf.ecore.EClass) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) EClassifier(org.eclipse.emf.ecore.EClassifier) 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)

Example 27 with TerminalRule

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

the class AbstractInternalAntlrParser method createAllRules.

/**
 * @since 2.11
 */
protected Map<String, AbstractRule> createAllRules(Grammar grammar) {
    AllRulesCache cache = AllRulesCache.findInEmfObject(grammar);
    if (cache != null) {
        return cache.getAllRules();
    }
    Map<String, AbstractRule> allRules = Maps.newHashMap();
    for (AbstractRule rule : GrammarUtil.allRules(grammar)) {
        if (rule instanceof TerminalRule)
            allRules.put(rule.getName().toUpperCase(), rule);
        else
            allRules.put(rule.getName(), rule);
    }
    return allRules;
}
Also used : AllRulesCache(org.eclipse.xtext.service.AllRulesCache) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 28 with TerminalRule

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

the class IdeaPluginGenerator method compileTokenTypeProvider.

public JavaFileAccess compileTokenTypeProvider(final Grammar grammar) {
    final TypeReference tokenSet = TypeReference.typeRef("com.intellij.psi.tree.TokenSet");
    final TypeReference iElementType = TypeReference.typeRef("com.intellij.psi.tree.IElementType");
    final String indexedElementType = "IndexedElementType";
    TypeReference _tokenTypeProvider = this._ideaPluginClassNames.getTokenTypeProvider(grammar);
    StringConcatenationClient _client = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            _builder.append("@");
            _builder.append(Singleton.class);
            _builder.newLineIfNotEmpty();
            _builder.append("public class ");
            String _simpleName = IdeaPluginGenerator.this._ideaPluginClassNames.getTokenTypeProvider(grammar).getSimpleName();
            _builder.append(_simpleName);
            _builder.append(" implements ");
            TypeReference _typeRef = TypeReference.typeRef("org.eclipse.xtext.idea.parser.TokenTypeProvider");
            _builder.append(_typeRef);
            _builder.append(" {");
            _builder.newLineIfNotEmpty();
            _builder.newLine();
            _builder.append("\t");
            _builder.append("private static final String[] TOKEN_NAMES = new ");
            TypeReference _psiInternalParser = IdeaPluginGenerator.this._ideaPluginClassNames.getPsiInternalParser(grammar);
            _builder.append(_psiInternalParser, "\t");
            _builder.append("(null).getTokenNames();");
            _builder.newLineIfNotEmpty();
            _builder.newLine();
            _builder.append("\t");
            _builder.append("private static final ");
            _builder.append(iElementType, "\t");
            _builder.append("[] tokenTypes = new ");
            _builder.append(iElementType, "\t");
            _builder.append("[TOKEN_NAMES.length];");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("static {");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("for (int i = 0; i < TOKEN_NAMES.length; i++) {");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("tokenTypes[i] = new ");
            _builder.append(indexedElementType, "\t\t\t");
            _builder.append("(TOKEN_NAMES[i], i, ");
            TypeReference _ideaLanguage = IdeaPluginGenerator.this._ideaPluginClassNames.getIdeaLanguage(grammar);
            _builder.append(_ideaLanguage, "\t\t\t");
            _builder.append(".INSTANCE);");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.newLine();
            {
                final Function1<TerminalRule, Boolean> _function = (TerminalRule it) -> {
                    String _name = it.getName();
                    return Boolean.valueOf(Objects.equal(_name, "WS"));
                };
                boolean _exists = IterableExtensions.<TerminalRule>exists(GrammarUtil.allTerminalRules(grammar), _function);
                if (_exists) {
                    _builder.append("\t");
                    _builder.append("private static final ");
                    _builder.append(tokenSet, "\t");
                    _builder.append(" WHITESPACE_TOKENS = ");
                    _builder.append(tokenSet, "\t");
                    _builder.append(".create(tokenTypes[");
                    TypeReference _psiInternalParser_1 = IdeaPluginGenerator.this._ideaPluginClassNames.getPsiInternalParser(grammar);
                    _builder.append(_psiInternalParser_1, "\t");
                    _builder.append(".RULE_WS]);");
                    _builder.newLineIfNotEmpty();
                } else {
                    _builder.append("\t");
                    _builder.append("private static final ");
                    _builder.append(tokenSet, "\t");
                    _builder.append(" WHITESPACE_TOKENS = ");
                    _builder.append(tokenSet, "\t");
                    _builder.append(".EMPTY;");
                    _builder.newLineIfNotEmpty();
                }
            }
            {
                if ((IterableExtensions.<TerminalRule>exists(GrammarUtil.allTerminalRules(grammar), ((Function1<TerminalRule, Boolean>) (TerminalRule it) -> {
                    String _name = it.getName();
                    return Boolean.valueOf(Objects.equal(_name, "SL_COMMENT"));
                })) && IterableExtensions.<TerminalRule>exists(GrammarUtil.allTerminalRules(grammar), ((Function1<TerminalRule, Boolean>) (TerminalRule it) -> {
                    String _name = it.getName();
                    return Boolean.valueOf(Objects.equal(_name, "ML_COMMENT"));
                })))) {
                    _builder.append("\t");
                    _builder.append("private static final ");
                    _builder.append(tokenSet, "\t");
                    _builder.append(" COMMENT_TOKENS = ");
                    _builder.append(tokenSet, "\t");
                    _builder.append(".create(tokenTypes[");
                    TypeReference _psiInternalParser_2 = IdeaPluginGenerator.this._ideaPluginClassNames.getPsiInternalParser(grammar);
                    _builder.append(_psiInternalParser_2, "\t");
                    _builder.append(".RULE_SL_COMMENT], tokenTypes[");
                    TypeReference _psiInternalParser_3 = IdeaPluginGenerator.this._ideaPluginClassNames.getPsiInternalParser(grammar);
                    _builder.append(_psiInternalParser_3, "\t");
                    _builder.append(".RULE_ML_COMMENT]);");
                    _builder.newLineIfNotEmpty();
                } else {
                    final Function1<TerminalRule, Boolean> _function_1 = (TerminalRule it) -> {
                        String _name = it.getName();
                        return Boolean.valueOf(Objects.equal(_name, "SL_COMMENT"));
                    };
                    boolean _exists_1 = IterableExtensions.<TerminalRule>exists(GrammarUtil.allTerminalRules(grammar), _function_1);
                    if (_exists_1) {
                        _builder.append("\t");
                        _builder.append("private static final ");
                        _builder.append(tokenSet, "\t");
                        _builder.append(" COMMENT_TOKENS = ");
                        _builder.append(tokenSet, "\t");
                        _builder.append(".create(tokenTypes[");
                        TypeReference _psiInternalParser_4 = IdeaPluginGenerator.this._ideaPluginClassNames.getPsiInternalParser(grammar);
                        _builder.append(_psiInternalParser_4, "\t");
                        _builder.append(".RULE_SL_COMMENT]);");
                        _builder.newLineIfNotEmpty();
                    } else {
                        final Function1<TerminalRule, Boolean> _function_2 = (TerminalRule it) -> {
                            String _name = it.getName();
                            return Boolean.valueOf(Objects.equal(_name, "ML_COMMENT"));
                        };
                        boolean _exists_2 = IterableExtensions.<TerminalRule>exists(GrammarUtil.allTerminalRules(grammar), _function_2);
                        if (_exists_2) {
                            _builder.append("\t");
                            _builder.append("private static final ");
                            _builder.append(tokenSet, "\t");
                            _builder.append(" COMMENT_TOKENS = ");
                            _builder.append(tokenSet, "\t");
                            _builder.append(".create(tokenTypes[");
                            TypeReference _psiInternalParser_5 = IdeaPluginGenerator.this._ideaPluginClassNames.getPsiInternalParser(grammar);
                            _builder.append(_psiInternalParser_5, "\t");
                            _builder.append(".RULE_ML_COMMENT]);");
                            _builder.newLineIfNotEmpty();
                        } else {
                            _builder.append("\t");
                            _builder.append("private static final ");
                            _builder.append(tokenSet, "\t");
                            _builder.append(" COMMENT_TOKENS = ");
                            _builder.append(tokenSet, "\t");
                            _builder.append(".EMPTY;");
                            _builder.newLineIfNotEmpty();
                        }
                    }
                }
            }
            {
                final Function1<TerminalRule, Boolean> _function_3 = (TerminalRule it) -> {
                    String _name = it.getName();
                    return Boolean.valueOf(Objects.equal(_name, "STRING"));
                };
                boolean _exists_3 = IterableExtensions.<TerminalRule>exists(GrammarUtil.allTerminalRules(grammar), _function_3);
                if (_exists_3) {
                    _builder.append("\t");
                    _builder.append("private static final ");
                    _builder.append(tokenSet, "\t");
                    _builder.append(" STRING_TOKENS = ");
                    _builder.append(tokenSet, "\t");
                    _builder.append(".create(tokenTypes[");
                    TypeReference _psiInternalParser_6 = IdeaPluginGenerator.this._ideaPluginClassNames.getPsiInternalParser(grammar);
                    _builder.append(_psiInternalParser_6, "\t");
                    _builder.append(".RULE_STRING]);");
                    _builder.newLineIfNotEmpty();
                } else {
                    _builder.append("\t");
                    _builder.append("private static final ");
                    _builder.append(tokenSet, "\t");
                    _builder.append(" STRING_TOKENS = ");
                    _builder.append(tokenSet, "\t");
                    _builder.append(".EMPTY;");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.newLine();
            _builder.append("\t");
            _builder.append("@Override");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("public int getAntlrType(");
            _builder.append(iElementType, "\t");
            _builder.append(" iElementType) {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("return (iElementType instanceof ");
            _builder.append(indexedElementType, "\t\t");
            _builder.append(") ? ((");
            _builder.append(indexedElementType, "\t\t");
            _builder.append(") iElementType).getLocalIndex() : ");
            _builder.append(Token.class, "\t\t");
            _builder.append(".INVALID_TOKEN_TYPE;");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.newLine();
            _builder.append("\t");
            _builder.append("@Override");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("public ");
            _builder.append(iElementType, "\t");
            _builder.append(" getIElementType(int antlrType) {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("return tokenTypes[antlrType];");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.newLine();
            _builder.append("\t");
            _builder.append("@Override");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("public ");
            _builder.append(tokenSet, "\t");
            _builder.append(" getWhitespaceTokens() {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("return WHITESPACE_TOKENS;");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.newLine();
            _builder.append("\t");
            _builder.append("@Override");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("public ");
            _builder.append(tokenSet, "\t");
            _builder.append(" getCommentTokens() {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("return COMMENT_TOKENS;");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.newLine();
            _builder.append("\t");
            _builder.append("@Override");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("public ");
            _builder.append(tokenSet, "\t");
            _builder.append(" getStringLiteralTokens() {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("return STRING_TOKENS;");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.newLine();
            _builder.append("}");
            _builder.newLine();
        }
    };
    return this.fileAccessFactory.createJavaFile(_tokenTypeProvider, _client);
}
Also used : StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference) TerminalRule(org.eclipse.xtext.TerminalRule)

Example 29 with TerminalRule

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

the class PsiAntlrGrammarGenerator method crossrefEbnf.

@Override
protected String crossrefEbnf(final AbstractRule it, final RuleCall call, final CrossReference ref, final boolean supportActions) {
    String _xifexpression = null;
    if (supportActions) {
        String _switchResult = null;
        boolean _matched = false;
        if (it instanceof EnumRule) {
            _matched = true;
        }
        if (!_matched) {
            if (it instanceof ParserRule) {
                _matched = true;
            }
        }
        if (_matched) {
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("{");
            _builder.newLine();
            _builder.append("\t");
            CharSequence _markComposite = this.markComposite(ref);
            _builder.append(_markComposite, "\t");
            _builder.newLineIfNotEmpty();
            _builder.append("}");
            _builder.newLine();
            String _crossrefEbnf = super.crossrefEbnf(it, call, ref, supportActions);
            _builder.append(_crossrefEbnf);
            _builder.newLineIfNotEmpty();
            _builder.append("{");
            _builder.newLine();
            _builder.append("\t");
            CharSequence _doneComposite = this.doneComposite(ref);
            _builder.append(_doneComposite, "\t");
            _builder.newLineIfNotEmpty();
            _builder.append("}");
            _builder.newLine();
            _switchResult = _builder.toString();
        }
        if (!_matched) {
            if (it instanceof TerminalRule) {
                _matched = true;
                StringConcatenation _builder_1 = new StringConcatenation();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                CharSequence _markLeaf = this.markLeaf(ref);
                _builder_1.append(_markLeaf, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                String _localVar = this._grammarAccessExtensions.localVar(GrammarUtil.containingAssignment(ref));
                _builder_1.append(_localVar);
                _builder_1.append("=");
                String _crossrefEbnf_1 = super.crossrefEbnf(it, call, ref, supportActions);
                _builder_1.append(_crossrefEbnf_1);
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("{");
                _builder_1.newLine();
                _builder_1.append("\t");
                CharSequence _doneLeaf = this.doneLeaf(ref, this._grammarAccessExtensions.localVar(GrammarUtil.containingAssignment(ref)));
                _builder_1.append(_doneLeaf, "\t");
                _builder_1.newLineIfNotEmpty();
                _builder_1.append("}");
                _builder_1.newLine();
                _switchResult = _builder_1.toString();
            }
        }
        if (!_matched) {
            throw new IllegalStateException(("crossrefEbnf is not supported for " + it));
        }
        _xifexpression = _switchResult;
    } else {
        _xifexpression = super.crossrefEbnf(it, call, ref, supportActions);
    }
    return _xifexpression;
}
Also used : EnumRule(org.eclipse.xtext.EnumRule) ParserRule(org.eclipse.xtext.ParserRule) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) TerminalRule(org.eclipse.xtext.TerminalRule)

Example 30 with TerminalRule

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

the class AbstractAntlrGrammarGenerator method compileKeywordRules.

protected CharSequence compileKeywordRules(final Grammar it, final AntlrOptions options) {
    CharSequence _xblockexpression = null;
    {
        final Set<String> allKeywords = this.keywordHelper.getAllKeywords();
        final List<TerminalRule> allTerminalRules = GrammarUtil.allTerminalRules(it);
        final ArrayList<String> synthetic_kw_alternatives = CollectionLiterals.<String>newArrayList();
        final Function1<Pair<Integer, String>, String> _function = (Pair<Integer, String> it_1) -> {
            final String ruleName = this.keywordHelper.getRuleName(it_1.getValue());
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("(FRAGMENT_");
            _builder.append(ruleName);
            _builder.append(")=> FRAGMENT_");
            _builder.append(ruleName);
            _builder.append(" {$type = ");
            _builder.append(ruleName);
            _builder.append("; }");
            return _builder.toString();
        };
        Iterables.<String>addAll(synthetic_kw_alternatives, IterableExtensions.<Pair<Integer, String>, String>map(IterableExtensions.<String>indexed(allKeywords), _function));
        final Function1<Pair<Integer, TerminalRule>, String> _function_1 = (Pair<Integer, TerminalRule> it_1) -> {
            if (((!this._syntheticTerminalDetector.isSyntheticTerminalRule(it_1.getValue())) && (!it_1.getValue().isFragment()))) {
                StringConcatenation _builder = new StringConcatenation();
                _builder.append("(FRAGMENT_");
                String _ruleName = this._grammarAccessExtensions.ruleName(it_1.getValue());
                _builder.append(_ruleName);
                _builder.append(")=> FRAGMENT_");
                String _ruleName_1 = this._grammarAccessExtensions.ruleName(it_1.getValue());
                _builder.append(_ruleName_1);
                _builder.append(" {$type = ");
                String _ruleName_2 = this._grammarAccessExtensions.ruleName(it_1.getValue());
                _builder.append(_ruleName_2);
                _builder.append("; }");
                return _builder.toString();
            }
            return null;
        };
        synthetic_kw_alternatives.addAll(IterableExtensions.<String>toList(IterableExtensions.<String>filterNull(IterableExtensions.<Pair<Integer, TerminalRule>, String>map(IterableExtensions.<TerminalRule>indexed(allTerminalRules), _function_1))));
        StringConcatenation _builder = new StringConcatenation();
        {
            boolean _isBacktrackLexer = options.isBacktrackLexer();
            if (_isBacktrackLexer) {
                _builder.append("SYNTHETIC_ALL_KEYWORDS :");
                _builder.newLine();
                {
                    boolean _hasElements = false;
                    for (final String kw : synthetic_kw_alternatives) {
                        if (!_hasElements) {
                            _hasElements = true;
                        } else {
                            _builder.appendImmediate(" |", "\t");
                        }
                        _builder.append("\t");
                        _builder.append(kw, "\t");
                        _builder.newLineIfNotEmpty();
                    }
                }
                _builder.append(";");
                _builder.newLine();
                _builder.newLine();
                {
                    for (final String kw_1 : allKeywords) {
                        _builder.append("fragment FRAGMENT_");
                        String _ruleName = this.keywordHelper.getRuleName(kw_1);
                        _builder.append(_ruleName);
                        _builder.append(" : \'");
                        String _antlrString = AntlrGrammarGenUtil.toAntlrString(kw_1);
                        _builder.append(_antlrString);
                        _builder.append("\';");
                        _builder.newLineIfNotEmpty();
                        _builder.newLine();
                    }
                }
            } else {
                {
                    for (final String rule : allKeywords) {
                        _builder.newLine();
                        CharSequence _compileRule = this.compileRule(rule, it, options);
                        _builder.append(_compileRule);
                        _builder.newLineIfNotEmpty();
                    }
                }
            }
        }
        _xblockexpression = _builder;
    }
    return _xblockexpression;
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) List(java.util.List) TerminalRule(org.eclipse.xtext.TerminalRule) Pair(org.eclipse.xtext.xbase.lib.Pair)

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