Sources {SYST:263 GOF:207}
Info {
Communicate with a representative rather than with the
actual object.
}
}Pattern "FaCADe" {
Categories {"Access control" "Structural decomposition::object"}
Sources {GOF:185}
Level design
Info {
Group sub-interfaces into a single interface.
}
}Pattern "Counted Pointer" {
Categories {"Resource handling"}
Level idiom
Sources {SYST:353}
Info {
Reference counting prevents memory leaks.
}
}这仅是我最初编写的输入文件的一部分,但它还是包含了足够的数据来作为一个较好的例子.样本的说明很短,还有些笨拙,但对这个例子来说已经够了.
正如你看到的,这个数据文件几个新的特点:
▲数据被包含在一些结构中,用大括号{}加以分组.每个结构都由一个关键字开头.
这些结构可以嵌套,如:结构"Pattern"可以包含一个"Info"结构.
▲结构中的元素可以采用很多形式 。它们中的一些是标志符或字符串(比如元素"Level"),其他的看上去象是特殊的代码(如SYST:353),还有一些甚至是自由格式的文本(如在结构Category和Info中的那样).
▲每个结构中的元素的排列顺序是任意的.观察一下最后两个样本就会发现Level和Sources两个元素的顺序可以互换.所有元素实际上都可以按你想要的顺序排列.
▲数据文件包含有TCL注释语句,他们不仅可以在结构之间出现,甚至可以出现在结构内部.注释语句能让你的数据更易理解.
你可能会想这种格式比前面的例子复杂太多了,用TCL语言为其写一个分析器几乎是不可能的.可能看上去不太明了,我们还可以用主动文件样本来使此工作更加简单.分析(解析)过程比前面的更细而已,但肯定不是"复杂".
下面是我的分析如上数据文件的工具:#我们把数据保存在以下三个列表内:
set l_patterns [list]
set l_sources [list]
set l_categories [list]#我们还需要一个变量跟踪我们当前所在的Pattern结构
set curPattern ""# 下面是关键字"Source"的分析过程.
# 正如你所看到的,关键字后面跟有一个id号(是source的唯一标志符),
#还有source的说明文本.
proc Source {id info} {
# Remember that we saw this source.
global l_sources
lappend l_sources $curSource# Remember the info of this source in a global array.
global a_sources
set a_sources($curSource,info) $info
}# The parsing proc for the "Category" keyword is similar.
proc Category {id info} {
global l_categories
lappend l_categories $curCategoryglobal a_categories
set a_categories($curCategory,info) $info
}# This is the parsing proc for the "Pattern" keyword.
# Since a "Pattern" structure can contain sub-structures,
# we use "uplevel" to recursively handle those.
proc Pattern {name args} {
global curPattern
set curPattern $name ; # This will be used in the sub-structures
# which are parsed next
global l_patterns
lappend l_patterns $curPattern# We treat the final argument as a piece of TCL code.
# We execute that code in the caller"s scope, to parse the elements
# of the structure.
# "uplevel" will call "Categories", "Level" and other commands that
# handle the sub-structures.
# This is similar to how we use the "source" command to parse the entire
# data file.
uplevel 1 [lindex $args end]set curPattern ""
}# The parsing proc for one of the sub-structures. It is called
# by "uplevel" when the "Pattern" keyword is handled.
proc Categories {categoryList} {
global curPattern ; # We access the global variable "curPattern"
# to find out inside which structure we are.
global a_patterns
set a_patterns($curPattern,categories) $categoryList
}# The following parsing procs are for the other sub-structures
# of the Pattern structure.proc Level {level} {
global curPattern
global a_patterns
set a_patterns($curPattern,level) $level
推荐阅读
- 我TCL6198
- 王者清理游戏数据后果
- 我的 TCL768 一周使用经历
- FreeBSD 数据迁移方法
- MIUI11怎么备份数据
- 内交外换 明基P30数据交换能力初探
- 金融信用信息基础数据库是征信吗
- 10 FreeBSD连载:系统启动脚本
- 安装鸿蒙系统数据会丢失吗
- 关于W219的数据传输
