-
Notifications
You must be signed in to change notification settings - Fork 28
/
template-parser-helper.js
52 lines (45 loc) · 1.02 KB
/
template-parser-helper.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
"use strict";
const Literal = require("./literal");
const isLiteral = token => token[0] === "word" && /^\$\{[\s\S]*\}$/.test(token[1]);
function literal (start) {
if (!isLiteral(start)) {
return;
}
const tokens = [];
let hasWord;
let type;
let token;
while ((token = this.tokenizer.nextToken())) {
tokens.push(token);
type = token[0];
if (type.length === 1) {
break;
} else if (type === "word") {
hasWord = true;
}
}
while (tokens.length) {
this.tokenizer.back(tokens.pop());
}
if (type === "{" || (type === ":" && !hasWord)) {
return;
}
const node = new Literal({
text: start[1],
});
this.init(node, start[2], start[3]);
return node;
}
function freeSemicolon (token) {
this.spaces += token[1];
const nodes = this.current.nodes;
const prev = nodes && nodes[nodes.length - 1];
if (prev && /^(rule|literal)$/.test(prev.type) && !prev.raws.ownSemicolon) {
prev.raws.ownSemicolon = this.spaces;
this.spaces = "";
}
}
module.exports = {
freeSemicolon: freeSemicolon,
literal: literal,
};