Split the library into smaller files

This commit is contained in:
2020-09-22 08:14:25 +10:00
parent 333154fd7b
commit 7f04a656c2
10 changed files with 342 additions and 323 deletions

53
source/parse-input.js Normal file
View File

@@ -0,0 +1,53 @@
//break the body down into tokens
const parseInput = (body, allowStrings, options) => {
let current = 0;
tokens = [];
while(body[current++]) {
switch(body[current - 1]) {
case '{':
case '}':
//push just this symbol
tokens.push(body.substring(current - 1, current));
break;
case '"': {
if (!allowStrings) {
throw 'Can\'t lex strings';
}
const start = current;
while (body[current++] !== '"') { //find the terminating "
if (!body[current - 1]) {
throw 'Unterminated string';
}
}
tokens.push(body.substring(start, current - 1));
break;
}
default: {
//ignore whitespace
if (/\s/.test(body[current - 1])) {
break;
}
//anything else is a multi-character token
const start = current;
while(body[current] && !/[{}"\s]/.test(body[current])) {
current++;
}
tokens.push(body.substring(start - 1, current));
break;
}
}
}
if (options.debug) {
console.log('Input:\n', tokens, '\n');
}
return tokens;
};
module.exports = parseInput;