1
1
using Base. Meta: isexpr
2
2
3
-
4
- """
5
- A parser for a simple human-readable of an MOI model.
6
- This should be thought of as a compact way to write small models
7
- for tests, and not an exchange format.
8
-
9
- variables: x, y, z
10
- minobjective: 2x + 3y
11
- con1: x + y <= 1
12
- con2: [x,y] in Set
13
-
14
- special labels: variables, minobjective, maxobjective
15
- everything else denotes a constraint with a name
16
- all constraints must be named
17
- "x - y" does NOT currently parse, needs to be written as "x + -1.0*y"
18
- "x^2" does NOT currently parse, needs to be written as "x*x"
19
- """
3
+ # A parser for a simple human-readable of an MOI model.
4
+ # This should be thought of as a compact way to write small models
5
+ # for tests, and not an exchange format.
6
+ #
7
+ # variables: x, y, z
8
+ # minobjective: 2x + 3y
9
+ # con1: x + y <= 1
10
+ # con2: [x,y] in Set
11
+ #
12
+ # special labels: variables, minobjective, maxobjective
13
+ # everything else denotes a constraint with a name
14
+ # all constraints must be named
15
+ # "x - y" does NOT currently parse, needs to be written as "x + -1.0*y"
16
+ # "x^2" does NOT currently parse, needs to be written as "x*x"
20
17
21
18
struct ParsedScalarAffineTerm
22
19
coefficient:: Float64
@@ -151,23 +148,45 @@ function parsefunction(ex)
151
148
end
152
149
153
150
# see tests for examples
154
- function separatelabel (ex)
155
- if isexpr (ex, :(:))
156
- return ex. args[1 ], ex. args[2 ]
157
- elseif isexpr (ex, :tuple )
158
- ex = copy (ex)
159
- @assert isexpr (ex. args[1 ], :(:))
160
- label = ex. args[1 ]. args[1 ]
161
- ex. args[1 ] = ex. args[1 ]. args[2 ]
162
- return label, ex
163
- elseif isexpr (ex, :call )
164
- ex = copy (ex)
165
- @assert isexpr (ex. args[2 ], :(:))
166
- label = ex. args[2 ]. args[1 ]
167
- ex. args[2 ] = ex. args[2 ]. args[2 ]
168
- return label, ex
169
- else
170
- error (" Unrecognized expression $ex " )
151
+ if VERSION > v " 0.7-"
152
+ function separatelabel (ex)
153
+ if isexpr (ex, :call ) && ex. args[1 ] == :(:)
154
+ return ex. args[2 ], ex. args[3 ]
155
+ elseif isexpr (ex, :tuple )
156
+ ex = copy (ex)
157
+ @assert isexpr (ex. args[1 ], :call ) && ex. args[1 ]. args[1 ] == :(:)
158
+ label = ex. args[1 ]. args[2 ]
159
+ ex. args[1 ] = ex. args[1 ]. args[3 ]
160
+ return label, ex
161
+ elseif isexpr (ex, :call )
162
+ ex = copy (ex)
163
+ @assert isexpr (ex. args[2 ], :call ) && ex. args[2 ]. args[1 ] == :(:)
164
+ label = ex. args[2 ]. args[2 ]
165
+ ex. args[2 ] = ex. args[2 ]. args[3 ]
166
+ return label, ex
167
+ else
168
+ error (" Unrecognized expression $ex " )
169
+ end
170
+ end
171
+ else
172
+ function separatelabel (ex)
173
+ if isexpr (ex, :(:))
174
+ return ex. args[1 ], ex. args[2 ]
175
+ elseif isexpr (ex, :tuple )
176
+ ex = copy (ex)
177
+ @assert isexpr (ex. args[1 ], :(:))
178
+ label = ex. args[1 ]. args[1 ]
179
+ ex. args[1 ] = ex. args[1 ]. args[2 ]
180
+ return label, ex
181
+ elseif isexpr (ex, :call )
182
+ ex = copy (ex)
183
+ @assert isexpr (ex. args[2 ], :(:))
184
+ label = ex. args[2 ]. args[1 ]
185
+ ex. args[2 ] = ex. args[2 ]. args[2 ]
186
+ return label, ex
187
+ else
188
+ error (" Unrecognized expression $ex " )
189
+ end
171
190
end
172
191
end
173
192
@@ -185,14 +204,14 @@ parsedtoMOI(model, s::Union{Float64, Int64}) = s
185
204
for typename in [:ParsedScalarAffineTerm ,:ParsedScalarAffineFunction ,:ParsedVectorAffineTerm ,:ParsedVectorAffineFunction ,
186
205
:ParsedScalarQuadraticTerm ,:ParsedScalarQuadraticFunction ,:ParsedVectorQuadraticTerm ,:ParsedVectorQuadraticFunction ,
187
206
:ParsedSingleVariable ,:ParsedVectorOfVariables ]
188
- moiname = parse (replace (string (typename), " Parsed" => " MOI." ))
207
+ moiname = Compat . Meta . parse (replace (string (typename), " Parsed" => " MOI." ))
189
208
fields = fieldnames (eval (typename))
190
209
constructor = Expr (:call , moiname, [Expr (:call ,:parsedtoMOI ,:model ,Expr (:.,:f ,Base. Meta. quot (field))) for field in fields]. .. )
191
210
@eval parsedtoMOI (model, f:: $typename ) = $ constructor
192
211
end
193
212
194
213
function loadfromstring! (model, s)
195
- parsedlines = filter (ex -> ex != nothing ,parse .(split (s," \n " )))
214
+ parsedlines = filter (ex -> ex != nothing , Compat . Meta . parse .(split (s," \n " )))
196
215
197
216
for line in parsedlines
198
217
label, ex = separatelabel (line)
@@ -221,7 +240,7 @@ function loadfromstring!(model, s)
221
240
f = parsedtoMOI (model, parsefunction (ex. args[2 ]))
222
241
if ex. args[1 ] == :in
223
242
# Could be safer here
224
- set = eval (MOI, ex. args[3 ])
243
+ set = Compat . Core . eval (MOI, ex. args[3 ])
225
244
elseif ex. args[1 ] == :<=
226
245
set = MOI. LessThan (ex. args[3 ])
227
246
elseif ex. args[1 ] == :>=
0 commit comments