1 | import State from "../tokenizer/state";
|
2 | import {charCodes} from "../util/charcodes";
|
3 |
|
4 | export let isJSXEnabled;
|
5 | export let isTypeScriptEnabled;
|
6 | export let isFlowEnabled;
|
7 | export let state;
|
8 | export let input;
|
9 | export let nextContextId;
|
10 |
|
11 | export function getNextContextId() {
|
12 | return nextContextId++;
|
13 | }
|
14 |
|
15 |
|
16 | export 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 |
|
25 | export class Loc {
|
26 |
|
27 |
|
28 | constructor(line, column) {
|
29 | this.line = line;
|
30 | this.column = column;
|
31 | }
|
32 | }
|
33 |
|
34 | export 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 |
|
48 | export 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 | }
|