Code coverage report for sc/lang/compiler/lexer/comment.js

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

All files » sc/lang/compiler/lexer/ » comment.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 911     1   1 1   1 15     1 15 15     1 15 15 15 15 6   9 8       1 6 6 6   6 6 6 114 114 114 5 5       6     1 8 8 8   8 8 8 109 109 109   109 10 99 13 13 13 86 12 12 12 12 7       102     1     1 13                
(function(sc) {
  "use strict";
 
  require("./lexer");
 
  var Token = sc.lang.compiler.Token;
  var Lexer = sc.lang.compiler.Lexer;
 
  Lexer.addLexMethod("Comment", function(source, index) {
    return new CommentLexer(source, index).scan();
  });
 
  function CommentLexer(source, index) {
    this.source = source;
    this.index = index;
  }
 
  CommentLexer.prototype.scan = function() {
    var source = this.source;
    var index = this.index;
    var op = source.charAt(index) + source.charAt(index + 1);
    if (op === "//") {
      return this.scanSingleLineComment();
    }
    if (op === "/*") {
      return this.scanMultiLineComment();
    }
  };
 
  CommentLexer.prototype.scanSingleLineComment = function() {
    var source = this.source;
    var index = this.index;
    var length = source.length;
 
    var value = "";
    var line = 0;
    while (index < length) {
      var ch = source.charAt(index++);
      value += ch;
      if (ch === "\n") {
        line = 1;
        break;
      }
    }
 
    return makeCommentToken(Token.SingleLineComment, value, line);
  };
 
  CommentLexer.prototype.scanMultiLineComment = function() {
    var source = this.source;
    var index = this.index;
    var length = source.length;
 
    var value = "";
    var line = 0, depth = 0;
    while (index < length) {
      var ch1 = source.charAt(index);
      var ch2 = source.charAt(index + 1);
      value += ch1;
 
      if (ch1 === "\n") {
        line += 1;
      } else if (ch1 === "/" && ch2 === "*") {
        depth += 1;
        index += 1;
        value += ch2;
      } else if (ch1 === "*" && ch2 === "/") {
        depth -= 1;
        index += 1;
        value += ch2;
        if (depth === 0) {
          return makeCommentToken(Token.MultiLineComment, value, line);
        }
      }
 
      index += 1;
    }
 
    return { error: true, value: "ILLEGAL", length: length, line: line };
  };
 
  function makeCommentToken(type, value, line) {
    return {
      type: type,
      value: value,
      length: value.length,
      line: line|0
    };
  }
})(sc);