KVIrc 5.2.0
Developer APIs
KviKvsParser.h
Go to the documentation of this file.
1#ifndef _KVI_KVS_PARSER_H_
2#define _KVI_KVS_PARSER_H_
3//=============================================================================
4//
5// File : KviKvsParser.h
6// Creation date : Thu 25 Sep 2003 05.12 CEST by Szymon Stefanek
7//
8// This file is part of the KVIrc IRC client distribution
9// Copyright (C) 2003-2010 Szymon Stefanek (pragma at kvirc dot net)
10//
11// This program is FREE software. You can redistribute it and/or
12// modify it under the terms of the GNU General Public License
13// as published by the Free Software Foundation; either version 2
14// of the License, or (at your option) any later version.
15//
16// This program is distributed in the HOPE that it will be USEFUL,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19// See the GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program. If not, write to the Free Software Foundation,
23// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24//
25//=============================================================================
26
27#include "kvi_settings.h"
28#include "KviQString.h"
29#include "KviPointerList.h"
30#include "KviPointerHashTable.h"
31
32class KviKvsScript;
33class KviKvsKernel;
34class KviWindow;
35class KviKvsTreeNode;
42class KviKvsTreeNodeVariableReference;
51
52// This is an ONE-TIME parser used by KviKvsScript
53
55{
56 friend class KviKvsKernel;
57
58public:
59 KviKvsParser(KviKvsScript * pScript, KviWindow * pOutputWindow);
61
62private:
63 const QChar * m_pBuffer = nullptr; // the local pointer to the beginning of the buffer
64 const QChar * m_ptr = nullptr; // the parsing pointer
65 // parsing state
66 KviPointerHashTable<QString, QString> * m_pGlobals; // the dict of the vars declared with global in this script
67 int m_iFlags = 0; // the current parsing flags
68 bool m_bError = false; // error(..) was called ?
69 // this stuff is used only for reporting errors and warnings
70 KviKvsScript * m_pScript; // parent script
71 KviWindow * m_pWindow; // output window
72public: // public interface
73 enum Flags
74 {
75 AssumeLocals = 1,
76 Pedantic = 2
77 };
78 // was there an error ?
79 bool error() const { return m_bError; };
80 // parses the buffer pointed by pBuffer and returns
81 // a syntax tree or 0 in case of failure
82 // if the parsing fails, the error code can be retrieved by calling error()
83 KviKvsTreeNodeInstruction * parse(const QChar * pBuffer, int iFlags = 0);
84 KviKvsTreeNodeInstruction * parseAsExpression(const QChar * pBuffer, int iFlags = 0);
85 KviKvsTreeNodeInstruction * parseAsParameter(const QChar * pBuffer, int iFlags = 0);
86
87private: // parsing helpers
88 // generic
89 void skipSpaces(); // skips only spaces and tabs (eventually with \‍)
90 bool skipSpacesAndNewlines(); // skips space and newlines
91 void skipToNextLine(); // skips up to a new line
92 // dedicated
94 // dedicated to parseSpecialCommandFor() : in KviKvsParser_specialCommands.cpp
95 bool skipToEndOfForControlBlock();
96 // error handlers
97 void error(const QChar * pLocation, QString szMsgFmt, ...);
98 void warning(const QChar * pLocation, QString szMsgFmt, ...);
99 void errorBadChar(const QChar * pLocation, char cExpected, const char * szCommandName);
100
101protected:
102 // this is called by KviKvsKernel to register the parsing routines
103 static void init();
104
105private:
106 // returns nullptr only in case of error
107 // starts on the first char of a buffer
108 // stops at the first null char encountered
109 KviKvsTreeNodeInstruction * parseInstructionList();
110 // may return nullptr (empty instruction), check error() for error conditions
111 // starts on the first character of an instruction
112 // if the first char is ';' '\n' or null it just returns nullptr without error
113 // stops after the ending char of the instruction
114 KviKvsTreeNodeInstruction * parseInstruction();
115 // may return nullptr (empty block), check error() for error conditions
116 // starts at the leading '{' of the block
117 // stops after the trailing '}' of the block
118 KviKvsTreeNodeInstruction * parseInstructionBlock();
119 // returns nullptr only in case of error
120 // starts on the first character of the parameters
121 // ends after the end of the command
122 KviKvsTreeNodeDataList * parseCommandParameterList();
123 // returns nullptr only in case of error
124 // starts on the leading '(' or a ',' in the middle of the list
125 // ends after the trailing ')'
126 // if started in the middle of the list returns only the remaining
127 // parameters.
128 KviKvsTreeNodeDataList * parseCommaSeparatedParameterList();
129 KviPointerList<QString> * parseCommaSeparatedParameterListNoTree();
130 // returns nullptr in case of error or if it starts on a terminating character (null parameter)
131 // check error() to see if there was an error condition (unless you already know that
132 // there was a valid first character)
133 // start on the first character of the parameter
134 // ends after the first character not included in the param (';','\n','\0',' ')
135 // If bPreferNumeric is true then when a single literal parameter
136 // is extracted an attempt to convert it to a numeric format is made.
137 // This optimizes assignments, self-sums etc...
138 KviKvsTreeNodeData * parseCommandParameter(bool bPreferNumeric = false);
139 // returns nullptr only in case of error
140 // start on the first character of the parameter
141 // ends after the first character not included in the param (')','\n','\0',',')
142 KviKvsTreeNodeData * parseCommaSeparatedParameter();
143 // returns nullptr only in case of error
144 // start on the first character of the parameter
145 // ends after the first character not included in the param (')','\n','\0')
146 KviKvsTreeNodeData * parseSingleParameterInParenthesis();
147 // never returns nullptr
149 // never returns nullptr
151 // never returns nullptr
153 // returns nullptr only in case of error
154 // starts at the leading '"'
155 // ends after the trailing '"'
156 KviKvsTreeNodeData * parseStringParameter();
157 // never returns nullptr
159 // returns nullptr in case of error or of an empty switch list (check the error code!)
160 // starts at the leading '-' of the first switch
161 // ends after the last switch
162 KviKvsTreeNodeSwitchList * parseCommandSwitchList();
163 // returns nullptr only in case of error
164 // starts at '%' or '$'
165 // and ends after the end of the data reference
166 // or just after the '%' or '$' if this was only a ConstandData (not a var or func)
167 KviKvsTreeNodeData * parseParameterPercentOrDollar();
168 // returns nullptr only in case of error
169 // starts at '%' or '$'
170 // ends after the end of the complete data reference (including scope operators!)
171 KviKvsTreeNodeData * parsePercentOrDollar(bool bInObjScope = false);
172 // returns nullptr only in case of error
173 // starts at '%'
174 // ends after the end of the structured data
175 KviKvsTreeNodeVariable * parsePercent(bool bInObjectScope = false);
176 // returns nullptr only in case of error
178 // never returns nullptr
180
181 //
182 // KviKvsParser_specialCommands.cpp
183 //
184
185 // return nullptr only in case of error
186 // starts at the leading '(' of the if command (after the switches)
187 // and stops after the end of the else block
188 // if the first character is not '(' then this function fails with an error
189 KviKvsTreeNodeCommand * parseSpecialCommandIf();
190 // always returns nullptr
191 // check error() for error conditions
192 // starts after the switches of the "global" keyword
193 // and stops at the end of the command
194 // if the first character is not '%' of a variable then this function fails with an error
195 KviKvsTreeNodeCommand * parseSpecialCommandGlobal();
196 // returns nullptr only in case of error
197 // starts at the leading '(' of the while command (after the switches)
198 // and stops after the end of the command block
199 // if the first character is not '(' then this function fails with an error
200 KviKvsTreeNodeCommand * parseSpecialCommandWhile();
201 // returns nullptr only in case of error
202 // starts at the leading '(' of the while command (after the switches)
203 // and stops after the end of the command block
204 // if the first character is not '(' then this function fails with an error
205 KviKvsTreeNodeCommand * parseSpecialCommandDo();
206 // returns nullptr only in case of error
207 // and stops after the end of the break command
208 KviKvsTreeNodeCommand * parseSpecialCommandBreak();
209 // returns nullptr only in case of error
210 // and jumps to the next iteration after the end of the continue command
211 KviKvsTreeNodeCommand * parseSpecialCommandContinue();
212 // returns nullptr only in case of error
213 // and stops after the end of the for command block
214 KviKvsTreeNodeCommand * parseSpecialCommandFor();
215 // returns nullptr only in case of error
216 // and stops after the end of the foreach command block
217 KviKvsTreeNodeCommand * parseSpecialCommandForeach();
218 // returns nullptr only in case of error
219 // and stops after the end of the switch command block
220 KviKvsTreeNodeCommand * parseSpecialCommandSwitch();
221 // returns nullptr only in case of error
222 // and stops after the end of the defpopup command block
223 KviKvsTreeNodeCommand * parseSpecialCommandUnset();
224 // returns nullptr only in case of error
225 // and stops after the end of the defpopup command block
226 KviKvsTreeNodeCommand * parseSpecialCommandDefpopup();
227 KviKvsTreeNodeSpecialCommandDefpopupLabelPopup * parseSpecialCommandDefpopupLabelPopup();
228 // returns nullptr only in case of error
229 // stops after the class command block
230 KviKvsTreeNodeCommand * parseSpecialCommandClass();
231 // returns nullptr only in case of error
232 // stops after the perl.end statement
234 // returns nullptr only in case of error
235 // stops after the perl.end statement
237 // returns nullptr only in case of error
238 // and stops after the end of the help command
239 KviKvsTreeNodeCommand * parseSpecialCommandHelp();
240
241 //
242 // KviKvsParser_command.cpp
243 //
244
245 // may return nullptr (empty command), check error() for error conditions
246 // starts at the beginning of a command (can be non valid)
247 // ends after the ending char of the command
248 KviKvsTreeNodeCommand * parseCommand();
249
250 //
251 // KviKvsParser_comment.cpp
252 //
253
254 // always returns nullptr, and it CAN be an error!
255 // starts at the beginning of a comment (must be '#' or '/')
256 // ends after the ending char of the comment
257 KviKvsTreeNode * parseComment();
258
259 //
260 // KviKvsParser_dollar.cpp
261 //
262
263 // returns nullptr only in case of error
264 // starts at '$'
265 // ends after the end of the function call
266 KviKvsTreeNodeData * parseDollar(bool bInObjScope = false);
267
268 // returns nullptr only in case of error
269 // starts at '@'
270 // ends after the end of the function call
271 KviKvsTreeNodeData * parseAt(bool bInObjScope = false);
272
273 //
274 // KviKvsParser_lside.cpp
275 //
276
277 // returns nullptr only in case of error
278 // returns after the command terminator
279 KviKvsTreeNodeInstruction * parseVoidFunctionCallOrOperation();
280 // returns nullptr only in case of error
281 // returns after the command terminator
282 KviKvsTreeNodeOperation * parseOperation();
283 // returns nullptr only in case of error
284 // returns after the command terminator
285 // If bPreferNumeric is propagated to parseCommandParameter() function
286 KviKvsTreeNodeData * parseOperationRightSide(bool bPreferNumeric = false);
287 // return nullptr only in case of error
288 // returns after the command terminator
289 KviKvsTreeNodeOperation * parseBindingOperation();
291 KviKvsTreeNodeData * parseBindingOperationParameter();
292
293 //
294 // KviKvsParser_expression.cpp
295 //
296
297 // returns nullptr only in case of error
298 // starts AFTER the leading char of the expression
299 // ends after the first terminator found
300 KviKvsTreeNodeExpression * parseExpression(char terminator);
301 KviKvsTreeNodeExpressionBinaryOperator * parseExpressionBinaryOperator();
302 KviKvsTreeNodeExpression * parseExpressionOperand(char terminator);
303 KviKvsTreeNodeExpression * parseExpressionOperandCore(char terminator);
304 bool parseExpressionMightPointToOperator();
305
306 void report(bool bError, const QChar * pLocation, const QString & szMsgFmt, kvi_va_list va);
307};
308
309#endif
Pointer Hash Table.
C++ Template based double linked pointer list class.
Helper functions for the QString class.
m_pBuffer
Definition KvsObject_memoryBuffer.cpp:73
Definition KviKvsKernel.h:71
Definition KviKvsParser.h:55
KviKvsTreeNodeCommand * parseSpecialCommandPythonBegin()
KviKvsTreeNodeData * parseHashKey()
KviKvsTreeNodeConstantData * parseCommaSeparatedLiteralParameter()
void skipToEndOfCStyleComment()
KviKvsTreeNodeConstantData * parseCommandLiteralParameter()
bool error() const
Definition KviKvsParser.h:79
KviKvsTreeNodeData * parseAt(bool bInObjScope=false)
KviPointerHashTable< QString, QString > * m_pGlobals
Definition KviKvsParser.h:66
KviWindow * m_pWindow
Definition KviKvsParser.h:71
KviKvsTreeNodeCommand * parseSpecialCommandPerlBegin()
KviKvsTreeNodeConstantData * parseHashKeyLiteralParameter()
KviKvsTreeNodeConstantData * parseStringLiteralParameter()
Flags
Definition KviKvsParser.h:74
KviKvsScript * m_pScript
Definition KviKvsParser.h:70
KviKvsTreeNodeConstantData * parseSingleLiteralParameterInParenthesis()
KviKvsTreeNodeConstantData * parseBindingOperationLiteralParameter()
The KVIrc Script class.
Definition KviKvsScript.h:60
Definition KviKvsTreeNodeCommand.h:35
Definition KviKvsTreeNodeConstantData.h:32
Definition KviKvsTreeNodeDataList.h:36
Definition KviKvsTreeNodeData.h:35
Definition KviKvsTreeNodeExpression.h:186
Definition KviKvsTreeNodeExpression.h:68
Definition KviKvsTreeNodeFunctionCall.h:32
Definition KviKvsTreeNodeInstructionBlock.h:36
Treenode class to handle instructions.
Definition KviKvsTreeNodeInstruction.h:43
Definition KviKvsTreeNodeOperation.h:35
Definition KviKvsTreeNodeSpecialCommandDefpopup.h:173
Definition KviKvsTreeNodeSpecialCommand.h:32
Definition KviKvsTreeNodeSwitchList.h:39
Definition KviKvsTreeNodeVariable.h:35
Treenode class.
Definition KviKvsTreeNodeBase.h:41
A fast pointer hash table implementation.
Definition KviPointerHashTable.h:450
A template double linked list of pointers.
Definition KviPointerList.h:371
Base class for all windows in KVIrc.
Definition KviWindow.h:75
This file contains compile time settings.
#define KVIRC_API
Definition kvi_settings.h:127
#define kvi_va_list
Definition kvi_stdarg.h:30
int init()
Definition winamp.cpp:118