All pastes #597958 Raw Edit

readtoken.adb

public text v1 · immutable
#597958 ·published 2007-06-30 23:17 UTC
rendered paste body
--   Author:         Christopher Waid                           
--   Course:         CIS310 010 
--   Assignment:     #2                                         
--   Due Date:       June 15, 2007                          
--   Filename:       readtoken.adb                                   
--   Purpose:        reads in data from file into segments
with Ada.Characters.Handling;
with Ada.Integer_Text_IO;
with Ada.Text_Io;	use Ada;

package body ReadToken is
	procedure ReadNextToken (Line_Number : in out Integer;fFile : in File_Type; Token : out String;Status: out Boolean) is
	  
	--   Function name:  readnexttoken                                   
	--   Description:    reads segment from file                            
	--   Parameters:     File_type fFile INPUT OUTPUT input file
	--                   String Token OUTPUT  segment          
	--   Return Value:   none

		--for the loop through the file
		Char	:	Character;
		--this is to keep track of the length of ids/numbers
		Len	:	Integer := 1;
		
		--this is just a extraction of id/number or symbol from the file
		Keyword: String(1..15):=(others=> ' ');
		--all caps version of keyword
		UpperKeyword: String(1..15):=(others=> ' ');
		--we use this for look ahead
		Next : Character;
		--unused, we need it for look_ahead
		Line_Break : Boolean := True;    
		
	begin
		--the status of success or failure of readnexttoken
		Status := True;
		--set to nothing
		Token := (others=> ' ');
		
		--look through input file
		while not Text_Io.End_Of_File (fFile) loop
			while not Text_IO.End_Of_Line (fFile) loop
					
				Text_IO.Get (File=>fFile, Item=>Char);
				case Char is
				--keep track of multicharacter segments
				when 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '.'=>
					--crazy way to add a character at a time to keyword
					Keyword(Len..Len+1):=Char & Keyword(Len + 1);
					--keeps track of the length for the current segnent
					Len:=Len+1;
					--if the next character is not of a type that is acceptable in a identifier/number
					--then we're done with this segment
					Text_IO.Look_Ahead (fFile, Next, Line_Break);
					case Next is
					when 'A' .. 'Z' | 'a' .. 'z' | '0' .. '9' | '.' =>  
						null;--we assume we're not done with this segment
					when others =>
						--we are done with this segment 
						--set to uppercase
						UpperKeyword:=Ada.Characters.Handling.To_Upper(Keyword);
						--we're done with this so set length to 1, token to upperkeyword
						Token:=UpperKeyword;
						Len:=1;
						--reset keyword
						Keyword(1..15):=(others=> ' ');
						return; --finished, end of procedure
					end case;  
				when ' ' =>
					null; -- we don't need to do anything for this since we're not keeping track of it
				when ',' =>
					Token(1..1):=",";
					return;
				when ':' =>
					Token(1..1):=":";
					return;
				when ';' =>
					Token(1..1):=";";
					return;
				when '(' =>
					Token(1..1):="(";
					return;
				when ')' =>
					Token(1..1):=")";
					return;
				when '*' =>
					Token(1..1):="*";
					return;
				when '+' =>
					Token(1..1):="+";
					return;
				when '=' =>
					Token(1..1):="=";
					return;
				when others =>            
					null;
				end case;
        		end loop;
        		Text_IO.Skip_Line (fFile);  -- Move pointer in input buffer past EOL
			Line_Number:=Line_Number + 1;
   		end loop;
		
   		--Text_IO.Close(fFile);
		Status:=False;
	end ReadNextToken;
end ReadToken;