Skip to content

Commit 13489d8

Browse files
authored
fix Utilities/parser.jl on 0.7 (#384)
1 parent dcc5ebb commit 13489d8

File tree

2 files changed

+57
-44
lines changed

2 files changed

+57
-44
lines changed

.travis.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,12 @@ os:
55
# - osx
66
julia:
77
- 0.6
8-
# - nightly
8+
- 0.7
99
notifications:
1010
email: false
1111
git:
1212
depth: 99999999
1313

14-
## uncomment the following lines to allow failures on nightly julia
15-
## (tests will run but not make your overall status red)
16-
matrix:
17-
allow_failures:
18-
- julia: nightly
19-
2014
# Integration with JuMP-dev gitter channel
2115
notifications:
2216
webhooks:

src/Utilities/parser.jl

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
using Base.Meta: isexpr
22

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"
2017

2118
struct ParsedScalarAffineTerm
2219
coefficient::Float64
@@ -151,23 +148,45 @@ function parsefunction(ex)
151148
end
152149

153150
# 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
171190
end
172191
end
173192

@@ -185,14 +204,14 @@ parsedtoMOI(model, s::Union{Float64, Int64}) = s
185204
for typename in [:ParsedScalarAffineTerm,:ParsedScalarAffineFunction,:ParsedVectorAffineTerm,:ParsedVectorAffineFunction,
186205
:ParsedScalarQuadraticTerm,:ParsedScalarQuadraticFunction,:ParsedVectorQuadraticTerm,:ParsedVectorQuadraticFunction,
187206
:ParsedSingleVariable,:ParsedVectorOfVariables]
188-
moiname = parse(replace(string(typename), "Parsed" => "MOI."))
207+
moiname = Compat.Meta.parse(replace(string(typename), "Parsed" => "MOI."))
189208
fields = fieldnames(eval(typename))
190209
constructor = Expr(:call, moiname, [Expr(:call,:parsedtoMOI,:model,Expr(:.,:f,Base.Meta.quot(field))) for field in fields]...)
191210
@eval parsedtoMOI(model, f::$typename) = $constructor
192211
end
193212

194213
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")))
196215

197216
for line in parsedlines
198217
label, ex = separatelabel(line)
@@ -221,7 +240,7 @@ function loadfromstring!(model, s)
221240
f = parsedtoMOI(model, parsefunction(ex.args[2]))
222241
if ex.args[1] == :in
223242
# Could be safer here
224-
set = eval(MOI, ex.args[3])
243+
set = Compat.Core.eval(MOI, ex.args[3])
225244
elseif ex.args[1] == :<=
226245
set = MOI.LessThan(ex.args[3])
227246
elseif ex.args[1] == :>=

0 commit comments

Comments
 (0)