/** * 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; }
/** * Overridden to make sure that we add the root AST element sneakily to the resource list to make sure that no * accidental proxy resolution happens and that we do not increment the modification counter of the contents list. */ @Override protected void updateInternalState(IParseResult newParseResult) { setParseResult(newParseResult); EObject newRootAstElement = newParseResult.getRootASTElement(); if (newRootAstElement != null && !getContents().contains(newRootAstElement)) { // do not increment the modification counter here sneakyAddToContent(newRootAstElement); } reattachModificationTracker(newRootAstElement); clearErrorsAndWarnings(); addSyntaxErrors(); doLinking(); // make sure that the cache adapter is installed on this resource IResourceScopeCache cache = getCache(); if (cache instanceof OnChangeEvictingCache) { ((OnChangeEvictingCache) cache).getOrCreate(this); } }
/** * Returns true if the previous document state was completely broken, e.g. the parser did not recover at all. * This may happen e.g. in Xtend for documents like * <pre>import static class C {}</pre> * where the class keyword is consumed as an invalid token in the import declaration and everything thereafter * is unrecoverable. */ protected boolean isBrokenPreviousState(IParseResult previousParseResult, int offset) { if (previousParseResult.hasSyntaxErrors()) { BidiTreeIterator<AbstractNode> iterator = ((AbstractNode) previousParseResult.getRootNode()).basicIterator(); while(iterator.hasPrevious()) { AbstractNode previous = iterator.previous(); if (previous.getGrammarElement() == null) { return true; } if (previous instanceof ILeafNode && previous.getOffset() <= offset) { break; } } } return false; }
@SuppressWarnings({ "rawtypes", "unchecked" }) protected void replaceOldSemanticElement(EObject oldElement, IParseResult previousParseResult, IParseResult newParseResult) { EObject oldSemanticParentElement = oldElement.eContainer(); if (oldSemanticParentElement != null) { EStructuralFeature feature = oldElement.eContainingFeature(); if (feature.isMany()) { List featureValueList = (List) oldSemanticParentElement.eGet(feature); int index = featureValueList.indexOf(oldElement); unloadSemanticObject(oldElement); featureValueList.set(index, newParseResult.getRootASTElement()); } else { unloadSemanticObject(oldElement); oldSemanticParentElement.eSet(feature, newParseResult.getRootASTElement()); } ((ParseResult) newParseResult).setRootASTElement(previousParseResult.getRootASTElement()); } else { unloadSemanticObject(oldElement); } }
@Override public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) { if (resource == null) return; IParseResult parseResult = resource.getParseResult(); if (parseResult == null || parseResult.getRootASTElement() == null) return; if (highlightedIdentifiers == null) { highlightedIdentifiers = initializeHighlightedIdentifiers(); idLengthsToHighlight = new BitSet(); for (String s : highlightedIdentifiers.keySet()) { idLengthsToHighlight.set(s.length()); } } //TODO remove this check when the typesystem works without a java project if (resource.isValidationDisabled()) { highlightSpecialIdentifiers(acceptor, parseResult.getRootNode()); return; } doProvideHighlightingFor(resource, acceptor, cancelIndicator); }
@Override public IParseResult createParseResult(AbstractParsedToken token, CharSequence input) { // currentNode = null; currentStack.clear(); nonterminalStack.clear(); // this.input = input; if (DebugUtil.PARSE_RESULT_FACTORY_DEBUG) { IParsedTokenVisitor visitor = new CompoundParsedTokenVisitor(new ParsedTokenPrinter(), this); token.accept(visitor); } else { token.accept(this); } throw new UnsupportedOperationException(); // this.input = null; // return new ParseResult(currentStack.isEmpty() ? null : currentStack.getLast(), null, false); }
protected final IParseResult parse(INonTerminalConsumer consumer) { if (activeMarker != null) throw new IllegalStateException("cannot parse now. Active marker is already assigned."); IMarker rootMarker = mark(); IRootConsumerListener listener = new RootConsumerListener(); try { consumer.consumeAsRoot(listener); IParseResult result = getParseResultFactory().createParseResult(activeMarker, input); rootMarker.commit(); if (activeMarker != null) throw new IllegalStateException("cannot finish parse: active marker is still present."); return result; } catch(Exception e) { throw new WrappedException(e); } }
protected IParseResult doParse(String ruleName, CharStream in, NodeModelBuilder nodeModelBuilder, int initialLookAhead) { TokenSource tokenSource = createLexer(in); XtextTokenStream tokenStream = createTokenStream(tokenSource); tokenStream.initCurrentLookAhead(initialLookAhead); setInitialHiddenTokens(tokenStream); AbstractInternalAntlrParser parser = createParser(tokenStream); parser.setTokenTypeMap(getTokenDefProvider().getTokenDefMap()); parser.setSyntaxErrorProvider(getSyntaxErrorProvider()); parser.setNodeModelBuilder(nodeModelBuilder); parser.setSemanticModelBuilder(getElementFactory()); IUnorderedGroupHelper helper = getUnorderedGroupHelper().get(); parser.setUnorderedGroupHelper(helper); helper.initializeWith(parser); try { if(ruleName != null) return parser.parse(ruleName); return parser.parse(); } catch (Exception re) { throw new ParseException(re.getMessage(),re); } }
public PartialParsingPointers calculatePartialParsingPointers(IParseResult previousParseResult, final int offset, int replacedTextLength) { int myOffset = offset; int myReplacedTextLength = replacedTextLength; ICompositeNode oldRootNode = previousParseResult.getRootNode(); if (myOffset == oldRootNode.getTotalLength() && myOffset != 0) { // newText is appended, so look for the last original character instead --myOffset; myReplacedTextLength = 1; } // include any existing parse errors Range range = new Range(myOffset, myReplacedTextLength + myOffset); if (previousParseResult.hasSyntaxErrors()) range.mergeAllSyntaxErrors(oldRootNode); myOffset = range.getOffset(); List<ICompositeNode> nodesEnclosingRegion = collectNodesEnclosingChangeRegion(oldRootNode, range); List<ICompositeNode> validReplaceRootNodes = internalFindValidReplaceRootNodeForChangeRegion(nodesEnclosingRegion, range); filterInvalidRootNodes(oldRootNode, validReplaceRootNodes); if (validReplaceRootNodes.isEmpty()) { validReplaceRootNodes = Collections.singletonList(oldRootNode); } return new PartialParsingPointers(oldRootNode, myOffset, myReplacedTextLength, validReplaceRootNodes, nodesEnclosingRegion); }
/** * Returns the first non-whitespace leaf node in the file if it is a multi-line comment node. * * @since 2.3 * @return a list with exactly one node or an empty list if there is no header is undocumented. */ /* @NonNull */ @Override public List<INode> getFileHeaderNodes(Resource resource) { if (resource instanceof XtextResource) { IParseResult parseResult = ((XtextResource) resource).getParseResult(); if(parseResult != null) { for(ILeafNode leafNode: parseResult.getRootNode().getLeafNodes()) { EObject grammarElement = leafNode.getGrammarElement(); if(grammarElement instanceof TerminalRule) { String terminalRuleName = ((TerminalRule) grammarElement).getName(); if (ruleName.equalsIgnoreCase(terminalRuleName)) { return singletonList((INode) leafNode); } else if(wsRuleName.equals(terminalRuleName)) { continue; } } break; } } } return Collections.emptyList(); }
public void update(int offset, int replacedTextLength, String newText) { if (!isLoaded()) { throw new IllegalStateException("You can't update an unloaded resource."); } try { isUpdating = true; IParseResult oldParseResult = parseResult; ReplaceRegion replaceRegion = new ReplaceRegion(new TextRegion(offset, replacedTextLength), newText); IParseResult newParseResult; ParserRule oldEntryPoint = NodeModelUtils.getEntryParserRule(oldParseResult.getRootNode()); if (entryPoint == null || entryPoint == oldEntryPoint) { newParseResult = parser.reparse(oldParseResult, replaceRegion); } else { StringBuilder builder = new StringBuilder(oldParseResult.getRootNode().getText()); replaceRegion.applyTo(builder); newParseResult = parser.parse(entryPoint, new StringReader(builder.toString())); } updateInternalState(oldParseResult, newParseResult); } finally { isUpdating = false; } }
/** * @since 2.1 */ public INode getCrossReferenceNode(XtextResource resource, ITextRegion region) { IParseResult parseResult = resource.getParseResult(); if (parseResult != null) { ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), region.getOffset()); INode crossRefNode = findCrossReferenceNode(leaf); // if not a cross reference position and the cursor is at the beginning of a node try the previous one. if (crossRefNode == null && leaf != null && region.getLength()==0 && leaf.getOffset() == region.getOffset()) { leaf = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), region.getOffset() - 1); return findCrossReferenceNode(leaf); } else if (crossRefNode != null && crossRefNode.getEndOffset() >= region.getOffset() + region.getLength()) { return crossRefNode; } } return null; }
protected EObject internalResolveElementAt(XtextResource resource, int offset, boolean containment) { if(!containment) { EObject crossRef = resolveCrossReferencedElementAt(resource, offset); if (crossRef != null) return crossRef; } IParseResult parseResult = resource.getParseResult(); if (parseResult != null) { ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset); if (leaf != null && leaf.isHidden() && leaf.getOffset() == offset) { leaf = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset - 1); } if (leaf != null) { return NodeModelUtils.findActualSemanticObjectFor(leaf); } } return null; }
@Override public String processFile(String completeData, String data, int offset, int len, String change) throws Exception { IParseResult initialParseResult = parser.parse(new StringReader(data)); String newData = applyDelta(data, offset, len, change); ReplaceRegion replaceRegion = new ReplaceRegion(offset, len, change); try { IParseResult reparsed = parser.reparse(initialParseResult, replaceRegion); IParseResult parsedFromScratch = parser.parse(new StringReader(newData)); assertEqual(data, newData, parsedFromScratch, reparsed); return newData; } catch(Throwable e) { ComparisonFailure throwMe = new ComparisonFailure(e.getMessage(), newData, replaceRegion + DELIM + data); throwMe.initCause(e); throw throwMe; } }
protected void replaceAndReparse(String model, int offset, int length, String change, String expectedReparseRegion) throws Exception { IParseResult parseResult = getParseResultAndExpect(model, UNKNOWN_EXPECTATION); PartialParsingPointers parsingPointers = getPartialParser().calculatePartialParsingPointers(parseResult, offset, length); String reparseRegion = getPartialParser().insertChangeIntoReplaceRegion(parsingPointers .getDefaultReplaceRootNode(), new ReplaceRegion(offset, length, change)); assertEquals(expectedReparseRegion, reparseRegion); final Wrapper<Boolean> unloaded = Wrapper.wrap(Boolean.FALSE); getPartialParser().setUnloader(new IReferableElementsUnloader() { @Override public void unloadRoot(EObject root) { unloaded.set(Boolean.TRUE); } }); IParseResult partiallyReparse = reparse(parseResult, offset, length, change); assertTrue("unloaded", unloaded.get()); String expectedReparseModel = model.substring(0, offset) + change + model.substring(offset + length); assertEquals(expectedReparseModel, partiallyReparse.getRootNode().getText()); compareWithFullParse(model, offset, length, change); }
public ICompositeNode findEntryPoint(IParseResult parseResult, int offset) { ICompositeNode rootNode = parseResult.getRootNode(); if (rootNode.getTotalLength() == offset) { return null; } ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(rootNode, offset); ICompositeNode parent = leafNode.getParent(); ICompositeNode result = findEntryPoint(parent, offset); if (result != null) { EObject grammarElement = result.getGrammarElement(); if (grammarElement instanceof AbstractElement) { return result; } } return null; }
/** {@inheritDoc} */ @Override public void createHyperlinksByOffset(final XtextResource resource, final int offset, final IHyperlinkAcceptor acceptor) { final IParseResult parseResult = resource.getParseResult(); if (parseResult == null || parseResult.getRootNode() == null) { return; // Return, no need to call in super.createAdditionalHyperlinks } // Check if the current parse tree node represents an override keyword, in which case we want to link // to the overridden rule INode node = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset); if (node != null && isOverrideKeyword(node.getGrammarElement())) { Rule rule = (Rule) eObjectAtOffsetHelper.resolveElementAt(resource, offset); Region region = new Region(node.getOffset(), node.getLength()); List<Rule> extendedRules = getExtendedRules(rule); for (Rule extendedRule : extendedRules) { createHyperlinksTo(resource, region, extendedRule, acceptor); } } super.createHyperlinksByOffset(resource, offset, acceptor); }
public PartialParsingPointers calculatePartialParsingPointers(final IParseResult previousParseResult, final int offset, final int replacedTextLength) { int myOffset = offset; int myReplacedTextLength = replacedTextLength; ICompositeNode oldRootNode = previousParseResult.getRootNode(); if (myOffset == oldRootNode.getTotalLength() && myOffset != 0) { // newText is appended, so look for the last original character instead --myOffset; myReplacedTextLength = 1; } // include any existing parse errors Range range = new Range(myOffset, myReplacedTextLength + myOffset); if (previousParseResult.hasSyntaxErrors()) { range.mergeAllSyntaxErrors(oldRootNode); } myOffset = range.getOffset(); List<ICompositeNode> nodesEnclosingRegion = collectNodesEnclosingChangeRegion(oldRootNode, range); List<ICompositeNode> validReplaceRootNodes = internalFindValidReplaceRootNodeForChangeRegion(nodesEnclosingRegion, range); filterInvalidRootNodes(oldRootNode, validReplaceRootNodes); if (validReplaceRootNodes.isEmpty()) { validReplaceRootNodes = Collections.singletonList(oldRootNode); } return new PartialParsingPointers(oldRootNode, myOffset, myReplacedTextLength, validReplaceRootNodes, nodesEnclosingRegion); }
public ITextRegion selectEnclosing(XtextResource resource, ITextRegion currentEditorSelection) { Pair<EObject, EObject> currentlySelected = getSelectedAstElements(resource, currentEditorSelection); if (currentlySelected == null) { IParseResult parseResult = resource.getParseResult(); if (parseResult != null) { ICompositeNode rootNode = parseResult.getRootNode(); int offset = getSelectionOffset(rootNode, currentEditorSelection); INode node = findLeafNodeAtOffset(rootNode, offset); ITextRegion fineGrainedRegion = computeInitialFineGrainedSelection(node, currentEditorSelection); if (fineGrainedRegion != null) { selectionHistory.clear(); register(currentEditorSelection); return register(fineGrainedRegion); } EObject eObject = findSemanticObjectFor(node); return register(getTextRegion(eObject)); } } else { EObject first = currentlySelected.getFirst(); if (first.eContainer() != null) { return register(getTextRegion(first.eContainer())); } } return ITextRegion.EMPTY_REGION; }
protected ITextRegion getTextRegion(EObject eObject) { if (eObject == null) return null; IParseResult parseResult = ((XtextResource)eObject.eResource()).getParseResult(); if (parseResult == null) return null; ICompositeNode rootNode = parseResult.getRootNode(); Map<ILeafNode, EObject> comments = commentAssociater.associateCommentsWithSemanticEObjects(eObject, singleton(rootNode)); final ITextRegion result = locationProvider.getFullTextRegion(eObject); int start = result.getOffset(); int end = result.getOffset() + result.getLength(); for (Entry<ILeafNode, EObject> entry : comments.entrySet()) { if (entry.getValue() == eObject) { ILeafNode node = entry.getKey(); if (node.getTotalOffset() < start) { start = node.getTotalOffset(); } if (node.getTotalEndOffset() > end) { end = node.getTotalEndOffset(); } } } return new TextRegion(start, end-start); }
protected void computeObjectFolding(XtextResource xtextResource, IFoldingRegionAcceptor<ITextRegion> foldingRegionAcceptor) { IParseResult parseResult = xtextResource.getParseResult(); if(parseResult != null){ EObject rootASTElement = parseResult.getRootASTElement(); if(rootASTElement != null){ TreeIterator<EObject> allContents = rootASTElement.eAllContents(); while (allContents.hasNext()) { EObject eObject = allContents.next(); if (isHandled(eObject)) { computeObjectFolding(eObject, foldingRegionAcceptor); } if (!shouldProcessContent(eObject)) { allContents.prune(); } } } } }
public void assertResouceParsedCorrectly(XtextResource resource, final ReconcilerReplaceRegion region) { IParseResult parseResult = resource.getParseResult(); if (parseResult != null) { ICompositeNode rootNode = parseResult.getRootNode(); final String resourceContent = rootNode.getText(); IParseResult reparseResult = parser.parse(new StringReader(resourceContent)); if(!emfStructureComparator.isSameStructure(parseResult.getRootASTElement(), reparseResult.getRootASTElement())) { new DisplayRunnable() { @Override protected void run() throws Exception { LOG.error("PartialParsing produced wrong model"); LOG.error("Events: \n\t" + Joiner.on("\n\t").join(region.getDocumentEvents())); LOG.error("ReplaceRegion: \n\t'" + region + "'" ); MessageDialog.openError( Display.getCurrent().getActiveShell(), "XtextReconcilerDebugger", "PartialParsing produced wrong model." + "\n\nSee log for details."); } }; } } }
protected EObject mergeBack(final EObject object, final TransactionalEditingDomain editingDomain) { XtextResource _resource = this.resourceProvider.getResource(); IParseResult _parseResult = _resource.getParseResult(); final EObject modelCopy = _parseResult.getRootASTElement(); final EObject copy = this.modelMerger.findMatchingObject(modelCopy, object); boolean _or = false; if ((copy == null)) { _or = true; } else { EObject _eContainer = copy.eContainer(); boolean _tripleEquals = (_eContainer == null); _or = _tripleEquals; } if (_or) { return null; } CommandStack _commandStack = editingDomain.getCommandStack(); _commandStack.execute(new RecordingCommand(editingDomain, "Text Changes") { @Override protected void doExecute() { TextPropertiesViewPart.this.modelMerger.merge(copy, object); } }); return copy; }
public ISelection getSelection() { if (getStyledText().isDisposed()) return StructuredSelection.EMPTY; int offset = getStyledText().getCaretOffset() - 1; XtextResource fakeResource = StyledTextXtextAdapter.this.getFakeResourceContext().getFakeResource(); IParseResult parseResult = fakeResource.getParseResult(); if (parseResult == null) return StructuredSelection.EMPTY; ICompositeNode rootNode = parseResult.getRootNode(); ILeafNode selectedNode = NodeModelUtils.findLeafNodeAtOffset(rootNode, offset); final EObject selectedObject = NodeModelUtils.findActualSemanticObjectFor(selectedNode); if (selectedObject == null) { return StructuredSelection.EMPTY; } return new StructuredSelection(selectedObject); }
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); } }
@Override public IHyperlink[] createHyperlinksByOffset(XtextResource resource, int offset, boolean createMultipleHyperlinks) { EObject eObject = helper.resolveElementAt(resource, offset); if (eObject instanceof StepDefinition || eObject instanceof StaticParam || eObject instanceof DynamicParam) { IParseResult parseResult = resource.getParseResult(); INode node = NodeModelUtils.findLeafNodeAtOffset( parseResult.getRootNode(), offset); while (!(node instanceof CompositeNode && node.getSemanticElement() instanceof StepDefinition)) { node = node.getParent(); } String description = StepUtil.getStepText(node); if (!openConceptDefinition(eObject, description)) { openStepDefinition(description); } } return null; }
public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor) { if (resource == null) return; IParseResult parseResult = resource.getParseResult(); if (parseResult == null) return; INode root = parseResult.getRootNode(); Iterator<ILeafNode> leafNodesIt = root.getLeafNodes().iterator(); while (leafNodesIt.hasNext()) { ILeafNode nextLeaf = leafNodesIt.next(); EObject semanticElement = nextLeaf.getSemanticElement(); if (semanticElement instanceof POperand) { acceptor.addPosition(nextLeaf.getOffset(), nextLeaf.getLength(), SqlHighlightingConfiguration.PARAM_TOKEN); } else if (semanticElement instanceof ExpOperand) { acceptor.addPosition(nextLeaf.getOffset(), nextLeaf.getLength(), SqlHighlightingConfiguration.PARAM_TOKEN); } else if (semanticElement instanceof XExpr) { acceptor.addPosition(nextLeaf.getOffset(), nextLeaf.getLength(), SqlHighlightingConfiguration.XEXPRESSION_TOKEN); } } }
public ISelection getSelection() { if (styledText.isDisposed()) return StructuredSelection.EMPTY; int offset = styledText.getCaretOffset() - 1; XtextResource fakeResource = StyledTextXtextAdapter.this.getFakeResourceContext().getFakeResource(); IParseResult parseResult = fakeResource.getParseResult(); if (parseResult == null) return StructuredSelection.EMPTY; ICompositeNode rootNode = parseResult.getRootNode(); ILeafNode selectedNode = NodeModelUtils.findLeafNodeAtOffset(rootNode, offset); final EObject selectedObject = NodeModelUtils.findActualSemanticObjectFor(selectedNode); if (selectedObject == null) { return StructuredSelection.EMPTY; } return new StructuredSelection(selectedObject); }
@Override public void selectionChanged(IWorkbenchPart part, ISelection selection) { if (part instanceof XtextEditor && !selection.isEmpty()) { final XtextEditor editor = (XtextEditor) part; final IXtextDocument document = editor.getDocument(); document.readOnly(new IUnitOfWork.Void<XtextResource>() { @Override public void process(XtextResource resource) throws Exception { IParseResult parseResult = resource.getParseResult(); if (parseResult != null) { ICompositeNode root = parseResult.getRootNode(); EObject taxonomy = NodeModelUtils.findActualSemanticObjectFor(root); if (taxonomy instanceof Model) { ModelRegistryPlugin.getModelRegistry().setActiveTaxonomy((Model) taxonomy); } } } }); } }
@Test public void defGrammar() { try { final String suffix = File.separator + "defGrammar" + File.separator + "G"; final String content = this.helper.getTextFromFile(GrammarSyntaxTest.class, suffix); final IParseResult parseResult = this.helper.parse(content); final boolean hasSyntaxErrors = parseResult.hasSyntaxErrors(); Assert.assertFalse(hasSyntaxErrors); final Grammar grammar = this.parser.parse(content); Assert.assertNotNull(grammar); Assert.assertEquals("G", grammar.getName()); Assert.assertEquals(GrammarType.DEFAULT, grammar.getType()); } catch (final Throwable throwable) { throw Exceptions.sneakyThrow(throwable); } }
@Test public void lexerGrammar() { try { final String suffix = File.separator + "lexerGrammar" + File.separator + "L"; //final String content = this.parser.getTextFromFile(GrammarSyntaxTest.class, suffix); final String content = this.helper.getTextFromFile(GrammarSyntaxTest.class, suffix); final IParseResult parseResult = this.helper.parse(content); final boolean hasSyntaxErrors = parseResult.hasSyntaxErrors(); Assert.assertFalse(hasSyntaxErrors); final Grammar grammar = this.parser.parse(content); Assert.assertNotNull(grammar); Assert.assertEquals("L", grammar.getName()); Assert.assertEquals(GrammarType.LEXER, grammar.getType()); } catch (final Throwable throwable) { throw Exceptions.sneakyThrow(throwable); } }
@Test public void parserGrammar() { try { final String suffix = File.separator + "parserGrammar" + File.separator + "P"; //final String content = this.parser.getTextFromFile(GrammarSyntaxTest.class, suffix); final String content = this.helper.getTextFromFile(GrammarSyntaxTest.class, suffix); final IParseResult parseResult = this.helper.parse(content); final boolean hasSyntaxErrors = parseResult.hasSyntaxErrors(); Assert.assertFalse(hasSyntaxErrors); final Grammar grammar = this.parser.parse(content); Assert.assertNotNull(grammar); Assert.assertEquals("P", grammar.getName()); Assert.assertEquals(GrammarType.PARSER, grammar.getType()); } catch (final Throwable throwable) { throw Exceptions.sneakyThrow(throwable); } }
@Test public void treeGrammar() { try { final String suffix = File.separator + "treeGrammar" + File.separator + "T"; //final String content = this.parser.getTextFromFile(GrammarSyntaxTest.class, suffix); final String content = this.helper.getTextFromFile(GrammarSyntaxTest.class, suffix, ".txt"); final IParseResult parseResult = this.helper.parse(content); final boolean hasSyntaxErrors = parseResult.hasSyntaxErrors(); Assert.assertFalse(hasSyntaxErrors); final Grammar grammar = this.parser.parse(content); Assert.assertNotNull(grammar); Assert.assertEquals("T", grammar.getName()); Assert.assertEquals(GrammarType.TREE, grammar.getType()); } catch (final Throwable throwable) { throw Exceptions.sneakyThrow(throwable); } }
@Test public void grammarEmptyOptions() { try { final String suffix = File.separator + "grammarEmptyOptions" + File.separator + "G"; final String content = this.helper.getTextFromFile(GrammarSyntaxTest.class, suffix); final IParseResult parseResult = this.helper.parse(content); final boolean hasSyntaxErrors = parseResult.hasSyntaxErrors(); Assert.assertFalse(hasSyntaxErrors); final Grammar grammar = this.parser.parse(content); Assert.assertNotNull(grammar); final Options options = getOptions(grammar); Assert.assertNotNull(options); } catch (final Throwable throwable) { throw Exceptions.sneakyThrow(throwable); } }
@Test public void grammarOptions() { try { final String suffix = File.separator + "grammarOptions" + File.separator + "G"; final String content = this.helper.getTextFromFile(GrammarSyntaxTest.class, suffix); final IParseResult parseResult = this.helper.parse(content); final boolean hasSyntaxErrors = parseResult.hasSyntaxErrors(); Assert.assertFalse(hasSyntaxErrors); final Grammar grammar = this.parser.parse(content); Assert.assertNotNull(grammar); final Options options = getOptions(grammar); Assert.assertNotNull(options); this.assertQualifiedOption(options, "language", "Java"); this.assertQualifiedOption(options, "superClass", "com.my.class.Parser"); } catch (final Throwable throwable) { throw Exceptions.sneakyThrow(throwable); } }
@Test public void slashInLangAction() { try { final String content = this.parser.getTextFromFile(LangActionTest.class, ""); final IParseResult parseResults = this.parser.parse(content); final boolean syntaxErrors = parseResults.hasSyntaxErrors(); Assert.assertFalse(syntaxErrors); final Grammar grammar = this.parseHelper.parse(content); Assert.assertNotNull(grammar); final GrammarAction action = getAction(grammar); Assert.assertNotNull(action); final String expected = getExpectedString(); final String actual = action.getAction(); Assert.assertEquals(expected, actual); } catch (final Throwable throwable) { throw Exceptions.sneakyThrow(throwable); } }
/** * Parses data provided by an input reader using Xtext and returns the root * node of the resulting object tree. * * @param reader * Input reader * @return root object node * @throws IOException * when errors occur during the parsing process */ public EObject parse(String path) throws IOException { // System.out.println(path ); File file = new File(path); if (!readable(file)) { return null; } if (file.length() > 70000000) { System.out.println("(" + path + ") is too big " + file.length()); return null; } Reader reader = new BufferedReader(new FileReader(file)); IParseResult result = parser.parse(reader); if (result.hasSyntaxErrors()) { for (INode e : result.getSyntaxErrors()) { System.out.println("(" + path + ":" + e.getStartLine() + "): " + e.getText()); } // throw new // ParseException("Provided input contains syntax errors."); } return result.getRootASTElement(); }
/** * * @param str * @return the result of parsing the input constraint string. Returns a valid result with a null input constraint if str is null or empty. */ public Result parse(final String str) { if(str == null || str.trim().length() == 0) { return new Result((InputConstraint)null); } final IParseResult result = parser.parse(new StringReader(str)); // Check for syntax errors if(result.hasSyntaxErrors()) { String errors = "Error parsing '" + str + "':"; for(final INode err : result.getSyntaxErrors()) { errors += "\n" + err.getSyntaxErrorMessage().getMessage(); } return new Result(errors); } final InputConstraint ic = (InputConstraint)result.getRootASTElement(); final Resource r = createResource(); r.getContents().add(ic); return new Result(ic); }
/** * Computes an MD5 hash from the given resource's source code (the actual source text), as stored in * {@link TModule#getAstMD5()}. Will fail with an exception if the given resource does not have a valid * {@link XtextResource#getParseResult() parse result}, as created by Xtext during parsing. */ public static String md5Hex(XtextResource resource) { final IParseResult parseResult = resource.getParseResult(); final INode rootNode = parseResult != null ? parseResult.getRootNode() : null; final String source = rootNode != null ? rootNode.getText() : null; if (source == null) { throw new IllegalStateException("resource does not have a valid parse result: " + resource.getURI()); } return Hashing.md5().hashString(source, Charsets.UTF_8).toString(); }
private XtextResource getNewResource(String content, URI uri) { IParseResult parseResult = parser.parse(new StringReader(content)); ResourceSet resourceSet2 = resourceSetProvider.get(); XtextResource newXtextResource = resourceProvider.get(); newXtextResource.setURI(uri); resourceSet2.getResources().add(newXtextResource); newXtextResource.setParseResult(parseResult); return newXtextResource; }