1616
1717package com .duy .ccppcompiler .compiler .diagnostic ;
1818
19+ import android .support .annotation .NonNull ;
20+ import android .support .annotation .Nullable ;
21+
22+ import java .io .LineNumberReader ;
23+ import java .io .StringReader ;
1924import java .util .regex .Matcher ;
2025import java .util .regex .Pattern ;
2126
2833 */
2934
3035public class OutputParser {
31- public static final Pattern PATTERN = Pattern .compile (
32- "(.*):" + /*File name */
36+ public static final Pattern DIAGNOSTICS_PATTERN = Pattern .compile (
37+ "(.*):" + /*File path */
3338 "([0-9]+):" + /*Line*/
3439 "([0-9]+):" + /*Col*/
3540 "(\\ s+.*:\\ s+)" + /*Type*/
36- "(.*)" /*Message*/
37- , Pattern .CASE_INSENSITIVE );
41+ "(.*)" /*Message*/ );
42+
43+ //fix-it:"/storage/emulated/0/examples/simple/bit_print.c":{6:7-6:11}:"printf"
44+ public static final Pattern FIX_IT_PATTERN = Pattern .compile (
45+ "(fix-it):" +/*prefix*/
46+ "(.*):" +/*File path*/
47+ "\\ {([0-9]+):([0-9]+)-([0-9]+):([0-9]+)}" + /*Index (line:col)-(line:col)*/
48+ "\" (.*)\" " /*Message*/
49+ );
3850
3951 private DiagnosticsCollector diagnosticsCollector ;
4052
@@ -44,21 +56,56 @@ public OutputParser(DiagnosticsCollector diagnosticsCollector) {
4456
4557 @ SuppressWarnings ("unchecked" )
4658 public void parse (String inputData ) {
47- Matcher matcher = PATTERN .matcher (inputData );
48- while (matcher .find ()) {
49- try {
50- String file = matcher .group (1 );
51- int line = Integer .parseInt (matcher .group (2 ));
52- int col = Integer .parseInt (matcher .group (3 ));
59+ try {
60+ StringReader stringReader = new StringReader (inputData );
61+ LineNumberReader lineNumberReader = new LineNumberReader (stringReader );
62+
63+ String line ;
64+ while ((line = lineNumberReader .readLine ()) != null ) {
65+ processLine (line , lineNumberReader .readLine ());
66+ }
67+
68+ } catch (Exception e ) {
69+ //should not happened
70+ e .printStackTrace ();
71+ }
72+ }
73+
74+ @ SuppressWarnings ("unchecked" )
75+ private void processLine (@ NonNull String line , @ Nullable String nextLine ) {
76+ try {
77+ Matcher matcher = DIAGNOSTICS_PATTERN .matcher (line );
78+ if (matcher .find ()) {
79+ String filePath = matcher .group (1 );
80+ int lineNumber = Integer .parseInt (matcher .group (2 ));
81+ int colNumber = Integer .parseInt (matcher .group (3 ));
5382 Kind type = DiagnosticFactory .createType (matcher .group (4 ));
5483 String message = matcher .group (5 );
5584
56- Diagnostic diagnostic = DiagnosticFactory .create (type , file , line , col , message );
85+ Diagnostic diagnostic = DiagnosticFactory .create (type , filePath , lineNumber , colNumber , message );
86+ diagnosticsCollector .report (diagnostic );
87+ return ;
88+ }
89+ if (nextLine == null ) {
90+ return ;
91+ }
92+
93+ matcher = FIX_IT_PATTERN .matcher (line );
94+ if (matcher .find ()) {
95+ String filePath = matcher .group (2 );
96+ int lineStart = Integer .parseInt (matcher .group (3 ));
97+ int colStart = Integer .parseInt (matcher .group (4 ));
98+ int lineEnd = Integer .parseInt (matcher .group (5 ));
99+ int colEnd = Integer .parseInt (matcher .group (6 ));
100+ String suggestion = matcher .group (7 );
101+ Diagnostic diagnostic = DiagnosticFactory .createFixIt (filePath , lineStart , colStart , lineEnd , colEnd , suggestion );
57102 diagnosticsCollector .report (diagnostic );
58- } catch (Exception e ) {
59- //should not happened
60- e .printStackTrace ();
103+ } else {
104+ processLine (nextLine , null );
61105 }
106+ } catch (Exception e ) {
107+ e .printStackTrace ();
108+ //should not happend
62109 }
63110 }
64111}
0 commit comments