[DGD] Working with parse_string()

Petter Nyström jimorie at gmail.com
Mon Jul 20 10:49:03 CEST 2009


Hi Kamil!

> FAIL, returns nil but whole tree gets discarded and there are
> no more tokens to try, so in effect parse_string returns nil, making
> me unable to process these result and form some meaningful message to
> user (other than maybe message: Give [what] [whom]?, without telling
> player which object name he specified wrong, was it item to give, or
> maybe target?)

In order to provide your users with more detailed messages of what
went wrong in parsing their input, you could store temporary data in
the object calling parse_string().

Here's an example to explain what I'm thinking.

    /* Kamil's parser object */

    static string error_message;

    mixed *parse_input (string input)
    {
        mixed *result;
        error_message = nil;
        result = parse_string("<grammar>", input);
        result = ({ result, error_message });
        error_message = nil;
        return result;
    }

    find_object_in_inventory(arr) {
       ob = FIND_IN_INVENTORY(arr);
       if (ob) {
          return ({ ob });
       }
       else {
          error_message = "You see no " + implode(arr, " ") + " here.";
          return nil;
       }
    }

So in this example your parsing function would return an array of two
elements, first your parse_string() results and second the last error
message that was generated.

You probably want to do something more advanced, but this is just to
show how you can let the functions called from parse_string()
manipulate global variables in the object, which you can use after
parse_string() returns.

Maybe that helps,

Jimorie



More information about the DGD mailing list