Socket
Socket
Sign inDemoInstall

@lezer/lr

Package Overview
Dependencies
1
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @lezer/lr

Incremental parser


Version published
Weekly downloads
1.8M
increased by0.09%
Maintainers
1
Created
Weekly downloads
 

Package description

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

Changelog

Source

0.15.8 (2022-02-04)

Bug fixes

Fix a bug that caused reductions that didn't consume anything to sometimes end up outside their parent node in the tree.

Readme

Source

@lezer/lr

[ WEBSITE | ISSUES | FORUM | CHANGELOG ]

Lezer ("reader" in Dutch, pronounced pretty much as laser) is an incremental GLR parser intended for use in an editor or similar system, which needs to keep a representation of the program current during changes and in the face of syntax errors.

It prioritizes speed and compactness (both of parser table files and of syntax tree) over having a highly usable parse tree—trees nodes are just blobs with a start, end, tag, and set of child nodes, with no further labeling of child nodes or extra metadata.

This package contains the run-time LR parser library. It consumes parsers generated by @lezer/generator.

The parser programming interface is documented on the website.

The code is licensed under an MIT license.

This project was hugely inspired by tree-sitter.

FAQs

Last updated on 04 Feb 2022

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc