src/HOL/Tools/string_syntax.ML
author wenzelm
Sun, 01 Mar 2009 23:36:12 +0100
changeset 30190 479806475f3c
parent 29317 9faf1dfb4e7c
child 31048 ac146fc38b51
permissions -rw-r--r--
use long names for old-style fold combinators;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21759
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Tools/string_syntax.ML
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
     3
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
     4
Concrete syntax for hex chars and strings.
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
     5
*)
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
     6
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
     7
signature STRING_SYNTAX =
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
     8
sig
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
     9
  val setup: theory -> theory
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    10
end;
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    11
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    12
structure StringSyntax: STRING_SYNTAX =
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    13
struct
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    14
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    15
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    16
(* nibble *)
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    17
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    18
val mk_nib =
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    19
  Syntax.Constant o unprefix "List.nibble." o
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    20
  fst o Term.dest_Const o HOLogic.mk_nibble;
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    21
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    22
fun dest_nib (Syntax.Constant c) =
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    23
  HOLogic.dest_nibble (Const ("List.nibble." ^ c, dummyT))
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    24
    handle TERM _ => raise Match;
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    25
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    26
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    27
(* char *)
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    28
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    29
fun mk_char s =
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    30
  if Symbol.is_ascii s then
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    31
    Syntax.Appl [Syntax.Constant "Char", mk_nib (ord s div 16), mk_nib (ord s mod 16)]
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    32
  else error ("Non-ASCII symbol: " ^ quote s);
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    33
21775
8be8da44ee56 specials: include single quote;
wenzelm
parents: 21759
diff changeset
    34
val specials = explode "\\\"`'";
21759
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    35
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    36
fun dest_chr c1 c2 =
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    37
  let val c = chr (dest_nib c1 * 16 + dest_nib c2) in
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    38
    if not (member (op =) specials c) andalso Symbol.is_ascii c andalso Symbol.is_printable c
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    39
    then c else raise Match
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    40
  end;
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    41
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    42
fun dest_char (Syntax.Appl [Syntax.Constant "Char", c1, c2]) = dest_chr c1 c2
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    43
  | dest_char _ = raise Match;
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    44
29317
9faf1dfb4e7c renamed token markup "_xstr" to "_inner_string";
wenzelm
parents: 24712
diff changeset
    45
fun syntax_string cs =
9faf1dfb4e7c renamed token markup "_xstr" to "_inner_string";
wenzelm
parents: 24712
diff changeset
    46
  Syntax.Appl [Syntax.Constant "_inner_string", Syntax.Variable (Syntax.implode_xstr cs)];
21759
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    47
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    48
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    49
fun char_ast_tr [Syntax.Variable xstr] =
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    50
    (case Syntax.explode_xstr xstr of
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    51
      [c] => mk_char c
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    52
    | _ => error ("Single character expected: " ^ xstr))
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    53
  | char_ast_tr asts = raise AST ("char_ast_tr", asts);
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    54
29317
9faf1dfb4e7c renamed token markup "_xstr" to "_inner_string";
wenzelm
parents: 24712
diff changeset
    55
fun char_ast_tr' [c1, c2] = Syntax.Appl [Syntax.Constant "_Char", syntax_string [dest_chr c1 c2]]
21759
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    56
  | char_ast_tr' _ = raise Match;
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    57
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    58
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    59
(* string *)
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    60
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    61
fun mk_string [] = Syntax.Constant "Nil"
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    62
  | mk_string (c :: cs) = Syntax.Appl [Syntax.Constant "Cons", mk_char c, mk_string cs];
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    63
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    64
fun string_ast_tr [Syntax.Variable xstr] =
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    65
    (case Syntax.explode_xstr xstr of
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    66
      [] => Syntax.Appl
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    67
        [Syntax.Constant Syntax.constrainC, Syntax.Constant "Nil", Syntax.Constant "string"]
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    68
    | cs => mk_string cs)
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    69
  | string_ast_tr asts = raise AST ("string_tr", asts);
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    70
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    71
fun list_ast_tr' [args] = Syntax.Appl [Syntax.Constant "_String",
29317
9faf1dfb4e7c renamed token markup "_xstr" to "_inner_string";
wenzelm
parents: 24712
diff changeset
    72
        syntax_string (map dest_char (Syntax.unfold_ast "_args" args))]
21759
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    73
  | list_ast_tr' ts = raise Match;
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    74
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    75
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    76
(* theory setup *)
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    77
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    78
val setup =
24712
64ed05609568 proper Sign operations instead of Theory aliases;
wenzelm
parents: 21775
diff changeset
    79
  Sign.add_trfuns
21759
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    80
    ([("_Char", char_ast_tr), ("_String", string_ast_tr)], [], [],
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    81
      [("Char", char_ast_tr'), ("@list", list_ast_tr')]);
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    82
f4b20360751f Concrete syntax for hex chars and strings.
wenzelm
parents:
diff changeset
    83
end;