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