Skip to content

Commit 289c397

Browse files
authored
Merge pull request #470 from brendanzab/rename-array-formats
Rename array formats to `repeat_len{8|16|32|64}`
2 parents 5b74b8e + 0961c0d commit 289c397

37 files changed

+344
-331
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def pixel = {
2525
def main = {
2626
width <- u16le,
2727
height <- u16le,
28-
pixels <- array16 (width * height) pixel,
28+
pixels <- repeat_len16 (width * height) pixel,
2929
};
3030
```
3131

doc/reference.md

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ elaboration, and core language is forthcoming.
2727
- [Conditional formats](#conditional-formats)
2828
- [Overlap formats](#overlap-formats)
2929
- [Number formats](#number-formats)
30-
- [Array formats](#array-formats)
31-
- [Repeat formats](#repeat-formats)
30+
- [Exact-length repetition formats](#exact-length-repetition-formats)
31+
- [Repeat until end formats](#repeat-until-end-formats)
3232
- [Limit formats](#limit-formats)
3333
- [Stream position formats](#stream-position-formats)
3434
- [Link formats](#link-formats)
@@ -140,7 +140,7 @@ If no binding is found, names can refer to one of the built-in primitives:
140140
- `u8`, `u16be`, `u16le`, `u32be`, `u32le`, `u64be`, `u64le`
141141
- `s8`, `s16be`, `s16le`, `s32be`, `s32le`, `s64be`, `s64le`
142142
- `f32be`, `f32le`, `f64be`, `f64le`
143-
- `array8`, `array16`, `array32`, `array64`
143+
- `repeat_len8`, `repeat_len16`, `repeat_len32`, `repeat_len64`
144144
- `link8`, `link16`, `link32`, `link64`
145145
- `stream_pos`
146146
- `succeed`, `fail`
@@ -297,9 +297,9 @@ data-dependent formats. For example:
297297
```fathom
298298
{
299299
len <- u32be,
300-
data <- array32 len { x <- u32be, y <- u32be },
301-
// ▲
302-
// └─── type of `len` is `Repr u32be`
300+
data <- repeat_len32 len { x <- u32be, y <- u32be },
301+
//
302+
// └─── type of `len` is `Repr u32be`
303303
}
304304
```
305305

@@ -319,10 +319,10 @@ let unit : Format =
319319
```
320320

321321
No annotations needed in the following example, as the type can be inferred from
322-
the type of `array8`:
322+
the type of `repeat_len8`:
323323

324324
```fathom
325-
array8 3 {}
325+
repeat_len8 3 {}
326326
```
327327

328328
#### Tuple syntax for record formats
@@ -386,10 +386,10 @@ more convenient:
386386
387387
388388
389-
start_code <- array16 seg_count u16be,
390-
id_delta <- array16 seg_count s16be,
391-
// ▲
392-
// └──── `seg_count` is used here
389+
start_code <- repeat_len16 seg_count u16be,
390+
id_delta <- repeat_len16 seg_count s16be,
391+
//
392+
// └──── `seg_count` is used here
393393
}
394394
```
395395

@@ -431,12 +431,12 @@ because they might appear in the representation types of subsequent fields.
431431

432432
Some some examples of record formats and their representations are:
433433

434-
| format | `Repr` format |
435-
| ------------------------------------------------- | -------------------------------------- |
436-
| `{ x <- f32le, y <- f32le }` | `{ x : F32, y : F32 }` |
437-
| `{ len <- u16be, data <- array16 len s8 }` | `{ len : U16, data : Array16 len S8 }` |
438-
| `{ magic <- u32be where u32_eq magic "icns" }` | `{ magic : U32 }` |
439-
| `{ let len = 4 : U32, data <- array32 len s8 }` | `{ len : U32, data : Array32 len S8 }` |
434+
| format | `Repr` format |
435+
| ----------------------------------------------------- | -------------------------------------- |
436+
| `{ x <- f32le, y <- f32le }` | `{ x : F32, y : F32 }` |
437+
| `{ len <- u16be, data <- repeat_len16 len s8 }` | `{ len : U16, data : Array16 len S8 }` |
438+
| `{ magic <- u32be where u32_eq magic "icns" }` | `{ magic : U32 }` |
439+
| `{ let len = 4 : U32, data <- repeat_len32 len s8 }` | `{ len : U32, data : Array32 len S8 }` |
440440

441441
### Conditional formats
442442

@@ -479,8 +479,8 @@ enriched with information that occurs later on in the stream:
479479

480480
```fathom
481481
overlap {
482-
records0 : array16 len array_record0,
483-
records1 : array16 len (array_record0 records0),
482+
records0 : repeat_len16 len array_record0,
483+
records1 : repeat_len16 len (array_record0 records0),
484484
}
485485
```
486486

@@ -535,36 +535,40 @@ corresponding host representation:
535535
| `f32be`, `f32le` | `F32` |
536536
| `f64be`, `f64le` | `F64` |
537537

538-
### Array formats
538+
### Exact-length repetition formats
539539

540-
There are four array formats, corresponding to the four [array types](#arrays):
540+
There are four length constrained repetition formats, corresponding to the four
541+
[array types](#arrays):
541542

542-
- `array8 : U8 -> Format -> Format`
543-
- `array16 : U16 -> Format -> Format`
544-
- `array32 : U32 -> Format -> Format`
545-
- `array64 : U64 -> Format -> Format`
543+
- `repeat_len8 : U8 -> Format -> Format`
544+
- `repeat_len16 : U16 -> Format -> Format`
545+
- `repeat_len32 : U32 -> Format -> Format`
546+
- `repeat_len64 : U64 -> Format -> Format`
546547

547-
#### Representation of array formats
548+
These formats will parse the specified number of elements, failing if one of
549+
those elements failed to parse, or if the end of the current binary stream was
550+
reached.
548551

549-
The [representation](#format-representations) of the array formats preserve the
550-
lengths, and use the representation of the element formats as the element types
551-
of the host [array types](#array-types).
552+
#### Representation of exact-length repetition formats
552553

553-
| format | `Repr` format |
554-
| ---------------------- | ----------------------------------- |
555-
| `array8 len format` | `Array8 len (Repr format)` |
556-
| `array16 len format` | `Array16 len (Repr format)` |
557-
| `array32 len format` | `Array32 len (Repr format)` |
558-
| `array64 len format` | `Array64 len (Repr format)` |
554+
The [representation](#format-representations) of the repetition formats preserve
555+
the lengths in a corresponding [array type](#array-types):
559556

560-
### Repeat formats
557+
| format | `Repr` format |
558+
| --------------------------- | ----------------------------------- |
559+
| `repeat_len8 len format` | `Array8 len (Repr format)` |
560+
| `repeat_len16 len format` | `Array16 len (Repr format)` |
561+
| `repeat_len32 len format` | `Array32 len (Repr format)` |
562+
| `repeat_len64 len format` | `Array64 len (Repr format)` |
563+
564+
### Repeat until end formats
561565

562566
The `repeat_until_end` format repeats parsing the given format until the end of
563567
the current binary stream is reached:
564568

565569
- `repeat_until_end : Format -> Format`
566570

567-
#### Representation of repeat formats
571+
#### Representation of repeat until end formats
568572

569573
Because the repeat format does not have a predefined length, it is
570574
[represented](#format-representations) as a dynamically sized

fathom/src/core.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,14 @@ def_prims! {
383383
FormatF64Be => "f64be",
384384
/// 64-bit, IEEE-754 floating point formats (little-endian).
385385
FormatF64Le => "f64le",
386-
/// Array formats, with unsigned 8-bit indices.
387-
FormatArray8 => "array8",
388-
/// Array formats, with unsigned 16-bit indices.
389-
FormatArray16 => "array16",
390-
/// Array formats, with unsigned 32-bit indices.
391-
FormatArray32 => "array32",
392-
/// Array formats, with unsigned 64-bit indices.
393-
FormatArray64 => "array64",
386+
/// Repeat formats up to an unsigned 8-bit length.
387+
FormatRepeatLen8 => "repeat_len8",
388+
/// Repeat formats up to an unsigned 16-bit length.
389+
FormatRepeatLen16 => "repeat_len16",
390+
/// Repeat formats up to an unsigned 32-bit length.
391+
FormatRepeatLen32 => "repeat_len32",
392+
/// Repeat formats up to an unsigned 64-bit length.
393+
FormatRepeatLen64 => "repeat_len64",
394394
/// Repeat a format until the length of the given parse scope is reached.
395395
FormatRepeatUntilEnd => "repeat_until_end",
396396
/// Limit the format to an unsigned 8-bit byte length.

fathom/src/core/binary.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,10 @@ impl<'arena, 'data> Context<'arena, 'data> {
425425
(Prim::FormatF32Le, []) => read_const(reader, span, read_f32le, Const::F32),
426426
(Prim::FormatF64Be, []) => read_const(reader, span, read_f64be, Const::F64),
427427
(Prim::FormatF64Le, []) => read_const(reader, span, read_f64le, Const::F64),
428-
(Prim::FormatArray8, [FunApp(_, len), FunApp(_, format)]) => self.read_array(reader, span, len, format),
429-
(Prim::FormatArray16, [FunApp(_, len), FunApp(_, format)]) => self.read_array(reader, span, len, format),
430-
(Prim::FormatArray32, [FunApp(_, len), FunApp(_, format)]) => self.read_array(reader, span, len, format),
431-
(Prim::FormatArray64, [FunApp(_, len), FunApp(_, format)]) => self.read_array(reader, span, len, format),
428+
(Prim::FormatRepeatLen8, [FunApp(_, len), FunApp(_, format)]) => self.read_repeat_len(reader, span, len, format),
429+
(Prim::FormatRepeatLen16, [FunApp(_, len), FunApp(_, format)]) => self.read_repeat_len(reader, span, len, format),
430+
(Prim::FormatRepeatLen32, [FunApp(_, len), FunApp(_, format)]) => self.read_repeat_len(reader, span, len, format),
431+
(Prim::FormatRepeatLen64, [FunApp(_, len), FunApp(_, format)]) => self.read_repeat_len(reader, span, len, format),
432432
(Prim::FormatRepeatUntilEnd, [FunApp(_,format)]) => self.read_repeat_until_end(reader, format),
433433
(Prim::FormatLimit8, [FunApp(_, limit), FunApp(_, format)]) => self.read_limit(reader, limit, format),
434434
(Prim::FormatLimit16, [FunApp(_, limit), FunApp(_, format)]) => self.read_limit(reader, limit, format),
@@ -448,7 +448,7 @@ impl<'arena, 'data> Context<'arena, 'data> {
448448
}
449449
}
450450

451-
fn read_array(
451+
fn read_repeat_len(
452452
&mut self,
453453
reader: &mut BufferReader<'data>,
454454
span: Span,

fathom/src/core/prim.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ impl<'arena> Env<'arena> {
9797
env.define_prim(FormatF32Le, &FORMAT_TYPE);
9898
env.define_prim(FormatF64Be, &FORMAT_TYPE);
9999
env.define_prim(FormatF64Le, &FORMAT_TYPE);
100-
env.define_prim_fun(FormatArray8, [&U8_TYPE, &FORMAT_TYPE], &FORMAT_TYPE);
101-
env.define_prim_fun(FormatArray16, [&U16_TYPE, &FORMAT_TYPE], &FORMAT_TYPE);
102-
env.define_prim_fun(FormatArray32, [&U32_TYPE, &FORMAT_TYPE], &FORMAT_TYPE);
103-
env.define_prim_fun(FormatArray64, [&U64_TYPE, &FORMAT_TYPE], &FORMAT_TYPE);
100+
env.define_prim_fun(FormatRepeatLen8, [&U8_TYPE, &FORMAT_TYPE], &FORMAT_TYPE);
101+
env.define_prim_fun(FormatRepeatLen16, [&U16_TYPE, &FORMAT_TYPE], &FORMAT_TYPE);
102+
env.define_prim_fun(FormatRepeatLen32, [&U32_TYPE, &FORMAT_TYPE], &FORMAT_TYPE);
103+
env.define_prim_fun(FormatRepeatLen64, [&U64_TYPE, &FORMAT_TYPE], &FORMAT_TYPE);
104104
env.define_prim_fun(FormatRepeatUntilEnd, [&FORMAT_TYPE], &FORMAT_TYPE);
105105
env.define_prim_fun(FormatLimit8, [&U8_TYPE, &FORMAT_TYPE], &FORMAT_TYPE);
106106
env.define_prim_fun(FormatLimit16, [&U16_TYPE, &FORMAT_TYPE], &FORMAT_TYPE);
@@ -619,10 +619,10 @@ pub fn repr(prim: Prim) -> Step {
619619
Prim::FormatF32Le => step!(_, [] => Spanned::empty(Arc::new(Value::prim(Prim::F32Type, [])))),
620620
Prim::FormatF64Be => step!(_, [] => Spanned::empty(Arc::new(Value::prim(Prim::F64Type, [])))),
621621
Prim::FormatF64Le => step!(_, [] => Spanned::empty(Arc::new(Value::prim(Prim::F64Type, [])))),
622-
Prim::FormatArray8 => step!(env, [len, elem] => Spanned::empty(Arc::new(Value::prim(Prim::Array8Type, [len.clone(), env.format_repr(elem)])))),
623-
Prim::FormatArray16 => step!(env, [len, elem] => Spanned::empty(Arc::new(Value::prim(Prim::Array16Type, [len.clone(), env.format_repr(elem)])))),
624-
Prim::FormatArray32 => step!(env, [len, elem] => Spanned::empty(Arc::new(Value::prim(Prim::Array32Type, [len.clone(), env.format_repr(elem)])))),
625-
Prim::FormatArray64 => step!(env, [len, elem] => Spanned::empty(Arc::new(Value::prim(Prim::Array64Type, [len.clone(), env.format_repr(elem)])))),
622+
Prim::FormatRepeatLen8 => step!(env, [len, elem] => Spanned::empty(Arc::new(Value::prim(Prim::Array8Type, [len.clone(), env.format_repr(elem)])))),
623+
Prim::FormatRepeatLen16 => step!(env, [len, elem] => Spanned::empty(Arc::new(Value::prim(Prim::Array16Type, [len.clone(), env.format_repr(elem)])))),
624+
Prim::FormatRepeatLen32 => step!(env, [len, elem] => Spanned::empty(Arc::new(Value::prim(Prim::Array32Type, [len.clone(), env.format_repr(elem)])))),
625+
Prim::FormatRepeatLen64 => step!(env, [len, elem] => Spanned::empty(Arc::new(Value::prim(Prim::Array64Type, [len.clone(), env.format_repr(elem)])))),
626626
Prim::FormatLimit8 => step!(env, [_, elem] => env.format_repr(elem)),
627627
Prim::FormatLimit16 => step!(env, [_, elem] => env.format_repr(elem)),
628628
Prim::FormatLimit32 => step!(env, [_, elem] => env.format_repr(elem)),

formats/edid.fathom

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def chromacity_coordinates = {
4747
};
4848

4949
def established_timing = {
50-
mode_bitmap <- array8 3 u8, // TODO: bit patterns
50+
mode_bitmap <- repeat_len8 3 u8, // TODO: bit patterns
5151
};
5252

5353
def standard_timing_information : Format = {

formats/edid.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def chromacity_coordinates : Format = {
2828
white_x_msb <- u8,
2929
white_y_msb <- u8,
3030
};
31-
def established_timing : Format = { mode_bitmap <- array8 3 u8 };
31+
def established_timing : Format = { mode_bitmap <- repeat_len8 3 u8 };
3232
def standard_timing_information : Format = ();
3333
def main : Format = {
3434
header <- header,

formats/gif.fathom

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def logical_screen_descriptor = {
2929
///
3030
/// - [GIF89a Specification: Section 17](https://www.w3.org/Graphics/GIF/spec-gif89a.txt)
3131
def header = {
32-
magic <- array8 3 u8, // TODO: where magic == ascii "GIF"`,
33-
version <- array8 3 u8,
32+
magic <- repeat_len8 3 u8, // TODO: where magic == ascii "GIF"`,
33+
version <- repeat_len8 3 u8,
3434
};
3535

3636
/// # Global Color Table Entry
@@ -50,7 +50,7 @@ def color_table_entry = {
5050
///
5151
/// - [GIF89a Specification: Section 19](https://www.w3.org/Graphics/GIF/spec-gif89a.txt)
5252
def global_color_table (len : U16) = {
53-
entries <- array16 len color_table_entry,
53+
entries <- repeat_len16 len color_table_entry,
5454
};
5555

5656
def main = {

formats/gif.snap

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ def logical_screen_descriptor : Format = {
66
bg_color_index <- u8,
77
pixel_aspect_ratio <- u8,
88
};
9-
def header : Format = { magic <- array8 3 u8, version <- array8 3 u8 };
9+
def header : Format = {
10+
magic <- repeat_len8 3 u8,
11+
version <- repeat_len8 3 u8,
12+
};
1013
def color_table_entry : Format = { red <- u8, green <- u8, blue <- u8 };
1114
def global_color_table : U16 -> Format = fun len => {
12-
entries <- array16 len color_table_entry,
15+
entries <- repeat_len16 len color_table_entry,
1316
};
1417
def main : Format = { header <- header, screen <- logical_screen_descriptor };
1518
'''

formats/ideas/ico.fathom

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def image = {
2424

2525
def main = {
2626
// NOTE: Could be useful to have invertible patterns here?
27-
magic <- array8 4 u8 match {
27+
magic <- repeat_len8 4 u8 match {
2828
[0, 0, 1, 0] => icon, // TODO
2929
[0, 0, 2, 0] => cursor, // TODO
3030
},

0 commit comments

Comments
 (0)