Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@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
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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.