diff --git a/parse_string.js b/parse_string.js new file mode 100644 index 0000000..4464eb4 --- /dev/null +++ b/parse_string.js @@ -0,0 +1,55 @@ +/** +* Parses a string and returns a list consisting of one of +* length-1-`string` (`char`) or `symbol`, representing the +* tokens the scanner finds. +* Every time a token is successfully found, a symbol representing +* the token is inserted, otherwise the current character is +* used instead. +* @param {string} str The string to scan. +* @param {string[]} toks A list of possible tokens to find. +* @return {(string|symbol)[]} A list representing the scanner's findings. +*/ +const parse_string = function parse_string(str="", toks=null) { + if( + str.length <= 0 || + (toks ?? null) === null || + toks.length <= 0 + ) return new Array(); + + let parsed_array = new Array(); + let tok_entpts = new Map(); + for(let i = 0; i < toks.length; i++) { + const c = toks[i][0]; + if(tok_entpts.has(c)) { + const v = tok_entpts.get(c); + if(v instanceof Array) v.push( toks[i] ); + else tok_entpts.set(c, [v, toks[i]]); + } else tok_entpts.set(c, toks[i]); + } + + for(let i = 0; i < str.length; i++) { + const c = str[i]; + if(tok_entpts.has(c)) { + const v = tok_entpts.get(c); + if(typeof v === "string") { + const slc = str.slice(i, i+v.length); + const tvs = slc === v; + if(tvs) { + parsed_array.push( Symbol.for(slc) ); + i += v.length-1; + } else parsed_array.push(c); + } + if(v instanceof Array) for(const tok of v) { + const slc = str.slice(i, i+tok.length); + const tvs = slc === tok; + if(tvs) { + parsed_array.push( Symbol.for(tok) ); + i += tok.length-1; + break; + } else parsed_array.push(c); + }; + } else parsed_array.push(c); + } + + return parsed_array; +} \ No newline at end of file