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