YSTest: SContext.h 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

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

- 文件列表
- 文件成员

SContext.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 NPL_INC_SContext_h_
29 #define NPL_INC_SContext_h_ 1
30
31 #include "YModules.h"
32 #include YFM_NPL_Lexical
33 #include YFM_YSLib_Core_ValueNode // for YSLib::ValueObject, YSLib::ValueNode;
34
35 namespace NPL
36 {
37
39 using YSLib::ValueObject;
41 using YSLib::ValueNode;
42 using TokenList = list<string>;
43 using TLIter = TokenList::iterator;
44 using TLCIter = TokenList::const_iterator;
46
47 using YSLib::observer_ptr;
48 using YSLib::LoggedEvent;
49
51 using TermNode = yimpl(ValueNode);
52
53 using TNIter = TermNode::iterator;
54 using TNCIter = TermNode::const_iterator;
55
61 inline PDefH(bool, IsBranch, const TermNode& term) ynothrow
62 ImplRet(!term.empty())
63
64 inline PDefH(bool, IsEmpty, const TermNode& term) ynothrow
65 ImplRet(!term)
66
67 inline PDefH(bool, IsLeaf, const TermNode& term) ynothrow
68 ImplRet(term.empty())
69
71 inline PDefH(bool, IsList, const TermNode& term) ynothrow
72 ImplRet(!term.empty() || !term.Value)
74
75 inline PDefH(TermNode&, MapToTermNode, TermNode& term)
76 ImplRet(term)
77 inline PDefH(const TermNode&, MapToTermNode, const TermNode& term)
78 ImplRet(term)
79
80 inline PDefH(ValueNode&, MapToValueNode, ValueNode& node)
81 ImplRet(node)
82 inline PDefH(const ValueNode&, MapToValueNode, const ValueNode& node)
83 ImplRet(node)
85
86
91 template<typename _type>
92 inline bool
93 HasValue(const TermNode& term, const _type& x)
94 {
95 return term.Value == x;
96 }
97
98
103 class YF_API Session
104 {
105 public:
107
108 using CharParser = std::function<void(LexicalAnalyzer&, char)>;
109
111 LexicalAnalyzer Lexer;
112
114 DefDeCtor(Session)
116 template<typename _tIn>
117 Session(_tIn first, _tIn last, CharParser parse = DefaultParseByte)
118 : Lexer()
119 {
120 std::for_each(first, last,
121 std::bind(parse, std::ref(Lexer), std::placeholders::_1));
122 }
123 template<typename _tRange,
124 yimpl(typename = ystdex::exclude_self_t<Session, _tRange>)>
125 Session(const _tRange& c, CharParser parse = DefaultParseByte)
126 : Session(begin(c), end(c), parse)
127 {}
128 DefDeCopyMoveCtorAssignment(Session)
129
130 DefGetterMem(const ynothrow, const string&, Buffer, Lexer)
132 DefGetter(const, TokenList, TokenList, Tokenize(Lexer.Literalize()))
133
138 static void
139 DefaultParseByte(LexicalAnalyzer&, char);
140
145 static void
146 DefaultParseQuoted(LexicalAnalyzer&, char);
147 };
148
149
154 namespace SContext
155 {
156
166 YF_API TLCIter
167 Validate(TLCIter b, TLCIter e);
168
170
171
180 YF_API TLCIter
181 Reduce(TermNode& term, TLCIter b, TLCIter e);
182
183
189 YF_API void
190 Analyze(TermNode&, const TokenList&);
191 YF_API void
192 Analyze(TermNode&, const Session&);
194 template<typename _type>
196 TermNode
197 Analyze(const _type& arg)
198 {
199 TermNode root;
200
201 Analyze(root, arg);
202 return root;
203 }
205
206 } // namespace SContext;
207
208 } // namespace NPL;
209
210 #endif
211
NPL::Session::Lexer
LexicalAnalyzer Lexer
Definition: SContext.h:111
NPL::Session::CharParser
std::function< void(LexicalAnalyzer &, char)> CharParser
Definition: SContext.h:108
NPL::MapToTermNode
const TermNode &term ynothrow const TermNode &term ynothrow const TermNode &term ynothrow MapToTermNode
Definition: SContext.h:75
NPL::IsList
const TermNode &term ynothrow const TermNode &term ynothrow IsList
Definition: SContext.h:71
NPL::SContext::Reduce
YF_API TLCIter Reduce(TermNode &term, TLCIter b, TLCIter e)
遍历规约记号列表，取抽象语法树储存至指定值类型节点。
Definition: SContext.cpp:71
NPL::x
const TermNode &term ynothrow const TermNode &term ynothrow const TermNode &term ynothrow TermNode &term const TermNode &term ValueNode &node const ValueNode &node const _type & x
Definition: SContext.h:94
YModules.h
NPL::Session
会话：分析指定 NPL 代码。
Definition: SContext.h:103
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:262
YSLib::LoggedEvent
记录日志的异常事件类。
Definition: YException.h:50
NPL::LexicalAnalyzer
词法分析器。
Definition: Lexical.h:156
DefDeCopyMoveCtorAssignment
#define DefDeCopyMoveCtorAssignment(_t)
Definition: YBaseMacro.h:202
ystdex::cpp2011::end
_type * end(_type(&&array)[_vN])
Definition: range.hpp:54
NPL::PDefH
YF_API const char * PDefH(void, InstallFile, const string &dst, const string &src) ImplExpr(InstallFile(dst.c_str()
NPL::SContext::Analyze
TermNode Analyze(const _type &arg)
Definition: SContext.h:197
DefDeCtor
#define DefDeCtor(_t)
简单通用成员函数定义。
Definition: YBaseMacro.h:157
NPL::Tokenize
YF_API list< string > Tokenize(const list< string > &)
记号化：提取字符串列表中的记号。
Definition: Lexical.cpp:407
NPL::TLCIter
TokenList::const_iterator TLCIter
Definition: SContext.h:44
YSLib::ValueObject
值类型对象类。
Definition: YObject.h:539
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:766
NPL::TermNode
yimpl(ValueNode) TermNode
项节点：存储语法分析结果的值类型节点。
Definition: SContext.h:51
DefGetterMem
#define DefGetterMem(_q, _t, _n, _m)
Definition: YBaseMacro.h:247
YF_API
#define YF_API
Definition: Platform.h:56
NPL::ImplRet
const TermNode &term ynothrow ImplRet(!term) inline PDefH(bool
NPL::TNCIter
TermNode::const_iterator TNCIter
Definition: SContext.h:54
NPL::TLIter
TokenList::iterator TLIter
Definition: SContext.h:43
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:109
YSLib::ValueNode
值类型节点。
Definition: ValueNode.h:58
ystdex::exclude_self_t
enable_if_t<!is_same< _tClass &, decay_t< _tParam > & >::value, _type > exclude_self_t
移除选择类类型的特定重载避免构造模板和复制/转移特殊成员函数冲突。
Definition: type_traits.hpp:748
NPL::TokenList
list< string > TokenList
Definition: SContext.h:42
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:241
NPL
Definition: NPL/Configuration.h:34
NPL::IsLeaf
const TermNode &term ynothrow IsLeaf
Definition: SContext.h:67
NPL::TNIter
TermNode::iterator TNIter
Definition: SContext.h:53
CHRLib::c
c
Definition: CharacterProcessing.h:81
ystdex::cpp2011::begin
_type * begin(_type(&&array)[_vN])
Definition: range.hpp:46
NPL::MapToValueNode
const TermNode &term ynothrow const TermNode &term ynothrow const TermNode &term ynothrow TermNode &term const TermNode &term MapToValueNode
Definition: SContext.h:80
NPL::Literalize
YF_API string Literalize(string_view, char= '"')
修饰字符串为字面量。
Definition: Lexical.cpp:371
ystdex::ref
lref< _type > ref(_type &t)
构造引用包装。
Definition: ref.hpp:132
NPL::IsEmpty
IsEmpty
Definition: SContext.h:64
NPL::SContext::Validate
YF_API TLCIter Validate(TLCIter b, TLCIter e)
遍历记号列表，验证基本合法性：圆括号是否对应。
Definition: SContext.cpp:53