Skip to content

Commit 27e3873

Browse files
authored
Add full-lexer feature (#36)
1 parent dd4cc25 commit 27e3873

File tree

7 files changed

+1216
-1188
lines changed

7 files changed

+1216
-1188
lines changed

parser/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ default = ["location"]
1313
location = ["rustpython-ast/location", "rustpython-parser-core/location"]
1414
serde = ["dep:serde", "rustpython-parser-core/serde"]
1515
all-nodes-with-ranges = ["rustpython-ast/all-nodes-with-ranges"]
16+
full-lexer = []
1617

1718
[build-dependencies]
1819
anyhow = { workspace = true }

parser/src/lexer.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ where
450450
}
451451

452452
/// Lex a single comment.
453+
#[cfg(feature = "full-lexer")]
453454
fn lex_comment(&mut self) -> LexResult {
454455
let start_pos = self.get_pos();
455456
let mut value = String::new();
@@ -465,6 +466,20 @@ where
465466
}
466467
}
467468

469+
/// Discard comment if full-lexer is not enabled.
470+
#[cfg(not(feature = "full-lexer"))]
471+
fn lex_comment(&mut self) {
472+
loop {
473+
match self.window[0] {
474+
Some('\n' | '\r') | None => {
475+
return;
476+
}
477+
Some(_) => {}
478+
}
479+
self.next_char().unwrap();
480+
}
481+
}
482+
468483
/// Lex a string literal.
469484
fn lex_string(&mut self, kind: StringKind) -> LexResult {
470485
let start_pos = self.get_pos();
@@ -611,8 +626,9 @@ where
611626
tabs += 1;
612627
}
613628
Some('#') => {
614-
let comment = self.lex_comment()?;
615-
self.emit(comment);
629+
let _comment = self.lex_comment();
630+
#[cfg(feature = "full-lexer")]
631+
self.emit(_comment?);
616632
spaces = 0;
617633
tabs = 0;
618634
}
@@ -753,8 +769,9 @@ where
753769
self.emit(number);
754770
}
755771
'#' => {
756-
let comment = self.lex_comment()?;
757-
self.emit(comment);
772+
let _comment = self.lex_comment();
773+
#[cfg(feature = "full-lexer")]
774+
self.emit(_comment?);
758775
}
759776
'"' | '\'' => {
760777
let string = self.lex_string(StringKind::String)?;
@@ -1101,6 +1118,7 @@ where
11011118
self.at_begin_of_line = true;
11021119
self.emit((Tok::Newline, TextRange::new(tok_start, tok_end)));
11031120
} else {
1121+
#[cfg(feature = "full-lexer")]
11041122
self.emit((Tok::NonLogicalNewline, TextRange::new(tok_start, tok_end)));
11051123
}
11061124
}
@@ -1408,6 +1426,7 @@ mod tests {
14081426
($($name:ident: $eol:expr,)*) => {
14091427
$(
14101428
#[test]
1429+
#[cfg(feature = "full-lexer")]
14111430
fn $name() {
14121431
let source = format!(r"99232 # {}", $eol);
14131432
let tokens = lex_source(&source);
@@ -1428,6 +1447,7 @@ mod tests {
14281447
($($name:ident: $eol:expr,)*) => {
14291448
$(
14301449
#[test]
1450+
#[cfg(feature = "full-lexer")]
14311451
fn $name() {
14321452
let source = format!("123 # Foo{}456", $eol);
14331453
let tokens = lex_source(&source);
@@ -1607,6 +1627,7 @@ mod tests {
16071627
($($name:ident: $eol:expr,)*) => {
16081628
$(
16091629
#[test]
1630+
#[cfg(feature = "full-lexer")]
16101631
fn $name() {
16111632
let source = r"x = [
16121633
@@ -1669,6 +1690,7 @@ mod tests {
16691690
}
16701691

16711692
#[test]
1693+
#[cfg(feature = "full-lexer")]
16721694
fn test_non_logical_newline_in_string_continuation() {
16731695
let source = r"(
16741696
'a'
@@ -1698,6 +1720,7 @@ mod tests {
16981720
}
16991721

17001722
#[test]
1723+
#[cfg(feature = "full-lexer")]
17011724
fn test_logical_newline_line_comment() {
17021725
let source = "#Hello\n#World";
17031726
let tokens = lex_source(source);

parser/src/parser.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,10 @@ pub fn parse_tokens(
190190
source_path: &str,
191191
) -> Result<ast::Mod, ParseError> {
192192
let marker_token = (Tok::start_marker(mode), Default::default());
193-
let lexer = iter::once(Ok(marker_token))
194-
.chain(lxr)
195-
.filter_ok(|(tok, _)| !matches!(tok, Tok::Comment { .. } | Tok::NonLogicalNewline));
193+
let lexer = iter::once(Ok(marker_token)).chain(lxr);
194+
#[cfg(feature = "full-lexer")]
195+
let lexer =
196+
lexer.filter_ok(|(tok, _)| !matches!(tok, Tok::Comment { .. } | Tok::NonLogicalNewline));
196197
python::TopParser::new()
197198
.parse(
198199
lexer

parser/src/python.lalrpop

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,6 @@ extern {
17431743
name => token::Tok::Name { name: <String> },
17441744
"\n" => token::Tok::Newline,
17451745
";" => token::Tok::Semi,
1746-
"#" => token::Tok::Comment(_),
1746+
// "#" => token::Tok::Comment(_),
17471747
}
17481748
}

0 commit comments

Comments
 (0)