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

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

  var p = adventurejs.Parser.prototype;

  /**
   * Look for "strings", meaning arbitrary
   * quote delimited substrings that aren't meant
   * to be parsed, and replace with the GlobalString
   * placeholder class.
   * <br><br>
   * Example:<br>
   * type "foo" on keyboard<br>
   * Becomes:<br>
   * type global_string on keyboard<br>
   * @memberOf adventurejs.Parser
   * @method adventurejs.Parser#parseStrings
   * @param {String} input Player input.
   * @returns {String}
   */
  p.parseStrings = function Parser_parseStrings(input) {
    this.game.log(
      "L1559",
      "log",
      "high",
      `[parseStrings.js] parseStrings() receive: ${input}`,
      "Parser"
    );
    this.game.world.global_string.set({ values: [] });

    // originally only searched for double quotes
    //var re = /"(.*?)"/g;

    // now search for both double and single quotes
    var re1 = /"(.*?)"/g; // search for double quotes
    var re2 = /'(.*?)'/g; // search for single quotes
    var reS = [re1, re2];

    for (var i = 0; i < reS.length; i++) {
      var current;
      var counter = 0;
      while ((current = reS[i].exec(input))) {
        this.game.log(
          "L1278",
          "log",
          "high",
          "parseStrings > found quoted substring " +
            counter +
            ": " +
            current[0],
          "Parser"
        );
        input = input.replace(current[0], "global_string");
        let str = current[0].replace(/^"(.*)"$/, "$1");

        // save it to the globel string object
        this.game.world.global_string.values.push(str);
        // @NOTE global_string.values gets reset at beginning of each turn
        // save it to input as a backup
        this.game.getInput().strings.push(str);

        counter++;
      }
    }

    this.game.log(
      "L1279",
      "log",
      "high",
      `[parseStrings.js] parseStrings() return:\n${input}`,
      "Parser"
    );
    return input;
  };
})();