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

Statements: 100% (32 / 32)      Branches: 100% (8 / 8)      Functions: 100% (5 / 5)      Lines: 100% (32 / 32)      Ignored: none     

All files » sc/lang/compiler/parser/ » string-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 611     1 1 1   1 1 1 1 1 1             1 15     1 15   1   1 15   15 1     14   14 8 8 4   4       14     1 22   22 6 6     16      
(function(sc) {
  "use strict";
 
  require("./parser");
  require("./interpolate-string");
  require("../lexer/lexer");
 
  var Syntax = sc.lang.compiler.Syntax;
  var Token = sc.lang.compiler.Token;
  var Node = sc.lang.compiler.Node;
  var Lexer = sc.lang.compiler.Lexer;
  var InterpolateString = sc.lang.compiler.InterpolateString;
  var Parser = sc.lang.compiler.Parser;
 
  /*
    StringExpression :
      StringLiteral
      StringLiterals StringLiteral
  */
  Parser.addParseMethod("StringExpression", function() {
    return new StringExpressionParser(this).parse();
  });
 
  function StringExpressionParser(parent) {
    Parser.call(this, parent);
  }
  sc.libs.extend(StringExpressionParser, Parser);
 
  StringExpressionParser.prototype.parse = function() {
    var marker = this.createMarker();
 
    if (this.lookahead.type !== Token.StringLiteral) {
      this.throwUnexpected(this.lookahead);
    }
 
    var expr = this.parseStringLiteral();
 
    while (this.lookahead.type === Token.StringLiteral) {
      var next = this.parseStringLiteral();
      if (expr.type === Syntax.Literal && next.type === Syntax.Literal) {
        expr.value += next.value;
      } else {
        expr = Node.createBinaryExpression({ value: "++" }, expr, next);
      }
    }
 
    return marker.update().apply(expr, true);
  };
 
  StringExpressionParser.prototype.parseStringLiteral = function() {
    var token = this.lex();
 
    if (InterpolateString.hasInterpolateString(token.value)) {
      var code = new InterpolateString(token.value).toCompiledString();
      return new Parser(null, new Lexer(code, {})).parseExpression();
    }
 
    return Node.createLiteral(token);
  };
})(sc);