DEV Community

Discussion on: how to make a parser?

burntcaramel profile image
Patrick Smith

I’ve made a library for JavaScript to make parsers called parcook.

You write generator functions that yield the different parts you are interested in reading next. You can yield strings or regular expressions. You can compose those functions together to parse more complex things.

  • To parse a plus you could yield ‘+’
  • To parse any operator you could const operator = yield [‘+’, ‘-‘, ‘*’, ‘/‘]
  • To parse an integer you could yield /^-?\d+/
  • To parse using the result of another generator function, yield ThatFunctionName

(Generator functions have been part of JavaScript for a few years. They are quite unusual at first but once you get familiar, very powerful)

I’d be happy to make an example parser of maths expressions if you like. Or if you want to have a go I’m happy to answer any questions you have.