1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
use pear::input::Span;
use pear::{macros::*, parsers::*};
type FourMarkers = (usize, usize, usize, usize);
type Input<'a> = pear::input::Pear<pear::input::Text<'a>>;
type Result<'a, T> = pear::input::Result<T, Input<'a>>;
#[parser]
fn simple<'a>(input: &mut Input<'a>) -> Result<'a, FourMarkers> {
let first = parse_last_marker!();
eat('.')?;
let second = parse_last_marker!();
eat_slice("..")?;
let third = parse_last_marker!();
eat_slice("..")?;
let fourth = parse_last_marker!();
(first, second, third, fourth)
}
#[parser]
fn simple_updating<'a>(input: &mut Input<'a>) -> Result<'a, FourMarkers> {
let first = parse_current_marker!();
eat('.')?;
let second = parse_current_marker!();
eat_slice("..")?;
let third = parse_current_marker!();
eat_slice("..")?;
let fourth = parse_current_marker!();
(first, second, third, fourth)
}
#[parser]
fn resetting<'a>(input: &mut Input<'a>) -> Result<'a, FourMarkers> {
let first = parse_last_marker!();
eat('.')?;
parse_mark!();
let second = parse_last_marker!();
eat_slice("..")?;
let third = parse_last_marker!();
eat_slice("..")?;
parse_mark!();
let fourth = parse_last_marker!();
(first, second, third, fourth)
}
#[test]
fn test_simple_marker() {
let result = parse!(simple: Input::new(".....")).unwrap();
assert_eq!(result, (0, 0, 0, 0));
}
#[test]
fn test_updating_marker() {
let result = parse!(simple_updating: Input::new(".....")).unwrap();
assert_eq!(result, (0, 1, 3, 5));
}
#[test]
fn test_resetting_marker() {
let result = parse!(resetting: Input::new(".....")).unwrap();
assert_eq!(result, (0, 1, 1, 5));
}
type TwoSpans<'a> = (Span<'a>, Span<'a>);
#[parser]
fn context<'a>(input: &mut Input<'a>) -> Result<'a, TwoSpans<'a>> {
eat_slice("...")?;
let first = parse_context!();
eat('\n')?;
eat_slice("..")?;
let second = parse_context!();
(first, second)
}
#[parser]
fn resetting_context<'a>(input: &mut Input<'a>) -> Result<'a, TwoSpans<'a>> {
eat_slice("...")?;
let first = parse_context!();
eat('\n')?;
parse_mark!();
eat_slice("..")?;
let second = parse_context!();
(first, second)
}
#[test]
fn test_context() {
let (first, second) = parse!(context: Input::new("...\n..")).unwrap();
assert_eq!(first, Span {
start: (1, 1, 0),
end: (1, 4, 3),
snippet: Some("..."),
cursor: Some('\n'),
});
assert_eq!(second, Span {
start: (1, 1, 0),
end: (2, 3, 6),
snippet: Some("...\n.."),
cursor: None,
});
}
#[test]
fn test_resetting_context() {
let (first, second) = parse!(resetting_context: Input::new("...\n..")).unwrap();
assert_eq!(first, Span {
start: (1, 1, 0),
end: (1, 4, 3),
snippet: Some("..."),
cursor: Some('\n'),
});
assert_eq!(second, Span {
start: (2, 1, 4),
end: (2, 3, 6),
snippet: Some(".."),
cursor: None,
});
}
|