John Kessenich | 9c86c6a | 2016-05-03 22:49:24 -0600 | [diff] [blame] | 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 | #include "hlslTokenStream.h" |
| 37 | |
| 38 | namespace glslang { |
| 39 | |
John Kessenich | 1cc1a28 | 2016-06-03 16:55:49 -0600 | [diff] [blame] | 40 | void HlslTokenStream::pushPreToken(const HlslToken& tok) |
| 41 | { |
steve-lunarg | 5964c64 | 2016-07-30 07:38:55 -0600 | [diff] [blame] | 42 | assert(preTokenStackSize < tokenBufferSize); |
| 43 | preTokenStack[preTokenStackSize++] = tok; |
John Kessenich | 1cc1a28 | 2016-06-03 16:55:49 -0600 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | HlslToken HlslTokenStream::popPreToken() |
| 47 | { |
steve-lunarg | 5964c64 | 2016-07-30 07:38:55 -0600 | [diff] [blame] | 48 | assert(preTokenStackSize > 0); |
John Kessenich | 1cc1a28 | 2016-06-03 16:55:49 -0600 | [diff] [blame] | 49 | |
steve-lunarg | 5964c64 | 2016-07-30 07:38:55 -0600 | [diff] [blame] | 50 | return preTokenStack[--preTokenStackSize]; |
John Kessenich | 1cc1a28 | 2016-06-03 16:55:49 -0600 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void HlslTokenStream::pushTokenBuffer(const HlslToken& tok) |
| 54 | { |
steve-lunarg | 5964c64 | 2016-07-30 07:38:55 -0600 | [diff] [blame] | 55 | tokenBuffer[tokenBufferPos] = tok; |
| 56 | tokenBufferPos = (tokenBufferPos+1) % tokenBufferSize; |
John Kessenich | 1cc1a28 | 2016-06-03 16:55:49 -0600 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | HlslToken HlslTokenStream::popTokenBuffer() |
| 60 | { |
steve-lunarg | 5964c64 | 2016-07-30 07:38:55 -0600 | [diff] [blame] | 61 | // Back up |
| 62 | tokenBufferPos = (tokenBufferPos+tokenBufferSize-1) % tokenBufferSize; |
| 63 | |
| 64 | return tokenBuffer[tokenBufferPos]; |
John Kessenich | 1cc1a28 | 2016-06-03 16:55:49 -0600 | [diff] [blame] | 65 | } |
| 66 | |
John Kessenich | 9c86c6a | 2016-05-03 22:49:24 -0600 | [diff] [blame] | 67 | // Load 'token' with the next token in the stream of tokens. |
| 68 | void HlslTokenStream::advanceToken() |
| 69 | { |
John Kessenich | 1cc1a28 | 2016-06-03 16:55:49 -0600 | [diff] [blame] | 70 | pushTokenBuffer(token); |
| 71 | if (preTokenStackSize > 0) |
| 72 | token = popPreToken(); |
| 73 | else |
| 74 | scanner.tokenize(token); |
| 75 | } |
| 76 | |
| 77 | void HlslTokenStream::recedeToken() |
| 78 | { |
| 79 | pushPreToken(token); |
| 80 | token = popTokenBuffer(); |
John Kessenich | 9c86c6a | 2016-05-03 22:49:24 -0600 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // Return the current token class. |
| 84 | EHlslTokenClass HlslTokenStream::peek() const |
| 85 | { |
| 86 | return token.tokenClass; |
| 87 | } |
| 88 | |
| 89 | // Return true, without advancing to the next token, if the current token is |
| 90 | // the expected (passed in) token class. |
| 91 | bool HlslTokenStream::peekTokenClass(EHlslTokenClass tokenClass) const |
| 92 | { |
| 93 | return peek() == tokenClass; |
| 94 | } |
| 95 | |
| 96 | // Return true and advance to the next token if the current token is the |
| 97 | // expected (passed in) token class. |
| 98 | bool HlslTokenStream::acceptTokenClass(EHlslTokenClass tokenClass) |
| 99 | { |
| 100 | if (peekTokenClass(tokenClass)) { |
| 101 | advanceToken(); |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | } // end namespace glslang |