blob: 5c8c2bd2154de208cb2e68850637be8b69b1eb04 [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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
14namespace SkSL {
15
ethannicholasf789b382016-08-03 12:43:36 -070016#undef IN
17#undef OUT
18#undef CONST
19
ethannicholasb3058bd2016-07-01 08:22:01 -070020/**
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 */
24struct 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,
ethannicholas5961bc92016-10-12 06:39:56 -070052 BITWISENOT,
ethannicholasb3058bd2016-07-01 08:22:01 -070053 LOGICALOR,
54 LOGICALXOR,
55 LOGICALAND,
ethannicholas5961bc92016-10-12 06:39:56 -070056 LOGICALNOT,
ethannicholasb3058bd2016-07-01 08:22:01 -070057 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,
ethannicholasf789b382016-08-03 12:43:36 -070097 FLAT,
98 NOPERSPECTIVE,
Brian Salomonf9f45122016-11-29 11:59:17 -050099 READONLY,
100 WRITEONLY,
101 COHERENT,
102 VOLATILE,
103 RESTRICT,
ethannicholasb3058bd2016-07-01 08:22:01 -0700104 STRUCT,
105 LAYOUT,
106 DIRECTIVE,
107 PRECISION,
108 INVALID_TOKEN
109 };
110
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500111 static SkString OperatorName(Kind kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700112 switch (kind) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500113 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("--");
ethannicholasb3058bd2016-07-01 08:22:01 -0700150 default:
151 ABORT("unsupported operator: %d\n", kind);
152 }
153 }
154
155 Token() {
156 }
157
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500158 Token(Position position, Kind kind, SkString text)
ethannicholasb3058bd2016-07-01 08:22:01 -0700159 : fPosition(position)
160 , fKind(kind)
161 , fText(std::move(text)) {}
162
163 Position fPosition;
164 Kind fKind;
ethannicholas471e8942016-10-28 09:02:46 -0700165 // will be the empty string unless the token has variable text content (identifiers, numeric
166 // literals, and directives)
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500167 SkString fText;
ethannicholasb3058bd2016-07-01 08:22:01 -0700168};
169
170} // namespace
171#endif