----------------------------------------------------------------------------
--                  written for use with aflex
----------------------------------------------------------------------------
--/*------------------------------------------------------------------------*/
--/* Lexical input for AFLEX for LALR(1) Grammar for ANSI Ada95             */
--/*                                                                        */
--/*               Dean R. Runzel                                           */
--/*      Monmouth College, West Long Branch, NJ                            */
--/*               February 16, 1995                                        */
--/*                                                                        */
--/* Accompanies Public Domain AYACC format Ada95 grammar                   */
--/*                                                                        */
--/* This work is based upon previous work performed by:                    */
--/*                                                                        */
--/* Herman Fischer  Litton Data Systems  1984                              */
--/* C. Schaefer                          1991                              */
--/* C. Howell                            1991                              */
--/* Richard Conn    Mitre, Inc.          1993                              */
--/*                 Intermetrics, Inc.   1994                              */
--/*                                                                        */
--/* This work is copyrighted by the author.                                */
--/*                                                                        */
--/* Please see the Parse95 reader for the complete text of all copyrights  */
--/* and acknowledgements.                                                  */
--/*                                                                        */
--/*------------------------------------------------------------------------*/
-- Modified for AdaDoc by Vincent Decorges & Julien Burdy
-- Date : 10.4.02
-- Note : This version is not anymore compatible with
--        scaflex use aflex instead

DIGIT            [0-9]
EXTENDED_DIGIT   [0-9a-fA-F]
INTEGER          ({DIGIT}(_?{DIGIT})*)
EXPONENT         ([eE](\+?|-){INTEGER})
DECIMAL_LITERAL  {INTEGER}(\.?{INTEGER})?{EXPONENT}?
BASE             {INTEGER}
BASED_INTEGER    {EXTENDED_DIGIT}(_?{EXTENDED_DIGIT})*
BASED_LITERAL    {BASE}#{BASED_INTEGER}(\.{BASED_INTEGER})?#{EXPONENT}?
IDENTIFIER       [a-zA-Z]("_"?[a-zA-Z0-9])*
STRING_LITERAL   \"([^"]*(\"\")*)*\"
CHAR_LITERAL     \'[^\n]\'

%%
-- Reserved Words
[aA][bB][oO][rR][tT]                { was_id := false; return(ABORT_TOKEN);}
[aA][bB][sS]                        { was_id := false; return(ABS_TOKEN);}
[aA][bB][sS][tT][rR][aA][cC][tT]    { was_id := false; return(ABSTRACT_TOKEN);}
[aA][cC][cC][eE][pP][tT]            { was_id := false; return(ACCEPT_TOKEN);}
[aA][cC][cC][eE][sS][sS]            { was_id := false; return(ACCESS_TOKEN);}
[aA][lL][iI][aA][sS][eE][dD]        { was_id := false; return(ALIASED_TOKEN);}
[aA][lL][lL]                        { was_id := false; return(ALL_TOKEN);}
[aA][nN][dD]                        { was_id := false; return(AND_TOKEN);}
[aA][rR][rR][aA][yY]                { was_id := false; return(ARRAY_TOKEN);}
[aA][tT]                            { was_id := false; return(AT_TOKEN);}

[bB][eE][gG][iI][nN]                { was_id := false; return(BEGIN_TOKEN);}
[bB][oO][dD][yY]                    { was_id := false; return(BODY_TOKEN);}

[cC][aA][sS][eE]                    { was_id := false;
                                      AdaDoc_Tree.Comment.Mark_Instruction
                                      (Natural'Image(Get_Current_Line));
                                      return(CASE_TOKEN);}
[cC][oO][nN][sS][tT][aA][nN][tT]    { was_id := false; return(CONSTANT_TOKEN);}

[dD][eE][cC][lL][aA][rR][eE]        { was_id := false; return(DECLARE_TOKEN);}
[dD][eE][lL][aA][yY]                { was_id := false; return(DELAY_TOKEN);}
[dD][eE][lL][tT][aA]                { was_id := false; return(DELTA_TOKEN);}
[dD][iI][gG][iI][tT][sS]            { was_id := false; return(DIGITS_TOKEN);}
[dD][oO]                            { was_id := false; return(DO_TOKEN);}

[eE][lL][sS][eE]                    { was_id := false; return(ELSE_TOKEN);}
[eE][lL][sS][iI][fF]                { was_id := false; return(ELSIF_TOKEN);}
[eE][nN][dD]                        { was_id := false; return(END_TOKEN);}
[eE][nN][tT][rR][yY]                { was_id := false;
                                      AdaDoc_Tree.Comment.Mark_Instruction
                                      (Natural'Image(Get_Current_Line));
                                      return(ENTRY_TOKEN);}
[eE][xX][cC][eE][pP][tT][iI][oO][nN] { was_id := false;
                                       AdaDoc_Tree.Comment.Mark_Instruction
                                       (Natural'Image(Get_Current_Line));
                                       return(EXCEPTION_TOKEN);}
[eE][xX][iI][tT]                    { was_id := false; return(EXIT_TOKEN);}

[fF][oO][rR]                        { was_id := false;
                                      AdaDoc_Tree.Comment.Mark_Instruction
                                      (Natural'Image(Get_Current_Line));
                                      return(FOR_TOKEN);}
[fF][uU][nN][cC][tT][iI][oO][nN]    { was_id := false;
                                      AdaDoc_Tree.Comment.Mark_Instruction
                                      (Natural'Image(Get_Current_Line));
                                      return(FUNCTION_TOKEN);}

[gG][eE][nN][eE][rR][iI][cC]        { was_id := false;
                                      AdaDoc_Tree.Comment.Mark_Instruction
                                      (Natural'Image(Get_Current_Line));
                                      return(GENERIC_TOKEN);}
[gG][oO][tT][oO]                    { was_id := false; return(GOTO_TOKEN);}

[iI][fF]                            { was_id := false; return(IF_TOKEN);}
[iI][nN]                            { was_id := false; return(IN_TOKEN);}
[iI][sS]                            { was_id := false; return(IS_TOKEN);}

[lL][iI][mM][iI][tT][eE][dD]        { was_id := false; return(LIMITED_TOKEN);}
[lL][oO][oO][pP]                    { was_id := false; return(LOOP_TOKEN);}

[mM][oO][dD]                        { was_id := false; return(MOD_TOKEN);}

[nN][eE][wW]                        { was_id := false; return(NEW_TOKEN);}
[nN][oO][tT]                        { was_id := false; return(NOT_TOKEN);}
[nN][uU][lL][lL]                    { was_id := false; return(NULL_TOKEN);}

[oO][fF]                            { was_id := false; return(OF_TOKEN);}
[oO][rR]                            { was_id := false; return(OR_TOKEN);}
[oO][tT][hH][eE][rR][sS]            { was_id := false; return(OTHERS_TOKEN);}
[oO][uU][tT]                        { was_id := false; return(OUT_TOKEN);}

[pP][aA][cC][kK][aA][gG][eE]        { was_id := false;
                                      AdaDoc_Tree.Comment.Mark_Instruction
                                      (Natural'Image(Get_Current_Line));
                                      return(PACKAGE_TOKEN);}
[pP][rR][aA][gG][mM][aA]            { was_id := false;
                                      AdaDoc_Tree.Comment.Mark_Instruction
                                      (Natural'Image(Get_Current_Line));
                                      return(PRAGMA_TOKEN);}
[pP][rR][iI][vV][aA][tT][eE]        { was_id := false;
                                      AdaDoc_Tree.Comment.Mark_Instruction
                                      (Natural'Image(Get_Current_Line));
                                      return(PRIVATE_TOKEN);}
[pP][rR][oO][cC][eE][dD][uU][rR][eE] { was_id := false;
                                       AdaDoc_Tree.Comment.Mark_Instruction
                                       (Natural'Image(Get_Current_Line));
                                       return(PROCEDURE_TOKEN);}
[pP][rR][oO][tT][eE][cC][tT][eE][dD] { was_id := false; return(PROTECTED_TOKEN);}

[rR][aA][iI][sS][eE]                { was_id := false; return(RAISE_TOKEN);}
[rR][aA][nN][gG][eE]                { was_id := false; return(RANGE_TOKEN);}
[rR][eE][cC][oO][rR][dD]            { was_id := false; return(RECORD_TOKEN);}
[rR][eE][mM]                        { was_id := false; return(REM_TOKEN);}
[rR][eE][nN][aA][mM][eE][sS]        { was_id := false; return(RENAMES_TOKEN);}
[rR][eE][qQ][uU][eE][uU][eE]        { was_id := false; return(RENAMES_TOKEN);}
[rR][eE][tT][uU][rR][nN]            { was_id := false; return(RETURN_TOKEN);}
[rR][eE][vV][eE][rR][sS][eE]        { was_id := false; return(REVERSE_TOKEN);}

[sS][eE][lL][eE][cC][tT]            { was_id := false; return(SELECT_TOKEN);}
[sS][eE][pP][aA][rR][aA][tT][eE]    { was_id := false; return(SEPARATE_TOKEN);}
[sS][uU][bB][tT][yY][pP][eE]        { was_id := false;
                                      AdaDoc_Tree.Comment.Mark_Instruction
                                      (Natural'Image(Get_Current_Line));
                                      return(SUBTYPE_TOKEN);}

[tT][aA][gG][gG][eE][dD]            { was_id := false; return(TAGGED_TOKEN);}
[tT][aA][sS][kK]                    { was_id := false;
                                      AdaDoc_Tree.Comment.Mark_Instruction
                                      (Natural'Image(Get_Current_Line));
                                      return(TASK_TOKEN);}
[tT][eE][rR][mM][iI][nN][aA][tT][eE] { was_id := false; return(TERMINATE_TOKEN);}
[tT][hH][eE][nN]                    { was_id := false; return(THEN_TOKEN);}
[tT][yY][pP][eE]                    { was_id := false;
                                      AdaDoc_Tree.Comment.Mark_Instruction
                                      (Natural'Image(Get_Current_Line));
                                      return(TYPE_TOKEN);}

[uU][nN][tT][iI][lL]                { was_id := false; return(UNTIL_TOKEN);}
[uU][sS][eE]                        { was_id := false;
                                      AdaDoc_Tree.Comment.Mark_Instruction
                                      (Natural'Image(Get_Current_Line));
                                      return(USE_TOKEN);}

[wW][hH][eE][nN]                    { was_id := false;
                                      AdaDoc_Tree.Comment.Mark_Instruction
                                      (Natural'Image(Get_Current_Line));
                                      return(WHEN_TOKEN);}
[wW][hH][iI][lL][eE]                { was_id := false; return(WHILE_TOKEN);}
[wW][iI][tT][hH]                    { was_id := false;
                                      AdaDoc_Tree.Comment.Mark_Instruction
                                      (Natural'Image(Get_Current_Line));
                                      return(WITH_TOKEN);}

[xX][oO][rR]                        { was_id := false; return(XOR_TOKEN);}

-- Delimiters
"=>"    { was_id := false; return(ARROW);}
".."    { was_id := false; return(DOUBLE_DOT);}
"**"    { was_id := false; return(DOUBLE_STAR);}
":="    { was_id := false; return(ASSIGNMENT);}
"/="    { was_id := false; return(INEQUALITY);}
">="    { was_id := false; return(GREATER_THAN_OR_EQUAL);}
"<="    { was_id := false; return(LESS_THAN_OR_EQUAL);}
"<<"    { was_id := false; return(LEFT_LABEL_BRACKET);}
">>"    { was_id := false; return(RIGHT_LABEL_BRACKET);}
"<>"    { was_id := false; return(BOX);}
"&"     { was_id := false; return('&'); }
"("     { was_id := false; return('('); }
")"     { was_id := false; return(')'); }
"*"     { was_id := false; return('*'); }
"+"     { was_id := false; return('+'); }
","     { was_id := false; return(','); }
"-"     { was_id := false; return('-'); }
"."     { was_id := false; return('.'); }
"/"     { was_id := false; return('/'); }
":"     { was_id := false; return(':'); }
";"     { was_id := false; return(';'); }
"<"     { was_id := false; return('<'); }
"="     { was_id := false; return('='); }
">"     { was_id := false; return('>'); }
"|"     { was_id := false; return('|'); }
"!"     { was_id := false; return('|'); } -- Ada Reference Manual 2.10(1)
"'"     { was_id := false; return('''); }

-- Literals
{IDENTIFIER}          { was_id := true; AdaDoc_Tree.Comment.Mark_Instruction
                        (Natural'Image(Get_Current_Line)); return(IDENTIFIER);}
{DECIMAL_LITERAL}     { was_id := false; return(DECIMAL_LITERAL);}
{BASED_LITERAL}       { was_id := false; return(BASED_LITERAL);}
{STRING_LITERAL}      { was_id := false; return(STRING_LITERAL);}
{CHAR_LITERAL}        {
                        --| kludge for character literal / attribute problem
                        if was_id then
                          yyless(1);
                          was_id := false;
                          return (''');
                        else
                          was_id := false;
                          return(CHAR_LITERAL);
                        end if;
                      }

-- Whitespace and Comments
[ \f\t\r] {Bogus;}          --/* ignore spaces, tabs, carriage returns */
--/* Treat comment */
"--".*    {AdaDoc_Tree.Comment.Add_To_Buffer(Get_Token_Value);}
[\n]      {Lines := Lines + 1;}

-- Errors
.                           {return (ERROR);}
-- _{IDENTIFIER}               {Handle_Errors;
--                              Put_Line("Leading underscore in identifier.")
--                              ;
--                             }
-- {IDENTIFIER}__{IDENTIFIER}  {Handle_Errors;
--                              Put_Line("Consecutive underscores in identifier.")
--                              ;
--                             }

%%

-- ***********************************************************
-- *                                                         *
-- * Ada95_Lexical_Analyzer                                  * SPEC
-- *                                                         *
-- ***********************************************************
with AdaDoc_Tree.Comment;
with Ada95_Parser_Tokens; use Ada95_Parser_Tokens;
package Ada95_Lexer is
--| PURPOSE
--| Analyze a stream of characters for tokens, producing a stream of
--| tokens as a result.  The input stream is obtained through Text_IO.
--| The output stream is obtained by the user of this package through
--| successive calls to Get_Token.
--|
--| AUTHOR AND REVISION HISTORY:
--| Version  Date        Author             Notes
--|   1.0    12 Nov 93   Richard Conn       Initial release
--|   1.1     6 Mar 95   Dean Runzel        Change package name and correct
--|                                         identifier errors
--|   1.2     3 Apr 97   Richard Conn       Modify for SCATC Kit
--|   1.3     9 Apr 02   Vincent Decorges   Modify name for AdaDoc
--| NOTES:
--| The sequence of invocation of the methods in this package is:
--|     Reset - initialize analyzer and open input file
--|     Get_Token -- as desired until END_OF_INPUT is returned
--|     Get_Token_Value -- as desired (returns string of token)


  procedure Reset (File_Name : in STRING);
  --| PURPOSE
  --| Zero line number count for current file and update total line
  --| count for all files.  Reset key variables for current file.
  --| Open input and output files.

  procedure Close_Files;
  --| PURPOSE
  --| Close the input and output files.

  function YYLex return TOKEN;
  --| PURPOSE
  --| Return current token

  function Get_Token return TOKEN;
  --| PURPOSE
  --| Return current token same as YYlex

  function Get_Token_Value return STRING;
  --| PURPOSE
  --| Return value associated with current token (e.g., if current
  --| token is an identifier, this is the name of the identifier);
  --| "Current token" is the last token returned by a call to
  --| Get_Token

  function Get_Current_Line return NATURAL;
  --| PURPOSE
  --| Return line on which current token is located

  procedure Get_Statistics (Number_of_Lines : out NATURAL);
  --| PURPOSE
  --| Return statistics on the last file processed.  These
  --| counters are reset by a call to Reset.  This
  --| routine should be called after the parse is complete.

end Ada95_Lexer;

-- ***********************************************************
-- *                                                         *
-- * Ada95_Lexical_Analyzer                                  * BODY
-- *                                                         *
-- ***********************************************************
package body Ada95_Lexer is

------------------------------------------------------------------
-- Local types and objects
------------------------------------------------------------------

  Lines            : NATURAL := 1;
  Was_ID           : BOOLEAN := FALSE;

------------------------------------------------------------------
-- Local routines
------------------------------------------------------------------

  -- ***********************************************************
  -- *                                                         *
  -- * Ada95_Lexical_Analyzer.Bogus                            *
  -- *                                                         *
  -- ***********************************************************
  procedure Bogus is
    -- dummy routine
  begin
    null;
  end Bogus;

------------------------------------------------------------------
-- Routines exported by the specification
------------------------------------------------------------------
  -- ***********************************************************
  -- *                                                         *
  -- * Ada95_Lexical_Analyzer.Reset                            *
  -- *                                                         *
  -- ***********************************************************
  procedure Reset (File_Name : in STRING) is
  --| PURPOSE
  --| Zero line number count and reset key variables for current
  --| file.  Open input and output files.
  begin -- Reset
    Lines           := 1;
    Was_ID          := FALSE;
    Ada95_Lexer_IO.Open_Input (File_Name);
    Ada95_Lexer_IO.Create_Output;
  end Reset;

  -- ***********************************************************
  -- *                                                         *
  -- * Ada95_Lexical_Analyzer.Close_Files                      *
  -- *                                                         *
  -- ***********************************************************
  procedure Close_Files is
  --| PURPOSE
  --| Close the input and output files.
  begin -- Close_Files
    Ada95_Lexer_IO.Close_Input;
    Ada95_Lexer_IO.Close_Output;
  end Close_Files;

  -- ***********************************************************
  -- *                                                         *
  -- * Ada95_Lexical_Analyzer.Get_Token                        *
  -- *                                                         *
  -- ***********************************************************
  function Get_Token return TOKEN is
  --| PURPOSE
  --| Return current token (same as YYLex)
  begin -- Get_Token
    return YYLex;
  end Get_Token;

  --| NOTE:
  --| This function is acronymic and does not reflect a good design
  --| choice, but it is retained for compatibility with the YYParse
  --| function generated by AYACC, where YYParse calls YYLex.
  --|
  --| Function YYLex is inserted here by AFLEX.
  --| Return current token (inserted below).

##

  -- ***********************************************************
  -- *                                                         *
  -- * Ada95_Lexical_Analyzer.Get_Token_Value                  *
  -- *                                                         *
  -- ***********************************************************
  function Get_Token_Value return STRING is
  --| PURPOSE
  --| Return value associated with current token (e.g., if current
  --| token is an identifier, this is the name of the identifier);
  --| "Current token" is the last token returned by a call to
  --| Get_Token
  begin -- Get_Token_Value
    return Ada95_Lexer_DFA.YYText;
  end Get_Token_Value;

  -- ***********************************************************
  -- *                                                         *
  -- * Ada95_Lexical_Analyzer.Get_Current_Line                 *
  -- *                                                         *
  -- ***********************************************************
  function Get_Current_Line return NATURAL is
  --| PURPOSE
  --| Return line on which current token is located
  begin -- Get_Current_Line
    return Lines;
  end Get_Current_Line;

  -- ***********************************************************
  -- *                                                         *
  -- * Ada95_Lexical_Analyzer.Get_Statistics                   *
  -- *                                                         *
  -- ***********************************************************
  procedure Get_Statistics (Number_of_Lines : out NATURAL) is
  --| PURPOSE
  --| Return statistics on the last file processed.  These
  --| counters are reset by a call to Reset.  This
  --| routine should be called after the parse is complete.
  begin -- Get_Statistics
    Number_of_Lines := Lines - 1;
  end Get_Statistics;

end Ada95_Lexer;



