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