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