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