1const isStickySupported = typeof /./g.sticky === 'boolean';
2
3export const _pattern = (input) => {
4 if (typeof input === 'function') return input;
5
6 const source = typeof input !== 'string' ? input.source : input;
7 return isStickySupported
8 ? new RegExp(source, 'y')
9 : new RegExp(`^(?:${source})`, 'g');
10};
11
12export const _substr = (state, pattern) => {
13 const end = state.index + pattern.length;
14 const sub = state.input.slice(state.index, end);
15 if (sub === pattern) {
16 state.index = end;
17 return sub;
18 }
19};
20
21export const _exec = (state, pattern) => {
22 if (typeof pattern === 'function') return pattern();
23
24 let match;
25 if (isStickySupported) {
26 pattern.lastIndex = state.index;
27 match = pattern.exec(state.input);
28 state.index = pattern.lastIndex;
29 } else {
30 pattern.lastIndex = 0;
31 match = pattern.exec(state.input.slice(state.input));
32 state.index += pattern.lastIndex;
33 }
34
35 return match && match[0];
36};
37
38export const tag = (array, tag) => {
39 array.tag = tag;
40 return array;
41};
42
43export const parse = (pattern) => (input) => {
44 const state = { input, index: 0 };
45 return pattern(state);
46};
47
48export const match = (_name) => {
49 throw new TypeError(
50 'This match() function was not transformed. ' +
51 'Ensure that the Babel plugin is set up correctly and try again.'
52 );
53};