1 #ifndef Header_Latex_Tokens
2 #define Header_Latex_Tokens
3 
4 #include "mostQtHeaders.h"
5 
6 class QDocumentLineHandle;
7 class QDocument;
8 
9 /*
10  * \brief Wrapper for the TokenType enum.
11  * \details This class contains all the TokenType enums. It is has two purposes:
12  * 	1. Provide enums for Token class.
13  * 	2. Generate a MOC object that is used to provide these enums to the script engine.
14  * The enums should really be a namespace, but Q_NAMESPACE is only supported in Qt5.8+
15  */
16 class EnumsTokenType
17 {
18 	Q_GADGET
19     Q_ENUMS(TokenType)
20 
21 public:
22 	enum TokenType {
23 		none = 0, word, command, braces, bracket,
24 		squareBracket, openBrace, openBracket, openSquare, less,
25 		closeBrace, closeBracket, closeSquareBracket, greater, math,
26 		comment, commandUnknown, label, bibItem, file,
27 		imagefile, bibfile, keyValArg, keyVal_key, keyVal_val,
28 		list, text, env, beginEnv, def,
29 		labelRef, package, width, placement, colDef,
30 		title, shorttitle, todo, url, documentclass,
31 		beamertheme, packageoption, color, verbatimStart, verbatimStop,
32 		verbatim, symbol, punctuation, number, generalArg,
33 		defArgNumber, optionalArgDefinition, definition, defWidth, labelRefList,
34         formula, specialArg, newTheorem, newBibItem, overlay,
35 		overlayRegion, _end = 255
36 	};
37 };
38 
39 /*!
40  * \brief repesent syntax information on text element
41  * The objective for this class is to translate a text(-line) into a series of tokens which can be interpreted much faster and easier subsequently
42  * The translation process is divided into 2 passes.
43  *
44  * Pass 1 simply determines word limits and symbols
45  * e.g.\n
46  * \\label{abc} def\n
47  * is translated to
48  \verbatim
49  [command 0 6] [openBrace 7 1] [word 8 3] [closeBrace 12 1] [word 14 3]
50  \endverbatim
51  *
52  * no context is used
53  *
54  * Pass 2 interprets the data in order to assign arguments to commands and to assign a purpose for the arguments if they are defined in the cwl
55  * It uses the tokens from the first pass to speed-up processing\n
56  * e.g. (from previous example, level is represented by the line, type/subtype is given)\n
57  \verbatim
58  level=0 [command/none 0 6] [braces/label 7 5]                  [word/none 14 3]
59  level=1                                       [label/none 8 3]
60  \endverbatim
61  The level is encoded via the level-property. The list is actually still linear.
62  */
63 class Token : public EnumsTokenType
64 {
65 public:
Token()66 	Token(): start(-1), length(-1), level(-1), dlh(nullptr), type(none), subtype(none), argLevel(0) {}
67 	int start;
68 	int length;
69 	int level;
70 	QString optionalCommandName;
71 	QDocumentLineHandle *dlh;
72 
73 	enum CommentType {
74 		unknownComment = 0, todoComment, magicComment
75 	};
76 
77 	static QString tokenTypeName(TokenType t);
78 
79 	TokenType type;
80 	/// subtype is used to determine the type of argument
81 	TokenType subtype;
82     bool ignoreSpelling;
83 	int argLevel; ///< number of argument (>0) or option (<0, =-numberOfOption)
84 	static const QHash<TokenType, int> leftDelimWidth;  ///< width of the left delimiter in the token (if applicable)
85 	static const QHash<TokenType, int> rightDelimWidth;  ///< width of the right delimiter in the token (if applicable)
86 	static QSet<TokenType> tkArg();
87 	static QSet<TokenType> tkOption();
88 	static QSet<TokenType> tkBraces();
89 	static QSet<TokenType> tkOpen();
90 	static QSet<TokenType> tkClose();
91 	static QSet<TokenType> tkCommalist();
92 	static QSet<TokenType> tkSingleArg();
93 	static TokenType opposite(TokenType type);
94 	static TokenType closed(TokenType type);
95 	bool operator==(const Token &v) const;
96 	int innerStart() const;
97 	int innerLength() const;
98 	QString getText() const;
99 	QString getInnerText() const;
100 };
101 QDebug operator<<(QDebug dbg, Token::TokenType tk);
102 QDebug operator<<(QDebug dbg, Token tk);
103 
104 typedef QList<Token> TokenList;
105 typedef QStack<Token> TokenStack;
106 
107 Q_DECLARE_METATYPE(Token::TokenType);
108 Q_DECLARE_METATYPE(TokenList);
109 Q_DECLARE_METATYPE(TokenStack);
110 void qDebugTokenList(TokenList tl);
111 
112 #endif // LATEXTOKENS_H
113