YSTest: Lexical.h 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

Lexical.h
浏览该文件的文档.
1 /*
2 © 2012-2017 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YF_INC_NPL_Lexical_h_
29 #define YF_INC_NPL_Lexical_h_ 1
30
31 #include "YModules.h"
32 #include YFM_NPL_NPL // for byte;
33 #include YFM_YSLib_Adaptor_YTextBase // for ystdex::byte, YSLib::list,
34 // YSLib::string, YSLib::string_view, YSLib::vector, YSLib::begin, YSLib::end;
35 #include <cctype> // for std::isgraph;
36
37 namespace NPL
38 {
39
41
42 using YSLib::list;
43 using YSLib::string;
45 using YSLib::string_view;
48 using YSLib::vector;
50 using YSLib::begin;
52 using YSLib::end;
53
54
59 class YF_API UnescapeContext
60 {
61 public:
67 string Prefix;
68
69 private:
71 string sequence;
72
73 public:
74 DefDeCtor(UnescapeContext)
75 DefDeCopyMoveCtorAssignment(UnescapeContext)
76
77 DefPred(const ynothrow, Handling, !Prefix.empty())
82 PDefH(bool, IsHandling, string_view pfx) const
83 ImplRet((YAssertNonnull(pfx.data()), Prefix == pfx))
84
85 DefGetter(const ynothrow, const string&, Sequence, sequence)
86
87 PDefH(void, Clear, ) ynothrow
88 ImplExpr(Prefix.clear(), sequence.clear())
89
90 string
91 Done();
92
93 PDefH(bool, PopIf, byte uc)
94 ImplRet(PopIf(char(uc)))
95 PDefH(bool, PopIf, char c)
96 ImplRet(ystdex::pop_back_val(sequence, c))
97 PDefH(void, Push, byte uc)
98 ImplExpr(Push(char(uc)))
99 PDefH(void, Push, char c)
100 ImplExpr(sequence += c)
101 };
102
103
109 YF_API bool
110 HandleBackslashPrefix(char, string&);
111
117 yconstfn PDefH(bool, IgnorePrefix, char, string&) ynothrow
118 ImplRet({})
119
132 YF_API bool
133 NPLUnescape(string&, const UnescapeContext&, char);
134
135
156 class YF_API LexicalAnalyzer
157 {
158 public:
165 using PrefixHandler = std::function<bool(char, string&)>;
172 using Unescaper
173 = std::function<bool(string&, const UnescapeContext&, char)>;
174
175 private:
180 char line_concat = {};
186 UnescapeContext unescape_context;
191 char ld;
195 string cbuf;
200 vector<size_t> qlist;
201
202 public:
203 LexicalAnalyzer();
205 DefDeCopyMoveCtorAssignment(LexicalAnalyzer)
206
207 DefGetter(const ynothrow, const string&, Buffer, cbuf)
209
210 DefGetter(const ynothrow, const vector<size_t>&, Quotes, qlist)
211
212 private:
213 bool
214 CheckEscape(byte, Unescaper);
215
216 bool
217 CheckLineConcatnater(char, char = '\\', char = '\n');
219
221 bool
222 FilterForParse(char, Unescaper, PrefixHandler);
223
224 public:
235 void
236 ParseByte(char, Unescaper = NPLUnescape,
237 PrefixHandler = HandleBackslashPrefix);
238
240 void
241 ParseQuoted(char, Unescaper = NPLUnescape,
242 PrefixHandler = HandleBackslashPrefix);
243
248 PDefH(void, ParseRaw, char c)
249 ImplExpr(cbuf += c);
251
256 list<string>
257 Literalize() const;
258 };
259
260
270 YF_API char
271 CheckLiteral(string_view) ynothrowv;
272
279 YF_API string_view
281 Deliteralize(string_view) ynothrowv;
282
284 inline PDefH(string_view, DeliteralizeUnchecked, string_view sv) ynothrowv
285 ImplRet(YAssertNonnull(sv.data()), ystdex::get_mid(sv))
287
292 YF_API string
293 Escape(string_view);
294
302 YF_API string
303 EscapeLiteral(string_view);
304
312 YF_API string
313 EscapeXML(string_view);
314
321 YF_API string
322 Literalize(string_view, char = '"');
324
325
327
328 yconstfn PDefH(bool, IsGraphicalDelimeter, char c) ynothrow
330 ImplRet(c == '(' || c == ')' || c == ',' || c == ';')
331
333 yconstfn PDefH(bool, IsDelimeter, char c) ynothrow
334 ImplRet(byte(c) < 0x80 && (!std::isgraph(c) || IsGraphicalDelimeter(c)))
336
337
344 YF_API list<string>
345 Decompose(string_view);
346
352 YF_API list<string>
353 Tokenize(const list<string>&);
354
355 } // namespace NPL;
356
357 #endif
358
NPL::HandleBackslashPrefix
YF_API bool HandleBackslashPrefix(char, string &)
设置反斜杠转义前缀：当输入 '\' 时设置前缀为 "\\" 。
Definition: Lexical.cpp:47
ystdex::get_mid
_tString get_mid(const _tString &str, typename _tString::size_type l=1)
取删除前缀和后缀的子字符串。
Definition: string.hpp:829
NPL::LexicalAnalyzer::Unescaper
std::function< bool(string &, const UnescapeContext &, char)> Unescaper
指定匹配转义序列的反转义算法：解析转义序列并按需修改指定缓存。
Definition: Lexical.h:173
NPL::LexicalAnalyzer::unescape_context
UnescapeContext unescape_context
反转移上下文：储存反转义中间结果。
Definition: Lexical.h:186
DefPred
#define DefPred(_q, _n,...)
Definition: YBaseMacro.h:232
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
NPL::Decompose
list< string > Decompose(string_view src)
Definition: Lexical.cpp:379
YModules.h
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:120
NPL::LexicalAnalyzer
词法分析器。
Definition: Lexical.h:156
DefDeCopyMoveCtorAssignment
#define DefDeCopyMoveCtorAssignment(_t)
Definition: YBaseMacro.h:202
platform::containers::string
basic_string< char > string
Definition: Container.h:101
NPL::UnescapeContext::sequence
string sequence
有效转义序列。
Definition: Lexical.h:71
ystdex::pop_back_val
bool pop_back_val(_tCon &con, const typename _tCon::value_type &value)
若容器末尾存在指定值的元素则移除。
Definition: container.hpp:886
ystdex::data
auto data(_tRange &c) -> decltype(c.data())
Definition: range.hpp:280
NPL::NPLUnescape
YF_API bool NPLUnescape(string &, const UnescapeContext &, char)
NPL 转义匹配算法。
Definition: Lexical.cpp:58
ystdex::cpp2011::end
_type * end(_type(&&array)[_vN])
Definition: range.hpp:54
NPL::LexicalAnalyzer::ld
char ld
字面分隔符状态：表示正在处理字面量中的有效字符。
Definition: Lexical.h:191
NPL::UnescapeContext
反转义上下文。
Definition: Lexical.h:59
NPL::PDefH
YF_API const char * PDefH(void, InstallFile, const string &dst, const string &src) ImplExpr(InstallFile(dst.c_str()
DefDeCtor
#define DefDeCtor(_t)
简单通用成员函数定义。
Definition: YBaseMacro.h:157
NPL::Tokenize
YF_API list< string > Tokenize(const list< string > &)
记号化：提取字符串列表中的记号。
Definition: Lexical.cpp:407
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:766
NPL::LexicalAnalyzer::qlist
vector< size_t > qlist
字符解析中间结果中非转义的引号出现的位置的有序列表。
Definition: Lexical.h:200
ystdex
特性测试宏。
Definition: ydef.h:822
NPL::EscapeLiteral
YF_API string EscapeLiteral(string_view)
编码转义字符串字面量。
Definition: Lexical.cpp:329
NPL::LexicalAnalyzer::PrefixHandler
std::function< bool(char, string &)> PrefixHandler
转移序列前缀处理器。
Definition: Lexical.h:165
YF_API
#define YF_API
Definition: Platform.h:56
NPL::ImplExpr
YF_API const char src const string const char const char *src ImplExpr(InstallDirectory(string(dst), string(src))) YF_API YB_NONNULL(1
安装硬链接：创建硬链接，失败则安装文件。
NPL::UnescapeContext::Prefix
string Prefix
转义序列前缀。
Definition: Lexical.h:67
NPL::ImplRet
const TermNode &term ynothrow ImplRet(!term) inline PDefH(bool
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:786
NPL::LexicalAnalyzer::cbuf
string cbuf
字符解析中间结果。
Definition: Lexical.h:195
std
Definition: memory.hpp:1183
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:241
ystdex::byte
unsigned char byte
字节类型。
Definition: ydef.h:834
NPL
Definition: NPL/Configuration.h:34
NPL::Escape
string Escape(string_view sv)
Definition: Lexical.cpp:255
NPL::Deliteralize
YF_API string_view Deliteralize(string_view) ynothrowv
去除字面量边界分隔符。
Definition: Lexical.cpp:249
CHRLib::c
c
Definition: CharacterProcessing.h:81
NPL::CheckLiteral
YF_API char CheckLiteral(string_view) ynothrowv
检查指定字符串是否为字面量。
Definition: Lexical.cpp:236
ystdex::cpp2011::begin
_type * begin(_type(&&array)[_vN])
Definition: range.hpp:46
NPL::Literalize
YF_API string Literalize(string_view, char= '"')
修饰字符串为字面量。
Definition: Lexical.cpp:371
ystdex::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:415
NPL::EscapeXML
YF_API string EscapeXML(string_view)
编码 XML 字符串。
Definition: Lexical.cpp:341