"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/cson-parser/lib/parse.js" (8 Mar 2017, 6672 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Generated by CoffeeScript 1.9.0
    2 
    3 /*
    4 Copyright (c) 2014, Groupon, Inc.
    5 All rights reserved.
    6 
    7 Redistribution and use in source and binary forms, with or without
    8 modification, are permitted provided that the following conditions
    9 are met:
   10 
   11 Redistributions of source code must retain the above copyright notice,
   12 this list of conditions and the following disclaimer.
   13 
   14 Redistributions in binary form must reproduce the above copyright
   15 notice, this list of conditions and the following disclaimer in the
   16 documentation and/or other materials provided with the distribution.
   17 
   18 Neither the name of GROUPON nor the names of its contributors may be
   19 used to endorse or promote products derived from this software without
   20 specific prior written permission.
   21 
   22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
   23 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   24 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
   25 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
   26 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   27 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
   28 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   30 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   31 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   33  */
   34 var defaultReviver, nodeTypeString, nodes, parse, parseStringLiteral, runInThisContext, syntaxErrorMessage;
   35 
   36 runInThisContext = require('vm').runInThisContext;
   37 
   38 nodes = require('coffee-script').nodes;
   39 
   40 defaultReviver = function(key, value) {
   41   return value;
   42 };
   43 
   44 nodeTypeString = function(csNode) {
   45   return csNode.constructor.name;
   46 };
   47 
   48 syntaxErrorMessage = function(csNode, msg) {
   49   var column, columnIdx, line, lineIdx, _ref;
   50   _ref = csNode.locationData, lineIdx = _ref.first_line, columnIdx = _ref.first_column;
   51   if (lineIdx != null) {
   52     line = lineIdx + 1;
   53   }
   54   if (columnIdx != null) {
   55     column = columnIdx + 1;
   56   }
   57   return "Syntax error on line " + line + ", column " + column + ": " + msg;
   58 };
   59 
   60 parseStringLiteral = function(literal) {
   61   return runInThisContext(literal);
   62 };
   63 
   64 parse = function(source, reviver) {
   65   var coffeeAst, contextObj, isLiteral, nodeTransforms, parsed, transformKey, transformNode;
   66   if (reviver == null) {
   67     reviver = defaultReviver;
   68   }
   69   nodeTransforms = {
   70     Block: function(node) {
   71       var expressions;
   72       expressions = node.expressions;
   73       if (!expressions || expressions.length !== 1) {
   74         throw new SyntaxError(syntaxErrorMessage(node, 'One top level value expected'));
   75       }
   76       return transformNode(expressions[0]);
   77     },
   78     Value: function(node) {
   79       return transformNode(node.base);
   80     },
   81     Bool: function(node) {
   82       return node.val === 'true';
   83     },
   84     Null: function() {
   85       return null;
   86     },
   87     Literal: function(node) {
   88       var err, value;
   89       value = node.value;
   90       try {
   91         switch (value.charAt(0)) {
   92           case "'":
   93           case '"':
   94             return parseStringLiteral(value);
   95           default:
   96             return JSON.parse(value);
   97         }
   98       } catch (_error) {
   99         err = _error;
  100         throw new SyntaxError(syntaxErrorMessage(node, err.message));
  101       }
  102     },
  103     Arr: function(node) {
  104       return node.objects.map(transformNode);
  105     },
  106     Obj: function(node) {
  107       return node.properties.reduce(function(outObject, property) {
  108         var keyName, value, variable;
  109         variable = property.variable, value = property.value;
  110         if (!variable) {
  111           return outObject;
  112         }
  113         keyName = transformKey(variable);
  114         value = transformNode(value);
  115         outObject[keyName] = reviver.call(outObject, keyName, value);
  116         return outObject;
  117       }, {});
  118     },
  119     Op: function(node) {
  120       var left, right;
  121       if (node.second != null) {
  122         left = transformNode(node.first);
  123         right = transformNode(node.second);
  124         switch (node.operator) {
  125           case '-':
  126             return left - right;
  127           case '+':
  128             return left + right;
  129           case '*':
  130             return left * right;
  131           case '/':
  132             return left / right;
  133           case '%':
  134             return left % right;
  135           case '&':
  136             return left & right;
  137           case '|':
  138             return left | right;
  139           case '^':
  140             return left ^ right;
  141           case '<<':
  142             return left << right;
  143           case '>>>':
  144             return left >>> right;
  145           case '>>':
  146             return left >> right;
  147           default:
  148             throw new SyntaxError(syntaxErrorMessage(node, "Unknown binary operator " + node.operator));
  149         }
  150       } else {
  151         switch (node.operator) {
  152           case '-':
  153             return -transformNode(node.first);
  154           case '~':
  155             return ~transformNode(node.first);
  156           default:
  157             throw new SyntaxError(syntaxErrorMessage(node, "Unknown unary operator " + node.operator));
  158         }
  159       }
  160     },
  161     Parens: function(node) {
  162       var expressions;
  163       expressions = node.body.expressions;
  164       if (!expressions || expressions.length !== 1) {
  165         throw new SyntaxError(syntaxErrorMessage(node, 'Parenthesis may only contain one expression'));
  166       }
  167       return transformNode(expressions[0]);
  168     }
  169   };
  170   isLiteral = function(csNode) {
  171     return LiteralTypes.some(function(LiteralType) {
  172       return csNode instanceof LiteralType;
  173     });
  174   };
  175   transformKey = function(csNode) {
  176     var type, value;
  177     type = nodeTypeString(csNode);
  178     if (type !== 'Value') {
  179       throw new SyntaxError(syntaxErrorMessage(csNode, type + " used as key"));
  180     }
  181     value = csNode.base.value;
  182     switch (value.charAt(0)) {
  183       case "'":
  184       case '"':
  185         return parseStringLiteral(value);
  186       default:
  187         return value;
  188     }
  189   };
  190   transformNode = function(csNode) {
  191     var transform, type;
  192     type = nodeTypeString(csNode);
  193     transform = nodeTransforms[type];
  194     if (!transform) {
  195       throw new SyntaxError(syntaxErrorMessage(csNode, "Unexpected " + type));
  196     }
  197     return transform(csNode);
  198   };
  199   if (typeof reviver !== 'function') {
  200     throw new TypeError("reviver has to be a function");
  201   }
  202   coffeeAst = nodes(source.toString('utf8'));
  203   parsed = transformNode(coffeeAst);
  204   if (reviver === defaultReviver) {
  205     return parsed;
  206   }
  207   contextObj = {};
  208   contextObj[''] = parsed;
  209   return reviver.call(contextObj, '', parsed);
  210 };
  211 
  212 module.exports = parse;