2 unstable releases

Uses old Rust 2015

0.2.0 Sep 9, 2020
0.1.0 Jun 17, 2018

#2759 in Parser implementations

Download history 3/week @ 2023-12-04 7/week @ 2024-02-19 43/week @ 2024-02-26 10/week @ 2024-03-04

60 downloads per month
Used in 3 crates

GPL-3.0+

235KB
6K SLoC

rust-python-parser

A Python parser for Rust libraries and programs.

Currently supports Python 3.8's syntax (except type comments, which are ignored like regular comments)


lib.rs:

A Python parser based on nom, plus some utilities.

Panics

Never (except stack overflows).

Numbers

Python's integer literals may be arbitrary large. This is supported thanks to the num_bigint crate. Disable the bigint feature to fall back to u64.

String encoding

ast::PyStrings are WTF8-encoded if the wtf8 feature is enabled (the default) allowing full support for Python's string literals.

If that feature is disabled, they default to regular Rust strings. Note that without the wtf8 feature, some valid string literals will be badly parsed (missing characters).

Python version support

Currently supports Python 3.7's syntax (and Python 3.8 up to 2018-09-22).

Example

use python_parser::ast::*;
let code = "print(2 + 3, fd=sys.stderr)";
let ast = python_parser::file_input(python_parser::make_strspan(code))
          .unwrap()
          .1;
assert_eq!(ast,
    vec![
        Statement::Assignment(
            vec![
                Expression::Call(
                    Box::new(Expression::Name("print".to_string())),
                    vec![
                        Argument::Positional(
                            Expression::Bop(
                                Bop::Add,
                                Box::new(Expression::Int(2u32.into())),
                                Box::new(Expression::Int(3u32.into())),
                            )
                        ),
                        Argument::Keyword(
                            "fd".to_string(),
                            Expression::Attribute(
                                Box::new(Expression::Name("sys".to_string())),
                                "stderr".to_string(),
                            )
                        ),
                    ]
                ),
            ],
            vec![],
        )
    ]
);

Dependencies

~0.9–1.2MB
~22K SLoC