124 lines
2.7 KiB
C++
124 lines
2.7 KiB
C++
#pragma once
|
|
|
|
#include "libwords_global.h"
|
|
#include <memory>
|
|
#include <QtCore/QString>
|
|
#include <QtCore/QTextStream>
|
|
|
|
|
|
namespace lib_words {
|
|
/**
|
|
* @brief 源码词语实例
|
|
*/
|
|
class IWordBase {
|
|
public:
|
|
/**
|
|
* @brief 源文件路径
|
|
* @return
|
|
*/
|
|
virtual QString file() const = 0;
|
|
/**
|
|
* @brief 获取位置
|
|
* @return 起始位置
|
|
*/
|
|
virtual uint64_t position() const = 0;
|
|
/**
|
|
* @brief 词语内容
|
|
* @return
|
|
*/
|
|
virtual QString content() const = 0;
|
|
/**
|
|
* @brief 源码行号
|
|
* @return
|
|
*/
|
|
virtual int row() const = 0;
|
|
/**
|
|
* @brief 源码列号
|
|
* @return
|
|
*/
|
|
virtual int column() const = 0;
|
|
};
|
|
|
|
class IPrimitiveWord : public IWordBase {
|
|
public:
|
|
/**
|
|
* @brief 下一个单词.
|
|
*
|
|
* \return
|
|
*/
|
|
virtual std::shared_ptr<const IPrimitiveWord> nextWord() const = 0;
|
|
};
|
|
|
|
/**
|
|
* @brief 未解析原始词语
|
|
*/
|
|
class LIBWORDS_EXPORT WordContent : public IPrimitiveWord {
|
|
private:
|
|
int row_n, col_n;
|
|
uint64_t doc_offset;
|
|
QString text_n, path_p;
|
|
|
|
public:
|
|
WordContent(int r, int c, const QString& t, const QString& p);
|
|
|
|
// WordBase interface
|
|
public:
|
|
virtual QString file() const override;
|
|
virtual uint64_t position() const override;
|
|
virtual QString content() const override;
|
|
virtual int row() const override;
|
|
virtual int column() const override;
|
|
virtual std::shared_ptr<const IPrimitiveWord> nextWord() const override;
|
|
};
|
|
|
|
class LIBWORDS_EXPORT WordImpl : public IPrimitiveWord {
|
|
private:
|
|
std::shared_ptr<const IWordBase> content_ptr;
|
|
std::shared_ptr<const IPrimitiveWord> next_ptr;
|
|
|
|
public:
|
|
WordImpl(std::shared_ptr<const IWordBase> content, std::shared_ptr<const IPrimitiveWord> next);
|
|
|
|
// 通过 IWordBase 继承
|
|
QString file() const override;
|
|
virtual uint64_t position() const override;
|
|
QString content() const override;
|
|
int row() const override;
|
|
int column() const override;
|
|
std::shared_ptr<const IPrimitiveWord> nextWord() const override;
|
|
};
|
|
|
|
|
|
/**
|
|
* @brief 源码词语获取器
|
|
*/
|
|
class LIBWORDS_EXPORT WordReader {
|
|
private:
|
|
QList<std::shared_ptr<const IWordBase>> parse_line(uint64_t start_pos, int row, const QString& line_text, const QString& path) const;
|
|
QList<std::shared_ptr<const IWordBase>> extract_primary(QTextStream& in, const QString &f_path) const;
|
|
|
|
public:
|
|
/**
|
|
* @brief 提取源码中所有的词语
|
|
*/
|
|
std::shared_ptr<const IPrimitiveWord> wordsFrom(QTextStream& tin, const QString& path) const;
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
* @brief 解析异常
|
|
*/
|
|
class LIBWORDS_EXPORT WordsException {
|
|
private:
|
|
QString msg_store;
|
|
|
|
public:
|
|
WordsException(const QString& message);
|
|
virtual QString message() const;
|
|
};
|
|
|
|
|
|
LIBWORDS_EXPORT uint qHash(const std::shared_ptr<const IPrimitiveWord>& t, uint seed = 0) Q_DECL_NOTHROW;
|
|
|
|
} |