TOML

Monday, May 24, 2021

“TOML”是“Tom’s Obvious, Minimal Language“的缩写,文件扩展名为.toml, MIME类型为application/tomlTOML在线测试

基本语法

  • 大小写敏感
  • 必须使用 UTF-8 编码
  • 空白可以是制表符(0x09)或空格(0x20)
  • 换行可以是 LF(0x0A)或 CRLF(0x0D0A)
  • 注释使用#

键值对

  • 每行一个键值对(有例外,值可以跨多行)
  • 键名可以是裸露的,引号引起来的,或点分隔的
  • 裸键只能包含 A-Za-z0-9_-
  • 裸键和引号键等价,不可重名
bare_key = "value"
1234 = "value" #等价于 "1234" = "value"

"127.0.0.1" = "value"
"character encoding" = "value"
"ʎǝʞ" = "value"
'key2' = "value"
'quoted "value"' = "value"

name = "Orange"
physical.color = "orange"
physical.shape = "round"
site."google.com" = true

布尔值

bool1 = true
bool2 = false

整数

int1 = +99
int2 = 42
int3 = 0
int4 = -17

#可以用下划线增加可读性
int5 = 1_000
int6 = 5_349_221

# 带有 `0x` 前缀的十六进制
hex1 = 0xDEADBEEF
hex2 = 0xdeadbeef
hex3 = 0xdead_beef

# 带有 `0o` 前缀的八进制
oct1 = 0o01234567
oct2 = 0o755 # 对于表示 Unix 文件权限很有用

# 带有 `0b` 前缀的二进制
bin1 = 0b11010110

浮点数

# 小数
flt1 = +1.0
flt2 = 3.1415
flt3 = -0.01

# 指数
flt4 = 5e+22
flt5 = 1e06
flt6 = -2E-2

# 都有
flt7 = 6.626e-34

# 无穷
sf1 = inf  # 正无穷
sf2 = +inf # 正无穷
sf3 = -inf # 负无穷

# 非数
sf4 = nan  # 实际上对应信号非数码还是静默非数码,取决于实现
sf5 = +nan # 等同于 `nan`
sf6 = -nan # 有效,实际码取决于实现

时间

odt1 = 1979-05-27T07:32:00Z
odt2 = 1979-05-27T00:32:00-07:00
odt3 = 1979-05-27T00:32:00.999999-07:00
odt4 = 1979-05-27 07:32:00Z

ldt1 = 1979-05-27T07:32:00
ldt2 = 1979-05-27T00:32:00.999999

ld1 = 1979-05-27

lt1 = 07:32:00
lt2 = 00:32:00.999999

字符串

#任何 Unicode 字符都可以使用,除了那些必须转义的:引号,反斜杠,以及除制表符外的控制字符(U+0000 至 U+0008,U+000A 至 U+001F,U+007F)
str = "我是一个字符串。\"你可以把我引起来\"。姓名\tJos\u00E9\n位置\t旧金山。"
#转义序列
\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)
\\         - backslash       (U+005C)
\uXXXX     - unicode         (U+XXXX)
\UXXXXXXXX - unicode         (U+XXXXXXXX)
# 多行字符串
str1 = """
Roses are red
Violets are blue"""

# 在 Unix 系统,上面的多行字符串可能等同于:
str2 = "Roses are red\nViolets are blue"

# 在 Windows 系统,它可能等价于:
str3 = "Roses are red\r\nViolets are blue"

# 使用\避免多余的换行符
str4 = """\
       The quick brown \
       fox jumps over \
       the lazy dog.\
       """
# 使用单引号不会转义,所见即所得。
winpath  = 'C:\Users\nodejs\templates'
winpath2 = '\\ServerX\admin$\system32\'
quoted   = 'Tom "Dubs" Preston-Werner'
regex    = '<\i\c*\s*>'

# 多行写法
regex2 = '''I [dw]on't need \d{2} apples'''
lines  = '''
原始字符串中的
第一个换行被剔除了。
   所有其它空白
   都保留了。
'''

数组

integers = [ 1, 2, 3 ]
colors = [ "红", "黄", "绿" ]
nested_array_of_ints = [ [ 1, 2 ], [3, 4, 5] ]
nested_mixed_array = [ [ 1, 2 ], ["a", "b", "c"] ]
string_array = [ "所有的", '字符串', """是相同的""", '''类型''' ]

# 允许混合类型的数组
numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ]
contributors = [
  "Foo Bar <foo@example.com>",
  { name = "Baz Qux", email = "bazqux@example.com", url = "https://example.com/bazqux" }
]

# 可以跨行
hosts = [
  "alpha",
  "omega",
]

# 属于根表的数据必须在文件开头
name = "Fido"
breed = "pug"

[table-1]
key1 = "some string"
key2 = 123

[table-2]
key1 = "another string"
key2 = 456

# 可以直接定义子孙表
# [x] 你
# [x.y] 不
# [x.y.z] 需要这些
[x.y.z.w] # 来让这生效

[x] # 后置父表定义是可以的

# 不能重复定义
[fruit]
apple.color = "红"
apple.taste.sweet = true

# [fruit] # 非法
# [fruit.apple]  # 非法
# [fruit.apple.taste]  # 非法

[fruit.apple.texture]  # 你可以添加子表
smooth = true
# 内联形式的表
name = { first = "Tom", last = "Preston-Werner" }
point = { x = 1, y = 2 }
animal = { type.name = "pug" }

表数组

[[products]]
name = "Hammer"
sku = 738594937

[[products]]  # 数组里的空表

[[products]]
name = "Nail"
sku = 284758393
color = "gray"
[[fruits]]
name = "apple"

[fruits.physical]  # 子表
color = "red"
shape = "round"

[[fruits.varieties]]  # 嵌套表数组
name = "red delicious"

[[fruits.varieties]]
name = "granny smith"

[[fruits]]
name = "banana"

[[fruits.varieties]]
name = "plantain"
# 内联形式的表数组
points = [ { x = 1, y = 2, z = 3 },
           { x = 7, y = 8, z = 9 },
           { x = 2, y = 4, z = 8 } ]

参考:

memotoml

YAML