X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Ferrors%2FCompilationErrorFormatter.java;fp=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Ferrors%2FCompilationErrorFormatter.java;h=bd29b328b6328df57e063268485852dd6dc3e377;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/errors/CompilationErrorFormatter.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/errors/CompilationErrorFormatter.java new file mode 100644 index 000000000..bd29b328b --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/errors/CompilationErrorFormatter.java @@ -0,0 +1,93 @@ +package org.simantics.scl.compiler.errors; + +import gnu.trove.list.array.TIntArrayList; + +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; + +public class CompilationErrorFormatter { + + public static int[] rows(Reader reader) throws IOException { + TIntArrayList rows = new TIntArrayList(); + rows.add(0); + + int i=0; + while(true) { + ++i; + int c = reader.read(); + if(c < 0) + break; + if(c == '\n') + rows.add(i); + } + reader.close(); + return rows.toArray(); + } + + public static int lineNumber(int[] rows, int location) { + if(location < 0) + return 1; + if(location >= rows[rows.length-1]) + return rows.length; + int a = 0; + int b = rows.length-1; + while(b-a > 1) { + int c = (a+b)/2; + if(rows[c] <= location) + a = c; + else + b = c; + } + return a+1; + } + + private static String locationToString(int[] rows, int location) { + if(location == -1) + return "?"; + if(location >= rows[rows.length-1]) + return rows.length + ":" + (location - rows[rows.length-1] + 1); + int a = 0; + int b = rows.length-1; + while(b-a > 1) { + int c = (a+b)/2; + if(rows[c] <= location) + a = c; + else + b = c; + } + return (a+1) + ":" + (location - rows[a] + 1); + } + + public static String toString(String source, CompilationError[] errors) { + return toString(new StringReader(source), errors); + } + + public static String toString(Reader source, CompilationError[] errors) { + int[] rows; + try { + if(source != null) + rows = rows(source); + else + rows = new int[] {0}; + } catch(IOException e) { + rows = new int[] {0}; + } + StringBuilder b = new StringBuilder(); + boolean first = true; + for(CompilationError error : errors) { + if(first) + first = false; + else + b.append('\n'); + b.append(locationToString(rows, Locations.beginOf(error.location)) + "-" + + locationToString(rows, Locations.endOf(error.location)) + ": " + + error.description); + } + return b.toString(); + } + + public static String toString(CompilationError[] errors) { + return toString((Reader)null, errors); + } +}