Skip to content

Commit 4c0b1ed

Browse files
committed
docs: document the syntax parsed by a particular block
1 parent 0d4c97d commit 4c0b1ed

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/systems/model_parsing.jl

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,27 +239,65 @@ function unit_handled_variable_value(meta, varname)
239239
return varval
240240
end
241241

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`
242247
function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
243248
def = nothing, type::Type = Real, meta = Dict{DataType, Expr}())
244249
arg isa LineNumberNode && return
245250
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`
246258
a::Symbol => begin
247259
var = generate_var!(dict, a, varclass; type)
248260
update_kwargs_and_metadata!(dict, kwargs, a, def, type,
249261
varclass, where_types, meta)
250262
return var, def, Dict()
251263
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`
252268
Expr(:(::), a, type) => begin
253269
type = getfield(mod, type)
254270
parse_variable_def!(
255271
dict, mod, a, varclass, kwargs, where_types; def, type, meta)
256272
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`
257278
Expr(:call, a, b) => begin
258279
var = generate_var!(dict, a, b, varclass, mod; type)
259280
update_kwargs_and_metadata!(dict, kwargs, a, def, type,
260281
varclass, where_types, meta)
261282
return var, def, Dict()
262283
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)"]`
263301
Expr(:tuple, Expr(:(=), Expr(:ref, a, indices...), default_val), meta_val) ||
264302
Expr(:tuple, Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), default_val), meta_val) ||
265303
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;
284322
dict, indices, kwargs, meta, type, varclass, varname, varval, where_types)
285323
(:($varname...), var), nothing, Dict()
286324
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"]`
287331
Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), def_n_meta) ||
288332
Expr(:(=), Expr(:ref, a, indices...), def_n_meta) => begin
289333
(@isdefined type) || (type = Real)
@@ -326,6 +370,13 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
326370
dict, indices, kwargs, meta, type, varclass, varname, varval, where_types)
327371
(:($varname...), var), nothing, Dict()
328372
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]`
329380
Expr(:(::), Expr(:ref, a, indices...), type) ||
330381
Expr(:ref, a, indices...) => begin
331382
(@isdefined type) || (type = Real)
@@ -346,6 +397,14 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
346397
dict, indices, kwargs, nothing, type, varclass, varname, nothing, where_types)
347398
(:($varname...), var), nothing, Dict()
348399
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`
349408
Expr(:(=), a, b) => begin
350409
Base.remove_linenums!(b)
351410
def, meta = parse_default(mod, b)
@@ -361,6 +420,9 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
361420
end
362421
return var, def, Dict()
363422
end
423+
# Parses: `e, [description = "e"]`
424+
# Parses: `h(t), [description = "h(t)"]`
425+
# Parses: `par2(t)::Int`
364426
Expr(:tuple, a, b) => begin
365427
meta = parse_metadata(mod, b)
366428
var, def, _ = parse_variable_def!(

0 commit comments

Comments
 (0)