@@ -239,27 +239,65 @@ function unit_handled_variable_value(meta, varname)
239
239
return varval
240
240
end
241
241
242
+ # This function parses various variable/parameter definitions.
243
+ #
244
+ # The comments indicate the syntax matched by a block; either when parsed directly
245
+ # when it is called recursively for parsing a part of an expression.
246
+ # These variable definitions are part of test suite in `test/model_parsing.jl`
242
247
function parse_variable_def! (dict, mod, arg, varclass, kwargs, where_types;
243
248
def = nothing , type:: Type = Real, meta = Dict {DataType, Expr} ())
244
249
arg isa LineNumberNode && return
245
250
MLStyle. @match arg begin
251
+ # Parses: `a`
252
+ # Recursively called by: `c(t) = cval + jval`
253
+ # Recursively called by: `d = 2`
254
+ # Recursively called by: `e, [description = "e"]`
255
+ # Recursively called by: `f = 3, [description = "f"]`
256
+ # Recursively called by: `k = kval, [description = "k"]`
257
+ # Recursively called by: `par0::Bool = true`
246
258
a:: Symbol => begin
247
259
var = generate_var! (dict, a, varclass; type)
248
260
update_kwargs_and_metadata! (dict, kwargs, a, def, type,
249
261
varclass, where_types, meta)
250
262
return var, def, Dict ()
251
263
end
264
+ # Parses: `par5[1:3]::BigFloat`
265
+ # Parses: `par6(t)[1:3]::BigFloat`
266
+ # Recursively called by: `par2(t)::Int`
267
+ # Recursively called by: `par3(t)::BigFloat = 1.0`
252
268
Expr (:(:: ), a, type) => begin
253
269
type = getfield (mod, type)
254
270
parse_variable_def! (
255
271
dict, mod, a, varclass, kwargs, where_types; def, type, meta)
256
272
end
273
+ # Recursively called by: `i(t) = 4, [description = "i(t)"]`
274
+ # Recursively called by: `h(t), [description = "h(t)"]`
275
+ # Recursively called by: `j(t) = jval, [description = "j(t)"]`
276
+ # Recursively called by: `par2(t)::Int`
277
+ # Recursively called by: `par3(t)::BigFloat = 1.0`
257
278
Expr (:call , a, b) => begin
258
279
var = generate_var! (dict, a, b, varclass, mod; type)
259
280
update_kwargs_and_metadata! (dict, kwargs, a, def, type,
260
281
varclass, where_types, meta)
261
282
return var, def, Dict ()
262
283
end
284
+ # Condition 1 parses:
285
+ # `(l(t)[1:2, 1:3] = 1), [description = "l is more than 1D"]`
286
+ # `(l2(t)[1:N, 1:M] = 2), [description = "l is more than 1D, with arbitrary length"]`
287
+ # `(l3(t)[1:3] = 3), [description = "l2 is 1D"]`
288
+ # `(l4(t)[1:N] = 4), [description = "l2 is 1D, with arbitrary length"]`
289
+ #
290
+ # Condition 2 parses:
291
+ # `(l5(t)[1:3]::Int = 5), [description = "l3 is 1D and has a type"]`
292
+ # `(l6(t)[1:N]::Int = 6), [description = "l3 is 1D and has a type, with arbitrary length"]`
293
+ #
294
+ # Condition 3 parses:
295
+ # `e2[1:2]::Int, [description = "e2"]`
296
+ # `h2(t)[1:2]::Int, [description = "h2(t)"]`
297
+ #
298
+ # Condition 4 parses:
299
+ # `e2[1:2], [description = "e2"]`
300
+ # `h2(t)[1:2], [description = "h2(t)"]`
263
301
Expr (:tuple , Expr (:(= ), Expr (:ref , a, indices... ), default_val), meta_val) ||
264
302
Expr (:tuple , Expr (:(= ), Expr (:(:: ), Expr (:ref , a, indices... ), type), default_val), meta_val) ||
265
303
Expr (:tuple , Expr (:(:: ), Expr (:ref , a, indices... ), type), meta_val) ||
@@ -284,6 +322,12 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
284
322
dict, indices, kwargs, meta, type, varclass, varname, varval, where_types)
285
323
(:($ varname... ), var), nothing , Dict ()
286
324
end
325
+ # Condition 1 parses:
326
+ # `par7(t)[1:3, 1:3]::BigFloat = 1.0, [description = "with description"]`
327
+ #
328
+ # Condition 2 parses:
329
+ # `d2[1:2] = 2`
330
+ # `l(t)[1:2, 1:3] = 2, [description = "l is more than 1D"]`
287
331
Expr (:(= ), Expr (:(:: ), Expr (:ref , a, indices... ), type), def_n_meta) ||
288
332
Expr (:(= ), Expr (:ref , a, indices... ), def_n_meta) => begin
289
333
(@isdefined type) || (type = Real)
@@ -326,6 +370,13 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
326
370
dict, indices, kwargs, meta, type, varclass, varname, varval, where_types)
327
371
(:($ varname... ), var), nothing , Dict ()
328
372
end
373
+ # Condition 1 is recursively called by:
374
+ # `par5[1:3]::BigFloat`
375
+ # `par6(t)[1:3]::BigFloat`
376
+ #
377
+ # Condition 2 parses:
378
+ # `b2(t)[1:2]`
379
+ # `a2[1:2]`
329
380
Expr (:(:: ), Expr (:ref , a, indices... ), type) ||
330
381
Expr (:ref , a, indices... ) => begin
331
382
(@isdefined type) || (type = Real)
@@ -346,6 +397,14 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
346
397
dict, indices, kwargs, nothing , type, varclass, varname, nothing , where_types)
347
398
(:($ varname... ), var), nothing , Dict ()
348
399
end
400
+ # Parses: `c(t) = cval + jval`
401
+ # Parses: `d = 2`
402
+ # Parses: `f = 3, [description = "f"]`
403
+ # Parses: `i(t) = 4, [description = "i(t)"]`
404
+ # Parses: `j(t) = jval, [description = "j(t)"]`
405
+ # Parses: `k = kval, [description = "k"]`
406
+ # Parses: `par0::Bool = true`
407
+ # Parses: `par3(t)::BigFloat = 1.0`
349
408
Expr (:(= ), a, b) => begin
350
409
Base. remove_linenums! (b)
351
410
def, meta = parse_default (mod, b)
@@ -361,6 +420,9 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
361
420
end
362
421
return var, def, Dict ()
363
422
end
423
+ # Parses: `e, [description = "e"]`
424
+ # Parses: `h(t), [description = "h(t)"]`
425
+ # Parses: `par2(t)::Int`
364
426
Expr (:tuple , a, b) => begin
365
427
meta = parse_metadata (mod, b)
366
428
var, def, _ = parse_variable_def! (
0 commit comments