It's a Clojure(Script) library, but it's still so good it's worth mentioning: Instaparse[0] generates a parser for you from a BNF-y grammar specification. Sample from the readme:<p><pre><code> (def as-and-bs
(insta/parser
"S = AB*
AB = A B
A = 'a'+
B = 'b'+"))
=> (as-and-bs "aaaaabbbaaaabb")
[:S
[:AB [:A "a" "a" "a" "a" "a"] [:B "b" "b" "b"]]
[:AB [:A "a" "a" "a" "a"] [:B "b" "b"]]]
</code></pre>
[0]: <a href="https://github.com/Engelberg/instaparse" rel="nofollow">https://github.com/Engelberg/instaparse</a>
I recently used "Arcsecond" JavaScript Parser Combinator library to output some abstract syntax based on John Reynold's "Definitional interpreters for higher-order programming languages".<p>It's based on Haskell's Parsec parser combinator library, and is zero-dependency.<p>I was convinced to give it a try based on watching the author's YouTube videos "Parser Combinators From Scratch" on "Low Level JavaScript". Enjoyable series - recommended.
hmm this article is a bit outdated; peg.js (mentioned in the article) has been abandoned by the maintainer for a few years now (and it never reached a stable 1.0 ); recently the project was picked up by another team under the name peggy.js <a href="https://github.com/peggyjs/peggy" rel="nofollow">https://github.com/peggyjs/peggy</a>
Anyone have experience with Nearley? I recently did some reading on JS parsers and ended up feeling like it was easier to use than most of the others where you have to pay attention to and know some tricks for handling recursive grammar rules. What I don't have a sense for yet is what the downsides of Nearley might be down the road once my grammar gets bigger and/or goes into production, if the performance issues might pop up suddenly and be hard to fix.
This is pretty cool and useful! I had been doing bootleg parser combinators in JavaScript up until now. This works, although being someone who doesn't write Haskell I assume that the magic that makes parser combinators efficient is not easy to implement in most languages. (I've been using nom with Rust on the side too and it seems to do the same naive stuff that I do.)<p>If you ever want to parse <i>binary</i> data using JavaScript, I will always recommend the excellent Kaitai Struct project.<p><a href="https://kaitai.io/" rel="nofollow">https://kaitai.io/</a>
See my library for parsing text: <a href="https://github.com/eguneys/tamcher" rel="nofollow">https://github.com/eguneys/tamcher</a>
Earlier this year, I wrote a small interpreting parser in JavaScript, which takes a grammar from a string and parses some input from a string according to the grammar into an AST. It is embedded in the page <a href="https://fransfaase.github.io/ParserWorkshop/Online_inter_parser.html" rel="nofollow">https://fransfaase.github.io/ParserWorkshop/Online_inter_par...</a>
I'd also suggest using jsfuzz to find sharp corners in your parser. I used it extensively for my recursive-descent BASIC interpreter (along with a test suite) and it found tons of issues.
Another thing that you can use if you're doing JS: any language that compile to JavaScript. Here are a few options:<p>- Clojure with ClojureScript<p>- F# with Fable<p>- Haskell with ghcjs<p>- OCaml with js_of_ocaml and ReScript<p>- Racket with RacketScript<p>- Scala with Scala.js