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 16 #undef IN 17 #undef OUT 18 #undef CONST 19 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 WHITESPACE, 28 IDENTIFIER, 29 INT_LITERAL, 30 FLOAT_LITERAL, 31 TRUE_LITERAL, 32 FALSE_LITERAL, 33 LPAREN, 34 RPAREN, 35 LBRACE, 36 RBRACE, 37 LBRACKET, 38 RBRACKET, 39 DOT, 40 COMMA, 41 PLUSPLUS, 42 MINUSMINUS, 43 PLUS, 44 MINUS, 45 STAR, 46 SLASH, 47 PERCENT, 48 SHL, 49 SHR, 50 BITWISEOR, 51 BITWISEXOR, 52 BITWISEAND, 53 BITWISENOT, 54 LOGICALOR, 55 LOGICALXOR, 56 LOGICALAND, 57 LOGICALNOT, 58 QUESTION, 59 COLON, 60 EQ, 61 EQEQ, 62 NEQ, 63 GT, 64 LT, 65 GTEQ, 66 LTEQ, 67 PLUSEQ, 68 MINUSEQ, 69 STAREQ, 70 SLASHEQ, 71 PERCENTEQ, 72 SHLEQ, 73 SHREQ, 74 BITWISEOREQ, 75 BITWISEXOREQ, 76 BITWISEANDEQ, 77 LOGICALOREQ, 78 LOGICALXOREQ, 79 LOGICALANDEQ, 80 SEMICOLON, 81 ARROW, 82 COLONCOLON, 83 IF, 84 STATIC_IF, 85 ELSE, 86 FOR, 87 WHILE, 88 DO, 89 SWITCH, 90 STATIC_SWITCH, 91 CASE, 92 DEFAULT, 93 RETURN, 94 BREAK, 95 CONTINUE, 96 DISCARD, 97 IN, 98 OUT, 99 INOUT, 100 CONST, 101 LOWP, 102 MEDIUMP, 103 HIGHP, 104 UNIFORM, 105 FLAT, 106 NOPERSPECTIVE, 107 READONLY, 108 WRITEONLY, 109 COHERENT, 110 VOLATILE, 111 RESTRICT, 112 BUFFER, 113 HASSIDEEFFECTS, 114 STRUCT, 115 LAYOUT, 116 DIRECTIVE, 117 SECTION, 118 PRECISION, 119 LOCATION, 120 OFFSET, 121 BINDING, 122 INDEX, 123 SET, 124 BUILTIN, 125 INPUT_ATTACHMENT_INDEX, 126 ORIGIN_UPPER_LEFT, 127 OVERRIDE_COVERAGE, 128 BLEND_SUPPORT_ALL_EQUATIONS, 129 PUSH_CONSTANT, 130 POINTS, 131 LINES, 132 LINE_STRIP, 133 LINES_ADJACENCY, 134 TRIANGLES, 135 TRIANGLE_STRIP, 136 TRIANGLES_ADJACENCY, 137 MAX_VERTICES, 138 INVOCATIONS, 139 WHEN, 140 KEY, 141 INVALID_TOKEN 142 }; 143 OperatorNameToken144 static String OperatorName(Kind kind) { 145 switch (kind) { 146 case Token::PLUS: return String("+"); 147 case Token::MINUS: return String("-"); 148 case Token::STAR: return String("*"); 149 case Token::SLASH: return String("/"); 150 case Token::PERCENT: return String("%"); 151 case Token::SHL: return String("<<"); 152 case Token::SHR: return String(">>"); 153 case Token::LOGICALNOT: return String("!"); 154 case Token::LOGICALAND: return String("&&"); 155 case Token::LOGICALOR: return String("||"); 156 case Token::LOGICALXOR: return String("^^"); 157 case Token::BITWISENOT: return String("~"); 158 case Token::BITWISEAND: return String("&"); 159 case Token::BITWISEOR: return String("|"); 160 case Token::BITWISEXOR: return String("^"); 161 case Token::EQ: return String("="); 162 case Token::EQEQ: return String("=="); 163 case Token::NEQ: return String("!="); 164 case Token::LT: return String("<"); 165 case Token::GT: return String(">"); 166 case Token::LTEQ: return String("<="); 167 case Token::GTEQ: return String(">="); 168 case Token::PLUSEQ: return String("+="); 169 case Token::MINUSEQ: return String("-="); 170 case Token::STAREQ: return String("*="); 171 case Token::SLASHEQ: return String("/="); 172 case Token::PERCENTEQ: return String("%="); 173 case Token::SHLEQ: return String("<<="); 174 case Token::SHREQ: return String(">>="); 175 case Token::LOGICALANDEQ: return String("&&="); 176 case Token::LOGICALOREQ: return String("||="); 177 case Token::LOGICALXOREQ: return String("^^="); 178 case Token::BITWISEANDEQ: return String("&="); 179 case Token::BITWISEOREQ: return String("|="); 180 case Token::BITWISEXOREQ: return String("^="); 181 case Token::PLUSPLUS: return String("++"); 182 case Token::MINUSMINUS: return String("--"); 183 case Token::COMMA: return String(","); 184 default: 185 ABORT("unsupported operator: %d\n", kind); 186 } 187 } 188 TokenToken189 Token() { 190 } 191 TokenToken192 Token(Position position, Kind kind, String text) 193 : fPosition(position) 194 , fKind(kind) 195 , fText(std::move(text)) {} 196 IsAssignmentToken197 static bool IsAssignment(Token::Kind op) { 198 switch (op) { 199 case Token::EQ: // fall through 200 case Token::PLUSEQ: // fall through 201 case Token::MINUSEQ: // fall through 202 case Token::STAREQ: // fall through 203 case Token::SLASHEQ: // fall through 204 case Token::PERCENTEQ: // fall through 205 case Token::SHLEQ: // fall through 206 case Token::SHREQ: // fall through 207 case Token::BITWISEOREQ: // fall through 208 case Token::BITWISEXOREQ: // fall through 209 case Token::BITWISEANDEQ: // fall through 210 case Token::LOGICALOREQ: // fall through 211 case Token::LOGICALXOREQ: // fall through 212 case Token::LOGICALANDEQ: 213 return true; 214 default: 215 return false; 216 } 217 } 218 219 Position fPosition; 220 Kind fKind; 221 // will be the empty string unless the token has variable text content (identifiers, numeric 222 // literals, and directives) 223 String fText; 224 }; 225 226 } // namespace 227 #endif 228