Tuesday, September 1, 2020

Pasukon – The easy JavaScript parser generator

Pasukon generates parsers using an easy to learn grammar. It's based on parser combinators, and also implements a lexing step.

It is highly extensible (you can make your own lexer and combinators), has no external dependencies, and works in both Node.js and Browser.

Try it #

lex
  match  NUMBER     /[0-9]+(?:\.[0-9]+)?/
  match  PLUS       '+'
  match  MINUS      '-'
  match  TIMES      '*'
  match  DIV        '/'
  match  POPEN      '('
  match  PCLOSE     ')'
  ignore WHITESPACE /^\s+/
/lex

addition
  | (substraction as :lhs) then :PLUS then (substraction as :rhs)
  |> 'return $.lhs + $.rhs'
  | substraction
  ;

substraction
  | (multiplication as :lhs) then :MINUS then (multiplication as :rhs)
  |> 'return $.lhs - $.rhs'
  | multiplication
  ;

multiplication
  | (division as :lhs) then :TIMES then (division as :rhs)
  |> 'return $.lhs * $.rhs'
  | division
  ;

division
  | (expression as :lhs) then :DIV then (expression as :rhs)
  |> 'return $.lhs / $.rhs'
  | expression
  ;

expression
  | :POPEN then (addition as :expr) then :PCLOSE
  |> 'return $.expr'
  | number
  ;

number
  | :NUMBER 'return +$1'
  ;

start
  | addition
  ;

Input

Documentation #

You can find the full documentation at the GitHub repo.



from Hacker News https://pasukon.rocks/

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.