Browse code

Fix detection for missing parameter in substitution

`${}`, `${:}` and so on are invalid because there's
no parameter within the brackets; fix detection for
this situation and add/update tests.

There were some existing test-cases that were testing
for the wrong behavior, which are now updated.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2018/05/26 00:31:14
Showing 2 changed files
... ...
@@ -29,10 +29,14 @@ A|he\$PWD                  |     he$PWD
29 29
 A|he\\$PWD                 |     he\/home
30 30
 A|"he\$PWD"                |     he$PWD
31 31
 A|"he\\$PWD"               |     he\/home
32
+A|\${}                     |     ${}
33
+A|\${}aaa                  |     ${}aaa
32 34
 A|he\${}                   |     he${}
33 35
 A|he\${}xx                 |     he${}xx
34
-A|he${}                    |     he
35
-A|he${}xx                  |     hexx
36
+A|${}                      |     error
37
+A|${}aaa                   |     error
38
+A|he${}                    |     error
39
+A|he${}xx                  |     error
36 40
 A|he${hi}                  |     he
37 41
 A|he${hi}xx                |     hexx
38 42
 A|he${PWD}                 |     he/home
... ...
@@ -88,8 +92,8 @@ A|안녕\$PWD                  |     안녕$PWD
88 88
 A|안녕\\$PWD                 |     안녕\/home
89 89
 A|안녕\${}                   |     안녕${}
90 90
 A|안녕\${}xx                 |     안녕${}xx
91
-A|안녕${}                    |     안녕
92
-A|안녕${}xx                  |     안녕xx
91
+A|안녕${}                    |     error
92
+A|안녕${}xx                  |     error
93 93
 A|안녕${hi}                  |     안녕
94 94
 A|안녕${hi}xx                |     안녕xx
95 95
 A|안녕${PWD}                 |     안녕/home
... ...
@@ -129,4 +133,7 @@ A|${aaa:-bbb}               |     bbb
129 129
 A|${aaa:-${bbb:-ccc}}       |     ccc
130 130
 A|${aaa:-bbb ${foo}         |     error
131 131
 A|${aaa:-bbb {foo}          |     bbb {foo
132
+A|${:}                      |     error
133
+A|${:-bbb}                  |     error
134
+A|${:+bbb}                  |     error
132 135
 
... ...
@@ -261,23 +261,22 @@ func (sw *shellWord) processDollar() (string, error) {
261 261
 	}
262 262
 
263 263
 	sw.scanner.Next()
264
+	switch sw.scanner.Peek() {
265
+	case scanner.EOF:
266
+		return "", errors.New("syntax error: missing '}'")
267
+	case '{', '}', ':':
268
+		// Invalid ${{xx}, ${:xx}, ${:}. ${} case
269
+		return "", errors.New("syntax error: bad substitution")
270
+	}
264 271
 	name := sw.processName()
265
-	ch := sw.scanner.Peek()
272
+	ch := sw.scanner.Next()
266 273
 	switch ch {
267 274
 	case '}':
268 275
 		// Normal ${xx} case
269
-		sw.scanner.Next()
270 276
 		return sw.getEnv(name), nil
271
-	case '{':
272
-		// Invalid ${{xx} case
273
-		return "", errors.New("syntax error: bad substitution")
274
-	case scanner.EOF:
275
-		return "", errors.New("syntax error: missing '}'")
276 277
 	case ':':
277 278
 		// Special ${xx:...} format processing
278 279
 		// Yes it allows for recursive $'s in the ... spot
279
-
280
-		sw.scanner.Next() // skip over :
281 280
 		modifier := sw.scanner.Next()
282 281
 
283 282
 		word, _, err := sw.processStopOn('}')