Java Code Examples for org.eclipse.xtext.ParserRule#getAlternatives()
The following examples show how to use
org.eclipse.xtext.ParserRule#getAlternatives() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractTextEditComposerTest.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
@Test public void testMultiEdit() throws Exception { Resource res = getResource(newTestGrammar()); composer.beginRecording(res); Grammar grammar = (Grammar) res.getContents().get(0); ParserRule fooRule = (ParserRule) grammar.getRules().get(0); ParserRule barRule = (ParserRule) grammar.getRules().get(1); Alternatives fooAlternatives = (Alternatives) fooRule.getAlternatives(); barRule.setAlternatives(fooAlternatives.getElements().remove(0)); TextEdit edit = composer.endRecording(); assertTrue(edit instanceof MultiTextEdit); TextEdit[] children = ((MultiTextEdit) edit).getChildren(); assertEquals(2, children.length); assertMatches("'bar' | 'baz'", children[0]); assertMatches("Bar: 'foo' ;", children[1]); }
Example 2
Source File: XtextLinkerTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Test public void testGuardLinking() throws Exception { StringConcatenation _builder = new StringConcatenation(); _builder.append("grammar test.Lang with org.eclipse.xtext.common.Terminals"); _builder.newLine(); _builder.append("generate test \'http://test\'"); _builder.newLine(); _builder.append("Root<MyArg>: <MyArg> name=ID | <!MyArg> name=STRING;"); _builder.newLine(); final String grammarAsString = _builder.toString(); EObject _model = this.getModel(grammarAsString); final Grammar grammar = ((Grammar) _model); AbstractRule _head = IterableExtensions.<AbstractRule>head(grammar.getRules()); final ParserRule rootRule = ((ParserRule) _head); AbstractElement _alternatives = rootRule.getAlternatives(); final Alternatives alternatives = ((Alternatives) _alternatives); AbstractElement _head_1 = IterableExtensions.<AbstractElement>head(alternatives.getElements()); Condition _guardCondition = ((Group) _head_1).getGuardCondition(); final ParameterReference firstGuard = ((ParameterReference) _guardCondition); Assert.assertEquals(IterableExtensions.<Parameter>head(rootRule.getParameters()), firstGuard.getParameter()); AbstractElement _last = IterableExtensions.<AbstractElement>last(alternatives.getElements()); Condition _guardCondition_1 = ((Group) _last).getGuardCondition(); final Negation secondGuard = ((Negation) _guardCondition_1); Condition _value = secondGuard.getValue(); Assert.assertEquals(IterableExtensions.<Parameter>head(rootRule.getParameters()), ((ParameterReference) _value).getParameter()); }
Example 3
Source File: XtextValidationTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Test public void testActionAllowed_02() throws Exception { String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Model: name=ID Fragment;\n" + "fragment Fragment: {Model.prev=current};\n"; Grammar grammar = (Grammar) getModel(grammarAsText); ParserRule rule = (ParserRule) grammar.getRules().get(1); Action action = (Action) rule.getAlternatives(); XtextValidator validator = get(XtextValidator.class); ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(action, true, false); validator.setMessageAcceptor(messageAcceptor); validator.checkAssignedActionAfterAssignment(action); messageAcceptor.validate(); }
Example 4
Source File: XtextValidationTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Test public void testActionAllowed_01() throws Exception { String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Model: name=ID Fragment;\n" + "fragment Fragment: {Model};\n"; Grammar grammar = (Grammar) getModel(grammarAsText); ParserRule rule = (ParserRule) grammar.getRules().get(1); Action action = (Action) rule.getAlternatives(); XtextValidator validator = get(XtextValidator.class); ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(action, true, false); validator.setMessageAcceptor(messageAcceptor); try { validator.checkUnassignedActionAfterAssignment(action); fail(); } catch(RuntimeException e) { assertEquals("org.eclipse.xtext.validation.GuardException", e.getClass().getName()); } messageAcceptor.validate(); }
Example 5
Source File: XtextValidationTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Test public void testRuleCallAllowed_12() throws Exception { String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Model: name=ID Fragment;\n" + "fragment Fragment: Other;\n" + "fragment Other: name=ID;"; Grammar grammar = (Grammar) getModel(grammarAsText); ParserRule rule = (ParserRule) grammar.getRules().get(1); RuleCall ruleCall = (RuleCall) rule.getAlternatives(); XtextValidator validator = get(XtextValidator.class); ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, false); validator.setMessageAcceptor(messageAcceptor); validator.checkUnassignedRuleCallAllowed(ruleCall); messageAcceptor.validate(); }
Example 6
Source File: XtextValidationTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Test public void testRuleCallAllowed_08_335692() throws Exception { String grammarAsText = "grammar test with org.eclipse.xtext.common.Terminals\n" + "generate test 'http://test'\n" + "Model : value=ID FRAGMENT;\n"+ "SubModel : FRAGMENT;\n"+ "terminal fragment FRAGMENT: ';';"; Grammar grammar = (Grammar) getModel(grammarAsText); ParserRule datatypeRule = (ParserRule) grammar.getRules().get(1); RuleCall ruleCall = (RuleCall) datatypeRule.getAlternatives(); XtextValidator validator = get(XtextValidator.class); ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(ruleCall, true, false); validator.setMessageAcceptor(messageAcceptor); validator.checkUnassignedRuleCallAllowed(ruleCall); validator.checkTerminalFragmentCalledFromTerminalRule(ruleCall); messageAcceptor.validate(); }
Example 7
Source File: XtextLinkerTest.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Test public void testNamedParameterLinking() throws Exception { StringConcatenation _builder = new StringConcatenation(); _builder.append("grammar test.Lang with org.eclipse.xtext.common.Terminals"); _builder.newLine(); _builder.append("generate test \'http://test\'"); _builder.newLine(); _builder.append("Root<MyArg>: rule=Rule<MyArg>;"); _builder.newLine(); _builder.append("Rule<MyParam>: name=ID child=Root<MyArg=MyParam>?;"); _builder.newLine(); final String grammarAsString = _builder.toString(); EObject _model = this.getModel(grammarAsString); final Grammar grammar = ((Grammar) _model); AbstractRule _head = IterableExtensions.<AbstractRule>head(grammar.getRules()); final ParserRule rootRule = ((ParserRule) _head); AbstractRule _last = IterableExtensions.<AbstractRule>last(grammar.getRules()); final ParserRule lastRule = ((ParserRule) _last); AbstractElement _alternatives = lastRule.getAlternatives(); AbstractElement _last_1 = IterableExtensions.<AbstractElement>last(((Group) _alternatives).getElements()); final Assignment lastAssignment = ((Assignment) _last_1); AbstractElement _terminal = lastAssignment.getTerminal(); final RuleCall ruleCall = ((RuleCall) _terminal); final NamedArgument argument = IterableExtensions.<NamedArgument>head(ruleCall.getArguments()); Assert.assertEquals(IterableExtensions.<Parameter>head(rootRule.getParameters()), argument.getParameter()); Condition _value = argument.getValue(); Assert.assertEquals(IterableExtensions.<Parameter>head(lastRule.getParameters()), ((ParameterReference) _value).getParameter()); }
Example 8
Source File: AbstractTextEditComposerTest.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
@Test public void testObjectAddition() throws Exception { Resource res = getResource(newTestGrammar()); composer.beginRecording(res); Grammar grammar = (Grammar) res.getContents().get(0); ParserRule rule = (ParserRule) grammar.getRules().get(0); Alternatives alternatives = (Alternatives) rule.getAlternatives(); Keyword keyword = XtextFactory.eINSTANCE.createKeyword(); keyword.setValue("qux"); alternatives.getElements().add(keyword); TextEdit edit = composer.endRecording(); assertMatches("'foo' | 'bar' | 'baz' | \"qux\"", edit); }
Example 9
Source File: XtextScopingTest.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Test public void testScope_01() { ParserRule rule = (ParserRule) GrammarUtil.findRuleForName(grammar, "AbstractCallOverridenParserRule"); assertNotNull(rule); Group group = (Group) rule.getAlternatives(); Assignment ass = (Assignment) group.getElements().get(1); assertNotNull(ass); RuleCall call = (RuleCall) ass.getTerminal(); AbstractRule calledRule = call.getRule(); assertNotNull(calledRule); XtextGrammarAccess ga = (XtextGrammarAccess) get(IGrammarAccess.class); String text = get(ICrossReferenceSerializer.class).serializeCrossRef(call, ga.getRuleCallAccess().getRuleAbstractRuleCrossReference_0_0(), calledRule, null); assertNotNull(text); assertEquals(calledRule.getName(), text); }
Example 10
Source File: XtextLinkerTest.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Test public void testImplicitNamedParameterLinking_01() throws Exception { StringConcatenation _builder = new StringConcatenation(); _builder.append("grammar test.Lang with org.eclipse.xtext.common.Terminals"); _builder.newLine(); _builder.append("generate test \'http://test\'"); _builder.newLine(); _builder.append("Root<MyParam>: rule=Rule<MyParam>;"); _builder.newLine(); _builder.append("Rule<MyParam>: name=ID child=Root<MyParam>?;"); _builder.newLine(); final String grammarAsString = _builder.toString(); EObject _model = this.getModel(grammarAsString); final Grammar grammar = ((Grammar) _model); AbstractRule _head = IterableExtensions.<AbstractRule>head(grammar.getRules()); final ParserRule rootRule = ((ParserRule) _head); AbstractRule _last = IterableExtensions.<AbstractRule>last(grammar.getRules()); final ParserRule lastRule = ((ParserRule) _last); AbstractElement _alternatives = lastRule.getAlternatives(); AbstractElement _last_1 = IterableExtensions.<AbstractElement>last(((Group) _alternatives).getElements()); final Assignment lastAssignment = ((Assignment) _last_1); AbstractElement _terminal = lastAssignment.getTerminal(); final RuleCall ruleCall = ((RuleCall) _terminal); final NamedArgument argument = IterableExtensions.<NamedArgument>head(ruleCall.getArguments()); Assert.assertEquals(IterableExtensions.<Parameter>head(rootRule.getParameters()), argument.getParameter()); Condition _value = argument.getValue(); Assert.assertEquals(IterableExtensions.<Parameter>head(lastRule.getParameters()), ((ParameterReference) _value).getParameter()); }
Example 11
Source File: ValidEntryRuleInspector.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Override protected boolean canInspect(ParserRule rule) { if (GrammarUtil.getGrammar(rule).getRules().get(0) != rule) return false; if (GrammarUtil.isDatatypeRule(rule) || rule.getAlternatives() == null) return false; return super.canInspect(rule); }
Example 12
Source File: UsedRulesFinder.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Override public Void caseParserRule(ParserRule object) { if (usedRules.add(object)) { if (object.getAlternatives() != null) doSwitch(object.getAlternatives()); if (object.isDefinesHiddenTokens()) for(AbstractRule rule: object.getHiddenTokens()) doSwitch(rule); } return null; }
Example 13
Source File: RuleWithoutInstantiationInspector.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Override protected boolean canInspect(ParserRule rule) { // special treatment of first rule if (GrammarUtil.getGrammar(rule).getRules().get(0) == rule) return false; if (GrammarUtil.isDatatypeRule(rule) || rule.getAlternatives() == null || rule.isFragment()) return false; return super.canInspect(rule); }
Example 14
Source File: CurrentTypeFinder.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Override public Boolean caseParserRule(ParserRule object) { if (visiting.add(object)) { try { if (object.getAlternatives() != null) return doSwitch(object.getAlternatives()); } finally { visiting.remove(object); } } return true; }
Example 15
Source File: Xtext2EcoreTransformer.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
private void deriveFeatures(ParserRule rule) throws TransformationException { EClassifierInfo classInfo = findEClassifierInfo(rule); if (classInfo == null) throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "No type available for rule " + rule.getName(), rule); Xtext2EcoreInterpretationContext context = new Xtext2EcoreInterpretationContext(eClassifierInfos, classInfo); if (rule.getAlternatives() != null) // might happen due to syntax errors in the document deriveFeatures(context, rule.getAlternatives()); }
Example 16
Source File: DatatypeRuleUtil.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@Override public Boolean caseParserRule(ParserRule object) { if (visitedRules.isEmpty()) { visitedRules.add(object); return object.getAlternatives() != null && doSwitch(object.getAlternatives()); } final TypeRef typeRef = object.getType(); if (typeRef == null || typeRef.getClassifier() == null) { throw new IllegalStateException("checks are only allowed for linked grammars. Rule: " + object.getName()); } if (!visitedRules.add(object)) return true; Boolean result = GrammarUtil.isDatatypeRule(object); return result; }
Example 17
Source File: DatatypeRuleUtil.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
/** * If a rule is visited reentrant, it is is shallow-checked for syntax * elements that indicate a production rule rather than a data type rule. */ protected Boolean getApproximatedResult(ParserRule object) { if (object.getAlternatives() == null) return true; Iterator<?> contents = EcoreUtil.getAllContents(object.getAlternatives(), true); return !Iterators.filter(contents, new Predicate<Object>() { @Override public boolean apply(Object input) { return input instanceof Assignment || input instanceof Action; } }).hasNext(); }
Example 18
Source File: OverriddenValueInspector.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
@Override protected boolean canInspect(ParserRule rule) { if (GrammarUtil.isDatatypeRule(rule) || rule.getAlternatives() == null) return false; return super.canInspect(rule); }
Example 19
Source File: XtextLinkerTest.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
@Test public void testNamedParameterAdjustment() throws Exception { StringConcatenation _builder = new StringConcatenation(); _builder.append("grammar test.Lang with org.eclipse.xtext.common.Terminals"); _builder.newLine(); _builder.append("generate test \'http://test\'"); _builder.newLine(); _builder.append("Root<MyParam>: rule=Rule<true>;"); _builder.newLine(); _builder.append("Rule<MyParam>: name=ID child=Root<false>?;"); _builder.newLine(); final String grammarAsString = _builder.toString(); EObject _model = this.getModel(grammarAsString); final Grammar grammar = ((Grammar) _model); final ResourceSet resourceSet = grammar.eResource().getResourceSet(); final Resource otherResource = resourceSet.createResource(URI.createURI("other.xtext")); StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("grammar test.SubLang with test.Lang"); _builder_1.newLine(); _builder_1.append("import \'http://test\'"); _builder_1.newLine(); _builder_1.append("Root<MyParam>: rule=super::Rule<true>;"); _builder_1.newLine(); LazyStringInputStream _lazyStringInputStream = new LazyStringInputStream(_builder_1.toString()); otherResource.load(_lazyStringInputStream, null); EObject _head = IterableExtensions.<EObject>head(otherResource.getContents()); final Grammar subGrammar = ((Grammar) _head); AbstractRule _head_1 = IterableExtensions.<AbstractRule>head(subGrammar.getRules()); final ParserRule rootRule = ((ParserRule) _head_1); AbstractRule _last = IterableExtensions.<AbstractRule>last(grammar.getRules()); final ParserRule parentRule = ((ParserRule) _last); AbstractElement _alternatives = parentRule.getAlternatives(); AbstractElement _last_1 = IterableExtensions.<AbstractElement>last(((Group) _alternatives).getElements()); final Assignment lastAssignment = ((Assignment) _last_1); AbstractElement _terminal = lastAssignment.getTerminal(); final RuleCall ruleCall = ((RuleCall) _terminal); final NamedArgument argument = IterableExtensions.<NamedArgument>head(ruleCall.getArguments()); Assert.assertEquals(IterableExtensions.<Parameter>head(rootRule.getParameters()), argument.getParameter()); Condition _value = argument.getValue(); Assert.assertFalse(((LiteralCondition) _value).isTrue()); }
Example 20
Source File: Xtext2EcoreTransformer.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
private boolean deriveTypeHierarchy() { boolean result = true; for (AbstractRule rule : grammar.getRules()) { try { EClassifierInfo generatedEClass = findOrCreateEClassifierInfo(rule); if (generatedEClass != null || !isWildcardFragment(rule)) { if (rule instanceof ParserRule) { ParserRule parserRule = (ParserRule) rule; if (parserRule.getAlternatives() != null) { if (!GrammarUtil.isDatatypeRule(parserRule)) { deriveTypesAndHierarchy(parserRule, generatedEClass, parserRule.getAlternatives()); } else { checkSupertypeOfOverriddenDatatypeRule(rule); } } } else if (rule instanceof TerminalRule) { if (rule.getType() != null) { if (!(rule.getType().getClassifier() instanceof EDataType)) throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Return type of a terminal rule must be an EDataType.", rule.getType()); checkSupertypeOfOverriddenDatatypeRule(rule); } } else if (rule instanceof EnumRule) { if (rule.getType() != null) { if (!(rule.getType().getClassifier() instanceof EEnum)) throw new TransformationException(TransformationErrorCode.NoSuchTypeAvailable, "Return type of an enum rule must be an EEnum.", rule.getType()); checkSupertypeOfOverriddenDatatypeRule(rule); } } else { throw new IllegalStateException("Unknown rule type: " + rule.eClass().getName()); } } if (isWildcardFragment(rule)) { for (Grammar usedGrammar: grammar.getUsedGrammars()) { deriveTypeHierarchyFromOverridden((ParserRule) rule, usedGrammar); } } } catch (TransformationException e) { reportError(e); result = false; } } return result; }