Table of Contents

Module: parser mudlib/parser.py

This module provides support for parsing user input against parse patterns. Patterns allow you to flag certain sections of user input as special. For example, you can flag that certain parts of the input must be a description of an object, or that they must match a particular syntax, or that they may optionally match a particular syntax, etc.

The parsing functionality as a whole is broken down into two phases: parsing and binding. Parsing is responsible for making sure that user input is valid per a parse pattern and for matching user input against any match patterns. The parsing phase does not make any attempt to validate any object descriptions against any actual objects.

Binding is responsible for actually matching an object description against one more objects. It includes support for singular and pural references, as well as cardinal and ordinal numeric modifiers. Binding occurs within a context, which is a list of possible objects that the description may match. Binding succeeds if a description (with specified numeric modifiers applied) matches one or more objects within the context; it fails otherwise.

Parsing occurs through use of the parse function. Binding occurs through the bind function. If you want to do both in a single step, you can use the parseAndBind function.

Tokens in the parse pattern are interpreted as follows:

text                    required word
(text1|text2|...|textN) Require either text1 or text2 or ... or textN
<a href="#text">[text]</a>                  optional word
%o                      match object description
%s                      match string
%a                      match optional adverb

Examples of parsing :

Pattern: "look"
Valid input examples:   "look"
Invalid input examples: "look ball", "look here", "look around"
Pattern: "look (at|toward|towards) <a href="#the">[the]</a> %o"
Valid input examples: "look at ball", "look toward ball",
'                     "look towards ball", "look at the ball",
'                     "look at the balls", "look at both balls",
'                     "look at the third ball"
Invalid input examples: "look", "look at at balls",
'                       "look at all 'em balls!"
Pattern: "get <a href="#the">[the]</a> %o from <a href="#the">[the]</a> %o"
Valid input examples: "get ball from basket", "get the ball from the basket",
'                     "get fifteen balls from the second basket",
'                     "get everything from both baskets"
Invalid input examples: "get ball", "get ball basket", "ball from basket",
'                       "get ball inside basket"
Pattern: "smile %a <a href="#at">[at]</a> [the] %o"
Valid input examples: "smile happily at john", "smile at john",
'                     "smile john", "smile sorrowfully at john"
Invalid input examples: "smile", "smile at john happily"

Imported modules   
import core
import language
from org.bluesock.bluemud.driver import Driver
import stdlib
import stdprops
import string
Functions   
bind
bindWithin
parse
parseAndBind
successful
  bind 
bind ( matches,  potentialObjectsTuple=None )

Attempts to bind the given list of matched object descriptions against the (optional) tuple of lists of potential objects. If no lists of potential objects are specified in the tuple, then this method will default to using all objects in the inventory of the player's environment as well as all objects in the inventory of the player. If multiple lists of potential objects are provided in the tuple, the lists will be used, in order, to bind the respective corresponding object match (that is, the first list will be used in binding the first object, the second list for the second object, and so on). If there are fewer lists than objects, then the last list in the tuple will be used to bind all subsequent objects. Note: Bindings by OID always succeed, regardless of whether the bound object resides within the list of potentialObjects (assuming the provided OID is valid).

Returns a list of lists, one per match token.

  bindWithin 
bindWithin ( match,  containers )

A utility function that binds the given list of matched object descriptions within the total inventory of the list of containers provided. Returns the results of the bind function. Note: all elements in the provided list that are not objects or are not descended from mudlib.core.Container are ignored.

  parse 
parse ( pattern,  input )

Parses the given user input against the given parsing pattern. Returns None if the input fails to parse properly against the pattern. Otherwise, returns a list of object descriptions (which will be empty if no objects were present in the pattern).

  parseAndBind 
parseAndBind (
        pattern,
        input,
        potentialObjectsTuple=None,
        )

Perform both the parse and bind operations. Returns the results of the bind function. See the parse and bind functions for details on what is expected and returned.

  successful 
successful ( results )

A utility function that can be used to quickly determine if the given results of parse, bind, or parseAndBind were successful.


Table of Contents

This document was automatically generated on Thu Jan 24 08:57:43 2002 by HappyDoc version 2.0