|
21477
|
1 |
(* Title: Pure/General/ml_syntax.ML
|
|
|
2 |
ID: $Id$
|
|
|
3 |
Author: Makarius
|
|
|
4 |
|
|
|
5 |
Basic ML syntax operations.
|
|
|
6 |
*)
|
|
|
7 |
|
|
|
8 |
signature ML_SYNTAX =
|
|
|
9 |
sig
|
|
|
10 |
val reserved: string list
|
|
|
11 |
val is_reserved: string -> bool
|
|
|
12 |
val is_identifier: string -> bool
|
|
|
13 |
val str_of_pair: ('a -> string) -> ('b -> string) -> 'a * 'b -> string
|
|
|
14 |
val str_of_list: ('a -> string) -> 'a list -> string
|
|
|
15 |
val str_of_option: ('a -> string) -> 'a option -> string
|
|
|
16 |
val str_of_char: string -> string
|
|
|
17 |
end;
|
|
|
18 |
|
|
|
19 |
structure ML_Syntax: ML_SYNTAX =
|
|
|
20 |
struct
|
|
|
21 |
|
|
|
22 |
(* reserved words *)
|
|
|
23 |
|
|
|
24 |
val reserved =
|
|
|
25 |
["abstype", "and", "andalso", "as", "case", "do", "datatype", "else",
|
|
|
26 |
"end", "exception", "fn", "fun", "handle", "if", "in", "infix",
|
|
|
27 |
"infixr", "let", "local", "nonfix", "of", "op", "open", "orelse",
|
|
|
28 |
"raise", "rec", "then", "type", "val", "with", "withtype", "while",
|
|
|
29 |
"eqtype", "functor", "include", "sharing", "sig", "signature",
|
|
|
30 |
"struct", "structure", "where"];
|
|
|
31 |
|
|
|
32 |
val is_reserved = member (op =) reserved;
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
(* identifiers *)
|
|
|
36 |
|
|
|
37 |
fun is_identifier name =
|
|
|
38 |
not (is_reserved name) andalso Syntax.is_ascii_identifier name;
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
(* unformatted output *)
|
|
|
42 |
|
|
|
43 |
fun str_of_pair f1 f2 (x, y) = "(" ^ f1 x ^ ", " ^ f2 y ^ ")";
|
|
|
44 |
|
|
|
45 |
fun str_of_list f = enclose "[" "]" o commas o map f;
|
|
|
46 |
|
|
|
47 |
fun str_of_option f NONE = "NONE"
|
|
|
48 |
| str_of_option f (SOME x) = "SOME (" ^ f x ^ ")";
|
|
|
49 |
|
|
21494
|
50 |
fun str_of_char s =
|
|
|
51 |
if not (Symbol.is_char s) then raise Fail ("Bad character: " ^ quote s)
|
|
|
52 |
else if s = "\"" then "\\\""
|
|
|
53 |
else if s = "\\" then "\\\\"
|
|
|
54 |
else
|
|
|
55 |
let val c = ord s in
|
|
|
56 |
if c < 32 then "\\^" ^ chr (c + ord "@")
|
|
|
57 |
else if c < 127 then s
|
|
|
58 |
else "\\" ^ string_of_int c
|
|
|
59 |
end;
|
|
21477
|
60 |
|
|
|
61 |
val str_of_string = quote o translate_string str_of_char;
|
|
|
62 |
|
|
|
63 |
end;
|