Good use case for PegJS?

79 views
Skip to first unread message

George Norris

unread,
Mar 6, 2018, 3:27:35 PM3/6/18
to PEG.js: Parser Generator for JavaScript

Trying to find best solution for some syntax that can set and get variables.


$a = 1 + 1


Internally can keep track of variables already set with a simple object..

const store = {

$a: '1',

$b: '$a + 2',

$c: '$b + 10'

}


$a 
// looks up $a, expands to 1
$b 
// looks up $b, finds $a + 2, looks up $a and expands to 1 + 2
$c 
// looks up $c, finds $b + 10, looks up $b and finds $a + 2, looks up $a and expands

So would need to be able to recursively (or loop) look up variables. Is this something PegJS would be good for?

Futago-za Ryuu

unread,
Mar 9, 2018, 6:57:42 AM3/9/18
to PEG.js: Parser Generator for JavaScript
Yes, PEG.js can be used here, you can look at the examples (https://github.com/pegjs/pegjs/tree/master/examples) on how to parse the syntax, and the arithmetics example is a simple but straightforward example of how to evaluate and deal with values within the rules.
Reply all
Reply to author
Forward
0 new messages