YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
Configuration.cpp
浏览该文件的文档.
1 /*
2  © 2012-2014 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_Configuration
30 #include YFM_NPL_SContext
31 
32 using namespace YSLib;
33 
34 namespace NPL
35 {
36 
37 namespace
38 {
39 
41 inline File&
42 WritePrefix(File& f, size_t n = 1, char c = '\t')
43 {
44  while(n--)
45  f << c;
46  return f;
47 }
48 
49 #if 0
50 File&
52 WriteNode(File& f, const ValueNode& node, size_t depth)
53 {
54  if(node.GetSize() != 0)
55  for(const auto& n : node)
56  {
57  WritePrefix(f, depth);
58  f << '(' << '\n';
59  try
60  {
61  WriteNode(f, n, depth + 1);
62  }
63  catch(std::out_of_range&)
64  {}
65  WritePrefix(f, depth);
66  f << ')' << '\n';
67  }
68  return f;
69 }
70 #endif
71 
73 string
74 EscapeNodeString(const string& str)
75 {
76  const char c(CheckLiteral(str));
77  auto content(MakeEscape(c == char() ? str : ystdex::get_mid(str)));
78 
79  return c == char() ? std::move(content) : c + content + c;
80 }
81 
83 bool
84 PrintNodeString(File& f, const ValueNode& node)
85 {
86  try
87  {
88  const auto& s(Access<string>(node));
89 
90  f << '"' << EscapeNodeString(s) << '"' << '\n';
91  return true;
92  }
93  catch(ystdex::bad_any_cast&)
94  {}
95  return false;
96 }
97 
99 File&
100 WriteNodeC(File& f, const ValueNode& node, size_t depth)
101 {
102  WritePrefix(f, depth);
103  f << node.GetName();
104  if(node)
105  {
106  f << ' ';
107  if(PrintNodeString(f, node))
108  return f;
109  f << '\n';
110  for(const auto& n : node)
111  {
112  WritePrefix(f, depth);
113  if(IsPrefixedIndex(n.GetName()))
114  PrintNodeString(f, n);
115  else
116  {
117  f << '(' << '\n';
118  try
119  {
120  WriteNodeC(f, n, depth + 1);
121  }
122  catch(std::out_of_range&)
123  {}
124  WritePrefix(f, depth);
125  f << ')' << '\n';
126  }
127  }
128  }
129  return f;
130 }
131 
132 } // unnamed namespace;
133 
134 File&
135 operator<<(File& f, const Configuration& conf)
136 {
137  return WriteNodeC(f, conf.GetRoot(), 0);
138 }
139 
140 TextFile&
142 {
143  tf.Rewind();
145  return tf;
146 }
147 
148 } // namespace NPL;
149 
会话:分析指定 NPL 代码。
Definition: SContext.h:54
YF_API string MakeEscape(const string &)
转义字符串:替换指定字符串中的可转义字符。
Definition: Lexical.cpp:191
设置:使用 S 表达式存储外部状态。
Definition: Configuration.h:49
动态泛型转换失败异常。
Definition: any.h:741
YF_API bool IsPrefixedIndex(const string &, char= '$')
判断字符串是否是一个指定字符和非负整数的组合。
Definition: ValueNode.cpp:107
_tString get_mid(const _tString &str, typename _tString::size_type l=1)
取删除前缀和后缀的子字符串。
Definition: string.hpp:256
YF_API ValueNode LoadNPLA1(ValueNode &&)
读取 NPLA1 翻译单元。
Definition: NPLA1.cpp:86
文本文件类。
Definition: TextFile.h:56
文件基类。
Definition: File.h:43
YF_API void Analyze(ValueNode &, const TokenList &)
分析指定源,取抽象语法树储存至指定值类型节点。
Definition: SContext.cpp:96
TextFile & operator>>(TextFile &tf, Configuration &conf)
YF_API char CheckLiteral(const string &)
检查指定字符串是否为字面量。
Definition: Lexical.cpp:173
值类型节点。
Definition: ValueNode.h:45
void Rewind() const
设置文件读位置为文本区段头。
Definition: TextFile.cpp:173
_tWidget _fCallable && f
Definition: ywgtevt.h:597
Container Value const string name size_t GetSize() const ynothrow
Definition: ValueNode.cpp:47
File & operator<<(File &f, const Configuration &conf)