consoleParser.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _CONSOLE_PARSER_H_
  23. #define _CONSOLE_PARSER_H_
  24. #include <stdio.h>
  25. /// @ingroup console_system Console System
  26. /// @{
  27. namespace Compiler
  28. {
  29. //-----------------------------------------------------------------------------
  30. /// \brief Function for GetCurrentFile from the lexer
  31. //-----------------------------------------------------------------------------
  32. typedef const char *(*fnGetCurrentFile)();
  33. //-----------------------------------------------------------------------------
  34. /// \brief Function for GetCurrentLine from the lexer
  35. //-----------------------------------------------------------------------------
  36. typedef S32 (*fnGetCurrentLine)();
  37. //-----------------------------------------------------------------------------
  38. /// \brief Function for Parse from the lexer
  39. //-----------------------------------------------------------------------------
  40. typedef S32 (*fnParse)();
  41. //-----------------------------------------------------------------------------
  42. /// \brief Function for Restart from the lexer
  43. //-----------------------------------------------------------------------------
  44. typedef void (*fnRestart)(FILE *input_file);
  45. //-----------------------------------------------------------------------------
  46. /// \brief Function for SetScanBuffer from the lexer
  47. //-----------------------------------------------------------------------------
  48. typedef void (*fnSetScanBuffer)(const char *sb, const char *fn);
  49. //-----------------------------------------------------------------------------
  50. /// \brief List of parsers for the compiler
  51. //-----------------------------------------------------------------------------
  52. struct ConsoleParser
  53. {
  54. struct ConsoleParser *next; //!< Next object in list or NULL
  55. char *ext; //!< Filename extension handled by this parser
  56. fnGetCurrentFile getCurrentFile; //!< GetCurrentFile lexer function
  57. fnGetCurrentLine getCurrentLine; //!< GetCurrentLine lexer function
  58. fnParse parse; //!< Parse lexer function
  59. fnRestart restart; //!< Restart lexer function
  60. fnSetScanBuffer setScanBuffer; //!< SetScanBuffer lexer function
  61. };
  62. // Macros
  63. //-----------------------------------------------------------------------------
  64. /// \brief Declare a parser's function prototypes
  65. //-----------------------------------------------------------------------------
  66. #define CON_DECLARE_PARSER(prefix) \
  67. const char * prefix##GetCurrentFile(); \
  68. S32 prefix##GetCurrentLine(); \
  69. void prefix##SetScanBuffer(const char *sb, const char *fn); \
  70. S32 prefix##parse(); \
  71. void prefix##restart(FILE *input_file)
  72. //-----------------------------------------------------------------------------
  73. /// \brief Helper macro to add console parsers
  74. //-----------------------------------------------------------------------------
  75. #define CON_ADD_PARSER(prefix, ext, def) \
  76. Compiler::addConsoleParser(ext, prefix##GetCurrentFile, prefix##GetCurrentLine, prefix##parse, \
  77. prefix##restart, prefix##SetScanBuffer, def)
  78. //-----------------------------------------------------------------------------
  79. /// \brief Free the console parser list
  80. ///
  81. /// \sa AddConsoleParser()
  82. //-----------------------------------------------------------------------------
  83. void freeConsoleParserList(void);
  84. //-----------------------------------------------------------------------------
  85. /// \brief Add a console parser to the list
  86. ///
  87. /// \param ext Filename extension
  88. /// \param gcf GetCurrentFile function
  89. /// \param gcl GetCurrentLine function
  90. /// \param p Parse function
  91. /// \param r Restart function
  92. /// \param ssb SetScanBuffer function
  93. /// \param def true if this is the default parser (<b>Note:</b> set this only on the .tscript parser!)
  94. /// \return true for success, false for failure (out of memory)
  95. /// \sa FreeConsoleParserList(), ConsoleParser
  96. //-----------------------------------------------------------------------------
  97. bool addConsoleParser(char *ext, fnGetCurrentFile gcf, fnGetCurrentLine gcl, fnParse p, fnRestart r, fnSetScanBuffer ssb, bool def = false);
  98. //-----------------------------------------------------------------------------
  99. /// \brief Get the parser for a particular file based on its extension
  100. ///
  101. /// \param filename Filename of file to obtain parser for
  102. /// \sa ConsoleParser
  103. //-----------------------------------------------------------------------------
  104. ConsoleParser * getParserForFile(const char *filename);
  105. } // end namespace Con
  106. /// @}
  107. #endif // _CONSOLE_PARSER_H_