about summary refs log tree commit diff stats
path: root/src/config/toml.nim
blob: 03c17af51119334f1ec9e0460d62d0600a414376 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import streams
import tables
import times
import strutils
import strformat
import unicode

import utils/twtstr

type
  ValueType* = enum
    VALUE_STRING, VALUE_INTEGER, VALUE_FLOAT, VALUE_BOOLEAN, VALUE_DATE_TIME,
    VALUE_TABLE, VALUE_ARRAY VALUE_TABLE_ARRAY

  SyntaxError = object of ValueError

  TomlParser = object
    at: int
    line: int
    stream: Stream
    buf: string
    root: TomlTable
    node: TomlNode
    currkey: seq[string]

  TomlValue* = ref object
    case vt*: ValueType
    of VALUE_STRING:
      s*: string
    of VALUE_INTEGER:
      i*: int64
    of VALUE_FLOAT:
      f*: float64
    of VALUE_BOOLEAN:
      b*: bool
    of VALUE_TABLE:
      t*: TomlTable
    of VALUE_DATE_TIME:
      dt*: DateTime
    of VALUE_ARRAY:
      a*: seq[TomlValue]
    of VALUE_TABLE_ARRAY:
      ta*: seq[TomlTable]

  TomlNode = ref object of RootObj
    comment: string

  TomlKVPair = ref object of TomlNode
    key*: seq[string]
    value*: TomlValue

  TomlTable = ref object of TomlNode
    key: seq[string]
    nodes: seq[TomlNode]
    map: Table[string, TomlValue]

func `[]`*(val: TomlValue, key: string): TomlValue =
  return val.t.map[key]

iterator pairs*(val: TomlValue): (string, TomlValue) =
  for k, v in val.t.map.pairs:
    yield (k, v)

func contains*(val: TomlValue, key: string): bool =
  return key in val.t.map

func isBare(c: char): bool =
  return c == '-' or c == '_' or c.isAlphaNumeric()

func peek(state: TomlParser, i: int): char =
  return state.buf[state.at + i]

func peek(state: TomlParser, i: int, len: int): string =
  return state.buf.substr(state.at + i, state.at + i + len)

proc syntaxError(state: TomlParser, msg: string) =
  raise newException(SyntaxError, fmt"on line {state.line}: {msg}")

proc valueError(state: TomlParser, msg: string) =
  raise newException(ValueError, fmt"on line {state.line}: {msg}")

proc consume(state: var TomlParser): char =
  result = state.buf[state.at]
  inc state.at

proc reconsume(state: var TomlParser) =
  dec state.at

proc has(state: var TomlParser, i: int = 0): bool =
  if state.at + i >= state.buf.len and not state.stream.atEnd():
    state.buf &= state.stream.readLine() & '\n'
  return state.at + i < state.buf.len

proc consumeEscape(state: var TomlParser, c: char): Rune =
  var len = 4
  if c == 'U':
    len = 8
  let c = state.consume()
  var num = hexValue(c)
  if num != -1:
    var i = 0
    while state.has() and i < len:
      let c = state.peek(0)
      if hexValue(c) == -1:
        break
      discard state.consume()
      num *= 0x10
      num += hexValue(c)
      inc i
    if i != len - 1:
      state.syntaxError(fmt"invalid escaped length ({i}, needs {len})")
    if num > 0x10FFFF or num in {0xD800..0xDFFF}:
      state.syntaxError(fmt"invalid escaped codepoint {num}")
    else:
      return Rune(num)
  else:
    state.syntaxError(fmt"invalid escaped codepoint {c}")

proc consumeString(state: var TomlParser, first: char): string =
  var multiline = false

  if first == '"':
    if state.has(1):
      let s = state.peek(0, 1)
      if s == "\"\"":
        multiline = true
  elif first == '\'':
    if state.has(1):
      let s = state.peek(0, 1)
      if s == "''":
        multiline = true

  if multiline:
    let c = state.peek(0)
    if c == '\n':
      discard state.consume()

  var escape = false
  var ml_trim = false
  while state.has():
    let c = state.consume()
    if c == '\n' and not multiline:
      state.syntaxError(fmt"newline in string")
    elif c == first:
      if multiline and state.has(1):
        let c2 = state.peek(0)
        let c3 = state.peek(1)
        if c2 == first and c3 == first:
          discard state.consume()
          discard state.consume()
          break
      else:
        break
    elif first == '"' and c == '\\':
      escape = true
    elif escape:
      case c
      of 'b': result &= '\b'
      of 't': result &= '\t'
      of 'n': result &= '\n'
      of 'f': result &= '\f'
      of 'r': result &= '\r'
      of '"': result &= '"'
      of '\\': result &= '\\'
      of 'u', 'U': result &= state.consumeEscape(c)
      of '\n': ml_trim = true
      else: state.syntaxError(fmt"invalid escape sequence \{c}")
      escape = false
    elif ml_trim:
      if not (c in {'\n', ' ', '\t'}):
        result &= c
        ml_trim = false
    else:
      result &= c

proc consumeBare(state: var TomlParser, c: char): string =
  result &= c
  while state.has():
    let c = state.consume()
    case c
    of ' ', '\t': break
    of '.', '=', ']', '\n':
      state.reconsume()
      break
    elif c.isBare():
      result &= c
    else:
      state.syntaxError(fmt"invalid value in token: {c}")

proc flushLine(state: var TomlParser) =
  if state.node != nil:
    if state.node of TomlKVPair:
      var i = 0
      let keys = state.currkey & TomlKVPair(state.node).key
      var table = state.root
      while i < keys.len - 1:
        if keys[i] in table.map:
          let node = table.map[keys[i]]
          if node.vt != VALUE_TABLE:
            let s = keys.join(".")
            state.valueError(fmt"re-definition of node {s}")
          else:
            table = node.t
        else:
          let node = TomlTable()
          table.map[keys[i]] = TomlValue(vt: VALUE_TABLE, t: node)
          table = node
        inc i

      if keys[i] in table.map:
        let s = keys.join(".")
        state.valueError(fmt"re-definition of node {s}")

      table.map[keys[i]] = TomlKVPair(state.node).value
      table.nodes.add(state.node)
    state.node = nil
  inc state.line

proc consumeComment(state: var TomlParser) =
  state.node = TomlNode()
  while state.has():
    let c = state.consume()
    if c == '\n':
      state.reconsume()
      break
    else:
      state.node.comment &= c

proc consumeKey(state: var TomlParser): seq[string] =
  var str = ""
  while state.has():
    let c = state.consume()
    case c
    of '"', '\'':
      if str.len > 0:
        state.syntaxError("multiple strings without dot")
      str = state.consumeString(c)
    of '=', ']':
      if str.len != 0:
        result.add(str)
        str = ""
      return result
    of '.':
      if str.len == 0: #TODO empty strings are allowed, only empty keys aren't
        state.syntaxError("redundant dot")
      else:
        result.add(str)
        str = ""
    of ' ', '\t': discard
    of '\n':
      if state.node != nil:
        state.syntaxError("newline without value")
      else:
        state.flushLine()
    elif c.isBare():
      if str.len > 0:
        state.syntaxError(fmt"multiple strings without dot: {str}")
      str = state.consumeBare(c)
    else: state.syntaxError(fmt"invalid character in key: {c}")

  state.syntaxError("key without value")

proc consumeTable(state: var TomlParser): TomlTable =
  new(result)
  while state.has():
    let c = state.peek(0)
    case c
    of ' ', '\t': discard
    of '\n':
      return result
    of '[':
      #TODO table array
      state.syntaxError("arrays of tables are not supported yet")
    of '"', '\'':
      result.key = state.consumeKey()
    elif c.isBare():
      result.key = state.consumeKey()
    else: state.syntaxError(fmt"invalid character before key: {c}")
  state.syntaxError("unexpected end of file")

proc consumeNoState(state: var TomlParser): bool =
  while state.has():
    let c = state.peek(0)
    case c
    of '#', '\n':
      return false
    of ' ', '\t': discard
    of '[':
      discard state.consume()
      let table = state.consumeTable()
      state.currkey = table.key
      state.node = table
      return false
    elif c == '"' or c == '\'' or c.isBare():
      let kvpair = TomlKVPair()
      kvpair.key = state.consumeKey()
      state.node = kvpair
      return true
    else: state.syntaxError(fmt"invalid character before key: {c}")
  state.syntaxError("unexpected end of file")

proc consumeNumber(state: var TomlParser): TomlValue =
  var repr: string
  var isfloat = false
  if state.has():
    if state.peek(0) == '+' or state.peek(0) == '-':
      repr &= state.consume()

  while state.has() and isDigit(state.peek(0)):
    repr &= state.consume()

  if state.has(1):
    if state.peek(0) == '.' and isDigit(state.peek(1)):
      repr &= state.consume()
      repr &= state.consume()
      isfloat = true
      while state.has() and isDigit(state.peek(0)):
        repr &= state.consume()

  if state.has(1):
    if state.peek(0) == 'E' or state.peek(0) == 'e':
      var j = 2
      if state.peek(1) == '-' or state.peek(1) == '+':
        inc j
      if state.has(j) and isDigit(state.peek(j)):
        while j > 0:
          repr &= state.consume()
          dec j

        while state.has() and isDigit(state.peek(0)):
          repr &= state.consume()

  if isfloat:
    let val = parseFloat64(repr)
    return TomlValue(vt: VALUE_FLOAT, f: val)

  let val = parseInt64(repr)
  return TomlValue(vt: VALUE_INTEGER, i: val)

proc consumeValue(state: var TomlParser): TomlValue

proc consumeArray(state: var TomlParser): TomlValue =
  result = TomlValue(vt: VALUE_ARRAY)
  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 val == nil:
        state.syntaxError("comma without element")
      result.a.add(val)
    else:
      state.reconsume()
      val = state.consumeValue()

proc consumeValue(state: var TomlParser): TomlValue =
  while state.has():
    let c = state.consume()
    case c
    of '"', '\'':
      return TomlValue(vt: VALUE_STRING, s: state.consumeString(c))
    of ' ', '\t': discard
    of '\n':
      state.syntaxError("newline without value")
    of '#':
      state.syntaxError("comment without value")
    of '+', '-', '0'..'9':
      return state.consumeNumber()
      #TODO date-time
    of '[':
      return state.consumeArray()
    elif c.isBare():
      let s = state.consumeBare(c)
      case s
      of "true": return TomlValue(vt: VALUE_BOOLEAN, b: true)
      of "false": return TomlValue(vt: VALUE_BOOLEAN, b: false)
      else: state.syntaxError(fmt"invalid token {s}")
    else:
      state.syntaxError(fmt"invalid character in value: {c}")

proc parseToml*(inputStream: Stream): TomlValue =
  var state: TomlParser
  state.stream = inputStream
  state.line = 1
  state.root = TomlTable()

  while state.has():
    if state.consumeNoState():
      let kvpair = TomlKVPair(state.node)
      kvpair.value = state.consumeValue()

    while state.has():
      let c = state.consume()
      case c
      of '\n':
        state.flushLine()
        break
      of '#':
        state.consumeComment()
      of '\t', ' ': discard
      else: state.syntaxError(fmt"invalid character after value: {c}")

  return TomlValue(vt: VALUE_TABLE, t: state.root)