Code coverage report for sc/lang/compiler/parser/list-expr.js

Statements: 100% (43 / 43)      Branches: 100% (10 / 10)      Functions: 100% (7 / 7)      Lines: 100% (43 / 43)      Ignored: none     

All files » sc/lang/compiler/parser/ » list-expr.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 961     1   1 1 1                             1 42             1 19 1   18   18   17 17 14   14     1 42   1   1 42   42   39   38   38         1 39 39   39 39 41 40 17       38   38     1 41 2     39 38 2 2     38      
(function(sc) {
  "use strict";
 
  require("./parser");
 
  var Token = sc.lang.compiler.Token;
  var Node = sc.lang.compiler.Node;
  var Parser = sc.lang.compiler.Parser;
 
  /*
    ListExpression :
      [ ListElements(opts) ]
 
    ListElements :
      ListElement
      ListElements , ListElement
 
    ListElement :
      Label         Expressions
      Expressions : Expressions
      Expressions
  */
  Parser.addParseMethod("ListExpression", function() {
    return new ListExpressionParser(this).parse();
  });
 
  /*
    ImmutableListExpression :
      # ListExpression
  */
  Parser.addParseMethod("ImmutableListExpression", function() {
    if (this.state.immutableList) {
      this.throwUnexpected(this.lookahead);
    }
    var marker = this.createMarker();
 
    this.expect("#");
 
    this.state.immutableList = true;
    var expr = this.parseListExpression();
    this.state.immutableList = false;
 
    return marker.update().apply(expr, true);
  });
 
  function ListExpressionParser(parent) {
    Parser.call(this, parent);
  }
  sc.libs.extend(ListExpressionParser, Parser);
 
  ListExpressionParser.prototype.parse = function() {
    var marker = this.createMarker();
 
    this.expect("[");
 
    var elements = this.parseListElements();
 
    this.expect("]");
 
    return marker.update().apply(
      Node.createListExpression(elements, this.state.immutableList)
    );
  };
 
  ListExpressionParser.prototype.parseListElements = function() {
    var innerElements = this.state.innerElements;
    this.state.innerElements = true;
 
    var elements = [];
    while (this.hasNextToken() && !this.match("]")) {
      elements.push.apply(elements, this.parseListElement());
      if (!this.match("]")) {
        this.expect(",");
      }
    }
 
    this.state.innerElements = innerElements;
 
    return elements;
  };
 
  ListExpressionParser.prototype.parseListElement = function() {
    if (this.lookahead.type === Token.Label) {
      return [ this.parseLabelAsSymbol(), this.parseExpressions() ];
    }
 
    var elements = [ this.parseExpressions() ];
    if (this.match(":")) {
      this.lex();
      elements.push(this.parseExpressions());
    }
 
    return elements;
  };
})(sc);