org.eclipse.xtext.ParserRule Java Examples

The following examples show how to use org.eclipse.xtext.ParserRule. 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: OverriddenValueInspectorTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@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 #2
Source File: SetEntryPointOnXtextResourceTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@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
Source File: NodeModelUtils.java    From xtext-core with 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 #4
Source File: RuleWithoutInstantiationInspectorTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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 #5
Source File: ConcreteSyntaxConstraintProviderTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
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 #6
Source File: ValidEntryRuleInspector.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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 #7
Source File: DefaultEObjectLabelProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@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 File: GrammarTest.java    From xtext-core with 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 #9
Source File: AbstractAntlrGrammarWithActionsGenerator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
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 #10
Source File: XtextValidationTest.java    From xtext-core with 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 #11
Source File: AntlrGrammarGenerator.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected String crossrefEbnf(final AbstractRule it, final RuleCall call, final CrossReference ref, final boolean supportActions) {
  String _xifexpression = null;
  if (supportActions) {
    String _switchResult = null;
    boolean _matched = false;
    if (it instanceof EnumRule) {
      _matched=true;
    }
    if (!_matched) {
      if (it instanceof ParserRule) {
        _matched=true;
      }
    }
    if (_matched) {
      StringConcatenation _builder = new StringConcatenation();
      _builder.append("{");
      _builder.newLine();
      _builder.append("\t");
      CharSequence _newCompositeNode = this.newCompositeNode(ref);
      _builder.append(_newCompositeNode, "\t");
      _builder.newLineIfNotEmpty();
      _builder.append("}");
      _builder.newLine();
      String _ruleName = this._grammarAccessExtensions.ruleName(it);
      _builder.append(_ruleName);
      String _argumentList = AntlrGrammarGenUtil.getArgumentList(call, this.isPassCurrentIntoFragment(), (!supportActions));
      _builder.append(_argumentList);
      _builder.newLineIfNotEmpty();
      _builder.append("{");
      _builder.newLine();
      _builder.append("\t");
      _builder.append("afterParserOrEnumRuleCall();");
      _builder.newLine();
      _builder.append("}");
      _builder.newLine();
      _switchResult = _builder.toString();
    }
    if (!_matched) {
      if (it instanceof TerminalRule) {
        _matched=true;
        StringConcatenation _builder_1 = new StringConcatenation();
        String _localVar = this._grammarAccessExtensions.localVar(GrammarUtil.containingAssignment(ref));
        _builder_1.append(_localVar);
        _builder_1.append("=");
        String _ruleName_1 = this._grammarAccessExtensions.ruleName(it);
        _builder_1.append(_ruleName_1);
        _builder_1.newLineIfNotEmpty();
        _builder_1.append("{");
        _builder_1.newLine();
        _builder_1.append("\t");
        CharSequence _newLeafNode = this.newLeafNode(ref, this._grammarAccessExtensions.localVar(GrammarUtil.containingAssignment(ref)));
        _builder_1.append(_newLeafNode, "\t");
        _builder_1.newLineIfNotEmpty();
        _builder_1.append("}");
        _builder_1.newLine();
        _switchResult = _builder_1.toString();
      }
    }
    if (!_matched) {
      throw new IllegalStateException(("crossrefEbnf is not supported for " + it));
    }
    _xifexpression = _switchResult;
  } else {
    _xifexpression = super.crossrefEbnf(it, call, ref, supportActions);
  }
  return _xifexpression;
}
 
Example #12
Source File: Bug347012TestLanguageGrammarAccess.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getMyClassRule() {
	return getMyClassAccess().getRule();
}
 
Example #13
Source File: HiddenTerminalsTestLanguageGrammarAccess.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getDatatypeRuleRule() {
	return getDatatypeRuleAccess().getRule();
}
 
Example #14
Source File: TwoParametersTestLanguageGrammarAccess.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getScenario8Rule() {
	return getScenario8Access().getRule();
}
 
Example #15
Source File: DomainModelTestLanguageGrammarAccess.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getDataTypeRule() {
	return getDataTypeAccess().getRule();
}
 
Example #16
Source File: PureXbaseGrammarAccess.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getQualifiedNameRule() {
	return getQualifiedNameAccess().getRule();
}
 
Example #17
Source File: TwoParametersTestLanguageGrammarAccess.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getScenario2Rule() {
	return getScenario2Access().getRule();
}
 
Example #18
Source File: XbaseWithAnnotationsGrammarAccess.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getXAnnotationRule() {
	return getXAnnotationAccess().getRule();
}
 
Example #19
Source File: IndentationAwareTestLanguageGrammarAccess.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getTreeNodeRule() {
	return getTreeNodeAccess().getRule();
}
 
Example #20
Source File: RegularExpressionGrammarAccess.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
public ParserRule getEscapedCharacterClassAtomRule() {
	return getEscapedCharacterClassAtomAccess().getRule();
}
 
Example #21
Source File: XImportSectionTestLangGrammarAccess.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getXCastedExpressionRule() {
	return getXCastedExpressionAccess().getRule();
}
 
Example #22
Source File: Bug303200TestLanguageGrammarAccess.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getListExpressionRule() {
	return getListExpressionAccess().getRule();
}
 
Example #23
Source File: GH1462TestLanguageGrammarAccess.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getRootRule() {
	return getRootAccess().getRule();
}
 
Example #24
Source File: Bug309949TestLanguageGrammarAccess.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getError_1Rule() {
	return getError_1Access().getRule();
}
 
Example #25
Source File: SerializationErrorTestLanguageGrammarAccess.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getIndentRule() {
	return getIndentAccess().getRule();
}
 
Example #26
Source File: EntitiesGrammarAccess.java    From xtext-web with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getJvmLowerBoundRule() {
	return getJvmLowerBoundAccess().getRule();
}
 
Example #27
Source File: Bug302128TestLanguageGrammarAccess.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getModelRule() {
	return getModelAccess().getRule();
}
 
Example #28
Source File: TestLanguageGrammarAccess.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getReducibleRuleRule() {
	return getReducibleRuleAccess().getRule();
}
 
Example #29
Source File: Ecore2XtextTestGrammarAccess.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getINT0Rule() {
	return getINT0Access().getRule();
}
 
Example #30
Source File: Bug462047LangGrammarAccess.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public ParserRule getOpOrRule() {
	return getOpOrAccess().getRule();
}