admin管理员组文章数量:1336311
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.
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.
Share edited Oct 30, 2015 at 14:32 Evgenij Reznik asked Oct 29, 2015 at 14:44 Evgenij ReznikEvgenij Reznik 18.6k42 gold badges115 silver badges191 bronze badges2 Answers
Reset to default 6This 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
:
Following that you can use:
<script src="parser.js"></script>
<script>
PARSER.parse("test123");
</script>
Plunker example
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./ajax/libs/pegjs/0.7.0/peg.min.js"></script>
本文标签: javascriptExample of how to use PEGjsStack Overflow
版权声明:本文标题:javascript - Example of how to use PEG.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742263884a2443023.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论