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