Last active
January 16, 2018 15:22
-
-
Save heypoom/5ceed2b751f3f662ac3bbf3887c62b18 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function* CharStream(text) { | |
const lines = text.split('\n') | |
for (const row in lines) { | |
const chars = lines[row].split('') | |
for (const col in chars) { | |
yield {token: chars[col], row, col} | |
} | |
} | |
} | |
function CharStream(text) { | |
const lines = text.split('\n') | |
return lines.map((line, row) => { | |
return line.split('').map((token, col) => ({token, row, col})) | |
}) | |
} | |
function InputStream(input) { | |
let pos = 0 | |
let line = 1 | |
let col = 0 | |
return { | |
next: () => { | |
const ch = input.charAt(pos++) | |
if (ch == "\n") { | |
line++ | |
col = 0 | |
} else { | |
col++ | |
} | |
return ch | |
}, | |
peek: () => input.charAt(pos), | |
eof: () => peek() == "", | |
croak: () => { | |
throw new Error(`${msg} (${line}:${col})`) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment