forked from ahmidou/SpliceAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KLParserImpl.h
326 lines (289 loc) · 10.8 KB
/
KLParserImpl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2010-2013 Fabric Engine Inc. All rights reserved.
#ifndef __FabricSpliceImpl_KLPARSERIMPL_H__
#define __FabricSpliceImpl_KLPARSERIMPL_H__
#include <string>
#include <vector>
#include <map>
#include <boost/shared_ptr.hpp>
namespace FabricSpliceImpl
{
class KLParserImpl;
typedef boost::shared_ptr<KLParserImpl> KLParserImplPtr;
class KLParserImpl
{
public:
class KLSymbol
{
friend class KLParserImpl;
friend class KLConstant;
public:
enum Type {
Type_none,
Type_separator,
Type_semicolon,
Type_comma,
Type_period,
Type_comment,
Type_firstkeyword,
Type_in,
Type_io,
Type_if,
Type_else,
Type_switch,
Type_case,
Type_default,
Type_for,
Type_while,
Type_do,
Type_break,
Type_continue,
Type_this,
Type_alias,
Type_require,
Type_return,
Type_const,
Type_null,
Type_inline,
Type_function,
Type_operator,
Type_struct,
Type_object,
Type_interface,
Type_lastkeyword,
Type_rt,
Type_name,
Type_number,
Type_string,
Type_assignment,
Type_arithmetic,
Type_brace1,
Type_brace2,
Type_bracket1,
Type_bracket2,
Type_curly1,
Type_curly2,
Type_pex1,
Type_pex2,
Type_maxtypes
};
unsigned int index() const;
unsigned int pos() const { return mPos; }
unsigned int length() const { return mLength; }
Type type() const { return mType; }
bool isKeyword() const;
bool isFunction() const;
const char * typeName() const;
static const char * typeNameFromType(Type index);
char front(unsigned int index = 0) const;
char back(unsigned int index = 0) const;
std::string str() const;
bool contains(char c) const;
unsigned int find(char c) const;
const KLSymbol * prev(bool skipComments = true, unsigned int offset = 1) const;
const KLSymbol * next(bool skipComments = true, unsigned int offset = 1) const;
const char * parser() const;
private:
KLSymbol(const KLParserImpl * parser, const std::string * ptr, unsigned int pos);
void setType(Type t) { mType = t; }
bool merge(const KLSymbol & other);
const KLParserImpl * mParser;
const char * mPtr;
const std::string * mStr;
unsigned int mPos;
unsigned int mLength;
Type mType;
};
class KLConstant
{
friend class KLParserImpl;
public:
const KLSymbol * symbol() const { return mKLSymbol; }
const char * comments() const { return mComments.c_str(); }
const char * type() const { return mType.c_str(); }
const char * name() const { return mName.c_str(); }
const char * value() const { return mValue.c_str(); }
private:
KLConstant(const KLSymbol *& s);
bool isValid() const { return mType.length() > 0 && mName.length() > 0 && mValue.length() > 0; }
const KLSymbol * mKLSymbol;
std::string mComments;
std::string mType;
std::string mName;
std::string mValue;
};
class KLVariable
{
friend class KLParserImpl;
public:
const KLSymbol * symbol() const { return mKLSymbol; }
const char * type() const { return mType.c_str(); }
const char * name() const { return mName.c_str(); }
private:
KLVariable(const KLSymbol * s, const KLSymbol * t);
bool isValid() const { return mType.length() > 0 && mName.length(); }
const KLSymbol * mKLSymbol;
std::string mType;
std::string mName;
};
class KLStruct
{
friend class KLParserImpl;
public:
const KLSymbol * symbol() const { return mKLSymbol; }
const char * comments() const { return mComments.c_str(); }
const char * type() const { return mType.c_str(); }
const char * name() const { return mName.c_str(); }
unsigned int nbInterfaces() const { return mInterfaces.size(); }
const char * interface(unsigned int index) const;
unsigned int nbMembers() const { return mMemberNames.size(); }
const char * memberType(unsigned int index) const;
const char * memberName(unsigned int index) const;
private:
KLStruct(const KLSymbol *& s);
bool isValid() const { return mName.length() > 0 && mMemberNames.size(); }
const KLSymbol * mKLSymbol;
std::string mComments;
std::string mType;
std::string mName;
std::vector<std::string> mInterfaces;
std::vector<std::string> mMemberTypes;
std::vector<std::string> mMemberNames;
};
class KLArgumentList
{
friend class KLOperator;
friend class KLFunction;
friend class KLParserImpl;
public:
unsigned int nbArgs() const { return mModes.size(); }
const char * mode(unsigned int index) const;
const char * type(unsigned int index) const;
const char * name(unsigned int index) const;
private:
KLArgumentList() {}
KLArgumentList(const KLSymbol *& s);
bool isValid() const { return mModes.size(); }
std::vector<std::string> mModes;
std::vector<std::string> mTypes;
std::vector<std::string> mNames;
};
class KLOperator
{
friend class KLParserImpl;
public:
const KLSymbol * symbol() const { return mKLSymbol; }
const char * comments() const { return mComments.c_str(); }
const char * name() const { return mName.c_str(); }
bool isPex() const { return mPexArg.length() > 0; }
const char * pexArgument() const { return mPexArg.c_str(); }
const KLArgumentList * arguments() const { return &mArguments; }
const KLSymbol * bodyStart() const { return mBody1; }
const KLSymbol * bodyEnd() const { return mBody2; }
private:
KLOperator(const KLSymbol *& s);
bool isValid() const { return mName.length(); }
const KLSymbol * mKLSymbol;
std::string mComments;
std::string mName;
std::string mPexArg;
KLArgumentList mArguments;
const KLSymbol * mBody1;
const KLSymbol * mBody2;
};
class KLFunction
{
friend class KLParserImpl;
public:
const KLSymbol * symbol() const { return mKLSymbol; }
const char * comments() const { return mComments.c_str(); }
const char * type() const { return mType.c_str(); }
const char * owner() const { return mOwner.c_str(); }
const char * name() const { return mName.c_str(); }
const KLArgumentList * arguments() const { return &mArguments; }
const KLSymbol * bodyStart() const { return mBody1; }
const KLSymbol * bodyEnd() const { return mBody2; }
private:
KLFunction(const KLSymbol *& s, bool requireFunctionKeyWord);
bool isValid() const { return mName.length(); }
const KLSymbol * mKLSymbol;
std::string mComments;
std::string mType;
std::string mOwner;
std::string mName;
KLArgumentList mArguments;
const KLSymbol * mBody1;
const KLSymbol * mBody2;
};
class KLInterface
{
friend class KLParserImpl;
public:
const KLSymbol * symbol() const { return mKLSymbol; }
const char * comments() const { return mComments.c_str(); }
const char * name() const { return mName.c_str(); }
unsigned int nbFunctions() const { return mFunctions.size(); }
const KLFunction * function(unsigned int index) const;
private:
KLInterface(const KLSymbol *& s);
bool isValid() const { return mName.length() > 0 && mFunctions.size(); }
const KLSymbol * mKLSymbol;
std::string mComments;
std::string mName;
std::vector<KLFunction> mFunctions;
};
~KLParserImpl();
const char * owner() const { return mOwner.c_str(); }
const char * name() const { return mName.c_str(); }
const char * code() const { return mStr.c_str(); }
static unsigned int getNbParsers();
static KLParserImplPtr getParser(unsigned int index);
static KLParserImplPtr getParser(const char * owner, const char * name, const char * klCode);
static void resetAll();
// update parsed code
bool parse(const char * klCode);
// basic accessors
unsigned int nbKLSymbols() const { return (unsigned int)mKLSymbols.size(); }
const KLSymbol * getKLSymbol(unsigned int symbolIndex) const;
const KLSymbol * getKLSymbolFromCharIndex(unsigned int charIndex) const;
bool getNextKLSymbol(const KLSymbol *& symbol, bool skipComments = true, unsigned int offset = 1) const;
bool getPrevKLSymbol(const KLSymbol *& symbol, bool skipComments = true, unsigned int offset = 1) const;
bool getNextKLSymbolOfType(const KLSymbol *& symbol, KLSymbol::Type type) const;
bool getPrevKLSymbolOfType(const KLSymbol *& symbol, KLSymbol::Type type) const;
// access to parsed symbols
unsigned int getNbKLRequires() const { return (unsigned int)mParsedRequires.size(); }
const char * getKLRequire(unsigned int index) const;
unsigned int getNbKLConstants() const { return (unsigned int)mParsedConstants.size(); }
const KLConstant * getKLConstant(unsigned int index) const;
unsigned int getNbKLVariables() const { return (unsigned int)mParsedVariables.size(); }
const KLVariable * getKLVariable(unsigned int index) const;
unsigned int getNbKLInterfaces() const { return (unsigned int)mParsedInterfaces.size(); }
const KLInterface * getKLInterface(unsigned int index) const;
unsigned int getNbKLStructs() const { return (unsigned int)mParsedStructs.size(); }
const KLStruct * getKLStruct(unsigned int index) const;
unsigned int getNbKLOperators() const { return (unsigned int)mParsedOperators.size(); }
const KLOperator * getKLOperator(unsigned int index) const;
unsigned int getNbKLFunctions() const { return (unsigned int)mParsedFunctions.size(); }
const KLFunction * getKLFunction(unsigned int index) const;
// code completion functions
const char * getKLTypeForSymbol(const KLSymbol * s) const;
static const char * getKLTypeForMemberOrMethod(const std::string & owner, const std::string & member);
private:
KLParserImpl(const char * owner, const char * name, const char * klCode);
unsigned int getIndexOfKLSymbol(const KLSymbol * symbol) const;
std::string getCommentsPriorToKLSymbol(const KLSymbol * s) const;
std::string mOwner;
std::string mName;
const char * mPtr;
std::string mStr;
std::vector<KLSymbol> mKLSymbols;
std::vector<std::string> mParsedRequires;
std::vector<KLConstant> mParsedConstants;
std::vector<KLVariable> mParsedVariables;
std::vector<KLInterface> mParsedInterfaces;
std::vector<KLStruct> mParsedStructs;
std::vector<KLOperator> mParsedOperators;
std::vector<KLFunction> mParsedFunctions;
static std::map<std::string, KLParserImplPtr> sParsers;
};
};
#endif