[m-rev.] for review: parsing_utils, allow optional + sign in float exponent
Peter Wang
novalazy at gmail.com
Wed Jun 13 13:46:05 AEST 2012
Branches: main, 11.07
library/parsing_utils.m:
Let float_literal, float_literal_as_string accept an optional plus sign
in the exponent. This matches the syntax for Mercury and many other
languages.
tests/general/test_parsing_utils.m:
tests/general/test_parsing_utils.exp:
tests/general/test_parsing_utils.exp2:
Update test case.
diff --git a/library/parsing_utils.m b/library/parsing_utils.m
index 66e1151..d1135ed 100644
--- a/library/parsing_utils.m
+++ b/library/parsing_utils.m
@@ -202,7 +202,7 @@
%
:- pred eof(src::in, unit::out, ps::in, ps::out) is semidet.
- % Parse a float literal matching [-][0-9]+[.][0-9]+([Ee][-][0-9]+)?
+ % Parse a float literal matching [-][0-9]+[.][0-9]+([Ee][-+][0-9]+)?
% followed by any whitespace. The float_literal_as_string version simply
% returns the matched string. The float_literal version uses
% string.to_float to convert the output of float_literal_as_string; this
@@ -828,7 +828,7 @@ float_literal_as_string(Src, FloatStr, !PS) :-
next_char(Src, ('.'), !PS),
digits(10, Src, _, !PS),
( if char_in_class("eE", Src, _, !PS) then
- ( if next_char(Src, ('-'), !PS) then true else true ),
+ optional_sign(Src, !PS),
digits(10, Src, _, !PS)
else
true
@@ -837,6 +837,20 @@ float_literal_as_string(Src, FloatStr, !PS) :-
skip_whitespace(Src, !PS),
input_substring(Src, Start, EndPlusOne, FloatStr).
+:- pred optional_sign(src::in, ps::in, ps::out) is det.
+
+optional_sign(Src, !PS) :-
+ ( if
+ next_char(Src, Char, !PS),
+ ( Char = ('-')
+ ; Char = ('+')
+ )
+ then
+ true
+ else
+ true
+ ).
+
%-----------------------------------------------------------------------------%
float_literal(Src, Float, !PS) :-
diff --git a/tests/general/test_parsing_utils.exp b/tests/general/test_parsing_utils.exp
index fa22c89..7f9af9a 100644
--- a/tests/general/test_parsing_utils.exp
+++ b/tests/general/test_parsing_utils.exp
@@ -117,9 +117,21 @@ pass: float_literal_as_string on "-123.0 abc"
pass: float_literal_as_string on "-123.0e1 abc"
returned "-123.0e1" as expected
[11 chars consumed]
+pass: float_literal_as_string on "-123.0e-1 abc"
+ returned "-123.0e-1" as expected
+ [12 chars consumed]
+pass: float_literal_as_string on "-123.0e+1 abc"
+ returned "-123.0e+1" as expected
+ [12 chars consumed]
+pass: float_literal_as_string on "-123.0E1 abc"
+ returned "-123.0E1" as expected
+ [11 chars consumed]
pass: float_literal_as_string on "-123.0E-1 abc"
returned "-123.0E-1" as expected
[12 chars consumed]
+pass: float_literal_as_string on "-123.0E+1 abc"
+ returned "-123.0E+1" as expected
+ [12 chars consumed]
pass: float_literal on ""
failed as expected
pass: float_literal on "abc"
@@ -138,6 +150,9 @@ pass: float_literal on "-123.0 abc"
pass: float_literal on "-123.0e1 abc"
returned -1230.0 as expected
[11 chars consumed]
+pass: float_literal on "-123.0e+1 abc"
+ returned -1230.0 as expected
+ [12 chars consumed]
pass: float_literal on "-123.0E-1 abc"
returned -12.3 as expected
[12 chars consumed]
diff --git a/tests/general/test_parsing_utils.exp2 b/tests/general/test_parsing_utils.exp2
index 6f05593..64aaed1 100644
--- a/tests/general/test_parsing_utils.exp2
+++ b/tests/general/test_parsing_utils.exp2
@@ -117,9 +117,21 @@ pass: float_literal_as_string on "-123.0 abc"
pass: float_literal_as_string on "-123.0e1 abc"
returned "-123.0e1" as expected
[11 chars consumed]
+pass: float_literal_as_string on "-123.0e-1 abc"
+ returned "-123.0e-1" as expected
+ [12 chars consumed]
+pass: float_literal_as_string on "-123.0e+1 abc"
+ returned "-123.0e+1" as expected
+ [12 chars consumed]
+pass: float_literal_as_string on "-123.0E1 abc"
+ returned "-123.0E1" as expected
+ [11 chars consumed]
pass: float_literal_as_string on "-123.0E-1 abc"
returned "-123.0E-1" as expected
[12 chars consumed]
+pass: float_literal_as_string on "-123.0E+1 abc"
+ returned "-123.0E+1" as expected
+ [12 chars consumed]
pass: float_literal on ""
failed as expected
pass: float_literal on "abc"
@@ -138,6 +150,9 @@ pass: float_literal on "-123.0 abc"
pass: float_literal on "-123.0e1 abc"
returned -1230.0 as expected
[11 chars consumed]
+pass: float_literal on "-123.0e+1 abc"
+ returned -1230.0 as expected
+ [12 chars consumed]
pass: float_literal on "-123.0E-1 abc"
returned -12.3 as expected
[12 chars consumed]
diff --git a/tests/general/test_parsing_utils.m b/tests/general/test_parsing_utils.m
index e9f0460..86e5625 100644
--- a/tests/general/test_parsing_utils.m
+++ b/tests/general/test_parsing_utils.m
@@ -245,7 +245,15 @@ test_case("float_literal_as_string", stringify(float_literal_as_string),
test_case("float_literal_as_string", stringify(float_literal_as_string),
"-123.0e1 abc", yes("\"-123.0e1\"")).
test_case("float_literal_as_string", stringify(float_literal_as_string),
+ "-123.0e-1 abc", yes("\"-123.0e-1\"")).
+test_case("float_literal_as_string", stringify(float_literal_as_string),
+ "-123.0e+1 abc", yes("\"-123.0e+1\"")).
+test_case("float_literal_as_string", stringify(float_literal_as_string),
+ "-123.0E1 abc", yes("\"-123.0E1\"")).
+test_case("float_literal_as_string", stringify(float_literal_as_string),
"-123.0E-1 abc", yes("\"-123.0E-1\"")).
+test_case("float_literal_as_string", stringify(float_literal_as_string),
+ "-123.0E+1 abc", yes("\"-123.0E+1\"")).
test_case("float_literal", stringify(float_literal),
"", no).
@@ -262,6 +270,8 @@ test_case("float_literal", stringify(float_literal),
test_case("float_literal", stringify(float_literal),
"-123.0e1 abc", yes("-1230.0")).
test_case("float_literal", stringify(float_literal),
+ "-123.0e+1 abc", yes("-1230.0")).
+test_case("float_literal", stringify(float_literal),
"-123.0E-1 abc", yes("-12.3")).
test_case("int_literal_as_string", stringify(int_literal_as_string),
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to: mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions: mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the reviews
mailing list