re: Writing a DSL parser using PegJS VIEW POST


PegJS and other parser combinator libraries are great when you want to parse something that is not amenable to regular expressions but please don't use it to make more external DSLs. For everything that you can use an external DSL for you could have just as well used a small library or framework in an actual programming language. External DSLs don't have syntax highlighting, linting, and error checking and they basically throw away years of effort that actual programming languages have invested in their tooling. We don't need more external DSLs. If you must for some reason make a DSL then use a language that is suited for making internal DSLs like Ruby.

The example in the post

User.ScheduleAppointment has { 
  a UserId userId 
  an AppointmentDatetime appointmentDatetime
  a Location location from {
    a LocationName locationName from location
    a Latitude latitude
    a Longitude longitude

could just as easily have been written as a small internal DSL in Ruby or any other language for that matter without losing all the benefits of syntax highlighting and modern IDE support

describe command: 'User.ScheduleAppointment' do
  field('userId', 'UserId')
  field('appointmentDatetime', 'AppointmentDatetime')
  field('location', 'Location', [
    field('locationName', 'LocationName', ['location']),
    field('latitude', 'Latitude'),
    field('longitude', 'Longitude')

Moreover internal DSLs are much easier to debug when issues invariably arise and external DSLs often don't have any kind of debugging capabilities so user are forced to jump through all sorts of hoops to debug an issue.


Both external DSLs and eDSLs implemented as syntax extensions can come with the whole range of tools (including proper editor with semantic highlighting, linter, auto-indentation and so on), for free.

You already have a gammar - your PEG. That's pretty much all you need for deriving your tooling for free.

Internal DSLs on top of non-extensible languages are inferior to proper embedded DSLs built on top of macros and syntax extensions.

code of conduct - report abuse