-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2c45829
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |