1 (* Bitmatch syntax extension.
2 * Copyright (C) 2008 Red Hat Inc., Richard W.M. Jones
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 * $Id: pa_bitmatch.ml,v 1.11 2008-04-25 14:57:11 rjones Exp $
27 (* If this is true then we emit some debugging code which can
28 * be useful to tell what is happening during matches. You
29 * also need to do 'Bitmatch.debug := true' in your main program.
31 * If this is false then no extra debugging code is emitted.
35 (* Work out if an expression is an integer constant.
37 * Returns [Some i] if so (where i is the integer value), else [None].
39 * Fairly simplistic algorithm: we can only detect simple constant
40 * expressions such as [k], [k+c], [k-c] etc.
42 let rec expr_is_constant = function
43 | <:expr< $int:i$ >> -> (* Literal integer constant. *)
44 Some (int_of_string i)
45 | <:expr< $a$ + $b$ >> -> (* Addition of constants. *)
46 (match expr_is_constant a, expr_is_constant b with
47 | Some a, Some b -> Some (a+b)
49 | <:expr< $a$ - $b$ >> -> (* Subtraction. *)
50 (match expr_is_constant a, expr_is_constant b with
51 | Some a, Some b -> Some (a-b)
53 | <:expr< $a$ * $b$ >> -> (* Multiplication. *)
54 (match expr_is_constant a, expr_is_constant b with
55 | Some a, Some b -> Some (a*b)
57 | <:expr< $a$ / $b$ >> -> (* Division. *)
58 (match expr_is_constant a, expr_is_constant b with
59 | Some a, Some b -> Some (a/b)
61 | <:expr< $a$ lsl $b$ >> -> (* Shift left. *)
62 (match expr_is_constant a, expr_is_constant b with
63 | Some a, Some b -> Some (a lsl b)
65 | <:expr< $a$ lsr $b$ >> -> (* Shift right. *)
66 (match expr_is_constant a, expr_is_constant b with
67 | Some a, Some b -> Some (a lsr b)
69 | _ -> None (* Anything else is not constant. *)
71 (* Field. In bitmatch (patterns) the type is [patt field]. In
72 * BITSTRING (constructor) the type is [expr field].
75 field : 'a; (* field ('a is either patt or expr) *)
76 flen : expr; (* length in bits, may be non-const *)
77 endian : endian; (* endianness *)
78 signed : bool; (* true if signed, false if unsigned *)
80 _loc : Loc.t; (* location in source code *)
81 printer : 'a -> string; (* turn the field into a string *)
83 and endian = BigEndian | LittleEndian | NativeEndian
84 and t = Int | String | Bitstring
86 (* Generate a fresh, unique symbol each time called. *)
91 sprintf "__pabitmatch_%s_%d" name i
93 (* Deal with the qualifiers which appear for a field of both types. *)
94 let parse_field _loc field flen qs printer =
95 let endian, signed, t =
97 | None -> (None, None, None)
100 fun (endian, signed, t) q ->
103 if endian <> None then
104 Loc.raise _loc (Failure "an endian flag has been set already")
106 let endian = Some BigEndian in
110 if endian <> None then
111 Loc.raise _loc (Failure "an endian flag has been set already")
113 let endian = Some LittleEndian in
117 if endian <> None then
118 Loc.raise _loc (Failure "an endian flag has been set already")
120 let endian = Some NativeEndian in
124 if signed <> None then
125 Loc.raise _loc (Failure "a signed flag has been set already")
127 let signed = Some true in
131 if signed <> None then
132 Loc.raise _loc (Failure "a signed flag has been set already")
134 let signed = Some false in
139 Loc.raise _loc (Failure "a type flag has been set already")
146 Loc.raise _loc (Failure "a type flag has been set already")
148 let t = Some String in
153 Loc.raise _loc (Failure "a type flag has been set already")
155 let t = Some Bitstring in
159 Loc.raise _loc (Failure (s ^ ": unknown qualifier"))
160 ) (None, None, None) qs in
162 (* If type is set to string or bitstring then endianness and
163 * signedness qualifiers are meaningless and must not be set.
165 if (t = Some Bitstring || t = Some String)
166 && (endian <> None || signed <> None) then
168 Failure "string types and endian or signed qualifiers cannot be mixed"
171 (* Default endianness, signedness, type. *)
172 let endian = match endian with None -> BigEndian | Some e -> e in
173 let signed = match signed with None -> false | Some s -> s in
174 let t = match t with None -> Int | Some t -> t in
186 let string_of_endian = function
187 | BigEndian -> "bigendian"
188 | LittleEndian -> "littleendian"
189 | NativeEndian -> "nativeendian"
191 let string_of_t = function
194 | Bitstring -> "bitstring"
196 let patt_printer = function
197 | <:patt< $lid:id$ >> -> id
200 let expr_printer = function
201 | <:expr< $lid:id$ >> -> id
202 | _ -> "[expression]"
204 let string_of_field { field = field; flen = flen;
205 endian = endian; signed = signed; t = t;
209 match expr_is_constant flen with
210 | Some i -> string_of_int i
211 | None -> "[non-const-len]" in
212 let endian = string_of_endian endian in
213 let signed = if signed then "signed" else "unsigned" in
214 let t = string_of_t t in
215 let loc_fname = Loc.file_name _loc in
216 let loc_line = Loc.start_line _loc in
217 let loc_char = Loc.start_off _loc - Loc.start_bol _loc in
219 sprintf "%s : %s : %s, %s, %s @ (%S, %d, %d)"
220 (printer field) flen t endian signed loc_fname loc_line loc_char
222 (* Generate the code for a constructor, ie. 'BITSTRING ...'. *)
223 let output_constructor _loc fields =
224 let loc_fname = Loc.file_name _loc in
225 let loc_line = string_of_int (Loc.start_line _loc) in
226 let loc_char = string_of_int (Loc.start_off _loc - Loc.start_bol _loc) in
228 (* Bitstrings are created like the 'Buffer' module (in fact, using
229 * the Buffer module), by appending snippets to a growing buffer.
230 * This is reasonably efficient and avoids a lot of garbage.
232 let buffer = gensym "buffer" in
234 (* General exception which is raised inside the constructor functions
235 * when an int expression is out of range at runtime.
237 let exn = gensym "exn" in
238 let exn_used = ref false in
240 (* Convert each field to a simple bitstring-generating expression. *)
241 let fields = List.map (
242 fun {field=fexpr; flen=flen; endian=endian; signed=signed;
244 (* Is flen an integer constant? If so, what is it? This
245 * is very simple-minded and only detects simple constants.
247 let flen_is_const = expr_is_constant flen in
249 let name_of_int_construct_const = function
250 (* XXX As an enhancement we should allow a 64-bit-only
251 * mode which lets us use 'int' up to 63 bits and won't
252 * compile on 32-bit platforms.
254 (* XXX The meaning of signed/unsigned breaks down at
255 * 31, 32, 63 and 64 bits.
257 | (1, _, _) -> "construct_bit"
258 | ((2|3|4|5|6|7|8), _, false) -> "construct_char_unsigned"
259 | ((2|3|4|5|6|7|8), _, true) -> "construct_char_signed"
260 | (i, BigEndian, false) when i <= 31 -> "construct_int_be_unsigned"
261 | (i, BigEndian, true) when i <= 31 -> "construct_int_be_signed"
262 | (i, LittleEndian, false) when i <= 31 -> "construct_int_le_unsigned"
263 | (i, LittleEndian, true) when i <= 31 -> "construct_int_le_signed"
264 | (i, NativeEndian, false) when i <= 31 -> "construct_int_ne_unsigned"
265 | (i, NativeEndian, true) when i <= 31 -> "construct_int_ne_signed"
266 | (32, BigEndian, false) -> "construct_int32_be_unsigned"
267 | (32, BigEndian, true) -> "construct_int32_be_signed"
268 | (32, LittleEndian, false) -> "construct_int32_le_unsigned"
269 | (32, LittleEndian, true) -> "construct_int32_le_signed"
270 | (32, NativeEndian, false) -> "construct_int32_ne_unsigned"
271 | (32, NativeEndian, true) -> "construct_int32_ne_signed"
272 | (_, BigEndian, false) -> "construct_int64_be_unsigned"
273 | (_, BigEndian, true) -> "construct_int64_be_signed"
274 | (_, LittleEndian, false) -> "construct_int64_le_unsigned"
275 | (_, LittleEndian, true) -> "construct_int64_le_signed"
276 | (_, NativeEndian, false) -> "construct_int64_ne_unsigned"
277 | (_, NativeEndian, true) -> "construct_int64_ne_signed"
279 let name_of_int_construct = function
280 (* XXX As an enhancement we should allow users to
281 * specify that a field length can fit into a char/int/int32
282 * (of course, this would have to be checked at runtime).
284 | (BigEndian, false) -> "construct_int64_be_unsigned"
285 | (BigEndian, true) -> "construct_int64_be_signed"
286 | (LittleEndian, false) -> "construct_int64_le_unsigned"
287 | (LittleEndian, true) -> "construct_int64_le_signed"
288 | (NativeEndian, false) -> "construct_int64_ne_unsigned"
289 | (NativeEndian, true) -> "construct_int64_ne_signed"
293 match t, flen_is_const with
294 (* Common case: int field, constant flen.
296 * Range checks are done inside the construction function
297 * because that's a lot simpler w.r.t. types. It might
298 * be better to move them here. XXX
300 | Int, Some i when i > 0 && i <= 64 ->
302 name_of_int_construct_const (i,endian,signed) in
306 Bitmatch.$lid:construct_func$ $lid:buffer$ $fexpr$ $flen$
311 Loc.raise _loc (Failure "length of int field must be [1..64]")
313 (* Int field, non-constant length. We need to perform a runtime
314 * test to ensure the length is [1..64].
316 * Range checks are done inside the construction function
317 * because that's a lot simpler w.r.t. types. It might
318 * be better to move them here. XXX
321 let construct_func = name_of_int_construct (endian,signed) in
325 if $flen$ >= 1 && $flen$ <= 64 then
326 Bitmatch.$lid:construct_func$ $lid:buffer$ $fexpr$ $flen$
329 raise (Bitmatch.Construct_failure
330 ("length of int field must be [1..64]",
332 $int:loc_line$, $int:loc_char$))
335 (* String, constant length > 0, must be a multiple of 8. *)
336 | String, Some i when i > 0 && i land 7 = 0 ->
337 let bs = gensym "bs" in
339 let $lid:bs$ = $fexpr$ in
340 if String.length $lid:bs$ = ($flen$ lsr 3) then
341 Bitmatch.construct_string $lid:buffer$ $lid:bs$
343 raise (Bitmatch.Construct_failure
344 ("length of string does not match declaration",
346 $int:loc_line$, $int:loc_char$))
349 (* String, constant length -1, means variable length string
352 | String, Some (-1) ->
353 <:expr< Bitmatch.construct_string $lid:buffer$ $fexpr$ >>
355 (* String, constant length = 0 is probably an error, and so is
359 Loc.raise _loc (Failure "length of string must be > 0 and a multiple of 8, or the special value -1")
361 (* String, non-constant length.
362 * We check at runtime that the length is > 0, a multiple of 8,
363 * and matches the declared length.
366 let bslen = gensym "bslen" in
367 let bs = gensym "bs" in
369 let $lid:bslen$ = $flen$ in
370 if $lid:bslen$ > 0 then (
371 if $lid:bslen$ land 7 = 0 then (
372 let $lid:bs$ = $fexpr$ in
373 if String.length $lid:bs$ = ($lid:bslen$ lsr 3) then
374 Bitmatch.construct_string $lid:buffer$ $lid:bs$
376 raise (Bitmatch.Construct_failure
377 ("length of string does not match declaration",
379 $int:loc_line$, $int:loc_char$))
381 raise (Bitmatch.Construct_failure
382 ("length of string must be a multiple of 8",
384 $int:loc_line$, $int:loc_char$))
386 raise (Bitmatch.Construct_failure
387 ("length of string must be > 0",
389 $int:loc_line$, $int:loc_char$))
392 (* Bitstring, constant length > 0. *)
393 | Bitstring, Some i when i > 0 ->
394 let bs = gensym "bs" in
396 let $lid:bs$ = $fexpr$ in
397 if Bitmatch.bitstring_length $lid:bs$ = $flen$ then
398 Bitmatch.construct_bitstring $lid:buffer$ $lid:bs$
400 raise (Bitmatch.Construct_failure
401 ("length of bitstring does not match declaration",
403 $int:loc_line$, $int:loc_char$))
406 (* Bitstring, constant length -1, means variable length bitstring
409 | Bitstring, Some (-1) ->
410 <:expr< Bitmatch.construct_bitstring $lid:buffer$ $fexpr$ >>
412 (* Bitstring, constant length = 0 is probably an error, and so is
415 | Bitstring, Some _ ->
418 "length of bitstring must be > 0 or the special value -1")
420 (* Bitstring, non-constant length.
421 * We check at runtime that the length is > 0 and matches
422 * the declared length.
425 let bslen = gensym "bslen" in
426 let bs = gensym "bs" in
428 let $lid:bslen$ = $flen$ in
429 if $lid:bslen$ > 0 then (
430 let $lid:bs$ = $fexpr$ in
431 if Bitmatch.bitstring_length $lid:bs$ = $lid:bslen$ then
432 Bitmatch.construct_bitstring $lid:buffer$ $lid:bs$
434 raise (Bitmatch.Construct_failure
435 ("length of bitstring does not match declaration",
437 $int:loc_line$, $int:loc_char$))
439 raise (Bitmatch.Construct_failure
440 ("length of bitstring must be > 0",
442 $int:loc_line$, $int:loc_char$))
447 (* Create the final bitstring. Start by creating an empty buffer
448 * and then evaluate each expression above in turn which will
449 * append some more to the bitstring buffer. Finally extract
452 * XXX We almost have enough information to be able to guess
453 * a good initial size for the buffer.
457 | [] -> <:expr< [] >>
458 | h::t -> List.fold_left (fun h t -> <:expr< $h$; $t$ >>) h t in
462 let $lid:buffer$ = Bitmatch.Buffer.create () in
464 Bitmatch.Buffer.contents $lid:buffer$
470 Bitmatch.Construct_failure ("value out of range",
472 $int:loc_line$, $int:loc_char$) in
478 (* Generate the code for a bitmatch statement. '_loc' is the
479 * location, 'bs' is the bitstring parameter, 'cases' are
480 * the list of cases to test against.
482 let output_bitmatch _loc bs cases =
483 let data = gensym "data" and off = gensym "off" and len = gensym "len" in
484 let result = gensym "result" in
486 (* This generates the field extraction code for each
487 * field a single case. Each field must be wider than
488 * the minimum permitted for the type and there must be
489 * enough remaining data in the bitstring to satisfy it.
490 * As we go through the fields, symbols 'data', 'off' and 'len'
491 * track our position and remaining length in the bitstring.
493 * The whole thing is a lot of nested 'if' statements. Code
494 * is generated from the inner-most (last) field outwards.
496 let rec output_field_extraction inner = function
499 let {field=fpatt; flen=flen; endian=endian; signed=signed;
503 (* Is flen an integer constant? If so, what is it? This
504 * is very simple-minded and only detects simple constants.
506 let flen_is_const = expr_is_constant flen in
508 let name_of_int_extract_const = function
509 (* XXX As an enhancement we should allow a 64-bit-only
510 * mode which lets us use 'int' up to 63 bits and won't
511 * compile on 32-bit platforms.
513 (* XXX The meaning of signed/unsigned breaks down at
514 * 31, 32, 63 and 64 bits.
516 | (1, _, _) -> "extract_bit"
517 | ((2|3|4|5|6|7|8), _, false) -> "extract_char_unsigned"
518 | ((2|3|4|5|6|7|8), _, true) -> "extract_char_signed"
519 | (i, BigEndian, false) when i <= 31 -> "extract_int_be_unsigned"
520 | (i, BigEndian, true) when i <= 31 -> "extract_int_be_signed"
521 | (i, LittleEndian, false) when i <= 31 -> "extract_int_le_unsigned"
522 | (i, LittleEndian, true) when i <= 31 -> "extract_int_le_signed"
523 | (i, NativeEndian, false) when i <= 31 -> "extract_int_ne_unsigned"
524 | (i, NativeEndian, true) when i <= 31 -> "extract_int_ne_signed"
525 | (32, BigEndian, false) -> "extract_int32_be_unsigned"
526 | (32, BigEndian, true) -> "extract_int32_be_signed"
527 | (32, LittleEndian, false) -> "extract_int32_le_unsigned"
528 | (32, LittleEndian, true) -> "extract_int32_le_signed"
529 | (32, NativeEndian, false) -> "extract_int32_ne_unsigned"
530 | (32, NativeEndian, true) -> "extract_int32_ne_signed"
531 | (_, BigEndian, false) -> "extract_int64_be_unsigned"
532 | (_, BigEndian, true) -> "extract_int64_be_signed"
533 | (_, LittleEndian, false) -> "extract_int64_le_unsigned"
534 | (_, LittleEndian, true) -> "extract_int64_le_signed"
535 | (_, NativeEndian, false) -> "extract_int64_ne_unsigned"
536 | (_, NativeEndian, true) -> "extract_int64_ne_signed"
538 let name_of_int_extract = function
539 (* XXX As an enhancement we should allow users to
540 * specify that a field length can fit into a char/int/int32
541 * (of course, this would have to be checked at runtime).
543 | (BigEndian, false) -> "extract_int64_be_unsigned"
544 | (BigEndian, true) -> "extract_int64_be_signed"
545 | (LittleEndian, false) -> "extract_int64_le_unsigned"
546 | (LittleEndian, true) -> "extract_int64_le_signed"
547 | (NativeEndian, false) -> "extract_int64_ne_unsigned"
548 | (NativeEndian, true) -> "extract_int64_ne_signed"
552 match t, flen_is_const with
553 (* Common case: int field, constant flen *)
554 | Int, Some i when i > 0 && i <= 64 ->
555 let extract_func = name_of_int_extract_const (i,endian,signed) in
556 let v = gensym "val" in
558 if $lid:len$ >= $flen$ then (
559 let $lid:v$, $lid:off$, $lid:len$ =
560 Bitmatch.$lid:extract_func$ $lid:data$ $lid:off$ $lid:len$
562 match $lid:v$ with $fpatt$ when true -> $inner$ | _ -> ()
567 Loc.raise _loc (Failure "length of int field must be [1..64]")
569 (* Int field, non-const flen. We have to test the range of
570 * the field at runtime. If outside the range it's a no-match
574 let extract_func = name_of_int_extract (endian,signed) in
575 let v = gensym "val" in
577 if $flen$ >= 1 && $flen$ <= 64 && $flen$ <= $lid:len$ then (
578 let $lid:v$, $lid:off$, $lid:len$ =
579 Bitmatch.$lid:extract_func$ $lid:data$ $lid:off$ $lid:len$
581 match $lid:v$ with $fpatt$ when true -> $inner$ | _ -> ()
585 (* String, constant flen > 0. *)
586 | String, Some i when i > 0 && i land 7 = 0 ->
587 let bs = gensym "bs" in
589 if $lid:len$ >= $flen$ then (
590 let $lid:bs$, $lid:off$, $lid:len$ =
591 Bitmatch.extract_bitstring $lid:data$ $lid:off$ $lid:len$
593 match Bitmatch.string_of_bitstring $lid:bs$ with
594 | $fpatt$ when true -> $inner$
599 (* String, constant flen = -1, means consume all the
602 | String, Some i when i = -1 ->
603 let bs = gensym "bs" in
605 let $lid:bs$, $lid:off$, $lid:len$ =
606 Bitmatch.extract_remainder $lid:data$ $lid:off$ $lid:len$ in
607 match Bitmatch.string_of_bitstring $lid:bs$ with
608 | $fpatt$ when true -> $inner$
613 Loc.raise _loc (Failure "length of string must be > 0 and a multiple of 8, or the special value -1")
615 (* String field, non-const flen. We check the flen is > 0
616 * and a multiple of 8 (-1 is not allowed here), at runtime.
619 let bs = gensym "bs" in
621 if $flen$ >= 0 && $flen$ <= $lid:len$
622 && $flen$ land 7 = 0 then (
623 let $lid:bs$, $lid:off$, $lid:len$ =
624 Bitmatch.extract_bitstring
625 $lid:data$ $lid:off$ $lid:len$ $flen$ in
626 match Bitmatch.string_of_bitstring $lid:bs$ with
627 | $fpatt$ when true -> $inner$
632 (* Bitstring, constant flen >= 0.
633 * At the moment all we can do is assign the bitstring to an
636 | Bitstring, Some i when i >= 0 ->
639 | <:patt< $lid:ident$ >> -> ident
640 | <:patt< _ >> -> "_"
643 (Failure "cannot compare a bitstring to a constant") in
645 if $lid:len$ >= $flen$ then (
646 let $lid:ident$, $lid:off$, $lid:len$ =
647 Bitmatch.extract_bitstring $lid:data$ $lid:off$ $lid:len$
653 (* Bitstring, constant flen = -1, means consume all the
656 | Bitstring, Some i when i = -1 ->
659 | <:patt< $lid:ident$ >> -> ident
662 (Failure "cannot compare a bitstring to a constant") in
664 let $lid:ident$, $lid:off$, $lid:len$ =
665 Bitmatch.extract_remainder $lid:data$ $lid:off$ $lid:len$ in
669 | Bitstring, Some _ ->
670 Loc.raise _loc (Failure "length of bitstring must be >= 0 or the special value -1")
672 (* Bitstring field, non-const flen. We check the flen is >= 0
673 * (-1 is not allowed here) at runtime.
678 | <:patt< $lid:ident$ >> -> ident
681 (Failure "cannot compare a bitstring to a constant") in
683 if $flen$ >= 0 && $flen$ <= $lid:len$ then (
684 let $lid:ident$, $lid:off$, $lid:len$ =
685 Bitmatch.extract_bitstring $lid:data$ $lid:off$ $lid:len$
692 (* Emit extra debugging code. *)
694 if not debug then expr else (
695 let field = string_of_field field in
698 if !Bitmatch.debug then (
699 Printf.eprintf "PA_BITMATCH: TEST:\n";
700 Printf.eprintf " %s\n" $str:field$;
701 Printf.eprintf " off %d len %d\n%!" $lid:off$ $lid:len$;
702 (*Bitmatch.hexdump_bitstring stderr
703 ($lid:data$,$lid:off$,$lid:len$);*)
709 output_field_extraction expr fields
712 (* Convert each case in the match. *)
713 let cases = List.map (
714 fun (fields, bind, whenclause, code) ->
715 let inner = <:expr< $lid:result$ := Some ($code$); raise Exit >> in
717 match whenclause with
719 <:expr< if $whenclause$ then $inner$ >>
725 let $lid:name$ = ($lid:data$, $lid:off$, $lid:len$) in
729 output_field_extraction inner (List.rev fields)
732 (* Join them into a single expression.
734 * Don't do it with a normal fold_right because that leaves
735 * 'raise Exit; ()' at the end which causes a compiler warning.
736 * Hence a bit of complexity here.
738 * Note that the number of cases is always >= 1 so List.hd is safe.
740 let cases = List.rev cases in
742 List.fold_left (fun base case -> <:expr< $case$ ; $base$ >>)
743 (List.hd cases) (List.tl cases) in
745 (* The final code just wraps the list of cases in a
746 * try/with construct so that each case is tried in
747 * turn until one case matches (that case sets 'result'
748 * and raises 'Exit' to leave the whole statement).
749 * If result isn't set by the end then we will raise
750 * Match_failure with the location of the bitmatch
751 * statement in the original code.
753 let loc_fname = Loc.file_name _loc in
754 let loc_line = string_of_int (Loc.start_line _loc) in
755 let loc_char = string_of_int (Loc.start_off _loc - Loc.start_bol _loc) in
758 let ($lid:data$, $lid:off$, $lid:len$) = $bs$ in
759 let $lid:result$ = ref None in
763 match ! $lid:result$ with
765 | None -> raise (Match_failure ($str:loc_fname$,
766 $int:loc_line$, $int:loc_char$))
773 [ LIST0 [ q = LIDENT -> q ] SEP "," ]
776 (* Field used in the bitmatch operator (a pattern). *)
778 [ fpatt = patt; ":"; len = expr LEVEL "top";
779 qs = OPT [ ":"; qs = qualifiers -> qs ] ->
780 parse_field _loc fpatt len qs patt_printer
784 (* Case inside bitmatch operator. *)
787 fields = LIST0 patt_field SEP ";";
789 bind = OPT [ "as"; name = LIDENT -> name ];
790 whenclause = OPT [ "when"; e = expr -> e ]; "->";
792 (fields, bind, whenclause, code)
796 (* Field used in the BITSTRING constructor (an expression). *)
798 [ fexpr = expr LEVEL "top"; ":"; len = expr LEVEL "top";
799 qs = OPT [ ":"; qs = qualifiers -> qs ] ->
800 parse_field _loc fexpr len qs expr_printer
804 (* 'bitmatch' expressions. *)
807 bs = expr; "with"; OPT "|";
808 cases = LIST1 match_case SEP "|" ->
809 output_bitmatch _loc bs cases
813 | [ "BITSTRING"; "{";
814 fields = LIST0 constr_field SEP ";";
816 output_constructor _loc fields