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

Statements: 100% (38 / 38)      Branches: 100% (14 / 14)      Functions: 100% (5 / 5)      Lines: 100% (38 / 38)      Ignored: none     

All files » sc/lang/compiler/lexer/ » identifier.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 571     1   1 1 1   1 618     1 618 618     1   1 618 618   618 618   618 618 25 25 593 63 530 1 1 529 1 1 528 10 518 7 511 5   506     618     1 593      
(function(sc) {
  "use strict";
 
  require("./lexer");
 
  var Token = sc.lang.compiler.Token;
  var Keywords = sc.lang.compiler.Keywords;
  var Lexer = sc.lang.compiler.Lexer;
 
  Lexer.addLexMethod("Identifier", function(source, index) {
    return new IdentifierLexer(source, index).scan();
  });
 
  function IdentifierLexer(source, index) {
    this.source = source;
    this.index = index;
  }
 
  var re = /^(_|[a-zA-Z][a-zA-Z0-9_]*)/;
 
  IdentifierLexer.prototype.scan = function() {
    var source = this.source;
    var index  = this.index;
 
    var value = re.exec(source.slice(index))[0];
    var length = value.length;
 
    var type;
    if (source.charAt(index + length) === ":") {
      type = Token.Label;
      length += 1;
    } else if (isKeyword(value)) {
      type = Token.Keyword;
    } else if (value === "inf") {
      type = Token.FloatLiteral;
      value = "Infinity";
    } else if (value === "pi") {
      type = Token.FloatLiteral;
      value = String(Math.PI);
    } else if (value === "nil") {
      type = Token.NilLiteral;
    } else if (value === "true") {
      type = Token.TrueLiteral;
    } else if (value === "false") {
      type = Token.FalseLiteral;
    } else {
      type = Token.Identifier;
    }
 
    return { type: type, value: value, length: length };
  };
 
  function isKeyword(value) {
    return Keywords.hasOwnProperty(value);
  }
})(sc);