doc-src/rail.ML
author haftmann
Fri Jan 22 13:38:28 2010 +0100 (2010-01-22)
changeset 34944 970e1466028d
parent 32449 696d64ed85da
child 36973 b0033a307d1f
permissions -rw-r--r--
code literals: distinguish numeral classes by different entries
wenzelm@32233
     1
(*  Title:      doc-src/rail.ML
wenzelm@32233
     2
    Author:     Michael Kerscher, TUM
wenzelm@32233
     3
wenzelm@32233
     4
Railroad diagrams for LaTeX.
wenzelm@32233
     5
*)
wenzelm@32233
     6
wenzelm@32233
     7
structure Rail =
wenzelm@32233
     8
struct
wenzelm@32233
     9
wenzelm@32132
    10
datatype token =
wenzelm@32132
    11
  Identifier of string |
wenzelm@32132
    12
  Special_Identifier of string |
wenzelm@32132
    13
  Text of string |
wenzelm@32132
    14
  Anot of string |
wenzelm@32132
    15
  Symbol of string |
wenzelm@32132
    16
  EOF;
wenzelm@32132
    17
wenzelm@32132
    18
fun is_identifier (Identifier _) = true
wenzelm@32132
    19
  | is_identifier (Special_Identifier _ ) = true
wenzelm@32132
    20
  | is_identifier _ = false;
wenzelm@32233
    21
wenzelm@32132
    22
fun is_text (Text _) = true
wenzelm@32132
    23
  | is_text _ = false;
wenzelm@32132
    24
wenzelm@32132
    25
fun is_anot (Anot _) = true
wenzelm@32132
    26
  | is_anot _ = false;
wenzelm@32132
    27
wenzelm@32132
    28
fun is_symbol (Symbol _) = true
wenzelm@32132
    29
  | is_symbol _ = false;
wenzelm@32132
    30
wenzelm@32132
    31
fun is_ str = (fn s => s = Symbol str);
wenzelm@32132
    32
wenzelm@32132
    33
wenzelm@32132
    34
local (* copied from antiquote-setup... *)
wenzelm@32132
    35
fun translate f = Symbol.explode #> map f #> implode;
wenzelm@32132
    36
wenzelm@32132
    37
fun clean_name "\<dots>" = "dots"
wenzelm@32132
    38
  | clean_name ".." = "ddot"
wenzelm@32132
    39
  | clean_name "." = "dot"
wenzelm@32132
    40
  | clean_name "_" = "underscore"
wenzelm@32132
    41
  | clean_name "{" = "braceleft"
wenzelm@32132
    42
  | clean_name "}" = "braceright"
wenzelm@32132
    43
  | clean_name s = s |> translate (fn "_" => "-" | "\<dash>" => "-" | c => c);
wenzelm@32132
    44
wenzelm@32132
    45
fun no_check _ _ = true;
wenzelm@32132
    46
wenzelm@32132
    47
fun false_check _ _ = false;
wenzelm@32132
    48
wenzelm@32132
    49
fun thy_check intern defined ctxt =
wenzelm@32132
    50
  let val thy = ProofContext.theory_of ctxt
wenzelm@32132
    51
  in defined thy o intern thy end;
wenzelm@32132
    52
wenzelm@32132
    53
fun check_tool name =
wenzelm@32132
    54
  File.exists (Path.append (Path.explode "~~/lib/Tools") (Path.basic name));
wenzelm@32132
    55
wenzelm@32132
    56
val arg = enclose "{" "}";
wenzelm@32132
    57
wenzelm@32132
    58
val markup_table =
wenzelm@32132
    59
(*  [(kind, (markup, check, style))*)
wenzelm@32132
    60
  Symtab.make [
wenzelm@32132
    61
  ("syntax", ("", no_check, true)),
wenzelm@32132
    62
  ("command", ("isacommand", K (is_some o OuterKeyword.command_keyword), true)),
wenzelm@32132
    63
  ("keyword", ("isakeyword", K OuterKeyword.is_keyword, true)),
wenzelm@32132
    64
  ("element", ("isakeyword", K OuterKeyword.is_keyword, true)),
wenzelm@32132
    65
  ("method", ("", thy_check Method.intern Method.defined, true)),
wenzelm@32132
    66
  ("attribute", ("", thy_check Attrib.intern Attrib.defined, true)),
wenzelm@32132
    67
  ("fact", ("", no_check, true)),
wenzelm@32132
    68
  ("variable", ("", no_check, true)),
wenzelm@32132
    69
  ("case", ("", no_check, true)),
wenzelm@32132
    70
  ("antiquotation", ("", K ThyOutput.defined_command, true)),
wenzelm@32132
    71
  ("antiquotation option", ("", K ThyOutput.defined_option, true)), (* kann mein scanner nicht erkennen *)
wenzelm@32132
    72
  ("setting", ("isatt", (fn _ => fn name => is_some (OS.Process.getEnv name)), true)),
wenzelm@32132
    73
  ("inference", ("", no_check, true)),
wenzelm@32132
    74
  ("executable", ("isatt", no_check, true)),
wenzelm@32132
    75
  ("tool", ("isatt", K check_tool, true)),
wenzelm@32132
    76
  ("file", ("isatt", K (File.exists o Path.explode), true)),
wenzelm@32233
    77
  ("theory", ("", K ThyInfo.known_thy, true))
wenzelm@32132
    78
  ];
wenzelm@32132
    79
wenzelm@32132
    80
in
wenzelm@32132
    81
wenzelm@32132
    82
fun decode_link ctxt (kind,index,logic,name) =
wenzelm@32132
    83
  let
wenzelm@32132
    84
  val (markup, check, style) = case Symtab.lookup markup_table kind of
wenzelm@32132
    85
    SOME x => x
wenzelm@32132
    86
  | NONE => ("", false_check, false);
wenzelm@32132
    87
  val hyper_name =
wenzelm@32132
    88
    "{" ^ Long_Name.append kind (Long_Name.append logic (clean_name name)) ^ "}";
wenzelm@32132
    89
  val hyper =
wenzelm@32132
    90
    enclose ("\\hyperlink" ^ hyper_name ^ "{") "}" #>
wenzelm@32132
    91
    index = "def" ? enclose ("\\hypertarget" ^ hyper_name ^ "{") "}";
wenzelm@32132
    92
  val idx =
wenzelm@32132
    93
    if index = "" then ""
wenzelm@32132
    94
    else "\\index" ^ index ^ arg logic ^ arg kind ^ arg name;
wenzelm@32132
    95
  in
wenzelm@32132
    96
  if check ctxt name then
wenzelm@32132
    97
    (idx ^
wenzelm@32132
    98
    (Output.output name
wenzelm@32132
    99
      |> (if markup = "" then I else enclose ("\\" ^ markup ^ "{") "}")
wenzelm@32132
   100
      |> (if ! ThyOutput.quotes then quote else I)
wenzelm@32132
   101
      |> (if ! ThyOutput.display then enclose "\\begin{isabelle}%\n" "%\n\\end{isabelle}"
wenzelm@32449
   102
          else hyper o enclose "\\mbox{\\isa{" "}}")), style)
wenzelm@32132
   103
  else ("Bad " ^ kind ^ " " ^ name, false)
wenzelm@32132
   104
  end;
wenzelm@32132
   105
end;
wenzelm@32132
   106
wenzelm@32132
   107
val blanks =
wenzelm@32132
   108
  Scan.many Symbol.is_blank >> implode;
wenzelm@32132
   109
wenzelm@32132
   110
val scan_symbol =
wenzelm@32132
   111
  $$ ";" || $$ ":"|| $$ "("|| $$ ")"|| $$ "+"|| $$ "|"|| $$ "*"|| $$ "?"|| $$ "\\";
wenzelm@32132
   112
wenzelm@32132
   113
(* escaped version *)
wenzelm@32132
   114
val scan_link = (* @{kind{_def|_ref (logic) name} *)
wenzelm@32132
   115
  let
wenzelm@32132
   116
    fun pseudo_antiquote inner_scan = ($$ "@" ^^ $$ "{") |-- inner_scan --| (blanks -- $$ "}");
wenzelm@32132
   117
    fun parens scan = $$ "(" |-- scan --| $$ ")";
wenzelm@32132
   118
    fun opt_quotes scan = $$ "'" |-- scan --| $$ "'" || scan;
wenzelm@32132
   119
    val letters = Scan.many Symbol.is_letter >> implode;
wenzelm@32132
   120
    val kind_name = letters;
wenzelm@32132
   121
    val opt_kind_type = Scan.optional (
wenzelm@32132
   122
      $$ "_" |-- (Scan.this_string "def" || Scan.this_string "ref")) "";
wenzelm@32132
   123
    val logic_name = letters;
wenzelm@32132
   124
    val escaped_singlequote = $$ "\\" |-- $$ "'";
wenzelm@32132
   125
    val text = Scan.repeat (Scan.one Symbol.is_letter || escaped_singlequote) >> implode;
wenzelm@32132
   126
  in
wenzelm@32132
   127
   pseudo_antiquote (
wenzelm@32132
   128
    kind_name -- opt_kind_type --
wenzelm@32132
   129
    (blanks |-- Scan.optional ( parens logic_name ) "") --
wenzelm@32132
   130
    (blanks |-- opt_quotes text) )
wenzelm@32132
   131
    >> (fn (((kind,index),logic),name) => (kind, index, logic, name))
wenzelm@32132
   132
end;
wenzelm@32132
   133
wenzelm@32132
   134
(* escaped version *)
wenzelm@32233
   135
fun scan_identifier ctxt =
wenzelm@32132
   136
  let fun is_identifier_start s =
wenzelm@32132
   137
    Symbol.is_letter s orelse
wenzelm@32132
   138
    s = "_"
wenzelm@32132
   139
  fun is_identifier_rest s =
wenzelm@32132
   140
    Symbol.is_letter s orelse
wenzelm@32132
   141
    Symbol.is_digit s orelse
wenzelm@32132
   142
    s = "_" orelse
wenzelm@32132
   143
    s = "."
wenzelm@32132
   144
  in
wenzelm@32132
   145
  (Scan.one is_identifier_start :::
wenzelm@32132
   146
    Scan.repeat (Scan.one is_identifier_rest || ($$ "\\" |-- $$ "'"))
wenzelm@32132
   147
  ) >> (Identifier o enclose "\\isa{" "}" o Output.output o implode) ||
wenzelm@32132
   148
  scan_link >> (decode_link ctxt) >>
wenzelm@32132
   149
    (fn (txt, style) =>
wenzelm@32449
   150
        if style then Special_Identifier(txt)
wenzelm@32449
   151
        else Identifier(txt))
wenzelm@32132
   152
end;
wenzelm@32132
   153
wenzelm@32132
   154
fun scan_anot ctxt =
wenzelm@32132
   155
  let val scan_anot =
wenzelm@32132
   156
    Scan.many (fn s =>
wenzelm@32132
   157
      s <> "\n" andalso
wenzelm@32132
   158
      s <> "\t" andalso
wenzelm@32132
   159
      s <> "]" andalso
wenzelm@32132
   160
      Symbol.is_regular s) >> implode
wenzelm@32132
   161
  in
wenzelm@32132
   162
  $$ "[" |-- scan_link --| $$ "]" >> (fst o (decode_link ctxt)) ||
wenzelm@32132
   163
  $$ "[" |-- scan_anot --| $$ "]" >> Output.output
wenzelm@32132
   164
end;
wenzelm@32132
   165
wenzelm@32132
   166
(* escaped version *)
wenzelm@32132
   167
fun scan_text ctxt =
wenzelm@32132
   168
  let
wenzelm@32132
   169
    val text_sq =
wenzelm@32132
   170
      Scan.repeat (
wenzelm@32132
   171
        Scan.one (fn s =>
wenzelm@32449
   172
          s <> "\n" andalso
wenzelm@32449
   173
          s <> "\t" andalso
wenzelm@32449
   174
          s <> "'" andalso
wenzelm@32449
   175
          s <> "\\" andalso
wenzelm@32449
   176
          Symbol.is_regular s) ||
wenzelm@32449
   177
        ($$ "\\" |-- $$ "'")
wenzelm@32132
   178
      ) >> implode
wenzelm@32132
   179
  fun quoted scan = $$ "'" |-- scan --| $$ "'";
wenzelm@32132
   180
  in
wenzelm@32132
   181
  quoted scan_link >> (fst o (decode_link ctxt)) ||
wenzelm@32132
   182
  quoted text_sq >> (enclose "\\isa{" "}" o Output.output)
wenzelm@32132
   183
end;
wenzelm@32132
   184
wenzelm@32132
   185
fun scan_rail ctxt =
wenzelm@32132
   186
  Scan.repeat ( blanks |-- (
wenzelm@32132
   187
    scan_identifier ctxt ||
wenzelm@32132
   188
    scan_anot ctxt >> Anot ||
wenzelm@32132
   189
    scan_text ctxt >> Text ||
wenzelm@32132
   190
    scan_symbol >> Symbol)
wenzelm@32132
   191
  ) --| blanks;
wenzelm@32132
   192
wenzelm@32132
   193
fun lex_rail txt ctxt = (* Symbol_Pos fuer spaeter durchgereicht *)
wenzelm@32132
   194
  Symbol.scanner "Malformed rail-declaration" (scan_rail ctxt) (map fst (Symbol_Pos.explode txt));
wenzelm@32132
   195
wenzelm@32132
   196
val lex = lex_rail;
wenzelm@32132
   197
wenzelm@32132
   198
datatype id_kind = UNKNOWN | TOKEN | TERM | NTERM;
wenzelm@32132
   199
wenzelm@32132
   200
datatype id_type =
wenzelm@32132
   201
  Id of string * id_kind |
wenzelm@32132
   202
  Null_Id;
wenzelm@32132
   203
wenzelm@32132
   204
datatype body_kind =
wenzelm@32132
   205
  CAT | BAR | PLUS |
wenzelm@32132
   206
  CR | EMPTY | ANNOTE | IDENT | STRING |
wenzelm@32132
   207
  Null_Kind;
wenzelm@32132
   208
wenzelm@32233
   209
datatype body_type =
wenzelm@32132
   210
  Body of body_kind * string * string * id_type * body_type list |
wenzelm@32132
   211
  Body_Pos of body_kind * string * string * id_type * body_type list * int * int |
wenzelm@32132
   212
  Empty_Body |
wenzelm@32132
   213
  Null_Body;
wenzelm@32132
   214
wenzelm@32233
   215
datatype rule =
wenzelm@32132
   216
  Rule of id_type * body_type;
wenzelm@32132
   217
wenzelm@32132
   218
fun new_id id kind = Id (id, kind);
wenzelm@32132
   219
wenzelm@32132
   220
fun is_empty (Body(kind,_,_,_,_)) = kind = EMPTY;
wenzelm@32132
   221
wenzelm@32132
   222
fun new_body (kind, Null_Body, Null_Body) = Body (kind, "", "", Null_Id, [])
wenzelm@32132
   223
  | new_body (kind, body1, body2) = Body (kind, "", "", Null_Id, body1 :: [body2]);
wenzelm@32132
   224
wenzelm@32132
   225
fun is_kind_of kind (Body(bodyKind,_,_,_,_)) = kind = bodyKind
wenzelm@32132
   226
  | is_kind_of _ _ = false;
wenzelm@32132
   227
wenzelm@32132
   228
fun add_list (Body(kind, text, annot, id, bodies), body) =
wenzelm@32132
   229
  Body(kind, text, annot, id, bodies @ [body]);
wenzelm@32132
   230
wenzelm@32233
   231
fun cat_body_lists (Body(kind, text, annot, id, bodies1), Body(_,_,_,_, bodies2)) =
wenzelm@32132
   232
      Body(kind, text, annot, id, bodies1 @ bodies2);
wenzelm@32132
   233
wenzelm@32132
   234
fun add_body (kind, body1 as Body(kind1,_,_,_,_), body2 as Body(kind2,_,_,_,_)) =
wenzelm@32132
   235
  if kind = kind1 andalso kind <> BAR then
wenzelm@32132
   236
    if kind = kind2 then
wenzelm@32132
   237
      cat_body_lists(body1, body2)
wenzelm@32132
   238
    else (* kind <> kind2 *)
wenzelm@32132
   239
      add_list(body1, body2)
wenzelm@32132
   240
  else (* kind <> kind1 orelse kind = BAR *)
wenzelm@32132
   241
    if kind = kind2 then
wenzelm@32132
   242
      cat_body_lists(add_list(new_body(kind,Null_Body,Null_Body), body1), body2)
wenzelm@32132
   243
    else (* kind <> kind2 *)
wenzelm@32132
   244
      add_list(add_list(new_body(kind,Null_Body,Null_Body), body1), body2);
wenzelm@32132
   245
wenzelm@32132
   246
fun rev_body (body as Body (kind, text, annot, id, [])) = body
wenzelm@32132
   247
  | rev_body (Body (CAT, text, annot, id, bodies)) =
wenzelm@32132
   248
      Body(CAT, text, annot, id, map rev_body (rev bodies))
wenzelm@32132
   249
  | rev_body (Body (kind, text, annot, id, bodies)) =
wenzelm@32132
   250
      Body(kind, text, annot, id, map rev_body bodies)
wenzelm@32132
   251
  | rev_body body = body;
wenzelm@32132
   252
wenzelm@32132
   253
fun set_body_text text (Body(k,_,a,i,b)) = Body(k,text,a,i,b);
wenzelm@32132
   254
fun set_body_anot anot (Body(k,t,_,i,b)) = Body(k,t,anot,i,b);
wenzelm@32132
   255
fun set_body_ident id (Body(k,t,a,_,b)) = Body(k,t,a, new_id id TOKEN,b);
wenzelm@32132
   256
fun set_body_special_ident id (Body(k,t,a,_,b)) = Body(k,t,a, new_id id TERM,b);
wenzelm@32132
   257
wenzelm@32132
   258
wenzelm@32132
   259
fun mk_eof _ = EOF;
wenzelm@32132
   260
fun is_eof s = s = EOF;
wenzelm@32132
   261
val stopper = Scan.stopper mk_eof is_eof;
wenzelm@32132
   262
wenzelm@32132
   263
(* TODO: change this, so the next or next two tokens are printed *)
wenzelm@32132
   264
fun lex_err msg (cs, _) = "rail grammar error: " ^ msg cs;
wenzelm@32132
   265
fun !!! msg scan = Scan.!! (lex_err (K msg)) scan;
wenzelm@32132
   266
fun $$$ tok = Scan.one (is_ tok);
wenzelm@32132
   267
wenzelm@32132
   268
wenzelm@32132
   269
local
wenzelm@32132
   270
fun new_bar_body([], body2) = body2
wenzelm@32132
   271
  | new_bar_body(body1::bodies, body2) =
wenzelm@32132
   272
      add_body(BAR, body1, new_bar_body(bodies, body2));
wenzelm@32132
   273
wenzelm@32132
   274
fun new_cat_body(body::[]) = body
wenzelm@32132
   275
  | new_cat_body(body1::bodies) = add_body(CAT, body1, new_cat_body(bodies));
wenzelm@32132
   276
wenzelm@32132
   277
fun new_annote_body (Anot anot) =
wenzelm@32132
   278
  set_body_text anot (new_body(ANNOTE, Empty_Body, Empty_Body));
wenzelm@32132
   279
wenzelm@32132
   280
fun new_text_annote_body (Text text, Anot anot) =
wenzelm@32132
   281
  set_body_anot anot (set_body_text text (new_body(STRING, Empty_Body, Empty_Body)));
wenzelm@32132
   282
wenzelm@32132
   283
fun new_ident_body (Identifier ident, Anot anot) =
wenzelm@32132
   284
      set_body_anot anot (set_body_ident ident (new_body(IDENT, Empty_Body, Empty_Body)))
wenzelm@32132
   285
  | new_ident_body (Special_Identifier ident, Anot anot) =
wenzelm@32132
   286
      set_body_anot anot (set_body_special_ident ident (new_body(IDENT, Empty_Body, Empty_Body)));
wenzelm@32132
   287
wenzelm@32132
   288
val new_empty_body = new_body(EMPTY, Null_Body, Null_Body);
wenzelm@32132
   289
in
wenzelm@32132
   290
wenzelm@32132
   291
fun parse_body x =
wenzelm@32132
   292
  (
wenzelm@32132
   293
  Scan.repeat1 (parse_body0 --| $$$ "|") -- !!! "body0 expected" (parse_body0) >>
wenzelm@32132
   294
    new_bar_body ||
wenzelm@32132
   295
  parse_body0
wenzelm@32132
   296
  ) x
wenzelm@32132
   297
and parse_body0 x =
wenzelm@32132
   298
  (
wenzelm@32132
   299
  Scan.one is_anot -- !!! "body1 expected" (parse_body1) >>
wenzelm@32132
   300
    (fn (anot, body) => add_body(CAT, new_annote_body(anot), body))  ||
wenzelm@32132
   301
  parse_body1
wenzelm@32132
   302
  ) x
wenzelm@32132
   303
and parse_body1 x =
wenzelm@32132
   304
  (
wenzelm@32132
   305
  parse_body2 -- ($$$ "*" |-- !!! "body4e expected" (parse_body4e)) >>
wenzelm@32132
   306
    (fn (body1, body2) =>
wenzelm@32132
   307
      if is_empty body2 then
wenzelm@32449
   308
        add_body(PLUS, new_empty_body, rev_body body1)
wenzelm@32132
   309
      else
wenzelm@32449
   310
        add_body(BAR, new_empty_body, add_body (PLUS, body1, rev_body body2)) ) ||
wenzelm@32233
   311
  parse_body2 -- ($$$ "+" |-- !!! "body4e expected" (parse_body4e)) >>
wenzelm@32132
   312
    (fn (body1, body2) => new_body (PLUS, body1, rev_body body2) ) ||
wenzelm@32132
   313
  parse_body2e
wenzelm@32132
   314
  ) x
wenzelm@32132
   315
and parse_body2e x =
wenzelm@32132
   316
  (
wenzelm@32132
   317
  parse_body2 ||
wenzelm@32132
   318
  (fn toks => (new_empty_body, toks))
wenzelm@32132
   319
  ) x
wenzelm@32132
   320
and parse_body2 x =
wenzelm@32132
   321
  (
wenzelm@32132
   322
  Scan.repeat1 (parse_body3) >> new_cat_body
wenzelm@32132
   323
  ) x
wenzelm@32132
   324
and parse_body3 x =
wenzelm@32132
   325
  (
wenzelm@32132
   326
  parse_body4 --| $$$ "?" >> (fn body => new_body (BAR, new_empty_body, body) ) ||
wenzelm@32132
   327
  parse_body4
wenzelm@32132
   328
  ) x
wenzelm@32132
   329
and parse_body4e x =
wenzelm@32132
   330
  (
wenzelm@32132
   331
  parse_body4 ||
wenzelm@32132
   332
  (fn toks => (new_empty_body, toks))
wenzelm@32132
   333
  ) x
wenzelm@32132
   334
and parse_body4 x =
wenzelm@32132
   335
  (
wenzelm@32132
   336
  $$$ "(" |-- !!! "body0 or ')' expected" (parse_body --| $$$ ")") ||
wenzelm@32132
   337
  Scan.one is_text -- (Scan.optional (Scan.one is_anot) (Anot(""))) >>
wenzelm@32233
   338
    (fn (text, anot) => new_text_annote_body (text,anot)) ||
wenzelm@32132
   339
  Scan.one is_identifier -- (Scan.optional (Scan.one is_anot) (Anot(""))) >>
wenzelm@32132
   340
    (fn (id, anot) => new_ident_body (id,anot)) ||
wenzelm@32132
   341
  $$$ "\\" >> (fn _ => new_body (CR, Null_Body, Null_Body))
wenzelm@32132
   342
  ) x;
wenzelm@32132
   343
end;
wenzelm@32132
   344
wenzelm@32132
   345
fun new_named_rule (Identifier name, body) = Rule(Id(name, UNKNOWN), body)
wenzelm@32132
   346
  | new_named_rule (Special_Identifier name, body) = Rule(Id(name, UNKNOWN), body);
wenzelm@32132
   347
fun new_anonym_rule body = Rule(Null_Id, body);
wenzelm@32132
   348
wenzelm@32132
   349
val parse_rule =
wenzelm@32132
   350
  (Scan.one (is_identifier) -- ($$$ ":" |-- !!! "body expected" (parse_body)) ) >>
wenzelm@32132
   351
    new_named_rule ||
wenzelm@32132
   352
  parse_body >> new_anonym_rule;
wenzelm@32132
   353
wenzelm@32132
   354
val parse_rules =
wenzelm@32132
   355
  Scan.repeat ( parse_rule --| $$$ ";") @@@ Scan.single parse_rule;
wenzelm@32132
   356
wenzelm@32132
   357
fun parse_rail s =
wenzelm@32132
   358
  Scan.read stopper parse_rules s;
wenzelm@32132
   359
wenzelm@32132
   360
val parse = parse_rail;
wenzelm@32132
   361
wenzelm@32132
   362
fun getystart (Body_Pos(_,_,_,_,_,ystart,_)) = ystart;
wenzelm@32132
   363
fun getynext (Body_Pos(_,_,_,_,_,_,ynext)) = ynext;
wenzelm@32132
   364
wenzelm@32132
   365
fun position_body (body as Body(kind, text, annot, id, bodies), ystart) =
wenzelm@32132
   366
  let fun max (x,y) = if x > y then x else y
wenzelm@32132
   367
    fun set_body_position (Body(kind, text, annot, id, bodies), ystart, ynext) =
wenzelm@32449
   368
          Body_Pos(kind, text, annot, id, bodies, ystart, ynext)
wenzelm@32132
   369
    fun pos_bodies_cat ([],_,ynext,liste) = (liste, ynext)
wenzelm@32132
   370
      | pos_bodies_cat (x::xs, ystart, ynext, liste) =
wenzelm@32449
   371
          if is_kind_of CR x then
wenzelm@32449
   372
              (case set_body_position(x, ystart, ynext+1) of
wenzelm@32449
   373
                body as Body_Pos(_,_,_,_,_,_,ynext1) =>
wenzelm@32449
   374
                  pos_bodies_cat(xs, ynext1, max(ynext,ynext1), liste@[body])
wenzelm@32449
   375
              )
wenzelm@32449
   376
          else
wenzelm@32449
   377
              (case position_body(x, ystart) of
wenzelm@32449
   378
                body as Body_Pos(_,_,_,_,_,_,ynext1) =>
wenzelm@32449
   379
                  pos_bodies_cat(xs, ystart, max(ynext,ynext1), liste@[body])
wenzelm@32449
   380
              )
wenzelm@32132
   381
    fun pos_bodies_bar_plus ([],_,ynext,liste) = (liste, ynext)
wenzelm@32132
   382
      | pos_bodies_bar_plus (x::xs, ystart, ynext, liste) =
wenzelm@32449
   383
          (case position_body(x, ystart) of
wenzelm@32449
   384
            body as Body_Pos(_,_,_,_,_,_,ynext1) =>
wenzelm@32449
   385
              pos_bodies_bar_plus(xs, ynext1, max(ynext,ynext1), liste@[body])
wenzelm@32449
   386
          )
wenzelm@32132
   387
  in
wenzelm@32132
   388
  (case kind of
wenzelm@32132
   389
    CAT => (case pos_bodies_cat(bodies,ystart,ystart+1,[]) of
wenzelm@32449
   390
              (bodiesPos, ynext) =>
wenzelm@32449
   391
                Body_Pos(kind, text, annot, id, bodiesPos, ystart, ynext))
wenzelm@32132
   392
  | BAR => (case pos_bodies_bar_plus(bodies,ystart,ystart+1,[]) of
wenzelm@32449
   393
              (bodiesPos, ynext) =>
wenzelm@32449
   394
                Body_Pos(kind, text, annot, id, bodiesPos, ystart, ynext))
wenzelm@32132
   395
  | PLUS => (case pos_bodies_bar_plus(bodies,ystart,ystart+1,[]) of
wenzelm@32449
   396
              (bodiesPos, ynext) =>
wenzelm@32449
   397
                Body_Pos(kind, text, annot, id, bodiesPos, ystart, ynext))
wenzelm@32132
   398
  | CR => set_body_position(body, ystart, ystart+3)
wenzelm@32132
   399
  | EMPTY => set_body_position(body, ystart, ystart+1)
wenzelm@32132
   400
  | ANNOTE => set_body_position(body, ystart, ystart+1)
wenzelm@32132
   401
  | IDENT => set_body_position(body, ystart, ystart+1)
wenzelm@32132
   402
  | STRING => set_body_position(body, ystart, ystart+1)
wenzelm@32132
   403
  )
wenzelm@32132
   404
  end;
wenzelm@32132
   405
wenzelm@32132
   406
fun format_body (Body_Pos(EMPTY,_,_,_,_,_,_), _) = ""
wenzelm@32132
   407
  | format_body (Body_Pos(CAT,_,_,_,bodies,_,_), cent) =
wenzelm@32132
   408
    let fun format_bodies([]) = ""
wenzelm@32449
   409
          | format_bodies(x::xs) = format_body (x, "") ^ format_bodies(xs)
wenzelm@32132
   410
    in
wenzelm@32132
   411
      format_bodies(bodies)
wenzelm@32132
   412
    end
wenzelm@32233
   413
  | format_body (Body_Pos(BAR,_,_,_,bodies,_,_),cent) =
wenzelm@32132
   414
    let fun format_bodies([]) = "\\rail@endbar\n"
wenzelm@32449
   415
          | format_bodies(x::xs) =
wenzelm@32449
   416
              "\\rail@nextbar{" ^ string_of_int(getystart(x)) ^"}\n" ^
wenzelm@32449
   417
              format_body(x, "") ^ format_bodies(xs)
wenzelm@32132
   418
    in
wenzelm@32132
   419
      "\\rail@bar\n" ^ format_body(hd(bodies), "") ^ format_bodies(tl(bodies))
wenzelm@32132
   420
    end
wenzelm@32132
   421
  | format_body (Body_Pos(PLUS,_,_,_,x::y::xs,_,_),cent) =
wenzelm@32132
   422
      "\\rail@plus\n" ^ format_body(x, cent) ^
wenzelm@32132
   423
      "\\rail@nextplus{" ^ string_of_int(getystart(y)) ^ "}\n" ^
wenzelm@32132
   424
      format_body(y, "c") ^
wenzelm@32132
   425
      "\\rail@endplus\n"
wenzelm@32132
   426
  | format_body (Body_Pos(ANNOTE,text,_,_,_,_,_),cent) =
wenzelm@32132
   427
      "\\rail@annote[" ^ text ^ "]\n"
wenzelm@32132
   428
  | format_body (Body_Pos(IDENT,_,annot,Id(name,TERM),_,_,_),cent) =
wenzelm@32132
   429
      "\\rail@" ^ cent ^ "token{" ^ name ^ "}" ^ "[" ^ annot ^ "]\n"
wenzelm@32132
   430
  | format_body (Body_Pos(IDENT,_,annot,Id(name,_),_,_,_),cent) =
wenzelm@32132
   431
      "\\rail@" ^ cent ^ "nont{" ^ name ^ "}" ^ "[" ^ annot ^ "]\n"
wenzelm@32132
   432
  | format_body (Body_Pos(CR,_,_,_,_,_,ynext),cent) =
wenzelm@32132
   433
      "\\rail@cr{" ^ string_of_int(ynext) ^ "}\n"
wenzelm@32132
   434
  | format_body (Body_Pos(STRING,text,annot,_,_,_,_),cent) =
wenzelm@32132
   435
      "\\rail@" ^ cent ^ "term{" ^ text ^ "}[" ^ annot ^ "]\n"
wenzelm@32132
   436
  | format_body _ =
wenzelm@32132
   437
      "\\rail@unknown\n";
wenzelm@32132
   438
wenzelm@32132
   439
fun out_body (Id(name,_), body) =
wenzelm@32132
   440
  let val bodyPos as Body_Pos(_,_,_,_,_,_,ynext) = position_body(body,0)
wenzelm@32132
   441
  in
wenzelm@32132
   442
    "\\rail@begin{" ^ string_of_int(ynext) ^ "}{" ^ name ^ "}\n" ^
wenzelm@32132
   443
    format_body(bodyPos,"") ^
wenzelm@32132
   444
    "\\rail@end\n"
wenzelm@32132
   445
  end
wenzelm@32132
   446
  | out_body (Null_Id, body) = out_body (Id("", UNKNOWN), body);
wenzelm@32132
   447
wenzelm@32132
   448
fun out_rule (Rule(id, body)) =
wenzelm@32132
   449
  if is_empty body then ""
wenzelm@32132
   450
  else out_body (id, body);
wenzelm@32132
   451
wenzelm@32132
   452
fun out_rules ([]) = ""
wenzelm@32132
   453
  | out_rules (rule::rules) = out_rule rule ^ out_rules rules;
wenzelm@32132
   454
wenzelm@32132
   455
val output_no_rules =
wenzelm@32132
   456
  "\\rail@begin{1}{}\n" ^
wenzelm@32132
   457
  "\\rail@setbox{\\bfseries ???}\n" ^
wenzelm@32132
   458
  "\\rail@oval\n" ^
wenzelm@32132
   459
  "\\rail@end\n";
wenzelm@32132
   460
wenzelm@32132
   461
wenzelm@32132
   462
fun print (SOME rules) =
wenzelm@32132
   463
    "\\begin{railoutput}\n" ^
wenzelm@32132
   464
    out_rules rules ^
wenzelm@32132
   465
    "\\end{railoutput}\n"
wenzelm@32132
   466
  | print (NONE) =
wenzelm@32132
   467
    "\\begin{railoutput}\n" ^
wenzelm@32132
   468
    output_no_rules ^
wenzelm@32132
   469
    "\\end{railoutput}\n";
wenzelm@32132
   470
wenzelm@32132
   471
fun process txt ctxt =
wenzelm@32132
   472
  lex txt ctxt
wenzelm@32132
   473
  |> parse
wenzelm@32132
   474
  |> print;
wenzelm@32132
   475
wenzelm@32132
   476
val _ = ThyOutput.antiquotation "rail" (Scan.lift ( OuterParse.position Args.name ))
wenzelm@32132
   477
  (fn {context = ctxt,...} => fn txt => process txt ctxt);
wenzelm@32233
   478
wenzelm@32233
   479
end;
wenzelm@32233
   480