Add Chef language implementation
This commit is contained in:
33
engines/chef/runtime/input-stream.ts
Normal file
33
engines/chef/runtime/input-stream.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* A barebones input stream implementation for consuming integers from a string.
|
||||
*/
|
||||
export default class InputStream {
|
||||
private _text: string;
|
||||
|
||||
/** Create a new input stream loaded with the given input */
|
||||
constructor(text: string) {
|
||||
this._text = text;
|
||||
}
|
||||
|
||||
/** Remove leading whitespace from the current input stream */
|
||||
private exhaustLeadingWhitespace(): void {
|
||||
const firstChar = this._text.trim()[0];
|
||||
const posn = this._text.search(firstChar);
|
||||
this._text = this._text.slice(posn);
|
||||
}
|
||||
|
||||
/** Parse input stream for an integer */
|
||||
getNumber(): number {
|
||||
this.exhaustLeadingWhitespace();
|
||||
// The extra whitespace differentiates whether string is empty or all numbers.
|
||||
if (this._text === "") throw new Error("Unexpected end of input");
|
||||
let posn = this._text.search(/[^0-9]/);
|
||||
if (posn === 0)
|
||||
throw new Error(`Unexpected input character: '${this._text[0]}'`);
|
||||
if (posn === -1) posn = this._text.length;
|
||||
// Consume and parse numeric part
|
||||
const numStr = this._text.slice(0, posn);
|
||||
this._text = this._text.slice(posn);
|
||||
return parseInt(numStr, 10);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user