diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index e829a88..392f84a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -37,12 +37,10 @@ jobs: - uses: Swatinem/rust-cache@v2 - - name: run tests with default features - run: cargo test --all - - name: run tests with embedded parser - run: cargo test --all --no-default-features - - name: run tests with generated parser - run: cargo test --all --all-features + - name: run tests with num-bigint + run: cargo test --all --no-default-features --features num-bigint + - name: run tests with malachite-bigint and all features + run: cargo test --all --features location,malachite-bigint,constant-optimization,fold,unparse,visitor,all-nodes-with-ranges,full-lexer,serde --exclude rustpython-ast-pyo3 lint: name: Check Rust code with rustfmt and clippy @@ -55,7 +53,9 @@ jobs: - name: run rustfmt run: cargo fmt --all -- --check - name: run clippy - run: cargo clippy --all --all-features -- -Dwarnings + run: cargo clippy --all --no-default-features --features num-bigint + - name: run clippy + run: cargo clippy --all --features location,malachite-bigint,constant-optimization,fold,unparse,visitor,all-nodes-with-ranges,full-lexer,serde --exclude rustpython-ast-pyo3 -- -Dwarnings - uses: actions/setup-python@v4 with: diff --git a/Cargo.toml b/Cargo.toml index 5d6bad1..2c8cbfc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ rustpython-ast = { path = "ast", default-features = false } rustpython-parser-core = { path = "core", features = [] } rustpython-literal = { path = "literal" } rustpython-format = { path = "format" } -rustpython-parser = { path = "parser" } +rustpython-parser = { path = "parser", default-features = false } ahash = "0.7.6" anyhow = "1.0.45" @@ -34,7 +34,7 @@ num-complex = "0.4.0" num-bigint = "0.4.3" num-traits = "0.2" pyo3 = { version = "0.18.3" } -malachite-bigint = { version = "0.1.8", git = "https://github.com/RustPython/malachite-bigint.git" } +malachite-bigint = { version = "0.1.0" } rand = "0.8.5" serde = "1.0" static_assertions = "1.1" diff --git a/ast-pyo3/Cargo.toml b/ast-pyo3/Cargo.toml index 80bddd3..bddf453 100644 --- a/ast-pyo3/Cargo.toml +++ b/ast-pyo3/Cargo.toml @@ -14,7 +14,7 @@ crate-type = ["cdylib"] [dependencies] rustpython-ast = { workspace = true, features = ["location"] } -rustpython-parser = { workspace = true } +rustpython-parser = { workspace = true, features = ["num-bigint"] } num-complex = { workspace = true } once_cell = { workspace = true } diff --git a/ast/Cargo.toml b/ast/Cargo.toml index edcb4f0..d3229bc 100644 --- a/ast/Cargo.toml +++ b/ast/Cargo.toml @@ -8,7 +8,7 @@ repository = "https://github.com/RustPython/Parser/" license = "MIT" [features] -default = ["location"] +default = ["location", "malachite-bigint"] constant-optimization = ["fold"] location = ["fold", "rustpython-parser-core/location"] fold = [] @@ -22,6 +22,5 @@ rustpython-literal = { workspace = true, optional = true } is-macro = { workspace = true } num-bigint = { workspace = true, optional = true } +malachite-bigint = { workspace = true, optional = true } static_assertions = "1.1.0" - -malachite-bigint = { workspace = true } diff --git a/ast/src/builtin.rs b/ast/src/builtin.rs index bd0b7e3..1a64efb 100644 --- a/ast/src/builtin.rs +++ b/ast/src/builtin.rs @@ -1,6 +1,6 @@ //! `builtin_types` in asdl.py and Attributed -use malachite_bigint::BigInt; +use crate::bigint::BigInt; pub type String = std::string::String; diff --git a/ast/src/lib.rs b/ast/src/lib.rs index 94f5901..b55266c 100644 --- a/ast/src/lib.rs +++ b/ast/src/lib.rs @@ -14,6 +14,11 @@ mod ranged; #[cfg(feature = "unparse")] mod unparse; +#[cfg(feature = "malachite-bigint")] +pub use malachite_bigint as bigint; +#[cfg(feature = "num-bigint")] +pub use num_bigint as bigint; + pub use builtin::*; pub use generic::*; pub use ranged::Ranged; diff --git a/ast/src/optimizer.rs b/ast/src/optimizer.rs index 8612895..42a6ddd 100644 --- a/ast/src/optimizer.rs +++ b/ast/src/optimizer.rs @@ -55,7 +55,7 @@ impl crate::fold::Fold for ConstantOptimizer { #[cfg(test)] mod tests { - use malachite_bigint::BigInt; + use crate::bigint::BigInt; use rustpython_parser_core::text_size::TextRange; #[cfg(feature = "constant-optimization")] diff --git a/parser/Cargo.toml b/parser/Cargo.toml index e805e69..863a28a 100644 --- a/parser/Cargo.toml +++ b/parser/Cargo.toml @@ -9,11 +9,13 @@ license = "MIT" edition = "2021" [features] -default = ["location"] +default = ["location", "malachite-bigint"] location = ["rustpython-ast/location", "rustpython-parser-core/location"] serde = ["dep:serde", "rustpython-parser-core/serde"] all-nodes-with-ranges = ["rustpython-ast/all-nodes-with-ranges"] full-lexer = [] +malachite-bigint = ["dep:malachite-bigint", "rustpython-ast/malachite-bigint"] +num-bigint = ["dep:num-bigint", "rustpython-ast/num-bigint"] [build-dependencies] anyhow = { workspace = true } @@ -28,7 +30,8 @@ rustpython-parser-core = { workspace = true } itertools = { workspace = true } is-macro = { workspace = true } log = { workspace = true } -malachite-bigint = { workspace = true } +malachite-bigint = { workspace = true, optional = true } +num-bigint = { workspace = true, optional = true } num-traits = { workspace = true } unicode_names2 = { workspace = true } diff --git a/parser/src/lexer.rs b/parser/src/lexer.rs index 5dc2be7..862b8ae 100644 --- a/parser/src/lexer.rs +++ b/parser/src/lexer.rs @@ -28,6 +28,7 @@ //! //! [Lexical analysis]: https://docs.python.org/3/reference/lexical_analysis.html use crate::{ + ast::bigint::BigInt, soft_keywords::SoftKeywordTransformer, string::FStringErrorType, text_size::{TextLen, TextRange, TextSize}, @@ -35,7 +36,6 @@ use crate::{ Mode, }; use log::trace; -use malachite_bigint::BigInt; use num_traits::{Num, Zero}; use std::{char, cmp::Ordering, ops::Index, slice::SliceIndex, str::FromStr}; use unic_emoji_char::is_emoji_presentation; @@ -466,6 +466,13 @@ where } } + #[cfg(feature = "full-lexer")] + fn lex_and_emit_comment(&mut self) -> Result<(), LexicalError> { + let comment = self.lex_comment()?; + self.emit(comment); + Ok(()) + } + /// Discard comment if full-lexer is not enabled. #[cfg(not(feature = "full-lexer"))] fn lex_comment(&mut self) { @@ -480,6 +487,13 @@ where } } + #[cfg(not(feature = "full-lexer"))] + #[inline] + fn lex_and_emit_comment(&mut self) -> Result<(), LexicalError> { + self.lex_comment(); + Ok(()) + } + /// Lex a string literal. fn lex_string(&mut self, kind: StringKind) -> LexResult { let start_pos = self.get_pos(); @@ -626,9 +640,7 @@ where tabs += 1; } Some('#') => { - let _comment = self.lex_comment(); - #[cfg(feature = "full-lexer")] - self.emit(_comment?); + self.lex_and_emit_comment()?; spaces = 0; tabs = 0; } @@ -775,9 +787,7 @@ where self.emit(number); } '#' => { - let _comment = self.lex_comment(); - #[cfg(feature = "full-lexer")] - self.emit(_comment?); + self.lex_and_emit_comment()?; } '"' | '\'' => { let string = self.lex_string(StringKind::String)?; @@ -1360,7 +1370,7 @@ impl std::fmt::Display for LexicalErrorType { #[cfg(test)] mod tests { use super::*; - use malachite_bigint::BigInt; + use crate::ast::bigint::BigInt; const WINDOWS_EOL: &str = "\r\n"; const MAC_EOL: &str = "\r"; diff --git a/parser/src/python.lalrpop b/parser/src/python.lalrpop index 90f9edb..31eefe7 100644 --- a/parser/src/python.lalrpop +++ b/parser/src/python.lalrpop @@ -4,7 +4,7 @@ // See also: https://greentreesnakes.readthedocs.io/en/latest/nodes.html#keyword use crate::{ - ast::{self as ast, Ranged}, + ast::{self as ast, Ranged, bigint::BigInt}, lexer::{LexicalError, LexicalErrorType}, function::{ArgumentList, parse_args, validate_pos_params, validate_arguments}, context::set_context, @@ -12,7 +12,6 @@ use crate::{ token::{self, StringKind}, text_size::TextSize, parser::optional_range }; -use malachite_bigint::BigInt; grammar; diff --git a/parser/src/python.rs b/parser/src/python.rs index cd79897..29e7922 100644 --- a/parser/src/python.rs +++ b/parser/src/python.rs @@ -1,7 +1,7 @@ // auto-generated: "lalrpop 0.20.0" -// sha3: 5b283ac4fb75961faa865fa48facd04eb934dc9500dc46f3c7c128ba0d860a73 +// sha3: b94dbacf01253c4fc4605d489e98f5929504a78e0baa83381e126895ec61cb59 use crate::{ - ast::{self as ast, Ranged}, + ast::{self as ast, Ranged, bigint::BigInt}, lexer::{LexicalError, LexicalErrorType}, function::{ArgumentList, parse_args, validate_pos_params, validate_arguments}, context::set_context, @@ -9,7 +9,6 @@ use crate::{ token::{self, StringKind}, text_size::TextSize, parser::optional_range }; -use malachite_bigint::BigInt; #[allow(unused_extern_crates)] extern crate lalrpop_util as __lalrpop_util; #[allow(unused_imports)] @@ -22,7 +21,7 @@ extern crate alloc; mod __parse__Top { use crate::{ - ast::{self as ast, Ranged}, + ast::{self as ast, Ranged, bigint::BigInt}, lexer::{LexicalError, LexicalErrorType}, function::{ArgumentList, parse_args, validate_pos_params, validate_arguments}, context::set_context, @@ -30,7 +29,6 @@ mod __parse__Top { token::{self, StringKind}, text_size::TextSize, parser::optional_range }; - use malachite_bigint::BigInt; #[allow(unused_extern_crates)] extern crate lalrpop_util as __lalrpop_util; #[allow(unused_imports)] diff --git a/parser/src/token.rs b/parser/src/token.rs index e62d544..f472eeb 100644 --- a/parser/src/token.rs +++ b/parser/src/token.rs @@ -4,8 +4,8 @@ //! loosely based on the token definitions found in the [CPython source]. //! //! [CPython source]: https://github.com/python/cpython/blob/dfc2e065a2e71011017077e549cd2f9bf4944c54/Include/internal/pycore_token.h +use crate::ast::bigint::BigInt; use crate::{text_size::TextSize, Mode}; -use malachite_bigint::BigInt; use std::fmt; /// The set of tokens the Python source code can be tokenized in.