Search in sources :

Example 16 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-core by eclipse.

the class DynamicChannelTest method testParseSimpleLanguage.

@Test
public void testParseSimpleLanguage() throws Exception {
    IParseResult result = parse(model);
    assertNotNull(result);
    assertNotNull(result.getRootASTElement());
    assertNotNull(result.getRootNode());
    assertNotNull(result.getSyntaxErrors());
    assertFalse(result.hasSyntaxErrors());
    assertTrue(result.getSyntaxErrors().toString(), Iterables.isEmpty(result.getSyntaxErrors()));
}
Also used : IParseResult(org.eclipse.xtext.parser.IParseResult) Test(org.junit.Test)

Example 17 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project xtext-core by eclipse.

the class DynamicChannelTest method testNodeModelOfSimpleLanguage.

@Test
public void testNodeModelOfSimpleLanguage() throws Exception {
    IParseResult result = parse(model);
    List<ILeafNode> leafs = Lists.newArrayList(result.getRootNode().getLeafNodes());
    assertEquals(23, leafs.size());
    int i = 0;
    checkLeaf(leafs.get(i++), grammar, false);
    checkLeaf(leafs.get(i++), space, true);
    checkLeaf(leafs.get(i++), a, false);
    checkLeaf(leafs.get(i++), space, true);
    checkLeaf(leafs.get(i++), ext, false);
    checkLeaf(leafs.get(i++), space, true);
    checkLeaf(leafs.get(i++), b, false);
    checkLeaf(leafs.get(i++), dot, false);
    checkLeaf(leafs.get(i++), space, true);
    checkLeaf(leafs.get(i++), c, false);
    checkLeaf(leafs.get(i++), lb, true);
    checkLeaf(leafs.get(i++), comment, true);
    checkLeaf(leafs.get(i++), rulename, false);
    checkLeaf(leafs.get(i++), space + space, true);
    checkLeaf(leafs.get(i++), returns, false);
    checkLeaf(leafs.get(i++), space + lb, true);
    checkLeaf(leafs.get(i++), d, false);
    checkLeaf(leafs.get(i++), colon, false);
    checkLeaf(leafs.get(i++), space, true);
    checkLeaf(leafs.get(i++), name, false);
    checkLeaf(leafs.get(i++), ass, false);
    checkLeaf(leafs.get(i++), id, false);
    checkLeaf(leafs.get(i++), sc, false);
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) IParseResult(org.eclipse.xtext.parser.IParseResult) Test(org.junit.Test)

Example 18 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project n4js by eclipse.

the class HighlightingParserTester method getTokens.

/**
 * Parses the input and returns a list of lexer tokens. Asserts that the produced tokens are equal to the tokens
 * that the production parser produced.
 *
 * @return the tokens for the highlighting.
 */
public List<Token> getTokens(CharSequence input) {
    List<Token> result;
    IParseResult parseResult = parser.parse(new StringReader(input.toString()));
    if (!parseResult.hasSyntaxErrors()) {
        result = throwingHighlightingParser.getTokens(input);
    } else {
        result = highlightingParser.getTokens(input);
    }
    // assert equal tokens
    Iterator<Token> iter = result.iterator();
    for (ILeafNode leaf : parseResult.getRootNode().getLeafNodes()) {
        Assert.assertTrue("hasNext at index " + leaf.getTotalOffset() + " for leaf '" + leaf.getText() + "'", iter.hasNext());
        Token token = iter.next();
        // TODO: assert token type
        Assert.assertEquals(leaf.getText(), token.getText());
    }
    return result;
}
Also used : ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) StringReader(java.io.StringReader) Token(org.antlr.runtime.Token) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 19 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project statecharts by Yakindu.

the class AbstractSGenTest method parseExpression.

protected EObject parseExpression(String expression, String ruleName) {
    XtextResource resource = resourceProvider.get();
    resource.setURI(URI.createPlatformPluginURI("path", true));
    ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
    parserRule.setName(ruleName);
    IParseResult result = parser.parse(parserRule, new StringReader(expression));
    EObject rootASTElement = result.getRootASTElement();
    resource.getContents().add(rootASTElement);
    ListBasedDiagnosticConsumer diagnosticsConsumer = new ListBasedDiagnosticConsumer();
    linker.linkModel(result.getRootASTElement(), diagnosticsConsumer);
    if (result.hasSyntaxErrors()) {
        StringBuilder errorMessages = new StringBuilder();
        Iterable<INode> syntaxErrors = result.getSyntaxErrors();
        for (INode iNode : syntaxErrors) {
            errorMessages.append(iNode.getSyntaxErrorMessage());
            errorMessages.append("\n");
        }
        throw new RuntimeException("Could not parse expression, syntax errors: " + errorMessages);
    }
    if (diagnosticsConsumer.hasConsumedDiagnostics(Severity.ERROR)) {
        throw new RuntimeException("Error during linking: " + diagnosticsConsumer.getResult(Severity.ERROR));
    }
    return rootASTElement;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) INode(org.eclipse.xtext.nodemodel.INode) EObject(org.eclipse.emf.ecore.EObject) StringReader(java.io.StringReader) ListBasedDiagnosticConsumer(org.eclipse.xtext.resource.impl.ListBasedDiagnosticConsumer) XtextResource(org.eclipse.xtext.resource.XtextResource) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 20 with IParseResult

use of org.eclipse.xtext.parser.IParseResult in project statecharts by Yakindu.

the class StextResource method parseStatechart.

protected void parseStatechart(Statechart statechart) {
    IParseResult parseResult = parse(statechart, StatechartSpecification.class.getSimpleName());
    StatechartSpecification rootASTElement = (StatechartSpecification) parseResult.getRootASTElement();
    statechart.setNamespace(rootASTElement.getNamespace());
    statechart.getScopes().clear();
    EList<Scope> definitionScopes = rootASTElement.getScopes();
    if (definitionScopes != null) {
        statechart.getScopes().addAll(definitionScopes);
    }
    statechart.getAnnotations().clear();
    EList<ArgumentedAnnotation> annotations = rootASTElement.getAnnotations();
    if (annotations != null) {
        statechart.getAnnotations().addAll(annotations);
    }
}
Also used : Scope(org.yakindu.sct.model.sgraph.Scope) StatechartSpecification(org.yakindu.sct.model.stext.stext.StatechartSpecification) ArgumentedAnnotation(org.yakindu.sct.model.stext.stext.ArgumentedAnnotation) IParseResult(org.eclipse.xtext.parser.IParseResult)

Aggregations

IParseResult (org.eclipse.xtext.parser.IParseResult)60 EObject (org.eclipse.emf.ecore.EObject)22 StringReader (java.io.StringReader)16 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)14 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)10 INode (org.eclipse.xtext.nodemodel.INode)10 XtextResource (org.eclipse.xtext.resource.XtextResource)10 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)9 ParserRule (org.eclipse.xtext.ParserRule)8 Test (org.junit.Test)7 ITextRegion (org.eclipse.xtext.util.ITextRegion)6 Resource (org.eclipse.emf.ecore.resource.Resource)4 TextRegion (org.eclipse.xtext.util.TextRegion)4 URI (org.eclipse.emf.common.util.URI)3 WrappedException (org.eclipse.emf.common.util.WrappedException)3 ParseResult (org.eclipse.xtext.parser.ParseResult)3 Scope (org.yakindu.sct.model.sgraph.Scope)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2