1 (* Title: doc-src/rail.ML
2 Author: Michael Kerscher, TUM
4 Railroad diagrams for LaTeX.
11 Identifier of string |
12 Special_Identifier of string |
18 fun is_identifier (Identifier _) = true
19 | is_identifier (Special_Identifier _ ) = true
20 | is_identifier _ = false;
22 fun is_text (Text _) = true
25 fun is_anot (Anot _) = true
28 fun is_symbol (Symbol _) = true
29 | is_symbol _ = false;
31 fun is_ str = (fn s => s = Symbol str);
34 local (* copied from antiquote-setup... *)
35 fun translate f = Symbol.explode #> map f #> implode;
37 fun clean_name "\<dots>" = "dots"
38 | clean_name ".." = "ddot"
39 | clean_name "." = "dot"
40 | clean_name "_" = "underscore"
41 | clean_name "{" = "braceleft"
42 | clean_name "}" = "braceright"
43 | clean_name s = s |> translate (fn "_" => "-" | "\<dash>" => "-" | c => c);
45 fun no_check _ _ = true;
47 fun false_check _ _ = false;
49 fun thy_check intern defined ctxt =
50 let val thy = ProofContext.theory_of ctxt
51 in defined thy o intern thy end;
54 File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
56 val arg = enclose "{" "}";
59 (* [(kind, (markup, check, style))*)
61 ("syntax", ("", no_check, true)),
62 ("command", ("isacommand", K (is_some o Keyword.command_keyword), true)),
63 ("keyword", ("isakeyword", K Keyword.is_keyword, true)),
64 ("element", ("isakeyword", K Keyword.is_keyword, true)),
65 ("method", ("", thy_check Method.intern Method.defined, true)),
66 ("attribute", ("", thy_check Attrib.intern Attrib.defined, true)),
67 ("fact", ("", no_check, true)),
68 ("variable", ("", no_check, true)),
69 ("case", ("", no_check, true)),
70 ("antiquotation", ("", K Thy_Output.defined_command, true)),
71 ("antiquotation option", ("", K Thy_Output.defined_option, true)), (* kann mein scanner nicht erkennen *)
72 ("setting", ("isatt", (fn _ => fn name => is_some (OS.Process.getEnv name)), true)),
73 ("inference", ("", no_check, true)),
74 ("executable", ("isatt", no_check, true)),
75 ("tool", ("isatt", K check_tool, true)),
76 ("file", ("isatt", K (File.exists o Path.explode), true)),
77 ("theory", ("", K Thy_Info.known_thy, true))
82 fun decode_link ctxt (kind,index,logic,name) =
84 val (markup, check, style) = case Symtab.lookup markup_table kind of
86 | NONE => ("", false_check, false);
88 "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
90 enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
91 index = "def" ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
94 else "\\index" ^ index ^ arg logic ^ arg kind ^ arg name;
96 if check ctxt name then
99 |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
100 |> (if ! Thy_Output.quotes then quote else I)
101 |> (if ! Thy_Output.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
102 else hyper o enclose "\\mbox{\\isa{" "}}")), style)
103 else ("Bad " ^ kind ^ " " ^ name, false)
108 Scan.many Symbol.is_blank >> implode;
111 $$ ";" || $$ ":"|| $$ "("|| $$ ")"|| $$ "+"|| $$ "|"|| $$ "*"|| $$ "?"|| $$ "\\";
113 (* escaped version *)
114 val scan_link = (* @{kind{_def|_ref (logic) name} *)
116 fun pseudo_antiquote inner_scan = ($$ "@" ^^ $$ "{") |-- inner_scan --| (blanks -- $$ "}");
117 fun parens scan = $$ "(" |-- scan --| $$ ")";
118 fun opt_quotes scan = $$ "'" |-- scan --| $$ "'" || scan;
119 val letters = Scan.many Symbol.is_letter >> implode;
120 val kind_name = letters;
121 val opt_kind_type = Scan.optional (
122 $$ "_" |-- (Scan.this_string "def" || Scan.this_string "ref")) "";
123 val logic_name = letters;
124 val escaped_singlequote = $$ "\\" |-- $$ "'";
125 val text = Scan.repeat (Scan.one Symbol.is_letter || escaped_singlequote) >> implode;
128 kind_name -- opt_kind_type --
129 (blanks |-- Scan.optional ( parens logic_name ) "") --
130 (blanks |-- opt_quotes text) )
131 >> (fn (((kind,index),logic),name) => (kind, index, logic, name))
134 (* escaped version *)
135 fun scan_identifier ctxt =
136 let fun is_identifier_start s =
137 Symbol.is_letter s orelse
139 fun is_identifier_rest s =
140 Symbol.is_letter s orelse
141 Symbol.is_digit s orelse
145 (Scan.one is_identifier_start :::
146 Scan.repeat (Scan.one is_identifier_rest || ($$ "\\" |-- $$ "'"))
147 ) >> (Identifier o enclose "\\isa{" "}" o Output.output o implode) ||
148 scan_link >> (decode_link ctxt) >>
150 if style then Special_Identifier(txt)
151 else Identifier(txt))
160 Symbol.is_regular s) >> implode
162 $$ "[" |-- scan_link --| $$ "]" >> (fst o (decode_link ctxt)) ||
163 $$ "[" |-- scan_anot --| $$ "]" >> Output.output
166 (* escaped version *)
176 Symbol.is_regular s) ||
179 fun quoted scan = $$ "'" |-- scan --| $$ "'";
181 quoted scan_link >> (fst o (decode_link ctxt)) ||
182 quoted text_sq >> (enclose "\\isa{" "}" o Output.output)
186 Scan.repeat ( blanks |-- (
187 scan_identifier ctxt ||
188 scan_anot ctxt >> Anot ||
189 scan_text ctxt >> Text ||
190 scan_symbol >> Symbol)
193 fun lex_rail txt ctxt = (* Symbol_Pos fuer spaeter durchgereicht *)
194 Symbol.scanner "Malformed rail-declaration" (scan_rail ctxt) (map fst (Symbol_Pos.explode txt));
198 datatype id_kind = UNKNOWN | TOKEN | TERM | NTERM;
201 Id of string * id_kind |
206 CR | EMPTY | ANNOTE | IDENT | STRING |
210 Body of body_kind * string * string * id_type * body_type list |
211 Body_Pos of body_kind * string * string * id_type * body_type list * int * int |
216 Rule of id_type * body_type;
218 fun new_id id kind = Id (id, kind);
220 fun is_empty (Body(kind,_,_,_,_)) = kind = EMPTY;
222 fun new_body (kind, Null_Body, Null_Body) = Body (kind, "", "", Null_Id, [])
223 | new_body (kind, body1, body2) = Body (kind, "", "", Null_Id, body1 :: [body2]);
225 fun is_kind_of kind (Body(bodyKind,_,_,_,_)) = kind = bodyKind
226 | is_kind_of _ _ = false;
228 fun add_list (Body(kind, text, annot, id, bodies), body) =
229 Body(kind, text, annot, id, bodies @ [body]);
231 fun cat_body_lists (Body(kind, text, annot, id, bodies1), Body(_,_,_,_, bodies2)) =
232 Body(kind, text, annot, id, bodies1 @ bodies2);
234 fun add_body (kind, body1 as Body(kind1,_,_,_,_), body2 as Body(kind2,_,_,_,_)) =
235 if kind = kind1 andalso kind <> BAR then
237 cat_body_lists(body1, body2)
238 else (* kind <> kind2 *)
239 add_list(body1, body2)
240 else (* kind <> kind1 orelse kind = BAR *)
242 cat_body_lists(add_list(new_body(kind,Null_Body,Null_Body), body1), body2)
243 else (* kind <> kind2 *)
244 add_list(add_list(new_body(kind,Null_Body,Null_Body), body1), body2);
246 fun rev_body (body as Body (kind, text, annot, id, [])) = body
247 | rev_body (Body (CAT, text, annot, id, bodies)) =
248 Body(CAT, text, annot, id, map rev_body (rev bodies))
249 | rev_body (Body (kind, text, annot, id, bodies)) =
250 Body(kind, text, annot, id, map rev_body bodies)
251 | rev_body body = body;
253 fun set_body_text text (Body(k,_,a,i,b)) = Body(k,text,a,i,b);
254 fun set_body_anot anot (Body(k,t,_,i,b)) = Body(k,t,anot,i,b);
255 fun set_body_ident id (Body(k,t,a,_,b)) = Body(k,t,a, new_id id TOKEN,b);
256 fun set_body_special_ident id (Body(k,t,a,_,b)) = Body(k,t,a, new_id id TERM,b);
260 fun is_eof s = s = EOF;
261 val stopper = Scan.stopper mk_eof is_eof;
263 (* TODO: change this, so the next or next two tokens are printed *)
264 fun lex_err msg (cs, _) = "rail grammar error: " ^ msg cs;
265 fun !!! msg scan = Scan.!! (lex_err (K msg)) scan;
266 fun $$$ tok = Scan.one (is_ tok);
270 fun new_bar_body([], body2) = body2
271 | new_bar_body(body1::bodies, body2) =
272 add_body(BAR, body1, new_bar_body(bodies, body2));
274 fun new_cat_body(body::[]) = body
275 | new_cat_body(body1::bodies) = add_body(CAT, body1, new_cat_body(bodies));
277 fun new_annote_body (Anot anot) =
278 set_body_text anot (new_body(ANNOTE, Empty_Body, Empty_Body));
280 fun new_text_annote_body (Text text, Anot anot) =
281 set_body_anot anot (set_body_text text (new_body(STRING, Empty_Body, Empty_Body)));
283 fun new_ident_body (Identifier ident, Anot anot) =
284 set_body_anot anot (set_body_ident ident (new_body(IDENT, Empty_Body, Empty_Body)))
285 | new_ident_body (Special_Identifier ident, Anot anot) =
286 set_body_anot anot (set_body_special_ident ident (new_body(IDENT, Empty_Body, Empty_Body)));
288 val new_empty_body = new_body(EMPTY, Null_Body, Null_Body);
293 Scan.repeat1 (parse_body0 --| $$$ "|") -- !!! "body0 expected" (parse_body0) >>
299 Scan.one is_anot -- !!! "body1 expected" (parse_body1) >>
300 (fn (anot, body) => add_body(CAT, new_annote_body(anot), body)) ||
305 parse_body2 -- ($$$ "*" |-- !!! "body4e expected" (parse_body4e)) >>
306 (fn (body1, body2) =>
307 if is_empty body2 then
308 add_body(PLUS, new_empty_body, rev_body body1)
310 add_body(BAR, new_empty_body, add_body (PLUS, body1, rev_body body2)) ) ||
311 parse_body2 -- ($$$ "+" |-- !!! "body4e expected" (parse_body4e)) >>
312 (fn (body1, body2) => new_body (PLUS, body1, rev_body body2) ) ||
318 (fn toks => (new_empty_body, toks))
322 Scan.repeat1 (parse_body3) >> new_cat_body
326 parse_body4 --| $$$ "?" >> (fn body => new_body (BAR, new_empty_body, body) ) ||
332 (fn toks => (new_empty_body, toks))
336 $$$ "(" |-- !!! "body0 or ')' expected" (parse_body --| $$$ ")") ||
337 Scan.one is_text -- (Scan.optional (Scan.one is_anot) (Anot(""))) >>
338 (fn (text, anot) => new_text_annote_body (text,anot)) ||
339 Scan.one is_identifier -- (Scan.optional (Scan.one is_anot) (Anot(""))) >>
340 (fn (id, anot) => new_ident_body (id,anot)) ||
341 $$$ "\\" >> (fn _ => new_body (CR, Null_Body, Null_Body))
345 fun new_named_rule (Identifier name, body) = Rule(Id(name, UNKNOWN), body)
346 | new_named_rule (Special_Identifier name, body) = Rule(Id(name, UNKNOWN), body);
347 fun new_anonym_rule body = Rule(Null_Id, body);
350 (Scan.one (is_identifier) -- ($$$ ":" |-- !!! "body expected" (parse_body)) ) >>
352 parse_body >> new_anonym_rule;
355 Scan.repeat ( parse_rule --| $$$ ";") @@@ Scan.single parse_rule;
358 Scan.read stopper parse_rules s;
360 val parse = parse_rail;
362 fun getystart (Body_Pos(_,_,_,_,_,ystart,_)) = ystart;
363 fun getynext (Body_Pos(_,_,_,_,_,_,ynext)) = ynext;
365 fun position_body (body as Body(kind, text, annot, id, bodies), ystart) =
366 let fun max (x,y) = if x > y then x else y
367 fun set_body_position (Body(kind, text, annot, id, bodies), ystart, ynext) =
368 Body_Pos(kind, text, annot, id, bodies, ystart, ynext)
369 fun pos_bodies_cat ([],_,ynext,liste) = (liste, ynext)
370 | pos_bodies_cat (x::xs, ystart, ynext, liste) =
371 if is_kind_of CR x then
372 (case set_body_position(x, ystart, ynext+1) of
373 body as Body_Pos(_,_,_,_,_,_,ynext1) =>
374 pos_bodies_cat(xs, ynext1, max(ynext,ynext1), liste@[body])
377 (case position_body(x, ystart) of
378 body as Body_Pos(_,_,_,_,_,_,ynext1) =>
379 pos_bodies_cat(xs, ystart, max(ynext,ynext1), liste@[body])
381 fun pos_bodies_bar_plus ([],_,ynext,liste) = (liste, ynext)
382 | pos_bodies_bar_plus (x::xs, ystart, ynext, liste) =
383 (case position_body(x, ystart) of
384 body as Body_Pos(_,_,_,_,_,_,ynext1) =>
385 pos_bodies_bar_plus(xs, ynext1, max(ynext,ynext1), liste@[body])
389 CAT => (case pos_bodies_cat(bodies,ystart,ystart+1,[]) of
390 (bodiesPos, ynext) =>
391 Body_Pos(kind, text, annot, id, bodiesPos, ystart, ynext))
392 | BAR => (case pos_bodies_bar_plus(bodies,ystart,ystart+1,[]) of
393 (bodiesPos, ynext) =>
394 Body_Pos(kind, text, annot, id, bodiesPos, ystart, ynext))
395 | PLUS => (case pos_bodies_bar_plus(bodies,ystart,ystart+1,[]) of
396 (bodiesPos, ynext) =>
397 Body_Pos(kind, text, annot, id, bodiesPos, ystart, ynext))
398 | CR => set_body_position(body, ystart, ystart+3)
399 | EMPTY => set_body_position(body, ystart, ystart+1)
400 | ANNOTE => set_body_position(body, ystart, ystart+1)
401 | IDENT => set_body_position(body, ystart, ystart+1)
402 | STRING => set_body_position(body, ystart, ystart+1)
406 fun format_body (Body_Pos(EMPTY,_,_,_,_,_,_), _) = ""
407 | format_body (Body_Pos(CAT,_,_,_,bodies,_,_), cent) =
408 let fun format_bodies([]) = ""
409 | format_bodies(x::xs) = format_body (x, "") ^ format_bodies(xs)
411 format_bodies(bodies)
413 | format_body (Body_Pos(BAR,_,_,_,bodies,_,_),cent) =
414 let fun format_bodies([]) = "\\rail@endbar\n"
415 | format_bodies(x::xs) =
416 "\\rail@nextbar{" ^ string_of_int(getystart(x)) ^"}\n" ^
417 format_body(x, "") ^ format_bodies(xs)
419 "\\rail@bar\n" ^ format_body(hd(bodies), "") ^ format_bodies(tl(bodies))
421 | format_body (Body_Pos(PLUS,_,_,_,x::y::xs,_,_),cent) =
422 "\\rail@plus\n" ^ format_body(x, cent) ^
423 "\\rail@nextplus{" ^ string_of_int(getystart(y)) ^ "}\n" ^
424 format_body(y, "c") ^
426 | format_body (Body_Pos(ANNOTE,text,_,_,_,_,_),cent) =
427 "\\rail@annote[" ^ text ^ "]\n"
428 | format_body (Body_Pos(IDENT,_,annot,Id(name,TERM),_,_,_),cent) =
429 "\\rail@" ^ cent ^ "token{" ^ name ^ "}" ^ "[" ^ annot ^ "]\n"
430 | format_body (Body_Pos(IDENT,_,annot,Id(name,_),_,_,_),cent) =
431 "\\rail@" ^ cent ^ "nont{" ^ name ^ "}" ^ "[" ^ annot ^ "]\n"
432 | format_body (Body_Pos(CR,_,_,_,_,_,ynext),cent) =
433 "\\rail@cr{" ^ string_of_int(ynext) ^ "}\n"
434 | format_body (Body_Pos(STRING,text,annot,_,_,_,_),cent) =
435 "\\rail@" ^ cent ^ "term{" ^ text ^ "}[" ^ annot ^ "]\n"
439 fun out_body (Id(name,_), body) =
440 let val bodyPos as Body_Pos(_,_,_,_,_,_,ynext) = position_body(body,0)
442 "\\rail@begin{" ^ string_of_int(ynext) ^ "}{" ^ name ^ "}\n" ^
443 format_body(bodyPos,"") ^
446 | out_body (Null_Id, body) = out_body (Id("", UNKNOWN), body);
448 fun out_rule (Rule(id, body)) =
449 if is_empty body then ""
450 else out_body (id, body);
452 fun out_rules ([]) = ""
453 | out_rules (rule::rules) = out_rule rule ^ out_rules rules;
455 val output_no_rules =
456 "\\rail@begin{1}{}\n" ^
457 "\\rail@setbox{\\bfseries ???}\n" ^
462 fun print (SOME rules) =
463 "\\begin{railoutput}\n" ^
465 "\\end{railoutput}\n"
467 "\\begin{railoutput}\n" ^
469 "\\end{railoutput}\n";
471 fun process txt ctxt =
476 val _ = Thy_Output.antiquotation "rail" (Scan.lift (Parse.position Args.name))
477 (fn {context = ctxt,...} => fn txt => process txt ctxt);