Code coverage report for sc/lang/compiler/parser/var-stmt.js

Statements: 100% (43 / 43)      Branches: 100% (4 / 4)      Functions: 100% (11 / 11)      Lines: 100% (43 / 43)      Ignored: none     

All files » sc/lang/compiler/parser/ » var-stmt.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 1001     1   1 1                   1 43               1 96     1 43   1   1 43   43   43     39   39   37     1 43   43 57 53 39   14     39     1 57     19         1 96   1   1 96   96 94   90   86         1 90 59   31   31      
(function(sc) {
  "use strict";
 
  require("./parser");
 
  var Node = sc.lang.compiler.Node;
  var Parser = sc.lang.compiler.Parser;
 
  /*
    VariableStatement :
      var VariableDeclarators ;
 
    VariableDeclarators :
      Declarator
      VariableDeclarators , Declarator
  */
  Parser.addParseMethod("VariableStatement", function(opts) {
    return new VariableStatementParser(this).parse(opts);
  });
 
  /*
    Declarator :
      Identifier
      Identifier = AssignmentExpression
  */
  Parser.addParseMethod("Declarator", function(opts) {
    return new DeclaratorParser(this).parse(opts);
  });
 
  function VariableStatementParser(parent) {
    Parser.call(this, parent);
  }
  sc.libs.extend(VariableStatementParser, Parser);
 
  VariableStatementParser.prototype.parse = function() {
    var marker = this.createMarker();
 
    this.lex(); // var
 
    var declaration = Node.createVariableDeclaration(
      this.parseVariableDeclarators(), "var"
    );
    declaration = marker.update().apply(declaration);
 
    this.expect(";");
 
    return declaration;
  };
 
  VariableStatementParser.prototype.parseVariableDeclarators = function() {
    var list = [];
 
    do {
      list.push(this.parseVariableDeclarator());
      if (!this.match(",")) {
        break;
      }
      this.lex();
    } while (this.hasNextToken());
 
    return list;
  };
 
  VariableStatementParser.prototype.parseVariableDeclarator = function() {
    return this.parseDeclarator({
      type: "var",
      delegate: function() {
        return this.parseAssignmentExpression();
      }
    });
  };
 
  function DeclaratorParser(parent) {
    Parser.call(this, parent);
  }
  sc.libs.extend(DeclaratorParser, Parser);
 
  DeclaratorParser.prototype.parse = function(opts) {
    var marker = this.createMarker();
 
    var identifier = this.parseIdentifier({ variable: true });
    this.addToScope(opts.type, identifier.name);
 
    var initialValue = this.parseInitialiser(opts.delegate);
 
    return marker.update().apply(
      Node.createVariableDeclarator(identifier, initialValue)
    );
  };
 
  DeclaratorParser.prototype.parseInitialiser = function(delegate) {
    if (!this.match("=")) {
      return null;
    }
    this.lex();
 
    return delegate.call(this);
  };
})(sc);