
Security News
CISA Extends MITRE Contract as Crisis Accelerates Alternative CVE Coordination Efforts
CISA extended MITRE’s CVE contract by 11 months, avoiding a shutdown but leaving long-term governance and coordination issues unresolved.
@lezer/python
Advanced tools
@lezer/python is a parser for the Python programming language built using the Lezer parser system. It is designed to be used in syntax highlighting, code analysis, and other language processing tasks.
Parsing Python Code
This feature allows you to parse Python code into a syntax tree. The code sample demonstrates how to parse a simple Python function and print the resulting syntax tree.
const {parser} = require('@lezer/python');
const input = 'def hello():\n print("Hello, world!")';
const tree = parser.parse(input);
console.log(tree.toString());
Syntax Highlighting
This feature allows you to use the parsed syntax tree for syntax highlighting. The code sample shows how to highlight a simple Python function using CodeMirror's highlighting utilities.
const {parser} = require('@lezer/python');
const {highlightTree} = require('@codemirror/highlight');
const {defaultHighlightStyle} = require('@codemirror/highlight');
const input = 'def hello():\n print("Hello, world!")';
const tree = parser.parse(input);
const highlights = highlightTree(tree, defaultHighlightStyle);
console.log(highlights);
Code Analysis
This feature allows you to perform code analysis on the parsed syntax tree. The code sample demonstrates a simple analysis that counts the number of function definitions in the input Python code.
const {parser} = require('@lezer/python');
const input = 'def hello():\n print("Hello, world!")';
const tree = parser.parse(input);
// Example analysis: count function definitions
let functionCount = 0;
tree.cursor().iterate(node => {
if (node.name === 'FunctionDefinition') functionCount++;
});
console.log(`Number of functions: ${functionCount}`);
tree-sitter-python is a parser for Python built using the Tree-sitter parsing library. It provides similar functionality to @lezer/python, including parsing Python code into syntax trees and enabling syntax highlighting and code analysis. Tree-sitter is known for its efficiency and is widely used in various text editors and IDEs.
esprima is a high-performance, standard-compliant ECMAScript parser. While it is designed for JavaScript rather than Python, it offers similar capabilities in terms of parsing code into syntax trees and enabling code analysis and syntax highlighting. It is a good example of a parser for a different language with similar functionalities.
acorn is a small, fast, JavaScript-based parser for ECMAScript. Like @lezer/python, it can parse code into syntax trees and is used for syntax highlighting and code analysis. Acorn is known for its speed and efficiency, making it a popular choice for JavaScript parsing tasks.
This is a Python grammar for the Lezer parser system.
The code is licensed under an MIT license.
1.1.12 (2024-03-12)
Emit syntax nodes for backslash escapes in strings.
FAQs
Lezer-based Python grammar
The npm package @lezer/python receives a total of 351,069 weekly downloads. As such, @lezer/python popularity was classified as popular.
We found that @lezer/python demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
CISA extended MITRE’s CVE contract by 11 months, avoiding a shutdown but leaving long-term governance and coordination issues unresolved.
Product
Socket's Rubygems ecosystem support is moving from beta to GA, featuring enhanced security scanning to detect supply chain threats beyond traditional CVEs in your Ruby dependencies.
Research
The Socket Research Team investigates a malicious npm package that appears to be an Advcash integration but triggers a reverse shell during payment success, targeting servers handling transactions.