script_class_parser.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*************************************************************************/
  2. /* script_class_parser.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef SCRIPT_CLASS_PARSER_H
  31. #define SCRIPT_CLASS_PARSER_H
  32. #include "core/ustring.h"
  33. #include "core/variant.h"
  34. #include "core/vector.h"
  35. class ScriptClassParser {
  36. public:
  37. struct NameDecl {
  38. enum Type {
  39. NAMESPACE_DECL,
  40. CLASS_DECL,
  41. STRUCT_DECL
  42. };
  43. String name;
  44. Type type;
  45. };
  46. struct ClassDecl {
  47. String name;
  48. String namespace_;
  49. Vector<String> base;
  50. bool nested;
  51. bool has_script_attr;
  52. };
  53. private:
  54. String code;
  55. int idx;
  56. int line;
  57. String error_str;
  58. bool error;
  59. Variant value;
  60. Vector<ClassDecl> classes;
  61. enum Token {
  62. TK_BRACKET_OPEN,
  63. TK_BRACKET_CLOSE,
  64. TK_CURLY_BRACKET_OPEN,
  65. TK_CURLY_BRACKET_CLOSE,
  66. TK_PERIOD,
  67. TK_COLON,
  68. TK_COMMA,
  69. TK_SYMBOL,
  70. TK_IDENTIFIER,
  71. TK_STRING,
  72. TK_NUMBER,
  73. TK_OP_LESS,
  74. TK_OP_GREATER,
  75. TK_EOF,
  76. TK_ERROR,
  77. TK_MAX
  78. };
  79. static const char *token_names[TK_MAX];
  80. static String get_token_name(Token p_token);
  81. Token get_token();
  82. Error _skip_generic_type_params();
  83. Error _parse_type_full_name(String &r_full_name);
  84. Error _parse_class_base(Vector<String> &r_base);
  85. Error _parse_type_constraints();
  86. Error _parse_namespace_name(String &r_name, int &r_curly_stack);
  87. public:
  88. Error parse(const String &p_code);
  89. Error parse_file(const String &p_filepath);
  90. String get_error();
  91. Vector<ClassDecl> get_classes();
  92. };
  93. #endif // SCRIPT_CLASS_PARSER_H