001    package org.codehaus.groovy.control.messages;
002    
003    import java.io.PrintWriter;
004    
005    import org.codehaus.groovy.control.Janitor;
006    import org.codehaus.groovy.control.SourceUnit;
007    import org.codehaus.groovy.syntax.SyntaxException;
008    
009    
010    /**
011     * A class for error messages produced by the parser system.
012     *
013     * @author <a href="mailto:cpoirier@dreaming.org">Chris Poirier</a>
014     * @version $Id: SyntaxErrorMessage.java,v 1.4 2005/06/10 09:55:30 cstein Exp $
015     */
016    
017    public class SyntaxErrorMessage extends Message {
018        protected SyntaxException cause = null;
019        protected SourceUnit source;
020        
021        public SyntaxErrorMessage(SyntaxException cause, SourceUnit source) {
022            this.cause = cause;
023            this.source = source;
024        }
025    
026    
027        /**
028         * Returns the underlying SyntaxException.
029         */
030    
031        public SyntaxException getCause() {
032            return this.cause;
033        }
034    
035    
036        /**
037         * Writes out a nicely formatted summary of the syntax error.
038         */
039    
040        public void write(PrintWriter output, Janitor janitor) {
041            String name = source.getName();
042            int line = getCause().getStartLine();
043            int column = getCause().getStartColumn();
044            String sample = source.getSample(line, column, janitor);
045    
046            output.print(name + ": " + line + ": " + getCause().getMessage());
047            if (sample != null) {
048                output.println();
049                output.print(sample);
050                output.println();
051            }
052        }
053    
054    
055    }
056    
057    
058