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