Skip to content

Commit 8fbab2e

Browse files
authored
Merge pull request RustPython#4229 from charliermarsh/charlie/tokens
Expose a method to parse AST from tokens directly
2 parents 1f9d852 + 9ced976 commit 8fbab2e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

compiler/parser/src/parser.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! parse a whole program, a single statement, or a single
66
//! expression.
77
8+
use crate::lexer::LexResult;
89
pub use crate::mode::Mode;
910
use crate::{ast, error::ParseError, lexer, python};
1011
use std::iter;
@@ -80,6 +81,20 @@ pub fn parse(source: &str, mode: Mode, source_path: &str) -> Result<ast::Mod, Pa
8081
.map_err(|e| crate::error::parse_error_from_lalrpop(e, source_path))
8182
}
8283

84+
// Parse a given token iterator.
85+
pub fn parse_tokens(
86+
lxr: impl IntoIterator<Item = LexResult>,
87+
mode: Mode,
88+
source_path: &str,
89+
) -> Result<ast::Mod, ParseError> {
90+
let marker_token = (Default::default(), mode.to_marker(), Default::default());
91+
let tokenizer = iter::once(Ok(marker_token)).chain(lxr);
92+
93+
python::TopParser::new()
94+
.parse(tokenizer)
95+
.map_err(|e| crate::error::parse_error_from_lalrpop(e, source_path))
96+
}
97+
8398
#[cfg(test)]
8499
mod tests {
85100
use super::*;

0 commit comments

Comments
 (0)