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