home *** CD-ROM | disk | FTP | other *** search
/ Game Audio Programming / GameAudioProgramming.iso / Game_Audio / audio_sdk / src / AudioScript / Lexer.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-28  |  7.1 KB  |  198 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. #ifndef __LEXER_H
  13. #define __LEXER_H
  14.  
  15. #include "Token.h"
  16.  
  17.  
  18. namespace Audio
  19. {
  20.  
  21. /*! \file Lexer.h*/ 
  22.  
  23.  
  24. #pragma warning(disable:4786)
  25. #pragma warning(disable:4251)
  26.  
  27. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  28. typedef std::stack<std::string> StringStack;
  29.  
  30.  
  31. class Macro
  32. {
  33. public:
  34.     Macro() {  Clear();  }
  35.     virtual ~Macro() { }
  36.  
  37.     void Clear();
  38.  
  39.     std::string m_sName;
  40.     StringVector m_ArgumentVector;
  41.     TokenVector m_SubstitutionVector;
  42.     TokenList m_MacroList;
  43.     int m_iTotalArguments;
  44.     int m_iCurrentArgument;
  45.     
  46.     bool operator < (const Macro& m) const
  47.     {  return m_sName < m.m_sName;  }
  48.     bool operator < (const char* psz) const
  49.     {  return m_sName < psz;  }
  50.  
  51.     bool empty()
  52.     {  return m_sName.length() ? false : true;  }
  53. };
  54.  
  55. typedef std::set<Macro> MacroSet;
  56. typedef MacroSet::iterator MacroSetItor;
  57.  
  58.  
  59. #endif // DOXYGEN_SHOULD_SKIP_THIS
  60.  
  61. //! Used for tokenizing text files
  62. /*! Lexer is used to tokenize any sort of text file or script
  63. ranging from simple ini files to full-blown C++.  It provides the ability 
  64. to define custom keywords and operators as well as more advanced features
  65. such as automatic include file processing, macro expansion, and GUID
  66. recognition and parsing.  Basic types recognized are integers, floats, bools
  67. strings, keywords, operators, variables, and guids.
  68. */
  69. class Lexer
  70. {
  71. public:
  72.     //! Constructor
  73.     Lexer()
  74.     {      
  75.         Clear();  
  76.     }
  77.     //! Destructor
  78.     virtual ~Lexer()
  79.     {  Term();  }
  80.  
  81.     //! Clears all internal parameters
  82.     void Clear();
  83.     //! Creates the lexer object
  84.     /*!
  85.     This function initializes a number of common operators and keywords required
  86.     by the Lexer class.  It optionally allows you to automatically reserve the
  87.     most common operators used by scripts, ini files, or languages such as
  88.     C++.  
  89.     \param bReserveCommonOperators Allows the user to reserve common C++ style 
  90.     operators
  91.     \return true indicates success, false indicates failure
  92.     */
  93.     bool Init(bool bReserveCommonOperators = true);
  94.     //! Destroys the parser object and clears all stored data
  95.     void Term();
  96.  
  97.     //! Reserve an operator
  98.     /*!
  99.     Note that one restriction exists for operators.  Any operator longer than
  100.     a single character must be created of smaller operators.  This is done
  101.     for efficiency reasons, as operators take the highest precedence in the
  102.     parsing process and must be parsed first.
  103.     \param sOperator String representing the operator to reserve
  104.     */
  105.     void ReserveOperator(std::string sOperator);
  106.     //! Reserve a keyword
  107.     /*!
  108.     Tell the parser to reserve a particular string as a keyword instead of
  109.     making it a generic variable.  This can aid in lexical analysis of the token
  110.     stream.
  111.     \param sKeyword String representing the keyword to reserve
  112.     */
  113.     void ReserveKeyword(std::string sKeyword);
  114.     //! Places an end-of-line token after each parsed line.  This is off by default
  115.     void MarkEndOfLine(bool bEOL)                    {  m_bMarkEndOfLine = bEOL;  }
  116.     //! Enables rejection of C and C++ style comments.  This is on by default
  117.     void EnableComments(bool bComments)                {  m_bEnableComments = bComments;  }
  118.     //! Enables automatic processing of C-style headers.  This is off by default
  119.     void ProcessHeaders(bool bHeaders)                {  m_bProcessHeaders = bHeaders;  }
  120.     //! Enables automatic processing of C-style macros.  This is off by default
  121.     void ProcessMacros(bool bMacros)                {  m_bProcessMacros = bMacros;  }
  122.     //! Tokenize a text file
  123.     /*!
  124.     This function performs a complete tokenization of the file, placing all
  125.     tokens into the tokenlist passed to it.
  126.     \param sFilename Name of the file to process.
  127.     \param tokenlist TokenList to insert processed tokens into.
  128.     */
  129.     bool Process(std::string sFilename, TokenList& tokenlist);
  130.     //! Tokenize a text file
  131.     /*!
  132.     This function performs a complete tokenization of the file, placing all
  133.     tokens into the tokenlist passed to it.
  134.     \param pData Memory location of the data to process.
  135.     \param nDataSize Size of the data in bytes.
  136.     \param tokenlist TokenList to insert processed tokens into.
  137.     */
  138.     bool Process(uint8* pData, uint32 nDataSize, TokenList& tokenlist);
  139.  
  140.  
  141. private:
  142.     void ReserveCommonOperators();
  143.     bool ProcessHeaders(TokenList& tokenlist);
  144.     bool ProcessMacros(TokenList& tokenlist);
  145.     bool ParseLine(uint8* pLineBuffer, uint32 nLineLength);
  146.     bool MakeToken(const std::string& sToken, Token::TOKEN_TYPE Type = Token::UNKNOWN_TOKEN);
  147.     bool ExpandMacro(TokenList& tokenlist, TokenListItor& itor, MacroSetItor itr);
  148.     bool ParseVector(TokenList& tokenlist, TokenListItor& itor);
  149.     bool ParseGUID(TokenList& tokenlist, TokenListItor& itor);
  150.     void ClearMacros();
  151.     int GetTotalLinesParsed() const                    {  return m_iTotalLinesParsed;  }
  152.     int GetCurrentLineNumber() const                {  return m_iCurrentLineNumber;  }
  153.     inline bool IsDigit(char c)    const                {  return ((c >= '0') && (c <= '9')) ? true : false;  }
  154.     inline bool IsHexDigit(char c) const            {  return (IsDigit(c) || ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F'))) ? true : false;  }
  155.     inline bool IsWhitespace(char c) const            {  return ((c == char(32)) || (c == char(9)) || (c == char(0x0D)) || (c == char(0x0A))) ? true : false;  }
  156.     bool IsReal(const std::string& sToken) const;
  157.     inline double GetReal(const std::string& sToken){  return atof(sToken.c_str());  }
  158.     bool IsInteger(const std::string& sToken) const;
  159.     inline int GetInteger(const std::string& sToken){  return atoi(sToken.c_str());  }
  160.     bool IsHex(const std::string& sToken) const;
  161.     DWORD GetHex(const std::string& sToken);
  162.     bool IsBoolean(const std::string& sToken) const;
  163.     bool GetBoolean(const std::string& sToken);
  164.     bool IsKeyword(const std::string& sToken) const;
  165.     bool IsOperator(const std::string& sToken) const;
  166.  
  167.     // indicates whether the end of line character should be 
  168.     // included as a token
  169.     bool m_bMarkEndOfLine;
  170.     // Are C and C++ style comments supported (supported means they will be
  171.     // excluded from the token list)?
  172.     bool m_bEnableComments;
  173.     // Should we process header files?
  174.     bool m_bProcessHeaders;
  175.     // Should we process macros?
  176.     bool m_bProcessMacros;
  177.     // Is a C style comment in effect?
  178.     bool m_bCStyleCommentActive;
  179.     // The current file being parsed
  180.     StringStack m_CurrentFileStack;
  181.     // The current line number of the file being parsed
  182.     int m_iCurrentLineNumber;
  183.     // Total number of lines parsed by the parser
  184.     int m_iTotalLinesParsed;
  185.     // contains a vector of operators defined as strings
  186.     StringVector m_OperatorVector;
  187.     // contains a vector of keywords defined as strings
  188.     StringVector m_KeywordVector;
  189.     // pointer to list of tokens as defined by the current list of keywords and operators
  190.     TokenList* m_pTokenList;
  191.     // We store macros here sorted by their string definition.  
  192.     MacroSet m_MacroSet;
  193.  
  194. };
  195.  
  196. }; // namespace Audio
  197.  
  198. #endif // __PARSER_H