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.42.176.130 with SMTP id be2mr3897435icb.3.1320331967498; Thu, 03 Nov 2011 07:52:47 -0700 (PDT) Path: p6ni66743pbn.0!nntp.google.com!news1.google.com!postnews.google.com!j36g2000prh.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: How does Ada.Text_IO.Enumeration_IO work? Date: Thu, 3 Nov 2011 07:52:47 -0700 (PDT) Organization: http://groups.google.com Message-ID: <7223b173-df96-4a12-82c0-268b7512e958@j36g2000prh.googlegroups.com> References: <865d6298-5a0b-45c5-b0fc-372d6222752f@z22g2000prd.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 X-Trace: posting.google.com 1320331967 1691 127.0.0.1 (3 Nov 2011 14:52:47 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 3 Nov 2011 14:52:47 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: j36g2000prh.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: ARLUEHNKC X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C),gzip(gfe) Xref: news1.google.com comp.lang.ada:18804 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Date: 2011-11-03T07:52:47-07:00 List-Id: 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 not. > =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. A.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." A.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 ...". That's the case when your input (skipping leading blanks) starts with an invalid character like a comma. Since 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. So 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. -- Adam