src/Pure/Tools/rail.ML
author wenzelm
Mon Feb 17 11:14:26 2014 +0100 (2014-02-17)
changeset 55526 39708e59f4b0
parent 55112 b1a5d603fd12
child 55613 ad446b45efff
permissions -rw-r--r--
more markup;
     1 (*  Title:      Pure/Tools/rail.ML
     2     Author:     Michael Kerscher, TU M√ľnchen
     3     Author:     Makarius
     4 
     5 Railroad diagrams in LaTeX.
     6 *)
     7 
     8 structure Rail: sig end =
     9 struct
    10 
    11 (** lexical syntax **)
    12 
    13 (* datatype token *)
    14 
    15 datatype kind =
    16   Keyword | Ident | String | Antiq of Antiquote.antiq | EOF;
    17 
    18 datatype token = Token of Position.range * (kind * string);
    19 
    20 fun pos_of (Token ((pos, _), _)) = pos;
    21 fun end_pos_of (Token ((_, pos), _)) = pos;
    22 
    23 fun kind_of (Token (_, (k, _))) = k;
    24 fun content_of (Token (_, (_, x))) = x;
    25 
    26 
    27 (* diagnostics *)
    28 
    29 val print_kind =
    30  fn Keyword => "rail keyword"
    31   | Ident => "identifier"
    32   | String => "single-quoted string"
    33   | Antiq _ => "antiquotation"
    34   | EOF => "end-of-input";
    35 
    36 fun print (Token ((pos, _), (k, x))) =
    37   (if k = EOF then print_kind k else print_kind k ^ " " ^ quote x) ^
    38   Position.here pos;
    39 
    40 fun print_keyword x = print_kind Keyword ^ " " ^ quote x;
    41 
    42 
    43 (* stopper *)
    44 
    45 fun mk_eof pos = Token ((pos, Position.none), (EOF, ""));
    46 val eof = mk_eof Position.none;
    47 
    48 fun is_eof (Token (_, (EOF, _))) = true
    49   | is_eof _ = false;
    50 
    51 val stopper =
    52   Scan.stopper (fn [] => eof | toks => mk_eof (end_pos_of (List.last toks))) is_eof;
    53 
    54 
    55 (* tokenize *)
    56 
    57 local
    58 
    59 fun token k ss = [Token (Symbol_Pos.range ss, (k, Symbol_Pos.content ss))];
    60 
    61 val scan_space = Scan.many1 (Symbol.is_blank o Symbol_Pos.symbol);
    62 
    63 val scan_keyword =
    64   Scan.one
    65     (member (op =) ["|", "*", "+", "?", "(", ")", "\<newline>", ";", ":", "@"] o Symbol_Pos.symbol);
    66 
    67 val err_prefix = "Rail lexical error: ";
    68 
    69 val scan_token =
    70   scan_space >> K [] ||
    71   Antiquote.scan_antiq >> (fn antiq as (ss, _) => token (Antiq antiq) ss) ||
    72   scan_keyword >> (token Keyword o single) ||
    73   Lexicon.scan_id >> token Ident ||
    74   Symbol_Pos.scan_string_q err_prefix >> (token String o #1 o #2);
    75 
    76 val scan =
    77   (Scan.repeat scan_token >> flat) --|
    78     Symbol_Pos.!!! (fn () => err_prefix ^ "bad input")
    79       (Scan.ahead (Scan.one Symbol_Pos.is_eof));
    80 
    81 in
    82 
    83 val tokenize = #1 o Scan.error (Scan.finite Symbol_Pos.stopper scan) o Symbol_Pos.explode;
    84 
    85 end;
    86 
    87 
    88 
    89 (** parsing **)
    90 
    91 fun !!! scan =
    92   let
    93     val prefix = "Rail syntax error";
    94 
    95     fun get_pos [] = " (end-of-input)"
    96       | get_pos (tok :: _) = Position.here (pos_of tok);
    97 
    98     fun err (toks, NONE) = (fn () => prefix ^ get_pos toks)
    99       | err (toks, SOME msg) =
   100           (fn () =>
   101             let val s = msg () in
   102               if String.isPrefix prefix s then s
   103               else prefix ^ get_pos toks ^ ": " ^ s
   104             end);
   105   in Scan.!! err scan end;
   106 
   107 fun $$$ x =
   108   Scan.one (fn tok => kind_of tok = Keyword andalso content_of tok = x) ||
   109   Scan.fail_with
   110     (fn [] => (fn () => print_keyword x ^ " expected,\nbut end-of-input was found")
   111       | tok :: _ => (fn () => print_keyword x ^ " expected,\nbut " ^ print tok ^ " was found"));
   112 
   113 fun enum1 sep scan = scan ::: Scan.repeat ($$$ sep |-- !!! scan);
   114 fun enum sep scan = enum1 sep scan || Scan.succeed [];
   115 
   116 val ident = Scan.some (fn tok => if kind_of tok = Ident then SOME (content_of tok) else NONE);
   117 val string = Scan.some (fn tok => if kind_of tok = String then SOME (content_of tok) else NONE);
   118 
   119 val antiq = Scan.some (fn tok => (case kind_of tok of Antiq a => SOME a | _ => NONE));
   120 
   121 
   122 
   123 (** rail expressions **)
   124 
   125 (* datatype *)
   126 
   127 datatype rails =
   128   Cat of int * rail list
   129 and rail =
   130   Bar of rails list |
   131   Plus of rails * rails |
   132   Newline of int |
   133   Nonterminal of string |
   134   Terminal of bool * string |
   135   Antiquote of bool * Antiquote.antiq;
   136 
   137 fun reverse_cat (Cat (y, rails)) = Cat (y, rev (map reverse rails))
   138 and reverse (Bar cats) = Bar (map reverse_cat cats)
   139   | reverse (Plus (cat1, cat2)) = Plus (reverse_cat cat1, reverse_cat cat2)
   140   | reverse x = x;
   141 
   142 fun cat rails = Cat (0, rails);
   143 
   144 val empty = cat [];
   145 fun is_empty (Cat (_, [])) = true | is_empty _ = false;
   146 
   147 fun is_newline (Newline _) = true | is_newline _ = false;
   148 
   149 fun bar [Cat (_, [rail])] = rail
   150   | bar cats = Bar cats;
   151 
   152 fun plus cat1 cat2 = Plus (cat1, reverse_cat cat2);
   153 
   154 fun star cat1 cat2 =
   155   if is_empty cat2 then plus empty cat1
   156   else bar [empty, cat [plus cat1 cat2]];
   157 
   158 fun maybe rail = bar [empty, cat [rail]];
   159 
   160 
   161 (* read *)
   162 
   163 local
   164 
   165 val at_mode = Scan.option ($$$ "@") >> (fn NONE => false | _ => true);
   166 
   167 fun body x = (enum1 "|" body1 >> bar) x
   168 and body0 x = (enum "|" body1 >> bar) x
   169 and body1 x =
   170  (body2 :|-- (fn a =>
   171    $$$ "*" |-- !!! body4e >> (cat o single o star a) ||
   172    $$$ "+" |-- !!! body4e >> (cat o single o plus a) ||
   173    Scan.succeed a)) x
   174 and body2 x = (Scan.repeat1 body3 >> cat) x
   175 and body3 x = (body4 :|-- (fn a => $$$ "?" >> K (maybe a) || Scan.succeed a)) x
   176 and body4 x =
   177  ($$$ "(" |-- !!! (body0 --| $$$ ")") ||
   178   $$$ "\<newline>" >> K (Newline 0) ||
   179   ident >> Nonterminal ||
   180   at_mode -- string >> Terminal ||
   181   at_mode -- antiq >> Antiquote) x
   182 and body4e x = (Scan.option body4 >> (cat o the_list)) x;
   183 
   184 val rule_name = ident >> Antiquote.Text || antiq >> Antiquote.Antiq;
   185 val rule = rule_name -- ($$$ ":" |-- !!! body) || body >> pair (Antiquote.Text "");
   186 val rules = enum1 ";" (Scan.option rule) >> map_filter I;
   187 
   188 in
   189 
   190 val read =
   191   #1 o Scan.error (Scan.finite stopper (rules --| !!! (Scan.ahead (Scan.one is_eof)))) o tokenize;
   192 
   193 end;
   194 
   195 
   196 (* latex output *)
   197 
   198 local
   199 
   200 fun vertical_range_cat (Cat (_, rails)) y =
   201   let val (rails', (_, y')) =
   202     fold_map (fn rail => fn (y0, y') =>
   203       if is_newline rail then (Newline (y' + 1), (y' + 1, y' + 2))
   204       else
   205         let val (rail', y0') = vertical_range rail y0;
   206         in (rail', (y0, Int.max (y0', y'))) end) rails (y, y + 1)
   207   in (Cat (y, rails'), y') end
   208 
   209 and vertical_range (Bar cats) y =
   210       let val (cats', y') = fold_map vertical_range_cat cats y
   211       in (Bar cats', Int.max (y + 1, y')) end
   212   | vertical_range (Plus (cat1, cat2)) y =
   213       let val ([cat1', cat2'], y') = fold_map vertical_range_cat [cat1, cat2] y;
   214       in (Plus (cat1', cat2'), Int.max (y + 1, y')) end
   215   | vertical_range (Newline _) y = (Newline (y + 2), y + 3)
   216   | vertical_range atom y = (atom, y + 1);
   217 
   218 fun output_rules state rules =
   219   let
   220     val output_antiq = Thy_Output.eval_antiq (#1 (Keyword.get_lexicons ())) state;
   221     fun output_text b s =
   222       Output.output s
   223       |> b ? enclose "\\isakeyword{" "}"
   224       |> enclose "\\isa{" "}";
   225 
   226     fun output_cat c (Cat (_, rails)) = outputs c rails
   227     and outputs c [rail] = output c rail
   228       | outputs _ rails = implode (map (output "") rails)
   229     and output _ (Bar []) = ""
   230       | output c (Bar [cat]) = output_cat c cat
   231       | output _ (Bar (cat :: cats)) =
   232           "\\rail@bar\n" ^ output_cat "" cat ^
   233           implode (map (fn Cat (y, rails) =>
   234               "\\rail@nextbar{" ^ string_of_int y ^ "}\n" ^ outputs "" rails) cats) ^
   235           "\\rail@endbar\n"
   236       | output c (Plus (cat, Cat (y, rails))) =
   237           "\\rail@plus\n" ^ output_cat c cat ^
   238           "\\rail@nextplus{" ^ string_of_int y ^ "}\n" ^ outputs "c" rails ^
   239           "\\rail@endplus\n"
   240       | output _ (Newline y) = "\\rail@cr{" ^ string_of_int y ^ "}\n"
   241       | output c (Nonterminal s) = "\\rail@" ^ c ^ "nont{" ^ output_text false s ^ "}[]\n"
   242       | output c (Terminal (b, s)) = "\\rail@" ^ c ^ "term{" ^ output_text b s ^ "}[]\n"
   243       | output c (Antiquote (b, a)) =
   244           "\\rail@" ^ c ^ (if b then "term{" else "nont{") ^ output_antiq a ^ "}[]\n";
   245 
   246     fun output_rule (name, rail) =
   247       let
   248         val (rail', y') = vertical_range rail 0;
   249         val out_name =
   250           (case name of
   251             Antiquote.Text "" => ""
   252           | Antiquote.Text s => output_text false s
   253           | Antiquote.Antiq a => output_antiq a);
   254       in
   255         "\\rail@begin{" ^ string_of_int y' ^ "}{" ^ out_name ^ "}\n" ^
   256         output "" rail' ^
   257         "\\rail@end\n"
   258       end;
   259   in
   260     "\\begin{railoutput}\n" ^
   261     implode (map output_rule rules) ^
   262     "\\end{railoutput}\n"
   263   end;
   264 
   265 in
   266 
   267 val _ = Theory.setup
   268   (Thy_Output.antiquotation @{binding rail}
   269     (Scan.lift (Parse.source_position (Parse.string || Parse.cartouche)))
   270     (fn {state, ...} => output_rules state o read));
   271 
   272 end;
   273 
   274 end;
   275