[DGD] parse_string() help

aishiteru at charter.net aishiteru at charter.net
Wed Feb 10 06:45:48 CET 2010


Greetings,

I am a newbie to DGD in general and am completely out of my element in terms of regexp and parse_string(); however, I need help with a command I am making to colorize an lpc file.  For starters, I have a simple workroom.c file as follows:

#include <mudlib.h>
#include <"workroom.h">

#define MAX_PEOPLE   5
/* How many people are allowed in this room
*/

inherit ROOM;

private string myname()
{
    return capitalize( owner_file( file_name() ) );
}

void setup( void )
{
    set_brief("Mount Taniquetil");
    set_long( "\
        %^HIGH_RED%^Mount Taniquetil%^RESET%^, the highest mountain of the world, in the halls\n\
of Ilmarin.  The winds and airs were his servants, and he was lord of\n\
air, wind, and clouds in Arda.\n\
" );

    set_exits( ([
        "out"  : STARTING_ROOM,
    ]) );
}

What I am trying to do is parse any of the lpc files on our mud into a colorized output for easier viewing.  For instance, in this command, I'd have the following defines:

string *statements =
({"break", "case", "continue", "default", "do", "else",
"for", "foreach", "goto", "if", "in", "inherit", "return",
"sizeof", "switch", "while"
});


string *pre_comp = 
({"#define", "#else", "#endif", "#if",
"#ifdef", "#include"
});


string *types = 
({"char", "enum", "float", "int", "array", "private",
"mapping", "mixed", "object", "static", "string",
"typedef", "union", "varargs", "void"
});


string *kfuns =
({ "acos", "act_mxp", "add_action", "all_inventory", "allocate",
"allocate_buffer", "allocate_mapping", "asin", "assemble_class", "atan",
"author_stats", "bind", "bufferp", "cache_stats", "call_other",
"call_out_info", "call_stack", "ceil", "children", "classp", "clear_bit",
"this_object", "this_player", "throw", "time", "to_float", "to_int", "typeof",
"undefinedp", "unique_array", "unique_mapping", "upper_case", "uptime",
"userp", "values", "variables", "virtualp", "write_buffer", "write_bytes",
"write_file", "zonetime"
});



string *safuns =
({ "abs", "absolute_path", "absolute_value", "add", "add_article",
"add_event", "add_maps", "add_sky_event", "admin_email", "adminp",
"append_line", "architecture", "archp", "arrange_string", "arrcmp", "assistp",
"valid_timezone", "version", "visibility", "visible", "web_translate",
"wild_card", "wipe_inv", "wizardp", "wrap", "write", "year"
});


string *lfuns =
({"eventTellHist","CanRemoveItem","GetDummyItems","direct_annihilate_liv",
"direct_listen_to_str_word_obj","direct_missile_liv","GetVoteStatus",
"GetRiders","GetLivingMaxCarry","CanManipulate","SaveObject","GetCreatorBirth",
"GetRandomLimb","direct_createfix","RestoreLimb","eventSteal","direct_remedy_liv",
"eventQueueCommand"
});

In my example code, I would colorize it based on these defines.  Precompiler statements turn blue, so #define, #include, etc. would be parsed to %^HIGH_BLUE%^#define%^RESET%^

All lfuns, kfuns, etc.. would have their own color.  In addition, text inside quotes would be colored.  "blah" -> %^HIGH_GREEN%^"blah"%^RESET%^.

I've been reading this mailing list trying to understand parse_string(), since it looks like this would be the best route for my colorized more command, but am totally overwhelmed.  I found an example of something that seems to help with what I am doing, but do not understand it.  Here is what I found:

mixed *parse(string foo, varargs int nalt)
{
    return parse_string("\
whitespace = /[ \t\n\r\v\f]+/           \
whitespace = /\\/\\*([^*]|\\*+[^/*])*\\*\\//        \
whitespace = /#[^\n]*\n/            \
"+"                 \
INT_CONST = /[1-9][0-9]*/           \
INT_CONST = /0[0-7]*/             \
INT_CONST = /0[Xx][0-9a-fA-F]+/           \
INT_CONST = /'[^\\\\]'/             \
INT_CONST = /'\\\\[^0-7xX]'/            \
INT_CONST = /'\\\\[0-7][0-7]?[0-7]?'/         \
INT_CONST = /'\\\\[xX][0-9a-fA-F][0-9a-fA-F]?'/       \
FLOAT_CONST = /[0-9]+\\.[0-9]*([eE][-+]?[0-9]+)/      \
FLOAT_CONST = /[0-9]*\\.[0-9]+([eE][-+]?[0-9]+)/      \
STRING_CONST = /\"([^\\\\\"\n]*(\\\\.)*)*\"/        \
IDENTIFIER = /[a-zA-Z_][a-zA-Z_0-9]*/         \
"+"                 \
program:                \
program: program top_level_declaration          \
"+"                 \
top_level_declaration: 'inherit' opt_inherit_label inherit_string ';' \
top_level_declaration: data_declaration         \
top_level_declaration: function_declaration       \
"+"                 \
opt_inherit_label:              \
opt_inherit_label: IDENTIFIER           \
"+"                 \
inherit_string: STRING_CONST            \
inherit_string: inherit_string '+' STRING_CONST       \
inherit_string: '(' inherit_string ')'          \
"+"                 \
data_declaration: class_specifier_list type_specifier list_dcltr ';'  \
"+"                 \
function_declaration: class_specifier_list type_specifier   \
          function_dcltr compound_stmt      \
function_declaration: class_specifier_list        \
          IDENTIFIER '(' formals_declaration ')'    \
          compound_stmt         \
"+"                 \
local_data_declaration: class_specifier_list type_specifier   \
      list_dcltr ';'          \
"+"                 \
formals_declaration:              \
formals_declaration: 'void'           \
formals_declaration: formal_declaration_list        \
formals_declaration: formal_declaration_list '...'      \
"+"                 \
formal_declaration_list: formal_declaration       \
formal_declaration_list: formal_declaration_list ',' formal_declaration \
"+"                 \
formal_declaration: type_specifier data_dcltr       \
formal_declaration: IDENTIFIER            \
"+"                 \
class_specifier_list:             \
class_specifier_list: class_specifier_list class_specifier    \
"+"                 \
class_specifier: 'private'            \
class_specifier: 'static'           \
class_specifier: 'atomic'           \
class_specifier: 'nomask'           \
class_specifier: 'varargs'            \
"+"                 \
type_specifier: 'int'             \
type_specifier: 'float'             \
type_specifier: 'string'            \
type_specifier: 'object'            \
type_specifier: 'mapping'           \
type_specifier: 'mixed'             \
type_specifier: 'void'              \
"+"                 \
star_list:                \
star_list: star_list '*'            \
"+"                 \
data_dcltr: star_list IDENTIFIER          \
"+"                 \
function_dcltr: star_list IDENTIFIER '(' formals_declaration ')'  \
"+"                 \
dcltr: data_dcltr             \
dcltr: function_dcltr             \
"+"                 \
list_dcltr: dcltr             \
list_dcltr: list_dcltr ',' dcltr          \
"+"                 \
dcltr_or_stmt_list:             \
dcltr_or_stmt_list: dcltr_or_stmt_list dcltr_or_stmt      \
"+"                 \
dcltr_or_stmt: local_data_declaration         \
dcltr_or_stmt: stmt             \
"+"                 \
stmt: list_exp ';'              \
stmt: compound_stmt             \
stmt: 'if' '(' list_exp ')' stmt ? ifelse       \
stmt: 'if' '(' list_exp ')' stmt 'else' stmt ? ifelse     \
stmt: 'do' stmt 'while' '(' list_exp ')' ';'        \
stmt: 'while' '(' list_exp ')' stmt         \
stmt: 'for' '(' opt_list_exp ';' opt_list_exp ';' opt_list_exp ')' stmt \
stmt: 'rlimits' '(' list_exp ';' list_exp ')' compound_stmt   \
stmt: 'catch' compound_stmt opt_caught_stmt       \
stmt: 'switch' '(' list_exp ')' compound_stmt       \
stmt: 'case' exp ':' stmt           \
stmt: 'case' exp '..' exp ':' stmt          \
stmt: 'default' ':' stmt            \
stmt: 'break' ';'             \
stmt: 'continue' ';'              \
stmt: 'return' opt_list_exp ';'           \
stmt: ';'               \
"+"                 \
compound_stmt: '{' dcltr_or_stmt_list '}'       \
"+"                 \
opt_caught_stmt:              \
opt_caught_stmt: ':' stmt           \
"+"                 \
function_name: IDENTIFIER           \
function_name: '::' IDENTIFIER            \
function_name: IDENTIFIER '::' IDENTIFIER       \
"+"                 \
primary_p1_exp: INT_CONST           \
primary_p1_exp: FLOAT_CONST           \
primary_p1_exp: STRING_CONST            \
primary_p1_exp: '(' '{' opt_arg_list_comma '}' ')'      \
primary_p1_exp: '(' '[' opt_assoc_arg_list_comma ']' ')'    \
primary_p1_exp: IDENTIFIER            \
primary_p1_exp: '(' list_exp ')'          \
primary_p1_exp: function_name '(' opt_arg_list ')'      \
primary_p1_exp: 'catch' '(' list_exp ')'        \
primary_p1_exp: primary_p2_exp '->' IDENTIFIER '(' opt_arg_list ')' \
"+"                 \
primary_p2_exp: primary_p1_exp            \
primary_p2_exp: primary_p2_exp '[' list_exp ']'       \
primary_p2_exp: primary_p2_exp '[' opt_list_exp '..' opt_list_exp ']' \
"+"                 \
postfix_exp: primary_p2_exp           \
postfix_exp: postfix_exp '++'           \
postfix_exp: postfix_exp '--'           \
"+"                 \
prefix_exp: postfix_exp             \
prefix_exp: '++' cast_exp           \
prefix_exp: '--' cast_exp           \
prefix_exp: '-' cast_exp            \
prefix_exp: '+' cast_exp            \
prefix_exp: '!' cast_exp            \
prefix_exp: '~' cast_exp            \
"+"                 \
cast_exp: prefix_exp              \
cast_exp: '(' type_specifier star_list ')' cast_exp     \
"+"                 \
mult_oper_exp: cast_exp             \
mult_oper_exp: mult_oper_exp '*' cast_exp       \
mult_oper_exp: mult_oper_exp '/' cast_exp       \
mult_oper_exp: mult_oper_exp '%' cast_exp       \
"+"                 \
add_oper_exp: mult_oper_exp           \
add_oper_exp: add_oper_exp '+' mult_oper_exp        \
add_oper_exp: add_oper_exp '-' mult_oper_exp        \
"+"                 \
shift_oper_exp: add_oper_exp            \
shift_oper_exp: shift_oper_exp '<<' add_oper_exp      \
shift_oper_exp: shift_oper_exp '>>' add_oper_exp      \
"+"                 \
rel_oper_exp: shift_oper_exp            \
rel_oper_exp: rel_oper_exp '<' shift_oper_exp       \
rel_oper_exp: rel_oper_exp '>' shift_oper_exp       \
rel_oper_exp: rel_oper_exp '<=' shift_oper_exp        \
rel_oper_exp: rel_oper_exp '>=' shift_oper_exp        \
"+"                 \
equ_oper_exp: rel_oper_exp            \
equ_oper_exp: equ_oper_exp '==' rel_oper_exp        \
equ_oper_exp: equ_oper_exp '!=' rel_oper_exp        \
"+"                 \
bitand_oper_exp: equ_oper_exp           \
bitand_oper_exp: bitand_oper_exp '&' equ_oper_exp     \
"+"                 \
bitxor_oper_exp: bitand_oper_exp          \
bitxor_oper_exp: bitxor_oper_exp '^' bitand_oper_exp      \
"+"                 \
bitor_oper_exp: bitxor_oper_exp           \
bitor_oper_exp: bitor_oper_exp '|' bitxor_oper_exp      \
"+"                 \
and_oper_exp: bitor_oper_exp            \
and_oper_exp: and_oper_exp '&&' bitor_oper_exp        \
"+"                 \
or_oper_exp: and_oper_exp           \
or_oper_exp: or_oper_exp '||' and_oper_exp        \
"+"                 \
cond_exp: or_oper_exp             \
cond_exp: or_oper_exp '?' list_exp ':' cond_exp       \
"+"                 \
exp: cond_exp               \
exp: cond_exp '=' exp             \
exp: cond_exp '+=' exp              \
exp: cond_exp '-=' exp              \
exp: cond_exp '*=' exp              \
exp: cond_exp '/=' exp              \
exp: cond_exp '%=' exp              \
exp: cond_exp '<<=' exp             \
exp: cond_exp '>>=' exp             \
exp: cond_exp '&=' exp              \
exp: cond_exp '^=' exp              \
exp: cond_exp '|=' exp              \
"+"                 \
list_exp: exp               \
list_exp: list_exp ',' exp            \
"+"                 \
opt_list_exp:               \
opt_list_exp: list_exp              \
"+"                 \
arg_list: exp               \
arg_list: arg_list ',' exp            \
"+"                 \
opt_arg_list:               \
opt_arg_list: arg_list              \
opt_arg_list: arg_list '...'            \
"+"                 \
opt_arg_list_comma:             \
opt_arg_list_comma: arg_list            \
opt_arg_list_comma: arg_list ','          \
"+"                 \
assoc_exp: exp ':' exp              \
"+"                 \
assoc_arg_list: assoc_exp           \
assoc_arg_list: assoc_arg_list ',' assoc_exp        \
"+"                 \
opt_assoc_arg_list_comma:           \
opt_assoc_arg_list_comma: assoc_arg_list        \
opt_assoc_arg_list_comma: assoc_arg_list ','        \
", foo, nalt);
}

mixed *ifelse(mixed *rule)
{
    /*
     * indicate if/else scope with extra bracket pairs
     */
    return ({ "{" }) + rule + ({ "}" });
}


Any ideas on where I can go from here?  Any help would be greatly appreciated by me and the admins of my mud.  Thanks in advance and I apologize for my inexperience.

Joseph



More information about the DGD mailing list