truffle icon indicating copy to clipboard operation
truffle copied to clipboard

Implement parser for mapping key lookup (e.g. `m[0][1]`)

Open gnidan opened this issue 4 years ago • 0 comments

Addresses #3877

Had this lying around from when I discovered parjs a few weeks ago. Decided to clean it up as a draft for review, in case it's something we can utilize in @truffle/decoder.

As it stands, this PR adds a new package @truffle/parse-mapping-lookup, whose name is terrible and needs to change. This new package exposes a function parse(), to accept a struct/mapping lookup expression (m[0], ledger.balances[0], etc.) and return an AST representation of that expression.

This handles literal floating point numbers and strings, but it doesn't do anything special with those values. It does handle escape sequences (thanks to parjs's own example). It currently loses information, in that m["0x"] will parse the same as m["\"0x\""]. @haltman-at I figure this is something that would be handled by @truffle/encoder, if we want it, so I didn't get fancy.

This also squashes all possibly useful error handling; that will likely be something to improve even slightly before merging.

See test cases for an overview of supported syntax.

Possibly minor note: I borrowed the original tsconfig for this new package from @truffle/db, so it uses src/, not lib/). This was because it seemed to be the fastest way to get jest working for tests, but we might want to update this to borrow from one of the codec packages instead.

gnidan avatar Mar 23 '21 04:03 gnidan