Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Utilities/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,22 @@ function _parse_function(ex)
end
end
else
# only accept Expr(:call, :+, ...), no recursive expressions
# TODO: generalize. x - y + z would be useful
# For simplicity, only accept Expr(:call, :+, ...); no recursive
# expressions
if isexpr(ex, :call) && ex.args[1] == :*
# handle 2x as (+)(2x)
ex = Expr(:call, :+, ex)
ex = Expr(:call, :+, ex) # Handle 2x as (+)(2x)
end
if ex isa Number
ex = Expr(:call, :+, ex)
end
@assert isexpr(ex, :call)
if ex.args[1] != :+
error("Expected `+`, got `$(ex.args[1])`.")
error(
"Unsupported operator in `loadfromstring!`: `$(ex.args[1])`. " *
"The parser is deliberately limited in the syntax it " *
"accepts. Write `x - y` as `x + -1 * y`, and `x^2` as " *
"`x * x`.",
)
end
affine_terms = _ParsedScalarAffineTerm[]
quadratic_terms = _ParsedScalarQuadraticTerm[]
Expand Down
7 changes: 6 additions & 1 deletion test/Utilities/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ function test__parse_function()
),
)

err = ErrorException("Expected `+`, got `-`.")
err = ErrorException(
"Unsupported operator in `loadfromstring!`: `-`. " *
"The parser is deliberately limited in the syntax it " *
"accepts. Write `x - y` as `x + -1 * y`, and `x^2` as " *
"`x * x`.",
)
@test_throws err MOIU._parse_function(:(x - y))

@test _struct_isequal(
Expand Down