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