src/tnetstring.nim
author Mahlon E. Smith <mahlon@martini.nu>
Wed, 27 Oct 2021 10:09:51 -0700
changeset 12 959dc81335c9
parent 9 c109bf5f2aa4
permissions -rw-r--r--
Updates for more recent (v1.6.0) nim.
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
#
12
959dc81335c9 Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
     2
# Copyright (c) 2015-2021, Mahlon E. Smith <mahlon@martini.nu>
0
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
type 
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   101
  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
   102
    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
   103
    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
   104
    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
   105
    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
   106
    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
   107
    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
   108
    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
   109
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   110
  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
   111
  TNetstringNodeObj* {.acyclic.} = object
5
d3804875d828 Expose the 'extra' attribute publicly.
Mahlon E. Smith <mahlon@martini.nu>
parents: 4
diff changeset
   112
      extra*: string
0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   113
      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
   114
      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
   115
          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
   116
      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
   117
          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
   118
      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
   119
          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
   120
      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
   121
          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
   122
      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
   123
          nil
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 TNetstringObject:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   125
          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
   126
      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
   127
          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
   128
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   129
  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
   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
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   132
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
   133
  ## 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
   134
  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
   135
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   136
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   137
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
   138
    ## Create a new String typed TNetstringNode.
12
959dc81335c9 Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   139
    result = TNetstringNode( kind: TNetstringString, str: s )
0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   140
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   141
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   142
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
   143
    ## Create a new Integer typed TNetstringNode.
12
959dc81335c9 Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   144
    result = TNetstringNode( kind: TNetstringInt, num: i )
0
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
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   147
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
   148
    ## Create a new Float typed TNetstringNode.
12
959dc81335c9 Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   149
    result = TNetstringNode( kind: TNetstringFloat, fnum: f )
0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   150
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
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
   153
    ## Create a new Boolean typed TNetstringNode.
12
959dc81335c9 Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   154
    result = TNetstringNode( kind: TNetstringBool, bval: b )
0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   155
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   156
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   157
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
   158
    ## Create a new nil typed TNetstringNode.
12
959dc81335c9 Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   159
    result = TNetstringNode( kind: TNetstringNull )
0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   160
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   161
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   162
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
   163
    ## Create a new Object typed TNetstringNode.
12
959dc81335c9 Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   164
    result = TNetstringNode( kind: TNetstringObject, fields: @[] )
0
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 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
   168
    ## Create a new Array typed TNetstringNode.
12
959dc81335c9 Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   169
    result = TNetstringNode( kind: TNetstringArray, elems: @[] )
0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   170
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   171
4
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   172
proc getStr*( node: TNetstringNode, default: string = "" ): string =
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   173
    ## Retrieves the string value of a `TNetstringString TNetstringNodee`.
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   174
    ## Returns ``default`` if ``node`` is not a ``TNetstringString``.
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   175
    if node.kind != TNetstringString: return default
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   176
    return node.str
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   177
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   178
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   179
proc getInt*( node: TNetstringNode, default: BiggestInt = 0 ): BiggestInt =
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   180
    ## Retrieves the int value of a `TNetstringInt TNetstringNode`.
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   181
    ## Returns ``default`` if ``node`` is not a ``TNetstringInt``.
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   182
    if node.kind != TNetstringInt: return default
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   183
    return node.num
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   184
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   185
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   186
proc getFloat*( node: TNetstringNode, default: float = 0.0 ): float =
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   187
    ## Retrieves the float value of a `TNetstringFloat TNetstringNode`.
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   188
    ## Returns ``default`` if ``node`` is not a ``TNetstringFloat``.
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   189
    if node.kind != TNetstringFloat: return default
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   190
    return node.fnum
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   191
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   192
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   193
proc getBool*( node: TNetstringNode, default: bool = false ): bool =
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   194
    ## Retrieves the bool value of a `TNetstringBool TNetstringNode`.
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   195
    ## Returns ``default`` if ``node`` is not a ``TNetstringBool``.
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   196
    if node.kind != TNetstringBool: return default
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   197
    return node.bval
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   198
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   199
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   200
proc getFields*( node: TNetstringNode,
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   201
    default: seq[tuple[key: string, val: TNetstringNode]] = @[] ):
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   202
        seq[tuple[key: string, val: TNetstringNode]] =
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   203
    ## Retrieves the key, value pairs of a `TNetstringObject TNetstringNode`.
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   204
    ## Returns ``default`` if ``node`` is not a ``TNetstringObject``.
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   205
    if node.kind != TNetstringObject: return default
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   206
    return node.fields
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   207
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   208
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   209
proc getElems*( node: TNetstringNode, default: seq[TNetstringNode] = @[] ): seq[TNetstringNode] =
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   210
    ## Retrieves the values of a `TNetstringArray TNetstringNode`.
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   211
    ## Returns ``default`` if ``node`` is not a ``TNetstringArray``.
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   212
    if node.kind != TNetstringArray: return default
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   213
    return node.elems
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   214
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   215
0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   216
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
   217
    ## 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
   218
    var
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   219
        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
   220
        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
   221
        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
   222
        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
   223
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   224
    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
   225
    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
   226
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   227
    try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   228
        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
   229
        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
   230
        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
   231
        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
   232
12
959dc81335c9 Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   233
    except ValueError, IndexDefect:
0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   234
        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
   235
        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
   236
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   237
    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
   238
        of ',':
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   239
            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
   240
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   241
        of '#':
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   242
            try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   243
                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
   244
            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
   245
                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
   246
                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
   247
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   248
        of '^':
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   249
            try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   250
                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
   251
            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
   252
                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
   253
                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
   254
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   255
        of '!':
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   256
            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
   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
        of '~':
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   259
            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
   260
            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
   261
            
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   262
        of ']':
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   263
            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
   264
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   265
            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
   266
            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
   267
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   268
            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
   269
                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
   270
                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
   271
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   272
        of '}':
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   273
            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
   274
            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
   275
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   276
            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
   277
            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
   278
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   279
            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
   280
            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
   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
            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
   283
                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
   284
                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
   285
                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
   286
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   287
                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
   288
                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
   289
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   290
        else:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   291
            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
   292
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   293
    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
   294
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
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
   297
    ## 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
   298
    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
   299
    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
   300
        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
   301
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   302
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   303
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
   304
    ## 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
   305
    ## modified.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   306
    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
   307
    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
   308
        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
   309
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   310
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   311
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
   312
    ## 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
   313
    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
   314
    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
   315
        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
   316
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   317
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   318
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
   319
    ## 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
   320
    ## 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
   321
    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
   322
    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
   323
        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
   324
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   325
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   326
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
   327
    ## 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
   328
    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
   329
    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
   330
        $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
   331
    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
   332
        $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
   333
    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
   334
        $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
   335
    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
   336
        $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
   337
    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
   338
        "(nil)"
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   339
    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
   340
        $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
   341
    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
   342
        $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
   343
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   344
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   345
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
   346
    ## 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
   347
    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
   348
        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
   349
        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
   350
    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
   351
        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
   352
    else:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   353
        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
   354
        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
   355
            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
   356
        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
   357
            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
   358
        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
   359
            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
   360
        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
   361
            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
   362
        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
   363
            true
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   364
        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
   365
            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
   366
        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
   367
            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
   368
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   369
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   370
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
   371
    ## Perform a deep copy of TNetstringNode.
12
959dc81335c9 Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   372
    result = TNetstringNode( kind: node.kind, extra: node.extra )
0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   373
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   374
    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
   375
    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
   376
        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
   377
    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
   378
        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
   379
    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
   380
        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
   381
    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
   382
        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
   383
    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
   384
        discard
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 TNetstringArray:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   386
        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
   387
        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
   388
            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
   389
    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
   390
        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
   391
        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
   392
            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
   393
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   394
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   395
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
   396
    ## 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
   397
    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
   398
    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
   399
        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
   400
            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
   401
            return
12
959dc81335c9 Updates for more recent (v1.6.0) nim.
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   402
    raise newException( IndexDefect, "key not in object" )
0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   403
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   404
7
370805b49c92 Remove deprecated THash type.
Mahlon E. Smith <mahlon@martini.nu>
parents: 5
diff changeset
   405
proc hash*( node: TNetstringNode ): 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
   406
    ## 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
   407
    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
   408
    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
   409
        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
   410
    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
   411
        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
   412
    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
   413
        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
   414
    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
   415
        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
   416
    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
   417
        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
   418
    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
   419
        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
   420
    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
   421
        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
   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 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
   425
    ## 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
   426
    ## 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
   427
    ## 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
   428
    ## 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
   429
    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
   430
    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
   431
        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
   432
    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
   433
        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
   434
    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
   435
        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
   436
    else:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   437
        0
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
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   440
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
   441
    ## 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
   442
    ## 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
   443
    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
   444
    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
   445
    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
   446
        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
   447
            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
   448
    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
   449
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   450
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   451
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
   452
    ## 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
   453
    ## 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
   454
    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
   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
    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
   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 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
   460
    ## 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
   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 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
   463
        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
   464
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   465
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   466
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
   467
    ## 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
   468
    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
   469
    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
   470
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   471
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   472
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
   473
    ## 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
   474
    ## 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
   475
    ## (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
   476
    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
   477
    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
   478
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   479
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   480
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
   481
    ## 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
   482
    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
   483
    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
   484
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   485
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   486
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
   487
    ## 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
   488
    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
   489
    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
   490
        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
   491
            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
   492
            return
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   493
    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
   494
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
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
   497
    ## 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
   498
    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
   499
    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
   500
        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
   501
    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
   502
        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
   503
        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
   504
    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
   505
        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
   506
        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
   507
    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
   508
        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
   509
    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
   510
        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
   511
    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
   512
        result = ""
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   513
        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
   514
            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
   515
        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
   516
    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
   517
        result = ""
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   518
        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
   519
            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
   520
            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
   521
        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
   522
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   523
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   524
#
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   525
# Tests!
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   526
#
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   527
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
   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
    # 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
   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
    try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   532
        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
   533
    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
   534
        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
   535
    try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   536
        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
   537
    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
   538
        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
   539
    try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   540
        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
   541
    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
   542
        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
   543
    try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   544
        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
   545
    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
   546
        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
   547
    try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   548
        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
   549
    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
   550
        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
   551
    try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   552
        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
   553
    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
   554
        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
   555
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   556
    # Equality
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   557
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   558
    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
   559
    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
   560
    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
   561
    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
   562
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   563
    # 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
   564
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   565
    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
   566
    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
   567
    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
   568
    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
   569
    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
   570
    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
   571
    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
   572
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   573
    # 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
   574
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   575
    var
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   576
        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
   577
        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
   578
        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
   579
        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
   580
    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
   581
        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
   582
        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
   583
        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
   584
            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
   585
            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
   586
    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
   587
    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
   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
    # 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
   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
    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
   592
    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
   593
    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
   594
    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
   595
    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
   596
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   597
    # 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
   598
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   599
    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
   600
    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
   601
    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
   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
    # Hashing
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   604
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   605
    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
   606
    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
   607
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   608
    # 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
   609
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   610
    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
   611
    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
   612
    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
   613
    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
   614
    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
   615
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   616
    # 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
   617
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   618
    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
   619
    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
   620
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   621
    # 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
   622
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   623
    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
   624
    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
   625
    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
   626
    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
   627
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   628
    # 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
   629
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   630
    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
   631
    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
   632
        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
   633
        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
   634
    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
   635
    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
   636
    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
   637
    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
   638
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   639
    # 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
   640
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   641
    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
   642
    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
   643
    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
   644
    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
   645
    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
   646
    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
   647
    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
   648
    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
   649
    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
   650
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   651
    # Serialization.
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   652
    #
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   653
    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
   654
        "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
   655
        "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
   656
        "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
   657
    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
   658
    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
   659
4
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   660
    # Value fetching methods
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   661
    #
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   662
    var tnet_null = newTNetstringNull()
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   663
    tnet_obj = newTNetstringString( "Hello." )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   664
    doAssert( tnet_obj.getStr == "Hello." )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   665
    doAssert( tnet_null.getStr("nope") == "nope" )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   666
    doAssert( tnet_null.getStr == "" )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   667
    tnet_obj = newTNetstringInt( 42 )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   668
    doAssert( tnet_obj.getInt == 42 )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   669
    doAssert( tnet_null.getInt == 0 )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   670
    doAssert( tnet_null.getInt(1) == 1 )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   671
    tnet_obj = newTNetstringFloat( 1.0 )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   672
    doAssert( tnet_obj.getFloat == 1.0 )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   673
    doAssert( tnet_null.getFloat == 0 )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   674
    doAssert( tnet_null.getFloat(0.1) == 0.1 )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   675
    tnet_obj = newTNetstringObject()
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   676
    tnet_obj[ "yay" ] = newTNetstringInt( 1 )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   677
    doAssert( tnet_obj.getFields[0].val == newTNetstringInt(1) )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   678
    doAssert( tnet_null.getFields.len == 0 )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   679
    tnet_obj = newTNetstringArray()
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   680
    tnet_obj.add( newTNetstringInt(1) )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   681
    doAssert( tnet_obj.getElems[0] == newTNetstringInt(1) )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   682
    doAssert( tnet_null.getElems.len == 0 )
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   683
800be124db98 Add value getters for the various TNetstringNode types.
Mahlon E. Smith <mahlon@martini.nu>
parents: 1
diff changeset
   684
0
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   685
    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
   686
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   687
    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
   688
        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
   689
            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
   690
            try:
755c3645e3dc Initial commit of a TNetstring parser and serializer for the Nim programming language.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   691
                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
   692
                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
   693
                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
   694
            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
   695
                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
   696