UNPKG

2.97 kBJavaScriptView Raw
1/* @flow */
2
3"use strict";
4
5var _inherits = require("babel-runtime/helpers/inherits")["default"];
6
7var _classCallCheck = require("babel-runtime/helpers/class-call-check")["default"];
8
9var _getIterator = require("babel-runtime/core-js/get-iterator")["default"];
10
11var _interopRequireDefault = require("babel-runtime/helpers/interop-require-default")["default"];
12
13exports.__esModule = true;
14
15var _utilIdentifier = require("../util/identifier");
16
17var _options = require("../options");
18
19var _tokenizer = require("../tokenizer");
20
21var _tokenizer2 = _interopRequireDefault(_tokenizer);
22
23var plugins = {};
24
25exports.plugins = plugins;
26
27var Parser = (function (_Tokenizer) {
28 _inherits(Parser, _Tokenizer);
29
30 function Parser(options, input /*: string*/) {
31 _classCallCheck(this, Parser);
32
33 options = _options.getOptions(options);
34 _Tokenizer.call(this, options, input);
35
36 this.options = options;
37 this.inModule = this.options.sourceType === "module";
38 this.isReservedWord = _utilIdentifier.reservedWords[6];
39 this.input = input;
40 this.plugins = this.loadPlugins(this.options.plugins);
41
42 // If enabled, skip leading hashbang line.
43 if (this.state.pos === 0 && this.input[0] === "#" && this.input[1] === "!") {
44 this.skipLineComment(2);
45 }
46 }
47
48 Parser.prototype.hasPlugin = function hasPlugin(name /*: string*/) /*: boolean*/ {
49 return !!(this.plugins["*"] || this.plugins[name]);
50 };
51
52 Parser.prototype.extend = function extend(name /*: string*/, f /*: Function*/) {
53 this[name] = f(this[name]);
54 };
55
56 Parser.prototype.loadPlugins = function loadPlugins(plugins /*: Array<string>*/) {
57 var pluginMap = {};
58
59 if (plugins.indexOf("flow") >= 0) {
60 // ensure flow plugin loads last
61 plugins.splice(plugins.indexOf("flow"), 1);
62 plugins.push("flow");
63 }
64
65 for (var _iterator = plugins, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _getIterator(_iterator);;) {
66 var _ref;
67
68 if (_isArray) {
69 if (_i >= _iterator.length) break;
70 _ref = _iterator[_i++];
71 } else {
72 _i = _iterator.next();
73 if (_i.done) break;
74 _ref = _i.value;
75 }
76
77 var _name = _ref;
78
79 pluginMap[_name] = true;
80
81 var plugin = exports.plugins[_name];
82 if (plugin) plugin(this);
83 }
84
85 return pluginMap;
86 };
87
88 Parser.prototype.parse = function parse() /*: {
89 type: "File",
90 program: {
91 type: "Program",
92 body: Array<Object>
93 }
94 }*/ {
95 var file = this.startNode();
96 var program = this.startNode();
97 this.nextToken();
98 return this.parseTopLevel(file, program);
99 };
100
101 return Parser;
102})(_tokenizer2["default"]);
103
104exports["default"] = Parser;
\No newline at end of file