| author | wenzelm | 
| Wed, 29 Jun 2005 15:13:38 +0200 | |
| changeset 16608 | 4f8d7b83c7e2 | 
| parent 15973 | 5fd94d84470f | 
| child 16610 | 58bf09036a6d | 
| permissions | -rw-r--r-- | 
| 240 | 1 | (* Title: Pure/Syntax/syn_ext.ML | 
| 2 | ID: $Id$ | |
| 911 
55754d6d399c
new in mixfix annotations: "' " (quote space) separates delimiters without
 wenzelm parents: 
865diff
changeset | 3 | Author: Markus Wenzel and Carsten Clasohm, TU Muenchen | 
| 240 | 4 | |
| 5 | Syntax extension (internal interface). | |
| 6 | *) | |
| 7 | ||
| 8 | signature SYN_EXT0 = | |
| 4050 | 9 | sig | 
| 6760 | 10 | val dddot_indexname: indexname | 
| 11 | val constrainC: string | |
| 240 | 12 | val typeT: typ | 
| 5690 | 13 | val max_pri: int | 
| 15754 | 14 |   val stamp_trfun: stamp -> string * 'a -> string * ('a * stamp)
 | 
| 15 |   val mk_trfun: string * 'a -> string * ('a * stamp)
 | |
| 16 |   val eq_trfun: ('a * stamp) * ('a * stamp) -> bool
 | |
| 15835 
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
 wenzelm parents: 
15754diff
changeset | 17 | val tokentrans_mode: string -> (string * (string -> string * real)) list -> | 
| 
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
 wenzelm parents: 
15754diff
changeset | 18 | (string * string * (string -> string * real)) list | 
| 
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
 wenzelm parents: 
15754diff
changeset | 19 | val standard_token_classes: string list | 
| 4050 | 20 | end; | 
| 240 | 21 | |
| 22 | signature SYN_EXT = | |
| 4050 | 23 | sig | 
| 240 | 24 | include SYN_EXT0 | 
| 1510 | 25 | val logic: string | 
| 26 | val args: string | |
| 27 | val cargs: string | |
| 28 | val any: string | |
| 29 | val sprop: string | |
| 30 | val typ_to_nonterm: typ -> string | |
| 31 | datatype xsymb = | |
| 32 | Delim of string | | |
| 33 | Argument of string * int | | |
| 34 | Space of string | | |
| 35 | Bg of int | Brk of int | En | |
| 36 | datatype xprod = XProd of string * xsymb list * string * int | |
| 37 | val chain_pri: int | |
| 4701 | 38 | val delims_of: xprod list -> string list list | 
| 1510 | 39 | datatype mfix = Mfix of string * typ * string * int list * int | 
| 40 | datatype syn_ext = | |
| 41 |     SynExt of {
 | |
| 42 | xprods: xprod list, | |
| 43 | consts: string list, | |
| 2913 | 44 | prmodes: string list, | 
| 15754 | 45 | parse_ast_translation: (string * ((Ast.ast list -> Ast.ast) * stamp)) list, | 
| 1510 | 46 | parse_rules: (Ast.ast * Ast.ast) list, | 
| 15754 | 47 | parse_translation: (string * ((term list -> term) * stamp)) list, | 
| 48 | print_translation: (string * ((bool -> typ -> term list -> term) * stamp)) list, | |
| 1510 | 49 | print_rules: (Ast.ast * Ast.ast) list, | 
| 15754 | 50 | print_ast_translation: (string * ((Ast.ast list -> Ast.ast) * stamp)) list, | 
| 6322 | 51 | token_translation: (string * string * (string -> string * real)) list} | 
| 4054 | 52 | val mfix_args: string -> int | 
| 12865 | 53 | val escape_mfix: string -> string | 
| 14903 | 54 | val syn_ext': bool -> (string -> bool) -> mfix list -> | 
| 15754 | 55 | string list -> (string * ((Ast.ast list -> Ast.ast) * stamp)) list * | 
| 56 | (string * ((term list -> term) * stamp)) list * | |
| 57 | (string * ((bool -> typ -> term list -> term) * stamp)) list * | |
| 58 | (string * ((Ast.ast list -> Ast.ast) * stamp)) list | |
| 6322 | 59 | -> (string * string * (string -> string * real)) list | 
| 1510 | 60 | -> (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext | 
| 14903 | 61 | val syn_ext: mfix list -> string list -> | 
| 15754 | 62 | (string * ((Ast.ast list -> Ast.ast) * stamp)) list * | 
| 63 | (string * ((term list -> term) * stamp)) list * | |
| 64 | (string * ((bool -> typ -> term list -> term) * stamp)) list * | |
| 65 | (string * ((Ast.ast list -> Ast.ast) * stamp)) list | |
| 6322 | 66 | -> (string * string * (string -> string * real)) list | 
| 1510 | 67 | -> (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext | 
| 14903 | 68 | val syn_ext_const_names: string list -> syn_ext | 
| 69 | val syn_ext_rules: (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext | |
| 70 | val syn_ext_trfuns: | |
| 15754 | 71 | (string * ((Ast.ast list -> Ast.ast) * stamp)) list * | 
| 72 | (string * ((term list -> term) * stamp)) list * | |
| 73 | (string * ((bool -> typ -> term list -> term) * stamp)) list * | |
| 74 | (string * ((Ast.ast list -> Ast.ast) * stamp)) list -> syn_ext | |
| 14903 | 75 | val syn_ext_tokentrfuns: (string * string * (string -> string * real)) list -> syn_ext | 
| 15835 
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
 wenzelm parents: 
15754diff
changeset | 76 | val standard_token_markers: string list | 
| 1510 | 77 | val pure_ext: syn_ext | 
| 4050 | 78 | end; | 
| 240 | 79 | |
| 15754 | 80 | structure SynExt: SYN_EXT = | 
| 240 | 81 | struct | 
| 82 | ||
| 2694 | 83 | |
| 240 | 84 | (** misc definitions **) | 
| 85 | ||
| 7472 | 86 | val dddot_indexname = ("dddot", 0);
 | 
| 6760 | 87 | val constrainC = "_constrain"; | 
| 88 | ||
| 89 | ||
| 240 | 90 | (* syntactic categories *) | 
| 91 | ||
| 92 | val logic = "logic"; | |
| 93 | val logicT = Type (logic, []); | |
| 94 | ||
| 95 | val args = "args"; | |
| 1178 | 96 | val cargs = "cargs"; | 
| 240 | 97 | |
| 330 
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
 clasohm parents: 
240diff
changeset | 98 | val typeT = Type ("type", []);
 | 
| 240 | 99 | |
| 764 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 100 | val sprop = "#prop"; | 
| 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 101 | val spropT = Type (sprop, []); | 
| 240 | 102 | |
| 764 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 103 | val any = "any"; | 
| 624 | 104 | val anyT = Type (any, []); | 
| 105 | ||
| 780 | 106 | |
| 240 | 107 | |
| 108 | (** datatype xprod **) | |
| 109 | ||
| 110 | (*Delim s: delimiter s | |
| 111 | Argument (s, p): nonterminal s requiring priority >= p, or valued token | |
| 112 | Space s: some white space for printing | |
| 113 | Bg, Brk, En: blocks and breaks for pretty printing*) | |
| 114 | ||
| 115 | datatype xsymb = | |
| 116 | Delim of string | | |
| 117 | Argument of string * int | | |
| 118 | Space of string | | |
| 119 | Bg of int | Brk of int | En; | |
| 120 | ||
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 121 | fun is_delim (Delim _) = true | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 122 | | is_delim _ = false; | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 123 | |
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 124 | fun is_terminal (Delim _) = true | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 125 | | is_terminal (Argument (s, _)) = Lexicon.is_terminal s | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 126 | | is_terminal _ = false; | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 127 | |
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 128 | fun is_argument (Argument _) = true | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 129 | | is_argument _ = false; | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 130 | |
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 131 | fun is_index (Argument ("index", _)) = true
 | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 132 | | is_index _ = false; | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 133 | |
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 134 | val index = Argument ("index", 1000);
 | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 135 | |
| 240 | 136 | |
| 137 | (*XProd (lhs, syms, c, p): | |
| 138 | lhs: name of nonterminal on the lhs of the production | |
| 139 | syms: list of symbols on the rhs of the production | |
| 140 | c: head of parse tree | |
| 141 | p: priority of this production*) | |
| 142 | ||
| 143 | datatype xprod = XProd of string * xsymb list * string * int; | |
| 144 | ||
| 145 | val max_pri = 1000; (*maximum legal priority*) | |
| 146 | val chain_pri = ~1; (*dummy for chain productions*) | |
| 147 | ||
| 148 | ||
| 149 | (* delims_of *) | |
| 150 | ||
| 151 | fun delims_of xprods = | |
| 152 | let | |
| 15531 | 153 | fun del_of (Delim s) = SOME s | 
| 154 | | del_of _ = NONE; | |
| 240 | 155 | |
| 156 | fun dels_of (XProd (_, xsymbs, _, _)) = | |
| 15570 | 157 | List.mapPartial del_of xsymbs; | 
| 240 | 158 | in | 
| 15570 | 159 | map Symbol.explode (distinct (List.concat (map dels_of xprods))) | 
| 240 | 160 | end; | 
| 161 | ||
| 162 | ||
| 163 | ||
| 164 | (** datatype mfix **) | |
| 165 | ||
| 166 | (*Mfix (sy, ty, c, ps, p): | |
| 167 | sy: rhs of production as symbolic string | |
| 168 | ty: type description of production | |
| 169 | c: head of parse tree | |
| 170 | ps: priorities of arguments in sy | |
| 171 | p: priority of production*) | |
| 172 | ||
| 173 | datatype mfix = Mfix of string * typ * string * int list * int; | |
| 174 | ||
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 175 | fun err_in_mfix msg (Mfix (sy, _, const, _, _)) = | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 176 | error ((if msg = "" then "" else msg ^ "\n") ^ | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 177 | "in mixfix annotation " ^ quote sy ^ " for " ^ quote const); | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 178 | |
| 240 | 179 | |
| 180 | (* typ_to_nonterm *) | |
| 181 | ||
| 865 | 182 | fun typ_to_nt _ (Type (c, _)) = c | 
| 183 | | typ_to_nt default _ = default; | |
| 184 | ||
| 764 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 185 | (*get nonterminal for rhs*) | 
| 865 | 186 | val typ_to_nonterm = typ_to_nt any; | 
| 240 | 187 | |
| 764 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 188 | (*get nonterminal for lhs*) | 
| 865 | 189 | val typ_to_nonterm1 = typ_to_nt logic; | 
| 240 | 190 | |
| 191 | ||
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 192 | (* read_mixfix *) | 
| 4050 | 193 | |
| 194 | local | |
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 195 |   fun is_meta c = c mem ["(", ")", "/", "_", "\\<index>"];
 | 
| 4050 | 196 | |
| 4701 | 197 | val scan_delim_char = | 
| 198 | $$ "'" |-- Scan.one ((not o Symbol.is_blank) andf Symbol.not_eof) || | |
| 199 | Scan.one ((not o is_meta) andf (not o Symbol.is_blank) andf Symbol.not_eof); | |
| 4050 | 200 | |
| 14819 | 201 | fun read_int ["0", "0"] = ~1 | 
| 202 | | read_int cs = #1 (Library.read_int cs); | |
| 203 | ||
| 4050 | 204 | val scan_sym = | 
| 205 |     $$ "_" >> K (Argument ("", 0)) ||
 | |
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 206 | $$ "\\<index>" >> K index || | 
| 14819 | 207 |     $$ "(" |-- Scan.any Symbol.is_digit >> (Bg o read_int) ||
 | 
| 4050 | 208 | $$ ")" >> K En || | 
| 209 | $$ "/" -- $$ "/" >> K (Brk ~1) || | |
| 4701 | 210 | $$ "/" |-- Scan.any Symbol.is_blank >> (Brk o length) || | 
| 211 | Scan.any1 Symbol.is_blank >> (Space o implode) || | |
| 212 | Scan.repeat1 scan_delim_char >> (Delim o implode); | |
| 4050 | 213 | |
| 214 | val scan_symb = | |
| 15531 | 215 | scan_sym >> SOME || | 
| 216 | $$ "'" -- Scan.one Symbol.is_blank >> K NONE; | |
| 4050 | 217 | |
| 4701 | 218 | val scan_symbs = Scan.repeat scan_symb --| Scan.ahead (Scan.one (not_equal "'")); | 
| 15973 | 219 | val read_symbs = List.mapPartial I o the o Scan.read Symbol.stopper scan_symbs; | 
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 220 | |
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 221 | fun unique_index xsymbs = | 
| 15570 | 222 | if length (List.filter is_index xsymbs) <= 1 then xsymbs | 
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 223 | else error "Duplicate index arguments (\\<index>)"; | 
| 4050 | 224 | in | 
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 225 | val read_mixfix = unique_index o read_symbs o Symbol.explode; | 
| 15570 | 226 | val mfix_args = length o List.filter is_argument o read_mixfix; | 
| 12865 | 227 | val escape_mfix = implode o map (fn s => if is_meta s then "'" ^ s else s) o Symbol.explode; | 
| 4050 | 228 | end; | 
| 229 | ||
| 230 | ||
| 240 | 231 | (* mfix_to_xprod *) | 
| 232 | ||
| 14903 | 233 | fun mfix_to_xprod convert is_logtype (mfix as Mfix (sy, typ, const, pris, pri)) = | 
| 240 | 234 | let | 
| 235 | fun check_pri p = | |
| 236 | if p >= 0 andalso p <= max_pri then () | |
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 237 |       else err_in_mfix ("Precedence out of range: " ^ string_of_int p) mfix;
 | 
| 240 | 238 | |
| 239 | fun blocks_ok [] 0 = true | |
| 240 | | blocks_ok [] _ = false | |
| 241 | | blocks_ok (Bg _ :: syms) n = blocks_ok syms (n + 1) | |
| 242 | | blocks_ok (En :: _) 0 = false | |
| 243 | | blocks_ok (En :: syms) n = blocks_ok syms (n - 1) | |
| 244 | | blocks_ok (_ :: syms) n = blocks_ok syms n; | |
| 245 | ||
| 246 | fun check_blocks syms = | |
| 247 | if blocks_ok syms 0 then () | |
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 248 | else err_in_mfix "Unbalanced block parentheses" mfix; | 
| 240 | 249 | |
| 250 | ||
| 251 | val cons_fst = apfst o cons; | |
| 252 | ||
| 253 | fun add_args [] ty [] = ([], typ_to_nonterm1 ty) | |
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 254 | | add_args [] _ _ = err_in_mfix "Too many precedences" mfix | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 255 |       | add_args ((arg as Argument ("index", _)) :: syms) ty ps =
 | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 256 | cons_fst arg (add_args syms ty ps) | 
| 240 | 257 |       | add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) [] =
 | 
| 258 | cons_fst (Argument (typ_to_nonterm ty, 0)) (add_args syms tys []) | |
| 259 |       | add_args (Argument _ :: syms) (Type ("fun", [ty, tys])) (p :: ps) =
 | |
| 260 | cons_fst (Argument (typ_to_nonterm ty, p)) (add_args syms tys ps) | |
| 261 | | add_args (Argument _ :: _) _ _ = | |
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 262 | err_in_mfix "More arguments than in corresponding type" mfix | 
| 240 | 263 | | add_args (sym :: syms) ty ps = cons_fst sym (add_args syms ty ps); | 
| 264 | ||
| 265 | fun rem_pri (Argument (s, _)) = Argument (s, chain_pri) | |
| 266 | | rem_pri sym = sym; | |
| 267 | ||
| 4054 | 268 | fun logify_types copy_prod (a as (Argument (s, p))) = | 
| 14903 | 269 | if s <> "prop" andalso is_logtype s then Argument (logic, p) else a | 
| 4054 | 270 | | logify_types _ a = a; | 
| 240 | 271 | |
| 2364 | 272 | |
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 273 | val raw_symbs = read_mixfix sy handle ERROR => err_in_mfix "" mfix; | 
| 15570 | 274 | val args = List.filter (fn Argument _ => true | _ => false) raw_symbs; | 
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 275 | val (const', typ', parse_rules) = | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 276 | if not (exists is_index args) then (const, typ, []) | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 277 | else | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 278 | let | 
| 14697 | 279 | val indexed_const = if const <> "" then "_indexed_" ^ const | 
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 280 | else err_in_mfix "Missing constant name for indexed syntax" mfix; | 
| 14697 | 281 | val rangeT = Term.range_type typ handle Match => | 
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 282 | err_in_mfix "Missing structure argument for indexed syntax" mfix; | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 283 | |
| 14697 | 284 | val xs = map Ast.Variable (Term.invent_names [] "xa" (length args - 1)); | 
| 285 | val (xs1, xs2) = Library.splitAt (Library.find_index is_index args, xs); | |
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 286 | val i = Ast.Variable "i"; | 
| 14697 | 287 | val lhs = Ast.mk_appl (Ast.Constant indexed_const) | 
| 288 | (xs1 @ [Ast.mk_appl (Ast.Constant "_index") [i]] @ xs2); | |
| 289 | val rhs = Ast.mk_appl (Ast.Constant const) (i :: xs); | |
| 290 | in (indexed_const, rangeT, [(lhs, rhs)]) end; | |
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 291 | |
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 292 | val (symbs, lhs) = add_args raw_symbs typ' pris; | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 293 | |
| 2364 | 294 | val copy_prod = | 
| 295 | lhs mem ["prop", "logic"] | |
| 296 | andalso const <> "" | |
| 297 | andalso not (null symbs) | |
| 298 | andalso not (exists is_delim symbs); | |
| 299 | val lhs' = | |
| 300 | if convert andalso not copy_prod then | |
| 14903 | 301 | (if lhs = "prop" then sprop else if is_logtype lhs then logic else lhs) | 
| 2364 | 302 | else lhs; | 
| 4054 | 303 | val symbs' = map (logify_types copy_prod) symbs; | 
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 304 | val xprod = XProd (lhs', symbs', const', pri); | 
| 240 | 305 | |
| 15570 | 306 | val _ = (List.app check_pri pris; check_pri pri; check_blocks symbs'); | 
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 307 | val xprod' = | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 308 |       if Lexicon.is_terminal lhs' then err_in_mfix ("Illegal lhs: " ^ lhs') mfix
 | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 309 | else if const <> "" then xprod | 
| 15570 | 310 | else if length (List.filter is_argument symbs') <> 1 then | 
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 311 | err_in_mfix "Copy production must have exactly one argument" mfix | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 312 | else if exists is_terminal symbs' then xprod | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 313 | else XProd (lhs', map rem_pri symbs', "", chain_pri); | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 314 | |
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 315 | in (xprod', parse_rules) end; | 
| 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 316 | |
| 240 | 317 | |
| 318 | ||
| 319 | (** datatype syn_ext **) | |
| 320 | ||
| 321 | datatype syn_ext = | |
| 322 |   SynExt of {
 | |
| 323 | xprods: xprod list, | |
| 324 | consts: string list, | |
| 2913 | 325 | prmodes: string list, | 
| 15754 | 326 | parse_ast_translation: (string * ((Ast.ast list -> Ast.ast) * stamp)) list, | 
| 1510 | 327 | parse_rules: (Ast.ast * Ast.ast) list, | 
| 15754 | 328 | parse_translation: (string * ((term list -> term) * stamp)) list, | 
| 329 | print_translation: (string * ((bool -> typ -> term list -> term) * stamp)) list, | |
| 1510 | 330 | print_rules: (Ast.ast * Ast.ast) list, | 
| 15754 | 331 | print_ast_translation: (string * ((Ast.ast list -> Ast.ast) * stamp)) list, | 
| 332 | token_translation: (string * string * (string -> string * real)) list}; | |
| 240 | 333 | |
| 334 | ||
| 335 | (* syn_ext *) | |
| 336 | ||
| 14903 | 337 | fun syn_ext' convert is_logtype mfixes consts trfuns tokentrfuns (parse_rules, print_rules) = | 
| 240 | 338 | let | 
| 339 | val (parse_ast_translation, parse_translation, print_translation, | |
| 340 | print_ast_translation) = trfuns; | |
| 341 | ||
| 14903 | 342 | val (xprods, parse_rules') = map (mfix_to_xprod convert is_logtype) mfixes | 
| 15570 | 343 | |> split_list |> apsnd (rev o List.concat); | 
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 344 | val mfix_consts = distinct (map (fn Mfix x => #3 x) mfixes @ map (fn XProd x => #3 x) xprods); | 
| 240 | 345 | in | 
| 346 |     SynExt {
 | |
| 624 | 347 | xprods = xprods, | 
| 11546 | 348 | consts = consts union_string mfix_consts, | 
| 2913 | 349 | prmodes = distinct (map (fn (m, _, _) => m) tokentrfuns), | 
| 240 | 350 | parse_ast_translation = parse_ast_translation, | 
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 351 | parse_rules = parse_rules' @ parse_rules, | 
| 240 | 352 | parse_translation = parse_translation, | 
| 353 | print_translation = print_translation, | |
| 12513 
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
 wenzelm parents: 
11546diff
changeset | 354 | print_rules = map swap parse_rules' @ print_rules, | 
| 2694 | 355 | print_ast_translation = print_ast_translation, | 
| 356 | token_translation = tokentrfuns} | |
| 240 | 357 | end; | 
| 358 | ||
| 2382 | 359 | |
| 14903 | 360 | val syn_ext = syn_ext' true (K false); | 
| 555 
a7f397a14b16
removed idT, varT, tidT, tvarT (now in lexicon.ML);
 wenzelm parents: 
441diff
changeset | 361 | |
| 14903 | 362 | fun syn_ext_const_names cs = syn_ext [] cs ([], [], [], []) [] ([], []); | 
| 363 | fun syn_ext_rules rules = syn_ext [] [] ([], [], [], []) [] rules; | |
| 364 | fun syn_ext_trfuns trfuns = syn_ext [] [] trfuns [] ([], []); | |
| 365 | fun syn_ext_tokentrfuns trfuns = syn_ext [] [] ([], [], [], []) trfuns ([], []); | |
| 2382 | 366 | |
| 15754 | 367 | fun stamp_trfun s (c, f) = (c, (f, s)); | 
| 368 | fun mk_trfun tr = stamp_trfun (stamp ()) tr; | |
| 369 | fun eq_trfun ((_, s1:stamp), (_, s2)) = s1 = s2; | |
| 370 | ||
| 240 | 371 | |
| 15835 
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
 wenzelm parents: 
15754diff
changeset | 372 | (* token translations *) | 
| 
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
 wenzelm parents: 
15754diff
changeset | 373 | |
| 
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
 wenzelm parents: 
15754diff
changeset | 374 | fun tokentrans_mode m trs = map (fn (s, f) => (m, s, f)) trs; | 
| 
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
 wenzelm parents: 
15754diff
changeset | 375 | |
| 
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
 wenzelm parents: 
15754diff
changeset | 376 | val standard_token_classes = | 
| 
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
 wenzelm parents: 
15754diff
changeset | 377 | ["class", "tfree", "tvar", "free", "bound", "var", "num", "xnum", "xstr"]; | 
| 
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
 wenzelm parents: 
15754diff
changeset | 378 | |
| 
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
 wenzelm parents: 
15754diff
changeset | 379 | val standard_token_markers = map (fn s => "_" ^ s) standard_token_classes; | 
| 
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
 wenzelm parents: 
15754diff
changeset | 380 | |
| 
fdf678bec567
added tokentrans_mode, standard_token_classes, standard_token_markers (from token_trans.ML);
 wenzelm parents: 
15754diff
changeset | 381 | |
| 764 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 382 | (* pure_ext *) | 
| 240 | 383 | |
| 14903 | 384 | val pure_ext = syn_ext' false (K false) | 
| 764 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 385 |   [Mfix ("_", spropT --> propT, "", [0], 0),
 | 
| 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 386 |    Mfix ("_", logicT --> anyT, "", [0], 0),
 | 
| 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 387 |    Mfix ("_", spropT --> anyT, "", [0], 0),
 | 
| 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 388 |    Mfix ("'(_')", logicT --> logicT, "", [0], max_pri),
 | 
| 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 389 |    Mfix ("'(_')", spropT --> spropT, "", [0], max_pri),
 | 
| 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 390 |    Mfix ("_::_",  [logicT, typeT] ---> logicT, "_constrain", [4, 0], 3),
 | 
| 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 391 |    Mfix ("_::_",  [spropT, typeT] ---> spropT, "_constrain", [4, 0], 3)]
 | 
| 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 392 | [] | 
| 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 393 | ([], [], [], []) | 
| 2694 | 394 | [] | 
| 764 
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
 clasohm parents: 
624diff
changeset | 395 | ([], []); | 
| 240 | 396 | |
| 397 | end; |