1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | "use strict";
|
18 |
|
19 | var _parser = require("./parser");
|
20 |
|
21 | var _tokenizer = require("./tokenizer");
|
22 |
|
23 | var _earlyErrors = require("./early-errors");
|
24 |
|
25 | function markLocation(node, location) {
|
26 | node.loc = {
|
27 | start: location,
|
28 | end: {
|
29 | line: this.lastLine + 1,
|
30 | column: this.lastIndex - this.lastLineStart,
|
31 | offset: this.lastIndex
|
32 | },
|
33 | source: null
|
34 | };
|
35 | return node;
|
36 | }
|
37 |
|
38 | function generateInterface(parsingFunctionName) {
|
39 | return function parse(code) {
|
40 | var _ref = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
41 |
|
42 | var _ref$loc = _ref.loc;
|
43 | var loc = _ref$loc === undefined ? false : _ref$loc;
|
44 | var _ref$earlyErrors = _ref.earlyErrors;
|
45 | var earlyErrors = _ref$earlyErrors === undefined ? true : _ref$earlyErrors;
|
46 |
|
47 | var parser = new _parser.Parser(code);
|
48 | if (loc) {
|
49 | parser.markLocation = markLocation;
|
50 | }
|
51 | var ast = parser[parsingFunctionName]();
|
52 | if (earlyErrors) {
|
53 | var errors = _earlyErrors.EarlyErrorChecker.check(ast);
|
54 |
|
55 | if (errors.length > 0) {
|
56 | var _errors$0 = errors[0];
|
57 | var node = _errors$0.node;
|
58 | var message = _errors$0.message;
|
59 |
|
60 | var offset = 0,
|
61 | line = 1,
|
62 | column = 0;
|
63 | if (node.loc != null) {
|
64 | var _node$loc$start = node.loc.start;
|
65 | offset = _node$loc$start.offset;
|
66 | line = _node$loc$start.line;
|
67 | column = _node$loc$start.column;
|
68 | }
|
69 | throw new _tokenizer.JsError(offset, line, column, message);
|
70 | }
|
71 | }
|
72 | return ast;
|
73 | };
|
74 | }
|
75 |
|
76 | var parseModule = generateInterface("parseModule");
|
77 | exports.parseModule = parseModule;
|
78 | var parseScript = generateInterface("parseScript");
|
79 | exports.parseScript = parseScript;
|
80 | exports["default"] = parseScript;
|
81 | exports.EarlyErrorChecker = _earlyErrors.EarlyErrorChecker; |
\ | No newline at end of file |