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