import { assert } from "../../assert.js";
import DataType from "./DataType.js";
import { readIdentifierToken } from "./readIdentifierToken.js";
import Token from "./Token.js";
import TokenType from "./TokenType.js";


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

    let i = cursor;

    const identifiers = [];

    let identifier;

    identifier = readIdentifierToken(text, i, length);

    identifiers.push(identifier);

    i = identifier.end;

    for (; ;) {

        const firstChar = text.charAt(i);

        if (firstChar !== '.') {
            break;
        }

        //skip over the separator
        i++;

        identifier = readIdentifierToken(text, i, length);

        identifiers.push(identifier);

        i = identifier.end;
    }

    return new Token(identifiers, cursor, i, TokenType.Reference, DataType.String);
}


