@@ -188,6 +188,8 @@ func parse() {
188
188
switch {
189
189
case char == ' ' || char == '\t' || char == '\n' :
190
190
advance ()
191
+ case isChar ('/' ):
192
+ collectComment ()
191
193
case tokenAhead (Question ):
192
194
collectQuestion ()
193
195
case tokenAhead (Definition ):
@@ -199,8 +201,6 @@ func parse() {
199
201
case tokenAhead (Constant ):
200
202
advance ()
201
203
collectVariable (true )
202
- case isChar ('/' ):
203
- collectComment ()
204
204
case tokenAhead (Repeat ):
205
205
collectRepeat ()
206
206
case tokenAhead (RepeatWithEach ):
@@ -293,12 +293,52 @@ func lookAheadUntil(until rune) string {
293
293
294
294
func collectVariableValue (constant bool , valueType * tokenType , value * any ) {
295
295
collectValue (valueType , value , '\n' )
296
+ if * valueType == Question {
297
+ parserError (fmt .Sprintf ("Illegal reference to import question '%s'. Shortcuts does not support import questions as variable values." , * value ))
298
+ }
296
299
300
+ var aheadOfValue = lookAheadUntil ('\n' )
301
+ if strings .Contains (aheadOfValue , "//" ) || strings .Contains (aheadOfValue , "/*" ) {
302
+ return
303
+ }
304
+ if containsExpressionTokens (aheadOfValue ) {
305
+ collectExpression (valueType , value )
306
+ return
307
+ }
297
308
if constant && (* valueType == Arr || * valueType == Variable ) {
298
309
parserError (fmt .Sprintf ("Type %v values cannot be constants." , * valueType ))
299
310
}
300
- if * valueType == Question {
301
- parserError (fmt .Sprintf ("Illegal reference to import question '%s'. Shortcuts does not support import questions as variable values." , * value ))
311
+ }
312
+
313
+ func collectExpression (valueType * tokenType , value * any ) {
314
+ if ! slices .Contains ([]tokenType {Integer , Float , Variable }, * valueType ) {
315
+ parserError (fmt .Sprintf ("Value of type '%s' not allowed in expression" , * valueType ))
316
+ }
317
+ if * valueType == Variable {
318
+ var valueRef = * value
319
+ * value = fmt .Sprintf ("{%s}" , valueRef .(varValue ).value )
320
+ } else {
321
+ * value = fmt .Sprintf ("%v" , * value )
322
+ }
323
+ * valueType = Expression
324
+
325
+ for char != - 1 && char != '\n' {
326
+ switch {
327
+ case char == ' ' || char == '+' || char == '-' || char == '*' || char == '/' || char == '%' || char == '(' || char == ')' :
328
+ * value = fmt .Sprintf ("%s%c" , * value , char )
329
+ advance ()
330
+ case intChar (char ):
331
+ var intValueType tokenType
332
+ var intValue any
333
+ collectIntegerValue (& intValueType , & intValue )
334
+ * value = fmt .Sprintf ("%s%v" , * value , intValue )
335
+ default :
336
+ var until = ' '
337
+ var refType tokenType
338
+ var refValue any
339
+ collectReference (& refType , & refValue , & until )
340
+ * value = fmt .Sprintf ("%s{%s}" , * value , refValue .(varValue ).value )
341
+ }
302
342
}
303
343
}
304
344
@@ -313,7 +353,7 @@ func collectValue(valueType *tokenType, value *any, until rune) {
313
353
if strings .Contains (ahead , "." ) {
314
354
* valueType = Float
315
355
}
316
- collectIntegerValue (valueType , value , until )
356
+ collectIntegerValue (valueType , value )
317
357
case char == '"' :
318
358
collectStringValue (valueType , value )
319
359
case char == '\'' :
@@ -328,6 +368,10 @@ func collectValue(valueType *tokenType, value *any, until rune) {
328
368
advance ()
329
369
* valueType = Dict
330
370
* value = collectDictionary ()
371
+ case char == '(' :
372
+ * valueType = Integer
373
+ * value = ""
374
+ collectExpression (valueType , value )
331
375
case tokenAhead (True ):
332
376
* valueType = Bool
333
377
* value = true
@@ -339,9 +383,6 @@ func collectValue(valueType *tokenType, value *any, until rune) {
339
383
advanceUntil (until )
340
384
case strings .Contains (ahead , "(" ):
341
385
collectActionValue (valueType , value )
342
- case containsTokens (& ahead , Plus , Minus , Multiply , Divide , Modulus ):
343
- * valueType = Expression
344
- * value = collectUntil (until )
345
386
default :
346
387
collectReference (valueType , value , & until )
347
388
}
@@ -1295,26 +1336,19 @@ func collectInteger() string {
1295
1336
return collection .String ()
1296
1337
}
1297
1338
1298
- func collectIntegerValue (valueType * tokenType , value * any , until rune ) {
1299
- var ahead = lookAheadUntil (until )
1300
- if ! containsTokens (& ahead , Plus , Minus , Multiply , Divide , Modulus ) {
1301
- var integerString = collectInteger ()
1339
+ func collectIntegerValue (valueType * tokenType , value * any ) {
1340
+ var integerString = collectInteger ()
1341
+ if * valueType == Integer {
1342
+ var integer , convErr = strconv .Atoi (integerString )
1343
+ handle (convErr )
1302
1344
1303
- if * valueType == Integer {
1304
- var integer , convErr = strconv .Atoi (integerString )
1305
- handle (convErr )
1306
-
1307
- * value = integer
1308
- } else {
1309
- var float , floatErr = strconv .ParseFloat (integerString , 64 )
1310
- handle (floatErr )
1345
+ * value = integer
1346
+ } else {
1347
+ var float , floatErr = strconv .ParseFloat (integerString , 64 )
1348
+ handle (floatErr )
1311
1349
1312
- * value = float
1313
- }
1314
- return
1350
+ * value = float
1315
1351
}
1316
- * valueType = Expression
1317
- * value = collectUntil (until )
1318
1352
}
1319
1353
1320
1354
func collectString () string {
@@ -1552,6 +1586,10 @@ func tokenAhead(token tokenType) bool {
1552
1586
return true
1553
1587
}
1554
1588
1589
+ func containsExpressionTokens (str string ) bool {
1590
+ return containsTokens (& str , Plus , Minus , Multiply , Divide , Modulus , LeftParen , RightParen )
1591
+ }
1592
+
1555
1593
func containsTokens (str * string , v ... tokenType ) bool {
1556
1594
for _ , aheadToken := range v {
1557
1595
if strings .Contains (* str , string (aheadToken )) {
0 commit comments