From e052ba81f0974c916fbfebccdf5075498620832b Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 24 Aug 2021 14:53:12 +1200 Subject: [PATCH 1/3] Improve error message loadfromstring --- src/Utilities/parser.jl | 14 +++++++++----- test/Utilities/parser.jl | 7 ++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Utilities/parser.jl b/src/Utilities/parser.jl index 9fe5cc3b52..77d8f6a740 100644 --- a/src/Utilities/parser.jl +++ b/src/Utilities/parser.jl @@ -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( + "Unsupport 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[] diff --git a/test/Utilities/parser.jl b/test/Utilities/parser.jl index 82e74e4601..9bfcfa7efc 100644 --- a/test/Utilities/parser.jl +++ b/test/Utilities/parser.jl @@ -54,7 +54,12 @@ function test__parse_function() ), ) - err = ErrorException("Expected `+`, got `-`.") + err = ErrorException( + "Unsupport 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( From ede80038e4dfd3c65beaf58c995b2ad951c39eb5 Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 24 Aug 2021 15:13:19 +1200 Subject: [PATCH 2/3] Fix formatting --- src/Utilities/parser.jl | 6 +++--- test/Utilities/parser.jl | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Utilities/parser.jl b/src/Utilities/parser.jl index 77d8f6a740..cf4d5a7c68 100644 --- a/src/Utilities/parser.jl +++ b/src/Utilities/parser.jl @@ -118,9 +118,9 @@ function _parse_function(ex) if ex.args[1] != :+ error( "Unsupport 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`." + "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[] diff --git a/test/Utilities/parser.jl b/test/Utilities/parser.jl index 9bfcfa7efc..5ca605159e 100644 --- a/test/Utilities/parser.jl +++ b/test/Utilities/parser.jl @@ -56,9 +56,9 @@ function test__parse_function() err = ErrorException( "Unsupport 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`." + "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)) From f492545f49d861297c18f16c89267f6cecfc7cfc Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 24 Aug 2021 16:57:58 +1200 Subject: [PATCH 3/3] Fix typo --- src/Utilities/parser.jl | 2 +- test/Utilities/parser.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Utilities/parser.jl b/src/Utilities/parser.jl index cf4d5a7c68..c9a91a742a 100644 --- a/src/Utilities/parser.jl +++ b/src/Utilities/parser.jl @@ -117,7 +117,7 @@ function _parse_function(ex) @assert isexpr(ex, :call) if ex.args[1] != :+ error( - "Unsupport operator in `loadfromstring!`: `$(ex.args[1])`. " * + "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`.", diff --git a/test/Utilities/parser.jl b/test/Utilities/parser.jl index 5ca605159e..a3a8096e1d 100644 --- a/test/Utilities/parser.jl +++ b/test/Utilities/parser.jl @@ -55,7 +55,7 @@ function test__parse_function() ) err = ErrorException( - "Unsupport operator in `loadfromstring!`: `-`. " * + "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`.",