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 2392 2005-07-01 03:01:19Z fraz $
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            cause.setSourceLocator(source.getName());
025        }
026    
027    
028        /**
029         * Returns the underlying SyntaxException.
030         */
031    
032        public SyntaxException getCause() {
033            return this.cause;
034        }
035    
036    
037        /**
038         * Writes out a nicely formatted summary of the syntax error.
039         */
040    
041        public void write(PrintWriter output, Janitor janitor) {
042            String name = source.getName();
043            int line = getCause().getStartLine();
044            int column = getCause().getStartColumn();
045            String sample = source.getSample(line, column, janitor);
046    
047            output.print(name + ": " + line + ": " + getCause().getMessage());
048            if (sample != null) {
049                output.println();
050                output.print(sample);
051                output.println();
052            }
053        }
054    
055    
056    }
057    
058    
059