🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
MarkdownTableParser.hpp
浏览该文件的文档.
1
22
23#pragma once
24
25#include "AstGlobal.h"
27#include "MarkdownSax.hpp"
28#include <string>
29#include <vector>
30
31AST_NAMESPACE_BEGIN
32
39class MarkdownSax;
40
58class AST_UTIL_API MarkdownTableParser
59{
60public:
61 explicit MarkdownTableParser(MarkdownSax& sax);
62 ~MarkdownTableParser() = default;
63
64 void feed(StringView chunk);
65
70 void feedChar(char c);
71
73 void finish();
74
76 void reset();
77
79 bool isIdle() const { return state_ == EState::eIdle; }
80
83 bool isCharConsumed() const { return charConsumed_; }
84
85private:
86 // ---- 内部状态 ----
87 enum class EState
88 {
89 eIdle,
90 eHeaderRow,
91 eSeparator,
92 eBodyRow,
93 };
94
95 // ---- 行处理 ----
97 void processRow(const std::string& rowContent);
98
100 void processHeaderRow(const std::string& row);
102 void processSeparatorRow(const std::string& row);
104 void processBodyRow(const std::string& row);
105
106 // ---- 表格结构发射 ----
108 void emitTableStart();
110 void emitTableEnd();
111
112 // ---- 回退 ----
114 void abortAsParagraph();
115
116 // ---- 辅助 ----
118 static std::vector<std::string> splitCells(const std::string& row);
120 static bool isTableRow(const std::string& row);
122 static bool isSeparatorRow(const std::string& row);
124 static std::vector<ETableAlign> parseAlignments(const std::string& sepRow);
126 static std::string trim(const std::string& s);
127
128 // ---- 状态成员 ----
129 MarkdownSax& sax_;
130 MarkdownInlineParser inlineSM_;
131
132 EState state_ = EState::eIdle;
133 bool charConsumed_ = true;
134
135 // 行缓冲(逐字符累积,\n 时处理并清空)
136 std::string rowBuf_;
137
138 // 表头缓冲(分隔行确认前暂存)
139 std::vector<std::string> headerCells_;
140
141 // 列对齐(分隔行解析后确定)
142 std::vector<ETableAlign> colAligns_;
143
144 // 回退文本(表格未确认时,累积的原始行文本,用于回退为段落)
145 std::string abortBuf_;
146};
147
148
151AST_NAMESPACE_END
定义 MarkdownSax.hpp:45
Markdown 表格流式解析状态机
定义 MarkdownTableParser.hpp:59
bool isIdle() const
是否处于空闲状态(未在解析表格)
定义 MarkdownTableParser.hpp:79
bool isCharConsumed() const
feedChar 返回后,最近一次输入的字符是否已被消费
定义 MarkdownTableParser.hpp:83