lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Tue, Jul 16, 2013, at 11:50 AM, Sean Conner wrote:
> It was thus said that the Great Todd Coram once stated:
> > Erlang's bit syntax is a joy to work with and I miss it every time I use
> > another programming language to decode protocols.
> > In particular, I like the inline binding to variables as opposed to the
> > regex/scanf like approach taken by other languages.
> > 
> > An efficient analogue (to Erlang's bit syntax) in Lua would be
> > tremendously useful.
> > 
> > Lua tables would work great for something like this.  :)
>   A good test for this would be DNS packets.  The header portion isn't
>   that
> bad:
> 	id:16,
> 	query:1,	-- boolean, 0=query, 1=response
> 	opcode:4,	-- type of query
> 	aa:1,		-- boolean, authoritative answer
> 	tc:1,		-- boolean, truncation
> 	rd:1,		-- boolean, recursion desired
> 	ra:1,		-- boolean, recursion available
> 	z:1,		-- not used, must be 0
> 	ad:1,		-- boolean, authentic bit
> 	cd:1,		-- boolean, checking disabled
> 	rcode:4,	-- return code
> 	qcount:16,	-- query count
> 	ancount:16,	-- answer count
> 	nscount:16,	-- nameserver count
> 	adcount:16	-- additional count
> But the record types are variable length:
> 	name:??,	-- key for record, example:
> 	type:16,	-- type of record
> 	class:16,	-- class of record
> 	ttl:16,		-- Time-To-Live
> 	rdlength:16,	-- length of rdata
> 	rdata:??	-- rest of data
> And the name portion isn't easy to skip---you *have* to parse it to find
> the
> end (check RFC-1035 for the gory details).  
>   -spc

I don't see the problem. You do have to do some parsing, but the header
is indeed a no-brainer. In Erlang i'd use the bit syntax parser for the
header, write a  parser for "name", then grab the rest of the record
with bit syntax (rdata can be captured by an addition bit string by
using the captured rdlength). 

I am not suggesting that Lua add a "half baked" implementation of Erlang
bit syntax or that the syntax magically solves all parsing problems. 
For sufficiently complex protocols  you are going to do some hand

I just think that the declarative portion provided by Erlang bit syntax
is very nice and is worthy of study when considering add bit parser
support to Lua.