ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SKSL_TOKEN |
| 9 | #define SKSL_TOKEN |
| 10 | |
| 11 | #include "SkSLPosition.h" |
| 12 | #include "SkSLUtil.h" |
| 13 | |
| 14 | namespace SkSL { |
| 15 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 16 | #undef IN |
| 17 | #undef OUT |
| 18 | #undef CONST |
| 19 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 20 | /** |
| 21 | * Represents a lexical analysis token. Token is generally only used during the parse process, but |
| 22 | * Token::Kind is also used to represent operator kinds. |
| 23 | */ |
| 24 | struct Token { |
| 25 | enum Kind { |
| 26 | END_OF_FILE, |
| 27 | IDENTIFIER, |
| 28 | INT_LITERAL, |
| 29 | FLOAT_LITERAL, |
| 30 | TRUE_LITERAL, |
| 31 | FALSE_LITERAL, |
| 32 | LPAREN, |
| 33 | RPAREN, |
| 34 | LBRACE, |
| 35 | RBRACE, |
| 36 | LBRACKET, |
| 37 | RBRACKET, |
| 38 | DOT, |
| 39 | COMMA, |
| 40 | PLUSPLUS, |
| 41 | MINUSMINUS, |
| 42 | PLUS, |
| 43 | MINUS, |
| 44 | STAR, |
| 45 | SLASH, |
| 46 | PERCENT, |
| 47 | SHL, |
| 48 | SHR, |
| 49 | BITWISEOR, |
| 50 | BITWISEXOR, |
| 51 | BITWISEAND, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 52 | BITWISENOT, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 53 | LOGICALOR, |
| 54 | LOGICALXOR, |
| 55 | LOGICALAND, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 56 | LOGICALNOT, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 57 | QUESTION, |
| 58 | COLON, |
| 59 | EQ, |
| 60 | EQEQ, |
| 61 | NEQ, |
| 62 | GT, |
| 63 | LT, |
| 64 | GTEQ, |
| 65 | LTEQ, |
| 66 | PLUSEQ, |
| 67 | MINUSEQ, |
| 68 | STAREQ, |
| 69 | SLASHEQ, |
| 70 | PERCENTEQ, |
| 71 | SHLEQ, |
| 72 | SHREQ, |
| 73 | BITWISEOREQ, |
| 74 | BITWISEXOREQ, |
| 75 | BITWISEANDEQ, |
| 76 | LOGICALOREQ, |
| 77 | LOGICALXOREQ, |
| 78 | LOGICALANDEQ, |
| 79 | SEMICOLON, |
| 80 | IF, |
| 81 | ELSE, |
| 82 | FOR, |
| 83 | WHILE, |
| 84 | DO, |
| 85 | RETURN, |
| 86 | BREAK, |
| 87 | CONTINUE, |
| 88 | DISCARD, |
| 89 | IN, |
| 90 | OUT, |
| 91 | INOUT, |
| 92 | CONST, |
| 93 | LOWP, |
| 94 | MEDIUMP, |
| 95 | HIGHP, |
| 96 | UNIFORM, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 97 | FLAT, |
| 98 | NOPERSPECTIVE, |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame^] | 99 | READONLY, |
| 100 | WRITEONLY, |
| 101 | COHERENT, |
| 102 | VOLATILE, |
| 103 | RESTRICT, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 104 | STRUCT, |
| 105 | LAYOUT, |
| 106 | DIRECTIVE, |
| 107 | PRECISION, |
| 108 | INVALID_TOKEN |
| 109 | }; |
| 110 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 111 | static SkString OperatorName(Kind kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 112 | switch (kind) { |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 113 | case Token::PLUS: return SkString("+"); |
| 114 | case Token::MINUS: return SkString("-"); |
| 115 | case Token::STAR: return SkString("*"); |
| 116 | case Token::SLASH: return SkString("/"); |
| 117 | case Token::PERCENT: return SkString("%"); |
| 118 | case Token::SHL: return SkString("<<"); |
| 119 | case Token::SHR: return SkString(">>"); |
| 120 | case Token::LOGICALNOT: return SkString("!"); |
| 121 | case Token::LOGICALAND: return SkString("&&"); |
| 122 | case Token::LOGICALOR: return SkString("||"); |
| 123 | case Token::LOGICALXOR: return SkString("^^"); |
| 124 | case Token::BITWISENOT: return SkString("~"); |
| 125 | case Token::BITWISEAND: return SkString("&"); |
| 126 | case Token::BITWISEOR: return SkString("|"); |
| 127 | case Token::BITWISEXOR: return SkString("^"); |
| 128 | case Token::EQ: return SkString("="); |
| 129 | case Token::EQEQ: return SkString("=="); |
| 130 | case Token::NEQ: return SkString("!="); |
| 131 | case Token::LT: return SkString("<"); |
| 132 | case Token::GT: return SkString(">"); |
| 133 | case Token::LTEQ: return SkString("<="); |
| 134 | case Token::GTEQ: return SkString(">="); |
| 135 | case Token::PLUSEQ: return SkString("+="); |
| 136 | case Token::MINUSEQ: return SkString("-="); |
| 137 | case Token::STAREQ: return SkString("*="); |
| 138 | case Token::SLASHEQ: return SkString("/="); |
| 139 | case Token::PERCENTEQ: return SkString("%="); |
| 140 | case Token::SHLEQ: return SkString("<<="); |
| 141 | case Token::SHREQ: return SkString(">>="); |
| 142 | case Token::LOGICALANDEQ: return SkString("&&="); |
| 143 | case Token::LOGICALOREQ: return SkString("||="); |
| 144 | case Token::LOGICALXOREQ: return SkString("^^="); |
| 145 | case Token::BITWISEANDEQ: return SkString("&="); |
| 146 | case Token::BITWISEOREQ: return SkString("|="); |
| 147 | case Token::BITWISEXOREQ: return SkString("^="); |
| 148 | case Token::PLUSPLUS: return SkString("++"); |
| 149 | case Token::MINUSMINUS: return SkString("--"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 150 | default: |
| 151 | ABORT("unsupported operator: %d\n", kind); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | Token() { |
| 156 | } |
| 157 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 158 | Token(Position position, Kind kind, SkString text) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 159 | : fPosition(position) |
| 160 | , fKind(kind) |
| 161 | , fText(std::move(text)) {} |
| 162 | |
| 163 | Position fPosition; |
| 164 | Kind fKind; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 165 | // will be the empty string unless the token has variable text content (identifiers, numeric |
| 166 | // literals, and directives) |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 167 | SkString fText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | } // namespace |
| 171 | #endif |