consoleParser.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. //-----------------------------------------------------------------------------
  23. // TorqueBASIC
  24. // (c) Copyright 2004 Burnt Wasp
  25. //-----------------------------------------------------------------------------
  26. #ifndef _CONSOLE_PARSER_H_
  27. #define _CONSOLE_PARSER_H_
  28. #include <stdio.h>
  29. namespace Compiler
  30. {
  31. //////////////////////////////////////////////////////////////////////////
  32. /// \brief Function for GetCurrentFile from the lexer
  33. //////////////////////////////////////////////////////////////////////////
  34. typedef const char *(*fnGetCurrentFile)();
  35. //////////////////////////////////////////////////////////////////////////
  36. /// \brief Function for GetCurrentLine from the lexer
  37. //////////////////////////////////////////////////////////////////////////
  38. typedef S32 (*fnGetCurrentLine)();
  39. //////////////////////////////////////////////////////////////////////////
  40. /// \brief Function for Parse from the lexer
  41. //////////////////////////////////////////////////////////////////////////
  42. typedef S32 (*fnParse)();
  43. //////////////////////////////////////////////////////////////////////////
  44. /// \brief Function for Restart from the lexer
  45. //////////////////////////////////////////////////////////////////////////
  46. typedef void (*fnRestart)(FILE *input_file);
  47. //////////////////////////////////////////////////////////////////////////
  48. /// \brief Function for SetScanBuffer from the lexer
  49. //////////////////////////////////////////////////////////////////////////
  50. typedef void (*fnSetScanBuffer)(const char *sb, const char *fn);
  51. //////////////////////////////////////////////////////////////////////////
  52. /// \brief List of parsers for the compiler
  53. //////////////////////////////////////////////////////////////////////////
  54. struct ConsoleParser
  55. {
  56. struct ConsoleParser *next; //!< Next object in list or NULL
  57. const char *ext; //!< Filename extension handled by this parser
  58. fnGetCurrentFile getCurrentFile; //!< GetCurrentFile lexer function
  59. fnGetCurrentLine getCurrentLine; //!< GetCurrentLine lexer function
  60. fnParse parse; //!< Parse lexer function
  61. fnRestart restart; //!< Restart lexer function
  62. fnSetScanBuffer setScanBuffer; //!< SetScanBuffer lexer function
  63. };
  64. // Macros
  65. //////////////////////////////////////////////////////////////////////////
  66. /// \brief Declare a parser's function prototypes
  67. //////////////////////////////////////////////////////////////////////////
  68. #define CON_DECLARE_PARSER(prefix) \
  69. const char * prefix##GetCurrentFile(); \
  70. S32 prefix##GetCurrentLine(); \
  71. void prefix##SetScanBuffer(const char *sb, const char *fn); \
  72. S32 prefix##parse(); \
  73. void prefix##restart(FILE *input_file)
  74. //////////////////////////////////////////////////////////////////////////
  75. /// \brief Helper macro to add console parsers
  76. //////////////////////////////////////////////////////////////////////////
  77. #define CON_ADD_PARSER(prefix, ext, def) \
  78. Compiler::addConsoleParser(ext, prefix##GetCurrentFile, prefix##GetCurrentLine, prefix##parse, \
  79. prefix##restart, prefix##SetScanBuffer, def)
  80. //////////////////////////////////////////////////////////////////////////
  81. /// \brief Free the console parser list
  82. ///
  83. /// \sa AddConsoleParser()
  84. //////////////////////////////////////////////////////////////////////////
  85. void freeConsoleParserList(void);
  86. //////////////////////////////////////////////////////////////////////////
  87. /// \brief Add a console parser to the list
  88. ///
  89. /// \param ext Filename extension
  90. /// \param gcf GetCurrentFile function
  91. /// \param gcl GetCurrentLine function
  92. /// \param p Parse function
  93. /// \param r Restart function
  94. /// \param ssb SetScanBuffer function
  95. /// \param def true if this is the default parser (<b>Note:</b> set this only on the .cs parser!)
  96. /// \return true for success, false for failure (out of memory)
  97. /// \sa FreeConsoleParserList(), ConsoleParser
  98. //////////////////////////////////////////////////////////////////////////
  99. bool addConsoleParser(const char *ext, fnGetCurrentFile gcf, fnGetCurrentLine gcl, fnParse p, fnRestart r, fnSetScanBuffer ssb, bool def = false);
  100. //////////////////////////////////////////////////////////////////////////
  101. /// \brief Get the parser for a particular file based on its extension
  102. ///
  103. /// \param filename Filename of file to obtain parser for
  104. /// \sa ConsoleParser
  105. //////////////////////////////////////////////////////////////////////////
  106. ConsoleParser * getParserForFile(const char *filename);
  107. } // end namespace Con
  108. #endif // _CONSOLE_PARSER_H_