with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with GNAT.Regpat; use GNAT.Regpat;
with Objects; use Objects;
package Tokens is
type Token_Type is (
TK_AND,
TK_BANG,
TK_COMMA,
TK_EQUALS,
TK_GREATER,
TK_GREATER_EQUALS,
TK_LEFT_PARENTHESIS,
TK_LESS,
TK_LESS_EQUALS,
TK_MINUS,
TK_NOT_EQUALS,
TK_OR,
TK_PERCENT,
TK_PLUS,
TK_RIGHT_PARENTHESIS,
TK_SLASH,
TK_STAR,
TK_IDENTIFIER,
TK_NUMBER,
TK_STRING,
TK_EOF
);
type Token_Location is
record
line : Natural := 0;
col : Natural := 0;
end record;
function To_String( loc : Token_Location ) return String;
type Token is new Limited_Object with private;
type A_Token is access all Token'Class;
function Create_Token( text : String; loc : Token_Location ) return A_Token;
function Get_Location( this : not null access Token'Class ) return Token_Location;
function Get_Type( this : not null access Token'Class ) return Token_Type;
procedure Delete( this : in out A_Token );
pragma Postcondition( this = null );
type Identifier_Token is new Token with private;
type A_Identifier_Token is access all Identifier_Token'Class;
function Get_Name( this : not null access Identifier_Token'Class ) return String;
type Number_Token is new Token with private;
type A_Number_Token is access all Number_Token'Class;
function Get_Value( this : not null access Number_Token'Class ) return Long_Float;
type String_Token is new Token with private;
type A_String_Token is access all String_Token'Class;
function Get_Value( this : not null access String_Token'Class ) return String;
private
type TokenStringArray is array (Token_Type) of access String;
tokenImage : constant TokenStringArray := (
TK_AND => new String'("&&"),
TK_BANG => new String'("!"),
TK_COMMA => new String'(","),
TK_EQUALS => new String'("="),
TK_GREATER => new String'(">"),
TK_GREATER_EQUALS => new String'(">="),
TK_LEFT_PARENTHESIS => new String'("("),
TK_LESS => new String'("<"),
TK_LESS_EQUALS => new String'("<="),
TK_MINUS => new String'("-"),
TK_NOT_EQUALS => new String'("!="),
TK_OR => new String'("||"),
TK_PERCENT => new String'("%"),
TK_PLUS => new String'("+"),
TK_RIGHT_PARENTHESIS => new String'(")"),
TK_SLASH => new String'("/"),
TK_STAR => new String'("*"),
TK_IDENTIFIER => null,
TK_NUMBER => null,
TK_STRING => null,
TK_EOF => new String'("")
);
type Token is new Limited_Object with
record
tokenType : Token_Type;
loc : Token_Location;
end record;
procedure Construct( this : access Token;
tokenType : Token_Type;
loc : Token_Location );
function Create_Token( tokenType : Token_Type;
loc : Token_Location ) return A_Token;
identPattern : constant Pattern_Matcher := Compile("^[a-zA-Z]+[a-zA-Z0-9_.]*$");
type Identifier_Token is new Token with
record
name : Unbounded_String;
end record;
procedure Construct( this : access Identifier_Token;
name : String;
loc : Token_Location );
function Create_Identifier_Token( text : String;
loc : Token_Location ) return A_Token;
numPattern : constant Pattern_Matcher := Compile("^([0-9]+)|([0-9]*\.[0-9]+)$");
type Number_Token is new Token with
record
val : Long_Float;
end record;
procedure Construct( this : access Number_Token;
val : Long_Float;
loc : Token_Location );
function Create_Number_Token( text : String;
loc : Token_Location ) return A_Token;
type String_Token is new Token with
record
val : Unbounded_String;
end record;
procedure Construct( this : access String_Token;
val : String;
loc : Token_Location );
function Create_String_Token( text : String;
loc : Token_Location ) return A_Token;
end Tokens;