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

@lezer/lr

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lezer/lr

Incremental parser

  • 0.16.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
369K
decreased by-80.53%
Maintainers
1
Weekly downloads
 
Created

What is @lezer/lr?

@lezer/lr is a library for building efficient parsers in JavaScript. It is part of the Lezer parser system, which is designed to be fast, flexible, and easy to use. The library provides tools to define grammars, build parsers, and work with syntax trees.

What are @lezer/lr's main functionalities?

Defining a Grammar

This feature allows you to define a grammar for your language. The code sample demonstrates how to define a simple grammar for a language that supports print statements with string literals.

const {parser} = require('@lezer/lr');
const {buildParser} = require('@lezer/generator');

const grammar = `
@top Program { statement* }

statement { "print" String }
String { "\"" (!"\"")* "\"" }
`;

const myParser = buildParser(grammar);
console.log(myParser.parse('print "Hello, World!"'));

Parsing Input

This feature allows you to parse input strings according to the defined grammar. The code sample shows how to parse a simple print statement and output the resulting syntax tree.

const {parser} = require('@lezer/lr');
const {buildParser} = require('@lezer/generator');

const grammar = `
@top Program { statement* }

statement { "print" String }
String { "\"" (!"\"")* "\"" }
`;

const myParser = buildParser(grammar);
const tree = myParser.parse('print "Hello, World!"');
console.log(tree);

Working with Syntax Trees

This feature allows you to work with and traverse syntax trees generated by the parser. The code sample demonstrates how to recursively print the structure of a syntax tree.

const {parser} = require('@lezer/lr');
const {buildParser} = require('@lezer/generator');

const grammar = `
@top Program { statement* }

statement { "print" String }
String { "\"" (!"\"")* "\"" }
`;

const myParser = buildParser(grammar);
const tree = myParser.parse('print "Hello, World!"');

function printTree(node, indent = 0) {
  console.log(' '.repeat(indent) + node.type.name);
  for (let child of node.children) {
    printTree(child, indent + 2);
  }
}

printTree(tree.topNode);

Other packages similar to @lezer/lr

FAQs

Package last updated on 11 May 2022

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