76 lines
1.3 KiB
C
76 lines
1.3 KiB
C
|
#pragma once
|
|||
|
|
|||
|
#include <QString>
|
|||
|
#include <QList>
|
|||
|
|
|||
|
namespace Lex {
|
|||
|
class LexicalBase;
|
|||
|
|
|||
|
/**
|
|||
|
* 解析基准定义单元.
|
|||
|
*/
|
|||
|
struct LexUnit
|
|||
|
{
|
|||
|
QString TokenType; // Token字符
|
|||
|
QString RegExpression; // 词法解析表达式
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* 词法分析结果.
|
|||
|
*/
|
|||
|
struct LexResult
|
|||
|
{
|
|||
|
QString Token; // Token字符
|
|||
|
QString Text; // 内容
|
|||
|
int StartRow, StartCol, EndRow, EndCol; // 波及范围
|
|||
|
|
|||
|
friend class LexicalBase;
|
|||
|
private:
|
|||
|
int index_at_segment;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* 字符提取富信息单元.
|
|||
|
*/
|
|||
|
class XChar
|
|||
|
{
|
|||
|
public:
|
|||
|
explicit XChar(QChar c, int row, int col);
|
|||
|
QChar value() const;
|
|||
|
int row() const;
|
|||
|
int col() const;
|
|||
|
|
|||
|
private:
|
|||
|
QChar value_store;
|
|||
|
int row_index, col_index;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* 基础词法分析器.
|
|||
|
*/
|
|||
|
class LexicalBase
|
|||
|
{
|
|||
|
public:
|
|||
|
explicit LexicalBase(QList<LexUnit> seqence, const QString UnknownToken);
|
|||
|
virtual ~LexicalBase() = default;
|
|||
|
|
|||
|
/**
|
|||
|
* 词法分析器累积解析单个字符流.
|
|||
|
*
|
|||
|
* \param row 行
|
|||
|
* \param col 列
|
|||
|
* \param w 字符
|
|||
|
* \return 累积适配结果,空累积,
|
|||
|
*/
|
|||
|
QList<LexResult> push(int row, int col, const QChar w);
|
|||
|
|
|||
|
private:
|
|||
|
QString unknown_token;
|
|||
|
QList<QChar> empty_seq;
|
|||
|
|
|||
|
QList<XChar> code_acc;
|
|||
|
QList<LexUnit> lexical_seq;
|
|||
|
|
|||
|
QList<LexResult> lexical_parse(const QString &segment);
|
|||
|
};
|
|||
|
}
|