src/Pure/General/scan.ML
author wenzelm
Sat, 23 Jul 2011 16:37:17 +0200
changeset 44818 9b00f09f7721
parent 40875 becf5d5187cc
child 49758 a72f8ffecf31
permissions -rw-r--r--
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
     1 (*  Title:      Pure/General/scan.ML
     2     Author:     Markus Wenzel and Tobias Nipkow, TU Muenchen
     3 
     4 Generic scanners (for potentially infinite input).
     5 *)
     6 
     7 infix 5 -- :-- :|-- |-- --| ^^;
     8 infixr 5 ::: @@@;
     9 infix 3 >>;
    10 infixr 0 ||;
    11 
    12 signature BASIC_SCAN =
    13 sig
    14   type message = unit -> string
    15   (*error msg handler*)
    16   val !! : ('a * message option -> message) -> ('a -> 'b) -> 'a -> 'b
    17   (*apply function*)
    18   val >> : ('a -> 'b * 'c) * ('b -> 'd) -> 'a -> 'd * 'c
    19   (*alternative*)
    20   val || : ('a -> 'b) * ('a -> 'b) -> 'a -> 'b
    21   (*sequential pairing*)
    22   val -- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e
    23   (*dependent pairing*)
    24   val :-- : ('a -> 'b * 'c) * ('b -> 'c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e
    25   (*projections*)
    26   val :|-- : ('a -> 'b * 'c) * ('b -> 'c -> 'd * 'e) -> 'a -> 'd * 'e
    27   val |-- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> 'd * 'e
    28   val --| : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> 'b * 'e
    29   (*concatenation*)
    30   val ^^ : ('a -> string * 'b) * ('b -> string * 'c) -> 'a -> string * 'c
    31   val ::: : ('a -> 'b * 'c) * ('c -> 'b list * 'd) -> 'a -> 'b list * 'd
    32   val @@@ : ('a -> 'b list * 'c) * ('c -> 'b list * 'd) -> 'a -> 'b list * 'd
    33   (*one element literal*)
    34   val $$ : string -> string list -> string * string list
    35   val ~$$ : string -> string list -> string * string list
    36 end;
    37 
    38 signature SCAN =
    39 sig
    40   include BASIC_SCAN
    41   val prompt: string -> ('a -> 'b) -> 'a -> 'b
    42   val error: ('a -> 'b) -> 'a -> 'b
    43   val catch: ('a -> 'b) -> 'a -> 'b    (*exception Fail*)
    44   val fail: 'a -> 'b
    45   val fail_with: ('a -> message) -> 'a -> 'b
    46   val succeed: 'a -> 'b -> 'a * 'b
    47   val some: ('a -> 'b option) -> 'a list -> 'b * 'a list
    48   val one: ('a -> bool) -> 'a list -> 'a * 'a list
    49   val this: string list -> string list -> string list * string list
    50   val this_string: string -> string list -> string * string list
    51   val many: ('a -> bool) -> 'a list -> 'a list * 'a list
    52   val many1: ('a -> bool) -> 'a list -> 'a list * 'a list
    53   val optional: ('a -> 'b * 'a) -> 'b -> 'a -> 'b * 'a
    54   val option: ('a -> 'b * 'a) -> 'a -> 'b option * 'a
    55   val repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
    56   val repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
    57   val single: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
    58   val bulk: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
    59   val max: ('a * 'a -> bool) -> ('b -> 'a * 'b) -> ('b -> 'a * 'b) -> 'b -> 'a * 'b
    60   val ahead: ('a -> 'b * 'c) -> 'a -> 'b * 'a
    61   val unless: ('a -> 'b * 'a) -> ('a -> 'c * 'd) -> 'a -> 'c * 'd
    62   val first: ('a -> 'b) list -> 'a -> 'b
    63   val state: 'a * 'b -> 'a * ('a * 'b)
    64   val depend: ('a -> 'b -> ('c * 'd) * 'e) -> 'a * 'b -> 'd * ('c * 'e)
    65   val peek: ('a -> 'b -> 'c * 'd) -> 'a * 'b -> 'c * ('a * 'd)
    66   val pass: 'a -> ('a * 'b -> 'c * ('d * 'e)) -> 'b -> 'c * 'e
    67   val lift: ('a -> 'b * 'c) -> 'd * 'a -> 'b * ('d * 'c)
    68   val unlift: (unit * 'a -> 'b * ('c * 'd)) -> 'a -> 'b * 'd
    69   val trace: ('a list -> 'b * 'c list) -> 'a list -> ('b * 'a list) * 'c list
    70   type 'a stopper
    71   val stopper: ('a list -> 'a) -> ('a -> bool) -> 'a stopper
    72   val is_stopper: 'a stopper -> 'a -> bool
    73   val finite': 'a stopper -> ('b * 'a list -> 'c * ('d * 'a list))
    74     -> 'b * 'a list -> 'c * ('d * 'a list)
    75   val finite: 'a stopper -> ('a list -> 'b * 'a list) -> 'a list -> 'b * 'a list
    76   val read: 'a stopper -> ('a list -> 'b * 'a list) -> 'a list -> 'b option
    77   val drain: string -> (string -> 'a -> 'b list * 'a) -> 'b stopper ->
    78     ('c * 'b list -> 'd * ('e * 'b list)) -> ('c * 'b list) * 'a -> ('d * ('e * 'b list)) * 'a
    79   type lexicon
    80   val is_literal: lexicon -> string list -> bool
    81   val literal: lexicon -> (string * 'a) list -> (string * 'a) list * (string * 'a) list
    82   val empty_lexicon: lexicon
    83   val extend_lexicon: string list -> lexicon -> lexicon
    84   val make_lexicon: string list list -> lexicon
    85   val dest_lexicon: lexicon -> string list
    86   val merge_lexicons: lexicon * lexicon -> lexicon
    87 end;
    88 
    89 structure Scan: SCAN =
    90 struct
    91 
    92 
    93 (** scanners **)
    94 
    95 (* exceptions *)
    96 
    97 type message = unit -> string;
    98 
    99 exception MORE of string option;        (*need more input (prompt)*)
   100 exception FAIL of message option;       (*try alternatives (reason of failure)*)
   101 exception ABORT of message;             (*dead end*)
   102 
   103 fun !! err scan xs = scan xs handle FAIL msg => raise ABORT (err (xs, msg));
   104 fun permissive scan xs = scan xs handle MORE _ => raise FAIL NONE | ABORT _ => raise FAIL NONE;
   105 fun strict scan xs = scan xs handle MORE _ => raise FAIL NONE;
   106 fun prompt str scan xs = scan xs handle MORE NONE => raise MORE (SOME str);
   107 fun error scan xs = scan xs handle ABORT msg => Library.error (msg ());
   108 
   109 fun catch scan xs = scan xs
   110   handle ABORT msg => raise Fail (msg ())
   111     | FAIL msg => raise Fail (case msg of NONE => "Syntax error" | SOME m => m ());
   112 
   113 
   114 (* scanner combinators *)
   115 
   116 fun (scan >> f) xs = scan xs |>> f;
   117 
   118 fun (scan1 || scan2) xs = scan1 xs handle FAIL _ => scan2 xs;
   119 
   120 fun (scan1 :-- scan2) xs =
   121   let
   122     val (x, ys) = scan1 xs;
   123     val (y, zs) = scan2 x ys;
   124   in ((x, y), zs) end;
   125 
   126 fun (scan1 -- scan2) = scan1 :-- (fn _ => scan2);
   127 fun (scan1 :|-- scan2) = scan1 :-- scan2 >> #2;
   128 fun (scan1 |-- scan2) = scan1 -- scan2 >> #2;
   129 fun (scan1 --| scan2) = scan1 -- scan2 >> #1;
   130 fun (scan1 ^^ scan2) = scan1 -- scan2 >> op ^;
   131 fun (scan1 ::: scan2) = scan1 -- scan2 >> op ::;
   132 fun (scan1 @@@ scan2) = scan1 -- scan2 >> op @;
   133 
   134 
   135 (* generic scanners *)
   136 
   137 fun fail _ = raise FAIL NONE;
   138 fun fail_with msg_of xs = raise FAIL (SOME (msg_of xs));
   139 fun succeed y xs = (y, xs);
   140 
   141 fun some _ [] = raise MORE NONE
   142   | some f (x :: xs) =
   143       (case f x of SOME y => (y, xs) | _ => raise FAIL NONE);
   144 
   145 fun one _ [] = raise MORE NONE
   146   | one pred (x :: xs) =
   147       if pred x then (x, xs) else raise FAIL NONE;
   148 
   149 fun $$ a = one (fn s: string => s = a);
   150 fun ~$$ a = one (fn s: string => s <> a);
   151 
   152 fun this ys xs =
   153   let
   154     fun drop_prefix [] xs = xs
   155       | drop_prefix (_ :: _) [] = raise MORE NONE
   156       | drop_prefix (y :: ys) (x :: xs) =
   157           if (y: string) = x then drop_prefix ys xs else raise FAIL NONE;
   158   in (ys, drop_prefix ys xs) end;
   159 
   160 fun this_string s = this (raw_explode s) >> K s;  (*primitive string -- no symbols here!*)
   161 
   162 fun many _ [] = raise MORE NONE
   163   | many pred (lst as x :: xs) =
   164       if pred x then apfst (cons x) (many pred xs)
   165       else ([], lst);
   166 
   167 fun many1 pred = one pred ::: many pred;
   168 
   169 fun optional scan def = scan || succeed def;
   170 fun option scan = (scan >> SOME) || succeed NONE;
   171 
   172 fun repeat scan =
   173   let
   174     fun rep ys xs =
   175       (case (SOME (scan xs) handle FAIL _ => NONE) of
   176         NONE => (rev ys, xs)
   177       | SOME (y, xs') => rep (y :: ys) xs');
   178   in rep [] end;
   179 
   180 fun repeat1 scan = scan ::: repeat scan;
   181 
   182 fun single scan = scan >> (fn x => [x]);
   183 fun bulk scan = scan -- repeat (permissive scan) >> (op ::);
   184 
   185 fun max leq scan1 scan2 xs =
   186   (case (option scan1 xs, option scan2 xs) of
   187     ((NONE, _), (NONE, _)) => raise FAIL NONE           (*looses FAIL msg!*)
   188   | ((SOME tok1, xs'), (NONE, _)) => (tok1, xs')
   189   | ((NONE, _), (SOME tok2, xs')) => (tok2, xs')
   190   | ((SOME tok1, xs1'), (SOME tok2, xs2')) =>
   191       if leq (tok2, tok1) then (tok1, xs1') else (tok2, xs2'));
   192 
   193 fun ahead scan xs = (fst (scan xs), xs);
   194 
   195 fun unless test scan =
   196   ahead (option test) :-- (fn NONE => scan | _ => fail) >> #2;
   197 
   198 fun first [] = fail
   199   | first (scan :: scans) = scan || first scans;
   200 
   201 
   202 (* state based scanners *)
   203 
   204 fun state (st, xs) = (st, (st, xs));
   205 
   206 fun depend scan (st, xs) =
   207   let val ((st', y), xs') = scan st xs
   208   in (y, (st', xs')) end;
   209 
   210 fun peek scan = depend (fn st => scan st >> pair st);
   211 
   212 fun pass st scan xs =
   213   let val (y, (_, xs')) = scan (st, xs)
   214   in (y, xs') end;
   215 
   216 fun lift scan (st, xs) =
   217   let val (y, xs') = scan xs
   218   in (y, (st, xs')) end;
   219 
   220 fun unlift scan = pass () scan;
   221 
   222 
   223 (* trace input *)
   224 
   225 fun trace scan xs =
   226   let val (y, xs') = scan xs
   227   in ((y, take (length xs - length xs') xs), xs') end;
   228 
   229 
   230 (* stopper *)
   231 
   232 datatype 'a stopper = Stopper of ('a list -> 'a) * ('a -> bool);
   233 
   234 fun stopper mk_stopper is_stopper = Stopper (mk_stopper, is_stopper);
   235 fun is_stopper (Stopper (_, is_stopper)) = is_stopper;
   236 
   237 
   238 (* finite scans *)
   239 
   240 fun finite' (Stopper (mk_stopper, is_stopper)) scan (state, input) =
   241   let
   242     fun lost () = raise ABORT (fn () => "Bad scanner: lost stopper of finite scan!");
   243 
   244     fun stop [] = lost ()
   245       | stop lst =
   246           let val (xs, x) = split_last lst
   247           in if is_stopper x then ((), xs) else lost () end;
   248   in
   249     if exists is_stopper input then
   250       raise ABORT (fn () => "Stopper may not occur in input of finite scan!")
   251     else (strict scan --| lift stop) (state, input @ [mk_stopper input])
   252   end;
   253 
   254 fun finite stopper scan = unlift (finite' stopper (lift scan));
   255 
   256 fun read stopper scan xs =
   257   (case error (finite stopper (option scan)) xs of
   258     (y as SOME _, []) => y
   259   | _ => NONE);
   260 
   261 
   262 (* infinite scans -- draining state-based source *)
   263 
   264 fun drain def_prompt get stopper scan ((state, xs), src) =
   265   (scan (state, xs), src) handle MORE prompt =>
   266     (case get (the_default def_prompt prompt) src of
   267       ([], _) => (finite' stopper scan (state, xs), src)
   268     | (xs', src') => drain def_prompt get stopper scan ((state, xs @ xs'), src'));
   269 
   270 
   271 
   272 (** datatype lexicon -- position tree **)
   273 
   274 datatype lexicon = Lexicon of (bool * lexicon) Symtab.table;
   275 
   276 val empty_lexicon = Lexicon Symtab.empty;
   277 
   278 fun is_literal _ [] = false
   279   | is_literal (Lexicon tab) (c :: cs) =
   280       (case Symtab.lookup tab c of
   281         SOME (tip, lex) => tip andalso null cs orelse is_literal lex cs
   282       | NONE => false);
   283 
   284 
   285 (* scan longest match *)
   286 
   287 fun literal lexicon =
   288   let
   289     fun finish (SOME (res, rest)) = (rev res, rest)
   290       | finish NONE = raise FAIL NONE;
   291     fun scan _ res (Lexicon tab) [] = if Symtab.is_empty tab then finish res else raise MORE NONE
   292       | scan path res (Lexicon tab) (c :: cs) =
   293           (case Symtab.lookup tab (fst c) of
   294             SOME (tip, lex) =>
   295               let val path' = c :: path
   296               in scan path' (if tip then SOME (path', cs) else res) lex cs end
   297           | NONE => finish res);
   298   in scan [] NONE lexicon end;
   299 
   300 
   301 (* build lexicons *)
   302 
   303 fun extend_lexicon chrs lexicon =
   304   let
   305     fun ext [] lex = lex
   306       | ext (c :: cs) (Lexicon tab) =
   307           (case Symtab.lookup tab c of
   308             SOME (tip, lex) => Lexicon (Symtab.update (c, (tip orelse null cs, ext cs lex)) tab)
   309           | NONE => Lexicon (Symtab.update (c, (null cs, ext cs empty_lexicon)) tab));
   310   in if is_literal lexicon chrs then lexicon else ext chrs lexicon end;
   311 
   312 fun make_lexicon chrss = fold extend_lexicon chrss empty_lexicon;
   313 
   314 
   315 (* merge lexicons *)
   316 
   317 fun dest path (Lexicon tab) = Symtab.fold (fn (d, (tip, lex)) =>
   318   let
   319     val path' = d :: path;
   320     val content = dest path' lex;
   321   in append (if tip then rev path' :: content else content) end) tab [];
   322 
   323 val dest_lexicon = map implode o dest [];
   324 fun merge_lexicons (lex1, lex2) = fold extend_lexicon (dest [] lex2) lex1;
   325 
   326 end;
   327 
   328 structure Basic_Scan: BASIC_SCAN = Scan;
   329 open Basic_Scan;