Search in sources :

Example 96 with AbstractRule

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

the class AntlrGrammarGenUtil method getEntryRuleName.

/**
 * @since 2.9
 */
public static String getEntryRuleName(ParserRule rule) {
    RuleWithParameterValues parameterValues = RuleWithParameterValues.findInEmfObject(rule);
    if (parameterValues != null) {
        if (parameterValues.getParamValues().isEmpty()) {
            AbstractRule original = parameterValues.getOriginal();
            RuleNames ruleNames = RuleNames.getRuleNames(original);
            return "entry" + Strings.toFirstUpper(ruleNames.getAntlrRuleName(original));
        }
        return null;
    } else {
        RuleNames ruleNames = RuleNames.getRuleNames(rule);
        return "entry" + Strings.toFirstUpper(ruleNames.getAntlrRuleName(rule));
    }
}
Also used : RuleNames(org.eclipse.xtext.xtext.RuleNames) RuleWithParameterValues(org.eclipse.xtext.xtext.RuleWithParameterValues) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 97 with AbstractRule

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

the class AntlrGrammarGenUtil method getArgumentList.

/**
 * @since 2.9
 */
public static String getArgumentList(final RuleCall ruleCall, boolean passCurrentIntoFragment, boolean isPredicate) {
    final List<NamedArgument> arguments = ruleCall.getArguments();
    AbstractRule abstractRule = ruleCall.getRule();
    boolean needsCurrent = passCurrentIntoFragment && GrammarUtil.isEObjectFragmentRule(abstractRule) && !GrammarUtil.isDatatypeRule((ParserRule) getOriginalElement(abstractRule));
    if (arguments.isEmpty()) {
        if (needsCurrent) {
            return isPredicate ? "[null]" : "[$current]";
        }
        return "";
    }
    ParserRule rule = (ParserRule) abstractRule;
    StringBuilder result = new StringBuilder();
    result.append("[");
    if (needsCurrent) {
        if (isPredicate) {
            result.append("null, ");
        } else {
            result.append("$current, ");
        }
    }
    Joiner.on(", ").appendTo(result, Iterables.transform(rule.getParameters(), new Function<Parameter, String>() {

        @Override
        public String apply(Parameter input) {
            for (NamedArgument argument : arguments) {
                if (argument.getParameter() == input) {
                    return conditionToAntlr(argument.getValue(), true);
                }
            }
            throw new IllegalStateException("Cannot find argument for parameter: " + input.getName());
        }
    }));
    result.append("]");
    return result.toString();
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Function(com.google.common.base.Function) Parameter(org.eclipse.xtext.Parameter) NamedArgument(org.eclipse.xtext.NamedArgument) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 98 with AbstractRule

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

the class Xtext2EcoreTransformer method deriveFeatures.

private Xtext2EcoreInterpretationContext deriveFeatures(final Xtext2EcoreInterpretationContext context, AbstractElement element) {
    XtextSwitch<Xtext2EcoreInterpretationContext> visitor = new XtextSwitch<Xtext2EcoreInterpretationContext>() {

        /*
			 * Used for Alternatives and UnorderedGroups
			 */
        @Override
        public Xtext2EcoreInterpretationContext caseCompoundElement(CompoundElement object) {
            List<Xtext2EcoreInterpretationContext> contexts = new ArrayList<Xtext2EcoreInterpretationContext>();
            for (AbstractElement group : object.getElements()) {
                contexts.add(deriveFeatures(context, group));
            }
            Xtext2EcoreInterpretationContext result = context;
            if (!contexts.isEmpty()) {
                if (GrammarUtil.isOptionalCardinality(object)) {
                    contexts.add(0, result);
                } else {
                    result = contexts.get(0);
                }
                result = result.mergeSpawnedContexts(contexts);
            }
            return result;
        }

        @Override
        public Xtext2EcoreInterpretationContext caseAssignment(Assignment object) {
            try {
                context.addFeature(object);
            } catch (TransformationException ex) {
                reportError(ex);
            }
            return context;
        }

        @Override
        public Xtext2EcoreInterpretationContext caseGroup(Group object) {
            return visitElements(object, object.getElements());
        }

        private Xtext2EcoreInterpretationContext visitElements(AbstractElement caller, List<AbstractElement> elementsToProcess) {
            Xtext2EcoreInterpretationContext result = deriveFeatures(context.spawnContextForGroup(), elementsToProcess);
            if (GrammarUtil.isMultipleCardinality(caller)) {
                result = deriveFeatures(result.spawnContextForGroup(), elementsToProcess);
            }
            if (GrammarUtil.isOptionalCardinality(caller)) {
                result = result.mergeSpawnedContexts(Arrays.asList(context, result));
            }
            return result;
        }

        @Override
        public Xtext2EcoreInterpretationContext caseAlternatives(Alternatives object) {
            List<Xtext2EcoreInterpretationContext> contexts = newArrayList();
            if (GrammarUtil.isOptionalCardinality(object)) {
                contexts.add(context);
            }
            for (AbstractElement alternative : object.getElements()) {
                contexts.add(deriveFeatures(context.spawnContextForGroup(), alternative));
            }
            Xtext2EcoreInterpretationContext result = context.mergeSpawnedContexts(contexts);
            if (GrammarUtil.isMultipleCardinality(object)) {
                for (AbstractElement alternative : object.getElements()) {
                    deriveFeatures(result.spawnContextForGroup(), alternative);
                }
            }
            return result;
        }

        @Override
        public Xtext2EcoreInterpretationContext caseRuleCall(RuleCall object) {
            AbstractRule calledRule = object.getRule();
            if (isWildcardFragment(calledRule)) {
                AbstractElement ruleBody = calledRule.getAlternatives();
                if (ruleBody != null) {
                    return visitElements(object, Collections.singletonList(ruleBody));
                }
                return context;
            }
            if (isParserRuleFragment(calledRule)) {
                return context;
            }
            if (!GrammarUtil.isOptionalCardinality(object)) {
                // announced during the first iteration
                if (calledRule != null && calledRule instanceof ParserRule && !GrammarUtil.isDatatypeRule((ParserRule) calledRule)) {
                    try {
                        EClassifierInfo eClassifierInfo = findOrCreateEClassifierInfo(calledRule);
                        return context.spawnContextWithCalledRule(eClassifierInfo, object);
                    } catch (TransformationException e) {
                        reportError(e);
                    }
                }
            }
            return context;
        }

        @Override
        public Xtext2EcoreInterpretationContext caseAction(Action object) {
            try {
                TypeRef actionTypeRef = object.getType();
                EClassifierInfo actionType = findOrCreateEClassifierInfo(actionTypeRef, null, true);
                EClassifierInfo currentCompatibleType = context.getCurrentCompatibleType();
                Xtext2EcoreInterpretationContext ctx = context.spawnContextWithReferencedType(actionType, object);
                if (object.getFeature() != null) {
                    ctx.addFeature(object.getFeature(), currentCompatibleType, GrammarUtil.isMultipleAssignment(object), true, object);
                }
                return ctx;
            } catch (TransformationException e) {
                reportError(e);
            }
            return context;
        }

        @Override
        public Xtext2EcoreInterpretationContext defaultCase(EObject object) {
            return context;
        }
    };
    return visitor.doSwitch(element);
}
Also used : Group(org.eclipse.xtext.Group) ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) AbstractElement(org.eclipse.xtext.AbstractElement) TypeRef(org.eclipse.xtext.TypeRef) ArrayList(java.util.ArrayList) Alternatives(org.eclipse.xtext.Alternatives) RuleCall(org.eclipse.xtext.RuleCall) Assignment(org.eclipse.xtext.Assignment) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) List(java.util.List) ArrayList(java.util.ArrayList) XtextSwitch(org.eclipse.xtext.util.XtextSwitch) CompoundElement(org.eclipse.xtext.CompoundElement) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 99 with AbstractRule

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

the class ContextPDAProvider method getFilterableRule.

protected ParserRule getFilterableRule(ISerState state) {
    if (state.getType() == SerStateType.PUSH) {
        RuleCall rc = (RuleCall) state.getGrammarElement();
        AbstractRule rule = rc.getRule();
        if (rule instanceof ParserRule) {
            ParserRule pr = (ParserRule) rule;
            if (pr.isFragment()) {
                return null;
            }
            if (pr.isDefinesHiddenTokens()) {
                return null;
            }
            return pr;
        }
    }
    return null;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 100 with AbstractRule

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

the class GrammarPDAProvider method getGrammarPDAs.

@Override
public SerializationContextMap<Pda<ISerState, RuleCall>> getGrammarPDAs(Grammar grammar) {
    RuleNames names = RuleNames.getRuleNames(grammar, true);
    RuleFilter filter = new RuleFilter();
    filter.setDiscardTerminalRules(true);
    filter.setDiscardUnreachableRules(false);
    filter.setDiscardRuleTypeRef(false);
    Grammar flattened = new FlattenedGrammarAccess(names, filter).getFlattenedGrammar();
    Builder<Pda<ISerState, RuleCall>> result = SerializationContextMap.<Pda<ISerState, RuleCall>>builder();
    for (ParserRule rule : GrammarUtil.allParserRules(flattened)) {
        RuleWithParameterValues withParams = RuleWithParameterValues.findInEmfObject(rule);
        AbstractRule original = withParams.getOriginal();
        if (original instanceof ParserRule && isValidRule((ParserRule) original)) {
            ISerializationContext context = createContext((ParserRule) original, withParams.getParamValues());
            try {
                Pda<ISerState, RuleCall> pda = createPDA(grammar, rule);
                result.put(context, pda);
            } catch (Exception e) {
                LOG.error("Error creating PDA for context '" + context + "': " + e.getMessage(), e);
            }
        }
    }
    return result.create();
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) RuleNames(org.eclipse.xtext.xtext.RuleNames) RuleWithParameterValues(org.eclipse.xtext.xtext.RuleWithParameterValues) Grammar(org.eclipse.xtext.Grammar) Pda(org.eclipse.xtext.util.formallang.Pda) FlattenedGrammarAccess(org.eclipse.xtext.xtext.FlattenedGrammarAccess) RuleCall(org.eclipse.xtext.RuleCall) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) RuleFilter(org.eclipse.xtext.xtext.RuleFilter) 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