-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
102 lines (95 loc) · 2.55 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"use strict";
var assert = require('assert');
function parse (src) {
// check if the runtime is node or browser
if (typeof Buffer === 'function' &&
Buffer.isBuffer(src)) {
src += '';
}
var pos = 0; // parsing cursor
var token = ''; // record token
var obj = newObj(); // the object to be returned
var ctx = obj; // context object
var isParsingArgs = false; // flag if the program is parsing arguments
var savedToken = null; // when parsing arguments, we need to record last token here
var savedArgs = null; // when having arguments parsing done, we need to save parsed
// arguments and use it in next `{`
do {
var ch = src[pos];
// check if the program is parsing arguments ()
if (isParsingArgs === true) {
// enter parsing arguments mode on `(`
if (ch === ')') {
// parse the arguments string
savedArgs = token.split(',').reduce(function (val, item) {
var obj = item.split('=');
// convert the string to number
var oval = Number(obj[1]);
if (isNaN(oval)) {
oval = obj[1];
}
if (oval === 'true') {
oval = true;
} else if (oval === 'false') {
oval = false;
}
val[obj[0].trim()] = oval;
return val;
}, {});
isParsingArgs = false;
token = savedToken;
} else {
token += ch;
}
continue;
}
assert.equal(isParsingArgs, false);
if (!/(\s|{|}|,|\()/.test(ch)) {
token += ch;
} else {
if (!obj.type) {
obj.type = token;
token = '';
} else if (ch === ',') {
if (token) {
ctx.fields.push(token);
token = '';
}
} else if (ch === '{') {
var o = newObj();
o.parent = ctx;
if (savedArgs) {
o.args = savedArgs;
savedArgs = null;
}
if (!token) {
ctx = ctx.root = o;
} else {
ctx = ctx.methods[token] = o;
token = '';
}
} else if (ch === '}') {
if (token) {
ctx.fields.push(token);
}
ctx = ctx.parent;
token = '';
} else if (ch === '(') {
assert.equal(isParsingArgs, false);
isParsingArgs = true;
savedToken = token;
token = '';
}
}
} while (src[pos++]);
// return objects
return obj;
}
function newObj () {
return {
type: false,
fields: [],
methods: {}
};
}
exports.parse = parse;