README.adoc: make it work offline too
[normand.git] / README.adoc
CommitLineData
bb2f9e9c
PP
1// Show ToC at a specific location for a GitHub rendering
2ifdef::env-github[]
3:toc: macro
4endif::env-github[]
5
6ifndef::env-github[]
71aaa3f7 7:toc: left
bb2f9e9c
PP
8endif::env-github[]
9
10// This is to mimic what GitHub does so that anchors work in an offline
11// rendering too.
12:idprefix:
13:idseparator: -
71aaa3f7 14
bb2f9e9c 15// Other attributes
71aaa3f7
PP
16:py3: Python{nbsp}3
17
bb2f9e9c
PP
18= Normand
19Philippe Proulx
20
71aaa3f7
PP
21[.normal]
22image:https://img.shields.io/pypi/v/normand.svg?label=Latest%20version[link="https://pypi.python.org/pypi/normand"]
23
24[.lead]
25_**Normand**_ is a text-to-binary processor with its own language.
26
27This package offers both a portable {py3} module and a command-line
28tool.
29
30WARNING: This version of Normand is 0.1, meaning both the Normand
31language and the module/CLI interface aren't stable.
32
bb2f9e9c
PP
33ifdef::env-github[]
34// ToC location for a GitHub rendering
35toc::[]
36endif::env-github[]
37
71aaa3f7
PP
38== Introduction
39
40The purpose of Normand is to consume human-readable text representing
41bytes and to produce the corresponding binary data.
42
43.Simple bytes input.
44====
45Consider the following Normand input:
46
47----
484f 55 32 bb $167 fe %10100111 a9 $-32
49----
50
51The generated nine bytes are:
52
53----
544f 55 32 bb a7 fe a7 a9 e0
55----
56====
57
58As you can see in the last example, the fundamental unit of the Normand
59language is the _byte_. The order in which you list bytes will be the
60order of the generated data.
61
62The Normand language is more than simple lists of bytes, though. Its
63main features are:
64
65Comments, including a bunch of insignificant symbols which may improve readability::
66+
67Input:
68+
69----
70ff bb %1101:0010 # This is a comment
7178 29 af $192 # This too # 99 $-80
72fe80::6257:18ff:fea3:4229
7360:57:18:a3:42:29
7410839636-5d65-4a68-8e6a-21608ddf7258
75----
76+
77Output:
78+
79----
80ff bb d2 78 29 af c0 99 b0 fe 80 62 57 18 ff fe
81a3 42 29 60 57 18 a3 42 29 10 83 96 36 5d 65 4a
8268 8e 6a 21 60 8d df 72 58
83----
84
85Hexadecimal, decimal, and binary byte constants::
86+
87Input:
88+
89----
90aa bb $247 $-89 %0011_0010 %11.01= 10/10
91----
92+
93Output:
94+
95----
96aa bb f7 a7 32 da
97----
98
99UTF-8, UTF-16, and UTF-32 literal strings::
100+
101Input:
102+
103----
104"hello world!" 00
105u16le"stress\nverdict 🤣"
106----
107+
108Output:
109+
110----
11168 65 6c 6c 6f 20 77 6f 72 6c 64 21 00 73 00 74 ┆ hello world!•s•t
11200 72 00 65 00 73 00 73 00 0a 00 76 00 65 00 72 ┆ •r•e•s•s•••v•e•r
11300 64 00 69 00 63 00 74 00 20 00 3e d8 23 dd ┆ •d•i•c•t• •>•#•
114----
115
116Labels: special variables holding the offset where they're defined::
117+
118----
119<beg> b2 52 e3 bc 91 05
120$100 $50 <chair> 33 9f fe
12125 e9 89 8a <end>
122----
123
124Variables::
125+
126----
1275e 65 {tower = 47} c6 7f f2 c4
12844 {hurl = tower - 14} b5 {tower = hurl} 26 2d
129----
130+
131The value of a variable assignment is the evaluation of a valid {py3}
132expression which may include label and variable names.
133
134Value encoding with a specific length (8{nbsp}bits to 64{nbsp}bits) and byte order::
135+
136Input:
137+
138----
139{strength = 4}
140{be} 67 <lbl> 44 $178 {(end - lbl) * 8 + strength : 16} $99 <end>
141{le} {-1993 : 32}
142----
143+
144Output:
145+
146----
14767 44 b2 00 2c 63 37 f8 ff ff
148----
149+
150The encoded value is the evaluation of a valid {py3} expression which
151may include label and variable names.
152
153Repetition::
154+
155Input:
156+
157----
158aa bb * 5 cc "yeah\0" * 8
159----
160+
161Output:
162+
163----
164aa bb bb bb bb bb cc 79 65 61 68 00 79 65 61 68 ┆ •••••••yeah.yeah
16500 79 65 61 68 00 79 65 61 68 00 79 65 61 68 00 ┆ •yeah•yeah•yeah•
16679 65 61 68 00 79 65 61 68 00 79 65 61 68 00 ┆ yeah•yeah•yeah•
167----
168
169
170Multilevel grouping::
171+
172Input:
173+
174----
175ff ((aa bb "zoom" cc) * 5) * 3 $-34 * 4
176----
177+
178Output:
179+
180----
181ff aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa ┆ •••zoom•••zoom••
182bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a ┆ •zoom•••zoom•••z
1836f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f ┆ oom•••zoom•••zoo
1846d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc ┆ m•••zoom•••zoom•
185aa bb 7a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb ┆ ••zoom•••zoom•••
1867a 6f 6f 6d cc aa bb 7a 6f 6f 6d cc aa bb 7a 6f ┆ zoom•••zoom•••zo
1876f 6d cc aa bb 7a 6f 6f 6d cc de de de de ┆ om•••zoom•••••
188----
189
190Precise error reporting::
191+
192----
193/tmp/meow.normand:10:24 - Expecting a bit (`0` or `1`).
194----
195+
196----
197/tmp/meow.normand:32:6 - Unexpected character `k`.
198----
199+
200----
201/tmp/meow.normand:24:19 - Unknown variable/label name `meow` in expression `(meow - 45) // 8`.
202----
203+
204----
205/tmp/meow.normand:18:9 - Value 315 is outside the 8-bit range when evaluating expression `end - ICITTE` at byte offset 45.
206----
207
208You can use Normand to track data source files in your favorite VCS
209instead of raw binary files. The binary files that Normand generates can
210be used to test file format decoding, including malformatted data, for
211example, as well as for education.
212
213See <<learn-normand>> to explore all the Normand features.
214
215== Install Normand
216
217Normand requires Python ≥ 3.4.
218
219To install Normand:
220
221----
222$ python3 -m pip install --user normand
223----
224
225See
226https://packaging.python.org/en/latest/tutorials/installing-packages/#installing-to-the-user-site[Installing to the User Site]
227to learn more about a user site installation.
228
229[NOTE]
230====
231Normand has a single module file, `normand.py`, which you can copy as is
232to your project to use it (both the <<python-3-api,`normand.parse()`>>
233function and the <<command-line-tool,command-line tool>>).
234
235`normand.py` has _no external dependencies_, but if you're using
236Python{nbsp}3.4, you'll need a local copy of the standard `typing`
237module.
238====
239
240== Learn Normand
241
242A Normand text input is a sequence of items which represent a sequence
243of raw bytes.
244
245[[state]] During the processing of items to data, Normand relies on a
246current state:
247
248[%header%autowidth]
249|===
250|State variable |Description |Initial value: <<python-3-api,{py3} API>> |Initial value: <<command-line-tool,CLI>>
251
252|[[cur-offset]] Current offset
253|
254The current offset has an effect on the value of
255<<label,labels>> and of the special `ICITTE` name in <<value,value>> and
256<<variable-assignment,variable assignment>> expression evaluation.
257
258Each generated byte increments the current offset.
259
260A <<current-offset-setting,current offset setting>> may change the
261current offset.
262|`init_offset` parameter of the `parse()` function.
263|`--offset` option.
264
265|[[cur-bo]] Current byte order
266|
267The current byte order has an effect on the encoding of <<value,values>>.
268
269A <<current-byte-order-setting,current byte order setting>> may change
270the current byte order.
271|`init_byte_order` parameter of the `parse()` function.
272|`--byte-order` option.
273
274|<<label,Labels>>
275|Mapping of label names to integral values.
276|`init_labels` parameter of the `parse()` function.
277|One or more `--label` options.
278
279|<<variable-assignment,Variables>>
280|Mapping of variable names to integral values.
281|`init_variables` parameter of the `parse()` function.
282|One or more `--var` options.
283|===
284
285The available items are:
286
287* A <<byte-constant,constant integer>> representing a single byte.
288
289* A <<literal-string,literal string>> representing a sequence of bytes
290 encoding UTF-8, UTF-16, or UTF-32 data.
291
292* A <<current-byte-order-setting,current byte order setting>> (big or
293 little endian).
294
295* A <<value,{py3} expression to be evaluated>> as an unsigned or signed
296 integer to be encoded on one or more bytes using the current byte
297 order.
298
299* A <<current-offset-setting,current offset setting>>.
300
301* A <<label,label>>, that is, a named constant holding the current
302 offset.
303+
304This is similar to an assembly label.
305
306* A <<variable-assignment,variable assignment>> associating a name to
307 the integral result of an evaluated {py3} expression.
308
309* A <<group,group>>, that is, a scoped sequence of items.
310
311Moreover, you can <<repetition,repeat>> any item above, except an offset
312or a label, a given number of times. This is called a repetition.
313
314A Normand comment may exist:
315
316* Between items, possibly within a group.
317* Between the nibbles of a constant hexadecimal byte.
318* Between the bits of a constant binary byte.
319* Between the last item and the ``pass:[*]`` character of a repetition,
320 and between that ``pass:[*]`` character and the following number.
321
322A comment is anything between two ``pass:[#]`` characters on the same
323line, or from ``pass:[#]`` until the end of the line. Whitespaces and
324the following symbol characters are also considered comments where a
325comment may exist:
326
327----
328! @ / \ ? & : ; . , + [ ] _ = | -
329----
330
331The latter serve to improve readability so that you may write, for
332example, a MAC address or a UUID as is.
333
334You can test the examples of this section with the `normand`
335<<command-line-tool,command-line tool>> as such:
336
337----
338$ normand file | hexdump -C
339----
340
341where `file` is the name of a file containing the Normand input.
342
343=== Byte constant
344
345A _byte constant_ represents a single byte.
346
347A byte constant is:
348
349Hexadecimal form::
350 Two consecutive hexits.
351
352Decimal form::
353 A decimal number after the `$` prefix.
354
355Binary form::
356 Eight bits after the `%` prefix.
357
358====
359Input:
360
361----
362ab cd [3d 8F] CC
363----
364
365Output:
366
367----
368ab cd 3d 8f cc
369----
370====
371
372====
373Input:
374
375----
376$192 %1100/0011 $ -77
377----
378
379Output:
380
381----
382c0 c3 b3
383----
384====
385
386====
387Input:
388
389----
39058f64689-6316-4d55-8a1a-04cada366172
391fe80::6257:18ff:fea3:4229
392----
393
394Output:
395
396----
39758 f6 46 89 63 16 4d 55 8a 1a 04 ca da 36 61 72 ┆ X•F•c•MU•••••6ar
398fe 80 62 57 18 ff fe a3 42 29 ┆ ••bW••••B)
399----
400====
401
402====
403Input:
404
405----
406%01110011 %01100001 %01101100 %01110101 %01110100
407----
408
409Output:
410
411----
41273 61 6c 75 74 ┆ salut
413----
414====
415
416=== Literal string
417
418A _literal string_ represents the UTF-8-, UTF-16-, or UTF-32-encoded
419bytes of a string.
420
421The string to encode isn't implicitly null-terminated: use `\0` at the
422end of the string to add a null character.
423
424A literal string is:
425
426. **Optional**: one of the following encodings instead of UTF-8:
427+
428--
429[horizontal]
430`u16be`:: UTF-16BE.
431`u16le`:: UTF-16LE.
432`u32be`:: UTF-32BE.
433`u32le`:: UTF-32LE.
434--
435
436. The ``pass:["]`` prefix.
437
438. A sequence of zero or more characters, possibly containing escape
439 sequences.
440+
441An escape sequence is the ``\`` character followed by one of:
442+
443--
444[horizontal]
445`0`:: Null (U+0000)
446`a`:: Alert (U+0007)
447`b`:: Backspace (U+0008)
448`e`:: Escape (U+001B)
449`f`:: Form feed (U+000C)
450`n`:: End of line (U+000A)
451`r`:: Carriage return (U+000D)
452`t`:: Character tabulation (U+0009)
453`v`:: Line tabulation (U+000B)
454``\``:: Reverse solidus (U+005C)
455``pass:["]``:: Quotation mark (U+0022)
456--
457
458. The ``pass:["]`` suffix.
459
460====
461Input:
462
463----
464"coucou tout le monde!"
465----
466
467Output:
468
469----
47063 6f 75 63 6f 75 20 74 6f 75 74 20 6c 65 20 6d ┆ coucou tout le m
4716f 6e 64 65 21 ┆ onde!
472----
473====
474
475====
476Input:
477
478----
479u16le"I am not young enough to know everything."
480----
481
482Output:
483
484----
48549 00 20 00 61 00 6d 00 20 00 6e 00 6f 00 74 00 ┆ I• •a•m• •n•o•t•
48620 00 79 00 6f 00 75 00 6e 00 67 00 20 00 65 00 ┆ •y•o•u•n•g• •e•
4876e 00 6f 00 75 00 67 00 68 00 20 00 74 00 6f 00 ┆ n•o•u•g•h• •t•o•
48820 00 6b 00 6e 00 6f 00 77 00 20 00 65 00 76 00 ┆ •k•n•o•w• •e•v•
48965 00 72 00 79 00 74 00 68 00 69 00 6e 00 67 00 ┆ e•r•y•t•h•i•n•g•
4902e 00 ┆ .•
491----
492====
493
494====
495Input:
496
497----
498u32be "\"illusion is the first\nof all pleasures\" 🦉"
499----
500
501Output:
502
503----
50400 00 00 22 00 00 00 69 00 00 00 6c 00 00 00 6c ┆ •••"•••i•••l•••l
50500 00 00 75 00 00 00 73 00 00 00 69 00 00 00 6f ┆ •••u•••s•••i•••o
50600 00 00 6e 00 00 00 20 00 00 00 69 00 00 00 73 ┆ •••n••• •••i•••s
50700 00 00 20 00 00 00 74 00 00 00 68 00 00 00 65 ┆ ••• •••t•••h•••e
50800 00 00 20 00 00 00 66 00 00 00 69 00 00 00 72 ┆ ••• •••f•••i•••r
50900 00 00 73 00 00 00 74 00 00 00 0a 00 00 00 6f ┆ •••s•••t•••••••o
51000 00 00 66 00 00 00 20 00 00 00 61 00 00 00 6c ┆ •••f••• •••a•••l
51100 00 00 6c 00 00 00 20 00 00 00 70 00 00 00 6c ┆ •••l••• •••p•••l
51200 00 00 65 00 00 00 61 00 00 00 73 00 00 00 75 ┆ •••e•••a•••s•••u
51300 00 00 72 00 00 00 65 00 00 00 73 00 00 00 22 ┆ •••r•••e•••s•••"
51400 00 00 20 00 01 f9 89 ┆ ••• ••••
515----
516====
517
518=== Current byte order setting
519
520This special item sets the <<cur-bo,_current byte order_>>.
521
522The two accepted forms are:
523
524[horizontal]
525``pass:[{be}]``:: Set the current byte order to big endian.
526``pass:[{le}]``:: Set the current byte order to little endian.
527
528=== Value
529
530A _value_ represents a fixed number of bytes encoding an unsigned or
531signed integer which is the result of evaluating a {py3} expression
532using the <<cur-bo,current byte order>>.
533
534For a value at some source location{nbsp}__**L**__, its {py3} expression
535may contain the name of any accessible <<label,label>>, including the
536name of a label defined after{nbsp}__**L**__, as well as the name of any
537<<variable-assignment,variable>> known at{nbsp}__**L**__.
538
539An accessible label is either:
540
541* Outside of the current <<group,group>>.
542* Within the same immediate group (not within a nested group).
543
544In the {py3} expression of a value, the value of the special name
545`ICITTE` is the <<cur-offset,current offset>> (before encoding the
546value).
547
548A value is:
549
550. The ``pass:[{]`` prefix.
551
552. A valid {py3} expression.
553
554. The `:` character.
555
556. An encoding length in bits amongst `8`, `16`, `24`, `32`, `40`,
557 `48`, `56`, and `64`.
558
559. The `}` suffix.
560
561====
562Input:
563
564----
565{le} {345:16}
566{be} {-0xabcd:32}
567----
568
569Output:
570
571----
57259 01 ff ff 54 33
573----
574====
575
576====
577Input:
578
579----
580{be}
581
582# String length in bits
583{8 * (str_end - str_beg) : 16}
584
585# String
586<str_beg>
587 "hello world!"
588<str_end>
589----
590
591Output:
592
593----
59400 60 68 65 6c 6c 6f 20 77 6f 72 6c 64 21 ┆ •`hello world!
595----
596====
597
598====
599Input:
600
601----
602{20 - ICITTE : 8} * 10
603----
604
605Output:
606
607----
60814 13 12 11 10 0f 0e 0d 0c 0b
609----
610====
611
612=== Current offset setting
613
614This special item sets the <<cur-offset,_current offset_>>.
615
616A current offset setting is:
617
618. The `<` prefix.
619
620. A positive integer (hexadecimal starting with `0x` or `0X` accepted)
621 which is the new current offset.
622
623. The `>` suffix.
624
625====
626Input:
627
628----
629 {ICITTE : 8} * 8
630<0x61> {ICITTE : 8} * 8
631----
632
633Output:
634
635----
63600 01 02 03 04 05 06 07 61 62 63 64 65 66 67 68 ┆ ••••••••abcdefgh
637----
638====
639
640====
641Input:
642
643----
644aa bb cc dd <meow> ee ff
645<12> 11 22 33 <mix> 44 55
646{meow : 8} {mix : 8}
647----
648
649Output:
650
651----
652aa bb cc dd ee ff 11 22 33 44 55 04 0f ┆ •••••••"3DU••
653----
654====
655
656=== Label
657
658A _label_ associates a name to the <<cur-offset,current offset>>.
659
660All the labels of a whole Normand input must have unique names.
661
662A label may not share the name of a <<variable-assignment,variable>>
663name.
664
665A label name may not be `ICITTE` (see <<value>> and
666<<variable-assignment>> to learn more).
667
668A label is:
669
670. The `<` prefix.
671
672. A valid {py3} name which is not `ICITTE`.
673
674. The `>` suffix.
675
676=== Variable assignment
677
678A _variable assignment_ associates a name to the integral result of an
679evaluated {py3} expression.
680
681For a variable assignment at some source location{nbsp}__**L**__, its
682{py3} expression may contain the name of any accessible <<label,label>>,
683including the name of a label defined after{nbsp}__**L**__, as well as
684the name of any variable known at{nbsp}__**L**__.
685
686An accessible label is either:
687
688* Outside of the current <<group,group>>.
689* Within the same immediate group (not within a nested group).
690
691A variable name may not be `ICITTE` (see <<value>> and
692<<variable-assignment>> to learn more).
693
694In the {py3} expression of a variable assignment, the special name
695`ICITTE` is the <<cur-offset,current offset>>.
696
697A variable is:
698
699. The ``pass:[{]`` prefix.
700
701. A valid {py3} name which is not `ICITTE`.
702
703. The `=` character.
704
705. A valid {py3} expression.
706
707. The `}` suffix.
708
709====
710Input:
711
712----
713{mix = 101} {le}
714{meow = 42} 11 22 {meow:8} 33 {meow = ICITTE + 17}
715"yooo" {meow + mix : 16}
716----
717
718Output:
719
720----
72111 22 2a 33 79 6f 6f 6f 7a 00 ┆ •"*3yoooz•
722----
723====
724
725=== Group
726
727A _group_ is a scoped sequence of items.
728
729The <<label,labels>> within a group aren't visible outside of it.
730
731The main purpose of a group is to <<repetition,repeat>> more than a
732single item.
733
734A group is:
735
736. The `(` prefix.
737
738. Zero or more items.
739
740. The `)` suffix.
741
742====
743Input:
744
745----
746((aa bb cc) dd () ee) "leclerc"
747----
748
749Output:
750
751----
752aa bb cc dd ee 6c 65 63 6c 65 72 63 ┆ •••••leclerc
753----
754====
755
756====
757Input:
758
759----
760((aa bb cc) * 3 dd ee) * 5
761----
762
763Output:
764
765----
766aa bb cc aa bb cc aa bb cc dd ee aa bb cc aa bb
767cc aa bb cc dd ee aa bb cc aa bb cc aa bb cc dd
768ee aa bb cc aa bb cc aa bb cc dd ee aa bb cc aa
769bb cc aa bb cc dd ee
770----
771====
772
773====
774Input:
775
776----
777{be}
778(
779 <str_beg> u16le"sébastien diaz" <str_end>
780 {ICITTE - str_beg : 8}
781 {(end - str_beg) * 5 : 24}
782) * 3
783<end>
784----
785
786Output:
787
788----
78973 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e•
7906e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 01 e0 ┆ n• •d•i•a•z•••••
79173 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e•
7926e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 01 40 ┆ n• •d•i•a•z••••@
79373 00 e9 00 62 00 61 00 73 00 74 00 69 00 65 00 ┆ s•••b•a•s•t•i•e•
7946e 00 20 00 64 00 69 00 61 00 7a 00 1c 00 00 a0 ┆ n• •d•i•a•z•••••
795----
796====
797
798=== Repetition
799
800A _repetition_ represents the bytes of an item repeated a given number
801of times.
802
803A repetition is:
804
805. Any item.
806
807. The ``pass:[*]`` character.
808
809. A positive integer (hexadecimal starting with `0x` or `0X` accepted)
810 which is the number of times to repeat the previous item.
811
812====
813Input:
814
815----
816{end - ICITTE - 1 : 8} * 0x100 <end>
817----
818
819Output:
820
821----
822ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ┆ ••••••••••••••••
823ef ee ed ec eb ea e9 e8 e7 e6 e5 e4 e3 e2 e1 e0 ┆ ••••••••••••••••
824df de dd dc db da d9 d8 d7 d6 d5 d4 d3 d2 d1 d0 ┆ ••••••••••••••••
825cf ce cd cc cb ca c9 c8 c7 c6 c5 c4 c3 c2 c1 c0 ┆ ••••••••••••••••
826bf be bd bc bb ba b9 b8 b7 b6 b5 b4 b3 b2 b1 b0 ┆ ••••••••••••••••
827af ae ad ac ab aa a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 ┆ ••••••••••••••••
8289f 9e 9d 9c 9b 9a 99 98 97 96 95 94 93 92 91 90 ┆ ••••••••••••••••
8298f 8e 8d 8c 8b 8a 89 88 87 86 85 84 83 82 81 80 ┆ ••••••••••••••••
8307f 7e 7d 7c 7b 7a 79 78 77 76 75 74 73 72 71 70 ┆ •~}|{zyxwvutsrqp
8316f 6e 6d 6c 6b 6a 69 68 67 66 65 64 63 62 61 60 ┆ onmlkjihgfedcba`
8325f 5e 5d 5c 5b 5a 59 58 57 56 55 54 53 52 51 50 ┆ _^]\[ZYXWVUTSRQP
8334f 4e 4d 4c 4b 4a 49 48 47 46 45 44 43 42 41 40 ┆ ONMLKJIHGFEDCBA@
8343f 3e 3d 3c 3b 3a 39 38 37 36 35 34 33 32 31 30 ┆ ?>=<;:9876543210
8352f 2e 2d 2c 2b 2a 29 28 27 26 25 24 23 22 21 20 ┆ /.-,+*)('&%$#"!
8361f 1e 1d 1c 1b 1a 19 18 17 16 15 14 13 12 11 10 ┆ ••••••••••••••••
8370f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00 ┆ ••••••••••••••••
838----
839====
840
841== Command-line tool
842
843If you <<install-normand,installed>> the `normand` package, then you
844can use the `normand` command-line tool:
845
846----
847$ normand <<< '"ma gang de malades"' | hexdump -C
848----
849
850----
85100000000 6d 61 20 67 61 6e 67 20 64 65 20 6d 61 6c 61 64 |ma gang de malad|
85200000010 65 73 |es|
853----
854
855If you copy the `normand.py` module to your own project, then you can
856run the module itself:
857
858----
859$ python3 -m normand <<< '"ma gang de malades"' | hexdump -C
860----
861
862----
86300000000 6d 61 20 67 61 6e 67 20 64 65 20 6d 61 6c 61 64 |ma gang de malad|
86400000010 65 73 |es|
865----
866
867Without a path argument, the `normand` tool reads from the standard
868input.
869
870The `normand` tool prints the generated binary data to the standard
871output.
872
873Various options control the initial <<state,state>> of the processor:
874use the `--help` option to learn more.
875
876== {py3} API
877
878The whole `normand` package/module API is:
879
880[source,python]
881----
882class ByteOrder(enum.Enum):
883 # Big endian.
884 BE = ...
885
886 # Little endian.
887 LE = ...
888
889
890VarsT = typing.Dict[str, int]
891
892
893class TextLoc:
894 # Line number.
895 @property
896 def line_no(self) -> int:
897 ...
898
899 # Column number.
900 @property
901 def col_no(self) -> int:
902 ...
903
904
905class ParseError(RuntimeError):
906 # Source text location.
907 @property
908 def text_loc(self) -> TextLoc:
909 ...
910
911
912class ParseResult:
913 # Generated data.
914 @property
915 def data(self) -> bytearray:
916 ...
917
918 # Updated variable values.
919 @property
920 def variables(self) -> VarsT:
921 ...
922
923 # Updated main group label values.
924 @property
925 def labels(self) -> VarsT:
926 ...
927
928 # Final offset.
929 @property
930 def offset(self) -> int:
931 ...
932
933 # Final byte order.
934 @property
935 def byte_order(self) -> typing.Optional[int]:
936 ...
937
938def parse(normand: str,
939 init_variables: typing.Optional[VarsT] = None,
940 init_labels: typing.Optional[VarsT] = None,
941 init_offset: int = 0,
942 init_byte_order: typing.Optional[ByteOrder] = None) -> ParseResult:
943 ...
944----
945
946The `normand` parameter is the actual <<learn-normand,Normand input>>
947while the other parameters control the initial <<state,state>>.
948
949The `parse()` function raises a `ParseError` instance should it fail to
950parse the `normand` string for any reason.
This page took 0.053353 seconds and 4 git commands to generate.