home *** CD-ROM | disk | FTP | other *** search
/ Game Audio Programming / GameAudioProgramming.iso / Game_Audio / audio_sdk / src / AudioScript / Token.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-27  |  8.6 KB  |  319 lines

  1. /***********************************************************\
  2. Copyright (C) James Boer, 2002. 
  3. All rights reserved worldwide.
  4.  
  5. This software is provided "as is" without express or implied
  6. warranties. You may freely copy and compile this source into
  7. applications you distribute provided that the copyright text
  8. below is included in the resulting source code, for example:
  9. "Portions Copyright (C) James Boer, 2002"
  10. \***********************************************************/
  11. #pragma once
  12.  
  13. #ifndef __TOKEN_H
  14. #define __TOKEN_H
  15.  
  16. #pragma warning(disable:4786)
  17. #pragma warning(disable:4251)
  18.  
  19. #define TOKEN_DEBUGGING_INFO
  20.  
  21. namespace Audio
  22. {
  23.  
  24. struct TokenVector3D
  25. {
  26.     float x;
  27.     float y;
  28.     float z;
  29. };
  30.  
  31. // This is the basic data object generated by the Parser class
  32. class Token 
  33. {
  34. public:
  35.     enum TOKEN_TYPE
  36.     {
  37.         UNKNOWN_TOKEN = 0,
  38.         KEYWORD,
  39.         OPERATOR,
  40.         VARIABLE,
  41.         STRING,
  42.         INTEGER,
  43.         REAL,
  44.         BOOLEAN,
  45.         VECTOR,
  46.         T_GUID
  47.     };
  48.  
  49.  
  50.     Token()                        {  Clear();  }
  51.     Token(const Token& tok);
  52.     virtual ~Token()            {  Term();  }
  53.     void Clear();
  54.  
  55.     void operator = ( const Token& tok);
  56.  
  57.     inline bool InitKeyword(const char* pszKeyword)
  58.     {
  59.         m_Type = KEYWORD;
  60.         m_pszKeyword = new char[strlen(pszKeyword) + 1];
  61.         if(!m_pszKeyword)
  62.             return false;
  63.         strcpy(m_pszKeyword, pszKeyword);
  64.         return true;
  65.     }
  66.  
  67.     inline bool InitOperator(const char* pszOperator)
  68.     {
  69.         m_Type = OPERATOR;
  70.         m_pszOperator = new char[strlen(pszOperator) + 1];
  71.         if(!m_pszOperator)
  72.             return false;
  73.         strcpy(m_pszOperator, pszOperator);
  74.         return true;
  75.     }
  76.  
  77.     inline bool InitVariable(const char* pszVariable)
  78.     {
  79.         m_Type = VARIABLE;
  80.         m_pszVariable = new char[strlen(pszVariable) + 1];
  81.         if(!m_pszVariable)
  82.             return false;
  83.         strcpy(m_pszVariable, pszVariable);
  84.         return true;
  85.     }
  86.  
  87.     inline bool InitString(const char* pszString)
  88.     {
  89.         m_Type = STRING;
  90.         m_pszString = new char[strlen(pszString) + 1];
  91.         if(!m_pszString)
  92.             return false;
  93.         strcpy(m_pszString, pszString);
  94.         return true;
  95.     }
  96.  
  97.     inline bool InitInteger(int iInteger)
  98.     {
  99.         m_Type = INTEGER;
  100.         m_iInteger = iInteger;
  101.         return true;
  102.     }
  103.  
  104.     inline bool InitReal(double dReal)
  105.     {
  106.         m_Type = REAL;
  107.         m_dReal = dReal;
  108.         return true;
  109.     }
  110.  
  111.     inline bool InitBoolean(bool bBoolean)
  112.     {
  113.         m_Type = BOOLEAN;
  114.         m_bBoolean = bBoolean;
  115.         return true;
  116.     }
  117.  
  118.     inline bool InitVector(const TokenVector3D& vec3D)
  119.     {
  120.         m_Type = VECTOR;
  121.         m_pvVector = new TokenVector3D;
  122.         if(!m_pvVector)
  123.             return false;
  124.         memcpy(m_pvVector, &vec3D, sizeof(TokenVector3D));
  125.         return true;
  126.     }
  127.  
  128.     inline bool InitGuid(const GUID& guidGuid)
  129.     {
  130.         m_Type = T_GUID;
  131.         m_pguidGuid = new GUID;
  132.         if(!m_pguidGuid)
  133.             return false;
  134.         memcpy(m_pguidGuid, &guidGuid, sizeof(GUID));
  135.         return true;
  136.     }
  137.  
  138.     void Term();
  139.  
  140.     inline void SetVariable(const char* pszVariable)
  141.     {
  142.         assert(m_Type == VARIABLE);
  143.         delete[] m_pszVariable;
  144.         m_pszVariable = 0;
  145.         InitVariable(pszVariable);
  146.     }
  147.  
  148.     inline void SetKeyword(const char* pszKeyword)
  149.     {
  150.         assert(m_Type == KEYWORD);
  151.         delete[] m_pszKeyword;
  152.         m_pszKeyword = 0;
  153.         InitKeyword(pszKeyword);
  154.     }
  155.  
  156.     inline void SetString(const char* pszString)
  157.     {
  158.         assert(m_Type == STRING);
  159.         delete[] m_pszString;
  160.         m_pszString = 0;
  161.         InitString(pszString);
  162.     }
  163.  
  164.     inline void SetOperator(const char* pszOperator)
  165.     {
  166.         assert(m_Type == OPERATOR);
  167.         delete[] m_pszOperator;
  168.         m_pszOperator = 0;
  169.         InitOperator(pszOperator);
  170.     }
  171.  
  172.     inline void SetInteger(const int iInteger)
  173.     {
  174.         assert(m_Type == INTEGER);
  175.         InitInteger(iInteger);
  176.     }
  177.  
  178.     inline void SetBoolean(const bool bBoolean)
  179.     {
  180.         assert(m_Type == BOOLEAN);
  181.         InitBoolean(bBoolean);
  182.     }
  183.  
  184.     inline void SetReal(const double dReal)
  185.     {
  186.         assert(m_Type == REAL);
  187.         InitReal(dReal);
  188.     }
  189.  
  190.     inline void SetVector(const TokenVector3D& vVector)
  191.     {
  192.         assert(m_Type == VECTOR);
  193.         delete m_pvVector;
  194.         m_pvVector = 0;
  195.         InitVector(vVector);
  196.     }
  197.  
  198.     inline void SetGuid(const GUID& guidGuid)
  199.     {
  200.         assert(m_Type == T_GUID);
  201.         delete m_pguidGuid;
  202.         m_pguidGuid = 0;
  203.         InitGuid(guidGuid);
  204.     }
  205.  
  206.     // querry the type of token
  207.     inline bool IsUnknown()    const        {  return (m_Type == UNKNOWN_TOKEN) ? true : false;  }
  208.     inline bool IsKeyword()    const        {  return (m_Type == KEYWORD) ? true : false;  }
  209.     inline bool IsOperator() const        {  return (m_Type == OPERATOR) ? true : false;  }
  210.     inline bool IsVariable() const        {  return (m_Type == VARIABLE) ? true : false;  }
  211.     inline bool IsString() const        {  return (m_Type == STRING) ? true : false;  }
  212.     inline bool IsInteger()    const        {  return (m_Type == INTEGER) ? true : false;  }
  213.     inline bool IsReal() const            {  return (m_Type == REAL) ? true : false;  }
  214.     inline bool IsBoolean()    const        {  return (m_Type == BOOLEAN) ? true : false;  }
  215.     inline bool IsVector() const        {  return (m_Type == VECTOR) ? true : false;  }
  216.     inline bool IsGuid() const            {  return (m_Type == T_GUID) ? true : false;  }
  217.     inline TOKEN_TYPE GetType() const    {  return m_Type;  }
  218.  
  219.     // get the token content
  220.     inline const char* GetKeyword() const    {  assert(m_Type == KEYWORD);  return (const char*)m_pszKeyword;  }
  221.     inline const char* GetOperator() const    {  assert(m_Type == OPERATOR);  return (const char*)m_pszOperator;  }
  222.     inline const char* GetVariable() const    {  assert(m_Type == VARIABLE);  return (const char*)m_pszVariable;  }
  223.     inline const char* GetString() const    {  assert(m_Type == STRING);  return (const char*)m_pszString;  }
  224.     inline int GetInteger() const            {  assert(m_Type == INTEGER);  return m_iInteger;  }
  225.     inline double GetReal() const            {  assert(m_Type == REAL);  return m_dReal;  }
  226.     inline bool GetBoolean() const            {  assert(m_Type == BOOLEAN);  return m_bBoolean;  }
  227.     inline TokenVector3D& GetVector() const    {  assert(m_Type == VECTOR);  return *m_pvVector;  }
  228.     inline GUID& GetGuid() const            {  assert(m_Type == T_GUID);  return *m_pguidGuid;  }
  229.  
  230.     inline bool IsNewline() const            {  return (IsOperator() && (GetOperator()[0] == '\n')) ? true : false;  }
  231.  
  232.     const char* GetDescriptiveString() const;
  233.     
  234. #ifdef TOKEN_DEBUGGING_INFO
  235.     inline void SetLineNumber(int iLineNo)    {  m_iLineNumber = iLineNo;  }
  236.     inline int GetLineNumber() const        {  return m_iLineNumber;  }
  237.     inline void SetFileIndex(int iIndex)    {  m_iFileIndex = iIndex;  }
  238.     inline int GetFileIndex() const            {  return m_iFileIndex;  }
  239. #endif // TOKEN_DEBUGGING_INFO
  240.  
  241.  
  242. protected:
  243.  
  244.     // This tells us which type of data we're carrying
  245.     TOKEN_TYPE m_Type;
  246.  
  247.     // The data is in one of these fields.  We're only going to be using
  248.     // one type per token, so no reason to waste space.  So, we make
  249.     // the data fields part of a union.
  250.     union
  251.     {
  252.         char*            m_pszKeyword;
  253.         char*            m_pszOperator;
  254.         char*            m_pszVariable;
  255.         char*            m_pszString;
  256.         int                m_iInteger;
  257.         double            m_dReal;
  258.         bool            m_bBoolean;
  259.         TokenVector3D*    m_pvVector;
  260.         GUID*            m_pguidGuid;
  261.     };
  262.  
  263. #ifdef TOKEN_DEBUGGING_INFO
  264.     // Indicates the line number in which the token was found.
  265.     // Note that although this is debugging information, it should
  266.     // not only be contained in debug builds, since we may want to
  267.     // "debug" release mode applications which still will be using
  268.     // this parser.
  269.     int m_iLineNumber;
  270.  
  271.     // References an index into a string table containing the source
  272.     // file in which this token was found.
  273.     int m_iFileIndex;
  274. #endif // TOKEN_DEBUGGING_INFO
  275.  
  276. };
  277.  
  278. // stream operators
  279. // this is to support a tokenized file representation of a parsed file
  280. inline std::ostream& operator<<( std::ostream& os, Token& tok );
  281. inline std::istream& operator>>( std::istream& is, Token& tok );
  282.  
  283.  
  284. typedef std::vector<std::string> StringVector;
  285. typedef std::vector<Token> TokenVector;
  286.  
  287. typedef std::list<Token>::iterator TokenListItor;
  288.  
  289. // TokenList is the standard list type for tokens.  It's a basic STL list
  290. // of type token with some enhancements for storing and retrieving debugging info
  291. class TokenList : public std::list<Token>
  292. {
  293. public:
  294.  
  295.     // Gets a descriptive string of the token including file name and line number
  296.     std::string GetDescriptiveString(TokenListItor itr) const;
  297.     // Gets a string of the file name from where the token was parsed
  298.     //std::string GetFileName(TokenListItor itr) const;
  299.     std::string GetFileName(int index) const;
  300.     // Gets the line number of the token
  301.     int GetLineNumber(TokenListItor itr) const;
  302.     
  303.     // Given a filename, find the index into the filelist vector.  If it doesn't
  304.     // currently exist in the vector, push it onto the end and return the
  305.     // new index value.
  306.     int GetFileIndex(std::string sFileName);
  307.  
  308.     // Get the total number of file references in this token list
  309.     int GetNumFileRefs() const;
  310.  
  311.     void clear();
  312.     
  313. private:
  314.     StringVector m_aFileRef;
  315. };
  316.  
  317. }; // namespace Audio
  318.  
  319. #endif // __TOKEN_H