mirror of
https://gitee.com/openharmony/third_party_rust_nom
synced 2024-11-27 09:40:54 +00:00
macros cannot define public methods for now
This commit is contained in:
parent
568e671ae0
commit
c146ef39ed
@ -5,6 +5,7 @@ pub use self::internal::*;
|
||||
pub use self::map::*;
|
||||
pub use self::producer::*;
|
||||
pub use self::consumer::*;
|
||||
pub use self::nom::*;
|
||||
|
||||
pub mod internal;
|
||||
pub mod producer;
|
||||
|
51
src/nom.rs
51
src/nom.rs
@ -8,9 +8,6 @@ use std::fmt::Show;
|
||||
use internal::*;
|
||||
use internal::IResult::*;
|
||||
|
||||
use map::*;
|
||||
use std::str;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! tag(
|
||||
($name:ident $inp:expr) => (
|
||||
@ -168,7 +165,19 @@ macro_rules! filter(
|
||||
)
|
||||
)
|
||||
|
||||
pub is_not!(line_ending "\r\n".as_bytes())
|
||||
// FIXME: when rust-lang/rust#17436 is fixed, macros will be able to export
|
||||
// public methods
|
||||
//pub is_not!(line_ending "\r\n".as_bytes())
|
||||
pub fn line_ending(input:&[u8]) -> IResult<&[u8], &[u8]> {
|
||||
for idx in range(0, input.len()) {
|
||||
for &i in "\r\n".as_bytes().iter() {
|
||||
if input[idx] == i {
|
||||
return Done(input.slice_from(idx), input.slice(0, idx))
|
||||
}
|
||||
}
|
||||
}
|
||||
Done("".as_bytes(), input)
|
||||
}
|
||||
|
||||
pub fn is_alphabetic(chr:u8) -> bool {
|
||||
(chr >= 0x41 && chr <= 0x5A) || (chr >= 0x61 && chr <= 0x7A)
|
||||
@ -182,9 +191,37 @@ pub fn is_alphanumeric(chr: u8) -> bool {
|
||||
is_alphabetic(chr) || is_digit(chr)
|
||||
}
|
||||
|
||||
pub filter!(alpha is_alphabetic)
|
||||
pub filter!(digit is_digit)
|
||||
pub filter!(alphanumeric is_alphanumeric)
|
||||
|
||||
// FIXME: when rust-lang/rust#17436 is fixed, macros will be able to export
|
||||
//pub filter!(alpha is_alphabetic)
|
||||
//pub filter!(digit is_digit)
|
||||
//pub filter!(alphanumeric is_alphanumeric)
|
||||
pub fn alpha(input:&[u8]) -> IResult<&[u8], &[u8]> {
|
||||
for idx in range(0, input.len()) {
|
||||
if !is_alphabetic(input[idx]) {
|
||||
return Done(input.slice_from(idx), input.slice(0, idx))
|
||||
}
|
||||
}
|
||||
Done("".as_bytes(), input)
|
||||
}
|
||||
|
||||
pub fn digit(input:&[u8]) -> IResult<&[u8], &[u8]> {
|
||||
for idx in range(0, input.len()) {
|
||||
if !is_digit(input[idx]) {
|
||||
return Done(input.slice_from(idx), input.slice(0, idx))
|
||||
}
|
||||
}
|
||||
Done("".as_bytes(), input)
|
||||
}
|
||||
|
||||
pub fn alphanumeric(input:&[u8]) -> IResult<&[u8], &[u8]> {
|
||||
for idx in range(0, input.len()) {
|
||||
if !is_alphanumeric(input[idx]) {
|
||||
return Done(input.slice_from(idx), input.slice(0, idx))
|
||||
}
|
||||
}
|
||||
Done("".as_bytes(), input)
|
||||
}
|
||||
|
||||
pub fn sized_buffer(input:&[u8]) -> IResult<&[u8], &[u8]> {
|
||||
if input.len() == 0 {
|
||||
|
@ -3,7 +3,7 @@
|
||||
#[phase(plugin,link)]
|
||||
extern crate nom;
|
||||
|
||||
use nom::{IResult,Producer,FileProducer,Mapper,Mapper2};
|
||||
use nom::{IResult,Producer,FileProducer,Mapper,Mapper2,line_ending};
|
||||
use nom::IResult::*;
|
||||
use nom::ProducerState::*;
|
||||
|
||||
@ -17,7 +17,7 @@ fn map_test_x() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tag_test() {
|
||||
fn tag() {
|
||||
FileProducer::new("links.txt", 20).map(|producer: FileProducer| {
|
||||
let mut p = producer;
|
||||
tag!(f "https://".as_bytes());
|
||||
@ -40,8 +40,16 @@ pub fn print<'a,T: Show>(input: T) -> IResult<T, ()> {
|
||||
|
||||
|
||||
#[test]
|
||||
fn is_not_test() {
|
||||
fn is_not() {
|
||||
is_not!(foo "\r\n".as_bytes())
|
||||
let a = "ab12cd\nefgh".as_bytes();
|
||||
assert_eq!(Done((), a).flat_map(foo), Done("\nefgh".as_bytes(), "ab12cd".as_bytes()))
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn exported_public_method_defined_by_macro() {
|
||||
let a = "ab12cd\nefgh".as_bytes();
|
||||
assert_eq!(Done((), a).flat_map(line_ending), Done("\nefgh".as_bytes(), "ab12cd".as_bytes()))
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user