00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #ifndef TOSQLPARSE_H
00047 #define TOSQLPARSE_H
00048
00049
00050
00051 #include <list>
00052 #include <algorithm>
00053
00054 #include <QString>
00055
00056
00057
00058
00059 class SqlEditorWidget;
00060
00064 class toSQLParse
00065 {
00066 public:
00067 struct settings
00068 {
00069 bool ExpandSpaces;
00070 bool CommaBefore;
00071 bool BlockOpenLine;
00072 bool OperatorSpace;
00073 bool KeywordUpper;
00074 bool RightSeparator;
00075 bool EndBlockNewline;
00076 int IndentLevel;
00077 int CommentColumn;
00078 };
00079
00082 class statement
00083 {
00086 std::list<statement> *SubTokens;
00087 public:
00090 enum type
00091 {
00094 Block,
00097 Statement,
00100 List,
00103 Keyword,
00106 Token,
00109 Raw
00112 } Type;
00115 QString Comment;
00118 QString String;
00121 int Line;
00124 statement(type ntype = Token, const QString &token = QString::null, int cline = -1);
00127 std::list<statement> &subTokens();
00130 statement(const statement &);
00133 const statement &operator = (const statement &);
00136 bool operator == (const statement &) const;
00139 bool operator != (const statement &stat) const
00140 {
00141 return !((*this) == stat);
00142 }
00145 ~statement();
00146 };
00147
00150 class tokenizer
00151 {
00152 private:
00153
00154 protected:
00155 int Offset;
00156 int Line;
00157 public:
00160 tokenizer(int offset = 0, int line = 0)
00161 {
00162 Line = line;
00163 Offset = offset;
00164
00165 }
00168
00169
00170
00171
00172
00173
00174 virtual ~tokenizer()
00175 { }
00180 virtual QString getToken(bool forward = true, bool comment = false) = 0;
00184
00185
00186
00187
00188
00191 virtual int line(void)
00192 {
00193 return Line;
00194 }
00197 virtual int offset(void)
00198 {
00199 return Offset;
00200 }
00203 virtual void setOffset(int offset)
00204 {
00205 Offset = offset;
00206 }
00210 virtual void setLine(int line)
00211 {
00212 Line = line;
00213 }
00217 virtual QString remaining(bool eol) = 0;
00218 };
00219
00222 class stringTokenizer : public tokenizer
00223 {
00224 QString String;
00225 public:
00226 stringTokenizer(const QString &str, int offset = 0, int line = 0)
00227 : tokenizer(offset, line)
00228 {
00229 String = str;
00230 }
00235 virtual QString getToken(bool forward = true, bool comment = false);
00239 virtual QString remaining(bool eol);
00240 };
00241
00244 class editorTokenizer : public tokenizer
00245 {
00246 SqlEditorWidget *Editor;
00247 public:
00253 editorTokenizer(SqlEditorWidget *editor, int offset = 0, int line = 0);
00258 virtual QString getToken(bool forward = true, bool comment = false);
00262 virtual QString remaining(bool eol);
00263 };
00264
00269 static std::list<statement> parse(tokenizer &tokens);
00274 static std::list<statement> parse(const QString &str)
00275 {
00276 stringTokenizer tokens(str);
00277 return parse(tokens);
00278 }
00279
00284 static statement parseStatement(tokenizer &tokens);
00289 static statement parseStatement(const QString &str)
00290 {
00291 stringTokenizer tokens(str);
00292 return parseStatement(tokens);
00293 }
00294
00299 static QString indent(const QString &str);
00306 static QString indentStatement(statement &stat, int level = 0);
00307
00311 static QString indentString(int level);
00312
00317 static QString indent(std::list<statement> &stat);
00318
00323 static int countIndent(const QString &str, int &chars);
00329
00335
00336 private:
00337 static settings Settings;
00338 static statement parseStatement(tokenizer &tokens,
00339 bool declare, bool lst);
00346
00347 public:
00350 static settings getSetting(void)
00351 {
00352 return Settings;
00353 }
00356 static void setSetting(const settings &setting)
00357 {
00358 Settings = setting;
00359 }
00360 };
00361
00362 #endif