Code coverage report for lib/parser.js

Statements: 95.15% (98 / 103)      Branches: 92.86% (52 / 56)      Functions: 100% (10 / 10)      Lines: 95.15% (98 / 103)      Ignored: none     

All files » lib/ » parser.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 174 175 176 177 178 179 180 181 182 183 184 185 186 187                                                              1 42     42 42 42 42     1 11 11 24 24 24 22 22     2 2 2   2   22 22 9       9     11     1 55 55 160 160 10     150   160 160 105   55 55 55 50     5 5       1 160   160 160 160 2   158 124   34 34 24 24 24     1 160 105 105     1 55 8 8   47 22 22   25         1 160 160 16   144 31     113   129 129   1 34 29 29 51 51 5   46 22 22     24 24 24         1 77   1 184 184 7   177 166     11   18 18     1    
'use strict';
 
/*
file  
  :  row+ EOF  
  ;  
row
  :  value (Comma value)* (LineBreak | EOF)
  ;
value
  :  SimpleValue
  |  QuotedValue
  ;
Comma
  :  ','
  ;
LineBreak
  :  '\r'? '\n'
  |  '\r'
  ;
SimpleValue
  :  ~(',' | '\r' | '\n' | '"')+
  ;
QuotedValue
  :  Residue '"' ('""' | ~'"')* '"' Residue
  ;
 
Residue
  : (' ' | '\t' | '\f')*
*/
 
function Parser(input, comma) {
  Iif (!(this instanceof Parser)) {
    return new Parser(input, comma);
  }
  this.input = input;
  this.pointer = 0;
  this.linePointer = 0;
  this.comma = comma || ',';
}
 
Parser.prototype.File = function () {
  var files = [], row, eof;
  while (1) {
    var tempointer = this.pointer
    row = this.Row();
    if (row.length > 0) {
      this.linePointer = tempointer;
      files.push(row);
    }
    else {
      Eif (this.linePointer && this.pointer !== this.input.length) {
        files.pop();
        this.pointer = this.linePointer;
      }
      break;
    }
    eof = this.EOF();
    if (eof) {
      Iif (this.linePointer && this.pointer !== this.input.length) {
        files.pop();
        this.pointer = this.linePointer;
      }
      break;
    }
  }
  return files;
}
 
Parser.prototype.Row = function () {
  var value, comma, linebreak, eof, row = [];
  while (1) {
    value = this.Value();
    if (!value) {
      row.push('');
    }
    else {
      row.push(value);
    }
    comma = this.Comma();
    if (comma) {
      continue;
    }
    linebreak = this.LineBreak();
    eof = this.EOF();
    if (linebreak || eof) {
      return row;
    }
    else {
      row.pop();
      return row;
    }
  }
}
Parser.prototype.Value = function () {
  var simplevalue, quotedvalue, residue;
 
  residue = this.Residue();
  simplevalue = this.SimpleValue();
  if (simplevalue && residue) {
    return residue + simplevalue;
  }
  else if (simplevalue && !residue) {
    return simplevalue;
  }
  quotedvalue = this.QuotedValue();
  if (quotedvalue) {
    var value = quotedvalue.slice(1, -1).replace(/\"\"/g, '"');
    this.Residue();
    return value;
  }
}
Parser.prototype.Comma = function () {
  if (this.input.slice(this.pointer, this.pointer + this.comma.length) === this.comma) {
    this.pointer += this.comma.length;
    return this.comma;
  }
}
Parser.prototype.LineBreak = function () {
  if (this.input.slice(this.pointer, this.pointer + 2) === '\r\n') {
    this.pointer += 2;
    return '\r\n';
  }
  if (this.input.charAt(this.pointer) === '\n') {
    this.pointer += 1;
    return '\n';
  }
  Iif (this.input.charAt(this.pointer) === '\r') {
    this.pointer += 1;
    return '\r';
  }
}
Parser.prototype.SimpleValue = function () {
  var value = '', index = this.input.slice(this.pointer).search(new RegExp('[' + this.comma + '\r\n\"]'))
  if (index === -1) {
    value = this.input.slice(this.pointer);
  }
  else if (index === 0) {
    return;
  }
  else {
    value = this.input.slice(this.pointer, this.pointer + index);
  }
  this.pointer += value.length;
  return value;
}
Parser.prototype.QuotedValue = function () {
  if (this.input.charAt(this.pointer) === '"') {
    var searchIndex, index = 1;
     while (1) {
      searchIndex = this.input.slice(this.pointer + index).search('"');
      if (searchIndex === -1) {
        return;
      }
      if (this.input.charAt(this.pointer + index + searchIndex + 1) === '"') {
        index += searchIndex + 2;
        continue;
      }
      else {
        var value = this.input.slice(this.pointer, this.pointer + index + searchIndex + 1);
        this.pointer += value.length;
        return value;
      }
    }
  }
}
Parser.prototype.EOF = function () {
  return (this.pointer >= this.input.length)
}
Parser.prototype.Residue = function () {
  var value = '', index = this.input.slice(this.pointer).search(new RegExp('[^ \f\t\v\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000]'))
  if (index === -1) {
    value = this.input.slice(this.pointer);
  }
  else if (index === 0) {
    return;
  }
  else {
    value = this.input.slice(this.pointer, this.pointer + index);
  }
  this.pointer += value.length;
  return value;
}
 
module.exports = Parser;