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