hlslTokenStream.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include "hlslTokenStream.h"
  36. namespace glslang {
  37. void HlslTokenStream::pushPreToken(const HlslToken& tok)
  38. {
  39. assert(preTokenStackSize < tokenBufferSize);
  40. preTokenStack[preTokenStackSize++] = tok;
  41. }
  42. HlslToken HlslTokenStream::popPreToken()
  43. {
  44. assert(preTokenStackSize > 0);
  45. return preTokenStack[--preTokenStackSize];
  46. }
  47. void HlslTokenStream::pushTokenBuffer(const HlslToken& tok)
  48. {
  49. tokenBuffer[tokenBufferPos] = tok;
  50. tokenBufferPos = (tokenBufferPos+1) % tokenBufferSize;
  51. }
  52. HlslToken HlslTokenStream::popTokenBuffer()
  53. {
  54. // Back up
  55. tokenBufferPos = (tokenBufferPos+tokenBufferSize-1) % tokenBufferSize;
  56. return tokenBuffer[tokenBufferPos];
  57. }
  58. //
  59. // Make a new source of tokens, not from the source, but from an
  60. // already pre-processed token stream.
  61. //
  62. // This interrupts current token processing which must be restored
  63. // later. Some simplifying assumptions are made (and asserted).
  64. //
  65. void HlslTokenStream::pushTokenStream(const TVector<HlslToken>* tokens)
  66. {
  67. // not yet setup to interrupt a stream that has been receded
  68. // and not yet reconsumed
  69. assert(preTokenStackSize == 0);
  70. // save current state
  71. currentTokenStack.push_back(token);
  72. // set up new token stream
  73. tokenStreamStack.push_back(tokens);
  74. // start position at first token:
  75. token = (*tokens)[0];
  76. tokenPosition.push_back(0);
  77. }
  78. // Undo pushTokenStream(), see above
  79. void HlslTokenStream::popTokenStream()
  80. {
  81. tokenStreamStack.pop_back();
  82. tokenPosition.pop_back();
  83. token = currentTokenStack.back();
  84. currentTokenStack.pop_back();
  85. }
  86. // Load 'token' with the next token in the stream of tokens.
  87. void HlslTokenStream::advanceToken()
  88. {
  89. pushTokenBuffer(token);
  90. if (preTokenStackSize > 0)
  91. token = popPreToken();
  92. else {
  93. if (tokenStreamStack.size() == 0)
  94. scanner.tokenize(token);
  95. else {
  96. ++tokenPosition.back();
  97. if (tokenPosition.back() >= (int)tokenStreamStack.back()->size())
  98. token.tokenClass = EHTokNone;
  99. else
  100. token = (*tokenStreamStack.back())[tokenPosition.back()];
  101. }
  102. }
  103. }
  104. void HlslTokenStream::recedeToken()
  105. {
  106. pushPreToken(token);
  107. token = popTokenBuffer();
  108. }
  109. // Return the current token class.
  110. EHlslTokenClass HlslTokenStream::peek() const
  111. {
  112. return token.tokenClass;
  113. }
  114. // Return true, without advancing to the next token, if the current token is
  115. // the expected (passed in) token class.
  116. bool HlslTokenStream::peekTokenClass(EHlslTokenClass tokenClass) const
  117. {
  118. return peek() == tokenClass;
  119. }
  120. // Return true and advance to the next token if the current token is the
  121. // expected (passed in) token class.
  122. bool HlslTokenStream::acceptTokenClass(EHlslTokenClass tokenClass)
  123. {
  124. if (peekTokenClass(tokenClass)) {
  125. advanceToken();
  126. return true;
  127. }
  128. return false;
  129. }
  130. } // end namespace glslang