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, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 99 | STRUCT, |
| 100 | LAYOUT, |
| 101 | DIRECTIVE, |
| 102 | PRECISION, |
| 103 | INVALID_TOKEN |
| 104 | }; |
| 105 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 106 | static SkString OperatorName(Kind kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 107 | switch (kind) { |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 108 | case Token::PLUS: return SkString("+"); |
| 109 | case Token::MINUS: return SkString("-"); |
| 110 | case Token::STAR: return SkString("*"); |
| 111 | case Token::SLASH: return SkString("/"); |
| 112 | case Token::PERCENT: return SkString("%"); |
| 113 | case Token::SHL: return SkString("<<"); |
| 114 | case Token::SHR: return SkString(">>"); |
| 115 | case Token::LOGICALNOT: return SkString("!"); |
| 116 | case Token::LOGICALAND: return SkString("&&"); |
| 117 | case Token::LOGICALOR: return SkString("||"); |
| 118 | case Token::LOGICALXOR: return SkString("^^"); |
| 119 | case Token::BITWISENOT: return SkString("~"); |
| 120 | case Token::BITWISEAND: return SkString("&"); |
| 121 | case Token::BITWISEOR: return SkString("|"); |
| 122 | case Token::BITWISEXOR: return SkString("^"); |
| 123 | case Token::EQ: return SkString("="); |
| 124 | case Token::EQEQ: return SkString("=="); |
| 125 | case Token::NEQ: return SkString("!="); |
| 126 | case Token::LT: return SkString("<"); |
| 127 | case Token::GT: return SkString(">"); |
| 128 | case Token::LTEQ: return SkString("<="); |
| 129 | case Token::GTEQ: return SkString(">="); |
| 130 | case Token::PLUSEQ: return SkString("+="); |
| 131 | case Token::MINUSEQ: return SkString("-="); |
| 132 | case Token::STAREQ: return SkString("*="); |
| 133 | case Token::SLASHEQ: return SkString("/="); |
| 134 | case Token::PERCENTEQ: return SkString("%="); |
| 135 | case Token::SHLEQ: return SkString("<<="); |
| 136 | case Token::SHREQ: return SkString(">>="); |
| 137 | case Token::LOGICALANDEQ: return SkString("&&="); |
| 138 | case Token::LOGICALOREQ: return SkString("||="); |
| 139 | case Token::LOGICALXOREQ: return SkString("^^="); |
| 140 | case Token::BITWISEANDEQ: return SkString("&="); |
| 141 | case Token::BITWISEOREQ: return SkString("|="); |
| 142 | case Token::BITWISEXOREQ: return SkString("^="); |
| 143 | case Token::PLUSPLUS: return SkString("++"); |
| 144 | case Token::MINUSMINUS: return SkString("--"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 145 | default: |
| 146 | ABORT("unsupported operator: %d\n", kind); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | Token() { |
| 151 | } |
| 152 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 153 | Token(Position position, Kind kind, SkString text) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 154 | : fPosition(position) |
| 155 | , fKind(kind) |
| 156 | , fText(std::move(text)) {} |
| 157 | |
| 158 | Position fPosition; |
| 159 | Kind fKind; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 160 | // will be the empty string unless the token has variable text content (identifiers, numeric |
| 161 | // literals, and directives) |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame^] | 162 | SkString fText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | } // namespace |
| 166 | #endif |