Java Code Examples for org.eclipse.xtext.TerminalRule#getAlternatives()

The following examples show how to use org.eclipse.xtext.TerminalRule#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: KeywordInspector.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public void inspectKeywordHidesTerminalRule(Keyword keyword) {
	AbstractRule container = GrammarUtil.containingRule(keyword);
	if (container instanceof TerminalRule)
		return;
	Grammar grammar = GrammarUtil.getGrammar(container);
	List<TerminalRule> rules = cache.get(KeywordInspector.class, grammar.eResource(), ()->GrammarUtil.allTerminalRules(grammar));
	for(TerminalRule rule: rules) {
		if (!rule.isFragment()) {
			AbstractElement element = rule.getAlternatives();
			if (element instanceof Keyword && Strings.isEmpty(element.getCardinality())) {
				String value = ((Keyword) element).getValue();
				if (value.equals(keyword.getValue()))
					acceptor.acceptError(
						"The keyword '" + value + "' hides the terminal rule " + rule.getName()+ ".", 
						keyword,
						XtextPackage.Literals.KEYWORD__VALUE,
						ValidationMessageAcceptor.INSIGNIFICANT_INDEX, null);
			}
		}
	}
}
 
Example 2
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testRuleCallAllowed_01() throws Exception {
	String grammarAsText =
		"grammar test with org.eclipse.xtext.common.Terminals\n" +
		"generate test 'http://test'\n" +
		"Model: name=ID;\n"+
		"terminal MyTerminal: Fragment;\n"+
		"terminal fragment Fragment: 'a'..'z'+;";

	Grammar grammar = (Grammar) getModel(grammarAsText);
	TerminalRule rule = (TerminalRule) 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);
	validator.checkTerminalFragmentCalledFromTerminalRule(ruleCall);
	messageAcceptor.validate();
}
 
Example 3
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testRuleCallAllowed_02() throws Exception {
	String grammarAsText =
		"grammar test with org.eclipse.xtext.common.Terminals\n" +
		"generate test 'http://test'\n" +
		"Model: name=ID;\n"+
		"terminal fragment FirstFragment: SecondFragment;\n"+
		"terminal fragment SecondFragment: 'a'..'z'+;";

	Grammar grammar = (Grammar) getModel(grammarAsText);
	TerminalRule rule = (TerminalRule) 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);
	validator.checkTerminalFragmentCalledFromTerminalRule(ruleCall);
	messageAcceptor.validate();
}
 
Example 4
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testRuleCallAllowed_03() throws Exception {
	String grammarAsText =
		"grammar test with org.eclipse.xtext.common.Terminals\n" +
		"generate test 'http://test'\n" +
		"Model: name=ID;\n"+
		"terminal fragment Fragment: Terminal;\n"+
		"terminal Terminal: 'a'..'z'+;";

	Grammar grammar = (Grammar) getModel(grammarAsText);
	TerminalRule rule = (TerminalRule) 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);
	validator.checkTerminalFragmentCalledFromTerminalRule(ruleCall);
	messageAcceptor.validate();
}
 
Example 5
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testRuleCallAllowed_06() throws Exception {
	String grammarAsText =
		"grammar test with org.eclipse.xtext.common.Terminals\n" +
		"generate test 'http://test'\n" +
		"Model: name=Terminal;\n"+
		"terminal Terminal: Enum;\n" +
		"enum Enum: A | B;";

	Grammar grammar = (Grammar) getModel(grammarAsText);
	TerminalRule rule = (TerminalRule) grammar.getRules().get(1);
	RuleCall ruleCall = (RuleCall) rule.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 6
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testRuleCallAllowed_07() throws Exception {
	String grammarAsText =
		"grammar test with org.eclipse.xtext.common.Terminals\n" +
		"generate test 'http://test'\n" +
		"Model: name=ID;\n"+
		"terminal Terminal: Model;";

	Grammar grammar = (Grammar) getModel(grammarAsText);
	TerminalRule rule = (TerminalRule) grammar.getRules().get(1);
	RuleCall ruleCall = (RuleCall) rule.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: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testNegatedTokenNotEOF() throws Exception {
	String grammarAsText =
			"grammar test with org.eclipse.xtext.common.Terminals\n" +
			"generate test 'http://test'\n" +
			"A: foo=DUMMY;\n" +
			"terminal DUMMY: !EOF;";
	Grammar grammar = (Grammar) getModel(grammarAsText);
	XtextValidator validator = get(XtextValidator.class);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false);
	TerminalRule terminal = (TerminalRule) grammar.getRules().get(1);
	NegatedToken token = (NegatedToken)terminal.getAlternatives();
	messageAcceptor.expectedContext(token.getTerminal());
	configureValidator(validator, messageAcceptor, token);
	validator.checkNegatedTokenNotEOF(token);
	messageAcceptor.validate();
}
 
Example 8
Source File: XtextValidationTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testNegatedTokenNotEOF_2() throws Exception {
	String grammarAsText =
			"grammar test with org.eclipse.xtext.common.Terminals\n" +
					"generate test 'http://test'\n" +
					"A: foo=DUMMY;\n" +
					"terminal DUMMY: !(EOF | ID);";
	Grammar grammar = (Grammar) getModel(grammarAsText);
	XtextValidator validator = get(XtextValidator.class);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, true, false);
	TerminalRule terminal = (TerminalRule) grammar.getRules().get(1);
	NegatedToken token = (NegatedToken)terminal.getAlternatives();
	messageAcceptor.expectedContext(((Alternatives)token.getTerminal()).getElements().get(0));
	configureValidator(validator, messageAcceptor, token);
	validator.checkNegatedTokenNotEOF(token);
	messageAcceptor.validate();
}
 
Example 9
Source File: XtextLinkerTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testQualifiedRuleCall_02() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("grammar test with org.eclipse.xtext.common.Terminals");
  _builder.newLine();
  _builder.append("generate test \'http://test\'");
  _builder.newLine();
  _builder.append("Rule: name=ID;");
  _builder.newLine();
  _builder.append("terminal STRING: super;");
  _builder.newLine();
  final String grammarAsString = _builder.toString();
  final XtextResource resource = this.getResourceFromString(grammarAsString);
  EObject _get = resource.getContents().get(0);
  Grammar grammar = ((Grammar) _get);
  AbstractRule _get_1 = grammar.getRules().get(1);
  final TerminalRule string = ((TerminalRule) _get_1);
  AbstractElement _alternatives = string.getAlternatives();
  final RuleCall callToSuper = ((RuleCall) _alternatives);
  Assert.assertEquals(GrammarUtil.findRuleForName(IterableExtensions.<Grammar>head(grammar.getUsedGrammars()), "STRING"), callToSuper.getRule());
}
 
Example 10
Source File: XtextLinkerTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testQualifiedRuleCall_03() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("grammar test with org.eclipse.xtext.common.Terminals");
  _builder.newLine();
  _builder.append("generate test \'http://test\'");
  _builder.newLine();
  _builder.append("Rule: name=ID;");
  _builder.newLine();
  _builder.append("terminal STRING: super;");
  _builder.newLine();
  _builder.append("terminal super: \'super\';");
  _builder.newLine();
  final String grammarAsString = _builder.toString();
  final XtextResource resource = this.getResourceFromString(grammarAsString);
  EObject _get = resource.getContents().get(0);
  Grammar grammar = ((Grammar) _get);
  AbstractRule _get_1 = grammar.getRules().get(1);
  final TerminalRule string = ((TerminalRule) _get_1);
  AbstractElement _alternatives = string.getAlternatives();
  final RuleCall callToSuper = ((RuleCall) _alternatives);
  Assert.assertEquals(IterableExtensions.<AbstractRule>last(grammar.getRules()), callToSuper.getRule());
}
 
Example 11
Source File: UsedRulesFinder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Void caseTerminalRule(TerminalRule object) {
	if (usedRules.add(object) && object.getAlternatives() != null) {
		return doSwitch(object.getAlternatives());
	}
	return null;
}
 
Example 12
Source File: SyntheticTerminalDetector.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/** 
 * Answers {@code true} if the given terminal rule is synthetic. That is,
 * the tokens for this rule will not be produced by the generated Antlr lexer
 * but manually in a custom token source.
 */
public boolean isSyntheticTerminalRule(TerminalRule rule) {
	if (rule.getAlternatives() instanceof Keyword) {
		String value = ((Keyword) rule.getAlternatives()).getValue();
		String sytheticValue = "synthetic:" + AntlrGrammarGenUtil.getOriginalElement(rule).getName();
		return Objects.equal(sytheticValue, value);
	}
	return false;
}