From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,64d33c985c8959f0 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Received: by 10.43.117.133 with SMTP id fm5mr24045875icc.7.1320447239350; Fri, 04 Nov 2011 15:53:59 -0700 (PDT) Path: p6ni71820pbn.0!nntp.google.com!news1.google.com!postnews.google.com!a12g2000vbz.googlegroups.com!not-for-mail From: Jerry Newsgroups: comp.lang.ada Subject: Re: How does Ada.Text_IO.Enumeration_IO work? Date: Fri, 4 Nov 2011 15:46:36 -0700 (PDT) Organization: http://groups.google.com Message-ID: <67163342-cdc1-427d-9267-c41b51c9e08f@a12g2000vbz.googlegroups.com> References: <865d6298-5a0b-45c5-b0fc-372d6222752f@z22g2000prd.googlegroups.com> <7223b173-df96-4a12-82c0-268b7512e958@j36g2000prh.googlegroups.com> NNTP-Posting-Host: 97.117.196.229 Mime-Version: 1.0 X-Trace: posting.google.com 1320447233 17055 127.0.0.1 (4 Nov 2011 22:53:53 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 4 Nov 2011 22:53:53 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: a12g2000vbz.googlegroups.com; posting-host=97.117.196.229; posting-account=x5rpZwoAAABMN2XPwcebPWPkebpwQNJG User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: HLUARECNK X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4,gzip(gfe) Xref: news1.google.com comp.lang.ada:18833 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Date: 2011-11-04T15:46:36-07:00 List-Id: On Nov 3, 7:52=A0am, Adam Beneschan wrote: > On Nov 3, 1:52=A0am, Jerry wrote: > > > > > > > I don't understand how the following program works, specifically, how > > instances of Ada.Text_IO.Enumeration_IO read input. I understand that > > it will skip leading white space, leave anything after finding a > > proper element including line terminator, that apostophe ' is a valid > > character, and that it can raise an exception (quoting ARM): > > > "The exception Data_Error is propagated if the sequence input does not > > have the required syntax, or if the identifier or character literal > > does not correspond to a value of the subtype Enum." > > > I want the program to read input until a valid element is found, then > > quit. > > > with Ada.Text_IO; use Ada.Text_IO; > > with Ada.IO_Exceptions; use Ada.IO_Exceptions; > > procedure Day_Proc is > > =A0 =A0 type Day_Type is (Sunday, Monday, Tuesday); > > =A0 =A0 Day : Day_Type; > > =A0 =A0 package Day_Type_IO is new Ada.Text_IO.Enumeration_IO(Day_Type)= ; > > =A0 =A0 Have_Good_Value : Boolean; > > begin > > =A0 =A0 Put("Enter a day: "); > > =A0 =A0 loop > > =A0 =A0 =A0 =A0 Have_Good_Value :=3D True; > > =A0 =A0 =A0 =A0 begin > > =A0 =A0 =A0 =A0 =A0 =A0 Day_Type_IO.Get(Day); > > =A0 =A0 =A0 =A0 =A0 =A0 --Skip_Line; -- Doesn't matter if present or no= t. > > =A0 =A0 =A0 =A0 exception > > =A0 =A0 =A0 =A0 when Ada.IO_Exceptions.Data_Error =3D> > > =A0 =A0 =A0 =A0 =A0 =A0 Have_Good_Value :=3D False; > > =A0 =A0 =A0 =A0 =A0 =A0 Put_Line("Exception raised"); > > =A0 =A0 =A0 =A0 end; > > =A0 =A0 =A0 =A0 Put_Line(Boolean'image(Have_Good_Value)); > > =A0 =A0 =A0 =A0 exit when Have_Good_Value; > > =A0 =A0 end loop; > > =A0 =A0 Put_Line("Your day is " & Day_Type'image(Day)); > > end Day_Proc; > > > This works as I expect for inputs such as > > > =A0 =A0Monday > > Monday Friday > > Friday Monday > > Fri7day Monday > > Friday ' Monday > > Monday. > > =A0etc. > > > but anytime the input contains a non-apostrophe punctuation mark > > before a valid element, or an otherwise inproperly syntaxed element, > > it loops endlessly, outputting Exception raised and FALSE for each > > passage through the loop. For instance, these lines cause infinite > > looping: > > > Friday. Monday > > Friday.Monday > > Friday ? Monday > > Fri?day Monday > > Friday 7Thursday Monday > > This is (or is not) a comment > > =A0etc. > > > It is correctly raising the exception upon encountering the bad input, > > but why does it keep looping and not proceed past the bad input to > > find the following correct element? > > 'Cuz the rules say so. =A0A.10.6(5) is the important one here: "Next, > characters are input only so long as the sequence input is an initial > sequence of an identifier or of a character literal (in particular, > input ceases when a line terminator is encountered). The character or > line terminator that causes input to cease remains available for > subsequent input." =A0A.10.6(10): "The exception Data_Error is > propagated by a Get procedure if the sequence finally input is not a > lexical element corresponding to the type, in particular if no > characters were input ...". =A0That's the case when your input (skipping > leading blanks) starts with an invalid character like a comma. =A0Since > the comma can't be the first character of an enumeration literal, the > comma "remains available for subsequent input", and thus "no > characters are input" and Data_Error is raised. > > By the way, apostrophes are "allowed" but they have to be in the > correct syntax. =A0So if your line begins with > > 'ABCDE' > > I believe the characters 'A will be input, but since 'AB cannot be the > start of an enumeration literal, input stops at that point, and B will > be the next character available for input. > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-- Adam [Not sure why my earlier reply didn't make it but here is a reconstruction of it.] Thanks, Adam. I believe that the clue for me is that "remains available for subsequent input" means that it is available only to non- enumeration input, characters excepted. Jerry