Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
Ignore MagicCommand token when parsing in Jupyter mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Jul 6, 2023
1 parent 17bdc29 commit 7509cd4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/src/mode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Control in the different modes by which a source file can be parsed.
/// The mode argument specifies in what way code must be parsed.
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum Mode {
/// The code consists of a sequence of statements.
Module,
Expand Down
9 changes: 7 additions & 2 deletions parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl Parse for ast::Constant {
}

/// Parse a full Python program usually consisting of multiple lines.
///
///
/// This is a convenience function that can be used to parse a full Python program without having to
/// specify the [`Mode`] or the location. It is probably what you want to use most of the time.
///
Expand Down Expand Up @@ -414,7 +414,12 @@ pub fn parse_tokens(
#[cfg(feature = "full-lexer")]
let lxr =
lxr.filter_ok(|(tok, _)| !matches!(tok, Tok::Comment { .. } | Tok::NonLogicalNewline));
parse_filtered_tokens(lxr, mode, source_path)
if mode == Mode::Jupyter {
let lxr = lxr.filter_ok(|(tok, _)| !matches!(tok, Tok::MagicCommand(..)));
parse_filtered_tokens(lxr, mode, source_path)
} else {
parse_filtered_tokens(lxr, mode, source_path)
}
}

fn parse_filtered_tokens(
Expand Down
3 changes: 1 addition & 2 deletions parser/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,9 @@ pub enum Tok {
impl Tok {
pub fn start_marker(mode: Mode) -> Self {
match mode {
Mode::Module => Tok::StartModule,
Mode::Module | Mode::Jupyter => Tok::StartModule,
Mode::Interactive => Tok::StartInteractive,
Mode::Expression => Tok::StartExpression,
Mode::Jupyter => Tok::StartModule,
}
}
}
Expand Down

0 comments on commit 7509cd4

Please sign in to comment.