@@ -129,8 +129,9 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector)
129129 Ref (dict), [:constants , :defaults , :kwargs , :structural_parameters ])
130130
131131 sys = :($ type ($ (flatten_equations)(equations), $ iv, variables, parameters;
132- name, description = $ description, systems, gui_metadata = $ gui_metadata, defaults,
133- costs = [$ (costs... )], constraints = [$ (cons... )], consolidate = $ consolidate))
132+ name, description = $ description, systems, gui_metadata = $ gui_metadata,
133+ continuous_events = [$ (c_evts... )], discrete_events = [$ (d_evts... )],
134+ defaults, costs = [$ (costs... )], constraints = [$ (cons... )], consolidate = $ consolidate))
134135
135136 if length (ext) == 0
136137 push! (exprs. args, :(var"#___sys___" = $ sys))
@@ -141,16 +142,6 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector)
141142 isconnector && push! (exprs. args,
142143 :($ Setfield. @set! (var"#___sys___" . connector_type= $ connector_type (var"#___sys___" ))))
143144
144- ! isempty (c_evts) && push! (exprs. args,
145- :($ Setfield. @set! (var"#___sys___" . continuous_events= $ SymbolicContinuousCallback .([
146- $ (c_evts... )
147- ]))))
148-
149- ! isempty (d_evts) && push! (exprs. args,
150- :($ Setfield. @set! (var"#___sys___" . discrete_events= $ SymbolicDiscreteCallback .([
151- $ (d_evts... )
152- ]))))
153-
154145 f = if length (where_types) == 0
155146 :($ (Symbol (:__ , name, :__ ))(; name, $ (kwargs... )) = $ exprs)
156147 else
@@ -1144,21 +1135,47 @@ end
11441135function parse_continuous_events! (c_evts, dict, body)
11451136 dict[:continuous_events ] = []
11461137 Base. remove_linenums! (body)
1147- for arg in body. args
1148- push! (c_evts, arg)
1138+ for line in body. args
1139+ if length (line. args) == 3 && line. args[1 ] == :(=> )
1140+ push! (c_evts, :(($ line, ())))
1141+ elseif length (line. args) == 2
1142+ event = line. args[1 ]
1143+ kwargs = parse_event_kwargs (line. args[2 ])
1144+ push! (c_evts, :(($ event, $ kwargs)))
1145+ else
1146+ error (" Malformed continuous event $line ." )
1147+ end
11491148 push! (dict[:continuous_events ], readable_code .(c_evts)... )
11501149 end
11511150end
11521151
11531152function parse_discrete_events! (d_evts, dict, body)
11541153 dict[:discrete_events ] = []
11551154 Base. remove_linenums! (body)
1156- for arg in body. args
1157- push! (d_evts, arg)
1155+ for line in body. args
1156+ if length (line. args) == 3 && line. args[1 ] == :(=> )
1157+ push! (d_evts, :(($ line, ())))
1158+ elseif length (line. args) == 2
1159+ event = line. args[1 ]
1160+ kwargs = parse_event_kwargs (line. args[2 ])
1161+ push! (d_evts, :(($ event, $ kwargs)))
1162+ else
1163+ error (" Malformed discrete event $line ." )
1164+ end
11581165 push! (dict[:discrete_events ], readable_code .(d_evts)... )
11591166 end
11601167end
11611168
1169+ function parse_event_kwargs (disc_expr)
1170+ kwargs = :([])
1171+ for arg in disc_expr. args
1172+ (arg. head != :(= )) && error (" Malformed event kwarg $arg ." )
1173+ (arg. args[1 ] isa Symbol) || error (" Invalid keyword argument name $(arg. args[1 ]) ." )
1174+ push! (kwargs. args, :($ (QuoteNode (arg. args[1 ])) => $ (arg. args[2 ])))
1175+ end
1176+ kwargs
1177+ end
1178+
11621179function parse_constraints! (cons, dict, body)
11631180 dict[:constraints ] = []
11641181 Base. remove_linenums! (body)
0 commit comments