Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// tokenizeBut.js

(function () {
  /* global AdventureJS A */

  var p = AdventureJS.Parser.prototype;

  /**
   * <p>
   * In order to handle sentences with multiple nouns
   * connected by 'but', we convert 'but' to a symbol
   * we can use down the line.
   * </p>
   * @memberOf AdventureJS.Parser
   * @method AdventureJS.Parser#tokenizeBut
   * @param {String} input_string Player input string.
   * @returns {String}
   */
  p.tokenizeBut = function Parser_tokenizeBut(input_string) {
    this.game.log(
      "L1707",
      "log",
      "high",
      `[tokenizeBut.js] tokenizeBut() receive: ${input_string}`,
      "Parser"
    );
    let this_turn = this.input_history[0];

    /* *
     * Convert " but " and  " except " to " -". Used
     * to omit specific items from collections.
     * Example:
     *  take all but sword
     * We save this to tokens for printing back the player's input
     * @TODO correctly parse
     * "take all from knapsack but coinpurse"
     * where but follows the direct object
     * currently this is parsed as
     * > take all from knapsack -coinpurse
     *
     * @TODO this only handles one "but" or "except"
     * which is not very robust
     */
    input_string = input_string.replace(/\s(but|except)\s/, (_, word) => {
      let context = `parser.tokenizeBut() found '${word}' and replaced it with ' -'`;
      this_turn.tokens[" -"] = {
        key: " -",
        source: ` ${word} `,
        context: context,
      };
      return " -";
    });

    this.game.log(
      "L1708",
      "log",
      "high",
      `[tokenizeBut.js] tokenizeBut() return:\n${input_string}`,
      "Parser"
    );
    return input_string;
  };
})();