/** * This is aware of warnings from the {@link N4JSStringValueConverter}. * * Issues from the parser are commonly treated as errors but here we want to create a warning. */ @Override protected void addSyntaxErrors() { if (isValidationDisabled()) return; // EList.add unnecessarily checks for uniqueness by default // so we use #addUnique below to save some CPU cycles for heavily broken // models BasicEList<Diagnostic> errorList = (BasicEList<Diagnostic>) getErrors(); BasicEList<Diagnostic> warningList = (BasicEList<Diagnostic>) getWarnings(); for (INode error : getParseResult().getSyntaxErrors()) { XtextSyntaxDiagnostic diagnostic = createSyntaxDiagnostic(error); String code = diagnostic.getCode(); if (AbstractN4JSStringValueConverter.WARN_ISSUE_CODE.equals(code) || RegExLiteralConverter.ISSUE_CODE.equals(code) || LegacyOctalIntValueConverter.ISSUE_CODE.equals(code)) { warningList.addUnique(diagnostic); } else if (!InternalSemicolonInjectingParser.SEMICOLON_INSERTED.equals(code)) { errorList.addUnique(diagnostic); } } }
private XtextSyntaxDiagnostic createSyntaxDiagnostic(INode error) { SyntaxErrorMessage syntaxErrorMessage = error.getSyntaxErrorMessage(); if (org.eclipse.xtext.diagnostics.Diagnostic.SYNTAX_DIAGNOSTIC_WITH_RANGE.equals(syntaxErrorMessage .getIssueCode())) { String[] issueData = syntaxErrorMessage.getIssueData(); if (issueData.length == 1) { String data = issueData[0]; int colon = data.indexOf(':'); return new XtextSyntaxDiagnosticWithRange(error, Integer.valueOf(data.substring(0, colon)), Integer.valueOf(data.substring(colon + 1)), null) { @Override public int getLine() { return getNode().getTotalStartLine(); } }; } } return new XtextSyntaxDiagnostic(error); }
@Override public void convertResourceDiagnostic(Diagnostic diagnostic, Severity severity, IAcceptor<Issue> acceptor) { IssueImpl issue = new Issue.IssueImpl(); issue.setSyntaxError(diagnostic instanceof XtextSyntaxDiagnostic); issue.setSeverity(severity); issue.setLineNumber(diagnostic.getLine()); issue.setColumn(diagnostic.getColumn()); issue.setMessage(diagnostic.getMessage()); if (diagnostic instanceof org.eclipse.xtext.diagnostics.Diagnostic) { org.eclipse.xtext.diagnostics.Diagnostic xtextDiagnostic = (org.eclipse.xtext.diagnostics.Diagnostic) diagnostic; issue.setOffset(xtextDiagnostic.getOffset()); issue.setLength(xtextDiagnostic.getLength()); } if (diagnostic instanceof AbstractDiagnostic) { AbstractDiagnostic castedDiagnostic = (AbstractDiagnostic)diagnostic; issue.setUriToProblem(castedDiagnostic.getUriToProblem()); issue.setCode(castedDiagnostic.getCode()); issue.setData(castedDiagnostic.getData()); } issue.setType(CheckType.FAST); acceptor.accept(issue); }
/** * @since 2.9 */ @Override protected void addSyntaxDiagnostic(List<Diagnostic> diagnostics, INode node) { SyntaxErrorMessage syntaxErrorMessage = node.getSyntaxErrorMessage(); if (CardinalityAwareSyntaxErrorMessageProvider.CARDINALITY_ISSUE.equals(syntaxErrorMessage.getIssueCode())) { super.getWarnings().add(new XtextSyntaxDiagnostic(node)); } else { super.addSyntaxDiagnostic(diagnostics, node); } }
@Override protected void issueFromXtextResourceDiagnostic(Diagnostic diagnostic, Severity severity, IAcceptor<Issue> acceptor) { if (diagnostic instanceof XtextSyntaxDiagnostic) { super.issueFromXtextResourceDiagnostic(diagnostic, severity, acceptor); } else if (diagnostic instanceof XtextLinkingDiagnostic) { XtextLinkingDiagnostic linkingDiagnostic = (XtextLinkingDiagnostic) diagnostic; if (linkingDiagnostic.getCode().equals(XtextLinkingDiagnosticMessageProvider.UNRESOLVED_RULE)) { super.issueFromXtextResourceDiagnostic(diagnostic, severity, acceptor); } else if (linkingDiagnostic.getMessage().contains("reference to Grammar")) { super.issueFromXtextResourceDiagnostic(diagnostic, severity, acceptor); } } }
@SuppressWarnings("unchecked") @Test public void testParseWithFractionError() throws Exception { String model = "a.b.c.d: 1/0;"; Resource r = getResourceFromStringAndExpect(model, 1); EObject parsedModel = r.getContents().get(0); assertNotNull(parsedModel); EObject firstModel = ((List<EObject>) parsedModel.eGet(modelFeature)).get(0); assertFalse(firstModel.eIsSet(valueFeature)); assertEquals(1, r.getErrors().size()); XtextSyntaxDiagnostic diag = (XtextSyntaxDiagnostic) r.getErrors().get(0); assertEquals(model.indexOf("1/0"), diag.getOffset()); assertEquals(3, diag.getLength()); }
@SuppressWarnings("unchecked") @Test public void testParseWithFractionErrorAndSpaces() throws Exception { String model = "a.b.c.d: 1 / 0 ; "; Resource r = getResourceFromStringAndExpect(model, 1); EObject parsedModel = r.getContents().get(0); assertNotNull(parsedModel); EObject firstModel = ((List<EObject>) parsedModel.eGet(modelFeature)).get(0); assertFalse(firstModel.eIsSet(valueFeature)); assertEquals(1, r.getErrors().size()); XtextSyntaxDiagnostic diag = (XtextSyntaxDiagnostic) r.getErrors().get(0); assertEquals(model.indexOf("1 / 0"), diag.getOffset()); assertEquals(5, diag.getLength()); }
@Test public void testNoExceptionUncaught() throws Exception { String modelAsString = "Hello max ! Hello peter! favourite peter"; Model model = (Model)getModelAndExpect(modelAsString, 2); EList<Diagnostic> errors = model.eResource().getErrors(); Diagnostic diagnosticSyntax = errors.get(0); Diagnostic diagnosticLinking = errors.get(1); assertTrue(diagnosticSyntax instanceof XtextSyntaxDiagnostic); assertTrue(diagnosticLinking instanceof XtextLinkingDiagnostic); }
/** * Validates if there is a syntax error present in the source content. * * @param sourceFileName * the file name that should be associated with the parsed content, must not be {@code null} * @param sourceContent * source, must not be {@code null} */ protected void assertNoSyntaxErrorsOnResource(final String sourceFileName, final CharSequence sourceContent) { final XtextTestSource testSource = createTestSource(sourceFileName, sourceContent.toString()); final List<Resource.Diagnostic> errors = testSource.getModel().eResource().getErrors().stream().filter(error -> error instanceof XtextSyntaxDiagnostic).collect(Collectors.toList()); if (!errors.isEmpty()) { StringBuilder sb = new StringBuilder("Syntax error is present in the test source.\nList of all found syntax errors:"); errors.forEach(err -> sb.append("\n\t " + err.getMessage())); Assert.fail(sb.toString()); } }
/** * @see org.eclipse.xtext.ui.editor.model.IXtextModelListener#modelChanged(org.eclipse.xtext.resource.XtextResource) */ public void modelChanged(XtextResource resource) { if (resource == null) return; boolean existingSyntaxErrors = Iterables.any(resource.getErrors(), new Predicate<Diagnostic>() { public boolean apply(Diagnostic diagnostic) { return diagnostic instanceof XtextSyntaxDiagnostic; } }); if (!existingSyntaxErrors) { calculateProjectionAnnotationModel(false); } }
private static boolean hasSyntaxError(List<Diagnostic> diagnostics) { for (Diagnostic diagnostic : diagnostics) { if (diagnostic instanceof XtextSyntaxDiagnostic) { return true; } } return false; }
public static boolean hasSyntaxError(List<Diagnostic> diagnostics) { for (Diagnostic diagnostic : diagnostics) { if (diagnostic instanceof XtextSyntaxDiagnostic) { return true; } } return false; }
public Iterable<Resource.Diagnostic> getLinkingAndSyntaxErrors(final Resource resource) { final Function1<Resource.Diagnostic, Boolean> _function = (Resource.Diagnostic it) -> { return Boolean.valueOf(((it instanceof XtextSyntaxDiagnostic) || (it instanceof XtextLinkingDiagnostic))); }; return IterableExtensions.<Resource.Diagnostic>filter(resource.getErrors(), _function); }
/** * Assert that the test source has no syntax error. */ protected void assertNoSyntaxError() { Assert.assertFalse("The source has syntax errors", Iterables.any(getTestSource().getXtextResource().getErrors(), Predicates.instanceOf(XtextSyntaxDiagnostic.class))); }
public void testInvalidExpressionParsing() { State state = createState("This is not a valid expression"); res.getContents().add(state); assertEquals(1, res.getSyntaxDiagnostics().size()); assertTrue(res.getErrors().get(0) instanceof XtextSyntaxDiagnostic); }
protected void createDiagnostics(IParseResult parseResult, SpecificationElement semanticTarget) { syntaxDiagnostics.get(semanticTarget).clear(); for (INode error : parseResult.getSyntaxErrors()) { syntaxDiagnostics.put(semanticTarget, new XtextSyntaxDiagnostic(error)); } }