From: Adam Beneschan <adam@irvine.com>
Subject: Re: How does Ada.Text_IO.Enumeration_IO work?
Date: Thu, 3 Nov 2011 07:52:47 -0700 (PDT)
Date: 2011-11-03T07:52:47-07:00 [thread overview]
Message-ID: <7223b173-df96-4a12-82c0-268b7512e958@j36g2000prh.googlegroups.com> (raw)
In-Reply-To: 865d6298-5a0b-45c5-b0fc-372d6222752f@z22g2000prd.googlegroups.com
On Nov 3, 1:52 am, Jerry <lancebo...@qwest.net> 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
> type Day_Type is (Sunday, Monday, Tuesday);
> Day : Day_Type;
> package Day_Type_IO is new Ada.Text_IO.Enumeration_IO(Day_Type);
> Have_Good_Value : Boolean;
> begin
> Put("Enter a day: ");
> loop
> Have_Good_Value := True;
> begin
> Day_Type_IO.Get(Day);
> --Skip_Line; -- Doesn't matter if present or not.
> exception
> when Ada.IO_Exceptions.Data_Error =>
> Have_Good_Value := False;
> Put_Line("Exception raised");
> end;
> Put_Line(Boolean'image(Have_Good_Value));
> exit when Have_Good_Value;
> end loop;
> Put_Line("Your day is " & Day_Type'image(Day));
> end Day_Proc;
>
> This works as I expect for inputs such as
>
> Monday
> Monday Friday
> Friday Monday
> Fri7day Monday
> Friday ' Monday
> Monday.
> etc.
>
> 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
> etc.
>
> 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
next prev parent reply other threads:[~2011-11-03 14:52 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-11-03 8:52 How does Ada.Text_IO.Enumeration_IO work? Jerry
2011-11-03 9:27 ` AdaMagica
2011-11-03 10:18 ` Jerry
2011-11-03 14:52 ` Adam Beneschan [this message]
2011-11-04 22:46 ` Jerry
2011-11-04 23:31 ` Adam Beneschan
2011-11-05 9:32 ` Dmitry A. Kazakov
2011-11-06 22:42 ` Jerry
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox