@@ -70,53 +70,74 @@ const parseLine = (() => {
7070 const re3 = new RegExp ( / \s + / g) ;
7171 return ( line => line . replace ( re1 , '' ) . replace ( re2 , '' ) . replace ( re3 , '' ) ) ;
7272 } ) ( ) ;
73- const re = / ( [ a - z A - Z ] [ 0 - 9 \+ \- \. ] * ) | ( \* [ 0 - 9 ] + ) | ( \$ [ a - z A - Z 0 - 9 $ # ] * ) / igm;
73+ const re = / ( % [ a - z A - Z ] [ a - z A - Z 0 - 9 _ ] * ) | ( [ a - z A - Z ] [ 0 - 9 \+ \- \. ] * ) | ( \* [ 0 - 9 ] + ) | ( (?: \$ \$ ) | (?: \$ [ a - z A - Z 0 - 9 # ] * ) ) / igm;
7474
7575 return ( line , options ) => {
7676 options = options || { } ;
77- options . noParseLine = options . noParseLine || false ;
77+ options . flatten = ! ! options . flatten ;
78+ options . noParseLine = ! ! options . noParseLine ;
7879
7980 const result = {
8081 line : line
8182 } ;
8283
83- if ( ! options . noParseLine ) {
84- result . words = [ ] ;
85-
86- let ln ; // Line number
87- let cs ; // Checksum
88- const words = stripComments ( line ) . match ( re ) || [ ] ;
89- for ( let i = 0 ; i < words . length ; ++ i ) {
90- const word = words [ i ] ;
91- const letter = word [ 0 ] . toUpperCase ( ) ;
92- const argument = word . slice ( 1 ) ;
93-
94- // $: Grbl-specific commands
95- if ( letter === '$' ) {
96- continue ;
97- }
98-
99- // N: Line number
100- if ( letter === 'N' && typeof ln === 'undefined' ) {
101- // Line (block) number in program
102- ln = Number ( argument ) ;
103- continue ;
104- }
105-
106- // *: Checksum
107- if ( letter === '*' && typeof cs === 'undefined' ) {
108- cs = Number ( argument ) ;
109- continue ;
110- }
84+ if ( options . noParseLine ) {
85+ return result ;
86+ }
11187
112- result . words . push ( [ letter , Number ( argument ) ] ) ;
88+ result . words = [ ] ;
89+
90+ let ln ; // Line number
91+ let cs ; // Checksum
92+ const words = stripComments ( line ) . match ( re ) || [ ] ;
93+
94+ for ( let i = 0 ; i < words . length ; ++ i ) {
95+ const word = words [ i ] ;
96+ const letter = word [ 0 ] . toUpperCase ( ) ;
97+ const argument = word . slice ( 1 ) ;
98+
99+ // Parse % commands for bCNC and CNCjs
100+ // - %wait Wait until the planner queue is empty
101+ if ( letter === '%' ) {
102+ result . cmds = ( result . cmds || [ ] ) . concat ( `${ letter } ${ argument } ` ) ;
103+ continue ;
104+ }
105+
106+ // Parse $ commands for Grbl
107+ // - $C Check gcode mode
108+ // - $H Run homing cycle
109+ if ( letter === '$' ) {
110+ result . cmds = ( result . cmds || [ ] ) . concat ( `${ letter } ${ argument } ` ) ;
111+ continue ;
113112 }
114113
115- ( typeof ( ln ) !== 'undefined' ) && ( result . ln = ln ) ; // Line number
116- ( typeof ( cs ) !== 'undefined' ) && ( result . cs = cs ) ; // Checksum
117- if ( result . cs && ( computeChecksum ( line ) !== result . cs ) ) {
118- result . err = true ; // checksum failed
114+ // N: Line number
115+ if ( letter === 'N' && typeof ln === 'undefined' ) {
116+ // Line (block) number in program
117+ ln = Number ( argument ) ;
118+ continue ;
119119 }
120+
121+ // *: Checksum
122+ if ( letter === '*' && typeof cs === 'undefined' ) {
123+ cs = Number ( argument ) ;
124+ continue ;
125+ }
126+
127+ if ( options . flatten ) {
128+ result . words . push ( `${ letter } ${ argument } ` ) ;
129+ } else {
130+ result . words . push ( [ letter , Number ( argument ) ] ) ;
131+ }
132+ }
133+
134+ // Line number
135+ ( typeof ( ln ) !== 'undefined' ) && ( result . ln = ln ) ;
136+
137+ // Checksum
138+ ( typeof ( cs ) !== 'undefined' ) && ( result . cs = cs ) ;
139+ if ( result . cs && ( computeChecksum ( line ) !== result . cs ) ) {
140+ result . err = true ; // checksum failed
120141 }
121142
122143 return result ;
@@ -184,7 +205,7 @@ const parseString = (str, options, callback = noop) => {
184205} ;
185206
186207const parseStringSync = ( str , options ) => {
187- const { noParseLine = false } = { ...options } ;
208+ const { flatten = false , noParseLine = false } = { ...options } ;
188209 const results = [ ] ;
189210 const lines = str . split ( '\n' ) ;
190211
@@ -193,7 +214,10 @@ const parseStringSync = (str, options) => {
193214 if ( line . length === 0 ) {
194215 continue ;
195216 }
196- const result = parseLine ( line , { noParseLine } ) ;
217+ const result = parseLine ( line , {
218+ flatten,
219+ noParseLine
220+ } ) ;
197221 results . push ( result ) ;
198222 }
199223
@@ -218,6 +242,7 @@ class GCodeLineStream extends Transform {
218242
219243 // @param {object } [options] The options object
220244 // @param {number } [options.batchSize] The batch size.
245+ // @param {boolean } [options.flatten] True to flatten the array, false otherwise.
221246 // @param {boolean } [options.noParseLine] True to not parse line, false otherwise.
222247 constructor ( options = { } ) {
223248 super ( { objectMode : true } ) ;
@@ -271,6 +296,7 @@ class GCodeLineStream extends Transform {
271296 line = line . trim ( ) ;
272297 if ( line . length > 0 ) {
273298 const result = parseLine ( line , {
299+ flatten : this . options . flatten ,
274300 noParseLine : this . options . noParseLine
275301 } ) ;
276302 this . push ( result ) ;
@@ -282,6 +308,7 @@ class GCodeLineStream extends Transform {
282308 const line = this . lineBuffer . trim ( ) ;
283309 if ( line . length > 0 ) {
284310 const result = parseLine ( line , {
311+ flatten : this . options . flatten ,
285312 noParseLine : this . options . noParseLine
286313 } ) ;
287314 this . push ( result ) ;
0 commit comments