blob: 76a1df7f665fd7ae3edae20f908991b3c33a3564 [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,
ethannicholasb3058bd2016-07-01 08:22:01 -070099 STRUCT,
100 LAYOUT,
101 DIRECTIVE,
102 PRECISION,
103 INVALID_TOKEN
104 };
105
Greg Daniel792d0f12016-11-20 14:53:35 +0000106 static std::string OperatorName(Kind kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700107 switch (kind) {
Greg Daniel792d0f12016-11-20 14:53:35 +0000108 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 "--";
ethannicholasb3058bd2016-07-01 08:22:01 -0700145 default:
146 ABORT("unsupported operator: %d\n", kind);
147 }
148 }
149
150 Token() {
151 }
152
Greg Daniel792d0f12016-11-20 14:53:35 +0000153 Token(Position position, Kind kind, std::string text)
ethannicholasb3058bd2016-07-01 08:22:01 -0700154 : fPosition(position)
155 , fKind(kind)
156 , fText(std::move(text)) {}
157
158 Position fPosition;
159 Kind fKind;
ethannicholas471e8942016-10-28 09:02:46 -0700160 // will be the empty string unless the token has variable text content (identifiers, numeric
161 // literals, and directives)
Greg Daniel792d0f12016-11-20 14:53:35 +0000162 std::string fText;
ethannicholasb3058bd2016-07-01 08:22:01 -0700163};
164
165} // namespace
166#endif