YSTest: Lexical.h 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

Lexical.h
浏览该文件的文档.
1 /*
2 © 2012-2016 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:
63 string Prefix;
64
65 private:
67 string sequence;
68
69 public:
70 DefDeCtor(UnescapeContext)
71 DefDeCopyMoveCtorAssignment(UnescapeContext)
72
73 DefPred(const ynothrow, Handling, !Prefix.empty())
78 PDefH(bool, IsHandling, string_view pfx) const
79 ImplRet((YAssertNonnull(pfx.data()), Prefix == pfx))
80
81 DefGetter(const ynothrow, const string&, Sequence, sequence)
82
83 PDefH(void, Clear, ) ynothrow
84 ImplExpr(Prefix.clear(), sequence.clear())
85
86 string
87 Done();
88
89 PDefH(bool, PopIf, byte uc)
90 ImplRet(PopIf(char(uc)))
91 PDefH(bool, PopIf, char c)
92 ImplRet(!sequence.empty() && sequence.back() == c
93 ? (sequence.pop_back(), true) : false)
94 PDefH(void, Push, byte uc)
95 ImplExpr(Push(char(uc)))
96 PDefH(void, Push, char c)
97 ImplExpr(sequence += c)
98 };
99
100
106 YF_API bool
107 HandleBackslashPrefix(char, string&);
108
121 YF_API bool
122 NPLUnescape(string&, const UnescapeContext&, char);
123
124
145 class YF_API LexicalAnalyzer
146 {
147 public:
154 using PrefixHandler = std::function<bool(char, string&)>;
161 using Unescaper
162 = std::function<bool(string&, const UnescapeContext&, char)>;
163
164 private:
169 char line_concat = {};
175 UnescapeContext unescape_context;
180 char ld;
184 string cbuf;
189 vector<size_t> qlist;
190
191 public:
192 LexicalAnalyzer();
194 DefDeCopyMoveCtorAssignment(LexicalAnalyzer)
195
196 DefGetter(const ynothrow, const string&, Buffer, cbuf)
198
199 DefGetter(const ynothrow, const vector<size_t>&, Quotes, qlist)
200
201 private:
202 bool
203 CheckEscape(byte, Unescaper);
204
205 bool
206 CheckLineConcatnater(char, char = '\\', char = '\n');
208
210 bool
211 FilterForParse(char, Unescaper, PrefixHandler);
212
213 public:
224 void
225 ParseByte(char, Unescaper = NPLUnescape,
226 PrefixHandler = HandleBackslashPrefix);
227
229 void
230 ParseQuoted(char, Unescaper = NPLUnescape,
231 PrefixHandler = HandleBackslashPrefix);
233
238 list<string>
239 Literalize() const;
240 };
241
242
252 YF_API char
253 CheckLiteral(string_view) ynothrowv;
254
259 YF_API string_view
260 Deliteralize(string_view) ynothrowv;
261
266 YF_API string
267 Escape(string_view);
268
276 YF_API string
277 EscapeLiteral(string_view);
278
286 YF_API string
287 EscapeXML(string_view);
288
295 YF_API string
296 Literalize(string_view, char = '"');
298
299
304 yconstfn bool
305 IsGraphicalDelimeter(char c)
306 {
307 // return std::ispunct(c);
308 return c == '(' || c == ')' || c == ':' || c == ',' || c == ';'
309 || c == '#' || c == '%' || c == '!';
310 }
311
316 yconstfn bool
317 IsDelimeter(char c)
318 {
319 return byte(c) < 0x80 && (!std::isgraph(c) || IsGraphicalDelimeter(c));
320 }
321
322
329 YF_API list<string>
330 Decompose(string_view);
331
337 YF_API list<string>
338 Tokenize(const list<string>&);
339
340 } // namespace NPL;
341
342 #endif
343
NPL::UnescapeContext::Prefix
string Prefix
转义序列前缀。
Definition: Lexical.h:63
NPL::IsGraphicalDelimeter
yconstfn bool IsGraphicalDelimeter(char c)
判断是否为 NPL 图形分隔符。
Definition: Lexical.h:305
ystdex::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:415
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
platform::containers::string
basic_string< char > string
Definition: Container.h:101
NPL::Decompose
YF_API list< string > Decompose(string_view)
分解字符串为记号。
Definition: Lexical.cpp:377
NPL::UnescapeContext::sequence
string sequence
有效转义序列。
Definition: Lexical.h:67
NPL::ImplRet
const TermNode &term ImplRet(term) inline PDefH(ValueNode &
NPL::Escape
YF_API string Escape(string_view)
编码转义字符串：替换指定字符串中的可转义字符为转义序列。
Definition: Lexical.cpp:253
DefDeCopyMoveCtorAssignment
#define DefDeCopyMoveCtorAssignment(_t)
Definition: YBaseMacro.h:201
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:760
ystdex::data
auto data(_tRange &c) -> decltype(c.data())
Definition: range.hpp:280
NPL::Tokenize
YF_API list< string > Tokenize(const list< string > &)
记号化：提取字符串列表中的记号。
Definition: Lexical.cpp:403
DefDeCtor
#define DefDeCtor(_t)
简单通用成员函数定义。
Definition: YBaseMacro.h:156
NPL::UnescapeContext
反转义上下文。
Definition: Lexical.h:59
NPL::EscapeXML
YF_API string EscapeXML(string_view)
编码 XML 字符串。
Definition: Lexical.cpp:339
CHRLib::c
c
Definition: CharacterProcessing.h:81
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:240
NPL::HandleBackslashPrefix
YF_API bool HandleBackslashPrefix(char, string &)
设置反斜杠转义前缀：当输入 '\' 时设置前缀为 "\\" 。
Definition: Lexical.cpp:47
NPL::LexicalAnalyzer::qlist
vector< size_t > qlist
字符解析中间结果中非转义的引号出现的位置的有序列表。
Definition: Lexical.h:189
NPL::LexicalAnalyzer::unescape_context
UnescapeContext unescape_context
反转移上下文：储存反转义中间结果。
Definition: Lexical.h:175
NPL::IsDelimeter
yconstfn bool IsDelimeter(char c)
判断是否为 NPL 分隔符。
Definition: Lexical.h:317
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:114
NPL::LexicalAnalyzer::cbuf
string cbuf
字符解析中间结果。
Definition: Lexical.h:184
YModules.h
NPL::Deliteralize
YF_API string_view Deliteralize(string_view) ynothrowv
去除字面量边界分隔符。
Definition: Lexical.cpp:247
NPL::LexicalAnalyzer::ld
char ld
字面分隔符状态：表示正在处理字面量中的有效字符。
Definition: Lexical.h:180
NPL
Definition: NPL/Configuration.h:34
NPL::LexicalAnalyzer::PrefixHandler
std::function< bool(char, string &)> PrefixHandler
转移序列前缀处理器。
Definition: Lexical.h:154
NPL::Literalize
YF_API string Literalize(string_view, char= '"')
修饰字符串为字面量。
Definition: Lexical.cpp:369
ystdex::byte
unsigned char byte
字节类型。
Definition: ydef.h:808
NPL::NPLUnescape
YF_API bool NPLUnescape(string &, const UnescapeContext &, char)
NPL 转义匹配算法。
Definition: Lexical.cpp:58
NPL::EscapeLiteral
YF_API string EscapeLiteral(string_view)
编码转义字符串字面量。
Definition: Lexical.cpp:327
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:654
NPL::LexicalAnalyzer::Unescaper
std::function< bool(string &, const UnescapeContext &, char)> Unescaper
指定匹配转义序列的反转义算法：解析转义序列并按需修改指定缓存。
Definition: Lexical.h:162
NPL::LexicalAnalyzer
词法分析器。
Definition: Lexical.h:145
YF_API
#define YF_API
Definition: Platform.h:56
ystdex::cpp2011::begin
_type * begin(_type(&&array)[_vN])
Definition: range.hpp:46
NPL::PDefH
YF_API const char * PDefH(void, InstallFile, const string &dst, const string &src) ImplExpr(InstallFile(dst.c_str()
DefPred
#define DefPred(_q, _n,...)
Definition: YBaseMacro.h:231
NPL::CheckLiteral
YF_API char CheckLiteral(string_view) ynothrowv
检查指定字符串是否为字面量。
Definition: Lexical.cpp:234
ystdex::cpp2011::end
_type * end(_type(&&array)[_vN])
Definition: range.hpp:54
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
安装硬链接：创建硬链接，失败则安装文件。
platform::n
size_t n
Definition: YCLib/FileSystem.cpp:321