Newer
Older
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
#include "jhammer.h"
#include "com_upstandinghackers_hammer_Hammer.h"
#include <stdlib.h>
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_parse
(JNIEnv *env, jclass class, jobject obj, jbyteArray input_, jint length_)
{
HParser *parser;
uint8_t* input;
size_t length;
HParseResult *result;
jclass resultClass;
jobject retVal;
parser = UNWRAP(env, obj);
input = (uint8_t *) ((*env)->GetByteArrayElements(env, input_, NULL));
length = (size_t) length_;
result = h_parse(parser, input, length);
if(result==NULL)
return NULL;
FIND_CLASS(resultClass, env, "com/upstandinghackers/hammer/ParseResult");
NEW_INSTANCE(retVal, env, resultClass, result);
return retVal;
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_token
(JNIEnv *env, jclass class, jbyteArray str, jint len)
{
RETURNWRAP(env, h_token((uint8_t *) ((*env)->GetByteArrayElements(env, str, NULL)), (size_t) len));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_ch
(JNIEnv *env, jclass class, jbyte c)
{
RETURNWRAP(env, h_ch((uint8_t) c));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_chRange
(JNIEnv *env, jclass class, jbyte lower, jbyte upper)
{
RETURNWRAP(env, h_ch_range((uint8_t) lower, (uint8_t) upper));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_intRange
(JNIEnv *env, jclass class, jobject obj, jlong lower, jlong upper)
{
HParser *parser;
parser = UNWRAP(env, obj);
RETURNWRAP(env, h_int_range(parser, (int64_t) lower, (int64_t) upper));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_bits
(JNIEnv *env, jclass class, jint len, jboolean sign)
{
RETURNWRAP(env, h_bits((size_t) len, (bool)(sign & JNI_TRUE)));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_int64
(JNIEnv *env, jclass class)
{
RETURNWRAP(env, h_int64());
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_int32
(JNIEnv *env, jclass class)
{
RETURNWRAP(env, h_int32());
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_int16
(JNIEnv *env, jclass class)
{
RETURNWRAP(env, h_int16());
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_int8
(JNIEnv *env, jclass class)
{
RETURNWRAP(env, h_int8());
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_uInt64
(JNIEnv *env, jclass class)
{
RETURNWRAP(env, h_uint64());
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_uInt32
(JNIEnv *env, jclass class)
{
RETURNWRAP(env, h_uint32());
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_uInt16
(JNIEnv *env, jclass class)
{
RETURNWRAP(env, h_uint16());
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_uInt8
(JNIEnv *env, jclass class)
{
RETURNWRAP(env, h_uint8());
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_whitespace
(JNIEnv *env, jclass class, jobject parser)
{
RETURNWRAP(env, h_whitespace(UNWRAP(env, parser)));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_left
(JNIEnv *env, jclass class, jobject p, jobject q)
{
RETURNWRAP(env, h_left(UNWRAP(env, p), UNWRAP(env, q)));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_right
(JNIEnv *env, jclass class, jobject p, jobject q)
{
RETURNWRAP(env, h_right(UNWRAP(env, p), UNWRAP(env, q)));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_middle
(JNIEnv *env, jclass class, jobject p, jobject x, jobject q)
{
RETURNWRAP(env, h_middle(UNWRAP(env, p), UNWRAP(env, x), UNWRAP(env, q)));
}
/**
* Given another parser, p, and a function f, returns a parser that
* applies p, then applies f to everything in the AST of p's result.
*
* Result token type: any
*/
//HAMMER_FN_DECL(HParser*, h_action, const HParser* p, const HAction a, void* user_data);
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_in
(JNIEnv *env, jclass class, jbyteArray charset, jint length)
{
RETURNWRAP(env, h_in((uint8_t *) ((*env)->GetByteArrayElements(env, charset, NULL)), (size_t)length));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_notIn
(JNIEnv *env, jclass class, jbyteArray charset, jint length)
{
RETURNWRAP(env, h_not_in((uint8_t*) ((*env)->GetByteArrayElements(env, charset, NULL)), (size_t)length));
}
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
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_endP
(JNIEnv *env, jclass class)
{
RETURNWRAP(env, h_end_p());
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_nothingP
(JNIEnv *env, jclass class)
{
RETURNWRAP(env, h_nothing_p());
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_sequence
(JNIEnv *env, jclass class, jobjectArray sequence)
{
jsize length;
void **parsers;
int i;
jobject current;
const HParser *result;
length = (*env)->GetArrayLength(env, sequence);
parsers = malloc(sizeof(void *)*(length+1));
if(NULL==parsers)
{
return NULL;
}
for(i=0; i<length; i++)
{
current = (*env)->GetObjectArrayElement(env, sequence, (jsize)i);
parsers[i] = UNWRAP(env, current);
}
parsers[length] = NULL;
result = h_sequence__a(parsers);
RETURNWRAP(env, result);
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_choice
(JNIEnv *env, jclass class, jobjectArray choices)
{
jsize length;
void **parsers;
int i;
jobject current;
const HParser *result;
length = (*env)->GetArrayLength(env, choices);
parsers = malloc(sizeof(HParser *)*(length+1));
if(NULL==parsers)
{
return NULL;
}
for(i=0; i<length; i++)
{
current = (*env)->GetObjectArrayElement(env, choices, (jsize)i);
parsers[i] = UNWRAP(env, current);
}
parsers[length] = NULL;
result = h_choice__a(parsers);
RETURNWRAP(env, result);
}
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
/**
* Given a null-terminated list of parsers, match a permutation phrase of these
* parsers, i.e. match all parsers exactly once in any order.
*
* If multiple orders would match, the lexically smallest permutation is used;
* in other words, at any step the remaining available parsers are tried in
* the order in which they appear in the arguments.
*
* As an exception, 'h_optional' parsers (actually those that return a result
* of token type TT_NONE) are detected and the algorithm will try to match them
* with a non-empty result. Specifically, a result of TT_NONE is treated as a
* non-match as long as any other argument matches.
*
* Other parsers that succeed on any input (e.g. h_many), that match the same
* input as others, or that match input which is a prefix of another match can
* lead to unexpected results and should probably not be used as arguments.
*
* The result is a sequence of the same length as the argument list.
* Each parser's result is placed at that parser's index in the arguments.
* The permutation itself (the order in which the arguments were matched) is
* not returned.
*
* Result token type: TT_SEQUENCE
*/
//HAMMER_FN_DECL_VARARGS_ATTR(H_GCC_ATTRIBUTE((sentinel)), HParser*, h_permutation, HParser* p);
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_permutation
(JNIEnv *env, jclass class, jobjectArray permutation)
{
jsize length;
void **parsers;
int i;
jobject current;
const HParser *result;
length = (*env)->GetArrayLength(env, permutation);
parsers = malloc(sizeof(HParser *)*(length+1));
if(NULL==parsers)
{
return NULL;
}
for(i=0; i<length; i++)
{
current = (*env)->GetObjectArrayElement(env, permutation, (jsize)i);
parsers[i] = UNWRAP(env, current);
}
parsers[length] = NULL;
result = h_permutation__a(parsers);
RETURNWRAP(env, result);
}
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
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_butNot
(JNIEnv *env, jclass class, jobject p, jobject q)
{
RETURNWRAP(env, h_butnot(UNWRAP(env, p), UNWRAP(env, q)));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_difference
(JNIEnv *env, jclass class, jobject p, jobject q)
{
RETURNWRAP(env, h_difference(UNWRAP(env, p), UNWRAP(env, q)));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_xor
(JNIEnv *env, jclass class, jobject p, jobject q)
{
RETURNWRAP(env, h_xor(UNWRAP(env, p), UNWRAP(env, q)));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_many
(JNIEnv *env, jclass class, jobject p)
{
RETURNWRAP(env, h_many(UNWRAP(env, p)));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_many1
(JNIEnv *env, jclass class, jobject p)
{
RETURNWRAP(env, h_many1(UNWRAP(env, p)));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_repeatN
(JNIEnv *env, jclass class, jobject p, jint n)
{
RETURNWRAP(env, h_repeat_n(UNWRAP(env, p), (size_t)n));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_optional
(JNIEnv *env, jclass class, jobject p)
{
RETURNWRAP(env, h_optional(UNWRAP(env, p)));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_ignore
(JNIEnv *env, jclass class, jobject p)
{
RETURNWRAP(env, h_ignore(UNWRAP(env, p)));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_sepBy
(JNIEnv *env, jclass class, jobject p, jobject sep)
{
RETURNWRAP(env, h_sepBy(UNWRAP(env, p), UNWRAP(env, sep)));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_sepBy1
(JNIEnv *env, jclass class, jobject p, jobject sep)
{
RETURNWRAP(env, h_sepBy1(UNWRAP(env, p), UNWRAP(env, sep)));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_epsilonP
(JNIEnv *env, jclass class)
{
RETURNWRAP(env, h_epsilon_p());
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_lengthValue
(JNIEnv *env, jclass class, jobject length, jobject value)
{
RETURNWRAP(env, h_length_value(UNWRAP(env, length), UNWRAP(env, value)));
}
/**
* This parser attaches a predicate function, which returns true or
* false, to a parser. The function is evaluated over the parser's
* result.
*
* The parse only succeeds if the attribute function returns true.
*
* attr_bool will check whether p's result exists and whether p's
* result AST exists; you do not need to check for this in your
* predicate function.
*
* Result token type: p's result type if pred succeeded, NULL otherwise.
*/
//HAMMER_FN_DECL(HParser*, h_attr_bool, const HParser* p, HPredicate pred, void* user_data);
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_and
(JNIEnv *env, jclass class, jobject p)
{
RETURNWRAP(env, h_and(UNWRAP(env, p)));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_not
(JNIEnv *env, jclass class, jobject p)
{
RETURNWRAP(env, h_not(UNWRAP(env, p)));
}
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_indirect
(JNIEnv *env, jclass class)
{
RETURNWRAP(env, h_indirect());
}
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/**
* Set the inner parser of an indirect. See comments on indirect for
* details.
*/
//HAMMER_FN_DECL(void, h_bind_indirect, HParser* indirect, const HParser* inner);
//JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_bind_indirect
// (JNIEnv *env, jclass class, jobject indirect, jobject inner)
//{
// RETURNWRAP(env, h_bind_indirect(UNWRAP(env, indirect), UNWRAP(env, inner)));
//}
/**
* This parser runs its argument parser with the given endianness setting.
*
* The value of 'endianness' should be a bit-wise or of the constants
* BYTE_BIG_ENDIAN/BYTE_LITTLE_ENDIAN and BIT_BIG_ENDIAN/BIT_LITTLE_ENDIAN.
*
* Result token type: p's result type.
*/
//HAMMER_FN_DECL(HParser*, h_with_endianness, char endianness, const HParser* p);
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_with_endianness
(JNIEnv *env, jclass class, jbyte endianess, jobject p)
{
RETURNWRAP(env, h_with_endianness((char) endianess, UNWRAP(env, p)));
}
/**
* The 'h_put_value' combinator stashes the result of the parser
* it wraps in a symbol table in the parse state, so that non-
* local actions and predicates can access this value.
*
* Try not to use this combinator if you can avoid it.
*
* Result token type: p's token type if name was not already in
* the symbol table. It is an error, and thus a NULL result (and
* parse failure), to attempt to rename a symbol.
*/
//HAMMER_FN_DECL(HParser*, h_put_value, const HParser *p, const char* name);
/**
* The 'h_get_value' combinator retrieves a named HParseResult that
* was previously stashed in the parse state.
*
* Try not to use this combinator if you can avoid it.
*
* Result token type: whatever the stashed HParseResult is, if
* present. If absent, NULL (and thus parse failure).
*/
//HAMMER_FN_DECL(HParser*, h_get_value, const char* name);
/**
* Monadic bind for HParsers, i.e.:
* Sequencing where later parsers may depend on the result(s) of earlier ones.
*
* Run p and call the result x. Then run k(env,x). Fail if p fails or if
* k(env,x) fails or if k(env,x) is NULL.
*
* Result: the result of k(x,env).
*/
//HAMMER_FN_DECL(HParser*, h_bind, const HParser *p, HContinuation k, void *env);
/**
* This parser skips 'n' bits of input.
*
* Result: None. The HParseResult exists but its AST is NULL.
*/
//HAMMER_FN_DECL(HParser*, h_skip, size_t n);
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_skip
(JNIEnv *env, jclass class, jint n)
{
RETURNWRAP(env, h_skip((size_t) n));
}
/**
* The HParser equivalent of fseek(), 'h_seek' modifies the parser's input
* position. Note that contrary to 'fseek', offsets are in bits, not bytes.
* The 'whence' argument uses the same values and semantics: SEEK_SET,
* SEEK_CUR, SEEK_END.
*
* Fails if the new input position would be negative or past the end of input.
*
* Result: TT_UINT. The new input position.
*/
//HAMMER_FN_DECL(HParser*, h_seek, ssize_t offset, int whence);
//TODO double check mapping for int
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_seek
(JNIEnv *env, jclass class, jint offset, jint whence)
{
RETURNWRAP(env, h_seek((ssize_t) offset, (int) whence));
}
/**
* Report the current position in bits. Consumes no input.
*
* Result: TT_UINT. The current input position.
*/
//HAMMER_FN_DECL_NOARG(HParser*, h_tell);
JNIEXPORT jobject JNICALL Java_com_upstandinghackers_hammer_Hammer_tell
(JNIEnv *env, jclass class)
{
RETURNWRAP(env, h_tell());
}