src/HOL/Tools/string_syntax.ML
author haftmann
Wed May 06 16:01:06 2009 +0200 (2009-05-06)
changeset 31048 ac146fc38b51
parent 29317 9faf1dfb4e7c
child 35115 446c5063e4fd
permissions -rw-r--r--
refined HOL string theories and corresponding ML fragments
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@21759
    12
structure StringSyntax: STRING_SYNTAX =
wenzelm@21759
    13
struct
wenzelm@21759
    14
wenzelm@21759
    15
wenzelm@21759
    16
(* nibble *)
wenzelm@21759
    17
haftmann@31048
    18
val nib_prefix = "String.nibble.";
haftmann@31048
    19
wenzelm@21759
    20
val mk_nib =
haftmann@31048
    21
  Syntax.Constant o unprefix nib_prefix o
wenzelm@21759
    22
  fst o Term.dest_Const o HOLogic.mk_nibble;
wenzelm@21759
    23
wenzelm@21759
    24
fun dest_nib (Syntax.Constant c) =
haftmann@31048
    25
  HOLogic.dest_nibble (Const (nib_prefix ^ c, dummyT))
wenzelm@21759
    26
    handle TERM _ => raise Match;
wenzelm@21759
    27
wenzelm@21759
    28
wenzelm@21759
    29
(* char *)
wenzelm@21759
    30
wenzelm@21759
    31
fun mk_char s =
wenzelm@21759
    32
  if Symbol.is_ascii s then
wenzelm@21759
    33
    Syntax.Appl [Syntax.Constant "Char", mk_nib (ord s div 16), mk_nib (ord s mod 16)]
wenzelm@21759
    34
  else error ("Non-ASCII symbol: " ^ quote s);
wenzelm@21759
    35
wenzelm@21775
    36
val specials = explode "\\\"`'";
wenzelm@21759
    37
wenzelm@21759
    38
fun dest_chr c1 c2 =
wenzelm@21759
    39
  let val c = chr (dest_nib c1 * 16 + dest_nib c2) in
wenzelm@21759
    40
    if not (member (op =) specials c) andalso Symbol.is_ascii c andalso Symbol.is_printable c
wenzelm@21759
    41
    then c else raise Match
wenzelm@21759
    42
  end;
wenzelm@21759
    43
wenzelm@21759
    44
fun dest_char (Syntax.Appl [Syntax.Constant "Char", c1, c2]) = dest_chr c1 c2
wenzelm@21759
    45
  | dest_char _ = raise Match;
wenzelm@21759
    46
wenzelm@29317
    47
fun syntax_string cs =
wenzelm@29317
    48
  Syntax.Appl [Syntax.Constant "_inner_string", Syntax.Variable (Syntax.implode_xstr cs)];
wenzelm@21759
    49
wenzelm@21759
    50
wenzelm@21759
    51
fun char_ast_tr [Syntax.Variable xstr] =
wenzelm@21759
    52
    (case Syntax.explode_xstr xstr of
wenzelm@21759
    53
      [c] => mk_char c
wenzelm@21759
    54
    | _ => error ("Single character expected: " ^ xstr))
wenzelm@21759
    55
  | char_ast_tr asts = raise AST ("char_ast_tr", asts);
wenzelm@21759
    56
wenzelm@29317
    57
fun char_ast_tr' [c1, c2] = Syntax.Appl [Syntax.Constant "_Char", syntax_string [dest_chr c1 c2]]
wenzelm@21759
    58
  | char_ast_tr' _ = raise Match;
wenzelm@21759
    59
wenzelm@21759
    60
wenzelm@21759
    61
(* string *)
wenzelm@21759
    62
wenzelm@21759
    63
fun mk_string [] = Syntax.Constant "Nil"
wenzelm@21759
    64
  | mk_string (c :: cs) = Syntax.Appl [Syntax.Constant "Cons", mk_char c, mk_string cs];
wenzelm@21759
    65
wenzelm@21759
    66
fun string_ast_tr [Syntax.Variable xstr] =
wenzelm@21759
    67
    (case Syntax.explode_xstr xstr of
wenzelm@21759
    68
      [] => Syntax.Appl
wenzelm@21759
    69
        [Syntax.Constant Syntax.constrainC, Syntax.Constant "Nil", Syntax.Constant "string"]
wenzelm@21759
    70
    | cs => mk_string cs)
wenzelm@21759
    71
  | string_ast_tr asts = raise AST ("string_tr", asts);
wenzelm@21759
    72
wenzelm@21759
    73
fun list_ast_tr' [args] = Syntax.Appl [Syntax.Constant "_String",
wenzelm@29317
    74
        syntax_string (map dest_char (Syntax.unfold_ast "_args" args))]
wenzelm@21759
    75
  | list_ast_tr' ts = raise Match;
wenzelm@21759
    76
wenzelm@21759
    77
wenzelm@21759
    78
(* theory setup *)
wenzelm@21759
    79
wenzelm@21759
    80
val setup =
wenzelm@24712
    81
  Sign.add_trfuns
wenzelm@21759
    82
    ([("_Char", char_ast_tr), ("_String", string_ast_tr)], [], [],
wenzelm@21759
    83
      [("Char", char_ast_tr'), ("@list", list_ast_tr')]);
wenzelm@21759
    84
wenzelm@21759
    85
end;