@@ -244,44 +244,65 @@ impl Executor {
244
244
245
245
let mut syntax = Vec :: new ( ) ; // Token string
246
246
let mut buffer = String :: new ( ) ; // Temporary storage
247
- let mut in_brackets = 0 ; // String's nest structure
248
- let mut in_parentheses = 0 ; // List's nest structure
249
- let mut in_hash = false ; // Is it Comment
247
+ let mut brackets = 0 ; // String's nest structure
248
+ let mut parentheses = 0 ; // List's nest structure
249
+ let mut hash = false ; // Is it Comment
250
+ let mut escape = false ; // Flag to indicate next character is escaped
250
251
251
252
for c in code. chars ( ) {
252
253
match c {
253
- '(' => {
254
- in_brackets += 1 ;
254
+ '\\' if !escape => {
255
+ escape = true ;
256
+ }
257
+ '(' if !hash && !escape => {
258
+ brackets += 1 ;
255
259
buffer. push ( '(' ) ;
256
260
}
257
- ')' => {
258
- in_brackets -= 1 ;
261
+ ')' if !hash && !escape => {
262
+ brackets -= 1 ;
259
263
buffer. push ( ')' ) ;
260
264
}
261
- '#' if !in_hash => {
262
- in_hash = true ;
265
+ '#' if !hash && !escape => {
266
+ hash = true ;
263
267
buffer. push ( '#' ) ;
264
268
}
265
- '#' if in_hash => {
266
- in_hash = false ;
269
+ '#' if hash && !escape => {
270
+ hash = false ;
267
271
buffer. push ( '#' ) ;
268
272
}
269
- '[' if in_brackets == 0 => {
270
- in_parentheses += 1 ;
273
+ '[' if !hash && brackets == 0 && !escape => {
274
+ parentheses += 1 ;
271
275
buffer. push ( '[' ) ;
272
276
}
273
- ']' if in_brackets == 0 => {
274
- in_parentheses -= 1 ;
277
+ ']' if !hash && brackets == 0 && !escape => {
278
+ parentheses -= 1 ;
275
279
buffer. push ( ']' ) ;
276
280
}
277
- ' ' if !in_hash && in_parentheses == 0 && in_brackets == 0 => {
281
+ ' ' if !hash && parentheses == 0 && brackets == 0 && !escape => {
278
282
if !buffer. is_empty ( ) {
279
283
syntax. push ( buffer. clone ( ) ) ;
280
284
buffer. clear ( ) ;
281
285
}
282
286
}
283
287
_ => {
284
- buffer. push ( c) ;
288
+ if parentheses == 0 && brackets == 0 && !hash {
289
+ if escape {
290
+ buffer. push ( match c {
291
+ 'n' => '\n' ,
292
+ 't' => '\t' ,
293
+ 'r' => '\r' ,
294
+ _ => c,
295
+ } )
296
+ } else {
297
+ buffer. push ( c) ;
298
+ }
299
+ } else {
300
+ if escape {
301
+ buffer. push ( '\\' ) ;
302
+ }
303
+ buffer. push ( c) ;
304
+ }
305
+ escape = false ; // Reset escape flag for non-escape characters
285
306
}
286
307
}
287
308
}
@@ -314,8 +335,67 @@ impl Executor {
314
335
self . stack . push ( Type :: Bool ( token. parse ( ) . unwrap_or ( true ) ) ) ;
315
336
} else if chars[ 0 ] == '(' && chars[ chars. len ( ) - 1 ] == ')' {
316
337
// Push string value on the stack
317
- self . stack
318
- . push ( Type :: String ( token[ 1 ..token. len ( ) - 1 ] . to_string ( ) ) ) ;
338
+ let string = {
339
+ let mut buffer = String :: new ( ) ; // Temporary storage
340
+ let mut brackets = 0 ; // String's nest structure
341
+ let mut parentheses = 0 ; // List's nest structure
342
+ let mut hash = false ; // Is it Comment
343
+ let mut escape = false ; // Flag to indicate next character is escaped
344
+
345
+ for c in token[ 1 ..token. len ( ) - 1 ] . to_string ( ) . chars ( ) {
346
+ match c {
347
+ '\\' if !escape => {
348
+ escape = true ;
349
+ }
350
+ '(' if !hash && !escape => {
351
+ brackets += 1 ;
352
+ buffer. push ( '(' ) ;
353
+ }
354
+ ')' if !hash && !escape => {
355
+ brackets -= 1 ;
356
+ buffer. push ( ')' ) ;
357
+ }
358
+ '#' if !hash && !escape => {
359
+ hash = true ;
360
+ buffer. push ( '#' ) ;
361
+ }
362
+ '#' if hash && !escape => {
363
+ hash = false ;
364
+ buffer. push ( '#' ) ;
365
+ }
366
+ '[' if !hash && brackets == 0 && !escape => {
367
+ parentheses += 1 ;
368
+ buffer. push ( '[' ) ;
369
+ }
370
+ ']' if !hash && brackets == 0 && !escape => {
371
+ parentheses -= 1 ;
372
+ buffer. push ( ']' ) ;
373
+ }
374
+ _ => {
375
+ if parentheses == 0 && brackets == 0 && !hash {
376
+ if escape {
377
+ buffer. push ( match c {
378
+ 'n' => '\n' ,
379
+ 't' => '\t' ,
380
+ 'r' => '\r' ,
381
+ _ => c,
382
+ } )
383
+ } else {
384
+ buffer. push ( c) ;
385
+ }
386
+ } else {
387
+ if escape {
388
+ buffer. push ( '\\' ) ;
389
+ }
390
+ buffer. push ( c) ;
391
+ }
392
+ escape = false ; // Reset escape flag for non-escape characters
393
+ }
394
+ }
395
+ }
396
+ buffer
397
+ } ;
398
+ self . stack . push ( Type :: String ( string) ) ;
319
399
} else if chars[ 0 ] == '[' && chars[ chars. len ( ) - 1 ] == ']' {
320
400
// Push list value on the stack
321
401
let old_len = self . stack . len ( ) ; // length of old stack
0 commit comments