improved string syntax (allow translation rules);
authorwenzelm
Tue Jan 16 00:30:06 2001 +0100 (2001-01-16)
changeset 109092bbb1797bbe2
parent 10908 a7cfffb5d7dc
child 10910 058775a575db
improved string syntax (allow translation rules);
src/HOL/String.thy
     1.1 --- a/src/HOL/String.thy	Tue Jan 16 00:29:43 2001 +0100
     1.2 +++ b/src/HOL/String.thy	Tue Jan 16 00:30:06 2001 +0100
     1.3 @@ -6,20 +6,75 @@
     1.4  Hex chars and strings.
     1.5  *)
     1.6  
     1.7 -theory String = List
     1.8 -files "Tools/string_syntax.ML":
     1.9 +theory String = List:
    1.10  
    1.11  datatype nibble =
    1.12 -    H00 | H01 | H02 | H03 | H04 | H05 | H06 | H07
    1.13 -  | H08 | H09 | H0A | H0B | H0C | H0D | H0E | H0F
    1.14 +    Nibble0 | Nibble1 | Nibble2 | Nibble3 | Nibble4 | Nibble5 | Nibble6 | Nibble7
    1.15 +  | Nibble8 | Nibble9 | NibbleA | NibbleB | NibbleC | NibbleD | NibbleE | NibbleF
    1.16  
    1.17  datatype char = Char nibble nibble
    1.18 +  -- "Note: canonical order of character encoding coincides with standard term ordering"
    1.19 +
    1.20  types string = "char list"
    1.21  
    1.22  syntax
    1.23    "_Char" :: "xstr => char"    ("CHR _")
    1.24    "_String" :: "xstr => string"    ("_")
    1.25  
    1.26 -setup StringSyntax.setup
    1.27 +parse_ast_translation {*
    1.28 +  let
    1.29 +    val constants = Syntax.Appl o map Syntax.Constant;
    1.30 +
    1.31 +    fun mk_nib n = "Nibble" ^ chr (n + (if n <= 9 then ord "0" else ord "A" - 10));
    1.32 +    fun mk_char c =
    1.33 +      if Symbol.is_ascii c andalso Symbol.is_printable c then
    1.34 +        constants ["Char", mk_nib (ord c div 16), mk_nib (ord c mod 16)]
    1.35 +      else error ("Printable ASCII character expected: " ^ quote c);
    1.36 +
    1.37 +    fun mk_string [] = Syntax.Constant "Nil"
    1.38 +      | mk_string (c :: cs) = Syntax.Appl [Syntax.Constant "Cons", mk_char c, mk_string cs];
    1.39 +
    1.40 +    fun char_ast_tr [Syntax.Variable xstr] =
    1.41 +        (case Syntax.explode_xstr xstr of
    1.42 +          [c] => mk_char c
    1.43 +        | _ => error ("Single character expected: " ^ xstr))
    1.44 +      | char_ast_tr asts = raise AST ("char_ast_tr", asts);
    1.45 +
    1.46 +    fun string_ast_tr [Syntax.Variable xstr] =
    1.47 +        (case Syntax.explode_xstr xstr of
    1.48 +          [] => constants [Syntax.constrainC, "Nil", "string"]
    1.49 +        | cs => mk_string cs)
    1.50 +      | string_ast_tr asts = raise AST ("string_tr", asts);
    1.51 +  in [("_Char", char_ast_tr), ("_String", string_ast_tr)] end;
    1.52 +*}
    1.53 +
    1.54 +print_ast_translation {*
    1.55 +  let
    1.56 +    fun dest_nib (Syntax.Constant c) =
    1.57 +        (case explode c of
    1.58 +          ["N", "i", "b", "b", "l", "e", h] =>
    1.59 +            if "0" <= h andalso h <= "9" then ord h - ord "0"
    1.60 +            else if "A" <= h andalso h <= "F" then ord h - ord "A" + 10
    1.61 +            else raise Match
    1.62 +        | _ => raise Match)
    1.63 +      | dest_nib _ = raise Match;
    1.64 +
    1.65 +    fun dest_chr c1 c2 =
    1.66 +      let val c = chr (dest_nib c1 * 16 + dest_nib c2)
    1.67 +      in if Symbol.is_printable c then c else raise Match end;
    1.68 +
    1.69 +    fun dest_char (Syntax.Appl [Syntax.Constant "Char", c1, c2]) = dest_chr c1 c2
    1.70 +      | dest_char _ = raise Match;
    1.71 +
    1.72 +    fun xstr cs = Syntax.Appl [Syntax.Constant "_xstr", Syntax.Variable (Syntax.implode_xstr cs)];
    1.73 +
    1.74 +    fun char_ast_tr' [c1, c2] = Syntax.Appl [Syntax.Constant "_Char", xstr [dest_chr c1 c2]]
    1.75 +      | char_ast_tr' _ = raise Match;
    1.76 +
    1.77 +    fun list_ast_tr' [args] = Syntax.Appl [Syntax.Constant "_String",
    1.78 +            xstr (map dest_char (Syntax.unfold_ast "_args" args))]
    1.79 +      | list_ast_tr' ts = raise Match;
    1.80 +  in [("Char", char_ast_tr'), ("@list", list_ast_tr')] end;
    1.81 +*}
    1.82  
    1.83  end