Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pegjs

Package Overview
Dependencies
Maintainers
2
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pegjs

Parser generator for JavaScript

  • 0.11.0-master.53b9662
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
304K
decreased by-5.28%
Maintainers
2
Weekly downloads
 
Created

What is pegjs?

PEG.js is a simple parser generator for JavaScript that produces fast parsers with excellent error reporting. It allows you to define a grammar in a simple syntax and then generates a parser for that grammar.

What are pegjs's main functionalities?

Grammar Definition

This feature allows you to define a grammar using PEG.js syntax. The example demonstrates a simple arithmetic expression parser that can handle addition and multiplication.

const peg = require('pegjs');
const grammar = `
  start = expression
  expression = term ('+' term)*
  term = factor ('*' factor)*
  factor = number / '(' expression ')'
  number = [0-9]+ { return parseInt(text(), 10); }
`;
const parser = peg.generate(grammar);
const result = parser.parse('2*(3+4)');
console.log(result); // Outputs: 14

Custom Error Messages

PEG.js provides excellent error reporting. This example shows how PEG.js can generate custom error messages when the input does not conform to the defined grammar.

const peg = require('pegjs');
const grammar = `
  start = expression
  expression = term ('+' term)*
  term = factor ('*' factor)*
  factor = number / '(' expression ')'
  number = [0-9]+ { return parseInt(text(), 10); }
`;
const parser = peg.generate(grammar, { output: 'source', format: 'commonjs' });
try {
  parser.parse('2*(3+)');
} catch (e) {
  console.log(e.message); // Outputs: Expected "(", number, or whitespace but ")" found.
}

Semantic Actions

Semantic actions allow you to specify what should happen when a rule matches. This example demonstrates how to add two numbers together when the '+' operator is encountered.

const peg = require('pegjs');
const grammar = `
  start = expression
  expression = left:term '+' right:term { return left + right; }
  term = number
  number = [0-9]+ { return parseInt(text(), 10); }
`;
const parser = peg.generate(grammar);
const result = parser.parse('3+4');
console.log(result); // Outputs: 7

Other packages similar to pegjs

Keywords

FAQs

Package last updated on 15 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc