src/Pure/Syntax/syntax.ML
author wenzelm
Sat May 02 13:27:42 1998 +0200 (1998-05-02)
changeset 4887 bbc13af86c16
parent 4703 a50ab39756db
child 5692 2e873c5f0e2c
permissions -rw-r--r--
added trfun_names;
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@260
    18
  include AST1
clasohm@0
    19
  include LEXICON0
wenzelm@237
    20
  include SYN_EXT0
clasohm@0
    21
  include TYPE_EXT0
wenzelm@556
    22
  include SYN_TRANS1
wenzelm@556
    23
  include MIXFIX1
clasohm@0
    24
  include PRINTER0
wenzelm@1158
    25
  datatype 'a trrule =
wenzelm@3526
    26
    ParseRule of 'a * 'a |
wenzelm@3526
    27
    PrintRule of 'a * 'a |
wenzelm@3526
    28
    ParsePrintRule of 'a * 'a
wenzelm@556
    29
  type syntax
wenzelm@556
    30
  val extend_log_types: syntax -> string list -> syntax
wenzelm@556
    31
  val extend_type_gram: syntax -> (string * int * mixfix) list -> syntax
wenzelm@2366
    32
  val extend_const_gram: syntax -> string * bool -> (string * typ * mixfix) list -> syntax
wenzelm@556
    33
  val extend_consts: syntax -> string list -> syntax
wenzelm@556
    34
  val extend_trfuns: syntax ->
wenzelm@556
    35
    (string * (ast list -> ast)) list *
wenzelm@556
    36
    (string * (term list -> term)) list *
wenzelm@556
    37
    (string * (term list -> term)) list *
wenzelm@556
    38
    (string * (ast list -> ast)) list -> syntax
wenzelm@4145
    39
  val extend_trfunsT: syntax -> (string * (bool -> typ -> term list -> term)) list -> syntax
wenzelm@2700
    40
  val extend_tokentrfuns: syntax -> (string * string * (string -> string * int)) list -> syntax
wenzelm@1158
    41
  val extend_trrules: syntax -> (string * string) trrule list -> syntax
wenzelm@1158
    42
  val extend_trrules_i: syntax -> ast trrule list -> syntax
wenzelm@4618
    43
  val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
wenzelm@556
    44
  val merge_syntaxes: syntax -> syntax -> syntax
wenzelm@556
    45
  val type_syn: syntax
clasohm@764
    46
  val pure_syn: syntax
wenzelm@556
    47
  val print_gram: syntax -> unit
wenzelm@556
    48
  val print_trans: syntax -> unit
wenzelm@556
    49
  val print_syntax: syntax -> unit
wenzelm@4887
    50
  val trfun_names: syntax -> string list * string list * string list * string list
wenzelm@556
    51
  val test_read: syntax -> string -> string -> unit
clasohm@624
    52
  val read: syntax -> typ -> string -> term list
wenzelm@3779
    53
  val read_typ: syntax -> ((indexname * sort) list -> indexname -> sort) -> string -> typ
wenzelm@556
    54
  val simple_read_typ: string -> typ
wenzelm@3779
    55
  val pretty_term: syntax -> bool -> term -> Pretty.T
wenzelm@556
    56
  val pretty_typ: syntax -> typ -> Pretty.T
wenzelm@3779
    57
  val pretty_sort: syntax -> sort -> Pretty.T
wenzelm@3782
    58
  val simple_str_of_sort: sort -> string
wenzelm@556
    59
  val simple_string_of_typ: typ -> string
wenzelm@556
    60
  val simple_pprint_typ: typ -> pprint_args -> unit
clasohm@882
    61
  val ambiguity_level: int ref
wenzelm@2383
    62
end;
clasohm@0
    63
paulson@1511
    64
structure Syntax : SYNTAX =
clasohm@0
    65
struct
clasohm@0
    66
paulson@1511
    67
open Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer;
wenzelm@237
    68
wenzelm@237
    69
wenzelm@237
    70
(** tables of translation functions **)
wenzelm@237
    71
wenzelm@2383
    72
(*does not subsume typed print translations*)
wenzelm@2504
    73
type 'a trtab = (('a list -> 'a) * stamp) Symtab.table;
wenzelm@237
    74
wenzelm@4887
    75
fun dest_trtab tab = map fst (Symtab.dest tab);
wenzelm@237
    76
wenzelm@237
    77
fun lookup_trtab tab c =
wenzelm@237
    78
  apsome fst (Symtab.lookup (tab, c));
clasohm@0
    79
clasohm@0
    80
wenzelm@237
    81
(* empty, extend, merge trtabs *)
wenzelm@237
    82
wenzelm@383
    83
fun err_dup_trfuns name cs =
wenzelm@383
    84
  error ("More than one " ^ name ^ " for " ^ commas_quote cs);
wenzelm@237
    85
wenzelm@4487
    86
val empty_trtab = Symtab.empty;
wenzelm@237
    87
wenzelm@237
    88
fun extend_trtab tab trfuns name =
wenzelm@4487
    89
  Symtab.extend (tab, map (fn (c, f) => (c, (f, stamp ()))) trfuns)
wenzelm@383
    90
    handle Symtab.DUPS cs => err_dup_trfuns name cs;
wenzelm@237
    91
wenzelm@237
    92
fun merge_trtabs tab1 tab2 name =
wenzelm@237
    93
  Symtab.merge eq_snd (tab1, tab2)
wenzelm@383
    94
    handle Symtab.DUPS cs => err_dup_trfuns name cs;
wenzelm@237
    95
wenzelm@237
    96
wenzelm@237
    97
wenzelm@2700
    98
(** tables of token translation functions **)
wenzelm@2700
    99
wenzelm@2700
   100
fun lookup_tokentr tabs modes =
wenzelm@4496
   101
  let val trs = gen_distinct eq_fst (flat (map (assocs tabs) (modes @ [""])))
wenzelm@2700
   102
  in fn c => apsome fst (assoc (trs, c)) end;
wenzelm@2700
   103
wenzelm@2700
   104
fun merge_tokentrtabs tabs1 tabs2 =
wenzelm@2700
   105
  let
wenzelm@2700
   106
    fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2;
wenzelm@2700
   107
wenzelm@4703
   108
    fun name (s, _) = implode (tl (Symbol.explode s));
wenzelm@2706
   109
wenzelm@2700
   110
    fun merge mode =
wenzelm@2700
   111
      let
wenzelm@2700
   112
        val trs1 = assocs tabs1 mode;
wenzelm@2700
   113
        val trs2 = assocs tabs2 mode;
wenzelm@2700
   114
        val trs = gen_distinct eq_tr (trs1 @ trs2);
wenzelm@2700
   115
      in
wenzelm@2700
   116
        (case gen_duplicates eq_fst trs of
wenzelm@2700
   117
          [] => (mode, trs)
wenzelm@2700
   118
        | dups => error ("More than one token translation function in mode " ^
wenzelm@2706
   119
            quote mode ^ " for " ^ commas_quote (map name dups)))
wenzelm@2700
   120
      end;
wenzelm@2700
   121
  in
wenzelm@2700
   122
    map merge (distinct (map fst (tabs1 @ tabs2)))
wenzelm@2700
   123
  end;
wenzelm@2700
   124
wenzelm@2700
   125
fun extend_tokentrtab tabs tokentrs =
wenzelm@2700
   126
  let
wenzelm@2700
   127
    fun ins_tokentr (ts, (m, c, f)) =
wenzelm@2700
   128
      overwrite (ts, (m, ("_" ^ c, (f, stamp ())) :: assocs ts m));
wenzelm@2700
   129
  in
wenzelm@2700
   130
    merge_tokentrtabs tabs (foldl ins_tokentr ([], tokentrs))
wenzelm@2700
   131
  end;
wenzelm@2700
   132
wenzelm@2700
   133
wenzelm@2700
   134
wenzelm@237
   135
(** tables of translation rules **)
wenzelm@237
   136
wenzelm@237
   137
type ruletab = (ast * ast) list Symtab.table;
wenzelm@237
   138
wenzelm@383
   139
fun dest_ruletab tab = flat (map snd (Symtab.dest tab));
wenzelm@237
   140
wenzelm@237
   141
wenzelm@237
   142
(* lookup_ruletab *)
wenzelm@237
   143
wenzelm@237
   144
fun lookup_ruletab tab =
wenzelm@4487
   145
  if Symtab.is_empty tab then None
wenzelm@237
   146
  else Some (fn a => Symtab.lookup_multi (tab, a));
wenzelm@237
   147
wenzelm@237
   148
wenzelm@237
   149
(* empty, extend, merge ruletabs *)
wenzelm@237
   150
wenzelm@4487
   151
val empty_ruletab = Symtab.empty;
wenzelm@237
   152
wenzelm@237
   153
fun extend_ruletab tab rules =
wenzelm@237
   154
  generic_extend (op =) Symtab.dest_multi Symtab.make_multi tab
wenzelm@237
   155
    (map (fn r => (head_of_rule r, r)) (distinct rules));
wenzelm@237
   156
wenzelm@237
   157
fun merge_ruletabs tab1 tab2 =
wenzelm@237
   158
  generic_merge (op =) Symtab.dest_multi Symtab.make_multi tab1 tab2;
clasohm@0
   159
clasohm@0
   160
clasohm@0
   161
clasohm@0
   162
(** datatype syntax **)
clasohm@0
   163
wenzelm@237
   164
datatype syntax =
wenzelm@237
   165
  Syntax of {
wenzelm@4703
   166
    lexicon: Scan.lexicon,
clasohm@764
   167
    logtypes: string list,
wenzelm@18
   168
    gram: gram,
wenzelm@18
   169
    consts: string list,
wenzelm@2913
   170
    prmodes: string list,
wenzelm@237
   171
    parse_ast_trtab: ast trtab,
wenzelm@237
   172
    parse_ruletab: ruletab,
wenzelm@237
   173
    parse_trtab: term trtab,
wenzelm@4145
   174
    print_trtab: ((bool -> typ -> term list -> term) * stamp) Symtab.table,
wenzelm@237
   175
    print_ruletab: ruletab,
wenzelm@237
   176
    print_ast_trtab: ast trtab,
wenzelm@2700
   177
    tokentrtab: (string * (string * ((string -> string * int) * stamp)) list) list,
wenzelm@2913
   178
    prtabs: prtabs}
clasohm@0
   179
wenzelm@18
   180
wenzelm@237
   181
(* empty_syntax *)
wenzelm@18
   182
wenzelm@237
   183
val empty_syntax =
wenzelm@237
   184
  Syntax {
wenzelm@4703
   185
    lexicon = Scan.empty_lexicon,
clasohm@764
   186
    logtypes = [],
wenzelm@171
   187
    gram = empty_gram,
wenzelm@167
   188
    consts = [],
wenzelm@2913
   189
    prmodes = [],
wenzelm@237
   190
    parse_ast_trtab = empty_trtab,
wenzelm@237
   191
    parse_ruletab = empty_ruletab,
wenzelm@237
   192
    parse_trtab = empty_trtab,
wenzelm@237
   193
    print_trtab = empty_trtab,
wenzelm@237
   194
    print_ruletab = empty_ruletab,
wenzelm@237
   195
    print_ast_trtab = empty_trtab,
wenzelm@2700
   196
    tokentrtab = [],
wenzelm@2913
   197
    prtabs = empty_prtabs}
wenzelm@167
   198
wenzelm@167
   199
wenzelm@237
   200
(* extend_syntax *)
wenzelm@167
   201
wenzelm@2366
   202
fun extend_syntax (mode, inout) (Syntax tabs) syn_ext =
wenzelm@167
   203
  let
wenzelm@2913
   204
    val {lexicon, logtypes = logtypes1, gram, consts = consts1, prmodes = prmodes1,
wenzelm@2366
   205
      parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab,
wenzelm@2700
   206
      print_ast_trtab, tokentrtab, prtabs} = tabs;
wenzelm@2913
   207
    val SynExt {logtypes = logtypes2, xprods, consts = consts2, prmodes = prmodes2,
wenzelm@2913
   208
      parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules,
wenzelm@2700
   209
      print_ast_translation, token_translation} = syn_ext;
wenzelm@167
   210
  in
wenzelm@237
   211
    Syntax {
wenzelm@4703
   212
      lexicon = if inout then Scan.extend_lexicon lexicon (delims_of xprods) else lexicon,
clasohm@764
   213
      logtypes = extend_list logtypes1 logtypes2,
wenzelm@2366
   214
      gram = if inout then extend_gram gram xprods else gram,
wenzelm@167
   215
      consts = consts2 union consts1,
wenzelm@2913
   216
      prmodes = (mode ins prmodes2) union 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@167
   221
      print_trtab = extend_trtab print_trtab print_translation "print translation",
wenzelm@167
   222
      print_ruletab = extend_ruletab print_ruletab print_rules,
wenzelm@237
   223
      print_ast_trtab =
wenzelm@237
   224
        extend_trtab print_ast_trtab print_ast_translation "print ast translation",
wenzelm@2700
   225
      tokentrtab = extend_tokentrtab tokentrtab token_translation,
wenzelm@2383
   226
      prtabs = extend_prtabs prtabs mode xprods}
wenzelm@18
   227
  end;
wenzelm@18
   228
wenzelm@18
   229
wenzelm@237
   230
(* merge_syntaxes *)
clasohm@0
   231
wenzelm@237
   232
fun merge_syntaxes (Syntax tabs1) (Syntax tabs2) =
clasohm@0
   233
  let
wenzelm@2913
   234
    val {lexicon = lexicon1, logtypes = logtypes1, gram = gram1, consts = consts1,
wenzelm@2913
   235
      prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1,
wenzelm@237
   236
      parse_trtab = parse_trtab1, print_trtab = print_trtab1,
wenzelm@237
   237
      print_ruletab = print_ruletab1, print_ast_trtab = print_ast_trtab1,
wenzelm@2700
   238
      tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1;
wenzelm@237
   239
wenzelm@2913
   240
    val {lexicon = lexicon2, logtypes = logtypes2, gram = gram2, consts = consts2,
wenzelm@2913
   241
      prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2,
wenzelm@237
   242
      parse_trtab = parse_trtab2, print_trtab = print_trtab2,
wenzelm@237
   243
      print_ruletab = print_ruletab2, print_ast_trtab = print_ast_trtab2,
wenzelm@2700
   244
      tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2;
clasohm@0
   245
  in
wenzelm@237
   246
    Syntax {
wenzelm@4703
   247
      lexicon = Scan.merge_lexicons lexicon1 lexicon2,
clasohm@764
   248
      logtypes = merge_lists logtypes1 logtypes2,
wenzelm@237
   249
      gram = merge_grams gram1 gram2,
wenzelm@237
   250
      consts = merge_lists 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@237
   256
      print_trtab = merge_trtabs print_trtab1 print_trtab2 "print translation",
wenzelm@237
   257
      print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
wenzelm@237
   258
      print_ast_trtab =
wenzelm@237
   259
        merge_trtabs print_ast_trtab1 print_ast_trtab2 "print ast translation",
wenzelm@2700
   260
      tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2,
wenzelm@2383
   261
      prtabs = merge_prtabs prtabs1 prtabs2}
clasohm@0
   262
  end;
clasohm@0
   263
clasohm@0
   264
wenzelm@260
   265
(* type_syn *)
wenzelm@260
   266
wenzelm@2700
   267
val type_syn =
wenzelm@2700
   268
  extend_syntax ("", true) empty_syntax type_ext;
wenzelm@2700
   269
wenzelm@2366
   270
val pure_syn = extend_syntax ("", true) type_syn pure_ext;
wenzelm@260
   271
clasohm@0
   272
wenzelm@4887
   273
wenzelm@237
   274
(** inspect syntax **)
clasohm@0
   275
wenzelm@260
   276
fun pretty_strs_qs name strs =
wenzelm@260
   277
  Pretty.strs (name :: map quote (sort_strings strs));
clasohm@0
   278
wenzelm@18
   279
wenzelm@18
   280
(* print_gram *)
clasohm@0
   281
wenzelm@237
   282
fun print_gram (Syntax tabs) =
clasohm@0
   283
  let
wenzelm@2913
   284
    val {lexicon, logtypes, prmodes, gram, prtabs, ...} = tabs;
wenzelm@2913
   285
    val prmodes' = sort_strings (filter_out (equal "") prmodes);
clasohm@0
   286
  in
wenzelm@4703
   287
    Pretty.writeln (pretty_strs_qs "lexicon:" (map implode (Scan.dest_lexicon lexicon)));
clasohm@764
   288
    Pretty.writeln (Pretty.strs ("logtypes:" :: logtypes));
wenzelm@2202
   289
    Pretty.writeln (Pretty.big_list "prods:" (pretty_gram gram));
wenzelm@2913
   290
    Pretty.writeln (pretty_strs_qs "print modes:" prmodes')
clasohm@0
   291
  end;
clasohm@0
   292
clasohm@0
   293
wenzelm@18
   294
(* print_trans *)
clasohm@0
   295
wenzelm@237
   296
fun print_trans (Syntax tabs) =
clasohm@0
   297
  let
wenzelm@260
   298
    fun pretty_trtab name tab =
wenzelm@4887
   299
      pretty_strs_qs name (dest_trtab tab);
clasohm@0
   300
wenzelm@260
   301
    fun pretty_ruletab name tab =
wenzelm@260
   302
      Pretty.big_list name (map pretty_rule (dest_ruletab tab));
clasohm@0
   303
wenzelm@4703
   304
    fun pretty_tokentr (mode, trs) = Pretty.strs (quote mode ^ ":" :: map fst trs);
wenzelm@4703
   305
wenzelm@237
   306
    val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
wenzelm@4703
   307
      print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs;
clasohm@0
   308
  in
wenzelm@260
   309
    Pretty.writeln (pretty_strs_qs "consts:" consts);
wenzelm@260
   310
    Pretty.writeln (pretty_trtab "parse_ast_translation:" parse_ast_trtab);
wenzelm@260
   311
    Pretty.writeln (pretty_ruletab "parse_rules:" parse_ruletab);
wenzelm@260
   312
    Pretty.writeln (pretty_trtab "parse_translation:" parse_trtab);
wenzelm@260
   313
    Pretty.writeln (pretty_trtab "print_translation:" print_trtab);
wenzelm@260
   314
    Pretty.writeln (pretty_ruletab "print_rules:" print_ruletab);
wenzelm@4703
   315
    Pretty.writeln (pretty_trtab "print_ast_translation:" print_ast_trtab);
wenzelm@4703
   316
    Pretty.writeln (Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab))
clasohm@0
   317
  end;
clasohm@0
   318
clasohm@0
   319
clasohm@0
   320
(* print_syntax *)
clasohm@0
   321
wenzelm@237
   322
fun print_syntax syn = (print_gram syn; print_trans syn);
clasohm@0
   323
clasohm@0
   324
wenzelm@4887
   325
(* trfun_names *)
wenzelm@4887
   326
wenzelm@4887
   327
fun trfun_names (Syntax {parse_ast_trtab, parse_trtab, print_trtab, print_ruletab, ...}) =
wenzelm@4887
   328
  (dest_trtab parse_ast_trtab, dest_trtab parse_trtab,
wenzelm@4887
   329
    dest_trtab print_trtab, dest_trtab print_ruletab);
wenzelm@4887
   330
wenzelm@4887
   331
clasohm@0
   332
wenzelm@237
   333
(** read **)
wenzelm@18
   334
wenzelm@237
   335
(* test_read *)
wenzelm@18
   336
wenzelm@237
   337
fun test_read (Syntax tabs) root str =
wenzelm@18
   338
  let
wenzelm@237
   339
    val {lexicon, gram, parse_ast_trtab, parse_ruletab, ...} = tabs;
wenzelm@18
   340
wenzelm@4703
   341
    val chars = Symbol.explode str;
wenzelm@2366
   342
    val toks = tokenize lexicon false chars;
clasohm@624
   343
    val _ = writeln ("tokens: " ^ space_implode " " (map display_token toks));
wenzelm@18
   344
wenzelm@383
   345
    fun show_pt pt =
wenzelm@383
   346
      let
wenzelm@383
   347
        val raw_ast = pt_to_ast (K None) pt;
wenzelm@383
   348
        val _ = writeln ("raw: " ^ str_of_ast raw_ast);
wenzelm@383
   349
        val pre_ast = pt_to_ast (lookup_trtab parse_ast_trtab) pt;
wenzelm@383
   350
        val _ = normalize true true (lookup_ruletab parse_ruletab) pre_ast;
wenzelm@383
   351
      in () end;
wenzelm@383
   352
  in
wenzelm@383
   353
    seq show_pt (parse gram root toks)
wenzelm@383
   354
  end;
wenzelm@18
   355
wenzelm@18
   356
wenzelm@260
   357
(* read_ast *)
wenzelm@260
   358
clasohm@882
   359
val ambiguity_level = ref 1;
clasohm@882
   360
clasohm@865
   361
fun read_asts (Syntax tabs) xids root str =
wenzelm@260
   362
  let
clasohm@764
   363
    val {lexicon, gram, parse_ast_trtab, logtypes, ...} = tabs;
clasohm@764
   364
    val root' = if root mem logtypes then logic else root;
wenzelm@4703
   365
    val chars = Symbol.explode str;
wenzelm@2366
   366
    val pts = parse gram root' (tokenize lexicon xids chars);
clasohm@330
   367
wenzelm@3739
   368
    fun show_pt pt =
wenzelm@3739
   369
      warning (Pretty.string_of (pretty_ast (pt_to_ast (K None) pt)));
wenzelm@260
   370
  in
wenzelm@888
   371
    if length pts > ! ambiguity_level then
berghofe@1580
   372
      (warning ("Ambiguous input " ^ quote str);
wenzelm@3526
   373
       warning "produces the following parse trees:";
clasohm@882
   374
       seq show_pt pts)
clasohm@624
   375
    else ();
clasohm@624
   376
    map (pt_to_ast (lookup_trtab parse_ast_trtab)) pts
wenzelm@260
   377
  end;
wenzelm@260
   378
wenzelm@260
   379
wenzelm@237
   380
(* read *)
clasohm@0
   381
wenzelm@237
   382
fun read (syn as Syntax tabs) ty str =
clasohm@0
   383
  let
wenzelm@237
   384
    val {parse_ruletab, parse_trtab, ...} = tabs;
clasohm@865
   385
    val asts = read_asts syn false (typ_to_nonterm ty) str;
clasohm@0
   386
  in
clasohm@624
   387
    map (ast_to_term (lookup_trtab parse_trtab))
clasohm@624
   388
      (map (normalize_ast (lookup_ruletab parse_ruletab)) asts)
clasohm@0
   389
  end;
clasohm@0
   390
clasohm@0
   391
wenzelm@237
   392
(* read types *)
clasohm@0
   393
wenzelm@3779
   394
fun read_typ syn get_sort str =
wenzelm@888
   395
  (case read syn typeT str of
wenzelm@3779
   396
    [t] => typ_of_term (get_sort (raw_term_sorts t)) t
wenzelm@2585
   397
  | _ => error "read_typ: ambiguous type syntax");
wenzelm@144
   398
wenzelm@2585
   399
fun simple_read_typ str =
wenzelm@2585
   400
  let fun get_sort env xi = if_none (assoc (env, xi)) [] in
wenzelm@3779
   401
    read_typ type_syn get_sort str
wenzelm@2585
   402
  end;
wenzelm@144
   403
wenzelm@144
   404
wenzelm@18
   405
wenzelm@1158
   406
(** prepare translation rules **)
wenzelm@1158
   407
wenzelm@1158
   408
datatype 'a trrule =
wenzelm@3526
   409
  ParseRule of 'a * 'a |
wenzelm@3526
   410
  PrintRule of 'a * 'a |
wenzelm@3526
   411
  ParsePrintRule of 'a * 'a;
wenzelm@888
   412
wenzelm@4618
   413
fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y)
wenzelm@4618
   414
  | map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y)
wenzelm@4618
   415
  | map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y);
wenzelm@1158
   416
wenzelm@3526
   417
fun parse_rule (ParseRule pats) = Some pats
wenzelm@3526
   418
  | parse_rule (PrintRule _) = None
wenzelm@3526
   419
  | parse_rule (ParsePrintRule pats) = Some pats;
wenzelm@1158
   420
wenzelm@3526
   421
fun print_rule (ParseRule _) = None
wenzelm@3526
   422
  | print_rule (PrintRule pats) = Some (swap pats)
wenzelm@3526
   423
  | print_rule (ParsePrintRule pats) = Some (swap pats);
wenzelm@1158
   424
wenzelm@1158
   425
wenzelm@1158
   426
fun check_rule (rule as (lhs, rhs)) =
wenzelm@1158
   427
  (case rule_error rule of
wenzelm@1158
   428
    Some msg =>
wenzelm@1158
   429
      error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
wenzelm@1158
   430
        str_of_ast lhs ^ "  ->  " ^ str_of_ast rhs)
wenzelm@1158
   431
  | None => rule);
wenzelm@888
   432
wenzelm@888
   433
wenzelm@888
   434
fun read_pattern syn (root, str) =
wenzelm@18
   435
  let
wenzelm@237
   436
    val Syntax {consts, ...} = syn;
wenzelm@18
   437
wenzelm@888
   438
    fun constify (ast as Constant _) = ast
wenzelm@888
   439
      | constify (ast as Variable x) =
wenzelm@3830
   440
          if x mem consts orelse NameSpace.qualified x then Constant x
wenzelm@3830
   441
          else ast
wenzelm@888
   442
      | constify (Appl asts) = Appl (map constify asts);
wenzelm@18
   443
  in
wenzelm@888
   444
    (case read_asts syn true root str of
wenzelm@888
   445
      [ast] => constify ast
wenzelm@888
   446
    | _ => error ("Syntactically ambiguous input: " ^ quote str))
wenzelm@888
   447
  end handle ERROR =>
wenzelm@888
   448
    error ("The error(s) above occurred in translation pattern " ^
wenzelm@888
   449
      quote str);
clasohm@0
   450
wenzelm@556
   451
wenzelm@1158
   452
fun prep_rules rd_pat raw_rules =
wenzelm@4618
   453
  let val rules = map (map_trrule rd_pat) raw_rules in
wenzelm@3526
   454
    (map check_rule (mapfilter parse_rule rules),
wenzelm@3526
   455
      map check_rule (mapfilter print_rule rules))
wenzelm@1158
   456
  end
wenzelm@18
   457
wenzelm@18
   458
wenzelm@18
   459
wenzelm@3779
   460
(** pretty terms, typs, sorts **)
wenzelm@18
   461
wenzelm@3779
   462
fun pretty_t t_to_ast prt_t (syn as Syntax tabs) curried t =
clasohm@0
   463
  let
wenzelm@2700
   464
    val {print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs;
wenzelm@237
   465
    val ast = t_to_ast (lookup_trtab print_trtab) t;
clasohm@0
   466
  in
wenzelm@2202
   467
    prt_t curried prtabs (lookup_trtab print_ast_trtab)
wenzelm@2700
   468
      (lookup_tokentr tokentrtab (! print_mode))
wenzelm@237
   469
      (normalize_ast (lookup_ruletab print_ruletab) ast)
clasohm@0
   470
  end;
clasohm@0
   471
clasohm@0
   472
val pretty_term = pretty_t term_to_ast pretty_term_ast;
wenzelm@3779
   473
fun pretty_typ syn = pretty_t typ_to_ast pretty_typ_ast syn false;
wenzelm@3779
   474
fun pretty_sort syn = pretty_t sort_to_ast pretty_typ_ast syn false;
clasohm@0
   475
wenzelm@3782
   476
val simple_str_of_sort = Pretty.str_of o pretty_sort type_syn;
wenzelm@3779
   477
val simple_string_of_typ = Pretty.string_of o (pretty_typ type_syn);
wenzelm@260
   478
val simple_pprint_typ = Pretty.pprint o Pretty.quote o (pretty_typ type_syn);
clasohm@0
   479
clasohm@0
   480
clasohm@0
   481
wenzelm@383
   482
(** extend syntax (external interfaces) **)
wenzelm@383
   483
wenzelm@2202
   484
fun ext_syntax mk_syn_ext prmode (syn as Syntax {logtypes, ...}) decls =
wenzelm@2202
   485
  extend_syntax prmode syn (mk_syn_ext logtypes decls);
wenzelm@383
   486
wenzelm@167
   487
clasohm@764
   488
fun extend_log_types syn logtypes =
wenzelm@2366
   489
  extend_syntax ("", true) syn (syn_ext_logtypes logtypes);
wenzelm@383
   490
wenzelm@2366
   491
val extend_type_gram = ext_syntax syn_ext_types ("", true);
wenzelm@383
   492
wenzelm@2202
   493
fun extend_const_gram syn prmode = ext_syntax syn_ext_consts prmode syn;
wenzelm@383
   494
wenzelm@2366
   495
val extend_consts = ext_syntax syn_ext_const_names ("", true);
wenzelm@383
   496
wenzelm@2366
   497
val extend_trfuns = ext_syntax syn_ext_trfuns ("", true);
wenzelm@383
   498
wenzelm@2383
   499
val extend_trfunsT = ext_syntax syn_ext_trfunsT ("", true);
wenzelm@2383
   500
wenzelm@2700
   501
val extend_tokentrfuns = ext_syntax syn_ext_tokentrfuns ("", true);
wenzelm@2700
   502
wenzelm@1158
   503
fun extend_trrules syn rules =
wenzelm@2366
   504
  ext_syntax syn_ext_rules ("", true) syn (prep_rules (read_pattern syn) rules);
wenzelm@1158
   505
wenzelm@1158
   506
fun extend_trrules_i syn rules =
wenzelm@2366
   507
  ext_syntax syn_ext_rules ("", true) syn (prep_rules I rules);
wenzelm@2366
   508
wenzelm@2366
   509
clasohm@0
   510
end;