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 | |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 106 | static std::string OperatorName(Kind kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 107 | switch (kind) { |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 108 | case Token::PLUS: return "+"; |
| 109 | case Token::MINUS: return "-"; |
| 110 | case Token::STAR: return "*"; |
| 111 | case Token::SLASH: return "/"; |
| 112 | case Token::PERCENT: return "%"; |
| 113 | case Token::SHL: return "<<"; |
| 114 | case Token::SHR: return ">>"; |
| 115 | case Token::LOGICALNOT: return "!"; |
| 116 | case Token::LOGICALAND: return "&&"; |
| 117 | case Token::LOGICALOR: return "||"; |
| 118 | case Token::LOGICALXOR: return "^^"; |
| 119 | case Token::BITWISENOT: return "~"; |
| 120 | case Token::BITWISEAND: return "&"; |
| 121 | case Token::BITWISEOR: return "|"; |
| 122 | case Token::BITWISEXOR: return "^"; |
| 123 | case Token::EQ: return "="; |
| 124 | case Token::EQEQ: return "=="; |
| 125 | case Token::NEQ: return "!="; |
| 126 | case Token::LT: return "<"; |
| 127 | case Token::GT: return ">"; |
| 128 | case Token::LTEQ: return "<="; |
| 129 | case Token::GTEQ: return ">="; |
| 130 | case Token::PLUSEQ: return "+="; |
| 131 | case Token::MINUSEQ: return "-="; |
| 132 | case Token::STAREQ: return "*="; |
| 133 | case Token::SLASHEQ: return "/="; |
| 134 | case Token::PERCENTEQ: return "%="; |
| 135 | case Token::SHLEQ: return "<<="; |
| 136 | case Token::SHREQ: return ">>="; |
| 137 | case Token::LOGICALANDEQ: return "&&="; |
| 138 | case Token::LOGICALOREQ: return "||="; |
| 139 | case Token::LOGICALXOREQ: return "^^="; |
| 140 | case Token::BITWISEANDEQ: return "&="; |
| 141 | case Token::BITWISEOREQ: return "|="; |
| 142 | case Token::BITWISEXOREQ: return "^="; |
| 143 | case Token::PLUSPLUS: return "++"; |
| 144 | case Token::MINUSMINUS: return "--"; |
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 | |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 153 | Token(Position position, Kind kind, std::string 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) |
Greg Daniel | 792d0f1 | 2016-11-20 14:53:35 +0000 | [diff] [blame^] | 162 | std::string fText; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | } // namespace |
| 166 | #endif |