Code coverage report for src/closer.js

Statements: 93.81% (91 / 97)      Branches: 81.58% (31 / 38)      Functions: 92.31% (12 / 13)      Lines: 93.81% (91 / 97)     

All files » src/ » closer.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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 1741 1   1   1   1   1   1 34         1 14476 14476 14476 14476           1 2789 2789 2789 2789     1 11137 10161   976                         1   1   1 1019 1019 1019     1 1 1018 1018 1018 1018 1018 1018     1       1   1 6 6         6 6 6 6 5 5 5   1   6 6 64 64 11 53 2 2 2     2             4 4 7 7   4     1 1 1018     1 1018 1018 6 6 4   3 3 3 3   6 1 1 1     1012   998     1       1   1018 31   1018         1   1       1          
(function() {
  var Closer, Parser, balanceDelimiters, builder, closer, con, nodes, oldParse, parser;
 
  parser = require('./parser').parser;
 
  nodes = require('./nodes');
 
  builder = {};
 
  nodes.defineNodes(builder);
 
  for (con in builder) {
    parser.yy[con] = function(a, b, c, d, e, f, g, h) {
      return builder[con](a, b, c, d, e, f, g, h);
    };
  }
 
  parser.yy.Node = function(type, a, b, c, d, e, f, g, h) {
    var buildName;
    buildName = type[0].toLowerCase() + type.slice(1);
    Eif (builder && buildName in builder) {
      return builder[buildName](a, b, c, d, e, f, g, h);
    } else {
      throw new ReferenceError("no such node type: " + type);
    }
  };
 
  parser.yy.locComb = function(start, end) {
    start.last_line = end.last_line;
    start.last_column = end.last_column;
    start.range = [start.range[0], end.range[1]];
    return start;
  };
 
  parser.yy.loc = function(loc) {
    if (!this.locations) {
      return null;
    }
    return {
      start: {
        line: this.startLine + loc.first_line - 1,
        column: loc.first_column
      },
      end: {
        line: this.startLine + loc.last_line - 1,
        column: loc.last_column
      },
      range: loc.range
    };
  };
 
  parser.lexer.options.ranges = true;
 
  oldParse = parser.parse;
 
  parser.parse = function(source, options) {
    this.yy.raw = [];
    this.yy.options = options;
    return oldParse.call(this, source);
  };
 
  Parser = (function() {
    function Parser(options) {
      this.yy.locs = options.loc !== false;
      this.yy.ranges = options.range === true;
      this.yy.locations = this.yy.locs || this.yy.ranges;
      this.yy.source = options.source || null;
      this.yy.startLine = options.line || 1;
      nodes.forceNoLoc = options.forceNoLoc;
    }
 
    return Parser;
 
  })();
 
  Parser.prototype = parser;
 
  balanceDelimiters = function(source) {
    var c, close, delims, existingClose, last, match, open, _i, _j, _len, _len1;
    match = {
      '(': ')',
      '[': ']',
      '{': '}'
    };
    open = /[(\[{]/g;
    close = /[)\]}]/g;
    existingClose = source.match(/[ \r\n)\]}]+$/);
    if (existingClose) {
      existingClose = existingClose[0];
      source = source.replace(existingClose, '');
      existingClose = existingClose.replace(/[ \r\n]+/g, '');
    } else {
      existingClose = '';
    }
    delims = [];
    for (_i = 0, _len = source.length; _i < _len; _i++) {
      c = source[_i];
      if (c.match(open)) {
        delims.push(c);
      } else if (c.match(close)) {
        last = delims[delims.length - 1];
        Eif (last) {
          Iif (c === match[last]) {
            delims.pop();
          } else {
            throw new Error("unmatched existing delimiters, can't balance");
          }
        } else {
          throw new Error("too many closing delimiters, can't balance");
        }
      }
    }
    delims.reverse();
    for (_j = 0, _len1 = delims.length; _j < _len1; _j++) {
      c = delims[_j];
      source += match[c];
    }
    return [source, delims.length - existingClose.length];
  };
 
  Closer = (function() {
    function Closer(options) {
      this.parser = new Parser(options);
    }
 
    Closer.prototype.parse = function(source, options) {
      var ast, e, unbalancedCount, _ref;
      if (options.loose === true) {
        try {
          _ref = balanceDelimiters(source), source = _ref[0], unbalancedCount = _ref[1];
          ast = this.parser.parse(source, options);
        } catch (_error) {
          e = _error;
          source = '';
          unbalancedCount = 0;
          ast = this.parser.parse(source, options);
        }
        if (!e && unbalancedCount > 0) {
          e = new Error("Missing " + unbalancedCount + " closing delimiters");
          e.startOffset = e.endOffset = source.length - 1;
          ast.errors = [e];
        }
      } else {
        ast = this.parser.parse(source, options);
      }
      return ast;
    };
 
    return Closer;
 
  })();
 
  closer = {
    parse: function(src, options) {
      if (options == null) {
        options = {};
      }
      return new Closer(options).parse(src, options);
    },
    node: parser.yy.Node
  };
 
  module.exports = closer;
 
  Iif (typeof self !== "undefined" && self !== null) {
    self.closer = closer;
  }
 
  Iif (typeof window !== "undefined" && window !== null) {
    window.closer = closer;
  }
 
}).call(this);