-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterpolation.js
71 lines (58 loc) · 1.5 KB
/
interpolation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
import Token from './token';
import { RULE_START, RULE_END } from './separator';
import { eatPair } from '@emmetio/stream-reader-utils';
import { eatString } from './string';
const HASH = 35; // #
const AT = 64; // @
/**
* Consumes interpolation token, e.g. `#{expression}`
* @param {StreamReader} stream
* @param {Function} tokenConsumer
* @return {Token}
*/
export default function interpolation(stream, tokenConsumer) {
const start = stream.pos;
tokenConsumer = tokenConsumer || defaultTokenConsumer;
if ((stream.eat(HASH) || stream.eat(AT)) && stream.eat(RULE_START)) {
const container = new Token(stream, 'interpolation', start);
let stack = 1, token;
while (!stream.eof()) {
if (stream.eat(RULE_START)) {
stack++;
} else if (stream.eat(RULE_END)) {
stack--;
if (!stack) {
container.end = stream.pos;
return container;
}
} else if (token = tokenConsumer(stream)) {
container.add(token);
} else {
break;
}
}
}
stream.pos = start;
}
export function eatInterpolation(stream) {
const start = stream.pos;
if ((stream.eat(HASH) || stream.eat(AT)) && eatPair(stream, RULE_START, RULE_END)) {
stream.start = start;
return true;
}
stream.pos = start;
return false;
}
function defaultTokenConsumer(stream) {
const start = stream.pos;
while (!stream.eof()) {
if (stream.peek() === RULE_END) {
break;
}
eatString(stream) || stream.next();
}
if (start !== stream.pos) {
return new Token(stream, 'expression', start);
}
}