src/Pure/Syntax/syntax.ML
author wenzelm
Fri, 27 Jan 1995 13:35:29 +0100
changeset 888 3a1de9454d13
parent 882 b118d1ea0dfd
child 922 196ca0973a6d
permissions -rw-r--r--
improved read_xrules: patterns no longer read twice; tuned read_typ;
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
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
     8
infix |-> <-| <->;
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
     9
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    10
signature BASIC_SYNTAX =
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    11
sig
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    12
  include AST0
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    13
  include SYN_TRANS0
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    14
  include MIXFIX0
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    15
  include PRINTER0
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
    16
  type xrule
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
    17
  val |-> : (string * string) * (string * string) -> xrule
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
    18
  val <-| : (string * string) * (string * string) -> xrule
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
    19
  val <-> : (string * string) * (string * string) -> xrule
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    20
end;
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    21
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
signature SYNTAX =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    23
sig
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
    24
  include AST1
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
  include LEXICON0
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    26
  include SYN_EXT0
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    27
  include TYPE_EXT0
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    28
  include SYN_TRANS1
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    29
  include MIXFIX1
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    30
  include PRINTER0
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
    31
  sharing type ast = Parser.SynExt.Ast.ast
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
    32
  type xrule
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
    33
  val |-> : (string * string) * (string * string) -> xrule
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
    34
  val <-| : (string * string) * (string * string) -> xrule
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
    35
  val <-> : (string * string) * (string * string) -> xrule
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    36
  type syntax
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    37
  val extend_log_types: syntax -> string list -> syntax
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    38
  val extend_type_gram: syntax -> (string * int * mixfix) list -> syntax
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    39
  val extend_const_gram: syntax -> (string * typ * mixfix) list -> syntax
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    40
  val extend_consts: syntax -> string list -> syntax
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    41
  val extend_trfuns: syntax ->
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    42
    (string * (ast list -> ast)) list *
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    43
    (string * (term list -> term)) list *
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    44
    (string * (term list -> term)) list *
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    45
    (string * (ast list -> ast)) list -> syntax
865
b38c67991122 added optional precedence for body of binder;
clasohm
parents: 764
diff changeset
    46
  val extend_trrules: syntax -> xrule list -> syntax
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    47
  val merge_syntaxes: syntax -> syntax -> syntax
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    48
  val type_syn: syntax
764
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
    49
  val pure_syn: syntax
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    50
  val print_gram: syntax -> unit
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    51
  val print_trans: syntax -> unit
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    52
  val print_syntax: syntax -> unit
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    53
  val test_read: syntax -> string -> string -> unit
624
33b9b5da3e6f made major changes to grammar;
clasohm
parents: 556
diff changeset
    54
  val read: syntax -> typ -> string -> term list
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    55
  val read_typ: syntax -> (indexname -> sort) -> string -> typ
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    56
  val simple_read_typ: string -> typ
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    57
  val pretty_term: syntax -> term -> Pretty.T
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    58
  val pretty_typ: syntax -> typ -> Pretty.T
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    59
  val string_of_term: syntax -> term -> string
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    60
  val string_of_typ: syntax -> typ -> string
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    61
  val simple_string_of_typ: typ -> string
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    62
  val simple_pprint_typ: typ -> pprint_args -> unit
882
b118d1ea0dfd moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents: 865
diff changeset
    63
  val ambiguity_level: int ref
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
functor SyntaxFun(structure Symtab: SYMTAB and TypeExt: TYPE_EXT
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    67
  and SynTrans: SYN_TRANS and Mixfix: MIXFIX and Printer: PRINTER
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    68
  sharing Mixfix.SynExt = SynTrans.Parser.SynExt = TypeExt.SynExt = Printer.SynExt)
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
    69
  : SYNTAX =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    71
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    72
structure SynExt = TypeExt.SynExt;
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    73
structure Parser = SynTrans.Parser;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    74
structure Lexicon = Parser.Lexicon;
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
    75
open Lexicon SynExt SynExt.Ast Parser TypeExt SynTrans Mixfix Printer;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    76
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    77
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    78
(** tables of translation functions **)
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
(*the ref serves as unique id*)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    81
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
    82
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
    83
val dest_trtab = Symtab.dest;
237
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 lookup_trtab tab c =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    86
  apsome fst (Symtab.lookup (tab, c));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    87
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    89
(* empty, extend, merge trtabs *)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    90
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
    91
fun err_dup_trfuns name cs =
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
    92
  error ("More than one " ^ name ^ " for " ^ commas_quote cs);
237
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
val empty_trtab = Symtab.null;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    95
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    96
fun extend_trtab tab trfuns name =
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
    97
  Symtab.extend_new (tab, map (fn (c, f) => (c, (f, ref ()))) trfuns)
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
    98
    handle Symtab.DUPS cs => err_dup_trfuns name cs;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
    99
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   100
fun merge_trtabs tab1 tab2 name =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   101
  Symtab.merge eq_snd (tab1, tab2)
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   102
    handle Symtab.DUPS cs => err_dup_trfuns name cs;
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   103
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   104
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   105
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   106
(** tables of translation rules **)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   107
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   108
type ruletab = (ast * ast) list Symtab.table;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   109
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   110
fun dest_ruletab tab = flat (map snd (Symtab.dest tab));
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   111
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   112
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   113
(* lookup_ruletab *)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   114
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   115
fun lookup_ruletab tab =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   116
  if Symtab.is_null tab then None
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   117
  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
   118
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   119
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   120
(* empty, extend, merge ruletabs *)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   121
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   122
val empty_ruletab = Symtab.null;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   123
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   124
fun extend_ruletab tab rules =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   125
  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
   126
    (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
   127
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   128
fun merge_ruletabs tab1 tab2 =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   129
  generic_merge (op =) Symtab.dest_multi Symtab.make_multi tab1 tab2;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   131
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   132
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
(** datatype syntax **)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   134
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   135
datatype syntax =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   136
  Syntax of {
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   137
    lexicon: lexicon,
764
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   138
    logtypes: string list,
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   139
    gram: gram,
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   140
    consts: string list,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   141
    parse_ast_trtab: ast trtab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   142
    parse_ruletab: ruletab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   143
    parse_trtab: term trtab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   144
    print_trtab: term trtab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   145
    print_ruletab: ruletab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   146
    print_ast_trtab: ast trtab,
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   147
    prtab: prtab};
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   148
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   149
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   150
(* empty_syntax *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   151
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   152
val empty_syntax =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   153
  Syntax {
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   154
    lexicon = empty_lexicon,
764
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   155
    logtypes = [],
171
ab0f93a291b5 *** empty log message ***
wenzelm
parents: 168
diff changeset
   156
    gram = empty_gram,
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   157
    consts = [],
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   158
    parse_ast_trtab = empty_trtab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   159
    parse_ruletab = empty_ruletab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   160
    parse_trtab = empty_trtab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   161
    print_trtab = empty_trtab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   162
    print_ruletab = empty_ruletab,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   163
    print_ast_trtab = empty_trtab,
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   164
    prtab = empty_prtab};
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   165
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   166
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   167
(* extend_syntax *)
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   168
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   169
fun extend_syntax (Syntax tabs) syn_ext =
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   170
  let
764
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   171
    val {lexicon, logtypes = logtypes1, gram, consts = consts1, parse_ast_trtab,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   172
      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
   173
      prtab} = tabs;
764
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   174
    val SynExt {logtypes = logtypes2, xprods, consts = consts2, parse_ast_translation,
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   175
      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
   176
      print_ast_translation} = syn_ext;
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   177
  in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   178
    Syntax {
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   179
      lexicon = extend_lexicon lexicon (delims_of xprods),
764
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   180
      logtypes = extend_list logtypes1 logtypes2,
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   181
      gram = extend_gram gram (logtypes1 @ logtypes2) xprods,
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   182
      consts = consts2 union consts1,
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   183
      parse_ast_trtab =
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   184
        extend_trtab parse_ast_trtab parse_ast_translation "parse ast translation",
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   185
      parse_ruletab = extend_ruletab parse_ruletab parse_rules,
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   186
      parse_trtab = extend_trtab parse_trtab parse_translation "parse translation",
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   187
      print_trtab = extend_trtab print_trtab print_translation "print translation",
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   188
      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
   189
      print_ast_trtab =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   190
        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
   191
      prtab = extend_prtab prtab xprods}
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   192
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   193
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   194
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   195
(* merge_syntaxes *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   196
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   197
fun merge_syntaxes (Syntax tabs1) (Syntax tabs2) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   198
  let
764
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   199
    val {lexicon = lexicon1, logtypes = logtypes1, gram = gram1, consts = consts1,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   200
      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
   201
      parse_trtab = parse_trtab1, print_trtab = print_trtab1,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   202
      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
   203
      prtab = prtab1} = tabs1;
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   204
764
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   205
    val {lexicon = lexicon2, logtypes = logtypes2, gram = gram2, consts = consts2,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   206
      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
   207
      parse_trtab = parse_trtab2, print_trtab = print_trtab2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   208
      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
   209
      prtab = prtab2} = tabs2;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   210
  in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   211
    Syntax {
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   212
      lexicon = merge_lexicons lexicon1 lexicon2,
764
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   213
      logtypes = merge_lists logtypes1 logtypes2,
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   214
      gram = merge_grams gram1 gram2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   215
      consts = merge_lists consts1 consts2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   216
      parse_ast_trtab =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   217
        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
   218
      parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   219
      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
   220
      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
   221
      print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   222
      print_ast_trtab =
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   223
        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
   224
      prtab = merge_prtabs prtab1 prtab2}
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   225
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   226
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   227
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   228
(* type_syn *)
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   229
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   230
val type_syn = extend_syntax empty_syntax type_ext;
764
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   231
val pure_syn = extend_syntax type_syn pure_ext;
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   232
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   233
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   234
(** inspect syntax **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   235
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   236
fun pretty_strs_qs name strs =
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   237
  Pretty.strs (name :: map quote (sort_strings strs));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   238
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   239
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   240
(* print_gram *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   241
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   242
fun print_gram (Syntax tabs) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   243
  let
764
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   244
    val {lexicon, logtypes, gram, ...} = tabs;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   245
  in
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   246
    Pretty.writeln (pretty_strs_qs "lexicon:" (dest_lexicon lexicon));
764
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   247
    Pretty.writeln (Pretty.strs ("logtypes:" :: logtypes));
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   248
    Pretty.writeln (Pretty.big_list "prods:" (pretty_gram gram))
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   249
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   250
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   251
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   252
(* print_trans *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   253
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   254
fun print_trans (Syntax tabs) =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   255
  let
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   256
    fun pretty_trtab name tab =
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   257
      pretty_strs_qs name (map fst (dest_trtab tab));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   258
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   259
    fun pretty_ruletab name tab =
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   260
      Pretty.big_list name (map pretty_rule (dest_ruletab tab));
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   261
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   262
    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
   263
      print_ruletab, print_ast_trtab, ...} = tabs;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   264
  in
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   265
    Pretty.writeln (pretty_strs_qs "consts:" consts);
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   266
    Pretty.writeln (pretty_trtab "parse_ast_translation:" parse_ast_trtab);
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   267
    Pretty.writeln (pretty_ruletab "parse_rules:" parse_ruletab);
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   268
    Pretty.writeln (pretty_trtab "parse_translation:" parse_trtab);
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   269
    Pretty.writeln (pretty_trtab "print_translation:" print_trtab);
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   270
    Pretty.writeln (pretty_ruletab "print_rules:" print_ruletab);
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   271
    Pretty.writeln (pretty_trtab "print_ast_translation:" print_ast_trtab)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   272
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   273
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   274
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   275
(* print_syntax *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   276
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   277
fun print_syntax syn = (print_gram syn; print_trans syn);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   278
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   279
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   280
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   281
(** read **)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   282
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   283
(* test_read *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   284
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   285
fun test_read (Syntax tabs) root str =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   286
  let
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   287
    val {lexicon, gram, parse_ast_trtab, parse_ruletab, ...} = tabs;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   288
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   289
    val toks = tokenize lexicon false str;
624
33b9b5da3e6f made major changes to grammar;
clasohm
parents: 556
diff changeset
   290
    val _ = writeln ("tokens: " ^ space_implode " " (map display_token toks));
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   291
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   292
    fun show_pt pt =
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   293
      let
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   294
        val raw_ast = pt_to_ast (K None) pt;
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   295
        val _ = writeln ("raw: " ^ str_of_ast raw_ast);
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   296
        val pre_ast = pt_to_ast (lookup_trtab parse_ast_trtab) pt;
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   297
        val _ = normalize true true (lookup_ruletab parse_ruletab) pre_ast;
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   298
      in () end;
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   299
  in
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   300
    seq show_pt (parse gram root toks)
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   301
  end;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   302
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   303
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   304
(* read_ast *)
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   305
882
b118d1ea0dfd moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents: 865
diff changeset
   306
val ambiguity_level = ref 1;
b118d1ea0dfd moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents: 865
diff changeset
   307
865
b38c67991122 added optional precedence for body of binder;
clasohm
parents: 764
diff changeset
   308
fun read_asts (Syntax tabs) xids root str =
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   309
  let
764
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   310
    val {lexicon, gram, parse_ast_trtab, logtypes, ...} = tabs;
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   311
    val root' = if root mem logtypes then logic else root;
624
33b9b5da3e6f made major changes to grammar;
clasohm
parents: 556
diff changeset
   312
    val pts = parse gram root' (tokenize lexicon xids str);
330
2fda15dd1e0f changed the way a grammar is generated to allow the new parser to work;
clasohm
parents: 260
diff changeset
   313
624
33b9b5da3e6f made major changes to grammar;
clasohm
parents: 556
diff changeset
   314
    fun show_pt pt = writeln (str_of_ast (pt_to_ast (K None) pt));
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   315
  in
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   316
    if length pts > ! ambiguity_level then
624
33b9b5da3e6f made major changes to grammar;
clasohm
parents: 556
diff changeset
   317
      (writeln ("Warning: Ambiguous input " ^ quote str);
882
b118d1ea0dfd moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents: 865
diff changeset
   318
       writeln "produces the following parse trees:";
b118d1ea0dfd moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents: 865
diff changeset
   319
       seq show_pt pts)
624
33b9b5da3e6f made major changes to grammar;
clasohm
parents: 556
diff changeset
   320
    else ();
33b9b5da3e6f made major changes to grammar;
clasohm
parents: 556
diff changeset
   321
    map (pt_to_ast (lookup_trtab parse_ast_trtab)) pts
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   322
  end;
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   323
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   324
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   325
(* read *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   326
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   327
fun read (syn as Syntax tabs) ty str =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   328
  let
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   329
    val {parse_ruletab, parse_trtab, ...} = tabs;
865
b38c67991122 added optional precedence for body of binder;
clasohm
parents: 764
diff changeset
   330
    val asts = read_asts syn false (typ_to_nonterm ty) str;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   331
  in
624
33b9b5da3e6f made major changes to grammar;
clasohm
parents: 556
diff changeset
   332
    map (ast_to_term (lookup_trtab parse_trtab))
33b9b5da3e6f made major changes to grammar;
clasohm
parents: 556
diff changeset
   333
      (map (normalize_ast (lookup_ruletab parse_ruletab)) asts)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   334
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   335
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   336
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   337
(* read types *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   338
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   339
fun read_typ syn def_sort str =
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   340
  (case read syn typeT str of
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   341
    [t] => typ_of_term (raw_term_sorts t) def_sort t
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   342
  | _ => sys_error "read_typ: ambiguous type syntax");
144
0a0da273a6c5 added Syntax.read_typ;
wenzelm
parents: 47
diff changeset
   343
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   344
fun simple_read_typ str = read_typ type_syn (K []) str;
144
0a0da273a6c5 added Syntax.read_typ;
wenzelm
parents: 47
diff changeset
   345
0a0da273a6c5 added Syntax.read_typ;
wenzelm
parents: 47
diff changeset
   346
865
b38c67991122 added optional precedence for body of binder;
clasohm
parents: 764
diff changeset
   347
(* read translation rules *)
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   348
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   349
datatype 'a rule =
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   350
  op |-> of 'a * 'a |
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   351
  op <-| of 'a * 'a |
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   352
  op <-> of 'a * 'a;
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   353
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   354
type xrule = (string * string) rule;
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   355
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   356
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   357
fun read_pattern syn (root, str) =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   358
  let
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   359
    val Syntax {consts, ...} = syn;
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   360
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   361
    fun constify (ast as Constant _) = ast
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   362
      | constify (ast as Variable x) =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   363
          if x mem consts then Constant x else ast
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   364
      | constify (Appl asts) = Appl (map constify asts);
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   365
  in
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   366
    (case read_asts syn true root str of
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   367
      [ast] => constify ast
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   368
    | _ => error ("Syntactically ambiguous input: " ^ quote str))
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   369
  end handle ERROR =>
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   370
    error ("The error(s) above occurred in translation pattern " ^
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   371
      quote str);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   372
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
   373
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   374
fun check_rule (rule as (lhs, rhs)) =
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   375
  (case rule_error rule of
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   376
    Some msg =>
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   377
      error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   378
        str_of_ast lhs ^ "  ->  " ^ str_of_ast rhs)
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   379
  | None => rule);
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   380
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
   381
865
b38c67991122 added optional precedence for body of binder;
clasohm
parents: 764
diff changeset
   382
fun read_xrules syn xrules =
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   383
  let
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   384
    fun map_rule f (x |-> y) = (f x |-> f y)
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   385
      | map_rule f (x <-| y) = (f x <-| f y)
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   386
      | map_rule f (x <-> y) = (f x <-> f y);
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   387
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   388
    fun right_rule (xpat1 |-> xpat2) = Some (xpat1, xpat2)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   389
      | right_rule (xpat1 <-| xpat2) = None
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   390
      | right_rule (xpat1 <-> xpat2) = Some (xpat1, xpat2);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   391
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   392
    fun left_rule (xpat1 |-> xpat2) = None
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   393
      | left_rule (xpat1 <-| xpat2) = Some (xpat2, xpat1)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   394
      | left_rule (xpat1 <-> xpat2) = Some (xpat2, xpat1);
888
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   395
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   396
    val rules = map (map_rule (read_pattern syn)) xrules;
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   397
  in
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   398
    (map check_rule (mapfilter right_rule rules),
3a1de9454d13 improved read_xrules: patterns no longer read twice;
wenzelm
parents: 882
diff changeset
   399
      map check_rule (mapfilter left_rule rules))
18
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   400
  end;
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   401
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   402
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   403
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   404
(** pretty terms or typs **)
c9ec452ff08f lots of internal cleaning and tuning;
wenzelm
parents: 0
diff changeset
   405
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   406
fun pretty_t t_to_ast pretty_t (syn as Syntax tabs) t =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   407
  let
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   408
    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
   409
    val ast = t_to_ast (lookup_trtab print_trtab) t;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   410
  in
237
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   411
    pretty_t prtab (lookup_trtab print_ast_trtab)
a7d3e712767a MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents: 175
diff changeset
   412
      (normalize_ast (lookup_ruletab print_ruletab) ast)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   413
  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   414
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   415
val pretty_term = pretty_t term_to_ast pretty_term_ast;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   416
val pretty_typ = pretty_t typ_to_ast pretty_typ_ast;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   417
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   418
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
   419
fun string_of_typ syn ty = Pretty.string_of (pretty_typ syn ty);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   420
260
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   421
val simple_string_of_typ = string_of_typ type_syn;
967813b8a7bf added simple_string_of_typ, simple_pprint_typ;
wenzelm
parents: 237
diff changeset
   422
val simple_pprint_typ = Pretty.pprint o Pretty.quote o (pretty_typ type_syn);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   423
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   424
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   425
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   426
(** extend syntax (external interfaces) **)
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   427
764
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   428
fun ext_syntax mk_syn_ext (syn as Syntax {logtypes, ...}) decls =
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   429
  extend_syntax syn (mk_syn_ext logtypes decls);
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   430
167
128e122acc89 added (partial) extend_tables;
wenzelm
parents: 144
diff changeset
   431
764
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   432
fun extend_log_types syn logtypes =
b60e77395d1a changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents: 624
diff changeset
   433
  extend_syntax syn (syn_ext_logtypes logtypes);
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   434
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
   435
val extend_type_gram = ext_syntax syn_ext_types;
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   436
556
3f5f42467717 added type xrule (from sextension.ML);
wenzelm
parents: 383
diff changeset
   437
val extend_const_gram = ext_syntax syn_ext_consts;
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   438
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   439
val extend_consts = ext_syntax syn_ext_const_names;
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   440
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   441
val extend_trfuns = ext_syntax syn_ext_trfuns;
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   442
865
b38c67991122 added optional precedence for body of binder;
clasohm
parents: 764
diff changeset
   443
fun extend_trrules syn xrules =
b38c67991122 added optional precedence for body of binder;
clasohm
parents: 764
diff changeset
   444
  ext_syntax syn_ext_rules syn (read_xrules syn xrules);
383
fcea89074e4c added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents: 330
diff changeset
   445
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   446
end;