author | wenzelm |
Fri, 20 Sep 2024 11:19:23 +0200 | |
changeset 80908 | d794caea94a5 |
parent 80907 | 5703399d9c56 |
child 80909 | 6ddbfad8ca20 |
permissions | -rw-r--r-- |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
1 |
(* Title: Pure/Syntax/syntax_ext.ML |
80893 | 2 |
Author: Makarius |
240 | 3 |
|
80893 | 4 |
Syntax extension as internal record. |
240 | 5 |
*) |
6 |
||
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
7 |
signature SYNTAX_EXT = |
4050 | 8 |
sig |
62753 | 9 |
datatype mfix = Mfix of Symbol_Pos.T list * typ * string * int list * int * Position.T |
80905
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
10 |
type block = {markup: Markup.T list, consistent: bool, unbreakable: bool, indent: int} |
80893 | 11 |
val block_indent: int -> block |
1510 | 12 |
datatype xsymb = |
13 |
Delim of string | |
|
14 |
Argument of string * int | |
|
15 |
Space of string | |
|
80893 | 16 |
Bg of block | |
62783 | 17 |
Brk of int | |
18 |
En |
|
1510 | 19 |
datatype xprod = XProd of string * xsymb list * string * int |
20 |
val chain_pri: int |
|
77999 | 21 |
val delims_of: xprod list -> Symbol.symbol list list |
1510 | 22 |
datatype syn_ext = |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
35429
diff
changeset
|
23 |
Syn_Ext of { |
1510 | 24 |
xprods: xprod list, |
80748
43c4817375bf
support for syntax const dependencies, with minimal integrity checks;
wenzelm
parents:
77999
diff
changeset
|
25 |
consts: (string * string list) list, |
21772 | 26 |
parse_ast_translation: (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list, |
1510 | 27 |
parse_rules: (Ast.ast * Ast.ast) list, |
21772 | 28 |
parse_translation: (string * ((Proof.context -> term list -> term) * stamp)) list, |
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
29 |
print_translation: (string * ((Proof.context -> typ -> term list -> term) * stamp)) list, |
1510 | 30 |
print_rules: (Ast.ast * Ast.ast) list, |
42268 | 31 |
print_ast_translation: (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list} |
80897
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
32 |
val mfix_args: Proof.context -> Symbol_Pos.T list -> int |
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
33 |
val mixfix_args: Proof.context -> Input.source -> int |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
34 |
val escape: string -> string |
80897
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
35 |
val syn_ext: Proof.context -> string list -> mfix list -> |
80748
43c4817375bf
support for syntax const dependencies, with minimal integrity checks;
wenzelm
parents:
77999
diff
changeset
|
36 |
(string * string list) list -> (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list * |
21772 | 37 |
(string * ((Proof.context -> term list -> term) * stamp)) list * |
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
38 |
(string * ((Proof.context -> typ -> term list -> term) * stamp)) list * |
42268 | 39 |
(string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list -> |
40 |
(Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext |
|
80897
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
41 |
val syn_ext_consts: Proof.context -> (string * string list) list -> syn_ext |
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
42 |
val syn_ext_rules: Proof.context -> (Ast.ast * Ast.ast) list * (Ast.ast * Ast.ast) list -> syn_ext |
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
43 |
val syn_ext_trfuns: Proof.context -> |
21772 | 44 |
(string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list * |
45 |
(string * ((Proof.context -> term list -> term) * stamp)) list * |
|
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
46 |
(string * ((Proof.context -> typ -> term list -> term) * stamp)) list * |
21772 | 47 |
(string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list -> syn_ext |
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
48 |
val stamp_trfun: stamp -> string * 'a -> string * ('a * stamp) |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
49 |
val mk_trfun: string * 'a -> string * ('a * stamp) |
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
50 |
val eq_trfun: ('a * stamp) * ('a * stamp) -> bool |
4050 | 51 |
end; |
240 | 52 |
|
42288
2074b31650e6
discontinued special treatment of structure Syntax_Ext (formerly Syn_Ext);
wenzelm
parents:
42268
diff
changeset
|
53 |
structure Syntax_Ext: SYNTAX_EXT = |
240 | 54 |
struct |
55 |
||
56 |
(** datatype xprod **) |
|
57 |
||
58 |
(*Delim s: delimiter s |
|
59 |
Argument (s, p): nonterminal s requiring priority >= p, or valued token |
|
60 |
Space s: some white space for printing |
|
61 |
Bg, Brk, En: blocks and breaks for pretty printing*) |
|
62 |
||
80905
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
63 |
type block = {markup: Markup.T list, consistent: bool, unbreakable: bool, indent: int}; |
62789 | 64 |
|
80893 | 65 |
fun block_indent indent : block = |
80905
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
66 |
{markup = [], consistent = false, unbreakable = false, indent = indent}; |
62783 | 67 |
|
240 | 68 |
datatype xsymb = |
69 |
Delim of string | |
|
70 |
Argument of string * int | |
|
71 |
Space of string | |
|
80893 | 72 |
Bg of block | |
62783 | 73 |
Brk of int | |
74 |
En; |
|
240 | 75 |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
76 |
fun is_delim (Delim _) = true |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
77 |
| is_delim _ = false; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
78 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
79 |
fun is_terminal (Delim _) = true |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
80 |
| is_terminal (Argument (s, _)) = Lexicon.is_terminal s |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
81 |
| is_terminal _ = false; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
82 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
83 |
fun is_argument (Argument _) = true |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
84 |
| is_argument _ = false; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
85 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
86 |
fun is_index (Argument ("index", _)) = true |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
87 |
| is_index _ = false; |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
88 |
|
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
89 |
val index = Argument ("index", 1000); |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
90 |
|
240 | 91 |
|
92 |
(*XProd (lhs, syms, c, p): |
|
93 |
lhs: name of nonterminal on the lhs of the production |
|
94 |
syms: list of symbols on the rhs of the production |
|
95 |
c: head of parse tree |
|
96 |
p: priority of this production*) |
|
97 |
||
98 |
datatype xprod = XProd of string * xsymb list * string * int; |
|
99 |
||
100 |
val chain_pri = ~1; (*dummy for chain productions*) |
|
101 |
||
102 |
fun delims_of xprods = |
|
19004 | 103 |
fold (fn XProd (_, xsymbs, _, _) => |
104 |
fold (fn Delim s => insert (op =) s | _ => I) xsymbs) xprods [] |
|
105 |
|> map Symbol.explode; |
|
240 | 106 |
|
107 |
||
108 |
||
109 |
(** datatype mfix **) |
|
110 |
||
62753 | 111 |
(*Mfix (sy, ty, c, ps, p, pos): |
62752 | 112 |
sy: rhs of production as symbolic text |
240 | 113 |
ty: type description of production |
114 |
c: head of parse tree |
|
115 |
ps: priorities of arguments in sy |
|
62753 | 116 |
p: priority of production |
117 |
pos: source position*) |
|
240 | 118 |
|
62753 | 119 |
datatype mfix = Mfix of Symbol_Pos.T list * typ * string * int list * int * Position.T; |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
120 |
|
240 | 121 |
|
62786 | 122 |
(* properties *) |
123 |
||
80904 | 124 |
fun show_names names = |
125 |
commas_quote (map (fn (name, pos) => Markup.markup (Position.markup pos) name) names); |
|
126 |
||
62786 | 127 |
local |
128 |
||
129 |
open Basic_Symbol_Pos; |
|
130 |
||
131 |
val err_prefix = "Error in mixfix block properties: "; |
|
62801
f9d102ef13f1
clarified errors -- disallow cartouche fragments as delimiter;
wenzelm
parents:
62795
diff
changeset
|
132 |
val !!! = Symbol_Pos.!!! (fn () => err_prefix ^ "atom expected (identifier, numeral, cartouche)"); |
62786 | 133 |
|
134 |
val scan_atom = |
|
135 |
Symbol_Pos.scan_ident || |
|
136 |
($$$ "-" @@@ (Symbol_Pos.scan_float || Symbol_Pos.scan_nat)) || |
|
137 |
Symbol_Pos.scan_float || Symbol_Pos.scan_nat || |
|
138 |
Symbol_Pos.scan_cartouche_content err_prefix; |
|
139 |
||
140 |
val scan_blanks = Scan.many (Symbol.is_blank o Symbol_Pos.symbol); |
|
141 |
val scan_item = |
|
142 |
scan_blanks |-- scan_atom --| scan_blanks |
|
143 |
>> (fn ss => (Symbol_Pos.content ss, #1 (Symbol_Pos.range ss))); |
|
144 |
||
80904 | 145 |
val scan_prop = scan_item -- Scan.option ($$ "=" |-- !!! scan_item); |
62786 | 146 |
|
80904 | 147 |
fun get_property default0 default1 parse name props = |
148 |
(case find_first (fn ((a, _), _) => a = name) props of |
|
149 |
NONE => default0 |
|
150 |
| SOME (_, NONE) => default1 |
|
151 |
| SOME ((_, pos1), SOME (b, pos2)) => |
|
152 |
(parse (b, pos2) handle Fail msg => |
|
153 |
error (msg ^ " for property " ^ quote name ^ Position.here_list [pos1, pos2]))); |
|
62786 | 154 |
|
155 |
in |
|
156 |
||
157 |
fun read_properties ss = |
|
158 |
let |
|
62801
f9d102ef13f1
clarified errors -- disallow cartouche fragments as delimiter;
wenzelm
parents:
62795
diff
changeset
|
159 |
val props = |
f9d102ef13f1
clarified errors -- disallow cartouche fragments as delimiter;
wenzelm
parents:
62795
diff
changeset
|
160 |
(case Scan.error (Scan.finite Symbol_Pos.stopper (Scan.repeat scan_prop)) ss of |
f9d102ef13f1
clarified errors -- disallow cartouche fragments as delimiter;
wenzelm
parents:
62795
diff
changeset
|
161 |
(props, []) => props |
f9d102ef13f1
clarified errors -- disallow cartouche fragments as delimiter;
wenzelm
parents:
62795
diff
changeset
|
162 |
| (_, (_, pos) :: _) => error (err_prefix ^ "bad input" ^ Position.here pos)); |
80904 | 163 |
fun ok (_, bs) = length bs <= 1; |
62786 | 164 |
val _ = |
80904 | 165 |
(case AList.group (eq_fst op =) props |> filter_out ok of |
62786 | 166 |
[] => () |
80904 | 167 |
| dups => error ("Duplicate properties: " ^ show_names (map #1 dups))); |
62786 | 168 |
in props end; |
169 |
||
80904 | 170 |
val get_string = get_property "" "" #1; |
171 |
val get_bool = get_property false true (Value.parse_bool o #1); |
|
172 |
val get_nat = get_property 0 1 (Value.parse_nat o #1); |
|
62786 | 173 |
|
80905
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
174 |
fun get_expression_markup ctxt = |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
175 |
get_property NONE (SOME (Markup.expression "")) (SOME o Markup_Expression.check ctxt) |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
176 |
Markup.expressionN; |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
177 |
|
62786 | 178 |
end; |
179 |
||
180 |
||
19004 | 181 |
(* read mixfix annotations *) |
4050 | 182 |
|
183 |
local |
|
19004 | 184 |
|
80889 | 185 |
val markup_block_begin = Markup_Expression.setup (Binding.make ("mixfix_block_begin", \<^here>)); |
186 |
val markup_block_end = Markup_Expression.setup (Binding.make ("mixfix_block_end", \<^here>)); |
|
187 |
val markup_delimiter = Markup_Expression.setup (Binding.make ("mixfix_delimiter", \<^here>)); |
|
188 |
val markup_argument = Markup_Expression.setup (Binding.make ("mixfix_argument", \<^here>)); |
|
189 |
val markup_space = Markup_Expression.setup (Binding.make ("mixfix_space", \<^here>)); |
|
190 |
val markup_break = Markup_Expression.setup (Binding.make ("mixfix_break", \<^here>)); |
|
191 |
||
62752 | 192 |
open Basic_Symbol_Pos; |
193 |
||
62801
f9d102ef13f1
clarified errors -- disallow cartouche fragments as delimiter;
wenzelm
parents:
62795
diff
changeset
|
194 |
val err_prefix = "Error in mixfix annotation: "; |
f9d102ef13f1
clarified errors -- disallow cartouche fragments as delimiter;
wenzelm
parents:
62795
diff
changeset
|
195 |
|
62752 | 196 |
fun scan_one pred = Scan.one (pred o Symbol_Pos.symbol); |
197 |
fun scan_many pred = Scan.many (pred o Symbol_Pos.symbol); |
|
198 |
fun scan_many1 pred = Scan.many1 (pred o Symbol_Pos.symbol); |
|
199 |
||
80889 | 200 |
fun reports_of_block pos = [(pos, markup_block_begin), (pos, Markup.keyword3)]; |
62806 | 201 |
|
62808 | 202 |
fun reports_of (xsym, pos) = |
62806 | 203 |
(case xsym of |
80889 | 204 |
Delim _ => [(pos, markup_delimiter), (pos, Markup.literal)] |
205 |
| Argument _ => [(pos, markup_argument)] |
|
206 |
| Space _ => [(pos, markup_space)] |
|
62806 | 207 |
| Bg _ => reports_of_block pos |
80889 | 208 |
| Brk _ => [(pos, markup_break), (pos, Markup.keyword3)] |
209 |
| En => [(pos, markup_block_end), (pos, Markup.keyword3)]); |
|
62806 | 210 |
|
80891 | 211 |
fun reports_text_of (Delim s, pos) = |
62808 | 212 |
if Position.is_reported pos andalso exists Symbol.is_utf8 (Symbol.explode s) then |
64677
8dc24130e8fe
more uniform treatment of "bad" like other messages (with serial number);
wenzelm
parents:
63933
diff
changeset
|
213 |
[((pos, Markup.bad ()), |
62808 | 214 |
"Mixfix delimiter contains raw Unicode -- this is non-portable and unreliable")] |
215 |
else [] |
|
80891 | 216 |
| reports_text_of _ = []; |
62808 | 217 |
|
80905
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
218 |
fun read_block_properties ctxt ss = |
62786 | 219 |
let |
220 |
val props = read_properties ss; |
|
62783 | 221 |
|
80905
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
222 |
val more_markups = the_list (get_expression_markup ctxt props); |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
223 |
|
62786 | 224 |
val markup_name = get_string Markup.markupN props; |
80904 | 225 |
val markup_props = props |> map_filter (fn (a, opt_b) => |
226 |
if member (op =) Markup.block_properties (#1 a) then NONE |
|
80905
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
227 |
else SOME (a, the_default ("true", Position.none) opt_b)); |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
228 |
val markups = |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
229 |
if markup_name <> "" then [(markup_name, map (apply2 #1) markup_props)] |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
230 |
else if null markup_props then [] |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
231 |
else error ("Markup name required for block properties: " ^ show_names (map #1 markup_props)); |
62783 | 232 |
|
80905
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
233 |
val block: block = |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
234 |
{markup = more_markups @ markups, |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
235 |
consistent = get_bool Markup.consistentN props, |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
236 |
unbreakable = get_bool Markup.unbreakableN props, |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
237 |
indent = get_nat Markup.indentN props}; |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
238 |
in Bg block end |
62806 | 239 |
handle ERROR msg => |
240 |
let |
|
241 |
val reported_texts = |
|
242 |
reports_of_block (#1 (Symbol_Pos.range ss)) |
|
243 |
|> map (fn (p, m) => Markup.markup_report (Position.reported_text p m "")) |
|
244 |
in error (msg ^ implode reported_texts) end; |
|
62783 | 245 |
|
246 |
val read_block_indent = |
|
62789 | 247 |
Bg o block_indent o #1 o Library.read_int o map Symbol_Pos.symbol; |
62783 | 248 |
|
71545
b0b16088ccf2
allow slightly odd "' " in mixfix as documented (introduced in 55754d6d399c, but broken in be8a8d60d962);
wenzelm
parents:
69584
diff
changeset
|
249 |
val is_meta = member (op =) ["'", "(", ")", "/", "_", "\<index>", Symbol.open_, Symbol.close]; |
19004 | 250 |
|
63933 | 251 |
val scan_delim = |
252 |
scan_one Symbol.is_control ::: Symbol_Pos.scan_cartouche "Mixfix error: " || |
|
253 |
$$ "'" |-- scan_one ((not o Symbol.is_blank) andf Symbol.not_eof) >> single || |
|
254 |
scan_one ((not o is_meta) andf (not o Symbol.is_blank) andf Symbol.not_eof) >> single; |
|
19004 | 255 |
|
80905
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
256 |
fun scan_symbs ctxt = |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
257 |
let |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
258 |
val scan_sym = |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
259 |
$$ "_" >> K (Argument ("", 0)) || |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
260 |
$$ "\<index>" >> K index || |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
261 |
$$ "(" |-- |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
262 |
(Symbol_Pos.scan_cartouche_content err_prefix >> read_block_properties ctxt || |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
263 |
scan_many Symbol.is_digit >> read_block_indent) || |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
264 |
$$ ")" >> K En || |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
265 |
$$ "/" -- $$ "/" >> K (Brk ~1) || |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
266 |
$$ "/" |-- scan_many Symbol.is_space >> (Brk o length) || |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
267 |
scan_many1 Symbol.is_space >> (Space o Symbol_Pos.content) || |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
268 |
Scan.repeat1 scan_delim >> (Delim o Symbol_Pos.content o flat); |
14819 | 269 |
|
80905
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
270 |
val scan_symb = |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
271 |
Scan.trace scan_sym >> (fn (syms, trace) => SOME (syms, #1 (Symbol_Pos.range trace))) || |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
272 |
$$ "'" -- scan_one Symbol.is_space >> K NONE; |
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
273 |
in Scan.repeat scan_symb --| Scan.ahead (~$$ "'") end; |
4050 | 274 |
|
19004 | 275 |
in |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
276 |
|
80897
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
277 |
fun read_mfix ctxt ss = |
62764 | 278 |
let |
62801
f9d102ef13f1
clarified errors -- disallow cartouche fragments as delimiter;
wenzelm
parents:
62795
diff
changeset
|
279 |
val xsymbs = |
80905
47793a46d06c
support for Markup.expression properties in pretty-blocks;
wenzelm
parents:
80904
diff
changeset
|
280 |
(case Scan.error (Scan.finite Symbol_Pos.stopper (scan_symbs ctxt)) ss of |
62801
f9d102ef13f1
clarified errors -- disallow cartouche fragments as delimiter;
wenzelm
parents:
62795
diff
changeset
|
281 |
(res, []) => map_filter I res |
f9d102ef13f1
clarified errors -- disallow cartouche fragments as delimiter;
wenzelm
parents:
62795
diff
changeset
|
282 |
| (_, (_, pos) :: _) => error (err_prefix ^ "bad input" ^ Position.here pos)); |
80897
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
283 |
val _ = Context_Position.reports ctxt (maps reports_of xsymbs); |
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
284 |
val _ = Context_Position.reports_text ctxt (maps reports_text_of xsymbs); |
62764 | 285 |
in xsymbs end; |
19004 | 286 |
|
80908 | 287 |
val read_mfix0 = read_mfix o Context_Position.set_visible false; |
288 |
||
289 |
fun mfix_args ctxt ss = |
|
290 |
Integer.build (read_mfix0 ctxt ss |> fold (fn (xsymb, _) => is_argument xsymb ? Integer.add 1)); |
|
80897
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
291 |
|
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
292 |
fun mixfix_args ctxt = mfix_args ctxt o Input.source_explode; |
19004 | 293 |
|
35390 | 294 |
val escape = implode o map (fn s => if is_meta s then "'" ^ s else s) o Symbol.explode; |
19004 | 295 |
|
4050 | 296 |
end; |
297 |
||
298 |
||
240 | 299 |
(* mfix_to_xprod *) |
300 |
||
80897
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
301 |
fun mfix_to_xprod ctxt logical_types (Mfix (sy, typ, const, pris, pri, pos)) = |
240 | 302 |
let |
80895 | 303 |
val nonterm_for_lhs = the_default "logic" o try dest_Type_name; |
304 |
val nonterm_for_rhs = the_default "any" o try dest_Type_name; |
|
305 |
||
80899
51c338103975
proper Context_Position.report, following 5328d67ec647;
wenzelm
parents:
80897
diff
changeset
|
306 |
val _ = Context_Position.report ctxt pos Markup.language_mixfix; |
80897
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
307 |
val symbs0 = read_mfix ctxt sy; |
62764 | 308 |
|
62762 | 309 |
fun err_in_mixfix msg = error (msg ^ " in mixfix annotation" ^ Position.here pos); |
240 | 310 |
|
62762 | 311 |
fun check_blocks [] pending bad = pending @ bad |
312 |
| check_blocks ((Bg _, pos) :: rest) pending bad = check_blocks rest (pos :: pending) bad |
|
313 |
| check_blocks ((En, pos) :: rest) [] bad = check_blocks rest [] (pos :: bad) |
|
314 |
| check_blocks ((En, _) :: rest) (_ :: pending) bad = check_blocks rest pending bad |
|
315 |
| check_blocks (_ :: rest) pending bad = check_blocks rest pending bad; |
|
240 | 316 |
|
80895 | 317 |
fun add_args [] ty [] = ([], nonterm_for_lhs ty) |
62762 | 318 |
| add_args [] _ _ = err_in_mixfix "Too many precedences" |
319 |
| add_args ((sym as (Argument ("index", _), _)) :: syms) ty ps = |
|
320 |
add_args syms ty ps |>> cons sym |
|
321 |
| add_args ((Argument _, pos) :: syms) (Type ("fun", [ty, tys])) [] = |
|
80895 | 322 |
add_args syms tys [] |>> cons (Argument (nonterm_for_rhs ty, 0), pos) |
62762 | 323 |
| add_args ((Argument _, pos) :: syms) (Type ("fun", [ty, tys])) (p :: ps) = |
80895 | 324 |
add_args syms tys ps |>> cons (Argument (nonterm_for_rhs ty, p), pos) |
62762 | 325 |
| add_args ((Argument _, _) :: _) _ _ = |
326 |
err_in_mixfix "More arguments than in corresponding type" |
|
327 |
| add_args (sym :: syms) ty ps = add_args syms ty ps |>> cons sym; |
|
328 |
||
329 |
fun logical_args (a as (Argument (s, p))) = |
|
330 |
if s <> "prop" andalso member (op =) logical_types s then Argument ("logic", p) else a |
|
331 |
| logical_args a = a; |
|
240 | 332 |
|
333 |
fun rem_pri (Argument (s, _)) = Argument (s, chain_pri) |
|
334 |
| rem_pri sym = sym; |
|
335 |
||
62764 | 336 |
val indexes = filter (is_index o #1) symbs0; |
62762 | 337 |
val _ = |
338 |
if length indexes <= 1 then () |
|
339 |
else error ("More than one index argument" ^ Position.here_list (map #2 indexes)); |
|
2364 | 340 |
|
62764 | 341 |
val args = map_filter (fn (arg as Argument _, _) => SOME arg | _ => NONE) symbs0; |
44470
6c6c31ef6bb2
more accurate treatment of index syntax constants, for proper entity references in concrete notation (e.g. infix "\<oplus>\<index>");
wenzelm
parents:
43329
diff
changeset
|
342 |
val (const', typ', syntax_consts, parse_rules) = |
6c6c31ef6bb2
more accurate treatment of index syntax constants, for proper entity references in concrete notation (e.g. infix "\<oplus>\<index>");
wenzelm
parents:
43329
diff
changeset
|
343 |
if not (exists is_index args) then (const, typ, NONE, NONE) |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
344 |
else |
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
345 |
let |
35429
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35390
diff
changeset
|
346 |
val indexed_const = |
afa8cf9e63d8
authentic syntax for classes and type constructors;
wenzelm
parents:
35390
diff
changeset
|
347 |
if const <> "" then const ^ "_indexed" |
62762 | 348 |
else err_in_mixfix "Missing constant name for indexed syntax"; |
14697 | 349 |
val rangeT = Term.range_type typ handle Match => |
62762 | 350 |
err_in_mixfix "Missing structure argument for indexed syntax"; |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
351 |
|
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43323
diff
changeset
|
352 |
val xs = map Ast.Variable (Name.invent Name.context "xa" (length args - 1)); |
19012 | 353 |
val (xs1, xs2) = chop (find_index is_index args) xs; |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
354 |
val i = Ast.Variable "i"; |
62762 | 355 |
val lhs = |
356 |
Ast.mk_appl (Ast.Constant indexed_const) |
|
357 |
(xs1 @ [Ast.mk_appl (Ast.Constant "_index") [i]] @ xs2); |
|
14697 | 358 |
val rhs = Ast.mk_appl (Ast.Constant const) (i :: xs); |
80748
43c4817375bf
support for syntax const dependencies, with minimal integrity checks;
wenzelm
parents:
77999
diff
changeset
|
359 |
in (indexed_const, rangeT, SOME (indexed_const, [const]), SOME (lhs, rhs)) end; |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
360 |
|
62764 | 361 |
val (symbs1, lhs) = add_args symbs0 typ' pris; |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
362 |
|
2364 | 363 |
val copy_prod = |
20675 | 364 |
(lhs = "prop" orelse lhs = "logic") |
2364 | 365 |
andalso const <> "" |
62764 | 366 |
andalso not (null symbs1) |
367 |
andalso not (exists (is_delim o #1) symbs1); |
|
2364 | 368 |
val lhs' = |
62764 | 369 |
if copy_prod orelse lhs = "prop" andalso map #1 symbs1 = [Argument ("prop'", 0)] then lhs |
42293
6cca0343ea48
renamed sprop "prop#" to "prop'" -- proper identifier;
wenzelm
parents:
42288
diff
changeset
|
370 |
else if lhs = "prop" then "prop'" |
62762 | 371 |
else if member (op =) logical_types lhs then "logic" |
2364 | 372 |
else lhs; |
62764 | 373 |
val symbs2 = map (apfst logical_args) symbs1; |
240 | 374 |
|
62762 | 375 |
val _ = |
376 |
(pri :: pris) |> List.app (fn p => |
|
377 |
if p >= 0 andalso p <= 1000 then () |
|
378 |
else err_in_mixfix ("Precedence " ^ string_of_int p ^ " out of range")); |
|
379 |
val _ = |
|
62764 | 380 |
(case check_blocks symbs2 [] [] of |
62762 | 381 |
[] => () |
382 |
| bad => error ("Unbalanced block parentheses" ^ Position.here_list bad)); |
|
383 |
||
62764 | 384 |
val xprod = XProd (lhs', map #1 symbs2, const', pri); |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
385 |
val xprod' = |
62762 | 386 |
if Lexicon.is_terminal lhs' then |
387 |
err_in_mixfix ("Illegal use of terminal " ^ quote lhs' ^ " as nonterminal") |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
388 |
else if const <> "" then xprod |
62764 | 389 |
else if length (filter (is_argument o #1) symbs2) <> 1 then |
62762 | 390 |
err_in_mixfix "Copy production must have exactly one argument" |
62764 | 391 |
else if exists (is_terminal o #1) symbs2 then xprod |
392 |
else XProd (lhs', map (rem_pri o #1) symbs2, "", chain_pri); |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
393 |
|
44470
6c6c31ef6bb2
more accurate treatment of index syntax constants, for proper entity references in concrete notation (e.g. infix "\<oplus>\<index>");
wenzelm
parents:
43329
diff
changeset
|
394 |
in (xprod', syntax_consts, parse_rules) end; |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
395 |
|
240 | 396 |
|
397 |
||
398 |
(** datatype syn_ext **) |
|
399 |
||
400 |
datatype syn_ext = |
|
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
35429
diff
changeset
|
401 |
Syn_Ext of { |
240 | 402 |
xprods: xprod list, |
80748
43c4817375bf
support for syntax const dependencies, with minimal integrity checks;
wenzelm
parents:
77999
diff
changeset
|
403 |
consts: (string * string list) list, |
21772 | 404 |
parse_ast_translation: (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list, |
1510 | 405 |
parse_rules: (Ast.ast * Ast.ast) list, |
21772 | 406 |
parse_translation: (string * ((Proof.context -> term list -> term) * stamp)) list, |
42247
12fe41a92cd5
typed_print_translation: discontinued show_sorts argument;
wenzelm
parents:
42245
diff
changeset
|
407 |
print_translation: (string * ((Proof.context -> typ -> term list -> term) * stamp)) list, |
1510 | 408 |
print_rules: (Ast.ast * Ast.ast) list, |
42268 | 409 |
print_ast_translation: (string * ((Proof.context -> Ast.ast list -> Ast.ast) * stamp)) list}; |
240 | 410 |
|
411 |
||
412 |
(* syn_ext *) |
|
413 |
||
80897
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
414 |
fun syn_ext ctxt logical_types mfixes consts trfuns (parse_rules, print_rules) = |
240 | 415 |
let |
416 |
val (parse_ast_translation, parse_translation, print_translation, |
|
417 |
print_ast_translation) = trfuns; |
|
418 |
||
80897
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
419 |
val xprod_results = map (mfix_to_xprod ctxt logical_types) mfixes; |
44470
6c6c31ef6bb2
more accurate treatment of index syntax constants, for proper entity references in concrete notation (e.g. infix "\<oplus>\<index>");
wenzelm
parents:
43329
diff
changeset
|
420 |
val xprods = map #1 xprod_results; |
6c6c31ef6bb2
more accurate treatment of index syntax constants, for proper entity references in concrete notation (e.g. infix "\<oplus>\<index>");
wenzelm
parents:
43329
diff
changeset
|
421 |
val consts' = map_filter #2 xprod_results; |
6c6c31ef6bb2
more accurate treatment of index syntax constants, for proper entity references in concrete notation (e.g. infix "\<oplus>\<index>");
wenzelm
parents:
43329
diff
changeset
|
422 |
val parse_rules' = rev (map_filter #3 xprod_results); |
80748
43c4817375bf
support for syntax const dependencies, with minimal integrity checks;
wenzelm
parents:
77999
diff
changeset
|
423 |
val mfix_consts = map (fn Mfix x => (#3 x, [])) mfixes @ map (fn XProd x => (#3 x, [])) xprods; |
240 | 424 |
in |
37216
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
wenzelm
parents:
35429
diff
changeset
|
425 |
Syn_Ext { |
624 | 426 |
xprods = xprods, |
44470
6c6c31ef6bb2
more accurate treatment of index syntax constants, for proper entity references in concrete notation (e.g. infix "\<oplus>\<index>");
wenzelm
parents:
43329
diff
changeset
|
427 |
consts = mfix_consts @ consts' @ consts, |
240 | 428 |
parse_ast_translation = parse_ast_translation, |
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
429 |
parse_rules = parse_rules' @ parse_rules, |
240 | 430 |
parse_translation = parse_translation, |
431 |
print_translation = print_translation, |
|
12513
0ffb824dc95c
support for ``indexed syntax'' (using "\<index>" argument instead of "_");
wenzelm
parents:
11546
diff
changeset
|
432 |
print_rules = map swap parse_rules' @ print_rules, |
42268 | 433 |
print_ast_translation = print_ast_translation} |
240 | 434 |
end; |
435 |
||
2382 | 436 |
|
80897
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
437 |
fun syn_ext_consts ctxt consts = syn_ext ctxt [] [] consts ([], [], [], []) ([], []); |
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
438 |
fun syn_ext_rules ctxt rules = syn_ext ctxt [] [] [] ([], [], [], []) rules; |
5328d67ec647
more explicit context for syn_ext/mixfix operations, but it often degenerates to background theory;
wenzelm
parents:
80895
diff
changeset
|
439 |
fun syn_ext_trfuns ctxt trfuns = syn_ext ctxt [] [] [] trfuns ([], []); |
16610 | 440 |
|
15754 | 441 |
fun stamp_trfun s (c, f) = (c, (f, s)); |
442 |
fun mk_trfun tr = stamp_trfun (stamp ()) tr; |
|
29318
6337d1cb2ba0
added numeral, which supercedes num, xnum, float;
wenzelm
parents:
28904
diff
changeset
|
443 |
fun eq_trfun ((_, s1: stamp), (_, s2)) = s1 = s2; |
15754 | 444 |
|
240 | 445 |
end; |