src/Pure/Thy/rail.ML
author wenzelm
Sat, 30 Apr 2011 20:48:29 +0200
changeset 42506 876887b07e8d
parent 42504 869c3f6f2d6e
child 42507 651aef3cc854
permissions -rw-r--r--
more robust error handling (NB: Source.source requires total scanner or recover); tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42504
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Thy/rail.ML
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
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
     8
structure Rail: sig end =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
     9
struct
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    10
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    11
(** lexical syntax **)
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    12
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    13
(* datatype token *)
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    14
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    15
datatype kind = Keyword | Ident | String | EOF;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    16
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    17
datatype token = Token of Position.range * (kind * string);
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    18
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    19
fun pos_of (Token ((pos, _), _)) = pos;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    20
fun end_pos_of (Token ((_, pos), _)) = pos;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    21
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    22
fun kind_of (Token (_, (k, _))) = k;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    23
fun content_of (Token (_, (_, x))) = x;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    24
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
(* diagnostics *)
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    27
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    28
val print_kind =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    29
 fn Keyword => "rail keyword"
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    30
  | Ident => "identifier"
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    31
  | String => "single-quoted string"
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    32
  | EOF => "end-of-file";
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    33
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    34
fun print (Token ((pos, _), (k, x))) =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    35
  (if k = EOF then print_kind k else print_kind k ^ " " ^ quote x) ^
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    36
  Position.str_of pos;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    37
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    38
fun print_keyword x = print_kind Keyword ^ " " ^ quote x;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    39
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    40
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    41
(* stopper *)
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    42
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    43
fun mk_eof pos = Token ((pos, Position.none), (EOF, ""));
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    44
val eof = mk_eof Position.none;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    45
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    46
fun is_eof (Token (_, (EOF, _))) = true
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    47
  | is_eof _ = false;
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
val stopper =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    50
  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
    51
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    52
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    53
(* tokenize *)
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    54
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    55
local
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    56
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    57
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
    58
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    59
val scan_space = Scan.many1 (Symbol.is_blank o Symbol_Pos.symbol);
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    60
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    61
val scan_keyword =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    62
  Scan.one (member (op =) ["|", "*", "+", "?", "(", ")", "\\", ";", ":"] o Symbol_Pos.symbol);
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    63
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    64
val scan_token =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    65
  scan_space >> K [] ||
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    66
  scan_keyword >> (token Keyword o single) ||
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    67
  Lexicon.scan_id >> token Ident ||
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    68
  Symbol_Pos.scan_string_q >> (token String o #1 o #2);
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    69
42506
876887b07e8d more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents: 42504
diff changeset
    70
val scan =
876887b07e8d more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents: 42504
diff changeset
    71
  (Scan.repeat scan_token >> flat) --|
876887b07e8d more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents: 42504
diff changeset
    72
    Symbol_Pos.!!! "Rail lexical error: bad input"
876887b07e8d more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents: 42504
diff changeset
    73
      (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
    74
42504
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    75
in
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    76
42506
876887b07e8d more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents: 42504
diff changeset
    77
fun tokenize text =
876887b07e8d more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents: 42504
diff changeset
    78
  #1 (Scan.error (Scan.finite Symbol_Pos.stopper scan) (Symbol_Pos.explode text));
42504
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    79
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    80
end;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    81
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    82
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    83
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    84
(** parsing **)
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    85
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    86
fun !!! scan =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    87
  let
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    88
    val prefix = "Rail syntax error";
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 get_pos [] = " (past end-of-file!)"
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    91
      | get_pos (tok :: _) = Position.str_of (pos_of tok);
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
    fun err (toks, NONE) = prefix ^ get_pos toks
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    94
      | err (toks, SOME msg) =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    95
          if String.isPrefix prefix msg then msg
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    96
          else prefix ^ get_pos toks ^ ": " ^ msg;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
    97
  in Scan.!! err scan end;
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
fun $$$ x =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   100
  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
   101
  Scan.fail_with
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   102
    (fn [] => print_keyword x ^ " expected (past end-of-file!)"
42506
876887b07e8d more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents: 42504
diff changeset
   103
      | tok :: _ => print_keyword x ^ " expected,\nbut " ^ print tok ^ " was found");
42504
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   104
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   105
fun enum1 sep scan = scan ::: Scan.repeat ($$$ sep |-- !!! scan);
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   106
fun enum sep scan = enum1 sep scan || Scan.succeed [];
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
fun parse_token kind =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   109
  Scan.some (fn tok => if kind_of tok = kind then SOME (content_of tok) else NONE);
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   110
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   111
val ident = parse_token Ident;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   112
val string = parse_token String;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   113
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   114
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   115
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   116
(** rail expressions **)
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   117
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   118
(* datatype *)
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   119
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   120
datatype rails =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   121
  Cat of int * rail list
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   122
and rail =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   123
  Bar of rails list |
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   124
  Plus of rails * rails |
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   125
  Newline of int |
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   126
  Nonterminal of string |
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   127
  Terminal of string;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   128
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   129
fun reverse_cat (Cat (y, rails)) = Cat (y, rev (map reverse rails))
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   130
and reverse (Bar cats) = Bar (map reverse_cat cats)
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   131
  | reverse (Plus (cat1, cat2)) = Plus (reverse_cat cat1, reverse_cat cat2)
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   132
  | reverse x = x;
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
fun cat rails = Cat (0, rails);
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   135
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   136
val empty = cat [];
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   137
fun is_empty (Cat (_, [])) = true | is_empty _ = false;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   138
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   139
fun is_newline (Newline _) = true | is_newline _ = false;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   140
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   141
fun bar [Cat (_, [rail])] = rail
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   142
  | bar cats = Bar cats;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   143
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   144
fun plus cat1 cat2 = Plus (cat1, reverse_cat cat2);
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   145
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   146
fun star cat1 cat2 =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   147
  if is_empty cat2 then plus empty cat1
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   148
  else bar [empty, cat [plus cat1 cat2]];
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   149
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   150
fun maybe rail = bar [empty, cat [rail]];
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   151
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   152
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   153
(* read *)
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   154
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   155
local
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   156
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   157
fun body x = (enum1 "|" body1 >> bar) x
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   158
and body0 x = (enum "|" body1 >> bar) x
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   159
and body1 x =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   160
 (body2 :|-- (fn a =>
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   161
   $$$ "*" |-- !!! body4e >> (cat o single o star a) ||
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   162
   $$$ "+" |-- !!! body4e >> (cat o single o plus a) ||
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   163
   Scan.succeed a)) x
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   164
and body2 x = (Scan.repeat1 body3 >> cat) x
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   165
and body3 x = (body4 :|-- (fn a => $$$ "?" >> K (maybe a) || Scan.succeed a)) x
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   166
and body4 x =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   167
 ($$$ "(" |-- !!! (body0 --| $$$ ")") ||
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   168
  $$$ "\\" >> K (Newline 0) ||
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   169
  ident >> Nonterminal ||
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   170
  string >> Terminal) x
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   171
and body4e x = (Scan.option body4 >> (cat o the_list)) x;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   172
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   173
val rule = ident -- ($$$ ":" |-- !!! body) || body >> pair "";
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   174
val rules = enum1 ";" (Scan.option rule) >> map_filter I;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   175
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   176
in
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   177
42506
876887b07e8d more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents: 42504
diff changeset
   178
fun read text =
876887b07e8d more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents: 42504
diff changeset
   179
  (case Scan.error (Scan.finite stopper rules) (tokenize text) of
42504
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   180
    (res, []) => res
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   181
  | (_, tok :: _) => error ("Malformed rail input: " ^ print tok));
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   182
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   183
end;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   184
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   185
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   186
(* latex output *)
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   187
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   188
local
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   189
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   190
fun vertical_range_cat (Cat (_, rails)) y =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   191
  let val (rails', (_, y')) =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   192
    fold_map (fn rail => fn (y0, y') =>
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   193
      if is_newline rail then (Newline (y' + 1), (y' + 1, y' + 2))
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   194
      else
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   195
        let val (rail', y0') = vertical_range rail y0;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   196
        in (rail', (y0, Int.max (y0', y'))) end) rails (y, y + 1)
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   197
  in (Cat (y, rails'), y') end
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   198
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   199
and vertical_range (Bar cats) y =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   200
      let val (cats', y') = fold_map vertical_range_cat cats y
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   201
      in (Bar cats', Int.max (y + 1, y')) end
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   202
  | vertical_range (Plus (cat1, cat2)) y =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   203
      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
   204
      in (Plus (cat1', cat2'), Int.max (y + 1, y')) end
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   205
  | vertical_range (Newline _) y = (Newline (y + 2), y + 3)
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   206
  | vertical_range atom y = (atom, y + 1);
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   207
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   208
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   209
fun output_text s = "\\isa{" ^ Output.output s ^ "}";
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   210
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   211
fun output_cat c (Cat (_, rails)) = outputs c rails
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   212
and outputs c [rail] = output c rail
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   213
  | outputs _ rails = implode (map (output "") rails)
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   214
and output _ (Bar []) = ""
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   215
  | output c (Bar [cat]) = output_cat c cat
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   216
  | output _ (Bar (cat :: cats)) =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   217
      "\\rail@bar\n" ^ output_cat "" cat ^
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   218
      implode (map (fn Cat (y, rails) =>
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   219
          "\\rail@nextbar{" ^ string_of_int y ^ "}\n" ^ outputs "" rails) cats) ^
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   220
      "\\rail@endbar\n"
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   221
  | output c (Plus (cat, Cat (y, rails))) =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   222
      "\\rail@plus\n" ^ output_cat c cat ^
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   223
      "\\rail@nextplus{" ^ string_of_int y ^ "}\n" ^ outputs "c" rails ^
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   224
      "\\rail@endplus\n"
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   225
  | output _ (Newline y) = "\\rail@cr{" ^ string_of_int y ^ "}\n"
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   226
  | output c (Nonterminal s) = "\\rail@" ^ c ^ "nont{" ^ output_text s ^ "}[]\n"
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   227
  | output c (Terminal s) = "\\rail@" ^ c ^ "term{" ^ output_text s ^ "}[]\n";
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   228
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   229
fun output_rule (name, rail) =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   230
  let val (rail', y') = vertical_range rail 0 in
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   231
    "\\rail@begin{" ^ string_of_int y' ^ "}{" ^ output_text name ^ "}\n" ^
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   232
    output "" rail' ^
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   233
    "\\rail@end\n"
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   234
  end;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   235
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   236
fun output_rules rules =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   237
  "\\begin{railoutput}\n" ^
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   238
  implode (map output_rule rules) ^
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   239
  "\\end{railoutput}\n";
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
in
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   242
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   243
val _ =
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   244
  Thy_Output.antiquotation "rail" (Scan.lift (Parse.position Args.name))
42506
876887b07e8d more robust error handling (NB: Source.source requires total scanner or recover);
wenzelm
parents: 42504
diff changeset
   245
    (fn _ => output_rules o read);
42504
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   246
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   247
end;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   248
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   249
end;
869c3f6f2d6e railroad diagrams in LaTeX as document antiquotation;
wenzelm
parents:
diff changeset
   250