package org.ringojs.engine;

import org.ringojs.util.StringUtils;

public class SyntaxError {

    public final String message, sourceName, lineSource;
    public final int line, offset;

    SyntaxError(String message, String sourceName, int line, String lineSource, int offset) {
        this.message = message;
        this.sourceName = sourceName;
        this.lineSource = lineSource;
        this.line = line;
        this.offset = offset;
    }

    public String toString() {
        String lineSeparator = System.getProperty("line.separator", "\n");
        StringBuffer b = new StringBuffer(sourceName).append(", line ").append(line)
                .append(": ").append(message).append(lineSeparator)
                .append(lineSource).append(lineSeparator);
        for(int i = 0; i < offset - 1; i++) {
            b.append(' ');
        }
        b.append('^');
        return b.toString();
    }

    public String toHtml() {
        String lineSeparator = System.getProperty("line.separator", "\n");
        StringBuffer b = new StringBuffer("<div>").append(sourceName).append(", line ")
                .append(line).append(": <b>").append(StringUtils.escapeHtml(message))
                .append("</b></div>").append(lineSeparator).append("<pre>");
        int srcLength = lineSource.length();
        int errorStart = Math.max(0, offset - 2);
        int errorEnd = Math.min(srcLength, offset);
        b.append(StringUtils.escapeHtml(lineSource.substring(0, errorStart)))
                .append("<span style='border-bottom: 3px solid red;'>")
                .append(StringUtils.escapeHtml(lineSource.substring(errorStart, errorEnd)))
                .append("</span>")
                .append(StringUtils.escapeHtml(lineSource.substring(errorEnd)))
                .append(lineSeparator).append("</pre>");
        return b.toString();
    }

}

@@ -332,16 +332,58 @@
 ceName =
+ sourceName == null ? %22%5Bunknown source%5D%22 :
  sourceN
@@ -387,16 +387,16 @@
 ceName;%0A
-
         
@@ -602,34 +602,35 @@
         StringBu
-ff
+ild
 er b = new Strin
@@ -632,18 +632,19 @@
 StringBu
-ff
+ild
 er(sourc
@@ -667,29 +667,16 @@
  line %22)
-.append(line)
 %0A       
@@ -676,32 +676,45 @@
                 
+.append(line)
 .append(%22: %22).ap
@@ -1070,18 +1070,19 @@
 StringBu
-ff
+ild
 er b = n
@@ -1092,18 +1092,19 @@
 StringBu
-ff
+ild
 er(%22%3Cdiv
@@ -1117,32 +1117,49 @@
 pend(sourceName)
+%0A                
 .append(%22, line 
@@ -1160,33 +1160,16 @@
  line %22)
-%0A                
 .append(
@@ -1189,16 +1189,33 @@
 %22: %3Cb%3E%22)
+%0A                
 .append(
@@ -1246,33 +1246,16 @@
 essage))
-%0A                
 .append(
@@ -1263,24 +1263,41 @@
 %3C/b%3E%3C/div%3E%22)
+%0A                
 .append(line

Handle null for SyntaxError sourceName (Rhino bug)