src/HOL/Tools/string_syntax.ML
author haftmann
Wed Dec 08 13:34:50 2010 +0100 (2010-12-08)
changeset 41075 4bed56dc95fb
parent 40627 becf5d5187cc
child 42224 578a51fae383
permissions -rw-r--r--
primitive definitions of bot/top/inf/sup for bool and fun are named with canonical suffix `_def` rather than `_eq`
wenzelm@21759
     1
(*  Title:      HOL/Tools/string_syntax.ML
wenzelm@21759
     2
    Author:     Makarius
wenzelm@21759
     3
wenzelm@21759
     4
Concrete syntax for hex chars and strings.
wenzelm@21759
     5
*)
wenzelm@21759
     6
wenzelm@21759
     7
signature STRING_SYNTAX =
wenzelm@21759
     8
sig
wenzelm@21759
     9
  val setup: theory -> theory
wenzelm@21759
    10
end;
wenzelm@21759
    11
wenzelm@35123
    12
structure String_Syntax: STRING_SYNTAX =
wenzelm@21759
    13
struct
wenzelm@21759
    14
wenzelm@21759
    15
wenzelm@21759
    16
(* nibble *)
wenzelm@21759
    17
wenzelm@21759
    18
val mk_nib =
wenzelm@35262
    19
  Syntax.Constant o Syntax.mark_const o
wenzelm@35123
    20
    fst o Term.dest_Const o HOLogic.mk_nibble;
wenzelm@21759
    21
wenzelm@35256
    22
fun dest_nib (Syntax.Constant s) =
wenzelm@35262
    23
  (case try Syntax.unmark_const s of
wenzelm@35256
    24
    NONE => raise Match
wenzelm@35256
    25
  | SOME c => (HOLogic.dest_nibble (Const (c, HOLogic.nibbleT)) handle TERM _ => raise Match));
wenzelm@21759
    26
wenzelm@21759
    27
wenzelm@21759
    28
(* char *)
wenzelm@21759
    29
wenzelm@21759
    30
fun mk_char s =
wenzelm@21759
    31
  if Symbol.is_ascii s then
wenzelm@35115
    32
    Syntax.Appl [Syntax.Constant @{const_syntax Char}, mk_nib (ord s div 16), mk_nib (ord s mod 16)]
wenzelm@21759
    33
  else error ("Non-ASCII symbol: " ^ quote s);
wenzelm@21759
    34
wenzelm@40627
    35
val specials = raw_explode "\\\"`'";
wenzelm@21759
    36
wenzelm@21759
    37
fun dest_chr c1 c2 =
wenzelm@21759
    38
  let val c = chr (dest_nib c1 * 16 + dest_nib c2) in
wenzelm@21759
    39
    if not (member (op =) specials c) andalso Symbol.is_ascii c andalso Symbol.is_printable c
wenzelm@21759
    40
    then c else raise Match
wenzelm@21759
    41
  end;
wenzelm@21759
    42
wenzelm@35115
    43
fun dest_char (Syntax.Appl [Syntax.Constant @{const_syntax Char}, c1, c2]) = dest_chr c1 c2
wenzelm@21759
    44
  | dest_char _ = raise Match;
wenzelm@21759
    45
wenzelm@29317
    46
fun syntax_string cs =
wenzelm@35115
    47
  Syntax.Appl
wenzelm@35115
    48
    [Syntax.Constant @{syntax_const "_inner_string"},
wenzelm@35115
    49
      Syntax.Variable (Syntax.implode_xstr cs)];
wenzelm@21759
    50
wenzelm@21759
    51
wenzelm@21759
    52
fun char_ast_tr [Syntax.Variable xstr] =
wenzelm@21759
    53
    (case Syntax.explode_xstr xstr of
wenzelm@21759
    54
      [c] => mk_char c
wenzelm@21759
    55
    | _ => error ("Single character expected: " ^ xstr))
wenzelm@21759
    56
  | char_ast_tr asts = raise AST ("char_ast_tr", asts);
wenzelm@21759
    57
wenzelm@35115
    58
fun char_ast_tr' [c1, c2] =
wenzelm@35115
    59
      Syntax.Appl [Syntax.Constant @{syntax_const "_Char"}, syntax_string [dest_chr c1 c2]]
wenzelm@21759
    60
  | char_ast_tr' _ = raise Match;
wenzelm@21759
    61
wenzelm@21759
    62
wenzelm@21759
    63
(* string *)
wenzelm@21759
    64
wenzelm@35115
    65
fun mk_string [] = Syntax.Constant @{const_syntax Nil}
wenzelm@35115
    66
  | mk_string (c :: cs) =
wenzelm@35115
    67
      Syntax.Appl [Syntax.Constant @{const_syntax Cons}, mk_char c, mk_string cs];
wenzelm@21759
    68
wenzelm@21759
    69
fun string_ast_tr [Syntax.Variable xstr] =
wenzelm@21759
    70
    (case Syntax.explode_xstr xstr of
wenzelm@35115
    71
      [] =>
wenzelm@35115
    72
        Syntax.Appl
wenzelm@35115
    73
          [Syntax.Constant Syntax.constrainC,
wenzelm@35363
    74
            Syntax.Constant @{const_syntax Nil}, Syntax.Constant @{type_syntax string}]
wenzelm@21759
    75
    | cs => mk_string cs)
wenzelm@21759
    76
  | string_ast_tr asts = raise AST ("string_tr", asts);
wenzelm@21759
    77
wenzelm@35115
    78
fun list_ast_tr' [args] =
wenzelm@35115
    79
      Syntax.Appl [Syntax.Constant @{syntax_const "_String"},
wenzelm@35115
    80
        syntax_string (map dest_char (Syntax.unfold_ast @{syntax_const "_args"} args))]
wenzelm@21759
    81
  | list_ast_tr' ts = raise Match;
wenzelm@21759
    82
wenzelm@21759
    83
wenzelm@21759
    84
(* theory setup *)
wenzelm@21759
    85
wenzelm@21759
    86
val setup =
wenzelm@24712
    87
  Sign.add_trfuns
wenzelm@35115
    88
   ([(@{syntax_const "_Char"}, char_ast_tr), (@{syntax_const "_String"}, string_ast_tr)], [], [],
wenzelm@35115
    89
    [(@{const_syntax Char}, char_ast_tr'), (@{syntax_const "_list"}, list_ast_tr')]);
wenzelm@21759
    90
wenzelm@21759
    91
end;