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
nearley
Nearley is a fast, feature-rich parser toolkit for JavaScript. It allows you to define grammars in a flexible way and can handle complex parsing tasks. Compared to @lezer/lr, Nearley is more feature-rich but may be less performant for certain tasks.
pegjs
PEG.js is a simple parser generator for JavaScript based on parsing expression grammars (PEGs). It is easy to use and integrates well with JavaScript projects. Compared to @lezer/lr, PEG.js is simpler but may not offer the same level of performance and flexibility.
antlr4
ANTLR (Another Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It is widely used and supports multiple languages. Compared to @lezer/lr, ANTLR4 is more powerful and versatile but has a steeper learning curve.
@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.