PMunch / binaryparse

Binary parser for Nim
MIT License
71 stars 6 forks source link

Make the parser tuple exported #5

Open singularperturbation opened 4 years ago

singularperturbation commented 4 years ago

Very minor change - I wanted to make sure that the (get, put) tuple was exported. I think I want to make the tuple type exportable as well. That way, it's easier to use this in a package.

Thought about adding a do_export flag in createParser, but that made things awkward. Had to specify do_export=true or do_export=false even when I had a default in the createParser macro.

singularperturbation commented 4 years ago

I also made the return type parsed and generated as a tuple so that the type can be used outside of the module:

createParser(frame_header, Header):
  u24: payload_length
  8: frame_type
  8: flags
  1: R = 0
  u31: stream_identifier

## Generates from -d:binaryparseEcho
type
  Header* = tuple[payload_length: uint32, frame_type: int8, flags: int8, R: int8,
                stream_identifier: uint32]
proc `:tmp`_258099(stream: Stream): Header =
  stream.readDataBE(result[0].addr, 3)
  result[0] = (result[0] shr 0) and 16777215
  stream.readDataBE(result[1].addr, 1)
  stream.readDataBE(result[2].addr, 1)
  stream.peekDataBE(result[3].addr, 1)
  result[3] = (result[3] shr 7) and 1
  if result[3] != 0:
    raise newException(MagicError, "Magic with size " &
        $1 & " didn\'t match value " &
        $0 & ", read: " &
        $result[3])
  stream.readDataBE(result[4].addr, 4)
  result[4] = (result[4] shr 0) and 2147483647

proc `:tmp`_258100(stream: Stream; input: var Header) =
  var :tmp_258066: int64 = 0
  stream.writeDataBE(input[0].addr, 3)
  stream.writeDataBE(input[1].addr, 1)
  stream.writeDataBE(input[2].addr, 1)
  :tmp_258066 = :tmp_258066 shl 24
  :tmp_258066 = :tmp_258066 or
      (input[4] and 2147483647).int64 shl 0
  stream.writeDataBE(:tmp_258066.addr, 4)
  :tmp_258066 = 0

let frame_header* = (get: :tmp_258099, put: :tmp_258100)