[][src]Crate python_parser

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![],
        )
    ]
);

Modules

ast

enums and structures that store the syntax tree outputed by the parser.

errors
visitors

Utilities that work on the AST.

Functions

eval_input

Parses the input of eval().

file_input

Parses a module or sequence of commands.

make_strspan

Helper to make an instance of StrSpan, that can be used as the argument to other parsers.

parse_single_input

Parses a single interactive statement, like in the REPL.