Enum json_syntax::parse::Context
source · pub enum Context {
None,
Array,
ObjectKey,
ObjectValue,
}
Expand description
Parsing context.
Defines what characters are allowed after a value.
Variants§
Implementations§
source§impl Context
impl Context
sourcepub fn follows(&self, c: char) -> bool
pub fn follows(&self, c: char) -> bool
Checks if the given character c
can follow a value in this context.
Examples found in repository?
src/parse.rs (line 307)
303 304 305 306 307 308 309 310 311 312 313 314
fn skip_trailing_whitespaces(&mut self, context: Context) -> Result<(), Meta<Error<M, E>, M>> {
self.skip_whitespaces()?;
if let Some(c) = self.peek_char()? {
if !context.follows(c) {
// panic!("unexpected {:?} in {:?}", c, context);
return Err(Meta(Error::unexpected(Some(c)), self.position.last()));
}
}
Ok(())
}
More examples
src/parse/number.rs (line 49)
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
fn parse_spanned<C, F, E>(
parser: &mut Parser<C, F, E>,
context: Context,
) -> Result<Meta<Self, Span>, Meta<Error<M, E>, M>>
where
C: Iterator<Item = Result<DecodedChar, E>>,
F: FnMut(Span) -> M,
{
let mut buffer: SmallVec<[u8; SMALL_STRING_CAPACITY]> = SmallVec::new();
enum State {
Init,
FirstDigit,
Zero,
NonZero,
FractionalFirst,
FractionalRest,
ExponentSign,
ExponentFirst,
ExponentRest,
}
let mut state = State::Init;
while let Some(c) = parser.peek_char()? {
match state {
State::Init => match c {
'-' => state = State::FirstDigit,
'0' => state = State::Zero,
'1'..='9' => state = State::NonZero,
_ => return Err(Meta(Error::unexpected(Some(c)), parser.position.last())),
},
State::FirstDigit => match c {
'0' => state = State::Zero,
'1'..='9' => state = State::NonZero,
_ => return Err(Meta(Error::unexpected(Some(c)), parser.position.last())),
},
State::Zero => match c {
'.' => state = State::FractionalFirst,
'e' | 'E' => state = State::ExponentSign,
_ => {
if context.follows(c) {
break;
} else {
return Err(Meta(Error::unexpected(Some(c)), parser.position.last()));
}
}
},
State::NonZero => match c {
'0'..='9' => state = State::NonZero,
'.' => state = State::FractionalFirst,
'e' | 'E' => state = State::ExponentSign,
_ => {
if context.follows(c) {
break;
} else {
return Err(Meta(Error::unexpected(Some(c)), parser.position.last()));
}
}
},
State::FractionalFirst => match c {
'0'..='9' => state = State::FractionalRest,
_ => return Err(Meta(Error::unexpected(Some(c)), parser.position.last())),
},
State::FractionalRest => match c {
'0'..='9' => state = State::FractionalRest,
'e' | 'E' => state = State::ExponentSign,
_ => {
if context.follows(c) {
break;
} else {
return Err(Meta(Error::unexpected(Some(c)), parser.position.last()));
}
}
},
State::ExponentSign => match c {
'+' | '-' => state = State::ExponentFirst,
'0'..='9' => state = State::ExponentRest,
_ => return Err(Meta(Error::unexpected(Some(c)), parser.position.last())),
},
State::ExponentFirst => match c {
'0'..='9' => state = State::ExponentRest,
_ => return Err(Meta(Error::unexpected(Some(c)), parser.position.last())),
},
State::ExponentRest => match c {
'0'..='9' => state = State::ExponentRest,
_ => {
if context.follows(c) {
break;
} else {
return Err(Meta(Error::unexpected(Some(c)), parser.position.last()));
}
}
},
}
// u8 conversion is safe since a number is composed of ASCII chars.
buffer.push(c as u8);
parser.next_char()?;
}
if matches!(
state,
State::Zero | State::NonZero | State::FractionalRest | State::ExponentRest
) {
Ok(Meta(
unsafe { NumberBuf::new_unchecked(buffer) },
parser.position.current_span(),
))
} else {
Err(Meta(Error::unexpected(None), parser.position.last()))
}
}
Trait Implementations§
source§impl PartialEq<Context> for Context
impl PartialEq<Context> for Context
impl Copy for Context
impl Eq for Context
impl StructuralEq for Context
impl StructuralPartialEq for Context
Auto Trait Implementations§
impl RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl UnwindSafe for Context
Blanket Implementations§
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.