(command, defaultaddress)
| 165 | const idRE = /^\s*([!=&~><]|[a-zA-Z]+)/; |
| 166 | |
| 167 | function 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 | |
| 219 | function string(text){ |
| 220 | // we use JSON strings if it is necessary to include special characters |
no test coverage detected