home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / Parser.h < prev    next >
C/C++ Source or Header  |  2005-11-14  |  9KB  |  255 lines

  1.  
  2. #ifndef __PARSER_H__
  3. #define __PARSER_H__
  4.  
  5. /*
  6. ===============================================================================
  7.  
  8.     C/C++ compatible pre-compiler
  9.  
  10. ===============================================================================
  11. */
  12.  
  13. #define DEFINE_FIXED            0x0001
  14.  
  15. #define BUILTIN_LINE            1
  16. #define BUILTIN_FILE            2
  17. #define BUILTIN_DATE            3
  18. #define BUILTIN_TIME            4
  19. #define BUILTIN_STDC            5
  20.  
  21. #define INDENT_IF                0x0001
  22. #define INDENT_ELSE                0x0002
  23. #define INDENT_ELIF                0x0004
  24. #define INDENT_IFDEF            0x0008
  25. #define INDENT_IFNDEF            0x0010
  26.  
  27. // macro definitions
  28. typedef struct define_s {
  29.     char *            name;                        // define name
  30.     int                flags;                        // define flags
  31.     int                builtin;                    // > 0 if builtin define
  32.     int                numparms;                    // number of define parameters
  33.     idToken *        parms;                        // define parameters
  34.     idToken *        tokens;                        // macro tokens (possibly containing parm tokens)
  35.     struct define_s    *next;                        // next defined macro in a list
  36.     struct define_s    *hashnext;                    // next define in the hash chain
  37. } define_t;
  38.  
  39. // indents used for conditional compilation directives:
  40. // #if, #else, #elif, #ifdef, #ifndef
  41. typedef struct indent_s {
  42.     int                type;                        // indent type
  43.     int                skip;                        // true if skipping current indent
  44.     idLexer *        script;                        // script the indent was in
  45.     struct indent_s    *next;                        // next indent on the indent stack
  46. } indent_t;
  47.  
  48.  
  49. class idParser {
  50.  
  51. public:
  52.                     // constructor
  53.                     idParser();
  54.                     idParser( int flags );
  55.                     idParser( const char *filename, int flags = 0, bool OSPath = false );
  56.                     idParser( const char *ptr, int length, const char *name, int flags = 0 );
  57.                     // destructor
  58.                     ~idParser();
  59.                     // load a source file
  60.     int                LoadFile( const char *filename, bool OSPath = false );
  61.                     // load a source from memory
  62.     int                LoadMemory( const char *ptr, int length, const char *name );
  63.                     // free the current source
  64.     void            FreeSource( bool keepDefines = false );
  65.                     // returns true if a source is loaded
  66.     int                IsLoaded( void ) const { return idParser::loaded; };
  67.                     // read a token from the source
  68.     int                ReadToken( idToken *token );
  69.                     // expect a certain token, reads the token when available
  70.     int                ExpectTokenString( const char *string );
  71.                     // expect a certain token type
  72.     int                ExpectTokenType( int type, int subtype, idToken *token );
  73.                     // expect a token
  74.     int                ExpectAnyToken( idToken *token );
  75.                     // returns true and reads the token when it is available
  76.     int                CheckTokenString( const char *string );
  77.                     // returns true and reads the token when a token with the given type is available
  78.     int                CheckTokenType( int type, int subtype, idToken *token );
  79.                     // skip tokens until the given token string is read
  80.     int                SkipUntilString( const char *string );
  81.                     // skip the rest of the current line
  82.     int                SkipRestOfLine( void );
  83.                     // skip the braced section
  84.     int                SkipBracedSection( bool parseFirstBrace = true );
  85.                     // parse a braced section into a string
  86.     const char *    ParseBracedSection( idStr &out, int tabs = -1 );
  87.                     // parse a braced section into a string, maintaining indents and newlines
  88.     const char *    ParseBracedSectionExact( idStr &out, int tabs = -1 );
  89.                     // parse the rest of the line
  90.     const char *    ParseRestOfLine( idStr &out );
  91.                     // unread the given token
  92.     void            UnreadToken( idToken *token );
  93.                     // read a token only if on the current line
  94.     int                ReadTokenOnLine( idToken *token );
  95.                     // read a signed integer
  96.     int                ParseInt( void );
  97.                     // read a boolean
  98.     bool            ParseBool( void );
  99.                     // read a floating point number
  100.     float            ParseFloat( void );
  101.                     // parse matrices with floats
  102.     int                Parse1DMatrix( int x, float *m );
  103.     int                Parse2DMatrix( int y, int x, float *m );
  104.     int                Parse3DMatrix( int z, int y, int x, float *m );
  105.                     // get the white space before the last read token
  106.     int                GetLastWhiteSpace( idStr &whiteSpace ) const;
  107.                     // Set a marker in the source file (there is only one marker)
  108.     void            SetMarker( void );
  109.                     // Get the string from the marker to the current position
  110.     void            GetStringFromMarker( idStr& out, bool clean = false );
  111.                     // add a define to the source
  112.     int                AddDefine( const char *string );
  113.                     // add builtin defines
  114.     void            AddBuiltinDefines( void );
  115.                     // set the source include path
  116.     void            SetIncludePath( const char *path );
  117.                     // set the punctuation set
  118.     void            SetPunctuations( const punctuation_t *p );
  119.                     // returns a pointer to the punctuation with the given id
  120.     const char *    GetPunctuationFromId( int id );
  121.                     // get the id for the given punctuation
  122.     int                GetPunctuationId( const char *p );
  123.                     // set lexer flags
  124.     void            SetFlags( int flags );
  125.                     // get lexer flags
  126.     int                GetFlags( void ) const;
  127.                     // returns the current filename
  128.     const char *    GetFileName( void ) const;
  129.                     // get current offset in current script
  130.     const int        GetFileOffset( void ) const;
  131.                     // get file time for current script
  132.     const unsigned int GetFileTime( void ) const;
  133.                     // returns the current line number
  134.     const int        GetLineNum( void ) const;
  135.                     // print an error message
  136.     void            Error( const char *str, ... ) const id_attribute((format(printf,2,3)));
  137.                     // print a warning message
  138.     void            Warning( const char *str, ... ) const id_attribute((format(printf,2,3)));
  139.  
  140.                     // add a global define that will be added to all opened sources
  141.     static int        AddGlobalDefine( const char *string );
  142.                     // remove the given global define
  143.     static int        RemoveGlobalDefine( const char *name );
  144.                     // remove all global defines
  145.     static void        RemoveAllGlobalDefines( void );
  146.                     // set the base folder to load files from
  147.     static void        SetBaseFolder( const char *path );
  148.  
  149. private:
  150.  
  151.     int                loaded;                        // set when a source file is loaded from file or memory
  152.     idStr            filename;                    // file name of the script
  153.     idStr            includepath;                // path to include files
  154.     bool            OSPath;                        // true if the file was loaded from an OS path
  155.     const punctuation_t *punctuations;            // punctuations to use
  156.     int                flags;                        // flags used for script parsing
  157.     idLexer *        scriptstack;                // stack with scripts of the source
  158.     idToken *        tokens;                        // tokens to read first
  159.     define_t *        defines;                    // list with macro definitions
  160.     define_t **        definehash;                    // hash chain with defines
  161.     indent_t *        indentstack;                // stack with indents
  162.     int                skip;                        // > 0 if skipping conditional code
  163.     idToken            token;                        // last read token
  164.     const char*        marker_p;
  165.  
  166.     static define_t *globaldefines;                // list with global defines added to every source loaded
  167.  
  168. private:
  169.     void            PushIndent( int type, int skip );
  170.     void            PopIndent( int *type, int *skip );
  171.     void            PushScript( idLexer *script );
  172.     int                ReadSourceToken( idToken *token );
  173.     int                ReadLine( idToken *token );
  174.     int                UnreadSourceToken( idToken *token );
  175.     int                ReadDefineParms( define_t *define, idToken **parms, int maxparms );
  176.     int                StringizeTokens( idToken *tokens, idToken *token );
  177.     int                MergeTokens( idToken *t1, idToken *t2 );
  178.     int                ExpandBuiltinDefine( idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken );
  179.     int                ExpandDefine( idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken );
  180.     int                ExpandDefineIntoSource( idToken *deftoken, define_t *define );
  181.     void            AddGlobalDefinesToSource( void );
  182.     define_t *        CopyDefine( define_t *define );
  183.     define_t *        FindHashedDefine(define_t **definehash, const char *name);
  184.     int                FindDefineParm( define_t *define, const char *name );
  185.     void            AddDefineToHash(define_t *define, define_t **definehash);
  186.     static void        PrintDefine( define_t *define );
  187.     static void        FreeDefine( define_t *define );
  188.     static define_t *FindDefine( define_t *defines, const char *name );
  189.     static define_t *DefineFromString( const char *string);
  190.     define_t *        CopyFirstDefine( void );
  191.     int                Directive_include( void );
  192.     int                Directive_undef( void );
  193.     int                Directive_if_def( int type );
  194.     int                Directive_ifdef( void );
  195.     int                Directive_ifndef( void );
  196.     int                Directive_else( void );
  197.     int                Directive_endif( void );
  198.     int                EvaluateTokens( idToken *tokens, signed long int *intvalue, double *floatvalue, int integer );
  199.     int                Evaluate( signed long int *intvalue, double *floatvalue, int integer );
  200.     int                DollarEvaluate( signed long int *intvalue, double *floatvalue, int integer);
  201.     int                Directive_define( void );
  202.     int                Directive_elif( void );
  203.     int                Directive_if( void );
  204.     int                Directive_line( void );
  205.     int                Directive_error( void );
  206.     int                Directive_warning( void );
  207.     int                Directive_pragma( void );
  208.     void            UnreadSignToken( void );
  209.     int                Directive_eval( void );
  210.     int                Directive_evalfloat( void );
  211.     int                ReadDirective( void );
  212.     int                DollarDirective_evalint( void );
  213.     int                DollarDirective_evalfloat( void );
  214.     int                ReadDollarDirective( void );
  215. };
  216.  
  217. ID_INLINE const char *idParser::GetFileName( void ) const {
  218.     if ( idParser::scriptstack ) {
  219.         return idParser::scriptstack->GetFileName();
  220.     }
  221.     else {
  222.         return "";
  223.     }
  224. }
  225.  
  226. ID_INLINE const int idParser::GetFileOffset( void ) const {
  227.     if ( idParser::scriptstack ) {
  228.         return idParser::scriptstack->GetFileOffset();
  229.     }
  230.     else {
  231.         return 0;
  232.     }
  233. }
  234.  
  235. ID_INLINE const unsigned int idParser::GetFileTime( void ) const {
  236.     if ( idParser::scriptstack ) {
  237.         return idParser::scriptstack->GetFileTime();
  238.     }
  239.     else {
  240.         return 0;
  241.     }
  242. }
  243.  
  244. ID_INLINE const int idParser::GetLineNum( void ) const {
  245.     if ( idParser::scriptstack ) {
  246.         return idParser::scriptstack->GetLineNum();
  247.     }
  248.     else {
  249.         return 0;
  250.     }
  251. }
  252.  
  253. #endif /* !__PARSER_H__ */
  254.  
  255.