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