1 (* Title: Pure/Syntax/syntax.ML
2 Author: Tobias Nipkow and Markus Wenzel, TU Muenchen
4 Standard Isabelle syntax, based on arbitrary context-free grammars
5 (specified by mixfix declarations).
8 signature BASIC_SYNTAX =
22 val positions_raw: Config.raw
23 val positions: bool Config.T
24 val ambiguity_enabled: bool Config.T
25 val ambiguity_level_raw: Config.raw
26 val ambiguity_level: int Config.T
27 val ambiguity_limit: int Config.T
28 val read_token: string -> Symbol_Pos.T list * Position.T
29 val parse_token: Proof.context -> Markup.T -> string -> Symbol_Pos.T list * Position.T
30 val parse_sort: Proof.context -> string -> sort
31 val parse_typ: Proof.context -> string -> typ
32 val parse_term: Proof.context -> string -> term
33 val parse_prop: Proof.context -> string -> term
34 val unparse_sort: Proof.context -> sort -> Pretty.T
35 val unparse_classrel: Proof.context -> class list -> Pretty.T
36 val unparse_arity: Proof.context -> arity -> Pretty.T
37 val unparse_typ: Proof.context -> typ -> Pretty.T
38 val unparse_term: Proof.context -> term -> Pretty.T
39 val install_operations:
40 {parse_sort: Proof.context -> string -> sort,
41 parse_typ: Proof.context -> string -> typ,
42 parse_term: Proof.context -> string -> term,
43 parse_prop: Proof.context -> string -> term,
44 unparse_sort: Proof.context -> sort -> Pretty.T,
45 unparse_typ: Proof.context -> typ -> Pretty.T,
46 unparse_term: Proof.context -> term -> Pretty.T} -> unit
47 val print_checks: Proof.context -> unit
48 val add_typ_check: int -> string ->
49 (typ list -> Proof.context -> (typ list * Proof.context) option) ->
50 Context.generic -> Context.generic
51 val add_term_check: int -> string ->
52 (term list -> Proof.context -> (term list * Proof.context) option) ->
53 Context.generic -> Context.generic
54 val add_typ_uncheck: int -> string ->
55 (typ list -> Proof.context -> (typ list * Proof.context) option) ->
56 Context.generic -> Context.generic
57 val add_term_uncheck: int -> string ->
58 (term list -> Proof.context -> (term list * Proof.context) option) ->
59 Context.generic -> Context.generic
60 val check_sort: Proof.context -> sort -> sort
61 val check_typ: Proof.context -> typ -> typ
62 val check_term: Proof.context -> term -> term
63 val check_prop: Proof.context -> term -> term
64 val check_typs: Proof.context -> typ list -> typ list
65 val check_terms: Proof.context -> term list -> term list
66 val check_props: Proof.context -> term list -> term list
67 val uncheck_sort: Proof.context -> sort -> sort
68 val uncheck_arity: Proof.context -> arity -> arity
69 val uncheck_classrel: Proof.context -> class list -> class list
70 val uncheck_typs: Proof.context -> typ list -> typ list
71 val uncheck_terms: Proof.context -> term list -> term list
72 val read_sort: Proof.context -> string -> sort
73 val read_typ: Proof.context -> string -> typ
74 val read_term: Proof.context -> string -> term
75 val read_prop: Proof.context -> string -> term
76 val read_terms: Proof.context -> string list -> term list
77 val read_props: Proof.context -> string list -> term list
78 val read_sort_global: theory -> string -> sort
79 val read_typ_global: theory -> string -> typ
80 val read_term_global: theory -> string -> term
81 val read_prop_global: theory -> string -> term
82 val pretty_term: Proof.context -> term -> Pretty.T
83 val pretty_typ: Proof.context -> typ -> Pretty.T
84 val pretty_sort: Proof.context -> sort -> Pretty.T
85 val pretty_classrel: Proof.context -> class list -> Pretty.T
86 val pretty_arity: Proof.context -> arity -> Pretty.T
87 val string_of_term: Proof.context -> term -> string
88 val string_of_typ: Proof.context -> typ -> string
89 val string_of_sort: Proof.context -> sort -> string
90 val string_of_classrel: Proof.context -> class list -> string
91 val string_of_arity: Proof.context -> arity -> string
92 val is_pretty_global: Proof.context -> bool
93 val set_pretty_global: bool -> Proof.context -> Proof.context
94 val init_pretty_global: theory -> Proof.context
95 val pretty_term_global: theory -> term -> Pretty.T
96 val pretty_typ_global: theory -> typ -> Pretty.T
97 val pretty_sort_global: theory -> sort -> Pretty.T
98 val string_of_term_global: theory -> term -> string
99 val string_of_typ_global: theory -> typ -> string
100 val string_of_sort_global: theory -> sort -> string
101 val pp: Proof.context -> Pretty.pp
102 val pp_global: theory -> Pretty.pp
105 val eq_syntax: syntax * syntax -> bool
106 val is_keyword: syntax -> string -> bool
107 val tokenize: syntax -> bool -> Symbol_Pos.T list -> Lexicon.token list
108 val parse: Proof.context -> syntax -> string -> Lexicon.token list -> Parser.parsetree list
109 val parse_ast_translation: syntax -> string -> (Proof.context -> Ast.ast list -> Ast.ast) option
110 val parse_rules: syntax -> string -> (Ast.ast * Ast.ast) list
111 val parse_translation: syntax -> string -> (Proof.context -> term list -> term) option
112 val print_translation: syntax -> string ->
113 Proof.context -> typ -> term list -> term (*exception Match*)
114 val print_rules: syntax -> string -> (Ast.ast * Ast.ast) list
115 val print_ast_translation: syntax -> string ->
116 Proof.context -> Ast.ast list -> Ast.ast (*exception Match*)
117 val prtabs: syntax -> Printer.prtabs
119 val mode_default: mode
121 val merge_syntaxes: syntax -> syntax -> syntax
122 val basic_syntax: syntax
123 val basic_nonterms: string list
124 val print_gram: syntax -> unit
125 val print_trans: syntax -> unit
126 val print_syntax: syntax -> unit
127 val guess_infix: syntax -> string -> mixfix option
129 Parse_Rule of 'a * 'a |
130 Print_Rule of 'a * 'a |
131 Parse_Print_Rule of 'a * 'a
132 val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
133 val is_const: syntax -> string -> bool
135 (string * ((Ast.ast list -> Ast.ast) * stamp)) list *
136 (string * ((term list -> term) * stamp)) list *
137 (string * ((typ -> term list -> term) * stamp)) list *
138 (string * ((Ast.ast list -> Ast.ast) * stamp)) list -> syntax -> syntax
139 val update_advanced_trfuns:
140 (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list *
141 (string * ((Proof.context -> term list -> term) * stamp)) list *
142 (string * ((Proof.context -> typ -> term list -> term) * stamp)) list *
143 (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list -> syntax -> syntax
144 val update_type_gram: bool -> mode -> (string * typ * mixfix) list -> syntax -> syntax
145 val update_const_gram: bool -> (string -> bool) ->
146 mode -> (string * typ * mixfix) list -> syntax -> syntax
147 val update_trrules: Ast.ast trrule list -> syntax -> syntax
148 val remove_trrules: Ast.ast trrule list -> syntax -> syntax
151 structure Syntax: SYNTAX =
154 (** inner syntax operations **)
156 (* configuration options *)
158 val positions_raw = Config.declare "syntax_positions" (fn _ => Config.Bool true);
159 val positions = Config.bool positions_raw;
161 val ambiguity_enabled =
162 Config.bool (Config.declare "syntax_ambiguity_enabled" (fn _ => Config.Bool true));
164 val ambiguity_level_raw = Config.declare "syntax_ambiguity_level" (fn _ => Config.Int 1);
165 val ambiguity_level = Config.int ambiguity_level_raw;
167 val ambiguity_limit =
168 Config.int (Config.declare "syntax_ambiguity_limit" (fn _ => Config.Int 10));
171 (* read token -- with optional YXML encoding of position *)
175 val tree = YXML.parse str handle Fail msg => error msg;
176 val text = XML.content_of [tree];
179 XML.Elem ((name, props), _) =>
180 if name = Markup.tokenN then Position.of_properties props
182 | XML.Text _ => Position.none);
183 in (Symbol_Pos.explode (text, pos), pos) end;
188 fun parse_token ctxt markup str =
190 val (syms, pos) = read_token str;
191 val _ = Context_Position.report ctxt pos markup;
197 {parse_sort: Proof.context -> string -> sort,
198 parse_typ: Proof.context -> string -> typ,
199 parse_term: Proof.context -> string -> term,
200 parse_prop: Proof.context -> string -> term,
201 unparse_sort: Proof.context -> sort -> Pretty.T,
202 unparse_typ: Proof.context -> typ -> Pretty.T,
203 unparse_term: Proof.context -> term -> Pretty.T};
205 val operations: operations Single_Assignment.var = Single_Assignment.var "Syntax.operations";
207 fun operation which ctxt x =
208 (case Single_Assignment.peek operations of
209 NONE => raise Fail "Inner syntax operations not installed"
210 | SOME ops => which ops ctxt x);
214 val parse_sort = operation #parse_sort;
215 val parse_typ = operation #parse_typ;
216 val parse_term = operation #parse_term;
217 val parse_prop = operation #parse_prop;
218 val unparse_sort = operation #unparse_sort;
219 val unparse_typ = operation #unparse_typ;
220 val unparse_term = operation #unparse_term;
222 fun install_operations ops = Single_Assignment.assign operations ops;
227 (* context-sensitive (un)checking *)
231 type key = int * bool;
232 type 'a check = 'a list -> Proof.context -> ('a list * Proof.context) option;
234 structure Checks = Generic_Data
237 ((key * ((string * typ check) * stamp) list) list *
238 (key * ((string * term check) * stamp) list) list);
239 val empty = ([], []);
241 fun merge ((typ_checks1, term_checks1), (typ_checks2, term_checks2)) : T =
242 (AList.join (op =) (K (Library.merge (eq_snd (op =)))) (typ_checks1, typ_checks2),
243 AList.join (op =) (K (Library.merge (eq_snd (op =)))) (term_checks1, term_checks2));
246 fun gen_add which (key: key) name f =
247 Checks.map (which (AList.map_default op = (key, []) (cons ((name, f), stamp ()))));
249 fun check_stage fs = perhaps_loop (perhaps_apply (map uncurry fs));
251 fun gen_check which uncheck ctxt0 xs0 =
253 val funs = which (Checks.get (Context.Proof ctxt0))
254 |> map_filter (fn ((i, u), fs) => if uncheck = u then SOME (i, map (snd o fst) fs) else NONE)
255 |> Library.sort (int_ord o pairself fst) |> map snd
256 |> not uncheck ? map rev;
257 val check_all = perhaps_apply (map check_stage funs);
258 in #1 (perhaps check_all (xs0, ctxt0)) end;
261 (case f (TFree ("", S)) of
263 | _ => raise TYPE ("map_sort", [TFree ("", S)], []));
267 fun print_checks ctxt =
269 fun split_checks checks =
270 List.partition (fn ((_, un), _) => not un) checks
271 |> pairself (map (fn ((i, _), fs) => (i, map (fst o fst) fs))
272 #> sort (int_ord o pairself fst));
273 fun pretty_checks kind checks =
274 checks |> map (fn (i, names) => Pretty.block
275 [Pretty.str (kind ^ " (stage " ^ signed_string_of_int i ^ "):"),
276 Pretty.brk 1, Pretty.strs names]);
278 val (typs, terms) = Checks.get (Context.Proof ctxt);
279 val (typ_checks, typ_unchecks) = split_checks typs;
280 val (term_checks, term_unchecks) = split_checks terms;
282 pretty_checks "typ_checks" typ_checks @
283 pretty_checks "term_checks" term_checks @
284 pretty_checks "typ_unchecks" typ_unchecks @
285 pretty_checks "term_unchecks" term_unchecks
286 end |> Pretty.chunks |> Pretty.writeln;
288 fun add_typ_check stage = gen_add apfst (stage, false);
289 fun add_term_check stage = gen_add apsnd (stage, false);
290 fun add_typ_uncheck stage = gen_add apfst (stage, true);
291 fun add_term_uncheck stage = gen_add apsnd (stage, true);
293 val check_typs = gen_check fst false;
294 val check_terms = gen_check snd false;
295 fun check_props ctxt = map (Type.constraint propT) #> check_terms ctxt;
297 val check_typ = singleton o check_typs;
298 val check_term = singleton o check_terms;
299 val check_prop = singleton o check_props;
300 val check_sort = map_sort o check_typ;
302 val uncheck_typs = gen_check fst true;
303 val uncheck_terms = gen_check snd true;
304 val uncheck_sort = map_sort o singleton o uncheck_typs;
309 (* derived operations for classrel and arity *)
311 val uncheck_classrel = map o singleton o uncheck_sort;
313 fun unparse_classrel ctxt cs = Pretty.block (flat
314 (separate [Pretty.str " <", Pretty.brk 1] (map (single o unparse_sort ctxt o single) cs)));
316 fun uncheck_arity ctxt (a, Ss, S) =
318 val T = Type (a, replicate (length Ss) dummyT);
320 (case singleton (uncheck_typs ctxt) T of
322 | T => raise TYPE ("uncheck_arity", [T], []));
323 val Ss' = map (uncheck_sort ctxt) Ss;
324 val S' = uncheck_sort ctxt S;
325 in (a', Ss', S') end;
327 fun unparse_arity ctxt (a, Ss, S) =
329 val prtT = unparse_typ ctxt (Type (a, []));
332 else [Pretty.list "(" ")" (map (unparse_sort ctxt) Ss), Pretty.brk 1];
333 in Pretty.block ([prtT, Pretty.str " ::", Pretty.brk 1] @ dom @ [unparse_sort ctxt S]) end;
336 (* read = parse + check *)
338 fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt;
339 fun read_typ ctxt = parse_typ ctxt #> singleton (check_typs ctxt);
341 fun read_terms ctxt = map (parse_term ctxt) #> check_terms ctxt;
342 fun read_props ctxt = map (parse_prop ctxt) #> check_props ctxt;
344 val read_term = singleton o read_terms;
345 val read_prop = singleton o read_props;
347 val read_sort_global = read_sort o ProofContext.init_global;
348 val read_typ_global = read_typ o ProofContext.init_global;
349 val read_term_global = read_term o ProofContext.init_global;
350 val read_prop_global = read_prop o ProofContext.init_global;
353 (* pretty = uncheck + unparse *)
355 fun pretty_term ctxt = singleton (uncheck_terms ctxt) #> unparse_term ctxt;
356 fun pretty_typ ctxt = singleton (uncheck_typs ctxt) #> unparse_typ ctxt;
357 fun pretty_sort ctxt = uncheck_sort ctxt #> unparse_sort ctxt;
358 fun pretty_classrel ctxt = uncheck_classrel ctxt #> unparse_classrel ctxt;
359 fun pretty_arity ctxt = uncheck_arity ctxt #> unparse_arity ctxt;
361 val string_of_term = Pretty.string_of oo pretty_term;
362 val string_of_typ = Pretty.string_of oo pretty_typ;
363 val string_of_sort = Pretty.string_of oo pretty_sort;
364 val string_of_classrel = Pretty.string_of oo pretty_classrel;
365 val string_of_arity = Pretty.string_of oo pretty_arity;
368 (* global pretty printing *)
370 val pretty_global = Config.bool (Config.declare "Syntax.pretty_global" (K (Config.Bool false)));
371 fun is_pretty_global ctxt = Config.get ctxt pretty_global;
372 val set_pretty_global = Config.put pretty_global;
373 val init_pretty_global = set_pretty_global true o ProofContext.init_global;
375 val pretty_term_global = pretty_term o init_pretty_global;
376 val pretty_typ_global = pretty_typ o init_pretty_global;
377 val pretty_sort_global = pretty_sort o init_pretty_global;
379 val string_of_term_global = string_of_term o init_pretty_global;
380 val string_of_typ_global = string_of_typ o init_pretty_global;
381 val string_of_sort_global = string_of_sort o init_pretty_global;
384 (* pp operations -- deferred evaluation *)
386 fun pp ctxt = Pretty.pp
387 (fn x => pretty_term ctxt x,
388 fn x => pretty_typ ctxt x,
389 fn x => pretty_sort ctxt x,
390 fn x => pretty_classrel ctxt x,
391 fn x => pretty_arity ctxt x);
393 fun pp_global thy = Pretty.pp
394 (fn x => pretty_term_global thy x,
395 fn x => pretty_typ_global thy x,
396 fn x => pretty_sort_global thy x,
397 fn x => pretty_classrel (init_pretty_global thy) x,
398 fn x => pretty_arity (init_pretty_global thy) x);
402 (** tables of translation functions **)
404 (* parse (ast) translations *)
406 fun err_dup_trfun name c =
407 error ("More than one " ^ name ^ " for " ^ quote c);
409 fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c);
411 fun remove_trtab trfuns = fold (Symtab.remove Syn_Ext.eq_trfun) trfuns;
413 fun update_trtab name trfuns tab = fold Symtab.update_new trfuns (remove_trtab trfuns tab)
414 handle Symtab.DUP c => err_dup_trfun name c;
416 fun merge_trtabs name tab1 tab2 = Symtab.merge Syn_Ext.eq_trfun (tab1, tab2)
417 handle Symtab.DUP c => err_dup_trfun name c;
420 (* print (ast) translations *)
422 fun apply_tr' tab c ctxt T args =
424 val fns = map fst (Symtab.lookup_list tab c);
425 fun app_first [] = raise Match
426 | app_first (f :: fs) = f ctxt T args handle Match => app_first fs;
427 in app_first fns end;
429 fun apply_ast_tr' tab c ctxt args =
431 val fns = map fst (Symtab.lookup_list tab c);
432 fun app_first [] = raise Match
433 | app_first (f :: fs) = f ctxt args handle Match => app_first fs;
434 in app_first fns end;
436 fun update_tr'tab trfuns = fold_rev (Symtab.update_list Syn_Ext.eq_trfun) trfuns;
437 fun remove_tr'tab trfuns = fold (Symtab.remove_list Syn_Ext.eq_trfun) trfuns;
438 fun merge_tr'tabs tab1 tab2 = Symtab.merge_list Syn_Ext.eq_trfun (tab1, tab2);
442 (** tables of translation rules **)
444 type ruletab = (Ast.ast * Ast.ast) list Symtab.table;
446 fun dest_ruletab tab = maps snd (Symtab.dest tab);
448 val update_ruletab = fold_rev (fn r => Symtab.update_list (op =) (Ast.head_of_rule r, r));
449 val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r));
450 fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2);
454 (** datatype syntax **)
458 input: Syn_Ext.xprod list,
459 lexicon: Scan.lexicon,
462 prmodes: string list,
463 parse_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) Symtab.table,
464 parse_ruletab: ruletab,
465 parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table,
466 print_trtab: ((Proof.context -> typ -> term list -> term) * stamp) list Symtab.table,
467 print_ruletab: ruletab,
468 print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table,
469 prtabs: Printer.prtabs} * stamp;
471 fun rep_syntax (Syntax (tabs, _)) = tabs;
472 fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2;
474 fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode;
475 fun tokenize (Syntax ({lexicon, ...}, _)) = Lexicon.tokenize lexicon;
476 fun parse ctxt (Syntax ({gram, ...}, _)) = Parser.parse ctxt gram;
478 fun parse_ast_translation (Syntax ({parse_ast_trtab, ...}, _)) = lookup_tr parse_ast_trtab;
479 fun parse_translation (Syntax ({parse_trtab, ...}, _)) = lookup_tr parse_trtab;
480 fun parse_rules (Syntax ({parse_ruletab, ...}, _)) = Symtab.lookup_list parse_ruletab;
481 fun print_translation (Syntax ({print_trtab, ...}, _)) = apply_tr' print_trtab;
482 fun print_rules (Syntax ({print_ruletab, ...}, _)) = Symtab.lookup_list print_ruletab;
483 fun print_ast_translation (Syntax ({print_ast_trtab, ...}, _)) = apply_ast_tr' print_ast_trtab;
485 fun prtabs (Syntax ({prtabs, ...}, _)) = prtabs;
487 type mode = string * bool;
488 val mode_default = ("", true);
489 val mode_input = (Print_Mode.input, true);
494 val empty_syntax = Syntax
496 lexicon = Scan.empty_lexicon,
497 gram = Parser.empty_gram,
500 parse_ast_trtab = Symtab.empty,
501 parse_ruletab = Symtab.empty,
502 parse_trtab = Symtab.empty,
503 print_trtab = Symtab.empty,
504 print_ruletab = Symtab.empty,
505 print_ast_trtab = Symtab.empty,
506 prtabs = Printer.empty_prtabs}, stamp ());
511 fun update_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
513 val {input, lexicon, gram, consts = consts1, prmodes, parse_ast_trtab, parse_ruletab,
514 parse_trtab, print_trtab, print_ruletab, print_ast_trtab, prtabs} = tabs;
515 val Syn_Ext.Syn_Ext {xprods, consts = consts2, parse_ast_translation,
516 parse_rules, parse_translation, print_translation, print_rules,
517 print_ast_translation} = syn_ext;
519 if inout then distinct (op =) (filter_out (member (op =) input) xprods) else [];
520 fun if_inout xs = if inout then xs else [];
523 ({input = new_xprods @ input,
524 lexicon = fold Scan.extend_lexicon (Syn_Ext.delims_of new_xprods) lexicon,
525 gram = Parser.extend_gram new_xprods gram,
526 consts = Library.merge (op =) (consts1, filter_out Lexicon.is_marked consts2),
527 prmodes = insert (op =) mode prmodes,
529 update_trtab "parse ast translation" (if_inout parse_ast_translation) parse_ast_trtab,
530 parse_ruletab = update_ruletab (if_inout parse_rules) parse_ruletab,
531 parse_trtab = update_trtab "parse translation" (if_inout parse_translation) parse_trtab,
532 print_trtab = update_tr'tab print_translation print_trtab,
533 print_ruletab = update_ruletab print_rules print_ruletab,
534 print_ast_trtab = update_tr'tab print_ast_translation print_ast_trtab,
535 prtabs = Printer.update_prtabs mode xprods prtabs}, stamp ())
541 fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) =
543 val Syn_Ext.Syn_Ext {xprods, consts = _, parse_ast_translation, parse_rules,
544 parse_translation, print_translation, print_rules, print_ast_translation} = syn_ext;
545 val {input, lexicon, gram, consts, prmodes, parse_ast_trtab, parse_ruletab,
546 parse_trtab, print_trtab, print_ruletab, print_ast_trtab, prtabs} = tabs;
547 val input' = if inout then subtract (op =) xprods input else input;
548 val changed = length input <> length input';
549 fun if_inout xs = if inout then xs else [];
553 lexicon = if changed then Scan.make_lexicon (Syn_Ext.delims_of input') else lexicon,
554 gram = if changed then Parser.extend_gram input' Parser.empty_gram else gram,
557 parse_ast_trtab = remove_trtab (if_inout parse_ast_translation) parse_ast_trtab,
558 parse_ruletab = remove_ruletab (if_inout parse_rules) parse_ruletab,
559 parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab,
560 print_trtab = remove_tr'tab print_translation print_trtab,
561 print_ruletab = remove_ruletab print_rules print_ruletab,
562 print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab,
563 prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ())
569 fun merge_syntaxes (Syntax (tabs1, _)) (Syntax (tabs2, _)) =
571 val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
572 prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1,
573 parse_trtab = parse_trtab1, print_trtab = print_trtab1, print_ruletab = print_ruletab1,
574 print_ast_trtab = print_ast_trtab1, prtabs = prtabs1} = tabs1;
576 val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
577 prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2,
578 parse_trtab = parse_trtab2, print_trtab = print_trtab2, print_ruletab = print_ruletab2,
579 print_ast_trtab = print_ast_trtab2, prtabs = prtabs2} = tabs2;
582 ({input = Library.merge (op =) (input1, input2),
583 lexicon = Scan.merge_lexicons (lexicon1, lexicon2),
584 gram = Parser.merge_gram (gram1, gram2),
585 consts = sort_distinct string_ord (consts1 @ consts2),
586 prmodes = Library.merge (op =) (prmodes1, prmodes2),
588 merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2,
589 parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2,
590 parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2,
591 print_trtab = merge_tr'tabs print_trtab1 print_trtab2,
592 print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2,
593 print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2,
594 prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ())
600 val basic_syntax = update_syntax mode_default Syn_Ext.pure_ext empty_syntax;
603 (Lexicon.terminals @ [Syn_Ext.logic, "type", "types", "sort", "classes",
604 Syn_Ext.args, Syn_Ext.cargs, "pttrn", "pttrns", "idt", "idts", "aprop",
605 "asms", Syn_Ext.any, Syn_Ext.sprop, "num_const", "float_const", "index",
606 "struct", "id_position", "longid_position", "type_name", "class_name"]);
614 fun pretty_strs_qs name strs =
615 Pretty.strs (name :: map quote (sort_strings strs));
617 fun pretty_gram (Syntax (tabs, _)) =
619 val {lexicon, prmodes, gram, ...} = tabs;
620 val prmodes' = sort_strings (filter_out (fn s => s = "") prmodes);
622 [pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon),
623 Pretty.big_list "prods:" (Parser.pretty_gram gram),
624 pretty_strs_qs "print modes:" prmodes']
627 fun pretty_trans (Syntax (tabs, _)) =
629 fun pretty_trtab name tab =
630 pretty_strs_qs name (Symtab.keys tab);
632 fun pretty_ruletab name tab =
633 Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab));
635 val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
636 print_ruletab, print_ast_trtab, ...} = tabs;
638 [pretty_strs_qs "consts:" consts,
639 pretty_trtab "parse_ast_translation:" parse_ast_trtab,
640 pretty_ruletab "parse_rules:" parse_ruletab,
641 pretty_trtab "parse_translation:" parse_trtab,
642 pretty_trtab "print_translation:" print_trtab,
643 pretty_ruletab "print_rules:" print_ruletab,
644 pretty_trtab "print_ast_translation:" print_ast_trtab]
649 fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn));
650 fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn));
651 fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn));
656 (* reconstructing infixes -- educated guessing *)
658 fun guess_infix (Syntax ({gram, ...}, _)) c =
659 (case Parser.guess_infix_lr gram c of
660 SOME (s, l, r, j) => SOME
661 (if l then Mixfix.Infixl (s, j)
662 else if r then Mixfix.Infixr (s, j)
663 else Mixfix.Infix (s, j))
668 (** prepare translation rules **)
673 Parse_Rule of 'a * 'a |
674 Print_Rule of 'a * 'a |
675 Parse_Print_Rule of 'a * 'a;
677 fun map_trrule f (Parse_Rule (x, y)) = Parse_Rule (f x, f y)
678 | map_trrule f (Print_Rule (x, y)) = Print_Rule (f x, f y)
679 | map_trrule f (Parse_Print_Rule (x, y)) = Parse_Print_Rule (f x, f y);
681 fun parse_rule (Parse_Rule pats) = SOME pats
682 | parse_rule (Print_Rule _) = NONE
683 | parse_rule (Parse_Print_Rule pats) = SOME pats;
685 fun print_rule (Parse_Rule _) = NONE
686 | print_rule (Print_Rule pats) = SOME (swap pats)
687 | print_rule (Parse_Print_Rule pats) = SOME (swap pats);
690 fun is_const (Syntax ({consts, ...}, _)) c = member (op =) consts c;
697 fun check_rule rule =
698 (case Ast.rule_error rule of
700 error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
701 Pretty.string_of (Ast.pretty_rule rule))
706 fun check_rules rules =
707 (map check_rule (map_filter parse_rule rules),
708 map check_rule (map_filter print_rule rules));
714 (** modify syntax **)
716 fun ext_syntax f decls = update_syntax mode_default (f decls);
718 val update_trfuns = ext_syntax Syn_Ext.syn_ext_trfuns;
719 val update_advanced_trfuns = ext_syntax Syn_Ext.syn_ext_advanced_trfuns;
721 fun update_type_gram add prmode decls =
722 (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_types decls);
724 fun update_const_gram add is_logtype prmode decls =
725 (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_consts is_logtype decls);
727 val update_trrules = ext_syntax Syn_Ext.syn_ext_rules o check_rules;
728 val remove_trrules = remove_syntax mode_default o Syn_Ext.syn_ext_rules o check_rules;
731 (*export parts of internal Syntax structures*)
732 val syntax_tokenize = tokenize;
733 open Lexicon Syn_Ext Syn_Trans Mixfix Printer;
734 val tokenize = syntax_tokenize;
738 structure Basic_Syntax: BASIC_SYNTAX = Syntax;
741 forget_structure "Syn_Ext";
742 forget_structure "Mixfix";