5 ms·
Visual programming with Elixir: Learning to write binary parsers (2019)
- quikoa 4y agohttps://archive.is/2rwPv https://archive.is/2rwPv
- Nezteb 4y agoYou can also replace `medium.com` with `scribe.rip` to get a much friendlier frontend: https://hansonkd.scribe.rip/building-beautiful-binary-parsers-in-elixir-1bd7f865bf17 https://hansonkd.scribe.rip/building-beautiful-binary-parser...
- quikoa 4y agoInteresting, I didn't know about that one, thanks!
- gen_greyface 4y agoi have a bookmarklet for this. love that it formats the github embeds quite nicely too. javascript:(()=>window.location.href="https://scribe.rip"+location.pathname)()
- codesnik 4y agosome errors there. head = 0x00 piece = <<0x01, 0x01, 0x01, 0x01, 0x01>> << head, # When a variable is already referenced it must be equal for the pattern to match piece, # Match a string rest :: binary >> # You can mix matching variables and assigning new variables. it should have ^head, and ^piece (the term is "pinning"). Unlike in erlang, elixir will happily "reassign" variables to the new content on whatever is on a right side of the match.
- codesnik 4y agoI sometimes ponder, how much effort it would take to make a similar matching interface not for binaries but for regexps and/or something grammar-like. After structured matching of binaries regexps in elixir just feel awkward. It wouldn't work well with other matching constructs, though, if matching is baked into beamvm itself as a core instruction for speed or something. Guess I have something to investigate this evening.
- bmitc 4y agoIt's not Elixir, but you might be interested in F#'s active patterns, particularly the parameterized active patterns: https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/active-patterns#parameterized-active-patterns https://learn.microsoft.com/en-us/dotnet/fsharp/language-ref... F#'s active patterns allow you to extend the built-in pattern matching to other things, and in this case, you can have patterns that return regular expression matches.
- codesnik 4y agosounds interesting, thanks.
- pmarreck 4y agoYou could probably write a regex parser in pure Elixir
- dugmartin 4y agoIf you are interested in learning about text (not binary) parsing in Elixir check out the nimbleparsec library: https://hexdocs.pm/nimble_parsec/NimbleParsec.html https://hexdocs.pm/nimble_parsec/NimbleParsec.html I was able to write a very succinct parser for woodworking (using imperial fractions) in around 38 lines of commented code. It parses strings like 0.25 or 2 or 1/4 or 1/4" or 1' 2 3/4" and pops out an integer or float in inches. I had never used a parser combinator library before but I have written many recursive descent parsers over the years. I was a little surprised when I added the `debug: true` option and saw the shear amount of code it generated. That probably could be reduced by refactoring my combinators.
- geekodour 4y agoi am interested in knowing what is woodworking here
- sceadu 4y agoPretty sure he means literal woodworking... You know, taking measurements, hammering, chiseling...
- dugmartin 4y agoI left out “design” after woodworking. I’m building an online 3D DIY design and instructional app where you can customize plans by entering dimensions in a form (https://plansformer.com/ https://plansformer.com/).
- foobiekr 4y agohttps://kaitai.io/ https://kaitai.io/ Worth a look if you are writing binary parsers.