/***************************************************************************
 *  aGrUM modified frames and atg files for cocoR
 *   Copyright (c) 2005-2024 by Pierre-Henri WUILLEMIN(@LIP6) and Christophe GONZALES(@AMU)
 *   info_at_agrum_dot_org
***************************************************************************/
/*----------------------------------------------------------------------
Compiler Generator Coco/R,
Copyright (c) 1990, 2004 Hanspeter Moessenboeck, University of Linz
extended by M. Loeberbauer & A. Woess, Univ. of Linz
ported to C++ by Csaba Balazs, University of Szeged
with improvements by Pat Terry, Rhodes University

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

This program 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 along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

As an exception, it is allowed to write an extension of Coco/R that is
used as a plugin in non-free software.

If not otherwise stated, any source code generated by Coco/R (other than
Coco/R itself) does not fall under the GNU General Public License.
-----------------------------------------------------------------------*/


#if !defined(gum_MDPDAT_COCO_SCANNER_H__)
#define gum_MDPDAT_COCO_SCANNER_H__

//#include <agrum/base/core/cocoR/common.h>
#include <agrum/base/core/signal/signaler.h>
#include <agrum/base/core/errorsContainer.h>

namespace gum {
namespace MDPDAT {


// Token, Buffer, InMemoryBuffer, StreamBuffer, MappedBuffer, UTF8Buffer,
// StartStates, KeywordMap are now defined in <agrum/base/core/cocoR/common.h>

class Scanner {
  private:
    void* firstHeap;
    void* heap;
    void* heapTop;
    void** heapEnd;

    std::wstring filenamne__;
    bool trace__;

    unsigned char EOL;
    int eofSym;
    int noSym;
    int maxT;
    // int charSetSize; // not used
    StartStates start;
    KeywordMap keywords;
    int percent;

    Token* t;         // current token
    wchar_t* tval;    // text of current token
    int tvalLength;   // length of text of current token
    int tlen;         // length of current token

    Token* tokens;    // list of tokens already peeked (first token is a dummy)
    Token* pt;        // current peek token

    int ch;           // current input character
    
    int pos;          // byte position of current character
    int charPos;      // position by unicode characters starting with 0
    int line;         // line number of current character
    int col;          // column number of current character
    int oldEols;      // EOLs that appeared in a comment;

    void CreateHeapBlock();
    Token* CreateToken();
    void AppendVal( Token* t );
    void SetScannerBehindT();

    void Init();
    void NextCh();
    void AddCh();
    	bool Comment0();
	bool Comment1();

    Token* NextToken();

  public:
    Signaler<int> onLoad;

    Buffer* buffer;   // scanner buffer

    Scanner( const unsigned char* buf, int len, std::string filename="anonymous buffer", bool trace=false );
    Scanner( const char* fileName,bool trace=false );
    Scanner( const wchar_t* fileName,bool trace=false );
    Scanner( FILE* s,bool trace=false );
    ~Scanner();

    void setTrace( bool b ) { trace__=b;}

    void Load( const wchar_t* fileName );
    Token* Scan();
    Token* Peek();
    void ResetPeek();

    const std::wstring& filename() const {return filenamne__;}
    Buffer* getBuffer() {return buffer;}

}; // end Scanner

} // namespace
} // namespace


#endif // !defined(COCO_SCANNER_H__)

