import { charat, from, strlen, substr, trim } from "./Utility.js";
var line = 1, column = 1, length = 0, position = 0, character = 0, characters = "";
function node(e, b, x, S, C, w, T, E) {
	return {
		value: e,
		root: b,
		parent: x,
		type: S,
		props: C,
		children: w,
		line,
		column,
		length: T,
		return: "",
		siblings: E
	};
}
function char() {
	return character;
}
function prev() {
	return character = position > 0 ? charat(characters, --position) : 0, column--, character === 10 && (column = 1, line--), character;
}
function next() {
	return character = position < length ? charat(characters, position++) : 0, column++, character === 10 && (column = 1, line++), character;
}
function peek() {
	return charat(characters, position);
}
function caret() {
	return position;
}
function slice(e, b) {
	return substr(characters, e, b);
}
function token(e) {
	switch (e) {
		case 0:
		case 9:
		case 10:
		case 13:
		case 32: return 5;
		case 33:
		case 43:
		case 44:
		case 47:
		case 62:
		case 64:
		case 126:
		case 59:
		case 123:
		case 125: return 4;
		case 58: return 3;
		case 34:
		case 39:
		case 40:
		case 91: return 2;
		case 41:
		case 93: return 1;
	}
	return 0;
}
function alloc(e) {
	return line = column = 1, length = strlen(characters = e), position = 0, [];
}
function dealloc(e) {
	return characters = "", e;
}
function delimit(e) {
	return trim(slice(position - 1, delimiter(e === 91 ? e + 2 : e === 40 ? e + 1 : e)));
}
function whitespace(e) {
	for (; (character = peek()) && character < 33;) next();
	return token(e) > 2 || token(character) > 3 ? "" : " ";
}
function escaping(e, b) {
	for (; --b && next() && !(character < 48 || character > 102 || character > 57 && character < 65 || character > 70 && character < 97););
	return slice(e, caret() + (b < 6 && peek() == 32 && next() == 32));
}
function delimiter(e) {
	for (; next();) switch (character) {
		case e: return position;
		case 34:
		case 39:
			e !== 34 && e !== 39 && delimiter(character);
			break;
		case 40:
			e === 41 && delimiter(e);
			break;
		case 92:
			next();
			break;
	}
	return position;
}
function commenter(e, x) {
	for (; next() && e + character !== 57 && !(e + character === 84 && peek() === 47););
	return "/*" + slice(x, position - 1) + "*" + from(e === 47 ? e : next());
}
function identifier(e) {
	for (; !token(peek());) next();
	return slice(e, position);
}
export { alloc, caret, char, commenter, dealloc, delimit, escaping, identifier, next, node, peek, prev, token, whitespace };
