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

Statements: 100% (67 / 67)      Branches: 100% (22 / 22)      Functions: 100% (17 / 17)      Lines: 100% (67 / 67)      Ignored: none     

All files » sc/lang/compiler/parser/ » parser.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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 1331     1 1 1 1   1 1   1 3066 513   2553 2553 2553       1 513 513 513               1 33     1   3338       1 85     1 1437     1 81 81     1 1025 1025 20   1005     1 4621     1 2643 2643 667   1976     1 4321     1 1264     1 97     1 90   5     8             8   3   1   18   47     1 104 6   98     1 186   186   186 186 175   175     1    
(function(sc) {
  "use strict";
 
  require("../compiler");
  require("../lexer/lexer");
  require("../node/node");
  require("../marker/marker");
 
  var Token = sc.lang.compiler.Token;
  var Message = sc.lang.compiler.Message;
 
  function Parser(parent, lexer) {
    if (!parent) {
      initialize(this, lexer);
    } else {
      this.parent = parent;
      this.lexer = parent.lexer;
      this.state = parent.state;
    }
  }
 
  function initialize(that, lexer) {
    that.parent = null;
    that.lexer = lexer;
    that.state = {
      innerElements: false,
      immutableList: false,
      declared: {},
      underscore: []
    };
  }
 
  Parser.addParseMethod = function(name, method) {
    Parser.prototype["parse" + name] = method;
  };
 
  Object.defineProperty(Parser.prototype, "lookahead", {
    get: function() {
      return this.lexer.lookahead;
    }
  });
 
  Parser.prototype.parse = function() {
    return this.parseProgram();
  };
 
  Parser.prototype.lex = function() {
    return this.lexer.lex();
  };
 
  Parser.prototype.unlex = function(token) {
    this.lexer.unlex(token);
    return this;
  };
 
  Parser.prototype.expect = function(value) {
    var token = this.lexer.lex();
    if (token.type !== Token.Punctuator || token.value !== value) {
      this.throwUnexpected(token, value);
    }
    return token;
  };
 
  Parser.prototype.match = function(value) {
    return this.lexer.lookahead.value === value;
  };
 
  Parser.prototype.matchAny = function(list) {
    var value = this.lexer.lookahead.value;
    if (list.indexOf(value) !== -1) {
      return value;
    }
    return null;
  };
 
  Parser.prototype.createMarker = function(node) {
    return this.lexer.createMarker(node);
  };
 
  Parser.prototype.hasNextToken = function() {
    return this.lookahead.type !== Token.EOF;
  };
 
  Parser.prototype.throwError = function() {
    return this.lexer.throwError.apply(this.lexer, arguments);
  };
 
  Parser.prototype.throwUnexpected = function(token) {
    switch (token.type) {
    case Token.EOF:
      return this.throwError(token, Message.UnexpectedEOS);
    case Token.FloatLiteral:
    case Token.IntegerLiteral:
      return this.throwError(token, Message.UnexpectedNumber);
    case Token.CharLiteral:
    case Token.StringLiteral:
    case Token.SymbolLiteral:
    case Token.TrueLiteral:
    case Token.FalseLiteral:
    case Token.NilLiteral:
      return this.throwError(token, Message.UnexpectedLiteral, token.type.toLowerCase());
    case Token.Keyword:
      return this.throwError(token, Message.UnexpectedKeyword, token.value);
    case Token.Label:
      return this.throwError(token, Message.UnexpectedLabel, token.value);
    case Token.Identifier:
      return this.throwError(token, Message.UnexpectedIdentifier, token.value);
    }
    return this.throwError(token, Message.UnexpectedToken, token.value);
  };
 
  Parser.prototype.addToScope = function(type, name) {
    if (this.state.declared[name]) {
      this.throwError({}, Message.Redeclaration, type, name);
    }
    this.state.declared[name] = true;
  };
 
  Parser.prototype.withScope = function(func) {
    var result;
 
    var declared = this.state.declared;
 
    this.state.declared = {};
    result = func.call(this);
    this.state.declared = declared;
 
    return result;
  };
 
  sc.lang.compiler.Parser = Parser;
})(sc);