author | wenzelm |
Mon, 31 May 2010 21:06:57 +0200 | |
changeset 37216 | 3165bc303f66 |
parent 37146 | f652333bbf8e |
child 37684 | d123b1e08856 |
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 |
||
260 | 8 |
signature BASIC_SYNTAX = |
2383 | 9 |
sig |
260 | 10 |
include AST0 |
556 | 11 |
include SYN_TRANS0 |
12 |
include MIXFIX0 |
|
260 | 13 |
include PRINTER0 |
2383 | 14 |
end; |
260 | 15 |
|
0 | 16 |
signature SYNTAX = |
2383 | 17 |
sig |
260 | 18 |
include AST1 |
0 | 19 |
include LEXICON0 |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
20 |
include SYN_EXT0 |
0 | 21 |
include TYPE_EXT0 |
556 | 22 |
include SYN_TRANS1 |
23 |
include MIXFIX1 |
|
0 | 24 |
include PRINTER0 |
556 | 25 |
type syntax |
17079 | 26 |
val eq_syntax: syntax * syntax -> bool |
14687 | 27 |
val is_keyword: syntax -> string -> bool |
20784 | 28 |
type mode |
24970 | 29 |
val mode_default: mode |
30 |
val mode_input: mode |
|
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
31 |
val merge_syntaxes: syntax -> syntax -> syntax |
35668 | 32 |
val basic_syntax: syntax |
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
33 |
val basic_nonterms: string list |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
34 |
val print_gram: syntax -> unit |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
35 |
val print_trans: syntax -> unit |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
36 |
val print_syntax: syntax -> unit |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
37 |
val guess_infix: syntax -> string -> mixfix option |
30573 | 38 |
val read_token: string -> Symbol_Pos.T list * Position.T |
32738 | 39 |
val ambiguity_is_error: bool Unsynchronized.ref |
40 |
val ambiguity_level: int Unsynchronized.ref |
|
41 |
val ambiguity_limit: int Unsynchronized.ref |
|
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
42 |
val standard_parse_term: Pretty.pp -> (term -> string option) -> |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
43 |
(((string * int) * sort) list -> string * int -> Term.sort) -> |
35429
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
44 |
(string -> bool * string) -> (string -> string option) -> Proof.context -> |
30573 | 45 |
(string -> bool) -> syntax -> typ -> Symbol_Pos.T list * Position.T -> term |
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
46 |
val standard_parse_typ: Proof.context -> syntax -> |
35429
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
47 |
((indexname * sort) list -> indexname -> sort) -> Symbol_Pos.T list * Position.T -> typ |
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
48 |
val standard_parse_sort: Proof.context -> syntax -> Symbol_Pos.T list * Position.T -> sort |
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
49 |
datatype 'a trrule = |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
50 |
ParseRule of 'a * 'a | |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
51 |
PrintRule of 'a * 'a | |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
52 |
ParsePrintRule of 'a * 'a |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
53 |
val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule |
35111 | 54 |
val is_const: syntax -> string -> bool |
35429
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
55 |
val standard_unparse_term: {structs: string list, fixes: string list} -> |
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
56 |
{extern_class: string -> xstring, extern_type: string -> xstring, |
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
57 |
extern_const: string -> xstring} -> Proof.context -> syntax -> bool -> term -> Pretty.T |
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
58 |
val standard_unparse_typ: {extern_class: string -> xstring, extern_type: string -> xstring} -> |
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
59 |
Proof.context -> syntax -> typ -> Pretty.T |
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
60 |
val standard_unparse_sort: {extern_class: string -> xstring} -> |
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
61 |
Proof.context -> syntax -> sort -> Pretty.T |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
62 |
val update_trfuns: |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
63 |
(string * ((ast list -> ast) * stamp)) list * |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
64 |
(string * ((term list -> term) * stamp)) list * |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
65 |
(string * ((bool -> typ -> term list -> term) * stamp)) list * |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
66 |
(string * ((ast list -> ast) * stamp)) list -> syntax -> syntax |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
67 |
val update_advanced_trfuns: |
21772 | 68 |
(string * ((Proof.context -> ast list -> ast) * stamp)) list * |
69 |
(string * ((Proof.context -> term list -> term) * stamp)) list * |
|
70 |
(string * ((Proof.context -> bool -> typ -> term list -> term) * stamp)) list * |
|
71 |
(string * ((Proof.context -> ast list -> ast) * stamp)) list -> syntax -> syntax |
|
26704
51ee753cc2e3
token translations: context dependent, result Pretty.T;
wenzelm
parents:
26684
diff
changeset
|
72 |
val extend_tokentrfuns: (string * string * (Proof.context -> string -> Pretty.T)) list -> |
51ee753cc2e3
token translations: context dependent, result Pretty.T;
wenzelm
parents:
26684
diff
changeset
|
73 |
syntax -> syntax |
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35394
diff
changeset
|
74 |
val update_type_gram: bool -> mode -> (string * typ * mixfix) list -> syntax -> syntax |
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35394
diff
changeset
|
75 |
val update_const_gram: bool -> (string -> bool) -> |
20784 | 76 |
mode -> (string * typ * mixfix) list -> syntax -> syntax |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
77 |
val update_trrules: Proof.context -> (string -> bool) -> syntax -> |
14904
7d8dc92fcb7f
removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
wenzelm
parents:
14798
diff
changeset
|
78 |
(string * string) trrule list -> syntax -> syntax |
21772 | 79 |
val remove_trrules: Proof.context -> (string -> bool) -> syntax -> |
19262 | 80 |
(string * string) trrule list -> syntax -> syntax |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
81 |
val update_trrules_i: ast trrule list -> syntax -> syntax |
19262 | 82 |
val remove_trrules_i: ast trrule list -> syntax -> syntax |
30573 | 83 |
val parse_token: Markup.T -> string -> Symbol_Pos.T list * Position.T |
24263 | 84 |
val parse_sort: Proof.context -> string -> sort |
85 |
val parse_typ: Proof.context -> string -> typ |
|
86 |
val parse_term: Proof.context -> string -> term |
|
87 |
val parse_prop: Proof.context -> string -> term |
|
24768 | 88 |
val unparse_sort: Proof.context -> sort -> Pretty.T |
24923 | 89 |
val unparse_classrel: Proof.context -> class list -> Pretty.T |
90 |
val unparse_arity: Proof.context -> arity -> Pretty.T |
|
24768 | 91 |
val unparse_typ: Proof.context -> typ -> Pretty.T |
92 |
val unparse_term: Proof.context -> term -> Pretty.T |
|
93 |
val install_operations: |
|
94 |
{parse_sort: Proof.context -> string -> sort, |
|
95 |
parse_typ: Proof.context -> string -> typ, |
|
96 |
parse_term: Proof.context -> string -> term, |
|
97 |
parse_prop: Proof.context -> string -> term, |
|
98 |
unparse_sort: Proof.context -> sort -> Pretty.T, |
|
99 |
unparse_typ: Proof.context -> typ -> Pretty.T, |
|
100 |
unparse_term: Proof.context -> term -> Pretty.T} -> unit |
|
101 |
val print_checks: Proof.context -> unit |
|
102 |
val add_typ_check: int -> string -> |
|
25060 | 103 |
(typ list -> Proof.context -> (typ list * Proof.context) option) -> |
24768 | 104 |
Context.generic -> Context.generic |
105 |
val add_term_check: int -> string -> |
|
25060 | 106 |
(term list -> Proof.context -> (term list * Proof.context) option) -> |
24768 | 107 |
Context.generic -> Context.generic |
108 |
val add_typ_uncheck: int -> string -> |
|
25060 | 109 |
(typ list -> Proof.context -> (typ list * Proof.context) option) -> |
24768 | 110 |
Context.generic -> Context.generic |
111 |
val add_term_uncheck: int -> string -> |
|
25060 | 112 |
(term list -> Proof.context -> (term list * Proof.context) option) -> |
24768 | 113 |
Context.generic -> Context.generic |
24488 | 114 |
val check_sort: Proof.context -> sort -> sort |
24512 | 115 |
val check_typ: Proof.context -> typ -> typ |
116 |
val check_term: Proof.context -> term -> term |
|
117 |
val check_prop: Proof.context -> term -> term |
|
24488 | 118 |
val check_typs: Proof.context -> typ list -> typ list |
24263 | 119 |
val check_terms: Proof.context -> term list -> term list |
120 |
val check_props: Proof.context -> term list -> term list |
|
24923 | 121 |
val uncheck_sort: Proof.context -> sort -> sort |
122 |
val uncheck_arity: Proof.context -> arity -> arity |
|
123 |
val uncheck_classrel: Proof.context -> class list -> class list |
|
24768 | 124 |
val uncheck_typs: Proof.context -> typ list -> typ list |
125 |
val uncheck_terms: Proof.context -> term list -> term list |
|
24263 | 126 |
val read_sort: Proof.context -> string -> sort |
127 |
val read_typ: Proof.context -> string -> typ |
|
24488 | 128 |
val read_term: Proof.context -> string -> term |
129 |
val read_prop: Proof.context -> string -> term |
|
24263 | 130 |
val read_terms: Proof.context -> string list -> term list |
131 |
val read_props: Proof.context -> string list -> term list |
|
24709 | 132 |
val read_sort_global: theory -> string -> sort |
133 |
val read_typ_global: theory -> string -> typ |
|
134 |
val read_term_global: theory -> string -> term |
|
135 |
val read_prop_global: theory -> string -> term |
|
24923 | 136 |
val pretty_term: Proof.context -> term -> Pretty.T |
137 |
val pretty_typ: Proof.context -> typ -> Pretty.T |
|
138 |
val pretty_sort: Proof.context -> sort -> Pretty.T |
|
139 |
val pretty_classrel: Proof.context -> class list -> Pretty.T |
|
140 |
val pretty_arity: Proof.context -> arity -> Pretty.T |
|
141 |
val string_of_term: Proof.context -> term -> string |
|
142 |
val string_of_typ: Proof.context -> typ -> string |
|
143 |
val string_of_sort: Proof.context -> sort -> string |
|
144 |
val string_of_classrel: Proof.context -> class list -> string |
|
145 |
val string_of_arity: Proof.context -> arity -> string |
|
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
146 |
val is_pretty_global: Proof.context -> bool |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
147 |
val set_pretty_global: bool -> Proof.context -> Proof.context |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
148 |
val init_pretty_global: theory -> Proof.context |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
149 |
val pretty_term_global: theory -> term -> Pretty.T |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
150 |
val pretty_typ_global: theory -> typ -> Pretty.T |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
151 |
val pretty_sort_global: theory -> sort -> Pretty.T |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
152 |
val string_of_term_global: theory -> term -> string |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
153 |
val string_of_typ_global: theory -> typ -> string |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
154 |
val string_of_sort_global: theory -> sort -> string |
24970 | 155 |
val pp: Proof.context -> Pretty.pp |
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
156 |
val pp_global: theory -> Pretty.pp |
2383 | 157 |
end; |
0 | 158 |
|
12094 | 159 |
structure Syntax: SYNTAX = |
0 | 160 |
struct |
161 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
162 |
(** tables of translation functions **) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
163 |
|
5692 | 164 |
(* parse (ast) translations *) |
165 |
||
17412 | 166 |
fun lookup_tr tab c = Option.map fst (Symtab.lookup tab c); |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
167 |
|
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23631
diff
changeset
|
168 |
fun err_dup_trfun name c = |
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23631
diff
changeset
|
169 |
error ("More than one " ^ name ^ " for " ^ quote c); |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
170 |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
171 |
fun remove_trtab trfuns = fold (Symtab.remove Syn_Ext.eq_trfun) trfuns; |
21536
f119c730f509
extend_trtab: allow identical trfuns to be overwritten;
wenzelm
parents:
20784
diff
changeset
|
172 |
|
29004 | 173 |
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:
23631
diff
changeset
|
174 |
handle Symtab.DUP c => err_dup_trfun name c; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
175 |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
176 |
fun merge_trtabs name tab1 tab2 = Symtab.merge Syn_Ext.eq_trfun (tab1, tab2) |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23631
diff
changeset
|
177 |
handle Symtab.DUP c => err_dup_trfun name c; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
178 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
179 |
|
5692 | 180 |
(* print (ast) translations *) |
181 |
||
18931 | 182 |
fun lookup_tr' tab c = map fst (Symtab.lookup_list tab c); |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
183 |
fun update_tr'tab trfuns = fold_rev (Symtab.update_list Syn_Ext.eq_trfun) trfuns; |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
184 |
fun remove_tr'tab trfuns = fold (Symtab.remove_list Syn_Ext.eq_trfun) trfuns; |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
185 |
fun merge_tr'tabs tab1 tab2 = Symtab.merge_list Syn_Ext.eq_trfun (tab1, tab2); |
5692 | 186 |
|
187 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
188 |
|
2700 | 189 |
(** tables of token translation functions **) |
190 |
||
191 |
fun lookup_tokentr tabs modes = |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19375
diff
changeset
|
192 |
let val trs = distinct (eq_fst (op =)) (maps (these o AList.lookup (op =) tabs) (modes @ [""])) |
17314 | 193 |
in fn c => Option.map fst (AList.lookup (op =) trs c) end; |
2700 | 194 |
|
195 |
fun merge_tokentrtabs tabs1 tabs2 = |
|
196 |
let |
|
197 |
fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2; |
|
198 |
||
4703 | 199 |
fun name (s, _) = implode (tl (Symbol.explode s)); |
2706 | 200 |
|
2700 | 201 |
fun merge mode = |
202 |
let |
|
17213 | 203 |
val trs1 = these (AList.lookup (op =) tabs1 mode); |
204 |
val trs2 = these (AList.lookup (op =) tabs2 mode); |
|
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
18977
diff
changeset
|
205 |
val trs = distinct eq_tr (trs1 @ trs2); |
2700 | 206 |
in |
18964 | 207 |
(case duplicates (eq_fst (op =)) trs of |
2700 | 208 |
[] => (mode, trs) |
209 |
| dups => error ("More than one token translation function in mode " ^ |
|
2706 | 210 |
quote mode ^ " for " ^ commas_quote (map name dups))) |
2700 | 211 |
end; |
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
18977
diff
changeset
|
212 |
in map merge (distinct (op =) (map fst (tabs1 @ tabs2))) end; |
2700 | 213 |
|
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
214 |
fun extend_tokentrtab tokentrs tabs = |
2700 | 215 |
let |
17213 | 216 |
fun ins_tokentr (m, c, f) = |
217 |
AList.default (op =) (m, []) |
|
218 |
#> AList.map_entry (op =) m (cons ("_" ^ c, (f, stamp ()))); |
|
15759 | 219 |
in merge_tokentrtabs tabs (fold ins_tokentr tokentrs []) end; |
2700 | 220 |
|
221 |
||
222 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
223 |
(** tables of translation rules **) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
224 |
|
5692 | 225 |
type ruletab = (Ast.ast * Ast.ast) list Symtab.table; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
226 |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19375
diff
changeset
|
227 |
fun dest_ruletab tab = maps snd (Symtab.dest tab); |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
228 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
229 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
230 |
(* empty, update, merge ruletabs *) |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
231 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
232 |
val update_ruletab = fold_rev (fn r => Symtab.update_list (op =) (Ast.head_of_rule r, r)); |
18931 | 233 |
val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r)); |
234 |
fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2); |
|
0 | 235 |
|
236 |
||
237 |
||
238 |
(** datatype syntax **) |
|
239 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
240 |
datatype syntax = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
241 |
Syntax of { |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
242 |
input: Syn_Ext.xprod list, |
4703 | 243 |
lexicon: Scan.lexicon, |
5692 | 244 |
gram: Parser.gram, |
18 | 245 |
consts: string list, |
2913 | 246 |
prmodes: string list, |
21772 | 247 |
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:
175
diff
changeset
|
248 |
parse_ruletab: ruletab, |
21772 | 249 |
parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table, |
250 |
print_trtab: ((Proof.context -> bool -> typ -> term list -> term) * stamp) list Symtab.table, |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
251 |
print_ruletab: ruletab, |
21772 | 252 |
print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table, |
26704
51ee753cc2e3
token translations: context dependent, result Pretty.T;
wenzelm
parents:
26684
diff
changeset
|
253 |
tokentrtab: (string * (string * ((Proof.context -> string -> Pretty.T) * stamp)) list) list, |
17079 | 254 |
prtabs: Printer.prtabs} * stamp; |
0 | 255 |
|
17079 | 256 |
fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2; |
257 |
||
258 |
fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode; |
|
14687 | 259 |
|
20784 | 260 |
type mode = string * bool; |
24970 | 261 |
val mode_default = ("", true); |
37146
f652333bbf8e
renamed structure PrintMode to Print_Mode, keeping the old name as legacy alias for some time;
wenzelm
parents:
36739
diff
changeset
|
262 |
val mode_input = (Print_Mode.input, true); |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
263 |
|
18 | 264 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
265 |
(* empty_syntax *) |
18 | 266 |
|
17079 | 267 |
val empty_syntax = Syntax |
268 |
({input = [], |
|
4703 | 269 |
lexicon = Scan.empty_lexicon, |
5692 | 270 |
gram = Parser.empty_gram, |
167 | 271 |
consts = [], |
2913 | 272 |
prmodes = [], |
5692 | 273 |
parse_ast_trtab = Symtab.empty, |
274 |
parse_ruletab = Symtab.empty, |
|
275 |
parse_trtab = Symtab.empty, |
|
276 |
print_trtab = Symtab.empty, |
|
277 |
print_ruletab = Symtab.empty, |
|
278 |
print_ast_trtab = Symtab.empty, |
|
2700 | 279 |
tokentrtab = [], |
17079 | 280 |
prtabs = Printer.empty_prtabs}, stamp ()); |
167 | 281 |
|
282 |
||
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
283 |
(* update_syntax *) |
167 | 284 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
285 |
fun update_syntax (mode, inout) syn_ext (Syntax (tabs, _)) = |
167 | 286 |
let |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
287 |
val {input, lexicon, gram, consts = consts1, prmodes = prmodes1, |
2366 | 288 |
parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab, |
2700 | 289 |
print_ast_trtab, tokentrtab, prtabs} = tabs; |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
290 |
val Syn_Ext.Syn_Ext {xprods, consts = consts2, prmodes = prmodes2, |
2913 | 291 |
parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules, |
2700 | 292 |
print_ast_translation, token_translation} = syn_ext; |
36208
74c5e6e3c1d3
update_syntax: add new productions only once, to allow repeated local notation, for example;
wenzelm
parents:
35668
diff
changeset
|
293 |
val new_xprods = |
74c5e6e3c1d3
update_syntax: add new productions only once, to allow repeated local notation, for example;
wenzelm
parents:
35668
diff
changeset
|
294 |
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:
19482
diff
changeset
|
295 |
fun if_inout xs = if inout then xs else []; |
167 | 296 |
in |
17079 | 297 |
Syntax |
36208
74c5e6e3c1d3
update_syntax: add new productions only once, to allow repeated local notation, for example;
wenzelm
parents:
35668
diff
changeset
|
298 |
({input = new_xprods @ input, |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
299 |
lexicon = fold Scan.extend_lexicon (Syn_Ext.delims_of new_xprods) lexicon, |
36208
74c5e6e3c1d3
update_syntax: add new productions only once, to allow repeated local notation, for example;
wenzelm
parents:
35668
diff
changeset
|
300 |
gram = Parser.extend_gram gram new_xprods, |
35429
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
301 |
consts = Library.merge (op =) (consts1, filter_out Lexicon.is_marked consts2), |
18921 | 302 |
prmodes = insert (op =) mode (Library.merge (op =) (prmodes1, prmodes2)), |
167 | 303 |
parse_ast_trtab = |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
304 |
update_trtab "parse ast translation" (if_inout parse_ast_translation) parse_ast_trtab, |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
305 |
parse_ruletab = update_ruletab (if_inout parse_rules) parse_ruletab, |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
306 |
parse_trtab = update_trtab "parse translation" (if_inout parse_translation) parse_trtab, |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
307 |
print_trtab = update_tr'tab print_translation print_trtab, |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
308 |
print_ruletab = update_ruletab print_rules print_ruletab, |
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
309 |
print_ast_trtab = update_tr'tab print_ast_translation print_ast_trtab, |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
310 |
tokentrtab = extend_tokentrtab token_translation tokentrtab, |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
311 |
prtabs = Printer.update_prtabs mode xprods prtabs}, stamp ()) |
18 | 312 |
end; |
313 |
||
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
314 |
|
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
315 |
(* remove_syntax *) |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
316 |
|
17079 | 317 |
fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) = |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
318 |
let |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
319 |
val Syn_Ext.Syn_Ext {xprods, consts = _, prmodes = _, |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
320 |
parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
321 |
print_ast_translation, token_translation = _} = syn_ext; |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
322 |
val {input, lexicon, gram, consts, prmodes, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
323 |
parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
324 |
print_ast_trtab, tokentrtab, prtabs} = tabs; |
19300 | 325 |
val input' = if inout then subtract (op =) xprods input else input; |
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
326 |
val changed = length input <> length input'; |
19546
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
327 |
fun if_inout xs = if inout then xs else []; |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
328 |
in |
17079 | 329 |
Syntax |
330 |
({input = input', |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
331 |
lexicon = if changed then Scan.make_lexicon (Syn_Ext.delims_of input') else lexicon, |
25387 | 332 |
gram = if changed then Parser.make_gram input' else gram, |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
333 |
consts = consts, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
334 |
prmodes = prmodes, |
19546
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
335 |
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:
19482
diff
changeset
|
336 |
parse_ruletab = remove_ruletab (if_inout parse_rules) parse_ruletab, |
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
337 |
parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab, |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
338 |
print_trtab = remove_tr'tab print_translation print_trtab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
339 |
print_ruletab = remove_ruletab print_rules print_ruletab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
340 |
print_ast_trtab = remove_tr'tab print_ast_translation print_ast_trtab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
341 |
tokentrtab = tokentrtab, |
17079 | 342 |
prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ()) |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
343 |
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:
14798
diff
changeset
|
344 |
|
18 | 345 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
346 |
(* merge_syntaxes *) |
0 | 347 |
|
17079 | 348 |
fun merge_syntaxes (Syntax (tabs1, _)) (Syntax (tabs2, _)) = |
0 | 349 |
let |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
350 |
val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
351 |
prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
352 |
parse_ruletab = parse_ruletab1, parse_trtab = parse_trtab1, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
353 |
print_trtab = print_trtab1, print_ruletab = print_ruletab1, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
354 |
print_ast_trtab = print_ast_trtab1, tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
355 |
|
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
356 |
val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
357 |
prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
358 |
parse_ruletab = parse_ruletab2, parse_trtab = parse_trtab2, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
359 |
print_trtab = print_trtab2, print_ruletab = print_ruletab2, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
360 |
print_ast_trtab = print_ast_trtab2, tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2; |
0 | 361 |
in |
17079 | 362 |
Syntax |
18921 | 363 |
({input = Library.merge (op =) (input1, input2), |
27768 | 364 |
lexicon = Scan.merge_lexicons (lexicon1, lexicon2), |
5692 | 365 |
gram = Parser.merge_grams gram1 gram2, |
18428 | 366 |
consts = sort_distinct string_ord (consts1 @ consts2), |
18921 | 367 |
prmodes = Library.merge (op =) (prmodes1, prmodes2), |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
368 |
parse_ast_trtab = |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
369 |
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:
175
diff
changeset
|
370 |
parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2, |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
371 |
parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2, |
5692 | 372 |
print_trtab = merge_tr'tabs print_trtab1 print_trtab2, |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
373 |
print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2, |
5692 | 374 |
print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2, |
2700 | 375 |
tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2, |
17079 | 376 |
prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ()) |
0 | 377 |
end; |
378 |
||
379 |
||
18720 | 380 |
(* basic syntax *) |
260 | 381 |
|
35668 | 382 |
val basic_syntax = |
17168 | 383 |
empty_syntax |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
384 |
|> update_syntax mode_default Type_Ext.type_ext |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
385 |
|> update_syntax mode_default Syn_Ext.pure_ext; |
260 | 386 |
|
18720 | 387 |
val basic_nonterms = |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
388 |
(Lexicon.terminals @ [Syn_Ext.logic, "type", "types", "sort", "classes", |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
389 |
Syn_Ext.args, Syn_Ext.cargs, "pttrn", "pttrns", "idt", "idts", "aprop", |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
390 |
"asms", Syn_Ext.any, Syn_Ext.sprop, "num_const", "float_const", |
28904 | 391 |
"index", "struct"]); |
18720 | 392 |
|
0 | 393 |
|
4887 | 394 |
|
15759 | 395 |
(** print syntax **) |
396 |
||
397 |
local |
|
0 | 398 |
|
260 | 399 |
fun pretty_strs_qs name strs = |
28840 | 400 |
Pretty.strs (name :: map quote (sort_strings strs)); |
0 | 401 |
|
17079 | 402 |
fun pretty_gram (Syntax (tabs, _)) = |
0 | 403 |
let |
32784 | 404 |
val {lexicon, prmodes, gram, ...} = tabs; |
28375 | 405 |
val prmodes' = sort_strings (filter_out (fn s => s = "") prmodes); |
0 | 406 |
in |
8720 | 407 |
[pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon), |
408 |
Pretty.big_list "prods:" (Parser.pretty_gram gram), |
|
409 |
pretty_strs_qs "print modes:" prmodes'] |
|
0 | 410 |
end; |
411 |
||
17079 | 412 |
fun pretty_trans (Syntax (tabs, _)) = |
0 | 413 |
let |
260 | 414 |
fun pretty_trtab name tab = |
5692 | 415 |
pretty_strs_qs name (Symtab.keys tab); |
0 | 416 |
|
260 | 417 |
fun pretty_ruletab name tab = |
5692 | 418 |
Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab)); |
0 | 419 |
|
28840 | 420 |
fun pretty_tokentr (mode, trs) = Pretty.strs (quote mode ^ ":" :: map fst trs); |
4703 | 421 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
422 |
val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, |
4703 | 423 |
print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs; |
0 | 424 |
in |
8720 | 425 |
[pretty_strs_qs "consts:" consts, |
426 |
pretty_trtab "parse_ast_translation:" parse_ast_trtab, |
|
427 |
pretty_ruletab "parse_rules:" parse_ruletab, |
|
428 |
pretty_trtab "parse_translation:" parse_trtab, |
|
429 |
pretty_trtab "print_translation:" print_trtab, |
|
430 |
pretty_ruletab "print_rules:" print_ruletab, |
|
431 |
pretty_trtab "print_ast_translation:" print_ast_trtab, |
|
432 |
Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab)] |
|
0 | 433 |
end; |
434 |
||
15759 | 435 |
in |
0 | 436 |
|
15759 | 437 |
fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn)); |
438 |
fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn)); |
|
439 |
fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn)); |
|
0 | 440 |
|
15759 | 441 |
end; |
0 | 442 |
|
443 |
||
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
444 |
(* reconstructing infixes -- educated guessing *) |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
445 |
|
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
446 |
fun guess_infix (Syntax ({gram, ...}, _)) c = |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
447 |
(case Parser.guess_infix_lr gram c of |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
448 |
SOME (s, l, r, j) => SOME |
35130 | 449 |
(if l then Mixfix.Infixl (s, j) |
450 |
else if r then Mixfix.Infixr (s, j) |
|
451 |
else Mixfix.Infix (s, j)) |
|
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
452 |
| NONE => NONE); |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
453 |
|
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
454 |
|
0 | 455 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
456 |
(** read **) |
18 | 457 |
|
27786
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
458 |
(* read token -- with optional YXML encoding of position *) |
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
459 |
|
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
460 |
fun read_token str = |
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
461 |
let val (text, pos) = |
27803 | 462 |
(case YXML.parse str handle Fail msg => error msg of |
463 |
XML.Elem ("token", props, [XML.Text text]) => (text, Position.of_properties props) |
|
464 |
| XML.Elem ("token", props, []) => ("", Position.of_properties props) |
|
465 |
| XML.Text text => (text, Position.none) |
|
466 |
| _ => (str, Position.none)) |
|
30573 | 467 |
in (Symbol_Pos.explode (text, pos), pos) end; |
27786
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
468 |
|
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
469 |
|
260 | 470 |
(* read_ast *) |
471 |
||
32738 | 472 |
val ambiguity_is_error = Unsynchronized.ref false; |
473 |
val ambiguity_level = Unsynchronized.ref 1; |
|
474 |
val ambiguity_limit = Unsynchronized.ref 10; |
|
882
b118d1ea0dfd
moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents:
865
diff
changeset
|
475 |
|
27786
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
476 |
fun ambiguity_msg pos = "Parse error: ambiguous syntax" ^ Position.str_of pos; |
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
477 |
|
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
478 |
fun read_asts ctxt is_logtype (Syntax (tabs, _)) xids root (syms, pos) = |
260 | 479 |
let |
14904
7d8dc92fcb7f
removed separate logtypes field of syntax; removed test_read, simple_str_of_sort, simple_string_of_typ; provide default_mode;
wenzelm
parents:
14798
diff
changeset
|
480 |
val {lexicon, gram, parse_ast_trtab, ...} = tabs; |
27889
c9e8a5bda32b
read_asts: Lexicon.report_token, filter Lexicon.is_proper;
wenzelm
parents:
27822
diff
changeset
|
481 |
val toks = Lexicon.tokenize lexicon xids syms; |
28413
ee73353fb87c
report_token/parse_token: back to context-less version;
wenzelm
parents:
28407
diff
changeset
|
482 |
val _ = List.app Lexicon.report_token toks; |
27889
c9e8a5bda32b
read_asts: Lexicon.report_token, filter Lexicon.is_proper;
wenzelm
parents:
27822
diff
changeset
|
483 |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
484 |
val root' = if root <> "prop" andalso is_logtype root then Syn_Ext.logic else root; |
27889
c9e8a5bda32b
read_asts: Lexicon.report_token, filter Lexicon.is_proper;
wenzelm
parents:
27822
diff
changeset
|
485 |
val pts = Parser.parse gram root' (filter Lexicon.is_proper toks); |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
486 |
val len = length pts; |
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
260
diff
changeset
|
487 |
|
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
488 |
val limit = ! ambiguity_limit; |
12292 | 489 |
fun show_pt pt = |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
490 |
Pretty.string_of (Ast.pretty_ast (hd (Syn_Trans.pts_to_asts ctxt (K NONE) [pt]))); |
260 | 491 |
in |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
492 |
if len <= ! ambiguity_level then () |
27786
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
493 |
else if ! ambiguity_is_error then error (ambiguity_msg pos) |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
494 |
else |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
495 |
(warning (cat_lines |
33919 | 496 |
(("Ambiguous input" ^ Position.str_of pos ^ |
27786
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
497 |
"\nproduces " ^ string_of_int len ^ " parse trees" ^ |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
498 |
(if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") :: |
33957 | 499 |
map show_pt (take limit pts)))); |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
500 |
Syn_Trans.pts_to_asts ctxt (lookup_tr parse_ast_trtab) pts |
260 | 501 |
end; |
502 |
||
503 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
504 |
(* read *) |
0 | 505 |
|
27786
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
506 |
fun read ctxt is_logtype (syn as Syntax (tabs, _)) ty inp = |
0 | 507 |
let |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
508 |
val {parse_ruletab, parse_trtab, ...} = tabs; |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
509 |
val asts = read_asts ctxt is_logtype syn false (Syn_Ext.typ_to_nonterm ty) inp; |
0 | 510 |
in |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
511 |
Syn_Trans.asts_to_terms ctxt (lookup_tr parse_trtab) |
18931 | 512 |
(map (Ast.normalize_ast (Symtab.lookup_list parse_ruletab)) asts) |
0 | 513 |
end; |
514 |
||
515 |
||
22703 | 516 |
(* read terms *) |
517 |
||
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
518 |
(*brute-force disambiguation via type-inference*) |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
519 |
fun disambig _ _ [t] = t |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
520 |
| disambig pp check ts = |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
521 |
let |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
522 |
val level = ! ambiguity_level; |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
523 |
val limit = ! ambiguity_limit; |
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
524 |
|
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
525 |
val ambiguity = length ts; |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
526 |
fun ambig_msg () = |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
527 |
if ambiguity > 1 andalso ambiguity <= level then |
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
528 |
"Got more than one parse tree.\n\ |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
529 |
\Retry with smaller Syntax.ambiguity_level for more information." |
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
530 |
else ""; |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
531 |
|
35394 | 532 |
val errs = Par_List.map check ts; |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
533 |
val results = map_filter (fn (t, NONE) => SOME t | _ => NONE) (ts ~~ errs); |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
534 |
val len = length results; |
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
535 |
in |
25476
03da46cfab9e
standard_parse_term: check ambiguous results without changing the result yet;
wenzelm
parents:
25394
diff
changeset
|
536 |
if null results then cat_error (ambig_msg ()) (cat_lines (map_filter I errs)) |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
537 |
else if len = 1 then |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
538 |
(if ambiguity > level then |
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
539 |
warning "Fortunately, only one parse tree is type correct.\n\ |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
540 |
\You may still want to disambiguate your grammar or your input." |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
541 |
else (); hd results) |
25993
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
542 |
else cat_error (ambig_msg ()) (cat_lines |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
543 |
(("Ambiguous input, " ^ string_of_int len ^ " terms are type correct" ^ |
d542486e39e7
added ambiguity_limit (restricts parse trees / terms printed in messages);
wenzelm
parents:
25476
diff
changeset
|
544 |
(if len <= limit then "" else " (" ^ string_of_int limit ^ " displayed)") ^ ":") :: |
33957 | 545 |
map (Pretty.string_of_term pp) (take limit results))) |
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
546 |
end; |
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
547 |
|
35429
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
548 |
fun standard_parse_term pp check get_sort map_const map_free ctxt is_logtype syn ty (syms, pos) = |
27786
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
549 |
read ctxt is_logtype syn ty (syms, pos) |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
550 |
|> map (Type_Ext.decode_term get_sort map_const map_free) |
24372
743575ccfec8
standard_parse_term: added pp/check argument, include disambig here (from sign.ML);
wenzelm
parents:
24341
diff
changeset
|
551 |
|> disambig (Printer.pp_show_brackets pp) check; |
22703 | 552 |
|
553 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
554 |
(* read types *) |
0 | 555 |
|
35429
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
556 |
fun standard_parse_typ ctxt syn get_sort (syms, pos) = |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
557 |
(case read ctxt (K false) syn Syn_Ext.typeT (syms, pos) of |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
558 |
[t] => Type_Ext.typ_of_term (get_sort (Type_Ext.term_sorts t)) t |
27786
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
559 |
| _ => error (ambiguity_msg pos)); |
144 | 560 |
|
561 |
||
8894 | 562 |
(* read sorts *) |
563 |
||
35429
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
564 |
fun standard_parse_sort ctxt syn (syms, pos) = |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
565 |
(case read ctxt (K false) syn Type_Ext.sortT (syms, pos) of |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
566 |
[t] => Type_Ext.sort_of_term t |
27786
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
567 |
| _ => error (ambiguity_msg pos)); |
8894 | 568 |
|
569 |
||
18 | 570 |
|
1158 | 571 |
(** prepare translation rules **) |
572 |
||
573 |
datatype 'a trrule = |
|
3526 | 574 |
ParseRule of 'a * 'a | |
575 |
PrintRule of 'a * 'a | |
|
576 |
ParsePrintRule of 'a * 'a; |
|
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
577 |
|
4618 | 578 |
fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y) |
579 |
| map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y) |
|
580 |
| map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y); |
|
1158 | 581 |
|
15531 | 582 |
fun parse_rule (ParseRule pats) = SOME pats |
583 |
| parse_rule (PrintRule _) = NONE |
|
584 |
| parse_rule (ParsePrintRule pats) = SOME pats; |
|
1158 | 585 |
|
15531 | 586 |
fun print_rule (ParseRule _) = NONE |
587 |
| print_rule (PrintRule pats) = SOME (swap pats) |
|
588 |
| print_rule (ParsePrintRule pats) = SOME (swap pats); |
|
1158 | 589 |
|
590 |
||
35111 | 591 |
fun is_const (Syntax ({consts, ...}, _)) c = member (op =) consts c; |
592 |
||
19262 | 593 |
local |
594 |
||
1158 | 595 |
fun check_rule (rule as (lhs, rhs)) = |
5692 | 596 |
(case Ast.rule_error rule of |
15531 | 597 |
SOME msg => |
1158 | 598 |
error ("Error in syntax translation rule: " ^ msg ^ "\n" ^ |
5692 | 599 |
Ast.str_of_ast lhs ^ " -> " ^ Ast.str_of_ast rhs) |
15531 | 600 |
| NONE => rule); |
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
601 |
|
21772 | 602 |
fun read_pattern ctxt is_logtype syn (root, str) = |
18 | 603 |
let |
5692 | 604 |
fun constify (ast as Ast.Constant _) = ast |
605 |
| constify (ast as Ast.Variable x) = |
|
35111 | 606 |
if is_const syn x orelse Long_Name.is_qualified x then Ast.Constant x |
3830 | 607 |
else ast |
5692 | 608 |
| constify (Ast.Appl asts) = Ast.Appl (map constify asts); |
27786
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
609 |
|
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
610 |
val (syms, pos) = read_token str; |
18 | 611 |
in |
27786
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
612 |
(case read_asts ctxt is_logtype syn true root (syms, pos) of |
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
613 |
[ast] => constify ast |
27786
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
614 |
| _ => error (ambiguity_msg pos)) |
4525c6f5d753
added read_token -- with optional YXML encoding of position;
wenzelm
parents:
27768
diff
changeset
|
615 |
end; |
0 | 616 |
|
1158 | 617 |
fun prep_rules rd_pat raw_rules = |
4618 | 618 |
let val rules = map (map_trrule rd_pat) raw_rules in |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19375
diff
changeset
|
619 |
(map check_rule (map_filter parse_rule rules), |
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19375
diff
changeset
|
620 |
map check_rule (map_filter print_rule rules)) |
1158 | 621 |
end |
18 | 622 |
|
19262 | 623 |
in |
624 |
||
625 |
val cert_rules = prep_rules I; |
|
626 |
||
21772 | 627 |
fun read_rules ctxt is_logtype syn = |
628 |
prep_rules (read_pattern ctxt is_logtype syn); |
|
19262 | 629 |
|
630 |
end; |
|
631 |
||
18 | 632 |
|
633 |
||
24923 | 634 |
(** unparse terms, typs, sorts **) |
18 | 635 |
|
24923 | 636 |
local |
637 |
||
32784 | 638 |
fun unparse_t t_to_ast prt_t markup ctxt (Syntax (tabs, _)) curried t = |
0 | 639 |
let |
35429
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
640 |
val {consts, print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs; |
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
641 |
val ast = t_to_ast consts ctxt (lookup_tr' print_trtab) t; |
0 | 642 |
in |
23631 | 643 |
Pretty.markup markup (prt_t ctxt curried prtabs (lookup_tr' print_ast_trtab) |
24613 | 644 |
(lookup_tokentr tokentrtab (print_mode_value ())) |
23631 | 645 |
(Ast.normalize_ast (Symtab.lookup_list print_ruletab) ast)) |
0 | 646 |
end; |
647 |
||
24923 | 648 |
in |
649 |
||
35429
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
650 |
fun standard_unparse_term idents extern = |
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
651 |
unparse_t (Printer.term_to_ast idents) (Printer.pretty_term_ast extern) Markup.term; |
24923 | 652 |
|
35429
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
653 |
fun standard_unparse_typ extern ctxt syn = |
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
654 |
unparse_t (K Printer.typ_to_ast) (Printer.pretty_typ_ast extern) Markup.typ ctxt syn false; |
24923 | 655 |
|
35429
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
656 |
fun standard_unparse_sort {extern_class} ctxt syn = |
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
657 |
unparse_t (K Printer.sort_to_ast) |
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
658 |
(Printer.pretty_typ_ast {extern_class = extern_class, extern_type = I}) |
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35412
diff
changeset
|
659 |
Markup.sort ctxt syn false; |
24923 | 660 |
|
661 |
end; |
|
0 | 662 |
|
663 |
||
664 |
||
19262 | 665 |
(** modify syntax **) |
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
666 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
667 |
fun ext_syntax f decls = update_syntax mode_default (f decls); |
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
668 |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
669 |
val update_trfuns = ext_syntax Syn_Ext.syn_ext_trfuns; |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
670 |
val update_advanced_trfuns = ext_syntax Syn_Ext.syn_ext_advanced_trfuns; |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
671 |
val extend_tokentrfuns = ext_syntax Syn_Ext.syn_ext_tokentrfuns; |
5692 | 672 |
|
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35394
diff
changeset
|
673 |
fun update_type_gram add prmode decls = |
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35394
diff
changeset
|
674 |
(if add then update_syntax else remove_syntax) prmode (Mixfix.syn_ext_types decls); |
25387 | 675 |
|
35412
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35394
diff
changeset
|
676 |
fun update_const_gram add is_logtype prmode decls = |
b8dead547d9e
more uniform treatment of syntax for types vs. consts;
wenzelm
parents:
35394
diff
changeset
|
677 |
(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:
15574
diff
changeset
|
678 |
|
25394
db25c98f32e1
syntax operations: turned extend'' into update'' (absorb duplicates);
wenzelm
parents:
25387
diff
changeset
|
679 |
fun update_trrules ctxt is_logtype syn = |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
680 |
ext_syntax Syn_Ext.syn_ext_rules o read_rules ctxt is_logtype syn; |
19262 | 681 |
|
21772 | 682 |
fun remove_trrules ctxt is_logtype syn = |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
683 |
remove_syntax mode_default o Syn_Ext.syn_ext_rules o read_rules ctxt is_logtype syn; |
19262 | 684 |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
685 |
val update_trrules_i = ext_syntax Syn_Ext.syn_ext_rules o cert_rules; |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
686 |
val remove_trrules_i = remove_syntax mode_default o Syn_Ext.syn_ext_rules o cert_rules; |
5692 | 687 |
|
688 |
||
24263 | 689 |
|
24768 | 690 |
(** inner syntax operations **) |
24263 | 691 |
|
24768 | 692 |
(* (un)parsing *) |
24263 | 693 |
|
28413
ee73353fb87c
report_token/parse_token: back to context-less version;
wenzelm
parents:
28407
diff
changeset
|
694 |
fun parse_token markup str = |
27822 | 695 |
let |
696 |
val (syms, pos) = read_token str; |
|
28413
ee73353fb87c
report_token/parse_token: back to context-less version;
wenzelm
parents:
28407
diff
changeset
|
697 |
val _ = Position.report markup pos; |
27822 | 698 |
in (syms, pos) end; |
699 |
||
24263 | 700 |
local |
24768 | 701 |
|
36739 | 702 |
type operations = |
703 |
{parse_sort: Proof.context -> string -> sort, |
|
704 |
parse_typ: Proof.context -> string -> typ, |
|
705 |
parse_term: Proof.context -> string -> term, |
|
706 |
parse_prop: Proof.context -> string -> term, |
|
707 |
unparse_sort: Proof.context -> sort -> Pretty.T, |
|
708 |
unparse_typ: Proof.context -> typ -> Pretty.T, |
|
709 |
unparse_term: Proof.context -> term -> Pretty.T}; |
|
24768 | 710 |
|
36739 | 711 |
val operations: operations Single_Assignment.var = Single_Assignment.var "Syntax.operations"; |
712 |
||
713 |
fun operation which ctxt x = |
|
714 |
(case Single_Assignment.peek operations of |
|
715 |
NONE => raise Fail "Inner syntax operations not installed" |
|
716 |
| SOME ops => which ops ctxt x); |
|
717 |
||
24263 | 718 |
in |
719 |
||
24768 | 720 |
val parse_sort = operation #parse_sort; |
721 |
val parse_typ = operation #parse_typ; |
|
722 |
val parse_term = operation #parse_term; |
|
723 |
val parse_prop = operation #parse_prop; |
|
724 |
val unparse_sort = operation #unparse_sort; |
|
725 |
val unparse_typ = operation #unparse_typ; |
|
726 |
val unparse_term = operation #unparse_term; |
|
727 |
||
36739 | 728 |
fun install_operations ops = Single_Assignment.assign operations ops; |
24768 | 729 |
|
24263 | 730 |
end; |
731 |
||
732 |
||
24768 | 733 |
(* context-sensitive (un)checking *) |
24263 | 734 |
|
24488 | 735 |
local |
736 |
||
24768 | 737 |
type key = int * bool; |
25060 | 738 |
type 'a check = 'a list -> Proof.context -> ('a list * Proof.context) option; |
24768 | 739 |
|
33519 | 740 |
structure Checks = Generic_Data |
24263 | 741 |
( |
24488 | 742 |
type T = |
24768 | 743 |
((key * ((string * typ check) * stamp) list) list * |
744 |
(key * ((string * term check) * stamp) list) list); |
|
24488 | 745 |
val empty = ([], []); |
24263 | 746 |
val extend = I; |
33519 | 747 |
fun merge ((typ_checks1, term_checks1), (typ_checks2, term_checks2)) : T = |
24768 | 748 |
(AList.join (op =) (K (Library.merge (eq_snd (op =)))) (typ_checks1, typ_checks2), |
749 |
AList.join (op =) (K (Library.merge (eq_snd (op =)))) (term_checks1, term_checks2)); |
|
24263 | 750 |
); |
751 |
||
24768 | 752 |
fun gen_add which (key: key) name f = |
753 |
Checks.map (which (AList.map_default op = (key, []) (cons ((name, f), stamp ())))); |
|
754 |
||
25060 | 755 |
fun check_stage fs = perhaps_loop (perhaps_apply (map uncurry fs)); |
756 |
||
757 |
fun gen_check which uncheck ctxt0 xs0 = |
|
24263 | 758 |
let |
24768 | 759 |
val funs = which (Checks.get (Context.Proof ctxt0)) |
760 |
|> map_filter (fn ((i, u), fs) => if uncheck = u then SOME (i, map (snd o fst) fs) else NONE) |
|
25060 | 761 |
|> Library.sort (int_ord o pairself fst) |> map snd |
762 |
|> not uncheck ? map rev; |
|
763 |
val check_all = perhaps_apply (map check_stage funs); |
|
764 |
in #1 (perhaps check_all (xs0, ctxt0)) end; |
|
24768 | 765 |
|
766 |
fun map_sort f S = |
|
767 |
(case f (TFree ("", S)) of |
|
768 |
TFree ("", S') => S' |
|
24923 | 769 |
| _ => raise TYPE ("map_sort", [TFree ("", S)], [])); |
24488 | 770 |
|
771 |
in |
|
24263 | 772 |
|
24768 | 773 |
fun print_checks ctxt = |
774 |
let |
|
775 |
fun split_checks checks = |
|
776 |
List.partition (fn ((_, un), _) => not un) checks |
|
777 |
|> pairself (map (fn ((i, _), fs) => (i, map (fst o fst) fs)) |
|
778 |
#> sort (int_ord o pairself fst)); |
|
779 |
fun pretty_checks kind checks = |
|
780 |
checks |> map (fn (i, names) => Pretty.block |
|
781 |
[Pretty.str (kind ^ " (stage " ^ signed_string_of_int i ^ "):"), |
|
782 |
Pretty.brk 1, Pretty.strs names]); |
|
783 |
||
784 |
val (typs, terms) = Checks.get (Context.Proof ctxt); |
|
785 |
val (typ_checks, typ_unchecks) = split_checks typs; |
|
786 |
val (term_checks, term_unchecks) = split_checks terms; |
|
787 |
in |
|
788 |
pretty_checks "typ_checks" typ_checks @ |
|
789 |
pretty_checks "term_checks" term_checks @ |
|
790 |
pretty_checks "typ_unchecks" typ_unchecks @ |
|
791 |
pretty_checks "term_unchecks" term_unchecks |
|
792 |
end |> Pretty.chunks |> Pretty.writeln; |
|
793 |
||
794 |
fun add_typ_check stage = gen_add apfst (stage, false); |
|
795 |
fun add_term_check stage = gen_add apsnd (stage, false); |
|
796 |
fun add_typ_uncheck stage = gen_add apfst (stage, true); |
|
797 |
fun add_term_uncheck stage = gen_add apsnd (stage, true); |
|
798 |
||
25060 | 799 |
val check_typs = gen_check fst false; |
800 |
val check_terms = gen_check snd false; |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
801 |
fun check_props ctxt = map (Type_Ext.type_constraint propT) #> check_terms ctxt; |
24488 | 802 |
|
24512 | 803 |
val check_typ = singleton o check_typs; |
804 |
val check_term = singleton o check_terms; |
|
805 |
val check_prop = singleton o check_props; |
|
24768 | 806 |
val check_sort = map_sort o check_typ; |
24263 | 807 |
|
25060 | 808 |
val uncheck_typs = gen_check fst true; |
809 |
val uncheck_terms = gen_check snd true; |
|
24768 | 810 |
val uncheck_sort = map_sort o singleton o uncheck_typs; |
24488 | 811 |
|
812 |
end; |
|
813 |
||
24263 | 814 |
|
24923 | 815 |
(* derived operations for classrel and arity *) |
816 |
||
817 |
val uncheck_classrel = map o singleton o uncheck_sort; |
|
818 |
||
819 |
fun unparse_classrel ctxt cs = Pretty.block (flat |
|
820 |
(separate [Pretty.str " <", Pretty.brk 1] (map (single o unparse_sort ctxt o single) cs))); |
|
821 |
||
822 |
fun uncheck_arity ctxt (a, Ss, S) = |
|
823 |
let |
|
824 |
val T = Type (a, replicate (length Ss) dummyT); |
|
825 |
val a' = |
|
826 |
(case singleton (uncheck_typs ctxt) T of |
|
827 |
Type (a', _) => a' |
|
828 |
| T => raise TYPE ("uncheck_arity", [T], [])); |
|
829 |
val Ss' = map (uncheck_sort ctxt) Ss; |
|
830 |
val S' = uncheck_sort ctxt S; |
|
831 |
in (a', Ss', S') end; |
|
832 |
||
833 |
fun unparse_arity ctxt (a, Ss, S) = |
|
834 |
let |
|
25043 | 835 |
val prtT = unparse_typ ctxt (Type (a, [])); |
24923 | 836 |
val dom = |
837 |
if null Ss then [] |
|
838 |
else [Pretty.list "(" ")" (map (unparse_sort ctxt) Ss), Pretty.brk 1]; |
|
25043 | 839 |
in Pretty.block ([prtT, Pretty.str " ::", Pretty.brk 1] @ dom @ [unparse_sort ctxt S]) end; |
24923 | 840 |
|
841 |
||
842 |
(* read = parse + check *) |
|
24263 | 843 |
|
844 |
fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt; |
|
845 |
fun read_typ ctxt = parse_typ ctxt #> singleton (check_typs ctxt); |
|
846 |
||
847 |
fun read_terms ctxt = map (parse_term ctxt) #> check_terms ctxt; |
|
848 |
fun read_props ctxt = map (parse_prop ctxt) #> check_props ctxt; |
|
849 |
||
850 |
val read_term = singleton o read_terms; |
|
851 |
val read_prop = singleton o read_props; |
|
852 |
||
36610
bafd82950e24
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm
parents:
36208
diff
changeset
|
853 |
val read_sort_global = read_sort o ProofContext.init_global; |
bafd82950e24
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm
parents:
36208
diff
changeset
|
854 |
val read_typ_global = read_typ o ProofContext.init_global; |
bafd82950e24
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm
parents:
36208
diff
changeset
|
855 |
val read_term_global = read_term o ProofContext.init_global; |
bafd82950e24
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm
parents:
36208
diff
changeset
|
856 |
val read_prop_global = read_prop o ProofContext.init_global; |
24278 | 857 |
|
24263 | 858 |
|
24923 | 859 |
(* pretty = uncheck + unparse *) |
860 |
||
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
861 |
fun pretty_term ctxt = singleton (uncheck_terms ctxt) #> unparse_term ctxt; |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
862 |
fun pretty_typ ctxt = singleton (uncheck_typs ctxt) #> unparse_typ ctxt; |
24923 | 863 |
fun pretty_sort ctxt = uncheck_sort ctxt #> unparse_sort ctxt; |
864 |
fun pretty_classrel ctxt = uncheck_classrel ctxt #> unparse_classrel ctxt; |
|
865 |
fun pretty_arity ctxt = uncheck_arity ctxt #> unparse_arity ctxt; |
|
866 |
||
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
867 |
val string_of_term = Pretty.string_of oo pretty_term; |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
868 |
val string_of_typ = Pretty.string_of oo pretty_typ; |
24923 | 869 |
val string_of_sort = Pretty.string_of oo pretty_sort; |
870 |
val string_of_classrel = Pretty.string_of oo pretty_classrel; |
|
871 |
val string_of_arity = Pretty.string_of oo pretty_arity; |
|
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
872 |
|
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
873 |
|
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
874 |
(* global pretty printing *) |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
875 |
|
33519 | 876 |
structure PrettyGlobal = Proof_Data(type T = bool fun init _ = false); |
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
877 |
val is_pretty_global = PrettyGlobal.get; |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
878 |
val set_pretty_global = PrettyGlobal.put; |
36610
bafd82950e24
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm
parents:
36208
diff
changeset
|
879 |
val init_pretty_global = set_pretty_global true o ProofContext.init_global; |
24923 | 880 |
|
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
881 |
val pretty_term_global = pretty_term o init_pretty_global; |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
882 |
val pretty_typ_global = pretty_typ o init_pretty_global; |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
883 |
val pretty_sort_global = pretty_sort o init_pretty_global; |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
884 |
|
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
885 |
val string_of_term_global = string_of_term o init_pretty_global; |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
886 |
val string_of_typ_global = string_of_typ o init_pretty_global; |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
887 |
val string_of_sort_global = string_of_sort o init_pretty_global; |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
888 |
|
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
889 |
|
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
890 |
(* pp operations -- deferred evaluation *) |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
891 |
|
24970 | 892 |
fun pp ctxt = Pretty.pp |
893 |
(fn x => pretty_term ctxt x, |
|
894 |
fn x => pretty_typ ctxt x, |
|
895 |
fn x => pretty_sort ctxt x, |
|
896 |
fn x => pretty_classrel ctxt x, |
|
897 |
fn x => pretty_arity ctxt x); |
|
24923 | 898 |
|
26951
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
899 |
fun pp_global thy = Pretty.pp |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
900 |
(fn x => pretty_term_global thy x, |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
901 |
fn x => pretty_typ_global thy x, |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
902 |
fn x => pretty_sort_global thy x, |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
903 |
fn x => pretty_classrel (init_pretty_global thy) x, |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
904 |
fn x => pretty_arity (init_pretty_global thy) x); |
030e4a818b39
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26704
diff
changeset
|
905 |
|
26678 | 906 |
|
15833
78109c7012ed
removed token_trans.ML (some content moved to syn_ext.ML);
wenzelm
parents:
15759
diff
changeset
|
907 |
(*export parts of internal Syntax structures*) |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
908 |
open Lexicon Syn_Ext Ast Parser Type_Ext Syn_Trans Mixfix Printer; |
2366 | 909 |
|
0 | 910 |
end; |
5692 | 911 |
|
35130 | 912 |
structure Basic_Syntax: BASIC_SYNTAX = Syntax; |
913 |
open Basic_Syntax; |
|
23923 | 914 |
|
26684 | 915 |
forget_structure "Ast"; |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
916 |
forget_structure "Syn_Ext"; |
26684 | 917 |
forget_structure "Parser"; |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
918 |
forget_structure "Type_Ext"; |
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
37146
diff
changeset
|
919 |
forget_structure "Syn_Trans"; |
26684 | 920 |
forget_structure "Mixfix"; |
921 |
forget_structure "Printer"; |