| author | haftmann | 
| Thu, 08 Sep 2011 11:31:23 +0200 | |
| changeset 44839 | d19c677eb812 | 
| parent 42290 | b1f544c84040 | 
| child 45490 | 20c8c0cca555 | 
| 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 = | |
| 42290 
b1f544c84040
discontinued special treatment of structure Lexicon;
 wenzelm parents: 
42248diff
changeset | 19 | Ast.Constant o Lexicon.mark_const o | 
| 35123 | 20 | fst o Term.dest_Const o HOLogic.mk_nibble; | 
| 21759 | 21 | |
| 42224 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 22 | fun dest_nib (Ast.Constant s) = | 
| 42290 
b1f544c84040
discontinued special treatment of structure Lexicon;
 wenzelm parents: 
42248diff
changeset | 23 | (case try Lexicon.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 | |
| 42224 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 32 |     Ast.Appl [Ast.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: 
35363diff
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 | ||
| 42224 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 43 | fun dest_char (Ast.Appl [Ast.Constant @{const_syntax Char}, c1, c2]) = dest_chr c1 c2
 | 
| 21759 | 44 | | dest_char _ = raise Match; | 
| 45 | ||
| 29317 | 46 | fun syntax_string cs = | 
| 42290 
b1f544c84040
discontinued special treatment of structure Lexicon;
 wenzelm parents: 
42248diff
changeset | 47 |   Ast.Appl [Ast.Constant @{syntax_const "_inner_string"},
 | 
| 
b1f544c84040
discontinued special treatment of structure Lexicon;
 wenzelm parents: 
42248diff
changeset | 48 | Ast.Variable (Lexicon.implode_xstr cs)]; | 
| 21759 | 49 | |
| 50 | ||
| 42224 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 51 | fun char_ast_tr [Ast.Variable xstr] = | 
| 42290 
b1f544c84040
discontinued special treatment of structure Lexicon;
 wenzelm parents: 
42248diff
changeset | 52 | (case Lexicon.explode_xstr xstr of | 
| 42224 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 53 | [c] => mk_char c | 
| 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 54 |       | _ => error ("Single character expected: " ^ xstr))
 | 
| 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 55 |   | char_ast_tr asts = raise Ast.AST ("char_ast_tr", asts);
 | 
| 21759 | 56 | |
| 35115 | 57 | fun char_ast_tr' [c1, c2] = | 
| 42224 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 58 |       Ast.Appl [Ast.Constant @{syntax_const "_Char"}, syntax_string [dest_chr c1 c2]]
 | 
| 21759 | 59 | | char_ast_tr' _ = raise Match; | 
| 60 | ||
| 61 | ||
| 62 | (* string *) | |
| 63 | ||
| 42224 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 64 | fun mk_string [] = Ast.Constant @{const_syntax Nil}
 | 
| 35115 | 65 | | mk_string (c :: cs) = | 
| 42224 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 66 |       Ast.Appl [Ast.Constant @{const_syntax Cons}, mk_char c, mk_string cs];
 | 
| 21759 | 67 | |
| 42224 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 68 | fun string_ast_tr [Ast.Variable xstr] = | 
| 42290 
b1f544c84040
discontinued special treatment of structure Lexicon;
 wenzelm parents: 
42248diff
changeset | 69 | (case Lexicon.explode_xstr xstr of | 
| 42224 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 70 | [] => | 
| 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 71 | Ast.Appl | 
| 42248 | 72 |             [Ast.Constant @{syntax_const "_constrain"},
 | 
| 42224 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 73 |               Ast.Constant @{const_syntax Nil}, Ast.Constant @{type_syntax string}]
 | 
| 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 74 | | cs => mk_string cs) | 
| 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 75 |   | string_ast_tr asts = raise Ast.AST ("string_tr", asts);
 | 
| 21759 | 76 | |
| 35115 | 77 | fun list_ast_tr' [args] = | 
| 42224 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 78 |       Ast.Appl [Ast.Constant @{syntax_const "_String"},
 | 
| 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
40627diff
changeset | 79 |         syntax_string (map dest_char (Ast.unfold_ast @{syntax_const "_args"} args))]
 | 
| 21759 | 80 | | list_ast_tr' ts = raise Match; | 
| 81 | ||
| 82 | ||
| 83 | (* theory setup *) | |
| 84 | ||
| 85 | val setup = | |
| 24712 
64ed05609568
proper Sign operations instead of Theory aliases;
 wenzelm parents: 
21775diff
changeset | 86 | Sign.add_trfuns | 
| 35115 | 87 |    ([(@{syntax_const "_Char"}, char_ast_tr), (@{syntax_const "_String"}, string_ast_tr)], [], [],
 | 
| 88 |     [(@{const_syntax Char}, char_ast_tr'), (@{syntax_const "_list"}, list_ast_tr')]);
 | |
| 21759 | 89 | |
| 90 | end; |