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