[DGD] Working with parse_string()

bart at wotf.org bart at wotf.org
Mon Jul 20 11:49:28 CEST 2009


This can work, and is used a bit in gurbalib (see for example the color
command, which does its own parsing with parse_string, and gives info on what
exactly it failed to understand in many cases).

See lib/cmds/player/color.c in the current gurbalib version
(svn://wotf.org/gurbalib) for an example.

As an aside, it is probably better to use a TLS variable for storing such
data. This is more friendly to dgdmp.

Bart.

On Mon, 20 Jul 2009 10:49:03 +0200, Petter Nyström wrote
> 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
> ___________________________________________
> https://mail.dworkin.nl/mailman/listinfo/dgd


--
Created with Open WebMail at http://www.bartsplace.net/
Read my weblog at http://soapbox.bartsplace.net/




More information about the DGD mailing list