1 /* 2 SPDX-FileCopyrightText: 2003-2009 Cies Breijs <cies AT kde DOT nl> 3 4 SPDX-License-Identifier: GPL-2.0-or-later 5 */ 6 7 8 #ifndef _TOKEN_H_ 9 #define _TOKEN_H_ 10 11 #include <QString> 12 13 14 /** 15 * @short The Token object, represents a piece of TurtleScript as found by the Tokenizer. 16 * 17 * Because of the goals of KTurtle it this class very elaborate. Much info 18 * about each token is kept so descriptive error messages can be printed, 19 * and proper highlighting and context help made possible. 20 * 21 * Tokens are made by the Tokenizer according to the TurtleScript, then they are stored 22 * in the node tree by the Parser or used by the Highlighter of for context help. 23 * 24 * A large potion of the code of this class (the Type enum) is generated code. 25 * 26 * @TODO investigate if it will be better to replace this class by a struct for speed. 27 * 28 * @author Cies Breijs 29 */ 30 class Token 31 { 32 public: 33 /** 34 * This is an enum for the different types a Token can have. 35 * The code for this enum is generated. 36 */ 37 enum Type 38 { 39 Error = -2, // when the Tokenizer finds something it cannot deal with (like a single dot) 40 Unknown = -1, // when the Translator found no translation (like when calling a learned function) 41 NotSet = 0, // when Tokens are constructed without being initialized (needed for ErrorList) 42 43 //BEGIN GENERATED token_type_h CODE 44 45 /* The code between the line that start with "//BEGIN GENERATED" and "//END GENERATED" 46 * is generated by "generate.rb" according to the definitions specified in 47 * "definitions.rb". Please make all changes in the "definitions.rb" file, since all 48 * all change you make here will be overwritten the next time "generate.rb" is run. 49 * Thanks for looking at the code! 50 */ 51 52 Root, 53 Scope, 54 WhiteSpace, 55 EndOfLine, 56 EndOfInput, 57 VariablePrefix, 58 Variable, 59 FunctionCall, 60 String, 61 Number, 62 True, 63 False, 64 Comment, 65 StringDelimiter, 66 ScopeOpen, 67 ScopeClose, 68 ParenthesisOpen, 69 ParenthesisClose, 70 ArgumentSeparator, 71 DecimalSeparator, 72 Exit, 73 If, 74 Else, 75 Repeat, 76 While, 77 For, 78 ForTo, 79 To, 80 Step, 81 Break, 82 Return, 83 Wait, 84 Assert, 85 And, 86 Or, 87 Not, 88 Equals, 89 NotEquals, 90 GreaterThan, 91 LessThan, 92 GreaterOrEquals, 93 LessOrEquals, 94 Addition, 95 Subtraction, 96 Multiplication, 97 Division, 98 Power, 99 Assign, 100 Learn, 101 ArgumentList, 102 Reset, 103 Clear, 104 Center, 105 Go, 106 GoX, 107 GoY, 108 Forward, 109 Backward, 110 Direction, 111 TurnLeft, 112 TurnRight, 113 PenWidth, 114 PenUp, 115 PenDown, 116 PenColor, 117 CanvasColor, 118 CanvasSize, 119 SpriteShow, 120 SpriteHide, 121 Print, 122 FontSize, 123 Random, 124 GetX, 125 GetY, 126 Message, 127 Ask, 128 Pi, 129 Tan, 130 Sin, 131 Cos, 132 ArcTan, 133 ArcSin, 134 ArcCos, 135 Sqrt, 136 Round, 137 GetDirection, 138 Mod 139 140 //END GENERATED token_type_h CODE 141 142 }; 143 144 145 /** 146 * This is an enum for the different categories a Token can belong to. 147 * It is used by the highlighter to know how to highlight the code, 148 * and the mainwindow to determine the 'context help keyword'. 149 * The code for this enum is mostly generated. 150 */ 151 enum Category 152 { 153 UnknownCategory, 154 155 //BEGIN GENERATED token_category_h CODE 156 157 /* The code between the line that start with "//BEGIN GENERATED" and "//END GENERATED" 158 * is generated by "generate.rb" according to the definitions specified in 159 * "definitions.rb". Please make all changes in the "definitions.rb" file, since all 160 * all change you make here will be overwritten the next time "generate.rb" is run. 161 * Thanks for looking at the code! 162 */ 163 164 CommandCategory, 165 ControllerCommandCategory, 166 NumberCategory, 167 ParenthesisCategory, 168 TrueFalseCategory, 169 FunctionCallCategory, 170 ExpressionCategory, 171 ArgumentSeparatorCategory, 172 MathOperatorCategory, 173 CommentCategory, 174 AssignmentCategory, 175 BooleanOperatorCategory, 176 ScopeCategory, 177 VariableCategory, 178 StringCategory, 179 LearnCommandCategory 180 181 //END GENERATED token_category_h CODE 182 183 }; 184 185 186 /** 187 * @short Constructor. 188 * Initialses a empty Token with Token::Type: Token::NotSet. 189 * This default constructor is needed for ErrorList (QValueList). 190 */ 191 Token(); 192 193 /** 194 * @short Constructor. 195 * Initialses a complete Token. 196 * 197 * @param type type of the token, see also the @ref Type enum in this class 198 * @param look the look of the Token as in the unicode string of the tokens 199 * appearance in the KTurtle code. 200 * @param startRow row position of the first character of this token in the code 201 * @param startCol column position of the first character of this token in the code 202 * @param endRow row position of the last character of this token in the code 203 * @param endCol column position of the last character of this token in the code 204 */ 205 Token(int type, const QString& look, int startRow, int startCol, int endRow, int endCol); 206 ~Token()207 virtual ~Token() {} 208 209 look()210 const QString& look() 211 const { return _look; } type()212 int type() const { return _type; } category()213 int category() const { return typeToCategory(_type); } startRow()214 int startRow() const { return _startRow; } startCol()215 int startCol() const { return _startCol; } endRow()216 int endRow() const { return _endRow; } endCol()217 int endCol() const { return _endCol; } 218 setType(int type)219 void setType(int type) { _type = type; } setStartRow(int startRow)220 void setStartRow(int startRow) { _startRow = startRow; } setStartCol(int startCol)221 void setStartCol(int startCol) { _startCol = startCol; } setEndRow(int endRow)222 void setEndRow(int endRow) { _endRow = endRow; } setEndCol(int endCol)223 void setEndCol(int endCol) { _endCol = endCol; } 224 225 /// Compares 2 Tokens. Needed for ErrorList (QValueList) to compare ErrorMessages which contain Tokens. 226 bool operator==(const Token&) const; 227 228 /// Assigns a Token, it needs to compare ErrorMessages which contain Tokens. 229 Token& operator=(const Token&); 230 231 /// returns the category a type belongs to (generated) 232 static int typeToCategory(int); 233 234 235 private: 236 int _type; 237 QString _look; 238 int _startRow, _startCol, _endRow, _endCol; 239 }; 240 241 242 #endif // _TOKEN_H_ 243