Parser.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef ANKI_MISC_PARSER_H
  2. #define ANKI_MISC_PARSER_H
  3. #include "anki/util/Exception.h"
  4. #include "anki/util/Scanner.h"
  5. namespace anki {
  6. /// It contains some functions and macros that are used pretty often while
  7. /// parsing
  8. namespace parser {
  9. /// Parser macros
  10. #define PARSER_EXCEPTION(x) \
  11. ANKI_EXCEPTION("Parser exception (" + scanner.getScriptName() + ':' + \
  12. std::to_string(scanner.getLineNumber()) + "): " + x)
  13. #define PARSER_EXCEPTION_EXPECTED(x) \
  14. PARSER_EXCEPTION("Expected " + x + " and not " + \
  15. scanner.getCrntToken().getInfoString())
  16. #define PARSER_EXCEPTION_UNEXPECTED() \
  17. PARSER_EXCEPTION("Unexpected token " + \
  18. scanner.getCrntToken().getInfoString())
  19. /// This template func is used for a common operation of parsing arrays of
  20. /// numbers
  21. ///
  22. /// It parses expressions like this one: { 10 -0.2 123.e-10 -0x0FF } and stores
  23. /// the result in the arr array. The acceptable types (typename Type) are
  24. /// integer or floating point types
  25. ///
  26. /// @param scanner The scanner that we will use
  27. /// @param bracket If true the array starts and ends with brackets eg {10 0 -1}
  28. /// @param signs If true the array has numbers that may contain sign
  29. /// @param size The count of numbers of the array wa want to parse
  30. /// @param arr The array that the func returns the numbers
  31. /// @exception Exception
  32. template <typename Type>
  33. void parseArrOfNumbers(scanner::Scanner& scanner, bool bracket, bool signs,
  34. uint size, Type* arr);
  35. /// Parse a single number
  36. /// @param scanner The scanner that we will use
  37. /// @param sign If true expect sign or not
  38. /// @param out The output number
  39. template <typename Type>
  40. void parseNumber(scanner::Scanner& scanner, bool sign, Type& out);
  41. /// Parses a math structure (Vec3, Vec4, Mat3 etc) with leading and following
  42. /// brackets. Eg {0.1 0.2 0.3}
  43. template <typename Type>
  44. void parseMathVector(scanner::Scanner& scanner, Type& out);
  45. /// Parse true or false identifiers
  46. extern bool parseBool(scanner::Scanner& scanner);
  47. /// Parse identifier
  48. extern std::string parseIdentifier(scanner::Scanner& scanner,
  49. const char* expectedIdentifier = NULL);
  50. /// Is identifier
  51. extern bool isIdentifier(const scanner::Token& token, const char* str);
  52. /// Parse string
  53. extern std::string parseString(scanner::Scanner& scanner);
  54. } // end namespace
  55. } // end namespace
  56. #include "anki/misc/Parser.inl.h"
  57. #endif