author | wenzelm |
Tue, 14 Aug 2007 23:23:09 +0200 | |
changeset 24278 | cf82c471f9ee |
parent 24263 | aff00d8b2e32 |
child 24341 | 7b8da2396c49 |
permissions | -rw-r--r-- |
18 | 1 |
(* Title: Pure/Syntax/syntax.ML |
0 | 2 |
ID: $Id$ |
3 |
Author: Tobias Nipkow and Markus Wenzel, TU Muenchen |
|
18 | 4 |
|
24263 | 5 |
Standard Isabelle syntax, based on arbitrary context-free grammars |
6 |
(specified by mixfix declarations). |
|
0 | 7 |
*) |
8 |
||
260 | 9 |
signature BASIC_SYNTAX = |
2383 | 10 |
sig |
260 | 11 |
include AST0 |
556 | 12 |
include SYN_TRANS0 |
13 |
include MIXFIX0 |
|
260 | 14 |
include PRINTER0 |
2383 | 15 |
end; |
260 | 16 |
|
0 | 17 |
signature SYNTAX = |
2383 | 18 |
sig |
260 | 19 |
include AST1 |
0 | 20 |
include LEXICON0 |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
21 |
include SYN_EXT0 |
0 | 22 |
include TYPE_EXT0 |
556 | 23 |
include SYN_TRANS1 |
24 |
include MIXFIX1 |
|
0 | 25 |
include PRINTER0 |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
26 |
val extend_trtab: string -> (string * ('a * stamp)) list -> |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
27 |
('a * stamp) Symtab.table -> ('a * stamp) Symtab.table |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
28 |
val merge_trtabs: string -> ('a * stamp) Symtab.table -> ('a * stamp) Symtab.table -> |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
29 |
('a * stamp) Symtab.table |
14648
0e67b385a6df
tuned interfaces to accomodate advanced translation functions;
wenzelm
parents:
14598
diff
changeset
|
30 |
val merge_tr'tabs: ('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table |
0e67b385a6df
tuned interfaces to accomodate advanced translation functions;
wenzelm
parents:
14598
diff
changeset
|
31 |
-> ('a * stamp) list Symtab.table |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
32 |
val extend_tr'tab: (string * ('a * stamp)) list -> |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
33 |
('a * stamp) list Symtab.table -> ('a * stamp) list Symtab.table |
1158 | 34 |
datatype 'a trrule = |
3526 | 35 |
ParseRule of 'a * 'a | |
36 |
PrintRule of 'a * 'a | |
|
37 |
ParsePrintRule of 'a * 'a |
|
556 | 38 |
type syntax |
17079 | 39 |
val eq_syntax: syntax * syntax -> bool |
14687 | 40 |
val is_keyword: syntax -> string -> bool |
20784 | 41 |
type mode |
42 |
val default_mode: mode |
|
21702 | 43 |
val input_mode: mode |
21826 | 44 |
val internalM: string |
12073
b4401452928e
extend_XXX: sane argument order ... -> syntax -> syntax;
wenzelm
parents:
11546
diff
changeset
|
45 |
val extend_type_gram: (string * int * mixfix) list -> syntax -> 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
|
46 |
val extend_const_gram: (string -> bool) -> |
20784 | 47 |
mode -> (string * typ * mixfix) list -> syntax -> syntax |
12073
b4401452928e
extend_XXX: sane argument order ... -> syntax -> syntax;
wenzelm
parents:
11546
diff
changeset
|
48 |
val extend_consts: string list -> syntax -> syntax |
b4401452928e
extend_XXX: sane argument order ... -> syntax -> syntax;
wenzelm
parents:
11546
diff
changeset
|
49 |
val extend_trfuns: |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
50 |
(string * ((ast list -> ast) * stamp)) list * |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
51 |
(string * ((term list -> term) * stamp)) list * |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
52 |
(string * ((bool -> typ -> term list -> term) * stamp)) list * |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
53 |
(string * ((ast list -> ast) * stamp)) list -> syntax -> syntax |
16613
76e57e08dcb5
proper treatment of advanced trfuns: pass thy argument;
wenzelm
parents:
15833
diff
changeset
|
54 |
val extend_advanced_trfuns: |
21772 | 55 |
(string * ((Proof.context -> ast list -> ast) * stamp)) list * |
56 |
(string * ((Proof.context -> term list -> term) * stamp)) list * |
|
57 |
(string * ((Proof.context -> bool -> typ -> term list -> term) * stamp)) list * |
|
58 |
(string * ((Proof.context -> ast list -> ast) * stamp)) list -> syntax -> syntax |
|
23660
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23655
diff
changeset
|
59 |
val extend_tokentrfuns: (string * string * (string -> output * int)) list -> syntax -> syntax |
19262 | 60 |
val remove_const_gram: (string -> bool) -> |
20784 | 61 |
mode -> (string * typ * mixfix) list -> syntax -> syntax |
21772 | 62 |
val extend_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
|
63 |
(string * string) trrule list -> syntax -> syntax |
21772 | 64 |
val remove_trrules: Proof.context -> (string -> bool) -> syntax -> |
19262 | 65 |
(string * string) trrule list -> syntax -> syntax |
66 |
val extend_trrules_i: ast trrule list -> syntax -> syntax |
|
67 |
val remove_trrules_i: ast trrule list -> syntax -> syntax |
|
4618 | 68 |
val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule |
556 | 69 |
val merge_syntaxes: syntax -> syntax -> syntax |
18720 | 70 |
val basic_syn: syntax |
71 |
val basic_nonterms: string list |
|
556 | 72 |
val print_gram: syntax -> unit |
73 |
val print_trans: syntax -> unit |
|
74 |
val print_syntax: syntax -> unit |
|
21772 | 75 |
val read: Proof.context -> (string -> bool) -> syntax -> typ -> string -> term list |
24278 | 76 |
val standard_parse_term: |
22703 | 77 |
(((string * int) * sort) list -> string * int -> Term.sort) -> |
78 |
(string -> string option) -> (string -> string option) -> |
|
79 |
(typ -> typ) -> (sort -> sort) -> Proof.context -> |
|
80 |
(string -> bool) -> syntax -> typ -> string -> term list |
|
24278 | 81 |
val standard_parse_typ: Proof.context -> syntax -> ((indexname * sort) list -> indexname -> sort) -> |
16613
76e57e08dcb5
proper treatment of advanced trfuns: pass thy argument;
wenzelm
parents:
15833
diff
changeset
|
82 |
(sort -> sort) -> string -> typ |
24278 | 83 |
val standard_parse_sort: Proof.context -> syntax -> (sort -> sort) -> string -> sort |
21772 | 84 |
val pretty_term: (string -> xstring) -> Proof.context -> syntax -> bool -> term -> Pretty.T |
85 |
val pretty_typ: Proof.context -> syntax -> typ -> Pretty.T |
|
86 |
val pretty_sort: Proof.context -> syntax -> sort -> Pretty.T |
|
882
b118d1ea0dfd
moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents:
865
diff
changeset
|
87 |
val ambiguity_level: int ref |
14177
06b19a7e675a
Added 'ambiguity_is_error' flag, which, if set, makes the parser fail,
skalberg
parents:
12785
diff
changeset
|
88 |
val ambiguity_is_error: bool ref |
24263 | 89 |
val install_parsers: |
90 |
{sort: Proof.context -> string -> sort, |
|
91 |
typ: Proof.context -> string -> typ, |
|
92 |
term: Proof.context -> string -> term, |
|
93 |
prop: Proof.context -> string -> term} -> unit |
|
94 |
val parse_sort: Proof.context -> string -> sort |
|
95 |
val parse_typ: Proof.context -> string -> typ |
|
96 |
val parse_term: Proof.context -> string -> term |
|
97 |
val parse_prop: Proof.context -> string -> term |
|
24278 | 98 |
val global_parse_sort: theory -> string -> sort |
99 |
val global_parse_typ: theory -> string -> typ |
|
100 |
val global_parse_term: theory -> string -> term |
|
101 |
val global_parse_prop: theory -> string -> term |
|
24263 | 102 |
val add_type_check: (term list -> Proof.context -> Term.term list * Proof.context) -> |
103 |
Context.generic -> Context.generic |
|
104 |
val type_check: term list -> Proof.context -> term list * Proof.context |
|
105 |
val check_terms: Proof.context -> term list -> term list |
|
106 |
val check_props: Proof.context -> term list -> term list |
|
107 |
val check_typs: Proof.context -> typ list -> typ list |
|
108 |
val read_sort: Proof.context -> string -> sort |
|
109 |
val read_typ: Proof.context -> string -> typ |
|
110 |
val read_terms: Proof.context -> string list -> term list |
|
111 |
val read_props: Proof.context -> string list -> term list |
|
112 |
val read_term: Proof.context -> string -> term |
|
113 |
val read_prop: Proof.context -> string -> term |
|
24278 | 114 |
val global_read_sort: theory -> string -> sort |
115 |
val global_read_typ: theory -> string -> typ |
|
116 |
val global_read_term: theory -> string -> term |
|
117 |
val global_read_prop: theory -> string -> term |
|
2383 | 118 |
end; |
0 | 119 |
|
12094 | 120 |
structure Syntax: SYNTAX = |
0 | 121 |
struct |
122 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
123 |
(** tables of translation functions **) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
124 |
|
5692 | 125 |
(* parse (ast) translations *) |
126 |
||
17412 | 127 |
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
|
128 |
|
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23631
diff
changeset
|
129 |
fun err_dup_trfun name c = |
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23631
diff
changeset
|
130 |
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
|
131 |
|
21536
f119c730f509
extend_trtab: allow identical trfuns to be overwritten;
wenzelm
parents:
20784
diff
changeset
|
132 |
fun remove_trtab trfuns = fold (Symtab.remove SynExt.eq_trfun) trfuns; |
f119c730f509
extend_trtab: allow identical trfuns to be overwritten;
wenzelm
parents:
20784
diff
changeset
|
133 |
|
f119c730f509
extend_trtab: allow identical trfuns to be overwritten;
wenzelm
parents:
20784
diff
changeset
|
134 |
fun extend_trtab name trfuns tab = Symtab.extend (remove_trtab trfuns tab, trfuns) |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23631
diff
changeset
|
135 |
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
|
136 |
|
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
137 |
fun merge_trtabs name tab1 tab2 = Symtab.merge SynExt.eq_trfun (tab1, tab2) |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23631
diff
changeset
|
138 |
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
|
139 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
140 |
|
5692 | 141 |
(* print (ast) translations *) |
142 |
||
18931 | 143 |
fun lookup_tr' tab c = map fst (Symtab.lookup_list tab c); |
144 |
fun extend_tr'tab trfuns = fold_rev Symtab.update_list trfuns; |
|
145 |
fun remove_tr'tab trfuns = fold (Symtab.remove_list SynExt.eq_trfun) trfuns; |
|
146 |
fun merge_tr'tabs tab1 tab2 = Symtab.merge_list SynExt.eq_trfun (tab1, tab2); |
|
5692 | 147 |
|
148 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
149 |
|
2700 | 150 |
(** tables of token translation functions **) |
151 |
||
152 |
fun lookup_tokentr tabs modes = |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19375
diff
changeset
|
153 |
let val trs = distinct (eq_fst (op =)) (maps (these o AList.lookup (op =) tabs) (modes @ [""])) |
17314 | 154 |
in fn c => Option.map fst (AList.lookup (op =) trs c) end; |
2700 | 155 |
|
156 |
fun merge_tokentrtabs tabs1 tabs2 = |
|
157 |
let |
|
158 |
fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2; |
|
159 |
||
4703 | 160 |
fun name (s, _) = implode (tl (Symbol.explode s)); |
2706 | 161 |
|
2700 | 162 |
fun merge mode = |
163 |
let |
|
17213 | 164 |
val trs1 = these (AList.lookup (op =) tabs1 mode); |
165 |
val trs2 = these (AList.lookup (op =) tabs2 mode); |
|
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
18977
diff
changeset
|
166 |
val trs = distinct eq_tr (trs1 @ trs2); |
2700 | 167 |
in |
18964 | 168 |
(case duplicates (eq_fst (op =)) trs of |
2700 | 169 |
[] => (mode, trs) |
170 |
| dups => error ("More than one token translation function in mode " ^ |
|
2706 | 171 |
quote mode ^ " for " ^ commas_quote (map name dups))) |
2700 | 172 |
end; |
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
18977
diff
changeset
|
173 |
in map merge (distinct (op =) (map fst (tabs1 @ tabs2))) end; |
2700 | 174 |
|
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
175 |
fun extend_tokentrtab tokentrs tabs = |
2700 | 176 |
let |
17213 | 177 |
fun ins_tokentr (m, c, f) = |
178 |
AList.default (op =) (m, []) |
|
179 |
#> AList.map_entry (op =) m (cons ("_" ^ c, (f, stamp ()))); |
|
15759 | 180 |
in merge_tokentrtabs tabs (fold ins_tokentr tokentrs []) end; |
2700 | 181 |
|
182 |
||
183 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
184 |
(** tables of translation rules **) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
185 |
|
5692 | 186 |
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
|
187 |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19375
diff
changeset
|
188 |
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
|
189 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
190 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
191 |
(* empty, extend, merge ruletabs *) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
192 |
|
18931 | 193 |
val extend_ruletab = fold_rev (fn r => Symtab.update_list (Ast.head_of_rule r, r)); |
194 |
val remove_ruletab = fold (fn r => Symtab.remove_list (op =) (Ast.head_of_rule r, r)); |
|
195 |
fun merge_ruletabs tab1 tab2 = Symtab.merge_list (op =) (tab1, tab2); |
|
0 | 196 |
|
197 |
||
198 |
||
199 |
(** datatype syntax **) |
|
200 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
201 |
datatype syntax = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
202 |
Syntax of { |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
203 |
input: SynExt.xprod list, |
4703 | 204 |
lexicon: Scan.lexicon, |
5692 | 205 |
gram: Parser.gram, |
18 | 206 |
consts: string list, |
2913 | 207 |
prmodes: string list, |
21772 | 208 |
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
|
209 |
parse_ruletab: ruletab, |
21772 | 210 |
parse_trtab: ((Proof.context -> term list -> term) * stamp) Symtab.table, |
211 |
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
|
212 |
print_ruletab: ruletab, |
21772 | 213 |
print_ast_trtab: ((Proof.context -> Ast.ast list -> Ast.ast) * stamp) list Symtab.table, |
23660
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23655
diff
changeset
|
214 |
tokentrtab: (string * (string * ((string -> output * int) * stamp)) list) list, |
17079 | 215 |
prtabs: Printer.prtabs} * stamp; |
0 | 216 |
|
17079 | 217 |
fun eq_syntax (Syntax (_, s1), Syntax (_, s2)) = s1 = s2; |
218 |
||
219 |
fun is_keyword (Syntax ({lexicon, ...}, _)) = Scan.is_literal lexicon o Symbol.explode; |
|
14687 | 220 |
|
20784 | 221 |
type mode = string * bool; |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
222 |
val default_mode = ("", true); |
21702 | 223 |
val input_mode = ("input", true); |
21826 | 224 |
val internalM = "internal"; |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
225 |
|
18 | 226 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
227 |
(* empty_syntax *) |
18 | 228 |
|
17079 | 229 |
val empty_syntax = Syntax |
230 |
({input = [], |
|
4703 | 231 |
lexicon = Scan.empty_lexicon, |
5692 | 232 |
gram = Parser.empty_gram, |
167 | 233 |
consts = [], |
2913 | 234 |
prmodes = [], |
5692 | 235 |
parse_ast_trtab = Symtab.empty, |
236 |
parse_ruletab = Symtab.empty, |
|
237 |
parse_trtab = Symtab.empty, |
|
238 |
print_trtab = Symtab.empty, |
|
239 |
print_ruletab = Symtab.empty, |
|
240 |
print_ast_trtab = Symtab.empty, |
|
2700 | 241 |
tokentrtab = [], |
17079 | 242 |
prtabs = Printer.empty_prtabs}, stamp ()); |
167 | 243 |
|
244 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
245 |
(* extend_syntax *) |
167 | 246 |
|
17079 | 247 |
fun extend_syntax (mode, inout) syn_ext (Syntax (tabs, _)) = |
167 | 248 |
let |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
249 |
val {input, lexicon, gram, consts = consts1, prmodes = prmodes1, |
2366 | 250 |
parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab, |
2700 | 251 |
print_ast_trtab, tokentrtab, prtabs} = tabs; |
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
|
252 |
val SynExt.SynExt {xprods, consts = consts2, prmodes = prmodes2, |
2913 | 253 |
parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules, |
2700 | 254 |
print_ast_translation, token_translation} = syn_ext; |
19546
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
255 |
fun if_inout xs = if inout then xs else []; |
167 | 256 |
in |
17079 | 257 |
Syntax |
19546
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
258 |
({input = if_inout xprods @ input, |
22121 | 259 |
lexicon = Scan.extend_lexicon (if_inout (SynExt.delims_of xprods)) lexicon, |
19546
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
260 |
gram = Parser.extend_gram gram (if_inout xprods), |
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
261 |
consts = Library.merge (op =) (consts1, filter_out NameSpace.is_qualified consts2), |
18921 | 262 |
prmodes = insert (op =) mode (Library.merge (op =) (prmodes1, prmodes2)), |
167 | 263 |
parse_ast_trtab = |
19546
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
264 |
extend_trtab "parse ast translation" (if_inout parse_ast_translation) parse_ast_trtab, |
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
265 |
parse_ruletab = extend_ruletab (if_inout parse_rules) parse_ruletab, |
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
266 |
parse_trtab = extend_trtab "parse translation" (if_inout parse_translation) parse_trtab, |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
267 |
print_trtab = extend_tr'tab print_translation print_trtab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
268 |
print_ruletab = extend_ruletab print_rules print_ruletab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
269 |
print_ast_trtab = extend_tr'tab print_ast_translation print_ast_trtab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
270 |
tokentrtab = extend_tokentrtab token_translation tokentrtab, |
17079 | 271 |
prtabs = Printer.extend_prtabs mode xprods prtabs}, stamp ()) |
18 | 272 |
end; |
273 |
||
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
274 |
|
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
275 |
(* remove_syntax *) |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
276 |
|
17079 | 277 |
fun remove_syntax (mode, inout) syn_ext (Syntax (tabs, _)) = |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
278 |
let |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
279 |
val SynExt.SynExt {xprods, consts = _, prmodes = _, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
280 |
parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
281 |
print_ast_translation, token_translation = _} = syn_ext; |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
282 |
val {input, lexicon, gram, consts, prmodes, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
283 |
parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
284 |
print_ast_trtab, tokentrtab, prtabs} = tabs; |
19300 | 285 |
val input' = if inout then subtract (op =) xprods input else input; |
19546
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
286 |
fun if_inout xs = if inout then xs else []; |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
287 |
in |
17079 | 288 |
Syntax |
289 |
({input = input', |
|
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
290 |
lexicon = if inout then Scan.make_lexicon (SynExt.delims_of input') else lexicon, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
291 |
gram = if inout then Parser.make_gram input' else gram, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
292 |
consts = consts, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
293 |
prmodes = prmodes, |
19546
00d5c7c7ce07
extend/remove_syntax: observe inout flag for translations, too;
wenzelm
parents:
19482
diff
changeset
|
294 |
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
|
295 |
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
|
296 |
parse_trtab = remove_trtab (if_inout parse_translation) parse_trtab, |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
297 |
print_trtab = remove_tr'tab print_translation print_trtab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
298 |
print_ruletab = remove_ruletab print_rules print_ruletab, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
299 |
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
|
300 |
tokentrtab = tokentrtab, |
17079 | 301 |
prtabs = Printer.remove_prtabs mode xprods prtabs}, stamp ()) |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
302 |
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
|
303 |
|
18 | 304 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
305 |
(* merge_syntaxes *) |
0 | 306 |
|
17079 | 307 |
fun merge_syntaxes (Syntax (tabs1, _)) (Syntax (tabs2, _)) = |
0 | 308 |
let |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
309 |
val {input = input1, lexicon = lexicon1, gram = gram1, consts = consts1, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
310 |
prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
311 |
parse_ruletab = parse_ruletab1, parse_trtab = parse_trtab1, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
312 |
print_trtab = print_trtab1, print_ruletab = print_ruletab1, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
313 |
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
|
314 |
|
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
315 |
val {input = input2, lexicon = lexicon2, gram = gram2, consts = consts2, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
316 |
prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
317 |
parse_ruletab = parse_ruletab2, parse_trtab = parse_trtab2, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
318 |
print_trtab = print_trtab2, print_ruletab = print_ruletab2, |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
319 |
print_ast_trtab = print_ast_trtab2, tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2; |
0 | 320 |
in |
17079 | 321 |
Syntax |
18921 | 322 |
({input = Library.merge (op =) (input1, input2), |
4703 | 323 |
lexicon = Scan.merge_lexicons lexicon1 lexicon2, |
5692 | 324 |
gram = Parser.merge_grams gram1 gram2, |
18428 | 325 |
consts = sort_distinct string_ord (consts1 @ consts2), |
18921 | 326 |
prmodes = Library.merge (op =) (prmodes1, prmodes2), |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
327 |
parse_ast_trtab = |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
328 |
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
|
329 |
parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2, |
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
330 |
parse_trtab = merge_trtabs "parse translation" parse_trtab1 parse_trtab2, |
5692 | 331 |
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
|
332 |
print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2, |
5692 | 333 |
print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2, |
2700 | 334 |
tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2, |
17079 | 335 |
prtabs = Printer.merge_prtabs prtabs1 prtabs2}, stamp ()) |
0 | 336 |
end; |
337 |
||
338 |
||
18720 | 339 |
(* basic syntax *) |
260 | 340 |
|
18720 | 341 |
val basic_syn = |
17168 | 342 |
empty_syntax |
343 |
|> extend_syntax default_mode TypeExt.type_ext |
|
344 |
|> extend_syntax default_mode SynExt.pure_ext; |
|
260 | 345 |
|
18720 | 346 |
val basic_nonterms = |
347 |
(Lexicon.terminals @ [SynExt.logic, "type", "types", "sort", "classes", |
|
348 |
SynExt.args, SynExt.cargs, "pttrn", "pttrns", "idt", "idts", "aprop", |
|
22573 | 349 |
"asms", SynExt.any_, SynExt.sprop, "num_const", "index", "struct"]); |
18720 | 350 |
|
0 | 351 |
|
4887 | 352 |
|
15759 | 353 |
(** print syntax **) |
354 |
||
355 |
local |
|
0 | 356 |
|
260 | 357 |
fun pretty_strs_qs name strs = |
14598
7009f59711e3
Replaced quote by Library.quote, since quote now refers to Symbol.quote
berghofe
parents:
14177
diff
changeset
|
358 |
Pretty.strs (name :: map Library.quote (sort_strings strs)); |
0 | 359 |
|
17079 | 360 |
fun pretty_gram (Syntax (tabs, _)) = |
0 | 361 |
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
|
362 |
val {lexicon, prmodes, gram, prtabs, ...} = tabs; |
2913 | 363 |
val prmodes' = sort_strings (filter_out (equal "") prmodes); |
0 | 364 |
in |
8720 | 365 |
[pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon), |
366 |
Pretty.big_list "prods:" (Parser.pretty_gram gram), |
|
367 |
pretty_strs_qs "print modes:" prmodes'] |
|
0 | 368 |
end; |
369 |
||
17079 | 370 |
fun pretty_trans (Syntax (tabs, _)) = |
0 | 371 |
let |
260 | 372 |
fun pretty_trtab name tab = |
5692 | 373 |
pretty_strs_qs name (Symtab.keys tab); |
0 | 374 |
|
260 | 375 |
fun pretty_ruletab name tab = |
5692 | 376 |
Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab)); |
0 | 377 |
|
14598
7009f59711e3
Replaced quote by Library.quote, since quote now refers to Symbol.quote
berghofe
parents:
14177
diff
changeset
|
378 |
fun pretty_tokentr (mode, trs) = Pretty.strs (Library.quote mode ^ ":" :: map fst trs); |
4703 | 379 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
380 |
val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, |
4703 | 381 |
print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs; |
0 | 382 |
in |
8720 | 383 |
[pretty_strs_qs "consts:" consts, |
384 |
pretty_trtab "parse_ast_translation:" parse_ast_trtab, |
|
385 |
pretty_ruletab "parse_rules:" parse_ruletab, |
|
386 |
pretty_trtab "parse_translation:" parse_trtab, |
|
387 |
pretty_trtab "print_translation:" print_trtab, |
|
388 |
pretty_ruletab "print_rules:" print_ruletab, |
|
389 |
pretty_trtab "print_ast_translation:" print_ast_trtab, |
|
390 |
Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab)] |
|
0 | 391 |
end; |
392 |
||
15759 | 393 |
in |
0 | 394 |
|
15759 | 395 |
fun print_gram syn = Pretty.writeln (Pretty.chunks (pretty_gram syn)); |
396 |
fun print_trans syn = Pretty.writeln (Pretty.chunks (pretty_trans syn)); |
|
397 |
fun print_syntax syn = Pretty.writeln (Pretty.chunks (pretty_gram syn @ pretty_trans syn)); |
|
0 | 398 |
|
15759 | 399 |
end; |
0 | 400 |
|
401 |
||
402 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
403 |
(** read **) |
18 | 404 |
|
260 | 405 |
(* read_ast *) |
406 |
||
882
b118d1ea0dfd
moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents:
865
diff
changeset
|
407 |
val ambiguity_level = ref 1; |
24263 | 408 |
val ambiguity_is_error = ref false; |
882
b118d1ea0dfd
moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents:
865
diff
changeset
|
409 |
|
21772 | 410 |
fun read_asts ctxt is_logtype (Syntax (tabs, _)) xids root str = |
260 | 411 |
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
|
412 |
val {lexicon, gram, parse_ast_trtab, ...} = tabs; |
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
|
413 |
val root' = if root <> "prop" andalso is_logtype root then SynExt.logic else root; |
4703 | 414 |
val chars = Symbol.explode str; |
5692 | 415 |
val pts = Parser.parse gram root' (Lexicon.tokenize lexicon xids chars); |
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
260
diff
changeset
|
416 |
|
12292 | 417 |
fun show_pt pt = |
21772 | 418 |
Pretty.string_of (Ast.pretty_ast (hd (SynTrans.pts_to_asts ctxt (K NONE) [pt]))); |
260 | 419 |
in |
21962 | 420 |
if length pts > ! ambiguity_level then |
16613
76e57e08dcb5
proper treatment of advanced trfuns: pass thy argument;
wenzelm
parents:
15833
diff
changeset
|
421 |
if ! ambiguity_is_error then error ("Ambiguous input " ^ quote str) |
16661
507438b27f66
use tracing for potentially voluminous ambiguity output;
wenzelm
parents:
16613
diff
changeset
|
422 |
else (warning ("Ambiguous input " ^ quote str ^ "\n" ^ |
16716 | 423 |
"produces " ^ string_of_int (length pts) ^ " parse trees."); |
21962 | 424 |
List.app (warning o show_pt) pts) |
425 |
else (); |
|
21772 | 426 |
SynTrans.pts_to_asts ctxt (lookup_tr parse_ast_trtab) pts |
260 | 427 |
end; |
428 |
||
429 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
430 |
(* read *) |
0 | 431 |
|
21772 | 432 |
fun read ctxt is_logtype (syn as Syntax (tabs, _)) ty str = |
0 | 433 |
let |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
434 |
val {parse_ruletab, parse_trtab, ...} = tabs; |
21772 | 435 |
val asts = read_asts ctxt is_logtype syn false (SynExt.typ_to_nonterm ty) str; |
0 | 436 |
in |
21772 | 437 |
SynTrans.asts_to_terms ctxt (lookup_tr parse_trtab) |
18931 | 438 |
(map (Ast.normalize_ast (Symtab.lookup_list parse_ruletab)) asts) |
0 | 439 |
end; |
440 |
||
441 |
||
22703 | 442 |
(* read terms *) |
443 |
||
24278 | 444 |
fun standard_parse_term get_sort map_const map_free map_type map_sort ctxt is_logtype syn ty str = |
22764 | 445 |
map (TypeExt.decode_term get_sort map_const map_free map_type map_sort) |
22703 | 446 |
(read ctxt is_logtype syn ty str); |
447 |
||
448 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
449 |
(* read types *) |
0 | 450 |
|
24278 | 451 |
fun standard_parse_typ ctxt syn get_sort map_sort str = |
21772 | 452 |
(case read ctxt (K false) syn SynExt.typeT str of |
22703 | 453 |
[t] => TypeExt.typ_of_term (get_sort (TypeExt.term_sorts map_sort t)) map_sort t |
8894 | 454 |
| _ => error "read_typ: ambiguous syntax"); |
144 | 455 |
|
456 |
||
8894 | 457 |
(* read sorts *) |
458 |
||
24278 | 459 |
fun standard_parse_sort ctxt syn map_sort str = |
21772 | 460 |
(case read ctxt (K false) syn TypeExt.sortT str of |
22703 | 461 |
[t] => TypeExt.sort_of_term map_sort t |
8894 | 462 |
| _ => error "read_sort: ambiguous syntax"); |
463 |
||
464 |
||
18 | 465 |
|
1158 | 466 |
(** prepare translation rules **) |
467 |
||
468 |
datatype 'a trrule = |
|
3526 | 469 |
ParseRule of 'a * 'a | |
470 |
PrintRule of 'a * 'a | |
|
471 |
ParsePrintRule of 'a * 'a; |
|
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
472 |
|
4618 | 473 |
fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y) |
474 |
| map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y) |
|
475 |
| map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y); |
|
1158 | 476 |
|
15531 | 477 |
fun parse_rule (ParseRule pats) = SOME pats |
478 |
| parse_rule (PrintRule _) = NONE |
|
479 |
| parse_rule (ParsePrintRule pats) = SOME pats; |
|
1158 | 480 |
|
15531 | 481 |
fun print_rule (ParseRule _) = NONE |
482 |
| print_rule (PrintRule pats) = SOME (swap pats) |
|
483 |
| print_rule (ParsePrintRule pats) = SOME (swap pats); |
|
1158 | 484 |
|
485 |
||
19262 | 486 |
local |
487 |
||
1158 | 488 |
fun check_rule (rule as (lhs, rhs)) = |
5692 | 489 |
(case Ast.rule_error rule of |
15531 | 490 |
SOME msg => |
1158 | 491 |
error ("Error in syntax translation rule: " ^ msg ^ "\n" ^ |
5692 | 492 |
Ast.str_of_ast lhs ^ " -> " ^ Ast.str_of_ast rhs) |
15531 | 493 |
| NONE => rule); |
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
494 |
|
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
495 |
|
21772 | 496 |
fun read_pattern ctxt is_logtype syn (root, str) = |
18 | 497 |
let |
17079 | 498 |
val Syntax ({consts, ...}, _) = syn; |
18 | 499 |
|
5692 | 500 |
fun constify (ast as Ast.Constant _) = ast |
501 |
| constify (ast as Ast.Variable x) = |
|
20664 | 502 |
if member (op =) consts x orelse NameSpace.is_qualified x then Ast.Constant x |
3830 | 503 |
else ast |
5692 | 504 |
| constify (Ast.Appl asts) = Ast.Appl (map constify asts); |
18 | 505 |
in |
21772 | 506 |
(case read_asts ctxt is_logtype syn true root str of |
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
507 |
[ast] => constify ast |
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
508 |
| _ => error ("Syntactically ambiguous input: " ^ quote str)) |
18678 | 509 |
end handle ERROR msg => |
510 |
cat_error msg ("The error(s) above occurred in translation pattern " ^ |
|
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
511 |
quote str); |
0 | 512 |
|
1158 | 513 |
fun prep_rules rd_pat raw_rules = |
4618 | 514 |
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
|
515 |
(map check_rule (map_filter parse_rule rules), |
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19375
diff
changeset
|
516 |
map check_rule (map_filter print_rule rules)) |
1158 | 517 |
end |
18 | 518 |
|
19262 | 519 |
in |
520 |
||
521 |
val cert_rules = prep_rules I; |
|
522 |
||
21772 | 523 |
fun read_rules ctxt is_logtype syn = |
524 |
prep_rules (read_pattern ctxt is_logtype syn); |
|
19262 | 525 |
|
526 |
end; |
|
527 |
||
18 | 528 |
|
529 |
||
3779 | 530 |
(** pretty terms, typs, sorts **) |
18 | 531 |
|
23631 | 532 |
fun pretty_t t_to_ast prt_t markup ctxt (syn as Syntax (tabs, _)) curried t = |
0 | 533 |
let |
2700 | 534 |
val {print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs; |
21772 | 535 |
val ast = t_to_ast ctxt (lookup_tr' print_trtab) t; |
0 | 536 |
in |
23631 | 537 |
Pretty.markup markup (prt_t ctxt curried prtabs (lookup_tr' print_ast_trtab) |
6167
2f354020efc3
corrected output of symbols for several (probably not all) relevant functions
oheimb
parents:
5702
diff
changeset
|
538 |
(lookup_tokentr tokentrtab (! print_mode)) |
23631 | 539 |
(Ast.normalize_ast (Symtab.lookup_list print_ruletab) ast)) |
0 | 540 |
end; |
541 |
||
23631 | 542 |
fun pretty_term extern = |
543 |
pretty_t Printer.term_to_ast (Printer.pretty_term_ast extern) Markup.term; |
|
544 |
fun pretty_typ ctxt syn = |
|
545 |
pretty_t Printer.typ_to_ast Printer.pretty_typ_ast Markup.typ ctxt syn false; |
|
546 |
fun pretty_sort ctxt syn = |
|
547 |
pretty_t Printer.sort_to_ast Printer.pretty_typ_ast Markup.sort ctxt syn false; |
|
0 | 548 |
|
549 |
||
550 |
||
19262 | 551 |
(** modify syntax **) |
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
552 |
|
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
|
553 |
fun ext_syntax' f is_logtype prmode decls = extend_syntax prmode (f is_logtype decls); |
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
|
554 |
fun ext_syntax f = ext_syntax' (K f) (K false) default_mode; |
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
555 |
|
16613
76e57e08dcb5
proper treatment of advanced trfuns: pass thy argument;
wenzelm
parents:
15833
diff
changeset
|
556 |
val extend_type_gram = ext_syntax Mixfix.syn_ext_types; |
76e57e08dcb5
proper treatment of advanced trfuns: pass thy argument;
wenzelm
parents:
15833
diff
changeset
|
557 |
val extend_const_gram = ext_syntax' Mixfix.syn_ext_consts; |
76e57e08dcb5
proper treatment of advanced trfuns: pass thy argument;
wenzelm
parents:
15833
diff
changeset
|
558 |
val extend_consts = ext_syntax SynExt.syn_ext_const_names; |
76e57e08dcb5
proper treatment of advanced trfuns: pass thy argument;
wenzelm
parents:
15833
diff
changeset
|
559 |
val extend_trfuns = ext_syntax SynExt.syn_ext_trfuns; |
76e57e08dcb5
proper treatment of advanced trfuns: pass thy argument;
wenzelm
parents:
15833
diff
changeset
|
560 |
val extend_advanced_trfuns = ext_syntax SynExt.syn_ext_advanced_trfuns; |
76e57e08dcb5
proper treatment of advanced trfuns: pass thy argument;
wenzelm
parents:
15833
diff
changeset
|
561 |
val extend_tokentrfuns = ext_syntax SynExt.syn_ext_tokentrfuns; |
5692 | 562 |
|
15755
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
563 |
fun remove_const_gram is_logtype prmode decls = |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
564 |
remove_syntax prmode (Mixfix.syn_ext_consts is_logtype decls); |
50ac97ebe7d8
expect translations functions to be stamped already;
wenzelm
parents:
15574
diff
changeset
|
565 |
|
21772 | 566 |
fun extend_trrules ctxt is_logtype syn = |
567 |
ext_syntax SynExt.syn_ext_rules o read_rules ctxt is_logtype syn; |
|
19262 | 568 |
|
21772 | 569 |
fun remove_trrules ctxt is_logtype syn = |
570 |
remove_syntax default_mode o SynExt.syn_ext_rules o read_rules ctxt is_logtype syn; |
|
19262 | 571 |
|
572 |
val extend_trrules_i = ext_syntax SynExt.syn_ext_rules o cert_rules; |
|
573 |
val remove_trrules_i = remove_syntax default_mode o SynExt.syn_ext_rules o cert_rules; |
|
5692 | 574 |
|
575 |
||
24263 | 576 |
|
577 |
(** generic parsing and type-checking **) |
|
578 |
||
579 |
(* parsing *) |
|
580 |
||
581 |
type parsers = |
|
582 |
{sort: Proof.context -> string -> sort, |
|
583 |
typ: Proof.context -> string -> typ, |
|
584 |
term: Proof.context -> string -> term, |
|
585 |
prop: Proof.context -> string -> term}; |
|
586 |
||
587 |
local |
|
588 |
val parsers = ref (NONE: parsers option); |
|
589 |
in |
|
590 |
fun install_parsers ps = CRITICAL (fn () => |
|
591 |
if is_some (! parsers) then error "Inner syntax parsers already installed" |
|
592 |
else parsers := SOME ps); |
|
593 |
||
594 |
fun parse which ctxt s = |
|
595 |
(case ! parsers of |
|
596 |
NONE => error "Inner syntax parsers not yet installed" |
|
597 |
| SOME ps => which ps ctxt s); |
|
598 |
end; |
|
599 |
||
600 |
val parse_sort = parse #sort; |
|
601 |
val parse_typ = parse #typ; |
|
602 |
val parse_term = parse #term; |
|
603 |
val parse_prop = parse #prop; |
|
604 |
||
24278 | 605 |
val global_parse_sort = parse_sort o ProofContext.init; |
606 |
val global_parse_typ = parse_typ o ProofContext.init; |
|
607 |
val global_parse_term = parse_term o ProofContext.init; |
|
608 |
val global_parse_prop = parse_prop o ProofContext.init; |
|
609 |
||
24263 | 610 |
|
611 |
(* type-checking *) |
|
612 |
||
613 |
structure TypeCheck = GenericDataFun |
|
614 |
( |
|
615 |
type T = ((term list -> Proof.context -> term list * Proof.context) * stamp) list; |
|
616 |
val empty = []; |
|
617 |
val extend = I; |
|
618 |
fun merge _ type_checks : T = Library.merge (eq_snd op =) type_checks; |
|
619 |
); |
|
620 |
||
621 |
fun add_type_check f = TypeCheck.map (cons (f, stamp ())); |
|
622 |
||
623 |
fun type_check ts0 ctxt0 = |
|
624 |
let |
|
625 |
val funs = rev (TypeCheck.get (Context.Proof ctxt0)); |
|
626 |
fun check [] ts ctxt = (ts, ctxt) |
|
627 |
| check ((f, _) :: fs) ts ctxt = f ts ctxt |-> check fs; |
|
628 |
||
629 |
fun check_fixpoint ts ctxt = |
|
630 |
let val (ts', ctxt') = check funs ts ctxt in |
|
631 |
if eq_list (op aconv) (ts, ts') then (ts, ctxt) |
|
632 |
else check_fixpoint ts' ctxt' |
|
633 |
end; |
|
634 |
in check_fixpoint ts0 ctxt0 end; |
|
635 |
||
636 |
fun check_terms ctxt ts = #1 (type_check ts ctxt); |
|
637 |
fun check_props ctxt = map (fn t => TypeInfer.constrain t propT) #> check_terms ctxt; |
|
638 |
fun check_typs ctxt = map Logic.mk_type #> check_terms ctxt #> map Logic.dest_type; |
|
639 |
||
640 |
fun check_sort ctxt S = |
|
641 |
(case singleton (check_typs ctxt) (TFree ("", S)) of (* FIXME TypeInfer.invent_type (!?) *) |
|
642 |
TFree ("", S') => S' |
|
643 |
| T => raise TYPE ("check_sort", [T], [])); |
|
644 |
||
645 |
||
646 |
(* combined operations *) |
|
647 |
||
648 |
fun read_sort ctxt = parse_sort ctxt #> check_sort ctxt; |
|
649 |
fun read_typ ctxt = parse_typ ctxt #> singleton (check_typs ctxt); |
|
650 |
||
651 |
fun read_terms ctxt = map (parse_term ctxt) #> check_terms ctxt; |
|
652 |
fun read_props ctxt = map (parse_prop ctxt) #> check_props ctxt; |
|
653 |
||
654 |
val read_term = singleton o read_terms; |
|
655 |
val read_prop = singleton o read_props; |
|
656 |
||
24278 | 657 |
val global_read_sort = read_sort o ProofContext.init; |
658 |
val global_read_typ = read_typ o ProofContext.init; |
|
659 |
val global_read_term = read_term o ProofContext.init; |
|
660 |
val global_read_prop = read_prop o ProofContext.init; |
|
661 |
||
24263 | 662 |
|
15833
78109c7012ed
removed token_trans.ML (some content moved to syn_ext.ML);
wenzelm
parents:
15759
diff
changeset
|
663 |
(*export parts of internal Syntax structures*) |
78109c7012ed
removed token_trans.ML (some content moved to syn_ext.ML);
wenzelm
parents:
15759
diff
changeset
|
664 |
open Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer; |
2366 | 665 |
|
0 | 666 |
end; |
5692 | 667 |
|
668 |
structure BasicSyntax: BASIC_SYNTAX = Syntax; |
|
669 |
open BasicSyntax; |
|
23923 | 670 |
|
671 |
structure Hidden = struct end; |
|
672 |
structure Ast = Hidden; |
|
673 |
structure SynExt = Hidden; |
|
674 |
structure Parser = Hidden; |
|
675 |
structure TypeExt = Hidden; |
|
676 |
structure SynTrans = Hidden; |
|
677 |
structure Mixfix = Hidden; |
|
678 |
structure Printer = Hidden; |