mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-09 22:25:23 +00:00
dual bigint support
This commit is contained in:
parent
a70d14c9ba
commit
c0b7af1a4a
12 changed files with 47 additions and 33 deletions
14
.github/workflows/ci.yaml
vendored
14
.github/workflows/ci.yaml
vendored
|
@ -37,12 +37,10 @@ jobs:
|
||||||
|
|
||||||
- uses: Swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
|
|
||||||
- name: run tests with default features
|
- name: run tests with num-bigint
|
||||||
run: cargo test --all
|
run: cargo test --all --no-default-features --features num-bigint
|
||||||
- name: run tests with embedded parser
|
- name: run tests with malachite-bigint and all features
|
||||||
run: cargo test --all --no-default-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
|
||||||
- name: run tests with generated parser
|
|
||||||
run: cargo test --all --all-features
|
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
name: Check Rust code with rustfmt and clippy
|
name: Check Rust code with rustfmt and clippy
|
||||||
|
@ -55,7 +53,9 @@ jobs:
|
||||||
- name: run rustfmt
|
- name: run rustfmt
|
||||||
run: cargo fmt --all -- --check
|
run: cargo fmt --all -- --check
|
||||||
- name: run clippy
|
- 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
|
- uses: actions/setup-python@v4
|
||||||
with:
|
with:
|
||||||
|
|
|
@ -21,7 +21,7 @@ rustpython-ast = { path = "ast", default-features = false }
|
||||||
rustpython-parser-core = { path = "core", features = [] }
|
rustpython-parser-core = { path = "core", features = [] }
|
||||||
rustpython-literal = { path = "literal" }
|
rustpython-literal = { path = "literal" }
|
||||||
rustpython-format = { path = "format" }
|
rustpython-format = { path = "format" }
|
||||||
rustpython-parser = { path = "parser" }
|
rustpython-parser = { path = "parser", default-features = false }
|
||||||
|
|
||||||
ahash = "0.7.6"
|
ahash = "0.7.6"
|
||||||
anyhow = "1.0.45"
|
anyhow = "1.0.45"
|
||||||
|
@ -34,7 +34,7 @@ num-complex = "0.4.0"
|
||||||
num-bigint = "0.4.3"
|
num-bigint = "0.4.3"
|
||||||
num-traits = "0.2"
|
num-traits = "0.2"
|
||||||
pyo3 = { version = "0.18.3" }
|
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"
|
rand = "0.8.5"
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
static_assertions = "1.1"
|
static_assertions = "1.1"
|
||||||
|
|
|
@ -14,7 +14,7 @@ crate-type = ["cdylib"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rustpython-ast = { workspace = true, features = ["location"] }
|
rustpython-ast = { workspace = true, features = ["location"] }
|
||||||
rustpython-parser = { workspace = true }
|
rustpython-parser = { workspace = true, features = ["num-bigint"] }
|
||||||
num-complex = { workspace = true }
|
num-complex = { workspace = true }
|
||||||
once_cell = { workspace = true }
|
once_cell = { workspace = true }
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ repository = "https://github.com/RustPython/Parser/"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["location"]
|
default = ["location", "malachite-bigint"]
|
||||||
constant-optimization = ["fold"]
|
constant-optimization = ["fold"]
|
||||||
location = ["fold", "rustpython-parser-core/location"]
|
location = ["fold", "rustpython-parser-core/location"]
|
||||||
fold = []
|
fold = []
|
||||||
|
@ -22,6 +22,5 @@ rustpython-literal = { workspace = true, optional = true }
|
||||||
|
|
||||||
is-macro = { workspace = true }
|
is-macro = { workspace = true }
|
||||||
num-bigint = { workspace = true, optional = true }
|
num-bigint = { workspace = true, optional = true }
|
||||||
|
malachite-bigint = { workspace = true, optional = true }
|
||||||
static_assertions = "1.1.0"
|
static_assertions = "1.1.0"
|
||||||
|
|
||||||
malachite-bigint = { workspace = true }
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
//! `builtin_types` in asdl.py and Attributed
|
//! `builtin_types` in asdl.py and Attributed
|
||||||
|
|
||||||
use malachite_bigint::BigInt;
|
use crate::bigint::BigInt;
|
||||||
|
|
||||||
pub type String = std::string::String;
|
pub type String = std::string::String;
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,11 @@ mod ranged;
|
||||||
#[cfg(feature = "unparse")]
|
#[cfg(feature = "unparse")]
|
||||||
mod 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 builtin::*;
|
||||||
pub use generic::*;
|
pub use generic::*;
|
||||||
pub use ranged::Ranged;
|
pub use ranged::Ranged;
|
||||||
|
|
|
@ -55,7 +55,7 @@ impl<U> crate::fold::Fold<U> for ConstantOptimizer {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use malachite_bigint::BigInt;
|
use crate::bigint::BigInt;
|
||||||
use rustpython_parser_core::text_size::TextRange;
|
use rustpython_parser_core::text_size::TextRange;
|
||||||
|
|
||||||
#[cfg(feature = "constant-optimization")]
|
#[cfg(feature = "constant-optimization")]
|
||||||
|
|
|
@ -9,11 +9,13 @@ license = "MIT"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["location"]
|
default = ["location", "malachite-bigint"]
|
||||||
location = ["rustpython-ast/location", "rustpython-parser-core/location"]
|
location = ["rustpython-ast/location", "rustpython-parser-core/location"]
|
||||||
serde = ["dep:serde", "rustpython-parser-core/serde"]
|
serde = ["dep:serde", "rustpython-parser-core/serde"]
|
||||||
all-nodes-with-ranges = ["rustpython-ast/all-nodes-with-ranges"]
|
all-nodes-with-ranges = ["rustpython-ast/all-nodes-with-ranges"]
|
||||||
full-lexer = []
|
full-lexer = []
|
||||||
|
malachite-bigint = ["dep:malachite-bigint", "rustpython-ast/malachite-bigint"]
|
||||||
|
num-bigint = ["dep:num-bigint", "rustpython-ast/num-bigint"]
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
anyhow = { workspace = true }
|
anyhow = { workspace = true }
|
||||||
|
@ -28,7 +30,8 @@ rustpython-parser-core = { workspace = true }
|
||||||
itertools = { workspace = true }
|
itertools = { workspace = true }
|
||||||
is-macro = { workspace = true }
|
is-macro = { workspace = true }
|
||||||
log = { 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 }
|
num-traits = { workspace = true }
|
||||||
unicode_names2 = { workspace = true }
|
unicode_names2 = { workspace = true }
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
//!
|
//!
|
||||||
//! [Lexical analysis]: https://docs.python.org/3/reference/lexical_analysis.html
|
//! [Lexical analysis]: https://docs.python.org/3/reference/lexical_analysis.html
|
||||||
use crate::{
|
use crate::{
|
||||||
|
ast::bigint::BigInt,
|
||||||
soft_keywords::SoftKeywordTransformer,
|
soft_keywords::SoftKeywordTransformer,
|
||||||
string::FStringErrorType,
|
string::FStringErrorType,
|
||||||
text_size::{TextLen, TextRange, TextSize},
|
text_size::{TextLen, TextRange, TextSize},
|
||||||
|
@ -35,7 +36,6 @@ use crate::{
|
||||||
Mode,
|
Mode,
|
||||||
};
|
};
|
||||||
use log::trace;
|
use log::trace;
|
||||||
use malachite_bigint::BigInt;
|
|
||||||
use num_traits::{Num, Zero};
|
use num_traits::{Num, Zero};
|
||||||
use std::{char, cmp::Ordering, ops::Index, slice::SliceIndex, str::FromStr};
|
use std::{char, cmp::Ordering, ops::Index, slice::SliceIndex, str::FromStr};
|
||||||
use unic_emoji_char::is_emoji_presentation;
|
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.
|
/// Discard comment if full-lexer is not enabled.
|
||||||
#[cfg(not(feature = "full-lexer"))]
|
#[cfg(not(feature = "full-lexer"))]
|
||||||
fn lex_comment(&mut self) {
|
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.
|
/// Lex a string literal.
|
||||||
fn lex_string(&mut self, kind: StringKind) -> LexResult {
|
fn lex_string(&mut self, kind: StringKind) -> LexResult {
|
||||||
let start_pos = self.get_pos();
|
let start_pos = self.get_pos();
|
||||||
|
@ -626,9 +640,7 @@ where
|
||||||
tabs += 1;
|
tabs += 1;
|
||||||
}
|
}
|
||||||
Some('#') => {
|
Some('#') => {
|
||||||
let _comment = self.lex_comment();
|
self.lex_and_emit_comment()?;
|
||||||
#[cfg(feature = "full-lexer")]
|
|
||||||
self.emit(_comment?);
|
|
||||||
spaces = 0;
|
spaces = 0;
|
||||||
tabs = 0;
|
tabs = 0;
|
||||||
}
|
}
|
||||||
|
@ -775,9 +787,7 @@ where
|
||||||
self.emit(number);
|
self.emit(number);
|
||||||
}
|
}
|
||||||
'#' => {
|
'#' => {
|
||||||
let _comment = self.lex_comment();
|
self.lex_and_emit_comment()?;
|
||||||
#[cfg(feature = "full-lexer")]
|
|
||||||
self.emit(_comment?);
|
|
||||||
}
|
}
|
||||||
'"' | '\'' => {
|
'"' | '\'' => {
|
||||||
let string = self.lex_string(StringKind::String)?;
|
let string = self.lex_string(StringKind::String)?;
|
||||||
|
@ -1360,7 +1370,7 @@ impl std::fmt::Display for LexicalErrorType {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use malachite_bigint::BigInt;
|
use crate::ast::bigint::BigInt;
|
||||||
|
|
||||||
const WINDOWS_EOL: &str = "\r\n";
|
const WINDOWS_EOL: &str = "\r\n";
|
||||||
const MAC_EOL: &str = "\r";
|
const MAC_EOL: &str = "\r";
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
// See also: https://greentreesnakes.readthedocs.io/en/latest/nodes.html#keyword
|
// See also: https://greentreesnakes.readthedocs.io/en/latest/nodes.html#keyword
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{self as ast, Ranged},
|
ast::{self as ast, Ranged, bigint::BigInt},
|
||||||
lexer::{LexicalError, LexicalErrorType},
|
lexer::{LexicalError, LexicalErrorType},
|
||||||
function::{ArgumentList, parse_args, validate_pos_params, validate_arguments},
|
function::{ArgumentList, parse_args, validate_pos_params, validate_arguments},
|
||||||
context::set_context,
|
context::set_context,
|
||||||
|
@ -12,7 +12,6 @@ use crate::{
|
||||||
token::{self, StringKind},
|
token::{self, StringKind},
|
||||||
text_size::TextSize, parser::optional_range
|
text_size::TextSize, parser::optional_range
|
||||||
};
|
};
|
||||||
use malachite_bigint::BigInt;
|
|
||||||
|
|
||||||
grammar;
|
grammar;
|
||||||
|
|
||||||
|
|
8
parser/src/python.rs
generated
8
parser/src/python.rs
generated
|
@ -1,7 +1,7 @@
|
||||||
// auto-generated: "lalrpop 0.20.0"
|
// auto-generated: "lalrpop 0.20.0"
|
||||||
// sha3: 5b283ac4fb75961faa865fa48facd04eb934dc9500dc46f3c7c128ba0d860a73
|
// sha3: b94dbacf01253c4fc4605d489e98f5929504a78e0baa83381e126895ec61cb59
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{self as ast, Ranged},
|
ast::{self as ast, Ranged, bigint::BigInt},
|
||||||
lexer::{LexicalError, LexicalErrorType},
|
lexer::{LexicalError, LexicalErrorType},
|
||||||
function::{ArgumentList, parse_args, validate_pos_params, validate_arguments},
|
function::{ArgumentList, parse_args, validate_pos_params, validate_arguments},
|
||||||
context::set_context,
|
context::set_context,
|
||||||
|
@ -9,7 +9,6 @@ use crate::{
|
||||||
token::{self, StringKind},
|
token::{self, StringKind},
|
||||||
text_size::TextSize, parser::optional_range
|
text_size::TextSize, parser::optional_range
|
||||||
};
|
};
|
||||||
use malachite_bigint::BigInt;
|
|
||||||
#[allow(unused_extern_crates)]
|
#[allow(unused_extern_crates)]
|
||||||
extern crate lalrpop_util as __lalrpop_util;
|
extern crate lalrpop_util as __lalrpop_util;
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
|
@ -22,7 +21,7 @@ extern crate alloc;
|
||||||
mod __parse__Top {
|
mod __parse__Top {
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{self as ast, Ranged},
|
ast::{self as ast, Ranged, bigint::BigInt},
|
||||||
lexer::{LexicalError, LexicalErrorType},
|
lexer::{LexicalError, LexicalErrorType},
|
||||||
function::{ArgumentList, parse_args, validate_pos_params, validate_arguments},
|
function::{ArgumentList, parse_args, validate_pos_params, validate_arguments},
|
||||||
context::set_context,
|
context::set_context,
|
||||||
|
@ -30,7 +29,6 @@ mod __parse__Top {
|
||||||
token::{self, StringKind},
|
token::{self, StringKind},
|
||||||
text_size::TextSize, parser::optional_range
|
text_size::TextSize, parser::optional_range
|
||||||
};
|
};
|
||||||
use malachite_bigint::BigInt;
|
|
||||||
#[allow(unused_extern_crates)]
|
#[allow(unused_extern_crates)]
|
||||||
extern crate lalrpop_util as __lalrpop_util;
|
extern crate lalrpop_util as __lalrpop_util;
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
|
|
|
@ -4,8 +4,8 @@
|
||||||
//! loosely based on the token definitions found in the [CPython source].
|
//! 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
|
//! [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 crate::{text_size::TextSize, Mode};
|
||||||
use malachite_bigint::BigInt;
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
/// The set of tokens the Python source code can be tokenized in.
|
/// The set of tokens the Python source code can be tokenized in.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue