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