@@ -7,26 +7,35 @@ const LINE_REGEX = /(.*)(\r?\n)/;
7
7
8
8
export class ArduinoParser extends MIParser {
9
9
10
+ protected rejectReady ?: ( error : Error ) => void ;
11
+
10
12
parseFull ( proc : ChildProcessWithoutNullStreams ) : Promise < void > {
11
13
return new Promise ( ( resolve , reject ) => {
14
+ // Detect errors when the child process could not be spawned
12
15
proc . on ( 'error' , reject ) ;
13
- let ready = false ;
14
- proc . on ( 'exit' , ( ) => {
15
- if ( ! ready ) {
16
- reject ( new Error ( 'The gdb debugger terminated unexpectedly.' ) ) ;
17
- }
18
- } ) ;
19
-
16
+ // Detect hanging process (does not print command prompt or error)
20
17
const timeout = setTimeout ( ( ) => {
21
18
reject ( new Error ( `No response from gdb after ${ READY_TIMEOUT } ms.` ) ) ;
22
19
} , READY_TIMEOUT ) ;
20
+
23
21
this . waitReady = ( ) => {
24
- ready = true ;
22
+ this . rejectReady = undefined ;
25
23
clearTimeout ( timeout ) ;
26
24
resolve ( ) ;
27
25
}
26
+ this . rejectReady = ( error : Error ) => {
27
+ this . waitReady = undefined ;
28
+ clearTimeout ( timeout ) ;
29
+ reject ( error ) ;
30
+ }
31
+ // Detect unexpected termination
32
+ proc . on ( 'exit' , ( ) => {
33
+ if ( this . rejectReady ) {
34
+ this . rejectReady ( new Error ( 'The gdb debugger terminated unexpectedly.' ) ) ;
35
+ }
36
+ } ) ;
28
37
this . readInputStream ( proc . stdout ) ;
29
- this . readErrorStream ( proc . stderr , reject ) ;
38
+ this . readErrorStream ( proc . stderr ) ;
30
39
} ) ;
31
40
}
32
41
@@ -36,25 +45,36 @@ export class ArduinoParser extends MIParser {
36
45
buff += chunk . toString ( ) ;
37
46
let regexArray = LINE_REGEX . exec ( buff ) ;
38
47
while ( regexArray ) {
39
- this . line = regexArray [ 1 ] ;
48
+ const line = regexArray [ 1 ] ;
49
+ this . line = line ;
40
50
this . pos = 0 ;
41
51
this . handleLine ( ) ;
52
+ // Detect error emitted as log message
53
+ if ( line . startsWith ( '&"error' ) && this . rejectReady ) {
54
+ this . line = line ;
55
+ this . pos = 0 ;
56
+ this . next ( ) ;
57
+ this . rejectReady ( new Error ( this . handleCString ( ) || regexArray [ 1 ] ) ) ;
58
+ this . rejectReady = undefined ;
59
+ }
42
60
buff = buff . substring ( regexArray [ 1 ] . length + regexArray [ 2 ] . length ) ;
43
61
regexArray = LINE_REGEX . exec ( buff ) ;
44
62
}
45
63
} ) ;
46
64
}
47
65
48
- private readErrorStream ( stream : Readable , reject : ( reason ?: any ) => void ) {
66
+ private readErrorStream ( stream : Readable ) {
49
67
let buff = '' ;
50
68
stream . on ( 'data' , chunk => {
51
69
buff += chunk . toString ( ) ;
52
70
let regexArray = LINE_REGEX . exec ( buff ) ;
53
71
while ( regexArray ) {
54
72
const line = regexArray [ 1 ] ;
55
73
this . gdb . emit ( 'consoleStreamOutput' , line + '\n' , 'stderr' ) ;
56
- if ( line . toLowerCase ( ) . startsWith ( 'error' ) ) {
57
- reject ( new Error ( line ) ) ;
74
+ // Detect error emitted on the stderr stream
75
+ if ( line . toLowerCase ( ) . startsWith ( 'error' ) && this . rejectReady ) {
76
+ this . rejectReady ( new Error ( line ) ) ;
77
+ this . rejectReady = undefined ;
58
78
}
59
79
buff = buff . substring ( regexArray [ 1 ] . length + regexArray [ 2 ] . length ) ;
60
80
regexArray = LINE_REGEX . exec ( buff ) ;
0 commit comments