-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparse.mjs
171 lines (162 loc) · 3.96 KB
/
parse.mjs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { isString } from './lib.mjs'
/**
*
* @param {string} str
*/
function isFunctionFlag (str) {
return str.includes('(')
}
function parseFlag(flagPart) {
const ret = {};
if (isString(flagPart)) {
flagPart.split(' ').forEach((flagItem) => {
const key = flagItem.trim();
if (key) {
if (isFunctionFlag(key)) {
const preStr = key.replace(/[(),]/g, ' ')
const [flagName, ...param] = preStr.split(' ')
if (!Array.isArray(ret[flagName])) {
ret[flagName] = []
}
ret[flagName].push(param)
} else {
ret[key] = true;
}
}
});
}
return ret;
}
function isConstraint(line) {
return line.startsWith('constraint');
}
function isRepeat(line) {
return line.startsWith('repeat');
}
function isRepeatGroup(line) {
return line.startsWith('repeat group');
}
function isGroupEnd(line) {
return line.startsWith('end group');
}
const repeatGroupStartLength = 'repeat group '.length;
function parse(input) {
const ret = [];
/**
* constraint n int lower higher | flags
* constraint n set values | flags
* constraint n graph 1 nodeNumber edgeNumber | flags
* @todo constraint n float lower higher length
*
* flags shuffle directed
*
* key is name
* {
* type: 'int',
* lower: string,
* higher: string
* }
*/
const constraint = {};
function addIntConstraint(name, [lower, higher], flag) {
constraint[name] = {
lower: lower,
higher: higher,
type: 'int',
flag
};
}
function addSetConstraint(name, list, flag) {
constraint[name] = {
list: list,
type: 'set',
flag
};
}
function addGraphConstraint(name, [graphNum, nodeNum, edgeNum], flag) {
constraint[name] = {
graphNum,
nodeNum,
edgeNum,
type: 'graph',
flag
};
}
function addAliasConstraint(name, aliasName) {
constraint[name] = {
aliasName,
type: 'alias',
};
}
const list = input.split('\n');
let index = 0;
const end = list.length;
while (index !== end) {
const line = list[index];
while (true) {
if (isConstraint(line)) {
const [definePart, flagsPart] = line.split('|');
const [_, name, type, ...other] = definePart.split(' ');
const flag = parseFlag(flagsPart);
if (type === 'int') {
addIntConstraint(name, other, flag);
break;
}
if (type === 'set') {
addSetConstraint(name, other, flag);
break;
}
if (type === 'graph') {
addGraphConstraint(name, other, flag);
break;
}
if (type === 'alias') {
addAliasConstraint(name, other[0]);
break;
}
break;
}
if (isRepeatGroup(line)) {
const repeat = line.substr(repeatGroupStartLength);
const children = [];
index += 1;
while (!isGroupEnd(list[index])) {
const repeatLine = list[index];
const repeatStart = 'repeat '.length;
const repeatEnd = repeatLine.indexOf(' ', repeatStart);
const repeat = repeatLine.substr(repeatStart, repeatEnd - repeatStart);
children.push({
template: repeatLine.substr(repeatEnd + 1),
repeat: repeat,
type: 'line'
});
index += 1;
}
ret.push(
{
type: 'group',
repeat: repeat,
children: children
}
);
break;
}
if (isRepeat(line)) {
// repeat n CONTENT
const repeatStart = 'repeat '.length;
const repeatEnd = line.indexOf(' ', repeatStart);
const repeat = line.substr(repeatStart, repeatEnd - repeatStart);
ret.push({
template: line.substr(repeatEnd + 1),
repeat: repeat,
type: 'line',
});
break;
}
break;
}
index += 1;
}
return [ret, constraint];
}
export { parse }