33import com .github .tomaslanger .chalk .Chalk ;
44import java .io .File ;
55import java .io .FileNotFoundException ;
6+ import java .io .PrintStream ;
67import java .util .ArrayList ;
78import java .util .List ;
89import java .util .Scanner ;
10+ import org .piccode .backend .Compiler ;
11+ import org .piccode .piccodescript .ErrorAsciiKind ;
912
1013/**
1114 *
@@ -17,6 +20,7 @@ public class PiccodeWarning implements PiccodeInfo {
1720 public int line , col ;
1821 public String message ;
1922
23+ private PrintStream out = Compiler .out ;
2024 public Integer frame = null ;
2125
2226 private List <PiccodeInfo > notes = new ArrayList <>();
@@ -50,6 +54,36 @@ public void report(boolean die, String kind) {
5054 }
5155 }
5256
57+
58+ var errorKind = Compiler .errorKind ;
59+ if (errorKind == ErrorAsciiKind .GLEAM_STYLE ) {
60+ printGleamStyleError (kind , lines , fp );
61+ } else {
62+ printEmacsCompStyleError (kind , lines , fp );
63+ }
64+
65+ if (!notes .isEmpty ()) {
66+ System .out .println ((Chalk .on ("." ).yellow () + "\n " ).repeat (2 ));
67+ for (var note : notes ) {
68+ note .report (false , "INFO" );
69+ }
70+ }
71+ }
72+
73+ private static String [] toLines (File fp ) {
74+ List <String > lines = new ArrayList <>();
75+
76+ try (Scanner sc = new Scanner (fp )) {
77+ while (sc .hasNextLine ()) {
78+ lines .add (sc .nextLine ());
79+ }
80+ } catch (FileNotFoundException ex ) {
81+ }
82+ return lines .toArray (String []::new );
83+ }
84+
85+
86+ private void printGleamStyleError (String kind , String [] lines , File fp ) {
5387 var line_fmt = String .format (" %d │" , line );
5488 var gap = " " .repeat (line_fmt .length ());
5589 var gap2 = " " .repeat (line_fmt .length () - 1 );
@@ -59,14 +93,14 @@ public void report(boolean die, String kind) {
5993 ? "repl"
6094 : fp .getName (),
6195 line , col + 1 ,
62- kind == null || kind .equals ("WARNING " )
63- ? Chalk .on ("WARNING " ).yellow () // I hard code ERROR just in case it is null;
96+ kind == null || kind .equals ("ERROR " )
97+ ? Chalk .on ("ERROR " ).red () // I hard code ERROR just in case it is null;
6498 : Chalk .on (kind .toUpperCase ()).yellow (),
6599 message
66100 );
67101
68102 if (file == null ) {
69- System . out .println ("" + fmt );
103+ out .println ("" + fmt );
70104 return ;
71105 }
72106
@@ -75,34 +109,52 @@ public void report(boolean die, String kind) {
75109 index = 0 ;
76110 }
77111 var code_line = lines [index ].replaceAll ("\t " , " " .repeat (1 ));
78- System . out .println (gap2 + fmt );
79- System . out .println (line_fmt + " " + code_line );
112+ out .println (gap2 + fmt );
113+ out .println (line_fmt + " " + code_line );
80114 var tick = "─" .repeat (col + 1 ) + "╯" ;
81115 var tick2 = " " .repeat (col + 1 ) + "^" ;
82- System . out .println (gap2 + "│" + tick2 );
83- System . out .println (gap2 + "╰" + tick );
116+ out .println (gap2 + "│" + tick2 );
117+ out .println (gap2 + "╰" + tick );
84118 if (line + 1 < lines .length ) {
85119 line_fmt = String .format (" %d │" , line + 1 );
86- System .out .println (line_fmt );
87- }
88-
89- if (!notes .isEmpty ()) {
90- System .out .println ((Chalk .on ("." ).yellow () + "\n " ).repeat (2 ));
91- for (var note : notes ) {
92- note .report (false , "INFO" );
93- }
120+ out .println (line_fmt );
94121 }
95122 }
96123
97- private static String [] toLines (File fp ) {
98- List <String > lines = new ArrayList <>();
124+ private void printEmacsCompStyleError (String kind , String [] lines , File fp ) {
125+ var line_fmt = String .format (" %d │" , line );
126+ var gap2 = " " .repeat (line_fmt .length () - 1 );
127+ var fmt = String .format (
128+ "%s:%d:%d: %s: %s" ,
129+ file == null || file .equals ("repl" )
130+ ? "repl"
131+ : fp .getName (),
132+ line , col + 1 ,
133+ kind == null || kind .equals ("ERROR" )
134+ ? Chalk .on ("ERROR" ).red () // I hard code ERROR just in case it is null;
135+ : Chalk .on (kind .toUpperCase ()).yellow (),
136+ message
137+ );
99138
100- try (Scanner sc = new Scanner (fp )) {
101- while (sc .hasNextLine ()) {
102- lines .add (sc .nextLine ());
103- }
104- } catch (FileNotFoundException ex ) {
139+ if (file == null ) {
140+ out .println ("" + fmt );
141+ return ;
142+ }
143+
144+ var index = line - 1 < lines .length ? line - 1 : lines .length - 1 ;
145+ if (index < 0 ) {
146+ index = 0 ;
147+ }
148+ var code_line = lines [index ].replaceAll ("\t " , " " .repeat (1 ));
149+ out .println (fmt );
150+ out .println (line_fmt + " " + code_line );
151+ var tick2 = " " .repeat (col + 1 ) + "^" ;
152+ out .println (gap2 + " " + tick2 );
153+ if (line + 1 < lines .length ) {
154+ line_fmt = String .format (" %d │" , line + 1 );
155+ out .println (line_fmt );
105156 }
106- return lines .toArray (String []::new );
107157 }
158+
159+
108160}
0 commit comments