Replace num-bigint with malachite-bigint (#18)

Co-authored-by: Jeong YunWon <jeong@youknowone.org>
This commit is contained in:
Steve Shi 2023-06-02 10:06:18 +02:00 committed by GitHub
parent 5e9e8a7589
commit a2e3209c42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 51 additions and 37 deletions

View file

@ -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:

View file

@ -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,6 +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.0" }
rand = "0.8.5"
serde = "1.0"
static_assertions = "1.1"

View file

@ -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 }

View file

@ -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 = []
@ -21,7 +21,6 @@ rustpython-parser-core = { workspace = true }
rustpython-literal = { workspace = true, optional = true }
is-macro = { workspace = true }
num-bigint = { workspace = true }
num-bigint = { workspace = true, optional = true }
malachite-bigint = { workspace = true, optional = true }
static_assertions = "1.1.0"
pyo3 = { workspace = true, optional = true, features = ["num-bigint", "num-complex"] }

View file

@ -1,6 +1,6 @@
//! `builtin_types` in asdl.py and Attributed
use num_bigint::BigInt;
use crate::bigint::BigInt;
pub type String = std::string::String;

View file

@ -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;

View file

@ -55,7 +55,7 @@ impl<U> crate::fold::Fold<U> for ConstantOptimizer {
#[cfg(test)]
mod tests {
use num_bigint::BigInt;
use crate::bigint::BigInt;
use rustpython_parser_core::text_size::TextRange;
#[cfg(feature = "constant-optimization")]

View file

@ -12,5 +12,5 @@ rustpython-literal = { workspace = true }
bitflags = "2.3.1"
itertools = "0.10.5"
num-bigint = { workspace = true }
malachite-bigint = { workspace = true }
num-traits = { workspace = true }

View file

@ -1,7 +1,7 @@
//! Implementation of Printf-Style string formatting
//! as per the [Python Docs](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting).
use bitflags::bitflags;
use num_bigint::{BigInt, Sign};
use malachite_bigint::{BigInt, Sign};
use num_traits::Signed;
use rustpython_literal::{float, format::Case};
use std::{

View file

@ -1,5 +1,5 @@
use itertools::{Itertools, PeekingNext};
use num_bigint::{BigInt, Sign};
use malachite_bigint::{BigInt, Sign};
use num_traits::{cast::ToPrimitive, Signed};
use rustpython_literal::float;
use rustpython_literal::format::Case;

View file

@ -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 }
num-bigint = { workspace = true }
malachite-bigint = { workspace = true, optional = true }
num-bigint = { workspace = true, optional = true }
num-traits = { workspace = true }
unicode_names2 = { workspace = true }

View file

@ -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 num_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 num_bigint::BigInt;
use crate::ast::bigint::BigInt;
const WINDOWS_EOL: &str = "\r\n";
const MAC_EOL: &str = "\r";

View file

@ -284,7 +284,6 @@ pub fn parse_program(source: &str, source_path: &str) -> Result<ast::Suite, Pars
/// For example, parsing a single expression denoting the addition of two numbers:
///
/// ```
/// extern crate num_bigint;
/// use rustpython_parser as parser;
/// let expr = parser::parse_expression("1 + 2", "<embedded>");
///

View file

@ -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 num_bigint::BigInt;
grammar;

8
parser/src/python.rs generated
View file

@ -1,7 +1,7 @@
// auto-generated: "lalrpop 0.20.0"
// sha3: 4ad71899432c2dd721d23812499e7f4385a10cb13467dbf34b4043443888f745
// 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 num_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 num_bigint::BigInt;
#[allow(unused_extern_crates)]
extern crate lalrpop_util as __lalrpop_util;
#[allow(unused_imports)]

View file

@ -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 num_bigint::BigInt;
use std::fmt;
/// The set of tokens the Python source code can be tokenized in.