|
|
|
|
|
|
|
|
static object Roxen; |
class RequestID { }; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <config.h> |
|
#include <request_trace.h> |
|
#define MAGIC_HELP_ARG |
|
|
|
|
|
|
#ifdef RXML_OBJ_DEBUG |
# define MARK_OBJECT \ |
Debug.ObjectMarker __object_marker = Debug.ObjectMarker (this_object()) |
# define MARK_OBJECT_ONLY \ |
Debug.ObjectMarker __object_marker = Debug.ObjectMarker (0) |
#else |
# define MARK_OBJECT |
# define MARK_OBJECT_ONLY |
#endif |
|
#ifdef OBJ_COUNT_DEBUG |
|
|
# ifndef RXML_OBJ_DEBUG |
# undef MARK_OBJECT |
# undef MARK_OBJECT_ONLY |
# define MARK_OBJECT \ |
mapping __object_marker = (["count": ++all_constants()->_obj_count]) |
# define MARK_OBJECT_ONLY \ |
mapping __object_marker = (["count": ++all_constants()->_obj_count]) |
# endif |
# define OBJ_COUNT (__object_marker ? "[" + __object_marker->count + "]" : "") |
#else |
# define OBJ_COUNT "" |
#endif |
|
#ifdef RXML_VERBOSE |
# define TAG_DEBUG_TEST(flags) 1 |
#elif defined (DEBUG) |
# define TAG_DEBUG_TEST(flags) ((flags) & FLAG_DEBUG) |
#else |
# define TAG_DEBUG_TEST(flags) 0 |
#endif |
|
#ifdef DEBUG |
# define TAG_DEBUG(frame, msg, args...) \ |
(TAG_DEBUG_TEST(frame->flags) && report_debug ("%O: " + (msg), (frame), args), 0) |
# define DO_IF_DEBUG(code...) code |
#else |
# define TAG_DEBUG(frame, msg, args...) 0 |
# define DO_IF_DEBUG(code...) |
#endif |
|
#ifdef MODULE_DEBUG |
# define DO_IF_MODULE_DEBUG(code...) code |
#else |
# define DO_IF_MODULE_DEBUG(code...) |
#endif |
|
#define HASH_INT2(m, n) (n < 65536 ? (m << 16) + n : sprintf ("%x,%x", m, n)) |
|
#undef RXML_CONTEXT |
#if constant (thread_create) |
# define RXML_CONTEXT (_cur_rxml_context->get()) |
# define SET_RXML_CONTEXT(ctx) (_cur_rxml_context->set (ctx)) |
#else |
# define RXML_CONTEXT (_cur_rxml_context) |
# define SET_RXML_CONTEXT(ctx) (_cur_rxml_context = (ctx)) |
#endif |
|
|
#define SCOPE_TYPE mapping(string:mixed)|object(Scope) |
#define UNWIND_STATE mapping(string|object:mixed|array) |
#define EVAL_ARGS_FUNC function(Context:mapping(string:mixed)) |
|
|
class Tag |
|
{ |
constant is_RXML_Tag = 1; |
|
|
|
|
|
|
|
TagSet tagset; |
|
|
int flags; |
|
|
|
mapping(string:Type) req_arg_types = ([]); |
mapping(string:Type) opt_arg_types = ([]); |
|
|
|
|
Type def_arg_type = t_text (PEnt); |
|
|
|
|
Type content_type = t_same (PXml); |
|
|
|
|
|
|
|
|
|
|
|
|
|
array(Type) result_types = ({t_string, t_any_text}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline final object `() (mapping(string:mixed) args, void|mixed content) |
|
|
|
{ |
Tag this = this_object(); |
object frame = ([function(:object)] this->Frame)(); |
frame->tag = this; |
frame->flags = flags; |
frame->args = args; |
frame->content = zero_type (content) ? nil : content; |
frame->result = nil; |
return frame; |
} |
|
int eval_args (mapping(string:mixed) args, void|int dont_throw, |
void|Context ctx, void|array(string) ignore_args) |
|
|
|
|
|
|
|
|
|
{ |
|
mapping(string:Type) atypes = args & req_arg_types; |
if (sizeof (atypes) < sizeof (req_arg_types)) |
if (dont_throw) return 0; |
else { |
array(string) missing = sort (indices (req_arg_types - atypes)); |
parse_error ("Required " + |
(sizeof (missing) > 1 ? |
"arguments " + String.implode_nicely (missing) + " are" : |
"argument " + missing[0] + " is") + " missing.\n"); |
} |
atypes += args & opt_arg_types; |
#ifdef MODULE_DEBUG |
if (mixed err = catch { |
#endif |
foreach (indices (args) - ( ignore_args||({}) ), string arg) |
args[arg] = (atypes[arg] || def_arg_type)->eval ( |
args[arg], ctx); |
#ifdef MODULE_DEBUG |
}) { |
if (objectp (err) && ([object] err)->thrown_at_unwind) |
fatal_error ("Can't save parser state when evaluating arguments.\n"); |
throw_fatal (err); |
} |
#endif |
return 1; |
} |
|
|
|
#define MAKE_FRAME(_frame, _ctx, _parser, _args) \ |
make_new_frame: do { \ |
if (UNWIND_STATE ustate = _ctx->unwind_state) \ |
if (ustate[_parser]) { \ |
_frame = [object/*(Frame)HMM*/] ustate[_parser][0]; \ |
m_delete (ustate, _parser); \ |
if (!sizeof (ustate)) _ctx->unwind_state = 0; \ |
break make_new_frame; \ |
} \ |
_frame = `() (0, nil); \ |
DO_IF_DEBUG( \ |
if (_args && ([mapping] (mixed) _args)["-debug-tag-"]) { \ |
_frame->flags |= FLAG_DEBUG; \ |
m_delete (_args, "-debug-tag-"); \ |
} \ |
); \ |
TAG_DEBUG (_frame, "New frame\n"); \ |
} while (0) |
|
#define EVAL_FRAME(_frame, _ctx, _parser, _type, _args, _content, _res) \ |
eval_frame: do { \ |
EVAL_ARGS_FUNC argfunc = 0; \ |
\ |
mixed err = 0; \ |
if (!_frame->args) { \ |
frame->up = ctx->frame; \ |
ctx->frame = frame; \ |
ctx->frame_depth++; \ |
err = catch { \ |
argfunc = _frame->_prepare ( \ |
_ctx, _type, _args, _parser->p_code); \ |
}; \ |
ctx->frame = frame->up, frame->up = 0; \ |
ctx->frame_depth--; \ |
} \ |
\ |
if (!err) err = catch { \ |
_res = _frame->_eval (_ctx, _parser, _type, 0, _content || ""); \ |
if (PCode p_code = _parser->p_code) \ |
p_code->add_frame (_frame, argfunc, _frame->content); \ |
break eval_frame; \ |
}; \ |
\ |
if (PCode p_code = _parser->p_code) { \ |
/* Make an unparsed frame in the p-code if the evaluation \ |
* fails. */ \ |
_frame->flags |= FLAG_UNPARSED; \ |
_frame->args = _args, _frame->content = _content || ""; \ |
p_code->add_frame (_frame, 0, 0); \ |
} \ |
\ |
if (objectp (err) && ([object] err)->thrown_at_unwind) { \ |
UNWIND_STATE ustate = _ctx->unwind_state; \ |
if (!ustate) ustate = _ctx->unwind_state = ([]); \ |
DO_IF_DEBUG ( \ |
if (err != _frame) \ |
fatal_error ("Unexpected unwind object catched.\n"); \ |
if (ustate[_parser]) \ |
fatal_error ("Clobbering unwind state for parser.\n"); \ |
); \ |
ustate[_parser] = ({_frame}); \ |
throw (_parser); \ |
} \ |
else { \ |
/* Will rethrow unknown errors. */ \ |
_ctx->handle_exception (err, _parser); \ |
_res = nil; \ |
} \ |
} while (0) |
|
final mixed handle_tag (TagSetParser parser, mapping(string:string) args, |
void|string content) |
|
|
{ |
|
|
Context ctx = parser->context; |
|
object frame; |
MAKE_FRAME (frame, ctx, parser, args); |
|
if (!zero_type (frame->raw_tag_text)) |
frame->raw_tag_text = parser->raw_tag_text(); |
|
mixed result; |
EVAL_FRAME (frame, ctx, parser, parser->type, args, content, result); |
|
return result; |
} |
|
final array _p_xml_handle_tag (object parser, mapping(string:string) args, |
void|string content) |
{ |
Type type = parser->type; |
parser->drain_output(); |
|
Context ctx = parser->context; |
|
object frame; |
MAKE_FRAME (frame, ctx, parser, args); |
|
if (!zero_type (frame->raw_tag_text)) |
frame->raw_tag_text = parser->current_input(); |
|
mixed result; |
EVAL_FRAME (frame, ctx, parser, type, args, content, result); |
|
if (result != nil) parser->add_value (result); |
return ({}); |
} |
|
final array _p_xml_handle_pi_tag (object parser, string content) |
{ |
Type type = parser->type; |
parser->drain_output(); |
|
sscanf (content, "%[ \t\n\r]%s", string ws, string rest); |
if (ws == "" && rest != "") { |
|
|
if (!type->free_text) |
return utils->unknown_pi_tag_error (parser, content); |
return 0; |
} |
|
Context ctx = parser->context; |
|
object frame; |
MAKE_FRAME (frame, ctx, parser, 0); |
|
if (!zero_type (frame->raw_tag_text)) |
frame->raw_tag_text = parser->current_input(); |
|
mixed result; |
EVAL_FRAME (frame, ctx, parser, type, 0, content, result); |
|
if (result != nil) parser->add_value (result); |
return ({}); |
} |
|
MARK_OBJECT; |
|
string _sprintf() |
{ |
return "RXML.Tag(" + [string] this_object()->name + |
(this_object()->plugin_name ? "#" + [string] this_object()->plugin_name : "") + |
([int] this_object()->flags & FLAG_PROC_INSTR ? " [PI]" : "") + ")" + |
OBJ_COUNT; |
} |
} |
|
|
class TagSet |
|
|
|
|
|
|
|
|
|
|
|
{ |
string name; |
|
|
|
string prefix; |
|
|
|
|
|
|
|
|
|
|
int prefix_req; |
|
|
array(TagSet) imported = ({}); |
|
|
|
|
function(Context:void) prepare_context; |
|
|
|
|
|
|
function(Context:void) eval_finish; |
|
|
|
|
int generation = 1; |
|
|
|
int id_number; |
|
|
static void create (string _name, void|array(Tag) _tags) |
|
{ |
id_number = ++tag_set_count; |
if (_name) { |
if (!zero_type (all_tagsets[_name])) { |
report_warning ("The tag set name %O is not unique - ignoring it.\n", _name); |
if (TagSet other = all_tagsets[_name]) { |
|
other->name = 0; |
all_tagsets[_name] = 0; |
} |
} |
else |
all_tagsets[name = _name] = this_object(); |
} |
if (_tags) add_tags (_tags); |
#ifdef RXML_OBJ_DEBUG |
__object_marker->create (this_object()); |
#endif |
} |
|
void add_tag (Tag tag) |
|
{ |
#ifdef MODULE_DEBUG |
if (!stringp (tag->name)) |
error ("Trying to register a tag %O without a name.\n", tag); |
if (!callablep (tag->Frame) && !tag->plugin_name) |
error ("Trying to register a tag %O without a Frame class or function.\n", tag); |
if (tag->name[..3] != "!--#" && |
replace (tag->name, "#<>& \t\n\r" / "", ({""}) * 8) != tag->name) |
error ("Invalid character(s) in name for tag %O.\n", tag); |
#endif |
if (tag->flags & FLAG_PROC_INSTR) { |
if (!proc_instrs) proc_instrs = ([]); |
if (tag->plugin_name) proc_instrs[tag->name + "#" + tag->plugin_name] = tag; |
else proc_instrs[tag->name] = tag; |
} |
else |
if (tag->plugin_name) tags[tag->name + "#" + tag->plugin_name] = tag; |
else tags[tag->name] = tag; |
changed(); |
} |
|
void add_tags (array(Tag) _tags) |
|
{ |
foreach (_tags, Tag tag) { |
#ifdef MODULE_DEBUG |
if (!stringp (tag->name)) |
error ("Trying to register a tag %O without a name.\n", tag); |
if (!callablep (tag->Frame)&& !tag->plugin_name) |
error ("Trying to register a tag %O without a Frame class or function.\n", tag); |
if (tag->name[..3] != "!--#" && |
replace (tag->name, "#<>& \t\n\r" / "", ({""}) * 8) != tag->name) |
error ("Invalid character(s) in name for tag %O.\n", tag); |
#endif |
if (tag->flags & FLAG_PROC_INSTR) { |
if (!proc_instrs) proc_instrs = ([]); |
if (tag->plugin_name) proc_instrs[tag->name + "#" + tag->plugin_name] = tag; |
else proc_instrs[tag->name] = tag; |
} |
else |
if (tag->plugin_name) tags[tag->name + "#" + tag->plugin_name] = tag; |
else tags[tag->name] = tag; |
tag->tagset = this_object(); |
} |
changed(); |
} |
|
void remove_tag (string|Tag tag, void|int proc_instr) |
|
|
|
|
{ |
if (stringp (tag)) |
if (proc_instr) { |
if (proc_instrs) m_delete (proc_instrs, tag); |
} |
else |
m_delete (tags, tag); |
else { |
string n; |
if (tag->flags & FLAG_PROC_INSTR) { |
if (proc_instrs && !zero_type (n = search (tags, [object(Tag)] tag))) |
m_delete (proc_instrs, n); |
} |
else |
if (!zero_type (n = search (tags, [object(Tag)] tag))) |
m_delete (tags, n); |
} |
changed(); |
} |
|
local Tag get_local_tag (string name, void|int proc_instr) |
|
|
|
{ |
return proc_instr ? proc_instrs && proc_instrs[name] : tags[name]; |
} |
|
local array(Tag) get_local_tags() |
|
{ |
array(Tag) res = values (tags); |
if (proc_instrs) res += values (proc_instrs); |
return res; |
} |
|
local Tag get_tag (string name, void|int proc_instr) |
|
|
|
|
{ |
if (object(Tag) def = get_local_tag (name, proc_instr)) |
return def; |
foreach (imported, TagSet tag_set) |
if (object(Tag) tag = [object(Tag)] tag_set->get_tag (name, proc_instr)) |
return tag; |
return 0; |
} |
|
local int has_tag (Tag tag) |
|
|
{ |
return !!get_tag (tag->name, tag->flags & FLAG_PROC_INSTR); |
} |
|
local multiset(string) get_tag_names() |
|
{ |
return `| ((multiset) indices (tags), @imported->get_tag_names()); |
} |
|
local multiset(string) get_proc_instr_names() |
|
{ |
return `| (proc_instrs ? (multiset) indices (proc_instrs) : (<>), |
@imported->get_proc_instr_names()); |
} |
|
local Tag get_overridden_tag (Tag overrider) |
|
|
{ |
if (!mappingp (overridden_tag_lookup)) |
overridden_tag_lookup = set_weak_flag (([]), 1); |
Tag tag; |
if (zero_type (tag = overridden_tag_lookup[overrider])) { |
string overrider_name = overrider->plugin_name ? |
overrider->plugin_name + "#" + overrider->name : overrider->name; |
tag = overridden_tag_lookup[overrider] = |
overrider->flags & FLAG_PROC_INSTR ? |
find_overridden_proc_instr (overrider, overrider_name) : |
find_overridden_tag (overrider, overrider_name); |
} |
return tag; |
} |
|
local array(Tag) get_overridden_tags (string name, void|int proc_instr) |
|
|
|
|
{ |
if (object(Tag) def = get_local_tag (name, proc_instr)) |
return ({def}) + imported->get_overridden_tags (name, proc_instr) * ({}); |
else |
return imported->get_overridden_tags (name, proc_instr) * ({}); |
} |
|
void add_string_entities (mapping(string:string) entities) |
|
|
|
{ |
if (string_entities) string_entities |= entities; |
else string_entities = entities + ([]); |
changed(); |
} |
|
void clear_string_entities() |
|
{ |
string_entities = 0; |
changed(); |
} |
|
local mapping(string:string) get_string_entities() |
|
|
{ |
if (string_entities) |
return `+(@imported->get_string_entities(), string_entities); |
else |
return `+(@imported->get_string_entities(), ([])); |
} |
|
local mapping(string:Tag) get_plugins (string name, void|int proc_instr) |
|
|
|
|
{ |
mapping(string:Tag) res; |
if (proc_instr) { |
if (!pi_plugins) pi_plugins = ([]); |
if ((res = pi_plugins[name])) return res; |
low_get_pi_plugins (name + "#", res = ([])); |
return pi_plugins[name] = res; |
} |
else { |
if (!plugins) plugins = ([]); |
if ((res = plugins[name])) return res; |
low_get_plugins (name + "#", res = ([])); |
return plugins[name] = res; |
} |
} |
|
local int has_effective_tags (TagSet tset) |
|
{ |
return tset == top_tag_set && !got_local_tags; |
} |
|
local mixed `->= (string var, mixed val) |
{ |
switch (var) { |
case "name": |
if (name) m_delete (all_tagsets, name); |
if (val) all_tagsets[name = val] = this_object(); |
break; |
case "imported": |
if (!val) return val; |
filter (imported, "dont_notify", changed); |
imported = [array(TagSet)] val; |
imported->do_notify (changed); |
top_tag_set = sizeof (imported) && imported[0]; |
break; |
default: |
::`->= (var, val); |
} |
changed(); |
return val; |
} |
|
local mixed `[]= (string var, mixed val) {return `->= (var, val);} |
|
final Context new_context (void|RequestID id) |
|
|
|
|
{ |
Context ctx = Context (this_object(), id); |
call_prepare_funs (ctx); |
return ctx; |
} |
|
final Parser get_parser (Type top_level_type, void|RequestID id, void|int make_p_code) |
|
|
|
|
{ |
return new_context (id)->new_parser (top_level_type, make_p_code); |
} |
|
final Parser `() (Type top_level_type, void|RequestID id, void|int make_p_code) |
|
{ |
return get_parser (top_level_type, id, make_p_code); |
} |
|
void changed() |
|
|
{ |
generation++; |
prepare_funs = 0; |
overridden_tag_lookup = 0; |
plugins = pi_plugins = 0; |
(notify_funcs -= ({0}))(); |
set_weak_flag (notify_funcs, 1); |
got_local_tags = sizeof (tags) || (proc_instrs && sizeof (proc_instrs)); |
} |
|
function(Backtrace,Type:string) handle_run_error = |
lambda (Backtrace err, Type type) |
|
|
|
{ |
string result; |
foreach(imported, TagSet tag_set) { |
result = tag_set->handle_run_error(err, type); |
if(result) return result; |
} |
return 0; |
}; |
|
function(Backtrace,Type:string) handle_parse_error = |
lambda (Backtrace err, Type type) |
|
|
|
{ |
string result; |
foreach(imported, TagSet tag_set) { |
result = tag_set->handle_parse_error(err, type); |
if(result) return result; |
} |
return 0; |
}; |
|
|
|
void do_notify (function(:void) func) |
{ |
notify_funcs |= ({func}); |
set_weak_flag (notify_funcs, 1); |
} |
|
void dont_notify (function(:void) func) |
{ |
notify_funcs -= ({func}); |
set_weak_flag (notify_funcs, 1); |
} |
|
static void destroy() |
{ |
catch (changed()); |
if (name) m_delete (all_tagsets, name); |
} |
|
static mapping(string:Tag) tags = ([]), proc_instrs; |
|
|
static mapping(string:string) string_entities; |
|
|
|
static TagSet top_tag_set; |
|
|
static int got_local_tags; |
|
|
static array(function(:void)) notify_funcs = ({}); |
|
|
static array(function(Context:void)) prepare_funs; |
|
array(function(Context:void)) get_prepare_funs() |
{ |
if (prepare_funs) return prepare_funs; |
array(function(Context:void)) funs = ({}); |
for (int i = sizeof (imported) - 1; i >= 0; i--) |
funs += imported[i]->get_prepare_funs(); |
if (prepare_context) funs += ({prepare_context}); |
|
return funs; |
} |
|
final void call_prepare_funs (Context ctx) |
{ |
if (!prepare_funs) prepare_funs = get_prepare_funs(); |
(prepare_funs -= ({0})) (ctx); |
} |
|
static array(function(Context:void)) eval_finish_funs; |
|
array(function(Context:void)) get_eval_finish_funs() |
{ |
if (eval_finish_funs) return eval_finish_funs; |
array(function(Context:void)) funs = ({}); |
for (int i = sizeof (imported) - 1; i >= 0; i--) |
funs += imported[i]->get_eval_finish_funs(); |
if (eval_finish) funs += ({eval_finish}); |
|
return funs; |
} |
|
void call_eval_finish_funs (Context ctx) |
{ |
if (!eval_finish_funs) eval_finish_funs = get_eval_finish_funs(); |
(eval_finish_funs -= ({0})) (ctx); |
} |
|
static mapping(Tag:Tag) overridden_tag_lookup; |
|
Tag find_overridden_tag (Tag overrider, string overrider_name) |
{ |
if (tags[overrider_name] == overrider) { |
foreach (imported, TagSet tag_set) |
if (object(Tag) overrider = tag_set->get_tag (overrider_name)) |
return overrider; |
} |
else { |
int found = 0; |
foreach (imported, TagSet tag_set) |
if (object(Tag) subtag = tag_set->get_tag (overrider_name)) |
if (found) return subtag; |
else if (subtag == overrider) |
if ((subtag = tag_set->find_overridden_tag ( |
overrider, overrider_name))) |
return subtag; |
else found = 1; |
} |
return 0; |
} |
|
Tag find_overridden_proc_instr (Tag overrider, string overrider_name) |
{ |
if (proc_instrs && proc_instrs[overrider_name] == overrider) { |
foreach (imported, TagSet tag_set) |
if (object(Tag) overrider = tag_set->get_tag (overrider_name, 1)) |
return overrider; |
} |
else { |
int found = 0; |
foreach (imported, TagSet tag_set) |
if (object(Tag) subtag = tag_set->get_tag (overrider_name, 1)) |
if (found) return subtag; |
else if (subtag == overrider) |
if ((subtag = tag_set->find_overridden_proc_instr ( |
overrider, overrider_name))) |
return subtag; |
else found = 1; |
} |
return 0; |
} |
|
static mapping(string:mapping(string:Tag)) plugins, pi_plugins; |
|
void low_get_plugins (string prefix, mapping(string:Tag) res) |
{ |
for (int i = sizeof (imported) - 1; i >= 0; i--) |
imported[i]->low_get_plugins (prefix, res); |
foreach (indices (tags), string name) |
if (name[..sizeof (prefix) - 1] == prefix) { |
Tag tag = tags[name]; |
if (tag->plugin_name) res[[string] tag->plugin_name] = tag; |
} |
|
} |
|
void low_get_pi_plugins (string prefix, mapping(string:Tag) res) |
{ |
for (int i = sizeof (imported) - 1; i >= 0; i--) |
imported[i]->low_get_pi_plugins (prefix, res); |
if (proc_instrs) |
foreach (indices (proc_instrs), string name) |
if (name[..sizeof (prefix) - 1] == prefix) { |
Tag tag = proc_instrs[name]; |
if (tag->plugin_name) res[[string] tag->plugin_name] = tag; |
} |
|
} |
|
string _sprintf() |
{ |
return sprintf ("RXML.TagSet(%O,%d)%s", name, id_number, OBJ_COUNT); |
} |
|
MARK_OBJECT_ONLY; |
} |
|
TagSet empty_tag_set; |
|
|
|
class Value |
|
|
{ |
mixed rxml_var_eval (Context ctx, string var, string scope_name, void|Type type) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ |
mixed val = rxml_const_eval (ctx, var, scope_name, type); |
ctx->set_var(var, val, scope_name); |
return val; |
} |
|
mixed rxml_const_eval (Context ctx, string var, string scope_name, void|Type type); |
|
|
|
string _sprintf() {return "RXML.Value";} |
} |
|
class Scope |
|
|
|
|
|
|
{ |
mixed `[] (string var, void|Context ctx, |
void|string scope_name, void|Type type) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{parse_error ("Cannot query variable" + _in_the_scope (scope_name) + ".\n");} |
|
mixed `[]= (string var, mixed val, void|Context ctx, void|string scope_name) |
|
|
|
|
|
|
|
{parse_error ("Cannot set variable" + _in_the_scope (scope_name) + ".\n");} |
|
array(string) _indices (void|Context ctx, void|string scope_name) |
|
|
|
|
|
|
|
{parse_error ("Cannot list variables" + _in_the_scope (scope_name) + ".\n");} |
|
void _m_delete (string var, void|Context ctx, void|string scope_name) |
|
|
|
{ |
if (m_delete != local::m_delete) |
m_delete (var, ctx, scope_name); |
else |
parse_error ("Cannot delete variable" + _in_the_scope (scope_name) + ".\n"); |
} |
|
void m_delete (string var, void|Context ctx, void|string scope_name) |
|
{_m_delete (var, ctx, scope_name);} |
|
private string _in_the_scope (string scope_name) |
{ |
if (scope_name) |
if (scope_name != "_") return " in the scope " + scope_name; |
else return " in the current scope"; |
else return ""; |
} |
|
string _sprintf() {return "RXML.Scope";} |
} |
|
class Context |
|
|
|
|
|
|
|
|
{ |
Frame frame; |
|
|
int frame_depth; |
|
|
|
int max_frame_depth = 100; |
|
|
RequestID id; |
|
|
mapping(mixed:mixed) misc = ([]); |
|
|
|
|
|
int type_check; |
|
|
int error_count; |
|
|
|
|
|
|
TagSet tag_set; |
|
|
#ifdef OLD_RXML_COMPAT |
int compatible_scope = 0; |
|
|
|
|
|
|
|
#endif |
|
array(string|int) parse_user_var (string var, void|string|int scope_name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ |
#ifdef OLD_RXML_COMPAT |
if (compatible_scope && !intp(scope_name)) |
return ({scope_name || "form", var}); |
#endif |
|
array(string|int) splitted; |
if(has_value(var, "..")) { |
|
|
string coded = replace (var, "\0", "\0\0"); |
if (coded != var) |
splitted = map (replace (coded, "..", "\0p") / ".", |
replace, ({"\0p", "\0\0"}), ({".", "\0"})); |
else |
splitted = map (replace (var, "..", "\0") / ".", replace, "\0", "."); |
} |
else |
splitted = var / "."; |
|
if (stringp (scope_name)) |
splitted = ({scope_name}) + splitted; |
else if (sizeof (splitted) == 1) |
splitted = ({scope_name || "_"}) + splitted; |
|
for (int i = 2; i < sizeof (splitted); i++) |
if (sscanf (splitted[i], "%d%*c", int d) == 1) splitted[i] = d; |
|
return splitted; |
} |
|
local mixed get_var (string|array(string|int) var, void|string scope_name, |
void|Type want_type) |
|
|
|
|
|
|
|
|
|
|
|
{ |
#ifdef MODULE_DEBUG |
if (arrayp (var) ? !sizeof (var) : !stringp (var)) |
fatal_error ("Invalid variable specifier.\n"); |
#endif |
if (!scope_name) scope_name = "_"; |
if (SCOPE_TYPE vars = scopes[scope_name]) |
return rxml_index (vars, var, scope_name, this_object(), want_type); |
else if (scope_name == "_") parse_error ("No current scope.\n"); |
else parse_error ("Unknown scope %O.\n", scope_name); |
} |
|
mixed user_get_var (string var, void|string scope_name, void|Type want_type) |
|
|
|
|
|
|
|
|
|
{ |
if(!var || !sizeof(var)) return ([])[0]; |
array(string|int) splitted = parse_user_var (var, scope_name); |
return get_var (splitted[1..], splitted[0], want_type); |
} |
|
local mixed set_var (string|array(string|int) var, mixed val, void|string scope_name) |
|
|
|
|
|
{ |
#ifdef MODULE_DEBUG |
if (arrayp (var) ? !sizeof (var) : !stringp (var)) |
fatal_error ("Invalid variable specifier.\n"); |
#endif |
|
if (!scope_name) scope_name = "_"; |
if (SCOPE_TYPE vars = scopes[scope_name]) { |
string|int index; |
if (arrayp (var)) |
if (sizeof (var) > 1) { |
index = var[-1]; |
var = var[..sizeof (var) - 1]; |
vars = rxml_index (vars, var, scope_name, this_object()); |
scope_name += "." + (array(string)) var * "."; |
} |
else index = var[0]; |
else index = var; |
|
if (objectp (vars) && vars->`[]=) |
return ([object(Scope)] vars)->`[]= (index, val, this_object(), scope_name); |
else if (mappingp (vars) || multisetp (vars)) |
return vars[index] = val; |
else if (arrayp (vars)) |
if (intp (index) && index) |
if ((index < 0 ? -index : index) > sizeof (vars)) |
parse_error( "Index %d out of range for array of size %d in %s.\n", |
index, sizeof (val), scope_name ); |
else if (index < 0) |
return vars[index] = val; |
else |
return vars[index - 1] = val; |
else |
parse_error( "Cannot index the array in %s with %O.\n", scope_name, index ); |
else |
parse_error ("%s is %O which cannot be indexed with %O.\n", |
scope_name, vars, index); |
} |
|
else if (scope_name == "_") parse_error ("No current scope.\n"); |
else parse_error ("Unknown scope %O.\n", scope_name); |
} |
|
mixed user_set_var (string var, mixed val, void|string scope_name) |
|
|
|
|
|
|
|
|
|
{ |
if(!var || !sizeof(var)) parse_error ("No variable specified.\n"); |
array(string|int) splitted = parse_user_var (var, scope_name); |
return set_var(splitted[1..], val, splitted[0]); |
} |
|
local void delete_var (string|array(string|int) var, void|string scope_name) |
|
|
|
|
|
{ |
#ifdef MODULE_DEBUG |
if (arrayp (var) ? !sizeof (var) : !stringp (var)) |
fatal_error ("Invalid variable specifier.\n"); |
#endif |
|
if (!scope_name) scope_name = "_"; |
if (SCOPE_TYPE vars = scopes[scope_name]) { |
if (arrayp (var)) |
if (sizeof (var) > 1) { |
string|int last = var[-1]; |
var = var[..sizeof (var) - 1]; |
vars = rxml_index (vars, var, scope_name, this_object()); |
scope_name += "." + (array(string)) var * "."; |
var = last; |
} |
else var = var[0]; |
|
if (objectp (vars) && vars->_m_delete) |
([object(Scope)] vars)->_m_delete (var, this_object(), scope_name); |
else if (mappingp (vars)) |
m_delete ([mapping(string:mixed)] vars, var); |
else if (multisetp (vars)) |
vars[var] = 0; |
else |
parse_error ("Cannot remove the index %O from the %t in %s.\n", |
var, vars, scope_name); |
} |
|
else if (scope_name == "_") parse_error ("No current scope.\n"); |
else parse_error ("Unknown scope %O.\n", scope_name); |
} |
|
void user_delete_var (string var, void|string scope_name) |
|
|
|
|
|
|
|
|
|
{ |
if(!var || !sizeof(var)) return; |
array(string|int) splitted = parse_user_var (var, scope_name); |
delete_var(splitted[1..], splitted[0]); |
} |
|
array(string) list_var (void|string scope_name) |
|
|
{ |
if (SCOPE_TYPE vars = scopes[scope_name || "_"]) |
if (objectp (vars)) |
return ([object(Scope)] vars)->_indices (this_object(), scope_name || "_"); |
else |
return indices ([mapping(string:mixed)] vars); |
else if ((<0, "_">)[scope_name]) parse_error ("No current scope.\n"); |
else parse_error ("Unknown scope %O.\n", scope_name); |
} |
|
array(string) list_scopes() |
|
{ |
return indices (scopes) - ({"_"}); |
} |
|
int exist_scope (void|string scope_name) |
|
{ |
return !!scopes[scope_name || "_"]; |
} |
|
void add_scope (string scope_name, SCOPE_TYPE vars) |
|
|
|
{ |
if (scopes[scope_name]) |
if (scope_name == "_") { |
array(SCOPE_TYPE) hid; |
for (Frame f = frame; f; f = f->up) |
if (array(SCOPE_TYPE) h = hidden[f]) hid = h; |
if (hid) hid[0] = vars; |
else scopes["_"] = vars; |
} |
else { |
Frame outermost; |
for (Frame f = frame; f; f = f->up) |
if (f->scope_name == scope_name) outermost = f; |
if (outermost) hidden[outermost][1] = vars; |
else scopes[scope_name] = vars; |
} |
else scopes[scope_name] = vars; |
} |
|
int extend_scope (string scope_name, SCOPE_TYPE vars) |
|
|
{ |
if (scopes[scope_name]) { |
SCOPE_TYPE oldvars; |
if (scope_name == "_") { |
array(SCOPE_TYPE) hid; |
for (Frame f = frame; f; f = f->up) |
if (array(SCOPE_TYPE) h = hidden[f]) hid = h; |
if (hid) oldvars = hid[0]; |
else oldvars = scopes["_"]; |
} |
else { |
Frame outermost; |
for (Frame f = frame; f; f = f->up) |
if (f->scope_name == scope_name) outermost = f; |
if (outermost) oldvars = hidden[outermost][1]; |
else oldvars = scopes[scope_name]; |
} |
#ifdef DEBUG |
if (!oldvars) fatal_error ("I before e except after c.\n"); |
#endif |
if (!mappingp(vars)) { |
return 0; |
} |
foreach (indices(vars), string var) |
set_var(var, vars[var], scope_name); |
} |
else scopes[scope_name] = vars; |
return 1; |
} |
|
void remove_scope (string scope_name) |
|
{ |
#ifdef MODULE_DEBUG |
if (scope_name == "_") fatal_error ("Cannot remove current scope.\n"); |
#endif |
Frame outermost; |
for (Frame f = frame; f; f = f->up) |
if (f->scope_name == scope_name) outermost = f; |
if (outermost) m_delete (hidden, outermost); |
else m_delete (scopes, scope_name); |
} |
|
string current_scope() |
|
{ |
if (SCOPE_TYPE vars = scopes["_"]) { |
string scope_name = search (scopes, vars); |
do |
if (scope_name != "_") return scope_name; |
while ((scope_name = search (scopes, vars, scope_name))); |
} |
return 0; |
} |
|
SCOPE_TYPE get_scope (string scope_name) |
|
{ |
return scopes[scope_name]; |
} |
|
void add_runtime_tag (Tag tag) |
|
|
{ |
#ifdef MODULE_DEBUG |
if (tag->plugin_name) |
fatal_error ("Can't currently handle adding of plugin tags at runtime.\n"); |
#endif |
if (!new_runtime_tags) new_runtime_tags = NewRuntimeTags(); |
new_runtime_tags->add_tag (tag); |
} |
|
void remove_runtime_tag (string|Tag tag, void|int proc_instr) |
|
|
|
|
|
{ |
if (!new_runtime_tags) new_runtime_tags = NewRuntimeTags(); |
if (objectp (tag)) tag = tag->name; |
new_runtime_tags->remove_tag (tag); |
} |
|
multiset(Tag) get_runtime_tags() |
|
{ |
mapping(string:Tag) tags = runtime_tags; |
if (new_runtime_tags) tags = new_runtime_tags->filter_tags (tags); |
return mkmultiset (values (tags)); |
} |
|
int incomplete_eval() |
|
|
|
{ |
return unwind_state && unwind_state->reason == "streaming"; |
} |
|
void handle_exception (mixed err, PCode|Parser evaluator, void|int compile_error) |
|
|
{ |
error_count++; |
if (objectp (err) && err->is_RXML_Backtrace) { |
if (evaluator->report_error && evaluator->recover_errors && |
evaluator->type->free_text) { |
string msg; |
if (tag_set && id && id->conf) { |
msg = err->type == "help" ? err->msg : |
(err->type == "run" ? |
([function(Backtrace,Type:string)] |
tag_set->handle_run_error) : |
([function(Backtrace,Type:string)] |
tag_set->handle_parse_error) |
) ([object(Backtrace)] err, evaluator->type); |
if(!msg) |
msg = describe_error(err); |
} |
else |
msg = err->msg; |
if (evaluator->report_error (msg)) { |
if (compile_error && evaluator->p_code) |
evaluator->p_code->add (CompiledError (err)); |
return; |
} |
} |
throw (err); |
} |
else throw_fatal (err); |
} |
|
final array(mixed|PCode) eval_and_compile (Type type, string to_parse) |
|
|
|
|
{ |
mixed res; |
PCode p_code; |
|
|
|
Parser parser = type->get_parser (this_object(), tag_set, 0, 1); |
parser->write_end (to_parse); |
res = parser->eval(); |
p_code = parser->p_code; |
parser->p_code = 0; |
|
|
|
return ({res, p_code}); |
} |
|
|
|
final Parser new_parser (Type top_level_type, void|int make_p_code) |
|
|
{ |
#ifdef MODULE_DEBUG |
if (in_use || frame) fatal_error ("Context already in use.\n"); |
#endif |
return top_level_type->get_parser (this_object(), tag_set, 0, make_p_code); |
} |
|
#ifdef DEBUG |
private int eval_finished = 0; |
private mixed foo; |
#endif |
|
final void eval_finish() |
|
{ |
if (!frame_depth && tag_set) { |
#ifdef DEBUG |
if (eval_finished) fatal_error ("Context already finished.\n" + foo); |
eval_finished = 1; |
foo = describe_backtrace (backtrace()); |
#endif |
tag_set->call_eval_finish_funs (this_object()); |
} |
} |
|
mapping(string:SCOPE_TYPE) scopes = ([]); |
|
|
|
mapping(Frame:array(SCOPE_TYPE)) hidden = ([]); |
|
|
|
|
void enter_scope (Frame frame) |
{ |
#ifdef DEBUG |
if (!frame->vars) fatal_error ("Frame has no variables.\n"); |
#endif |
if (string scope_name = [string] frame->scope_name) { |
if (!hidden[frame]) |
hidden[frame] = ({scopes["_"], scopes[scope_name]}); |
scopes["_"] = scopes[scope_name] = [SCOPE_TYPE] frame->vars; |
} |
else { |
if (!hidden[frame]) |
hidden[frame] = ({scopes["_"], 0}); |
scopes["_"] = [SCOPE_TYPE] frame->vars; |
} |
} |
|
void leave_scope (Frame frame) |
{ |
if (array(SCOPE_TYPE) back = hidden[frame]) { |
if (SCOPE_TYPE cur = back[0]) scopes["_"] = cur; |
else m_delete (scopes, "_"); |
if (SCOPE_TYPE named = back[1]) { |
#ifdef MODULE_DEBUG |
if (!stringp (frame->scope_name)) |
fatal_error ("Scope named changed to %O during parsing.\n", frame->scope_name); |
#endif |
scopes[[string] frame->scope_name] = named; |
} |
else m_delete (scopes, [string] frame->scope_name); |
m_delete (hidden, frame); |
} |
} |
|
#define ENTER_SCOPE(ctx, frame) \ |
(frame->vars && frame->vars != ctx->scopes["_"] && ctx->enter_scope (frame)) |
#define LEAVE_SCOPE(ctx, frame) \ |
(frame->vars && ctx->leave_scope (frame)) |
|
mapping(string:Tag) runtime_tags = ([]); |
|
|
|
NewRuntimeTags new_runtime_tags; |
|
|
|
void create (void|TagSet _tag_set, void|RequestID _id) |
|
{ |
tag_set = _tag_set; |
id = _id; |
#ifdef RXML_OBJ_DEBUG |
__object_marker->create (this_object()); |
#endif |
} |
|
UNWIND_STATE unwind_state; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MARK_OBJECT_ONLY; |
|
string _sprintf() {return "RXML.Context" + OBJ_COUNT;} |
|
#ifdef MODULE_DEBUG |
#if constant (thread_create) |
Thread.Thread in_use; |
#else |
int in_use; |
#endif |
#endif |
} |
|
static class NewRuntimeTags |
|
{ |
static mapping(string:Tag) add_tags; |
static mapping(string:int|string) remove_tags; |
|
void add_tag (Tag tag) |
{ |
if (!add_tags) add_tags = ([]); |
if (tag->flags & FLAG_PROC_INSTR) { |
add_tags["?" + tag->name] = tag; |
|
if (remove_tags) m_delete (remove_tags, "?" + tag->name); |
} |
else { |
add_tags[tag->name] = tag; |
if (remove_tags) m_delete (remove_tags, tag->name); |
} |
} |
|
void remove_tag (string name, int proc_instr) |
{ |
if (!remove_tags) remove_tags = ([]); |
if (proc_instr) remove_tags["?" + name] = name; |
else remove_tags[name] = 1; |
} |
|
array(Tag) added_tags() |
{ |
if (!add_tags) return ({}); |
if (remove_tags) return values (add_tags - remove_tags); |
return values (add_tags); |
} |
|
array(string) removed_tags() |
{ |
return remove_tags ? indices (filter (remove_tags, intp)) : ({}); |
} |
|
array(string) removed_pi_tags() |
{ |
return remove_tags ? values (remove_tags) - ({1}) : ({}); |
} |
|
mapping(string:Tag) filter_tags (mapping(string:Tag) tags) |
{ |
if (add_tags) tags |= add_tags; |
if (remove_tags) tags -= remove_tags; |
return tags; |
} |
} |
|
class Backtrace |
|
{ |
constant is_generic_error = 1; |
constant is_RXML_Backtrace = 1; |
|
string type; |
string msg; |
Context context; |
array(Frame) frames; |
string current_var; |
array backtrace; |
|
void create (void|string _type, void|string _msg, void|Context _context, |
void|array _backtrace) |
{ |
type = _type; |
msg = _msg; |
if (context = _context || RXML_CONTEXT) { |
frames = allocate (context->frame_depth); |
Frame frame = context->frame; |
int i = 0; |
for (; frame; i++, frame = frame->up) frames[i] = frame; |
frames = frames[..i - 1]; |
} |
if (_backtrace) backtrace = _backtrace; |
else { |
backtrace = predef::backtrace(); |
backtrace = backtrace[..sizeof (backtrace) - 2]; |
} |
} |
|
string describe_rxml_backtrace (void|int no_msg) |
|
{ |
String.Buffer txt = String.Buffer(); |
function(string...:void) add = txt->add; |
add (no_msg ? "" : "RXML" + (type ? " " + type : "") + " error"); |
if (context) { |
if (!no_msg) add (": ", msg || "(no error message)\n"); |
if (current_var) add (" | &", current_var, ";\n"); |
foreach (frames, Frame f) { |
string name; |
if (f->tag) name = f->tag->name; |
|
else name = "(unknown)"; |
if (f->flags & FLAG_PROC_INSTR) |
add (" | <?", name, "?>\n"); |
else { |
add (" | <", name); |
if (mappingp (f->args)) |
foreach (sort (indices (f->args)), string arg) { |
mixed val = f->args[arg]; |
add (" ", arg, "="); |
if (arrayp (val)) add (map (val, error_print_val) * ","); |
else add (error_print_val (val)); |
} |
else add (" (no argmap)"); |
add (">\n"); |
} |
} |
} |
else |
if (!no_msg) add (" (no context): ", msg || "(no error message)\n"); |
return txt->get(); |
} |
|
private string error_print_val (mixed val) |
{ |
if (arrayp (val)) return "array"; |
else if (mappingp (val)) return "mapping"; |
else if (multisetp (val)) return "multiset"; |
else return sprintf ("%O", val); |
} |
|
string|array `[] (int i) |
{ |
switch (i) { |
case 0: return describe_rxml_backtrace(); |
case 1: return backtrace; |
} |
} |
|
string _sprintf() {return "RXML.Backtrace(" + (type || "") + ")";} |
} |
|
|
|
|
final void set_context (Context ctx) {SET_RXML_CONTEXT (ctx);} |
|
final Context get_context() {return [object(Context)] RXML_CONTEXT;} |
|
|
|
|
|
|
|
|
#if defined (MODULE_DEBUG) && constant (thread_create) |
|
|
|
#define ENTER_CONTEXT(ctx) \ |
Context __old_ctx = RXML_CONTEXT; \ |
SET_RXML_CONTEXT (ctx); \ |
if (ctx) { \ |
if (ctx->in_use && ctx->in_use != this_thread()) \ |
fatal_error ("Attempt to use context asynchronously.\n"); \ |
ctx->in_use = this_thread(); \ |
} |
|
#define LEAVE_CONTEXT() \ |
if (Context ctx = RXML_CONTEXT) \ |
if (__old_ctx != ctx) ctx->in_use = 0; \ |
SET_RXML_CONTEXT (__old_ctx); |
|
#else |
|
#define ENTER_CONTEXT(ctx) \ |
Context __old_ctx = RXML_CONTEXT; \ |
SET_RXML_CONTEXT (ctx); |
|
#define LEAVE_CONTEXT() \ |
SET_RXML_CONTEXT (__old_ctx); |
|
#endif |
|
|
|
|
constant FLAG_NONE = 0x00000000; |
|
|
constant FLAG_DEBUG = 0x40000000; |
|
|
|
|
|
|
|
|
constant FLAG_PROC_INSTR = 0x00000010; |
|
|
|
|
|
constant FLAG_COMPAT_PARSE = 0x00000002; |
|
|
|
|
|
constant FLAG_NO_PREFIX = 0x00000004; |
|
|
constant FLAG_SOCKET_TAG = 0x00000008; |
|
|
|
constant FLAG_DONT_PREPARSE = 0x00000040; |
|
|
|
|
|
constant FLAG_POSTPARSE = 0x00000080; |
|
|
|
|
|
constant FLAG_EMPTY_ELEMENT = 0x00000001; |
|
|
|
|
|
|
constant FLAG_PARENT_SCOPE = 0x00000100; |
|
|
|
constant FLAG_NO_IMPLICIT_ARGS = 0x00000200; |
|
|
|
constant FLAG_STREAM_RESULT = 0x00000400; |
|
|
|
constant FLAG_STREAM_CONTENT = 0x00000800; |
|
|
|
|
|
|
|
|
|
constant FLAG_STREAM = FLAG_STREAM_RESULT | FLAG_STREAM_CONTENT; |
|
constant FLAG_UNPARSED = 0x00001000; |
|
|
|
|
|
constant FLAG_DONT_RECOVER = 0x00002000; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constant FLAG_DONT_REPORT_ERRORS = FLAG_DONT_RECOVER; |
|
constant FLAG_RAW_ARGS = 0x00004000; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constant FLAG_CACHE_DIFF_ARGS = 0x00010000; |
|
|
|
constant FLAG_CACHE_DIFF_CONTENT = 0x00020000; |
|
|
constant FLAG_CACHE_DIFF_RESULT_TYPE = 0x00040000; |
|
|
|
constant FLAG_CACHE_DIFF_VARS = 0x00080000; |
|
|
|
|
constant FLAG_CACHE_DIFF_TAG_INSTANCE = 0x00100000; |
|
|
|
constant FLAG_CACHE_EXECUTE_RESULT = 0x00200000; |
|
|
|
|
class Frame |
|
|
|
|
|
{ |
constant is_RXML_Frame = 1; |
constant thrown_at_unwind = 1; |
|
|
|
Frame up; |
|
|
|
|
Tag tag; |
|
|
int flags; |
|
|
|
|
|
|
|
|
|
|
|
mapping(string:mixed)|int(1..1) args; |
|
|
|
|
|
|
|
|
|
Type content_type; |
|
|
mixed content; |
|
|
|
|
Type result_type; |
|
|
|
|
|
|
|
|
|
|
|
mixed result; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
optional array cached_return (Context ctx, void|mixed piece); |
|
|
|
|
|
this_program clone() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ |
this_program this = this_object(), clone = object_program (this)(); |
clone->tag = tag; |
clone->flags = flags; |
clone->args = args; |
clone->content_type = content_type; |
clone->content = content; |
clone->result_type = result_type; |
if (string v = this->scope_name) clone->scope_name = v; |
if (TagSet v = this->additional_tags) clone->additional_tags = v; |
if (TagSet v = this->local_tags) clone->local_tags = v; |
if (string v = this->raw_tag_text) clone->raw_tag_text = v; |
return clone; |
} |
|
|
|
final mixed get_var (string var, void|string scope_name, void|Type want_type) |
|
{ |
return RXML_CONTEXT->get_var (var, scope_name, want_type); |
} |
|
final mixed set_var (string var, mixed val, void|string scope_name) |
|
{ |
return RXML_CONTEXT->set_var (var, val, scope_name); |
} |
|
final void delete_var (string var, void|string scope_name) |
|
{ |
RXML_CONTEXT->delete_var (var, scope_name); |
} |
|
void run_error (string msg, mixed... args) |
|
{ |
_run_error (msg, @args); |
} |
|
void parse_error (string msg, mixed... args) |
|
{ |
_parse_error (msg, @args); |
} |
|
void tag_debug (string msg, mixed... args) |
|
|
{ |
if (flags & FLAG_DEBUG) report_debug (msg, @args); |
} |
|
void terminate() |
|
|
{ |
fatal_error ("FIXME\n"); |
} |
|
void suspend() |
|
|
|
|
|
{ |
fatal_error ("FIXME\n"); |
} |
|
void resume() |
|
|
{ |
fatal_error ("FIXME\n"); |
} |
|
mapping(string:Tag) get_plugins() |
|
|
|
|
|
|
{ |
#ifdef MODULE_DEBUG |
if (!(tag->flags & FLAG_SOCKET_TAG)) |
fatal_error ("This tag is not a socket tag.\n"); |
#endif |
return RXML_CONTEXT->tag_set->get_plugins (tag->name, tag->flags & FLAG_PROC_INSTR); |
} |
|
final Tag get_overridden_tag() |
|
|
{ |
return RXML_CONTEXT->tag_set->get_overridden_tag (tag); |
} |
|
Frame|string propagate_tag (void|mapping(string:string) args, void|string content) |
|
|
|
|
|
|
|
|
|
{ |
Frame this = this_object(); |
#ifdef MODULE_DEBUG |
#define CHECK_RAW_TEXT \ |
if (zero_type (this->raw_tag_text)) \ |
fatal_error ("The variable raw_tag_text must be defined.\n"); \ |
if (!stringp (this->raw_tag_text)) \ |
fatal_error ("raw_tag_text must have a string value.\n"); |
#else |
#define CHECK_RAW_TEXT |
#endif |
|
|
if (object(Tag) overridden = get_overridden_tag()) { |
Frame frame; |
if (flags & FLAG_PROC_INSTR) { |
if (!content) { |
CHECK_RAW_TEXT; |
content = t_xml->parse_tag (this->raw_tag_text)[2]; |
#ifdef DEBUG |
if (!stringp (content)) |
fatal_error ("Failed to parse PI tag content for <?%s?> from %O.\n", |
tag->name, this->raw_tag_text); |
#endif |
} |
} |
else if (!args || !content && !(flags & FLAG_EMPTY_ELEMENT)) { |
CHECK_RAW_TEXT; |
string ignored; |
[ignored, args, content] = t_xml->parse_tag (this->raw_tag_text); |
#ifdef DEBUG |
if (!mappingp (args)) |
fatal_error ("Failed to parse tag args for <%s> from %O.\n", |
tag->name, this->raw_tag_text); |
if (!stringp (content) && !(flags & FLAG_EMPTY_ELEMENT)) |
fatal_error ("Failed to parse tag content for <%s> from %O.\n", |
tag->name, this->raw_tag_text); |
#endif |
} |
frame = overridden (args, content || ""); |
frame->flags |= FLAG_UNPARSED; |
return frame; |
} |
|
else { |
CHECK_RAW_TEXT; |
|
|
if (flags & FLAG_PROC_INSTR) { |
if (content) { |
string name; |
[name, args, content] = t_xml->parse_tag (this->raw_tag_text); |
return result_type->format_tag (name, 0, content, tag->flags); |
} |
else |
return this->raw_tag_text; |
} |
|
else { |
string s; |
if (!args || !content && !(flags & FLAG_EMPTY_ELEMENT)) { |
#ifdef MODULE_DEBUG |
if (mixed err = catch { |
#endif |
s = t_xml (PXml)->eval (this->raw_tag_text, |
RXML_CONTEXT, empty_tag_set); |
#ifdef MODULE_DEBUG |
}) { |
if (objectp (err) && ([object] err)->thrown_at_unwind) |
fatal_error ("Can't save parser state when evaluating arguments.\n"); |
throw_fatal (err); |
} |
#endif |
if (!args && !content) return s; |
} |
else s = this->raw_tag_text; |
|
[string name, mapping(string:string) parsed_args, |
string parsed_content] = t_xml->parse_tag (this->raw_tag_text); |
#ifdef DEBUG |
if (!mappingp (parsed_args)) |
fatal_error ("Failed to parse tag args for <%s> from %O.\n", |
tag->name, this->raw_tag_text); |
if (!stringp (parsed_content)) |
fatal_error ("Failed to parse tag content for <%s> from %O.\n", |
tag->name, this->raw_tag_text); |
#endif |
if (!args) args = parsed_args; |
if (!content && !(flags & FLAG_EMPTY_ELEMENT)) content = parsed_content; |
return result_type->format_tag (name, args, content, tag->flags); |
} |
#undef CHECK_RAW_TEXT |
} |
} |
|
|
|
#ifdef DEBUG |
# define THIS_TAG_TOP_DEBUG(msg, args...) \ |
(TAG_DEBUG_TEST (flags) && report_debug ("%O: " + (msg), this_object(), args), 0) |
# define THIS_TAG_DEBUG(msg, args...) \ |
(TAG_DEBUG_TEST (flags) && report_debug ("%O: " + (msg), this_object(), args), 0) |
# define THIS_TAG_DEBUG_ENTER_SCOPE(ctx, this) \ |
if (this->vars && ctx->scopes["_"] != this->vars) \ |
THIS_TAG_DEBUG ("(Re)entering scope %O\n", this->scope_name) |
# define THIS_TAG_DEBUG_LEAVE_SCOPE(ctx, this) \ |
if (this->vars && ctx->scopes["_"] == this->vars) \ |
THIS_TAG_DEBUG ("Leaving scope %O\n", this->scope_name) |
#else |
# define THIS_TAG_TOP_DEBUG(msg, args...) 0 |
# define THIS_TAG_DEBUG(msg, args...) 0 |
# define THIS_TAG_DEBUG_ENTER_SCOPE(ctx, this) 0 |
# define THIS_TAG_DEBUG_LEAVE_SCOPE(ctx, this) 0 |
#endif |
|
#define SET_SEQUENTIAL(from, to, desc) \ |
do { \ |
THIS_TAG_DEBUG ("Adding %s to " desc "\n", \ |
utils->format_short (from)); \ |
/* Keep only one ref to to to allow destructive change. */ \ |
to = to + (to = 0, from); \ |
} while (0) |
|
#define SET_NONSEQUENTIAL(from, to, to_type, desc) \ |
do { \ |
if (from != nil) { \ |
if (to != nil) \ |
parse_error ( \ |
"Cannot append another value %s to non-sequential " desc \ |
" of type %s.\n", utils->format_short (from), \ |
to_type->name); \ |
THIS_TAG_DEBUG ("Setting " desc " to %s\n", \ |
utils->format_short (from)); \ |
to = from; \ |
} \ |
} while (0) |
|
#define CONVERT_VALUE(from, from_type, to, to_type, desc) \ |
do { \ |
if (from_type->name != to_type->name) { \ |
THIS_TAG_DEBUG (desc, from_type->name, to_type->name); \ |
to = to_type->encode (from, from_type); \ |
} \ |
else to = from; \ |
} while (0) |
|
#define CONV_RESULT(from, from_type, to, to_type) \ |
CONVERT_VALUE(from, from_type, to, to_type, \ |
"Converting result from %s to %s of surrounding content\n") |
|
private void _exec_array_fatal (string where, int pos, mixed elem, |
string msg, mixed... args) |
{ |
if (sizeof (args)) msg = sprintf (msg, args); |
fatal_error ("Position %d in exec array from %s is %s: %s", pos, where, elem, msg); |
}; |
|
mixed _exec_array (Context ctx, TagSetParser|PCode evaler, array exec, string where) |
{ |
Frame this = this_object(); |
int i = 0, parent_scope = flags & FLAG_PARENT_SCOPE; |
mixed res = nil; |
Parser subparser = 0; |
|
mixed err = catch { |
if (parent_scope) { |
THIS_TAG_DEBUG_LEAVE_SCOPE (ctx, this); |
LEAVE_SCOPE (ctx, this); |
} |
|
for (; i < sizeof (exec); i++) { |
mixed elem = exec[i], piece = nil; |
|
switch (sprintf ("%t", elem)) { |
case "string": |
if (result_type->parser_prog == PNone) { |
THIS_TAG_DEBUG ("Exec[%d]: String %s\n", i, utils->format_short (elem)); |
piece = elem; |
} |
else { |
subparser = result_type->get_parser (ctx, 0, evaler); |
if (evaler->recover_errors && !(flags & FLAG_DONT_RECOVER)) |
subparser->recover_errors = 1; |
THIS_TAG_DEBUG ("Exec[%d]: Parsing string %s with %O\n", i, |
utils->format_short (elem), subparser); |
subparser->finish ([string] elem); |
piece = subparser->eval(); |
subparser = 0; |
} |
break; |
|
case "mapping": |
THIS_TAG_DEBUG ("Exec[%d]: Response mapping\n", i); |
_exec_array_fatal (where, i, elem, |
"Response mappings not yet implemented.\n"); |
break; |
|
case "multiset": |
if (sizeof ([multiset] elem) == 1) { |
piece = ((array) elem)[0]; |
THIS_TAG_DEBUG ("Exec[%d]: Verbatim value %s\n", i, |
utils->format_short (piece)); |
} |
else |
_exec_array_fatal (where, i, elem, |
"Not exactly one value in multiset.\n"); |
break; |
|
default: |
if (objectp (elem)) |
|
|
if (([object] elem)->is_RXML_Frame) { |
THIS_TAG_DEBUG ("Exec[%d]: Evaluating frame %O\n", i, ([object] elem)); |
piece = ([object(Frame)] elem)->_eval ( |
ctx, evaler, result_type); |
break; |
} |
|
|
|
|
|
|
|
|
else if (([object] elem)->is_RXML_Parser) { |
|
THIS_TAG_DEBUG ("Exec[%d]: Continuing eval of frame %O\n", |
i, ([object] elem)); |
([object(Parser)] elem)->finish(); |
piece = ([object(Parser)] elem)->eval(); |
break; |
} |
_exec_array_fatal (where, i, elem, "Not a valid type.\n"); |
} |
|
if (result_type->sequential) SET_SEQUENTIAL (piece, res, "result"); |
else SET_NONSEQUENTIAL (piece, result, result_type, "result"); |
} |
|
if (result_type->sequential) result = result + (result = 0, res); |
else res = result; |
|
if (parent_scope) { |
THIS_TAG_DEBUG_ENTER_SCOPE (ctx, this); |
ENTER_SCOPE (ctx, this); |
} |
return res; |
}; |
|
if (result_type->sequential) result = result + (result = 0, res); |
if (parent_scope) { |
THIS_TAG_DEBUG_ENTER_SCOPE (ctx, this); |
ENTER_SCOPE (ctx, this); |
} |
|
if (objectp (err) && ([object] err)->thrown_at_unwind) { |
THIS_TAG_DEBUG ("Exec: Interrupted at position %d\n", i); |
UNWIND_STATE ustate; |
if ((ustate = ctx->unwind_state) && !zero_type (ustate->stream_piece)) { |
|
if (result_type->name != evaler->type->name) |
res = evaler->type->encode (res, result_type); |
if (result_type->sequential) |
SET_SEQUENTIAL (ustate->stream_piece, res, "stream piece"); |
else |
SET_NONSEQUENTIAL (ustate->stream_piece, res, result_type, "stream piece"); |
ustate->stream_piece = res; |
} |
ustate->exec_left = exec[i..]; |
if (subparser) |
|
|
|
([array] ustate->exec_left)[0] = subparser; |
throw (err); |
} |
throw_fatal (err); |
} |
|
private void _handle_runtime_tags (Context ctx, TagSetParser|PCode evaler) |
{ |
if (evaler->is_RXML_Parser) { |
array(Tag) arr_add_tags = ctx->new_runtime_tags->added_tags(); |
array(string) arr_rem_tags = ctx->new_runtime_tags->removed_tags(); |
array(string) arr_rem_pi_tags = ctx->new_runtime_tags->removed_pi_tags(); |
for (Parser p = evaler; p; p = p->_parent) |
if (p->tag_set_eval && !p->_local_tag_set && p->add_runtime_tag) { |
foreach (arr_add_tags, Tag tag) { |
THIS_TAG_DEBUG ("Adding runtime tag %O\n", tag); |
([object(TagSetParser)] p)->add_runtime_tag (tag); |
} |
foreach (arr_rem_tags, string tag) { |
THIS_TAG_DEBUG ("Removing runtime tag %s\n", tag); |
([object(TagSetParser)] p)->remove_runtime_tag (tag); |
} |
foreach (arr_rem_pi_tags, string tag) { |
THIS_TAG_DEBUG ("Removing runtime tag %s\n", tag); |
([object(TagSetParser)] p)->remove_runtime_tag (tag, 1); |
} |
} |
} |
|
|
|
ctx->runtime_tags = ctx->new_runtime_tags->filter_tags (ctx->runtime_tags); |
ctx->new_runtime_tags = 0; |
} |
|
#define LOW_CALL_CALLBACK(res, cb, args...) \ |
do { \ |
THIS_TAG_DEBUG ("Calling " #cb "\n"); \ |
COND_PROF_ENTER(tag,tag->name,"tag"); \ |
res = (cb) (args); /* Might unwind. */ \ |
COND_PROF_LEAVE(tag,tag->name,"tag"); \ |
} while (0) |
|
#define EXEC_CALLBACK(ctx, evaler, exec, cb, args...) \ |
do { \ |
if (!exec) \ |
if (arrayp (cb)) { \ |
THIS_TAG_DEBUG ("Getting exec array from " #cb "\n"); \ |
exec = [array] cb; \ |
} \ |
else { \ |
LOW_CALL_CALLBACK (exec, cb, args); \ |
THIS_TAG_DEBUG ((exec ? "Exec array of length " + \ |
sizeof (exec) : "Zero") + \ |
" returned from " #cb "\n"); \ |
THIS_TAG_DEBUG_ENTER_SCOPE (ctx, this); \ |
ENTER_SCOPE (ctx, this); \ |
if (ctx->new_runtime_tags) \ |
_handle_runtime_tags (ctx, evaler); \ |
} \ |
} while (0) |
|
#define EXEC_ARRAY(ctx, evaler, exec, cb) \ |
do { \ |
if (exec) { \ |
mixed res = \ |
_exec_array (ctx, evaler, exec, #cb); /* Might unwind. */ \ |
if (flags & FLAG_STREAM_RESULT) { \ |
DO_IF_DEBUG ( \ |
if (ctx->unwind_state) \ |
fatal_error ("Clobbering unwind_state to do streaming.\n"); \ |
if (piece != nil) \ |
fatal_error ("Thanks, we think about how nice it must be " \ |
"to play the harmonica...\n"); \ |
); \ |
CONV_RESULT (res, result_type, res, type); \ |
ctx->unwind_state = (["stream_piece": res, \ |
"reason": "streaming"]); \ |
THIS_TAG_DEBUG ("Streaming %s from " #cb "\n", \ |
utils->format_short (res)); \ |
throw (this); \ |
} \ |
exec = 0; \ |
} \ |
} while (0) |
|
mapping(string:mixed) _eval_args (Context ctx, |
mapping(string:string) raw_args, |
mapping(string:Type) my_req_args) |
|
|
{ |
|
mapping(string:Type) atypes = |
raw_args & (tag->req_arg_types | tag->opt_arg_types); |
if (my_req_args) { |
mapping(string:Type) missing = my_req_args - atypes; |
if (sizeof (missing)) |
parse_error ("Required " + |
(sizeof (missing) > 1 ? |
"arguments " + String.implode_nicely ( |
sort (indices (missing))) + " are" : |
"argument " + indices (missing)[0] + " is") + " missing.\n"); |
} |
|
#ifdef MODULE_DEBUG |
if (mixed err = catch { |
#endif |
foreach (indices (raw_args), string arg) { |
Type t = atypes[arg] || tag->def_arg_type; |
if (t->parser_prog != PNone) { |
Parser parser = t->get_parser (ctx, 0, 0); |
THIS_TAG_DEBUG ("Evaluating argument value %s with %O\n", |
utils->format_short (raw_args[arg]), parser); |
parser->finish (raw_args[arg]); |
raw_args[arg] = parser->eval(); |
THIS_TAG_DEBUG ("Setting dynamic argument %s to %s\n", |
utils->format_short (arg), |
utils->format_short (raw_args[arg])); |
t->give_back (parser); |
} |
} |
#ifdef MODULE_DEBUG |
}) { |
if (objectp (err) && ([object] err)->thrown_at_unwind) |
fatal_error ("Can't save parser state when evaluating dynamic arguments.\n"); |
throw_fatal (err); |
} |
#endif |
|
return raw_args; |
} |
|
EVAL_ARGS_FUNC|string _prepare (Context ctx, Type type, |
mapping(string:string) raw_args, |
PCode p_code) |
|
|
|
{ |
Frame this = this_object(); |
|
mixed err = catch { |
if (ctx->frame_depth >= ctx->max_frame_depth) |
_run_error ("Too deep recursion -- exceeding %d nested tags.\n", |
ctx->max_frame_depth); |
|
EVAL_ARGS_FUNC|string func; |
|
if (raw_args) { |
#ifdef MODULE_DEBUG |
if (flags & FLAG_PROC_INSTR) |
fatal_error ("Can't pass arguments to a processing instruction tag.\n"); |
#endif |
|
#ifdef MAGIC_HELP_ARG |
if (raw_args->help) { |
func = utils->return_help_arg; |
args = raw_args; |
} |
else |
#endif |
if (sizeof (raw_args)) { |
|
|
string splice_arg = raw_args["::"]; |
if (splice_arg) m_delete (raw_args, "::"); |
else splice_arg = 0; |
mapping(string:Type) splice_req_types; |
|
mapping(string:Type) atypes = raw_args & tag->req_arg_types; |
if (sizeof (atypes) < sizeof (tag->req_arg_types)) |
if (splice_arg) |
splice_req_types = tag->req_arg_types - atypes; |
else { |
array(string) missing = sort (indices (tag->req_arg_types - atypes)); |
parse_error ("Required " + |
(sizeof (missing) > 1 ? |
"arguments " + String.implode_nicely (missing) + " are" : |
"argument " + missing[0] + " is") + " missing.\n"); |
} |
atypes += raw_args & tag->opt_arg_types; |
|
String.Buffer fn_text = p_code && String.Buffer(); |
|
if (splice_arg) { |
|
Parser p = splice_arg_type->get_parser (ctx, 0, 0, p_code); |
THIS_TAG_DEBUG ("Evaluating splice argument %s\n", |
utils->format_short (splice_arg)); |
#ifdef MODULE_DEBUG |
if (mixed err = catch { |
#endif |
p->finish (splice_arg); |
splice_arg = p->eval(); |
#ifdef MODULE_DEBUG |
}) { |
if (objectp (err) && ([object] err)->thrown_at_unwind) |
fatal_error ("Can't save parser state when " |
"evaluating splice argument.\n"); |
throw_fatal (err); |
} |
#endif |
if (p_code) |
fn_text->add ( |
"return ", p_code->bind (_eval_args), " (context, " |
"RXML.xml_tag_parser->parse_tag_args ((", |
p->p_code->_compile_text(), ") || \"\"), ", |
p_code->bind (splice_req_types), ") + ([\n"); |
splice_arg_type->give_back (p); |
args = _eval_args (ctx, xml_tag_parser->parse_tag_args (splice_arg || ""), |
splice_req_types); |
} |
else { |
args = raw_args; |
if (p_code) fn_text->add ("return ([\n"); |
} |
|
#ifdef MODULE_DEBUG |
if (mixed err = catch { |
#endif |
if (p_code) |
foreach (indices (raw_args), string arg) { |
Type t = atypes[arg] || tag->def_arg_type; |
if (t->parser_prog != PNone) { |
Parser parser = t->get_parser (ctx, 0, 0, p_code); |
THIS_TAG_DEBUG ("Evaluating and compiling " |
"argument value %s with %O\n", |
utils->format_short (raw_args[arg]), parser); |
parser->finish (raw_args[arg]); |
args[arg] = parser->eval(); |
THIS_TAG_DEBUG ("Setting argument %s to %s\n", |
utils->format_short (arg), |
utils->format_short (args[arg])); |
fn_text->add (sprintf ("%O: %s,\n", arg, |
parser->p_code->_compile_text())); |
t->give_back (parser); |
} |
else { |
args[arg] = raw_args[arg]; |
fn_text->add (sprintf ("%O: %s,\n", arg, |
p_code->bind (raw_args[arg]))); |
} |
} |
else |
foreach (indices (raw_args), string arg) { |
Type t = atypes[arg] || tag->def_arg_type; |
if (t->parser_prog != PNone) { |
Parser parser = t->get_parser (ctx, 0, 0, 0); |
THIS_TAG_DEBUG ("Evaluating argument value %s with %O\n", |
utils->format_short (raw_args[arg]), parser); |
parser->finish (raw_args[arg]); |
args[arg] = parser->eval(); |
THIS_TAG_DEBUG ("Setting argument %s to %s\n", |
utils->format_short (arg), |
utils->format_short (args[arg])); |
t->give_back (parser); |
} |
else |
args[arg] = raw_args[arg]; |
} |
#ifdef MODULE_DEBUG |
}) { |
if (objectp (err) && ([object] err)->thrown_at_unwind) |
fatal_error ("Can't save parser state when evaluating arguments.\n"); |
throw_fatal (err); |
} |
#endif |
|
if (p_code) { |
fn_text->add ("]);\n"); |
func = p_code->add_func ( |
"mapping(string:mixed)", "RXML.Context context", fn_text->get()); |
} |
} |
else { |
func = utils->return_empty_mapping; |
args = raw_args; |
} |
} |
else |
func = utils->return_zero; |
|
if (!zero_type (this->parent_frame)) |
if (up->local_tags && up->local_tags->has_tag (tag)) { |
THIS_TAG_DEBUG ("Setting parent_frame to %O from local_tags\n", up); |
this->parent_frame = up; |
} |
else { |
int nest = 1; |
Frame frame = up; |
for (; frame; frame = frame->up) |
if (frame->additional_tags && frame->additional_tags->has_tag (tag)) { |
if (!--nest) break; |
} |
else if (frame->tag == tag) nest++; |
THIS_TAG_DEBUG ("Setting parent_frame to %O from additional_tags\n", frame); |
this->parent_frame = frame; |
break; |
} |
|
if (!result_type) { |
#ifdef MODULE_DEBUG |
if (!tag) fatal_error ("result_type not set in Frame object %O, " |
"and it has no Tag object to use for inferring it.\n", |
this); |
#endif |
find_result_type: { |
|
|
foreach (tag->result_types, Type rtype) |
if (rtype->subtype_of (type)) { |
result_type = rtype; |
break find_result_type; |
} |
|
|
|
|
foreach (tag->result_types, Type rtype) |
if (type->subtype_of (rtype)) { |
result_type = type (rtype->parser_prog, @rtype->parser_args); |
break find_result_type; |
} |
parse_error ( |
"Tag returns %s but %s is expected.\n", |
String.implode_nicely ([array(string)] tag->result_types->name, "or"), |
type->name); |
} |
THIS_TAG_DEBUG ("Resolved result_type to %s from surrounding %s\n", |
result_type->name, type->name); |
} |
else THIS_TAG_DEBUG ("Keeping result_type %s\n", result_type->name); |
|
if (!content_type) { |
#ifdef MODULE_DEBUG |
if (!tag) fatal_error ("content_type not set in Frame object %O, " |
"and it has no Tag object to use for inferring it.\n", |
this); |
#endif |
content_type = tag->content_type; |
if (content_type == t_same) { |
content_type = |
result_type (content_type->parser_prog, @content_type->parser_args); |
THIS_TAG_DEBUG ("Resolved t_same to content_type %O\n", content_type); |
} |
else THIS_TAG_DEBUG ("Setting content_type to %O from tag\n", content_type); |
} |
else THIS_TAG_DEBUG ("Keeping content_type %O\n", content_type); |
|
return func; |
}; |
throw (err); |
} |
|
mixed _eval (Context ctx, TagSetParser|PCode evaler, Type type, |
void|EVAL_ARGS_FUNC in_args, void|string|PCode in_content) |
|
|
{ |
Frame this = this_object(); |
RequestID id = ctx->id; |
|
|
#define EVSTAT_BEGIN 0 |
#define EVSTAT_ENTERED 1 |
#define EVSTAT_LAST_ITER 2 |
#define EVSTAT_ITER_DONE 3 |
int eval_state = EVSTAT_BEGIN; |
|
|
int iter; |
#ifdef DEBUG |
int debug_iter = 1; |
#endif |
object(Parser)|object(PCode) subevaler; |
mixed piece; |
array exec = 0; |
TagSet orig_tag_set; |
|
|
#define PRE_INIT_ERROR(X...) (ctx->frame = this, fatal_error (X)) |
#ifdef DEBUG |
|
if (ctx != RXML_CONTEXT) |
PRE_INIT_ERROR ("Context not current.\n"); |
if (!evaler->tag_set_eval) |
PRE_INIT_ERROR ("Calling _eval() with non-tag set parser.\n"); |
if (up) |
PRE_INIT_ERROR ("up frame already set.\n"); |
#endif |
#ifdef MODULE_DEBUG |
if (ctx->new_runtime_tags) |
PRE_INIT_ERROR ("Looks like Context.add_runtime_tag() or " |
"Context.remove_runtime_tag() was used outside any parser.\n"); |
#endif |
#undef PRE_INIT_ERROR |
|
up = ctx->frame; |
ctx->frame = this; |
ctx->frame_depth++; |
|
mixed conv_result = nil; |
mixed err1 = 0; |
|
process_tag: |
do { |
if ((err1 = catch { |
if (array state = ctx->unwind_state && ctx->unwind_state[this]) { |
#ifdef DEBUG |
if (in_args || in_content) |
fatal_error ("in_args or in_content set when resuming parse.\n"); |
#endif |
object ignored; |
[ignored, eval_state, in_args, in_content, iter, |
subevaler, piece, exec, orig_tag_set, ctx->new_runtime_tags |
#ifdef DEBUG |
, debug_iter |
#endif |
] = state; |
m_delete (ctx->unwind_state, this); |
if (!sizeof (ctx->unwind_state)) ctx->unwind_state = 0; |
THIS_TAG_TOP_DEBUG ("Continuing evaluation" + |
(piece ? " with stream piece\n" : "\n")); |
} |
|
else { |
if (tag) { |
TRACE_ENTER("tag <" + tag->name + ">", tag); |
#ifdef MODULE_LEVEL_SECURITY |
if (id->conf->check_security (tag, id, id->misc->seclevel)) { |
THIS_TAG_TOP_DEBUG ("Access denied - exiting\n"); |
TRACE_LEAVE("access denied"); |
break process_tag; |
} |
#endif |
} |
|
if (in_args) { |
THIS_TAG_TOP_DEBUG ("Evaluating with compiled arguments\n"); |
args = in_args (ctx); |
content = nil; |
} |
else if (flags & FLAG_UNPARSED) { |
#ifdef DEBUG |
if (args && !mappingp (args)) |
fatal_error ("args is not a mapping in unparsed frame.\n"); |
if (!stringp (content)) |
fatal_error ("content is not a string in unparsed frame.\n"); |
#endif |
THIS_TAG_TOP_DEBUG ("Evaluating unparsed\n"); |
in_args = _prepare (ctx, type, args, |
evaler->is_RXML_PCode ? evaler : evaler->p_code); |
in_content = content; |
content = nil; |
flags &= ~FLAG_UNPARSED; |
} |
else if (!in_content) { |
_prepare (ctx, type, 0, 0); |
THIS_TAG_TOP_DEBUG ("Evaluating with constant arguments and content.\n"); |
} |
|
piece = result = nil; |
} |
|
if (TagSet add_tags = [object(TagSet)] this->additional_tags) { |
TagSet tset = ctx->tag_set; |
if (!tset->has_effective_tags (add_tags)) { |
THIS_TAG_DEBUG ("Installing additional_tags %O\n", add_tags); |
int hash = HASH_INT2 (tset->id_number, add_tags->id_number); |
orig_tag_set = tset; |
TagSet local_ts; |
if (!(local_ts = local_tag_set_cache[hash])) { |
local_ts = TagSet ( |
add_tags->name + "+" + orig_tag_set->name); |
local_ts->imported = ({add_tags, orig_tag_set}); |
|
local_tag_set_cache[hash] = local_ts; |
} |
ctx->tag_set = local_ts; |
} |
else |
THIS_TAG_DEBUG ("Not installing additional_tags %O " |
"since they're already in the tag set\n", add_tags); |
} |
|
})) { |
#ifdef MODULE_DEBUG |
if (objectp (err1) && ([object] err1)->thrown_at_unwind) |
err1 = catch ( |
fatal_error ("Can't save parser state when evaluating arguments.\n")); |
#endif |
break process_tag; |
} |
|
if (mixed err2 = catch { |
#ifdef MAGIC_HELP_ARG |
if (tag && (args || ([]))->help) { |
TRACE_ENTER ("tag <" + tag->name + " help>", tag); |
string help = id->conf->find_tag_doc (tag->name, id); |
TRACE_LEAVE (""); |
THIS_TAG_TOP_DEBUG ("Reporting help - frame done\n"); |
ctx->handle_exception ( |
Backtrace ("help", help, ctx), evaler); |
break process_tag; |
} |
#endif |
|
switch (eval_state) { |
case EVSTAT_BEGIN: |
if (array|function(RequestID:array) do_enter = |
[array|function(RequestID:array)] this->do_enter) { |
EXEC_CALLBACK (ctx, evaler, exec, do_enter, id); |
EXEC_ARRAY (ctx, evaler, exec, do_enter); |
} |
else { |
THIS_TAG_DEBUG_ENTER_SCOPE (ctx, this); |
ENTER_SCOPE (ctx, this); |
} |
eval_state = EVSTAT_ENTERED; |
|
|
case EVSTAT_ENTERED: |
case EVSTAT_LAST_ITER: |
int|function(RequestID:int) do_iterate = |
[int|function(RequestID:int)] this->do_iterate; |
array|function(RequestID:array) do_process = |
[array|function(RequestID:array)] this->do_process; |
|
int compile = 0; |
do { |
if (eval_state != EVSTAT_LAST_ITER) { |
if (intp (do_iterate)) { |
iter = [int] do_iterate || 1; |
eval_state = EVSTAT_LAST_ITER; |
#ifdef DEBUG |
if (iter > 1) |
THIS_TAG_DEBUG ("Getting %d iterations from do_iterate\n", iter); |
else if (iter < 0) |
THIS_TAG_DEBUG ("Skipping to finish since do_iterate is negative\n"); |
#endif |
} |
else { |
LOW_CALL_CALLBACK (iter, do_iterate, id); |
THIS_TAG_DEBUG ("%O returned from do_iterate\n", iter); |
THIS_TAG_DEBUG_ENTER_SCOPE (ctx, this); |
ENTER_SCOPE (ctx, this); |
if (ctx->new_runtime_tags) |
_handle_runtime_tags (ctx, evaler); |
if (iter <= 0) eval_state = EVSTAT_LAST_ITER; |
} |
} |
|
for (; iter > 0; iter-- DO_IF_DEBUG (, debug_iter++)) { |
eval_content: |
if (in_content) { |
int finished = 1; |
if (subevaler) |
finished = 0; |
else if (stringp (in_content)) { |
if (in_content == "") { |
in_content = 0; |
break eval_content; |
} |
else if (flags & FLAG_EMPTY_ELEMENT) |
parse_error ("This tag doesn't handle content.\n"); |
else { |
object(PCode)|int p_code = |
(evaler->is_RXML_PCode ? evaler : evaler->p_code) || compile; |
if (this->local_tags) { |
subevaler = content_type->get_parser ( |
ctx, [object(TagSet)] this->local_tags, evaler, p_code); |
subevaler->_local_tag_set = 1; |
THIS_TAG_DEBUG ("Iter[%d]: Parsing and %s content %s " |
"with %O from local_tags\n", debug_iter, |
p_code ? "compiling" : "evaluating", |
utils->format_short (in_content), subevaler); |
} |
else { |
subevaler = content_type->get_parser (ctx, 0, evaler, p_code); |
#ifdef DEBUG |
if (content_type->parser_prog != PNone) |
THIS_TAG_DEBUG ("Iter[%d]: Parsing and %s content %s " |
"with %O\n", debug_iter, |
p_code ? "compiling" : "evaluating", |
utils->format_short (in_content), subevaler); |
#endif |
} |
p_code = subevaler->p_code; |
if (evaler->recover_errors && !(flags & FLAG_DONT_RECOVER)) { |
subevaler->recover_errors = 1; |
if (p_code) p_code->recover_errors = 1; |
} |
subevaler->finish (in_content); |
if (p_code) (in_content = p_code)->finish(); |
finished = 1; |
|
compile = 1; |
} |
} |
else { |
THIS_TAG_DEBUG ("Iter[%d]: Evaluating compiled content\n", |
debug_iter); |
subevaler = in_content; |
} |
|
eval_sub: |
do { |
if (piece != nil && flags & FLAG_STREAM_CONTENT) { |
|
THIS_TAG_DEBUG ("Iter[%d]: Got %s stream piece %s\n", |
debug_iter, finished ? "ending" : "a", |
utils->format_short (piece)); |
if (!arrayp (do_process)) { |
EXEC_CALLBACK (ctx, evaler, exec, do_process, id, piece); |
if (exec) { |
mixed res = _exec_array ( |
ctx, evaler, exec, "do_process"); |
if (flags & FLAG_STREAM_RESULT) { |
#ifdef DEBUG |
if (!zero_type (ctx->unwind_state->stream_piece)) |
fatal_error ("Clobbering unwind_state->stream_piece.\n"); |
#endif |
CONV_RESULT (res, result_type, res, type); |
ctx->unwind_state->stream_piece = res; |
ctx->unwind_state->reason = "streaming"; |
THIS_TAG_DEBUG ("Iter[%d]: Streaming %s from do_process\n", |
debug_iter, utils->format_short (res)); |
throw (this); |
} |
exec = 0; |
} |
else if (flags & FLAG_STREAM_RESULT) { |
THIS_TAG_DEBUG ("Iter[%d]: do_process finished the stream; " |
"ignoring remaining content\n", debug_iter); |
ctx->unwind_state = 0; |
piece = nil; |
break eval_sub; |
} |
} |
piece = nil; |
if (finished) break eval_sub; |
} |
|
else { |
piece = nil; |
if (finished) { |
mixed res = subevaler->_eval (ctx); |
if (content_type->sequential) |
SET_SEQUENTIAL (res, content, "content"); |
else if (res != nil) |
SET_NONSEQUENTIAL (res, content, content_type, "content"); |
break eval_sub; |
} |
} |
|
if (subevaler->is_RXML_Parser) { |
subevaler->finish(); |
(in_content = subevaler->p_code)->finish(); |
} |
finished = 1; |
} while (1); |
|
subevaler = 0; |
} |
|
if (do_process) { |
EXEC_CALLBACK (ctx, evaler, exec, do_process, id); |
EXEC_ARRAY (ctx, evaler, exec, do_process); |
} |
} |
} while (eval_state != EVSTAT_LAST_ITER); |
eval_state = EVSTAT_ITER_DONE; |
|
|
case EVSTAT_ITER_DONE: |
if (array|function(RequestID:array) do_return = |
[array|function(RequestID:array)] this->do_return) { |
EXEC_CALLBACK (ctx, evaler, exec, do_return, id); |
if (exec) { |
|
|
_exec_array (ctx, evaler, exec, "do_return"); |
exec = 0; |
} |
} |
|
else if (result == nil && !(flags & FLAG_EMPTY_ELEMENT)) { |
if (result_type->parser_prog == PNone) { |
if (content_type->name != result_type->name) { |
THIS_TAG_DEBUG ("Assigning content to result after " |
"conversion from %s to %s\n", |
content_type->name, result_type->name); |
result = result_type->encode (content, content_type); |
} |
else { |
THIS_TAG_DEBUG ("Assigning content to result\n"); |
result = content; |
} |
} |
else |
if (stringp (content)) { |
if (!exec) { |
THIS_TAG_DEBUG ("Parsing content with exec array " |
"for assignment to result\n"); |
exec = ({content}); |
} |
_exec_array (ctx, evaler, exec, "content parse"); |
exec = 0; |
} |
} |
|
if (result != nil) |
CONV_RESULT (result, result_type, conv_result, type); |
#ifdef DEBUG |
else THIS_TAG_DEBUG ("Skipping nil result\n"); |
#endif |
|
THIS_TAG_DEBUG_LEAVE_SCOPE (ctx, this); |
LEAVE_SCOPE (ctx, this); |
|
if (ctx->new_runtime_tags) |
_handle_runtime_tags (ctx, evaler); |
} |
|
}) { |
THIS_TAG_DEBUG_LEAVE_SCOPE (ctx, this); |
LEAVE_SCOPE (ctx, this); |
|
string action; |
exception: |
{ |
unwind: |
if (objectp (err2) && ([object] err2)->thrown_at_unwind) { |
UNWIND_STATE ustate = ctx->unwind_state; |
if (!ustate) ustate = ctx->unwind_state = ([]); |
#ifdef DEBUG |
if (ustate[this]) fatal_error ("Frame already has an unwind state.\n"); |
#endif |
|
if (ustate->exec_left) { |
exec = [array] ustate->exec_left; |
m_delete (ustate, "exec_left"); |
} |
|
if (err2 == this || exec && sizeof (exec) && err2 == exec[0]) |
|
if (evaler->read && evaler->unwind_safe) { |
|
|
|
mixed piece = evaler->read(); |
if (err2 = catch { |
if (type->sequential) |
SET_SEQUENTIAL (ustate->stream_piece, piece, "stream piece"); |
else |
SET_NONSEQUENTIAL (ustate->stream_piece, piece, type, "stream piece"); |
}) break unwind; |
if (err2 == this) err2 = 0; |
if (orig_tag_set) ctx->tag_set = orig_tag_set, orig_tag_set = 0; |
action = "break"; |
THIS_TAG_TOP_DEBUG ("Breaking to parent frame to do streaming\n"); |
} |
else { |
|
|
m_delete (ustate, "stream_piece"); |
action = "continue"; |
THIS_TAG_TOP_DEBUG ("Not streaming since the parser " |
"doesn't allow that\n"); |
} |
else if (!zero_type (ustate->stream_piece)) { |
|
|
piece = ustate->stream_piece; |
m_delete (ustate, "stream_piece"); |
action = "continue"; |
THIS_TAG_TOP_DEBUG ("Continuing with stream piece.\n"); |
} |
else { |
action = "break"; |
THIS_TAG_TOP_DEBUG ("Interrupted\n"); |
} |
|
ustate[this] = ({err2, eval_state, in_args, in_content, iter, |
subevaler, piece, exec, orig_tag_set, ctx->new_runtime_tags, |
#ifdef DEBUG |
debug_iter, |
#endif |
}); |
TRACE_LEAVE (action); |
break exception; |
} |
|
THIS_TAG_TOP_DEBUG ("Exception\n"); |
TRACE_LEAVE ("exception"); |
ctx->handle_exception (err2, evaler); |
result = nil; |
action = "return"; |
} |
|
switch (action) { |
case "break": |
#ifdef MODULE_DEBUG |
if (!evaler->unwind_state) |
fatal_error ("Trying to unwind inside an evaluator " |
"that isn't unwind safe.\n"); |
#endif |
throw (this); |
case "continue": |
continue process_tag; |
case "return": |
break process_tag; |
default: |
fatal_error ("Don't you come here and %O on me!\n", action); |
} |
} |
|
else { |
THIS_TAG_TOP_DEBUG ("Done\n"); |
TRACE_LEAVE (""); |
} |
break process_tag; |
} while (1); |
|
|
if (orig_tag_set) ctx->tag_set = orig_tag_set; |
ctx->frame = up, up = 0; |
ctx->frame_depth--; |
if (err1) throw (err1); |
args = 1, content = in_content; |
return conv_result; |
} |
|
MARK_OBJECT; |
|
string _sprintf() |
{ |
return "RXML.Frame(" + (tag && [string] tag->name) + ")" + OBJ_COUNT; |
} |
|
mixed _encode() |
{ |
return (["_content_type":content_type, "_content":content, |
"_result_type":result_type, "_args":args, |
"_raw_tag_text":this_object()->raw_tag_text, |
"_tag" : !tag->tagset && tag ]); |
} |
|
void _decode(mixed cookie) |
{ |
content_type = cookie->_content_type; |
content = cookie->_content; |
result_type = cookie->_result_type; |
args = cookie->_args; |
if (cookie->_tag) |
tag = cookie->_tag; |
if(cookie->_raw_tag_text) |
this_object()->raw_tag_text = cookie->_raw_tag_text; |
} |
} |
|
|
|
|
|
|
final mixed get_var (string var, void|string scope_name, void|Type want_type) |
{return RXML_CONTEXT->get_var (var, scope_name, want_type);} |
final mixed user_get_var (string var, void|string scope_name, void|Type want_type) |
{return RXML_CONTEXT->user_get_var (var, scope_name, want_type);} |
final mixed set_var (string var, mixed val, void|string scope_name) |
{return RXML_CONTEXT->set_var (var, val, scope_name);} |
final mixed us |