From: Syntax Issues <syntax.issues@gmail.com>
Subject: Re: Text parsing package
Date: Wed, 23 Mar 2011 04:19:29 -0700 (PDT)
Date: 2011-03-23T04:19:29-07:00 [thread overview]
Message-ID: <814c977e-5b95-4b84-b5ba-2039ca3b6f26@d26g2000prn.googlegroups.com> (raw)
In-Reply-To: v6w93r8pxrcx$.b590s9zimz0o$.dlg@40tude.net
Thanks for posting the techniques Dmitry. I am going to try
implementing as many as I can -- I am still a fairly new programmer,
and I still have a lot to learn.
Right now it uses text_io and strings.unbounded.text_io (this is
probably bad :/) and only works with files. I tried to make it as
simple as possible with exception checking -- global booleans are set
and checked by the running program.
-- Spec
with
Ada.Exceptions,
Ada.Text_Io,
Ada.Strings.Unbounded.Text_Io,
Ada.Strings.Unbounded;
use
Ada.Exceptions,
Ada.Text_Io,
Ada.Strings.Unbounded.Text_Io,
Ada.Strings.Unbounded;
package Parsing
is
---------------
-- Constants --
---------------
DEBUGGING_ON : constant Boolean := true;
EXCEPTION_PARSE_NOTHING : constant String :=
"Attempted to parse passed the end-of-file.";
EXCEPTION_PARSE_UNOPENED_FILE : constant String :=
"Attempted to parse an unopened file.";
EXCEPTION_PARSE_NON_NUMBER : constant String := "Failed
to parse a number.";
NULL_STRING_FIXED : constant String := "";
NULL_STRING_UNBOUNDED : constant Unbounded_String :=
To_Unbounded_String(NULL_STRING_FIXED);
--------------
-- Packages --
--------------
package Io_Integer
is new Ada.Text_Io.Integer_Io(Integer);
package Io_Float
is new Ada.Text_Io.Float_Io(Float);
---------------
-- Variables --
---------------
Error_On_Recent_Operation : Boolean := false;
Error_Occured_Parsing_File : Boolean := false;
Line : Unbounded_String :=
NULL_STRING_UNBOUNDED;
File : File_Type;
-----------------
-- Subprograms --
-----------------
procedure Open
(Name : in String);
pragma Inline(Open);
procedure Close;
pragma Inline(Close);
function Next_Unbounded_String
return Unbounded_String;
function Next_String
return String;
pragma Inline(Next_String);
function Next_Integer
return Integer;
function Next_Float
return Float;
end Parsing;
--- Body
package body Parsing
is
--
-- Open_File
--
procedure Open
(Name : in String)
is
begin
Ada.Text_Io.Open(File, In_File, Name);
if not End_Of_File(File) then
Line := Get_Line(File);
end if;
end Open;
--
-- Close_File
--
procedure Close
is
begin
if Is_Open(File) then
Ada.Text_Io.Close(File);
Error_Occured_Parsing_File := false;
end if;
end Close;
--
-- Next_Unbounded_String
--
function Next_Unbounded_String
return Unbounded_String
is
Result : Unbounded_String := NULL_UNBOUNDED_STRING;
begin
Error_On_Recent_Operation := false;
Trim(Line, Ada.Strings.Both);
if not Is_Open(File) then
if DEBUGGING_ON then
Put_Line(EXCEPTION_PARSE_UNOPENED_FILE);
end if;
Error_Occured_Parsing_File := true;
Error_On_Recent_Operation := true;
return Result;
end if;
loop
if Length(Line) /= 0 then
for I in 1..Length(Line) loop
if Element(Line, I) = ' ' or I = Length(Line) then
Result := To_Unbounded_String(Slice(Line, 1, I));
Delete(Line, 1, I);
return Result;
end if;
end loop;
else
if End_Of_File(File) then
if DEBUGGING_ON then
Put_Line(EXCEPTION_PARSE_NOTHING);
end if;
Error_Occured_Parsing_File := true;
Error_On_Recent_Operation := true;
return Result;
end if;
Line := Trim(Get_Line(File), Ada.Strings.Both);
end if;
end loop;
end Next_Unbounded_String;
--
-- Next_String
--
function Next_String
return String
is
begin
return To_String(Next_Unbounded_String);
end Next_String;
--
-- Next_Integer
--
function Next_Integer
return Integer
is
Last : Positive;
Result : Integer := 0;
begin
Io_Integer.Get(To_String(Next_Unbounded_String), Result, Last);
return Result;
exception
when Data_Error | Constraint_Error =>
if DEBUGGING_ON then
Put_Line(EXCEPTION_PARSE_NON_NUMBER);
end if;
Error_Occured_Parsing_File := true;
Error_On_Recent_Operation := true;
return Result;
end Next_Integer;
--
-- Next_Float
--
function Next_Float
return Float
is
Last : Positive;
Result : Float := 0.0;
begin
Io_Float.Get(To_String(Next_Unbounded_String), Result, Last);
return Result;
exception
when Data_Error | Constraint_Error =>
if DEBUGGING_ON then
Put_Line(EXCEPTION_PARSE_NON_NUMBER);
end if;
Error_Occured_Parsing_File := true;
Error_On_Recent_Operation := true;
return Result;
end Next_Float;
end Parsing;
next prev parent reply other threads:[~2011-03-23 11:19 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-03-22 23:34 Text parsing package Syntax Issues
2011-03-23 3:01 ` Shark8
2011-03-23 6:29 ` Alex Mentis
2011-03-23 6:36 ` J-P. Rosen
2011-03-23 8:32 ` Dmitry A. Kazakov
2011-03-23 11:19 ` Syntax Issues [this message]
2011-03-28 0:15 ` Yannick Duchêne (Hibou57)
2011-03-28 8:15 ` Dmitry A. Kazakov
2011-03-28 10:18 ` Yannick Duchêne (Hibou57)
2011-03-28 12:08 ` Dmitry A. Kazakov
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox