]>
git.saurik.com Git - cycript.git/blob - sig/parse.cpp
c5948c46743ed0a423446ed7ecc6829c60ba2bc0
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
22 #include <apr_strings.h>
23 #include "sig/parse.hpp"
32 void Parse_(apr_pool_t
*pool
, struct Signature
*signature
, const char **name
, char eos
, Callback callback
);
33 struct Type
*Parse_(apr_pool_t
*pool
, const char **name
, char eos
, bool named
, Callback callback
);
36 /* XXX: I really screwed up this time */
37 void *prealloc_(apr_pool_t
*pool
, void *odata
, size_t osize
, size_t nsize
) {
38 void *ndata
= apr_palloc(pool
, nsize
);
39 memcpy(ndata
, odata
, osize
);
43 void Parse_(apr_pool_t
*pool
, struct Signature
*signature
, const char **name
, char eos
, Callback callback
) {
44 _assert(*name
!= NULL
);
46 // XXX: this is just a stupid check :(
47 bool named(**name
== '"');
49 signature
->elements
= NULL
;
58 signature
->elements
= (struct Element
*) prealloc_(pool
, signature
->elements
, signature
->count
* sizeof(struct Element
), (signature
->count
+ 1) * sizeof(struct Element
));
59 _assert(signature
->elements
!= NULL
);
61 struct Element
*element
= &signature
->elements
[signature
->count
++];
66 const char *quote
= strchr(++*name
, '"');
67 element
->name
= apr_pstrmemdup(pool
, *name
, quote
- *name
);
71 element
->type
= Parse_(pool
, name
, eos
, named
, callback
);
73 if (**name
< '0' || **name
> '9')
74 element
->offset
= _not(size_t);
79 element
->offset
= element
->offset
* 10 + (*(*name
)++ - '0');
80 while (**name
>= '0' && **name
<= '9');
85 struct Type
*Parse_(apr_pool_t
*pool
, const char **name
, char eos
, bool named
, Callback callback
) {
86 char next
= *(*name
)++;
90 struct Type
*type
= (struct Type
*) apr_palloc(pool
, sizeof(struct Type
));
91 _assert(type
!= NULL
);
92 memset(type
, 0, sizeof(struct Type
));
96 case '#': type
->primitive
= typename_P
; break;
99 if (type
->data
.signature
.count
< 2)
100 type
->primitive
= struct_P
;
102 type
->primitive
= union_P
;
106 case '*': type
->primitive
= string_P
; break;
107 case ':': type
->primitive
= selector_P
; break;
113 type
->primitive
= block_P
;
116 type
->primitive
= object_P
;
119 const char *quote
= strchr(*name
+ 1, '"');
120 if (!named
|| quote
[1] == eos
|| quote
[1] == '"') {
121 type
->name
= apr_pstrmemdup(pool
, *name
+ 1, quote
- *name
- 1);
129 case 'B': type
->primitive
= boolean_P
; break;
130 case 'C': type
->primitive
= uchar_P
; break;
131 case 'I': type
->primitive
= uint_P
; break;
132 case 'L': type
->primitive
= ulong_P
; break;
133 case 'Q': type
->primitive
= ulonglong_P
; break;
134 case 'S': type
->primitive
= ushort_P
; break;
137 type
->primitive
= array_P
;
138 type
->data
.data
.size
= strtoul(*name
, (char **) name
, 10);
139 type
->data
.data
.type
= Parse_(pool
, name
, eos
, false, callback
);
141 printf("']' != \"%s\"\n", *name
);
148 type
->primitive
= pointer_P
;
150 type
->data
.data
.type
= NULL
;
152 type
->data
.data
.type
= Parse_(pool
, name
, eos
, named
, callback
);
153 sig::Type
*&target(type
->data
.data
.type
);
154 if (target
!= NULL
&& target
->primitive
== void_P
)
160 type
->primitive
= bit_P
;
161 type
->data
.data
.size
= strtoul(*name
, (char **) name
, 10);
164 case 'c': type
->primitive
= char_P
; break;
165 case 'd': type
->primitive
= double_P
; break;
166 case 'f': type
->primitive
= float_P
; break;
167 case 'i': type
->primitive
= int_P
; break;
168 case 'l': type
->primitive
= long_P
; break;
169 case 'q': type
->primitive
= longlong_P
; break;
170 case 's': type
->primitive
= short_P
; break;
171 case 'v': type
->primitive
= void_P
; break;
174 type
->primitive
= struct_P
;
180 const char *begin
= *name
;
181 do next
= *(*name
)++;
186 size_t length
= *name
- begin
- 1;
187 if (strncmp(begin
, "?", length
) != 0)
188 type
->name
= (char *) apr_pstrmemdup(pool
, begin
, length
);
192 // XXX: this types thing is a throwback to JocStrap
195 Parse_(pool
, &type
->data
.signature
, name
, end
, callback
);
198 case 'N': type
->flags
|= JOC_TYPE_INOUT
; goto next
;
199 case 'n': type
->flags
|= JOC_TYPE_IN
; goto next
;
200 case 'O': type
->flags
|= JOC_TYPE_BYCOPY
; goto next
;
201 case 'o': type
->flags
|= JOC_TYPE_OUT
; goto next
;
202 case 'R': type
->flags
|= JOC_TYPE_BYREF
; goto next
;
203 case 'r': type
->flags
|= JOC_TYPE_CONST
; goto next
;
204 case 'V': type
->flags
|= JOC_TYPE_ONEWAY
; goto next
;
212 printf("invalid type character: '%c' {%s}\n", next
, *name
- 10);
216 if (callback
!= NULL
)
217 (*callback
)(pool
, type
);
222 void Parse(apr_pool_t
*pool
, struct Signature
*signature
, const char *name
, Callback callback
) {
223 const char *temp
= name
;
224 Parse_(pool
, signature
, &temp
, '\0', callback
);
225 _assert(temp
[-1] == '\0');
228 const char *Unparse(apr_pool_t
*pool
, struct Signature
*signature
) {
229 const char *value
= "";
232 for (offset
= 0; offset
!= signature
->count
; ++offset
) {
233 const char *type
= Unparse(pool
, signature
->elements
[offset
].type
);
234 value
= apr_pstrcat(pool
, value
, type
, NULL
);
240 const char *Unparse(apr_pool_t
*pool
, struct Type
*type
) {
243 else switch (type
->primitive
) {
244 case typename_P
: return "#";
245 case union_P
: return apr_psprintf(pool
, "(%s)", Unparse(pool
, &type
->data
.signature
));
246 case string_P
: return "*";
247 case selector_P
: return ":";
248 case block_P
: return "@?";
249 case object_P
: return type
->name
== NULL
? "@" : apr_psprintf(pool
, "@\"%s\"", type
->name
);
250 case boolean_P
: return "B";
251 case uchar_P
: return "C";
252 case uint_P
: return "I";
253 case ulong_P
: return "L";
254 case ulonglong_P
: return "Q";
255 case ushort_P
: return "S";
258 const char *value
= Unparse(pool
, type
->data
.data
.type
);
259 return apr_psprintf(pool
, "[%"APR_SIZE_T_FMT
"%s]", type
->data
.data
.size
, value
);
262 case pointer_P
: return apr_psprintf(pool
, "^%s", type
->data
.data
.type
== NULL
? "v" : Unparse(pool
, type
->data
.data
.type
));
263 case bit_P
: return apr_psprintf(pool
, "b%"APR_SIZE_T_FMT
"", type
->data
.data
.size
);
264 case char_P
: return "c";
265 case double_P
: return "d";
266 case float_P
: return "f";
267 case int_P
: return "i";
268 case long_P
: return "l";
269 case longlong_P
: return "q";
270 case short_P
: return "s";
271 case void_P
: return "v";
272 case struct_P
: return apr_psprintf(pool
, "{%s=%s}", type
->name
== NULL
? "?" : type
->name
, Unparse(pool
, &type
->data
.signature
));