MCPcopy Create free account
hub / github.com/dwachss/bililiteRange / parseCommand

Function parseCommand

bililiteRange.ex.js:167–217  ·  view source on GitHub ↗
(command, defaultaddress)

Source from the content-addressed store, hash-verified

165const idRE = /^\s*([!=&~><]|[a-zA-Z]+)/;
166
167function parseCommand(command, defaultaddress){
168 return {
169 addresses: parseAddresses(),
170 command: commandCompletion(parseCommandWord()),
171 variant: parseVariant(),
172 parameter: parseParameter()
173 };
174
175 function parseAddresses(){
176 var addresses = [defaultaddress];
177 // basic addresses
178 command = command.replace(addressRE, function(match, c){
179 addresses = [c];
180 return '';
181 });
182 // relative addresses
183 command = command.replace(/^\s*[-+\d]+/, function (match){
184 addresses[0] += match;
185 return '';
186 });
187 // a comma separates addresses
188 if (/^\s*[,;]\s*/.test(command)){
189 if (/^\s*;/.test(command)) addresses.push(';'); // need to track semicolons since they change the value of '.'
190 command = command.replace(/^\s*[,;]\s*/, '');
191 addresses.push.apply(addresses, parseAddresses()); // recursively parse the whole list
192 }
193 return addresses;
194 }
195
196 function parseCommandWord(){
197 if (/^\s*$/.test(command)) return 'print'; // blank line just goes to the addressed line, which is what we do with print
198 var ret;
199 command = command.replace(idRE, function (match, c){
200 ret = c;
201 return '';
202 });
203 if (!ret) throw new Error("No command string");
204 return ret;
205 }
206 function parseVariant(){
207 var variant = false;
208 command = command.replace(/^\s*!/, function (){
209 variant = true;
210 return '';
211 });
212 return variant;
213 }
214 function parseParameter(){
215 return (string(command));
216 }
217}
218
219function string(text){
220 // we use JSON strings if it is necessary to include special characters

Callers 1

Calls 5

parseAddressesFunction · 0.85
commandCompletionFunction · 0.85
parseCommandWordFunction · 0.85
parseVariantFunction · 0.85
parseParameterFunction · 0.85

Tested by

no test coverage detected