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
use std::usize; #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub struct Span { pub lo: usize, pub hi: usize, } impl Span { pub fn extend(self, other: Span) -> Span { Span { lo: self.lo, hi: other.hi, } } } pub const EMPTY_SPAN: Span = Span { lo: 0, hi: 0 }; pub const DUMMY_SPAN: Span = Span { lo: usize::MAX, hi: usize::MAX }; #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub struct Spanned<T> { pub node: T, pub span: Span, } #[cfg(feature = "parsing")] #[macro_use] mod parsing { #[macro_export] macro_rules! spanned { ($i:expr, $submac:ident!( $($args:tt)* )) => { match $submac!($i, $($args)*) { ::synom::IResult::Done(rest, node) => { let span = $crate::Span { lo: ::synom::space::skip_whitespace($i).idx(), hi: rest.idx() }; ::synom::IResult::Done(rest, $crate::Spanned { node: node, span: span }) } ::synom::IResult::Error => ::synom::IResult::Error, } }; ($i:expr, $f:expr) => { spanned!($i, call!($f)) }; } #[macro_export] macro_rules! add_span { ($i:expr, $submac:ident!( $($args:tt)* )) => { match $submac!($i, $($args)*) { ::synom::IResult::Done(rest, mut val) => { assert_eq!(val.span, $crate::DUMMY_SPAN); val.span = $crate::Span { lo: ::synom::space::skip_whitespace($i).idx(), hi: rest.idx() }; ::synom::IResult::Done(rest, val) } ::synom::IResult::Error => ::synom::IResult::Error, } }; ($i:expr, $f:expr) => { spanned!($i, call!($f)) }; } }