(tk: &mut Tokenizer, t: Token, ctx: &LoadCtx)
| 30 | } |
| 31 | |
| 32 | fn parse_value_with(tk: &mut Tokenizer, t: Token, ctx: &LoadCtx) -> Result<Handle> { |
| 33 | match t { |
| 34 | Token::Null => encode(Value::None), |
| 35 | Token::True => encode(Value::Bool(true)), |
| 36 | Token::False => encode(Value::Bool(false)), |
| 37 | Token::Int(i, src) => { |
| 38 | if let Some(hook) = &ctx.parse_int { |
| 39 | let arg = encode(Value::Bytes(src.into_bytes()))?; |
| 40 | hook.call("__call__", &[arg.raw()]) |
| 41 | } else { |
| 42 | encode(Value::Int(i)) |
| 43 | } |
| 44 | } |
| 45 | Token::Float(f, src) => { |
| 46 | if let Some(hook) = &ctx.parse_float { |
| 47 | let arg = encode(Value::Bytes(src.into_bytes()))?; |
| 48 | hook.call("__call__", &[arg.raw()]) |
| 49 | } else { |
| 50 | encode(Value::Float(f)) |
| 51 | } |
| 52 | } |
| 53 | Token::Constant(name) => { |
| 54 | if let Some(hook) = &ctx.parse_constant { |
| 55 | let arg = encode(Value::Bytes(name.into_bytes()))?; |
| 56 | hook.call("__call__", &[arg.raw()]) |
| 57 | } else { |
| 58 | encode(Value::Float(match name.as_str() { |
| 59 | "NaN" => f64::NAN, |
| 60 | "Infinity" => f64::INFINITY, |
| 61 | "-Infinity" => f64::NEG_INFINITY, |
| 62 | _ => return Err(value_err(tk.pos(), "unknown constant")), |
| 63 | })) |
| 64 | } |
| 65 | } |
| 66 | Token::Str(s) => encode(Value::Bytes(s.into_bytes())), |
| 67 | Token::LBracket => parse_array(tk, ctx), |
| 68 | Token::LBrace => parse_object(tk, ctx), |
| 69 | Token::RBracket | Token::RBrace | Token::Comma | Token::Colon => { |
| 70 | Err(value_err(tk.pos(), "unexpected token")) |
| 71 | } |
| 72 | Token::Eof => Err(value_err(tk.pos(), "unexpected end of input")), |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | fn parse_array(tk: &mut Tokenizer, ctx: &LoadCtx) -> Result<Handle> { |
| 77 | let list = Handle::new_list()?; |
no test coverage detected