src/Pure/ML/ml_syntax.ML
author wenzelm
Thu Oct 04 20:29:42 2007 +0200 (2007-10-04)
changeset 24850 0cfd722ab579
parent 24582 57599da58045
child 26362 d9ce159a41d1
permissions -rw-r--r--
Name.uu, Name.aT;
wenzelm@24582
     1
(*  Title:      Pure/ML/ml_syntax.ML
wenzelm@24574
     2
    ID:         $Id$
wenzelm@24574
     3
    Author:     Makarius
wenzelm@24574
     4
wenzelm@24574
     5
Basic ML syntax operations.
wenzelm@24574
     6
*)
wenzelm@24574
     7
wenzelm@24574
     8
signature ML_SYNTAX =
wenzelm@24574
     9
sig
wenzelm@24574
    10
  val reserved_names: string list
wenzelm@24574
    11
  val reserved: Name.context
wenzelm@24574
    12
  val is_reserved: string -> bool
wenzelm@24574
    13
  val is_identifier: string -> bool
wenzelm@24574
    14
  val atomic: string -> string
wenzelm@24574
    15
  val print_int: int -> string
wenzelm@24574
    16
  val print_pair: ('a -> string) -> ('b -> string) -> 'a * 'b -> string
wenzelm@24574
    17
  val print_list: ('a -> string) -> 'a list -> string
wenzelm@24574
    18
  val print_option: ('a -> string) -> 'a option -> string
wenzelm@24574
    19
  val print_char: string -> string
wenzelm@24574
    20
  val print_string: string -> string
wenzelm@24574
    21
  val print_strings: string list -> string
wenzelm@24574
    22
  val print_indexname: indexname -> string
wenzelm@24574
    23
  val print_class: class -> string
wenzelm@24574
    24
  val print_sort: sort -> string
wenzelm@24574
    25
  val print_typ: typ -> string
wenzelm@24574
    26
  val print_term: term -> string
wenzelm@24574
    27
end;
wenzelm@24574
    28
wenzelm@24574
    29
structure ML_Syntax: ML_SYNTAX =
wenzelm@24574
    30
struct
wenzelm@24574
    31
wenzelm@24574
    32
(* reserved words *)
wenzelm@24574
    33
wenzelm@24582
    34
val reserved_names = filter Syntax.is_ascii_identifier ML_Lex.keywords;
wenzelm@24574
    35
val reserved = Name.make_context reserved_names;
wenzelm@24574
    36
val is_reserved = Name.is_declared reserved;
wenzelm@24574
    37
wenzelm@24574
    38
wenzelm@24574
    39
(* identifiers *)
wenzelm@24574
    40
wenzelm@24574
    41
fun is_identifier name =
wenzelm@24574
    42
  not (is_reserved name) andalso Syntax.is_ascii_identifier name;
wenzelm@24574
    43
wenzelm@24574
    44
wenzelm@24574
    45
(* literal output -- unformatted *)
wenzelm@24574
    46
wenzelm@24574
    47
val atomic = enclose "(" ")";
wenzelm@24574
    48
wenzelm@24574
    49
val print_int = Int.toString;
wenzelm@24574
    50
wenzelm@24574
    51
fun print_pair f1 f2 (x, y) = "(" ^ f1 x ^ ", " ^ f2 y ^ ")";
wenzelm@24574
    52
wenzelm@24574
    53
fun print_list f = enclose "[" "]" o commas o map f;
wenzelm@24574
    54
wenzelm@24574
    55
fun print_option f NONE = "NONE"
wenzelm@24574
    56
  | print_option f (SOME x) = "SOME (" ^ f x ^ ")";
wenzelm@24574
    57
wenzelm@24574
    58
fun print_char s =
wenzelm@24574
    59
  if not (Symbol.is_char s) then raise Fail ("Bad character: " ^ quote s)
wenzelm@24574
    60
  else if s = "\"" then "\\\""
wenzelm@24574
    61
  else if s = "\\" then "\\\\"
wenzelm@24574
    62
  else
wenzelm@24574
    63
    let val c = ord s in
wenzelm@24574
    64
      if c < 32 then "\\^" ^ chr (c + ord "@")
wenzelm@24574
    65
      else if c < 127 then s
wenzelm@24574
    66
      else "\\" ^ string_of_int c
wenzelm@24574
    67
    end;
wenzelm@24574
    68
wenzelm@24574
    69
val print_string = quote o translate_string print_char;
wenzelm@24574
    70
val print_strings = print_list print_string;
wenzelm@24574
    71
wenzelm@24574
    72
val print_indexname = print_pair print_string print_int;
wenzelm@24574
    73
wenzelm@24574
    74
val print_class = print_string;
wenzelm@24574
    75
val print_sort = print_list print_class;
wenzelm@24574
    76
wenzelm@24574
    77
fun print_typ (Type arg) = "Type " ^ print_pair print_string (print_list print_typ) arg
wenzelm@24574
    78
  | print_typ (TFree arg) = "TFree " ^ print_pair print_string print_sort arg
wenzelm@24574
    79
  | print_typ (TVar arg) = "TVar " ^ print_pair print_indexname print_sort arg;
wenzelm@24574
    80
wenzelm@24574
    81
fun print_term (Const arg) = "Const " ^ print_pair print_string print_typ arg
wenzelm@24574
    82
  | print_term (Free arg) = "Free " ^ print_pair print_string print_typ arg
wenzelm@24574
    83
  | print_term (Var arg) = "Var " ^ print_pair print_indexname print_typ arg
wenzelm@24574
    84
  | print_term (Bound i) = "Bound " ^ print_int i
wenzelm@24574
    85
  | print_term (Abs (s, T, t)) =
wenzelm@24574
    86
      "Abs (" ^ print_string s ^ ", " ^ print_typ T ^ ", " ^ print_term t ^ ")"
wenzelm@24574
    87
  | print_term (t1 $ t2) = atomic (print_term t1) ^ " $ " ^ atomic (print_term t2);
wenzelm@24574
    88
wenzelm@24574
    89
end;