pkg-psyc: callback for psyc_parse

This commit is contained in:
Gabor Adam Toth 2011-05-19 21:27:20 +02:00
parent 79c479b563
commit 1d35e854e3
3 changed files with 58 additions and 50 deletions

View File

@ -604,7 +604,7 @@ int send_erq(int, int*|string, null|closure default: F_CONST0);
#endif
#ifdef USE_PSYC
mixed* psyc_parse(int* | string);
void psyc_parse(int* | string);
string psyc_render(mixed*);
#endif /* USE_PSYC */

View File

@ -3,37 +3,47 @@
*------------------------------------------------------------------
* test LPC code:
*
// doesn't work with (int *) yet
string s = ":_context\ttest\n\n:_topic\ttesting\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n";
mixed *f = psyc_parse(s);
mixed fr = psyc_render(f);
mixed r = ({ ([ "_context": "test" ]), ([ "_topic": "testing" ]),
"_notice_test_libpsyc", "Just [_topic] libpsyc." });
mixed rr = psyc_render(r);
debug_message(sprintf("libpsyc returned %O,\n%O\nand\n%O\n(%s)\n", f, fr, rr,
fr == s && rr == s ? "SUCCESS": "FAIL!"));
mixed *p1 = psyc_parse(":_context\ttest\n\n:_topic\ttest");
debug_message(sprintf("p1: libpsyc returned %O\n", p1));
mixed *p2 = psyc_parse("ing\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n");
debug_message(sprintf("p2: libpsyc returned %O\n", p2));
string s, s2;
mixed pr = psyc_render(p2);
debug_message(sprintf("libpsyc returned %O (%s)\n", p2,
pr == s ? "SUCCESS": "FAIL!"));
create() {
mixed p, r;
s = ":_context\ttest\n\n:_topic\ttesting\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n";
psyc_parse(s+s+s);
string ls = ":_context\ttest\n\n:_list_foo\t|foo|bar|baz\n:_list_bar\t\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n";
string ls2 = ":_context\ttest\n\n:_list_bar\t\n:_list_foo\t|foo|bar|baz\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n";
mixed *lf = psyc_parse(ls);
mixed *lfr = psyc_render(lf);
mixed lr = ({ ([ "_context": "test" ]),
([ "_list_foo": ({ "foo", "bar", "baz" }), "_list_bar": ({}) ]),
"_notice_test_libpsyc",
"Just [_topic] libpsyc." });
mixed lrr = psyc_render(lr);
psyc_parse(":_context\ttest\n\n:_topic\ttest");
psyc_parse("ing\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n");
p = ({ ([ "_context": "test" ]), ([ "_topic": "testing" ]),
"_notice_test_libpsyc", "Just [_topic] libpsyc." });
r = psyc_render(p);
debug_message(sprintf("rendering test:\n%s\n", r));
if (r != s) raise_error("FAIL\n");
s = ":_context\ttest\n\n:_list_foo\t|foo|bar|baz\n:_list_bar\t\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n";
s2 = ":_context\ttest\n\n:_list_bar\t\n:_list_foo\t|foo|bar|baz\n_notice_test_libpsyc\nJust [_topic] libpsyc.\n|\n";
psyc_parse(s);
p = ({ ([ "_context": "test" ]),
([ "_list_foo": ({ "foo", "bar", "baz" }), "_list_bar": ({}) ]),
"_notice_test_libpsyc", "Just [_topic] libpsyc." });
r = psyc_render(p);
debug_message(sprintf("list rendering test:\n%s\n", r));
if (r != s) raise_error("FAIL\n");
debug_message(sprintf(">> SUCCESS\n"));
}
psyc_dispatch(mixed p) {
debug_message(sprintf(">> psyc_parse returned:\n%O\n", p));
mixed r = psyc_render(p);
debug_message(sprintf(">> psyc_render returned:\n%s", r));
if (r != s && r != s2) raise_error("FAIL\n");
}
debug_message(sprintf("libpsyc returned %O,\n%O\nand\n%O\n(%s)\n", lf, lfr, lrr,
(lfr == ls || lfr == ls2) && (lrr == ls || lrr == ls2) ? "SUCCESS": "FAIL!"));
*/
#include "array.h"
@ -188,7 +198,7 @@ f_psyc_render(svalue_t *sp) {
} /* f_psyc_render */
/*-------------------------------------------------------------------------*/
// mixed psyc_parse(int* | string);
// void psyc_parse(int* | string);
svalue_t *
f_psyc_parse (svalue_t *sp) {
@ -201,6 +211,7 @@ f_psyc_parse (svalue_t *sp) {
psycParseListState listState;
int ret, retl;
size_t size, i;
string_t *callback = mstring_new_string("psyc_dispatch");
assert_shadow_sent(current_object);
psyc_state_t *state = O_GET_PSYC_STATE(current_object);
@ -214,6 +225,7 @@ f_psyc_parse (svalue_t *sp) {
if (!state->parser) errorf("Out of memory for psyc parse state struct.\n");
psyc_initParseState(state->parser);
}
v = state->packet;
if (sp->type == T_POINTER) {
errorf("\npsyc_parse got %ld int* bytes... not supported yet\n", VEC_SIZE(sp->u.vec));
@ -238,27 +250,24 @@ f_psyc_parse (svalue_t *sp) {
errorf("\npsyc_parse got type %d, not supported\n", sp->type);
}
free_svalue(sp);
if (!state->packet) {
state->packet = allocate_array(4);
if (!state->packet) errorf("Out of memory for psyc_parse array.\n");
v = state->packet;
map = allocate_mapping(0, 1); // empty mapping
if (!map) errorf("Out of memory for psyc_parse routing header.\n");
put_mapping(&v->item[PACKET_ROUTING], map);
map = allocate_mapping(0, 1); // empty mapping
if (!map) errorf("Out of memory for psyc_parse entity header.\n");
put_mapping(&v->item[PACKET_ENTITY], map);
} else
v = state->packet;
do {
ret = psyc_parse(state->parser, &oper, &name, &value);
printf("#%2d %c%.*s = %.*s\n", ret, oper ? oper : ' ', (int)name.length, name.ptr, (int)value.length, value.ptr);
if (!state->packet) {
state->packet = allocate_array(4);
if (!state->packet) errorf("Out of memory for psyc_parse array.\n");
v = state->packet;
map = allocate_mapping(0, 1); // empty mapping
if (!map) errorf("Out of memory for psyc_parse routing header.\n");
put_mapping(&v->item[PACKET_ROUTING], map);
map = allocate_mapping(0, 1); // empty mapping
if (!map) errorf("Out of memory for psyc_parse entity header.\n");
put_mapping(&v->item[PACKET_ENTITY], map);
}
switch (ret) {
case PSYC_PARSE_ENTITY_START: case PSYC_PARSE_BODY_START:
// save oper, name & value in state at the start of incomplete entity or body
@ -360,9 +369,9 @@ f_psyc_parse (svalue_t *sp) {
break;
case PSYC_PARSE_COMPLETE:
put_array(inter_sp, v);
sapply(callback, current_object, 1);
state->packet = NULL;
put_array(sp, v);
ret = 0;
break;
case PSYC_PARSE_INSUFFICIENT:
@ -376,7 +385,6 @@ f_psyc_parse (svalue_t *sp) {
} else
state->remaining = NULL;
put_number(sp, 0);
ret = 0;
break;
@ -435,7 +443,7 @@ f_psyc_parse (svalue_t *sp) {
if (buffer)
pfree(buffer);
return sp;
return --sp;
} /* f_psyc_parse */
#endif /* USE_PSYC */

View File

@ -17,7 +17,7 @@ version_longtype="stable"
# A timestamp, to be used by bumpversion and other scripts.
# It can be used, for example, to 'touch' this file on every build, thus
# forcing revision control systems to add it on every checkin automatically.
version_stamp="Thu May 19 18:45:43 CEST 2011"
version_stamp="Thu May 19 21:24:11 CEST 2011"
# Okay, LDMUD is using 3.x.x so to avoid conflicts let's just use 4.x.x
version_major=4