10000 Expose a method to parse AST from tokens directly by charliermarsh · Pull Request #4229 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Expose a method to parse AST from tokens directly #4229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions compiler/parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! parse a whole program, a single statement, or a single
//! expression.

use crate::lexer::LexResult;
pub use crate::mode::Mode;
use crate::{ast, error::ParseError, lexer, python};
use std::iter;
Expand Down Expand Up @@ -80,6 +81,20 @@ pub fn parse(source: &str, mode: Mode, source_path: &str) -> Result<ast::Mod, Pa
.map_err(|e| crate::error::parse_error_from_lalrpop(e, source_path))
}

// Parse a given token iterator.
pub fn parse_tokens(
lxr: impl IntoIterator<Item = LexResult>,
mode: Mode,
source_path: &str,
) -> Result<ast::Mod, ParseError> {
let marker_token = (Default::default(), mode.to_marker(), Default::default());
let tokenizer = iter::once(Ok(marker_token)).chain(lxr);

python::TopParser::new()
.parse(tokenizer)
.map_err(|e| crate::error::parse_error_from_lalrpop(e, source_path))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
0