YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
SContext.cpp
浏览该文件的文档.
1 /*
2  © 2012-2013 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 #include "NPL/YModules.h"
29 #include YFM_NPL_SContext
30 
31 using namespace YSLib;
32 
33 namespace NPL
34 {
35 
36 Session::Session(const string& line)
37  : llex()
38 {
39  for(const auto& c : line)
40  llex.ParseByte(c);
41 }
43 {
44  ystdex::ifile_iterator i(tf.GetPtr());
45 
46  while(!tf.CheckEOF())
47  {
49  throw LoggedEvent("Bad Source!", Critical);
50  llex.ParseByte(*i);
51  ++i;
52  }
53 }
54 
55 
56 namespace SContext
57 {
58 
59 TLCIter
61 {
62  while(b != e && *b != ")")
63  if(*b == "(")
64  {
65  auto res(Validate(++b, e));
66 
67  if(res == e || *res != ")")
68  throw LoggedEvent("Redundant '(' found.", Alert);
69  b = ++res;
70  }
71  else
72  ++b;
73  return b;
74 }
75 
76 TLCIter
78 {
79  while(b != e && *b != ")")
80  if(*b == "(")
81  {
82  auto nd(MakeNode(to_string(node.GetSize())));
83  auto res(Reduce(nd, ++b, e));
84 
85  if(res == e || *res != ")")
86  throw LoggedEvent("Redundant '(' found.", Alert);
87  node += std::move(nd);
88  b = ++res;
89  }
90  else
91  node += {0, to_string(node.GetSize()), *b++};
92  return b;
93 }
94 
95 void
96 Analyze(ValueNode& root, const TokenList& token_list)
97 {
98 #if 0
99  if(token_list.empty())
100  throw LoggedEvent("Empty token list found;", Alert);
101 #endif
102  if(Validate(token_list.begin(), token_list.end()) != token_list.end())
103  throw LoggedEvent("Redundant ')' found.", Alert);
104 
105  const auto res(Reduce(root, token_list.begin(), token_list.end()));
106 
107  yassume(res == token_list.end());
108 }
109 void
110 Analyze(ValueNode& root, const Session& session)
111 {
112  Analyze(root, session.GetTokenList());
113 }
114 void
115 Analyze(ValueNode& root, const string& unit)
116 {
117  Analyze(root, Session(unit).GetTokenList());
118 }
119 
120 } // namespace SContext;
121 
122 } // namespace NPL;
123 
会话:分析指定 NPL 代码。
Definition: SContext.h:54
TokenList::const_iterator TLCIter
Definition: SContext.h:47
YF_API TLCIter Reduce(ValueNode &node, TLCIter b, TLCIter e)
遍历规约记号列表,取抽象语法树储存至指定值类型节点。
Definition: SContext.cpp:77
基于 ISO C 标准库的流只读迭代器。
Definition: cstdio.h:73
文本文件类。
Definition: TextFile.h:56
YF_API TLCIter Validate(TLCIter b, TLCIter e)
遍历记号列表,验证基本合法性:圆括号是否对应。
Definition: SContext.cpp:60
YF_API void Analyze(ValueNode &, const TokenList &)
分析指定源,取抽象语法树储存至指定值类型节点。
Definition: SContext.cpp:96
#define YB_UNLIKELY(expr)
分支预测提示。
Definition: ydef.h:298
#define yassume
假定:环境语义。
Definition: cassert.h:58
值类型节点。
Definition: ValueNode.h:45
std::string to_string(unsigned char val)
转换为字符串。
Definition: string.hpp:353
Container Value const string name size_t GetSize() const ynothrow
Definition: ValueNode.cpp:47
记录日志的异常事件类。
Definition: yexcept.h:58
list< string > TokenList
Definition: SContext.h:45
bool is_undereferenceable(const any_input_iterator< _type, _tDifference, _tPointer, _tReference > &i)
LexicalAnalyzer llex
Definition: SContext.h:57
Session(const string &)
Definition: SContext.cpp:36
fp fsize ptrdiff_t int whence const CheckEOF
Definition: File.h:111
void ParseByte(byte)
Definition: Lexical.cpp:98