| author | wenzelm | 
| Sun, 14 Jun 2015 23:22:08 +0200 | |
| changeset 60477 | 051b200f7578 | 
| parent 59196 | 73a6403637b3 | 
| child 61466 | 9a468c3a1fa1 | 
| permissions | -rw-r--r-- | 
| 11523 | 1 | (* Title: Pure/General/scan.ML | 
| 2 | Author: Markus Wenzel and Tobias Nipkow, TU Muenchen | |
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 3 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 4 | Generic scanners (for potentially infinite input). | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 5 | *) | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 6 | |
| 24025 | 7 | infix 5 -- :-- :|-- |-- --| ^^; | 
| 25999 | 8 | infixr 5 ::: @@@; | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 9 | infix 3 >>; | 
| 23699 | 10 | infixr 0 ||; | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 11 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 12 | signature BASIC_SCAN = | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 13 | sig | 
| 43947 
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
 wenzelm parents: 
40627diff
changeset | 14 | type message = unit -> string | 
| 14677 | 15 | (*error msg handler*) | 
| 43947 
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
 wenzelm parents: 
40627diff
changeset | 16 |   val !! : ('a * message option -> message) -> ('a -> 'b) -> 'a -> 'b
 | 
| 14677 | 17 | (*apply function*) | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 18 |   val >> : ('a -> 'b * 'c) * ('b -> 'd) -> 'a -> 'd * 'c
 | 
| 14677 | 19 | (*alternative*) | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 20 |   val || : ('a -> 'b) * ('a -> 'b) -> 'a -> 'b
 | 
| 14677 | 21 | (*sequential pairing*) | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 22 |   val -- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e
 | 
| 14677 | 23 | (*dependent pairing*) | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 24 |   val :-- : ('a -> 'b * 'c) * ('b -> 'c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e
 | 
| 24025 | 25 | (*projections*) | 
| 26 |   val :|-- : ('a -> 'b * 'c) * ('b -> 'c -> 'd * 'e) -> 'a -> 'd * 'e
 | |
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 27 |   val |-- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> 'd * 'e
 | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 28 |   val --| : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> 'b * 'e
 | 
| 14677 | 29 | (*concatenation*) | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 30 |   val ^^ : ('a -> string * 'b) * ('b -> string * 'c) -> 'a -> string * 'c
 | 
| 25999 | 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
 | |
| 14677 | 33 | (*one element literal*) | 
| 19291 | 34 | val $$ : string -> string list -> string * string list | 
| 19306 | 35 | val ~$$ : string -> string list -> string * string list | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 36 | end; | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 37 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 38 | signature SCAN = | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 39 | sig | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 40 | include BASIC_SCAN | 
| 48743 
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
 wenzelm parents: 
43947diff
changeset | 41 |   val permissive: ('a -> 'b) -> 'a -> 'b
 | 
| 23699 | 42 |   val error: ('a -> 'b) -> 'a -> 'b
 | 
| 43 |   val catch: ('a -> 'b) -> 'a -> 'b    (*exception Fail*)
 | |
| 58864 | 44 |   val recover: ('a -> 'b) -> (string -> 'a -> 'b) -> 'a -> 'b
 | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 45 | val fail: 'a -> 'b | 
| 43947 
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
 wenzelm parents: 
40627diff
changeset | 46 |   val fail_with: ('a -> message) -> 'a -> 'b
 | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 47 | val succeed: 'a -> 'b -> 'a * 'b | 
| 15664 | 48 |   val some: ('a -> 'b option) -> 'a list -> 'b * 'a list
 | 
| 49 |   val one: ('a -> bool) -> 'a list -> 'a * 'a list
 | |
| 19291 | 50 | val this: string list -> string list -> string list * string list | 
| 14927 | 51 | val this_string: string -> string list -> string * string list | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19473diff
changeset | 52 |   val many: ('a -> bool) -> 'a list -> 'a list * 'a list
 | 
| 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19473diff
changeset | 53 |   val many1: ('a -> bool) -> 'a list -> 'a list * 'a list
 | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 54 |   val optional: ('a -> 'b * 'a) -> 'b -> 'a -> 'b * 'a
 | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 55 |   val option: ('a -> 'b * 'a) -> 'a -> 'b option * 'a
 | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 56 |   val repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
 | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 57 |   val repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
 | 
| 23699 | 58 |   val single: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
 | 
| 59 |   val bulk: ('a -> 'b * 'a) -> 'a -> 'b list * 'a
 | |
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 60 |   val max: ('a * 'a -> bool) -> ('b -> 'a * 'b) -> ('b -> 'a * 'b) -> 'b -> 'a * 'b
 | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 61 |   val ahead: ('a -> 'b * 'c) -> 'a -> 'b * 'a
 | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 62 |   val unless: ('a -> 'b * 'a) -> ('a -> 'c * 'd) -> 'a -> 'c * 'd
 | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 63 |   val first: ('a -> 'b) list -> 'a -> 'b
 | 
| 14677 | 64 |   val state: 'a * 'b -> 'a * ('a * 'b)
 | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 65 |   val depend: ('a -> 'b -> ('c * 'd) * 'e) -> 'a * 'b -> 'd * ('c * 'e)
 | 
| 15664 | 66 |   val peek: ('a -> 'b -> 'c * 'd) -> 'a * 'b -> 'c * ('a * 'd)
 | 
| 55104 
8284c0d5bf52
clarified scan_cartouche_depth, according to Scala version;
 wenzelm parents: 
48743diff
changeset | 67 |   val provide: ('a -> bool) -> 'b -> ('b * 'c -> 'd * ('a * 'e)) -> 'c -> 'd * 'e
 | 
| 15664 | 68 |   val pass: 'a -> ('a * 'b -> 'c * ('d * 'e)) -> 'b -> 'c * 'e
 | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 69 |   val lift: ('a -> 'b * 'c) -> 'd * 'a -> 'b * ('d * 'c)
 | 
| 23699 | 70 |   val unlift: (unit * 'a -> 'b * ('c * 'd)) -> 'a -> 'b * 'd
 | 
| 15664 | 71 |   val trace: ('a list -> 'b * 'c list) -> 'a list -> ('b * 'a list) * 'c list
 | 
| 27731 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 72 | type 'a stopper | 
| 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 73 |   val stopper: ('a list -> 'a) -> ('a -> bool) -> 'a stopper
 | 
| 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 74 | val is_stopper: 'a stopper -> 'a -> bool | 
| 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 75 |   val finite': 'a stopper -> ('b * 'a list -> 'c * ('d * 'a list))
 | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 76 |     -> 'b * 'a list -> 'c * ('d * 'a list)
 | 
| 27731 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 77 |   val finite: 'a stopper -> ('a list -> 'b * 'a list) -> 'a list -> 'b * 'a list
 | 
| 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 78 |   val read: 'a stopper -> ('a list -> 'b * 'a list) -> 'a list -> 'b option
 | 
| 58850 | 79 |   val drain: ('a -> 'b list * 'a) -> 'b stopper -> ('c * 'b list -> 'd * ('e * 'b list)) ->
 | 
| 80 |     ('c * 'b list) * 'a -> ('d * ('e * 'b list)) * 'a
 | |
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 81 | type lexicon | 
| 27765 
5df443dd9deb
reorganized lexicon: allow scanning of annotated symbols, tuned representation and interfaces;
 wenzelm parents: 
27731diff
changeset | 82 | val is_literal: lexicon -> string list -> bool | 
| 
5df443dd9deb
reorganized lexicon: allow scanning of annotated symbols, tuned representation and interfaces;
 wenzelm parents: 
27731diff
changeset | 83 | val literal: lexicon -> (string * 'a) list -> (string * 'a) list * (string * 'a) list | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 84 | val empty_lexicon: lexicon | 
| 27765 
5df443dd9deb
reorganized lexicon: allow scanning of annotated symbols, tuned representation and interfaces;
 wenzelm parents: 
27731diff
changeset | 85 | val extend_lexicon: string list -> lexicon -> lexicon | 
| 
5df443dd9deb
reorganized lexicon: allow scanning of annotated symbols, tuned representation and interfaces;
 wenzelm parents: 
27731diff
changeset | 86 | val make_lexicon: string list list -> lexicon | 
| 
5df443dd9deb
reorganized lexicon: allow scanning of annotated symbols, tuned representation and interfaces;
 wenzelm parents: 
27731diff
changeset | 87 | val dest_lexicon: lexicon -> string list | 
| 
5df443dd9deb
reorganized lexicon: allow scanning of annotated symbols, tuned representation and interfaces;
 wenzelm parents: 
27731diff
changeset | 88 | val merge_lexicons: lexicon * lexicon -> lexicon | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 89 | end; | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 90 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 91 | structure Scan: SCAN = | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 92 | struct | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 93 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 94 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 95 | (** scanners **) | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 96 | |
| 23699 | 97 | (* exceptions *) | 
| 98 | ||
| 43947 
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
 wenzelm parents: 
40627diff
changeset | 99 | type message = unit -> string; | 
| 
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
 wenzelm parents: 
40627diff
changeset | 100 | |
| 58850 | 101 | exception MORE of unit; (*need more input*) | 
| 102 | exception FAIL of message option; (*try alternatives (reason of failure)*) | |
| 103 | exception ABORT of message; (*dead end*) | |
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 104 | |
| 23699 | 105 | fun !! err scan xs = scan xs handle FAIL msg => raise ABORT (err (xs, msg)); | 
| 58850 | 106 | fun permissive scan xs = scan xs handle MORE () => raise FAIL NONE | ABORT _ => raise FAIL NONE; | 
| 107 | fun strict scan xs = scan xs handle MORE () => raise FAIL NONE; | |
| 43947 
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
 wenzelm parents: 
40627diff
changeset | 108 | fun error scan xs = scan xs handle ABORT msg => Library.error (msg ()); | 
| 23699 | 109 | |
| 110 | fun catch scan xs = scan xs | |
| 43947 
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
 wenzelm parents: 
40627diff
changeset | 111 | handle ABORT msg => raise Fail (msg ()) | 
| 
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
 wenzelm parents: 
40627diff
changeset | 112 | | FAIL msg => raise Fail (case msg of NONE => "Syntax error" | SOME m => m ()); | 
| 23699 | 113 | |
| 58864 | 114 | fun recover scan1 scan2 xs = | 
| 115 | catch scan1 xs handle Fail msg => scan2 msg xs; | |
| 116 | ||
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 117 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 118 | (* scanner combinators *) | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 119 | |
| 19306 | 120 | fun (scan >> f) xs = scan xs |>> f; | 
| 14078 | 121 | |
| 19306 | 122 | fun (scan1 || scan2) xs = scan1 xs handle FAIL _ => scan2 xs; | 
| 14078 | 123 | |
| 19306 | 124 | fun (scan1 :-- scan2) xs = | 
| 14108 
eaf3c75f2c8e
Restored old (tail recursive!) version of repeat.
 berghofe parents: 
14078diff
changeset | 125 | let | 
| 19306 | 126 | val (x, ys) = scan1 xs; | 
| 127 | val (y, zs) = scan2 x ys; | |
| 128 | in ((x, y), zs) end; | |
| 14078 | 129 | |
| 19306 | 130 | fun (scan1 -- scan2) = scan1 :-- (fn _ => scan2); | 
| 24025 | 131 | fun (scan1 :|-- scan2) = scan1 :-- scan2 >> #2; | 
| 19306 | 132 | fun (scan1 |-- scan2) = scan1 -- scan2 >> #2; | 
| 133 | fun (scan1 --| scan2) = scan1 -- scan2 >> #1; | |
| 134 | fun (scan1 ^^ scan2) = scan1 -- scan2 >> op ^; | |
| 25999 | 135 | fun (scan1 ::: scan2) = scan1 -- scan2 >> op ::; | 
| 136 | fun (scan1 @@@ scan2) = scan1 -- scan2 >> op @; | |
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 137 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 138 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 139 | (* generic scanners *) | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 140 | |
| 15531 | 141 | fun fail _ = raise FAIL NONE; | 
| 142 | fun fail_with msg_of xs = raise FAIL (SOME (msg_of xs)); | |
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 143 | fun succeed y xs = (y, xs); | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 144 | |
| 58850 | 145 | fun some _ [] = raise MORE () | 
| 15664 | 146 | | some f (x :: xs) = | 
| 147 | (case f x of SOME y => (y, xs) | _ => raise FAIL NONE); | |
| 148 | ||
| 58850 | 149 | fun one _ [] = raise MORE () | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 150 | | one pred (x :: xs) = | 
| 15531 | 151 | if pred x then (x, xs) else raise FAIL NONE; | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 152 | |
| 19306 | 153 | fun $$ a = one (fn s: string => s = a); | 
| 154 | fun ~$$ a = one (fn s: string => s <> a); | |
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 155 | |
| 14833 | 156 | fun this ys xs = | 
| 14726 | 157 | let | 
| 158 | fun drop_prefix [] xs = xs | |
| 58850 | 159 | | drop_prefix (_ :: _) [] = raise MORE () | 
| 14726 | 160 | | drop_prefix (y :: ys) (x :: xs) = | 
| 19291 | 161 | if (y: string) = x then drop_prefix ys xs else raise FAIL NONE; | 
| 14726 | 162 | in (ys, drop_prefix ys xs) end; | 
| 163 | ||
| 40627 
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
 wenzelm parents: 
38875diff
changeset | 164 | fun this_string s = this (raw_explode s) >> K s; (*primitive string -- no symbols here!*) | 
| 14907 | 165 | |
| 58850 | 166 | fun many _ [] = raise MORE () | 
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19473diff
changeset | 167 | | many pred (lst as x :: xs) = | 
| 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
19473diff
changeset | 168 | if pred x then apfst (cons x) (many pred xs) | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 169 | else ([], lst); | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 170 | |
| 25999 | 171 | fun many1 pred = one pred ::: many pred; | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 172 | |
| 15664 | 173 | fun optional scan def = scan || succeed def; | 
| 174 | fun option scan = (scan >> SOME) || succeed NONE; | |
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 175 | |
| 13795 | 176 | fun repeat scan = | 
| 15664 | 177 | let | 
| 178 | fun rep ys xs = | |
| 179 | (case (SOME (scan xs) handle FAIL _ => NONE) of | |
| 180 | NONE => (rev ys, xs) | |
| 181 | | SOME (y, xs') => rep (y :: ys) xs'); | |
| 14108 
eaf3c75f2c8e
Restored old (tail recursive!) version of repeat.
 berghofe parents: 
14078diff
changeset | 182 | in rep [] end; | 
| 13795 | 183 | |
| 25999 | 184 | fun repeat1 scan = scan ::: repeat scan; | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 185 | |
| 23699 | 186 | fun single scan = scan >> (fn x => [x]); | 
| 187 | fun bulk scan = scan -- repeat (permissive scan) >> (op ::); | |
| 188 | ||
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 189 | fun max leq scan1 scan2 xs = | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 190 | (case (option scan1 xs, option scan2 xs) of | 
| 15531 | 191 | ((NONE, _), (NONE, _)) => raise FAIL NONE (*looses FAIL msg!*) | 
| 192 | | ((SOME tok1, xs'), (NONE, _)) => (tok1, xs') | |
| 193 | | ((NONE, _), (SOME tok2, xs')) => (tok2, xs') | |
| 194 | | ((SOME tok1, xs1'), (SOME tok2, xs2')) => | |
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 195 | if leq (tok2, tok1) then (tok1, xs1') else (tok2, xs2')); | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 196 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 197 | fun ahead scan xs = (fst (scan xs), xs); | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 198 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 199 | fun unless test scan = | 
| 15531 | 200 | ahead (option test) :-- (fn NONE => scan | _ => fail) >> #2; | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 201 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 202 | fun first [] = fail | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 203 | | first (scan :: scans) = scan || first scans; | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 204 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 205 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 206 | (* state based scanners *) | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 207 | |
| 9122 | 208 | fun state (st, xs) = (st, (st, xs)); | 
| 209 | ||
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 210 | fun depend scan (st, xs) = | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 211 | let val ((st', y), xs') = scan st xs | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 212 | in (y, (st', xs')) end; | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 213 | |
| 15664 | 214 | fun peek scan = depend (fn st => scan st >> pair st); | 
| 215 | ||
| 55104 
8284c0d5bf52
clarified scan_cartouche_depth, according to Scala version;
 wenzelm parents: 
48743diff
changeset | 216 | fun provide pred st scan xs = | 
| 
8284c0d5bf52
clarified scan_cartouche_depth, according to Scala version;
 wenzelm parents: 
48743diff
changeset | 217 | let val (y, (st', xs')) = scan (st, xs) | 
| 
8284c0d5bf52
clarified scan_cartouche_depth, according to Scala version;
 wenzelm parents: 
48743diff
changeset | 218 | in if pred st' then (y, xs') else fail () end; | 
| 
8284c0d5bf52
clarified scan_cartouche_depth, according to Scala version;
 wenzelm parents: 
48743diff
changeset | 219 | |
| 
8284c0d5bf52
clarified scan_cartouche_depth, according to Scala version;
 wenzelm parents: 
48743diff
changeset | 220 | fun pass st = provide (K true) st; | 
| 15664 | 221 | |
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 222 | fun lift scan (st, xs) = | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 223 | let val (y, xs') = scan xs | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 224 | in (y, (st, xs')) end; | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 225 | |
| 15664 | 226 | fun unlift scan = pass () scan; | 
| 227 | ||
| 228 | ||
| 229 | (* trace input *) | |
| 230 | ||
| 23699 | 231 | fun trace scan xs = | 
| 232 | let val (y, xs') = scan xs | |
| 33957 | 233 | in ((y, take (length xs - length xs') xs), xs') end; | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 234 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 235 | |
| 27731 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 236 | (* stopper *) | 
| 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 237 | |
| 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 238 | datatype 'a stopper = Stopper of ('a list -> 'a) * ('a -> bool);
 | 
| 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 239 | |
| 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 240 | fun stopper mk_stopper is_stopper = Stopper (mk_stopper, is_stopper); | 
| 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 241 | fun is_stopper (Stopper (_, is_stopper)) = is_stopper; | 
| 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 242 | |
| 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 243 | |
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 244 | (* finite scans *) | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 245 | |
| 27731 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 246 | fun finite' (Stopper (mk_stopper, is_stopper)) scan (state, input) = | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 247 | let | 
| 43947 
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
 wenzelm parents: 
40627diff
changeset | 248 | fun lost () = raise ABORT (fn () => "Bad scanner: lost stopper of finite scan!"); | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 249 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 250 | fun stop [] = lost () | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 251 | | stop lst = | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 252 | let val (xs, x) = split_last lst | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 253 | in if is_stopper x then ((), xs) else lost () end; | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 254 | in | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 255 | if exists is_stopper input then | 
| 43947 
9b00f09f7721
defer evaluation of Scan.message, for improved performance in the frequent situation where failure is handled later (e.g. via ||);
 wenzelm parents: 
40627diff
changeset | 256 | raise ABORT (fn () => "Stopper may not occur in input of finite scan!") | 
| 27731 
a7444ded92cf
abstract type stopper, may depend on final input;
 wenzelm parents: 
25999diff
changeset | 257 | else (strict scan --| lift stop) (state, input @ [mk_stopper input]) | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 258 | end; | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 259 | |
| 15664 | 260 | fun finite stopper scan = unlift (finite' stopper (lift scan)); | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 261 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 262 | fun read stopper scan xs = | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 263 | (case error (finite stopper (option scan)) xs of | 
| 15531 | 264 | (y as SOME _, []) => y | 
| 265 | | _ => NONE); | |
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 266 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 267 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 268 | (* infinite scans -- draining state-based source *) | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 269 | |
| 58850 | 270 | fun drain get stopper scan ((state, xs), src) = | 
| 271 | (scan (state, xs), src) handle MORE () => | |
| 272 | (case get src of | |
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 273 | ([], _) => (finite' stopper scan (state, xs), src) | 
| 58850 | 274 | | (xs', src') => drain get stopper scan ((state, xs @ xs'), src')); | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 275 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 276 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 277 | |
| 27782 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 278 | (** datatype lexicon -- position tree **) | 
| 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 279 | |
| 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 280 | datatype lexicon = Lexicon of (bool * lexicon) Symtab.table; | 
| 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 281 | |
| 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 282 | val empty_lexicon = Lexicon Symtab.empty; | 
| 59071 | 283 | fun is_empty_lexicon (Lexicon tab) = Symtab.is_empty tab; | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 284 | |
| 27782 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 285 | fun is_literal _ [] = false | 
| 32784 | 286 | | is_literal (Lexicon tab) (c :: cs) = | 
| 27782 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 287 | (case Symtab.lookup tab c of | 
| 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 288 | SOME (tip, lex) => tip andalso null cs orelse is_literal lex cs | 
| 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 289 | | NONE => false); | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 290 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 291 | |
| 27782 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 292 | (* scan longest match *) | 
| 27765 
5df443dd9deb
reorganized lexicon: allow scanning of annotated symbols, tuned representation and interfaces;
 wenzelm parents: 
27731diff
changeset | 293 | |
| 27782 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 294 | fun literal lexicon = | 
| 27765 
5df443dd9deb
reorganized lexicon: allow scanning of annotated symbols, tuned representation and interfaces;
 wenzelm parents: 
27731diff
changeset | 295 | let | 
| 27782 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 296 | fun finish (SOME (res, rest)) = (rev res, rest) | 
| 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 297 | | finish NONE = raise FAIL NONE; | 
| 58850 | 298 | fun scan _ res (Lexicon tab) [] = | 
| 299 | if Symtab.is_empty tab then finish res else raise MORE () | |
| 32784 | 300 | | scan path res (Lexicon tab) (c :: cs) = | 
| 27782 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 301 | (case Symtab.lookup tab (fst c) of | 
| 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 302 | SOME (tip, lex) => | 
| 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 303 | let val path' = c :: path | 
| 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 304 | in scan path' (if tip then SOME (path', cs) else res) lex cs end | 
| 27784 | 305 | | NONE => finish res); | 
| 27782 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 306 | in scan [] NONE lexicon end; | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 307 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 308 | |
| 27765 
5df443dd9deb
reorganized lexicon: allow scanning of annotated symbols, tuned representation and interfaces;
 wenzelm parents: 
27731diff
changeset | 309 | (* build lexicons *) | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 310 | |
| 27765 
5df443dd9deb
reorganized lexicon: allow scanning of annotated symbols, tuned representation and interfaces;
 wenzelm parents: 
27731diff
changeset | 311 | fun extend_lexicon chrs lexicon = | 
| 
5df443dd9deb
reorganized lexicon: allow scanning of annotated symbols, tuned representation and interfaces;
 wenzelm parents: 
27731diff
changeset | 312 | let | 
| 27782 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 313 | fun ext [] lex = lex | 
| 32784 | 314 | | ext (c :: cs) (Lexicon tab) = | 
| 27782 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 315 | (case Symtab.lookup tab c of | 
| 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 316 | SOME (tip, lex) => Lexicon (Symtab.update (c, (tip orelse null cs, ext cs lex)) tab) | 
| 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 317 | | NONE => Lexicon (Symtab.update (c, (null cs, ext cs empty_lexicon)) tab)); | 
| 27765 
5df443dd9deb
reorganized lexicon: allow scanning of annotated symbols, tuned representation and interfaces;
 wenzelm parents: 
27731diff
changeset | 318 | in if is_literal lexicon chrs then lexicon else ext chrs lexicon end; | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 319 | |
| 27765 
5df443dd9deb
reorganized lexicon: allow scanning of annotated symbols, tuned representation and interfaces;
 wenzelm parents: 
27731diff
changeset | 320 | fun make_lexicon chrss = fold extend_lexicon chrss empty_lexicon; | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 321 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 322 | |
| 27765 
5df443dd9deb
reorganized lexicon: allow scanning of annotated symbols, tuned representation and interfaces;
 wenzelm parents: 
27731diff
changeset | 323 | (* merge lexicons *) | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 324 | |
| 27782 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 325 | fun dest path (Lexicon tab) = Symtab.fold (fn (d, (tip, lex)) => | 
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 326 | let | 
| 27782 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 327 | val path' = d :: path; | 
| 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 328 | val content = dest path' lex; | 
| 
377810fd718e
datatype lexicon: alternative representation using nested Symtab.table;
 wenzelm parents: 
27765diff
changeset | 329 | in append (if tip then rev path' :: content else content) end) tab []; | 
| 27765 
5df443dd9deb
reorganized lexicon: allow scanning of annotated symbols, tuned representation and interfaces;
 wenzelm parents: 
27731diff
changeset | 330 | |
| 59196 | 331 | val dest_lexicon = sort_strings o map implode o dest []; | 
| 59071 | 332 | |
| 333 | fun merge_lexicons (lex1, lex2) = | |
| 334 | if pointer_eq (lex1, lex2) then lex1 | |
| 335 | else if is_empty_lexicon lex1 then lex2 | |
| 336 | else fold extend_lexicon (dest [] lex2) lex1; | |
| 6116 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 337 | |
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 338 | end; | 
| 
8ba2f25610f7
files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
 wenzelm parents: diff
changeset | 339 | |
| 37216 
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
 wenzelm parents: 
33957diff
changeset | 340 | structure Basic_Scan: BASIC_SCAN = Scan; | 
| 
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
 wenzelm parents: 
33957diff
changeset | 341 | open Basic_Scan; |