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