import { assert } from "../../assert.js";
import ParserError from "./ParserError.js";
import { readArrayLiteral } from "./readArrayLiteral.js";
import { readBooleanToken } from "./readBooleanToken.js";
import { readNumberToken } from "./readNumberToken.js";
import { readStringToken } from "./readStringToken.js";

/**
 *
 * @param {string} text
 * @param {number} cursor
 * @param {number} length
 * @returns {Token}
 */
function readLiteralToken(text, cursor, length) {
    assert.isString(text, 'text');
    assert.isNonNegativeInteger(cursor, 'cursor');
    assert.isNonNegativeInteger(length, 'length');

    const firstChar = text.charAt(cursor);

    switch (firstChar) {
        case 't':
        case 'f':
            return readBooleanToken(text, cursor, length);
        case '\"':
        case "\'":
            return readStringToken(text, cursor, length);
        case '-':
        case '+':
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            return readNumberToken(text, cursor, length);
        case '[':
            return readArrayLiteral(text, cursor, length);
        default:
            throw new ParserError(cursor, "Expected literal start, but found '" + firstChar + "'", text);
    }
}

export { readLiteralToken };
