TOML v0.1.0
Tom 的顯而易見、極簡語言。
作者 Tom Preston-Werner。
請注意,此規格仍在大量變動中。在標示為 1.0 之前,您應假設它不穩定並採取相應措施。
目標
TOML 旨在成為一個極簡的設定檔格式,由於語意明顯而易於閱讀。TOML 設計為明確對應到雜湊表。TOML 應易於解析為各種語言中的資料結構。
規格
- TOML 區分大小寫。
- 空白表示 tab (0x09) 或空白 (0x20)。
註解
用井號符號表達您的想法。它們從符號延伸到該行的結尾。
# I am a comment. Hear me roar. Roar.
key = "value" # Yeah, you can do this.
字串
ProTip™:您可能會注意到此規格與 JSON 的字串定義相同,但 TOML 需要 UTF-8 編碼。這是故意的。
字串是單行值,用引號包圍。字串只能包含有效的 UTF-8 字元。可以使用任何 Unicode 字元,但必須跳脫下列字元:引號、反斜線和控制字元(U+0000 至 U+001F)。
"I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF."
為了方便,一些熱門字元有簡潔的跳脫序列。
\b - backspace (U+0008)
\t - tab (U+0009)
\n - linefeed (U+000A)
\f - form feed (U+000C)
\r - carriage return (U+000D)
\" - quote (U+0022)
\/ - slash (U+002F)
\\ - backslash (U+005C)
\uXXXX - unicode (U+XXXX)
任何 Unicode 字元都可以用 \uXXXX
形式跳脫。
其他特殊字元是保留字,如果使用,TOML 應產生錯誤。這表示 Windows 上的路徑永遠必須使用雙反斜線。
wrong = "C:\Users\nodejs\templates" # note: doesn't produce a valid path
right = "C:\\Users\\nodejs\\templates"
對於二進位資料,建議您使用 Base 64 或其他適當的編碼。該編碼的處理方式將取決於應用程式。
整數
整數是單獨存在的裸數字。覺得負面嗎?做自然的事。預期 64 位元最小大小。
42
-17
浮點數
浮點數是數字,中間有一個點。小數點兩側必須至少有一個數字。預期 64 位元(雙精度)精度。
3.1415
-0.01
布林值
布林值就是您習慣的符號。永遠是小寫。
true
false
日期時間
日期時間是 ISO8601 日期,但僅允許完整的 Zulu 形式。
1979-05-27T07:32:00Z
陣列
陣列是方括號,裡面有其他基本類型。空白會被忽略。元素以逗號分隔。不,您不能混合資料類型,那很愚蠢。
[ 1, 2, 3 ]
[ "red", "yellow", "green" ]
[ [ 1, 2 ], [3, 4, 5] ]
[ [ 1, 2 ], ["a", "b", "c"] ] # this is ok
[ 1, 2.0 ] # note: this is NOT ok
陣列也可以是多行的。因此,除了忽略空白之外,陣列也會忽略方括號之間的新行。終止逗號可以在閉合方括號之前。
key = [
1, 2, 3
]
key = [
1,
2, # this is ok
]
雜湊
有兩種方法可以建立金鑰。我稱它們為「金鑰群組」和「金鑰」。兩者都是常規金鑰,但金鑰群組只有一個雜湊作為其值。
金鑰群組出現在方括號中,在單獨一行上。您可以將它們與陣列區分開來,因為陣列永遠只會是值。
[keygroup]
在該項目下方,直到下一個鍵或 EOF 為該鍵組的鍵/值。鍵位於等號符號的左側,值位於右側。鍵從第一個非空白字元開始,並在等號符號之前的最後一個非空白字元結束。鍵組內的鍵/值對是無序的。
[keygroup]
key = "value"
您可以根據需要縮排鍵及其值。使用 Tab 或空白。盡情發揮。您可能會問為什麼?因為您可以擁有巢狀雜湊。輕而易舉。
巢狀雜湊由包含點的鍵組表示。您可以隨意命名您的鍵組,但請勿使用點。點是保留字。遵守規則。
[key.tater]
type = "pug"
在 JSON 領域中,這將為您提供以下結構。
{ "key": { "tater": { "type": "pug" } } }
如果您不希望指定所有超鍵,則不必指定。TOML 知道如何為您執行此操作。
# [x] you
# [x.y] don't
# [x.y.z] need these
[x.y.z.w] # for this to work
轉換為雜湊表時,空鍵組應導致鍵的值為空雜湊表。
小心不要覆寫先前的鍵。那很愚蠢。而且應該會產生錯誤。
# DO NOT WANT
[fruit]
type = "apple"
[fruit.type]
apple = "yes"
認真嗎?
沒錯。
但為什麼?
因為我們需要一個體面的可讀格式,該格式映射到雜湊,而 YAML 規格長達 80 頁,而且會讓我抓狂。不,JSON 不算。您知道為什麼。
天啊,你說得對
沒錯。想幫忙嗎?發送拉取請求。或撰寫解析器。勇敢一點。
實作
如果您有實作,請發送拉取請求以新增到此清單。請在自述檔案中註明您的解析器支援的提交 SHA1 或版本標籤。
- C#/.NET - https://github.com/LBreedlove/Toml.net
- C#/.NET - https://github.com/rossipedia/toml-net
- C#/.NET - https://github.com/RichardVasquez/TomlDotNet
- C (@ajwans) - https://github.com/ajwans/libtoml
- C++ (@evilncrazy) - https://github.com/evilncrazy/ctoml
- Clojure (@lantiga) - https://github.com/lantiga/clj-toml
- Clojure (@manicolosi) - https://github.com/manicolosi/clojoml
- CoffeeScript (@biilmann) - https://github.com/biilmann/coffee-toml
- Erlang - https://github.com/kalta/etoml.git
- Erlang - https://github.com/kaos/tomle
- Go (@thompelletier) - https://github.com/pelletier/go-toml
- Go (@laurent22) - https://github.com/laurent22/toml-go
- Go w/ Reflection (@BurntSushi) - https://github.com/BurntSushi/toml
- Haskell (@seliopou) - https://github.com/seliopou/toml
- Haxe (@raincole) https://github.com/raincole/haxetoml
- Java (@agrison) - https://github.com/agrison/jtoml
- Java (@johnlcox) - https://github.com/johnlcox/toml4j
- Java (@mwanji) - https://github.com/mwanji/toml4j
- Java - https://github.com/asafh/jtoml
- Java w/ ANTLR (@MatthiasSchuetz) - https://github.com/mschuetz/toml
- Julia (@pygy) - https://github.com/pygy/TOML.jl
- Literate CoffeeScript (@JonathanAbrams) - https://github.com/JonAbrams/tomljs
- node.js - https://github.com/aaronblohowiak/toml
- node.js/browser - https://github.com/ricardobeat/toml.js (npm install tomljs)
- node.js - https://github.com/BinaryMuse/toml-node
- node.js (@redhotvengeance) - https://github.com/redhotvengeance/topl (topl npm package)
- node.js/browser (@alexanderbeletsky) - https://github.com/alexanderbeletsky/toml-js (npm browser amd)
- Objective C (@mneorr) - https://github.com/mneorr/toml-objc.git
- Objective-C (@SteveStreza) - https://github.com/amazingsyco/TOML
- Ocaml (@mackwic) https://github.com/mackwic/to.ml
- Perl (@alexkalderimis) - https://github.com/alexkalderimis/config-toml.pl
- Perl - https://github.com/dlc/toml
- PHP (@leonelquinteros) - https://github.com/leonelquinteros/php-toml.git
- PHP (@jimbomoss) - https://github.com/jamesmoss/toml
- PHP (@coop182) - https://github.com/coop182/toml-php
- PHP (@checkdomain) - https://github.com/checkdomain/toml
- PHP (@zidizei) - https://github.com/zidizei/toml-php
- Python (@socketubs) - https://github.com/socketubs/pytoml
- Python (@f03lipe) - https://github.com/f03lipe/toml-python
- Python (@uiri) - https://github.com/uiri/toml
- Python - https://github.com/bryant/pytoml
- Python (@elssar) - https://github.com/elssar/tomlgun
- Python (@marksteve) - https://github.com/marksteve/toml-ply
- Ruby (@jm) - https://github.com/jm/toml (toml gem)
- Ruby (@eMancu) - https://github.com/eMancu/toml-rb (toml-rb gem)
- Ruby (@charliesome) - https://github.com/charliesome/toml2 (toml2 gem)
- Ruby (@sandeepravi) - https://github.com/sandeepravi/tomlp (tomlp gem)
- Scala - https://github.com/axelarge/tomelette
驗證器
- Go (@BurntSushi) - https://github.com/BurntSushi/toml/tree/master/tomlv
TOML 解析器語言不可知測試套件
- toml-test (@BurntSushi) - https://github.com/BurntSushi/toml-test
編輯器支援
- Emacs (@dryman) - https://github.com/dryman/toml-mode.el
- Sublime Text 2 (@lmno) - https://github.com/lmno/TOML
- TextMate (@infininight) - https://github.com/textmate/toml.tmbundle
- Vim (@cespare) - https://github.com/cespare/vim-toml
編碼器
- PHP (@ayushchd) - https://github.com/ayushchd/php-toml-encoder