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