]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/errors/CompilationErrorFormatter.java
bd29b328b6328df57e063268485852dd6dc3e377
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / errors / CompilationErrorFormatter.java
1 package org.simantics.scl.compiler.errors;
2
3 import gnu.trove.list.array.TIntArrayList;
4
5 import java.io.IOException;
6 import java.io.Reader;
7 import java.io.StringReader;
8
9 public class CompilationErrorFormatter {
10
11     public static int[] rows(Reader reader) throws IOException {
12         TIntArrayList rows = new TIntArrayList();
13         rows.add(0);
14         
15         int i=0;
16         while(true) {
17             ++i;
18             int c = reader.read();
19             if(c < 0)
20                 break;
21             if(c == '\n')
22                 rows.add(i);
23         }
24         reader.close();
25         return rows.toArray();
26     }
27     
28     public static int lineNumber(int[] rows, int location) {
29         if(location < 0)
30             return 1;
31         if(location >= rows[rows.length-1])
32             return rows.length;
33         int a = 0;
34         int b = rows.length-1;
35         while(b-a > 1) {
36             int c = (a+b)/2;
37             if(rows[c] <= location)
38                 a = c;
39             else
40                 b = c;
41         }
42         return a+1;
43     }
44     
45     private static String locationToString(int[] rows, int location) {
46         if(location == -1)
47             return "?";
48         if(location >= rows[rows.length-1])
49             return rows.length + ":" + (location - rows[rows.length-1] + 1);
50         int a = 0;
51         int b = rows.length-1;
52         while(b-a > 1) {
53             int c = (a+b)/2;
54             if(rows[c] <= location)
55                 a = c;
56             else
57                 b = c;
58         }
59         return (a+1) + ":" + (location - rows[a] + 1);
60     }
61     
62     public static String toString(String source, CompilationError[] errors) {
63         return toString(new StringReader(source), errors);
64     }
65     
66     public static String toString(Reader source, CompilationError[] errors) {
67         int[] rows;
68         try {
69             if(source != null)
70                 rows = rows(source);
71             else
72                 rows = new int[] {0};
73         } catch(IOException e) {
74             rows = new int[] {0};
75         }
76         StringBuilder b = new StringBuilder();
77         boolean first = true;
78         for(CompilationError error : errors) {
79             if(first)
80                 first = false;
81             else
82                 b.append('\n');
83             b.append(locationToString(rows, Locations.beginOf(error.location)) + "-" + 
84                     locationToString(rows, Locations.endOf(error.location)) + ": " +
85                     error.description);
86         }
87         return b.toString();
88     }
89
90     public static String toString(CompilationError[] errors) {
91         return toString((Reader)null, errors);
92     }
93 }