KVIrc  4.9.2
DeveloperAPIs
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 
32 class KviKvsScript;
33 class KviKvsKernel;
34 class KviWindow;
35 class KviKvsTreeNode;
40 class KviKvsTreeNodeData;
42 class KviKvsTreeNodeVariableReference;
51 
52 // This is an ONE-TIME parser used by KviKvsScript
53 
55 {
56  friend class KviKvsKernel;
57 
58 public:
59  KviKvsParser(KviKvsScript * pScript, KviWindow * pOutputWindow);
60  ~KviKvsParser();
61 
62 private:
63  const QChar * m_pBuffer; // the local pointer to the beginning of the buffer
64  const QChar * m_ptr; // 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; // the current parsing flags
68  bool m_bError; // 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
72 public: // 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 
87 private: // 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
93  void skipToEndOfCStyleComment();
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 
101 protected:
102  // this is called by KviKvsKernel to register the parsing routines
103  static void init();
104 
105 private:
106  // returns 0 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 0 (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 0 without error
113  // stops after the ending char of the instruction
114  KviKvsTreeNodeInstruction * parseInstruction();
115  // may return 0 (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 0 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 0 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 0 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 0 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 0 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 0
148  KviKvsTreeNodeConstantData * parseCommandLiteralParameter();
149  // never returns 0
150  KviKvsTreeNodeConstantData * parseCommaSeparatedLiteralParameter();
151  // never returns 0
152  KviKvsTreeNodeConstantData * parseSingleLiteralParameterInParenthesis();
153  // returns 0 only in case of error
154  // starts at the leading '"'
155  // ends after the trailing '"'
156  KviKvsTreeNodeData * parseStringParameter();
157  // never returns 0
158  KviKvsTreeNodeConstantData * parseStringLiteralParameter();
159  // returns 0 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 0 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 0 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 0 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 0 only in case of error
177  KviKvsTreeNodeData * parseHashKey();
178  // never returns 0
179  KviKvsTreeNodeConstantData * parseHashKeyLiteralParameter();
180 
181  //
182  // KviKvsParser_specialCommands.cpp
183  //
184 
185  // return 0 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 0
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 0 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 0 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 0 only in case of error
207  // and stops after the end of the break command
208  KviKvsTreeNodeCommand * parseSpecialCommandBreak();
209  // returns 0 only in case of error
210  // and jumps to the next iteration after the end of the continue command
211  KviKvsTreeNodeCommand * parseSpecialCommandContinue();
212  // returns 0 only in case of error
213  // and stops after the end of the for command block
214  KviKvsTreeNodeCommand * parseSpecialCommandFor();
215  // returns 0 only in case of error
216  // and stops after the end of the foreach command block
217  KviKvsTreeNodeCommand * parseSpecialCommandForeach();
218  // returns 0 only in case of error
219  // and stops after the end of the switch command block
220  KviKvsTreeNodeCommand * parseSpecialCommandSwitch();
221  // returns 0 only in case of error
222  // and stops after the end of the defpopup command block
223  KviKvsTreeNodeCommand * parseSpecialCommandUnset();
224  // returns 0 only in case of error
225  // and stops after the end of the defpopup command block
226  KviKvsTreeNodeCommand * parseSpecialCommandDefpopup();
227  KviKvsTreeNodeSpecialCommandDefpopupLabelPopup * parseSpecialCommandDefpopupLabelPopup();
228  // returns 0 only in case of error
229  // stops after the class command block
230  KviKvsTreeNodeCommand * parseSpecialCommandClass();
231  // returns 0 only in case of error
232  // stops after the perl.end statement
233  KviKvsTreeNodeCommand * parseSpecialCommandPerlBegin();
234  // returns 0 only in case of error
235  // stops after the perl.end statement
236  KviKvsTreeNodeCommand * parseSpecialCommandPythonBegin();
237  // returns 0 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 0 (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
249 
250  //
251  // KviKvsParser_comment.cpp
252  //
253 
254  // always returns 0, 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 0 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 0 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 0 only in case of error
278  // returns after the command terminator
279  KviKvsTreeNodeInstruction * parseVoidFunctionCallOrOperation();
280  // returns 0 only in case of error
281  // returns after the command terminator
282  KviKvsTreeNodeOperation * parseOperation();
283  // returns 0 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 0 only in case of error
288  // returns after the command terminator
289  KviKvsTreeNodeOperation * parseBindingOperation();
290  KviKvsTreeNodeConstantData * parseBindingOperationLiteralParameter();
291  KviKvsTreeNodeData * parseBindingOperationParameter();
292 
293  //
294  // KviKvsParser_expression.cpp
295  //
296 
297  // returns 0 only in case of error
298  // starts AFTER the leading char of the expression
299  // ends afer 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
Treenode class.
Definition: KviKvsTreeNodeBase.h:40
KviPointerHashTable< QString, QString > * m_pGlobals
Definition: KviKvsParser.h:66
#define KVIRC_API
Definition: kvi_settings.h:128
The KVIrc Script class.
Definition: KviKvsScript.h:59
int m_iFlags
Definition: KviKvsParser.h:67
bool parseCommand(const QString &szData, KviWindow *pWindow, const QString &szContext, bool bUserFriendlyCommandline)
Returns true if the command run.
Definition: KviUserInput.cpp:82
Definition: KviKvsTreeNodeOperation.h:34
Definition: KviKvsParser.h:54
KviKvsScript * m_pScript
Definition: KviKvsParser.h:70
#define kvi_va_list
Definition: kvi_stdarg.h:31
const QChar * m_pBuffer
Definition: KviKvsParser.h:63
Definition: KviKvsTreeNodeExpression.h:185
Pointer Hash Table.
Definition: KviKvsTreeNodeConstantData.h:31
Definition: KviKvsTreeNodeVariable.h:34
Flags
Definition: KviKvsParser.h:73
Definition: KviKvsKernel.h:69
KviWindow * m_pWindow
Definition: KviKvsParser.h:71
Definition: KviKvsTreeNodeSwitchList.h:38
Definition: KviKvsTreeNodeExpression.h:67
Treenode class to handle instructions.
Definition: KviKvsTreeNodeInstruction.h:42
Definition: KviKvsTreeNodeDataList.h:35
void warning(QString fmt,...)
Definition: KviMessageBox.cpp:41
bool error() const
Definition: KviKvsParser.h:79
Definition: KviKvsTreeNodeSpecialCommand.h:31
Definition: KviKvsTreeNodeInstructionBlock.h:35
C++ Template based double linked pointer list class.
Base class for all windows in KVIrc.
Definition: KviWindow.h:74
Definition: KviKvsTreeNodeCommand.h:34
Definition: KviKvsTreeNodeFunctionCall.h:31
Definition: KviKvsTreeNodeData.h:34
Definition: KviKvsTreeNodeSpecialCommandDefpopup.h:172
int init()
Definition: winamp.cpp:118
This file contains compile time settings.
bool m_bError
Definition: KviKvsParser.h:68
KVIRC_API bool parse(const char *url, KviCString &cmdBuffer, int contextSpec=KVI_IRCURL_CONTEXT_FIRSTFREE)
Definition: KviIrcUrl.cpp:46
const QChar * m_ptr
Definition: KviKvsParser.h:64
Helper functions for the QString class.