src/Pure/Syntax/syntax.ML
author wenzelm
Sat Apr 16 18:58:55 2005 +0200 (2005-04-16)
changeset 15755 50ac97ebe7d8
parent 15574 b1d1b5bfc464
child 15759 144c9f9a8ade
permissions -rw-r--r--
expect translations functions to be stamped already;
added remove_const_gram;
wenzelm@18
     1
(*  Title:      Pure/Syntax/syntax.ML
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
wenzelm@18
     4
wenzelm@18
     5
Root of Isabelle's syntax module.
clasohm@0
     6
*)
clasohm@0
     7
wenzelm@260
     8
signature BASIC_SYNTAX =
wenzelm@2383
     9
sig
wenzelm@260
    10
  include AST0
wenzelm@556
    11
  include SYN_TRANS0
wenzelm@556
    12
  include MIXFIX0
wenzelm@260
    13
  include PRINTER0
wenzelm@2383
    14
end;
wenzelm@260
    15
clasohm@0
    16
signature SYNTAX =
wenzelm@2383
    17
sig
wenzelm@6322
    18
  include TOKEN_TRANS0
wenzelm@260
    19
  include AST1
clasohm@0
    20
  include LEXICON0
wenzelm@237
    21
  include SYN_EXT0
clasohm@0
    22
  include TYPE_EXT0
wenzelm@556
    23
  include SYN_TRANS1
wenzelm@556
    24
  include MIXFIX1
clasohm@0
    25
  include PRINTER0
wenzelm@15755
    26
  val extend_trtab: string -> (string * ('a * stamp)) list ->
wenzelm@15755
    27
    ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table
wenzelm@15755
    28
  val merge_trtabs: string -> ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table ->
wenzelm@15755
    29
    ('a * stamp) Symtab.table
wenzelm@14648
    30
  val merge_tr'tabs: ('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table
wenzelm@14648
    31
    -> ('a * stamp) list Symtab.table
wenzelm@15755
    32
  val extend_tr'tab: (string * ('a * stamp)) list ->
wenzelm@15755
    33
    ('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table
wenzelm@1158
    34
  datatype 'a trrule =
wenzelm@3526
    35
    ParseRule of 'a * 'a |
wenzelm@3526
    36
    PrintRule of 'a * 'a |
wenzelm@3526
    37
    ParsePrintRule of 'a * 'a
wenzelm@556
    38
  type syntax
wenzelm@14687
    39
  val is_keyword: syntax -> string -> bool
wenzelm@15755
    40
  val default_mode: string * bool
wenzelm@12073
    41
  val extend_type_gram: (string * int * mixfix) list -> syntax -> syntax
wenzelm@14904
    42
  val extend_const_gram: (string -> bool) ->
wenzelm@14904
    43
    string * bool -> (string * typ * mixfix) list -> syntax -> syntax
wenzelm@12073
    44
  val extend_consts: string list -> syntax -> syntax
wenzelm@12073
    45
  val extend_trfuns:
wenzelm@15755
    46
    (string * ((ast list -> ast) * stamp)) list *
wenzelm@15755
    47
    (string * ((term list -> term) * stamp)) list *
wenzelm@15755
    48
    (string * ((bool -> typ -> term list -> term) * stamp)) list *
wenzelm@15755
    49
    (string * ((ast list -> ast) * stamp)) list -> syntax -> syntax
wenzelm@15755
    50
  val extend_tokentrfuns: (string * string * (string -> string * real)) list -> syntax -> syntax
wenzelm@12073
    51
  val extend_trrules_i: ast trrule list -> syntax -> syntax
wenzelm@14904
    52
  val extend_trrules: (string -> bool) -> syntax ->
wenzelm@14904
    53
    (string * string) trrule list -> syntax -> syntax
wenzelm@15755
    54
  val remove_const_gram: (string -> bool) ->
wenzelm@15755
    55
    string * bool -> (string * typ * mixfix) list -> syntax -> syntax
wenzelm@4618
    56
  val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
wenzelm@556
    57
  val merge_syntaxes: syntax -> syntax -> syntax
wenzelm@556
    58
  val type_syn: syntax
clasohm@764
    59
  val pure_syn: syntax
wenzelm@556
    60
  val print_gram: syntax -> unit
wenzelm@556
    61
  val print_trans: syntax -> unit
wenzelm@556
    62
  val print_syntax: syntax -> unit
wenzelm@14904
    63
  val read: (string -> bool) -> syntax -> typ -> string -> term list
wenzelm@12316
    64
  val read_typ: syntax -> ((indexname * sort) list -> indexname -> sort) -> (sort -> sort) ->
wenzelm@12316
    65
    string -> typ
wenzelm@8894
    66
  val read_sort: syntax -> string -> sort
wenzelm@3779
    67
  val pretty_term: syntax -> bool -> term -> Pretty.T
wenzelm@556
    68
  val pretty_typ: syntax -> typ -> Pretty.T
wenzelm@3779
    69
  val pretty_sort: syntax -> sort -> Pretty.T
wenzelm@556
    70
  val simple_pprint_typ: typ -> pprint_args -> unit
clasohm@882
    71
  val ambiguity_level: int ref
skalberg@14177
    72
  val ambiguity_is_error: bool ref
wenzelm@2383
    73
end;
clasohm@0
    74
wenzelm@12094
    75
structure Syntax: SYNTAX =
clasohm@0
    76
struct
clasohm@0
    77
wenzelm@237
    78
(** tables of translation functions **)
wenzelm@237
    79
wenzelm@5692
    80
(* parse (ast) translations *)
wenzelm@5692
    81
skalberg@15570
    82
fun lookup_tr tab c = Option.map fst (Symtab.lookup (tab, c));
wenzelm@237
    83
wenzelm@383
    84
fun err_dup_trfuns name cs =
wenzelm@383
    85
  error ("More than one " ^ name ^ " for " ^ commas_quote cs);
wenzelm@237
    86
wenzelm@15755
    87
fun extend_trtab name trfuns tab = Symtab.extend (tab, trfuns)
wenzelm@15755
    88
  handle Symtab.DUPS cs => err_dup_trfuns name cs;
wenzelm@237
    89
wenzelm@15755
    90
fun remove_trtab trfuns = trfuns |> fold (fn (c, (_, s: stamp)) => fn tab =>
wenzelm@15755
    91
  if Option.map snd (Symtab.lookup (tab, c)) = SOME s
wenzelm@15755
    92
  then Symtab.delete c tab
wenzelm@15755
    93
  else tab);
wenzelm@15755
    94
wenzelm@15755
    95
fun merge_trtabs name tab1 tab2 = Symtab.merge SynExt.eq_trfun (tab1, tab2)
wenzelm@15755
    96
  handle Symtab.DUPS cs => err_dup_trfuns name cs;
wenzelm@237
    97
wenzelm@237
    98
wenzelm@5692
    99
(* print (ast) translations *)
wenzelm@5692
   100
wenzelm@5692
   101
fun lookup_tr' tab c = map fst (Symtab.lookup_multi (tab, c));
wenzelm@15755
   102
wenzelm@15755
   103
fun extend_tr'tab trfuns tab = foldr Symtab.update_multi tab trfuns;
wenzelm@15755
   104
wenzelm@15755
   105
fun remove_tr'tab trfuns = trfuns |> fold (fn (c, tr) =>
wenzelm@15755
   106
  Symtab.map_entry c (gen_remove SynExt.eq_trfun tr));
wenzelm@15755
   107
wenzelm@15755
   108
fun merge_tr'tabs tab1 tab2 = Symtab.merge_multi' SynExt.eq_trfun (tab1, tab2);
wenzelm@5692
   109
wenzelm@5692
   110
wenzelm@237
   111
wenzelm@2700
   112
(** tables of token translation functions **)
wenzelm@2700
   113
wenzelm@2700
   114
fun lookup_tokentr tabs modes =
skalberg@15570
   115
  let val trs = gen_distinct eq_fst (List.concat (map (assocs tabs) (modes @ [""])))
skalberg@15570
   116
  in fn c => Option.map fst (assoc (trs, c)) end;
wenzelm@2700
   117
wenzelm@2700
   118
fun merge_tokentrtabs tabs1 tabs2 =
wenzelm@2700
   119
  let
wenzelm@2700
   120
    fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2;
wenzelm@2700
   121
wenzelm@4703
   122
    fun name (s, _) = implode (tl (Symbol.explode s));
wenzelm@2706
   123
wenzelm@2700
   124
    fun merge mode =
wenzelm@2700
   125
      let
wenzelm@2700
   126
        val trs1 = assocs tabs1 mode;
wenzelm@2700
   127
        val trs2 = assocs tabs2 mode;
wenzelm@2700
   128
        val trs = gen_distinct eq_tr (trs1 @ trs2);
wenzelm@2700
   129
      in
wenzelm@2700
   130
        (case gen_duplicates eq_fst trs of
wenzelm@2700
   131
          [] => (mode, trs)
wenzelm@2700
   132
        | dups => error ("More than one token translation function in mode " ^
wenzelm@2706
   133
            quote mode ^ " for " ^ commas_quote (map name dups)))
wenzelm@2700
   134
      end;
wenzelm@2700
   135
  in
wenzelm@2700
   136
    map merge (distinct (map fst (tabs1 @ tabs2)))
wenzelm@2700
   137
  end;
wenzelm@2700
   138
wenzelm@15755
   139
fun extend_tokentrtab tokentrs tabs =
wenzelm@2700
   140
  let
wenzelm@2700
   141
    fun ins_tokentr (ts, (m, c, f)) =
wenzelm@2700
   142
      overwrite (ts, (m, ("_" ^ c, (f, stamp ())) :: assocs ts m));
wenzelm@2700
   143
  in
skalberg@15570
   144
    merge_tokentrtabs tabs (Library.foldl ins_tokentr ([], tokentrs))
wenzelm@2700
   145
  end;
wenzelm@2700
   146
wenzelm@2700
   147
wenzelm@2700
   148
wenzelm@237
   149
(** tables of translation rules **)
wenzelm@237
   150
wenzelm@5692
   151
type ruletab = (Ast.ast * Ast.ast) list Symtab.table;
wenzelm@237
   152
skalberg@15570
   153
fun dest_ruletab tab = List.concat (map snd (Symtab.dest tab));
wenzelm@9372
   154
fun lookup_ruletab tab a = Symtab.lookup_multi (tab, a);
wenzelm@237
   155
wenzelm@237
   156
wenzelm@237
   157
(* empty, extend, merge ruletabs *)
wenzelm@237
   158
wenzelm@15755
   159
fun extend_ruletab rules tab =
skalberg@15574
   160
  foldr Symtab.update_multi tab (map (fn r => (Ast.head_of_rule r, r)) rules);
wenzelm@237
   161
wenzelm@15755
   162
val remove_ruletab = fold (fn r => Symtab.map_entry (Ast.head_of_rule r) (remove r));
wenzelm@15755
   163
wenzelm@12292
   164
fun merge_ruletabs tab1 tab2 = Symtab.merge_multi' (op =) (tab1, tab2);
clasohm@0
   165
clasohm@0
   166
clasohm@0
   167
clasohm@0
   168
(** datatype syntax **)
clasohm@0
   169
wenzelm@237
   170
datatype syntax =
wenzelm@237
   171
  Syntax of {
wenzelm@15755
   172
    input: SynExt.xprod list,
wenzelm@4703
   173
    lexicon: Scan.lexicon,
wenzelm@5692
   174
    gram: Parser.gram,
wenzelm@18
   175
    consts: string list,
wenzelm@2913
   176
    prmodes: string list,
wenzelm@5692
   177
    parse_ast_trtab: ((Ast.ast list -> Ast.ast) * stamp) Symtab.table,
wenzelm@237
   178
    parse_ruletab: ruletab,
wenzelm@5692
   179
    parse_trtab: ((term list -> term) * stamp) Symtab.table,
wenzelm@5692
   180
    print_trtab: ((bool -> typ -> term list -> term) * stamp) list Symtab.table,
wenzelm@237
   181
    print_ruletab: ruletab,
wenzelm@5692
   182
    print_ast_trtab: ((Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
wenzelm@6322
   183
    tokentrtab: (string * (string * ((string -> string * real) * stamp)) list) list,
wenzelm@5692
   184
    prtabs: Printer.prtabs}
clasohm@0
   185
wenzelm@14687
   186
fun is_keyword (Syntax {lexicon, ...}) = Scan.is_literal lexicon o Symbol.explode;
wenzelm@14687
   187
wenzelm@15755
   188
val default_mode = ("", true);
wenzelm@15755
   189
wenzelm@18
   190
wenzelm@237
   191
(* empty_syntax *)
wenzelm@18
   192
wenzelm@237
   193
val empty_syntax =
wenzelm@237
   194
  Syntax {
wenzelm@15755
   195
    input = [],
wenzelm@4703
   196
    lexicon = Scan.empty_lexicon,
wenzelm@5692
   197
    gram = Parser.empty_gram,
wenzelm@167
   198
    consts = [],
wenzelm@2913
   199
    prmodes = [],
wenzelm@5692
   200
    parse_ast_trtab = Symtab.empty,
wenzelm@5692
   201
    parse_ruletab = Symtab.empty,
wenzelm@5692
   202
    parse_trtab = Symtab.empty,
wenzelm@5692
   203
    print_trtab = Symtab.empty,
wenzelm@5692
   204
    print_ruletab = Symtab.empty,
wenzelm@5692
   205
    print_ast_trtab = Symtab.empty,
wenzelm@2700
   206
    tokentrtab = [],
wenzelm@15755
   207
    prtabs = Printer.empty_prtabs};
wenzelm@167
   208
wenzelm@167
   209
wenzelm@237
   210
(* extend_syntax *)
wenzelm@167
   211
wenzelm@14904
   212
fun extend_syntax (mode, inout) syn_ext (Syntax tabs) =
wenzelm@167
   213
  let
wenzelm@15755
   214
    val {input, lexicon, gram, consts = consts1, prmodes = prmodes1,
wenzelm@2366
   215
      parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
wenzelm@2700
   216
      print_ast_trtab, tokentrtab, prtabs} = tabs;
wenzelm@14904
   217
    val SynExt.SynExt {xprods, consts = consts2, prmodes = prmodes2,
wenzelm@2913
   218
      parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
wenzelm@2700
   219
      print_ast_translation, token_translation} = syn_ext;
wenzelm@167
   220
  in
wenzelm@237
   221
    Syntax {
wenzelm@15755
   222
      input = if inout then xprods @ input else input,
wenzelm@5692
   223
      lexicon = if inout then Scan.extend_lexicon lexicon (SynExt.delims_of xprods) else lexicon,
wenzelm@5692
   224
      gram = if inout then Parser.extend_gram gram xprods else gram,
wenzelm@15755
   225
      consts = merge_lists' consts1 consts2,
wenzelm@15755
   226
      prmodes = mode ins_string (merge_lists' prmodes1 prmodes2),
wenzelm@167
   227
      parse_ast_trtab =
wenzelm@15755
   228
        extend_trtab "parse ast translation" parse_ast_translation parse_ast_trtab,
wenzelm@15755
   229
      parse_ruletab = extend_ruletab parse_rules parse_ruletab,
wenzelm@15755
   230
      parse_trtab = extend_trtab "parse translation" parse_translation parse_trtab,
wenzelm@15755
   231
      print_trtab = extend_tr'tab print_translation print_trtab,
wenzelm@15755
   232
      print_ruletab = extend_ruletab print_rules print_ruletab,
wenzelm@15755
   233
      print_ast_trtab = extend_tr'tab print_ast_translation print_ast_trtab,
wenzelm@15755
   234
      tokentrtab = extend_tokentrtab token_translation tokentrtab,
wenzelm@15755
   235
      prtabs = Printer.extend_prtabs mode xprods prtabs}
wenzelm@18
   236
  end;
wenzelm@18
   237
wenzelm@15755
   238
wenzelm@15755
   239
(* remove_syntax *)
wenzelm@15755
   240
wenzelm@15755
   241
fun remove_syntax (mode, inout) syn_ext (Syntax tabs) =
wenzelm@15755
   242
  let
wenzelm@15755
   243
    val SynExt.SynExt {xprods, consts = _, prmodes = _,
wenzelm@15755
   244
      parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
wenzelm@15755
   245
      print_ast_translation, token_translation = _} = syn_ext;
wenzelm@15755
   246
    val {input, lexicon, gram, consts, prmodes,
wenzelm@15755
   247
      parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
wenzelm@15755
   248
      print_ast_trtab, tokentrtab, prtabs} = tabs;
wenzelm@15755
   249
    val input' = if inout then fold remove xprods input else input;
wenzelm@15755
   250
  in
wenzelm@15755
   251
    Syntax {
wenzelm@15755
   252
      input = input',
wenzelm@15755
   253
      lexicon = if inout then Scan.make_lexicon (SynExt.delims_of input') else lexicon,
wenzelm@15755
   254
      gram = if inout then Parser.make_gram input' else gram,
wenzelm@15755
   255
      consts = consts,
wenzelm@15755
   256
      prmodes = prmodes,
wenzelm@15755
   257
      parse_ast_trtab = remove_trtab parse_ast_translation parse_ast_trtab,
wenzelm@15755
   258
      parse_ruletab = remove_ruletab parse_rules parse_ruletab,
wenzelm@15755
   259
      parse_trtab = remove_trtab parse_translation parse_trtab,
wenzelm@15755
   260
      print_trtab = remove_tr'tab print_translation print_trtab,
wenzelm@15755
   261
      print_ruletab = remove_ruletab print_rules print_ruletab,
wenzelm@15755
   262
      print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab,
wenzelm@15755
   263
      tokentrtab = tokentrtab,
wenzelm@15755
   264
      prtabs = Printer.remove_prtabs mode xprods prtabs}
wenzelm@15755
   265
  end;
wenzelm@14904
   266
wenzelm@18
   267
wenzelm@237
   268
(* merge_syntaxes *)
clasohm@0
   269
wenzelm@237
   270
fun merge_syntaxes (Syntax tabs1) (Syntax tabs2) =
clasohm@0
   271
  let
wenzelm@15755
   272
    val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
wenzelm@15755
   273
      prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1,
wenzelm@15755
   274
      parse_ruletab = parse_ruletab1, parse_trtab = parse_trtab1,
wenzelm@15755
   275
      print_trtab = print_trtab1, print_ruletab = print_ruletab1,
wenzelm@15755
   276
      print_ast_trtab = print_ast_trtab1, tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
wenzelm@237
   277
wenzelm@15755
   278
    val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
wenzelm@15755
   279
      prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2,
wenzelm@15755
   280
      parse_ruletab = parse_ruletab2, parse_trtab = parse_trtab2,
wenzelm@15755
   281
      print_trtab = print_trtab2, print_ruletab = print_ruletab2,
wenzelm@15755
   282
      print_ast_trtab = print_ast_trtab2, tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
clasohm@0
   283
  in
wenzelm@237
   284
    Syntax {
wenzelm@15755
   285
      input = merge_lists' input1 input2,
wenzelm@4703
   286
      lexicon = Scan.merge_lexicons lexicon1 lexicon2,
wenzelm@5692
   287
      gram = Parser.merge_grams gram1 gram2,
berghofe@11528
   288
      consts = unique_strings (sort_strings (consts1 @ consts2)),
wenzelm@2913
   289
      prmodes = merge_lists prmodes1 prmodes2,
wenzelm@237
   290
      parse_ast_trtab =
wenzelm@15755
   291
        merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
wenzelm@237
   292
      parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
wenzelm@15755
   293
      parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
wenzelm@5692
   294
      print_trtab = merge_tr'tabs print_trtab1 print_trtab2,
wenzelm@237
   295
      print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
wenzelm@5692
   296
      print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
wenzelm@2700
   297
      tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2,
wenzelm@5692
   298
      prtabs = Printer.merge_prtabs prtabs1 prtabs2}
clasohm@0
   299
  end;
clasohm@0
   300
clasohm@0
   301
wenzelm@260
   302
(* type_syn *)
wenzelm@260
   303
wenzelm@14904
   304
val type_syn = empty_syntax |> extend_syntax default_mode TypeExt.type_ext;
wenzelm@14904
   305
val pure_syn = type_syn |> extend_syntax default_mode SynExt.pure_ext;
wenzelm@260
   306
clasohm@0
   307
wenzelm@4887
   308
wenzelm@237
   309
(** inspect syntax **)
clasohm@0
   310
wenzelm@260
   311
fun pretty_strs_qs name strs =
berghofe@14598
   312
  Pretty.strs (name :: map Library.quote (sort_strings strs));
clasohm@0
   313
wenzelm@18
   314
wenzelm@18
   315
(* print_gram *)
clasohm@0
   316
wenzelm@237
   317
fun print_gram (Syntax tabs) =
clasohm@0
   318
  let
wenzelm@14904
   319
    val {lexicon, prmodes, gram, prtabs, ...} = tabs;
wenzelm@2913
   320
    val prmodes' = sort_strings (filter_out (equal "") prmodes);
clasohm@0
   321
  in
wenzelm@8720
   322
    [pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon),
wenzelm@8720
   323
      Pretty.big_list "prods:" (Parser.pretty_gram gram),
wenzelm@8720
   324
      pretty_strs_qs "print modes:" prmodes']
wenzelm@8720
   325
    |> Pretty.chunks |> Pretty.writeln
clasohm@0
   326
  end;
clasohm@0
   327
clasohm@0
   328
wenzelm@18
   329
(* print_trans *)
clasohm@0
   330
wenzelm@237
   331
fun print_trans (Syntax tabs) =
clasohm@0
   332
  let
wenzelm@260
   333
    fun pretty_trtab name tab =
wenzelm@5692
   334
      pretty_strs_qs name (Symtab.keys tab);
clasohm@0
   335
wenzelm@260
   336
    fun pretty_ruletab name tab =
wenzelm@5692
   337
      Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab));
clasohm@0
   338
berghofe@14598
   339
    fun pretty_tokentr (mode, trs) = Pretty.strs (Library.quote mode ^ ":" :: map fst trs);
wenzelm@4703
   340
wenzelm@237
   341
    val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
wenzelm@4703
   342
      print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs;
clasohm@0
   343
  in
wenzelm@8720
   344
    [pretty_strs_qs "consts:" consts,
wenzelm@8720
   345
      pretty_trtab "parse_ast_translation:" parse_ast_trtab,
wenzelm@8720
   346
      pretty_ruletab "parse_rules:" parse_ruletab,
wenzelm@8720
   347
      pretty_trtab "parse_translation:" parse_trtab,
wenzelm@8720
   348
      pretty_trtab "print_translation:" print_trtab,
wenzelm@8720
   349
      pretty_ruletab "print_rules:" print_ruletab,
wenzelm@8720
   350
      pretty_trtab "print_ast_translation:" print_ast_trtab,
wenzelm@8720
   351
      Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab)]
wenzelm@8720
   352
    |> Pretty.chunks |> Pretty.writeln
clasohm@0
   353
  end;
clasohm@0
   354
clasohm@0
   355
clasohm@0
   356
(* print_syntax *)
clasohm@0
   357
wenzelm@237
   358
fun print_syntax syn = (print_gram syn; print_trans syn);
clasohm@0
   359
clasohm@0
   360
clasohm@0
   361
wenzelm@237
   362
(** read **)
wenzelm@18
   363
wenzelm@260
   364
(* read_ast *)
wenzelm@260
   365
clasohm@882
   366
val ambiguity_level = ref 1;
wenzelm@15755
   367
val ambiguity_is_error = ref false
clasohm@882
   368
wenzelm@14904
   369
fun read_asts is_logtype (Syntax tabs) xids root str =
wenzelm@260
   370
  let
wenzelm@14904
   371
    val {lexicon, gram, parse_ast_trtab, ...} = tabs;
wenzelm@14904
   372
    val root' = if root <> "prop" andalso is_logtype root then SynExt.logic else root;
wenzelm@4703
   373
    val chars = Symbol.explode str;
wenzelm@5692
   374
    val pts = Parser.parse gram root' (Lexicon.tokenize lexicon xids chars);
clasohm@330
   375
wenzelm@12292
   376
    fun show_pt pt =
skalberg@15531
   377
      warning (Pretty.string_of (Ast.pretty_ast (hd (SynTrans.pts_to_asts (K NONE) [pt]))));
wenzelm@260
   378
  in
wenzelm@888
   379
    if length pts > ! ambiguity_level then
wenzelm@14687
   380
        if ! ambiguity_is_error then
wenzelm@14687
   381
            error ("Ambiguous input " ^ quote str)
wenzelm@14687
   382
        else
wenzelm@14687
   383
            (warning ("Ambiguous input " ^ quote str);
wenzelm@14687
   384
             warning "produces the following parse trees:";
skalberg@15570
   385
             List.app show_pt pts)
clasohm@624
   386
    else ();
berghofe@14798
   387
    SynTrans.pts_to_asts (lookup_tr parse_ast_trtab) pts
wenzelm@260
   388
  end;
wenzelm@260
   389
wenzelm@260
   390
wenzelm@237
   391
(* read *)
clasohm@0
   392
wenzelm@14904
   393
fun read is_logtype (syn as Syntax tabs) ty str =
clasohm@0
   394
  let
wenzelm@237
   395
    val {parse_ruletab, parse_trtab, ...} = tabs;
wenzelm@14904
   396
    val asts = read_asts is_logtype syn false (SynExt.typ_to_nonterm ty) str;
clasohm@0
   397
  in
berghofe@14798
   398
    SynTrans.asts_to_terms (lookup_tr parse_trtab)
wenzelm@5692
   399
      (map (Ast.normalize_ast (lookup_ruletab parse_ruletab)) asts)
clasohm@0
   400
  end;
clasohm@0
   401
clasohm@0
   402
wenzelm@237
   403
(* read types *)
clasohm@0
   404
wenzelm@12316
   405
fun read_typ syn get_sort map_sort str =
wenzelm@14904
   406
  (case read (K false) syn SynExt.typeT str of
wenzelm@12316
   407
    [t] => TypeExt.typ_of_term (get_sort (TypeExt.raw_term_sorts t)) map_sort t
wenzelm@8894
   408
  | _ => error "read_typ: ambiguous syntax");
wenzelm@144
   409
wenzelm@144
   410
wenzelm@8894
   411
(* read sorts *)
wenzelm@8894
   412
wenzelm@8894
   413
fun read_sort syn str =
wenzelm@14904
   414
  (case read (K false) syn TypeExt.sortT str of
wenzelm@8894
   415
    [t] => TypeExt.sort_of_term t
wenzelm@8894
   416
  | _ => error "read_sort: ambiguous syntax");
wenzelm@8894
   417
wenzelm@8894
   418
wenzelm@18
   419
wenzelm@1158
   420
(** prepare translation rules **)
wenzelm@1158
   421
wenzelm@1158
   422
datatype 'a trrule =
wenzelm@3526
   423
  ParseRule of 'a * 'a |
wenzelm@3526
   424
  PrintRule of 'a * 'a |
wenzelm@3526
   425
  ParsePrintRule of 'a * 'a;
wenzelm@888
   426
wenzelm@4618
   427
fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y)
wenzelm@4618
   428
  | map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y)
wenzelm@4618
   429
  | map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y);
wenzelm@1158
   430
skalberg@15531
   431
fun parse_rule (ParseRule pats) = SOME pats
skalberg@15531
   432
  | parse_rule (PrintRule _) = NONE
skalberg@15531
   433
  | parse_rule (ParsePrintRule pats) = SOME pats;
wenzelm@1158
   434
skalberg@15531
   435
fun print_rule (ParseRule _) = NONE
skalberg@15531
   436
  | print_rule (PrintRule pats) = SOME (swap pats)
skalberg@15531
   437
  | print_rule (ParsePrintRule pats) = SOME (swap pats);
wenzelm@1158
   438
wenzelm@1158
   439
wenzelm@1158
   440
fun check_rule (rule as (lhs, rhs)) =
wenzelm@5692
   441
  (case Ast.rule_error rule of
skalberg@15531
   442
    SOME msg =>
wenzelm@1158
   443
      error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
wenzelm@5692
   444
        Ast.str_of_ast lhs ^ "  ->  " ^ Ast.str_of_ast rhs)
skalberg@15531
   445
  | NONE => rule);
wenzelm@888
   446
wenzelm@888
   447
wenzelm@14904
   448
fun read_pattern is_logtype syn (root, str) =
wenzelm@18
   449
  let
wenzelm@237
   450
    val Syntax {consts, ...} = syn;
wenzelm@18
   451
wenzelm@5692
   452
    fun constify (ast as Ast.Constant _) = ast
wenzelm@5692
   453
      | constify (ast as Ast.Variable x) =
wenzelm@8720
   454
          if x mem consts orelse NameSpace.is_qualified x then Ast.Constant x
wenzelm@3830
   455
          else ast
wenzelm@5692
   456
      | constify (Ast.Appl asts) = Ast.Appl (map constify asts);
wenzelm@18
   457
  in
wenzelm@14904
   458
    (case read_asts is_logtype syn true root str of
wenzelm@888
   459
      [ast] => constify ast
wenzelm@888
   460
    | _ => error ("Syntactically ambiguous input: " ^ quote str))
wenzelm@888
   461
  end handle ERROR =>
wenzelm@888
   462
    error ("The error(s) above occurred in translation pattern " ^
wenzelm@888
   463
      quote str);
clasohm@0
   464
wenzelm@556
   465
wenzelm@1158
   466
fun prep_rules rd_pat raw_rules =
wenzelm@4618
   467
  let val rules = map (map_trrule rd_pat) raw_rules in
skalberg@15570
   468
    (map check_rule (List.mapPartial parse_rule rules),
skalberg@15570
   469
      map check_rule (List.mapPartial print_rule rules))
wenzelm@1158
   470
  end
wenzelm@18
   471
wenzelm@18
   472
wenzelm@18
   473
wenzelm@3779
   474
(** pretty terms, typs, sorts **)
wenzelm@18
   475
wenzelm@3779
   476
fun pretty_t t_to_ast prt_t (syn as Syntax tabs) curried t =
clasohm@0
   477
  let
wenzelm@2700
   478
    val {print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs;
wenzelm@5692
   479
    val ast = t_to_ast (lookup_tr' print_trtab) t;
clasohm@0
   480
  in
wenzelm@5692
   481
    prt_t curried prtabs (lookup_tr' print_ast_trtab)
oheimb@6167
   482
      (lookup_tokentr tokentrtab (! print_mode))
wenzelm@5692
   483
      (Ast.normalize_ast (lookup_ruletab print_ruletab) ast)
clasohm@0
   484
  end;
clasohm@0
   485
wenzelm@5692
   486
val pretty_term = pretty_t Printer.term_to_ast Printer.pretty_term_ast;
wenzelm@5692
   487
fun pretty_typ syn = pretty_t Printer.typ_to_ast Printer.pretty_typ_ast syn false;
wenzelm@5692
   488
fun pretty_sort syn = pretty_t Printer.sort_to_ast Printer.pretty_typ_ast syn false;
clasohm@0
   489
wenzelm@260
   490
val simple_pprint_typ = Pretty.pprint o Pretty.quote o (pretty_typ type_syn);
clasohm@0
   491
clasohm@0
   492
clasohm@0
   493
wenzelm@14904
   494
(** extend syntax **)
wenzelm@383
   495
wenzelm@14904
   496
fun ext_syntax' f is_logtype prmode decls = extend_syntax prmode (f is_logtype decls);
wenzelm@14904
   497
fun ext_syntax f = ext_syntax' (K f) (K false) default_mode;
wenzelm@383
   498
wenzelm@14904
   499
val extend_type_gram   = ext_syntax Mixfix.syn_ext_types;
wenzelm@14904
   500
val extend_const_gram  = ext_syntax' Mixfix.syn_ext_consts;
wenzelm@14904
   501
val extend_consts      = ext_syntax SynExt.syn_ext_const_names;
wenzelm@14904
   502
val extend_trfuns      = ext_syntax SynExt.syn_ext_trfuns;
wenzelm@14904
   503
val extend_tokentrfuns = ext_syntax SynExt.syn_ext_tokentrfuns;
wenzelm@14904
   504
val extend_trrules_i   = ext_syntax SynExt.syn_ext_rules o prep_rules I;
wenzelm@383
   505
wenzelm@14904
   506
fun extend_trrules is_logtype syn rules =
wenzelm@14904
   507
  ext_syntax' (K SynExt.syn_ext_rules) (K false) default_mode
wenzelm@14904
   508
    (prep_rules (read_pattern is_logtype syn) rules);
wenzelm@5692
   509
wenzelm@15755
   510
fun remove_const_gram is_logtype prmode decls =
wenzelm@15755
   511
  remove_syntax prmode (Mixfix.syn_ext_consts is_logtype decls);
wenzelm@15755
   512
wenzelm@5692
   513
wenzelm@5692
   514
wenzelm@5692
   515
(** export parts of internal Syntax structures **)
wenzelm@5692
   516
wenzelm@6322
   517
open TokenTrans Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer;
wenzelm@2366
   518
clasohm@0
   519
end;
wenzelm@5692
   520
wenzelm@5692
   521
structure BasicSyntax: BASIC_SYNTAX = Syntax;
wenzelm@5692
   522
open BasicSyntax;