+// SPDX-FileCopyrightText: 2023 Philippe Proulx <eeppeliteloop@gmail.com>
+// SPDX-License-Identifier: CC-BY-SA-4.0
+
// Show ToC at a specific location for a GitHub rendering
ifdef::env-github[]
:toc: macro
This package offers both a portable {py3} module and a command-line
tool.
-WARNING: This version of Normand is 0.21, meaning both the Normand
+WARNING: This version of Normand is 0.23, meaning both the Normand
language and the module/CLI interface aren't stable.
ifdef::env-github[]
+
----
{strength = 4}
-{be} 67 <lbl> 44 $178 {(end - lbl) * 8 + strength : 16} $99 <end>
-{le} {-1993 : 32}
-{-3.141593 : 64}
+!be 67 <lbl> 44 $178 [(end - lbl) * 8 + strength : 16] $99 <end>
+!le [-1993 : 32]
+[-3.141593 : 64be]
----
+
Output:
+
----
-67 44 b2 00 2c 63 37 f8 ff ff 7f bd c2 82 fb 21
-09 c0
+67 44 b2 00 2c 63 37 f8 ff ff c0 09 21 fb 82 c2
+bd 7f
----
+
The encoded number is the evaluation of a valid {py3} expression which
Input:
+
----
-aa bb cc {-1993 : sleb128} <meow> dd ee ff
-{meow * 199 : uleb128}
+aa bb cc [-1993 : sleb128] <meow> dd ee ff
+[meow * 199 : uleb128]
----
+
Output:
Input:
+
----
-{be}
+!be
- {199:32}
-@64 {43:64}
-@16 {-123:16}
-@32~255 {5584:32}
+ [199:32]
+@64 [43:64]
+@16 [-123:16]
+@32~255 [5584:32]
----
+
Output:
Input:
+
----
-{le}
-{0xdeadbeef:32}
-{-1993:16}
-{9:16}
+!le
+[0xdeadbeef:32]
+[-1993:16]
+[9:16]
+0x40
-{ICITTE:8}
+[ICITTE:8]
"meow mix"
+200~FFh
-{ICITTE:8}
+[ICITTE:8]
----
+
Output:
Input:
+
----
-"end of file @ " {end:8}
+"end of file @ " [end:8]
!transform gzip
"this part will be gzipped"
function and the <<command-line-tool,command-line tool>>).
`normand.py` has _no external dependencies_, but if you're using
-Python{nbsp}3.4, you'll need a local copy of the standard `typing`
-module.
+Python{nbsp}3.4 or Python{nbsp}3.5, you'll need a local copy of the
+standard `typing` module.
====
== Design goals
Compare the following Normand input and some hypothetical XML
equivalent, for example:
+
-.Actual normand input.
+.Actual Normand input.
----
ff dd 01 ab $192 $-128 %1101:0011
-{end:8}
+[end:8]
{iter = 1}
!if {not something}
# five times because xyz
!repeat 5
- "hello world " {iter:8}
+ "hello world " [iter:8]
{iter = iter + 1}
!end
!end
|
The current offset has an effect on the value of <<label,labels>> and of
the special `ICITTE` name in <<fixed-length-number,fixed-length
-number>>, <<leb-128-integer,LEB128 integer>>, <<string,string>>,
+number>>, <<leb128-integer,LEB128 integer>>, <<string,string>>,
<<filling,filling>>, <<variable-assignment,variable assignment>>,
<<conditional-block,conditional block>>, <<repetition-block,repetition
block>>, <<macro-expansion,macro expansion>>, and
|[[cur-bo]] Current byte order
|
-The current byte order has an effect on the encoding of
+The current byte order can have an effect on the encoding of
<<fixed-length-number,fixed-length numbers>>.
A <<current-byte-order-setting,current byte order setting>> may change
little endian).
* A <<fixed-length-number,fixed-length number>> (integer or
- floating point) using the <<cur-bo,current byte order>> and of which
- the value is the result of a {py3} expression.
+ floating point), possibly using the <<cur-bo,current byte order>>, and
+ of which the value is the result of a {py3} expression.
* An <<leb128-integer,LEB128 integer>> of which the value is the result
of a {py3} expression.
nibbles and binary bits of <<byte-constant,byte constants>>:
----
-/ \ ? & : ; . , [ ] _ = | -
+& , - . / : ; = ? \ _ |
----
The latter serve to improve readability so that you may write, for
* The `0b` or `0B` prefix followed with one or more bits (`0` or `1`).
* One or more bits followed with the `b` or `B` suffix.
+In general, anything between `pass:[{]` and `}` is a {py3} expression.
+
You can test the examples of this section with the `normand`
<<command-line-tool,command-line tool>> as such:
Input:
----
-ab cd [3d 8F] CC
+ab cd (3d 8F) CC
----
Output:
The two accepted forms are:
[horizontal]
-``pass:[{be}]``:: Set the current byte order to big endian.
-``pass:[{le}]``:: Set the current byte order to little endian.
+`!be`:: Set the current byte order to big endian.
+`!le`:: Set the current byte order to little endian.
=== Fixed-length number
* A floating point number
(https://standards.ieee.org/standard/754-2008.html[IEEE{nbsp}754-2008]).
+
-The available length are 32 (_binary32_) and 64 (_binary64_).
+The available lengths are 32 (_binary32_) and 64 (_binary64_).
+
+The value is the result of evaluating a {py3} expression.
-The value is the result of evaluating a {py3} expression using the
-<<cur-bo,current byte order>>.
+The byte order to use to encode the value is either directly specified
+or is the <<cur-bo,current byte order>>.
A fixed-length number is:
-. The ``pass:[{]`` prefix.
+. The `[` prefix.
. A valid {py3} expression.
+
For a fixed-length number at some source location{nbsp}__**L**__, this
expression may contain the name of any accessible <<label,label>> (not
within a nested group), including the name of a label defined
-after{nbsp}__**L**__ (except within an <<encoded-block,encoded block>>),
-as well as the name of any <<variable-assignment,variable>> known
-at{nbsp}__**L**__.
+after{nbsp}__**L**__ (except within a
+<<transformation-block,transformation block>>), as well as the name of
+any <<variable-assignment,variable>> known at{nbsp}__**L**__.
+
The value of the special name `ICITTE` (`int` type) in this expression
is the <<cur-offset,current offset>> (before encoding the number).
`32` and `64`.
--
-. The `}` suffix.
+. **Optional**: a suffix of the previous encoding length, without
+ any whitespace, amongst:
++
+--
+[horizontal]
+`be`:: Encode in big endian.
+`le`:: Encode in little endian.
+--
++
+Without this suffix, the encoding byte order is the <<cur-bo,current
+byte order>> which must be defined if the encoding length is greater
+than eight.
+
+. The `]` suffix.
====
Input:
----
-{le} {345:16}
-{be} {-0xabcd:32}
+[345:16le]
+[-0xabcd:32be]
----
Output:
Input:
----
-{be}
+!be
# String length in bits
-{8 * (str_end - str_beg) : 16}
+[8 * (str_end - str_beg) : 16]
# String
<str_beg>
Input:
----
-{20 - ICITTE : 8} * 10
+[20 - ICITTE : 8] * 10
----
Output:
Input:
----
-{le}
-{2 * 0.0529 : 32}
+[2 * 0.0529 : 32le]
----
Output:
An LEB128 integer is:
-. The ``pass:[{]`` prefix.
+. The `[` prefix.
. A valid {py3} expression of which the evaluation result type
is `int` or `bool` (automatically converted to `int`).
`sleb128`:: Use the signed LEB128 format.
--
-. The `}` suffix.
+. The `]` suffix.
====
Input:
----
-{624485 : uleb128}
+[624485 : uleb128]
----
Output:
aa bb cc dd
<meow>
ee ff
-{-981238311 + (meow * -23) : sleb128}
+[-981238311 + (meow * -23) : sleb128]
"hello"
----
Encoding suffix form:: {empty}
+
-. The ``pass:[{]`` prefix.
+. The `[` prefix.
. A valid {py3} expression of which the evaluation result type
is `bool`, `int`, `float`, or `str` (the first three automatically
ISO/IEC 8859-16.
--
-. The `}` suffix.
+. The `]` suffix.
====
Input:
{iter = 1}
!repeat 10
- {iter : s:u8} " "
+ u8{iter} " "
{iter = iter + 1}
!end
----
----
{meow = 'salut jérémie'}
-{meow.upper() : s:latin1}
+[meow.upper() : s:latin1]
----
Output:
Input:
----
- {ICITTE : 8} * 8
-<0x61> {ICITTE : 8} * 8
+ [ICITTE : 8] * 8
+<0x61> [ICITTE : 8] * 8
----
Output:
----
aa bb cc dd <meow> ee ff
<12> 11 22 33 <mix> 44 55
-{meow : 8} {mix : 8}
+[meow : 8] [mix : 8]
----
Output:
Input:
----
-{le}
+!le
77 88
-@32~0xcc {-893.5:32}
+@32~0xcc [-893.5:32]
@128~0x55 "meow"
----
** The ``pass:[{]`` prefix, a valid {py3} expression of which the
evaluation result type is `int` or `bool` (automatically converted to
- `int`), and the ``pass:[}]`` suffix.
+ `int`), and the `}` suffix.
+
For a filling at some source location{nbsp}__**L**__, this expression
may contain:
** A valid {py3} name.
+
For the name `__NAME__`, this is equivalent to the
-`pass:[{]__NAME__pass:[}]` form above.
+`pass:[{]__NAME__}` form above.
+
This value must be greater than or equal to the current offset where
----
!macro part(iter, fill)
- <0> "particular security " {ord('0') + iter : 8} +fill~0x80
+ <0> "particular security " [ord('0') + iter : 8] +fill~0x80
!end
{iter = 1}
Input:
----
-{mix = 101} {le}
-{meow = 42} 11 22 {meow:8} 33 {meow = ICITTE + 17}
-"yooo" {meow + mix : 16}
+{mix = 101} !le
+{meow = 42} 11 22 [meow:8] 33 {meow = ICITTE + 17}
+"yooo" [meow + mix : 16]
----
Output:
Input:
----
-{be}
+!be
(
<str_beg> u16le"sébastien diaz" <str_end>
- {ICITTE - str_beg : 8}
- {(end - str_beg) * 5 : 24}
+ [ICITTE - str_beg : 8]
+ [(end - str_beg) * 5 : 24]
) * 3
<end>
----
** The ``pass:[{]`` prefix, a valid {py3} expression of which the
evaluation result type is `int` or `bool` (automatically converted to
- `int`), and the ``pass:[}]`` suffix.
+ `int`), and the `}` suffix.
+
For a conditional block at some source location{nbsp}__**L**__, this
expression may contain:
** A valid {py3} name.
+
For the name `__NAME__`, this is equivalent to the
-`pass:[{]__NAME__pass:[}]` form above.
+`pass:[{]__NAME__}` form above.
. Zero or more items to be handled when the condition is true
except, recursively, a macro definition block.
** The ``pass:[{]`` prefix, a valid {py3} expression of which the
evaluation result type is `int` or `bool` (automatically converted to
- `int`), and the ``pass:[}]`` suffix.
+ `int`), and the `}` suffix.
+
For a repetition block at some source location{nbsp}__**L**__, this
expression may contain:
** A valid {py3} name.
+
For the name `__NAME__`, this is equivalent to the
-`pass:[{]__NAME__pass:[}]` form above.
+`pass:[{]__NAME__}` form above.
. Zero or more items except, recursively, a macro definition block.
----
!repeat 0o400
- {end - ICITTE - 1 : 8}
+ [end - ICITTE - 1 : 8]
!end
<end>
----
aa bb cc dd
-"size of compressed section: " {end - start : 8}
+"size of compressed section: " [end - start : 8]
<start>
@128~99h
-!t qp <beg> {ICITTE - beg : 8} * 50 !end
+!t qp <beg> [ICITTE - beg : 8] * 50 !end
----
Output:
====
----
!macro bake()
- {le} {ICITTE * 8 : 16}
+ !le [ICITTE * 8 : 16]
u16le"predict explode"
!end
----
{iter = 1}
!repeat rep
- {val + iter : uleb128}
- {0xdeadbeef : 32}
+ [val + iter : uleb128]
+ [0xdeadbeef : 32]
{iter = iter + 1}
!end
* The ``pass:[{]`` prefix, a valid {py3} expression of which the
evaluation result type is `int` or `bool` (automatically converted to
- `int`), and the ``pass:[}]`` suffix.
+ `int`), and the `}` suffix.
+
For a macro expansion at some source location{nbsp}__**L**__, this
expression may contain:
----
!macro bake()
- {le} {ICITTE * 8 : 16}
+ !le [ICITTE * 8 : 16]
u16le"predict explode"
!end
----
!macro A(val, is_be)
- {le}
+ !le
!if is_be
- {be}
+ !be
!end
- {val : 16}
+ [val : 16]
!end
!macro B(rep, is_be)
Input:
----
-!macro flt32be(val) {be} {val : 32} !end
+!macro flt32be(val) !be [val : 32] !end
"CHEETOS"
m:flt32be(-42.17)
** The ``pass:[{]`` prefix, a valid {py3} expression of which the
evaluation result type is `int` or `bool` (automatically converted to
- `int`), and the ``pass:[}]`` suffix.
+ `int`), and the `}` suffix.
+
For a post-item repetition at some source location{nbsp}__**L**__, this
expression may contain:
Input:
----
-{end - ICITTE - 1 : 8} * 0x100 <end>
+[end - ICITTE - 1 : 8] * 0x100 <end>
----
Output:
* https://github.com/psf/black[Black]
* https://pycqa.github.io/isort/[isort]
+Licensing and copyright follows the
+https://reuse.software/tutorial/[REUSE] specification and is checked
+with the https://github.com/fsfe/reuse-tool[reuse tool].
+
=== Testing
Use https://docs.pytest.org/[pytest] to test Normand once the package is