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