1 package org.simantics.scl.compiler.errors;
3 import gnu.trove.list.array.TIntArrayList;
5 import java.io.IOException;
7 import java.io.StringReader;
9 public class CompilationErrorFormatter {
11 public static int[] rows(Reader reader) throws IOException {
12 TIntArrayList rows = new TIntArrayList();
18 int c = reader.read();
25 return rows.toArray();
28 public static int lineNumber(int[] rows, int location) {
31 if(location >= rows[rows.length-1])
34 int b = rows.length-1;
37 if(rows[c] <= location)
45 private static String locationToString(int[] rows, int location) {
48 if(location >= rows[rows.length-1])
49 return rows.length + ":" + (location - rows[rows.length-1] + 1);
51 int b = rows.length-1;
54 if(rows[c] <= location)
59 return (a+1) + ":" + (location - rows[a] + 1);
62 public static String toString(String source, CompilationError[] errors) {
63 return toString(new StringReader(source), errors);
66 public static String toString(Reader source, CompilationError[] errors) {
73 } catch(IOException e) {
76 StringBuilder b = new StringBuilder();
78 for(CompilationError error : errors) {
83 b.append(locationToString(rows, Locations.beginOf(error.location)) + "-" +
84 locationToString(rows, Locations.endOf(error.location)) + ": " +
90 public static String toString(CompilationError[] errors) {
91 return toString((Reader)null, errors);