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