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
// Copyright (c) 2019 Rouven Spreckels <n3vu0r@qu1x.org>
//
// Usage of the works is permitted provided that
// this instrument is retained with the works, so that
// any entity that uses the works is notified of this instrument.
//
// DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.

//! **BML Markup Language**
//!
//! Based on [PEG], see the interactive parser implementation at [pest.rs].
//!
//! [BML] is used by the icarus database of [higan]/[bsnes].
//!
//! [BML]: https://news.ycombinator.com/item?id=8645591
//! [PEG]: https://en.wikipedia.org/wiki/Parsing_expression_grammar
//! [pest.rs]: https://pest.rs/?bin=ov6wy#editor
//! [higan]: https://byuu.org/emulation/higan/
//! [bsnes]: https://byuu.org/emulation/bsnes/
//!
//! # Usage
//!
//! This crate works on Rust stable channel. It is
//! [on crates.io](https://crates.io/crates/bml) and can be used by adding
//! `bml` to the dependencies in your project's `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! bml = "0.2"
//! ```
//!
//! Use `ordered-multimap` feature on Rust nightly channel:
//!
//! ```toml
//! [dependencies]
//! bml = { version = "0.2", features = ["ordered-multimap"] }
//! ```
//!
//! # Examples
//!
//! ```
//! use std::convert::TryFrom;
//!
//! use bml::BmlNode;
//!
//! let root = BmlNode::try_from(concat!(
//! 	"server\n",
//! 	"  path: /core/www/\n",
//! 	"  host: example.com\n",
//! 	"  port: 80\n",
//! 	"  service: true\n",
//! 	"  proxy\n",
//! 	"    host: proxy.example.com\n",
//! 	"    port: 8080\n",
//! 	"    authentication: plain\n",
//! 	"  description\n",
//! 	"    :Primary web-facing server\n",
//! 	"    :Provides commerce-related functionality\n",
//! 	"\n",
//! 	"server\n",
//! 	"  // ...\n",
//! 	"  proxy host=\"proxy.example.com\" port=\"8080\"\n",
//! 	"    authentication: plain\n",
//! )).unwrap();
//!
//! let (name, node) = root.nodes().next().unwrap();
//!
//! assert_eq!(name, "server");
//! assert_eq!(node.named("port").next().unwrap().value(), "80");
//! ```

#![deny(missing_docs)]

#[macro_use]
extern crate err_derive;

#[macro_use]
extern crate pest_derive;

use pest::Parser;
use pest::error::Error;
use pest::iterators::Pair;

pub(crate) mod parser {
	#[derive(Parser)]
	#[grammar = "bml.pest"]
	pub struct BmlParser;
}

use parser::{BmlParser, Rule};

/// BML parser error.
#[derive(Debug, PartialEq, Eq, Clone, Error)]
#[error(display = "Invalid BML\n{}", inner)]
pub struct BmlError {
	inner: Error<Rule>,
}

#[cfg(feature = "ordered-multimap")]
use ordered_multimap::ListOrderedMultimap;
use smallstr::SmallString;
use std::convert::TryFrom;
use std::fmt;

type BmlName = SmallString<[u8; 32]>;
type BmlData = SmallString<[u8; 32]>;

#[derive(Debug, Eq, Clone, Copy)]
enum BmlKind {
	Root { indent: BmlIndent },
	Elem,
	Attr { quote: bool },
}

use BmlKind::*;

impl PartialEq for BmlKind {
	fn eq(&self, other: &BmlKind) -> bool {
		match (self, other) {
			(Root { .. }, Root { .. }) => true,
			(Elem, Elem) => true,
			(Attr { .. }, Attr { .. }) => true,
			_ => false,
		}
	}
}

impl Default for BmlKind {
	fn default() -> Self {
		Root { indent: BmlIndent::default() }
	}
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
struct BmlIndent {
	string: &'static str,
	repeat: usize,
}

impl BmlIndent {
	fn next(mut self) -> Self {
		self.repeat += 1;
		self
	}
}

impl Default for BmlIndent {
	fn default() -> Self {
		Self { string: "  ".into(), repeat: 0 }
	}
}

impl fmt::Display for BmlIndent {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "{}", self.string.repeat(self.repeat))
	}
}

/// BML node comprising data `lines()` and child `nodes()`.
///
/// By design, attributes are considered child nodes as well but carry a flag
/// marking them as attributes for serialization purpose only.
#[derive(Debug, Eq, Clone, Default)]
pub struct BmlNode {
	kind: BmlKind,
	data: BmlData,
	#[cfg(feature = "ordered-multimap")]
	node: ListOrderedMultimap<BmlName, BmlNode>,
	#[cfg(not(feature = "ordered-multimap"))]
	node: Vec<(BmlName, BmlNode)>,
}

impl BmlNode {
	/// Value comprising data lines with `'\n'` removed from last line.
	pub fn value(&self) -> &str {
		&self.data[..self.data.len() - 1]
	}
	/// Iterator over data lines.
	pub fn lines(&self) -> impl DoubleEndedIterator<Item = &str> {
		self.data.lines()
	}
	/// Iterator over child nodes as `(name, node)` tuples.
	pub fn nodes(&self)
	-> impl DoubleEndedIterator<Item = (&str, &BmlNode)> + ExactSizeIterator {
		self.node.iter().map(|(name, node)| (name.as_str(), node))
	}
	/// Iterator over child nodes of `name`.
	///
	/// **NOTE**: Verify with `cargo test --features ordered-multimap`.
	///
	/// Complexity: *O(m)* where *m* is the number of nodes matching `name`.
	#[cfg(feature = "ordered-multimap")]
	pub fn named(&self, name: &str)
	-> impl DoubleEndedIterator<Item = &BmlNode> + ExactSizeIterator {
		self.node.get_all(name)
	}
	/// Iterator over child nodes of `name`.
	///
	/// **NOTE**: Fallback implementation for stable Rust using `Vec` instead
	/// of `ordered_multimap::ListOrderedMultimap`. On nightly Rust enable the
	/// `ordered-multimap` feature to reduce the complexity to *O(m)* where *m*
	/// is the number of nodes matching `name`.
	///
	/// Complexity: *O(n)* where *n* is the total number of child nodes.
	#[cfg(not(feature = "ordered-multimap"))]
	pub fn named(&self, name: &str)
	-> impl DoubleEndedIterator<Item = &BmlNode> {
		let name = BmlName::from(name);
		self.node.iter().filter_map(move |(key, value)|
			if key == &name { Some(value) } else { None })
	}
	/// Indent `string` of child nodes and level as `repeat` times `string`.
	///
	/// Default is two spaces as in `"  "` and no root indent (`0`).
	/// Usual alternative is a tabulator as in `"\t"` and no root indent (`0`).
	pub fn set_indent(&mut self, string: &'static str, repeat: usize) {
		match self.kind {
			Root { ref mut indent } => *indent = BmlIndent { string, repeat },
			_ => panic!("BML indent can be set for root node only"),
		}
	}
	fn root() -> Self {
		Self { kind: Root { indent: BmlIndent::default() }, ..Self::default() }
	}
	fn elem() -> Self {
		Self { kind: Elem, ..Self::default() }
	}
	fn attr() -> Self {
		Self { kind: Attr { quote: true }, ..Self::default() }
	}
	fn append(&mut self, (name, node): (BmlName, BmlNode)) {
		#[cfg(feature = "ordered-multimap")]
		self.node.append(name, node);
		#[cfg(not(feature = "ordered-multimap"))]
		self.node.push((name, node));
	}
	fn serialize(&self, f: &mut fmt::Formatter,
		name: &str, indent: BmlIndent,
	) -> fmt::Result {
		match self.kind {
			Root { indent } => {
				let mut nodes = self.nodes().peekable();
				while let Some((name, node)) = nodes.next() {
					node.serialize(f, name, indent)?;
					if nodes.peek().is_some() {
						writeln!(f)?;
					}
				}
			},
			Elem => {
				write!(f, "{}{}", indent, name)?;
				let indent = indent.next();
				let mut attrs = 0;
				for (name, attr) in self.nodes().take_while(|(_name, node)|
					if let Attr { .. } = node.kind
						{ true} else { false }
				) {
					attrs += 1;
					attr.serialize(f, name, indent)?;
				}
				let mut lines = self.lines();
				let line0 = lines.next();
				let line1 = lines.next();
				if attrs == 0 && line0.is_some() && line1.is_none() {
					writeln!(f, ": {}", line0.unwrap())?;
				} else {
					writeln!(f)?;
					for line in self.lines() {
						writeln!(f, "{}:{}", indent, line)?;
					}
				}
				for (name, elem) in self.nodes().skip(attrs) {
					elem.serialize(f, name, indent)?;
				}
			},
			Attr { quote } => {
				write!(f, " {}", name)?;
				if let Some(line) = self.lines().next() {
					if quote {
						write!(f, "=\"{}\"", line)?;
					} else {
						write!(f, "={}", line)?;
					}
				}
			},
		}
		Ok(())
	}
}

impl PartialEq for BmlNode {
	fn eq(&self, other: &BmlNode) -> bool {
		self.data == other.data && self.node == other.node
	}
}

impl TryFrom<&str> for BmlNode {
	type Error = BmlError;

	fn try_from(bml: &str) -> Result<Self, Self::Error> {
		fn parse_node(pair: Pair<Rule>) -> (BmlName, BmlNode) {
			let mut name = BmlName::new();
			let mut node = BmlNode::elem();
			for pair in pair.into_inner() {
				match pair.as_rule() {
					Rule::name => name = pair.as_str().into(),
					Rule::data => {
						node.data.push_str(pair.into_inner().as_str());
						node.data.push('\n');
					},
					Rule::attr => {
						let mut name = BmlName::new();
						let mut attr = BmlNode::attr();
						for pair in pair.into_inner() {
							match pair.as_rule() {
								Rule::name => name = pair.as_str().into(),
								Rule::data => for data in pair.into_inner() {
									if data.as_rule() == Rule::space_data_inner
										{ attr.kind = Attr { quote: false } };
									attr.data.push_str(data.as_str());
									attr.data.push('\n');
								},
								_ => unreachable!(),
							}
						}
						node.append((name, attr));
					},
					Rule::node => node.append(parse_node(pair)),
					_ => unreachable!(),
				}
			}
			(name, node)
		}

		let mut root = BmlNode::root();
		for pair in BmlParser::parse(Rule::root, &bml)
			.map_err(|inner| BmlError { inner })?
		{
			match pair.as_rule() {
				Rule::node => root.append(parse_node(pair)),
				Rule::EOI => (),
				_ => unreachable!(),
			}
		}
		Ok(root)
	}
}

impl fmt::Display for BmlNode {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.serialize(f, "", BmlIndent::default())
	}
}