Search in sources :

Example 91 with AbstractRule

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

the class ValidatorFragment2Tests method testGenerate_NoValidation.

@Test
public void testGenerate_NoValidation() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar org.xtext.Foo with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("generate foo \"http://org.xtext/foo\"");
    _builder.newLine();
    _builder.append("Model: rules+=Rule;");
    _builder.newLine();
    _builder.append("@Deprecated");
    _builder.newLine();
    _builder.append("Rule: name=ID;");
    _builder.newLine();
    _builder.append("@Deprecated");
    _builder.newLine();
    _builder.append("CustomRule returns Rule: name=ID;");
    _builder.newLine();
    final ValidatorFragment2Tests.TestableValidatorFragment2 fragment = this.<ValidatorFragment2Tests.TestableValidatorFragment2>initializeFragmentWithGrammarFromString(ValidatorFragment2Tests.TestableValidatorFragment2.class, _builder.toString());
    fragment.setGenerateDeprecationValidation(false);
    final List<AbstractRule> deprecatedRulesFromGrammar = fragment.getDeprecatedRulesFromGrammar();
    Assert.assertEquals(1, deprecatedRulesFromGrammar.size());
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("package org.xtext.validation;");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("import java.util.ArrayList;");
    _builder_1.newLine();
    _builder_1.append("import java.util.List;");
    _builder_1.newLine();
    _builder_1.append("import org.eclipse.emf.ecore.EPackage;");
    _builder_1.newLine();
    _builder_1.append("import org.eclipse.xtext.validation.AbstractDeclarativeValidator;");
    _builder_1.newLine();
    _builder_1.newLine();
    _builder_1.append("public abstract class AbstractFooValidator extends AbstractDeclarativeValidator {");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("@Override");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("protected List<EPackage> getEPackages() {");
    _builder_1.newLine();
    _builder_1.append("\t\t");
    _builder_1.append("List<EPackage> result = new ArrayList<EPackage>();");
    _builder_1.newLine();
    _builder_1.append("\t\t");
    _builder_1.append("result.add(org.xtext.foo.FooPackage.eINSTANCE);");
    _builder_1.newLine();
    _builder_1.append("\t\t");
    _builder_1.append("return result;");
    _builder_1.newLine();
    _builder_1.append("\t");
    _builder_1.append("}");
    _builder_1.newLine();
    _builder_1.append("}");
    _builder_1.newLine();
    Assert.assertEquals(_builder_1.toString(), this.concatenationClientToString(fragment.generateGenValidator()));
}
Also used : StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) AbstractRule(org.eclipse.xtext.AbstractRule) Test(org.junit.Test)

Example 92 with AbstractRule

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

the class GrammarAccessExtensions method gaTypeAccessor.

/**
 * Returns the invocation of an element or rule accessor, including the .getType() call.
 * Example1: getFooRule().getType()
 * Example2: getBarRule().getFooAction().getType()
 */
public String gaTypeAccessor(final TypeRef ele) {
    String _switchResult = null;
    EObject _eContainer = ele.eContainer();
    final EObject cnt = _eContainer;
    boolean _matched = false;
    if (cnt instanceof AbstractElement) {
        _matched = true;
        String _gaRuleElementAccessor = this.gaRuleElementAccessor(((AbstractElement) cnt));
        _switchResult = (_gaRuleElementAccessor + ".getType()");
    }
    if (!_matched) {
        if (cnt instanceof AbstractRule) {
            _matched = true;
            String _gaRuleAccessor = this.gaRuleAccessor(((AbstractRule) cnt));
            _switchResult = (_gaRuleAccessor + ".getType()");
        }
    }
    if (!_matched) {
        String _name = ele.eContainer().eClass().getName();
        String _plus = ("<error: unknown type " + _name);
        _switchResult = (_plus + ">");
    }
    return _switchResult;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 93 with AbstractRule

use of org.eclipse.xtext.AbstractRule in project xtext-core 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;
            _switchResult = AntlrGrammarGenUtil.getQualifiedNameAsString(((RuleCall) it));
        }
    }
    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 94 with AbstractRule

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

the class GrammarAccessFragment2 method doGenerateGrammarAccess.

protected void doGenerateGrammarAccess() {
    final GeneratedJavaFileAccess javaFile = this.fileAccessFactory.createGeneratedJavaFile(this._grammarAccessExtensions.getGrammarAccess(this.getGrammar()));
    List<IClassAnnotation> _annotations = javaFile.getAnnotations();
    SingletonClassAnnotation _singletonClassAnnotation = new SingletonClassAnnotation();
    _annotations.add(_singletonClassAnnotation);
    StringConcatenationClient _client = new StringConcatenationClient() {

        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
            _builder.append("public class ");
            String _simpleName = GrammarAccessFragment2.this._grammarAccessExtensions.getGrammarAccess(GrammarAccessFragment2.this.getLanguage().getGrammar()).getSimpleName();
            _builder.append(_simpleName);
            _builder.append(" extends ");
            _builder.append(AbstractElementFinder.AbstractGrammarElementFinder.class);
            _builder.append(" {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.newLine();
            {
                Iterable<ParserRule> _filter = Iterables.<ParserRule>filter(GrammarAccessFragment2.this.getLanguage().getGrammar().getRules(), ParserRule.class);
                for (final ParserRule r : _filter) {
                    _builder.append("\t");
                    StringConcatenationClient _parserRuleClasses = GrammarAccessFragment2.this.parserRuleClasses(r);
                    _builder.append(_parserRuleClasses, "\t");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t");
            _builder.newLine();
            {
                Iterable<EnumRule> _filter_1 = Iterables.<EnumRule>filter(GrammarAccessFragment2.this.getLanguage().getGrammar().getRules(), EnumRule.class);
                for (final EnumRule r_1 : _filter_1) {
                    _builder.append("\t");
                    StringConcatenationClient _parserRuleClasses_1 = GrammarAccessFragment2.this.parserRuleClasses(r_1);
                    _builder.append(_parserRuleClasses_1, "\t");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t");
            _builder.newLine();
            {
                EList<AbstractRule> _rules = GrammarAccessFragment2.this.getLanguage().getGrammar().getRules();
                for (final AbstractRule r_2 : _rules) {
                    _builder.append("\t");
                    StringConcatenationClient _cache = GrammarAccessFragment2.this.cache(r_2);
                    _builder.append(_cache, "\t");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("private final ");
            _builder.append(Grammar.class, "\t");
            _builder.append(" grammar;");
            _builder.newLineIfNotEmpty();
            {
                List<Grammar> _effectivelyUsedGrammars = GrammarAccessFragment2.this.getEffectivelyUsedGrammars(GrammarAccessFragment2.this.getLanguage().getGrammar());
                for (final Grammar g : _effectivelyUsedGrammars) {
                    _builder.append("\t");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("private final ");
                    TypeReference _grammarAccess = GrammarAccessFragment2.this._grammarAccessExtensions.getGrammarAccess(g);
                    _builder.append(_grammarAccess, "\t");
                    _builder.append(" ");
                    String _gaGrammarAccessLocalVarName = GrammarAccessFragment2.this.gaGrammarAccessLocalVarName(g);
                    _builder.append(_gaGrammarAccessLocalVarName, "\t");
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.newLine();
            _builder.append("\t");
            _builder.append("@");
            _builder.append(Inject.class, "\t");
            _builder.newLineIfNotEmpty();
            _builder.append("\t");
            _builder.append("public ");
            String _simpleName_1 = GrammarAccessFragment2.this._grammarAccessExtensions.getGrammarAccess(GrammarAccessFragment2.this.getLanguage().getGrammar()).getSimpleName();
            _builder.append(_simpleName_1, "\t");
            _builder.append("(");
            _builder.append(GrammarProvider.class, "\t");
            _builder.append(" grammarProvider");
            {
                List<Grammar> _effectivelyUsedGrammars_1 = GrammarAccessFragment2.this.getEffectivelyUsedGrammars(GrammarAccessFragment2.this.getLanguage().getGrammar());
                for (final Grammar g_1 : _effectivelyUsedGrammars_1) {
                    _builder.append(",");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t\t");
                    TypeReference _grammarAccess_1 = GrammarAccessFragment2.this._grammarAccessExtensions.getGrammarAccess(g_1);
                    _builder.append(_grammarAccess_1, "\t\t\t");
                    _builder.append(" ");
                    String _gaGrammarAccessLocalVarName_1 = GrammarAccessFragment2.this.gaGrammarAccessLocalVarName(g_1);
                    _builder.append(_gaGrammarAccessLocalVarName_1, "\t\t\t");
                }
            }
            _builder.append(") {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("this.grammar = internalFindGrammar(grammarProvider);");
            _builder.newLine();
            {
                List<Grammar> _effectivelyUsedGrammars_2 = GrammarAccessFragment2.this.getEffectivelyUsedGrammars(GrammarAccessFragment2.this.getLanguage().getGrammar());
                for (final Grammar g_2 : _effectivelyUsedGrammars_2) {
                    _builder.append("\t\t");
                    _builder.append("this.");
                    String _gaGrammarAccessLocalVarName_2 = GrammarAccessFragment2.this.gaGrammarAccessLocalVarName(g_2);
                    _builder.append(_gaGrammarAccessLocalVarName_2, "\t\t");
                    _builder.append(" = ");
                    String _gaGrammarAccessLocalVarName_3 = GrammarAccessFragment2.this.gaGrammarAccessLocalVarName(g_2);
                    _builder.append(_gaGrammarAccessLocalVarName_3, "\t\t");
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                }
            }
            {
                EList<AbstractRule> _rules_1 = GrammarAccessFragment2.this.getLanguage().getGrammar().getRules();
                for (final AbstractRule r_3 : _rules_1) {
                    _builder.append("\t\t");
                    StringConcatenationClient _initializer = GrammarAccessFragment2.this.initializer(r_3);
                    _builder.append(_initializer, "\t\t");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("protected ");
            _builder.append(Grammar.class, "\t");
            _builder.append(" internalFindGrammar(");
            _builder.append(GrammarProvider.class, "\t");
            _builder.append(" grammarProvider) {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append(Grammar.class, "\t\t");
            _builder.append(" grammar = grammarProvider.getGrammar(this);");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("while (grammar != null) {");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("if (\"");
            String _name = GrammarAccessFragment2.this.getLanguage().getGrammar().getName();
            _builder.append(_name, "\t\t\t");
            _builder.append("\".equals(grammar.getName())) {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t\t\t");
            _builder.append("return grammar;");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append(List.class, "\t\t\t");
            _builder.append("<");
            _builder.append(Grammar.class, "\t\t\t");
            _builder.append("> grammars = grammar.getUsedGrammars();");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t\t");
            _builder.append("if (!grammars.isEmpty()) {");
            _builder.newLine();
            _builder.append("\t\t\t\t");
            _builder.append("grammar = grammars.iterator().next();");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("} else {");
            _builder.newLine();
            _builder.append("\t\t\t\t");
            _builder.append("return null;");
            _builder.newLine();
            _builder.append("\t\t\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t\t");
            _builder.append("return grammar;");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("@Override");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("public ");
            _builder.append(Grammar.class, "\t");
            _builder.append(" getGrammar() {");
            _builder.newLineIfNotEmpty();
            _builder.append("\t\t");
            _builder.append("return grammar;");
            _builder.newLine();
            _builder.append("\t");
            _builder.append("}");
            _builder.newLine();
            _builder.append("\t");
            _builder.newLine();
            {
                List<Grammar> _effectivelyUsedGrammars_3 = GrammarAccessFragment2.this.getEffectivelyUsedGrammars(GrammarAccessFragment2.this.getLanguage().getGrammar());
                for (final Grammar g_3 : _effectivelyUsedGrammars_3) {
                    _builder.append("\t");
                    _builder.newLine();
                    _builder.append("\t");
                    _builder.append("public ");
                    TypeReference _grammarAccess_2 = GrammarAccessFragment2.this._grammarAccessExtensions.getGrammarAccess(g_3);
                    _builder.append(_grammarAccess_2, "\t");
                    _builder.append(" get");
                    String _simpleName_2 = GrammarAccessFragment2.this._grammarAccessExtensions.getGrammarAccess(g_3).getSimpleName();
                    _builder.append(_simpleName_2, "\t");
                    _builder.append("() {");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("\t");
                    _builder.append("return ");
                    String _gaGrammarAccessLocalVarName_4 = GrammarAccessFragment2.this.gaGrammarAccessLocalVarName(g_3);
                    _builder.append(_gaGrammarAccessLocalVarName_4, "\t\t");
                    _builder.append(";");
                    _builder.newLineIfNotEmpty();
                    _builder.append("\t");
                    _builder.append("}");
                    _builder.newLine();
                }
            }
            _builder.newLine();
            {
                List<AbstractRule> _allRules = GrammarUtil.allRules(GrammarAccessFragment2.this.getLanguage().getGrammar());
                for (final AbstractRule r_4 : _allRules) {
                    _builder.append("\t");
                    _builder.newLine();
                    _builder.append("\t");
                    StringConcatenationClient _ter = GrammarAccessFragment2.this.getter(r_4, GrammarAccessFragment2.this.getLanguage().getGrammar());
                    _builder.append(_ter, "\t");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("}");
            _builder.newLine();
        }
    };
    javaFile.setContent(_client);
    javaFile.writeTo(this.getProjectConfig().getRuntime().getSrcGen());
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) SingletonClassAnnotation(org.eclipse.xtext.xtext.generator.model.annotations.SingletonClassAnnotation) Grammar(org.eclipse.xtext.Grammar) EnumRule(org.eclipse.xtext.EnumRule) EList(org.eclipse.emf.common.util.EList) AbstractElementFinder(org.eclipse.xtext.service.AbstractElementFinder) IClassAnnotation(org.eclipse.xtext.xtext.generator.model.annotations.IClassAnnotation) GeneratedJavaFileAccess(org.eclipse.xtext.xtext.generator.model.GeneratedJavaFileAccess) List(java.util.List) EList(org.eclipse.emf.common.util.EList) TypeReference(org.eclipse.xtext.xtext.generator.model.TypeReference) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 95 with AbstractRule

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

the class AntlrGrammarGenUtil method getQualifiedNameAsString.

/**
 * @since 2.9
 */
public static String getQualifiedNameAsString(RuleCall ruleCall) {
    AbstractRule rule = getOriginalElement(ruleCall).getRule();
    String result = RuleNames.getRuleNames(rule).getQualifiedName(rule);
    return '"' + result + '"';
}
Also used : AbstractRule(org.eclipse.xtext.AbstractRule)

Aggregations

AbstractRule (org.eclipse.xtext.AbstractRule)222 Test (org.junit.Test)128 Grammar (org.eclipse.xtext.Grammar)106 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)96 ParserRule (org.eclipse.xtext.ParserRule)94 EObject (org.eclipse.emf.ecore.EObject)59 AbstractXtextRuleInspectorTest (org.eclipse.xtext.xtext.AbstractXtextRuleInspectorTest)49 RuleCall (org.eclipse.xtext.RuleCall)36 AbstractElement (org.eclipse.xtext.AbstractElement)34 TerminalRule (org.eclipse.xtext.TerminalRule)20 XtextResource (org.eclipse.xtext.resource.XtextResource)17 EClassifier (org.eclipse.emf.ecore.EClassifier)14 EnumRule (org.eclipse.xtext.EnumRule)14 ArrayList (java.util.ArrayList)12 Assignment (org.eclipse.xtext.Assignment)12 Group (org.eclipse.xtext.Group)12 InternalEObject (org.eclipse.emf.ecore.InternalEObject)11 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)11 EClass (org.eclipse.emf.ecore.EClass)10 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)10