author | wenzelm |
Fri, 23 Aug 2013 20:53:00 +0200 | |
changeset 53172 | 31e24d6ff1ea |
parent 53171 | a5e54d4d9081 |
child 55029 | 61a6bf7d4b02 |
permissions | -rw-r--r-- |
42504 | 1 |
(* Title: Pure/Thy/rail.ML |
2 |
Author: Michael Kerscher, TU München |
|
3 |
Author: Makarius |
|
4 |
||
5 |
Railroad diagrams in LaTeX. |
|
6 |
*) |
|
7 |
||
8 |
structure Rail: sig end = |
|
9 |
struct |
|
10 |
||
11 |
(** lexical syntax **) |
|
12 |
||
13 |
(* datatype token *) |
|
14 |
||
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
15 |
datatype kind = |
42516 | 16 |
Keyword | Ident | String | Antiq of Symbol_Pos.T list * Position.range | EOF; |
42504 | 17 |
|
18 |
datatype token = Token of Position.range * (kind * string); |
|
19 |
||
20 |
fun pos_of (Token ((pos, _), _)) = pos; |
|
21 |
fun end_pos_of (Token ((_, pos), _)) = pos; |
|
22 |
||
23 |
fun kind_of (Token (_, (k, _))) = k; |
|
24 |
fun content_of (Token (_, (_, x))) = x; |
|
25 |
||
26 |
||
27 |
(* diagnostics *) |
|
28 |
||
29 |
val print_kind = |
|
30 |
fn Keyword => "rail keyword" |
|
31 |
| Ident => "identifier" |
|
32 |
| String => "single-quoted string" |
|
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
33 |
| Antiq _ => "antiquotation" |
48911
5debc3e4fa81
tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents:
48764
diff
changeset
|
34 |
| EOF => "end-of-input"; |
42504 | 35 |
|
36 |
fun print (Token ((pos, _), (k, x))) = |
|
37 |
(if k = EOF then print_kind k else print_kind k ^ " " ^ quote x) ^ |
|
48992 | 38 |
Position.here pos; |
42504 | 39 |
|
40 |
fun print_keyword x = print_kind Keyword ^ " " ^ quote x; |
|
41 |
||
42 |
||
43 |
(* stopper *) |
|
44 |
||
45 |
fun mk_eof pos = Token ((pos, Position.none), (EOF, "")); |
|
46 |
val eof = mk_eof Position.none; |
|
47 |
||
48 |
fun is_eof (Token (_, (EOF, _))) = true |
|
49 |
| is_eof _ = false; |
|
50 |
||
51 |
val stopper = |
|
52 |
Scan.stopper (fn [] => eof | toks => mk_eof (end_pos_of (List.last toks))) is_eof; |
|
53 |
||
54 |
||
55 |
(* tokenize *) |
|
56 |
||
57 |
local |
|
58 |
||
59 |
fun token k ss = [Token (Symbol_Pos.range ss, (k, Symbol_Pos.content ss))]; |
|
60 |
||
61 |
val scan_space = Scan.many1 (Symbol.is_blank o Symbol_Pos.symbol); |
|
62 |
||
63 |
val scan_keyword = |
|
42516 | 64 |
Scan.one (member (op =) ["|", "*", "+", "?", "(", ")", "\\", ";", ":", "@"] o Symbol_Pos.symbol); |
42504 | 65 |
|
48764 | 66 |
val err_prefix = "Rail lexical error: "; |
67 |
||
42504 | 68 |
val scan_token = |
69 |
scan_space >> K [] || |
|
42516 | 70 |
Antiquote.scan_antiq >> (fn antiq as (ss, _) => token (Antiq antiq) ss) || |
42504 | 71 |
scan_keyword >> (token Keyword o single) || |
72 |
Lexicon.scan_id >> token Ident || |
|
48764 | 73 |
Symbol_Pos.scan_string_q err_prefix >> (token String o #1 o #2); |
42504 | 74 |
|
42506
876887b07e8d
more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents:
42504
diff
changeset
|
75 |
val scan = |
876887b07e8d
more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents:
42504
diff
changeset
|
76 |
(Scan.repeat scan_token >> flat) --| |
48764 | 77 |
Symbol_Pos.!!! (fn () => err_prefix ^ "bad input") |
42506
876887b07e8d
more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents:
42504
diff
changeset
|
78 |
(Scan.ahead (Scan.one Symbol_Pos.is_eof)); |
876887b07e8d
more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents:
42504
diff
changeset
|
79 |
|
42504 | 80 |
in |
81 |
||
42507 | 82 |
val tokenize = #1 o Scan.error (Scan.finite Symbol_Pos.stopper scan) o Symbol_Pos.explode; |
42504 | 83 |
|
84 |
end; |
|
85 |
||
86 |
||
87 |
||
88 |
(** parsing **) |
|
89 |
||
90 |
fun !!! scan = |
|
91 |
let |
|
92 |
val prefix = "Rail syntax error"; |
|
93 |
||
48911
5debc3e4fa81
tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents:
48764
diff
changeset
|
94 |
fun get_pos [] = " (end-of-input)" |
48992 | 95 |
| get_pos (tok :: _) = Position.here (pos_of tok); |
42504 | 96 |
|
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43564
diff
changeset
|
97 |
fun err (toks, NONE) = (fn () => prefix ^ get_pos toks) |
42504 | 98 |
| err (toks, SOME msg) = |
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43564
diff
changeset
|
99 |
(fn () => |
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43564
diff
changeset
|
100 |
let val s = msg () in |
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43564
diff
changeset
|
101 |
if String.isPrefix prefix s then s |
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43564
diff
changeset
|
102 |
else prefix ^ get_pos toks ^ ": " ^ s |
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43564
diff
changeset
|
103 |
end); |
42504 | 104 |
in Scan.!! err scan end; |
105 |
||
106 |
fun $$$ x = |
|
107 |
Scan.one (fn tok => kind_of tok = Keyword andalso content_of tok = x) || |
|
108 |
Scan.fail_with |
|
48911
5debc3e4fa81
tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents:
48764
diff
changeset
|
109 |
(fn [] => (fn () => print_keyword x ^ " expected,\nbut end-of-input was found") |
43947
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
wenzelm
parents:
43564
diff
changeset
|
110 |
| tok :: _ => (fn () => print_keyword x ^ " expected,\nbut " ^ print tok ^ " was found")); |
42504 | 111 |
|
112 |
fun enum1 sep scan = scan ::: Scan.repeat ($$$ sep |-- !!! scan); |
|
113 |
fun enum sep scan = enum1 sep scan || Scan.succeed []; |
|
114 |
||
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
115 |
val ident = Scan.some (fn tok => if kind_of tok = Ident then SOME (content_of tok) else NONE); |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
116 |
val string = Scan.some (fn tok => if kind_of tok = String then SOME (content_of tok) else NONE); |
42504 | 117 |
|
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
118 |
val antiq = Scan.some (fn tok => (case kind_of tok of Antiq a => SOME a | _ => NONE)); |
42504 | 119 |
|
120 |
||
121 |
||
122 |
(** rail expressions **) |
|
123 |
||
124 |
(* datatype *) |
|
125 |
||
126 |
datatype rails = |
|
127 |
Cat of int * rail list |
|
128 |
and rail = |
|
129 |
Bar of rails list | |
|
130 |
Plus of rails * rails | |
|
131 |
Newline of int | |
|
132 |
Nonterminal of string | |
|
42516 | 133 |
Terminal of bool * string | |
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
134 |
Antiquote of bool * (Symbol_Pos.T list * Position.range); |
42504 | 135 |
|
136 |
fun reverse_cat (Cat (y, rails)) = Cat (y, rev (map reverse rails)) |
|
137 |
and reverse (Bar cats) = Bar (map reverse_cat cats) |
|
138 |
| reverse (Plus (cat1, cat2)) = Plus (reverse_cat cat1, reverse_cat cat2) |
|
139 |
| reverse x = x; |
|
140 |
||
141 |
fun cat rails = Cat (0, rails); |
|
142 |
||
143 |
val empty = cat []; |
|
144 |
fun is_empty (Cat (_, [])) = true | is_empty _ = false; |
|
145 |
||
146 |
fun is_newline (Newline _) = true | is_newline _ = false; |
|
147 |
||
148 |
fun bar [Cat (_, [rail])] = rail |
|
149 |
| bar cats = Bar cats; |
|
150 |
||
151 |
fun plus cat1 cat2 = Plus (cat1, reverse_cat cat2); |
|
152 |
||
153 |
fun star cat1 cat2 = |
|
154 |
if is_empty cat2 then plus empty cat1 |
|
155 |
else bar [empty, cat [plus cat1 cat2]]; |
|
156 |
||
157 |
fun maybe rail = bar [empty, cat [rail]]; |
|
158 |
||
159 |
||
160 |
(* read *) |
|
161 |
||
162 |
local |
|
163 |
||
42516 | 164 |
val at_mode = Scan.option ($$$ "@") >> (fn NONE => false | _ => true); |
165 |
||
42504 | 166 |
fun body x = (enum1 "|" body1 >> bar) x |
167 |
and body0 x = (enum "|" body1 >> bar) x |
|
168 |
and body1 x = |
|
169 |
(body2 :|-- (fn a => |
|
170 |
$$$ "*" |-- !!! body4e >> (cat o single o star a) || |
|
171 |
$$$ "+" |-- !!! body4e >> (cat o single o plus a) || |
|
172 |
Scan.succeed a)) x |
|
173 |
and body2 x = (Scan.repeat1 body3 >> cat) x |
|
174 |
and body3 x = (body4 :|-- (fn a => $$$ "?" >> K (maybe a) || Scan.succeed a)) x |
|
175 |
and body4 x = |
|
176 |
($$$ "(" |-- !!! (body0 --| $$$ ")") || |
|
177 |
$$$ "\\" >> K (Newline 0) || |
|
178 |
ident >> Nonterminal || |
|
42516 | 179 |
at_mode -- string >> Terminal || |
180 |
at_mode -- antiq >> Antiquote) x |
|
42504 | 181 |
and body4e x = (Scan.option body4 >> (cat o the_list)) x; |
182 |
||
42516 | 183 |
val rule_name = ident >> Antiquote.Text || antiq >> Antiquote.Antiq; |
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
184 |
val rule = rule_name -- ($$$ ":" |-- !!! body) || body >> pair (Antiquote.Text ""); |
42504 | 185 |
val rules = enum1 ";" (Scan.option rule) >> map_filter I; |
186 |
||
187 |
in |
|
188 |
||
42507 | 189 |
val read = |
190 |
#1 o Scan.error (Scan.finite stopper (rules --| !!! (Scan.ahead (Scan.one is_eof)))) o tokenize; |
|
42504 | 191 |
|
192 |
end; |
|
193 |
||
194 |
||
195 |
(* latex output *) |
|
196 |
||
197 |
local |
|
198 |
||
199 |
fun vertical_range_cat (Cat (_, rails)) y = |
|
200 |
let val (rails', (_, y')) = |
|
201 |
fold_map (fn rail => fn (y0, y') => |
|
202 |
if is_newline rail then (Newline (y' + 1), (y' + 1, y' + 2)) |
|
203 |
else |
|
204 |
let val (rail', y0') = vertical_range rail y0; |
|
205 |
in (rail', (y0, Int.max (y0', y'))) end) rails (y, y + 1) |
|
206 |
in (Cat (y, rails'), y') end |
|
207 |
||
208 |
and vertical_range (Bar cats) y = |
|
209 |
let val (cats', y') = fold_map vertical_range_cat cats y |
|
210 |
in (Bar cats', Int.max (y + 1, y')) end |
|
211 |
| vertical_range (Plus (cat1, cat2)) y = |
|
212 |
let val ([cat1', cat2'], y') = fold_map vertical_range_cat [cat1, cat2] y; |
|
213 |
in (Plus (cat1', cat2'), Int.max (y + 1, y')) end |
|
214 |
| vertical_range (Newline _) y = (Newline (y + 2), y + 3) |
|
215 |
| vertical_range atom y = (atom, y + 1); |
|
216 |
||
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
217 |
fun output_rules state rules = |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
218 |
let |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
219 |
val output_antiq = Thy_Output.eval_antiq (#1 (Keyword.get_lexicons ())) state; |
42516 | 220 |
fun output_text b s = |
221 |
Output.output s |
|
222 |
|> b ? enclose "\\isakeyword{" "}" |
|
223 |
|> enclose "\\isa{" "}"; |
|
42504 | 224 |
|
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
225 |
fun output_cat c (Cat (_, rails)) = outputs c rails |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
226 |
and outputs c [rail] = output c rail |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
227 |
| outputs _ rails = implode (map (output "") rails) |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
228 |
and output _ (Bar []) = "" |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
229 |
| output c (Bar [cat]) = output_cat c cat |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
230 |
| output _ (Bar (cat :: cats)) = |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
231 |
"\\rail@bar\n" ^ output_cat "" cat ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
232 |
implode (map (fn Cat (y, rails) => |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
233 |
"\\rail@nextbar{" ^ string_of_int y ^ "}\n" ^ outputs "" rails) cats) ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
234 |
"\\rail@endbar\n" |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
235 |
| output c (Plus (cat, Cat (y, rails))) = |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
236 |
"\\rail@plus\n" ^ output_cat c cat ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
237 |
"\\rail@nextplus{" ^ string_of_int y ^ "}\n" ^ outputs "c" rails ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
238 |
"\\rail@endplus\n" |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
239 |
| output _ (Newline y) = "\\rail@cr{" ^ string_of_int y ^ "}\n" |
42516 | 240 |
| output c (Nonterminal s) = "\\rail@" ^ c ^ "nont{" ^ output_text false s ^ "}[]\n" |
241 |
| output c (Terminal (b, s)) = "\\rail@" ^ c ^ "term{" ^ output_text b s ^ "}[]\n" |
|
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
242 |
| output c (Antiquote (b, a)) = |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
243 |
"\\rail@" ^ c ^ (if b then "term{" else "nont{") ^ output_antiq a ^ "}[]\n"; |
42504 | 244 |
|
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
245 |
fun output_rule (name, rail) = |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
246 |
let |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
247 |
val (rail', y') = vertical_range rail 0; |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
248 |
val out_name = |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
249 |
(case name of |
42661
824d3f1d8de6
proper treatment of empty name -- avoid excessive vertical space;
wenzelm
parents:
42657
diff
changeset
|
250 |
Antiquote.Text "" => "" |
824d3f1d8de6
proper treatment of empty name -- avoid excessive vertical space;
wenzelm
parents:
42657
diff
changeset
|
251 |
| Antiquote.Text s => output_text false s |
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
252 |
| Antiquote.Antiq a => output_antiq a); |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
253 |
in |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
254 |
"\\rail@begin{" ^ string_of_int y' ^ "}{" ^ out_name ^ "}\n" ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
255 |
output "" rail' ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
256 |
"\\rail@end\n" |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
257 |
end; |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
258 |
in |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
259 |
"\\begin{railoutput}\n" ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
260 |
implode (map output_rule rules) ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
261 |
"\\end{railoutput}\n" |
42504 | 262 |
end; |
263 |
||
264 |
in |
|
265 |
||
53171 | 266 |
val _ = Theory.setup |
267 |
(Thy_Output.antiquotation (Binding.name "rail") |
|
268 |
(Scan.lift (Parse.source_position Parse.string)) |
|
269 |
(fn {state, ...} => output_rules state o read)); |
|
42504 | 270 |
|
271 |
end; |
|
272 |
||
273 |
end; |
|
274 |