Last active
November 28, 2020 17:38
-
-
Save exbotanical/8fd013cb3fe0f9098772103fde06c3f1 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
// curried generator | |
const renderStringInterval = value => function*(num) { | |
yield setInterval(() => console.log(value), num); | |
}; | |
renderStringInterval('test')(3000).next(); |
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* id(idx) { | |
while (true) { | |
yield idx++; | |
} | |
} | |
class Base { | |
constructor() { | |
this.generator = id(); | |
} | |
} | |
class Parser extends Base { | |
constructor(input) { | |
super(input); | |
this.data = input; | |
this.current = null; | |
this.alignment = { | |
position: 0, | |
column: 1, | |
line: 0 | |
} | |
} | |
next() { | |
this.current = this.data.charAt(this.alignment.position++); | |
if (this.current === "\n") { | |
this.alignment.column = 1; | |
this.alignment.line = 0; | |
} else { | |
this.alignment.column++; | |
} | |
return this; | |
} | |
peek() { | |
this.log(this.data.charAt(this.alignment.position)); | |
return this; | |
} | |
log (arg) { | |
const charIs = arg || this.current; | |
console.table({ charIs }); | |
return this; | |
} | |
} | |
/* incomplete */ | |
const d = "a dog jumped over a log with a frog and a fox"; | |
const p = new Parser(d); | |
p.next().log().next().log().next().log().peek().log(); |
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
class Parser { | |
constructor(input) { | |
this.position = 0; | |
this.column = 0; | |
this.line = 1; | |
this.input = input; | |
this.current = ''; | |
} | |
// return next value, discard from stream | |
// advance to next extant (non-newline) character | |
next = () => { | |
this.current = this.input.charAt(this.position++); | |
// newline encountered, increment line count and reset column counter | |
if (this.current == '\n') { | |
this.line++; | |
this.column = 0; | |
} | |
// no newline, proceed to next char | |
else { | |
this.column++; | |
} | |
return this.current; | |
}; | |
// return next value, does not disrupt stream | |
// artificially advance to next character | |
peek = () => this.input.charAt(this.position); | |
// returns true if and only if stream end | |
eof = () => this.peek() === ''; | |
// forcibly terminate, throwing an exception w/reference to source thereof | |
term = (reason, cause) => { | |
throw new Error(`${reason} ${this.line}:${this.column} ${cause ? `(${cause})` : '' }`); | |
}; | |
// return previous value, discard from stream | |
// advance to previous extant (non-newline) character | |
rewind = () => { | |
this.current = this.input.charAt(--this.position); | |
if (this.current == '\n') --this.line; | |
return this.current; | |
}; | |
// return previous value, does not disrupt stream | |
// artificially recede to preceding character | |
past = () => this.input.charAt(this.position - 1); | |
}; | |
module.exports = Parser; | |
const parser = new Parser('Nevermind'); | |
let i = 0; | |
while (i < parser.input.length + 10) { | |
if (parser.eof()) { | |
return; | |
} | |
console.log(parser.next()); | |
i++; | |
}; |
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
class Parser { | |
constructor(input) { | |
this.data = input; | |
this.current = null; | |
this.alignment = { | |
position: 0, | |
column: 1, | |
line: 0 | |
}; | |
} | |
next() { | |
this.current = this.data.charAt(this.alignment.position++); | |
if (this.current === "\n") { | |
this.alignment.column = 1; | |
this.alignment.line++; | |
} | |
else { | |
this.alignment.column++; | |
} | |
return this.current; | |
} | |
peek() { | |
return this.data.charAt(this.alignment.position++); | |
} | |
} | |
const parseSlowly = instance => input => { | |
const parser = new instance(input); | |
return (function* () { | |
do { | |
var nextValue = parser.next(); | |
yield nextValue; | |
} while(nextValue.done); | |
})(); | |
} | |
const m = parseSlowly(Parser)("another patently absurd parser"); | |
const a = slow => interval => new Promise((resolve, reject) => setTimeout(() => resolve(slow.next().value), interval)); | |
a(m)(2000).then(console.log).catch(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment