Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0

Class: Parser

Defined in: adventure/Parser.js, line 6

The Parser class interprets player input. In short, it takes a string, sanitizes it of undesirable characters, performs several operations to strip away extraneous grammar such as definite/indefinite articles, performs a number of regular expression searches, compares the input against different verb/noun patterns stored in a lookup table, all to reduce input to a simple format such as 'verb noun noun'. Parser can accept one verb and up to three nouns. Clauses joined by 'and' or 'then' are split into multiple inputs and queued for serial handling.

This is an internal class that authors should not need to construct, though there are methods to allow authors to inject their own custom parser code. See Write Parsers for more info.

Here are some examples of inputs and how they would be parsed.

Examples:

  • unlock the icy door with the glass key
    parses to  =>  unlock icy_door glass_key
  • ask Mr. Giles about Mrs. Beasley
    parses to  =>  ask_about mr_giles mrs_beasley
  • climb over to branch
    parses to  =>  climb_to branch
  • swing from the tree to the cliff on the vine
    parses to  =>  swing_from_to_on tree cliff vine
  • look at the zebra through the binoculars
    parses to  =>  lookAt_through zebra binoculars
  • unlock door then open door then go north
    parses to three inputs  =>  unlock door / open door / go north

Private Constructor:

var foo = new adventurejs.Parser(game)

Parameters:

  • game Game
    A reference to the game instance.
Inherited Overrides

Index

Methods:

Properties:

Methods Collapse all  |  Expand all

createCustomParser(parsers)

Defined in: adventure/parser/createCustomParser.js, line 8

Parameters:

  • parsers Object
    An object containing one or more parser functions.
Install custom parser(s) defined by author.
disableCustomParser(parser)

Defined in: adventure/parser/disableCustomParser.js, line 8

Parameters:

  • parser String
    The name of a previously created parser that has been stored at game.parser.custom_parsers[name].
Disable a custom parser(s) created by author.
enableCustomParser(parser)

Defined in: adventure/parser/enableCustomParser.js, line 8

Parameters:

  • parser String
    The name of a previously created parser that has been stored at game.parser.custom_parsers[name].
Enable a custom parser(s) created by author.
findMatchIn(newQualified, oldQualified) → {Object}

Defined in: adventure/parser/findMatchIn.js, line 9

Parameters:

  • newQualified Object
  • oldQualified Object
Used by noun disambiguation in handleWord.js, typically in cases where user has been asked a question like "Which thing do you want, 1) A or 2) B ?", to compare this turn's input against last turn's input.

Returns:

Object foundMatch
getInputCount() → {int}

Defined in: adventure/Parser.js, line 258

Get a count of input history.

Returns:

int
getLastInput() → {String}

Defined in: adventure/Parser.js, line 248

Get input from last turn.

Returns:

String
getNewerInput()

Defined in: adventure/Parser.js, line 173

Listen for arrowDown key, used to present player input from previous turns in the input field, as in a terminal.
getOlderInput()

Defined in: adventure/Parser.js, line 154

Track input for display, used to present player input from previous turns in the input field, like a terminal does.
getUnparsedMessage()

Defined in: adventure/Parser.js, line 230

Take input and return a standardized "unparsed" message. settings.if_parser_has_no_response_print_this.
handleSentence()

Defined in: adventure/parser/handleSentence.js, line 9

Handle multi-word input.
handleWord()

Defined in: adventure/parser/handleWord.js, line 9

Handle single-word input.
isParsingMultiple()

Defined in: adventure/Parser.js, line 195

Todos: Is this and is_input_queued duplicative?

Method to get whether we're parsing through a queue of inputs. Possible redundant with is_input_queued?
joinCompoundNames(input) → {String}

Defined in: adventure/parser/joinCompoundNames.js, line 9

Parameters:

  • input String
    Player input.

Search input for compound object names, by comparing the input string against entries in this.game.world lookup table, which was populated during the creation of Game objects. If we find multiple words that match an entry, we compress the words into a single string that is a noun id.

For example:
"turn on brass lantern" becomes "turn on brass_lantern".

Returns:

String
joinCompoundPhrases(input) → {String}

Defined in: adventure/parser/joinCompoundPhrases.js, line 9

Todos: This might be too greedy, or perhaps need to exclude first word of input. I had a room called "Put Plug In This" and this method parsed "put plug in sink" to "put_plug_in_this in sink" Alternately, revise the method that populates world_lookup for instance world_lookup has keys for "Plug Something With Something" and "sink with"

Parameters:

  • input String
    Player input.

Search input for multi-word phrases that are not names, such as "north door" to describe an aperture related to a north exit, by comparing the input string against entries in game.world_lookup, which contains space delimited phrases as well as single words which make up those phrases.

For example if we had an object named: "Maxwell's silver hammer"

User might ask for "hammer" or "silver hammer" or "maxwell's silver hammer" In order to maximize chances of understanding partial user input, each word of the phrase becomes a separate entry in world_lookup.

When performing search/replace we don't want to accidentally replace substrings of larger phrases, which means we need to search for longer phrases first and work our way down. We also need to ensure we don't substitute words in phrases we've already serialized.

Unfortunately we can't just sort world_lookup by word count because JS object properties aren't indexed, by definition. Instead we figure out the longest word count property and do a loop for each word count from the longest down to two words. (No substitution is needed for single words.)

Returns:

String
joinCompoundPrepositions(input) → {String}

Defined in: adventure/parser/joinCompoundPrepositions.js, line 9

Parameters:

  • input String
    Player input.
Convert some common compound prepositions into single words to streamline preposition handling.

Returns:

String
joinCompoundVerbs(input) → {String}

Defined in: adventure/parser/joinCompoundVerbs.js, line 27

Parameters:

  • input String
    Player input.
joinCompoundVerbsis a holdover from an earlier version of the parser that didn't parse prepositions as distinct words, but instead performed multiple regexs on input, finding patterns like "swing from tree to cliff on vine" and joining them into "swing_from_to_on tree cliff vine". All verbs were originally handled this way. Most now are not, with a few remaining exceptions. There are still some verbs with baked-in prepositions because it's just an easier way of catching variants like "get off" or "get down" and funneling them to the same chunk of logic while also distinguishing them from "get lamp".

The listed order of regex operations is important. We're calling them in order of longest to shortest, to ensure we don't accidentally catch partial phrases, like only finding "swing_from" out of "swing from tree to cliff on vine", and then not finding "swing_from_to_on", which is (or was) a distinct verb.

Returns:

String
joinVerbNounPrepNounPrepNouns(input) → {String}

Defined in: adventure/parser/joinVerbNounPrepNounPrepNouns.js, line 9

Parameters:

  • input String
    Player input.

Search input for compound verb phrases in the format of "verb noun prep noun prep nouns", by comparing the input string against entries in dictionary.verb_noun_prep_noun_prep_nouns, which was populated by Verb definitions. If we find a set of words that match an entry, we compress the words into a single string that is a Verb name.

For example:
"tie dog to tree with rope" becomes "tie_to_with dog tree rope"

Returns:

String
joinVerbNounPrepNouns(input) → {String}

Defined in: adventure/parser/joinVerbNounPrepNouns.js, line 9

Parameters:

  • input String
    Player input.

Search input for compound verb phrases in the format of "verb noun preposition noun", by comparing the input string against entries in dictionary.verb_noun_prep_nouns, which was populated by Verb definitions. If we find a set of words that match an entry, we compress the words into a single string that is a Verb name.

This is a holdover from an earlier parsing technique that is not much used now. (This particular example is no longer valid.)
"ask grocer about eggplant" becomes "ask_about grocer eggplant"

Returns:

String
joinVerbNounPrepPrepNouns(input) → {String}

Defined in: adventure/parser/joinVerbNounPrepPrepNouns.js, line 9

Parameters:

  • input String
    Player input.

Search input for compound verb phrases in the format of "verb noun preposition preposition noun", by comparing the input string against entries in dictionary.verb_noun_prep_prep_nouns, which was populated by Verb definitions. If we find a set of words that match an entry, we compress the words into a single string that is a Verb name.

For example:
"take skateboard from under bed" becomes "take_from skateboard bed"

Returns:

String
joinVerbNounPreps(input) → {String}

Defined in: adventure/parser/joinVerbNounPreps.js, line 9

Parameters:

  • input String
    Player input.

Search input for compound verb phrases in the format of "verb noun preposition", by comparing the input string against entries in dictionary.verb_noun_preps, which was populated by Verb definitions. If we find a set of words that match an entry, we compress the words into a single string that is a Verb name.

For example:
"look in chest" becomes "lookIn chest"

Returns:

String
joinVerbPrepNounPrepNounPrepNouns(input) → {String}

Defined in: adventure/parser/joinVerbPrepNounPrepNounPrepNouns.js, line 9

Parameters:

  • input String
    Player input.

Search input for compound verb phrases in the format of "verb preposition noun preposition noun preposition noun", by comparing the input string against entries in dictionary.verb_prep_noun_prep_noun_prep_nouns, which was populated by Verb definitions. If we find a set of words that match an entry, we compress the words into a single string that is a Verb name.

For example:
"swing from tree to stalactite on vine" becomes "swing_from_to_on tree stalactite vine".

Returns:

String
joinVerbPrepNounPrepNouns(input) → {String}

Defined in: adventure/parser/joinVerbPrepNounPrepNouns.js, line 9

Parameters:

  • input String
    Player input.

Search input for compound verb phrases in the format of "verb prep noun prep noun", by comparing the input string against entries in dictionary.verb_prep_noun_prep_nouns, which was populated by Verb definitions. If we find a set of words that match an entry, we compress the words into a single string that is a Verb name.

For example:
"jump from branch to vine" becomes "jumpFrom_to branch vine"

Returns:

String
joinVerbPrepNouns(input) → {String}

Defined in: adventure/parser/joinVerbPrepNouns.js, line 9

Parameters:

  • input String
    Player input.

Search input for compound verb phrases in the format of "verb prep noun", by comparing the input string against entries in dictionary.verb_prep_nouns, which was populated by Verb definitions. If we find a set of words that match an entry, we compress the words into a single string that is a Verb name.

For example:
"look in desk" becomes "lookIn desk"

Returns:

String
joinVerbPrepPrepNouns(input) → {String}

Defined in: adventure/parser/joinVerbPrepPrepNouns.js, line 9

Parameters:

  • input String
    Player input.

Search input for compound verb phrases in the format of "verb prep prep noun", by comparing the input string against entries in dictionary.verb_prep_prep_nouns, which was populated by Verb definitions. If we find a set of words that match an entry, we compress the words into a single string that is a Verb name.

For example:
"get out of boat" becomes "go_out boat"

Returns:

String
joinVerbPrepPrepPrepNouns(input) → {String}

Defined in: adventure/parser/joinVerbPrepPrepPrepNouns.js, line 9

Parameters:

  • input String
    Player input.

Search input for compound verb phrases in the format of "verb prep prep prep noun", by comparing the input string against entries in dictionary.verb_prep_prep_prep_nouns, which was populated by Verb definitions. If we find a set of words that match an entry, we compress the words into a single string that is a Verb name.

For example:
"get out from behind boulder" becomes "go_out_from_behind boulder"

Returns:

String
parseInput(input)

Defined in: adventure/parser/parseInput.js, line 9

Parameters:

  • input String
    Player input.
Parse an input string.
parseNoInput()

Defined in: adventure/Parser.js, line 206

Respond to blank input from player with settings.if_input_is_empty_print_this.
parseNoun(word) → {adventurejs.parsedNoun}

Defined in: adventure/parser/parseNoun.js, line 9

Parameters:

  • word String
Handle noun input.

Returns:

adventurejs.parsedNoun
parseSentence()

Defined in: adventure/parser/parseSentence.js, line 9

Todos: Will eventually need to look for "person, do thing" or "tell person to do thing"

Parse each word in a sentence.
parseStrings(input) → {String}

Defined in: adventure/parser/parseStrings.js, line 9

Parameters:

  • input String
    Player input.
Look for "strings", meaning arbitrary quote delimited substrings that aren't meant to be parsed, and replace with the GlobalString placeholder class.

Example:
type "foo" on keyboard
Becomes:
type global_string on keyboard

Returns:

String
parseVerb(verb) → {String}

Defined in: adventure/parser/parseVerb.js, line 9

Parameters:

  • verb String
    One word from input string.
Handle verb input. Verify a string against dictionary verbs. If string is a verb alias (ex: 'look at' is an alias for 'examine'), replace it with the primary verb.

Returns:

String
printNounDisambiguation(params)

Defined in: adventure/parser/printNounDisambiguation.js, line 9

Parameters:

  • params Object
qualifyParsedNoun(params) → {adventurejs.parsedNoun}

Defined in: adventure/parser/qualifyParsedNoun.js, line 9

Parameters:

  • params Object
Exclude nouns found by parseNoun that don't meet qualifications for verb found by parseVerb.

Returns:

adventurejs.parsedNoun
qualifyParsedVerb(params) → {adventurejs.parsedVerb}

Defined in: adventure/parser/qualifyParsedVerb.js, line 9

Parameters:

  • params Object
End turn if input verb can't act upon noun or in current circumstance.

Returns:

adventurejs.parsedVerb
sanitizeInput(parsed_input, unparsed_input) → {Array}

Defined in: adventure/parser/sanitizeInput.js, line 9

Parameters:

  • parsed_input String
  • unparsed_input String
  • toLowerCase()
  • strip trailing "."
  • replace tabs with spaces
  • trim()
  • reduce multiple spaces to single
  • replace " and then " with " then "
  • split by periods
  • split by "then"
  • check for "again"

Returns:

Array [string, string]
selectAll(word) → {Array}

Defined in: adventure/parser/selectAll.js, line 9

Parameters:

  • word String
Get all gettable assets in current room.

Returns:

Array
selectBodyOfSubstance(list) → {Array}

Defined in: adventure/parser/selectBodyOfSubstance.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are not bodies of substance such as a lake or a beach.

Returns:

Array
selectCarried(list) → {Array}

Defined in: adventure/parser/selectCarried.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are not carried by player.

Returns:

Array
selectCharacter(list) → {Array}

Defined in: adventure/parser/selectCharacter.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are not characters.

Returns:

Array
selectExtant(list) → {Array}

Defined in: adventure/parser/selectExtant.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that have been destroyed.

Returns:

Array
selectGlobalSubstance(list) → {Array}

Defined in: adventure/parser/selectGlobalSubstance.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are not global substances.

Returns:

Array
selectHeld(list) → {Array}

Defined in: adventure/parser/selectHeld.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are not being held by player.

Returns:

Array
selectInHands(list) → {Array}

Defined in: adventure/parser/selectInHands.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are not in player's hands.

Returns:

Array
selectInInventory(list) → {Array}

Defined in: adventure/parser/selectInInventory.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are not in player's inventory.

Returns:

Array
selectInInventoryIfTakeable(list) → {Array}

Defined in: adventure/parser/selectInInventoryIfTakeable.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are not takeable from player inventory.

Returns:

Array
selectIntangible(list) → {Array}

Defined in: adventure/parser/selectIntangible.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all non-intangible assets.

Returns:

Array
selectKnown(list) → {Array}

Defined in: adventure/parser/selectKnown.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are unknown by player.

Returns:

Array
selectMatter(list) → {Array}

Defined in: adventure/parser/selectMatter.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all non-matter assets.

Returns:

Array
selectNotExit(list) → {Array}

Defined in: adventure/parser/selectNotExit.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are exits.

Returns:

Array
selectNotGlobal(list) → {Array}

Defined in: adventure/parser/selectNotGlobal.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all global assets.

Returns:

Array
selectNotInHands(list) → {Array}

Defined in: adventure/parser/selectNotInHands.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets held in player's hands.

Returns:

Array
selectNotInInventory(list) → {Array}

Defined in: adventure/parser/selectNotInInventory.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets in player's inventory.

Returns:

Array
selectNotNestedInventoryIfAll(list) → {Array}

Defined in: adventure/parser/selectNotNestedInventoryIfAll.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets in player's inventory.

Returns:

Array
selectNotPlayerParent(list) → {Array}

Defined in: adventure/parser/selectNotPlayerParent.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are nested in player.

Returns:

Array
selectNotScenery(list) → {Array}

Defined in: adventure/parser/selectNotScenery.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all scenery assets.

Returns:

Array
selectNotSubstance(list) → {Array}

Defined in: adventure/parser/selectNotSubstance.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all substance assets.

Returns:

Array
selectNotWorn(list) → {Array}

Defined in: adventure/parser/selectNotWorn.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets worn by player.

Returns:

Array
selectNotWornIfAll(list) → {Array}

Defined in: adventure/parser/selectNotWornIfAll.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all worn assets if player input "all".

Returns:

Array
selectPlayerParent(list) → {Array}

Defined in: adventure/parser/selectPlayerParent.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets but player's parent. Should only ever return one object.

Returns:

Array
selectPresent(list) → {Array}

Defined in: adventure/parser/selectPresent.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are not present in current room.

Returns:

Array
selectPresentIfTangible(list) → {Array}

Defined in: adventure/parser/selectPresentIfTangible.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all tangible assets that are not present in current room.

Returns:

Array
selectReachable(list) → {Array}

Defined in: adventure/parser/selectReachable.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are not reachable by player.

Returns:

Array
selectReachableIfTangible(list) → {Array}

Defined in: adventure/parser/selectReachableIfTangible.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are not reachable by player.

Returns:

Array
selectSingular(list) → {Array}

Defined in: adventure/parser/selectSingular.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all objects that are collections.

Returns:

Array
selectSubstance(list) → {Array}

Defined in: adventure/parser/selectSubstance.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are not substances.

Returns:

Array
selectTakeable(list) → {Array}

Defined in: adventure/parser/selectTakeable.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that can't be taken by player.

Returns:

Array
selectTangible(list) → {Array}

Defined in: adventure/parser/selectTangible.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all non-tangible assets.

Returns:

Array
selectTangibleOrSubstance(list) → {Array}

Defined in: adventure/parser/selectTangibleOrSubstance.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all non-tangible assets.

Returns:

Array
selectVisible(list) → {Array}

Defined in: adventure/parser/selectVisible.js, line 9

Todos: consider global darkness

Parameters:

  • list Array
Exclude from a list of assets all assets that are not visible to player.

Returns:

Array
selectVisible(list) → {Array}

Defined in: adventure/parser/selectWorn.js, line 9

Parameters:

  • list Array
Exclude from a list of assets all assets that are not worn by player.

Returns:

Array
set(props) → {adventurejs.Parser}

Defined in: adventure/Parser.js, line 268

Parameters:

  • props Object
    A generic object containing properties to copy to the Object instance.
Provides a chainable shortcut method for setting a number of properties on the instance.

Returns:

adventurejs.Parser Returns the instance the method is called on (useful for chaining calls.)
splitByPeriods(input) → {Array}

Defined in: adventure/parser/splitByPeriods.js, line 9

Parameters:

  • input String
Split distinct sentences by period.

Returns:

Array
splitByThens(input) → {String}

Defined in: adventure/parser/splitByThens.js, line 9

Parameters:

  • input String
Each clause separated by 'then' is a distinct input. We treat clauses as unrelated without depencencies and stack each into a queue.

Example: "take sword then go north"

Returns:

String
stripArticles(input) → {String}

Defined in: adventure/parser/stripArticles.js, line 9

Parameters:

  • input String
    Player input.

Convert some common compound prepositions into single words to streamline preposition handling.

Returns:

String
stripConjunctions(input) → {String}

Defined in: adventure/parser/stripConjunctions.js, line 9

Parameters:

  • input String
    Player input.

In order to handle sentences with multiple clauses connected by conjunctions 'and' or 'but', we convert them to symbols we can use down the line.

Returns:

String
verifySentence()

Defined in: adventure/parser/verifySentence.js, line 9

After parse, verify each word in sentence.
verifySentenceStructure(input, this_turn)

Defined in: adventure/parser/verifySentenceStructure.js, line 9

Parameters:

  • input Object
    A reference to the current turn's input object.
  • this_turn Object
    A reference to the current turn's input object.
Verify that the active verb accepts the sentence structure.

Properties  | 

custom_parsers :Object

Defined in: adventure/Parser.js, line 76

Default value: {}

Used to store custom parser code that might be written by authors.
custom_parsers_enabled :Object

Defined in: adventure/Parser.js, line 83

Default value: {}

If custom parsers have been written by author, this provides a way to turn them on/off, something you might want to do depending on context.
dictionary :Object

Defined in: adventure/Parser.js, line 62

Default value: {}

A reference back to the main Game Dictionary object.
display :Object

Defined in: adventure/Parser.js, line 69

Default value: {}

A reference back to the main Game Display object.
game :Object

Defined in: adventure/Parser.js, line 55

Default value: {}

A reference back to the main Game object.
input_history :Array

Defined in: adventure/Parser.js, line 106

Default value: [{input:"wait"}]

Stores all player input. Used for Undo and Again, and in letting player use arrow keys to recall prior turns' input, as in a terminal application.
input_history_index :int

Defined in: adventure/Parser.js, line 120

When player uses arrow keys to navigate between old inputs, this stores player's position in the history array.
input_object :Object

Defined in: adventure/Parser.js, line 136

Default value: {}

Todos: Is this irrelevant?

Unused?
input_queue :Array

Defined in: adventure/Parser.js, line 127

Default value: []

When player uses conjuctive clauses joined by 'and' or 'then' we split the input into separate inputs and queue them up to handle in sequence. Queued inputs are saved here.
input_string :String

Defined in: adventure/Parser.js, line 92

Default value: ""

Used to store a copy of player input when player hits enter and before parsing.
is_input_queued :Boolean

Defined in: adventure/Parser.js, line 144

Default value: false

Save (this.input_queue.length > 0) during parse, because at some point later in the parse we shift input_queue, but later than that we still need to know whether we're queued.
joins :Array

Defined in: adventure/parser/joinCompoundVerbs.js, line 9

List of methods to be called during parse that perform regex operations on input in order to join compound verb phrases.