Java Code Examples for org.codehaus.groovy.syntax.SyntaxException#getStartColumn()

The following examples show how to use org.codehaus.groovy.syntax.SyntaxException#getStartColumn() . 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: StreamingTemplateEngine.java    From groovy with Apache License 2.0 5 votes vote down vote up
private RuntimeException mangleMultipleCompilationErrorsException(MultipleCompilationErrorsException e, List<StringSection> sections) {
    RuntimeException result = e;

    ErrorCollector collector = e.getErrorCollector();
    @SuppressWarnings({"unchecked"})
    List<Message> errors = (List<Message>) collector.getErrors();
    if (!errors.isEmpty()) {
        Message firstMessage = errors.get(0);
        if (firstMessage instanceof SyntaxErrorMessage) {
            @SuppressWarnings({"ThrowableResultOfMethodCallIgnored"})
            SyntaxException syntaxException = ((SyntaxErrorMessage) firstMessage).getCause();
            Position errorPosition = new Position(syntaxException.getLine(), syntaxException.getStartColumn());

            //find the string section which precedes the row/col of the thrown exception
            StringSection precedingSection = findPrecedingSection(errorPosition, sections);

            //and now use the string section to mangle the line numbers so that they refer to the
            //appropriate line in the source template data
            if (precedingSection != null) {
                //if the error was thrown on the same row as where the last string section
                //ended, fix column value
                offsetPositionFromSection(errorPosition, precedingSection);
                //the below being true indicates that we had an unterminated ${ or <% sequence and
                //the column is thus meaningless, we reset it to where the %{ or <% starts to at
                //least give the user a sporting chance
                if (sections.get(sections.size() - 1) == precedingSection) {
                    errorPosition.column = precedingSection.lastSourcePosition.column;
                }

                String message = mangleExceptionMessage(e.getMessage(), errorPosition);
                result = new TemplateParseException(message, e, errorPosition.row, errorPosition.column);
            }
        }
    }

    return result;
}
 
Example 2
Source File: GroovyParser.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void handleErrorCollector(ErrorCollector errorCollector, Context context,
        ModuleNode moduleNode, boolean ignoreErrors, Sanitize sanitizing) {

    if (!ignoreErrors && errorCollector != null) {
        List errors = errorCollector.getErrors();
        if (errors != null) {
            for (Object object : errors) {
                LOG.log(Level.FINEST, "Error found in collector: {0}", object);
                if (object instanceof SyntaxErrorMessage) {
                    SyntaxException ex = ((SyntaxErrorMessage)object).getCause();

                    String sourceLocator = ex.getSourceLocator();
                    String name = null;
                    if (moduleNode != null) {
                        name = moduleNode.getContext().getName();
                    } else if (context.snapshot.getSource().getFileObject() != null) {
                        name = context.snapshot.getSource().getFileObject().getNameExt();
                    }

                    if (sourceLocator != null && name != null && sourceLocator.equals(name)) {
                        int startLine = ex.getStartLine();
                        int startColumn = ex.getStartColumn();
                        int line = ex.getLine();
                        int endColumn = ex.getEndColumn();
                        // FIXME parsing API
                        int startOffset = 0;
                        int endOffset = 0;
                        if (context.document != null) {
                            startOffset = ASTUtils.getOffset(context.document, startLine > 0 ? startLine : 1, startColumn > 0 ? startColumn : 1);
                            endOffset = ASTUtils.getOffset(context.document, line > 0 ? line : 1, endColumn > 0 ? endColumn : 1);
                        }
                        notifyError(context, null, Severity.ERROR, ex.getMessage(), null, startOffset, endOffset, sanitizing);
                    }
                } else if (object instanceof SimpleMessage) {
                    String message = ((SimpleMessage)object).getMessage();
                    notifyError(context, null, Severity.ERROR, message, null, -1, sanitizing);
                } else {
                    notifyError(context, null, Severity.ERROR, "Error", null, -1, sanitizing);
                }
            }
        }
    }
}