hlslScanContext.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Copyright (C) 2016 Google, Inc.
  3. //
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions
  8. // are met:
  9. //
  10. // Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. //
  13. // Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following
  15. // disclaimer in the documentation and/or other materials provided
  16. // with the distribution.
  17. //
  18. // Neither the name of Google, Inc., nor the names of its
  19. // contributors may be used to endorse or promote products derived
  20. // from this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. // POSSIBILITY OF SUCH DAMAGE.
  34. //
  35. //
  36. // This holds context specific to the HLSL scanner, which
  37. // sits between the preprocessor scanner and HLSL parser.
  38. //
  39. #ifndef HLSLSCANCONTEXT_H_
  40. #define HLSLSCANCONTEXT_H_
  41. #include "../glslang/MachineIndependent/ParseHelper.h"
  42. #include "hlslTokens.h"
  43. namespace glslang {
  44. class TPpContext;
  45. class TPpToken;
  46. //
  47. // Everything needed to fully describe a token.
  48. //
  49. struct HlslToken {
  50. HlslToken() : string(nullptr) { loc.init(); }
  51. TSourceLoc loc; // location of token in the source
  52. EHlslTokenClass tokenClass; // what kind of token it is
  53. union { // what data the token holds
  54. glslang::TString *string; // for identifiers
  55. int i; // for literals
  56. unsigned int u;
  57. bool b;
  58. double d;
  59. };
  60. };
  61. //
  62. // The state of scanning and translating raw tokens to slightly richer
  63. // semantics, like knowing if an identifier is an existing symbol, or
  64. // user-defined type.
  65. //
  66. class HlslScanContext {
  67. public:
  68. HlslScanContext(TParseContextBase& parseContext, TPpContext& ppContext)
  69. : parseContext(parseContext), ppContext(ppContext) { }
  70. virtual ~HlslScanContext() { }
  71. static void fillInKeywordMap();
  72. static void deleteKeywordMap();
  73. void tokenize(HlslToken&);
  74. glslang::TBuiltInVariable mapSemantic(const char*);
  75. protected:
  76. HlslScanContext(HlslScanContext&);
  77. HlslScanContext& operator=(HlslScanContext&);
  78. EHlslTokenClass tokenizeClass(HlslToken&);
  79. EHlslTokenClass tokenizeIdentifier();
  80. EHlslTokenClass identifierOrType();
  81. EHlslTokenClass reservedWord();
  82. EHlslTokenClass identifierOrReserved(bool reserved);
  83. EHlslTokenClass nonreservedKeyword(int version);
  84. TParseContextBase& parseContext;
  85. TPpContext& ppContext;
  86. TSourceLoc loc;
  87. TPpToken* ppToken;
  88. HlslToken* parserToken;
  89. const char* tokenText;
  90. EHlslTokenClass keyword;
  91. };
  92. } // end namespace glslang
  93. #endif // HLSLSCANCONTEXT_H_