src/Tools/Code/code_printer.ML
author haftmann
Sat Jan 25 23:50:49 2014 +0100 (2014-01-25)
changeset 55147 bce3dbc11f95
parent 55145 2bb3cd36bcf7
child 55148 7e1b7cb54114
permissions -rw-r--r--
prefer explicit code symbol type over ad-hoc name mangling
haftmann@37744
     1
(*  Title:      Tools/Code/code_printer.ML
haftmann@28060
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@28060
     3
haftmann@28060
     4
Generic operations for pretty printing of target language code.
haftmann@28060
     5
*)
haftmann@28060
     6
haftmann@28060
     7
signature CODE_PRINTER =
haftmann@28060
     8
sig
haftmann@32908
     9
  type itype = Code_Thingol.itype
haftmann@32908
    10
  type iterm = Code_Thingol.iterm
haftmann@32908
    11
  type const = Code_Thingol.const
haftmann@32908
    12
  type dict = Code_Thingol.dict
haftmann@32908
    13
haftmann@35228
    14
  val eqn_error: thm option -> string -> 'a
haftmann@28060
    15
haftmann@28060
    16
  val @@ : 'a * 'a -> 'a list
haftmann@28060
    17
  val @| : 'a list * 'a -> 'a list
haftmann@28060
    18
  val str: string -> Pretty.T
haftmann@28060
    19
  val concat: Pretty.T list -> Pretty.T
haftmann@28060
    20
  val brackets: Pretty.T list -> Pretty.T
haftmann@34178
    21
  val enclose: string -> string -> Pretty.T list -> Pretty.T
haftmann@38778
    22
  val commas: Pretty.T list -> Pretty.T list
haftmann@34178
    23
  val enum: string -> string -> string -> Pretty.T list -> Pretty.T
haftmann@34178
    24
  val enum_default: string -> string -> string -> string -> Pretty.T list -> Pretty.T
haftmann@28060
    25
  val semicolon: Pretty.T list -> Pretty.T
haftmann@33989
    26
  val doublesemicolon: Pretty.T list -> Pretty.T
haftmann@34178
    27
  val indent: int -> Pretty.T -> Pretty.T
haftmann@55147
    28
  val markup_stmt: Code_Symbol.symbol -> Pretty.T -> Pretty.T
haftmann@55147
    29
  val format: Code_Symbol.symbol list -> int -> Pretty.T -> string
haftmann@28060
    30
haftmann@30648
    31
  val first_upper: string -> string
haftmann@30648
    32
  val first_lower: string -> string
haftmann@30648
    33
  type var_ctxt
haftmann@30648
    34
  val make_vars: string list -> var_ctxt
haftmann@30648
    35
  val intro_vars: string list -> var_ctxt -> var_ctxt
haftmann@30648
    36
  val lookup_var: var_ctxt -> string -> string
haftmann@32913
    37
  val intro_base_names: (string -> bool) -> (string -> string)
haftmann@32913
    38
    -> string list -> var_ctxt -> var_ctxt
haftmann@55147
    39
  val intro_base_names_for: (string -> bool) -> (Code_Symbol.symbol -> string)
haftmann@55145
    40
    -> iterm list -> var_ctxt -> var_ctxt
haftmann@32908
    41
  val aux_params: var_ctxt -> iterm list list -> string list
haftmann@30648
    42
haftmann@31056
    43
  type literals
haftmann@31056
    44
  val Literals: { literal_char: string -> string, literal_string: string -> string,
haftmann@37958
    45
        literal_numeral: int -> string,
haftmann@31056
    46
        literal_list: Pretty.T list -> Pretty.T, infix_cons: int * string }
haftmann@31056
    47
    -> literals
haftmann@31056
    48
  val literal_char: literals -> string -> string
haftmann@31056
    49
  val literal_string: literals -> string -> string
haftmann@34944
    50
  val literal_numeral: literals -> int -> string
haftmann@31056
    51
  val literal_list: literals -> Pretty.T list -> Pretty.T
haftmann@31056
    52
  val infix_cons: literals -> int * string
haftmann@31056
    53
haftmann@28060
    54
  type lrx
haftmann@28060
    55
  val L: lrx
haftmann@28060
    56
  val R: lrx
haftmann@28060
    57
  val X: lrx
haftmann@28060
    58
  type fixity
haftmann@28060
    59
  val BR: fixity
haftmann@28060
    60
  val NOBR: fixity
haftmann@28060
    61
  val INFX: int * lrx -> fixity
haftmann@28060
    62
  val APP: fixity
haftmann@28060
    63
  val brackify: fixity -> Pretty.T list -> Pretty.T
haftmann@37242
    64
  val brackify_infix: int * lrx -> fixity -> Pretty.T * Pretty.T * Pretty.T -> Pretty.T
haftmann@31665
    65
  val brackify_block: fixity -> Pretty.T -> Pretty.T list -> Pretty.T -> Pretty.T
haftmann@50626
    66
  val gen_applify: bool -> string -> string -> ('a -> Pretty.T) -> fixity -> Pretty.T -> 'a list -> Pretty.T
haftmann@37638
    67
  val applify: string -> string -> ('a -> Pretty.T) -> fixity -> Pretty.T -> 'a list -> Pretty.T
haftmann@38922
    68
  val tuplify: (fixity -> 'a -> Pretty.T) -> fixity -> 'a list -> Pretty.T option
haftmann@38922
    69
haftmann@34152
    70
  type simple_const_syntax
haftmann@37881
    71
  type complex_const_syntax
haftmann@28060
    72
  type const_syntax
haftmann@37881
    73
  type activated_complex_const_syntax
haftmann@37881
    74
  datatype activated_const_syntax = Plain_const_syntax of int * string
haftmann@37881
    75
    | Complex_const_syntax of activated_complex_const_syntax
haftmann@52070
    76
  type tyco_syntax
haftmann@37881
    77
  val requires_args: const_syntax -> int
wenzelm@52079
    78
  val parse_const_syntax: const_syntax parser
wenzelm@52079
    79
  val parse_tyco_syntax: tyco_syntax parser
haftmann@37881
    80
  val plain_const_syntax: string -> const_syntax
haftmann@37876
    81
  val simple_const_syntax: simple_const_syntax -> const_syntax
haftmann@37881
    82
  val complex_const_syntax: complex_const_syntax -> const_syntax
haftmann@31056
    83
  val activate_const_syntax: theory -> literals
haftmann@55147
    84
    -> string -> const_syntax -> activated_const_syntax
haftmann@35228
    85
  val gen_print_app: (thm option -> var_ctxt -> const * iterm list -> Pretty.T list)
haftmann@35228
    86
    -> (thm option -> var_ctxt -> fixity -> iterm -> Pretty.T)
haftmann@37876
    87
    -> (string -> activated_const_syntax option)
haftmann@35228
    88
    -> thm option -> var_ctxt -> fixity -> const * iterm list -> Pretty.T
haftmann@35228
    89
  val gen_print_bind: (thm option -> var_ctxt -> fixity -> iterm -> Pretty.T)
haftmann@35228
    90
    -> thm option -> fixity
haftmann@31889
    91
    -> iterm -> var_ctxt -> Pretty.T * var_ctxt
haftmann@28060
    92
end;
haftmann@28060
    93
haftmann@28060
    94
structure Code_Printer : CODE_PRINTER =
haftmann@28060
    95
struct
haftmann@28060
    96
haftmann@28060
    97
open Code_Thingol;
haftmann@28060
    98
haftmann@39034
    99
(** generic nonsense *)
haftmann@39034
   100
wenzelm@43947
   101
fun eqn_error (SOME thm) s =
wenzelm@43947
   102
      error (s ^ ",\nin equation " ^ Display.string_of_thm_without_context thm)
haftmann@35228
   103
  | eqn_error NONE s = error s;
haftmann@28060
   104
haftmann@39034
   105
val code_presentationN = "code_presentation";
haftmann@39057
   106
val stmt_nameN = "stmt_name";
haftmann@39034
   107
val _ = Markup.add_mode code_presentationN YXML.output_markup;
haftmann@39034
   108
haftmann@39034
   109
haftmann@34071
   110
(** assembling and printing text pieces **)
haftmann@28060
   111
haftmann@28060
   112
infixr 5 @@;
haftmann@28060
   113
infixr 5 @|;
haftmann@28060
   114
fun x @@ y = [x, y];
haftmann@28060
   115
fun xs @| y = xs @ [y];
haftmann@39660
   116
val str = Print_Mode.setmp [] Pretty.str;
haftmann@28060
   117
val concat = Pretty.block o Pretty.breaks;
haftmann@39660
   118
val commas = Print_Mode.setmp [] Pretty.commas;
haftmann@39660
   119
fun enclose l r = Print_Mode.setmp [] (Pretty.enclose l r);
haftmann@34178
   120
val brackets = enclose "(" ")" o Pretty.breaks;
haftmann@39660
   121
fun enum sep l r = Print_Mode.setmp [] (Pretty.enum sep l r);
haftmann@34178
   122
fun enum_default default sep l r [] = str default
haftmann@34178
   123
  | enum_default default sep l r xs = enum sep l r xs;
haftmann@28060
   124
fun semicolon ps = Pretty.block [concat ps, str ";"];
haftmann@33989
   125
fun doublesemicolon ps = Pretty.block [concat ps, str ";;"];
haftmann@39660
   126
fun indent i = Print_Mode.setmp [] (Pretty.indent i);
haftmann@28060
   127
haftmann@55147
   128
fun markup_stmt sym = Print_Mode.setmp [code_presentationN]
haftmann@55147
   129
  (Pretty.mark (code_presentationN, [(stmt_nameN, Code_Symbol.marker sym)]));
haftmann@39062
   130
haftmann@39558
   131
fun filter_presentation [] tree =
haftmann@39558
   132
      Buffer.empty
wenzelm@43947
   133
      |> fold XML.add_content tree
haftmann@55147
   134
  | filter_presentation presentation_syms tree =
haftmann@39057
   135
      let
haftmann@55147
   136
        val presentation_idents = map Code_Symbol.marker presentation_syms
wenzelm@43947
   137
        fun is_selected (name, attrs) =
haftmann@39558
   138
          name = code_presentationN
haftmann@55147
   139
          andalso member (op =) presentation_idents (the (Properties.get attrs stmt_nameN));
haftmann@39558
   140
        fun add_content_with_space tree (is_first, buf) =
haftmann@39558
   141
          buf
haftmann@39558
   142
          |> not is_first ? Buffer.add "\n\n"
haftmann@39558
   143
          |> XML.add_content tree
haftmann@39558
   144
          |> pair false;
haftmann@39558
   145
        fun filter (XML.Elem (name_attrs, xs)) =
haftmann@39558
   146
              fold (if is_selected name_attrs then add_content_with_space else filter) xs
haftmann@47576
   147
          | filter (XML.Text _) = I;
haftmann@39558
   148
      in snd (fold filter tree (true, Buffer.empty)) end;
haftmann@39062
   149
haftmann@39062
   150
fun format presentation_names width =
haftmann@39660
   151
  Print_Mode.setmp [code_presentationN] (Pretty.string_of_margin width)
haftmann@39062
   152
  #> YXML.parse_body
haftmann@39558
   153
  #> filter_presentation presentation_names
haftmann@39678
   154
  #> Buffer.add "\n"
haftmann@39558
   155
  #> Buffer.content;
haftmann@34071
   156
haftmann@28060
   157
haftmann@30648
   158
(** names and variable name contexts **)
haftmann@30648
   159
haftmann@30648
   160
type var_ctxt = string Symtab.table * Name.context;
haftmann@30648
   161
haftmann@30648
   162
fun make_vars names = (fold (fn name => Symtab.update_new (name, name)) names Symtab.empty,
haftmann@30648
   163
  Name.make_context names);
haftmann@30648
   164
haftmann@30648
   165
fun intro_vars names (namemap, namectxt) =
haftmann@30648
   166
  let
wenzelm@43326
   167
    val (names', namectxt') = fold_map Name.variant names namectxt;
haftmann@30648
   168
    val namemap' = fold2 (curry Symtab.update) names names' namemap;
haftmann@30648
   169
  in (namemap', namectxt') end;
haftmann@30648
   170
wenzelm@43947
   171
fun lookup_var (namemap, _) name =
wenzelm@43947
   172
  case Symtab.lookup namemap name of
wenzelm@43947
   173
    SOME name' => name'
haftmann@30648
   174
  | NONE => error ("Invalid name in context: " ^ quote name);
haftmann@30648
   175
wenzelm@40627
   176
val first_upper = implode o nth_map 0 Symbol.to_ascii_upper o raw_explode;
wenzelm@40627
   177
val first_lower = implode o nth_map 0 Symbol.to_ascii_lower o raw_explode;
haftmann@30648
   178
haftmann@32908
   179
fun aux_params vars lhss =
haftmann@32908
   180
  let
haftmann@32908
   181
    fun fish_param _ (w as SOME _) = w
haftmann@32908
   182
      | fish_param (IVar (SOME v)) NONE = SOME v
haftmann@32908
   183
      | fish_param _ NONE = NONE;
haftmann@32908
   184
    fun fillup_param _ (_, SOME v) = v
haftmann@32908
   185
      | fillup_param x (i, NONE) = x ^ string_of_int i;
haftmann@32908
   186
    val fished1 = fold (map2 fish_param) lhss (replicate (length (hd lhss)) NONE);
wenzelm@43324
   187
    val x = singleton (Name.variant_list (map_filter I fished1)) "x";
haftmann@32908
   188
    val fished2 = map_index (fillup_param x) fished1;
wenzelm@43326
   189
    val (fished3, _) = fold_map Name.variant fished2 Name.context;
haftmann@32908
   190
    val vars' = intro_vars fished3 vars;
haftmann@32908
   191
  in map (lookup_var vars') fished3 end;
haftmann@32908
   192
haftmann@55145
   193
fun intro_base_names no_syntax deresolve =
haftmann@55145
   194
  map_filter (fn name => if no_syntax name then
haftmann@32913
   195
      let val name' = deresolve name in
haftmann@32913
   196
        if Long_Name.is_qualified name' then NONE else SOME name'
haftmann@32913
   197
      end else NONE)
haftmann@55145
   198
  #> intro_vars;
haftmann@55145
   199
haftmann@55145
   200
fun intro_base_names_for no_syntax deresolve ts =
haftmann@55145
   201
  []
haftmann@55147
   202
  |> fold Code_Thingol.add_constsyms ts 
haftmann@55147
   203
  |> intro_base_names (fn Code_Symbol.Constant const => no_syntax const | _ => true) deresolve;
haftmann@32913
   204
haftmann@30648
   205
haftmann@31056
   206
(** pretty literals **)
haftmann@31056
   207
haftmann@31056
   208
datatype literals = Literals of {
haftmann@31056
   209
  literal_char: string -> string,
haftmann@31056
   210
  literal_string: string -> string,
haftmann@34944
   211
  literal_numeral: int -> string,
haftmann@31056
   212
  literal_list: Pretty.T list -> Pretty.T,
haftmann@31056
   213
  infix_cons: int * string
haftmann@31056
   214
};
haftmann@31056
   215
haftmann@31056
   216
fun dest_Literals (Literals lits) = lits;
haftmann@31056
   217
haftmann@31056
   218
val literal_char = #literal_char o dest_Literals;
haftmann@31056
   219
val literal_string = #literal_string o dest_Literals;
haftmann@31056
   220
val literal_numeral = #literal_numeral o dest_Literals;
haftmann@31056
   221
val literal_list = #literal_list o dest_Literals;
haftmann@31056
   222
val infix_cons = #infix_cons o dest_Literals;
haftmann@31056
   223
haftmann@31056
   224
haftmann@28060
   225
(** syntax printer **)
haftmann@28060
   226
haftmann@28060
   227
(* binding priorities *)
haftmann@28060
   228
haftmann@28060
   229
datatype lrx = L | R | X;
haftmann@28060
   230
haftmann@28060
   231
datatype fixity =
haftmann@28060
   232
    BR
haftmann@28060
   233
  | NOBR
haftmann@28060
   234
  | INFX of (int * lrx);
haftmann@28060
   235
haftmann@28060
   236
val APP = INFX (~1, L);
haftmann@28060
   237
haftmann@28060
   238
fun fixity_lrx L L = false
haftmann@28060
   239
  | fixity_lrx R R = false
haftmann@28060
   240
  | fixity_lrx _ _ = true;
haftmann@28060
   241
haftmann@28060
   242
fun fixity NOBR _ = false
haftmann@28060
   243
  | fixity _ NOBR = false
haftmann@52433
   244
  | fixity (INFX (pr, lr)) (INFX (pr_ctxt, lr_ctxt)) =
haftmann@52433
   245
      pr < pr_ctxt
haftmann@52433
   246
      orelse pr = pr_ctxt
haftmann@28060
   247
        andalso fixity_lrx lr lr_ctxt
haftmann@52433
   248
      orelse pr_ctxt = ~1
haftmann@28060
   249
  | fixity BR (INFX _) = false
haftmann@28060
   250
  | fixity _ _ = true;
haftmann@28060
   251
haftmann@28060
   252
fun gen_brackify _ [p] = p
haftmann@34178
   253
  | gen_brackify true (ps as _::_) = enclose "(" ")" ps
haftmann@28060
   254
  | gen_brackify false (ps as _::_) = Pretty.block ps;
haftmann@28060
   255
haftmann@28060
   256
fun brackify fxy_ctxt =
haftmann@28060
   257
  gen_brackify (fixity BR fxy_ctxt) o Pretty.breaks;
haftmann@28060
   258
haftmann@37242
   259
fun brackify_infix infx fxy_ctxt (l, m, r) =
haftmann@50627
   260
  gen_brackify (fixity (INFX infx) fxy_ctxt) [l, str " ", m, Pretty.brk 1, r];
haftmann@28060
   261
haftmann@31665
   262
fun brackify_block fxy_ctxt p1 ps p2 =
haftmann@31665
   263
  let val p = Pretty.block_enclose (p1, p2) ps
haftmann@31665
   264
  in if fixity BR fxy_ctxt
haftmann@34178
   265
    then enclose "(" ")" [p]
haftmann@31665
   266
    else p
haftmann@31665
   267
  end;
haftmann@31665
   268
haftmann@50626
   269
fun gen_applify strict opn cls f fxy_ctxt p [] =
haftmann@50626
   270
      if strict
haftmann@50628
   271
      then gen_brackify (fixity BR fxy_ctxt) [p, str (opn ^ cls)]
haftmann@50626
   272
      else p
haftmann@50626
   273
  | gen_applify strict opn cls f fxy_ctxt p ps =
haftmann@50627
   274
      gen_brackify (fixity BR fxy_ctxt) (p @@ enum "," opn cls (map f ps));
haftmann@34247
   275
haftmann@50626
   276
fun applify opn = gen_applify false opn;
haftmann@50626
   277
haftmann@38922
   278
fun tuplify _ _ [] = NONE
haftmann@38922
   279
  | tuplify print fxy [x] = SOME (print fxy x)
haftmann@38922
   280
  | tuplify print _ xs = SOME (enum "," "(" ")" (map (print NOBR) xs));
haftmann@38922
   281
haftmann@28060
   282
haftmann@52070
   283
(* generic syntax *)
haftmann@34152
   284
haftmann@34152
   285
type simple_const_syntax = int * ((fixity -> iterm -> Pretty.T)
haftmann@34152
   286
  -> fixity -> (iterm * itype) list -> Pretty.T);
haftmann@37881
   287
haftmann@37881
   288
type complex_const_syntax = int * (string list * (literals -> string list
haftmann@31056
   289
  -> (var_ctxt -> fixity -> iterm -> Pretty.T)
haftmann@35228
   290
    -> thm option -> var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T));
haftmann@37881
   291
haftmann@37881
   292
datatype const_syntax = plain_const_syntax of string
haftmann@37881
   293
  | complex_const_syntax of complex_const_syntax;
haftmann@37881
   294
haftmann@37881
   295
fun requires_args (plain_const_syntax _) = 0
haftmann@37881
   296
  | requires_args (complex_const_syntax (k, _)) = k;
haftmann@28060
   297
haftmann@34176
   298
fun simple_const_syntax syn =
wenzelm@43947
   299
  complex_const_syntax
wenzelm@43947
   300
    (apsnd (fn f => ([], (fn _ => fn _ => fn print => fn _ => fn vars => f (print vars)))) syn);
haftmann@28060
   301
haftmann@37881
   302
type activated_complex_const_syntax = int * ((var_ctxt -> fixity -> iterm -> Pretty.T)
haftmann@37881
   303
  -> thm option -> var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T)
haftmann@37881
   304
haftmann@37881
   305
datatype activated_const_syntax = Plain_const_syntax of int * string
haftmann@37881
   306
  | Complex_const_syntax of activated_complex_const_syntax;
haftmann@31056
   307
haftmann@55147
   308
fun activate_const_syntax thy literals c (plain_const_syntax s) =
haftmann@55147
   309
      Plain_const_syntax (Code.args_number thy c, s)
haftmann@55147
   310
  | activate_const_syntax thy literals c (complex_const_syntax (n, (cs, f)))=
haftmann@55147
   311
      Complex_const_syntax (n, f literals cs);
haftmann@37881
   312
wenzelm@43947
   313
fun gen_print_app print_app_expr print_term const_syntax some_thm vars fxy
haftmann@55147
   314
    (app as ({ sym, dom, ... }, ts)) =
haftmann@55147
   315
  case sym of
haftmann@55147
   316
    Code_Symbol.Constant const => (case const_syntax const of
haftmann@55147
   317
      NONE => brackify fxy (print_app_expr some_thm vars app)
haftmann@55147
   318
    | SOME (Plain_const_syntax (_, s)) =>
haftmann@55147
   319
        brackify fxy (str s :: map (print_term some_thm vars BR) ts)
haftmann@55147
   320
    | SOME (Complex_const_syntax (k, print)) =>
haftmann@55147
   321
        let
haftmann@55147
   322
          fun print' fxy ts =
haftmann@55147
   323
            print (print_term some_thm) some_thm vars fxy (ts ~~ take k dom);
haftmann@55147
   324
        in
haftmann@55147
   325
          if k = length ts
haftmann@55147
   326
          then print' fxy ts
haftmann@55147
   327
          else if k < length ts
haftmann@55147
   328
          then case chop k ts of (ts1, ts2) =>
haftmann@55147
   329
            brackify fxy (print' APP ts1 :: map (print_term some_thm vars BR) ts2)
haftmann@55147
   330
          else print_term some_thm vars fxy (Code_Thingol.eta_expand k app)
haftmann@55147
   331
        end)
haftmann@55147
   332
  | _ => brackify fxy (print_app_expr some_thm vars app);
haftmann@28060
   333
haftmann@33989
   334
fun gen_print_bind print_term thm (fxy : fixity) pat vars =
haftmann@28060
   335
  let
haftmann@31889
   336
    val vs = Code_Thingol.fold_varnames (insert (op =)) pat [];
haftmann@31874
   337
    val vars' = intro_vars vs vars;
haftmann@33989
   338
  in (print_term thm vars' fxy pat, vars') end;
haftmann@28060
   339
haftmann@52070
   340
type tyco_syntax = int * ((fixity -> itype -> Pretty.T)
haftmann@52070
   341
  -> fixity -> itype list -> Pretty.T);
haftmann@52070
   342
haftmann@28060
   343
haftmann@28060
   344
(* mixfix syntax *)
haftmann@28060
   345
haftmann@28060
   346
datatype 'a mixfix =
haftmann@28060
   347
    Arg of fixity
haftmann@37881
   348
  | String of string
haftmann@37881
   349
  | Break;
haftmann@28060
   350
haftmann@52069
   351
fun printer_of_mixfix prep_arg (fixity_this, mfx) =
haftmann@28060
   352
  let
haftmann@28060
   353
    fun is_arg (Arg _) = true
haftmann@28060
   354
      | is_arg _ = false;
haftmann@28060
   355
    val i = (length o filter is_arg) mfx;
haftmann@28060
   356
    fun fillin _ [] [] =
haftmann@28060
   357
          []
haftmann@33989
   358
      | fillin print (Arg fxy :: mfx) (a :: args) =
haftmann@33989
   359
          (print fxy o prep_arg) a :: fillin print mfx args
haftmann@37881
   360
      | fillin print (String s :: mfx) args =
haftmann@37881
   361
          str s :: fillin print mfx args
haftmann@37881
   362
      | fillin print (Break :: mfx) args =
haftmann@37881
   363
          Pretty.brk 1 :: fillin print mfx args;
haftmann@28060
   364
  in
haftmann@33989
   365
    (i, fn print => fn fixity_ctxt => fn args =>
haftmann@33989
   366
      gen_brackify (fixity fixity_this fixity_ctxt) (fillin print mfx args))
haftmann@28060
   367
  end;
haftmann@28060
   368
haftmann@52069
   369
fun read_infix (fixity_this, i) s =
haftmann@28060
   370
  let
haftmann@52069
   371
    val l = case fixity_this of L => INFX (i, L) | _ => INFX (i, X);
haftmann@52069
   372
    val r = case fixity_this of R => INFX (i, R) | _ => INFX (i, X);
haftmann@28060
   373
  in
haftmann@52069
   374
    (INFX (i, fixity_this), [Arg l, String " ", String s, Break, Arg r])
haftmann@28060
   375
  end;
haftmann@28060
   376
haftmann@52069
   377
fun read_mixfix s =
haftmann@28060
   378
  let
haftmann@28060
   379
    val sym_any = Scan.one Symbol.is_regular;
haftmann@52069
   380
    val parse = Scan.optional ($$ "!" >> K NOBR) BR -- Scan.repeat (
haftmann@28060
   381
         ($$ "(" -- $$ "_" -- $$ ")" >> K (Arg NOBR))
haftmann@28060
   382
      || ($$ "_" >> K (Arg BR))
haftmann@37881
   383
      || ($$ "/" |-- Scan.repeat ($$ " ") >> (K Break))
haftmann@28060
   384
      || (Scan.repeat1
haftmann@28060
   385
           (   $$ "'" |-- sym_any
haftmann@28060
   386
            || Scan.unless ($$ "_" || $$ "/" || $$ "(" |-- $$ "_" |-- $$ ")")
haftmann@37881
   387
                 sym_any) >> (String o implode)));
wenzelm@43947
   388
    fun err s (_, NONE) = (fn () => "malformed mixfix annotation: " ^ quote s)
wenzelm@43947
   389
      | err _ (_, SOME msg) = msg;
wenzelm@43947
   390
  in
wenzelm@43947
   391
    case Scan.finite Symbol.stopper parse (Symbol.explode s) of
haftmann@52069
   392
        (fixity_mixfix, []) => fixity_mixfix
haftmann@52069
   393
      | _ => Scan.!! (err s) Scan.fail ()
haftmann@28060
   394
  end;
haftmann@28060
   395
haftmann@52069
   396
val parse_fixity =
haftmann@52069
   397
  (@{keyword "infix"} >> K X) || (@{keyword "infixl"} >> K L) || (@{keyword "infixr"} >> K R)
haftmann@52069
   398
wenzelm@52099
   399
fun parse_mixfix x =
wenzelm@52099
   400
  (Parse.string >> read_mixfix
haftmann@52069
   401
  || parse_fixity -- Parse.nat -- Parse.string
wenzelm@52099
   402
     >> (fn ((fixity, i), s) => read_infix (fixity, i) s)) x;
haftmann@28060
   403
haftmann@52069
   404
fun syntax_of_mixfix of_plain of_printer prep_arg (BR, [String s]) = of_plain s
haftmann@52069
   405
  | syntax_of_mixfix of_plain of_printer prep_arg (fixity, mfx) =
haftmann@52069
   406
      of_printer (printer_of_mixfix prep_arg (fixity, mfx));
haftmann@37881
   407
wenzelm@52099
   408
fun parse_tyco_syntax x =
wenzelm@52099
   409
  (parse_mixfix >> syntax_of_mixfix (fn s => (0, (K o K o K o str) s)) I I) x;
haftmann@52069
   410
haftmann@52069
   411
val parse_const_syntax =
haftmann@52069
   412
  parse_mixfix >> syntax_of_mixfix plain_const_syntax simple_const_syntax fst;
haftmann@37881
   413
haftmann@28060
   414
end; (*struct*)