| author | blanchet | 
| Thu, 26 Jun 2014 20:32:31 +0200 | |
| changeset 57387 | 2b6fe2a48352 | 
| parent 56438 | 7f6b2634d853 | 
| child 58047 | 9f3826352b52 | 
| permissions | -rw-r--r-- | 
| 18 | 1 | (* Title: Pure/Syntax/syntax.ML | 
| 0 | 2 | Author: Tobias Nipkow and Markus Wenzel, TU Muenchen | 
| 18 | 3 | |
| 24263 | 4 | Standard Isabelle syntax, based on arbitrary context-free grammars | 
| 5 | (specified by mixfix declarations). | |
| 0 | 6 | *) | 
| 7 | ||
| 8 | signature SYNTAX = | |
| 2383 | 9 | sig | 
| 42382 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 10 | type operations | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 11 | val install_operations: operations -> unit | 
| 42288 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 wenzelm parents: 
42287diff
changeset | 12 | val root: string Config.T | 
| 46512 
4f9f61f9b535
simplified configuration options for syntax ambiguity;
 wenzelm parents: 
46506diff
changeset | 13 | val ambiguity_warning_raw: Config.raw | 
| 
4f9f61f9b535
simplified configuration options for syntax ambiguity;
 wenzelm parents: 
46506diff
changeset | 14 | val ambiguity_warning: bool Config.T | 
| 46506 
c7faa011bfa7
simplified configuration options for syntax ambiguity;
 wenzelm parents: 
46483diff
changeset | 15 | val ambiguity_limit_raw: Config.raw | 
| 42268 | 16 | val ambiguity_limit: int Config.T | 
| 46849 | 17 | val read_token_pos: string -> Position.T | 
| 55829 | 18 | val read_token: string -> Symbol_Pos.source | 
| 43731 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43558diff
changeset | 19 | val parse_token: Proof.context -> (XML.tree list -> 'a) -> | 
| 55828 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 20 | (bool -> Markup.T) -> (Symbol_Pos.T list * Position.T -> 'a) -> string -> 'a | 
| 24263 | 21 | val parse_sort: Proof.context -> string -> sort | 
| 22 | val parse_typ: Proof.context -> string -> typ | |
| 23 | val parse_term: Proof.context -> string -> term | |
| 24 | val parse_prop: Proof.context -> string -> term | |
| 24768 | 25 | val unparse_sort: Proof.context -> sort -> Pretty.T | 
| 24923 | 26 | val unparse_classrel: Proof.context -> class list -> Pretty.T | 
| 27 | val unparse_arity: Proof.context -> arity -> Pretty.T | |
| 24768 | 28 | val unparse_typ: Proof.context -> typ -> Pretty.T | 
| 29 | val unparse_term: Proof.context -> term -> Pretty.T | |
| 24488 | 30 | val check_sort: Proof.context -> sort -> sort | 
| 24512 | 31 | val check_typ: Proof.context -> typ -> typ | 
| 32 | val check_term: Proof.context -> term -> term | |
| 33 | val check_prop: Proof.context -> term -> term | |
| 24488 | 34 | val check_typs: Proof.context -> typ list -> typ list | 
| 24263 | 35 | val check_terms: Proof.context -> term list -> term list | 
| 36 | val check_props: Proof.context -> term list -> term list | |
| 24923 | 37 | val uncheck_sort: Proof.context -> sort -> sort | 
| 38 | val uncheck_arity: Proof.context -> arity -> arity | |
| 39 | val uncheck_classrel: Proof.context -> class list -> class list | |
| 24768 | 40 | val uncheck_typs: Proof.context -> typ list -> typ list | 
| 41 | val uncheck_terms: Proof.context -> term list -> term list | |
| 24263 | 42 | val read_sort: Proof.context -> string -> sort | 
| 43 | val read_typ: Proof.context -> string -> typ | |
| 24488 | 44 | val read_term: Proof.context -> string -> term | 
| 45 | val read_prop: Proof.context -> string -> term | |
| 46989 | 46 | val read_typs: Proof.context -> string list -> typ list | 
| 24263 | 47 | val read_terms: Proof.context -> string list -> term list | 
| 48 | val read_props: Proof.context -> string list -> term list | |
| 24709 | 49 | val read_sort_global: theory -> string -> sort | 
| 50 | val read_typ_global: theory -> string -> typ | |
| 51 | val read_term_global: theory -> string -> term | |
| 52 | val read_prop_global: theory -> string -> term | |
| 24923 | 53 | val pretty_term: Proof.context -> term -> Pretty.T | 
| 54 | val pretty_typ: Proof.context -> typ -> Pretty.T | |
| 55 | val pretty_sort: Proof.context -> sort -> Pretty.T | |
| 56 | val pretty_classrel: Proof.context -> class list -> Pretty.T | |
| 57 | val pretty_arity: Proof.context -> arity -> Pretty.T | |
| 58 | val string_of_term: Proof.context -> term -> string | |
| 59 | val string_of_typ: Proof.context -> typ -> string | |
| 60 | val string_of_sort: Proof.context -> sort -> string | |
| 61 | val string_of_classrel: Proof.context -> class list -> string | |
| 62 | val string_of_arity: Proof.context -> arity -> string | |
| 26951 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 63 | val is_pretty_global: Proof.context -> bool | 
| 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 64 | val set_pretty_global: bool -> Proof.context -> Proof.context | 
| 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 65 | val init_pretty_global: theory -> Proof.context | 
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42382diff
changeset | 66 | val init_pretty: Context.pretty -> Proof.context | 
| 26951 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 67 | val pretty_term_global: theory -> term -> Pretty.T | 
| 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 68 | val pretty_typ_global: theory -> typ -> Pretty.T | 
| 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 69 | val pretty_sort_global: theory -> sort -> Pretty.T | 
| 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 70 | val string_of_term_global: theory -> term -> string | 
| 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 71 | val string_of_typ_global: theory -> typ -> string | 
| 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 72 | val string_of_sort_global: theory -> sort -> string | 
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 73 | type syntax | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 74 | val eq_syntax: syntax * syntax -> bool | 
| 45632 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 75 | val force_syntax: syntax -> unit | 
| 42298 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 76 | val lookup_const: syntax -> string -> string option | 
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 77 | val is_keyword: syntax -> string -> bool | 
| 42251 | 78 | val tokenize: syntax -> bool -> Symbol_Pos.T list -> Lexicon.token list | 
| 45641 | 79 | val parse: syntax -> string -> Lexicon.token list -> Parser.parsetree list | 
| 42253 | 80 | val parse_ast_translation: syntax -> string -> (Proof.context -> Ast.ast list -> Ast.ast) option | 
| 42255 | 81 | val parse_rules: syntax -> string -> (Ast.ast * Ast.ast) list | 
| 42253 | 82 | val parse_translation: syntax -> string -> (Proof.context -> term list -> term) option | 
| 42254 | 83 | val print_translation: syntax -> string -> | 
| 84 | Proof.context -> typ -> term list -> term (*exception Match*) | |
| 42255 | 85 | val print_rules: syntax -> string -> (Ast.ast * Ast.ast) list | 
| 42254 | 86 | val print_ast_translation: syntax -> string -> | 
| 87 | Proof.context -> Ast.ast list -> Ast.ast (*exception Match*) | |
| 42255 | 88 | val prtabs: syntax -> Printer.prtabs | 
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 89 | type mode | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 90 | val mode_default: mode | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 91 | val mode_input: mode | 
| 42294 | 92 | val empty_syntax: syntax | 
| 45632 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 93 | val merge_syntax: syntax * syntax -> syntax | 
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 94 | val print_gram: syntax -> unit | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 95 | val print_trans: syntax -> unit | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 96 | val print_syntax: syntax -> unit | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 97 | val guess_infix: syntax -> string -> mixfix option | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 98 | datatype 'a trrule = | 
| 42204 | 99 | Parse_Rule of 'a * 'a | | 
| 100 | Print_Rule of 'a * 'a | | |
| 101 | Parse_Print_Rule of 'a * 'a | |
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 102 |   val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule
 | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 103 | val update_trfuns: | 
| 42224 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
42223diff
changeset | 104 | (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list * | 
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 105 | (string * ((Proof.context -> term list -> term) * stamp)) list * | 
| 42247 
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
 wenzelm parents: 
42245diff
changeset | 106 | (string * ((Proof.context -> typ -> term list -> term) * stamp)) list * | 
| 42224 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
42223diff
changeset | 107 | (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list -> syntax -> syntax | 
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 108 | val update_type_gram: bool -> mode -> (string * typ * mixfix) list -> syntax -> syntax | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 109 | val update_const_gram: bool -> (string -> bool) -> | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 110 | mode -> (string * typ * mixfix) list -> syntax -> syntax | 
| 42224 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
42223diff
changeset | 111 | val update_trrules: Ast.ast trrule list -> syntax -> syntax | 
| 
578a51fae383
discontinued special treatment of structure Ast: no pervasive content, no inclusion in structure Syntax;
 wenzelm parents: 
42223diff
changeset | 112 | val remove_trrules: Ast.ast trrule list -> syntax -> syntax | 
| 42476 | 113 | val const: string -> term | 
| 114 | val free: string -> term | |
| 115 | val var: indexname -> term | |
| 2383 | 116 | end; | 
| 0 | 117 | |
| 12094 | 118 | structure Syntax: SYNTAX = | 
| 0 | 119 | struct | 
| 120 | ||
| 42288 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 wenzelm parents: 
42287diff
changeset | 121 | |
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 122 | (** inner syntax operations **) | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 123 | |
| 42382 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 124 | (* back-patched operations *) | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 125 | |
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 126 | type operations = | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 127 |  {parse_sort: Proof.context -> string -> sort,
 | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 128 | parse_typ: Proof.context -> string -> typ, | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 129 | parse_term: Proof.context -> string -> term, | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 130 | parse_prop: Proof.context -> string -> term, | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 131 | unparse_sort: Proof.context -> sort -> Pretty.T, | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 132 | unparse_typ: Proof.context -> typ -> Pretty.T, | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 133 | unparse_term: Proof.context -> term -> Pretty.T, | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 134 | check_typs: Proof.context -> typ list -> typ list, | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 135 | check_terms: Proof.context -> term list -> term list, | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 136 | check_props: Proof.context -> term list -> term list, | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 137 | uncheck_typs: Proof.context -> typ list -> typ list, | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 138 | uncheck_terms: Proof.context -> term list -> term list}; | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 139 | |
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 140 | val operations: operations Single_Assignment.var = Single_Assignment.var "Syntax.operations"; | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 141 | fun install_operations ops = Single_Assignment.assign operations ops; | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 142 | |
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 143 | fun operation which ctxt x = | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 144 | (case Single_Assignment.peek operations of | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 145 | NONE => raise Fail "Inner syntax operations not installed" | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 146 | | SOME ops => which ops ctxt x); | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 147 | |
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 148 | |
| 42268 | 149 | (* configuration options *) | 
| 150 | ||
| 56438 | 151 | val root = Config.string (Config.declare ("syntax_root", @{here}) (K (Config.String "any")));
 | 
| 42288 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 wenzelm parents: 
42287diff
changeset | 152 | |
| 56438 | 153 | val ambiguity_warning_raw = | 
| 154 |   Config.declare ("syntax_ambiguity_warning", @{here}) (fn _ => Config.Bool true);
 | |
| 46512 
4f9f61f9b535
simplified configuration options for syntax ambiguity;
 wenzelm parents: 
46506diff
changeset | 155 | val ambiguity_warning = Config.bool ambiguity_warning_raw; | 
| 42268 | 156 | |
| 56438 | 157 | val ambiguity_limit_raw = | 
| 158 |   Config.declare ("syntax_ambiguity_limit", @{here}) (fn _ => Config.Int 10);
 | |
| 46506 
c7faa011bfa7
simplified configuration options for syntax ambiguity;
 wenzelm parents: 
46483diff
changeset | 159 | val ambiguity_limit = Config.int ambiguity_limit_raw; | 
| 44069 | 160 | |
| 42268 | 161 | |
| 43731 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43558diff
changeset | 162 | (* outer syntax token -- with optional YXML content *) | 
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 163 | |
| 55828 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 164 | local | 
| 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 165 | |
| 46849 | 166 | fun token_position (XML.Elem ((name, props), _)) = | 
| 55828 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 167 | if name = Markup.tokenN | 
| 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 168 | then (Markup.is_delimited props, Position.of_properties props) | 
| 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 169 | else (false, Position.none) | 
| 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 170 | | token_position (XML.Text _) = (false, Position.none); | 
| 46849 | 171 | |
| 55828 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 172 | fun token_source tree = | 
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 173 | let | 
| 39555 
ccb223a4d49c
added XML.content_of convenience -- cover XML.body, which is the general situation;
 wenzelm parents: 
39510diff
changeset | 174 | val text = XML.content_of [tree]; | 
| 55828 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 175 | val (delimited, pos) = token_position tree; | 
| 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 176 |   in {delimited = delimited, text = text, pos = pos} end;
 | 
| 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 177 | |
| 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 178 | in | 
| 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 179 | |
| 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 180 | fun read_token_pos str = #2 (token_position (YXML.parse str handle Fail msg => error msg)); | 
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 181 | |
| 55829 | 182 | fun read_token str = token_source (YXML.parse str handle Fail msg => error msg); | 
| 43731 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43558diff
changeset | 183 | |
| 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43558diff
changeset | 184 | fun parse_token ctxt decode markup parse str = | 
| 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43558diff
changeset | 185 | let | 
| 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43558diff
changeset | 186 | fun parse_tree tree = | 
| 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43558diff
changeset | 187 | let | 
| 55828 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 188 |         val {delimited, text, pos} = token_source tree;
 | 
| 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 189 | val syms = Symbol_Pos.explode (text, pos); | 
| 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 190 | val _ = Context_Position.report ctxt pos (markup delimited); | 
| 43731 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43558diff
changeset | 191 | in parse (syms, pos) end; | 
| 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43558diff
changeset | 192 | in | 
| 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43558diff
changeset | 193 | (case YXML.parse_body str handle Fail msg => error msg of | 
| 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43558diff
changeset | 194 | body as [tree as XML.Elem ((name, _), _)] => | 
| 50201 
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
 wenzelm parents: 
49674diff
changeset | 195 | if name = Markup.tokenN then parse_tree tree else decode body | 
| 43731 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43558diff
changeset | 196 | | [tree as XML.Text _] => parse_tree tree | 
| 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43558diff
changeset | 197 | | body => decode body) | 
| 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43558diff
changeset | 198 | end; | 
| 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43558diff
changeset | 199 | |
| 55828 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 200 | end; | 
| 
42ac3cfb89f6
clarified language markup: added "delimited" property;
 wenzelm parents: 
52160diff
changeset | 201 | |
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 202 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 203 | (* (un)parsing *) | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 204 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 205 | val parse_sort = operation #parse_sort; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 206 | val parse_typ = operation #parse_typ; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 207 | val parse_term = operation #parse_term; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 208 | val parse_prop = operation #parse_prop; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 209 | val unparse_sort = operation #unparse_sort; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 210 | val unparse_typ = operation #unparse_typ; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 211 | val unparse_term = operation #unparse_term; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 212 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 213 | |
| 45429 | 214 | (* (un)checking *) | 
| 42382 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 215 | |
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 216 | val check_typs = operation #check_typs; | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 217 | val check_terms = operation #check_terms; | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 218 | val check_props = operation #check_props; | 
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 219 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 220 | val check_typ = singleton o check_typs; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 221 | val check_term = singleton o check_terms; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 222 | val check_prop = singleton o check_props; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 223 | |
| 42382 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 224 | val uncheck_typs = operation #uncheck_typs; | 
| 
dcd983ee2c29
provide structure Syntax early (before structure Type), back-patch check/uncheck later;
 wenzelm parents: 
42360diff
changeset | 225 | val uncheck_terms = operation #uncheck_terms; | 
| 42402 
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
 wenzelm parents: 
42383diff
changeset | 226 | |
| 
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
 wenzelm parents: 
42383diff
changeset | 227 | |
| 
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
 wenzelm parents: 
42383diff
changeset | 228 | (* derived operations for algebra of sorts *) | 
| 
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
 wenzelm parents: 
42383diff
changeset | 229 | |
| 
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
 wenzelm parents: 
42383diff
changeset | 230 | local | 
| 
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
 wenzelm parents: 
42383diff
changeset | 231 | |
| 
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
 wenzelm parents: 
42383diff
changeset | 232 | fun map_sort f S = | 
| 
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
 wenzelm parents: 
42383diff
changeset | 233 |   (case f (TFree ("", S)) of
 | 
| 
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
 wenzelm parents: 
42383diff
changeset | 234 |     TFree ("", S') => S'
 | 
| 
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
 wenzelm parents: 
42383diff
changeset | 235 |   | _ => raise TYPE ("map_sort", [TFree ("", S)], []));
 | 
| 
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
 wenzelm parents: 
42383diff
changeset | 236 | |
| 
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
 wenzelm parents: 
42383diff
changeset | 237 | in | 
| 
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
 wenzelm parents: 
42383diff
changeset | 238 | |
| 
c7139609b67d
simplified check/uncheck interfaces: result comparison is hardwired by default;
 wenzelm parents: 
42383diff
changeset | 239 | val check_sort = map_sort o check_typ; | 
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 240 | val uncheck_sort = map_sort o singleton o uncheck_typs; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 241 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 242 | end; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 243 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 244 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 245 | val uncheck_classrel = map o singleton o uncheck_sort; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 246 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 247 | fun unparse_classrel ctxt cs = Pretty.block (flat | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 248 | (separate [Pretty.str " <", Pretty.brk 1] (map (single o unparse_sort ctxt o single) cs))); | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 249 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 250 | fun uncheck_arity ctxt (a, Ss, S) = | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 251 | let | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 252 | val T = Type (a, replicate (length Ss) dummyT); | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 253 | val a' = | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 254 | (case singleton (uncheck_typs ctxt) T of | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 255 | Type (a', _) => a' | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 256 |       | T => raise TYPE ("uncheck_arity", [T], []));
 | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 257 | val Ss' = map (uncheck_sort ctxt) Ss; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 258 | val S' = uncheck_sort ctxt S; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 259 | in (a', Ss', S') end; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 260 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 261 | fun unparse_arity ctxt (a, Ss, S) = | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 262 | let | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 263 | val prtT = unparse_typ ctxt (Type (a, [])); | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 264 | val dom = | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 265 | if null Ss then [] | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 266 |       else [Pretty.list "(" ")" (map (unparse_sort ctxt) Ss), Pretty.brk 1];
 | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 267 | in Pretty.block ([prtT, Pretty.str " ::", Pretty.brk 1] @ dom @ [unparse_sort ctxt S]) end; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 268 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 269 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 270 | (* read = parse + check *) | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 271 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 272 | fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt; | 
| 46989 | 273 | |
| 274 | fun read_typs ctxt = | |
| 275 | grouped 10 (Par_List.map_name "Syntax.read_typs") (parse_typ ctxt) #> check_typs ctxt; | |
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 276 | |
| 46989 | 277 | fun read_terms ctxt = | 
| 278 | grouped 10 (Par_List.map_name "Syntax.read_terms") (parse_term ctxt) #> check_terms ctxt; | |
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 279 | |
| 46989 | 280 | fun read_props ctxt = | 
| 281 | grouped 10 (Par_List.map_name "Syntax.read_props") (parse_prop ctxt) #> check_props ctxt; | |
| 282 | ||
| 283 | val read_typ = singleton o read_typs; | |
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 284 | val read_term = singleton o read_terms; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 285 | val read_prop = singleton o read_props; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 286 | |
| 42360 | 287 | val read_sort_global = read_sort o Proof_Context.init_global; | 
| 288 | val read_typ_global = read_typ o Proof_Context.init_global; | |
| 289 | val read_term_global = read_term o Proof_Context.init_global; | |
| 290 | val read_prop_global = read_prop o Proof_Context.init_global; | |
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 291 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 292 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 293 | (* pretty = uncheck + unparse *) | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 294 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 295 | fun pretty_term ctxt = singleton (uncheck_terms ctxt) #> unparse_term ctxt; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 296 | fun pretty_typ ctxt = singleton (uncheck_typs ctxt) #> unparse_typ ctxt; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 297 | fun pretty_sort ctxt = uncheck_sort ctxt #> unparse_sort ctxt; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 298 | fun pretty_classrel ctxt = uncheck_classrel ctxt #> unparse_classrel ctxt; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 299 | fun pretty_arity ctxt = uncheck_arity ctxt #> unparse_arity ctxt; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 300 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 301 | val string_of_term = Pretty.string_of oo pretty_term; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 302 | val string_of_typ = Pretty.string_of oo pretty_typ; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 303 | val string_of_sort = Pretty.string_of oo pretty_sort; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 304 | val string_of_classrel = Pretty.string_of oo pretty_classrel; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 305 | val string_of_arity = Pretty.string_of oo pretty_arity; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 306 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 307 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 308 | (* global pretty printing *) | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 309 | |
| 56438 | 310 | val pretty_global = | 
| 311 |   Config.bool (Config.declare ("Syntax.pretty_global", @{here}) (K (Config.Bool false)));
 | |
| 39508 
dfacdb01e1ec
simplified some internal flags using Config.T instead of full-blown Proof_Data;
 wenzelm parents: 
39507diff
changeset | 312 | fun is_pretty_global ctxt = Config.get ctxt pretty_global; | 
| 
dfacdb01e1ec
simplified some internal flags using Config.T instead of full-blown Proof_Data;
 wenzelm parents: 
39507diff
changeset | 313 | val set_pretty_global = Config.put pretty_global; | 
| 42360 | 314 | val init_pretty_global = set_pretty_global true o Proof_Context.init_global; | 
| 42383 
0ae4ad40d7b5
simplified pretty printing context, which is only required for certain kernel operations;
 wenzelm parents: 
42382diff
changeset | 315 | val init_pretty = Context.pretty_context init_pretty_global; | 
| 39135 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 316 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 317 | val pretty_term_global = pretty_term o init_pretty_global; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 318 | val pretty_typ_global = pretty_typ o init_pretty_global; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 319 | val pretty_sort_global = pretty_sort o init_pretty_global; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 320 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 321 | val string_of_term_global = string_of_term o init_pretty_global; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 322 | val string_of_typ_global = string_of_typ o init_pretty_global; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 323 | val string_of_sort_global = string_of_sort o init_pretty_global; | 
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 324 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 325 | |
| 
6f5f64542405
structure Syntax: define "interfaces" before actual implementations;
 wenzelm parents: 
39126diff
changeset | 326 | |
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 327 | (** tables of translation functions **) | 
| 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 328 | |
| 5692 | 329 | (* parse (ast) translations *) | 
| 330 | ||
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
23631diff
changeset | 331 | fun err_dup_trfun name c = | 
| 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
23631diff
changeset | 332 |   error ("More than one " ^ name ^ " for " ^ quote c);
 | 
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 333 | |
| 42253 | 334 | fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c); | 
| 335 | ||
| 42288 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 wenzelm parents: 
42287diff
changeset | 336 | fun remove_trtab trfuns = fold (Symtab.remove Syntax_Ext.eq_trfun) trfuns; | 
| 21536 
f119c730f509
extend_trtab: allow identical trfuns to be overwritten;
 wenzelm parents: 
20784diff
changeset | 337 | |
| 29004 | 338 | fun update_trtab name trfuns tab = fold Symtab.update_new trfuns (remove_trtab trfuns tab) | 
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
23631diff
changeset | 339 | handle Symtab.DUP c => err_dup_trfun name c; | 
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 340 | |
| 42288 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 wenzelm parents: 
42287diff
changeset | 341 | fun merge_trtabs name tab1 tab2 = Symtab.merge Syntax_Ext.eq_trfun (tab1, tab2) | 
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
23631diff
changeset | 342 | handle Symtab.DUP c => err_dup_trfun name c; | 
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 343 | |
| 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 344 | |
| 5692 | 345 | (* print (ast) translations *) | 
| 346 | ||
| 42254 | 347 | fun apply_tr' tab c ctxt T args = | 
| 348 | let | |
| 349 | val fns = map fst (Symtab.lookup_list tab c); | |
| 350 | fun app_first [] = raise Match | |
| 351 | | app_first (f :: fs) = f ctxt T args handle Match => app_first fs; | |
| 352 | in app_first fns end; | |
| 353 | ||
| 354 | fun apply_ast_tr' tab c ctxt args = | |
| 355 | let | |
| 356 | val fns = map fst (Symtab.lookup_list tab c); | |
| 357 | fun app_first [] = raise Match | |
| 358 | | app_first (f :: fs) = f ctxt args handle Match => app_first fs; | |
| 359 | in app_first fns end; | |
| 42253 | 360 | |
| 42288 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 wenzelm parents: 
42287diff
changeset | 361 | fun update_tr'tab trfuns = fold_rev (Symtab.update_list Syntax_Ext.eq_trfun) trfuns; | 
| 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 wenzelm parents: 
42287diff
changeset | 362 | fun remove_tr'tab trfuns = fold (Symtab.remove_list Syntax_Ext.eq_trfun) trfuns; | 
| 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 wenzelm parents: 
42287diff
changeset | 363 | fun merge_tr'tabs tab1 tab2 = Symtab.merge_list Syntax_Ext.eq_trfun (tab1, tab2); | 
| 5692 | 364 | |
| 365 | ||
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 366 | |
| 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 367 | (** tables of translation rules **) | 
| 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 368 | |
| 5692 | 369 | type ruletab = (Ast.ast * Ast.ast) list Symtab.table; | 
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 370 | |
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19375diff
changeset | 371 | fun dest_ruletab tab = maps snd (Symtab.dest tab); | 
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 372 | |
| 25394 
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
 wenzelm parents: 
25387diff
changeset | 373 | val update_ruletab = fold_rev (fn r => Symtab.update_list (op =) (Ast.head_of_rule r, r)); | 
| 18931 | 374 | val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r)); | 
| 375 | fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2); | |
| 0 | 376 | |
| 377 | ||
| 378 | ||
| 379 | (** datatype syntax **) | |
| 380 | ||
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 381 | datatype syntax = | 
| 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 382 |   Syntax of {
 | 
| 42288 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 wenzelm parents: 
42287diff
changeset | 383 | input: Syntax_Ext.xprod list, | 
| 4703 | 384 | lexicon: Scan.lexicon, | 
| 45632 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 385 | gram: Parser.gram lazy, | 
| 42298 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 386 | consts: string Symtab.table, | 
| 2913 | 387 | prmodes: string list, | 
| 21772 | 388 | parse_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) Symtab.table, | 
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 389 | parse_ruletab: ruletab, | 
| 21772 | 390 | parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table, | 
| 42247 
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
 wenzelm parents: 
42245diff
changeset | 391 | print_trtab: ((Proof.context -> typ -> term list -> term) * stamp) list Symtab.table, | 
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 392 | print_ruletab: ruletab, | 
| 21772 | 393 | print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table, | 
| 17079 | 394 | prtabs: Printer.prtabs} * stamp; | 
| 0 | 395 | |
| 17079 | 396 | fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2; | 
| 397 | ||
| 45632 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 398 | fun force_syntax (Syntax ({gram, ...}, _)) = ignore (Lazy.force gram);
 | 
| 44802 
65c397cc44ec
explicit join_syntax ensures command transaction integrity of 'theory';
 wenzelm parents: 
44113diff
changeset | 399 | |
| 42298 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 400 | fun lookup_const (Syntax ({consts, ...}, _)) = Symtab.lookup consts;
 | 
| 17079 | 401 | fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode;
 | 
| 42251 | 402 | fun tokenize (Syntax ({lexicon, ...}, _)) = Lexicon.tokenize lexicon;
 | 
| 45641 | 403 | fun parse (Syntax ({gram, ...}, _)) = Parser.parse (Lazy.force gram);
 | 
| 14687 | 404 | |
| 42253 | 405 | fun parse_ast_translation (Syntax ({parse_ast_trtab, ...}, _)) = lookup_tr parse_ast_trtab;
 | 
| 406 | fun parse_translation (Syntax ({parse_trtab, ...}, _)) = lookup_tr parse_trtab;
 | |
| 42255 | 407 | fun parse_rules (Syntax ({parse_ruletab, ...}, _)) = Symtab.lookup_list parse_ruletab;
 | 
| 42254 | 408 | fun print_translation (Syntax ({print_trtab, ...}, _)) = apply_tr' print_trtab;
 | 
| 42255 | 409 | fun print_rules (Syntax ({print_ruletab, ...}, _)) = Symtab.lookup_list print_ruletab;
 | 
| 42254 | 410 | fun print_ast_translation (Syntax ({print_ast_trtab, ...}, _)) = apply_ast_tr' print_ast_trtab;
 | 
| 42253 | 411 | |
| 42255 | 412 | fun prtabs (Syntax ({prtabs, ...}, _)) = prtabs;
 | 
| 413 | ||
| 20784 | 414 | type mode = string * bool; | 
| 24970 | 415 | val mode_default = ("", true);
 | 
| 37146 
f652333bbf8e
renamed structure PrintMode to Print_Mode, keeping the old name as legacy alias for some time;
 wenzelm parents: 
36739diff
changeset | 416 | val mode_input = (Print_Mode.input, true); | 
| 15755 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 417 | |
| 18 | 418 | |
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 419 | (* empty_syntax *) | 
| 18 | 420 | |
| 17079 | 421 | val empty_syntax = Syntax | 
| 422 |   ({input = [],
 | |
| 4703 | 423 | lexicon = Scan.empty_lexicon, | 
| 45632 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 424 | gram = Lazy.value Parser.empty_gram, | 
| 42298 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 425 | consts = Symtab.empty, | 
| 2913 | 426 | prmodes = [], | 
| 5692 | 427 | parse_ast_trtab = Symtab.empty, | 
| 428 | parse_ruletab = Symtab.empty, | |
| 429 | parse_trtab = Symtab.empty, | |
| 430 | print_trtab = Symtab.empty, | |
| 431 | print_ruletab = Symtab.empty, | |
| 432 | print_ast_trtab = Symtab.empty, | |
| 17079 | 433 | prtabs = Printer.empty_prtabs}, stamp ()); | 
| 167 | 434 | |
| 435 | ||
| 25394 
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
 wenzelm parents: 
25387diff
changeset | 436 | (* update_syntax *) | 
| 167 | 437 | |
| 42298 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 438 | fun update_const (c, b) tab = | 
| 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 439 | if c = "" orelse (b = "" andalso (Lexicon.is_marked c orelse Symtab.defined tab c)) | 
| 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 440 | then tab | 
| 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 441 | else Symtab.update (c, b) tab; | 
| 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 442 | |
| 25394 
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
 wenzelm parents: 
25387diff
changeset | 443 | fun update_syntax (mode, inout) syn_ext (Syntax (tabs, _)) = | 
| 167 | 444 | let | 
| 42268 | 445 |     val {input, lexicon, gram, consts = consts1, prmodes, parse_ast_trtab, parse_ruletab,
 | 
| 446 | parse_trtab, print_trtab, print_ruletab, print_ast_trtab, prtabs} = tabs; | |
| 42288 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 wenzelm parents: 
42287diff
changeset | 447 |     val Syntax_Ext.Syn_Ext {xprods, consts = consts2, parse_ast_translation,
 | 
| 42268 | 448 | parse_rules, parse_translation, print_translation, print_rules, | 
| 449 | print_ast_translation} = syn_ext; | |
| 36208 
74c5e6e3c1d3
update_syntax: add new productions only once, to allow repeated local notation, for example;
 wenzelm parents: 
35668diff
changeset | 450 | val new_xprods = | 
| 
74c5e6e3c1d3
update_syntax: add new productions only once, to allow repeated local notation, for example;
 wenzelm parents: 
35668diff
changeset | 451 | if inout then distinct (op =) (filter_out (member (op =) input) xprods) else []; | 
| 19546 
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
 wenzelm parents: 
19482diff
changeset | 452 | fun if_inout xs = if inout then xs else []; | 
| 167 | 453 | in | 
| 17079 | 454 | Syntax | 
| 36208 
74c5e6e3c1d3
update_syntax: add new productions only once, to allow repeated local notation, for example;
 wenzelm parents: 
35668diff
changeset | 455 |     ({input = new_xprods @ input,
 | 
| 42288 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 wenzelm parents: 
42287diff
changeset | 456 | lexicon = fold Scan.extend_lexicon (Syntax_Ext.delims_of new_xprods) lexicon, | 
| 45632 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 457 | gram = Lazy.value (Parser.extend_gram new_xprods (Lazy.force gram)), | 
| 42298 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 458 | consts = fold update_const consts2 consts1, | 
| 42268 | 459 | prmodes = insert (op =) mode prmodes, | 
| 167 | 460 | parse_ast_trtab = | 
| 25394 
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
 wenzelm parents: 
25387diff
changeset | 461 | update_trtab "parse ast translation" (if_inout parse_ast_translation) parse_ast_trtab, | 
| 
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
 wenzelm parents: 
25387diff
changeset | 462 | parse_ruletab = update_ruletab (if_inout parse_rules) parse_ruletab, | 
| 
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
 wenzelm parents: 
25387diff
changeset | 463 | parse_trtab = update_trtab "parse translation" (if_inout parse_translation) parse_trtab, | 
| 
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
 wenzelm parents: 
25387diff
changeset | 464 | print_trtab = update_tr'tab print_translation print_trtab, | 
| 
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
 wenzelm parents: 
25387diff
changeset | 465 | print_ruletab = update_ruletab print_rules print_ruletab, | 
| 
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
 wenzelm parents: 
25387diff
changeset | 466 | print_ast_trtab = update_tr'tab print_ast_translation print_ast_trtab, | 
| 
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
 wenzelm parents: 
25387diff
changeset | 467 | prtabs = Printer.update_prtabs mode xprods prtabs}, stamp ()) | 
| 18 | 468 | end; | 
| 469 | ||
| 15755 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 470 | |
| 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 471 | (* remove_syntax *) | 
| 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 472 | |
| 17079 | 473 | fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) = | 
| 15755 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 474 | let | 
| 42288 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 wenzelm parents: 
42287diff
changeset | 475 |     val Syntax_Ext.Syn_Ext {xprods, consts = _, parse_ast_translation, parse_rules,
 | 
| 42268 | 476 | parse_translation, print_translation, print_rules, print_ast_translation} = syn_ext; | 
| 477 |     val {input, lexicon, gram, consts, prmodes, parse_ast_trtab, parse_ruletab,
 | |
| 478 | parse_trtab, print_trtab, print_ruletab, print_ast_trtab, prtabs} = tabs; | |
| 19300 | 479 | val input' = if inout then subtract (op =) xprods input else input; | 
| 25394 
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
 wenzelm parents: 
25387diff
changeset | 480 | val changed = length input <> length input'; | 
| 19546 
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
 wenzelm parents: 
19482diff
changeset | 481 | fun if_inout xs = if inout then xs else []; | 
| 15755 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 482 | in | 
| 17079 | 483 | Syntax | 
| 484 |     ({input = input',
 | |
| 42288 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 wenzelm parents: 
42287diff
changeset | 485 | lexicon = if changed then Scan.make_lexicon (Syntax_Ext.delims_of input') else lexicon, | 
| 45632 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 486 | gram = if changed then Lazy.value (Parser.make_gram input') else gram, | 
| 15755 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 487 | consts = consts, | 
| 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 488 | prmodes = prmodes, | 
| 19546 
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
 wenzelm parents: 
19482diff
changeset | 489 | parse_ast_trtab = remove_trtab (if_inout parse_ast_translation) parse_ast_trtab, | 
| 
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
 wenzelm parents: 
19482diff
changeset | 490 | parse_ruletab = remove_ruletab (if_inout parse_rules) parse_ruletab, | 
| 
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
 wenzelm parents: 
19482diff
changeset | 491 | parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab, | 
| 15755 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 492 | print_trtab = remove_tr'tab print_translation print_trtab, | 
| 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 493 | print_ruletab = remove_ruletab print_rules print_ruletab, | 
| 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 494 | print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab, | 
| 17079 | 495 | prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ()) | 
| 15755 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 496 | end; | 
| 14904 
7d8dc92fcb7f
removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
 wenzelm parents: 
14798diff
changeset | 497 | |
| 18 | 498 | |
| 45632 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 499 | (* merge_syntax *) | 
| 0 | 500 | |
| 45632 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 501 | fun merge_syntax (Syntax (tabs1, _), Syntax (tabs2, _)) = | 
| 0 | 502 | let | 
| 15755 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 503 |     val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1,
 | 
| 42268 | 504 | prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1, | 
| 505 | parse_trtab = parse_trtab1, print_trtab = print_trtab1, print_ruletab = print_ruletab1, | |
| 506 | print_ast_trtab = print_ast_trtab1, prtabs = prtabs1} = tabs1; | |
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 507 | |
| 15755 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 508 |     val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2,
 | 
| 42268 | 509 | prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2, | 
| 510 | parse_trtab = parse_trtab2, print_trtab = print_trtab2, print_ruletab = print_ruletab2, | |
| 511 | print_ast_trtab = print_ast_trtab2, prtabs = prtabs2} = tabs2; | |
| 45632 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 512 | |
| 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 513 | val (input', gram') = | 
| 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 514 | (case subtract (op =) input1 input2 of | 
| 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 515 | [] => (input1, gram1) | 
| 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 516 | | new_xprods2 => | 
| 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 517 | if subset (op =) (input1, input2) then (input2, gram2) | 
| 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 518 | else | 
| 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 519 | let | 
| 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 520 | val input' = new_xprods2 @ input1; | 
| 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 521 | val gram' = Lazy.lazy (fn () => Parser.make_gram input'); | 
| 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 522 | in (input', gram') end); | 
| 0 | 523 | in | 
| 17079 | 524 | Syntax | 
| 45632 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 525 |     ({input = input',
 | 
| 27768 | 526 | lexicon = Scan.merge_lexicons (lexicon1, lexicon2), | 
| 45632 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 527 | gram = gram', | 
| 42298 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 528 | consts = Symtab.merge (K true) (consts1, consts2), | 
| 18921 | 529 | prmodes = Library.merge (op =) (prmodes1, prmodes2), | 
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 530 | parse_ast_trtab = | 
| 15755 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 531 | merge_trtabs "parse ast translation" parse_ast_trtab1 parse_ast_trtab2, | 
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 532 | parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2, | 
| 15755 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 533 | parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2, | 
| 5692 | 534 | print_trtab = merge_tr'tabs print_trtab1 print_trtab2, | 
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 535 | print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2, | 
| 5692 | 536 | print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2, | 
| 17079 | 537 | prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ()) | 
| 0 | 538 | end; | 
| 539 | ||
| 540 | ||
| 4887 | 541 | |
| 15759 | 542 | (** print syntax **) | 
| 543 | ||
| 544 | local | |
| 0 | 545 | |
| 260 | 546 | fun pretty_strs_qs name strs = | 
| 28840 | 547 | Pretty.strs (name :: map quote (sort_strings strs)); | 
| 0 | 548 | |
| 17079 | 549 | fun pretty_gram (Syntax (tabs, _)) = | 
| 0 | 550 | let | 
| 32784 | 551 |     val {lexicon, prmodes, gram, ...} = tabs;
 | 
| 28375 | 552 | val prmodes' = sort_strings (filter_out (fn s => s = "") prmodes); | 
| 0 | 553 | in | 
| 8720 | 554 | [pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon), | 
| 45632 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 555 | Pretty.big_list "prods:" (Parser.pretty_gram (Lazy.force gram)), | 
| 8720 | 556 | pretty_strs_qs "print modes:" prmodes'] | 
| 0 | 557 | end; | 
| 558 | ||
| 17079 | 559 | fun pretty_trans (Syntax (tabs, _)) = | 
| 0 | 560 | let | 
| 42298 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 561 | fun pretty_tab name tab = | 
| 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 562 | pretty_strs_qs name (sort_strings (Symtab.keys tab)); | 
| 0 | 563 | |
| 260 | 564 | fun pretty_ruletab name tab = | 
| 51580 
64ef8260dc60
Pretty.item markup for improved readability of lists of items;
 wenzelm parents: 
50201diff
changeset | 565 | Pretty.big_list name (map (Pretty.item o single o Ast.pretty_rule) (dest_ruletab tab)); | 
| 0 | 566 | |
| 237 
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
 wenzelm parents: 
175diff
changeset | 567 |     val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab,
 | 
| 42268 | 568 | print_ruletab, print_ast_trtab, ...} = tabs; | 
| 0 | 569 | in | 
| 42298 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 570 | [pretty_tab "consts:" consts, | 
| 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 571 | pretty_tab "parse_ast_translation:" parse_ast_trtab, | 
| 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 572 | pretty_ruletab "parse_rules:" parse_ruletab, | 
| 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 573 | pretty_tab "parse_translation:" parse_trtab, | 
| 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 574 | pretty_tab "print_translation:" print_trtab, | 
| 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 575 | pretty_ruletab "print_rules:" print_ruletab, | 
| 
d622145603ee
more accurate markup for syntax consts, notably binders which point back to the original logical entity;
 wenzelm parents: 
42297diff
changeset | 576 | pretty_tab "print_ast_translation:" print_ast_trtab] | 
| 0 | 577 | end; | 
| 578 | ||
| 15759 | 579 | in | 
| 0 | 580 | |
| 56334 
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
 wenzelm parents: 
55829diff
changeset | 581 | fun print_gram syn = Pretty.writeln_chunks (pretty_gram syn); | 
| 
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
 wenzelm parents: 
55829diff
changeset | 582 | fun print_trans syn = Pretty.writeln_chunks (pretty_trans syn); | 
| 
6b3739fee456
some shortcuts for chunks, which sometimes avoid bulky string output;
 wenzelm parents: 
55829diff
changeset | 583 | fun print_syntax syn = Pretty.writeln_chunks (pretty_gram syn @ pretty_trans syn); | 
| 0 | 584 | |
| 15759 | 585 | end; | 
| 0 | 586 | |
| 587 | ||
| 26951 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 588 | (* reconstructing infixes -- educated guessing *) | 
| 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 589 | |
| 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 590 | fun guess_infix (Syntax ({gram, ...}, _)) c =
 | 
| 45632 
b23c42b9f78a
prefer Parser.make_gram over Parser.merge_gram, to approximate n-ary merges on theory import;
 wenzelm parents: 
45490diff
changeset | 591 | (case Parser.guess_infix_lr (Lazy.force gram) c of | 
| 26951 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 592 | SOME (s, l, r, j) => SOME | 
| 35130 | 593 | (if l then Mixfix.Infixl (s, j) | 
| 594 | else if r then Mixfix.Infixr (s, j) | |
| 595 | else Mixfix.Infix (s, j)) | |
| 26951 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 596 | | NONE => NONE); | 
| 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 597 | |
| 
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26704diff
changeset | 598 | |
| 0 | 599 | |
| 1158 | 600 | (** prepare translation rules **) | 
| 601 | ||
| 42204 | 602 | (* rules *) | 
| 603 | ||
| 1158 | 604 | datatype 'a trrule = | 
| 42204 | 605 | Parse_Rule of 'a * 'a | | 
| 606 | Print_Rule of 'a * 'a | | |
| 607 | Parse_Print_Rule of 'a * 'a; | |
| 888 
3a1de9454d13
improved read_xrules: patterns no longer read twice;
 wenzelm parents: 
882diff
changeset | 608 | |
| 42204 | 609 | fun map_trrule f (Parse_Rule (x, y)) = Parse_Rule (f x, f y) | 
| 610 | | map_trrule f (Print_Rule (x, y)) = Print_Rule (f x, f y) | |
| 611 | | map_trrule f (Parse_Print_Rule (x, y)) = Parse_Print_Rule (f x, f y); | |
| 1158 | 612 | |
| 42204 | 613 | fun parse_rule (Parse_Rule pats) = SOME pats | 
| 614 | | parse_rule (Print_Rule _) = NONE | |
| 615 | | parse_rule (Parse_Print_Rule pats) = SOME pats; | |
| 1158 | 616 | |
| 42204 | 617 | fun print_rule (Parse_Rule _) = NONE | 
| 618 | | print_rule (Print_Rule pats) = SOME (swap pats) | |
| 619 | | print_rule (Parse_Print_Rule pats) = SOME (swap pats); | |
| 1158 | 620 | |
| 621 | ||
| 42204 | 622 | (* check_rules *) | 
| 623 | ||
| 19262 | 624 | local | 
| 625 | ||
| 42048 
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
 wenzelm parents: 
42044diff
changeset | 626 | fun check_rule rule = | 
| 5692 | 627 | (case Ast.rule_error rule of | 
| 15531 | 628 | SOME msg => | 
| 1158 | 629 |       error ("Error in syntax translation rule: " ^ msg ^ "\n" ^
 | 
| 42048 
afd11ca8e018
support for encoded positions (for id_position, longid_position) as pseudo type-constraints -- still inactive;
 wenzelm parents: 
42044diff
changeset | 630 | Pretty.string_of (Ast.pretty_rule rule)) | 
| 15531 | 631 | | NONE => rule); | 
| 888 
3a1de9454d13
improved read_xrules: patterns no longer read twice;
 wenzelm parents: 
882diff
changeset | 632 | |
| 42204 | 633 | in | 
| 634 | ||
| 635 | fun check_rules rules = | |
| 636 | (map check_rule (map_filter parse_rule rules), | |
| 637 | map check_rule (map_filter print_rule rules)); | |
| 638 | ||
| 639 | end; | |
| 640 | ||
| 641 | ||
| 18 | 642 | |
| 19262 | 643 | (** modify syntax **) | 
| 383 
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
 wenzelm parents: 
330diff
changeset | 644 | |
| 52143 | 645 | val update_trfuns = update_syntax mode_default o Syntax_Ext.syn_ext_trfuns; | 
| 5692 | 646 | |
| 35412 
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
 wenzelm parents: 
35394diff
changeset | 647 | fun update_type_gram add prmode decls = | 
| 
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
 wenzelm parents: 
35394diff
changeset | 648 | (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_types decls); | 
| 25387 | 649 | |
| 35412 
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
 wenzelm parents: 
35394diff
changeset | 650 | fun update_const_gram add is_logtype prmode decls = | 
| 
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
 wenzelm parents: 
35394diff
changeset | 651 | (if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_consts is_logtype decls); | 
| 15755 
50ac97ebe7d8
expect translations functions to be stamped already;
 wenzelm parents: 
15574diff
changeset | 652 | |
| 52143 | 653 | val update_trrules = update_syntax mode_default o Syntax_Ext.syn_ext_rules o check_rules; | 
| 42288 
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
 wenzelm parents: 
42287diff
changeset | 654 | val remove_trrules = remove_syntax mode_default o Syntax_Ext.syn_ext_rules o check_rules; | 
| 5692 | 655 | |
| 42476 | 656 | |
| 657 | open Lexicon.Syntax; | |
| 658 | ||
| 0 | 659 | end; | 
| 5692 | 660 |