org.eclipse.xtext.nodemodel.SyntaxErrorMessage Java Examples
The following examples show how to use
org.eclipse.xtext.nodemodel.SyntaxErrorMessage.
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: SARLSyntaxErrorMessageProvider.java From sarl with Apache License 2.0 | 6 votes |
@Override public SyntaxErrorMessage getSyntaxErrorMessage(IParserErrorContext context) { if (context != null) { final RecognitionException recognitionException = context.getRecognitionException(); if (recognitionException != null && recognitionException.token != null) { final String text = recognitionException.token.getText(); if (!Strings.isEmpty(text)) { if (this.grammarAccess.isPureKeyword(text)) { final String protectedText = this.converter.getQualifiedNameValueConverter().toString(text); return new SyntaxErrorMessage( MessageFormat.format(Messages.SARLSyntaxErrorMessageProvider_0, text, protectedText), SyntaxIssueCodes.USED_RESERVED_KEYWORD, new String[] {text, protectedText}); } else if (this.grammarAccess.isKeyword(text)) { return new SyntaxErrorMessage( MessageFormat.format(Messages.SARLSyntaxErrorMessageProvider_1, text), SyntaxIssueCodes.USED_RESERVED_KEYWORD); } } } } return super.getSyntaxErrorMessage(context); }
Example #2
Source File: N4LanguageUtils.java From n4js with Eclipse Public License 1.0 | 6 votes |
/** * Parses the given string with the parser of the Xtext language denoted by the given file extension. In case of * syntax errors, the returned parse result will have a non-empty list of {@link ParseResult#errors}. */ public static <T extends EObject> ParseResult<T> parseXtextLanguage(String fileExtOfLanguage, ParserRule parserRuleOrNull, Class<T> expectedTypeOfRoot, Reader sourceReader) { final IParser parser = getServiceForContext(fileExtOfLanguage, IParser.class) .orElseThrow(() -> new RuntimeException( "Cannot obtain Xtext parser for language with file extension: " + fileExtOfLanguage)); final IParseResult result; if (parserRuleOrNull != null) { result = parser.parse(parserRuleOrNull, sourceReader); } else { result = parser.parse(sourceReader); } final Iterable<SyntaxErrorMessage> errors = Iterables.transform(result.getSyntaxErrors(), node -> node.getSyntaxErrorMessage()); final EObject root = result.getRootASTElement(); if (root != null && expectedTypeOfRoot.isInstance(root)) { final T rootCasted = expectedTypeOfRoot.cast(root); return new ParseResult<>(rootCasted, errors); } return new ParseResult<>(null, errors); }
Example #3
Source File: SyntaxErrorMessageProvider.java From statecharts with Eclipse Public License 1.0 | 6 votes |
@Override public SyntaxErrorMessage getSyntaxErrorMessage(IParserErrorContext context) { String defaultMessage = context.getDefaultMessage(); if (!defaultMessage.contains(SyntaxErrorMessages.MISSING_EOF)) { return new SyntaxErrorMessage(defaultMessage, SYNTAX_DIAGNOSTIC); } RecognitionException recognitionException = context.getRecognitionException(); String message = "Failed to parse Expression."; boolean lexicalAnalysisFailed = recognitionException == null; if (!lexicalAnalysisFailed) { message = "Failed to parse Expression '" + recognitionException.token.getText() + "'."; } return new SyntaxErrorMessage(message, SYNTAX_DIAGNOSTIC); }
Example #4
Source File: InternalSemicolonInjectingParser.java From n4js with Eclipse Public License 1.0 | 6 votes |
/** * 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); } }
Example #5
Source File: NodeModelBuilder.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
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; }
Example #6
Source File: CompositeSyntaxErrorMessages.java From n4js with Eclipse Public License 1.0 | 6 votes |
/** * Unpack the encoded nested messages or process with the default logic. */ public static void toDiagnostic(SyntaxErrorMessage syntaxErrorMessage, Consumer<? super SyntaxErrorMessage> delegate) { if (CODE.equals(syntaxErrorMessage.getIssueCode())) { String[] encodedMessages = syntaxErrorMessage.getIssueData(); for (int messageIndex = 0; messageIndex < encodedMessages.length; messageIndex += 3) { String message = encodedMessages[messageIndex]; String issueCode = encodedMessages[messageIndex + 1]; String encodedIssueData = encodedMessages[messageIndex + 2]; String[] issueData = "".equals(encodedIssueData) ? null : Strings.unpack(encodedIssueData); delegate.accept(new SyntaxErrorMessage(message, issueCode, issueData)); } } else { delegate.accept(syntaxErrorMessage); } }
Example #7
Source File: CompositeSyntaxErrorMessages.java From n4js with Eclipse Public License 1.0 | 6 votes |
/** * Convert the given N4JSValueConverterWithValueException to a SyntaxErrorMessage. */ public static SyntaxErrorMessage toSyntaxErrorMessage( N4JSValueConverterWithValueException vce, Function<? super N4JSValueConverterWithValueException, ? extends SyntaxErrorMessage> delegate) { if (vce.getSuppressed().length == 0) { return delegate.apply(vce); } List<String> encoded = new ArrayList<>(); SyntaxErrorMessage first = delegate.apply(vce); encode(first, encoded); for (Throwable t : vce.getSuppressed()) { if (t instanceof N4JSValueConverterWithValueException) { SyntaxErrorMessage next = delegate.apply((N4JSValueConverterWithValueException) t); encode(next, encoded); } } return new SyntaxErrorMessage(first.getMessage(), CODE, encoded.toArray(String[]::new)); }
Example #8
Source File: SerializationUtil.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
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; }
Example #9
Source File: AbstractInternalAntlrParser.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
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); }
Example #10
Source File: Bug299237Test.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@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); }
Example #11
Source File: Bug299237Test.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@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); }
Example #12
Source File: UtilN4.java From n4js with Eclipse Public License 1.0 | 5 votes |
/** * {@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; }
Example #13
Source File: ErrorMessagesTest.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@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); }
Example #14
Source File: FormatterTestHelper.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
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()); } }
Example #15
Source File: XtextResource.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
/** * @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)); }
Example #16
Source File: CardinalityAwareSyntaxErrorMessageProvider.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@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); }
Example #17
Source File: GrammarResource.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
/** * @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); } }
Example #18
Source File: SyntaxErrorMessageProvider.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
@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); }
Example #19
Source File: SerializationUtil.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
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()); } }
Example #20
Source File: RichStringFormatter.java From xtext-xtend with Eclipse Public License 2.0 | 5 votes |
protected boolean _hasSyntaxError(final NodeEObjectRegion region) { final BidiTreeIterator<INode> i = region.getNode().getAsTreeIterable().iterator(); while (i.hasNext()) { SyntaxErrorMessage _syntaxErrorMessage = i.next().getSyntaxErrorMessage(); boolean _tripleNotEquals = (_syntaxErrorMessage != null); if (_tripleNotEquals) { return true; } } return false; }
Example #21
Source File: FormatterTester.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
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()); } }
Example #22
Source File: NodeModelTokenSource.java From n4js with Eclipse Public License 1.0 | 5 votes |
/** * 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 = Collections.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()); }
Example #23
Source File: FormatterTester.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
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()); } }
Example #24
Source File: CompositeSyntaxErrorMessages.java From n4js with Eclipse Public License 1.0 | 5 votes |
private static void encode(SyntaxErrorMessage message, List<String> result) { result.add(message.getMessage()); result.add(message.getIssueCode()); String[] issueData = message.getIssueData(); if (issueData != null && issueData.length > 0) { result.add(Strings.pack(issueData)); } else { result.add(""); } }
Example #25
Source File: N4JSResource.java From n4js with Eclipse Public License 1.0 | 5 votes |
private boolean isRangeBased(SyntaxErrorMessage syntaxErrorMessage) { String issueCode = syntaxErrorMessage.getIssueCode(); return SYNTAX_DIAGNOSTIC_WITH_RANGE.equals(issueCode) || RegExLiteralConverter.ISSUE_CODE.equals(issueCode) || IssueCodes.VCO_REGEX_NAMED_GROUP.equals(issueCode) || IssueCodes.VCO_REGEX_ILLEGAL_ESCAPE.equals(issueCode) || (IssueCodes.VCO_TEMPLATE_QUOTE.equals(issueCode) && syntaxErrorMessage.getIssueData() != null); }
Example #26
Source File: SyntaxErrorMessageProvider.java From statecharts with Eclipse Public License 1.0 | 5 votes |
@Override public SyntaxErrorMessage getSyntaxErrorMessage(IValueConverterErrorContext context) { ValueConverterException cause = context.getValueConverterException(); if (cause instanceof ValueConverterException) { return new SyntaxErrorMessage(cause.getMessage(), SYNTAX_DIAGNOSTIC); } return super.getSyntaxErrorMessage(context); }
Example #27
Source File: BadEscapementAwareMessageProvider.java From n4js with Eclipse Public License 1.0 | 5 votes |
@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 N4JSValueConverterWithValueException) { return CompositeSyntaxErrorMessages.toSyntaxErrorMessage((N4JSValueConverterWithValueException) cause, (vce) -> { if (vce instanceof RegExLiteralConverter.BogusRegExLiteralException) { return createRangedSyntaxErrorMessage(vce.getMessage(), RegExLiteralConverter.ISSUE_CODE, vce.getOffset(), vce.getLength()); } String issueCode = vce.getIssueCode(); if (vce.hasRange()) { return createRangedSyntaxErrorMessage(vce.getMessage(), issueCode, vce.getOffset(), vce.getLength()); } return new SyntaxErrorMessage(vce.getMessage(), issueCode); }); } return super.getSyntaxErrorMessage(context); }
Example #28
Source File: InternalSemicolonInjectingParser.java From n4js with Eclipse Public License 1.0 | 5 votes |
private void setCurrentError(SyntaxErrorMessage syntaxErrorMessage) { try { reflectCurrentError.set(this, syntaxErrorMessage); } catch (Exception e) { throw new RuntimeException(); } }
Example #29
Source File: InternalSemicolonInjectingParser.java From n4js with Eclipse Public License 1.0 | 5 votes |
/** * <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); } }
Example #30
Source File: LeafNodeWithSyntaxError.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
@Override public SyntaxErrorMessage getSyntaxErrorMessage() { return syntaxErrorMessage; }