CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Token.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "SourceArea.hpp"
11
15
16#include <memory>
17
19
20 // Token class used by the scanners and parsers.
21 class Token {
22 public:
23 // Token type enumeration.
24 enum class Types {
26
27 // MARK: Identifiers
28 Ident, // (letter | '_') (letter | '_' | digit)*
29
30 // MARK: Literals
31 BoolLiteral, // true | false
32 IntLiteral, // digit+
33 FloatLiteral, // digit+ '.' digit+
34 StringLiteral, // '"' ANY+ '"'
35 CharLiteral, // '\'' ANY '\''
36 NullLiteral, // NULL
37
38 // MARK: Operators
39 AssignOp, // =, +=, -=, *=, /=, %=, <<=, >>=, |= , &=, ^=
40 BinaryOp, // &&, ||, |, ^, &, <<, >>, +, -, *, /, %, ==, !=, <, >, <=, >=
41 UnaryOp, // !, ~, +, -, ++, --
42 TernaryOp, // ?
43
44 // MARK: Punctuation
45 Dot, // .
46 Colon, // :
47 DColon, // ::
48 Semicolon, // ;
49 Comma, // ,
50
51 // MARK: Brackets
52 LBracket, // (
53 RBracket, // )
54 LCurly, // {
55 RCurly, // }
56 LParen, // [
57 RParen, // ]
58
59 // MARK: Type denoters
60 StringType, // string
61 ScalarType, // bool, int, uint, half, float, double
62 VectorType, // ScalarType ('1'-'4')
63 MatrixType, // ScalarType ('1'-'4') 'x' ('1'-'4')
64
65 UniformBuffer, // cbuffer, tbuffer (HLSL); uniform (GLSL)
66 Sampler, // sampler, sampler1D, sampler2D, sampler3D, samplerCUBE (HLSL); sampler2D, isampler1DArray etc. (GLSL)
67
68 Void, // void
69
70 // MARK: Keywords
71 Reserved, // reserved keyword (not allowed, but reserved for future use)
72 Unsupported, // unsupported keyword: interface, class (HLSL); subroutine (GLSL)
73
74 Do, // do
75 While, // while
76 For, // for
77
78 If, // if
79 Else, // else
80
81 Switch, // switch
82 Case, // case
83 Default, // default
84
85 Struct, // struct
86 Class, // class
87 //Interface, // interface
88
89 CtrlTransfer, // break, continue, discard
90 Return, // return
91
92 InputModifier, // in, out, inout, uniform
93 InterpModifier, // linear, centroid, nointerpolation, noperspective, sample
94 TypeModifier, // const, row_major, column_major (also 'snorm' and 'unorm' for floats)
95 StorageClass, // extern, precise, shared, groupshared, static, uniform, volatile
96
97 Inline, // inline
98
99 // MARK: HLSL only Tokens
100 SamplerState, // sampler_state, SamplerState, SamplerComparisonState
101
102 Typedef, // typedef
103 Register, // register
104 PackOffset, // packoffset
105
106 Buffer, // texture, Texture2D, RWStructuredBuffer etc.
107 PrimitiveType, // point, line, lineadj, triangle, triangleadj
108 Vector, // vector (e.g. "vector<float, 3>")
109 Matrix, // matrix (e.g. "matrix<int, 4, 4>")
110
111 // MARK: GLSL only Tokens
112 Attribute, // attribute
113 Varying, // varying
114 Precision, // precision
115 LayoutQualifier, // layout
116 MemoryQualifier, // coherent, volatile, restrict, readonly, writeonly
117 InvariantQualifier, // invariant
118 PrecisionQualifier, // lowp, mediump, highp
119
120 Image, // image1D, uimageCube etc. (GLSL)
121 StorageBuffer, // buffer (GLSL)
122 AtomicCounter, // atomic_uint (GLSL)
123
124 // MARK: Technique keywords
125 Technique, // technique
126 Pass, // pass
127 Compile, // compile
128
129 // MARK: Preprocessor specific tokens
130 Directive, // Preprocessor directive ('#' IDENT).
131 DirectiveConcat, // Preprocessor directive concatenation ('##').
132 Comment, // Commentary (only a single text line)
133 WhiteSpace, // White spaces (' ', '\t')
134 NewLine, // New-line characters ('\n', '\r')
135 LineBreak, // Line break for pre-processor directives '\'
136 VarArg, // Variadic argument specifier ('...').
137 Misc, // Miscellaneous
138
139 // MARK: Special tokens
140 EndOfStream, // End-of-stream
141 };
142
143 Token(Token&& other);
144
145 Token(const SourcePosition& pos, const Types type);
146 Token(const SourcePosition& pos, const Types type, const String& spell);
147 Token(const SourcePosition& pos, const Types type, String&& spell);
148
149 // Returns the source area of this token.
151
152 // Returns a descriptive string for the specified token type.
153 static String typeToString(const Types type);
154
155 // Returns the token spelling of the content (e.g. only the content of a string literal within the quotes).
157
158 // Returns the token type.
159 [[nodiscard]] inline Types getType() const { return mType; }
160
161 // Returns the token source position.
162 [[nodiscard]] inline const SourcePosition& getPosition() const { return mPos; }
163
164 // Returns the token spelling.
165 [[nodiscard]] inline const String& getSpelling() const { return mSpell; }
166
167 private:
168 Types mType; // Type of this token.
169 SourcePosition mPos; // Source area of this token.
170 String mSpell; // Token spelling.
171 };
172
173 // Token shared pointer type.
175
176 // Keyword-to-Token map type.
178
179} // namespace CeresEngine::ShaderCompiler
Definition SourceArea.hpp:20
Definition SourcePosition.hpp:30
Definition Token.hpp:21
Token(const SourcePosition &pos, const Types type, String &&spell)
Token(const SourcePosition &pos, const Types type)
Types
Definition Token.hpp:24
SourcePosition mPos
Definition Token.hpp:169
const String & getSpelling() const
Definition Token.hpp:165
Types getType() const
Definition Token.hpp:159
static String typeToString(const Types type)
const SourcePosition & getPosition() const
Definition Token.hpp:162
Types mType
Definition Token.hpp:168
String mSpell
Definition Token.hpp:170
Token(const SourcePosition &pos, const Types type, const String &spell)
Definition AST.hpp:33
Map< String, Token::Types > KeywordMapType
Definition Token.hpp:177
SPtr< Token > TokenPtr
Definition Token.hpp:174
std::shared_ptr< T > SPtr
SPtr is a smart pointer that retains shared ownership of an object through a pointer.
Definition SmartPtr.hpp:37
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
std::map< Key, T, Compare, ScopedAllocatorAdaptor< StdAllocator< Pair< const Key, T >, RawAllocator > > > Map
Map is a sorted associative container that contains key-value pairs with unique keys.
Definition Map.hpp:24