Code coverage report for sc/lang/compiler/parser/parentheses.js

Statements: 100% (56 / 56)      Branches: 100% (18 / 18)      Functions: 100% (18 / 18)      Lines: 100% (56 / 56)      Ignored: none     

All files » sc/lang/compiler/parser/ » parentheses.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 1101     1   1 1                   1 37     1 37   1   1 37   36 2     36   36   36     1 36 6 6     30 2 2     28 6 6       22 22 2 2     20 4 4     16 8 8 8 4 4     4 4       8 8       1 4 4       1 8 8       1 12   12 12 12   12   12      
(function(sc) {
  "use strict";
 
  require("./parser");
 
  var Token = sc.lang.compiler.Token;
  var Parser = sc.lang.compiler.Parser;
 
  /*
    (...)
      EventExpression
      SeriesExpression
      BlockExpression
      ( Expressions )
      ( Expression  )
  */
  Parser.addParseMethod("Parentheses", function() {
    return new ParenthesesParser(this).parse();
  });
 
  function ParenthesesParser(parent) {
    Parser.call(this, parent);
  }
  sc.libs.extend(ParenthesesParser, Parser);
 
  ParenthesesParser.prototype.parse = function() {
    var token = this.expect("(");
 
    if (this.match(":")) {
      this.lex();
    }
 
    var delegate = this.selectParenthesesParseMethod();
 
    this.unlex(token);
 
    return delegate.call(this);
  };
 
  ParenthesesParser.prototype.selectParenthesesParseMethod = function() {
    if (this.lookahead.type === Token.Label || this.match(")")) {
      return function() {
        return this.parseEventExpression();
      };
    }
    if (this.match("var")) {
      return function() {
        return this.parseBlockExpression();
      };
    }
    if (this.match("..")) {
      return function() {
        return this.parseSeriesExpression();
      };
    }
 
    this.parseExpression();
    if (this.matchAny([ ",", ".." ])) {
      return function() {
        return this.parseSeriesExpression();
      };
    }
    if (this.match(":")) {
      return function() {
        return this.parseEventExpression();
      };
    }
    if (this.match(";")) {
      this.lex();
      this.parseExpressions();
      if (this.matchAny([ ",", ".." ])) {
        return function() {
          return this.parseSeriesExpression();
        };
      }
      return function() {
        return this.parseExpressionsWithParentheses();
      };
    }
 
    return function() {
      return this.parsePartialExpressionWithParentheses();
    };
  };
 
  ParenthesesParser.prototype.parseExpressionsWithParentheses = function() {
    return this.parseWithParentheses(function() {
      return this.parseExpressions();
    });
  };
 
  ParenthesesParser.prototype.parsePartialExpressionWithParentheses = function() {
    return this.parseWithParentheses(function() {
      return this.parsePartialExpression();
    });
  };
 
  ParenthesesParser.prototype.parseWithParentheses = function(delegate) {
    this.expect("(");
 
    var marker = this.createMarker();
    var expr = delegate.call(this);
    expr = marker.update().apply(expr);
 
    this.expect(")");
 
    return expr;
  };
})(sc);