UNPKG

2.49 kBJavaScriptView Raw
1/**
2 * Copyright 2014 Shape Security, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17"use strict";
18
19var _parser = require("./parser");
20
21var _tokenizer = require("./tokenizer");
22
23var _earlyErrors = require("./early-errors");
24
25function 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
38function 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 // for now, just throw the first error; we will handle multiple errors later
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
76var parseModule = generateInterface("parseModule");
77exports.parseModule = parseModule;
78var parseScript = generateInterface("parseScript");
79exports.parseScript = parseScript;
80exports["default"] = parseScript;
81exports.EarlyErrorChecker = _earlyErrors.EarlyErrorChecker;
\No newline at end of file