src/Pure/Syntax/syntax.ML
author wenzelm
Thu, 03 Feb 1994 13:59:56 +0100
changeset 260 967813b8a7bf
parent 237 a7d3e712767a
child 330 2fda15dd1e0f
permissions -rw-r--r--
added simple_string_of_typ, simple_pprint_typ; various internal changes;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     1
(*  Title:      Pure/Syntax/syntax.ML
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     3
    Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     4
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
     5
Root of Isabelle's syntax module.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
     8
signature BASIC_SYNTAX =
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
     9
sig
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    10
  include AST0
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    11
  include SEXTENSION0
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    12
  include PRINTER0
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    13
end;
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    14
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    15
signature SYNTAX =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    16
sig
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    17
  include AST1
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
  include LEXICON0
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    19
  include SYN_EXT0
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    20
  include TYPE_EXT0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
  include SEXTENSION1
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
  include PRINTER0
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    23
  type syntax
144
0a0da273a6c5 added Syntax.read_typ;
wenzelm
parents: 47
diff changeset
    24
  val extend: syntax -> (string -> typ) -> string list * string list * sext -> syntax
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
    25
  val merge: string list -> syntax -> syntax -> syntax
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    26
  val type_syn: syntax
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    27
  val print_gram: syntax -> unit
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    28
  val print_trans: syntax -> unit
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    29
  val print_syntax: syntax -> unit
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    30
  val test_read: syntax -> string -> string -> unit
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    31
  val read: syntax -> typ -> string -> term
144
0a0da273a6c5 added Syntax.read_typ;
wenzelm
parents: 47
diff changeset
    32
  val read_typ: syntax -> (indexname -> sort) -> string -> typ
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    33
  val simple_read_typ: string -> typ
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    34
  val pretty_term: syntax -> term -> Pretty.T
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    35
  val pretty_typ: syntax -> typ -> Pretty.T
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    36
  val string_of_term: syntax -> term -> string
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
    37
  val string_of_typ: syntax -> typ -> string
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    38
  val simple_string_of_typ: typ -> string
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    39
  val simple_pprint_typ: typ -> pprint_args -> unit
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
functor SyntaxFun(structure Symtab: SYMTAB and TypeExt: TYPE_EXT
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    43
  and SExtension: SEXTENSION and Printer: PRINTER
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    44
  sharing SExtension.Parser.SynExt = TypeExt.SynExt = Printer.SynExt)(*: SYNTAX *) = (* FIXME *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    46
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    47
structure SynExt = TypeExt.SynExt;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    48
structure Parser = SExtension.Parser;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    49
structure Lexicon = Parser.Lexicon;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    50
open Lexicon SynExt SynExt.Ast Parser TypeExt SExtension Printer;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    51
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    52
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    53
(** tables of translation functions **)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    54
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    55
(*the ref serves as unique id*)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    56
type 'a trtab = (('a list -> 'a) * unit ref) Symtab.table;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    57
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    58
val dest_trtab = Symtab.alist_of;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    59
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    60
fun lookup_trtab tab c =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    61
  apsome fst (Symtab.lookup (tab, c));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    63
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    64
(* empty, extend, merge trtabs *)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    65
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    66
fun err_dup_trfun name c =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    67
  error ("More than one " ^ name ^ " for " ^ quote c);
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    68
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    69
val empty_trtab = Symtab.null;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    70
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    71
fun extend_trtab tab trfuns name =
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    72
  Symtab.extend (K false) (tab, map (fn (c, f) => (c, (f, ref ()))) trfuns)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    73
    handle Symtab.DUPLICATE c => err_dup_trfun name c;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    74
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    75
fun merge_trtabs tab1 tab2 name =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    76
  Symtab.merge eq_snd (tab1, tab2)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    77
    handle Symtab.DUPLICATE c => err_dup_trfun name c;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    78
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    79
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    80
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    81
(** tables of translation rules **)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    82
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    83
type ruletab = (ast * ast) list Symtab.table;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    84
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    85
fun dest_ruletab tab = flat (map snd (Symtab.alist_of tab));
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    86
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    87
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    88
(* lookup_ruletab *)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    89
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    90
fun lookup_ruletab tab =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    91
  if Symtab.is_null tab then None
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    92
  else Some (fn a => Symtab.lookup_multi (tab, a));
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    93
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    94
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    95
(* empty, extend, merge ruletabs *)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    96
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    97
val empty_ruletab = Symtab.null;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    98
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    99
fun extend_ruletab tab rules =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   100
  generic_extend (op =) Symtab.dest_multi Symtab.make_multi tab
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   101
    (map (fn r => (head_of_rule r, r)) (distinct rules));
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   102
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   103
fun merge_ruletabs tab1 tab2 =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   104
  generic_merge (op =) Symtab.dest_multi Symtab.make_multi tab1 tab2;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   105
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   106
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   107
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   108
(** datatype syntax **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   110
datatype syntax =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   111
  Syntax of {
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   112
    lexicon: lexicon,
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   113
    roots: string list,
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   114
    gram: gram,
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   115
    consts: string list,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   116
    parse_ast_trtab: ast trtab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   117
    parse_ruletab: ruletab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   118
    parse_trtab: term trtab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   119
    print_trtab: term trtab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   120
    print_ruletab: ruletab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   121
    print_ast_trtab: ast trtab,
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   122
    prtab: prtab};
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   124
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   125
(* empty_syntax *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   126
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   127
val empty_syntax =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   128
  Syntax {
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   129
    lexicon = empty_lexicon,
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   130
    roots = [],
171
ab0f93a291b5 *** empty log message ***
wenzelm
parents: 168
diff changeset
   131
    gram = empty_gram,
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   132
    consts = [],
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   133
    parse_ast_trtab = empty_trtab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   134
    parse_ruletab = empty_ruletab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   135
    parse_trtab = empty_trtab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   136
    print_trtab = empty_trtab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   137
    print_ruletab = empty_ruletab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   138
    print_ast_trtab = empty_trtab,
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   139
    prtab = empty_prtab};
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   140
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   141
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   142
(* extend_syntax *)
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   143
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   144
fun extend_syntax (Syntax tabs) syn_ext =
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   145
  let
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   146
    val {lexicon, roots = roots1, gram, consts = consts1, parse_ast_trtab,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   147
      parse_ruletab, parse_trtab, print_trtab, print_ruletab, print_ast_trtab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   148
      prtab} = tabs;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   149
    val SynExt {roots = roots2, xprods, consts = consts2, parse_ast_translation,
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   150
      parse_rules, parse_translation, print_translation, print_rules,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   151
      print_ast_translation} = syn_ext;
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   152
  in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   153
    Syntax {
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   154
      lexicon = extend_lexicon lexicon (delims_of xprods),
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   155
      roots = extend_list roots1 roots2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   156
      gram = extend_gram gram roots2 xprods,
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   157
      consts = consts2 union consts1,
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   158
      parse_ast_trtab =
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   159
        extend_trtab parse_ast_trtab parse_ast_translation "parse ast translation",
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   160
      parse_ruletab = extend_ruletab parse_ruletab parse_rules,
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   161
      parse_trtab = extend_trtab parse_trtab parse_translation "parse translation",
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   162
      print_trtab = extend_trtab print_trtab print_translation "print translation",
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   163
      print_ruletab = extend_ruletab print_ruletab print_rules,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   164
      print_ast_trtab =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   165
        extend_trtab print_ast_trtab print_ast_translation "print ast translation",
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   166
      prtab = extend_prtab prtab xprods}
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   167
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   168
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   169
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   170
(* merge_syntaxes *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   171
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   172
fun merge_syntaxes (Syntax tabs1) (Syntax tabs2) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   173
  let
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   174
    val {lexicon = lexicon1, roots = roots1, gram = gram1, consts = consts1,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   175
      parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   176
      parse_trtab = parse_trtab1, print_trtab = print_trtab1,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   177
      print_ruletab = print_ruletab1, print_ast_trtab = print_ast_trtab1,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   178
      prtab = prtab1} = tabs1;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   179
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   180
    val {lexicon = lexicon2, roots = roots2, gram = gram2, consts = consts2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   181
      parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   182
      parse_trtab = parse_trtab2, print_trtab = print_trtab2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   183
      print_ruletab = print_ruletab2, print_ast_trtab = print_ast_trtab2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   184
      prtab = prtab2} = tabs2;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   185
  in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   186
    Syntax {
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   187
      lexicon = merge_lexicons lexicon1 lexicon2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   188
      roots = merge_lists roots1 roots2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   189
      gram = merge_grams gram1 gram2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   190
      consts = merge_lists consts1 consts2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   191
      parse_ast_trtab =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   192
        merge_trtabs parse_ast_trtab1 parse_ast_trtab2 "parse ast translation",
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   193
      parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   194
      parse_trtab = merge_trtabs parse_trtab1 parse_trtab2 "parse translation",
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   195
      print_trtab = merge_trtabs print_trtab1 print_trtab2 "print translation",
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   196
      print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   197
      print_ast_trtab =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   198
        merge_trtabs print_ast_trtab1 print_ast_trtab2 "print ast translation",
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   199
      prtab = merge_prtabs prtab1 prtab2}
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   200
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   201
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   202
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   203
(* type_syn *)
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   204
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   205
val type_syn = extend_syntax empty_syntax type_ext;
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   206
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   207
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   208
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   209
(** inspect syntax **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   210
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   211
fun pretty_strs_qs name strs =
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   212
  Pretty.strs (name :: map quote (sort_strings strs));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   213
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   214
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   215
(* print_gram *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   216
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   217
fun print_gram (Syntax tabs) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   218
  let
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   219
    val {lexicon, roots, gram, ...} = tabs;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   220
  in
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   221
    Pretty.writeln (pretty_strs_qs "lexicon:" (dest_lexicon lexicon));
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   222
    Pretty.writeln (Pretty.strs ("roots:" :: roots));
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   223
    Pretty.writeln (Pretty.big_list "prods:" (pretty_gram gram))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   224
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   225
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   226
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   227
(* print_trans *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   228
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   229
fun print_trans (Syntax tabs) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   230
  let
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   231
    fun pretty_trtab name tab =
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   232
      pretty_strs_qs name (map fst (dest_trtab tab));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   233
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   234
    fun pretty_ruletab name tab =
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   235
      Pretty.big_list name (map pretty_rule (dest_ruletab tab));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   236
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   237
    val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   238
      print_ruletab, print_ast_trtab, ...} = tabs;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   239
  in
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   240
    Pretty.writeln (pretty_strs_qs "consts:" consts);
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   241
    Pretty.writeln (pretty_trtab "parse_ast_translation:" parse_ast_trtab);
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   242
    Pretty.writeln (pretty_ruletab "parse_rules:" parse_ruletab);
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   243
    Pretty.writeln (pretty_trtab "parse_translation:" parse_trtab);
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   244
    Pretty.writeln (pretty_trtab "print_translation:" print_trtab);
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   245
    Pretty.writeln (pretty_ruletab "print_rules:" print_ruletab);
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   246
    Pretty.writeln (pretty_trtab "print_ast_translation:" print_ast_trtab)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   247
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   248
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   249
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   250
(* print_syntax *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   251
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   252
fun print_syntax syn = (print_gram syn; print_trans syn);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   253
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   254
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   255
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   256
(** read **)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   257
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   258
(* test_read *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   259
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   260
fun test_read (Syntax tabs) root str =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   261
  let
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   262
    val {lexicon, gram, parse_ast_trtab, parse_ruletab, ...} = tabs;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   263
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   264
    val toks = tokenize lexicon false str;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   265
    val _ = writeln ("tokens: " ^ space_implode " " (map display_token toks));
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   266
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   267
    val pt = parse gram root toks;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   268
    val raw_ast = pt_to_ast (K None) pt;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   269
    val _ = writeln ("raw: " ^ str_of_ast raw_ast);
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   270
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   271
    val pre_ast = pt_to_ast (lookup_trtab parse_ast_trtab) pt;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   272
    val _ = normalize true true (lookup_ruletab parse_ruletab) pre_ast;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   273
  in () end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   274
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   275
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   276
(* read_ast *)
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   277
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   278
fun read_ast (Syntax tabs) xids root str =
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   279
  let
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   280
    val {lexicon, gram, parse_ast_trtab, ...} = tabs;
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   281
  in
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   282
    pt_to_ast (lookup_trtab parse_ast_trtab)
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   283
      (parse gram root (tokenize lexicon xids str))
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   284
  end;
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   285
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   286
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   287
(* read *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   288
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   289
fun read (syn as Syntax tabs) ty str =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   290
  let
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   291
    val {parse_ruletab, parse_trtab, ...} = tabs;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   292
    val ast = read_ast syn false (typ_to_nonterm ty) str;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   293
  in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   294
    ast_to_term (lookup_trtab parse_trtab)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   295
      (normalize_ast (lookup_ruletab parse_ruletab) ast)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   296
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   297
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   298
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   299
(* read types *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   300
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   301
fun read_typ syn def_sort str =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   302
  typ_of_term def_sort (read syn typeT str);
144
0a0da273a6c5 added Syntax.read_typ;
wenzelm
parents: 47
diff changeset
   303
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   304
fun simple_read_typ str = read_typ type_syn (K []) str;
144
0a0da273a6c5 added Syntax.read_typ;
wenzelm
parents: 47
diff changeset
   305
0a0da273a6c5 added Syntax.read_typ;
wenzelm
parents: 47
diff changeset
   306
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   307
(* read rules *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   308
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   309
fun read_rule syn (xrule as ((_, lhs_src), (_, rhs_src))) =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   310
  let
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   311
    val Syntax {consts, ...} = syn;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   312
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   313
    fun constantify (ast as Constant _) = ast
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   314
      | constantify (ast as Variable x) =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   315
          if x mem consts then Constant x else ast
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   316
      | constantify (Appl asts) = Appl (map constantify asts);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   317
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   318
    fun read_pat (root, str) =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   319
      constantify (read_ast syn true root str)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   320
        handle ERROR => error ("The error above occurred in " ^ quote str);
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   321
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   322
    val rule as (lhs, rhs) = (pairself read_pat) xrule;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   323
  in
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   324
    (case rule_error rule of
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   325
      Some msg =>
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   326
        error ("Error in syntax translation rule: " ^ msg ^
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   327
          "\nexternal: " ^ quote lhs_src ^ "  ->  " ^ quote rhs_src ^
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   328
          "\ninternal: " ^ str_of_ast lhs ^ "  ->  " ^ str_of_ast rhs)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   329
    | None => rule)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   330
  end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   331
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   332
fun read_xrules syn xrules =
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   333
  let
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   334
    fun right_rule (xpat1 |-> xpat2) = Some (xpat1, xpat2)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   335
      | right_rule (xpat1 <-| xpat2) = None
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   336
      | right_rule (xpat1 <-> xpat2) = Some (xpat1, xpat2);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   337
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   338
    fun left_rule (xpat1 |-> xpat2) = None
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   339
      | left_rule (xpat1 <-| xpat2) = Some (xpat2, xpat1)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   340
      | left_rule (xpat1 <-> xpat2) = Some (xpat2, xpat1);
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   341
  in
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   342
    (map (read_rule syn) (mapfilter right_rule xrules),
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   343
     map (read_rule syn) (mapfilter left_rule xrules))
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   344
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   345
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   346
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   347
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   348
(** pretty terms or typs **)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   349
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   350
fun pretty_t t_to_ast pretty_t (syn as Syntax tabs) t =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   351
  let
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   352
    val {print_trtab, print_ruletab, print_ast_trtab, prtab, ...} = tabs;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   353
    val ast = t_to_ast (lookup_trtab print_trtab) t;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   354
  in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   355
    pretty_t prtab (lookup_trtab print_ast_trtab)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   356
      (normalize_ast (lookup_ruletab print_ruletab) ast)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   357
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   358
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   359
val pretty_term = pretty_t term_to_ast pretty_term_ast;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   360
val pretty_typ = pretty_t typ_to_ast pretty_typ_ast;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   361
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   362
fun string_of_term syn t = Pretty.string_of (pretty_term syn t);
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   363
fun string_of_typ syn ty = Pretty.string_of (pretty_typ syn ty);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   364
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   365
val simple_string_of_typ = string_of_typ type_syn;
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   366
val simple_pprint_typ = Pretty.pprint o Pretty.quote o (pretty_typ type_syn);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   367
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   368
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   369
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   370
(** build syntax **)
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   371
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   372
(* extend *)
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   373
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   374
fun extend syn0 read_ty (all_roots, xconsts, sext) =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   375
  let
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   376
    val Syntax {roots, ...} = syn0;
174
319ff2d6760b *** empty log message ***
wenzelm
parents: 171
diff changeset
   377
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   378
    val syn1 = extend_syntax syn0
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   379
      (syn_ext_of_sext (all_roots \\ roots) xconsts read_ty sext);
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   380
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   381
    val syn2 = extend_syntax syn1
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   382
      (syn_ext_rules (read_xrules syn1 (xrules_of sext)));
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   383
  in syn2 end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   384
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   385
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   386
(* merge *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   387
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   388
fun merge all_roots syn1 syn2 =
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   389
  let
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   390
    val syn as (Syntax {roots, ...}) = merge_syntaxes syn1 syn2;
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   391
  in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   392
    (case all_roots \\ roots of
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   393
      [] => syn
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   394
    | new_roots => (writeln ("DEBUG new roots:" ^ commas new_roots); (* FIXME debug *)
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   395
        extend_syntax syn (syn_ext_roots new_roots)))
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   396
  end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   397
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   398
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   399
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   400