Java Code Examples for org.eclipse.xtext.ParserRule

The following examples show how to use org.eclipse.xtext.ParserRule. These examples are extracted from open source projects. 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 Project: xtext-core   Source File: NodeModelUtils.java    License: Eclipse Public License 2.0 6 votes vote down vote up
public static ParserRule getEntryParserRule(INode node) {
	ICompositeNode root = node.getRootNode();
	EObject ge1 = root.getGrammarElement();
	if (ge1 instanceof ParserRule) {
		return (ParserRule) ge1;
	} else if (ge1 instanceof Action) {
		INode firstChild = root.getFirstChild();
		while (firstChild.getGrammarElement() instanceof Action && firstChild instanceof CompositeNode) {
			firstChild = ((CompositeNode)firstChild).getFirstChild();
		}
		EObject ge2 = firstChild.getGrammarElement();
		if (ge2 instanceof ParserRule) {
			return (ParserRule) ge2;
		}
	}
	throw new IllegalStateException("No Root Parser Rule found; The Node Model is broken.");
}
 
Example 2
@Test
public void test1() throws Exception {
	with(ReferenceGrammarTestLanguageStandaloneSetup.class);
	String model = "kind (Hugo 13)";
	ParserRule kindRule = get(ReferenceGrammarTestLanguageGrammarAccess.class).getKindRule();
	XtextResource resource = createResource();
	// test 1: parse and assume there are no errors
	resource.setEntryPoint(kindRule);
	resource.load(new StringInputStream(model), Collections.emptyMap());
	Assert.assertTrue(resource.getErrors().isEmpty());
	Assert.assertEquals(kindRule, NodeModelUtils.getEntryParserRule(resource.getParseResult().getRootNode()));
	// test 2: update and assume node model does not change
	String originalNodeModel = NodeModelUtils.compactDump(resource.getParseResult().getRootNode(), false);
	resource.update(0, model.length(), " " + model + " ");
	String reparsedNodeModel = NodeModelUtils.compactDump(resource.getParseResult().getRootNode(), false);
	Assert.assertEquals(originalNodeModel, reparsedNodeModel);
	// test 3: change parser rule
	ParserRule erwachsenerRule = get(ReferenceGrammarTestLanguageGrammarAccess.class).getErwachsenerRule();
	resource.setEntryPoint(erwachsenerRule);
	resource.update(0, model.length(), "erwachsener (Peter 30)");
	Assert.assertEquals(erwachsenerRule,
			NodeModelUtils.getEntryParserRule(resource.getParseResult().getRootNode()));
}
 
Example 3
@Test
public void testBug306281_10() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("grammar org.foo with org.eclipse.xtext.common.Terminals");
  _builder.newLine();
  _builder.append("generate metamodel \'foo.sample\'");
  _builder.newLine();
  _builder.append("Model : name=ID (({Binary.left=current} operator = [Model] | {Binary.left=current} operator = [Model]) name=ID);");
  _builder.newLine();
  String grammarAsString = _builder.toString();
  final Grammar grammar = this.getGrammar(grammarAsString);
  AbstractRule _findRuleForName = GrammarUtil.findRuleForName(grammar, "Model");
  final ParserRule rule = ((ParserRule) _findRuleForName);
  this.validateRule(rule);
  Assert.assertTrue(this.warnings.toString(), this.warnings.isEmpty());
}
 
Example 4
private String parseRule(String body) throws Exception {
	Grammar grammar = (Grammar) getModel(HEADER + body);
	Matcher m = Pattern.compile("^[a-zA-Z]+").matcher(body);
	m.find();
	String firstRuleName = m.group();
	AbstractRule firstRule = GrammarUtil.findRuleForName(grammar, firstRuleName);
	IConcreteSyntaxConstraintProvider cscp = new CSCPTest(grammar);
	return toString(cscp.getConstraint((ParserRule) firstRule));
}
 
Example 5
@Test public void testAssignment_02() throws Exception {
	String grammarAsString = "grammar org.foo with org.eclipse.xtext.common.Terminals\n" +
			"generate metamodel 'foo.sample'\n" +
			"Model: x=X;\n" +
			"X : (x+='x')+;";
	Grammar grammar = getGrammar(grammarAsString);
	ParserRule rule = (ParserRule) GrammarUtil.findRuleForName(grammar, "X");
	validateRule(rule);
	assertTrue(warnings.toString(), warnings.isEmpty());
}
 
Example 6
protected CharSequence compileRestoreHiddenTokens(final AbstractRule it, final AntlrOptions options) {
  if (it instanceof ParserRule) {
    return _compileRestoreHiddenTokens((ParserRule)it, options);
  } else if (it != null) {
    return _compileRestoreHiddenTokens(it, options);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, options).toString());
  }
}
 
Example 7
@Test public void testGetStyledTextFallbackText() throws Exception {
	DefaultEObjectLabelProvider defaultLabelProvider = new DefaultEObjectLabelProvider();
	ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
	parserRule.setName("testCreateStyledString");
	StyledString styledText = defaultLabelProvider.getStyledText(parserRule);
	assertEquals("testCreateStyledString", styledText.getString());
}
 
Example 8
Source Project: xtext-core   Source File: XtextValidationTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testCheckRuleCallInUnorderedGroup_02() throws Exception {
	XtextValidator validator = get(XtextValidator.class);
	UnorderedGroup unorderedGroup = XtextFactory.eINSTANCE.createUnorderedGroup();
	RuleCall ruleCall = XtextFactory.eINSTANCE.createRuleCall();
	TypeRef typeRef = XtextFactory.eINSTANCE.createTypeRef();
	typeRef.setClassifier(EcorePackage.Literals.EBIG_DECIMAL);
	ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
	parserRule.setType(typeRef);
	ruleCall.setRule(parserRule);
	unorderedGroup.getElements().add(ruleCall);
	ValidatingMessageAcceptor messageAcceptor = new ValidatingMessageAcceptor(null, false, false);
	validator.setMessageAcceptor(messageAcceptor);
	validator.checkRuleCallInUnorderedGroup(ruleCall);
	messageAcceptor.validate();
}
 
Example 9
Source Project: xtext-core   Source File: GrammarTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testRuleModelId() {
	AbstractRule rule = grammar.getRules().get(2);
	assertEquals("ModelId", rule.getName());
	ParserRule parserRule = (ParserRule) rule;
	assertTrue(GrammarUtil.isDatatypeRule(parserRule));
	assertNotNull(rule.getType());
	assertNotNull(rule.getType().getClassifier());
	assertEquals(EcorePackage.Literals.ESTRING, rule.getType().getClassifier());
}
 
Example 10
@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 11
public ParserRule getArrayBracketsRule() {
	return getArrayBracketsAccess().getRule();
}
 
Example 12
public ParserRule getMyIntRule() {
	return getMyIntAccess().getRule();
}
 
Example 13
public ParserRule getPostopExpressionRule() {
	return getPostopExpressionAccess().getRule();
}
 
Example 14
Source Project: xtext-core   Source File: XtextGrammarAccess.java    License: Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getAbstractTokenRule() {
	return getAbstractTokenAccess().getRule();
}
 
Example 15
public ParserRule getXAdditiveExpressionRule() {
	return getXAdditiveExpressionAccess().getRule();
}
 
Example 16
Source Project: xtext-extras   Source File: AntlrGrammarGenUtil.java    License: Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.9
 */
public static String getParameterList(ParserRule rule, Boolean skipCurrent) {
	return org.eclipse.xtext.xtext.generator.parser.antlr.AntlrGrammarGenUtil.getParameterList(rule, skipCurrent, "EObject");
}
 
Example 17
public ParserRule getConditionalBranchRule() {
	return getConditionalBranchAccess().getRule();
}
 
Example 18
public ParserRule getElementRule() {
	return getElementAccess().getRule();
}
 
Example 19
public ParserRule getXMemberFeatureCallRule() {
	return getXMemberFeatureCallAccess().getRule();
}
 
Example 20
Source Project: xtext-web   Source File: EntitiesGrammarAccess.java    License: Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getXBooleanLiteralRule() {
	return getXBooleanLiteralAccess().getRule();
}
 
Example 21
public ParserRule getPolygonBasedShapeRule() {
	return getPolygonBasedShapeAccess().getRule();
}
 
Example 22
public ParserRule getArrayBracketsRule() {
	return getArrayBracketsAccess().getRule();
}
 
Example 23
Source Project: xtext-web   Source File: EntitiesGrammarAccess.java    License: Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getOpEqualityRule() {
	return getOpEqualityAccess().getRule();
}
 
Example 24
Source Project: xtext-web   Source File: EntitiesGrammarAccess.java    License: Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getOpCompareRule() {
	return getOpCompareAccess().getRule();
}
 
Example 25
Source Project: xtext-core   Source File: Xtext2EcoreTransformer.java    License: Eclipse Public License 2.0 4 votes vote down vote up
private boolean deriveTypeHierarchyFromOverridden(ParserRule rule, Grammar grammar) throws TransformationException {
	AbstractRule parentRule = GrammarUtil.findRuleForName(grammar, rule.getName());
	if (parentRule != null) {
		if (parentRule != rule && parentRule instanceof ParserRule) {
			ParserRule casted = (ParserRule) parentRule;
			if (casted.isFragment() != rule.isFragment()) {
				if (rule.isFragment()) {
					throw new TransformationException(TransformationErrorCode.InvalidFragmentOverride,
							"A fragment rule cannot override a production rule.", rule);
				} else {
					throw new TransformationException(TransformationErrorCode.InvalidFragmentOverride,
							"Only fragment rule can override other fragment rules.", rule);
				}
			}
			if (casted.isWildcard() != rule.isWildcard()) {
				if (rule.isWildcard()) {
					throw new TransformationException(TransformationErrorCode.InvalidFragmentOverride,
							"A wildcard fragment rule cannot override a typed fragment rule.", rule);
				} else {
					throw new TransformationException(TransformationErrorCode.InvalidFragmentOverride,
							"Only wildcard fragment rules can override other wildcard fragments.", rule);
				}
			}
			if (rule.isFragment() && !rule.isWildcard() && parentRule.getType() != null) {
				if (rule.getType().getClassifier() != parentRule.getType().getClassifier()) {
					throw new TransformationException(TransformationErrorCode.InvalidFragmentOverride,
							"Overriding fragment rules cannot redeclare their type.", rule.getType());
				}
			}
			checkParameterLists(rule, casted);
		}
		if (parentRule.getType() != null && parentRule != rule) {			
			if (parentRule.getType().getClassifier() instanceof EDataType)
				throw new TransformationException(TransformationErrorCode.InvalidSupertype,
						"Cannot inherit from datatype rule and return another type.", rule.getType());
			EClassifierInfo parentTypeInfo = eClassifierInfos.getInfoOrNull(parentRule.getType());
			if (parentTypeInfo == null)
				throw new TransformationException(TransformationErrorCode.InvalidSupertype,
						"Cannot determine return type of overridden rule.", rule.getType());
			addSuperType(rule, rule.getType(), parentTypeInfo);
			return true;
		}
	}
	return false;
}
 
Example 26
public ParserRule getXNullLiteralRule() {
	return getXNullLiteralAccess().getRule();
}
 
Example 27
public ParserRule getPrefixedDelegateRule() {
	return getPrefixedDelegateAccess().getRule();
}
 
Example 28
public ParserRule getTimeDef3Rule() {
	return getTimeDef3Access().getRule();
}
 
Example 29
public ParserRule getXImportDeclarationRule() {
	return getXImportDeclarationAccess().getRule();
}
 
Example 30
Source Project: n4js   Source File: JSONGrammarAccess.java    License: Eclipse Public License 1.0 4 votes vote down vote up
public ParserRule getJSONDocumentRule() {
	return getJSONDocumentAccess().getRule();
}