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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
/* This file is part of kdev-pg-qt
Copyright (C) 2005 Roberto Raggi <roberto@kdevelop.org>
Copyright (C) 2006 Jakob Petsovits <jpetso@gmx.at>
Copyright (C) 2010 Jonathan Schmidt-Dominé <devel@the-user.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef KDEV_PG_AST_H
#define KDEV_PG_AST_H
#include "kdev-pg-allocator.h"
#include "kdev-pg-memory-pool.h"
#include <vector>
#include <utility>
using std::vector;
using std::pair;
using std::make_pair;
#include <QtCore/QString>
#include <QtCore/QDebug>
#define PG_NODE(k) \
enum { NodeKind = NodeKind##k };
namespace KDevPG
{
// the kdev-pg calculus
namespace Model
{
enum NodeKind {
NodeKindItem = 0,
NodeKindZero = 1,
NodeKindPlus = 2,
NodeKindStar = 3,
NodeKindSymbol = 4,
NodeKindAction = 5,
NodeKindAlternative = 6,
NodeKindCons = 7,
NodeKindEvolve = 8,
NodeKindTryCatch = 9,
NodeKindAlias = 10,
NodeKindTerminal = 11,
NodeKindNonTerminal = 12,
NodeKindAnnotation = 13,
NodeKindCondition = 14,
NodeKindVariableDeclaration = 15,
NodeKindOperator = 16,
NodeKindInlinedNonTerminal = 17,
NodeKindLast
};
class Node
{
public:
PG_NODE(Item)
int kind;
};
class ZeroItem: public Node
{
public:
PG_NODE(Zero)
};
class PlusItem: public Node
{
public:
PG_NODE(Plus)
Node *mItem;
};
class StarItem: public Node
{
public:
PG_NODE(Star)
Node *mItem;
};
class SymbolItem: public Node
{
public:
PG_NODE(Symbol)
QString mName;
QString mCapitalizedName;
};
class ActionItem: public Node
{
public:
PG_NODE(Action)
Node *mItem;
QString mCode;
};
class AlternativeItem: public Node
{
public:
PG_NODE(Alternative)
Node *mLeft;
Node *mRight;
};
class ConsItem: public Node
{
public:
PG_NODE(Cons)
Node *mLeft;
Node *mRight;
};
class TryCatchItem: public Node
{
public:
PG_NODE(TryCatch)
Node *mTryItem;
Node *mCatchItem; // contains 0 for "catch(recover)"
bool mUnsafe;
};
class AliasItem: public Node
{
public:
PG_NODE(Alias)
QString mCode;
SymbolItem *mSymbol;
};
class InlinedNonTerminalItem: public Node
{
public:
PG_NODE(InlinedNonTerminal)
SymbolItem *mSymbol;
};
class TerminalItem: public Node
{
public:
PG_NODE(Terminal)
QString mName;
QString mDescription;
};
class NonTerminalItem: public Node
{
public:
PG_NODE(NonTerminal)
SymbolItem *mSymbol;
QString mArguments;
};
class Operator
{
public:
Node *mTok;
QString mCond, mCode;
};
class OperatorItem : public Node
{
public:
PG_NODE(Operator)
struct TernDescription
{
Operator first, second;
QString left;
QString priority;
};
struct BinDescription
{
Operator op;
QString left;
QString priority;
};
struct UnaryDescription
{
Operator op;
QString priority;
};
struct ParenDescription
{
Operator first, second;
};
QString mName;
NonTerminalItem *mBase;
vector< ParenDescription > mParen;
vector< TernDescription > mTern;
vector< BinDescription > mBin, mPost;
vector< UnaryDescription > mPre;
inline void pushParen(const Operator& op1, const Operator& op2)
{
ParenDescription d;
d.first = op1;
d.second = op2;
mParen.push_back(d);
}
inline void pushPre(const Operator& op, const QString& priority)
{
UnaryDescription d;
d.op = op;
d.priority = priority;
mPre.push_back(d);
}
inline void pushPost(const Operator& op, const QString& left, const QString& priority)
{
BinDescription d;
d.op = op;
d.priority = priority;
d.left = left;
mPost.push_back(d);
}
inline void pushBin(const Operator& op, const QString& left, const QString& priority)
{
BinDescription d;
d.op = op;
d.left = left;
d.priority = priority;
mBin.push_back(d);
}
inline void pushTern(const Operator& op1, const Operator& op2, const QString& left, const QString& priority)
{
TernDescription d;
d.first = op1;
d.second = op2;
d.left = left;
d.priority = priority;
mTern.push_back(d);
}
};
class VariableDeclarationItem: public Node
{
public:
PG_NODE(VariableDeclaration)
enum DeclarationType {
DeclarationArgument,
DeclarationLocal
};
enum StorageType {
StorageAstMember,
StorageTemporary
};
enum VariableType {
TypeNode,
TypeToken,
TypeVariable
};
QString mType;
QString mCapitalizedType;
QString mName;
DeclarationType mDeclarationType;
StorageType mStorageType;
VariableType mVariableType;
bool mIsSequence;
VariableDeclarationItem *mNext;
};
class AnnotationItem: public Node
{
public:
PG_NODE(Annotation)
Node *mItem;
VariableDeclarationItem *mDeclaration;
};
class ConditionItem: public Node
{
public:
PG_NODE(Condition)
QString mCode;
Node *mItem;
};
class EvolveItem: public Node
{
public:
PG_NODE(Evolve)
Node *mItem;
SymbolItem *mSymbol;
VariableDeclarationItem *mDeclarations;
QString mCode;
};
} // namespace model
// configuration stuff outside the model
namespace Settings
{
enum NodeKind {
NodeKindMember = 30,
NodeKindLast
};
class MemberItem: public Model::Node
{
public:
PG_NODE(Member)
enum MemberKind {
PublicDeclaration = 1,
ProtectedDeclaration = 2,
PrivateDeclaration = 4,
ConstructorCode = 8,
DestructorCode = 16
};
MemberKind mMemberKind;
QString mCode;
};
} // namespace settings
template<class T>
struct StripPtr { typedef T Result; };
template<class T>
struct StripPtr<T*> { typedef T Result; };
template <class _Tp>
_Tp nodeCast(Model::Node *item)
{
if (StripPtr<_Tp>::Result::NodeKind == item->kind)
return static_cast<_Tp>(item);
return 0;
}
extern KDevPG::Allocator<char> globalMemoryPool;
template <class _Tp>
_Tp *createNode()
{
_Tp *node = new (globalMemoryPool.allocate(sizeof(_Tp)))_Tp();
node->kind = _Tp::NodeKind;
return node;
}
}
#endif // KDEV_PG_AST_H
|