1 ## our_shell: ysh
2
3 #### Unquoted backslash escapes, as in J8 strings
4
5 # everything except \b \f \n
6
7 var nl = \n
8 pp test_ (nl)
9
10 var tab = \t
11 pp test_ (tab)
12
13 pp test_ (\r)
14
15 pp test_ (\" ++ \' ++ \\)
16
17 echo backslash $[\\]
18 echo "backslash $[\\]"
19
20 ## STDOUT:
21 (Str) "\n"
22 (Str) "\t"
23 (Str) "\r"
24 (Str) "\"'\\"
25 backslash \
26 backslash \
27 ## END
28
29 #### Unquoted \u{3bc} escape
30
31 var x = 'mu ' ++ \u{3bc}
32 echo $x
33
34 echo mu $[\u{3bc}]
35 echo "mu $[\u{3bc}]"
36
37 ## STDOUT:
38 mu μ
39 mu μ
40 mu μ
41 ## END
42
43 #### Unquoted \y24 escape
44
45 var x = 'foo ' ++ \y24
46 echo $x
47
48 var y = 0x24
49 echo $y
50
51 echo foo $[\y40]
52 echo "foo $[\y41]"
53
54 ## STDOUT:
55 foo $
56 36
57 foo @
58 foo A
59 ## END
60
61 #### single quoted -- implicit and explicit raw
62 var x = 'foo bar'
63 echo $x
64 setvar x = r'foo bar' # Same string
65 echo $x
66 setvar x = r'\t\n' # This is raw
67 echo $x
68 ## STDOUT:
69 foo bar
70 foo bar
71 \t\n
72 ## END
73
74 #### Implicit raw single quote with backslash is a syntax error
75 var x = '\t\n'
76 echo $x
77 ## status: 2
78 ## stdout-json: ""
79
80 #### $"foo $x" to make "foo $x" explicit
81
82 var x = $"bar"
83
84 # expression mode
85 var y = $"foo $x"
86 echo "$y"
87
88 # command mode
89 if test "$y" = $"foo $x"; then
90 echo equal
91 fi
92
93 ## STDOUT:
94 foo bar
95 equal
96 ## END
97
98 #### single quoted C strings: $'foo\n'
99
100 # expression mode
101 var x = $'foo\nbar'
102 echo "$x"
103
104 # command mode
105 if test "$x" = $'foo\nbar'; then
106 echo equal
107 fi
108
109 ## STDOUT:
110 foo
111 bar
112 equal
113 ## END
114
115 #### raw strings and J8 strings don't work in OSH
116 shopt --unset ysh:all
117
118 echo r'hello \'
119 echo u'mu \u{3bc}'
120 echo b'byte \yff'
121
122 echo --
123
124 echo r'''
125 raw multi
126 '''
127
128 echo u'''
129 u multi
130 '''
131
132 echo b'''
133 b multi
134 '''
135
136 ## STDOUT:
137 rhello \
138 umu \u{3bc}
139 bbyte \yff
140 --
141 r
142 raw multi
143
144 u
145 u multi
146
147 b
148 b multi
149
150 ## END
151
152 #### J8-style u'' and b'' strings in expression mode
153
154 var x = u'\u{3bc}'
155 var y = b'\yff'
156
157
158 write --end '' -- $x | od -A n -t x1
159 write --end '' -- $y | od -A n -t x1
160
161 ## STDOUT:
162 ce bc
163 ff
164 ## END
165
166 #### J8-style u'' and b'' strings in command mode
167
168 write --end '' -- u'\u{3bc}' | od -A n -t x1
169 write --end '' -- b'\yff' | od -A n -t x1
170
171 # TODO: make this be illegal
172 # echo u'hello \u03bc'
173
174 ## STDOUT:
175 ce bc
176 ff
177 ## END
178
179 #### J8-style multi-line strings u''' b''' in command mode
180
181 write --end '' -- u'''
182 --
183 \u{61}
184 --
185 '''
186 write --end '' -- b'''
187 --
188 \y62
189 --
190 '''
191
192 # Should be illegal?
193 #echo u'hello \u03bc'
194
195 ## STDOUT:
196 --
197 a
198 --
199 --
200 b
201 --
202 ## END
203
204 #### Double Quoted
205 var name = 'World'
206 var g = "Hello $name"
207
208 echo "Hello $name"
209 echo $g
210 ## STDOUT:
211 Hello World
212 Hello World
213 ## END
214
215 #### Multiline strings with '' and ""
216
217 var single = '
218 single
219 '
220
221 var x = 42
222 var double = "
223 double $x
224 "
225
226 echo $single
227 echo $double
228
229 ## STDOUT:
230
231 single
232
233
234 double 42
235
236 ## END
237
238 #### C strings in :| | array literals
239 shopt -s ysh:upgrade
240
241 var lines=:| $'aa\tbb' $'cc\tdd' |
242 write @lines
243
244 ## STDOUT:
245 aa bb
246 cc dd
247 ## END
248
249 #### shopt parse_ysh_string
250
251 # Ignored prefix
252 echo r'\'
253
254 # space
255 write r '' end
256
257 # These use shell rules!
258 echo ra'\'
259 echo raw'\'
260
261 echo r"\\"
262
263 # Now it's a regular r
264 shopt --unset parse_ysh_string
265 write unset r'\'
266
267 ## STDOUT:
268 \
269 r
270
271 end
272 ra\
273 raw\
274 r\
275 unset
276 r\
277 ## END
278
279 #### Special rule for <<< ''' and <<< """ - no extra newline
280
281 read --all <<< unquoted
282 pp test_ (_reply)
283
284 read --all <<< 'single with newline'
285 pp test_ (_reply)
286
287 read --all <<< "double with newline"
288 pp test_ (_reply)
289
290 read --all <<< u'j8 with newline'
291 pp test_ (_reply)
292
293 echo
294
295 read --all <<< '''
296 multi
297 single
298 '''
299 pp test_ (_reply)
300
301 read --all <<< """
302 multi
303 double
304 """
305 pp test_ (_reply)
306
307 read --all <<< u'''
308 multi
309 j8
310 '''
311 pp test_ (_reply)
312
313
314 ## STDOUT:
315 (Str) "unquoted\n"
316 (Str) "single with newline\n"
317 (Str) "double with newline\n"
318 (Str) "j8 with newline\n"
319
320 (Str) "multi\nsingle\n"
321 (Str) "multi\ndouble\n"
322 (Str) "multi\nj8\n"
323 ## END
324
325 #### $''' isn't a a multiline string (removed)
326
327 shopt -s ysh:upgrade
328
329 echo $'''
330 foo
331 '''
332
333 ## STDOUT:
334
335 foo
336
337 ## END
338
339
340 #### """ and $""" in Expression Mode
341
342 var line1 = """line1"""
343 echo line1=$line1
344 var line2 = """
345 line2"""
346 echo line2=$line2
347
348 var two = 2
349 var three = 3
350 var x = """
351 one "
352 two = $two ""
353 three = $three
354 """
355 echo "[$x]"
356
357 var i = 42
358 var x = """
359 good
360 bad $i
361 """
362 echo "[$x]"
363
364 # alias
365 var x = $"""
366 good
367 bad $i
368 """
369 echo "[$x]"
370
371 ## STDOUT:
372 line1=line1
373 line2=line2
374 [one "
375 two = 2 ""
376 three = 3
377 ]
378 [good
379 bad 42
380 ]
381 [good
382 bad 42
383 ]
384 ## END
385
386 #### ''' in Expression Mode
387
388 var two = 2
389 var three = 2
390
391 var x = '''
392 two = $two '
393 three = $three ''
394 \u{61}
395 '''
396 echo "[$x]"
397
398 var x = u'''
399 two = $two '
400 three = $three ''
401 \u{61}
402 '''
403 echo "[$x]"
404
405 var x = b'''
406 two = $two '
407 three = $three ''
408 \u{61} \y61
409 '''
410 echo "[$x]"
411
412 ## STDOUT:
413 [two = $two '
414 three = $three ''
415 \u{61}
416 ]
417 [two = $two '
418 three = $three ''
419 a
420 ]
421 [two = $two '
422 three = $three ''
423 a a
424 ]
425 ## END
426
427
428 #### """ and $""" in Command Mode
429
430 var two=2
431 var three=3
432
433 echo ""a # test lookahead
434
435 echo --
436 echo """
437 one "
438 two = $two ""
439 three = $three
440 """
441
442 # optional $ prefix
443 echo --
444 echo $"""
445 one "
446 two = $two ""
447 three = $three
448 """
449
450 echo --
451 tac <<< """
452 one "
453 two = $two ""
454 three = $three
455 """
456
457 shopt --unset parse_triple_quote
458
459 echo --
460 echo """
461 one
462 two = $two
463 three = $three
464 """
465
466
467 ## STDOUT:
468 a
469 --
470 one "
471 two = 2 ""
472 three = 3
473
474 --
475 one "
476 two = 2 ""
477 three = 3
478
479 --
480 three = 3
481 two = 2 ""
482 one "
483 --
484
485 one
486 two = 2
487 three = 3
488
489 ## END
490
491
492 #### ''' in Command Mode
493
494 echo ''a # make sure lookahead doesn't mess up
495
496 echo --
497 echo '''
498 two = $two
499 '
500 '' '
501 \u{61}
502 '''
503 ## STDOUT:
504 a
505 --
506 two = $two
507 '
508 '' '
509 \u{61}
510
511 ## END
512
513 #### r''' in Command Mode, Expression mode
514
515 echo r'''\'''
516
517 var x = r'''\'''
518 echo $x
519
520 shopt --unset parse_ysh_string
521
522 echo r'''\'''
523
524 ## STDOUT:
525 \
526 \
527 r\
528 ## END
529
530
531 #### ''' in Here Doc
532
533 tac <<< '''
534 two = $two
535 '
536 '' '
537 \u{61}
538 '''
539
540 ## STDOUT:
541 \u{61}
542 '' '
543 '
544 two = $two
545 ## END
546
547 #### ''' without parse_triple_quote
548
549 shopt --unset parse_triple_quote
550
551 echo '''
552 two = $two
553 \u{61}
554 '''
555
556 ## STDOUT:
557
558 two = $two
559 \u{61}
560
561 ## END
562
563 #### here doc with quotes
564
565 # This has 3 right double quotes
566
567 cat <<EOF
568 "hello"
569 ""
570 """
571 EOF
572
573
574 ## STDOUT:
575 "hello"
576 ""
577 """
578 ## END
579
580 #### triple quoted and implicit concatenation
581
582 # Should we allow this? Or I think it's possible to make it a syntax error
583
584 echo '''
585 single
586 '''zz
587
588 echo """
589 double
590 """zz
591 ## status: 2
592 ## stdout-json: ""
593