tnetstring.nim
author Mahlon E. Smith <mahlon@martini.nu>
Tue, 19 May 2015 12:55:18 -0700
changeset 1 87a0365b799b
parent 0 755c3645e3dc
child 4 800be124db98
permissions -rw-r--r--
Remove unnecessary object creation.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
#
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     2
# Copyright (c) 2015, Mahlon E. Smith <mahlon@martini.nu>
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
# All rights reserved.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
# Redistribution and use in source and binary forms, with or without
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
# modification, are permitted provided that the following conditions are met:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
#
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
#     * Redistributions of source code must retain the above copyright
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
#       notice, this list of conditions and the following disclaimer.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
#
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
#     * Redistributions in binary form must reproduce the above copyright
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
#       notice, this list of conditions and the following disclaimer in the
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
#       documentation and/or other materials provided with the distribution.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
#
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
#     * Neither the name of Mahlon E. Smith nor the names of his
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
#       contributors may be used to endorse or promote products derived
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
#       from this software without specific prior written permission.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
#
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
## This module implements a simple TNetstring parser and serializer.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
## TNetString stands for "tagged netstring" and is a modification of Dan
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
## Bernstein's netstrings specification.  TNetstrings allow for the same data
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
## structures as JSON but in a format that is resistant to buffer overflows
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
## and backward compatible with original netstrings.  They make no assumptions
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
## about string contents, allowing for easy transmission of binary data mixed
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    35
## with strongly typed values.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
## See http://cr.yp.to/proto/netstrings.txt and http://tnetstrings.org/ for additional information.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
##
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
## This module borrows heavily (in both usage and code) from the nim JSON stdlib
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
## (json.nim) -- (c) Copyright 2015 Andreas Rumpf, Dominik Picheta.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
## 
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
## Usage example:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
##
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
## .. code-block:: nim
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    45
##
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    46
##   let
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    47
##       tnetstr = "52:4:test,3:1.3^4:key2,4:true!6:things,12:1:1#1:2#1:3#]}"
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
##       tnetobj = parse_tnetstring( tnetstr )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
##
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
##   # tnetobj is now equivalent to the structure:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
##   # @[(key: test, val: 1.3), (key: key2, val: true), (key: things, val: @[1, 2, 3])]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
##
1
87a0365b799b Remove unnecessary object creation.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    53
##   assert( tnetobj.kind == TNetstringObject )
0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
##   echo tnetobj[ "test" ]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
##   echo tnetobj[ "key2" ]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
##   for item in tnetobj[ "things" ]:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
##       echo item
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    58
##   
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
## Results in:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    60
##
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    61
## .. code-block:: nim
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
##
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
##   1.3
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
##   true
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
##   1
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
##   2
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
##   3
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
##
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    69
## This module can also be used to reasonably create a serialized
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    70
## TNetstring, suitable for network transmission:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    71
##
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    72
## .. code-block:: nim
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    73
##    
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    74
##    let
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    75
##        number  = 1000
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    76
##        list    = @[ "thing1", "thing2" ]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77
##        tnettop = newTNetstringArray() # top-level array
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    78
##        tnetsub = newTNetstringArray() # sub array
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    79
##    
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    80
##    tnettop.add( newTNetstringInt(number) )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    81
##    for item in list:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    82
##        tnetsub.add( newTNetstringString(item) )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    83
##    tnettop.add( tnetsub )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    84
##    
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    85
##    # Equivalent to: @[1000, @[thing1, thing2]]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    86
##    echo dump_tnetstring( tnettop )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    87
##
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    88
## Results in:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    89
##
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    90
## .. code-block:: nim
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    91
##    
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    92
##    29:4:1000#18:6:thing1,6:thing2,]]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    93
##
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    94
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    95
import
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    96
    hashes,
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    97
    parseutils,
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    98
    strutils
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    99
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   100
const version = "0.1.0"
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   101
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   102
type 
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   103
  TNetstringKind* = enum     ## enumeration of all valid types
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   104
    TNetstringString,        ## a string literal
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   105
    TNetstringInt,           ## an integer literal
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   106
    TNetstringFloat,         ## a float literal
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   107
    TNetstringBool,          ## a ``true`` or ``false`` value
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   108
    TNetstringNull,          ## the value ``null``
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   109
    TNetstringObject,        ## an object: the ``}`` token
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   110
    TNetstringArray          ## an array: the ``]`` token
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   111
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   112
  TNetstringNode* = ref TNetstringNodeObj
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   113
  TNetstringNodeObj* {.acyclic.} = object
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   114
      extra: string
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   115
      case kind*: TNetstringKind
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   116
      of TNetstringString:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   117
          str*: string
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   118
      of TNetstringInt:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   119
          num*: BiggestInt
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   120
      of TNetstringFloat:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   121
          fnum*: float
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   122
      of TNetstringBool:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   123
          bval*: bool
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   124
      of TNetstringNull:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   125
          nil
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   126
      of TNetstringObject:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   127
          fields*: seq[ tuple[key: string, val: TNetstringNode] ]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   128
      of TNetstringArray:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   129
          elems*: seq[ TNetstringNode ]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   130
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   131
  TNetstringParseError* = object of ValueError ## Raised for a TNetstring error
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   132
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   133
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   134
proc raiseParseErr*( t: TNetstringNode, msg: string ) {.noinline, noreturn.} =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   135
  ## Raises a `TNetstringParseError` exception.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   136
  raise newException( TNetstringParseError, msg )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   137
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   138
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   139
proc newTNetstringString*( s: string ): TNetstringNode =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   140
    ## Create a new String typed TNetstringNode.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   141
    new( result )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   142
    result.kind = TNetstringString
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   143
    result.str = s
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   144
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   145
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   146
proc newTNetstringInt*( i: BiggestInt ): TNetstringNode =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   147
    ## Create a new Integer typed TNetstringNode.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   148
    new( result )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   149
    result.kind = TNetstringInt
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   150
    result.num = i
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   151
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   152
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   153
proc newTNetstringFloat*( f: float ): TNetstringNode =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   154
    ## Create a new Float typed TNetstringNode.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   155
    new( result )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   156
    result.kind = TNetstringFloat
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   157
    result.fnum = f
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   158
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   159
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   160
proc newTNetstringBool*( b: bool ): TNetstringNode =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   161
    ## Create a new Boolean typed TNetstringNode.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   162
    new( result )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   163
    result.kind = TNetstringBool
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   164
    result.bval = b
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   165
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   166
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   167
proc newTNetstringNull*(): TNetstringNode =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   168
    ## Create a new nil typed TNetstringNode.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   169
    new( result )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   170
    result.kind = TNetstringNull
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   171
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   172
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   173
proc newTNetstringObject*(): TNetstringNode =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   174
    ## Create a new Object typed TNetstringNode.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   175
    new( result )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   176
    result.kind = TNetstringObject
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   177
    result.fields = @[]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   178
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   179
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   180
proc newTNetstringArray*(): TNetstringNode =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   181
    ## Create a new Array typed TNetstringNode.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   182
    new( result )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   183
    result.kind = TNetstringArray
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   184
    result.elems = @[]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   185
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   186
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   187
proc parse_tnetstring*( data: string ): TNetstringNode =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   188
    ## Given an encoded tnetstring, parse and return a TNetstringNode.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   189
    var
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   190
        length:  int
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   191
        kind:    char
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   192
        payload: string
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   193
        extra:   string
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   194
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   195
    let sep_pos = data.skipUntil( ':' )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   196
    if sep_pos == data.len: raiseParseErr( result, "Invalid data: No separator token found." )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   197
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   198
    try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   199
        length       = data[ 0 .. sep_pos - 1 ].parseInt
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   200
        kind         = data[ sep_pos + length + 1 ]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   201
        payload      = data[ sep_pos + 1 .. sep_pos + length ]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   202
        extra        = data[ sep_pos + length + 2 .. ^1 ]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   203
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   204
    except ValueError, IndexError:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   205
        var msg = getCurrentExceptionMsg()
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   206
        raiseParseErr( result, msg )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   207
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   208
    case kind:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   209
        of ',':
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   210
            result = newTNetstringString( payload )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   211
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   212
        of '#':
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   213
            try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   214
                result = newTNetstringInt( payload.parseBiggestInt )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   215
            except ValueError:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   216
                var msg = getCurrentExceptionMsg()
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   217
                raiseParseErr( result, msg )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   218
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   219
        of '^':
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   220
            try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   221
                result = newTNetstringFloat( payload.parseFloat )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   222
            except ValueError:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   223
                var msg = getCurrentExceptionMsg()
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   224
                raiseParseErr( result, msg )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   225
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   226
        of '!':
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   227
            result = newTNetstringBool( payload == "true" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   228
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   229
        of '~':
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   230
            if length != 0: raiseParseErr( result, "Invalid data: Payload must be 0 length for null." )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   231
            result = newTNetstringNull()
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   232
            
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   233
        of ']':
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   234
            result = newTNetstringArray()
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   235
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   236
            var subnode = parse_tnetstring( payload )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   237
            result.elems.add( subnode )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   238
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   239
            while subnode.extra != "":
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   240
                subnode = parse_tnetstring( subnode.extra )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   241
                result.elems.add( subnode )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   242
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   243
        of '}':
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   244
            result = newTNetstringObject()
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   245
            var key = parse_tnetstring( payload )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   246
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   247
            if ( key.extra == "" ): raiseParseErr( result, "Invalid data: Unbalanced tuple." )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   248
            if ( key.kind != TNetstringString ): raiseParseErr( result, "Invalid data: Object keys must be strings." )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   249
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   250
            var value = parse_tnetstring( key.extra )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   251
            result.fields.add( (key: key.str, val: value) )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   252
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   253
            while value.extra != "":
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   254
                var subkey = parse_tnetstring( value.extra )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   255
                if ( subkey.extra == "" ): raiseParseErr( result, "Invalid data: Unbalanced tuple." )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   256
                if ( subkey.kind != TNetstringString ): raiseParseErr( result, "Invalid data: Object keys must be strings." )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   257
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   258
                value = parse_tnetstring( subkey.extra )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   259
                result.fields.add( (key: subkey.str, val: value) )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   260
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   261
        else:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   262
            raiseParseErr( result, "Invalid data: Unknown tnetstring type '$1'." % $kind )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   263
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   264
    result.extra = extra
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   265
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   266
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   267
iterator items*( node: TNetstringNode ): TNetstringNode =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   268
    ## Iterator for the items of `node`. `node` has to be a TNetstringArray.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   269
    assert node.kind == TNetstringArray
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   270
    for i in items( node.elems ):
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   271
        yield i
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   272
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   273
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   274
iterator mitems*( node: var TNetstringNode ): var TNetstringNode =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   275
    ## Iterator for the items of `node`. `node` has to be a TNetstringArray. Items can be
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   276
    ## modified.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   277
    assert node.kind == TNetstringArray
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   278
    for i in mitems( node.elems ):
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   279
        yield i
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   280
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   281
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   282
iterator pairs*( node: TNetstringNode ): tuple[ key: string, val: TNetstringNode ] =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   283
    ## Iterator for the child elements of `node`. `node` has to be a TNetstringObject.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   284
    assert node.kind == TNetstringObject
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   285
    for key, val in items( node.fields ):
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   286
        yield ( key, val )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   287
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   288
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   289
iterator mpairs*( node: var TNetstringNode ): var tuple[ key: string, val: TNetstringNode ] =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   290
    ## Iterator for the child elements of `node`. `node` has to be a TNetstringObject.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   291
    ## Items can be modified.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   292
    assert node.kind == TNetstringObject
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   293
    for keyVal in mitems( node.fields ):
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   294
        yield keyVal
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   295
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   296
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   297
proc `$`*( node: TNetstringNode ): string =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   298
    ## Delegate stringification of `TNetstringNode` to its underlying object.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   299
    return case node.kind:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   300
    of TNetstringString:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   301
        $node.str
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   302
    of TNetstringInt:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   303
        $node.num
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   304
    of TNetstringFloat:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   305
        $node.fnum
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   306
    of TNetstringBool:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   307
        $node.bval
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   308
    of TNetstringNull:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   309
        "(nil)"
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   310
    of TNetstringArray:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   311
        $node.elems
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   312
    of TNetstringObject:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   313
        $node.fields
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   314
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   315
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   316
proc `==`* ( a, b: TNetstringNode ): bool =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   317
    ## Check two TNetstring nodes for equality.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   318
    if a.isNil:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   319
        if b.isNil: return true
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   320
        return false
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   321
    elif b.isNil or a.kind != b.kind:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   322
        return false
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   323
    else:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   324
        return case a.kind
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   325
        of TNetstringString:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   326
            a.str == b.str
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   327
        of TNetstringInt:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   328
            a.num == b.num
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   329
        of TNetstringFloat:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   330
            a.fnum == b.fnum
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   331
        of TNetstringBool:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   332
            a.bval == b.bval
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   333
        of TNetstringNull:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   334
            true
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   335
        of TNetstringArray:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   336
            a.elems == b.elems
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   337
        of TNetstringObject:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   338
            a.fields == b.fields
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   339
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   340
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   341
proc copy*( node: TNetstringNode ): TNetstringNode =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   342
    ## Perform a deep copy of TNetstringNode.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   343
    new( result )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   344
    result.kind  = node.kind
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   345
    result.extra = node.extra
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   346
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   347
    case node.kind
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   348
    of TNetstringString:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   349
        result.str = node.str
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   350
    of TNetstringInt:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   351
        result.num = node.num
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   352
    of TNetstringFloat:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   353
        result.fnum = node.fnum
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   354
    of TNetstringBool:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   355
        result.bval = node.bval
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   356
    of TNetstringNull:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   357
        discard
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   358
    of TNetstringArray:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   359
        result.elems = @[]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   360
        for item in items( node ):
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   361
            result.elems.add( copy(item) )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   362
    of TNetstringObject:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   363
        result.fields = @[]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   364
        for key, value in items( node.fields ):
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   365
            result.fields.add( (key, copy(value)) )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   366
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   367
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   368
proc delete*( node: TNetstringNode, key: string ) =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   369
    ## Deletes ``node[key]`` preserving the order of the other (key, value)-pairs.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   370
    assert( node.kind == TNetstringObject )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   371
    for i in 0..node.fields.len - 1:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   372
        if node.fields[i].key == key:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   373
            node.fields.delete( i )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   374
            return
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   375
    raise newException( IndexError, "key not in object" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   376
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   377
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   378
proc hash*( node: TNetstringNode ): THash =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   379
    ## Compute the hash for a TNetstringString node
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   380
    return case node.kind
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   381
    of TNetstringString:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   382
        hash( node.str )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   383
    of TNetstringInt:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   384
        hash( node.num )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   385
    of TNetstringFloat:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   386
        hash( node.fnum )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   387
    of TNetstringBool:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   388
        hash( node.bval.int )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   389
    of TNetstringNull:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   390
        hash( 0 )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   391
    of TNetstringArray:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   392
        hash( node.elems )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   393
    of TNetstringObject:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   394
        hash( node.fields )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   395
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   396
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   397
proc len*( node: TNetstringNode ): int =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   398
    ## If `node` is a `TNetstringArray`, it returns the number of elements.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   399
    ## If `node` is a `TNetstringObject`, it returns the number of pairs.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   400
    ## If `node` is a `TNetstringString`, it returns strlen.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   401
    ## Else it returns 0.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   402
    return case node.kind
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   403
    of TNetstringString:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   404
        node.str.len
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   405
    of TNetstringArray:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   406
        node.elems.len
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   407
    of TNetstringObject:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   408
        node.fields.len
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   409
    else:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   410
        0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   411
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   412
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   413
proc `[]`*( node: TNetstringNode, name: string ): TNetstringNode =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   414
    ## Gets a field from a `TNetstringNode`, which must not be nil.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   415
    ## If the value at `name` does not exist, returns nil
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   416
    assert( not isNil(node) )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   417
    assert( node.kind == TNetstringObject )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   418
    for key, item in node:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   419
        if key == name:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   420
            return item
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   421
    return nil
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   422
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   423
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   424
proc `[]`*( node: TNetstringNode, index: int ): TNetstringNode =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   425
    ## Gets the node at `index` in an Array. Result is undefined if `index`
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   426
    ## is out of bounds.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   427
    assert( not isNil(node) )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   428
    assert( node.kind == TNetstringArray )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   429
    return node.elems[ index ]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   430
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   431
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   432
proc hasKey*( node: TNetstringNode, key: string ): bool =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   433
    ## Checks if `key` exists in `node`.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   434
    assert( node.kind == TNetstringObject )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   435
    for k, item in items( node.fields ):
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   436
        if k == key: return true
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   437
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   438
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   439
proc add*( parent, child: TNetstringNode ) =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   440
    ## Appends `child` to a TNetstringArray node `parent`.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   441
    assert( parent.kind == TNetstringArray )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   442
    parent.elems.add( child )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   443
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   444
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   445
proc add*( node: TNetstringNode, key: string, val: TNetstringNode ) =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   446
    ## Adds ``(key, val)`` pair to the TNetstringObject `node`.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   447
    ## For speed reasons no check for duplicate keys is performed.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   448
    ## (Note, ``[]=`` performs the check.)
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   449
    assert( node.kind == TNetstringObject )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   450
    node.fields.add( (key, val) )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   451
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   452
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   453
proc `[]=`*( node: TNetstringNode, index: int, val: TNetstringNode ) =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   454
    ## Sets an index for a `TNetstringArray`.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   455
    assert( node.kind == TNetstringArray )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   456
    node.elems[ index ] = val
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   457
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   458
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   459
proc `[]=`*( node: TNetstringNode, key: string, val: TNetstringNode ) =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   460
    ## Sets a field from a `TNetstringObject`. Performs a check for duplicate keys.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   461
    assert( node.kind == TNetstringObject )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   462
    for i in 0 .. node.fields.len - 1:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   463
        if node.fields[i].key == key:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   464
            node.fields[i].val = val
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   465
            return
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   466
    node.fields.add( (key, val) )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   467
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   468
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   469
proc dump_tnetstring*( node: TNetstringNode ): string =
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   470
    ## Renders a TNetstring `node` as a regular string.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   471
    case node.kind
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   472
    of TNetstringString:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   473
        result = $( node.str.len ) & ':' & node.str & ','
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   474
    of TNetstringInt:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   475
        let str = $( node.num )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   476
        result = $( str.len ) & ':' & str & '#'
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   477
    of TNetstringFloat:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   478
        let str = $( node.fnum )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   479
        result = $( str.len ) & ':' & str & '^'
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   480
    of TNetstringBool:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   481
        result = if node.bval: "4:true!" else: "5:false!"
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   482
    of TNetstringNull:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   483
        result = "0:~"
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   484
    of TNetstringArray:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   485
        result = ""
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   486
        for n in node.items:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   487
            result = result & n.dump_tnetstring
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   488
        result = $( result.len ) & ':' & result & ']'
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   489
    of TNetstringObject:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   490
        result = ""
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   491
        for key, val in node.pairs:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   492
            result = result & $( key.len ) & ':' & key & ',' # key
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   493
            result = result & val.dump_tnetstring            # val
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   494
        result = $( result.len ) & ':' & result & '}'
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   495
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   496
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   497
#
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   498
# Tests!
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   499
#
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   500
when isMainModule:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   501
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   502
    # Expected exceptions
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   503
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   504
    try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   505
        discard parse_tnetstring( "totally invalid" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   506
    except TNetstringParseError:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   507
        doAssert( true, "invalid tnetstring" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   508
    try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   509
        discard parse_tnetstring( "what:ever" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   510
    except TNetstringParseError:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   511
        doAssert( true, "bad length" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   512
    try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   513
        discard parse_tnetstring( "3:yep~" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   514
    except TNetstringParseError:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   515
        doAssert( true, "null w/ > 0 length" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   516
    try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   517
        discard parse_tnetstring( "8:1:1#1:1#}" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   518
    except TNetstringParseError:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   519
        doAssert( true, "hash with non-string key" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   520
    try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   521
        discard parse_tnetstring( "7:4:test,}" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   522
    except TNetstringParseError:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   523
        doAssert( true, "hash with odd number of elements" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   524
    try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   525
        discard parse_tnetstring( "2:25*" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   526
    except TNetstringParseError:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   527
        doAssert( true, "unknown netstring tag" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   528
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   529
    # Equality
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   530
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   531
    let tnet_int = parse_tnetstring( "1:1#" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   532
    doAssert( tnet_int == tnet_int )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   533
    doAssert( tnet_int == parse_tnetstring( "1:1#" ) )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   534
    doAssert( parse_tnetstring( "0:~" ) == parse_tnetstring( "0:~" ) )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   535
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   536
    # Type detection
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   537
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   538
    doAssert( tnet_int.kind == TNetstringInt )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   539
    doAssert( parse_tnetstring( "1:a," ).kind         == TNetstringString )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   540
    doAssert( parse_tnetstring( "3:1.0^" ).kind       == TNetstringFloat )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   541
    doAssert( parse_tnetstring( "5:false!" ).kind     == TNetstringBool )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   542
    doAssert( parse_tnetstring( "0:~" ).kind          == TNetstringNull )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   543
    doAssert( parse_tnetstring( "9:2:hi,1:1#}" ).kind == TNetstringObject )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   544
    doAssert( parse_tnetstring( "8:1:1#1:2#]" ).kind  == TNetstringArray )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   545
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   546
    # Iteration (both array and tuple)
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   547
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   548
    var
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   549
        keys: array[ 2, string ]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   550
        vals: array[ 4, string ]
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   551
        k_idx = 0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   552
        idx = 0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   553
    for key, val in parse_tnetstring( "35:2:hi,8:1:a,1:b,]5:there,8:1:c,1:d,]}" ):
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   554
        keys[ idx ] = key
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   555
        idx = idx + 1
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   556
        for item in val:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   557
            vals[ k_idx ] = item.str
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   558
            k_idx = k_idx + 1
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   559
    doAssert( keys == ["hi","there"] )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   560
    doassert( vals == ["a","b","c","d"] )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   561
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   562
    # Deep copies
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   563
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   564
    var original = parse_tnetstring( "35:2:hi,8:1:a,1:b,]5:there,8:1:c,1:d,]}" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   565
    var copied   = original.copy
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   566
    doAssert( original == copied )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   567
    doAssert( original.repr != copied.repr )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   568
    doAssert( original.fields.pop.val.elems.pop.repr != copied.fields.pop.val.elems.pop.repr )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   569
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   570
    # Key deletion
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   571
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   572
    var tnet_obj = parse_tnetstring( "35:2:hi,8:1:a,1:b,]5:there,8:1:c,1:d,]}" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   573
    tnet_obj.delete( "hi" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   574
    doAssert( tnet_obj.fields.len == 1 )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   575
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   576
    # Hashing
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   577
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   578
    doAssert( tnet_int.hash == 1.hash )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   579
    doAssert( parse_tnetstring( "4:true!" ).hash == hash( true.int ) )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   580
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   581
    # Length checks.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   582
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   583
    tnet_obj = parse_tnetstring( "35:2:hi,8:1:a,1:b,]5:there,8:1:c,1:d,]}" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   584
    doAssert( parse_tnetstring( "0:~" ).len == 0 )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   585
    doAssert( tnet_obj.len == 2 )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   586
    doAssert( parse_tnetstring( "8:1:1#1:2#]" ).len == 2 )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   587
    doAssert( parse_tnetstring( "5:hallo," ).len == 5 )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   588
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   589
    # Index accessors
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   590
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   591
    tnet_obj = parse_tnetstring( "20:1:1#1:2#1:3#1:4#1:5#]" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   592
    doAssert( tnet_obj[ 2 ].num == 3 )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   593
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   594
    # Key accessors
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   595
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   596
    tnet_obj = parse_tnetstring( "11:2:hi,3:yep,}" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   597
    doAssert( $tnet_obj["hi"] == "yep" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   598
    doAssert( tnet_obj.has_key( "hi" ) == true )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   599
    doAssert( tnet_obj.has_key( "nope-not-here" ) == false )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   600
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   601
    # Adding elements to an existing TNetstring array
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   602
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   603
    var tnet_array = newTNetstringArray()
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   604
    for i in 1 .. 10:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   605
        tnet_obj = newTNetstringInt( i )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   606
        tnet_array.add( tnet_obj )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   607
    tnet_array[ 6 ] = newTNetstringString( "yep" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   608
    doAssert( tnet_array.len == 10 )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   609
    doAssert( tnet_array[ 4 ].num == 5 )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   610
    doAssert( tnet_array[ 6 ].str == "yep" )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   611
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   612
    # Adding pairs to an existing TNetstring aobject.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   613
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   614
    tnet_obj = newTNetstringObject()
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   615
    tnet_obj.add( "yo", newTNetstringInt(1) )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   616
    tnet_obj.add( "yep", newTNetstringInt(2) )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   617
    doAssert( tnet_obj["yo"].num == 1 )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   618
    doAssert( tnet_obj["yep"].num == 2 )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   619
    doAssert( tnet_obj.len == 2 )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   620
    tnet_obj[ "more" ] = newTNetstringInt(1)
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   621
    tnet_obj[ "yo" ] = newTNetstringInt(1) # dup check
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   622
    doAssert( tnet_obj.len == 3 )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   623
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   624
    # Serialization.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   625
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   626
    var tstr = "308:9:givenName,6:Mahlon,16:departmentNumber,22:Information Technology," &
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   627
        "5:title,19:Senior Technologist,13:accountConfig,48:7:vmemail,4:true!7:allpage," &
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   628
        "5:false!7:galhide,0:~}13:homeDirectory,14:/home/m/mahlon,3:uid,6:mahlon,9:yubi" &
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   629
        "KeyId,12:vvidhghkhehj,5:gecos,12:Mahlon Smith,2:sn,5:Smith,14:employeeNumber,5:12921#}"
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   630
    tnet_obj = parse_tnetstring( tstr )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   631
    doAssert( tstr == tnet_obj.dump_tnetstring )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   632
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   633
    echo "* Tests passed!"
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   634
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   635
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   636
    while true and defined( testing ):
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   637
        for line in readline( stdin ).split_lines:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   638
            let input = line.strip
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   639
            try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   640
                var tnetstring = parse_tnetstring( input )
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   641
                echo "  parsed     --> ", tnetstring
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   642
                echo "  serialized --> ", tnetstring.dump_tnetstring, "\n"
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   643
            except TNetstringParseError:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   644
                echo input, " --> ", getCurrentExceptionMsg()
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   645