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