about summary refs log tree commit diff stats
path: root/src/config
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-12-13 02:03:47 +0100
committerbptato <nincsnevem662@gmail.com>2022-12-13 02:03:47 +0100
commit89750ef4ad621fe7fdce533d1265f3d970098a19 (patch)
tree0aa80ceb07ce0ad49e5c3ba369f96bb67c1b6dd5 /src/config
parentd9e430c8147c8c2d81b4ca5405786269b2cfc94d (diff)
downloadchawan-89750ef4ad621fe7fdce533d1265f3d970098a19.tar.gz
Update config and config docs
Diffstat (limited to 'src/config')
-rw-r--r--src/config/config.nim19
-rw-r--r--src/config/toml.nim57
2 files changed, 66 insertions, 10 deletions
diff --git a/src/config/config.nim b/src/config/config.nim
index 73072763..16e1c652 100644
--- a/src/config/config.nim
+++ b/src/config/config.nim
@@ -315,14 +315,17 @@ proc parseConfig(config: Config, dir: string, t: TomlValue) =
         assert conf.url.isSome != conf.host.isSome
         config.siteconf.add(conf)
     of "omnirule":
-      for v in v:
-        var rule = StaticOmniRule()
-        for k, v in v:
-          case k
-          of "match": rule.match = v.s
-          of "substitute-url": rule.subst = v.s
-        if rule.match != "":
-          config.omnirules.add(rule)
+      if v.vt == VALUE_ARRAY and v.a.len == 0:
+        config.omnirules.setLen(0)
+      else:
+        for v in v:
+          var rule = StaticOmniRule()
+          for k, v in v:
+            case k
+            of "match": rule.match = v.s
+            of "substitute": rule.subst = v.s
+          if rule.match != "":
+            config.omnirules.add(rule)
 
 proc parseConfig(config: Config, dir: string, stream: Stream) =
   config.parseConfig(dir, parseToml(stream))
diff --git a/src/config/toml.nim b/src/config/toml.nim
index 2c4550b5..89277d24 100644
--- a/src/config/toml.nim
+++ b/src/config/toml.nim
@@ -323,7 +323,10 @@ proc consumeNoState(state: var TomlParser): bool =
             node.map[table.key[i]] = TomlValue(vt: VALUE_TABLE, t: t2)
             node = t2
         if table.key[^1] in node.map:
-          let last = node.map[table.key[^1]]
+          var last = node.map[table.key[^1]]
+          if last.vt == VALUE_ARRAY and last.a.len == 0:
+            last = TomlValue(vt: VALUE_TABLE_ARRAY)
+            node.map[table.key[^1]] = last
           if last.vt != VALUE_TABLE_ARRAY:
             let key = table.key.join('.')
             state.valueError(fmt"re-definition of node {key} as table array (was {last.vt})")
@@ -396,11 +399,59 @@ proc consumeArray(state: var TomlParser): TomlValue =
     of ',':
       if val == nil:
         state.syntaxError("comma without element")
-      result.a.add(val)
+      if val.vt == VALUE_TABLE:
+        # inline table array
+        result = TomlValue(vt: VALUE_TABLE_ARRAY)
+        result.ta.add(val.t)
+      else:
+        result.a.add(val)
     else:
+      if val != nil:
+        state.syntaxError("missing comma")
       state.reconsume()
       val = state.consumeValue()
 
+proc consumeInlineTable(state: var TomlParser): TomlValue =
+  result = TomlValue(vt: VALUE_TABLE, t: TomlTable())
+  var key: seq[string]
+  var haskey: bool
+  var val: TomlValue
+  while state.has():
+    let c = state.consume()
+    case c
+    of ' ', '\t', '\n': discard
+    of '}':
+      if val != nil:
+        result.a.add(val)
+      break
+    of ',':
+      if key.len == 0:
+        state.syntaxError("missing key")
+      if val == nil:
+        state.syntaxError("comma without element")
+      var table = result.t
+      for i in 0 ..< key.high:
+        let k = key[i]
+        if k in table.map:
+          state.syntaxError(fmt"invalid re-definition of key {k}")
+        else:
+          let node = TomlTable()
+          table.map[k] = TomlValue(vt: VALUE_TABLE, t: node)
+          table = node
+      let k = key[^1]
+      if k in table.map:
+        state.syntaxError(fmt"invalid re-definition of key {k}")
+      table.map[k] = val
+    else:
+      if val != nil:
+        state.syntaxError("missing comma")
+      if not haskey:
+        key = state.consumeKey()
+        haskey = true
+      else:
+        state.reconsume()
+        val = state.consumeValue()
+
 proc consumeValue(state: var TomlParser): TomlValue =
   while state.has():
     let c = state.consume()
@@ -417,6 +468,8 @@ proc consumeValue(state: var TomlParser): TomlValue =
       #TODO date-time
     of '[':
       return state.consumeArray()
+    of '{':
+      return state.consumeInlineTable()
     elif c.isBare():
       let s = state.consumeBare(c)
       case s