日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

TCL腳本數(shù)據(jù)文件格式( 六 )


}proc Sources {sourceList} {
global curPattern
global a_patterns
# We store the codes such as "SYST:99" in a global array.
# My implementation uses regular expressions to extract the source tag
# and the page number from such a code (not shown here).
set a_patterns($curPattern,sources) $sourceList
}proc Info {info} {
global curPattern
global a_patterns
set a_patterns($curPattern,info) $info
}猛一看,這個(gè)程序比我們?cè)谙鄬?duì)簡(jiǎn)單的繪圖例子所做的要多很多.但考慮到這個(gè)方法的功能,只用幾個(gè)分析過程并靈活運(yùn)用命令"uplevel",我們同樣可以分析包含有復(fù)雜結(jié)構(gòu),注釋,嵌套子結(jié)構(gòu)和自由格式文本數(shù)據(jù)的數(shù)據(jù)文件.設(shè)想一下如果我們從頭寫這樣一個(gè)分析器會(huì)有多難.
數(shù)據(jù)由Source,Pattern或Info等過程進(jìn)行解析.解析后的數(shù)據(jù)在內(nèi)部存儲(chǔ)在三個(gè)列表和三個(gè)數(shù)組中.數(shù)據(jù)的嵌套由調(diào)用uplevel來進(jìn)行處理,用變量curPattern來記住我們當(dāng)前所在的位置.
要注意的是這種方法需要你的數(shù)據(jù)能夠理解TCL語(yǔ)法.這意味著大括號(hào)應(yīng)該放在一行的最后,而不是下一行的開頭.
▲遞歸結(jié)構(gòu)
在倉(cāng)庫(kù)的樣例中,Pattern類型的結(jié)構(gòu)包含有其他類型的子結(jié)構(gòu)如Info和Sources.那么當(dāng)一個(gè)結(jié)構(gòu)包含有相同類型的子結(jié)構(gòu)時(shí)會(huì)如何呢?換句話說,我們?nèi)绾翁幚磉f歸結(jié)構(gòu)?
例如,你要描述一個(gè)面向?qū)ο笙到y(tǒng)的設(shè)計(jì),該設(shè)計(jì)由遞歸子系統(tǒng)實(shí)現(xiàn).
example6/datafile.dat
# Description of an object-oriented video game
System VideoGame {
System Maze {
System Walls {
Object WallGenerator
Object TextureMapper
}
System Monsters {
Object FightingEngine
Object MonsterManipulator
}
}
System Scores {
Object ScoreKeeper
}
}為跟蹤我們當(dāng)前處于哪一個(gè)System系統(tǒng)結(jié)構(gòu)中,看上去我們需要不只一個(gè)全局變量currPattern.在分析的任何時(shí)刻,我們都可能處在很多嵌套的System結(jié)構(gòu)中,因此我們需要兩個(gè)以上的變量.我們可能需要某種堆棧,在遇到System過程時(shí)壓入一個(gè)值,在過程的結(jié)束時(shí)再?gòu)棾鰜?我們用一個(gè)TCL列表可以構(gòu)造這樣一個(gè)棧.
但若你不想維護(hù)一個(gè)棧的話,也可以不用它.這種方法也是基于一個(gè)非常簡(jiǎn)單的建議:當(dāng)你需要使用一個(gè)棧時(shí),看一下能否使用函數(shù)調(diào)用棧.處理遞歸數(shù)據(jù)時(shí),我通常就用這個(gè)方法來實(shí)現(xiàn)我的分析過程的.
example6/parser.tcl
set currSystem ""proc System {name args} {
# Instead of pushing the new system on the "stack" of current systems,
# we remember it in a local variable, which ends up on TCL"s
# function call stack.
global currSystem
set tmpSystem $currSystem
set currSystem $name ; # Thanks to this, all sub-structures called by
# "uplevel" will know what the name of their
# immediate parent System is# Store the system in an internal data structure
# (details not shown here)
puts "Storing system $currSystem"# Execute the parsing procedures for the sub-systems
uplevel 1 [lindex $args end]# Pop the system off the "stack" again.
set currSystem $tmpSystem
}proc Object {name} {
global currSystem
# Store the object in the internal data structure of the current
# system (details not shown here)
puts "System $currSystem contains object $name"
}source "datafile.dat"與把嵌套的系統(tǒng)名存儲(chǔ)在一個(gè)棧中(該棧由TCL的列表或數(shù)組來模擬)不同,我們只把對(duì)象名存儲(chǔ)在一個(gè)名為tmpSystem的局部變量中.由于解析過程會(huì)由TCL依據(jù)棧中的順序自動(dòng)調(diào)用,我們無(wú)需再去顯式地壓入/彈出任何數(shù)據(jù)了.
▲其他例子
由Don Libes 寫的CGI庫(kù)使用主動(dòng)文件樣本來表達(dá)HTML文檔.這個(gè)想法是寫一個(gè)TCL腳本作為HTML文檔并為你生成純正的HTML文件.該文檔包含有核心列表,格式化文本和其他的HTML元素.分析過程調(diào)用uplevel處理遞歸子結(jié)構(gòu).
下面是Don的代碼的一部分,告訴你他是如何應(yīng)用本文所講述的技巧的.# Output preformatted text. This text must be surrounded by "" tags.

推薦閱讀