-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparseKnitout.js
executable file
·239 lines (220 loc) · 7.8 KB
/
parseKnitout.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"use strict";
//parseKnitout will parse knitout, catch syntax errors, and dispatch to calls on 'machine', an abstract knitting machine:
function parseKnitout(codeText, machine, useKnitoutAsSource=false) {
var errors = [];
var warnings = [];
var carrierNames = [];
var inCommentHeader = true;
var end = false;
codeText.split("\n").forEach(function(line, lineNumber) {
if (end) return;
//console.log(lineNumber + ": " + line); //DEBUG
function addError(info) {
console.log("Parse Error on line " + lineNumber + ": " + info);
errors.push({lineNumber:lineNumber, text:info});
}
function addWarning(info) {
console.log("Parse Warning on line " + lineNumber + ": " + info);
warnings.push({lineNumber:lineNumber, text:info});
}
//magic first line:
if (lineNumber == 0) {
//knitout must begin with ';!knitout-N'.
var m = line.match(/^;!knitout-(\d+)$/);
if (m !== null) {
if (parseInt(m[1]) != 2) {
addWarning("Parsed version (" + m.groups(1) + ") is not what was expected.");
}
} else {
addError("Knitout should always start with a ';!knitout-2' line.");
}
//nothing more to do with the first line.
return;
}
//comment header lines:
var m = line.match(/^;;([^:]+): (.*)$/);
if (m !== null) {
if (!inCommentHeader) {
addWarning("Comment-header-like line outside comment header.");
} else {
var name = m[1];
var value = m[2];
//console.log("Comment header: '" + name + "' is '" + value + "'.");
if (name === "Carriers") {
carrierNames = value.split(" ");
machine.setCarriers(carrierNames);
//console.log("Carrier names (front-to-back): ", carrierNames);
}
//TODO: handle other comment headers.
return; //nothing left to do with this line.
}
} else {
inCommentHeader = false;
}
//split line into op and comment parts:
var m = line.match(/^([^;]*)(;.*)?$/);
if (m === null) {
console.log("Weird, our line regex should have gotten everything.");
return;
}
var tokens = m[1].split(/[ \t]+/);
var comment = m[2];
//handle !source: directive in comment
if (comment && comment.startsWith(';!source:')) {
if ('source' in machine) {
machine.source(comment.substr(9));
}
} else if (useKnitoutAsSource){
machine.source(lineNumber);
}
//trim leading/trailing whitespace from operation token list:
if (tokens.length !== 0 && tokens[0] === "") tokens.shift();
if (tokens.length !== 0 && tokens[tokens.length-1] === "") tokens.pop();
if (tokens.length === 0) {
//empty operation: nothing to do
return;
}
var op = tokens.shift();
function parseCarrierSet(tokens) {
//check that tokens are in carrierNames and aren't repeated:
var usedAlready = {};
tokens.forEach(function(name){
if (carrierNames.indexOf(name) == -1) {
throw "Carrier name does not appear in Carriers header.";
}
if (name in usedAlready) {
throw "Carrier name appears twice in carrier set.";
}
usedAlready[name] = true;
});
return tokens.slice();
}
function parseStitchValue(token) {
if (!/^[-+]?(\.\d+|\d+.\d*|\d+)$/.test(token)) {
throw "Stitch value [" + token + "] must be a simple floating point value.";
}
return parseFloat(token);
}
function parseRackingValue(token) {
if (!/^[-+]?(\.\d+|\d+.\d*|\d+)$/.test(token)) {
throw "Racking value [" + token + "] must be a simple floating point value.";
}
return parseFloat(token);
}
function parseDirection(token) {
if (!(token === '-' || token === '+')) {
throw "Direction [" + token + "] must be '+' or '-'.";
}
return token;
}
function parseNeedle(token) {
if (!/^([fb]s?)([-+]?\d+)$/.test(token)) throw "Needle [" + token + "] must be f,b,fs, or bs followed by an integer.";
return token;
}
//dispatch all basic knitout functions to the machine, catch anything thrown and add to errors:
try {
//all the in/out/hook ops take a carrierset as an argument:
if (["in", "out", "inhook", "releasehook", "outhook"].indexOf(op) !== -1) {
var cs = parseCarrierSet(tokens);
machine[op](cs);
} else if (op === "stitch") {
if (tokens.length !== 2) throw "stitch takes exactly two arguments";
var l = parseStitchValue(tokens[0]);
var t = parseStitchValue(tokens[1]);
machine.stitch(l, t);
} else if (op === "rack") {
if (tokens.length !== 1) throw "rack takes exactly one argument";
var r = parseRackingValue(tokens[0]);
machine.rack(r);
} else if (op === "knit" || op === "drop") {
if (op === "drop") {
if (tokens.length !== 1) throw "drop takes exactly one argument";
//interpret drop as "knit + N":
tokens.unshift("+");
}
if (tokens.length < 2) throw "knit requires at least two arguments";
var d = parseDirection(tokens.shift());
var n = parseNeedle(tokens.shift());
var cs = parseCarrierSet(tokens);
machine.knit(d, n, cs);
} else if (op === "tuck" || op === "amiss") {
if (op === "amiss") {
if (tokens.length !== 1) throw "amiss takes exactly one argument";
tokens.unshift("+");
}
if (tokens.length < 2) throw "tuck requires at least two arguments";
var d = parseDirection(tokens.shift());
var n = parseNeedle(tokens.shift());
var cs = parseCarrierSet(tokens);
machine.tuck(d, n, cs);
} else if (op === "split" || op === "xfer") {
if (op === "xfer") {
if (tokens.length !== 2) throw "xfer takes exactly two arguments";
tokens.unshift("+");
}
if (tokens.length < 3) throw "split requires at least three arguments";
var d = parseDirection(tokens.shift());
var n = parseNeedle(tokens.shift());
var n2 = parseNeedle(tokens.shift());
var cs = parseCarrierSet(tokens);
machine.split(d, n, n2, cs);
} else if (op === "miss") {
if (tokens.length < 2) throw "miss requires at least two arguments";
var d = parseDirection(tokens.shift());
var n = parseNeedle(tokens.shift());
var cs = parseCarrierSet(tokens);
machine.miss(d, n, cs);
} else if (op === "pause") {
if (tokens.length !== 0) throw "pause takes no arguments";
machine.pause();
} else if (op === "x-end") {
end = true;
} else if (op === "x-yarn-in") {
//yarn carrier stack appears just before a given location
if (tokens.length < 3) throw "x-yarn-in requires at least three arguments";
var d = parseDirection(tokens.shift());
var n = parseNeedle(tokens.shift());
var cs = parseCarrierSet(tokens);
machine.yarnInEdge(d, n, cs);
} else if (op === "x-yarn-out") {
//yarn carrier stack vanishes just after a given location
if (tokens.length < 3) throw "x-yarn-out requires at least three arguments";
var d = parseDirection(tokens.shift());
var n = parseNeedle(tokens.shift());
var cs = parseCarrierSet(tokens);
machine.yarnOutEdge(d, n, cs);
} else if (op === "x-loop-in") {
//yarn loop appears at a given location, as if made by given carriers (but not connected)
if (tokens.length < 3) throw "x-loop-in requires at least three arguments";
var d = parseDirection(tokens.shift());
var n = parseNeedle(tokens.shift());
var cs = parseCarrierSet(tokens);
machine.loopInEdge(d, n, cs);
} else if (op === "x-loop-out") {
//yarn loop vanishes at a given location
if (tokens.length !== 1) throw "x-loop-out requires one argument";
var n = parseNeedle(tokens.shift());
machine.loopOutEdge(n);
} else if (op.startsWith("x-")) {
if (op in machine) {
machine[op](...tokens);
} else {
addWarning("Unrecognized extension operation '" + op + "'.");
}
} else {
addError("Unrecognized operation.");
}
} catch (e) {
if (typeof(e) === "string") {
addError(e);
} else {
addError("[error that wasn't a string]");
throw e;
//console.log(e); //DEBUG
}
}
});
}
if (typeof(module) !== "undefined") {
module.exports.parseKnitout = parseKnitout;
}