Code coverage report for sc/lang/compiler/parser/interpolate-string.js

Statements: 100% (54 / 54)      Branches: 100% (16 / 16)      Functions: 100% (7 / 7)      Lines: 100% (54 / 54)      Ignored: none     

All files » sc/lang/compiler/parser/ » interpolate-string.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 961     1   1 13     1 25     1 13     1 13 13   13 13 13 17 17 7   10 10 2     10 10   10 10 9     10     13 7     13     1 17   17 95   11 10   1   1 1   85     7     1 10   10 10 43   9 8   1 1   1 1   35     2     1    
(function(sc) {
  "use strict";
 
  require("../compiler");
 
  function InterpolateString(str) {
    this.str = str;
  }
 
  InterpolateString.hasInterpolateString = function(str) {
    return (/(?:^|[^\\\\])#\{/).test(str);
  };
 
  InterpolateString.prototype.toCompiledString = function() {
    return toCompiledString(this.str);
  };
 
  function toCompiledString(str) {
    var len = str.length;
    var items = [];
 
    var index1 = 0;
    var code;
    do {
      var index2 = findString(str, index1);
      if (index2 >= len) {
        break;
      }
      code = str.substr(index1, index2 - index1);
      if (code) {
        items.push('"' + code + '"');
      }
 
      index1 = index2 + 2;
      index2 = findExpression(str, index1, items);
 
      code = str.substr(index1, index2 - index1);
      if (code) {
        items.push("(" + code + ").asString");
      }
 
      index1 = index2 + 1;
    } while (index1 < len);
 
    if (index1 < len) {
      items.push('"' + str.substr(index1) + '"');
    }
 
    return items.join("++");
  }
 
  function findString(str, index) {
    var len = str.length;
 
    while (index < len) {
      switch (str.charAt(index)) {
      case "#":
        if (str.charAt(index + 1) === "{") {
          return index;
        }
        break;
      case "\\":
        index += 1;
        break;
      }
      index += 1;
    }
 
    return index;
  }
 
  function findExpression(str, index) {
    var len = str.length;
 
    var depth = 0;
    while (index < len) {
      switch (str.charAt(index)) {
      case "}":
        if (depth === 0) {
          return index;
        }
        depth -= 1;
        break;
      case "{":
        depth += 1;
        break;
      }
      index += 1;
    }
 
    return index;
  }
 
  sc.lang.compiler.InterpolateString = InterpolateString;
})(sc);