<commit_before>/*
 * Copyright (C) 2015-2017 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef YDSH_LEXER_H
#define YDSH_LEXER_H

#include <cstdlib>
#include <cstdint>

#include "token_kind.h"
#include "misc/lexer_base.hpp"
#include "misc/resource.hpp"

namespace ydsh {

#include <yycond.h>

const char *toModeName(LexerMode mode);


class SourceInfo : public RefCount<SourceInfo> {
private:
    std::string sourceName;

    /**
     * default value is 1.
     */
    unsigned int lineNumOffset{1};

    /**
     * contains newline character position.
     */
    std::vector<unsigned int> lineNumTable;

public:
    explicit SourceInfo(const char *sourceName) : sourceName(sourceName) { }
    ~SourceInfo() = default;

    const std::string &getSourceName() const {
        return this->sourceName;
    }

    void setLineNumOffset(unsigned int offset) {
        this->lineNumOffset = offset;
    }

    unsigned int getLineNumOffset() const {
        return this->lineNumOffset;
    }

    const std::vector<unsigned int> &getLineNumTable() const {
        return this->lineNumTable;
    }

    void addNewlinePos(unsigned int pos);
    unsigned int getLineNum(unsigned int pos) const;
};

using SourceInfoPtr = IntrusivePtr<SourceInfo>;

class Lexer : public ydsh::parser_base::LexerBase {
private:
    SourceInfoPtr srcInfoPtr;

    /**
     * default mode is yycSTMT
     */
    std::vector<LexerMode> modeStack;

    bool prevNewLine{false};

    /**
     * only available in command mode.
     */
    bool prevSpace{false};

    LexerMode prevMode{yycSTMT};

public:
    NON_COPYABLE(Lexer);

    /**
     *
     * @param sourceName
     * must not be null.
     * @param source
     * must be null terminated.
     * @return
     */
    Lexer(const char *sourceName, const char *source) : Lexer(sourceName, source, strlen(source)) {}

    /**
     *
     * @param sourceName
     * must not be null
     * @param source
     * must not be null
     * @param size
     * @return
     */
    Lexer(const char *sourceName, const char *source, unsigned int size) :
            LexerBase(source, size), srcInfoPtr(SourceInfoPtr::create(sourceName)), modeStack(1, yycSTMT) {}

    /**
     * 
     * @param sourceName
     * must not be null.
     * @param fp
     * must be opened with binary mode.
     * @return
     */
    Lexer(const char *sourceName, FILE *fp) :
            LexerBase(fp), srcInfoPtr(SourceInfoPtr::create(sourceName)), modeStack(1, yycSTMT) {}

    ~Lexer() = default;

    void setPos(unsigned int pos) {
        assert(this->buf + pos <= this->limit);
        this->cursor = this->buf + pos;
    }

    const SourceInfoPtr &getSourceInfoPtr() const {
        return this->srcInfoPtr;<commit_msg>Lexer is move assignable<commit_after>/*
 * Copyright (C) 2015-2017 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef YDSH_LEXER_H
#define YDSH_LEXER_H

#include <cstdlib>
#include <cstdint>

#include "token_kind.h"
#include "misc/lexer_base.hpp"
#include "misc/resource.hpp"

namespace ydsh {

#include <yycond.h>

const char *toModeName(LexerMode mode);


class SourceInfo : public RefCount<SourceInfo> {
private:
    std::string sourceName;

    /**
     * default value is 1.
     */
    unsigned int lineNumOffset{1};

    /**
     * contains newline character position.
     */
    std::vector<unsigned int> lineNumTable;

public:
    explicit SourceInfo(const char *sourceName) : sourceName(sourceName) { }
    ~SourceInfo() = default;

    const std::string &getSourceName() const {
        return this->sourceName;
    }

    void setLineNumOffset(unsigned int offset) {
        this->lineNumOffset = offset;
    }

    unsigned int getLineNumOffset() const {
        return this->lineNumOffset;
    }

    const std::vector<unsigned int> &getLineNumTable() const {
        return this->lineNumTable;
    }

    void addNewlinePos(unsigned int pos);
    unsigned int getLineNum(unsigned int pos) const;
};

using SourceInfoPtr = IntrusivePtr<SourceInfo>;

class Lexer : public ydsh::parser_base::LexerBase {
private:
    SourceInfoPtr srcInfoPtr;

    /**
     * default mode is yycSTMT
     */
    std::vector<LexerMode> modeStack;

    bool prevNewLine{false};

    /**
     * only available in command mode.
     */
    bool prevSpace{false};

    LexerMode prevMode{yycSTMT};

public:
    NON_COPYABLE(Lexer);

    Lexer() = default;

    /**
     *
     * @param sourceName
     * must not be null.
     * @param source
     * must be null terminated.
     * @return
     */
    Lexer(const char *sourceName, const char *source) : Lexer(sourceName, source, strlen(source)) {}

    /**
     *
     * @param sourceName
     * must not be null
     * @param source
     * must not be null
     * @param size
     * @return
     */
    Lexer(const char *sourceName, const char *source, unsigned int size) :
            LexerBase(source, size), srcInfoPtr(SourceInfoPtr::create(sourceName)), modeStack(1, yycSTMT) {}

    /**
     * 
     * @param sourceName
     * must not be null.
     * @param fp
     * must be opened with binary mode.
     * @return
     */
    Lexer(const char *sourceName, FILE *fp) :
            LexerBase(fp), srcInfoPtr(SourceInfoPtr::create(sourceName)), modeStack(1, yycSTMT) {}

    Lexer(Lexer &&lex) noexcept :
            LexerBase(std::move(lex)), srcInfoPtr(std::move(lex.srcInfoPtr)),
            modeStack(std::move(lex.modeStack)), prevNewLine(lex.prevNewLine),
            prevSpace(lex.prevSpace), prevMode(lex.prevMode) {}

    ~Lexer() = default;

    Lexer &operator=(Lexer &&lex) noexcept {
        auto tmp(std::move(lex));
        this->swap(lex);
        return *this;
    }

    void swap(Lexer &lex) {
        LexerBase::swap(lex);
        std::swap(this->srcInfoPtr, lex.srcInfoPtr);
        std::swap(prevNewLine, lex.prevNewLine);
        std::swap(this->prevSpace, lex.prevSpace);
        std::swap(this->prevMode, lex.prevMode);
    }

    void setPos(unsigned int pos) {
        assert(this->buf + pos <= this->limit);
        this->cursor = this->buf + pos;
    }

    const SourceInfoPtr &getSourceInfoPtr() const {
        return this->srcInfoPtr;
{'fraction_non_alphanumeric': 0.11312091275914846, 'fraction_numerical': 0.004452483651036594, 'mean_word_length': 2.856223175965665, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2673435', 'n_tokens_mistral': 2269, 'n_tokens_neox': 2173, 'n_words': 616}