File indexing completed on 2024-12-08 03:32:04
0001 /* 0002 SPDX-FileCopyrightText: 2003-2009 Cies Breijs <cies AT kde DOT nl> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 0008 #ifndef _TOKEN_H_ 0009 #define _TOKEN_H_ 0010 0011 #include <QString> 0012 0013 0014 /** 0015 * @short The Token object, represents a piece of TurtleScript as found by the Tokenizer. 0016 * 0017 * Because of the goals of KTurtle it this class very elaborate. Much info 0018 * about each token is kept so descriptive error messages can be printed, 0019 * and proper highlighting and context help made possible. 0020 * 0021 * Tokens are made by the Tokenizer according to the TurtleScript, then they are stored 0022 * in the node tree by the Parser or used by the Highlighter of for context help. 0023 * 0024 * A large potion of the code of this class (the Type enum) is generated code. 0025 * 0026 * @TODO investigate if it will be better to replace this class by a struct for speed. 0027 * 0028 * @author Cies Breijs 0029 */ 0030 class Token 0031 { 0032 public: 0033 /** 0034 * This is an enum for the different types a Token can have. 0035 * The code for this enum is generated. 0036 */ 0037 enum Type 0038 { 0039 Error = -2, // when the Tokenizer finds something it cannot deal with (like a single dot) 0040 Unknown = -1, // when the Translator found no translation (like when calling a learned function) 0041 NotSet = 0, // when Tokens are constructed without being initialized (needed for ErrorList) 0042 0043 //BEGIN GENERATED token_type_h CODE 0044 0045 /* The code between the line that start with "//BEGIN GENERATED" and "//END GENERATED" 0046 * is generated by "generate.rb" according to the definitions specified in 0047 * "definitions.rb". Please make all changes in the "definitions.rb" file, since all 0048 * all change you make here will be overwritten the next time "generate.rb" is run. 0049 * Thanks for looking at the code! 0050 */ 0051 0052 Root, 0053 Scope, 0054 WhiteSpace, 0055 EndOfLine, 0056 EndOfInput, 0057 VariablePrefix, 0058 Variable, 0059 FunctionCall, 0060 String, 0061 Number, 0062 True, 0063 False, 0064 Comment, 0065 StringDelimiter, 0066 ScopeOpen, 0067 ScopeClose, 0068 ParenthesisOpen, 0069 ParenthesisClose, 0070 ArgumentSeparator, 0071 DecimalSeparator, 0072 Exit, 0073 If, 0074 Else, 0075 Repeat, 0076 While, 0077 For, 0078 ForTo, 0079 To, 0080 Step, 0081 Break, 0082 Return, 0083 Wait, 0084 Assert, 0085 And, 0086 Or, 0087 Not, 0088 Equals, 0089 NotEquals, 0090 GreaterThan, 0091 LessThan, 0092 GreaterOrEquals, 0093 LessOrEquals, 0094 Addition, 0095 Subtraction, 0096 Multiplication, 0097 Division, 0098 Power, 0099 Assign, 0100 Learn, 0101 ArgumentList, 0102 Reset, 0103 Clear, 0104 Center, 0105 Go, 0106 GoX, 0107 GoY, 0108 Forward, 0109 Backward, 0110 Direction, 0111 TurnLeft, 0112 TurnRight, 0113 PenWidth, 0114 PenUp, 0115 PenDown, 0116 PenColor, 0117 CanvasColor, 0118 CanvasSize, 0119 SpriteShow, 0120 SpriteHide, 0121 Print, 0122 FontSize, 0123 Random, 0124 GetX, 0125 GetY, 0126 Message, 0127 Ask, 0128 Pi, 0129 Tan, 0130 Sin, 0131 Cos, 0132 ArcTan, 0133 ArcSin, 0134 ArcCos, 0135 Sqrt, 0136 Round, 0137 GetDirection, 0138 Mod 0139 0140 //END GENERATED token_type_h CODE 0141 0142 }; 0143 0144 0145 /** 0146 * This is an enum for the different categories a Token can belong to. 0147 * It is used by the highlighter to know how to highlight the code, 0148 * and the mainwindow to determine the 'context help keyword'. 0149 * The code for this enum is mostly generated. 0150 */ 0151 enum Category 0152 { 0153 UnknownCategory, 0154 0155 //BEGIN GENERATED token_category_h CODE 0156 0157 /* The code between the line that start with "//BEGIN GENERATED" and "//END GENERATED" 0158 * is generated by "generate.rb" according to the definitions specified in 0159 * "definitions.rb". Please make all changes in the "definitions.rb" file, since all 0160 * all change you make here will be overwritten the next time "generate.rb" is run. 0161 * Thanks for looking at the code! 0162 */ 0163 0164 CommandCategory, 0165 ControllerCommandCategory, 0166 NumberCategory, 0167 ParenthesisCategory, 0168 TrueFalseCategory, 0169 FunctionCallCategory, 0170 ExpressionCategory, 0171 ArgumentSeparatorCategory, 0172 MathOperatorCategory, 0173 CommentCategory, 0174 AssignmentCategory, 0175 BooleanOperatorCategory, 0176 ScopeCategory, 0177 VariableCategory, 0178 StringCategory, 0179 LearnCommandCategory 0180 0181 //END GENERATED token_category_h CODE 0182 0183 }; 0184 0185 0186 /** 0187 * @short Constructor. 0188 * Initialses a empty Token with Token::Type: Token::NotSet. 0189 * This default constructor is needed for ErrorList (QValueList). 0190 */ 0191 Token(); 0192 0193 /** 0194 * @short Constructor. 0195 * Initialses a complete Token. 0196 * 0197 * @param type type of the token, see also the @ref Type enum in this class 0198 * @param look the look of the Token as in the unicode string of the tokens 0199 * appearance in the KTurtle code. 0200 * @param startRow row position of the first character of this token in the code 0201 * @param startCol column position of the first character of this token in the code 0202 * @param endRow row position of the last character of this token in the code 0203 * @param endCol column position of the last character of this token in the code 0204 */ 0205 Token(int type, const QString& look, int startRow, int startCol, int endRow, int endCol); 0206 0207 virtual ~Token() {} 0208 0209 0210 const QString& look() 0211 const { return _look; } 0212 int type() const { return _type; } 0213 int category() const { return typeToCategory(_type); } 0214 int startRow() const { return _startRow; } 0215 int startCol() const { return _startCol; } 0216 int endRow() const { return _endRow; } 0217 int endCol() const { return _endCol; } 0218 0219 void setType(int type) { _type = type; } 0220 void setStartRow(int startRow) { _startRow = startRow; } 0221 void setStartCol(int startCol) { _startCol = startCol; } 0222 void setEndRow(int endRow) { _endRow = endRow; } 0223 void setEndCol(int endCol) { _endCol = endCol; } 0224 0225 /// Compares 2 Tokens. Needed for ErrorList (QValueList) to compare ErrorMessages which contain Tokens. 0226 bool operator==(const Token&) const; 0227 0228 /// Assigns a Token, it needs to compare ErrorMessages which contain Tokens. 0229 Token& operator=(const Token&) = default; 0230 Token(const Token&) = default; 0231 0232 /// returns the category a type belongs to (generated) 0233 static int typeToCategory(int); 0234 0235 0236 private: 0237 int _type; 0238 QString _look; 0239 int _startRow, _startCol, _endRow, _endCol; 0240 }; 0241 0242 0243 #endif // _TOKEN_H_