tangled
alpha
login
or
join now
kitten.sh
/
reghex
0
fork
atom
Mirror: The magical sticky regex-based parser generator 🧙
0
fork
atom
overview
issues
pulls
pipelines
Add predicate support to interpolations
kitten.sh
5 years ago
8b12428d
2f2cc66a
+19
-11
2 changed files
expand all
collapse all
unified
split
src
core.js
core.test.js
+7
-4
src/core.js
···
33
33
return match;
34
34
};
35
35
36
36
-
export const interpolation = (state) => {
36
36
+
export const interpolation = (predicate) => (state) => {
37
37
let match;
38
38
39
39
-
const input = state.quasis[state.x];
40
40
-
if (!input || state.y >= input.length) {
39
39
+
if (
40
40
+
state.y >= state.quasis[state.x].length &&
41
41
+
state.x < state.expressions.length
42
42
+
) {
41
43
state.y = 0;
42
42
-
match = state.expressions[state.x++] || match;
44
44
+
match = state.expressions[state.x++];
45
45
+
if (predicate && match) match = predicate(match);
43
46
}
44
47
45
48
return match;
+12
-7
src/core.test.js
···
556
556
describe('interpolation parsing', () => {
557
557
const node = match('node')`
558
558
${/1/}
559
559
-
${interpolation}
559
559
+
${interpolation((x) => x > 1 && x)}
560
560
${/3/}
561
561
`;
562
562
563
563
-
const expected = ['1', 2, '3'];
564
564
-
expected.tag = 'node';
563
563
+
it('matches interpolations', () => {
564
564
+
const expected = ['1', 2, '3'];
565
565
+
expected.tag = 'node';
566
566
+
expect(parse(node)`1${2}3`).toEqual(expected);
567
567
+
});
565
568
566
566
-
expect(parse(node)`1${2}3`).toEqual(expected);
567
567
-
expect(parse(node)`13`).toBe(undefined);
568
568
-
expect(parse(node)`13${2}`).toBe(undefined);
569
569
-
expect(parse(node)`${2}13`).toBe(undefined);
569
569
+
it('does not match invalid inputs', () => {
570
570
+
expect(parse(node)`13`).toBe(undefined);
571
571
+
expect(parse(node)`13${2}`).toBe(undefined);
572
572
+
expect(parse(node)`${2}13`).toBe(undefined);
573
573
+
expect(parse(node)`1${1}3`).toBe(undefined);
574
574
+
});
570
575
});