Skip to content

Commit a60ed14

Browse files
committed
Separate parser and nom dependency into feature
1 parent 65fabe3 commit a60ed14

File tree

6 files changed

+73
-55
lines changed

6 files changed

+73
-55
lines changed

Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ keywords = ["typing", "language", "type", "inference", "unification"]
1212
categories = ["science", "data-structures", "algorithms"]
1313
edition = "2018"
1414

15+
[features]
16+
default = ["parser"]
17+
parser = ["nom"]
18+
1519
[dependencies]
1620
indexmap = "1.0"
1721
itertools = "0.8"
18-
nom = "=4.1.1"
22+
23+
[dependencies.nom]
24+
version = "=4.1.1"
25+
optional = true
26+

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,8 @@ assert_eq!(t2.to_string(), "list(t1)");
6767
```
6868

6969
See the [documentation](https://docs.rs/polytype) for more details.
70+
71+
## Features
72+
By default `polytype` includes a type parser that can be invoked with `Type::parse`.
73+
This can be disabled with `default-features = false`.
74+

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
102102
mod context;
103103
mod macros;
104+
#[cfg(feature = "parser")]
104105
mod parser;
105106
mod types;
106107

src/parser.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,60 @@ impl<N: Name> Parser<N> {
9797
map!(call_m!(self.monotype), TypeSchema::Monotype))
9898
);
9999
}
100+
impl<N: Name> TypeSchema<N> {
101+
/// Parse a [`TypeSchema`] from a string. This round-trips with [`Display`].
102+
/// This is a **leaky** operation and should be avoided wherever possible:
103+
/// names of constructed types will remain until program termination.
104+
///
105+
/// The "for-all" `∀` is optional.
106+
///
107+
/// # Examples
108+
///
109+
/// ```
110+
/// # use polytype::{ptp, tp, TypeSchema};
111+
/// let t_par = TypeSchema::parse("∀t0. t0 -> t0").expect("valid type");
112+
/// let t_lit = ptp!(0; @arrow[tp!(0), tp!(0)]);
113+
/// assert_eq!(t_par, t_lit);
114+
///
115+
/// let s = "∀t0. ∀t1. (t1 → t0 → t1) → t1 → list(t0) → t1";
116+
/// let t: TypeSchema<&'static str> = TypeSchema::parse(s).expect("valid type");
117+
/// let round_trip = t.to_string();
118+
/// assert_eq!(s, round_trip);
119+
/// ```
120+
///
121+
/// [`Display`]: https://doc.rust-lang.org/std/fmt/trait.Display.html
122+
/// [`TypeSchema`]: enum.TypeSchema.html
123+
pub fn parse(s: &str) -> Result<TypeSchema<N>, ()> {
124+
parse_typeschema(s)
125+
}
126+
}
127+
impl<N: Name> Type<N> {
128+
/// Parse a type from a string. This round-trips with [`Display`]. This is a
129+
/// **leaky** operation and should be avoided wherever possible: names of
130+
/// constructed types will remain until program termination.
131+
///
132+
/// # Examples
133+
///
134+
/// ```
135+
/// # use polytype::{tp, Type};
136+
/// let t_par = Type::parse("int -> hashmap(str, list(bool))").expect("valid type");
137+
/// let t_lit = tp!(@arrow[
138+
/// tp!(int),
139+
/// tp!(hashmap(
140+
/// tp!(str),
141+
/// tp!(list(tp!(bool))),
142+
/// )),
143+
/// ]);
144+
/// assert_eq!(t_par, t_lit);
145+
///
146+
/// let s = "(t1 → t0 → t1) → t1 → list(t0) → t1";
147+
/// let t: Type<&'static str> = Type::parse(s).expect("valid type");
148+
/// let round_trip = t.to_string();
149+
/// assert_eq!(s, round_trip);
150+
/// ```
151+
///
152+
/// [`Display`]: https://doc.rust-lang.org/std/fmt/trait.Display.html
153+
pub fn parse(s: &str) -> Result<Type<N>, ()> {
154+
parse_type(s)
155+
}
156+
}

src/types.rs

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use itertools::Itertools;
22
use std::collections::{HashMap, VecDeque};
33
use std::fmt;
44

5-
use crate::parser::{parse_type, parse_typeschema};
65
use crate::{Context, Name};
76

87
/// Represents a [type variable][1] (an unknown type).
@@ -158,31 +157,6 @@ impl<N: Name> TypeSchema<N> {
158157
}
159158
}
160159
}
161-
/// Parse a [`TypeSchema`] from a string. This round-trips with [`Display`].
162-
/// This is a **leaky** operation and should be avoided wherever possible:
163-
/// names of constructed types will remain until program termination.
164-
///
165-
/// The "for-all" `∀` is optional.
166-
///
167-
/// # Examples
168-
///
169-
/// ```
170-
/// # use polytype::{ptp, tp, TypeSchema};
171-
/// let t_par = TypeSchema::parse("∀t0. t0 -> t0").expect("valid type");
172-
/// let t_lit = ptp!(0; @arrow[tp!(0), tp!(0)]);
173-
/// assert_eq!(t_par, t_lit);
174-
///
175-
/// let s = "∀t0. ∀t1. (t1 → t0 → t1) → t1 → list(t0) → t1";
176-
/// let t: TypeSchema<&'static str> = TypeSchema::parse(s).expect("valid type");
177-
/// let round_trip = t.to_string();
178-
/// assert_eq!(s, round_trip);
179-
/// ```
180-
///
181-
/// [`Display`]: https://doc.rust-lang.org/std/fmt/trait.Display.html
182-
/// [`TypeSchema`]: enum.TypeSchema.html
183-
pub fn parse(s: &str) -> Result<TypeSchema<N>, ()> {
184-
parse_typeschema(s)
185-
}
186160
}
187161
impl<N: Name> fmt::Display for TypeSchema<N> {
188162
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
@@ -570,34 +544,6 @@ impl<N: Name> Type<N> {
570544
}
571545
}
572546
}
573-
/// Parse a type from a string. This round-trips with [`Display`]. This is a
574-
/// **leaky** operation and should be avoided wherever possible: names of
575-
/// constructed types will remain until program termination.
576-
///
577-
/// # Examples
578-
///
579-
/// ```
580-
/// # use polytype::{tp, Type};
581-
/// let t_par = Type::parse("int -> hashmap(str, list(bool))").expect("valid type");
582-
/// let t_lit = tp!(@arrow[
583-
/// tp!(int),
584-
/// tp!(hashmap(
585-
/// tp!(str),
586-
/// tp!(list(tp!(bool))),
587-
/// )),
588-
/// ]);
589-
/// assert_eq!(t_par, t_lit);
590-
///
591-
/// let s = "(t1 → t0 → t1) → t1 → list(t0) → t1";
592-
/// let t: Type<&'static str> = Type::parse(s).expect("valid type");
593-
/// let round_trip = t.to_string();
594-
/// assert_eq!(s, round_trip);
595-
/// ```
596-
///
597-
/// [`Display`]: https://doc.rust-lang.org/std/fmt/trait.Display.html
598-
pub fn parse(s: &str) -> Result<Type<N>, ()> {
599-
parse_type(s)
600-
}
601547
}
602548
impl<N: Name> fmt::Display for Type<N> {
603549
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {

tests/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ fn test_merge_with_sacreds() {
330330
assert_eq!(ctx.new_variable(), tp!(7));
331331
}
332332

333+
#[cfg(feature = "parser")]
333334
#[test]
334335
fn test_parse() {
335336
let t = tp!(int);

0 commit comments

Comments
 (0)