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());
}
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;
}
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);
}
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;
}
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;
}
Aggregations