4.5.08

Javascript recursive descent parser generator

Here is a recursive descent parser generator for javascript. As always, I'm too lazy to document how it works. I did not test it much. Just play with it if you like.

In the first section, you define the tokens. There is a special token called SKIP, which is ignored.

In the second section, you define the grammar of your language. Some examples:

C = { 
  A A;
  B;
};
means C is A followed by A, or C is B.

You can add types to your productions:

C = { 
  [A_PAIR] A A;
  [JUST_B] B;
};

You can name the symbols in the productions:

C = { 
  [A_PAIR] first:A second:A;
  [JUST_B] value:B;
};
Say that A denotes the token /[a-z]/, than the parse result of "x y" is an object like this:
{ Type: "A_PAIR", first:"x", second:"y" }

You can omit braces if there is only one production for a rule:

C = A A;

You can also inherit the parse result of a sub-rule. So instead of:

C = A A;
X = lbrace value:C rbrace;
then using x.value.first, you can do:
C = A A;
X = lbrace #C rbrace;
then use x.first.

You can also use wildcards in restricted cases:

Alist = A+;
Blist = B*;
in which case the result object will be an array.