Skip to content

Commit 1f00721

Browse files
committed
Make the dependency on unicode_names2 optional.
1 parent 844ccfd commit 1f00721

File tree

6 files changed

+29
-7
lines changed

6 files changed

+29
-7
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ script:
99
- cargo test --no-default-features --features "$FEATURES"
1010
env:
1111
matrix:
12-
- FEATURES="bigint wtf8"
12+
- FEATURES="bigint wtf8 unicode-names"
1313
- FEATURES=""

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ documentation = "https://docs.rs/python-parser"
88
repository = "https://github.com/ProgVal/rust-python-parser"
99

1010
[features]
11-
default = ["bigint", "wtf8"]
11+
default = ["bigint", "wtf8", "unicode-names"]
1212
bigint = ["num-traits", "num-bigint"]
13+
unicode-names = ["unicode_names2"]
1314

1415
[[bin]]
1516
name = "prettyprint"
1617
path = "src/main.rs"
1718

1819
[dependencies]
1920
nom = "^4.0"
20-
#nom_locate = { path = "../nom_locate/" }
2121
nom_locate = "^0.3.0"
2222
unicode-xid = "^0.1"
23-
unicode_names2 = "^0.2.1"
23+
unicode_names2 = { version="^0.2.1", optional=true }
2424
num-traits = { version="^0.2.4", optional=true }
2525
num-bigint = { version="^0.2.0", optional=true }
2626
wtf8 = { version="^0.0.3", optional=true }

src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
pub enum PyParseError {
44
UnexpectedIndent,
55
ExpectedIndent,
6+
DisabledFeature,
67
}
78
impl From<PyParseError> for u32 {
89
fn from(e: PyParseError) -> u32 {

src/expressions.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,12 @@ mod tests {
628628
Box::new(Expression::String(vec![new_pystring("", "\u{8a}")])))
629629
));
630630

631+
}
632+
633+
#[test]
634+
#[cfg_attr(not(feature="unicode-names"), ignore)]
635+
fn test_unicode_name() {
636+
let atom = ExpressionParser::<NewlinesAreNotSpaces>::atom;
631637
assert_parse_eq(atom(make_strspan(r#"'\N{snowman}'"#)), Ok((make_strspan(""),
632638
Box::new(Expression::String(vec![new_pystring("", "☃")])))
633639
));

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ extern crate nom_locate;
6868
extern crate pretty_assertions;
6969

7070
extern crate unicode_xid;
71+
72+
#[cfg(feature="unicode-names")]
7173
extern crate unicode_names2;
7274

7375
#[cfg(feature="bigint")]

src/strings.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use nom::anychar;
22

3+
#[cfg(feature="unicode-names")]
34
use unicode_names2;
45

56
#[cfg(feature="wtf8")]
67
use wtf8;
78

89
use helpers::StrSpan;
10+
use errors::PyParseError;
911
use ast::*;
1012

1113
#[cfg(feature="wtf8")]
@@ -25,6 +27,19 @@ fn cp_from_u32(n: u32) -> Option<char> {
2527
::std::char::from_u32(n)
2628
}
2729

30+
#[cfg(feature="unicode-names")]
31+
named!(unicode_escaped_name<StrSpan, Option<PyStringCodePoint>>,
32+
map!(
33+
preceded!(char!('N'), delimited!(char!('{'), many1!(none_of!("}")), char!('}'))),
34+
|name: Vec<char>| unicode_names2::character(&name.iter().collect::<String>()).map(cp_from_char)
35+
)
36+
);
37+
38+
#[cfg(not(feature="unicode-names"))]
39+
pub fn unicode_escaped_name(i: StrSpan) -> Result<(StrSpan, Option<PyStringCodePoint>), ::nom::Err<StrSpan>> {
40+
Err(::nom::Err::Error(::nom::Context::Code(i, ::nom::ErrorKind::Custom(PyParseError::DisabledFeature.into()))))
41+
}
42+
2843
named!(escapedchar<StrSpan, Option<PyStringCodePoint>>,
2944
preceded!(char!('\\'),
3045
alt!(
@@ -53,9 +68,7 @@ named!(escapedchar<StrSpan, Option<PyStringCodePoint>>,
5368
_ => unreachable!(),
5469
}
5570
}
56-
| preceded!(char!('N'), delimited!(char!('{'), many1!(none_of!("}")), char!('}'))) => { |name: Vec<char>|
57-
unicode_names2::character(&name.iter().collect::<String>()).map(cp_from_char)
58-
}
71+
| unicode_escaped_name
5972
| preceded!(char!('u'), count!(one_of!("0123456789abcdefABCDEF"), 4)) => { |v: Vec<char>| {
6073
let v: Vec<u32> = v.iter().map(|c| c.to_digit(16).unwrap()).collect();
6174
cp_from_u32((v[0] << 12) + (v[1] << 8) + (v[2] << 4) + v[3])

0 commit comments

Comments
 (0)