]> gcc.gnu.org Git - gcc.git/commitdiff
ada: Fix endings of pretty-printed numeric literals
authorPiotr Trojanek <trojanek@adacore.com>
Fri, 17 Feb 2023 23:02:08 +0000 (00:02 +0100)
committerMarc Poulhiès <poulhies@adacore.com>
Tue, 23 May 2023 07:59:04 +0000 (09:59 +0200)
When looking for the end of an numeric literal we consumed '+' and '-'
characters, because they might appear in the exponent part. This was too
aggressive when they separated the number from the subsequent operand,
like in "123+456". Now we skip past numeric literals by strictly
following their grammar and only consume '+' and '-' when they belong to
the exponent.

gcc/ada/

* errout.adb (Last_Sloc): Rewrite skipping past numeric literals.

gcc/ada/errout.adb

index 5c3e76c5ecaff48d06579a73bb338cc8d225d854..6531410f0d230c0cea0543bdd2e9ac47b44db793 100644 (file)
@@ -2021,33 +2021,129 @@ package body Errout is
          when N_Character_Literal =>
             return S + 2;
 
-         --  Skip past numeric literals, but they allow a wider set of
-         --  characters than keywords and identifiers.
+         --  Skip past integer literals, both decimal and based, integer and
+         --  real. We can't greedily accept all allowed character, because
+         --  we would consme too many of them in expressions like "123+ABC"
+         --  or "123..456", so we follow quite precisely the Ada grammar and
+         --  consume different characters depending on the context.
 
          when N_Integer_Literal =>
-            while S in SF .. SL - 1
-              and then Src (S + 1)
-                in
-              '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_' | '#' | '+' | '-'
+
+            --  Skip past the initial numeral, which either leads the decimal
+            --  literal or is the base of a based literal.
+
+            while S < SL
+              and then Src (S + 1) in '0' .. '9' | '_'
             loop
                S := S + 1;
             end loop;
 
-         when N_Real_Literal =>
-            declare
-               Dot_Seen : Boolean := False;
-            begin
-               while S in SF .. SL - 1
+            --  Skip past #based_numeral#, if present
+
+            if S < SL
+              and then Src (S + 1) = '#'
+            then
+               S := S + 1;
+
+               while S < SL
                  and then
-                   (Src (S + 1) in '0' .. '9'
-                                 | 'a' .. 'f' | 'A' .. 'F'
-                                 | '_' | '#' | '+' | '-'
-                      or else (Src (S + 1) = '.' and then not Dot_Seen))
+                   Src (S + 1) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_'
                loop
-                  Dot_Seen := Src (S + 1) = '.';
                   S := S + 1;
                end loop;
-            end;
+
+               pragma Assert (S < SL and then Src (S + 1) = '#');
+
+               S := S + 1;
+            end if;
+
+            --  Skip past exponent, if present
+
+            if S < SL + 1
+              and then Src (S + 1) in 'e' | 'E'
+            then
+               --  For positive exponents the plus sign is optional, but we
+               --  can simply skip past both plus and minus.
+
+               if Src (S + 2) in '+' | '-' then
+                  S := S + 1;
+               end if;
+
+               --  Skip past the numeral part
+
+               while S < SL
+                 and then Src (S + 1) in '0' .. '9' | '_'
+               loop
+                  S := S + 1;
+               end loop;
+            end if;
+
+         when N_Real_Literal =>
+            --  Skip past the initial numeral, which either leads the decimal
+            --  literal or is the base of a based literal.
+
+            while S < SL
+              and then Src (S + 1) in '0' .. '9' | '_'
+            loop
+               S := S + 1;
+            end loop;
+
+            if S < SL then
+               if Src (S + 1) = '.' then
+                  while S < SL
+                    and then Src (S + 1) in '0' .. '9' | '_'
+                  loop
+                     S := S + 1;
+                  end loop;
+               else
+                  pragma Assert (Src (S + 1) = '#');
+
+                  S := S + 1;
+
+                  while S < SL
+                    and then
+                      Src (S + 1) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_'
+                  loop
+                     S := S + 1;
+                  end loop;
+
+                  pragma Assert (S < SL and then Src (S + 1) = '.');
+
+                  S := S + 1;
+
+                  while S < SL
+                    and then
+                      Src (S + 1) in '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' | '_'
+                  loop
+                     S := S + 1;
+                  end loop;
+
+                  pragma Assert (S < SL and then Src (S + 1) = '#');
+
+                  S := S + 1;
+               end if;
+            end if;
+
+            --  Skip past exponent, if present
+
+            if S < SL + 1
+              and then Src (S + 1) in 'e' | 'E'
+            then
+               --  For positive exponents the plus sign is optional, but we
+               --  can simply skip past both plus and minus.
+
+               if Src (S + 2) in '+' | '-' then
+                  S := S + 1;
+               end if;
+
+               --  Skip past the numeral part
+
+               while S < SL
+                 and then Src (S + 1) in '0' .. '9' | '_'
+               loop
+                  S := S + 1;
+               end loop;
+            end if;
 
          --  For other nodes simply skip past a keyword/identifier
 
This page took 0.07517 seconds and 5 git commands to generate.