/** * string parser will parse given individual tokens with c style string parser in double quotes. * @param text input */ export function *stringParser( text: string, tokens = /([^0-9a-z\-\_\$\@])/gi, stringRegex = /(\"((\"\")|(\\\")|[^\"])+\")/gi, ) { tokens.lastIndex = 0; for(;;) { const { lastIndex } = tokens; const g = tokens.exec(text); if (!g?.length) { if (text.length > lastIndex) { yield text.substring(lastIndex); } return; } if (tokens.lastIndex > lastIndex + 1) { yield text.substring(lastIndex, tokens.lastIndex - 1); } const token = g[1]; switch(token) { case '\"': // start reading string... stringRegex.lastIndex = lastIndex; const sg = stringRegex.exec(text); if (!sg?.length) { throw new Error("Incomplete string found"); } yield sg[1]; tokens.lastIndex = stringRegex.lastIndex; continue; default: yield token; continue; } } }