Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
CalinZBaenen committed Dec 11, 2021
0 parents commit 2c45829
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions parse_string.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 2c45829

Please sign in to comment.