author | wenzelm |
Mon, 15 Feb 2016 14:55:44 +0100 | |
changeset 62337 | d3996d5873dd |
parent 58822 | 90a5e981af3e |
child 62597 | b3f2b8c906a6 |
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 |
||
58822 | 7 |
structure String_Syntax: sig end = |
21759 | 8 |
struct |
9 |
||
10 |
(* nibble *) |
|
11 |
||
12 |
val mk_nib = |
|
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42248
diff
changeset
|
13 |
Ast.Constant o Lexicon.mark_const o |
35123 | 14 |
fst o Term.dest_Const o HOLogic.mk_nibble; |
21759 | 15 |
|
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
40627
diff
changeset
|
16 |
fun dest_nib (Ast.Constant s) = |
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42248
diff
changeset
|
17 |
(case try Lexicon.unmark_const s of |
35256 | 18 |
NONE => raise Match |
19 |
| SOME c => (HOLogic.dest_nibble (Const (c, HOLogic.nibbleT)) handle TERM _ => raise Match)); |
|
21759 | 20 |
|
21 |
||
22 |
(* char *) |
|
23 |
||
24 |
fun mk_char s = |
|
55015
e33c5bd729ff
added \<newline> symbol, which is used for char/string literals in HOL;
wenzelm
parents:
52143
diff
changeset
|
25 |
let |
e33c5bd729ff
added \<newline> symbol, which is used for char/string literals in HOL;
wenzelm
parents:
52143
diff
changeset
|
26 |
val c = |
e33c5bd729ff
added \<newline> symbol, which is used for char/string literals in HOL;
wenzelm
parents:
52143
diff
changeset
|
27 |
if Symbol.is_ascii s then ord s |
e33c5bd729ff
added \<newline> symbol, which is used for char/string literals in HOL;
wenzelm
parents:
52143
diff
changeset
|
28 |
else if s = "\<newline>" then 10 |
e33c5bd729ff
added \<newline> symbol, which is used for char/string literals in HOL;
wenzelm
parents:
52143
diff
changeset
|
29 |
else error ("Bad character: " ^ quote s); |
e33c5bd729ff
added \<newline> symbol, which is used for char/string literals in HOL;
wenzelm
parents:
52143
diff
changeset
|
30 |
in Ast.Appl [Ast.Constant @{const_syntax Char}, mk_nib (c div 16), mk_nib (c mod 16)] end; |
21759 | 31 |
|
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
35363
diff
changeset
|
32 |
val specials = raw_explode "\\\"`'"; |
21759 | 33 |
|
34 |
fun dest_chr c1 c2 = |
|
55108
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55015
diff
changeset
|
35 |
let val s = chr (dest_nib c1 * 16 + dest_nib c2) in |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55015
diff
changeset
|
36 |
if not (member (op =) specials s) andalso Symbol.is_ascii s andalso Symbol.is_printable s |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55015
diff
changeset
|
37 |
then s |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55015
diff
changeset
|
38 |
else if s = "\n" then "\<newline>" |
55015
e33c5bd729ff
added \<newline> symbol, which is used for char/string literals in HOL;
wenzelm
parents:
52143
diff
changeset
|
39 |
else raise Match |
21759 | 40 |
end; |
41 |
||
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
40627
diff
changeset
|
42 |
fun dest_char (Ast.Appl [Ast.Constant @{const_syntax Char}, c1, c2]) = dest_chr c1 c2 |
21759 | 43 |
| dest_char _ = raise Match; |
44 |
||
55108
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55015
diff
changeset
|
45 |
fun syntax_string ss = |
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42248
diff
changeset
|
46 |
Ast.Appl [Ast.Constant @{syntax_const "_inner_string"}, |
55108
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55015
diff
changeset
|
47 |
Ast.Variable (Lexicon.implode_str ss)]; |
21759 | 48 |
|
49 |
||
46483 | 50 |
fun char_ast_tr [Ast.Variable str] = |
55108
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55015
diff
changeset
|
51 |
(case Lexicon.explode_str (str, Position.none) of |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55015
diff
changeset
|
52 |
[(s, _)] => mk_char s |
46483 | 53 |
| _ => error ("Single character expected: " ^ str)) |
45490 | 54 |
| char_ast_tr [Ast.Appl [Ast.Constant @{syntax_const "_constrain"}, ast1, ast2]] = |
55 |
Ast.Appl [Ast.Constant @{syntax_const "_constrain"}, char_ast_tr [ast1], ast2] |
|
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
40627
diff
changeset
|
56 |
| char_ast_tr asts = raise Ast.AST ("char_ast_tr", asts); |
21759 | 57 |
|
35115 | 58 |
fun char_ast_tr' [c1, c2] = |
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
40627
diff
changeset
|
59 |
Ast.Appl [Ast.Constant @{syntax_const "_Char"}, syntax_string [dest_chr c1 c2]] |
21759 | 60 |
| char_ast_tr' _ = raise Match; |
61 |
||
62 |
||
63 |
(* string *) |
|
64 |
||
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
40627
diff
changeset
|
65 |
fun mk_string [] = Ast.Constant @{const_syntax Nil} |
55108
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55015
diff
changeset
|
66 |
| mk_string (s :: ss) = |
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55015
diff
changeset
|
67 |
Ast.Appl [Ast.Constant @{const_syntax Cons}, mk_char s, mk_string ss]; |
21759 | 68 |
|
46483 | 69 |
fun string_ast_tr [Ast.Variable str] = |
55108
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55015
diff
changeset
|
70 |
(case Lexicon.explode_str (str, Position.none) of |
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
40627
diff
changeset
|
71 |
[] => |
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
40627
diff
changeset
|
72 |
Ast.Appl |
42248 | 73 |
[Ast.Constant @{syntax_const "_constrain"}, |
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
40627
diff
changeset
|
74 |
Ast.Constant @{const_syntax Nil}, Ast.Constant @{type_syntax string}] |
55108
0b7a0c1fdf7e
inner syntax token language allows regular quoted strings;
wenzelm
parents:
55015
diff
changeset
|
75 |
| ss => mk_string (map Symbol_Pos.symbol ss)) |
45490 | 76 |
| string_ast_tr [Ast.Appl [Ast.Constant @{syntax_const "_constrain"}, ast1, ast2]] = |
77 |
Ast.Appl [Ast.Constant @{syntax_const "_constrain"}, string_ast_tr [ast1], ast2] |
|
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
40627
diff
changeset
|
78 |
| string_ast_tr asts = raise Ast.AST ("string_tr", asts); |
21759 | 79 |
|
35115 | 80 |
fun list_ast_tr' [args] = |
42224
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
40627
diff
changeset
|
81 |
Ast.Appl [Ast.Constant @{syntax_const "_String"}, |
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
wenzelm
parents:
40627
diff
changeset
|
82 |
syntax_string (map dest_char (Ast.unfold_ast @{syntax_const "_args"} args))] |
51160
599ff65b85e2
systematic conversions between nat and nibble/char;
haftmann
parents:
46483
diff
changeset
|
83 |
| list_ast_tr' _ = raise Match; |
21759 | 84 |
|
85 |
||
86 |
(* theory setup *) |
|
87 |
||
58822 | 88 |
val _ = |
89 |
Theory.setup |
|
90 |
(Sign.parse_ast_translation |
|
91 |
[(@{syntax_const "_Char"}, K char_ast_tr), |
|
92 |
(@{syntax_const "_String"}, K string_ast_tr)] #> |
|
93 |
Sign.print_ast_translation |
|
94 |
[(@{const_syntax Char}, K char_ast_tr'), |
|
95 |
(@{syntax_const "_list"}, K list_ast_tr')]); |
|
21759 | 96 |
|
97 |
end; |