3

I'm playing around with PEG.js.

I created some simple code that accepts inputs in the form [LettersNumbers]:

  • abc123
  • hello98765
  • etc.

This is the code:

start = expression 

expression = text + number

text = 
a: [a-z]+
{return a.join("");}

number = 
b:[0-9]+
{return b.join("");}

Here: Online version the code can be tested and the parser downloaded, additionally I downloaded peg.js itself.

Unfortunately, the documentation is very sparse. I tried:

<script src="peg-0.9.0.min.js"></script>
<script src="parser.js"></script>
<script>
var parser = new PEG;
parser.parse("test123");
</script>

But got these errors:

Uncaught ReferenceError: module is not defined
Uncaught TypeError: PEG is not a function

Could anybody please provide me with a working example? I just need to integrate the generated js-files into a website.

Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181

2 Answers2

6

This answer assumes that you would like to continue using the PEG.js online version to build your parser.

You only need peg-0.9.0.min.js if you are generating the parser in your web page. Since you are using the PEG.js online version to generate your parser you don't need to do that.

You do need to include the downloaded parser.js. You need to specify a browser-friendly global variable in the PEG.js web version and re-download.

This example uses PARSER:

enter image description here

Following that you can use:

<script src="parser.js"></script>
<script>
PARSER.parse("test123");
</script>

Plunker example

fuyushimoya
  • 9,715
  • 3
  • 27
  • 34
joews
  • 29,767
  • 10
  • 79
  • 91
  • What do you mean by "browser-friendly global variable"? Where is the difference between "module.exports" and "window.PARSER"? – Evgenij Reznik Oct 29 '15 at 15:12
  • @joews Added a [plunker example](http://plnkr.co/edit/tyFQgkrUUcOU4lFLmwgo?p=preview). Mind me add it to your answer? – fuyushimoya Oct 29 '15 at 15:16
  • @user1170330, the generated parser code will try to expose itself to something that can be seen by others, in `node.js`, we use `module.export = XXX` to make other libs able to get `var XXX = require('./parser')`, but in browser, there's no such module syntax, so the generator just use `window.PARSER = XXX` to make other script able to use `var parser = PARSER` to get it. – fuyushimoya Oct 29 '15 at 15:19
  • @fuyushimoya: Thanks! So is there any difference between your and joews' example that needs to be considered? E.g. security etc. In your example the whole grammar is visible, could that be a problem? – Evgenij Reznik Oct 29 '15 at 15:22
  • 3
    Nope, one can easily view the parser.js from devtool. The diff is that joews' gives you a parser that is already available, while mine needs to build from given grammer, and lets you able to build different ones, if you only has one grammer, joews' would be better to use. – fuyushimoya Oct 29 '15 at 15:27
  • @fuyushimoya sure, go ahead – joews Oct 29 '15 at 15:40
2

If you want to use it on web, you need to download the browser version, or ref it from CDN.

Also, you need to use its javascriptAPI to create a parser.

// One line version of your grammer.
var parser = PEG.buildParser('start = expression;expression = text + number;text = a: [a-z]+{return a.join("");};number = b:[0-9]+{return b.join("");}');

console.log(parser.parse("test123"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/pegjs/0.7.0/peg.min.js"></script>
fuyushimoya
  • 9,715
  • 3
  • 27
  • 34