-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathLuaModule.h
164 lines (145 loc) · 5.18 KB
/
LuaModule.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
#ifndef LUAMODULE_H
#define LUAMODULE_H
/*
* Copyright 2020 Rochus Keller <mailto:[email protected]>
*
* This file is part of the Lua parser library.
*
* The following is the license that applies to this copy of the
* library. For a license to use the library under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include <QObject>
#include <QSharedData>
#include <LjTools/LuaSynTree.h>
namespace Ljas
{
class Errors;
class FileCache;
}
namespace Lua
{
class Module : public QObject
{
public:
struct SymbolUse;
struct Scope;
template <class T>
struct Ref : public QExplicitlySharedDataPointer<T>
{
Ref(T* t = 0):QExplicitlySharedDataPointer<T>(t) {}
bool isNull() const { return QExplicitlySharedDataPointer<T>::constData() == 0; }
};
struct Thing : public QSharedData
{
enum Tag { T_Thing, T_Variable, T_Block, T_Function, T_Global, T_GlobalSym, T_SymbolUse };
static const char* s_tagName[];
Token d_tok;
QList< Ref<SymbolUse> > d_uses;
Thing(){}
virtual ~Thing(){}
virtual int getTag() const { return T_Thing; }
virtual bool isScope() const { return false; }
virtual bool isLhsUse() const { return false; }
virtual bool isImplicitDecl() const { return false; }
};
struct Variable : public Thing
{
// Declaration
Scope* d_owner;
Variable():d_owner(0) {}
int getTag() const { return T_Variable; }
};
struct SymbolUse : public Thing
{
Thing* d_sym;
bool d_lhs;
bool d_implicitDecl;
SymbolUse():d_sym(0),d_implicitDecl(false),d_lhs(false){}
int getTag() const { return T_SymbolUse; }
bool isLhsUse() const { return d_lhs; }
bool isImplicitDecl() const { return d_implicitDecl; }
};
struct Block;
struct Scope : public Thing
{
Scope* d_outer;
typedef QHash<const char*,Ref<Thing> > Names;
Names d_names;
QList< Ref<Thing> > d_locals; // vars and funcs
QList< Ref<Block> > d_stats;
QList< Ref<SymbolUse> > d_refs;
Scope():d_outer(0){}
Thing* find( const char* name ) const;
bool isScope() const { return true; }
};
struct Block : public Scope
{
int getTag() const { return T_Block; }
};
struct Function : public Scope
{
enum Kind { Local, NonLocal, Global };
quint16 d_parCount; // param vars are in d_locals
quint8 d_kind;
Function():d_parCount(0),d_kind(Local){}
int getTag() const { return T_Function; }
};
struct Global : public Scope
{
void clear();
int getTag() const { return T_Global; }
};
struct GlobalSym : public Thing
{
bool d_builtIn;
GlobalSym(bool builtIn = false):d_builtIn(builtIn){}
int getTag() const { return T_GlobalSym; }
};
explicit Module(QObject *parent = 0);
void setErrors(Ljas::Errors* p) { d_err = p; }
void setCache(Ljas::FileCache* p) { d_fcache = p; }
bool parse( const QString& path, bool clearGlobal = true );
Block* getTopChunk() const { return d_topChunk.data(); }
const QList< Ref<Function> >& getNonLocals() const { return d_nonLocals; }
const QString& getPath() const { return d_path; }
Global* getGlobal() const { return d_global.data(); }
void setGlobal(Global* g) { d_global = g; }
static void initBuiltIns(Global*);
static void addBuiltInSym( Global*, const QByteArray& );
protected:
void analyze( SynTree* );
void chunk(SynTree*, Scope* );
void stat( SynTree*, Scope* );
void localdecl(SynTree*,Scope*);
void lvardecl(SynTree*,Scope*);
void explist(SynTree*,Scope*);
void exp(SynTree*,Scope*);
void lfuncdecl(SynTree*,Scope*);
void funcbody(SynTree*,Function*);
void namelist(SynTree*,Scope*);
void forstat(SynTree*,Scope*);
void gfuncdecl(SynTree*,Scope*);
void prefixexp(SynTree*,Scope*,bool lhs);
void assignment(SynTree*,Scope*);
void use(SynTree*,Scope*,bool lhs);
void lambdecl(SynTree*,Scope*);
private:
QString d_path;
Ljas::Errors* d_err;
Ljas::FileCache* d_fcache;
Ref<Global> d_global;
Ref<Block> d_topChunk;
QList< Ref<Function> > d_nonLocals;
};
}
#endif // LUAMODULE_H