comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Help parsing the language manual on Get'ing integers from Strings
Date: Mon, 21 Dec 2020 08:57:36 +0100	[thread overview]
Message-ID: <rrpkhg$1ct2$1@gioia.aioe.org> (raw)
In-Reply-To: 75fbea31-93f7-4b0d-bd73-34c4beefff44n@googlegroups.com

On 2020-12-21 01:11, John Perry wrote:

> Sorry if the subject is unclear. I recently tried to use
> 
>     Get(S, Value, Last);
> 
> ...in a program where Value was a Natural and S has the value "29: 116 82 | 119 24". GNAT gave me a Data_Error.
> 
> I don't understand why. Here's what the language manual says:
> 
> "Reads an integer value from the beginning of the given string, following the same rules as the Get procedure that reads an integer value from a file, but treating the end of the string as a file terminator. ...The exception Data_Error is propagated if the sequence input does not have the required syntax or if the value obtained is not of the subtype Num."
> 
> The referenced Get procedure says, (some irrelevant (?) parts omitted)
> 
> "...skips any leading blanks, line terminators, or page terminators, then ...reads the longest possible sequence of characters matching the syntax of a numeric literal without a point."
> 
> I've used this procedure before, and as far as I can tell:
> 
>     - GNAT is fine with "29:"
>     - GNAT is NOT fine with "29: " or any larger substring of S
> 
> So:
> 
> 1) Apparently GNAT thinks the colon is a character that matches the syntax of a numeric literal; do I interpret this correctly?
> 
> 2) Where does the language manual say this? I didn't see it in Section 3.5.4 ("Integer Types").
> 
> 3) or is this a bug?
> 
> 4) or do I misinterpret the language manual?

I think the problem is that the implementation tries to interpret

   29: 116 ...

as a based number. Colon : is a replacement character for # (see 
allowable replacements of characters). So it might think of 29: 116 as a 
malformed base-29 number with wrong base and missing closing :.

You could use this

    http://dmitry-kazakov.de/ada/strings_edit.htm

instead. Its Get has the base as the argument and uses string index 
rather than inconvenient in case of parsing Last.

Parsing your string would look like:

    Pointer := S'First; -- Start here
    Get (S, Pointer);   -- Skip blanks
    Get (S, Pointer, Value_1);
    Get (S, Pointer);
    Get (S, Pointer, Value_2);
    Get (S, Pointer);
    Get (S, Pointer, Value_3);
    Get (S, Pointer);
    if not Is_Prefix ("|", S, Pointer) then
       raise Data_Error;
    else
       Pointer := Pointer + 1;
    end if;
    Get (S, Pointer);
    Get (S, Pointer, Value_4);
    Get (S, Pointer);
    Get (S, Pointer, Value_5);

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  parent reply	other threads:[~2020-12-21  7:57 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-21  0:11 Help parsing the language manual on Get'ing integers from Strings John Perry
2020-12-21  7:44 ` Niklas Holsti
2020-12-21  9:33   ` AdaMagica
2020-12-21  7:57 ` Dmitry A. Kazakov [this message]
2020-12-21  8:06   ` Niklas Holsti
2020-12-21  9:40     ` Jeffrey R. Carter
2020-12-22  1:11       ` Randy Brukardt
2020-12-21  8:16   ` Dmitry A. Kazakov
2020-12-21 11:30 ` John Perry
2020-12-21 23:25   ` John Perry
2020-12-22  1:19     ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox