author | wenzelm |
Sun, 21 May 2000 14:31:41 +0200 | |
changeset 8894 | 0281bde335ca |
parent 8720 | 840c75ab2a7f |
child 9372 | 7834e56e2277 |
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 |
|
5 |
Root of Isabelle's syntax module. |
|
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 |
6322 | 18 |
include TOKEN_TRANS0 |
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 |
1158 | 26 |
datatype 'a trrule = |
3526 | 27 |
ParseRule of 'a * 'a | |
28 |
PrintRule of 'a * 'a | |
|
29 |
ParsePrintRule of 'a * 'a |
|
556 | 30 |
type syntax |
31 |
val extend_log_types: syntax -> string list -> syntax |
|
32 |
val extend_type_gram: syntax -> (string * int * mixfix) list -> syntax |
|
2366 | 33 |
val extend_const_gram: syntax -> string * bool -> (string * typ * mixfix) list -> syntax |
556 | 34 |
val extend_consts: syntax -> string list -> syntax |
35 |
val extend_trfuns: syntax -> |
|
36 |
(string * (ast list -> ast)) list * |
|
37 |
(string * (term list -> term)) list * |
|
38 |
(string * (term list -> term)) list * |
|
39 |
(string * (ast list -> ast)) list -> syntax |
|
4145 | 40 |
val extend_trfunsT: syntax -> (string * (bool -> typ -> term list -> term)) list -> syntax |
6322 | 41 |
val extend_tokentrfuns: syntax -> (string * string * (string -> string * real)) list -> syntax |
1158 | 42 |
val extend_trrules: syntax -> (string * string) trrule list -> syntax |
43 |
val extend_trrules_i: syntax -> ast trrule list -> syntax |
|
4618 | 44 |
val map_trrule: ('a -> 'b) -> 'a trrule -> 'b trrule |
556 | 45 |
val merge_syntaxes: syntax -> syntax -> syntax |
46 |
val type_syn: syntax |
|
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
47 |
val pure_syn: syntax |
556 | 48 |
val print_gram: syntax -> unit |
49 |
val print_trans: syntax -> unit |
|
50 |
val print_syntax: syntax -> unit |
|
51 |
val test_read: syntax -> string -> string -> unit |
|
624 | 52 |
val read: syntax -> typ -> string -> term list |
3779 | 53 |
val read_typ: syntax -> ((indexname * sort) list -> indexname -> sort) -> string -> typ |
8894 | 54 |
val read_sort: syntax -> string -> sort |
556 | 55 |
val simple_read_typ: string -> typ |
3779 | 56 |
val pretty_term: syntax -> bool -> term -> Pretty.T |
556 | 57 |
val pretty_typ: syntax -> typ -> Pretty.T |
3779 | 58 |
val pretty_sort: syntax -> sort -> Pretty.T |
3782 | 59 |
val simple_str_of_sort: sort -> string |
556 | 60 |
val simple_string_of_typ: typ -> string |
61 |
val simple_pprint_typ: typ -> pprint_args -> unit |
|
882
b118d1ea0dfd
moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents:
865
diff
changeset
|
62 |
val ambiguity_level: int ref |
2383 | 63 |
end; |
0 | 64 |
|
1511 | 65 |
structure Syntax : SYNTAX = |
0 | 66 |
struct |
67 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
68 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
69 |
(** tables of translation functions **) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
70 |
|
5692 | 71 |
fun mk_trfun (c, f) = (c, (f, stamp ())); |
72 |
fun eq_trfuns ((c1:string, (_, s1:stamp)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2; |
|
0 | 73 |
|
74 |
||
5692 | 75 |
(* parse (ast) translations *) |
76 |
||
77 |
fun lookup_tr tab c = apsome fst (Symtab.lookup (tab, c)); |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
78 |
|
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
79 |
fun err_dup_trfuns name cs = |
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
80 |
error ("More than one " ^ name ^ " for " ^ commas_quote cs); |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
81 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
82 |
fun extend_trtab tab trfuns name = |
5692 | 83 |
Symtab.extend (tab, map mk_trfun trfuns) |
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
84 |
handle Symtab.DUPS cs => err_dup_trfuns name cs; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
85 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
86 |
fun merge_trtabs tab1 tab2 name = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
87 |
Symtab.merge eq_snd (tab1, tab2) |
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
88 |
handle Symtab.DUPS cs => err_dup_trfuns name cs; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
89 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
90 |
|
5692 | 91 |
(* print (ast) translations *) |
92 |
||
93 |
fun lookup_tr' tab c = map fst (Symtab.lookup_multi (tab, c)); |
|
94 |
||
95 |
fun extend_tr'tab tab trfuns = |
|
96 |
generic_extend eq_trfuns Symtab.dest_multi Symtab.make_multi tab (map mk_trfun trfuns); |
|
97 |
||
5702 | 98 |
fun merge_tr'tabs tabs = generic_merge eq_trfuns Symtab.dest_multi Symtab.make_multi tabs; |
5692 | 99 |
|
100 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
101 |
|
2700 | 102 |
(** tables of token translation functions **) |
103 |
||
104 |
fun lookup_tokentr tabs modes = |
|
4496 | 105 |
let val trs = gen_distinct eq_fst (flat (map (assocs tabs) (modes @ [""]))) |
2700 | 106 |
in fn c => apsome fst (assoc (trs, c)) end; |
107 |
||
108 |
fun merge_tokentrtabs tabs1 tabs2 = |
|
109 |
let |
|
110 |
fun eq_tr ((c1, (_, s1)), (c2, (_, s2))) = c1 = c2 andalso s1 = s2; |
|
111 |
||
4703 | 112 |
fun name (s, _) = implode (tl (Symbol.explode s)); |
2706 | 113 |
|
2700 | 114 |
fun merge mode = |
115 |
let |
|
116 |
val trs1 = assocs tabs1 mode; |
|
117 |
val trs2 = assocs tabs2 mode; |
|
118 |
val trs = gen_distinct eq_tr (trs1 @ trs2); |
|
119 |
in |
|
120 |
(case gen_duplicates eq_fst trs of |
|
121 |
[] => (mode, trs) |
|
122 |
| dups => error ("More than one token translation function in mode " ^ |
|
2706 | 123 |
quote mode ^ " for " ^ commas_quote (map name dups))) |
2700 | 124 |
end; |
125 |
in |
|
126 |
map merge (distinct (map fst (tabs1 @ tabs2))) |
|
127 |
end; |
|
128 |
||
129 |
fun extend_tokentrtab tabs tokentrs = |
|
130 |
let |
|
131 |
fun ins_tokentr (ts, (m, c, f)) = |
|
132 |
overwrite (ts, (m, ("_" ^ c, (f, stamp ())) :: assocs ts m)); |
|
133 |
in |
|
134 |
merge_tokentrtabs tabs (foldl ins_tokentr ([], tokentrs)) |
|
135 |
end; |
|
136 |
||
137 |
||
138 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
139 |
(** tables of translation rules **) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
140 |
|
5692 | 141 |
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
|
142 |
|
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
143 |
fun dest_ruletab tab = flat (map snd (Symtab.dest tab)); |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
144 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
145 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
146 |
(* lookup_ruletab *) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
147 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
148 |
fun lookup_ruletab tab = |
4487 | 149 |
if Symtab.is_empty tab then None |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
150 |
else Some (fn a => Symtab.lookup_multi (tab, a)); |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
151 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
152 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
153 |
(* empty, extend, merge ruletabs *) |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
154 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
155 |
fun extend_ruletab tab rules = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
156 |
generic_extend (op =) Symtab.dest_multi Symtab.make_multi tab |
5692 | 157 |
(map (fn r => (Ast.head_of_rule r, r)) (distinct rules)); |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
158 |
|
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
159 |
fun merge_ruletabs tab1 tab2 = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
160 |
generic_merge (op =) Symtab.dest_multi Symtab.make_multi tab1 tab2; |
0 | 161 |
|
162 |
||
163 |
||
164 |
(** datatype syntax **) |
|
165 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
166 |
datatype syntax = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
167 |
Syntax of { |
4703 | 168 |
lexicon: Scan.lexicon, |
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
169 |
logtypes: string list, |
5692 | 170 |
gram: Parser.gram, |
18 | 171 |
consts: string list, |
2913 | 172 |
prmodes: string list, |
5692 | 173 |
parse_ast_trtab: ((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
|
174 |
parse_ruletab: ruletab, |
5692 | 175 |
parse_trtab: ((term list -> term) * stamp) Symtab.table, |
176 |
print_trtab: ((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
|
177 |
print_ruletab: ruletab, |
5692 | 178 |
print_ast_trtab: ((Ast.ast list -> Ast.ast) * stamp) list Symtab.table, |
6322 | 179 |
tokentrtab: (string * (string * ((string -> string * real) * stamp)) list) list, |
5692 | 180 |
prtabs: Printer.prtabs} |
0 | 181 |
|
18 | 182 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
183 |
(* empty_syntax *) |
18 | 184 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
185 |
val empty_syntax = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
186 |
Syntax { |
4703 | 187 |
lexicon = Scan.empty_lexicon, |
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
188 |
logtypes = [], |
5692 | 189 |
gram = Parser.empty_gram, |
167 | 190 |
consts = [], |
2913 | 191 |
prmodes = [], |
5692 | 192 |
parse_ast_trtab = Symtab.empty, |
193 |
parse_ruletab = Symtab.empty, |
|
194 |
parse_trtab = Symtab.empty, |
|
195 |
print_trtab = Symtab.empty, |
|
196 |
print_ruletab = Symtab.empty, |
|
197 |
print_ast_trtab = Symtab.empty, |
|
2700 | 198 |
tokentrtab = [], |
5692 | 199 |
prtabs = Printer.empty_prtabs} |
167 | 200 |
|
201 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
202 |
(* extend_syntax *) |
167 | 203 |
|
2366 | 204 |
fun extend_syntax (mode, inout) (Syntax tabs) syn_ext = |
167 | 205 |
let |
2913 | 206 |
val {lexicon, logtypes = logtypes1, gram, consts = consts1, prmodes = prmodes1, |
2366 | 207 |
parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, print_ruletab, |
2700 | 208 |
print_ast_trtab, tokentrtab, prtabs} = tabs; |
5692 | 209 |
val SynExt.SynExt {logtypes = logtypes2, xprods, consts = consts2, prmodes = prmodes2, |
2913 | 210 |
parse_ast_translation, parse_rules, parse_translation, print_translation, print_rules, |
2700 | 211 |
print_ast_translation, token_translation} = syn_ext; |
167 | 212 |
in |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
213 |
Syntax { |
5692 | 214 |
lexicon = if inout then Scan.extend_lexicon lexicon (SynExt.delims_of xprods) else lexicon, |
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
215 |
logtypes = extend_list logtypes1 logtypes2, |
5692 | 216 |
gram = if inout then Parser.extend_gram gram xprods else gram, |
167 | 217 |
consts = consts2 union consts1, |
2913 | 218 |
prmodes = (mode ins prmodes2) union prmodes1, |
167 | 219 |
parse_ast_trtab = |
220 |
extend_trtab parse_ast_trtab parse_ast_translation "parse ast translation", |
|
221 |
parse_ruletab = extend_ruletab parse_ruletab parse_rules, |
|
222 |
parse_trtab = extend_trtab parse_trtab parse_translation "parse translation", |
|
5692 | 223 |
print_trtab = extend_tr'tab print_trtab print_translation, |
167 | 224 |
print_ruletab = extend_ruletab print_ruletab print_rules, |
5692 | 225 |
print_ast_trtab = extend_tr'tab print_ast_trtab print_ast_translation, |
2700 | 226 |
tokentrtab = extend_tokentrtab tokentrtab token_translation, |
5692 | 227 |
prtabs = Printer.extend_prtabs prtabs mode xprods} |
18 | 228 |
end; |
229 |
||
230 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
231 |
(* merge_syntaxes *) |
0 | 232 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
233 |
fun merge_syntaxes (Syntax tabs1) (Syntax tabs2) = |
0 | 234 |
let |
2913 | 235 |
val {lexicon = lexicon1, logtypes = logtypes1, gram = gram1, consts = consts1, |
236 |
prmodes = prmodes1, parse_ast_trtab = parse_ast_trtab1, parse_ruletab = parse_ruletab1, |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
237 |
parse_trtab = parse_trtab1, print_trtab = print_trtab1, |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
238 |
print_ruletab = print_ruletab1, print_ast_trtab = print_ast_trtab1, |
2700 | 239 |
tokentrtab = tokentrtab1, prtabs = prtabs1} = tabs1; |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
240 |
|
2913 | 241 |
val {lexicon = lexicon2, logtypes = logtypes2, gram = gram2, consts = consts2, |
242 |
prmodes = prmodes2, parse_ast_trtab = parse_ast_trtab2, parse_ruletab = parse_ruletab2, |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
243 |
parse_trtab = parse_trtab2, print_trtab = print_trtab2, |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
244 |
print_ruletab = print_ruletab2, print_ast_trtab = print_ast_trtab2, |
2700 | 245 |
tokentrtab = tokentrtab2, prtabs = prtabs2} = tabs2; |
0 | 246 |
in |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
247 |
Syntax { |
4703 | 248 |
lexicon = Scan.merge_lexicons lexicon1 lexicon2, |
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
249 |
logtypes = merge_lists logtypes1 logtypes2, |
5692 | 250 |
gram = Parser.merge_grams gram1 gram2, |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
251 |
consts = merge_lists consts1 consts2, |
2913 | 252 |
prmodes = merge_lists prmodes1 prmodes2, |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
253 |
parse_ast_trtab = |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
254 |
merge_trtabs parse_ast_trtab1 parse_ast_trtab2 "parse ast translation", |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
255 |
parse_ruletab = merge_ruletabs parse_ruletab1 parse_ruletab2, |
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
256 |
parse_trtab = merge_trtabs parse_trtab1 parse_trtab2 "parse translation", |
5692 | 257 |
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
|
258 |
print_ruletab = merge_ruletabs print_ruletab1 print_ruletab2, |
5692 | 259 |
print_ast_trtab = merge_tr'tabs print_ast_trtab1 print_ast_trtab2, |
2700 | 260 |
tokentrtab = merge_tokentrtabs tokentrtab1 tokentrtab2, |
5692 | 261 |
prtabs = Printer.merge_prtabs prtabs1 prtabs2} |
0 | 262 |
end; |
263 |
||
264 |
||
260 | 265 |
(* type_syn *) |
266 |
||
5692 | 267 |
val type_syn = extend_syntax ("", true) empty_syntax TypeExt.type_ext; |
268 |
val pure_syn = extend_syntax ("", true) type_syn SynExt.pure_ext; |
|
260 | 269 |
|
0 | 270 |
|
4887 | 271 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
272 |
(** inspect syntax **) |
0 | 273 |
|
260 | 274 |
fun pretty_strs_qs name strs = |
275 |
Pretty.strs (name :: map quote (sort_strings strs)); |
|
0 | 276 |
|
18 | 277 |
|
278 |
(* print_gram *) |
|
0 | 279 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
280 |
fun print_gram (Syntax tabs) = |
0 | 281 |
let |
2913 | 282 |
val {lexicon, logtypes, prmodes, gram, prtabs, ...} = tabs; |
283 |
val prmodes' = sort_strings (filter_out (equal "") prmodes); |
|
0 | 284 |
in |
8720 | 285 |
[pretty_strs_qs "lexicon:" (Scan.dest_lexicon lexicon), |
286 |
Pretty.strs ("logtypes:" :: logtypes), |
|
287 |
Pretty.big_list "prods:" (Parser.pretty_gram gram), |
|
288 |
pretty_strs_qs "print modes:" prmodes'] |
|
289 |
|> Pretty.chunks |> Pretty.writeln |
|
0 | 290 |
end; |
291 |
||
292 |
||
18 | 293 |
(* print_trans *) |
0 | 294 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
295 |
fun print_trans (Syntax tabs) = |
0 | 296 |
let |
260 | 297 |
fun pretty_trtab name tab = |
5692 | 298 |
pretty_strs_qs name (Symtab.keys tab); |
0 | 299 |
|
260 | 300 |
fun pretty_ruletab name tab = |
5692 | 301 |
Pretty.big_list name (map Ast.pretty_rule (dest_ruletab tab)); |
0 | 302 |
|
4703 | 303 |
fun pretty_tokentr (mode, trs) = Pretty.strs (quote mode ^ ":" :: map fst trs); |
304 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
305 |
val {consts, parse_ast_trtab, parse_ruletab, parse_trtab, print_trtab, |
4703 | 306 |
print_ruletab, print_ast_trtab, tokentrtab, ...} = tabs; |
0 | 307 |
in |
8720 | 308 |
[pretty_strs_qs "consts:" consts, |
309 |
pretty_trtab "parse_ast_translation:" parse_ast_trtab, |
|
310 |
pretty_ruletab "parse_rules:" parse_ruletab, |
|
311 |
pretty_trtab "parse_translation:" parse_trtab, |
|
312 |
pretty_trtab "print_translation:" print_trtab, |
|
313 |
pretty_ruletab "print_rules:" print_ruletab, |
|
314 |
pretty_trtab "print_ast_translation:" print_ast_trtab, |
|
315 |
Pretty.big_list "token_translation:" (map pretty_tokentr tokentrtab)] |
|
316 |
|> Pretty.chunks |> Pretty.writeln |
|
0 | 317 |
end; |
318 |
||
319 |
||
320 |
(* print_syntax *) |
|
321 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
322 |
fun print_syntax syn = (print_gram syn; print_trans syn); |
0 | 323 |
|
324 |
||
325 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
326 |
(** read **) |
18 | 327 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
328 |
(* test_read *) |
18 | 329 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
330 |
fun test_read (Syntax tabs) root str = |
18 | 331 |
let |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
332 |
val {lexicon, gram, parse_ast_trtab, parse_ruletab, ...} = tabs; |
18 | 333 |
|
4703 | 334 |
val chars = Symbol.explode str; |
5692 | 335 |
val toks = Lexicon.tokenize lexicon false chars; |
336 |
val _ = writeln ("tokens: " ^ space_implode " " (map Lexicon.display_token toks)); |
|
18 | 337 |
|
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
338 |
fun show_pt pt = |
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
339 |
let |
5692 | 340 |
val raw_ast = SynTrans.pt_to_ast (K None) pt; |
341 |
val _ = writeln ("raw: " ^ Ast.str_of_ast raw_ast); |
|
342 |
val pre_ast = SynTrans.pt_to_ast (lookup_tr parse_ast_trtab) pt; |
|
343 |
val _ = Ast.normalize true true (lookup_ruletab parse_ruletab) pre_ast; |
|
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
344 |
in () end; |
5692 | 345 |
in seq show_pt (Parser.parse gram root toks) end; |
18 | 346 |
|
347 |
||
260 | 348 |
(* read_ast *) |
349 |
||
882
b118d1ea0dfd
moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents:
865
diff
changeset
|
350 |
val ambiguity_level = ref 1; |
b118d1ea0dfd
moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents:
865
diff
changeset
|
351 |
|
865 | 352 |
fun read_asts (Syntax tabs) xids root str = |
260 | 353 |
let |
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
354 |
val {lexicon, gram, parse_ast_trtab, logtypes, ...} = tabs; |
5692 | 355 |
val root' = if root mem logtypes then SynExt.logic else root; |
4703 | 356 |
val chars = Symbol.explode str; |
5692 | 357 |
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
|
358 |
|
7944
cc1930ad1a88
symbols in (error) messages now consistently with single backslash
oheimb
parents:
7025
diff
changeset
|
359 |
fun show_pt pt = warning (Pretty.string_of |
cc1930ad1a88
symbols in (error) messages now consistently with single backslash
oheimb
parents:
7025
diff
changeset
|
360 |
(Ast.pretty_ast (SynTrans.pt_to_ast (K None) pt))); |
260 | 361 |
in |
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
362 |
if length pts > ! ambiguity_level then |
7944
cc1930ad1a88
symbols in (error) messages now consistently with single backslash
oheimb
parents:
7025
diff
changeset
|
363 |
(warning ("Ambiguous input " ^ quote str); |
3526 | 364 |
warning "produces the following parse trees:"; |
882
b118d1ea0dfd
moved ambiguity_level from sign.ML to Syntax/syntax.ML
clasohm
parents:
865
diff
changeset
|
365 |
seq show_pt pts) |
624 | 366 |
else (); |
5692 | 367 |
map (SynTrans.pt_to_ast (lookup_tr parse_ast_trtab)) pts |
260 | 368 |
end; |
369 |
||
370 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
371 |
(* read *) |
0 | 372 |
|
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
373 |
fun read (syn as Syntax tabs) ty str = |
0 | 374 |
let |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
375 |
val {parse_ruletab, parse_trtab, ...} = tabs; |
5692 | 376 |
val asts = read_asts syn false (SynExt.typ_to_nonterm ty) str; |
0 | 377 |
in |
5692 | 378 |
map (SynTrans.ast_to_term (lookup_tr parse_trtab)) |
379 |
(map (Ast.normalize_ast (lookup_ruletab parse_ruletab)) asts) |
|
0 | 380 |
end; |
381 |
||
382 |
||
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
383 |
(* read types *) |
0 | 384 |
|
3779 | 385 |
fun read_typ syn get_sort str = |
5692 | 386 |
(case read syn SynExt.typeT str of |
387 |
[t] => TypeExt.typ_of_term (get_sort (TypeExt.raw_term_sorts t)) t |
|
8894 | 388 |
| _ => error "read_typ: ambiguous syntax"); |
144 | 389 |
|
2585 | 390 |
fun simple_read_typ str = |
391 |
let fun get_sort env xi = if_none (assoc (env, xi)) [] in |
|
3779 | 392 |
read_typ type_syn get_sort str |
2585 | 393 |
end; |
144 | 394 |
|
395 |
||
8894 | 396 |
(* read sorts *) |
397 |
||
398 |
fun read_sort syn str = |
|
399 |
(case read syn TypeExt.sortT str of |
|
400 |
[t] => TypeExt.sort_of_term t |
|
401 |
| _ => error "read_sort: ambiguous syntax"); |
|
402 |
||
403 |
||
18 | 404 |
|
1158 | 405 |
(** prepare translation rules **) |
406 |
||
407 |
datatype 'a trrule = |
|
3526 | 408 |
ParseRule of 'a * 'a | |
409 |
PrintRule of 'a * 'a | |
|
410 |
ParsePrintRule of 'a * 'a; |
|
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
411 |
|
4618 | 412 |
fun map_trrule f (ParseRule (x, y)) = ParseRule (f x, f y) |
413 |
| map_trrule f (PrintRule (x, y)) = PrintRule (f x, f y) |
|
414 |
| map_trrule f (ParsePrintRule (x, y)) = ParsePrintRule (f x, f y); |
|
1158 | 415 |
|
3526 | 416 |
fun parse_rule (ParseRule pats) = Some pats |
417 |
| parse_rule (PrintRule _) = None |
|
418 |
| parse_rule (ParsePrintRule pats) = Some pats; |
|
1158 | 419 |
|
3526 | 420 |
fun print_rule (ParseRule _) = None |
421 |
| print_rule (PrintRule pats) = Some (swap pats) |
|
422 |
| print_rule (ParsePrintRule pats) = Some (swap pats); |
|
1158 | 423 |
|
424 |
||
425 |
fun check_rule (rule as (lhs, rhs)) = |
|
5692 | 426 |
(case Ast.rule_error rule of |
1158 | 427 |
Some msg => |
428 |
error ("Error in syntax translation rule: " ^ msg ^ "\n" ^ |
|
5692 | 429 |
Ast.str_of_ast lhs ^ " -> " ^ Ast.str_of_ast rhs) |
1158 | 430 |
| None => rule); |
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
431 |
|
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
432 |
|
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
433 |
fun read_pattern syn (root, str) = |
18 | 434 |
let |
237
a7d3e712767a
MAJOR INTERNAL CHANGE: extend and merge operations of syntax tables
wenzelm
parents:
175
diff
changeset
|
435 |
val Syntax {consts, ...} = syn; |
18 | 436 |
|
5692 | 437 |
fun constify (ast as Ast.Constant _) = ast |
438 |
| constify (ast as Ast.Variable x) = |
|
8720 | 439 |
if x mem consts orelse NameSpace.is_qualified x then Ast.Constant x |
3830 | 440 |
else ast |
5692 | 441 |
| constify (Ast.Appl asts) = Ast.Appl (map constify asts); |
18 | 442 |
in |
888
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
443 |
(case read_asts syn true root str of |
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
444 |
[ast] => constify ast |
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
445 |
| _ => error ("Syntactically ambiguous input: " ^ quote str)) |
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
446 |
end handle ERROR => |
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
447 |
error ("The error(s) above occurred in translation pattern " ^ |
3a1de9454d13
improved read_xrules: patterns no longer read twice;
wenzelm
parents:
882
diff
changeset
|
448 |
quote str); |
0 | 449 |
|
556 | 450 |
|
1158 | 451 |
fun prep_rules rd_pat raw_rules = |
4618 | 452 |
let val rules = map (map_trrule rd_pat) raw_rules in |
3526 | 453 |
(map check_rule (mapfilter parse_rule rules), |
454 |
map check_rule (mapfilter print_rule rules)) |
|
1158 | 455 |
end |
18 | 456 |
|
457 |
||
458 |
||
3779 | 459 |
(** pretty terms, typs, sorts **) |
18 | 460 |
|
6167
2f354020efc3
corrected output of symbols for several (probably not all) relevant functions
oheimb
parents:
5702
diff
changeset
|
461 |
val print_mode = print_mode; |
3779 | 462 |
fun pretty_t t_to_ast prt_t (syn as Syntax tabs) curried t = |
0 | 463 |
let |
2700 | 464 |
val {print_trtab, print_ruletab, print_ast_trtab, tokentrtab, prtabs, ...} = tabs; |
5692 | 465 |
val ast = t_to_ast (lookup_tr' print_trtab) t; |
0 | 466 |
in |
5692 | 467 |
prt_t 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
|
468 |
(lookup_tokentr tokentrtab (! print_mode)) |
5692 | 469 |
(Ast.normalize_ast (lookup_ruletab print_ruletab) ast) |
0 | 470 |
end; |
471 |
||
5692 | 472 |
val pretty_term = pretty_t Printer.term_to_ast Printer.pretty_term_ast; |
473 |
fun pretty_typ syn = pretty_t Printer.typ_to_ast Printer.pretty_typ_ast syn false; |
|
474 |
fun pretty_sort syn = pretty_t Printer.sort_to_ast Printer.pretty_typ_ast syn false; |
|
0 | 475 |
|
3782 | 476 |
val simple_str_of_sort = Pretty.str_of o pretty_sort type_syn; |
3779 | 477 |
val simple_string_of_typ = Pretty.string_of o (pretty_typ type_syn); |
260 | 478 |
val simple_pprint_typ = Pretty.pprint o Pretty.quote o (pretty_typ type_syn); |
0 | 479 |
|
480 |
||
481 |
||
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
482 |
(** extend syntax (external interfaces) **) |
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
483 |
|
2202
cc15a7bfbe12
extend_const_gram now supports multiple disjoint printer tables;
wenzelm
parents:
1813
diff
changeset
|
484 |
fun ext_syntax mk_syn_ext prmode (syn as Syntax {logtypes, ...}) decls = |
cc15a7bfbe12
extend_const_gram now supports multiple disjoint printer tables;
wenzelm
parents:
1813
diff
changeset
|
485 |
extend_syntax prmode syn (mk_syn_ext logtypes decls); |
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
486 |
|
167 | 487 |
|
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
624
diff
changeset
|
488 |
fun extend_log_types syn logtypes = |
5692 | 489 |
extend_syntax ("", true) syn (SynExt.syn_ext_logtypes logtypes); |
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
490 |
|
5692 | 491 |
val extend_type_gram = ext_syntax Mixfix.syn_ext_types ("", true); |
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
492 |
|
5692 | 493 |
fun extend_const_gram syn prmode = ext_syntax Mixfix.syn_ext_consts prmode syn; |
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
494 |
|
5692 | 495 |
val extend_consts = ext_syntax SynExt.syn_ext_const_names ("", true); |
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
496 |
|
5692 | 497 |
val extend_trfuns = ext_syntax SynExt.syn_ext_trfuns ("", true); |
383
fcea89074e4c
added incremental extension functions: extend_log_types, extend_type_gram,
wenzelm
parents:
330
diff
changeset
|
498 |
|
5692 | 499 |
val extend_trfunsT = ext_syntax SynExt.syn_ext_trfunsT ("", true); |
2383 | 500 |
|
5692 | 501 |
val extend_tokentrfuns = ext_syntax SynExt.syn_ext_tokentrfuns ("", true); |
2700 | 502 |
|
1158 | 503 |
fun extend_trrules syn rules = |
5692 | 504 |
ext_syntax SynExt.syn_ext_rules ("", true) syn (prep_rules (read_pattern syn) rules); |
1158 | 505 |
|
506 |
fun extend_trrules_i syn rules = |
|
5692 | 507 |
ext_syntax SynExt.syn_ext_rules ("", true) syn (prep_rules I rules); |
508 |
||
509 |
||
510 |
||
511 |
(** export parts of internal Syntax structures **) |
|
512 |
||
6322 | 513 |
open TokenTrans Lexicon SynExt Ast Parser TypeExt SynTrans Mixfix Printer; |
2366 | 514 |
|
515 |
||
0 | 516 |
end; |
5692 | 517 |
|
518 |
||
519 |
structure BasicSyntax: BASIC_SYNTAX = Syntax; |
|
520 |
open BasicSyntax; |