Skip to content

Commit

Permalink
Merge pull request #39 from yeslogic/brendanzab/rework-builtins
Browse files Browse the repository at this point in the history
Rework primitive types and built-ins
  • Loading branch information
brendanzab authored Nov 20, 2017
2 parents 99da98c + a59bb17 commit c26e444
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 123 deletions.
96 changes: 47 additions & 49 deletions examples/ddl/cmap.ddl
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
//! endian: be

Offset32 = u32;
Offset32 = u32be;

/// https://www.microsoft.com/typography/otspec/cmap.htm
CMap = struct {
/// Table version number (0).
version: u16,
version: u16be,
/// Number of encoding tables that follow.
numTables: u16,
numTables: u16be,
encodingRecords: [EncodingRecord; numTables],
};

EncodingRecord = struct {
/// Platform ID.
platformID: u16,
platformID: u16be,
/// Platform-specific encoding ID.
encodingID: u16,
encodingID: u16be,
/// Byte offset from beginning of table to the subtable for this encoding.
offset: Offset32,
};
Expand All @@ -35,11 +33,11 @@ CMapSubtable = union {
/// Format 0: Byte encoding table
Format0 = struct {
/// Format number is set to 0.
format: u16 where format => format == 0,
format: u16be where format => format == 0,
/// This is the length in bytes of the subtable.
length: u16,
length: u16be,
/// Please see “Note on the language field in 'cmap' subtables“ in this document.
language: u16,
language: u16be,
/// An array that maps character codes to glyph index values.
glyphIdArray: [u8; 256],
};
Expand All @@ -51,36 +49,36 @@ Format0 = struct {
/// Format 6: Trimmed table mapping
Format6 = struct {
/// Format number is set to 6.
format: u16 where format => format == 6,
format: u16be where format => format == 6,
/// This is the length in bytes of the subtable.
length: u16,
length: u16be,
/// Please see “Note on the language field in 'cmap' subtables“ in this
/// document.
language: u16,
language: u16be,
/// First character code of subrange.
first_code: u16,
first_code: u16be,
/// Number of character codes in subrange.
entry_count: u16,
entry_count: u16be,
/// Array of glyph index values for character codes in the range.
glyph_id_array: [u16; entry_count],
glyph_id_array: [u16be; entry_count],
};

/// Format 8: mixed 16-bit and 32-bit coverage
Format8 = struct {
/// Subtable format; set to 8.
format: u16 where format => format == 8,
format: u16be where format => format == 8,
/// Reserved; set to 0
reserved: u16,
reserved: u16be,
/// Byte length of this subtable (including the header)
length: u32,
length: u32be,
/// Please see “Note on the language field in 'cmap' subtables“ in this
/// document.
language: u32,
language: u32be,
/// Tightly packed array of bits (8K bytes total) indicating whether the
/// particular 16-bit (index) value is the start of a 32-bit character code
is32: [u8; 8192],
/// Number of groupings which follow
num_groups: u32,
num_groups: u32be,
/// Array of SequentialMapGroup records.
groups: [Format8SequentialMapGroup; num_groups],
};
Expand All @@ -89,83 +87,83 @@ Format8SequentialMapGroup = struct {
/// First character code in this group; note that if this group is for one
/// or more 16-bit character codes (which is determined from the is32
/// array), this 32-bit value will have the high 16-bits set to zero
start_char_code: u32,
start_char_code: u32be,
/// Last character code in this group; same condition as listed above for
/// the `start_char_code`
end_char_code: u32,
end_char_code: u32be,
/// Glyph index corresponding to the starting character code
start_glyph_id: u32,
start_glyph_id: u32be,
};

// TODO: Format10

// Format 12: Segmented coverage
Format12 = struct {
/// Subtable format; set to 12.
format: u16 where format => format == 12,
format: u16be where format => format == 12,
/// Reserved; set to 0
reserved: u16,
reserved: u16be,
/// Byte length of this subtable (including the header)
length: u32,
length: u32be,
/// Please see “Note on the language field in 'cmap' subtables“ in this
/// document.
language: u32,
language: u32be,
/// Number of groupings which follow
num_groups: u32,
num_groups: u32be,
/// Array of SequentialMapGroup records.
groups: [Format12SequentialMapGroup; num_groups],
};

Format12SequentialMapGroup = struct {
/// First character code in this group
start_char_code: u32,
start_char_code: u32be,
/// Last character code in this group
end_char_code: u32,
end_char_code: u32be,
/// Glyph index corresponding to the starting character code
start_glyph_id: u32,
start_glyph_id: u32be,
};

// Format 13: Many-to-one range mappings
Format13 = struct {
/// Subtable format; set to 13.
format: u16 where format => format == 13,
format: u16be where format => format == 13,
/// Reserved; set to 0
reserved: u16,
reserved: u16be,
/// Byte length of this subtable (including the header)
length: u32,
length: u32be,
/// Please see “Note on the language field in 'cmap' subtables“ in this
/// document.
language: u32,
language: u32be,
/// Number of groupings which follow
num_groups: u32,
num_groups: u32be,
/// Array of ConstantMapGroup records.
groups: [ConstantMapGroup; num_groups],
};

ConstantMapGroup = struct {
/// First character code in this group
start_char_code: u32,
start_char_code: u32be,
/// Last character code in this group
end_char_code: u32,
end_char_code: u32be,
/// Glyph index to be used for all the characters in the group's range.
start_glyph_id: u32,
start_glyph_id: u32be,
};
/// Format 14: Unicode Variation Sequences
Format14 = struct {
/// Subtable format. Set to 14.
format: u16 where format => format == 14,
format: u16be where format => format == 14,
/// Byte length of this subtable (including this header)
length: u32,
length: u32be,
/// Number of variation Selector Records
num_var_selector_records: u32,
num_var_selector_records: u32be,
/// Array of VariationSelector records.
var_selector: [VariationSelector; num_var_selector_records],
};
VariationSelector = struct {
/// Variation selector
var_selector: [u8; 3], // FIXME: should be u24
var_selector: [u8; 3], // FIXME: should be u24be
/// Offset from the start of the format 14 subtable to Default UVS Table. May be 0.
default_uvs_offset: Offset32,
/// Offset from the start of the format 14 subtable to Non-Default UVS Table. May be 0.
Expand All @@ -175,29 +173,29 @@ VariationSelector = struct {
/// Default UVS table
DefaultUVS = struct {
/// Number of Unicode character ranges.
num_unicode_value_ranges: u32,
num_unicode_value_ranges: u32be,
/// Array of UnicodeRange records.
ranges: [UnicodeRange; num_unicode_value_ranges],
};
UnicodeRange = struct {
/// First value in this range
start_unicode_value: [u8; 3], // FIXME: should be u24
start_unicode_value: [u8; 3], // FIXME: should be u24be
/// Number of additional values in this range
additional_count: u8,
};
/// NonDefaultUVS Table
NonDefaultUVS = struct {
/// Number of UVS Mappings that follow
num_uvs_mappings: u32,
num_uvs_mappings: u32be,
/// Array of UVSMapping records.
uvs_mappings: [UVSMapping; num_uvs_mappings]
};
UVSMapping = struct {
/// Base Unicode value of the UVS
unicode_value: [u8; 3], // FIXME: should be u24
unicode_value: [u8; 3], // FIXME: should be u24be
/// Glyph ID of the UVS
glyph_id: u16,
glyph_id: u16be,
};
8 changes: 3 additions & 5 deletions examples/ddl/edid.ddl
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
//! endian: le

Header = struct {
/// Fixed header pattern
magic: [u8; 8], // FIXME: constrain to [0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00]
/// Manufacturer ID
mfg_bytes: u16,
mfg_bytes: u16le,
/// Manufacturer product code
product_code: u16,
product_code: u16le,
/// Serial number
serial: u32,
serial: u32le,
/// Week of manufacture. Week numbering is not consistent between manufacturers.
mfg_week: u8,
/// Year of manufacture, less 1990. (19902245). If week=255, it is the model year instead.
Expand Down
8 changes: 3 additions & 5 deletions examples/ddl/heroes_of_might_and_magic_bmp.ddl
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! endian: le

/// http://formats.kaitai.io/heroes_of_might_and_magic_bmp/index.html
HerosOfMightAndMagicBmp = struct {
magic: u16,
width: u16,
height: u16,
magic: u16le,
width: u16le,
height: u16le,
data: [u8; width * height],
};
10 changes: 4 additions & 6 deletions examples/ddl/object_id.ddl
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//! endian: le

/// https://docs.mongodb.com/manual/reference/method/ObjectId/
ObjectId = struct {
epoch_time: u32,
machine_id: [u8; 3], // FIXME: should be u24
process_id: u16,
counter: [u8; 3], // FIXME: should be u24
epoch_time: u32le,
machine_id: [u8; 3], // FIXME: should be u24le
process_id: u16le,
counter: [u8; 3], // FIXME: should be u24le
};
9 changes: 4 additions & 5 deletions examples/ddl/stl.ddl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! endian: le
//! file-extension: stl

Vec3d = struct {
x: f32,
y: f32,
z: f32,
x: f32le,
y: f32le,
z: f32le,
};

Triangle = struct {
Expand All @@ -19,7 +18,7 @@ Stl = struct {
/// Generally ignored
header: [u8; 80],
/// Number of triangles that follow
num_triangles: u32,
num_triangles: u32le,
/// The triangle data
triangles: [Triangle; num_triangles],
};
6 changes: 3 additions & 3 deletions src/ir/owned/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub enum RepeatBound<N> {
pub enum ParseExpr<N> {
/// A reference to another parser
Var(Var<N, u32>),
/// Parse a bit
Bit,
/// Parse a byte
U8,
/// The name of another parsable type
Ident(N),
/// Parse that is repeated for the given bound
Expand Down Expand Up @@ -150,7 +150,7 @@ impl<'a, N: Name + for<'b> From<&'b str>> From<&'a binary::Type<N>> for ParseExp

match *src {
Type::Var(_, ref var) => ParseExpr::Var(var.clone()),
Type::Const(TypeConst::Bit) => ParseExpr::Bit,
Type::Const(TypeConst::U8) => ParseExpr::U8,
Type::Array(_, ref elem_ty, ref size_expr) => {
let elem_parser = ParseExpr::from(&**elem_ty);
ParseExpr::repeat(elem_parser, RepeatBound::Exact(size_expr.clone()))
Expand Down
10 changes: 5 additions & 5 deletions src/syntax/ast/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Kind {

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TypeConst {
Bit,
U8,
}

/// A binary type
Expand Down Expand Up @@ -71,9 +71,9 @@ impl<N: Name> Type<N> {
Type::Var(span, Var::Bound(Named(x.into(), i)))
}

/// Bit type constant
pub fn bit() -> Type<N> {
Type::Const(TypeConst::Bit)
/// Byte type constant
pub fn u8() -> Type<N> {
Type::Const(TypeConst::U8)
}

/// An array of the specified type, with a size: eg. `[T; n]`
Expand Down Expand Up @@ -307,7 +307,7 @@ impl<N: Name> Type<N> {
pub fn repr(&self) -> host::RcType<N> {
match *self {
Type::Var(_, ref v) => Rc::new(host::Type::Var(v.clone())),
Type::Const(TypeConst::Bit) => Rc::new(host::Type::Const(host::TypeConst::Bit)),
Type::Const(TypeConst::U8) => Rc::new(host::Type::Const(host::TypeConst::U8)),
Type::Array(_, ref elem_ty, _) => Rc::new(host::Type::Array(elem_ty.repr())),
Type::Assert(_, ref ty, _) => ty.repr(),
Type::Interp(_, _, _, ref repr_ty) => repr_ty.clone(),
Expand Down
Loading

0 comments on commit c26e444

Please sign in to comment.