/json_parser_nearley

MVP to see how easy JSON parser can be created using nearley

Primary LanguageJavaScriptMIT LicenseMIT

json_parser_nearley

MVP to see how easy simple JSON parser can be created using nearley. Of course, this parser is implemented for educational reasons and may not implement full JSON spec. BTW, if you're interested there's also JSON parser grammar in nearley repo

Prerequisites:

  • understanding of ebnf form (used to describe grammar of your custom language)
  • node

Getting started

  • npm install
  • npm test - to check that all units are passing should see smth like

image

Grammar

Using nearley we can omit implementing tokenizing work and focus on features of our DSL. grammar.ne

When you finished describing your grammar in ebnf form, it is time to compile it to js:

nearleyc grammar.ne -o grammar.js

Parser

Compiled grammar.js we can use to create our parser (in most cases it is tokenizer but in current case parser is implemented right in grammar.ne using postprocessors)

parser

Summary

It is great to have such tools as nearley, because it gives you possibility to write high level DSL logic without bothering too much about tokenizing stuff. This allows to drammaticcally speed up moving DSL project from your head to MVP you can test. For example, in less than hour, current parser was ready to parse simple JSON strings.