Skip to content

Instantly share code, notes, and snippets.

@heypoom
Last active January 16, 2018 15:22
Show Gist options
  • Save heypoom/5ceed2b751f3f662ac3bbf3887c62b18 to your computer and use it in GitHub Desktop.
Save heypoom/5ceed2b751f3f662ac3bbf3887c62b18 to your computer and use it in GitHub Desktop.
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