conf_json 0.1.4

A human editable configure file in JSON parser
Documentation
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! The parser that do the real work
//!

use std::io;
use std::fmt::{self, Display, Debug};
use std::error::Error;
use std::str::FromStr;

use crate::value::{ArrayType, ObjectType, Value};

/// A error type that indicate lines and columns when parsing goes wrong
#[derive(Debug, Clone)]
pub struct ParseError {
	msg: String,
	line: usize,
	col: usize
}

impl Display for ParseError {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
		write!(f, "\"{}\" at line {} col {}", self.msg, self.line, self.col)
	}
}

impl Error for ParseError {}

/// Alias for a parsing `Result` with the error type ParseError
pub type ParseResult = Result<(), ParseError>;

/// N must able to hold the longest keyword, for standard json which has keywords
/// null true false, hence N >= 5 is good enough
const N: usize = 5;

#[derive(Debug)]
pub struct Parser<T> {
	buf: [u8; N],
	cur_pos: usize,
	end_pos: usize,
	eof: bool,
	lines: usize,
	cols: usize,
	src: T
}

impl<T: io::Read + Debug> Parser<T> {
	pub fn new(src: T) -> Self {
		Parser {
			buf: [b'\0'; N],
			cur_pos: 0,
			end_pos: 0,
			lines: 0,
			cols: 0,
			eof: false,
			src: src
		}
	}

	/// Raise a ParseError when parsing goes wrong with a brief description and
	/// position to tell why and where
	fn raise(&self, msg: &str) -> ParseError {
		ParseError {
			msg: msg.to_string(),
			line: self.lines,
			col: self.cols
		}
	}

	/// The loaded but not-yet parsed data size in inner buffer
	fn available(&self) -> usize {
		self.end_pos - self.cur_pos
	}

	/// If there is not enough data in inner buffer, pump as much as possible
	/// from underlying data source
	fn pump(&mut self, n: usize) {
		if self.available() < n {
			// the implement will make sure following assertion pass
			assert!(n <= self.buf.len());

			if !self.eof {
				// roll remaining data to the begining of inner buffer
				self.buf.rotate_left(self.cur_pos);
				self.end_pos -= self.cur_pos;
				self.cur_pos = 0;

				// try to fill inner buffer, pump as much as possible
				let r = self.src.read(&mut self.buf[self.end_pos..]);
				if let Ok(rd) = r {
					if rd == 0 {
						self.eof = true;
					} else {
						self.end_pos += rd;
					}
				} else {
					panic!("data source reading error");
				}
			}
		}
	}

	/// Pop parsed char, this is the only way to move internal cursor to next char
	/// it will also update the current lines and columns numbers so we can know
	/// when and where if parsing gose wrong
	fn pop(&mut self, n: usize) {
		// the implement will make sure following assertion pass
		assert!(n <= self.available());

		// this is the only way to `mark` each char as parsed, thus it's a good place
		// to record line and column numbers
		for i in self.cur_pos..self.cur_pos + n {
			if self.buf[i] == b'\n' {
				self.lines += 1;
				self.cols = 0;
			} else {
				self.cols += 1;
			}
		}

		self.cur_pos += n;
	}

	/// Get current char without moving the cursor, `None` if reach EOF
	fn peek(&mut self) -> Option<u8> {
		self.pump(1);
		if self.cur_pos == self.end_pos {
			None
		} else {
			Some(self.buf[self.cur_pos])
		}
	}

	/// Compare a ascii string in inner buffer, it won't move the cursor
	fn peek_match(&mut self, v: &[u8]) -> bool {
		self.pump(v.len());
		self.available() >= v.len() && v == &self.buf[self.cur_pos..self.cur_pos + v.len()]
	}

	/// Skip chars by predicate `F`, it will move the cursor by calling `pop()`
	fn skip_by<F>(&mut self, test: F) where F: Fn(u8) -> bool {
		while let Some(c) = self.peek() {
			if !test(c) {
				break
			} else {
				self.pop(1);
			}
		}
	}

	/// Skip chars until we meat the first `stop` char
	fn skip_to(&mut self, stop: u8) {
		self.skip_by(|c| { c != stop })
	}

	/// Skip spaces and comments to next meaningful char
	fn skip_to_next(&mut self) {
		loop {
			// skip spaces
			self.skip_by(|c| { c.is_ascii_whitespace() });

			// check the first char after spaces
			// '#' means comments so we skip to the end of current line
			if let Some(c) = self.peek() {
				if c == b'#' {
					self.skip_to(b'\n');
				} else {
					break;
				}
			} else {
				break;
			}
		}
		// now, cur_pos points to next valid char or EOF
	}

	/// This is the main entrance of parsing, by checking the leading char,
	/// different parsing method for each `Value` type were called respectively
	pub fn parse(&mut self) -> Result<Value, ParseError> {
		self.skip_to_next();
		if let Some(c) = self.peek() {
			match c {
				b'{' => self.parse_object(),
				b'[' => self.parse_array(),
				b'\'' | b'"' => self.parse_string(),
				_ => {
					if self.peek_match(b"true") {
						self.pop(4);
						Ok(Value::Bool(true))
					} else if self.peek_match(b"false") {
						self.pop(5);
						Ok(Value::Bool(false))
					} else if self.peek_match(b"null") {
						self.pop(4);
						Ok(Value::Null)
					} else {
						self.parse_number()
					}
				}
			}
		} else {
			Err(self.raise("not enough data"))
		}
	}

	/// Parse `Value::Object`, the trailing comma is allowed
	fn parse_object(&mut self) -> Result<Value, ParseError> {
		assert_eq!(self.peek(), Some(b'{'));
		self.pop(1);

		let mut obj = ObjectType::new();
		loop {
			// read item key
			self.skip_to_next();
			if let Some(c) = self.peek() {
				if c == b'}' {
					self.pop(1);
					return Ok(Value::Object(obj));
				} else if c == b'\'' || c == b'"' {
				} else {
					return Err(self.raise("object: key expecting ' or \""));
				}
			} else {
				return Err(self.raise("object: key expecting more data"));
			}
			let k = self.parse_string_raw()?;

			// read kv delimiter :
			self.skip_to_next();
			if self.peek() != Some(b':') {
				return Err(self.raise("object: expecting \":\""));
			}
			self.pop(1);

			// read item value
			self.skip_to_next();
			let v = self.parse()?;

			obj.insert(k, v);

			// read item delimiter , or end }
			// trailing , allowed, eg { k: v, kk: vv, } but not {,}
			self.skip_to_next();
			if let Some(c) = self.peek() {
				if c == b',' {
					self.pop(1);
				} else if c == b'}' {
					self.pop(1);
					return Ok(Value::Object(obj));
				} else {
					return Err(self.raise("object: bad item delimeter, expecting , or }"));
				}
			} else {
				return Err(self.raise("object: expecting , or }"));
			}
		}
	}

	/// Parse `Value::Array`, the trailing comma is allowed
	fn parse_array(&mut self) -> Result<Value, ParseError> {
		assert_eq!(self.peek(), Some(b'['));
		self.pop(1);

		let mut arr = ArrayType::new();
		loop {
			self.skip_to_next();
			if Some(b']') == self.peek() {
				self.pop(1);
				return Ok(Value::Array(arr));
			}
			arr.push(self.parse()?);

			// read array item delimiter , or end ]
			// trailing , allowed eg [aaa, bbb, ], but not [,]
			self.skip_to_next();
			if let Some(c) = self.peek() {
				if c == b',' {
					self.pop(1);
				} else if c == b']' {
					self.pop(1);
					return Ok(Value::Array(arr));
				}
			} else {
				return Err(self.raise("array: expecting , or ]"));
			}
		}
	}

	/// Extract `String`, this method can be called by `parse_object()` for pasing object item key name
	/// and `parse_string()`, both singal and double quotation marks are allowed
	fn parse_string_raw(&mut self) -> Result<String, ParseError> {
		// save quotation mark ' or "
		assert!(self.peek() == Some(b'"') || self.peek() == Some(b'\''));
		let quoter = self.peek().unwrap();
		self.pop(1);

		let mut v: Vec<u8> = Vec::new();
		let mut esc = false;
		while let Some(c) = self.peek() {
			if esc {
				match c {
					b't' => v.push(b'\t'),
					b'r' => v.push(b'\r'),
					b'n' => v.push(b'\n'),
					_ => v.push(c),
				}
				esc = false;
			} else if c == b'\\' {
				esc = true;
			} else if c == quoter {
				self.pop(1);
				return if let Ok(s) = String::from_utf8(v) {
					Ok(s)
				} else {
					Err(self.raise("string: bad utf-8 encode"))
				}
			} else {
				v.push(c);
			}
			self.pop(1)
		}
		Err(self.raise("string: expecting more data"))
	}

	/// Parse `Value::String` by calling `parse_string_raw()`
	fn parse_string(&mut self) -> Result<Value, ParseError> {
		Ok(Value::String(self.parse_string_raw()?))
	}

	/// Check number car validation according to radix, for radix 2, '0' and '1' are allowed,
	/// for radix 8, '0' ~ '7', etc
	fn is_valid_number_char(c: u8, radix: u32) -> bool {
		match radix {
			2 => c >= b'0' && c <= b'1',
			8 => c >= b'0' && c <= b'7',
			10 => c >= b'0' && c <= b'9',
			16 => c >= b'0' && c <= b'9' || c >= b'a' && c <= b'f' || c >= b'A' && c <= b'F',
			_ => false,
		}
	}

	/// Parse `Value::Number` they were store with type f64, thers are some valid forms:
	///
	/// 123, -123, 123.456, 0x00ff, 0XAA, 123E4, 123e-4, 123E+4
	fn parse_number(&mut self) -> Result<Value, ParseError> {
		#[derive(PartialEq)]
		enum Phase {
			Sign,
			Radix,
			Int,
			Float,
			SciSign,
			Sci
		}

		let mut v: Vec<u8> = Vec::new();
		let mut ph = Phase::Sign;
		let mut radix = 10;

		while let Some(c) = self.peek() {
			match ph {
				Phase::Sign => {
					if b'-' == c || b'+' == c {
						v.push(c);
						self.pop(1);
					} else if c.is_ascii_digit() {
					} else {
						return Err(self.raise("number: bad leading char, expecting \"+-[0-9]\""))
					}
					ph = Phase::Radix;
				}
				Phase::Radix => {
					if self.peek_match(b"0b") || self.peek_match(b"0B") {
						self.pop(2);
						radix = 2;
					} else if self.peek_match(b"0o") || self.peek_match(b"0O") {
						self.pop(2);
						radix = 8;
					} else if self.peek_match(b"0x") || self.peek_match(b"0X") {
						self.pop(2);
						radix = 16;
					}
					ph = Phase::Int;
				}
				Phase::Int => {
					if b'.' == c {
						if radix != 10 {
							return Err(self.raise(&format!("number: bad float parts for radix {}", radix)))
						}
						v.push(c);
						self.pop(1);
						ph = Phase::Float;
					} else if Self::is_valid_number_char(c, radix) {
						v.push(c);
						self.pop(1);
					} else if radix == 10 && (b'e' == c || b'E' == c) {
						v.push(c);
						self.pop(1);
						ph = Phase::SciSign;
					} else {
						break;
					}
				}
				Phase::Float => {
					assert_eq!(radix, 10);
					if Self::is_valid_number_char(c, 10) {
						v.push(c);
						self.pop(1);
					} else if b'e' == c || b'E' == c {
						v.push(c);
						self.pop(1);
						ph = Phase::SciSign;
					} else {
						break;
					}
				}
				Phase::SciSign => {
					if b'-' == c || b'+' == c {
						v.push(c);
						self.pop(1);
					}
					ph = Phase::Sci;
				}
				Phase::Sci => {
					if Self::is_valid_number_char(c, 10) {
						v.push(c);
						self.pop(1);
					} else {
						break;
					}
				}
			}
		}

		if v.is_empty() {
			Err(self.raise("number: expecting more data"))
		} else {
			if let Ok(s) = String::from_utf8(v) {
				if radix != 10 {
					if let Ok(v) = i64::from_str_radix(&s, radix) {
						Ok(Value::Number(v as f64))
					} else {
						Err(self.raise("number: bad i64 string"))
					}
				} else {
					if let Ok(v) = f64::from_str(&s) {
						Ok(Value::Number(v))
					} else {
						Err(self.raise("number: bad f64 string"))
					}
				}
			} else {
				Err(self.raise("number: bad utf-8 encoding"))
			}
		}
	}
}