/** * Reports errors that should be present in the node model. This is specialized because we want real errors to * override the synthesized diagnostics for automatically inserted semicolons. * * Usually only one syntax error message will be reported per location. For input documents of the form * * <pre> * var i}; * </pre> * * we insert a semicolon automatically before the closing brace. That implies, that we have a message on the brace * itself. The parser will try to match the real brace afterwards against the follow element set and fails. It tries * to report that error but since there is already a message it would be discarded. Here we force the real error * message to be replace the former info. */ @Override public void reportError(RecognitionException e) { if (state.errorRecovery) { return; } try { SyntaxErrorMessage currentError = (SyntaxErrorMessage) reflectCurrentError.get(this); if (currentError != null && SEMICOLON_INSERTED.equals(currentError.getIssueCode())) { setCurrentError(null); } super.reportError(e); } catch (IllegalArgumentException | IllegalAccessException e1) { super.reportError(e); } }
@Override public SyntaxErrorMessage getSyntaxErrorMessage( IValueConverterErrorContext context) { ValueConverterException cause = context.getValueConverterException(); if (cause instanceof N4JSStringValueConverter.BadEscapementException) { if (((N4JSStringValueConverter.BadEscapementException) cause).isError()) return new SyntaxErrorMessage(context.getDefaultMessage(), AbstractN4JSStringValueConverter.ERROR_ISSUE_CODE); return new SyntaxErrorMessage(context.getDefaultMessage(), AbstractN4JSStringValueConverter.WARN_ISSUE_CODE); } if (cause instanceof LegacyOctalIntValueConverter.LeadingZerosException) { return new SyntaxErrorMessage(context.getDefaultMessage(), LegacyOctalIntValueConverter.ISSUE_CODE); } if (cause instanceof RegExLiteralConverter.BogusRegExLiteralException) { return new SyntaxErrorMessage(context.getDefaultMessage(), RegExLiteralConverter.ISSUE_CODE); } return super.getSyntaxErrorMessage(context); }
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); }
public ILeafNode newLeafNode(int offset, int length, EObject grammarElement, boolean isHidden, /* @Nullable */ SyntaxErrorMessage errorMessage, ICompositeNode parent) { LeafNode result = null; if (errorMessage != null) { if (isHidden) { result = new HiddenLeafNodeWithSyntaxError(); ((HiddenLeafNodeWithSyntaxError)result).basicSetSyntaxErrorMessage(errorMessage); } else { result = new LeafNodeWithSyntaxError(); ((LeafNodeWithSyntaxError)result).basicSetSyntaxErrorMessage(errorMessage); } } else { if (isHidden) { result = new HiddenLeafNode(); } else { result = new LeafNode(); } } result.basicSetGrammarElement(grammarElement); result.basicSetTotalOffset(offset); result.basicSetTotalLength(length); addChild(parent, result); return result; }
/** * {@code true} if the leaf node argument is an instance of {@link LeafNodeWithSyntaxError} and the issue code of * the syntax error message matches with any of the ignored syntax error issue codes argument. Otherwise, returns * with {@code false}. */ public static boolean isIgnoredSyntaxErrorNode(final INode leaf, final String... ignoredSyntaxErrorIssues) { if (leaf instanceof LeafNodeWithSyntaxError) { final SyntaxErrorMessage errorMessage = leaf.getSyntaxErrorMessage(); if (null != errorMessage) { return contains(errorMessage.getIssueCode(), ignoredSyntaxErrorIssues); } } return false; }
/** * Implementation of the {@link TokenSource} interface. Return new tokens as long as there are some, afterwards * return {@link Token#EOF_TOKEN}. */ @Override public Token nextToken() { if (next != null) { Token result = next; next = null; return result; } if (!leafNodes.hasNext()) { return Token.EOF_TOKEN; } ILeafNode leaf = leafNodes.next(); if (leaf.getTotalOffset() >= endOffset) { leafNodes = Iterators.emptyIterator(); return Token.EOF_TOKEN; } if (leaf.getTotalEndOffset() <= startOffset) { return nextToken(); } if (leaf.getTotalEndOffset() > endOffset) { return toPrefixToken(leaf); } SyntaxErrorMessage syntaxErrorMessage = leaf.getSyntaxErrorMessage(); if (syntaxErrorMessage != null && SEMICOLON_INSERTED.equals(syntaxErrorMessage.getIssueCode())) { return toASIToken(leaf); } if (leaf.isHidden()) { return processHiddenToken(leaf); } int tokenType = tokenTypeMapper.getInternalTokenType(leaf); return new CommonToken(tokenType, leaf.getText()); }
private void setCurrentError(SyntaxErrorMessage syntaxErrorMessage) { try { reflectCurrentError.set(this, syntaxErrorMessage); } catch (Exception e) { throw new RuntimeException(); } }
/** * <p> * Overrides method stub generated by customized ANTLR/Xtext generator. * </p> */ @Override public void addASIMessage() { if (!hasCurrentError()) { SyntaxErrorMessage message = new SyntaxErrorMessage("Automatically inserted semicolon", SEMICOLON_INSERTED); setCurrentError(message); } }
@SuppressWarnings("unchecked") private static <T> ParserResults<T> parse(String parseRuleName, String versionNo) { final ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule(); parserRule.setName(parseRuleName); final N4MFParser parser = getService(N4MFParser.class, getRandomURI()); final IParseResult result = parser.parse(parserRule, new StringReader(versionNo)); final ParserResults<T> res = new ParserResults<>(); res.ast = (T) result.getRootASTElement(); result.getSyntaxErrors().forEach(n -> { SyntaxErrorMessage syntaxErrorMessage = n.getSyntaxErrorMessage(); res.addErrors(syntaxErrorMessage.getMessage()); }); return res; }
protected void assertNoSyntaxErrors(XtextResource resource) { Iterable<INode> syntaxErrors = resource.getParseResult().getSyntaxErrors(); if (!Iterables.isEmpty(syntaxErrors)) { StringBuilder builder = new StringBuilder(); builder.append("This document can't be formatted because of syntax errors:\n"); for (INode node : syntaxErrors) { SyntaxErrorMessage msg = node.getSyntaxErrorMessage(); builder.append(String.format("Line %02d: %s\n", node.getTotalStartLine(), msg.getMessage())); } fail(builder, resource.getParseResult().getRootNode().getText()); } }
public static void writeSyntaxErrorMessage(DataOutputStream out, SerializationConversionContext scc, SyntaxErrorMessage syntaxErrorMessage) throws IOException { if (syntaxErrorMessage == null) { out.writeBoolean(true); } else { out.writeBoolean(false); SerializationUtil.writeString(out, syntaxErrorMessage.getMessage()); SerializationUtil.writeString(out, syntaxErrorMessage.getIssueCode()); SerializationUtil.writeStringArray(out, syntaxErrorMessage.getIssueData()); } }
public static SyntaxErrorMessage readSyntaxErrorMessage(DataInputStream in, DeserializationConversionContext context) throws IOException { boolean isNull = in.readBoolean(); if (isNull) return null; String message = SerializationUtil.readString(in); String issueCode = SerializationUtil.readString(in); String[] issueData = SerializationUtil.readStringArray(in); SyntaxErrorMessage result = new SyntaxErrorMessage(message, issueCode, issueData); return result; }
@Override public SyntaxErrorMessage getSyntaxErrorMessage(IValueConverterErrorContext context) { ValueConverterException cause = context.getValueConverterException(); if (cause instanceof MoreThanOneCardinalityException) { return new SyntaxErrorMessage(context.getDefaultMessage(), CARDINALITY_ISSUE); } return super.getSyntaxErrorMessage(context); }
/** * @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); } }
private ILeafNode createLeafNode(Token token, EObject grammarElement) { boolean isHidden = token.getChannel() == HIDDEN; SyntaxErrorMessage error = null; if (!isHidden) { if (currentError != null) { error = currentError; currentError = null; } } if (token.getType() == Token.INVALID_TOKEN_TYPE) { if (error == null) { String lexerErrorMessage = ((XtextTokenStream) input).getLexerErrorMessage(token); LexerErrorContext errorContext = new LexerErrorContext(lexerErrorMessage); error = syntaxErrorProvider.getSyntaxErrorMessage(errorContext); } } if (grammarElement == null) { String ruleName = antlrTypeToLexerName.get(token.getType()); grammarElement = allRules.get(ruleName); } CommonToken commonToken = (CommonToken) token; if (error != null) hadErrors = true; return nodeBuilder.newLeafNode( commonToken.getStartIndex(), commonToken.getStopIndex() - commonToken.getStartIndex() + 1, grammarElement, isHidden, error, currentNode); }
@Override public SyntaxErrorMessage getSyntaxErrorMessage(IValueConverterErrorContext context) { ValueConverterException cause = context.getValueConverterException(); if (cause instanceof ValueConverterWithValueException) { ValueConverterWithValueException casted = (ValueConverterWithValueException) cause; if (casted.hasRange()) { return createRangedSyntaxErrorMessage(context, casted.getOffset(), casted.getLength()); } } return new SyntaxErrorMessage(context.getDefaultMessage(), SYNTAX_DIAGNOSTIC); }
/** * @since 2.7 */ protected void addSyntaxDiagnostic(List<Diagnostic> diagnostics, 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(':'); diagnostics.add(new XtextSyntaxDiagnosticWithRange(error, Integer.valueOf(data.substring(0, colon)), Integer.valueOf(data.substring(colon + 1)), null)); return; } } diagnostics.add(new XtextSyntaxDiagnostic(error)); }
@Override public SyntaxErrorMessage getSyntaxErrorMessage(IParserErrorContext context) { if (expectation != null) { assertTrue(context instanceof IUnorderedGroupErrorContext); IUnorderedGroupErrorContext casted = (IUnorderedGroupErrorContext) context; List<AbstractElement> mandatoryElements = casted.getMissingMandatoryElements(); assertEquals(expectation, mandatoryElements); } return delegate.getSyntaxErrorMessage(context); }
@Test public void testSyntaxError_03() throws Exception { syntaxErrorProvider = new SyntaxErrorMessageProvider() { @Override public SyntaxErrorMessage getSyntaxErrorMessage(IParserErrorContext context) { SyntaxErrorMessage result = super.getSyntaxErrorMessage(context); assertEquals("mismatched input '<EOF>' expecting 'model'", result.getMessage()); return result; } }; getModelAndExpect("", 1); }
@Override public SyntaxErrorMessage getSyntaxErrorMessage(IParserErrorContext context) { for(String tokenName: context.getTokenNames()) { assertFalse(tokenName.startsWith("KEYWORD")); } if (expectedMessage != null) return new SyntaxErrorMessage(expectedMessage, null); return syntaxErrorProvider.getSyntaxErrorMessage(context); }
@Override public SyntaxErrorMessage getSyntaxErrorMessage(IParserErrorContext context) { // Ref: https://github.com/getgauge/Gauge-Eclipse/issues/5 // HACK : Ideally the Spec.xtext grammar should allow EOF to be a valid line end // The grammar workarounds do not seem to work without major refactoring. // Added this to prevent enforcing a newline at the end of a file. if(!context.getDefaultMessage().contains("'<EOF>'")) return super.getSyntaxErrorMessage(context); return null; }
@Override public SyntaxErrorMessage getSyntaxErrorMessage() { return syntaxErrorMessage; }
protected void basicSetSyntaxErrorMessage(SyntaxErrorMessage syntaxErrorMessage) { this.syntaxErrorMessage = syntaxErrorMessage; }
@Override public SyntaxErrorMessage getSyntaxErrorMessage() { return null; }
public SyntaxErrorMessage getSyntaxErrorMessage(RecognitionException e, String[] tokenNames) { hadErrors = true; IParserErrorContext parseErrorContext = createErrorContext(e); return syntaxErrorProvider.getSyntaxErrorMessage(parseErrorContext); }
@Override public SyntaxErrorMessage getSyntaxErrorMessage(IParserErrorContext context) { return new SyntaxErrorMessage(context.getDefaultMessage(), SYNTAX_DIAGNOSTIC); }
/** * @since 2.7 */ protected SyntaxErrorMessage createRangedSyntaxErrorMessage(IValueConverterErrorContext context, int offset, int length) { String range = offset + ":" + length; return new SyntaxErrorMessage(context.getDefaultMessage(), SYNTAX_DIAGNOSTIC_WITH_RANGE, new String[] { range }); }
@Override public SyntaxErrorMessage getSyntaxErrorMessage(IValueConverterErrorContext context) { return delegate.getSyntaxErrorMessage(context); }
@Override public SyntaxErrorMessage getSyntaxErrorMessage(IValueConverterErrorContext context) { fail("Unexpected Invocation"); return null; }
/** {@inheritDoc} */ @Override public SyntaxErrorMessage getSyntaxErrorMessage() { initializeDelegateNode(); return delegateNode.getSyntaxErrorMessage(); }
/** * Provide the error message for a syntax error. * @see IParserErrorContext * @see IUnorderedGroupErrorContext * @return a {@link SyntaxErrorMessage}. */ SyntaxErrorMessage getSyntaxErrorMessage(IParserErrorContext context);
/** * Provide the error message for a value conversion error. * @see IValueConverterErrorContext * @return a {@link SyntaxErrorMessage}. */ SyntaxErrorMessage getSyntaxErrorMessage(IValueConverterErrorContext context);