pub trait Parse: Sized {
    // Required methods
    fn lex_starts_at(
        source: &str,
        offset: TextSize
    ) -> SoftKeywordTransformer<Lexer<Chars<'_>>>;
    fn parse_tokens(
        lxr: impl IntoIterator<Item = Result<(Tok, TextRange), LexicalError>>,
        source_path: &str
    ) -> Result<Self, BaseError<ParseErrorType>>;

    // Provided methods
    fn parse(
        source: &str,
        source_path: &str
    ) -> Result<Self, BaseError<ParseErrorType>> { ... }
    fn parse_without_path(
        source: &str
    ) -> Result<Self, BaseError<ParseErrorType>> { ... }
    fn parse_starts_at(
        source: &str,
        source_path: &str,
        offset: TextSize
    ) -> Result<Self, BaseError<ParseErrorType>> { ... }
}
Expand description

Parse Python code string to implementor’s type.

Example

For example, parsing a simple function definition and a call to that function:

use rustpython_parser::{self as parser, ast, Parse};
let source = r#"
def foo():
   return 42

print(foo())
"#;
let program = ast::Suite::parse(source, "<embedded>");
assert!(program.is_ok());

Parsing a single expression denoting the addition of two numbers, but this time specifying a different, somewhat silly, location:

use rustpython_parser::{self as parser, ast, Parse, text_size::TextSize};

let expr = ast::Expr::parse_starts_at("1 + 2", "<embedded>", TextSize::from(400));
assert!(expr.is_ok());

Required Methods§

source

fn lex_starts_at( source: &str, offset: TextSize ) -> SoftKeywordTransformer<Lexer<Chars<'_>>>

source

fn parse_tokens( lxr: impl IntoIterator<Item = Result<(Tok, TextRange), LexicalError>>, source_path: &str ) -> Result<Self, BaseError<ParseErrorType>>

Provided Methods§

source

fn parse( source: &str, source_path: &str ) -> Result<Self, BaseError<ParseErrorType>>

source

fn parse_without_path(source: &str) -> Result<Self, BaseError<ParseErrorType>>

source

fn parse_starts_at( source: &str, source_path: &str, offset: TextSize ) -> Result<Self, BaseError<ParseErrorType>>

Implementations on Foreign Types§

source§

impl Parse for Vec<Stmt<TextRange>, Global>

source§

fn lex_starts_at( source: &str, offset: TextSize ) -> SoftKeywordTransformer<Lexer<Chars<'_>>>

source§

fn parse_tokens( lxr: impl IntoIterator<Item = Result<(Tok, TextRange), LexicalError>>, source_path: &str ) -> Result<Vec<Stmt<TextRange>, Global>, BaseError<ParseErrorType>>

Implementors§

source§

impl Parse for Constant

source§

impl Parse for Expr<TextRange>

source§

impl Parse for Stmt<TextRange>

source§

impl Parse for ExprAttribute<TextRange>

source§

impl Parse for ExprAwait<TextRange>

source§

impl Parse for ExprBinOp<TextRange>

source§

impl Parse for ExprBoolOp<TextRange>

source§

impl Parse for ExprCall<TextRange>

source§

impl Parse for ExprCompare<TextRange>

source§

impl Parse for ExprConstant<TextRange>

source§

impl Parse for ExprDict<TextRange>

source§

impl Parse for ExprDictComp<TextRange>

source§

impl Parse for ExprFormattedValue<TextRange>

source§

impl Parse for ExprGeneratorExp<TextRange>

source§

impl Parse for ExprIfExp<TextRange>

source§

impl Parse for ExprJoinedStr<TextRange>

source§

impl Parse for ExprLambda<TextRange>

source§

impl Parse for ExprList<TextRange>

source§

impl Parse for ExprListComp<TextRange>

source§

impl Parse for ExprName<TextRange>

source§

impl Parse for ExprNamedExpr<TextRange>

source§

impl Parse for ExprSet<TextRange>

source§

impl Parse for ExprSetComp<TextRange>

source§

impl Parse for ExprSlice<TextRange>

source§

impl Parse for ExprStarred<TextRange>

source§

impl Parse for ExprSubscript<TextRange>

source§

impl Parse for ExprTuple<TextRange>

source§

impl Parse for ExprUnaryOp<TextRange>

source§

impl Parse for ExprYield<TextRange>

source§

impl Parse for ExprYieldFrom<TextRange>

source§

impl Parse for Identifier

source§

impl Parse for ModExpression<TextRange>

source§

impl Parse for ModInteractive<TextRange>

source§

impl Parse for ModModule<TextRange>

source§

impl Parse for StmtAnnAssign<TextRange>

source§

impl Parse for StmtAssert<TextRange>

source§

impl Parse for StmtAssign<TextRange>

source§

impl Parse for StmtAsyncFor<TextRange>

source§

impl Parse for StmtAsyncFunctionDef<TextRange>

source§

impl Parse for StmtAsyncWith<TextRange>

source§

impl Parse for StmtAugAssign<TextRange>

source§

impl Parse for StmtBreak<TextRange>

source§

impl Parse for StmtClassDef<TextRange>

source§

impl Parse for StmtContinue<TextRange>

source§

impl Parse for StmtDelete<TextRange>

source§

impl Parse for StmtExpr<TextRange>

source§

impl Parse for StmtFor<TextRange>

source§

impl Parse for StmtFunctionDef<TextRange>

source§

impl Parse for StmtGlobal<TextRange>

source§

impl Parse for StmtIf<TextRange>

source§

impl Parse for StmtImport<TextRange>

source§

impl Parse for StmtImportFrom<TextRange>

source§

impl Parse for StmtMatch<TextRange>

source§

impl Parse for StmtNonlocal<TextRange>

source§

impl Parse for StmtPass<TextRange>

source§

impl Parse for StmtRaise<TextRange>

source§

impl Parse for StmtReturn<TextRange>

source§

impl Parse for StmtTry<TextRange>

source§

impl Parse for StmtTryStar<TextRange>

source§

impl Parse for StmtTypeAlias<TextRange>

source§

impl Parse for StmtWhile<TextRange>

source§

impl Parse for StmtWith<TextRange>