author | wenzelm |
Wed, 26 Nov 2014 20:05:34 +0100 | |
changeset 59058 | a78612c67ec0 |
parent 58978 | e42da880c61e |
child 59064 | a8bcb5a446c8 |
permissions | -rw-r--r-- |
55030 | 1 |
(* Title: Pure/Tools/rail.ML |
42504 | 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 |
||
56165 | 13 |
(* singleton keywords *) |
14 |
||
15 |
val keywords = |
|
16 |
Symtab.make [ |
|
17 |
("|", Markup.keyword3), |
|
18 |
("*", Markup.keyword3), |
|
19 |
("+", Markup.keyword3), |
|
20 |
("?", Markup.keyword3), |
|
21 |
("(", Markup.empty), |
|
22 |
(")", Markup.empty), |
|
23 |
("\<newline>", Markup.keyword2), |
|
24 |
(";", Markup.keyword2), |
|
25 |
(":", Markup.keyword2), |
|
26 |
("@", Markup.keyword1)]; |
|
27 |
||
28 |
||
42504 | 29 |
(* datatype token *) |
30 |
||
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
31 |
datatype kind = |
55526 | 32 |
Keyword | Ident | String | Antiq of Antiquote.antiq | EOF; |
42504 | 33 |
|
34 |
datatype token = Token of Position.range * (kind * string); |
|
35 |
||
36 |
fun pos_of (Token ((pos, _), _)) = pos; |
|
37 |
fun end_pos_of (Token ((_, pos), _)) = pos; |
|
38 |
||
58465 | 39 |
fun range_of (toks as tok :: _) = |
40 |
let val pos' = end_pos_of (List.last toks) |
|
41 |
in Position.range (pos_of tok) pos' end |
|
42 |
| range_of [] = Position.no_range; |
|
43 |
||
42504 | 44 |
fun kind_of (Token (_, (k, _))) = k; |
45 |
fun content_of (Token (_, (_, x))) = x; |
|
46 |
||
47 |
||
48 |
(* diagnostics *) |
|
49 |
||
50 |
val print_kind = |
|
51 |
fn Keyword => "rail keyword" |
|
52 |
| Ident => "identifier" |
|
53 |
| String => "single-quoted string" |
|
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
54 |
| Antiq _ => "antiquotation" |
48911
5debc3e4fa81
tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents:
48764
diff
changeset
|
55 |
| EOF => "end-of-input"; |
42504 | 56 |
|
57 |
fun print (Token ((pos, _), (k, x))) = |
|
58 |
(if k = EOF then print_kind k else print_kind k ^ " " ^ quote x) ^ |
|
48992 | 59 |
Position.here pos; |
42504 | 60 |
|
61 |
fun print_keyword x = print_kind Keyword ^ " " ^ quote x; |
|
62 |
||
55613 | 63 |
fun reports_of_token (Token ((pos, _), (String, _))) = [(pos, Markup.inner_string)] |
56163 | 64 |
| reports_of_token (Token ((pos, _), (Keyword, x))) = |
56165 | 65 |
map (pair pos) (the_list (Symtab.lookup keywords x) @ Completion.suppress_abbrevs x) |
55613 | 66 |
| reports_of_token (Token (_, (Antiq antiq, _))) = Antiquote.antiq_reports antiq |
67 |
| reports_of_token _ = []; |
|
68 |
||
42504 | 69 |
|
70 |
(* stopper *) |
|
71 |
||
72 |
fun mk_eof pos = Token ((pos, Position.none), (EOF, "")); |
|
73 |
val eof = mk_eof Position.none; |
|
74 |
||
75 |
fun is_eof (Token (_, (EOF, _))) = true |
|
76 |
| is_eof _ = false; |
|
77 |
||
78 |
val stopper = |
|
79 |
Scan.stopper (fn [] => eof | toks => mk_eof (end_pos_of (List.last toks))) is_eof; |
|
80 |
||
81 |
||
82 |
(* tokenize *) |
|
83 |
||
84 |
local |
|
85 |
||
86 |
fun token k ss = [Token (Symbol_Pos.range ss, (k, Symbol_Pos.content ss))]; |
|
87 |
||
58465 | 88 |
fun antiq_token (antiq as (ss, {range, ...})) = |
89 |
[Token (range, (Antiq antiq, Symbol_Pos.content ss))]; |
|
90 |
||
42504 | 91 |
val scan_space = Scan.many1 (Symbol.is_blank o Symbol_Pos.symbol); |
92 |
||
93 |
val scan_keyword = |
|
56165 | 94 |
Scan.one (Symtab.defined keywords o Symbol_Pos.symbol); |
42504 | 95 |
|
48764 | 96 |
val err_prefix = "Rail lexical error: "; |
97 |
||
42504 | 98 |
val scan_token = |
99 |
scan_space >> K [] || |
|
58465 | 100 |
Antiquote.scan_antiq >> antiq_token || |
42504 | 101 |
scan_keyword >> (token Keyword o single) || |
102 |
Lexicon.scan_id >> token Ident || |
|
55613 | 103 |
Symbol_Pos.scan_string_q err_prefix >> (fn (pos1, (ss, pos2)) => |
104 |
[Token (Position.range pos1 pos2, (String, Symbol_Pos.content ss))]); |
|
42504 | 105 |
|
42506
876887b07e8d
more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents:
42504
diff
changeset
|
106 |
val scan = |
876887b07e8d
more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents:
42504
diff
changeset
|
107 |
(Scan.repeat scan_token >> flat) --| |
48764 | 108 |
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
|
109 |
(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
|
110 |
|
42504 | 111 |
in |
112 |
||
55613 | 113 |
val tokenize = #1 o Scan.error (Scan.finite Symbol_Pos.stopper scan); |
42504 | 114 |
|
115 |
end; |
|
116 |
||
117 |
||
118 |
||
119 |
(** parsing **) |
|
120 |
||
58465 | 121 |
(* parser combinators *) |
122 |
||
42504 | 123 |
fun !!! scan = |
124 |
let |
|
125 |
val prefix = "Rail syntax error"; |
|
126 |
||
48911
5debc3e4fa81
tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents:
48764
diff
changeset
|
127 |
fun get_pos [] = " (end-of-input)" |
48992 | 128 |
| get_pos (tok :: _) = Position.here (pos_of tok); |
42504 | 129 |
|
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
|
130 |
fun err (toks, NONE) = (fn () => prefix ^ get_pos toks) |
42504 | 131 |
| 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
|
132 |
(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
|
133 |
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
|
134 |
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
|
135 |
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
|
136 |
end); |
42504 | 137 |
in Scan.!! err scan end; |
138 |
||
139 |
fun $$$ x = |
|
140 |
Scan.one (fn tok => kind_of tok = Keyword andalso content_of tok = x) || |
|
141 |
Scan.fail_with |
|
48911
5debc3e4fa81
tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents:
48764
diff
changeset
|
142 |
(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
|
143 |
| tok :: _ => (fn () => print_keyword x ^ " expected,\nbut " ^ print tok ^ " was found")); |
42504 | 144 |
|
145 |
fun enum1 sep scan = scan ::: Scan.repeat ($$$ sep |-- !!! scan); |
|
146 |
fun enum sep scan = enum1 sep scan || Scan.succeed []; |
|
147 |
||
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
148 |
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
|
149 |
val string = Scan.some (fn tok => if kind_of tok = String then SOME (content_of tok) else NONE); |
42504 | 150 |
|
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
151 |
val antiq = Scan.some (fn tok => (case kind_of tok of Antiq a => SOME a | _ => NONE)); |
42504 | 152 |
|
58465 | 153 |
fun RANGE scan = Scan.trace scan >> apsnd range_of; |
154 |
fun RANGE_APP scan = RANGE scan >> (fn (f, r) => f r); |
|
155 |
||
156 |
||
157 |
(* parse trees *) |
|
158 |
||
159 |
datatype trees = |
|
160 |
CAT of tree list * Position.range |
|
161 |
and tree = |
|
162 |
BAR of trees list * Position.range | |
|
163 |
STAR of (trees * trees) * Position.range | |
|
164 |
PLUS of (trees * trees) * Position.range | |
|
165 |
MAYBE of tree * Position.range | |
|
166 |
NEWLINE of Position.range | |
|
167 |
NONTERMINAL of string * Position.range | |
|
168 |
TERMINAL of (bool * string) * Position.range | |
|
169 |
ANTIQUOTE of (bool * Antiquote.antiq) * Position.range; |
|
170 |
||
171 |
fun reports_of_tree ctxt = |
|
172 |
if Context_Position.is_visible ctxt then |
|
173 |
let |
|
174 |
fun reports r = |
|
175 |
if r = Position.no_range then [] |
|
176 |
else [(Position.set_range r, Markup.expression)]; |
|
177 |
fun trees (CAT (ts, r)) = reports r @ maps tree ts |
|
178 |
and tree (BAR (Ts, r)) = reports r @ maps trees Ts |
|
179 |
| tree (STAR ((T1, T2), r)) = reports r @ trees T1 @ trees T2 |
|
180 |
| tree (PLUS ((T1, T2), r)) = reports r @ trees T1 @ trees T2 |
|
181 |
| tree (MAYBE (t, r)) = reports r @ tree t |
|
182 |
| tree (NEWLINE r) = reports r |
|
183 |
| tree (NONTERMINAL (_, r)) = reports r |
|
184 |
| tree (TERMINAL (_, r)) = reports r |
|
185 |
| tree (ANTIQUOTE (_, r)) = reports r; |
|
186 |
in distinct (op =) o tree end |
|
187 |
else K []; |
|
188 |
||
189 |
local |
|
190 |
||
191 |
val at_mode = Scan.option ($$$ "@") >> (fn NONE => false | _ => true); |
|
192 |
||
193 |
fun body x = (RANGE (enum1 "|" body1) >> BAR) x |
|
194 |
and body0 x = (RANGE (enum "|" body1) >> BAR) x |
|
195 |
and body1 x = |
|
196 |
(RANGE_APP (body2 :|-- (fn a => |
|
197 |
$$$ "*" |-- !!! body4e >> (fn b => fn r => CAT ([STAR ((a, b), r)], r)) || |
|
198 |
$$$ "+" |-- !!! body4e >> (fn b => fn r => CAT ([PLUS ((a, b), r)], r)) || |
|
199 |
Scan.succeed (K a)))) x |
|
200 |
and body2 x = (RANGE (Scan.repeat1 body3) >> CAT) x |
|
201 |
and body3 x = |
|
202 |
(RANGE_APP (body4 :|-- (fn a => |
|
203 |
$$$ "?" >> K (curry MAYBE a) || |
|
204 |
Scan.succeed (K a)))) x |
|
205 |
and body4 x = |
|
206 |
($$$ "(" |-- !!! (body0 --| $$$ ")") || |
|
207 |
RANGE_APP |
|
208 |
($$$ "\<newline>" >> K NEWLINE || |
|
209 |
ident >> curry NONTERMINAL || |
|
210 |
at_mode -- string >> curry TERMINAL || |
|
211 |
at_mode -- antiq >> curry ANTIQUOTE)) x |
|
212 |
and body4e x = |
|
213 |
(RANGE (Scan.option body4) >> (fn (a, r) => CAT (the_list a, r))) x; |
|
214 |
||
215 |
val rule_name = ident >> Antiquote.Text || antiq >> Antiquote.Antiq; |
|
216 |
val rule = rule_name -- ($$$ ":" |-- !!! body) || body >> pair (Antiquote.Text ""); |
|
217 |
val rules = enum1 ";" (Scan.option rule) >> map_filter I; |
|
218 |
||
219 |
in |
|
220 |
||
221 |
fun parse_rules toks = |
|
222 |
#1 (Scan.error (Scan.finite stopper (rules --| !!! (Scan.ahead (Scan.one is_eof)))) toks); |
|
223 |
||
224 |
end; |
|
42504 | 225 |
|
226 |
||
227 |
(** rail expressions **) |
|
228 |
||
229 |
(* datatype *) |
|
230 |
||
231 |
datatype rails = |
|
232 |
Cat of int * rail list |
|
233 |
and rail = |
|
234 |
Bar of rails list | |
|
235 |
Plus of rails * rails | |
|
236 |
Newline of int | |
|
237 |
Nonterminal of string | |
|
42516 | 238 |
Terminal of bool * string | |
55526 | 239 |
Antiquote of bool * Antiquote.antiq; |
42504 | 240 |
|
58465 | 241 |
fun is_newline (Newline _) = true | is_newline _ = false; |
242 |
||
243 |
||
244 |
(* prepare *) |
|
245 |
||
246 |
local |
|
42504 | 247 |
|
248 |
fun cat rails = Cat (0, rails); |
|
249 |
||
250 |
val empty = cat []; |
|
251 |
fun is_empty (Cat (_, [])) = true | is_empty _ = false; |
|
252 |
||
253 |
fun bar [Cat (_, [rail])] = rail |
|
254 |
| bar cats = Bar cats; |
|
255 |
||
58465 | 256 |
fun reverse_cat (Cat (y, rails)) = Cat (y, rev (map reverse rails)) |
257 |
and reverse (Bar cats) = Bar (map reverse_cat cats) |
|
258 |
| reverse (Plus (cat1, cat2)) = Plus (reverse_cat cat1, reverse_cat cat2) |
|
259 |
| reverse x = x; |
|
260 |
||
261 |
fun plus (cat1, cat2) = Plus (cat1, reverse_cat cat2); |
|
262 |
||
263 |
in |
|
42504 | 264 |
|
58465 | 265 |
fun prepare_trees (CAT (ts, _)) = Cat (0, map prepare_tree ts) |
266 |
and prepare_tree (BAR (Ts, _)) = bar (map prepare_trees Ts) |
|
267 |
| prepare_tree (STAR (Ts, _)) = |
|
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58978
diff
changeset
|
268 |
let val (cat1, cat2) = apply2 prepare_trees Ts in |
58465 | 269 |
if is_empty cat2 then plus (empty, cat1) |
270 |
else bar [empty, cat [plus (cat1, cat2)]] |
|
271 |
end |
|
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58978
diff
changeset
|
272 |
| prepare_tree (PLUS (Ts, _)) = plus (apply2 prepare_trees Ts) |
58465 | 273 |
| prepare_tree (MAYBE (t, _)) = bar [empty, cat [prepare_tree t]] |
274 |
| prepare_tree (NEWLINE _) = Newline 0 |
|
275 |
| prepare_tree (NONTERMINAL (a, _)) = Nonterminal a |
|
276 |
| prepare_tree (TERMINAL (a, _)) = Terminal a |
|
277 |
| prepare_tree (ANTIQUOTE (a, _)) = Antiquote a; |
|
42504 | 278 |
|
58465 | 279 |
end; |
42504 | 280 |
|
281 |
||
282 |
(* read *) |
|
283 |
||
55828
42ac3cfb89f6
clarified language markup: added "delimited" property;
wenzelm
parents:
55613
diff
changeset
|
284 |
fun read ctxt (source: Symbol_Pos.source) = |
55613 | 285 |
let |
58978
e42da880c61e
more position information, e.g. relevant for errors in generated ML source;
wenzelm
parents:
58928
diff
changeset
|
286 |
val {text, range = (pos, _), ...} = source; |
55613 | 287 |
val _ = Context_Position.report ctxt pos Markup.language_rail; |
55828
42ac3cfb89f6
clarified language markup: added "delimited" property;
wenzelm
parents:
55613
diff
changeset
|
288 |
val toks = tokenize (Symbol_Pos.explode (text, pos)); |
55613 | 289 |
val _ = Context_Position.reports ctxt (maps reports_of_token toks); |
58465 | 290 |
val rules = parse_rules toks; |
291 |
val _ = Position.reports (maps (reports_of_tree ctxt o #2) rules); |
|
292 |
in map (apsnd prepare_tree) rules end; |
|
42504 | 293 |
|
294 |
||
295 |
(* latex output *) |
|
296 |
||
297 |
local |
|
298 |
||
299 |
fun vertical_range_cat (Cat (_, rails)) y = |
|
300 |
let val (rails', (_, y')) = |
|
301 |
fold_map (fn rail => fn (y0, y') => |
|
302 |
if is_newline rail then (Newline (y' + 1), (y' + 1, y' + 2)) |
|
303 |
else |
|
304 |
let val (rail', y0') = vertical_range rail y0; |
|
305 |
in (rail', (y0, Int.max (y0', y'))) end) rails (y, y + 1) |
|
306 |
in (Cat (y, rails'), y') end |
|
307 |
||
308 |
and vertical_range (Bar cats) y = |
|
309 |
let val (cats', y') = fold_map vertical_range_cat cats y |
|
310 |
in (Bar cats', Int.max (y + 1, y')) end |
|
311 |
| vertical_range (Plus (cat1, cat2)) y = |
|
312 |
let val ([cat1', cat2'], y') = fold_map vertical_range_cat [cat1, cat2] y; |
|
313 |
in (Plus (cat1', cat2'), Int.max (y + 1, y')) end |
|
314 |
| vertical_range (Newline _) y = (Newline (y + 2), y + 3) |
|
315 |
| vertical_range atom y = (atom, y + 1); |
|
316 |
||
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
317 |
fun output_rules state rules = |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
318 |
let |
58928
23d0ffd48006
plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents:
58903
diff
changeset
|
319 |
val output_antiq = Thy_Output.eval_antiq state; |
42516 | 320 |
fun output_text b s = |
321 |
Output.output s |
|
322 |
|> b ? enclose "\\isakeyword{" "}" |
|
323 |
|> enclose "\\isa{" "}"; |
|
42504 | 324 |
|
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
325 |
fun output_cat c (Cat (_, rails)) = outputs c rails |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
326 |
and outputs c [rail] = output c rail |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
327 |
| outputs _ rails = implode (map (output "") rails) |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
328 |
and output _ (Bar []) = "" |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
329 |
| output c (Bar [cat]) = output_cat c cat |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
330 |
| output _ (Bar (cat :: cats)) = |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
331 |
"\\rail@bar\n" ^ output_cat "" cat ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
332 |
implode (map (fn Cat (y, rails) => |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
333 |
"\\rail@nextbar{" ^ string_of_int y ^ "}\n" ^ outputs "" rails) cats) ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
334 |
"\\rail@endbar\n" |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
335 |
| output c (Plus (cat, Cat (y, rails))) = |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
336 |
"\\rail@plus\n" ^ output_cat c cat ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
337 |
"\\rail@nextplus{" ^ string_of_int y ^ "}\n" ^ outputs "c" rails ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
338 |
"\\rail@endplus\n" |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
339 |
| output _ (Newline y) = "\\rail@cr{" ^ string_of_int y ^ "}\n" |
42516 | 340 |
| output c (Nonterminal s) = "\\rail@" ^ c ^ "nont{" ^ output_text false s ^ "}[]\n" |
341 |
| 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
|
342 |
| output c (Antiquote (b, a)) = |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
343 |
"\\rail@" ^ c ^ (if b then "term{" else "nont{") ^ output_antiq a ^ "}[]\n"; |
42504 | 344 |
|
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
345 |
fun output_rule (name, rail) = |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
346 |
let |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
347 |
val (rail', y') = vertical_range rail 0; |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
348 |
val out_name = |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
349 |
(case name of |
42661
824d3f1d8de6
proper treatment of empty name -- avoid excessive vertical space;
wenzelm
parents:
42657
diff
changeset
|
350 |
Antiquote.Text "" => "" |
824d3f1d8de6
proper treatment of empty name -- avoid excessive vertical space;
wenzelm
parents:
42657
diff
changeset
|
351 |
| Antiquote.Text s => output_text false s |
42508
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
352 |
| Antiquote.Antiq a => output_antiq a); |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
353 |
in |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
354 |
"\\rail@begin{" ^ string_of_int y' ^ "}{" ^ out_name ^ "}\n" ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
355 |
output "" rail' ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
356 |
"\\rail@end\n" |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
357 |
end; |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
358 |
in |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
359 |
"\\begin{railoutput}\n" ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
360 |
implode (map output_rule rules) ^ |
e21362bf1d93
allow nested @{antiq} (nonterminal) and @@{antiq} terminal;
wenzelm
parents:
42507
diff
changeset
|
361 |
"\\end{railoutput}\n" |
42504 | 362 |
end; |
363 |
||
364 |
in |
|
365 |
||
53171 | 366 |
val _ = Theory.setup |
55030 | 367 |
(Thy_Output.antiquotation @{binding rail} |
55112
b1a5d603fd12
prefer rail cartouche -- avoid back-slashed quotes;
wenzelm
parents:
55030
diff
changeset
|
368 |
(Scan.lift (Parse.source_position (Parse.string || Parse.cartouche))) |
55613 | 369 |
(fn {state, context, ...} => output_rules state o read context)); |
42504 | 370 |
|
371 |
end; |
|
372 |
||
373 |
end; |
|
374 |