-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
40 lines (35 loc) · 880 Bytes
/
Copy pathindex.js
File metadata and controls
40 lines (35 loc) · 880 Bytes
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
'use strict';
const Parsimmon = require('parsimmon');
const string = Parsimmon.string;
const regex = Parsimmon.regex;
const seq = Parsimmon.seq;
const ignore = regex(/\s*/m);
const lexeme = (p) => p.skip(ignore);
const startOfLine = lexeme(regex(/\n*/));
const property = seq(
lexeme(regex(/([^:])+/i)), lexeme(string(':'))
).map((v) => v[0]);
const value = seq(
lexeme(regex(/([^;])+/i)), lexeme(regex(/(\;)?/))
).map((v) => v[0]);
const cfs = startOfLine.then(
seq(property, value).many()
).map((v) => {
return v.reduce((r, d) => {
let key = d[0];
let val = d[1];
switch (key) {
case 'z-index': val = parseInt(val); break;
default: break;
}
r[key] = val;
return r;
}, {});
});
module.exports = function(src) {
const r = cfs.parse(src);
if (!r || !r.status) {
throw new SyntaxError(r.expected);
}
return r.value;
};