UNPKG

1.21 kBJavaScriptView Raw
1import State from "../tokenizer/state";
2import {charCodes} from "../util/charcodes";
3
4export let isJSXEnabled;
5export let isTypeScriptEnabled;
6export let isFlowEnabled;
7export let state;
8export let input;
9export let nextContextId;
10
11export function getNextContextId() {
12 return nextContextId++;
13}
14
15// tslint:disable-next-line no-any
16export function augmentError(error) {
17 if ("pos" in error) {
18 const loc = locationForIndex(error.pos);
19 error.message += ` (${loc.line}:${loc.column})`;
20 error.loc = loc;
21 }
22 return error;
23}
24
25export class Loc {
26
27
28 constructor(line, column) {
29 this.line = line;
30 this.column = column;
31 }
32}
33
34export function locationForIndex(pos) {
35 let line = 1;
36 let column = 1;
37 for (let i = 0; i < pos; i++) {
38 if (input.charCodeAt(i) === charCodes.lineFeed) {
39 line++;
40 column = 1;
41 } else {
42 column++;
43 }
44 }
45 return new Loc(line, column);
46}
47
48export function initParser(
49 inputCode,
50 isJSXEnabledArg,
51 isTypeScriptEnabledArg,
52 isFlowEnabledArg,
53) {
54 input = inputCode;
55 state = new State();
56 nextContextId = 1;
57 isJSXEnabled = isJSXEnabledArg;
58 isTypeScriptEnabled = isTypeScriptEnabledArg;
59 isFlowEnabled = isFlowEnabledArg;
60}