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