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