Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make pest no_std compatible. #498

Merged
merged 5 commits into from
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ license = "MIT/Apache-2.0"
readme = "_README.md"

[features]
default = ["std"]
# Implements `std::error::Error` for the `Error` type
std = []
# Enables the `to_json` function for `Pair` and `Pairs`
pretty-print = ["serde", "serde_json"]
# Enable const fn constructor for `PrecClimber` (requires nightly)
Expand Down
18 changes: 12 additions & 6 deletions pest/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@

//! Types for different kinds of parsing failures.

use std::borrow::Cow;
use std::cmp;
use std::error;
use std::fmt;
use std::mem;
use alloc::borrow::Cow;
use alloc::borrow::ToOwned;
use alloc::format;
use alloc::string::String;
use alloc::string::ToString;
use alloc::vec::Vec;
use core::cmp;
use core::fmt;
use core::mem;

use position::Position;
use span::Span;
Expand Down Expand Up @@ -468,7 +472,8 @@ impl<R: RuleType> fmt::Display for Error<R> {
}
}

impl<'i, R: RuleType> error::Error for Error<R> {
#[cfg(feature = "std")]
impl<'i, R: RuleType> std::error::Error for Error<R> {
fn description(&self) -> &str {
match self.variant {
ErrorVariant::ParsingError { .. } => "parsing error",
Expand All @@ -485,6 +490,7 @@ fn visualize_whitespace(input: &str) -> String {
mod tests {
use super::super::position;
use super::*;
use alloc::vec;

#[test]
fn display_parsing_error_mixed() {
Expand Down
7 changes: 5 additions & 2 deletions pest/src/iterators/flat_pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use std::fmt;
use std::rc::Rc;
use alloc::rc::Rc;
use alloc::vec::Vec;
use core::fmt;

use super::pair::{self, Pair};
use super::queueable_token::QueueableToken;
Expand Down Expand Up @@ -151,6 +152,8 @@ impl<'i, R: Clone> Clone for FlatPairs<'i, R> {
mod tests {
use super::super::super::macros::tests::*;
use super::super::super::Parser;
use alloc::vec;
use alloc::vec::Vec;

#[test]
fn iter_for_flat_pairs() {
Expand Down
12 changes: 7 additions & 5 deletions pest/src/iterators/pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use std::fmt;
use std::hash::{Hash, Hasher};
use std::ptr;
use std::rc::Rc;
use std::str;
use alloc::format;
use alloc::rc::Rc;
use alloc::vec::Vec;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ptr;
use core::str;

#[cfg(feature = "pretty-print")]
use serde::ser::SerializeStruct;
Expand Down
17 changes: 12 additions & 5 deletions pest/src/iterators/pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use std::fmt;
use std::hash::{Hash, Hasher};
use std::ptr;
use std::rc::Rc;
use std::str;
use alloc::format;
use alloc::rc::Rc;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ptr;
use core::str;

#[cfg(feature = "pretty-print")]
use serde::ser::SerializeStruct;
Expand Down Expand Up @@ -302,6 +305,10 @@ impl<'i, R: RuleType> ::serde::Serialize for Pairs<'i, R> {
mod tests {
use super::super::super::macros::tests::*;
use super::super::super::Parser;
use alloc::borrow::ToOwned;
use alloc::format;
use alloc::vec;
use alloc::vec::Vec;

#[test]
#[cfg(feature = "pretty-print")]
Expand Down
8 changes: 5 additions & 3 deletions pest/src/iterators/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use std::fmt;
use std::rc::Rc;
use std::str;
use alloc::rc::Rc;
use alloc::vec::Vec;
use core::fmt;
use core::str;

use super::queueable_token::QueueableToken;
use position;
Expand Down Expand Up @@ -132,6 +133,7 @@ mod tests {
use super::super::super::macros::tests::*;
use super::super::super::Parser;
use super::Token;
use alloc::vec::Vec;

#[test]
fn double_ended_iter_for_tokens() {
Expand Down
9 changes: 6 additions & 3 deletions pest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

#![cfg_attr(feature = "const_prec_climber", feature(const_fn))]
#![cfg_attr(not(feature = "std"), no_std)]
CAD97 marked this conversation as resolved.
Show resolved Hide resolved

//! # pest. The Elegant Parser
//!
Expand Down Expand Up @@ -65,19 +65,22 @@

#![doc(html_root_url = "https://docs.rs/pest")]

extern crate alloc;
#[cfg(feature = "std")]
extern crate core;
extern crate ucd_trie;

#[cfg(feature = "pretty-print")]
extern crate serde;
#[cfg(feature = "pretty-print")]
extern crate serde_json;

use core::fmt::Debug;
use core::hash::Hash;
pub use parser::Parser;
pub use parser_state::{state, Atomicity, Lookahead, MatchDir, ParseResult, ParserState};
pub use position::Position;
pub use span::{Lines, Span};
use std::fmt::Debug;
use std::hash::Hash;
pub use token::Token;

pub mod error;
Expand Down
7 changes: 5 additions & 2 deletions pest/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,11 @@ macro_rules! parses_to {
) => {
assert!(
format!("{:?}", first_rule) == "EOI",
format!("expected end of input, but found {:?}", rest)
"expected end of input, but found {:?}", rest
);
assert!(
format!("{:?}", second_rule) == "EOI",
format!("expected end of input, but found {:?}", rest)
"expected end of input, but found {:?}", rest
);
}
_ => panic!("expected end of input, but found {:?}", rest)
Expand Down Expand Up @@ -334,6 +334,9 @@ pub mod tests {
use super::super::error::Error;
use super::super::iterators::Pairs;
use super::super::{state, Parser};
use alloc::format;
use alloc::vec;
use alloc::vec::Vec;

#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down
7 changes: 5 additions & 2 deletions pest/src/parser_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use std::ops::Range;
use std::rc::Rc;
use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::vec;
use alloc::vec::Vec;
use core::ops::Range;

use error::{Error, ErrorVariant};
use iterators::{pairs, QueueableToken};
Expand Down
17 changes: 9 additions & 8 deletions pest/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::Range;
use std::ptr;
use std::str;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ops::Range;
use core::ptr;
use core::str;

use span;

Expand Down Expand Up @@ -427,8 +427,6 @@ impl<'i> Hash for Position<'i> {

#[cfg(test)]
mod tests {
use std::collections::HashSet;

use super::*;

#[test]
Expand Down Expand Up @@ -609,7 +607,10 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn hash() {
use std::collections::HashSet;

let input = "a";
let start = Position::from_start(input);
let mut positions = HashSet::new();
Expand Down
8 changes: 5 additions & 3 deletions pest/src/prec_climber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

//! Constructs useful in infix operator parsing with the precedence climbing method.

use std::borrow::Cow;
use std::iter::Peekable;
use std::ops::BitOr;
use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::iter::Peekable;
use core::ops::BitOr;

use iterators::Pair;
use RuleType;
Expand Down
14 changes: 8 additions & 6 deletions pest/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use std::fmt;
use std::hash::{Hash, Hasher};
use std::ptr;
use std::str;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ptr;
use core::str;

use position;

Expand Down Expand Up @@ -262,6 +262,8 @@ impl<'i> Iterator for Lines<'i> {
#[cfg(test)]
mod tests {
use super::*;
use alloc::borrow::ToOwned;
use alloc::vec::Vec;

#[test]
fn split() {
Expand All @@ -281,7 +283,7 @@ mod tests {
let input = "abc\ndef\nghi";
let span = Span::new(input, 1, 7).unwrap();
let lines: Vec<_> = span.lines().collect();
println!("{:?}", lines);
//println!("{:?}", lines);
assert_eq!(lines.len(), 2);
assert_eq!(lines[0], "abc\n".to_owned());
assert_eq!(lines[1], "def\n".to_owned());
Expand All @@ -293,7 +295,7 @@ mod tests {
let span = Span::new(input, 5, 11).unwrap();
assert!(span.end_pos().at_end());
let lines: Vec<_> = span.lines().collect();
println!("{:?}", lines);
//println!("{:?}", lines);
assert_eq!(lines.len(), 2);
assert_eq!(lines[0], "def\n".to_owned());
assert_eq!(lines[1], "ghi".to_owned());
Expand Down
4 changes: 3 additions & 1 deletion pest/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use std::ops::{Index, Range};
use alloc::vec;
use alloc::vec::Vec;
use core::ops::{Index, Range};

/// Implementation of a `Stack` which maintains an log of `StackOp`s in order to rewind the stack
/// to a previous state.
Expand Down
2 changes: 2 additions & 0 deletions pest/src/unicode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#![allow(bad_style)]
#![allow(clippy::all)]

use alloc::boxed::Box;

macro_rules! char_property_functions {
{$(
mod $module:ident;
Expand Down