index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.plugins = undefined;
  6. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  7. var _getIterator3 = _interopRequireDefault(_getIterator2);
  8. var _getPrototypeOf = require("babel-runtime/core-js/object/get-prototype-of");
  9. var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
  10. var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
  11. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  12. var _createClass2 = require("babel-runtime/helpers/createClass");
  13. var _createClass3 = _interopRequireDefault(_createClass2);
  14. var _possibleConstructorReturn2 = require("babel-runtime/helpers/possibleConstructorReturn");
  15. var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
  16. var _inherits2 = require("babel-runtime/helpers/inherits");
  17. var _inherits3 = _interopRequireDefault(_inherits2);
  18. var _identifier = require("../util/identifier");
  19. var _options = require("../options");
  20. var _tokenizer = require("../tokenizer");
  21. var _tokenizer2 = _interopRequireDefault(_tokenizer);
  22. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  23. var plugins = exports.plugins = {};
  24. var Parser = function (_Tokenizer) {
  25. (0, _inherits3.default)(Parser, _Tokenizer);
  26. function Parser(options, input) {
  27. (0, _classCallCheck3.default)(this, Parser);
  28. options = (0, _options.getOptions)(options);
  29. var _this = (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(Parser).call(this, options, input));
  30. _this.options = options;
  31. _this.inModule = _this.options.sourceType === "module";
  32. _this.isReservedWord = _identifier.reservedWords[6];
  33. _this.input = input;
  34. _this.plugins = _this.loadPlugins(_this.options.plugins);
  35. _this.filename = options.sourceFilename;
  36. // If enabled, skip leading hashbang line.
  37. if (_this.state.pos === 0 && _this.input[0] === "#" && _this.input[1] === "!") {
  38. _this.skipLineComment(2);
  39. }
  40. return _this;
  41. }
  42. (0, _createClass3.default)(Parser, [{
  43. key: "hasPlugin",
  44. value: function hasPlugin(name) {
  45. return !!(this.plugins["*"] || this.plugins[name]);
  46. }
  47. }, {
  48. key: "extend",
  49. value: function extend(name, f) {
  50. this[name] = f(this[name]);
  51. }
  52. }, {
  53. key: "loadPlugins",
  54. value: function loadPlugins(plugins) {
  55. var pluginMap = {};
  56. if (plugins.indexOf("flow") >= 0) {
  57. // ensure flow plugin loads last
  58. plugins = plugins.filter(function (plugin) {
  59. return plugin !== "flow";
  60. });
  61. plugins.push("flow");
  62. }
  63. var _iteratorNormalCompletion = true;
  64. var _didIteratorError = false;
  65. var _iteratorError = undefined;
  66. try {
  67. for (var _iterator = (0, _getIterator3.default)(plugins), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  68. var name = _step.value;
  69. if (!pluginMap[name]) {
  70. pluginMap[name] = true;
  71. var plugin = exports.plugins[name];
  72. if (plugin) plugin(this);
  73. }
  74. }
  75. } catch (err) {
  76. _didIteratorError = true;
  77. _iteratorError = err;
  78. } finally {
  79. try {
  80. if (!_iteratorNormalCompletion && _iterator.return) {
  81. _iterator.return();
  82. }
  83. } finally {
  84. if (_didIteratorError) {
  85. throw _iteratorError;
  86. }
  87. }
  88. }
  89. return pluginMap;
  90. }
  91. }, {
  92. key: "parse",
  93. value: function parse() {
  94. var file = this.startNode();
  95. var program = this.startNode();
  96. this.nextToken();
  97. return this.parseTopLevel(file, program);
  98. }
  99. }]);
  100. return Parser;
  101. }(_tokenizer2.default);
  102. exports.default = Parser;