ed8175 | 1999-12-11 | Martin Stjernholm | |
|
5bcbe8 | 2000-01-18 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
#pragma strict_types
class Tag
{
|
cece9e | 2000-01-11 | Martin Stjernholm | | constant is_RXML_Tag = 1;
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
f58572 | 2000-01-18 | Martin Stjernholm | | int flags;
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
mapping(string:Type) req_arg_types;
mapping(string:Type) opt_arg_types;
Type content_type = t_text (PHtml);
array(Type) result_types = ({t_text});
|
c6245b | 1999-12-31 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
string scope_name;
TagSet additional_tags, local_tags;
|
56532d | 1999-12-19 | Martin Stjernholm | | function(:Frame) frame();
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
inline Frame `() (mapping(string:mixed) args, void|mixed|PCode content)
{
|
56532d | 1999-12-19 | Martin Stjernholm | | Tag this = this_object();
Frame frame = ([function(:Frame)] this->frame)();
|
ed8175 | 1999-12-11 | Martin Stjernholm | | frame->tag = this;
frame->flags = flags;
if (scope_name) frame->scope_name = scope_name;
if (additional_tags) frame->additional_tags = additional_tags;
if (local_tags) frame->local_tags = local_tags;
frame->args = args;
if (!zero_type (content)) frame->content = content;
return frame;
}
|
01e43b | 2000-01-14 | Martin Stjernholm | | array _handle_tag (TagSetParser parser, mapping(string:string) args,
|
c6245b | 1999-12-31 | Martin Stjernholm | | void|string content)
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
c6245b | 1999-12-31 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | | {
|
56532d | 1999-12-19 | Martin Stjernholm | | Context ctx = parser->context;
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
56532d | 1999-12-19 | Martin Stjernholm | | Frame frame;
if (mapping(string:mixed)|mapping(object:array) ustate = ctx->unwind_state)
|
f9c80f | 1999-12-30 | Martin Stjernholm | | if (ustate[parser]) frame = [object(Frame)] ustate[parser][0];
|
56532d | 1999-12-19 | Martin Stjernholm | | else frame = `() (args, Void);
else frame = `() (args, Void);
mixed err = catch {
frame->_eval (parser, args, content);
return frame->result == Void ? ({}) : ({frame->result});
};
if (objectp (err) && ([object] err)->thrown_at_unwind) {
mapping(string:mixed)|mapping(object:array) ustate = ctx->unwind_state;
if (!ustate) ustate = ctx->unwind_state = ([]);
#ifdef DEBUG
if (err != frame)
|
01e43b | 2000-01-14 | Martin Stjernholm | | error ("Internal error: Unexpected unwind object catched.\n");
|
56532d | 1999-12-19 | Martin Stjernholm | | if (ustate[parser])
|
01e43b | 2000-01-14 | Martin Stjernholm | | error ("Internal error: Clobbering unwind state for parser.\n");
|
56532d | 1999-12-19 | Martin Stjernholm | | #endif
ustate[parser] = ({err});
err = parser;
}
throw (err);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
string _sprintf()
{
|
2bd21a | 2000-01-05 | Martin Stjernholm | | return "RXML.Tag(" + [string] this_object()->name + ")";
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
}
class TagSet
|
2bd21a | 2000-01-05 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | | {
|
2bd21a | 2000-01-05 | Martin Stjernholm | | string name;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | string prefix;
int prefix_required;
array(TagSet) imported = ({});
|
660cb5 | 2000-01-12 | Martin Stjernholm | | function(Context:void) prepare_context;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | int generation = 1;
|
56ffee | 2000-01-12 | Martin Stjernholm | | #define LOW_TAG_TYPE \
string|array| \
function(:int(1..1)|string|array)| \
function(object,mapping(string:string):int(1..1)|string|array)
#define LOW_CONTAINER_TYPE \
string|array| \
function(:int(1..1)|string|array)| \
function(object,mapping(string:string),string:int(1..1)|string|array)
#define LOW_ENTITY_TYPE \
string|array| \
function(:int(1..1)|string|array)| \
function(object:int(1..1)|string|array)
mapping(string:LOW_TAG_TYPE) low_tags;
mapping(string:LOW_CONTAINER_TYPE) low_containers;
mapping(string:LOW_ENTITY_TYPE) low_entities;
|
da54bd | 2000-01-08 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
2bd21a | 2000-01-05 | Martin Stjernholm | | static void create (string _name, void|array(Tag) _tags)
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
{
|
2bd21a | 2000-01-05 | Martin Stjernholm | | name = _name;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (_tags) tags = mkmapping ([array(string)] _tags->name, _tags);
}
void add_tag (Tag tag)
{
tags[tag->name] = tag;
changed();
}
void add_tags (array(Tag) _tags)
{
tags += mkmapping ( _tags->name, _tags);
changed();
}
void remove_tag (string|Tag tag)
{
if (stringp (tag))
m_delete (tags, tag);
|
56532d | 1999-12-19 | Martin Stjernholm | | else for (string n; !zero_type (n = search (tags, [object(Tag)] tag));)
|
ed8175 | 1999-12-11 | Martin Stjernholm | | m_delete (tags, n);
changed();
}
|
56ffee | 2000-01-12 | Martin Stjernholm | | local Tag|array(LOW_TAG_TYPE|LOW_CONTAINER_TYPE) get_local_tag (string name)
|
ed8175 | 1999-12-11 | Martin Stjernholm | | {
|
56ffee | 2000-01-12 | Martin Stjernholm | | if (Tag tag = tags[name]) return tag;
else if (LOW_CONTAINER_TYPE cdef = low_containers && low_containers[name])
return ({0, cdef});
else if (LOW_TAG_TYPE tdef = low_tags && low_tags[name])
return ({tdef, 0});
else return 0;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
array(Tag) get_local_tags()
|
56ffee | 2000-01-12 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | | {
return values (tags);
}
|
56ffee | 2000-01-12 | Martin Stjernholm | | Tag|array(LOW_TAG_TYPE|LOW_CONTAINER_TYPE) get_tag (string name)
|
2bd21a | 2000-01-05 | Martin Stjernholm | | {
|
56ffee | 2000-01-12 | Martin Stjernholm | | if (object(Tag)|array(LOW_TAG_TYPE|LOW_CONTAINER_TYPE) def = get_local_tag (name))
return def;
foreach (imported, TagSet tag_set)
if (object(Tag) tag = [object(Tag)] tag_set->get_tag (name)) return tag;
return 0;
|
da54bd | 2000-01-08 | Martin Stjernholm | | }
|
56ffee | 2000-01-12 | Martin Stjernholm | | Tag|array(LOW_TAG_TYPE|LOW_CONTAINER_TYPE) get_overridden_tag (
Tag|LOW_TAG_TYPE|LOW_CONTAINER_TYPE tagdef, void|string name)
|
da54bd | 2000-01-08 | Martin Stjernholm | | {
|
56ffee | 2000-01-12 | Martin Stjernholm | | if (objectp (tagdef) && ([object] tagdef)->is_RXML_Tag)
name = [string] ([object] tagdef)->name;
#ifdef MODULE_DEBUG
if (!name) error ("Need tag name.\n");
#endif
if (tags[name] == tagdef ||
(low_containers && low_containers[name] == tagdef) ||
(low_tags && low_tags[name] == tagdef)) {
|
da54bd | 2000-01-08 | Martin Stjernholm | | foreach (imported, TagSet tag_set)
|
56ffee | 2000-01-12 | Martin Stjernholm | | if (object(Tag)|array(LOW_TAG_TYPE|LOW_CONTAINER_TYPE) tagdef =
tag_set->get_tag (name)) return tagdef;
|
da54bd | 2000-01-08 | Martin Stjernholm | | }
else {
int found = 0;
foreach (imported, TagSet tag_set)
|
56ffee | 2000-01-12 | Martin Stjernholm | | if (object(Tag)|array(LOW_TAG_TYPE|LOW_CONTAINER_TYPE) subtag =
tag_set->get_tag (name))
|
da54bd | 2000-01-08 | Martin Stjernholm | | if (found) return subtag;
|
56ffee | 2000-01-12 | Martin Stjernholm | | else if (arrayp (subtag) ?
subtag[0] == tagdef || subtag[1] == tagdef :
subtag == tagdef)
if ((subtag = tag_set->get_overridden_tag (tagdef, name)))
return subtag;
else found = 1;
|
2bd21a | 2000-01-05 | Martin Stjernholm | | }
|
da54bd | 2000-01-08 | Martin Stjernholm | | return 0;
|
2bd21a | 2000-01-05 | Martin Stjernholm | | }
|
56ffee | 2000-01-12 | Martin Stjernholm | | array(Tag|array(LOW_TAG_TYPE|LOW_CONTAINER_TYPE)) get_overridden_tags (string name)
{
if (object(Tag)|array(LOW_TAG_TYPE|LOW_CONTAINER_TYPE) def = get_local_tag (name))
return ({def}) + imported->get_all_tags (name) * ({});
else return imported->get_all_tags (name) * ({});
}
multiset(string) get_tag_names()
{
multiset(string) res = (multiset) indices (tags);
if (low_tags) res |= (multiset) indices (low_tags);
if (low_containers) res |= (multiset) indices (low_containers);
return `| (res, @imported->get_tag_names());
}
|
ed8175 | 1999-12-11 | Martin Stjernholm | | mixed `->= (string var, mixed val)
{
switch (var) {
case "imported":
(imported - ({0}))->dont_notify (changed);
imported = [array(TagSet)] val;
imported->do_notify (changed);
break;
default:
::`->= (var, val);
}
changed();
return val;
}
mixed `[]= (string var, mixed val) {return `->= (var, val);}
Parser `() (Type top_level_type, void|RequestID id)
{
|
660cb5 | 2000-01-12 | Martin Stjernholm | | Context ctx = Context (this_object(), id);
if (!prepare_funs) prepare_funs = get_prepare_funs();
prepare_funs -= ({0});
prepare_funs (ctx);
return ctx->new_parser (top_level_type);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
void changed()
{
generation++;
|
660cb5 | 2000-01-12 | Martin Stjernholm | | prepare_funs = 0;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | (notify_funcs -= ({0}))();
set_weak_flag (notify_funcs, 1);
}
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);
}
void destroy()
{
catch (changed());
}
private mapping(string:Tag) tags = ([]);
private array(function(:void)) notify_funcs = ({});
|
2bd21a | 2000-01-05 | Martin Stjernholm | |
|
660cb5 | 2000-01-12 | Martin Stjernholm | | private 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;
}
|
2bd21a | 2000-01-05 | Martin Stjernholm | | string _sprintf()
{
return name ? "RXML.TagSet(" + name + ")" : "RXML.TagSet";
}
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | TagSet empty_tag_set;
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
af06d5 | 2000-01-12 | Martin Stjernholm | | class Value
{
mixed rxml_var_eval (Context ctx, string var, string scope_name);
}
class Scope
{
|
01e43b | 2000-01-14 | Martin Stjernholm | | mixed `[] (string var, void|Context ctx, void|string scope_name)
{rxml_error ("Cannot query variable" + _in_the_scope (scope_name) + ".\n");}
mixed `[]= (string var, mixed val, void|Context ctx, void|string scope_name)
{rxml_error ("Cannot set variable" + _in_the_scope (scope_name) + ".\n");}
array(string) _indices (void|Context ctx, void|string scope_name)
{rxml_error ("Cannot list variables" + _in_the_scope (scope_name) + ".\n");}
void m_delete (string var, void|Context ctx, void|string scope_name)
{rxml_error ("Cannot delete variable" + _in_the_scope (scope_name) + ".\n");}
private string _in_the_scope (string scope_name)
{
if (scope_name)
|
3e5934 | 2000-01-18 | Martin Stjernholm | | if (scope_name != "_") return " in the scope " + scope_name;
|
01e43b | 2000-01-14 | Martin Stjernholm | | else return " in the current scope";
else return "";
}
|
af06d5 | 2000-01-12 | Martin Stjernholm | | }
|
01e43b | 2000-01-14 | Martin Stjernholm | | #define SCOPE_TYPE mapping(string:mixed)|object(Scope)
|
af06d5 | 2000-01-12 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | | class Context
|
db0417 | 2000-01-14 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
{
Frame frame;
RequestID id;
int type_check;
|
db0417 | 2000-01-14 | Martin Stjernholm | | int error_count;
|
01e43b | 2000-01-14 | Martin Stjernholm | |
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | TagSet tag_set;
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | int tag_set_is_local;
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
mixed get_var (string var, void|string scope_name)
{
|
3e5934 | 2000-01-18 | Martin Stjernholm | | if (SCOPE_TYPE vars = scopes[scope_name || "_"]) {
|
ed8175 | 1999-12-11 | Martin Stjernholm | | mixed val;
|
570853 | 2000-01-15 | Martin Stjernholm | | if (objectp (vars)) {
|
3e5934 | 2000-01-18 | Martin Stjernholm | | val = ([object(Scope)] vars)->`[] (var, this_object(), scope_name || "_");
|
01e43b | 2000-01-14 | Martin Stjernholm | | if (val == Void) return ([])[0];
}
else if (zero_type (val = vars[var])) return ([])[0];
|
5bcbe8 | 2000-01-18 | Martin Stjernholm | | if (objectp (val) && ([object] val)->rxml_var_eval)
|
01e43b | 2000-01-14 | Martin Stjernholm | | return ([object(Value)] val)->
|
3e5934 | 2000-01-18 | Martin Stjernholm | | rxml_var_eval (this_object(), var, scope_name || "_");
|
ed8175 | 1999-12-11 | Martin Stjernholm | | else return val;
}
|
01e43b | 2000-01-14 | Martin Stjernholm | | else if (scope_name) rxml_error ("Unknown scope %O.\n", scope_name);
else rxml_error ("No current scope.\n");
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
mixed set_var (string var, mixed val, void|string scope_name)
{
|
3e5934 | 2000-01-18 | Martin Stjernholm | | if (SCOPE_TYPE vars = scopes[scope_name || "_"])
|
01e43b | 2000-01-14 | Martin Stjernholm | | if (objectp (vars))
|
3e5934 | 2000-01-18 | Martin Stjernholm | | return ([object(Scope)] vars)->`[]= (var, val, this_object(), scope_name || "_");
|
01e43b | 2000-01-14 | Martin Stjernholm | | else
return vars[var] = val;
else if (scope_name) rxml_error ("Unknown scope %O.\n", scope_name);
else rxml_error ("No current scope.\n");
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
void delete_var (string var, void|string scope_name)
{
|
3e5934 | 2000-01-18 | Martin Stjernholm | | if (SCOPE_TYPE vars = scopes[scope_name || "_"])
|
01e43b | 2000-01-14 | Martin Stjernholm | | if (objectp (vars))
|
3e5934 | 2000-01-18 | Martin Stjernholm | | ([object(Scope)] vars)->m_delete (var, this_object(), scope_name || "_");
|
01e43b | 2000-01-14 | Martin Stjernholm | | else
m_delete ([mapping(string:mixed)] vars, var);
else if (scope_name) rxml_error ("Unknown scope %O.\n", scope_name);
else rxml_error ("No current scope.\n");
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
array(string) list_var (void|string scope_name)
{
|
3e5934 | 2000-01-18 | Martin Stjernholm | | if (SCOPE_TYPE vars = scopes[scope_name || "_"])
|
01e43b | 2000-01-14 | Martin Stjernholm | | if (objectp (vars))
|
3e5934 | 2000-01-18 | Martin Stjernholm | | return ([object(Scope)] vars)->_indices (this_object(), scope_name || "_");
|
01e43b | 2000-01-14 | Martin Stjernholm | | else
return indices ([mapping(string:mixed)] vars);
else if (scope_name) rxml_error ("Unknown scope %O.\n", scope_name);
else rxml_error ("No current scope.\n");
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
array(string) list_scopes()
{
|
3e5934 | 2000-01-18 | Martin Stjernholm | | return indices (scopes) - ({"_"});
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
|
af06d5 | 2000-01-12 | Martin Stjernholm | | int exist_scope (void|string scope_name)
{
|
3e5934 | 2000-01-18 | Martin Stjernholm | | return !!scopes[scope_name || "_"];
|
af06d5 | 2000-01-12 | Martin Stjernholm | | }
void add_scope (string scope_name, SCOPE_TYPE vars)
|
ed8175 | 1999-12-11 | Martin Stjernholm | | {
if (scopes[scope_name])
|
3e5934 | 2000-01-18 | Martin Stjernholm | | if (scope_name == "_") {
SCOPE_TYPE inner = scopes["_"];
|
af06d5 | 2000-01-12 | Martin Stjernholm | | while (SCOPE_TYPE outer = hidden[inner]) inner = outer;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | hidden[inner] = vars;
}
else {
Frame outermost;
for (Frame f = frame; f; f = f->up)
if (f->scope_name == scope_name) outermost = f;
if (outermost) hidden[outermost] = vars;
else scopes[scope_name] = vars;
}
else scopes[scope_name] = vars;
}
void remove_scope (string scope_name)
{
#ifdef MODULE_DEBUG
|
3e5934 | 2000-01-18 | Martin Stjernholm | | if (scope_name == "_") error ("Cannot remove current scope.\n");
|
ed8175 | 1999-12-11 | Martin Stjernholm | | #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()
{
|
3e5934 | 2000-01-18 | Martin Stjernholm | | if (SCOPE_TYPE vars = scopes["_"]) {
|
ed8175 | 1999-12-11 | Martin Stjernholm | | string scope_name;
while (scope_name = search (scopes, vars, scope_name))
|
3e5934 | 2000-01-18 | Martin Stjernholm | | if (scope_name != "_") return scope_name;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
return 0;
}
|
af06d5 | 2000-01-12 | Martin Stjernholm | | void add_runtime_tag (Tag tag)
{
if (!new_runtime_tags) new_runtime_tags = RuntimeTags();
new_runtime_tags->add_tags[tag] = 1;
new_runtime_tags->remove_tags[tag] = 0;
new_runtime_tags->remove_tags[tag->name] = 0;
}
void remove_runtime_tag (string|Tag tag)
{
if (!new_runtime_tags) new_runtime_tags = RuntimeTags();
new_runtime_tags->remove_tags[tag] = 1;
}
|
01e43b | 2000-01-14 | Martin Stjernholm | | void rxml_error (string msg, mixed... args)
|
ed8175 | 1999-12-11 | Martin Stjernholm | | {
if (sizeof (args)) msg = sprintf (msg, @args);
|
01e43b | 2000-01-14 | Martin Stjernholm | | msg = rxml_errmsg_prefix + ": " + msg;
|
db0417 | 2000-01-14 | Martin Stjernholm | | if (current_var) msg += " | &" + current_var + ";\n";
|
ed8175 | 1999-12-11 | Martin Stjernholm | | for (Frame f = frame; f; f = f->up) {
|
db0417 | 2000-01-14 | Martin Stjernholm | | if (f->tag) msg += " | <" + f->tag->name;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | else if (!f->up) break;
|
db0417 | 2000-01-14 | Martin Stjernholm | | else msg += " | <(unknown tag)";
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (f->args)
foreach (sort (indices (f->args)), string arg) {
mixed val = f->args[arg];
msg += " " + arg + "=";
if (arrayp (val)) msg += map (val, error_print_val) * ",";
else msg += error_print_val (val);
}
else msg += " (no argmap)";
msg += ">\n";
}
array b = backtrace();
throw (({msg, b[..sizeof (b) - 2]}));
}
|
01e43b | 2000-01-14 | Martin Stjernholm | | void handle_exception (mixed err, PCode|Parser evaluator)
|
c757c4 | 2000-01-08 | Martin Stjernholm | | {
|
db0417 | 2000-01-14 | Martin Stjernholm | | error_count++;
|
01e43b | 2000-01-14 | Martin Stjernholm | | string msg = describe_error (err);
if (msg[..sizeof (rxml_errmsg_prefix) - 1] == rxml_errmsg_prefix) {
|
db0417 | 2000-01-14 | Martin Stjernholm | | while (evaluator->_parent) {
evaluator->error_count++;
evaluator = evaluator->_parent;
}
|
01e43b | 2000-01-14 | Martin Stjernholm | | if (id && id->conf)
msg = ([function(mixed,Type:string)]
|
97022d | 2000-01-14 | Martin Stjernholm | | ([object] id->conf)->handle_rxml_error) (err, evaluator->type);
|
01e43b | 2000-01-14 | Martin Stjernholm | | else {
|
c757c4 | 2000-01-08 | Martin Stjernholm | | #ifdef MODULE_DEBUG
|
01e43b | 2000-01-14 | Martin Stjernholm | | report_notice (describe_backtrace (err));
|
c757c4 | 2000-01-08 | Martin Stjernholm | | #else
|
01e43b | 2000-01-14 | Martin Stjernholm | | report_notice (msg);
|
c757c4 | 2000-01-08 | Martin Stjernholm | | #endif
|
01e43b | 2000-01-14 | Martin Stjernholm | | }
if (evaluator->type->free_text && evaluator->report_error)
evaluator->report_error (msg);
|
c757c4 | 2000-01-08 | Martin Stjernholm | | }
|
01e43b | 2000-01-14 | Martin Stjernholm | | else throw (err);
|
c757c4 | 2000-01-08 | Martin Stjernholm | | }
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
01e43b | 2000-01-14 | Martin Stjernholm | | constant rxml_errmsg_prefix = "RXML parser error";
|
ed8175 | 1999-12-11 | Martin Stjernholm | | 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);
}
|
db0417 | 2000-01-14 | Martin Stjernholm | | string current_var;
|
c6245b | 1999-12-31 | Martin Stjernholm | | Parser new_parser (Type top_level_type)
{
#ifdef MODULE_DEBUG
if (in_use || frame) error ("Context already in use.\n");
#endif
return top_level_type->get_parser (this_object());
}
|
af06d5 | 2000-01-12 | Martin Stjernholm | | mapping(string:SCOPE_TYPE) scopes = ([]);
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
3e5934 | 2000-01-18 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
af06d5 | 2000-01-12 | Martin Stjernholm | | mapping(SCOPE_TYPE|Frame:SCOPE_TYPE) hidden = ([]);
|
3e5934 | 2000-01-18 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
void enter_scope (Frame frame)
{
|
af06d5 | 2000-01-12 | Martin Stjernholm | | SCOPE_TYPE vars;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | #ifdef DEBUG
if (!frame->vars) error ("Internal error: Frame has no variables.\n");
#endif
|
3e5934 | 2000-01-18 | Martin Stjernholm | | if ((vars = [SCOPE_TYPE] frame->vars) != scopes["_"]) {
hidden[vars] = scopes["_"];
scopes["_"] = vars;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (string scope_name = [string] frame->scope_name) {
hidden[frame] = scopes[scope_name];
scopes[scope_name] = vars;
}
}
}
void leave_scope (Frame frame)
{
if (string scope_name = [string] frame->scope_name)
if (hidden[frame]) {
scopes[scope_name] = hidden[frame];
m_delete (hidden, frame);
}
|
af06d5 | 2000-01-12 | Martin Stjernholm | | SCOPE_TYPE vars;
if (hidden[vars = [SCOPE_TYPE] frame->vars]) {
|
3e5934 | 2000-01-18 | Martin Stjernholm | | scopes["_"] = hidden[vars];
|
ed8175 | 1999-12-11 | Martin Stjernholm | | m_delete (hidden, vars);
}
}
#define ENTER_SCOPE(ctx, frame) (frame->vars && ctx->enter_scope (frame))
#define LEAVE_SCOPE(ctx, frame) (frame->vars && ctx->leave_scope (frame))
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | void make_tag_set_local()
|
ed8175 | 1999-12-11 | Martin Stjernholm | | {
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | if (!tag_set_is_local) {
TagSet new_tag_set = TagSet (tag_set->name + " (local)");
new_tag_set->imported = ({tag_set});
tag_set = new_tag_set;
tag_set_is_local = 1;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
}
|
c6245b | 1999-12-31 | Martin Stjernholm | | class RuntimeTags
|
ed8175 | 1999-12-11 | Martin Stjernholm | | {
|
c6245b | 1999-12-31 | Martin Stjernholm | | multiset(Tag) add_tags = (<>);
|
2bd21a | 2000-01-05 | Martin Stjernholm | | multiset(Tag|string) remove_tags = (<>);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
|
c6245b | 1999-12-31 | Martin Stjernholm | | RuntimeTags new_runtime_tags;
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | void create (TagSet _tag_set, void|RequestID _id)
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
{
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | tag_set = _tag_set;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | id = _id;
}
|
56532d | 1999-12-19 | Martin Stjernholm | | mapping(string:mixed)|mapping(object:array) unwind_state;
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
56532d | 1999-12-19 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
56532d | 1999-12-19 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
2bd21a | 2000-01-05 | Martin Stjernholm | | string _sprintf() {return "RXML.Context";}
|
ed8175 | 1999-12-11 | Martin Stjernholm | | #ifdef MODULE_DEBUG
int in_use;
#endif
}
#if constant (thread_create)
|
56532d | 1999-12-19 | Martin Stjernholm | | private Thread.Local _context = thread_local();
|
ed8175 | 1999-12-11 | Martin Stjernholm | | inline void set_context (Context ctx) {_context->set (ctx);}
inline Context get_context() {return [object(Context)] _context->get();}
#else
private Context _context;
inline void set_context (Context ctx) {_context = ctx;}
inline Context get_context() {return _context;}
#endif
#ifdef MODULE_DEBUG
#define ENTER_CONTEXT(ctx) \
Context __old_ctx = get_context(); \
set_context (ctx); \
if (ctx) { \
if (ctx->in_use && __old_ctx != ctx) \
|
01e43b | 2000-01-14 | Martin Stjernholm | | error ("Attempt to use context asynchronously.\n"); \
|
ed8175 | 1999-12-11 | Martin Stjernholm | | ctx->in_use = 1; \
}
#define LEAVE_CONTEXT() \
if (Context ctx = get_context()) \
if (__old_ctx != ctx) ctx->in_use = 0; \
set_context (__old_ctx);
#else
#define ENTER_CONTEXT(ctx) \
Context __old_ctx = get_context(); \
set_context (ctx);
#define LEAVE_CONTEXT() \
set_context (__old_ctx);
#endif
|
01e43b | 2000-01-14 | Martin Stjernholm | | void rxml_error (string msg, mixed... args)
|
ed8175 | 1999-12-11 | Martin Stjernholm | | {
Context ctx = get_context();
|
01e43b | 2000-01-14 | Martin Stjernholm | | if (ctx && ctx->rxml_error)
ctx->rxml_error (msg, @args);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | else {
if (sizeof (args)) msg = sprintf (msg, @args);
|
01e43b | 2000-01-14 | Martin Stjernholm | | msg = Context.rxml_errmsg_prefix + " (no context): " + msg;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | array b = backtrace();
throw (({msg, b[..sizeof (b) - 2]}));
}
}
constant FLAG_CONTAINER = 0x00000001;
|
cece9e | 2000-01-11 | Martin Stjernholm | | constant FLAG_NO_PREFIX = 0x00000002;
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
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_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_SAME_STACK = 0x00100000;
constant FLAG_CACHE_EXECUTE_RESULT = 0x00200000;
class Frame
{
constant is_RXML_Frame = 1;
|
56532d | 1999-12-19 | Martin Stjernholm | | constant thrown_at_unwind = 1;
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
Frame up;
Tag tag;
int flags;
mapping(string:mixed) args;
Type content_type;
mixed content = Void;
Type result_type;
mixed result = Void;
|
c6245b | 1999-12-31 | Martin Stjernholm | | optional int|function(RequestID:int|function) do_enter (RequestID id);
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
c6245b | 1999-12-31 | Martin Stjernholm | | optional array cached_return (Context ctx, void|mixed piece);
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
01e43b | 2000-01-14 | Martin Stjernholm | | void rxml_error (string msg, mixed... args)
|
ed8175 | 1999-12-11 | Martin Stjernholm | | {
|
01e43b | 2000-01-14 | Martin Stjernholm | | get_context()->rxml_error (msg, @args);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
void terminate()
{
|
c6245b | 1999-12-31 | Martin Stjernholm | | error ("FIXME\n");
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
void suspend()
{
|
c6245b | 1999-12-31 | Martin Stjernholm | | error ("FIXME\n");
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
void resume()
{
|
c6245b | 1999-12-31 | Martin Stjernholm | | error ("FIXME\n");
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
|
c6245b | 1999-12-31 | Martin Stjernholm | | mixed _exec_array (TagSetParser parser, array exec)
|
ed8175 | 1999-12-11 | Martin Stjernholm | | {
|
56532d | 1999-12-19 | Martin Stjernholm | | Frame this = this_object();
|
c6245b | 1999-12-31 | Martin Stjernholm | | Context ctx = parser->context;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | int i = 0;
mixed res = Void;
Parser subparser = 0;
mixed err = catch {
if (flags & FLAG_PARENT_SCOPE) LEAVE_SCOPE (ctx, this);
for (; i < sizeof (exec); i++) {
mixed elem = exec[i], piece = Void;
switch (sprintf ("%t", elem)) {
case "string":
if (result_type->_parser_prog == PNone)
piece = elem;
else {
subparser = result_type->get_parser (ctx);
|
c6245b | 1999-12-31 | Martin Stjernholm | | subparser->_parent = parser;
|
56532d | 1999-12-19 | Martin Stjernholm | | subparser->finish ([string] elem);
piece = subparser->eval();
|
ed8175 | 1999-12-11 | Martin Stjernholm | | subparser = 0;
}
break;
case "object":
|
56532d | 1999-12-19 | Martin Stjernholm | | if (([object] elem)->is_RXML_Frame) {
([object(Frame)] elem)->_eval (0);
piece = ([object(Frame)] elem)->result;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
|
56532d | 1999-12-19 | Martin Stjernholm | | else if (([object] elem)->is_RXML_Parser) {
([object(Parser)] elem)->finish();
piece = ([object(Parser)] elem)->eval();
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
else
error ("File objects not yet implemented.\n");
break;
case "mapping":
error ("Header mappings not yet implemented.\n");
break;
case "multiset":
|
56532d | 1999-12-19 | Martin Stjernholm | | if (sizeof ([multiset] elem) == 1) piece = ((array) elem)[0];
else if (sizeof ([multiset] elem) > 1)
error (sizeof ([multiset] elem) + " values in multiset in exec array.\n");
|
ed8175 | 1999-12-11 | Martin Stjernholm | | else error ("No value in multiset in exec array.\n");
break;
default:
error ("Invalid type %t in exec array.\n", elem);
}
if (result_type->sequential) res += piece;
else if (piece != Void) result = res = piece;
}
if (result_type->sequential) result += res;
if (flags & FLAG_PARENT_SCOPE) ENTER_SCOPE (ctx, this);
return res;
};
if (result_type->sequential) result += res;
|
56532d | 1999-12-19 | Martin Stjernholm | | if (objectp (err) && ([object] err)->thrown_at_unwind) {
mapping(string:mixed)|mapping(object:array) ustate;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if ((ustate = ctx->unwind_state) && !zero_type (ustate->stream_piece))
if (result_type->sequential)
ustate->stream_piece = res + ustate->stream_piece;
else if (ustate->stream_piece == Void)
ustate->stream_piece = res;
ustate->exec_left = exec[i..];
if (subparser)
|
56532d | 1999-12-19 | Martin Stjernholm | |
([array] ustate->exec_left)[0] = subparser;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
throw (err);
}
|
2bd21a | 2000-01-05 | Martin Stjernholm | | private void _handle_runtime_tags (TagSetParser parser,
Context.RuntimeTags runtime_tags)
{
multiset(string|Tag) rem_tags = runtime_tags->remove_tags;
multiset(Tag) add_tags = runtime_tags->add_tags - rem_tags;
if (sizeof (rem_tags))
foreach (indices (add_tags), Tag tag)
if (rem_tags[tag->name]) add_tags[tag] = 0;
array(string|Tag) arr_rem_tags = (array) rem_tags;
array(Tag) arr_add_tags = (array) add_tags;
for (Parser p = parser; p; p = p->_parent)
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | if (p->tag_set_eval) {
|
2bd21a | 2000-01-05 | Martin Stjernholm | | foreach (arr_add_tags, Tag tag)
([object(TagSetParser)] p)->add_runtime_tag (tag);
foreach (arr_rem_tags, string|object(Tag) tag)
([object(TagSetParser)] p)->remove_runtime_tag (tag);
}
}
|
ed8175 | 1999-12-11 | Martin Stjernholm | | void _eval (TagSetParser parser,
void|mapping(string:string) raw_args,
void|string raw_content)
{
|
56532d | 1999-12-19 | Martin Stjernholm | | Frame this = this_object();
|
ed8175 | 1999-12-11 | Martin Stjernholm | | Context ctx = parser->context;
|
c6245b | 1999-12-31 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | | int|function(RequestID:int|function) fn, iter;
Parser subparser;
mixed piece;
array exec;
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | int tags_added;
|
c6245b | 1999-12-31 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
#define PRE_INIT_ERROR(X) (ctx->frame = this, error (X))
|
2bd21a | 2000-01-05 | Martin Stjernholm | | #ifdef DEBUG
if (ctx != get_context())
PRE_INIT_ERROR ("Internal error: Context not current.\n");
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | if (!parser->tag_set_eval)
|
2bd21a | 2000-01-05 | Martin Stjernholm | | PRE_INIT_ERROR ("Internal error: Calling _eval() with non-tag set parser.\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
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (array state = ctx->unwind_state && ctx->unwind_state[this]) {
#ifdef DEBUG
if (!up)
PRE_INIT_ERROR ("Internal error: Resuming frame without up pointer.\n");
if (raw_args || raw_content)
PRE_INIT_ERROR ("Internal error: Can't feed new arguments or content "
"when resuming parse.\n");
#endif
|
56532d | 1999-12-19 | Martin Stjernholm | | object ignored;
|
2bd21a | 2000-01-05 | Martin Stjernholm | | [ignored, fn, iter, raw_content, subparser, piece, exec, tags_added,
ctx->new_runtime_tags] = state;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | m_delete (ctx->unwind_state, this);
if (!sizeof (ctx->unwind_state)) ctx->unwind_state = 0;
}
else {
#ifdef MODULE_DEBUG
if (up && up != ctx->frame)
PRE_INIT_ERROR ("Reuse of frame in different context.\n");
#endif
up = ctx->frame;
piece = Void;
}
|
2bd21a | 2000-01-05 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | | #undef PRE_INIT_ERROR
ctx->frame = this;
if (raw_args) {
|
01e43b | 2000-01-14 | Martin Stjernholm | | args = raw_args;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | mapping(string:Type) atypes;
if (tag->req_arg_types) {
|
56532d | 1999-12-19 | Martin Stjernholm | | atypes = raw_args & tag->req_arg_types;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (sizeof (atypes) < sizeof (tag->req_arg_types)) {
array(string) missing = sort (indices (tag->req_arg_types - atypes));
|
01e43b | 2000-01-14 | Martin Stjernholm | | rxml_error ("Required " +
(sizeof (missing) > 1 ?
"arguments " + String.implode_nicely (missing) + " are" :
"argument " + missing[0] + " is") + " missing.\n");
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
}
if (tag->opt_arg_types)
|
56532d | 1999-12-19 | Martin Stjernholm | | if (atypes) atypes += raw_args & tag->opt_arg_types;
else atypes = raw_args & tag->opt_arg_types;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (atypes)
if (mixed err = catch {
foreach (indices (atypes), string arg)
|
56532d | 1999-12-19 | Martin Stjernholm | | args[arg] = atypes[arg]->eval (
|
01e43b | 2000-01-14 | Martin Stjernholm | | raw_args[arg], ctx, 0, parser, 1);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }) {
|
56532d | 1999-12-19 | Martin Stjernholm | | if (objectp (err) && ([object] err)->thrown_at_unwind)
|
ed8175 | 1999-12-11 | Martin Stjernholm | | error ("Can't save parser state when evaluating arguments.\n");
throw (err);
}
}
#ifdef DEBUG
if (!args) error ("Internal error: args not set.\n");
#endif
if (TagSet add_tags = raw_content && [object(TagSet)] this->additional_tags) {
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | if (!ctx->tag_set_is_local) ctx->make_tag_set_local();
if (search (ctx->tag_set->imported, add_tags) < 0) {
ctx->tag_set->imported = ({add_tags}) + ctx->tag_set->imported;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | tags_added = 1;
}
}
if (!result_type) {
|
56532d | 1999-12-19 | Martin Stjernholm | | Type ptype = parser->type;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | foreach (tag->result_types, Type rtype)
|
01e43b | 2000-01-14 | Martin Stjernholm | | if (ptype->subtype_of (rtype)) {result_type = rtype; break;}
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (!result_type)
|
01e43b | 2000-01-14 | Martin Stjernholm | | rxml_error (
"Tag returns " +
String.implode_nicely ([array(string)] tag->result_types->name, "or") +
" but " + [string] parser->type->name + " is expected.\n");
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
if (!content_type) content_type = tag->content_type || result_type;
mixed err = catch {
|
56532d | 1999-12-19 | Martin Stjernholm | | if (!fn) fn = this->do_enter ? this->do_enter (ctx->id) : 1;
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
do {
if (!iter) {
|
8e8ac6 | 2000-01-07 | Martin Stjernholm | | iter = fn, fn = 0;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | while (functionp (iter)) {
int|function(RequestID:int|function) newiter =
|
56532d | 1999-12-19 | Martin Stjernholm | | [int|function(RequestID:int|function)] iter (ctx->id);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | fn = iter, iter = newiter;
}
}
ENTER_SCOPE (ctx, this);
for (; iter > 0; iter--) {
if (raw_content) {
|
2bd21a | 2000-01-05 | Martin Stjernholm | | if (ctx->new_runtime_tags) {
_handle_runtime_tags (parser, ctx->new_runtime_tags);
ctx->new_runtime_tags = 0;
}
|
ed8175 | 1999-12-11 | Martin Stjernholm | | int finished = 0;
if (!subparser) {
|
56532d | 1999-12-19 | Martin Stjernholm | | subparser = content_type->get_parser (
ctx, [object(TagSet)] this->local_tags);
|
c6245b | 1999-12-31 | Martin Stjernholm | | subparser->_parent = parser;
|
56532d | 1999-12-19 | Martin Stjernholm | | subparser->finish (raw_content);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | finished = 1;
}
do {
if (flags & FLAG_STREAM_CONTENT && subparser->read) {
|
2bd21a | 2000-01-05 | Martin Stjernholm | | mixed res = subparser->read();
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (content_type->sequential) piece = res + piece;
else if (piece == Void) piece = res;
if (piece != Void) {
|
2bd21a | 2000-01-05 | Martin Stjernholm | | int|array|function(RequestID,void|mixed:array) do_return;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if ((do_return =
|
2bd21a | 2000-01-05 | Martin Stjernholm | | [int|array|function(RequestID,void|mixed:array)]
this->do_return) &&
functionp (do_return)) {
|
56532d | 1999-12-19 | Martin Stjernholm | | if (!exec) exec = do_return (ctx->id, piece);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (exec) {
|
c6245b | 1999-12-31 | Martin Stjernholm | | mixed res = _exec_array (parser, exec);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (flags & FLAG_STREAM_RESULT) {
#ifdef DEBUG
if (!zero_type (ctx->unwind_state->stream_piece))
error ("Internal error: "
"Clobbering unwind_state->stream_piece.\n");
#endif
ctx->unwind_state->stream_piece = res;
throw (this);
}
exec = 0;
}
else if (flags & FLAG_STREAM_RESULT) {
ctx->unwind_state = 0;
piece = Void;
break;
}
}
piece = Void;
}
if (finished) break;
}
else {
piece = Void;
if (finished) {
|
56532d | 1999-12-19 | Martin Stjernholm | | mixed res = subparser->eval();
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (content_type->sequential) content += res;
else if (res != Void) content = res;
break;
}
}
|
56532d | 1999-12-19 | Martin Stjernholm | | subparser->finish();
|
ed8175 | 1999-12-11 | Martin Stjernholm | | finished = 1;
} while (1);
subparser = 0;
}
|
2bd21a | 2000-01-05 | Martin Stjernholm | | if (int|array|function(RequestID,void|mixed:array) do_return =
[int|array|function(RequestID,void|mixed:array)] this->do_return) {
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (!exec)
|
2bd21a | 2000-01-05 | Martin Stjernholm | | exec = functionp (do_return) ?
([function(RequestID,void|mixed:array)] do_return) (
ctx->id) :
[array] do_return;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (exec) {
|
c6245b | 1999-12-31 | Martin Stjernholm | | mixed res = _exec_array (parser, exec);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (flags & FLAG_STREAM_RESULT) {
#ifdef DEBUG
if (ctx->unwind_state)
error ("Internal error: Clobbering unwind_state to do streaming.\n");
if (piece != Void)
error ("Internal error: Thanks, we think about how nice it must "
"be to play the harmonica...\n");
#endif
ctx->unwind_state = (["stream_piece": res]);
throw (this);
}
}
}
else if (result == Void && content_type->subtype_of (result_type))
result = content;
}
} while (fn);
|
c6245b | 1999-12-31 | Martin Stjernholm | |
if (ctx->new_runtime_tags) {
|
2bd21a | 2000-01-05 | Martin Stjernholm | | _handle_runtime_tags (parser, ctx->new_runtime_tags);
|
c6245b | 1999-12-31 | Martin Stjernholm | | ctx->new_runtime_tags = 0;
}
|
ed8175 | 1999-12-11 | Martin Stjernholm | | };
LEAVE_SCOPE (ctx, this);
if (err) {
string action;
|
56532d | 1999-12-19 | Martin Stjernholm | | if (objectp (err) && ([object] err)->thrown_at_unwind) {
mapping(string:mixed)|mapping(object:array) ustate = ctx->unwind_state;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (!ustate) ustate = ctx->unwind_state = ([]);
#ifdef DEBUG
if (ustate[this])
error ("Internal error: Frame already has an unwind state.\n");
#endif
if (ustate->exec_left) {
exec = [array] ustate->exec_left;
m_delete (ustate, "exec_left");
}
|
56532d | 1999-12-19 | Martin Stjernholm | | if (err == this || exec && sizeof (exec) && err == exec[0])
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
56532d | 1999-12-19 | Martin Stjernholm | | if (parser->unwind_safe) {
if (err == this) err = 0;
if (tags_added) {
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | ctx->tag_set->imported -= ({ this->additional_tags});
|
56532d | 1999-12-19 | Martin Stjernholm | | tags_added = 0;
}
action = "break";
}
else {
m_delete (ustate, "stream_piece");
action = "continue";
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
else if (!zero_type (ustate->stream_piece)) {
piece = ustate->stream_piece;
m_delete (ustate, "stream_piece");
action = "continue";
}
else action = "break";
|
2bd21a | 2000-01-05 | Martin Stjernholm | | ustate[this] = ({err, fn, iter, raw_content, subparser, piece, exec, tags_added,
ctx->new_runtime_tags});
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
|
01e43b | 2000-01-14 | Martin Stjernholm | | else {
ctx->handle_exception (err, parser);
action = "return";
}
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
switch (action) {
case "break":
|
56532d | 1999-12-19 | Martin Stjernholm | | #ifdef MODULE_DEBUG
if (!parser->unwind_state)
error ("Trying to unwind inside a parser that isn't unwind safe.\n");
#endif
|
ed8175 | 1999-12-11 | Martin Stjernholm | | throw (this);
case "continue":
_eval (parser);
return;
|
01e43b | 2000-01-14 | Martin Stjernholm | | case "return":
break;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | default:
error ("Internal error: Don't you come here and %O on me!\n", action);
}
}
|
01e43b | 2000-01-14 | Martin Stjernholm | | if (tags_added)
ctx->tag_set->imported -= ({ this->additional_tags});
ctx->frame = up;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
string _sprintf()
{
|
2bd21a | 2000-01-05 | Martin Stjernholm | | return "RXML.Frame(" + (tag && [string] tag->name) + ")";
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
}
class Parser
{
constant is_RXML_Parser = 1;
|
56532d | 1999-12-19 | Martin Stjernholm | | constant thrown_at_unwind = 1;
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
db0417 | 2000-01-14 | Martin Stjernholm | | int error_count;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | function(Parser:void) data_callback;
int write (string in)
{
int res;
ENTER_CONTEXT (context);
mixed err = catch {
|
56532d | 1999-12-19 | Martin Stjernholm | | if (context && context->unwind_state && context->unwind_state->top) {
|
c6245b | 1999-12-31 | Martin Stjernholm | | #ifdef MODULE_DEBUG
if (context->unwind_state->top != this_object())
error ("The context got an unwound state from another parser. Can't rewind.\n");
#endif
|
56532d | 1999-12-19 | Martin Stjernholm | | m_delete (context->unwind_state, "top");
if (!sizeof (context->unwind_state)) context->unwind_state = 0;
}
|
2bd21a | 2000-01-05 | Martin Stjernholm | | if (this_object()->feed (in)) res = 1;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (res && data_callback) data_callback (this_object());
};
LEAVE_CONTEXT();
|
56532d | 1999-12-19 | Martin Stjernholm | | if (err)
if (objectp (err) && ([object] err)->thrown_at_unwind) {
|
c6245b | 1999-12-31 | Martin Stjernholm | | #ifdef DEBUG
if (err != this_object())
|
01e43b | 2000-01-14 | Martin Stjernholm | | error ("Internal error: Unexpected unwind object catched.\n");
|
c6245b | 1999-12-31 | Martin Stjernholm | | #endif
|
56532d | 1999-12-19 | Martin Stjernholm | | if (!context->unwind_state) context->unwind_state = ([]);
context->unwind_state->top = err;
}
|
01e43b | 2000-01-14 | Martin Stjernholm | | else if (context)
context->handle_exception (err, this_object());
else throw (err);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | return res;
}
void write_end (void|string in)
{
int res;
ENTER_CONTEXT (context);
mixed err = catch {
|
56532d | 1999-12-19 | Martin Stjernholm | | if (context && context->unwind_state && context->unwind_state->top) {
|
c6245b | 1999-12-31 | Martin Stjernholm | | #ifdef MODULE_DEBUG
if (context->unwind_state->top != this_object())
error ("The context got an unwound state from another parser. Can't rewind.\n");
#endif
|
56532d | 1999-12-19 | Martin Stjernholm | | m_delete (context->unwind_state, "top");
if (!sizeof (context->unwind_state)) context->unwind_state = 0;
}
|
2bd21a | 2000-01-05 | Martin Stjernholm | | this_object()->finish (in);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (data_callback) data_callback (this_object());
};
LEAVE_CONTEXT();
|
56532d | 1999-12-19 | Martin Stjernholm | | if (err)
if (objectp (err) && ([object] err)->thrown_at_unwind) {
|
c6245b | 1999-12-31 | Martin Stjernholm | | #ifdef DEBUG
if (err != this_object())
|
01e43b | 2000-01-14 | Martin Stjernholm | | error ("Internal error: Unexpected unwind object catched.\n");
|
c6245b | 1999-12-31 | Martin Stjernholm | | #endif
|
56532d | 1999-12-19 | Martin Stjernholm | | if (!context->unwind_state) context->unwind_state = ([]);
context->unwind_state->top = err;
}
|
01e43b | 2000-01-14 | Martin Stjernholm | | else if (context)
context->handle_exception (err, this_object());
else throw (err);
}
array handle_var (string varref)
{
array(string) split = varref / ".";
if (sizeof (split) == 2)
if (mixed err = catch {
|
db0417 | 2000-01-14 | Martin Stjernholm | | context->current_var = varref;
|
01e43b | 2000-01-14 | Martin Stjernholm | | mixed val;
|
db0417 | 2000-01-14 | Martin Stjernholm | | if (zero_type (val = context->get_var (split[1], split[0]))) {
context->current_var = 0;
|
01e43b | 2000-01-14 | Martin Stjernholm | | return ({});
|
db0417 | 2000-01-14 | Martin Stjernholm | | }
context->current_var = 0;
|
01e43b | 2000-01-14 | Martin Stjernholm | | if (type->free_text) val = (string) val;
|
f58572 | 2000-01-18 | Martin Stjernholm | | return val == Void ? ({}) : ({val});
|
01e43b | 2000-01-14 | Martin Stjernholm | | }) {
|
db0417 | 2000-01-14 | Martin Stjernholm | | context->current_var = 0;
|
01e43b | 2000-01-14 | Martin Stjernholm | | context->handle_exception (err, this_object());
return ({});
}
return type->free_text ? 0 : ({});
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
Context context;
Type type;
int compile;
|
56532d | 1999-12-19 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | | mixed feed (string in);
void finish (void|string in);
|
01e43b | 2000-01-14 | Martin Stjernholm | | optional void report_error (string msg);
|
c6245b | 1999-12-31 | Martin Stjernholm | | optional mixed read();
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
56532d | 1999-12-19 | Martin Stjernholm | | mixed eval();
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
c6245b | 1999-12-31 | Martin Stjernholm | | optional PCode p_compile();
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
c6245b | 1999-12-31 | Martin Stjernholm | | optional void reset (Context ctx, Type type, mixed... args);
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
c6245b | 1999-12-31 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
c6245b | 1999-12-31 | Martin Stjernholm | | optional Parser clone (Context ctx, Type type, mixed... args);
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
c6245b | 1999-12-31 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
2bd21a | 2000-01-05 | Martin Stjernholm | | static void create (Context ctx, Type _type, mixed... args)
|
ed8175 | 1999-12-11 | Martin Stjernholm | | {
context = ctx;
type = _type;
}
Parser _next_free;
|
c6245b | 1999-12-31 | Martin Stjernholm | |
Parser _parent;
|
2bd21a | 2000-01-05 | Martin Stjernholm | |
|
01e43b | 2000-01-14 | Martin Stjernholm | | Stdio.File _source_file;
mapping _defines;
|
2bd21a | 2000-01-05 | Martin Stjernholm | | string _sprintf() {return "RXML.Parser";}
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
class TagSetParser
|
c6245b | 1999-12-31 | Martin Stjernholm | |
|
01e43b | 2000-01-14 | Martin Stjernholm | |
|
c6245b | 1999-12-31 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | | {
inherit Parser;
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | constant tag_set_eval = 1;
|
2bd21a | 2000-01-05 | Martin Stjernholm | |
mixed eval() {return this_object()->read();}
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | TagSet tag_set;
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | optional void reset (Context ctx, Type type, TagSet tag_set, mixed... args);
optional Parser clone (Context ctx, Type type, TagSet tag_set, mixed... args);
static void create (Context ctx, Type type, TagSet _tag_set, mixed... args)
|
ed8175 | 1999-12-11 | Martin Stjernholm | | {
::create (ctx, type);
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | tag_set = _tag_set;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
|
2bd21a | 2000-01-05 | Martin Stjernholm | | mixed read();
|
c6245b | 1999-12-31 | Martin Stjernholm | | void add_runtime_tag (Tag tag);
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
c6245b | 1999-12-31 | Martin Stjernholm | | void remove_runtime_tag (string|Tag tag);
|
2bd21a | 2000-01-05 | Martin Stjernholm | |
string _sprintf() {return "RXML.TagSetParser";}
|
c6245b | 1999-12-31 | Martin Stjernholm | | }
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
class PNone
{
inherit Parser;
string data = "";
int evalpos = 0;
int feed (string in)
{
data += in;
return 1;
}
void finish (void|string in)
{
if (in) data += in;
}
string eval()
{
string res = data[evalpos..];
evalpos = sizeof (data);
return res;
}
string byte_compile()
{
return data;
}
string byte_interpret (string byte_code, Context ctx)
{
return byte_code;
}
void reset (Context ctx)
{
context = ctx;
data = "";
evalpos = 0;
}
|
2bd21a | 2000-01-05 | Martin Stjernholm | |
string _sprintf() {return "RXML.PNone";}
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
mixed simple_parse (string in, void|program parser)
{
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | return t_any (parser || PExpr)->eval (in, Context (empty_tag_set));
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
class Type
{
constant is_RXML_Type = 1;
|
c6245b | 1999-12-31 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
e213f0 | 1999-12-13 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
e213f0 | 1999-12-13 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
void type_check (mixed val);
|
01e43b | 2000-01-14 | Martin Stjernholm | |
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
Type clone()
{
|
56532d | 1999-12-19 | Martin Stjernholm | | Type newtype = object_program (this_object())();
|
ed8175 | 1999-12-11 | Martin Stjernholm | | newtype->_parser_prog = _parser_prog;
|
c757c4 | 2000-01-08 | Martin Stjernholm | | newtype->_parser_args = _parser_args;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | newtype->_t_obj_cache = _t_obj_cache;
return newtype;
}
int `== (mixed other)
{
|
56532d | 1999-12-19 | Martin Stjernholm | | return objectp (other) && ([object] other)->is_RXML_Type &&
([object(Type)] other)->name == this_object()->name;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
int subtype_of (Type other)
{
|
56532d | 1999-12-19 | Martin Stjernholm | | return glob ([string] other->name, [string] this_object()->name);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
|
2bd21a | 2000-01-05 | Martin Stjernholm | | Type `() (program newparser, mixed... parser_args)
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
{
Type newtype;
if (sizeof (parser_args)) {
newtype = clone();
|
c757c4 | 2000-01-08 | Martin Stjernholm | | newtype->_parser_prog = newparser;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | newtype->_parser_args = parser_args;
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | if (newparser->tag_set_eval) newtype->_p_cache = ([]);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
else {
if (!_t_obj_cache) _t_obj_cache = ([]);
if (!(newtype = _t_obj_cache[newparser]))
if (newparser == _parser_prog)
|
56532d | 1999-12-19 | Martin Stjernholm | | _t_obj_cache[newparser] = newtype = this_object();
|
ed8175 | 1999-12-11 | Martin Stjernholm | | else {
_t_obj_cache[newparser] = newtype = clone();
newtype->_parser_prog = newparser;
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | if (newparser->tag_set_eval) newtype->_p_cache = ([]);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
}
return newtype;
}
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | inline Parser get_parser (Context ctx, void|TagSet tag_set)
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
{
Parser p;
if (_p_cache) {
TagSet tset;
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | PCacheObj pco = _p_cache[tset = tag_set || ctx->tag_set];
if (pco && pco->tag_set_gen == tset->generation) {
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if ((p = pco->free_parser)) {
pco->free_parser = p->_next_free;
p->data_callback = p->compile = 0;
|
2bd21a | 2000-01-05 | Martin Stjernholm | | p->reset (ctx, this_object(), tset, @_parser_args);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
else
if (pco->clone_parser)
|
2bd21a | 2000-01-05 | Martin Stjernholm | | p = pco->clone_parser->clone (ctx, this_object(), tset, @_parser_args);
else if ((p = _parser_prog (ctx, this_object(), tset, @_parser_args))->clone)
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
2bd21a | 2000-01-05 | Martin Stjernholm | | p = (pco->clone_parser = p)->clone (ctx, this_object(), tset, @_parser_args);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
else {
pco = PCacheObj();
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | pco->tag_set_gen = tset->generation;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | _p_cache[tset] = pco;
|
2bd21a | 2000-01-05 | Martin Stjernholm | | if ((p = _parser_prog (ctx, this_object(), tset, @_parser_args))->clone)
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
2bd21a | 2000-01-05 | Martin Stjernholm | | p = (pco->clone_parser = p)->clone (ctx, this_object(), tset, @_parser_args);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
}
else {
if ((p = free_parser)) {
free_parser = p->_next_free;
p->data_callback = p->compile = 0;
p->reset (ctx, this_object(), @_parser_args);
}
else if (clone_parser)
|
c6245b | 1999-12-31 | Martin Stjernholm | | p = clone_parser->clone (ctx, this_object(), @_parser_args);
|
56532d | 1999-12-19 | Martin Stjernholm | | else if ((p = _parser_prog (ctx, this_object(), @_parser_args))->clone)
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
c6245b | 1999-12-31 | Martin Stjernholm | | p = (clone_parser = p)->clone (ctx, this_object(), @_parser_args);
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
return p;
}
|
01e43b | 2000-01-14 | Martin Stjernholm | | mixed eval (string in, void|Context ctx, void|TagSet tag_set,
void|Parser|PCode parent, void|int dont_switch_ctx)
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
{
mixed res;
if (!ctx) ctx = get_context();
if (_parser_prog == PNone) res = in;
else {
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | Parser p = get_parser (ctx, tag_set);
|
01e43b | 2000-01-14 | Martin Stjernholm | | p->_parent = parent;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | if (dont_switch_ctx) p->finish (in);
else p->write_end (in);
res = p->eval();
if (p->reset)
if (_p_cache) {
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | PCacheObj pco = _p_cache[tag_set || ctx->tag_set];
|
ed8175 | 1999-12-11 | Martin Stjernholm | | p->_next_free = pco->free_parser;
pco->free_parser = p;
}
else {
p->_next_free = free_parser;
free_parser = p;
}
}
if (ctx->type_check) type_check (res);
return res;
}
program _parser_prog = PNone;
|
c757c4 | 2000-01-08 | Martin Stjernholm | | array(mixed) _parser_args = ({});
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
mapping(program:Type) _t_obj_cache;
private Parser clone_parser;
private Parser free_parser;
private class PCacheObj
{
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | int tag_set_gen;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | Parser clone_parser;
Parser free_parser;
}
mapping(TagSet:PCacheObj) _p_cache;
|
2bd21a | 2000-01-05 | Martin Stjernholm | |
string _sprintf() {return "RXML.Type";}
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
|
01e43b | 2000-01-14 | Martin Stjernholm | | static class TypeAny
{
inherit Type;
constant name = "*";
string _sprintf() {return "RXML.t_any";}
}
TypeAny t_any = TypeAny();
static class TypeText
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
{
inherit Type;
constant name = "text/*";
constant sequential = 1;
constant empty_value = "";
constant free_text = 1;
|
2bd21a | 2000-01-05 | Martin Stjernholm | | string _sprintf() {return "RXML.t_text";}
|
01e43b | 2000-01-14 | Martin Stjernholm | | }
TypeText t_text = TypeText();
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
01e43b | 2000-01-14 | Martin Stjernholm | | static class TypeHtml
|
ed8175 | 1999-12-11 | Martin Stjernholm | | {
|
01e43b | 2000-01-14 | Martin Stjernholm | | inherit TypeText;
constant name = "text/html";
string _sprintf() {return "RXML.t_html";}
}
TypeHtml t_html = TypeHtml();
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
class VarRef
{
constant is_RXML_VarRef = 1;
string scope, var;
|
2bd21a | 2000-01-05 | Martin Stjernholm | | static void create (string _scope, string _var) {scope = _scope, var = _var;}
|
af06d5 | 2000-01-12 | Martin Stjernholm | | int valid (Context ctx) {return ctx->exist_scope (scope);}
mixed get (Context ctx) {return ctx->get_var (var, scope);}
mixed set (Context ctx, mixed val) {return ctx->set_var (var, val, scope);}
void delete (Context ctx) {ctx->delete_var (var, scope);}
|
ed8175 | 1999-12-11 | Martin Stjernholm | | string name() {return scope + "." + var;}
|
af06d5 | 2000-01-12 | Martin Stjernholm | | string _sprintf() {return "RXML.VarRef(" + scope + "." + var + ")";}
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
class PCode
{
constant is_RXML_PCode = 1;
|
56532d | 1999-12-19 | Martin Stjernholm | | constant thrown_at_unwind = 1;
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
array p_code = ({});
|
db0417 | 2000-01-14 | Martin Stjernholm | | int error_count;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | mixed eval (Context ctx)
{
}
|
56532d | 1999-12-19 | Martin Stjernholm | | function(Context:mixed) compile();
|
ed8175 | 1999-12-11 | Martin Stjernholm | |
|
2bd21a | 2000-01-05 | Martin Stjernholm | |
string _sprintf() {return "RXML.PCode";}
|
01e43b | 2000-01-14 | Martin Stjernholm | |
void report_error (string msg)
{
}
PCode|Parser _parent;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
static class VoidType
{
mixed `+ (mixed... vals) {return sizeof (vals) ? predef::`+ (@vals) : this_object();}
mixed ``+ (mixed val) {return val;}
int `!() {return 1;}
|
2bd21a | 2000-01-05 | Martin Stjernholm | | string _sprintf() {return "RXML.Void";}
|
ed8175 | 1999-12-11 | Martin Stjernholm | | };
VoidType Void = VoidType();
class ScanStream
{
private class Link
{
array data;
Link next;
}
private Link head = Link();
private Link tail = head;
private int next_token = 0;
private string end = "";
private int fin = 0;
array scan (string in, int finished);
void feed (string in)
{
#ifdef MODULE_DEBUG
if (fin) error ("Cannot feed data to a finished stream.\n");
#endif
array tokens = scan (end + in, 0);
end = [string] tokens[-1];
if (sizeof (tokens) > 1) {
tail->data = tokens[..sizeof (tokens) - 2];
tail = tail->next = Link();
}
}
void finish (void|string in)
{
if (in || !fin && sizeof (end)) {
#ifdef MODULE_DEBUG
if (in && fin) error ("Cannot feed data to a finished stream.\n");
#endif
fin = 1;
if (in) end += in;
tail->data = scan (end, 1);
tail = tail->next = Link();
}
}
void reset()
{
head = Link();
tail = head;
next_token = 0;
end = "";
fin = 0;
}
mixed read()
{
while (head->next)
if (next_token >= sizeof (head->data)) {
next_token = 0;
head = head->next;
}
else return head->data[next_token++];
return Void;
}
void unread (mixed... put_back)
{
int i = sizeof (put_back);
while (i) head->data[--next_token] = put_back[--i];
if (i) {
Link l = Link();
l->next = head, head = l;
|
56532d | 1999-12-19 | Martin Stjernholm | | l->data = allocate (next_token = max (i - 32, 0)) + put_back[..--i];
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
}
array read_all()
{
array data;
if (next_token) {
data = head->data[next_token..];
head = head->next;
next_token = 0;
}
else data = ({});
while (head->next) {
data += head->data;
head = head->next;
}
return data;
}
int finished()
{
return fin;
}
|
2bd21a | 2000-01-05 | Martin Stjernholm | |
string _sprintf() {return "RXML.ScanStream";}
|
ed8175 | 1999-12-11 | Martin Stjernholm | | }
static program PHtml;
static program PExpr;
void _fix_module_ref (string name, mixed val)
{
mixed err = catch {
switch (name) {
case "PHtml": PHtml = [program] val; break;
case "PExpr": PExpr = [program] val; break;
|
1b2b75 | 2000-01-07 | Martin Stjernholm | | case "empty_tag_set": empty_tag_set = [object(TagSet)] val; break;
|
ed8175 | 1999-12-11 | Martin Stjernholm | | default: error ("Herk\n");
}
};
if (err) werror (describe_backtrace (err));
}
|