Skip to content

Commit

Permalink
Merge pull request #75 from yeslogic/brendanzab/cond-types
Browse files Browse the repository at this point in the history
Add cond types
  • Loading branch information
brendanzab authored Dec 4, 2017
2 parents e5fdb9f + c5b01d5 commit 23902ce
Show file tree
Hide file tree
Showing 24 changed files with 1,923 additions and 258 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ keywords = ["binary", "byte", "parser", "parsing", "endian", "big-endian", "litt
members = [
"ddl-util",
"examples/formats/bitmap",
"examples/formats/bson",
"examples/formats/edid",
"examples/formats/object_id",
"examples/formats/stl",
Expand Down
16 changes: 16 additions & 0 deletions ddl-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ extern crate byteorder;
use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
use std::io::{self, Read};

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Never {}

#[inline]
pub fn empty() -> io::Result<()> {
Ok(())
}

#[inline]
pub fn error<T>() -> io::Result<T> {
Err(io::Error::new(
io::ErrorKind::InvalidData,
"Invalid binary data",
))
}

#[inline]
pub fn from_u8<R: Read>(r: &mut R) -> io::Result<u8> {
r.read_u8()
Expand Down
11 changes: 11 additions & 0 deletions examples/formats/bson/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "ddl-bson"
version = "0.0.0"
license = "Apache-2.0"
publish = false

[dependencies]
ddl-util = { path = "../../../ddl-util" }

[build-dependencies]
ddl = { path = "../../.." }
26 changes: 26 additions & 0 deletions examples/formats/bson/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extern crate ddl;

use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::str::FromStr;

use ddl::syntax::ast::Program;

fn main() {
let src = {
let mut src_file = File::open("src/bson.ddl").unwrap();
let mut src = String::new();
src_file.read_to_string(&mut src).unwrap();
src
};

let mut program = Program::from_str(&src).unwrap();
program.substitute(&ddl::syntax::ast::base_defs());
ddl::syntax::check::check_program(&program).unwrap();
let ir = ddl::ir::ast::Program::from(&program);

let out_dir = env::var("OUT_DIR").unwrap();
let mut file = File::create(out_dir + "/bson.rs").unwrap();
write!(file, "{}", ddl::codegen::LowerProgram(&ir)).unwrap();
}
85 changes: 85 additions & 0 deletions examples/formats/bson/src/bson.ddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/// Binary data
BinData = struct {
/// The number of bytes stored
len : i32be,
/// Binary data subtype
///
/// - `0x00`: Generic binary subtype
/// - `0x01`: Function
/// - `0x02`: Binary (Old)
/// - `0x03`: UUID (Old)
/// - `0x04`: UUID
/// - `0x05`: MD5
/// - `0x80`: User defined
subtype : u8,
/// The binary data
data: [u8; len as u64],
};

ObjectId = struct {
epoch_time: i32be,
machine_id: u24be,
process_id: u16be,
counter: u24be,
};

Element = struct {
elem_type : u8,
content: cond {
/// 64-bit floating point
double : elem_type == 0x01u8 => f64le,
// TODO: strings
// /// UTF8 string
// string : elem_type == 0x02u8 => String,
// FIXME: mutual recursion
// /// Embedded document
// document : elem_type == 0x03u8 => Document,
// /// Array
// array : elem_type == 0x04u8 => Array,
/// Binary data
bin_data : elem_type == 0x05u8 => BinData,
/// Undefined (value) - deprecated
undefined : elem_type == 0x06u8 => empty,
/// ObjectId
object_id : elem_type == 0x07u8 => ObjectId,
/// Boolean
///
/// - `0x00`: false
/// - `0x01`: true
boolean : elem_type == 0x08u8 => u8,
/// UTC datetime
utc_datetime : elem_type == 0x09u8 => i64le,
/// Null value
null : elem_type == 0x0au8 => empty,
// TODO: Regular Expression
// reg_ex : elem_type == 0x0bu8 => RegEx,
// TODO: DBPointer — Deprecated
// db_pointer : elem_type == 0x0cu8 => DbPointer,
// TODO: String
// javascript : elem_type == 0x0du8 => String,
// TODO: Symbol. Deprecated
// symbol : elem_type == 0x0eu8 => Symbol,
// TODO: JavaScript code w/ scope
// code_with_scope : elem_type == 0x0fu8 => CodeWithScope,
/// 32-bit integer
int : elem_type == 0x10u8 => i32le,
/// Timestamp
timestamp : elem_type == 0x11u8 => u64le,
/// 64-bit integer
long : elem_type == 0x12u8 => i64le,
// TODO: 128-bit decimal floating point
// number_decimal : elem_type == 0x13u8 => NumberDecimal,
},
};

/// BSON Document
///
/// <http://bsonspec.org/spec.html>
Document = struct {
/// Total number of bytes comprising the document.
len : i32be,
/// The elements that make up this document
fields : [Element; 1u8],
// /// Terminating byte signalling the end of the file
// terminator : u8 where u8 == 0x00,
};
1 change: 1 addition & 0 deletions examples/formats/bson/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include!(concat!(env!("OUT_DIR"), "/bson.rs"));
Loading

0 comments on commit 23902ce

Please sign in to comment.