
/**
 * Expression and identifier regexp.
 * @param  {RegExp}
 */

var identifier = new RegExp(/\.\w+|\w+ *\(|"[^"]*"|'[^']*'|\/([^/]+)\/|[a-zA-Z_]\w*/g);
var brackets = new RegExp(/\$\{([^{}]*)\}/g);


/**
 * Substitute data variables in
 * template string.
 *
 * Examples:
 *
 *   var tmpl = mouth('${ beep }');
 *   tmpl(data);
 *   
 * @param  {String} str
 * @return {String}
 * @api public
 */


module.exports = function(str) {
  var props = [];
  var text = str.replace(brackets, function(_, expr) {
    return '"+' + compile(expr, props) + '+"';
  });
  return {
    text: Function('o', 'return "' + text + '"'),
    props: props
  };
};


/**
 * Replace expression
 * identifier.
 *
 * Examples:
 *
 *   compile('name + last');
 *   // => o.name + o.last
 *
 *   compile('name[0]');
 *   // => o.name[0]
 *   
 * @param  {String} str
 * @return {String}
 * @api private
 */

function compile(str, arr) {
  return str.replace(identifier, function(expr) {
    if(arr.indexOf(expr) < 0) arr.push(expr);
    return 'o.' + expr;
  });
}



@@ -207,75 +207,74 @@
 %0A * 
-Substitute data variables in%0A * template string.%0A *%0A * Examples:%0A *
+Compile expression.%0A *%0A * Examples:%0A %C4%99%0A *   // compiled expression
 %0A * 
@@ -319,16 +319,21 @@
 tmpl
+.text
 (data);%0A
  *  
@@ -328,16 +328,74 @@
 (data);%0A
+ *%0A *   // expresssion unique identifiers%0A *   tmp.props;%0A
  *   %0A *
@@ -420,38 +420,38 @@
 str%0A * @return %7B
-String
+Object
 %7D%0A * @api public

update code doc