-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalafile.vala
257 lines (222 loc) · 7.62 KB
/
valafile.vala
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
/*
* valafile.vala
*
* Copyright 2014 Jott <das.jott at gmail com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
public class ValaFile : GLib.Object
{
private File m_oFile = null;
private bool m_bIsVapi = false;
public ValaFile(string sFilename) throws FileError
{
m_oFile = File.new_for_path(sFilename);
m_bIsVapi = sFilename.down().has_suffix(".vapi");
if ( ! m_oFile.query_exists() ) {
throw new FileError.EXIST("File \"%s\" not found\n", sFilename);
}
}
public bool isVapi()
{
return m_bIsVapi;
}
public Class? parse(string sClassname)
{
if (m_oFile != null) {
try {
Class oClass = new Class(sClassname);
oClass.filename = getGenericFilename();
int nOpenBraces = 0;
string sNamespace = "";
bool bIsComment = false, bInClass = false, bIsInNamespace = false;
// the Regex's to find all the stuff
var regComment = new Regex("^.*/\\*+((?!\\*/).)*$");
var regNoComment = new Regex("\\*/+");
var regClass = new Regex("^ *(public{1,1} +)?class{1,1} +([a-zA-Z_0-9]+) *:? *[a-zA-Z_0-9<> ,.]* *({?)$");
var regNamespace = new Regex("^ *(namespace{1,1}) +([a-zA-Z_0-9]+) *({?)$");
var regBraceOpen = new Regex("^[^}]*{[^}]*$");
var regBraceClose = new Regex("^[^{]*}[^{]*$");
var regMethod = new Regex("^ *(public|private)? *(static)? *([a-z0-9\\[\\]]+)? +([a-zA-Z0-9_]+) *\\({1,1}([a-zA-Z0-9<>_,\\*\\[\\] ]*)\\){1,1} *({?)$");
var oStream = new DataInputStream( m_oFile.read() );
string sLine;
while ( (sLine = oStream.read_line(null)) != null ) {
if (bIsComment) {
if (regNoComment.match(sLine)) {
bIsComment = false;
}
} else {
if (regComment.match(sLine)) {
bIsComment = true;
}
}
if (!bIsComment) {
if (regBraceOpen.match(sLine)) {
++nOpenBraces;
}
if (regBraceClose.match(sLine)) {
--nOpenBraces;
}
if (!bInClass) {
MatchInfo info;
if (regClass.match( sLine, 0, out info)) {
// 1: public, 2: Classname, 3: opening brace (or not)
if ( info.fetch(2) == sClassname ) {
bInClass = true;
oClass.namespce = sNamespace;
}
if ( info.fetch(3) == "{" ) {
++nOpenBraces;
}
} else if (regNamespace.match(sLine, 0, out info)){
// 1: namespace, 2: Erik, 3: opening brace (or not)
bIsInNamespace = true;
sNamespace = info.fetch(2);
if ( info.fetch(3) == "{" ) {
++nOpenBraces;
}
}
} else {
// we are in the desired class
if (bIsInNamespace && nOpenBraces < 1) {
sNamespace = "";
bIsInNamespace = false;
}
if ((!bIsInNamespace && nOpenBraces < 1) || (bIsInNamespace && nOpenBraces < 2)) {
bInClass = false;
return oClass;
}
if ((!bIsInNamespace && nOpenBraces == 1) || (bIsInNamespace && nOpenBraces == 2)) {
// we are in the classes scope
MatchInfo info;
if (regMethod.match(sLine, 0, out info)) {
// 1: public/private, 2: static, 3: Return type, 4: Method name, 5: parameters (all), 6: opening brace (or not)
if ( info.fetch(1) == "public" ) {
var oMethod = new Class.Method( info.fetch(4) );
if ( info.fetch(2) == "static" ){
oMethod.isStatic = true;
}
oMethod.returnType = DataType.from_name( info.fetch(3) );
if ( setParameters( info.fetch(5), ref oMethod.params ) ) {
oClass.methods.append( oMethod );
} else {
// TODO: Maybe breakup and exit?
}
}
if ( info.fetch(6) == "{" ) {
++nOpenBraces;
}
}
}
}
}
}
} catch (Error e) {
stderr.printf("%s\n", e.message);
}
}
return null;
}
public bool compile2C(string[]? pkgs=null, string[]? flags=null)
{
if (m_oFile != null) {
string sWorkDir = m_oFile.get_basename();
string sFilename = m_oFile.get_path();
string sHeader = getGenericFilename() + ".h";
return compile2Ccode(sWorkDir, {sFilename}, pkgs, flags, sHeader);
}
return false;
}
public static bool compile2Ccode(string sWorkDir, string[] asFiles, string[]? pkgs=null, string[]? flags=null, string? sHeader=null)
{
string sHdr = "";
if (sHeader != null && sHeader != "") {
sHdr = " --header=%s".printf(sHeader);
}
string sPkgs = "";
if (pkgs != null) {
foreach (string pkg in pkgs) {
sPkgs += " --pkg=%s".printf(pkg);
}
}
string sFlags = "";
if (flags != null) {
foreach (string flag in flags) {
sFlags += " %s".printf(flag);
}
}
string sFiles = "";
foreach (string sFile in asFiles){
sFiles += " %s".printf(sFile);
}
string sCmd = "valac%s%s -C%s %s".printf(sFlags, sPkgs, sHdr, sFiles);
var shell = new Sys();
return shell.spawn_cmd(sWorkDir, sCmd);
}
private bool setParameters(string sParams, ref List<Class.Method.Parameter> oParams)
{
bool ok = true;
try {
MatchInfo info;
var regParam = new Regex("^ *((ref|out)? *)([a-zA-Z0-9\\[\\]]+) +([a-zA-Z0-9_]+)$");
string[] asParams = sParams.split(",");
foreach (string sParam in asParams) {
ok = regParam.match( sParam, 0, out info );
if (ok) {
// 1: ref /out , 2: ref/out, 3: type, 4: name
var oParam = new Class.Method.Parameter( info.fetch(4) );
oParam.direction = info.fetch(2);
oParam.type = DataType.from_name( info.fetch(3) );
ok = (oParam.type != DataType.UNKNOWN);
if (ok) {
oParams.append( oParam );
}
}
if (!ok) {
stderr.printf("Fatal error - Invalid parameter: \"%s\"\n", sParam.strip());
break;
}
}
} catch (Error e) {
stderr.printf("%s\n", e.message);
ok = false;
}
return ok;
}
public string getPath()
{
string sPath = Path.get_dirname( m_oFile.get_path() );
if (!sPath.has_suffix("/")) {
sPath += "/";
}
return sPath;
}
public string getGenericFilename()
{
string sBasename = m_oFile.get_basename();
int nPos = sBasename.last_index_of(".");
if (nPos > 0) {
return sBasename.slice(0, nPos);
}
return "";
}
public string getHeaderName()
{
return getGenericFilename() + ".h";
}
}