1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
-------------------------------------------------------------------------------
-- (C) Altran Praxis Limited
-------------------------------------------------------------------------------
--
-- The SPARK toolset is free software; you can redistribute it and/or modify it
-- under terms of the GNU General Public License as published by the Free
-- Software Foundation; either version 3, or (at your option) any later
-- version. The SPARK toolset is distributed in the hope that it will be
-- useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
-- Public License for more details. You should have received a copy of the GNU
-- General Public License distributed with the SPARK toolset; see file
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy of
-- the license.
--
--=============================================================================
with LexTokenManager;
with SparkLex;
with SP_Symbols;
with SparkMakeErrors;
use type SP_Symbols.SP_Terminal;
package body TokenManager is
procedure Next (It : in out Iterator) is
Unused : Boolean;
Token_Kind : SP_Symbols.SP_Terminal;
Lex_Value : LexTokenManager.Lex_Value;
procedure Get_Identifier (It : in out Iterator)
--# global in CommandLineData.Content;
--# in Dictionary.Dict;
--# in out ErrorHandler.Error_Context;
--# in out LexTokenManager.State;
--# in out SparkLex.Curr_Line;
--# in out SPARK_IO.File_Sys;
--# derives ErrorHandler.Error_Context,
--# It,
--# LexTokenManager.State,
--# SparkLex.Curr_Line,
--# SPARK_IO.File_Sys from CommandLineData.Content,
--# Dictionary.Dict,
--# ErrorHandler.Error_Context,
--# It,
--# LexTokenManager.State,
--# SparkLex.Curr_Line,
--# SPARK_IO.File_Sys;
--# pre SparkLex.Curr_Line_Invariant (SparkLex.Curr_Line);
--# post SparkLex.Curr_Line_Invariant (SparkLex.Curr_Line);
is
Unused : Boolean;
Token_Kind : SP_Symbols.SP_Terminal;
Lex_Value : LexTokenManager.Lex_Value;
begin
while It /= Null_Iterator loop
--# assert SparkLex.Curr_Line_Invariant (SparkLex.Curr_Line);
-- is there more dotted notation?
--# accept Flow, 10, Unused, "Ineffective assignment OK";
SparkLex.Examiner_Lex (Prog_Text => It.File,
Token => Token_Kind,
Lex_Val => Lex_Value,
Punct_Token => Unused);
--# end accept;
-- No, this is the end of the identifier and we have read a token too far.
if Token_Kind /= SP_Symbols.point then
It.Next_Token :=
Token'(Kind => Token_Kind,
Value => LexTokenManager.Lex_String_To_String (Lex_Str => Lex_Value.Token_Str));
exit;
end if;
-- Read the next part of the dotted identifier
--# accept Flow, 10, Unused, "Ineffective assignment OK";
SparkLex.Examiner_Lex (Prog_Text => It.File,
Token => Token_Kind,
Lex_Val => Lex_Value,
Punct_Token => Unused);
--# end accept;
if Token_Kind /= SP_Symbols.identifier then
-- Invlaid syntax return a null iterator
It := Null_Iterator;
else
E_Strings.Append_String (E_Str => It.Current_Token.Value,
Str => ".");
E_Strings.Append_Examiner_String
(E_Str1 => It.Current_Token.Value,
E_Str2 => LexTokenManager.Lex_String_To_String (Lex_Str => Lex_Value.Token_Str));
end if;
end loop;
--# accept Flow, 33, Unused, "Unused not references OK";
end Get_Identifier;
begin
if It = Null_Iterator then
SparkMakeErrors.Fatal ("calling TokenManager.Next with a null iterator.");
elsif It.Is_Look_Ahead then
It.Is_Look_Ahead := False;
elsif It.Next_Token /= Null_Token then
It.Current_Token := It.Next_Token;
It.Next_Token := Null_Token;
else
--# accept Flow, 10, Unused, "Ineffective assignment OK";
SparkLex.Examiner_Lex (Prog_Text => It.File,
Token => Token_Kind,
Lex_Val => Lex_Value,
Punct_Token => Unused);
--# end accept;
if Token_Kind = SP_Symbols.SPEND then
It := Null_Iterator;
else
It.Current_Token :=
Token'(Kind => Token_Kind,
Value => LexTokenManager.Lex_String_To_String (Lex_Str => Lex_Value.Token_Str));
if Token_Kind = SP_Symbols.identifier then
Get_Identifier (It => It);
end if;
end if;
end if;
--# accept Flow, 33, Unused, "Unused not references OK";
end Next;
-----------------------------------------------------------------------------
procedure Look_Ahead (It : in out Iterator) is
begin
if It = Null_Iterator then
SparkMakeErrors.Fatal ("calling TokenManager.Look_Ahead with a null iterator.");
elsif not It.Is_Look_Ahead then
Next (It => It);
It.Is_Look_Ahead := True;
end if;
end Look_Ahead;
-----------------------------------------------------------------------------
procedure Get_First_Token (File_Id : in SPARK_IO.File_Type;
It : out Iterator) is
New_It : Iterator;
begin
SparkLex.Clear_Line_Context;
New_It := Iterator'(File => File_Id,
Current_Token => Null_Token,
Next_Token => Null_Token,
Is_Look_Ahead => False);
Next (It => New_It);
It := New_It;
end Get_First_Token;
-----------------------------------------------------------------------------
function Is_Null (It : Iterator) return Boolean is
begin
return It = Null_Iterator;
end Is_Null;
-----------------------------------------------------------------------------
function Current (It : Iterator) return Token is
Result : Token;
begin
if It = Null_Iterator then
Result := Null_Token;
else
Result := It.Current_Token;
end if;
return Result;
end Current;
-----------------------------------------------------------------------------
function To_String (Tok : Token) return E_Strings.T is
--# hide To_String;
Result : E_Strings.T := E_Strings.Empty_String;
begin
E_Strings.Append_String (E_Str => Result,
Str => SP_Symbols.SP_Terminal'Image (Tok.Kind) & ": ");
E_Strings.Append_Examiner_String (E_Str1 => Result,
E_Str2 => Tok.Value);
return Result;
end To_String;
end TokenManager;
|