@airtasker/form-schema-compiler
Version:
a form schema compiler
64 lines (51 loc) • 1.57 kB
JavaScript
;
exports.__esModule = true;
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
/**
* transpiling string input to a stream
* @param input:string
* @returns {{next: (function()), peek: (function(): string), eof: (function(): boolean), croak: (function(*)), position: (function(): {pos: number, line: number, col: number})}}
*/
var createInputStream = function createInputStream(input) {
if (typeof input !== "string") {
throw new Error("expression must be a string instead of " + _typeof(input));
}
var pos = 0;
var line = 1;
var col = 0;
var next = function next() {
var ch = input.charAt(pos);
pos += 1;
if (ch === "\n") {
line += 1;
col = 0;
} else {
col += 1;
}
return ch;
};
var peek = function peek() {
return input.charAt(pos);
};
var eof = function eof() {
return peek() === "";
};
var croak = function croak(msg) {
throw new Error("".concat(msg, " (").concat(line, ":").concat(col, ")"));
};
var position = function position() {
return {
pos: pos,
line: line,
col: col
};
};
return {
next: next,
peek: peek,
eof: eof,
croak: croak,
position: position
};
};
exports["default"] = createInputStream;