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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
//! This is a parser for JSON.
//! Run it with the following command:
//! cargo run --example json -- examples/sample.json
use ariadne::{Color, Fmt, Label, Report, ReportKind, Source};
use chumsky::prelude::*;
use std::{collections::HashMap, env, fs};
#[derive(Clone, Debug)]
enum Json {
Invalid,
Null,
Bool(bool),
Str(String),
Num(f64),
Array(Vec<Json>),
Object(HashMap<String, Json>),
}
fn parser() -> impl Parser<char, Json, Error = Simple<char>> {
recursive(|value| {
let frac = just('.').chain(text::digits(10));
let exp = just('e')
.or(just('E'))
.chain(just('+').or(just('-')).or_not())
.chain::<char, _, _>(text::digits(10));
let number = just('-')
.or_not()
.chain::<char, _, _>(text::int(10))
.chain::<char, _, _>(frac.or_not().flatten())
.chain::<char, _, _>(exp.or_not().flatten())
.collect::<String>()
.from_str()
.unwrapped()
.labelled("number");
let escape = just('\\').ignore_then(
just('\\')
.or(just('/'))
.or(just('"'))
.or(just('b').to('\x08'))
.or(just('f').to('\x0C'))
.or(just('n').to('\n'))
.or(just('r').to('\r'))
.or(just('t').to('\t'))
.or(just('u').ignore_then(
filter(|c: &char| c.is_digit(16))
.repeated()
.exactly(4)
.collect::<String>()
.validate(|digits, span, emit| {
char::from_u32(u32::from_str_radix(&digits, 16).unwrap())
.unwrap_or_else(|| {
emit(Simple::custom(span, "invalid unicode character"));
'\u{FFFD}' // unicode replacement character
})
}),
)),
);
let string = just('"')
.ignore_then(filter(|c| *c != '\\' && *c != '"').or(escape).repeated())
.then_ignore(just('"'))
.collect::<String>()
.labelled("string");
let array = value
.clone()
.chain(just(',').ignore_then(value.clone()).repeated())
.or_not()
.flatten()
.delimited_by(just('['), just(']'))
.map(Json::Array)
.labelled("array");
let member = string.clone().then_ignore(just(':').padded()).then(value);
let object = member
.clone()
.chain(just(',').padded().ignore_then(member).repeated())
.or_not()
.flatten()
.padded()
.delimited_by(just('{'), just('}'))
.collect::<HashMap<String, Json>>()
.map(Json::Object)
.labelled("object");
just("null")
.to(Json::Null)
.labelled("null")
.or(just("true").to(Json::Bool(true)).labelled("true"))
.or(just("false").to(Json::Bool(false)).labelled("false"))
.or(number.map(Json::Num))
.or(string.map(Json::Str))
.or(array)
.or(object)
.recover_with(nested_delimiters('{', '}', [('[', ']')], |_| Json::Invalid))
.recover_with(nested_delimiters('[', ']', [('{', '}')], |_| Json::Invalid))
.recover_with(skip_then_retry_until(['}', ']']))
.padded()
})
.then_ignore(end().recover_with(skip_then_retry_until([])))
}
fn main() {
let src = fs::read_to_string(env::args().nth(1).expect("Expected file argument"))
.expect("Failed to read file");
let (json, errs) = parser().parse_recovery(src.trim());
println!("{:#?}", json);
errs.into_iter().for_each(|e| {
let msg = if let chumsky::error::SimpleReason::Custom(msg) = e.reason() {
msg.clone()
} else {
format!(
"{}{}, expected {}",
if e.found().is_some() {
"Unexpected token"
} else {
"Unexpected end of input"
},
if let Some(label) = e.label() {
format!(" while parsing {}", label)
} else {
String::new()
},
if e.expected().len() == 0 {
"something else".to_string()
} else {
e.expected()
.map(|expected| match expected {
Some(expected) => expected.to_string(),
None => "end of input".to_string(),
})
.collect::<Vec<_>>()
.join(", ")
},
)
};
let report = Report::build(ReportKind::Error, (), e.span().start)
.with_code(3)
.with_message(msg)
.with_label(
Label::new(e.span())
.with_message(match e.reason() {
chumsky::error::SimpleReason::Custom(msg) => msg.clone(),
_ => format!(
"Unexpected {}",
e.found()
.map(|c| format!("token {}", c.fg(Color::Red)))
.unwrap_or_else(|| "end of input".to_string())
),
})
.with_color(Color::Red),
);
let report = match e.reason() {
chumsky::error::SimpleReason::Unclosed { span, delimiter } => report.with_label(
Label::new(span.clone())
.with_message(format!(
"Unclosed delimiter {}",
delimiter.fg(Color::Yellow)
))
.with_color(Color::Yellow),
),
chumsky::error::SimpleReason::Unexpected => report,
chumsky::error::SimpleReason::Custom(_) => report,
};
report.finish().print(Source::from(&src)).unwrap();
});
}
|