5252# # nest parsers. If you need values from the outer parser you can add parameters
5353# # to the inner parser by giving it colon expressions before the body (e.g the
5454# # call ``createParser(list, size: uint16)`` would create a parser
55- # # ``proc list (stream: Stream, size: uint16): <return type>``). To call a parser
55+ # # ``proc (stream: Stream, size: uint16): <return type>``). To call a parser
5656# # use the ``*`` type as described above and give it the name of the parser and
5757# # any optional arguments. The stream object will get added automatically as the
5858# # first parameter.
5959# #
60+ # # When creating a parser you get a tuple with two members, ``get`` and ``put``
61+ # # which is stored by a let as the identifier given when calling createParser.
62+ # # These are both procedures, the first only takes a stream (and any optional
63+ # # arguments as described above) and returns a tuple containing all the fields.
64+ # # The second takes a stream and a tuple containing all the fields, this is the
65+ # # same tuple returned by the ``get`` procedure and writes the format to the
66+ # # stream.
67+ # #
6068# # Example:
6169# # In lieu of proper examples the binaryparse.nim file contains a ``when
6270# # isMainModule()`` block showcasing how it can be used. The table below
@@ -151,16 +159,15 @@ proc decodeType(t: NimNode, stream: NimNode, seenFields: seq[string]):
151159 of nnkCall:
152160 var
153161 t0 = t[0 ]
154- customProcRead = newCall (nnkDotExpr.newTree (t[0 ], newIdentNode (" read " )), stream)
155- customProcWrite = newCall (nnkDotExpr.newTree (t[0 ], newIdentNode (" writes " )), stream)
162+ customProcRead = newCall (nnkDotExpr.newTree (t[0 ], newIdentNode (" get " )), stream)
163+ customProcWrite = newCall (nnkDotExpr.newTree (t[0 ], newIdentNode (" put " )), stream)
156164 retType = quote do :
157165 typeGetter (`t0`)
158166 i = 1
159167 while i < t.len:
160168 customProcRead.add (t[i])
161169 inc i
162170 customProcRead.replace (seenFields)
163- echo t.treeRepr
164171 return (size: BiggestInt (0 ), kind: retType, customReader: customProcRead, customWriter: customProcWrite)
165172 else :
166173 raise newException (AssertionError ,
@@ -326,9 +333,12 @@ proc createWriteStatement(
326333
327334
328335macro createParser * (name: untyped , paramsAndDef: varargs [untyped ]): untyped =
329- # # The main macro in this module. It takes the ``name`` of the procedure to
336+ # # The main macro in this module. It takes the ``name`` of the tuple to
330337 # # create along with a block on the format described above and creates a
331- # # parser for it on the form ``proc <name>(Stream): tuple[<fields>]``
338+ # # reader and a writer for it. The output is a tuple with ``name`` that has
339+ # # two fields ``get`` and ``put``. Get is on the form
340+ # # ``proc (stream: Stream): tuple[<fields>]`` and put is
341+ # # ``proc (stream: Stream, input: tuple[<fields>])``
332342 let
333343 body = paramsAndDef[^ 1 ]
334344 res = newIdentNode (" result" )
@@ -583,6 +593,7 @@ macro createParser*(name: untyped, paramsAndDef: varargs[untyped]): untyped =
583593 else :
584594 discard
585595 #[
596+ # This can be used for debugging, should possibly be exposed by a flag
586597 inner.add(quote do:
587598 when `field` >= 0:
588599 echo "Done reading field " & $`i` & ": " & $`res`[`field`]
@@ -600,13 +611,12 @@ macro createParser*(name: untyped, paramsAndDef: varargs[untyped]): untyped =
600611 `inner`
601612 proc `writerName` (`stream`: Stream , `input`: var `tupleMeat`) =
602613 `writer`
603- let `name` = (read : `readerName`, writes : `writerName`)
614+ let `name` = (get : `readerName`, put : `writerName`)
604615 for p in extraParams:
605616 result [0 ][3 ].add p
606617
607618 echo result .toStrLit
608619 # echo result.treeRepr
609- # echo writer.toStrLit
610620
611621when isMainModule :
612622 createParser (list, size: uint16 ):
@@ -629,12 +639,12 @@ when isMainModule:
629639 var fs = newFileStream (" data.hex" , fmRead)
630640 defer : fs.close ()
631641 if not fs.isNil:
632- var data = myParser.read (fs)
642+ var data = myParser.get (fs)
633643 echo data.size
634644 echo data.data
635645 echo data.str
636646 echo data.inner.data
637647 var fs2 = newFileStream (" out.hex" , fmWrite)
638648 defer : fs2.close ()
639649 if not fs2.isNil:
640- myParser.writes (fs2, data)
650+ myParser.put (fs2, data)
0 commit comments