src/Tools/code/code_printer.ML
author haftmann
Fri, 24 Oct 2008 17:51:36 +0200
changeset 28690 fc51fa5efea1
parent 28663 bd8438543bf2
child 28708 a1a436f09ec6
permissions -rw-r--r--
simplified user-defined class syntax
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
     1
(*  Title:      Tools/code/code_printer.ML
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
     2
    ID:         $Id$
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
     4
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
     5
Generic operations for pretty printing of target language code.
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
     6
*)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
     7
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
     8
signature CODE_PRINTER =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
     9
sig
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    10
  val nerror: thm -> string -> 'a
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    11
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    12
  val @@ : 'a * 'a -> 'a list
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    13
  val @| : 'a list * 'a -> 'a list
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    14
  val str: string -> Pretty.T
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    15
  val concat: Pretty.T list -> Pretty.T
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    16
  val brackets: Pretty.T list -> Pretty.T
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    17
  val semicolon: Pretty.T list -> Pretty.T
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    18
  val enum_default: string -> string -> string -> string -> Pretty.T list -> Pretty.T
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    19
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    20
  type lrx
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    21
  val L: lrx
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    22
  val R: lrx
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    23
  val X: lrx
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    24
  type fixity
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    25
  val BR: fixity
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    26
  val NOBR: fixity
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    27
  val INFX: int * lrx -> fixity
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    28
  val APP: fixity
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    29
  val brackify: fixity -> Pretty.T list -> Pretty.T
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    30
  val brackify_infix: int * lrx -> fixity -> Pretty.T list -> Pretty.T
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    31
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    32
  type itype = Code_Thingol.itype
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    33
  type iterm = Code_Thingol.iterm
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    34
  type const = Code_Thingol.const
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    35
  type dict = Code_Thingol.dict
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    36
  type tyco_syntax
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    37
  type const_syntax
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    38
  val parse_infix: ('a -> 'b) -> lrx * int -> string
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    39
    -> int * ((fixity -> 'b -> Pretty.T)
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    40
    -> fixity -> 'a list -> Pretty.T)
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    41
  val parse_syntax: ('a -> 'b) -> OuterParse.token list
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    42
    -> (int * ((fixity -> 'b -> Pretty.T)
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    43
    -> fixity -> 'a list -> Pretty.T)) option * OuterParse.token list
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    44
  val simple_const_syntax: (int * ((fixity -> iterm -> Pretty.T)
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    45
    -> fixity -> (iterm * itype) list -> Pretty.T)) option -> const_syntax option
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    46
  val gen_pr_app: (thm -> bool -> Code_Name.var_ctxt -> const * iterm list -> Pretty.T list)
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    47
    -> (thm -> bool -> Code_Name.var_ctxt -> fixity -> iterm -> Pretty.T)
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    48
    -> (string -> const_syntax option) -> (string -> bool) -> Code_Thingol.naming
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    49
    -> thm -> bool -> Code_Name.var_ctxt -> fixity -> const * iterm list -> Pretty.T
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    50
  val gen_pr_bind: ((string option * Pretty.T option) * itype -> Pretty.T)
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    51
    -> (thm -> bool -> Code_Name.var_ctxt -> fixity -> iterm -> Pretty.T)
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    52
    -> thm -> fixity
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    53
    -> (string option * iterm option) * itype -> Code_Name.var_ctxt -> Pretty.T * Code_Name.var_ctxt
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    54
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
    55
  type literals
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
    56
  val Literals: { literal_char: string -> string, literal_string: string -> string,
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
    57
        literal_numeral: bool -> int -> string, literal_list: Pretty.T list -> Pretty.T, infix_cons: int * string }
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
    58
    -> literals
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
    59
  val literal_char: literals -> string -> string
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
    60
  val literal_string: literals -> string -> string
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
    61
  val literal_numeral: literals -> bool -> int -> string
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
    62
  val literal_list: literals -> Pretty.T list -> Pretty.T
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
    63
  val infix_cons: literals -> int * string
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    64
end;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    65
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    66
structure Code_Printer : CODE_PRINTER =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    67
struct
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    68
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    69
open Code_Thingol;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    70
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    71
fun nerror thm s = error (s ^ ",\nin equation " ^ Display.string_of_thm thm);
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    72
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    73
(** assembling text pieces **)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    74
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    75
infixr 5 @@;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    76
infixr 5 @|;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    77
fun x @@ y = [x, y];
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    78
fun xs @| y = xs @ [y];
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    79
val str = PrintMode.setmp [] Pretty.str;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    80
val concat = Pretty.block o Pretty.breaks;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    81
val brackets = Pretty.enclose "(" ")" o Pretty.breaks;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    82
fun semicolon ps = Pretty.block [concat ps, str ";"];
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    83
fun enum_default default sep opn cls [] = str default
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    84
  | enum_default default sep opn cls xs = Pretty.enum sep opn cls xs;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    85
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    86
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    87
(** syntax printer **)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    88
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    89
(* binding priorities *)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    90
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    91
datatype lrx = L | R | X;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    92
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    93
datatype fixity =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    94
    BR
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    95
  | NOBR
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    96
  | INFX of (int * lrx);
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    97
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    98
val APP = INFX (~1, L);
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    99
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   100
fun fixity_lrx L L = false
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   101
  | fixity_lrx R R = false
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   102
  | fixity_lrx _ _ = true;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   103
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   104
fun fixity NOBR _ = false
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   105
  | fixity _ NOBR = false
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   106
  | fixity (INFX (pr, lr)) (INFX (pr_ctxt, lr_ctxt)) =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   107
      pr < pr_ctxt
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   108
      orelse pr = pr_ctxt
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   109
        andalso fixity_lrx lr lr_ctxt
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   110
      orelse pr_ctxt = ~1
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   111
  | fixity BR (INFX _) = false
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   112
  | fixity _ _ = true;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   113
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   114
fun gen_brackify _ [p] = p
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   115
  | gen_brackify true (ps as _::_) = Pretty.enclose "(" ")" ps
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   116
  | gen_brackify false (ps as _::_) = Pretty.block ps;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   117
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   118
fun brackify fxy_ctxt =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   119
  gen_brackify (fixity BR fxy_ctxt) o Pretty.breaks;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   120
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   121
fun brackify_infix infx fxy_ctxt =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   122
  gen_brackify (fixity (INFX infx) fxy_ctxt) o Pretty.breaks;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   123
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   124
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   125
(* generic syntax *)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   126
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   127
type tyco_syntax = int * ((fixity -> itype -> Pretty.T)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   128
  -> fixity -> itype list -> Pretty.T);
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
   129
type const_syntax = int * ((Code_Name.var_ctxt -> fixity -> iterm -> Pretty.T)
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
   130
  -> Code_Thingol.naming -> thm -> bool -> Code_Name.var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T);
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   131
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   132
fun simple_const_syntax x = (Option.map o apsnd)
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
   133
  (fn pretty => fn pr => fn naming => fn thm => fn pat => fn vars => pretty (pr vars)) x;
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   134
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
   135
fun gen_pr_app pr_app pr_term syntax_const is_cons naming thm pat
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   136
    vars fxy (app as ((c, (_, tys)), ts)) =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   137
  case syntax_const c
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   138
   of NONE => if pat andalso not (is_cons c) then
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   139
        nerror thm "Non-constructor in pattern"
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   140
        else brackify fxy (pr_app thm pat vars app)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   141
    | SOME (k, pr) =>
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   142
        let
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
   143
          fun pr' fxy ts = pr (pr_term thm pat) naming thm pat vars fxy (ts ~~ curry Library.take k tys);
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   144
        in if k = length ts
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   145
          then pr' fxy ts
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   146
        else if k < length ts
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   147
          then case chop k ts of (ts1, ts2) =>
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   148
            brackify fxy (pr' APP ts1 :: map (pr_term thm pat vars BR) ts2)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   149
          else pr_term thm pat vars fxy (Code_Thingol.eta_expand k app)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   150
        end;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   151
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   152
fun gen_pr_bind pr_bind pr_term thm (fxy : fixity) ((v, pat), ty : itype) vars =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   153
  let
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   154
    val vs = case pat
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   155
     of SOME pat => Code_Thingol.fold_varnames (insert (op =)) pat []
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   156
      | NONE => [];
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
   157
    val vars' = Code_Name.intro_vars (the_list v) vars;
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
   158
    val vars'' = Code_Name.intro_vars vs vars';
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
   159
    val v' = Option.map (Code_Name.lookup_var vars') v;
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   160
    val pat' = Option.map (pr_term thm true vars'' fxy) pat;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   161
  in (pr_bind ((v', pat'), ty), vars'') end;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   162
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   163
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   164
(* mixfix syntax *)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   165
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   166
datatype 'a mixfix =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   167
    Arg of fixity
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   168
  | Pretty of Pretty.T;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   169
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   170
fun mk_mixfix prep_arg (fixity_this, mfx) =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   171
  let
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   172
    fun is_arg (Arg _) = true
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   173
      | is_arg _ = false;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   174
    val i = (length o filter is_arg) mfx;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   175
    fun fillin _ [] [] =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   176
          []
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   177
      | fillin pr (Arg fxy :: mfx) (a :: args) =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   178
          (pr fxy o prep_arg) a :: fillin pr mfx args
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   179
      | fillin pr (Pretty p :: mfx) args =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   180
          p :: fillin pr mfx args;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   181
  in
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   182
    (i, fn pr => fn fixity_ctxt => fn args =>
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   183
      gen_brackify (fixity fixity_this fixity_ctxt) (fillin pr mfx args))
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   184
  end;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   185
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   186
fun parse_infix prep_arg (x, i) s =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   187
  let
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   188
    val l = case x of L => INFX (i, L) | _ => INFX (i, X);
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   189
    val r = case x of R => INFX (i, R) | _ => INFX (i, X);
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   190
  in
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   191
    mk_mixfix prep_arg (INFX (i, x),
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   192
      [Arg l, (Pretty o Pretty.brk) 1, (Pretty o str) s, (Pretty o Pretty.brk) 1, Arg r])
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   193
  end;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   194
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   195
fun parse_mixfix prep_arg s =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   196
  let
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   197
    val sym_any = Scan.one Symbol.is_regular;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   198
    val parse = Scan.optional ($$ "!" >> K true) false -- Scan.repeat (
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   199
         ($$ "(" -- $$ "_" -- $$ ")" >> K (Arg NOBR))
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   200
      || ($$ "_" >> K (Arg BR))
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   201
      || ($$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length))
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   202
      || (Scan.repeat1
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   203
           (   $$ "'" |-- sym_any
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   204
            || Scan.unless ($$ "_" || $$ "/" || $$ "(" |-- $$ "_" |-- $$ ")")
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   205
                 sym_any) >> (Pretty o str o implode)));
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   206
  in case Scan.finite Symbol.stopper parse (Symbol.explode s)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   207
   of ((_, p as [_]), []) => mk_mixfix prep_arg (NOBR, p)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   208
    | ((b, p as _ :: _ :: _), []) => mk_mixfix prep_arg (if b then NOBR else BR, p)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   209
    | _ => Scan.!!
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   210
        (the_default ("malformed mixfix annotation: " ^ quote s) o snd) Scan.fail ()
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   211
  end;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   212
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   213
val (infixK, infixlK, infixrK) = ("infix", "infixl", "infixr");
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   214
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   215
fun parse_syntax prep_arg xs =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   216
  Scan.option ((
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   217
      ((OuterParse.$$$ infixK  >> K X)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   218
        || (OuterParse.$$$ infixlK >> K L)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   219
        || (OuterParse.$$$ infixrK >> K R))
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   220
        -- OuterParse.nat >> parse_infix prep_arg
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   221
      || Scan.succeed (parse_mixfix prep_arg))
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   222
      -- OuterParse.string
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   223
      >> (fn (parse, s) => parse s)) xs;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   224
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   225
val _ = List.app OuterKeyword.keyword [infixK, infixlK, infixrK];
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   226
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   227
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   228
(** pretty literals **)
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   229
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   230
datatype literals = Literals of {
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   231
  literal_char: string -> string,
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   232
  literal_string: string -> string,
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   233
  literal_numeral: bool -> int -> string,
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   234
  literal_list: Pretty.T list -> Pretty.T,
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   235
  infix_cons: int * string
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   236
};
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   237
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   238
fun dest_Literals (Literals lits) = lits;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   239
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   240
val literal_char = #literal_char o dest_Literals;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   241
val literal_string = #literal_string o dest_Literals;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   242
val literal_numeral = #literal_numeral o dest_Literals;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   243
val literal_list = #literal_list o dest_Literals;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   244
val infix_cons = #infix_cons o dest_Literals;
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   245
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   246
end; (*struct*)