'use strict';

require('../../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/parse/parser/parser.js');
var lexer_public = require('../../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/scan/lexer_public.js');
require('../../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/scan/tokens_public.js');
require('../../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/parse/grammar/lookahead.js');
require('../../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/parse/errors_public.js');
require('../../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/parse/exceptions_public.js');
var lexer_errors_public = require('../../../../../chevrotain@11.0.3/node_modules/chevrotain/lib/src/scan/lexer_errors_public.js');

/******************************************************************************
 * Copyright 2022 TypeFox GmbH
 * This program and the accompanying materials are made available under the
 * terms of the MIT License, which is available in the project root.
 ******************************************************************************/
class DefaultLexerErrorMessageProvider {
    buildUnexpectedCharactersMessage(fullText, startOffset, length, line, column) {
        return lexer_errors_public.defaultLexerErrorProvider.buildUnexpectedCharactersMessage(fullText, startOffset, length, line, column);
    }
    buildUnableToPopLexerModeMessage(token) {
        return lexer_errors_public.defaultLexerErrorProvider.buildUnableToPopLexerModeMessage(token);
    }
}
const DEFAULT_TOKENIZE_OPTIONS = { mode: 'full' };
class DefaultLexer {
    constructor(services) {
        this.errorMessageProvider = services.parser.LexerErrorMessageProvider;
        this.tokenBuilder = services.parser.TokenBuilder;
        const tokens = this.tokenBuilder.buildTokens(services.Grammar, {
            caseInsensitive: services.LanguageMetaData.caseInsensitive
        });
        this.tokenTypes = this.toTokenTypeDictionary(tokens);
        const lexerTokens = isTokenTypeDictionary(tokens) ? Object.values(tokens) : tokens;
        const production = services.LanguageMetaData.mode === 'production';
        this.chevrotainLexer = new lexer_public.Lexer(lexerTokens, {
            positionTracking: 'full',
            skipValidations: production,
            errorMessageProvider: this.errorMessageProvider
        });
    }
    get definition() {
        return this.tokenTypes;
    }
    tokenize(text, _options = DEFAULT_TOKENIZE_OPTIONS) {
        var _a, _b, _c;
        const chevrotainResult = this.chevrotainLexer.tokenize(text);
        return {
            tokens: chevrotainResult.tokens,
            errors: chevrotainResult.errors,
            hidden: (_a = chevrotainResult.groups.hidden) !== null && _a !== void 0 ? _a : [],
            report: (_c = (_b = this.tokenBuilder).flushLexingReport) === null || _c === void 0 ? void 0 : _c.call(_b, text)
        };
    }
    toTokenTypeDictionary(buildTokens) {
        if (isTokenTypeDictionary(buildTokens))
            return buildTokens;
        const tokens = isIMultiModeLexerDefinition(buildTokens) ? Object.values(buildTokens.modes).flat() : buildTokens;
        const res = {};
        tokens.forEach(token => res[token.name] = token);
        return res;
    }
}
/**
 * Returns a check whether the given TokenVocabulary is TokenType array
 */
function isTokenTypeArray(tokenVocabulary) {
    return Array.isArray(tokenVocabulary) && (tokenVocabulary.length === 0 || 'name' in tokenVocabulary[0]);
}
/**
 * Returns a check whether the given TokenVocabulary is IMultiModeLexerDefinition
 */
function isIMultiModeLexerDefinition(tokenVocabulary) {
    return tokenVocabulary && 'modes' in tokenVocabulary && 'defaultMode' in tokenVocabulary;
}
/**
 * Returns a check whether the given TokenVocabulary is TokenTypeDictionary
 */
function isTokenTypeDictionary(tokenVocabulary) {
    return !isTokenTypeArray(tokenVocabulary) && !isIMultiModeLexerDefinition(tokenVocabulary);
}

exports.DEFAULT_TOKENIZE_OPTIONS = DEFAULT_TOKENIZE_OPTIONS;
exports.DefaultLexer = DefaultLexer;
exports.DefaultLexerErrorMessageProvider = DefaultLexerErrorMessageProvider;
exports.isIMultiModeLexerDefinition = isIMultiModeLexerDefinition;
exports.isTokenTypeArray = isTokenTypeArray;
exports.isTokenTypeDictionary = isTokenTypeDictionary;
